Web (@agentimhq/client-web) API
Generated from the source and its doc comments (sdk/client-web/src/client.ts, sdk/client-web/src/view.ts, sdk/client-web/src/store.ts).
npm install @agentimhq/client-webClientOptions
Section titled “ClientOptions”interface ClientOptionsHow the client starts: the server address, this device’s id, the token provider, the cache and how it names itself.
ClientOptions.url
Section titled “ClientOptions.url”url: stringhttps://chat.example.com.
ClientOptions.deviceId
Section titled “ClientOptions.deviceId”deviceId: stringThis device’s id, the same every run (1..64 of A-Z a-z 0-9 _ -).
ClientOptions.deviceName
Section titled “ClientOptions.deviceName”deviceName?: stringA name for this device in the user’s device list, like “Maya’s laptop”.
ClientOptions.platform
Section titled “ClientOptions.platform”platform?: stringThe platform reported to the server (web, ios, android…), for the device list and push.
ClientOptions.tokenProvider
Section titled “ClientOptions.tokenProvider”tokenProvider: () => Promise<string>A client token from your backend; throw TokenRefused to stop.
ClientOptions.store
Section titled “ClientOptions.store”store?: Store | 'indexeddb' | 'memory'The cache: IndexedDB by default where there is one, else memory.
ClientOptions.storeName
Section titled “ClientOptions.storeName”storeName?: stringName of the IndexedDB databases (default agentim-<deviceId>).
ClientOptions.wasm
Section titled “ClientOptions.wasm”wasm?: WasmSourceThe core module (default: agentim_core.wasm next to this file).
ClientOptions.multiTab
Section titled “ClientOptions.multiTab”multiTab?: booleanShare one connection between tabs (default: where the browser can).
ClientOptions.WebSocket
Section titled “ClientOptions.WebSocket”WebSocket?: typeof WebSocketThe WebSocket class to use, for Node and tests; browsers have their own.
ClientOptions.clientName
Section titled “ClientOptions.clientName”clientName?: stringThis app’s name, sent with each connection (for diagnostics and the server’s minimum-version rules).
ClientOptions.clientVersion
Section titled “ClientOptions.clientVersion”clientVersion?: stringThis app’s version, sent with clientName.
ClientOptions.log
Section titled “ClientOptions.log”log?: (m: string) => voidReceives the client’s diagnostic lines (connections, retries, failures); none by default.
AgentIMClient
Section titled “AgentIMClient”class AgentIMClientThe AgentIM client for one account on one device: keeps the connection, syncs, sends the offline queue in order, keeps the local cache, and turns everything into observable state.
AgentIMClient.connection
Section titled “AgentIMClient.connection”readonly connection = new Observable<Connection>({ state: 'closed' })The connection state (closed, connecting, connected, disconnected, suspended, failed) and why; subscribe to follow it.
AgentIMClient.conversations
Section titled “AgentIMClient.conversations”readonly conversations = new Observable<any[]>([])The conversation list, newest first, kept up to date.
AgentIMClient.create
Section titled “AgentIMClient.create”static async create(o: ClientOptions): Promise<AgentIMClient>Starts a client with its options: the server address, this device’s id, the token provider and the cache. Nothing connects until connect. In a browser the first tab holds the connection; other tabs share it.
AgentIMClient.command
Section titled “AgentIMClient.command”async command(cmd: Record<string, unknown>, blobs?: Record<string, Blob>): Promise<any>Any core command ({cmd: …}); resolves with its result.
AgentIMClient.on
Section titled “AgentIMClient.on”on(type: string, fn: Listener): () => voidEvents: operation, side, signal, reset, storage, event, read, mute.
AgentIMClient.connect
Section titled “AgentIMClient.connect”connect()Connects and keeps connecting: reconnects with backoff when the network or the server goes, syncs what it missed, sends the offline queue.
AgentIMClient.close
Section titled “AgentIMClient.close”close()Disconnects; the queue and the cache stay. connect again picks up where it stopped.
AgentIMClient.logout
Section titled “AgentIMClient.logout”logout()Logs out here: closes and wipes the cache and the queue (GDPR).
AgentIMClient.dispose
Section titled “AgentIMClient.dispose”async dispose()Stops this tab’s part (the next tab takes over the connection).
AgentIMClient.messages
Section titled “AgentIMClient.messages”messages(convId: string): MessagesThe conversation’s messages, kept up to date while subscribed.
AgentIMClient.refreshConversations
Section titled “AgentIMClient.refreshConversations”async refreshConversations()Loads the conversation list (it updates by itself afterwards).
AgentIMClient.sendFile
Section titled “AgentIMClient.sendFile”async sendFile(convId: string, file: Blob, opts: { name?: string; caption?: string; replyTo?: string; clientMsgId?: string; keepMetadata?: boolean } = {})Sends a file or an image: shown at once as uploading (with progress), sent when it is up. An image loses its EXIF and GPS (unless keepMetadata) and gets a thumbnail of 320 pixels.
AgentIMClient.localPreview
Section titled “AgentIMClient.localPreview”localPreview(key?: string | null): string | undefinedA local address for a file this tab is sending (payload.local_thumb or local_source).
AgentIMClient.fileUrl
Section titled “AgentIMClient.fileUrl”fileUrl(convId: string, fileId: string, variant: 'original' | 'thumb' = 'original'): Promise<{ url: string; variant: string; expires_at: number }>A link to read a file (original or thumb), valid a few minutes.
AgentIMClient.send
Section titled “AgentIMClient.send”send(convId: string, text: string, opts: { kind?: string; payload?: unknown; replyTo?: string; clientMsgId?: string; threadId?: string; alsoInConv?: boolean } = {})Sends a text (or a custom kind with its payload): shown at once as sending, sent when the connection allows, in order. Returns the client_msg_id and the local key; a retry with the same client_msg_id is the same message.
AgentIMClient.edit
Section titled “AgentIMClient.edit”edit(convId: string, target: { msgId?: string; clientMsgId?: string }, text: string)Edits one of my messages (by msgId, or by clientMsgId while it is still queued); the change shows at once.
AgentIMClient.recall
Section titled “AgentIMClient.recall”recall(convId: string, target: { msgId?: string; clientMsgId?: string })Recalls one of my messages for everyone; a message still in the queue is simply not sent.
AgentIMClient.read
Section titled “AgentIMClient.read”read(convId: string, seq?: number)Marks the conversation read up to seq (default: the newest) on all my devices.
AgentIMClient.feedback
Section titled “AgentIMClient.feedback”feedback(convId: string, msgId: string, rating: 'up' | 'down', reason?: string)Rates an agent’s message up or down, with an optional reason.
AgentIMClient.react
Section titled “AgentIMClient.react”react(convId: string, msgId: string, key: string): Promise<{ changed: boolean; seq?: number }>Adds a reaction (one emoji, or one of the app’s :names:); adding it twice changes nothing.
AgentIMClient.unreact
Section titled “AgentIMClient.unreact”unreact(convId: string, msgId: string, key: string): Promise<{ changed: boolean; seq?: number }>Removes my reaction; removing one that is not there changes nothing.
AgentIMClient.listReactions
Section titled “AgentIMClient.listReactions”listReactions(convId: string, msgId: string, key: string, opts: { afterSeq?: number; limit?: number } = {})Who reacted to a message with a key, oldest first.
AgentIMClient.pin
Section titled “AgentIMClient.pin”pin(convId: string, msgId: string): Promise<{ changed: boolean; seq?: number }>Pins a message to the conversation; pinning it again changes nothing.
AgentIMClient.unpin
Section titled “AgentIMClient.unpin”unpin(convId: string, msgId: string): Promise<{ changed: boolean; seq?: number }>Unpins a message.
AgentIMClient.listPins
Section titled “AgentIMClient.listPins”listPins(convId: string, opts: { beforeSeq?: number; limit?: number } = {})The conversation’s pinned messages, latest pin first.
AgentIMClient.pullThread
Section titled “AgentIMClient.pullThread”pullThread(convId: string, threadId: string, opts: { afterSeq?: number; limit?: number } = {})A thread’s root and its replies after afterSeq.
AgentIMClient.listThreads
Section titled “AgentIMClient.listThreads”listThreads(opts: { convId?: string; before?: number; limit?: number } = {})The threads I follow (in one conversation, or all), latest reply first, with unread counts.
AgentIMClient.readThread
Section titled “AgentIMClient.readThread”readThread(convId: string, threadId: string, seq: number)Marks a thread read up to seq on all my devices.
AgentIMClient.followThread
Section titled “AgentIMClient.followThread”followThread(convId: string, threadId: string, follow = true)Follows a thread (its replies notify me) or stops following it.
AgentIMClient.msgSummary
Section titled “AgentIMClient.msgSummary”msgSummary(convId: string, msgIds: string[])The thread summary, reactions and pin of up to 100 messages (for messages loaded from history).
AgentIMClient.respond
Section titled “AgentIMClient.respond”respond(convId: string, msgId: string, blockId: string, r: { action?: string; values?: Record<string, unknown> })Answers a card: a button (action) or a filled form (values).
AgentIMClient.retry
Section titled “AgentIMClient.retry”retry(convId: string, clientMsgId: string)Sends a failed message again (the same client_msg_id, so it is never doubled).
AgentIMClient.discard
Section titled “AgentIMClient.discard”discard(convId: string, clientMsgId: string)Drops a failed or waiting message from the queue.
AgentIMClient.focus
Section titled “AgentIMClient.focus”focus(convId: string | null)The conversation on screen (or none): its agents’ replies stream in live.
AgentIMClient.pendingOps
Section titled “AgentIMClient.pendingOps”pendingOps()What waits in the offline queue.
AgentIMClient.call
Section titled “AgentIMClient.call”call(op: string, args: Record<string, unknown> = {})A request that needs the server now (approvals, decisions, queries).
AgentIMClient.debugState
Section titled “AgentIMClient.debugState”debugState()Everything that helps report a problem: the connection, positions, the queue and recent failures.
Commands
Section titled “Commands”interface CommandsWhat a view needs from its client.
Commands.command
Section titled “Commands.command”command(cmd: Record<string, unknown>): Promise<any>Runs one core command and resolves with its answer; the typed methods are built on this.
TokenRefused
Section titled “TokenRefused”class TokenRefusedThrown by a token provider when your backend refuses the user: the client stops instead of retrying.
AgentIMError
Section titled “AgentIMError”class AgentIMErrorA refused or failed command: code is the error code, retryable whether trying again may work.
AgentIMError.code
Section titled “AgentIMError.code”code: stringThe error code: the server’s (forbidden, not_found, conflict, bad_request, …) or the client’s (disconnected, timeout, closed, unauthenticated).
AgentIMError.retryable
Section titled “AgentIMError.retryable”retryable: booleanWhether the same request may work later.
AgentIMError.retryAfterMs
Section titled “AgentIMError.retryAfterMs”retryAfterMs?: numberHow long the server asked to wait before trying again.
Observable
Section titled “Observable”class ObservableA value the UI follows: get() returns the same object until it changes.
Observable.get
Section titled “Observable.get”get = (): T => this.vThe current value (the same object until it changes, for React’s useSyncExternalStore).
Observable.subscribe
Section titled “Observable.subscribe”subscribe = (fn: (v: T) => void): (() => void) =>Calls fn on every change; returns the function that stops it.
Observable.set
Section titled “Observable.set”set(v: T)Replaces the value and tells the subscribers.
Connection
Section titled “Connection”interface ConnectionThe connection state and why.
Connection.state
Section titled “Connection.state”state: 'closed' | 'connecting' | 'connected' | 'disconnected' | 'suspended' | 'failed'closed (not started or closed), connecting, connected, disconnected (waiting to reconnect), suspended (offline, or down for a long time: retries less often) or failed (stopped: the token was refused or the account deactivated).
Connection.reason
Section titled “Connection.reason”reason?: string | nullWhy it is not connected (an error code), when it is not.
Connection.retryAt
Section titled “Connection.retryAt”retryAt?: number | nullWhen the next reconnect attempt is due (Unix ms), while waiting to reconnect.
Message
Section titled “Message”interface MessageOne message as shown in a list.
Message.key
Section titled “Message.key”key: stringThe list identity: local:<client_msg_id> for a message sent from this device until the server’s copy replaces it (in one change), then its msg_id.
Message.msg_id
Section titled “Message.msg_id”msg_id: stringThe server’s id (local:<client_msg_id> until the server has it).
Message.mine
Section titled “Message.mine”mine?: booleanWhether I sent it (from any of my devices).
Message.client_msg_id
Section titled “Message.client_msg_id”client_msg_id?: stringThe id this device gave the message, for retries.
Message.conv_id
Section titled “Message.conv_id”conv_id: stringThe conversation it belongs to.
Message.content
Section titled “Message.content”content: stringThe text (for a streaming reply, what has arrived so far).
Message.status
Section titled “Message.status”status: 'sent' | 'sending' | 'uploading' | 'failed'sent, sending (queued or on its way), uploading (its file is uploading) or failed (refused; retry or discard it).
Message.order
Section titled “Message.order”order: numberThe sort position in the list.
Messages
Section titled “Messages”class MessagesOne conversation’s messages as shown: snapshot, then changes.
Messages.hasOlder
Section titled “Messages.hasOlder”hasOlder = falseWhether older messages can still be loaded.
Messages.olderEnd
Section titled “Messages.olderEnd”olderEnd?: stringWhy nothing older can be loaded: start (the beginning) or retention (deleted by the retention period).
Messages.reset
Section titled “Messages.reset”reset(list: Message[], hasOlder?: boolean, olderEnd?: string)@internal
Messages.apply
Section titled “Messages.apply”apply(changes: any[])@internal
Messages.applyEvent
Section titled “Messages.applyEvent”applyEvent(e: { reset?: boolean; changes?: any[] })@internal A messages event: with reset the view is replaced by the changes in one step, so no one sees it empty in between.
Messages.loadOlder
Section titled “Messages.loadOlder”async loadOlder(): Promise<boolean>The page before the oldest shown: the cache first, then the server.
Messages.jumpTo
Section titled “Messages.jumpTo”jumpTo(seq: number)A page around a message (it stays loaded).
interface BatchOne atomic write to the cache: everything in it is written or nothing is.
Batch.id
Section titled “Batch.id”id: numberThe batch number; the core waits for each in order.
Batch.convs
Section titled “Batch.convs”convs: Array<{ conv_id: string; [k: string]: unknown }>Conversations to write (replacing the stored ones).
Batch.put_messages
Section titled “Batch.put_messages”put_messages: Array<Record<string, any>>Messages to write.
Batch.remove_messages
Section titled “Batch.remove_messages”remove_messages: Array<[string, string]>Messages to delete, as [conv_id, key].
Batch.put_ops
Section titled “Batch.put_ops”put_ops: Array<Record<string, any>>Queued operations to write (the offline queue).
Batch.remove_ops
Section titled “Batch.remove_ops”remove_ops: string[]Queued operations that are done, by id.
Batch.meta
Section titled “Batch.meta”meta?: { account?: string | null } | nullWhich account the cache belongs to; a different account wipes it.
Snapshot
Section titled “Snapshot”interface SnapshotWhat the cache holds at start: the account, the conversations, the queue and recent messages.
Snapshot.account
Section titled “Snapshot.account”account: string | nullThe account the cache belongs to, or null when empty.
Snapshot.convs
Section titled “Snapshot.convs”convs: unknown[]The stored conversations.
Snapshot.ops
Section titled “Snapshot.ops”ops: unknown[]The stored offline queue, in order.
Snapshot.messages
Section titled “Snapshot.messages”messages: unknown[]The newest messages of each conversation, up to the page size.
Failure
Section titled “Failure”type Failure = 'full' | 'corrupt' | 'unavailable'Why a write failed, as the core names it.
StoreError
Section titled “StoreError”class StoreErrorWhy the cache could not be written: full, corrupt or unavailable.
interface StoreA cache for the client (IndexedDB in browsers, memory elsewhere); implement it to keep the cache somewhere else.
Store.kind
Section titled “Store.kind”readonly kind: 'indexeddb' | 'memory'indexeddb or memory.
Store.open
Section titled “Store.open”open(): Promise<void>Opens the cache; fails with StoreError when it cannot.
Store.snapshot
Section titled “Store.snapshot”snapshot(page: number): Promise<Snapshot>Reads what the cache holds, with up to page messages per conversation.
Store.page
Section titled “Store.page”page(convId: string, beforeSeq: number, limit: number): Promise<unknown[]>Reads up to limit older messages of a conversation before beforeSeq.
Store.write
Section titled “Store.write”write(b: Batch): Promise<void>Writes one batch atomically.
Store.wipe
Section titled “Store.wipe”wipe(): Promise<void>Deletes everything (sign-out, a different account).
Store.persisted
Section titled “Store.persisted”persisted?: booleannavigator.storage.persist() result, when asked.
MemoryStore
Section titled “MemoryStore”class MemoryStoreNothing kept after the page closes.
MemoryStore.kind
Section titled “MemoryStore.kind”readonly kind = 'memory'memory.
MemoryStore.open
Section titled “MemoryStore.open”async open() {}Nothing to open.
MemoryStore.snapshot
Section titled “MemoryStore.snapshot”async snapshot(page: number): Promise<Snapshot>What the core reads at start: the account, conversations, the queue and the newest page messages of each conversation.
MemoryStore.page
Section titled “MemoryStore.page”async page(convId: string, before: number, limit: number)Messages of a conversation before a seq, for paging back.
MemoryStore.write
Section titled “MemoryStore.write”async write(b: Batch)Writes a batch from the core atomically.
MemoryStore.wipe
Section titled “MemoryStore.wipe”async wipe()Deletes everything (logout, another account).
IndexedDBStore
Section titled “IndexedDBStore”class IndexedDBStoreIndexedDB, one per account and device (name). Asks the browser to keep it (navigator.storage.persist()) unless told not to.
IndexedDBStore.kind
Section titled “IndexedDBStore.kind”readonly kind = 'indexeddb'indexeddb.
IndexedDBStore.persisted
Section titled “IndexedDBStore.persisted”persisted?: booleanWhether the browser agreed to keep the cache (navigator.storage.persist()).
IndexedDBStore.open
Section titled “IndexedDBStore.open”async open()Opens (or creates) the cache and the queue databases and the encryption key.
IndexedDBStore.snapshot
Section titled “IndexedDBStore.snapshot”async snapshot(page: number): Promise<Snapshot>What the core reads at start: the account, conversations, the queue and the newest page messages of each conversation.
IndexedDBStore.page
Section titled “IndexedDBStore.page”async page(convId: string, before: number, limit: number)Messages of a conversation before a seq, for paging back.
IndexedDBStore.write
Section titled “IndexedDBStore.write”async write(b: Batch)Writes a batch from the core in one transaction (the queue in its own database).
IndexedDBStore.wipe
Section titled “IndexedDBStore.wipe”async wipe()Deletes both databases (logout, another account).