0.3.6 • Published 6 months ago

sarah-client v0.3.6

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

Sarah Client

This library is a JavaScript client for the Sarah chat API.

Installation

npm install sarah-client

Usage

import { ChatClient, Message } from 'sarah-client';

const chat = new ChatClient({
  endpoint: `/chat`,
  config: "foo",
  streaming: true,
  throttleTime: 50,
  onWaiting: waiting => setWaiting(waiting),
  onLoading: loading => setLoading(loading),
  onMessage: conversation => setConversation(conversation),
  onSearch: search => setSearch(search),
  onIntent: intent => setIntent(intent),
  onResults: results => setResults(results),
  onError: err => console.error(err)  
});

const userMessage = "Hello Sarah! What do you have in store for me today?";

chat.fetchAnswer(userMessage);

The configuration object passed to the ChatClient constructor has the following properties:

  • endpoint (required): Absolute or relative URL to the chat API.
  • config (required): Name of the chat configuration configured on the chat server.
  • bearerToken (optional): (⚠️ only for development purposes) Bearer token to authenticate the organization using the chat API.
  • streaming (optional, default: false): In streaming mode the search, results and messages are sent back in real time. The onMessage callback is called multiple times.
  • throttleTime (optional, default: 0): Time in milliseconds to wait before sending the next message. This is useful to avoid rendering too many messages in a short period of time.
  • onWaiting (optional): Callback function called before and after the chat gets a new message. In streaming mode, waiting is set to false at the beginning of the reception of a new message.
  • onLoading (optional): Callback function called before and after the chat gets a new message. In streaming mode, loading is set to false after the reception of a new message.
  • onMessage (optional): Callback function called when a new message is received from the chat API. The full list of message is passed as argument.
  • onSearch (optional): Callback function called when the chat API returns a search request. The search object is passed as argument.
  • onIntent (optional): Callback function called when the chat API returns an intent. The intent object is passed as argument.
  • onResults (optional): Callback function called when the chat API returns a list of results. The composition of the results depends on the server-side chat configuration. With Typescript the result object can be typed by specifying the type of the ChatClient generic parameter. e.g. ChatClient<SearchResult>.
  • onError (optional): Callback function called when an error occurs during the generation of an answer.

The ChatClient class exposes the following public methods:

  • fetchAnswer(message: string): Sends a message to the chat API and returns the answer.
  • logSignal(type: string, value: string|null, data?: any): Logs a "signal" to the chat API. This is useful to track user actions such as clicks on buttons.
  • abort(): Aborts the current request to the chat API and resets the client.
  • reset(): Resets the client. Must not be called while a request is in progress.
  • getConversation(): Returns the full conversation history.
  • getSearch(): Returns the last search request.
  • getIntent(): Returns the last intent.
  • getResults(): Returns the last list of results.
  • isLoading(): Returns true if a request is in progress.
  • isWaiting(): Returns true if the chat is waiting for a new message. (in streaming mode waiting may be false and loading may be true at the same time; when loading is false, waiting is false as well).
0.3.0

6 months ago

0.3.6

6 months ago

0.3.5

6 months ago

0.3.1

6 months ago

0.3.4

6 months ago

0.3.3

6 months ago

0.2.1

8 months ago

0.2.0

8 months ago

0.1.0

8 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago