@happyvertical/smrt-chat 0.61.5 → 0.62.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/AGENTS.md +4 -0
- package/agents/assistant-turn-runs.md +74 -0
- package/agents/tool-summaries.md +1 -1
- package/agents/turn-drivers.md +64 -46
- package/dist/assistant-approvals.js +1 -1
- package/dist/assistant-approvals.js.map +1 -1
- package/dist/assistant-turn-driver.d.ts +11 -8
- package/dist/assistant-turn-driver.d.ts.map +1 -1
- package/dist/assistant-turn-events.d.ts +1 -1
- package/dist/assistant-turn-events.d.ts.map +1 -1
- package/dist/assistant-turn.d.ts +34 -3
- package/dist/assistant-turn.d.ts.map +1 -1
- package/dist/chunks/{ChatService-CeOVzOqN.js → ChatService-CDLAEJms.js} +2 -2
- package/dist/chunks/{ChatService-CeOVzOqN.js.map → ChatService-CDLAEJms.js.map} +1 -1
- package/dist/chunks/assistant-turn-events-DwdCZzQ0.js.map +1 -1
- package/dist/chunks/{chat-stream-5NpFBPe2.js → chat-stream-BJbmqHsu.js} +742 -254
- package/dist/chunks/chat-stream-BJbmqHsu.js.map +1 -0
- package/dist/engines.d.ts +4 -4
- package/dist/engines.d.ts.map +1 -1
- package/dist/engines.js +22 -24
- package/dist/engines.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/internal/agent-runtime.js +1 -1
- package/dist/manifest.json +2 -1
- package/dist/smrt-knowledge.json +28 -10
- package/dist/smrt-knowledge.okf.md +16 -11
- package/dist/svelte/components/assistant/AssistantDock.svelte +2 -2
- package/dist/svelte/components/assistant/assistant-http-client.d.ts +2 -2
- package/dist/svelte/components/assistant/assistant-http-client.d.ts.map +1 -1
- package/dist/svelte/components/assistant/assistant-http-client.js +5 -5
- package/dist/svelte/components/assistant/assistant-transport.d.ts +7 -6
- package/dist/svelte/components/assistant/assistant-transport.d.ts.map +1 -1
- package/dist/svelte/components/assistant/create-assistant-dock-controller.svelte.d.ts +3 -3
- package/dist/svelte/components/assistant/create-assistant-dock-controller.svelte.d.ts.map +1 -1
- package/dist/svelte/components/assistant/create-assistant-dock-controller.svelte.js +30 -30
- package/dist/svelte/i18n.d.ts +1 -1
- package/dist/svelte/i18n.js +1 -1
- package/dist/svelte/index.d.ts +1 -1
- package/dist/svelte/index.d.ts.map +1 -1
- package/dist/sveltekit.d.ts +55 -24
- package/dist/sveltekit.d.ts.map +1 -1
- package/dist/sveltekit.js +109 -44
- package/dist/sveltekit.js.map +1 -1
- package/dist/tool-loop.d.ts +2 -1
- package/dist/tool-loop.d.ts.map +1 -1
- package/dist/tool-run-types.d.ts +102 -0
- package/dist/tool-run-types.d.ts.map +1 -0
- package/dist/turn-tracking.d.ts +246 -0
- package/dist/turn-tracking.d.ts.map +1 -0
- package/package.json +20 -19
- package/dist/chunks/chat-stream-5NpFBPe2.js.map +0 -1
- package/dist/work-profile.d.ts +0 -64
- package/dist/work-profile.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assistant-turn-events-DwdCZzQ0.js","names":[],"sources":["../../src/tool-allow-list.ts","../../src/assistant-turn-events.ts"],"sourcesContent":["/**\n * Tool allow-list matching, shared by the server loop (which narrows the\n * browser tools a page declares) and browser hosts (which narrow what they\n * declare in the first place). No server dependencies: safe in the browser.\n */\n\n/** Whether `name` matches an allow-list entry (exact, or `prefix*`). */\nexport function matchesToolAllowList(\n name: string,\n allowList: readonly string[],\n): boolean {\n return allowList.some((pattern) =>\n pattern.endsWith('*')\n ? pattern.length > 1 && name.startsWith(pattern.slice(0, -1))\n : pattern === name,\n );\n}\n","/**\n * Assistant turn wire contract (#2908): the events one streamed assistant\n * turn emits, the small generic status a host shell can show while it runs,\n * and the browser-side SSE reader.\n *\n * BROWSER-SAFE and dependency-free, like `client.ts`: the server engine\n * (`assistant-turn.ts`) imports the types from here, and the AssistantDock\n * (`svelte/components/assistant/`) imports the reader. Exported under\n * `@happyvertical/smrt-chat/assistant-turn`.\n *\n * Response: text/event-stream, one `data: <json>` line per event:\n * { \"type\": \"status\", \"status\": AssistantStatus }\n * { \"type\": \"token\", \"text\": \"...\" } (live preview only)\n * { \"type\": \"step\", \"step\": AssistantTurnStep } (\"calling X\", \"done\")\n * { \"type\": \"message\", \"message\": {...} } (a persisted message)\n * { \"type\": \"client_tool_calls\", \"continuationId\": \"...\", \"calls\": [...] }\n * { \"type\": \"done\", \"message\": {...}?, \"stoppedReason\": \"...\" }\n * { \"type\": \"error\", \"error\": \"...\", \"code\": \"...\" }\n * plus `: heartbeat` comment lines, which readers ignore.\n *\n * A turn ends with exactly one of `done`, `error`, or `client_tool_calls`.\n * `client_tool_calls` suspends the turn: the browser runs the calls and\n * resumes it with their results under `continuationId`.\n */\n\n/** The coarse state a host shows (\"working strip\", status line). */\nexport type AssistantStatusState = 'idle' | 'working' | 'done' | 'error';\n\n/**\n * The generic assistant status: one plain-language line plus a state.\n * Emitted by the server engine as `status` events and derived by the dock\n * controller (`controller.status`, `onStatus`).\n */\nexport interface AssistantStatus {\n state: AssistantStatusState;\n /** Plain-language line (\"Looking up articles…\", \"Done\"), or null. */\n label: string | null;\n /** Changes the user applied during this piece of work (done state). */\n changes?: number;\n /** Whether the in-flight work can be stopped (`controller.cancel()`). */\n cancellable?: boolean;\n}\n\n/** Where a tool ran. */\nexport type AssistantToolLocation = 'server' | 'client';\n\n/** A browser tool's declared effect (mirrors smrt-web's `WebMcpToolEffect`). */\nexport type AssistantToolEffect = 'read' | 'write' | 'destructive';\n\n/** One progress step of a turn. */\nexport type AssistantTurnStep =\n | { kind: 'thinking'; step: number }\n | {\n kind: 'tool_call';\n callId: string;\n tool: string;\n label: string;\n location: AssistantToolLocation;\n }\n | {\n kind: 'tool_result';\n callId: string;\n tool: string;\n label: string;\n ok: boolean;\n error?: string;\n };\n\n/** A browser tool call the server suspended on. */\nexport interface AssistantClientToolCall {\n id: string;\n name: string;\n args: Record<string, unknown>;\n /** The effect the page declared; the dock re-reads it from its registry. */\n effect: AssistantToolEffect;\n}\n\n/** A browser tool declaration sent with a turn (untrusted on the server). */\nexport interface AssistantClientToolDeclaration {\n name: string;\n description: string;\n inputSchema: Record<string, unknown>;\n effect: AssistantToolEffect;\n}\n\n/** The browser's answer to one {@link AssistantClientToolCall}. */\nexport interface AssistantClientToolResult {\n id: string;\n ok: boolean;\n result?: string;\n /** `declined` when the user said no; otherwise a short reason. */\n error?: string;\n}\n\n/** Why a turn finished. */\nexport type AssistantTurnStopReason =\n | 'approval'\n | 'stop'\n | 'max_steps'\n /** The turn's token or time budget ran out (`maxTurnTokens`/`maxTurnMs`). */\n | 'budget'\n /**\n * One read-only call was repeated `maxRepeatedCalls` times; the model\n * answered without tools (#4391). Not continuable: repeating won't help.\n */\n | 'repeated_calls'\n | 'no_tools'\n | 'cancelled'\n /** The model returned no answer, even after one retry; the reply is the fallback (#4379). */\n | 'empty'\n /** A model call timed out; the reply is the fallback (#4379). */\n | 'timeout'\n /** A model call failed and the host's `providerErrorFallback` answered (#4454). */\n | 'provider_error';\n\n/** A server-owned approval reference; authority and storage belong to the host. */\nexport interface AssistantApprovalRequest {\n id: string;\n tool: string;\n callId: string;\n summary: string;\n /** Optional host-authored business preview, supplied by the authenticated presentation seam. */\n presentation?: { summary: string };\n}\n\n// Public projection only: never send the principal/arguments recovery envelope to the browser.\nexport interface AssistantApprovalView extends AssistantApprovalRequest {\n status: import('@happyvertical/smrt-app-runtime').OwnerApprovalStatus;\n decidedBy?: string | null;\n decidedAt?: number | null;\n}\n\n/**\n * Reconcile same-hold browser presentation when canonical reads overlap.\n * This grants no execution authority; server verification remains mandatory.\n * The first terminal view wins, and unrelated hold identities never merge.\n */\nexport function reconcileAssistantApprovalView(\n current: AssistantApprovalView | undefined,\n next: AssistantApprovalView,\n): AssistantApprovalView {\n if (!current) return next;\n if (current.id !== next.id) return current;\n const active = ['pending', 'approved', 'executing'];\n const previousRank = active.indexOf(current.status);\n const nextRank = active.indexOf(next.status);\n if (previousRank < 0 || (nextRank >= 0 && nextRank < previousRank))\n return current;\n return next;\n}\n\n/** One event of a streamed assistant turn. `M` is the host's message shape. */\nexport type AssistantTurnEvent<M = Record<string, unknown>> =\n | { type: 'status'; status: AssistantStatus }\n | { type: 'token'; text: string }\n | { type: 'approval_needed'; approval: AssistantApprovalRequest }\n | { type: 'step'; step: AssistantTurnStep }\n | { type: 'message'; message: M }\n | {\n type: 'client_tool_calls';\n continuationId: string;\n calls: AssistantClientToolCall[];\n }\n | {\n type: 'done';\n /** Durable design run accepted by the authenticated host. */\n runId?: string;\n message?: M;\n stoppedReason: AssistantTurnStopReason;\n /**\n * Why tools were withdrawn on the final round, kept when the turn then\n * ends `empty` or `timeout` (#4379). `repeated_calls` (#4391) is not\n * continuable; `max_steps` and `budget` are.\n */\n capReason?: 'max_steps' | 'budget' | 'repeated_calls';\n }\n | {\n type: 'error';\n /**\n * A message safe to show the user: either a deliberate user-facing\n * message or a generic one. Server detail is logged, never sent.\n */\n error: string;\n /** A stable machine code (`internal_error` for an unexpected failure). */\n code?: string;\n };\n\n/** Serialize one event as an SSE frame. */\nexport function encodeAssistantTurnEvent(\n event: AssistantTurnEvent<unknown>,\n): string {\n return `data: ${JSON.stringify(event)}\\n\\n`;\n}\n\n/** The outcome of reading one streamed turn. */\nexport interface AssistantTurnStreamOutcome<M = Record<string, unknown>> {\n /** Every persisted message the turn reported (tool results, the reply). */\n messages: M[];\n /** The terminal `done` event, when the turn finished. */\n done?: Extract<AssistantTurnEvent<M>, { type: 'done' }>;\n /** The suspension, when the turn is waiting on browser tools. */\n clientToolCalls?: Extract<\n AssistantTurnEvent<M>,\n { type: 'client_tool_calls' }\n >;\n}\n\n/** Thrown by {@link readAssistantTurnStream} for an in-band `error` or a cut stream. */\nexport class AssistantTurnStreamError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'AssistantTurnStreamError';\n }\n}\n\nfunction parseFrame<M>(data: string): AssistantTurnEvent<M> | null {\n try {\n const parsed = JSON.parse(data) as { type?: unknown };\n return parsed && typeof parsed.type === 'string'\n ? (parsed as AssistantTurnEvent<M>)\n : null;\n } catch {\n return null;\n }\n}\n\n/**\n * Read a streamed assistant turn from a `text/event-stream` response,\n * reporting each event to `onEvent` as it arrives.\n *\n * Resolves once a terminal event (`done` or `client_tool_calls`) arrives;\n * rejects with {@link AssistantTurnStreamError} on an in-band `error` or when\n * the stream closes without a terminal event (an intermediary cut it).\n * Tolerates heartbeat comments and frames split across chunks.\n */\nexport async function readAssistantTurnStream<M = Record<string, unknown>>(\n response: Response,\n onEvent?: (event: AssistantTurnEvent<M>) => void,\n): Promise<AssistantTurnStreamOutcome<M>> {\n const body = response.body;\n if (!body) {\n throw new AssistantTurnStreamError('The assistant sent no response.');\n }\n const reader = body.getReader();\n const decoder = new TextDecoder();\n const outcome: AssistantTurnStreamOutcome<M> = { messages: [] };\n let buffer = '';\n\n const handle = (event: AssistantTurnEvent<M>): boolean => {\n try {\n onEvent?.(event);\n } catch {\n // A host callback never breaks the stream.\n }\n switch (event.type) {\n case 'message':\n outcome.messages.push(event.message);\n return false;\n case 'done':\n outcome.done = event;\n return true;\n case 'client_tool_calls':\n outcome.clientToolCalls = event;\n return true;\n case 'error':\n throw new AssistantTurnStreamError(\n event.error || 'The assistant failed.',\n );\n default:\n return false;\n }\n };\n\n try {\n for (;;) {\n const { value, done } = await reader.read();\n if (value) buffer += decoder.decode(value, { stream: true });\n if (done) buffer += decoder.decode();\n let boundary = buffer.indexOf('\\n\\n');\n while (boundary >= 0) {\n const block = buffer.slice(0, boundary);\n buffer = buffer.slice(boundary + 2);\n const data = block\n .split('\\n')\n .filter((line) => line.startsWith('data:'))\n .map((line) => line.slice(5).trimStart())\n .join('\\n');\n if (data) {\n const event = parseFrame<M>(data);\n if (event && handle(event)) return outcome;\n }\n boundary = buffer.indexOf('\\n\\n');\n }\n if (done) break;\n }\n } finally {\n // Release the connection promptly once the turn has settled.\n void reader.cancel().catch(() => undefined);\n }\n throw new AssistantTurnStreamError(\n 'The assistant stopped responding before finishing.',\n );\n}\n\n/** Tool allow-list matching (browser-safe), re-exported for hosts. */\nexport { matchesToolAllowList } from './tool-allow-list.js';\n"],"mappings":";AAOO,SAAS,qBACd,MACA,WACS;CACT,OAAO,UAAU,MAAM,YACrB,QAAQ,SAAS,GAAG,IAChB,QAAQ,SAAS,KAAK,KAAK,WAAW,QAAQ,MAAM,GAAG,EAAE,CAAC,IAC1D,YAAY,IAClB;AACF;;;ACyHO,SAAS,+BACd,SACA,MACuB;CACvB,IAAI,CAAC,SAAS,OAAO;CACrB,IAAI,QAAQ,OAAO,KAAK,IAAI,OAAO;CACnC,MAAM,SAAS;EAAC;EAAW;EAAY;CAAW;CAClD,MAAM,eAAe,OAAO,QAAQ,QAAQ,MAAM;CAClD,MAAM,WAAW,OAAO,QAAQ,KAAK,MAAM;CAC3C,IAAI,eAAe,KAAM,YAAY,KAAK,WAAW,cACnD,OAAO;CACT,OAAO;AACT;AAuCO,SAAS,yBACd,OACQ;CACR,OAAO,SAAS,KAAK,UAAU,KAAK,EAAC;;;AACvC;AAgBO,IAAM,2BAAN,cAAuC,MAAM;CAClD,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF;AAEA,SAAS,WAAc,MAA4C;CACjE,IAAI;EACF,MAAM,SAAS,KAAK,MAAM,IAAI;EAC9B,OAAO,UAAU,OAAO,OAAO,SAAS,WACnC,SACD;CACN,QAAQ;EACN,OAAO;CACT;AACF;AAWA,eAAsB,wBACpB,UACA,SACwC;CACxC,MAAM,OAAO,SAAS;CACtB,IAAI,CAAC,MACH,MAAM,IAAI,yBAAyB,iCAAiC;CAEtE,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,UAAU,IAAI,YAAY;CAChC,MAAM,UAAyC,EAAE,UAAU,CAAC,EAAE;CAC9D,IAAI,SAAS;CAEb,MAAM,UAAU,UAA0C;EACxD,IAAI;GACF,UAAU,KAAK;EACjB,QAAQ,CAER;EACA,QAAQ,MAAM,MAAd;GACE,KAAK;IACH,QAAQ,SAAS,KAAK,MAAM,OAAO;IACnC,OAAO;GACT,KAAK;IACH,QAAQ,OAAO;IACf,OAAO;GACT,KAAK;IACH,QAAQ,kBAAkB;IAC1B,OAAO;GACT,KAAK,SACH,MAAM,IAAI,yBACR,MAAM,SAAS,uBACjB;GACF,SACE,OAAO;EACX;CACF;CAEA,IAAI;EACF,SAAS;GACP,MAAM,EAAE,OAAO,SAAS,MAAM,OAAO,KAAK;GAC1C,IAAI,OAAO,UAAU,QAAQ,OAAO,OAAO,EAAE,QAAQ,KAAK,CAAC;GAC3D,IAAI,MAAM,UAAU,QAAQ,OAAO;GACnC,IAAI,WAAW,OAAO,QAAQ,MAAM;GACpC,OAAO,YAAY,GAAG;IACpB,MAAM,QAAQ,OAAO,MAAM,GAAG,QAAQ;IACtC,SAAS,OAAO,MAAM,WAAW,CAAC;IAClC,MAAM,OAAO,MACV,MAAM,IAAI,CAAA,CACV,QAAQ,SAAS,KAAK,WAAW,OAAO,CAAC,CAAA,CACzC,KAAK,SAAS,KAAK,MAAM,CAAC,CAAA,CAAE,UAAU,CAAC,CAAA,CACvC,KAAK,IAAI;IACZ,IAAI,MAAM;KACR,MAAM,QAAQ,WAAc,IAAI;KAChC,IAAI,SAAS,OAAO,KAAK,GAAG,OAAO;IACrC;IACA,WAAW,OAAO,QAAQ,MAAM;GAClC;GACA,IAAI,MAAM;EACZ;CACF,UAAE;EAEA,OAAY,OAAO,CAAA,CAAE,YAAY,KAAA,CAAS;CAC5C;CACA,MAAM,IAAI,yBACR,oDACF;AACF"}
|
|
1
|
+
{"version":3,"file":"assistant-turn-events-DwdCZzQ0.js","names":[],"sources":["../../src/tool-allow-list.ts","../../src/assistant-turn-events.ts"],"sourcesContent":["/**\n * Tool allow-list matching, shared by the server loop (which narrows the\n * browser tools a page declares) and browser hosts (which narrow what they\n * declare in the first place). No server dependencies: safe in the browser.\n */\n\n/** Whether `name` matches an allow-list entry (exact, or `prefix*`). */\nexport function matchesToolAllowList(\n name: string,\n allowList: readonly string[],\n): boolean {\n return allowList.some((pattern) =>\n pattern.endsWith('*')\n ? pattern.length > 1 && name.startsWith(pattern.slice(0, -1))\n : pattern === name,\n );\n}\n","/**\n * Assistant turn wire contract (#2908): the events one streamed assistant\n * turn emits, the small generic status a host shell can show while it runs,\n * and the browser-side SSE reader.\n *\n * BROWSER-SAFE and dependency-free, like `client.ts`: the server engine\n * (`assistant-turn.ts`) imports the types from here, and the AssistantDock\n * (`svelte/components/assistant/`) imports the reader. Exported under\n * `@happyvertical/smrt-chat/assistant-turn`.\n *\n * Response: text/event-stream, one `data: <json>` line per event:\n * { \"type\": \"status\", \"status\": AssistantStatus }\n * { \"type\": \"token\", \"text\": \"...\" } (live preview only)\n * { \"type\": \"step\", \"step\": AssistantTurnStep } (\"calling X\", \"done\")\n * { \"type\": \"message\", \"message\": {...} } (a persisted message)\n * { \"type\": \"client_tool_calls\", \"continuationId\": \"...\", \"calls\": [...] }\n * { \"type\": \"done\", \"message\": {...}?, \"stoppedReason\": \"...\" }\n * { \"type\": \"error\", \"error\": \"...\", \"code\": \"...\" }\n * plus `: heartbeat` comment lines, which readers ignore.\n *\n * A turn ends with exactly one of `done`, `error`, or `client_tool_calls`.\n * `client_tool_calls` suspends the turn: the browser runs the calls and\n * resumes it with their results under `continuationId`.\n */\n\n/** The coarse state a host shows (\"working strip\", status line). */\nexport type AssistantStatusState = 'idle' | 'working' | 'done' | 'error';\n\n/**\n * The generic assistant status: one plain-language line plus a state.\n * Emitted by the server engine as `status` events and derived by the dock\n * controller (`controller.status`, `onStatus`).\n */\nexport interface AssistantStatus {\n state: AssistantStatusState;\n /** Plain-language line (\"Looking up articles…\", \"Done\"), or null. */\n label: string | null;\n /** Changes the user applied during this piece of work (done state). */\n changes?: number;\n /** Whether the in-flight work can be stopped (`controller.cancel()`). */\n cancellable?: boolean;\n}\n\n/** Where a tool ran. */\nexport type AssistantToolLocation = 'server' | 'client';\n\n/** A browser tool's declared effect (mirrors smrt-web's `WebMcpToolEffect`). */\nexport type AssistantToolEffect = 'read' | 'write' | 'destructive';\n\n/** One progress step of a turn. */\nexport type AssistantTurnStep =\n | { kind: 'thinking'; step: number }\n | {\n kind: 'tool_call';\n callId: string;\n tool: string;\n label: string;\n location: AssistantToolLocation;\n }\n | {\n kind: 'tool_result';\n callId: string;\n tool: string;\n label: string;\n ok: boolean;\n error?: string;\n };\n\n/** A browser tool call the server suspended on. */\nexport interface AssistantClientToolCall {\n id: string;\n name: string;\n args: Record<string, unknown>;\n /** The effect the page declared; the dock re-reads it from its registry. */\n effect: AssistantToolEffect;\n}\n\n/** A browser tool declaration sent with a turn (untrusted on the server). */\nexport interface AssistantClientToolDeclaration {\n name: string;\n description: string;\n inputSchema: Record<string, unknown>;\n effect: AssistantToolEffect;\n}\n\n/** The browser's answer to one {@link AssistantClientToolCall}. */\nexport interface AssistantClientToolResult {\n id: string;\n ok: boolean;\n result?: string;\n /** `declined` when the user said no; otherwise a short reason. */\n error?: string;\n}\n\n/** Why a turn finished. */\nexport type AssistantTurnStopReason =\n | 'approval'\n | 'stop'\n | 'max_steps'\n /** The turn's token or time budget ran out (`maxTurnTokens`/`maxTurnMs`). */\n | 'budget'\n /**\n * One read-only call was repeated `maxRepeatedCalls` times; the model\n * answered without tools (#4391). Not continuable: repeating won't help.\n */\n | 'repeated_calls'\n | 'no_tools'\n | 'cancelled'\n /** The model returned no answer, even after one retry; the reply is the fallback (#4379). */\n | 'empty'\n /** A model call timed out; the reply is the fallback (#4379). */\n | 'timeout'\n /** A model call failed and the host's `providerErrorFallback` answered (#4454). */\n | 'provider_error';\n\n/** A server-owned approval reference; authority and storage belong to the host. */\nexport interface AssistantApprovalRequest {\n id: string;\n tool: string;\n callId: string;\n summary: string;\n /** Optional host-authored business preview, supplied by the authenticated presentation seam. */\n presentation?: { summary: string };\n}\n\n// Public projection only: never send the principal/arguments recovery envelope to the browser.\nexport interface AssistantApprovalView extends AssistantApprovalRequest {\n status: import('@happyvertical/smrt-app-runtime').OwnerApprovalStatus;\n decidedBy?: string | null;\n decidedAt?: number | null;\n}\n\n/**\n * Reconcile same-hold browser presentation when canonical reads overlap.\n * This grants no execution authority; server verification remains mandatory.\n * The first terminal view wins, and unrelated hold identities never merge.\n */\nexport function reconcileAssistantApprovalView(\n current: AssistantApprovalView | undefined,\n next: AssistantApprovalView,\n): AssistantApprovalView {\n if (!current) return next;\n if (current.id !== next.id) return current;\n const active = ['pending', 'approved', 'executing'];\n const previousRank = active.indexOf(current.status);\n const nextRank = active.indexOf(next.status);\n if (previousRank < 0 || (nextRank >= 0 && nextRank < previousRank))\n return current;\n return next;\n}\n\n/** One event of a streamed assistant turn. `M` is the host's message shape. */\nexport type AssistantTurnEvent<M = Record<string, unknown>> =\n | { type: 'status'; status: AssistantStatus }\n | { type: 'token'; text: string }\n | { type: 'approval_needed'; approval: AssistantApprovalRequest }\n | { type: 'step'; step: AssistantTurnStep }\n | { type: 'message'; message: M }\n | {\n type: 'client_tool_calls';\n continuationId: string;\n calls: AssistantClientToolCall[];\n }\n | {\n type: 'done';\n /** Durable workflow run accepted by the authenticated host. */\n runId?: string;\n message?: M;\n stoppedReason: AssistantTurnStopReason;\n /**\n * Why tools were withdrawn on the final round, kept when the turn then\n * ends `empty` or `timeout` (#4379). `repeated_calls` (#4391) is not\n * continuable; `max_steps` and `budget` are.\n */\n capReason?: 'max_steps' | 'budget' | 'repeated_calls';\n }\n | {\n type: 'error';\n /**\n * A message safe to show the user: either a deliberate user-facing\n * message or a generic one. Server detail is logged, never sent.\n */\n error: string;\n /** A stable machine code (`internal_error` for an unexpected failure). */\n code?: string;\n };\n\n/** Serialize one event as an SSE frame. */\nexport function encodeAssistantTurnEvent(\n event: AssistantTurnEvent<unknown>,\n): string {\n return `data: ${JSON.stringify(event)}\\n\\n`;\n}\n\n/** The outcome of reading one streamed turn. */\nexport interface AssistantTurnStreamOutcome<M = Record<string, unknown>> {\n /** Every persisted message the turn reported (tool results, the reply). */\n messages: M[];\n /** The terminal `done` event, when the turn finished. */\n done?: Extract<AssistantTurnEvent<M>, { type: 'done' }>;\n /** The suspension, when the turn is waiting on browser tools. */\n clientToolCalls?: Extract<\n AssistantTurnEvent<M>,\n { type: 'client_tool_calls' }\n >;\n}\n\n/** Thrown by {@link readAssistantTurnStream} for an in-band `error` or a cut stream. */\nexport class AssistantTurnStreamError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'AssistantTurnStreamError';\n }\n}\n\nfunction parseFrame<M>(data: string): AssistantTurnEvent<M> | null {\n try {\n const parsed = JSON.parse(data) as { type?: unknown };\n return parsed && typeof parsed.type === 'string'\n ? (parsed as AssistantTurnEvent<M>)\n : null;\n } catch {\n return null;\n }\n}\n\n/**\n * Read a streamed assistant turn from a `text/event-stream` response,\n * reporting each event to `onEvent` as it arrives.\n *\n * Resolves once a terminal event (`done` or `client_tool_calls`) arrives;\n * rejects with {@link AssistantTurnStreamError} on an in-band `error` or when\n * the stream closes without a terminal event (an intermediary cut it).\n * Tolerates heartbeat comments and frames split across chunks.\n */\nexport async function readAssistantTurnStream<M = Record<string, unknown>>(\n response: Response,\n onEvent?: (event: AssistantTurnEvent<M>) => void,\n): Promise<AssistantTurnStreamOutcome<M>> {\n const body = response.body;\n if (!body) {\n throw new AssistantTurnStreamError('The assistant sent no response.');\n }\n const reader = body.getReader();\n const decoder = new TextDecoder();\n const outcome: AssistantTurnStreamOutcome<M> = { messages: [] };\n let buffer = '';\n\n const handle = (event: AssistantTurnEvent<M>): boolean => {\n try {\n onEvent?.(event);\n } catch {\n // A host callback never breaks the stream.\n }\n switch (event.type) {\n case 'message':\n outcome.messages.push(event.message);\n return false;\n case 'done':\n outcome.done = event;\n return true;\n case 'client_tool_calls':\n outcome.clientToolCalls = event;\n return true;\n case 'error':\n throw new AssistantTurnStreamError(\n event.error || 'The assistant failed.',\n );\n default:\n return false;\n }\n };\n\n try {\n for (;;) {\n const { value, done } = await reader.read();\n if (value) buffer += decoder.decode(value, { stream: true });\n if (done) buffer += decoder.decode();\n let boundary = buffer.indexOf('\\n\\n');\n while (boundary >= 0) {\n const block = buffer.slice(0, boundary);\n buffer = buffer.slice(boundary + 2);\n const data = block\n .split('\\n')\n .filter((line) => line.startsWith('data:'))\n .map((line) => line.slice(5).trimStart())\n .join('\\n');\n if (data) {\n const event = parseFrame<M>(data);\n if (event && handle(event)) return outcome;\n }\n boundary = buffer.indexOf('\\n\\n');\n }\n if (done) break;\n }\n } finally {\n // Release the connection promptly once the turn has settled.\n void reader.cancel().catch(() => undefined);\n }\n throw new AssistantTurnStreamError(\n 'The assistant stopped responding before finishing.',\n );\n}\n\n/** Tool allow-list matching (browser-safe), re-exported for hosts. */\nexport { matchesToolAllowList } from './tool-allow-list.js';\n"],"mappings":";AAOO,SAAS,qBACd,MACA,WACS;CACT,OAAO,UAAU,MAAM,YACrB,QAAQ,SAAS,GAAG,IAChB,QAAQ,SAAS,KAAK,KAAK,WAAW,QAAQ,MAAM,GAAG,EAAE,CAAC,IAC1D,YAAY,IAClB;AACF;;;ACyHO,SAAS,+BACd,SACA,MACuB;CACvB,IAAI,CAAC,SAAS,OAAO;CACrB,IAAI,QAAQ,OAAO,KAAK,IAAI,OAAO;CACnC,MAAM,SAAS;EAAC;EAAW;EAAY;CAAW;CAClD,MAAM,eAAe,OAAO,QAAQ,QAAQ,MAAM;CAClD,MAAM,WAAW,OAAO,QAAQ,KAAK,MAAM;CAC3C,IAAI,eAAe,KAAM,YAAY,KAAK,WAAW,cACnD,OAAO;CACT,OAAO;AACT;AAuCO,SAAS,yBACd,OACQ;CACR,OAAO,SAAS,KAAK,UAAU,KAAK,EAAC;;;AACvC;AAgBO,IAAM,2BAAN,cAAuC,MAAM;CAClD,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,KAAK,OAAO;CACd;AACF;AAEA,SAAS,WAAc,MAA4C;CACjE,IAAI;EACF,MAAM,SAAS,KAAK,MAAM,IAAI;EAC9B,OAAO,UAAU,OAAO,OAAO,SAAS,WACnC,SACD;CACN,QAAQ;EACN,OAAO;CACT;AACF;AAWA,eAAsB,wBACpB,UACA,SACwC;CACxC,MAAM,OAAO,SAAS;CACtB,IAAI,CAAC,MACH,MAAM,IAAI,yBAAyB,iCAAiC;CAEtE,MAAM,SAAS,KAAK,UAAU;CAC9B,MAAM,UAAU,IAAI,YAAY;CAChC,MAAM,UAAyC,EAAE,UAAU,CAAC,EAAE;CAC9D,IAAI,SAAS;CAEb,MAAM,UAAU,UAA0C;EACxD,IAAI;GACF,UAAU,KAAK;EACjB,QAAQ,CAER;EACA,QAAQ,MAAM,MAAd;GACE,KAAK;IACH,QAAQ,SAAS,KAAK,MAAM,OAAO;IACnC,OAAO;GACT,KAAK;IACH,QAAQ,OAAO;IACf,OAAO;GACT,KAAK;IACH,QAAQ,kBAAkB;IAC1B,OAAO;GACT,KAAK,SACH,MAAM,IAAI,yBACR,MAAM,SAAS,uBACjB;GACF,SACE,OAAO;EACX;CACF;CAEA,IAAI;EACF,SAAS;GACP,MAAM,EAAE,OAAO,SAAS,MAAM,OAAO,KAAK;GAC1C,IAAI,OAAO,UAAU,QAAQ,OAAO,OAAO,EAAE,QAAQ,KAAK,CAAC;GAC3D,IAAI,MAAM,UAAU,QAAQ,OAAO;GACnC,IAAI,WAAW,OAAO,QAAQ,MAAM;GACpC,OAAO,YAAY,GAAG;IACpB,MAAM,QAAQ,OAAO,MAAM,GAAG,QAAQ;IACtC,SAAS,OAAO,MAAM,WAAW,CAAC;IAClC,MAAM,OAAO,MACV,MAAM,IAAI,CAAA,CACV,QAAQ,SAAS,KAAK,WAAW,OAAO,CAAC,CAAA,CACzC,KAAK,SAAS,KAAK,MAAM,CAAC,CAAA,CAAE,UAAU,CAAC,CAAA,CACvC,KAAK,IAAI;IACZ,IAAI,MAAM;KACR,MAAM,QAAQ,WAAc,IAAI;KAChC,IAAI,SAAS,OAAO,KAAK,GAAG,OAAO;IACrC;IACA,WAAW,OAAO,QAAQ,MAAM;GAClC;GACA,IAAI,MAAM;EACZ;CACF,UAAE;EAEA,OAAY,OAAO,CAAA,CAAE,YAAY,KAAA,CAAS;CAC5C;CACA,MAAM,IAAI,yBACR,oDACF;AACF"}
|