[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-tanstack-ai-coremiddleware":3,"mdc--4ja2xc-key":53,"related-repo-tanstack-ai-coremiddleware":11603,"related-org-tanstack-ai-coremiddleware":11704},{"slug":4,"name":5,"fn":6,"description":7,"org":8,"tags":12,"stars":24,"repoUrl":25,"updatedAt":26,"license":27,"forks":28,"topics":29,"repo":48,"sourceUrl":51,"mdContent":52},"ai-coremiddleware","ai-core\u002Fmiddleware","configure chat lifecycle middleware","Chat lifecycle middleware hooks: onConfig, onStart, onChunk, onBeforeToolCall, onAfterToolCall, onUsage, onFinish, onAbort, onError. Use for analytics, event firing, tool caching (toolCacheMiddleware), logging, and tracing. Middleware array in chat() config, left-to-right execution order. NOT onEnd\u002FonFinish callbacks on chat() — use middleware.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},"tanstack","TanStack","https:\u002F\u002Fpexgzepcugksgbtrxkhf.supabase.co\u002Fstorage\u002Fv1\u002Fobject\u002Fpublic\u002Forg-logos\u002Ftanstack.png",[13,17,20,23],{"name":14,"slug":15,"type":16},"Observability","observability","tag",{"name":18,"slug":19,"type":16},"AI SDK","ai-sdk",{"name":21,"slug":22,"type":16},"Middleware","middleware",{"name":10,"slug":9,"type":16},2884,"https:\u002F\u002Fgithub.com\u002FTanStack\u002Fai","2026-07-30T05:26:09.402989",null,269,[30,31,19,32,33,34,35,36,37,38,39,40,41,42,43,9,44,45,46,47],"ai","ai-agents","anthropic","chatbot","function-calling","gemini","generative-ai","llm","multimodal","openai","react","solidjs","streaming","svelte","tool-calling","typescript","typescript-sdk","vue",{"repoUrl":25,"stars":24,"forks":28,"topics":49,"description":50},[30,31,19,32,33,34,35,36,37,38,39,40,41,42,43,9,44,45,46,47],"🤖 Type-safe, provider-agnostic TypeScript AI SDK for streaming chat, tool calling, agents, and multimodal apps across OpenAI, Anthropic, Gemini, React, Vue, Svelte, and Solid.","https:\u002F\u002Fgithub.com\u002FTanStack\u002Fai\u002Ftree\u002FHEAD\u002Fpackages\u002Fai\u002Fskills\u002Fai-core\u002Fmiddleware","---\nname: ai-core\u002Fmiddleware\ndescription: >\n  Chat lifecycle middleware hooks: onConfig, onStart, onChunk,\n  onBeforeToolCall, onAfterToolCall, onUsage, onFinish, onAbort, onError.\n  Use for analytics, event firing, tool caching (toolCacheMiddleware),\n  logging, and tracing. Middleware array in chat() config, left-to-right\n  execution order. NOT onEnd\u002FonFinish callbacks on chat() — use middleware.\ntype: sub-skill\nlibrary: tanstack-ai\nlibrary_version: '0.10.0'\nsources:\n  - 'TanStack\u002Fai:docs\u002Fadvanced\u002Fmiddleware.md'\n  - 'TanStack\u002Fai:docs\u002Fsandbox\u002Fobservability.md'\n  - 'TanStack\u002Fai:docs\u002Fpersistence\u002Foverview.md'\n---\n\n# Middleware\n\n> **Dependency note:** This skill builds on ai-core. Read it first for critical rules.\n\n## Setup — Analytics Tracking Middleware\n\n```typescript\nimport { chat, toServerSentEventsResponse } from '@tanstack\u002Fai'\nimport { openaiText } from '@tanstack\u002Fai-openai'\n\nconst stream = chat({\n  adapter: openaiText('gpt-5.2'),\n  messages,\n  middleware: [\n    {\n      onStart: (ctx) => {\n        console.log('Chat started:', ctx.model)\n      },\n      onFinish: (ctx, info) => {\n        trackAnalytics({ model: ctx.model, tokens: info.usage?.totalTokens })\n      },\n      onError: (ctx, info) => {\n        reportError(info.error)\n      },\n    },\n  ],\n})\n\nreturn toServerSentEventsResponse(stream)\n```\n\n## Hooks Reference\n\nEvery hook receives a `ChatMiddlewareContext` as its first argument, which provides\n`requestId`, `streamId`, `phase`, `iteration`, `chunkIndex`, `model`, `provider`,\n`signal`, `abort()`, `defer()`, and more.\n\n| Hook                       | When                                                                                               | Second Argument                                     |\n| -------------------------- | -------------------------------------------------------------------------------------------------- | --------------------------------------------------- |\n| `onConfig`                 | Once at startup (`init`) + once per iteration (`beforeModel`) + once at structured-output boundary | `ChatMiddlewareConfig` (return partial to merge)    |\n| `onStructuredOutputConfig` | Once at the structured-output boundary (only when `chat({ outputSchema })`)                        | `StructuredOutputMiddlewareConfig` (return partial) |\n| `onStart`                  | Once after initial `onConfig`                                                                      | none                                                |\n| `onIteration`              | Start of each agent loop iteration                                                                 | `IterationInfo`                                     |\n| `onShouldContinue`         | Whether to start another agent-loop iteration (AND with strategy; `false` stops)                   | `AgentLoopState`                                    |\n| `onChunk`                  | Every streamed chunk                                                                               | `StreamChunk` (return void\u002Fchunk\u002Fchunk[]\u002Fnull)      |\n| `onBeforeToolCall`         | Before each tool executes                                                                          | `ToolCallHookContext` (return decision or void)     |\n| `onAfterToolCall`          | After each tool executes                                                                           | `AfterToolCallInfo`                                 |\n| `onToolPhaseComplete`      | After all tool calls in an iteration                                                               | `ToolPhaseCompleteInfo`                             |\n| `onUsage`                  | When `RUN_FINISHED` includes usage data                                                            | `UsageInfo`                                         |\n| `onFinish`                 | Run completed normally                                                                             | `FinishInfo`                                        |\n| `onAbort`                  | Run was aborted                                                                                    | `AbortInfo`                                         |\n| `onError`                  | Unhandled error occurred                                                                           | `ErrorInfo`                                         |\n\nTerminal hooks (`onFinish`, `onAbort`, `onError`) are **mutually exclusive** -- exactly\none fires per `chat()` invocation.\n\n> **Sampling in `onConfig`:** `temperature`, `topP`, and `maxTokens` are **not**\n> first-class fields on `ChatMiddlewareConfig`. To adjust sampling from\n> middleware, return a partial that mutates `config.modelOptions` using the\n> provider's native key (e.g. OpenAI `temperature` \u002F `max_output_tokens`,\n> Anthropic `max_tokens`, Ollama nested `options.num_predict`). Returning a\n> top-level `temperature`\u002F`maxTokens` has no effect.\n\n### Phase values\n\n`ctx.phase` is one of:\n\n| Phase                | When                                                                                                                                                                                                                                           |\n| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `'init'`             | Initial setup (before the first `onConfig` snapshot is built).                                                                                                                                                                                 |\n| `'beforeModel'`      | Right before each agent-loop adapter call (`onConfig` re-fires here).                                                                                                                                                                          |\n| `'modelStream'`      | During model streaming chunks within the agent loop.                                                                                                                                                                                           |\n| `'beforeTools'`      | Before tool execution phase.                                                                                                                                                                                                                   |\n| `'afterTools'`       | After tool execution phase.                                                                                                                                                                                                                    |\n| `'structuredOutput'` | During the final structured-output adapter call (set for all chunks from `adapter.structuredOutputStream` or the synthesized fallback). Triggered only when `chat({ outputSchema })` is invoked; one phase transition per `chat()` invocation. |\n\n**Structured-output lifecycle rules** (when `chat({ outputSchema })` is used):\n\n- `onStructuredOutputConfig` fires **before** `onConfig` at the structured-output boundary.\n- `onConfig` re-fires at the same boundary with `ctx.phase === 'structuredOutput'`, receiving the post-`onStructuredOutputConfig` view of the config (minus `outputSchema`).\n- `onChunk` and `onUsage` fire for every chunk and usage event emitted by the structured-output call, with `ctx.phase === 'structuredOutput'`.\n- `onIteration` does **not** fire for finalization — it is agent-loop-only.\n- `onFinish` fires once at the end of the whole `chat()` invocation, **after** the structured-output finalization completes (not after the agent loop). Terminal-hook exclusivity still holds (one of `onFinish` \u002F `onAbort` \u002F `onError`).\n- **Terminal `info` and structured-output:** `info.usage` \u002F `info.finishReason` \u002F `info.content` reflect the **agent loop's** terminal state, NOT the finalization step. Finalization state is intentionally segregated to keep agent-loop semantics clean. For a tools-less `chat({ outputSchema })` run, `info.usage` is `undefined` and `info.finishReason` is `null` (no agent-loop iteration produced `RUN_FINISHED`). To capture finalization tokens, use `onUsage` — it fires for both agent-loop iterations and the final call. For the structured-output result itself, observe the `structured-output.complete` CUSTOM event in `onChunk`.\n\n## onStructuredOutputConfig\n\nA dedicated config hook that fires **only** at the structured-output boundary\n(when `chat({ outputSchema })` is invoked). Use it to transform the JSON Schema\nsent to the provider (inject `$defs`, strip vendor-incompatible keywords) or to\napply structured-output-specific config changes that should not affect the\nagent-loop adapter calls.\n\n**Signature:**\n\n```ts\nonStructuredOutputConfig?: (\n  ctx: ChatMiddlewareContext,\n  config: StructuredOutputMiddlewareConfig,\n) =>\n  | void\n  | null\n  | Partial\u003CStructuredOutputMiddlewareConfig>\n  | Promise\u003Cvoid | Partial\u003CStructuredOutputMiddlewareConfig>>\n```\n\n**`StructuredOutputMiddlewareConfig` shape:**\n\n```ts\ninterface StructuredOutputMiddlewareConfig extends ChatMiddlewareConfig {\n  outputSchema: JSONSchema \u002F\u002F The JSON Schema being sent to the provider\n}\n```\n\n**Ordering rule:**\n\n- `onStructuredOutputConfig` fires **before** `onConfig` at the structured-output boundary.\n- `onConfig` re-fires at the same boundary with `ctx.phase === 'structuredOutput'`, receiving the post-`onStructuredOutputConfig` view of the config (minus `outputSchema`).\n- Use `onConfig` for general-purpose transforms that apply to every adapter call (agent-loop iterations and the final structured-output call).\n- Use `onStructuredOutputConfig` when you need to transform the JSON Schema or apply structured-output-specific behavior.\n\n## Core Patterns\n\n### Pattern 1: Analytics and Logging Middleware\n\nUse `onStart`, `onFinish`, `onUsage`, and `onError` for comprehensive observability.\nUse `ctx.defer()` for non-blocking async side effects that should not block the stream.\n\n```typescript\nimport {\n  chat,\n  toServerSentEventsResponse,\n  type ChatMiddleware,\n} from '@tanstack\u002Fai'\nimport { openaiText } from '@tanstack\u002Fai-openai'\n\nconst analytics: ChatMiddleware = {\n  name: 'analytics',\n  onStart: (ctx) => {\n    console.log(`[${ctx.requestId}] Chat started — model: ${ctx.model}`)\n  },\n  onUsage: (ctx, usage) => {\n    console.log(`[${ctx.requestId}] Tokens: ${usage.totalTokens}`)\n  },\n  onFinish: (ctx, info) => {\n    ctx.defer(\n      fetch('\u002Fapi\u002Fanalytics', {\n        method: 'POST',\n        body: JSON.stringify({\n          requestId: ctx.requestId,\n          model: ctx.model,\n          duration: info.duration,\n          tokens: info.usage?.totalTokens,\n          finishReason: info.finishReason,\n        }),\n      }),\n    )\n  },\n  onError: (ctx, info) => {\n    ctx.defer(\n      fetch('\u002Fapi\u002Ferrors', {\n        method: 'POST',\n        body: JSON.stringify({\n          requestId: ctx.requestId,\n          error: String(info.error),\n          duration: info.duration,\n        }),\n      }),\n    )\n  },\n}\n\nconst stream = chat({\n  adapter: openaiText('gpt-5.2'),\n  messages,\n  middleware: [analytics],\n})\n\nreturn toServerSentEventsResponse(stream)\n```\n\n### Pattern 2: Tool Interception Middleware\n\nUse `onBeforeToolCall` to validate, gate, or transform tool arguments before execution.\nUse `onAfterToolCall` to log results and timing. The first middleware that returns a\nnon-void decision from `onBeforeToolCall` short-circuits remaining middleware for that call.\n\n```typescript\nimport type { ChatMiddleware } from '@tanstack\u002Fai'\n\nconst toolGuard: ChatMiddleware = {\n  name: 'tool-guard',\n  onBeforeToolCall: (ctx, hookCtx) => {\n    \u002F\u002F Block dangerous tools\n    if (hookCtx.toolName === 'deleteDatabase') {\n      return { type: 'abort', reason: 'Dangerous operation blocked' }\n    }\n\n    \u002F\u002F Enforce default arguments\n    if (hookCtx.toolName === 'search' && !hookCtx.args.limit) {\n      return {\n        type: 'transformArgs',\n        args: { ...hookCtx.args, limit: 10 },\n      }\n    }\n\n    \u002F\u002F Return void to continue normally\n  },\n  onAfterToolCall: (ctx, info) => {\n    if (info.ok) {\n      console.log(`${info.toolName} completed in ${info.duration}ms`)\n    } else {\n      console.error(`${info.toolName} failed:`, info.error)\n    }\n  },\n}\n```\n\n**`onBeforeToolCall` decision types:**\n\n| Decision                          | Effect                                                              |\n| --------------------------------- | ------------------------------------------------------------------- |\n| `void` \u002F `undefined`              | Continue normally, next middleware decides                          |\n| `{ type: 'transformArgs', args }` | Replace tool arguments before execution                             |\n| `{ type: 'skip', result }`        | Skip execution, use provided result (used by `toolCacheMiddleware`) |\n| `{ type: 'abort', reason? }`      | Abort the entire chat run                                           |\n\n### Pattern 3: Structured-Output Middleware\n\nWhen `chat({ outputSchema })` is used, the final structured-output adapter call\nnow flows through the same middleware chain as the agent loop (with\n`ctx.phase === 'structuredOutput'`). Before this change, the final call bypassed\nmiddleware entirely — `onChunk`, `onUsage`, `onConfig`, and terminal hooks did\nnot see it.\n\n**Example A — Observability (tracing every chunk, including finalization):**\n\n```typescript\nimport type { ChatMiddleware } from '@tanstack\u002Fai'\n\nconst tracing: ChatMiddleware = {\n  name: 'tracing',\n  onChunk(ctx, chunk) {\n    span.addEvent('chunk', { phase: ctx.phase, type: chunk.type })\n  },\n}\n```\n\nThis middleware now observes every chunk from the final structured-output call,\nattributed to `ctx.phase === 'structuredOutput'`. Before the fix, the final\nadapter call bypassed middleware entirely — `tracing` would only see agent-loop\nchunks.\n\n**Example B — Schema rewriting (inject shared `$defs`):**\n\n```typescript\nimport type { ChatMiddleware } from '@tanstack\u002Fai'\n\nconst injectDefs: ChatMiddleware = {\n  name: 'inject-defs',\n  onStructuredOutputConfig(_ctx, config) {\n    return {\n      outputSchema: { ...config.outputSchema, $defs: { ...sharedDefs } },\n    }\n  },\n}\n```\n\n`onStructuredOutputConfig` is the right hook here because it has direct access\nto `config.outputSchema` and runs only on the structured-output boundary —\nschema rewrites do not leak into the agent-loop adapter calls.\n\n### Pattern 4: Multiple Middleware Composition\n\nMiddleware executes in array order (left-to-right). Ordering matters for hooks that\npipe or short-circuit:\n\n```typescript\nimport { chat, type ChatMiddleware } from '@tanstack\u002Fai'\nimport { toolCacheMiddleware } from '@tanstack\u002Fai\u002Fmiddlewares'\nimport { openaiText } from '@tanstack\u002Fai-openai'\n\nconst logging: ChatMiddleware = {\n  name: 'logging',\n  onStart: (ctx) => console.log(`[${ctx.requestId}] started`),\n  onChunk: (ctx, chunk) => {\n    console.log(`[${ctx.requestId}] chunk: ${chunk.type}`)\n  },\n  onFinish: (ctx, info) => {\n    console.log(`[${ctx.requestId}] done in ${info.duration}ms`)\n  },\n}\n\nconst configTransform: ChatMiddleware = {\n  name: 'config-transform',\n  onConfig: (ctx, config) => {\n    if (ctx.phase === 'init') {\n      return {\n        systemPrompts: [...config.systemPrompts, 'Always respond in JSON.'],\n        \u002F\u002F Sampling options are NOT first-class config fields — mutate them\n        \u002F\u002F through `config.modelOptions` using the provider's native key.\n        \u002F\u002F (e.g. OpenAI `temperature` \u002F `max_output_tokens`.)\n        modelOptions: { ...config.modelOptions, temperature: 0.2 },\n      }\n    }\n  },\n}\n\nconst stream = chat({\n  adapter: openaiText('gpt-5.2'),\n  messages,\n  tools: [weatherTool, stockTool],\n  middleware: [\n    logging, \u002F\u002F Runs first\n    configTransform, \u002F\u002F Transforms config second\n    toolCacheMiddleware({ ttl: 60_000 }), \u002F\u002F Caches tool results third\n  ],\n})\n```\n\n**Composition rules by hook:**\n\n| Hook                       | Composition                                   | Effect of Order                            |\n| -------------------------- | --------------------------------------------- | ------------------------------------------ |\n| `onConfig`                 | **Piped** -- each receives previous output    | Earlier middleware transforms first        |\n| `onStructuredOutputConfig` | **Piped** -- each receives previous output    | Earlier middleware transforms first        |\n| `onStart`                  | Sequential                                    | All run in order                           |\n| `onChunk`                  | **Piped** -- chunks flow through each         | If first drops a chunk, later never see it |\n| `onBeforeToolCall`         | **First-win** -- first non-void decision wins | Earlier middleware has priority            |\n| `onAfterToolCall`          | Sequential                                    | All run in order                           |\n| `onUsage`                  | Sequential                                    | All run in order                           |\n| `onFinish\u002FonAbort\u002FonError` | Sequential                                    | All run in order                           |\n\n## Pattern: tool-call budget (app-owned)\n\nNot a built-in. Cap fan-out with `onBeforeToolCall` skip + `onShouldContinue`.\nSee `docs\u002Fchat\u002Fagentic-cycle.md` (\"Tool-call budgets\").\n\n```typescript\nimport { chat, maxIterations, type ChatMiddleware } from '@tanstack\u002Fai'\n\nfunction toolCallBudget(opts: {\n  max?: number\n  maxPerTurn?: number\n}): ChatMiddleware {\n  let perTurn = 0\n  return {\n    onIteration: () => {\n      perTurn = 0\n    },\n    onToolPhaseComplete: () => {\n      perTurn = 0\n    },\n    onBeforeToolCall: () => {\n      if (opts.maxPerTurn == null) return undefined\n      if (++perTurn > opts.maxPerTurn) {\n        return {\n          type: 'skip',\n          result: {\n            error: `Skipped: exceeded maxToolCallsPerTurn (${opts.maxPerTurn})`,\n          },\n        }\n      }\n      return undefined\n    },\n    onShouldContinue: (_ctx, state) =>\n      opts.max != null && state.toolCallCount >= opts.max ? false : undefined,\n  }\n}\n\nchat({\n  adapter,\n  messages,\n  tools: [weatherTool],\n  agentLoopStrategy: maxIterations(20),\n  middleware: [toolCallBudget({ maxPerTurn: 10, max: 20 })],\n})\n```\n\n## Built-in: toolCacheMiddleware\n\nCaches tool call results by name + arguments. Import from `@tanstack\u002Fai\u002Fmiddlewares`:\n\n```typescript\nimport { chat } from '@tanstack\u002Fai'\nimport { toolCacheMiddleware } from '@tanstack\u002Fai\u002Fmiddlewares'\n\nconst stream = chat({\n  adapter,\n  messages,\n  tools: [weatherTool],\n  middleware: [\n    toolCacheMiddleware({\n      ttl: 60_000, \u002F\u002F Cache entries expire after 60 seconds\n      maxSize: 50, \u002F\u002F Max 50 entries (LRU eviction)\n      toolNames: ['getWeather'], \u002F\u002F Only cache specific tools\n    }),\n  ],\n})\n```\n\nOptions: `maxSize` (default 100), `ttl` (default Infinity), `toolNames` (default all),\n`keyFn` (custom cache key), `storage` (custom backend like Redis). See\n`docs\u002Fadvanced\u002Fmiddleware.md` for custom storage examples.\n\n## Server State Persistence: withPersistence\n\n`withPersistence(persistence)` (from `@tanstack\u002Fai-persistence`) is a\n`ChatMiddleware` that persists **state** for `chat()` — thread messages, run\nrecords (status\u002Ftiming\u002Fusage\u002Ferrors), and interrupt state — to a backend store.\nAdd it to the `middleware` array like any other middleware. It never mutates the\nchunk stream; replaying a dropped\u002Freloaded _stream_ is a separate transport-layer\nconcern (see ai-core\u002Fchat-experience\u002FSKILL.md resumability, not this middleware).\n\n```typescript\nimport {\n  chat,\n  chatParamsFromRequest,\n  toServerSentEventsResponse,\n} from '@tanstack\u002Fai'\nimport { openaiText } from '@tanstack\u002Fai-openai'\nimport { withPersistence, memoryPersistence } from '@tanstack\u002Fai-persistence'\n\n\u002F\u002F memoryPersistence() is the in-process reference backend (dev\u002Ftests). For a\n\u002F\u002F durable one, implement the store contracts against your database — see the\n\u002F\u002F @tanstack\u002Fai-persistence skills.\nconst persistence = memoryPersistence()\n\nexport async function POST(request: Request) {\n  const params = await chatParamsFromRequest(request)\n\n  const stream = chat({\n    adapter: openaiText('gpt-5.5'),\n    messages: params.messages,\n    threadId: params.threadId,\n    runId: params.runId,\n    ...(params.resume ? { resume: params.resume } : {}),\n    middleware: [withPersistence(persistence)],\n  })\n\n  return toServerSentEventsResponse(stream)\n}\n```\n\n### Authoritative-history contract\n\nThe middleware treats each request's `messages` as the source of truth for the\nthread:\n\n- **Non-empty `messages`** → on a successful finish (and at an interrupt\n  boundary) the middleware **overwrites** the entire stored thread with that\n  array. Post the **complete** transcript, never just the newest message(s) — a\n  delta would replace and destroy the stored history.\n- **Empty `messages`** → the middleware **loads** the stored thread and runs the\n  turn from the server's copy. This is how you continue a conversation without\n  resending history from the client.\n\n### Backends\n\n`@tanstack\u002Fai-persistence` ships **contracts, not a database backend**. It\nprovides the four store interfaces (`messages`, `runs`, `interrupts`,\n`metadata`), the middleware that drives them, `memoryPersistence()` for\ndev\u002Ftests, and a conformance testkit. For anything durable you implement the\nstores against your own database and pass the result to `withPersistence`.\n\nAnnotate your factory with a named shape (`ChatPersistence` \u002F\n`ChatTranscriptPersistence`) — bare `AIPersistence` is the all-optional bag and\n`withPersistence` rejects it.\n\nLocks are separate from state and are **not** a `stores` key: wire a\n`LockStore` with `withLocks(lockStore)`.\n\n**Full guidance lives in the package's own skills** — start at\n`node_modules\u002F@tanstack\u002Fai-persistence\u002Fskills\u002Fai-persistence\u002FSKILL.md`,\nwhich routes to the server, client, stores, locks, and adapter-recipe\n(Drizzle \u002F Prisma \u002F Cloudflare) sub-skills.\n\n### Resume reconstruction is the middleware's job (server-authoritative path)\n\nWhen a thread has pending interrupts, the middleware **records** them and\n**gates** new input: a request that carries pending interrupts must include a\n`resume` batch that references them, or `onConfig` throws. On a valid resume\nbatch the middleware also **builds `ChatResumeToolState`** (approvals \u002F\nclient-tool results) and **clears `config.resume`** so the chat engine skips\nits ephemeral reconstruction — that path needs client message history the\npersistence flow deliberately omits when the server owns the transcript.\nResumes accepted in `onConfig` are committed (marked resolved\u002Fcancelled) only\nonce the run reaches a successful boundary, so a provider failure between\naccepting a resume and finishing leaves the interrupt pending and a retry with\nthe same resume succeeds.\n\n> A companion `withGenerationPersistence(persistence)` tracks run records for\n> non-chat generation activities (image, audio, TTS, video, transcription).\n\nSource: docs\u002Fpersistence\u002Foverview.md\n\n## Sandbox File-Event Hooks (`sandbox` group)\n\nDeclare a `sandbox: ChatSandboxHooks` group on `defineChatMiddleware` to react\nto every file created\u002Fchanged\u002Fdeleted inside a sandbox provided by\n`withSandbox` (from `@tanstack\u002Fai-sandbox`). These fire **per-run**,\nserver-side, and each handler receives the run's `ChatMiddlewareContext` as\nthe first argument:\n\n```typescript\nimport { defineChatMiddleware } from '@tanstack\u002Fai'\nimport { db } from '.\u002Fdb'\n\nconst auditMiddleware = defineChatMiddleware({\n  name: 'audit',\n  sandbox: {\n    onFile: (ctx, e) => console.log(ctx.runId, e.type, e.path),\n    onFileCreate: (ctx, e) => db.log({ run: ctx.runId, event: e }),\n  },\n})\n```\n\n| Hook           | Fires for                  |\n| -------------- | -------------------------- |\n| `onFile`       | Every create\u002Fchange\u002Fdelete |\n| `onFileCreate` | File creates only          |\n| `onFileChange` | File changes only          |\n| `onFileDelete` | File deletes only          |\n\nThese are independent of the stream: the engine also emits a `sandbox.file`\n`CUSTOM` chunk per change regardless of whether any `sandbox` hooks are\nregistered, so a client can react to the same edits without middleware. See\n`ai-core\u002Fag-ui-protocol\u002FSKILL.md` for reading that chunk (and the opt-in\n`sandbox.file.diff` chunk) off `ChatStream`.\n\n### `before()` \u002F `after()` \u002F `diff()` — lazy, git-backed content accessors\n\nEach hook receives a `SandboxFileHookEvent`: the serializable\n`{ type, path, timestamp }` plus three lazy accessors for the file's content:\n\n```ts\ninterface SandboxFileHookEvent {\n  type: 'create' | 'change' | 'delete'\n  path: string\n  timestamp: number\n  before(): Promise\u003Cstring> \u002F\u002F content at the session baseline ('' if new \u002F non-git)\n  after(): Promise\u003Cstring> \u002F\u002F current content ('' if deleted)\n  diff(): Promise\u003Cstring> \u002F\u002F unified patch vs the baseline\n}\n```\n\n```typescript\nimport { defineChatMiddleware } from '@tanstack\u002Fai'\nimport { db } from '.\u002Fdb'\n\nconst auditMiddleware = defineChatMiddleware({\n  name: 'audit',\n  sandbox: {\n    onFileChange: async (ctx, e) => {\n      const [before, after] = await Promise.all([e.before(), e.after()])\n      db.log({ run: ctx.runId, path: e.path, before, after })\n    },\n  },\n})\n```\n\n**Lazy — path-only hooks pay nothing.** `before()`, `after()`, and `diff()`\nare methods, not fields: each only reads the file or shells out to `git` when\ncalled. A hook that only reads `e.path`\u002F`e.type` (like the `onFile` logger\nabove) never touches the filesystem or spawns a process.\n\n**Git session baseline.** The sandbox snapshots `git rev-parse HEAD` once at\nsetup as the session baseline (empty string if the workspace isn't a git repo\nor has no commits). `before()` and `diff()` always diff against that same\nfixed baseline for the rest of the run, so `onFileChange` reports the file's\n**cumulative** change since the run started, not just the delta since the\nlast poll. `after()` always reads current on-disk content. None of the three\naccessors throw: a deleted file resolves `after()` to `''` (it still has\n`before()`); a new file resolves `before()` to `''` (it still has `after()`);\na non-git workspace resolves **both** `before()` and `after()` to `''` and\nmakes `diff()` fall back to a synthesized add-patch built from `after()` —\nexcept for a `delete` event in a non-git workspace, where there's nothing to\nsynthesize and `diff()` resolves to `''`. In a git workspace a file git\n**isn't tracking yet** (a file the agent created, and every later edit to it)\ndiffs empty because `git diff` ignores untracked files, so `diff()` falls\nback to the same synthesized add-patch whenever the file is absent at the\nbaseline — a create-or-edit of an untracked file never streams an empty diff.\nAn empty diff for a **tracked** file (identical to the baseline) stays empty,\nas it should. A **git-ignored** file is withheld: the file event still fires\n(you're notified it changed) but `diff()` returns `''`, so a secret like a\n`.env` never has its contents surfaced in the diff feed.\n\n**Failures are logged, not silent.** Every git\u002Fexec\u002Ffs failure behind these\naccessors (and behind the `find`-poll watcher) still falls back to `''`\u002Fan\nempty snapshot, but logs first: real anomalies (a failed `git diff`, an\nunreadable file, a lost `find` poll) under the `errors` category (on by\ndefault); expected-empty conditions (a new file's `before()`, a non-git\nbaseline) under the `sandbox` debug category.\n\n**Hook errors are swallowed per hook.** A throwing `sandbox` hook is caught\nand logged under the `errors` category (on by default) — it cannot break the\nrun or stop other hooks (or the `sandbox.file` chunk) from continuing.\n\nSource: docs\u002Fsandbox\u002Fobservability.md\n\n## Common Mistakes\n\n### a. MEDIUM: Trying to modify StreamChunks in middleware\n\n```typescript\n\u002F\u002F WRONG -- mutating the chunk object directly\nconst broken: ChatMiddleware = {\n  name: 'broken',\n  onChunk: (ctx, chunk) => {\n    chunk.delta = 'modified' \u002F\u002F Mutation does nothing; chunk is not modified in-place\n  },\n}\n\n\u002F\u002F CORRECT -- return a new chunk to replace the original\nconst correct: ChatMiddleware = {\n  name: 'correct',\n  onChunk: (ctx, chunk) => {\n    if (chunk.type === 'TEXT_MESSAGE_CONTENT') {\n      return { ...chunk, delta: chunk.delta.replace(\u002Fsecret\u002Fg, '[REDACTED]') }\n    }\n    \u002F\u002F Return void to pass through unchanged\n  },\n}\n```\n\nMiddleware `onChunk` hooks are functional transforms. Return a new chunk, an array\nof chunks, null (to drop), or void (to pass through). Mutating the input object\nhas no effect on the stream output.\n\nSource: docs\u002Fadvanced\u002Fmiddleware.md\n\n### b. MEDIUM: Middleware exceptions breaking the stream\n\n```typescript\n\u002F\u002F WRONG -- unhandled error kills the entire streaming response\nconst fragile: ChatMiddleware = {\n  name: 'fragile-analytics',\n  onFinish: async (ctx, info) => {\n    \u002F\u002F If this fetch fails, the stream breaks\n    await fetch('\u002Fapi\u002Fanalytics', {\n      method: 'POST',\n      body: JSON.stringify({ duration: info.duration }),\n    })\n  },\n}\n\n\u002F\u002F CORRECT -- wrap in try-catch and\u002For use ctx.defer()\nconst resilient: ChatMiddleware = {\n  name: 'resilient-analytics',\n  onFinish: (ctx, info) => {\n    \u002F\u002F Option 1: defer (non-blocking, errors are isolated)\n    ctx.defer(\n      fetch('\u002Fapi\u002Fanalytics', {\n        method: 'POST',\n        body: JSON.stringify({ duration: info.duration }),\n      }),\n    )\n  },\n  onChunk: (ctx, chunk) => {\n    \u002F\u002F Option 2: try-catch for synchronous\u002Fcritical hooks\n    try {\n      logChunk(chunk)\n    } catch (err) {\n      console.error('Logging failed:', err)\n    }\n    \u002F\u002F Return void to pass through\n  },\n}\n```\n\nWrap all middleware hooks in try-catch to prevent analytics or logging failures\nfrom killing the chat stream. For async side effects, prefer `ctx.defer()` which\nruns after the terminal hook and isolates failures.\n\nSource: docs\u002Fadvanced\u002Fmiddleware.md\n\n## Cross-References\n\n- See also: **ai-core\u002Fchat-experience\u002FSKILL.md** -- Middleware hooks into the chat lifecycle\n- See also: **ai-core\u002Fstructured-outputs\u002FSKILL.md** -- Middleware now wraps the final structured-output call; use `onStructuredOutputConfig` for JSON-Schema transforms\n- See also: **ai-core\u002Fag-ui-protocol\u002FSKILL.md** -- Reading the `sandbox.file` \u002F `sandbox.file.diff` `CUSTOM` chunks the sandbox runtime emits alongside these `sandbox` hooks, via `ChatStream`'s typed `KnownCustomEvent` narrowing\n- See also: **`@tanstack\u002Fai-persistence` skills** (`skills\u002Fai-persistence\u002FSKILL.md` in that package) -- Full persistence suite (`withPersistence`, client storage, store contracts, adapter recipes, locks). This file only sketches server `withPersistence`.\n",{"data":54,"body":62},{"name":5,"description":7,"type":55,"library":56,"library_version":57,"sources":58},"sub-skill","tanstack-ai","0.10.0",[59,60,61],"TanStack\u002Fai:docs\u002Fadvanced\u002Fmiddleware.md","TanStack\u002Fai:docs\u002Fsandbox\u002Fobservability.md","TanStack\u002Fai:docs\u002Fpersistence\u002Foverview.md",{"type":63,"children":64},"root",[65,72,88,95,716,722,806,1230,1269,1378,1385,1396,1554,1571,1821,1826,1853,1861,2015,2028,2093,2101,2174,2180,2186,2223,3465,3471,3496,4265,4278,4381,4387,4424,4432,4684,4703,4718,4961,4979,4985,4990,6088,6096,6301,6307,6334,7197,7203,7214,7526,7579,7585,7641,8273,8279,8291,8345,8351,8410,8446,8480,8498,8504,8570,8586,8591,8605,8655,9038,9127,9176,9202,9223,9446,9857,9915,10127,10188,10219,10224,10230,10236,10686,10698,10703,10709,11455,11467,11471,11477,11597],{"type":66,"tag":67,"props":68,"children":69},"element","h1",{"id":22},[70],{"type":71,"value":21},"text",{"type":66,"tag":73,"props":74,"children":75},"blockquote",{},[76],{"type":66,"tag":77,"props":78,"children":79},"p",{},[80,86],{"type":66,"tag":81,"props":82,"children":83},"strong",{},[84],{"type":71,"value":85},"Dependency note:",{"type":71,"value":87}," This skill builds on ai-core. Read it first for critical rules.",{"type":66,"tag":89,"props":90,"children":92},"h2",{"id":91},"setup-analytics-tracking-middleware",[93],{"type":71,"value":94},"Setup — Analytics Tracking Middleware",{"type":66,"tag":96,"props":97,"children":101},"pre",{"className":98,"code":99,"language":45,"meta":100,"style":100},"language-typescript shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","import { chat, toServerSentEventsResponse } from '@tanstack\u002Fai'\nimport { openaiText } from '@tanstack\u002Fai-openai'\n\nconst stream = chat({\n  adapter: openaiText('gpt-5.2'),\n  messages,\n  middleware: [\n    {\n      onStart: (ctx) => {\n        console.log('Chat started:', ctx.model)\n      },\n      onFinish: (ctx, info) => {\n        trackAnalytics({ model: ctx.model, tokens: info.usage?.totalTokens })\n      },\n      onError: (ctx, info) => {\n        reportError(info.error)\n      },\n    },\n  ],\n})\n\nreturn toServerSentEventsResponse(stream)\n","",[102],{"type":66,"tag":103,"props":104,"children":105},"code",{"__ignoreMap":100},[106,166,204,214,249,296,309,327,336,374,433,442,484,567,575,616,647,655,664,677,690,698],{"type":66,"tag":107,"props":108,"children":111},"span",{"class":109,"line":110},"line",1,[112,118,124,130,135,140,145,150,155,161],{"type":66,"tag":107,"props":113,"children":115},{"style":114},"--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic",[116],{"type":71,"value":117},"import",{"type":66,"tag":107,"props":119,"children":121},{"style":120},"--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF",[122],{"type":71,"value":123}," {",{"type":66,"tag":107,"props":125,"children":127},{"style":126},"--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8",[128],{"type":71,"value":129}," chat",{"type":66,"tag":107,"props":131,"children":132},{"style":120},[133],{"type":71,"value":134},",",{"type":66,"tag":107,"props":136,"children":137},{"style":126},[138],{"type":71,"value":139}," toServerSentEventsResponse",{"type":66,"tag":107,"props":141,"children":142},{"style":120},[143],{"type":71,"value":144}," }",{"type":66,"tag":107,"props":146,"children":147},{"style":114},[148],{"type":71,"value":149}," from",{"type":66,"tag":107,"props":151,"children":152},{"style":120},[153],{"type":71,"value":154}," '",{"type":66,"tag":107,"props":156,"children":158},{"style":157},"--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D",[159],{"type":71,"value":160},"@tanstack\u002Fai",{"type":66,"tag":107,"props":162,"children":163},{"style":120},[164],{"type":71,"value":165},"'\n",{"type":66,"tag":107,"props":167,"children":169},{"class":109,"line":168},2,[170,174,178,183,187,191,195,200],{"type":66,"tag":107,"props":171,"children":172},{"style":114},[173],{"type":71,"value":117},{"type":66,"tag":107,"props":175,"children":176},{"style":120},[177],{"type":71,"value":123},{"type":66,"tag":107,"props":179,"children":180},{"style":126},[181],{"type":71,"value":182}," openaiText",{"type":66,"tag":107,"props":184,"children":185},{"style":120},[186],{"type":71,"value":144},{"type":66,"tag":107,"props":188,"children":189},{"style":114},[190],{"type":71,"value":149},{"type":66,"tag":107,"props":192,"children":193},{"style":120},[194],{"type":71,"value":154},{"type":66,"tag":107,"props":196,"children":197},{"style":157},[198],{"type":71,"value":199},"@tanstack\u002Fai-openai",{"type":66,"tag":107,"props":201,"children":202},{"style":120},[203],{"type":71,"value":165},{"type":66,"tag":107,"props":205,"children":207},{"class":109,"line":206},3,[208],{"type":66,"tag":107,"props":209,"children":211},{"emptyLinePlaceholder":210},true,[212],{"type":71,"value":213},"\n",{"type":66,"tag":107,"props":215,"children":217},{"class":109,"line":216},4,[218,224,229,234,239,244],{"type":66,"tag":107,"props":219,"children":221},{"style":220},"--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA",[222],{"type":71,"value":223},"const",{"type":66,"tag":107,"props":225,"children":226},{"style":126},[227],{"type":71,"value":228}," stream ",{"type":66,"tag":107,"props":230,"children":231},{"style":120},[232],{"type":71,"value":233},"=",{"type":66,"tag":107,"props":235,"children":237},{"style":236},"--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF",[238],{"type":71,"value":129},{"type":66,"tag":107,"props":240,"children":241},{"style":126},[242],{"type":71,"value":243},"(",{"type":66,"tag":107,"props":245,"children":246},{"style":120},[247],{"type":71,"value":248},"{\n",{"type":66,"tag":107,"props":250,"children":252},{"class":109,"line":251},5,[253,259,264,268,272,277,282,286,291],{"type":66,"tag":107,"props":254,"children":256},{"style":255},"--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178",[257],{"type":71,"value":258},"  adapter",{"type":66,"tag":107,"props":260,"children":261},{"style":120},[262],{"type":71,"value":263},":",{"type":66,"tag":107,"props":265,"children":266},{"style":236},[267],{"type":71,"value":182},{"type":66,"tag":107,"props":269,"children":270},{"style":126},[271],{"type":71,"value":243},{"type":66,"tag":107,"props":273,"children":274},{"style":120},[275],{"type":71,"value":276},"'",{"type":66,"tag":107,"props":278,"children":279},{"style":157},[280],{"type":71,"value":281},"gpt-5.2",{"type":66,"tag":107,"props":283,"children":284},{"style":120},[285],{"type":71,"value":276},{"type":66,"tag":107,"props":287,"children":288},{"style":126},[289],{"type":71,"value":290},")",{"type":66,"tag":107,"props":292,"children":293},{"style":120},[294],{"type":71,"value":295},",\n",{"type":66,"tag":107,"props":297,"children":299},{"class":109,"line":298},6,[300,305],{"type":66,"tag":107,"props":301,"children":302},{"style":126},[303],{"type":71,"value":304},"  messages",{"type":66,"tag":107,"props":306,"children":307},{"style":120},[308],{"type":71,"value":295},{"type":66,"tag":107,"props":310,"children":312},{"class":109,"line":311},7,[313,318,322],{"type":66,"tag":107,"props":314,"children":315},{"style":255},[316],{"type":71,"value":317},"  middleware",{"type":66,"tag":107,"props":319,"children":320},{"style":120},[321],{"type":71,"value":263},{"type":66,"tag":107,"props":323,"children":324},{"style":126},[325],{"type":71,"value":326}," [\n",{"type":66,"tag":107,"props":328,"children":330},{"class":109,"line":329},8,[331],{"type":66,"tag":107,"props":332,"children":333},{"style":120},[334],{"type":71,"value":335},"    {\n",{"type":66,"tag":107,"props":337,"children":339},{"class":109,"line":338},9,[340,345,349,354,360,364,369],{"type":66,"tag":107,"props":341,"children":342},{"style":236},[343],{"type":71,"value":344},"      onStart",{"type":66,"tag":107,"props":346,"children":347},{"style":120},[348],{"type":71,"value":263},{"type":66,"tag":107,"props":350,"children":351},{"style":120},[352],{"type":71,"value":353}," (",{"type":66,"tag":107,"props":355,"children":357},{"style":356},"--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic",[358],{"type":71,"value":359},"ctx",{"type":66,"tag":107,"props":361,"children":362},{"style":120},[363],{"type":71,"value":290},{"type":66,"tag":107,"props":365,"children":366},{"style":220},[367],{"type":71,"value":368}," =>",{"type":66,"tag":107,"props":370,"children":371},{"style":120},[372],{"type":71,"value":373}," {\n",{"type":66,"tag":107,"props":375,"children":377},{"class":109,"line":376},10,[378,383,388,393,397,401,406,410,414,419,423,428],{"type":66,"tag":107,"props":379,"children":380},{"style":126},[381],{"type":71,"value":382},"        console",{"type":66,"tag":107,"props":384,"children":385},{"style":120},[386],{"type":71,"value":387},".",{"type":66,"tag":107,"props":389,"children":390},{"style":236},[391],{"type":71,"value":392},"log",{"type":66,"tag":107,"props":394,"children":395},{"style":255},[396],{"type":71,"value":243},{"type":66,"tag":107,"props":398,"children":399},{"style":120},[400],{"type":71,"value":276},{"type":66,"tag":107,"props":402,"children":403},{"style":157},[404],{"type":71,"value":405},"Chat started:",{"type":66,"tag":107,"props":407,"children":408},{"style":120},[409],{"type":71,"value":276},{"type":66,"tag":107,"props":411,"children":412},{"style":120},[413],{"type":71,"value":134},{"type":66,"tag":107,"props":415,"children":416},{"style":126},[417],{"type":71,"value":418}," ctx",{"type":66,"tag":107,"props":420,"children":421},{"style":120},[422],{"type":71,"value":387},{"type":66,"tag":107,"props":424,"children":425},{"style":126},[426],{"type":71,"value":427},"model",{"type":66,"tag":107,"props":429,"children":430},{"style":255},[431],{"type":71,"value":432},")\n",{"type":66,"tag":107,"props":434,"children":436},{"class":109,"line":435},11,[437],{"type":66,"tag":107,"props":438,"children":439},{"style":120},[440],{"type":71,"value":441},"      },\n",{"type":66,"tag":107,"props":443,"children":445},{"class":109,"line":444},12,[446,451,455,459,463,467,472,476,480],{"type":66,"tag":107,"props":447,"children":448},{"style":236},[449],{"type":71,"value":450},"      onFinish",{"type":66,"tag":107,"props":452,"children":453},{"style":120},[454],{"type":71,"value":263},{"type":66,"tag":107,"props":456,"children":457},{"style":120},[458],{"type":71,"value":353},{"type":66,"tag":107,"props":460,"children":461},{"style":356},[462],{"type":71,"value":359},{"type":66,"tag":107,"props":464,"children":465},{"style":120},[466],{"type":71,"value":134},{"type":66,"tag":107,"props":468,"children":469},{"style":356},[470],{"type":71,"value":471}," info",{"type":66,"tag":107,"props":473,"children":474},{"style":120},[475],{"type":71,"value":290},{"type":66,"tag":107,"props":477,"children":478},{"style":220},[479],{"type":71,"value":368},{"type":66,"tag":107,"props":481,"children":482},{"style":120},[483],{"type":71,"value":373},{"type":66,"tag":107,"props":485,"children":487},{"class":109,"line":486},13,[488,493,497,502,507,511,515,519,523,527,532,536,540,544,549,554,559,563],{"type":66,"tag":107,"props":489,"children":490},{"style":236},[491],{"type":71,"value":492},"        trackAnalytics",{"type":66,"tag":107,"props":494,"children":495},{"style":255},[496],{"type":71,"value":243},{"type":66,"tag":107,"props":498,"children":499},{"style":120},[500],{"type":71,"value":501},"{",{"type":66,"tag":107,"props":503,"children":504},{"style":255},[505],{"type":71,"value":506}," model",{"type":66,"tag":107,"props":508,"children":509},{"style":120},[510],{"type":71,"value":263},{"type":66,"tag":107,"props":512,"children":513},{"style":126},[514],{"type":71,"value":418},{"type":66,"tag":107,"props":516,"children":517},{"style":120},[518],{"type":71,"value":387},{"type":66,"tag":107,"props":520,"children":521},{"style":126},[522],{"type":71,"value":427},{"type":66,"tag":107,"props":524,"children":525},{"style":120},[526],{"type":71,"value":134},{"type":66,"tag":107,"props":528,"children":529},{"style":255},[530],{"type":71,"value":531}," tokens",{"type":66,"tag":107,"props":533,"children":534},{"style":120},[535],{"type":71,"value":263},{"type":66,"tag":107,"props":537,"children":538},{"style":126},[539],{"type":71,"value":471},{"type":66,"tag":107,"props":541,"children":542},{"style":120},[543],{"type":71,"value":387},{"type":66,"tag":107,"props":545,"children":546},{"style":126},[547],{"type":71,"value":548},"usage",{"type":66,"tag":107,"props":550,"children":551},{"style":120},[552],{"type":71,"value":553},"?.",{"type":66,"tag":107,"props":555,"children":556},{"style":126},[557],{"type":71,"value":558},"totalTokens",{"type":66,"tag":107,"props":560,"children":561},{"style":120},[562],{"type":71,"value":144},{"type":66,"tag":107,"props":564,"children":565},{"style":255},[566],{"type":71,"value":432},{"type":66,"tag":107,"props":568,"children":570},{"class":109,"line":569},14,[571],{"type":66,"tag":107,"props":572,"children":573},{"style":120},[574],{"type":71,"value":441},{"type":66,"tag":107,"props":576,"children":578},{"class":109,"line":577},15,[579,584,588,592,596,600,604,608,612],{"type":66,"tag":107,"props":580,"children":581},{"style":236},[582],{"type":71,"value":583},"      onError",{"type":66,"tag":107,"props":585,"children":586},{"style":120},[587],{"type":71,"value":263},{"type":66,"tag":107,"props":589,"children":590},{"style":120},[591],{"type":71,"value":353},{"type":66,"tag":107,"props":593,"children":594},{"style":356},[595],{"type":71,"value":359},{"type":66,"tag":107,"props":597,"children":598},{"style":120},[599],{"type":71,"value":134},{"type":66,"tag":107,"props":601,"children":602},{"style":356},[603],{"type":71,"value":471},{"type":66,"tag":107,"props":605,"children":606},{"style":120},[607],{"type":71,"value":290},{"type":66,"tag":107,"props":609,"children":610},{"style":220},[611],{"type":71,"value":368},{"type":66,"tag":107,"props":613,"children":614},{"style":120},[615],{"type":71,"value":373},{"type":66,"tag":107,"props":617,"children":619},{"class":109,"line":618},16,[620,625,629,634,638,643],{"type":66,"tag":107,"props":621,"children":622},{"style":236},[623],{"type":71,"value":624},"        reportError",{"type":66,"tag":107,"props":626,"children":627},{"style":255},[628],{"type":71,"value":243},{"type":66,"tag":107,"props":630,"children":631},{"style":126},[632],{"type":71,"value":633},"info",{"type":66,"tag":107,"props":635,"children":636},{"style":120},[637],{"type":71,"value":387},{"type":66,"tag":107,"props":639,"children":640},{"style":126},[641],{"type":71,"value":642},"error",{"type":66,"tag":107,"props":644,"children":645},{"style":255},[646],{"type":71,"value":432},{"type":66,"tag":107,"props":648,"children":650},{"class":109,"line":649},17,[651],{"type":66,"tag":107,"props":652,"children":653},{"style":120},[654],{"type":71,"value":441},{"type":66,"tag":107,"props":656,"children":658},{"class":109,"line":657},18,[659],{"type":66,"tag":107,"props":660,"children":661},{"style":120},[662],{"type":71,"value":663},"    },\n",{"type":66,"tag":107,"props":665,"children":667},{"class":109,"line":666},19,[668,673],{"type":66,"tag":107,"props":669,"children":670},{"style":126},[671],{"type":71,"value":672},"  ]",{"type":66,"tag":107,"props":674,"children":675},{"style":120},[676],{"type":71,"value":295},{"type":66,"tag":107,"props":678,"children":680},{"class":109,"line":679},20,[681,686],{"type":66,"tag":107,"props":682,"children":683},{"style":120},[684],{"type":71,"value":685},"}",{"type":66,"tag":107,"props":687,"children":688},{"style":126},[689],{"type":71,"value":432},{"type":66,"tag":107,"props":691,"children":693},{"class":109,"line":692},21,[694],{"type":66,"tag":107,"props":695,"children":696},{"emptyLinePlaceholder":210},[697],{"type":71,"value":213},{"type":66,"tag":107,"props":699,"children":701},{"class":109,"line":700},22,[702,707,711],{"type":66,"tag":107,"props":703,"children":704},{"style":114},[705],{"type":71,"value":706},"return",{"type":66,"tag":107,"props":708,"children":709},{"style":236},[710],{"type":71,"value":139},{"type":66,"tag":107,"props":712,"children":713},{"style":126},[714],{"type":71,"value":715},"(stream)\n",{"type":66,"tag":89,"props":717,"children":719},{"id":718},"hooks-reference",[720],{"type":71,"value":721},"Hooks Reference",{"type":66,"tag":77,"props":723,"children":724},{},[725,727,733,735,741,743,749,750,756,757,763,764,770,771,776,777,783,784,790,791,797,798,804],{"type":71,"value":726},"Every hook receives a ",{"type":66,"tag":103,"props":728,"children":730},{"className":729},[],[731],{"type":71,"value":732},"ChatMiddlewareContext",{"type":71,"value":734}," as its first argument, which provides\n",{"type":66,"tag":103,"props":736,"children":738},{"className":737},[],[739],{"type":71,"value":740},"requestId",{"type":71,"value":742},", ",{"type":66,"tag":103,"props":744,"children":746},{"className":745},[],[747],{"type":71,"value":748},"streamId",{"type":71,"value":742},{"type":66,"tag":103,"props":751,"children":753},{"className":752},[],[754],{"type":71,"value":755},"phase",{"type":71,"value":742},{"type":66,"tag":103,"props":758,"children":760},{"className":759},[],[761],{"type":71,"value":762},"iteration",{"type":71,"value":742},{"type":66,"tag":103,"props":765,"children":767},{"className":766},[],[768],{"type":71,"value":769},"chunkIndex",{"type":71,"value":742},{"type":66,"tag":103,"props":772,"children":774},{"className":773},[],[775],{"type":71,"value":427},{"type":71,"value":742},{"type":66,"tag":103,"props":778,"children":780},{"className":779},[],[781],{"type":71,"value":782},"provider",{"type":71,"value":295},{"type":66,"tag":103,"props":785,"children":787},{"className":786},[],[788],{"type":71,"value":789},"signal",{"type":71,"value":742},{"type":66,"tag":103,"props":792,"children":794},{"className":793},[],[795],{"type":71,"value":796},"abort()",{"type":71,"value":742},{"type":66,"tag":103,"props":799,"children":801},{"className":800},[],[802],{"type":71,"value":803},"defer()",{"type":71,"value":805},", and more.",{"type":66,"tag":807,"props":808,"children":809},"table",{},[810,834],{"type":66,"tag":811,"props":812,"children":813},"thead",{},[814],{"type":66,"tag":815,"props":816,"children":817},"tr",{},[818,824,829],{"type":66,"tag":819,"props":820,"children":821},"th",{},[822],{"type":71,"value":823},"Hook",{"type":66,"tag":819,"props":825,"children":826},{},[827],{"type":71,"value":828},"When",{"type":66,"tag":819,"props":830,"children":831},{},[832],{"type":71,"value":833},"Second Argument",{"type":66,"tag":835,"props":836,"children":837},"tbody",{},[838,883,918,945,971,1005,1038,1066,1092,1118,1152,1178,1204],{"type":66,"tag":815,"props":839,"children":840},{},[841,851,872],{"type":66,"tag":842,"props":843,"children":844},"td",{},[845],{"type":66,"tag":103,"props":846,"children":848},{"className":847},[],[849],{"type":71,"value":850},"onConfig",{"type":66,"tag":842,"props":852,"children":853},{},[854,856,862,864,870],{"type":71,"value":855},"Once at startup (",{"type":66,"tag":103,"props":857,"children":859},{"className":858},[],[860],{"type":71,"value":861},"init",{"type":71,"value":863},") + once per iteration (",{"type":66,"tag":103,"props":865,"children":867},{"className":866},[],[868],{"type":71,"value":869},"beforeModel",{"type":71,"value":871},") + once at structured-output boundary",{"type":66,"tag":842,"props":873,"children":874},{},[875,881],{"type":66,"tag":103,"props":876,"children":878},{"className":877},[],[879],{"type":71,"value":880},"ChatMiddlewareConfig",{"type":71,"value":882}," (return partial to merge)",{"type":66,"tag":815,"props":884,"children":885},{},[886,895,907],{"type":66,"tag":842,"props":887,"children":888},{},[889],{"type":66,"tag":103,"props":890,"children":892},{"className":891},[],[893],{"type":71,"value":894},"onStructuredOutputConfig",{"type":66,"tag":842,"props":896,"children":897},{},[898,900,906],{"type":71,"value":899},"Once at the structured-output boundary (only when ",{"type":66,"tag":103,"props":901,"children":903},{"className":902},[],[904],{"type":71,"value":905},"chat({ outputSchema })",{"type":71,"value":290},{"type":66,"tag":842,"props":908,"children":909},{},[910,916],{"type":66,"tag":103,"props":911,"children":913},{"className":912},[],[914],{"type":71,"value":915},"StructuredOutputMiddlewareConfig",{"type":71,"value":917}," (return partial)",{"type":66,"tag":815,"props":919,"children":920},{},[921,930,940],{"type":66,"tag":842,"props":922,"children":923},{},[924],{"type":66,"tag":103,"props":925,"children":927},{"className":926},[],[928],{"type":71,"value":929},"onStart",{"type":66,"tag":842,"props":931,"children":932},{},[933,935],{"type":71,"value":934},"Once after initial ",{"type":66,"tag":103,"props":936,"children":938},{"className":937},[],[939],{"type":71,"value":850},{"type":66,"tag":842,"props":941,"children":942},{},[943],{"type":71,"value":944},"none",{"type":66,"tag":815,"props":946,"children":947},{},[948,957,962],{"type":66,"tag":842,"props":949,"children":950},{},[951],{"type":66,"tag":103,"props":952,"children":954},{"className":953},[],[955],{"type":71,"value":956},"onIteration",{"type":66,"tag":842,"props":958,"children":959},{},[960],{"type":71,"value":961},"Start of each agent loop iteration",{"type":66,"tag":842,"props":963,"children":964},{},[965],{"type":66,"tag":103,"props":966,"children":968},{"className":967},[],[969],{"type":71,"value":970},"IterationInfo",{"type":66,"tag":815,"props":972,"children":973},{},[974,983,996],{"type":66,"tag":842,"props":975,"children":976},{},[977],{"type":66,"tag":103,"props":978,"children":980},{"className":979},[],[981],{"type":71,"value":982},"onShouldContinue",{"type":66,"tag":842,"props":984,"children":985},{},[986,988,994],{"type":71,"value":987},"Whether to start another agent-loop iteration (AND with strategy; ",{"type":66,"tag":103,"props":989,"children":991},{"className":990},[],[992],{"type":71,"value":993},"false",{"type":71,"value":995}," stops)",{"type":66,"tag":842,"props":997,"children":998},{},[999],{"type":66,"tag":103,"props":1000,"children":1002},{"className":1001},[],[1003],{"type":71,"value":1004},"AgentLoopState",{"type":66,"tag":815,"props":1006,"children":1007},{},[1008,1017,1022],{"type":66,"tag":842,"props":1009,"children":1010},{},[1011],{"type":66,"tag":103,"props":1012,"children":1014},{"className":1013},[],[1015],{"type":71,"value":1016},"onChunk",{"type":66,"tag":842,"props":1018,"children":1019},{},[1020],{"type":71,"value":1021},"Every streamed chunk",{"type":66,"tag":842,"props":1023,"children":1024},{},[1025,1031,1033,1036],{"type":66,"tag":103,"props":1026,"children":1028},{"className":1027},[],[1029],{"type":71,"value":1030},"StreamChunk",{"type":71,"value":1032}," (return void\u002Fchunk\u002Fchunk",{"type":66,"tag":107,"props":1034,"children":1035},{},[],{"type":71,"value":1037},"\u002Fnull)",{"type":66,"tag":815,"props":1039,"children":1040},{},[1041,1050,1055],{"type":66,"tag":842,"props":1042,"children":1043},{},[1044],{"type":66,"tag":103,"props":1045,"children":1047},{"className":1046},[],[1048],{"type":71,"value":1049},"onBeforeToolCall",{"type":66,"tag":842,"props":1051,"children":1052},{},[1053],{"type":71,"value":1054},"Before each tool executes",{"type":66,"tag":842,"props":1056,"children":1057},{},[1058,1064],{"type":66,"tag":103,"props":1059,"children":1061},{"className":1060},[],[1062],{"type":71,"value":1063},"ToolCallHookContext",{"type":71,"value":1065}," (return decision or void)",{"type":66,"tag":815,"props":1067,"children":1068},{},[1069,1078,1083],{"type":66,"tag":842,"props":1070,"children":1071},{},[1072],{"type":66,"tag":103,"props":1073,"children":1075},{"className":1074},[],[1076],{"type":71,"value":1077},"onAfterToolCall",{"type":66,"tag":842,"props":1079,"children":1080},{},[1081],{"type":71,"value":1082},"After each tool executes",{"type":66,"tag":842,"props":1084,"children":1085},{},[1086],{"type":66,"tag":103,"props":1087,"children":1089},{"className":1088},[],[1090],{"type":71,"value":1091},"AfterToolCallInfo",{"type":66,"tag":815,"props":1093,"children":1094},{},[1095,1104,1109],{"type":66,"tag":842,"props":1096,"children":1097},{},[1098],{"type":66,"tag":103,"props":1099,"children":1101},{"className":1100},[],[1102],{"type":71,"value":1103},"onToolPhaseComplete",{"type":66,"tag":842,"props":1105,"children":1106},{},[1107],{"type":71,"value":1108},"After all tool calls in an iteration",{"type":66,"tag":842,"props":1110,"children":1111},{},[1112],{"type":66,"tag":103,"props":1113,"children":1115},{"className":1114},[],[1116],{"type":71,"value":1117},"ToolPhaseCompleteInfo",{"type":66,"tag":815,"props":1119,"children":1120},{},[1121,1130,1143],{"type":66,"tag":842,"props":1122,"children":1123},{},[1124],{"type":66,"tag":103,"props":1125,"children":1127},{"className":1126},[],[1128],{"type":71,"value":1129},"onUsage",{"type":66,"tag":842,"props":1131,"children":1132},{},[1133,1135,1141],{"type":71,"value":1134},"When ",{"type":66,"tag":103,"props":1136,"children":1138},{"className":1137},[],[1139],{"type":71,"value":1140},"RUN_FINISHED",{"type":71,"value":1142}," includes usage data",{"type":66,"tag":842,"props":1144,"children":1145},{},[1146],{"type":66,"tag":103,"props":1147,"children":1149},{"className":1148},[],[1150],{"type":71,"value":1151},"UsageInfo",{"type":66,"tag":815,"props":1153,"children":1154},{},[1155,1164,1169],{"type":66,"tag":842,"props":1156,"children":1157},{},[1158],{"type":66,"tag":103,"props":1159,"children":1161},{"className":1160},[],[1162],{"type":71,"value":1163},"onFinish",{"type":66,"tag":842,"props":1165,"children":1166},{},[1167],{"type":71,"value":1168},"Run completed normally",{"type":66,"tag":842,"props":1170,"children":1171},{},[1172],{"type":66,"tag":103,"props":1173,"children":1175},{"className":1174},[],[1176],{"type":71,"value":1177},"FinishInfo",{"type":66,"tag":815,"props":1179,"children":1180},{},[1181,1190,1195],{"type":66,"tag":842,"props":1182,"children":1183},{},[1184],{"type":66,"tag":103,"props":1185,"children":1187},{"className":1186},[],[1188],{"type":71,"value":1189},"onAbort",{"type":66,"tag":842,"props":1191,"children":1192},{},[1193],{"type":71,"value":1194},"Run was aborted",{"type":66,"tag":842,"props":1196,"children":1197},{},[1198],{"type":66,"tag":103,"props":1199,"children":1201},{"className":1200},[],[1202],{"type":71,"value":1203},"AbortInfo",{"type":66,"tag":815,"props":1205,"children":1206},{},[1207,1216,1221],{"type":66,"tag":842,"props":1208,"children":1209},{},[1210],{"type":66,"tag":103,"props":1211,"children":1213},{"className":1212},[],[1214],{"type":71,"value":1215},"onError",{"type":66,"tag":842,"props":1217,"children":1218},{},[1219],{"type":71,"value":1220},"Unhandled error occurred",{"type":66,"tag":842,"props":1222,"children":1223},{},[1224],{"type":66,"tag":103,"props":1225,"children":1227},{"className":1226},[],[1228],{"type":71,"value":1229},"ErrorInfo",{"type":66,"tag":77,"props":1231,"children":1232},{},[1233,1235,1240,1241,1246,1247,1252,1254,1259,1261,1267],{"type":71,"value":1234},"Terminal hooks (",{"type":66,"tag":103,"props":1236,"children":1238},{"className":1237},[],[1239],{"type":71,"value":1163},{"type":71,"value":742},{"type":66,"tag":103,"props":1242,"children":1244},{"className":1243},[],[1245],{"type":71,"value":1189},{"type":71,"value":742},{"type":66,"tag":103,"props":1248,"children":1250},{"className":1249},[],[1251],{"type":71,"value":1215},{"type":71,"value":1253},") are ",{"type":66,"tag":81,"props":1255,"children":1256},{},[1257],{"type":71,"value":1258},"mutually exclusive",{"type":71,"value":1260}," -- exactly\none fires per ",{"type":66,"tag":103,"props":1262,"children":1264},{"className":1263},[],[1265],{"type":71,"value":1266},"chat()",{"type":71,"value":1268}," invocation.",{"type":66,"tag":73,"props":1270,"children":1271},{},[1272],{"type":66,"tag":77,"props":1273,"children":1274},{},[1275,1286,1288,1294,1295,1301,1303,1309,1311,1316,1318,1323,1325,1331,1333,1338,1340,1346,1348,1354,1356,1362,1364,1369,1371,1376],{"type":66,"tag":81,"props":1276,"children":1277},{},[1278,1280,1285],{"type":71,"value":1279},"Sampling in ",{"type":66,"tag":103,"props":1281,"children":1283},{"className":1282},[],[1284],{"type":71,"value":850},{"type":71,"value":263},{"type":71,"value":1287}," ",{"type":66,"tag":103,"props":1289,"children":1291},{"className":1290},[],[1292],{"type":71,"value":1293},"temperature",{"type":71,"value":742},{"type":66,"tag":103,"props":1296,"children":1298},{"className":1297},[],[1299],{"type":71,"value":1300},"topP",{"type":71,"value":1302},", and ",{"type":66,"tag":103,"props":1304,"children":1306},{"className":1305},[],[1307],{"type":71,"value":1308},"maxTokens",{"type":71,"value":1310}," are ",{"type":66,"tag":81,"props":1312,"children":1313},{},[1314],{"type":71,"value":1315},"not",{"type":71,"value":1317},"\nfirst-class fields on ",{"type":66,"tag":103,"props":1319,"children":1321},{"className":1320},[],[1322],{"type":71,"value":880},{"type":71,"value":1324},". To adjust sampling from\nmiddleware, return a partial that mutates ",{"type":66,"tag":103,"props":1326,"children":1328},{"className":1327},[],[1329],{"type":71,"value":1330},"config.modelOptions",{"type":71,"value":1332}," using the\nprovider's native key (e.g. OpenAI ",{"type":66,"tag":103,"props":1334,"children":1336},{"className":1335},[],[1337],{"type":71,"value":1293},{"type":71,"value":1339}," \u002F ",{"type":66,"tag":103,"props":1341,"children":1343},{"className":1342},[],[1344],{"type":71,"value":1345},"max_output_tokens",{"type":71,"value":1347},",\nAnthropic ",{"type":66,"tag":103,"props":1349,"children":1351},{"className":1350},[],[1352],{"type":71,"value":1353},"max_tokens",{"type":71,"value":1355},", Ollama nested ",{"type":66,"tag":103,"props":1357,"children":1359},{"className":1358},[],[1360],{"type":71,"value":1361},"options.num_predict",{"type":71,"value":1363},"). Returning a\ntop-level ",{"type":66,"tag":103,"props":1365,"children":1367},{"className":1366},[],[1368],{"type":71,"value":1293},{"type":71,"value":1370},"\u002F",{"type":66,"tag":103,"props":1372,"children":1374},{"className":1373},[],[1375],{"type":71,"value":1308},{"type":71,"value":1377}," has no effect.",{"type":66,"tag":1379,"props":1380,"children":1382},"h3",{"id":1381},"phase-values",[1383],{"type":71,"value":1384},"Phase values",{"type":66,"tag":77,"props":1386,"children":1387},{},[1388,1394],{"type":66,"tag":103,"props":1389,"children":1391},{"className":1390},[],[1392],{"type":71,"value":1393},"ctx.phase",{"type":71,"value":1395}," is one of:",{"type":66,"tag":807,"props":1397,"children":1398},{},[1399,1414],{"type":66,"tag":811,"props":1400,"children":1401},{},[1402],{"type":66,"tag":815,"props":1403,"children":1404},{},[1405,1410],{"type":66,"tag":819,"props":1406,"children":1407},{},[1408],{"type":71,"value":1409},"Phase",{"type":66,"tag":819,"props":1411,"children":1412},{},[1413],{"type":71,"value":828},{"type":66,"tag":835,"props":1415,"children":1416},{},[1417,1441,1465,1482,1499,1516],{"type":66,"tag":815,"props":1418,"children":1419},{},[1420,1429],{"type":66,"tag":842,"props":1421,"children":1422},{},[1423],{"type":66,"tag":103,"props":1424,"children":1426},{"className":1425},[],[1427],{"type":71,"value":1428},"'init'",{"type":66,"tag":842,"props":1430,"children":1431},{},[1432,1434,1439],{"type":71,"value":1433},"Initial setup (before the first ",{"type":66,"tag":103,"props":1435,"children":1437},{"className":1436},[],[1438],{"type":71,"value":850},{"type":71,"value":1440}," snapshot is built).",{"type":66,"tag":815,"props":1442,"children":1443},{},[1444,1453],{"type":66,"tag":842,"props":1445,"children":1446},{},[1447],{"type":66,"tag":103,"props":1448,"children":1450},{"className":1449},[],[1451],{"type":71,"value":1452},"'beforeModel'",{"type":66,"tag":842,"props":1454,"children":1455},{},[1456,1458,1463],{"type":71,"value":1457},"Right before each agent-loop adapter call (",{"type":66,"tag":103,"props":1459,"children":1461},{"className":1460},[],[1462],{"type":71,"value":850},{"type":71,"value":1464}," re-fires here).",{"type":66,"tag":815,"props":1466,"children":1467},{},[1468,1477],{"type":66,"tag":842,"props":1469,"children":1470},{},[1471],{"type":66,"tag":103,"props":1472,"children":1474},{"className":1473},[],[1475],{"type":71,"value":1476},"'modelStream'",{"type":66,"tag":842,"props":1478,"children":1479},{},[1480],{"type":71,"value":1481},"During model streaming chunks within the agent loop.",{"type":66,"tag":815,"props":1483,"children":1484},{},[1485,1494],{"type":66,"tag":842,"props":1486,"children":1487},{},[1488],{"type":66,"tag":103,"props":1489,"children":1491},{"className":1490},[],[1492],{"type":71,"value":1493},"'beforeTools'",{"type":66,"tag":842,"props":1495,"children":1496},{},[1497],{"type":71,"value":1498},"Before tool execution phase.",{"type":66,"tag":815,"props":1500,"children":1501},{},[1502,1511],{"type":66,"tag":842,"props":1503,"children":1504},{},[1505],{"type":66,"tag":103,"props":1506,"children":1508},{"className":1507},[],[1509],{"type":71,"value":1510},"'afterTools'",{"type":66,"tag":842,"props":1512,"children":1513},{},[1514],{"type":71,"value":1515},"After tool execution phase.",{"type":66,"tag":815,"props":1517,"children":1518},{},[1519,1528],{"type":66,"tag":842,"props":1520,"children":1521},{},[1522],{"type":66,"tag":103,"props":1523,"children":1525},{"className":1524},[],[1526],{"type":71,"value":1527},"'structuredOutput'",{"type":66,"tag":842,"props":1529,"children":1530},{},[1531,1533,1539,1541,1546,1548,1553],{"type":71,"value":1532},"During the final structured-output adapter call (set for all chunks from ",{"type":66,"tag":103,"props":1534,"children":1536},{"className":1535},[],[1537],{"type":71,"value":1538},"adapter.structuredOutputStream",{"type":71,"value":1540}," or the synthesized fallback). Triggered only when ",{"type":66,"tag":103,"props":1542,"children":1544},{"className":1543},[],[1545],{"type":71,"value":905},{"type":71,"value":1547}," is invoked; one phase transition per ",{"type":66,"tag":103,"props":1549,"children":1551},{"className":1550},[],[1552],{"type":71,"value":1266},{"type":71,"value":1268},{"type":66,"tag":77,"props":1555,"children":1556},{},[1557,1562,1564,1569],{"type":66,"tag":81,"props":1558,"children":1559},{},[1560],{"type":71,"value":1561},"Structured-output lifecycle rules",{"type":71,"value":1563}," (when ",{"type":66,"tag":103,"props":1565,"children":1567},{"className":1566},[],[1568],{"type":71,"value":905},{"type":71,"value":1570}," is used):",{"type":66,"tag":1572,"props":1573,"children":1574},"ul",{},[1575,1599,1632,1655,1671,1713],{"type":66,"tag":1576,"props":1577,"children":1578},"li",{},[1579,1584,1586,1591,1592,1597],{"type":66,"tag":103,"props":1580,"children":1582},{"className":1581},[],[1583],{"type":71,"value":894},{"type":71,"value":1585}," fires ",{"type":66,"tag":81,"props":1587,"children":1588},{},[1589],{"type":71,"value":1590},"before",{"type":71,"value":1287},{"type":66,"tag":103,"props":1593,"children":1595},{"className":1594},[],[1596],{"type":71,"value":850},{"type":71,"value":1598}," at the structured-output boundary.",{"type":66,"tag":1576,"props":1600,"children":1601},{},[1602,1607,1609,1615,1617,1622,1624,1630],{"type":66,"tag":103,"props":1603,"children":1605},{"className":1604},[],[1606],{"type":71,"value":850},{"type":71,"value":1608}," re-fires at the same boundary with ",{"type":66,"tag":103,"props":1610,"children":1612},{"className":1611},[],[1613],{"type":71,"value":1614},"ctx.phase === 'structuredOutput'",{"type":71,"value":1616},", receiving the post-",{"type":66,"tag":103,"props":1618,"children":1620},{"className":1619},[],[1621],{"type":71,"value":894},{"type":71,"value":1623}," view of the config (minus ",{"type":66,"tag":103,"props":1625,"children":1627},{"className":1626},[],[1628],{"type":71,"value":1629},"outputSchema",{"type":71,"value":1631},").",{"type":66,"tag":1576,"props":1633,"children":1634},{},[1635,1640,1642,1647,1649,1654],{"type":66,"tag":103,"props":1636,"children":1638},{"className":1637},[],[1639],{"type":71,"value":1016},{"type":71,"value":1641}," and ",{"type":66,"tag":103,"props":1643,"children":1645},{"className":1644},[],[1646],{"type":71,"value":1129},{"type":71,"value":1648}," fire for every chunk and usage event emitted by the structured-output call, with ",{"type":66,"tag":103,"props":1650,"children":1652},{"className":1651},[],[1653],{"type":71,"value":1614},{"type":71,"value":387},{"type":66,"tag":1576,"props":1656,"children":1657},{},[1658,1663,1665,1669],{"type":66,"tag":103,"props":1659,"children":1661},{"className":1660},[],[1662],{"type":71,"value":956},{"type":71,"value":1664}," does ",{"type":66,"tag":81,"props":1666,"children":1667},{},[1668],{"type":71,"value":1315},{"type":71,"value":1670}," fire for finalization — it is agent-loop-only.",{"type":66,"tag":1576,"props":1672,"children":1673},{},[1674,1679,1681,1686,1688,1693,1695,1700,1701,1706,1707,1712],{"type":66,"tag":103,"props":1675,"children":1677},{"className":1676},[],[1678],{"type":71,"value":1163},{"type":71,"value":1680}," fires once at the end of the whole ",{"type":66,"tag":103,"props":1682,"children":1684},{"className":1683},[],[1685],{"type":71,"value":1266},{"type":71,"value":1687}," invocation, ",{"type":66,"tag":81,"props":1689,"children":1690},{},[1691],{"type":71,"value":1692},"after",{"type":71,"value":1694}," the structured-output finalization completes (not after the agent loop). Terminal-hook exclusivity still holds (one of ",{"type":66,"tag":103,"props":1696,"children":1698},{"className":1697},[],[1699],{"type":71,"value":1163},{"type":71,"value":1339},{"type":66,"tag":103,"props":1702,"children":1704},{"className":1703},[],[1705],{"type":71,"value":1189},{"type":71,"value":1339},{"type":66,"tag":103,"props":1708,"children":1710},{"className":1709},[],[1711],{"type":71,"value":1215},{"type":71,"value":1631},{"type":66,"tag":1576,"props":1714,"children":1715},{},[1716,1728,1729,1735,1736,1742,1743,1749,1751,1756,1758,1763,1765,1770,1772,1778,1779,1784,1785,1791,1793,1798,1800,1805,1807,1813,1815,1820],{"type":66,"tag":81,"props":1717,"children":1718},{},[1719,1721,1726],{"type":71,"value":1720},"Terminal ",{"type":66,"tag":103,"props":1722,"children":1724},{"className":1723},[],[1725],{"type":71,"value":633},{"type":71,"value":1727}," and structured-output:",{"type":71,"value":1287},{"type":66,"tag":103,"props":1730,"children":1732},{"className":1731},[],[1733],{"type":71,"value":1734},"info.usage",{"type":71,"value":1339},{"type":66,"tag":103,"props":1737,"children":1739},{"className":1738},[],[1740],{"type":71,"value":1741},"info.finishReason",{"type":71,"value":1339},{"type":66,"tag":103,"props":1744,"children":1746},{"className":1745},[],[1747],{"type":71,"value":1748},"info.content",{"type":71,"value":1750}," reflect the ",{"type":66,"tag":81,"props":1752,"children":1753},{},[1754],{"type":71,"value":1755},"agent loop's",{"type":71,"value":1757}," terminal state, NOT the finalization step. Finalization state is intentionally segregated to keep agent-loop semantics clean. For a tools-less ",{"type":66,"tag":103,"props":1759,"children":1761},{"className":1760},[],[1762],{"type":71,"value":905},{"type":71,"value":1764}," run, ",{"type":66,"tag":103,"props":1766,"children":1768},{"className":1767},[],[1769],{"type":71,"value":1734},{"type":71,"value":1771}," is ",{"type":66,"tag":103,"props":1773,"children":1775},{"className":1774},[],[1776],{"type":71,"value":1777},"undefined",{"type":71,"value":1641},{"type":66,"tag":103,"props":1780,"children":1782},{"className":1781},[],[1783],{"type":71,"value":1741},{"type":71,"value":1771},{"type":66,"tag":103,"props":1786,"children":1788},{"className":1787},[],[1789],{"type":71,"value":1790},"null",{"type":71,"value":1792}," (no agent-loop iteration produced ",{"type":66,"tag":103,"props":1794,"children":1796},{"className":1795},[],[1797],{"type":71,"value":1140},{"type":71,"value":1799},"). To capture finalization tokens, use ",{"type":66,"tag":103,"props":1801,"children":1803},{"className":1802},[],[1804],{"type":71,"value":1129},{"type":71,"value":1806}," — it fires for both agent-loop iterations and the final call. For the structured-output result itself, observe the ",{"type":66,"tag":103,"props":1808,"children":1810},{"className":1809},[],[1811],{"type":71,"value":1812},"structured-output.complete",{"type":71,"value":1814}," CUSTOM event in ",{"type":66,"tag":103,"props":1816,"children":1818},{"className":1817},[],[1819],{"type":71,"value":1016},{"type":71,"value":387},{"type":66,"tag":89,"props":1822,"children":1824},{"id":1823},"onstructuredoutputconfig",[1825],{"type":71,"value":894},{"type":66,"tag":77,"props":1827,"children":1828},{},[1829,1831,1836,1838,1843,1845,1851],{"type":71,"value":1830},"A dedicated config hook that fires ",{"type":66,"tag":81,"props":1832,"children":1833},{},[1834],{"type":71,"value":1835},"only",{"type":71,"value":1837}," at the structured-output boundary\n(when ",{"type":66,"tag":103,"props":1839,"children":1841},{"className":1840},[],[1842],{"type":71,"value":905},{"type":71,"value":1844}," is invoked). Use it to transform the JSON Schema\nsent to the provider (inject ",{"type":66,"tag":103,"props":1846,"children":1848},{"className":1847},[],[1849],{"type":71,"value":1850},"$defs",{"type":71,"value":1852},", strip vendor-incompatible keywords) or to\napply structured-output-specific config changes that should not affect the\nagent-loop adapter calls.",{"type":66,"tag":77,"props":1854,"children":1855},{},[1856],{"type":66,"tag":81,"props":1857,"children":1858},{},[1859],{"type":71,"value":1860},"Signature:",{"type":66,"tag":96,"props":1862,"children":1866},{"className":1863,"code":1864,"language":1865,"meta":100,"style":100},"language-ts shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","onStructuredOutputConfig?: (\n  ctx: ChatMiddlewareContext,\n  config: StructuredOutputMiddlewareConfig,\n) =>\n  | void\n  | null\n  | Partial\u003CStructuredOutputMiddlewareConfig>\n  | Promise\u003Cvoid | Partial\u003CStructuredOutputMiddlewareConfig>>\n","ts",[1867],{"type":66,"tag":103,"props":1868,"children":1869},{"__ignoreMap":100},[1870,1887,1899,1911,1924,1937,1949,1975],{"type":66,"tag":107,"props":1871,"children":1872},{"class":109,"line":110},[1873,1877,1882],{"type":66,"tag":107,"props":1874,"children":1875},{"style":126},[1876],{"type":71,"value":894},{"type":66,"tag":107,"props":1878,"children":1879},{"style":120},[1880],{"type":71,"value":1881},"?:",{"type":66,"tag":107,"props":1883,"children":1884},{"style":126},[1885],{"type":71,"value":1886}," (\n",{"type":66,"tag":107,"props":1888,"children":1889},{"class":109,"line":168},[1890,1895],{"type":66,"tag":107,"props":1891,"children":1892},{"style":126},[1893],{"type":71,"value":1894},"  ctx: ChatMiddlewareContext",{"type":66,"tag":107,"props":1896,"children":1897},{"style":120},[1898],{"type":71,"value":295},{"type":66,"tag":107,"props":1900,"children":1901},{"class":109,"line":206},[1902,1907],{"type":66,"tag":107,"props":1903,"children":1904},{"style":126},[1905],{"type":71,"value":1906},"  config: StructuredOutputMiddlewareConfig",{"type":66,"tag":107,"props":1908,"children":1909},{"style":120},[1910],{"type":71,"value":295},{"type":66,"tag":107,"props":1912,"children":1913},{"class":109,"line":216},[1914,1919],{"type":66,"tag":107,"props":1915,"children":1916},{"style":126},[1917],{"type":71,"value":1918},") ",{"type":66,"tag":107,"props":1920,"children":1921},{"style":220},[1922],{"type":71,"value":1923},"=>\n",{"type":66,"tag":107,"props":1925,"children":1926},{"class":109,"line":251},[1927,1932],{"type":66,"tag":107,"props":1928,"children":1929},{"style":120},[1930],{"type":71,"value":1931},"  |",{"type":66,"tag":107,"props":1933,"children":1934},{"style":120},[1935],{"type":71,"value":1936}," void\n",{"type":66,"tag":107,"props":1938,"children":1939},{"class":109,"line":298},[1940,1944],{"type":66,"tag":107,"props":1941,"children":1942},{"style":120},[1943],{"type":71,"value":1931},{"type":66,"tag":107,"props":1945,"children":1946},{"style":120},[1947],{"type":71,"value":1948}," null\n",{"type":66,"tag":107,"props":1950,"children":1951},{"class":109,"line":311},[1952,1956,1961,1966,1970],{"type":66,"tag":107,"props":1953,"children":1954},{"style":120},[1955],{"type":71,"value":1931},{"type":66,"tag":107,"props":1957,"children":1958},{"style":126},[1959],{"type":71,"value":1960}," Partial",{"type":66,"tag":107,"props":1962,"children":1963},{"style":120},[1964],{"type":71,"value":1965},"\u003C",{"type":66,"tag":107,"props":1967,"children":1968},{"style":126},[1969],{"type":71,"value":915},{"type":66,"tag":107,"props":1971,"children":1972},{"style":120},[1973],{"type":71,"value":1974},">\n",{"type":66,"tag":107,"props":1976,"children":1977},{"class":109,"line":329},[1978,1982,1988,1993,1998,2002,2006,2010],{"type":66,"tag":107,"props":1979,"children":1980},{"style":120},[1981],{"type":71,"value":1931},{"type":66,"tag":107,"props":1983,"children":1985},{"style":1984},"--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B",[1986],{"type":71,"value":1987}," Promise",{"type":66,"tag":107,"props":1989,"children":1990},{"style":120},[1991],{"type":71,"value":1992},"\u003Cvoid",{"type":66,"tag":107,"props":1994,"children":1995},{"style":120},[1996],{"type":71,"value":1997}," |",{"type":66,"tag":107,"props":1999,"children":2000},{"style":126},[2001],{"type":71,"value":1960},{"type":66,"tag":107,"props":2003,"children":2004},{"style":120},[2005],{"type":71,"value":1965},{"type":66,"tag":107,"props":2007,"children":2008},{"style":126},[2009],{"type":71,"value":915},{"type":66,"tag":107,"props":2011,"children":2012},{"style":120},[2013],{"type":71,"value":2014},">>\n",{"type":66,"tag":77,"props":2016,"children":2017},{},[2018],{"type":66,"tag":81,"props":2019,"children":2020},{},[2021,2026],{"type":66,"tag":103,"props":2022,"children":2024},{"className":2023},[],[2025],{"type":71,"value":915},{"type":71,"value":2027}," shape:",{"type":66,"tag":96,"props":2029,"children":2031},{"className":1863,"code":2030,"language":1865,"meta":100,"style":100},"interface StructuredOutputMiddlewareConfig extends ChatMiddlewareConfig {\n  outputSchema: JSONSchema \u002F\u002F The JSON Schema being sent to the provider\n}\n",[2032],{"type":66,"tag":103,"props":2033,"children":2034},{"__ignoreMap":100},[2035,2062,2085],{"type":66,"tag":107,"props":2036,"children":2037},{"class":109,"line":110},[2038,2043,2048,2053,2058],{"type":66,"tag":107,"props":2039,"children":2040},{"style":220},[2041],{"type":71,"value":2042},"interface",{"type":66,"tag":107,"props":2044,"children":2045},{"style":1984},[2046],{"type":71,"value":2047}," StructuredOutputMiddlewareConfig",{"type":66,"tag":107,"props":2049,"children":2050},{"style":220},[2051],{"type":71,"value":2052}," extends",{"type":66,"tag":107,"props":2054,"children":2055},{"style":1984},[2056],{"type":71,"value":2057}," ChatMiddlewareConfig",{"type":66,"tag":107,"props":2059,"children":2060},{"style":120},[2061],{"type":71,"value":373},{"type":66,"tag":107,"props":2063,"children":2064},{"class":109,"line":168},[2065,2070,2074,2079],{"type":66,"tag":107,"props":2066,"children":2067},{"style":255},[2068],{"type":71,"value":2069},"  outputSchema",{"type":66,"tag":107,"props":2071,"children":2072},{"style":120},[2073],{"type":71,"value":263},{"type":66,"tag":107,"props":2075,"children":2076},{"style":1984},[2077],{"type":71,"value":2078}," JSONSchema",{"type":66,"tag":107,"props":2080,"children":2082},{"style":2081},"--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic",[2083],{"type":71,"value":2084}," \u002F\u002F The JSON Schema being sent to the provider\n",{"type":66,"tag":107,"props":2086,"children":2087},{"class":109,"line":206},[2088],{"type":66,"tag":107,"props":2089,"children":2090},{"style":120},[2091],{"type":71,"value":2092},"}\n",{"type":66,"tag":77,"props":2094,"children":2095},{},[2096],{"type":66,"tag":81,"props":2097,"children":2098},{},[2099],{"type":71,"value":2100},"Ordering rule:",{"type":66,"tag":1572,"props":2102,"children":2103},{},[2104,2124,2151,2163],{"type":66,"tag":1576,"props":2105,"children":2106},{},[2107,2112,2113,2117,2118,2123],{"type":66,"tag":103,"props":2108,"children":2110},{"className":2109},[],[2111],{"type":71,"value":894},{"type":71,"value":1585},{"type":66,"tag":81,"props":2114,"children":2115},{},[2116],{"type":71,"value":1590},{"type":71,"value":1287},{"type":66,"tag":103,"props":2119,"children":2121},{"className":2120},[],[2122],{"type":71,"value":850},{"type":71,"value":1598},{"type":66,"tag":1576,"props":2125,"children":2126},{},[2127,2132,2133,2138,2139,2144,2145,2150],{"type":66,"tag":103,"props":2128,"children":2130},{"className":2129},[],[2131],{"type":71,"value":850},{"type":71,"value":1608},{"type":66,"tag":103,"props":2134,"children":2136},{"className":2135},[],[2137],{"type":71,"value":1614},{"type":71,"value":1616},{"type":66,"tag":103,"props":2140,"children":2142},{"className":2141},[],[2143],{"type":71,"value":894},{"type":71,"value":1623},{"type":66,"tag":103,"props":2146,"children":2148},{"className":2147},[],[2149],{"type":71,"value":1629},{"type":71,"value":1631},{"type":66,"tag":1576,"props":2152,"children":2153},{},[2154,2156,2161],{"type":71,"value":2155},"Use ",{"type":66,"tag":103,"props":2157,"children":2159},{"className":2158},[],[2160],{"type":71,"value":850},{"type":71,"value":2162}," for general-purpose transforms that apply to every adapter call (agent-loop iterations and the final structured-output call).",{"type":66,"tag":1576,"props":2164,"children":2165},{},[2166,2167,2172],{"type":71,"value":2155},{"type":66,"tag":103,"props":2168,"children":2170},{"className":2169},[],[2171],{"type":71,"value":894},{"type":71,"value":2173}," when you need to transform the JSON Schema or apply structured-output-specific behavior.",{"type":66,"tag":89,"props":2175,"children":2177},{"id":2176},"core-patterns",[2178],{"type":71,"value":2179},"Core Patterns",{"type":66,"tag":1379,"props":2181,"children":2183},{"id":2182},"pattern-1-analytics-and-logging-middleware",[2184],{"type":71,"value":2185},"Pattern 1: Analytics and Logging Middleware",{"type":66,"tag":77,"props":2187,"children":2188},{},[2189,2190,2195,2196,2201,2202,2207,2208,2213,2215,2221],{"type":71,"value":2155},{"type":66,"tag":103,"props":2191,"children":2193},{"className":2192},[],[2194],{"type":71,"value":929},{"type":71,"value":742},{"type":66,"tag":103,"props":2197,"children":2199},{"className":2198},[],[2200],{"type":71,"value":1163},{"type":71,"value":742},{"type":66,"tag":103,"props":2203,"children":2205},{"className":2204},[],[2206],{"type":71,"value":1129},{"type":71,"value":1302},{"type":66,"tag":103,"props":2209,"children":2211},{"className":2210},[],[2212],{"type":71,"value":1215},{"type":71,"value":2214}," for comprehensive observability.\nUse ",{"type":66,"tag":103,"props":2216,"children":2218},{"className":2217},[],[2219],{"type":71,"value":2220},"ctx.defer()",{"type":71,"value":2222}," for non-blocking async side effects that should not block the stream.",{"type":66,"tag":96,"props":2224,"children":2226},{"className":98,"code":2225,"language":45,"meta":100,"style":100},"import {\n  chat,\n  toServerSentEventsResponse,\n  type ChatMiddleware,\n} from '@tanstack\u002Fai'\nimport { openaiText } from '@tanstack\u002Fai-openai'\n\nconst analytics: ChatMiddleware = {\n  name: 'analytics',\n  onStart: (ctx) => {\n    console.log(`[${ctx.requestId}] Chat started — model: ${ctx.model}`)\n  },\n  onUsage: (ctx, usage) => {\n    console.log(`[${ctx.requestId}] Tokens: ${usage.totalTokens}`)\n  },\n  onFinish: (ctx, info) => {\n    ctx.defer(\n      fetch('\u002Fapi\u002Fanalytics', {\n        method: 'POST',\n        body: JSON.stringify({\n          requestId: ctx.requestId,\n          model: ctx.model,\n          duration: info.duration,\n          tokens: info.usage?.totalTokens,\n          finishReason: info.finishReason,\n        }),\n      }),\n    )\n  },\n  onError: (ctx, info) => {\n    ctx.defer(\n      fetch('\u002Fapi\u002Ferrors', {\n        method: 'POST',\n        body: JSON.stringify({\n          requestId: ctx.requestId,\n          error: String(info.error),\n          duration: info.duration,\n        }),\n      }),\n    )\n  },\n}\n\nconst stream = chat({\n  adapter: openaiText('gpt-5.2'),\n  messages,\n  middleware: [analytics],\n})\n\nreturn toServerSentEventsResponse(stream)\n",[2227],{"type":66,"tag":103,"props":2228,"children":2229},{"__ignoreMap":100},[2230,2241,2253,2265,2282,2305,2340,2347,2376,2405,2437,2518,2526,2567,2643,2650,2690,2712,2745,2774,2808,2836,2864,2894,2931,2961,2978,2995,3004,3012,3053,3073,3106,3134,3166,3194,3236,3264,3280,3296,3304,3312,3320,3328,3356,3396,3408,3429,3441,3449],{"type":66,"tag":107,"props":2231,"children":2232},{"class":109,"line":110},[2233,2237],{"type":66,"tag":107,"props":2234,"children":2235},{"style":114},[2236],{"type":71,"value":117},{"type":66,"tag":107,"props":2238,"children":2239},{"style":120},[2240],{"type":71,"value":373},{"type":66,"tag":107,"props":2242,"children":2243},{"class":109,"line":168},[2244,2249],{"type":66,"tag":107,"props":2245,"children":2246},{"style":126},[2247],{"type":71,"value":2248},"  chat",{"type":66,"tag":107,"props":2250,"children":2251},{"style":120},[2252],{"type":71,"value":295},{"type":66,"tag":107,"props":2254,"children":2255},{"class":109,"line":206},[2256,2261],{"type":66,"tag":107,"props":2257,"children":2258},{"style":126},[2259],{"type":71,"value":2260},"  toServerSentEventsResponse",{"type":66,"tag":107,"props":2262,"children":2263},{"style":120},[2264],{"type":71,"value":295},{"type":66,"tag":107,"props":2266,"children":2267},{"class":109,"line":216},[2268,2273,2278],{"type":66,"tag":107,"props":2269,"children":2270},{"style":114},[2271],{"type":71,"value":2272},"  type",{"type":66,"tag":107,"props":2274,"children":2275},{"style":126},[2276],{"type":71,"value":2277}," ChatMiddleware",{"type":66,"tag":107,"props":2279,"children":2280},{"style":120},[2281],{"type":71,"value":295},{"type":66,"tag":107,"props":2283,"children":2284},{"class":109,"line":251},[2285,2289,2293,2297,2301],{"type":66,"tag":107,"props":2286,"children":2287},{"style":120},[2288],{"type":71,"value":685},{"type":66,"tag":107,"props":2290,"children":2291},{"style":114},[2292],{"type":71,"value":149},{"type":66,"tag":107,"props":2294,"children":2295},{"style":120},[2296],{"type":71,"value":154},{"type":66,"tag":107,"props":2298,"children":2299},{"style":157},[2300],{"type":71,"value":160},{"type":66,"tag":107,"props":2302,"children":2303},{"style":120},[2304],{"type":71,"value":165},{"type":66,"tag":107,"props":2306,"children":2307},{"class":109,"line":298},[2308,2312,2316,2320,2324,2328,2332,2336],{"type":66,"tag":107,"props":2309,"children":2310},{"style":114},[2311],{"type":71,"value":117},{"type":66,"tag":107,"props":2313,"children":2314},{"style":120},[2315],{"type":71,"value":123},{"type":66,"tag":107,"props":2317,"children":2318},{"style":126},[2319],{"type":71,"value":182},{"type":66,"tag":107,"props":2321,"children":2322},{"style":120},[2323],{"type":71,"value":144},{"type":66,"tag":107,"props":2325,"children":2326},{"style":114},[2327],{"type":71,"value":149},{"type":66,"tag":107,"props":2329,"children":2330},{"style":120},[2331],{"type":71,"value":154},{"type":66,"tag":107,"props":2333,"children":2334},{"style":157},[2335],{"type":71,"value":199},{"type":66,"tag":107,"props":2337,"children":2338},{"style":120},[2339],{"type":71,"value":165},{"type":66,"tag":107,"props":2341,"children":2342},{"class":109,"line":311},[2343],{"type":66,"tag":107,"props":2344,"children":2345},{"emptyLinePlaceholder":210},[2346],{"type":71,"value":213},{"type":66,"tag":107,"props":2348,"children":2349},{"class":109,"line":329},[2350,2354,2359,2363,2367,2372],{"type":66,"tag":107,"props":2351,"children":2352},{"style":220},[2353],{"type":71,"value":223},{"type":66,"tag":107,"props":2355,"children":2356},{"style":126},[2357],{"type":71,"value":2358}," analytics",{"type":66,"tag":107,"props":2360,"children":2361},{"style":120},[2362],{"type":71,"value":263},{"type":66,"tag":107,"props":2364,"children":2365},{"style":1984},[2366],{"type":71,"value":2277},{"type":66,"tag":107,"props":2368,"children":2369},{"style":120},[2370],{"type":71,"value":2371}," =",{"type":66,"tag":107,"props":2373,"children":2374},{"style":120},[2375],{"type":71,"value":373},{"type":66,"tag":107,"props":2377,"children":2378},{"class":109,"line":338},[2379,2384,2388,2392,2397,2401],{"type":66,"tag":107,"props":2380,"children":2381},{"style":255},[2382],{"type":71,"value":2383},"  name",{"type":66,"tag":107,"props":2385,"children":2386},{"style":120},[2387],{"type":71,"value":263},{"type":66,"tag":107,"props":2389,"children":2390},{"style":120},[2391],{"type":71,"value":154},{"type":66,"tag":107,"props":2393,"children":2394},{"style":157},[2395],{"type":71,"value":2396},"analytics",{"type":66,"tag":107,"props":2398,"children":2399},{"style":120},[2400],{"type":71,"value":276},{"type":66,"tag":107,"props":2402,"children":2403},{"style":120},[2404],{"type":71,"value":295},{"type":66,"tag":107,"props":2406,"children":2407},{"class":109,"line":376},[2408,2413,2417,2421,2425,2429,2433],{"type":66,"tag":107,"props":2409,"children":2410},{"style":236},[2411],{"type":71,"value":2412},"  onStart",{"type":66,"tag":107,"props":2414,"children":2415},{"style":120},[2416],{"type":71,"value":263},{"type":66,"tag":107,"props":2418,"children":2419},{"style":120},[2420],{"type":71,"value":353},{"type":66,"tag":107,"props":2422,"children":2423},{"style":356},[2424],{"type":71,"value":359},{"type":66,"tag":107,"props":2426,"children":2427},{"style":120},[2428],{"type":71,"value":290},{"type":66,"tag":107,"props":2430,"children":2431},{"style":220},[2432],{"type":71,"value":368},{"type":66,"tag":107,"props":2434,"children":2435},{"style":120},[2436],{"type":71,"value":373},{"type":66,"tag":107,"props":2438,"children":2439},{"class":109,"line":435},[2440,2445,2449,2453,2457,2462,2467,2472,2476,2480,2484,2488,2493,2497,2501,2505,2509,2514],{"type":66,"tag":107,"props":2441,"children":2442},{"style":126},[2443],{"type":71,"value":2444},"    console",{"type":66,"tag":107,"props":2446,"children":2447},{"style":120},[2448],{"type":71,"value":387},{"type":66,"tag":107,"props":2450,"children":2451},{"style":236},[2452],{"type":71,"value":392},{"type":66,"tag":107,"props":2454,"children":2455},{"style":255},[2456],{"type":71,"value":243},{"type":66,"tag":107,"props":2458,"children":2459},{"style":120},[2460],{"type":71,"value":2461},"`",{"type":66,"tag":107,"props":2463,"children":2464},{"style":157},[2465],{"type":71,"value":2466},"[",{"type":66,"tag":107,"props":2468,"children":2469},{"style":120},[2470],{"type":71,"value":2471},"${",{"type":66,"tag":107,"props":2473,"children":2474},{"style":126},[2475],{"type":71,"value":359},{"type":66,"tag":107,"props":2477,"children":2478},{"style":120},[2479],{"type":71,"value":387},{"type":66,"tag":107,"props":2481,"children":2482},{"style":126},[2483],{"type":71,"value":740},{"type":66,"tag":107,"props":2485,"children":2486},{"style":120},[2487],{"type":71,"value":685},{"type":66,"tag":107,"props":2489,"children":2490},{"style":157},[2491],{"type":71,"value":2492},"] Chat started — model: ",{"type":66,"tag":107,"props":2494,"children":2495},{"style":120},[2496],{"type":71,"value":2471},{"type":66,"tag":107,"props":2498,"children":2499},{"style":126},[2500],{"type":71,"value":359},{"type":66,"tag":107,"props":2502,"children":2503},{"style":120},[2504],{"type":71,"value":387},{"type":66,"tag":107,"props":2506,"children":2507},{"style":126},[2508],{"type":71,"value":427},{"type":66,"tag":107,"props":2510,"children":2511},{"style":120},[2512],{"type":71,"value":2513},"}`",{"type":66,"tag":107,"props":2515,"children":2516},{"style":255},[2517],{"type":71,"value":432},{"type":66,"tag":107,"props":2519,"children":2520},{"class":109,"line":444},[2521],{"type":66,"tag":107,"props":2522,"children":2523},{"style":120},[2524],{"type":71,"value":2525},"  },\n",{"type":66,"tag":107,"props":2527,"children":2528},{"class":109,"line":486},[2529,2534,2538,2542,2546,2550,2555,2559,2563],{"type":66,"tag":107,"props":2530,"children":2531},{"style":236},[2532],{"type":71,"value":2533},"  onUsage",{"type":66,"tag":107,"props":2535,"children":2536},{"style":120},[2537],{"type":71,"value":263},{"type":66,"tag":107,"props":2539,"children":2540},{"style":120},[2541],{"type":71,"value":353},{"type":66,"tag":107,"props":2543,"children":2544},{"style":356},[2545],{"type":71,"value":359},{"type":66,"tag":107,"props":2547,"children":2548},{"style":120},[2549],{"type":71,"value":134},{"type":66,"tag":107,"props":2551,"children":2552},{"style":356},[2553],{"type":71,"value":2554}," usage",{"type":66,"tag":107,"props":2556,"children":2557},{"style":120},[2558],{"type":71,"value":290},{"type":66,"tag":107,"props":2560,"children":2561},{"style":220},[2562],{"type":71,"value":368},{"type":66,"tag":107,"props":2564,"children":2565},{"style":120},[2566],{"type":71,"value":373},{"type":66,"tag":107,"props":2568,"children":2569},{"class":109,"line":569},[2570,2574,2578,2582,2586,2590,2594,2598,2602,2606,2610,2614,2619,2623,2627,2631,2635,2639],{"type":66,"tag":107,"props":2571,"children":2572},{"style":126},[2573],{"type":71,"value":2444},{"type":66,"tag":107,"props":2575,"children":2576},{"style":120},[2577],{"type":71,"value":387},{"type":66,"tag":107,"props":2579,"children":2580},{"style":236},[2581],{"type":71,"value":392},{"type":66,"tag":107,"props":2583,"children":2584},{"style":255},[2585],{"type":71,"value":243},{"type":66,"tag":107,"props":2587,"children":2588},{"style":120},[2589],{"type":71,"value":2461},{"type":66,"tag":107,"props":2591,"children":2592},{"style":157},[2593],{"type":71,"value":2466},{"type":66,"tag":107,"props":2595,"children":2596},{"style":120},[2597],{"type":71,"value":2471},{"type":66,"tag":107,"props":2599,"children":2600},{"style":126},[2601],{"type":71,"value":359},{"type":66,"tag":107,"props":2603,"children":2604},{"style":120},[2605],{"type":71,"value":387},{"type":66,"tag":107,"props":2607,"children":2608},{"style":126},[2609],{"type":71,"value":740},{"type":66,"tag":107,"props":2611,"children":2612},{"style":120},[2613],{"type":71,"value":685},{"type":66,"tag":107,"props":2615,"children":2616},{"style":157},[2617],{"type":71,"value":2618},"] Tokens: ",{"type":66,"tag":107,"props":2620,"children":2621},{"style":120},[2622],{"type":71,"value":2471},{"type":66,"tag":107,"props":2624,"children":2625},{"style":126},[2626],{"type":71,"value":548},{"type":66,"tag":107,"props":2628,"children":2629},{"style":120},[2630],{"type":71,"value":387},{"type":66,"tag":107,"props":2632,"children":2633},{"style":126},[2634],{"type":71,"value":558},{"type":66,"tag":107,"props":2636,"children":2637},{"style":120},[2638],{"type":71,"value":2513},{"type":66,"tag":107,"props":2640,"children":2641},{"style":255},[2642],{"type":71,"value":432},{"type":66,"tag":107,"props":2644,"children":2645},{"class":109,"line":577},[2646],{"type":66,"tag":107,"props":2647,"children":2648},{"style":120},[2649],{"type":71,"value":2525},{"type":66,"tag":107,"props":2651,"children":2652},{"class":109,"line":618},[2653,2658,2662,2666,2670,2674,2678,2682,2686],{"type":66,"tag":107,"props":2654,"children":2655},{"style":236},[2656],{"type":71,"value":2657},"  onFinish",{"type":66,"tag":107,"props":2659,"children":2660},{"style":120},[2661],{"type":71,"value":263},{"type":66,"tag":107,"props":2663,"children":2664},{"style":120},[2665],{"type":71,"value":353},{"type":66,"tag":107,"props":2667,"children":2668},{"style":356},[2669],{"type":71,"value":359},{"type":66,"tag":107,"props":2671,"children":2672},{"style":120},[2673],{"type":71,"value":134},{"type":66,"tag":107,"props":2675,"children":2676},{"style":356},[2677],{"type":71,"value":471},{"type":66,"tag":107,"props":2679,"children":2680},{"style":120},[2681],{"type":71,"value":290},{"type":66,"tag":107,"props":2683,"children":2684},{"style":220},[2685],{"type":71,"value":368},{"type":66,"tag":107,"props":2687,"children":2688},{"style":120},[2689],{"type":71,"value":373},{"type":66,"tag":107,"props":2691,"children":2692},{"class":109,"line":649},[2693,2698,2702,2707],{"type":66,"tag":107,"props":2694,"children":2695},{"style":126},[2696],{"type":71,"value":2697},"    ctx",{"type":66,"tag":107,"props":2699,"children":2700},{"style":120},[2701],{"type":71,"value":387},{"type":66,"tag":107,"props":2703,"children":2704},{"style":236},[2705],{"type":71,"value":2706},"defer",{"type":66,"tag":107,"props":2708,"children":2709},{"style":255},[2710],{"type":71,"value":2711},"(\n",{"type":66,"tag":107,"props":2713,"children":2714},{"class":109,"line":657},[2715,2720,2724,2728,2733,2737,2741],{"type":66,"tag":107,"props":2716,"children":2717},{"style":236},[2718],{"type":71,"value":2719},"      fetch",{"type":66,"tag":107,"props":2721,"children":2722},{"style":255},[2723],{"type":71,"value":243},{"type":66,"tag":107,"props":2725,"children":2726},{"style":120},[2727],{"type":71,"value":276},{"type":66,"tag":107,"props":2729,"children":2730},{"style":157},[2731],{"type":71,"value":2732},"\u002Fapi\u002Fanalytics",{"type":66,"tag":107,"props":2734,"children":2735},{"style":120},[2736],{"type":71,"value":276},{"type":66,"tag":107,"props":2738,"children":2739},{"style":120},[2740],{"type":71,"value":134},{"type":66,"tag":107,"props":2742,"children":2743},{"style":120},[2744],{"type":71,"value":373},{"type":66,"tag":107,"props":2746,"children":2747},{"class":109,"line":666},[2748,2753,2757,2761,2766,2770],{"type":66,"tag":107,"props":2749,"children":2750},{"style":255},[2751],{"type":71,"value":2752},"        method",{"type":66,"tag":107,"props":2754,"children":2755},{"style":120},[2756],{"type":71,"value":263},{"type":66,"tag":107,"props":2758,"children":2759},{"style":120},[2760],{"type":71,"value":154},{"type":66,"tag":107,"props":2762,"children":2763},{"style":157},[2764],{"type":71,"value":2765},"POST",{"type":66,"tag":107,"props":2767,"children":2768},{"style":120},[2769],{"type":71,"value":276},{"type":66,"tag":107,"props":2771,"children":2772},{"style":120},[2773],{"type":71,"value":295},{"type":66,"tag":107,"props":2775,"children":2776},{"class":109,"line":679},[2777,2782,2786,2791,2795,2800,2804],{"type":66,"tag":107,"props":2778,"children":2779},{"style":255},[2780],{"type":71,"value":2781},"        body",{"type":66,"tag":107,"props":2783,"children":2784},{"style":120},[2785],{"type":71,"value":263},{"type":66,"tag":107,"props":2787,"children":2788},{"style":126},[2789],{"type":71,"value":2790}," JSON",{"type":66,"tag":107,"props":2792,"children":2793},{"style":120},[2794],{"type":71,"value":387},{"type":66,"tag":107,"props":2796,"children":2797},{"style":236},[2798],{"type":71,"value":2799},"stringify",{"type":66,"tag":107,"props":2801,"children":2802},{"style":255},[2803],{"type":71,"value":243},{"type":66,"tag":107,"props":2805,"children":2806},{"style":120},[2807],{"type":71,"value":248},{"type":66,"tag":107,"props":2809,"children":2810},{"class":109,"line":692},[2811,2816,2820,2824,2828,2832],{"type":66,"tag":107,"props":2812,"children":2813},{"style":255},[2814],{"type":71,"value":2815},"          requestId",{"type":66,"tag":107,"props":2817,"children":2818},{"style":120},[2819],{"type":71,"value":263},{"type":66,"tag":107,"props":2821,"children":2822},{"style":126},[2823],{"type":71,"value":418},{"type":66,"tag":107,"props":2825,"children":2826},{"style":120},[2827],{"type":71,"value":387},{"type":66,"tag":107,"props":2829,"children":2830},{"style":126},[2831],{"type":71,"value":740},{"type":66,"tag":107,"props":2833,"children":2834},{"style":120},[2835],{"type":71,"value":295},{"type":66,"tag":107,"props":2837,"children":2838},{"class":109,"line":700},[2839,2844,2848,2852,2856,2860],{"type":66,"tag":107,"props":2840,"children":2841},{"style":255},[2842],{"type":71,"value":2843},"          model",{"type":66,"tag":107,"props":2845,"children":2846},{"style":120},[2847],{"type":71,"value":263},{"type":66,"tag":107,"props":2849,"children":2850},{"style":126},[2851],{"type":71,"value":418},{"type":66,"tag":107,"props":2853,"children":2854},{"style":120},[2855],{"type":71,"value":387},{"type":66,"tag":107,"props":2857,"children":2858},{"style":126},[2859],{"type":71,"value":427},{"type":66,"tag":107,"props":2861,"children":2862},{"style":120},[2863],{"type":71,"value":295},{"type":66,"tag":107,"props":2865,"children":2867},{"class":109,"line":2866},23,[2868,2873,2877,2881,2885,2890],{"type":66,"tag":107,"props":2869,"children":2870},{"style":255},[2871],{"type":71,"value":2872},"          duration",{"type":66,"tag":107,"props":2874,"children":2875},{"style":120},[2876],{"type":71,"value":263},{"type":66,"tag":107,"props":2878,"children":2879},{"style":126},[2880],{"type":71,"value":471},{"type":66,"tag":107,"props":2882,"children":2883},{"style":120},[2884],{"type":71,"value":387},{"type":66,"tag":107,"props":2886,"children":2887},{"style":126},[2888],{"type":71,"value":2889},"duration",{"type":66,"tag":107,"props":2891,"children":2892},{"style":120},[2893],{"type":71,"value":295},{"type":66,"tag":107,"props":2895,"children":2897},{"class":109,"line":2896},24,[2898,2903,2907,2911,2915,2919,2923,2927],{"type":66,"tag":107,"props":2899,"children":2900},{"style":255},[2901],{"type":71,"value":2902},"          tokens",{"type":66,"tag":107,"props":2904,"children":2905},{"style":120},[2906],{"type":71,"value":263},{"type":66,"tag":107,"props":2908,"children":2909},{"style":126},[2910],{"type":71,"value":471},{"type":66,"tag":107,"props":2912,"children":2913},{"style":120},[2914],{"type":71,"value":387},{"type":66,"tag":107,"props":2916,"children":2917},{"style":126},[2918],{"type":71,"value":548},{"type":66,"tag":107,"props":2920,"children":2921},{"style":120},[2922],{"type":71,"value":553},{"type":66,"tag":107,"props":2924,"children":2925},{"style":126},[2926],{"type":71,"value":558},{"type":66,"tag":107,"props":2928,"children":2929},{"style":120},[2930],{"type":71,"value":295},{"type":66,"tag":107,"props":2932,"children":2934},{"class":109,"line":2933},25,[2935,2940,2944,2948,2952,2957],{"type":66,"tag":107,"props":2936,"children":2937},{"style":255},[2938],{"type":71,"value":2939},"          finishReason",{"type":66,"tag":107,"props":2941,"children":2942},{"style":120},[2943],{"type":71,"value":263},{"type":66,"tag":107,"props":2945,"children":2946},{"style":126},[2947],{"type":71,"value":471},{"type":66,"tag":107,"props":2949,"children":2950},{"style":120},[2951],{"type":71,"value":387},{"type":66,"tag":107,"props":2953,"children":2954},{"style":126},[2955],{"type":71,"value":2956},"finishReason",{"type":66,"tag":107,"props":2958,"children":2959},{"style":120},[2960],{"type":71,"value":295},{"type":66,"tag":107,"props":2962,"children":2964},{"class":109,"line":2963},26,[2965,2970,2974],{"type":66,"tag":107,"props":2966,"children":2967},{"style":120},[2968],{"type":71,"value":2969},"        }",{"type":66,"tag":107,"props":2971,"children":2972},{"style":255},[2973],{"type":71,"value":290},{"type":66,"tag":107,"props":2975,"children":2976},{"style":120},[2977],{"type":71,"value":295},{"type":66,"tag":107,"props":2979,"children":2981},{"class":109,"line":2980},27,[2982,2987,2991],{"type":66,"tag":107,"props":2983,"children":2984},{"style":120},[2985],{"type":71,"value":2986},"      }",{"type":66,"tag":107,"props":2988,"children":2989},{"style":255},[2990],{"type":71,"value":290},{"type":66,"tag":107,"props":2992,"children":2993},{"style":120},[2994],{"type":71,"value":295},{"type":66,"tag":107,"props":2996,"children":2998},{"class":109,"line":2997},28,[2999],{"type":66,"tag":107,"props":3000,"children":3001},{"style":255},[3002],{"type":71,"value":3003},"    )\n",{"type":66,"tag":107,"props":3005,"children":3007},{"class":109,"line":3006},29,[3008],{"type":66,"tag":107,"props":3009,"children":3010},{"style":120},[3011],{"type":71,"value":2525},{"type":66,"tag":107,"props":3013,"children":3015},{"class":109,"line":3014},30,[3016,3021,3025,3029,3033,3037,3041,3045,3049],{"type":66,"tag":107,"props":3017,"children":3018},{"style":236},[3019],{"type":71,"value":3020},"  onError",{"type":66,"tag":107,"props":3022,"children":3023},{"style":120},[3024],{"type":71,"value":263},{"type":66,"tag":107,"props":3026,"children":3027},{"style":120},[3028],{"type":71,"value":353},{"type":66,"tag":107,"props":3030,"children":3031},{"style":356},[3032],{"type":71,"value":359},{"type":66,"tag":107,"props":3034,"children":3035},{"style":120},[3036],{"type":71,"value":134},{"type":66,"tag":107,"props":3038,"children":3039},{"style":356},[3040],{"type":71,"value":471},{"type":66,"tag":107,"props":3042,"children":3043},{"style":120},[3044],{"type":71,"value":290},{"type":66,"tag":107,"props":3046,"children":3047},{"style":220},[3048],{"type":71,"value":368},{"type":66,"tag":107,"props":3050,"children":3051},{"style":120},[3052],{"type":71,"value":373},{"type":66,"tag":107,"props":3054,"children":3056},{"class":109,"line":3055},31,[3057,3061,3065,3069],{"type":66,"tag":107,"props":3058,"children":3059},{"style":126},[3060],{"type":71,"value":2697},{"type":66,"tag":107,"props":3062,"children":3063},{"style":120},[3064],{"type":71,"value":387},{"type":66,"tag":107,"props":3066,"children":3067},{"style":236},[3068],{"type":71,"value":2706},{"type":66,"tag":107,"props":3070,"children":3071},{"style":255},[3072],{"type":71,"value":2711},{"type":66,"tag":107,"props":3074,"children":3076},{"class":109,"line":3075},32,[3077,3081,3085,3089,3094,3098,3102],{"type":66,"tag":107,"props":3078,"children":3079},{"style":236},[3080],{"type":71,"value":2719},{"type":66,"tag":107,"props":3082,"children":3083},{"style":255},[3084],{"type":71,"value":243},{"type":66,"tag":107,"props":3086,"children":3087},{"style":120},[3088],{"type":71,"value":276},{"type":66,"tag":107,"props":3090,"children":3091},{"style":157},[3092],{"type":71,"value":3093},"\u002Fapi\u002Ferrors",{"type":66,"tag":107,"props":3095,"children":3096},{"style":120},[3097],{"type":71,"value":276},{"type":66,"tag":107,"props":3099,"children":3100},{"style":120},[3101],{"type":71,"value":134},{"type":66,"tag":107,"props":3103,"children":3104},{"style":120},[3105],{"type":71,"value":373},{"type":66,"tag":107,"props":3107,"children":3109},{"class":109,"line":3108},33,[3110,3114,3118,3122,3126,3130],{"type":66,"tag":107,"props":3111,"children":3112},{"style":255},[3113],{"type":71,"value":2752},{"type":66,"tag":107,"props":3115,"children":3116},{"style":120},[3117],{"type":71,"value":263},{"type":66,"tag":107,"props":3119,"children":3120},{"style":120},[3121],{"type":71,"value":154},{"type":66,"tag":107,"props":3123,"children":3124},{"style":157},[3125],{"type":71,"value":2765},{"type":66,"tag":107,"props":3127,"children":3128},{"style":120},[3129],{"type":71,"value":276},{"type":66,"tag":107,"props":3131,"children":3132},{"style":120},[3133],{"type":71,"value":295},{"type":66,"tag":107,"props":3135,"children":3137},{"class":109,"line":3136},34,[3138,3142,3146,3150,3154,3158,3162],{"type":66,"tag":107,"props":3139,"children":3140},{"style":255},[3141],{"type":71,"value":2781},{"type":66,"tag":107,"props":3143,"children":3144},{"style":120},[3145],{"type":71,"value":263},{"type":66,"tag":107,"props":3147,"children":3148},{"style":126},[3149],{"type":71,"value":2790},{"type":66,"tag":107,"props":3151,"children":3152},{"style":120},[3153],{"type":71,"value":387},{"type":66,"tag":107,"props":3155,"children":3156},{"style":236},[3157],{"type":71,"value":2799},{"type":66,"tag":107,"props":3159,"children":3160},{"style":255},[3161],{"type":71,"value":243},{"type":66,"tag":107,"props":3163,"children":3164},{"style":120},[3165],{"type":71,"value":248},{"type":66,"tag":107,"props":3167,"children":3169},{"class":109,"line":3168},35,[3170,3174,3178,3182,3186,3190],{"type":66,"tag":107,"props":3171,"children":3172},{"style":255},[3173],{"type":71,"value":2815},{"type":66,"tag":107,"props":3175,"children":3176},{"style":120},[3177],{"type":71,"value":263},{"type":66,"tag":107,"props":3179,"children":3180},{"style":126},[3181],{"type":71,"value":418},{"type":66,"tag":107,"props":3183,"children":3184},{"style":120},[3185],{"type":71,"value":387},{"type":66,"tag":107,"props":3187,"children":3188},{"style":126},[3189],{"type":71,"value":740},{"type":66,"tag":107,"props":3191,"children":3192},{"style":120},[3193],{"type":71,"value":295},{"type":66,"tag":107,"props":3195,"children":3197},{"class":109,"line":3196},36,[3198,3203,3207,3212,3216,3220,3224,3228,3232],{"type":66,"tag":107,"props":3199,"children":3200},{"style":255},[3201],{"type":71,"value":3202},"          error",{"type":66,"tag":107,"props":3204,"children":3205},{"style":120},[3206],{"type":71,"value":263},{"type":66,"tag":107,"props":3208,"children":3209},{"style":236},[3210],{"type":71,"value":3211}," String",{"type":66,"tag":107,"props":3213,"children":3214},{"style":255},[3215],{"type":71,"value":243},{"type":66,"tag":107,"props":3217,"children":3218},{"style":126},[3219],{"type":71,"value":633},{"type":66,"tag":107,"props":3221,"children":3222},{"style":120},[3223],{"type":71,"value":387},{"type":66,"tag":107,"props":3225,"children":3226},{"style":126},[3227],{"type":71,"value":642},{"type":66,"tag":107,"props":3229,"children":3230},{"style":255},[3231],{"type":71,"value":290},{"type":66,"tag":107,"props":3233,"children":3234},{"style":120},[3235],{"type":71,"value":295},{"type":66,"tag":107,"props":3237,"children":3239},{"class":109,"line":3238},37,[3240,3244,3248,3252,3256,3260],{"type":66,"tag":107,"props":3241,"children":3242},{"style":255},[3243],{"type":71,"value":2872},{"type":66,"tag":107,"props":3245,"children":3246},{"style":120},[3247],{"type":71,"value":263},{"type":66,"tag":107,"props":3249,"children":3250},{"style":126},[3251],{"type":71,"value":471},{"type":66,"tag":107,"props":3253,"children":3254},{"style":120},[3255],{"type":71,"value":387},{"type":66,"tag":107,"props":3257,"children":3258},{"style":126},[3259],{"type":71,"value":2889},{"type":66,"tag":107,"props":3261,"children":3262},{"style":120},[3263],{"type":71,"value":295},{"type":66,"tag":107,"props":3265,"children":3267},{"class":109,"line":3266},38,[3268,3272,3276],{"type":66,"tag":107,"props":3269,"children":3270},{"style":120},[3271],{"type":71,"value":2969},{"type":66,"tag":107,"props":3273,"children":3274},{"style":255},[3275],{"type":71,"value":290},{"type":66,"tag":107,"props":3277,"children":3278},{"style":120},[3279],{"type":71,"value":295},{"type":66,"tag":107,"props":3281,"children":3283},{"class":109,"line":3282},39,[3284,3288,3292],{"type":66,"tag":107,"props":3285,"children":3286},{"style":120},[3287],{"type":71,"value":2986},{"type":66,"tag":107,"props":3289,"children":3290},{"style":255},[3291],{"type":71,"value":290},{"type":66,"tag":107,"props":3293,"children":3294},{"style":120},[3295],{"type":71,"value":295},{"type":66,"tag":107,"props":3297,"children":3299},{"class":109,"line":3298},40,[3300],{"type":66,"tag":107,"props":3301,"children":3302},{"style":255},[3303],{"type":71,"value":3003},{"type":66,"tag":107,"props":3305,"children":3307},{"class":109,"line":3306},41,[3308],{"type":66,"tag":107,"props":3309,"children":3310},{"style":120},[3311],{"type":71,"value":2525},{"type":66,"tag":107,"props":3313,"children":3315},{"class":109,"line":3314},42,[3316],{"type":66,"tag":107,"props":3317,"children":3318},{"style":120},[3319],{"type":71,"value":2092},{"type":66,"tag":107,"props":3321,"children":3323},{"class":109,"line":3322},43,[3324],{"type":66,"tag":107,"props":3325,"children":3326},{"emptyLinePlaceholder":210},[3327],{"type":71,"value":213},{"type":66,"tag":107,"props":3329,"children":3331},{"class":109,"line":3330},44,[3332,3336,3340,3344,3348,3352],{"type":66,"tag":107,"props":3333,"children":3334},{"style":220},[3335],{"type":71,"value":223},{"type":66,"tag":107,"props":3337,"children":3338},{"style":126},[3339],{"type":71,"value":228},{"type":66,"tag":107,"props":3341,"children":3342},{"style":120},[3343],{"type":71,"value":233},{"type":66,"tag":107,"props":3345,"children":3346},{"style":236},[3347],{"type":71,"value":129},{"type":66,"tag":107,"props":3349,"children":3350},{"style":126},[3351],{"type":71,"value":243},{"type":66,"tag":107,"props":3353,"children":3354},{"style":120},[3355],{"type":71,"value":248},{"type":66,"tag":107,"props":3357,"children":3359},{"class":109,"line":3358},45,[3360,3364,3368,3372,3376,3380,3384,3388,3392],{"type":66,"tag":107,"props":3361,"children":3362},{"style":255},[3363],{"type":71,"value":258},{"type":66,"tag":107,"props":3365,"children":3366},{"style":120},[3367],{"type":71,"value":263},{"type":66,"tag":107,"props":3369,"children":3370},{"style":236},[3371],{"type":71,"value":182},{"type":66,"tag":107,"props":3373,"children":3374},{"style":126},[3375],{"type":71,"value":243},{"type":66,"tag":107,"props":3377,"children":3378},{"style":120},[3379],{"type":71,"value":276},{"type":66,"tag":107,"props":3381,"children":3382},{"style":157},[3383],{"type":71,"value":281},{"type":66,"tag":107,"props":3385,"children":3386},{"style":120},[3387],{"type":71,"value":276},{"type":66,"tag":107,"props":3389,"children":3390},{"style":126},[3391],{"type":71,"value":290},{"type":66,"tag":107,"props":3393,"children":3394},{"style":120},[3395],{"type":71,"value":295},{"type":66,"tag":107,"props":3397,"children":3399},{"class":109,"line":3398},46,[3400,3404],{"type":66,"tag":107,"props":3401,"children":3402},{"style":126},[3403],{"type":71,"value":304},{"type":66,"tag":107,"props":3405,"children":3406},{"style":120},[3407],{"type":71,"value":295},{"type":66,"tag":107,"props":3409,"children":3411},{"class":109,"line":3410},47,[3412,3416,3420,3425],{"type":66,"tag":107,"props":3413,"children":3414},{"style":255},[3415],{"type":71,"value":317},{"type":66,"tag":107,"props":3417,"children":3418},{"style":120},[3419],{"type":71,"value":263},{"type":66,"tag":107,"props":3421,"children":3422},{"style":126},[3423],{"type":71,"value":3424}," [analytics]",{"type":66,"tag":107,"props":3426,"children":3427},{"style":120},[3428],{"type":71,"value":295},{"type":66,"tag":107,"props":3430,"children":3432},{"class":109,"line":3431},48,[3433,3437],{"type":66,"tag":107,"props":3434,"children":3435},{"style":120},[3436],{"type":71,"value":685},{"type":66,"tag":107,"props":3438,"children":3439},{"style":126},[3440],{"type":71,"value":432},{"type":66,"tag":107,"props":3442,"children":3444},{"class":109,"line":3443},49,[3445],{"type":66,"tag":107,"props":3446,"children":3447},{"emptyLinePlaceholder":210},[3448],{"type":71,"value":213},{"type":66,"tag":107,"props":3450,"children":3452},{"class":109,"line":3451},50,[3453,3457,3461],{"type":66,"tag":107,"props":3454,"children":3455},{"style":114},[3456],{"type":71,"value":706},{"type":66,"tag":107,"props":3458,"children":3459},{"style":236},[3460],{"type":71,"value":139},{"type":66,"tag":107,"props":3462,"children":3463},{"style":126},[3464],{"type":71,"value":715},{"type":66,"tag":1379,"props":3466,"children":3468},{"id":3467},"pattern-2-tool-interception-middleware",[3469],{"type":71,"value":3470},"Pattern 2: Tool Interception Middleware",{"type":66,"tag":77,"props":3472,"children":3473},{},[3474,3475,3480,3482,3487,3489,3494],{"type":71,"value":2155},{"type":66,"tag":103,"props":3476,"children":3478},{"className":3477},[],[3479],{"type":71,"value":1049},{"type":71,"value":3481}," to validate, gate, or transform tool arguments before execution.\nUse ",{"type":66,"tag":103,"props":3483,"children":3485},{"className":3484},[],[3486],{"type":71,"value":1077},{"type":71,"value":3488}," to log results and timing. The first middleware that returns a\nnon-void decision from ",{"type":66,"tag":103,"props":3490,"children":3492},{"className":3491},[],[3493],{"type":71,"value":1049},{"type":71,"value":3495}," short-circuits remaining middleware for that call.",{"type":66,"tag":96,"props":3497,"children":3499},{"className":98,"code":3498,"language":45,"meta":100,"style":100},"import type { ChatMiddleware } from '@tanstack\u002Fai'\n\nconst toolGuard: ChatMiddleware = {\n  name: 'tool-guard',\n  onBeforeToolCall: (ctx, hookCtx) => {\n    \u002F\u002F Block dangerous tools\n    if (hookCtx.toolName === 'deleteDatabase') {\n      return { type: 'abort', reason: 'Dangerous operation blocked' }\n    }\n\n    \u002F\u002F Enforce default arguments\n    if (hookCtx.toolName === 'search' && !hookCtx.args.limit) {\n      return {\n        type: 'transformArgs',\n        args: { ...hookCtx.args, limit: 10 },\n      }\n    }\n\n    \u002F\u002F Return void to continue normally\n  },\n  onAfterToolCall: (ctx, info) => {\n    if (info.ok) {\n      console.log(`${info.toolName} completed in ${info.duration}ms`)\n    } else {\n      console.error(`${info.toolName} failed:`, info.error)\n    }\n  },\n}\n",[3500],{"type":66,"tag":103,"props":3501,"children":3502},{"__ignoreMap":100},[3503,3543,3550,3578,3606,3647,3655,3707,3771,3779,3786,3794,3874,3885,3914,3971,3979,3986,3993,4001,4008,4048,4080,4159,4176,4244,4251,4258],{"type":66,"tag":107,"props":3504,"children":3505},{"class":109,"line":110},[3506,3510,3515,3519,3523,3527,3531,3535,3539],{"type":66,"tag":107,"props":3507,"children":3508},{"style":114},[3509],{"type":71,"value":117},{"type":66,"tag":107,"props":3511,"children":3512},{"style":114},[3513],{"type":71,"value":3514}," type",{"type":66,"tag":107,"props":3516,"children":3517},{"style":120},[3518],{"type":71,"value":123},{"type":66,"tag":107,"props":3520,"children":3521},{"style":126},[3522],{"type":71,"value":2277},{"type":66,"tag":107,"props":3524,"children":3525},{"style":120},[3526],{"type":71,"value":144},{"type":66,"tag":107,"props":3528,"children":3529},{"style":114},[3530],{"type":71,"value":149},{"type":66,"tag":107,"props":3532,"children":3533},{"style":120},[3534],{"type":71,"value":154},{"type":66,"tag":107,"props":3536,"children":3537},{"style":157},[3538],{"type":71,"value":160},{"type":66,"tag":107,"props":3540,"children":3541},{"style":120},[3542],{"type":71,"value":165},{"type":66,"tag":107,"props":3544,"children":3545},{"class":109,"line":168},[3546],{"type":66,"tag":107,"props":3547,"children":3548},{"emptyLinePlaceholder":210},[3549],{"type":71,"value":213},{"type":66,"tag":107,"props":3551,"children":3552},{"class":109,"line":206},[3553,3557,3562,3566,3570,3574],{"type":66,"tag":107,"props":3554,"children":3555},{"style":220},[3556],{"type":71,"value":223},{"type":66,"tag":107,"props":3558,"children":3559},{"style":126},[3560],{"type":71,"value":3561}," toolGuard",{"type":66,"tag":107,"props":3563,"children":3564},{"style":120},[3565],{"type":71,"value":263},{"type":66,"tag":107,"props":3567,"children":3568},{"style":1984},[3569],{"type":71,"value":2277},{"type":66,"tag":107,"props":3571,"children":3572},{"style":120},[3573],{"type":71,"value":2371},{"type":66,"tag":107,"props":3575,"children":3576},{"style":120},[3577],{"type":71,"value":373},{"type":66,"tag":107,"props":3579,"children":3580},{"class":109,"line":216},[3581,3585,3589,3593,3598,3602],{"type":66,"tag":107,"props":3582,"children":3583},{"style":255},[3584],{"type":71,"value":2383},{"type":66,"tag":107,"props":3586,"children":3587},{"style":120},[3588],{"type":71,"value":263},{"type":66,"tag":107,"props":3590,"children":3591},{"style":120},[3592],{"type":71,"value":154},{"type":66,"tag":107,"props":3594,"children":3595},{"style":157},[3596],{"type":71,"value":3597},"tool-guard",{"type":66,"tag":107,"props":3599,"children":3600},{"style":120},[3601],{"type":71,"value":276},{"type":66,"tag":107,"props":3603,"children":3604},{"style":120},[3605],{"type":71,"value":295},{"type":66,"tag":107,"props":3607,"children":3608},{"class":109,"line":251},[3609,3614,3618,3622,3626,3630,3635,3639,3643],{"type":66,"tag":107,"props":3610,"children":3611},{"style":236},[3612],{"type":71,"value":3613},"  onBeforeToolCall",{"type":66,"tag":107,"props":3615,"children":3616},{"style":120},[3617],{"type":71,"value":263},{"type":66,"tag":107,"props":3619,"children":3620},{"style":120},[3621],{"type":71,"value":353},{"type":66,"tag":107,"props":3623,"children":3624},{"style":356},[3625],{"type":71,"value":359},{"type":66,"tag":107,"props":3627,"children":3628},{"style":120},[3629],{"type":71,"value":134},{"type":66,"tag":107,"props":3631,"children":3632},{"style":356},[3633],{"type":71,"value":3634}," hookCtx",{"type":66,"tag":107,"props":3636,"children":3637},{"style":120},[3638],{"type":71,"value":290},{"type":66,"tag":107,"props":3640,"children":3641},{"style":220},[3642],{"type":71,"value":368},{"type":66,"tag":107,"props":3644,"children":3645},{"style":120},[3646],{"type":71,"value":373},{"type":66,"tag":107,"props":3648,"children":3649},{"class":109,"line":298},[3650],{"type":66,"tag":107,"props":3651,"children":3652},{"style":2081},[3653],{"type":71,"value":3654},"    \u002F\u002F Block dangerous tools\n",{"type":66,"tag":107,"props":3656,"children":3657},{"class":109,"line":311},[3658,3663,3667,3672,3676,3681,3686,3690,3695,3699,3703],{"type":66,"tag":107,"props":3659,"children":3660},{"style":114},[3661],{"type":71,"value":3662},"    if",{"type":66,"tag":107,"props":3664,"children":3665},{"style":255},[3666],{"type":71,"value":353},{"type":66,"tag":107,"props":3668,"children":3669},{"style":126},[3670],{"type":71,"value":3671},"hookCtx",{"type":66,"tag":107,"props":3673,"children":3674},{"style":120},[3675],{"type":71,"value":387},{"type":66,"tag":107,"props":3677,"children":3678},{"style":126},[3679],{"type":71,"value":3680},"toolName",{"type":66,"tag":107,"props":3682,"children":3683},{"style":120},[3684],{"type":71,"value":3685}," ===",{"type":66,"tag":107,"props":3687,"children":3688},{"style":120},[3689],{"type":71,"value":154},{"type":66,"tag":107,"props":3691,"children":3692},{"style":157},[3693],{"type":71,"value":3694},"deleteDatabase",{"type":66,"tag":107,"props":3696,"children":3697},{"style":120},[3698],{"type":71,"value":276},{"type":66,"tag":107,"props":3700,"children":3701},{"style":255},[3702],{"type":71,"value":1918},{"type":66,"tag":107,"props":3704,"children":3705},{"style":120},[3706],{"type":71,"value":248},{"type":66,"tag":107,"props":3708,"children":3709},{"class":109,"line":329},[3710,3715,3719,3723,3727,3731,3736,3740,3744,3749,3753,3757,3762,3766],{"type":66,"tag":107,"props":3711,"children":3712},{"style":114},[3713],{"type":71,"value":3714},"      return",{"type":66,"tag":107,"props":3716,"children":3717},{"style":120},[3718],{"type":71,"value":123},{"type":66,"tag":107,"props":3720,"children":3721},{"style":255},[3722],{"type":71,"value":3514},{"type":66,"tag":107,"props":3724,"children":3725},{"style":120},[3726],{"type":71,"value":263},{"type":66,"tag":107,"props":3728,"children":3729},{"style":120},[3730],{"type":71,"value":154},{"type":66,"tag":107,"props":3732,"children":3733},{"style":157},[3734],{"type":71,"value":3735},"abort",{"type":66,"tag":107,"props":3737,"children":3738},{"style":120},[3739],{"type":71,"value":276},{"type":66,"tag":107,"props":3741,"children":3742},{"style":120},[3743],{"type":71,"value":134},{"type":66,"tag":107,"props":3745,"children":3746},{"style":255},[3747],{"type":71,"value":3748}," reason",{"type":66,"tag":107,"props":3750,"children":3751},{"style":120},[3752],{"type":71,"value":263},{"type":66,"tag":107,"props":3754,"children":3755},{"style":120},[3756],{"type":71,"value":154},{"type":66,"tag":107,"props":3758,"children":3759},{"style":157},[3760],{"type":71,"value":3761},"Dangerous operation blocked",{"type":66,"tag":107,"props":3763,"children":3764},{"style":120},[3765],{"type":71,"value":276},{"type":66,"tag":107,"props":3767,"children":3768},{"style":120},[3769],{"type":71,"value":3770}," }\n",{"type":66,"tag":107,"props":3772,"children":3773},{"class":109,"line":338},[3774],{"type":66,"tag":107,"props":3775,"children":3776},{"style":120},[3777],{"type":71,"value":3778},"    }\n",{"type":66,"tag":107,"props":3780,"children":3781},{"class":109,"line":376},[3782],{"type":66,"tag":107,"props":3783,"children":3784},{"emptyLinePlaceholder":210},[3785],{"type":71,"value":213},{"type":66,"tag":107,"props":3787,"children":3788},{"class":109,"line":435},[3789],{"type":66,"tag":107,"props":3790,"children":3791},{"style":2081},[3792],{"type":71,"value":3793},"    \u002F\u002F Enforce default arguments\n",{"type":66,"tag":107,"props":3795,"children":3796},{"class":109,"line":444},[3797,3801,3805,3809,3813,3817,3821,3825,3830,3834,3839,3844,3848,3852,3857,3861,3866,3870],{"type":66,"tag":107,"props":3798,"children":3799},{"style":114},[3800],{"type":71,"value":3662},{"type":66,"tag":107,"props":3802,"children":3803},{"style":255},[3804],{"type":71,"value":353},{"type":66,"tag":107,"props":3806,"children":3807},{"style":126},[3808],{"type":71,"value":3671},{"type":66,"tag":107,"props":3810,"children":3811},{"style":120},[3812],{"type":71,"value":387},{"type":66,"tag":107,"props":3814,"children":3815},{"style":126},[3816],{"type":71,"value":3680},{"type":66,"tag":107,"props":3818,"children":3819},{"style":120},[3820],{"type":71,"value":3685},{"type":66,"tag":107,"props":3822,"children":3823},{"style":120},[3824],{"type":71,"value":154},{"type":66,"tag":107,"props":3826,"children":3827},{"style":157},[3828],{"type":71,"value":3829},"search",{"type":66,"tag":107,"props":3831,"children":3832},{"style":120},[3833],{"type":71,"value":276},{"type":66,"tag":107,"props":3835,"children":3836},{"style":120},[3837],{"type":71,"value":3838}," &&",{"type":66,"tag":107,"props":3840,"children":3841},{"style":120},[3842],{"type":71,"value":3843}," !",{"type":66,"tag":107,"props":3845,"children":3846},{"style":126},[3847],{"type":71,"value":3671},{"type":66,"tag":107,"props":3849,"children":3850},{"style":120},[3851],{"type":71,"value":387},{"type":66,"tag":107,"props":3853,"children":3854},{"style":126},[3855],{"type":71,"value":3856},"args",{"type":66,"tag":107,"props":3858,"children":3859},{"style":120},[3860],{"type":71,"value":387},{"type":66,"tag":107,"props":3862,"children":3863},{"style":126},[3864],{"type":71,"value":3865},"limit",{"type":66,"tag":107,"props":3867,"children":3868},{"style":255},[3869],{"type":71,"value":1918},{"type":66,"tag":107,"props":3871,"children":3872},{"style":120},[3873],{"type":71,"value":248},{"type":66,"tag":107,"props":3875,"children":3876},{"class":109,"line":486},[3877,3881],{"type":66,"tag":107,"props":3878,"children":3879},{"style":114},[3880],{"type":71,"value":3714},{"type":66,"tag":107,"props":3882,"children":3883},{"style":120},[3884],{"type":71,"value":373},{"type":66,"tag":107,"props":3886,"children":3887},{"class":109,"line":569},[3888,3893,3897,3901,3906,3910],{"type":66,"tag":107,"props":3889,"children":3890},{"style":255},[3891],{"type":71,"value":3892},"        type",{"type":66,"tag":107,"props":3894,"children":3895},{"style":120},[3896],{"type":71,"value":263},{"type":66,"tag":107,"props":3898,"children":3899},{"style":120},[3900],{"type":71,"value":154},{"type":66,"tag":107,"props":3902,"children":3903},{"style":157},[3904],{"type":71,"value":3905},"transformArgs",{"type":66,"tag":107,"props":3907,"children":3908},{"style":120},[3909],{"type":71,"value":276},{"type":66,"tag":107,"props":3911,"children":3912},{"style":120},[3913],{"type":71,"value":295},{"type":66,"tag":107,"props":3915,"children":3916},{"class":109,"line":577},[3917,3922,3926,3930,3935,3939,3943,3947,3951,3956,3960,3966],{"type":66,"tag":107,"props":3918,"children":3919},{"style":255},[3920],{"type":71,"value":3921},"        args",{"type":66,"tag":107,"props":3923,"children":3924},{"style":120},[3925],{"type":71,"value":263},{"type":66,"tag":107,"props":3927,"children":3928},{"style":120},[3929],{"type":71,"value":123},{"type":66,"tag":107,"props":3931,"children":3932},{"style":120},[3933],{"type":71,"value":3934}," ...",{"type":66,"tag":107,"props":3936,"children":3937},{"style":126},[3938],{"type":71,"value":3671},{"type":66,"tag":107,"props":3940,"children":3941},{"style":120},[3942],{"type":71,"value":387},{"type":66,"tag":107,"props":3944,"children":3945},{"style":126},[3946],{"type":71,"value":3856},{"type":66,"tag":107,"props":3948,"children":3949},{"style":120},[3950],{"type":71,"value":134},{"type":66,"tag":107,"props":3952,"children":3953},{"style":255},[3954],{"type":71,"value":3955}," limit",{"type":66,"tag":107,"props":3957,"children":3958},{"style":120},[3959],{"type":71,"value":263},{"type":66,"tag":107,"props":3961,"children":3963},{"style":3962},"--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C",[3964],{"type":71,"value":3965}," 10",{"type":66,"tag":107,"props":3967,"children":3968},{"style":120},[3969],{"type":71,"value":3970}," },\n",{"type":66,"tag":107,"props":3972,"children":3973},{"class":109,"line":618},[3974],{"type":66,"tag":107,"props":3975,"children":3976},{"style":120},[3977],{"type":71,"value":3978},"      }\n",{"type":66,"tag":107,"props":3980,"children":3981},{"class":109,"line":649},[3982],{"type":66,"tag":107,"props":3983,"children":3984},{"style":120},[3985],{"type":71,"value":3778},{"type":66,"tag":107,"props":3987,"children":3988},{"class":109,"line":657},[3989],{"type":66,"tag":107,"props":3990,"children":3991},{"emptyLinePlaceholder":210},[3992],{"type":71,"value":213},{"type":66,"tag":107,"props":3994,"children":3995},{"class":109,"line":666},[3996],{"type":66,"tag":107,"props":3997,"children":3998},{"style":2081},[3999],{"type":71,"value":4000},"    \u002F\u002F Return void to continue normally\n",{"type":66,"tag":107,"props":4002,"children":4003},{"class":109,"line":679},[4004],{"type":66,"tag":107,"props":4005,"children":4006},{"style":120},[4007],{"type":71,"value":2525},{"type":66,"tag":107,"props":4009,"children":4010},{"class":109,"line":692},[4011,4016,4020,4024,4028,4032,4036,4040,4044],{"type":66,"tag":107,"props":4012,"children":4013},{"style":236},[4014],{"type":71,"value":4015},"  onAfterToolCall",{"type":66,"tag":107,"props":4017,"children":4018},{"style":120},[4019],{"type":71,"value":263},{"type":66,"tag":107,"props":4021,"children":4022},{"style":120},[4023],{"type":71,"value":353},{"type":66,"tag":107,"props":4025,"children":4026},{"style":356},[4027],{"type":71,"value":359},{"type":66,"tag":107,"props":4029,"children":4030},{"style":120},[4031],{"type":71,"value":134},{"type":66,"tag":107,"props":4033,"children":4034},{"style":356},[4035],{"type":71,"value":471},{"type":66,"tag":107,"props":4037,"children":4038},{"style":120},[4039],{"type":71,"value":290},{"type":66,"tag":107,"props":4041,"children":4042},{"style":220},[4043],{"type":71,"value":368},{"type":66,"tag":107,"props":4045,"children":4046},{"style":120},[4047],{"type":71,"value":373},{"type":66,"tag":107,"props":4049,"children":4050},{"class":109,"line":700},[4051,4055,4059,4063,4067,4072,4076],{"type":66,"tag":107,"props":4052,"children":4053},{"style":114},[4054],{"type":71,"value":3662},{"type":66,"tag":107,"props":4056,"children":4057},{"style":255},[4058],{"type":71,"value":353},{"type":66,"tag":107,"props":4060,"children":4061},{"style":126},[4062],{"type":71,"value":633},{"type":66,"tag":107,"props":4064,"children":4065},{"style":120},[4066],{"type":71,"value":387},{"type":66,"tag":107,"props":4068,"children":4069},{"style":126},[4070],{"type":71,"value":4071},"ok",{"type":66,"tag":107,"props":4073,"children":4074},{"style":255},[4075],{"type":71,"value":1918},{"type":66,"tag":107,"props":4077,"children":4078},{"style":120},[4079],{"type":71,"value":248},{"type":66,"tag":107,"props":4081,"children":4082},{"class":109,"line":2866},[4083,4088,4092,4096,4100,4105,4109,4113,4117,4121,4126,4130,4134,4138,4142,4146,4151,4155],{"type":66,"tag":107,"props":4084,"children":4085},{"style":126},[4086],{"type":71,"value":4087},"      console",{"type":66,"tag":107,"props":4089,"children":4090},{"style":120},[4091],{"type":71,"value":387},{"type":66,"tag":107,"props":4093,"children":4094},{"style":236},[4095],{"type":71,"value":392},{"type":66,"tag":107,"props":4097,"children":4098},{"style":255},[4099],{"type":71,"value":243},{"type":66,"tag":107,"props":4101,"children":4102},{"style":120},[4103],{"type":71,"value":4104},"`${",{"type":66,"tag":107,"props":4106,"children":4107},{"style":126},[4108],{"type":71,"value":633},{"type":66,"tag":107,"props":4110,"children":4111},{"style":120},[4112],{"type":71,"value":387},{"type":66,"tag":107,"props":4114,"children":4115},{"style":126},[4116],{"type":71,"value":3680},{"type":66,"tag":107,"props":4118,"children":4119},{"style":120},[4120],{"type":71,"value":685},{"type":66,"tag":107,"props":4122,"children":4123},{"style":157},[4124],{"type":71,"value":4125}," completed in ",{"type":66,"tag":107,"props":4127,"children":4128},{"style":120},[4129],{"type":71,"value":2471},{"type":66,"tag":107,"props":4131,"children":4132},{"style":126},[4133],{"type":71,"value":633},{"type":66,"tag":107,"props":4135,"children":4136},{"style":120},[4137],{"type":71,"value":387},{"type":66,"tag":107,"props":4139,"children":4140},{"style":126},[4141],{"type":71,"value":2889},{"type":66,"tag":107,"props":4143,"children":4144},{"style":120},[4145],{"type":71,"value":685},{"type":66,"tag":107,"props":4147,"children":4148},{"style":157},[4149],{"type":71,"value":4150},"ms",{"type":66,"tag":107,"props":4152,"children":4153},{"style":120},[4154],{"type":71,"value":2461},{"type":66,"tag":107,"props":4156,"children":4157},{"style":255},[4158],{"type":71,"value":432},{"type":66,"tag":107,"props":4160,"children":4161},{"class":109,"line":2896},[4162,4167,4172],{"type":66,"tag":107,"props":4163,"children":4164},{"style":120},[4165],{"type":71,"value":4166},"    }",{"type":66,"tag":107,"props":4168,"children":4169},{"style":114},[4170],{"type":71,"value":4171}," else",{"type":66,"tag":107,"props":4173,"children":4174},{"style":120},[4175],{"type":71,"value":373},{"type":66,"tag":107,"props":4177,"children":4178},{"class":109,"line":2933},[4179,4183,4187,4191,4195,4199,4203,4207,4211,4215,4220,4224,4228,4232,4236,4240],{"type":66,"tag":107,"props":4180,"children":4181},{"style":126},[4182],{"type":71,"value":4087},{"type":66,"tag":107,"props":4184,"children":4185},{"style":120},[4186],{"type":71,"value":387},{"type":66,"tag":107,"props":4188,"children":4189},{"style":236},[4190],{"type":71,"value":642},{"type":66,"tag":107,"props":4192,"children":4193},{"style":255},[4194],{"type":71,"value":243},{"type":66,"tag":107,"props":4196,"children":4197},{"style":120},[4198],{"type":71,"value":4104},{"type":66,"tag":107,"props":4200,"children":4201},{"style":126},[4202],{"type":71,"value":633},{"type":66,"tag":107,"props":4204,"children":4205},{"style":120},[4206],{"type":71,"value":387},{"type":66,"tag":107,"props":4208,"children":4209},{"style":126},[4210],{"type":71,"value":3680},{"type":66,"tag":107,"props":4212,"children":4213},{"style":120},[4214],{"type":71,"value":685},{"type":66,"tag":107,"props":4216,"children":4217},{"style":157},[4218],{"type":71,"value":4219}," failed:",{"type":66,"tag":107,"props":4221,"children":4222},{"style":120},[4223],{"type":71,"value":2461},{"type":66,"tag":107,"props":4225,"children":4226},{"style":120},[4227],{"type":71,"value":134},{"type":66,"tag":107,"props":4229,"children":4230},{"style":126},[4231],{"type":71,"value":471},{"type":66,"tag":107,"props":4233,"children":4234},{"style":120},[4235],{"type":71,"value":387},{"type":66,"tag":107,"props":4237,"children":4238},{"style":126},[4239],{"type":71,"value":642},{"type":66,"tag":107,"props":4241,"children":4242},{"style":255},[4243],{"type":71,"value":432},{"type":66,"tag":107,"props":4245,"children":4246},{"class":109,"line":2963},[4247],{"type":66,"tag":107,"props":4248,"children":4249},{"style":120},[4250],{"type":71,"value":3778},{"type":66,"tag":107,"props":4252,"children":4253},{"class":109,"line":2980},[4254],{"type":66,"tag":107,"props":4255,"children":4256},{"style":120},[4257],{"type":71,"value":2525},{"type":66,"tag":107,"props":4259,"children":4260},{"class":109,"line":2997},[4261],{"type":66,"tag":107,"props":4262,"children":4263},{"style":120},[4264],{"type":71,"value":2092},{"type":66,"tag":77,"props":4266,"children":4267},{},[4268],{"type":66,"tag":81,"props":4269,"children":4270},{},[4271,4276],{"type":66,"tag":103,"props":4272,"children":4274},{"className":4273},[],[4275],{"type":71,"value":1049},{"type":71,"value":4277}," decision types:",{"type":66,"tag":807,"props":4279,"children":4280},{},[4281,4297],{"type":66,"tag":811,"props":4282,"children":4283},{},[4284],{"type":66,"tag":815,"props":4285,"children":4286},{},[4287,4292],{"type":66,"tag":819,"props":4288,"children":4289},{},[4290],{"type":71,"value":4291},"Decision",{"type":66,"tag":819,"props":4293,"children":4294},{},[4295],{"type":71,"value":4296},"Effect",{"type":66,"tag":835,"props":4298,"children":4299},{},[4300,4323,4340,4364],{"type":66,"tag":815,"props":4301,"children":4302},{},[4303,4318],{"type":66,"tag":842,"props":4304,"children":4305},{},[4306,4312,4313],{"type":66,"tag":103,"props":4307,"children":4309},{"className":4308},[],[4310],{"type":71,"value":4311},"void",{"type":71,"value":1339},{"type":66,"tag":103,"props":4314,"children":4316},{"className":4315},[],[4317],{"type":71,"value":1777},{"type":66,"tag":842,"props":4319,"children":4320},{},[4321],{"type":71,"value":4322},"Continue normally, next middleware decides",{"type":66,"tag":815,"props":4324,"children":4325},{},[4326,4335],{"type":66,"tag":842,"props":4327,"children":4328},{},[4329],{"type":66,"tag":103,"props":4330,"children":4332},{"className":4331},[],[4333],{"type":71,"value":4334},"{ type: 'transformArgs', args }",{"type":66,"tag":842,"props":4336,"children":4337},{},[4338],{"type":71,"value":4339},"Replace tool arguments before execution",{"type":66,"tag":815,"props":4341,"children":4342},{},[4343,4352],{"type":66,"tag":842,"props":4344,"children":4345},{},[4346],{"type":66,"tag":103,"props":4347,"children":4349},{"className":4348},[],[4350],{"type":71,"value":4351},"{ type: 'skip', result }",{"type":66,"tag":842,"props":4353,"children":4354},{},[4355,4357,4363],{"type":71,"value":4356},"Skip execution, use provided result (used by ",{"type":66,"tag":103,"props":4358,"children":4360},{"className":4359},[],[4361],{"type":71,"value":4362},"toolCacheMiddleware",{"type":71,"value":290},{"type":66,"tag":815,"props":4365,"children":4366},{},[4367,4376],{"type":66,"tag":842,"props":4368,"children":4369},{},[4370],{"type":66,"tag":103,"props":4371,"children":4373},{"className":4372},[],[4374],{"type":71,"value":4375},"{ type: 'abort', reason? }",{"type":66,"tag":842,"props":4377,"children":4378},{},[4379],{"type":71,"value":4380},"Abort the entire chat run",{"type":66,"tag":1379,"props":4382,"children":4384},{"id":4383},"pattern-3-structured-output-middleware",[4385],{"type":71,"value":4386},"Pattern 3: Structured-Output Middleware",{"type":66,"tag":77,"props":4388,"children":4389},{},[4390,4391,4396,4398,4403,4405,4410,4411,4416,4417,4422],{"type":71,"value":1134},{"type":66,"tag":103,"props":4392,"children":4394},{"className":4393},[],[4395],{"type":71,"value":905},{"type":71,"value":4397}," is used, the final structured-output adapter call\nnow flows through the same middleware chain as the agent loop (with\n",{"type":66,"tag":103,"props":4399,"children":4401},{"className":4400},[],[4402],{"type":71,"value":1614},{"type":71,"value":4404},"). Before this change, the final call bypassed\nmiddleware entirely — ",{"type":66,"tag":103,"props":4406,"children":4408},{"className":4407},[],[4409],{"type":71,"value":1016},{"type":71,"value":742},{"type":66,"tag":103,"props":4412,"children":4414},{"className":4413},[],[4415],{"type":71,"value":1129},{"type":71,"value":742},{"type":66,"tag":103,"props":4418,"children":4420},{"className":4419},[],[4421],{"type":71,"value":850},{"type":71,"value":4423},", and terminal hooks did\nnot see it.",{"type":66,"tag":77,"props":4425,"children":4426},{},[4427],{"type":66,"tag":81,"props":4428,"children":4429},{},[4430],{"type":71,"value":4431},"Example A — Observability (tracing every chunk, including finalization):",{"type":66,"tag":96,"props":4433,"children":4435},{"className":98,"code":4434,"language":45,"meta":100,"style":100},"import type { ChatMiddleware } from '@tanstack\u002Fai'\n\nconst tracing: ChatMiddleware = {\n  name: 'tracing',\n  onChunk(ctx, chunk) {\n    span.addEvent('chunk', { phase: ctx.phase, type: chunk.type })\n  },\n}\n",[4436],{"type":66,"tag":103,"props":4437,"children":4438},{"__ignoreMap":100},[4439,4478,4485,4513,4541,4574,4670,4677],{"type":66,"tag":107,"props":4440,"children":4441},{"class":109,"line":110},[4442,4446,4450,4454,4458,4462,4466,4470,4474],{"type":66,"tag":107,"props":4443,"children":4444},{"style":114},[4445],{"type":71,"value":117},{"type":66,"tag":107,"props":4447,"children":4448},{"style":114},[4449],{"type":71,"value":3514},{"type":66,"tag":107,"props":4451,"children":4452},{"style":120},[4453],{"type":71,"value":123},{"type":66,"tag":107,"props":4455,"children":4456},{"style":126},[4457],{"type":71,"value":2277},{"type":66,"tag":107,"props":4459,"children":4460},{"style":120},[4461],{"type":71,"value":144},{"type":66,"tag":107,"props":4463,"children":4464},{"style":114},[4465],{"type":71,"value":149},{"type":66,"tag":107,"props":4467,"children":4468},{"style":120},[4469],{"type":71,"value":154},{"type":66,"tag":107,"props":4471,"children":4472},{"style":157},[4473],{"type":71,"value":160},{"type":66,"tag":107,"props":4475,"children":4476},{"style":120},[4477],{"type":71,"value":165},{"type":66,"tag":107,"props":4479,"children":4480},{"class":109,"line":168},[4481],{"type":66,"tag":107,"props":4482,"children":4483},{"emptyLinePlaceholder":210},[4484],{"type":71,"value":213},{"type":66,"tag":107,"props":4486,"children":4487},{"class":109,"line":206},[4488,4492,4497,4501,4505,4509],{"type":66,"tag":107,"props":4489,"children":4490},{"style":220},[4491],{"type":71,"value":223},{"type":66,"tag":107,"props":4493,"children":4494},{"style":126},[4495],{"type":71,"value":4496}," tracing",{"type":66,"tag":107,"props":4498,"children":4499},{"style":120},[4500],{"type":71,"value":263},{"type":66,"tag":107,"props":4502,"children":4503},{"style":1984},[4504],{"type":71,"value":2277},{"type":66,"tag":107,"props":4506,"children":4507},{"style":120},[4508],{"type":71,"value":2371},{"type":66,"tag":107,"props":4510,"children":4511},{"style":120},[4512],{"type":71,"value":373},{"type":66,"tag":107,"props":4514,"children":4515},{"class":109,"line":216},[4516,4520,4524,4528,4533,4537],{"type":66,"tag":107,"props":4517,"children":4518},{"style":255},[4519],{"type":71,"value":2383},{"type":66,"tag":107,"props":4521,"children":4522},{"style":120},[4523],{"type":71,"value":263},{"type":66,"tag":107,"props":4525,"children":4526},{"style":120},[4527],{"type":71,"value":154},{"type":66,"tag":107,"props":4529,"children":4530},{"style":157},[4531],{"type":71,"value":4532},"tracing",{"type":66,"tag":107,"props":4534,"children":4535},{"style":120},[4536],{"type":71,"value":276},{"type":66,"tag":107,"props":4538,"children":4539},{"style":120},[4540],{"type":71,"value":295},{"type":66,"tag":107,"props":4542,"children":4543},{"class":109,"line":251},[4544,4549,4553,4557,4561,4566,4570],{"type":66,"tag":107,"props":4545,"children":4546},{"style":255},[4547],{"type":71,"value":4548},"  onChunk",{"type":66,"tag":107,"props":4550,"children":4551},{"style":120},[4552],{"type":71,"value":243},{"type":66,"tag":107,"props":4554,"children":4555},{"style":356},[4556],{"type":71,"value":359},{"type":66,"tag":107,"props":4558,"children":4559},{"style":120},[4560],{"type":71,"value":134},{"type":66,"tag":107,"props":4562,"children":4563},{"style":356},[4564],{"type":71,"value":4565}," chunk",{"type":66,"tag":107,"props":4567,"children":4568},{"style":120},[4569],{"type":71,"value":290},{"type":66,"tag":107,"props":4571,"children":4572},{"style":120},[4573],{"type":71,"value":373},{"type":66,"tag":107,"props":4575,"children":4576},{"class":109,"line":298},[4577,4582,4586,4591,4595,4599,4604,4608,4612,4616,4621,4625,4629,4633,4637,4641,4645,4649,4653,4657,4662,4666],{"type":66,"tag":107,"props":4578,"children":4579},{"style":126},[4580],{"type":71,"value":4581},"    span",{"type":66,"tag":107,"props":4583,"children":4584},{"style":120},[4585],{"type":71,"value":387},{"type":66,"tag":107,"props":4587,"children":4588},{"style":236},[4589],{"type":71,"value":4590},"addEvent",{"type":66,"tag":107,"props":4592,"children":4593},{"style":255},[4594],{"type":71,"value":243},{"type":66,"tag":107,"props":4596,"children":4597},{"style":120},[4598],{"type":71,"value":276},{"type":66,"tag":107,"props":4600,"children":4601},{"style":157},[4602],{"type":71,"value":4603},"chunk",{"type":66,"tag":107,"props":4605,"children":4606},{"style":120},[4607],{"type":71,"value":276},{"type":66,"tag":107,"props":4609,"children":4610},{"style":120},[4611],{"type":71,"value":134},{"type":66,"tag":107,"props":4613,"children":4614},{"style":120},[4615],{"type":71,"value":123},{"type":66,"tag":107,"props":4617,"children":4618},{"style":255},[4619],{"type":71,"value":4620}," phase",{"type":66,"tag":107,"props":4622,"children":4623},{"style":120},[4624],{"type":71,"value":263},{"type":66,"tag":107,"props":4626,"children":4627},{"style":126},[4628],{"type":71,"value":418},{"type":66,"tag":107,"props":4630,"children":4631},{"style":120},[4632],{"type":71,"value":387},{"type":66,"tag":107,"props":4634,"children":4635},{"style":126},[4636],{"type":71,"value":755},{"type":66,"tag":107,"props":4638,"children":4639},{"style":120},[4640],{"type":71,"value":134},{"type":66,"tag":107,"props":4642,"children":4643},{"style":255},[4644],{"type":71,"value":3514},{"type":66,"tag":107,"props":4646,"children":4647},{"style":120},[4648],{"type":71,"value":263},{"type":66,"tag":107,"props":4650,"children":4651},{"style":126},[4652],{"type":71,"value":4565},{"type":66,"tag":107,"props":4654,"children":4655},{"style":120},[4656],{"type":71,"value":387},{"type":66,"tag":107,"props":4658,"children":4659},{"style":126},[4660],{"type":71,"value":4661},"type",{"type":66,"tag":107,"props":4663,"children":4664},{"style":120},[4665],{"type":71,"value":144},{"type":66,"tag":107,"props":4667,"children":4668},{"style":255},[4669],{"type":71,"value":432},{"type":66,"tag":107,"props":4671,"children":4672},{"class":109,"line":311},[4673],{"type":66,"tag":107,"props":4674,"children":4675},{"style":120},[4676],{"type":71,"value":2525},{"type":66,"tag":107,"props":4678,"children":4679},{"class":109,"line":329},[4680],{"type":66,"tag":107,"props":4681,"children":4682},{"style":120},[4683],{"type":71,"value":2092},{"type":66,"tag":77,"props":4685,"children":4686},{},[4687,4689,4694,4696,4701],{"type":71,"value":4688},"This middleware now observes every chunk from the final structured-output call,\nattributed to ",{"type":66,"tag":103,"props":4690,"children":4692},{"className":4691},[],[4693],{"type":71,"value":1614},{"type":71,"value":4695},". Before the fix, the final\nadapter call bypassed middleware entirely — ",{"type":66,"tag":103,"props":4697,"children":4699},{"className":4698},[],[4700],{"type":71,"value":4532},{"type":71,"value":4702}," would only see agent-loop\nchunks.",{"type":66,"tag":77,"props":4704,"children":4705},{},[4706],{"type":66,"tag":81,"props":4707,"children":4708},{},[4709,4711,4716],{"type":71,"value":4710},"Example B — Schema rewriting (inject shared ",{"type":66,"tag":103,"props":4712,"children":4714},{"className":4713},[],[4715],{"type":71,"value":1850},{"type":71,"value":4717},"):",{"type":66,"tag":96,"props":4719,"children":4721},{"className":98,"code":4720,"language":45,"meta":100,"style":100},"import type { ChatMiddleware } from '@tanstack\u002Fai'\n\nconst injectDefs: ChatMiddleware = {\n  name: 'inject-defs',\n  onStructuredOutputConfig(_ctx, config) {\n    return {\n      outputSchema: { ...config.outputSchema, $defs: { ...sharedDefs } },\n    }\n  },\n}\n",[4722],{"type":66,"tag":103,"props":4723,"children":4724},{"__ignoreMap":100},[4725,4764,4771,4799,4827,4861,4873,4940,4947,4954],{"type":66,"tag":107,"props":4726,"children":4727},{"class":109,"line":110},[4728,4732,4736,4740,4744,4748,4752,4756,4760],{"type":66,"tag":107,"props":4729,"children":4730},{"style":114},[4731],{"type":71,"value":117},{"type":66,"tag":107,"props":4733,"children":4734},{"style":114},[4735],{"type":71,"value":3514},{"type":66,"tag":107,"props":4737,"children":4738},{"style":120},[4739],{"type":71,"value":123},{"type":66,"tag":107,"props":4741,"children":4742},{"style":126},[4743],{"type":71,"value":2277},{"type":66,"tag":107,"props":4745,"children":4746},{"style":120},[4747],{"type":71,"value":144},{"type":66,"tag":107,"props":4749,"children":4750},{"style":114},[4751],{"type":71,"value":149},{"type":66,"tag":107,"props":4753,"children":4754},{"style":120},[4755],{"type":71,"value":154},{"type":66,"tag":107,"props":4757,"children":4758},{"style":157},[4759],{"type":71,"value":160},{"type":66,"tag":107,"props":4761,"children":4762},{"style":120},[4763],{"type":71,"value":165},{"type":66,"tag":107,"props":4765,"children":4766},{"class":109,"line":168},[4767],{"type":66,"tag":107,"props":4768,"children":4769},{"emptyLinePlaceholder":210},[4770],{"type":71,"value":213},{"type":66,"tag":107,"props":4772,"children":4773},{"class":109,"line":206},[4774,4778,4783,4787,4791,4795],{"type":66,"tag":107,"props":4775,"children":4776},{"style":220},[4777],{"type":71,"value":223},{"type":66,"tag":107,"props":4779,"children":4780},{"style":126},[4781],{"type":71,"value":4782}," injectDefs",{"type":66,"tag":107,"props":4784,"children":4785},{"style":120},[4786],{"type":71,"value":263},{"type":66,"tag":107,"props":4788,"children":4789},{"style":1984},[4790],{"type":71,"value":2277},{"type":66,"tag":107,"props":4792,"children":4793},{"style":120},[4794],{"type":71,"value":2371},{"type":66,"tag":107,"props":4796,"children":4797},{"style":120},[4798],{"type":71,"value":373},{"type":66,"tag":107,"props":4800,"children":4801},{"class":109,"line":216},[4802,4806,4810,4814,4819,4823],{"type":66,"tag":107,"props":4803,"children":4804},{"style":255},[4805],{"type":71,"value":2383},{"type":66,"tag":107,"props":4807,"children":4808},{"style":120},[4809],{"type":71,"value":263},{"type":66,"tag":107,"props":4811,"children":4812},{"style":120},[4813],{"type":71,"value":154},{"type":66,"tag":107,"props":4815,"children":4816},{"style":157},[4817],{"type":71,"value":4818},"inject-defs",{"type":66,"tag":107,"props":4820,"children":4821},{"style":120},[4822],{"type":71,"value":276},{"type":66,"tag":107,"props":4824,"children":4825},{"style":120},[4826],{"type":71,"value":295},{"type":66,"tag":107,"props":4828,"children":4829},{"class":109,"line":251},[4830,4835,4839,4844,4848,4853,4857],{"type":66,"tag":107,"props":4831,"children":4832},{"style":255},[4833],{"type":71,"value":4834},"  onStructuredOutputConfig",{"type":66,"tag":107,"props":4836,"children":4837},{"style":120},[4838],{"type":71,"value":243},{"type":66,"tag":107,"props":4840,"children":4841},{"style":356},[4842],{"type":71,"value":4843},"_ctx",{"type":66,"tag":107,"props":4845,"children":4846},{"style":120},[4847],{"type":71,"value":134},{"type":66,"tag":107,"props":4849,"children":4850},{"style":356},[4851],{"type":71,"value":4852}," config",{"type":66,"tag":107,"props":4854,"children":4855},{"style":120},[4856],{"type":71,"value":290},{"type":66,"tag":107,"props":4858,"children":4859},{"style":120},[4860],{"type":71,"value":373},{"type":66,"tag":107,"props":4862,"children":4863},{"class":109,"line":298},[4864,4869],{"type":66,"tag":107,"props":4865,"children":4866},{"style":114},[4867],{"type":71,"value":4868},"    return",{"type":66,"tag":107,"props":4870,"children":4871},{"style":120},[4872],{"type":71,"value":373},{"type":66,"tag":107,"props":4874,"children":4875},{"class":109,"line":311},[4876,4881,4885,4889,4893,4898,4902,4906,4910,4915,4919,4923,4927,4932,4936],{"type":66,"tag":107,"props":4877,"children":4878},{"style":255},[4879],{"type":71,"value":4880},"      outputSchema",{"type":66,"tag":107,"props":4882,"children":4883},{"style":120},[4884],{"type":71,"value":263},{"type":66,"tag":107,"props":4886,"children":4887},{"style":120},[4888],{"type":71,"value":123},{"type":66,"tag":107,"props":4890,"children":4891},{"style":120},[4892],{"type":71,"value":3934},{"type":66,"tag":107,"props":4894,"children":4895},{"style":126},[4896],{"type":71,"value":4897},"config",{"type":66,"tag":107,"props":4899,"children":4900},{"style":120},[4901],{"type":71,"value":387},{"type":66,"tag":107,"props":4903,"children":4904},{"style":126},[4905],{"type":71,"value":1629},{"type":66,"tag":107,"props":4907,"children":4908},{"style":120},[4909],{"type":71,"value":134},{"type":66,"tag":107,"props":4911,"children":4912},{"style":255},[4913],{"type":71,"value":4914}," $defs",{"type":66,"tag":107,"props":4916,"children":4917},{"style":120},[4918],{"type":71,"value":263},{"type":66,"tag":107,"props":4920,"children":4921},{"style":120},[4922],{"type":71,"value":123},{"type":66,"tag":107,"props":4924,"children":4925},{"style":120},[4926],{"type":71,"value":3934},{"type":66,"tag":107,"props":4928,"children":4929},{"style":126},[4930],{"type":71,"value":4931},"sharedDefs",{"type":66,"tag":107,"props":4933,"children":4934},{"style":120},[4935],{"type":71,"value":144},{"type":66,"tag":107,"props":4937,"children":4938},{"style":120},[4939],{"type":71,"value":3970},{"type":66,"tag":107,"props":4941,"children":4942},{"class":109,"line":329},[4943],{"type":66,"tag":107,"props":4944,"children":4945},{"style":120},[4946],{"type":71,"value":3778},{"type":66,"tag":107,"props":4948,"children":4949},{"class":109,"line":338},[4950],{"type":66,"tag":107,"props":4951,"children":4952},{"style":120},[4953],{"type":71,"value":2525},{"type":66,"tag":107,"props":4955,"children":4956},{"class":109,"line":376},[4957],{"type":66,"tag":107,"props":4958,"children":4959},{"style":120},[4960],{"type":71,"value":2092},{"type":66,"tag":77,"props":4962,"children":4963},{},[4964,4969,4971,4977],{"type":66,"tag":103,"props":4965,"children":4967},{"className":4966},[],[4968],{"type":71,"value":894},{"type":71,"value":4970}," is the right hook here because it has direct access\nto ",{"type":66,"tag":103,"props":4972,"children":4974},{"className":4973},[],[4975],{"type":71,"value":4976},"config.outputSchema",{"type":71,"value":4978}," and runs only on the structured-output boundary —\nschema rewrites do not leak into the agent-loop adapter calls.",{"type":66,"tag":1379,"props":4980,"children":4982},{"id":4981},"pattern-4-multiple-middleware-composition",[4983],{"type":71,"value":4984},"Pattern 4: Multiple Middleware Composition",{"type":66,"tag":77,"props":4986,"children":4987},{},[4988],{"type":71,"value":4989},"Middleware executes in array order (left-to-right). Ordering matters for hooks that\npipe or short-circuit:",{"type":66,"tag":96,"props":4991,"children":4993},{"className":98,"code":4992,"language":45,"meta":100,"style":100},"import { chat, type ChatMiddleware } from '@tanstack\u002Fai'\nimport { toolCacheMiddleware } from '@tanstack\u002Fai\u002Fmiddlewares'\nimport { openaiText } from '@tanstack\u002Fai-openai'\n\nconst logging: ChatMiddleware = {\n  name: 'logging',\n  onStart: (ctx) => console.log(`[${ctx.requestId}] started`),\n  onChunk: (ctx, chunk) => {\n    console.log(`[${ctx.requestId}] chunk: ${chunk.type}`)\n  },\n  onFinish: (ctx, info) => {\n    console.log(`[${ctx.requestId}] done in ${info.duration}ms`)\n  },\n}\n\nconst configTransform: ChatMiddleware = {\n  name: 'config-transform',\n  onConfig: (ctx, config) => {\n    if (ctx.phase === 'init') {\n      return {\n        systemPrompts: [...config.systemPrompts, 'Always respond in JSON.'],\n        \u002F\u002F Sampling options are NOT first-class config fields — mutate them\n        \u002F\u002F through `config.modelOptions` using the provider's native key.\n        \u002F\u002F (e.g. OpenAI `temperature` \u002F `max_output_tokens`.)\n        modelOptions: { ...config.modelOptions, temperature: 0.2 },\n      }\n    }\n  },\n}\n\nconst stream = chat({\n  adapter: openaiText('gpt-5.2'),\n  messages,\n  tools: [weatherTool, stockTool],\n  middleware: [\n    logging, \u002F\u002F Runs first\n    configTransform, \u002F\u002F Transforms config second\n    toolCacheMiddleware({ ttl: 60_000 }), \u002F\u002F Caches tool results third\n  ],\n})\n",[4994],{"type":66,"tag":103,"props":4995,"children":4996},{"__ignoreMap":100},[4997,5044,5081,5116,5123,5151,5179,5268,5307,5383,5390,5429,5513,5520,5527,5534,5562,5590,5630,5677,5688,5749,5757,5765,5773,5828,5835,5842,5849,5856,5863,5890,5929,5940,5970,5985,6002,6019,6066,6077],{"type":66,"tag":107,"props":4998,"children":4999},{"class":109,"line":110},[5000,5004,5008,5012,5016,5020,5024,5028,5032,5036,5040],{"type":66,"tag":107,"props":5001,"children":5002},{"style":114},[5003],{"type":71,"value":117},{"type":66,"tag":107,"props":5005,"children":5006},{"style":120},[5007],{"type":71,"value":123},{"type":66,"tag":107,"props":5009,"children":5010},{"style":126},[5011],{"type":71,"value":129},{"type":66,"tag":107,"props":5013,"children":5014},{"style":120},[5015],{"type":71,"value":134},{"type":66,"tag":107,"props":5017,"children":5018},{"style":114},[5019],{"type":71,"value":3514},{"type":66,"tag":107,"props":5021,"children":5022},{"style":126},[5023],{"type":71,"value":2277},{"type":66,"tag":107,"props":5025,"children":5026},{"style":120},[5027],{"type":71,"value":144},{"type":66,"tag":107,"props":5029,"children":5030},{"style":114},[5031],{"type":71,"value":149},{"type":66,"tag":107,"props":5033,"children":5034},{"style":120},[5035],{"type":71,"value":154},{"type":66,"tag":107,"props":5037,"children":5038},{"style":157},[5039],{"type":71,"value":160},{"type":66,"tag":107,"props":5041,"children":5042},{"style":120},[5043],{"type":71,"value":165},{"type":66,"tag":107,"props":5045,"children":5046},{"class":109,"line":168},[5047,5051,5055,5060,5064,5068,5072,5077],{"type":66,"tag":107,"props":5048,"children":5049},{"style":114},[5050],{"type":71,"value":117},{"type":66,"tag":107,"props":5052,"children":5053},{"style":120},[5054],{"type":71,"value":123},{"type":66,"tag":107,"props":5056,"children":5057},{"style":126},[5058],{"type":71,"value":5059}," toolCacheMiddleware",{"type":66,"tag":107,"props":5061,"children":5062},{"style":120},[5063],{"type":71,"value":144},{"type":66,"tag":107,"props":5065,"children":5066},{"style":114},[5067],{"type":71,"value":149},{"type":66,"tag":107,"props":5069,"children":5070},{"style":120},[5071],{"type":71,"value":154},{"type":66,"tag":107,"props":5073,"children":5074},{"style":157},[5075],{"type":71,"value":5076},"@tanstack\u002Fai\u002Fmiddlewares",{"type":66,"tag":107,"props":5078,"children":5079},{"style":120},[5080],{"type":71,"value":165},{"type":66,"tag":107,"props":5082,"children":5083},{"class":109,"line":206},[5084,5088,5092,5096,5100,5104,5108,5112],{"type":66,"tag":107,"props":5085,"children":5086},{"style":114},[5087],{"type":71,"value":117},{"type":66,"tag":107,"props":5089,"children":5090},{"style":120},[5091],{"type":71,"value":123},{"type":66,"tag":107,"props":5093,"children":5094},{"style":126},[5095],{"type":71,"value":182},{"type":66,"tag":107,"props":5097,"children":5098},{"style":120},[5099],{"type":71,"value":144},{"type":66,"tag":107,"props":5101,"children":5102},{"style":114},[5103],{"type":71,"value":149},{"type":66,"tag":107,"props":5105,"children":5106},{"style":120},[5107],{"type":71,"value":154},{"type":66,"tag":107,"props":5109,"children":5110},{"style":157},[5111],{"type":71,"value":199},{"type":66,"tag":107,"props":5113,"children":5114},{"style":120},[5115],{"type":71,"value":165},{"type":66,"tag":107,"props":5117,"children":5118},{"class":109,"line":216},[5119],{"type":66,"tag":107,"props":5120,"children":5121},{"emptyLinePlaceholder":210},[5122],{"type":71,"value":213},{"type":66,"tag":107,"props":5124,"children":5125},{"class":109,"line":251},[5126,5130,5135,5139,5143,5147],{"type":66,"tag":107,"props":5127,"children":5128},{"style":220},[5129],{"type":71,"value":223},{"type":66,"tag":107,"props":5131,"children":5132},{"style":126},[5133],{"type":71,"value":5134}," logging",{"type":66,"tag":107,"props":5136,"children":5137},{"style":120},[5138],{"type":71,"value":263},{"type":66,"tag":107,"props":5140,"children":5141},{"style":1984},[5142],{"type":71,"value":2277},{"type":66,"tag":107,"props":5144,"children":5145},{"style":120},[5146],{"type":71,"value":2371},{"type":66,"tag":107,"props":5148,"children":5149},{"style":120},[5150],{"type":71,"value":373},{"type":66,"tag":107,"props":5152,"children":5153},{"class":109,"line":298},[5154,5158,5162,5166,5171,5175],{"type":66,"tag":107,"props":5155,"children":5156},{"style":255},[5157],{"type":71,"value":2383},{"type":66,"tag":107,"props":5159,"children":5160},{"style":120},[5161],{"type":71,"value":263},{"type":66,"tag":107,"props":5163,"children":5164},{"style":120},[5165],{"type":71,"value":154},{"type":66,"tag":107,"props":5167,"children":5168},{"style":157},[5169],{"type":71,"value":5170},"logging",{"type":66,"tag":107,"props":5172,"children":5173},{"style":120},[5174],{"type":71,"value":276},{"type":66,"tag":107,"props":5176,"children":5177},{"style":120},[5178],{"type":71,"value":295},{"type":66,"tag":107,"props":5180,"children":5181},{"class":109,"line":311},[5182,5186,5190,5194,5198,5202,5206,5211,5215,5219,5223,5227,5231,5235,5239,5243,5247,5251,5256,5260,5264],{"type":66,"tag":107,"props":5183,"children":5184},{"style":236},[5185],{"type":71,"value":2412},{"type":66,"tag":107,"props":5187,"children":5188},{"style":120},[5189],{"type":71,"value":263},{"type":66,"tag":107,"props":5191,"children":5192},{"style":120},[5193],{"type":71,"value":353},{"type":66,"tag":107,"props":5195,"children":5196},{"style":356},[5197],{"type":71,"value":359},{"type":66,"tag":107,"props":5199,"children":5200},{"style":120},[5201],{"type":71,"value":290},{"type":66,"tag":107,"props":5203,"children":5204},{"style":220},[5205],{"type":71,"value":368},{"type":66,"tag":107,"props":5207,"children":5208},{"style":126},[5209],{"type":71,"value":5210}," console",{"type":66,"tag":107,"props":5212,"children":5213},{"style":120},[5214],{"type":71,"value":387},{"type":66,"tag":107,"props":5216,"children":5217},{"style":236},[5218],{"type":71,"value":392},{"type":66,"tag":107,"props":5220,"children":5221},{"style":126},[5222],{"type":71,"value":243},{"type":66,"tag":107,"props":5224,"children":5225},{"style":120},[5226],{"type":71,"value":2461},{"type":66,"tag":107,"props":5228,"children":5229},{"style":157},[5230],{"type":71,"value":2466},{"type":66,"tag":107,"props":5232,"children":5233},{"style":120},[5234],{"type":71,"value":2471},{"type":66,"tag":107,"props":5236,"children":5237},{"style":126},[5238],{"type":71,"value":359},{"type":66,"tag":107,"props":5240,"children":5241},{"style":120},[5242],{"type":71,"value":387},{"type":66,"tag":107,"props":5244,"children":5245},{"style":126},[5246],{"type":71,"value":740},{"type":66,"tag":107,"props":5248,"children":5249},{"style":120},[5250],{"type":71,"value":685},{"type":66,"tag":107,"props":5252,"children":5253},{"style":157},[5254],{"type":71,"value":5255},"] started",{"type":66,"tag":107,"props":5257,"children":5258},{"style":120},[5259],{"type":71,"value":2461},{"type":66,"tag":107,"props":5261,"children":5262},{"style":126},[5263],{"type":71,"value":290},{"type":66,"tag":107,"props":5265,"children":5266},{"style":120},[5267],{"type":71,"value":295},{"type":66,"tag":107,"props":5269,"children":5270},{"class":109,"line":329},[5271,5275,5279,5283,5287,5291,5295,5299,5303],{"type":66,"tag":107,"props":5272,"children":5273},{"style":236},[5274],{"type":71,"value":4548},{"type":66,"tag":107,"props":5276,"children":5277},{"style":120},[5278],{"type":71,"value":263},{"type":66,"tag":107,"props":5280,"children":5281},{"style":120},[5282],{"type":71,"value":353},{"type":66,"tag":107,"props":5284,"children":5285},{"style":356},[5286],{"type":71,"value":359},{"type":66,"tag":107,"props":5288,"children":5289},{"style":120},[5290],{"type":71,"value":134},{"type":66,"tag":107,"props":5292,"children":5293},{"style":356},[5294],{"type":71,"value":4565},{"type":66,"tag":107,"props":5296,"children":5297},{"style":120},[5298],{"type":71,"value":290},{"type":66,"tag":107,"props":5300,"children":5301},{"style":220},[5302],{"type":71,"value":368},{"type":66,"tag":107,"props":5304,"children":5305},{"style":120},[5306],{"type":71,"value":373},{"type":66,"tag":107,"props":5308,"children":5309},{"class":109,"line":338},[5310,5314,5318,5322,5326,5330,5334,5338,5342,5346,5350,5354,5359,5363,5367,5371,5375,5379],{"type":66,"tag":107,"props":5311,"children":5312},{"style":126},[5313],{"type":71,"value":2444},{"type":66,"tag":107,"props":5315,"children":5316},{"style":120},[5317],{"type":71,"value":387},{"type":66,"tag":107,"props":5319,"children":5320},{"style":236},[5321],{"type":71,"value":392},{"type":66,"tag":107,"props":5323,"children":5324},{"style":255},[5325],{"type":71,"value":243},{"type":66,"tag":107,"props":5327,"children":5328},{"style":120},[5329],{"type":71,"value":2461},{"type":66,"tag":107,"props":5331,"children":5332},{"style":157},[5333],{"type":71,"value":2466},{"type":66,"tag":107,"props":5335,"children":5336},{"style":120},[5337],{"type":71,"value":2471},{"type":66,"tag":107,"props":5339,"children":5340},{"style":126},[5341],{"type":71,"value":359},{"type":66,"tag":107,"props":5343,"children":5344},{"style":120},[5345],{"type":71,"value":387},{"type":66,"tag":107,"props":5347,"children":5348},{"style":126},[5349],{"type":71,"value":740},{"type":66,"tag":107,"props":5351,"children":5352},{"style":120},[5353],{"type":71,"value":685},{"type":66,"tag":107,"props":5355,"children":5356},{"style":157},[5357],{"type":71,"value":5358},"] chunk: ",{"type":66,"tag":107,"props":5360,"children":5361},{"style":120},[5362],{"type":71,"value":2471},{"type":66,"tag":107,"props":5364,"children":5365},{"style":126},[5366],{"type":71,"value":4603},{"type":66,"tag":107,"props":5368,"children":5369},{"style":120},[5370],{"type":71,"value":387},{"type":66,"tag":107,"props":5372,"children":5373},{"style":126},[5374],{"type":71,"value":4661},{"type":66,"tag":107,"props":5376,"children":5377},{"style":120},[5378],{"type":71,"value":2513},{"type":66,"tag":107,"props":5380,"children":5381},{"style":255},[5382],{"type":71,"value":432},{"type":66,"tag":107,"props":5384,"children":5385},{"class":109,"line":376},[5386],{"type":66,"tag":107,"props":5387,"children":5388},{"style":120},[5389],{"type":71,"value":2525},{"type":66,"tag":107,"props":5391,"children":5392},{"class":109,"line":435},[5393,5397,5401,5405,5409,5413,5417,5421,5425],{"type":66,"tag":107,"props":5394,"children":5395},{"style":236},[5396],{"type":71,"value":2657},{"type":66,"tag":107,"props":5398,"children":5399},{"style":120},[5400],{"type":71,"value":263},{"type":66,"tag":107,"props":5402,"children":5403},{"style":120},[5404],{"type":71,"value":353},{"type":66,"tag":107,"props":5406,"children":5407},{"style":356},[5408],{"type":71,"value":359},{"type":66,"tag":107,"props":5410,"children":5411},{"style":120},[5412],{"type":71,"value":134},{"type":66,"tag":107,"props":5414,"children":5415},{"style":356},[5416],{"type":71,"value":471},{"type":66,"tag":107,"props":5418,"children":5419},{"style":120},[5420],{"type":71,"value":290},{"type":66,"tag":107,"props":5422,"children":5423},{"style":220},[5424],{"type":71,"value":368},{"type":66,"tag":107,"props":5426,"children":5427},{"style":120},[5428],{"type":71,"value":373},{"type":66,"tag":107,"props":5430,"children":5431},{"class":109,"line":444},[5432,5436,5440,5444,5448,5452,5456,5460,5464,5468,5472,5476,5481,5485,5489,5493,5497,5501,5505,5509],{"type":66,"tag":107,"props":5433,"children":5434},{"style":126},[5435],{"type":71,"value":2444},{"type":66,"tag":107,"props":5437,"children":5438},{"style":120},[5439],{"type":71,"value":387},{"type":66,"tag":107,"props":5441,"children":5442},{"style":236},[5443],{"type":71,"value":392},{"type":66,"tag":107,"props":5445,"children":5446},{"style":255},[5447],{"type":71,"value":243},{"type":66,"tag":107,"props":5449,"children":5450},{"style":120},[5451],{"type":71,"value":2461},{"type":66,"tag":107,"props":5453,"children":5454},{"style":157},[5455],{"type":71,"value":2466},{"type":66,"tag":107,"props":5457,"children":5458},{"style":120},[5459],{"type":71,"value":2471},{"type":66,"tag":107,"props":5461,"children":5462},{"style":126},[5463],{"type":71,"value":359},{"type":66,"tag":107,"props":5465,"children":5466},{"style":120},[5467],{"type":71,"value":387},{"type":66,"tag":107,"props":5469,"children":5470},{"style":126},[5471],{"type":71,"value":740},{"type":66,"tag":107,"props":5473,"children":5474},{"style":120},[5475],{"type":71,"value":685},{"type":66,"tag":107,"props":5477,"children":5478},{"style":157},[5479],{"type":71,"value":5480},"] done in ",{"type":66,"tag":107,"props":5482,"children":5483},{"style":120},[5484],{"type":71,"value":2471},{"type":66,"tag":107,"props":5486,"children":5487},{"style":126},[5488],{"type":71,"value":633},{"type":66,"tag":107,"props":5490,"children":5491},{"style":120},[5492],{"type":71,"value":387},{"type":66,"tag":107,"props":5494,"children":5495},{"style":126},[5496],{"type":71,"value":2889},{"type":66,"tag":107,"props":5498,"children":5499},{"style":120},[5500],{"type":71,"value":685},{"type":66,"tag":107,"props":5502,"children":5503},{"style":157},[5504],{"type":71,"value":4150},{"type":66,"tag":107,"props":5506,"children":5507},{"style":120},[5508],{"type":71,"value":2461},{"type":66,"tag":107,"props":5510,"children":5511},{"style":255},[5512],{"type":71,"value":432},{"type":66,"tag":107,"props":5514,"children":5515},{"class":109,"line":486},[5516],{"type":66,"tag":107,"props":5517,"children":5518},{"style":120},[5519],{"type":71,"value":2525},{"type":66,"tag":107,"props":5521,"children":5522},{"class":109,"line":569},[5523],{"type":66,"tag":107,"props":5524,"children":5525},{"style":120},[5526],{"type":71,"value":2092},{"type":66,"tag":107,"props":5528,"children":5529},{"class":109,"line":577},[5530],{"type":66,"tag":107,"props":5531,"children":5532},{"emptyLinePlaceholder":210},[5533],{"type":71,"value":213},{"type":66,"tag":107,"props":5535,"children":5536},{"class":109,"line":618},[5537,5541,5546,5550,5554,5558],{"type":66,"tag":107,"props":5538,"children":5539},{"style":220},[5540],{"type":71,"value":223},{"type":66,"tag":107,"props":5542,"children":5543},{"style":126},[5544],{"type":71,"value":5545}," configTransform",{"type":66,"tag":107,"props":5547,"children":5548},{"style":120},[5549],{"type":71,"value":263},{"type":66,"tag":107,"props":5551,"children":5552},{"style":1984},[5553],{"type":71,"value":2277},{"type":66,"tag":107,"props":5555,"children":5556},{"style":120},[5557],{"type":71,"value":2371},{"type":66,"tag":107,"props":5559,"children":5560},{"style":120},[5561],{"type":71,"value":373},{"type":66,"tag":107,"props":5563,"children":5564},{"class":109,"line":649},[5565,5569,5573,5577,5582,5586],{"type":66,"tag":107,"props":5566,"children":5567},{"style":255},[5568],{"type":71,"value":2383},{"type":66,"tag":107,"props":5570,"children":5571},{"style":120},[5572],{"type":71,"value":263},{"type":66,"tag":107,"props":5574,"children":5575},{"style":120},[5576],{"type":71,"value":154},{"type":66,"tag":107,"props":5578,"children":5579},{"style":157},[5580],{"type":71,"value":5581},"config-transform",{"type":66,"tag":107,"props":5583,"children":5584},{"style":120},[5585],{"type":71,"value":276},{"type":66,"tag":107,"props":5587,"children":5588},{"style":120},[5589],{"type":71,"value":295},{"type":66,"tag":107,"props":5591,"children":5592},{"class":109,"line":657},[5593,5598,5602,5606,5610,5614,5618,5622,5626],{"type":66,"tag":107,"props":5594,"children":5595},{"style":236},[5596],{"type":71,"value":5597},"  onConfig",{"type":66,"tag":107,"props":5599,"children":5600},{"style":120},[5601],{"type":71,"value":263},{"type":66,"tag":107,"props":5603,"children":5604},{"style":120},[5605],{"type":71,"value":353},{"type":66,"tag":107,"props":5607,"children":5608},{"style":356},[5609],{"type":71,"value":359},{"type":66,"tag":107,"props":5611,"children":5612},{"style":120},[5613],{"type":71,"value":134},{"type":66,"tag":107,"props":5615,"children":5616},{"style":356},[5617],{"type":71,"value":4852},{"type":66,"tag":107,"props":5619,"children":5620},{"style":120},[5621],{"type":71,"value":290},{"type":66,"tag":107,"props":5623,"children":5624},{"style":220},[5625],{"type":71,"value":368},{"type":66,"tag":107,"props":5627,"children":5628},{"style":120},[5629],{"type":71,"value":373},{"type":66,"tag":107,"props":5631,"children":5632},{"class":109,"line":666},[5633,5637,5641,5645,5649,5653,5657,5661,5665,5669,5673],{"type":66,"tag":107,"props":5634,"children":5635},{"style":114},[5636],{"type":71,"value":3662},{"type":66,"tag":107,"props":5638,"children":5639},{"style":255},[5640],{"type":71,"value":353},{"type":66,"tag":107,"props":5642,"children":5643},{"style":126},[5644],{"type":71,"value":359},{"type":66,"tag":107,"props":5646,"children":5647},{"style":120},[5648],{"type":71,"value":387},{"type":66,"tag":107,"props":5650,"children":5651},{"style":126},[5652],{"type":71,"value":755},{"type":66,"tag":107,"props":5654,"children":5655},{"style":120},[5656],{"type":71,"value":3685},{"type":66,"tag":107,"props":5658,"children":5659},{"style":120},[5660],{"type":71,"value":154},{"type":66,"tag":107,"props":5662,"children":5663},{"style":157},[5664],{"type":71,"value":861},{"type":66,"tag":107,"props":5666,"children":5667},{"style":120},[5668],{"type":71,"value":276},{"type":66,"tag":107,"props":5670,"children":5671},{"style":255},[5672],{"type":71,"value":1918},{"type":66,"tag":107,"props":5674,"children":5675},{"style":120},[5676],{"type":71,"value":248},{"type":66,"tag":107,"props":5678,"children":5679},{"class":109,"line":679},[5680,5684],{"type":66,"tag":107,"props":5681,"children":5682},{"style":114},[5683],{"type":71,"value":3714},{"type":66,"tag":107,"props":5685,"children":5686},{"style":120},[5687],{"type":71,"value":373},{"type":66,"tag":107,"props":5689,"children":5690},{"class":109,"line":692},[5691,5696,5700,5705,5710,5714,5718,5723,5727,5731,5736,5740,5745],{"type":66,"tag":107,"props":5692,"children":5693},{"style":255},[5694],{"type":71,"value":5695},"        systemPrompts",{"type":66,"tag":107,"props":5697,"children":5698},{"style":120},[5699],{"type":71,"value":263},{"type":66,"tag":107,"props":5701,"children":5702},{"style":255},[5703],{"type":71,"value":5704}," [",{"type":66,"tag":107,"props":5706,"children":5707},{"style":120},[5708],{"type":71,"value":5709},"...",{"type":66,"tag":107,"props":5711,"children":5712},{"style":126},[5713],{"type":71,"value":4897},{"type":66,"tag":107,"props":5715,"children":5716},{"style":120},[5717],{"type":71,"value":387},{"type":66,"tag":107,"props":5719,"children":5720},{"style":126},[5721],{"type":71,"value":5722},"systemPrompts",{"type":66,"tag":107,"props":5724,"children":5725},{"style":120},[5726],{"type":71,"value":134},{"type":66,"tag":107,"props":5728,"children":5729},{"style":120},[5730],{"type":71,"value":154},{"type":66,"tag":107,"props":5732,"children":5733},{"style":157},[5734],{"type":71,"value":5735},"Always respond in JSON.",{"type":66,"tag":107,"props":5737,"children":5738},{"style":120},[5739],{"type":71,"value":276},{"type":66,"tag":107,"props":5741,"children":5742},{"style":255},[5743],{"type":71,"value":5744},"]",{"type":66,"tag":107,"props":5746,"children":5747},{"style":120},[5748],{"type":71,"value":295},{"type":66,"tag":107,"props":5750,"children":5751},{"class":109,"line":700},[5752],{"type":66,"tag":107,"props":5753,"children":5754},{"style":2081},[5755],{"type":71,"value":5756},"        \u002F\u002F Sampling options are NOT first-class config fields — mutate them\n",{"type":66,"tag":107,"props":5758,"children":5759},{"class":109,"line":2866},[5760],{"type":66,"tag":107,"props":5761,"children":5762},{"style":2081},[5763],{"type":71,"value":5764},"        \u002F\u002F through `config.modelOptions` using the provider's native key.\n",{"type":66,"tag":107,"props":5766,"children":5767},{"class":109,"line":2896},[5768],{"type":66,"tag":107,"props":5769,"children":5770},{"style":2081},[5771],{"type":71,"value":5772},"        \u002F\u002F (e.g. OpenAI `temperature` \u002F `max_output_tokens`.)\n",{"type":66,"tag":107,"props":5774,"children":5775},{"class":109,"line":2933},[5776,5781,5785,5789,5793,5797,5801,5806,5810,5815,5819,5824],{"type":66,"tag":107,"props":5777,"children":5778},{"style":255},[5779],{"type":71,"value":5780},"        modelOptions",{"type":66,"tag":107,"props":5782,"children":5783},{"style":120},[5784],{"type":71,"value":263},{"type":66,"tag":107,"props":5786,"children":5787},{"style":120},[5788],{"type":71,"value":123},{"type":66,"tag":107,"props":5790,"children":5791},{"style":120},[5792],{"type":71,"value":3934},{"type":66,"tag":107,"props":5794,"children":5795},{"style":126},[5796],{"type":71,"value":4897},{"type":66,"tag":107,"props":5798,"children":5799},{"style":120},[5800],{"type":71,"value":387},{"type":66,"tag":107,"props":5802,"children":5803},{"style":126},[5804],{"type":71,"value":5805},"modelOptions",{"type":66,"tag":107,"props":5807,"children":5808},{"style":120},[5809],{"type":71,"value":134},{"type":66,"tag":107,"props":5811,"children":5812},{"style":255},[5813],{"type":71,"value":5814}," temperature",{"type":66,"tag":107,"props":5816,"children":5817},{"style":120},[5818],{"type":71,"value":263},{"type":66,"tag":107,"props":5820,"children":5821},{"style":3962},[5822],{"type":71,"value":5823}," 0.2",{"type":66,"tag":107,"props":5825,"children":5826},{"style":120},[5827],{"type":71,"value":3970},{"type":66,"tag":107,"props":5829,"children":5830},{"class":109,"line":2963},[5831],{"type":66,"tag":107,"props":5832,"children":5833},{"style":120},[5834],{"type":71,"value":3978},{"type":66,"tag":107,"props":5836,"children":5837},{"class":109,"line":2980},[5838],{"type":66,"tag":107,"props":5839,"children":5840},{"style":120},[5841],{"type":71,"value":3778},{"type":66,"tag":107,"props":5843,"children":5844},{"class":109,"line":2997},[5845],{"type":66,"tag":107,"props":5846,"children":5847},{"style":120},[5848],{"type":71,"value":2525},{"type":66,"tag":107,"props":5850,"children":5851},{"class":109,"line":3006},[5852],{"type":66,"tag":107,"props":5853,"children":5854},{"style":120},[5855],{"type":71,"value":2092},{"type":66,"tag":107,"props":5857,"children":5858},{"class":109,"line":3014},[5859],{"type":66,"tag":107,"props":5860,"children":5861},{"emptyLinePlaceholder":210},[5862],{"type":71,"value":213},{"type":66,"tag":107,"props":5864,"children":5865},{"class":109,"line":3055},[5866,5870,5874,5878,5882,5886],{"type":66,"tag":107,"props":5867,"children":5868},{"style":220},[5869],{"type":71,"value":223},{"type":66,"tag":107,"props":5871,"children":5872},{"style":126},[5873],{"type":71,"value":228},{"type":66,"tag":107,"props":5875,"children":5876},{"style":120},[5877],{"type":71,"value":233},{"type":66,"tag":107,"props":5879,"children":5880},{"style":236},[5881],{"type":71,"value":129},{"type":66,"tag":107,"props":5883,"children":5884},{"style":126},[5885],{"type":71,"value":243},{"type":66,"tag":107,"props":5887,"children":5888},{"style":120},[5889],{"type":71,"value":248},{"type":66,"tag":107,"props":5891,"children":5892},{"class":109,"line":3075},[5893,5897,5901,5905,5909,5913,5917,5921,5925],{"type":66,"tag":107,"props":5894,"children":5895},{"style":255},[5896],{"type":71,"value":258},{"type":66,"tag":107,"props":5898,"children":5899},{"style":120},[5900],{"type":71,"value":263},{"type":66,"tag":107,"props":5902,"children":5903},{"style":236},[5904],{"type":71,"value":182},{"type":66,"tag":107,"props":5906,"children":5907},{"style":126},[5908],{"type":71,"value":243},{"type":66,"tag":107,"props":5910,"children":5911},{"style":120},[5912],{"type":71,"value":276},{"type":66,"tag":107,"props":5914,"children":5915},{"style":157},[5916],{"type":71,"value":281},{"type":66,"tag":107,"props":5918,"children":5919},{"style":120},[5920],{"type":71,"value":276},{"type":66,"tag":107,"props":5922,"children":5923},{"style":126},[5924],{"type":71,"value":290},{"type":66,"tag":107,"props":5926,"children":5927},{"style":120},[5928],{"type":71,"value":295},{"type":66,"tag":107,"props":5930,"children":5931},{"class":109,"line":3108},[5932,5936],{"type":66,"tag":107,"props":5933,"children":5934},{"style":126},[5935],{"type":71,"value":304},{"type":66,"tag":107,"props":5937,"children":5938},{"style":120},[5939],{"type":71,"value":295},{"type":66,"tag":107,"props":5941,"children":5942},{"class":109,"line":3136},[5943,5948,5952,5957,5961,5966],{"type":66,"tag":107,"props":5944,"children":5945},{"style":255},[5946],{"type":71,"value":5947},"  tools",{"type":66,"tag":107,"props":5949,"children":5950},{"style":120},[5951],{"type":71,"value":263},{"type":66,"tag":107,"props":5953,"children":5954},{"style":126},[5955],{"type":71,"value":5956}," [weatherTool",{"type":66,"tag":107,"props":5958,"children":5959},{"style":120},[5960],{"type":71,"value":134},{"type":66,"tag":107,"props":5962,"children":5963},{"style":126},[5964],{"type":71,"value":5965}," stockTool]",{"type":66,"tag":107,"props":5967,"children":5968},{"style":120},[5969],{"type":71,"value":295},{"type":66,"tag":107,"props":5971,"children":5972},{"class":109,"line":3168},[5973,5977,5981],{"type":66,"tag":107,"props":5974,"children":5975},{"style":255},[5976],{"type":71,"value":317},{"type":66,"tag":107,"props":5978,"children":5979},{"style":120},[5980],{"type":71,"value":263},{"type":66,"tag":107,"props":5982,"children":5983},{"style":126},[5984],{"type":71,"value":326},{"type":66,"tag":107,"props":5986,"children":5987},{"class":109,"line":3196},[5988,5993,5997],{"type":66,"tag":107,"props":5989,"children":5990},{"style":126},[5991],{"type":71,"value":5992},"    logging",{"type":66,"tag":107,"props":5994,"children":5995},{"style":120},[5996],{"type":71,"value":134},{"type":66,"tag":107,"props":5998,"children":5999},{"style":2081},[6000],{"type":71,"value":6001}," \u002F\u002F Runs first\n",{"type":66,"tag":107,"props":6003,"children":6004},{"class":109,"line":3238},[6005,6010,6014],{"type":66,"tag":107,"props":6006,"children":6007},{"style":126},[6008],{"type":71,"value":6009},"    configTransform",{"type":66,"tag":107,"props":6011,"children":6012},{"style":120},[6013],{"type":71,"value":134},{"type":66,"tag":107,"props":6015,"children":6016},{"style":2081},[6017],{"type":71,"value":6018}," \u002F\u002F Transforms config second\n",{"type":66,"tag":107,"props":6020,"children":6021},{"class":109,"line":3266},[6022,6027,6031,6035,6040,6044,6049,6053,6057,6061],{"type":66,"tag":107,"props":6023,"children":6024},{"style":236},[6025],{"type":71,"value":6026},"    toolCacheMiddleware",{"type":66,"tag":107,"props":6028,"children":6029},{"style":126},[6030],{"type":71,"value":243},{"type":66,"tag":107,"props":6032,"children":6033},{"style":120},[6034],{"type":71,"value":501},{"type":66,"tag":107,"props":6036,"children":6037},{"style":255},[6038],{"type":71,"value":6039}," ttl",{"type":66,"tag":107,"props":6041,"children":6042},{"style":120},[6043],{"type":71,"value":263},{"type":66,"tag":107,"props":6045,"children":6046},{"style":3962},[6047],{"type":71,"value":6048}," 60_000",{"type":66,"tag":107,"props":6050,"children":6051},{"style":120},[6052],{"type":71,"value":144},{"type":66,"tag":107,"props":6054,"children":6055},{"style":126},[6056],{"type":71,"value":290},{"type":66,"tag":107,"props":6058,"children":6059},{"style":120},[6060],{"type":71,"value":134},{"type":66,"tag":107,"props":6062,"children":6063},{"style":2081},[6064],{"type":71,"value":6065}," \u002F\u002F Caches tool results third\n",{"type":66,"tag":107,"props":6067,"children":6068},{"class":109,"line":3282},[6069,6073],{"type":66,"tag":107,"props":6070,"children":6071},{"style":126},[6072],{"type":71,"value":672},{"type":66,"tag":107,"props":6074,"children":6075},{"style":120},[6076],{"type":71,"value":295},{"type":66,"tag":107,"props":6078,"children":6079},{"class":109,"line":3298},[6080,6084],{"type":66,"tag":107,"props":6081,"children":6082},{"style":120},[6083],{"type":71,"value":685},{"type":66,"tag":107,"props":6085,"children":6086},{"style":126},[6087],{"type":71,"value":432},{"type":66,"tag":77,"props":6089,"children":6090},{},[6091],{"type":66,"tag":81,"props":6092,"children":6093},{},[6094],{"type":71,"value":6095},"Composition rules by hook:",{"type":66,"tag":807,"props":6097,"children":6098},{},[6099,6119],{"type":66,"tag":811,"props":6100,"children":6101},{},[6102],{"type":66,"tag":815,"props":6103,"children":6104},{},[6105,6109,6114],{"type":66,"tag":819,"props":6106,"children":6107},{},[6108],{"type":71,"value":823},{"type":66,"tag":819,"props":6110,"children":6111},{},[6112],{"type":71,"value":6113},"Composition",{"type":66,"tag":819,"props":6115,"children":6116},{},[6117],{"type":71,"value":6118},"Effect of Order",{"type":66,"tag":835,"props":6120,"children":6121},{},[6122,6148,6171,6192,6217,6243,6262,6281],{"type":66,"tag":815,"props":6123,"children":6124},{},[6125,6133,6143],{"type":66,"tag":842,"props":6126,"children":6127},{},[6128],{"type":66,"tag":103,"props":6129,"children":6131},{"className":6130},[],[6132],{"type":71,"value":850},{"type":66,"tag":842,"props":6134,"children":6135},{},[6136,6141],{"type":66,"tag":81,"props":6137,"children":6138},{},[6139],{"type":71,"value":6140},"Piped",{"type":71,"value":6142}," -- each receives previous output",{"type":66,"tag":842,"props":6144,"children":6145},{},[6146],{"type":71,"value":6147},"Earlier middleware transforms first",{"type":66,"tag":815,"props":6149,"children":6150},{},[6151,6159,6167],{"type":66,"tag":842,"props":6152,"children":6153},{},[6154],{"type":66,"tag":103,"props":6155,"children":6157},{"className":6156},[],[6158],{"type":71,"value":894},{"type":66,"tag":842,"props":6160,"children":6161},{},[6162,6166],{"type":66,"tag":81,"props":6163,"children":6164},{},[6165],{"type":71,"value":6140},{"type":71,"value":6142},{"type":66,"tag":842,"props":6168,"children":6169},{},[6170],{"type":71,"value":6147},{"type":66,"tag":815,"props":6172,"children":6173},{},[6174,6182,6187],{"type":66,"tag":842,"props":6175,"children":6176},{},[6177],{"type":66,"tag":103,"props":6178,"children":6180},{"className":6179},[],[6181],{"type":71,"value":929},{"type":66,"tag":842,"props":6183,"children":6184},{},[6185],{"type":71,"value":6186},"Sequential",{"type":66,"tag":842,"props":6188,"children":6189},{},[6190],{"type":71,"value":6191},"All run in order",{"type":66,"tag":815,"props":6193,"children":6194},{},[6195,6203,6212],{"type":66,"tag":842,"props":6196,"children":6197},{},[6198],{"type":66,"tag":103,"props":6199,"children":6201},{"className":6200},[],[6202],{"type":71,"value":1016},{"type":66,"tag":842,"props":6204,"children":6205},{},[6206,6210],{"type":66,"tag":81,"props":6207,"children":6208},{},[6209],{"type":71,"value":6140},{"type":71,"value":6211}," -- chunks flow through each",{"type":66,"tag":842,"props":6213,"children":6214},{},[6215],{"type":71,"value":6216},"If first drops a chunk, later never see it",{"type":66,"tag":815,"props":6218,"children":6219},{},[6220,6228,6238],{"type":66,"tag":842,"props":6221,"children":6222},{},[6223],{"type":66,"tag":103,"props":6224,"children":6226},{"className":6225},[],[6227],{"type":71,"value":1049},{"type":66,"tag":842,"props":6229,"children":6230},{},[6231,6236],{"type":66,"tag":81,"props":6232,"children":6233},{},[6234],{"type":71,"value":6235},"First-win",{"type":71,"value":6237}," -- first non-void decision wins",{"type":66,"tag":842,"props":6239,"children":6240},{},[6241],{"type":71,"value":6242},"Earlier middleware has priority",{"type":66,"tag":815,"props":6244,"children":6245},{},[6246,6254,6258],{"type":66,"tag":842,"props":6247,"children":6248},{},[6249],{"type":66,"tag":103,"props":6250,"children":6252},{"className":6251},[],[6253],{"type":71,"value":1077},{"type":66,"tag":842,"props":6255,"children":6256},{},[6257],{"type":71,"value":6186},{"type":66,"tag":842,"props":6259,"children":6260},{},[6261],{"type":71,"value":6191},{"type":66,"tag":815,"props":6263,"children":6264},{},[6265,6273,6277],{"type":66,"tag":842,"props":6266,"children":6267},{},[6268],{"type":66,"tag":103,"props":6269,"children":6271},{"className":6270},[],[6272],{"type":71,"value":1129},{"type":66,"tag":842,"props":6274,"children":6275},{},[6276],{"type":71,"value":6186},{"type":66,"tag":842,"props":6278,"children":6279},{},[6280],{"type":71,"value":6191},{"type":66,"tag":815,"props":6282,"children":6283},{},[6284,6293,6297],{"type":66,"tag":842,"props":6285,"children":6286},{},[6287],{"type":66,"tag":103,"props":6288,"children":6290},{"className":6289},[],[6291],{"type":71,"value":6292},"onFinish\u002FonAbort\u002FonError",{"type":66,"tag":842,"props":6294,"children":6295},{},[6296],{"type":71,"value":6186},{"type":66,"tag":842,"props":6298,"children":6299},{},[6300],{"type":71,"value":6191},{"type":66,"tag":89,"props":6302,"children":6304},{"id":6303},"pattern-tool-call-budget-app-owned",[6305],{"type":71,"value":6306},"Pattern: tool-call budget (app-owned)",{"type":66,"tag":77,"props":6308,"children":6309},{},[6310,6312,6317,6319,6324,6326,6332],{"type":71,"value":6311},"Not a built-in. Cap fan-out with ",{"type":66,"tag":103,"props":6313,"children":6315},{"className":6314},[],[6316],{"type":71,"value":1049},{"type":71,"value":6318}," skip + ",{"type":66,"tag":103,"props":6320,"children":6322},{"className":6321},[],[6323],{"type":71,"value":982},{"type":71,"value":6325},".\nSee ",{"type":66,"tag":103,"props":6327,"children":6329},{"className":6328},[],[6330],{"type":71,"value":6331},"docs\u002Fchat\u002Fagentic-cycle.md",{"type":71,"value":6333}," (\"Tool-call budgets\").",{"type":66,"tag":96,"props":6335,"children":6337},{"className":98,"code":6336,"language":45,"meta":100,"style":100},"import { chat, maxIterations, type ChatMiddleware } from '@tanstack\u002Fai'\n\nfunction toolCallBudget(opts: {\n  max?: number\n  maxPerTurn?: number\n}): ChatMiddleware {\n  let perTurn = 0\n  return {\n    onIteration: () => {\n      perTurn = 0\n    },\n    onToolPhaseComplete: () => {\n      perTurn = 0\n    },\n    onBeforeToolCall: () => {\n      if (opts.maxPerTurn == null) return undefined\n      if (++perTurn > opts.maxPerTurn) {\n        return {\n          type: 'skip',\n          result: {\n            error: `Skipped: exceeded maxToolCallsPerTurn (${opts.maxPerTurn})`,\n          },\n        }\n      }\n      return undefined\n    },\n    onShouldContinue: (_ctx, state) =>\n      opts.max != null && state.toolCallCount >= opts.max ? false : undefined,\n  }\n}\n\nchat({\n  adapter,\n  messages,\n  tools: [weatherTool],\n  agentLoopStrategy: maxIterations(20),\n  middleware: [toolCallBudget({ maxPerTurn: 10, max: 20 })],\n})\n",[6338],{"type":66,"tag":103,"props":6339,"children":6340},{"__ignoreMap":100},[6341,6397,6404,6434,6451,6467,6483,6505,6517,6542,6558,6565,6589,6604,6611,6635,6683,6730,6742,6771,6787,6841,6849,6857,6864,6875,6882,6920,7001,7009,7016,7023,7039,7050,7061,7081,7114,7186],{"type":66,"tag":107,"props":6342,"children":6343},{"class":109,"line":110},[6344,6348,6352,6356,6360,6365,6369,6373,6377,6381,6385,6389,6393],{"type":66,"tag":107,"props":6345,"children":6346},{"style":114},[6347],{"type":71,"value":117},{"type":66,"tag":107,"props":6349,"children":6350},{"style":120},[6351],{"type":71,"value":123},{"type":66,"tag":107,"props":6353,"children":6354},{"style":126},[6355],{"type":71,"value":129},{"type":66,"tag":107,"props":6357,"children":6358},{"style":120},[6359],{"type":71,"value":134},{"type":66,"tag":107,"props":6361,"children":6362},{"style":126},[6363],{"type":71,"value":6364}," maxIterations",{"type":66,"tag":107,"props":6366,"children":6367},{"style":120},[6368],{"type":71,"value":134},{"type":66,"tag":107,"props":6370,"children":6371},{"style":114},[6372],{"type":71,"value":3514},{"type":66,"tag":107,"props":6374,"children":6375},{"style":126},[6376],{"type":71,"value":2277},{"type":66,"tag":107,"props":6378,"children":6379},{"style":120},[6380],{"type":71,"value":144},{"type":66,"tag":107,"props":6382,"children":6383},{"style":114},[6384],{"type":71,"value":149},{"type":66,"tag":107,"props":6386,"children":6387},{"style":120},[6388],{"type":71,"value":154},{"type":66,"tag":107,"props":6390,"children":6391},{"style":157},[6392],{"type":71,"value":160},{"type":66,"tag":107,"props":6394,"children":6395},{"style":120},[6396],{"type":71,"value":165},{"type":66,"tag":107,"props":6398,"children":6399},{"class":109,"line":168},[6400],{"type":66,"tag":107,"props":6401,"children":6402},{"emptyLinePlaceholder":210},[6403],{"type":71,"value":213},{"type":66,"tag":107,"props":6405,"children":6406},{"class":109,"line":206},[6407,6412,6417,6421,6426,6430],{"type":66,"tag":107,"props":6408,"children":6409},{"style":220},[6410],{"type":71,"value":6411},"function",{"type":66,"tag":107,"props":6413,"children":6414},{"style":236},[6415],{"type":71,"value":6416}," toolCallBudget",{"type":66,"tag":107,"props":6418,"children":6419},{"style":120},[6420],{"type":71,"value":243},{"type":66,"tag":107,"props":6422,"children":6423},{"style":356},[6424],{"type":71,"value":6425},"opts",{"type":66,"tag":107,"props":6427,"children":6428},{"style":120},[6429],{"type":71,"value":263},{"type":66,"tag":107,"props":6431,"children":6432},{"style":120},[6433],{"type":71,"value":373},{"type":66,"tag":107,"props":6435,"children":6436},{"class":109,"line":216},[6437,6442,6446],{"type":66,"tag":107,"props":6438,"children":6439},{"style":255},[6440],{"type":71,"value":6441},"  max",{"type":66,"tag":107,"props":6443,"children":6444},{"style":120},[6445],{"type":71,"value":1881},{"type":66,"tag":107,"props":6447,"children":6448},{"style":1984},[6449],{"type":71,"value":6450}," number\n",{"type":66,"tag":107,"props":6452,"children":6453},{"class":109,"line":251},[6454,6459,6463],{"type":66,"tag":107,"props":6455,"children":6456},{"style":255},[6457],{"type":71,"value":6458},"  maxPerTurn",{"type":66,"tag":107,"props":6460,"children":6461},{"style":120},[6462],{"type":71,"value":1881},{"type":66,"tag":107,"props":6464,"children":6465},{"style":1984},[6466],{"type":71,"value":6450},{"type":66,"tag":107,"props":6468,"children":6469},{"class":109,"line":298},[6470,6475,6479],{"type":66,"tag":107,"props":6471,"children":6472},{"style":120},[6473],{"type":71,"value":6474},"}):",{"type":66,"tag":107,"props":6476,"children":6477},{"style":1984},[6478],{"type":71,"value":2277},{"type":66,"tag":107,"props":6480,"children":6481},{"style":120},[6482],{"type":71,"value":373},{"type":66,"tag":107,"props":6484,"children":6485},{"class":109,"line":311},[6486,6491,6496,6500],{"type":66,"tag":107,"props":6487,"children":6488},{"style":220},[6489],{"type":71,"value":6490},"  let",{"type":66,"tag":107,"props":6492,"children":6493},{"style":126},[6494],{"type":71,"value":6495}," perTurn",{"type":66,"tag":107,"props":6497,"children":6498},{"style":120},[6499],{"type":71,"value":2371},{"type":66,"tag":107,"props":6501,"children":6502},{"style":3962},[6503],{"type":71,"value":6504}," 0\n",{"type":66,"tag":107,"props":6506,"children":6507},{"class":109,"line":329},[6508,6513],{"type":66,"tag":107,"props":6509,"children":6510},{"style":114},[6511],{"type":71,"value":6512},"  return",{"type":66,"tag":107,"props":6514,"children":6515},{"style":120},[6516],{"type":71,"value":373},{"type":66,"tag":107,"props":6518,"children":6519},{"class":109,"line":338},[6520,6525,6529,6534,6538],{"type":66,"tag":107,"props":6521,"children":6522},{"style":236},[6523],{"type":71,"value":6524},"    onIteration",{"type":66,"tag":107,"props":6526,"children":6527},{"style":120},[6528],{"type":71,"value":263},{"type":66,"tag":107,"props":6530,"children":6531},{"style":120},[6532],{"type":71,"value":6533}," ()",{"type":66,"tag":107,"props":6535,"children":6536},{"style":220},[6537],{"type":71,"value":368},{"type":66,"tag":107,"props":6539,"children":6540},{"style":120},[6541],{"type":71,"value":373},{"type":66,"tag":107,"props":6543,"children":6544},{"class":109,"line":376},[6545,6550,6554],{"type":66,"tag":107,"props":6546,"children":6547},{"style":126},[6548],{"type":71,"value":6549},"      perTurn",{"type":66,"tag":107,"props":6551,"children":6552},{"style":120},[6553],{"type":71,"value":2371},{"type":66,"tag":107,"props":6555,"children":6556},{"style":3962},[6557],{"type":71,"value":6504},{"type":66,"tag":107,"props":6559,"children":6560},{"class":109,"line":435},[6561],{"type":66,"tag":107,"props":6562,"children":6563},{"style":120},[6564],{"type":71,"value":663},{"type":66,"tag":107,"props":6566,"children":6567},{"class":109,"line":444},[6568,6573,6577,6581,6585],{"type":66,"tag":107,"props":6569,"children":6570},{"style":236},[6571],{"type":71,"value":6572},"    onToolPhaseComplete",{"type":66,"tag":107,"props":6574,"children":6575},{"style":120},[6576],{"type":71,"value":263},{"type":66,"tag":107,"props":6578,"children":6579},{"style":120},[6580],{"type":71,"value":6533},{"type":66,"tag":107,"props":6582,"children":6583},{"style":220},[6584],{"type":71,"value":368},{"type":66,"tag":107,"props":6586,"children":6587},{"style":120},[6588],{"type":71,"value":373},{"type":66,"tag":107,"props":6590,"children":6591},{"class":109,"line":486},[6592,6596,6600],{"type":66,"tag":107,"props":6593,"children":6594},{"style":126},[6595],{"type":71,"value":6549},{"type":66,"tag":107,"props":6597,"children":6598},{"style":120},[6599],{"type":71,"value":2371},{"type":66,"tag":107,"props":6601,"children":6602},{"style":3962},[6603],{"type":71,"value":6504},{"type":66,"tag":107,"props":6605,"children":6606},{"class":109,"line":569},[6607],{"type":66,"tag":107,"props":6608,"children":6609},{"style":120},[6610],{"type":71,"value":663},{"type":66,"tag":107,"props":6612,"children":6613},{"class":109,"line":577},[6614,6619,6623,6627,6631],{"type":66,"tag":107,"props":6615,"children":6616},{"style":236},[6617],{"type":71,"value":6618},"    onBeforeToolCall",{"type":66,"tag":107,"props":6620,"children":6621},{"style":120},[6622],{"type":71,"value":263},{"type":66,"tag":107,"props":6624,"children":6625},{"style":120},[6626],{"type":71,"value":6533},{"type":66,"tag":107,"props":6628,"children":6629},{"style":220},[6630],{"type":71,"value":368},{"type":66,"tag":107,"props":6632,"children":6633},{"style":120},[6634],{"type":71,"value":373},{"type":66,"tag":107,"props":6636,"children":6637},{"class":109,"line":618},[6638,6643,6647,6651,6655,6660,6665,6670,6674,6678],{"type":66,"tag":107,"props":6639,"children":6640},{"style":114},[6641],{"type":71,"value":6642},"      if",{"type":66,"tag":107,"props":6644,"children":6645},{"style":255},[6646],{"type":71,"value":353},{"type":66,"tag":107,"props":6648,"children":6649},{"style":126},[6650],{"type":71,"value":6425},{"type":66,"tag":107,"props":6652,"children":6653},{"style":120},[6654],{"type":71,"value":387},{"type":66,"tag":107,"props":6656,"children":6657},{"style":126},[6658],{"type":71,"value":6659},"maxPerTurn",{"type":66,"tag":107,"props":6661,"children":6662},{"style":120},[6663],{"type":71,"value":6664}," ==",{"type":66,"tag":107,"props":6666,"children":6667},{"style":120},[6668],{"type":71,"value":6669}," null",{"type":66,"tag":107,"props":6671,"children":6672},{"style":255},[6673],{"type":71,"value":1918},{"type":66,"tag":107,"props":6675,"children":6676},{"style":114},[6677],{"type":71,"value":706},{"type":66,"tag":107,"props":6679,"children":6680},{"style":120},[6681],{"type":71,"value":6682}," undefined\n",{"type":66,"tag":107,"props":6684,"children":6685},{"class":109,"line":649},[6686,6690,6694,6699,6704,6709,6714,6718,6722,6726],{"type":66,"tag":107,"props":6687,"children":6688},{"style":114},[6689],{"type":71,"value":6642},{"type":66,"tag":107,"props":6691,"children":6692},{"style":255},[6693],{"type":71,"value":353},{"type":66,"tag":107,"props":6695,"children":6696},{"style":120},[6697],{"type":71,"value":6698},"++",{"type":66,"tag":107,"props":6700,"children":6701},{"style":126},[6702],{"type":71,"value":6703},"perTurn",{"type":66,"tag":107,"props":6705,"children":6706},{"style":120},[6707],{"type":71,"value":6708}," >",{"type":66,"tag":107,"props":6710,"children":6711},{"style":126},[6712],{"type":71,"value":6713}," opts",{"type":66,"tag":107,"props":6715,"children":6716},{"style":120},[6717],{"type":71,"value":387},{"type":66,"tag":107,"props":6719,"children":6720},{"style":126},[6721],{"type":71,"value":6659},{"type":66,"tag":107,"props":6723,"children":6724},{"style":255},[6725],{"type":71,"value":1918},{"type":66,"tag":107,"props":6727,"children":6728},{"style":120},[6729],{"type":71,"value":248},{"type":66,"tag":107,"props":6731,"children":6732},{"class":109,"line":657},[6733,6738],{"type":66,"tag":107,"props":6734,"children":6735},{"style":114},[6736],{"type":71,"value":6737},"        return",{"type":66,"tag":107,"props":6739,"children":6740},{"style":120},[6741],{"type":71,"value":373},{"type":66,"tag":107,"props":6743,"children":6744},{"class":109,"line":666},[6745,6750,6754,6758,6763,6767],{"type":66,"tag":107,"props":6746,"children":6747},{"style":255},[6748],{"type":71,"value":6749},"          type",{"type":66,"tag":107,"props":6751,"children":6752},{"style":120},[6753],{"type":71,"value":263},{"type":66,"tag":107,"props":6755,"children":6756},{"style":120},[6757],{"type":71,"value":154},{"type":66,"tag":107,"props":6759,"children":6760},{"style":157},[6761],{"type":71,"value":6762},"skip",{"type":66,"tag":107,"props":6764,"children":6765},{"style":120},[6766],{"type":71,"value":276},{"type":66,"tag":107,"props":6768,"children":6769},{"style":120},[6770],{"type":71,"value":295},{"type":66,"tag":107,"props":6772,"children":6773},{"class":109,"line":679},[6774,6779,6783],{"type":66,"tag":107,"props":6775,"children":6776},{"style":255},[6777],{"type":71,"value":6778},"          result",{"type":66,"tag":107,"props":6780,"children":6781},{"style":120},[6782],{"type":71,"value":263},{"type":66,"tag":107,"props":6784,"children":6785},{"style":120},[6786],{"type":71,"value":373},{"type":66,"tag":107,"props":6788,"children":6789},{"class":109,"line":692},[6790,6795,6799,6804,6809,6813,6817,6821,6825,6829,6833,6837],{"type":66,"tag":107,"props":6791,"children":6792},{"style":255},[6793],{"type":71,"value":6794},"            error",{"type":66,"tag":107,"props":6796,"children":6797},{"style":120},[6798],{"type":71,"value":263},{"type":66,"tag":107,"props":6800,"children":6801},{"style":120},[6802],{"type":71,"value":6803}," `",{"type":66,"tag":107,"props":6805,"children":6806},{"style":157},[6807],{"type":71,"value":6808},"Skipped: exceeded maxToolCallsPerTurn (",{"type":66,"tag":107,"props":6810,"children":6811},{"style":120},[6812],{"type":71,"value":2471},{"type":66,"tag":107,"props":6814,"children":6815},{"style":126},[6816],{"type":71,"value":6425},{"type":66,"tag":107,"props":6818,"children":6819},{"style":120},[6820],{"type":71,"value":387},{"type":66,"tag":107,"props":6822,"children":6823},{"style":126},[6824],{"type":71,"value":6659},{"type":66,"tag":107,"props":6826,"children":6827},{"style":120},[6828],{"type":71,"value":685},{"type":66,"tag":107,"props":6830,"children":6831},{"style":157},[6832],{"type":71,"value":290},{"type":66,"tag":107,"props":6834,"children":6835},{"style":120},[6836],{"type":71,"value":2461},{"type":66,"tag":107,"props":6838,"children":6839},{"style":120},[6840],{"type":71,"value":295},{"type":66,"tag":107,"props":6842,"children":6843},{"class":109,"line":700},[6844],{"type":66,"tag":107,"props":6845,"children":6846},{"style":120},[6847],{"type":71,"value":6848},"          },\n",{"type":66,"tag":107,"props":6850,"children":6851},{"class":109,"line":2866},[6852],{"type":66,"tag":107,"props":6853,"children":6854},{"style":120},[6855],{"type":71,"value":6856},"        }\n",{"type":66,"tag":107,"props":6858,"children":6859},{"class":109,"line":2896},[6860],{"type":66,"tag":107,"props":6861,"children":6862},{"style":120},[6863],{"type":71,"value":3978},{"type":66,"tag":107,"props":6865,"children":6866},{"class":109,"line":2933},[6867,6871],{"type":66,"tag":107,"props":6868,"children":6869},{"style":114},[6870],{"type":71,"value":3714},{"type":66,"tag":107,"props":6872,"children":6873},{"style":120},[6874],{"type":71,"value":6682},{"type":66,"tag":107,"props":6876,"children":6877},{"class":109,"line":2963},[6878],{"type":66,"tag":107,"props":6879,"children":6880},{"style":120},[6881],{"type":71,"value":663},{"type":66,"tag":107,"props":6883,"children":6884},{"class":109,"line":2980},[6885,6890,6894,6898,6902,6906,6911,6915],{"type":66,"tag":107,"props":6886,"children":6887},{"style":236},[6888],{"type":71,"value":6889},"    onShouldContinue",{"type":66,"tag":107,"props":6891,"children":6892},{"style":120},[6893],{"type":71,"value":263},{"type":66,"tag":107,"props":6895,"children":6896},{"style":120},[6897],{"type":71,"value":353},{"type":66,"tag":107,"props":6899,"children":6900},{"style":356},[6901],{"type":71,"value":4843},{"type":66,"tag":107,"props":6903,"children":6904},{"style":120},[6905],{"type":71,"value":134},{"type":66,"tag":107,"props":6907,"children":6908},{"style":356},[6909],{"type":71,"value":6910}," state",{"type":66,"tag":107,"props":6912,"children":6913},{"style":120},[6914],{"type":71,"value":290},{"type":66,"tag":107,"props":6916,"children":6917},{"style":220},[6918],{"type":71,"value":6919}," =>\n",{"type":66,"tag":107,"props":6921,"children":6922},{"class":109,"line":2997},[6923,6928,6932,6937,6942,6946,6950,6954,6958,6963,6968,6972,6976,6980,6985,6991,6996],{"type":66,"tag":107,"props":6924,"children":6925},{"style":126},[6926],{"type":71,"value":6927},"      opts",{"type":66,"tag":107,"props":6929,"children":6930},{"style":120},[6931],{"type":71,"value":387},{"type":66,"tag":107,"props":6933,"children":6934},{"style":126},[6935],{"type":71,"value":6936},"max",{"type":66,"tag":107,"props":6938,"children":6939},{"style":120},[6940],{"type":71,"value":6941}," !=",{"type":66,"tag":107,"props":6943,"children":6944},{"style":120},[6945],{"type":71,"value":6669},{"type":66,"tag":107,"props":6947,"children":6948},{"style":120},[6949],{"type":71,"value":3838},{"type":66,"tag":107,"props":6951,"children":6952},{"style":126},[6953],{"type":71,"value":6910},{"type":66,"tag":107,"props":6955,"children":6956},{"style":120},[6957],{"type":71,"value":387},{"type":66,"tag":107,"props":6959,"children":6960},{"style":126},[6961],{"type":71,"value":6962},"toolCallCount",{"type":66,"tag":107,"props":6964,"children":6965},{"style":120},[6966],{"type":71,"value":6967}," >=",{"type":66,"tag":107,"props":6969,"children":6970},{"style":126},[6971],{"type":71,"value":6713},{"type":66,"tag":107,"props":6973,"children":6974},{"style":120},[6975],{"type":71,"value":387},{"type":66,"tag":107,"props":6977,"children":6978},{"style":126},[6979],{"type":71,"value":6936},{"type":66,"tag":107,"props":6981,"children":6982},{"style":120},[6983],{"type":71,"value":6984}," ?",{"type":66,"tag":107,"props":6986,"children":6988},{"style":6987},"--shiki-light:#FF5370;--shiki-default:#FF9CAC;--shiki-dark:#FF9CAC",[6989],{"type":71,"value":6990}," false",{"type":66,"tag":107,"props":6992,"children":6993},{"style":120},[6994],{"type":71,"value":6995}," :",{"type":66,"tag":107,"props":6997,"children":6998},{"style":120},[6999],{"type":71,"value":7000}," undefined,\n",{"type":66,"tag":107,"props":7002,"children":7003},{"class":109,"line":3006},[7004],{"type":66,"tag":107,"props":7005,"children":7006},{"style":120},[7007],{"type":71,"value":7008},"  }\n",{"type":66,"tag":107,"props":7010,"children":7011},{"class":109,"line":3014},[7012],{"type":66,"tag":107,"props":7013,"children":7014},{"style":120},[7015],{"type":71,"value":2092},{"type":66,"tag":107,"props":7017,"children":7018},{"class":109,"line":3055},[7019],{"type":66,"tag":107,"props":7020,"children":7021},{"emptyLinePlaceholder":210},[7022],{"type":71,"value":213},{"type":66,"tag":107,"props":7024,"children":7025},{"class":109,"line":3075},[7026,7031,7035],{"type":66,"tag":107,"props":7027,"children":7028},{"style":236},[7029],{"type":71,"value":7030},"chat",{"type":66,"tag":107,"props":7032,"children":7033},{"style":126},[7034],{"type":71,"value":243},{"type":66,"tag":107,"props":7036,"children":7037},{"style":120},[7038],{"type":71,"value":248},{"type":66,"tag":107,"props":7040,"children":7041},{"class":109,"line":3108},[7042,7046],{"type":66,"tag":107,"props":7043,"children":7044},{"style":126},[7045],{"type":71,"value":258},{"type":66,"tag":107,"props":7047,"children":7048},{"style":120},[7049],{"type":71,"value":295},{"type":66,"tag":107,"props":7051,"children":7052},{"class":109,"line":3136},[7053,7057],{"type":66,"tag":107,"props":7054,"children":7055},{"style":126},[7056],{"type":71,"value":304},{"type":66,"tag":107,"props":7058,"children":7059},{"style":120},[7060],{"type":71,"value":295},{"type":66,"tag":107,"props":7062,"children":7063},{"class":109,"line":3168},[7064,7068,7072,7077],{"type":66,"tag":107,"props":7065,"children":7066},{"style":255},[7067],{"type":71,"value":5947},{"type":66,"tag":107,"props":7069,"children":7070},{"style":120},[7071],{"type":71,"value":263},{"type":66,"tag":107,"props":7073,"children":7074},{"style":126},[7075],{"type":71,"value":7076}," [weatherTool]",{"type":66,"tag":107,"props":7078,"children":7079},{"style":120},[7080],{"type":71,"value":295},{"type":66,"tag":107,"props":7082,"children":7083},{"class":109,"line":3196},[7084,7089,7093,7097,7101,7106,7110],{"type":66,"tag":107,"props":7085,"children":7086},{"style":255},[7087],{"type":71,"value":7088},"  agentLoopStrategy",{"type":66,"tag":107,"props":7090,"children":7091},{"style":120},[7092],{"type":71,"value":263},{"type":66,"tag":107,"props":7094,"children":7095},{"style":236},[7096],{"type":71,"value":6364},{"type":66,"tag":107,"props":7098,"children":7099},{"style":126},[7100],{"type":71,"value":243},{"type":66,"tag":107,"props":7102,"children":7103},{"style":3962},[7104],{"type":71,"value":7105},"20",{"type":66,"tag":107,"props":7107,"children":7108},{"style":126},[7109],{"type":71,"value":290},{"type":66,"tag":107,"props":7111,"children":7112},{"style":120},[7113],{"type":71,"value":295},{"type":66,"tag":107,"props":7115,"children":7116},{"class":109,"line":3238},[7117,7121,7125,7129,7134,7138,7142,7147,7151,7155,7159,7164,7168,7173,7177,7182],{"type":66,"tag":107,"props":7118,"children":7119},{"style":255},[7120],{"type":71,"value":317},{"type":66,"tag":107,"props":7122,"children":7123},{"style":120},[7124],{"type":71,"value":263},{"type":66,"tag":107,"props":7126,"children":7127},{"style":126},[7128],{"type":71,"value":5704},{"type":66,"tag":107,"props":7130,"children":7131},{"style":236},[7132],{"type":71,"value":7133},"toolCallBudget",{"type":66,"tag":107,"props":7135,"children":7136},{"style":126},[7137],{"type":71,"value":243},{"type":66,"tag":107,"props":7139,"children":7140},{"style":120},[7141],{"type":71,"value":501},{"type":66,"tag":107,"props":7143,"children":7144},{"style":255},[7145],{"type":71,"value":7146}," maxPerTurn",{"type":66,"tag":107,"props":7148,"children":7149},{"style":120},[7150],{"type":71,"value":263},{"type":66,"tag":107,"props":7152,"children":7153},{"style":3962},[7154],{"type":71,"value":3965},{"type":66,"tag":107,"props":7156,"children":7157},{"style":120},[7158],{"type":71,"value":134},{"type":66,"tag":107,"props":7160,"children":7161},{"style":255},[7162],{"type":71,"value":7163}," max",{"type":66,"tag":107,"props":7165,"children":7166},{"style":120},[7167],{"type":71,"value":263},{"type":66,"tag":107,"props":7169,"children":7170},{"style":3962},[7171],{"type":71,"value":7172}," 20",{"type":66,"tag":107,"props":7174,"children":7175},{"style":120},[7176],{"type":71,"value":144},{"type":66,"tag":107,"props":7178,"children":7179},{"style":126},[7180],{"type":71,"value":7181},")]",{"type":66,"tag":107,"props":7183,"children":7184},{"style":120},[7185],{"type":71,"value":295},{"type":66,"tag":107,"props":7187,"children":7188},{"class":109,"line":3266},[7189,7193],{"type":66,"tag":107,"props":7190,"children":7191},{"style":120},[7192],{"type":71,"value":685},{"type":66,"tag":107,"props":7194,"children":7195},{"style":126},[7196],{"type":71,"value":432},{"type":66,"tag":89,"props":7198,"children":7200},{"id":7199},"built-in-toolcachemiddleware",[7201],{"type":71,"value":7202},"Built-in: toolCacheMiddleware",{"type":66,"tag":77,"props":7204,"children":7205},{},[7206,7208,7213],{"type":71,"value":7207},"Caches tool call results by name + arguments. Import from ",{"type":66,"tag":103,"props":7209,"children":7211},{"className":7210},[],[7212],{"type":71,"value":5076},{"type":71,"value":263},{"type":66,"tag":96,"props":7215,"children":7217},{"className":98,"code":7216,"language":45,"meta":100,"style":100},"import { chat } from '@tanstack\u002Fai'\nimport { toolCacheMiddleware } from '@tanstack\u002Fai\u002Fmiddlewares'\n\nconst stream = chat({\n  adapter,\n  messages,\n  tools: [weatherTool],\n  middleware: [\n    toolCacheMiddleware({\n      ttl: 60_000, \u002F\u002F Cache entries expire after 60 seconds\n      maxSize: 50, \u002F\u002F Max 50 entries (LRU eviction)\n      toolNames: ['getWeather'], \u002F\u002F Only cache specific tools\n    }),\n  ],\n})\n",[7218],{"type":66,"tag":103,"props":7219,"children":7220},{"__ignoreMap":100},[7221,7256,7291,7298,7325,7336,7347,7366,7381,7396,7421,7447,7489,7504,7515],{"type":66,"tag":107,"props":7222,"children":7223},{"class":109,"line":110},[7224,7228,7232,7236,7240,7244,7248,7252],{"type":66,"tag":107,"props":7225,"children":7226},{"style":114},[7227],{"type":71,"value":117},{"type":66,"tag":107,"props":7229,"children":7230},{"style":120},[7231],{"type":71,"value":123},{"type":66,"tag":107,"props":7233,"children":7234},{"style":126},[7235],{"type":71,"value":129},{"type":66,"tag":107,"props":7237,"children":7238},{"style":120},[7239],{"type":71,"value":144},{"type":66,"tag":107,"props":7241,"children":7242},{"style":114},[7243],{"type":71,"value":149},{"type":66,"tag":107,"props":7245,"children":7246},{"style":120},[7247],{"type":71,"value":154},{"type":66,"tag":107,"props":7249,"children":7250},{"style":157},[7251],{"type":71,"value":160},{"type":66,"tag":107,"props":7253,"children":7254},{"style":120},[7255],{"type":71,"value":165},{"type":66,"tag":107,"props":7257,"children":7258},{"class":109,"line":168},[7259,7263,7267,7271,7275,7279,7283,7287],{"type":66,"tag":107,"props":7260,"children":7261},{"style":114},[7262],{"type":71,"value":117},{"type":66,"tag":107,"props":7264,"children":7265},{"style":120},[7266],{"type":71,"value":123},{"type":66,"tag":107,"props":7268,"children":7269},{"style":126},[7270],{"type":71,"value":5059},{"type":66,"tag":107,"props":7272,"children":7273},{"style":120},[7274],{"type":71,"value":144},{"type":66,"tag":107,"props":7276,"children":7277},{"style":114},[7278],{"type":71,"value":149},{"type":66,"tag":107,"props":7280,"children":7281},{"style":120},[7282],{"type":71,"value":154},{"type":66,"tag":107,"props":7284,"children":7285},{"style":157},[7286],{"type":71,"value":5076},{"type":66,"tag":107,"props":7288,"children":7289},{"style":120},[7290],{"type":71,"value":165},{"type":66,"tag":107,"props":7292,"children":7293},{"class":109,"line":206},[7294],{"type":66,"tag":107,"props":7295,"children":7296},{"emptyLinePlaceholder":210},[7297],{"type":71,"value":213},{"type":66,"tag":107,"props":7299,"children":7300},{"class":109,"line":216},[7301,7305,7309,7313,7317,7321],{"type":66,"tag":107,"props":7302,"children":7303},{"style":220},[7304],{"type":71,"value":223},{"type":66,"tag":107,"props":7306,"children":7307},{"style":126},[7308],{"type":71,"value":228},{"type":66,"tag":107,"props":7310,"children":7311},{"style":120},[7312],{"type":71,"value":233},{"type":66,"tag":107,"props":7314,"children":7315},{"style":236},[7316],{"type":71,"value":129},{"type":66,"tag":107,"props":7318,"children":7319},{"style":126},[7320],{"type":71,"value":243},{"type":66,"tag":107,"props":7322,"children":7323},{"style":120},[7324],{"type":71,"value":248},{"type":66,"tag":107,"props":7326,"children":7327},{"class":109,"line":251},[7328,7332],{"type":66,"tag":107,"props":7329,"children":7330},{"style":126},[7331],{"type":71,"value":258},{"type":66,"tag":107,"props":7333,"children":7334},{"style":120},[7335],{"type":71,"value":295},{"type":66,"tag":107,"props":7337,"children":7338},{"class":109,"line":298},[7339,7343],{"type":66,"tag":107,"props":7340,"children":7341},{"style":126},[7342],{"type":71,"value":304},{"type":66,"tag":107,"props":7344,"children":7345},{"style":120},[7346],{"type":71,"value":295},{"type":66,"tag":107,"props":7348,"children":7349},{"class":109,"line":311},[7350,7354,7358,7362],{"type":66,"tag":107,"props":7351,"children":7352},{"style":255},[7353],{"type":71,"value":5947},{"type":66,"tag":107,"props":7355,"children":7356},{"style":120},[7357],{"type":71,"value":263},{"type":66,"tag":107,"props":7359,"children":7360},{"style":126},[7361],{"type":71,"value":7076},{"type":66,"tag":107,"props":7363,"children":7364},{"style":120},[7365],{"type":71,"value":295},{"type":66,"tag":107,"props":7367,"children":7368},{"class":109,"line":329},[7369,7373,7377],{"type":66,"tag":107,"props":7370,"children":7371},{"style":255},[7372],{"type":71,"value":317},{"type":66,"tag":107,"props":7374,"children":7375},{"style":120},[7376],{"type":71,"value":263},{"type":66,"tag":107,"props":7378,"children":7379},{"style":126},[7380],{"type":71,"value":326},{"type":66,"tag":107,"props":7382,"children":7383},{"class":109,"line":338},[7384,7388,7392],{"type":66,"tag":107,"props":7385,"children":7386},{"style":236},[7387],{"type":71,"value":6026},{"type":66,"tag":107,"props":7389,"children":7390},{"style":126},[7391],{"type":71,"value":243},{"type":66,"tag":107,"props":7393,"children":7394},{"style":120},[7395],{"type":71,"value":248},{"type":66,"tag":107,"props":7397,"children":7398},{"class":109,"line":376},[7399,7404,7408,7412,7416],{"type":66,"tag":107,"props":7400,"children":7401},{"style":255},[7402],{"type":71,"value":7403},"      ttl",{"type":66,"tag":107,"props":7405,"children":7406},{"style":120},[7407],{"type":71,"value":263},{"type":66,"tag":107,"props":7409,"children":7410},{"style":3962},[7411],{"type":71,"value":6048},{"type":66,"tag":107,"props":7413,"children":7414},{"style":120},[7415],{"type":71,"value":134},{"type":66,"tag":107,"props":7417,"children":7418},{"style":2081},[7419],{"type":71,"value":7420}," \u002F\u002F Cache entries expire after 60 seconds\n",{"type":66,"tag":107,"props":7422,"children":7423},{"class":109,"line":435},[7424,7429,7433,7438,7442],{"type":66,"tag":107,"props":7425,"children":7426},{"style":255},[7427],{"type":71,"value":7428},"      maxSize",{"type":66,"tag":107,"props":7430,"children":7431},{"style":120},[7432],{"type":71,"value":263},{"type":66,"tag":107,"props":7434,"children":7435},{"style":3962},[7436],{"type":71,"value":7437}," 50",{"type":66,"tag":107,"props":7439,"children":7440},{"style":120},[7441],{"type":71,"value":134},{"type":66,"tag":107,"props":7443,"children":7444},{"style":2081},[7445],{"type":71,"value":7446}," \u002F\u002F Max 50 entries (LRU eviction)\n",{"type":66,"tag":107,"props":7448,"children":7449},{"class":109,"line":444},[7450,7455,7459,7463,7467,7472,7476,7480,7484],{"type":66,"tag":107,"props":7451,"children":7452},{"style":255},[7453],{"type":71,"value":7454},"      toolNames",{"type":66,"tag":107,"props":7456,"children":7457},{"style":120},[7458],{"type":71,"value":263},{"type":66,"tag":107,"props":7460,"children":7461},{"style":126},[7462],{"type":71,"value":5704},{"type":66,"tag":107,"props":7464,"children":7465},{"style":120},[7466],{"type":71,"value":276},{"type":66,"tag":107,"props":7468,"children":7469},{"style":157},[7470],{"type":71,"value":7471},"getWeather",{"type":66,"tag":107,"props":7473,"children":7474},{"style":120},[7475],{"type":71,"value":276},{"type":66,"tag":107,"props":7477,"children":7478},{"style":126},[7479],{"type":71,"value":5744},{"type":66,"tag":107,"props":7481,"children":7482},{"style":120},[7483],{"type":71,"value":134},{"type":66,"tag":107,"props":7485,"children":7486},{"style":2081},[7487],{"type":71,"value":7488}," \u002F\u002F Only cache specific tools\n",{"type":66,"tag":107,"props":7490,"children":7491},{"class":109,"line":486},[7492,7496,7500],{"type":66,"tag":107,"props":7493,"children":7494},{"style":120},[7495],{"type":71,"value":4166},{"type":66,"tag":107,"props":7497,"children":7498},{"style":126},[7499],{"type":71,"value":290},{"type":66,"tag":107,"props":7501,"children":7502},{"style":120},[7503],{"type":71,"value":295},{"type":66,"tag":107,"props":7505,"children":7506},{"class":109,"line":569},[7507,7511],{"type":66,"tag":107,"props":7508,"children":7509},{"style":126},[7510],{"type":71,"value":672},{"type":66,"tag":107,"props":7512,"children":7513},{"style":120},[7514],{"type":71,"value":295},{"type":66,"tag":107,"props":7516,"children":7517},{"class":109,"line":577},[7518,7522],{"type":66,"tag":107,"props":7519,"children":7520},{"style":120},[7521],{"type":71,"value":685},{"type":66,"tag":107,"props":7523,"children":7524},{"style":126},[7525],{"type":71,"value":432},{"type":66,"tag":77,"props":7527,"children":7528},{},[7529,7531,7537,7539,7545,7547,7553,7555,7561,7563,7569,7571,7577],{"type":71,"value":7530},"Options: ",{"type":66,"tag":103,"props":7532,"children":7534},{"className":7533},[],[7535],{"type":71,"value":7536},"maxSize",{"type":71,"value":7538}," (default 100), ",{"type":66,"tag":103,"props":7540,"children":7542},{"className":7541},[],[7543],{"type":71,"value":7544},"ttl",{"type":71,"value":7546}," (default Infinity), ",{"type":66,"tag":103,"props":7548,"children":7550},{"className":7549},[],[7551],{"type":71,"value":7552},"toolNames",{"type":71,"value":7554}," (default all),\n",{"type":66,"tag":103,"props":7556,"children":7558},{"className":7557},[],[7559],{"type":71,"value":7560},"keyFn",{"type":71,"value":7562}," (custom cache key), ",{"type":66,"tag":103,"props":7564,"children":7566},{"className":7565},[],[7567],{"type":71,"value":7568},"storage",{"type":71,"value":7570}," (custom backend like Redis). See\n",{"type":66,"tag":103,"props":7572,"children":7574},{"className":7573},[],[7575],{"type":71,"value":7576},"docs\u002Fadvanced\u002Fmiddleware.md",{"type":71,"value":7578}," for custom storage examples.",{"type":66,"tag":89,"props":7580,"children":7582},{"id":7581},"server-state-persistence-withpersistence",[7583],{"type":71,"value":7584},"Server State Persistence: withPersistence",{"type":66,"tag":77,"props":7586,"children":7587},{},[7588,7594,7596,7602,7604,7610,7612,7617,7619,7624,7626,7631,7633,7639],{"type":66,"tag":103,"props":7589,"children":7591},{"className":7590},[],[7592],{"type":71,"value":7593},"withPersistence(persistence)",{"type":71,"value":7595}," (from ",{"type":66,"tag":103,"props":7597,"children":7599},{"className":7598},[],[7600],{"type":71,"value":7601},"@tanstack\u002Fai-persistence",{"type":71,"value":7603},") is a\n",{"type":66,"tag":103,"props":7605,"children":7607},{"className":7606},[],[7608],{"type":71,"value":7609},"ChatMiddleware",{"type":71,"value":7611}," that persists ",{"type":66,"tag":81,"props":7613,"children":7614},{},[7615],{"type":71,"value":7616},"state",{"type":71,"value":7618}," for ",{"type":66,"tag":103,"props":7620,"children":7622},{"className":7621},[],[7623],{"type":71,"value":1266},{"type":71,"value":7625}," — thread messages, run\nrecords (status\u002Ftiming\u002Fusage\u002Ferrors), and interrupt state — to a backend store.\nAdd it to the ",{"type":66,"tag":103,"props":7627,"children":7629},{"className":7628},[],[7630],{"type":71,"value":22},{"type":71,"value":7632}," array like any other middleware. It never mutates the\nchunk stream; replaying a dropped\u002Freloaded ",{"type":66,"tag":7634,"props":7635,"children":7636},"em",{},[7637],{"type":71,"value":7638},"stream",{"type":71,"value":7640}," is a separate transport-layer\nconcern (see ai-core\u002Fchat-experience\u002FSKILL.md resumability, not this middleware).",{"type":66,"tag":96,"props":7642,"children":7644},{"className":98,"code":7643,"language":45,"meta":100,"style":100},"import {\n  chat,\n  chatParamsFromRequest,\n  toServerSentEventsResponse,\n} from '@tanstack\u002Fai'\nimport { openaiText } from '@tanstack\u002Fai-openai'\nimport { withPersistence, memoryPersistence } from '@tanstack\u002Fai-persistence'\n\n\u002F\u002F memoryPersistence() is the in-process reference backend (dev\u002Ftests). For a\n\u002F\u002F durable one, implement the store contracts against your database — see the\n\u002F\u002F @tanstack\u002Fai-persistence skills.\nconst persistence = memoryPersistence()\n\nexport async function POST(request: Request) {\n  const params = await chatParamsFromRequest(request)\n\n  const stream = chat({\n    adapter: openaiText('gpt-5.5'),\n    messages: params.messages,\n    threadId: params.threadId,\n    runId: params.runId,\n    ...(params.resume ? { resume: params.resume } : {}),\n    middleware: [withPersistence(persistence)],\n  })\n\n  return toServerSentEventsResponse(stream)\n}\n",[7645],{"type":66,"tag":103,"props":7646,"children":7647},{"__ignoreMap":100},[7648,7659,7670,7682,7693,7716,7751,7796,7803,7811,7819,7827,7852,7859,7908,7947,7954,7982,8023,8052,8081,8110,8186,8224,8236,8243,8266],{"type":66,"tag":107,"props":7649,"children":7650},{"class":109,"line":110},[7651,7655],{"type":66,"tag":107,"props":7652,"children":7653},{"style":114},[7654],{"type":71,"value":117},{"type":66,"tag":107,"props":7656,"children":7657},{"style":120},[7658],{"type":71,"value":373},{"type":66,"tag":107,"props":7660,"children":7661},{"class":109,"line":168},[7662,7666],{"type":66,"tag":107,"props":7663,"children":7664},{"style":126},[7665],{"type":71,"value":2248},{"type":66,"tag":107,"props":7667,"children":7668},{"style":120},[7669],{"type":71,"value":295},{"type":66,"tag":107,"props":7671,"children":7672},{"class":109,"line":206},[7673,7678],{"type":66,"tag":107,"props":7674,"children":7675},{"style":126},[7676],{"type":71,"value":7677},"  chatParamsFromRequest",{"type":66,"tag":107,"props":7679,"children":7680},{"style":120},[7681],{"type":71,"value":295},{"type":66,"tag":107,"props":7683,"children":7684},{"class":109,"line":216},[7685,7689],{"type":66,"tag":107,"props":7686,"children":7687},{"style":126},[7688],{"type":71,"value":2260},{"type":66,"tag":107,"props":7690,"children":7691},{"style":120},[7692],{"type":71,"value":295},{"type":66,"tag":107,"props":7694,"children":7695},{"class":109,"line":251},[7696,7700,7704,7708,7712],{"type":66,"tag":107,"props":7697,"children":7698},{"style":120},[7699],{"type":71,"value":685},{"type":66,"tag":107,"props":7701,"children":7702},{"style":114},[7703],{"type":71,"value":149},{"type":66,"tag":107,"props":7705,"children":7706},{"style":120},[7707],{"type":71,"value":154},{"type":66,"tag":107,"props":7709,"children":7710},{"style":157},[7711],{"type":71,"value":160},{"type":66,"tag":107,"props":7713,"children":7714},{"style":120},[7715],{"type":71,"value":165},{"type":66,"tag":107,"props":7717,"children":7718},{"class":109,"line":298},[7719,7723,7727,7731,7735,7739,7743,7747],{"type":66,"tag":107,"props":7720,"children":7721},{"style":114},[7722],{"type":71,"value":117},{"type":66,"tag":107,"props":7724,"children":7725},{"style":120},[7726],{"type":71,"value":123},{"type":66,"tag":107,"props":7728,"children":7729},{"style":126},[7730],{"type":71,"value":182},{"type":66,"tag":107,"props":7732,"children":7733},{"style":120},[7734],{"type":71,"value":144},{"type":66,"tag":107,"props":7736,"children":7737},{"style":114},[7738],{"type":71,"value":149},{"type":66,"tag":107,"props":7740,"children":7741},{"style":120},[7742],{"type":71,"value":154},{"type":66,"tag":107,"props":7744,"children":7745},{"style":157},[7746],{"type":71,"value":199},{"type":66,"tag":107,"props":7748,"children":7749},{"style":120},[7750],{"type":71,"value":165},{"type":66,"tag":107,"props":7752,"children":7753},{"class":109,"line":311},[7754,7758,7762,7767,7771,7776,7780,7784,7788,7792],{"type":66,"tag":107,"props":7755,"children":7756},{"style":114},[7757],{"type":71,"value":117},{"type":66,"tag":107,"props":7759,"children":7760},{"style":120},[7761],{"type":71,"value":123},{"type":66,"tag":107,"props":7763,"children":7764},{"style":126},[7765],{"type":71,"value":7766}," withPersistence",{"type":66,"tag":107,"props":7768,"children":7769},{"style":120},[7770],{"type":71,"value":134},{"type":66,"tag":107,"props":7772,"children":7773},{"style":126},[7774],{"type":71,"value":7775}," memoryPersistence",{"type":66,"tag":107,"props":7777,"children":7778},{"style":120},[7779],{"type":71,"value":144},{"type":66,"tag":107,"props":7781,"children":7782},{"style":114},[7783],{"type":71,"value":149},{"type":66,"tag":107,"props":7785,"children":7786},{"style":120},[7787],{"type":71,"value":154},{"type":66,"tag":107,"props":7789,"children":7790},{"style":157},[7791],{"type":71,"value":7601},{"type":66,"tag":107,"props":7793,"children":7794},{"style":120},[7795],{"type":71,"value":165},{"type":66,"tag":107,"props":7797,"children":7798},{"class":109,"line":329},[7799],{"type":66,"tag":107,"props":7800,"children":7801},{"emptyLinePlaceholder":210},[7802],{"type":71,"value":213},{"type":66,"tag":107,"props":7804,"children":7805},{"class":109,"line":338},[7806],{"type":66,"tag":107,"props":7807,"children":7808},{"style":2081},[7809],{"type":71,"value":7810},"\u002F\u002F memoryPersistence() is the in-process reference backend (dev\u002Ftests). For a\n",{"type":66,"tag":107,"props":7812,"children":7813},{"class":109,"line":376},[7814],{"type":66,"tag":107,"props":7815,"children":7816},{"style":2081},[7817],{"type":71,"value":7818},"\u002F\u002F durable one, implement the store contracts against your database — see the\n",{"type":66,"tag":107,"props":7820,"children":7821},{"class":109,"line":435},[7822],{"type":66,"tag":107,"props":7823,"children":7824},{"style":2081},[7825],{"type":71,"value":7826},"\u002F\u002F @tanstack\u002Fai-persistence skills.\n",{"type":66,"tag":107,"props":7828,"children":7829},{"class":109,"line":444},[7830,7834,7839,7843,7847],{"type":66,"tag":107,"props":7831,"children":7832},{"style":220},[7833],{"type":71,"value":223},{"type":66,"tag":107,"props":7835,"children":7836},{"style":126},[7837],{"type":71,"value":7838}," persistence ",{"type":66,"tag":107,"props":7840,"children":7841},{"style":120},[7842],{"type":71,"value":233},{"type":66,"tag":107,"props":7844,"children":7845},{"style":236},[7846],{"type":71,"value":7775},{"type":66,"tag":107,"props":7848,"children":7849},{"style":126},[7850],{"type":71,"value":7851},"()\n",{"type":66,"tag":107,"props":7853,"children":7854},{"class":109,"line":486},[7855],{"type":66,"tag":107,"props":7856,"children":7857},{"emptyLinePlaceholder":210},[7858],{"type":71,"value":213},{"type":66,"tag":107,"props":7860,"children":7861},{"class":109,"line":569},[7862,7867,7872,7877,7882,7886,7891,7895,7900,7904],{"type":66,"tag":107,"props":7863,"children":7864},{"style":114},[7865],{"type":71,"value":7866},"export",{"type":66,"tag":107,"props":7868,"children":7869},{"style":220},[7870],{"type":71,"value":7871}," async",{"type":66,"tag":107,"props":7873,"children":7874},{"style":220},[7875],{"type":71,"value":7876}," function",{"type":66,"tag":107,"props":7878,"children":7879},{"style":236},[7880],{"type":71,"value":7881}," POST",{"type":66,"tag":107,"props":7883,"children":7884},{"style":120},[7885],{"type":71,"value":243},{"type":66,"tag":107,"props":7887,"children":7888},{"style":356},[7889],{"type":71,"value":7890},"request",{"type":66,"tag":107,"props":7892,"children":7893},{"style":120},[7894],{"type":71,"value":263},{"type":66,"tag":107,"props":7896,"children":7897},{"style":1984},[7898],{"type":71,"value":7899}," Request",{"type":66,"tag":107,"props":7901,"children":7902},{"style":120},[7903],{"type":71,"value":290},{"type":66,"tag":107,"props":7905,"children":7906},{"style":120},[7907],{"type":71,"value":373},{"type":66,"tag":107,"props":7909,"children":7910},{"class":109,"line":577},[7911,7916,7921,7925,7930,7935,7939,7943],{"type":66,"tag":107,"props":7912,"children":7913},{"style":220},[7914],{"type":71,"value":7915},"  const",{"type":66,"tag":107,"props":7917,"children":7918},{"style":126},[7919],{"type":71,"value":7920}," params",{"type":66,"tag":107,"props":7922,"children":7923},{"style":120},[7924],{"type":71,"value":2371},{"type":66,"tag":107,"props":7926,"children":7927},{"style":114},[7928],{"type":71,"value":7929}," await",{"type":66,"tag":107,"props":7931,"children":7932},{"style":236},[7933],{"type":71,"value":7934}," chatParamsFromRequest",{"type":66,"tag":107,"props":7936,"children":7937},{"style":255},[7938],{"type":71,"value":243},{"type":66,"tag":107,"props":7940,"children":7941},{"style":126},[7942],{"type":71,"value":7890},{"type":66,"tag":107,"props":7944,"children":7945},{"style":255},[7946],{"type":71,"value":432},{"type":66,"tag":107,"props":7948,"children":7949},{"class":109,"line":618},[7950],{"type":66,"tag":107,"props":7951,"children":7952},{"emptyLinePlaceholder":210},[7953],{"type":71,"value":213},{"type":66,"tag":107,"props":7955,"children":7956},{"class":109,"line":649},[7957,7961,7966,7970,7974,7978],{"type":66,"tag":107,"props":7958,"children":7959},{"style":220},[7960],{"type":71,"value":7915},{"type":66,"tag":107,"props":7962,"children":7963},{"style":126},[7964],{"type":71,"value":7965}," stream",{"type":66,"tag":107,"props":7967,"children":7968},{"style":120},[7969],{"type":71,"value":2371},{"type":66,"tag":107,"props":7971,"children":7972},{"style":236},[7973],{"type":71,"value":129},{"type":66,"tag":107,"props":7975,"children":7976},{"style":255},[7977],{"type":71,"value":243},{"type":66,"tag":107,"props":7979,"children":7980},{"style":120},[7981],{"type":71,"value":248},{"type":66,"tag":107,"props":7983,"children":7984},{"class":109,"line":657},[7985,7990,7994,7998,8002,8006,8011,8015,8019],{"type":66,"tag":107,"props":7986,"children":7987},{"style":255},[7988],{"type":71,"value":7989},"    adapter",{"type":66,"tag":107,"props":7991,"children":7992},{"style":120},[7993],{"type":71,"value":263},{"type":66,"tag":107,"props":7995,"children":7996},{"style":236},[7997],{"type":71,"value":182},{"type":66,"tag":107,"props":7999,"children":8000},{"style":255},[8001],{"type":71,"value":243},{"type":66,"tag":107,"props":8003,"children":8004},{"style":120},[8005],{"type":71,"value":276},{"type":66,"tag":107,"props":8007,"children":8008},{"style":157},[8009],{"type":71,"value":8010},"gpt-5.5",{"type":66,"tag":107,"props":8012,"children":8013},{"style":120},[8014],{"type":71,"value":276},{"type":66,"tag":107,"props":8016,"children":8017},{"style":255},[8018],{"type":71,"value":290},{"type":66,"tag":107,"props":8020,"children":8021},{"style":120},[8022],{"type":71,"value":295},{"type":66,"tag":107,"props":8024,"children":8025},{"class":109,"line":666},[8026,8031,8035,8039,8043,8048],{"type":66,"tag":107,"props":8027,"children":8028},{"style":255},[8029],{"type":71,"value":8030},"    messages",{"type":66,"tag":107,"props":8032,"children":8033},{"style":120},[8034],{"type":71,"value":263},{"type":66,"tag":107,"props":8036,"children":8037},{"style":126},[8038],{"type":71,"value":7920},{"type":66,"tag":107,"props":8040,"children":8041},{"style":120},[8042],{"type":71,"value":387},{"type":66,"tag":107,"props":8044,"children":8045},{"style":126},[8046],{"type":71,"value":8047},"messages",{"type":66,"tag":107,"props":8049,"children":8050},{"style":120},[8051],{"type":71,"value":295},{"type":66,"tag":107,"props":8053,"children":8054},{"class":109,"line":679},[8055,8060,8064,8068,8072,8077],{"type":66,"tag":107,"props":8056,"children":8057},{"style":255},[8058],{"type":71,"value":8059},"    threadId",{"type":66,"tag":107,"props":8061,"children":8062},{"style":120},[8063],{"type":71,"value":263},{"type":66,"tag":107,"props":8065,"children":8066},{"style":126},[8067],{"type":71,"value":7920},{"type":66,"tag":107,"props":8069,"children":8070},{"style":120},[8071],{"type":71,"value":387},{"type":66,"tag":107,"props":8073,"children":8074},{"style":126},[8075],{"type":71,"value":8076},"threadId",{"type":66,"tag":107,"props":8078,"children":8079},{"style":120},[8080],{"type":71,"value":295},{"type":66,"tag":107,"props":8082,"children":8083},{"class":109,"line":692},[8084,8089,8093,8097,8101,8106],{"type":66,"tag":107,"props":8085,"children":8086},{"style":255},[8087],{"type":71,"value":8088},"    runId",{"type":66,"tag":107,"props":8090,"children":8091},{"style":120},[8092],{"type":71,"value":263},{"type":66,"tag":107,"props":8094,"children":8095},{"style":126},[8096],{"type":71,"value":7920},{"type":66,"tag":107,"props":8098,"children":8099},{"style":120},[8100],{"type":71,"value":387},{"type":66,"tag":107,"props":8102,"children":8103},{"style":126},[8104],{"type":71,"value":8105},"runId",{"type":66,"tag":107,"props":8107,"children":8108},{"style":120},[8109],{"type":71,"value":295},{"type":66,"tag":107,"props":8111,"children":8112},{"class":109,"line":700},[8113,8118,8122,8127,8131,8136,8140,8144,8149,8153,8157,8161,8165,8169,8173,8178,8182],{"type":66,"tag":107,"props":8114,"children":8115},{"style":120},[8116],{"type":71,"value":8117},"    ...",{"type":66,"tag":107,"props":8119,"children":8120},{"style":255},[8121],{"type":71,"value":243},{"type":66,"tag":107,"props":8123,"children":8124},{"style":126},[8125],{"type":71,"value":8126},"params",{"type":66,"tag":107,"props":8128,"children":8129},{"style":120},[8130],{"type":71,"value":387},{"type":66,"tag":107,"props":8132,"children":8133},{"style":126},[8134],{"type":71,"value":8135},"resume",{"type":66,"tag":107,"props":8137,"children":8138},{"style":120},[8139],{"type":71,"value":6984},{"type":66,"tag":107,"props":8141,"children":8142},{"style":120},[8143],{"type":71,"value":123},{"type":66,"tag":107,"props":8145,"children":8146},{"style":255},[8147],{"type":71,"value":8148}," resume",{"type":66,"tag":107,"props":8150,"children":8151},{"style":120},[8152],{"type":71,"value":263},{"type":66,"tag":107,"props":8154,"children":8155},{"style":126},[8156],{"type":71,"value":7920},{"type":66,"tag":107,"props":8158,"children":8159},{"style":120},[8160],{"type":71,"value":387},{"type":66,"tag":107,"props":8162,"children":8163},{"style":126},[8164],{"type":71,"value":8135},{"type":66,"tag":107,"props":8166,"children":8167},{"style":120},[8168],{"type":71,"value":144},{"type":66,"tag":107,"props":8170,"children":8171},{"style":120},[8172],{"type":71,"value":6995},{"type":66,"tag":107,"props":8174,"children":8175},{"style":120},[8176],{"type":71,"value":8177}," {}",{"type":66,"tag":107,"props":8179,"children":8180},{"style":255},[8181],{"type":71,"value":290},{"type":66,"tag":107,"props":8183,"children":8184},{"style":120},[8185],{"type":71,"value":295},{"type":66,"tag":107,"props":8187,"children":8188},{"class":109,"line":2866},[8189,8194,8198,8202,8207,8211,8216,8220],{"type":66,"tag":107,"props":8190,"children":8191},{"style":255},[8192],{"type":71,"value":8193},"    middleware",{"type":66,"tag":107,"props":8195,"children":8196},{"style":120},[8197],{"type":71,"value":263},{"type":66,"tag":107,"props":8199,"children":8200},{"style":255},[8201],{"type":71,"value":5704},{"type":66,"tag":107,"props":8203,"children":8204},{"style":236},[8205],{"type":71,"value":8206},"withPersistence",{"type":66,"tag":107,"props":8208,"children":8209},{"style":255},[8210],{"type":71,"value":243},{"type":66,"tag":107,"props":8212,"children":8213},{"style":126},[8214],{"type":71,"value":8215},"persistence",{"type":66,"tag":107,"props":8217,"children":8218},{"style":255},[8219],{"type":71,"value":7181},{"type":66,"tag":107,"props":8221,"children":8222},{"style":120},[8223],{"type":71,"value":295},{"type":66,"tag":107,"props":8225,"children":8226},{"class":109,"line":2896},[8227,8232],{"type":66,"tag":107,"props":8228,"children":8229},{"style":120},[8230],{"type":71,"value":8231},"  }",{"type":66,"tag":107,"props":8233,"children":8234},{"style":255},[8235],{"type":71,"value":432},{"type":66,"tag":107,"props":8237,"children":8238},{"class":109,"line":2933},[8239],{"type":66,"tag":107,"props":8240,"children":8241},{"emptyLinePlaceholder":210},[8242],{"type":71,"value":213},{"type":66,"tag":107,"props":8244,"children":8245},{"class":109,"line":2963},[8246,8250,8254,8258,8262],{"type":66,"tag":107,"props":8247,"children":8248},{"style":114},[8249],{"type":71,"value":6512},{"type":66,"tag":107,"props":8251,"children":8252},{"style":236},[8253],{"type":71,"value":139},{"type":66,"tag":107,"props":8255,"children":8256},{"style":255},[8257],{"type":71,"value":243},{"type":66,"tag":107,"props":8259,"children":8260},{"style":126},[8261],{"type":71,"value":7638},{"type":66,"tag":107,"props":8263,"children":8264},{"style":255},[8265],{"type":71,"value":432},{"type":66,"tag":107,"props":8267,"children":8268},{"class":109,"line":2980},[8269],{"type":66,"tag":107,"props":8270,"children":8271},{"style":120},[8272],{"type":71,"value":2092},{"type":66,"tag":1379,"props":8274,"children":8276},{"id":8275},"authoritative-history-contract",[8277],{"type":71,"value":8278},"Authoritative-history contract",{"type":66,"tag":77,"props":8280,"children":8281},{},[8282,8284,8289],{"type":71,"value":8283},"The middleware treats each request's ",{"type":66,"tag":103,"props":8285,"children":8287},{"className":8286},[],[8288],{"type":71,"value":8047},{"type":71,"value":8290}," as the source of truth for the\nthread:",{"type":66,"tag":1572,"props":8292,"children":8293},{},[8294,8323],{"type":66,"tag":1576,"props":8295,"children":8296},{},[8297,8307,8309,8314,8316,8321],{"type":66,"tag":81,"props":8298,"children":8299},{},[8300,8302],{"type":71,"value":8301},"Non-empty ",{"type":66,"tag":103,"props":8303,"children":8305},{"className":8304},[],[8306],{"type":71,"value":8047},{"type":71,"value":8308}," → on a successful finish (and at an interrupt\nboundary) the middleware ",{"type":66,"tag":81,"props":8310,"children":8311},{},[8312],{"type":71,"value":8313},"overwrites",{"type":71,"value":8315}," the entire stored thread with that\narray. Post the ",{"type":66,"tag":81,"props":8317,"children":8318},{},[8319],{"type":71,"value":8320},"complete",{"type":71,"value":8322}," transcript, never just the newest message(s) — a\ndelta would replace and destroy the stored history.",{"type":66,"tag":1576,"props":8324,"children":8325},{},[8326,8336,8338,8343],{"type":66,"tag":81,"props":8327,"children":8328},{},[8329,8331],{"type":71,"value":8330},"Empty ",{"type":66,"tag":103,"props":8332,"children":8334},{"className":8333},[],[8335],{"type":71,"value":8047},{"type":71,"value":8337}," → the middleware ",{"type":66,"tag":81,"props":8339,"children":8340},{},[8341],{"type":71,"value":8342},"loads",{"type":71,"value":8344}," the stored thread and runs the\nturn from the server's copy. This is how you continue a conversation without\nresending history from the client.",{"type":66,"tag":1379,"props":8346,"children":8348},{"id":8347},"backends",[8349],{"type":71,"value":8350},"Backends",{"type":66,"tag":77,"props":8352,"children":8353},{},[8354,8359,8361,8366,8368,8373,8374,8380,8381,8387,8388,8394,8396,8402,8404,8409],{"type":66,"tag":103,"props":8355,"children":8357},{"className":8356},[],[8358],{"type":71,"value":7601},{"type":71,"value":8360}," ships ",{"type":66,"tag":81,"props":8362,"children":8363},{},[8364],{"type":71,"value":8365},"contracts, not a database backend",{"type":71,"value":8367},". It\nprovides the four store interfaces (",{"type":66,"tag":103,"props":8369,"children":8371},{"className":8370},[],[8372],{"type":71,"value":8047},{"type":71,"value":742},{"type":66,"tag":103,"props":8375,"children":8377},{"className":8376},[],[8378],{"type":71,"value":8379},"runs",{"type":71,"value":742},{"type":66,"tag":103,"props":8382,"children":8384},{"className":8383},[],[8385],{"type":71,"value":8386},"interrupts",{"type":71,"value":295},{"type":66,"tag":103,"props":8389,"children":8391},{"className":8390},[],[8392],{"type":71,"value":8393},"metadata",{"type":71,"value":8395},"), the middleware that drives them, ",{"type":66,"tag":103,"props":8397,"children":8399},{"className":8398},[],[8400],{"type":71,"value":8401},"memoryPersistence()",{"type":71,"value":8403}," for\ndev\u002Ftests, and a conformance testkit. For anything durable you implement the\nstores against your own database and pass the result to ",{"type":66,"tag":103,"props":8405,"children":8407},{"className":8406},[],[8408],{"type":71,"value":8206},{"type":71,"value":387},{"type":66,"tag":77,"props":8411,"children":8412},{},[8413,8415,8421,8423,8429,8431,8437,8439,8444],{"type":71,"value":8414},"Annotate your factory with a named shape (",{"type":66,"tag":103,"props":8416,"children":8418},{"className":8417},[],[8419],{"type":71,"value":8420},"ChatPersistence",{"type":71,"value":8422}," \u002F\n",{"type":66,"tag":103,"props":8424,"children":8426},{"className":8425},[],[8427],{"type":71,"value":8428},"ChatTranscriptPersistence",{"type":71,"value":8430},") — bare ",{"type":66,"tag":103,"props":8432,"children":8434},{"className":8433},[],[8435],{"type":71,"value":8436},"AIPersistence",{"type":71,"value":8438}," is the all-optional bag and\n",{"type":66,"tag":103,"props":8440,"children":8442},{"className":8441},[],[8443],{"type":71,"value":8206},{"type":71,"value":8445}," rejects it.",{"type":66,"tag":77,"props":8447,"children":8448},{},[8449,8451,8455,8457,8463,8465,8471,8473,8479],{"type":71,"value":8450},"Locks are separate from state and are ",{"type":66,"tag":81,"props":8452,"children":8453},{},[8454],{"type":71,"value":1315},{"type":71,"value":8456}," a ",{"type":66,"tag":103,"props":8458,"children":8460},{"className":8459},[],[8461],{"type":71,"value":8462},"stores",{"type":71,"value":8464}," key: wire a\n",{"type":66,"tag":103,"props":8466,"children":8468},{"className":8467},[],[8469],{"type":71,"value":8470},"LockStore",{"type":71,"value":8472}," with ",{"type":66,"tag":103,"props":8474,"children":8476},{"className":8475},[],[8477],{"type":71,"value":8478},"withLocks(lockStore)",{"type":71,"value":387},{"type":66,"tag":77,"props":8481,"children":8482},{},[8483,8488,8490,8496],{"type":66,"tag":81,"props":8484,"children":8485},{},[8486],{"type":71,"value":8487},"Full guidance lives in the package's own skills",{"type":71,"value":8489}," — start at\n",{"type":66,"tag":103,"props":8491,"children":8493},{"className":8492},[],[8494],{"type":71,"value":8495},"node_modules\u002F@tanstack\u002Fai-persistence\u002Fskills\u002Fai-persistence\u002FSKILL.md",{"type":71,"value":8497},",\nwhich routes to the server, client, stores, locks, and adapter-recipe\n(Drizzle \u002F Prisma \u002F Cloudflare) sub-skills.",{"type":66,"tag":1379,"props":8499,"children":8501},{"id":8500},"resume-reconstruction-is-the-middlewares-job-server-authoritative-path",[8502],{"type":71,"value":8503},"Resume reconstruction is the middleware's job (server-authoritative path)",{"type":66,"tag":77,"props":8505,"children":8506},{},[8507,8509,8514,8516,8521,8523,8528,8530,8535,8537,8548,8550,8561,8563,8568],{"type":71,"value":8508},"When a thread has pending interrupts, the middleware ",{"type":66,"tag":81,"props":8510,"children":8511},{},[8512],{"type":71,"value":8513},"records",{"type":71,"value":8515}," them and\n",{"type":66,"tag":81,"props":8517,"children":8518},{},[8519],{"type":71,"value":8520},"gates",{"type":71,"value":8522}," new input: a request that carries pending interrupts must include a\n",{"type":66,"tag":103,"props":8524,"children":8526},{"className":8525},[],[8527],{"type":71,"value":8135},{"type":71,"value":8529}," batch that references them, or ",{"type":66,"tag":103,"props":8531,"children":8533},{"className":8532},[],[8534],{"type":71,"value":850},{"type":71,"value":8536}," throws. On a valid resume\nbatch the middleware also ",{"type":66,"tag":81,"props":8538,"children":8539},{},[8540,8542],{"type":71,"value":8541},"builds ",{"type":66,"tag":103,"props":8543,"children":8545},{"className":8544},[],[8546],{"type":71,"value":8547},"ChatResumeToolState",{"type":71,"value":8549}," (approvals \u002F\nclient-tool results) and ",{"type":66,"tag":81,"props":8551,"children":8552},{},[8553,8555],{"type":71,"value":8554},"clears ",{"type":66,"tag":103,"props":8556,"children":8558},{"className":8557},[],[8559],{"type":71,"value":8560},"config.resume",{"type":71,"value":8562}," so the chat engine skips\nits ephemeral reconstruction — that path needs client message history the\npersistence flow deliberately omits when the server owns the transcript.\nResumes accepted in ",{"type":66,"tag":103,"props":8564,"children":8566},{"className":8565},[],[8567],{"type":71,"value":850},{"type":71,"value":8569}," are committed (marked resolved\u002Fcancelled) only\nonce the run reaches a successful boundary, so a provider failure between\naccepting a resume and finishing leaves the interrupt pending and a retry with\nthe same resume succeeds.",{"type":66,"tag":73,"props":8571,"children":8572},{},[8573],{"type":66,"tag":77,"props":8574,"children":8575},{},[8576,8578,8584],{"type":71,"value":8577},"A companion ",{"type":66,"tag":103,"props":8579,"children":8581},{"className":8580},[],[8582],{"type":71,"value":8583},"withGenerationPersistence(persistence)",{"type":71,"value":8585}," tracks run records for\nnon-chat generation activities (image, audio, TTS, video, transcription).",{"type":66,"tag":77,"props":8587,"children":8588},{},[8589],{"type":71,"value":8590},"Source: docs\u002Fpersistence\u002Foverview.md",{"type":66,"tag":89,"props":8592,"children":8594},{"id":8593},"sandbox-file-event-hooks-sandbox-group",[8595,8597,8603],{"type":71,"value":8596},"Sandbox File-Event Hooks (",{"type":66,"tag":103,"props":8598,"children":8600},{"className":8599},[],[8601],{"type":71,"value":8602},"sandbox",{"type":71,"value":8604}," group)",{"type":66,"tag":77,"props":8606,"children":8607},{},[8608,8610,8616,8618,8624,8626,8632,8633,8639,8641,8646,8648,8653],{"type":71,"value":8609},"Declare a ",{"type":66,"tag":103,"props":8611,"children":8613},{"className":8612},[],[8614],{"type":71,"value":8615},"sandbox: ChatSandboxHooks",{"type":71,"value":8617}," group on ",{"type":66,"tag":103,"props":8619,"children":8621},{"className":8620},[],[8622],{"type":71,"value":8623},"defineChatMiddleware",{"type":71,"value":8625}," to react\nto every file created\u002Fchanged\u002Fdeleted inside a sandbox provided by\n",{"type":66,"tag":103,"props":8627,"children":8629},{"className":8628},[],[8630],{"type":71,"value":8631},"withSandbox",{"type":71,"value":7595},{"type":66,"tag":103,"props":8634,"children":8636},{"className":8635},[],[8637],{"type":71,"value":8638},"@tanstack\u002Fai-sandbox",{"type":71,"value":8640},"). These fire ",{"type":66,"tag":81,"props":8642,"children":8643},{},[8644],{"type":71,"value":8645},"per-run",{"type":71,"value":8647},",\nserver-side, and each handler receives the run's ",{"type":66,"tag":103,"props":8649,"children":8651},{"className":8650},[],[8652],{"type":71,"value":732},{"type":71,"value":8654}," as\nthe first argument:",{"type":66,"tag":96,"props":8656,"children":8658},{"className":98,"code":8657,"language":45,"meta":100,"style":100},"import { defineChatMiddleware } from '@tanstack\u002Fai'\nimport { db } from '.\u002Fdb'\n\nconst auditMiddleware = defineChatMiddleware({\n  name: 'audit',\n  sandbox: {\n    onFile: (ctx, e) => console.log(ctx.runId, e.type, e.path),\n    onFileCreate: (ctx, e) => db.log({ run: ctx.runId, event: e }),\n  },\n})\n",[8659],{"type":66,"tag":103,"props":8660,"children":8661},{"__ignoreMap":100},[8662,8698,8735,8742,8770,8798,8814,8913,9020,9027],{"type":66,"tag":107,"props":8663,"children":8664},{"class":109,"line":110},[8665,8669,8673,8678,8682,8686,8690,8694],{"type":66,"tag":107,"props":8666,"children":8667},{"style":114},[8668],{"type":71,"value":117},{"type":66,"tag":107,"props":8670,"children":8671},{"style":120},[8672],{"type":71,"value":123},{"type":66,"tag":107,"props":8674,"children":8675},{"style":126},[8676],{"type":71,"value":8677}," defineChatMiddleware",{"type":66,"tag":107,"props":8679,"children":8680},{"style":120},[8681],{"type":71,"value":144},{"type":66,"tag":107,"props":8683,"children":8684},{"style":114},[8685],{"type":71,"value":149},{"type":66,"tag":107,"props":8687,"children":8688},{"style":120},[8689],{"type":71,"value":154},{"type":66,"tag":107,"props":8691,"children":8692},{"style":157},[8693],{"type":71,"value":160},{"type":66,"tag":107,"props":8695,"children":8696},{"style":120},[8697],{"type":71,"value":165},{"type":66,"tag":107,"props":8699,"children":8700},{"class":109,"line":168},[8701,8705,8709,8714,8718,8722,8726,8731],{"type":66,"tag":107,"props":8702,"children":8703},{"style":114},[8704],{"type":71,"value":117},{"type":66,"tag":107,"props":8706,"children":8707},{"style":120},[8708],{"type":71,"value":123},{"type":66,"tag":107,"props":8710,"children":8711},{"style":126},[8712],{"type":71,"value":8713}," db",{"type":66,"tag":107,"props":8715,"children":8716},{"style":120},[8717],{"type":71,"value":144},{"type":66,"tag":107,"props":8719,"children":8720},{"style":114},[8721],{"type":71,"value":149},{"type":66,"tag":107,"props":8723,"children":8724},{"style":120},[8725],{"type":71,"value":154},{"type":66,"tag":107,"props":8727,"children":8728},{"style":157},[8729],{"type":71,"value":8730},".\u002Fdb",{"type":66,"tag":107,"props":8732,"children":8733},{"style":120},[8734],{"type":71,"value":165},{"type":66,"tag":107,"props":8736,"children":8737},{"class":109,"line":206},[8738],{"type":66,"tag":107,"props":8739,"children":8740},{"emptyLinePlaceholder":210},[8741],{"type":71,"value":213},{"type":66,"tag":107,"props":8743,"children":8744},{"class":109,"line":216},[8745,8749,8754,8758,8762,8766],{"type":66,"tag":107,"props":8746,"children":8747},{"style":220},[8748],{"type":71,"value":223},{"type":66,"tag":107,"props":8750,"children":8751},{"style":126},[8752],{"type":71,"value":8753}," auditMiddleware ",{"type":66,"tag":107,"props":8755,"children":8756},{"style":120},[8757],{"type":71,"value":233},{"type":66,"tag":107,"props":8759,"children":8760},{"style":236},[8761],{"type":71,"value":8677},{"type":66,"tag":107,"props":8763,"children":8764},{"style":126},[8765],{"type":71,"value":243},{"type":66,"tag":107,"props":8767,"children":8768},{"style":120},[8769],{"type":71,"value":248},{"type":66,"tag":107,"props":8771,"children":8772},{"class":109,"line":251},[8773,8777,8781,8785,8790,8794],{"type":66,"tag":107,"props":8774,"children":8775},{"style":255},[8776],{"type":71,"value":2383},{"type":66,"tag":107,"props":8778,"children":8779},{"style":120},[8780],{"type":71,"value":263},{"type":66,"tag":107,"props":8782,"children":8783},{"style":120},[8784],{"type":71,"value":154},{"type":66,"tag":107,"props":8786,"children":8787},{"style":157},[8788],{"type":71,"value":8789},"audit",{"type":66,"tag":107,"props":8791,"children":8792},{"style":120},[8793],{"type":71,"value":276},{"type":66,"tag":107,"props":8795,"children":8796},{"style":120},[8797],{"type":71,"value":295},{"type":66,"tag":107,"props":8799,"children":8800},{"class":109,"line":298},[8801,8806,8810],{"type":66,"tag":107,"props":8802,"children":8803},{"style":255},[8804],{"type":71,"value":8805},"  sandbox",{"type":66,"tag":107,"props":8807,"children":8808},{"style":120},[8809],{"type":71,"value":263},{"type":66,"tag":107,"props":8811,"children":8812},{"style":120},[8813],{"type":71,"value":373},{"type":66,"tag":107,"props":8815,"children":8816},{"class":109,"line":311},[8817,8822,8826,8830,8834,8838,8843,8847,8851,8855,8859,8863,8868,8872,8876,8880,8884,8888,8892,8896,8900,8904,8909],{"type":66,"tag":107,"props":8818,"children":8819},{"style":236},[8820],{"type":71,"value":8821},"    onFile",{"type":66,"tag":107,"props":8823,"children":8824},{"style":120},[8825],{"type":71,"value":263},{"type":66,"tag":107,"props":8827,"children":8828},{"style":120},[8829],{"type":71,"value":353},{"type":66,"tag":107,"props":8831,"children":8832},{"style":356},[8833],{"type":71,"value":359},{"type":66,"tag":107,"props":8835,"children":8836},{"style":120},[8837],{"type":71,"value":134},{"type":66,"tag":107,"props":8839,"children":8840},{"style":356},[8841],{"type":71,"value":8842}," e",{"type":66,"tag":107,"props":8844,"children":8845},{"style":120},[8846],{"type":71,"value":290},{"type":66,"tag":107,"props":8848,"children":8849},{"style":220},[8850],{"type":71,"value":368},{"type":66,"tag":107,"props":8852,"children":8853},{"style":126},[8854],{"type":71,"value":5210},{"type":66,"tag":107,"props":8856,"children":8857},{"style":120},[8858],{"type":71,"value":387},{"type":66,"tag":107,"props":8860,"children":8861},{"style":236},[8862],{"type":71,"value":392},{"type":66,"tag":107,"props":8864,"children":8865},{"style":126},[8866],{"type":71,"value":8867},"(ctx",{"type":66,"tag":107,"props":8869,"children":8870},{"style":120},[8871],{"type":71,"value":387},{"type":66,"tag":107,"props":8873,"children":8874},{"style":126},[8875],{"type":71,"value":8105},{"type":66,"tag":107,"props":8877,"children":8878},{"style":120},[8879],{"type":71,"value":134},{"type":66,"tag":107,"props":8881,"children":8882},{"style":126},[8883],{"type":71,"value":8842},{"type":66,"tag":107,"props":8885,"children":8886},{"style":120},[8887],{"type":71,"value":387},{"type":66,"tag":107,"props":8889,"children":8890},{"style":126},[8891],{"type":71,"value":4661},{"type":66,"tag":107,"props":8893,"children":8894},{"style":120},[8895],{"type":71,"value":134},{"type":66,"tag":107,"props":8897,"children":8898},{"style":126},[8899],{"type":71,"value":8842},{"type":66,"tag":107,"props":8901,"children":8902},{"style":120},[8903],{"type":71,"value":387},{"type":66,"tag":107,"props":8905,"children":8906},{"style":126},[8907],{"type":71,"value":8908},"path)",{"type":66,"tag":107,"props":8910,"children":8911},{"style":120},[8912],{"type":71,"value":295},{"type":66,"tag":107,"props":8914,"children":8915},{"class":109,"line":329},[8916,8921,8925,8929,8933,8937,8941,8945,8949,8953,8957,8961,8965,8969,8974,8978,8982,8986,8990,8994,8999,9003,9008,9012,9016],{"type":66,"tag":107,"props":8917,"children":8918},{"style":236},[8919],{"type":71,"value":8920},"    onFileCreate",{"type":66,"tag":107,"props":8922,"children":8923},{"style":120},[8924],{"type":71,"value":263},{"type":66,"tag":107,"props":8926,"children":8927},{"style":120},[8928],{"type":71,"value":353},{"type":66,"tag":107,"props":8930,"children":8931},{"style":356},[8932],{"type":71,"value":359},{"type":66,"tag":107,"props":8934,"children":8935},{"style":120},[8936],{"type":71,"value":134},{"type":66,"tag":107,"props":8938,"children":8939},{"style":356},[8940],{"type":71,"value":8842},{"type":66,"tag":107,"props":8942,"children":8943},{"style":120},[8944],{"type":71,"value":290},{"type":66,"tag":107,"props":8946,"children":8947},{"style":220},[8948],{"type":71,"value":368},{"type":66,"tag":107,"props":8950,"children":8951},{"style":126},[8952],{"type":71,"value":8713},{"type":66,"tag":107,"props":8954,"children":8955},{"style":120},[8956],{"type":71,"value":387},{"type":66,"tag":107,"props":8958,"children":8959},{"style":236},[8960],{"type":71,"value":392},{"type":66,"tag":107,"props":8962,"children":8963},{"style":126},[8964],{"type":71,"value":243},{"type":66,"tag":107,"props":8966,"children":8967},{"style":120},[8968],{"type":71,"value":501},{"type":66,"tag":107,"props":8970,"children":8971},{"style":255},[8972],{"type":71,"value":8973}," run",{"type":66,"tag":107,"props":8975,"children":8976},{"style":120},[8977],{"type":71,"value":263},{"type":66,"tag":107,"props":8979,"children":8980},{"style":126},[8981],{"type":71,"value":418},{"type":66,"tag":107,"props":8983,"children":8984},{"style":120},[8985],{"type":71,"value":387},{"type":66,"tag":107,"props":8987,"children":8988},{"style":126},[8989],{"type":71,"value":8105},{"type":66,"tag":107,"props":8991,"children":8992},{"style":120},[8993],{"type":71,"value":134},{"type":66,"tag":107,"props":8995,"children":8996},{"style":255},[8997],{"type":71,"value":8998}," event",{"type":66,"tag":107,"props":9000,"children":9001},{"style":120},[9002],{"type":71,"value":263},{"type":66,"tag":107,"props":9004,"children":9005},{"style":126},[9006],{"type":71,"value":9007}," e ",{"type":66,"tag":107,"props":9009,"children":9010},{"style":120},[9011],{"type":71,"value":685},{"type":66,"tag":107,"props":9013,"children":9014},{"style":126},[9015],{"type":71,"value":290},{"type":66,"tag":107,"props":9017,"children":9018},{"style":120},[9019],{"type":71,"value":295},{"type":66,"tag":107,"props":9021,"children":9022},{"class":109,"line":338},[9023],{"type":66,"tag":107,"props":9024,"children":9025},{"style":120},[9026],{"type":71,"value":2525},{"type":66,"tag":107,"props":9028,"children":9029},{"class":109,"line":376},[9030,9034],{"type":66,"tag":107,"props":9031,"children":9032},{"style":120},[9033],{"type":71,"value":685},{"type":66,"tag":107,"props":9035,"children":9036},{"style":126},[9037],{"type":71,"value":432},{"type":66,"tag":807,"props":9039,"children":9040},{},[9041,9056],{"type":66,"tag":811,"props":9042,"children":9043},{},[9044],{"type":66,"tag":815,"props":9045,"children":9046},{},[9047,9051],{"type":66,"tag":819,"props":9048,"children":9049},{},[9050],{"type":71,"value":823},{"type":66,"tag":819,"props":9052,"children":9053},{},[9054],{"type":71,"value":9055},"Fires for",{"type":66,"tag":835,"props":9057,"children":9058},{},[9059,9076,9093,9110],{"type":66,"tag":815,"props":9060,"children":9061},{},[9062,9071],{"type":66,"tag":842,"props":9063,"children":9064},{},[9065],{"type":66,"tag":103,"props":9066,"children":9068},{"className":9067},[],[9069],{"type":71,"value":9070},"onFile",{"type":66,"tag":842,"props":9072,"children":9073},{},[9074],{"type":71,"value":9075},"Every create\u002Fchange\u002Fdelete",{"type":66,"tag":815,"props":9077,"children":9078},{},[9079,9088],{"type":66,"tag":842,"props":9080,"children":9081},{},[9082],{"type":66,"tag":103,"props":9083,"children":9085},{"className":9084},[],[9086],{"type":71,"value":9087},"onFileCreate",{"type":66,"tag":842,"props":9089,"children":9090},{},[9091],{"type":71,"value":9092},"File creates only",{"type":66,"tag":815,"props":9094,"children":9095},{},[9096,9105],{"type":66,"tag":842,"props":9097,"children":9098},{},[9099],{"type":66,"tag":103,"props":9100,"children":9102},{"className":9101},[],[9103],{"type":71,"value":9104},"onFileChange",{"type":66,"tag":842,"props":9106,"children":9107},{},[9108],{"type":71,"value":9109},"File changes only",{"type":66,"tag":815,"props":9111,"children":9112},{},[9113,9122],{"type":66,"tag":842,"props":9114,"children":9115},{},[9116],{"type":66,"tag":103,"props":9117,"children":9119},{"className":9118},[],[9120],{"type":71,"value":9121},"onFileDelete",{"type":66,"tag":842,"props":9123,"children":9124},{},[9125],{"type":71,"value":9126},"File deletes only",{"type":66,"tag":77,"props":9128,"children":9129},{},[9130,9132,9138,9144,9146,9151,9153,9159,9161,9167,9169,9175],{"type":71,"value":9131},"These are independent of the stream: the engine also emits a ",{"type":66,"tag":103,"props":9133,"children":9135},{"className":9134},[],[9136],{"type":71,"value":9137},"sandbox.file",{"type":66,"tag":103,"props":9139,"children":9141},{"className":9140},[],[9142],{"type":71,"value":9143},"CUSTOM",{"type":71,"value":9145}," chunk per change regardless of whether any ",{"type":66,"tag":103,"props":9147,"children":9149},{"className":9148},[],[9150],{"type":71,"value":8602},{"type":71,"value":9152}," hooks are\nregistered, so a client can react to the same edits without middleware. See\n",{"type":66,"tag":103,"props":9154,"children":9156},{"className":9155},[],[9157],{"type":71,"value":9158},"ai-core\u002Fag-ui-protocol\u002FSKILL.md",{"type":71,"value":9160}," for reading that chunk (and the opt-in\n",{"type":66,"tag":103,"props":9162,"children":9164},{"className":9163},[],[9165],{"type":71,"value":9166},"sandbox.file.diff",{"type":71,"value":9168}," chunk) off ",{"type":66,"tag":103,"props":9170,"children":9172},{"className":9171},[],[9173],{"type":71,"value":9174},"ChatStream",{"type":71,"value":387},{"type":66,"tag":1379,"props":9177,"children":9179},{"id":9178},"before-after-diff-lazy-git-backed-content-accessors",[9180,9186,9187,9193,9194,9200],{"type":66,"tag":103,"props":9181,"children":9183},{"className":9182},[],[9184],{"type":71,"value":9185},"before()",{"type":71,"value":1339},{"type":66,"tag":103,"props":9188,"children":9190},{"className":9189},[],[9191],{"type":71,"value":9192},"after()",{"type":71,"value":1339},{"type":66,"tag":103,"props":9195,"children":9197},{"className":9196},[],[9198],{"type":71,"value":9199},"diff()",{"type":71,"value":9201}," — lazy, git-backed content accessors",{"type":66,"tag":77,"props":9203,"children":9204},{},[9205,9207,9213,9215,9221],{"type":71,"value":9206},"Each hook receives a ",{"type":66,"tag":103,"props":9208,"children":9210},{"className":9209},[],[9211],{"type":71,"value":9212},"SandboxFileHookEvent",{"type":71,"value":9214},": the serializable\n",{"type":66,"tag":103,"props":9216,"children":9218},{"className":9217},[],[9219],{"type":71,"value":9220},"{ type, path, timestamp }",{"type":71,"value":9222}," plus three lazy accessors for the file's content:",{"type":66,"tag":96,"props":9224,"children":9226},{"className":1863,"code":9225,"language":1865,"meta":100,"style":100},"interface SandboxFileHookEvent {\n  type: 'create' | 'change' | 'delete'\n  path: string\n  timestamp: number\n  before(): Promise\u003Cstring> \u002F\u002F content at the session baseline ('' if new \u002F non-git)\n  after(): Promise\u003Cstring> \u002F\u002F current content ('' if deleted)\n  diff(): Promise\u003Cstring> \u002F\u002F unified patch vs the baseline\n}\n",[9227],{"type":66,"tag":103,"props":9228,"children":9229},{"__ignoreMap":100},[9230,9246,9304,9321,9337,9373,9406,9439],{"type":66,"tag":107,"props":9231,"children":9232},{"class":109,"line":110},[9233,9237,9242],{"type":66,"tag":107,"props":9234,"children":9235},{"style":220},[9236],{"type":71,"value":2042},{"type":66,"tag":107,"props":9238,"children":9239},{"style":1984},[9240],{"type":71,"value":9241}," SandboxFileHookEvent",{"type":66,"tag":107,"props":9243,"children":9244},{"style":120},[9245],{"type":71,"value":373},{"type":66,"tag":107,"props":9247,"children":9248},{"class":109,"line":168},[9249,9253,9257,9261,9266,9270,9274,9278,9283,9287,9291,9295,9300],{"type":66,"tag":107,"props":9250,"children":9251},{"style":255},[9252],{"type":71,"value":2272},{"type":66,"tag":107,"props":9254,"children":9255},{"style":120},[9256],{"type":71,"value":263},{"type":66,"tag":107,"props":9258,"children":9259},{"style":120},[9260],{"type":71,"value":154},{"type":66,"tag":107,"props":9262,"children":9263},{"style":157},[9264],{"type":71,"value":9265},"create",{"type":66,"tag":107,"props":9267,"children":9268},{"style":120},[9269],{"type":71,"value":276},{"type":66,"tag":107,"props":9271,"children":9272},{"style":120},[9273],{"type":71,"value":1997},{"type":66,"tag":107,"props":9275,"children":9276},{"style":120},[9277],{"type":71,"value":154},{"type":66,"tag":107,"props":9279,"children":9280},{"style":157},[9281],{"type":71,"value":9282},"change",{"type":66,"tag":107,"props":9284,"children":9285},{"style":120},[9286],{"type":71,"value":276},{"type":66,"tag":107,"props":9288,"children":9289},{"style":120},[9290],{"type":71,"value":1997},{"type":66,"tag":107,"props":9292,"children":9293},{"style":120},[9294],{"type":71,"value":154},{"type":66,"tag":107,"props":9296,"children":9297},{"style":157},[9298],{"type":71,"value":9299},"delete",{"type":66,"tag":107,"props":9301,"children":9302},{"style":120},[9303],{"type":71,"value":165},{"type":66,"tag":107,"props":9305,"children":9306},{"class":109,"line":206},[9307,9312,9316],{"type":66,"tag":107,"props":9308,"children":9309},{"style":255},[9310],{"type":71,"value":9311},"  path",{"type":66,"tag":107,"props":9313,"children":9314},{"style":120},[9315],{"type":71,"value":263},{"type":66,"tag":107,"props":9317,"children":9318},{"style":1984},[9319],{"type":71,"value":9320}," string\n",{"type":66,"tag":107,"props":9322,"children":9323},{"class":109,"line":216},[9324,9329,9333],{"type":66,"tag":107,"props":9325,"children":9326},{"style":255},[9327],{"type":71,"value":9328},"  timestamp",{"type":66,"tag":107,"props":9330,"children":9331},{"style":120},[9332],{"type":71,"value":263},{"type":66,"tag":107,"props":9334,"children":9335},{"style":1984},[9336],{"type":71,"value":6450},{"type":66,"tag":107,"props":9338,"children":9339},{"class":109,"line":251},[9340,9345,9350,9354,9358,9363,9368],{"type":66,"tag":107,"props":9341,"children":9342},{"style":255},[9343],{"type":71,"value":9344},"  before",{"type":66,"tag":107,"props":9346,"children":9347},{"style":120},[9348],{"type":71,"value":9349},"():",{"type":66,"tag":107,"props":9351,"children":9352},{"style":1984},[9353],{"type":71,"value":1987},{"type":66,"tag":107,"props":9355,"children":9356},{"style":120},[9357],{"type":71,"value":1965},{"type":66,"tag":107,"props":9359,"children":9360},{"style":1984},[9361],{"type":71,"value":9362},"string",{"type":66,"tag":107,"props":9364,"children":9365},{"style":120},[9366],{"type":71,"value":9367},">",{"type":66,"tag":107,"props":9369,"children":9370},{"style":2081},[9371],{"type":71,"value":9372}," \u002F\u002F content at the session baseline ('' if new \u002F non-git)\n",{"type":66,"tag":107,"props":9374,"children":9375},{"class":109,"line":298},[9376,9381,9385,9389,9393,9397,9401],{"type":66,"tag":107,"props":9377,"children":9378},{"style":255},[9379],{"type":71,"value":9380},"  after",{"type":66,"tag":107,"props":9382,"children":9383},{"style":120},[9384],{"type":71,"value":9349},{"type":66,"tag":107,"props":9386,"children":9387},{"style":1984},[9388],{"type":71,"value":1987},{"type":66,"tag":107,"props":9390,"children":9391},{"style":120},[9392],{"type":71,"value":1965},{"type":66,"tag":107,"props":9394,"children":9395},{"style":1984},[9396],{"type":71,"value":9362},{"type":66,"tag":107,"props":9398,"children":9399},{"style":120},[9400],{"type":71,"value":9367},{"type":66,"tag":107,"props":9402,"children":9403},{"style":2081},[9404],{"type":71,"value":9405}," \u002F\u002F current content ('' if deleted)\n",{"type":66,"tag":107,"props":9407,"children":9408},{"class":109,"line":311},[9409,9414,9418,9422,9426,9430,9434],{"type":66,"tag":107,"props":9410,"children":9411},{"style":255},[9412],{"type":71,"value":9413},"  diff",{"type":66,"tag":107,"props":9415,"children":9416},{"style":120},[9417],{"type":71,"value":9349},{"type":66,"tag":107,"props":9419,"children":9420},{"style":1984},[9421],{"type":71,"value":1987},{"type":66,"tag":107,"props":9423,"children":9424},{"style":120},[9425],{"type":71,"value":1965},{"type":66,"tag":107,"props":9427,"children":9428},{"style":1984},[9429],{"type":71,"value":9362},{"type":66,"tag":107,"props":9431,"children":9432},{"style":120},[9433],{"type":71,"value":9367},{"type":66,"tag":107,"props":9435,"children":9436},{"style":2081},[9437],{"type":71,"value":9438}," \u002F\u002F unified patch vs the baseline\n",{"type":66,"tag":107,"props":9440,"children":9441},{"class":109,"line":329},[9442],{"type":66,"tag":107,"props":9443,"children":9444},{"style":120},[9445],{"type":71,"value":2092},{"type":66,"tag":96,"props":9447,"children":9449},{"className":98,"code":9448,"language":45,"meta":100,"style":100},"import { defineChatMiddleware } from '@tanstack\u002Fai'\nimport { db } from '.\u002Fdb'\n\nconst auditMiddleware = defineChatMiddleware({\n  name: 'audit',\n  sandbox: {\n    onFileChange: async (ctx, e) => {\n      const [before, after] = await Promise.all([e.before(), e.after()])\n      db.log({ run: ctx.runId, path: e.path, before, after })\n    },\n  },\n})\n",[9450],{"type":66,"tag":103,"props":9451,"children":9452},{"__ignoreMap":100},[9453,9488,9523,9530,9557,9584,9599,9643,9737,9832,9839,9846],{"type":66,"tag":107,"props":9454,"children":9455},{"class":109,"line":110},[9456,9460,9464,9468,9472,9476,9480,9484],{"type":66,"tag":107,"props":9457,"children":9458},{"style":114},[9459],{"type":71,"value":117},{"type":66,"tag":107,"props":9461,"children":9462},{"style":120},[9463],{"type":71,"value":123},{"type":66,"tag":107,"props":9465,"children":9466},{"style":126},[9467],{"type":71,"value":8677},{"type":66,"tag":107,"props":9469,"children":9470},{"style":120},[9471],{"type":71,"value":144},{"type":66,"tag":107,"props":9473,"children":9474},{"style":114},[9475],{"type":71,"value":149},{"type":66,"tag":107,"props":9477,"children":9478},{"style":120},[9479],{"type":71,"value":154},{"type":66,"tag":107,"props":9481,"children":9482},{"style":157},[9483],{"type":71,"value":160},{"type":66,"tag":107,"props":9485,"children":9486},{"style":120},[9487],{"type":71,"value":165},{"type":66,"tag":107,"props":9489,"children":9490},{"class":109,"line":168},[9491,9495,9499,9503,9507,9511,9515,9519],{"type":66,"tag":107,"props":9492,"children":9493},{"style":114},[9494],{"type":71,"value":117},{"type":66,"tag":107,"props":9496,"children":9497},{"style":120},[9498],{"type":71,"value":123},{"type":66,"tag":107,"props":9500,"children":9501},{"style":126},[9502],{"type":71,"value":8713},{"type":66,"tag":107,"props":9504,"children":9505},{"style":120},[9506],{"type":71,"value":144},{"type":66,"tag":107,"props":9508,"children":9509},{"style":114},[9510],{"type":71,"value":149},{"type":66,"tag":107,"props":9512,"children":9513},{"style":120},[9514],{"type":71,"value":154},{"type":66,"tag":107,"props":9516,"children":9517},{"style":157},[9518],{"type":71,"value":8730},{"type":66,"tag":107,"props":9520,"children":9521},{"style":120},[9522],{"type":71,"value":165},{"type":66,"tag":107,"props":9524,"children":9525},{"class":109,"line":206},[9526],{"type":66,"tag":107,"props":9527,"children":9528},{"emptyLinePlaceholder":210},[9529],{"type":71,"value":213},{"type":66,"tag":107,"props":9531,"children":9532},{"class":109,"line":216},[9533,9537,9541,9545,9549,9553],{"type":66,"tag":107,"props":9534,"children":9535},{"style":220},[9536],{"type":71,"value":223},{"type":66,"tag":107,"props":9538,"children":9539},{"style":126},[9540],{"type":71,"value":8753},{"type":66,"tag":107,"props":9542,"children":9543},{"style":120},[9544],{"type":71,"value":233},{"type":66,"tag":107,"props":9546,"children":9547},{"style":236},[9548],{"type":71,"value":8677},{"type":66,"tag":107,"props":9550,"children":9551},{"style":126},[9552],{"type":71,"value":243},{"type":66,"tag":107,"props":9554,"children":9555},{"style":120},[9556],{"type":71,"value":248},{"type":66,"tag":107,"props":9558,"children":9559},{"class":109,"line":251},[9560,9564,9568,9572,9576,9580],{"type":66,"tag":107,"props":9561,"children":9562},{"style":255},[9563],{"type":71,"value":2383},{"type":66,"tag":107,"props":9565,"children":9566},{"style":120},[9567],{"type":71,"value":263},{"type":66,"tag":107,"props":9569,"children":9570},{"style":120},[9571],{"type":71,"value":154},{"type":66,"tag":107,"props":9573,"children":9574},{"style":157},[9575],{"type":71,"value":8789},{"type":66,"tag":107,"props":9577,"children":9578},{"style":120},[9579],{"type":71,"value":276},{"type":66,"tag":107,"props":9581,"children":9582},{"style":120},[9583],{"type":71,"value":295},{"type":66,"tag":107,"props":9585,"children":9586},{"class":109,"line":298},[9587,9591,9595],{"type":66,"tag":107,"props":9588,"children":9589},{"style":255},[9590],{"type":71,"value":8805},{"type":66,"tag":107,"props":9592,"children":9593},{"style":120},[9594],{"type":71,"value":263},{"type":66,"tag":107,"props":9596,"children":9597},{"style":120},[9598],{"type":71,"value":373},{"type":66,"tag":107,"props":9600,"children":9601},{"class":109,"line":311},[9602,9607,9611,9615,9619,9623,9627,9631,9635,9639],{"type":66,"tag":107,"props":9603,"children":9604},{"style":236},[9605],{"type":71,"value":9606},"    onFileChange",{"type":66,"tag":107,"props":9608,"children":9609},{"style":120},[9610],{"type":71,"value":263},{"type":66,"tag":107,"props":9612,"children":9613},{"style":220},[9614],{"type":71,"value":7871},{"type":66,"tag":107,"props":9616,"children":9617},{"style":120},[9618],{"type":71,"value":353},{"type":66,"tag":107,"props":9620,"children":9621},{"style":356},[9622],{"type":71,"value":359},{"type":66,"tag":107,"props":9624,"children":9625},{"style":120},[9626],{"type":71,"value":134},{"type":66,"tag":107,"props":9628,"children":9629},{"style":356},[9630],{"type":71,"value":8842},{"type":66,"tag":107,"props":9632,"children":9633},{"style":120},[9634],{"type":71,"value":290},{"type":66,"tag":107,"props":9636,"children":9637},{"style":220},[9638],{"type":71,"value":368},{"type":66,"tag":107,"props":9640,"children":9641},{"style":120},[9642],{"type":71,"value":373},{"type":66,"tag":107,"props":9644,"children":9645},{"class":109,"line":329},[9646,9651,9655,9659,9663,9668,9672,9676,9680,9684,9688,9693,9698,9703,9707,9711,9716,9720,9724,9728,9732],{"type":66,"tag":107,"props":9647,"children":9648},{"style":220},[9649],{"type":71,"value":9650},"      const",{"type":66,"tag":107,"props":9652,"children":9653},{"style":120},[9654],{"type":71,"value":5704},{"type":66,"tag":107,"props":9656,"children":9657},{"style":126},[9658],{"type":71,"value":1590},{"type":66,"tag":107,"props":9660,"children":9661},{"style":120},[9662],{"type":71,"value":134},{"type":66,"tag":107,"props":9664,"children":9665},{"style":126},[9666],{"type":71,"value":9667}," after",{"type":66,"tag":107,"props":9669,"children":9670},{"style":120},[9671],{"type":71,"value":5744},{"type":66,"tag":107,"props":9673,"children":9674},{"style":120},[9675],{"type":71,"value":2371},{"type":66,"tag":107,"props":9677,"children":9678},{"style":114},[9679],{"type":71,"value":7929},{"type":66,"tag":107,"props":9681,"children":9682},{"style":1984},[9683],{"type":71,"value":1987},{"type":66,"tag":107,"props":9685,"children":9686},{"style":120},[9687],{"type":71,"value":387},{"type":66,"tag":107,"props":9689,"children":9690},{"style":236},[9691],{"type":71,"value":9692},"all",{"type":66,"tag":107,"props":9694,"children":9695},{"style":255},[9696],{"type":71,"value":9697},"([",{"type":66,"tag":107,"props":9699,"children":9700},{"style":126},[9701],{"type":71,"value":9702},"e",{"type":66,"tag":107,"props":9704,"children":9705},{"style":120},[9706],{"type":71,"value":387},{"type":66,"tag":107,"props":9708,"children":9709},{"style":236},[9710],{"type":71,"value":1590},{"type":66,"tag":107,"props":9712,"children":9713},{"style":255},[9714],{"type":71,"value":9715},"()",{"type":66,"tag":107,"props":9717,"children":9718},{"style":120},[9719],{"type":71,"value":134},{"type":66,"tag":107,"props":9721,"children":9722},{"style":126},[9723],{"type":71,"value":8842},{"type":66,"tag":107,"props":9725,"children":9726},{"style":120},[9727],{"type":71,"value":387},{"type":66,"tag":107,"props":9729,"children":9730},{"style":236},[9731],{"type":71,"value":1692},{"type":66,"tag":107,"props":9733,"children":9734},{"style":255},[9735],{"type":71,"value":9736},"()])\n",{"type":66,"tag":107,"props":9738,"children":9739},{"class":109,"line":338},[9740,9745,9749,9753,9757,9761,9765,9769,9773,9777,9781,9785,9790,9794,9798,9802,9807,9811,9816,9820,9824,9828],{"type":66,"tag":107,"props":9741,"children":9742},{"style":126},[9743],{"type":71,"value":9744},"      db",{"type":66,"tag":107,"props":9746,"children":9747},{"style":120},[9748],{"type":71,"value":387},{"type":66,"tag":107,"props":9750,"children":9751},{"style":236},[9752],{"type":71,"value":392},{"type":66,"tag":107,"props":9754,"children":9755},{"style":255},[9756],{"type":71,"value":243},{"type":66,"tag":107,"props":9758,"children":9759},{"style":120},[9760],{"type":71,"value":501},{"type":66,"tag":107,"props":9762,"children":9763},{"style":255},[9764],{"type":71,"value":8973},{"type":66,"tag":107,"props":9766,"children":9767},{"style":120},[9768],{"type":71,"value":263},{"type":66,"tag":107,"props":9770,"children":9771},{"style":126},[9772],{"type":71,"value":418},{"type":66,"tag":107,"props":9774,"children":9775},{"style":120},[9776],{"type":71,"value":387},{"type":66,"tag":107,"props":9778,"children":9779},{"style":126},[9780],{"type":71,"value":8105},{"type":66,"tag":107,"props":9782,"children":9783},{"style":120},[9784],{"type":71,"value":134},{"type":66,"tag":107,"props":9786,"children":9787},{"style":255},[9788],{"type":71,"value":9789}," path",{"type":66,"tag":107,"props":9791,"children":9792},{"style":120},[9793],{"type":71,"value":263},{"type":66,"tag":107,"props":9795,"children":9796},{"style":126},[9797],{"type":71,"value":8842},{"type":66,"tag":107,"props":9799,"children":9800},{"style":120},[9801],{"type":71,"value":387},{"type":66,"tag":107,"props":9803,"children":9804},{"style":126},[9805],{"type":71,"value":9806},"path",{"type":66,"tag":107,"props":9808,"children":9809},{"style":120},[9810],{"type":71,"value":134},{"type":66,"tag":107,"props":9812,"children":9813},{"style":126},[9814],{"type":71,"value":9815}," before",{"type":66,"tag":107,"props":9817,"children":9818},{"style":120},[9819],{"type":71,"value":134},{"type":66,"tag":107,"props":9821,"children":9822},{"style":126},[9823],{"type":71,"value":9667},{"type":66,"tag":107,"props":9825,"children":9826},{"style":120},[9827],{"type":71,"value":144},{"type":66,"tag":107,"props":9829,"children":9830},{"style":255},[9831],{"type":71,"value":432},{"type":66,"tag":107,"props":9833,"children":9834},{"class":109,"line":376},[9835],{"type":66,"tag":107,"props":9836,"children":9837},{"style":120},[9838],{"type":71,"value":663},{"type":66,"tag":107,"props":9840,"children":9841},{"class":109,"line":435},[9842],{"type":66,"tag":107,"props":9843,"children":9844},{"style":120},[9845],{"type":71,"value":2525},{"type":66,"tag":107,"props":9847,"children":9848},{"class":109,"line":444},[9849,9853],{"type":66,"tag":107,"props":9850,"children":9851},{"style":120},[9852],{"type":71,"value":685},{"type":66,"tag":107,"props":9854,"children":9855},{"style":126},[9856],{"type":71,"value":432},{"type":66,"tag":77,"props":9858,"children":9859},{},[9860,9865,9866,9871,9872,9877,9878,9883,9885,9891,9893,9899,9900,9906,9908,9913],{"type":66,"tag":81,"props":9861,"children":9862},{},[9863],{"type":71,"value":9864},"Lazy — path-only hooks pay nothing.",{"type":71,"value":1287},{"type":66,"tag":103,"props":9867,"children":9869},{"className":9868},[],[9870],{"type":71,"value":9185},{"type":71,"value":742},{"type":66,"tag":103,"props":9873,"children":9875},{"className":9874},[],[9876],{"type":71,"value":9192},{"type":71,"value":1302},{"type":66,"tag":103,"props":9879,"children":9881},{"className":9880},[],[9882],{"type":71,"value":9199},{"type":71,"value":9884},"\nare methods, not fields: each only reads the file or shells out to ",{"type":66,"tag":103,"props":9886,"children":9888},{"className":9887},[],[9889],{"type":71,"value":9890},"git",{"type":71,"value":9892}," when\ncalled. A hook that only reads ",{"type":66,"tag":103,"props":9894,"children":9896},{"className":9895},[],[9897],{"type":71,"value":9898},"e.path",{"type":71,"value":1370},{"type":66,"tag":103,"props":9901,"children":9903},{"className":9902},[],[9904],{"type":71,"value":9905},"e.type",{"type":71,"value":9907}," (like the ",{"type":66,"tag":103,"props":9909,"children":9911},{"className":9910},[],[9912],{"type":71,"value":9070},{"type":71,"value":9914}," logger\nabove) never touches the filesystem or spawns a process.",{"type":66,"tag":77,"props":9916,"children":9917},{},[9918,9923,9925,9931,9933,9938,9939,9944,9946,9951,9953,9958,9960,9965,9967,9972,9974,9980,9982,9987,9989,9994,9995,10000,10002,10007,10009,10014,10015,10020,10021,10026,10027,10032,10034,10039,10041,10046,10048,10053,10055,10060,10062,10067,10069,10074,10076,10082,10084,10089,10091,10096,10098,10103,10105,10110,10112,10117,10119,10125],{"type":66,"tag":81,"props":9919,"children":9920},{},[9921],{"type":71,"value":9922},"Git session baseline.",{"type":71,"value":9924}," The sandbox snapshots ",{"type":66,"tag":103,"props":9926,"children":9928},{"className":9927},[],[9929],{"type":71,"value":9930},"git rev-parse HEAD",{"type":71,"value":9932}," once at\nsetup as the session baseline (empty string if the workspace isn't a git repo\nor has no commits). ",{"type":66,"tag":103,"props":9934,"children":9936},{"className":9935},[],[9937],{"type":71,"value":9185},{"type":71,"value":1641},{"type":66,"tag":103,"props":9940,"children":9942},{"className":9941},[],[9943],{"type":71,"value":9199},{"type":71,"value":9945}," always diff against that same\nfixed baseline for the rest of the run, so ",{"type":66,"tag":103,"props":9947,"children":9949},{"className":9948},[],[9950],{"type":71,"value":9104},{"type":71,"value":9952}," reports the file's\n",{"type":66,"tag":81,"props":9954,"children":9955},{},[9956],{"type":71,"value":9957},"cumulative",{"type":71,"value":9959}," change since the run started, not just the delta since the\nlast poll. ",{"type":66,"tag":103,"props":9961,"children":9963},{"className":9962},[],[9964],{"type":71,"value":9192},{"type":71,"value":9966}," always reads current on-disk content. None of the three\naccessors throw: a deleted file resolves ",{"type":66,"tag":103,"props":9968,"children":9970},{"className":9969},[],[9971],{"type":71,"value":9192},{"type":71,"value":9973}," to ",{"type":66,"tag":103,"props":9975,"children":9977},{"className":9976},[],[9978],{"type":71,"value":9979},"''",{"type":71,"value":9981}," (it still has\n",{"type":66,"tag":103,"props":9983,"children":9985},{"className":9984},[],[9986],{"type":71,"value":9185},{"type":71,"value":9988},"); a new file resolves ",{"type":66,"tag":103,"props":9990,"children":9992},{"className":9991},[],[9993],{"type":71,"value":9185},{"type":71,"value":9973},{"type":66,"tag":103,"props":9996,"children":9998},{"className":9997},[],[9999],{"type":71,"value":9979},{"type":71,"value":10001}," (it still has ",{"type":66,"tag":103,"props":10003,"children":10005},{"className":10004},[],[10006],{"type":71,"value":9192},{"type":71,"value":10008},");\na non-git workspace resolves ",{"type":66,"tag":81,"props":10010,"children":10011},{},[10012],{"type":71,"value":10013},"both",{"type":71,"value":1287},{"type":66,"tag":103,"props":10016,"children":10018},{"className":10017},[],[10019],{"type":71,"value":9185},{"type":71,"value":1641},{"type":66,"tag":103,"props":10022,"children":10024},{"className":10023},[],[10025],{"type":71,"value":9192},{"type":71,"value":9973},{"type":66,"tag":103,"props":10028,"children":10030},{"className":10029},[],[10031],{"type":71,"value":9979},{"type":71,"value":10033}," and\nmakes ",{"type":66,"tag":103,"props":10035,"children":10037},{"className":10036},[],[10038],{"type":71,"value":9199},{"type":71,"value":10040}," fall back to a synthesized add-patch built from ",{"type":66,"tag":103,"props":10042,"children":10044},{"className":10043},[],[10045],{"type":71,"value":9192},{"type":71,"value":10047}," —\nexcept for a ",{"type":66,"tag":103,"props":10049,"children":10051},{"className":10050},[],[10052],{"type":71,"value":9299},{"type":71,"value":10054}," event in a non-git workspace, where there's nothing to\nsynthesize and ",{"type":66,"tag":103,"props":10056,"children":10058},{"className":10057},[],[10059],{"type":71,"value":9199},{"type":71,"value":10061}," resolves to ",{"type":66,"tag":103,"props":10063,"children":10065},{"className":10064},[],[10066],{"type":71,"value":9979},{"type":71,"value":10068},". In a git workspace a file git\n",{"type":66,"tag":81,"props":10070,"children":10071},{},[10072],{"type":71,"value":10073},"isn't tracking yet",{"type":71,"value":10075}," (a file the agent created, and every later edit to it)\ndiffs empty because ",{"type":66,"tag":103,"props":10077,"children":10079},{"className":10078},[],[10080],{"type":71,"value":10081},"git diff",{"type":71,"value":10083}," ignores untracked files, so ",{"type":66,"tag":103,"props":10085,"children":10087},{"className":10086},[],[10088],{"type":71,"value":9199},{"type":71,"value":10090}," falls\nback to the same synthesized add-patch whenever the file is absent at the\nbaseline — a create-or-edit of an untracked file never streams an empty diff.\nAn empty diff for a ",{"type":66,"tag":81,"props":10092,"children":10093},{},[10094],{"type":71,"value":10095},"tracked",{"type":71,"value":10097}," file (identical to the baseline) stays empty,\nas it should. A ",{"type":66,"tag":81,"props":10099,"children":10100},{},[10101],{"type":71,"value":10102},"git-ignored",{"type":71,"value":10104}," file is withheld: the file event still fires\n(you're notified it changed) but ",{"type":66,"tag":103,"props":10106,"children":10108},{"className":10107},[],[10109],{"type":71,"value":9199},{"type":71,"value":10111}," returns ",{"type":66,"tag":103,"props":10113,"children":10115},{"className":10114},[],[10116],{"type":71,"value":9979},{"type":71,"value":10118},", so a secret like a\n",{"type":66,"tag":103,"props":10120,"children":10122},{"className":10121},[],[10123],{"type":71,"value":10124},".env",{"type":71,"value":10126}," never has its contents surfaced in the diff feed.",{"type":66,"tag":77,"props":10128,"children":10129},{},[10130,10135,10137,10143,10145,10150,10152,10157,10159,10164,10166,10172,10174,10179,10181,10186],{"type":66,"tag":81,"props":10131,"children":10132},{},[10133],{"type":71,"value":10134},"Failures are logged, not silent.",{"type":71,"value":10136}," Every git\u002Fexec\u002Ffs failure behind these\naccessors (and behind the ",{"type":66,"tag":103,"props":10138,"children":10140},{"className":10139},[],[10141],{"type":71,"value":10142},"find",{"type":71,"value":10144},"-poll watcher) still falls back to ",{"type":66,"tag":103,"props":10146,"children":10148},{"className":10147},[],[10149],{"type":71,"value":9979},{"type":71,"value":10151},"\u002Fan\nempty snapshot, but logs first: real anomalies (a failed ",{"type":66,"tag":103,"props":10153,"children":10155},{"className":10154},[],[10156],{"type":71,"value":10081},{"type":71,"value":10158},", an\nunreadable file, a lost ",{"type":66,"tag":103,"props":10160,"children":10162},{"className":10161},[],[10163],{"type":71,"value":10142},{"type":71,"value":10165}," poll) under the ",{"type":66,"tag":103,"props":10167,"children":10169},{"className":10168},[],[10170],{"type":71,"value":10171},"errors",{"type":71,"value":10173}," category (on by\ndefault); expected-empty conditions (a new file's ",{"type":66,"tag":103,"props":10175,"children":10177},{"className":10176},[],[10178],{"type":71,"value":9185},{"type":71,"value":10180},", a non-git\nbaseline) under the ",{"type":66,"tag":103,"props":10182,"children":10184},{"className":10183},[],[10185],{"type":71,"value":8602},{"type":71,"value":10187}," debug category.",{"type":66,"tag":77,"props":10189,"children":10190},{},[10191,10196,10198,10203,10205,10210,10212,10217],{"type":66,"tag":81,"props":10192,"children":10193},{},[10194],{"type":71,"value":10195},"Hook errors are swallowed per hook.",{"type":71,"value":10197}," A throwing ",{"type":66,"tag":103,"props":10199,"children":10201},{"className":10200},[],[10202],{"type":71,"value":8602},{"type":71,"value":10204}," hook is caught\nand logged under the ",{"type":66,"tag":103,"props":10206,"children":10208},{"className":10207},[],[10209],{"type":71,"value":10171},{"type":71,"value":10211}," category (on by default) — it cannot break the\nrun or stop other hooks (or the ",{"type":66,"tag":103,"props":10213,"children":10215},{"className":10214},[],[10216],{"type":71,"value":9137},{"type":71,"value":10218}," chunk) from continuing.",{"type":66,"tag":77,"props":10220,"children":10221},{},[10222],{"type":71,"value":10223},"Source: docs\u002Fsandbox\u002Fobservability.md",{"type":66,"tag":89,"props":10225,"children":10227},{"id":10226},"common-mistakes",[10228],{"type":71,"value":10229},"Common Mistakes",{"type":66,"tag":1379,"props":10231,"children":10233},{"id":10232},"a-medium-trying-to-modify-streamchunks-in-middleware",[10234],{"type":71,"value":10235},"a. MEDIUM: Trying to modify StreamChunks in middleware",{"type":66,"tag":96,"props":10237,"children":10239},{"className":98,"code":10238,"language":45,"meta":100,"style":100},"\u002F\u002F WRONG -- mutating the chunk object directly\nconst broken: ChatMiddleware = {\n  name: 'broken',\n  onChunk: (ctx, chunk) => {\n    chunk.delta = 'modified' \u002F\u002F Mutation does nothing; chunk is not modified in-place\n  },\n}\n\n\u002F\u002F CORRECT -- return a new chunk to replace the original\nconst correct: ChatMiddleware = {\n  name: 'correct',\n  onChunk: (ctx, chunk) => {\n    if (chunk.type === 'TEXT_MESSAGE_CONTENT') {\n      return { ...chunk, delta: chunk.delta.replace(\u002Fsecret\u002Fg, '[REDACTED]') }\n    }\n    \u002F\u002F Return void to pass through unchanged\n  },\n}\n",[10240],{"type":66,"tag":103,"props":10241,"children":10242},{"__ignoreMap":100},[10243,10251,10279,10307,10346,10385,10392,10399,10406,10414,10442,10470,10509,10557,10657,10664,10672,10679],{"type":66,"tag":107,"props":10244,"children":10245},{"class":109,"line":110},[10246],{"type":66,"tag":107,"props":10247,"children":10248},{"style":2081},[10249],{"type":71,"value":10250},"\u002F\u002F WRONG -- mutating the chunk object directly\n",{"type":66,"tag":107,"props":10252,"children":10253},{"class":109,"line":168},[10254,10258,10263,10267,10271,10275],{"type":66,"tag":107,"props":10255,"children":10256},{"style":220},[10257],{"type":71,"value":223},{"type":66,"tag":107,"props":10259,"children":10260},{"style":126},[10261],{"type":71,"value":10262}," broken",{"type":66,"tag":107,"props":10264,"children":10265},{"style":120},[10266],{"type":71,"value":263},{"type":66,"tag":107,"props":10268,"children":10269},{"style":1984},[10270],{"type":71,"value":2277},{"type":66,"tag":107,"props":10272,"children":10273},{"style":120},[10274],{"type":71,"value":2371},{"type":66,"tag":107,"props":10276,"children":10277},{"style":120},[10278],{"type":71,"value":373},{"type":66,"tag":107,"props":10280,"children":10281},{"class":109,"line":206},[10282,10286,10290,10294,10299,10303],{"type":66,"tag":107,"props":10283,"children":10284},{"style":255},[10285],{"type":71,"value":2383},{"type":66,"tag":107,"props":10287,"children":10288},{"style":120},[10289],{"type":71,"value":263},{"type":66,"tag":107,"props":10291,"children":10292},{"style":120},[10293],{"type":71,"value":154},{"type":66,"tag":107,"props":10295,"children":10296},{"style":157},[10297],{"type":71,"value":10298},"broken",{"type":66,"tag":107,"props":10300,"children":10301},{"style":120},[10302],{"type":71,"value":276},{"type":66,"tag":107,"props":10304,"children":10305},{"style":120},[10306],{"type":71,"value":295},{"type":66,"tag":107,"props":10308,"children":10309},{"class":109,"line":216},[10310,10314,10318,10322,10326,10330,10334,10338,10342],{"type":66,"tag":107,"props":10311,"children":10312},{"style":236},[10313],{"type":71,"value":4548},{"type":66,"tag":107,"props":10315,"children":10316},{"style":120},[10317],{"type":71,"value":263},{"type":66,"tag":107,"props":10319,"children":10320},{"style":120},[10321],{"type":71,"value":353},{"type":66,"tag":107,"props":10323,"children":10324},{"style":356},[10325],{"type":71,"value":359},{"type":66,"tag":107,"props":10327,"children":10328},{"style":120},[10329],{"type":71,"value":134},{"type":66,"tag":107,"props":10331,"children":10332},{"style":356},[10333],{"type":71,"value":4565},{"type":66,"tag":107,"props":10335,"children":10336},{"style":120},[10337],{"type":71,"value":290},{"type":66,"tag":107,"props":10339,"children":10340},{"style":220},[10341],{"type":71,"value":368},{"type":66,"tag":107,"props":10343,"children":10344},{"style":120},[10345],{"type":71,"value":373},{"type":66,"tag":107,"props":10347,"children":10348},{"class":109,"line":251},[10349,10354,10358,10363,10367,10371,10376,10380],{"type":66,"tag":107,"props":10350,"children":10351},{"style":126},[10352],{"type":71,"value":10353},"    chunk",{"type":66,"tag":107,"props":10355,"children":10356},{"style":120},[10357],{"type":71,"value":387},{"type":66,"tag":107,"props":10359,"children":10360},{"style":126},[10361],{"type":71,"value":10362},"delta",{"type":66,"tag":107,"props":10364,"children":10365},{"style":120},[10366],{"type":71,"value":2371},{"type":66,"tag":107,"props":10368,"children":10369},{"style":120},[10370],{"type":71,"value":154},{"type":66,"tag":107,"props":10372,"children":10373},{"style":157},[10374],{"type":71,"value":10375},"modified",{"type":66,"tag":107,"props":10377,"children":10378},{"style":120},[10379],{"type":71,"value":276},{"type":66,"tag":107,"props":10381,"children":10382},{"style":2081},[10383],{"type":71,"value":10384}," \u002F\u002F Mutation does nothing; chunk is not modified in-place\n",{"type":66,"tag":107,"props":10386,"children":10387},{"class":109,"line":298},[10388],{"type":66,"tag":107,"props":10389,"children":10390},{"style":120},[10391],{"type":71,"value":2525},{"type":66,"tag":107,"props":10393,"children":10394},{"class":109,"line":311},[10395],{"type":66,"tag":107,"props":10396,"children":10397},{"style":120},[10398],{"type":71,"value":2092},{"type":66,"tag":107,"props":10400,"children":10401},{"class":109,"line":329},[10402],{"type":66,"tag":107,"props":10403,"children":10404},{"emptyLinePlaceholder":210},[10405],{"type":71,"value":213},{"type":66,"tag":107,"props":10407,"children":10408},{"class":109,"line":338},[10409],{"type":66,"tag":107,"props":10410,"children":10411},{"style":2081},[10412],{"type":71,"value":10413},"\u002F\u002F CORRECT -- return a new chunk to replace the original\n",{"type":66,"tag":107,"props":10415,"children":10416},{"class":109,"line":376},[10417,10421,10426,10430,10434,10438],{"type":66,"tag":107,"props":10418,"children":10419},{"style":220},[10420],{"type":71,"value":223},{"type":66,"tag":107,"props":10422,"children":10423},{"style":126},[10424],{"type":71,"value":10425}," correct",{"type":66,"tag":107,"props":10427,"children":10428},{"style":120},[10429],{"type":71,"value":263},{"type":66,"tag":107,"props":10431,"children":10432},{"style":1984},[10433],{"type":71,"value":2277},{"type":66,"tag":107,"props":10435,"children":10436},{"style":120},[10437],{"type":71,"value":2371},{"type":66,"tag":107,"props":10439,"children":10440},{"style":120},[10441],{"type":71,"value":373},{"type":66,"tag":107,"props":10443,"children":10444},{"class":109,"line":435},[10445,10449,10453,10457,10462,10466],{"type":66,"tag":107,"props":10446,"children":10447},{"style":255},[10448],{"type":71,"value":2383},{"type":66,"tag":107,"props":10450,"children":10451},{"style":120},[10452],{"type":71,"value":263},{"type":66,"tag":107,"props":10454,"children":10455},{"style":120},[10456],{"type":71,"value":154},{"type":66,"tag":107,"props":10458,"children":10459},{"style":157},[10460],{"type":71,"value":10461},"correct",{"type":66,"tag":107,"props":10463,"children":10464},{"style":120},[10465],{"type":71,"value":276},{"type":66,"tag":107,"props":10467,"children":10468},{"style":120},[10469],{"type":71,"value":295},{"type":66,"tag":107,"props":10471,"children":10472},{"class":109,"line":444},[10473,10477,10481,10485,10489,10493,10497,10501,10505],{"type":66,"tag":107,"props":10474,"children":10475},{"style":236},[10476],{"type":71,"value":4548},{"type":66,"tag":107,"props":10478,"children":10479},{"style":120},[10480],{"type":71,"value":263},{"type":66,"tag":107,"props":10482,"children":10483},{"style":120},[10484],{"type":71,"value":353},{"type":66,"tag":107,"props":10486,"children":10487},{"style":356},[10488],{"type":71,"value":359},{"type":66,"tag":107,"props":10490,"children":10491},{"style":120},[10492],{"type":71,"value":134},{"type":66,"tag":107,"props":10494,"children":10495},{"style":356},[10496],{"type":71,"value":4565},{"type":66,"tag":107,"props":10498,"children":10499},{"style":120},[10500],{"type":71,"value":290},{"type":66,"tag":107,"props":10502,"children":10503},{"style":220},[10504],{"type":71,"value":368},{"type":66,"tag":107,"props":10506,"children":10507},{"style":120},[10508],{"type":71,"value":373},{"type":66,"tag":107,"props":10510,"children":10511},{"class":109,"line":486},[10512,10516,10520,10524,10528,10532,10536,10540,10545,10549,10553],{"type":66,"tag":107,"props":10513,"children":10514},{"style":114},[10515],{"type":71,"value":3662},{"type":66,"tag":107,"props":10517,"children":10518},{"style":255},[10519],{"type":71,"value":353},{"type":66,"tag":107,"props":10521,"children":10522},{"style":126},[10523],{"type":71,"value":4603},{"type":66,"tag":107,"props":10525,"children":10526},{"style":120},[10527],{"type":71,"value":387},{"type":66,"tag":107,"props":10529,"children":10530},{"style":126},[10531],{"type":71,"value":4661},{"type":66,"tag":107,"props":10533,"children":10534},{"style":120},[10535],{"type":71,"value":3685},{"type":66,"tag":107,"props":10537,"children":10538},{"style":120},[10539],{"type":71,"value":154},{"type":66,"tag":107,"props":10541,"children":10542},{"style":157},[10543],{"type":71,"value":10544},"TEXT_MESSAGE_CONTENT",{"type":66,"tag":107,"props":10546,"children":10547},{"style":120},[10548],{"type":71,"value":276},{"type":66,"tag":107,"props":10550,"children":10551},{"style":255},[10552],{"type":71,"value":1918},{"type":66,"tag":107,"props":10554,"children":10555},{"style":120},[10556],{"type":71,"value":248},{"type":66,"tag":107,"props":10558,"children":10559},{"class":109,"line":569},[10560,10564,10568,10572,10576,10580,10585,10589,10593,10597,10601,10605,10610,10614,10618,10623,10627,10632,10636,10640,10645,10649,10653],{"type":66,"tag":107,"props":10561,"children":10562},{"style":114},[10563],{"type":71,"value":3714},{"type":66,"tag":107,"props":10565,"children":10566},{"style":120},[10567],{"type":71,"value":123},{"type":66,"tag":107,"props":10569,"children":10570},{"style":120},[10571],{"type":71,"value":3934},{"type":66,"tag":107,"props":10573,"children":10574},{"style":126},[10575],{"type":71,"value":4603},{"type":66,"tag":107,"props":10577,"children":10578},{"style":120},[10579],{"type":71,"value":134},{"type":66,"tag":107,"props":10581,"children":10582},{"style":255},[10583],{"type":71,"value":10584}," delta",{"type":66,"tag":107,"props":10586,"children":10587},{"style":120},[10588],{"type":71,"value":263},{"type":66,"tag":107,"props":10590,"children":10591},{"style":126},[10592],{"type":71,"value":4565},{"type":66,"tag":107,"props":10594,"children":10595},{"style":120},[10596],{"type":71,"value":387},{"type":66,"tag":107,"props":10598,"children":10599},{"style":126},[10600],{"type":71,"value":10362},{"type":66,"tag":107,"props":10602,"children":10603},{"style":120},[10604],{"type":71,"value":387},{"type":66,"tag":107,"props":10606,"children":10607},{"style":236},[10608],{"type":71,"value":10609},"replace",{"type":66,"tag":107,"props":10611,"children":10612},{"style":255},[10613],{"type":71,"value":243},{"type":66,"tag":107,"props":10615,"children":10616},{"style":120},[10617],{"type":71,"value":1370},{"type":66,"tag":107,"props":10619,"children":10620},{"style":157},[10621],{"type":71,"value":10622},"secret",{"type":66,"tag":107,"props":10624,"children":10625},{"style":120},[10626],{"type":71,"value":1370},{"type":66,"tag":107,"props":10628,"children":10629},{"style":3962},[10630],{"type":71,"value":10631},"g",{"type":66,"tag":107,"props":10633,"children":10634},{"style":120},[10635],{"type":71,"value":134},{"type":66,"tag":107,"props":10637,"children":10638},{"style":120},[10639],{"type":71,"value":154},{"type":66,"tag":107,"props":10641,"children":10642},{"style":157},[10643],{"type":71,"value":10644},"[REDACTED]",{"type":66,"tag":107,"props":10646,"children":10647},{"style":120},[10648],{"type":71,"value":276},{"type":66,"tag":107,"props":10650,"children":10651},{"style":255},[10652],{"type":71,"value":1918},{"type":66,"tag":107,"props":10654,"children":10655},{"style":120},[10656],{"type":71,"value":2092},{"type":66,"tag":107,"props":10658,"children":10659},{"class":109,"line":577},[10660],{"type":66,"tag":107,"props":10661,"children":10662},{"style":120},[10663],{"type":71,"value":3778},{"type":66,"tag":107,"props":10665,"children":10666},{"class":109,"line":618},[10667],{"type":66,"tag":107,"props":10668,"children":10669},{"style":2081},[10670],{"type":71,"value":10671},"    \u002F\u002F Return void to pass through unchanged\n",{"type":66,"tag":107,"props":10673,"children":10674},{"class":109,"line":649},[10675],{"type":66,"tag":107,"props":10676,"children":10677},{"style":120},[10678],{"type":71,"value":2525},{"type":66,"tag":107,"props":10680,"children":10681},{"class":109,"line":657},[10682],{"type":66,"tag":107,"props":10683,"children":10684},{"style":120},[10685],{"type":71,"value":2092},{"type":66,"tag":77,"props":10687,"children":10688},{},[10689,10691,10696],{"type":71,"value":10690},"Middleware ",{"type":66,"tag":103,"props":10692,"children":10694},{"className":10693},[],[10695],{"type":71,"value":1016},{"type":71,"value":10697}," hooks are functional transforms. Return a new chunk, an array\nof chunks, null (to drop), or void (to pass through). Mutating the input object\nhas no effect on the stream output.",{"type":66,"tag":77,"props":10699,"children":10700},{},[10701],{"type":71,"value":10702},"Source: docs\u002Fadvanced\u002Fmiddleware.md",{"type":66,"tag":1379,"props":10704,"children":10706},{"id":10705},"b-medium-middleware-exceptions-breaking-the-stream",[10707],{"type":71,"value":10708},"b. MEDIUM: Middleware exceptions breaking the stream",{"type":66,"tag":96,"props":10710,"children":10712},{"className":98,"code":10711,"language":45,"meta":100,"style":100},"\u002F\u002F WRONG -- unhandled error kills the entire streaming response\nconst fragile: ChatMiddleware = {\n  name: 'fragile-analytics',\n  onFinish: async (ctx, info) => {\n    \u002F\u002F If this fetch fails, the stream breaks\n    await fetch('\u002Fapi\u002Fanalytics', {\n      method: 'POST',\n      body: JSON.stringify({ duration: info.duration }),\n    })\n  },\n}\n\n\u002F\u002F CORRECT -- wrap in try-catch and\u002For use ctx.defer()\nconst resilient: ChatMiddleware = {\n  name: 'resilient-analytics',\n  onFinish: (ctx, info) => {\n    \u002F\u002F Option 1: defer (non-blocking, errors are isolated)\n    ctx.defer(\n      fetch('\u002Fapi\u002Fanalytics', {\n        method: 'POST',\n        body: JSON.stringify({ duration: info.duration }),\n      }),\n    )\n  },\n  onChunk: (ctx, chunk) => {\n    \u002F\u002F Option 2: try-catch for synchronous\u002Fcritical hooks\n    try {\n      logChunk(chunk)\n    } catch (err) {\n      console.error('Logging failed:', err)\n    }\n    \u002F\u002F Return void to pass through\n  },\n}\n",[10713],{"type":66,"tag":103,"props":10714,"children":10715},{"__ignoreMap":100},[10716,10724,10752,10780,10823,10831,10868,10896,10961,10972,10979,10986,10993,11001,11029,11057,11096,11104,11123,11154,11181,11244,11259,11266,11273,11312,11320,11332,11352,11381,11426,11433,11441,11448],{"type":66,"tag":107,"props":10717,"children":10718},{"class":109,"line":110},[10719],{"type":66,"tag":107,"props":10720,"children":10721},{"style":2081},[10722],{"type":71,"value":10723},"\u002F\u002F WRONG -- unhandled error kills the entire streaming response\n",{"type":66,"tag":107,"props":10725,"children":10726},{"class":109,"line":168},[10727,10731,10736,10740,10744,10748],{"type":66,"tag":107,"props":10728,"children":10729},{"style":220},[10730],{"type":71,"value":223},{"type":66,"tag":107,"props":10732,"children":10733},{"style":126},[10734],{"type":71,"value":10735}," fragile",{"type":66,"tag":107,"props":10737,"children":10738},{"style":120},[10739],{"type":71,"value":263},{"type":66,"tag":107,"props":10741,"children":10742},{"style":1984},[10743],{"type":71,"value":2277},{"type":66,"tag":107,"props":10745,"children":10746},{"style":120},[10747],{"type":71,"value":2371},{"type":66,"tag":107,"props":10749,"children":10750},{"style":120},[10751],{"type":71,"value":373},{"type":66,"tag":107,"props":10753,"children":10754},{"class":109,"line":206},[10755,10759,10763,10767,10772,10776],{"type":66,"tag":107,"props":10756,"children":10757},{"style":255},[10758],{"type":71,"value":2383},{"type":66,"tag":107,"props":10760,"children":10761},{"style":120},[10762],{"type":71,"value":263},{"type":66,"tag":107,"props":10764,"children":10765},{"style":120},[10766],{"type":71,"value":154},{"type":66,"tag":107,"props":10768,"children":10769},{"style":157},[10770],{"type":71,"value":10771},"fragile-analytics",{"type":66,"tag":107,"props":10773,"children":10774},{"style":120},[10775],{"type":71,"value":276},{"type":66,"tag":107,"props":10777,"children":10778},{"style":120},[10779],{"type":71,"value":295},{"type":66,"tag":107,"props":10781,"children":10782},{"class":109,"line":216},[10783,10787,10791,10795,10799,10803,10807,10811,10815,10819],{"type":66,"tag":107,"props":10784,"children":10785},{"style":236},[10786],{"type":71,"value":2657},{"type":66,"tag":107,"props":10788,"children":10789},{"style":120},[10790],{"type":71,"value":263},{"type":66,"tag":107,"props":10792,"children":10793},{"style":220},[10794],{"type":71,"value":7871},{"type":66,"tag":107,"props":10796,"children":10797},{"style":120},[10798],{"type":71,"value":353},{"type":66,"tag":107,"props":10800,"children":10801},{"style":356},[10802],{"type":71,"value":359},{"type":66,"tag":107,"props":10804,"children":10805},{"style":120},[10806],{"type":71,"value":134},{"type":66,"tag":107,"props":10808,"children":10809},{"style":356},[10810],{"type":71,"value":471},{"type":66,"tag":107,"props":10812,"children":10813},{"style":120},[10814],{"type":71,"value":290},{"type":66,"tag":107,"props":10816,"children":10817},{"style":220},[10818],{"type":71,"value":368},{"type":66,"tag":107,"props":10820,"children":10821},{"style":120},[10822],{"type":71,"value":373},{"type":66,"tag":107,"props":10824,"children":10825},{"class":109,"line":251},[10826],{"type":66,"tag":107,"props":10827,"children":10828},{"style":2081},[10829],{"type":71,"value":10830},"    \u002F\u002F If this fetch fails, the stream breaks\n",{"type":66,"tag":107,"props":10832,"children":10833},{"class":109,"line":298},[10834,10839,10844,10848,10852,10856,10860,10864],{"type":66,"tag":107,"props":10835,"children":10836},{"style":114},[10837],{"type":71,"value":10838},"    await",{"type":66,"tag":107,"props":10840,"children":10841},{"style":236},[10842],{"type":71,"value":10843}," fetch",{"type":66,"tag":107,"props":10845,"children":10846},{"style":255},[10847],{"type":71,"value":243},{"type":66,"tag":107,"props":10849,"children":10850},{"style":120},[10851],{"type":71,"value":276},{"type":66,"tag":107,"props":10853,"children":10854},{"style":157},[10855],{"type":71,"value":2732},{"type":66,"tag":107,"props":10857,"children":10858},{"style":120},[10859],{"type":71,"value":276},{"type":66,"tag":107,"props":10861,"children":10862},{"style":120},[10863],{"type":71,"value":134},{"type":66,"tag":107,"props":10865,"children":10866},{"style":120},[10867],{"type":71,"value":373},{"type":66,"tag":107,"props":10869,"children":10870},{"class":109,"line":311},[10871,10876,10880,10884,10888,10892],{"type":66,"tag":107,"props":10872,"children":10873},{"style":255},[10874],{"type":71,"value":10875},"      method",{"type":66,"tag":107,"props":10877,"children":10878},{"style":120},[10879],{"type":71,"value":263},{"type":66,"tag":107,"props":10881,"children":10882},{"style":120},[10883],{"type":71,"value":154},{"type":66,"tag":107,"props":10885,"children":10886},{"style":157},[10887],{"type":71,"value":2765},{"type":66,"tag":107,"props":10889,"children":10890},{"style":120},[10891],{"type":71,"value":276},{"type":66,"tag":107,"props":10893,"children":10894},{"style":120},[10895],{"type":71,"value":295},{"type":66,"tag":107,"props":10897,"children":10898},{"class":109,"line":329},[10899,10904,10908,10912,10916,10920,10924,10928,10933,10937,10941,10945,10949,10953,10957],{"type":66,"tag":107,"props":10900,"children":10901},{"style":255},[10902],{"type":71,"value":10903},"      body",{"type":66,"tag":107,"props":10905,"children":10906},{"style":120},[10907],{"type":71,"value":263},{"type":66,"tag":107,"props":10909,"children":10910},{"style":126},[10911],{"type":71,"value":2790},{"type":66,"tag":107,"props":10913,"children":10914},{"style":120},[10915],{"type":71,"value":387},{"type":66,"tag":107,"props":10917,"children":10918},{"style":236},[10919],{"type":71,"value":2799},{"type":66,"tag":107,"props":10921,"children":10922},{"style":255},[10923],{"type":71,"value":243},{"type":66,"tag":107,"props":10925,"children":10926},{"style":120},[10927],{"type":71,"value":501},{"type":66,"tag":107,"props":10929,"children":10930},{"style":255},[10931],{"type":71,"value":10932}," duration",{"type":66,"tag":107,"props":10934,"children":10935},{"style":120},[10936],{"type":71,"value":263},{"type":66,"tag":107,"props":10938,"children":10939},{"style":126},[10940],{"type":71,"value":471},{"type":66,"tag":107,"props":10942,"children":10943},{"style":120},[10944],{"type":71,"value":387},{"type":66,"tag":107,"props":10946,"children":10947},{"style":126},[10948],{"type":71,"value":2889},{"type":66,"tag":107,"props":10950,"children":10951},{"style":120},[10952],{"type":71,"value":144},{"type":66,"tag":107,"props":10954,"children":10955},{"style":255},[10956],{"type":71,"value":290},{"type":66,"tag":107,"props":10958,"children":10959},{"style":120},[10960],{"type":71,"value":295},{"type":66,"tag":107,"props":10962,"children":10963},{"class":109,"line":338},[10964,10968],{"type":66,"tag":107,"props":10965,"children":10966},{"style":120},[10967],{"type":71,"value":4166},{"type":66,"tag":107,"props":10969,"children":10970},{"style":255},[10971],{"type":71,"value":432},{"type":66,"tag":107,"props":10973,"children":10974},{"class":109,"line":376},[10975],{"type":66,"tag":107,"props":10976,"children":10977},{"style":120},[10978],{"type":71,"value":2525},{"type":66,"tag":107,"props":10980,"children":10981},{"class":109,"line":435},[10982],{"type":66,"tag":107,"props":10983,"children":10984},{"style":120},[10985],{"type":71,"value":2092},{"type":66,"tag":107,"props":10987,"children":10988},{"class":109,"line":444},[10989],{"type":66,"tag":107,"props":10990,"children":10991},{"emptyLinePlaceholder":210},[10992],{"type":71,"value":213},{"type":66,"tag":107,"props":10994,"children":10995},{"class":109,"line":486},[10996],{"type":66,"tag":107,"props":10997,"children":10998},{"style":2081},[10999],{"type":71,"value":11000},"\u002F\u002F CORRECT -- wrap in try-catch and\u002For use ctx.defer()\n",{"type":66,"tag":107,"props":11002,"children":11003},{"class":109,"line":569},[11004,11008,11013,11017,11021,11025],{"type":66,"tag":107,"props":11005,"children":11006},{"style":220},[11007],{"type":71,"value":223},{"type":66,"tag":107,"props":11009,"children":11010},{"style":126},[11011],{"type":71,"value":11012}," resilient",{"type":66,"tag":107,"props":11014,"children":11015},{"style":120},[11016],{"type":71,"value":263},{"type":66,"tag":107,"props":11018,"children":11019},{"style":1984},[11020],{"type":71,"value":2277},{"type":66,"tag":107,"props":11022,"children":11023},{"style":120},[11024],{"type":71,"value":2371},{"type":66,"tag":107,"props":11026,"children":11027},{"style":120},[11028],{"type":71,"value":373},{"type":66,"tag":107,"props":11030,"children":11031},{"class":109,"line":577},[11032,11036,11040,11044,11049,11053],{"type":66,"tag":107,"props":11033,"children":11034},{"style":255},[11035],{"type":71,"value":2383},{"type":66,"tag":107,"props":11037,"children":11038},{"style":120},[11039],{"type":71,"value":263},{"type":66,"tag":107,"props":11041,"children":11042},{"style":120},[11043],{"type":71,"value":154},{"type":66,"tag":107,"props":11045,"children":11046},{"style":157},[11047],{"type":71,"value":11048},"resilient-analytics",{"type":66,"tag":107,"props":11050,"children":11051},{"style":120},[11052],{"type":71,"value":276},{"type":66,"tag":107,"props":11054,"children":11055},{"style":120},[11056],{"type":71,"value":295},{"type":66,"tag":107,"props":11058,"children":11059},{"class":109,"line":618},[11060,11064,11068,11072,11076,11080,11084,11088,11092],{"type":66,"tag":107,"props":11061,"children":11062},{"style":236},[11063],{"type":71,"value":2657},{"type":66,"tag":107,"props":11065,"children":11066},{"style":120},[11067],{"type":71,"value":263},{"type":66,"tag":107,"props":11069,"children":11070},{"style":120},[11071],{"type":71,"value":353},{"type":66,"tag":107,"props":11073,"children":11074},{"style":356},[11075],{"type":71,"value":359},{"type":66,"tag":107,"props":11077,"children":11078},{"style":120},[11079],{"type":71,"value":134},{"type":66,"tag":107,"props":11081,"children":11082},{"style":356},[11083],{"type":71,"value":471},{"type":66,"tag":107,"props":11085,"children":11086},{"style":120},[11087],{"type":71,"value":290},{"type":66,"tag":107,"props":11089,"children":11090},{"style":220},[11091],{"type":71,"value":368},{"type":66,"tag":107,"props":11093,"children":11094},{"style":120},[11095],{"type":71,"value":373},{"type":66,"tag":107,"props":11097,"children":11098},{"class":109,"line":649},[11099],{"type":66,"tag":107,"props":11100,"children":11101},{"style":2081},[11102],{"type":71,"value":11103},"    \u002F\u002F Option 1: defer (non-blocking, errors are isolated)\n",{"type":66,"tag":107,"props":11105,"children":11106},{"class":109,"line":657},[11107,11111,11115,11119],{"type":66,"tag":107,"props":11108,"children":11109},{"style":126},[11110],{"type":71,"value":2697},{"type":66,"tag":107,"props":11112,"children":11113},{"style":120},[11114],{"type":71,"value":387},{"type":66,"tag":107,"props":11116,"children":11117},{"style":236},[11118],{"type":71,"value":2706},{"type":66,"tag":107,"props":11120,"children":11121},{"style":255},[11122],{"type":71,"value":2711},{"type":66,"tag":107,"props":11124,"children":11125},{"class":109,"line":666},[11126,11130,11134,11138,11142,11146,11150],{"type":66,"tag":107,"props":11127,"children":11128},{"style":236},[11129],{"type":71,"value":2719},{"type":66,"tag":107,"props":11131,"children":11132},{"style":255},[11133],{"type":71,"value":243},{"type":66,"tag":107,"props":11135,"children":11136},{"style":120},[11137],{"type":71,"value":276},{"type":66,"tag":107,"props":11139,"children":11140},{"style":157},[11141],{"type":71,"value":2732},{"type":66,"tag":107,"props":11143,"children":11144},{"style":120},[11145],{"type":71,"value":276},{"type":66,"tag":107,"props":11147,"children":11148},{"style":120},[11149],{"type":71,"value":134},{"type":66,"tag":107,"props":11151,"children":11152},{"style":120},[11153],{"type":71,"value":373},{"type":66,"tag":107,"props":11155,"children":11156},{"class":109,"line":679},[11157,11161,11165,11169,11173,11177],{"type":66,"tag":107,"props":11158,"children":11159},{"style":255},[11160],{"type":71,"value":2752},{"type":66,"tag":107,"props":11162,"children":11163},{"style":120},[11164],{"type":71,"value":263},{"type":66,"tag":107,"props":11166,"children":11167},{"style":120},[11168],{"type":71,"value":154},{"type":66,"tag":107,"props":11170,"children":11171},{"style":157},[11172],{"type":71,"value":2765},{"type":66,"tag":107,"props":11174,"children":11175},{"style":120},[11176],{"type":71,"value":276},{"type":66,"tag":107,"props":11178,"children":11179},{"style":120},[11180],{"type":71,"value":295},{"type":66,"tag":107,"props":11182,"children":11183},{"class":109,"line":692},[11184,11188,11192,11196,11200,11204,11208,11212,11216,11220,11224,11228,11232,11236,11240],{"type":66,"tag":107,"props":11185,"children":11186},{"style":255},[11187],{"type":71,"value":2781},{"type":66,"tag":107,"props":11189,"children":11190},{"style":120},[11191],{"type":71,"value":263},{"type":66,"tag":107,"props":11193,"children":11194},{"style":126},[11195],{"type":71,"value":2790},{"type":66,"tag":107,"props":11197,"children":11198},{"style":120},[11199],{"type":71,"value":387},{"type":66,"tag":107,"props":11201,"children":11202},{"style":236},[11203],{"type":71,"value":2799},{"type":66,"tag":107,"props":11205,"children":11206},{"style":255},[11207],{"type":71,"value":243},{"type":66,"tag":107,"props":11209,"children":11210},{"style":120},[11211],{"type":71,"value":501},{"type":66,"tag":107,"props":11213,"children":11214},{"style":255},[11215],{"type":71,"value":10932},{"type":66,"tag":107,"props":11217,"children":11218},{"style":120},[11219],{"type":71,"value":263},{"type":66,"tag":107,"props":11221,"children":11222},{"style":126},[11223],{"type":71,"value":471},{"type":66,"tag":107,"props":11225,"children":11226},{"style":120},[11227],{"type":71,"value":387},{"type":66,"tag":107,"props":11229,"children":11230},{"style":126},[11231],{"type":71,"value":2889},{"type":66,"tag":107,"props":11233,"children":11234},{"style":120},[11235],{"type":71,"value":144},{"type":66,"tag":107,"props":11237,"children":11238},{"style":255},[11239],{"type":71,"value":290},{"type":66,"tag":107,"props":11241,"children":11242},{"style":120},[11243],{"type":71,"value":295},{"type":66,"tag":107,"props":11245,"children":11246},{"class":109,"line":700},[11247,11251,11255],{"type":66,"tag":107,"props":11248,"children":11249},{"style":120},[11250],{"type":71,"value":2986},{"type":66,"tag":107,"props":11252,"children":11253},{"style":255},[11254],{"type":71,"value":290},{"type":66,"tag":107,"props":11256,"children":11257},{"style":120},[11258],{"type":71,"value":295},{"type":66,"tag":107,"props":11260,"children":11261},{"class":109,"line":2866},[11262],{"type":66,"tag":107,"props":11263,"children":11264},{"style":255},[11265],{"type":71,"value":3003},{"type":66,"tag":107,"props":11267,"children":11268},{"class":109,"line":2896},[11269],{"type":66,"tag":107,"props":11270,"children":11271},{"style":120},[11272],{"type":71,"value":2525},{"type":66,"tag":107,"props":11274,"children":11275},{"class":109,"line":2933},[11276,11280,11284,11288,11292,11296,11300,11304,11308],{"type":66,"tag":107,"props":11277,"children":11278},{"style":236},[11279],{"type":71,"value":4548},{"type":66,"tag":107,"props":11281,"children":11282},{"style":120},[11283],{"type":71,"value":263},{"type":66,"tag":107,"props":11285,"children":11286},{"style":120},[11287],{"type":71,"value":353},{"type":66,"tag":107,"props":11289,"children":11290},{"style":356},[11291],{"type":71,"value":359},{"type":66,"tag":107,"props":11293,"children":11294},{"style":120},[11295],{"type":71,"value":134},{"type":66,"tag":107,"props":11297,"children":11298},{"style":356},[11299],{"type":71,"value":4565},{"type":66,"tag":107,"props":11301,"children":11302},{"style":120},[11303],{"type":71,"value":290},{"type":66,"tag":107,"props":11305,"children":11306},{"style":220},[11307],{"type":71,"value":368},{"type":66,"tag":107,"props":11309,"children":11310},{"style":120},[11311],{"type":71,"value":373},{"type":66,"tag":107,"props":11313,"children":11314},{"class":109,"line":2963},[11315],{"type":66,"tag":107,"props":11316,"children":11317},{"style":2081},[11318],{"type":71,"value":11319},"    \u002F\u002F Option 2: try-catch for synchronous\u002Fcritical hooks\n",{"type":66,"tag":107,"props":11321,"children":11322},{"class":109,"line":2980},[11323,11328],{"type":66,"tag":107,"props":11324,"children":11325},{"style":114},[11326],{"type":71,"value":11327},"    try",{"type":66,"tag":107,"props":11329,"children":11330},{"style":120},[11331],{"type":71,"value":373},{"type":66,"tag":107,"props":11333,"children":11334},{"class":109,"line":2997},[11335,11340,11344,11348],{"type":66,"tag":107,"props":11336,"children":11337},{"style":236},[11338],{"type":71,"value":11339},"      logChunk",{"type":66,"tag":107,"props":11341,"children":11342},{"style":255},[11343],{"type":71,"value":243},{"type":66,"tag":107,"props":11345,"children":11346},{"style":126},[11347],{"type":71,"value":4603},{"type":66,"tag":107,"props":11349,"children":11350},{"style":255},[11351],{"type":71,"value":432},{"type":66,"tag":107,"props":11353,"children":11354},{"class":109,"line":3006},[11355,11359,11364,11368,11373,11377],{"type":66,"tag":107,"props":11356,"children":11357},{"style":120},[11358],{"type":71,"value":4166},{"type":66,"tag":107,"props":11360,"children":11361},{"style":114},[11362],{"type":71,"value":11363}," catch",{"type":66,"tag":107,"props":11365,"children":11366},{"style":255},[11367],{"type":71,"value":353},{"type":66,"tag":107,"props":11369,"children":11370},{"style":126},[11371],{"type":71,"value":11372},"err",{"type":66,"tag":107,"props":11374,"children":11375},{"style":255},[11376],{"type":71,"value":1918},{"type":66,"tag":107,"props":11378,"children":11379},{"style":120},[11380],{"type":71,"value":248},{"type":66,"tag":107,"props":11382,"children":11383},{"class":109,"line":3014},[11384,11388,11392,11396,11400,11404,11409,11413,11417,11422],{"type":66,"tag":107,"props":11385,"children":11386},{"style":126},[11387],{"type":71,"value":4087},{"type":66,"tag":107,"props":11389,"children":11390},{"style":120},[11391],{"type":71,"value":387},{"type":66,"tag":107,"props":11393,"children":11394},{"style":236},[11395],{"type":71,"value":642},{"type":66,"tag":107,"props":11397,"children":11398},{"style":255},[11399],{"type":71,"value":243},{"type":66,"tag":107,"props":11401,"children":11402},{"style":120},[11403],{"type":71,"value":276},{"type":66,"tag":107,"props":11405,"children":11406},{"style":157},[11407],{"type":71,"value":11408},"Logging failed:",{"type":66,"tag":107,"props":11410,"children":11411},{"style":120},[11412],{"type":71,"value":276},{"type":66,"tag":107,"props":11414,"children":11415},{"style":120},[11416],{"type":71,"value":134},{"type":66,"tag":107,"props":11418,"children":11419},{"style":126},[11420],{"type":71,"value":11421}," err",{"type":66,"tag":107,"props":11423,"children":11424},{"style":255},[11425],{"type":71,"value":432},{"type":66,"tag":107,"props":11427,"children":11428},{"class":109,"line":3055},[11429],{"type":66,"tag":107,"props":11430,"children":11431},{"style":120},[11432],{"type":71,"value":3778},{"type":66,"tag":107,"props":11434,"children":11435},{"class":109,"line":3075},[11436],{"type":66,"tag":107,"props":11437,"children":11438},{"style":2081},[11439],{"type":71,"value":11440},"    \u002F\u002F Return void to pass through\n",{"type":66,"tag":107,"props":11442,"children":11443},{"class":109,"line":3108},[11444],{"type":66,"tag":107,"props":11445,"children":11446},{"style":120},[11447],{"type":71,"value":2525},{"type":66,"tag":107,"props":11449,"children":11450},{"class":109,"line":3136},[11451],{"type":66,"tag":107,"props":11452,"children":11453},{"style":120},[11454],{"type":71,"value":2092},{"type":66,"tag":77,"props":11456,"children":11457},{},[11458,11460,11465],{"type":71,"value":11459},"Wrap all middleware hooks in try-catch to prevent analytics or logging failures\nfrom killing the chat stream. For async side effects, prefer ",{"type":66,"tag":103,"props":11461,"children":11463},{"className":11462},[],[11464],{"type":71,"value":2220},{"type":71,"value":11466}," which\nruns after the terminal hook and isolates failures.",{"type":66,"tag":77,"props":11468,"children":11469},{},[11470],{"type":71,"value":10702},{"type":66,"tag":89,"props":11472,"children":11474},{"id":11473},"cross-references",[11475],{"type":71,"value":11476},"Cross-References",{"type":66,"tag":1572,"props":11478,"children":11479},{},[11480,11492,11510,11561],{"type":66,"tag":1576,"props":11481,"children":11482},{},[11483,11485,11490],{"type":71,"value":11484},"See also: ",{"type":66,"tag":81,"props":11486,"children":11487},{},[11488],{"type":71,"value":11489},"ai-core\u002Fchat-experience\u002FSKILL.md",{"type":71,"value":11491}," -- Middleware hooks into the chat lifecycle",{"type":66,"tag":1576,"props":11493,"children":11494},{},[11495,11496,11501,11503,11508],{"type":71,"value":11484},{"type":66,"tag":81,"props":11497,"children":11498},{},[11499],{"type":71,"value":11500},"ai-core\u002Fstructured-outputs\u002FSKILL.md",{"type":71,"value":11502}," -- Middleware now wraps the final structured-output call; use ",{"type":66,"tag":103,"props":11504,"children":11506},{"className":11505},[],[11507],{"type":71,"value":894},{"type":71,"value":11509}," for JSON-Schema transforms",{"type":66,"tag":1576,"props":11511,"children":11512},{},[11513,11514,11518,11520,11525,11526,11531,11532,11537,11539,11544,11546,11551,11553,11559],{"type":71,"value":11484},{"type":66,"tag":81,"props":11515,"children":11516},{},[11517],{"type":71,"value":9158},{"type":71,"value":11519}," -- Reading the ",{"type":66,"tag":103,"props":11521,"children":11523},{"className":11522},[],[11524],{"type":71,"value":9137},{"type":71,"value":1339},{"type":66,"tag":103,"props":11527,"children":11529},{"className":11528},[],[11530],{"type":71,"value":9166},{"type":71,"value":1287},{"type":66,"tag":103,"props":11533,"children":11535},{"className":11534},[],[11536],{"type":71,"value":9143},{"type":71,"value":11538}," chunks the sandbox runtime emits alongside these ",{"type":66,"tag":103,"props":11540,"children":11542},{"className":11541},[],[11543],{"type":71,"value":8602},{"type":71,"value":11545}," hooks, via ",{"type":66,"tag":103,"props":11547,"children":11549},{"className":11548},[],[11550],{"type":71,"value":9174},{"type":71,"value":11552},"'s typed ",{"type":66,"tag":103,"props":11554,"children":11556},{"className":11555},[],[11557],{"type":71,"value":11558},"KnownCustomEvent",{"type":71,"value":11560}," narrowing",{"type":66,"tag":1576,"props":11562,"children":11563},{},[11564,11565,11575,11576,11582,11584,11589,11591,11596],{"type":71,"value":11484},{"type":66,"tag":81,"props":11566,"children":11567},{},[11568,11573],{"type":66,"tag":103,"props":11569,"children":11571},{"className":11570},[],[11572],{"type":71,"value":7601},{"type":71,"value":11574}," skills",{"type":71,"value":353},{"type":66,"tag":103,"props":11577,"children":11579},{"className":11578},[],[11580],{"type":71,"value":11581},"skills\u002Fai-persistence\u002FSKILL.md",{"type":71,"value":11583}," in that package) -- Full persistence suite (",{"type":66,"tag":103,"props":11585,"children":11587},{"className":11586},[],[11588],{"type":71,"value":8206},{"type":71,"value":11590},", client storage, store contracts, adapter recipes, locks). This file only sketches server ",{"type":66,"tag":103,"props":11592,"children":11594},{"className":11593},[],[11595],{"type":71,"value":8206},{"type":71,"value":387},{"type":66,"tag":11598,"props":11599,"children":11600},"style",{},[11601],{"type":71,"value":11602},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"items":11604,"total":2980},[11605,11622,11635,11650,11663,11677,11690],{"slug":11606,"name":11606,"fn":11607,"description":11608,"org":11609,"tags":11610,"stars":24,"repoUrl":25,"updatedAt":11621},"ai-code-mode","execute sandboxed TypeScript code with LLMs","LLM-generated TypeScript execution in sandboxed environments: createCodeModeTool() with isolate drivers (createNodeIsolateDriver, createQuickJSIsolateDriver, createCloudflareIsolateDriver), codeModeWithSkills() for persistent skill libraries, trust strategies, skill storage (FileSystem, LocalStorage, InMemory, Mongo), client-side execution progress via code_mode:* custom events in useChat.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11611,11612,11615,11618,11619],{"name":18,"slug":19,"type":16},{"name":11613,"slug":11614,"type":16},"Code Execution","code-execution",{"name":11616,"slug":11617,"type":16},"Sandboxing","sandboxing",{"name":10,"slug":9,"type":16},{"name":11620,"slug":45,"type":16},"TypeScript","2026-07-16T06:04:13.597905",{"slug":11623,"name":11623,"fn":11624,"description":11625,"org":11626,"tags":11627,"stars":24,"repoUrl":25,"updatedAt":11634},"ai-core","configure TanStack AI agent features","Entry point for TanStack AI skills. Routes to chat-experience, tool-calling, media-generation, structured-outputs, adapter-configuration, ag-ui-protocol, middleware, locks, custom-backend-integration, and debug-logging, plus the skills shipped by companion packages (@tanstack\u002Fai-persistence, @tanstack\u002Fai-code-mode). Use chat() not streamText(), openaiText() not createOpenAI(), toServerSentEventsResponse() not manual SSE, middleware hooks not onEnd callbacks.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11628,11631,11633],{"name":11629,"slug":11630,"type":16},"Agents","agents",{"name":11632,"slug":30,"type":16},"AI",{"name":21,"slug":22,"type":16},"2026-07-30T05:26:10.404565",{"slug":11636,"name":11637,"fn":11638,"description":11639,"org":11640,"tags":11641,"stars":24,"repoUrl":25,"updatedAt":11649},"ai-coreadapter-configuration","ai-core\u002Fadapter-configuration","configure AI provider adapters","Provider adapter selection and configuration: openaiText, anthropicText, geminiText, ollamaText, grokText, groqText, openRouterText, bedrockText, openaiCompatible. Per-model type safety with modelOptions, reasoning\u002Fthinking configuration, runtime adapter switching, extendAdapter() for custom models, createModel(). Generic OpenAI-compatible providers (DeepSeek, Together, Fireworks, etc.) via openaiCompatible({ baseURL, apiKey, models }) from @tanstack\u002Fai-openai\u002Fcompatible. API key env vars: OPENAI_API_KEY, ANTHROPIC_API_KEY, GOOGLE_API_KEY\u002FGEMINI_API_KEY, XAI_API_KEY, GROQ_API_KEY, OPENROUTER_API_KEY, OLLAMA_HOST, BEDROCK_API_KEY (or AWS_BEARER_TOKEN_BEDROCK).\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11642,11643,11646,11648],{"name":18,"slug":19,"type":16},{"name":11644,"slug":11645,"type":16},"Configuration","configuration",{"name":11647,"slug":37,"type":16},"LLM",{"name":10,"slug":9,"type":16},"2026-07-16T06:04:17.82075",{"slug":11651,"name":11652,"fn":11653,"description":11654,"org":11655,"tags":11656,"stars":24,"repoUrl":25,"updatedAt":11662},"ai-coreag-ui-protocol","ai-core\u002Fag-ui-protocol","implement TanStack AI streaming protocol","Server-side AG-UI streaming protocol implementation: StreamChunk event types (RUN_STARTED, TEXT_MESSAGE_START\u002FCONTENT\u002FEND, TOOL_CALL_START\u002FARGS\u002FEND, RUN_FINISHED, RUN_ERROR, STEP_STARTED\u002FSTEP_FINISHED, STATE_SNAPSHOT\u002FDELTA, CUSTOM), toServerSentEventsStream() for SSE format, toHttpStream() for NDJSON format. For backends serving AG-UI events without client packages.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11657,11660,11661],{"name":11658,"slug":11659,"type":16},"API Development","api-development",{"name":11647,"slug":37,"type":16},{"name":10,"slug":9,"type":16},"2026-07-16T06:04:10.093367",{"slug":11664,"name":11665,"fn":11666,"description":11667,"org":11668,"tags":11669,"stars":24,"repoUrl":25,"updatedAt":11676},"ai-corechat-experience","ai-core\u002Fchat-experience","implement chat experiences with TanStack AI","End-to-end chat implementation: server endpoint with chat() and toServerSentEventsResponse(), client-side useChat hook with fetchServerSentEvents(), message rendering with UIMessage parts, multimodal content, thinking\u002Freasoning display. Covers streaming states, connection adapters, and message format conversions. NOT Vercel AI SDK — uses chat() not streamText().\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11670,11671,11674,11675],{"name":11658,"slug":11659,"type":16},{"name":11672,"slug":11673,"type":16},"Frontend","frontend",{"name":11647,"slug":37,"type":16},{"name":10,"slug":9,"type":16},"2026-07-30T05:26:11.505241",{"slug":11678,"name":11679,"fn":11680,"description":11681,"org":11682,"tags":11683,"stars":24,"repoUrl":25,"updatedAt":11689},"ai-coreclient-persistence","ai-core\u002Fclient-persistence","implement browser chat persistence for TanStack AI","Browser chat persistence on useChat \u002F ChatClient: localStoragePersistence, sessionStoragePersistence, indexedDBPersistence. Client-authoritative (adapter, full transcript) vs server-authoritative (persistence: true, no client cache). Reload restore, pending interrupts, mid-stream rejoin with delivery durability. Use for SPA reload durability — NOT server history alone. No extra package: the adapters ship in the framework packages.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11684,11685,11686,11688],{"name":11632,"slug":30,"type":16},{"name":11672,"slug":11673,"type":16},{"name":11687,"slug":8215,"type":16},"Persistence",{"name":10,"slug":9,"type":16},"2026-07-30T05:53:35.503176",{"slug":11691,"name":11692,"fn":11693,"description":11694,"org":11695,"tags":11696,"stars":24,"repoUrl":25,"updatedAt":11703},"ai-corecustom-backend-integration","ai-core\u002Fcustom-backend-integration","integrate custom backends with TanStack AI","Connect useChat to a non-TanStack-AI backend through custom connection adapters. ConnectConnectionAdapter (single async iterable) vs SubscribeConnectionAdapter (separate subscribe\u002Fsend). Customize fetchServerSentEvents() and fetchHttpStream() with auth headers, custom URLs, and request options. Import from framework package, not @tanstack\u002Fai-client.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11697,11698,11699,11702],{"name":11632,"slug":30,"type":16},{"name":11658,"slug":11659,"type":16},{"name":11700,"slug":11701,"type":16},"Backend","backend",{"name":10,"slug":9,"type":16},"2026-07-17T06:06:39.855351",{"items":11705,"total":11845},[11706,11720,11732,11744,11759,11771,11781,11791,11804,11814,11825,11835],{"slug":11707,"name":11707,"fn":11708,"description":11709,"org":11710,"tags":11711,"stars":11717,"repoUrl":11718,"updatedAt":11719},"aggregation","perform data aggregation in TanStack Table","Aggregate TanStack Table columns independently of grouping, including grand totals, caller-selected row totals, multiple keyed aggregations, custom context-based definitions, grouped merges, manual values, and worker constraints.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11712,11715,11716],{"name":11713,"slug":11714,"type":16},"Data Analysis","data-analysis",{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},28175,"https:\u002F\u002Fgithub.com\u002FTanStack\u002Ftable","2026-07-30T05:25:59.429787",{"slug":11721,"name":11721,"fn":11722,"description":11723,"org":11724,"tags":11725,"stars":11717,"repoUrl":11718,"updatedAt":11731},"api-not-found","diagnose TanStack Table API errors","Diagnose missing TanStack Table v9 exports, options, state slices, and instance methods. Load before inventing an API when code sees a type error, undefined feature method, absent object key, adapter mismatch, or v8-shaped example.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11726,11729,11730],{"name":11727,"slug":11728,"type":16},"Debugging","debugging",{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},"2026-07-30T05:26:05.418735",{"slug":11733,"name":11733,"fn":11734,"description":11735,"org":11736,"tags":11737,"stars":11717,"repoUrl":11718,"updatedAt":11743},"cell-selection","select rectangular cell ranges in tables","Select rectangular cell ranges with cellSelectionFeature: two-corner range state keyed by row and column id, mousedown\u002Fmouseenter handlers, selection edges, render-order resolution under pinning, and autoResetCellSelection. Load when ranges widen unexpectedly after sorting or column reordering, when a drag re-renders the whole table, or when building copy-to-clipboard from a selection.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11738,11739,11740],{"name":11713,"slug":11714,"type":16},{"name":10,"slug":9,"type":16},{"name":11741,"slug":11742,"type":16},"UI Components","ui-components","2026-07-30T05:25:38.403427",{"slug":11745,"name":11745,"fn":11746,"description":11747,"org":11748,"tags":11749,"stars":11717,"repoUrl":11718,"updatedAt":11758},"client-vs-server","manage TanStack Table data pipelines","Choose client or server ownership for filtering, grouping, sorting, expanding, and pagination in TanStack Table v9. Load for manual* flags, mixed pipelines, server counts, or deciding which dataset each row-model stage receives.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11750,11753,11754,11757],{"name":11751,"slug":11752,"type":16},"Data Pipeline","data-pipeline",{"name":11672,"slug":11673,"type":16},{"name":11755,"slug":11756,"type":16},"Performance","performance",{"name":10,"slug":9,"type":16},"2026-07-30T05:25:45.400104",{"slug":11760,"name":11760,"fn":11761,"description":11762,"org":11763,"tags":11764,"stars":11717,"repoUrl":11718,"updatedAt":11770},"column-faceting","build faceted filter UIs","Build faceted filter UIs with columnFacetingFeature, facetedRowModel, facetedUniqueValues, and facetedMinMaxValues. Load for facet counts, numeric ranges, own-filter exclusion, or server-page facet completeness.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11765,11768,11769],{"name":11766,"slug":11767,"type":16},"Data Visualization","data-visualization",{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},"2026-07-30T05:25:41.397257",{"slug":11772,"name":11772,"fn":11773,"description":11774,"org":11775,"tags":11776,"stars":11717,"repoUrl":11718,"updatedAt":11780},"column-filtering","implement column filtering in TanStack Table","Filter columns with columnFilteringFeature, filteredRowModel, filterFns, filterMeta, nested-row direction, and manualFiltering. Load for accessor compatibility, controlled filter updaters, fuzzy metadata, or client\u002Fserver ownership.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11777,11778,11779],{"name":11713,"slug":11714,"type":16},{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},"2026-07-30T05:25:53.391632",{"slug":11782,"name":11782,"fn":11783,"description":11784,"org":11785,"tags":11786,"stars":11717,"repoUrl":11718,"updatedAt":11790},"column-ordering","manage TanStack Table column ordering","Control TanStack Table v9 leaf columnOrder with stable IDs while accounting for pinning regions, visibility, and groupedColumnMode precedence. Load for drag-and-drop columns or rendered order that differs from state.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11787,11788,11789],{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},{"name":11741,"slug":11742,"type":16},"2026-07-30T05:26:03.37801",{"slug":11792,"name":11792,"fn":11793,"description":11794,"org":11795,"tags":11796,"stars":11717,"repoUrl":11718,"updatedAt":11803},"column-pinning","configure column pinning in TanStack Table","Pin columns into logical start, center, and end regions with columnPinningFeature and renderer-owned sticky CSS. Load for RTL offsets, z-index, backgrounds, overflow, widths, gaps, or overlaps.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11797,11800,11801,11802],{"name":11798,"slug":11799,"type":16},"CSS","css",{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},{"name":11741,"slug":11742,"type":16},"2026-07-30T05:25:55.377366",{"slug":11805,"name":11805,"fn":11806,"description":11807,"org":11808,"tags":11809,"stars":11717,"repoUrl":11718,"updatedAt":11813},"column-resizing","implement column resizing in TanStack Table","Wire columnResizingFeature, header.getResizeHandler, resize mode and direction, pointer or touch events, and performant CSS-variable updates. Load when resize state changes but widths do not, or large tables resize slowly.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11810,11811,11812],{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},{"name":11741,"slug":11742,"type":16},"2026-07-30T05:25:51.400011",{"slug":11815,"name":11815,"fn":11816,"description":11817,"org":11818,"tags":11819,"stars":11717,"repoUrl":11718,"updatedAt":11824},"column-sizing","configure column sizing in TanStack Table","Use columnSizingFeature numeric size, minSize, maxSize, getSize, getStart, getAfter, and total-size APIs in table, grid, or flex CSS. Load for auto or percentage misconceptions and sizing\u002Fpinning layout mismatch.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11820,11821,11822,11823],{"name":11798,"slug":11799,"type":16},{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},{"name":11741,"slug":11742,"type":16},"2026-07-30T05:25:48.703799",{"slug":11826,"name":11826,"fn":11827,"description":11828,"org":11829,"tags":11830,"stars":11717,"repoUrl":11718,"updatedAt":11834},"column-visibility","manage column visibility in TanStack Table","Hide columns with columnVisibilityFeature while rendering visibility-aware header, column, and cell collections. Load when hidden columns remain in the DOM, false-versus-absent state is confused, or enableHiding is misunderstood.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11831,11832,11833],{"name":11672,"slug":11673,"type":16},{"name":10,"slug":9,"type":16},{"name":11741,"slug":11742,"type":16},"2026-07-30T05:25:47.367943",{"slug":11836,"name":11836,"fn":11837,"description":11838,"org":11839,"tags":11840,"stars":11717,"repoUrl":11718,"updatedAt":11844},"core","build data grids with TanStack Table","Use TanStack Table v9 as a headless data-grid state and row-processing engine. Load for first-table architecture, stable data and columns, row numbering with getDisplayIndex, semantic rendering, framework adapter choice, or deciding what Table owns versus the renderer.\n",{"slug":9,"name":10,"logoUrl":11,"githubOrg":10},[11841,11842,11843],{"name":11713,"slug":11714,"type":16},{"name":11672,"slug":11673,"type":16},{"name":11741,"slug":11742,"type":16},"2026-07-30T05:25:52.366295",125]