{"version":3,"file":"helpers.mjs","names":[],"sources":["../../../../src/batteries/llm/ollama/helpers.ts"],"sourcesContent":["/**\n * Swappable translation helpers for rendering ADK state into native Ollama `/api/chat` requests.\n *\n * @module @nhtio/adk/batteries/llm/ollama/helpers\n *\n * @remarks\n * The wire-shape-agnostic helpers (`renderUntrustedContent`, `renderMemories`,\n * `renderChatCompletionsSystemPrompt`, `toolsToChatCompletionsTools`, …) are shared with the OpenAI\n * battery via the internal `../chat_common/helpers` submodule and re-exported here under their\n * original names. Only the Ollama-WIRE-SPECIFIC helpers are defined here:\n * `renderOllamaTimelineMessage` (flat `content` + base64 `images[]` + `thinking`),\n * `renderOllamaToolCallResult` (string-only result content), `buildOllamaHistory` (synthetic\n * `assistant.tool_calls` with object-form `arguments` + `tool`-role messages labelled by\n * `tool_name`), and `ollamaToolsFromTools` (alias of the shared tool-definition renderer — native\n * `/api/chat` uses the same function-tool wire shape).\n *\n * Native `/api/chat` supports only base64 `images[]`; every non-image modality routes through the\n * `unsupportedMediaPolicy` fallback (stash text / synthetic description) or throws.\n */\n\nimport { isInstanceOf } from '@nhtio/adk/guards'\nimport { Media, effectiveToolMethods } from '@nhtio/adk/common'\nimport { E_OLLAMA_UNSUPPORTED_MEDIA_MODALITY } from './exceptions'\nimport {\n  escapeXmlAttribute,\n  memoryToAttrs,\n  retrievableToAttrs,\n  renderTrustedContent,\n  renderUntrustedContent,\n  toolsToChatCompletionsTools,\n  neutraliseDeveloperRulesTag,\n  sanitizeMimeType,\n  sanitizeFilenameForDescription,\n  floorTrustTier,\n} from '../chat_common/helpers'\nimport type { ChatHelpersCommon } from '../chat_common/types'\nimport type {\n  OllamaMessage,\n  OllamaTool,\n  OllamaHelpers,\n  MemoryAttrs,\n  RetrievableAttrs,\n  ChatCompletionsBucketOrder,\n  UnsupportedMediaPolicy,\n} from './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'\n\n// ─── Re-exported wire-shape-agnostic helpers (shared submodule) ───────────────\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  renderRetrievableHandleBody,\n  defaultRenderRetrievableHandleBody,\n  renderThought,\n  defaultRenderThought,\n  filterThoughts,\n  defaultFilterThoughts,\n  toolsToChatCompletionsTools,\n  defaultToolsToChatCompletionsTools,\n  renderChatCompletionsSystemPrompt,\n  defaultRenderChatCompletionsSystemPrompt,\n} from '../chat_common/helpers'\n\n// ─── ollamaToolsFromTools (alias — native tool wire == Chat Completions wire) ──\n\n/**\n * Convert ADK tools to the native Ollama `tools[]` wire. Native `/api/chat` uses the identical\n * `{ type: 'function', function: { name, description, parameters } }` shape as Chat Completions, so\n * this is an alias of the shared renderer.\n */\nexport const ollamaToolsFromTools = (\n  tools: ReadonlyArray<Tool | ArtifactTool>,\n  deps: Parameters<typeof toolsToChatCompletionsTools>[1]\n): OllamaTool[] => toolsToChatCompletionsTools(tools, deps)\n/** Default implementation of {@link OllamaHelpers}-style tool translation; alias of {@link ollamaToolsFromTools}. */\nexport const defaultOllamaToolsFromTools = ollamaToolsFromTools\n\n// ─── Media rendering (Ollama native — images only) ────────────────────────────\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 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: ${sanitizeFilenameForDescription(media.filename)}, ${sanitizeMimeType(media.mimeType, media.kind === 'image' || media.kind === 'audio' ? media.kind : undefined)}, ${formatBytesHumanReadable(byteLen)}]`\n\n/**\n * Render a single {@link Media} for the native Ollama wire. Images become a base64 entry pushed to\n * the message's `images[]` array (returned via `image`); every other modality is unsupported and\n * routes through `unsupportedMediaPolicy` to a text envelope (returned via `text`) or throws.\n */\n/**\n * The inline media id-marker (cross-battery convention; see the OpenAI battery's\n * `renderMediaIdMarker`): structural reference text authored by the harness from the\n * harness-controlled `Media.id`, rendered alongside each media so the model can reference it\n * by id in tool calls. Carries no authority; renders outside the untrusted envelope.\n */\nconst renderMediaIdMarker = (media: Media): string =>\n  `[media id: ${media.id} | ${sanitizeFilenameForDescription(media.filename)}]`\n\nconst renderMediaForOllama = 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<{ image?: string; text?: string }> => {\n  const { media, toolName, nonce, unsupportedMediaPolicy, warn } = input\n  const modality = modalityHazardToAttr(media.modalityHazard)\n\n  if (media.kind === 'image') {\n    const b64 = await media.asBase64()\n    return { image: b64 }\n  }\n\n  // Non-image modality — native /api/chat has no representation for it.\n  const fallbackText = async (\n    keys: ReadonlyArray<string>,\n    allowSyntheticFallthrough: boolean\n  ): Promise<{ text: string }> => {\n    const fallback = resolveFallbackStash(media, keys)\n    if (fallback) {\n      return {\n        text: renderTextInEnvelope(fallback.text, {\n          // Floor the stash entry's tier to the parent media's (a stash entry may render less trusted\n          // than its asset, never more) — the committee's #1-ranked media escalation.\n          trustTier: floorTrustTier(media.trustTier, fallback.entryTier),\n          modality,\n          nonce,\n          toolName,\n          renderTrustedContent: input.renderTrustedContent,\n          renderUntrustedContent: input.renderUntrustedContent,\n        }),\n      }\n    }\n    if (!allowSyntheticFallthrough) {\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    return {\n      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    }\n  }\n\n  if (unsupportedMediaPolicy === 'throw') {\n    throw new E_OLLAMA_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 fallbackText(keys, false)\n  }\n  return fallbackText([], true)\n}\n\n// ─── renderOllamaTimelineMessage ──────────────────────────────────────────────\n\n/**\n * Renders a single timeline {@link @nhtio/adk!Message} into a native Ollama message — flattening any\n * media into the base64 `images[]` array, surfacing reasoning as `thinking`, and wrapping textual\n * bodies in the appropriate trust envelope.\n */\nexport const renderOllamaTimelineMessage = async (input: {\n  message: Message\n  selfIdentity: string\n  unsupportedMediaPolicy: UnsupportedMediaPolicy\n  warn?: (msg: string) => void\n}): Promise<OllamaMessage> => {\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  const representation = representationRaw.length > 0 ? representationRaw : identifier\n  // Neutralise a body-embedded no-nonce developer-rules tier (envelope-mimicry defense).\n  const text = neutraliseDeveloperRulesTag(\n    message.content !== undefined ? message.content.toString() : ''\n  )\n  const createdAtStr = message.createdAt.toISO?.() ?? ''\n  const createdAtAttr = createdAtStr ? ` createdAt=\"${escapeXmlAttribute(createdAtStr)}\"` : ''\n  const attachments = message.attachments\n\n  // Build the text envelope (same identity logic as the OpenAI battery — wire-agnostic).\n  let envelopeText: string\n  let role: 'user' | 'assistant'\n  if (message.role === 'user') {\n    role = 'user'\n    if (identifier.length === 0) {\n      envelopeText = text\n    } else {\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      envelopeText = text\n    } else {\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  const images: string[] = []\n  const extraTexts: string[] = []\n  for (const media of attachments) {\n    const rendered = await renderMediaForOllama({\n      media,\n      toolName: undefined,\n      nonce: message.id,\n      unsupportedMediaPolicy,\n      renderTrustedContent,\n      renderUntrustedContent,\n      warn,\n    })\n    extraTexts.push(renderMediaIdMarker(media))\n    if (rendered.image !== undefined) images.push(rendered.image)\n    if (rendered.text !== undefined) extraTexts.push(rendered.text)\n  }\n\n  // Non-image fallbacks (text envelopes) are appended to the message content; images ride in the\n  // separate native `images[]` field.\n  const contentParts: string[] = []\n  if (envelopeText.length > 0) contentParts.push(envelopeText)\n  for (const t of extraTexts) contentParts.push(t)\n  const out: OllamaMessage = { role, content: contentParts.join('\\n') }\n  if (images.length > 0) out.images = images\n  return out\n}\n/** Default timeline-message renderer; alias of {@link renderOllamaTimelineMessage}. */\nexport const defaultRenderOllamaTimelineMessage = renderOllamaTimelineMessage\n\n// ─── renderOllamaToolCallResult ───────────────────────────────────────────────\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): string => {\n  const ctor = (\n    artifact as unknown as {\n      constructor: { toolMethods?: ReadonlyArray<{ name: string; description?: string }> }\n    }\n  ).constructor\n  const methods = effectiveToolMethods(ctor ?? {})\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  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 * Render a tool-call result to native Ollama tool-message content (always a string). Media results\n * are routed through the internal media renderer: images cannot ride on a tool-role message's\n * `content`, so an image result is replaced with a short text marker (the image bytes are not\n * re-sent on a tool message); non-image media use the same fallback-text path as elsewhere.\n */\nexport const renderOllamaToolCallResult = 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> => {\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 silo — bypasses Tool.trusted (Trust-Is-Content rule).\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 parts: string[] = []\n    for (const media of mediaList) {\n      const rendered = await renderMediaForOllama({\n        media,\n        toolName: toolCall.tool,\n        nonce: toolCall.checksum,\n        unsupportedMediaPolicy,\n        renderTrustedContent: input.renderTrustedContent,\n        renderUntrustedContent: input.renderUntrustedContent,\n        warn,\n      })\n      parts.push(renderMediaIdMarker(media))\n      if (rendered.text !== undefined) {\n        parts.push(rendered.text)\n      } else {\n        // An image tool-result cannot be carried on a tool-role message's string content; emit a\n        // text marker in its place so the model knows an image was produced.\n        const byteLen = await media.byteLength()\n        parts.push(\n          input.renderUntrustedContent(renderSyntheticMediaDescription(media, byteLen), {\n            nonce: toolCall.checksum,\n            kind: 'tool-result-image',\n            tool: toolCall.tool,\n            modality: modalityHazardToAttr(media.modalityHazard),\n          })\n        )\n      }\n    }\n    return parts.join('\\n')\n  }\n\n  // SpooledArtifact[] silo.\n  if (Array.isArray(results)) {\n    const parts: string[] = []\n    for (const a of results) {\n      parts.push(await (a as SpooledArtifact).asString())\n    }\n    const joined = parts.join('\\n\\n')\n    return isTrusted\n      ? input.renderTrustedContent(joined, {\n          nonce: toolCall.checksum,\n          kind: 'trusted-tool-result',\n          tool: toolCall.tool,\n        })\n      : 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).\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(toolCall, artifact, byteLength, lineCount)\n    return input.renderUntrustedContent(body, {\n      nonce: toolCall.checksum,\n      kind: 'artifact-handle',\n      tool: toolCall.tool,\n    })\n  }\n\n  // A Tokenizable result (an ArtifactTool query answer, an error string) has no queryable artifact to\n  // hand back, so it always renders inline regardless of the `inline` flag — under handle-by-default\n  // (inline:false) this is the ordinary case, not a misconfiguration, so it is silent.\n  const body = isSpooled\n    ? await (results as SpooledArtifact).asString()\n    : (results as Tokenizable).toString()\n\n  return isTrusted\n    ? input.renderTrustedContent(body, {\n        nonce: toolCall.checksum,\n        kind: 'trusted-tool-result',\n        tool: toolCall.tool,\n      })\n    : 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 renderOllamaToolCallResult}. */\nexport const defaultRenderOllamaToolCallResult = renderOllamaToolCallResult\n\n// ─── buildOllamaHistory ───────────────────────────────────────────────────────\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 native Ollama message history for a dispatch — system prompt and content\n * buckets, the interleaved timeline of messages/thoughts/tool calls, and the collected opaque\n * reasoning payloads — by delegating to the injected sub-renderers.\n */\nexport const buildOllamaHistory = 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  /** Pre-rendered results keyed by the same live ToolCall instances iterated below. */\n  renderedToolCallResults: Map<ToolCall, string>\n  bucketOrder: ChatCompletionsBucketOrder\n  selfIdentity: string\n  thoughtSurfacing: 'all-self' | 'latest-self' | 'all'\n  replayCompatibility: ReadonlyArray<string>\n  unsupportedMediaPolicy: UnsupportedMediaPolicy\n  renderOllamaToolCallResult: OllamaHelpers['renderOllamaToolCallResult']\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  renderRetrievableHandleBody?: ChatHelpersCommon['renderRetrievableHandleBody']\n  renderOllamaTimelineMessage: OllamaHelpers['renderOllamaTimelineMessage']\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: OllamaMessage[]\n  reasoningPayloads: Array<{ id: string; replayCompatibility: string; payload: unknown }>\n}> => {\n  const out: OllamaMessage[] = []\n  const reasoningPayloads: Array<{ id: string; replayCompatibility: string; payload: unknown }> = []\n\n  const buckets = input.bucketOrder\n  const timelineIdx = buckets.indexOf('timeline')\n\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    renderRetrievableHandleBody: input.renderRetrievableHandleBody,\n    renderUntrustedContent: input.renderUntrustedContent,\n  })\n  if (leadingSystem.length > 0) {\n    out.push({ role: 'system', content: leadingSystem })\n  }\n\n  const includesTimeline = timelineIdx !== -1\n  if (includesTimeline) {\n    const survivingThoughts = input.filterThoughts(\n      input.thoughts,\n      input.thoughtSurfacing,\n      input.selfIdentity,\n      input.replayCompatibility\n    )\n\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.renderOllamaTimelineMessage({\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          reasoningPayloads.push({ id: t.id, replayCompatibility: compatTag, payload: t.payload })\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          out.push({ role: 'assistant', content: envelope })\n        } else if (!hasPayload) {\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          out.push({ role: 'assistant', content: envelope })\n        }\n        // else: opaque, non-matching → elided.\n      } else {\n        // tool call: synthetic assistant message carrying tool_calls[] (object-form arguments),\n        // followed by a tool-role message labelled by `tool_name` (NOT tool_call_id).\n        const tc = item.value\n        const args =\n          tc.args !== null && typeof tc.args === 'object' && !Array.isArray(tc.args)\n            ? (tc.args as Record<string, unknown>)\n            : {}\n        out.push({\n          role: 'assistant',\n          content: '',\n          tool_calls: [{ function: { name: tc.tool, arguments: args } }],\n        })\n\n        // Instance identity prevents duplicate ids from pairing a call with another call's result.\n        let rendered = input.renderedToolCallResults.get(tc)\n        if (rendered === undefined) {\n          const tool = input.tools.get?.(tc.tool)\n          rendered = await input.renderOllamaToolCallResult({\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({ role: 'tool', content: rendered, tool_name: tc.tool })\n      }\n    }\n  }\n\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 native-history assembler; alias of {@link buildOllamaHistory}. */\nexport const defaultBuildOllamaHistory = buildOllamaHistory\n\n// suppress unused\nvoid isInstanceOf\n"],"mappings":";;;;;;;;;;;;AAqGA,IAAa,wBACX,OACA,SACiB,4BAA4B,OAAO,IAAI;;AAE1D,IAAa,8BAA8B;AAI3C,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,MA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