@assistant-ui/react-ag-ui 0.0.54 → 0.0.56

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.
Files changed (45) hide show
  1. package/dist/index.d.ts +2 -2
  2. package/dist/index.js +2 -2
  3. package/dist/runtime/AgUiThreadRuntimeCore.d.ts +23 -2
  4. package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
  5. package/dist/runtime/AgUiThreadRuntimeCore.js +169 -35
  6. package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
  7. package/dist/runtime/adapter/conversions.d.ts +13 -3
  8. package/dist/runtime/adapter/conversions.d.ts.map +1 -1
  9. package/dist/runtime/adapter/conversions.js +58 -26
  10. package/dist/runtime/adapter/conversions.js.map +1 -1
  11. package/dist/runtime/adapter/run-aggregator.d.ts +5 -1
  12. package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
  13. package/dist/runtime/adapter/run-aggregator.js +26 -1
  14. package/dist/runtime/adapter/run-aggregator.js.map +1 -1
  15. package/dist/runtime/adapter/subscriber.d.ts +3 -0
  16. package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
  17. package/dist/runtime/adapter/subscriber.js +9 -0
  18. package/dist/runtime/adapter/subscriber.js.map +1 -1
  19. package/dist/runtime/adapter/tool-approval.d.ts +56 -0
  20. package/dist/runtime/adapter/tool-approval.d.ts.map +1 -0
  21. package/dist/runtime/adapter/tool-approval.js +244 -0
  22. package/dist/runtime/adapter/tool-approval.js.map +1 -0
  23. package/dist/runtime/event-parser.d.ts.map +1 -1
  24. package/dist/runtime/event-parser.js +2 -1
  25. package/dist/runtime/event-parser.js.map +1 -1
  26. package/dist/runtime/interrupt-internals.d.ts +8 -0
  27. package/dist/runtime/interrupt-internals.d.ts.map +1 -0
  28. package/dist/runtime/interrupt-internals.js +25 -0
  29. package/dist/runtime/interrupt-internals.js.map +1 -0
  30. package/dist/useAgUiRuntime.js +3 -0
  31. package/dist/useAgUiRuntime.js.map +1 -1
  32. package/package.json +7 -7
  33. package/src/index.ts +8 -2
  34. package/src/runtime/AgUiThreadRuntimeCore.ts +314 -41
  35. package/src/runtime/adapter/conversions.content.test.ts +61 -3
  36. package/src/runtime/adapter/conversions.ts +83 -39
  37. package/src/runtime/adapter/run-aggregator.approval.test.ts +103 -0
  38. package/src/runtime/adapter/run-aggregator.ts +37 -1
  39. package/src/runtime/adapter/subscriber.ts +14 -0
  40. package/src/runtime/adapter/tool-approval.test.ts +681 -0
  41. package/src/runtime/adapter/tool-approval.ts +366 -0
  42. package/src/runtime/event-parser.ts +10 -1
  43. package/src/runtime/interrupt-internals.ts +35 -0
  44. package/src/useAgUiRuntime.approval.test.tsx +885 -0
  45. package/src/useAgUiRuntime.ts +4 -0
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
  import { parseMcpToolCallResult, readMcpAppResourceUri } from "../mcp-tool-result.js";
3
+ import { projectAgUiToolApprovals } from "./tool-approval.js";
3
4
  import { AG_UI_METADATA_NAMESPACE } from "./run-aggregator.js";
4
5
  import { getAutoStatus, httpUrlPattern, parseDataUrl } from "@assistant-ui/core/internal";
5
6
  import { applyA2uiOperations, convertSurfaceToUISpec } from "@assistant-ui/react-generative-ui/a2ui";
@@ -375,13 +376,21 @@ function withOpaqueReasoning(message, opaqueReasoning) {
375
376
  }
376
377
  function toAssistantSnapshotMessage(rawMessage) {
377
378
  const text = extractText(rawMessage.content);
378
- const toolCallParts = extractAssistantToolCalls(rawMessage);
379
+ const interrupts = readPersistedInterrupts(rawMessage.metadata);
380
+ const restoredToolCalls = extractAssistantToolCalls(rawMessage);
381
+ const approvals = projectAgUiToolApprovals(interrupts, new Set(restoredToolCalls.map((part) => part.toolCallId).filter((id) => !!id)));
382
+ const toolCallParts = restoredToolCalls.map((part) => {
383
+ const approval = part.toolCallId ? approvals.get(part.toolCallId) : void 0;
384
+ return approval ? {
385
+ ...part,
386
+ approval
387
+ } : part;
388
+ });
379
389
  const assistantContent = [...text.length > 0 ? [{
380
390
  type: "text",
381
391
  text
382
392
  }] : [], ...toolCallParts];
383
393
  const messageName = getString(rawMessage, "name");
384
- const interrupts = readPersistedInterrupts(rawMessage.metadata);
385
394
  return {
386
395
  id: getString(rawMessage, "id") ?? generateId(),
387
396
  role: "assistant",
@@ -601,42 +610,43 @@ function convertAssistantMessage(message, converted) {
601
610
  reasoningIndex++;
602
611
  }
603
612
  if (shellIsDropped) return;
604
- const assistantMessage = {
613
+ converted.push({
605
614
  id: message.id,
606
615
  role: "assistant",
607
- content
608
- };
609
- if (message.name) assistantMessage.name = message.name;
610
- if (toolCalls.length > 0) assistantMessage.toolCalls = toolCalls.map((entry) => entry.call);
611
- converted.push(assistantMessage);
616
+ content,
617
+ ...message.name ? { name: message.name } : {},
618
+ ...toolCalls.length > 0 ? { toolCalls: toolCalls.map((entry) => entry.call) } : {}
619
+ });
612
620
  for (const { id: toolCallId, part } of toolCalls) {
613
621
  if (part.result === void 0) continue;
614
622
  const resultContent = part.modelContent !== void 0 ? extractText(part.modelContent) : typeof part.result === "string" ? part.result : JSON.stringify(part.result);
615
- const toolMessage = {
623
+ converted.push({
616
624
  id: part.unstable_toolMessageId ?? `${toolCallId}:tool`,
617
625
  role: "tool",
618
626
  content: resultContent,
619
- toolCallId
620
- };
621
- if (part.isError) toolMessage.error = resultContent;
622
- converted.push(toolMessage);
627
+ toolCallId,
628
+ ...part.isError ? { error: resultContent } : {}
629
+ });
623
630
  }
624
631
  }
625
632
  function convertToolMessage(message, converted) {
626
633
  const content = extractText(message.content);
627
634
  const toolCallId = message.toolCallId ?? generateId();
628
- const toolMessage = {
635
+ converted.push({
629
636
  id: message.id,
630
637
  role: "tool",
631
638
  content,
632
- toolCallId
633
- };
634
- if (typeof message.error === "string") toolMessage.error = message.error;
635
- converted.push(toolMessage);
639
+ toolCallId,
640
+ ...typeof message.error === "string" ? { error: message.error } : {}
641
+ });
636
642
  }
637
643
  function toAgUiMessages(messages) {
638
644
  const converted = [];
639
- for (const message of messages) {
645
+ for (const rawMessage of messages) {
646
+ const message = {
647
+ ...rawMessage,
648
+ id: rawMessage.id ?? generateId()
649
+ };
640
650
  const opaqueReasoning = readOpaqueReasoning(message.metadata);
641
651
  const toOpaqueRecord = (entry) => ({
642
652
  id: entry.id,
@@ -658,13 +668,35 @@ function toAgUiMessages(messages) {
658
668
  flushTrailingOpaqueReasoning();
659
669
  continue;
660
670
  }
661
- const genericMessage = {
662
- id: message.id,
663
- role: message.role,
664
- content: message.role === "user" ? buildUserContent(message) : extractText(message.content)
665
- };
666
- if (message.name) genericMessage.name = message.name;
667
- converted.push(genericMessage);
671
+ if (message.role === "user") {
672
+ converted.push({
673
+ id: message.id,
674
+ role: "user",
675
+ content: buildUserContent(message),
676
+ ...message.name ? { name: message.name } : {}
677
+ });
678
+ flushTrailingOpaqueReasoning();
679
+ continue;
680
+ }
681
+ if (message.role === "system" || message.role === "developer") {
682
+ converted.push({
683
+ id: message.id,
684
+ role: message.role,
685
+ content: extractText(message.content),
686
+ ...message.name ? { name: message.name } : {}
687
+ });
688
+ flushTrailingOpaqueReasoning();
689
+ continue;
690
+ }
691
+ if (message.role === "reasoning") {
692
+ converted.push({
693
+ id: message.id,
694
+ role: "reasoning",
695
+ content: extractText(message.content)
696
+ });
697
+ flushTrailingOpaqueReasoning();
698
+ continue;
699
+ }
668
700
  flushTrailingOpaqueReasoning();
669
701
  }
670
702
  return converted;
@@ -1 +1 @@
1
- {"version":3,"file":"conversions.js","names":[],"sources":["../../../src/runtime/adapter/conversions.ts"],"sourcesContent":["\"use client\";\n\nimport type { InputContent, RunAgentParameters } from \"@ag-ui/client\";\nimport type {\n ThreadMessageLike as CoreThreadMessageLike,\n PartProviderMetadata,\n ToolCallMessagePartMcpMetadata,\n ToolModelContentPart,\n} from \"@assistant-ui/core\";\nimport {\n getAutoStatus,\n httpUrlPattern,\n parseDataUrl,\n} from \"@assistant-ui/core/internal\";\nimport { type Tool, toToolsJSONSchema } from \"assistant-stream\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\nimport {\n AG_UI_METADATA_NAMESPACE,\n A2UI_SURFACE_ACTIVITY_TYPE,\n type AgUiCustomMetadata,\n type AgUiOpaqueReasoning,\n} from \"./run-aggregator\";\nimport {\n applyA2uiOperations,\n convertSurfaceToUISpec,\n type A2uiState,\n type A2uiSurfaceState,\n} from \"@assistant-ui/react-generative-ui/a2ui\";\nimport type { AgUiInterrupt } from \"../types\";\nimport {\n parseMcpToolCallResult,\n readMcpAppResourceUri,\n} from \"../mcp-tool-result\";\n\nexport type { InputContent };\n\ntype AttachmentLike = {\n name?: string | undefined;\n contentType?: string | undefined;\n content?: readonly unknown[] | undefined;\n};\n\ntype ThreadMessageLike = {\n id: string;\n role: string;\n content: unknown;\n metadata?: unknown;\n name?: string;\n toolCallId?: string;\n error?: string;\n attachments?: readonly AttachmentLike[];\n};\n\ntype AgUiToolCall = {\n id: string;\n type: \"function\";\n function: { name: string; arguments: string };\n};\n\nexport type AgUiMessage =\n | {\n id: string;\n role: string;\n content: string | InputContent[];\n name?: string;\n toolCalls?: AgUiToolCall[];\n }\n | {\n id: string;\n role: \"reasoning\";\n content: string;\n encryptedValue?: string;\n }\n | {\n id: string;\n role: \"tool\";\n content: string;\n toolCallId: string;\n error?: string;\n };\n\ntype ToolCallPart = {\n type: \"tool-call\";\n toolCallId?: string;\n toolName: string;\n argsText?: string;\n args?: ReadonlyJSONObject;\n result?: unknown;\n isError?: boolean;\n modelContent?: readonly ToolModelContentPart[];\n unstable_toolMessageId?: string;\n mcp?: ToolCallMessagePartMcpMetadata;\n};\n\nconst isObject = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null;\n\nconst getString = (record: Record<string, unknown>, key: string) => {\n const value = record[key];\n return typeof value === \"string\" ? value : undefined;\n};\n\nconst getToolCallId = (record: Record<string, unknown>) =>\n getString(record, \"toolCallId\") ?? getString(record, \"tool_call_id\");\n\nconst readAgUiNamespace = (providerMetadata: unknown) => {\n if (!isObject(providerMetadata)) return undefined;\n const namespaced = providerMetadata[AG_UI_METADATA_NAMESPACE];\n return isObject(namespaced) ? namespaced : undefined;\n};\n\nconst readAgUiReasoningMeta = (providerMetadata: unknown) => {\n const namespaced = readAgUiNamespace(providerMetadata);\n if (!namespaced) return {};\n return {\n encryptedValue: getString(namespaced, \"encryptedValue\"),\n reasoningId: getString(namespaced, \"reasoningId\"),\n };\n};\n\n// AG-UI carries per-item extras in `metadata`; anything else on the item is\n// stripped by its schema. The part's own `filename` wins over a key of the\n// same name in the bag.\nconst buildInputMetadata = (\n part: Record<string, unknown>,\n filename?: string | undefined,\n) => {\n const metadata = {\n ...readAgUiNamespace(part.providerMetadata),\n ...(filename !== undefined && { filename }),\n };\n return Object.keys(metadata).length > 0 ? metadata : undefined;\n};\n\n// The inverse: `filename` lands on the part's own field, everything else the\n// item carried goes back into the namespace it came from, so a snapshot echo\n// resends what the host attached.\nconst readInputMetadata = (\n metadata: unknown,\n): {\n filename?: string | undefined;\n providerMetadata?: PartProviderMetadata | undefined;\n} => {\n if (!isObject(metadata)) return {};\n const { filename: _filename, ...rest } = metadata;\n return {\n filename: getString(metadata, \"filename\"),\n ...(Object.keys(rest).length > 0 && {\n providerMetadata: {\n [AG_UI_METADATA_NAMESPACE]: rest as PartProviderMetadata[string],\n },\n }),\n };\n};\n\nfunction parseJSONText(value: string): unknown {\n if (!value) return value;\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nfunction generateId(): string {\n return (\n (globalThis.crypto as { randomUUID?: () => string })?.randomUUID?.() ??\n Math.random().toString(36).slice(2)\n );\n}\n\nfunction normalizeToolCall(part: ToolCallPart): {\n id: string;\n call: AgUiToolCall;\n} {\n const id = part.toolCallId ?? generateId();\n const argsText =\n typeof part.argsText === \"string\"\n ? part.argsText\n : JSON.stringify(part.args ?? {});\n\n return {\n id,\n call: {\n id,\n type: \"function\",\n function: {\n name: part.toolName ?? \"tool\",\n arguments: argsText,\n },\n },\n };\n}\n\nfunction extractText(content: unknown): string {\n if (typeof content === \"string\") return content;\n if (!Array.isArray(content)) return \"\";\n\n return content\n .filter(\n (part): part is { type: \"text\"; text: string } =>\n part?.type === \"text\" && typeof part?.text === \"string\",\n )\n .map((part) => part.text)\n .join(\"\\n\");\n}\n\ntype InputContentSource =\n | { type: \"data\"; value: string; mimeType: string }\n | { type: \"url\"; value: string; mimeType?: string };\n\ntype MediaInputType = \"image\" | \"audio\" | \"video\" | \"document\";\n\nfunction mediaTypeForMime(mimeType: string | undefined): MediaInputType {\n if (mimeType?.startsWith(\"image/\")) return \"image\";\n if (mimeType?.startsWith(\"audio/\")) return \"audio\";\n if (mimeType?.startsWith(\"video/\")) return \"video\";\n return \"document\";\n}\n\n// Build an AG-UI multimodal source from a data URL, raw base64 payload, or an\n// http(s) URL. A `url` source may omit the mime type; a `data` source always\n// resolves one (falling back to application/octet-stream). An explicit\n// `sourceType: \"url\"` on the part forces the url leg for non-http references.\nfunction buildInputSource(\n value: string,\n declaredMimeType: string | undefined,\n sourceType?: string,\n): InputContentSource {\n if (sourceType === \"url\" || httpUrlPattern.test(value)) {\n return declaredMimeType !== undefined\n ? { type: \"url\", value, mimeType: declaredMimeType }\n : { type: \"url\", value };\n }\n const parsed = parseDataUrl(value);\n return {\n type: \"data\",\n value: parsed?.data ?? value,\n mimeType:\n parsed?.mimeType ?? declaredMimeType ?? \"application/octet-stream\",\n };\n}\n\nfunction toInputContent(\n part: unknown,\n fallbackMimeType: string | undefined,\n): InputContent | null {\n if (!isObject(part)) return null;\n const type = getString(part, \"type\");\n\n if (type === \"text\") {\n const text = getString(part, \"text\");\n if (text === undefined) return null;\n return { type: \"text\", text };\n }\n\n if (type === \"image\") {\n const image = getString(part, \"image\");\n if (image === undefined) return null;\n const metadata = buildInputMetadata(part, getString(part, \"filename\"));\n return {\n type: \"image\",\n source: buildInputSource(image, fallbackMimeType),\n ...(metadata && { metadata }),\n };\n }\n\n if (type === \"file\") {\n const data = getString(part, \"data\");\n if (data === undefined) return null;\n const declaredMimeType = getString(part, \"mimeType\") || fallbackMimeType;\n const filename = getString(part, \"filename\");\n const source = buildInputSource(\n data,\n declaredMimeType,\n getString(part, \"sourceType\"),\n );\n const metadata = buildInputMetadata(part, filename);\n switch (mediaTypeForMime(source.mimeType)) {\n case \"image\":\n return { type: \"image\", source, ...(metadata && { metadata }) };\n case \"audio\":\n return { type: \"audio\", source, ...(metadata && { metadata }) };\n case \"video\":\n return { type: \"video\", source, ...(metadata && { metadata }) };\n default:\n return { type: \"document\", source, ...(metadata && { metadata }) };\n }\n }\n\n if (type === \"audio\") {\n const audio = part.audio;\n if (!isObject(audio)) return null;\n const data = getString(audio, \"data\");\n const format = getString(audio, \"format\");\n if (data === undefined || format === undefined) return null;\n return {\n type: \"audio\",\n source: buildInputSource(\n parseDataUrl(data)?.data ?? data,\n `audio/${format}`,\n ),\n };\n }\n\n return null;\n}\n\ntype SnapshotAttachment = NonNullable<\n CoreThreadMessageLike[\"attachments\"]\n>[number];\n\nconst mediaInputTypes = new Set([\"image\", \"audio\", \"video\", \"document\"]);\n\n// Inverse of buildInputSource.\nfunction inputSourceToString(\n value: unknown,\n): { value: string; mimeType?: string; isUrl?: boolean } | null {\n if (!isObject(value)) return null;\n const sourceValue = getString(value, \"value\");\n if (sourceValue === undefined) return null;\n const mimeType = getString(value, \"mimeType\");\n const type = getString(value, \"type\");\n if (type === \"url\") {\n return {\n value: sourceValue,\n isUrl: true,\n ...(mimeType !== undefined && { mimeType }),\n };\n }\n if (type === \"data\") {\n const resolvedMimeType = mimeType ?? \"application/octet-stream\";\n return {\n value: `data:${resolvedMimeType};base64,${sourceValue}`,\n mimeType: resolvedMimeType,\n };\n }\n return null;\n}\n\n// Mirrors @ag-ui/client's BackwardCompatibility_0_0_47 middleware.\nfunction upgradeBinaryInputPart(\n part: Record<string, unknown>,\n): Record<string, unknown> | null {\n const mimeType = getString(part, \"mimeType\");\n if (mimeType === undefined) return null;\n const data = getString(part, \"data\");\n const url = getString(part, \"url\");\n const source = data\n ? { type: \"data\", value: data, mimeType }\n : url\n ? { type: \"url\", value: url, mimeType }\n : null;\n if (!source) return null;\n const filename = getString(part, \"filename\");\n return {\n type: mediaTypeForMime(mimeType),\n source,\n ...(filename && { metadata: { filename } }),\n };\n}\n\nfunction toSnapshotAttachments(content: unknown): SnapshotAttachment[] {\n if (!Array.isArray(content)) return [];\n\n const attachments: SnapshotAttachment[] = [];\n for (const rawPart of content) {\n if (!isObject(rawPart)) continue;\n const part =\n getString(rawPart, \"type\") === \"binary\"\n ? upgradeBinaryInputPart(rawPart)\n : rawPart;\n if (!part) continue;\n const type = getString(part, \"type\");\n if (type === undefined || !mediaInputTypes.has(type)) continue;\n const source = inputSourceToString(part.source);\n if (!source) continue;\n\n const { filename, providerMetadata } = readInputMetadata(part.metadata);\n const id = attachments.length.toString();\n\n if (type === \"image\") {\n attachments.push({\n id,\n type: \"image\",\n name: filename ?? \"image\",\n ...(source.mimeType !== undefined && { contentType: source.mimeType }),\n status: { type: \"complete\" },\n content: [\n {\n type: \"image\",\n image: source.value,\n ...(filename !== undefined && { filename }),\n ...(providerMetadata && { providerMetadata }),\n },\n ],\n });\n continue;\n }\n\n const mimeType = source.mimeType ?? \"application/octet-stream\";\n attachments.push({\n id,\n type: type === \"document\" ? \"document\" : \"file\",\n name: filename ?? \"file\",\n contentType: mimeType,\n status: { type: \"complete\" },\n content: [\n {\n type: \"file\",\n data: source.value,\n mimeType,\n ...(source.isUrl && { sourceType: \"url\" as const }),\n ...(filename !== undefined && { filename }),\n ...(providerMetadata && { providerMetadata }),\n },\n ],\n });\n }\n return attachments;\n}\n\nfunction buildUserContent(message: ThreadMessageLike): string | InputContent[] {\n const contentParts = Array.isArray(message.content) ? message.content : [];\n\n const attachments = message.attachments ?? [];\n\n const converted: InputContent[] = [];\n\n // Promote string-form content to a leading text part so it survives when\n // non-text attachments are present (fromAgUiMessages emits string content).\n if (typeof message.content === \"string\" && message.content.length > 0) {\n converted.push({ type: \"text\", text: message.content });\n }\n\n for (const part of contentParts) {\n const input = toInputContent(part, undefined);\n if (input) converted.push(input);\n }\n for (const attachment of attachments) {\n if (!isObject(attachment)) continue;\n const attachmentContent = attachment.content;\n if (!Array.isArray(attachmentContent)) continue;\n const fallbackMime = getString(attachment, \"contentType\");\n for (const part of attachmentContent) {\n const input = toInputContent(part, fallbackMime);\n if (input) converted.push(input);\n }\n }\n\n const hasNonText = converted.some((part) => part.type !== \"text\");\n if (hasNonText) return converted;\n\n // All-text path: collapse to plain string. Join text parts collected from\n // both content and attachments so attachment-sourced text is not dropped.\n if (converted.length === 0) return extractText(message.content);\n return converted\n .filter(\n (part): part is { type: \"text\"; text: string } => part.type === \"text\",\n )\n .map((part) => part.text)\n .join(\"\\n\");\n}\n\nfunction toToolCallPart(value: unknown): ToolCallPart | null {\n if (!isObject(value)) return null;\n const rawFunction = isObject(value.function) ? value.function : null;\n const toolCallId = getString(value, \"toolCallId\") ?? getString(value, \"id\");\n const toolName =\n getString(value, \"toolName\") ??\n getString(value, \"name\") ??\n (rawFunction ? getString(rawFunction, \"name\") : undefined) ??\n \"tool\";\n const argsText =\n getString(value, \"argsText\") ??\n getString(value, \"arguments\") ??\n (rawFunction ? getString(rawFunction, \"arguments\") : undefined);\n\n const parsedArgs =\n typeof argsText === \"string\" ? parseJSONText(argsText) : undefined;\n const args =\n isObject(parsedArgs) && !Array.isArray(parsedArgs)\n ? (parsedArgs as ReadonlyJSONObject)\n : isObject(value.args) && !Array.isArray(value.args)\n ? (value.args as ReadonlyJSONObject)\n : undefined;\n\n const part: ToolCallPart = {\n type: \"tool-call\",\n ...(toolCallId !== undefined ? { toolCallId } : {}),\n toolName,\n argsText: argsText ?? JSON.stringify(args ?? {}),\n ...(args !== undefined ? { args } : {}),\n };\n\n if (value.type === \"tool-call\") {\n const result = value.result;\n const isError = value.isError;\n if (result !== undefined) part.result = result;\n if (typeof isError === \"boolean\") part.isError = isError;\n }\n\n return part;\n}\n\nfunction extractAssistantToolCalls(\n message: Record<string, unknown>,\n): ToolCallPart[] {\n const parts: ToolCallPart[] = [];\n const seenToolCallIds = new Set<string>();\n const pushPart = (part: ToolCallPart | null) => {\n if (!part) return;\n const id = part.toolCallId ?? generateId();\n if (seenToolCallIds.has(id)) return;\n seenToolCallIds.add(id);\n parts.push({\n ...part,\n toolCallId: id,\n });\n };\n\n const content = message.content;\n if (Array.isArray(content)) {\n for (const part of content) {\n if (isObject(part) && part.type === \"tool-call\") {\n pushPart(toToolCallPart(part));\n }\n }\n }\n\n const toolCalls = Array.isArray(message.toolCalls)\n ? message.toolCalls\n : Array.isArray(message.tool_calls)\n ? message.tool_calls\n : [];\n for (const call of toolCalls) {\n pushPart(toToolCallPart(call));\n }\n\n return parts;\n}\n\nfunction validateInterrupts(value: unknown): AgUiInterrupt[] | undefined {\n if (!Array.isArray(value)) return undefined;\n const valid = value.filter(\n (entry): entry is AgUiInterrupt =>\n isObject(entry) &&\n typeof entry.id === \"string\" &&\n typeof entry.reason === \"string\",\n );\n return valid.length > 0 ? valid : undefined;\n}\n\nfunction readPersistedInterrupts(\n metadata: unknown,\n): AgUiInterrupt[] | undefined {\n if (!isObject(metadata)) return undefined;\n const custom = metadata.custom;\n if (!isObject(custom)) return undefined;\n const namespaced = custom[AG_UI_METADATA_NAMESPACE];\n if (!isObject(namespaced)) return undefined;\n return validateInterrupts(namespaced.interrupts);\n}\n\nfunction readOpaqueReasoning(metadata: unknown): AgUiOpaqueReasoning[] {\n if (!isObject(metadata)) return [];\n const custom = metadata.custom;\n if (!isObject(custom)) return [];\n const namespaced = custom[AG_UI_METADATA_NAMESPACE];\n if (!isObject(namespaced)) return [];\n const entries = namespaced.opaqueReasoning;\n if (!Array.isArray(entries)) return [];\n return entries.flatMap((entry) => {\n if (!isObject(entry)) return [];\n const id = getString(entry, \"id\");\n const encryptedValue = getString(entry, \"encryptedValue\");\n if (!id?.trim() || !encryptedValue?.trim()) return [];\n return [\n { id, encryptedValue, ...(entry.after === true ? { after: true } : {}) },\n ];\n });\n}\n\nfunction withOpaqueReasoning(\n message: CoreThreadMessageLike,\n opaqueReasoning: AgUiOpaqueReasoning[],\n): CoreThreadMessageLike {\n const metadata = isObject(message.metadata) ? message.metadata : {};\n const custom = isObject(metadata.custom) ? metadata.custom : {};\n const namespaced = isObject(custom[AG_UI_METADATA_NAMESPACE])\n ? (custom[AG_UI_METADATA_NAMESPACE] as Record<string, unknown>)\n : {};\n return {\n ...message,\n metadata: {\n ...metadata,\n custom: {\n ...custom,\n [AG_UI_METADATA_NAMESPACE]: { ...namespaced, opaqueReasoning },\n },\n },\n } as CoreThreadMessageLike;\n}\n\nfunction toAssistantSnapshotMessage(\n rawMessage: Record<string, unknown>,\n): CoreThreadMessageLike {\n const text = extractText(rawMessage.content);\n const toolCallParts = extractAssistantToolCalls(rawMessage);\n const assistantContent = [\n ...(text.length > 0 ? [{ type: \"text\" as const, text }] : []),\n ...toolCallParts,\n ];\n const messageName = getString(rawMessage, \"name\");\n const interrupts = readPersistedInterrupts(rawMessage.metadata);\n return {\n id: getString(rawMessage, \"id\") ?? generateId(),\n role: \"assistant\",\n content: assistantContent.length > 0 ? assistantContent : \"\",\n ...(messageName !== undefined ? { name: messageName } : {}),\n ...(interrupts\n ? {\n metadata: {\n custom: {\n [AG_UI_METADATA_NAMESPACE]: {\n interrupts,\n } satisfies AgUiCustomMetadata,\n },\n },\n }\n : {}),\n };\n}\n\nfunction toUserOrSystemSnapshotMessage(\n role: \"user\" | \"system\",\n rawMessage: Record<string, unknown>,\n): CoreThreadMessageLike {\n const messageName = getString(rawMessage, \"name\");\n const attachments =\n role === \"user\" ? toSnapshotAttachments(rawMessage.content) : [];\n return {\n id: getString(rawMessage, \"id\") ?? generateId(),\n role,\n content: extractText(rawMessage.content),\n ...(messageName !== undefined ? { name: messageName } : {}),\n ...(attachments.length > 0 ? { attachments } : {}),\n };\n}\n\n// Rebuilds the a2ui:<surfaceId> \"present\" tool-call parts for one owning\n// assistant message from a bucket of rebuilt surface state, mirroring the\n// live RunAggregator.synthesizeA2uiToolCalls shape. Non-a2ui parts (text,\n// other tool calls) are preserved; existing a2ui parts are replaced wholesale\n// so create/update/delete within the bucket all converge on the rebuilt set.\nfunction attachA2uiSurfaces(\n message: CoreThreadMessageLike,\n state: A2uiState,\n): CoreThreadMessageLike {\n const a2uiParts: ToolCallPart[] = [];\n for (const [surfaceId, surface] of state) {\n const { spec } = convertSurfaceToUISpec(surface);\n if (!spec) continue;\n a2uiParts.push({\n type: \"tool-call\",\n toolCallId: `a2ui:${surfaceId}`,\n toolName: \"present\",\n args: spec as unknown as ReadonlyJSONObject,\n argsText: JSON.stringify(spec),\n result: {},\n });\n }\n\n const content = Array.isArray(message.content) ? message.content : [];\n const preserved = content.filter(\n (part) =>\n !(\n isObject(part) &&\n part.type === \"tool-call\" &&\n typeof part.toolCallId === \"string\" &&\n part.toolCallId.startsWith(\"a2ui:\")\n ),\n );\n return { ...message, content: [...preserved, ...a2uiParts] };\n}\n\nexport type FromAgUiMessagesOptions = {\n /**\n * Whether to convert `reasoning` messages into visible reasoning parts.\n * Matches the `showThinking` option of `useAgUiRuntime`. Defaults to `true`.\n */\n showThinking?: boolean;\n};\n\nexport function fromAgUiMessages(\n messages: readonly unknown[],\n options?: FromAgUiMessagesOptions,\n): CoreThreadMessageLike[] {\n const showThinking = options?.showThinking ?? true;\n const converted: CoreThreadMessageLike[] = [];\n const a2uiBuckets = new Map<string, A2uiState>();\n const a2uiBucketOwnerIndices = new Map<string, number>();\n // A zero-data-retention run carries its payload in encryptedValue with no\n // readable content, so the record has no part to become and rides on the\n // neighbouring message instead of being dropped. The anchor is the index the\n // next pushed message will occupy, which is where it sat on the wire.\n const opaqueReasoning: (AgUiOpaqueReasoning & { anchor: number })[] = [];\n\n for (const rawMessage of messages) {\n if (!isObject(rawMessage)) continue;\n const role = getString(rawMessage, \"role\");\n if (!role) continue;\n\n if (role === \"tool\") {\n const toolCallId = getToolCallId(rawMessage) ?? `tool-${generateId()}`;\n const toolMessageId = getString(rawMessage, \"id\");\n const modelContent = extractText(rawMessage.content);\n const mcpResult = parseMcpToolCallResult(rawMessage, modelContent);\n const mcpModelContent = mcpResult\n ? [{ type: \"text\" as const, text: modelContent }]\n : undefined;\n const result =\n mcpResult ??\n (rawMessage.result !== undefined\n ? rawMessage.result\n : typeof rawMessage.content === \"string\"\n ? parseJSONText(rawMessage.content)\n : rawMessage.content);\n const isError =\n typeof rawMessage.error === \"string\" ||\n rawMessage.isError === true ||\n rawMessage.status === \"error\"\n ? true\n : rawMessage.isError === false\n ? false\n : undefined;\n const mcpAppUri = readMcpAppResourceUri(mcpResult?._meta);\n const mcpApp =\n mcpAppUri !== undefined ? { resourceUri: mcpAppUri } : undefined;\n\n let updated = false;\n for (\n let messageIndex = converted.length - 1;\n messageIndex >= 0 && !updated;\n messageIndex--\n ) {\n const message = converted[messageIndex];\n if (\n !message ||\n message.role !== \"assistant\" ||\n !Array.isArray(message.content)\n )\n continue;\n\n for (\n let partIndex = message.content.length - 1;\n partIndex >= 0;\n partIndex--\n ) {\n const part = message.content[partIndex];\n if (!isObject(part) || part.type !== \"tool-call\") continue;\n if (getString(part, \"toolCallId\") !== toolCallId) continue;\n\n const updatedPart: ToolCallPart = {\n ...(part as ToolCallPart),\n result,\n ...(mcpModelContent ? { modelContent: mcpModelContent } : {}),\n ...(isError !== undefined ? { isError } : {}),\n ...(toolMessageId !== undefined\n ? { unstable_toolMessageId: toolMessageId }\n : {}),\n ...(mcpApp ? { mcp: { app: mcpApp } } : {}),\n };\n const updatedContent = message.content.map((contentPart, index) =>\n index === partIndex ? updatedPart : contentPart,\n );\n converted[messageIndex] = { ...message, content: updatedContent };\n updated = true;\n break;\n }\n }\n\n if (updated) {\n continue;\n }\n\n const id = toolMessageId ?? toolCallId;\n const toolName =\n getString(rawMessage, \"name\") ??\n getString(rawMessage, \"toolName\") ??\n \"tool\";\n converted.push({\n id: `${id}:assistant`,\n role: \"assistant\",\n content: [\n {\n type: \"tool-call\",\n toolCallId,\n toolName,\n args: {},\n argsText: \"{}\",\n result,\n ...(mcpModelContent ? { modelContent: mcpModelContent } : {}),\n ...(isError !== undefined ? { isError } : {}),\n ...(toolMessageId !== undefined\n ? { unstable_toolMessageId: toolMessageId }\n : {}),\n ...(mcpApp ? { mcp: { app: mcpApp } } : {}),\n },\n ],\n });\n continue;\n }\n\n if (role === \"activity\") {\n // Only a2ui-surface activity messages have an assistant-part equivalent\n // to rehydrate; other activity types still have no surface to repaint.\n const activityType = getString(rawMessage, \"activityType\");\n if (activityType !== A2UI_SURFACE_ACTIVITY_TYPE) continue;\n const activityContent = isObject(rawMessage.content)\n ? (rawMessage.content as Record<string, unknown>)\n : null;\n const operations = activityContent?.[\"a2ui_operations\"];\n if (!Array.isArray(operations)) continue;\n\n let ownerIndex = -1;\n for (let i = converted.length - 1; i >= 0; i--) {\n const candidate = converted[i];\n if (candidate && candidate.role === \"assistant\") {\n ownerIndex = i;\n break;\n }\n }\n if (ownerIndex === -1) continue;\n\n const owner = converted[ownerIndex]!;\n const bucketKey = getString(rawMessage, \"id\") ?? \"a2ui:anonymous\";\n const { state } = applyA2uiOperations(new Map(), operations);\n a2uiBuckets.delete(bucketKey);\n a2uiBucketOwnerIndices.delete(bucketKey);\n a2uiBuckets.set(bucketKey, state);\n a2uiBucketOwnerIndices.set(bucketKey, ownerIndex);\n\n const ownerState = new Map<string, A2uiSurfaceState>();\n for (const [candidateBucketKey, candidateState] of a2uiBuckets) {\n if (a2uiBucketOwnerIndices.get(candidateBucketKey) !== ownerIndex)\n continue;\n for (const [surfaceId, surface] of candidateState) {\n ownerState.set(surfaceId, surface);\n }\n }\n converted[ownerIndex] = attachA2uiSurfaces(owner, ownerState);\n continue;\n }\n\n if (role === \"assistant\") {\n converted.push(toAssistantSnapshotMessage(rawMessage));\n continue;\n }\n\n if (role === \"reasoning\") {\n const text = extractText(rawMessage.content);\n const encryptedValue = getString(rawMessage, \"encryptedValue\");\n if (text.trim().length === 0) {\n // showThinking hides reasoning; this record is never rendered anyway,\n // and dropping it would deny the provider the payload it needs to\n // accept the next run.\n const opaqueId = getString(rawMessage, \"id\");\n if (opaqueId?.trim() && encryptedValue?.trim()) {\n opaqueReasoning.push({\n id: opaqueId,\n encryptedValue,\n anchor: converted.length,\n });\n }\n continue;\n }\n // Gate visible reasoning on showThinking so a cold reload matches the\n // live run: the aggregator never stores reasoning parts when it is off.\n // A signature on a hidden record is still transport state, so it is kept\n // opaque rather than discarded with the text.\n if (!showThinking) {\n const hiddenId = getString(rawMessage, \"id\");\n if (hiddenId?.trim() && encryptedValue?.trim()) {\n opaqueReasoning.push({\n id: hiddenId,\n encryptedValue,\n anchor: converted.length,\n });\n }\n continue;\n }\n converted.push({\n id: getString(rawMessage, \"id\") ?? generateId(),\n role: \"assistant\",\n content: [\n {\n type: \"reasoning\",\n text,\n ...(encryptedValue !== undefined\n ? {\n providerMetadata: {\n [AG_UI_METADATA_NAMESPACE]: { encryptedValue },\n },\n }\n : {}),\n },\n ],\n });\n continue;\n }\n\n if (role === \"user\" || role === \"system\") {\n converted.push(toUserOrSystemSnapshotMessage(role, rawMessage));\n }\n }\n\n for (const { anchor, ...entry } of opaqueReasoning) {\n // Nothing followed it on the wire, so it trails the last message instead.\n const trailing = anchor >= converted.length;\n const index = trailing ? converted.length - 1 : anchor;\n const target = converted[index];\n if (!target) continue;\n converted[index] = withOpaqueReasoning(target, [\n ...readOpaqueReasoning(target.metadata),\n ...(trailing ? [{ ...entry, after: true }] : [entry]),\n ]);\n }\n\n for (let i = 0; i < converted.length; i++) {\n const message = converted[i]!;\n if (message.role !== \"assistant\") continue;\n\n const hasInterrupt =\n readPersistedInterrupts(message.metadata) !== undefined;\n const hasPendingToolCall =\n Array.isArray(message.content) &&\n message.content.some(\n (part) =>\n isObject(part) &&\n part.type === \"tool-call\" &&\n part.result === undefined,\n );\n\n if (hasInterrupt || hasPendingToolCall) {\n converted[i] = {\n ...message,\n status: getAutoStatus(\n false,\n false,\n hasInterrupt,\n hasPendingToolCall,\n undefined,\n ),\n };\n }\n }\n\n return converted;\n}\n\nfunction convertAssistantMessage(\n message: ThreadMessageLike,\n converted: AgUiMessage[],\n): void {\n const content = extractText(message.content);\n const contentArray = Array.isArray(message.content) ? message.content : [];\n\n const toolCallParts = contentArray.filter(\n (part): part is ToolCallPart =>\n part?.type === \"tool-call\" &&\n !(\n typeof part.toolCallId === \"string\" &&\n part.toolCallId.startsWith(\"a2ui:\")\n ),\n );\n\n const toolCalls = toolCallParts.map((part) => ({\n ...normalizeToolCall(part),\n part,\n }));\n\n // An AG-UI assistant record has no reasoning field, so reasoning leaves as\n // the standalone record it arrived as, ahead of the assistant it belongs to.\n const shellIsDropped = content.length === 0 && toolCalls.length === 0;\n let reasoningIndex = 0;\n for (const part of contentArray) {\n if (!isObject(part) || part.type !== \"reasoning\") continue;\n const text = getString(part, \"text\") ?? \"\";\n if (text.trim().length === 0) continue;\n const { encryptedValue, reasoningId } = readAgUiReasoningMeta(\n part.providerMetadata,\n );\n converted.push({\n // The wire id is what a signature was issued against, so it wins over a\n // synthesized one whenever the run carried it.\n id: reasoningId?.trim()\n ? reasoningId\n : shellIsDropped && reasoningIndex === 0\n ? message.id\n : `${message.id}:reasoning-${reasoningIndex}`,\n role: \"reasoning\",\n content: text,\n ...(encryptedValue !== undefined ? { encryptedValue } : {}),\n });\n reasoningIndex++;\n }\n\n // Drop assistant messages with no text or tool calls (e.g. an imported\n // reasoning-only entry) so they are not re-sent as a blank assistant turn.\n if (shellIsDropped) {\n return;\n }\n\n const assistantMessage: AgUiMessage = {\n id: message.id,\n role: \"assistant\",\n content,\n };\n if (message.name) {\n assistantMessage.name = message.name;\n }\n if (toolCalls.length > 0) {\n assistantMessage.toolCalls = toolCalls.map((entry) => entry.call);\n }\n converted.push(assistantMessage);\n\n for (const { id: toolCallId, part } of toolCalls) {\n if (part.result === undefined) continue;\n\n const resultContent =\n part.modelContent !== undefined\n ? extractText(part.modelContent)\n : typeof part.result === \"string\"\n ? part.result\n : JSON.stringify(part.result);\n\n const toolMessage: AgUiMessage = {\n id: part.unstable_toolMessageId ?? `${toolCallId}:tool`,\n role: \"tool\",\n content: resultContent,\n toolCallId,\n };\n if (part.isError) {\n toolMessage.error = resultContent;\n }\n converted.push(toolMessage);\n }\n}\n\nfunction convertToolMessage(\n message: ThreadMessageLike,\n converted: AgUiMessage[],\n): void {\n const content = extractText(message.content);\n const toolCallId = message.toolCallId ?? generateId();\n\n const toolMessage: AgUiMessage = {\n id: message.id,\n role: \"tool\",\n content,\n toolCallId,\n };\n if (typeof message.error === \"string\") {\n toolMessage.error = message.error;\n }\n converted.push(toolMessage);\n}\n\nexport function toAgUiMessages(\n messages: readonly ThreadMessageLike[],\n): AgUiMessage[] {\n const converted: AgUiMessage[] = [];\n\n for (const message of messages) {\n const opaqueReasoning = readOpaqueReasoning(message.metadata);\n const toOpaqueRecord = (entry: AgUiOpaqueReasoning): AgUiMessage => ({\n id: entry.id,\n role: \"reasoning\",\n content: \"\",\n encryptedValue: entry.encryptedValue,\n });\n for (const entry of opaqueReasoning) {\n if (entry.after !== true) converted.push(toOpaqueRecord(entry));\n }\n const flushTrailingOpaqueReasoning = () => {\n for (const entry of opaqueReasoning) {\n if (entry.after === true) converted.push(toOpaqueRecord(entry));\n }\n };\n\n if (message.role === \"assistant\") {\n convertAssistantMessage(message, converted);\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n if (message.role === \"tool\") {\n convertToolMessage(message, converted);\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n const genericMessage: AgUiMessage = {\n id: message.id,\n role: message.role,\n content:\n message.role === \"user\"\n ? buildUserContent(message)\n : extractText(message.content),\n };\n if (message.name) {\n genericMessage.name = message.name;\n }\n converted.push(genericMessage);\n flushTrailingOpaqueReasoning();\n }\n\n return converted;\n}\n\ntype AgUiTool = NonNullable<RunAgentParameters[\"tools\"]>[number];\n\nexport function toAgUiTools(\n tools: Record<string, Tool> | undefined,\n): AgUiTool[] {\n if (!tools) return [];\n\n const toolsSchema = toToolsJSONSchema(tools);\n return Object.entries(to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BAA6B;GAC7B;EACF;EAEA,IAAI,QAAQ,SAAS,QAAQ;GAC3B,mBAAmB,SAAS,SAAS;GACrC,6BAA6B;GAC7B;EACF;EAEA,MAAM,iBAA8B;GAClC,IAAI,QAAQ;GACZ,MAAM,QAAQ;GACd,SACE,QAAQ,SAAS,SACb,iBAAiB,OAAO,IACxB,YAAY,QAAQ,OAAO;EACnC;EACA,IAAI,QAAQ,MACV,eAAe,OAAO,QAAQ;EAEhC,UAAU,KAAK,cAAc;EAC7B,6BAA6B;CAC/B;CAEA,OAAO;AACT;AAIA,SAAgB,YACd,OACY;CACZ,IAAI,CAAC,OAAO,OAAO,CAAC;CAEpB,MAAM,cAAc,kBAAkB,KAAK;CAC3C,OAAO,OAAO,QAAQ,WAAW,CAAC,CAAC,KAAK,CAAC,MAAM,WAAW;EACxD;EACA,aAAa,KAAK,eAAe;EACjC,YAAY,KAAK;CACnB,EAAE;AACJ"}
1
+ {"version":3,"file":"conversions.js","names":[],"sources":["../../../src/runtime/adapter/conversions.ts"],"sourcesContent":["\"use client\";\n\nimport type { InputContent, RunAgentParameters } from \"@ag-ui/client\";\nimport type {\n ThreadMessageLike as CoreThreadMessageLike,\n PartProviderMetadata,\n ToolCallMessagePartMcpMetadata,\n ToolModelContentPart,\n} from \"@assistant-ui/core\";\nimport {\n getAutoStatus,\n httpUrlPattern,\n parseDataUrl,\n} from \"@assistant-ui/core/internal\";\nimport { type Tool, toToolsJSONSchema } from \"assistant-stream\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\nimport {\n AG_UI_METADATA_NAMESPACE,\n A2UI_SURFACE_ACTIVITY_TYPE,\n type AgUiCustomMetadata,\n type AgUiOpaqueReasoning,\n} from \"./run-aggregator\";\nimport {\n applyA2uiOperations,\n convertSurfaceToUISpec,\n type A2uiState,\n type A2uiSurfaceState,\n} from \"@assistant-ui/react-generative-ui/a2ui\";\nimport type { AgUiInterrupt } from \"../types\";\nimport { projectAgUiToolApprovals } from \"./tool-approval\";\nimport {\n parseMcpToolCallResult,\n readMcpAppResourceUri,\n} from \"../mcp-tool-result\";\n\nexport type { InputContent };\n\ntype AttachmentLike = {\n name?: string | undefined;\n contentType?: string | undefined;\n content?: readonly unknown[] | undefined;\n};\n\ntype ThreadMessageLike = {\n id?: string;\n role: string;\n content: unknown;\n metadata?: unknown;\n name?: string;\n toolCallId?: string;\n error?: string;\n attachments?: readonly AttachmentLike[];\n};\n\ntype NormalizedThreadMessageLike = ThreadMessageLike & { id: string };\n\ntype AgUiToolCall = {\n id: string;\n type: \"function\";\n function: { name: string; arguments: string };\n};\n\nexport type AgUiMessage =\n | {\n id: string;\n role: \"user\";\n content: string | InputContent[];\n name?: string;\n }\n | {\n id: string;\n role: \"assistant\";\n content: string;\n name?: string;\n toolCalls?: AgUiToolCall[];\n }\n | {\n id: string;\n role: \"system\" | \"developer\";\n content: string;\n name?: string;\n }\n | {\n id: string;\n role: \"reasoning\";\n content: string;\n encryptedValue?: string;\n }\n | {\n id: string;\n role: \"tool\";\n content: string;\n toolCallId: string;\n error?: string;\n };\n\ntype ToolCallPart = {\n type: \"tool-call\";\n toolCallId?: string;\n toolName: string;\n argsText?: string;\n args?: ReadonlyJSONObject;\n result?: unknown;\n isError?: boolean;\n modelContent?: readonly ToolModelContentPart[];\n unstable_toolMessageId?: string;\n mcp?: ToolCallMessagePartMcpMetadata;\n};\n\nconst isObject = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null;\n\nconst getString = (record: Record<string, unknown>, key: string) => {\n const value = record[key];\n return typeof value === \"string\" ? value : undefined;\n};\n\nconst getToolCallId = (record: Record<string, unknown>) =>\n getString(record, \"toolCallId\") ?? getString(record, \"tool_call_id\");\n\nconst readAgUiNamespace = (providerMetadata: unknown) => {\n if (!isObject(providerMetadata)) return undefined;\n const namespaced = providerMetadata[AG_UI_METADATA_NAMESPACE];\n return isObject(namespaced) ? namespaced : undefined;\n};\n\nconst readAgUiReasoningMeta = (providerMetadata: unknown) => {\n const namespaced = readAgUiNamespace(providerMetadata);\n if (!namespaced) return {};\n return {\n encryptedValue: getString(namespaced, \"encryptedValue\"),\n reasoningId: getString(namespaced, \"reasoningId\"),\n };\n};\n\n// AG-UI carries per-item extras in `metadata`; anything else on the item is\n// stripped by its schema. The part's own `filename` wins over a key of the\n// same name in the bag.\nconst buildInputMetadata = (\n part: Record<string, unknown>,\n filename?: string | undefined,\n) => {\n const metadata = {\n ...readAgUiNamespace(part.providerMetadata),\n ...(filename !== undefined && { filename }),\n };\n return Object.keys(metadata).length > 0 ? metadata : undefined;\n};\n\n// The inverse: `filename` lands on the part's own field, everything else the\n// item carried goes back into the namespace it came from, so a snapshot echo\n// resends what the host attached.\nconst readInputMetadata = (\n metadata: unknown,\n): {\n filename?: string | undefined;\n providerMetadata?: PartProviderMetadata | undefined;\n} => {\n if (!isObject(metadata)) return {};\n const { filename: _filename, ...rest } = metadata;\n return {\n filename: getString(metadata, \"filename\"),\n ...(Object.keys(rest).length > 0 && {\n providerMetadata: {\n [AG_UI_METADATA_NAMESPACE]: rest as PartProviderMetadata[string],\n },\n }),\n };\n};\n\nfunction parseJSONText(value: string): unknown {\n if (!value) return value;\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n}\n\nfunction generateId(): string {\n return (\n (globalThis.crypto as { randomUUID?: () => string })?.randomUUID?.() ??\n Math.random().toString(36).slice(2)\n );\n}\n\nfunction normalizeToolCall(part: ToolCallPart): {\n id: string;\n call: AgUiToolCall;\n} {\n const id = part.toolCallId ?? generateId();\n const argsText =\n typeof part.argsText === \"string\"\n ? part.argsText\n : JSON.stringify(part.args ?? {});\n\n return {\n id,\n call: {\n id,\n type: \"function\",\n function: {\n name: part.toolName ?? \"tool\",\n arguments: argsText,\n },\n },\n };\n}\n\nfunction extractText(content: unknown): string {\n if (typeof content === \"string\") return content;\n if (!Array.isArray(content)) return \"\";\n\n return content\n .filter(\n (part): part is { type: \"text\"; text: string } =>\n part?.type === \"text\" && typeof part?.text === \"string\",\n )\n .map((part) => part.text)\n .join(\"\\n\");\n}\n\ntype InputContentSource =\n | { type: \"data\"; value: string; mimeType: string }\n | { type: \"url\"; value: string; mimeType?: string };\n\ntype MediaInputType = \"image\" | \"audio\" | \"video\" | \"document\";\n\nfunction mediaTypeForMime(mimeType: string | undefined): MediaInputType {\n if (mimeType?.startsWith(\"image/\")) return \"image\";\n if (mimeType?.startsWith(\"audio/\")) return \"audio\";\n if (mimeType?.startsWith(\"video/\")) return \"video\";\n return \"document\";\n}\n\n// Build an AG-UI multimodal source from a data URL, raw base64 payload, or an\n// http(s) URL. A `url` source may omit the mime type; a `data` source always\n// resolves one (falling back to application/octet-stream). An explicit\n// `sourceType: \"url\"` on the part forces the url leg for non-http references.\nfunction buildInputSource(\n value: string,\n declaredMimeType: string | undefined,\n sourceType?: string,\n): InputContentSource {\n if (sourceType === \"url\" || httpUrlPattern.test(value)) {\n return declaredMimeType !== undefined\n ? { type: \"url\", value, mimeType: declaredMimeType }\n : { type: \"url\", value };\n }\n const parsed = parseDataUrl(value);\n return {\n type: \"data\",\n value: parsed?.data ?? value,\n mimeType:\n parsed?.mimeType ?? declaredMimeType ?? \"application/octet-stream\",\n };\n}\n\nfunction toInputContent(\n part: unknown,\n fallbackMimeType: string | undefined,\n): InputContent | null {\n if (!isObject(part)) return null;\n const type = getString(part, \"type\");\n\n if (type === \"text\") {\n const text = getString(part, \"text\");\n if (text === undefined) return null;\n return { type: \"text\", text };\n }\n\n if (type === \"image\") {\n const image = getString(part, \"image\");\n if (image === undefined) return null;\n const metadata = buildInputMetadata(part, getString(part, \"filename\"));\n return {\n type: \"image\",\n source: buildInputSource(image, fallbackMimeType),\n ...(metadata && { metadata }),\n };\n }\n\n if (type === \"file\") {\n const data = getString(part, \"data\");\n if (data === undefined) return null;\n const declaredMimeType = getString(part, \"mimeType\") || fallbackMimeType;\n const filename = getString(part, \"filename\");\n const source = buildInputSource(\n data,\n declaredMimeType,\n getString(part, \"sourceType\"),\n );\n const metadata = buildInputMetadata(part, filename);\n switch (mediaTypeForMime(source.mimeType)) {\n case \"image\":\n return { type: \"image\", source, ...(metadata && { metadata }) };\n case \"audio\":\n return { type: \"audio\", source, ...(metadata && { metadata }) };\n case \"video\":\n return { type: \"video\", source, ...(metadata && { metadata }) };\n default:\n return { type: \"document\", source, ...(metadata && { metadata }) };\n }\n }\n\n if (type === \"audio\") {\n const audio = part.audio;\n if (!isObject(audio)) return null;\n const data = getString(audio, \"data\");\n const format = getString(audio, \"format\");\n if (data === undefined || format === undefined) return null;\n return {\n type: \"audio\",\n source: buildInputSource(\n parseDataUrl(data)?.data ?? data,\n `audio/${format}`,\n ),\n };\n }\n\n return null;\n}\n\ntype SnapshotAttachment = NonNullable<\n CoreThreadMessageLike[\"attachments\"]\n>[number];\n\nconst mediaInputTypes = new Set([\"image\", \"audio\", \"video\", \"document\"]);\n\n// Inverse of buildInputSource.\nfunction inputSourceToString(\n value: unknown,\n): { value: string; mimeType?: string; isUrl?: boolean } | null {\n if (!isObject(value)) return null;\n const sourceValue = getString(value, \"value\");\n if (sourceValue === undefined) return null;\n const mimeType = getString(value, \"mimeType\");\n const type = getString(value, \"type\");\n if (type === \"url\") {\n return {\n value: sourceValue,\n isUrl: true,\n ...(mimeType !== undefined && { mimeType }),\n };\n }\n if (type === \"data\") {\n const resolvedMimeType = mimeType ?? \"application/octet-stream\";\n return {\n value: `data:${resolvedMimeType};base64,${sourceValue}`,\n mimeType: resolvedMimeType,\n };\n }\n return null;\n}\n\n// Mirrors @ag-ui/client's BackwardCompatibility_0_0_47 middleware.\nfunction upgradeBinaryInputPart(\n part: Record<string, unknown>,\n): Record<string, unknown> | null {\n const mimeType = getString(part, \"mimeType\");\n if (mimeType === undefined) return null;\n const data = getString(part, \"data\");\n const url = getString(part, \"url\");\n const source = data\n ? { type: \"data\", value: data, mimeType }\n : url\n ? { type: \"url\", value: url, mimeType }\n : null;\n if (!source) return null;\n const filename = getString(part, \"filename\");\n return {\n type: mediaTypeForMime(mimeType),\n source,\n ...(filename && { metadata: { filename } }),\n };\n}\n\nfunction toSnapshotAttachments(content: unknown): SnapshotAttachment[] {\n if (!Array.isArray(content)) return [];\n\n const attachments: SnapshotAttachment[] = [];\n for (const rawPart of content) {\n if (!isObject(rawPart)) continue;\n const part =\n getString(rawPart, \"type\") === \"binary\"\n ? upgradeBinaryInputPart(rawPart)\n : rawPart;\n if (!part) continue;\n const type = getString(part, \"type\");\n if (type === undefined || !mediaInputTypes.has(type)) continue;\n const source = inputSourceToString(part.source);\n if (!source) continue;\n\n const { filename, providerMetadata } = readInputMetadata(part.metadata);\n const id = attachments.length.toString();\n\n if (type === \"image\") {\n attachments.push({\n id,\n type: \"image\",\n name: filename ?? \"image\",\n ...(source.mimeType !== undefined && { contentType: source.mimeType }),\n status: { type: \"complete\" },\n content: [\n {\n type: \"image\",\n image: source.value,\n ...(filename !== undefined && { filename }),\n ...(providerMetadata && { providerMetadata }),\n },\n ],\n });\n continue;\n }\n\n const mimeType = source.mimeType ?? \"application/octet-stream\";\n attachments.push({\n id,\n type: type === \"document\" ? \"document\" : \"file\",\n name: filename ?? \"file\",\n contentType: mimeType,\n status: { type: \"complete\" },\n content: [\n {\n type: \"file\",\n data: source.value,\n mimeType,\n ...(source.isUrl && { sourceType: \"url\" as const }),\n ...(filename !== undefined && { filename }),\n ...(providerMetadata && { providerMetadata }),\n },\n ],\n });\n }\n return attachments;\n}\n\nfunction buildUserContent(message: ThreadMessageLike): string | InputContent[] {\n const contentParts = Array.isArray(message.content) ? message.content : [];\n\n const attachments = message.attachments ?? [];\n\n const converted: InputContent[] = [];\n\n // Promote string-form content to a leading text part so it survives when\n // non-text attachments are present (fromAgUiMessages emits string content).\n if (typeof message.content === \"string\" && message.content.length > 0) {\n converted.push({ type: \"text\", text: message.content });\n }\n\n for (const part of contentParts) {\n const input = toInputContent(part, undefined);\n if (input) converted.push(input);\n }\n for (const attachment of attachments) {\n if (!isObject(attachment)) continue;\n const attachmentContent = attachment.content;\n if (!Array.isArray(attachmentContent)) continue;\n const fallbackMime = getString(attachment, \"contentType\");\n for (const part of attachmentContent) {\n const input = toInputContent(part, fallbackMime);\n if (input) converted.push(input);\n }\n }\n\n const hasNonText = converted.some((part) => part.type !== \"text\");\n if (hasNonText) return converted;\n\n // All-text path: collapse to plain string. Join text parts collected from\n // both content and attachments so attachment-sourced text is not dropped.\n if (converted.length === 0) return extractText(message.content);\n return converted\n .filter(\n (part): part is { type: \"text\"; text: string } => part.type === \"text\",\n )\n .map((part) => part.text)\n .join(\"\\n\");\n}\n\nfunction toToolCallPart(value: unknown): ToolCallPart | null {\n if (!isObject(value)) return null;\n const rawFunction = isObject(value.function) ? value.function : null;\n const toolCallId = getString(value, \"toolCallId\") ?? getString(value, \"id\");\n const toolName =\n getString(value, \"toolName\") ??\n getString(value, \"name\") ??\n (rawFunction ? getString(rawFunction, \"name\") : undefined) ??\n \"tool\";\n const argsText =\n getString(value, \"argsText\") ??\n getString(value, \"arguments\") ??\n (rawFunction ? getString(rawFunction, \"arguments\") : undefined);\n\n const parsedArgs =\n typeof argsText === \"string\" ? parseJSONText(argsText) : undefined;\n const args =\n isObject(parsedArgs) && !Array.isArray(parsedArgs)\n ? (parsedArgs as ReadonlyJSONObject)\n : isObject(value.args) && !Array.isArray(value.args)\n ? (value.args as ReadonlyJSONObject)\n : undefined;\n\n const part: ToolCallPart = {\n type: \"tool-call\",\n ...(toolCallId !== undefined ? { toolCallId } : {}),\n toolName,\n argsText: argsText ?? JSON.stringify(args ?? {}),\n ...(args !== undefined ? { args } : {}),\n };\n\n if (value.type === \"tool-call\") {\n const result = value.result;\n const isError = value.isError;\n if (result !== undefined) part.result = result;\n if (typeof isError === \"boolean\") part.isError = isError;\n }\n\n return part;\n}\n\nfunction extractAssistantToolCalls(\n message: Record<string, unknown>,\n): ToolCallPart[] {\n const parts: ToolCallPart[] = [];\n const seenToolCallIds = new Set<string>();\n const pushPart = (part: ToolCallPart | null) => {\n if (!part) return;\n const id = part.toolCallId ?? generateId();\n if (seenToolCallIds.has(id)) return;\n seenToolCallIds.add(id);\n parts.push({\n ...part,\n toolCallId: id,\n });\n };\n\n const content = message.content;\n if (Array.isArray(content)) {\n for (const part of content) {\n if (isObject(part) && part.type === \"tool-call\") {\n pushPart(toToolCallPart(part));\n }\n }\n }\n\n const toolCalls = Array.isArray(message.toolCalls)\n ? message.toolCalls\n : Array.isArray(message.tool_calls)\n ? message.tool_calls\n : [];\n for (const call of toolCalls) {\n pushPart(toToolCallPart(call));\n }\n\n return parts;\n}\n\nfunction validateInterrupts(value: unknown): AgUiInterrupt[] | undefined {\n if (!Array.isArray(value)) return undefined;\n const valid = value.filter(\n (entry): entry is AgUiInterrupt =>\n isObject(entry) &&\n typeof entry.id === \"string\" &&\n typeof entry.reason === \"string\",\n );\n return valid.length > 0 ? valid : undefined;\n}\n\nfunction readPersistedInterrupts(\n metadata: unknown,\n): AgUiInterrupt[] | undefined {\n if (!isObject(metadata)) return undefined;\n const custom = metadata.custom;\n if (!isObject(custom)) return undefined;\n const namespaced = custom[AG_UI_METADATA_NAMESPACE];\n if (!isObject(namespaced)) return undefined;\n return validateInterrupts(namespaced.interrupts);\n}\n\nfunction readOpaqueReasoning(metadata: unknown): AgUiOpaqueReasoning[] {\n if (!isObject(metadata)) return [];\n const custom = metadata.custom;\n if (!isObject(custom)) return [];\n const namespaced = custom[AG_UI_METADATA_NAMESPACE];\n if (!isObject(namespaced)) return [];\n const entries = namespaced.opaqueReasoning;\n if (!Array.isArray(entries)) return [];\n return entries.flatMap((entry) => {\n if (!isObject(entry)) return [];\n const id = getString(entry, \"id\");\n const encryptedValue = getString(entry, \"encryptedValue\");\n if (!id?.trim() || !encryptedValue?.trim()) return [];\n return [\n { id, encryptedValue, ...(entry.after === true ? { after: true } : {}) },\n ];\n });\n}\n\nfunction withOpaqueReasoning(\n message: CoreThreadMessageLike,\n opaqueReasoning: AgUiOpaqueReasoning[],\n): CoreThreadMessageLike {\n const metadata = isObject(message.metadata) ? message.metadata : {};\n const custom = isObject(metadata.custom) ? metadata.custom : {};\n const namespaced = isObject(custom[AG_UI_METADATA_NAMESPACE])\n ? (custom[AG_UI_METADATA_NAMESPACE] as Record<string, unknown>)\n : {};\n return {\n ...message,\n metadata: {\n ...metadata,\n custom: {\n ...custom,\n [AG_UI_METADATA_NAMESPACE]: { ...namespaced, opaqueReasoning },\n },\n },\n } as CoreThreadMessageLike;\n}\n\nfunction toAssistantSnapshotMessage(\n rawMessage: Record<string, unknown>,\n): CoreThreadMessageLike {\n const text = extractText(rawMessage.content);\n const interrupts = readPersistedInterrupts(rawMessage.metadata);\n const restoredToolCalls = extractAssistantToolCalls(rawMessage);\n const approvals = projectAgUiToolApprovals(\n interrupts,\n new Set(\n restoredToolCalls\n .map((part) => part.toolCallId)\n .filter((id): id is string => !!id),\n ),\n );\n const toolCallParts = restoredToolCalls.map((part) => {\n const approval = part.toolCallId\n ? approvals.get(part.toolCallId)\n : undefined;\n return approval ? { ...part, approval } : part;\n });\n const assistantContent = [\n ...(text.length > 0 ? [{ type: \"text\" as const, text }] : []),\n ...toolCallParts,\n ];\n const messageName = getString(rawMessage, \"name\");\n return {\n id: getString(rawMessage, \"id\") ?? generateId(),\n role: \"assistant\",\n content: assistantContent.length > 0 ? assistantContent : \"\",\n ...(messageName !== undefined ? { name: messageName } : {}),\n ...(interrupts\n ? {\n metadata: {\n custom: {\n [AG_UI_METADATA_NAMESPACE]: {\n interrupts,\n } satisfies AgUiCustomMetadata,\n },\n },\n }\n : {}),\n };\n}\n\nfunction toUserOrSystemSnapshotMessage(\n role: \"user\" | \"system\",\n rawMessage: Record<string, unknown>,\n): CoreThreadMessageLike {\n const messageName = getString(rawMessage, \"name\");\n const attachments =\n role === \"user\" ? toSnapshotAttachments(rawMessage.content) : [];\n return {\n id: getString(rawMessage, \"id\") ?? generateId(),\n role,\n content: extractText(rawMessage.content),\n ...(messageName !== undefined ? { name: messageName } : {}),\n ...(attachments.length > 0 ? { attachments } : {}),\n };\n}\n\n// Rebuilds the a2ui:<surfaceId> \"present\" tool-call parts for one owning\n// assistant message from a bucket of rebuilt surface state, mirroring the\n// live RunAggregator.synthesizeA2uiToolCalls shape. Non-a2ui parts (text,\n// other tool calls) are preserved; existing a2ui parts are replaced wholesale\n// so create/update/delete within the bucket all converge on the rebuilt set.\nfunction attachA2uiSurfaces(\n message: CoreThreadMessageLike,\n state: A2uiState,\n): CoreThreadMessageLike {\n const a2uiParts: ToolCallPart[] = [];\n for (const [surfaceId, surface] of state) {\n const { spec } = convertSurfaceToUISpec(surface);\n if (!spec) continue;\n a2uiParts.push({\n type: \"tool-call\",\n toolCallId: `a2ui:${surfaceId}`,\n toolName: \"present\",\n args: spec as unknown as ReadonlyJSONObject,\n argsText: JSON.stringify(spec),\n result: {},\n });\n }\n\n const content = Array.isArray(message.content) ? message.content : [];\n const preserved = content.filter(\n (part) =>\n !(\n isObject(part) &&\n part.type === \"tool-call\" &&\n typeof part.toolCallId === \"string\" &&\n part.toolCallId.startsWith(\"a2ui:\")\n ),\n );\n return { ...message, content: [...preserved, ...a2uiParts] };\n}\n\nexport type FromAgUiMessagesOptions = {\n /**\n * Whether to convert `reasoning` messages into visible reasoning parts.\n * Matches the `showThinking` option of `useAgUiRuntime`. Defaults to `true`.\n */\n showThinking?: boolean;\n};\n\nexport function fromAgUiMessages(\n messages: readonly unknown[],\n options?: FromAgUiMessagesOptions,\n): CoreThreadMessageLike[] {\n const showThinking = options?.showThinking ?? true;\n const converted: CoreThreadMessageLike[] = [];\n const a2uiBuckets = new Map<string, A2uiState>();\n const a2uiBucketOwnerIndices = new Map<string, number>();\n // A zero-data-retention run carries its payload in encryptedValue with no\n // readable content, so the record has no part to become and rides on the\n // neighbouring message instead of being dropped. The anchor is the index the\n // next pushed message will occupy, which is where it sat on the wire.\n const opaqueReasoning: (AgUiOpaqueReasoning & { anchor: number })[] = [];\n\n for (const rawMessage of messages) {\n if (!isObject(rawMessage)) continue;\n const role = getString(rawMessage, \"role\");\n if (!role) continue;\n\n if (role === \"tool\") {\n const toolCallId = getToolCallId(rawMessage) ?? `tool-${generateId()}`;\n const toolMessageId = getString(rawMessage, \"id\");\n const modelContent = extractText(rawMessage.content);\n const mcpResult = parseMcpToolCallResult(rawMessage, modelContent);\n const mcpModelContent = mcpResult\n ? [{ type: \"text\" as const, text: modelContent }]\n : undefined;\n const result =\n mcpResult ??\n (rawMessage.result !== undefined\n ? rawMessage.result\n : typeof rawMessage.content === \"string\"\n ? parseJSONText(rawMessage.content)\n : rawMessage.content);\n const isError =\n typeof rawMessage.error === \"string\" ||\n rawMessage.isError === true ||\n rawMessage.status === \"error\"\n ? true\n : rawMessage.isError === false\n ? false\n : undefined;\n const mcpAppUri = readMcpAppResourceUri(mcpResult?._meta);\n const mcpApp =\n mcpAppUri !== undefined ? { resourceUri: mcpAppUri } : undefined;\n\n let updated = false;\n for (\n let messageIndex = converted.length - 1;\n messageIndex >= 0 && !updated;\n messageIndex--\n ) {\n const message = converted[messageIndex];\n if (\n !message ||\n message.role !== \"assistant\" ||\n !Array.isArray(message.content)\n )\n continue;\n\n for (\n let partIndex = message.content.length - 1;\n partIndex >= 0;\n partIndex--\n ) {\n const part = message.content[partIndex];\n if (!isObject(part) || part.type !== \"tool-call\") continue;\n if (getString(part, \"toolCallId\") !== toolCallId) continue;\n\n const updatedPart: ToolCallPart = {\n ...(part as ToolCallPart),\n result,\n ...(mcpModelContent ? { modelContent: mcpModelContent } : {}),\n ...(isError !== undefined ? { isError } : {}),\n ...(toolMessageId !== undefined\n ? { unstable_toolMessageId: toolMessageId }\n : {}),\n ...(mcpApp ? { mcp: { app: mcpApp } } : {}),\n };\n const updatedContent = message.content.map((contentPart, index) =>\n index === partIndex ? updatedPart : contentPart,\n );\n converted[messageIndex] = { ...message, content: updatedContent };\n updated = true;\n break;\n }\n }\n\n if (updated) {\n continue;\n }\n\n const id = toolMessageId ?? toolCallId;\n const toolName =\n getString(rawMessage, \"name\") ??\n getString(rawMessage, \"toolName\") ??\n \"tool\";\n converted.push({\n id: `${id}:assistant`,\n role: \"assistant\",\n content: [\n {\n type: \"tool-call\",\n toolCallId,\n toolName,\n args: {},\n argsText: \"{}\",\n result,\n ...(mcpModelContent ? { modelContent: mcpModelContent } : {}),\n ...(isError !== undefined ? { isError } : {}),\n ...(toolMessageId !== undefined\n ? { unstable_toolMessageId: toolMessageId }\n : {}),\n ...(mcpApp ? { mcp: { app: mcpApp } } : {}),\n },\n ],\n });\n continue;\n }\n\n if (role === \"activity\") {\n // Only a2ui-surface activity messages have an assistant-part equivalent\n // to rehydrate; other activity types still have no surface to repaint.\n const activityType = getString(rawMessage, \"activityType\");\n if (activityType !== A2UI_SURFACE_ACTIVITY_TYPE) continue;\n const activityContent = isObject(rawMessage.content)\n ? (rawMessage.content as Record<string, unknown>)\n : null;\n const operations = activityContent?.[\"a2ui_operations\"];\n if (!Array.isArray(operations)) continue;\n\n let ownerIndex = -1;\n for (let i = converted.length - 1; i >= 0; i--) {\n const candidate = converted[i];\n if (candidate && candidate.role === \"assistant\") {\n ownerIndex = i;\n break;\n }\n }\n if (ownerIndex === -1) continue;\n\n const owner = converted[ownerIndex]!;\n const bucketKey = getString(rawMessage, \"id\") ?? \"a2ui:anonymous\";\n const { state } = applyA2uiOperations(new Map(), operations);\n a2uiBuckets.delete(bucketKey);\n a2uiBucketOwnerIndices.delete(bucketKey);\n a2uiBuckets.set(bucketKey, state);\n a2uiBucketOwnerIndices.set(bucketKey, ownerIndex);\n\n const ownerState = new Map<string, A2uiSurfaceState>();\n for (const [candidateBucketKey, candidateState] of a2uiBuckets) {\n if (a2uiBucketOwnerIndices.get(candidateBucketKey) !== ownerIndex)\n continue;\n for (const [surfaceId, surface] of candidateState) {\n ownerState.set(surfaceId, surface);\n }\n }\n converted[ownerIndex] = attachA2uiSurfaces(owner, ownerState);\n continue;\n }\n\n if (role === \"assistant\") {\n converted.push(toAssistantSnapshotMessage(rawMessage));\n continue;\n }\n\n if (role === \"reasoning\") {\n const text = extractText(rawMessage.content);\n const encryptedValue = getString(rawMessage, \"encryptedValue\");\n if (text.trim().length === 0) {\n // showThinking hides reasoning; this record is never rendered anyway,\n // and dropping it would deny the provider the payload it needs to\n // accept the next run.\n const opaqueId = getString(rawMessage, \"id\");\n if (opaqueId?.trim() && encryptedValue?.trim()) {\n opaqueReasoning.push({\n id: opaqueId,\n encryptedValue,\n anchor: converted.length,\n });\n }\n continue;\n }\n // Gate visible reasoning on showThinking so a cold reload matches the\n // live run: the aggregator never stores reasoning parts when it is off.\n // A signature on a hidden record is still transport state, so it is kept\n // opaque rather than discarded with the text.\n if (!showThinking) {\n const hiddenId = getString(rawMessage, \"id\");\n if (hiddenId?.trim() && encryptedValue?.trim()) {\n opaqueReasoning.push({\n id: hiddenId,\n encryptedValue,\n anchor: converted.length,\n });\n }\n continue;\n }\n converted.push({\n id: getString(rawMessage, \"id\") ?? generateId(),\n role: \"assistant\",\n content: [\n {\n type: \"reasoning\",\n text,\n ...(encryptedValue !== undefined\n ? {\n providerMetadata: {\n [AG_UI_METADATA_NAMESPACE]: { encryptedValue },\n },\n }\n : {}),\n },\n ],\n });\n continue;\n }\n\n if (role === \"user\" || role === \"system\") {\n converted.push(toUserOrSystemSnapshotMessage(role, rawMessage));\n }\n }\n\n for (const { anchor, ...entry } of opaqueReasoning) {\n // Nothing followed it on the wire, so it trails the last message instead.\n const trailing = anchor >= converted.length;\n const index = trailing ? converted.length - 1 : anchor;\n const target = converted[index];\n if (!target) continue;\n converted[index] = withOpaqueReasoning(target, [\n ...readOpaqueReasoning(target.metadata),\n ...(trailing ? [{ ...entry, after: true }] : [entry]),\n ]);\n }\n\n for (let i = 0; i < converted.length; i++) {\n const message = converted[i]!;\n if (message.role !== \"assistant\") continue;\n\n const hasInterrupt =\n readPersistedInterrupts(message.metadata) !== undefined;\n const hasPendingToolCall =\n Array.isArray(message.content) &&\n message.content.some(\n (part) =>\n isObject(part) &&\n part.type === \"tool-call\" &&\n part.result === undefined,\n );\n\n if (hasInterrupt || hasPendingToolCall) {\n converted[i] = {\n ...message,\n status: getAutoStatus(\n false,\n false,\n hasInterrupt,\n hasPendingToolCall,\n undefined,\n ),\n };\n }\n }\n\n return converted;\n}\n\nfunction convertAssistantMessage(\n message: NormalizedThreadMessageLike,\n converted: AgUiMessage[],\n): void {\n const content = extractText(message.content);\n const contentArray = Array.isArray(message.content) ? message.content : [];\n\n const toolCallParts = contentArray.filter(\n (part): part is ToolCallPart =>\n part?.type === \"tool-call\" &&\n !(\n typeof part.toolCallId === \"string\" &&\n part.toolCallId.startsWith(\"a2ui:\")\n ),\n );\n\n const toolCalls = toolCallParts.map((part) => ({\n ...normalizeToolCall(part),\n part,\n }));\n\n // An AG-UI assistant record has no reasoning field, so reasoning leaves as\n // the standalone record it arrived as, ahead of the assistant it belongs to.\n const shellIsDropped = content.length === 0 && toolCalls.length === 0;\n let reasoningIndex = 0;\n for (const part of contentArray) {\n if (!isObject(part) || part.type !== \"reasoning\") continue;\n const text = getString(part, \"text\") ?? \"\";\n if (text.trim().length === 0) continue;\n const { encryptedValue, reasoningId } = readAgUiReasoningMeta(\n part.providerMetadata,\n );\n converted.push({\n // The wire id is what a signature was issued against, so it wins over a\n // synthesized one whenever the run carried it.\n id: reasoningId?.trim()\n ? reasoningId\n : shellIsDropped && reasoningIndex === 0\n ? message.id\n : `${message.id}:reasoning-${reasoningIndex}`,\n role: \"reasoning\",\n content: text,\n ...(encryptedValue !== undefined ? { encryptedValue } : {}),\n });\n reasoningIndex++;\n }\n\n // Drop assistant messages with no text or tool calls (e.g. an imported\n // reasoning-only entry) so they are not re-sent as a blank assistant turn.\n if (shellIsDropped) {\n return;\n }\n\n converted.push({\n id: message.id,\n role: \"assistant\",\n content,\n ...(message.name ? { name: message.name } : {}),\n ...(toolCalls.length > 0\n ? { toolCalls: toolCalls.map((entry) => entry.call) }\n : {}),\n });\n\n for (const { id: toolCallId, part } of toolCalls) {\n if (part.result === undefined) continue;\n\n const resultContent =\n part.modelContent !== undefined\n ? extractText(part.modelContent)\n : typeof part.result === \"string\"\n ? part.result\n : JSON.stringify(part.result);\n\n converted.push({\n id: part.unstable_toolMessageId ?? `${toolCallId}:tool`,\n role: \"tool\",\n content: resultContent,\n toolCallId,\n ...(part.isError ? { error: resultContent } : {}),\n });\n }\n}\n\nfunction convertToolMessage(\n message: NormalizedThreadMessageLike,\n converted: AgUiMessage[],\n): void {\n const content = extractText(message.content);\n const toolCallId = message.toolCallId ?? generateId();\n\n converted.push({\n id: message.id,\n role: \"tool\",\n content,\n toolCallId,\n ...(typeof message.error === \"string\" ? { error: message.error } : {}),\n });\n}\n\nexport function toAgUiMessages(\n messages: readonly ThreadMessageLike[],\n): AgUiMessage[] {\n const converted: AgUiMessage[] = [];\n\n for (const rawMessage of messages) {\n const message: NormalizedThreadMessageLike = {\n ...rawMessage,\n id: rawMessage.id ?? generateId(),\n };\n const opaqueReasoning = readOpaqueReasoning(message.metadata);\n const toOpaqueRecord = (entry: AgUiOpaqueReasoning): AgUiMessage => ({\n id: entry.id,\n role: \"reasoning\",\n content: \"\",\n encryptedValue: entry.encryptedValue,\n });\n for (const entry of opaqueReasoning) {\n if (entry.after !== true) converted.push(toOpaqueRecord(entry));\n }\n const flushTrailingOpaqueReasoning = () => {\n for (const entry of opaqueReasoning) {\n if (entry.after === true) converted.push(toOpaqueRecord(entry));\n }\n };\n\n if (message.role === \"assistant\") {\n convertAssistantMessage(message, converted);\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n if (message.role === \"tool\") {\n convertToolMessage(message, converted);\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n if (message.role === \"user\") {\n converted.push({\n id: message.id,\n role: \"user\",\n content: buildUserContent(message),\n ...(message.name ? { name: message.name } : {}),\n });\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n if (message.role === \"system\" || message.role === \"developer\") {\n converted.push({\n id: message.id,\n role: message.role,\n content: extractText(message.content),\n ...(message.name ? { name: message.name } : {}),\n });\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n if (message.role === \"reasoning\") {\n converted.push({\n id: message.id,\n role: \"reasoning\",\n content: extractText(message.content),\n });\n flushTrailingOpaqueReasoning();\n continue;\n }\n\n flushTrailingOpaqueReasoning();\n }\n\n return converted;\n}\n\ntype AgUiTool = NonNullable<RunAgentParameters[\"tools\"]>[number];\n\nexport function toAgUiTools(\n tools: Record<string, Tool> | undefined,\n): AgUiTool[] {\n if (!tools) return [];\n\n const toolsSchema = toToolsJSONSchema(tools);\n return Object.entries(toolsSchema).map(([name, tool]) => ({\n name,\n description: tool.description ?? \"\",\n parameters: tool.parameters,\n }));\n}\n"],"mappings":";;;;;;;;AA6GA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU;AAEzC,MAAM,aAAa,QAAiC,QAAgB;CAClE,MAAM,QAAQ,OAAO;CACrB,OAAO,OAAO,UAAU,WAAW,QAAQ,KAAA;AAC7C;AAEA,MAAM,iBAAiB,WACrB,UAAU,QAAQ,YAAY,KAAK,UAAU,QAAQ,cAAc;AAErE,MAAM,qBAAqB,qBAA8B;CACvD,IAAI,CAAC,SAAS,gBAAgB,GAAG,OAAO,KAAA;CACxC,MAAM,aAAa,iBAAiB;CACpC,OAAO,SAAS,UAAU,IAAI,aAAa,KAAA;AAC7C;AAEA,MAAM,yBAAyB,qBAA8B;CAC3D,MAAM,aAAa,kBAAkB,gBAAgB;CACrD,IAAI,CAAC,YAAY,OAAO,CAAC;CACzB,OAAO;EACL,gBAAgB,UAAU,YAAY,gBAAgB;EACtD,aAAa,UAAU,YAAY,aAAa;CAClD;AACF;AAKA,MAAM,sBACJ,MACA,aACG;CACH,MAAM,WAAW;EACf,GAAG,kBAAkB,KAAK,gBAAgB;EAC1C,GAAI,aAAa,KAAA,KAAa,EAAE,SAAS;CAC3C;CACA,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,SAAS,IAAI,WAAW,KAAA;AACvD;AAKA,MAAM,qBACJ,aAIG;CACH,IAAI,CAAC,SAAS,QAAQ,GAAG,OAAO,CAAC;CACjC,MAAM,EAAE,UAAU,WAAW,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AAQ;IACZ,MAAM;IACN,SAAS,YAAY,QAAQ,OAAO;GACtC,CAAC;GACD,6BAA6B;GAC7B;EACF;EAEA,6BAA6B;CAC/B;CAEA,OAAO;AACT;AAIA,SAAgB,YACd,OACY;CACZ,IAAI,CAAC,OAAO,OAAO,CAAC;CAEpB,MAAM,cAAc,kBAAkB,KAAK;CAC3C,OAAO,OAAO,QAAQ,WAAW,CAAC,CAAC,KAAK,CAAC,MAAM,WAAW;EACxD;EACA,aAAa,KAAK,eAAe;EACjC,YAAY,KAAK;CACnB,EAAE;AACJ"}
@@ -26,7 +26,11 @@ type RunAggregatorOptions = {
26
26
  * Collects AG-UI events into assistant-ui run snapshots that can be yielded from a ChatModelAdapter.
27
27
  *
28
28
  * The aggregator keeps a single assistant message worth of parts. Each incoming event updates the parts and
29
- * emits a fresh snapshot through the provided `emit` callback.
29
+ * emits a fresh snapshot through the provided `emit` callback. `CUSTOM` events
30
+ * become canonical data parts; integration plumbing names such as
31
+ * `on_interrupt`, `PredictState`, `Exit`, `hook_error`, `state_update_error`,
32
+ * `system:*`, and `MultiAgentHandoff` are forwarded the same way, so apps only
33
+ * need data renderers for names they own.
30
34
  */
31
35
  declare class RunAggregator {
32
36
  private readonly emitUpdate;
@@ -1 +1 @@
1
- {"version":3,"file":"run-aggregator.d.ts","names":[],"sources":["../../../src/runtime/adapter/run-aggregator.ts"],"mappings":";;;;cAoBa;KAED;EACV;EACA;EACA;;KAGU;EACV,aAAa;EACb,kBAAkB;;KAGf,QAAQ,QAAQ;cAiBR;cAEA;cAEA,gBACX,mBACC,SAAS;KAGA;EACV;EACA,QAAQ;EACR,MAAM;EACN,qBAAqB;;;;;;;;cASV;mBACM;mBACA;mBACA;mBACA;UAET;UACA;mBACS;UAIT;mBACS;mBACA;mBACA;mBACA;UACT;UACA;mBACS;mBACA;mBACA;UACT;mBACS;UAKT;UACA;UAEA;UACA;UACA;EAEI,YAAA,SAAS;EAOrB,YAAY;EAIZ,OAAO,OAAO;UA4ON;UAmBA;UA+CA;UAMA;UAIA;UAKA;UAOA;UAiBA;UAWA;UAMA;UAQA;UAiCA;UA2BA;UAgBA;UAsDA;UAwFA;UASA;UAgCA;UAsBA;UAgBA;;iBAOM,aAAa"}
1
+ {"version":3,"file":"run-aggregator.d.ts","names":[],"sources":["../../../src/runtime/adapter/run-aggregator.ts"],"mappings":";;;;cAqBa;KAED;EACV;EACA;EACA;;KAGU;EACV,aAAa;EACb,kBAAkB;;KAGf,QAAQ,QAAQ;cAiBR;cAEA;cAEA,gBACX,mBACC,SAAS;KAGA;EACV;EACA,QAAQ;EACR,MAAM;EACN,qBAAqB;;;;;;;;;;;;cAaV;mBACM;mBACA;mBACA;mBACA;UAET;UACA;mBACS;UAIT;mBACS;mBACA;mBACA;mBACA;UACT;UACA;mBACS;mBACA;mBACA;UACT;mBACS;UAMT;UACA;UAEA;UACA;UACA;EAEI,YAAA,SAAS;EAOrB,YAAY;EAIZ,OAAO,OAAO;UAuPN;UAmBA;UA+CA;UAMA;UAIA;UAKA;UAOA;UAiBA;UAWA;UAMA;UAQA;UAiCA;UA2BA;UAgBA;UAsDA;UA2GA;UASA;UAgCA;UAsBA;UAgBA;;iBAOM,aAAa"}
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
  import { readMcpAppResourceUri } from "../mcp-tool-result.js";
3
+ import { projectAgUiToolApprovals } from "./tool-approval.js";
3
4
  import { isMcpAppUri } from "@assistant-ui/core";
4
5
  import { applyA2uiOperations, convertSurfaceToUISpec } from "@assistant-ui/react-generative-ui/a2ui";
5
6
  //#region src/runtime/adapter/run-aggregator.ts
@@ -11,7 +12,11 @@ const isPlainObject = (value) => !!value && typeof value === "object" && !Array.
11
12
  * Collects AG-UI events into assistant-ui run snapshots that can be yielded from a ChatModelAdapter.
12
13
  *
13
14
  * The aggregator keeps a single assistant message worth of parts. Each incoming event updates the parts and
14
- * emits a fresh snapshot through the provided `emit` callback.
15
+ * emits a fresh snapshot through the provided `emit` callback. `CUSTOM` events
16
+ * become canonical data parts; integration plumbing names such as
17
+ * `on_interrupt`, `PredictState`, `Exit`, `hook_error`, `state_update_error`,
18
+ * `system:*`, and `MultiAgentHandoff` are forwarded the same way, so apps only
19
+ * need data renderers for names they own.
15
20
  */
16
21
  var RunAggregator = class {
17
22
  emitUpdate;
@@ -133,6 +138,15 @@ var RunAggregator = class {
133
138
  if (event.messageId && this.activeTextMessageId === event.messageId) this.activeTextMessageId = void 0;
134
139
  this.emit();
135
140
  break;
141
+ case "CUSTOM":
142
+ this.activeTextMessageId = void 0;
143
+ this.partOrder.push({
144
+ kind: "data",
145
+ name: event.name,
146
+ value: event.value
147
+ });
148
+ this.emit();
149
+ break;
136
150
  case "THINKING_START":
137
151
  case "THINKING_TEXT_MESSAGE_START":
138
152
  case "REASONING_START":
@@ -413,6 +427,7 @@ var RunAggregator = class {
413
427
  }
414
428
  emit() {
415
429
  const snapshot = [];
430
+ const approvals = projectAgUiToolApprovals(this.status?.type === "requires-action" ? this.interrupts : void 0, new Set(this.toolCalls.keys()));
416
431
  for (const part of this.partOrder) {
417
432
  if (part.kind === "reasoning") {
418
433
  if (this.showThinking) {
@@ -441,14 +456,24 @@ var RunAggregator = class {
441
456
  });
442
457
  continue;
443
458
  }
459
+ if (part.kind === "data") {
460
+ snapshot.push({
461
+ type: "data",
462
+ name: part.name,
463
+ data: part.value
464
+ });
465
+ continue;
466
+ }
444
467
  const entry = this.toolCalls.get(part.toolCallId);
445
468
  if (!entry) continue;
469
+ const approval = approvals.get(entry.toolCallId);
446
470
  const toolPart = {
447
471
  type: "tool-call",
448
472
  toolCallId: entry.toolCallId,
449
473
  toolName: entry.toolCallName,
450
474
  args: entry.parsedArgs ?? {},
451
475
  argsText: entry.argsText,
476
+ ...approval ? { approval } : {},
452
477
  ...entry.result !== void 0 ? { result: entry.result } : {},
453
478
  ...entry.modelContent !== void 0 ? { modelContent: entry.modelContent } : {},
454
479
  ...entry.isError !== void 0 ? { isError: entry.isError } : {},