@athenaintel/react 0.55.0 → 0.56.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/README.md +13 -0
- package/dist/agent-runtime/src/answer-label.cjs +8 -2
- package/dist/agent-runtime/src/answer-label.cjs.map +1 -1
- package/dist/agent-runtime/src/answer-label.d.ts +10 -4
- package/dist/agent-runtime/src/answer-label.js +8 -2
- package/dist/agent-runtime/src/answer-label.js.map +1 -1
- package/dist/agent-runtime/src/deep-agent-transport-recipe.cjs +6 -3
- package/dist/agent-runtime/src/deep-agent-transport-recipe.cjs.map +1 -1
- package/dist/agent-runtime/src/deep-agent-transport-recipe.js +6 -3
- package/dist/agent-runtime/src/deep-agent-transport-recipe.js.map +1 -1
- package/dist/agent-runtime/src/edit-command.cjs +40 -19
- package/dist/agent-runtime/src/edit-command.cjs.map +1 -1
- package/dist/agent-runtime/src/edit-command.d.ts +2 -0
- package/dist/agent-runtime/src/edit-command.js +41 -20
- package/dist/agent-runtime/src/edit-command.js.map +1 -1
- package/dist/agent-runtime/src/generated/runconfig-contract.cjs +8 -0
- package/dist/agent-runtime/src/generated/runconfig-contract.cjs.map +1 -1
- package/dist/agent-runtime/src/generated/runconfig-contract.d.ts +9 -0
- package/dist/agent-runtime/src/generated/runconfig-contract.js +8 -0
- package/dist/agent-runtime/src/generated/runconfig-contract.js.map +1 -1
- package/dist/agent-runtime/src/history-store.cjs +426 -0
- package/dist/agent-runtime/src/history-store.cjs.map +1 -0
- package/dist/agent-runtime/src/history-store.d.ts +282 -0
- package/dist/agent-runtime/src/history-store.js +426 -0
- package/dist/agent-runtime/src/history-store.js.map +1 -0
- package/dist/agent-runtime/src/index.d.ts +4 -4
- package/dist/agent-runtime/src/message-window.cjs +43 -5
- package/dist/agent-runtime/src/message-window.cjs.map +1 -1
- package/dist/agent-runtime/src/message-window.d.ts +26 -2
- package/dist/agent-runtime/src/message-window.js +44 -6
- package/dist/agent-runtime/src/message-window.js.map +1 -1
- package/dist/agent-runtime/src/offline-status.cjs.map +1 -1
- package/dist/agent-runtime/src/offline-status.d.ts +85 -4
- package/dist/agent-runtime/src/offline-status.js.map +1 -1
- package/dist/agent-runtime/src/runtime.d.ts +1 -0
- package/dist/agent-runtime/src/tool-metadata.cjs +8 -0
- package/dist/agent-runtime/src/tool-metadata.cjs.map +1 -1
- package/dist/agent-runtime/src/tool-metadata.js +8 -0
- package/dist/agent-runtime/src/tool-metadata.js.map +1 -1
- package/dist/agent-runtime/src/use-deep-agent-thread.cjs +8 -16
- package/dist/agent-runtime/src/use-deep-agent-thread.cjs.map +1 -1
- package/dist/agent-runtime/src/use-deep-agent-thread.d.ts +1 -9
- package/dist/agent-runtime/src/use-deep-agent-thread.js +9 -17
- package/dist/agent-runtime/src/use-deep-agent-thread.js.map +1 -1
- package/dist/agent-runtime/src/use-deep-agent-window-sync.cjs +86 -79
- package/dist/agent-runtime/src/use-deep-agent-window-sync.cjs.map +1 -1
- package/dist/agent-runtime/src/use-deep-agent-window-sync.d.ts +2 -27
- package/dist/agent-runtime/src/use-deep-agent-window-sync.js +89 -82
- package/dist/agent-runtime/src/use-deep-agent-window-sync.js.map +1 -1
- package/dist/chat/super-grouping/CollapsibleGroup.cjs +1 -1
- package/dist/chat/super-grouping/CollapsibleGroup.cjs.map +1 -1
- package/dist/chat/super-grouping/CollapsibleGroup.d.ts +6 -5
- package/dist/chat/super-grouping/CollapsibleGroup.js +1 -1
- package/dist/chat/super-grouping/CollapsibleGroup.js.map +1 -1
- package/dist/chat/super-grouping/earlier-steps-button.cjs +5 -6
- package/dist/chat/super-grouping/earlier-steps-button.cjs.map +1 -1
- package/dist/chat/super-grouping/earlier-steps-button.d.ts +6 -6
- package/dist/chat/super-grouping/earlier-steps-button.js +5 -6
- package/dist/chat/super-grouping/earlier-steps-button.js.map +1 -1
- package/dist/chat/super-grouping/toolkit-map.cjs +1 -0
- package/dist/chat/super-grouping/toolkit-map.cjs.map +1 -1
- package/dist/chat/super-grouping/toolkit-map.js +1 -0
- package/dist/chat/super-grouping/toolkit-map.js.map +1 -1
- package/dist/chat/super-grouping/use-turn-steps.cjs +3 -3
- package/dist/chat/super-grouping/use-turn-steps.js +1 -1
- package/dist/chat-ui/thread/statewire-history-edge.cjs +0 -4
- package/dist/chat-ui/thread/statewire-history-edge.cjs.map +1 -1
- package/dist/chat-ui/thread/statewire-history-edge.d.ts +5 -7
- package/dist/chat-ui/thread/statewire-history-edge.js +1 -5
- package/dist/chat-ui/thread/statewire-history-edge.js.map +1 -1
- package/dist/provider/AthenaProvider.cjs +12 -11
- package/dist/provider/AthenaProvider.cjs.map +1 -1
- package/dist/provider/AthenaProvider.js +11 -10
- package/dist/provider/AthenaProvider.js.map +1 -1
- package/dist/provider/statewire-window-context.cjs.map +1 -1
- package/dist/provider/statewire-window-context.d.ts +8 -8
- package/dist/provider/statewire-window-context.js.map +1 -1
- package/dist/react/package.json.cjs +1 -1
- package/dist/react/package.json.js +1 -1
- package/dist/runtime/useAthenaStatewireRuntime.cjs +0 -2
- package/dist/runtime/useAthenaStatewireRuntime.cjs.map +1 -1
- package/dist/runtime/useAthenaStatewireRuntime.d.ts +5 -5
- package/dist/runtime/useAthenaStatewireRuntime.js +0 -2
- package/dist/runtime/useAthenaStatewireRuntime.js.map +1 -1
- package/package.json +1 -1
- package/dist/agent-runtime/src/history-pages.cjs +0 -279
- package/dist/agent-runtime/src/history-pages.cjs.map +0 -1
- package/dist/agent-runtime/src/history-pages.d.ts +0 -252
- package/dist/agent-runtime/src/history-pages.js +0 -279
- package/dist/agent-runtime/src/history-pages.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-window.cjs","sources":["../../../../agent-runtime/src/message-window.ts"],"sourcesContent":["import type { LangChainMessage } from '@assistant-ui/react-langgraph';\nimport type { StatewireDocuments } from 'statewire';\nimport {\n DEEP_AGENT_CUT_LANE_DOCUMENT,\n DEEP_AGENT_DEFAULT_MESSAGE_WINDOW,\n DEEP_AGENT_HISTORY_MESSAGE_COMMAND,\n DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND,\n DEEP_AGENT_INTEREST_PROTOCOL,\n DEEP_AGENT_MAIN_NS,\n DEEP_AGENT_MAX_INTERESTS,\n DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES,\n DEEP_AGENT_MAX_MESSAGE_WINDOW,\n DEEP_AGENT_MESSAGE_INTERESTS_HEADER,\n DEEP_AGENT_MESSAGE_WINDOW_HEADER,\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n DEEP_AGENT_TRUNCATED_CONTENT_KEY,\n} from './generated/runconfig-contract';\nimport { DEEP_AGENT_STATEWIRE_PROTOCOL } from './statewire-transport';\n\n/**\n * The interest window — the client half of agora's\n * `agora/graphs/athena_deep_agent/message_window.py`.\n *\n * The wire main document carries no `messages`. A client states its interests\n * on the `athena/interest` lane as one array (`set(requests)`): the first is\n * the head (the newest `before` messages of the `main` namespace), the rest\n * are pages anchored `at` the first message of the window they extend. The\n * host holds one ephemeral main document per attach, `{ windows }`, one\n * resolution per request in order (the request echoed plus the covered\n * `chain` and `more` flags; an anchor that vanished leaves an empty window in\n * place), and one shared `athena` document per covered message, keyed\n * `[threadId, \"main\", messageId]` — or `[threadId, \"main\", messageId, \"cut\"]`\n * for a finished card a version-2 view carries cut to its newest steps (its\n * whole lane pages in through `history/lane`, see `lane-history.ts`).\n * {@link readDeepAgentMessageWindow} joins them into the transcript the\n * converter renders.\n *\n * Two host rules shape the client half. The head window never drops below\n * the oldest message it has carried on an attach (a run's appends grow the\n * view, nothing loaded slides out), so a page stays anchored at the head's\n * first message while the transcript grows; and a re-attach's hello carries\n * the view the client held. A fresh client's hello carries the head it\n * declared on the attach headers ({@link withDeclaredMessageWindow}) — a\n * statement cannot ride the first attach, the hello negotiates the lanes. A\n * re-attach declares the view it holds there too\n * ({@link readDeepAgentInterestDeclaration}, {@link withDeclaredInterests}),\n * so its hello carries every page it had loaded on whichever host instance it\n * lands on: the host's own memo of a client id lives on one instance, and a\n * deploy moves the thread to another.\n *\n * Pages are appended one per load until the host's interest cap, then the\n * loaded range is restated in as few maximal requests as it takes\n * ({@link pageInterestRequests}), up to `MAX_INTERESTS × MAX_MESSAGE_WINDOW`\n * messages. The interest document lists every loaded message and the host\n * keeps it within a byte budget, which binds first: about a thousand\n * messages, after which the host refuses a page as `oversized-interest`\n * (`useDeepAgentWindowSync` then reports `canLoadOlder` false). A truncated tool output\n * comes back whole through `history/tool_output` ({@link buildToolOutputCommand});\n * a message's stored form — provenance blobs and all — through\n * `history/message` ({@link buildHistoryMessageCommand}).\n */\n\nconst isIndex = (value: unknown): value is number =>\n typeof value === 'number' && Number.isInteger(value) && value >= 0;\n\n/** What a surface declares for its window: the interest size (the host clamps to its bounds). */\nexport interface DeepAgentMessageWindowDeclaration {\n size?: number;\n}\n\nexport function clampMessageWindowSize(size?: number): number {\n if (size === undefined || !Number.isFinite(size)) return DEEP_AGENT_DEFAULT_MESSAGE_WINDOW;\n return Math.max(\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.trunc(size))\n );\n}\n\n/** An `athena/interest` request over the linear transcript. */\nexport interface DeepAgentInterestRequest {\n readonly ns: typeof DEEP_AGENT_MAIN_NS;\n /** Messages ending at the anchor (the tail without `at`). */\n readonly before: number;\n /** Anchor message id; a page's is the first message of the window it extends. */\n readonly at?: string;\n /** Messages past the anchor (requires `at`). */\n readonly after?: number;\n /** The newest steps of every subagent lane a covered card names (see `lane-follow.ts`). */\n readonly follow?: { readonly before: number };\n}\n\nexport function headInterestRequest(size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size) };\n}\n\nexport function pageInterestRequest(at: string, size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size), at };\n}\n\n/**\n * Attach headers with the head window declared (`x-athena-deep-agent-window`):\n * the hello of a client the host does not remember carries the newest `size`\n * messages. Part of the one attach recipe, so a pooled dial asks for the shape\n * the thread's own attach would. An explicit entry (tests, overrides) wins.\n */\nexport function withDeclaredMessageWindow(\n headers: Record<string, string>,\n declaration: DeepAgentMessageWindowDeclaration = {}\n): Record<string, string> {\n if (DEEP_AGENT_MESSAGE_WINDOW_HEADER in headers) return headers;\n return {\n ...headers,\n [DEEP_AGENT_MESSAGE_WINDOW_HEADER]: String(clampMessageWindowSize(declaration.size)),\n };\n}\n\nexport interface DeepAgentInterestChainEntry {\n id: string;\n siblings: string[];\n}\n\n/** The host's resolution document of one interest: the request echoed plus its coverage. */\nexport interface DeepAgentInterestResolution extends DeepAgentInterestRequest {\n /** Covered message ids, oldest first (a page ends at its anchor). */\n chain: DeepAgentInterestChainEntry[];\n more: { before: boolean; after: boolean };\n}\n\nexport function readDeepAgentInterestResolution(value: unknown): DeepAgentInterestResolution | null {\n if (typeof value !== 'object' || value === null) return null;\n const { ns, before, at, after, chain, more } = value as Record<string, unknown>;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before) || !Array.isArray(chain)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n if (typeof more !== 'object' || more === null) return null;\n const { before: moreBefore, after: moreAfter } = more as Record<string, unknown>;\n if (typeof moreBefore !== 'boolean' || typeof moreAfter !== 'boolean') return null;\n const entries: DeepAgentInterestChainEntry[] = [];\n for (const entry of chain) {\n const { id, siblings } = (entry ?? {}) as Record<string, unknown>;\n if (typeof id !== 'string') return null;\n entries.push({\n id,\n siblings: Array.isArray(siblings) ? siblings.filter((s) => typeof s === 'string') : [id],\n });\n }\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n chain: entries,\n more: { before: moreBefore, after: moreAfter },\n };\n}\n\n/** The attach's interest document: one resolution per stated request, in order. */\nexport interface DeepAgentInterestDocument {\n windows: DeepAgentInterestResolution[];\n}\n\nexport function readDeepAgentInterestDocument(value: unknown): DeepAgentInterestDocument | null {\n if (typeof value !== 'object' || value === null) return null;\n const { windows } = value as Record<string, unknown>;\n if (!Array.isArray(windows)) return null;\n const resolutions: DeepAgentInterestResolution[] = [];\n for (const window of windows) {\n const resolution = readDeepAgentInterestResolution(window);\n if (resolution === null) return null;\n resolutions.push(resolution);\n }\n return { windows: resolutions };\n}\n\nconst interestRequest = ({ ns, before, at, after }: DeepAgentInterestRequest): DeepAgentInterestRequest => ({\n ns,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n});\n\n/** The loaded transcript window, joined from the head interest and its anchored pages. */\nexport interface DeepAgentMessageWindow {\n /** The requests the joined view stands on, in order (the head first): what a `set` restates. A stale page (its anchor gone) or one the head already covers is left out. */\n requests: DeepAgentInterestRequest[];\n /** Covered chain, oldest first. */\n chain: DeepAgentInterestChainEntry[];\n /** The chain's message documents, in chain order (an unmounted one is skipped). */\n messages: LangChainMessage[];\n /** Whether the host holds messages older than the window. */\n hasOlder: boolean;\n /** Pages joined onto the head. */\n pages: number;\n /** The conversation the window's message documents belong to (their key's thread); absent before one is mounted. */\n threadId?: string;\n}\n\n/**\n * The loaded chain plus `grow` older messages, restated in as few requests as\n * it takes: a maximal head, then maximal pages each anchored at the first\n * message of the window above it. `null` once even that exceeds the host's\n * interest cap — the reachable depth is `MAX_INTERESTS × MAX_MESSAGE_WINDOW`.\n */\nfunction coalescedInterestRequests(\n chain: readonly DeepAgentInterestChainEntry[],\n grow: number\n): DeepAgentInterestRequest[] | null {\n const loaded = chain.length;\n const requests: DeepAgentInterestRequest[] = [\n { ns: DEEP_AGENT_MAIN_NS, before: Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, loaded) },\n ];\n // `low` is the chain index of the lowest message the plan covers so far.\n let low = loaded - (requests[0] as DeepAgentInterestRequest).before;\n while (low > 0) {\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, low + 1);\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before,\n at: (chain[low] as DeepAgentInterestChainEntry).id,\n });\n low = low + 1 - before;\n }\n // The plan covers the chain; the new messages extend its lowest request\n // when that stays within one window, else they are a page of their own.\n const last = requests[requests.length - 1] as DeepAgentInterestRequest;\n if (last.before + grow <= DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n requests[requests.length - 1] = { ...last, before: last.before + grow };\n } else {\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before: grow + 1,\n at: (chain[0] as DeepAgentInterestChainEntry).id,\n });\n }\n return requests.length > DEEP_AGENT_MAX_INTERESTS ? null : requests;\n}\n\n/**\n * The requests to state for one more page of older history, or `null` when\n * there is nothing to load (no anchor, the beginning reached) or the host's\n * interest cap is spent. One page is appended, anchored at the window's first\n * message, while the cap allows; at the cap the loaded range is coalesced\n * into maximal requests first (see {@link coalescedInterestRequests}).\n */\nexport function pageInterestRequests(\n window: DeepAgentMessageWindow,\n size?: number\n): DeepAgentInterestRequest[] | null {\n const anchor = window.chain[0]?.id;\n if (anchor === undefined || !window.hasOlder) return null;\n const page = pageInterestRequest(anchor, size);\n const appended = [...window.requests, page];\n if (appended.length <= DEEP_AGENT_MAX_INTERESTS) return appended;\n // A page's `before` counts its anchor: it adds `before - 1` older messages.\n return coalescedInterestRequests(window.chain, page.before - 1);\n}\n\n/**\n * The window off the mounted documents, or `null` while the head interest has\n * not resolved (pre-attach, before the first `set` answer, a wire without\n * documents). A page joins wherever its anchor sits in the chain so far — at\n * the first message it extends the view, inside the head (the head widened\n * past it) it adds nothing and is left out of `requests`; a page whose anchor\n * left the transcript (a rewind emptied it in place) is left out too. A\n * malformed document reads as absent.\n */\nexport function readDeepAgentMessageWindow(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined\n): DeepAgentMessageWindow | null {\n if (mounted === undefined) return null;\n let windows: DeepAgentInterestResolution[] | undefined;\n let threadId: string | undefined;\n const documents = new Map<string, LangChainMessage>();\n for (const document of mounted.values()) {\n const key = document.key;\n if (document.protocol === DEEP_AGENT_INTEREST_PROTOCOL && document.main === true) {\n windows = readDeepAgentInterestDocument(document.value)?.windows;\n } else if (\n document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL &&\n key !== null &&\n key[1] === DEEP_AGENT_MAIN_NS &&\n (key.length === 3 || (key.length === 4 && key[3] === DEEP_AGENT_CUT_LANE_DOCUMENT))\n ) {\n documents.set(key[2] as string, document.value as LangChainMessage);\n if (threadId === undefined && typeof key[0] === 'string') threadId = key[0];\n }\n }\n const head = windows?.[0];\n if (windows === undefined || head === undefined) return null;\n let chain = head.chain;\n let hasOlder = head.more.before;\n const requests = [interestRequest(head)];\n const loaded = new Set(chain.map((entry) => entry.id));\n let pages = 0;\n for (const page of windows.slice(1)) {\n if (page.at === undefined || !loaded.has(page.at)) continue;\n const anchorAt = page.chain.findIndex((entry) => entry.id === page.at);\n if (anchorAt < 0) continue;\n // Both windows are ranges of one linear transcript sharing the anchor:\n // what the page holds below it and the chain does not is a prefix.\n const fresh = page.chain.slice(0, anchorAt).filter((entry) => !loaded.has(entry.id));\n if (fresh.length === 0) {\n // Nothing new, but a page that starts where the loaded chain starts\n // has looked below it: its `more.before` is the answer for the edge.\n if (page.chain[0]?.id === chain[0]?.id) hasOlder = page.more.before;\n continue;\n }\n chain = [...fresh, ...chain];\n for (const entry of fresh) loaded.add(entry.id);\n hasOlder = page.more.before;\n requests.push(interestRequest(page));\n pages += 1;\n }\n const messages: LangChainMessage[] = [];\n for (const entry of chain) {\n const message = documents.get(entry.id);\n if (message !== undefined) messages.push(message);\n }\n return { requests, chain, messages, hasOlder, pages, ...(threadId !== undefined && { threadId }) };\n}\n\n/**\n * The view a client holds, as it declares it on a re-attach\n * (`x-athena-deep-agent-interests`): what the host would have remembered for\n * its client id, read off the attach's own interest document.\n */\nexport interface DeepAgentInterestDeclaration {\n /** Every request the interest document echoes that still covers something, the head first. */\n readonly requests: readonly DeepAgentInterestRequest[];\n /** The oldest message the head covers: the host holds the head down to it as the transcript grows. */\n readonly floor?: string;\n /** Per followed subagent lane, the oldest step a follow window covers. */\n readonly follows?: Readonly<Record<string, string>>;\n}\n\nconst echoedRequest = (window: Record<string, unknown>): DeepAgentInterestRequest | null => {\n const { ns, before, at, after, follow } = window;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n const followBefore =\n typeof follow === 'object' && follow !== null ? (follow as Record<string, unknown>).before : undefined;\n if (follow !== undefined && !isIndex(followBefore)) return null;\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n ...(isIndex(followBefore) && { follow: { before: followBefore } }),\n };\n};\n\nconst firstChainId = (chain: unknown): string | undefined => {\n if (!Array.isArray(chain)) return undefined;\n const id = (chain[0] as { id?: unknown } | undefined)?.id;\n return typeof id === 'string' && id ? id : undefined;\n};\n\n/**\n * The view to declare on a re-attach, read off the mounted interest document,\n * or `null` before one resolved. Requests are taken as the host echoed them\n * (lane `follow`s included); a page whose anchor left the transcript covers\n * nothing and is left out, the head never. The floors are what the host\n * commits from the same document (the head's first message, each lane's\n * deepest follow window's first step).\n */\nexport function readDeepAgentInterestDeclaration(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined\n): DeepAgentInterestDeclaration | null {\n if (mounted === undefined) return null;\n let windows: unknown[] | undefined;\n for (const document of mounted.values()) {\n if (document.protocol !== DEEP_AGENT_INTEREST_PROTOCOL || document.main !== true) continue;\n const value = document.value as { windows?: unknown } | null;\n if (value !== null && typeof value === 'object' && Array.isArray(value.windows)) {\n windows = value.windows;\n }\n }\n if (windows === undefined || windows.length === 0) return null;\n const requests: DeepAgentInterestRequest[] = [];\n const deepest = new Map<string, unknown[]>();\n for (const [index, window] of windows.entries()) {\n if (typeof window !== 'object' || window === null) return null;\n const record = window as Record<string, unknown>;\n const request = echoedRequest(record);\n if (request === null) return null;\n if (index === 0 && request.at !== undefined) return null;\n if (index === 0 || firstChainId(record.chain) !== undefined) requests.push(request);\n const follows = record.follows;\n if (typeof follows !== 'object' || follows === null) continue;\n for (const [ns, follow] of Object.entries(follows)) {\n const chain = (follow as { chain?: unknown } | null)?.chain;\n if (!Array.isArray(chain) || chain.length <= (deepest.get(ns)?.length ?? 0)) continue;\n deepest.set(ns, chain);\n }\n }\n const floor = firstChainId((windows[0] as Record<string, unknown>).chain);\n const follows: Record<string, string> = {};\n for (const [ns, chain] of deepest) {\n const step = firstChainId(chain);\n if (step !== undefined) follows[ns] = step;\n }\n return {\n requests,\n ...(floor !== undefined && { floor }),\n ...(Object.keys(follows).length > 0 && { follows }),\n };\n}\n\n/**\n * `declaration` as the header value: JSON with every non-ASCII character\n * escaped (a header value is Latin-1 on the wire), or `null` when it is\n * larger than the host reads — the attach then falls back to the host's memo\n * or the declared head, as before.\n */\nexport function encodeDeepAgentInterestDeclaration(\n declaration: DeepAgentInterestDeclaration\n): string | null {\n // Everything outside printable ASCII as a JSON escape (a surrogate pair as two).\n const encoded = JSON.stringify(declaration).replace(\n /[^ -~]/g,\n (char) => `\\\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`\n );\n return encoded.length > DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES ? null : encoded;\n}\n\n/**\n * Attach headers with the view a re-attach holds declared\n * (`x-athena-deep-agent-interests`), so its hello carries that view on any\n * host instance. A first attach declares nothing (`null`); an explicit entry\n * wins.\n */\nexport function withDeclaredInterests(\n headers: Record<string, string>,\n declaration: DeepAgentInterestDeclaration | null\n): Record<string, string> {\n if (declaration === null || DEEP_AGENT_MESSAGE_INTERESTS_HEADER in headers) return headers;\n const encoded = encodeDeepAgentInterestDeclaration(declaration);\n return encoded === null ? headers : { ...headers, [DEEP_AGENT_MESSAGE_INTERESTS_HEADER]: encoded };\n}\n\n/**\n * Whether `hello` (a re-attach's window) carries what `held` covered: it\n * reaches `held`'s oldest message, or there is nothing older to reach.\n */\nexport function messageWindowCarries({\n hello,\n held,\n}: {\n hello: DeepAgentMessageWindow;\n held: DeepAgentMessageWindow;\n}): boolean {\n const oldest = held.chain[0]?.id;\n if (oldest === undefined || !hello.hasOlder) return true;\n return hello.chain.some((entry) => entry.id === oldest);\n}\n\n/**\n * The statement that loads `held` again after a hello that did not carry it\n * (a host that neither read the declared view nor remembers the client).\n *\n * The requests it stood on, the head widened to reach the first page's anchor:\n * a head holding its floor covers more than it asked for once a run appended\n * past it, and `hello` (the re-attach's own window) shows what was appended\n * while the view was away, which the head must span too. Without pages, the\n * head is widened to everything it covered. Where that is more than one\n * request can state, the whole chain is restated in maximal windows, each\n * anchored inside the one above it (`coalescedInterestRequests`), so no page\n * is left without its anchor. A hello that does not reach the held view at\n * all (more was appended than it shows) gets the widest head one request can\n * state: the best an older host allows.\n */\nexport function restatedInterestRequests({\n held,\n hello = null,\n}: {\n held: DeepAgentMessageWindow;\n /** The re-attach's own window; omitted, nothing is known to have been appended. */\n hello?: DeepAgentMessageWindow | null;\n}): DeepAgentInterestRequest[] {\n const [head, ...pages] = held.requests;\n if (head === undefined) return [];\n const newest = held.chain.at(-1)?.id;\n const since =\n hello === null || newest === undefined\n ? -1\n : hello.chain.findIndex((entry) => entry.id === newest);\n const chain = since === -1 || hello === null ? held.chain : [...held.chain, ...hello.chain.slice(since + 1)];\n const anchor = pages[0]?.at;\n const start = anchor === undefined ? 0 : chain.findIndex((entry) => entry.id === anchor);\n const covered =\n hello !== null && since === -1\n ? DEEP_AGENT_MAX_MESSAGE_WINDOW\n : chain.length - Math.max(0, start);\n if (covered > DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n const coalesced = coalescedInterestRequests(chain, 0);\n if (coalesced !== null) return coalesced;\n }\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.max(head.before, covered));\n return [{ ...head, before }, ...pages];\n}\n\n/**\n * The windowed wire's read command on the statewire command union, so\n * `sendCommand({ type: 'history/tool_output', … })` typechecks wherever the\n * deep-agent host is driven (`DeepAgentThread.history_tool_output`, an\n * attach-ACL read).\n */\ndeclare module '@assistant-ui/core' {\n namespace Assistant {\n interface Commands {\n /** A tool output the windowed wire carried cut, whole. */\n historyToolOutput: ToolOutputCommand;\n /** One stored message whole — the per-client shape, by command. */\n historyMessage: HistoryMessageCommand;\n }\n }\n}\n\n/** The `history/tool_output` command: a truncated tool output, whole. */\nexport interface ToolOutputCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildToolOutputCommand({ messageId }: { messageId: string }): ToolOutputCommand {\n return { type: DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND, messageId };\n}\n\nexport interface ToolOutputResult {\n messageId: string;\n content: unknown;\n}\n\nexport function readToolOutputResult(payload: unknown): ToolOutputResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, content } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n return { messageId, content };\n}\n\n/**\n * The `history/message` command: a message as the host stores it — the\n * model-provenance blobs the wire strips and an untruncated tool output\n * included. Every client receives one projected wire form; this is the one\n * per-client shape, fetched on demand like `history/tool_output`.\n */\nexport interface HistoryMessageCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_MESSAGE_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildHistoryMessageCommand({\n messageId,\n}: {\n messageId: string;\n}): HistoryMessageCommand {\n return { type: DEEP_AGENT_HISTORY_MESSAGE_COMMAND, messageId };\n}\n\nexport interface HistoryMessageResult {\n messageId: string;\n message: LangChainMessage;\n}\n\nexport function readHistoryMessageResult(payload: unknown): HistoryMessageResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, message } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n if (typeof message !== 'object' || message === null) return null;\n return { messageId, message: message as LangChainMessage };\n}\n\n/** The wire marker on a tool message whose content the host cut. */\nexport interface TruncatedToolContent {\n /** Length of the full output, in characters. */\n chars: number;\n}\n\n/**\n * The truncation marker off a raw wire message, or `null` when the message's\n * content is whole. Pure and selector-safe on the marker object.\n */\nexport function readTruncatedToolContent(message: unknown): TruncatedToolContent | null {\n if (typeof message !== 'object' || message === null) return null;\n const marker = (message as Record<string, unknown>)[DEEP_AGENT_TRUNCATED_CONTENT_KEY];\n if (typeof marker !== 'object' || marker === null) return null;\n const chars = (marker as Record<string, unknown>).chars;\n return isIndex(chars) ? { chars } : null;\n}\n\n/**\n * `artifact` key the shared converter lifts a truncated tool output's marker\n * into (with the tool message id), so a tool card can ask for the full output\n * — the converter joins the ToolMessage into its tool-call part and would\n * otherwise drop the wire marker.\n */\nexport const ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY = '_athena_content_truncated';\n\nexport interface TruncatedToolOutputArtifact extends TruncatedToolContent {\n /** The tool message to name in `history/tool_output`. */\n messageId: string;\n}\n\n/**\n * The lifted truncation marker off a tool-call part's `artifact`, or `null`.\n */\nexport function readTruncatedToolOutputArtifact(\n artifact: unknown\n): TruncatedToolOutputArtifact | null {\n if (typeof artifact !== 'object' || artifact === null) return null;\n const lifted = (artifact as Record<string, unknown>)[ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY];\n if (typeof lifted !== 'object' || lifted === null) return null;\n const { chars, messageId } = lifted as Record<string, unknown>;\n if (!isIndex(chars) || typeof messageId !== 'string' || !messageId) return null;\n return { chars, messageId };\n}\n"],"names":["DEEP_AGENT_DEFAULT_MESSAGE_WINDOW","DEEP_AGENT_MIN_MESSAGE_WINDOW","DEEP_AGENT_MAX_MESSAGE_WINDOW","DEEP_AGENT_MAIN_NS","DEEP_AGENT_MESSAGE_WINDOW_HEADER","DEEP_AGENT_MAX_INTERESTS","DEEP_AGENT_INTEREST_PROTOCOL","DEEP_AGENT_STATEWIRE_PROTOCOL","DEEP_AGENT_CUT_LANE_DOCUMENT","follows","DEEP_AGENT_MAX_M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IAAI,EAAE,UAAU;AACtC;AAQO,MAAM,4CAA4C;AAUlD,SAAS,gCACd,UACoC;AACpC,MAAI,OAAO,aAAa,YAAY,aAAa,KAAM,QAAO;AAC9D,QAAM,SAAU,SAAqC,yCAAyC;AAC9F,MAAI,OAAO,WAAW,YAAY,WAAW,KAAM,QAAO;AAC1D,QAAM,EAAE,OAAO,UAAA,IAAc;AAC7B,MAAI,CAAC,QAAQ,KAAK,KAAK,OAAO,cAAc,YAAY,CAAC,UAAW,QAAO;AAC3E,SAAO,EAAE,OAAO,UAAA;AAClB;;;;;;;;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"message-window.cjs","sources":["../../../../agent-runtime/src/message-window.ts"],"sourcesContent":["import type { LangChainMessage } from '@assistant-ui/react-langgraph';\nimport type { StatewireDocuments } from 'statewire';\nimport {\n DEEP_AGENT_CUT_LANE_DOCUMENT,\n DEEP_AGENT_DEFAULT_MESSAGE_WINDOW,\n DEEP_AGENT_HISTORY_MESSAGE_COMMAND,\n DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND,\n DEEP_AGENT_INTEREST_PROTOCOL,\n DEEP_AGENT_MAIN_NS,\n DEEP_AGENT_MAX_INTERESTS,\n DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES,\n DEEP_AGENT_MAX_MESSAGE_WINDOW,\n DEEP_AGENT_MESSAGE_INTERESTS_HEADER,\n DEEP_AGENT_MESSAGE_WINDOW_HEADER,\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n DEEP_AGENT_TRUNCATED_CONTENT_KEY,\n} from './generated/runconfig-contract';\nimport {\n type DeepAgentHistoryStore,\n deepAgentHistoryStore,\n hostServesHistoryPage,\n} from './history-store';\nimport { readDeepAgentProtocol } from './protocol';\nimport { DEEP_AGENT_STATEWIRE_PROTOCOL } from './statewire-transport';\n\n/**\n * The interest window — the client half of agora's\n * `agora/graphs/athena_deep_agent/message_window.py`.\n *\n * The wire main document carries no `messages`. A client states its interests\n * on the `athena/interest` lane as one array (`set(requests)`): the first is\n * the head (the newest `before` messages of the `main` namespace), the rest\n * are pages anchored `at` the first message of the window they extend. The\n * host holds one ephemeral main document per attach, `{ windows }`, one\n * resolution per request in order (the request echoed plus the covered\n * `chain` and `more` flags; an anchor that vanished leaves an empty window in\n * place), and one shared `athena` document per covered message, keyed\n * `[threadId, \"main\", messageId]` — or `[threadId, \"main\", messageId, \"cut\"]`\n * for a finished card a version-2 view carries cut to its newest steps (its\n * whole lane pages in through `history/lane`, see `lane-history.ts`).\n * {@link readDeepAgentMessageWindow} joins them into the transcript the\n * converter renders.\n *\n * Two host rules shape the client half. The head window never drops below\n * the oldest message it has carried on an attach (a run's appends grow the\n * view, nothing loaded slides out), so a page stays anchored at the head's\n * first message while the transcript grows; and a re-attach's hello carries\n * the view the client held. A fresh client's hello carries the head it\n * declared on the attach headers ({@link withDeclaredMessageWindow}) — a\n * statement cannot ride the first attach, the hello negotiates the lanes. A\n * re-attach declares the view it holds there too\n * ({@link readDeepAgentInterestDeclaration}, {@link withDeclaredInterests}),\n * so its hello carries every page it had loaded on whichever host instance it\n * lands on: the host's own memo of a client id lives on one instance, and a\n * deploy moves the thread to another.\n *\n * On a host that serves `history/page` (its `protocol` lane advertises\n * `DEEP_AGENT_HISTORY_PAGE_PROTOCOL_VERSION`) older history is not an\n * interest at all: it is read by command into the conversation's history\n * store (`history-store.ts`), which {@link readDeepAgentMessageWindow} joins\n * under the live chain (`historyPaged`, `stored`). The interest document then\n * holds the head and lane follows alone, whatever the depth.\n *\n * On older hosts pages are appended one per load until the host's interest\n * cap, then the loaded range is restated in as few maximal requests as it\n * takes ({@link pageInterestRequests}), up to `MAX_INTERESTS ×\n * MAX_MESSAGE_WINDOW` messages. The interest document lists every loaded\n * message and the host keeps it within a byte budget, which binds first:\n * about a thousand messages, after which the host refuses a page as\n * `oversized-interest` (`useDeepAgentWindowSync` then reports `canLoadOlder`\n * false). A truncated tool output\n * comes back whole through `history/tool_output` ({@link buildToolOutputCommand});\n * a message's stored form — provenance blobs and all — through\n * `history/message` ({@link buildHistoryMessageCommand}).\n */\n\nconst isIndex = (value: unknown): value is number =>\n typeof value === 'number' && Number.isInteger(value) && value >= 0;\n\n/** What a surface declares for its window: the interest size (the host clamps to its bounds). */\nexport interface DeepAgentMessageWindowDeclaration {\n size?: number;\n}\n\nexport function clampMessageWindowSize(size?: number): number {\n if (size === undefined || !Number.isFinite(size)) return DEEP_AGENT_DEFAULT_MESSAGE_WINDOW;\n return Math.max(\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.trunc(size))\n );\n}\n\n/** An `athena/interest` request over the linear transcript. */\nexport interface DeepAgentInterestRequest {\n readonly ns: typeof DEEP_AGENT_MAIN_NS;\n /** Messages ending at the anchor (the tail without `at`). */\n readonly before: number;\n /** Anchor message id; a page's is the first message of the window it extends. */\n readonly at?: string;\n /** Messages past the anchor (requires `at`). */\n readonly after?: number;\n /** The newest steps of every subagent lane a covered card names (see `lane-follow.ts`). */\n readonly follow?: { readonly before: number };\n}\n\nexport function headInterestRequest(size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size) };\n}\n\nexport function pageInterestRequest(at: string, size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size), at };\n}\n\n/**\n * Attach headers with the head window declared (`x-athena-deep-agent-window`):\n * the hello of a client the host does not remember carries the newest `size`\n * messages. Part of the one attach recipe, so a pooled dial asks for the shape\n * the thread's own attach would. An explicit entry (tests, overrides) wins.\n */\nexport function withDeclaredMessageWindow(\n headers: Record<string, string>,\n declaration: DeepAgentMessageWindowDeclaration = {}\n): Record<string, string> {\n if (DEEP_AGENT_MESSAGE_WINDOW_HEADER in headers) return headers;\n return {\n ...headers,\n [DEEP_AGENT_MESSAGE_WINDOW_HEADER]: String(clampMessageWindowSize(declaration.size)),\n };\n}\n\nexport interface DeepAgentInterestChainEntry {\n id: string;\n siblings: string[];\n}\n\n/** The host's resolution document of one interest: the request echoed plus its coverage. */\nexport interface DeepAgentInterestResolution extends DeepAgentInterestRequest {\n /** Covered message ids, oldest first (a page ends at its anchor). */\n chain: DeepAgentInterestChainEntry[];\n more: { before: boolean; after: boolean };\n}\n\nexport function readDeepAgentInterestResolution(value: unknown): DeepAgentInterestResolution | null {\n if (typeof value !== 'object' || value === null) return null;\n const { ns, before, at, after, chain, more } = value as Record<string, unknown>;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before) || !Array.isArray(chain)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n if (typeof more !== 'object' || more === null) return null;\n const { before: moreBefore, after: moreAfter } = more as Record<string, unknown>;\n if (typeof moreBefore !== 'boolean' || typeof moreAfter !== 'boolean') return null;\n const entries: DeepAgentInterestChainEntry[] = [];\n for (const entry of chain) {\n const { id, siblings } = (entry ?? {}) as Record<string, unknown>;\n if (typeof id !== 'string') return null;\n entries.push({\n id,\n siblings: Array.isArray(siblings) ? siblings.filter((s) => typeof s === 'string') : [id],\n });\n }\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n chain: entries,\n more: { before: moreBefore, after: moreAfter },\n };\n}\n\n/** The attach's interest document: one resolution per stated request, in order. */\nexport interface DeepAgentInterestDocument {\n windows: DeepAgentInterestResolution[];\n}\n\nexport function readDeepAgentInterestDocument(value: unknown): DeepAgentInterestDocument | null {\n if (typeof value !== 'object' || value === null) return null;\n const { windows } = value as Record<string, unknown>;\n if (!Array.isArray(windows)) return null;\n const resolutions: DeepAgentInterestResolution[] = [];\n for (const window of windows) {\n const resolution = readDeepAgentInterestResolution(window);\n if (resolution === null) return null;\n resolutions.push(resolution);\n }\n return { windows: resolutions };\n}\n\nconst interestRequest = ({ ns, before, at, after }: DeepAgentInterestRequest): DeepAgentInterestRequest => ({\n ns,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n});\n\n/** The loaded transcript window, joined from the head interest and its anchored pages. */\nexport interface DeepAgentMessageWindow {\n /** The requests the joined view stands on, in order (the head first): what a `set` restates. A stale page (its anchor gone) or one the head already covers is left out. */\n requests: DeepAgentInterestRequest[];\n /** Covered chain, oldest first. */\n chain: DeepAgentInterestChainEntry[];\n /** The chain's message documents, in chain order (an unmounted one is skipped). */\n messages: LangChainMessage[];\n /** Whether the host holds messages older than the window. */\n hasOlder: boolean;\n /** Pages joined onto the head. */\n pages: number;\n /** The conversation the window's message documents belong to (their key's thread); absent before one is mounted. */\n threadId?: string;\n /**\n * Of `chain`, how many oldest entries come from the conversation's history\n * store (`history/page`, see `history-store.ts`) rather than the attach's\n * live view; absent when none do (always, on a host that pages history by\n * interest).\n */\n stored?: number;\n /**\n * Set when older history pages by `history/page` into the conversation's\n * store (the host advertises it and has not refused it): what loads the\n * next page. Absent, pages are interests ({@link pageInterestRequests}).\n */\n historyPaged?: true;\n}\n\n/** How {@link readDeepAgentMessageWindow} reads the conversation's history store. */\nexport interface ReadDeepAgentMessageWindowOptions {\n /**\n * The store to record into and join from; `null` reads the live view alone.\n * Default: the conversation's store in the shared registry.\n */\n history?: DeepAgentHistoryStore | null;\n}\n\n/**\n * The loaded chain plus `grow` older messages, restated in as few requests as\n * it takes: a maximal head, then maximal pages each anchored at the first\n * message of the window above it. `null` once even that exceeds the host's\n * interest cap — the reachable depth is `MAX_INTERESTS × MAX_MESSAGE_WINDOW`.\n */\nfunction coalescedInterestRequests(\n chain: readonly DeepAgentInterestChainEntry[],\n grow: number\n): DeepAgentInterestRequest[] | null {\n const loaded = chain.length;\n const requests: DeepAgentInterestRequest[] = [\n { ns: DEEP_AGENT_MAIN_NS, before: Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, loaded) },\n ];\n // `low` is the chain index of the lowest message the plan covers so far.\n let low = loaded - (requests[0] as DeepAgentInterestRequest).before;\n while (low > 0) {\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, low + 1);\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before,\n at: (chain[low] as DeepAgentInterestChainEntry).id,\n });\n low = low + 1 - before;\n }\n // The plan covers the chain; the new messages extend its lowest request\n // when that stays within one window, else they are a page of their own.\n const last = requests[requests.length - 1] as DeepAgentInterestRequest;\n if (last.before + grow <= DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n requests[requests.length - 1] = { ...last, before: last.before + grow };\n } else {\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before: grow + 1,\n at: (chain[0] as DeepAgentInterestChainEntry).id,\n });\n }\n return requests.length > DEEP_AGENT_MAX_INTERESTS ? null : requests;\n}\n\n/**\n * The requests to state for one more page of older history, or `null` when\n * there is nothing to load (no anchor, the beginning reached) or the host's\n * interest cap is spent. One page is appended, anchored at the window's first\n * message, while the cap allows; at the cap the loaded range is coalesced\n * into maximal requests first (see {@link coalescedInterestRequests}).\n */\nexport function pageInterestRequests(\n window: DeepAgentMessageWindow,\n size?: number\n): DeepAgentInterestRequest[] | null {\n const anchor = window.chain[0]?.id;\n if (anchor === undefined || !window.hasOlder) return null;\n const page = pageInterestRequest(anchor, size);\n const appended = [...window.requests, page];\n if (appended.length <= DEEP_AGENT_MAX_INTERESTS) return appended;\n // A page's `before` counts its anchor: it adds `before - 1` older messages.\n return coalescedInterestRequests(window.chain, page.before - 1);\n}\n\n/**\n * The window off the mounted documents, or `null` while the head interest has\n * not resolved (pre-attach, before the first `set` answer, a wire without\n * documents). It records the parents its live chain shows into the\n * conversation's history store and, on a host that serves `history/page`,\n * joins what the store holds older than the chain's first message. A page joins wherever its anchor sits in the chain so far — at\n * the first message it extends the view, inside the head (the head widened\n * past it) it adds nothing and is left out of `requests`; a page whose anchor\n * left the transcript (a rewind emptied it in place) is left out too. A\n * malformed document reads as absent.\n */\nexport function readDeepAgentMessageWindow(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined,\n options: ReadDeepAgentMessageWindowOptions = {}\n): DeepAgentMessageWindow | null {\n if (mounted === undefined) return null;\n let windows: DeepAgentInterestResolution[] | undefined;\n let threadId: string | undefined;\n let hostVersion: number | undefined;\n const documents = new Map<string, LangChainMessage>();\n for (const document of mounted.values()) {\n const key = document.key;\n if (document.protocol === DEEP_AGENT_INTEREST_PROTOCOL && document.main === true) {\n windows = readDeepAgentInterestDocument(document.value)?.windows;\n } else if (document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL && document.main === true) {\n hostVersion = readDeepAgentProtocol(document.value)?.version;\n } else if (\n document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL &&\n key !== null &&\n key[1] === DEEP_AGENT_MAIN_NS &&\n (key.length === 3 || (key.length === 4 && key[3] === DEEP_AGENT_CUT_LANE_DOCUMENT))\n ) {\n documents.set(key[2] as string, document.value as LangChainMessage);\n if (threadId === undefined && typeof key[0] === 'string') threadId = key[0];\n }\n }\n const head = windows?.[0];\n if (windows === undefined || head === undefined) return null;\n let chain = head.chain;\n let hasOlder = head.more.before;\n const requests = [interestRequest(head)];\n const loaded = new Set(chain.map((entry) => entry.id));\n let pages = 0;\n for (const page of windows.slice(1)) {\n if (page.at === undefined || !loaded.has(page.at)) continue;\n const anchorAt = page.chain.findIndex((entry) => entry.id === page.at);\n if (anchorAt < 0) continue;\n // Both windows are ranges of one linear transcript sharing the anchor:\n // what the page holds below it and the chain does not is a prefix.\n const fresh = page.chain.slice(0, anchorAt).filter((entry) => !loaded.has(entry.id));\n if (fresh.length === 0) {\n // Nothing new, but a page that starts where the loaded chain starts\n // has looked below it: its `more.before` is the answer for the edge.\n if (page.chain[0]?.id === chain[0]?.id) hasOlder = page.more.before;\n continue;\n }\n chain = [...fresh, ...chain];\n for (const entry of fresh) loaded.add(entry.id);\n hasOlder = page.more.before;\n requests.push(interestRequest(page));\n pages += 1;\n }\n let messages: LangChainMessage[] = [];\n for (const entry of chain) {\n const message = documents.get(entry.id);\n if (message !== undefined) messages.push(message);\n }\n let stored = 0;\n let historyPaged = false;\n const history =\n options.history !== undefined\n ? options.history\n : threadId !== undefined\n ? deepAgentHistoryStore(threadId)\n : null;\n const first = chain[0]?.id;\n if (history !== null && first !== undefined) {\n history.recordChain(\n chain.map((entry) => entry.id),\n documents,\n !hasOlder\n );\n if (hostServesHistoryPage(hostVersion) && !history.unserved) {\n historyPaged = true;\n // Older history joins under the live chain by its parents, so only\n // true ancestors of what the host shows now are ever read in.\n const older = history.olderThan(first);\n if (older.ids.length > 0) {\n stored = older.ids.length;\n chain = [...older.ids.map((id) => ({ id, siblings: [id] })), ...chain];\n messages = [...older.messages, ...messages];\n hasOlder = older.hasOlder;\n } else if (history.parentOf(first) === null) {\n hasOlder = false;\n }\n // A summarized turn's first reply carries its exact counts, wherever\n // it sits (a turn the page cut short resumes in the live chain).\n if (threadId !== undefined) messages = history.markTurns(threadId, messages);\n }\n }\n return {\n requests,\n chain,\n messages,\n hasOlder,\n pages,\n ...(stored > 0 && { stored }),\n ...(historyPaged && { historyPaged: true as const }),\n ...(threadId !== undefined && { threadId }),\n };\n}\n\n/**\n * The view a client holds, as it declares it on a re-attach\n * (`x-athena-deep-agent-interests`): what the host would have remembered for\n * its client id, read off the attach's own interest document.\n */\nexport interface DeepAgentInterestDeclaration {\n /** Every request the interest document echoes that still covers something, the head first. */\n readonly requests: readonly DeepAgentInterestRequest[];\n /** The oldest message the head covers: the host holds the head down to it as the transcript grows. */\n readonly floor?: string;\n /** Per followed subagent lane, the oldest step a follow window covers. */\n readonly follows?: Readonly<Record<string, string>>;\n}\n\nconst echoedRequest = (window: Record<string, unknown>): DeepAgentInterestRequest | null => {\n const { ns, before, at, after, follow } = window;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n const followBefore =\n typeof follow === 'object' && follow !== null ? (follow as Record<string, unknown>).before : undefined;\n if (follow !== undefined && !isIndex(followBefore)) return null;\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n ...(isIndex(followBefore) && { follow: { before: followBefore } }),\n };\n};\n\nconst firstChainId = (chain: unknown): string | undefined => {\n if (!Array.isArray(chain)) return undefined;\n const id = (chain[0] as { id?: unknown } | undefined)?.id;\n return typeof id === 'string' && id ? id : undefined;\n};\n\n/**\n * The view to declare on a re-attach, read off the mounted interest document,\n * or `null` before one resolved. Requests are taken as the host echoed them\n * (lane `follow`s included); a page whose anchor left the transcript covers\n * nothing and is left out, the head never. The floors are what the host\n * commits from the same document (the head's first message, each lane's\n * deepest follow window's first step).\n */\nexport function readDeepAgentInterestDeclaration(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined\n): DeepAgentInterestDeclaration | null {\n if (mounted === undefined) return null;\n let windows: unknown[] | undefined;\n for (const document of mounted.values()) {\n if (document.protocol !== DEEP_AGENT_INTEREST_PROTOCOL || document.main !== true) continue;\n const value = document.value as { windows?: unknown } | null;\n if (value !== null && typeof value === 'object' && Array.isArray(value.windows)) {\n windows = value.windows;\n }\n }\n if (windows === undefined || windows.length === 0) return null;\n const requests: DeepAgentInterestRequest[] = [];\n const deepest = new Map<string, unknown[]>();\n for (const [index, window] of windows.entries()) {\n if (typeof window !== 'object' || window === null) return null;\n const record = window as Record<string, unknown>;\n const request = echoedRequest(record);\n if (request === null) return null;\n if (index === 0 && request.at !== undefined) return null;\n if (index === 0 || firstChainId(record.chain) !== undefined) requests.push(request);\n const follows = record.follows;\n if (typeof follows !== 'object' || follows === null) continue;\n for (const [ns, follow] of Object.entries(follows)) {\n const chain = (follow as { chain?: unknown } | null)?.chain;\n if (!Array.isArray(chain) || chain.length <= (deepest.get(ns)?.length ?? 0)) continue;\n deepest.set(ns, chain);\n }\n }\n const floor = firstChainId((windows[0] as Record<string, unknown>).chain);\n const follows: Record<string, string> = {};\n for (const [ns, chain] of deepest) {\n const step = firstChainId(chain);\n if (step !== undefined) follows[ns] = step;\n }\n return {\n requests,\n ...(floor !== undefined && { floor }),\n ...(Object.keys(follows).length > 0 && { follows }),\n };\n}\n\n/**\n * `declaration` as the header value: JSON with every non-ASCII character\n * escaped (a header value is Latin-1 on the wire), or `null` when it is\n * larger than the host reads — the attach then falls back to the host's memo\n * or the declared head, as before.\n */\nexport function encodeDeepAgentInterestDeclaration(\n declaration: DeepAgentInterestDeclaration\n): string | null {\n // Everything outside printable ASCII as a JSON escape (a surrogate pair as two).\n const encoded = JSON.stringify(declaration).replace(\n /[^ -~]/g,\n (char) => `\\\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`\n );\n return encoded.length > DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES ? null : encoded;\n}\n\n/**\n * Attach headers with the view a re-attach holds declared\n * (`x-athena-deep-agent-interests`), so its hello carries that view on any\n * host instance. A first attach declares nothing (`null`); an explicit entry\n * wins.\n */\nexport function withDeclaredInterests(\n headers: Record<string, string>,\n declaration: DeepAgentInterestDeclaration | null\n): Record<string, string> {\n if (declaration === null || DEEP_AGENT_MESSAGE_INTERESTS_HEADER in headers) return headers;\n const encoded = encodeDeepAgentInterestDeclaration(declaration);\n return encoded === null ? headers : { ...headers, [DEEP_AGENT_MESSAGE_INTERESTS_HEADER]: encoded };\n}\n\n/**\n * Whether `hello` (a re-attach's window) carries what `held` covered: it\n * reaches `held`'s oldest message, or there is nothing older to reach.\n */\nexport function messageWindowCarries({\n hello,\n held,\n}: {\n hello: DeepAgentMessageWindow;\n held: DeepAgentMessageWindow;\n}): boolean {\n const oldest = held.chain[0]?.id;\n if (oldest === undefined || !hello.hasOlder) return true;\n return hello.chain.some((entry) => entry.id === oldest);\n}\n\n/**\n * The statement that loads `held` again after a hello that did not carry it\n * (a host that neither read the declared view nor remembers the client).\n *\n * The requests it stood on, the head widened to reach the first page's anchor:\n * a head holding its floor covers more than it asked for once a run appended\n * past it, and `hello` (the re-attach's own window) shows what was appended\n * while the view was away, which the head must span too. Without pages, the\n * head is widened to everything it covered. Where that is more than one\n * request can state, the whole chain is restated in maximal windows, each\n * anchored inside the one above it (`coalescedInterestRequests`), so no page\n * is left without its anchor. A hello that does not reach the held view at\n * all (more was appended than it shows) gets the widest head one request can\n * state: the best an older host allows.\n */\nexport function restatedInterestRequests({\n held,\n hello = null,\n}: {\n held: DeepAgentMessageWindow;\n /** The re-attach's own window; omitted, nothing is known to have been appended. */\n hello?: DeepAgentMessageWindow | null;\n}): DeepAgentInterestRequest[] {\n const [head, ...pages] = held.requests;\n if (head === undefined) return [];\n const newest = held.chain.at(-1)?.id;\n const since =\n hello === null || newest === undefined\n ? -1\n : hello.chain.findIndex((entry) => entry.id === newest);\n const chain = since === -1 || hello === null ? held.chain : [...held.chain, ...hello.chain.slice(since + 1)];\n const anchor = pages[0]?.at;\n const start = anchor === undefined ? 0 : chain.findIndex((entry) => entry.id === anchor);\n const covered =\n hello !== null && since === -1\n ? DEEP_AGENT_MAX_MESSAGE_WINDOW\n : chain.length - Math.max(0, start);\n if (covered > DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n const coalesced = coalescedInterestRequests(chain, 0);\n if (coalesced !== null) return coalesced;\n }\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.max(head.before, covered));\n return [{ ...head, before }, ...pages];\n}\n\n/**\n * The windowed wire's read command on the statewire command union, so\n * `sendCommand({ type: 'history/tool_output', … })` typechecks wherever the\n * deep-agent host is driven (`DeepAgentThread.history_tool_output`, an\n * attach-ACL read).\n */\ndeclare module '@assistant-ui/core' {\n namespace Assistant {\n interface Commands {\n /** A tool output the windowed wire carried cut, whole. */\n historyToolOutput: ToolOutputCommand;\n /** One stored message whole — the per-client shape, by command. */\n historyMessage: HistoryMessageCommand;\n }\n }\n}\n\n/** The `history/tool_output` command: a truncated tool output, whole. */\nexport interface ToolOutputCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildToolOutputCommand({ messageId }: { messageId: string }): ToolOutputCommand {\n return { type: DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND, messageId };\n}\n\nexport interface ToolOutputResult {\n messageId: string;\n content: unknown;\n}\n\nexport function readToolOutputResult(payload: unknown): ToolOutputResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, content } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n return { messageId, content };\n}\n\n/**\n * The `history/message` command: a message as the host stores it — the\n * model-provenance blobs the wire strips and an untruncated tool output\n * included. Every client receives one projected wire form; this is the one\n * per-client shape, fetched on demand like `history/tool_output`.\n */\nexport interface HistoryMessageCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_MESSAGE_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildHistoryMessageCommand({\n messageId,\n}: {\n messageId: string;\n}): HistoryMessageCommand {\n return { type: DEEP_AGENT_HISTORY_MESSAGE_COMMAND, messageId };\n}\n\nexport interface HistoryMessageResult {\n messageId: string;\n message: LangChainMessage;\n}\n\nexport function readHistoryMessageResult(payload: unknown): HistoryMessageResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, message } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n if (typeof message !== 'object' || message === null) return null;\n return { messageId, message: message as LangChainMessage };\n}\n\n/** The wire marker on a tool message whose content the host cut. */\nexport interface TruncatedToolContent {\n /** Length of the full output, in characters. */\n chars: number;\n}\n\n/**\n * The truncation marker off a raw wire message, or `null` when the message's\n * content is whole. Pure and selector-safe on the marker object.\n */\nexport function readTruncatedToolContent(message: unknown): TruncatedToolContent | null {\n if (typeof message !== 'object' || message === null) return null;\n const marker = (message as Record<string, unknown>)[DEEP_AGENT_TRUNCATED_CONTENT_KEY];\n if (typeof marker !== 'object' || marker === null) return null;\n const chars = (marker as Record<string, unknown>).chars;\n return isIndex(chars) ? { chars } : null;\n}\n\n/**\n * `artifact` key the shared converter lifts a truncated tool output's marker\n * into (with the tool message id), so a tool card can ask for the full output\n * — the converter joins the ToolMessage into its tool-call part and would\n * otherwise drop the wire marker.\n */\nexport const ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY = '_athena_content_truncated';\n\nexport interface TruncatedToolOutputArtifact extends TruncatedToolContent {\n /** The tool message to name in `history/tool_output`. */\n messageId: string;\n}\n\n/**\n * The lifted truncation marker off a tool-call part's `artifact`, or `null`.\n */\nexport function readTruncatedToolOutputArtifact(\n artifact: unknown\n): TruncatedToolOutputArtifact | null {\n if (typeof artifact !== 'object' || artifact === null) return null;\n const lifted = (artifact as Record<string, unknown>)[ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY];\n if (typeof lifted !== 'object' || lifted === null) return null;\n const { chars, messageId } = lifted as Record<string, unknown>;\n if (!isIndex(chars) || typeof messageId !== 'string' || !messageId) return null;\n return { chars, messageId };\n}\n"],"names":["DEEP_AGENT_DEFAULT_MESSAGE_WINDOW","DEEP_AGENT_MIN_MESSAGE_WINDOW","DEEP_AGENT_MAX_MESSAGE_WINDOW","DEEP_AGENT_MAIN_NS","DEEP_AGENT_MESSAGE_WINDOW_HEADER","DEEP_AGENT_MAX_INTERESTS","DEEP_AGENT_INTEREST_PROTOCOL","DEEP_AGENT_STATEWIRE_PROTOCOL","readDeepAgentProtocol","DEEP_AGENT_CUT_LANE_DOCUMENT","deepAgentHistoryStore","hostServesHistoryPage","follows","DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES","DEEP_AGENT_MESSAGE_INTERESTS_HEADER","DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND","DEEP_AGENT_HISTORY_MESSAGE_COMMAND","DEEP_AGENT_TRUNCATED_CONTENT_KEY"],"mappings":";;;;;;AA4EA,MAAM,UAAU,CAAC,UACf,OAAO,UAAU,YAAY,OAAO,UAAU,KAAK,KAAK,SAAS;AAO5D,SAAS,uBAAuB,MAAuB;AAC5D,M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istant-ui/react-langgraph';
|
|
2
2
|
import type { StatewireDocuments } from 'statewire';
|
|
3
3
|
import { DEEP_AGENT_HISTORY_MESSAGE_COMMAND, DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND, DEEP_AGENT_MAIN_NS } from './generated/runconfig-contract';
|
|
4
|
+
import { type DeepAgentHistoryStore } from './history-store';
|
|
4
5
|
/** What a surface declares for its window: the interest size (the host clamps to its bounds). */
|
|
5
6
|
export interface DeepAgentMessageWindowDeclaration {
|
|
6
7
|
size?: number;
|
|
@@ -62,6 +63,27 @@ export interface DeepAgentMessageWindow {
|
|
|
62
63
|
pages: number;
|
|
63
64
|
/** The conversation the window's message documents belong to (their key's thread); absent before one is mounted. */
|
|
64
65
|
threadId?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Of `chain`, how many oldest entries come from the conversation's history
|
|
68
|
+
* store (`history/page`, see `history-store.ts`) rather than the attach's
|
|
69
|
+
* live view; absent when none do (always, on a host that pages history by
|
|
70
|
+
* interest).
|
|
71
|
+
*/
|
|
72
|
+
stored?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Set when older history pages by `history/page` into the conversation's
|
|
75
|
+
* store (the host advertises it and has not refused it): what loads the
|
|
76
|
+
* next page. Absent, pages are interests ({@link pageInterestRequests}).
|
|
77
|
+
*/
|
|
78
|
+
historyPaged?: true;
|
|
79
|
+
}
|
|
80
|
+
/** How {@link readDeepAgentMessageWindow} reads the conversation's history store. */
|
|
81
|
+
export interface ReadDeepAgentMessageWindowOptions {
|
|
82
|
+
/**
|
|
83
|
+
* The store to record into and join from; `null` reads the live view alone.
|
|
84
|
+
* Default: the conversation's store in the shared registry.
|
|
85
|
+
*/
|
|
86
|
+
history?: DeepAgentHistoryStore | null;
|
|
65
87
|
}
|
|
66
88
|
/**
|
|
67
89
|
* The requests to state for one more page of older history, or `null` when
|
|
@@ -74,13 +96,15 @@ export declare function pageInterestRequests(window: DeepAgentMessageWindow, siz
|
|
|
74
96
|
/**
|
|
75
97
|
* The window off the mounted documents, or `null` while the head interest has
|
|
76
98
|
* not resolved (pre-attach, before the first `set` answer, a wire without
|
|
77
|
-
* documents).
|
|
99
|
+
* documents). It records the parents its live chain shows into the
|
|
100
|
+
* conversation's history store and, on a host that serves `history/page`,
|
|
101
|
+
* joins what the store holds older than the chain's first message. A page joins wherever its anchor sits in the chain so far — at
|
|
78
102
|
* the first message it extends the view, inside the head (the head widened
|
|
79
103
|
* past it) it adds nothing and is left out of `requests`; a page whose anchor
|
|
80
104
|
* left the transcript (a rewind emptied it in place) is left out too. A
|
|
81
105
|
* malformed document reads as absent.
|
|
82
106
|
*/
|
|
83
|
-
export declare function readDeepAgentMessageWindow(mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined): DeepAgentMessageWindow | null;
|
|
107
|
+
export declare function readDeepAgentMessageWindow(mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined, options?: ReadDeepAgentMessageWindowOptions): DeepAgentMessageWindow | null;
|
|
84
108
|
/**
|
|
85
109
|
* The view a client holds, as it declares it on a re-attach
|
|
86
110
|
* (`x-athena-deep-agent-interests`): what the host would have remembered for
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { DEEP_AGENT_MAX_MESSAGE_WINDOW,
|
|
1
|
+
import { DEEP_AGENT_MAX_MESSAGE_WINDOW, DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND, DEEP_AGENT_INTEREST_PROTOCOL, DEEP_AGENT_MAIN_NS, DEEP_AGENT_CUT_LANE_DOCUMENT, DEEP_AGENT_MAX_INTERESTS, DEEP_AGENT_DEFAULT_MESSAGE_WINDOW, DEEP_AGENT_MIN_MESSAGE_WINDOW, DEEP_AGENT_MESSAGE_INTERESTS_HEADER, DEEP_AGENT_MESSAGE_WINDOW_HEADER, DEEP_AGENT_HISTORY_MESSAGE_COMMAND, DEEP_AGENT_TRUNCATED_CONTENT_KEY, DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES } from "./generated/runconfig-contract.js";
|
|
2
|
+
import { deepAgentHistoryStore, hostServesHistoryPage } from "./history-store.js";
|
|
3
|
+
import { readDeepAgentProtocol } from "./protocol.js";
|
|
2
4
|
import { DEEP_AGENT_STATEWIRE_PROTOCOL } from "./statewire-transport.js";
|
|
3
5
|
const isIndex = (value) => typeof value === "number" && Number.isInteger(value) && value >= 0;
|
|
4
6
|
function clampMessageWindowSize(size) {
|
|
@@ -102,16 +104,19 @@ function pageInterestRequests(window, size) {
|
|
|
102
104
|
if (appended.length <= DEEP_AGENT_MAX_INTERESTS) return appended;
|
|
103
105
|
return coalescedInterestRequests(window.chain, page.before - 1);
|
|
104
106
|
}
|
|
105
|
-
function readDeepAgentMessageWindow(mounted) {
|
|
106
|
-
var _a, _b, _c;
|
|
107
|
+
function readDeepAgentMessageWindow(mounted, options = {}) {
|
|
108
|
+
var _a, _b, _c, _d, _e;
|
|
107
109
|
if (mounted === void 0) return null;
|
|
108
110
|
let windows;
|
|
109
111
|
let threadId;
|
|
112
|
+
let hostVersion;
|
|
110
113
|
const documents = /* @__PURE__ */ new Map();
|
|
111
114
|
for (const document of mounted.values()) {
|
|
112
115
|
const key = document.key;
|
|
113
116
|
if (document.protocol === DEEP_AGENT_INTEREST_PROTOCOL && document.main === true) {
|
|
114
117
|
windows = (_a = readDeepAgentInterestDocument(document.value)) == null ? void 0 : _a.windows;
|
|
118
|
+
} else if (document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL && document.main === true) {
|
|
119
|
+
hostVersion = (_b = readDeepAgentProtocol(document.value)) == null ? void 0 : _b.version;
|
|
115
120
|
} else if (document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL && key !== null && key[1] === DEEP_AGENT_MAIN_NS && (key.length === 3 || key.length === 4 && key[3] === DEEP_AGENT_CUT_LANE_DOCUMENT)) {
|
|
116
121
|
documents.set(key[2], document.value);
|
|
117
122
|
if (threadId === void 0 && typeof key[0] === "string") threadId = key[0];
|
|
@@ -130,7 +135,7 @@ function readDeepAgentMessageWindow(mounted) {
|
|
|
130
135
|
if (anchorAt < 0) continue;
|
|
131
136
|
const fresh = page.chain.slice(0, anchorAt).filter((entry) => !loaded.has(entry.id));
|
|
132
137
|
if (fresh.length === 0) {
|
|
133
|
-
if (((
|
|
138
|
+
if (((_c = page.chain[0]) == null ? void 0 : _c.id) === ((_d = chain[0]) == null ? void 0 : _d.id)) hasOlder = page.more.before;
|
|
134
139
|
continue;
|
|
135
140
|
}
|
|
136
141
|
chain = [...fresh, ...chain];
|
|
@@ -139,12 +144,45 @@ function readDeepAgentMessageWindow(mounted) {
|
|
|
139
144
|
requests.push(interestRequest(page));
|
|
140
145
|
pages += 1;
|
|
141
146
|
}
|
|
142
|
-
|
|
147
|
+
let messages = [];
|
|
143
148
|
for (const entry of chain) {
|
|
144
149
|
const message = documents.get(entry.id);
|
|
145
150
|
if (message !== void 0) messages.push(message);
|
|
146
151
|
}
|
|
147
|
-
|
|
152
|
+
let stored = 0;
|
|
153
|
+
let historyPaged = false;
|
|
154
|
+
const history = options.history !== void 0 ? options.history : threadId !== void 0 ? deepAgentHistoryStore(threadId) : null;
|
|
155
|
+
const first = (_e = chain[0]) == null ? void 0 : _e.id;
|
|
156
|
+
if (history !== null && first !== void 0) {
|
|
157
|
+
history.recordChain(
|
|
158
|
+
chain.map((entry) => entry.id),
|
|
159
|
+
documents,
|
|
160
|
+
!hasOlder
|
|
161
|
+
);
|
|
162
|
+
if (hostServesHistoryPage(hostVersion) && !history.unserved) {
|
|
163
|
+
historyPaged = true;
|
|
164
|
+
const older = history.olderThan(first);
|
|
165
|
+
if (older.ids.length > 0) {
|
|
166
|
+
stored = older.ids.length;
|
|
167
|
+
chain = [...older.ids.map((id) => ({ id, siblings: [id] })), ...chain];
|
|
168
|
+
messages = [...older.messages, ...messages];
|
|
169
|
+
hasOlder = older.hasOlder;
|
|
170
|
+
} else if (history.parentOf(first) === null) {
|
|
171
|
+
hasOlder = false;
|
|
172
|
+
}
|
|
173
|
+
if (threadId !== void 0) messages = history.markTurns(threadId, messages);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
requests,
|
|
178
|
+
chain,
|
|
179
|
+
messages,
|
|
180
|
+
hasOlder,
|
|
181
|
+
pages,
|
|
182
|
+
...stored > 0 && { stored },
|
|
183
|
+
...historyPaged && { historyPaged: true },
|
|
184
|
+
...threadId !== void 0 && { threadId }
|
|
185
|
+
};
|
|
148
186
|
}
|
|
149
187
|
const echoedRequest = (window) => {
|
|
150
188
|
const { ns, before, at, after, follow } = window;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-window.js","sources":["../../../../agent-runtime/src/message-window.ts"],"sourcesContent":["import type { LangChainMessage } from '@assistant-ui/react-langgraph';\nimport type { StatewireDocuments } from 'statewire';\nimport {\n DEEP_AGENT_CUT_LANE_DOCUMENT,\n DEEP_AGENT_DEFAULT_MESSAGE_WINDOW,\n DEEP_AGENT_HISTORY_MESSAGE_COMMAND,\n DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND,\n DEEP_AGENT_INTEREST_PROTOCOL,\n DEEP_AGENT_MAIN_NS,\n DEEP_AGENT_MAX_INTERESTS,\n DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES,\n DEEP_AGENT_MAX_MESSAGE_WINDOW,\n DEEP_AGENT_MESSAGE_INTERESTS_HEADER,\n DEEP_AGENT_MESSAGE_WINDOW_HEADER,\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n DEEP_AGENT_TRUNCATED_CONTENT_KEY,\n} from './generated/runconfig-contract';\nimport { DEEP_AGENT_STATEWIRE_PROTOCOL } from './statewire-transport';\n\n/**\n * The interest window — the client half of agora's\n * `agora/graphs/athena_deep_agent/message_window.py`.\n *\n * The wire main document carries no `messages`. A client states its interests\n * on the `athena/interest` lane as one array (`set(requests)`): the first is\n * the head (the newest `before` messages of the `main` namespace), the rest\n * are pages anchored `at` the first message of the window they extend. The\n * host holds one ephemeral main document per attach, `{ windows }`, one\n * resolution per request in order (the request echoed plus the covered\n * `chain` and `more` flags; an anchor that vanished leaves an empty window in\n * place), and one shared `athena` document per covered message, keyed\n * `[threadId, \"main\", messageId]` — or `[threadId, \"main\", messageId, \"cut\"]`\n * for a finished card a version-2 view carries cut to its newest steps (its\n * whole lane pages in through `history/lane`, see `lane-history.ts`).\n * {@link readDeepAgentMessageWindow} joins them into the transcript the\n * converter renders.\n *\n * Two host rules shape the client half. The head window never drops below\n * the oldest message it has carried on an attach (a run's appends grow the\n * view, nothing loaded slides out), so a page stays anchored at the head's\n * first message while the transcript grows; and a re-attach's hello carries\n * the view the client held. A fresh client's hello carries the head it\n * declared on the attach headers ({@link withDeclaredMessageWindow}) — a\n * statement cannot ride the first attach, the hello negotiates the lanes. A\n * re-attach declares the view it holds there too\n * ({@link readDeepAgentInterestDeclaration}, {@link withDeclaredInterests}),\n * so its hello carries every page it had loaded on whichever host instance it\n * lands on: the host's own memo of a client id lives on one instance, and a\n * deploy moves the thread to another.\n *\n * Pages are appended one per load until the host's interest cap, then the\n * loaded range is restated in as few maximal requests as it takes\n * ({@link pageInterestRequests}), up to `MAX_INTERESTS × MAX_MESSAGE_WINDOW`\n * messages. The interest document lists every loaded message and the host\n * keeps it within a byte budget, which binds first: about a thousand\n * messages, after which the host refuses a page as `oversized-interest`\n * (`useDeepAgentWindowSync` then reports `canLoadOlder` false). A truncated tool output\n * comes back whole through `history/tool_output` ({@link buildToolOutputCommand});\n * a message's stored form — provenance blobs and all — through\n * `history/message` ({@link buildHistoryMessageCommand}).\n */\n\nconst isIndex = (value: unknown): value is number =>\n typeof value === 'number' && Number.isInteger(value) && value >= 0;\n\n/** What a surface declares for its window: the interest size (the host clamps to its bounds). */\nexport interface DeepAgentMessageWindowDeclaration {\n size?: number;\n}\n\nexport function clampMessageWindowSize(size?: number): number {\n if (size === undefined || !Number.isFinite(size)) return DEEP_AGENT_DEFAULT_MESSAGE_WINDOW;\n return Math.max(\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.trunc(size))\n );\n}\n\n/** An `athena/interest` request over the linear transcript. */\nexport interface DeepAgentInterestRequest {\n readonly ns: typeof DEEP_AGENT_MAIN_NS;\n /** Messages ending at the anchor (the tail without `at`). */\n readonly before: number;\n /** Anchor message id; a page's is the first message of the window it extends. */\n readonly at?: string;\n /** Messages past the anchor (requires `at`). */\n readonly after?: number;\n /** The newest steps of every subagent lane a covered card names (see `lane-follow.ts`). */\n readonly follow?: { readonly before: number };\n}\n\nexport function headInterestRequest(size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size) };\n}\n\nexport function pageInterestRequest(at: string, size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size), at };\n}\n\n/**\n * Attach headers with the head window declared (`x-athena-deep-agent-window`):\n * the hello of a client the host does not remember carries the newest `size`\n * messages. Part of the one attach recipe, so a pooled dial asks for the shape\n * the thread's own attach would. An explicit entry (tests, overrides) wins.\n */\nexport function withDeclaredMessageWindow(\n headers: Record<string, string>,\n declaration: DeepAgentMessageWindowDeclaration = {}\n): Record<string, string> {\n if (DEEP_AGENT_MESSAGE_WINDOW_HEADER in headers) return headers;\n return {\n ...headers,\n [DEEP_AGENT_MESSAGE_WINDOW_HEADER]: String(clampMessageWindowSize(declaration.size)),\n };\n}\n\nexport interface DeepAgentInterestChainEntry {\n id: string;\n siblings: string[];\n}\n\n/** The host's resolution document of one interest: the request echoed plus its coverage. */\nexport interface DeepAgentInterestResolution extends DeepAgentInterestRequest {\n /** Covered message ids, oldest first (a page ends at its anchor). */\n chain: DeepAgentInterestChainEntry[];\n more: { before: boolean; after: boolean };\n}\n\nexport function readDeepAgentInterestResolution(value: unknown): DeepAgentInterestResolution | null {\n if (typeof value !== 'object' || value === null) return null;\n const { ns, before, at, after, chain, more } = value as Record<string, unknown>;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before) || !Array.isArray(chain)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n if (typeof more !== 'object' || more === null) return null;\n const { before: moreBefore, after: moreAfter } = more as Record<string, unknown>;\n if (typeof moreBefore !== 'boolean' || typeof moreAfter !== 'boolean') return null;\n const entries: DeepAgentInterestChainEntry[] = [];\n for (const entry of chain) {\n const { id, siblings } = (entry ?? {}) as Record<string, unknown>;\n if (typeof id !== 'string') return null;\n entries.push({\n id,\n siblings: Array.isArray(siblings) ? siblings.filter((s) => typeof s === 'string') : [id],\n });\n }\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n chain: entries,\n more: { before: moreBefore, after: moreAfter },\n };\n}\n\n/** The attach's interest document: one resolution per stated request, in order. */\nexport interface DeepAgentInterestDocument {\n windows: DeepAgentInterestResolution[];\n}\n\nexport function readDeepAgentInterestDocument(value: unknown): DeepAgentInterestDocument | null {\n if (typeof value !== 'object' || value === null) return null;\n const { windows } = value as Record<string, unknown>;\n if (!Array.isArray(windows)) return null;\n const resolutions: DeepAgentInterestResolution[] = [];\n for (const window of windows) {\n const resolution = readDeepAgentInterestResolution(window);\n if (resolution === null) return null;\n resolutions.push(resolution);\n }\n return { windows: resolutions };\n}\n\nconst interestRequest = ({ ns, before, at, after }: DeepAgentInterestRequest): DeepAgentInterestRequest => ({\n ns,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n});\n\n/** The loaded transcript window, joined from the head interest and its anchored pages. */\nexport interface DeepAgentMessageWindow {\n /** The requests the joined view stands on, in order (the head first): what a `set` restates. A stale page (its anchor gone) or one the head already covers is left out. */\n requests: DeepAgentInterestRequest[];\n /** Covered chain, oldest first. */\n chain: DeepAgentInterestChainEntry[];\n /** The chain's message documents, in chain order (an unmounted one is skipped). */\n messages: LangChainMessage[];\n /** Whether the host holds messages older than the window. */\n hasOlder: boolean;\n /** Pages joined onto the head. */\n pages: number;\n /** The conversation the window's message documents belong to (their key's thread); absent before one is mounted. */\n threadId?: string;\n}\n\n/**\n * The loaded chain plus `grow` older messages, restated in as few requests as\n * it takes: a maximal head, then maximal pages each anchored at the first\n * message of the window above it. `null` once even that exceeds the host's\n * interest cap — the reachable depth is `MAX_INTERESTS × MAX_MESSAGE_WINDOW`.\n */\nfunction coalescedInterestRequests(\n chain: readonly DeepAgentInterestChainEntry[],\n grow: number\n): DeepAgentInterestRequest[] | null {\n const loaded = chain.length;\n const requests: DeepAgentInterestRequest[] = [\n { ns: DEEP_AGENT_MAIN_NS, before: Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, loaded) },\n ];\n // `low` is the chain index of the lowest message the plan covers so far.\n let low = loaded - (requests[0] as DeepAgentInterestRequest).before;\n while (low > 0) {\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, low + 1);\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before,\n at: (chain[low] as DeepAgentInterestChainEntry).id,\n });\n low = low + 1 - before;\n }\n // The plan covers the chain; the new messages extend its lowest request\n // when that stays within one window, else they are a page of their own.\n const last = requests[requests.length - 1] as DeepAgentInterestRequest;\n if (last.before + grow <= DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n requests[requests.length - 1] = { ...last, before: last.before + grow };\n } else {\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before: grow + 1,\n at: (chain[0] as DeepAgentInterestChainEntry).id,\n });\n }\n return requests.length > DEEP_AGENT_MAX_INTERESTS ? null : requests;\n}\n\n/**\n * The requests to state for one more page of older history, or `null` when\n * there is nothing to load (no anchor, the beginning reached) or the host's\n * interest cap is spent. One page is appended, anchored at the window's first\n * message, while the cap allows; at the cap the loaded range is coalesced\n * into maximal requests first (see {@link coalescedInterestRequests}).\n */\nexport function pageInterestRequests(\n window: DeepAgentMessageWindow,\n size?: number\n): DeepAgentInterestRequest[] | null {\n const anchor = window.chain[0]?.id;\n if (anchor === undefined || !window.hasOlder) return null;\n const page = pageInterestRequest(anchor, size);\n const appended = [...window.requests, page];\n if (appended.length <= DEEP_AGENT_MAX_INTERESTS) return appended;\n // A page's `before` counts its anchor: it adds `before - 1` older messages.\n return coalescedInterestRequests(window.chain, page.before - 1);\n}\n\n/**\n * The window off the mounted documents, or `null` while the head interest has\n * not resolved (pre-attach, before the first `set` answer, a wire without\n * documents). A page joins wherever its anchor sits in the chain so far — at\n * the first message it extends the view, inside the head (the head widened\n * past it) it adds nothing and is left out of `requests`; a page whose anchor\n * left the transcript (a rewind emptied it in place) is left out too. A\n * malformed document reads as absent.\n */\nexport function readDeepAgentMessageWindow(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined\n): DeepAgentMessageWindow | null {\n if (mounted === undefined) return null;\n let windows: DeepAgentInterestResolution[] | undefined;\n let threadId: string | undefined;\n const documents = new Map<string, LangChainMessage>();\n for (const document of mounted.values()) {\n const key = document.key;\n if (document.protocol === DEEP_AGENT_INTEREST_PROTOCOL && document.main === true) {\n windows = readDeepAgentInterestDocument(document.value)?.windows;\n } else if (\n document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL &&\n key !== null &&\n key[1] === DEEP_AGENT_MAIN_NS &&\n (key.length === 3 || (key.length === 4 && key[3] === DEEP_AGENT_CUT_LANE_DOCUMENT))\n ) {\n documents.set(key[2] as string, document.value as LangChainMessage);\n if (threadId === undefined && typeof key[0] === 'string') threadId = key[0];\n }\n }\n const head = windows?.[0];\n if (windows === undefined || head === undefined) return null;\n let chain = head.chain;\n let hasOlder = head.more.before;\n const requests = [interestRequest(head)];\n const loaded = new Set(chain.map((entry) => entry.id));\n let pages = 0;\n for (const page of windows.slice(1)) {\n if (page.at === undefined || !loaded.has(page.at)) continue;\n const anchorAt = page.chain.findIndex((entry) => entry.id === page.at);\n if (anchorAt < 0) continue;\n // Both windows are ranges of one linear transcript sharing the anchor:\n // what the page holds below it and the chain does not is a prefix.\n const fresh = page.chain.slice(0, anchorAt).filter((entry) => !loaded.has(entry.id));\n if (fresh.length === 0) {\n // Nothing new, but a page that starts where the loaded chain starts\n // has looked below it: its `more.before` is the answer for the edge.\n if (page.chain[0]?.id === chain[0]?.id) hasOlder = page.more.before;\n continue;\n }\n chain = [...fresh, ...chain];\n for (const entry of fresh) loaded.add(entry.id);\n hasOlder = page.more.before;\n requests.push(interestRequest(page));\n pages += 1;\n }\n const messages: LangChainMessage[] = [];\n for (const entry of chain) {\n const message = documents.get(entry.id);\n if (message !== undefined) messages.push(message);\n }\n return { requests, chain, messages, hasOlder, pages, ...(threadId !== undefined && { threadId }) };\n}\n\n/**\n * The view a client holds, as it declares it on a re-attach\n * (`x-athena-deep-agent-interests`): what the host would have remembered for\n * its client id, read off the attach's own interest document.\n */\nexport interface DeepAgentInterestDeclaration {\n /** Every request the interest document echoes that still covers something, the head first. */\n readonly requests: readonly DeepAgentInterestRequest[];\n /** The oldest message the head covers: the host holds the head down to it as the transcript grows. */\n readonly floor?: string;\n /** Per followed subagent lane, the oldest step a follow window covers. */\n readonly follows?: Readonly<Record<string, string>>;\n}\n\nconst echoedRequest = (window: Record<string, unknown>): DeepAgentInterestRequest | null => {\n const { ns, before, at, after, follow } = window;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n const followBefore =\n typeof follow === 'object' && follow !== null ? (follow as Record<string, unknown>).before : undefined;\n if (follow !== undefined && !isIndex(followBefore)) return null;\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n ...(isIndex(followBefore) && { follow: { before: followBefore } }),\n };\n};\n\nconst firstChainId = (chain: unknown): string | undefined => {\n if (!Array.isArray(chain)) return undefined;\n const id = (chain[0] as { id?: unknown } | undefined)?.id;\n return typeof id === 'string' && id ? id : undefined;\n};\n\n/**\n * The view to declare on a re-attach, read off the mounted interest document,\n * or `null` before one resolved. Requests are taken as the host echoed them\n * (lane `follow`s included); a page whose anchor left the transcript covers\n * nothing and is left out, the head never. The floors are what the host\n * commits from the same document (the head's first message, each lane's\n * deepest follow window's first step).\n */\nexport function readDeepAgentInterestDeclaration(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined\n): DeepAgentInterestDeclaration | null {\n if (mounted === undefined) return null;\n let windows: unknown[] | undefined;\n for (const document of mounted.values()) {\n if (document.protocol !== DEEP_AGENT_INTEREST_PROTOCOL || document.main !== true) continue;\n const value = document.value as { windows?: unknown } | null;\n if (value !== null && typeof value === 'object' && Array.isArray(value.windows)) {\n windows = value.windows;\n }\n }\n if (windows === undefined || windows.length === 0) return null;\n const requests: DeepAgentInterestRequest[] = [];\n const deepest = new Map<string, unknown[]>();\n for (const [index, window] of windows.entries()) {\n if (typeof window !== 'object' || window === null) return null;\n const record = window as Record<string, unknown>;\n const request = echoedRequest(record);\n if (request === null) return null;\n if (index === 0 && request.at !== undefined) return null;\n if (index === 0 || firstChainId(record.chain) !== undefined) requests.push(request);\n const follows = record.follows;\n if (typeof follows !== 'object' || follows === null) continue;\n for (const [ns, follow] of Object.entries(follows)) {\n const chain = (follow as { chain?: unknown } | null)?.chain;\n if (!Array.isArray(chain) || chain.length <= (deepest.get(ns)?.length ?? 0)) continue;\n deepest.set(ns, chain);\n }\n }\n const floor = firstChainId((windows[0] as Record<string, unknown>).chain);\n const follows: Record<string, string> = {};\n for (const [ns, chain] of deepest) {\n const step = firstChainId(chain);\n if (step !== undefined) follows[ns] = step;\n }\n return {\n requests,\n ...(floor !== undefined && { floor }),\n ...(Object.keys(follows).length > 0 && { follows }),\n };\n}\n\n/**\n * `declaration` as the header value: JSON with every non-ASCII character\n * escaped (a header value is Latin-1 on the wire), or `null` when it is\n * larger than the host reads — the attach then falls back to the host's memo\n * or the declared head, as before.\n */\nexport function encodeDeepAgentInterestDeclaration(\n declaration: DeepAgentInterestDeclaration\n): string | null {\n // Everything outside printable ASCII as a JSON escape (a surrogate pair as two).\n const encoded = JSON.stringify(declaration).replace(\n /[^ -~]/g,\n (char) => `\\\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`\n );\n return encoded.length > DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES ? null : encoded;\n}\n\n/**\n * Attach headers with the view a re-attach holds declared\n * (`x-athena-deep-agent-interests`), so its hello carries that view on any\n * host instance. A first attach declares nothing (`null`); an explicit entry\n * wins.\n */\nexport function withDeclaredInterests(\n headers: Record<string, string>,\n declaration: DeepAgentInterestDeclaration | null\n): Record<string, string> {\n if (declaration === null || DEEP_AGENT_MESSAGE_INTERESTS_HEADER in headers) return headers;\n const encoded = encodeDeepAgentInterestDeclaration(declaration);\n return encoded === null ? headers : { ...headers, [DEEP_AGENT_MESSAGE_INTERESTS_HEADER]: encoded };\n}\n\n/**\n * Whether `hello` (a re-attach's window) carries what `held` covered: it\n * reaches `held`'s oldest message, or there is nothing older to reach.\n */\nexport function messageWindowCarries({\n hello,\n held,\n}: {\n hello: DeepAgentMessageWindow;\n held: DeepAgentMessageWindow;\n}): boolean {\n const oldest = held.chain[0]?.id;\n if (oldest === undefined || !hello.hasOlder) return true;\n return hello.chain.some((entry) => entry.id === oldest);\n}\n\n/**\n * The statement that loads `held` again after a hello that did not carry it\n * (a host that neither read the declared view nor remembers the client).\n *\n * The requests it stood on, the head widened to reach the first page's anchor:\n * a head holding its floor covers more than it asked for once a run appended\n * past it, and `hello` (the re-attach's own window) shows what was appended\n * while the view was away, which the head must span too. Without pages, the\n * head is widened to everything it covered. Where that is more than one\n * request can state, the whole chain is restated in maximal windows, each\n * anchored inside the one above it (`coalescedInterestRequests`), so no page\n * is left without its anchor. A hello that does not reach the held view at\n * all (more was appended than it shows) gets the widest head one request can\n * state: the best an older host allows.\n */\nexport function restatedInterestRequests({\n held,\n hello = null,\n}: {\n held: DeepAgentMessageWindow;\n /** The re-attach's own window; omitted, nothing is known to have been appended. */\n hello?: DeepAgentMessageWindow | null;\n}): DeepAgentInterestRequest[] {\n const [head, ...pages] = held.requests;\n if (head === undefined) return [];\n const newest = held.chain.at(-1)?.id;\n const since =\n hello === null || newest === undefined\n ? -1\n : hello.chain.findIndex((entry) => entry.id === newest);\n const chain = since === -1 || hello === null ? held.chain : [...held.chain, ...hello.chain.slice(since + 1)];\n const anchor = pages[0]?.at;\n const start = anchor === undefined ? 0 : chain.findIndex((entry) => entry.id === anchor);\n const covered =\n hello !== null && since === -1\n ? DEEP_AGENT_MAX_MESSAGE_WINDOW\n : chain.length - Math.max(0, start);\n if (covered > DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n const coalesced = coalescedInterestRequests(chain, 0);\n if (coalesced !== null) return coalesced;\n }\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.max(head.before, covered));\n return [{ ...head, before }, ...pages];\n}\n\n/**\n * The windowed wire's read command on the statewire command union, so\n * `sendCommand({ type: 'history/tool_output', … })` typechecks wherever the\n * deep-agent host is driven (`DeepAgentThread.history_tool_output`, an\n * attach-ACL read).\n */\ndeclare module '@assistant-ui/core' {\n namespace Assistant {\n interface Commands {\n /** A tool output the windowed wire carried cut, whole. */\n historyToolOutput: ToolOutputCommand;\n /** One stored message whole — the per-client shape, by command. */\n historyMessage: HistoryMessageCommand;\n }\n }\n}\n\n/** The `history/tool_output` command: a truncated tool output, whole. */\nexport interface ToolOutputCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildToolOutputCommand({ messageId }: { messageId: string }): ToolOutputCommand {\n return { type: DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND, messageId };\n}\n\nexport interface ToolOutputResult {\n messageId: string;\n content: unknown;\n}\n\nexport function readToolOutputResult(payload: unknown): ToolOutputResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, content } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n return { messageId, content };\n}\n\n/**\n * The `history/message` command: a message as the host stores it — the\n * model-provenance blobs the wire strips and an untruncated tool output\n * included. Every client receives one projected wire form; this is the one\n * per-client shape, fetched on demand like `history/tool_output`.\n */\nexport interface HistoryMessageCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_MESSAGE_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildHistoryMessageCommand({\n messageId,\n}: {\n messageId: string;\n}): HistoryMessageCommand {\n return { type: DEEP_AGENT_HISTORY_MESSAGE_COMMAND, messageId };\n}\n\nexport interface HistoryMessageResult {\n messageId: string;\n message: LangChainMessage;\n}\n\nexport function readHistoryMessageResult(payload: unknown): HistoryMessageResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, message } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n if (typeof message !== 'object' || message === null) return null;\n return { messageId, message: message as LangChainMessage };\n}\n\n/** The wire marker on a tool message whose content the host cut. */\nexport interface TruncatedToolContent {\n /** Length of the full output, in characters. */\n chars: number;\n}\n\n/**\n * The truncation marker off a raw wire message, or `null` when the message's\n * content is whole. Pure and selector-safe on the marker object.\n */\nexport function readTruncatedToolContent(message: unknown): TruncatedToolContent | null {\n if (typeof message !== 'object' || message === null) return null;\n const marker = (message as Record<string, unknown>)[DEEP_AGENT_TRUNCATED_CONTENT_KEY];\n if (typeof marker !== 'object' || marker === null) return null;\n const chars = (marker as Record<string, unknown>).chars;\n return isIndex(chars) ? { chars } : null;\n}\n\n/**\n * `artifact` key the shared converter lifts a truncated tool output's marker\n * into (with the tool message id), so a tool card can ask for the full output\n * — the converter joins the ToolMessage into its tool-call part and would\n * otherwise drop the wire marker.\n */\nexport const ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY = '_athena_content_truncated';\n\nexport interface TruncatedToolOutputArtifact extends TruncatedToolContent {\n /** The tool message to name in `history/tool_output`. */\n messageId: string;\n}\n\n/**\n * The lifted truncation marker off a tool-call part's `artifact`, or `null`.\n */\nexport function readTruncatedToolOutputArtifact(\n artifact: unknown\n): TruncatedToolOutputArtifact | null {\n if (typeof artifact !== 'object' || artifact === null) return null;\n const lifted = (artifact as Record<string, unknown>)[ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY];\n if (typeof lifted !== 'object' || lifted === null) return null;\n const { chars, messageId } = lifted as Record<string, unknown>;\n if (!isIndex(chars) || typeof messageId !== 'string' || !messageId) return null;\n return { chars, messageId };\n}\n"],"names":["follows"],"mappings":";;AA8DA,MAAM,UAAU,CAAC,UACf,OAAO,UAAU,YAAY,OAAO,UAAU,KAAK,KAAK,SAAS;AAO5D,SAAS,uBAAuB,MAAuB;AAC5D,MAAI,SAAS,UAAa,CAAC,OAAO,SAAS,IAAI,EAAG,QAAO;AACzD,SAAO,KAAK;AAAA,IACV;AAAA,IACA,KAAK,IAAI,+BAA+B,KAAK,MAAM,IAAI,CAAC;AAAA,EAAA;AAE5D;AAeO,SAAS,oBAAoB,MAAyC;AAC3E,SAAO,EAAE,IAAI,oBAAoB,QAAQ,uBAAuB,IAAI,EAAA;AACtE;AAEO,SAAS,oBAAoB,IAAY,MAAyC;AACvF,SAAO,EAAE,IAAI,oBAAoB,QAAQ,uBAAuB,IAAI,GAAG,GAAA;AACzE;AAQO,SAAS,0BACd,SACA,cAAiD,IACzB;AACxB,MAAI,oCAAoC,QAAS,QAAO;AACxD,SAAO;AAAA,IACL,GAAG;AAAA,IACH,CAAC,gCAAgC,GAAG,OAAO,uBAAuB,YAAY,IAAI,CAAC;AAAA,EAAA;AAEvF;AAcO,SAAS,gCAAgC,OAAoD;AAClG,MAAI,OAAO,UAAU,YAAY,UAAU,KAAM,QAAO;AACxD,QAAM,EAAE,IAAI,QAAQ,IAAI,OAAO,OAAO,SAAS;AAC/C,MAAI,OAAO,sBAAsB,CAAC,QAAQ,MAAM,KAAK,CAAC,MAAM,QAAQ,KAAK,EAAG,QAAO;AACnF,MAAI,OAAO,UAAa,OAAO,OAAO,SAAU,QAAO;AACvD,MAAI,UAA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AAW,YAAY,WAAW,KAAM,QAAO;AAC1D,QAAM,EAAE,OAAO,UAAA,IAAc;AAC7B,MAAI,CAAC,QAAQ,KAAK,KAAK,OAAO,cAAc,YAAY,CAAC,UAAW,QAAO;AAC3E,SAAO,EAAE,OAAO,UAAA;AAClB;"}
|
|
1
|
+
{"version":3,"file":"message-window.js","sources":["../../../../agent-runtime/src/message-window.ts"],"sourcesContent":["import type { LangChainMessage } from '@assistant-ui/react-langgraph';\nimport type { StatewireDocuments } from 'statewire';\nimport {\n DEEP_AGENT_CUT_LANE_DOCUMENT,\n DEEP_AGENT_DEFAULT_MESSAGE_WINDOW,\n DEEP_AGENT_HISTORY_MESSAGE_COMMAND,\n DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND,\n DEEP_AGENT_INTEREST_PROTOCOL,\n DEEP_AGENT_MAIN_NS,\n DEEP_AGENT_MAX_INTERESTS,\n DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES,\n DEEP_AGENT_MAX_MESSAGE_WINDOW,\n DEEP_AGENT_MESSAGE_INTERESTS_HEADER,\n DEEP_AGENT_MESSAGE_WINDOW_HEADER,\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n DEEP_AGENT_TRUNCATED_CONTENT_KEY,\n} from './generated/runconfig-contract';\nimport {\n type DeepAgentHistoryStore,\n deepAgentHistoryStore,\n hostServesHistoryPage,\n} from './history-store';\nimport { readDeepAgentProtocol } from './protocol';\nimport { DEEP_AGENT_STATEWIRE_PROTOCOL } from './statewire-transport';\n\n/**\n * The interest window — the client half of agora's\n * `agora/graphs/athena_deep_agent/message_window.py`.\n *\n * The wire main document carries no `messages`. A client states its interests\n * on the `athena/interest` lane as one array (`set(requests)`): the first is\n * the head (the newest `before` messages of the `main` namespace), the rest\n * are pages anchored `at` the first message of the window they extend. The\n * host holds one ephemeral main document per attach, `{ windows }`, one\n * resolution per request in order (the request echoed plus the covered\n * `chain` and `more` flags; an anchor that vanished leaves an empty window in\n * place), and one shared `athena` document per covered message, keyed\n * `[threadId, \"main\", messageId]` — or `[threadId, \"main\", messageId, \"cut\"]`\n * for a finished card a version-2 view carries cut to its newest steps (its\n * whole lane pages in through `history/lane`, see `lane-history.ts`).\n * {@link readDeepAgentMessageWindow} joins them into the transcript the\n * converter renders.\n *\n * Two host rules shape the client half. The head window never drops below\n * the oldest message it has carried on an attach (a run's appends grow the\n * view, nothing loaded slides out), so a page stays anchored at the head's\n * first message while the transcript grows; and a re-attach's hello carries\n * the view the client held. A fresh client's hello carries the head it\n * declared on the attach headers ({@link withDeclaredMessageWindow}) — a\n * statement cannot ride the first attach, the hello negotiates the lanes. A\n * re-attach declares the view it holds there too\n * ({@link readDeepAgentInterestDeclaration}, {@link withDeclaredInterests}),\n * so its hello carries every page it had loaded on whichever host instance it\n * lands on: the host's own memo of a client id lives on one instance, and a\n * deploy moves the thread to another.\n *\n * On a host that serves `history/page` (its `protocol` lane advertises\n * `DEEP_AGENT_HISTORY_PAGE_PROTOCOL_VERSION`) older history is not an\n * interest at all: it is read by command into the conversation's history\n * store (`history-store.ts`), which {@link readDeepAgentMessageWindow} joins\n * under the live chain (`historyPaged`, `stored`). The interest document then\n * holds the head and lane follows alone, whatever the depth.\n *\n * On older hosts pages are appended one per load until the host's interest\n * cap, then the loaded range is restated in as few maximal requests as it\n * takes ({@link pageInterestRequests}), up to `MAX_INTERESTS ×\n * MAX_MESSAGE_WINDOW` messages. The interest document lists every loaded\n * message and the host keeps it within a byte budget, which binds first:\n * about a thousand messages, after which the host refuses a page as\n * `oversized-interest` (`useDeepAgentWindowSync` then reports `canLoadOlder`\n * false). A truncated tool output\n * comes back whole through `history/tool_output` ({@link buildToolOutputCommand});\n * a message's stored form — provenance blobs and all — through\n * `history/message` ({@link buildHistoryMessageCommand}).\n */\n\nconst isIndex = (value: unknown): value is number =>\n typeof value === 'number' && Number.isInteger(value) && value >= 0;\n\n/** What a surface declares for its window: the interest size (the host clamps to its bounds). */\nexport interface DeepAgentMessageWindowDeclaration {\n size?: number;\n}\n\nexport function clampMessageWindowSize(size?: number): number {\n if (size === undefined || !Number.isFinite(size)) return DEEP_AGENT_DEFAULT_MESSAGE_WINDOW;\n return Math.max(\n DEEP_AGENT_MIN_MESSAGE_WINDOW,\n Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.trunc(size))\n );\n}\n\n/** An `athena/interest` request over the linear transcript. */\nexport interface DeepAgentInterestRequest {\n readonly ns: typeof DEEP_AGENT_MAIN_NS;\n /** Messages ending at the anchor (the tail without `at`). */\n readonly before: number;\n /** Anchor message id; a page's is the first message of the window it extends. */\n readonly at?: string;\n /** Messages past the anchor (requires `at`). */\n readonly after?: number;\n /** The newest steps of every subagent lane a covered card names (see `lane-follow.ts`). */\n readonly follow?: { readonly before: number };\n}\n\nexport function headInterestRequest(size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size) };\n}\n\nexport function pageInterestRequest(at: string, size?: number): DeepAgentInterestRequest {\n return { ns: DEEP_AGENT_MAIN_NS, before: clampMessageWindowSize(size), at };\n}\n\n/**\n * Attach headers with the head window declared (`x-athena-deep-agent-window`):\n * the hello of a client the host does not remember carries the newest `size`\n * messages. Part of the one attach recipe, so a pooled dial asks for the shape\n * the thread's own attach would. An explicit entry (tests, overrides) wins.\n */\nexport function withDeclaredMessageWindow(\n headers: Record<string, string>,\n declaration: DeepAgentMessageWindowDeclaration = {}\n): Record<string, string> {\n if (DEEP_AGENT_MESSAGE_WINDOW_HEADER in headers) return headers;\n return {\n ...headers,\n [DEEP_AGENT_MESSAGE_WINDOW_HEADER]: String(clampMessageWindowSize(declaration.size)),\n };\n}\n\nexport interface DeepAgentInterestChainEntry {\n id: string;\n siblings: string[];\n}\n\n/** The host's resolution document of one interest: the request echoed plus its coverage. */\nexport interface DeepAgentInterestResolution extends DeepAgentInterestRequest {\n /** Covered message ids, oldest first (a page ends at its anchor). */\n chain: DeepAgentInterestChainEntry[];\n more: { before: boolean; after: boolean };\n}\n\nexport function readDeepAgentInterestResolution(value: unknown): DeepAgentInterestResolution | null {\n if (typeof value !== 'object' || value === null) return null;\n const { ns, before, at, after, chain, more } = value as Record<string, unknown>;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before) || !Array.isArray(chain)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n if (typeof more !== 'object' || more === null) return null;\n const { before: moreBefore, after: moreAfter } = more as Record<string, unknown>;\n if (typeof moreBefore !== 'boolean' || typeof moreAfter !== 'boolean') return null;\n const entries: DeepAgentInterestChainEntry[] = [];\n for (const entry of chain) {\n const { id, siblings } = (entry ?? {}) as Record<string, unknown>;\n if (typeof id !== 'string') return null;\n entries.push({\n id,\n siblings: Array.isArray(siblings) ? siblings.filter((s) => typeof s === 'string') : [id],\n });\n }\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n chain: entries,\n more: { before: moreBefore, after: moreAfter },\n };\n}\n\n/** The attach's interest document: one resolution per stated request, in order. */\nexport interface DeepAgentInterestDocument {\n windows: DeepAgentInterestResolution[];\n}\n\nexport function readDeepAgentInterestDocument(value: unknown): DeepAgentInterestDocument | null {\n if (typeof value !== 'object' || value === null) return null;\n const { windows } = value as Record<string, unknown>;\n if (!Array.isArray(windows)) return null;\n const resolutions: DeepAgentInterestResolution[] = [];\n for (const window of windows) {\n const resolution = readDeepAgentInterestResolution(window);\n if (resolution === null) return null;\n resolutions.push(resolution);\n }\n return { windows: resolutions };\n}\n\nconst interestRequest = ({ ns, before, at, after }: DeepAgentInterestRequest): DeepAgentInterestRequest => ({\n ns,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n});\n\n/** The loaded transcript window, joined from the head interest and its anchored pages. */\nexport interface DeepAgentMessageWindow {\n /** The requests the joined view stands on, in order (the head first): what a `set` restates. A stale page (its anchor gone) or one the head already covers is left out. */\n requests: DeepAgentInterestRequest[];\n /** Covered chain, oldest first. */\n chain: DeepAgentInterestChainEntry[];\n /** The chain's message documents, in chain order (an unmounted one is skipped). */\n messages: LangChainMessage[];\n /** Whether the host holds messages older than the window. */\n hasOlder: boolean;\n /** Pages joined onto the head. */\n pages: number;\n /** The conversation the window's message documents belong to (their key's thread); absent before one is mounted. */\n threadId?: string;\n /**\n * Of `chain`, how many oldest entries come from the conversation's history\n * store (`history/page`, see `history-store.ts`) rather than the attach's\n * live view; absent when none do (always, on a host that pages history by\n * interest).\n */\n stored?: number;\n /**\n * Set when older history pages by `history/page` into the conversation's\n * store (the host advertises it and has not refused it): what loads the\n * next page. Absent, pages are interests ({@link pageInterestRequests}).\n */\n historyPaged?: true;\n}\n\n/** How {@link readDeepAgentMessageWindow} reads the conversation's history store. */\nexport interface ReadDeepAgentMessageWindowOptions {\n /**\n * The store to record into and join from; `null` reads the live view alone.\n * Default: the conversation's store in the shared registry.\n */\n history?: DeepAgentHistoryStore | null;\n}\n\n/**\n * The loaded chain plus `grow` older messages, restated in as few requests as\n * it takes: a maximal head, then maximal pages each anchored at the first\n * message of the window above it. `null` once even that exceeds the host's\n * interest cap — the reachable depth is `MAX_INTERESTS × MAX_MESSAGE_WINDOW`.\n */\nfunction coalescedInterestRequests(\n chain: readonly DeepAgentInterestChainEntry[],\n grow: number\n): DeepAgentInterestRequest[] | null {\n const loaded = chain.length;\n const requests: DeepAgentInterestRequest[] = [\n { ns: DEEP_AGENT_MAIN_NS, before: Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, loaded) },\n ];\n // `low` is the chain index of the lowest message the plan covers so far.\n let low = loaded - (requests[0] as DeepAgentInterestRequest).before;\n while (low > 0) {\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, low + 1);\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before,\n at: (chain[low] as DeepAgentInterestChainEntry).id,\n });\n low = low + 1 - before;\n }\n // The plan covers the chain; the new messages extend its lowest request\n // when that stays within one window, else they are a page of their own.\n const last = requests[requests.length - 1] as DeepAgentInterestRequest;\n if (last.before + grow <= DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n requests[requests.length - 1] = { ...last, before: last.before + grow };\n } else {\n requests.push({\n ns: DEEP_AGENT_MAIN_NS,\n before: grow + 1,\n at: (chain[0] as DeepAgentInterestChainEntry).id,\n });\n }\n return requests.length > DEEP_AGENT_MAX_INTERESTS ? null : requests;\n}\n\n/**\n * The requests to state for one more page of older history, or `null` when\n * there is nothing to load (no anchor, the beginning reached) or the host's\n * interest cap is spent. One page is appended, anchored at the window's first\n * message, while the cap allows; at the cap the loaded range is coalesced\n * into maximal requests first (see {@link coalescedInterestRequests}).\n */\nexport function pageInterestRequests(\n window: DeepAgentMessageWindow,\n size?: number\n): DeepAgentInterestRequest[] | null {\n const anchor = window.chain[0]?.id;\n if (anchor === undefined || !window.hasOlder) return null;\n const page = pageInterestRequest(anchor, size);\n const appended = [...window.requests, page];\n if (appended.length <= DEEP_AGENT_MAX_INTERESTS) return appended;\n // A page's `before` counts its anchor: it adds `before - 1` older messages.\n return coalescedInterestRequests(window.chain, page.before - 1);\n}\n\n/**\n * The window off the mounted documents, or `null` while the head interest has\n * not resolved (pre-attach, before the first `set` answer, a wire without\n * documents). It records the parents its live chain shows into the\n * conversation's history store and, on a host that serves `history/page`,\n * joins what the store holds older than the chain's first message. A page joins wherever its anchor sits in the chain so far — at\n * the first message it extends the view, inside the head (the head widened\n * past it) it adds nothing and is left out of `requests`; a page whose anchor\n * left the transcript (a rewind emptied it in place) is left out too. A\n * malformed document reads as absent.\n */\nexport function readDeepAgentMessageWindow(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined,\n options: ReadDeepAgentMessageWindowOptions = {}\n): DeepAgentMessageWindow | null {\n if (mounted === undefined) return null;\n let windows: DeepAgentInterestResolution[] | undefined;\n let threadId: string | undefined;\n let hostVersion: number | undefined;\n const documents = new Map<string, LangChainMessage>();\n for (const document of mounted.values()) {\n const key = document.key;\n if (document.protocol === DEEP_AGENT_INTEREST_PROTOCOL && document.main === true) {\n windows = readDeepAgentInterestDocument(document.value)?.windows;\n } else if (document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL && document.main === true) {\n hostVersion = readDeepAgentProtocol(document.value)?.version;\n } else if (\n document.protocol === DEEP_AGENT_STATEWIRE_PROTOCOL &&\n key !== null &&\n key[1] === DEEP_AGENT_MAIN_NS &&\n (key.length === 3 || (key.length === 4 && key[3] === DEEP_AGENT_CUT_LANE_DOCUMENT))\n ) {\n documents.set(key[2] as string, document.value as LangChainMessage);\n if (threadId === undefined && typeof key[0] === 'string') threadId = key[0];\n }\n }\n const head = windows?.[0];\n if (windows === undefined || head === undefined) return null;\n let chain = head.chain;\n let hasOlder = head.more.before;\n const requests = [interestRequest(head)];\n const loaded = new Set(chain.map((entry) => entry.id));\n let pages = 0;\n for (const page of windows.slice(1)) {\n if (page.at === undefined || !loaded.has(page.at)) continue;\n const anchorAt = page.chain.findIndex((entry) => entry.id === page.at);\n if (anchorAt < 0) continue;\n // Both windows are ranges of one linear transcript sharing the anchor:\n // what the page holds below it and the chain does not is a prefix.\n const fresh = page.chain.slice(0, anchorAt).filter((entry) => !loaded.has(entry.id));\n if (fresh.length === 0) {\n // Nothing new, but a page that starts where the loaded chain starts\n // has looked below it: its `more.before` is the answer for the edge.\n if (page.chain[0]?.id === chain[0]?.id) hasOlder = page.more.before;\n continue;\n }\n chain = [...fresh, ...chain];\n for (const entry of fresh) loaded.add(entry.id);\n hasOlder = page.more.before;\n requests.push(interestRequest(page));\n pages += 1;\n }\n let messages: LangChainMessage[] = [];\n for (const entry of chain) {\n const message = documents.get(entry.id);\n if (message !== undefined) messages.push(message);\n }\n let stored = 0;\n let historyPaged = false;\n const history =\n options.history !== undefined\n ? options.history\n : threadId !== undefined\n ? deepAgentHistoryStore(threadId)\n : null;\n const first = chain[0]?.id;\n if (history !== null && first !== undefined) {\n history.recordChain(\n chain.map((entry) => entry.id),\n documents,\n !hasOlder\n );\n if (hostServesHistoryPage(hostVersion) && !history.unserved) {\n historyPaged = true;\n // Older history joins under the live chain by its parents, so only\n // true ancestors of what the host shows now are ever read in.\n const older = history.olderThan(first);\n if (older.ids.length > 0) {\n stored = older.ids.length;\n chain = [...older.ids.map((id) => ({ id, siblings: [id] })), ...chain];\n messages = [...older.messages, ...messages];\n hasOlder = older.hasOlder;\n } else if (history.parentOf(first) === null) {\n hasOlder = false;\n }\n // A summarized turn's first reply carries its exact counts, wherever\n // it sits (a turn the page cut short resumes in the live chain).\n if (threadId !== undefined) messages = history.markTurns(threadId, messages);\n }\n }\n return {\n requests,\n chain,\n messages,\n hasOlder,\n pages,\n ...(stored > 0 && { stored }),\n ...(historyPaged && { historyPaged: true as const }),\n ...(threadId !== undefined && { threadId }),\n };\n}\n\n/**\n * The view a client holds, as it declares it on a re-attach\n * (`x-athena-deep-agent-interests`): what the host would have remembered for\n * its client id, read off the attach's own interest document.\n */\nexport interface DeepAgentInterestDeclaration {\n /** Every request the interest document echoes that still covers something, the head first. */\n readonly requests: readonly DeepAgentInterestRequest[];\n /** The oldest message the head covers: the host holds the head down to it as the transcript grows. */\n readonly floor?: string;\n /** Per followed subagent lane, the oldest step a follow window covers. */\n readonly follows?: Readonly<Record<string, string>>;\n}\n\nconst echoedRequest = (window: Record<string, unknown>): DeepAgentInterestRequest | null => {\n const { ns, before, at, after, follow } = window;\n if (ns !== DEEP_AGENT_MAIN_NS || !isIndex(before)) return null;\n if (at !== undefined && typeof at !== 'string') return null;\n if (after !== undefined && !isIndex(after)) return null;\n const followBefore =\n typeof follow === 'object' && follow !== null ? (follow as Record<string, unknown>).before : undefined;\n if (follow !== undefined && !isIndex(followBefore)) return null;\n return {\n ns: DEEP_AGENT_MAIN_NS,\n before,\n ...(at !== undefined && { at }),\n ...(after !== undefined && { after }),\n ...(isIndex(followBefore) && { follow: { before: followBefore } }),\n };\n};\n\nconst firstChainId = (chain: unknown): string | undefined => {\n if (!Array.isArray(chain)) return undefined;\n const id = (chain[0] as { id?: unknown } | undefined)?.id;\n return typeof id === 'string' && id ? id : undefined;\n};\n\n/**\n * The view to declare on a re-attach, read off the mounted interest document,\n * or `null` before one resolved. Requests are taken as the host echoed them\n * (lane `follow`s included); a page whose anchor left the transcript covers\n * nothing and is left out, the head never. The floors are what the host\n * commits from the same document (the head's first message, each lane's\n * deepest follow window's first step).\n */\nexport function readDeepAgentInterestDeclaration(\n mounted: ReadonlyMap<number, StatewireDocuments.Document> | undefined\n): DeepAgentInterestDeclaration | null {\n if (mounted === undefined) return null;\n let windows: unknown[] | undefined;\n for (const document of mounted.values()) {\n if (document.protocol !== DEEP_AGENT_INTEREST_PROTOCOL || document.main !== true) continue;\n const value = document.value as { windows?: unknown } | null;\n if (value !== null && typeof value === 'object' && Array.isArray(value.windows)) {\n windows = value.windows;\n }\n }\n if (windows === undefined || windows.length === 0) return null;\n const requests: DeepAgentInterestRequest[] = [];\n const deepest = new Map<string, unknown[]>();\n for (const [index, window] of windows.entries()) {\n if (typeof window !== 'object' || window === null) return null;\n const record = window as Record<string, unknown>;\n const request = echoedRequest(record);\n if (request === null) return null;\n if (index === 0 && request.at !== undefined) return null;\n if (index === 0 || firstChainId(record.chain) !== undefined) requests.push(request);\n const follows = record.follows;\n if (typeof follows !== 'object' || follows === null) continue;\n for (const [ns, follow] of Object.entries(follows)) {\n const chain = (follow as { chain?: unknown } | null)?.chain;\n if (!Array.isArray(chain) || chain.length <= (deepest.get(ns)?.length ?? 0)) continue;\n deepest.set(ns, chain);\n }\n }\n const floor = firstChainId((windows[0] as Record<string, unknown>).chain);\n const follows: Record<string, string> = {};\n for (const [ns, chain] of deepest) {\n const step = firstChainId(chain);\n if (step !== undefined) follows[ns] = step;\n }\n return {\n requests,\n ...(floor !== undefined && { floor }),\n ...(Object.keys(follows).length > 0 && { follows }),\n };\n}\n\n/**\n * `declaration` as the header value: JSON with every non-ASCII character\n * escaped (a header value is Latin-1 on the wire), or `null` when it is\n * larger than the host reads — the attach then falls back to the host's memo\n * or the declared head, as before.\n */\nexport function encodeDeepAgentInterestDeclaration(\n declaration: DeepAgentInterestDeclaration\n): string | null {\n // Everything outside printable ASCII as a JSON escape (a surrogate pair as two).\n const encoded = JSON.stringify(declaration).replace(\n /[^ -~]/g,\n (char) => `\\\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`\n );\n return encoded.length > DEEP_AGENT_MAX_MESSAGE_INTERESTS_HEADER_BYTES ? null : encoded;\n}\n\n/**\n * Attach headers with the view a re-attach holds declared\n * (`x-athena-deep-agent-interests`), so its hello carries that view on any\n * host instance. A first attach declares nothing (`null`); an explicit entry\n * wins.\n */\nexport function withDeclaredInterests(\n headers: Record<string, string>,\n declaration: DeepAgentInterestDeclaration | null\n): Record<string, string> {\n if (declaration === null || DEEP_AGENT_MESSAGE_INTERESTS_HEADER in headers) return headers;\n const encoded = encodeDeepAgentInterestDeclaration(declaration);\n return encoded === null ? headers : { ...headers, [DEEP_AGENT_MESSAGE_INTERESTS_HEADER]: encoded };\n}\n\n/**\n * Whether `hello` (a re-attach's window) carries what `held` covered: it\n * reaches `held`'s oldest message, or there is nothing older to reach.\n */\nexport function messageWindowCarries({\n hello,\n held,\n}: {\n hello: DeepAgentMessageWindow;\n held: DeepAgentMessageWindow;\n}): boolean {\n const oldest = held.chain[0]?.id;\n if (oldest === undefined || !hello.hasOlder) return true;\n return hello.chain.some((entry) => entry.id === oldest);\n}\n\n/**\n * The statement that loads `held` again after a hello that did not carry it\n * (a host that neither read the declared view nor remembers the client).\n *\n * The requests it stood on, the head widened to reach the first page's anchor:\n * a head holding its floor covers more than it asked for once a run appended\n * past it, and `hello` (the re-attach's own window) shows what was appended\n * while the view was away, which the head must span too. Without pages, the\n * head is widened to everything it covered. Where that is more than one\n * request can state, the whole chain is restated in maximal windows, each\n * anchored inside the one above it (`coalescedInterestRequests`), so no page\n * is left without its anchor. A hello that does not reach the held view at\n * all (more was appended than it shows) gets the widest head one request can\n * state: the best an older host allows.\n */\nexport function restatedInterestRequests({\n held,\n hello = null,\n}: {\n held: DeepAgentMessageWindow;\n /** The re-attach's own window; omitted, nothing is known to have been appended. */\n hello?: DeepAgentMessageWindow | null;\n}): DeepAgentInterestRequest[] {\n const [head, ...pages] = held.requests;\n if (head === undefined) return [];\n const newest = held.chain.at(-1)?.id;\n const since =\n hello === null || newest === undefined\n ? -1\n : hello.chain.findIndex((entry) => entry.id === newest);\n const chain = since === -1 || hello === null ? held.chain : [...held.chain, ...hello.chain.slice(since + 1)];\n const anchor = pages[0]?.at;\n const start = anchor === undefined ? 0 : chain.findIndex((entry) => entry.id === anchor);\n const covered =\n hello !== null && since === -1\n ? DEEP_AGENT_MAX_MESSAGE_WINDOW\n : chain.length - Math.max(0, start);\n if (covered > DEEP_AGENT_MAX_MESSAGE_WINDOW) {\n const coalesced = coalescedInterestRequests(chain, 0);\n if (coalesced !== null) return coalesced;\n }\n const before = Math.min(DEEP_AGENT_MAX_MESSAGE_WINDOW, Math.max(head.before, covered));\n return [{ ...head, before }, ...pages];\n}\n\n/**\n * The windowed wire's read command on the statewire command union, so\n * `sendCommand({ type: 'history/tool_output', … })` typechecks wherever the\n * deep-agent host is driven (`DeepAgentThread.history_tool_output`, an\n * attach-ACL read).\n */\ndeclare module '@assistant-ui/core' {\n namespace Assistant {\n interface Commands {\n /** A tool output the windowed wire carried cut, whole. */\n historyToolOutput: ToolOutputCommand;\n /** One stored message whole — the per-client shape, by command. */\n historyMessage: HistoryMessageCommand;\n }\n }\n}\n\n/** The `history/tool_output` command: a truncated tool output, whole. */\nexport interface ToolOutputCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildToolOutputCommand({ messageId }: { messageId: string }): ToolOutputCommand {\n return { type: DEEP_AGENT_HISTORY_TOOL_OUTPUT_COMMAND, messageId };\n}\n\nexport interface ToolOutputResult {\n messageId: string;\n content: unknown;\n}\n\nexport function readToolOutputResult(payload: unknown): ToolOutputResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, content } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n return { messageId, content };\n}\n\n/**\n * The `history/message` command: a message as the host stores it — the\n * model-provenance blobs the wire strips and an untruncated tool output\n * included. Every client receives one projected wire form; this is the one\n * per-client shape, fetched on demand like `history/tool_output`.\n */\nexport interface HistoryMessageCommand {\n readonly type: typeof DEEP_AGENT_HISTORY_MESSAGE_COMMAND;\n readonly messageId: string;\n}\n\nexport function buildHistoryMessageCommand({\n messageId,\n}: {\n messageId: string;\n}): HistoryMessageCommand {\n return { type: DEEP_AGENT_HISTORY_MESSAGE_COMMAND, messageId };\n}\n\nexport interface HistoryMessageResult {\n messageId: string;\n message: LangChainMessage;\n}\n\nexport function readHistoryMessageResult(payload: unknown): HistoryMessageResult | null {\n if (typeof payload !== 'object' || payload === null) return null;\n const { messageId, message } = payload as Record<string, unknown>;\n if (typeof messageId !== 'string' || !messageId) return null;\n if (typeof message !== 'object' || message === null) return null;\n return { messageId, message: message as LangChainMessage };\n}\n\n/** The wire marker on a tool message whose content the host cut. */\nexport interface TruncatedToolContent {\n /** Length of the full output, in characters. */\n chars: number;\n}\n\n/**\n * The truncation marker off a raw wire message, or `null` when the message's\n * content is whole. Pure and selector-safe on the marker object.\n */\nexport function readTruncatedToolContent(message: unknown): TruncatedToolContent | null {\n if (typeof message !== 'object' || message === null) return null;\n const marker = (message as Record<string, unknown>)[DEEP_AGENT_TRUNCATED_CONTENT_KEY];\n if (typeof marker !== 'object' || marker === null) return null;\n const chars = (marker as Record<string, unknown>).chars;\n return isIndex(chars) ? { chars } : null;\n}\n\n/**\n * `artifact` key the shared converter lifts a truncated tool output's marker\n * into (with the tool message id), so a tool card can ask for the full output\n * — the converter joins the ToolMessage into its tool-call part and would\n * otherwise drop the wire marker.\n */\nexport const ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY = '_athena_content_truncated';\n\nexport interface TruncatedToolOutputArtifact extends TruncatedToolContent {\n /** The tool message to name in `history/tool_output`. */\n messageId: string;\n}\n\n/**\n * The lifted truncation marker off a tool-call part's `artifact`, or `null`.\n */\nexport function readTruncatedToolOutputArtifact(\n artifact: unknown\n): TruncatedToolOutputArtifact | null {\n if (typeof artifact !== 'object' || artifact === null) return null;\n const lifted = (artifact as Record<string, unknown>)[ATHENA_TRUNCATED_TOOL_OUTPUT_ARTIFACT_KEY];\n if (typeof lifted !== 'object' || lifted === null) return null;\n const { chars, messageId } = lifted as Record<string, unknown>;\n if (!isIndex(chars) || typeof messageId !== 'string' || !messageId) return null;\n return { chars, messageId };\n}\n"],"names":["follows"],"mappings":";;;;AA4EA,MAAM,UAAU,CAAC,UACf,OAAO,UAAU,YAAY,OAAO,UAAU,KAAK,KAAK,SAAS;AAO5D,SAAS,uBAAuB,MAAuB;AAC5D,MAAI,SAAS,UAAa,CAAC,OAAO,SAAS,IAAI,EAAG,QAAO;AACzD,SAAO,KAAK;AAAA,IACV;AAAA,IACA,KAAK,IAAI,+BAA+B,KAAK,MAAM,IAAI,CAAC;AAAA,EAAA;AAE5D;AAeO,SAAS,oBAAoB,MAAyC;AAC3E,SAAO,EAAE,IAAI,oBAAoB,QAAQ,uBAAuB,IAAI,EAAA;AACtE;AAEO,SAAS,oBAAoB,IAAY,MAAyC;AACvF,SAAO,EAAE,IAAI,oBAAoB,QAAQ,uBAAuB,IAAI,GAAG,GAAA;AACzE;AAQO,SAAS,0BACd,SACA,cAAiD,IACzB;AACxB,MAAI,oCAAoC,QAAS,QAAO;AACxD,SAAO;AAAA,IACL,GAAG;AAAA,IACH,CAAC,gCAAgC,GAAG,OAAO,uBAAuB,YAAY,IAAI,CAAC;AAAA,EAAA;AAEvF;AAcO,SAAS,gCAAgC,OAAoD;AAClG,MAAI,OAAO,UAAU,YAAY,UAAU,KAAM,QAAO;AACxD,QAAM,EAAE,IAAI,QAAQ,IAAI,OAAO,OAAO,SAAS;AAC/C,MAAI,OAAO,sBAAsB,CAAC,QAAQ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