sarah-client
v0.3.6
Published
A Typescript library for interacting with the Sarah chat API
Downloads
8
Readme
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. TheonMessage
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 tofalse
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 tofalse
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 theChatClient
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()
: Returnstrue
if a request is in progress.isWaiting()
: Returnstrue
if the chat is waiting for a new message. (in streaming modewaiting
may befalse
andloading
may betrue
at the same time; whenloading
isfalse
,waiting
isfalse
as well).