@nhtio/adk 1.20260609.1 → 1.20260610.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.
Files changed (129) hide show
  1. package/CHANGELOG.md +78 -0
  2. package/batteries/llm/ollama/helpers.cjs +9 -0
  3. package/batteries/llm/ollama/helpers.cjs.map +1 -1
  4. package/batteries/llm/ollama/helpers.mjs +9 -0
  5. package/batteries/llm/ollama/helpers.mjs.map +1 -1
  6. package/batteries/llm/openai_chat_completions/helpers.cjs +19 -0
  7. package/batteries/llm/openai_chat_completions/helpers.cjs.map +1 -1
  8. package/batteries/llm/openai_chat_completions/helpers.mjs +19 -0
  9. package/batteries/llm/openai_chat_completions/helpers.mjs.map +1 -1
  10. package/batteries/media/builder.d.ts +245 -0
  11. package/batteries/media/contracts.cjs +119 -0
  12. package/batteries/media/contracts.cjs.map +1 -0
  13. package/batteries/media/contracts.d.ts +321 -0
  14. package/batteries/media/contracts.mjs +110 -0
  15. package/batteries/media/contracts.mjs.map +1 -0
  16. package/batteries/media/engines/audio_decode.cjs +92 -0
  17. package/batteries/media/engines/audio_decode.cjs.map +1 -0
  18. package/batteries/media/engines/audio_decode.d.ts +46 -0
  19. package/batteries/media/engines/audio_decode.mjs +90 -0
  20. package/batteries/media/engines/audio_decode.mjs.map +1 -0
  21. package/batteries/media/engines/execa_executor.cjs +64 -0
  22. package/batteries/media/engines/execa_executor.cjs.map +1 -0
  23. package/batteries/media/engines/execa_executor.d.ts +54 -0
  24. package/batteries/media/engines/execa_executor.mjs +62 -0
  25. package/batteries/media/engines/execa_executor.mjs.map +1 -0
  26. package/batteries/media/engines/fs_workspace.cjs +84 -0
  27. package/batteries/media/engines/fs_workspace.cjs.map +1 -0
  28. package/batteries/media/engines/fs_workspace.d.ts +51 -0
  29. package/batteries/media/engines/fs_workspace.mjs +82 -0
  30. package/batteries/media/engines/fs_workspace.mjs.map +1 -0
  31. package/batteries/media/engines/jimp.cjs +116 -0
  32. package/batteries/media/engines/jimp.cjs.map +1 -0
  33. package/batteries/media/engines/jimp.d.ts +32 -0
  34. package/batteries/media/engines/jimp.mjs +114 -0
  35. package/batteries/media/engines/jimp.mjs.map +1 -0
  36. package/batteries/media/engines/sharp.cjs +120 -0
  37. package/batteries/media/engines/sharp.cjs.map +1 -0
  38. package/batteries/media/engines/sharp.d.ts +42 -0
  39. package/batteries/media/engines/sharp.mjs +117 -0
  40. package/batteries/media/engines/sharp.mjs.map +1 -0
  41. package/batteries/media/engines/soffice.cjs +246 -0
  42. package/batteries/media/engines/soffice.cjs.map +1 -0
  43. package/batteries/media/engines/soffice.d.ts +39 -0
  44. package/batteries/media/engines/soffice.mjs +244 -0
  45. package/batteries/media/engines/soffice.mjs.map +1 -0
  46. package/batteries/media/engines/tesseract_js.cjs +87 -0
  47. package/batteries/media/engines/tesseract_js.cjs.map +1 -0
  48. package/batteries/media/engines/tesseract_js.d.ts +41 -0
  49. package/batteries/media/engines/tesseract_js.mjs +85 -0
  50. package/batteries/media/engines/tesseract_js.mjs.map +1 -0
  51. package/batteries/media/engines/transformers_asr.cjs +111 -0
  52. package/batteries/media/engines/transformers_asr.cjs.map +1 -0
  53. package/batteries/media/engines/transformers_asr.d.ts +41 -0
  54. package/batteries/media/engines/transformers_asr.mjs +109 -0
  55. package/batteries/media/engines/transformers_asr.mjs.map +1 -0
  56. package/batteries/media/exceptions.d.ts +103 -0
  57. package/batteries/media/forge.cjs +403 -0
  58. package/batteries/media/forge.cjs.map +1 -0
  59. package/batteries/media/forge.d.ts +90 -0
  60. package/batteries/media/forge.mjs +399 -0
  61. package/batteries/media/forge.mjs.map +1 -0
  62. package/batteries/media/formats.d.ts +72 -0
  63. package/batteries/media/index.d.ts +136 -0
  64. package/batteries/media/lint.cjs +339 -0
  65. package/batteries/media/lint.cjs.map +1 -0
  66. package/batteries/media/lint.d.ts +117 -0
  67. package/batteries/media/lint.mjs +331 -0
  68. package/batteries/media/lint.mjs.map +1 -0
  69. package/batteries/media/pipe.d.ts +66 -0
  70. package/batteries/media/plan.d.ts +133 -0
  71. package/batteries/media/registry.d.ts +92 -0
  72. package/batteries/media/runtime.d.ts +105 -0
  73. package/batteries/media/steps/doc.d.ts +33 -0
  74. package/batteries/media/steps/image_audio.d.ts +24 -0
  75. package/batteries/media/steps/ingest.d.ts +25 -0
  76. package/batteries/media/steps/pages.d.ts +18 -0
  77. package/batteries/media/steps/sheet.d.ts +36 -0
  78. package/batteries/media/steps/slides.d.ts +35 -0
  79. package/batteries/media/steps/text.d.ts +43 -0
  80. package/batteries/media/validate.d.ts +49 -0
  81. package/batteries/media/verbs.d.ts +126 -0
  82. package/batteries/media.cjs +3049 -0
  83. package/batteries/media.cjs.map +1 -0
  84. package/batteries/media.mjs +3009 -0
  85. package/batteries/media.mjs.map +1 -0
  86. package/batteries/tools/_shared/index.d.ts +21 -0
  87. package/batteries/tools/_shared.cjs +16 -0
  88. package/batteries/tools/_shared.cjs.map +1 -1
  89. package/batteries/tools/_shared.mjs +16 -1
  90. package/batteries/tools/_shared.mjs.map +1 -1
  91. package/batteries/tools/scrapper/shared.d.ts +8 -1
  92. package/batteries/tools/scrapper.cjs +1 -1
  93. package/batteries/tools/scrapper.mjs +1 -1
  94. package/batteries/tools/searxng/index.d.ts +8 -1
  95. package/batteries/tools/searxng.cjs +1 -1
  96. package/batteries/tools/searxng.mjs +1 -1
  97. package/batteries/tools.cjs +2 -2
  98. package/batteries/tools.mjs +2 -2
  99. package/batteries.cjs +2 -2
  100. package/batteries.mjs +2 -2
  101. package/eslint/rules.cjs +1 -1
  102. package/eslint/rules.mjs +1 -1
  103. package/eslint.cjs +2 -2
  104. package/eslint.mjs +2 -2
  105. package/exceptions-C7FSHEnV.mjs +87 -0
  106. package/exceptions-C7FSHEnV.mjs.map +1 -0
  107. package/exceptions-CQi_lNs1.js +152 -0
  108. package/exceptions-CQi_lNs1.js.map +1 -0
  109. package/index.cjs +2 -2
  110. package/index.mjs +2 -2
  111. package/mcp/adk-docs-corpus.json +1 -1
  112. package/package.json +262 -154
  113. package/{scrapper-BeweWurk.js → scrapper-BOLWYGbD.js} +3 -2
  114. package/scrapper-BOLWYGbD.js.map +1 -0
  115. package/{scrapper-BHM1mCde.mjs → scrapper-hDKlNuCT.mjs} +4 -3
  116. package/scrapper-hDKlNuCT.mjs.map +1 -0
  117. package/{searxng-B_D--V5q.js → searxng-CJtEpa8p.js} +3 -2
  118. package/searxng-CJtEpa8p.js.map +1 -0
  119. package/{searxng-BJFulNcK.mjs → searxng-riarj_0u.mjs} +4 -3
  120. package/searxng-riarj_0u.mjs.map +1 -0
  121. package/skills/adk-assembly/SKILL.md +2 -2
  122. package/validate-BFaUYHDN.js +1298 -0
  123. package/validate-BFaUYHDN.js.map +1 -0
  124. package/validate-DSZ3wicB.mjs +1215 -0
  125. package/validate-DSZ3wicB.mjs.map +1 -0
  126. package/scrapper-BHM1mCde.mjs.map +0 -1
  127. package/scrapper-BeweWurk.js.map +0 -1
  128. package/searxng-BJFulNcK.mjs.map +0 -1
  129. package/searxng-B_D--V5q.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.cjs","names":[],"sources":["../../../../src/batteries/llm/openai_chat_completions/helpers.ts"],"sourcesContent":["/**\n * Swappable translation helpers for rendering ADK state into Chat Completions requests.\n *\n * @module @nhtio/adk/batteries/llm/openai_chat_completions/helpers\n *\n * @remarks\n * The swappable translation helpers that turn ADK primitives into OpenAI Chat Completions wire\n * shapes. Each helper is exported under its unprefixed name AND under a `default*` alias so\n * consumers can compose partial overrides. Helpers that compose other helpers receive their\n * dependents via explicit input arguments — never via module-level closure — so a swap at any\n * layer propagates correctly.\n *\n * The wire-shape-AGNOSTIC helpers (`renderUntrustedContent`, `renderMemories`,\n * `renderChatCompletionsSystemPrompt`, `toolsToChatCompletionsTools`, …) now live in the shared,\n * internal `../chat_common/helpers` submodule and are re-exported here under their original names\n * so every existing import keeps resolving. Only the OpenAI-WIRE-SPECIFIC helpers\n * (`renderTimelineMessage`, `renderChatCompletionsToolCallResult`, `buildChatCompletionsHistory`,\n * `createChatCompletionsToolCallDeltaAccumulator`) and the reasoning-field extractor are defined\n * here.\n */\n\nimport { Media } from '@nhtio/adk/common'\nimport { isInstanceOf } from '@nhtio/adk/guards'\nimport { E_UNSUPPORTED_MEDIA_MODALITY } from './exceptions'\nimport {\n escapeXmlAttribute,\n sanitiseNameField,\n memoryToAttrs,\n retrievableToAttrs,\n renderTrustedContent,\n renderUntrustedContent,\n} from '../chat_common/helpers'\nimport type { ChatHelpersCommon } from '../chat_common/types'\nimport type {\n Tool,\n ArtifactTool,\n ToolRegistry,\n Tokenizable,\n Memory,\n Message,\n Thought,\n ToolCall,\n Retrievable,\n SpooledArtifact,\n MediaModalityHazard,\n MediaStashEntry,\n} from '@nhtio/adk/common'\nimport type {\n ChatCompletionsMessage,\n ChatCompletionsContentBlock,\n ChatCompletionsToolCallDelta,\n ChatCompletionsToolCallDeltaAccumulator,\n AssembledToolCall,\n ChatCompletionsBucketOrder,\n MemoryAttrs,\n RetrievableAttrs,\n ChatCompletionsHelpers,\n UnsupportedMediaPolicy,\n ReasoningField,\n ReasoningFieldPrecedence,\n ReasoningExtract,\n} from './types'\n\n// ─── Re-exported wire-shape-agnostic helpers (shared submodule) ───────────────\n// These are defined once in `../chat_common/helpers` and shared with the native Ollama battery.\n// Re-exported here (bare + `default*`) so every existing\n// `@nhtio/adk/batteries/llm/openai_chat_completions` import keeps resolving unchanged.\nexport {\n descriptionToChatCompletionsJsonSchema,\n defaultDescriptionToChatCompletionsJsonSchema,\n renderUntrustedContent,\n defaultRenderUntrustedContent,\n renderTrustedContent,\n defaultRenderTrustedContent,\n renderStandingInstructions,\n defaultRenderStandingInstructions,\n renderMemories,\n defaultRenderMemories,\n renderRetrievableSafetyDirective,\n defaultRenderRetrievableSafetyDirective,\n renderFirstPartyRetrievables,\n defaultRenderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables,\n defaultRenderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables,\n defaultRenderThirdPartyPrivateRetrievables,\n renderRetrievables,\n defaultRenderRetrievables,\n renderThought,\n defaultRenderThought,\n filterThoughts,\n defaultFilterThoughts,\n toolsToChatCompletionsTools,\n defaultToolsToChatCompletionsTools,\n renderChatCompletionsSystemPrompt,\n defaultRenderChatCompletionsSystemPrompt,\n} from '../chat_common/helpers'\n\n// ─── extractReasoningFields ───────────────────────────────────────────────────\n\n/**\n * Pulls model reasoning/thinking text out of a Chat Completions response message or stream delta,\n * reading every wire field named in `precedence` that carries a non-empty string.\n *\n * @remarks\n * Reasoning is not part of OpenAI's official Chat Completions spec, so OpenAI-compatible providers\n * disagree on the field name (`reasoning` for Ollama and current vLLM; `reasoning_content` for\n * legacy vLLM and DeepSeek). This reads the union, in `precedence` order, and de-duplicates by\n * content value: a field whose text exactly matches one already kept is dropped.\n *\n * The result length encodes the emission rule the callers follow:\n * - `0` — no reasoning present.\n * - `1` — a single thought (covers \"only one field present\" AND \"several present but identical\").\n * - `≥2` — divergent fields; each surfaces as its own thought rather than silently dropping one.\n *\n * @param src - The response `message` or stream `delta` to read from.\n * @param precedence - Ordered, de-duplicated field names to read (see `reasoningFieldPrecedence`).\n * @returns The present, content-deduplicated reasoning traces in precedence order.\n */\nexport const extractReasoningFields = (\n src: Partial<Record<ReasoningField, string | null | undefined>> | undefined,\n precedence: ReasoningFieldPrecedence\n): ReasoningExtract[] => {\n const out: ReasoningExtract[] = []\n for (const field of precedence) {\n const value = src?.[field]\n if (typeof value !== 'string' || value.length === 0) continue\n if (out.some((e) => e.content === value)) continue\n out.push({ field, content: value })\n }\n return out\n}\n\n// ─── Media rendering helpers (OpenAI content-block specific) ──────────────────\n\nconst DEFAULT_STASH_FALLBACK_KEYS: ReadonlyArray<string> = [\n 'text:transcript',\n 'text:caption',\n 'text:description',\n]\n\nconst modalityHazardToAttr = (h: MediaModalityHazard): 'inert' | 'extractable' | 'opaque' => {\n if (h === 'inert') return 'inert'\n if (h === 'extractable-instructions') return 'extractable'\n return 'opaque'\n}\n\nconst formatBytesHumanReadable = (bytes: number | undefined): string => {\n if (bytes === undefined || !Number.isFinite(bytes)) return 'unknown size'\n if (bytes < 1024) return `${bytes} B`\n if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`\n if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`\n return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`\n}\n\nconst audioFormatFromMime = (mime: string): 'wav' | 'mp3' | undefined => {\n const m = mime.toLowerCase()\n if (m.includes('wav') || m.includes('x-wav') || m.includes('wave')) return 'wav'\n if (m.includes('mpeg') || m.includes('mp3')) return 'mp3'\n return undefined\n}\n\nconst isMediaTextStashEntry = (e: unknown): e is MediaStashEntry => {\n if (!e || typeof e !== 'object') return false\n const r = e as Record<string, unknown>\n return typeof r.value === 'string' && typeof r.trustTier === 'string'\n}\n\nconst resolveFallbackStash = (\n media: Media,\n keys: ReadonlyArray<string>\n): { text: string; entryTier: MediaStashEntry['trustTier'] } | undefined => {\n for (const key of keys) {\n const entry = media.stash.get(key)\n if (isMediaTextStashEntry(entry)) {\n return { text: entry.value as string, entryTier: entry.trustTier }\n }\n }\n return undefined\n}\n\nconst renderTextInEnvelope = (\n text: string,\n args: {\n trustTier: MediaStashEntry['trustTier']\n modality: 'inert' | 'extractable' | 'opaque'\n nonce: string\n toolName: string | undefined\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n }\n): string => {\n if (args.trustTier === 'first-party') {\n return args.renderTrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n }\n return args.renderUntrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n}\n\nconst renderSyntheticMediaDescription = (media: Media, byteLen: number | undefined): string =>\n `[media: ${media.filename}, ${media.mimeType}, ${formatBytesHumanReadable(byteLen)}]`\n\nconst renderMediaToContentBlocks = async (input: {\n media: Media\n toolName: string | undefined\n nonce: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsContentBlock[]> => {\n const { media, toolName, nonce, unsupportedMediaPolicy, warn } = input\n const modality = modalityHazardToAttr(media.modalityHazard)\n const kind = media.kind\n\n const fallbackPath = async (\n keys: ReadonlyArray<string>,\n allowSyntheticFallthrough: boolean\n ): Promise<ChatCompletionsContentBlock[]> => {\n const fallback = resolveFallbackStash(media, keys)\n if (fallback) {\n const text = renderTextInEnvelope(fallback.text, {\n trustTier: fallback.entryTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n if (!allowSyntheticFallthrough) {\n // 'fallback-stash' falls through to 'synthetic-description' when no entry is found.\n warn?.(\n `unsupportedMediaPolicy='fallback-stash' for ${media.filename}: no matching stash entry — falling through to synthetic description.`\n )\n }\n const byteLen = await media.byteLength()\n const text = renderTextInEnvelope(renderSyntheticMediaDescription(media, byteLen), {\n trustTier: media.trustTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n\n if (kind === 'image') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'image_url',\n image_url: { url: `data:${media.mimeType};base64,${b64}` },\n },\n ]\n }\n\n if (kind === 'audio') {\n const fmt = audioFormatFromMime(media.mimeType)\n if (fmt === undefined) {\n // Audio mime not natively expressible — same policy path as video.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' &&\n unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n }\n const data = await media.asBase64()\n return [\n {\n type: 'input_audio',\n input_audio: { data, format: fmt },\n },\n ]\n }\n\n if (kind === 'document') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'file',\n file: {\n filename: media.filename,\n file_data: `data:${media.mimeType};base64,${b64}`,\n },\n },\n ]\n }\n\n // kind === 'video' — not natively supported by Chat Completions wire format.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' && unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n}\n\n// ─── renderTimelineMessage ────────────────────────────────────────────────────\n\n/**\n * Renders a single timeline {@link @nhtio/adk!Message} into an OpenAI Chat Completions message —\n * mapping media to content blocks (`image_url` / `input_audio` / `file`), wrapping textual bodies\n * in the appropriate trust envelope, and applying the unsupported-media policy.\n */\nexport const renderTimelineMessage = async (input: {\n message: Message\n selfIdentity: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsMessage> => {\n const { message, selfIdentity, unsupportedMediaPolicy, warn } = input\n const identifier =\n message.identity?.identifier !== undefined && message.identity?.identifier !== null\n ? String(message.identity.identifier)\n : ''\n const representationRaw =\n message.identity?.representation !== undefined && message.identity?.representation !== null\n ? message.identity.representation.toString()\n : ''\n // Prompt-facing identity (the `from=` attribute) reads `representation`;\n // structural `messages[].name` reads `identifier`. Fall back to `identifier`\n // when `representation` is empty so a bare-string identity still renders.\n const representation = representationRaw.length > 0 ? representationRaw : identifier\n const text = message.content !== undefined ? message.content.toString() : ''\n const createdAtStr = message.createdAt.toISO?.() ?? ''\n const createdAtAttr = createdAtStr ? ` createdAt=\"${escapeXmlAttribute(createdAtStr)}\"` : ''\n const attachments = message.attachments\n const hasAttachments = attachments.length > 0\n\n // Build the text envelope first (same logic as before).\n let envelopeText: string\n let nameField: string | undefined\n let role: 'user' | 'assistant'\n if (message.role === 'user') {\n role = 'user'\n if (identifier.length === 0) {\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<message_${message.id} from=\"${fromAttr}\" role=\"user\"${createdAtAttr}>\\n${text}\\n</message_${message.id}>`\n }\n } else {\n role = 'assistant'\n if (identifier.length === 0 || identifier === selfIdentity) {\n if (identifier.length > 0) {\n nameField = sanitiseNameField(identifier)\n }\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<peer_agent_output_${message.id} from=\"${fromAttr}\"${createdAtAttr}>\\n${text}\\n</peer_agent_output_${message.id}>`\n }\n }\n\n if (!hasAttachments) {\n const out: ChatCompletionsMessage = { role, content: envelopeText }\n if (nameField !== undefined) out.name = nameField\n return out\n }\n\n // Content-array path: text first (when present), then attachment blocks in array order.\n const blocks: ChatCompletionsContentBlock[] = []\n if (text.length > 0) {\n blocks.push({ type: 'text', text: envelopeText })\n }\n for (const media of attachments) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: undefined,\n nonce: message.id,\n unsupportedMediaPolicy,\n renderTrustedContent,\n renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n const out: ChatCompletionsMessage = { role, content: blocks }\n if (nameField !== undefined) out.name = nameField\n return out\n}\n/** Default timeline-message renderer; alias of {@link renderTimelineMessage}. */\nexport const defaultRenderTimelineMessage = renderTimelineMessage\n\n// ─── renderChatCompletionsToolCallResult ──────────────────────────────────────\n\nconst isSpooledArtifactResult = (\n results: SpooledArtifact | Tokenizable\n): results is SpooledArtifact =>\n isInstanceOf(results, 'SpooledArtifact') ||\n // Subclasses identify via the base class guard upstream\n ((results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== null &&\n (results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== undefined &&\n typeof (\n results as unknown as {\n constructor: { isSpooledArtifactConstructor?: (c: unknown) => boolean }\n }\n ).constructor.isSpooledArtifactConstructor === 'function')\n\nconst looksLikeSpooledArtifact = (value: unknown): value is SpooledArtifact => {\n if (!value || typeof value !== 'object') return false\n const v = value as Record<string, unknown>\n return (\n typeof v.asString === 'function' &&\n typeof v.byteLength === 'function' &&\n typeof v.lineCount === 'function' &&\n typeof v.estimateTokens === 'function'\n )\n}\n\nconst renderArtifactHandleBody = (\n toolCall: ToolCall,\n artifact: SpooledArtifact,\n byteLength: number,\n lineCount: number,\n estimatedTokens: number | undefined,\n encoding: string | undefined\n): string => {\n const ctor = (\n artifact as unknown as {\n constructor: { toolMethods?: ReadonlyArray<{ name: string; description?: string }> }\n }\n ).constructor\n const methods = ctor?.toolMethods ?? []\n const lines: string[] = []\n lines.push(`This tool returned a large artifact that was not inlined to preserve context budget.`)\n lines.push(``)\n lines.push(`Artifact metadata:`)\n lines.push(`- callId: ${toolCall.id}`)\n lines.push(`- kind: ${ctor?.constructor?.name ?? 'SpooledArtifact'}`)\n lines.push(`- byteLength: ${byteLength}`)\n lines.push(`- lineCount: ${lineCount}`)\n if (estimatedTokens !== undefined && encoding) {\n lines.push(`- estimatedTokens: ${estimatedTokens} (encoding: ${encoding})`)\n }\n lines.push(``)\n lines.push(`To read this artifact in this turn, call one of the following tools with`)\n lines.push(`callId=${toolCall.id}:`)\n for (const m of methods) {\n if (m.description) {\n lines.push(`- ${m.name} — ${m.description}`)\n } else {\n lines.push(`- ${m.name}`)\n }\n }\n lines.push(``)\n lines.push(\n `The artifact persists in this turn's context — multiple queries against the same callId are allowed and efficient. Do not assume the body has been inlined anywhere else.`\n )\n return lines.join('\\n')\n}\n\n/**\n * Renders a tool call's result(s) into the OpenAI Chat Completions tool-message body — either a\n * plain string or an array of content blocks when the result carries media — wrapping textual\n * output in the trust envelope appropriate to the tool's trust level.\n */\nexport const renderChatCompletionsToolCallResult = async (input: {\n toolCall: ToolCall\n results: Tokenizable | SpooledArtifact | SpooledArtifact[] | Media | Media[]\n tool: Tool | ArtifactTool | undefined\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<string | ChatCompletionsContentBlock[]> => {\n const { toolCall, results, tool, warn, unsupportedMediaPolicy } = input\n const isTrusted =\n tool !== null && tool !== undefined && (tool as { trusted?: boolean }).trusted === true\n\n if (tool === undefined) {\n warn?.(\n `Tool \"${toolCall.tool}\" is not present in the bound tool registry at render time; defaulting to untrusted envelope.`\n )\n }\n\n // Media / Media[] silo — bypasses Tool.trusted (Trust-Is-Content rule). Envelope is sourced\n // from each Media's own trustTier; modality from each Media's modalityHazard.\n const isMediaResult = Media.isMedia(results)\n const isMediaArrayResult =\n Array.isArray(results) && results.length > 0 && results.every((r) => Media.isMedia(r))\n if (isMediaResult || isMediaArrayResult) {\n const mediaList = isMediaResult ? [results as Media] : (results as Media[])\n const blocks: ChatCompletionsContentBlock[] = []\n for (const media of mediaList) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: toolCall.tool,\n nonce: toolCall.checksum,\n unsupportedMediaPolicy,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n return blocks\n }\n\n // SpooledArtifact[] silo — render each artifact through the existing single-artifact path\n // and concatenate the bodies. Trust envelope is decided per-artifact via the surrounding\n // Tool.trusted flag (same as single SpooledArtifact today).\n if (Array.isArray(results)) {\n const parts: string[] = []\n for (const a of results) {\n const body = await (a as SpooledArtifact).asString()\n parts.push(body)\n }\n const joined = parts.join('\\n\\n')\n if (isTrusted) {\n return input.renderTrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n }\n\n const isSpooled = looksLikeSpooledArtifact(results)\n\n // Handle-pattern branch: spooled + inline=false → always untrusted (queryable-data, not policy).\n if (isSpooled && toolCall.inline === false) {\n const artifact = results as SpooledArtifact\n let byteLength = 0\n let lineCount = 0\n try {\n byteLength = await artifact.byteLength()\n } catch {\n byteLength = 0\n }\n try {\n lineCount = await artifact.lineCount()\n } catch {\n lineCount = 0\n }\n const body = renderArtifactHandleBody(\n toolCall,\n artifact,\n byteLength,\n lineCount,\n undefined,\n undefined\n )\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'artifact-handle',\n tool: toolCall.tool,\n })\n }\n\n // Inline path: render full body via the appropriate envelope.\n if (!isSpooled && toolCall.inline === false) {\n warn?.(\n `Tool call ${toolCall.id} has inline=false but results is a Tokenizable (not a SpooledArtifact); rendering inline anyway.`\n )\n }\n\n let body: string\n if (isSpooled) {\n body = await (results as SpooledArtifact).asString()\n } else {\n body = (results as Tokenizable).toString()\n }\n\n if (isTrusted) {\n return input.renderTrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n}\n/** Default tool-call-result renderer; alias of {@link renderChatCompletionsToolCallResult}. */\nexport const defaultRenderChatCompletionsToolCallResult = renderChatCompletionsToolCallResult\n\n// suppress unused; kept for forward-compat with stricter spool guards\nvoid isSpooledArtifactResult\n\n// ─── buildChatCompletionsHistory ──────────────────────────────────────────────\n\ntype TimelineItem =\n | { kind: 'message'; createdAt: number; value: Message }\n | { kind: 'thought'; createdAt: number; value: Thought }\n | { kind: 'toolCall'; createdAt: number; value: ToolCall }\n\n/**\n * Assembles the complete OpenAI Chat Completions message history for a dispatch — system prompt and\n * content buckets, the interleaved timeline of messages/thoughts/tool calls (with synthetic\n * `assistant.tool_calls` and `tool.tool_call_id` shaping), and the collected opaque reasoning\n * payloads — by delegating to the injected sub-renderers.\n */\nexport const buildChatCompletionsHistory = async (input: {\n systemPrompt: Tokenizable\n standingInstructions: Iterable<Tokenizable>\n memories: Iterable<Memory>\n retrievables: Iterable<Retrievable>\n messages: Iterable<Message>\n thoughts: Iterable<Thought>\n toolCalls: Iterable<ToolCall>\n tools: ToolRegistry\n renderedToolCallResults: Map<string, string | ChatCompletionsContentBlock[]>\n bucketOrder: ChatCompletionsBucketOrder\n selfIdentity: string\n thoughtSurfacing: 'all-self' | 'latest-self' | 'all'\n replayCompatibility: ReadonlyArray<string>\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderChatCompletionsToolCallResult: ChatCompletionsHelpers['renderChatCompletionsToolCallResult']\n renderChatCompletionsSystemPrompt: ChatHelpersCommon['renderChatCompletionsSystemPrompt']\n renderStandingInstructions: ChatHelpersCommon['renderStandingInstructions']\n renderMemories: ChatHelpersCommon['renderMemories']\n renderRetrievables: ChatHelpersCommon['renderRetrievables']\n renderRetrievableSafetyDirective: ChatHelpersCommon['renderRetrievableSafetyDirective']\n renderFirstPartyRetrievables: ChatHelpersCommon['renderFirstPartyRetrievables']\n renderThirdPartyPublicRetrievables: ChatHelpersCommon['renderThirdPartyPublicRetrievables']\n renderThirdPartyPrivateRetrievables: ChatHelpersCommon['renderThirdPartyPrivateRetrievables']\n renderTimelineMessage: ChatCompletionsHelpers['renderTimelineMessage']\n renderThought: ChatHelpersCommon['renderThought']\n filterThoughts: ChatHelpersCommon['filterThoughts']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n warn?: (msg: string) => void\n}): Promise<{\n messages: ChatCompletionsMessage[]\n reasoningPayloads: Array<{ id: string; replayCompatibility: string; payload: unknown }>\n}> => {\n const out: ChatCompletionsMessage[] = []\n const reasoningPayloads: Array<{\n id: string\n replayCompatibility: string\n payload: unknown\n }> = []\n\n const buckets = input.bucketOrder\n const timelineIdx = buckets.indexOf('timeline')\n\n // Build leading system content from base prompt + before-timeline buckets.\n const leadingSystem = await input.renderChatCompletionsSystemPrompt({\n systemPrompt: input.systemPrompt,\n standingInstructions: input.standingInstructions,\n memories: input.memories,\n retrievables: input.retrievables,\n bucketOrder: buckets,\n renderStandingInstructions: input.renderStandingInstructions,\n renderMemories: input.renderMemories,\n renderRetrievables: input.renderRetrievables,\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (leadingSystem.length > 0) {\n out.push({ role: 'system', content: leadingSystem })\n }\n\n // Build the timeline (if present in bucketOrder).\n const includesTimeline = timelineIdx !== -1\n if (includesTimeline) {\n // Filter thoughts per surfacing mode + compatibility.\n const survivingThoughts = input.filterThoughts(\n input.thoughts,\n input.thoughtSurfacing,\n input.selfIdentity,\n input.replayCompatibility\n )\n\n // Build sorted timeline items.\n const items: TimelineItem[] = []\n for (const m of input.messages) {\n items.push({ kind: 'message', createdAt: m.createdAt.toMillis(), value: m })\n }\n for (const t of survivingThoughts) {\n items.push({ kind: 'thought', createdAt: t.createdAt.toMillis(), value: t })\n }\n for (const tc of input.toolCalls) {\n items.push({ kind: 'toolCall', createdAt: tc.createdAt.toMillis(), value: tc })\n }\n items.sort((a, b) => a.createdAt - b.createdAt)\n\n const replaySet = new Set<string>([...input.replayCompatibility])\n\n for (const item of items) {\n if (item.kind === 'message') {\n out.push(\n await input.renderTimelineMessage({\n message: item.value,\n selfIdentity: input.selfIdentity,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n )\n } else if (item.kind === 'thought') {\n const t = item.value\n const identifier = String(t.identity?.identifier ?? '')\n const isSelf = identifier === input.selfIdentity\n const hasPayload = t.payload !== undefined\n const compatTag = t.replayCompatibility\n\n if (hasPayload && compatTag && replaySet.has(compatTag)) {\n // Opaque reasoning — side-channel + summary envelope.\n reasoningPayloads.push({\n id: t.id,\n replayCompatibility: compatTag,\n payload: t.payload,\n })\n const envelope = input.renderThought(\n t.content.toString(),\n {\n nonce: t.id,\n kind: 'opaque-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n replayCompatibility: compatTag,\n },\n t.payload\n )\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n } else if (!hasPayload) {\n // Plain-text reasoning (no payload, or tagged plain-text, or tagged but matched).\n const envelope = input.renderThought(t.content.toString(), {\n nonce: t.id,\n kind: isSelf ? 'self-reasoning' : 'peer-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n })\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n }\n // else: opaque, non-matching → elided (NOT removed from ctx.turnThoughts upstream).\n } else {\n // tool call: emit a synthetic assistant message carrying tool_calls[],\n // followed by a tool-role message with the result.\n const tc = item.value\n const assistantMsg: ChatCompletionsMessage = {\n role: 'assistant',\n content: null,\n tool_calls: [\n {\n id: tc.id,\n type: 'function',\n function: {\n name: tc.tool,\n arguments: typeof tc.args === 'string' ? tc.args : JSON.stringify(tc.args ?? {}),\n },\n },\n ],\n }\n out.push(assistantMsg)\n\n let rendered = input.renderedToolCallResults.get(tc.id)\n if (rendered === undefined) {\n const tool = input.tools.get?.(tc.tool)\n rendered = await input.renderChatCompletionsToolCallResult({\n toolCall: tc,\n results: tc.results as\n | Tokenizable\n | SpooledArtifact\n | SpooledArtifact[]\n | Media\n | Media[],\n tool: tool as Tool | ArtifactTool | undefined,\n renderUntrustedContent: input.renderUntrustedContent,\n renderTrustedContent: input.renderTrustedContent,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n }\n out.push({\n role: 'tool',\n content: rendered,\n tool_call_id: tc.id,\n })\n }\n }\n }\n\n // Trailing system message for after-timeline buckets.\n if (includesTimeline) {\n const trailingParts: string[] = []\n for (let i = timelineIdx + 1; i < buckets.length; i++) {\n const label = buckets[i]!\n if (label === 'standingInstructions') {\n const block = input.renderStandingInstructions(input.standingInstructions)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'memories') {\n const wrapped: Array<{ memory: Memory; attrs: MemoryAttrs }> = []\n for (const m of input.memories) {\n wrapped.push(memoryToAttrs(m))\n }\n const block = input.renderMemories(wrapped)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'retrievables') {\n const wrapped: Array<{ retrievable: Retrievable; attrs: RetrievableAttrs }> = []\n for (const r of input.retrievables) {\n wrapped.push(retrievableToAttrs(r))\n }\n const block = await input.renderRetrievables(wrapped, {\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (block.length > 0) trailingParts.push(block)\n }\n }\n if (trailingParts.length > 0) {\n out.push({ role: 'system', content: trailingParts.join('\\n\\n') })\n }\n }\n\n return { messages: out, reasoningPayloads }\n}\n/** Default history assembler; alias of {@link buildChatCompletionsHistory}. */\nexport const defaultBuildChatCompletionsHistory = buildChatCompletionsHistory\n\n// ─── createChatCompletionsToolCallDeltaAccumulator ────────────────────────────\n\n/**\n * Creates a fresh accumulator that stitches streamed {@link ChatCompletionsToolCallDelta} fragments\n * (keyed by their stream index) into fully-assembled tool calls, drained once the stream completes.\n */\nexport const createChatCompletionsToolCallDeltaAccumulator =\n (): ChatCompletionsToolCallDeltaAccumulator => {\n const byIndex = new Map<\n number,\n { id?: string; type?: 'function'; name: string; args: string }\n >()\n return {\n feed(delta: ChatCompletionsToolCallDelta): void {\n const idx = delta.index\n let entry = byIndex.get(idx)\n if (!entry) {\n entry = { name: '', args: '' }\n byIndex.set(idx, entry)\n }\n if (delta.id !== undefined) entry.id = delta.id\n if (delta.type !== undefined) entry.type = delta.type\n if (delta.function?.name !== undefined) {\n entry.name = entry.name + delta.function.name\n }\n if (delta.function?.arguments !== undefined) {\n entry.args = entry.args + delta.function.arguments\n }\n },\n drain(): AssembledToolCall[] {\n const out: AssembledToolCall[] = []\n const indices = Array.from(byIndex.keys()).sort((a, b) => a - b)\n for (const idx of indices) {\n const e = byIndex.get(idx)!\n out.push({\n id: e.id ?? `call_${idx}`,\n type: e.type ?? 'function',\n name: e.name,\n args: e.args,\n })\n }\n return out\n },\n }\n }\n/** Default delta-accumulator factory; alias of {@link createChatCompletionsToolCallDeltaAccumulator}. */\nexport const defaultCreateChatCompletionsToolCallDeltaAccumulator =\n createChatCompletionsToolCallDeltaAccumulator\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuHA,IAAa,0BACX,KACA,eACuB;CACvB,MAAM,MAA0B,CAAC;CACjC,KAAK,MAAM,SAAS,YAAY;EAC9B,MAAM,QAAQ,MAAM;EACpB,IAAI,OAAO,UAAU,YAAY,MAAM,WAAW,GAAG;EACrD,IAA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,UAAU,SAAS,KAAA,GAC3B,MAAM,OAAO,MAAM,OAAO,MAAM,SAAS;GAE3C,IAAI,MAAM,UAAU,cAAc,KAAA,GAChC,MAAM,OAAO,MAAM,OAAO,MAAM,SAAS;EAE7C;EACA,QAA6B;GAC3B,MAAM,MAA2B,CAAC;GAClC,MAAM,UAAU,MAAM,KAAK,QAAQ,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,IAAI,CAAC;GAC/D,KAAK,MAAM,OAAO,SAAS;IACzB,MAAM,IAAI,QAAQ,IAAI,GAAG;IACzB,IAAI,KAAK;KACP,IAAI,EAAE,MAAM,QAAQ;KACpB,MAAM,EAAE,QAAQ;KAChB,MAAM,EAAE;KACR,MAAM,EAAE;IACV,CAAC;GACH;GACA,OAAO;EACT;CACF;AACF;;AAEF,IAAa,uDACX"}
1
+ {"version":3,"file":"helpers.cjs","names":[],"sources":["../../../../src/batteries/llm/openai_chat_completions/helpers.ts"],"sourcesContent":["/**\n * Swappable translation helpers for rendering ADK state into Chat Completions requests.\n *\n * @module @nhtio/adk/batteries/llm/openai_chat_completions/helpers\n *\n * @remarks\n * The swappable translation helpers that turn ADK primitives into OpenAI Chat Completions wire\n * shapes. Each helper is exported under its unprefixed name AND under a `default*` alias so\n * consumers can compose partial overrides. Helpers that compose other helpers receive their\n * dependents via explicit input arguments — never via module-level closure — so a swap at any\n * layer propagates correctly.\n *\n * The wire-shape-AGNOSTIC helpers (`renderUntrustedContent`, `renderMemories`,\n * `renderChatCompletionsSystemPrompt`, `toolsToChatCompletionsTools`, …) now live in the shared,\n * internal `../chat_common/helpers` submodule and are re-exported here under their original names\n * so every existing import keeps resolving. Only the OpenAI-WIRE-SPECIFIC helpers\n * (`renderTimelineMessage`, `renderChatCompletionsToolCallResult`, `buildChatCompletionsHistory`,\n * `createChatCompletionsToolCallDeltaAccumulator`) and the reasoning-field extractor are defined\n * here.\n */\n\nimport { Media } from '@nhtio/adk/common'\nimport { isInstanceOf } from '@nhtio/adk/guards'\nimport { E_UNSUPPORTED_MEDIA_MODALITY } from './exceptions'\nimport {\n escapeXmlAttribute,\n sanitiseNameField,\n memoryToAttrs,\n retrievableToAttrs,\n renderTrustedContent,\n renderUntrustedContent,\n} from '../chat_common/helpers'\nimport type { ChatHelpersCommon } from '../chat_common/types'\nimport type {\n Tool,\n ArtifactTool,\n ToolRegistry,\n Tokenizable,\n Memory,\n Message,\n Thought,\n ToolCall,\n Retrievable,\n SpooledArtifact,\n MediaModalityHazard,\n MediaStashEntry,\n} from '@nhtio/adk/common'\nimport type {\n ChatCompletionsMessage,\n ChatCompletionsContentBlock,\n ChatCompletionsToolCallDelta,\n ChatCompletionsToolCallDeltaAccumulator,\n AssembledToolCall,\n ChatCompletionsBucketOrder,\n MemoryAttrs,\n RetrievableAttrs,\n ChatCompletionsHelpers,\n UnsupportedMediaPolicy,\n ReasoningField,\n ReasoningFieldPrecedence,\n ReasoningExtract,\n} from './types'\n\n// ─── Re-exported wire-shape-agnostic helpers (shared submodule) ───────────────\n// These are defined once in `../chat_common/helpers` and shared with the native Ollama battery.\n// Re-exported here (bare + `default*`) so every existing\n// `@nhtio/adk/batteries/llm/openai_chat_completions` import keeps resolving unchanged.\nexport {\n descriptionToChatCompletionsJsonSchema,\n defaultDescriptionToChatCompletionsJsonSchema,\n renderUntrustedContent,\n defaultRenderUntrustedContent,\n renderTrustedContent,\n defaultRenderTrustedContent,\n renderStandingInstructions,\n defaultRenderStandingInstructions,\n renderMemories,\n defaultRenderMemories,\n renderRetrievableSafetyDirective,\n defaultRenderRetrievableSafetyDirective,\n renderFirstPartyRetrievables,\n defaultRenderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables,\n defaultRenderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables,\n defaultRenderThirdPartyPrivateRetrievables,\n renderRetrievables,\n defaultRenderRetrievables,\n renderThought,\n defaultRenderThought,\n filterThoughts,\n defaultFilterThoughts,\n toolsToChatCompletionsTools,\n defaultToolsToChatCompletionsTools,\n renderChatCompletionsSystemPrompt,\n defaultRenderChatCompletionsSystemPrompt,\n} from '../chat_common/helpers'\n\n// ─── extractReasoningFields ───────────────────────────────────────────────────\n\n/**\n * Pulls model reasoning/thinking text out of a Chat Completions response message or stream delta,\n * reading every wire field named in `precedence` that carries a non-empty string.\n *\n * @remarks\n * Reasoning is not part of OpenAI's official Chat Completions spec, so OpenAI-compatible providers\n * disagree on the field name (`reasoning` for Ollama and current vLLM; `reasoning_content` for\n * legacy vLLM and DeepSeek). This reads the union, in `precedence` order, and de-duplicates by\n * content value: a field whose text exactly matches one already kept is dropped.\n *\n * The result length encodes the emission rule the callers follow:\n * - `0` — no reasoning present.\n * - `1` — a single thought (covers \"only one field present\" AND \"several present but identical\").\n * - `≥2` — divergent fields; each surfaces as its own thought rather than silently dropping one.\n *\n * @param src - The response `message` or stream `delta` to read from.\n * @param precedence - Ordered, de-duplicated field names to read (see `reasoningFieldPrecedence`).\n * @returns The present, content-deduplicated reasoning traces in precedence order.\n */\nexport const extractReasoningFields = (\n src: Partial<Record<ReasoningField, string | null | undefined>> | undefined,\n precedence: ReasoningFieldPrecedence\n): ReasoningExtract[] => {\n const out: ReasoningExtract[] = []\n for (const field of precedence) {\n const value = src?.[field]\n if (typeof value !== 'string' || value.length === 0) continue\n if (out.some((e) => e.content === value)) continue\n out.push({ field, content: value })\n }\n return out\n}\n\n// ─── Media rendering helpers (OpenAI content-block specific) ──────────────────\n\nconst DEFAULT_STASH_FALLBACK_KEYS: ReadonlyArray<string> = [\n 'text:transcript',\n 'text:caption',\n 'text:description',\n]\n\nconst modalityHazardToAttr = (h: MediaModalityHazard): 'inert' | 'extractable' | 'opaque' => {\n if (h === 'inert') return 'inert'\n if (h === 'extractable-instructions') return 'extractable'\n return 'opaque'\n}\n\nconst formatBytesHumanReadable = (bytes: number | undefined): string => {\n if (bytes === undefined || !Number.isFinite(bytes)) return 'unknown size'\n if (bytes < 1024) return `${bytes} B`\n if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`\n if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`\n return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`\n}\n\nconst audioFormatFromMime = (mime: string): 'wav' | 'mp3' | undefined => {\n const m = mime.toLowerCase()\n if (m.includes('wav') || m.includes('x-wav') || m.includes('wave')) return 'wav'\n if (m.includes('mpeg') || m.includes('mp3')) return 'mp3'\n return undefined\n}\n\nconst isMediaTextStashEntry = (e: unknown): e is MediaStashEntry => {\n if (!e || typeof e !== 'object') return false\n const r = e as Record<string, unknown>\n return typeof r.value === 'string' && typeof r.trustTier === 'string'\n}\n\nconst resolveFallbackStash = (\n media: Media,\n keys: ReadonlyArray<string>\n): { text: string; entryTier: MediaStashEntry['trustTier'] } | undefined => {\n for (const key of keys) {\n const entry = media.stash.get(key)\n if (isMediaTextStashEntry(entry)) {\n return { text: entry.value as string, entryTier: entry.trustTier }\n }\n }\n return undefined\n}\n\nconst renderTextInEnvelope = (\n text: string,\n args: {\n trustTier: MediaStashEntry['trustTier']\n modality: 'inert' | 'extractable' | 'opaque'\n nonce: string\n toolName: string | undefined\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n }\n): string => {\n if (args.trustTier === 'first-party') {\n return args.renderTrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n }\n return args.renderUntrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n}\n\nconst renderSyntheticMediaDescription = (media: Media, byteLen: number | undefined): string =>\n `[media: ${media.filename}, ${media.mimeType}, ${formatBytesHumanReadable(byteLen)}]`\n\n/**\n * The inline media id-marker: a harness-authored text block rendered immediately BEFORE each\n * media content block, so the model can reference the media by id in subsequent tool calls\n * (`media_id` args, `@id` pipe refs).\n *\n * Trust posture: the marker is structural reference data authored by the harness from the\n * harness-controlled `Media.id` (a UUID, not derivable from the payload) — it is NOT payload\n * content, carries no authority, and deliberately renders OUTSIDE the untrusted envelope with\n * fixed, non-instruction phrasing. This is a documented cross-battery convention: every LLM\n * battery that renders media emits the same marker shape.\n */\nconst renderMediaIdMarker = (media: Media): string => `[media id: ${media.id} | ${media.filename}]`\n\nconst renderMediaToContentBlocks = async (input: {\n media: Media\n toolName: string | undefined\n nonce: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsContentBlock[]> => {\n const blocks = await renderMediaBodyBlocks(input)\n return [{ type: 'text', text: renderMediaIdMarker(input.media) }, ...blocks]\n}\n\nconst renderMediaBodyBlocks = async (input: {\n media: Media\n toolName: string | undefined\n nonce: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsContentBlock[]> => {\n const { media, toolName, nonce, unsupportedMediaPolicy, warn } = input\n const modality = modalityHazardToAttr(media.modalityHazard)\n const kind = media.kind\n\n const fallbackPath = async (\n keys: ReadonlyArray<string>,\n allowSyntheticFallthrough: boolean\n ): Promise<ChatCompletionsContentBlock[]> => {\n const fallback = resolveFallbackStash(media, keys)\n if (fallback) {\n const text = renderTextInEnvelope(fallback.text, {\n trustTier: fallback.entryTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n if (!allowSyntheticFallthrough) {\n // 'fallback-stash' falls through to 'synthetic-description' when no entry is found.\n warn?.(\n `unsupportedMediaPolicy='fallback-stash' for ${media.filename}: no matching stash entry — falling through to synthetic description.`\n )\n }\n const byteLen = await media.byteLength()\n const text = renderTextInEnvelope(renderSyntheticMediaDescription(media, byteLen), {\n trustTier: media.trustTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n\n if (kind === 'image') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'image_url',\n image_url: { url: `data:${media.mimeType};base64,${b64}` },\n },\n ]\n }\n\n if (kind === 'audio') {\n const fmt = audioFormatFromMime(media.mimeType)\n if (fmt === undefined) {\n // Audio mime not natively expressible — same policy path as video.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' &&\n unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n }\n const data = await media.asBase64()\n return [\n {\n type: 'input_audio',\n input_audio: { data, format: fmt },\n },\n ]\n }\n\n if (kind === 'document') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'file',\n file: {\n filename: media.filename,\n file_data: `data:${media.mimeType};base64,${b64}`,\n },\n },\n ]\n }\n\n // kind === 'video' — not natively supported by Chat Completions wire format.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' && unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n}\n\n// ─── renderTimelineMessage ────────────────────────────────────────────────────\n\n/**\n * Renders a single timeline {@link @nhtio/adk!Message} into an OpenAI Chat Completions message —\n * mapping media to content blocks (`image_url` / `input_audio` / `file`), wrapping textual bodies\n * in the appropriate trust envelope, and applying the unsupported-media policy.\n */\nexport const renderTimelineMessage = async (input: {\n message: Message\n selfIdentity: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsMessage> => {\n const { message, selfIdentity, unsupportedMediaPolicy, warn } = input\n const identifier =\n message.identity?.identifier !== undefined && message.identity?.identifier !== null\n ? String(message.identity.identifier)\n : ''\n const representationRaw =\n message.identity?.representation !== undefined && message.identity?.representation !== null\n ? message.identity.representation.toString()\n : ''\n // Prompt-facing identity (the `from=` attribute) reads `representation`;\n // structural `messages[].name` reads `identifier`. Fall back to `identifier`\n // when `representation` is empty so a bare-string identity still renders.\n const representation = representationRaw.length > 0 ? representationRaw : identifier\n const text = message.content !== undefined ? message.content.toString() : ''\n const createdAtStr = message.createdAt.toISO?.() ?? ''\n const createdAtAttr = createdAtStr ? ` createdAt=\"${escapeXmlAttribute(createdAtStr)}\"` : ''\n const attachments = message.attachments\n const hasAttachments = attachments.length > 0\n\n // Build the text envelope first (same logic as before).\n let envelopeText: string\n let nameField: string | undefined\n let role: 'user' | 'assistant'\n if (message.role === 'user') {\n role = 'user'\n if (identifier.length === 0) {\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<message_${message.id} from=\"${fromAttr}\" role=\"user\"${createdAtAttr}>\\n${text}\\n</message_${message.id}>`\n }\n } else {\n role = 'assistant'\n if (identifier.length === 0 || identifier === selfIdentity) {\n if (identifier.length > 0) {\n nameField = sanitiseNameField(identifier)\n }\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<peer_agent_output_${message.id} from=\"${fromAttr}\"${createdAtAttr}>\\n${text}\\n</peer_agent_output_${message.id}>`\n }\n }\n\n if (!hasAttachments) {\n const out: ChatCompletionsMessage = { role, content: envelopeText }\n if (nameField !== undefined) out.name = nameField\n return out\n }\n\n // Content-array path: text first (when present), then attachment blocks in array order.\n const blocks: ChatCompletionsContentBlock[] = []\n if (text.length > 0) {\n blocks.push({ type: 'text', text: envelopeText })\n }\n for (const media of attachments) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: undefined,\n nonce: message.id,\n unsupportedMediaPolicy,\n renderTrustedContent,\n renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n const out: ChatCompletionsMessage = { role, content: blocks }\n if (nameField !== undefined) out.name = nameField\n return out\n}\n/** Default timeline-message renderer; alias of {@link renderTimelineMessage}. */\nexport const defaultRenderTimelineMessage = renderTimelineMessage\n\n// ─── renderChatCompletionsToolCallResult ──────────────────────────────────────\n\nconst isSpooledArtifactResult = (\n results: SpooledArtifact | Tokenizable\n): results is SpooledArtifact =>\n isInstanceOf(results, 'SpooledArtifact') ||\n // Subclasses identify via the base class guard upstream\n ((results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== null &&\n (results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== undefined &&\n typeof (\n results as unknown as {\n constructor: { isSpooledArtifactConstructor?: (c: unknown) => boolean }\n }\n ).constructor.isSpooledArtifactConstructor === 'function')\n\nconst looksLikeSpooledArtifact = (value: unknown): value is SpooledArtifact => {\n if (!value || typeof value !== 'object') return false\n const v = value as Record<string, unknown>\n return (\n typeof v.asString === 'function' &&\n typeof v.byteLength === 'function' &&\n typeof v.lineCount === 'function' &&\n typeof v.estimateTokens === 'function'\n )\n}\n\nconst renderArtifactHandleBody = (\n toolCall: ToolCall,\n artifact: SpooledArtifact,\n byteLength: number,\n lineCount: number,\n estimatedTokens: number | undefined,\n encoding: string | undefined\n): string => {\n const ctor = (\n artifact as unknown as {\n constructor: { toolMethods?: ReadonlyArray<{ name: string; description?: string }> }\n }\n ).constructor\n const methods = ctor?.toolMethods ?? []\n const lines: string[] = []\n lines.push(`This tool returned a large artifact that was not inlined to preserve context budget.`)\n lines.push(``)\n lines.push(`Artifact metadata:`)\n lines.push(`- callId: ${toolCall.id}`)\n lines.push(`- kind: ${ctor?.constructor?.name ?? 'SpooledArtifact'}`)\n lines.push(`- byteLength: ${byteLength}`)\n lines.push(`- lineCount: ${lineCount}`)\n if (estimatedTokens !== undefined && encoding) {\n lines.push(`- estimatedTokens: ${estimatedTokens} (encoding: ${encoding})`)\n }\n lines.push(``)\n lines.push(`To read this artifact in this turn, call one of the following tools with`)\n lines.push(`callId=${toolCall.id}:`)\n for (const m of methods) {\n if (m.description) {\n lines.push(`- ${m.name} — ${m.description}`)\n } else {\n lines.push(`- ${m.name}`)\n }\n }\n lines.push(``)\n lines.push(\n `The artifact persists in this turn's context — multiple queries against the same callId are allowed and efficient. Do not assume the body has been inlined anywhere else.`\n )\n return lines.join('\\n')\n}\n\n/**\n * Renders a tool call's result(s) into the OpenAI Chat Completions tool-message body — either a\n * plain string or an array of content blocks when the result carries media — wrapping textual\n * output in the trust envelope appropriate to the tool's trust level.\n */\nexport const renderChatCompletionsToolCallResult = async (input: {\n toolCall: ToolCall\n results: Tokenizable | SpooledArtifact | SpooledArtifact[] | Media | Media[]\n tool: Tool | ArtifactTool | undefined\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<string | ChatCompletionsContentBlock[]> => {\n const { toolCall, results, tool, warn, unsupportedMediaPolicy } = input\n const isTrusted =\n tool !== null && tool !== undefined && (tool as { trusted?: boolean }).trusted === true\n\n if (tool === undefined) {\n warn?.(\n `Tool \"${toolCall.tool}\" is not present in the bound tool registry at render time; defaulting to untrusted envelope.`\n )\n }\n\n // Media / Media[] silo — bypasses Tool.trusted (Trust-Is-Content rule). Envelope is sourced\n // from each Media's own trustTier; modality from each Media's modalityHazard.\n const isMediaResult = Media.isMedia(results)\n const isMediaArrayResult =\n Array.isArray(results) && results.length > 0 && results.every((r) => Media.isMedia(r))\n if (isMediaResult || isMediaArrayResult) {\n const mediaList = isMediaResult ? [results as Media] : (results as Media[])\n const blocks: ChatCompletionsContentBlock[] = []\n for (const media of mediaList) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: toolCall.tool,\n nonce: toolCall.checksum,\n unsupportedMediaPolicy,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n return blocks\n }\n\n // SpooledArtifact[] silo — render each artifact through the existing single-artifact path\n // and concatenate the bodies. Trust envelope is decided per-artifact via the surrounding\n // Tool.trusted flag (same as single SpooledArtifact today).\n if (Array.isArray(results)) {\n const parts: string[] = []\n for (const a of results) {\n const body = await (a as SpooledArtifact).asString()\n parts.push(body)\n }\n const joined = parts.join('\\n\\n')\n if (isTrusted) {\n return input.renderTrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n }\n\n const isSpooled = looksLikeSpooledArtifact(results)\n\n // Handle-pattern branch: spooled + inline=false → always untrusted (queryable-data, not policy).\n if (isSpooled && toolCall.inline === false) {\n const artifact = results as SpooledArtifact\n let byteLength = 0\n let lineCount = 0\n try {\n byteLength = await artifact.byteLength()\n } catch {\n byteLength = 0\n }\n try {\n lineCount = await artifact.lineCount()\n } catch {\n lineCount = 0\n }\n const body = renderArtifactHandleBody(\n toolCall,\n artifact,\n byteLength,\n lineCount,\n undefined,\n undefined\n )\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'artifact-handle',\n tool: toolCall.tool,\n })\n }\n\n // Inline path: render full body via the appropriate envelope.\n if (!isSpooled && toolCall.inline === false) {\n warn?.(\n `Tool call ${toolCall.id} has inline=false but results is a Tokenizable (not a SpooledArtifact); rendering inline anyway.`\n )\n }\n\n let body: string\n if (isSpooled) {\n body = await (results as SpooledArtifact).asString()\n } else {\n body = (results as Tokenizable).toString()\n }\n\n if (isTrusted) {\n return input.renderTrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n}\n/** Default tool-call-result renderer; alias of {@link renderChatCompletionsToolCallResult}. */\nexport const defaultRenderChatCompletionsToolCallResult = renderChatCompletionsToolCallResult\n\n// suppress unused; kept for forward-compat with stricter spool guards\nvoid isSpooledArtifactResult\n\n// ─── buildChatCompletionsHistory ──────────────────────────────────────────────\n\ntype TimelineItem =\n | { kind: 'message'; createdAt: number; value: Message }\n | { kind: 'thought'; createdAt: number; value: Thought }\n | { kind: 'toolCall'; createdAt: number; value: ToolCall }\n\n/**\n * Assembles the complete OpenAI Chat Completions message history for a dispatch — system prompt and\n * content buckets, the interleaved timeline of messages/thoughts/tool calls (with synthetic\n * `assistant.tool_calls` and `tool.tool_call_id` shaping), and the collected opaque reasoning\n * payloads — by delegating to the injected sub-renderers.\n */\nexport const buildChatCompletionsHistory = async (input: {\n systemPrompt: Tokenizable\n standingInstructions: Iterable<Tokenizable>\n memories: Iterable<Memory>\n retrievables: Iterable<Retrievable>\n messages: Iterable<Message>\n thoughts: Iterable<Thought>\n toolCalls: Iterable<ToolCall>\n tools: ToolRegistry\n renderedToolCallResults: Map<string, string | ChatCompletionsContentBlock[]>\n bucketOrder: ChatCompletionsBucketOrder\n selfIdentity: string\n thoughtSurfacing: 'all-self' | 'latest-self' | 'all'\n replayCompatibility: ReadonlyArray<string>\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderChatCompletionsToolCallResult: ChatCompletionsHelpers['renderChatCompletionsToolCallResult']\n renderChatCompletionsSystemPrompt: ChatHelpersCommon['renderChatCompletionsSystemPrompt']\n renderStandingInstructions: ChatHelpersCommon['renderStandingInstructions']\n renderMemories: ChatHelpersCommon['renderMemories']\n renderRetrievables: ChatHelpersCommon['renderRetrievables']\n renderRetrievableSafetyDirective: ChatHelpersCommon['renderRetrievableSafetyDirective']\n renderFirstPartyRetrievables: ChatHelpersCommon['renderFirstPartyRetrievables']\n renderThirdPartyPublicRetrievables: ChatHelpersCommon['renderThirdPartyPublicRetrievables']\n renderThirdPartyPrivateRetrievables: ChatHelpersCommon['renderThirdPartyPrivateRetrievables']\n renderTimelineMessage: ChatCompletionsHelpers['renderTimelineMessage']\n renderThought: ChatHelpersCommon['renderThought']\n filterThoughts: ChatHelpersCommon['filterThoughts']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n warn?: (msg: string) => void\n}): Promise<{\n messages: ChatCompletionsMessage[]\n reasoningPayloads: Array<{ id: string; replayCompatibility: string; payload: unknown }>\n}> => {\n const out: ChatCompletionsMessage[] = []\n const reasoningPayloads: Array<{\n id: string\n replayCompatibility: string\n payload: unknown\n }> = []\n\n const buckets = input.bucketOrder\n const timelineIdx = buckets.indexOf('timeline')\n\n // Build leading system content from base prompt + before-timeline buckets.\n const leadingSystem = await input.renderChatCompletionsSystemPrompt({\n systemPrompt: input.systemPrompt,\n standingInstructions: input.standingInstructions,\n memories: input.memories,\n retrievables: input.retrievables,\n bucketOrder: buckets,\n renderStandingInstructions: input.renderStandingInstructions,\n renderMemories: input.renderMemories,\n renderRetrievables: input.renderRetrievables,\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (leadingSystem.length > 0) {\n out.push({ role: 'system', content: leadingSystem })\n }\n\n // Build the timeline (if present in bucketOrder).\n const includesTimeline = timelineIdx !== -1\n if (includesTimeline) {\n // Filter thoughts per surfacing mode + compatibility.\n const survivingThoughts = input.filterThoughts(\n input.thoughts,\n input.thoughtSurfacing,\n input.selfIdentity,\n input.replayCompatibility\n )\n\n // Build sorted timeline items.\n const items: TimelineItem[] = []\n for (const m of input.messages) {\n items.push({ kind: 'message', createdAt: m.createdAt.toMillis(), value: m })\n }\n for (const t of survivingThoughts) {\n items.push({ kind: 'thought', createdAt: t.createdAt.toMillis(), value: t })\n }\n for (const tc of input.toolCalls) {\n items.push({ kind: 'toolCall', createdAt: tc.createdAt.toMillis(), value: tc })\n }\n items.sort((a, b) => a.createdAt - b.createdAt)\n\n const replaySet = new Set<string>([...input.replayCompatibility])\n\n for (const item of items) {\n if (item.kind === 'message') {\n out.push(\n await input.renderTimelineMessage({\n message: item.value,\n selfIdentity: input.selfIdentity,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n )\n } else if (item.kind === 'thought') {\n const t = item.value\n const identifier = String(t.identity?.identifier ?? '')\n const isSelf = identifier === input.selfIdentity\n const hasPayload = t.payload !== undefined\n const compatTag = t.replayCompatibility\n\n if (hasPayload && compatTag && replaySet.has(compatTag)) {\n // Opaque reasoning — side-channel + summary envelope.\n reasoningPayloads.push({\n id: t.id,\n replayCompatibility: compatTag,\n payload: t.payload,\n })\n const envelope = input.renderThought(\n t.content.toString(),\n {\n nonce: t.id,\n kind: 'opaque-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n replayCompatibility: compatTag,\n },\n t.payload\n )\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n } else if (!hasPayload) {\n // Plain-text reasoning (no payload, or tagged plain-text, or tagged but matched).\n const envelope = input.renderThought(t.content.toString(), {\n nonce: t.id,\n kind: isSelf ? 'self-reasoning' : 'peer-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n })\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n }\n // else: opaque, non-matching → elided (NOT removed from ctx.turnThoughts upstream).\n } else {\n // tool call: emit a synthetic assistant message carrying tool_calls[],\n // followed by a tool-role message with the result.\n const tc = item.value\n const assistantMsg: ChatCompletionsMessage = {\n role: 'assistant',\n content: null,\n tool_calls: [\n {\n id: tc.id,\n type: 'function',\n function: {\n name: tc.tool,\n arguments: typeof tc.args === 'string' ? tc.args : JSON.stringify(tc.args ?? {}),\n },\n },\n ],\n }\n out.push(assistantMsg)\n\n let rendered = input.renderedToolCallResults.get(tc.id)\n if (rendered === undefined) {\n const tool = input.tools.get?.(tc.tool)\n rendered = await input.renderChatCompletionsToolCallResult({\n toolCall: tc,\n results: tc.results as\n | Tokenizable\n | SpooledArtifact\n | SpooledArtifact[]\n | Media\n | Media[],\n tool: tool as Tool | ArtifactTool | undefined,\n renderUntrustedContent: input.renderUntrustedContent,\n renderTrustedContent: input.renderTrustedContent,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n }\n out.push({\n role: 'tool',\n content: rendered,\n tool_call_id: tc.id,\n })\n }\n }\n }\n\n // Trailing system message for after-timeline buckets.\n if (includesTimeline) {\n const trailingParts: string[] = []\n for (let i = timelineIdx + 1; i < buckets.length; i++) {\n const label = buckets[i]!\n if (label === 'standingInstructions') {\n const block = input.renderStandingInstructions(input.standingInstructions)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'memories') {\n const wrapped: Array<{ memory: Memory; attrs: MemoryAttrs }> = []\n for (const m of input.memories) {\n wrapped.push(memoryToAttrs(m))\n }\n const block = input.renderMemories(wrapped)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'retrievables') {\n const wrapped: Array<{ retrievable: Retrievable; attrs: RetrievableAttrs }> = []\n for (const r of input.retrievables) {\n wrapped.push(retrievableToAttrs(r))\n }\n const block = await input.renderRetrievables(wrapped, {\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (block.length > 0) trailingParts.push(block)\n }\n }\n if (trailingParts.length > 0) {\n out.push({ role: 'system', content: trailingParts.join('\\n\\n') })\n }\n }\n\n return { messages: out, reasoningPayloads }\n}\n/** Default history assembler; alias of {@link buildChatCompletionsHistory}. */\nexport const defaultBuildChatCompletionsHistory = buildChatCompletionsHistory\n\n// ─── createChatCompletionsToolCallDeltaAccumulator ────────────────────────────\n\n/**\n * Creates a fresh accumulator that stitches streamed {@link ChatCompletionsToolCallDelta} fragments\n * (keyed by their stream index) into fully-assembled tool calls, drained once the stream completes.\n */\nexport const createChatCompletionsToolCallDeltaAccumulator =\n (): ChatCompletionsToolCallDeltaAccumulator => {\n const byIndex = new Map<\n number,\n { id?: string; type?: 'function'; name: string; args: string }\n >()\n return {\n feed(delta: ChatCompletionsToolCallDelta): void {\n const idx = delta.index\n let entry = byIndex.get(idx)\n if (!entry) {\n entry = { name: '', args: '' }\n byIndex.set(idx, entry)\n }\n if (delta.id !== undefined) entry.id = delta.id\n if (delta.type !== undefined) entry.type = delta.type\n if (delta.function?.name !== undefined) {\n entry.name = entry.name + delta.function.name\n }\n if (delta.function?.arguments !== undefined) {\n entry.args = entry.args + delta.function.arguments\n }\n },\n drain(): AssembledToolCall[] {\n const out: AssembledToolCall[] = []\n const indices = Array.from(byIndex.keys()).sort((a, b) => a - b)\n for (const idx of indices) {\n const e = byIndex.get(idx)!\n out.push({\n id: e.id ?? `call_${idx}`,\n type: e.type ?? 'function',\n name: e.name,\n args: e.args,\n })\n }\n return out\n },\n }\n }\n/** Default delta-accumulator factory; alias of {@link createChatCompletionsToolCallDeltaAccumulator}. */\nexport const defaultCreateChatCompletionsToolCallDeltaAccumulator =\n createChatCompletionsToolCallDeltaAccumulator\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuHA,IAAa,0BACX,KACA,eACuB;CACvB,MAAM,MAA0B,CAAC;CACjC,KAAK,MAAM,SAAS,YAAY;EAC9B,MAAM,QAAQ,MAAM;EACpB,IAAI,OAAO,UAAU,YAAY,MAAM,WAAW,GAAG;EACrD,IAAI,IAAI,MAAM,MAAM,EAAE,YAAY,KAAK,GAAG;EAC1C,IAAI,KAAK;GAAE;GAAO,SAAS;EAAM,CAAC;CACpC;CACA,OAAO;AACT;AAIA,IAAM,8BAAqD;CACzD;CACA;CACA;AACF;AAEA,IAAM,wBAAwB,MAA+D;CAC3F,IAAI,MAAM,SAAS,OAAO;CAC1B,IAAI,MAAM,4BAA4B,OAAO;CAC7C,OAAO;AACT;AAEA,IAAM,4BAA4B,UAAsC;CACtE,IAAI,UAAU,KAAA,KAAa,CAAC,OAAO,SAAS,KAAK,GAAG,OAAO;CAC3D,IAAI,QAAQ,MAAM,OAAO,GAAG,MAAM;CAClC,IAAI,QAAQ,OAAO,MAAM,OAAO,IAAI,QAAQ,MAAM,QAAQ,CAAC,EAAE;CAC7D,IAAI,QAAQ,OAAO,OAAO,MAAM,OAAO,IAAI,SAAS,OAAO,OAAO,QAAQ,CAAC,EAAE;CAC7E,OAAO,IAAI,SAAS,OAAO,OAAO,OAAO,QAAQ,CAAC,EAAE;AACtD;AAEA,IAAM,uBAAuB,SAA4C;CACvE,MAAM,IAAI,KAAK,YAAY;CAC3B,IAAI,EAAE,SAAS,KAAK,KAAK,EAAE,SAAS,OAAO,KAA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"}
@@ -107,7 +107,26 @@ var renderTextInEnvelope = (text, args) => {
107
107
  });
108
108
  };
109
109
  var renderSyntheticMediaDescription = (media, byteLen) => `[media: ${media.filename}, ${media.mimeType}, ${formatBytesHumanReadable(byteLen)}]`;
110
+ /**
111
+ * The inline media id-marker: a harness-authored text block rendered immediately BEFORE each
112
+ * media content block, so the model can reference the media by id in subsequent tool calls
113
+ * (`media_id` args, `@id` pipe refs).
114
+ *
115
+ * Trust posture: the marker is structural reference data authored by the harness from the
116
+ * harness-controlled `Media.id` (a UUID, not derivable from the payload) — it is NOT payload
117
+ * content, carries no authority, and deliberately renders OUTSIDE the untrusted envelope with
118
+ * fixed, non-instruction phrasing. This is a documented cross-battery convention: every LLM
119
+ * battery that renders media emits the same marker shape.
120
+ */
121
+ var renderMediaIdMarker = (media) => `[media id: ${media.id} | ${media.filename}]`;
110
122
  var renderMediaToContentBlocks = async (input) => {
123
+ const blocks = await renderMediaBodyBlocks(input);
124
+ return [{
125
+ type: "text",
126
+ text: renderMediaIdMarker(input.media)
127
+ }, ...blocks];
128
+ };
129
+ var renderMediaBodyBlocks = async (input) => {
111
130
  const { media, toolName, nonce, unsupportedMediaPolicy, warn } = input;
112
131
  const modality = modalityHazardToAttr(media.modalityHazard);
113
132
  const kind = media.kind;
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.mjs","names":[],"sources":["../../../../src/batteries/llm/openai_chat_completions/helpers.ts"],"sourcesContent":["/**\n * Swappable translation helpers for rendering ADK state into Chat Completions requests.\n *\n * @module @nhtio/adk/batteries/llm/openai_chat_completions/helpers\n *\n * @remarks\n * The swappable translation helpers that turn ADK primitives into OpenAI Chat Completions wire\n * shapes. Each helper is exported under its unprefixed name AND under a `default*` alias so\n * consumers can compose partial overrides. Helpers that compose other helpers receive their\n * dependents via explicit input arguments — never via module-level closure — so a swap at any\n * layer propagates correctly.\n *\n * The wire-shape-AGNOSTIC helpers (`renderUntrustedContent`, `renderMemories`,\n * `renderChatCompletionsSystemPrompt`, `toolsToChatCompletionsTools`, …) now live in the shared,\n * internal `../chat_common/helpers` submodule and are re-exported here under their original names\n * so every existing import keeps resolving. Only the OpenAI-WIRE-SPECIFIC helpers\n * (`renderTimelineMessage`, `renderChatCompletionsToolCallResult`, `buildChatCompletionsHistory`,\n * `createChatCompletionsToolCallDeltaAccumulator`) and the reasoning-field extractor are defined\n * here.\n */\n\nimport { Media } from '@nhtio/adk/common'\nimport { isInstanceOf } from '@nhtio/adk/guards'\nimport { E_UNSUPPORTED_MEDIA_MODALITY } from './exceptions'\nimport {\n escapeXmlAttribute,\n sanitiseNameField,\n memoryToAttrs,\n retrievableToAttrs,\n renderTrustedContent,\n renderUntrustedContent,\n} from '../chat_common/helpers'\nimport type { ChatHelpersCommon } from '../chat_common/types'\nimport type {\n Tool,\n ArtifactTool,\n ToolRegistry,\n Tokenizable,\n Memory,\n Message,\n Thought,\n ToolCall,\n Retrievable,\n SpooledArtifact,\n MediaModalityHazard,\n MediaStashEntry,\n} from '@nhtio/adk/common'\nimport type {\n ChatCompletionsMessage,\n ChatCompletionsContentBlock,\n ChatCompletionsToolCallDelta,\n ChatCompletionsToolCallDeltaAccumulator,\n AssembledToolCall,\n ChatCompletionsBucketOrder,\n MemoryAttrs,\n RetrievableAttrs,\n ChatCompletionsHelpers,\n UnsupportedMediaPolicy,\n ReasoningField,\n ReasoningFieldPrecedence,\n ReasoningExtract,\n} from './types'\n\n// ─── Re-exported wire-shape-agnostic helpers (shared submodule) ───────────────\n// These are defined once in `../chat_common/helpers` and shared with the native Ollama battery.\n// Re-exported here (bare + `default*`) so every existing\n// `@nhtio/adk/batteries/llm/openai_chat_completions` import keeps resolving unchanged.\nexport {\n descriptionToChatCompletionsJsonSchema,\n defaultDescriptionToChatCompletionsJsonSchema,\n renderUntrustedContent,\n defaultRenderUntrustedContent,\n renderTrustedContent,\n defaultRenderTrustedContent,\n renderStandingInstructions,\n defaultRenderStandingInstructions,\n renderMemories,\n defaultRenderMemories,\n renderRetrievableSafetyDirective,\n defaultRenderRetrievableSafetyDirective,\n renderFirstPartyRetrievables,\n defaultRenderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables,\n defaultRenderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables,\n defaultRenderThirdPartyPrivateRetrievables,\n renderRetrievables,\n defaultRenderRetrievables,\n renderThought,\n defaultRenderThought,\n filterThoughts,\n defaultFilterThoughts,\n toolsToChatCompletionsTools,\n defaultToolsToChatCompletionsTools,\n renderChatCompletionsSystemPrompt,\n defaultRenderChatCompletionsSystemPrompt,\n} from '../chat_common/helpers'\n\n// ─── extractReasoningFields ───────────────────────────────────────────────────\n\n/**\n * Pulls model reasoning/thinking text out of a Chat Completions response message or stream delta,\n * reading every wire field named in `precedence` that carries a non-empty string.\n *\n * @remarks\n * Reasoning is not part of OpenAI's official Chat Completions spec, so OpenAI-compatible providers\n * disagree on the field name (`reasoning` for Ollama and current vLLM; `reasoning_content` for\n * legacy vLLM and DeepSeek). This reads the union, in `precedence` order, and de-duplicates by\n * content value: a field whose text exactly matches one already kept is dropped.\n *\n * The result length encodes the emission rule the callers follow:\n * - `0` — no reasoning present.\n * - `1` — a single thought (covers \"only one field present\" AND \"several present but identical\").\n * - `≥2` — divergent fields; each surfaces as its own thought rather than silently dropping one.\n *\n * @param src - The response `message` or stream `delta` to read from.\n * @param precedence - Ordered, de-duplicated field names to read (see `reasoningFieldPrecedence`).\n * @returns The present, content-deduplicated reasoning traces in precedence order.\n */\nexport const extractReasoningFields = (\n src: Partial<Record<ReasoningField, string | null | undefined>> | undefined,\n precedence: ReasoningFieldPrecedence\n): ReasoningExtract[] => {\n const out: ReasoningExtract[] = []\n for (const field of precedence) {\n const value = src?.[field]\n if (typeof value !== 'string' || value.length === 0) continue\n if (out.some((e) => e.content === value)) continue\n out.push({ field, content: value })\n }\n return out\n}\n\n// ─── Media rendering helpers (OpenAI content-block specific) ──────────────────\n\nconst DEFAULT_STASH_FALLBACK_KEYS: ReadonlyArray<string> = [\n 'text:transcript',\n 'text:caption',\n 'text:description',\n]\n\nconst modalityHazardToAttr = (h: MediaModalityHazard): 'inert' | 'extractable' | 'opaque' => {\n if (h === 'inert') return 'inert'\n if (h === 'extractable-instructions') return 'extractable'\n return 'opaque'\n}\n\nconst formatBytesHumanReadable = (bytes: number | undefined): string => {\n if (bytes === undefined || !Number.isFinite(bytes)) return 'unknown size'\n if (bytes < 1024) return `${bytes} B`\n if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`\n if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`\n return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`\n}\n\nconst audioFormatFromMime = (mime: string): 'wav' | 'mp3' | undefined => {\n const m = mime.toLowerCase()\n if (m.includes('wav') || m.includes('x-wav') || m.includes('wave')) return 'wav'\n if (m.includes('mpeg') || m.includes('mp3')) return 'mp3'\n return undefined\n}\n\nconst isMediaTextStashEntry = (e: unknown): e is MediaStashEntry => {\n if (!e || typeof e !== 'object') return false\n const r = e as Record<string, unknown>\n return typeof r.value === 'string' && typeof r.trustTier === 'string'\n}\n\nconst resolveFallbackStash = (\n media: Media,\n keys: ReadonlyArray<string>\n): { text: string; entryTier: MediaStashEntry['trustTier'] } | undefined => {\n for (const key of keys) {\n const entry = media.stash.get(key)\n if (isMediaTextStashEntry(entry)) {\n return { text: entry.value as string, entryTier: entry.trustTier }\n }\n }\n return undefined\n}\n\nconst renderTextInEnvelope = (\n text: string,\n args: {\n trustTier: MediaStashEntry['trustTier']\n modality: 'inert' | 'extractable' | 'opaque'\n nonce: string\n toolName: string | undefined\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n }\n): string => {\n if (args.trustTier === 'first-party') {\n return args.renderTrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n }\n return args.renderUntrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n}\n\nconst renderSyntheticMediaDescription = (media: Media, byteLen: number | undefined): string =>\n `[media: ${media.filename}, ${media.mimeType}, ${formatBytesHumanReadable(byteLen)}]`\n\nconst renderMediaToContentBlocks = async (input: {\n media: Media\n toolName: string | undefined\n nonce: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsContentBlock[]> => {\n const { media, toolName, nonce, unsupportedMediaPolicy, warn } = input\n const modality = modalityHazardToAttr(media.modalityHazard)\n const kind = media.kind\n\n const fallbackPath = async (\n keys: ReadonlyArray<string>,\n allowSyntheticFallthrough: boolean\n ): Promise<ChatCompletionsContentBlock[]> => {\n const fallback = resolveFallbackStash(media, keys)\n if (fallback) {\n const text = renderTextInEnvelope(fallback.text, {\n trustTier: fallback.entryTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n if (!allowSyntheticFallthrough) {\n // 'fallback-stash' falls through to 'synthetic-description' when no entry is found.\n warn?.(\n `unsupportedMediaPolicy='fallback-stash' for ${media.filename}: no matching stash entry — falling through to synthetic description.`\n )\n }\n const byteLen = await media.byteLength()\n const text = renderTextInEnvelope(renderSyntheticMediaDescription(media, byteLen), {\n trustTier: media.trustTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n\n if (kind === 'image') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'image_url',\n image_url: { url: `data:${media.mimeType};base64,${b64}` },\n },\n ]\n }\n\n if (kind === 'audio') {\n const fmt = audioFormatFromMime(media.mimeType)\n if (fmt === undefined) {\n // Audio mime not natively expressible — same policy path as video.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' &&\n unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n }\n const data = await media.asBase64()\n return [\n {\n type: 'input_audio',\n input_audio: { data, format: fmt },\n },\n ]\n }\n\n if (kind === 'document') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'file',\n file: {\n filename: media.filename,\n file_data: `data:${media.mimeType};base64,${b64}`,\n },\n },\n ]\n }\n\n // kind === 'video' — not natively supported by Chat Completions wire format.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' && unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n}\n\n// ─── renderTimelineMessage ────────────────────────────────────────────────────\n\n/**\n * Renders a single timeline {@link @nhtio/adk!Message} into an OpenAI Chat Completions message —\n * mapping media to content blocks (`image_url` / `input_audio` / `file`), wrapping textual bodies\n * in the appropriate trust envelope, and applying the unsupported-media policy.\n */\nexport const renderTimelineMessage = async (input: {\n message: Message\n selfIdentity: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsMessage> => {\n const { message, selfIdentity, unsupportedMediaPolicy, warn } = input\n const identifier =\n message.identity?.identifier !== undefined && message.identity?.identifier !== null\n ? String(message.identity.identifier)\n : ''\n const representationRaw =\n message.identity?.representation !== undefined && message.identity?.representation !== null\n ? message.identity.representation.toString()\n : ''\n // Prompt-facing identity (the `from=` attribute) reads `representation`;\n // structural `messages[].name` reads `identifier`. Fall back to `identifier`\n // when `representation` is empty so a bare-string identity still renders.\n const representation = representationRaw.length > 0 ? representationRaw : identifier\n const text = message.content !== undefined ? message.content.toString() : ''\n const createdAtStr = message.createdAt.toISO?.() ?? ''\n const createdAtAttr = createdAtStr ? ` createdAt=\"${escapeXmlAttribute(createdAtStr)}\"` : ''\n const attachments = message.attachments\n const hasAttachments = attachments.length > 0\n\n // Build the text envelope first (same logic as before).\n let envelopeText: string\n let nameField: string | undefined\n let role: 'user' | 'assistant'\n if (message.role === 'user') {\n role = 'user'\n if (identifier.length === 0) {\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<message_${message.id} from=\"${fromAttr}\" role=\"user\"${createdAtAttr}>\\n${text}\\n</message_${message.id}>`\n }\n } else {\n role = 'assistant'\n if (identifier.length === 0 || identifier === selfIdentity) {\n if (identifier.length > 0) {\n nameField = sanitiseNameField(identifier)\n }\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<peer_agent_output_${message.id} from=\"${fromAttr}\"${createdAtAttr}>\\n${text}\\n</peer_agent_output_${message.id}>`\n }\n }\n\n if (!hasAttachments) {\n const out: ChatCompletionsMessage = { role, content: envelopeText }\n if (nameField !== undefined) out.name = nameField\n return out\n }\n\n // Content-array path: text first (when present), then attachment blocks in array order.\n const blocks: ChatCompletionsContentBlock[] = []\n if (text.length > 0) {\n blocks.push({ type: 'text', text: envelopeText })\n }\n for (const media of attachments) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: undefined,\n nonce: message.id,\n unsupportedMediaPolicy,\n renderTrustedContent,\n renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n const out: ChatCompletionsMessage = { role, content: blocks }\n if (nameField !== undefined) out.name = nameField\n return out\n}\n/** Default timeline-message renderer; alias of {@link renderTimelineMessage}. */\nexport const defaultRenderTimelineMessage = renderTimelineMessage\n\n// ─── renderChatCompletionsToolCallResult ──────────────────────────────────────\n\nconst isSpooledArtifactResult = (\n results: SpooledArtifact | Tokenizable\n): results is SpooledArtifact =>\n isInstanceOf(results, 'SpooledArtifact') ||\n // Subclasses identify via the base class guard upstream\n ((results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== null &&\n (results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== undefined &&\n typeof (\n results as unknown as {\n constructor: { isSpooledArtifactConstructor?: (c: unknown) => boolean }\n }\n ).constructor.isSpooledArtifactConstructor === 'function')\n\nconst looksLikeSpooledArtifact = (value: unknown): value is SpooledArtifact => {\n if (!value || typeof value !== 'object') return false\n const v = value as Record<string, unknown>\n return (\n typeof v.asString === 'function' &&\n typeof v.byteLength === 'function' &&\n typeof v.lineCount === 'function' &&\n typeof v.estimateTokens === 'function'\n )\n}\n\nconst renderArtifactHandleBody = (\n toolCall: ToolCall,\n artifact: SpooledArtifact,\n byteLength: number,\n lineCount: number,\n estimatedTokens: number | undefined,\n encoding: string | undefined\n): string => {\n const ctor = (\n artifact as unknown as {\n constructor: { toolMethods?: ReadonlyArray<{ name: string; description?: string }> }\n }\n ).constructor\n const methods = ctor?.toolMethods ?? []\n const lines: string[] = []\n lines.push(`This tool returned a large artifact that was not inlined to preserve context budget.`)\n lines.push(``)\n lines.push(`Artifact metadata:`)\n lines.push(`- callId: ${toolCall.id}`)\n lines.push(`- kind: ${ctor?.constructor?.name ?? 'SpooledArtifact'}`)\n lines.push(`- byteLength: ${byteLength}`)\n lines.push(`- lineCount: ${lineCount}`)\n if (estimatedTokens !== undefined && encoding) {\n lines.push(`- estimatedTokens: ${estimatedTokens} (encoding: ${encoding})`)\n }\n lines.push(``)\n lines.push(`To read this artifact in this turn, call one of the following tools with`)\n lines.push(`callId=${toolCall.id}:`)\n for (const m of methods) {\n if (m.description) {\n lines.push(`- ${m.name} — ${m.description}`)\n } else {\n lines.push(`- ${m.name}`)\n }\n }\n lines.push(``)\n lines.push(\n `The artifact persists in this turn's context — multiple queries against the same callId are allowed and efficient. Do not assume the body has been inlined anywhere else.`\n )\n return lines.join('\\n')\n}\n\n/**\n * Renders a tool call's result(s) into the OpenAI Chat Completions tool-message body — either a\n * plain string or an array of content blocks when the result carries media — wrapping textual\n * output in the trust envelope appropriate to the tool's trust level.\n */\nexport const renderChatCompletionsToolCallResult = async (input: {\n toolCall: ToolCall\n results: Tokenizable | SpooledArtifact | SpooledArtifact[] | Media | Media[]\n tool: Tool | ArtifactTool | undefined\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<string | ChatCompletionsContentBlock[]> => {\n const { toolCall, results, tool, warn, unsupportedMediaPolicy } = input\n const isTrusted =\n tool !== null && tool !== undefined && (tool as { trusted?: boolean }).trusted === true\n\n if (tool === undefined) {\n warn?.(\n `Tool \"${toolCall.tool}\" is not present in the bound tool registry at render time; defaulting to untrusted envelope.`\n )\n }\n\n // Media / Media[] silo — bypasses Tool.trusted (Trust-Is-Content rule). Envelope is sourced\n // from each Media's own trustTier; modality from each Media's modalityHazard.\n const isMediaResult = Media.isMedia(results)\n const isMediaArrayResult =\n Array.isArray(results) && results.length > 0 && results.every((r) => Media.isMedia(r))\n if (isMediaResult || isMediaArrayResult) {\n const mediaList = isMediaResult ? [results as Media] : (results as Media[])\n const blocks: ChatCompletionsContentBlock[] = []\n for (const media of mediaList) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: toolCall.tool,\n nonce: toolCall.checksum,\n unsupportedMediaPolicy,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n return blocks\n }\n\n // SpooledArtifact[] silo — render each artifact through the existing single-artifact path\n // and concatenate the bodies. Trust envelope is decided per-artifact via the surrounding\n // Tool.trusted flag (same as single SpooledArtifact today).\n if (Array.isArray(results)) {\n const parts: string[] = []\n for (const a of results) {\n const body = await (a as SpooledArtifact).asString()\n parts.push(body)\n }\n const joined = parts.join('\\n\\n')\n if (isTrusted) {\n return input.renderTrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n }\n\n const isSpooled = looksLikeSpooledArtifact(results)\n\n // Handle-pattern branch: spooled + inline=false → always untrusted (queryable-data, not policy).\n if (isSpooled && toolCall.inline === false) {\n const artifact = results as SpooledArtifact\n let byteLength = 0\n let lineCount = 0\n try {\n byteLength = await artifact.byteLength()\n } catch {\n byteLength = 0\n }\n try {\n lineCount = await artifact.lineCount()\n } catch {\n lineCount = 0\n }\n const body = renderArtifactHandleBody(\n toolCall,\n artifact,\n byteLength,\n lineCount,\n undefined,\n undefined\n )\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'artifact-handle',\n tool: toolCall.tool,\n })\n }\n\n // Inline path: render full body via the appropriate envelope.\n if (!isSpooled && toolCall.inline === false) {\n warn?.(\n `Tool call ${toolCall.id} has inline=false but results is a Tokenizable (not a SpooledArtifact); rendering inline anyway.`\n )\n }\n\n let body: string\n if (isSpooled) {\n body = await (results as SpooledArtifact).asString()\n } else {\n body = (results as Tokenizable).toString()\n }\n\n if (isTrusted) {\n return input.renderTrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n}\n/** Default tool-call-result renderer; alias of {@link renderChatCompletionsToolCallResult}. */\nexport const defaultRenderChatCompletionsToolCallResult = renderChatCompletionsToolCallResult\n\n// suppress unused; kept for forward-compat with stricter spool guards\nvoid isSpooledArtifactResult\n\n// ─── buildChatCompletionsHistory ──────────────────────────────────────────────\n\ntype TimelineItem =\n | { kind: 'message'; createdAt: number; value: Message }\n | { kind: 'thought'; createdAt: number; value: Thought }\n | { kind: 'toolCall'; createdAt: number; value: ToolCall }\n\n/**\n * Assembles the complete OpenAI Chat Completions message history for a dispatch — system prompt and\n * content buckets, the interleaved timeline of messages/thoughts/tool calls (with synthetic\n * `assistant.tool_calls` and `tool.tool_call_id` shaping), and the collected opaque reasoning\n * payloads — by delegating to the injected sub-renderers.\n */\nexport const buildChatCompletionsHistory = async (input: {\n systemPrompt: Tokenizable\n standingInstructions: Iterable<Tokenizable>\n memories: Iterable<Memory>\n retrievables: Iterable<Retrievable>\n messages: Iterable<Message>\n thoughts: Iterable<Thought>\n toolCalls: Iterable<ToolCall>\n tools: ToolRegistry\n renderedToolCallResults: Map<string, string | ChatCompletionsContentBlock[]>\n bucketOrder: ChatCompletionsBucketOrder\n selfIdentity: string\n thoughtSurfacing: 'all-self' | 'latest-self' | 'all'\n replayCompatibility: ReadonlyArray<string>\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderChatCompletionsToolCallResult: ChatCompletionsHelpers['renderChatCompletionsToolCallResult']\n renderChatCompletionsSystemPrompt: ChatHelpersCommon['renderChatCompletionsSystemPrompt']\n renderStandingInstructions: ChatHelpersCommon['renderStandingInstructions']\n renderMemories: ChatHelpersCommon['renderMemories']\n renderRetrievables: ChatHelpersCommon['renderRetrievables']\n renderRetrievableSafetyDirective: ChatHelpersCommon['renderRetrievableSafetyDirective']\n renderFirstPartyRetrievables: ChatHelpersCommon['renderFirstPartyRetrievables']\n renderThirdPartyPublicRetrievables: ChatHelpersCommon['renderThirdPartyPublicRetrievables']\n renderThirdPartyPrivateRetrievables: ChatHelpersCommon['renderThirdPartyPrivateRetrievables']\n renderTimelineMessage: ChatCompletionsHelpers['renderTimelineMessage']\n renderThought: ChatHelpersCommon['renderThought']\n filterThoughts: ChatHelpersCommon['filterThoughts']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n warn?: (msg: string) => void\n}): Promise<{\n messages: ChatCompletionsMessage[]\n reasoningPayloads: Array<{ id: string; replayCompatibility: string; payload: unknown }>\n}> => {\n const out: ChatCompletionsMessage[] = []\n const reasoningPayloads: Array<{\n id: string\n replayCompatibility: string\n payload: unknown\n }> = []\n\n const buckets = input.bucketOrder\n const timelineIdx = buckets.indexOf('timeline')\n\n // Build leading system content from base prompt + before-timeline buckets.\n const leadingSystem = await input.renderChatCompletionsSystemPrompt({\n systemPrompt: input.systemPrompt,\n standingInstructions: input.standingInstructions,\n memories: input.memories,\n retrievables: input.retrievables,\n bucketOrder: buckets,\n renderStandingInstructions: input.renderStandingInstructions,\n renderMemories: input.renderMemories,\n renderRetrievables: input.renderRetrievables,\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (leadingSystem.length > 0) {\n out.push({ role: 'system', content: leadingSystem })\n }\n\n // Build the timeline (if present in bucketOrder).\n const includesTimeline = timelineIdx !== -1\n if (includesTimeline) {\n // Filter thoughts per surfacing mode + compatibility.\n const survivingThoughts = input.filterThoughts(\n input.thoughts,\n input.thoughtSurfacing,\n input.selfIdentity,\n input.replayCompatibility\n )\n\n // Build sorted timeline items.\n const items: TimelineItem[] = []\n for (const m of input.messages) {\n items.push({ kind: 'message', createdAt: m.createdAt.toMillis(), value: m })\n }\n for (const t of survivingThoughts) {\n items.push({ kind: 'thought', createdAt: t.createdAt.toMillis(), value: t })\n }\n for (const tc of input.toolCalls) {\n items.push({ kind: 'toolCall', createdAt: tc.createdAt.toMillis(), value: tc })\n }\n items.sort((a, b) => a.createdAt - b.createdAt)\n\n const replaySet = new Set<string>([...input.replayCompatibility])\n\n for (const item of items) {\n if (item.kind === 'message') {\n out.push(\n await input.renderTimelineMessage({\n message: item.value,\n selfIdentity: input.selfIdentity,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n )\n } else if (item.kind === 'thought') {\n const t = item.value\n const identifier = String(t.identity?.identifier ?? '')\n const isSelf = identifier === input.selfIdentity\n const hasPayload = t.payload !== undefined\n const compatTag = t.replayCompatibility\n\n if (hasPayload && compatTag && replaySet.has(compatTag)) {\n // Opaque reasoning — side-channel + summary envelope.\n reasoningPayloads.push({\n id: t.id,\n replayCompatibility: compatTag,\n payload: t.payload,\n })\n const envelope = input.renderThought(\n t.content.toString(),\n {\n nonce: t.id,\n kind: 'opaque-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n replayCompatibility: compatTag,\n },\n t.payload\n )\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n } else if (!hasPayload) {\n // Plain-text reasoning (no payload, or tagged plain-text, or tagged but matched).\n const envelope = input.renderThought(t.content.toString(), {\n nonce: t.id,\n kind: isSelf ? 'self-reasoning' : 'peer-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n })\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n }\n // else: opaque, non-matching → elided (NOT removed from ctx.turnThoughts upstream).\n } else {\n // tool call: emit a synthetic assistant message carrying tool_calls[],\n // followed by a tool-role message with the result.\n const tc = item.value\n const assistantMsg: ChatCompletionsMessage = {\n role: 'assistant',\n content: null,\n tool_calls: [\n {\n id: tc.id,\n type: 'function',\n function: {\n name: tc.tool,\n arguments: typeof tc.args === 'string' ? tc.args : JSON.stringify(tc.args ?? {}),\n },\n },\n ],\n }\n out.push(assistantMsg)\n\n let rendered = input.renderedToolCallResults.get(tc.id)\n if (rendered === undefined) {\n const tool = input.tools.get?.(tc.tool)\n rendered = await input.renderChatCompletionsToolCallResult({\n toolCall: tc,\n results: tc.results as\n | Tokenizable\n | SpooledArtifact\n | SpooledArtifact[]\n | Media\n | Media[],\n tool: tool as Tool | ArtifactTool | undefined,\n renderUntrustedContent: input.renderUntrustedContent,\n renderTrustedContent: input.renderTrustedContent,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n }\n out.push({\n role: 'tool',\n content: rendered,\n tool_call_id: tc.id,\n })\n }\n }\n }\n\n // Trailing system message for after-timeline buckets.\n if (includesTimeline) {\n const trailingParts: string[] = []\n for (let i = timelineIdx + 1; i < buckets.length; i++) {\n const label = buckets[i]!\n if (label === 'standingInstructions') {\n const block = input.renderStandingInstructions(input.standingInstructions)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'memories') {\n const wrapped: Array<{ memory: Memory; attrs: MemoryAttrs }> = []\n for (const m of input.memories) {\n wrapped.push(memoryToAttrs(m))\n }\n const block = input.renderMemories(wrapped)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'retrievables') {\n const wrapped: Array<{ retrievable: Retrievable; attrs: RetrievableAttrs }> = []\n for (const r of input.retrievables) {\n wrapped.push(retrievableToAttrs(r))\n }\n const block = await input.renderRetrievables(wrapped, {\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (block.length > 0) trailingParts.push(block)\n }\n }\n if (trailingParts.length > 0) {\n out.push({ role: 'system', content: trailingParts.join('\\n\\n') })\n }\n }\n\n return { messages: out, reasoningPayloads }\n}\n/** Default history assembler; alias of {@link buildChatCompletionsHistory}. */\nexport const defaultBuildChatCompletionsHistory = buildChatCompletionsHistory\n\n// ─── createChatCompletionsToolCallDeltaAccumulator ────────────────────────────\n\n/**\n * Creates a fresh accumulator that stitches streamed {@link ChatCompletionsToolCallDelta} fragments\n * (keyed by their stream index) into fully-assembled tool calls, drained once the stream completes.\n */\nexport const createChatCompletionsToolCallDeltaAccumulator =\n (): ChatCompletionsToolCallDeltaAccumulator => {\n const byIndex = new Map<\n number,\n { id?: string; type?: 'function'; name: string; args: string }\n >()\n return {\n feed(delta: ChatCompletionsToolCallDelta): void {\n const idx = delta.index\n let entry = byIndex.get(idx)\n if (!entry) {\n entry = { name: '', args: '' }\n byIndex.set(idx, entry)\n }\n if (delta.id !== undefined) entry.id = delta.id\n if (delta.type !== undefined) entry.type = delta.type\n if (delta.function?.name !== undefined) {\n entry.name = entry.name + delta.function.name\n }\n if (delta.function?.arguments !== undefined) {\n entry.args = entry.args + delta.function.arguments\n }\n },\n drain(): AssembledToolCall[] {\n const out: AssembledToolCall[] = []\n const indices = Array.from(byIndex.keys()).sort((a, b) => a - b)\n for (const idx of indices) {\n const e = byIndex.get(idx)!\n out.push({\n id: e.id ?? `call_${idx}`,\n type: e.type ?? 'function',\n name: e.name,\n args: e.args,\n })\n }\n return out\n },\n }\n }\n/** Default delta-accumulator factory; alias of {@link createChatCompletionsToolCallDeltaAccumulator}. */\nexport const defaultCreateChatCompletionsToolCallDeltaAccumulator =\n createChatCompletionsToolCallDeltaAccumulator\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuHA,IAAa,0BACX,KACA,eACuB;CACv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,MAAM,UAAU,SAAS,KAAA,GAC3B,MAAM,OAAO,MAAM,OAAO,MAAM,SAAS;GAE3C,IAAI,MAAM,UAAU,cAAc,KAAA,GAChC,MAAM,OAAO,MAAM,OAAO,MAAM,SAAS;EAE7C;EACA,QAA6B;GAC3B,MAAM,MAA2B,CAAC;GAClC,MAAM,UAAU,MAAM,KAAK,QAAQ,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,IAAI,CAAC;GAC/D,KAAK,MAAM,OAAO,SAAS;IACzB,MAAM,IAAI,QAAQ,IAAI,GAAG;IACzB,IAAI,KAAK;KACP,IAAI,EAAE,MAAM,QAAQ;KACpB,MAAM,EAAE,QAAQ;KAChB,MAAM,EAAE;KACR,MAAM,EAAE;IACV,CAAC;GACH;GACA,OAAO;EACT;CACF;AACF;;AAEF,IAAa,uDACX"}
1
+ {"version":3,"file":"helpers.mjs","names":[],"sources":["../../../../src/batteries/llm/openai_chat_completions/helpers.ts"],"sourcesContent":["/**\n * Swappable translation helpers for rendering ADK state into Chat Completions requests.\n *\n * @module @nhtio/adk/batteries/llm/openai_chat_completions/helpers\n *\n * @remarks\n * The swappable translation helpers that turn ADK primitives into OpenAI Chat Completions wire\n * shapes. Each helper is exported under its unprefixed name AND under a `default*` alias so\n * consumers can compose partial overrides. Helpers that compose other helpers receive their\n * dependents via explicit input arguments — never via module-level closure — so a swap at any\n * layer propagates correctly.\n *\n * The wire-shape-AGNOSTIC helpers (`renderUntrustedContent`, `renderMemories`,\n * `renderChatCompletionsSystemPrompt`, `toolsToChatCompletionsTools`, …) now live in the shared,\n * internal `../chat_common/helpers` submodule and are re-exported here under their original names\n * so every existing import keeps resolving. Only the OpenAI-WIRE-SPECIFIC helpers\n * (`renderTimelineMessage`, `renderChatCompletionsToolCallResult`, `buildChatCompletionsHistory`,\n * `createChatCompletionsToolCallDeltaAccumulator`) and the reasoning-field extractor are defined\n * here.\n */\n\nimport { Media } from '@nhtio/adk/common'\nimport { isInstanceOf } from '@nhtio/adk/guards'\nimport { E_UNSUPPORTED_MEDIA_MODALITY } from './exceptions'\nimport {\n escapeXmlAttribute,\n sanitiseNameField,\n memoryToAttrs,\n retrievableToAttrs,\n renderTrustedContent,\n renderUntrustedContent,\n} from '../chat_common/helpers'\nimport type { ChatHelpersCommon } from '../chat_common/types'\nimport type {\n Tool,\n ArtifactTool,\n ToolRegistry,\n Tokenizable,\n Memory,\n Message,\n Thought,\n ToolCall,\n Retrievable,\n SpooledArtifact,\n MediaModalityHazard,\n MediaStashEntry,\n} from '@nhtio/adk/common'\nimport type {\n ChatCompletionsMessage,\n ChatCompletionsContentBlock,\n ChatCompletionsToolCallDelta,\n ChatCompletionsToolCallDeltaAccumulator,\n AssembledToolCall,\n ChatCompletionsBucketOrder,\n MemoryAttrs,\n RetrievableAttrs,\n ChatCompletionsHelpers,\n UnsupportedMediaPolicy,\n ReasoningField,\n ReasoningFieldPrecedence,\n ReasoningExtract,\n} from './types'\n\n// ─── Re-exported wire-shape-agnostic helpers (shared submodule) ───────────────\n// These are defined once in `../chat_common/helpers` and shared with the native Ollama battery.\n// Re-exported here (bare + `default*`) so every existing\n// `@nhtio/adk/batteries/llm/openai_chat_completions` import keeps resolving unchanged.\nexport {\n descriptionToChatCompletionsJsonSchema,\n defaultDescriptionToChatCompletionsJsonSchema,\n renderUntrustedContent,\n defaultRenderUntrustedContent,\n renderTrustedContent,\n defaultRenderTrustedContent,\n renderStandingInstructions,\n defaultRenderStandingInstructions,\n renderMemories,\n defaultRenderMemories,\n renderRetrievableSafetyDirective,\n defaultRenderRetrievableSafetyDirective,\n renderFirstPartyRetrievables,\n defaultRenderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables,\n defaultRenderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables,\n defaultRenderThirdPartyPrivateRetrievables,\n renderRetrievables,\n defaultRenderRetrievables,\n renderThought,\n defaultRenderThought,\n filterThoughts,\n defaultFilterThoughts,\n toolsToChatCompletionsTools,\n defaultToolsToChatCompletionsTools,\n renderChatCompletionsSystemPrompt,\n defaultRenderChatCompletionsSystemPrompt,\n} from '../chat_common/helpers'\n\n// ─── extractReasoningFields ───────────────────────────────────────────────────\n\n/**\n * Pulls model reasoning/thinking text out of a Chat Completions response message or stream delta,\n * reading every wire field named in `precedence` that carries a non-empty string.\n *\n * @remarks\n * Reasoning is not part of OpenAI's official Chat Completions spec, so OpenAI-compatible providers\n * disagree on the field name (`reasoning` for Ollama and current vLLM; `reasoning_content` for\n * legacy vLLM and DeepSeek). This reads the union, in `precedence` order, and de-duplicates by\n * content value: a field whose text exactly matches one already kept is dropped.\n *\n * The result length encodes the emission rule the callers follow:\n * - `0` — no reasoning present.\n * - `1` — a single thought (covers \"only one field present\" AND \"several present but identical\").\n * - `≥2` — divergent fields; each surfaces as its own thought rather than silently dropping one.\n *\n * @param src - The response `message` or stream `delta` to read from.\n * @param precedence - Ordered, de-duplicated field names to read (see `reasoningFieldPrecedence`).\n * @returns The present, content-deduplicated reasoning traces in precedence order.\n */\nexport const extractReasoningFields = (\n src: Partial<Record<ReasoningField, string | null | undefined>> | undefined,\n precedence: ReasoningFieldPrecedence\n): ReasoningExtract[] => {\n const out: ReasoningExtract[] = []\n for (const field of precedence) {\n const value = src?.[field]\n if (typeof value !== 'string' || value.length === 0) continue\n if (out.some((e) => e.content === value)) continue\n out.push({ field, content: value })\n }\n return out\n}\n\n// ─── Media rendering helpers (OpenAI content-block specific) ──────────────────\n\nconst DEFAULT_STASH_FALLBACK_KEYS: ReadonlyArray<string> = [\n 'text:transcript',\n 'text:caption',\n 'text:description',\n]\n\nconst modalityHazardToAttr = (h: MediaModalityHazard): 'inert' | 'extractable' | 'opaque' => {\n if (h === 'inert') return 'inert'\n if (h === 'extractable-instructions') return 'extractable'\n return 'opaque'\n}\n\nconst formatBytesHumanReadable = (bytes: number | undefined): string => {\n if (bytes === undefined || !Number.isFinite(bytes)) return 'unknown size'\n if (bytes < 1024) return `${bytes} B`\n if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`\n if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`\n return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`\n}\n\nconst audioFormatFromMime = (mime: string): 'wav' | 'mp3' | undefined => {\n const m = mime.toLowerCase()\n if (m.includes('wav') || m.includes('x-wav') || m.includes('wave')) return 'wav'\n if (m.includes('mpeg') || m.includes('mp3')) return 'mp3'\n return undefined\n}\n\nconst isMediaTextStashEntry = (e: unknown): e is MediaStashEntry => {\n if (!e || typeof e !== 'object') return false\n const r = e as Record<string, unknown>\n return typeof r.value === 'string' && typeof r.trustTier === 'string'\n}\n\nconst resolveFallbackStash = (\n media: Media,\n keys: ReadonlyArray<string>\n): { text: string; entryTier: MediaStashEntry['trustTier'] } | undefined => {\n for (const key of keys) {\n const entry = media.stash.get(key)\n if (isMediaTextStashEntry(entry)) {\n return { text: entry.value as string, entryTier: entry.trustTier }\n }\n }\n return undefined\n}\n\nconst renderTextInEnvelope = (\n text: string,\n args: {\n trustTier: MediaStashEntry['trustTier']\n modality: 'inert' | 'extractable' | 'opaque'\n nonce: string\n toolName: string | undefined\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n }\n): string => {\n if (args.trustTier === 'first-party') {\n return args.renderTrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n }\n return args.renderUntrustedContent(text, {\n nonce: args.nonce,\n kind: 'media-fallback',\n tool: args.toolName,\n modality: args.modality,\n })\n}\n\nconst renderSyntheticMediaDescription = (media: Media, byteLen: number | undefined): string =>\n `[media: ${media.filename}, ${media.mimeType}, ${formatBytesHumanReadable(byteLen)}]`\n\n/**\n * The inline media id-marker: a harness-authored text block rendered immediately BEFORE each\n * media content block, so the model can reference the media by id in subsequent tool calls\n * (`media_id` args, `@id` pipe refs).\n *\n * Trust posture: the marker is structural reference data authored by the harness from the\n * harness-controlled `Media.id` (a UUID, not derivable from the payload) — it is NOT payload\n * content, carries no authority, and deliberately renders OUTSIDE the untrusted envelope with\n * fixed, non-instruction phrasing. This is a documented cross-battery convention: every LLM\n * battery that renders media emits the same marker shape.\n */\nconst renderMediaIdMarker = (media: Media): string => `[media id: ${media.id} | ${media.filename}]`\n\nconst renderMediaToContentBlocks = async (input: {\n media: Media\n toolName: string | undefined\n nonce: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsContentBlock[]> => {\n const blocks = await renderMediaBodyBlocks(input)\n return [{ type: 'text', text: renderMediaIdMarker(input.media) }, ...blocks]\n}\n\nconst renderMediaBodyBlocks = async (input: {\n media: Media\n toolName: string | undefined\n nonce: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsContentBlock[]> => {\n const { media, toolName, nonce, unsupportedMediaPolicy, warn } = input\n const modality = modalityHazardToAttr(media.modalityHazard)\n const kind = media.kind\n\n const fallbackPath = async (\n keys: ReadonlyArray<string>,\n allowSyntheticFallthrough: boolean\n ): Promise<ChatCompletionsContentBlock[]> => {\n const fallback = resolveFallbackStash(media, keys)\n if (fallback) {\n const text = renderTextInEnvelope(fallback.text, {\n trustTier: fallback.entryTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n if (!allowSyntheticFallthrough) {\n // 'fallback-stash' falls through to 'synthetic-description' when no entry is found.\n warn?.(\n `unsupportedMediaPolicy='fallback-stash' for ${media.filename}: no matching stash entry — falling through to synthetic description.`\n )\n }\n const byteLen = await media.byteLength()\n const text = renderTextInEnvelope(renderSyntheticMediaDescription(media, byteLen), {\n trustTier: media.trustTier,\n modality,\n nonce,\n toolName,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n return [{ type: 'text', text }]\n }\n\n if (kind === 'image') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'image_url',\n image_url: { url: `data:${media.mimeType};base64,${b64}` },\n },\n ]\n }\n\n if (kind === 'audio') {\n const fmt = audioFormatFromMime(media.mimeType)\n if (fmt === undefined) {\n // Audio mime not natively expressible — same policy path as video.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' &&\n unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n }\n const data = await media.asBase64()\n return [\n {\n type: 'input_audio',\n input_audio: { data, format: fmt },\n },\n ]\n }\n\n if (kind === 'document') {\n const b64 = await media.asBase64()\n return [\n {\n type: 'file',\n file: {\n filename: media.filename,\n file_data: `data:${media.mimeType};base64,${b64}`,\n },\n },\n ]\n }\n\n // kind === 'video' — not natively supported by Chat Completions wire format.\n if (unsupportedMediaPolicy === 'throw') {\n throw new E_UNSUPPORTED_MEDIA_MODALITY([media.kind, media.mimeType, media.filename])\n }\n if (\n unsupportedMediaPolicy === 'fallback-stash' ||\n (typeof unsupportedMediaPolicy === 'object' && unsupportedMediaPolicy.mode === 'fallback-stash')\n ) {\n const keys =\n typeof unsupportedMediaPolicy === 'object'\n ? unsupportedMediaPolicy.stashKeys\n : DEFAULT_STASH_FALLBACK_KEYS\n return fallbackPath(keys, false)\n }\n return fallbackPath([], true)\n}\n\n// ─── renderTimelineMessage ────────────────────────────────────────────────────\n\n/**\n * Renders a single timeline {@link @nhtio/adk!Message} into an OpenAI Chat Completions message —\n * mapping media to content blocks (`image_url` / `input_audio` / `file`), wrapping textual bodies\n * in the appropriate trust envelope, and applying the unsupported-media policy.\n */\nexport const renderTimelineMessage = async (input: {\n message: Message\n selfIdentity: string\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<ChatCompletionsMessage> => {\n const { message, selfIdentity, unsupportedMediaPolicy, warn } = input\n const identifier =\n message.identity?.identifier !== undefined && message.identity?.identifier !== null\n ? String(message.identity.identifier)\n : ''\n const representationRaw =\n message.identity?.representation !== undefined && message.identity?.representation !== null\n ? message.identity.representation.toString()\n : ''\n // Prompt-facing identity (the `from=` attribute) reads `representation`;\n // structural `messages[].name` reads `identifier`. Fall back to `identifier`\n // when `representation` is empty so a bare-string identity still renders.\n const representation = representationRaw.length > 0 ? representationRaw : identifier\n const text = message.content !== undefined ? message.content.toString() : ''\n const createdAtStr = message.createdAt.toISO?.() ?? ''\n const createdAtAttr = createdAtStr ? ` createdAt=\"${escapeXmlAttribute(createdAtStr)}\"` : ''\n const attachments = message.attachments\n const hasAttachments = attachments.length > 0\n\n // Build the text envelope first (same logic as before).\n let envelopeText: string\n let nameField: string | undefined\n let role: 'user' | 'assistant'\n if (message.role === 'user') {\n role = 'user'\n if (identifier.length === 0) {\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<message_${message.id} from=\"${fromAttr}\" role=\"user\"${createdAtAttr}>\\n${text}\\n</message_${message.id}>`\n }\n } else {\n role = 'assistant'\n if (identifier.length === 0 || identifier === selfIdentity) {\n if (identifier.length > 0) {\n nameField = sanitiseNameField(identifier)\n }\n envelopeText = text\n } else {\n nameField = sanitiseNameField(identifier)\n const fromAttr = escapeXmlAttribute(representation)\n envelopeText = `<peer_agent_output_${message.id} from=\"${fromAttr}\"${createdAtAttr}>\\n${text}\\n</peer_agent_output_${message.id}>`\n }\n }\n\n if (!hasAttachments) {\n const out: ChatCompletionsMessage = { role, content: envelopeText }\n if (nameField !== undefined) out.name = nameField\n return out\n }\n\n // Content-array path: text first (when present), then attachment blocks in array order.\n const blocks: ChatCompletionsContentBlock[] = []\n if (text.length > 0) {\n blocks.push({ type: 'text', text: envelopeText })\n }\n for (const media of attachments) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: undefined,\n nonce: message.id,\n unsupportedMediaPolicy,\n renderTrustedContent,\n renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n const out: ChatCompletionsMessage = { role, content: blocks }\n if (nameField !== undefined) out.name = nameField\n return out\n}\n/** Default timeline-message renderer; alias of {@link renderTimelineMessage}. */\nexport const defaultRenderTimelineMessage = renderTimelineMessage\n\n// ─── renderChatCompletionsToolCallResult ──────────────────────────────────────\n\nconst isSpooledArtifactResult = (\n results: SpooledArtifact | Tokenizable\n): results is SpooledArtifact =>\n isInstanceOf(results, 'SpooledArtifact') ||\n // Subclasses identify via the base class guard upstream\n ((results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== null &&\n (results as unknown as { constructor?: { isSpooledArtifactConstructor?: boolean } })\n ?.constructor !== undefined &&\n typeof (\n results as unknown as {\n constructor: { isSpooledArtifactConstructor?: (c: unknown) => boolean }\n }\n ).constructor.isSpooledArtifactConstructor === 'function')\n\nconst looksLikeSpooledArtifact = (value: unknown): value is SpooledArtifact => {\n if (!value || typeof value !== 'object') return false\n const v = value as Record<string, unknown>\n return (\n typeof v.asString === 'function' &&\n typeof v.byteLength === 'function' &&\n typeof v.lineCount === 'function' &&\n typeof v.estimateTokens === 'function'\n )\n}\n\nconst renderArtifactHandleBody = (\n toolCall: ToolCall,\n artifact: SpooledArtifact,\n byteLength: number,\n lineCount: number,\n estimatedTokens: number | undefined,\n encoding: string | undefined\n): string => {\n const ctor = (\n artifact as unknown as {\n constructor: { toolMethods?: ReadonlyArray<{ name: string; description?: string }> }\n }\n ).constructor\n const methods = ctor?.toolMethods ?? []\n const lines: string[] = []\n lines.push(`This tool returned a large artifact that was not inlined to preserve context budget.`)\n lines.push(``)\n lines.push(`Artifact metadata:`)\n lines.push(`- callId: ${toolCall.id}`)\n lines.push(`- kind: ${ctor?.constructor?.name ?? 'SpooledArtifact'}`)\n lines.push(`- byteLength: ${byteLength}`)\n lines.push(`- lineCount: ${lineCount}`)\n if (estimatedTokens !== undefined && encoding) {\n lines.push(`- estimatedTokens: ${estimatedTokens} (encoding: ${encoding})`)\n }\n lines.push(``)\n lines.push(`To read this artifact in this turn, call one of the following tools with`)\n lines.push(`callId=${toolCall.id}:`)\n for (const m of methods) {\n if (m.description) {\n lines.push(`- ${m.name} — ${m.description}`)\n } else {\n lines.push(`- ${m.name}`)\n }\n }\n lines.push(``)\n lines.push(\n `The artifact persists in this turn's context — multiple queries against the same callId are allowed and efficient. Do not assume the body has been inlined anywhere else.`\n )\n return lines.join('\\n')\n}\n\n/**\n * Renders a tool call's result(s) into the OpenAI Chat Completions tool-message body — either a\n * plain string or an array of content blocks when the result carries media — wrapping textual\n * output in the trust envelope appropriate to the tool's trust level.\n */\nexport const renderChatCompletionsToolCallResult = async (input: {\n toolCall: ToolCall\n results: Tokenizable | SpooledArtifact | SpooledArtifact[] | Media | Media[]\n tool: Tool | ArtifactTool | undefined\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n warn?: (msg: string) => void\n}): Promise<string | ChatCompletionsContentBlock[]> => {\n const { toolCall, results, tool, warn, unsupportedMediaPolicy } = input\n const isTrusted =\n tool !== null && tool !== undefined && (tool as { trusted?: boolean }).trusted === true\n\n if (tool === undefined) {\n warn?.(\n `Tool \"${toolCall.tool}\" is not present in the bound tool registry at render time; defaulting to untrusted envelope.`\n )\n }\n\n // Media / Media[] silo — bypasses Tool.trusted (Trust-Is-Content rule). Envelope is sourced\n // from each Media's own trustTier; modality from each Media's modalityHazard.\n const isMediaResult = Media.isMedia(results)\n const isMediaArrayResult =\n Array.isArray(results) && results.length > 0 && results.every((r) => Media.isMedia(r))\n if (isMediaResult || isMediaArrayResult) {\n const mediaList = isMediaResult ? [results as Media] : (results as Media[])\n const blocks: ChatCompletionsContentBlock[] = []\n for (const media of mediaList) {\n const mediaBlocks = await renderMediaToContentBlocks({\n media,\n toolName: toolCall.tool,\n nonce: toolCall.checksum,\n unsupportedMediaPolicy,\n renderTrustedContent: input.renderTrustedContent,\n renderUntrustedContent: input.renderUntrustedContent,\n warn,\n })\n for (const b of mediaBlocks) blocks.push(b)\n }\n return blocks\n }\n\n // SpooledArtifact[] silo — render each artifact through the existing single-artifact path\n // and concatenate the bodies. Trust envelope is decided per-artifact via the surrounding\n // Tool.trusted flag (same as single SpooledArtifact today).\n if (Array.isArray(results)) {\n const parts: string[] = []\n for (const a of results) {\n const body = await (a as SpooledArtifact).asString()\n parts.push(body)\n }\n const joined = parts.join('\\n\\n')\n if (isTrusted) {\n return input.renderTrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(joined, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n }\n\n const isSpooled = looksLikeSpooledArtifact(results)\n\n // Handle-pattern branch: spooled + inline=false → always untrusted (queryable-data, not policy).\n if (isSpooled && toolCall.inline === false) {\n const artifact = results as SpooledArtifact\n let byteLength = 0\n let lineCount = 0\n try {\n byteLength = await artifact.byteLength()\n } catch {\n byteLength = 0\n }\n try {\n lineCount = await artifact.lineCount()\n } catch {\n lineCount = 0\n }\n const body = renderArtifactHandleBody(\n toolCall,\n artifact,\n byteLength,\n lineCount,\n undefined,\n undefined\n )\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'artifact-handle',\n tool: toolCall.tool,\n })\n }\n\n // Inline path: render full body via the appropriate envelope.\n if (!isSpooled && toolCall.inline === false) {\n warn?.(\n `Tool call ${toolCall.id} has inline=false but results is a Tokenizable (not a SpooledArtifact); rendering inline anyway.`\n )\n }\n\n let body: string\n if (isSpooled) {\n body = await (results as SpooledArtifact).asString()\n } else {\n body = (results as Tokenizable).toString()\n }\n\n if (isTrusted) {\n return input.renderTrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'trusted-tool-result',\n tool: toolCall.tool,\n })\n }\n return input.renderUntrustedContent(body, {\n nonce: toolCall.checksum,\n kind: 'tool-result',\n tool: toolCall.tool,\n })\n}\n/** Default tool-call-result renderer; alias of {@link renderChatCompletionsToolCallResult}. */\nexport const defaultRenderChatCompletionsToolCallResult = renderChatCompletionsToolCallResult\n\n// suppress unused; kept for forward-compat with stricter spool guards\nvoid isSpooledArtifactResult\n\n// ─── buildChatCompletionsHistory ──────────────────────────────────────────────\n\ntype TimelineItem =\n | { kind: 'message'; createdAt: number; value: Message }\n | { kind: 'thought'; createdAt: number; value: Thought }\n | { kind: 'toolCall'; createdAt: number; value: ToolCall }\n\n/**\n * Assembles the complete OpenAI Chat Completions message history for a dispatch — system prompt and\n * content buckets, the interleaved timeline of messages/thoughts/tool calls (with synthetic\n * `assistant.tool_calls` and `tool.tool_call_id` shaping), and the collected opaque reasoning\n * payloads — by delegating to the injected sub-renderers.\n */\nexport const buildChatCompletionsHistory = async (input: {\n systemPrompt: Tokenizable\n standingInstructions: Iterable<Tokenizable>\n memories: Iterable<Memory>\n retrievables: Iterable<Retrievable>\n messages: Iterable<Message>\n thoughts: Iterable<Thought>\n toolCalls: Iterable<ToolCall>\n tools: ToolRegistry\n renderedToolCallResults: Map<string, string | ChatCompletionsContentBlock[]>\n bucketOrder: ChatCompletionsBucketOrder\n selfIdentity: string\n thoughtSurfacing: 'all-self' | 'latest-self' | 'all'\n replayCompatibility: ReadonlyArray<string>\n unsupportedMediaPolicy: UnsupportedMediaPolicy\n renderChatCompletionsToolCallResult: ChatCompletionsHelpers['renderChatCompletionsToolCallResult']\n renderChatCompletionsSystemPrompt: ChatHelpersCommon['renderChatCompletionsSystemPrompt']\n renderStandingInstructions: ChatHelpersCommon['renderStandingInstructions']\n renderMemories: ChatHelpersCommon['renderMemories']\n renderRetrievables: ChatHelpersCommon['renderRetrievables']\n renderRetrievableSafetyDirective: ChatHelpersCommon['renderRetrievableSafetyDirective']\n renderFirstPartyRetrievables: ChatHelpersCommon['renderFirstPartyRetrievables']\n renderThirdPartyPublicRetrievables: ChatHelpersCommon['renderThirdPartyPublicRetrievables']\n renderThirdPartyPrivateRetrievables: ChatHelpersCommon['renderThirdPartyPrivateRetrievables']\n renderTimelineMessage: ChatCompletionsHelpers['renderTimelineMessage']\n renderThought: ChatHelpersCommon['renderThought']\n filterThoughts: ChatHelpersCommon['filterThoughts']\n renderUntrustedContent: ChatHelpersCommon['renderUntrustedContent']\n renderTrustedContent: ChatHelpersCommon['renderTrustedContent']\n warn?: (msg: string) => void\n}): Promise<{\n messages: ChatCompletionsMessage[]\n reasoningPayloads: Array<{ id: string; replayCompatibility: string; payload: unknown }>\n}> => {\n const out: ChatCompletionsMessage[] = []\n const reasoningPayloads: Array<{\n id: string\n replayCompatibility: string\n payload: unknown\n }> = []\n\n const buckets = input.bucketOrder\n const timelineIdx = buckets.indexOf('timeline')\n\n // Build leading system content from base prompt + before-timeline buckets.\n const leadingSystem = await input.renderChatCompletionsSystemPrompt({\n systemPrompt: input.systemPrompt,\n standingInstructions: input.standingInstructions,\n memories: input.memories,\n retrievables: input.retrievables,\n bucketOrder: buckets,\n renderStandingInstructions: input.renderStandingInstructions,\n renderMemories: input.renderMemories,\n renderRetrievables: input.renderRetrievables,\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (leadingSystem.length > 0) {\n out.push({ role: 'system', content: leadingSystem })\n }\n\n // Build the timeline (if present in bucketOrder).\n const includesTimeline = timelineIdx !== -1\n if (includesTimeline) {\n // Filter thoughts per surfacing mode + compatibility.\n const survivingThoughts = input.filterThoughts(\n input.thoughts,\n input.thoughtSurfacing,\n input.selfIdentity,\n input.replayCompatibility\n )\n\n // Build sorted timeline items.\n const items: TimelineItem[] = []\n for (const m of input.messages) {\n items.push({ kind: 'message', createdAt: m.createdAt.toMillis(), value: m })\n }\n for (const t of survivingThoughts) {\n items.push({ kind: 'thought', createdAt: t.createdAt.toMillis(), value: t })\n }\n for (const tc of input.toolCalls) {\n items.push({ kind: 'toolCall', createdAt: tc.createdAt.toMillis(), value: tc })\n }\n items.sort((a, b) => a.createdAt - b.createdAt)\n\n const replaySet = new Set<string>([...input.replayCompatibility])\n\n for (const item of items) {\n if (item.kind === 'message') {\n out.push(\n await input.renderTimelineMessage({\n message: item.value,\n selfIdentity: input.selfIdentity,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n )\n } else if (item.kind === 'thought') {\n const t = item.value\n const identifier = String(t.identity?.identifier ?? '')\n const isSelf = identifier === input.selfIdentity\n const hasPayload = t.payload !== undefined\n const compatTag = t.replayCompatibility\n\n if (hasPayload && compatTag && replaySet.has(compatTag)) {\n // Opaque reasoning — side-channel + summary envelope.\n reasoningPayloads.push({\n id: t.id,\n replayCompatibility: compatTag,\n payload: t.payload,\n })\n const envelope = input.renderThought(\n t.content.toString(),\n {\n nonce: t.id,\n kind: 'opaque-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n replayCompatibility: compatTag,\n },\n t.payload\n )\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n } else if (!hasPayload) {\n // Plain-text reasoning (no payload, or tagged plain-text, or tagged but matched).\n const envelope = input.renderThought(t.content.toString(), {\n nonce: t.id,\n kind: isSelf ? 'self-reasoning' : 'peer-reasoning',\n from: identifier,\n createdAt: t.createdAt?.toISO?.() ?? undefined,\n })\n const synthetic: ChatCompletionsMessage = {\n role: 'assistant',\n content: envelope,\n }\n if (!isSelf && identifier.length > 0) {\n synthetic.name = sanitiseNameField(identifier)\n }\n out.push(synthetic)\n }\n // else: opaque, non-matching → elided (NOT removed from ctx.turnThoughts upstream).\n } else {\n // tool call: emit a synthetic assistant message carrying tool_calls[],\n // followed by a tool-role message with the result.\n const tc = item.value\n const assistantMsg: ChatCompletionsMessage = {\n role: 'assistant',\n content: null,\n tool_calls: [\n {\n id: tc.id,\n type: 'function',\n function: {\n name: tc.tool,\n arguments: typeof tc.args === 'string' ? tc.args : JSON.stringify(tc.args ?? {}),\n },\n },\n ],\n }\n out.push(assistantMsg)\n\n let rendered = input.renderedToolCallResults.get(tc.id)\n if (rendered === undefined) {\n const tool = input.tools.get?.(tc.tool)\n rendered = await input.renderChatCompletionsToolCallResult({\n toolCall: tc,\n results: tc.results as\n | Tokenizable\n | SpooledArtifact\n | SpooledArtifact[]\n | Media\n | Media[],\n tool: tool as Tool | ArtifactTool | undefined,\n renderUntrustedContent: input.renderUntrustedContent,\n renderTrustedContent: input.renderTrustedContent,\n unsupportedMediaPolicy: input.unsupportedMediaPolicy,\n warn: input.warn,\n })\n }\n out.push({\n role: 'tool',\n content: rendered,\n tool_call_id: tc.id,\n })\n }\n }\n }\n\n // Trailing system message for after-timeline buckets.\n if (includesTimeline) {\n const trailingParts: string[] = []\n for (let i = timelineIdx + 1; i < buckets.length; i++) {\n const label = buckets[i]!\n if (label === 'standingInstructions') {\n const block = input.renderStandingInstructions(input.standingInstructions)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'memories') {\n const wrapped: Array<{ memory: Memory; attrs: MemoryAttrs }> = []\n for (const m of input.memories) {\n wrapped.push(memoryToAttrs(m))\n }\n const block = input.renderMemories(wrapped)\n if (block.length > 0) trailingParts.push(block)\n } else if (label === 'retrievables') {\n const wrapped: Array<{ retrievable: Retrievable; attrs: RetrievableAttrs }> = []\n for (const r of input.retrievables) {\n wrapped.push(retrievableToAttrs(r))\n }\n const block = await input.renderRetrievables(wrapped, {\n renderRetrievableSafetyDirective: input.renderRetrievableSafetyDirective,\n renderFirstPartyRetrievables: input.renderFirstPartyRetrievables,\n renderThirdPartyPublicRetrievables: input.renderThirdPartyPublicRetrievables,\n renderThirdPartyPrivateRetrievables: input.renderThirdPartyPrivateRetrievables,\n renderUntrustedContent: input.renderUntrustedContent,\n })\n if (block.length > 0) trailingParts.push(block)\n }\n }\n if (trailingParts.length > 0) {\n out.push({ role: 'system', content: trailingParts.join('\\n\\n') })\n }\n }\n\n return { messages: out, reasoningPayloads }\n}\n/** Default history assembler; alias of {@link buildChatCompletionsHistory}. */\nexport const defaultBuildChatCompletionsHistory = buildChatCompletionsHistory\n\n// ─── createChatCompletionsToolCallDeltaAccumulator ────────────────────────────\n\n/**\n * Creates a fresh accumulator that stitches streamed {@link ChatCompletionsToolCallDelta} fragments\n * (keyed by their stream index) into fully-assembled tool calls, drained once the stream completes.\n */\nexport const createChatCompletionsToolCallDeltaAccumulator =\n (): ChatCompletionsToolCallDeltaAccumulator => {\n const byIndex = new Map<\n number,\n { id?: string; type?: 'function'; name: string; args: string }\n >()\n return {\n feed(delta: ChatCompletionsToolCallDelta): void {\n const idx = delta.index\n let entry = byIndex.get(idx)\n if (!entry) {\n entry = { name: '', args: '' }\n byIndex.set(idx, entry)\n }\n if (delta.id !== undefined) entry.id = delta.id\n if (delta.type !== undefined) entry.type = delta.type\n if (delta.function?.name !== undefined) {\n entry.name = entry.name + delta.function.name\n }\n if (delta.function?.arguments !== undefined) {\n entry.args = entry.args + delta.function.arguments\n }\n },\n drain(): AssembledToolCall[] {\n const out: AssembledToolCall[] = []\n const indices = Array.from(byIndex.keys()).sort((a, b) => a - b)\n for (const idx of indices) {\n const e = byIndex.get(idx)!\n out.push({\n id: e.id ?? `call_${idx}`,\n type: e.type ?? 'function',\n name: e.name,\n args: e.args,\n })\n }\n return out\n },\n }\n }\n/** Default delta-accumulator factory; alias of {@link createChatCompletionsToolCallDeltaAccumulator}. */\nexport const defaultCreateChatCompletionsToolCallDeltaAccumulator =\n createChatCompletionsToolCallDeltaAccumulator\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuHA,IAAa,0BACX,KACA,eACuB;CACvB,MAAM,MAA0B,CAAC;CACjC,KAAK,MAAM,SAAS,YAAY;EAC9B,MAAM,QAAQ,MAAM;EACpB,IAAI,OAAO,UAAU,YAAY,MAAM,WAAW,GAAG;EACrD,IAAI,IAAI,MAAM,MAAM,EAAE,YAAY,KAAK,GAAG;EAC1C,IAAI,KAAK;GAAE;GAAO,SAAS;EAAM,CAAC;CACpC;CACA,OAAO;AACT;AAIA,IAAM,8BAAqD;CACzD;CACA;CACA;AACF;AAEA,IAAM,wBAAwB,MAA+D;CAC3F,IAAI,MAAM,SAAS,OAAO;CAC1B,IAAI,MAAM,4BAA4B,OAAO;CAC7C,OAAO;AACT;AAEA,IAAM,4BAA4B,UAAsC;CACtE,IAAI,UAAU,KAAA,KAAa,CAAC,OAAO,SAAS,KAAK,GAAG,OAAO;CAC3D,IAAI,QAAQ,MAAM,OAAO,GAAG,MAAM;CAClC,IAAI,QAAQ,OAAO,MAAM,OAAO,IAAI,QAAQ,MAAM,QAAQ,CAAC,EAAE;CAC7D,IAAI,QAAQ,OAAO,OAAO,MAAM,OAAO,IAAI,SAAS,OAAO,OAAO,QAAQ,CAAC,EAAE;CAC7E,OAAO,IAAI,SAAS,OAAO,OAAO,OAAO,QAAQ,CAAC,EAAE;AACtD;AAEA,IAAM,uBAAuB,SAA4C;CACvE,MAAM,IAAI,KAAK,YAAY;CAC3B,IAAI,EAAE,SAAS,KAAK,KAAK,EAAE,SAAS,OAAO,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