npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sarah-client

v0.3.6

Published

A Typescript library for interacting with the Sarah chat API

Downloads

24

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. 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).