react-websocket 4.20.0 → 4.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ag-ui.cjs CHANGED
@@ -314,7 +314,7 @@ const ENDS = /* @__PURE__ */ new Set(["RUN_FINISHED", "RUN_ERROR"]);
314
314
  * ```
315
315
  */
316
316
  function createAgUiAgent(client, options = {}) {
317
- const threadId = options.threadId ?? randomId();
317
+ let threadId = options.threadId ?? randomId();
318
318
  const empty = {
319
319
  messages: [],
320
320
  state: options.state,
@@ -400,6 +400,13 @@ function createAgUiAgent(client, options = {}) {
400
400
  return runId;
401
401
  },
402
402
  abort,
403
+ reset() {
404
+ base = empty;
405
+ resynced.clear();
406
+ if (options.threadId === void 0) threadId = randomId();
407
+ runs.clear();
408
+ update();
409
+ },
403
410
  getSnapshot: () => snapshot,
404
411
  subscribe(listener) {
405
412
  listeners.add(listener);
@@ -1 +1 @@
1
- {"version":3,"file":"ag-ui.cjs","names":["createWebSocketRuns"],"sources":["../src/ag-ui.ts"],"sourcesContent":["// The build keeps `./core` external, so this entry shares the client with the others.\nimport {\n createWebSocketRuns,\n type RunPolicy,\n type RunStorage,\n type WebSocketClient,\n type WebSocketRuns\n} from './core';\n\n/** A JSON Patch (RFC 6902) operation, as in AG-UI's `STATE_DELTA` and `ACTIVITY_DELTA`. */\nexport interface JsonPatchOperation {\n readonly op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n /** A JSON Pointer (RFC 6901), such as `/items/0/name`. */\n readonly path: string;\n /** The source of `move` and `copy`. */\n readonly from?: string;\n /** The value to `add`, `replace` with or `test` for. */\n readonly value?: unknown;\n}\n\n/** An AG-UI event: `type` is `'RUN_STARTED'`, `'TEXT_MESSAGE_CONTENT'`, `'STATE_DELTA'`, … */\nexport interface AgUiEvent {\n readonly type: string;\n readonly [field: string]: any;\n}\n\n/** A tool call on an assistant message, as in AG-UI. */\nexport interface AgUiToolCall {\n readonly id: string;\n readonly type: 'function';\n readonly function: { readonly name: string; readonly arguments: string };\n}\n\n/** A message in an AG-UI thread. */\nexport interface AgUiMessage {\n readonly id: string;\n readonly role: 'developer' | 'system' | 'assistant' | 'user' | 'tool' | 'reasoning' | 'activity';\n readonly content?: unknown;\n readonly toolCalls?: readonly AgUiToolCall[];\n /** For a `tool` message, the call it answers. */\n readonly toolCallId?: string;\n /** For an `activity` message. */\n readonly activityType?: string;\n readonly [field: string]: unknown;\n}\n\n/** AG-UI's `RunAgentInput`: what a run starts from. */\nexport interface RunAgentInput {\n readonly threadId: string;\n readonly runId: string;\n readonly parentRunId?: string;\n readonly state?: unknown;\n readonly messages: readonly AgUiMessage[];\n readonly tools: readonly unknown[];\n readonly context: readonly unknown[];\n readonly forwardedProps: unknown;\n}\n\n/** A thread's messages and shared state, reduced from AG-UI events. */\nexport interface AgUiThread<S = unknown> {\n readonly messages: readonly AgUiMessage[];\n /** The shared state, from `STATE_SNAPSHOT` and `STATE_DELTA`. */\n readonly state: S;\n /**\n * `'running'` from `RUN_STARTED`, `'finished'` after `RUN_FINISHED`, `'error'` after\n * `RUN_ERROR`, `'aborted'` when the run was stopped from this side.\n */\n readonly status: 'idle' | 'running' | 'finished' | 'error' | 'aborted';\n /** The step in progress, between `STEP_STARTED` and `STEP_FINISHED`. */\n readonly step?: string;\n /** From `RUN_ERROR`. */\n readonly error?: { readonly message: string; readonly code?: string };\n /** From `RUN_FINISHED`. */\n readonly result?: unknown;\n /**\n * A `STATE_DELTA` couldn't be applied. `state` is the last good one, and later deltas are\n * skipped until a `STATE_SNAPSHOT` arrives.\n */\n readonly stale: boolean;\n}\n\ntype Json = any;\n\nfunction unescape(token: string) {\n return token.replace(/~1/g, '/').replace(/~0/g, '~');\n}\n\nfunction equal(a: Json, b: Json): boolean {\n if (a === b) return true;\n if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n const keys = Object.keys(a);\n return keys.length === Object.keys(b).length && keys.every(key => equal(a[key], b[key]));\n}\n\nfunction fail(operation: JsonPatchOperation): never {\n throw new Error(`JSON Patch failed: ${operation.op} ${operation.path}`);\n}\n\nconst INDEX = /^(0|[1-9]\\d*)$/;\n\n/** A JSON Pointer's tokens. It's empty, for the whole document, or starts with `/`. */\nfunction tokens(pointer: string, operation: JsonPatchOperation) {\n if (pointer !== '' && pointer[0] !== '/') fail(operation);\n return pointer.split('/').slice(1).map(unescape);\n}\n\n/** Whether `node` has `token`: an index in range for an array, not `length`; an own key otherwise. */\nfunction has(node: Json, token: string) {\n return Array.isArray(node)\n ? INDEX.test(token) && +token < node.length\n : Object.hasOwn(node, token);\n}\n\n/** Set an own property, even `__proto__`, as JSON.parse would. */\nfunction put(object: Json, key: string, value: Json) {\n Object.defineProperty(object, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true\n });\n}\n\n/**\n * Apply a JSON Patch (RFC 6902) without changing `document`: what changes is copied, the rest is\n * shared. Throws, leaving `document` as it was, if an operation can't be applied: a missing path,\n * an index out of range, a pointer that doesn't start with `/`, or a failed `test`.\n */\nexport function applyPatch<T>(document: T, operations: readonly JsonPatchOperation[]): T {\n let root: Json = document;\n\n function get(path: string, operation: JsonPatchOperation): Json {\n let node = root;\n for (const token of tokens(path, operation)) {\n if (typeof node !== 'object' || node === null || !has(node, token)) fail(operation);\n node = node[token];\n }\n return node;\n }\n\n // Replace the parent of `path` with a copy, change it with `change`, and copy its ancestors.\n function edit(\n path: string,\n operation: JsonPatchOperation,\n change: (parent: Json, key: string) => void\n ) {\n const keys = tokens(path, operation);\n if (!keys.length) fail(operation);\n const walk = (node: Json, depth: number): Json => {\n if (typeof node !== 'object' || node === null) fail(operation);\n const copy = Array.isArray(node) ? [...node] : { ...node };\n const key = keys[depth]!;\n if (depth === keys.length - 1) change(copy, key);\n else if (!has(node, key)) fail(operation);\n else copy[key] = walk(node[key], depth + 1);\n return copy;\n };\n root = walk(root, 0);\n }\n\n function index(parent: Json[], key: string, operation: JsonPatchOperation, adding: boolean) {\n // `-` is the end of the array, which only `add` can use.\n const i = key === '-' ? (adding ? parent.length : -1) : INDEX.test(key) ? +key : -1;\n if (i < 0 || i > parent.length - (adding ? 0 : 1)) fail(operation);\n return i;\n }\n\n function add(path: string, value: Json, operation: JsonPatchOperation) {\n if (path === '') return void (root = value);\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, true), 0, value);\n else put(parent, key, value);\n });\n }\n\n function remove(path: string, operation: JsonPatchOperation) {\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, false), 1);\n else if (Object.hasOwn(parent, key)) delete parent[key];\n else fail(operation);\n });\n }\n\n for (const operation of operations) {\n const { op, path, from, value } = operation;\n if (op === 'add') add(path, value, operation);\n else if (op === 'remove') remove(path, operation);\n else if (op === 'replace') {\n get(path, operation);\n if (path === '') root = value;\n else\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent[key as unknown as number] = value;\n else put(parent, key, value);\n });\n } else if (op === 'move' || op === 'copy') {\n const moved = get(from ?? fail(operation), operation);\n if (op === 'move') remove(from!, operation);\n add(path, op === 'copy' ? structuredClone(moved) : moved, operation);\n } else if (op === 'test') {\n if (!equal(get(path, operation), value)) fail(operation);\n } else fail(operation);\n }\n return root;\n}\n\nfunction updateMessage(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (message: AgUiMessage) => AgUiMessage\n) {\n return thread.messages.map(message => (message.id === id ? change(message) : message));\n}\n\nfunction appendText(message: AgUiMessage, delta: string | undefined) {\n return { ...message, content: `${(message.content as string | undefined) ?? ''}${delta ?? ''}` };\n}\n\nfunction updateToolCall(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (call: AgUiToolCall) => AgUiToolCall\n) {\n return thread.messages.map(message =>\n message.toolCalls?.some(call => call.id === id)\n ? {\n ...message,\n toolCalls: message.toolCalls.map(call => (call.id === id ? change(call) : call))\n }\n : message\n );\n}\n\n/** The last message with a tool call, for chunks that leave out `toolCallId`. */\nfunction lastToolCallId(thread: AgUiThread<any>) {\n for (let i = thread.messages.length - 1; i >= 0; i--) {\n const calls = thread.messages[i]!.toolCalls;\n if (calls?.length) return calls.at(-1)!.id;\n }\n}\n\n/**\n * Fold one AG-UI event into a thread, without changing it. Handles the run lifecycle, steps,\n * text, tool calls and their results, reasoning, activity, and message and state snapshots and\n * deltas. A `STATE_DELTA` that can't be applied marks the thread `stale` instead of throwing.\n * Other events, such as `CUSTOM` and `RAW`, return the thread unchanged.\n *\n * Use it as a `reduce` function, or wrap it to handle events of your own.\n */\nexport function applyAgUiEvent<S>(thread: AgUiThread<S>, event: AgUiEvent): AgUiThread<S> {\n const e = event;\n const message = (role: AgUiMessage['role'], extra?: object): AgUiThread<S> => ({\n ...thread,\n messages: [...thread.messages, { id: e.messageId, role, content: '', ...extra }]\n });\n const known = (id: string | undefined) => thread.messages.some(m => m.id === id);\n switch (e.type) {\n case 'RUN_STARTED':\n return { ...thread, status: 'running', error: undefined, result: undefined };\n case 'RUN_FINISHED':\n return { ...thread, status: 'finished', step: undefined, result: e.result };\n case 'RUN_ERROR':\n return {\n ...thread,\n status: 'error',\n step: undefined,\n error: { message: e.message, code: e.code }\n };\n case 'STEP_STARTED':\n return { ...thread, step: e.stepName };\n case 'STEP_FINISHED':\n return { ...thread, step: undefined };\n case 'TEXT_MESSAGE_START':\n return message(e.role ?? 'assistant');\n case 'REASONING_MESSAGE_START':\n return message('reasoning');\n case 'TEXT_MESSAGE_CONTENT':\n case 'REASONING_MESSAGE_CONTENT':\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => appendText(m, e.delta))\n };\n case 'TEXT_MESSAGE_CHUNK':\n case 'REASONING_MESSAGE_CHUNK': {\n // Only the first chunk of a message must carry its id.\n const id = e.messageId ?? thread.messages.at(-1)?.id;\n const role = e.type === 'TEXT_MESSAGE_CHUNK' ? (e.role ?? 'assistant') : 'reasoning';\n const next = known(id) ? thread : message(role);\n return { ...next, messages: updateMessage(next, id, m => appendText(m, e.delta)) };\n }\n case 'TOOL_CALL_START':\n case 'TOOL_CALL_CHUNK': {\n const id = e.toolCallId ?? lastToolCallId(thread);\n let next = thread;\n if (\n e.type === 'TOOL_CALL_START' ||\n !thread.messages.some(m => m.toolCalls?.some(c => c.id === id))\n ) {\n const call: AgUiToolCall = {\n id,\n type: 'function',\n function: { name: e.toolCallName, arguments: '' }\n };\n const parent = e.parentMessageId ?? id;\n next = known(parent)\n ? thread\n : { ...thread, messages: [...thread.messages, { id: parent, role: 'assistant' }] };\n next = {\n ...next,\n messages: updateMessage(next, parent, m => ({\n ...m,\n toolCalls: [...(m.toolCalls ?? []), call]\n }))\n };\n }\n if (e.type === 'TOOL_CALL_START') return next;\n return toolArgs(next, id, e.delta);\n }\n case 'TOOL_CALL_ARGS':\n return toolArgs(thread, e.toolCallId, e.delta);\n case 'TOOL_CALL_RESULT':\n return message(e.role ?? 'tool', { toolCallId: e.toolCallId, content: e.content });\n case 'STATE_SNAPSHOT':\n return { ...thread, state: e.snapshot, stale: false };\n case 'STATE_DELTA':\n if (thread.stale) return thread;\n try {\n return { ...thread, state: applyPatch(thread.state, e.delta) };\n } catch {\n return { ...thread, stale: true };\n }\n case 'MESSAGES_SNAPSHOT':\n return { ...thread, messages: e.messages };\n case 'ACTIVITY_SNAPSHOT': {\n if (known(e.messageId) && e.replace === false) return thread;\n const next = known(e.messageId) ? thread : message('activity');\n return {\n ...next,\n messages: updateMessage(next, e.messageId, m => ({\n ...m,\n activityType: e.activityType,\n content: e.content\n }))\n };\n }\n case 'ACTIVITY_DELTA':\n try {\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => ({\n ...m,\n content: applyPatch(m.content, e.patch)\n }))\n };\n } catch {\n return thread;\n }\n default:\n return thread;\n }\n}\n\nfunction toolArgs<S>(thread: AgUiThread<S>, id: string | undefined, delta: string | undefined) {\n return {\n ...thread,\n messages: updateToolCall(thread, id, call => ({\n ...call,\n function: { ...call.function, arguments: call.function.arguments + (delta ?? '') }\n }))\n };\n}\n\n/** What the server sends for each event of a run: its run, its offset (from 1), and the event. */\nexport interface AgUiEnvelope {\n readonly runId: string;\n readonly offset: number;\n readonly event: AgUiEvent;\n}\n\n/** Options for {@link createAgUiAgent}. The message formats have defaults; see the guide. */\nexport interface AgUiAgentOptions<S, In, Out> {\n /** The thread's id, sent in every `RunAgentInput`. A new random one by default. */\n threadId?: string;\n /** The shared state before the first `STATE_SNAPSHOT`, sent with the first run. */\n state?: S;\n /** Names where streaming runs are remembered across reloads. @default 'ag-ui' */\n name?: string;\n /** @default `{ type: 'run', runId, input }` */\n run?: (input: RunAgentInput, runId: string) => Out;\n /** @default `{ type: 'resume', runId, after }` */\n resume?: (runId: string, after: number | undefined) => Out;\n /** @default `{ type: 'cancel', runId }` */\n cancel?: (runId: string) => Out;\n /**\n * The message that asks the server for a `STATE_SNAPSHOT`, sent when a `STATE_DELTA` can't\n * be applied. Without it, the state stays `stale` until the server sends one by itself.\n */\n resync?: (runId: string) => Out;\n /** Recognize a run's event. @default messages shaped like {@link AgUiEnvelope} */\n event?: (message: In) => AgUiEnvelope | null | undefined;\n /** What happens to a run when its connection drops. @default 'keep' */\n policy?: RunPolicy;\n /** Where streaming runs are remembered. @default sessionStorage */\n storage?: RunStorage | null;\n}\n\n/** An AG-UI agent over a WebSocket. Create with {@link createAgUiAgent}. */\nexport interface AgUiAgent<S> {\n /** The client the runs stream on. */\n readonly client: WebSocketClient<any, any>;\n /** The runs underneath, from `createWebSocketRuns`. */\n readonly runs: WebSocketRuns<RunAgentInput, AgUiThread<S>>;\n /**\n * Start a run with the thread so far plus `input.messages`, which show up in the thread right\n * away. A run that's still streaming is aborted first. Returns the run's id.\n */\n run(\n input?: {\n messages?: readonly AgUiMessage[];\n tools?: readonly unknown[];\n context?: readonly unknown[];\n forwardedProps?: unknown;\n },\n options?: { policy?: RunPolicy }\n ): string;\n /** Stop the run that's streaming, if any. */\n abort(): void;\n /** The thread as of the latest run. The same object until it changes. */\n getSnapshot(): AgUiThread<S>;\n /** Get notified when the thread changes. Compatible with `useSyncExternalStore`. */\n subscribe(listener: () => void): () => void;\n /** Stop following runs, and release the connection. */\n dispose(): void;\n}\n\nfunction randomId() {\n return (\n globalThis.crypto?.randomUUID?.() ??\n `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`\n );\n}\n\nconst isEnvelope = (message: any): AgUiEnvelope | undefined =>\n typeof message?.runId === 'string' &&\n typeof message.offset === 'number' &&\n typeof message.event?.type === 'string'\n ? message\n : undefined;\n\nconst ENDS = new Set(['RUN_FINISHED', 'RUN_ERROR']);\n\n/**\n * Run an [AG-UI](https://docs.ag-ui.com) agent over a WebSocket. Each run streams AG-UI events,\n * which are folded into the thread's messages and shared state with {@link applyAgUiEvent}.\n * Runs come from `createWebSocketRuns`, so they resume after a reconnect or a reload, and a\n * state delta that can't be applied asks for a snapshot (`resync`).\n *\n * AG-UI doesn't define messages for WebSockets, so the defaults are small: the client sends\n * `{ type: 'run', runId, input }`, `{ type: 'resume', runId, after }` and\n * `{ type: 'cancel', runId }`, and the server sends `{ runId, offset, event }`, with `offset`\n * counting each run's events from 1. Each option replaces one of them.\n *\n * @example\n * ```ts\n * import { createAgUiAgent } from 'react-websocket/ag-ui';\n *\n * export const agent = createAgUiAgent(createWebSocket(url, { json: true }));\n *\n * agent.run({ messages: [{ id: crypto.randomUUID(), role: 'user', content: 'Plan my week' }] });\n * ```\n */\nexport function createAgUiAgent<S = unknown, In = any, Out = any>(\n client: WebSocketClient<In, Out>,\n options: AgUiAgentOptions<S, NoInfer<In>, NoInfer<Out>> = {}\n): AgUiAgent<S> {\n const threadId = options.threadId ?? randomId();\n const empty: AgUiThread<S> = {\n messages: [],\n state: options.state as S,\n status: 'idle',\n stale: false\n };\n let base = empty;\n let snapshot = empty;\n const listeners = new Set<() => void>();\n // Runs that asked for a snapshot after a failed delta, until one arrives.\n const resynced = new Set<string>();\n const event = options.event ?? isEnvelope;\n // Now or never: not queued, and not `volatile`, which can be a datagram that never arrives.\n const send = (message: unknown) => client.socket?.readyState === 1 && client.send(message as Out);\n\n const runs = createWebSocketRuns<RunAgentInput, AgUiThread<S>, In, Out>(client, {\n name: options.name ?? 'ag-ui',\n start: options.run ?? ((input, runId) => ({ type: 'run', runId, input }) as Out),\n resume: options.resume ?? ((runId, after) => ({ type: 'resume', runId, after }) as Out),\n cancel: options.cancel ?? (runId => ({ type: 'cancel', runId }) as Out),\n event: message => event(message),\n done: message => ENDS.has(event(message)!.event.type),\n initial: empty,\n reduce: (thread, message) => applyAgUiEvent(thread, event(message)!.event),\n policy: options.policy,\n storage: options.storage\n });\n\n function update() {\n const latest = runs.list().at(-1);\n let next = latest ? latest.value : base;\n if (latest?.status === 'aborted' && next.status === 'running')\n next = { ...next, status: 'aborted' };\n // Once each time the state goes stale, or on a later event if the socket wasn't open.\n if (latest && !latest.value.stale) resynced.delete(latest.runId);\n else if (latest && options.resync && !resynced.has(latest.runId)) {\n if (send(options.resync(latest.runId))) resynced.add(latest.runId);\n }\n if (next === snapshot) return;\n snapshot = next;\n for (const listener of [...listeners]) listener();\n }\n const off = runs.subscribe(update);\n update();\n\n function abort() {\n for (const run of runs.list()) runs.abort(run.runId);\n }\n\n return {\n client,\n runs,\n run(input = {}, runOptions) {\n abort();\n const runId = randomId();\n // The run starts from the state sent with it, the last good one: its deltas apply to it.\n base = {\n ...snapshot,\n stale: false,\n messages: [...snapshot.messages, ...(input.messages ?? [])]\n };\n runs.start(\n {\n threadId,\n runId,\n state: base.state,\n messages: base.messages,\n tools: input.tools ?? [],\n context: input.context ?? [],\n forwardedProps: input.forwardedProps ?? {}\n },\n { runId, initial: { ...base, status: 'running' }, policy: runOptions?.policy }\n );\n return runId;\n },\n abort,\n getSnapshot: () => snapshot,\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n dispose() {\n off();\n runs.dispose();\n }\n };\n}\n"],"mappings":";;;AAmFA,SAAS,SAAS,OAAe;CAC/B,OAA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I,OAAO,KAAK,GACzD;OAAA,KAAK,QAAQ,OAAO,OAAO,KAAK,CAAC,GAAG,SAAS,IAAI,OAAO,KAAK;EAAA;EAEnE,IAAI,SAAS,UAAU;EACvB,WAAW;EACX,KAAK,MAAM,YAAY,CAAC,GAAG,SAAS,GAAG,SAAS;CAClD;CACA,MAAM,MAAM,KAAK,UAAU,MAAM;CACjC,OAAO;CAEP,SAAS,QAAQ;EACf,KAAK,MAAM,OAAO,KAAK,KAAK,GAAG,KAAK,MAAM,IAAI,KAAK;CACrD;CAEA,OAAO;EACL;EACA;EACA,IAAI,QAAQ,CAAC,GAAG,YAAY;GAC1B,MAAM;GACN,MAAM,QAAQ,SAAS;GAEvB,OAAO;IACL,GAAG;IACH,OAAO;IACP,UAAU,CAAC,GAAG,SAAS,UAAU,GAAI,MAAM,YAAY,CAAC,CAAE;GAC5D;GACA,KAAK,MACH;IACE;IACA;IACA,OAAO,KAAK;IACZ,UAAU,KAAK;IACf,OAAO,MAAM,SAAS,CAAC;IACvB,SAAS,MAAM,WAAW,CAAC;IAC3B,gBAAgB,MAAM,kBAAkB,CAAC;GAC3C,GACA;IAAE;IAAO,SAAS;KAAE,GAAG;KAAM,QAAQ;IAAU;IAAG,QAAQ,YAAY;GAAO,CAC/E;GACA,OAAO;EACT;EACA;EACA,mBAAmB;EACnB,UAAU,UAAU;GAClB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;EACA,UAAU;GACR,IAAI;GACJ,KAAK,QAAQ;EACf;CACF;AACF"}
1
+ {"version":3,"file":"ag-ui.cjs","names":["createWebSocketRuns"],"sources":["../src/ag-ui.ts"],"sourcesContent":["// The build keeps `./core` external, so this entry shares the client with the others.\nimport {\n createWebSocketRuns,\n type RunPolicy,\n type RunStorage,\n type WebSocketClient,\n type WebSocketRuns\n} from './core';\n\n/** A JSON Patch (RFC 6902) operation, as in AG-UI's `STATE_DELTA` and `ACTIVITY_DELTA`. */\nexport interface JsonPatchOperation {\n readonly op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n /** A JSON Pointer (RFC 6901), such as `/items/0/name`. */\n readonly path: string;\n /** The source of `move` and `copy`. */\n readonly from?: string;\n /** The value to `add`, `replace` with or `test` for. */\n readonly value?: unknown;\n}\n\n/** An AG-UI event: `type` is `'RUN_STARTED'`, `'TEXT_MESSAGE_CONTENT'`, `'STATE_DELTA'`, … */\nexport interface AgUiEvent {\n readonly type: string;\n readonly [field: string]: any;\n}\n\n/** A tool call on an assistant message, as in AG-UI. */\nexport interface AgUiToolCall {\n readonly id: string;\n readonly type: 'function';\n readonly function: { readonly name: string; readonly arguments: string };\n}\n\n/** A message in an AG-UI thread. */\nexport interface AgUiMessage {\n readonly id: string;\n readonly role: 'developer' | 'system' | 'assistant' | 'user' | 'tool' | 'reasoning' | 'activity';\n readonly content?: unknown;\n readonly toolCalls?: readonly AgUiToolCall[];\n /** For a `tool` message, the call it answers. */\n readonly toolCallId?: string;\n /** For an `activity` message. */\n readonly activityType?: string;\n readonly [field: string]: unknown;\n}\n\n/** AG-UI's `RunAgentInput`: what a run starts from. */\nexport interface RunAgentInput {\n readonly threadId: string;\n readonly runId: string;\n readonly parentRunId?: string;\n readonly state?: unknown;\n readonly messages: readonly AgUiMessage[];\n readonly tools: readonly unknown[];\n readonly context: readonly unknown[];\n readonly forwardedProps: unknown;\n}\n\n/** A thread's messages and shared state, reduced from AG-UI events. */\nexport interface AgUiThread<S = unknown> {\n readonly messages: readonly AgUiMessage[];\n /** The shared state, from `STATE_SNAPSHOT` and `STATE_DELTA`. */\n readonly state: S;\n /**\n * `'running'` from `RUN_STARTED`, `'finished'` after `RUN_FINISHED`, `'error'` after\n * `RUN_ERROR`, `'aborted'` when the run was stopped from this side.\n */\n readonly status: 'idle' | 'running' | 'finished' | 'error' | 'aborted';\n /** The step in progress, between `STEP_STARTED` and `STEP_FINISHED`. */\n readonly step?: string;\n /** From `RUN_ERROR`. */\n readonly error?: { readonly message: string; readonly code?: string };\n /** From `RUN_FINISHED`. */\n readonly result?: unknown;\n /**\n * A `STATE_DELTA` couldn't be applied. `state` is the last good one, and later deltas are\n * skipped until a `STATE_SNAPSHOT` arrives.\n */\n readonly stale: boolean;\n}\n\ntype Json = any;\n\nfunction unescape(token: string) {\n return token.replace(/~1/g, '/').replace(/~0/g, '~');\n}\n\nfunction equal(a: Json, b: Json): boolean {\n if (a === b) return true;\n if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n const keys = Object.keys(a);\n return keys.length === Object.keys(b).length && keys.every(key => equal(a[key], b[key]));\n}\n\nfunction fail(operation: JsonPatchOperation): never {\n throw new Error(`JSON Patch failed: ${operation.op} ${operation.path}`);\n}\n\nconst INDEX = /^(0|[1-9]\\d*)$/;\n\n/** A JSON Pointer's tokens. It's empty, for the whole document, or starts with `/`. */\nfunction tokens(pointer: string, operation: JsonPatchOperation) {\n if (pointer !== '' && pointer[0] !== '/') fail(operation);\n return pointer.split('/').slice(1).map(unescape);\n}\n\n/** Whether `node` has `token`: an index in range for an array, not `length`; an own key otherwise. */\nfunction has(node: Json, token: string) {\n return Array.isArray(node)\n ? INDEX.test(token) && +token < node.length\n : Object.hasOwn(node, token);\n}\n\n/** Set an own property, even `__proto__`, as JSON.parse would. */\nfunction put(object: Json, key: string, value: Json) {\n Object.defineProperty(object, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true\n });\n}\n\n/**\n * Apply a JSON Patch (RFC 6902) without changing `document`: what changes is copied, the rest is\n * shared. Throws, leaving `document` as it was, if an operation can't be applied: a missing path,\n * an index out of range, a pointer that doesn't start with `/`, or a failed `test`.\n */\nexport function applyPatch<T>(document: T, operations: readonly JsonPatchOperation[]): T {\n let root: Json = document;\n\n function get(path: string, operation: JsonPatchOperation): Json {\n let node = root;\n for (const token of tokens(path, operation)) {\n if (typeof node !== 'object' || node === null || !has(node, token)) fail(operation);\n node = node[token];\n }\n return node;\n }\n\n // Replace the parent of `path` with a copy, change it with `change`, and copy its ancestors.\n function edit(\n path: string,\n operation: JsonPatchOperation,\n change: (parent: Json, key: string) => void\n ) {\n const keys = tokens(path, operation);\n if (!keys.length) fail(operation);\n const walk = (node: Json, depth: number): Json => {\n if (typeof node !== 'object' || node === null) fail(operation);\n const copy = Array.isArray(node) ? [...node] : { ...node };\n const key = keys[depth]!;\n if (depth === keys.length - 1) change(copy, key);\n else if (!has(node, key)) fail(operation);\n else copy[key] = walk(node[key], depth + 1);\n return copy;\n };\n root = walk(root, 0);\n }\n\n function index(parent: Json[], key: string, operation: JsonPatchOperation, adding: boolean) {\n // `-` is the end of the array, which only `add` can use.\n const i = key === '-' ? (adding ? parent.length : -1) : INDEX.test(key) ? +key : -1;\n if (i < 0 || i > parent.length - (adding ? 0 : 1)) fail(operation);\n return i;\n }\n\n function add(path: string, value: Json, operation: JsonPatchOperation) {\n if (path === '') return void (root = value);\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, true), 0, value);\n else put(parent, key, value);\n });\n }\n\n function remove(path: string, operation: JsonPatchOperation) {\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, false), 1);\n else if (Object.hasOwn(parent, key)) delete parent[key];\n else fail(operation);\n });\n }\n\n for (const operation of operations) {\n const { op, path, from, value } = operation;\n if (op === 'add') add(path, value, operation);\n else if (op === 'remove') remove(path, operation);\n else if (op === 'replace') {\n get(path, operation);\n if (path === '') root = value;\n else\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent[key as unknown as number] = value;\n else put(parent, key, value);\n });\n } else if (op === 'move' || op === 'copy') {\n const moved = get(from ?? fail(operation), operation);\n if (op === 'move') remove(from!, operation);\n add(path, op === 'copy' ? structuredClone(moved) : moved, operation);\n } else if (op === 'test') {\n if (!equal(get(path, operation), value)) fail(operation);\n } else fail(operation);\n }\n return root;\n}\n\nfunction updateMessage(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (message: AgUiMessage) => AgUiMessage\n) {\n return thread.messages.map(message => (message.id === id ? change(message) : message));\n}\n\nfunction appendText(message: AgUiMessage, delta: string | undefined) {\n return { ...message, content: `${(message.content as string | undefined) ?? ''}${delta ?? ''}` };\n}\n\nfunction updateToolCall(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (call: AgUiToolCall) => AgUiToolCall\n) {\n return thread.messages.map(message =>\n message.toolCalls?.some(call => call.id === id)\n ? {\n ...message,\n toolCalls: message.toolCalls.map(call => (call.id === id ? change(call) : call))\n }\n : message\n );\n}\n\n/** The last message with a tool call, for chunks that leave out `toolCallId`. */\nfunction lastToolCallId(thread: AgUiThread<any>) {\n for (let i = thread.messages.length - 1; i >= 0; i--) {\n const calls = thread.messages[i]!.toolCalls;\n if (calls?.length) return calls.at(-1)!.id;\n }\n}\n\n/**\n * Fold one AG-UI event into a thread, without changing it. Handles the run lifecycle, steps,\n * text, tool calls and their results, reasoning, activity, and message and state snapshots and\n * deltas. A `STATE_DELTA` that can't be applied marks the thread `stale` instead of throwing.\n * Other events, such as `CUSTOM` and `RAW`, return the thread unchanged.\n *\n * Use it as a `reduce` function, or wrap it to handle events of your own.\n */\nexport function applyAgUiEvent<S>(thread: AgUiThread<S>, event: AgUiEvent): AgUiThread<S> {\n const e = event;\n const message = (role: AgUiMessage['role'], extra?: object): AgUiThread<S> => ({\n ...thread,\n messages: [...thread.messages, { id: e.messageId, role, content: '', ...extra }]\n });\n const known = (id: string | undefined) => thread.messages.some(m => m.id === id);\n switch (e.type) {\n case 'RUN_STARTED':\n return { ...thread, status: 'running', error: undefined, result: undefined };\n case 'RUN_FINISHED':\n return { ...thread, status: 'finished', step: undefined, result: e.result };\n case 'RUN_ERROR':\n return {\n ...thread,\n status: 'error',\n step: undefined,\n error: { message: e.message, code: e.code }\n };\n case 'STEP_STARTED':\n return { ...thread, step: e.stepName };\n case 'STEP_FINISHED':\n return { ...thread, step: undefined };\n case 'TEXT_MESSAGE_START':\n return message(e.role ?? 'assistant');\n case 'REASONING_MESSAGE_START':\n return message('reasoning');\n case 'TEXT_MESSAGE_CONTENT':\n case 'REASONING_MESSAGE_CONTENT':\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => appendText(m, e.delta))\n };\n case 'TEXT_MESSAGE_CHUNK':\n case 'REASONING_MESSAGE_CHUNK': {\n // Only the first chunk of a message must carry its id.\n const id = e.messageId ?? thread.messages.at(-1)?.id;\n const role = e.type === 'TEXT_MESSAGE_CHUNK' ? (e.role ?? 'assistant') : 'reasoning';\n const next = known(id) ? thread : message(role);\n return { ...next, messages: updateMessage(next, id, m => appendText(m, e.delta)) };\n }\n case 'TOOL_CALL_START':\n case 'TOOL_CALL_CHUNK': {\n const id = e.toolCallId ?? lastToolCallId(thread);\n let next = thread;\n if (\n e.type === 'TOOL_CALL_START' ||\n !thread.messages.some(m => m.toolCalls?.some(c => c.id === id))\n ) {\n const call: AgUiToolCall = {\n id,\n type: 'function',\n function: { name: e.toolCallName, arguments: '' }\n };\n const parent = e.parentMessageId ?? id;\n next = known(parent)\n ? thread\n : { ...thread, messages: [...thread.messages, { id: parent, role: 'assistant' }] };\n next = {\n ...next,\n messages: updateMessage(next, parent, m => ({\n ...m,\n toolCalls: [...(m.toolCalls ?? []), call]\n }))\n };\n }\n if (e.type === 'TOOL_CALL_START') return next;\n return toolArgs(next, id, e.delta);\n }\n case 'TOOL_CALL_ARGS':\n return toolArgs(thread, e.toolCallId, e.delta);\n case 'TOOL_CALL_RESULT':\n return message(e.role ?? 'tool', { toolCallId: e.toolCallId, content: e.content });\n case 'STATE_SNAPSHOT':\n return { ...thread, state: e.snapshot, stale: false };\n case 'STATE_DELTA':\n if (thread.stale) return thread;\n try {\n return { ...thread, state: applyPatch(thread.state, e.delta) };\n } catch {\n return { ...thread, stale: true };\n }\n case 'MESSAGES_SNAPSHOT':\n return { ...thread, messages: e.messages };\n case 'ACTIVITY_SNAPSHOT': {\n if (known(e.messageId) && e.replace === false) return thread;\n const next = known(e.messageId) ? thread : message('activity');\n return {\n ...next,\n messages: updateMessage(next, e.messageId, m => ({\n ...m,\n activityType: e.activityType,\n content: e.content\n }))\n };\n }\n case 'ACTIVITY_DELTA':\n try {\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => ({\n ...m,\n content: applyPatch(m.content, e.patch)\n }))\n };\n } catch {\n return thread;\n }\n default:\n return thread;\n }\n}\n\nfunction toolArgs<S>(thread: AgUiThread<S>, id: string | undefined, delta: string | undefined) {\n return {\n ...thread,\n messages: updateToolCall(thread, id, call => ({\n ...call,\n function: { ...call.function, arguments: call.function.arguments + (delta ?? '') }\n }))\n };\n}\n\n/** What the server sends for each event of a run: its run, its offset (from 1), and the event. */\nexport interface AgUiEnvelope {\n readonly runId: string;\n readonly offset: number;\n readonly event: AgUiEvent;\n}\n\n/** Options for {@link createAgUiAgent}. The message formats have defaults; see the guide. */\nexport interface AgUiAgentOptions<S, In, Out> {\n /** The thread's id, sent in every `RunAgentInput`. A new random one by default. */\n threadId?: string;\n /** The shared state before the first `STATE_SNAPSHOT`, sent with the first run. */\n state?: S;\n /** Names where streaming runs are remembered across reloads. @default 'ag-ui' */\n name?: string;\n /** @default `{ type: 'run', runId, input }` */\n run?: (input: RunAgentInput, runId: string) => Out;\n /** @default `{ type: 'resume', runId, after }` */\n resume?: (runId: string, after: number | undefined) => Out;\n /** @default `{ type: 'cancel', runId }` */\n cancel?: (runId: string) => Out;\n /**\n * The message that asks the server for a `STATE_SNAPSHOT`, sent when a `STATE_DELTA` can't\n * be applied. Without it, the state stays `stale` until the server sends one by itself.\n */\n resync?: (runId: string) => Out;\n /** Recognize a run's event. @default messages shaped like {@link AgUiEnvelope} */\n event?: (message: In) => AgUiEnvelope | null | undefined;\n /** What happens to a run when its connection drops. @default 'keep' */\n policy?: RunPolicy;\n /** Where streaming runs are remembered. @default sessionStorage */\n storage?: RunStorage | null;\n}\n\n/** An AG-UI agent over a WebSocket. Create with {@link createAgUiAgent}. */\nexport interface AgUiAgent<S> {\n /** The client the runs stream on. */\n readonly client: WebSocketClient<any, any>;\n /** The runs underneath, from `createWebSocketRuns`. */\n readonly runs: WebSocketRuns<RunAgentInput, AgUiThread<S>>;\n /**\n * Start a run with the thread so far plus `input.messages`, which show up in the thread right\n * away. A run that's still streaming is aborted first. Returns the run's id.\n */\n run(\n input?: {\n messages?: readonly AgUiMessage[];\n tools?: readonly unknown[];\n context?: readonly unknown[];\n forwardedProps?: unknown;\n },\n options?: { policy?: RunPolicy }\n ): string;\n /** Stop the run that's streaming, if any. */\n abort(): void;\n /**\n * Forget the thread, e.g. on logout or when another user signs in: its runs (with\n * `runs.clear()`, which sends nothing), messages and state. The next run starts over from\n * `state`, with a new thread id unless `threadId` was given. `abort()` a streaming run first\n * to stop it on the server.\n */\n reset(): void;\n /** The thread as of the latest run. The same object until it changes. */\n getSnapshot(): AgUiThread<S>;\n /** Get notified when the thread changes. Compatible with `useSyncExternalStore`. */\n subscribe(listener: () => void): () => void;\n /** Stop following runs, and release the connection. */\n dispose(): void;\n}\n\nfunction randomId() {\n return (\n globalThis.crypto?.randomUUID?.() ??\n `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`\n );\n}\n\nconst isEnvelope = (message: any): AgUiEnvelope | undefined =>\n typeof message?.runId === 'string' &&\n typeof message.offset === 'number' &&\n typeof message.event?.type === 'string'\n ? message\n : undefined;\n\nconst ENDS = new Set(['RUN_FINISHED', 'RUN_ERROR']);\n\n/**\n * Run an [AG-UI](https://docs.ag-ui.com) agent over a WebSocket. Each run streams AG-UI events,\n * which are folded into the thread's messages and shared state with {@link applyAgUiEvent}.\n * Runs come from `createWebSocketRuns`, so they resume after a reconnect or a reload, and a\n * state delta that can't be applied asks for a snapshot (`resync`).\n *\n * AG-UI doesn't define messages for WebSockets, so the defaults are small: the client sends\n * `{ type: 'run', runId, input }`, `{ type: 'resume', runId, after }` and\n * `{ type: 'cancel', runId }`, and the server sends `{ runId, offset, event }`, with `offset`\n * counting each run's events from 1. Each option replaces one of them.\n *\n * @example\n * ```ts\n * import { createAgUiAgent } from 'react-websocket/ag-ui';\n *\n * export const agent = createAgUiAgent(createWebSocket(url, { json: true }));\n *\n * agent.run({ messages: [{ id: crypto.randomUUID(), role: 'user', content: 'Plan my week' }] });\n * ```\n */\nexport function createAgUiAgent<S = unknown, In = any, Out = any>(\n client: WebSocketClient<In, Out>,\n options: AgUiAgentOptions<S, NoInfer<In>, NoInfer<Out>> = {}\n): AgUiAgent<S> {\n let threadId = options.threadId ?? randomId();\n const empty: AgUiThread<S> = {\n messages: [],\n state: options.state as S,\n status: 'idle',\n stale: false\n };\n let base = empty;\n let snapshot = empty;\n const listeners = new Set<() => void>();\n // Runs that asked for a snapshot after a failed delta, until one arrives.\n const resynced = new Set<string>();\n const event = options.event ?? isEnvelope;\n // Now or never: not queued, and not `volatile`, which can be a datagram that never arrives.\n const send = (message: unknown) => client.socket?.readyState === 1 && client.send(message as Out);\n\n const runs = createWebSocketRuns<RunAgentInput, AgUiThread<S>, In, Out>(client, {\n name: options.name ?? 'ag-ui',\n start: options.run ?? ((input, runId) => ({ type: 'run', runId, input }) as Out),\n resume: options.resume ?? ((runId, after) => ({ type: 'resume', runId, after }) as Out),\n cancel: options.cancel ?? (runId => ({ type: 'cancel', runId }) as Out),\n event: message => event(message),\n done: message => ENDS.has(event(message)!.event.type),\n initial: empty,\n reduce: (thread, message) => applyAgUiEvent(thread, event(message)!.event),\n policy: options.policy,\n storage: options.storage\n });\n\n function update() {\n const latest = runs.list().at(-1);\n let next = latest ? latest.value : base;\n if (latest?.status === 'aborted' && next.status === 'running')\n next = { ...next, status: 'aborted' };\n // Once each time the state goes stale, or on a later event if the socket wasn't open.\n if (latest && !latest.value.stale) resynced.delete(latest.runId);\n else if (latest && options.resync && !resynced.has(latest.runId)) {\n if (send(options.resync(latest.runId))) resynced.add(latest.runId);\n }\n if (next === snapshot) return;\n snapshot = next;\n for (const listener of [...listeners]) listener();\n }\n const off = runs.subscribe(update);\n update();\n\n function abort() {\n for (const run of runs.list()) runs.abort(run.runId);\n }\n\n return {\n client,\n runs,\n run(input = {}, runOptions) {\n abort();\n const runId = randomId();\n // The run starts from the state sent with it, the last good one: its deltas apply to it.\n base = {\n ...snapshot,\n stale: false,\n messages: [...snapshot.messages, ...(input.messages ?? [])]\n };\n runs.start(\n {\n threadId,\n runId,\n state: base.state,\n messages: base.messages,\n tools: input.tools ?? [],\n context: input.context ?? [],\n forwardedProps: input.forwardedProps ?? {}\n },\n { runId, initial: { ...base, status: 'running' }, policy: runOptions?.policy }\n );\n return runId;\n },\n abort,\n reset() {\n // Before the runs go, whose change shows `base`.\n base = empty;\n resynced.clear();\n if (options.threadId === undefined) threadId = randomId();\n runs.clear();\n update();\n },\n getSnapshot: () => snapshot,\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n dispose() {\n off();\n runs.dispose();\n }\n };\n}\n"],"mappings":";;;AAmFA,SAAS,SAAS,OAAe;CAC/B,OAAO,MAAM,QAAQ,OAAO,GAAG,CAAC,CAAC,QAAQ,OAAO,GAAG;AACrD;AAEA,SAAS,MAAM,GAAS,GAAkB;CACxC,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,YAAY,CAAC,KAAK,CAAC,GAAG,OAAO;CACvE,IAAI,MAAM,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,GAAG,OAAO;CAClD,MAAM,OAAO,OAAO,KAAK,CAAC;CAC1B,OAAO,KAAK,WAAW,OAAO,KAAK,CAAC,CAAC,CAAC,UAAU,KAAK,OAAM,QAAO,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC;AACzF;AAEA,SAAS,KAAK,WAAsC;CAClD,MAAM,IAAI,MAAM,sBAAsB,UAAU,GAAG,GAAG,UAAU,MAAM;AACxE;AAEA,MAAM,QAAQ;;AAGd,SAAS,OAAO,SAAiB,WAA+B;CAC9D,IAAI,YAAY,MAAM,QAAQ,OAA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AAS,CAAC;IACvB,SAAS,MAAM,WAAW,CAAC;IAC3B,gBAAgB,MAAM,kBAAkB,CAAC;GAC3C,GACA;IAAE;IAAO,SAAS;KAAE,GAAG;KAAM,QAAQ;IAAU;IAAG,QAAQ,YAAY;GAAO,CAC/E;GACA,OAAO;EACT;EACA;EACA,QAAQ;GAEN,OAAO;GACP,SAAS,MAAM;GACf,IAAI,QAAQ,aAAa,KAAA,GAAW,WAAW,SAAS;GACxD,KAAK,MAAM;GACX,OAAO;EACT;EACA,mBAAmB;EACnB,UAAU,UAAU;GAClB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;EACA,UAAU;GACR,IAAI;GACJ,KAAK,QAAQ;EACf;CACF;AACF"}
package/dist/ag-ui.d.cts CHANGED
@@ -28,7 +28,10 @@ type Status = 'idle' | 'connecting' | 'open' | 'reconnecting' | 'closed';
28
28
  interface Snapshot<In> {
29
29
  /** Connection state. */
30
30
  readonly status: Status;
31
- /** Most recent message (after `parse`/`json` and `schema`), kept across reconnects. */
31
+ /**
32
+ * Most recent message (after `parse`/`json` and `schema`), kept across reconnects and
33
+ * cleared by `close()`.
34
+ */
32
35
  readonly lastMessage: In | undefined;
33
36
  /**
34
37
  * Consecutive failed attempts since the last stable connection. A connection that closes
@@ -220,9 +223,10 @@ interface WebSocketClient<In = unknown, Out = unknown> {
220
223
  */
221
224
  connect(): void;
222
225
  /**
223
- * Close the connection and stop reconnecting. The status becomes `closed` and the
224
- * send queue is cleared. Mounted hooks stay mounted; call `connect()` or `reconnect()`
225
- * to start again. Closed while no hook holds it, the next hook to mount starts it again.
226
+ * Close the connection and stop reconnecting. The status becomes `closed`, and the send
227
+ * queue, a `migrate()` target and `lastMessage` are cleared, so call it on logout. Mounted
228
+ * hooks stay mounted; call `connect()` or `reconnect()` to start again. Closed while no hook
229
+ * holds it, the next hook to mount starts it again.
226
230
  *
227
231
  * Like `WebSocket.close()`, it throws for a code or reason the socket rejects (browsers take
228
232
  * `1000` or `3000`–`4999`, and a reason up to 123 bytes), and then nothing has changed.
@@ -231,7 +235,8 @@ interface WebSocketClient<In = unknown, Out = unknown> {
231
235
  close(code?: number, reason?: string): void;
232
236
  /**
233
237
  * Drop the current socket (if any) and connect again immediately, resetting the
234
- * backoff. Also restarts a `closed` client.
238
+ * backoff. Also restarts a `closed` client. The send queue and a `migrate()` target are
239
+ * kept: when another user signs in, call `close()` and then `connect()` instead.
235
240
  */
236
241
  reconnect(): void;
237
242
  /**
@@ -317,6 +322,13 @@ interface WebSocketRuns<Req, T> {
317
322
  subscribe(listener: () => void): () => void;
318
323
  /** Forget a run, aborting it first if it's streaming. */
319
324
  delete(runId: string): void;
325
+ /**
326
+ * Forget every run, e.g. on logout or when another user signs in: their values, unsent start
327
+ * requests and pending cancels, here and in `storage`. Sends nothing, so nothing of the
328
+ * previous user's goes out on a later connection; `abort()` runs that should stop on the
329
+ * server first, while connected.
330
+ */
331
+ clear(): void;
320
332
  /** Stop following runs, and release the connection. Remembered runs stay remembered. */
321
333
  dispose(): void;
322
334
  }
@@ -461,6 +473,13 @@ export interface AgUiAgent<S> {
461
473
  }): string;
462
474
  /** Stop the run that's streaming, if any. */
463
475
  abort(): void;
476
+ /**
477
+ * Forget the thread, e.g. on logout or when another user signs in: its runs (with
478
+ * `runs.clear()`, which sends nothing), messages and state. The next run starts over from
479
+ * `state`, with a new thread id unless `threadId` was given. `abort()` a streaming run first
480
+ * to stop it on the server.
481
+ */
482
+ reset(): void;
464
483
  /** The thread as of the latest run. The same object until it changes. */
465
484
  getSnapshot(): AgUiThread<S>;
466
485
  /** Get notified when the thread changes. Compatible with `useSyncExternalStore`. */
@@ -490,4 +509,5 @@ export interface AgUiAgent<S> {
490
509
  */
491
510
  export declare function createAgUiAgent<S = unknown, In = any, Out = any>(client: WebSocketClient<In, Out>, options?: AgUiAgentOptions<S, NoInfer<In>, NoInfer<Out>>): AgUiAgent<S>;
492
511
  //#endregion
512
+ export {};
493
513
  //# sourceMappingURL=ag-ui.d.cts.map
package/dist/ag-ui.d.ts CHANGED
@@ -139,6 +139,13 @@ export interface AgUiAgent<S> {
139
139
  }): string;
140
140
  /** Stop the run that's streaming, if any. */
141
141
  abort(): void;
142
+ /**
143
+ * Forget the thread, e.g. on logout or when another user signs in: its runs (with
144
+ * `runs.clear()`, which sends nothing), messages and state. The next run starts over from
145
+ * `state`, with a new thread id unless `threadId` was given. `abort()` a streaming run first
146
+ * to stop it on the server.
147
+ */
148
+ reset(): void;
142
149
  /** The thread as of the latest run. The same object until it changes. */
143
150
  getSnapshot(): AgUiThread<S>;
144
151
  /** Get notified when the thread changes. Compatible with `useSyncExternalStore`. */
package/dist/ag-ui.js CHANGED
@@ -313,7 +313,7 @@ const ENDS = /* @__PURE__ */ new Set(["RUN_FINISHED", "RUN_ERROR"]);
313
313
  * ```
314
314
  */
315
315
  function createAgUiAgent(client, options = {}) {
316
- const threadId = options.threadId ?? randomId();
316
+ let threadId = options.threadId ?? randomId();
317
317
  const empty = {
318
318
  messages: [],
319
319
  state: options.state,
@@ -399,6 +399,13 @@ function createAgUiAgent(client, options = {}) {
399
399
  return runId;
400
400
  },
401
401
  abort,
402
+ reset() {
403
+ base = empty;
404
+ resynced.clear();
405
+ if (options.threadId === void 0) threadId = randomId();
406
+ runs.clear();
407
+ update();
408
+ },
402
409
  getSnapshot: () => snapshot,
403
410
  subscribe(listener) {
404
411
  listeners.add(listener);
package/dist/ag-ui.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ag-ui.js","names":[],"sources":["../src/ag-ui.ts"],"sourcesContent":["// The build keeps `./core` external, so this entry shares the client with the others.\nimport {\n createWebSocketRuns,\n type RunPolicy,\n type RunStorage,\n type WebSocketClient,\n type WebSocketRuns\n} from './core';\n\n/** A JSON Patch (RFC 6902) operation, as in AG-UI's `STATE_DELTA` and `ACTIVITY_DELTA`. */\nexport interface JsonPatchOperation {\n readonly op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n /** A JSON Pointer (RFC 6901), such as `/items/0/name`. */\n readonly path: string;\n /** The source of `move` and `copy`. */\n readonly from?: string;\n /** The value to `add`, `replace` with or `test` for. */\n readonly value?: unknown;\n}\n\n/** An AG-UI event: `type` is `'RUN_STARTED'`, `'TEXT_MESSAGE_CONTENT'`, `'STATE_DELTA'`, … */\nexport interface AgUiEvent {\n readonly type: string;\n readonly [field: string]: any;\n}\n\n/** A tool call on an assistant message, as in AG-UI. */\nexport interface AgUiToolCall {\n readonly id: string;\n readonly type: 'function';\n readonly function: { readonly name: string; readonly arguments: string };\n}\n\n/** A message in an AG-UI thread. */\nexport interface AgUiMessage {\n readonly id: string;\n readonly role: 'developer' | 'system' | 'assistant' | 'user' | 'tool' | 'reasoning' | 'activity';\n readonly content?: unknown;\n readonly toolCalls?: readonly AgUiToolCall[];\n /** For a `tool` message, the call it answers. */\n readonly toolCallId?: string;\n /** For an `activity` message. */\n readonly activityType?: string;\n readonly [field: string]: unknown;\n}\n\n/** AG-UI's `RunAgentInput`: what a run starts from. */\nexport interface RunAgentInput {\n readonly threadId: string;\n readonly runId: string;\n readonly parentRunId?: string;\n readonly state?: unknown;\n readonly messages: readonly AgUiMessage[];\n readonly tools: readonly unknown[];\n readonly context: readonly unknown[];\n readonly forwardedProps: unknown;\n}\n\n/** A thread's messages and shared state, reduced from AG-UI events. */\nexport interface AgUiThread<S = unknown> {\n readonly messages: readonly AgUiMessage[];\n /** The shared state, from `STATE_SNAPSHOT` and `STATE_DELTA`. */\n readonly state: S;\n /**\n * `'running'` from `RUN_STARTED`, `'finished'` after `RUN_FINISHED`, `'error'` after\n * `RUN_ERROR`, `'aborted'` when the run was stopped from this side.\n */\n readonly status: 'idle' | 'running' | 'finished' | 'error' | 'aborted';\n /** The step in progress, between `STEP_STARTED` and `STEP_FINISHED`. */\n readonly step?: string;\n /** From `RUN_ERROR`. */\n readonly error?: { readonly message: string; readonly code?: string };\n /** From `RUN_FINISHED`. */\n readonly result?: unknown;\n /**\n * A `STATE_DELTA` couldn't be applied. `state` is the last good one, and later deltas are\n * skipped until a `STATE_SNAPSHOT` arrives.\n */\n readonly stale: boolean;\n}\n\ntype Json = any;\n\nfunction unescape(token: string) {\n return token.replace(/~1/g, '/').replace(/~0/g, '~');\n}\n\nfunction equal(a: Json, b: Json): boolean {\n if (a === b) return true;\n if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n const keys = Object.keys(a);\n return keys.length === Object.keys(b).length && keys.every(key => equal(a[key], b[key]));\n}\n\nfunction fail(operation: JsonPatchOperation): never {\n throw new Error(`JSON Patch failed: ${operation.op} ${operation.path}`);\n}\n\nconst INDEX = /^(0|[1-9]\\d*)$/;\n\n/** A JSON Pointer's tokens. It's empty, for the whole document, or starts with `/`. */\nfunction tokens(pointer: string, operation: JsonPatchOperation) {\n if (pointer !== '' && pointer[0] !== '/') fail(operation);\n return pointer.split('/').slice(1).map(unescape);\n}\n\n/** Whether `node` has `token`: an index in range for an array, not `length`; an own key otherwise. */\nfunction has(node: Json, token: string) {\n return Array.isArray(node)\n ? INDEX.test(token) && +token < node.length\n : Object.hasOwn(node, token);\n}\n\n/** Set an own property, even `__proto__`, as JSON.parse would. */\nfunction put(object: Json, key: string, value: Json) {\n Object.defineProperty(object, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true\n });\n}\n\n/**\n * Apply a JSON Patch (RFC 6902) without changing `document`: what changes is copied, the rest is\n * shared. Throws, leaving `document` as it was, if an operation can't be applied: a missing path,\n * an index out of range, a pointer that doesn't start with `/`, or a failed `test`.\n */\nexport function applyPatch<T>(document: T, operations: readonly JsonPatchOperation[]): T {\n let root: Json = document;\n\n function get(path: string, operation: JsonPatchOperation): Json {\n let node = root;\n for (const token of tokens(path, operation)) {\n if (typeof node !== 'object' || node === null || !has(node, token)) fail(operation);\n node = node[token];\n }\n return node;\n }\n\n // Replace the parent of `path` with a copy, change it with `change`, and copy its ancestors.\n function edit(\n path: string,\n operation: JsonPatchOperation,\n change: (parent: Json, key: string) => void\n ) {\n const keys = tokens(path, operation);\n if (!keys.length) fail(operation);\n const walk = (node: Json, depth: number): Json => {\n if (typeof node !== 'object' || node === null) fail(operation);\n const copy = Array.isArray(node) ? [...node] : { ...node };\n const key = keys[depth]!;\n if (depth === keys.length - 1) change(copy, key);\n else if (!has(node, key)) fail(operation);\n else copy[key] = walk(node[key], depth + 1);\n return copy;\n };\n root = walk(root, 0);\n }\n\n function index(parent: Json[], key: string, operation: JsonPatchOperation, adding: boolean) {\n // `-` is the end of the array, which only `add` can use.\n const i = key === '-' ? (adding ? parent.length : -1) : INDEX.test(key) ? +key : -1;\n if (i < 0 || i > parent.length - (adding ? 0 : 1)) fail(operation);\n return i;\n }\n\n function add(path: string, value: Json, operation: JsonPatchOperation) {\n if (path === '') return void (root = value);\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, true), 0, value);\n else put(parent, key, value);\n });\n }\n\n function remove(path: string, operation: JsonPatchOperation) {\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, false), 1);\n else if (Object.hasOwn(parent, key)) delete parent[key];\n else fail(operation);\n });\n }\n\n for (const operation of operations) {\n const { op, path, from, value } = operation;\n if (op === 'add') add(path, value, operation);\n else if (op === 'remove') remove(path, operation);\n else if (op === 'replace') {\n get(path, operation);\n if (path === '') root = value;\n else\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent[key as unknown as number] = value;\n else put(parent, key, value);\n });\n } else if (op === 'move' || op === 'copy') {\n const moved = get(from ?? fail(operation), operation);\n if (op === 'move') remove(from!, operation);\n add(path, op === 'copy' ? structuredClone(moved) : moved, operation);\n } else if (op === 'test') {\n if (!equal(get(path, operation), value)) fail(operation);\n } else fail(operation);\n }\n return root;\n}\n\nfunction updateMessage(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (message: AgUiMessage) => AgUiMessage\n) {\n return thread.messages.map(message => (message.id === id ? change(message) : message));\n}\n\nfunction appendText(message: AgUiMessage, delta: string | undefined) {\n return { ...message, content: `${(message.content as string | undefined) ?? ''}${delta ?? ''}` };\n}\n\nfunction updateToolCall(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (call: AgUiToolCall) => AgUiToolCall\n) {\n return thread.messages.map(message =>\n message.toolCalls?.some(call => call.id === id)\n ? {\n ...message,\n toolCalls: message.toolCalls.map(call => (call.id === id ? change(call) : call))\n }\n : message\n );\n}\n\n/** The last message with a tool call, for chunks that leave out `toolCallId`. */\nfunction lastToolCallId(thread: AgUiThread<any>) {\n for (let i = thread.messages.length - 1; i >= 0; i--) {\n const calls = thread.messages[i]!.toolCalls;\n if (calls?.length) return calls.at(-1)!.id;\n }\n}\n\n/**\n * Fold one AG-UI event into a thread, without changing it. Handles the run lifecycle, steps,\n * text, tool calls and their results, reasoning, activity, and message and state snapshots and\n * deltas. A `STATE_DELTA` that can't be applied marks the thread `stale` instead of throwing.\n * Other events, such as `CUSTOM` and `RAW`, return the thread unchanged.\n *\n * Use it as a `reduce` function, or wrap it to handle events of your own.\n */\nexport function applyAgUiEvent<S>(thread: AgUiThread<S>, event: AgUiEvent): AgUiThread<S> {\n const e = event;\n const message = (role: AgUiMessage['role'], extra?: object): AgUiThread<S> => ({\n ...thread,\n messages: [...thread.messages, { id: e.messageId, role, content: '', ...extra }]\n });\n const known = (id: string | undefined) => thread.messages.some(m => m.id === id);\n switch (e.type) {\n case 'RUN_STARTED':\n return { ...thread, status: 'running', error: undefined, result: undefined };\n case 'RUN_FINISHED':\n return { ...thread, status: 'finished', step: undefined, result: e.result };\n case 'RUN_ERROR':\n return {\n ...thread,\n status: 'error',\n step: undefined,\n error: { message: e.message, code: e.code }\n };\n case 'STEP_STARTED':\n return { ...thread, step: e.stepName };\n case 'STEP_FINISHED':\n return { ...thread, step: undefined };\n case 'TEXT_MESSAGE_START':\n return message(e.role ?? 'assistant');\n case 'REASONING_MESSAGE_START':\n return message('reasoning');\n case 'TEXT_MESSAGE_CONTENT':\n case 'REASONING_MESSAGE_CONTENT':\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => appendText(m, e.delta))\n };\n case 'TEXT_MESSAGE_CHUNK':\n case 'REASONING_MESSAGE_CHUNK': {\n // Only the first chunk of a message must carry its id.\n const id = e.messageId ?? thread.messages.at(-1)?.id;\n const role = e.type === 'TEXT_MESSAGE_CHUNK' ? (e.role ?? 'assistant') : 'reasoning';\n const next = known(id) ? thread : message(role);\n return { ...next, messages: updateMessage(next, id, m => appendText(m, e.delta)) };\n }\n case 'TOOL_CALL_START':\n case 'TOOL_CALL_CHUNK': {\n const id = e.toolCallId ?? lastToolCallId(thread);\n let next = thread;\n if (\n e.type === 'TOOL_CALL_START' ||\n !thread.messages.some(m => m.toolCalls?.some(c => c.id === id))\n ) {\n const call: AgUiToolCall = {\n id,\n type: 'function',\n function: { name: e.toolCallName, arguments: '' }\n };\n const parent = e.parentMessageId ?? id;\n next = known(parent)\n ? thread\n : { ...thread, messages: [...thread.messages, { id: parent, role: 'assistant' }] };\n next = {\n ...next,\n messages: updateMessage(next, parent, m => ({\n ...m,\n toolCalls: [...(m.toolCalls ?? []), call]\n }))\n };\n }\n if (e.type === 'TOOL_CALL_START') return next;\n return toolArgs(next, id, e.delta);\n }\n case 'TOOL_CALL_ARGS':\n return toolArgs(thread, e.toolCallId, e.delta);\n case 'TOOL_CALL_RESULT':\n return message(e.role ?? 'tool', { toolCallId: e.toolCallId, content: e.content });\n case 'STATE_SNAPSHOT':\n return { ...thread, state: e.snapshot, stale: false };\n case 'STATE_DELTA':\n if (thread.stale) return thread;\n try {\n return { ...thread, state: applyPatch(thread.state, e.delta) };\n } catch {\n return { ...thread, stale: true };\n }\n case 'MESSAGES_SNAPSHOT':\n return { ...thread, messages: e.messages };\n case 'ACTIVITY_SNAPSHOT': {\n if (known(e.messageId) && e.replace === false) return thread;\n const next = known(e.messageId) ? thread : message('activity');\n return {\n ...next,\n messages: updateMessage(next, e.messageId, m => ({\n ...m,\n activityType: e.activityType,\n content: e.content\n }))\n };\n }\n case 'ACTIVITY_DELTA':\n try {\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => ({\n ...m,\n content: applyPatch(m.content, e.patch)\n }))\n };\n } catch {\n return thread;\n }\n default:\n return thread;\n }\n}\n\nfunction toolArgs<S>(thread: AgUiThread<S>, id: string | undefined, delta: string | undefined) {\n return {\n ...thread,\n messages: updateToolCall(thread, id, call => ({\n ...call,\n function: { ...call.function, arguments: call.function.arguments + (delta ?? '') }\n }))\n };\n}\n\n/** What the server sends for each event of a run: its run, its offset (from 1), and the event. */\nexport interface AgUiEnvelope {\n readonly runId: string;\n readonly offset: number;\n readonly event: AgUiEvent;\n}\n\n/** Options for {@link createAgUiAgent}. The message formats have defaults; see the guide. */\nexport interface AgUiAgentOptions<S, In, Out> {\n /** The thread's id, sent in every `RunAgentInput`. A new random one by default. */\n threadId?: string;\n /** The shared state before the first `STATE_SNAPSHOT`, sent with the first run. */\n state?: S;\n /** Names where streaming runs are remembered across reloads. @default 'ag-ui' */\n name?: string;\n /** @default `{ type: 'run', runId, input }` */\n run?: (input: RunAgentInput, runId: string) => Out;\n /** @default `{ type: 'resume', runId, after }` */\n resume?: (runId: string, after: number | undefined) => Out;\n /** @default `{ type: 'cancel', runId }` */\n cancel?: (runId: string) => Out;\n /**\n * The message that asks the server for a `STATE_SNAPSHOT`, sent when a `STATE_DELTA` can't\n * be applied. Without it, the state stays `stale` until the server sends one by itself.\n */\n resync?: (runId: string) => Out;\n /** Recognize a run's event. @default messages shaped like {@link AgUiEnvelope} */\n event?: (message: In) => AgUiEnvelope | null | undefined;\n /** What happens to a run when its connection drops. @default 'keep' */\n policy?: RunPolicy;\n /** Where streaming runs are remembered. @default sessionStorage */\n storage?: RunStorage | null;\n}\n\n/** An AG-UI agent over a WebSocket. Create with {@link createAgUiAgent}. */\nexport interface AgUiAgent<S> {\n /** The client the runs stream on. */\n readonly client: WebSocketClient<any, any>;\n /** The runs underneath, from `createWebSocketRuns`. */\n readonly runs: WebSocketRuns<RunAgentInput, AgUiThread<S>>;\n /**\n * Start a run with the thread so far plus `input.messages`, which show up in the thread right\n * away. A run that's still streaming is aborted first. Returns the run's id.\n */\n run(\n input?: {\n messages?: readonly AgUiMessage[];\n tools?: readonly unknown[];\n context?: readonly unknown[];\n forwardedProps?: unknown;\n },\n options?: { policy?: RunPolicy }\n ): string;\n /** Stop the run that's streaming, if any. */\n abort(): void;\n /** The thread as of the latest run. The same object until it changes. */\n getSnapshot(): AgUiThread<S>;\n /** Get notified when the thread changes. Compatible with `useSyncExternalStore`. */\n subscribe(listener: () => void): () => void;\n /** Stop following runs, and release the connection. */\n dispose(): void;\n}\n\nfunction randomId() {\n return (\n globalThis.crypto?.randomUUID?.() ??\n `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`\n );\n}\n\nconst isEnvelope = (message: any): AgUiEnvelope | undefined =>\n typeof message?.runId === 'string' &&\n typeof message.offset === 'number' &&\n typeof message.event?.type === 'string'\n ? message\n : undefined;\n\nconst ENDS = new Set(['RUN_FINISHED', 'RUN_ERROR']);\n\n/**\n * Run an [AG-UI](https://docs.ag-ui.com) agent over a WebSocket. Each run streams AG-UI events,\n * which are folded into the thread's messages and shared state with {@link applyAgUiEvent}.\n * Runs come from `createWebSocketRuns`, so they resume after a reconnect or a reload, and a\n * state delta that can't be applied asks for a snapshot (`resync`).\n *\n * AG-UI doesn't define messages for WebSockets, so the defaults are small: the client sends\n * `{ type: 'run', runId, input }`, `{ type: 'resume', runId, after }` and\n * `{ type: 'cancel', runId }`, and the server sends `{ runId, offset, event }`, with `offset`\n * counting each run's events from 1. Each option replaces one of them.\n *\n * @example\n * ```ts\n * import { createAgUiAgent } from 'react-websocket/ag-ui';\n *\n * export const agent = createAgUiAgent(createWebSocket(url, { json: true }));\n *\n * agent.run({ messages: [{ id: crypto.randomUUID(), role: 'user', content: 'Plan my week' }] });\n * ```\n */\nexport function createAgUiAgent<S = unknown, In = any, Out = any>(\n client: WebSocketClient<In, Out>,\n options: AgUiAgentOptions<S, NoInfer<In>, NoInfer<Out>> = {}\n): AgUiAgent<S> {\n const threadId = options.threadId ?? randomId();\n const empty: AgUiThread<S> = {\n messages: [],\n state: options.state as S,\n status: 'idle',\n stale: false\n };\n let base = empty;\n let snapshot = empty;\n const listeners = new Set<() => void>();\n // Runs that asked for a snapshot after a failed delta, until one arrives.\n const resynced = new Set<string>();\n const event = options.event ?? isEnvelope;\n // Now or never: not queued, and not `volatile`, which can be a datagram that never arrives.\n const send = (message: unknown) => client.socket?.readyState === 1 && client.send(message as Out);\n\n const runs = createWebSocketRuns<RunAgentInput, AgUiThread<S>, In, Out>(client, {\n name: options.name ?? 'ag-ui',\n start: options.run ?? ((input, runId) => ({ type: 'run', runId, input }) as Out),\n resume: options.resume ?? ((runId, after) => ({ type: 'resume', runId, after }) as Out),\n cancel: options.cancel ?? (runId => ({ type: 'cancel', runId }) as Out),\n event: message => event(message),\n done: message => ENDS.has(event(message)!.event.type),\n initial: empty,\n reduce: (thread, message) => applyAgUiEvent(thread, event(message)!.event),\n policy: options.policy,\n storage: options.storage\n });\n\n function update() {\n const latest = runs.list().at(-1);\n let next = latest ? latest.value : base;\n if (latest?.status === 'aborted' && next.status === 'running')\n next = { ...next, status: 'aborted' };\n // Once each time the state goes stale, or on a later event if the socket wasn't open.\n if (latest && !latest.value.stale) resynced.delete(latest.runId);\n else if (latest && options.resync && !resynced.has(latest.runId)) {\n if (send(options.resync(latest.runId))) resynced.add(latest.runId);\n }\n if (next === snapshot) return;\n snapshot = next;\n for (const listener of [...listeners]) listener();\n }\n const off = runs.subscribe(update);\n update();\n\n function abort() {\n for (const run of runs.list()) runs.abort(run.runId);\n }\n\n return {\n client,\n runs,\n run(input = {}, runOptions) {\n abort();\n const runId = randomId();\n // The run starts from the state sent with it, the last good one: its deltas apply to it.\n base = {\n ...snapshot,\n stale: false,\n messages: [...snapshot.messages, ...(input.messages ?? [])]\n };\n runs.start(\n {\n threadId,\n runId,\n state: base.state,\n messages: base.messages,\n tools: input.tools ?? [],\n context: input.context ?? [],\n forwardedProps: input.forwardedProps ?? {}\n },\n { runId, initial: { ...base, status: 'running' }, policy: runOptions?.policy }\n );\n return runId;\n },\n abort,\n getSnapshot: () => snapshot,\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n dispose() {\n off();\n runs.dispose();\n }\n };\n}\n"],"mappings":";;AAmFA,SAAS,SAAS,OAAe;CAC/B,OAAO,MAAM,QAAQ,OAAO,GAAG,CAAC,CAAC,QAAQ,OAAO,GAAG;AACrD;AAEA,SAAS,MAAM,GAAS,GAAkB;CACxC,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,YAAY,CAAC,KAAK,CAAC,GAAG,OAAO;CACvE,IAAI,MAAM,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,GAAG,OAAO;CAClD,MAAM,OAAO,OAAO,KAAK,CAAC;CAC1B,OAAO,KAAK,WAAW,OAAO,KAAK,CAAC,CAAC,CAAC,UAAU,KAAK,OAAM,QAAO,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC;AACzF;AAEA,SAAS,KAAK,WAAsC;CAClD,MAAM,IAAI,MAAM,sBAAsB,UAAU,GAAG,GAAG,UAAU,MAAM;AACxE;AAEA,MAAM,QAAQ;;AAGd,SAAS,OAAO,SAAiB,WAA+B;CAC9D,IAAI,YAAY,MAAM,QAAQ,OAAO,KAAK,KAAK,SAAS;CACxD,OAAO,QAAQ,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,QAAQ;AACjD;;AAGA,SAAS,IAAI,MAAY,OAAe;CACtC,OAAO,MAAM,QAAQ,IAAI,IACrB,MAAM,KAAK,KAAK,KAAK,CAAC,QAAQ,KAAK,SACnC,OAAO,OAAO,MAAM,KAAK;AAC/B;;AAGA,SAAS,IAAI,QAAc,KAAa,OAAa;CACnD,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,KAAK,QAAQ;EACf;CACF;AACF"}
1
+ {"version":3,"file":"ag-ui.js","names":[],"sources":["../src/ag-ui.ts"],"sourcesContent":["// The build keeps `./core` external, so this entry shares the client with the others.\nimport {\n createWebSocketRuns,\n type RunPolicy,\n type RunStorage,\n type WebSocketClient,\n type WebSocketRuns\n} from './core';\n\n/** A JSON Patch (RFC 6902) operation, as in AG-UI's `STATE_DELTA` and `ACTIVITY_DELTA`. */\nexport interface JsonPatchOperation {\n readonly op: 'add' | 'remove' | 'replace' | 'move' | 'copy' | 'test';\n /** A JSON Pointer (RFC 6901), such as `/items/0/name`. */\n readonly path: string;\n /** The source of `move` and `copy`. */\n readonly from?: string;\n /** The value to `add`, `replace` with or `test` for. */\n readonly value?: unknown;\n}\n\n/** An AG-UI event: `type` is `'RUN_STARTED'`, `'TEXT_MESSAGE_CONTENT'`, `'STATE_DELTA'`, … */\nexport interface AgUiEvent {\n readonly type: string;\n readonly [field: string]: any;\n}\n\n/** A tool call on an assistant message, as in AG-UI. */\nexport interface AgUiToolCall {\n readonly id: string;\n readonly type: 'function';\n readonly function: { readonly name: string; readonly arguments: string };\n}\n\n/** A message in an AG-UI thread. */\nexport interface AgUiMessage {\n readonly id: string;\n readonly role: 'developer' | 'system' | 'assistant' | 'user' | 'tool' | 'reasoning' | 'activity';\n readonly content?: unknown;\n readonly toolCalls?: readonly AgUiToolCall[];\n /** For a `tool` message, the call it answers. */\n readonly toolCallId?: string;\n /** For an `activity` message. */\n readonly activityType?: string;\n readonly [field: string]: unknown;\n}\n\n/** AG-UI's `RunAgentInput`: what a run starts from. */\nexport interface RunAgentInput {\n readonly threadId: string;\n readonly runId: string;\n readonly parentRunId?: string;\n readonly state?: unknown;\n readonly messages: readonly AgUiMessage[];\n readonly tools: readonly unknown[];\n readonly context: readonly unknown[];\n readonly forwardedProps: unknown;\n}\n\n/** A thread's messages and shared state, reduced from AG-UI events. */\nexport interface AgUiThread<S = unknown> {\n readonly messages: readonly AgUiMessage[];\n /** The shared state, from `STATE_SNAPSHOT` and `STATE_DELTA`. */\n readonly state: S;\n /**\n * `'running'` from `RUN_STARTED`, `'finished'` after `RUN_FINISHED`, `'error'` after\n * `RUN_ERROR`, `'aborted'` when the run was stopped from this side.\n */\n readonly status: 'idle' | 'running' | 'finished' | 'error' | 'aborted';\n /** The step in progress, between `STEP_STARTED` and `STEP_FINISHED`. */\n readonly step?: string;\n /** From `RUN_ERROR`. */\n readonly error?: { readonly message: string; readonly code?: string };\n /** From `RUN_FINISHED`. */\n readonly result?: unknown;\n /**\n * A `STATE_DELTA` couldn't be applied. `state` is the last good one, and later deltas are\n * skipped until a `STATE_SNAPSHOT` arrives.\n */\n readonly stale: boolean;\n}\n\ntype Json = any;\n\nfunction unescape(token: string) {\n return token.replace(/~1/g, '/').replace(/~0/g, '~');\n}\n\nfunction equal(a: Json, b: Json): boolean {\n if (a === b) return true;\n if (typeof a !== 'object' || typeof b !== 'object' || !a || !b) return false;\n if (Array.isArray(a) !== Array.isArray(b)) return false;\n const keys = Object.keys(a);\n return keys.length === Object.keys(b).length && keys.every(key => equal(a[key], b[key]));\n}\n\nfunction fail(operation: JsonPatchOperation): never {\n throw new Error(`JSON Patch failed: ${operation.op} ${operation.path}`);\n}\n\nconst INDEX = /^(0|[1-9]\\d*)$/;\n\n/** A JSON Pointer's tokens. It's empty, for the whole document, or starts with `/`. */\nfunction tokens(pointer: string, operation: JsonPatchOperation) {\n if (pointer !== '' && pointer[0] !== '/') fail(operation);\n return pointer.split('/').slice(1).map(unescape);\n}\n\n/** Whether `node` has `token`: an index in range for an array, not `length`; an own key otherwise. */\nfunction has(node: Json, token: string) {\n return Array.isArray(node)\n ? INDEX.test(token) && +token < node.length\n : Object.hasOwn(node, token);\n}\n\n/** Set an own property, even `__proto__`, as JSON.parse would. */\nfunction put(object: Json, key: string, value: Json) {\n Object.defineProperty(object, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true\n });\n}\n\n/**\n * Apply a JSON Patch (RFC 6902) without changing `document`: what changes is copied, the rest is\n * shared. Throws, leaving `document` as it was, if an operation can't be applied: a missing path,\n * an index out of range, a pointer that doesn't start with `/`, or a failed `test`.\n */\nexport function applyPatch<T>(document: T, operations: readonly JsonPatchOperation[]): T {\n let root: Json = document;\n\n function get(path: string, operation: JsonPatchOperation): Json {\n let node = root;\n for (const token of tokens(path, operation)) {\n if (typeof node !== 'object' || node === null || !has(node, token)) fail(operation);\n node = node[token];\n }\n return node;\n }\n\n // Replace the parent of `path` with a copy, change it with `change`, and copy its ancestors.\n function edit(\n path: string,\n operation: JsonPatchOperation,\n change: (parent: Json, key: string) => void\n ) {\n const keys = tokens(path, operation);\n if (!keys.length) fail(operation);\n const walk = (node: Json, depth: number): Json => {\n if (typeof node !== 'object' || node === null) fail(operation);\n const copy = Array.isArray(node) ? [...node] : { ...node };\n const key = keys[depth]!;\n if (depth === keys.length - 1) change(copy, key);\n else if (!has(node, key)) fail(operation);\n else copy[key] = walk(node[key], depth + 1);\n return copy;\n };\n root = walk(root, 0);\n }\n\n function index(parent: Json[], key: string, operation: JsonPatchOperation, adding: boolean) {\n // `-` is the end of the array, which only `add` can use.\n const i = key === '-' ? (adding ? parent.length : -1) : INDEX.test(key) ? +key : -1;\n if (i < 0 || i > parent.length - (adding ? 0 : 1)) fail(operation);\n return i;\n }\n\n function add(path: string, value: Json, operation: JsonPatchOperation) {\n if (path === '') return void (root = value);\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, true), 0, value);\n else put(parent, key, value);\n });\n }\n\n function remove(path: string, operation: JsonPatchOperation) {\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent.splice(index(parent, key, operation, false), 1);\n else if (Object.hasOwn(parent, key)) delete parent[key];\n else fail(operation);\n });\n }\n\n for (const operation of operations) {\n const { op, path, from, value } = operation;\n if (op === 'add') add(path, value, operation);\n else if (op === 'remove') remove(path, operation);\n else if (op === 'replace') {\n get(path, operation);\n if (path === '') root = value;\n else\n edit(path, operation, (parent, key) => {\n if (Array.isArray(parent)) parent[key as unknown as number] = value;\n else put(parent, key, value);\n });\n } else if (op === 'move' || op === 'copy') {\n const moved = get(from ?? fail(operation), operation);\n if (op === 'move') remove(from!, operation);\n add(path, op === 'copy' ? structuredClone(moved) : moved, operation);\n } else if (op === 'test') {\n if (!equal(get(path, operation), value)) fail(operation);\n } else fail(operation);\n }\n return root;\n}\n\nfunction updateMessage(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (message: AgUiMessage) => AgUiMessage\n) {\n return thread.messages.map(message => (message.id === id ? change(message) : message));\n}\n\nfunction appendText(message: AgUiMessage, delta: string | undefined) {\n return { ...message, content: `${(message.content as string | undefined) ?? ''}${delta ?? ''}` };\n}\n\nfunction updateToolCall(\n thread: AgUiThread<any>,\n id: string | undefined,\n change: (call: AgUiToolCall) => AgUiToolCall\n) {\n return thread.messages.map(message =>\n message.toolCalls?.some(call => call.id === id)\n ? {\n ...message,\n toolCalls: message.toolCalls.map(call => (call.id === id ? change(call) : call))\n }\n : message\n );\n}\n\n/** The last message with a tool call, for chunks that leave out `toolCallId`. */\nfunction lastToolCallId(thread: AgUiThread<any>) {\n for (let i = thread.messages.length - 1; i >= 0; i--) {\n const calls = thread.messages[i]!.toolCalls;\n if (calls?.length) return calls.at(-1)!.id;\n }\n}\n\n/**\n * Fold one AG-UI event into a thread, without changing it. Handles the run lifecycle, steps,\n * text, tool calls and their results, reasoning, activity, and message and state snapshots and\n * deltas. A `STATE_DELTA` that can't be applied marks the thread `stale` instead of throwing.\n * Other events, such as `CUSTOM` and `RAW`, return the thread unchanged.\n *\n * Use it as a `reduce` function, or wrap it to handle events of your own.\n */\nexport function applyAgUiEvent<S>(thread: AgUiThread<S>, event: AgUiEvent): AgUiThread<S> {\n const e = event;\n const message = (role: AgUiMessage['role'], extra?: object): AgUiThread<S> => ({\n ...thread,\n messages: [...thread.messages, { id: e.messageId, role, content: '', ...extra }]\n });\n const known = (id: string | undefined) => thread.messages.some(m => m.id === id);\n switch (e.type) {\n case 'RUN_STARTED':\n return { ...thread, status: 'running', error: undefined, result: undefined };\n case 'RUN_FINISHED':\n return { ...thread, status: 'finished', step: undefined, result: e.result };\n case 'RUN_ERROR':\n return {\n ...thread,\n status: 'error',\n step: undefined,\n error: { message: e.message, code: e.code }\n };\n case 'STEP_STARTED':\n return { ...thread, step: e.stepName };\n case 'STEP_FINISHED':\n return { ...thread, step: undefined };\n case 'TEXT_MESSAGE_START':\n return message(e.role ?? 'assistant');\n case 'REASONING_MESSAGE_START':\n return message('reasoning');\n case 'TEXT_MESSAGE_CONTENT':\n case 'REASONING_MESSAGE_CONTENT':\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => appendText(m, e.delta))\n };\n case 'TEXT_MESSAGE_CHUNK':\n case 'REASONING_MESSAGE_CHUNK': {\n // Only the first chunk of a message must carry its id.\n const id = e.messageId ?? thread.messages.at(-1)?.id;\n const role = e.type === 'TEXT_MESSAGE_CHUNK' ? (e.role ?? 'assistant') : 'reasoning';\n const next = known(id) ? thread : message(role);\n return { ...next, messages: updateMessage(next, id, m => appendText(m, e.delta)) };\n }\n case 'TOOL_CALL_START':\n case 'TOOL_CALL_CHUNK': {\n const id = e.toolCallId ?? lastToolCallId(thread);\n let next = thread;\n if (\n e.type === 'TOOL_CALL_START' ||\n !thread.messages.some(m => m.toolCalls?.some(c => c.id === id))\n ) {\n const call: AgUiToolCall = {\n id,\n type: 'function',\n function: { name: e.toolCallName, arguments: '' }\n };\n const parent = e.parentMessageId ?? id;\n next = known(parent)\n ? thread\n : { ...thread, messages: [...thread.messages, { id: parent, role: 'assistant' }] };\n next = {\n ...next,\n messages: updateMessage(next, parent, m => ({\n ...m,\n toolCalls: [...(m.toolCalls ?? []), call]\n }))\n };\n }\n if (e.type === 'TOOL_CALL_START') return next;\n return toolArgs(next, id, e.delta);\n }\n case 'TOOL_CALL_ARGS':\n return toolArgs(thread, e.toolCallId, e.delta);\n case 'TOOL_CALL_RESULT':\n return message(e.role ?? 'tool', { toolCallId: e.toolCallId, content: e.content });\n case 'STATE_SNAPSHOT':\n return { ...thread, state: e.snapshot, stale: false };\n case 'STATE_DELTA':\n if (thread.stale) return thread;\n try {\n return { ...thread, state: applyPatch(thread.state, e.delta) };\n } catch {\n return { ...thread, stale: true };\n }\n case 'MESSAGES_SNAPSHOT':\n return { ...thread, messages: e.messages };\n case 'ACTIVITY_SNAPSHOT': {\n if (known(e.messageId) && e.replace === false) return thread;\n const next = known(e.messageId) ? thread : message('activity');\n return {\n ...next,\n messages: updateMessage(next, e.messageId, m => ({\n ...m,\n activityType: e.activityType,\n content: e.content\n }))\n };\n }\n case 'ACTIVITY_DELTA':\n try {\n return {\n ...thread,\n messages: updateMessage(thread, e.messageId, m => ({\n ...m,\n content: applyPatch(m.content, e.patch)\n }))\n };\n } catch {\n return thread;\n }\n default:\n return thread;\n }\n}\n\nfunction toolArgs<S>(thread: AgUiThread<S>, id: string | undefined, delta: string | undefined) {\n return {\n ...thread,\n messages: updateToolCall(thread, id, call => ({\n ...call,\n function: { ...call.function, arguments: call.function.arguments + (delta ?? '') }\n }))\n };\n}\n\n/** What the server sends for each event of a run: its run, its offset (from 1), and the event. */\nexport interface AgUiEnvelope {\n readonly runId: string;\n readonly offset: number;\n readonly event: AgUiEvent;\n}\n\n/** Options for {@link createAgUiAgent}. The message formats have defaults; see the guide. */\nexport interface AgUiAgentOptions<S, In, Out> {\n /** The thread's id, sent in every `RunAgentInput`. A new random one by default. */\n threadId?: string;\n /** The shared state before the first `STATE_SNAPSHOT`, sent with the first run. */\n state?: S;\n /** Names where streaming runs are remembered across reloads. @default 'ag-ui' */\n name?: string;\n /** @default `{ type: 'run', runId, input }` */\n run?: (input: RunAgentInput, runId: string) => Out;\n /** @default `{ type: 'resume', runId, after }` */\n resume?: (runId: string, after: number | undefined) => Out;\n /** @default `{ type: 'cancel', runId }` */\n cancel?: (runId: string) => Out;\n /**\n * The message that asks the server for a `STATE_SNAPSHOT`, sent when a `STATE_DELTA` can't\n * be applied. Without it, the state stays `stale` until the server sends one by itself.\n */\n resync?: (runId: string) => Out;\n /** Recognize a run's event. @default messages shaped like {@link AgUiEnvelope} */\n event?: (message: In) => AgUiEnvelope | null | undefined;\n /** What happens to a run when its connection drops. @default 'keep' */\n policy?: RunPolicy;\n /** Where streaming runs are remembered. @default sessionStorage */\n storage?: RunStorage | null;\n}\n\n/** An AG-UI agent over a WebSocket. Create with {@link createAgUiAgent}. */\nexport interface AgUiAgent<S> {\n /** The client the runs stream on. */\n readonly client: WebSocketClient<any, any>;\n /** The runs underneath, from `createWebSocketRuns`. */\n readonly runs: WebSocketRuns<RunAgentInput, AgUiThread<S>>;\n /**\n * Start a run with the thread so far plus `input.messages`, which show up in the thread right\n * away. A run that's still streaming is aborted first. Returns the run's id.\n */\n run(\n input?: {\n messages?: readonly AgUiMessage[];\n tools?: readonly unknown[];\n context?: readonly unknown[];\n forwardedProps?: unknown;\n },\n options?: { policy?: RunPolicy }\n ): string;\n /** Stop the run that's streaming, if any. */\n abort(): void;\n /**\n * Forget the thread, e.g. on logout or when another user signs in: its runs (with\n * `runs.clear()`, which sends nothing), messages and state. The next run starts over from\n * `state`, with a new thread id unless `threadId` was given. `abort()` a streaming run first\n * to stop it on the server.\n */\n reset(): void;\n /** The thread as of the latest run. The same object until it changes. */\n getSnapshot(): AgUiThread<S>;\n /** Get notified when the thread changes. Compatible with `useSyncExternalStore`. */\n subscribe(listener: () => void): () => void;\n /** Stop following runs, and release the connection. */\n dispose(): void;\n}\n\nfunction randomId() {\n return (\n globalThis.crypto?.randomUUID?.() ??\n `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`\n );\n}\n\nconst isEnvelope = (message: any): AgUiEnvelope | undefined =>\n typeof message?.runId === 'string' &&\n typeof message.offset === 'number' &&\n typeof message.event?.type === 'string'\n ? message\n : undefined;\n\nconst ENDS = new Set(['RUN_FINISHED', 'RUN_ERROR']);\n\n/**\n * Run an [AG-UI](https://docs.ag-ui.com) agent over a WebSocket. Each run streams AG-UI events,\n * which are folded into the thread's messages and shared state with {@link applyAgUiEvent}.\n * Runs come from `createWebSocketRuns`, so they resume after a reconnect or a reload, and a\n * state delta that can't be applied asks for a snapshot (`resync`).\n *\n * AG-UI doesn't define messages for WebSockets, so the defaults are small: the client sends\n * `{ type: 'run', runId, input }`, `{ type: 'resume', runId, after }` and\n * `{ type: 'cancel', runId }`, and the server sends `{ runId, offset, event }`, with `offset`\n * counting each run's events from 1. Each option replaces one of them.\n *\n * @example\n * ```ts\n * import { createAgUiAgent } from 'react-websocket/ag-ui';\n *\n * export const agent = createAgUiAgent(createWebSocket(url, { json: true }));\n *\n * agent.run({ messages: [{ id: crypto.randomUUID(), role: 'user', content: 'Plan my week' }] });\n * ```\n */\nexport function createAgUiAgent<S = unknown, In = any, Out = any>(\n client: WebSocketClient<In, Out>,\n options: AgUiAgentOptions<S, NoInfer<In>, NoInfer<Out>> = {}\n): AgUiAgent<S> {\n let threadId = options.threadId ?? randomId();\n const empty: AgUiThread<S> = {\n messages: [],\n state: options.state as S,\n status: 'idle',\n stale: false\n };\n let base = empty;\n let snapshot = empty;\n const listeners = new Set<() => void>();\n // Runs that asked for a snapshot after a failed delta, until one arrives.\n const resynced = new Set<string>();\n const event = options.event ?? isEnvelope;\n // Now or never: not queued, and not `volatile`, which can be a datagram that never arrives.\n const send = (message: unknown) => client.socket?.readyState === 1 && client.send(message as Out);\n\n const runs = createWebSocketRuns<RunAgentInput, AgUiThread<S>, In, Out>(client, {\n name: options.name ?? 'ag-ui',\n start: options.run ?? ((input, runId) => ({ type: 'run', runId, input }) as Out),\n resume: options.resume ?? ((runId, after) => ({ type: 'resume', runId, after }) as Out),\n cancel: options.cancel ?? (runId => ({ type: 'cancel', runId }) as Out),\n event: message => event(message),\n done: message => ENDS.has(event(message)!.event.type),\n initial: empty,\n reduce: (thread, message) => applyAgUiEvent(thread, event(message)!.event),\n policy: options.policy,\n storage: options.storage\n });\n\n function update() {\n const latest = runs.list().at(-1);\n let next = latest ? latest.value : base;\n if (latest?.status === 'aborted' && next.status === 'running')\n next = { ...next, status: 'aborted' };\n // Once each time the state goes stale, or on a later event if the socket wasn't open.\n if (latest && !latest.value.stale) resynced.delete(latest.runId);\n else if (latest && options.resync && !resynced.has(latest.runId)) {\n if (send(options.resync(latest.runId))) resynced.add(latest.runId);\n }\n if (next === snapshot) return;\n snapshot = next;\n for (const listener of [...listeners]) listener();\n }\n const off = runs.subscribe(update);\n update();\n\n function abort() {\n for (const run of runs.list()) runs.abort(run.runId);\n }\n\n return {\n client,\n runs,\n run(input = {}, runOptions) {\n abort();\n const runId = randomId();\n // The run starts from the state sent with it, the last good one: its deltas apply to it.\n base = {\n ...snapshot,\n stale: false,\n messages: [...snapshot.messages, ...(input.messages ?? [])]\n };\n runs.start(\n {\n threadId,\n runId,\n state: base.state,\n messages: base.messages,\n tools: input.tools ?? [],\n context: input.context ?? [],\n forwardedProps: input.forwardedProps ?? {}\n },\n { runId, initial: { ...base, status: 'running' }, policy: runOptions?.policy }\n );\n return runId;\n },\n abort,\n reset() {\n // Before the runs go, whose change shows `base`.\n base = empty;\n resynced.clear();\n if (options.threadId === undefined) threadId = randomId();\n runs.clear();\n update();\n },\n getSnapshot: () => snapshot,\n subscribe(listener) {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n dispose() {\n off();\n runs.dispose();\n }\n };\n}\n"],"mappings":";;AAmFA,SAAS,SAAS,OAAe;CAC/B,OAAO,MAAM,QAAQ,OAAO,GAAG,CAAC,CAAC,QAAQ,OAAO,GAAG;AACrD;AAEA,SAAS,MAAM,GAAS,GAAkB;CACxC,IAAI,MAAM,GAAG,OAAO;CACpB,IAAI,OAAO,MAAM,YAAY,OAAO,MAAM,YAAY,CAAC,KAAK,CAAC,GAAG,OAAO;CACvE,IAAI,MAAM,QAAQ,CAAC,MAAM,MAAM,QAAQ,CAAC,GAAG,OAAO;CAClD,MAAM,OAAO,OAAO,KAAK,CAAC;CAC1B,OAAO,KAAK,WAA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;EACA,IAAI,QAAQ,CAAC,GAAG,YAAY;GAC1B,MAAM;GACN,MAAM,QAAQ,SAAS;GAEvB,OAAO;IACL,GAAG;IACH,OAAO;IACP,UAAU,CAAC,GAAG,SAAS,UAAU,GAAI,MAAM,YAAY,CAAC,CAAE;GAC5D;GACA,KAAK,MACH;IACE;IACA;IACA,OAAO,KAAK;IACZ,UAAU,KAAK;IACf,OAAO,MAAM,SAAS,CAAC;IACvB,SAAS,MAAM,WAAW,CAAC;IAC3B,gBAAgB,MAAM,kBAAkB,CAAC;GAC3C,GACA;IAAE;IAAO,SAAS;KAAE,GAAG;KAAM,QAAQ;IAAU;IAAG,QAAQ,YAAY;GAAO,CAC/E;GACA,OAAO;EACT;EACA;EACA,QAAQ;GAEN,OAAO;GACP,SAAS,MAAM;GACf,IAAI,QAAQ,aAAa,KAAA,GAAW,WAAW,SAAS;GACxD,KAAK,MAAM;GACX,OAAO;EACT;EACA,mBAAmB;EACnB,UAAU,UAAU;GAClB,UAAU,IAAI,QAAQ;GACtB,aAAa;IACX,UAAU,OAAO,QAAQ;GAC3B;EACF;EACA,UAAU;GACR,IAAI;GACJ,KAAK,QAAQ;EACf;CACF;AACF"}
package/dist/core.cjs CHANGED
@@ -575,7 +575,8 @@ function createClient(url, options = {}) {
575
575
  unheldStop = refs === 0;
576
576
  set({
577
577
  status: "closed",
578
- attempt: 0
578
+ attempt: 0,
579
+ lastMessage: void 0
579
580
  });
580
581
  rejectReady(/* @__PURE__ */ new Error("WebSocket was closed"));
581
582
  }
@@ -850,6 +851,15 @@ function createWebSocketFeeds(client, options) {
850
851
  };
851
852
  },
852
853
  position: (topic) => last.get(topic),
854
+ reset(topic) {
855
+ if (topic === void 0) {
856
+ last.clear();
857
+ resuming.clear();
858
+ } else {
859
+ last.delete(topic);
860
+ resuming.delete(topic);
861
+ }
862
+ },
853
863
  dispose() {
854
864
  topics.dispose();
855
865
  for (const off of offs) off();
@@ -1438,6 +1448,7 @@ function createWebSocketRuns(client, options) {
1438
1448
  const starting = /* @__PURE__ */ new Map();
1439
1449
  const policies = /* @__PURE__ */ new Map();
1440
1450
  const releases = /* @__PURE__ */ new Map();
1451
+ const cancels = /* @__PURE__ */ new Set();
1441
1452
  const listeners = /* @__PURE__ */ new Set();
1442
1453
  let list = [];
1443
1454
  const feeds = createWebSocketFeeds(client, {
@@ -1501,11 +1512,18 @@ function createWebSocketRuns(client, options) {
1501
1512
  if (!message) return;
1502
1513
  if (client.socket?.readyState === 1) return void client.send(message(runId));
1503
1514
  const release = client.acquire();
1515
+ const drop = () => {
1516
+ cancels.delete(drop);
1517
+ off();
1518
+ release();
1519
+ };
1504
1520
  const off = client.on("open", () => {
1521
+ cancels.delete(drop);
1505
1522
  off();
1506
1523
  client.send(message(runId));
1507
1524
  release();
1508
1525
  });
1526
+ cancels.add(drop);
1509
1527
  }
1510
1528
  function abort(runId) {
1511
1529
  if (runs.get(runId)?.status !== "streaming") return;
@@ -1594,6 +1612,21 @@ function createWebSocketRuns(client, options) {
1594
1612
  for (const listener of [...listeners]) listener();
1595
1613
  }
1596
1614
  },
1615
+ clear() {
1616
+ for (const release of releases.values()) release();
1617
+ releases.clear();
1618
+ for (const drop of [...cancels]) drop();
1619
+ stale.length = 0;
1620
+ starting.clear();
1621
+ policies.clear();
1622
+ try {
1623
+ storage?.removeItem(key);
1624
+ } catch {}
1625
+ if (!runs.size) return;
1626
+ runs.clear();
1627
+ list = [];
1628
+ for (const listener of [...listeners]) listener();
1629
+ },
1597
1630
  dispose() {
1598
1631
  for (const release of releases.values()) release();
1599
1632
  releases.clear();