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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chat-leo/chatleolib

v2.2.0

Published

ChatLeo library for web integration

Readme

chatleolib

Core library of ChatLeo: login, real-time connection, state (ChatStore) and uploads. It is normally loaded by <chatleo-component>; an application shell can also drive it directly with ChatLeoLib.getInstance() and setServerUrl() (see below).

Real-time transport

The library talks to the server through a Transport (src/transport/):

| Transport | Protocol | |---|---| | WsV2Transport | ChatLeo Protocol v2 over WebSocket (chatleo-v2) — the default | | SocketIoTransport | Socket.IO (socketio-v1) — legacy compatibility mode |

Configuration

import { ChatLeoLib } from '@chat-leo/chatleolib';

ChatLeoLib.transport = 'v2'; // 'v2' (default) | 'socketio' | 'auto'

or, per component, <chatleo-component transport="socketio"> (takes precedence over the static setting).

  • v2 (default) — ChatLeo Protocol v2 only. The endpoint comes from discovery, or <serverUrl>/v2/ws if the server has no discovery document. If the WebSocket cannot be established, login still succeeds and the connection keeps retrying in the background.
  • socketio — legacy Socket.IO at serverUrl. Nothing else is requested (no discovery document); keep this for Socket.IO-only deployments during the transition.
  • auto — after login the library fetches GET /.well-known/chatleo (cached for the session). If it lists chatleo-v2, v2 is used at the advertised URL (REST calls stay on serverUrl, even if the WebSocket lives on another host). If the document is missing, invalid or does not offer v2, Socket.IO is used. If v2 cannot be established after 3 attempts the library logs a warning and falls back to Socket.IO when the document lists it.

The default changed from socketio to v2, so default logins now fetch the discovery document once; a missing or failed document keeps the client on v2 at <serverUrl>/v2/ws rather than switching to Socket.IO. Set the value before the first login. Servers must offer v2, or hosts must pin socketio/auto; rolling back to the previous behaviour is configuration-only.

Reliable sending and unread state

  • sendChatMessage() shows the message immediately with delivery: 'pending' and returns the generated client_id. On ack the message becomes sent and takes the server id/seq; broadcasts that arrive first are merged by client_id, so nothing is shown twice.
  • Failed sends stay in an Outbox and are retried automatically after reconnect with the same client_id (10 s ack timeout, exponential backoff, 5 attempts). forbidden, read_only, not_found and invalid are permanent. On failure the message has delivery: 'failed' and error; components emit messageRetry/messageDiscard, which map to chatStore.retryMessage()/discardMessage().
  • When the server sequences messages, reconnect catch-up uses one POST /api/v1/sync request with per-room cursors. Missing sequences are fetched with after_seq, scroll-back uses before_seq, and unread counts/read markers come from the server (POST /rooms/:id/read, room-read events). Opening the active room reports the read marker after a 2 s debounce while the tab is visible.
  • Servers without sequencing keep the previous behaviour (active-room re-fetch, local unread counting) while still showing pending/sent state.

What differs between protocols

Sending, receiving, room events, presence, read state and catch-up sync behave identically (the same behaviour tests run against both). Only v2 additionally provides:

  • Resume — on every reconnect the library sends its per-room cursors so the server replays missed messages; room.resync triggers a REST catch-up.
  • In-band token refresh — a refreshed access token is sent as auth.refresh on the open connection instead of reconnecting.
  • Activity and streams — ChatStore dispatches activity and streamUpdated events (typing / thinking / tool use of agents, and messages that are produced incrementally). With Socket.IO these events are never produced.
chatStore.addEventListener('activity', (e) => {
  const { roomId, senderId, kind, state, expiresAt, data } = (e as CustomEvent).detail;
});
chatStore.addEventListener('streamUpdated', (e) => {
  const { streamId, roomId, text, status } = (e as CustomEvent).detail; // status: streaming | ended | aborted
});
chatStore.getActivities(roomId); // currently active activities
chatStore.getStreams(roomId);    // streams in progress

Testing

pnpm --filter @chat-leo/chatleolib test

behaviour.socketio.test.ts and behaviour.v2.test.ts run one shared scenario suite (src/testing/behaviourSuite.ts) against a fake Socket.IO server and a fake v2 server. src/testing/FakeV2Server.ts can be reused to test other v2 clients.

Calls (protocol v2 only)

Calls require protocol v2. On Socket.IO the library reports calls as unavailable, ignores call events and refuses the call REST actions; chat keeps working. ChatStore exposes activeCall(roomId) and incomingCall, and dispatches callChanged / callRinging. ChatLeoLib.ensureCallsAvailable() checks the discovery calls feature; startCall, joinCall, declineCall, leaveCall, endCall and refreshCallToken call /api/v1/.../calls. joinCall returns {sfu_url, token, room_id, mode, ice_servers}, which maps directly to call-core's ConnectionCredentials. Call state also comes from active_call in the room list and sync response, so a client that missed live events still shows a running call.

Push notifications

ChatLeoLib.push exposes state(), enable(), disable() and syncOnLogin(); the component uses pushState, enablePush and disablePush. Push requires the server's push discovery feature and a served worker (chatleolib/dist/chatleo-push-sw.js, or import chatleolib/push-sw into an existing worker). Subscriptions always go through the ChatLeo server; the PulseLeo SDK is loaded lazily. See docs/push.md.

Application shell without an embedded component

A host that owns the session at the shell level — no chat page and no <chatleo-component> in the DOM — drives the shared instance directly, points it at the server and signs in:

import { ChatLeoLib, ChatLeoLoginError } from '@chat-leo/chatleolib';

const lib = ChatLeoLib.getInstance(); // the one instance the page (and its components) share
lib.setServerUrl('https://chat.example.com'); // origin of the ChatLeo server

try {
  const profile = await lib.signIn({ ticket, transport: 'auto' });
} catch (e) {
  if (e instanceof ChatLeoLoginError) {
    // e.reason: 'invalid_credentials' | 'invalid_ticket' | 'unreachable' | 'server_error'
  }
}
  • setServerUrl() is required before the first sign-in when no component provides server-url. It accepts an origin URL (https://chat.example.com) — REST and discovery endpoints are resolved from it — and rejects paths, queries, hashes and non-http(s) schemes.
  • A programmatically set URL wins over the DOM scan: a <chatleo-component> mounted later uses the configured server even if its server-url attribute differs (or is absent).
  • Re-setting the current URL is a no-op. Changing it while signed in throws — call logout() first, because the session's tokens, transport and rooms belong to the previous server.
  • A session returned by lib.getSession() can be persisted and restored on the next load with lib.restoreSession(session); its server URL is re-applied and stays sticky.
  • Components discovered later are bound to the same instance and join the existing session; mount <chatleo-component chatLeoLib={lib}> when a chat view is needed.