gemi 0.65.0 → 0.67.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/ai/Agent.d.ts +55 -1
  2. package/dist/ai/Agent.d.ts.map +1 -1
  3. package/dist/ai/AgentProvider.d.ts +8 -44
  4. package/dist/ai/AgentProvider.d.ts.map +1 -1
  5. package/dist/ai/ImageModel.d.ts +127 -0
  6. package/dist/ai/ImageModel.d.ts.map +1 -0
  7. package/dist/ai/ImageProvider.d.ts +94 -0
  8. package/dist/ai/ImageProvider.d.ts.map +1 -0
  9. package/dist/ai/index.d.ts +5 -0
  10. package/dist/ai/index.d.ts.map +1 -1
  11. package/dist/ai/index.js +12 -12
  12. package/dist/ai/index.js.map +12 -8
  13. package/dist/ai/providers/call.d.ts +12 -0
  14. package/dist/ai/providers/call.d.ts.map +1 -1
  15. package/dist/ai/providers/endpoints.d.ts +153 -0
  16. package/dist/ai/providers/endpoints.d.ts.map +1 -0
  17. package/dist/ai/providers/fakeProvider.d.ts +54 -0
  18. package/dist/ai/providers/fakeProvider.d.ts.map +1 -1
  19. package/dist/ai/providers/http.d.ts +28 -0
  20. package/dist/ai/providers/http.d.ts.map +1 -1
  21. package/dist/ai/providers/images.d.ts +126 -0
  22. package/dist/ai/providers/images.d.ts.map +1 -0
  23. package/dist/ai/store/Attachments.d.ts +52 -0
  24. package/dist/ai/store/Attachments.d.ts.map +1 -1
  25. package/dist/ai/types.d.ts +26 -0
  26. package/dist/ai/types.d.ts.map +1 -1
  27. package/dist/{chunk-se5ekvgb.js → chunk-43asnfa8.js} +1 -1
  28. package/dist/{chunk-nxg51m0q.js → chunk-j3p8c0xp.js} +1 -1
  29. package/dist/{chunk-fk127cwf.js → chunk-ka2qqsys.js} +2 -2
  30. package/dist/{chunk-fk127cwf.js.map → chunk-ka2qqsys.js.map} +1 -1
  31. package/dist/{chunk-x917yccy.js → chunk-rnztqasa.js} +1 -1
  32. package/dist/{chunk-j3b3qerv.js → chunk-sksks0tz.js} +1 -1
  33. package/dist/chunk-z7vmrf00.js +5 -0
  34. package/dist/{chunk-a85fn4zf.js.map → chunk-z7vmrf00.js.map} +4 -4
  35. package/dist/client/ProgressManager.d.ts +1 -1
  36. package/dist/facades/index.js +1 -1
  37. package/dist/http/HttpRequest.d.ts +18 -3
  38. package/dist/http/HttpRequest.d.ts.map +1 -1
  39. package/dist/http/index.js +1 -1
  40. package/dist/i18n/index.js +1 -1
  41. package/dist/kernel/index.js +1 -1
  42. package/dist/server/index.js +1 -1
  43. package/dist/services/file-storage/drivers/FileSystemDriver.d.ts +23 -1
  44. package/dist/services/file-storage/drivers/FileSystemDriver.d.ts.map +1 -1
  45. package/dist/services/index.js +1 -1
  46. package/package.json +1 -1
  47. package/dist/chunk-a85fn4zf.js +0 -5
  48. /package/dist/{chunk-se5ekvgb.js.map → chunk-43asnfa8.js.map} +0 -0
  49. /package/dist/{chunk-nxg51m0q.js.map → chunk-j3p8c0xp.js.map} +0 -0
  50. /package/dist/{chunk-x917yccy.js.map → chunk-rnztqasa.js.map} +0 -0
  51. /package/dist/{chunk-j3b3qerv.js.map → chunk-sksks0tz.js.map} +0 -0
@@ -1,24 +1,28 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../ai/signing.ts", "../ai/store/Attachments.ts", "../ai/store/sse.ts", "../ai/Agent.ts", "../services/mcp/toAgentTools.ts", "../ai/providers/errors.ts", "../ai/providers/http.ts", "../ai/providers/stream.ts", "../ai/providers/call.ts", "../ai/providers/capabilities.ts", "../ai/providers/request.ts", "../ai/AgentProvider.ts", "../ai/store/LiveRuns.ts", "../ai/store/MemoryAgentStore.ts", "../ai/AgentController.ts"],
3
+ "sources": ["../ai/signing.ts", "../ai/store/Attachments.ts", "../ai/store/sse.ts", "../ai/Agent.ts", "../services/mcp/toAgentTools.ts", "../ai/ImageModel.ts", "../ai/providers/endpoints.ts", "../ai/providers/errors.ts", "../ai/providers/http.ts", "../ai/providers/images.ts", "../ai/ImageProvider.ts", "../ai/providers/stream.ts", "../ai/providers/call.ts", "../ai/providers/capabilities.ts", "../ai/providers/request.ts", "../ai/AgentProvider.ts", "../ai/store/LiveRuns.ts", "../ai/store/MemoryAgentStore.ts", "../ai/AgentController.ts"],
4
4
  "sourcesContent": [
5
5
  "import { createHmac, randomBytes, timingSafeEqual } from \"crypto\";\n\n/**\n * Signing for pending tool calls.\n *\n * A pending call travels through the browser and comes back — in stateless mode\n * the whole history does — so the server cannot trust that what it gets back is\n * what it sent. Without a signature the client asserts not just *that* a call\n * was approved but *what* was approved, and nothing would stop it from\n * returning `approve: true` against an input it rewrote on the way. Signing is\n * what makes the round trip safe, and it is why approvals need no server-side\n * storage at all.\n *\n * What is signed, and what deliberately is not:\n *\n * signed — `runId`, `toolCallId`, the tool `name`, the `kind` of pending call\n * and a canonical serialization of the input, plus a nonce, an\n * expiry, and — for a call a sub-agent asked — the `path` of\n * tool-call ids it is nested under.\n * not — the client's answer. `approve: true` / `approve: false` and a\n * question's output are the *point* of asking; a client that flips\n * its own answer has refused, not forged. What the signature buys is\n * that the answer is bound to the call the server actually made,\n * with the input the server actually saw.\n *\n * A verified token is not yet an answer the server may act on: it says the\n * question was asked, not that it is still open. `consumePendingCall` at the\n * bottom of this file spends the nonce, which is what makes an approval\n * single-use — see the note there for what that guarantee is worth.\n *\n * `kind` is in there for a specific attack: an `approval`-kind call is one the\n * *server* runs, so a client that reused its signature on the \"here is the\n * output\" arm of `ClientToolResult` would be fabricating a server tool's result\n * rather than approving it. Binding the kind makes that a forgery instead of a\n * shape the caller has to remember to check.\n */\n\n/** Everything the signature commits to. */\nexport type PendingCallClaims = {\n runId: string;\n toolCallId: string;\n name: string;\n kind: \"approval\" | \"question\" | \"client\";\n input: unknown;\n /**\n * The chain of tool-call ids the call is nested under, outermost first.\n * Absent — or empty, which means the same thing — for a top-level call.\n *\n * A sub-agent's question reaches the user through its parent's pending list,\n * so `toolCallId` stops being an address on its own: two sub-runs under two\n * different tools can each hold a call the outer run never made. Binding the\n * path is what stops a token minted for a call nested under tool call X from\n * being replayed as a top-level call, or as one nested under Y.\n */\n path?: string[];\n};\n\nexport type SignOptions = {\n /** Overrides `process.env.SECRET`. Exists for tests; apps use the app key. */\n secret?: string;\n /**\n * Default 24 hours. An approval waits on a human, and humans go to lunch —\n * a short expiry turns \"I approved it after standup\" into an unexplained\n * failure. Long enough to survive a working day, short enough that a token\n * lifted from a log is not useful next month.\n */\n ttlMs?: number;\n /** Injected clock, so the expiry path is testable without waiting. */\n now?: number;\n};\n\nexport type VerifyOptions = {\n secret?: string;\n now?: number;\n};\n\n/**\n * A discriminated result rather than a boolean, because the two failures are\n * different events: `expired` is a sentence to show the user, `forged` is worth\n * logging and possibly alerting on. Collapsing them loses the only signal that\n * says someone is probing.\n */\nexport type VerifyResult =\n | { ok: true; runId: string; nonce: string; expiresAt: number }\n | { ok: false; reason: \"malformed\" | \"expired\" | \"forged\" };\n\nconst VERSION = \"agt1\";\nconst DEFAULT_TTL_MS = 24 * 60 * 60 * 1000;\n\nfunction secretKey(override?: string): string {\n const secret = override ?? process.env.SECRET;\n if (!secret) {\n // Refusing is the only safe answer. A fallback constant would make every\n // approval in every deployment forgeable by anyone who read this file, and\n // it would do it silently — the feature would appear to work.\n throw new Error(\n \"Signing a pending tool call needs an app secret. Set SECRET in the environment.\",\n );\n }\n return secret;\n}\n\n/**\n * Serializes a value so that the same value always produces the same string.\n *\n * `JSON.stringify` is not enough: it preserves insertion order, so an input\n * that made a round trip through a client — parsed and re-serialized, with the\n * keys in whatever order the parser produced — would hash differently and a\n * legitimate approval would come back looking forged. Keys are sorted,\n * `undefined` members are dropped (they do not survive JSON anyway), and arrays\n * keep their order because in an array order *is* the value.\n */\nexport function canonicalize(value: unknown): string {\n if (value === null || typeof value !== \"object\") {\n return JSON.stringify(value) ?? \"null\";\n }\n if (Array.isArray(value)) {\n return `[${value.map(canonicalize).join(\",\")}]`;\n }\n const record = value as Record<string, unknown>;\n const keys = Object.keys(record)\n .filter((key) => record[key] !== undefined)\n .sort();\n return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalize(record[key])}`).join(\",\")}}`;\n}\n\n/**\n * Length-prefixed rather than delimiter-joined. A separator is a place for two\n * different claim sets to hash the same — `runId: \"a\", toolCallId: \"b|c\"` and\n * `runId: \"a|b\", toolCallId: \"c\"` — and while neither field contains the\n * separator today, that is a property of the id generator, not of this code.\n */\nfunction payload(fields: string[]): string {\n return fields.map((field) => `${field.length}:${field}`).join(\"\");\n}\n\nfunction mac(secret: string, fields: string[]): Buffer {\n return createHmac(\"sha256\", secret).update(payload(fields)).digest();\n}\n\n/**\n * The path is appended, and only when there is one.\n *\n * Byte-identical output for a call with no path is the whole requirement here:\n * every approval already in flight was minted from the eight fields below, and\n * a ninth field carrying `\"[]\"` or `\"undefined\"` would invalidate all of them\n * on deploy — the user who clicked Approve before the release would be told\n * their answer was forged. So an absent path adds nothing at all, and an empty\n * array is treated as absent because it says the same thing.\n *\n * `payload` is length-prefixed, so appending a field cannot collide with a\n * longer value in the one before it; that is why this can be an append rather\n * than a new version tag.\n */\nfunction claimFields(claims: PendingCallClaims, nonce: string, expiresAt: number): string[] {\n const fields = [\n VERSION,\n claims.runId,\n claims.toolCallId,\n claims.name,\n claims.kind,\n nonce,\n String(expiresAt),\n canonicalize(claims.input),\n ];\n if (claims.path && claims.path.length > 0) {\n fields.push(canonicalize(claims.path));\n }\n return fields;\n}\n\nconst encode = (value: string) => Buffer.from(value, \"utf8\").toString(\"base64url\");\nconst decode = (value: string) => Buffer.from(value, \"base64url\").toString(\"utf8\");\n\n/** `agt1.<runId>.<nonce>.<expiry>.<mac>`, all base64url or base36. */\nexport function signPendingCall(claims: PendingCallClaims, options: SignOptions = {}): string {\n const secret = secretKey(options.secret);\n const now = options.now ?? Date.now();\n const expiresAt = now + (options.ttlMs ?? DEFAULT_TTL_MS);\n const nonce = randomBytes(12).toString(\"base64url\");\n const signature = mac(secret, claimFields(claims, nonce, expiresAt)).toString(\"base64url\");\n return [VERSION, encode(claims.runId), nonce, expiresAt.toString(36), signature].join(\".\");\n}\n\n/**\n * The metadata a signature carries in the clear.\n *\n * `Agent` needs the issuing `runId` before it can verify anything: the call was\n * signed under the run that made it, and the turn answering it is a *new* run\n * with a new id. Reading it out of the token is safe because the token's own\n * MAC covers it — a client that edits the runId here fails verification, so\n * this is \"which run does this claim to belong to\", not \"which run does the\n * client say it belongs to\".\n */\nexport function readSignature(\n signature: string,\n): { runId: string; nonce: string; expiresAt: number } | null {\n return readToken(signature, VERSION);\n}\n\n/**\n * The version tag is checked here and nowhere else, which is what keeps the\n * two token kinds apart: a parked-run record presented where a pending call's\n * signature is expected fails as malformed before its MAC is even looked at,\n * and the other way round. Their claim sets are different lengths and would\n * not collide anyway, but a tag makes that a rule rather than an accident of\n * the field count.\n */\nfunction readToken(\n signature: string,\n version: string,\n): { runId: string; nonce: string; expiresAt: number } | null {\n const parts = signature.split(\".\");\n if (parts.length !== 5 || parts[0] !== version) {\n return null;\n }\n const expiresAt = Number.parseInt(parts[3], 36);\n if (!Number.isFinite(expiresAt)) {\n return null;\n }\n try {\n return { runId: decode(parts[1]), nonce: parts[2], expiresAt };\n } catch {\n return null;\n }\n}\n\nexport function verifyPendingCall(\n signature: string,\n claims: PendingCallClaims,\n options: VerifyOptions = {},\n): VerifyResult {\n const secret = secretKey(options.secret);\n const parsed = readSignature(signature);\n if (!parsed) {\n return { ok: false, reason: \"malformed\" };\n }\n\n const presented = Buffer.from(signature.split(\".\")[4], \"base64url\");\n const expected = mac(secret, claimFields(claims, parsed.nonce, parsed.expiresAt));\n // `timingSafeEqual` throws on a length mismatch, and a wrong length is\n // already a public fact about the token — nothing is leaked by checking it\n // first, and everything is leaked by comparing the bytes with `===`.\n if (presented.length !== expected.length || !timingSafeEqual(presented, expected)) {\n return { ok: false, reason: \"forged\" };\n }\n\n // Expiry is checked *after* the MAC on purpose: only a genuine token can be\n // \"expired\". Reporting a forgery as expired would tell the UI to say \"your\n // approval timed out\" to someone who was tampering.\n if ((options.now ?? Date.now()) > parsed.expiresAt) {\n return { ok: false, reason: \"expired\" };\n }\n\n return { ok: true, runId: parsed.runId, nonce: parsed.nonce, expiresAt: parsed.expiresAt };\n}\n\n// --- parked sub-runs -----------------------------------------------------\n\n/**\n * What a parked sub-run's record is signed over.\n *\n * `ToolCallPart.nested` is the parent's own record of where a sub-run stopped,\n * and in stateless mode it makes the same trip through the browser a pending\n * call does. The next turn *runs a tool* on the strength of that record — the\n * tool is re-entered because the record says a sub-run under it is waiting on\n * the question being answered — so an unsigned record lets the client choose\n * which tools run, with what input, before any answer is verified. A MAC over\n * what the server actually recorded is what makes the record safe to carry.\n *\n * Only a parked record is signed, because only a parked record executes\n * anything: a finished sub-run is replayed out of its transcript and spends\n * nothing, and a client that forges one has fed its own tool a made-up answer,\n * which a client-carried history already allows everywhere.\n *\n * Deliberately not signed: the transcript. The sub-run resumes from messages\n * the client carried, exactly as the parent does in stateless mode, and the\n * same argument applies — what the signature pins is that the server parked\n * *here*, on *these* calls, with *this* input, and not what was said on the\n * way.\n */\nexport type NestedRunClaims = {\n /** The root run's id, the one every pending call of the tree is minted under. */\n runId: string;\n /**\n * Tool-call ids from the root down to and including the call the record\n * hangs off. A sub-run's id is not an address on its own for the same reason\n * a nested call's is not: two sub-runs under two different tools can carry\n * the same one.\n */\n path: string[];\n /** The sub-run's own id, so a record cannot be moved between sub-runs. */\n nestedRunId: string;\n /** The tool calls the sub-run is waiting on: every call left open in its transcript. */\n open: string[];\n /**\n * The input the tool that parked was running on, as the transcript carries\n * it. Re-entry runs the tool body with the input the history holds, and the\n * history is the client's — so a record that pinned where the sub-run parked\n * but not what its tool was given would let the client keep the run and\n * rewrite the arguments to anything the schema accepts. The same bargain a\n * pending call makes: the input executed is the input signed.\n */\n input: unknown;\n};\n\nconst NESTED_VERSION = \"agn1\";\n\nfunction nestedFields(claims: NestedRunClaims, nonce: string, expiresAt: number): string[] {\n return [\n NESTED_VERSION,\n claims.runId,\n canonicalize(claims.path),\n claims.nestedRunId,\n nonce,\n String(expiresAt),\n // A set, so it is sorted: the ids are read back out of a transcript the\n // client re-serialized, and message order is not part of the claim.\n canonicalize([...claims.open].sort()),\n canonicalize(claims.input),\n ];\n}\n\n/**\n * Same shape as a pending call's token, so the same reader serves both — nonce\n * included, and the nonce is spent, by `consumeNestedRun` below. A verified\n * record is permission to run the tool it hangs off, and the tool body runs\n * before the sub-run gets to look at the answer's own nonce; a record that\n * could be presented twice would run the body twice before anything refused\n * the replay. Every park mints a fresh record, so spending one costs a\n * legitimate re-park nothing.\n */\nexport function signNestedRun(claims: NestedRunClaims, options: SignOptions = {}): string {\n const secret = secretKey(options.secret);\n const now = options.now ?? Date.now();\n const expiresAt = now + (options.ttlMs ?? DEFAULT_TTL_MS);\n const nonce = randomBytes(12).toString(\"base64url\");\n const signature = mac(secret, nestedFields(claims, nonce, expiresAt)).toString(\"base64url\");\n return [NESTED_VERSION, encode(claims.runId), nonce, expiresAt.toString(36), signature].join(\".\");\n}\n\n/**\n * The run that verifies a record is never the run that minted it — the turn\n * answering a question is a new run with a new id — so the minting run's id is\n * read out of the token rather than asked of the caller, which has no other\n * source for it. The MAC covers it, so a client that edits the id in the clear\n * fails here rather than being believed.\n */\nexport function verifyNestedRun(\n signature: string,\n claims: Omit<NestedRunClaims, \"runId\">,\n options: VerifyOptions = {},\n): VerifyResult {\n const secret = secretKey(options.secret);\n const parsed = readToken(signature, NESTED_VERSION);\n if (!parsed) {\n return { ok: false, reason: \"malformed\" };\n }\n\n const presented = Buffer.from(signature.split(\".\")[4], \"base64url\");\n const expected = mac(\n secret,\n nestedFields({ ...claims, runId: parsed.runId }, parsed.nonce, parsed.expiresAt),\n );\n if (presented.length !== expected.length || !timingSafeEqual(presented, expected)) {\n return { ok: false, reason: \"forged\" };\n }\n\n // A parked record outlives its usefulness with the answers it exists to\n // deliver: those expire on the pending call's TTL, and a record older than\n // that can route nothing that would still verify.\n if ((options.now ?? Date.now()) > parsed.expiresAt) {\n return { ok: false, reason: \"expired\" };\n }\n\n return { ok: true, runId: parsed.runId, nonce: parsed.nonce, expiresAt: parsed.expiresAt };\n}\n\n// --- single use ----------------------------------------------------------\n\n/**\n * Nonces already spent, mapped to the moment they stop mattering.\n *\n * The MAC makes a token unforgeable; it does not make it single-use. Without\n * this a captured signature approves the same call again every time it is\n * presented, because a token that carries its own `runId` is a token that\n * asserts its own binding — which is no binding at all. Verifying tells you the\n * server once asked this exact question; spending the nonce is what says nobody\n * has answered it yet.\n *\n * Deliberately in memory, and deliberately not a hard guarantee:\n *\n * bounded — an entry lives at most as long as the token's TTL, and the sweep\n * below is amortized O(1), so the map is bounded by the approvals\n * actually issued in one TTL window rather than by uptime.\n * local — one process. A second replica has never seen the nonce and will\n * accept it, so this closes the replay window rather than sealing\n * it. That is still worth having and it fails open, which is the\n * only direction a cache may fail: a lost registry costs a replay,\n * never a legitimate approval that stops working.\n *\n * The stronger guard is the app's own message store: once a call has a result\n * next to it, the call is no longer open and the answer has nothing to attach\n * to. This is what stands in for that in stateless mode, where the history the\n * client returns can be rewound to before the result existed.\n */\nconst spent = new Map<string, number>();\n\n/** Sweep when the map has grown past this, so sweeping costs O(1) per insert\n * amortized instead of walking every entry on every approval. */\nlet sweepAt = 1024;\n\nfunction sweep(now: number) {\n for (const [nonce, expiresAt] of spent) {\n if (expiresAt <= now) spent.delete(nonce);\n }\n sweepAt = Math.max(1024, spent.size * 2);\n}\n\n/**\n * Spends a signature's nonce. `false` means it was already spent — the answer\n * is a replay and must not be acted on.\n *\n * Separate from `verifyPendingCall` rather than folded into it, because verify\n * is a pure question a caller may want to ask twice (logging a forgery, say)\n * and this one is a state change that must happen exactly once per answer.\n */\nexport function consumePendingCall(signature: string, options: VerifyOptions = {}): boolean {\n return spend(readSignature(signature), options);\n}\n\n/**\n * Spends a parked-run record's nonce, on the same registry and the same terms.\n * `false` means the record has already re-entered its tool once — the turn is\n * a replay of a history from before the answer was delivered, and the body\n * must not run again on it.\n */\nexport function consumeNestedRun(signature: string, options: VerifyOptions = {}): boolean {\n return spend(readToken(signature, NESTED_VERSION), options);\n}\n\nfunction spend(\n parsed: { nonce: string; expiresAt: number } | null,\n options: VerifyOptions,\n): boolean {\n if (!parsed) return false;\n const now = options.now ?? Date.now();\n if (spent.size >= sweepAt) sweep(now);\n const spentUntil = spent.get(parsed.nonce);\n // A record past its own expiry binds nothing: the token it refers to fails\n // verification on its own, so holding the nonce would only grow the map.\n if (spentUntil !== undefined && spentUntil > now) return false;\n spent.set(parsed.nonce, parsed.expiresAt);\n return true;\n}\n",
6
- "import type {\n PutFileParams,\n ReadFileParams,\n ReadResult,\n} from \"../../services/file-storage/drivers/types\";\n\n/**\n * The bytes of an upload, kept by gemi, and the scope that says whose they are.\n *\n * WHY THIS EXISTS. `AgentController.upload` used to hand the file straight to\n * the model provider and keep nothing, which is the trade `AgentProvider.upload`\n * documents — provider file ids in the history, no storage story. That is a fine\n * trade for vision and it fails the moment a *tool* needs the file: the user\n * uploads an image and asks the agent to create a product, `POST /products`\n * wants multipart bytes, and the server is holding a provider file id and\n * nothing else. The model cannot make up the difference — it *saw* the image, it\n * cannot reproduce it — and the only recovery left is downloading from the\n * vendor, which is a round trip for bytes we held minutes ago, is not declared\n * anywhere (`AgentProvider` has `upload` and no counterpart), and is subject to\n * a retention policy that is not ours.\n *\n * WHY THE SCOPE IS NOT OPTIONAL. Once a tool takes an attachment id as an\n * argument, that id arrives *from the model*, and a model's arguments are as\n * untrusted as a request body: whatever the model wrote is a function of what it\n * read, and it reads the user's text, tool output, and any document it was\n * shown. A store that resolves ids globally therefore lets a prompt-injected\n * model name another tenant's upload and have the server fetch it — the model as\n * confused deputy, spending the server's own credentials.\n *\n * So there is no `find(id)` on this interface, only `find(scope, id)`, and the\n * thing a tool is handed is a `ScopedAttachments` whose methods take no scope at\n * all because it already closed over one. The unscoped read is not discouraged,\n * it is unspellable: the tool has no object to call it on. The scope itself is\n * derived from the server's own knowledge of the request — see\n * `AgentController.attachmentScope` — and never from anything the model can\n * write.\n */\n\n/**\n * The subject an attachment belongs to, as one opaque string.\n *\n * ONE STRING, COMPARED WITH `===`, RATHER THAN A STRUCTURE OF user/thread/org\n * fields. gemi does not have an opinion about tenancy — there is no `Org` in\n * this framework — so a structured scope would have had to be a bag of optional\n * fields, and a bag of optional fields has a worst member: `{}`, which under any\n * partial-match rule matches everything. Every such rule admits `{}` — or\n * `{ userId: undefined }`, which is the same thing arrived at by a typo — so one\n * forgetful line in an app turns the scope check into a no-op that still\n * typechecks. An opaque key has no such member: an app that wants org isolation\n * writes `org:${org.id}` and gets it, and an app that decides a request has no\n * subject returns `null` from `attachmentScope()` and gets no attachment ids at\n * all, rather than a bucket everyone shares.\n *\n * It is an object rather than a bare `string` for one reason, and it is a real\n * one: `find(scope, id)` taking two bare strings is a signature whose arguments\n * can be transposed, and the transposition typechecks, runs, and is a scope\n * check comparing an id against an id. The wrapper makes that a compile error.\n *\n * The key is never shown to the model and never sent to the client. It is a\n * server-side comparison value; it is not a path component and is not parsed.\n */\nexport type AttachmentScope = { readonly key: string };\n\n/**\n * Where an upload's bytes were sent. See `AgentController.attachmentDestination`.\n *\n * `\"provider\"` DESCRIBES A RECORD `upload` NEVER WRITES. A file that goes to the\n * provider alone leaves gemi holding nothing to resolve — `read` and `file`\n * would throw for it — so the route answers `fileId` and mints no attachment id\n * at all, and there is no id for anyone to look up its name with. The value is\n * on this type because a store *can* hold such a row (a custom `AttachmentStore`\n * filing provider uploads for its own bookkeeping is a reasonable thing to\n * write, and `ScopedAttachments` handles it correctly), not because the shipped\n * route produces one.\n */\nexport type AttachmentDestination = \"both\" | \"provider\" | \"storage\";\n\n/**\n * One recorded upload.\n *\n * `fileId` and `objectName` are both optional and at least one is always\n * present, because which of them exists is exactly the destination decision:\n * `provider` has a `fileId` and no bytes of ours, `storage` has bytes and no\n * `fileId`, `both` has both.\n */\nexport type Attachment = {\n /**\n * gemi's id for the upload — the one a tool takes as an argument, and the only\n * id in this module that may be shown to a model.\n *\n * Prefixed `gemi_att_` so that the mistake everyone makes once — a gemi\n * attachment id put into `FilePart.fileId`, which is a *provider* id — is\n * caught by a sentence to read rather than by whatever the vendor says about\n * a file it has never heard of, mid-conversation. `toResponsesInput` checks\n * for the prefix; the vendor's own answer to a bogus `file_id` has not been\n * measured, and the guard does not depend on it.\n */\n id: string;\n /**\n * The scope this was filed under, carried on the record so `ScopedAttachments`\n * can check it a second time. See `ScopedAttachments.get` for why the second\n * check is not redundant.\n */\n scopeKey: string;\n /** The provider file id, when the file was sent to the provider. */\n fileId?: string;\n /** The storage object name, when the bytes were kept. Feed it to `read`. */\n objectName?: string;\n /** The client's filename, kept so a tool can forward the file under it. */\n name: string;\n mimeType: string;\n size: number;\n createdAt: string;\n destination: AttachmentDestination;\n};\n\n/**\n * Where attachment *records* live. The bytes are in `AttachmentStorage`; this\n * holds the row that says which bytes, whose, and under what id.\n *\n * The default is `MemoryAttachmentStore`, which dies with the process, exactly\n * like `MemoryAgentStore`. An app that wants an attachment to outlive a deploy\n * implements this over a table — `id` primary key, `scope_key` column — and\n * assigns it to `AgentController.attachments`.\n *\n * IF YOU IMPLEMENT THIS, THE ONE THING YOU MUST NOT DO is answer `find` from the\n * id alone. `SELECT * FROM attachments WHERE id = ?` is the leak this module\n * exists to prevent, and it passes every test an app writes about its own\n * uploads, because those uploads are always in scope. It is wrong only for an id\n * that came from somewhere else, which is to say only when it matters. The\n * clause is `WHERE id = ? AND scope_key = ?`.\n */\nexport interface AttachmentStore {\n /** Records an attachment under `scope`. The caller has already minted the id. */\n put(scope: AttachmentScope, attachment: Attachment): Promise<void>;\n /**\n * The attachment with this id *within this scope*, or `null`.\n *\n * `null` for an id that does not exist and `null` for an id that exists under\n * another scope, and the caller cannot tell which — deliberately. See\n * `AttachmentNotFoundError`.\n */\n find(scope: AttachmentScope, id: string): Promise<Attachment | null>;\n}\n\n/**\n * The bytes half: whatever the app's file storage is.\n *\n * Structurally satisfied by the `Storage` facade, which is what\n * `AgentController` defaults to. Declared as an interface of two methods rather\n * than as `typeof Storage` so a test — or an app with a second bucket for user\n * uploads — can pass something else without standing up a container.\n */\nexport interface AttachmentStorage {\n put(params: PutFileParams | Blob): Promise<string>;\n read(params: ReadFileParams | string): Promise<ReadResult>;\n}\n\n/**\n * What both a missing id and someone else's id answer.\n *\n * THE MESSAGE MUST NOT SAY WHICH. \"Exists but is not yours\" and \"does not exist\"\n * are two answers to a question the caller is not entitled to ask, and a store\n * that distinguishes them is an oracle: a prompt-injected model that can tell\n * the two apart can walk an id space and report back which ids are live, which\n * is a tenant census delivered through the very tool the injection was aimed at.\n * One error, one wording, one code, for both.\n *\n * A plain `Error` rather than a `RequestBreakerError`: this is thrown inside a\n * tool, where the agent loop turns a throw into a tool result the model reads\n * and can recover from, not inside a route where it would decide a status code.\n */\nexport class AttachmentNotFoundError extends Error {\n readonly code = \"attachment_not_found\";\n constructor(public readonly id: string) {\n super(`No attachment ${id}.`);\n this.name = \"AttachmentNotFoundError\";\n }\n}\n\n/** Raised for a scope no caller could have meant. See `assertScope`. */\nexport class InvalidAttachmentScopeError extends Error {\n readonly code = \"invalid_attachment_scope\";\n constructor(message: string) {\n super(message);\n this.name = \"InvalidAttachmentScopeError\";\n }\n}\n\n/**\n * Rejects the empty scope.\n *\n * `{ key: \"\" }` is what an app produces by writing `` `org:${org?.id ?? \"\"}` ``\n * for a request that has no org, and it is the one value that must never reach a\n * store: it compares equal to itself, so every subject-less upload in the\n * process lands in one shared bucket that every subject-less caller can read out\n * of. That is the global store this module exists not to be, arrived at by a\n * template literal. Failing here is loud, happens on the first request, and\n * names the method to fix.\n */\nfunction assertScope(scope: AttachmentScope): AttachmentScope {\n if (!scope || typeof scope.key !== \"string\" || scope.key.length === 0) {\n throw new InvalidAttachmentScopeError(\n \"An attachment scope needs a non-empty key. Return `null` from `attachmentScope()` for a request that has no subject — an empty key is a bucket every caller shares.\",\n );\n }\n return scope;\n}\n\n/** The prefix on every gemi attachment id. See `Attachment.id`. */\nexport const ATTACHMENT_ID_PREFIX = \"gemi_att_\";\n\nexport function newAttachmentId(): string {\n return `${ATTACHMENT_ID_PREFIX}${crypto.randomUUID()}`;\n}\n\n/**\n * The handle a tool is given, and the only attachment API a tool should ever\n * see.\n *\n * IT TAKES NO SCOPE, ON ANY METHOD. That is the whole design, and it is not a\n * convenience: an API where the scope is a parameter is an API where the scope\n * is a parameter someone passes the wrong thing to — and the wrong thing here is\n * whatever the model wrote, because inside a tool body the model's arguments are\n * the variables closest to hand. Handing the tool an object that has already\n * closed over the server's answer leaves nothing to pass. `resolve(id, scope)`\n * was the rejected shape: it is the same code with the mistake still available.\n */\nexport class ScopedAttachments {\n constructor(\n private readonly store: AttachmentStore,\n private readonly storage: AttachmentStorage,\n private readonly scope: AttachmentScope,\n ) {\n assertScope(scope);\n }\n\n /**\n * The record for `id`, or `AttachmentNotFoundError`.\n *\n * THE SCOPE IS CHECKED TWICE HERE, and the second check is the one that\n * matters. `store.find` is an app's code — a table lookup somebody wrote, with\n * a `WHERE` clause somebody has to have remembered — and the failure mode of\n * forgetting the scope clause is a query that works perfectly for every upload\n * the app's own tests make. Comparing `record.scopeKey` again is three lines\n * that hold even when that clause is missing, so a careless store finds nothing\n * across scopes instead of leaking.\n *\n * It is not defence against a *malicious* store — an app's store is the app's\n * own code and could return anything. It is defence against the ordinary\n * version of this bug, which is the one that ships.\n */\n async get(id: string): Promise<Attachment> {\n const record = await this.store.find(this.scope, id);\n if (!record || record.scopeKey !== this.scope.key) {\n throw new AttachmentNotFoundError(id);\n }\n return record;\n }\n\n /**\n * The bytes, streaming, with the metadata `createStreamResponse` wants.\n *\n * Throws `AttachmentNotFoundError` for an attachment that exists but whose\n * bytes were never kept — a `provider`-destination upload. The same error as\n * an unknown id, for the reason on that class: \"it is at the vendor and not\n * here\" is information about someone else's upload as readily as about your\n * own.\n */\n async read(id: string): Promise<ReadResult> {\n return await this.readRecord(id, await this.get(id));\n }\n\n /**\n * The bytes for a record already resolved, so `file()` does one lookup rather\n * than two. `store.find` is a `SELECT` in any real store, and the second one\n * was not a second check of anything — same scope, same id, same row.\n *\n * It still takes the id the *caller* asked for, rather than reading\n * `record.id`, so the error stays a function of the caller's own input: an app\n * store that answers with the wrong row must not get to choose which id\n * appears in a message the model may read.\n */\n private async readRecord(id: string, record: Attachment): Promise<ReadResult> {\n if (!record.objectName) {\n throw new AttachmentNotFoundError(id);\n }\n return await this.storage.read({ name: record.objectName });\n }\n\n /**\n * The bytes as a `File`, under the name and type they were uploaded with —\n * which is the shape the motivating case actually needs, because\n * `form.append(\"image\", file)` is what a tool forwarding an upload to a\n * multipart endpoint writes.\n *\n * Buffers, and says so. `read()` is there for anything large enough that\n * buffering it is the wrong call.\n */\n async file(id: string): Promise<File> {\n const record = await this.get(id);\n const result = await this.readRecord(id, record);\n const body = result.body;\n const blob =\n body instanceof Blob ? body : body ? await new Response(body).blob() : new Blob([]);\n return new File([blob], record.name, { type: record.mimeType });\n }\n\n /**\n * Stores bytes a tool produced, under this caller's scope, and answers the\n * record.\n *\n * Here rather than on the store because the id, the object name and the scope\n * all have to be decided together, and because an attachment a tool created\n * has to land in the scope the tool is reading from — otherwise a tool writes\n * a file the next tool of the same run cannot see.\n *\n * Nothing is sent to the provider: bytes a tool made are bytes for the app,\n * and a tool that wants the model to look at its output says so by returning\n * something the model can read. Issue #490 builds `ctx.attachments.put(blob)`\n * on exactly this.\n */\n async put(\n blob: Blob,\n params: { name?: string; mimeType?: string; fileId?: string } = {},\n ): Promise<Attachment> {\n const id = newAttachmentId();\n const mimeType = params.mimeType ?? blob.type ?? \"\";\n const name = params.name ?? (blob instanceof File ? blob.name : id);\n const objectName = await this.storage.put({\n name: attachmentObjectName(id, name),\n body: blob,\n contentType: mimeType || undefined,\n });\n const record: Attachment = {\n id,\n scopeKey: this.scope.key,\n objectName,\n name,\n mimeType: mimeType || \"application/octet-stream\",\n size: blob.size,\n createdAt: new Date().toISOString(),\n // `both` when the caller has already sent the same bytes to the provider,\n // which is what `ctx.attachments.put(blob, { showModel: true })` does\n // before it gets here.\n //\n // The parameter exists rather than a second `markShown` call because the\n // alternative is a row that is briefly, and then permanently if the second\n // write fails, a lie: `destination` is the field that says where the bytes\n // went, and a file sitting in the vendor's file list under a record that\n // says `storage` is exactly the discrepancy the field was added to close.\n // Nothing here uploads — the provider is the run's, not this module's —\n // so the id is passed in rather than produced.\n destination: params.fileId ? \"both\" : \"storage\",\n ...(params.fileId ? { fileId: params.fileId } : {}),\n };\n await this.store.put(this.scope, record);\n return record;\n }\n}\n\n/**\n * What a tool asks for when it parks bytes.\n *\n * `showModel` is the whole of issue #490 in one flag: without it a tool's\n * output is an id in a string, which the model can quote back and cannot look\n * at, so \"edit this image\" produces a file nobody but the app ever sees. With\n * it the same bytes also go to the provider and come back into the transcript\n * as an input-role message the next step is built on — generate, look, fix.\n *\n * It is a flag rather than the default because the default costs money on every\n * call. The reasoning is `capabilitiesForModel`'s, run the other way round: a\n * file the model did not need and was shown anyway is an upload plus a set of\n * image tokens on every subsequent request of the run, on an invoice, for a\n * tool whose author never asked to be looked at. A file the model needed and\n * was not shown is a tool that returned an id, which is what tools did before\n * this existed and is visible in the transcript the moment anyone reads it. The\n * expensive mistake is the silent one here, so the expensive thing is opt in.\n */\nexport type PutAttachmentParams = {\n /** The filename to keep, so a later `file()` hands the bytes back under it. */\n name?: string;\n /** Overrides `blob.type`, which a `Blob` built from raw bytes does not have. */\n mimeType?: string;\n /**\n * Also send these bytes to the model provider and put them in front of the\n * model as an input-role message, once this tool call settles.\n *\n * Refused, loudly, by a provider whose `capabilities.fileInput` is false: the\n * request builder drops a file part such a provider cannot read, and an\n * upload paid for, stored, and then dropped on the way to the wire is the\n * silent-forever failure — the tool reports success, the model answers about\n * an image it was never shown, and nothing anywhere says why.\n */\n showModel?: boolean;\n};\n\n/**\n * The attachment API a tool is given, as `ctx.attachments`.\n *\n * Everything a `ScopedAttachments` does, plus `showModel`, plus the memo that\n * makes a re-entered tool call idempotent. It is a separate interface from\n * `ScopedAttachments` because those two additions are not properties of the\n * scope, they are properties of *one tool call*: the run has to know which call\n * a `put` belongs to in order to replay it, and the object handed to a tool is\n * therefore built per call — the same shape `ctx.runAgent` has, for the same\n * reason.\n *\n * The read half (`get`, `read`, `file`) is delegated to the `ScopedAttachments`\n * unchanged, including its two scope checks. A tool reading a file it did not\n * create is reading an id the model wrote, and #489's whole argument applies to\n * it word for word.\n */\nexport interface ToolAttachments {\n /**\n * Parks bytes under this caller's scope and answers the record.\n *\n * ON A REPLAY THIS STORES NOTHING. An escalating tool is re-entered from the\n * top on the next turn, so the body that built this blob has run before and\n * built one already; the record from that first attempt is what comes back,\n * and the bytes handed in now are dropped. That is the same bargain\n * `ctx.runAgent` makes and it is not avoidable: the id from the first attempt\n * is already in the transcript the model read, so minting a second one would\n * leave the model holding an id for bytes nobody kept — and uploading the\n * second copy would pay the vendor twice and put the same image into the\n * context twice. Work *before* a `put` still runs again; if producing the\n * bytes is what costs, branch on `ctx.resumed`.\n */\n put(blob: Blob, params?: PutAttachmentParams): Promise<Attachment>;\n /** The record for `id`, or `AttachmentNotFoundError`. Scoped. */\n get(id: string): Promise<Attachment>;\n /** The bytes, streaming. Scoped. */\n read(id: string): Promise<ReadResult>;\n /** The bytes as a `File`, under their original name and type. Scoped. */\n file(id: string): Promise<File>;\n}\n\n/**\n * One successful `ctx.attachments.put` of one tool call, written down so the\n * next turn can replay it instead of doing it again.\n *\n * Lives on `ToolCallPart.attachments`, indexed by the order the puts happened\n * in — exactly where and how `ToolCallPart.nested` records sub-runs, and for\n * exactly the same reason: the message history is the only state that survives\n * a turn boundary, in a thread and in the browser both, so a memo that is not\n * on the message is a memo a stateless app does not have.\n *\n * `shown` is absent for a plain `put`. When present it carries the provider\n * file id AND the identity of the message injected for it, because both have to\n * come back byte for byte: a replay that minted a fresh message id would put a\n * second copy of the same image in the transcript, and a client that had\n * already applied the first would show it twice.\n */\nexport type ToolAttachmentPut = {\n /** What `put` answered, replayed verbatim. */\n attachment: Attachment;\n /** Set when `showModel` was asked for. See above. */\n shown?: {\n /** The provider's file id — what the injected `FilePart` carries. */\n fileId: string;\n /** The injected message's id, so a replay reproduces it rather than a twin. */\n messageId: string;\n /** Its `createdAt`, for the same reason. */\n createdAt: string;\n };\n};\n\n/**\n * A slot in that list, which is one `put` — or one `put` that threw.\n *\n * The failure arm exists because the index is what identifies a `put`, and an\n * index is taken the moment the call is made rather than when it succeeds: a\n * body that runs its puts concurrently must number them by the order it *asked*\n * and not by the order the network answered, or the next turn will not\n * reproduce the numbering. So a `put` that throws and is caught — a provider\n * that cannot read files, a vendor that refuses the type, a storage write that\n * fails — leaves a slot behind, and a later successful `put` in the same call\n * would otherwise leave a hole in front of it. A hole is `null` once it has\n * been through JSON, which is what a consumer walking `part.attachments`\n * crashes on; `{ failed: true }` is the same fact said out loud.\n *\n * It is written lazily, only when a later `put` needs the index above it, so a\n * tool whose puts all threw still adds no `attachments` field at all. And it is\n * not a memo: a replayed slot marked `failed` is re-attempted from scratch,\n * because there is nothing recorded to hand back and the failure may have been\n * the network's rather than the tool's.\n */\nexport type ToolAttachmentRecord = ToolAttachmentPut | { failed: true };\n\n/**\n * The object name an attachment's bytes are stored under.\n *\n * The scope is deliberately NOT in the path. A scope key is app-authored text —\n * `org:${slug}`, with whatever a slug turns out to be — and putting arbitrary\n * app text into an object name is how a `..` or a `/` ends up somewhere nobody\n * meant on the one driver that resolves names literally (`FileSystemDriver`\n * concatenates them onto a folder path). The record is what carries the scope;\n * the object name only has to be unique, and a uuid already is.\n *\n * The extension is carried over from the client's filename when it looks like\n * one, because `FileSystemDriver` reads a stored object's content type back off\n * its path and would otherwise answer `application/octet-stream` for everything.\n * `Attachment.mimeType` is the authority either way — this is for anything that\n * reads the bucket without the record.\n */\nexport function attachmentObjectName(id: string, name: string): string {\n const match = /\\.([A-Za-z0-9]{1,8})$/.exec(name);\n return `attachments/${id}${match ? `.${match[1]!.toLowerCase()}` : \"\"}`;\n}\n\n/**\n * The default: attachment records last as long as the process.\n *\n * Modelled as one flat map keyed by id, holding the scope beside the record and\n * comparing it in `find` — rather than as a map of maps, which would make the\n * scoping structural and unforgettable. That looks like the safer shape and is\n * the wrong reference implementation: an app's store is a table with an `id`\n * primary key and a `scope_key` column, and its `find` is a `WHERE` clause that\n * can be written wrong. The store gemi ships should have the same failure\n * available to it as the store an app writes, so that a test written against\n * this one is a test of the check every real store also has to make.\n */\nexport class MemoryAttachmentStore implements AttachmentStore {\n private readonly rows = new Map<string, Attachment>();\n\n async put(scope: AttachmentScope, attachment: Attachment): Promise<void> {\n assertScope(scope);\n this.rows.set(attachment.id, { ...attachment, scopeKey: scope.key });\n }\n\n async find(scope: AttachmentScope, id: string): Promise<Attachment | null> {\n assertScope(scope);\n const row = this.rows.get(id);\n // The scope clause. An id belonging to another scope answers `null`, which\n // is the answer an id that does not exist gets — see\n // `AttachmentNotFoundError` for why the two must be indistinguishable.\n if (!row || row.scopeKey !== scope.key) {\n return null;\n }\n return row;\n }\n\n /** Test and dev affordance: how many rows are held, across every scope. */\n get size(): number {\n return this.rows.size;\n }\n}\n\n/**\n * The process-wide default, so an app that configures nothing still gets\n * attachment ids that work for the length of a conversation.\n *\n * Module-level for the reason `defaultAgentStore` is: the controller is\n * constructed per request, so a store assigned as `new MemoryAttachmentStore()`\n * in a field initializer is an empty store on every request, and every\n * attachment id is a miss one turn after it was minted.\n */\nexport const defaultAttachmentStore = new MemoryAttachmentStore();\n",
6
+ "import type {\n PutFileParams,\n ReadFileParams,\n ReadResult,\n} from \"../../services/file-storage/drivers/types\";\nimport type { Usage } from \"../types\";\n\n/**\n * The bytes of an upload, kept by gemi, and the scope that says whose they are.\n *\n * WHY THIS EXISTS. `AgentController.upload` used to hand the file straight to\n * the model provider and keep nothing, which is the trade `AgentProvider.upload`\n * documents — provider file ids in the history, no storage story. That is a fine\n * trade for vision and it fails the moment a *tool* needs the file: the user\n * uploads an image and asks the agent to create a product, `POST /products`\n * wants multipart bytes, and the server is holding a provider file id and\n * nothing else. The model cannot make up the difference — it *saw* the image, it\n * cannot reproduce it — and the only recovery left is downloading from the\n * vendor, which is a round trip for bytes we held minutes ago, is not declared\n * anywhere (`AgentProvider` has `upload` and no counterpart), and is subject to\n * a retention policy that is not ours.\n *\n * WHY THE SCOPE IS NOT OPTIONAL. Once a tool takes an attachment id as an\n * argument, that id arrives *from the model*, and a model's arguments are as\n * untrusted as a request body: whatever the model wrote is a function of what it\n * read, and it reads the user's text, tool output, and any document it was\n * shown. A store that resolves ids globally therefore lets a prompt-injected\n * model name another tenant's upload and have the server fetch it — the model as\n * confused deputy, spending the server's own credentials.\n *\n * So there is no `find(id)` on this interface, only `find(scope, id)`, and the\n * thing a tool is handed is a `ScopedAttachments` whose methods take no scope at\n * all because it already closed over one. The unscoped read is not discouraged,\n * it is unspellable: the tool has no object to call it on. The scope itself is\n * derived from the server's own knowledge of the request — see\n * `AgentController.attachmentScope` — and never from anything the model can\n * write.\n */\n\n/**\n * The subject an attachment belongs to, as one opaque string.\n *\n * ONE STRING, COMPARED WITH `===`, RATHER THAN A STRUCTURE OF user/thread/org\n * fields. gemi does not have an opinion about tenancy — there is no `Org` in\n * this framework — so a structured scope would have had to be a bag of optional\n * fields, and a bag of optional fields has a worst member: `{}`, which under any\n * partial-match rule matches everything. Every such rule admits `{}` — or\n * `{ userId: undefined }`, which is the same thing arrived at by a typo — so one\n * forgetful line in an app turns the scope check into a no-op that still\n * typechecks. An opaque key has no such member: an app that wants org isolation\n * writes `org:${org.id}` and gets it, and an app that decides a request has no\n * subject returns `null` from `attachmentScope()` and gets no attachment ids at\n * all, rather than a bucket everyone shares.\n *\n * It is an object rather than a bare `string` for one reason, and it is a real\n * one: `find(scope, id)` taking two bare strings is a signature whose arguments\n * can be transposed, and the transposition typechecks, runs, and is a scope\n * check comparing an id against an id. The wrapper makes that a compile error.\n *\n * The key is never shown to the model and never sent to the client. It is a\n * server-side comparison value; it is not a path component and is not parsed.\n */\nexport type AttachmentScope = { readonly key: string };\n\n/**\n * Where an upload's bytes were sent. See `AgentController.attachmentDestination`.\n *\n * `\"provider\"` DESCRIBES A RECORD `upload` NEVER WRITES. A file that goes to the\n * provider alone leaves gemi holding nothing to resolve — `read` and `file`\n * would throw for it — so the route answers `fileId` and mints no attachment id\n * at all, and there is no id for anyone to look up its name with. The value is\n * on this type because a store *can* hold such a row (a custom `AttachmentStore`\n * filing provider uploads for its own bookkeeping is a reasonable thing to\n * write, and `ScopedAttachments` handles it correctly), not because the shipped\n * route produces one.\n */\nexport type AttachmentDestination = \"both\" | \"provider\" | \"storage\";\n\n/**\n * One recorded upload.\n *\n * `fileId` and `objectName` are both optional and at least one is always\n * present, because which of them exists is exactly the destination decision:\n * `provider` has a `fileId` and no bytes of ours, `storage` has bytes and no\n * `fileId`, `both` has both.\n */\nexport type Attachment = {\n /**\n * gemi's id for the upload — the one a tool takes as an argument, and the only\n * id in this module that may be shown to a model.\n *\n * Prefixed `gemi_att_` so that the mistake everyone makes once — a gemi\n * attachment id put into `FilePart.fileId`, which is a *provider* id — is\n * caught by a sentence to read rather than by whatever the vendor says about\n * a file it has never heard of, mid-conversation. `toResponsesInput` checks\n * for the prefix; the vendor's own answer to a bogus `file_id` has not been\n * measured, and the guard does not depend on it.\n */\n id: string;\n /**\n * The scope this was filed under, carried on the record so `ScopedAttachments`\n * can check it a second time. See `ScopedAttachments.get` for why the second\n * check is not redundant.\n */\n scopeKey: string;\n /** The provider file id, when the file was sent to the provider. */\n fileId?: string;\n /** The storage object name, when the bytes were kept. Feed it to `read`. */\n objectName?: string;\n /** The client's filename, kept so a tool can forward the file under it. */\n name: string;\n mimeType: string;\n size: number;\n createdAt: string;\n destination: AttachmentDestination;\n};\n\n/**\n * Where attachment *records* live. The bytes are in `AttachmentStorage`; this\n * holds the row that says which bytes, whose, and under what id.\n *\n * The default is `MemoryAttachmentStore`, which dies with the process, exactly\n * like `MemoryAgentStore`. An app that wants an attachment to outlive a deploy\n * implements this over a table — `id` primary key, `scope_key` column — and\n * assigns it to `AgentController.attachments`.\n *\n * IF YOU IMPLEMENT THIS, THE ONE THING YOU MUST NOT DO is answer `find` from the\n * id alone. `SELECT * FROM attachments WHERE id = ?` is the leak this module\n * exists to prevent, and it passes every test an app writes about its own\n * uploads, because those uploads are always in scope. It is wrong only for an id\n * that came from somewhere else, which is to say only when it matters. The\n * clause is `WHERE id = ? AND scope_key = ?`.\n */\nexport interface AttachmentStore {\n /** Records an attachment under `scope`. The caller has already minted the id. */\n put(scope: AttachmentScope, attachment: Attachment): Promise<void>;\n /**\n * The attachment with this id *within this scope*, or `null`.\n *\n * `null` for an id that does not exist and `null` for an id that exists under\n * another scope, and the caller cannot tell which — deliberately. See\n * `AttachmentNotFoundError`.\n */\n find(scope: AttachmentScope, id: string): Promise<Attachment | null>;\n}\n\n/**\n * The bytes half: whatever the app's file storage is.\n *\n * Structurally satisfied by the `Storage` facade, which is what\n * `AgentController` defaults to. Declared as an interface of two methods rather\n * than as `typeof Storage` so a test — or an app with a second bucket for user\n * uploads — can pass something else without standing up a container.\n */\nexport interface AttachmentStorage {\n put(params: PutFileParams | Blob): Promise<string>;\n read(params: ReadFileParams | string): Promise<ReadResult>;\n}\n\n/**\n * What both a missing id and someone else's id answer.\n *\n * THE MESSAGE MUST NOT SAY WHICH. \"Exists but is not yours\" and \"does not exist\"\n * are two answers to a question the caller is not entitled to ask, and a store\n * that distinguishes them is an oracle: a prompt-injected model that can tell\n * the two apart can walk an id space and report back which ids are live, which\n * is a tenant census delivered through the very tool the injection was aimed at.\n * One error, one wording, one code, for both.\n *\n * A plain `Error` rather than a `RequestBreakerError`: this is thrown inside a\n * tool, where the agent loop turns a throw into a tool result the model reads\n * and can recover from, not inside a route where it would decide a status code.\n */\nexport class AttachmentNotFoundError extends Error {\n readonly code = \"attachment_not_found\";\n constructor(public readonly id: string) {\n super(`No attachment ${id}.`);\n this.name = \"AttachmentNotFoundError\";\n }\n}\n\n/** Raised for a scope no caller could have meant. See `assertScope`. */\nexport class InvalidAttachmentScopeError extends Error {\n readonly code = \"invalid_attachment_scope\";\n constructor(message: string) {\n super(message);\n this.name = \"InvalidAttachmentScopeError\";\n }\n}\n\n/**\n * Rejects the empty scope.\n *\n * `{ key: \"\" }` is what an app produces by writing `` `org:${org?.id ?? \"\"}` ``\n * for a request that has no org, and it is the one value that must never reach a\n * store: it compares equal to itself, so every subject-less upload in the\n * process lands in one shared bucket that every subject-less caller can read out\n * of. That is the global store this module exists not to be, arrived at by a\n * template literal. Failing here is loud, happens on the first request, and\n * names the method to fix.\n */\nfunction assertScope(scope: AttachmentScope): AttachmentScope {\n if (!scope || typeof scope.key !== \"string\" || scope.key.length === 0) {\n throw new InvalidAttachmentScopeError(\n \"An attachment scope needs a non-empty key. Return `null` from `attachmentScope()` for a request that has no subject — an empty key is a bucket every caller shares.\",\n );\n }\n return scope;\n}\n\n/** The prefix on every gemi attachment id. See `Attachment.id`. */\nexport const ATTACHMENT_ID_PREFIX = \"gemi_att_\";\n\nexport function newAttachmentId(): string {\n return `${ATTACHMENT_ID_PREFIX}${crypto.randomUUID()}`;\n}\n\n/**\n * The handle a tool is given, and the only attachment API a tool should ever\n * see.\n *\n * IT TAKES NO SCOPE, ON ANY METHOD. That is the whole design, and it is not a\n * convenience: an API where the scope is a parameter is an API where the scope\n * is a parameter someone passes the wrong thing to — and the wrong thing here is\n * whatever the model wrote, because inside a tool body the model's arguments are\n * the variables closest to hand. Handing the tool an object that has already\n * closed over the server's answer leaves nothing to pass. `resolve(id, scope)`\n * was the rejected shape: it is the same code with the mistake still available.\n */\nexport class ScopedAttachments {\n constructor(\n private readonly store: AttachmentStore,\n private readonly storage: AttachmentStorage,\n private readonly scope: AttachmentScope,\n ) {\n assertScope(scope);\n }\n\n /**\n * The record for `id`, or `AttachmentNotFoundError`.\n *\n * THE SCOPE IS CHECKED TWICE HERE, and the second check is the one that\n * matters. `store.find` is an app's code — a table lookup somebody wrote, with\n * a `WHERE` clause somebody has to have remembered — and the failure mode of\n * forgetting the scope clause is a query that works perfectly for every upload\n * the app's own tests make. Comparing `record.scopeKey` again is three lines\n * that hold even when that clause is missing, so a careless store finds nothing\n * across scopes instead of leaking.\n *\n * It is not defence against a *malicious* store — an app's store is the app's\n * own code and could return anything. It is defence against the ordinary\n * version of this bug, which is the one that ships.\n */\n async get(id: string): Promise<Attachment> {\n const record = await this.store.find(this.scope, id);\n if (!record || record.scopeKey !== this.scope.key) {\n throw new AttachmentNotFoundError(id);\n }\n return record;\n }\n\n /**\n * The bytes, streaming, with the metadata `createStreamResponse` wants.\n *\n * Throws `AttachmentNotFoundError` for an attachment that exists but whose\n * bytes were never kept — a `provider`-destination upload. The same error as\n * an unknown id, for the reason on that class: \"it is at the vendor and not\n * here\" is information about someone else's upload as readily as about your\n * own.\n */\n async read(id: string): Promise<ReadResult> {\n return await this.readRecord(id, await this.get(id));\n }\n\n /**\n * The bytes for a record already resolved, so `file()` does one lookup rather\n * than two. `store.find` is a `SELECT` in any real store, and the second one\n * was not a second check of anything — same scope, same id, same row.\n *\n * It still takes the id the *caller* asked for, rather than reading\n * `record.id`, so the error stays a function of the caller's own input: an app\n * store that answers with the wrong row must not get to choose which id\n * appears in a message the model may read.\n */\n private async readRecord(id: string, record: Attachment): Promise<ReadResult> {\n if (!record.objectName) {\n throw new AttachmentNotFoundError(id);\n }\n return await this.storage.read({ name: record.objectName });\n }\n\n /**\n * The bytes as a `File`, under the name and type they were uploaded with —\n * which is the shape the motivating case actually needs, because\n * `form.append(\"image\", file)` is what a tool forwarding an upload to a\n * multipart endpoint writes.\n *\n * Buffers, and says so. `read()` is there for anything large enough that\n * buffering it is the wrong call.\n *\n * THIS IS ALSO HOW AN ATTACHMENT OUTLIVES THE RUN, and it is `file()` rather\n * than `read()` that composes for it:\n *\n * const file = await ctx.attachments.file(id);\n * await Storage.put({ name: `pages/${pageId}/hero.png`, body: file });\n *\n * `read()` answers a `ReadResult` whose `body` is `ReadableStream | Blob |\n * null`, and `PutFileParams.body` is `Blob | File | Buffer` — a stream body\n * has nowhere to go without being drained first.\n *\n * Two things that bite on the way out, both silent:\n *\n * - **Do not keep a `gemi_att_` id as an app's durable handle to a file.**\n * The default record store is `MemoryAttachmentStore`, so the id→object\n * mapping dies with the process while the bytes stay in storage. Copy the\n * file under a key of the app's own and keep that.\n * - **Put an extension on that key.** `FileSystemDriver` — the default —\n * ignores `contentType` on write and re-derives the type from the stored\n * path on read, so an extensionless object serves as\n * `application/octet-stream` and an `<img>` pointed at it renders nothing,\n * with no error anywhere. `attachmentObjectName` carries one over for this\n * module's own writes; an app's key is the app's to name.\n */\n async file(id: string): Promise<File> {\n const record = await this.get(id);\n const result = await this.readRecord(id, record);\n const body = result.body;\n const blob =\n body instanceof Blob ? body : body ? await new Response(body).blob() : new Blob([]);\n return new File([blob], record.name, { type: record.mimeType });\n }\n\n /**\n * Stores bytes a tool produced, under this caller's scope, and answers the\n * record.\n *\n * Here rather than on the store because the id, the object name and the scope\n * all have to be decided together, and because an attachment a tool created\n * has to land in the scope the tool is reading from — otherwise a tool writes\n * a file the next tool of the same run cannot see.\n *\n * Nothing is sent to the provider: bytes a tool made are bytes for the app,\n * and a tool that wants the model to look at its output says so by returning\n * something the model can read. Issue #490 builds `ctx.attachments.put(blob)`\n * on exactly this.\n */\n async put(\n blob: Blob,\n params: { name?: string; mimeType?: string; fileId?: string } = {},\n ): Promise<Attachment> {\n const id = newAttachmentId();\n const mimeType = params.mimeType ?? blob.type ?? \"\";\n const name = params.name ?? (blob instanceof File ? blob.name : id);\n const objectName = await this.storage.put({\n name: attachmentObjectName(id, name),\n body: blob,\n contentType: mimeType || undefined,\n });\n const record: Attachment = {\n id,\n scopeKey: this.scope.key,\n objectName,\n name,\n mimeType: mimeType || \"application/octet-stream\",\n size: blob.size,\n createdAt: new Date().toISOString(),\n // `both` when the caller has already sent the same bytes to the provider,\n // which is what `ctx.attachments.put(blob, { showModel: true })` does\n // before it gets here.\n //\n // The parameter exists rather than a second `markShown` call because the\n // alternative is a row that is briefly, and then permanently if the second\n // write fails, a lie: `destination` is the field that says where the bytes\n // went, and a file sitting in the vendor's file list under a record that\n // says `storage` is exactly the discrepancy the field was added to close.\n // Nothing here uploads — the provider is the run's, not this module's —\n // so the id is passed in rather than produced.\n destination: params.fileId ? \"both\" : \"storage\",\n ...(params.fileId ? { fileId: params.fileId } : {}),\n };\n await this.store.put(this.scope, record);\n return record;\n }\n}\n\n/**\n * What a tool asks for when it parks bytes.\n *\n * `showModel` is the whole of issue #490 in one flag: without it a tool's\n * output is an id in a string, which the model can quote back and cannot look\n * at, so \"edit this image\" produces a file nobody but the app ever sees. With\n * it the same bytes also go to the provider and come back into the transcript\n * as an input-role message the next step is built on — generate, look, fix.\n *\n * It is a flag rather than the default because the default costs money on every\n * call. The reasoning is `capabilitiesForModel`'s, run the other way round: a\n * file the model did not need and was shown anyway is an upload plus a set of\n * image tokens on every subsequent request of the run, on an invoice, for a\n * tool whose author never asked to be looked at. A file the model needed and\n * was not shown is a tool that returned an id, which is what tools did before\n * this existed and is visible in the transcript the moment anyone reads it. The\n * expensive mistake is the silent one here, so the expensive thing is opt in.\n */\nexport type PutAttachmentParams = {\n /** The filename to keep, so a later `file()` hands the bytes back under it. */\n name?: string;\n /** Overrides `blob.type`, which a `Blob` built from raw bytes does not have. */\n mimeType?: string;\n /**\n * Also send these bytes to the model provider and put them in front of the\n * model as an input-role message, once this tool call settles.\n *\n * Refused, loudly, by a provider whose `capabilities.fileInput` is false: the\n * request builder drops a file part such a provider cannot read, and an\n * upload paid for, stored, and then dropped on the way to the wire is the\n * silent-forever failure — the tool reports success, the model answers about\n * an image it was never shown, and nothing anywhere says why.\n */\n showModel?: boolean;\n};\n\n/**\n * The attachment API a tool is given, as `ctx.attachments`.\n *\n * Everything a `ScopedAttachments` does, plus `showModel`, plus the memo that\n * makes a re-entered tool call idempotent. It is a separate interface from\n * `ScopedAttachments` because those two additions are not properties of the\n * scope, they are properties of *one tool call*: the run has to know which call\n * a `put` belongs to in order to replay it, and the object handed to a tool is\n * therefore built per call — the same shape `ctx.runAgent` has, for the same\n * reason.\n *\n * The read half (`get`, `read`, `file`) is delegated to the `ScopedAttachments`\n * unchanged, including its two scope checks. A tool reading a file it did not\n * create is reading an id the model wrote, and #489's whole argument applies to\n * it word for word.\n */\nexport interface ToolAttachments {\n /**\n * Parks bytes under this caller's scope and answers the record.\n *\n * ON A REPLAY THIS STORES NOTHING. An escalating tool is re-entered from the\n * top on the next turn, so the body that built this blob has run before and\n * built one already; the record from that first attempt is what comes back,\n * and the bytes handed in now are dropped. That is the same bargain\n * `ctx.runAgent` makes and it is not avoidable: the id from the first attempt\n * is already in the transcript the model read, so minting a second one would\n * leave the model holding an id for bytes nobody kept — and uploading the\n * second copy would pay the vendor twice and put the same image into the\n * context twice. Work *before* a `put` still runs again; if producing the\n * bytes is what costs, branch on `ctx.resumed`.\n */\n put(blob: Blob, params?: PutAttachmentParams): Promise<Attachment>;\n /** The record for `id`, or `AttachmentNotFoundError`. Scoped. */\n get(id: string): Promise<Attachment>;\n /** The bytes, streaming. Scoped. */\n read(id: string): Promise<ReadResult>;\n /** The bytes as a `File`, under their original name and type. Scoped. */\n file(id: string): Promise<File>;\n}\n\n/**\n * One successful `ctx.attachments.put` of one tool call, written down so the\n * next turn can replay it instead of doing it again.\n *\n * Lives on `ToolCallPart.attachments`, indexed by the order the puts happened\n * in — exactly where and how `ToolCallPart.nested` records sub-runs, and for\n * exactly the same reason: the message history is the only state that survives\n * a turn boundary, in a thread and in the browser both, so a memo that is not\n * on the message is a memo a stateless app does not have.\n *\n * `shown` is absent for a plain `put`. When present it carries the provider\n * file id AND the identity of the message injected for it, because both have to\n * come back byte for byte: a replay that minted a fresh message id would put a\n * second copy of the same image in the transcript, and a client that had\n * already applied the first would show it twice.\n */\nexport type ToolAttachmentPut = {\n /** What `put` answered, replayed verbatim. */\n attachment: Attachment;\n /**\n * Set when this slot was filled by `ctx.generateImage` / `ctx.editImage`\n * rather than a plain `put`, carrying what the render reported and the\n * attachment record cannot.\n *\n * WHY THE RENDER IS MEMOIZED AT ALL, and why it is memoized *here*. An\n * escalating tool is re-entered from the top on the next turn, so a render\n * that is not written down is paid for again — and unlike a `put`, the thing\n * being repeated costs money at the vendor and about two minutes of wall\n * clock. The memo cannot hold the image: this record lives in the message\n * history, which goes over the wire and into a store on every turn, and a\n * megabyte of base64 per generated image would go with it. So what is\n * recorded is the attachment id, and the bytes live in storage where they\n * were going anyway.\n *\n * That is the whole reason `ctx.generateImage` answers an `Attachment`\n * instead of a `Blob` while `ImageModel.generate` answers the bytes: outside\n * a run there is nothing to replay and nowhere to park, and inside one there\n * has to be both.\n *\n * `size` is the pixel dimensions the model actually produced — read off the\n * response, which does not always echo the request — and is not derivable\n * from `Attachment.size`, which is a byte count.\n */\n generated?: {\n size: string;\n usage: Usage;\n };\n /** Set when `showModel` was asked for. See above. */\n shown?: {\n /** The provider's file id — what the injected `FilePart` carries. */\n fileId: string;\n /** The injected message's id, so a replay reproduces it rather than a twin. */\n messageId: string;\n /** Its `createdAt`, for the same reason. */\n createdAt: string;\n };\n};\n\n/**\n * A slot in that list, which is one `put` — or one `put` that threw.\n *\n * The failure arm exists because the index is what identifies a `put`, and an\n * index is taken the moment the call is made rather than when it succeeds: a\n * body that runs its puts concurrently must number them by the order it *asked*\n * and not by the order the network answered, or the next turn will not\n * reproduce the numbering. So a `put` that throws and is caught — a provider\n * that cannot read files, a vendor that refuses the type, a storage write that\n * fails — leaves a slot behind, and a later successful `put` in the same call\n * would otherwise leave a hole in front of it. A hole is `null` once it has\n * been through JSON, which is what a consumer walking `part.attachments`\n * crashes on; `{ failed: true }` is the same fact said out loud.\n *\n * It is written lazily, only when a later `put` needs the index above it, so a\n * tool whose puts all threw still adds no `attachments` field at all. And it is\n * not a memo: a replayed slot marked `failed` is re-attempted from scratch,\n * because there is nothing recorded to hand back and the failure may have been\n * the network's rather than the tool's.\n */\nexport type ToolAttachmentRecord = ToolAttachmentPut | { failed: true };\n\n/**\n * The object name an attachment's bytes are stored under.\n *\n * The scope is deliberately NOT in the path. A scope key is app-authored text —\n * `org:${slug}`, with whatever a slug turns out to be — and putting arbitrary\n * app text into an object name is how a `..` or a `/` ends up somewhere nobody\n * meant on the one driver that resolves names literally (`FileSystemDriver`\n * concatenates them onto a folder path). The record is what carries the scope;\n * the object name only has to be unique, and a uuid already is.\n *\n * The extension is carried over from the client's filename when it looks like\n * one, because `FileSystemDriver` reads a stored object's content type back off\n * its path and would otherwise answer `application/octet-stream` for everything.\n * `Attachment.mimeType` is the authority either way — this is for anything that\n * reads the bucket without the record.\n */\nexport function attachmentObjectName(id: string, name: string): string {\n const match = /\\.([A-Za-z0-9]{1,8})$/.exec(name);\n return `attachments/${id}${match ? `.${match[1]!.toLowerCase()}` : \"\"}`;\n}\n\n/**\n * The default: attachment records last as long as the process.\n *\n * Modelled as one flat map keyed by id, holding the scope beside the record and\n * comparing it in `find` — rather than as a map of maps, which would make the\n * scoping structural and unforgettable. That looks like the safer shape and is\n * the wrong reference implementation: an app's store is a table with an `id`\n * primary key and a `scope_key` column, and its `find` is a `WHERE` clause that\n * can be written wrong. The store gemi ships should have the same failure\n * available to it as the store an app writes, so that a test written against\n * this one is a test of the check every real store also has to make.\n */\nexport class MemoryAttachmentStore implements AttachmentStore {\n private readonly rows = new Map<string, Attachment>();\n\n async put(scope: AttachmentScope, attachment: Attachment): Promise<void> {\n assertScope(scope);\n this.rows.set(attachment.id, { ...attachment, scopeKey: scope.key });\n }\n\n async find(scope: AttachmentScope, id: string): Promise<Attachment | null> {\n assertScope(scope);\n const row = this.rows.get(id);\n // The scope clause. An id belonging to another scope answers `null`, which\n // is the answer an id that does not exist gets — see\n // `AttachmentNotFoundError` for why the two must be indistinguishable.\n if (!row || row.scopeKey !== scope.key) {\n return null;\n }\n return row;\n }\n\n /** Test and dev affordance: how many rows are held, across every scope. */\n get size(): number {\n return this.rows.size;\n }\n}\n\n/**\n * The process-wide default, so an app that configures nothing still gets\n * attachment ids that work for the length of a conversation.\n *\n * Module-level for the reason `defaultAgentStore` is: the controller is\n * constructed per request, so a store assigned as `new MemoryAttachmentStore()`\n * in a field initializer is an empty store on every request, and every\n * attachment id is a miss one turn after it was minted.\n */\nexport const defaultAttachmentStore = new MemoryAttachmentStore();\n",
7
7
  "import type { AgentError, AgentStreamFrame } from \"../types\";\n\nconst encoder = new TextEncoder();\n\n/**\n * One frame, one SSE event.\n *\n * `id:` carries the frame's `seq`, which is what makes the browser's own\n * `Last-Event-ID` the right cursor on reconnect — the transport asks the\n * question the run already knows how to answer, and the client never has to\n * track a position of its own.\n */\nexport function encodeFrame(frame: AgentStreamFrame): string {\n return `id: ${frame.seq}\\ndata: ${JSON.stringify(frame.event)}\\n\\n`;\n}\n\n/**\n * How long a connection may sit idle before a comment line goes out.\n *\n * The nearest ceiling is not a proxy but our own server: Bun's `idleTimeout`\n * counts socket silence, a streaming body included, and gemi runs at its\n * 10-second default unless `SERVER_IDLE_TIMEOUT` says otherwise. A comment\n * line every 25 seconds was measured to lose the connection at 12; every 5\n * keeps it open, with room for a write that lands late. Azure App Service's\n * front end, at about 230 seconds, is the far ceiling, and 5 clears it by the\n * same margin. A thousand quiet streams cost two hundred thirteen-byte writes\n * a second, which is nothing. Both encoders read this one value, so the two\n * cannot drift apart.\n */\nexport const SSE_KEEPALIVE_INTERVAL_MS = 5_000;\n\n/**\n * The comment line itself. A line starting with `:` is a comment under the SSE\n * spec: every parser on our side skips it, and so does the browser's own\n * `EventSource`.\n */\nexport const SSE_KEEPALIVE = \": keepalive\\n\\n\";\n\n/**\n * Writes a keepalive whenever the stream has been silent for the interval.\n *\n * Armed on creation because the silence before the first frame is real\n * silence too — a model thinking is the common case. `touch()` after every\n * frame is what makes it measure silence rather than elapsed time; `stop()`\n * on close or cancel is what keeps a finished stream from holding a timer.\n *\n * The write is guarded because a cancel can land between the timer firing and\n * the enqueue, and a closed controller throws. There is nothing to do about\n * that except stop. `arm` checks `stopped` too, so a `touch()` that arrives\n * after `stop()` cannot hand a finished stream a timer for one more interval.\n */\nexport function sseKeepalive(\n controller: ReadableStreamDefaultController<Uint8Array>,\n intervalMs = SSE_KEEPALIVE_INTERVAL_MS,\n): { touch(): void; stop(): void } {\n let timer: ReturnType<typeof setTimeout> | null = null;\n let stopped = false;\n\n const arm = () => {\n if (stopped) return;\n if (timer !== null) clearTimeout(timer);\n timer = setTimeout(() => {\n timer = null;\n if (stopped) return;\n try {\n controller.enqueue(encoder.encode(SSE_KEEPALIVE));\n } catch {\n stop();\n return;\n }\n arm();\n }, intervalMs);\n };\n\n const stop = () => {\n stopped = true;\n if (timer !== null) clearTimeout(timer);\n timer = null;\n };\n\n arm();\n return { touch: arm, stop };\n}\n\nexport function sseHeaders(): Record<string, string> {\n return {\n \"Content-Type\": \"text/event-stream\",\n // `no-transform` as well as `no-store`: a proxy that gzips or rechunks the\n // body is a proxy that buffers it, and a buffered token stream arrives all\n // at once, which is the same as not streaming at all.\n \"Cache-Control\": \"no-store, no-transform\",\n Connection: \"keep-alive\",\n // nginx's own name for the same thing.\n \"X-Accel-Buffering\": \"no\",\n };\n}\n\n/**\n * Encodes an async iterable of frames as an SSE response.\n *\n * Pulls one frame per `pull` rather than looping inside `start`: a `start` that\n * awaits the whole run does not resolve until the run is over, and the stream\n * is not readable until it does — which would turn every streamed answer into a\n * single delivery at the end.\n */\nexport function sseResponse(frames: AsyncIterable<AgentStreamFrame>, status = 200): Response {\n const iterator = frames[Symbol.asyncIterator]();\n let lastSeq = -1;\n let keepalive!: ReturnType<typeof sseKeepalive>;\n\n const body = new ReadableStream<Uint8Array>({\n start(controller) {\n keepalive = sseKeepalive(controller);\n },\n async pull(controller) {\n try {\n const next = await iterator.next();\n if (next.done) {\n keepalive.stop();\n controller.close();\n return;\n }\n lastSeq = next.value.seq;\n controller.enqueue(encoder.encode(encodeFrame(next.value)));\n keepalive.touch();\n } catch (err) {\n // Past the headers there is no status left to fail with, so the reason\n // goes out as the last event. Closing silently would be\n // indistinguishable from a run that finished, which is the one thing a\n // reattaching client must not be told by mistake.\n const event = { type: \"error\", error: toAgentError(err) } as const;\n keepalive.stop();\n controller.enqueue(encoder.encode(encodeFrame({ seq: lastSeq + 1, event })));\n controller.close();\n }\n },\n cancel(reason) {\n // The client went away. Let the generator unwind so its `finally` runs\n // and it stops waiting on frames nobody will read.\n keepalive.stop();\n void iterator.return?.(reason);\n },\n });\n\n return new Response(body, { status, headers: sseHeaders() });\n}\n\n// `unknown` rather than a new code: `AgentErrorCode` is the wire contract and\n// belongs to the model's failures, not to the transport's. The message names\n// the cursor, which is what a client can actually act on.\nfunction toAgentError(err: unknown): AgentError {\n return {\n code: \"unknown\",\n message: err instanceof Error ? err.message : String(err),\n retryable: false,\n };\n}\n",
8
- "import type { HttpRequest } from \"../http\";\nimport type { AgentProvider, ProviderToolNamespace, ProviderToolSpec } from \"./AgentProvider\";\nimport { supportsStrict } from \"./Schema\";\nimport type { Infer, Schema } from \"./Schema\";\nimport {\n consumeNestedRun,\n consumePendingCall,\n readSignature,\n signNestedRun,\n signPendingCall,\n verifyNestedRun,\n verifyPendingCall,\n} from \"./signing\";\nimport type {\n Attachment,\n PutAttachmentParams,\n ScopedAttachments,\n ToolAttachmentPut,\n ToolAttachmentRecord,\n ToolAttachments,\n} from \"./store/Attachments\";\nimport { ATTACHMENT_ID_PREFIX, InvalidAttachmentScopeError } from \"./store/Attachments\";\nimport { sseKeepalive } from \"./store/sse\";\nimport type {\n AgentError,\n AgentMessage,\n AgentStreamEvent,\n AgentStreamFrame,\n ClientToolResult,\n ClientTurn,\n FilePart,\n FinishReason,\n NestedRun,\n PendingToolCall,\n ToolCallPart,\n ToolResultPart,\n ToolShapes,\n Usage,\n} from \"./types\";\n\n// --- tools ---------------------------------------------------------------\n\n/**\n * Everything a tool needs from the request it is running inside.\n *\n * Tools are created once at module scope and shared by every request, so they\n * cannot close over a user or an abort signal — and anything mutable stored on\n * the tool itself would leak across requests. That is why the run's state\n * arrives as an argument instead: the tool stays a singleton and the context is\n * per call.\n */\nexport interface ToolContext {\n req: HttpRequest<any, any>;\n /**\n * The app's own fields from the turn's request body — what `useChat`'s\n * `body` option sent, minus the keys the turn envelope owns.\n *\n * Here rather than behind `ctx.req.input()`, and not because that would be\n * inconvenient: `AgentController` has already consumed the body, and a run\n * outlives the request anyway, so by the time a tool executes there is\n * nothing left to read. The values are copied onto the run when it starts.\n *\n * `{}` for a run started with none. Untyped on purpose — a tool is a\n * module-scope singleton that any controller may mount, so there is no one\n * `Body` for it to be. The controller that declares one gets it typed in\n * `instructions()`; a tool validates, the way it would any other input it\n * did not define.\n *\n * CLIENT-CONTROLLED. Same trust as a request body: fine to read, not a\n * finding about who the user is. An id from here says which record the\n * client wants, never that it may have it.\n */\n body: Record<string, unknown>;\n runId: string;\n threadId?: string;\n toolCallId: string;\n /**\n * Aborted when the user calls `stop()`. Not when the connection drops — a run\n * outlives the request that started it so a refresh can reattach, which means\n * a disconnect is no longer a signal to stop working.\n */\n signal: AbortSignal;\n /** Which step of the tool loop this is, starting at 1. */\n step: number;\n /**\n * How deep this tool is inside nested runs: 0 at the top, 1 inside a tool of\n * an agent started by `runAgent`, and so on. Compared against `maxDepth` on\n * `Agent.create` so a cycle — agent A with a tool that runs agent A — fails\n * with a sentence to read instead of exhausting the stack.\n */\n readonly depth: number;\n /**\n * True when this tool is being re-entered after a sub-agent it started asked\n * the user something and the user answered.\n *\n * READ THE `runAgent` NOTE BEFORE USING IT. This is the flag that lets a tool\n * tell a first attempt from a replay, and it exists because there is nothing\n * to tell it otherwise: the tool body ran once already.\n */\n readonly resumed: boolean;\n /**\n * Files, both directions.\n *\n * WHAT THIS FIXES. Before it, files travelled one way: a user's upload became\n * a provider file id the model could look at, and a tool that *produced*\n * something — an edited image, a rendered chart — had a string to return and\n * nowhere to put the bytes. `put(blob)` parks them and answers a record whose\n * `id` is the handle everything else uses; `put(blob, { showModel: true })`\n * also sends them to the provider and puts them in front of the model as an\n * input-role message once this tool call settles, which is what makes\n * generate → look → fix a loop rather than a one-way report.\n *\n * SCOPED, AND THAT IS THE WHOLE SECURITY STORY. The object is built from the\n * `ScopedAttachments` the controller resolved for *this request* (see\n * `AgentController.attachmentScope`), and none of its methods takes a scope,\n * so there is nothing for a tool to pass the model's arguments into. An id\n * that came from another user resolves to `AttachmentNotFoundError`, with the\n * same wording an unknown id gets.\n *\n * PER TOOL CALL, LIKE `runAgent`, AND FOR THE SAME REASON. `put` is memoized\n * by call index within the tool call, so a tool that escalates and is\n * re-entered does not store, upload or inject a second time. Read the note on\n * `ToolAttachments.put` before writing a body whose `put` calls sit in a\n * branch.\n *\n * ALWAYS PRESENT, NEVER NULL. A request with no attachment scope — an\n * unauthenticated, thread-less chat — gets an object whose every method\n * throws with a sentence naming `attachmentScope()`. A nullable `ctx`\n * member would be a guard every tool has to remember and most would not,\n * and the failure of forgetting is a `TypeError` in a tool body rather than\n * an explanation.\n */\n attachments: ToolAttachments;\n /**\n * What the user sent with the turn this tool call answers. See `ToolTurn`.\n */\n readonly turn: ToolTurn;\n /**\n * Runs another agent from inside this tool, wired into the parent run.\n *\n * A tool can already drive a sub-agent by hand — make one, iterate it, yield\n * its events as progress. What this does that hand-rolling cannot is join the\n * two runs: the sub-run inherits `ctx.signal` so the parent's `stop()` reaches\n * it; every sub-run event is re-emitted on the parent stream as\n * `nested-event`, numbered in the parent's `seq`, so `/attach` replay stays\n * correct through the nesting; the sub-run's usage rolls into the parent's;\n * its transcript is recorded on the parent's `ToolCallPart.nested`; and the\n * depth and agent-name chain travel with it, so a cycle fails fast.\n *\n * ESCALATION. If the sub-run ends `awaiting-input` — it has an approval tool,\n * or it asked a question — `onPending: \"escalate\"` (the default) throws a\n * `PendingEscalation` carrying the inner pending calls, which the parent run\n * collects exactly like pending calls of its own: the parent ends\n * `awaiting-input` with the sub-agent's questions in its list, and the client\n * answers them with the same `approve()` / `answer()` it uses for any other.\n * `onPending: \"deny\"` refuses them instead and lets the sub-run finish.\n *\n * THE COST, WHICH IS REAL AND WHICH YOU MUST DESIGN AROUND. A JS async\n * generator cannot be suspended across a turn boundary: `awaiting-input` is\n * terminal for the stream, the next turn re-enters the loop at the top and\n * rebuilds its state from the message history, and a paused generator is not\n * in that history and cannot be put there. So an escalating tool is\n * RE-ENTERED FROM THE TOP on the next turn, with `ctx.resumed === true`, and\n * `runAgent` is memoized by call index within the tool call: the Nth\n * `runAgent` of a tool call that already completed on an earlier turn returns\n * its persisted result immediately, calling no provider and running no\n * sub-tool, and only the sub-run that escalated actually continues.\n *\n * The index is the only key there is, so a body whose `runAgent` calls sit in\n * a branch or a loop can produce a different sequence on the replay and make\n * index N mean two different things. That is checked, not trusted: a mismatch\n * fails the tool call with a sentence naming both sub-runs, because pairing a\n * user's answer with a sub-run they never saw would be invisible.\n *\n * Which means: CODE BEFORE AN ESCALATING `runAgent` RUNS AGAIN ON RESUME.\n * Side effects there are repeated. Put your side effects after the\n * `runAgent`, or make them idempotent, or branch on `ctx.resumed`. This is\n * inherent to replay and it is the same bargain the outer tool loop already\n * makes; it is written here in plain words rather than solved with a\n * checkpoint API, because that is a much larger feature than this one.\n */\n runAgent<A extends AnyAgent>(agent: A, params?: RunAgentParams): Promise<NestedRunResult>;\n}\n\n/**\n * The files of the turn a tool call belongs to, as data the model cannot write.\n *\n * WHAT THIS FIXES. Every method on `ctx.attachments` takes an id, and before\n * this the only ids a tool could get were the ones the model put in its\n * arguments. So a tool meant to use \"the image the user attached\" had to trust\n * the model to name it, and a prompt-injected document could name a different\n * upload of the same user's — yesterday's contract instead of today's photo —\n * which the scope check passes, because it is the user's file. Reading the ids\n * from here instead leaves the model nothing to steer.\n *\n * WHICH TURN: THE USER MESSAGE BEFORE THIS CALL, NOT THE LATEST ONE. Found by\n * walking back from the assistant message that made the call, never by taking\n * the last user message in the history when the tool runs, and the difference\n * is re-entry. An escalating tool runs again from the top on a later turn (see\n * `runAgent`), and \"latest\" is recomputed each time: a turn that answers the\n * sub-agent can carry text or files of its own, and when the re-entered tool\n * asks again, that turn's user message is appended *after* the still-open call\n * — so on the next re-entry \"latest\" is the answer turn, and a tool that read\n * \"the image\" would pick a different file, or none, on its second attempt. The\n * replay checks do not reliably catch that: `putMismatch` compares the media\n * type and showModel, never the bytes, and a sub-run seeded with the file as\n * a message part fingerprints as `<file>` whichever file it was. The message\n * that preceded the call is in the history on every attempt and does not\n * move, so every attempt sees the same list.\n *\n * ONE TURN, NOT THE THREAD. The whole thread is what \"the image I sent earlier\"\n * needs, and it is the wider thing to bind to: a tool that picks \"the first\n * image\" out of forty turns picks whichever one the history happens to put\n * first, and the user who uploaded a new one sees the old one used. A tool that\n * wants earlier turns can take an id from the model and let the scope check it;\n * one that binds should bind to the turn the user is looking at. A turn with\n * text and no files gives an empty list, which is also the answer to \"the user\n * attached nothing this time\".\n *\n * NOT THE FILES A TOOL MADE. A file a tool showed with `put(…, { showModel:\n * true })` is injected as a user-role message and carries an `attachmentId`\n * too, and taking it would make it \"the user's upload\" to the next tool — so\n * the walk steps over every message a tool call's record names as injected,\n * the same test `historyForProvider` uses. A tool that wants what an earlier\n * tool produced has that tool's result to read the id from.\n *\n * IDS ONLY, AND NOT TRUSTED. The name and type beside them on the `FilePart`\n * are what the client said, so they are left off rather than offered as a\n * second, weaker copy of what `ctx.attachments.get(id)` answers from the\n * store. And the ids themselves are only as trustworthy as the history they\n * came from — which, for a stateless client, is whatever it posted. That is\n * fine for the reason everything else about attachments is fine: resolution\n * goes through `ctx.attachments`, whose scope was fixed by the request, so an\n * id from somebody else's upload answers `AttachmentNotFoundError` here\n * exactly as it would from the model's arguments. This list narrows which of\n * the caller's own files a tool reaches for; it grants nothing.\n *\n * Inside a sub-run the turn is the sub-run's own: the message its parent's\n * tool started it with, which has files only if that tool put them there. The\n * parent's upload is not inherited — the tool that called `runAgent` read its\n * own `ctx.turn` and decided what the sub-agent is given.\n */\nexport interface ToolTurn {\n /**\n * The `attachmentId`s on that user message's file parts, in order, without\n * duplicates, and only ids of gemi's own shape. Frozen, and computed once\n * before the body runs.\n */\n readonly attachments: readonly string[];\n}\n\n/** What `ctx.runAgent` is given. `messages` and `prompt` are alternatives. */\nexport interface RunAgentParams {\n /** Prior turns for the sub-agent. Starts empty when omitted. */\n messages?: AgentMessage[];\n /** Sugar for a single user turn — the common case, and the whole message\n * list when there is no sub-conversation to continue. */\n prompt?: string;\n /** Appended to the sub-agent's own `instructions`, for this run only. */\n instructions?: string;\n /** Shown on the nested transcript, e.g. \"researching pricing\". */\n label?: string;\n /**\n * What to do when the sub-run ends `awaiting-input`. `\"escalate\"` (the\n * default) throws `PendingEscalation` so the question reaches the user;\n * `\"deny\"` refuses every pending call and lets the sub-run finish, which is\n * what a tool wants when the sub-agent is meant to be autonomous.\n *\n * `\"deny\"` is refused *in place*, inside the sub-run's own loop, so the\n * sub-agent is told it cannot ask and takes another step rather than ending\n * parked — and it is inherited by everything below, so a grandchild asking to\n * escalate is overruled too. A promise that nothing from this subtree reaches\n * the user is only worth making if the whole subtree keeps it.\n */\n onPending?: \"escalate\" | \"deny\";\n /**\n * Override the sub-agent's own ceiling for this run.\n *\n * Not inherited from the parent. A sub-agent is a different job with a\n * different output size — a generator writing a document against a router\n * answering one word — and silently handing down the caller's ceiling would\n * either strangle the one or fail to bound the other.\n */\n maxOutputTokens?: number;\n temperature?: number;\n}\n\n/**\n * What a completed sub-run gives back.\n *\n * `nested` is the transcript as it is recorded on the parent's tool-call part,\n * so a tool that wants to summarize what its sub-agent did reads the same\n * object the UI renders rather than a second representation of it.\n */\nexport interface NestedRunResult<O = unknown> {\n runId: string;\n /** The sub-agent's name — carried so a caller that fans out over several\n * agents can tell the results apart without tracking the order. */\n agent: string;\n messages: AgentMessage[];\n finishReason: FinishReason;\n usage: Usage;\n /** Set when the sub-agent declares an `output` schema and the run finished. */\n output?: O;\n /** The record written to the parent's `ToolCallPart.nested`. */\n nested: NestedRun;\n}\n\n/**\n * Thrown by `ctx.runAgent` when a sub-run ends `awaiting-input`.\n *\n * An exception rather than a return value because it must not be mistaken for\n * an answer: a tool that ignored an `{ escalated: true }` field would return a\n * result to the model as if the sub-agent had finished, and the model would act\n * on an answer nobody gave. `executeTool` lets this one propagate instead of\n * turning it into a `tool_error`, and the step loop collects `pending` exactly\n * like the pending calls it produced itself.\n *\n * `path` is the chain of tool-call ids down to the escalating call; each entry\n * of `pending` already carries its own full path, and this is the prefix they\n * share.\n */\nexport class PendingEscalation extends Error {\n readonly pending: PendingToolCall[];\n readonly path: string[];\n /** The sub-run that parked, so the parent can record its transcript before\n * ending the turn — an escalation is a pause, not a lost run. */\n readonly nested: NestedRun;\n\n constructor(params: { pending: PendingToolCall[]; path: string[]; nested: NestedRun }) {\n super(`A nested agent run is waiting on the user for ${params.pending.length} tool call(s).`);\n this.name = \"PendingEscalation\";\n this.pending = params.pending;\n this.path = params.path;\n this.nested = params.nested;\n }\n}\n\n/**\n * A tool either resolves once, or yields progress and then returns.\n *\n * The generator form exists because a tool that takes twenty seconds is the\n * normal case, not the exotic one, and a chat UI that shows nothing for twenty\n * seconds looks broken. Yields become `tool-progress` events; the return value\n * is the result the model sees.\n */\nexport type ToolExecute<Input, Output, Progress = unknown> = (\n input: Input,\n ctx: ToolContext,\n) => Promise<Output> | AsyncGenerator<Progress, Output, void>;\n\ntype ToolDefinitionBase<Name extends string, Input, Output> = {\n name: Name;\n /** The model's only description of when to reach for this. */\n description: string;\n inputSchema: Schema<Input>;\n /**\n * Optional for a server tool, required for a client one — there it is what\n * the answer is validated against before the model sees it, and what types\n * the value the browser has to produce.\n */\n outputSchema?: Schema<Output>;\n /**\n * Withholds this tool's parameter schema from the request: the model is shown\n * only the name and description, and pulls the rest in with the provider's\n * `tool_search` when it decides it wants the tool (`defer_loading` on the\n * wire).\n *\n * It says nothing about who runs the tool or when — it is a statement about\n * the prompt, not about execution. What it buys is context: an agent with\n * forty tools spends most of its prompt on schemas for tools it will not\n * call, and deferred ones load at the end of the window, so adding one\n * mid-conversation does not invalidate the cache.\n *\n * Purely an optimization, and gemi treats it as one: a provider that cannot\n * do tool search is sent the schemas inline, and the agent behaves the same.\n * So it is safe to set on a model that does not support it, and worth setting\n * only for tools that are large, numerous, or rarely reached.\n */\n deferred?: boolean;\n};\n\n/**\n * Two ways a tool's result comes to exist, and neither changes the shape of the\n * conversation.\n *\n * `execute` — the server runs it.\n * `answeredBy: \"client\"` — the browser produces the result: a question for the\n * user, or something only the page can do. The stream ends `awaiting-input`\n * and the answer arrives as an ordinary turn.\n *\n * `requiresApproval` applies to the first: the server can run the tool, but\n * asks first. That, too, ends the stream `awaiting-input`, which is the whole\n * reason there is no second endpoint — an approval is a question whose answer\n * happens to be yes or no.\n */\nexport type ToolDefinition<Name extends string, Input, Output, Progress = never> =\n | (ToolDefinitionBase<Name, Input, Output> & {\n answeredBy?: \"server\";\n execute: ToolExecute<Input, Output, Progress>;\n requiresApproval?: boolean;\n })\n | (ToolDefinitionBase<Name, Input, Output> & {\n answeredBy: \"client\";\n outputSchema: Schema<Output>;\n execute?: never;\n /** Meaningless here: the client answering *is* the approval. */\n requiresApproval?: never;\n });\n\n/**\n * `Progress` is inferred, never written down.\n *\n * It comes from the yield type of an `execute` that is an async generator, and\n * from nothing else — a tool that returns a promise gets `never`, which is the\n * honest statement that it cannot yield and is what makes\n * `ToolShapesOf`'s `progress` member safe to emit unconditionally. It is\n * carried as a fourth parameter rather than derived on demand because it has to\n * survive the trip through `ToolNamespace`, `FlattenTools` and `ToolShapesOf`\n * into the browser, and only a type argument does that.\n *\n * Structurally it lives on `execute`, which is optional, and which is also why\n * `AnyAgentTool` must pass `any` here: `Progress` sits covariantly inside\n * `AsyncGenerator<Progress, …>`, so a bound of `never` would make every\n * yielding tool fail the `Extract` in `ToolShapesOf` and silently vanish from\n * the shapes.\n */\nexport class AgentTool<\n Name extends string = string,\n Input = unknown,\n Output = unknown,\n Progress = never,\n> {\n readonly name: Name;\n readonly description: string;\n readonly inputSchema: Schema<Input>;\n readonly outputSchema?: Schema<Output>;\n readonly requiresApproval: boolean;\n readonly deferred: boolean;\n readonly answeredBy: \"server\" | \"client\";\n /**\n * There is deliberately no `namespace` here. A tool is a module-scope\n * singleton, so a field naming its group would hold whichever agent\n * constructed its namespace last and report that to every other one — the\n * same global-effect-from-a-local-declaration that `ToolNamespace.deferred`\n * avoids. Where a tool sits is a property of the agent, and it lives on the\n * agent's `ResolvedTool`.\n */\n readonly execute?: ToolExecute<Input, Output, Progress>;\n\n private constructor(params: ToolDefinition<Name, Input, Output, Progress>) {\n this.name = params.name;\n this.description = params.description;\n this.inputSchema = params.inputSchema;\n this.outputSchema = params.outputSchema;\n this.requiresApproval = params.requiresApproval === true;\n this.deferred = params.deferred === true;\n this.answeredBy = params.answeredBy === \"client\" ? \"client\" : \"server\";\n this.execute = params.execute ?? undefined;\n }\n\n /**\n * `const` on the params is what preserves `name` as a literal, which is what\n * lets the browser discriminate a tool part by name.\n */\n static create<const Name extends string, Input, Output, Progress = never>(\n params: ToolDefinition<Name, Input, Output, Progress>,\n ): AgentTool<Name, Input, Output, Progress> {\n return new AgentTool(params);\n }\n\n /**\n * Sugar for the common client tool: the agent asks the user something and\n * waits. Equivalent to `answeredBy: \"client\"` with an input schema of one\n * prompt field.\n */\n static ask<const Name extends string, Output>(params: {\n name: Name;\n description: string;\n outputSchema: Schema<Output>;\n }): AgentTool<Name, { question: string }, Output> {\n return AgentTool.create({\n name: params.name,\n description: params.description,\n inputSchema: questionSchema,\n outputSchema: params.outputSchema,\n answeredBy: \"client\",\n });\n }\n}\n\n/**\n * The one schema this module owns, rather than one built with `s`.\n *\n * `Schema<T>` carries a phantom property keyed by a symbol `Schema.ts` does not\n * export, so nothing outside that file can produce one without a cast — and\n * reaching for `s` here would make the agent runtime depend on the schema\n * builder for a single hard-coded object. One field, no `describe`, no\n * optionality: the cast is cheaper than the coupling.\n */\nconst questionSchema = {\n toJSONSchema: () => ({\n type: \"object\",\n properties: { question: { type: \"string\", description: \"What to ask the user\" } },\n required: [\"question\"],\n additionalProperties: false as const,\n }),\n parse(value: unknown) {\n const result = questionSchema.safeParse(value);\n if (result.ok === false) throw new Error(result.errors.join(\", \"));\n return result.value;\n },\n safeParse(value: unknown) {\n if (\n typeof value !== \"object\" ||\n value === null ||\n typeof (value as any).question !== \"string\"\n ) {\n return { ok: false as const, errors: [\"question: expected a string\"] };\n }\n return { ok: true as const, value: { question: (value as any).question } };\n },\n} as unknown as Schema<{ question: string }>;\n\nexport type AnyAgentTool = AgentTool<string, any, any, any>;\n\n/**\n * A group of tools the model can search as a unit.\n *\n * The provider's tool search works over namespaces, and the guidance is fewer\n * than ten functions in each — the model looks at a namespace's description to\n * decide whether anything inside is worth loading, so the grouping is part of\n * the prompt, not bookkeeping. A namespace is also the only place a\n * *collection* of tools can be described; on a flat list that sentence has\n * nowhere to go.\n *\n * Tool names stay globally unique within an agent, so the browser still\n * discriminates on `name` alone and the namespace never leaks into the client's\n * types.\n */\nexport class ToolNamespace<\n Name extends string = string,\n T extends readonly AnyAgentTool[] = readonly AnyAgentTool[],\n> {\n readonly name: Name;\n readonly description: string;\n readonly tools: T;\n /**\n * Kept here rather than pushed onto each tool. A tool is a module-scope\n * singleton and may be listed bare as well as inside a group; writing the\n * group's `deferred` onto it would defer it everywhere, which is a global\n * effect from a local declaration.\n */\n readonly deferred: boolean;\n\n private constructor(params: { name: Name; description: string; tools: T; deferred?: boolean }) {\n this.name = params.name;\n this.description = params.description;\n this.tools = params.tools;\n this.deferred = params.deferred === true;\n }\n\n static create<const Name extends string, const T extends readonly AnyAgentTool[]>(params: {\n name: Name;\n /** What the model reads when deciding whether to search inside. */\n description: string;\n tools: T;\n /** Defers every tool in the group, so the whole namespace costs its own\n * description plus one line per tool until something is loaded. */\n deferred?: boolean;\n }): ToolNamespace<Name, T> {\n return new ToolNamespace(params);\n }\n}\n\n/** What an agent's `tools` may hold: tools, or namespaces of them. */\nexport type ToolEntry = AnyAgentTool | ToolNamespace<string, readonly AnyAgentTool[]>;\n\ntype FlattenTools<T extends readonly ToolEntry[]> = T[number] extends infer E\n ? E extends ToolNamespace<any, infer NT>\n ? NT[number]\n : E\n : never;\n\n/**\n * The tool tuple, erased to the payload types the client is allowed to see.\n *\n * `progress` is emitted for every tool, `never` included, rather than only for\n * the ones that can yield. A conditional that dropped the member would make\n * `T[K][\"progress\"]` in `types.ts` resolve differently per tool, and this\n * package compiles with `strict: false` — where `undefined extends T` is true\n * of everything and an optional member is indistinguishable from a required\n * one. Two inference bugs in this module already came from testing a shape\n * under those options and believing the answer (see `OptionalSchema` in\n * `Schema.ts`); an unconditional member has nothing to get wrong.\n */\nexport type ToolShapesOf<T extends readonly ToolEntry[]> = {\n [K in Extract<FlattenTools<T>, AnyAgentTool> as K[\"name\"]]: K extends AgentTool<\n any,\n infer I,\n infer O,\n infer P\n >\n ? { input: I; output: O; progress: P }\n : never;\n};\n\n// --- skills --------------------------------------------------------------\n\n/**\n * A skill is instructions the model can go and fetch.\n *\n * Inlining every skill into the system prompt costs its tokens on every request\n * and gets worse with each skill added. So a skill is lowered to a tool: one\n * zero-parameter function per skill, in a reserved `skills` namespace, whose\n * description is the skill's and whose result is `instructions` plus any\n * `files`. Only those descriptions are prompted, and a skill the model never\n * reaches for costs a line of text.\n *\n * Lowering to a tool rather than to a synthetic `load_skill(name)` dispatcher\n * is the whole trick: discovery is then the same mechanism as everything else\n * the model chooses between, which means it runs on the provider's own\n * tool-selection machinery instead of on a string argument gemi would have to\n * validate, and a skill that is never loaded is a namespace entry rather than a\n * branch in our code. It is also why `deferred` applies here unchanged — with\n * tool search the namespace is searched, and without it the same tools are\n * listed inline, which for zero-parameter functions costs almost nothing.\n */\nexport interface SkillDefinition<Name extends string = string> {\n name: Name;\n /** Read on every request — this is what the model decides to load from. */\n description: string;\n /** A thunk so a large body stays off the startup path and out of memory. */\n instructions: string | (() => string | Promise<string>);\n /** Paths resolved relative to the app root, appended after `instructions`. */\n files?: string[];\n}\n\nexport class Skill<Name extends string = string> {\n readonly name: Name;\n readonly description: string;\n readonly instructions: string | (() => string | Promise<string>);\n readonly files?: string[];\n\n private constructor(params: SkillDefinition<Name>) {\n this.name = params.name;\n this.description = params.description;\n this.instructions = params.instructions;\n this.files = params.files;\n }\n\n static create<const Name extends string>(params: SkillDefinition<Name>): Skill<Name> {\n return new Skill(params);\n }\n}\n\n/** Reserved: a skill is lowered into a namespace of exactly this name. */\nexport const SKILLS_NAMESPACE = \"skills\";\n\nconst SKILLS_NAMESPACE_DESCRIPTION =\n \"Instructions this agent can load on demand. Load the relevant one before acting in the area it covers.\";\n\nconst EMPTY_PARAMETERS = {\n type: \"object\",\n properties: {},\n required: [] as string[],\n additionalProperties: false as const,\n};\n\n// --- agent ---------------------------------------------------------------\n\nexport type ReasoningEffort = \"minimal\" | \"low\" | \"medium\" | \"high\";\n\nexport interface CreateAgentParams<\n T extends readonly ToolEntry[],\n S extends readonly Skill[],\n O extends Schema<any> | undefined,\n> {\n name: string;\n /** The system prompt. Per-request additions belong on the controller, which\n * has the request; this is the part that is the same for everyone. */\n instructions?: string;\n provider: AgentProvider;\n tools?: T;\n /** Lowered into the reserved `skills` namespace — see `Skill`. The name is\n * reserved, so a namespace of your own cannot be called `skills`. */\n skills?: S;\n /**\n * Makes the final assistant turn strict JSON instead of prose. Tool turns are\n * unaffected — only the answer is constrained, which is the only place a\n * schema can apply once there is a tool loop.\n */\n output?: O;\n /** Ends the run with `finishReason: \"max-steps\"` rather than throwing: an\n * agent that loops is a bug to show, not an exception to swallow. */\n maxSteps?: number;\n /**\n * How far `ctx.runAgent` may nest below this agent. Default 3.\n *\n * It is a limit on the *tree*, taken from the run at the root, so raising it\n * on a sub-agent cannot deepen a run it did not start. A cycle is caught by\n * the agent-name chain before this is reached — this is for the mutually\n * recursive shape a name check cannot see, and for the merely runaway one.\n */\n maxDepth?: number;\n reasoning?: ReasoningEffort;\n /**\n * A ceiling on the tokens one model call may produce, passed to the provider\n * as its own `max_output_tokens`.\n *\n * It bounds a degenerate generation. A model asked for non-strict JSON can\n * fall into emitting the same character until something stops it, and with\n * no cap the only thing that does is the client giving up — a turn that\n * never ends rather than one that fails. The cap turns that into a run\n * ending with `finishReason: \"length\"`, which an app can retry.\n *\n * Per model call, not per run: a run of four steps may produce four times\n * this. `maxSteps` is the bound on the run.\n */\n maxOutputTokens?: number;\n /**\n * Passed to the provider unchanged. Lower is steadier, which is worth having\n * for a generation whose shape matters more than its phrasing.\n *\n * SENT WHENEVER SET, and not capability-gated the way `reasoning` is.\n * `ProviderCapabilities` carries no flag for it, so `buildResponsesRequest`\n * writes it whenever it is a number and never drops it. That matters because\n * the newer reasoning models reject the parameter outright: setting this for\n * one is a 400 rather than a quietly degraded request. It is the same bargain\n * `output` takes in `request.ts` — an explicit choice is sent and the API gets\n * to say no, because silently dropping one leaves an app believing something\n * about its request that is not true.\n */\n temperature?: number;\n}\n\n/**\n * One call per client turn — a first message and an answer to a pending\n * approval take the same path, because they are the same thing: the next turn\n * of a conversation.\n */\nexport interface AgentStreamParams {\n /** Prior turns. The controller loads these from its store, or takes what the\n * client sent when running stateless. */\n messages: AgentMessage[];\n /** The client's turn: text, answers to pending calls, or both. */\n turn?: ClientTurn;\n req: HttpRequest<any, any>;\n /** Aborted by an explicit `stop`, not by a disconnect. */\n signal?: AbortSignal;\n runId?: string;\n threadId?: string;\n /** Appended to the agent's own `instructions` for this request only. */\n instructions?: string;\n /**\n * The app's own fields from the turn's request body, handed to every tool of\n * this run as `ctx.body`.\n *\n * Carried on the run rather than left to be read from `ctx.req`: a run\n * outlives the request that started it, so that a refresh can reattach, and\n * by the time a tool executes there is no body left to read. See\n * `AgentController`'s `Body`.\n */\n body?: Record<string, unknown>;\n /** Per-request model choice, e.g. letting a user pick. */\n provider?: AgentProvider;\n maxSteps?: number;\n reasoning?: ReasoningEffort;\n /** Overrides the agent's own for this run. A generation whose size varies by\n * request — a page with ten components against one with two — is the case\n * a fixed ceiling on the agent cannot serve. */\n maxOutputTokens?: number;\n temperature?: number;\n /**\n * Fires once for every message this run completes — the user's turn, each\n * assistant turn, and any earlier message this turn amended by resolving a\n * pending call. It is the controller's persistence point, and it fires\n * whether or not anyone is still reading the stream, which is what makes a\n * run that outlives its request useful.\n *\n * A message may be reported twice across runs under the same id when a\n * pending call is resolved later; a store keyed by id should upsert.\n */\n onMessage?: (message: AgentMessage) => void | Promise<void>;\n /**\n * The attachment handle every tool of this run is given as `ctx.attachments`.\n *\n * Resolved by the controller from the request — `attachmentsFor(req,\n * threadId)` — and passed in rather than reached for, because the scope is a\n * fact about the caller and the run has no way to derive one. `null`, or\n * omitted, is a request with no subject: the object tools get still exists\n * and every method on it throws a sentence naming `attachmentScope()`.\n *\n * Handed down unchanged to a sub-run started by `ctx.runAgent`. A sub-agent\n * is running on behalf of the same caller — that is the only reason it is\n * allowed to run at all — so it reads and writes the same scope, and a tool\n * three levels down can be given an id its parent parked. Widening it here\n * would be the confused-deputy hole in reverse.\n */\n attachments?: ScopedAttachments | null;\n /**\n * Set by `ctx.runAgent` and by nothing else.\n *\n * It rides on the public params rather than on a back door because\n * `Agent.stream` is the only way to start a run and a sub-run is a run —\n * giving nesting its own construction path would mean two places where a run\n * is set up, and the second one would drift. Omitted, a run is a root: depth\n * 0, no path, signatures over its own id.\n */\n nesting?: NestedContext;\n}\n\n/**\n * Where a run sits inside a tree of runs. Carried down by `ctx.runAgent`.\n *\n * `signingRunId` and `signingPath` are the reason this is threaded rather than\n * recomputed: a pending call a sub-agent raises is answered by the *client*,\n * which only ever sees the root run, so the token has to be minted under the\n * root's id and the sub-run's path from the start. Re-signing the token at each\n * level on the way up would work too, and would throw away every signature but\n * the outermost one — this way the run that asks the question is also the run\n * that can check the answer, which is where the tool, its schema and its `kind`\n * all already are.\n */\nexport type NestedContext = {\n /** 0 at the root; `ctx.depth` inside a tool of this run. */\n depth: number;\n /** The `maxDepth` of the run at the root of the tree. */\n maxDepth: number;\n /** Agent names from the root down to and including this one, so a cycle can\n * be reported as the chain that caused it. */\n chain: string[];\n /** The root run's id: what a pending call raised here is signed under. */\n signingRunId: string;\n /** Tool-call ids from the root down to the call that started this run. */\n signingPath: string[];\n /**\n * Inherited, and once it is `\"deny\"` it stays `\"deny\"` all the way down. A\n * caller that asked for an autonomous sub-agent must not have a question\n * surface from three levels below it, and the only way to promise that is to\n * make the whole subtree refuse rather than to check at the top.\n */\n onPending: \"escalate\" | \"deny\";\n};\n\nexport type AgentRunResult<T extends ToolShapes, O> = {\n runId: string;\n /** Everything produced this run — the controller persists these. */\n messages: AgentMessage<T, O>[];\n finishReason: FinishReason;\n usage: Usage;\n /** Set when the agent declares an `output` schema and the run finished. */\n output?: O;\n};\n\n/**\n * A run is an async iterable of events, and the SSE encoding is a method on it\n * rather than a separate helper — so the same object serves a controller\n * returning a `Response` and a server-side caller that just wants to await the\n * result.\n *\n * A run keeps going when its request ends. That is what makes reattaching after\n * a refresh possible, and it is why `stop()` is an explicit call rather than the\n * client closing a socket.\n */\nexport interface AgentRun<T extends ToolShapes = ToolShapes, O = unknown> extends AsyncIterable<\n AgentStreamEvent<T, O>\n> {\n readonly runId: string;\n /** Numbered events, replayable from a cursor. `toResponse` is this, encoded. */\n frames(from?: number): AsyncIterable<AgentStreamFrame<T, O>>;\n toResponse(params?: { from?: number }): Response;\n result(): Promise<AgentRunResult<T, O>>;\n /**\n * Cancels the run and closes the conversation behind it: every tool call\n * still in flight gets a `denied` result with `cause: \"stopped\"`, the\n * assistant message is finalized with `finishReason: \"aborted\"`, and both go\n * through `onMessage` like any other message.\n *\n * That last part is the point. A cancel that merely stops emitting leaves a\n * history the provider will reject on the next turn, so the run's last act is\n * to make the transcript valid — which is also what lets the user carry on\n * talking instead of starting over.\n */\n stop(params?: { reason?: string }): void;\n}\n\n/** A tool plus where it sits in the prompt. Fixed for the life of the agent. */\ntype ResolvedTool = {\n tool: AnyAgentTool;\n namespace?: string;\n deferred: boolean;\n};\n\n/** What a run needs from its agent, resolved once at `Agent.create`. */\ntype RunConfig = {\n name: string;\n instructions?: string;\n provider: AgentProvider;\n registry: Map<string, ResolvedTool>;\n providerTools: (ProviderToolSpec | ProviderToolNamespace)[];\n output?: Schema<any>;\n maxSteps: number;\n maxDepth: number;\n reasoning?: ReasoningEffort;\n maxOutputTokens?: number;\n temperature?: number;\n};\n\nconst DEFAULT_MAX_STEPS = 8;\n/** Three is enough for \"agent, sub-agent, specialist\" and small enough that a\n * runaway tree is a readable error rather than a stack trace. */\nconst DEFAULT_MAX_DEPTH = 3;\n\nexport class Agent<\n T extends readonly ToolEntry[] = readonly ToolEntry[],\n S extends readonly Skill[] = readonly Skill[],\n O extends Schema<any> | undefined = undefined,\n> {\n readonly name: string;\n readonly tools: T;\n readonly skills: S;\n readonly provider: AgentProvider;\n readonly output: O;\n readonly instructions?: string;\n readonly maxSteps: number;\n readonly maxDepth: number;\n readonly reasoning?: ReasoningEffort;\n\n private readonly config: RunConfig;\n\n private constructor(params: CreateAgentParams<T, S, O>) {\n this.name = params.name;\n this.instructions = params.instructions;\n this.provider = params.provider;\n this.tools = (params.tools ?? ([] as unknown as T)) as T;\n this.skills = (params.skills ?? ([] as unknown as S)) as S;\n this.output = params.output as O;\n this.maxSteps = params.maxSteps ?? DEFAULT_MAX_STEPS;\n this.maxDepth = params.maxDepth ?? DEFAULT_MAX_DEPTH;\n this.reasoning = params.reasoning;\n\n const { registry, providerTools } = lowerTools(this.tools, this.skills);\n this.config = {\n name: this.name,\n instructions: this.instructions,\n provider: this.provider,\n registry,\n providerTools,\n output: params.output as Schema<any> | undefined,\n maxSteps: this.maxSteps,\n maxDepth: this.maxDepth,\n reasoning: this.reasoning,\n maxOutputTokens: params.maxOutputTokens,\n temperature: params.temperature,\n };\n }\n\n static create<\n const T extends readonly ToolEntry[],\n const S extends readonly Skill[],\n O extends Schema<any> | undefined = undefined,\n >(params: CreateAgentParams<T, S, O>): Agent<T, S, O> {\n return new Agent(params);\n }\n\n stream(params: AgentStreamParams): AgentRun<ToolShapesOf<T>, OutputOf<O>> {\n const config: RunConfig = {\n ...this.config,\n provider: params.provider ?? this.config.provider,\n maxSteps: params.maxSteps ?? this.config.maxSteps,\n reasoning: params.reasoning ?? this.config.reasoning,\n maxOutputTokens: params.maxOutputTokens ?? this.config.maxOutputTokens,\n temperature: params.temperature ?? this.config.temperature,\n };\n return new AgentRunImpl(config, params) as unknown as AgentRun<ToolShapesOf<T>, OutputOf<O>>;\n }\n}\n\nexport type OutputOf<O> = O extends Schema<any> ? Infer<O> : never;\n\nexport type AnyAgent = Agent<any, any, any>;\n\n// --- lowering ------------------------------------------------------------\n\nfunction toolSpec(resolved: ResolvedTool): ProviderToolSpec {\n return {\n name: resolved.tool.name,\n description: resolved.tool.description,\n parameters: resolved.tool.inputSchema.toJSONSchema(),\n // Read off the schema, not asserted: an input containing an `s.json()`\n // field cannot be sent strict, and the tool that says so is the only place\n // that knows.\n strict: supportsStrict(resolved.tool.inputSchema),\n deferred: resolved.deferred,\n };\n}\n\n/**\n * Flattens the declared tuple into the registry the loop dispatches on, and the\n * shape the provider is shown.\n *\n * Both are built once, at `Agent.create`, because neither depends on the\n * request: a tool is a singleton and a namespace is a static grouping. Building\n * them per run would be work repeated on every turn for an answer that cannot\n * change — and it would move the name-collision errors below out of startup and\n * into the first user's first message.\n */\nfunction lowerTools(\n entries: readonly ToolEntry[],\n skills: readonly Skill[],\n): {\n registry: Map<string, ResolvedTool>;\n providerTools: (ProviderToolSpec | ProviderToolNamespace)[];\n} {\n const registry = new Map<string, ResolvedTool>();\n const providerTools: (ProviderToolSpec | ProviderToolNamespace)[] = [];\n\n const register = (resolved: ResolvedTool) => {\n if (registry.has(resolved.tool.name)) {\n throw new Error(\n `Two tools are named \"${resolved.tool.name}\". Tool names are global within an agent — the client discriminates a tool part by name alone.`,\n );\n }\n registry.set(resolved.tool.name, resolved);\n };\n\n for (const entry of entries) {\n if (entry instanceof ToolNamespace) {\n if (entry.name === SKILLS_NAMESPACE) {\n throw new Error(\n `\"${SKILLS_NAMESPACE}\" is reserved for the namespace skills are lowered into. Rename the namespace — silently shadowing it would make every skill unreachable with no error to read.`,\n );\n }\n const members: ProviderToolSpec[] = [];\n for (const tool of entry.tools) {\n const resolved = { tool, namespace: entry.name, deferred: entry.deferred || tool.deferred };\n register(resolved);\n members.push(toolSpec(resolved));\n }\n providerTools.push({ name: entry.name, description: entry.description, tools: members });\n continue;\n }\n const resolved = { tool: entry, deferred: entry.deferred };\n register(resolved);\n providerTools.push(toolSpec(resolved));\n }\n\n if (skills.length > 0) {\n const members: ProviderToolSpec[] = [];\n for (const skill of skills) {\n const tool = skillTool(skill);\n register({ tool, namespace: SKILLS_NAMESPACE, deferred: false });\n members.push({\n name: skill.name,\n description: skill.description,\n parameters: EMPTY_PARAMETERS,\n strict: true,\n // Not deferred: the whole cost of a skill in the prompt is its name and\n // description, and those are exactly what deferral keeps. Withholding\n // an empty parameter object saves nothing and adds a round trip.\n deferred: false,\n });\n }\n providerTools.push({\n name: SKILLS_NAMESPACE,\n description: SKILLS_NAMESPACE_DESCRIPTION,\n tools: members,\n });\n }\n\n return { registry, providerTools };\n}\n\n/** The zero-parameter tool a skill becomes. */\nfunction skillTool(skill: Skill): AnyAgentTool {\n return AgentTool.create({\n name: skill.name,\n description: skill.description,\n inputSchema: {\n toJSONSchema: () => EMPTY_PARAMETERS,\n parse: () => ({}),\n safeParse: () => ({ ok: true as const, value: {} }),\n } as unknown as Schema<Record<string, never>>,\n // The thunk is called here, on load, and not at startup: a skill body can\n // be a megabyte of markdown, and an agent that declares twelve of them\n // should not read twelve files to answer \"hello\".\n execute: async () => {\n const body =\n typeof skill.instructions === \"function\" ? await skill.instructions() : skill.instructions;\n const sections = [body];\n for (const file of skill.files ?? []) {\n sections.push(`--- ${file} ---\\n${await readSkillFile(file)}`);\n }\n return sections.join(\"\\n\\n\");\n },\n }) as unknown as AnyAgentTool;\n}\n\nasync function readSkillFile(file: string): Promise<string> {\n try {\n return await Bun.file(file).text();\n } catch (error) {\n // A missing file is told to the model rather than thrown: the rest of the\n // skill is still worth having, and a run should not die because one of\n // several appendices moved.\n return `(could not be read: ${(error as Error).message})`;\n }\n}\n\n// --- the run -------------------------------------------------------------\n\nclass RunAborted extends Error {\n constructor() {\n super(\"The run was stopped\");\n this.name = \"RunAborted\";\n }\n}\n\nfunction raceAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {\n if (signal.aborted) {\n return Promise.reject(new RunAborted());\n }\n return new Promise<T>((resolve, reject) => {\n const onAbort = () => reject(new RunAborted());\n signal.addEventListener(\"abort\", onAbort, { once: true });\n promise.then(resolve, reject).finally(() => signal.removeEventListener(\"abort\", onAbort));\n });\n}\n\nfunction emptyUsage(): Usage {\n return { inputTokens: 0, outputTokens: 0, totalTokens: 0 };\n}\n\nfunction addUsage(total: Usage, next: Usage | undefined): Usage {\n if (!next) return total;\n const merged: Usage = {\n inputTokens: total.inputTokens + (next.inputTokens ?? 0),\n outputTokens: total.outputTokens + (next.outputTokens ?? 0),\n totalTokens: total.totalTokens + (next.totalTokens ?? 0),\n };\n if (next.reasoningTokens !== undefined || total.reasoningTokens !== undefined) {\n merged.reasoningTokens = (total.reasoningTokens ?? 0) + (next.reasoningTokens ?? 0);\n }\n if (next.cachedInputTokens !== undefined || total.cachedInputTokens !== undefined) {\n merged.cachedInputTokens = (total.cachedInputTokens ?? 0) + (next.cachedInputTokens ?? 0);\n }\n return merged;\n}\n\nfunction isAsyncGenerator(value: unknown): value is AsyncGenerator<unknown, unknown, void> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n typeof (value as AsyncGenerator).next === \"function\" &&\n Symbol.asyncIterator in (value as object)\n );\n}\n\n/**\n * The best parse of a JSON document that is still arriving.\n *\n * Exists so a UI can bind fields before the object closes. It closes whatever\n * brackets are open and drops a trailing key with no value; when even that does\n * not parse it gives up and returns an empty object rather than throwing,\n * because a snapshot is a convenience and a run must not die for one.\n */\nfunction bestEffortParse(text: string): any {\n if (!text.trim()) return {};\n try {\n return JSON.parse(text);\n } catch {\n // fall through to repair\n }\n const closers: string[] = [];\n let inString = false;\n let escaped = false;\n for (const char of text) {\n if (inString) {\n if (escaped) escaped = false;\n else if (char === \"\\\\\") escaped = true;\n else if (char === '\"') inString = false;\n continue;\n }\n if (char === '\"') inString = true;\n else if (char === \"{\") closers.push(\"}\");\n else if (char === \"[\") closers.push(\"]\");\n else if (char === \"}\" || char === \"]\") closers.pop();\n }\n let repaired = text;\n if (inString) repaired += '\"';\n repaired = repaired.replace(/[,:]\\s*$/, \"\");\n const suffix = closers.reverse().join(\"\");\n try {\n return JSON.parse(repaired + suffix);\n } catch {\n // A trailing `\"key\":` leaves a property with no value; drop the key too.\n try {\n return JSON.parse(repaired.replace(/,?\\s*\"[^\"]*\"\\s*$/, \"\") + suffix);\n } catch {\n return {};\n }\n }\n}\n\ntype StepOutcome = {\n reason: FinishReason;\n error?: AgentError;\n};\n\n/**\n * The prior transcript plus what a later run produced, upserted by id.\n *\n * A run only reports the messages it *made*, so a resumed sub-run's\n * `result().messages` is the tail and not the whole thing — and a message it\n * amended (the one holding the call that was finally answered) comes back under\n * an id the prior transcript already has. Appending would duplicate it and\n * replacing the array would lose everything before the resume, so the record on\n * `ToolCallPart.nested` is rebuilt by upsert, which is the same rule a store\n * keyed by message id follows.\n */\nfunction mergeMessages(prior: AgentMessage[], produced: AgentMessage[]): AgentMessage[] {\n const merged = [...prior];\n const index = new Map(merged.map((message, at) => [message.id, at]));\n for (const message of produced) {\n const at = index.get(message.id);\n if (at === undefined) {\n index.set(message.id, merged.length);\n merged.push(message);\n } else {\n merged[at] = message;\n }\n }\n return merged;\n}\n\n/** Tool calls in a transcript with no result anywhere in it. */\nfunction openCallIds(messages: AgentMessage[]): Set<string> {\n const resolved = new Set<string>();\n for (const message of messages) {\n for (const part of message.content) {\n if (part.type === \"tool-result\") resolved.add(part.toolCallId);\n }\n }\n const open = new Set<string>();\n for (const message of messages) {\n for (const part of message.content) {\n if (part.type === \"tool-call\" && !resolved.has(part.toolCallId)) open.add(part.toolCallId);\n }\n }\n return open;\n}\n\n/**\n * A sub-agent's structured answer, read back out of its transcript.\n *\n * `NestedRun` has nowhere to put an `output` — it is a transcript, and the\n * output part is already in it — so a memoized run recovers the value the same\n * way a client would. That keeps the memo honest: what a replay returns is\n * derived from what was persisted, not from a second copy that could disagree\n * with it.\n */\nfunction outputOf(messages: AgentMessage[]): unknown {\n for (let i = messages.length - 1; i >= 0; i--) {\n const content = messages[i].content;\n for (let j = content.length - 1; j >= 0; j--) {\n const part = content[j];\n if (part.type === \"output\" && part.partial !== true) return part.value;\n }\n }\n return undefined;\n}\n\n/** For the replay-mismatch message, where the label is what tells two runs of\n * the same agent apart. */\nfunction describeRun(agent: string, label: string | undefined): string {\n return label === undefined ? `\"${agent}\"` : `\"${agent}\" labelled \"${label}\"`;\n}\n\n/** A message flattened to text, for comparing one turn's seed against another's. */\nfunction textOfMessage(message: AgentMessage): string {\n return message.content\n .map((part) => (part.type === \"text\" ? part.text : `<${part.type}>`))\n .join(\"\");\n}\n\n/**\n * What a `runAgent` call would start its sub-run from, as a comparable string.\n *\n * `null` when the call names no seed at all — `runAgent(agent, {})` — which is\n * the one shape with nothing to compare against, since the record's first\n * message would then be something the sub-agent said rather than something it\n * was told.\n */\nfunction seedOf(params: RunAgentParams): string | null {\n if (params.prompt !== undefined) return `user:${params.prompt}`;\n const first = params.messages?.[0];\n return first ? `${first.role}:${textOfMessage(first)}` : null;\n}\n\n/**\n * The ids of the messages a tool injected to show a file, read from the\n * `ToolCallPart.attachments` records in `messages`.\n *\n * The record is the test, not `attachmentId` on the part: a user's own upload\n * carries an `attachmentId` too. `historyForProvider` keys its window on this\n * and `toolTurn` steps over these when it looks for the user's turn.\n */\nfunction injectedMessageIds(messages: AgentMessage[]): Set<string> {\n const injected = new Set<string>();\n for (const message of messages) {\n for (const part of message.content) {\n if (part.type !== \"tool-call\") continue;\n for (const record of part.attachments ?? []) {\n if (\"shown\" in record && record.shown) injected.add(record.shown.messageId);\n }\n }\n }\n return injected;\n}\n\n/**\n * How many tool-produced files stay attached to the request. See\n * `AgentRunImpl.historyForProvider`, which is where the reasoning lives.\n */\nconst SHOWN_FILE_WINDOW = 1;\n\n/**\n * Why the Nth attachment of a replayed tool body is not the Nth attachment of\n * the turn that escalated, or `null` when it is.\n *\n * DELIBERATELY WEAKER THAN `replayMismatch`, and the reason is what each of them\n * is protecting. A crossed sub-run pairs a human's answer with a question they\n * never saw, which is consent applied to the wrong thing and is invisible\n * afterwards; a crossed attachment shows the model the wrong picture, which is\n * wrong and is also the sort of wrong the next turn can talk its way out of. So\n * this checks the two things that cannot change for an honest reason and\n * nothing else.\n *\n * Not the size, and not any digest of the bytes: the body that produced this\n * blob ran again from the top and produced it again, and almost nothing that\n * makes an image — a model, a renderer with a timestamp in it, a compressor\n * with a thread pool — is byte-identical twice. Comparing bytes would fail the\n * common case and catch the rare one.\n *\n * Not the name either, for a smaller version of the same reason: filenames\n * carry dates and counters, and an app that names its output\n * `chart-${Date.now()}.png` would find its tool broken on every resume.\n *\n * What is left is the media type and whether the caller asked to show it, which\n * is exactly what changes when a body takes a different branch — the CSV path\n * instead of the PNG path, the quiet `put` instead of the `showModel` one.\n */\nfunction putMismatch(\n recorded: ToolAttachmentPut,\n blob: Blob,\n params: PutAttachmentParams,\n): string | null {\n const mimeType = params.mimeType || blob.type || \"\";\n if (mimeType && recorded.attachment.mimeType !== mimeType) {\n return (\n `was ${JSON.stringify(recorded.attachment.mimeType)} on the turn that escalated ` +\n `and is ${JSON.stringify(mimeType)} on the replay`\n );\n }\n const shown = Boolean(params.showModel);\n if (shown !== Boolean(recorded.shown)) {\n return shown\n ? \"was stored without showModel on the turn that escalated and asks for showModel on the replay\"\n : \"was stored with showModel on the turn that escalated and asks for a plain put on the replay\";\n }\n return null;\n}\n\n/**\n * Why the Nth sub-run of a replayed tool body is not the Nth sub-run of the\n * turn that escalated, or `null` when it is.\n *\n * Agent and label catch the branchy shape. THE SEED IS WHAT CATCHES THE SHAPE\n * THE DOC COMMENT NAMES FIRST: `runAgent` in a loop, the same agent every time,\n * no label — the default and the common case — over a list that came back in a\n * different order, from a `Set`, a re-sorted query, or a second read of a\n * mutable column. Agent and label match for every element of such a loop, so\n * without this the user's answer to the second question is folded into the run\n * the tool now believes is the first, and the model is told the crossed pair as\n * fact. Nothing in the transcript, the stream or the store shows it happened.\n *\n * The record's first message is the seed because `runNested` records it that\n * way — the user turn built from `prompt`, or the first of `params.messages`.\n * `instructions` is not compared: it never enters the transcript, and\n * `NestedRun` has nowhere to keep it.\n */\nfunction replayMismatch(\n recorded: NestedRun,\n agent: AnyAgent,\n params: RunAgentParams,\n): string | null {\n if (recorded.agent !== agent.name || recorded.label !== params.label) {\n return (\n `was ${describeRun(recorded.agent, recorded.label)} on the turn that escalated ` +\n `and is ${describeRun(agent.name, params.label)} on the replay`\n );\n }\n const seed = seedOf(params);\n const first = recorded.messages[0];\n const was = first ? `${first.role}:${textOfMessage(first)}` : null;\n if (seed !== null && was !== null && seed !== was) {\n return (\n `was started with ${JSON.stringify(was)} on the turn that escalated ` +\n `and with ${JSON.stringify(seed)} on the replay`\n );\n }\n return null;\n}\n\n/**\n * What is left of an answer's path once this run's own prefix is removed.\n *\n * `null` means the answer is not addressed to this run at all, which is a\n * client error rather than a routing decision — an empty remainder means \"a\n * call this run made itself\", and a non-empty one names the tool call to\n * re-enter.\n */\nfunction pathBelow(path: string[] | undefined, prefix: string[]): string[] | null {\n const full = path ?? [];\n if (full.length < prefix.length) return null;\n for (let i = 0; i < prefix.length; i++) {\n if (full[i] !== prefix[i]) return null;\n }\n return full.slice(prefix.length);\n}\n\n/**\n * The one memo rule a re-entered tool body plays by, in one place.\n *\n * A tool that escalates cannot be suspended — a paused async generator does not\n * fit in a message history — so it is re-entered FROM THE TOP on the next turn\n * and everything it did the first time has to be recognised rather than done\n * again. The only key available is the order the calls happened in, and the only\n * place a record can live is the message history, because that is the sole state\n * that crosses a turn boundary in a thread and in the browser alike.\n *\n * So: an array on the `ToolCallPart`, a snapshot of its length taken before the\n * body runs (everything already there came from an earlier turn and is\n * replayable; everything appended past that point is happening for the first\n * time), and an index that walks it. `ctx.runAgent` and `ctx.attachments.put`\n * both need exactly this and they share it here rather than each growing their\n * own copy — the failure of two copies is that one of them is fixed and the\n * other is not, and both are invisible until someone resumes.\n *\n * The array is created by the first WRITE and not before. `nested: []` or\n * `attachments: []` on every tool call would be a wire and store change paid for\n * by every app that has neither — and \"on first use\" is not good enough, because\n * a slot is handed out before the work that fills it can fail. A tool whose only\n * `put` throws (no attachment scope, a provider that cannot read files) must\n * leave a tool call with no `attachments` field, not an empty array announcing\n * an attachment that does not exist.\n *\n * A SLOT IS RESERVED WHEN IT IS ASKED FOR, THOUGH, NOT WHEN IT IS FILLED, and\n * that is deliberate: a tool body may run its `put`s or its `runAgent`s\n * concurrently — `await Promise.all(images.map((i) => ctx.attachments.put(i)))`\n * is the obvious way to write it — and every one of them takes its index\n * synchronously, in map order, before its first `await`. Handing out the index\n * on success instead would number them by the order they *finished*, which the\n * network decides and which the next turn will not reproduce. So the index\n * always advances, and a slot whose work threw stays unwritten.\n *\n * An unwritten slot before a written one would be a hole, and a hole is `null`\n * once it goes through JSON — which is what a consumer walking `part.attachments`\n * would crash on. `fill` is what stands in its place. `runAgent` passes none, so\n * `nested` keeps exactly the shape it has had since sub-agents existed, holes\n * and all: `NestedRun` describes a sub-run that happened and has no honest shape\n * for one that did not, and inventing one is a change to nesting rather than to\n * the issue this memo was extracted for.\n *\n * What is NOT shared is what to do with a hit, and it could not be: a recorded\n * sub-run that parked has to be *continued*, so `runAgent`'s memo sometimes\n * returns and sometimes falls through, while a recorded `put` is total — the\n * bytes are already stored and the id is already in the transcript the model\n * read, so there is nothing left to finish. The drift check differs too, and for\n * a reason worth reading: see `replayMismatch` against the note on\n * `putMismatch`.\n */\nclass ReplayMemo<R> {\n private index = 0;\n private readonly replayable: number;\n\n constructor(\n private readonly read: () => R[] | undefined,\n private readonly create: () => R[],\n /** What stands in for a slot whose work threw. See the note above. */\n private readonly fill?: () => R,\n ) {\n this.replayable = read()?.length ?? 0;\n }\n\n /**\n * The next slot: its index, the record a previous turn left there if it left\n * one, and the way to write this turn's.\n *\n * `write` is the only thing that touches the array. Call it once the record\n * exists and not before — everything between `next()` and `write()` is work\n * that may throw, and a slot nobody writes is a slot that costs nothing.\n */\n next(): { at: number; recorded: R | undefined; write: (record: R) => void } {\n const at = this.index++;\n return {\n at,\n recorded: at < this.replayable ? this.read()?.[at] : undefined,\n write: (record: R) => {\n const rows = this.read() ?? this.create();\n if (this.fill) {\n for (let i = rows.length; i < at; i++) rows[i] = this.fill();\n }\n rows[at] = record;\n },\n };\n }\n}\n\nclass AgentRunImpl implements AgentRun<ToolShapes, unknown> {\n readonly runId: string;\n\n private readonly config: RunConfig;\n private readonly params: AgentStreamParams;\n private readonly controller = new AbortController();\n\n private readonly buffer: AgentStreamFrame<ToolShapes, unknown>[] = [];\n private readonly waiters = new Set<() => void>();\n private seq = 0;\n private ended = false;\n\n /** The working history handed to the provider, and what this run produced. */\n private history: AgentMessage[] = [];\n private produced: AgentMessage[] = [];\n private current: AgentMessage | null = null;\n /**\n * Whether a step of the message now open ran out of output budget.\n *\n * Separate from `finishReason` because they are different facts: a step that\n * hits the ceiling and also calls a tool closes its message `awaiting-input` or\n * `max-steps`. Cleared as each message is finalized.\n */\n private outputTruncated = false;\n\n /** Messages from an earlier run this one has amended, by id. Cloned once and\n * reused, so two results for the same message do not fork it. */\n private readonly amended = new Map<string, AgentMessage>();\n /**\n * Messages this run finished outside `finalizeMessage` that have not yet gone\n * through `onMessage`: earlier turns' messages it amended, and messages it\n * injected for a file a tool showed.\n *\n * They wait rather than being reported where they are made, because\n * `onMessage` is the persistence point for an app that has no `store` and an\n * append-only table keyed by a serial id reads back in the order the hook was\n * called. Reporting an injected message from inside `runTools` would call the\n * hook for it BEFORE the assistant message whose tool call produced it, since\n * that message is only finalized once the step is over — so the transcript in\n * the app's database would put the file above the turn that made it, while\n * `result.messages`, the stream and `history` all put it below.\n */\n private readonly unreported = new Set<AgentMessage>();\n\n private usage: Usage = emptyUsage();\n private finishReason: FinishReason = \"stop\";\n private output: unknown;\n private stopReason: string | undefined;\n\n private readonly settled: Promise<AgentRunResult<ToolShapes, unknown>>;\n\n /**\n * Where this run sits in a tree of runs, all of it constant for the run.\n *\n * `signingRunId` is the *root's* id rather than this one's: the client only\n * ever sees the root run, so a question a sub-agent asks has to travel under\n * an id the client can hand back. `pathPrefix` is the chain of tool calls\n * above this run, and it is both what a pending call raised here is signed\n * over and what an answer coming back is matched against.\n */\n private readonly depth: number;\n private readonly maxDepth: number;\n private readonly chain: string[];\n private readonly signingRunId: string;\n private readonly pathPrefix: string[];\n private readonly onPending: \"escalate\" | \"deny\";\n /**\n * Sub-runs that have not yet written their transcript to the tool call.\n *\n * The abort path waits on these. A `stop()` reaches a sub-run through the\n * shared signal, so it is already closing — but `raceAbort` in `runTools`\n * returns the moment the signal fires, which would finalize and persist the\n * parent's message before the sub-run had recorded what it managed to do.\n * The work would be on the stream and missing from the store.\n */\n private readonly nestedSettling = new Set<Promise<unknown>>();\n /**\n * Messages for files a tool asked to show, held until its call settles, keyed\n * by tool call id. See `queueShown`.\n */\n private readonly shownQueue = new Map<string, AgentMessage[]>();\n\n constructor(config: RunConfig, params: AgentStreamParams) {\n this.config = config;\n this.params = params;\n this.runId = params.runId ?? `run_${crypto.randomUUID()}`;\n this.history = [...params.messages];\n\n const nesting = params.nesting;\n this.depth = nesting?.depth ?? 0;\n this.maxDepth = nesting?.maxDepth ?? config.maxDepth;\n this.chain = nesting?.chain ?? [config.name];\n this.signingRunId = nesting?.signingRunId ?? this.runId;\n this.pathPrefix = nesting?.signingPath ?? [];\n this.onPending = nesting?.onPending ?? \"escalate\";\n\n if (params.signal) {\n if (params.signal.aborted) this.controller.abort();\n else params.signal.addEventListener(\"abort\", () => this.stop(), { once: true });\n }\n\n // Started here, not on first read. A run outlives the request that began\n // it, so nothing may depend on someone being attached — a client that\n // never reads still gets its tools run and its messages persisted.\n this.settled = this.execute();\n // And the request that started it stays open until it settles. Its tools\n // read the user from that request's context, and a client that leaves\n // mid-run cancels the response body without stopping the run — ending the\n // request there would take the user away from step four's tool call.\n // `ctx()` is the ambient request store: undefined outside a request.\n params.req?.ctx?.()?.waitUntil(this.settled);\n }\n\n // --- event plumbing ----------------------------------------------------\n\n private emit(event: AgentStreamEvent<ToolShapes, unknown>) {\n if (this.ended) return;\n this.buffer.push({ seq: ++this.seq, event });\n this.wake();\n }\n\n private wake() {\n const pending = [...this.waiters];\n this.waiters.clear();\n for (const resolve of pending) resolve();\n }\n\n private nextFrame(): Promise<void> {\n return new Promise<void>((resolve) => this.waiters.add(resolve));\n }\n\n /**\n * Replays from the buffer, then follows the run live.\n *\n * The whole run is buffered rather than a sliding window: a run is bounded by\n * `maxSteps`, and a client that reconnects two steps late wanting frame 42\n * must get frame 42 and not \"the oldest I still have\". Bounding it is the\n * live-run registry's job, where the policy question is how long a *finished*\n * run is kept.\n */\n async *frames(from = 0): AsyncIterable<AgentStreamFrame<ToolShapes, unknown>> {\n let index = from > 0 ? from - 1 : 0;\n for (;;) {\n while (index < this.buffer.length) {\n yield this.buffer[index++];\n }\n if (this.ended) return;\n await this.nextFrame();\n }\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<AgentStreamEvent<ToolShapes, unknown>> {\n for await (const frame of this.frames()) {\n yield frame.event;\n }\n }\n\n toResponse(params?: { from?: number }): Response {\n const frames = this.frames(params?.from);\n const encoder = new TextEncoder();\n let cancelled = false;\n\n let keepalive!: ReturnType<typeof sseKeepalive>;\n\n const body = new ReadableStream<Uint8Array>({\n start: async (controller) => {\n // A slow tool or a thinking sub-agent can leave the connection silent\n // for longer than a proxy's idle timeout, and a proxy that closes it\n // looks to the client exactly like a run that finished.\n keepalive = sseKeepalive(controller);\n try {\n for await (const frame of frames) {\n if (cancelled) break;\n // `id:` carries the cursor so a browser reconnecting with\n // `Last-Event-ID` is already asking the right question.\n controller.enqueue(\n encoder.encode(`id: ${frame.seq}\\ndata: ${JSON.stringify(frame.event)}\\n\\n`),\n );\n keepalive.touch();\n }\n } catch {\n // A stream that cannot be written to is a dead reader, not a dead\n // run. Nothing to report and nothing to stop.\n }\n keepalive.stop();\n try {\n controller.close();\n } catch {\n // already closed by a cancel\n }\n },\n cancel: () => {\n // Deliberately does not touch the run. A disconnect is a reader\n // leaving; `stop()` is the only thing that cancels work, because the\n // tool loop is here and a closed tab has not stopped step four from\n // charging a card.\n cancelled = true;\n keepalive.stop();\n },\n });\n\n return new Response(body, {\n headers: {\n \"Content-Type\": \"text/event-stream; charset=utf-8\",\n \"Cache-Control\": \"no-cache, no-transform\",\n Connection: \"keep-alive\",\n // Tells nginx not to buffer, which would otherwise hold every frame\n // until the response ended and make a stream look like a long pause.\n \"X-Accel-Buffering\": \"no\",\n },\n });\n }\n\n result(): Promise<AgentRunResult<ToolShapes, unknown>> {\n return this.settled;\n }\n\n stop(params?: { reason?: string }): void {\n if (this.ended || this.controller.signal.aborted) return;\n this.stopReason = params?.reason;\n this.controller.abort();\n }\n\n // --- the loop ----------------------------------------------------------\n\n private async execute(): Promise<AgentRunResult<ToolShapes, unknown>> {\n this.emit({ type: \"run-start\", runId: this.runId, threadId: this.params.threadId });\n\n try {\n // A turn that answers a sub-agent's question re-enters the tool that\n // asked it, and that tool may ask again — so the run can be finished\n // before it has taken a single model step. Going on to `loop()` here\n // would step the model with a tool call still open, which is exactly the\n // history the provider rejects.\n const escalated = await this.ingestTurn();\n if (escalated.length > 0) {\n this.finishReason = \"awaiting-input\";\n this.emit({ type: \"awaiting-input\", runId: this.runId, pending: escalated });\n } else {\n await this.loop();\n }\n } catch (error) {\n if (error instanceof RunAborted || this.controller.signal.aborted) {\n await this.finalizeAborted();\n } else {\n const normalized = this.config.provider.normalizeError(error);\n this.emit({ type: \"error\", error: normalized });\n await this.finalizeMessage(\"error\");\n this.finishReason = \"error\";\n }\n }\n\n this.emit({ type: \"usage\", usage: this.usage });\n this.emit({ type: \"run-end\", runId: this.runId, finishReason: this.finishReason });\n this.ended = true;\n this.wake();\n\n return {\n runId: this.runId,\n messages: this.produced as AgentMessage<ToolShapes, unknown>[],\n finishReason: this.finishReason,\n usage: this.usage,\n output: this.output,\n };\n }\n\n private async loop(): Promise<void> {\n const maxSteps = Math.max(1, this.config.maxSteps);\n\n for (let step = 1; step <= maxSteps; step++) {\n const message = this.startMessage();\n const outcome = await this.runStep(message);\n\n if (outcome.error) {\n this.emit({ type: \"error\", error: outcome.error });\n await this.finalizeMessage(\"error\");\n this.finishReason = \"error\";\n return;\n }\n\n const calls = message.content.filter(\n (part): part is ToolCallPart => part.type === \"tool-call\",\n );\n\n if (calls.length === 0) {\n this.finishReason = outcome.reason;\n await this.finalizeMessage(outcome.reason);\n return;\n }\n\n const pending = await this.runTools(message, calls, step);\n\n if (pending.length > 0) {\n // The message closes first, then the run says what it is waiting for:\n // `awaiting-input` is terminal, and everything needed to answer it has\n // to already be on the stream when it arrives.\n this.finishReason = \"awaiting-input\";\n await this.finalizeMessage(\"awaiting-input\");\n this.emit({ type: \"awaiting-input\", runId: this.runId, pending });\n return;\n }\n\n if (step === maxSteps) {\n // Not an exception. An agent that will not stop calling tools is a bug\n // the app has to be able to see and show, and a throw here would put it\n // in a log instead of in the transcript.\n this.finishReason = \"max-steps\";\n await this.finalizeMessage(\"max-steps\");\n return;\n }\n\n await this.finalizeMessage(outcome.reason);\n }\n }\n\n private startMessage(): AgentMessage {\n const message: AgentMessage = {\n id: `msg_${crypto.randomUUID()}`,\n role: \"assistant\",\n content: [],\n createdAt: new Date().toISOString(),\n };\n this.current = message;\n this.history.push(message);\n this.produced.push(message);\n this.emit({ type: \"message-start\", messageId: message.id, role: \"assistant\" });\n return message;\n }\n\n private async finalizeMessage(reason: FinishReason): Promise<void> {\n const message = this.current;\n if (!message) return;\n this.current = null;\n // Read and cleared together: it describes the message being closed, and the\n // next one starts with no opinion.\n const outputTruncated = this.outputTruncated;\n this.outputTruncated = false;\n message.finishReason = reason;\n // On the message and not only on the frame. The frame reaches a live client,\n // which is half the audience: `onMessage` persists this object and\n // `result().messages` hands it back, and a transcript restored through\n // `useChat({ initialMessages })` has nothing else to read. `finishReason`\n // cannot answer it — a step that ran out of budget while calling a tool\n // closes `awaiting-input`, which is the case this exists for.\n if (outputTruncated) message.outputTruncated = true;\n // Before the message is handed to `onMessage` to be persisted and before it\n // reaches `result()` — the two places it stops being written and starts\n // being kept. Every exit lands here, aborted and errored runs included.\n for (const part of message.content) {\n if (part.type === \"text\" || part.type === \"reasoning\") {\n if (typeof part.text === \"string\") part.text = resolveRope(part.text);\n }\n }\n this.emit({\n type: \"message-end\",\n messageId: message.id,\n finishReason: reason,\n // Only when true, so the frame an ordinary message ends with is unchanged.\n ...(outputTruncated ? { outputTruncated: true as const } : {}),\n });\n await this.report(message);\n // After it, never before: a file a tool showed during this message belongs\n // below the message whose tool call produced it, in the hook exactly as it\n // is in `result().messages` and on the stream. Empty on every step of a run\n // with no such file, which is most of them.\n await this.reportDeferred();\n }\n\n private async report(message: AgentMessage): Promise<void> {\n if (!this.params.onMessage) return;\n try {\n await this.params.onMessage(message);\n } catch {\n // Persistence failing must not take the transcript with it: the messages\n // are still on the stream and still in `result()`.\n }\n }\n\n // --- one model call ----------------------------------------------------\n\n private async runStep(message: AgentMessage): Promise<StepOutcome> {\n const signal = this.controller.signal;\n const provider = this.config.provider;\n\n let outcome: StepOutcome = { reason: \"stop\" };\n const partialArgs = new Map<string, { name: string; args: string }>();\n let outputText = \"\";\n\n const stream = provider.stream({\n // Not `this.history` directly: an image a tool showed is in the history\n // forever and must not be in every *request* forever. See\n // `historyForProvider`.\n messages: this.historyForProvider(message),\n systemPrompt: await this.systemPrompt(),\n tools: this.config.providerTools.length > 0 ? this.config.providerTools : undefined,\n output: this.config.output\n ? {\n name: \"output\",\n schema: this.config.output.toJSONSchema(),\n strict: supportsStrict(this.config.output),\n }\n : undefined,\n reasoning: this.config.reasoning,\n maxOutputTokens: this.config.maxOutputTokens,\n temperature: this.config.temperature,\n signal,\n });\n\n const iterator = stream[Symbol.asyncIterator]();\n for (;;) {\n const next = await raceAbort(Promise.resolve(iterator.next()), signal);\n if (next.done) break;\n const event = next.value;\n\n switch (event.type) {\n case \"text-delta\": {\n appendText(message, \"text\", event.delta);\n this.emit({ type: \"text-delta\", messageId: message.id, delta: event.delta });\n break;\n }\n case \"reasoning-delta\": {\n appendReasoning(message, event.id, event.delta);\n this.emit({\n type: \"reasoning-delta\",\n messageId: message.id,\n delta: event.delta,\n id: event.id,\n });\n break;\n }\n case \"output-delta\": {\n outputText += event.delta;\n this.emit({\n type: \"output-delta\",\n messageId: message.id,\n delta: event.delta,\n snapshot: bestEffortParse(outputText),\n });\n break;\n }\n case \"tool-search\": {\n this.emit({ type: \"tool-search\", loaded: event.loaded });\n break;\n }\n case \"tool-call-delta\": {\n const held = partialArgs.get(event.toolCallId) ?? { name: event.name, args: \"\" };\n held.args += event.argsDelta;\n held.name = event.name || held.name;\n partialArgs.set(event.toolCallId, held);\n this.emit({\n type: \"tool-call\",\n messageId: message.id,\n part: {\n type: \"tool-call\",\n toolCallId: event.toolCallId,\n name: held.name,\n input: bestEffortParse(held.args),\n partial: true,\n },\n });\n break;\n }\n case \"tool-call\": {\n partialArgs.delete(event.toolCallId);\n const part: ToolCallPart = {\n type: \"tool-call\",\n toolCallId: event.toolCallId,\n name: event.name,\n // A raw string when the model produced something that is not JSON.\n // Keeping it is what makes the `invalid_tool_input` result below\n // readable instead of an empty object nobody can explain.\n input: parseArgs(event.args),\n };\n message.content.push(part);\n this.emit({ type: \"tool-call\", messageId: message.id, part });\n break;\n }\n case \"finish\": {\n this.usage = addUsage(this.usage, event.usage);\n // The usage is taken either way, the reason only if nothing has\n // already failed. A provider is allowed to report an error and then\n // close the call with a finish frame — a content filter does exactly\n // that, and it still bills for the tokens — and letting the closing\n // frame overwrite the outcome would turn \"blocked\" into an empty\n // answer with no explanation anywhere.\n if (!outcome.error) outcome = { reason: event.reason };\n break;\n }\n case \"error\": {\n outcome = { reason: \"error\", error: event.error };\n break;\n }\n }\n }\n\n // A tool call whose arguments never finished arriving. It is still a call\n // the model made, so it gets a part and, below, an `invalid_tool_input`\n // result — dropping it would leave the model unable to see what went wrong.\n for (const [toolCallId, held] of partialArgs) {\n const part: ToolCallPart = {\n type: \"tool-call\",\n toolCallId,\n name: held.name,\n input: parseArgs(held.args),\n };\n message.content.push(part);\n this.emit({ type: \"tool-call\", messageId: message.id, part });\n }\n\n // `length` is excluded, and it is the reason `maxOutputTokens` is worth\n // having at all. A cut-off answer is a prefix of the JSON the model meant\n // to write, and `bestEffortParse` closes whatever brackets are open — so a\n // truncated document arrives at `safeParse` looking like a whole one. For\n // a schema of `s.json()` fields it then *passes*, and the app is handed a\n // half-written page with nothing to distinguish it from a finished one.\n //\n // So a run that hit the ceiling produces no `output` part and ends with\n // `finishReason: \"length\"`, which is the channel for \"not an error, and not\n // a finished answer\" — the argument `max-steps` already makes in the\n // `FinishReason` type.\n //\n // No error is emitted, and that is a deliberate change rather than a\n // consequence of the cap. `length` does not mean the app set one: the\n // provider reports it from the model's own ceiling too, which is how this\n // was reachable before `maxOutputTokens` existed at all. Before this, a\n // truncated run fell through to `safeParse`, and a schema with required\n // keys among the missing ones failed it and raised a schema-mismatch error.\n // That diagnostic is gone on purpose — it described the truncation as a\n // model mistake, and it never fired for a schema loose enough to accept the\n // repaired prefix, which is the case that actually needed saying. What\n // replaces it is one answer for every schema: no output, and a finish\n // reason that says why.\n const truncated = outcome.reason === \"length\";\n // Recorded on the run rather than inferred from the message's finish reason,\n // which is not this: a step that hits the ceiling and also calls a tool ends\n // the message `awaiting-input` or `max-steps`. The client needs the fact\n // itself, or it completes a partial output the server withheld.\n if (truncated) this.outputTruncated = true;\n if (this.config.output && outputText && !outcome.error && !truncated) {\n const parsed = this.config.output.safeParse(bestEffortParse(outputText));\n if (parsed.ok === true) {\n this.output = parsed.value;\n message.content.push({ type: \"output\", value: parsed.value });\n } else {\n this.emit({\n type: \"error\",\n error: {\n code: \"unknown\",\n message: `The model's structured answer did not match the output schema: ${parsed.errors.join(\", \")}`,\n retryable: true,\n },\n });\n }\n }\n\n return outcome;\n }\n\n private async systemPrompt(): Promise<string | undefined> {\n const parts = [this.config.instructions, this.params.instructions].filter(\n (part): part is string => Boolean(part && part.trim()),\n );\n return parts.length > 0 ? parts.join(\"\\n\\n\") : undefined;\n }\n\n // --- tools -------------------------------------------------------------\n\n private async runTools(\n message: AgentMessage,\n calls: ToolCallPart[],\n step: number,\n ): Promise<PendingToolCall[]> {\n const pending: PendingToolCall[] = [];\n const running: Promise<void>[] = [];\n\n for (const call of calls) {\n const resolved = this.config.registry.get(String(call.name));\n\n if (!resolved) {\n this.addResult(message, {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"tool_error\",\n message: `There is no tool named \"${String(call.name)}\".`,\n toolCallId: call.toolCallId,\n retryable: true,\n },\n });\n continue;\n }\n\n const parsed = resolved.tool.inputSchema.safeParse(call.input);\n if (parsed.ok === false) {\n // Back to the model, not up the stack. A model that mis-typed one\n // argument can usually fix it on the next step, and throwing turns a\n // recoverable mistake into a dead run.\n this.addResult(message, {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_input\",\n message: `Invalid arguments for \"${String(call.name)}\": ${parsed.errors.join(\", \")}`,\n toolCallId: call.toolCallId,\n retryable: true,\n },\n });\n continue;\n }\n\n // The parsed value replaces the raw arguments on the part, and from here\n // on it is the only input this call has.\n //\n // A schema normalizes — it fills defaults, coerces, and drops the `null`s\n // that strict mode forces a model to send for an omitted optional. So\n // `safeParse(input)` and `input` are different values, and a pending call\n // has to be signed over, shown as, verified against and executed with the\n // *same* one. Keeping the raw value in the transcript and signing the\n // parsed one meant the MACs could not match on the way back: every\n // approval of a tool with an optional field came back looking forged, and\n // the user who clicked Approve was told they had refused.\n //\n // Writing it here rather than re-parsing on the way back also avoids\n // assuming `safeParse` is idempotent — the history now carries the value\n // the signature covers, so verification is a comparison and not a second\n // guess at what the first parse produced.\n call.input = parsed.value;\n\n const kind = pendingKind(resolved.tool);\n if (kind) {\n if (this.onPending === \"deny\") {\n this.addResult(message, this.deniedByPolicy(call));\n continue;\n }\n pending.push({\n toolCallId: call.toolCallId,\n name: call.name,\n input: parsed.value,\n kind,\n signature: signPendingCall(\n this.claimsFor(call.toolCallId, String(call.name), kind, parsed.value),\n ),\n ...(this.pathPrefix.length > 0 ? { path: [...this.pathPrefix] } : {}),\n });\n continue;\n }\n\n running.push(\n this.executeTool(resolved, message.id, call, parsed.value, step)\n .then(async (result) => {\n this.addResult(message, result);\n // The call has settled. Anything it asked to show goes into the\n // history now, after its own result — which is the order the\n // provider validates and the order it happened in.\n await this.settleShown(call.toolCallId);\n })\n .catch((error) => {\n if (error instanceof PendingEscalation) {\n if (this.onPending === \"deny\") {\n this.addResult(message, this.deniedByPolicy(call));\n return;\n }\n // Collected exactly like a pending call this run made itself, and\n // deliberately without a result on `call`: the tool did not\n // finish, so its call stays open and the next turn re-enters it.\n // Siblings are untouched — `Promise.all` below still waits for\n // them, and one that completes keeps its result rather than being\n // thrown away because a different tool asked a question.\n pending.push(...error.pending);\n return;\n }\n // Only `RunAborted` reaches here, and the abort path denies every\n // unresolved call at once — swallowing it keeps a stopped run from\n // also raising an unhandled rejection.\n }),\n );\n }\n\n await raceAbort(\n Promise.all(running).then(() => undefined),\n this.controller.signal,\n );\n return pending;\n }\n\n /**\n * What a pending call is signed over.\n *\n * `signingRunId` is the root run's, not this one's: the client only ever sees\n * the root, so a sub-agent's question has to be minted under an id the client\n * can hand back and this run can still recognise on the way in.\n */\n private claimsFor(\n toolCallId: string,\n name: string,\n kind: \"approval\" | \"question\" | \"client\",\n input: unknown,\n ) {\n return {\n runId: this.signingRunId,\n toolCallId,\n name,\n kind,\n input,\n // Absent rather than empty at the top level, so the signature a root run\n // mints is byte-for-byte the one it minted before nesting existed.\n path: this.pathPrefix.length > 0 ? [...this.pathPrefix] : undefined,\n };\n }\n\n /**\n * The refusal a sub-run running under `onPending: \"deny\"` gives itself.\n *\n * Told to the model rather than dropped, like every other denial: the\n * sub-agent asked for something it cannot have here, and the next step goes\n * better for knowing that than for finding a hole where a result should be.\n */\n private deniedByPolicy(call: ToolCallPart): ToolResultPart {\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"denied\",\n cause: \"refused\",\n reason: `\"${String(call.name)}\" needs the user, and this run was started with onPending: \"deny\". Answer from what you already have.`,\n };\n }\n\n private async executeTool(\n resolved: ResolvedTool,\n messageId: string,\n call: ToolCallPart,\n input: unknown,\n step: number,\n resume?: { answers: ClientToolResult[] },\n ): Promise<ToolResultPart> {\n const ctx: ToolContext = {\n req: this.params.req,\n // `{}` rather than undefined, so a tool can read a field without a guard\n // and get the same answer — absent — whether the client sent nothing or\n // the run was started without a body at all.\n body: this.params.body ?? {},\n runId: this.runId,\n threadId: this.params.threadId,\n toolCallId: call.toolCallId,\n signal: this.controller.signal,\n step,\n depth: this.depth,\n resumed: resume !== undefined,\n attachments: this.toolAttachments(call),\n turn: this.toolTurn(messageId),\n runAgent: this.nestedRunner(messageId, call, resume),\n };\n\n try {\n const started = resolved.tool.execute!(input as any, ctx);\n let output: unknown;\n if (isAsyncGenerator(started)) {\n let next = await started.next();\n while (!next.done) {\n this.emit({ type: \"tool-progress\", toolCallId: call.toolCallId, data: next.value });\n next = await started.next();\n }\n output = next.value;\n } else {\n output = await started;\n }\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"ok\",\n output,\n };\n } catch (error) {\n if (error instanceof RunAborted || this.controller.signal.aborted) {\n // Left to the abort path, which denies every unresolved call at once.\n throw new RunAborted();\n }\n if (error instanceof PendingEscalation) {\n // The one throw that is not a failure. Turning it into a `tool_error`\n // here would tell the model the tool broke and tell the user nothing,\n // and the question the sub-agent asked would be lost with no trace of\n // where it went — which is precisely the silent failure this branch\n // exists to prevent.\n throw error;\n }\n // A throwing tool is a result, not an exception out of the run: the model\n // is told the call failed and can try something else, which is what a\n // person would do.\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"tool_error\",\n message: error instanceof Error ? error.message : String(error),\n toolCallId: call.toolCallId,\n retryable: true,\n },\n };\n }\n }\n\n // --- nested runs -------------------------------------------------------\n\n /**\n * The `ctx.runAgent` given to one tool call, with its own memo.\n *\n * MEMOIZATION IS BY CALL INDEX, and the index is the only key there is. A\n * paused async generator cannot be put into a message history, so an\n * escalating tool is re-entered from the top rather than resumed in place,\n * and the Nth `runAgent` of the re-entered body has to be paired with the Nth\n * sub-run of the previous attempt. `ToolCallPart.nested` is that record, which\n * is also why it lives on the message: it is exactly the history the next turn\n * loads anyway, from the store or from the client.\n *\n * A tool whose `runAgent` calls sit inside a branch or a loop can produce a\n * different sequence on replay, and then index N means two different things.\n * That is checked below rather than trusted — pairing a user's answer with the\n * wrong sub-run is the failure this whole mechanism exists to avoid, and it\n * would be invisible.\n */\n private nestedRunner(\n messageId: string,\n call: ToolCallPart,\n resume?: { answers: ClientToolResult[] },\n ): ToolContext[\"runAgent\"] {\n // Snapshotted before the tool body runs, and lazily created on first use.\n // Both rules, and the reasoning behind them, are on `ReplayMemo` — which\n // `ctx.attachments.put` shares, so that the two memos on one tool call\n // cannot drift apart in how they decide what is a replay.\n const memo = new ReplayMemo<NestedRun>(\n () => call.nested,\n () => (call.nested = []),\n );\n\n return async (agent: AnyAgent, params: RunAgentParams = {}): Promise<NestedRunResult> => {\n const { at, recorded, write } = memo.next();\n\n if (recorded) {\n const mismatch = replayMismatch(recorded, agent, params);\n if (mismatch) {\n throw new Error(\n `Nested run ${at} of \"${String(call.name)}\" ${mismatch}. ` +\n `runAgent is memoized by call index, so a body whose runAgent calls depend on a condition that changed between turns cannot be resumed — the answer would be paired with a different sub-run. ` +\n `Make the sequence of runAgent calls, and what each one is asked, the same every time this tool runs, or branch on ctx.resumed.`,\n );\n }\n if (recorded.finishReason !== \"awaiting-input\") {\n // The whole point of the memo: no provider is called, no sub-tool\n // runs, and no usage is counted a second time — this turn did not\n // spend it, an earlier one did.\n return {\n runId: recorded.runId,\n agent: recorded.agent,\n messages: recorded.messages,\n finishReason: recorded.finishReason ?? \"stop\",\n usage: recorded.usage ?? emptyUsage(),\n output: outputOf(recorded.messages),\n nested: recorded,\n };\n }\n }\n\n return this.runNested(messageId, call, agent, params, write, recorded, resume?.answers ?? []);\n };\n }\n\n /**\n * Starts, or continues, one sub-run and joins it to this one.\n *\n * Joining is the only reason this exists — a tool can already make an agent\n * and iterate it. What it cannot do by hand is put the sub-run's events on\n * this run's stream in this run's `seq`, roll its usage up, record its\n * transcript where the next turn will look for it, and carry the depth and\n * the name chain so a cycle is a sentence rather than a stack overflow.\n */\n private async runNested(\n messageId: string,\n call: ToolCallPart,\n agent: AnyAgent,\n params: RunAgentParams,\n write: (record: NestedRun) => void,\n recorded: NestedRun | undefined,\n answers: ClientToolResult[],\n ): Promise<NestedRunResult> {\n // Both checks before anything starts, so the failure is a tool result the\n // model can read rather than a partly-run tree. The name chain catches the\n // common cycle (A runs A, A runs B runs A) exactly; the depth limit catches\n // the shapes a name cannot see, such as the same agent under two names.\n const chain = [...this.chain, agent.name];\n if (this.chain.includes(agent.name)) {\n throw new Error(\n `\"${agent.name}\" is already running further up this chain: ${chain.join(\" -> \")}. An agent cannot run itself, directly or through another agent.`,\n );\n }\n const depth = this.depth + 1;\n if (depth > this.maxDepth) {\n throw new Error(\n `Nested agent runs are ${this.maxDepth} deep at most and this one would be ${depth}: ${chain.join(\" -> \")}. Raise maxDepth on the agent at the root of the run if the tree is meant to be this deep.`,\n );\n }\n\n const label = params.label;\n const signingPath = [...this.pathPrefix, call.toolCallId];\n // Inherited downwards and never relaxed: a caller that asked for an\n // autonomous sub-agent must not have a question surface from two levels\n // below it, and only the subtree refusing can promise that.\n const onPending = this.onPending === \"deny\" ? \"deny\" : (params.onPending ?? \"escalate\");\n\n const resuming = recorded !== undefined;\n const open = resuming ? openCallIds(recorded.messages) : new Set<string>();\n // Only the answers this sub-run can actually attach to a call of its own,\n // or route further down. Handing it the rest would make it report a result\n // for a call nobody made.\n const mine = resuming\n ? answers.filter((answer) => {\n const below = pathBelow(answer.path, signingPath);\n if (below === null) return false;\n return open.has(below.length > 0 ? below[0] : answer.toolCallId);\n })\n : [];\n\n const sub = agent.stream({\n // A resume starts from what was persisted, not from what the tool passed\n // this time: the body ran again from the top and rebuilt its `prompt`,\n // and honouring it would replay a first turn the sub-agent has already\n // had. The persisted transcript already contains it.\n messages: resuming ? recorded.messages : (params.messages ?? []),\n turn: resuming ? { toolResults: mine } : params.prompt ? { text: params.prompt } : undefined,\n req: this.params.req,\n // The same scope, down the whole tree. A sub-agent runs on behalf of the\n // caller who started the parent — that is the only reason it is allowed\n // to run at all — so it reads and writes the caller's attachments, and a\n // tool three levels down can be handed an id its parent parked. There is\n // nothing to widen here and nothing to narrow: a second scope would be a\n // second answer to a question the request already answered once.\n attachments: this.params.attachments,\n // Inherited for the same reason the scope above is: a sub-agent is part\n // of the turn its parent is serving, so the fields the client sent with\n // that turn are as much its context as the parent's. A generator run\n // through `runAgent` whose tools could not see `pageId` would have to be\n // passed it through the prompt, as text, for the model to copy back.\n body: this.params.body,\n // Inherited, not new: this is what makes the parent's `stop()` reach a\n // sub-run three levels down without anything in between forwarding it.\n signal: this.controller.signal,\n threadId: this.params.threadId,\n instructions: params.instructions,\n maxOutputTokens: params.maxOutputTokens,\n temperature: params.temperature,\n // Kept across turns so the transcript the client already has keeps its\n // identity when the run continues.\n runId: resuming ? recorded.runId : undefined,\n nesting: {\n depth,\n maxDepth: this.maxDepth,\n chain,\n signingRunId: this.signingRunId,\n signingPath,\n onPending,\n },\n }) as AgentRun;\n\n let asked: PendingToolCall[] = [];\n const forwarding: Promise<void> = (async () => {\n for await (const event of sub as AsyncIterable<AgentStreamEvent>) {\n if (event.type === \"awaiting-input\") asked = event.pending;\n this.emit({\n type: \"nested-event\",\n toolCallId: call.toolCallId,\n runId: sub.runId,\n agent: agent.name,\n label,\n event,\n });\n }\n })();\n\n // Recording is its own promise so that the abort path can wait for exactly\n // this — the transcript reaching the tool call — rather than for the whole\n // tool, which may be ignoring the signal.\n const recording = (async () => {\n const result = await sub.result();\n await forwarding;\n // The seed leads, because a run only reports the messages it *made* and\n // `params.messages` is not one of them. Recording the transcript without\n // its opening is two bugs: a resume re-enters the sub-agent with the\n // conversation it was started from missing, and the replay check below\n // has nothing to fingerprint the seed against. Upserted rather than\n // concatenated because the sub-run may have amended one of these on its\n // way through.\n const messages = resuming\n ? mergeMessages(recorded.messages, result.messages)\n : mergeMessages(params.messages ?? [], result.messages);\n const record: NestedRun = {\n runId: sub.runId,\n agent: agent.name,\n label,\n messages,\n finishReason: result.finishReason,\n usage: resuming ? addUsage(recorded.usage ?? emptyUsage(), result.usage) : result.usage,\n // A parked record is what the next turn re-enters the tool on, and in\n // stateless mode it comes back from the browser. Signed here, by the\n // run that knows it is true, over where the sub-run parked, what it is\n // waiting on and the input the tool was running with — `parkedBelow`\n // will not act on a record without it. `call.input` is the parsed\n // value by now, on every path that reaches here, so the transcript\n // carries exactly what was signed.\n ...(result.finishReason === \"awaiting-input\"\n ? {\n signature: signNestedRun({\n runId: this.signingRunId,\n path: signingPath,\n nestedRunId: sub.runId,\n open: [...openCallIds(messages)],\n input: call.input,\n }),\n }\n : {}),\n };\n // Written before anything below can throw. An escalation is a pause, not\n // a lost run, and a cancelled sub-run is still work the user should be\n // able to read — both of those depend on the transcript already being on\n // the part when the throw happens.\n write(record);\n // Only what this turn actually spent. A memoized sub-run adds nothing,\n // above, because the turn that ran it already counted it.\n this.usage = addUsage(this.usage, result.usage);\n return { result, record };\n })();\n\n const settling = recording.then(\n () => undefined,\n () => undefined,\n );\n this.nestedSettling.add(settling);\n let result: AgentRunResult<ToolShapes, unknown>;\n let record: NestedRun;\n try {\n ({ result, record } = await recording);\n } finally {\n this.nestedSettling.delete(settling);\n }\n\n if (this.controller.signal.aborted) {\n // The sub-run was cancelled by the parent's `stop()`. Failing the tool\n // rather than returning an aborted result is what stops the tool body\n // from carrying on with half an answer while the run around it is dying.\n throw new RunAborted();\n }\n\n if (result.finishReason === \"awaiting-input\") {\n if (asked.length === 0) {\n // Nothing to ask means nothing the client could answer, and escalating\n // an empty list would end the parent awaiting-input with a tool call\n // that can never be resolved.\n throw new Error(\n `\"${agent.name}\" ended awaiting input but asked nothing, so there is no question to escalate.`,\n );\n }\n // The signature is on the server's copy of the record, and a stateless\n // client has built its own from the forwarded events. Re-sending the\n // call is what puts the server's copy in the client's hands to carry\n // back: the reducer takes a re-sent `nested` as authoritative, so this\n // replaces what the client accumulated rather than adding to it. Marked\n // `resent` because it is not a call: the model made this one earlier —\n // in this run or, on a re-park, in a previous one — and a hook that\n // counts calls has already seen it.\n this.emit({ type: \"tool-call\", messageId, part: call, resent: true });\n throw new PendingEscalation({ pending: asked, path: signingPath, nested: record });\n }\n\n return {\n runId: record.runId,\n agent: agent.name,\n messages: record.messages,\n finishReason: result.finishReason,\n usage: record.usage ?? emptyUsage(),\n output: result.output ?? outputOf(record.messages),\n nested: record,\n };\n }\n\n private addResult(message: AgentMessage, part: ToolResultPart) {\n message.content.push(part);\n this.emit({ type: \"tool-result\", messageId: message.id, part });\n }\n\n // --- files a tool made -------------------------------------------------\n\n /**\n * The `ctx.attachments` given to one tool call.\n *\n * Built per call rather than per run for one reason, and it is the same\n * reason `runAgent` is: the memo. A `put` has to be recognisable on the next\n * turn as the same `put`, and the only address a re-entered body has is \"the\n * Nth attachment of this tool call\" — so the object that counts them has to\n * belong to the call, not to the run.\n */\n private toolAttachments(call: ToolCallPart): ToolAttachments {\n const scoped = this.params.attachments ?? null;\n const memo = new ReplayMemo<ToolAttachmentRecord>(\n () => call.attachments,\n () => (call.attachments = []),\n // A put that threw took an index and wrote nothing, and if a later put in\n // the same call succeeds the gap has to be something rather than a hole:\n // `[null, { attachment }]` is what a hole becomes on the wire and in the\n // store, and it is what a UI or an app walking the memo crashes on. The\n // slot is re-attempted on a replay — `put` treats a failed record as no\n // record — so a transient failure heals itself on the turn the call\n // finally settles, and a permanent one stays legible as \"this put did not\n // produce a file\" instead of pretending it produced nothing at all.\n () => ({ failed: true }),\n );\n\n /**\n * The scope, or a sentence saying why there isn't one.\n *\n * A request with no subject gets no attachments at all — that is #489's\n * rule and this is where a tool meets it. The throw lands in `executeTool`,\n * which turns it into a `tool_error` the model reads, so the model is told\n * the tool cannot store files rather than being told nothing while the tool\n * quietly answers an id for bytes nobody kept. The message names the hook\n * an app has to override, because the reader who can act on it is the\n * developer looking at the transcript, not the model.\n */\n const scopeOrThrow = (): ScopedAttachments => {\n if (!scoped) {\n throw new InvalidAttachmentScopeError(\n \"This request has no attachment scope, so a tool cannot store or read files on it. `attachmentScope()` returned null — put the chat route behind authentication, send a `threadId`, or override `attachmentScope()` on the controller.\",\n );\n }\n return scoped;\n };\n\n return {\n get: (id: string) => scopeOrThrow().get(id),\n read: (id: string) => scopeOrThrow().read(id),\n file: (id: string) => scopeOrThrow().file(id),\n put: async (blob: Blob, params: PutAttachmentParams = {}): Promise<Attachment> => {\n const { at, recorded, write } = memo.next();\n\n // A `failed` record is a slot an earlier turn took and could not fill,\n // so there is nothing to replay and the work is done again.\n if (recorded && \"attachment\" in recorded) {\n const mismatch = putMismatch(recorded, blob, params);\n if (mismatch) {\n throw new Error(\n `Attachment ${at} of \"${String(call.name)}\" ${mismatch}. ` +\n `ctx.attachments.put is memoized by call index within a tool call, so a body whose put calls depend on a condition that changed between turns cannot be replayed — the model would be shown a file under an id that names different bytes. ` +\n `Make the sequence of put calls the same every time this tool runs, or branch on ctx.resumed.`,\n );\n }\n // Nothing is stored and nothing is uploaded. What IS repeated is the\n // queueing: the first attempt escalated before its result attached,\n // so the message was queued and never flushed, and this turn is the\n // one where the call finally settles. `settleShown` refuses a message\n // id the history already holds, which is what makes queueing twice\n // safe rather than merely unlikely.\n if (recorded.shown) this.queueShown(call.toolCallId, recorded);\n return recorded.attachment;\n }\n\n const attachments = scopeOrThrow();\n const mimeType = params.mimeType || blob.type || \"\";\n // A name is settled here rather than left to `ScopedAttachments.put`,\n // which falls back to the attachment id. The provider's upload wants a\n // filename, the injected `FilePart` carries one, and the stored record\n // has one — three copies that have to agree, so there is one value.\n const name = params.name ?? (blob instanceof File ? blob.name : \"attachment\");\n\n let fileId: string | undefined;\n if (params.showModel) {\n const provider = this.config.provider;\n if (!provider.capabilities.fileInput) {\n // Refused before a byte moves. `toResponsesInput` drops a file part\n // for a provider that cannot read one, so without this the tool\n // pays for an upload, stores a record claiming the model was shown\n // the file, and the model answers about an image that never reached\n // the wire — with nothing in the transcript, the logs or the bill\n // saying which of those three things went wrong.\n throw new Error(\n `\"${String(call.name)}\" asked to show a file to ${provider.model}, which does not accept file input. Drop \\`showModel\\` for this provider, or run this agent on a model that takes files — \\`capabilities.fileInput\\` is what says which do.`,\n );\n }\n // The provider first, storage second — the opposite of `upload`'s\n // order in `AgentController`, deliberately.\n //\n // That route stores first because either failure fails the request\n // and the only question is whose orphan it becomes. Here there is a\n // second question and it decides: the record this writes claims\n // `destination: \"both\"`, and a record cannot claim the vendor has a\n // copy before the vendor says so. Uploading first also means a file\n // the vendor refuses — a type it will not take, a size over its cap —\n // costs no storage write at all, and `showModel` is exactly the path\n // where that refusal is likeliest. The orphan when storage fails\n // afterwards is a file at the vendor with no record here, which is\n // the same orphan `AgentController.upload` accepts in the other\n // direction.\n fileId = await provider.upload(new File([blob], name, { type: mimeType || undefined }));\n }\n\n const attachment = await attachments.put(blob, { name, mimeType, fileId });\n const record: ToolAttachmentPut = {\n attachment,\n ...(fileId\n ? {\n shown: {\n fileId,\n // Minted here and written down, not derived later. The next\n // turn replays this record and has to produce the same\n // message — same id, same timestamp — or a reattached client\n // and a live one hold two copies of one image.\n messageId: `msg_${crypto.randomUUID()}`,\n createdAt: new Date().toISOString(),\n },\n }\n : {}),\n };\n // The slot is filled only now, with everything that could throw behind\n // it: a put that fails leaves the tool call exactly as it found it.\n write(record);\n if (record.shown) this.queueShown(call.toolCallId, record);\n return attachment;\n },\n };\n }\n\n /**\n * The `ctx.turn` given to one tool call. The reasoning is on `ToolTurn`.\n *\n * Anchored on `messageId`, the assistant message holding the call, which is\n * the same id on a re-entry: `amend` replaces the history entry with a clone\n * under the original's id, so the lookup is by id rather than by reference.\n * A message that is not in the history at all gets an empty list rather than\n * a guess.\n */\n private toolTurn(messageId: string): ToolTurn {\n const at = this.history.findIndex((message) => message.id === messageId);\n const injected = injectedMessageIds(this.history);\n let user: AgentMessage | undefined;\n for (let i = at - 1; i >= 0; i--) {\n const message = this.history[i];\n if (message.role !== \"user\" || injected.has(message.id)) continue;\n user = message;\n break;\n }\n const ids: string[] = [];\n for (const part of user?.content ?? []) {\n if (part.type !== \"file\") continue;\n // The prefix test `providers/request.ts` applies before telling the model\n // an id. A stateless client's history can put anything here, and a value\n // the model was never shown as an id should not reach a tool as one.\n const id = part.attachmentId;\n if (typeof id !== \"string\" || !id.startsWith(ATTACHMENT_ID_PREFIX)) continue;\n if (!ids.includes(id)) ids.push(id);\n }\n return Object.freeze({ attachments: Object.freeze(ids) });\n }\n\n /**\n * Holds the message for a shown file until its tool call settles.\n *\n * WHY IT WAITS. The message is input-role and it has to sit *after* the tool\n * call's result, because that is the order it happened in and the order the\n * provider validates: a `function_call` and its `function_call_output` are a\n * pair, and a user message wedged between them is a history the API rejects.\n * Emitting it the moment `put` returns would do exactly that, since the tool\n * is still running.\n *\n * WHY A TOOL THAT ESCALATED GETS NOTHING. Its call has no result yet, so\n * flushing would leave an image in the transcript attached to a call that has\n * not finished — and the next turn re-enters the body, replays the `put` and\n * would queue a second copy. The queue simply dies with the run; the record\n * on the tool call survives, and the turn that finally settles the call is\n * the turn that shows the file.\n */\n private queueShown(toolCallId: string, record: ToolAttachmentPut) {\n const shown = record.shown;\n if (!shown) return;\n const queued = this.shownQueue.get(toolCallId) ?? [];\n if (queued.some((message) => message.id === shown.messageId)) return;\n queued.push({\n id: shown.messageId,\n role: \"user\",\n content: [\n {\n type: \"file\",\n fileId: shown.fileId,\n name: record.attachment.name,\n mimeType: record.attachment.mimeType,\n // Shown to the model beside the file (see `attachmentLine` in\n // `providers/request.ts`), so a file one tool made can be the input\n // of the next. Not what marks the part as injected — a user's upload\n // carries one too; `historyForProvider` keys on the message id.\n attachmentId: record.attachment.id,\n },\n ],\n createdAt: shown.createdAt,\n // Complete the instant it is made. Without this the client's `run-end`\n // safety net would find a message with no finish reason and stamp one on,\n // which is a difference between a live client and a reattached one over a\n // message that was never streaming in the first place.\n finishReason: \"stop\",\n });\n this.shownQueue.set(toolCallId, queued);\n }\n\n /**\n * The tool call settled: its files go into the transcript now.\n *\n * Pushed to `history` (so the next step sees them), to `produced` (so\n * `result().messages` carries them and the controller persists them), emitted\n * (so a client watching live sees the same conversation a reattached one\n * replays), and queued for `onMessage` (so an app that persists from the hook\n * stores it). A message that is in some of those four and not the others is\n * the bug class #470 is about, and an injected message is the easiest place in\n * the codebase to write it.\n *\n * QUEUED FOR THE HOOK RATHER THAN REPORTED HERE, so that all four agree on\n * ORDER and not merely on contents. The assistant message that made this tool\n * call has not been finalized yet — `loop` does that after `runTools`\n * returns — so calling `onMessage` from here would hand an app the file\n * before the turn that produced it. `reportDeferred` is where the queue is\n * drained, immediately after that assistant message is reported.\n *\n * The id guard is for a history that already holds the message — a stateless\n * client posting back a transcript that contains the injection *and* still\n * shows the call as open, which is a rewind the server cannot rule out. One\n * copy either way.\n *\n * NOTHING IS SHOWN ONCE THE RUN IS OVER, and the check belongs here rather than\n * at the three call sites because one of those sites fires after the run has\n * finished. `finalizeAborted` never calls this — it denies every open call\n * instead — but a `/stop` does not wait for a tool that is already running:\n * `raceAbort` in `runTools` returns the moment the signal fires, the run\n * finalizes and ends, and the tool's own `.then` lands afterwards and calls\n * this. Without the guard the message goes into `history` and `produced` and\n * through `onMessage` while `emit` is already a no-op, so it is persisted and\n * never announced, and a client that reloads the thread sees an image a client\n * that watched it live never saw. That is the exact divergence this issue\n * asked to avoid, arrived at from the one direction nobody looks. Appending an\n * image to a conversation the user has just cancelled would also be the run\n * getting the last word. The bytes are kept and the record is on the tool\n * call, so a tool that runs again can still resolve the id; nothing is lost\n * but the showing.\n */\n private async settleShown(toolCallId: string): Promise<void> {\n const queued = this.shownQueue.get(toolCallId);\n if (!queued || queued.length === 0) return;\n this.shownQueue.delete(toolCallId);\n if (this.ended || this.controller.signal.aborted) return;\n for (const message of queued) {\n if (this.history.some((held) => held.id === message.id)) continue;\n this.history.push(message);\n this.produced.push(message);\n this.emit({ type: \"message\", message });\n this.unreported.add(message);\n }\n }\n\n /**\n * The history as the provider sees it: everything, minus all but the most\n * recent tool-produced file.\n *\n * THE PROBLEM. `buildResponsesRequest` is handed the whole history on every\n * step, so a file injected at step two is re-sent at steps three, four and\n * five. An edit loop that iterates three times therefore pays for three\n * images on every later call — image tokens are not small, and they are\n * charged again each step — on top of one provider upload per iteration. Left\n * alone this is a cost that grows with the square of the loop and shows up on\n * an invoice rather than in a stack trace.\n *\n * WHAT IS TRIMMED, AND WHERE. Here, on the way to the provider, and nowhere\n * else. The transcript keeps every injected message: `history`, `produced`,\n * `onMessage`, the stream, and the client all hold the same conversation they\n * would have held without this method, so a `/attach` replay still matches a\n * live stream and a user scrolling back still sees every version the agent\n * made. Trimming the stored transcript instead would have meant editing a\n * message after it was persisted and announced, which is the one thing the\n * message contract does not allow.\n *\n * WHAT IT COSTS, AND IT IS A REAL CAPABILITY. With a window of one, the model\n * cannot compare this iteration against the last one. \"Is this closer than\n * the previous attempt?\" is a question it can no longer answer from what it\n * can see, and an agent whose job is to converge on a target by comparison\n * genuinely wants two. One is the default because the failure of too small a\n * window is visible and cheap — the model says it cannot see the earlier\n * image, in the transcript, on the first run — while the failure of too large\n * a one is a bill nobody reads until the end of the month. The dropped part\n * is replaced by a line of text rather than removed, so the model is told the\n * image existed and why it is gone; a hole would leave it to conclude it had\n * imagined seeing anything.\n *\n * IT IS A CONSTANT, NOT A KNOB, ON PURPOSE. A configurable window is one line\n * to add later and cannot be taken back once apps depend on it, and nobody\n * has a second value to name yet. Only files the run injected are counted —\n * a file the *user* attached is one they expect to stay attached, and\n * dropping it would be the agent losing the thing it was asked about.\n */\n private historyForProvider(current: AgentMessage): AgentMessage[] {\n const messages = this.history.filter((message) => message !== current);\n\n // Injected messages are named by the tool call that made them, and that\n // record is the test — not `attachmentId` on the part. A user's own upload\n // carries an `attachmentId` too: `ingestTurn` copies it from `turn.files`,\n // and `useChat` spreads the entry onto its local user message.\n const injected = injectedMessageIds(messages);\n\n const shown: FilePart[] = [];\n for (const message of messages) {\n if (!injected.has(message.id)) continue;\n for (const part of message.content) {\n if (part.type === \"file\") shown.push(part);\n }\n }\n if (shown.length <= SHOWN_FILE_WINDOW) return messages;\n\n const dropped = new Set(shown.slice(0, shown.length - SHOWN_FILE_WINDOW));\n return messages.map((message) => {\n if (!message.content.some((part) => part.type === \"file\" && dropped.has(part))) {\n return message;\n }\n return {\n ...message,\n content: message.content.map((part) =>\n part.type === \"file\" && dropped.has(part)\n ? {\n type: \"text\" as const,\n text: `[A ${part.mimeType || \"file\"} produced by a tool (attachment ${part.attachmentId}) was attached here and has been dropped from this request: only the most recent tool-produced file is kept attached, to keep the context bounded. Call the tool again if you need to look at it.]`,\n }\n : part,\n ),\n };\n });\n }\n\n // --- the client's turn -------------------------------------------------\n\n /**\n * Resolves what the client sent back, then adds its words.\n *\n * Every pending call has to come out of this with a result — signed, refused\n * or implicitly denied. The provider rejects a history holding a tool call\n * with no result, so leaving one open would break not this turn but the next\n * one, at a point where the cause is no longer visible.\n */\n private async ingestTurn(): Promise<PendingToolCall[]> {\n const turn = this.params.turn;\n const open = this.openCalls();\n /** Questions a re-entered tool asked again. The run ends on these. */\n const escalated: PendingToolCall[] = [];\n\n if (open.length > 0) {\n const answered = new Set<string>();\n const seen = new Set<string>();\n /** Answers addressed *below* one of this run's tool calls, grouped by the\n * call that has to be re-entered to deliver them. */\n const reentry = new Map<string, ClientToolResult[]>();\n\n for (const answer of turn?.toolResults ?? []) {\n // One answer per call, first one wins. A turn carrying the same entry\n // twice is a retried submit or a double-clicked form, and without this\n // it ran the approved tool twice and left two results for one\n // toolCallId — a history the provider rejects, arrived at by exactly\n // the machinery that exists to keep the history well formed.\n //\n // Keyed by path *and* id, because a tool-call id is only unique within\n // one run: two sub-agents under two different tools each number their\n // calls from their own provider, and dropping the second as a duplicate\n // would strand the tool that was waiting on it. For a top-level answer\n // the key is the id, exactly as before.\n const key = `${(answer.path ?? []).join(\"/\")}#${answer.toolCallId}`;\n if (seen.has(key)) continue;\n seen.add(key);\n\n const reject = (message: string) =>\n this.emit({\n type: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message,\n toolCallId: answer.toolCallId,\n retryable: false,\n },\n });\n\n // The path says which run the answer belongs to; `toolCallId` only says\n // which call *within* that run. Both are covered by the signature, so a\n // client that moves an answer to another tool's sub-run does not\n // redirect anything — it routes the answer somewhere the MAC no longer\n // verifies, which is the property that makes carrying the path safe.\n const below = pathBelow(answer.path, this.pathPrefix);\n if (below === null) {\n reject(\n `The answer for \"${answer.toolCallId}\" is addressed to a tool call this run is not inside.`,\n );\n continue;\n }\n\n if (below.length > 0) {\n const host = below[0];\n const hosting = open.find((entry) => entry.call.toolCallId === host);\n if (!hosting) {\n reject(`No pending tool call with id \"${host}\" to deliver a nested answer to.`);\n continue;\n }\n // THE ONE CHECK THAT MAKES RE-ENTRY SAFE, and the reason it is here\n // rather than in `reenter`.\n //\n // Re-entry runs the tool. Everything else in this method verifies a\n // signature first, but a path cannot be verified here — the claims\n // are the *inner* call's, and only the run that minted them knows its\n // tool, its `kind` and its input, which is why `resolveAnswer` runs\n // down there and not up here. So the decision to execute has to be\n // gated on something the server derived instead: there must be a\n // sub-run parked on this exact call, and it must be waiting on the\n // exact question the answer names. Without this, a turn that posts\n // `{ path: [<any open call>], signature: \"\" }` re-enters a tool that\n // is merely awaiting an *approval* — running, unapproved, a call the\n // user was shown and never said yes to, with its input taken from a\n // client-carried history. Content is still checked below, in the\n // sub-run; this is what stops an unsigned request from choosing to\n // execute at all.\n const parked = this.parkedBelow(hosting.call, below, answer.toolCallId);\n if (parked.ok === false) {\n const under = `The answer for \"${answer.toolCallId}\" is addressed under \"${host}\", which`;\n reject(\n parked.reason === \"unparked\"\n ? `${under} has no sub-agent run waiting on that question.`\n : parked.reason === \"expired\"\n ? `${under} parked a sub-agent run that has since expired. Ask again.`\n : `${under} carries a record of a parked sub-agent run the server did not sign.`,\n );\n continue;\n }\n let group = reentry.get(host);\n if (!group) {\n // Spent here, ahead of the body, for the reason the record is\n // signed at all: re-entry runs the tool before the sub-run gets to\n // refuse a spent answer, so a history rewound to before the result\n // would run it once per replay. Once per record per turn — the\n // sub-run may have asked two things at once, and every answer to\n // it re-enters the same tool a single time.\n if (!consumeNestedRun(parked.signature)) {\n reject(\n `The answer for \"${answer.toolCallId}\" is addressed under \"${host}\", which has already been re-entered on that record. Ask again.`,\n );\n continue;\n }\n group = [];\n reentry.set(host, group);\n // Marked answered so the refusal pass below leaves it alone: the\n // tool is about to be re-entered and will produce the real result.\n answered.add(host);\n }\n group.push(answer);\n continue;\n }\n\n const target = open.find((entry) => entry.call.toolCallId === answer.toolCallId);\n if (!target) {\n // Nothing to attach it to, so it cannot be told to the model even as\n // an error part — a result for a call that was never made.\n reject(`No pending tool call with id \"${answer.toolCallId}\".`);\n continue;\n }\n\n const result = await this.resolveAnswer(target, answer, escalated);\n if (result === null) continue;\n answered.add(answer.toolCallId);\n // `\"open\"` is an approved tool whose own sub-agent asked something on\n // the way through: answered, so the refusal pass leaves it alone, but\n // no result attaches — the call stays open and the next turn re-enters\n // it, exactly as an escalation from the step loop does. Nothing it\n // showed is flushed either, for the same reason: the call has not\n // settled, so the file waits for the turn where it does.\n if (result !== \"open\") {\n this.attachToHistory(target, result);\n await this.settleShown(answer.toolCallId);\n }\n }\n\n for (const [host, answers] of reentry) {\n const entry = open.find((item) => item.call.toolCallId === host)!;\n const result = await this.reenter(entry, answers, escalated);\n if (result) {\n this.attachToHistory(entry, result);\n // The turn a re-entered tool finally settles on is the turn its files\n // are shown, however many turns ago it stored them.\n await this.settleShown(host);\n }\n }\n\n for (const entry of open) {\n if (answered.has(entry.call.toolCallId)) continue;\n // The turn said something else. That is a refusal — the honest reading,\n // and the only one that cannot strand the thread. A tool whose\n // sub-agent asked a question and did not get an answer is refused here\n // like any other: the sub-run is abandoned with its transcript intact,\n // and the call gets a result rather than dangling into the next turn.\n this.attachToHistory(entry, {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"denied\",\n cause: \"refused\",\n });\n }\n\n await this.reportDeferred();\n }\n\n if (turn && (turn.text || (turn.files && turn.files.length > 0))) {\n const message: AgentMessage = {\n id: `msg_${crypto.randomUUID()}`,\n role: \"user\",\n content: [\n ...(turn.text ? [{ type: \"text\" as const, text: turn.text }] : []),\n // Field by field rather than spread: an entry can carry whatever\n // `attach()` answered (`downgraded`, `size`), and only these four\n // mean anything on a `FilePart`. `attachmentId` is among them — it is\n // what lets the model name this file to a tool — and an absent\n // `fileId` is a storage-only upload, left out rather than written as\n // `undefined`.\n ...(turn.files ?? []).map((file) => ({\n type: \"file\" as const,\n ...(file.fileId ? { fileId: file.fileId } : {}),\n name: file.name,\n mimeType: file.mimeType,\n ...(file.attachmentId ? { attachmentId: file.attachmentId } : {}),\n })),\n ],\n createdAt: new Date().toISOString(),\n finishReason: \"stop\",\n };\n this.history.push(message);\n this.produced.push(message);\n await this.report(message);\n }\n\n return escalated;\n }\n\n /**\n * Whether a sub-run under `call` is actually parked on the question named.\n *\n * The transcript is the server's own record of where the run stopped:\n * `nested` is written from the sub-run's result before the escalation throws,\n * so a call that has never nested has no `nested` at all, and one whose\n * sub-runs all finished has none with `awaiting-input`. In stateless mode\n * that record arrives from the client and could say anything, and what\n * saying it buys is not small: the tool body runs — from the top, with the\n * input the history carries — before the sub-run gets to verify the answer,\n * so everything the body does ahead of its first `runAgent` happens on the\n * client's say-so. Which is why the record has to carry the server's\n * signature over the sub-run it parked, the calls it left open and the\n * input the tool was given, and why a record without one, or with one that\n * does not match what it now says, is not a parked run at all.\n *\n * The failure says which of those it was. \"Expired\" is an ordinary outcome\n * in threaded mode — a question answered a day late — and telling that\n * user the run never parked would send them looking for a bug that is not\n * there; a record that fails its MAC is the other thing entirely, and the\n * two are kept apart for the same reason `resolveAnswer` keeps them apart.\n *\n * `below` is the answer's path with this run's prefix already removed, so\n * `below[0]` is `call` itself and `below[1]`, when there is one, names the\n * call to re-enter one level further down.\n */\n private parkedBelow(\n call: ToolCallPart,\n below: string[],\n toolCallId: string,\n ): { ok: true; signature: string } | { ok: false; reason: \"unparked\" | \"unsigned\" | \"expired\" } {\n const wanted = below.length > 1 ? below[1] : toolCallId;\n const path = [...this.pathPrefix, call.toolCallId];\n const parked = (call.nested ?? []).find(\n (run) => run.finishReason === \"awaiting-input\" && openCallIds(run.messages).has(wanted),\n );\n if (!parked) return { ok: false, reason: \"unparked\" };\n if (typeof parked.signature !== \"string\") return { ok: false, reason: \"unsigned\" };\n const verified = verifyNestedRun(parked.signature, {\n path,\n nestedRunId: parked.runId,\n open: [...openCallIds(parked.messages)],\n input: call.input,\n });\n if (verified.ok === false) {\n return { ok: false, reason: verified.reason === \"expired\" ? \"expired\" : \"unsigned\" };\n }\n return { ok: true, signature: parked.signature };\n }\n\n /**\n * Re-enters a tool whose sub-agent asked the user something.\n *\n * From the top, with `ctx.resumed === true` — there is no other way. A JS\n * async generator cannot be suspended across a turn boundary, so the body\n * runs again and `runAgent` replays its finished sub-runs out of\n * `ToolCallPart.nested` instead of re-running them. Which means the code\n * *before* the escalating `runAgent` runs twice; that bargain is documented on\n * `ToolContext.runAgent` and it is the price of not needing a checkpoint API.\n */\n private async reenter(\n entry: { message: AgentMessage; call: ToolCallPart },\n answers: ClientToolResult[],\n escalated: PendingToolCall[],\n ): Promise<ToolResultPart | null> {\n const name = String(entry.call.name);\n const resolved = this.config.registry.get(name);\n if (!resolved || !resolved.tool.execute) {\n return {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message: `The tool \"${name}\" no longer exists, so the sub-agent's question cannot be delivered.`,\n toolCallId: entry.call.toolCallId,\n retryable: false,\n },\n };\n }\n\n // Checked the way `runTools` checked the model's arguments. The record's\n // signature has already said this is the input the tool parked on, so what\n // this catches is the tool itself having moved: a schema that changed\n // between the turn that parked and the turn that answers. The tool's typed\n // input is a contract with the tool as it is now, and a value the schema\n // rejects must not reach it — the failure is a result the model can read,\n // exactly like a mis-typed argument on the way in.\n const parsed = resolved.tool.inputSchema.safeParse(entry.call.input);\n if (parsed.ok === false) {\n return {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_input\",\n message: `Invalid arguments for \"${name}\": ${parsed.errors.join(\", \")}`,\n toolCallId: entry.call.toolCallId,\n retryable: true,\n },\n };\n }\n\n const call = this.amendCall(entry);\n // The parsed value is the only input the call has from here on, as in\n // `runTools` — the clone carries what the tool was actually given.\n call.input = parsed.value;\n try {\n // Step 0, like an approval executed on the way in: this belongs to the\n // turn, not to a step of the loop that has not started yet.\n return await raceAbort(\n this.executeTool(resolved, entry.message.id, call, parsed.value, 0, { answers }),\n this.controller.signal,\n );\n } catch (error) {\n if (error instanceof PendingEscalation) {\n // Asked again. No result attaches, so the call stays open and the next\n // turn re-enters it exactly as this one did.\n escalated.push(...error.pending);\n return null;\n }\n throw error;\n }\n }\n\n /**\n * Clones the tool-call part before a replay writes to its `nested`.\n *\n * The message holding it came from an earlier run and belongs to the caller's\n * `messages` array; the run must not reach back into its own input and change\n * it under a controller that has already persisted it. Cloning the part into\n * the amended copy is also what makes the updated sub-run transcript\n * something `onMessage` can report — see `reportDeferred`, which is why the\n * message is marked unreported here even though no result may ever attach.\n */\n private amendCall(entry: { message: AgentMessage; call: ToolCallPart }): ToolCallPart {\n const message = this.amend(entry.message);\n const at = message.content.indexOf(entry.call);\n const call: ToolCallPart = {\n ...entry.call,\n nested: (entry.call.nested ?? []).map((run) => ({ ...run })),\n // Cloned for the same reason `nested` is: the replayed body's memo writes\n // into this array, and the array on the original part belongs to the\n // caller's `messages`, which is an input and not scratch space.\n ...(entry.call.attachments\n ? { attachments: entry.call.attachments.map((record) => ({ ...record })) }\n : {}),\n };\n if (at >= 0) message.content[at] = call;\n this.unreported.add(message);\n return call;\n }\n\n /** Tool calls in the history with no result anywhere after them. */\n private openCalls(): { message: AgentMessage; call: ToolCallPart }[] {\n const resolvedIds = new Set<string>();\n for (const message of this.history) {\n for (const part of message.content) {\n if (part.type === \"tool-result\") resolvedIds.add(part.toolCallId);\n }\n }\n const open: { message: AgentMessage; call: ToolCallPart }[] = [];\n for (const message of this.history) {\n for (const part of message.content) {\n if (part.type === \"tool-call\" && !resolvedIds.has(part.toolCallId)) {\n open.push({ message, call: part });\n }\n }\n }\n return open;\n }\n\n /**\n * Verifies one answer and turns it into a result part, or reports why not.\n *\n * `null` means the call stays unanswered and falls through to the implicit\n * denial above — which is the right outcome for a bad signature: the model\n * must not see a result the server cannot vouch for. `\"open\"` means the\n * opposite: the answer was good, the tool ran, and it is now waiting on a\n * question of its own, so the call must stay open *without* being denied.\n */\n private async resolveAnswer(\n entry: { message: AgentMessage; call: ToolCallPart },\n answer: ClientToolResult,\n escalated: PendingToolCall[],\n ): Promise<ToolResultPart | \"open\" | null> {\n const call = entry.call;\n const name = String(call.name);\n const resolved = this.config.registry.get(name);\n const reject = (message: string) => {\n this.emit({\n type: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message,\n toolCallId: call.toolCallId,\n retryable: false,\n },\n });\n return null;\n };\n\n if (!resolved) {\n return reject(`The tool \"${name}\" no longer exists, so its answer cannot be checked.`);\n }\n const kind = pendingKind(resolved.tool);\n if (!kind) {\n return reject(`\"${name}\" is a server tool with no pending question.`);\n }\n if (typeof answer.signature !== \"string\" || answer.signature.length === 0) {\n return reject(`The answer for \"${name}\" carried no signature.`);\n }\n\n // The issuing run, read out of the token. The turn answering a pending call\n // is a *new* run with a new id, so the id the signature was made under has\n // to travel with the signature — and it is covered by the MAC, so a client\n // that edits it fails below rather than being believed.\n const issued = readSignature(answer.signature);\n if (!issued) {\n return reject(`The signature for \"${name}\" is malformed.`);\n }\n\n // The path is this run's own, not the one the client sent. The client's\n // copy was used to route the answer here and nothing else; recomputing the\n // MAC over what the server issued is what makes a moved answer fail instead\n // of being believed.\n const verified = verifyPendingCall(answer.signature, {\n ...this.claimsFor(call.toolCallId, name, kind, call.input),\n runId: issued.runId,\n });\n\n if (verified.ok === false) {\n return reject(\n verified.reason === \"expired\"\n ? `The approval for \"${name}\" has expired. Ask again.`\n : `The answer for \"${name}\" does not match the call the server made.`,\n );\n }\n\n // Verifying says the server once asked this exact question; spending the\n // nonce says nobody has answered it yet. Without this step a captured token\n // approves the same call every time it is presented — the client rewinds to\n // the history from before the result existed and replays, and the human who\n // approved once has approved forever.\n if (!consumePendingCall(answer.signature)) {\n return reject(`The answer for \"${name}\" has already been used. Ask again.`);\n }\n\n if (\"approve\" in answer) {\n if (kind !== \"approval\") {\n return reject(`\"${name}\" is answered by the client, not approved.`);\n }\n if (answer.approve === true) {\n // Raced against the abort signal exactly as `runTools` does. A tool\n // that does not honour `ctx.signal` must not be able to hold the run\n // open, and this is the one execution outside the loop — a `stop()`\n // landing here used to hang the run forever, which is precisely the\n // dangling state `stop()` exists to prevent.\n //\n // Step 0: the approval landed before this run took its first model\n // step, so it belongs to no step of this loop. `call.input` is the\n // value the signature covers — see `runTools`.\n //\n // Cloned first, for the same reason `reenter` clones: an approved tool\n // may nest, and `nestedRunner` writes `nested` onto the part it is\n // given. That part belongs to the caller's `messages` array, which is\n // an input and not scratch space — and the clone is what makes the\n // sub-run transcript something `onMessage` can report.\n const executing = this.amendCall(entry);\n try {\n return await raceAbort(\n this.executeTool(resolved, entry.message.id, executing, executing.input, 0),\n this.controller.signal,\n );\n } catch (error) {\n if (error instanceof PendingEscalation) {\n // An approval whose tool asked the user something of its own. The\n // step loop and `reenter` both collect this; without the same catch\n // here the rejection left `ingestTurn` and was normalized into a\n // `provider_error`, which ended the run with the sub-agent's\n // question thrown away and the approval's nonce already spent.\n escalated.push(...error.pending);\n return \"open\";\n }\n throw error;\n }\n }\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"denied\",\n cause: \"refused\",\n reason: answer.reason,\n };\n }\n\n if (\"output\" in answer) {\n if (kind === \"approval\") {\n // An approval is a tool the *server* runs. A client handing back its\n // output would be fabricating a result, not approving one.\n return reject(`\"${name}\" is approved, not answered: the server produces its result.`);\n }\n const schema = resolved.tool.outputSchema;\n const parsed = schema\n ? schema.safeParse(answer.output)\n : { ok: true as const, value: answer.output };\n if (parsed.ok === false) {\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message: `The answer for \"${name}\" did not match its output schema: ${parsed.errors.join(\", \")}`,\n toolCallId: call.toolCallId,\n retryable: true,\n },\n };\n }\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"ok\",\n output: parsed.value,\n };\n }\n\n return reject(`The answer for \"${name}\" carried neither an approval nor an output.`);\n }\n\n /**\n * Puts the result next to the call that asked for it.\n *\n * The message being amended came from an earlier run, so it is cloned before\n * it is touched — the caller's `messages` array is an input, not scratch\n * space, and a controller that persisted it would otherwise see it change\n * under it. The clone is reported through `onMessage` and returned in\n * `result()`, which is why a store keyed by message id has to upsert.\n */\n private attachToHistory(\n entry: { message: AgentMessage; call: ToolCallPart },\n result: ToolResultPart,\n ) {\n const message = this.amend(entry.message);\n message.content.push(result);\n this.unreported.add(message);\n this.emit({ type: \"tool-result\", messageId: message.id, part: result });\n }\n\n /** The clone of an earlier run's message that this run may write to. One per\n * message id, so two results for the same message do not fork it. */\n private amend(original: AgentMessage): AgentMessage {\n const existing = this.amended.get(original.id);\n if (existing) return existing;\n const message: AgentMessage = { ...original, content: [...original.content] };\n const index = this.history.indexOf(original);\n if (index >= 0) this.history[index] = message;\n this.produced.push(message);\n this.amended.set(original.id, message);\n return message;\n }\n\n /**\n * Persists the messages this run finished outside `finalizeMessage`, once each\n * and in the order it finished them.\n *\n * Called from the end of `ingestTurn`, from the abort path, and from\n * `finalizeMessage`. From the abort path because a stop that lands while an\n * approved tool is running has to persist the results that *did* attach this\n * turn — otherwise the work is done, the transcript on the stream shows it,\n * and the store never hears about it. From `finalizeMessage` because an\n * injected message has to reach `onMessage` after the assistant message it\n * sits below, and a `Set` preserves insertion order, which here is transcript\n * order.\n */\n private async reportDeferred(): Promise<void> {\n const pending = [...this.unreported];\n this.unreported.clear();\n for (const message of pending) await this.report(message);\n }\n\n // --- stopping ----------------------------------------------------------\n\n /**\n * The run's last act: leave a transcript the next turn can be built on.\n *\n * Everything the model asked for and did not get becomes a `denied` result\n * with `cause: \"stopped\"`, and the interrupted message is finalized as\n * `aborted` keeping whatever text it had produced. Both go out on the stream\n * and through `onMessage`. A cancel that merely stopped emitting would leave\n * a dangling tool call, and the provider would reject the history on the very\n * next message the user sent.\n */\n private async finalizeAborted(): Promise<void> {\n // Sub-runs first. They share this run's signal so they are already closing;\n // what is being waited for is the moment each writes its transcript onto\n // its tool call, because everything below this line persists messages.\n if (this.nestedSettling.size > 0) {\n await Promise.all([...this.nestedSettling]);\n }\n // Calls left open anywhere in the history, not just on the message this run\n // was building. A stop that lands while an approval this turn is executing\n // has no current message at all — the call belongs to an *earlier* turn's\n // message — and denying only `this.current` would leave that one dangling\n // in the very transcript this method exists to keep valid.\n for (const entry of this.openCalls()) {\n if (entry.message === this.current) continue;\n this.attachToHistory(entry, {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"denied\",\n cause: \"stopped\",\n reason: this.stopReason,\n });\n }\n // Results that did attach this turn have not been persisted yet: the report\n // pass at the end of `ingestTurn` is one of the things the abort skipped.\n await this.reportDeferred();\n\n const message = this.current;\n if (message) {\n const answered = new Set(\n message.content\n .filter((part): part is ToolResultPart => part.type === \"tool-result\")\n .map((part) => part.toolCallId),\n );\n for (const part of [...message.content]) {\n if (part.type !== \"tool-call\" || answered.has(part.toolCallId)) continue;\n // A call whose arguments were still arriving is finalized too: the\n // client has already been shown it, and an unresolved part is exactly\n // what this method exists to prevent.\n delete part.partial;\n this.addResult(message, {\n type: \"tool-result\",\n toolCallId: part.toolCallId,\n name: part.name,\n status: \"denied\",\n cause: \"stopped\",\n reason: this.stopReason,\n });\n }\n }\n this.finishReason = \"aborted\";\n await this.finalizeMessage(\"aborted\");\n }\n}\n\nfunction pendingKind(tool: AnyAgentTool): \"approval\" | \"question\" | \"client\" | null {\n if (tool.answeredBy === \"client\") {\n // A question is a client tool whose input is the prompt itself. The\n // distinction is for the UI — one renders a dialog, the other runs code —\n // and it costs nothing to carry.\n return tool.inputSchema === questionSchema ? \"question\" : \"client\";\n }\n return tool.requiresApproval ? \"approval\" : null;\n}\n\nfunction parseArgs(args: string): any {\n if (!args || !args.trim()) return {};\n try {\n return JSON.parse(args);\n } catch {\n return args;\n }\n}\n\n/**\n * Resolves a string built by repeated concatenation, in place.\n *\n * `text = text + delta`, run once per streamed token, does not build a string —\n * it builds a rope: a tree of pointers to every fragment, which the engine\n * flattens only when something needs the characters contiguously. A message\n * that nothing reads before it is persisted therefore keeps all of its\n * fragments alive, and the tree costs several times the text.\n *\n * Measured on Bun 1.x, 600 deltas of six characters (a ~450-token answer, 3.5 KB\n * of ASCII): held as a rope, 18.7 KB. Resolved, 3.5 KB — half the UTF-16 size,\n * because a flat ASCII string is stored one byte per character and a rope\n * cannot be. That is 5.3x, and it is paid by every message a store keeps and\n * every run the live registry holds.\n *\n * Indexing is what forces the resolution: `text[0]` cannot be answered without\n * the characters, so the engine collapses the tree and drops the fragments.\n * Nothing is allocated and nothing is copied, which is why this is not\n * `split(\"\").join(\"\")` — that measures the same but allocates one string per\n * character to get there.\n *\n * DO NOT DELETE THIS AS A NO-OP. It reads like one and it is not; the value is\n * the side effect on the receiver. If a future engine does not resolve on\n * index, this silently becomes a real no-op and memory returns to what it is\n * today — a safe failure, which is why it is written as a hint rather than as a\n * round trip through an encoder that would also mangle a lone surrogate.\n */\nfunction resolveRope(text: string): string {\n if (text.length > 0) void text[0];\n return text;\n}\n\nfunction appendText(message: AgentMessage, type: \"text\" | \"reasoning\", delta: string) {\n const last = message.content[message.content.length - 1];\n if (last && last.type === type) {\n (last as { text?: string }).text = ((last as { text?: string }).text ?? \"\") + delta;\n return;\n }\n message.content.push(\n type === \"text\" ? { type: \"text\", text: delta } : { type: \"reasoning\", text: delta },\n );\n}\n\n/**\n * Reasoning is accumulated per ITEM, not per message, and the item's id is kept.\n *\n * This used to go through `appendText`, which merges on the part *type* alone\n * and has nowhere to put an id. Both halves of that were wrong and neither was\n * visible in the transcript:\n *\n * - `request.ts` drops a reasoning item with no id, deliberately — the id is\n * the API's handle on the stored reasoning and a fabricated one would look\n * like continuity that is not there. So an id dropped here meant reasoning\n * was never sent back at all: on a two-step run the model re-derived its\n * own argument from nothing, and the prompt cache (which keys on the\n * literal item) missed every time. Measured against the live Responses API\n * in `live/live.test.ts`: the second call's input carried zero reasoning\n * items.\n * - a step that produces two reasoning items was flattening them into one\n * part, so even with an id there would have been one id for two items'\n * text.\n *\n * A part with no id is still appended rather than dropped: the text is what a\n * UI renders, and a provider that reports no item id (Azure does not always)\n * should still show its thinking. It just cannot be echoed back, which is the\n * bargain `reasoningItem` already documents.\n */\nfunction appendReasoning(message: AgentMessage, id: string | undefined, delta: string) {\n const last = message.content[message.content.length - 1];\n if (last && last.type === \"reasoning\" && last.id === id) {\n last.text = (last.text ?? \"\") + delta;\n return;\n }\n message.content.push(\n id ? { type: \"reasoning\", id, text: delta } : { type: \"reasoning\", text: delta },\n );\n}\n",
8
+ "import type { HttpRequest } from \"../http\";\nimport type { AgentProvider, ProviderToolNamespace, ProviderToolSpec } from \"./AgentProvider\";\nimport type { GeneratedImage, GenerateImageParams, ImageInput, ImageModel } from \"./ImageModel\";\nimport { supportsStrict } from \"./Schema\";\nimport type { Infer, Schema } from \"./Schema\";\nimport {\n consumeNestedRun,\n consumePendingCall,\n readSignature,\n signNestedRun,\n signPendingCall,\n verifyNestedRun,\n verifyPendingCall,\n} from \"./signing\";\nimport type {\n Attachment,\n PutAttachmentParams,\n ScopedAttachments,\n ToolAttachmentPut,\n ToolAttachmentRecord,\n ToolAttachments,\n} from \"./store/Attachments\";\nimport { ATTACHMENT_ID_PREFIX, InvalidAttachmentScopeError } from \"./store/Attachments\";\nimport { sseKeepalive } from \"./store/sse\";\nimport type {\n AgentError,\n AgentMessage,\n AgentStreamEvent,\n AgentStreamFrame,\n ClientToolResult,\n ClientTurn,\n FilePart,\n FinishReason,\n NestedRun,\n PendingToolCall,\n ToolCallPart,\n ToolResultPart,\n ToolShapes,\n Usage,\n} from \"./types\";\n\n// --- tools ---------------------------------------------------------------\n\n/**\n * Everything a tool needs from the request it is running inside.\n *\n * Tools are created once at module scope and shared by every request, so they\n * cannot close over a user or an abort signal — and anything mutable stored on\n * the tool itself would leak across requests. That is why the run's state\n * arrives as an argument instead: the tool stays a singleton and the context is\n * per call.\n */\nexport interface ToolContext {\n req: HttpRequest<any, any>;\n /**\n * The app's own fields from the turn's request body — what `useChat`'s\n * `body` option sent, minus the keys the turn envelope owns.\n *\n * Here rather than behind `ctx.req.input()`, and not because that would be\n * inconvenient: `AgentController` has already consumed the body, and a run\n * outlives the request anyway, so by the time a tool executes there is\n * nothing left to read. The values are copied onto the run when it starts.\n *\n * `{}` for a run started with none. Untyped on purpose — a tool is a\n * module-scope singleton that any controller may mount, so there is no one\n * `Body` for it to be. The controller that declares one gets it typed in\n * `instructions()`; a tool validates, the way it would any other input it\n * did not define.\n *\n * CLIENT-CONTROLLED. Same trust as a request body: fine to read, not a\n * finding about who the user is. An id from here says which record the\n * client wants, never that it may have it.\n */\n body: Record<string, unknown>;\n runId: string;\n threadId?: string;\n toolCallId: string;\n /**\n * Aborted when the user calls `stop()`. Not when the connection drops — a run\n * outlives the request that started it so a refresh can reattach, which means\n * a disconnect is no longer a signal to stop working.\n */\n signal: AbortSignal;\n /** Which step of the tool loop this is, starting at 1. */\n step: number;\n /**\n * How deep this tool is inside nested runs: 0 at the top, 1 inside a tool of\n * an agent started by `runAgent`, and so on. Compared against `maxDepth` on\n * `Agent.create` so a cycle — agent A with a tool that runs agent A — fails\n * with a sentence to read instead of exhausting the stack.\n */\n readonly depth: number;\n /**\n * True when this tool is being re-entered after a sub-agent it started asked\n * the user something and the user answered.\n *\n * READ THE `runAgent` NOTE BEFORE USING IT. This is the flag that lets a tool\n * tell a first attempt from a replay, and it exists because there is nothing\n * to tell it otherwise: the tool body ran once already.\n */\n readonly resumed: boolean;\n /**\n * Files, both directions.\n *\n * WHAT THIS FIXES. Before it, files travelled one way: a user's upload became\n * a provider file id the model could look at, and a tool that *produced*\n * something — an edited image, a rendered chart — had a string to return and\n * nowhere to put the bytes. `put(blob)` parks them and answers a record whose\n * `id` is the handle everything else uses; `put(blob, { showModel: true })`\n * also sends them to the provider and puts them in front of the model as an\n * input-role message once this tool call settles, which is what makes\n * generate → look → fix a loop rather than a one-way report.\n *\n * SCOPED, AND THAT IS THE WHOLE SECURITY STORY. The object is built from the\n * `ScopedAttachments` the controller resolved for *this request* (see\n * `AgentController.attachmentScope`), and none of its methods takes a scope,\n * so there is nothing for a tool to pass the model's arguments into. An id\n * that came from another user resolves to `AttachmentNotFoundError`, with the\n * same wording an unknown id gets.\n *\n * PER TOOL CALL, LIKE `runAgent`, AND FOR THE SAME REASON. `put` is memoized\n * by call index within the tool call, so a tool that escalates and is\n * re-entered does not store, upload or inject a second time. Read the note on\n * `ToolAttachments.put` before writing a body whose `put` calls sit in a\n * branch.\n *\n * ALWAYS PRESENT, NEVER NULL. A request with no attachment scope — an\n * unauthenticated, thread-less chat — gets an object whose every method\n * throws with a sentence naming `attachmentScope()`. A nullable `ctx`\n * member would be a guard every tool has to remember and most would not,\n * and the failure of forgetting is a `TypeError` in a tool body rather than\n * an explanation.\n */\n attachments: ToolAttachments;\n /**\n * What the user sent with the turn this tool call answers. See `ToolTurn`.\n */\n readonly turn: ToolTurn;\n /**\n * Runs another agent from inside this tool, wired into the parent run.\n *\n * A tool can already drive a sub-agent by hand — make one, iterate it, yield\n * its events as progress. What this does that hand-rolling cannot is join the\n * two runs: the sub-run inherits `ctx.signal` so the parent's `stop()` reaches\n * it; every sub-run event is re-emitted on the parent stream as\n * `nested-event`, numbered in the parent's `seq`, so `/attach` replay stays\n * correct through the nesting; the sub-run's usage rolls into the parent's;\n * its transcript is recorded on the parent's `ToolCallPart.nested`; and the\n * depth and agent-name chain travel with it, so a cycle fails fast.\n *\n * ESCALATION. If the sub-run ends `awaiting-input` — it has an approval tool,\n * or it asked a question — `onPending: \"escalate\"` (the default) throws a\n * `PendingEscalation` carrying the inner pending calls, which the parent run\n * collects exactly like pending calls of its own: the parent ends\n * `awaiting-input` with the sub-agent's questions in its list, and the client\n * answers them with the same `approve()` / `answer()` it uses for any other.\n * `onPending: \"deny\"` refuses them instead and lets the sub-run finish.\n *\n * THE COST, WHICH IS REAL AND WHICH YOU MUST DESIGN AROUND. A JS async\n * generator cannot be suspended across a turn boundary: `awaiting-input` is\n * terminal for the stream, the next turn re-enters the loop at the top and\n * rebuilds its state from the message history, and a paused generator is not\n * in that history and cannot be put there. So an escalating tool is\n * RE-ENTERED FROM THE TOP on the next turn, with `ctx.resumed === true`, and\n * `runAgent` is memoized by call index within the tool call: the Nth\n * `runAgent` of a tool call that already completed on an earlier turn returns\n * its persisted result immediately, calling no provider and running no\n * sub-tool, and only the sub-run that escalated actually continues.\n *\n * The index is the only key there is, so a body whose `runAgent` calls sit in\n * a branch or a loop can produce a different sequence on the replay and make\n * index N mean two different things. That is checked, not trusted: a mismatch\n * fails the tool call with a sentence naming both sub-runs, because pairing a\n * user's answer with a sub-run they never saw would be invisible.\n *\n * Which means: CODE BEFORE AN ESCALATING `runAgent` RUNS AGAIN ON RESUME.\n * Side effects there are repeated. Put your side effects after the\n * `runAgent`, or make them idempotent, or branch on `ctx.resumed`. This is\n * inherent to replay and it is the same bargain the outer tool loop already\n * makes; it is written here in plain words rather than solved with a\n * checkpoint API, because that is a much larger feature than this one.\n */\n runAgent<A extends AnyAgent>(agent: A, params?: RunAgentParams): Promise<NestedRunResult>;\n\n /**\n * Renders an image and parks it, in one step, wired into this tool call.\n *\n * THE SPELLING DIFFERS FROM `ImageModel.generate` ON PURPOSE, and the reason\n * is the same one behind `ctx.runAgent`: an escalating tool is re-entered\n * from the top on the next turn, so a render reached by a bare\n * `model.generate()` inside a tool body is paid for again on every replay —\n * two minutes and an invoice line, for an image whose id the model has\n * already read. This memoizes the render against the tool call, exactly as\n * `ctx.attachments.put` memoizes a store.\n *\n * IT ANSWERS AN `Attachment`, NOT BYTES, and that is forced rather than\n * chosen: the memo lives in the message history, so it cannot hold an image.\n * The bytes are in the app's Storage, which is where a generated image was\n * going anyway; `ctx.attachments.file(id)` hands them back, and copying them\n * under a key of the app's own is what makes the image outlive the run.\n *\n * `showModel: true` puts the result in front of the model as an input-role\n * message once this call settles, so an agent can look at what it made and\n * try again.\n */\n generateImage(model: ImageModel, params: ToolGenerateImageParams): Promise<GeneratedAttachment>;\n\n /**\n * Edits images and parks the result. Everything above applies.\n *\n * `images` takes attachment ids as well as bytes, and the ids are the point:\n * the image to change is usually the one the model just named (\"make that one\n * warmer\"), so it arrives with exactly the trust of a request body. Ids are\n * resolved through this call's `ctx.attachments`, which applies #489's scope\n * checks; there is no spelling here that reaches an id outside them.\n */\n editImage(model: ImageModel, params: ToolEditImageParams): Promise<GeneratedAttachment>;\n}\n\n/** What an image parked by a tool answers. See `ToolContext.generateImage`. */\nexport type GeneratedAttachment = {\n attachment: Attachment;\n /** The pixel dimensions the model produced, read off the response. */\n size: string;\n mimeType: string;\n usage: Usage;\n};\n\ntype ToolImageSettings = Omit<GenerateImageParams, \"prompt\" | \"signal\">;\n\nexport type ToolGenerateImageParams = ToolImageSettings & {\n prompt: string;\n /** The filename to store it under. Defaults to `<model name>.<extension>`. */\n name?: string;\n /** Also show the model its own output, once this tool call settles. */\n showModel?: boolean;\n};\n\nexport type ToolEditImageParams = ToolGenerateImageParams & {\n /** Attachment ids, or raw bytes. At least one. */\n images: [ImageInput | string, ...(ImageInput | string)[]];\n mask?: ImageInput | string;\n};\n\n/**\n * The files of the turn a tool call belongs to, as data the model cannot write.\n *\n * WHAT THIS FIXES. Every method on `ctx.attachments` takes an id, and before\n * this the only ids a tool could get were the ones the model put in its\n * arguments. So a tool meant to use \"the image the user attached\" had to trust\n * the model to name it, and a prompt-injected document could name a different\n * upload of the same user's — yesterday's contract instead of today's photo —\n * which the scope check passes, because it is the user's file. Reading the ids\n * from here instead leaves the model nothing to steer.\n *\n * WHICH TURN: THE USER MESSAGE BEFORE THIS CALL, NOT THE LATEST ONE. Found by\n * walking back from the assistant message that made the call, never by taking\n * the last user message in the history when the tool runs, and the difference\n * is re-entry. An escalating tool runs again from the top on a later turn (see\n * `runAgent`), and \"latest\" is recomputed each time: a turn that answers the\n * sub-agent can carry text or files of its own, and when the re-entered tool\n * asks again, that turn's user message is appended *after* the still-open call\n * — so on the next re-entry \"latest\" is the answer turn, and a tool that read\n * \"the image\" would pick a different file, or none, on its second attempt. The\n * replay checks do not reliably catch that: `putMismatch` compares the media\n * type and showModel, never the bytes, and a sub-run seeded with the file as\n * a message part fingerprints as `<file>` whichever file it was. The message\n * that preceded the call is in the history on every attempt and does not\n * move, so every attempt sees the same list.\n *\n * ONE TURN, NOT THE THREAD. The whole thread is what \"the image I sent earlier\"\n * needs, and it is the wider thing to bind to: a tool that picks \"the first\n * image\" out of forty turns picks whichever one the history happens to put\n * first, and the user who uploaded a new one sees the old one used. A tool that\n * wants earlier turns can take an id from the model and let the scope check it;\n * one that binds should bind to the turn the user is looking at. A turn with\n * text and no files gives an empty list, which is also the answer to \"the user\n * attached nothing this time\".\n *\n * NOT THE FILES A TOOL MADE. A file a tool showed with `put(…, { showModel:\n * true })` is injected as a user-role message and carries an `attachmentId`\n * too, and taking it would make it \"the user's upload\" to the next tool — so\n * the walk steps over every message a tool call's record names as injected,\n * the same test `historyForProvider` uses. A tool that wants what an earlier\n * tool produced has that tool's result to read the id from.\n *\n * IDS ONLY, AND NOT TRUSTED. The name and type beside them on the `FilePart`\n * are what the client said, so they are left off rather than offered as a\n * second, weaker copy of what `ctx.attachments.get(id)` answers from the\n * store. And the ids themselves are only as trustworthy as the history they\n * came from — which, for a stateless client, is whatever it posted. That is\n * fine for the reason everything else about attachments is fine: resolution\n * goes through `ctx.attachments`, whose scope was fixed by the request, so an\n * id from somebody else's upload answers `AttachmentNotFoundError` here\n * exactly as it would from the model's arguments. This list narrows which of\n * the caller's own files a tool reaches for; it grants nothing.\n *\n * Inside a sub-run the turn is the sub-run's own: the message its parent's\n * tool started it with, which has files only if that tool put them there. The\n * parent's upload is not inherited — the tool that called `runAgent` read its\n * own `ctx.turn` and decided what the sub-agent is given.\n */\nexport interface ToolTurn {\n /**\n * The `attachmentId`s on that user message's file parts, in order, without\n * duplicates, and only ids of gemi's own shape. Frozen, and computed once\n * before the body runs.\n */\n readonly attachments: readonly string[];\n}\n\n/** What `ctx.runAgent` is given. `messages` and `prompt` are alternatives. */\nexport interface RunAgentParams {\n /** Prior turns for the sub-agent. Starts empty when omitted. */\n messages?: AgentMessage[];\n /** Sugar for a single user turn — the common case, and the whole message\n * list when there is no sub-conversation to continue. */\n prompt?: string;\n /** Appended to the sub-agent's own `instructions`, for this run only. */\n instructions?: string;\n /** Shown on the nested transcript, e.g. \"researching pricing\". */\n label?: string;\n /**\n * What to do when the sub-run ends `awaiting-input`. `\"escalate\"` (the\n * default) throws `PendingEscalation` so the question reaches the user;\n * `\"deny\"` refuses every pending call and lets the sub-run finish, which is\n * what a tool wants when the sub-agent is meant to be autonomous.\n *\n * `\"deny\"` is refused *in place*, inside the sub-run's own loop, so the\n * sub-agent is told it cannot ask and takes another step rather than ending\n * parked — and it is inherited by everything below, so a grandchild asking to\n * escalate is overruled too. A promise that nothing from this subtree reaches\n * the user is only worth making if the whole subtree keeps it.\n */\n onPending?: \"escalate\" | \"deny\";\n /**\n * Override the sub-agent's own ceiling for this run.\n *\n * Not inherited from the parent. A sub-agent is a different job with a\n * different output size — a generator writing a document against a router\n * answering one word — and silently handing down the caller's ceiling would\n * either strangle the one or fail to bound the other.\n */\n maxOutputTokens?: number;\n temperature?: number;\n}\n\n/**\n * What a completed sub-run gives back.\n *\n * `nested` is the transcript as it is recorded on the parent's tool-call part,\n * so a tool that wants to summarize what its sub-agent did reads the same\n * object the UI renders rather than a second representation of it.\n */\nexport interface NestedRunResult<O = unknown> {\n runId: string;\n /** The sub-agent's name — carried so a caller that fans out over several\n * agents can tell the results apart without tracking the order. */\n agent: string;\n messages: AgentMessage[];\n finishReason: FinishReason;\n usage: Usage;\n /** Set when the sub-agent declares an `output` schema and the run finished. */\n output?: O;\n /** The record written to the parent's `ToolCallPart.nested`. */\n nested: NestedRun;\n}\n\n/**\n * Thrown by `ctx.runAgent` when a sub-run ends `awaiting-input`.\n *\n * An exception rather than a return value because it must not be mistaken for\n * an answer: a tool that ignored an `{ escalated: true }` field would return a\n * result to the model as if the sub-agent had finished, and the model would act\n * on an answer nobody gave. `executeTool` lets this one propagate instead of\n * turning it into a `tool_error`, and the step loop collects `pending` exactly\n * like the pending calls it produced itself.\n *\n * `path` is the chain of tool-call ids down to the escalating call; each entry\n * of `pending` already carries its own full path, and this is the prefix they\n * share.\n */\nexport class PendingEscalation extends Error {\n readonly pending: PendingToolCall[];\n readonly path: string[];\n /** The sub-run that parked, so the parent can record its transcript before\n * ending the turn — an escalation is a pause, not a lost run. */\n readonly nested: NestedRun;\n\n constructor(params: { pending: PendingToolCall[]; path: string[]; nested: NestedRun }) {\n super(`A nested agent run is waiting on the user for ${params.pending.length} tool call(s).`);\n this.name = \"PendingEscalation\";\n this.pending = params.pending;\n this.path = params.path;\n this.nested = params.nested;\n }\n}\n\n/**\n * A tool either resolves once, or yields progress and then returns.\n *\n * The generator form exists because a tool that takes twenty seconds is the\n * normal case, not the exotic one, and a chat UI that shows nothing for twenty\n * seconds looks broken. Yields become `tool-progress` events; the return value\n * is the result the model sees.\n */\nexport type ToolExecute<Input, Output, Progress = unknown> = (\n input: Input,\n ctx: ToolContext,\n) => Promise<Output> | AsyncGenerator<Progress, Output, void>;\n\ntype ToolDefinitionBase<Name extends string, Input, Output> = {\n name: Name;\n /** The model's only description of when to reach for this. */\n description: string;\n inputSchema: Schema<Input>;\n /**\n * Optional for a server tool, required for a client one — there it is what\n * the answer is validated against before the model sees it, and what types\n * the value the browser has to produce.\n */\n outputSchema?: Schema<Output>;\n /**\n * Withholds this tool's parameter schema from the request: the model is shown\n * only the name and description, and pulls the rest in with the provider's\n * `tool_search` when it decides it wants the tool (`defer_loading` on the\n * wire).\n *\n * It says nothing about who runs the tool or when — it is a statement about\n * the prompt, not about execution. What it buys is context: an agent with\n * forty tools spends most of its prompt on schemas for tools it will not\n * call, and deferred ones load at the end of the window, so adding one\n * mid-conversation does not invalidate the cache.\n *\n * Purely an optimization, and gemi treats it as one: a provider that cannot\n * do tool search is sent the schemas inline, and the agent behaves the same.\n * So it is safe to set on a model that does not support it, and worth setting\n * only for tools that are large, numerous, or rarely reached.\n */\n deferred?: boolean;\n};\n\n/**\n * Two ways a tool's result comes to exist, and neither changes the shape of the\n * conversation.\n *\n * `execute` — the server runs it.\n * `answeredBy: \"client\"` — the browser produces the result: a question for the\n * user, or something only the page can do. The stream ends `awaiting-input`\n * and the answer arrives as an ordinary turn.\n *\n * `requiresApproval` applies to the first: the server can run the tool, but\n * asks first. That, too, ends the stream `awaiting-input`, which is the whole\n * reason there is no second endpoint — an approval is a question whose answer\n * happens to be yes or no.\n */\nexport type ToolDefinition<Name extends string, Input, Output, Progress = never> =\n | (ToolDefinitionBase<Name, Input, Output> & {\n answeredBy?: \"server\";\n execute: ToolExecute<Input, Output, Progress>;\n requiresApproval?: boolean;\n })\n | (ToolDefinitionBase<Name, Input, Output> & {\n answeredBy: \"client\";\n outputSchema: Schema<Output>;\n execute?: never;\n /** Meaningless here: the client answering *is* the approval. */\n requiresApproval?: never;\n });\n\n/**\n * `Progress` is inferred, never written down.\n *\n * It comes from the yield type of an `execute` that is an async generator, and\n * from nothing else — a tool that returns a promise gets `never`, which is the\n * honest statement that it cannot yield and is what makes\n * `ToolShapesOf`'s `progress` member safe to emit unconditionally. It is\n * carried as a fourth parameter rather than derived on demand because it has to\n * survive the trip through `ToolNamespace`, `FlattenTools` and `ToolShapesOf`\n * into the browser, and only a type argument does that.\n *\n * Structurally it lives on `execute`, which is optional, and which is also why\n * `AnyAgentTool` must pass `any` here: `Progress` sits covariantly inside\n * `AsyncGenerator<Progress, …>`, so a bound of `never` would make every\n * yielding tool fail the `Extract` in `ToolShapesOf` and silently vanish from\n * the shapes.\n */\nexport class AgentTool<\n Name extends string = string,\n Input = unknown,\n Output = unknown,\n Progress = never,\n> {\n readonly name: Name;\n readonly description: string;\n readonly inputSchema: Schema<Input>;\n readonly outputSchema?: Schema<Output>;\n readonly requiresApproval: boolean;\n readonly deferred: boolean;\n readonly answeredBy: \"server\" | \"client\";\n /**\n * There is deliberately no `namespace` here. A tool is a module-scope\n * singleton, so a field naming its group would hold whichever agent\n * constructed its namespace last and report that to every other one — the\n * same global-effect-from-a-local-declaration that `ToolNamespace.deferred`\n * avoids. Where a tool sits is a property of the agent, and it lives on the\n * agent's `ResolvedTool`.\n */\n readonly execute?: ToolExecute<Input, Output, Progress>;\n\n private constructor(params: ToolDefinition<Name, Input, Output, Progress>) {\n this.name = params.name;\n this.description = params.description;\n this.inputSchema = params.inputSchema;\n this.outputSchema = params.outputSchema;\n this.requiresApproval = params.requiresApproval === true;\n this.deferred = params.deferred === true;\n this.answeredBy = params.answeredBy === \"client\" ? \"client\" : \"server\";\n this.execute = params.execute ?? undefined;\n }\n\n /**\n * `const` on the params is what preserves `name` as a literal, which is what\n * lets the browser discriminate a tool part by name.\n */\n static create<const Name extends string, Input, Output, Progress = never>(\n params: ToolDefinition<Name, Input, Output, Progress>,\n ): AgentTool<Name, Input, Output, Progress> {\n return new AgentTool(params);\n }\n\n /**\n * Sugar for the common client tool: the agent asks the user something and\n * waits. Equivalent to `answeredBy: \"client\"` with an input schema of one\n * prompt field.\n */\n static ask<const Name extends string, Output>(params: {\n name: Name;\n description: string;\n outputSchema: Schema<Output>;\n }): AgentTool<Name, { question: string }, Output> {\n return AgentTool.create({\n name: params.name,\n description: params.description,\n inputSchema: questionSchema,\n outputSchema: params.outputSchema,\n answeredBy: \"client\",\n });\n }\n}\n\n/**\n * The one schema this module owns, rather than one built with `s`.\n *\n * `Schema<T>` carries a phantom property keyed by a symbol `Schema.ts` does not\n * export, so nothing outside that file can produce one without a cast — and\n * reaching for `s` here would make the agent runtime depend on the schema\n * builder for a single hard-coded object. One field, no `describe`, no\n * optionality: the cast is cheaper than the coupling.\n */\nconst questionSchema = {\n toJSONSchema: () => ({\n type: \"object\",\n properties: { question: { type: \"string\", description: \"What to ask the user\" } },\n required: [\"question\"],\n additionalProperties: false as const,\n }),\n parse(value: unknown) {\n const result = questionSchema.safeParse(value);\n if (result.ok === false) throw new Error(result.errors.join(\", \"));\n return result.value;\n },\n safeParse(value: unknown) {\n if (\n typeof value !== \"object\" ||\n value === null ||\n typeof (value as any).question !== \"string\"\n ) {\n return { ok: false as const, errors: [\"question: expected a string\"] };\n }\n return { ok: true as const, value: { question: (value as any).question } };\n },\n} as unknown as Schema<{ question: string }>;\n\nexport type AnyAgentTool = AgentTool<string, any, any, any>;\n\n/**\n * A group of tools the model can search as a unit.\n *\n * The provider's tool search works over namespaces, and the guidance is fewer\n * than ten functions in each — the model looks at a namespace's description to\n * decide whether anything inside is worth loading, so the grouping is part of\n * the prompt, not bookkeeping. A namespace is also the only place a\n * *collection* of tools can be described; on a flat list that sentence has\n * nowhere to go.\n *\n * Tool names stay globally unique within an agent, so the browser still\n * discriminates on `name` alone and the namespace never leaks into the client's\n * types.\n */\nexport class ToolNamespace<\n Name extends string = string,\n T extends readonly AnyAgentTool[] = readonly AnyAgentTool[],\n> {\n readonly name: Name;\n readonly description: string;\n readonly tools: T;\n /**\n * Kept here rather than pushed onto each tool. A tool is a module-scope\n * singleton and may be listed bare as well as inside a group; writing the\n * group's `deferred` onto it would defer it everywhere, which is a global\n * effect from a local declaration.\n */\n readonly deferred: boolean;\n\n private constructor(params: { name: Name; description: string; tools: T; deferred?: boolean }) {\n this.name = params.name;\n this.description = params.description;\n this.tools = params.tools;\n this.deferred = params.deferred === true;\n }\n\n static create<const Name extends string, const T extends readonly AnyAgentTool[]>(params: {\n name: Name;\n /** What the model reads when deciding whether to search inside. */\n description: string;\n tools: T;\n /** Defers every tool in the group, so the whole namespace costs its own\n * description plus one line per tool until something is loaded. */\n deferred?: boolean;\n }): ToolNamespace<Name, T> {\n return new ToolNamespace(params);\n }\n}\n\n/** What an agent's `tools` may hold: tools, or namespaces of them. */\nexport type ToolEntry = AnyAgentTool | ToolNamespace<string, readonly AnyAgentTool[]>;\n\ntype FlattenTools<T extends readonly ToolEntry[]> = T[number] extends infer E\n ? E extends ToolNamespace<any, infer NT>\n ? NT[number]\n : E\n : never;\n\n/**\n * The tool tuple, erased to the payload types the client is allowed to see.\n *\n * `progress` is emitted for every tool, `never` included, rather than only for\n * the ones that can yield. A conditional that dropped the member would make\n * `T[K][\"progress\"]` in `types.ts` resolve differently per tool, and this\n * package compiles with `strict: false` — where `undefined extends T` is true\n * of everything and an optional member is indistinguishable from a required\n * one. Two inference bugs in this module already came from testing a shape\n * under those options and believing the answer (see `OptionalSchema` in\n * `Schema.ts`); an unconditional member has nothing to get wrong.\n */\nexport type ToolShapesOf<T extends readonly ToolEntry[]> = {\n [K in Extract<FlattenTools<T>, AnyAgentTool> as K[\"name\"]]: K extends AgentTool<\n any,\n infer I,\n infer O,\n infer P\n >\n ? { input: I; output: O; progress: P }\n : never;\n};\n\n// --- skills --------------------------------------------------------------\n\n/**\n * A skill is instructions the model can go and fetch.\n *\n * Inlining every skill into the system prompt costs its tokens on every request\n * and gets worse with each skill added. So a skill is lowered to a tool: one\n * zero-parameter function per skill, in a reserved `skills` namespace, whose\n * description is the skill's and whose result is `instructions` plus any\n * `files`. Only those descriptions are prompted, and a skill the model never\n * reaches for costs a line of text.\n *\n * Lowering to a tool rather than to a synthetic `load_skill(name)` dispatcher\n * is the whole trick: discovery is then the same mechanism as everything else\n * the model chooses between, which means it runs on the provider's own\n * tool-selection machinery instead of on a string argument gemi would have to\n * validate, and a skill that is never loaded is a namespace entry rather than a\n * branch in our code. It is also why `deferred` applies here unchanged — with\n * tool search the namespace is searched, and without it the same tools are\n * listed inline, which for zero-parameter functions costs almost nothing.\n */\nexport interface SkillDefinition<Name extends string = string> {\n name: Name;\n /** Read on every request — this is what the model decides to load from. */\n description: string;\n /** A thunk so a large body stays off the startup path and out of memory. */\n instructions: string | (() => string | Promise<string>);\n /** Paths resolved relative to the app root, appended after `instructions`. */\n files?: string[];\n}\n\nexport class Skill<Name extends string = string> {\n readonly name: Name;\n readonly description: string;\n readonly instructions: string | (() => string | Promise<string>);\n readonly files?: string[];\n\n private constructor(params: SkillDefinition<Name>) {\n this.name = params.name;\n this.description = params.description;\n this.instructions = params.instructions;\n this.files = params.files;\n }\n\n static create<const Name extends string>(params: SkillDefinition<Name>): Skill<Name> {\n return new Skill(params);\n }\n}\n\n/** Reserved: a skill is lowered into a namespace of exactly this name. */\nexport const SKILLS_NAMESPACE = \"skills\";\n\nconst SKILLS_NAMESPACE_DESCRIPTION =\n \"Instructions this agent can load on demand. Load the relevant one before acting in the area it covers.\";\n\nconst EMPTY_PARAMETERS = {\n type: \"object\",\n properties: {},\n required: [] as string[],\n additionalProperties: false as const,\n};\n\n// --- agent ---------------------------------------------------------------\n\nexport type ReasoningEffort = \"minimal\" | \"low\" | \"medium\" | \"high\";\n\nexport interface CreateAgentParams<\n T extends readonly ToolEntry[],\n S extends readonly Skill[],\n O extends Schema<any> | undefined,\n> {\n name: string;\n /** The system prompt. Per-request additions belong on the controller, which\n * has the request; this is the part that is the same for everyone. */\n instructions?: string;\n provider: AgentProvider;\n tools?: T;\n /** Lowered into the reserved `skills` namespace — see `Skill`. The name is\n * reserved, so a namespace of your own cannot be called `skills`. */\n skills?: S;\n /**\n * Makes the final assistant turn strict JSON instead of prose. Tool turns are\n * unaffected — only the answer is constrained, which is the only place a\n * schema can apply once there is a tool loop.\n */\n output?: O;\n /** Ends the run with `finishReason: \"max-steps\"` rather than throwing: an\n * agent that loops is a bug to show, not an exception to swallow. */\n maxSteps?: number;\n /**\n * How far `ctx.runAgent` may nest below this agent. Default 3.\n *\n * It is a limit on the *tree*, taken from the run at the root, so raising it\n * on a sub-agent cannot deepen a run it did not start. A cycle is caught by\n * the agent-name chain before this is reached — this is for the mutually\n * recursive shape a name check cannot see, and for the merely runaway one.\n */\n maxDepth?: number;\n reasoning?: ReasoningEffort;\n /**\n * A ceiling on the tokens one model call may produce, passed to the provider\n * as its own `max_output_tokens`.\n *\n * It bounds a degenerate generation. A model asked for non-strict JSON can\n * fall into emitting the same character until something stops it, and with\n * no cap the only thing that does is the client giving up — a turn that\n * never ends rather than one that fails. The cap turns that into a run\n * ending with `finishReason: \"length\"`, which an app can retry.\n *\n * Per model call, not per run: a run of four steps may produce four times\n * this. `maxSteps` is the bound on the run.\n */\n maxOutputTokens?: number;\n /**\n * Passed to the provider unchanged. Lower is steadier, which is worth having\n * for a generation whose shape matters more than its phrasing.\n *\n * SENT WHENEVER SET, and not capability-gated the way `reasoning` is.\n * `ProviderCapabilities` carries no flag for it, so `buildResponsesRequest`\n * writes it whenever it is a number and never drops it. That matters because\n * the newer reasoning models reject the parameter outright: setting this for\n * one is a 400 rather than a quietly degraded request. It is the same bargain\n * `output` takes in `request.ts` — an explicit choice is sent and the API gets\n * to say no, because silently dropping one leaves an app believing something\n * about its request that is not true.\n */\n temperature?: number;\n}\n\n/**\n * One call per client turn — a first message and an answer to a pending\n * approval take the same path, because they are the same thing: the next turn\n * of a conversation.\n */\nexport interface AgentStreamParams {\n /** Prior turns. The controller loads these from its store, or takes what the\n * client sent when running stateless. */\n messages: AgentMessage[];\n /** The client's turn: text, answers to pending calls, or both. */\n turn?: ClientTurn;\n req: HttpRequest<any, any>;\n /** Aborted by an explicit `stop`, not by a disconnect. */\n signal?: AbortSignal;\n runId?: string;\n threadId?: string;\n /** Appended to the agent's own `instructions` for this request only. */\n instructions?: string;\n /**\n * The app's own fields from the turn's request body, handed to every tool of\n * this run as `ctx.body`.\n *\n * Carried on the run rather than left to be read from `ctx.req`: a run\n * outlives the request that started it, so that a refresh can reattach, and\n * by the time a tool executes there is no body left to read. See\n * `AgentController`'s `Body`.\n */\n body?: Record<string, unknown>;\n /** Per-request model choice, e.g. letting a user pick. */\n provider?: AgentProvider;\n maxSteps?: number;\n reasoning?: ReasoningEffort;\n /** Overrides the agent's own for this run. A generation whose size varies by\n * request — a page with ten components against one with two — is the case\n * a fixed ceiling on the agent cannot serve. */\n maxOutputTokens?: number;\n temperature?: number;\n /**\n * Fires once for every message this run completes — the user's turn, each\n * assistant turn, and any earlier message this turn amended by resolving a\n * pending call. It is the controller's persistence point, and it fires\n * whether or not anyone is still reading the stream, which is what makes a\n * run that outlives its request useful.\n *\n * A message may be reported twice across runs under the same id when a\n * pending call is resolved later; a store keyed by id should upsert.\n */\n onMessage?: (message: AgentMessage) => void | Promise<void>;\n /**\n * The attachment handle every tool of this run is given as `ctx.attachments`.\n *\n * Resolved by the controller from the request — `attachmentsFor(req,\n * threadId)` — and passed in rather than reached for, because the scope is a\n * fact about the caller and the run has no way to derive one. `null`, or\n * omitted, is a request with no subject: the object tools get still exists\n * and every method on it throws a sentence naming `attachmentScope()`.\n *\n * Handed down unchanged to a sub-run started by `ctx.runAgent`. A sub-agent\n * is running on behalf of the same caller — that is the only reason it is\n * allowed to run at all — so it reads and writes the same scope, and a tool\n * three levels down can be given an id its parent parked. Widening it here\n * would be the confused-deputy hole in reverse.\n */\n attachments?: ScopedAttachments | null;\n /**\n * Set by `ctx.runAgent` and by nothing else.\n *\n * It rides on the public params rather than on a back door because\n * `Agent.stream` is the only way to start a run and a sub-run is a run —\n * giving nesting its own construction path would mean two places where a run\n * is set up, and the second one would drift. Omitted, a run is a root: depth\n * 0, no path, signatures over its own id.\n */\n nesting?: NestedContext;\n}\n\n/**\n * Where a run sits inside a tree of runs. Carried down by `ctx.runAgent`.\n *\n * `signingRunId` and `signingPath` are the reason this is threaded rather than\n * recomputed: a pending call a sub-agent raises is answered by the *client*,\n * which only ever sees the root run, so the token has to be minted under the\n * root's id and the sub-run's path from the start. Re-signing the token at each\n * level on the way up would work too, and would throw away every signature but\n * the outermost one — this way the run that asks the question is also the run\n * that can check the answer, which is where the tool, its schema and its `kind`\n * all already are.\n */\nexport type NestedContext = {\n /** 0 at the root; `ctx.depth` inside a tool of this run. */\n depth: number;\n /** The `maxDepth` of the run at the root of the tree. */\n maxDepth: number;\n /** Agent names from the root down to and including this one, so a cycle can\n * be reported as the chain that caused it. */\n chain: string[];\n /** The root run's id: what a pending call raised here is signed under. */\n signingRunId: string;\n /** Tool-call ids from the root down to the call that started this run. */\n signingPath: string[];\n /**\n * Inherited, and once it is `\"deny\"` it stays `\"deny\"` all the way down. A\n * caller that asked for an autonomous sub-agent must not have a question\n * surface from three levels below it, and the only way to promise that is to\n * make the whole subtree refuse rather than to check at the top.\n */\n onPending: \"escalate\" | \"deny\";\n};\n\nexport type AgentRunResult<T extends ToolShapes, O> = {\n runId: string;\n /** Everything produced this run — the controller persists these. */\n messages: AgentMessage<T, O>[];\n finishReason: FinishReason;\n usage: Usage;\n /** Set when the agent declares an `output` schema and the run finished. */\n output?: O;\n};\n\n/**\n * A run is an async iterable of events, and the SSE encoding is a method on it\n * rather than a separate helper — so the same object serves a controller\n * returning a `Response` and a server-side caller that just wants to await the\n * result.\n *\n * A run keeps going when its request ends. That is what makes reattaching after\n * a refresh possible, and it is why `stop()` is an explicit call rather than the\n * client closing a socket.\n */\nexport interface AgentRun<T extends ToolShapes = ToolShapes, O = unknown> extends AsyncIterable<\n AgentStreamEvent<T, O>\n> {\n readonly runId: string;\n /** Numbered events, replayable from a cursor. `toResponse` is this, encoded. */\n frames(from?: number): AsyncIterable<AgentStreamFrame<T, O>>;\n toResponse(params?: { from?: number }): Response;\n result(): Promise<AgentRunResult<T, O>>;\n /**\n * Cancels the run and closes the conversation behind it: every tool call\n * still in flight gets a `denied` result with `cause: \"stopped\"`, the\n * assistant message is finalized with `finishReason: \"aborted\"`, and both go\n * through `onMessage` like any other message.\n *\n * That last part is the point. A cancel that merely stops emitting leaves a\n * history the provider will reject on the next turn, so the run's last act is\n * to make the transcript valid — which is also what lets the user carry on\n * talking instead of starting over.\n */\n stop(params?: { reason?: string }): void;\n}\n\n/** A tool plus where it sits in the prompt. Fixed for the life of the agent. */\ntype ResolvedTool = {\n tool: AnyAgentTool;\n namespace?: string;\n deferred: boolean;\n};\n\n/** What a run needs from its agent, resolved once at `Agent.create`. */\ntype RunConfig = {\n name: string;\n instructions?: string;\n provider: AgentProvider;\n registry: Map<string, ResolvedTool>;\n providerTools: (ProviderToolSpec | ProviderToolNamespace)[];\n output?: Schema<any>;\n maxSteps: number;\n maxDepth: number;\n reasoning?: ReasoningEffort;\n maxOutputTokens?: number;\n temperature?: number;\n};\n\nconst DEFAULT_MAX_STEPS = 8;\n/** Three is enough for \"agent, sub-agent, specialist\" and small enough that a\n * runaway tree is a readable error rather than a stack trace. */\nconst DEFAULT_MAX_DEPTH = 3;\n\nexport class Agent<\n T extends readonly ToolEntry[] = readonly ToolEntry[],\n S extends readonly Skill[] = readonly Skill[],\n O extends Schema<any> | undefined = undefined,\n> {\n readonly name: string;\n readonly tools: T;\n readonly skills: S;\n readonly provider: AgentProvider;\n readonly output: O;\n readonly instructions?: string;\n readonly maxSteps: number;\n readonly maxDepth: number;\n readonly reasoning?: ReasoningEffort;\n\n private readonly config: RunConfig;\n\n private constructor(params: CreateAgentParams<T, S, O>) {\n this.name = params.name;\n this.instructions = params.instructions;\n this.provider = params.provider;\n this.tools = (params.tools ?? ([] as unknown as T)) as T;\n this.skills = (params.skills ?? ([] as unknown as S)) as S;\n this.output = params.output as O;\n this.maxSteps = params.maxSteps ?? DEFAULT_MAX_STEPS;\n this.maxDepth = params.maxDepth ?? DEFAULT_MAX_DEPTH;\n this.reasoning = params.reasoning;\n\n const { registry, providerTools } = lowerTools(this.tools, this.skills);\n this.config = {\n name: this.name,\n instructions: this.instructions,\n provider: this.provider,\n registry,\n providerTools,\n output: params.output as Schema<any> | undefined,\n maxSteps: this.maxSteps,\n maxDepth: this.maxDepth,\n reasoning: this.reasoning,\n maxOutputTokens: params.maxOutputTokens,\n temperature: params.temperature,\n };\n }\n\n static create<\n const T extends readonly ToolEntry[],\n const S extends readonly Skill[],\n O extends Schema<any> | undefined = undefined,\n >(params: CreateAgentParams<T, S, O>): Agent<T, S, O> {\n return new Agent(params);\n }\n\n stream(params: AgentStreamParams): AgentRun<ToolShapesOf<T>, OutputOf<O>> {\n const config: RunConfig = {\n ...this.config,\n provider: params.provider ?? this.config.provider,\n maxSteps: params.maxSteps ?? this.config.maxSteps,\n reasoning: params.reasoning ?? this.config.reasoning,\n maxOutputTokens: params.maxOutputTokens ?? this.config.maxOutputTokens,\n temperature: params.temperature ?? this.config.temperature,\n };\n return new AgentRunImpl(config, params) as unknown as AgentRun<ToolShapesOf<T>, OutputOf<O>>;\n }\n}\n\nexport type OutputOf<O> = O extends Schema<any> ? Infer<O> : never;\n\nexport type AnyAgent = Agent<any, any, any>;\n\n// --- lowering ------------------------------------------------------------\n\nfunction toolSpec(resolved: ResolvedTool): ProviderToolSpec {\n return {\n name: resolved.tool.name,\n description: resolved.tool.description,\n parameters: resolved.tool.inputSchema.toJSONSchema(),\n // Read off the schema, not asserted: an input containing an `s.json()`\n // field cannot be sent strict, and the tool that says so is the only place\n // that knows.\n strict: supportsStrict(resolved.tool.inputSchema),\n deferred: resolved.deferred,\n };\n}\n\n/**\n * Flattens the declared tuple into the registry the loop dispatches on, and the\n * shape the provider is shown.\n *\n * Both are built once, at `Agent.create`, because neither depends on the\n * request: a tool is a singleton and a namespace is a static grouping. Building\n * them per run would be work repeated on every turn for an answer that cannot\n * change — and it would move the name-collision errors below out of startup and\n * into the first user's first message.\n */\nfunction lowerTools(\n entries: readonly ToolEntry[],\n skills: readonly Skill[],\n): {\n registry: Map<string, ResolvedTool>;\n providerTools: (ProviderToolSpec | ProviderToolNamespace)[];\n} {\n const registry = new Map<string, ResolvedTool>();\n const providerTools: (ProviderToolSpec | ProviderToolNamespace)[] = [];\n\n const register = (resolved: ResolvedTool) => {\n if (registry.has(resolved.tool.name)) {\n throw new Error(\n `Two tools are named \"${resolved.tool.name}\". Tool names are global within an agent — the client discriminates a tool part by name alone.`,\n );\n }\n registry.set(resolved.tool.name, resolved);\n };\n\n for (const entry of entries) {\n if (entry instanceof ToolNamespace) {\n if (entry.name === SKILLS_NAMESPACE) {\n throw new Error(\n `\"${SKILLS_NAMESPACE}\" is reserved for the namespace skills are lowered into. Rename the namespace — silently shadowing it would make every skill unreachable with no error to read.`,\n );\n }\n const members: ProviderToolSpec[] = [];\n for (const tool of entry.tools) {\n const resolved = { tool, namespace: entry.name, deferred: entry.deferred || tool.deferred };\n register(resolved);\n members.push(toolSpec(resolved));\n }\n providerTools.push({ name: entry.name, description: entry.description, tools: members });\n continue;\n }\n const resolved = { tool: entry, deferred: entry.deferred };\n register(resolved);\n providerTools.push(toolSpec(resolved));\n }\n\n if (skills.length > 0) {\n const members: ProviderToolSpec[] = [];\n for (const skill of skills) {\n const tool = skillTool(skill);\n register({ tool, namespace: SKILLS_NAMESPACE, deferred: false });\n members.push({\n name: skill.name,\n description: skill.description,\n parameters: EMPTY_PARAMETERS,\n strict: true,\n // Not deferred: the whole cost of a skill in the prompt is its name and\n // description, and those are exactly what deferral keeps. Withholding\n // an empty parameter object saves nothing and adds a round trip.\n deferred: false,\n });\n }\n providerTools.push({\n name: SKILLS_NAMESPACE,\n description: SKILLS_NAMESPACE_DESCRIPTION,\n tools: members,\n });\n }\n\n return { registry, providerTools };\n}\n\n/** The zero-parameter tool a skill becomes. */\nfunction skillTool(skill: Skill): AnyAgentTool {\n return AgentTool.create({\n name: skill.name,\n description: skill.description,\n inputSchema: {\n toJSONSchema: () => EMPTY_PARAMETERS,\n parse: () => ({}),\n safeParse: () => ({ ok: true as const, value: {} }),\n } as unknown as Schema<Record<string, never>>,\n // The thunk is called here, on load, and not at startup: a skill body can\n // be a megabyte of markdown, and an agent that declares twelve of them\n // should not read twelve files to answer \"hello\".\n execute: async () => {\n const body =\n typeof skill.instructions === \"function\" ? await skill.instructions() : skill.instructions;\n const sections = [body];\n for (const file of skill.files ?? []) {\n sections.push(`--- ${file} ---\\n${await readSkillFile(file)}`);\n }\n return sections.join(\"\\n\\n\");\n },\n }) as unknown as AnyAgentTool;\n}\n\nasync function readSkillFile(file: string): Promise<string> {\n try {\n return await Bun.file(file).text();\n } catch (error) {\n // A missing file is told to the model rather than thrown: the rest of the\n // skill is still worth having, and a run should not die because one of\n // several appendices moved.\n return `(could not be read: ${(error as Error).message})`;\n }\n}\n\n// --- the run -------------------------------------------------------------\n\nclass RunAborted extends Error {\n constructor() {\n super(\"The run was stopped\");\n this.name = \"RunAborted\";\n }\n}\n\nfunction raceAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {\n if (signal.aborted) {\n return Promise.reject(new RunAborted());\n }\n return new Promise<T>((resolve, reject) => {\n const onAbort = () => reject(new RunAborted());\n signal.addEventListener(\"abort\", onAbort, { once: true });\n promise.then(resolve, reject).finally(() => signal.removeEventListener(\"abort\", onAbort));\n });\n}\n\nfunction emptyUsage(): Usage {\n return { inputTokens: 0, outputTokens: 0, totalTokens: 0 };\n}\n\nfunction addUsage(total: Usage, next: Usage | undefined): Usage {\n if (!next) return total;\n const merged: Usage = {\n inputTokens: total.inputTokens + (next.inputTokens ?? 0),\n outputTokens: total.outputTokens + (next.outputTokens ?? 0),\n totalTokens: total.totalTokens + (next.totalTokens ?? 0),\n };\n if (next.reasoningTokens !== undefined || total.reasoningTokens !== undefined) {\n merged.reasoningTokens = (total.reasoningTokens ?? 0) + (next.reasoningTokens ?? 0);\n }\n if (next.cachedInputTokens !== undefined || total.cachedInputTokens !== undefined) {\n merged.cachedInputTokens = (total.cachedInputTokens ?? 0) + (next.cachedInputTokens ?? 0);\n }\n if (next.imageInputTokens !== undefined || total.imageInputTokens !== undefined) {\n merged.imageInputTokens = (total.imageInputTokens ?? 0) + (next.imageInputTokens ?? 0);\n }\n if (next.imageOutputTokens !== undefined || total.imageOutputTokens !== undefined) {\n merged.imageOutputTokens = (total.imageOutputTokens ?? 0) + (next.imageOutputTokens ?? 0);\n }\n return merged;\n}\n\nfunction isAsyncGenerator(value: unknown): value is AsyncGenerator<unknown, unknown, void> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n typeof (value as AsyncGenerator).next === \"function\" &&\n Symbol.asyncIterator in (value as object)\n );\n}\n\n/**\n * The best parse of a JSON document that is still arriving.\n *\n * Exists so a UI can bind fields before the object closes. It closes whatever\n * brackets are open and drops a trailing key with no value; when even that does\n * not parse it gives up and returns an empty object rather than throwing,\n * because a snapshot is a convenience and a run must not die for one.\n */\nfunction bestEffortParse(text: string): any {\n if (!text.trim()) return {};\n try {\n return JSON.parse(text);\n } catch {\n // fall through to repair\n }\n const closers: string[] = [];\n let inString = false;\n let escaped = false;\n for (const char of text) {\n if (inString) {\n if (escaped) escaped = false;\n else if (char === \"\\\\\") escaped = true;\n else if (char === '\"') inString = false;\n continue;\n }\n if (char === '\"') inString = true;\n else if (char === \"{\") closers.push(\"}\");\n else if (char === \"[\") closers.push(\"]\");\n else if (char === \"}\" || char === \"]\") closers.pop();\n }\n let repaired = text;\n if (inString) repaired += '\"';\n repaired = repaired.replace(/[,:]\\s*$/, \"\");\n const suffix = closers.reverse().join(\"\");\n try {\n return JSON.parse(repaired + suffix);\n } catch {\n // A trailing `\"key\":` leaves a property with no value; drop the key too.\n try {\n return JSON.parse(repaired.replace(/,?\\s*\"[^\"]*\"\\s*$/, \"\") + suffix);\n } catch {\n return {};\n }\n }\n}\n\ntype StepOutcome = {\n reason: FinishReason;\n error?: AgentError;\n};\n\n/**\n * The prior transcript plus what a later run produced, upserted by id.\n *\n * A run only reports the messages it *made*, so a resumed sub-run's\n * `result().messages` is the tail and not the whole thing — and a message it\n * amended (the one holding the call that was finally answered) comes back under\n * an id the prior transcript already has. Appending would duplicate it and\n * replacing the array would lose everything before the resume, so the record on\n * `ToolCallPart.nested` is rebuilt by upsert, which is the same rule a store\n * keyed by message id follows.\n */\nfunction mergeMessages(prior: AgentMessage[], produced: AgentMessage[]): AgentMessage[] {\n const merged = [...prior];\n const index = new Map(merged.map((message, at) => [message.id, at]));\n for (const message of produced) {\n const at = index.get(message.id);\n if (at === undefined) {\n index.set(message.id, merged.length);\n merged.push(message);\n } else {\n merged[at] = message;\n }\n }\n return merged;\n}\n\n/** Tool calls in a transcript with no result anywhere in it. */\nfunction openCallIds(messages: AgentMessage[]): Set<string> {\n const resolved = new Set<string>();\n for (const message of messages) {\n for (const part of message.content) {\n if (part.type === \"tool-result\") resolved.add(part.toolCallId);\n }\n }\n const open = new Set<string>();\n for (const message of messages) {\n for (const part of message.content) {\n if (part.type === \"tool-call\" && !resolved.has(part.toolCallId)) open.add(part.toolCallId);\n }\n }\n return open;\n}\n\n/**\n * A sub-agent's structured answer, read back out of its transcript.\n *\n * `NestedRun` has nowhere to put an `output` — it is a transcript, and the\n * output part is already in it — so a memoized run recovers the value the same\n * way a client would. That keeps the memo honest: what a replay returns is\n * derived from what was persisted, not from a second copy that could disagree\n * with it.\n */\nfunction outputOf(messages: AgentMessage[]): unknown {\n for (let i = messages.length - 1; i >= 0; i--) {\n const content = messages[i].content;\n for (let j = content.length - 1; j >= 0; j--) {\n const part = content[j];\n if (part.type === \"output\" && part.partial !== true) return part.value;\n }\n }\n return undefined;\n}\n\n/** For the replay-mismatch message, where the label is what tells two runs of\n * the same agent apart. */\nfunction describeRun(agent: string, label: string | undefined): string {\n return label === undefined ? `\"${agent}\"` : `\"${agent}\" labelled \"${label}\"`;\n}\n\n/** A message flattened to text, for comparing one turn's seed against another's. */\nfunction textOfMessage(message: AgentMessage): string {\n return message.content\n .map((part) => (part.type === \"text\" ? part.text : `<${part.type}>`))\n .join(\"\");\n}\n\n/**\n * What a `runAgent` call would start its sub-run from, as a comparable string.\n *\n * `null` when the call names no seed at all — `runAgent(agent, {})` — which is\n * the one shape with nothing to compare against, since the record's first\n * message would then be something the sub-agent said rather than something it\n * was told.\n */\nfunction seedOf(params: RunAgentParams): string | null {\n if (params.prompt !== undefined) return `user:${params.prompt}`;\n const first = params.messages?.[0];\n return first ? `${first.role}:${textOfMessage(first)}` : null;\n}\n\n/**\n * The ids of the messages a tool injected to show a file, read from the\n * `ToolCallPart.attachments` records in `messages`.\n *\n * The record is the test, not `attachmentId` on the part: a user's own upload\n * carries an `attachmentId` too. `historyForProvider` keys its window on this\n * and `toolTurn` steps over these when it looks for the user's turn.\n */\nfunction injectedMessageIds(messages: AgentMessage[]): Set<string> {\n const injected = new Set<string>();\n for (const message of messages) {\n for (const part of message.content) {\n if (part.type !== \"tool-call\") continue;\n for (const record of part.attachments ?? []) {\n if (\"shown\" in record && record.shown) injected.add(record.shown.messageId);\n }\n }\n }\n return injected;\n}\n\n/**\n * How many tool-produced files stay attached to the request. See\n * `AgentRunImpl.historyForProvider`, which is where the reasoning lives.\n */\nconst SHOWN_FILE_WINDOW = 1;\n\n/**\n * Why the Nth attachment of a replayed tool body is not the Nth attachment of\n * the turn that escalated, or `null` when it is.\n *\n * DELIBERATELY WEAKER THAN `replayMismatch`, and the reason is what each of them\n * is protecting. A crossed sub-run pairs a human's answer with a question they\n * never saw, which is consent applied to the wrong thing and is invisible\n * afterwards; a crossed attachment shows the model the wrong picture, which is\n * wrong and is also the sort of wrong the next turn can talk its way out of. So\n * this checks the two things that cannot change for an honest reason and\n * nothing else.\n *\n * Not the size, and not any digest of the bytes: the body that produced this\n * blob ran again from the top and produced it again, and almost nothing that\n * makes an image — a model, a renderer with a timestamp in it, a compressor\n * with a thread pool — is byte-identical twice. Comparing bytes would fail the\n * common case and catch the rare one.\n *\n * Not the name either, for a smaller version of the same reason: filenames\n * carry dates and counters, and an app that names its output\n * `chart-${Date.now()}.png` would find its tool broken on every resume.\n *\n * What is left is the media type and whether the caller asked to show it, which\n * is exactly what changes when a body takes a different branch — the CSV path\n * instead of the PNG path, the quiet `put` instead of the `showModel` one.\n */\nfunction putMismatch(\n recorded: ToolAttachmentPut,\n blob: Blob,\n params: PutAttachmentParams,\n): string | null {\n const mimeType = params.mimeType || blob.type || \"\";\n if (mimeType && recorded.attachment.mimeType !== mimeType) {\n return (\n `was ${JSON.stringify(recorded.attachment.mimeType)} on the turn that escalated ` +\n `and is ${JSON.stringify(mimeType)} on the replay`\n );\n }\n const shown = Boolean(params.showModel);\n if (shown !== Boolean(recorded.shown)) {\n return shown\n ? \"was stored without showModel on the turn that escalated and asks for showModel on the replay\"\n : \"was stored with showModel on the turn that escalated and asks for a plain put on the replay\";\n }\n return null;\n}\n\n/**\n * Why the Nth sub-run of a replayed tool body is not the Nth sub-run of the\n * turn that escalated, or `null` when it is.\n *\n * Agent and label catch the branchy shape. THE SEED IS WHAT CATCHES THE SHAPE\n * THE DOC COMMENT NAMES FIRST: `runAgent` in a loop, the same agent every time,\n * no label — the default and the common case — over a list that came back in a\n * different order, from a `Set`, a re-sorted query, or a second read of a\n * mutable column. Agent and label match for every element of such a loop, so\n * without this the user's answer to the second question is folded into the run\n * the tool now believes is the first, and the model is told the crossed pair as\n * fact. Nothing in the transcript, the stream or the store shows it happened.\n *\n * The record's first message is the seed because `runNested` records it that\n * way — the user turn built from `prompt`, or the first of `params.messages`.\n * `instructions` is not compared: it never enters the transcript, and\n * `NestedRun` has nowhere to keep it.\n */\nfunction replayMismatch(\n recorded: NestedRun,\n agent: AnyAgent,\n params: RunAgentParams,\n): string | null {\n if (recorded.agent !== agent.name || recorded.label !== params.label) {\n return (\n `was ${describeRun(recorded.agent, recorded.label)} on the turn that escalated ` +\n `and is ${describeRun(agent.name, params.label)} on the replay`\n );\n }\n const seed = seedOf(params);\n const first = recorded.messages[0];\n const was = first ? `${first.role}:${textOfMessage(first)}` : null;\n if (seed !== null && was !== null && seed !== was) {\n return (\n `was started with ${JSON.stringify(was)} on the turn that escalated ` +\n `and with ${JSON.stringify(seed)} on the replay`\n );\n }\n return null;\n}\n\n/**\n * What is left of an answer's path once this run's own prefix is removed.\n *\n * `null` means the answer is not addressed to this run at all, which is a\n * client error rather than a routing decision — an empty remainder means \"a\n * call this run made itself\", and a non-empty one names the tool call to\n * re-enter.\n */\nfunction pathBelow(path: string[] | undefined, prefix: string[]): string[] | null {\n const full = path ?? [];\n if (full.length < prefix.length) return null;\n for (let i = 0; i < prefix.length; i++) {\n if (full[i] !== prefix[i]) return null;\n }\n return full.slice(prefix.length);\n}\n\n/**\n * The one memo rule a re-entered tool body plays by, in one place.\n *\n * A tool that escalates cannot be suspended — a paused async generator does not\n * fit in a message history — so it is re-entered FROM THE TOP on the next turn\n * and everything it did the first time has to be recognised rather than done\n * again. The only key available is the order the calls happened in, and the only\n * place a record can live is the message history, because that is the sole state\n * that crosses a turn boundary in a thread and in the browser alike.\n *\n * So: an array on the `ToolCallPart`, a snapshot of its length taken before the\n * body runs (everything already there came from an earlier turn and is\n * replayable; everything appended past that point is happening for the first\n * time), and an index that walks it. `ctx.runAgent` and `ctx.attachments.put`\n * both need exactly this and they share it here rather than each growing their\n * own copy — the failure of two copies is that one of them is fixed and the\n * other is not, and both are invisible until someone resumes.\n *\n * The array is created by the first WRITE and not before. `nested: []` or\n * `attachments: []` on every tool call would be a wire and store change paid for\n * by every app that has neither — and \"on first use\" is not good enough, because\n * a slot is handed out before the work that fills it can fail. A tool whose only\n * `put` throws (no attachment scope, a provider that cannot read files) must\n * leave a tool call with no `attachments` field, not an empty array announcing\n * an attachment that does not exist.\n *\n * A SLOT IS RESERVED WHEN IT IS ASKED FOR, THOUGH, NOT WHEN IT IS FILLED, and\n * that is deliberate: a tool body may run its `put`s or its `runAgent`s\n * concurrently — `await Promise.all(images.map((i) => ctx.attachments.put(i)))`\n * is the obvious way to write it — and every one of them takes its index\n * synchronously, in map order, before its first `await`. Handing out the index\n * on success instead would number them by the order they *finished*, which the\n * network decides and which the next turn will not reproduce. So the index\n * always advances, and a slot whose work threw stays unwritten.\n *\n * An unwritten slot before a written one would be a hole, and a hole is `null`\n * once it goes through JSON — which is what a consumer walking `part.attachments`\n * would crash on. `fill` is what stands in its place. `runAgent` passes none, so\n * `nested` keeps exactly the shape it has had since sub-agents existed, holes\n * and all: `NestedRun` describes a sub-run that happened and has no honest shape\n * for one that did not, and inventing one is a change to nesting rather than to\n * the issue this memo was extracted for.\n *\n * What is NOT shared is what to do with a hit, and it could not be: a recorded\n * sub-run that parked has to be *continued*, so `runAgent`'s memo sometimes\n * returns and sometimes falls through, while a recorded `put` is total — the\n * bytes are already stored and the id is already in the transcript the model\n * read, so there is nothing left to finish. The drift check differs too, and for\n * a reason worth reading: see `replayMismatch` against the note on\n * `putMismatch`.\n */\nclass ReplayMemo<R> {\n private index = 0;\n private readonly replayable: number;\n\n constructor(\n private readonly read: () => R[] | undefined,\n private readonly create: () => R[],\n /** What stands in for a slot whose work threw. See the note above. */\n private readonly fill?: () => R,\n ) {\n this.replayable = read()?.length ?? 0;\n }\n\n /**\n * The next slot: its index, the record a previous turn left there if it left\n * one, and the way to write this turn's.\n *\n * `write` is the only thing that touches the array. Call it once the record\n * exists and not before — everything between `next()` and `write()` is work\n * that may throw, and a slot nobody writes is a slot that costs nothing.\n */\n next(): { at: number; recorded: R | undefined; write: (record: R) => void } {\n const at = this.index++;\n return {\n at,\n recorded: at < this.replayable ? this.read()?.[at] : undefined,\n write: (record: R) => {\n const rows = this.read() ?? this.create();\n if (this.fill) {\n for (let i = rows.length; i < at; i++) rows[i] = this.fill();\n }\n rows[at] = record;\n },\n };\n }\n}\n\nclass AgentRunImpl implements AgentRun<ToolShapes, unknown> {\n readonly runId: string;\n\n private readonly config: RunConfig;\n private readonly params: AgentStreamParams;\n private readonly controller = new AbortController();\n\n private readonly buffer: AgentStreamFrame<ToolShapes, unknown>[] = [];\n private readonly waiters = new Set<() => void>();\n private seq = 0;\n private ended = false;\n\n /** The working history handed to the provider, and what this run produced. */\n private history: AgentMessage[] = [];\n private produced: AgentMessage[] = [];\n private current: AgentMessage | null = null;\n /**\n * Whether a step of the message now open ran out of output budget.\n *\n * Separate from `finishReason` because they are different facts: a step that\n * hits the ceiling and also calls a tool closes its message `awaiting-input` or\n * `max-steps`. Cleared as each message is finalized.\n */\n private outputTruncated = false;\n\n /** Messages from an earlier run this one has amended, by id. Cloned once and\n * reused, so two results for the same message do not fork it. */\n private readonly amended = new Map<string, AgentMessage>();\n /**\n * Messages this run finished outside `finalizeMessage` that have not yet gone\n * through `onMessage`: earlier turns' messages it amended, and messages it\n * injected for a file a tool showed.\n *\n * They wait rather than being reported where they are made, because\n * `onMessage` is the persistence point for an app that has no `store` and an\n * append-only table keyed by a serial id reads back in the order the hook was\n * called. Reporting an injected message from inside `runTools` would call the\n * hook for it BEFORE the assistant message whose tool call produced it, since\n * that message is only finalized once the step is over — so the transcript in\n * the app's database would put the file above the turn that made it, while\n * `result.messages`, the stream and `history` all put it below.\n */\n private readonly unreported = new Set<AgentMessage>();\n\n private usage: Usage = emptyUsage();\n private finishReason: FinishReason = \"stop\";\n private output: unknown;\n private stopReason: string | undefined;\n\n private readonly settled: Promise<AgentRunResult<ToolShapes, unknown>>;\n\n /**\n * Where this run sits in a tree of runs, all of it constant for the run.\n *\n * `signingRunId` is the *root's* id rather than this one's: the client only\n * ever sees the root run, so a question a sub-agent asks has to travel under\n * an id the client can hand back. `pathPrefix` is the chain of tool calls\n * above this run, and it is both what a pending call raised here is signed\n * over and what an answer coming back is matched against.\n */\n private readonly depth: number;\n private readonly maxDepth: number;\n private readonly chain: string[];\n private readonly signingRunId: string;\n private readonly pathPrefix: string[];\n private readonly onPending: \"escalate\" | \"deny\";\n /**\n * Sub-runs that have not yet written their transcript to the tool call.\n *\n * The abort path waits on these. A `stop()` reaches a sub-run through the\n * shared signal, so it is already closing — but `raceAbort` in `runTools`\n * returns the moment the signal fires, which would finalize and persist the\n * parent's message before the sub-run had recorded what it managed to do.\n * The work would be on the stream and missing from the store.\n */\n private readonly nestedSettling = new Set<Promise<unknown>>();\n /**\n * Messages for files a tool asked to show, held until its call settles, keyed\n * by tool call id. See `queueShown`.\n */\n private readonly shownQueue = new Map<string, AgentMessage[]>();\n\n constructor(config: RunConfig, params: AgentStreamParams) {\n this.config = config;\n this.params = params;\n this.runId = params.runId ?? `run_${crypto.randomUUID()}`;\n this.history = [...params.messages];\n\n const nesting = params.nesting;\n this.depth = nesting?.depth ?? 0;\n this.maxDepth = nesting?.maxDepth ?? config.maxDepth;\n this.chain = nesting?.chain ?? [config.name];\n this.signingRunId = nesting?.signingRunId ?? this.runId;\n this.pathPrefix = nesting?.signingPath ?? [];\n this.onPending = nesting?.onPending ?? \"escalate\";\n\n if (params.signal) {\n if (params.signal.aborted) this.controller.abort();\n else params.signal.addEventListener(\"abort\", () => this.stop(), { once: true });\n }\n\n // Started here, not on first read. A run outlives the request that began\n // it, so nothing may depend on someone being attached — a client that\n // never reads still gets its tools run and its messages persisted.\n this.settled = this.execute();\n // And the request that started it stays open until it settles. Its tools\n // read the user from that request's context, and a client that leaves\n // mid-run cancels the response body without stopping the run — ending the\n // request there would take the user away from step four's tool call.\n // `ctx()` is the ambient request store: undefined outside a request.\n params.req?.ctx?.()?.waitUntil(this.settled);\n }\n\n // --- event plumbing ----------------------------------------------------\n\n private emit(event: AgentStreamEvent<ToolShapes, unknown>) {\n if (this.ended) return;\n this.buffer.push({ seq: ++this.seq, event });\n this.wake();\n }\n\n private wake() {\n const pending = [...this.waiters];\n this.waiters.clear();\n for (const resolve of pending) resolve();\n }\n\n private nextFrame(): Promise<void> {\n return new Promise<void>((resolve) => this.waiters.add(resolve));\n }\n\n /**\n * Replays from the buffer, then follows the run live.\n *\n * The whole run is buffered rather than a sliding window: a run is bounded by\n * `maxSteps`, and a client that reconnects two steps late wanting frame 42\n * must get frame 42 and not \"the oldest I still have\". Bounding it is the\n * live-run registry's job, where the policy question is how long a *finished*\n * run is kept.\n */\n async *frames(from = 0): AsyncIterable<AgentStreamFrame<ToolShapes, unknown>> {\n let index = from > 0 ? from - 1 : 0;\n for (;;) {\n while (index < this.buffer.length) {\n yield this.buffer[index++];\n }\n if (this.ended) return;\n await this.nextFrame();\n }\n }\n\n async *[Symbol.asyncIterator](): AsyncIterator<AgentStreamEvent<ToolShapes, unknown>> {\n for await (const frame of this.frames()) {\n yield frame.event;\n }\n }\n\n toResponse(params?: { from?: number }): Response {\n const frames = this.frames(params?.from);\n const encoder = new TextEncoder();\n let cancelled = false;\n\n let keepalive!: ReturnType<typeof sseKeepalive>;\n\n const body = new ReadableStream<Uint8Array>({\n start: async (controller) => {\n // A slow tool or a thinking sub-agent can leave the connection silent\n // for longer than a proxy's idle timeout, and a proxy that closes it\n // looks to the client exactly like a run that finished.\n keepalive = sseKeepalive(controller);\n try {\n for await (const frame of frames) {\n if (cancelled) break;\n // `id:` carries the cursor so a browser reconnecting with\n // `Last-Event-ID` is already asking the right question.\n controller.enqueue(\n encoder.encode(`id: ${frame.seq}\\ndata: ${JSON.stringify(frame.event)}\\n\\n`),\n );\n keepalive.touch();\n }\n } catch {\n // A stream that cannot be written to is a dead reader, not a dead\n // run. Nothing to report and nothing to stop.\n }\n keepalive.stop();\n try {\n controller.close();\n } catch {\n // already closed by a cancel\n }\n },\n cancel: () => {\n // Deliberately does not touch the run. A disconnect is a reader\n // leaving; `stop()` is the only thing that cancels work, because the\n // tool loop is here and a closed tab has not stopped step four from\n // charging a card.\n cancelled = true;\n keepalive.stop();\n },\n });\n\n return new Response(body, {\n headers: {\n \"Content-Type\": \"text/event-stream; charset=utf-8\",\n \"Cache-Control\": \"no-cache, no-transform\",\n Connection: \"keep-alive\",\n // Tells nginx not to buffer, which would otherwise hold every frame\n // until the response ended and make a stream look like a long pause.\n \"X-Accel-Buffering\": \"no\",\n },\n });\n }\n\n result(): Promise<AgentRunResult<ToolShapes, unknown>> {\n return this.settled;\n }\n\n stop(params?: { reason?: string }): void {\n if (this.ended || this.controller.signal.aborted) return;\n this.stopReason = params?.reason;\n this.controller.abort();\n }\n\n // --- the loop ----------------------------------------------------------\n\n private async execute(): Promise<AgentRunResult<ToolShapes, unknown>> {\n this.emit({ type: \"run-start\", runId: this.runId, threadId: this.params.threadId });\n\n try {\n // A turn that answers a sub-agent's question re-enters the tool that\n // asked it, and that tool may ask again — so the run can be finished\n // before it has taken a single model step. Going on to `loop()` here\n // would step the model with a tool call still open, which is exactly the\n // history the provider rejects.\n const escalated = await this.ingestTurn();\n if (escalated.length > 0) {\n this.finishReason = \"awaiting-input\";\n this.emit({ type: \"awaiting-input\", runId: this.runId, pending: escalated });\n } else {\n await this.loop();\n }\n } catch (error) {\n if (error instanceof RunAborted || this.controller.signal.aborted) {\n await this.finalizeAborted();\n } else {\n const normalized = this.config.provider.normalizeError(error);\n this.emit({ type: \"error\", error: normalized });\n await this.finalizeMessage(\"error\");\n this.finishReason = \"error\";\n }\n }\n\n this.emit({ type: \"usage\", usage: this.usage });\n this.emit({ type: \"run-end\", runId: this.runId, finishReason: this.finishReason });\n this.ended = true;\n this.wake();\n\n return {\n runId: this.runId,\n messages: this.produced as AgentMessage<ToolShapes, unknown>[],\n finishReason: this.finishReason,\n usage: this.usage,\n output: this.output,\n };\n }\n\n private async loop(): Promise<void> {\n const maxSteps = Math.max(1, this.config.maxSteps);\n\n for (let step = 1; step <= maxSteps; step++) {\n const message = this.startMessage();\n const outcome = await this.runStep(message);\n\n if (outcome.error) {\n this.emit({ type: \"error\", error: outcome.error });\n await this.finalizeMessage(\"error\");\n this.finishReason = \"error\";\n return;\n }\n\n const calls = message.content.filter(\n (part): part is ToolCallPart => part.type === \"tool-call\",\n );\n\n if (calls.length === 0) {\n this.finishReason = outcome.reason;\n await this.finalizeMessage(outcome.reason);\n return;\n }\n\n const pending = await this.runTools(message, calls, step);\n\n if (pending.length > 0) {\n // The message closes first, then the run says what it is waiting for:\n // `awaiting-input` is terminal, and everything needed to answer it has\n // to already be on the stream when it arrives.\n this.finishReason = \"awaiting-input\";\n await this.finalizeMessage(\"awaiting-input\");\n this.emit({ type: \"awaiting-input\", runId: this.runId, pending });\n return;\n }\n\n if (step === maxSteps) {\n // Not an exception. An agent that will not stop calling tools is a bug\n // the app has to be able to see and show, and a throw here would put it\n // in a log instead of in the transcript.\n this.finishReason = \"max-steps\";\n await this.finalizeMessage(\"max-steps\");\n return;\n }\n\n await this.finalizeMessage(outcome.reason);\n }\n }\n\n private startMessage(): AgentMessage {\n const message: AgentMessage = {\n id: `msg_${crypto.randomUUID()}`,\n role: \"assistant\",\n content: [],\n createdAt: new Date().toISOString(),\n };\n this.current = message;\n this.history.push(message);\n this.produced.push(message);\n this.emit({ type: \"message-start\", messageId: message.id, role: \"assistant\" });\n return message;\n }\n\n private async finalizeMessage(reason: FinishReason): Promise<void> {\n const message = this.current;\n if (!message) return;\n this.current = null;\n // Read and cleared together: it describes the message being closed, and the\n // next one starts with no opinion.\n const outputTruncated = this.outputTruncated;\n this.outputTruncated = false;\n message.finishReason = reason;\n // On the message and not only on the frame. The frame reaches a live client,\n // which is half the audience: `onMessage` persists this object and\n // `result().messages` hands it back, and a transcript restored through\n // `useChat({ initialMessages })` has nothing else to read. `finishReason`\n // cannot answer it — a step that ran out of budget while calling a tool\n // closes `awaiting-input`, which is the case this exists for.\n if (outputTruncated) message.outputTruncated = true;\n // Before the message is handed to `onMessage` to be persisted and before it\n // reaches `result()` — the two places it stops being written and starts\n // being kept. Every exit lands here, aborted and errored runs included.\n for (const part of message.content) {\n if (part.type === \"text\" || part.type === \"reasoning\") {\n if (typeof part.text === \"string\") part.text = resolveRope(part.text);\n }\n }\n this.emit({\n type: \"message-end\",\n messageId: message.id,\n finishReason: reason,\n // Only when true, so the frame an ordinary message ends with is unchanged.\n ...(outputTruncated ? { outputTruncated: true as const } : {}),\n });\n await this.report(message);\n // After it, never before: a file a tool showed during this message belongs\n // below the message whose tool call produced it, in the hook exactly as it\n // is in `result().messages` and on the stream. Empty on every step of a run\n // with no such file, which is most of them.\n await this.reportDeferred();\n }\n\n private async report(message: AgentMessage): Promise<void> {\n if (!this.params.onMessage) return;\n try {\n await this.params.onMessage(message);\n } catch {\n // Persistence failing must not take the transcript with it: the messages\n // are still on the stream and still in `result()`.\n }\n }\n\n // --- one model call ----------------------------------------------------\n\n private async runStep(message: AgentMessage): Promise<StepOutcome> {\n const signal = this.controller.signal;\n const provider = this.config.provider;\n\n let outcome: StepOutcome = { reason: \"stop\" };\n const partialArgs = new Map<string, { name: string; args: string }>();\n let outputText = \"\";\n\n const stream = provider.stream({\n // Not `this.history` directly: an image a tool showed is in the history\n // forever and must not be in every *request* forever. See\n // `historyForProvider`.\n messages: this.historyForProvider(message),\n systemPrompt: await this.systemPrompt(),\n tools: this.config.providerTools.length > 0 ? this.config.providerTools : undefined,\n output: this.config.output\n ? {\n name: \"output\",\n schema: this.config.output.toJSONSchema(),\n strict: supportsStrict(this.config.output),\n }\n : undefined,\n reasoning: this.config.reasoning,\n maxOutputTokens: this.config.maxOutputTokens,\n temperature: this.config.temperature,\n signal,\n });\n\n const iterator = stream[Symbol.asyncIterator]();\n for (;;) {\n const next = await raceAbort(Promise.resolve(iterator.next()), signal);\n if (next.done) break;\n const event = next.value;\n\n switch (event.type) {\n case \"text-delta\": {\n appendText(message, \"text\", event.delta);\n this.emit({ type: \"text-delta\", messageId: message.id, delta: event.delta });\n break;\n }\n case \"reasoning-delta\": {\n appendReasoning(message, event.id, event.delta);\n this.emit({\n type: \"reasoning-delta\",\n messageId: message.id,\n delta: event.delta,\n id: event.id,\n });\n break;\n }\n case \"output-delta\": {\n outputText += event.delta;\n this.emit({\n type: \"output-delta\",\n messageId: message.id,\n delta: event.delta,\n snapshot: bestEffortParse(outputText),\n });\n break;\n }\n case \"tool-search\": {\n this.emit({ type: \"tool-search\", loaded: event.loaded });\n break;\n }\n case \"tool-call-delta\": {\n const held = partialArgs.get(event.toolCallId) ?? { name: event.name, args: \"\" };\n held.args += event.argsDelta;\n held.name = event.name || held.name;\n partialArgs.set(event.toolCallId, held);\n this.emit({\n type: \"tool-call\",\n messageId: message.id,\n part: {\n type: \"tool-call\",\n toolCallId: event.toolCallId,\n name: held.name,\n input: bestEffortParse(held.args),\n partial: true,\n },\n });\n break;\n }\n case \"tool-call\": {\n partialArgs.delete(event.toolCallId);\n const part: ToolCallPart = {\n type: \"tool-call\",\n toolCallId: event.toolCallId,\n name: event.name,\n // A raw string when the model produced something that is not JSON.\n // Keeping it is what makes the `invalid_tool_input` result below\n // readable instead of an empty object nobody can explain.\n input: parseArgs(event.args),\n };\n message.content.push(part);\n this.emit({ type: \"tool-call\", messageId: message.id, part });\n break;\n }\n case \"finish\": {\n this.usage = addUsage(this.usage, event.usage);\n // The usage is taken either way, the reason only if nothing has\n // already failed. A provider is allowed to report an error and then\n // close the call with a finish frame — a content filter does exactly\n // that, and it still bills for the tokens — and letting the closing\n // frame overwrite the outcome would turn \"blocked\" into an empty\n // answer with no explanation anywhere.\n if (!outcome.error) outcome = { reason: event.reason };\n break;\n }\n case \"error\": {\n outcome = { reason: \"error\", error: event.error };\n break;\n }\n }\n }\n\n // A tool call whose arguments never finished arriving. It is still a call\n // the model made, so it gets a part and, below, an `invalid_tool_input`\n // result — dropping it would leave the model unable to see what went wrong.\n for (const [toolCallId, held] of partialArgs) {\n const part: ToolCallPart = {\n type: \"tool-call\",\n toolCallId,\n name: held.name,\n input: parseArgs(held.args),\n };\n message.content.push(part);\n this.emit({ type: \"tool-call\", messageId: message.id, part });\n }\n\n // `length` is excluded, and it is the reason `maxOutputTokens` is worth\n // having at all. A cut-off answer is a prefix of the JSON the model meant\n // to write, and `bestEffortParse` closes whatever brackets are open — so a\n // truncated document arrives at `safeParse` looking like a whole one. For\n // a schema of `s.json()` fields it then *passes*, and the app is handed a\n // half-written page with nothing to distinguish it from a finished one.\n //\n // So a run that hit the ceiling produces no `output` part and ends with\n // `finishReason: \"length\"`, which is the channel for \"not an error, and not\n // a finished answer\" — the argument `max-steps` already makes in the\n // `FinishReason` type.\n //\n // No error is emitted, and that is a deliberate change rather than a\n // consequence of the cap. `length` does not mean the app set one: the\n // provider reports it from the model's own ceiling too, which is how this\n // was reachable before `maxOutputTokens` existed at all. Before this, a\n // truncated run fell through to `safeParse`, and a schema with required\n // keys among the missing ones failed it and raised a schema-mismatch error.\n // That diagnostic is gone on purpose — it described the truncation as a\n // model mistake, and it never fired for a schema loose enough to accept the\n // repaired prefix, which is the case that actually needed saying. What\n // replaces it is one answer for every schema: no output, and a finish\n // reason that says why.\n const truncated = outcome.reason === \"length\";\n // Recorded on the run rather than inferred from the message's finish reason,\n // which is not this: a step that hits the ceiling and also calls a tool ends\n // the message `awaiting-input` or `max-steps`. The client needs the fact\n // itself, or it completes a partial output the server withheld.\n if (truncated) this.outputTruncated = true;\n if (this.config.output && outputText && !outcome.error && !truncated) {\n const parsed = this.config.output.safeParse(bestEffortParse(outputText));\n if (parsed.ok === true) {\n this.output = parsed.value;\n message.content.push({ type: \"output\", value: parsed.value });\n } else {\n this.emit({\n type: \"error\",\n error: {\n code: \"unknown\",\n message: `The model's structured answer did not match the output schema: ${parsed.errors.join(\", \")}`,\n retryable: true,\n },\n });\n }\n }\n\n return outcome;\n }\n\n private async systemPrompt(): Promise<string | undefined> {\n const parts = [this.config.instructions, this.params.instructions].filter(\n (part): part is string => Boolean(part && part.trim()),\n );\n return parts.length > 0 ? parts.join(\"\\n\\n\") : undefined;\n }\n\n // --- tools -------------------------------------------------------------\n\n private async runTools(\n message: AgentMessage,\n calls: ToolCallPart[],\n step: number,\n ): Promise<PendingToolCall[]> {\n const pending: PendingToolCall[] = [];\n const running: Promise<void>[] = [];\n\n for (const call of calls) {\n const resolved = this.config.registry.get(String(call.name));\n\n if (!resolved) {\n this.addResult(message, {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"tool_error\",\n message: `There is no tool named \"${String(call.name)}\".`,\n toolCallId: call.toolCallId,\n retryable: true,\n },\n });\n continue;\n }\n\n const parsed = resolved.tool.inputSchema.safeParse(call.input);\n if (parsed.ok === false) {\n // Back to the model, not up the stack. A model that mis-typed one\n // argument can usually fix it on the next step, and throwing turns a\n // recoverable mistake into a dead run.\n this.addResult(message, {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_input\",\n message: `Invalid arguments for \"${String(call.name)}\": ${parsed.errors.join(\", \")}`,\n toolCallId: call.toolCallId,\n retryable: true,\n },\n });\n continue;\n }\n\n // The parsed value replaces the raw arguments on the part, and from here\n // on it is the only input this call has.\n //\n // A schema normalizes — it fills defaults, coerces, and drops the `null`s\n // that strict mode forces a model to send for an omitted optional. So\n // `safeParse(input)` and `input` are different values, and a pending call\n // has to be signed over, shown as, verified against and executed with the\n // *same* one. Keeping the raw value in the transcript and signing the\n // parsed one meant the MACs could not match on the way back: every\n // approval of a tool with an optional field came back looking forged, and\n // the user who clicked Approve was told they had refused.\n //\n // Writing it here rather than re-parsing on the way back also avoids\n // assuming `safeParse` is idempotent — the history now carries the value\n // the signature covers, so verification is a comparison and not a second\n // guess at what the first parse produced.\n call.input = parsed.value;\n\n const kind = pendingKind(resolved.tool);\n if (kind) {\n if (this.onPending === \"deny\") {\n this.addResult(message, this.deniedByPolicy(call));\n continue;\n }\n pending.push({\n toolCallId: call.toolCallId,\n name: call.name,\n input: parsed.value,\n kind,\n signature: signPendingCall(\n this.claimsFor(call.toolCallId, String(call.name), kind, parsed.value),\n ),\n ...(this.pathPrefix.length > 0 ? { path: [...this.pathPrefix] } : {}),\n });\n continue;\n }\n\n running.push(\n this.executeTool(resolved, message.id, call, parsed.value, step)\n .then(async (result) => {\n this.addResult(message, result);\n // The call has settled. Anything it asked to show goes into the\n // history now, after its own result — which is the order the\n // provider validates and the order it happened in.\n await this.settleShown(call.toolCallId);\n })\n .catch((error) => {\n if (error instanceof PendingEscalation) {\n if (this.onPending === \"deny\") {\n this.addResult(message, this.deniedByPolicy(call));\n return;\n }\n // Collected exactly like a pending call this run made itself, and\n // deliberately without a result on `call`: the tool did not\n // finish, so its call stays open and the next turn re-enters it.\n // Siblings are untouched — `Promise.all` below still waits for\n // them, and one that completes keeps its result rather than being\n // thrown away because a different tool asked a question.\n pending.push(...error.pending);\n return;\n }\n // Only `RunAborted` reaches here, and the abort path denies every\n // unresolved call at once — swallowing it keeps a stopped run from\n // also raising an unhandled rejection.\n }),\n );\n }\n\n await raceAbort(\n Promise.all(running).then(() => undefined),\n this.controller.signal,\n );\n return pending;\n }\n\n /**\n * What a pending call is signed over.\n *\n * `signingRunId` is the root run's, not this one's: the client only ever sees\n * the root, so a sub-agent's question has to be minted under an id the client\n * can hand back and this run can still recognise on the way in.\n */\n private claimsFor(\n toolCallId: string,\n name: string,\n kind: \"approval\" | \"question\" | \"client\",\n input: unknown,\n ) {\n return {\n runId: this.signingRunId,\n toolCallId,\n name,\n kind,\n input,\n // Absent rather than empty at the top level, so the signature a root run\n // mints is byte-for-byte the one it minted before nesting existed.\n path: this.pathPrefix.length > 0 ? [...this.pathPrefix] : undefined,\n };\n }\n\n /**\n * The refusal a sub-run running under `onPending: \"deny\"` gives itself.\n *\n * Told to the model rather than dropped, like every other denial: the\n * sub-agent asked for something it cannot have here, and the next step goes\n * better for knowing that than for finding a hole where a result should be.\n */\n private deniedByPolicy(call: ToolCallPart): ToolResultPart {\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"denied\",\n cause: \"refused\",\n reason: `\"${String(call.name)}\" needs the user, and this run was started with onPending: \"deny\". Answer from what you already have.`,\n };\n }\n\n private async executeTool(\n resolved: ResolvedTool,\n messageId: string,\n call: ToolCallPart,\n input: unknown,\n step: number,\n resume?: { answers: ClientToolResult[] },\n ): Promise<ToolResultPart> {\n // One object, because the three of them share a memo — see `toolFiles`.\n const files = this.toolFiles(call);\n const ctx: ToolContext = {\n req: this.params.req,\n // `{}` rather than undefined, so a tool can read a field without a guard\n // and get the same answer — absent — whether the client sent nothing or\n // the run was started without a body at all.\n body: this.params.body ?? {},\n runId: this.runId,\n threadId: this.params.threadId,\n toolCallId: call.toolCallId,\n signal: this.controller.signal,\n step,\n depth: this.depth,\n resumed: resume !== undefined,\n attachments: files.attachments,\n generateImage: files.generateImage,\n editImage: files.editImage,\n turn: this.toolTurn(messageId),\n runAgent: this.nestedRunner(messageId, call, resume),\n };\n\n try {\n const started = resolved.tool.execute!(input as any, ctx);\n let output: unknown;\n if (isAsyncGenerator(started)) {\n let next = await started.next();\n while (!next.done) {\n this.emit({ type: \"tool-progress\", toolCallId: call.toolCallId, data: next.value });\n next = await started.next();\n }\n output = next.value;\n } else {\n output = await started;\n }\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"ok\",\n output,\n };\n } catch (error) {\n if (error instanceof RunAborted || this.controller.signal.aborted) {\n // Left to the abort path, which denies every unresolved call at once.\n throw new RunAborted();\n }\n if (error instanceof PendingEscalation) {\n // The one throw that is not a failure. Turning it into a `tool_error`\n // here would tell the model the tool broke and tell the user nothing,\n // and the question the sub-agent asked would be lost with no trace of\n // where it went — which is precisely the silent failure this branch\n // exists to prevent.\n throw error;\n }\n // A throwing tool is a result, not an exception out of the run: the model\n // is told the call failed and can try something else, which is what a\n // person would do.\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"tool_error\",\n message: error instanceof Error ? error.message : String(error),\n toolCallId: call.toolCallId,\n retryable: true,\n },\n };\n }\n }\n\n // --- nested runs -------------------------------------------------------\n\n /**\n * The `ctx.runAgent` given to one tool call, with its own memo.\n *\n * MEMOIZATION IS BY CALL INDEX, and the index is the only key there is. A\n * paused async generator cannot be put into a message history, so an\n * escalating tool is re-entered from the top rather than resumed in place,\n * and the Nth `runAgent` of the re-entered body has to be paired with the Nth\n * sub-run of the previous attempt. `ToolCallPart.nested` is that record, which\n * is also why it lives on the message: it is exactly the history the next turn\n * loads anyway, from the store or from the client.\n *\n * A tool whose `runAgent` calls sit inside a branch or a loop can produce a\n * different sequence on replay, and then index N means two different things.\n * That is checked below rather than trusted — pairing a user's answer with the\n * wrong sub-run is the failure this whole mechanism exists to avoid, and it\n * would be invisible.\n */\n private nestedRunner(\n messageId: string,\n call: ToolCallPart,\n resume?: { answers: ClientToolResult[] },\n ): ToolContext[\"runAgent\"] {\n // Snapshotted before the tool body runs, and lazily created on first use.\n // Both rules, and the reasoning behind them, are on `ReplayMemo` — which\n // `ctx.attachments.put` shares, so that the two memos on one tool call\n // cannot drift apart in how they decide what is a replay.\n const memo = new ReplayMemo<NestedRun>(\n () => call.nested,\n () => (call.nested = []),\n );\n\n return async (agent: AnyAgent, params: RunAgentParams = {}): Promise<NestedRunResult> => {\n const { at, recorded, write } = memo.next();\n\n if (recorded) {\n const mismatch = replayMismatch(recorded, agent, params);\n if (mismatch) {\n throw new Error(\n `Nested run ${at} of \"${String(call.name)}\" ${mismatch}. ` +\n `runAgent is memoized by call index, so a body whose runAgent calls depend on a condition that changed between turns cannot be resumed — the answer would be paired with a different sub-run. ` +\n `Make the sequence of runAgent calls, and what each one is asked, the same every time this tool runs, or branch on ctx.resumed.`,\n );\n }\n if (recorded.finishReason !== \"awaiting-input\") {\n // The whole point of the memo: no provider is called, no sub-tool\n // runs, and no usage is counted a second time — this turn did not\n // spend it, an earlier one did.\n return {\n runId: recorded.runId,\n agent: recorded.agent,\n messages: recorded.messages,\n finishReason: recorded.finishReason ?? \"stop\",\n usage: recorded.usage ?? emptyUsage(),\n output: outputOf(recorded.messages),\n nested: recorded,\n };\n }\n }\n\n return this.runNested(messageId, call, agent, params, write, recorded, resume?.answers ?? []);\n };\n }\n\n /**\n * Starts, or continues, one sub-run and joins it to this one.\n *\n * Joining is the only reason this exists — a tool can already make an agent\n * and iterate it. What it cannot do by hand is put the sub-run's events on\n * this run's stream in this run's `seq`, roll its usage up, record its\n * transcript where the next turn will look for it, and carry the depth and\n * the name chain so a cycle is a sentence rather than a stack overflow.\n */\n private async runNested(\n messageId: string,\n call: ToolCallPart,\n agent: AnyAgent,\n params: RunAgentParams,\n write: (record: NestedRun) => void,\n recorded: NestedRun | undefined,\n answers: ClientToolResult[],\n ): Promise<NestedRunResult> {\n // Both checks before anything starts, so the failure is a tool result the\n // model can read rather than a partly-run tree. The name chain catches the\n // common cycle (A runs A, A runs B runs A) exactly; the depth limit catches\n // the shapes a name cannot see, such as the same agent under two names.\n const chain = [...this.chain, agent.name];\n if (this.chain.includes(agent.name)) {\n throw new Error(\n `\"${agent.name}\" is already running further up this chain: ${chain.join(\" -> \")}. An agent cannot run itself, directly or through another agent.`,\n );\n }\n const depth = this.depth + 1;\n if (depth > this.maxDepth) {\n throw new Error(\n `Nested agent runs are ${this.maxDepth} deep at most and this one would be ${depth}: ${chain.join(\" -> \")}. Raise maxDepth on the agent at the root of the run if the tree is meant to be this deep.`,\n );\n }\n\n const label = params.label;\n const signingPath = [...this.pathPrefix, call.toolCallId];\n // Inherited downwards and never relaxed: a caller that asked for an\n // autonomous sub-agent must not have a question surface from two levels\n // below it, and only the subtree refusing can promise that.\n const onPending = this.onPending === \"deny\" ? \"deny\" : (params.onPending ?? \"escalate\");\n\n const resuming = recorded !== undefined;\n const open = resuming ? openCallIds(recorded.messages) : new Set<string>();\n // Only the answers this sub-run can actually attach to a call of its own,\n // or route further down. Handing it the rest would make it report a result\n // for a call nobody made.\n const mine = resuming\n ? answers.filter((answer) => {\n const below = pathBelow(answer.path, signingPath);\n if (below === null) return false;\n return open.has(below.length > 0 ? below[0] : answer.toolCallId);\n })\n : [];\n\n const sub = agent.stream({\n // A resume starts from what was persisted, not from what the tool passed\n // this time: the body ran again from the top and rebuilt its `prompt`,\n // and honouring it would replay a first turn the sub-agent has already\n // had. The persisted transcript already contains it.\n messages: resuming ? recorded.messages : (params.messages ?? []),\n turn: resuming ? { toolResults: mine } : params.prompt ? { text: params.prompt } : undefined,\n req: this.params.req,\n // The same scope, down the whole tree. A sub-agent runs on behalf of the\n // caller who started the parent — that is the only reason it is allowed\n // to run at all — so it reads and writes the caller's attachments, and a\n // tool three levels down can be handed an id its parent parked. There is\n // nothing to widen here and nothing to narrow: a second scope would be a\n // second answer to a question the request already answered once.\n attachments: this.params.attachments,\n // Inherited for the same reason the scope above is: a sub-agent is part\n // of the turn its parent is serving, so the fields the client sent with\n // that turn are as much its context as the parent's. A generator run\n // through `runAgent` whose tools could not see `pageId` would have to be\n // passed it through the prompt, as text, for the model to copy back.\n body: this.params.body,\n // Inherited, not new: this is what makes the parent's `stop()` reach a\n // sub-run three levels down without anything in between forwarding it.\n signal: this.controller.signal,\n threadId: this.params.threadId,\n instructions: params.instructions,\n maxOutputTokens: params.maxOutputTokens,\n temperature: params.temperature,\n // Kept across turns so the transcript the client already has keeps its\n // identity when the run continues.\n runId: resuming ? recorded.runId : undefined,\n nesting: {\n depth,\n maxDepth: this.maxDepth,\n chain,\n signingRunId: this.signingRunId,\n signingPath,\n onPending,\n },\n }) as AgentRun;\n\n let asked: PendingToolCall[] = [];\n const forwarding: Promise<void> = (async () => {\n for await (const event of sub as AsyncIterable<AgentStreamEvent>) {\n if (event.type === \"awaiting-input\") asked = event.pending;\n this.emit({\n type: \"nested-event\",\n toolCallId: call.toolCallId,\n runId: sub.runId,\n agent: agent.name,\n label,\n event,\n });\n }\n })();\n\n // Recording is its own promise so that the abort path can wait for exactly\n // this — the transcript reaching the tool call — rather than for the whole\n // tool, which may be ignoring the signal.\n const recording = (async () => {\n const result = await sub.result();\n await forwarding;\n // The seed leads, because a run only reports the messages it *made* and\n // `params.messages` is not one of them. Recording the transcript without\n // its opening is two bugs: a resume re-enters the sub-agent with the\n // conversation it was started from missing, and the replay check below\n // has nothing to fingerprint the seed against. Upserted rather than\n // concatenated because the sub-run may have amended one of these on its\n // way through.\n const messages = resuming\n ? mergeMessages(recorded.messages, result.messages)\n : mergeMessages(params.messages ?? [], result.messages);\n const record: NestedRun = {\n runId: sub.runId,\n agent: agent.name,\n label,\n messages,\n finishReason: result.finishReason,\n usage: resuming ? addUsage(recorded.usage ?? emptyUsage(), result.usage) : result.usage,\n // A parked record is what the next turn re-enters the tool on, and in\n // stateless mode it comes back from the browser. Signed here, by the\n // run that knows it is true, over where the sub-run parked, what it is\n // waiting on and the input the tool was running with — `parkedBelow`\n // will not act on a record without it. `call.input` is the parsed\n // value by now, on every path that reaches here, so the transcript\n // carries exactly what was signed.\n ...(result.finishReason === \"awaiting-input\"\n ? {\n signature: signNestedRun({\n runId: this.signingRunId,\n path: signingPath,\n nestedRunId: sub.runId,\n open: [...openCallIds(messages)],\n input: call.input,\n }),\n }\n : {}),\n };\n // Written before anything below can throw. An escalation is a pause, not\n // a lost run, and a cancelled sub-run is still work the user should be\n // able to read — both of those depend on the transcript already being on\n // the part when the throw happens.\n write(record);\n // Only what this turn actually spent. A memoized sub-run adds nothing,\n // above, because the turn that ran it already counted it.\n this.usage = addUsage(this.usage, result.usage);\n return { result, record };\n })();\n\n const settling = recording.then(\n () => undefined,\n () => undefined,\n );\n this.nestedSettling.add(settling);\n let result: AgentRunResult<ToolShapes, unknown>;\n let record: NestedRun;\n try {\n ({ result, record } = await recording);\n } finally {\n this.nestedSettling.delete(settling);\n }\n\n if (this.controller.signal.aborted) {\n // The sub-run was cancelled by the parent's `stop()`. Failing the tool\n // rather than returning an aborted result is what stops the tool body\n // from carrying on with half an answer while the run around it is dying.\n throw new RunAborted();\n }\n\n if (result.finishReason === \"awaiting-input\") {\n if (asked.length === 0) {\n // Nothing to ask means nothing the client could answer, and escalating\n // an empty list would end the parent awaiting-input with a tool call\n // that can never be resolved.\n throw new Error(\n `\"${agent.name}\" ended awaiting input but asked nothing, so there is no question to escalate.`,\n );\n }\n // The signature is on the server's copy of the record, and a stateless\n // client has built its own from the forwarded events. Re-sending the\n // call is what puts the server's copy in the client's hands to carry\n // back: the reducer takes a re-sent `nested` as authoritative, so this\n // replaces what the client accumulated rather than adding to it. Marked\n // `resent` because it is not a call: the model made this one earlier —\n // in this run or, on a re-park, in a previous one — and a hook that\n // counts calls has already seen it.\n this.emit({ type: \"tool-call\", messageId, part: call, resent: true });\n throw new PendingEscalation({ pending: asked, path: signingPath, nested: record });\n }\n\n return {\n runId: record.runId,\n agent: agent.name,\n messages: record.messages,\n finishReason: result.finishReason,\n usage: record.usage ?? emptyUsage(),\n output: result.output ?? outputOf(record.messages),\n nested: record,\n };\n }\n\n private addResult(message: AgentMessage, part: ToolResultPart) {\n message.content.push(part);\n this.emit({ type: \"tool-result\", messageId: message.id, part });\n }\n\n // --- files a tool made -------------------------------------------------\n\n /**\n * The `ctx.attachments` given to one tool call.\n *\n * Built per call rather than per run for one reason, and it is the same\n * reason `runAgent` is: the memo. A `put` has to be recognisable on the next\n * turn as the same `put`, and the only address a re-entered body has is \"the\n * Nth attachment of this tool call\" — so the object that counts them has to\n * belong to the call, not to the run.\n */\n /**\n * The file half of a tool's context: `ctx.attachments`, `ctx.generateImage`\n * and `ctx.editImage`.\n *\n * BUILT TOGETHER BECAUSE THEY SHARE ONE MEMO, and sharing it is not an\n * optimization. `ToolCallPart.attachments` is a list indexed by the order the\n * puts happened in, and a generated image *is* a put — so two `ReplayMemo`s\n * over the same list would each start at zero and hand out the same slots,\n * and a tool that both stored a file and generated one would replay the wrong\n * record for each. One list, one cursor.\n */\n private toolFiles(call: ToolCallPart): {\n attachments: ToolAttachments;\n generateImage: ToolContext[\"generateImage\"];\n editImage: ToolContext[\"editImage\"];\n } {\n const scoped = this.params.attachments ?? null;\n const memo = new ReplayMemo<ToolAttachmentRecord>(\n () => call.attachments,\n () => (call.attachments = []),\n // A put that threw took an index and wrote nothing, and if a later put in\n // the same call succeeds the gap has to be something rather than a hole:\n // `[null, { attachment }]` is what a hole becomes on the wire and in the\n // store, and it is what a UI or an app walking the memo crashes on. The\n // slot is re-attempted on a replay — `put` treats a failed record as no\n // record — so a transient failure heals itself on the turn the call\n // finally settles, and a permanent one stays legible as \"this put did not\n // produce a file\" instead of pretending it produced nothing at all.\n () => ({ failed: true }),\n );\n\n /**\n * The scope, or a sentence saying why there isn't one.\n *\n * A request with no subject gets no attachments at all — that is #489's\n * rule and this is where a tool meets it. The throw lands in `executeTool`,\n * which turns it into a `tool_error` the model reads, so the model is told\n * the tool cannot store files rather than being told nothing while the tool\n * quietly answers an id for bytes nobody kept. The message names the hook\n * an app has to override, because the reader who can act on it is the\n * developer looking at the transcript, not the model.\n */\n const scopeOrThrow = (): ScopedAttachments => {\n if (!scoped) {\n throw new InvalidAttachmentScopeError(\n \"This request has no attachment scope, so a tool cannot store or read files on it. `attachmentScope()` returned null — put the chat route behind authentication, send a `threadId`, or override `attachmentScope()` on the controller.\",\n );\n }\n return scoped;\n };\n\n /**\n * Everything a filled slot needs, with nothing of the memo in it.\n *\n * Shared by `put` and by the two image calls, which differ only in where the\n * bytes came from and in whether a `generated` record rides along. The\n * upload-then-store ordering, the `fileInput` refusal and the minted message\n * id are one copy for all three.\n */\n const park = async (\n blob: Blob,\n params: PutAttachmentParams,\n generated?: ToolAttachmentPut[\"generated\"],\n ): Promise<ToolAttachmentPut> => {\n const attachments = scopeOrThrow();\n const mimeType = params.mimeType || blob.type || \"\";\n // A name is settled here rather than left to `ScopedAttachments.put`,\n // which falls back to the attachment id. The provider's upload wants a\n // filename, the injected `FilePart` carries one, and the stored record\n // has one — three copies that have to agree, so there is one value.\n const name = params.name ?? (blob instanceof File ? blob.name : \"attachment\");\n\n let fileId: string | undefined;\n if (params.showModel) {\n const provider = this.config.provider;\n if (!provider.capabilities.fileInput) {\n // Refused before a byte moves. `toResponsesInput` drops a file part\n // for a provider that cannot read one, so without this the tool\n // pays for an upload, stores a record claiming the model was shown\n // the file, and the model answers about an image that never reached\n // the wire — with nothing in the transcript, the logs or the bill\n // saying which of those three things went wrong.\n throw new Error(\n `\"${String(call.name)}\" asked to show a file to ${provider.model}, which does not accept file input. Drop \\`showModel\\` for this provider, or run this agent on a model that takes files — \\`capabilities.fileInput\\` is what says which do.`,\n );\n }\n // The provider first, storage second — the opposite of `upload`'s\n // order in `AgentController`, deliberately.\n //\n // That route stores first because either failure fails the request\n // and the only question is whose orphan it becomes. Here there is a\n // second question and it decides: the record this writes claims\n // `destination: \"both\"`, and a record cannot claim the vendor has a\n // copy before the vendor says so. Uploading first also means a file\n // the vendor refuses — a type it will not take, a size over its cap —\n // costs no storage write at all, and `showModel` is exactly the path\n // where that refusal is likeliest. The orphan when storage fails\n // afterwards is a file at the vendor with no record here, which is\n // the same orphan `AgentController.upload` accepts in the other\n // direction.\n fileId = await provider.upload(new File([blob], name, { type: mimeType || undefined }));\n }\n\n const attachment = await attachments.put(blob, { name, mimeType, fileId });\n return {\n attachment,\n ...(generated ? { generated } : {}),\n ...(fileId\n ? {\n shown: {\n fileId,\n // Minted here and written down, not derived later. The next\n // turn replays this record and has to produce the same\n // message — same id, same timestamp — or a reattached client\n // and a live one hold two copies of one image.\n messageId: `msg_${crypto.randomUUID()}`,\n createdAt: new Date().toISOString(),\n },\n }\n : {}),\n };\n };\n\n /**\n * A slot that was filled by a different call than the one asking for it now.\n *\n * The sequence of file calls inside a tool body is the memo's only key, so a\n * body that stored a file on the first attempt and generates one at the same\n * index on the replay has changed in a way nothing can reconcile — and the\n * failure without this check is silent and expensive: `generateImage` would\n * hand back an attachment nobody rendered, or `put` would return the id of a\n * generated image in place of the bytes it was given.\n */\n const assertKind = (recorded: ToolAttachmentPut, wanted: \"put\" | \"image\", at: number) => {\n const was = recorded.generated ? \"image\" : \"put\";\n if (was === wanted) return;\n throw new Error(\n `Attachment ${at} of \"${String(call.name)}\" was ${was === \"image\" ? \"a generated image\" : \"a stored file\"} on the first attempt and is ${wanted === \"image\" ? \"a generated image\" : \"a stored file\"} now. ` +\n `ctx.attachments.put, ctx.generateImage and ctx.editImage share one memo indexed by call order within a tool call, so the sequence has to be the same every time this tool runs. Branch on ctx.resumed if it cannot be.`,\n );\n };\n\n const runImage = async (\n model: ImageModel,\n params: ToolGenerateImageParams,\n render: () => Promise<GeneratedImage>,\n ): Promise<GeneratedAttachment> => {\n const { at, recorded, write } = memo.next();\n\n // Read BEFORE the render, which is the whole point of memoizing this\n // rather than letting it fall through to `ctx.attachments.put`. `put`\n // checks its memo when it is called, and by then the image has been\n // rendered and paid for — its own docblock says so: \"Work before a `put`\n // still runs again.\" Here the expensive part is the work before.\n if (recorded && \"attachment\" in recorded) {\n assertKind(recorded, \"image\", at);\n if (recorded.shown) this.queueShown(call.toolCallId, recorded);\n return {\n attachment: recorded.attachment,\n size: recorded.generated!.size,\n mimeType: recorded.attachment.mimeType,\n usage: recorded.generated!.usage,\n };\n }\n\n const image = await render();\n // Counted against the run exactly as a nested agent's is, so\n // `AgentRunResult.usage` is the whole cost of the turn and not only its\n // text. `imageInputTokens` keeps the image share legible inside it.\n this.usage = addUsage(this.usage, image.usage);\n\n const extension = image.mimeType === \"image/jpeg\" ? \"jpg\" : \"png\";\n const record = await park(\n image.image,\n {\n name: params.name ?? `${model.name}.${extension}`,\n mimeType: image.mimeType,\n ...(params.showModel ? { showModel: true } : {}),\n },\n { size: image.size, usage: image.usage },\n );\n\n write(record);\n if (record.shown) this.queueShown(call.toolCallId, record);\n return {\n attachment: record.attachment,\n size: image.size,\n mimeType: image.mimeType,\n usage: image.usage,\n };\n };\n\n /** An attachment id resolves through the scope; bytes pass through. */\n const asInput = async (input: ImageInput | string): Promise<ImageInput> =>\n typeof input === \"string\" ? await scopeOrThrow().file(input) : input;\n\n return {\n generateImage: (model, params) =>\n runImage(model, params, () =>\n model.generate({ ...params, signal: this.controller.signal }),\n ),\n\n editImage: (model, params) =>\n runImage(model, params, async () => {\n // `images` and `mask` are peeled off rather than spread over: both\n // may be attachment ids here and neither is one by the time it\n // reaches `ImageModel`, so letting the originals through would type\n // as `string | Blob` and ship an id where bytes belong.\n const { images, mask, ...rest } = params;\n return await model.edit({\n ...rest,\n images: (await Promise.all(images.map(asInput))) as [ImageInput, ...ImageInput[]],\n ...(mask ? { mask: await asInput(mask) } : {}),\n signal: this.controller.signal,\n });\n }),\n\n attachments: {\n get: (id: string) => scopeOrThrow().get(id),\n read: (id: string) => scopeOrThrow().read(id),\n file: (id: string) => scopeOrThrow().file(id),\n put: async (blob: Blob, params: PutAttachmentParams = {}): Promise<Attachment> => {\n const { at, recorded, write } = memo.next();\n\n // A `failed` record is a slot an earlier turn took and could not fill,\n // so there is nothing to replay and the work is done again.\n if (recorded && \"attachment\" in recorded) {\n assertKind(recorded, \"put\", at);\n const mismatch = putMismatch(recorded, blob, params);\n if (mismatch) {\n throw new Error(\n `Attachment ${at} of \"${String(call.name)}\" ${mismatch}. ` +\n `ctx.attachments.put is memoized by call index within a tool call, so a body whose put calls depend on a condition that changed between turns cannot be replayed — the model would be shown a file under an id that names different bytes. ` +\n `Make the sequence of put calls the same every time this tool runs, or branch on ctx.resumed.`,\n );\n }\n // Nothing is stored and nothing is uploaded. What IS repeated is the\n // queueing: the first attempt escalated before its result attached,\n // so the message was queued and never flushed, and this turn is the\n // one where the call finally settles. `settleShown` refuses a message\n // id the history already holds, which is what makes queueing twice\n // safe rather than merely unlikely.\n if (recorded.shown) this.queueShown(call.toolCallId, recorded);\n return recorded.attachment;\n }\n\n const record = await park(blob, params);\n // The slot is filled only now, with everything that could throw behind\n // it: a put that fails leaves the tool call exactly as it found it.\n write(record);\n if (record.shown) this.queueShown(call.toolCallId, record);\n return record.attachment;\n },\n },\n };\n }\n\n /**\n * The `ctx.turn` given to one tool call. The reasoning is on `ToolTurn`.\n *\n * Anchored on `messageId`, the assistant message holding the call, which is\n * the same id on a re-entry: `amend` replaces the history entry with a clone\n * under the original's id, so the lookup is by id rather than by reference.\n * A message that is not in the history at all gets an empty list rather than\n * a guess.\n */\n private toolTurn(messageId: string): ToolTurn {\n const at = this.history.findIndex((message) => message.id === messageId);\n const injected = injectedMessageIds(this.history);\n let user: AgentMessage | undefined;\n for (let i = at - 1; i >= 0; i--) {\n const message = this.history[i];\n if (message.role !== \"user\" || injected.has(message.id)) continue;\n user = message;\n break;\n }\n const ids: string[] = [];\n for (const part of user?.content ?? []) {\n if (part.type !== \"file\") continue;\n // The prefix test `providers/request.ts` applies before telling the model\n // an id. A stateless client's history can put anything here, and a value\n // the model was never shown as an id should not reach a tool as one.\n const id = part.attachmentId;\n if (typeof id !== \"string\" || !id.startsWith(ATTACHMENT_ID_PREFIX)) continue;\n if (!ids.includes(id)) ids.push(id);\n }\n return Object.freeze({ attachments: Object.freeze(ids) });\n }\n\n /**\n * Holds the message for a shown file until its tool call settles.\n *\n * WHY IT WAITS. The message is input-role and it has to sit *after* the tool\n * call's result, because that is the order it happened in and the order the\n * provider validates: a `function_call` and its `function_call_output` are a\n * pair, and a user message wedged between them is a history the API rejects.\n * Emitting it the moment `put` returns would do exactly that, since the tool\n * is still running.\n *\n * WHY A TOOL THAT ESCALATED GETS NOTHING. Its call has no result yet, so\n * flushing would leave an image in the transcript attached to a call that has\n * not finished — and the next turn re-enters the body, replays the `put` and\n * would queue a second copy. The queue simply dies with the run; the record\n * on the tool call survives, and the turn that finally settles the call is\n * the turn that shows the file.\n */\n private queueShown(toolCallId: string, record: ToolAttachmentPut) {\n const shown = record.shown;\n if (!shown) return;\n const queued = this.shownQueue.get(toolCallId) ?? [];\n if (queued.some((message) => message.id === shown.messageId)) return;\n queued.push({\n id: shown.messageId,\n role: \"user\",\n content: [\n {\n type: \"file\",\n fileId: shown.fileId,\n name: record.attachment.name,\n mimeType: record.attachment.mimeType,\n // Shown to the model beside the file (see `attachmentLine` in\n // `providers/request.ts`), so a file one tool made can be the input\n // of the next. Not what marks the part as injected — a user's upload\n // carries one too; `historyForProvider` keys on the message id.\n attachmentId: record.attachment.id,\n },\n ],\n createdAt: shown.createdAt,\n // Complete the instant it is made. Without this the client's `run-end`\n // safety net would find a message with no finish reason and stamp one on,\n // which is a difference between a live client and a reattached one over a\n // message that was never streaming in the first place.\n finishReason: \"stop\",\n });\n this.shownQueue.set(toolCallId, queued);\n }\n\n /**\n * The tool call settled: its files go into the transcript now.\n *\n * Pushed to `history` (so the next step sees them), to `produced` (so\n * `result().messages` carries them and the controller persists them), emitted\n * (so a client watching live sees the same conversation a reattached one\n * replays), and queued for `onMessage` (so an app that persists from the hook\n * stores it). A message that is in some of those four and not the others is\n * the bug class #470 is about, and an injected message is the easiest place in\n * the codebase to write it.\n *\n * QUEUED FOR THE HOOK RATHER THAN REPORTED HERE, so that all four agree on\n * ORDER and not merely on contents. The assistant message that made this tool\n * call has not been finalized yet — `loop` does that after `runTools`\n * returns — so calling `onMessage` from here would hand an app the file\n * before the turn that produced it. `reportDeferred` is where the queue is\n * drained, immediately after that assistant message is reported.\n *\n * The id guard is for a history that already holds the message — a stateless\n * client posting back a transcript that contains the injection *and* still\n * shows the call as open, which is a rewind the server cannot rule out. One\n * copy either way.\n *\n * NOTHING IS SHOWN ONCE THE RUN IS OVER, and the check belongs here rather than\n * at the three call sites because one of those sites fires after the run has\n * finished. `finalizeAborted` never calls this — it denies every open call\n * instead — but a `/stop` does not wait for a tool that is already running:\n * `raceAbort` in `runTools` returns the moment the signal fires, the run\n * finalizes and ends, and the tool's own `.then` lands afterwards and calls\n * this. Without the guard the message goes into `history` and `produced` and\n * through `onMessage` while `emit` is already a no-op, so it is persisted and\n * never announced, and a client that reloads the thread sees an image a client\n * that watched it live never saw. That is the exact divergence this issue\n * asked to avoid, arrived at from the one direction nobody looks. Appending an\n * image to a conversation the user has just cancelled would also be the run\n * getting the last word. The bytes are kept and the record is on the tool\n * call, so a tool that runs again can still resolve the id; nothing is lost\n * but the showing.\n */\n private async settleShown(toolCallId: string): Promise<void> {\n const queued = this.shownQueue.get(toolCallId);\n if (!queued || queued.length === 0) return;\n this.shownQueue.delete(toolCallId);\n if (this.ended || this.controller.signal.aborted) return;\n for (const message of queued) {\n if (this.history.some((held) => held.id === message.id)) continue;\n this.history.push(message);\n this.produced.push(message);\n this.emit({ type: \"message\", message });\n this.unreported.add(message);\n }\n }\n\n /**\n * The history as the provider sees it: everything, minus all but the most\n * recent tool-produced file.\n *\n * THE PROBLEM. `buildResponsesRequest` is handed the whole history on every\n * step, so a file injected at step two is re-sent at steps three, four and\n * five. An edit loop that iterates three times therefore pays for three\n * images on every later call — image tokens are not small, and they are\n * charged again each step — on top of one provider upload per iteration. Left\n * alone this is a cost that grows with the square of the loop and shows up on\n * an invoice rather than in a stack trace.\n *\n * WHAT IS TRIMMED, AND WHERE. Here, on the way to the provider, and nowhere\n * else. The transcript keeps every injected message: `history`, `produced`,\n * `onMessage`, the stream, and the client all hold the same conversation they\n * would have held without this method, so a `/attach` replay still matches a\n * live stream and a user scrolling back still sees every version the agent\n * made. Trimming the stored transcript instead would have meant editing a\n * message after it was persisted and announced, which is the one thing the\n * message contract does not allow.\n *\n * WHAT IT COSTS, AND IT IS A REAL CAPABILITY. With a window of one, the model\n * cannot compare this iteration against the last one. \"Is this closer than\n * the previous attempt?\" is a question it can no longer answer from what it\n * can see, and an agent whose job is to converge on a target by comparison\n * genuinely wants two. One is the default because the failure of too small a\n * window is visible and cheap — the model says it cannot see the earlier\n * image, in the transcript, on the first run — while the failure of too large\n * a one is a bill nobody reads until the end of the month. The dropped part\n * is replaced by a line of text rather than removed, so the model is told the\n * image existed and why it is gone; a hole would leave it to conclude it had\n * imagined seeing anything.\n *\n * IT IS A CONSTANT, NOT A KNOB, ON PURPOSE. A configurable window is one line\n * to add later and cannot be taken back once apps depend on it, and nobody\n * has a second value to name yet. Only files the run injected are counted —\n * a file the *user* attached is one they expect to stay attached, and\n * dropping it would be the agent losing the thing it was asked about.\n */\n private historyForProvider(current: AgentMessage): AgentMessage[] {\n const messages = this.history.filter((message) => message !== current);\n\n // Injected messages are named by the tool call that made them, and that\n // record is the test — not `attachmentId` on the part. A user's own upload\n // carries an `attachmentId` too: `ingestTurn` copies it from `turn.files`,\n // and `useChat` spreads the entry onto its local user message.\n const injected = injectedMessageIds(messages);\n\n const shown: FilePart[] = [];\n for (const message of messages) {\n if (!injected.has(message.id)) continue;\n for (const part of message.content) {\n if (part.type === \"file\") shown.push(part);\n }\n }\n if (shown.length <= SHOWN_FILE_WINDOW) return messages;\n\n const dropped = new Set(shown.slice(0, shown.length - SHOWN_FILE_WINDOW));\n return messages.map((message) => {\n if (!message.content.some((part) => part.type === \"file\" && dropped.has(part))) {\n return message;\n }\n return {\n ...message,\n content: message.content.map((part) =>\n part.type === \"file\" && dropped.has(part)\n ? {\n type: \"text\" as const,\n text: `[A ${part.mimeType || \"file\"} produced by a tool (attachment ${part.attachmentId}) was attached here and has been dropped from this request: only the most recent tool-produced file is kept attached, to keep the context bounded. Call the tool again if you need to look at it.]`,\n }\n : part,\n ),\n };\n });\n }\n\n // --- the client's turn -------------------------------------------------\n\n /**\n * Resolves what the client sent back, then adds its words.\n *\n * Every pending call has to come out of this with a result — signed, refused\n * or implicitly denied. The provider rejects a history holding a tool call\n * with no result, so leaving one open would break not this turn but the next\n * one, at a point where the cause is no longer visible.\n */\n private async ingestTurn(): Promise<PendingToolCall[]> {\n const turn = this.params.turn;\n const open = this.openCalls();\n /** Questions a re-entered tool asked again. The run ends on these. */\n const escalated: PendingToolCall[] = [];\n\n if (open.length > 0) {\n const answered = new Set<string>();\n const seen = new Set<string>();\n /** Answers addressed *below* one of this run's tool calls, grouped by the\n * call that has to be re-entered to deliver them. */\n const reentry = new Map<string, ClientToolResult[]>();\n\n for (const answer of turn?.toolResults ?? []) {\n // One answer per call, first one wins. A turn carrying the same entry\n // twice is a retried submit or a double-clicked form, and without this\n // it ran the approved tool twice and left two results for one\n // toolCallId — a history the provider rejects, arrived at by exactly\n // the machinery that exists to keep the history well formed.\n //\n // Keyed by path *and* id, because a tool-call id is only unique within\n // one run: two sub-agents under two different tools each number their\n // calls from their own provider, and dropping the second as a duplicate\n // would strand the tool that was waiting on it. For a top-level answer\n // the key is the id, exactly as before.\n const key = `${(answer.path ?? []).join(\"/\")}#${answer.toolCallId}`;\n if (seen.has(key)) continue;\n seen.add(key);\n\n const reject = (message: string) =>\n this.emit({\n type: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message,\n toolCallId: answer.toolCallId,\n retryable: false,\n },\n });\n\n // The path says which run the answer belongs to; `toolCallId` only says\n // which call *within* that run. Both are covered by the signature, so a\n // client that moves an answer to another tool's sub-run does not\n // redirect anything — it routes the answer somewhere the MAC no longer\n // verifies, which is the property that makes carrying the path safe.\n const below = pathBelow(answer.path, this.pathPrefix);\n if (below === null) {\n reject(\n `The answer for \"${answer.toolCallId}\" is addressed to a tool call this run is not inside.`,\n );\n continue;\n }\n\n if (below.length > 0) {\n const host = below[0];\n const hosting = open.find((entry) => entry.call.toolCallId === host);\n if (!hosting) {\n reject(`No pending tool call with id \"${host}\" to deliver a nested answer to.`);\n continue;\n }\n // THE ONE CHECK THAT MAKES RE-ENTRY SAFE, and the reason it is here\n // rather than in `reenter`.\n //\n // Re-entry runs the tool. Everything else in this method verifies a\n // signature first, but a path cannot be verified here — the claims\n // are the *inner* call's, and only the run that minted them knows its\n // tool, its `kind` and its input, which is why `resolveAnswer` runs\n // down there and not up here. So the decision to execute has to be\n // gated on something the server derived instead: there must be a\n // sub-run parked on this exact call, and it must be waiting on the\n // exact question the answer names. Without this, a turn that posts\n // `{ path: [<any open call>], signature: \"\" }` re-enters a tool that\n // is merely awaiting an *approval* — running, unapproved, a call the\n // user was shown and never said yes to, with its input taken from a\n // client-carried history. Content is still checked below, in the\n // sub-run; this is what stops an unsigned request from choosing to\n // execute at all.\n const parked = this.parkedBelow(hosting.call, below, answer.toolCallId);\n if (parked.ok === false) {\n const under = `The answer for \"${answer.toolCallId}\" is addressed under \"${host}\", which`;\n reject(\n parked.reason === \"unparked\"\n ? `${under} has no sub-agent run waiting on that question.`\n : parked.reason === \"expired\"\n ? `${under} parked a sub-agent run that has since expired. Ask again.`\n : `${under} carries a record of a parked sub-agent run the server did not sign.`,\n );\n continue;\n }\n let group = reentry.get(host);\n if (!group) {\n // Spent here, ahead of the body, for the reason the record is\n // signed at all: re-entry runs the tool before the sub-run gets to\n // refuse a spent answer, so a history rewound to before the result\n // would run it once per replay. Once per record per turn — the\n // sub-run may have asked two things at once, and every answer to\n // it re-enters the same tool a single time.\n if (!consumeNestedRun(parked.signature)) {\n reject(\n `The answer for \"${answer.toolCallId}\" is addressed under \"${host}\", which has already been re-entered on that record. Ask again.`,\n );\n continue;\n }\n group = [];\n reentry.set(host, group);\n // Marked answered so the refusal pass below leaves it alone: the\n // tool is about to be re-entered and will produce the real result.\n answered.add(host);\n }\n group.push(answer);\n continue;\n }\n\n const target = open.find((entry) => entry.call.toolCallId === answer.toolCallId);\n if (!target) {\n // Nothing to attach it to, so it cannot be told to the model even as\n // an error part — a result for a call that was never made.\n reject(`No pending tool call with id \"${answer.toolCallId}\".`);\n continue;\n }\n\n const result = await this.resolveAnswer(target, answer, escalated);\n if (result === null) continue;\n answered.add(answer.toolCallId);\n // `\"open\"` is an approved tool whose own sub-agent asked something on\n // the way through: answered, so the refusal pass leaves it alone, but\n // no result attaches — the call stays open and the next turn re-enters\n // it, exactly as an escalation from the step loop does. Nothing it\n // showed is flushed either, for the same reason: the call has not\n // settled, so the file waits for the turn where it does.\n if (result !== \"open\") {\n this.attachToHistory(target, result);\n await this.settleShown(answer.toolCallId);\n }\n }\n\n for (const [host, answers] of reentry) {\n const entry = open.find((item) => item.call.toolCallId === host)!;\n const result = await this.reenter(entry, answers, escalated);\n if (result) {\n this.attachToHistory(entry, result);\n // The turn a re-entered tool finally settles on is the turn its files\n // are shown, however many turns ago it stored them.\n await this.settleShown(host);\n }\n }\n\n for (const entry of open) {\n if (answered.has(entry.call.toolCallId)) continue;\n // The turn said something else. That is a refusal — the honest reading,\n // and the only one that cannot strand the thread. A tool whose\n // sub-agent asked a question and did not get an answer is refused here\n // like any other: the sub-run is abandoned with its transcript intact,\n // and the call gets a result rather than dangling into the next turn.\n this.attachToHistory(entry, {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"denied\",\n cause: \"refused\",\n });\n }\n\n await this.reportDeferred();\n }\n\n if (turn && (turn.text || (turn.files && turn.files.length > 0))) {\n const message: AgentMessage = {\n id: `msg_${crypto.randomUUID()}`,\n role: \"user\",\n content: [\n ...(turn.text ? [{ type: \"text\" as const, text: turn.text }] : []),\n // Field by field rather than spread: an entry can carry whatever\n // `attach()` answered (`downgraded`, `size`), and only these four\n // mean anything on a `FilePart`. `attachmentId` is among them — it is\n // what lets the model name this file to a tool — and an absent\n // `fileId` is a storage-only upload, left out rather than written as\n // `undefined`.\n ...(turn.files ?? []).map((file) => ({\n type: \"file\" as const,\n ...(file.fileId ? { fileId: file.fileId } : {}),\n name: file.name,\n mimeType: file.mimeType,\n ...(file.attachmentId ? { attachmentId: file.attachmentId } : {}),\n })),\n ],\n createdAt: new Date().toISOString(),\n finishReason: \"stop\",\n };\n this.history.push(message);\n this.produced.push(message);\n await this.report(message);\n }\n\n return escalated;\n }\n\n /**\n * Whether a sub-run under `call` is actually parked on the question named.\n *\n * The transcript is the server's own record of where the run stopped:\n * `nested` is written from the sub-run's result before the escalation throws,\n * so a call that has never nested has no `nested` at all, and one whose\n * sub-runs all finished has none with `awaiting-input`. In stateless mode\n * that record arrives from the client and could say anything, and what\n * saying it buys is not small: the tool body runs — from the top, with the\n * input the history carries — before the sub-run gets to verify the answer,\n * so everything the body does ahead of its first `runAgent` happens on the\n * client's say-so. Which is why the record has to carry the server's\n * signature over the sub-run it parked, the calls it left open and the\n * input the tool was given, and why a record without one, or with one that\n * does not match what it now says, is not a parked run at all.\n *\n * The failure says which of those it was. \"Expired\" is an ordinary outcome\n * in threaded mode — a question answered a day late — and telling that\n * user the run never parked would send them looking for a bug that is not\n * there; a record that fails its MAC is the other thing entirely, and the\n * two are kept apart for the same reason `resolveAnswer` keeps them apart.\n *\n * `below` is the answer's path with this run's prefix already removed, so\n * `below[0]` is `call` itself and `below[1]`, when there is one, names the\n * call to re-enter one level further down.\n */\n private parkedBelow(\n call: ToolCallPart,\n below: string[],\n toolCallId: string,\n ): { ok: true; signature: string } | { ok: false; reason: \"unparked\" | \"unsigned\" | \"expired\" } {\n const wanted = below.length > 1 ? below[1] : toolCallId;\n const path = [...this.pathPrefix, call.toolCallId];\n const parked = (call.nested ?? []).find(\n (run) => run.finishReason === \"awaiting-input\" && openCallIds(run.messages).has(wanted),\n );\n if (!parked) return { ok: false, reason: \"unparked\" };\n if (typeof parked.signature !== \"string\") return { ok: false, reason: \"unsigned\" };\n const verified = verifyNestedRun(parked.signature, {\n path,\n nestedRunId: parked.runId,\n open: [...openCallIds(parked.messages)],\n input: call.input,\n });\n if (verified.ok === false) {\n return { ok: false, reason: verified.reason === \"expired\" ? \"expired\" : \"unsigned\" };\n }\n return { ok: true, signature: parked.signature };\n }\n\n /**\n * Re-enters a tool whose sub-agent asked the user something.\n *\n * From the top, with `ctx.resumed === true` — there is no other way. A JS\n * async generator cannot be suspended across a turn boundary, so the body\n * runs again and `runAgent` replays its finished sub-runs out of\n * `ToolCallPart.nested` instead of re-running them. Which means the code\n * *before* the escalating `runAgent` runs twice; that bargain is documented on\n * `ToolContext.runAgent` and it is the price of not needing a checkpoint API.\n */\n private async reenter(\n entry: { message: AgentMessage; call: ToolCallPart },\n answers: ClientToolResult[],\n escalated: PendingToolCall[],\n ): Promise<ToolResultPart | null> {\n const name = String(entry.call.name);\n const resolved = this.config.registry.get(name);\n if (!resolved || !resolved.tool.execute) {\n return {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message: `The tool \"${name}\" no longer exists, so the sub-agent's question cannot be delivered.`,\n toolCallId: entry.call.toolCallId,\n retryable: false,\n },\n };\n }\n\n // Checked the way `runTools` checked the model's arguments. The record's\n // signature has already said this is the input the tool parked on, so what\n // this catches is the tool itself having moved: a schema that changed\n // between the turn that parked and the turn that answers. The tool's typed\n // input is a contract with the tool as it is now, and a value the schema\n // rejects must not reach it — the failure is a result the model can read,\n // exactly like a mis-typed argument on the way in.\n const parsed = resolved.tool.inputSchema.safeParse(entry.call.input);\n if (parsed.ok === false) {\n return {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_input\",\n message: `Invalid arguments for \"${name}\": ${parsed.errors.join(\", \")}`,\n toolCallId: entry.call.toolCallId,\n retryable: true,\n },\n };\n }\n\n const call = this.amendCall(entry);\n // The parsed value is the only input the call has from here on, as in\n // `runTools` — the clone carries what the tool was actually given.\n call.input = parsed.value;\n try {\n // Step 0, like an approval executed on the way in: this belongs to the\n // turn, not to a step of the loop that has not started yet.\n return await raceAbort(\n this.executeTool(resolved, entry.message.id, call, parsed.value, 0, { answers }),\n this.controller.signal,\n );\n } catch (error) {\n if (error instanceof PendingEscalation) {\n // Asked again. No result attaches, so the call stays open and the next\n // turn re-enters it exactly as this one did.\n escalated.push(...error.pending);\n return null;\n }\n throw error;\n }\n }\n\n /**\n * Clones the tool-call part before a replay writes to its `nested`.\n *\n * The message holding it came from an earlier run and belongs to the caller's\n * `messages` array; the run must not reach back into its own input and change\n * it under a controller that has already persisted it. Cloning the part into\n * the amended copy is also what makes the updated sub-run transcript\n * something `onMessage` can report — see `reportDeferred`, which is why the\n * message is marked unreported here even though no result may ever attach.\n */\n private amendCall(entry: { message: AgentMessage; call: ToolCallPart }): ToolCallPart {\n const message = this.amend(entry.message);\n const at = message.content.indexOf(entry.call);\n const call: ToolCallPart = {\n ...entry.call,\n nested: (entry.call.nested ?? []).map((run) => ({ ...run })),\n // Cloned for the same reason `nested` is: the replayed body's memo writes\n // into this array, and the array on the original part belongs to the\n // caller's `messages`, which is an input and not scratch space.\n ...(entry.call.attachments\n ? { attachments: entry.call.attachments.map((record) => ({ ...record })) }\n : {}),\n };\n if (at >= 0) message.content[at] = call;\n this.unreported.add(message);\n return call;\n }\n\n /** Tool calls in the history with no result anywhere after them. */\n private openCalls(): { message: AgentMessage; call: ToolCallPart }[] {\n const resolvedIds = new Set<string>();\n for (const message of this.history) {\n for (const part of message.content) {\n if (part.type === \"tool-result\") resolvedIds.add(part.toolCallId);\n }\n }\n const open: { message: AgentMessage; call: ToolCallPart }[] = [];\n for (const message of this.history) {\n for (const part of message.content) {\n if (part.type === \"tool-call\" && !resolvedIds.has(part.toolCallId)) {\n open.push({ message, call: part });\n }\n }\n }\n return open;\n }\n\n /**\n * Verifies one answer and turns it into a result part, or reports why not.\n *\n * `null` means the call stays unanswered and falls through to the implicit\n * denial above — which is the right outcome for a bad signature: the model\n * must not see a result the server cannot vouch for. `\"open\"` means the\n * opposite: the answer was good, the tool ran, and it is now waiting on a\n * question of its own, so the call must stay open *without* being denied.\n */\n private async resolveAnswer(\n entry: { message: AgentMessage; call: ToolCallPart },\n answer: ClientToolResult,\n escalated: PendingToolCall[],\n ): Promise<ToolResultPart | \"open\" | null> {\n const call = entry.call;\n const name = String(call.name);\n const resolved = this.config.registry.get(name);\n const reject = (message: string) => {\n this.emit({\n type: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message,\n toolCallId: call.toolCallId,\n retryable: false,\n },\n });\n return null;\n };\n\n if (!resolved) {\n return reject(`The tool \"${name}\" no longer exists, so its answer cannot be checked.`);\n }\n const kind = pendingKind(resolved.tool);\n if (!kind) {\n return reject(`\"${name}\" is a server tool with no pending question.`);\n }\n if (typeof answer.signature !== \"string\" || answer.signature.length === 0) {\n return reject(`The answer for \"${name}\" carried no signature.`);\n }\n\n // The issuing run, read out of the token. The turn answering a pending call\n // is a *new* run with a new id, so the id the signature was made under has\n // to travel with the signature — and it is covered by the MAC, so a client\n // that edits it fails below rather than being believed.\n const issued = readSignature(answer.signature);\n if (!issued) {\n return reject(`The signature for \"${name}\" is malformed.`);\n }\n\n // The path is this run's own, not the one the client sent. The client's\n // copy was used to route the answer here and nothing else; recomputing the\n // MAC over what the server issued is what makes a moved answer fail instead\n // of being believed.\n const verified = verifyPendingCall(answer.signature, {\n ...this.claimsFor(call.toolCallId, name, kind, call.input),\n runId: issued.runId,\n });\n\n if (verified.ok === false) {\n return reject(\n verified.reason === \"expired\"\n ? `The approval for \"${name}\" has expired. Ask again.`\n : `The answer for \"${name}\" does not match the call the server made.`,\n );\n }\n\n // Verifying says the server once asked this exact question; spending the\n // nonce says nobody has answered it yet. Without this step a captured token\n // approves the same call every time it is presented — the client rewinds to\n // the history from before the result existed and replays, and the human who\n // approved once has approved forever.\n if (!consumePendingCall(answer.signature)) {\n return reject(`The answer for \"${name}\" has already been used. Ask again.`);\n }\n\n if (\"approve\" in answer) {\n if (kind !== \"approval\") {\n return reject(`\"${name}\" is answered by the client, not approved.`);\n }\n if (answer.approve === true) {\n // Raced against the abort signal exactly as `runTools` does. A tool\n // that does not honour `ctx.signal` must not be able to hold the run\n // open, and this is the one execution outside the loop — a `stop()`\n // landing here used to hang the run forever, which is precisely the\n // dangling state `stop()` exists to prevent.\n //\n // Step 0: the approval landed before this run took its first model\n // step, so it belongs to no step of this loop. `call.input` is the\n // value the signature covers — see `runTools`.\n //\n // Cloned first, for the same reason `reenter` clones: an approved tool\n // may nest, and `nestedRunner` writes `nested` onto the part it is\n // given. That part belongs to the caller's `messages` array, which is\n // an input and not scratch space — and the clone is what makes the\n // sub-run transcript something `onMessage` can report.\n const executing = this.amendCall(entry);\n try {\n return await raceAbort(\n this.executeTool(resolved, entry.message.id, executing, executing.input, 0),\n this.controller.signal,\n );\n } catch (error) {\n if (error instanceof PendingEscalation) {\n // An approval whose tool asked the user something of its own. The\n // step loop and `reenter` both collect this; without the same catch\n // here the rejection left `ingestTurn` and was normalized into a\n // `provider_error`, which ended the run with the sub-agent's\n // question thrown away and the approval's nonce already spent.\n escalated.push(...error.pending);\n return \"open\";\n }\n throw error;\n }\n }\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"denied\",\n cause: \"refused\",\n reason: answer.reason,\n };\n }\n\n if (\"output\" in answer) {\n if (kind === \"approval\") {\n // An approval is a tool the *server* runs. A client handing back its\n // output would be fabricating a result, not approving one.\n return reject(`\"${name}\" is approved, not answered: the server produces its result.`);\n }\n const schema = resolved.tool.outputSchema;\n const parsed = schema\n ? schema.safeParse(answer.output)\n : { ok: true as const, value: answer.output };\n if (parsed.ok === false) {\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"error\",\n error: {\n code: \"invalid_tool_result\",\n message: `The answer for \"${name}\" did not match its output schema: ${parsed.errors.join(\", \")}`,\n toolCallId: call.toolCallId,\n retryable: true,\n },\n };\n }\n return {\n type: \"tool-result\",\n toolCallId: call.toolCallId,\n name: call.name,\n status: \"ok\",\n output: parsed.value,\n };\n }\n\n return reject(`The answer for \"${name}\" carried neither an approval nor an output.`);\n }\n\n /**\n * Puts the result next to the call that asked for it.\n *\n * The message being amended came from an earlier run, so it is cloned before\n * it is touched — the caller's `messages` array is an input, not scratch\n * space, and a controller that persisted it would otherwise see it change\n * under it. The clone is reported through `onMessage` and returned in\n * `result()`, which is why a store keyed by message id has to upsert.\n */\n private attachToHistory(\n entry: { message: AgentMessage; call: ToolCallPart },\n result: ToolResultPart,\n ) {\n const message = this.amend(entry.message);\n message.content.push(result);\n this.unreported.add(message);\n this.emit({ type: \"tool-result\", messageId: message.id, part: result });\n }\n\n /** The clone of an earlier run's message that this run may write to. One per\n * message id, so two results for the same message do not fork it. */\n private amend(original: AgentMessage): AgentMessage {\n const existing = this.amended.get(original.id);\n if (existing) return existing;\n const message: AgentMessage = { ...original, content: [...original.content] };\n const index = this.history.indexOf(original);\n if (index >= 0) this.history[index] = message;\n this.produced.push(message);\n this.amended.set(original.id, message);\n return message;\n }\n\n /**\n * Persists the messages this run finished outside `finalizeMessage`, once each\n * and in the order it finished them.\n *\n * Called from the end of `ingestTurn`, from the abort path, and from\n * `finalizeMessage`. From the abort path because a stop that lands while an\n * approved tool is running has to persist the results that *did* attach this\n * turn — otherwise the work is done, the transcript on the stream shows it,\n * and the store never hears about it. From `finalizeMessage` because an\n * injected message has to reach `onMessage` after the assistant message it\n * sits below, and a `Set` preserves insertion order, which here is transcript\n * order.\n */\n private async reportDeferred(): Promise<void> {\n const pending = [...this.unreported];\n this.unreported.clear();\n for (const message of pending) await this.report(message);\n }\n\n // --- stopping ----------------------------------------------------------\n\n /**\n * The run's last act: leave a transcript the next turn can be built on.\n *\n * Everything the model asked for and did not get becomes a `denied` result\n * with `cause: \"stopped\"`, and the interrupted message is finalized as\n * `aborted` keeping whatever text it had produced. Both go out on the stream\n * and through `onMessage`. A cancel that merely stopped emitting would leave\n * a dangling tool call, and the provider would reject the history on the very\n * next message the user sent.\n */\n private async finalizeAborted(): Promise<void> {\n // Sub-runs first. They share this run's signal so they are already closing;\n // what is being waited for is the moment each writes its transcript onto\n // its tool call, because everything below this line persists messages.\n if (this.nestedSettling.size > 0) {\n await Promise.all([...this.nestedSettling]);\n }\n // Calls left open anywhere in the history, not just on the message this run\n // was building. A stop that lands while an approval this turn is executing\n // has no current message at all — the call belongs to an *earlier* turn's\n // message — and denying only `this.current` would leave that one dangling\n // in the very transcript this method exists to keep valid.\n for (const entry of this.openCalls()) {\n if (entry.message === this.current) continue;\n this.attachToHistory(entry, {\n type: \"tool-result\",\n toolCallId: entry.call.toolCallId,\n name: entry.call.name,\n status: \"denied\",\n cause: \"stopped\",\n reason: this.stopReason,\n });\n }\n // Results that did attach this turn have not been persisted yet: the report\n // pass at the end of `ingestTurn` is one of the things the abort skipped.\n await this.reportDeferred();\n\n const message = this.current;\n if (message) {\n const answered = new Set(\n message.content\n .filter((part): part is ToolResultPart => part.type === \"tool-result\")\n .map((part) => part.toolCallId),\n );\n for (const part of [...message.content]) {\n if (part.type !== \"tool-call\" || answered.has(part.toolCallId)) continue;\n // A call whose arguments were still arriving is finalized too: the\n // client has already been shown it, and an unresolved part is exactly\n // what this method exists to prevent.\n delete part.partial;\n this.addResult(message, {\n type: \"tool-result\",\n toolCallId: part.toolCallId,\n name: part.name,\n status: \"denied\",\n cause: \"stopped\",\n reason: this.stopReason,\n });\n }\n }\n this.finishReason = \"aborted\";\n await this.finalizeMessage(\"aborted\");\n }\n}\n\nfunction pendingKind(tool: AnyAgentTool): \"approval\" | \"question\" | \"client\" | null {\n if (tool.answeredBy === \"client\") {\n // A question is a client tool whose input is the prompt itself. The\n // distinction is for the UI — one renders a dialog, the other runs code —\n // and it costs nothing to carry.\n return tool.inputSchema === questionSchema ? \"question\" : \"client\";\n }\n return tool.requiresApproval ? \"approval\" : null;\n}\n\nfunction parseArgs(args: string): any {\n if (!args || !args.trim()) return {};\n try {\n return JSON.parse(args);\n } catch {\n return args;\n }\n}\n\n/**\n * Resolves a string built by repeated concatenation, in place.\n *\n * `text = text + delta`, run once per streamed token, does not build a string —\n * it builds a rope: a tree of pointers to every fragment, which the engine\n * flattens only when something needs the characters contiguously. A message\n * that nothing reads before it is persisted therefore keeps all of its\n * fragments alive, and the tree costs several times the text.\n *\n * Measured on Bun 1.x, 600 deltas of six characters (a ~450-token answer, 3.5 KB\n * of ASCII): held as a rope, 18.7 KB. Resolved, 3.5 KB — half the UTF-16 size,\n * because a flat ASCII string is stored one byte per character and a rope\n * cannot be. That is 5.3x, and it is paid by every message a store keeps and\n * every run the live registry holds.\n *\n * Indexing is what forces the resolution: `text[0]` cannot be answered without\n * the characters, so the engine collapses the tree and drops the fragments.\n * Nothing is allocated and nothing is copied, which is why this is not\n * `split(\"\").join(\"\")` — that measures the same but allocates one string per\n * character to get there.\n *\n * DO NOT DELETE THIS AS A NO-OP. It reads like one and it is not; the value is\n * the side effect on the receiver. If a future engine does not resolve on\n * index, this silently becomes a real no-op and memory returns to what it is\n * today — a safe failure, which is why it is written as a hint rather than as a\n * round trip through an encoder that would also mangle a lone surrogate.\n */\nfunction resolveRope(text: string): string {\n if (text.length > 0) void text[0];\n return text;\n}\n\nfunction appendText(message: AgentMessage, type: \"text\" | \"reasoning\", delta: string) {\n const last = message.content[message.content.length - 1];\n if (last && last.type === type) {\n (last as { text?: string }).text = ((last as { text?: string }).text ?? \"\") + delta;\n return;\n }\n message.content.push(\n type === \"text\" ? { type: \"text\", text: delta } : { type: \"reasoning\", text: delta },\n );\n}\n\n/**\n * Reasoning is accumulated per ITEM, not per message, and the item's id is kept.\n *\n * This used to go through `appendText`, which merges on the part *type* alone\n * and has nowhere to put an id. Both halves of that were wrong and neither was\n * visible in the transcript:\n *\n * - `request.ts` drops a reasoning item with no id, deliberately — the id is\n * the API's handle on the stored reasoning and a fabricated one would look\n * like continuity that is not there. So an id dropped here meant reasoning\n * was never sent back at all: on a two-step run the model re-derived its\n * own argument from nothing, and the prompt cache (which keys on the\n * literal item) missed every time. Measured against the live Responses API\n * in `live/live.test.ts`: the second call's input carried zero reasoning\n * items.\n * - a step that produces two reasoning items was flattening them into one\n * part, so even with an id there would have been one id for two items'\n * text.\n *\n * A part with no id is still appended rather than dropped: the text is what a\n * UI renders, and a provider that reports no item id (Azure does not always)\n * should still show its thinking. It just cannot be echoed back, which is the\n * bargain `reasoningItem` already documents.\n */\nfunction appendReasoning(message: AgentMessage, id: string | undefined, delta: string) {\n const last = message.content[message.content.length - 1];\n if (last && last.type === \"reasoning\" && last.id === id) {\n last.text = (last.text ?? \"\") + delta;\n return;\n }\n message.content.push(\n id ? { type: \"reasoning\", id, text: delta } : { type: \"reasoning\", text: delta },\n );\n}\n",
9
9
  "import { AgentTool, type AnyAgentTool } from \"../../ai/Agent\";\nimport type { McpRegistry, McpToolFilter } from \"./McpRegistry\";\n\n/**\n * The registry's tools as `AgentTool`s, for an agent running in this server —\n * v1's only projection.\n *\n * Build them once and hand them to `Agent.create`. None of them holds a user:\n * the caller is `{ kind: \"local\", req: ctx.req }`, taken when the tool\n * executes, so the same tools run as whichever user's run calls them. That is\n * the reason the caller is not an argument here.\n *\n * `requiresApproval` comes from the route's meta, where the app wrote it down;\n * it is never inferred from the verb.\n *\n * A failure the model should read — a 4xx, a missing file, a bad argument —\n * throws, and the agent loop turns a throw into a `tool_error` result the model\n * sees and can correct on its next step.\n */\nexport function toAgentTools(registry: McpRegistry, filter?: McpToolFilter): AnyAgentTool[] {\n return registry.descriptors(filter).map((descriptor) =>\n AgentTool.create({\n name: descriptor.name,\n description: descriptor.description,\n inputSchema: descriptor.inputSchema,\n requiresApproval: descriptor.requiresApproval,\n execute: (input, ctx) =>\n registry.execute({ kind: \"local\", req: ctx.req }, descriptor.name, input, ctx),\n }),\n );\n}\n",
10
+ "import type { ImageProvider } from \"./ImageProvider\";\nimport type { ImageResponse } from \"./providers/images\";\nimport type { Usage } from \"./types\";\n\n/**\n * A named image model with its settings, the way `Agent` is a named text model\n * with its settings.\n *\n * `ImageModel`, not `Image`, because `Image` is already gemi's `<img>` srcset\n * component (`client/Image.tsx`). Different entrypoints, so nothing collides\n * technically — and a page builder, which is what this feature is for, imports\n * both in the same file.\n *\n * export const banner = ImageModel.create({\n * name: \"banner\",\n * provider: AzureOpenAIImageProvider.model(\"gpt-image-2\"),\n * size: \"1536x1024\",\n * quality: \"high\",\n * });\n *\n * const { image, mimeType } = await banner.generate({ prompt });\n * await Storage.put({ name: `pages/${id}/hero.png`, body: image, contentType: mimeType });\n *\n * Inside an agent tool, call `ctx.generateImage(banner, …)` instead: it memoizes\n * the render against the tool call so a replay does not pay for it twice, and it\n * parks the bytes as an attachment. See `ToolContext.generateImage`.\n */\n\n/**\n * `WIDTHxHEIGHT`, or `auto`.\n *\n * NOT A CLOSED UNION OF THREE LITERALS, which is what the older image models\n * allowed and what a first draft of this type was. `gpt-image-2` takes arbitrary\n * dimensions, and a page builder deriving a size from a layout slot's aspect\n * ratio is precisely the caller that needs them.\n */\nexport type ImageSize = `${number}x${number}` | \"auto\";\nexport type ImageQuality = \"low\" | \"medium\" | \"high\" | \"auto\";\nexport type ImageBackground = \"transparent\" | \"opaque\" | \"auto\";\n\n/**\n * `png` or `jpeg`. Not `webp`, although every source lists it — measured against\n * `gpt-image-2`:\n *\n * 400 Invalid value: 'webp'. Supported values are: 'png' and 'jpeg'.\n * param: output_format\n */\nexport type ImageFormat = \"png\" | \"jpeg\";\n\ntype Settings = {\n size?: ImageSize;\n quality?: ImageQuality;\n background?: ImageBackground;\n format?: ImageFormat;\n /** JPEG quality, 0–100. Ignored by the vendor for PNG. */\n compression?: number;\n};\n\nexport type CreateImageModelParams = Settings & {\n /** For logs and, later, telemetry — the same role `Agent.name` plays. */\n name: string;\n provider: ImageProvider;\n};\n\nexport type GenerateImageParams = Settings & {\n prompt: string;\n /** Aborts the render. Inside a tool, `ctx.generateImage` passes the run's. */\n signal?: AbortSignal;\n};\n\nexport type ImageInput = Blob | File;\n\nexport type EditImageParams = GenerateImageParams & {\n /**\n * The images to edit, at least one. Typed as a non-empty tuple so an empty\n * array is a compile error rather than a paid call that quietly generates from\n * scratch.\n */\n images: [ImageInput, ...ImageInput[]];\n /**\n * A PNG with an alpha channel, the same dimensions as the input. The\n * **transparent** region is the part to replace.\n *\n * MEASURED, and honoured rather than merely accepted: with a transparent\n * centre square and the prompt \"put a yellow star in the masked area\", the\n * centre came back yellow and the surrounding ring kept the original colour,\n * while the same edit without a mask recoloured the whole image.\n */\n mask?: ImageInput;\n};\n\n/**\n * One generated image.\n *\n * NO `revisedPrompt`. It was in the original design, the requesting app asked\n * for it, and `gpt-image-2` does not return one — five successful responses,\n * generations and edits, none carrying `revised_prompt`. A field that is always\n * `undefined` teaches callers to write dead branches; adding it back if a model\n * ever populates it is not a breaking change.\n */\nexport type GeneratedImage = {\n /** The bytes, with `type` set — ready for `Storage.put` or\n * `ctx.attachments.put`, neither of which accepts a `Uint8Array`. */\n image: Blob;\n mimeType: string;\n /**\n * What was actually produced, read off the response and never assumed.\n *\n * MEASURED: an edit sent with no `size` answered `1254x1254` — neither a size\n * anyone asked for nor a multiple of 16. So the divisible-by-16 rule below\n * constrains the *request* and says nothing about the answer.\n */\n size: string;\n usage: Usage;\n};\n\n/** The measured request rules. Each one is a real 400, quoted. */\nfunction assertSize(size: string): void {\n if (size === \"auto\") return;\n\n const match = /^(\\d+)x(\\d+)$/.exec(size);\n if (!match) {\n throw new Error(`An image size is \"WIDTHxHEIGHT\" or \"auto\"; got ${JSON.stringify(size)}.`);\n }\n\n const width = Number(match[1]);\n const height = Number(match[2]);\n\n // 400 Invalid size '1000x1000'. Width and height must both be divisible by 16.\n if (width % 16 !== 0 || height % 16 !== 0) {\n throw new Error(\n `Image size ${size} is not supported: width and height must both be divisible by 16.`,\n );\n }\n\n // 400 Invalid size '1024x256'. The maximum supported aspect ratio is 3:1.\n if (Math.max(width / height, height / width) > 3) {\n throw new Error(\n `Image size ${size} is not supported: the maximum aspect ratio is 3:1, either way up.`,\n );\n }\n}\n\nfunction assertSettings(settings: Settings, where: string): void {\n if (settings.size !== undefined) {\n try {\n assertSize(settings.size);\n } catch (error) {\n throw new Error(`${where}: ${(error as Error).message}`);\n }\n }\n\n // 400 Transparent background is not supported for JPEG output format\n if (settings.background === \"transparent\" && settings.format === \"jpeg\") {\n throw new Error(`${where}: a transparent background needs \"png\"; JPEG has no alpha channel.`);\n }\n\n if (settings.compression !== undefined) {\n const value = settings.compression;\n if (!Number.isInteger(value) || value < 0 || value > 100) {\n throw new Error(`${where}: compression is an integer from 0 to 100; got ${value}.`);\n }\n }\n}\n\nfunction toBlob(input: ImageInput, fallbackName: string): File {\n if (input instanceof File) return input;\n // A Blob built from raw bytes has no name, and the vendor reads the part's\n // filename: an extensionless part is answered with a type error rather than a\n // useful one.\n const type = input.type || \"image/png\";\n const extension = type === \"image/jpeg\" ? \"jpg\" : (type.split(\"/\")[1] ?? \"png\");\n return new File([input], `${fallbackName}.${extension}`, { type });\n}\n\nexport class ImageModel {\n readonly name: string;\n readonly provider: ImageProvider;\n private readonly settings: Settings;\n\n private constructor(params: CreateImageModelParams) {\n const { name, provider, ...settings } = params;\n // Checked here as well as per call, so a bad default fails when the module\n // loads rather than on whichever request first happens to hit it.\n assertSettings(settings, `ImageModel \"${name}\"`);\n this.name = name;\n this.provider = provider;\n this.settings = settings;\n }\n\n static create(params: CreateImageModelParams): ImageModel {\n return new ImageModel(params);\n }\n\n async generate(params: GenerateImageParams): Promise<GeneratedImage> {\n const merged = this.merge(params);\n return present(\n await this.provider.generate({ prompt: params.prompt, ...merged, signal: params.signal }),\n );\n }\n\n /**\n * Edits one or more images.\n *\n * A SEPARATE METHOD, not an `images?` on `generate`. One method would mean an\n * `images` that arrives `undefined` — a mistyped field, a destructure of the\n * wrong object — silently generates a fresh image instead of editing: the\n * wrong picture, no error, on a call that is billed either way. Requiring a\n * non-empty `images` here makes \"edit with nothing to edit\" unspellable.\n */\n async edit(params: EditImageParams): Promise<GeneratedImage> {\n const merged = this.merge(params);\n return present(\n await this.provider.edit({\n prompt: params.prompt,\n ...merged,\n images: params.images.map((image, index) => toBlob(image, `image-${index}`)),\n ...(params.mask ? { mask: toBlob(params.mask, \"mask\") } : {}),\n signal: params.signal,\n }),\n );\n }\n\n /** Per-call params over the model's defaults, validated as one. */\n private merge(params: Settings): Settings {\n const merged: Settings = {\n ...this.settings,\n ...(params.size !== undefined ? { size: params.size } : {}),\n ...(params.quality !== undefined ? { quality: params.quality } : {}),\n ...(params.background !== undefined ? { background: params.background } : {}),\n ...(params.format !== undefined ? { format: params.format } : {}),\n ...(params.compression !== undefined ? { compression: params.compression } : {}),\n };\n // The merged pair is what matters: a model with `background: \"transparent\"`\n // and a call passing `format: \"jpeg\"` is the combination the vendor rejects,\n // and neither half is wrong on its own.\n assertSettings(merged, `ImageModel \"${this.name}\"`);\n return merged;\n }\n}\n\nfunction present(response: ImageResponse): GeneratedImage {\n return {\n image: new Blob([response.bytes as BlobPart], { type: response.mimeType }),\n mimeType: response.mimeType,\n size: response.size,\n usage: response.usage,\n };\n}\n",
11
+ "/**\n * Where a vendor lives, and who we are to it — resolved once, for every kind of\n * call.\n *\n * WHY THIS IS ITS OWN MODULE. The Responses path and the images path differ only\n * in the last segment of the URL. Everything before it — which host, whether the\n * configured endpoint already carried `/openai`, whether the api-version is\n * dated and therefore routes to the older path, whether to send `authorization:\n * Bearer` or `api-key`, whether an Entra token has to be minted again because\n * the last one expired mid-conversation — is identical, and every line of it was\n * worked out by measuring a real resource rather than reading documentation (the\n * transcripts are on `azureBase` below).\n *\n * That is exactly the kind of knowledge that must not be copied. A second copy\n * in an image provider would be correct on the day it was written and would\n * drift on the first fix that only one of them got, and the symptom of the drift\n * is a 404 on every request for whichever apps configured the provider the\n * documented way — which is the bug `azureBase` exists because of.\n *\n * So a provider resolves a `ProviderTarget` and appends its own path. Nothing\n * here knows about Responses, files or images.\n */\n\nexport type ProviderConfig = {\n apiKey?: string;\n baseURL?: string;\n timeoutMs?: number;\n maxRetries?: number;\n headers?: Record<string, string>;\n};\n\nexport type AzureConfig = ProviderConfig & {\n /**\n * The resource host, with or without a trailing `/openai`. Both spellings\n * work — `https://<resource>.cognitiveservices.azure.com` and\n * `https://<resource>.openai.azure.com` — and neither is rewritten, because\n * only one of them exists for a resource that was not created as\n * kind=OpenAI. See `azureBase` for what is done to it.\n */\n endpoint?: string;\n /**\n * Just the resource name, when there is no endpoint to hand. `<name>` is\n * expanded to `https://<name>.cognitiveservices.azure.com/openai`.\n */\n resourceName?: string;\n apiVersion?: string;\n /**\n * The deployment to call, when it is not named after the model. Azure lets\n * whoever ran the template call it anything, and plenty of them are called\n * `prod` — this is the override the class comment promises.\n */\n deployment?: string;\n /**\n * For Entra ID instead of a key. A function, not a token, because these\n * expire mid-conversation.\n */\n getToken?: () => Promise<string>;\n};\n\n/**\n * A resolved vendor, ready for a path to be appended.\n *\n * `base` and `query` are separate because Azure's api-version is a query\n * parameter and a caller appending `/images/generations` must not have to know\n * that it goes *before* the `?`. Every URL is `${base}${path}${query}`.\n */\nexport type ProviderTarget = {\n /** e.g. `https://api.openai.com/v1`, or `https://<host>/openai/v1`. */\n base: string;\n /** e.g. `` or `?api-version=preview`. Already encoded. */\n query: string;\n /** Called per request. See the Azure branch for why that is not an accident. */\n headers: () => Promise<Record<string, string>>;\n timeoutMs: number;\n maxRetries: number;\n};\n\n/** Long enough for a reasoning model to think before it says anything, short\n * enough that a hung connection is not mistaken for a slow one. For a streamed\n * call it only covers getting a response; the stream that follows has no\n * deadline. For a one-shot call it is the whole deadline — which is why the\n * images path sets its own, and why `retryTimeouts` exists. */\nexport const DEFAULT_TIMEOUT_MS = 120_000;\nexport const DEFAULT_MAX_RETRIES = 2;\n\nexport function env(name: string): string | undefined {\n return typeof process === \"undefined\" ? undefined : process.env?.[name];\n}\n\n/**\n * `preview` selects Azure's `/openai/v1` surface, which is the OpenAI-shaped\n * one — same request body, same SSE frames, same `model` field naming the\n * deployment — and it is the only surface the Responses API has that gemi's\n * request builder can talk to unchanged. It takes no dated version: sending\n * `api-version=2025-04-01-preview` to `/openai/v1/responses` answers\n * `400 {\"code\":\"BadRequest\",\"message\":\"API version not supported\"}`.\n *\n * A dated version is still honoured, and routes to the older\n * `/openai/responses` path instead — see `azurePath`. So an app that pinned\n * one keeps working, which is the promise the old comment here made and could\n * not keep once the paths diverged.\n */\nexport const AZURE_API_VERSION = \"preview\";\n\n/**\n * Where an Azure call goes, worked out live rather than from docs.\n *\n * THE PROBLEM THIS SOLVES. `AZURE_OPENAI_ENDPOINT` is conventionally written\n * with `/openai` already on the end, and the old code appended `/openai` again\n * and then a deployment path, producing\n * `…/openai/openai/deployments/<dep>/responses` — a 404 on every request, for\n * every app that configured the provider the documented way. Two separate\n * mistakes were stacked there, and only measuring told them apart.\n *\n * WHAT WAS MEASURED, against a real resource, POSTing a Responses body:\n *\n * 404 {endpoint}/openai/deployments/gpt-5.4/responses?api-version=2025-04-01-preview\n * 404 {host}/openai/deployments/gpt-5.4/responses?api-version=2025-04-01-preview\n * 404 {host}/openai/deployments/gpt-5.4/responses?api-version=preview\n * 400 {host}/openai/v1/responses?api-version=2025-04-01-preview (\"API version not supported\")\n * 200 {host}/openai/v1/responses?api-version=preview\n * 200 {host}/openai/v1/responses (no api-version at all)\n * 200 {host}/openai/responses?api-version=2025-04-01-preview\n *\n * for {host} in BOTH `https://<resource>.cognitiveservices.azure.com` and\n * `https://<resource>.openai.azure.com` — both spellings answered identically,\n * so the host was never the variable. The deployment-in-the-URL path is the\n * Chat Completions shape and the Responses API does not serve it at all; the\n * deployment goes in the body's `model`, which is what `buildResponsesRequest`\n * already puts there.\n *\n * `/openai/v1/files?api-version=preview` and\n * `/openai/files?api-version=2025-04-01-preview` were both checked too (200,\n * empty list), so uploads follow the same fork.\n *\n * And so do images, measured the same way against a `gpt-image-2` deployment:\n *\n * 200 {host}/openai/v1/images/generations?api-version=preview\n * 200 {host}/openai/v1/images/edits?api-version=preview\n *\n * which is the whole reason this function is shared rather than reimplemented.\n */\nexport function azureBase(raw: string): string {\n const trimmed = raw.trim().replace(/\\/+$/, \"\");\n if (!trimmed) return \"\";\n // A configured endpoint may or may not already carry `/openai`, and may even\n // carry `/openai/v1` if someone copied a full URL. Normalize down to the\n // resource base and put exactly one `/openai` back, rather than appending\n // blind — appending blind is the bug.\n const base = trimmed.replace(/\\/openai(?:\\/v1)?$/i, \"\");\n return `${base}/openai`;\n}\n\n/** Dated versions belong to the older path; `preview` (and anything that is not\n * a date) belongs to `/v1`. Both were verified above. */\nexport function azurePath(apiVersion: string): string {\n return /^\\d{4}-\\d{2}-\\d{2}/.test(apiVersion) ? \"\" : \"/v1\";\n}\n\n/** Overrides for a call whose deadline is not a streaming handshake's. */\nexport type TargetDefaults = { timeoutMs?: number };\n\nexport function openAITarget(\n config: ProviderConfig,\n defaults: TargetDefaults = {},\n): ProviderTarget {\n const base = (config.baseURL ?? env(\"OPENAI_BASE_URL\") ?? \"https://api.openai.com/v1\").replace(\n /\\/+$/,\n \"\",\n );\n\n return {\n base,\n query: \"\",\n headers: async () => {\n const apiKey = config.apiKey ?? env(\"OPENAI_API_KEY\");\n return {\n ...(apiKey ? { authorization: `Bearer ${apiKey}` } : {}),\n ...config.headers,\n };\n },\n timeoutMs: config.timeoutMs ?? defaults.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n };\n}\n\n/**\n * The resource base, `<host>/openai`, from whichever of the three ways it was\n * configured. A bare resource name expands to the `cognitiveservices` host\n * rather than the `openai.azure.com` one: both answer for a resource created\n * as kind=OpenAI, only `cognitiveservices` answers for an AI Foundry or\n * multi-service resource, so it is the spelling that is right more often. An\n * app on the other one sets `endpoint` and nothing rewrites it.\n */\nexport function azureResourceBase(config: AzureConfig): string {\n const configured = config.baseURL ?? config.endpoint ?? env(\"AZURE_OPENAI_ENDPOINT\");\n if (configured) return azureBase(configured);\n const resource =\n config.resourceName ?? env(\"AZURE_OPENAI_RESOURCE_NAME\") ?? env(\"AZURE_RESOURCE_NAME\");\n return resource ? azureBase(`https://${resource.trim()}.cognitiveservices.azure.com`) : \"\";\n}\n\nexport function azureTarget(config: AzureConfig, defaults: TargetDefaults = {}): ProviderTarget {\n const apiVersion = config.apiVersion ?? env(\"AZURE_OPENAI_API_VERSION\") ?? AZURE_API_VERSION;\n\n return {\n base: `${azureResourceBase(config)}${azurePath(apiVersion)}`,\n query: `?api-version=${encodeURIComponent(apiVersion)}`,\n headers: async () => {\n // Called per request, not per provider: an Entra token minted when the\n // app booted is expired by the time a long conversation reaches step\n // nine, and that failure looks like a random 401 in the middle of a\n // working feature.\n if (config.getToken) {\n return { authorization: `Bearer ${await config.getToken()}`, ...config.headers };\n }\n const apiKey = config.apiKey ?? env(\"AZURE_OPENAI_API_KEY\");\n return { ...(apiKey ? { \"api-key\": apiKey } : {}), ...config.headers };\n },\n timeoutMs: config.timeoutMs ?? defaults.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n };\n}\n",
10
12
  "import type { AgentError } from \"../types\";\n\n/**\n * A non-2xx response, carried as an exception so the retry loop and\n * `normalizeError` can both read it.\n *\n * The body is kept parsed-if-possible and raw-if-not: OpenAI and Azure both\n * answer `{ error: { message, type, code } }` on a good day, and an HTML error\n * page from a proxy on a bad one, and the bad day is exactly when the text\n * matters.\n */\nexport class ProviderHttpError extends Error {\n readonly status: number;\n readonly body: unknown;\n readonly requestId?: string;\n\n constructor(status: number, body: unknown, requestId?: string) {\n super(`Provider request failed with status ${status}: ${describeBody(body)}`);\n this.name = \"ProviderHttpError\";\n this.status = status;\n this.body = body;\n this.requestId = requestId;\n }\n}\n\n/**\n * The request ran out of time. Its own class because it must not read as a\n * user abort: `stop()` means the person is done, and a timeout means the\n * network was, and only one of those is worth trying again.\n */\nexport class ProviderTimeoutError extends Error {\n readonly timeoutMs: number;\n\n constructor(timeoutMs: number) {\n super(`Provider request timed out after ${timeoutMs}ms`);\n this.name = \"ProviderTimeoutError\";\n this.timeoutMs = timeoutMs;\n }\n}\n\nfunction describeBody(body: unknown): string {\n const detail = errorBody(body);\n if (detail?.message) return detail.message;\n if (typeof body === \"string\") return body.slice(0, 500);\n return \"no error body\";\n}\n\ntype OpenAIErrorBody = { message?: string; type?: string; code?: string; param?: string };\n\nfunction errorBody(body: unknown): OpenAIErrorBody | null {\n if (!body || typeof body !== \"object\") return null;\n const wrapper = body as { error?: unknown };\n const inner = wrapper.error && typeof wrapper.error === \"object\" ? wrapper.error : body;\n const e = inner as OpenAIErrorBody & { innererror?: { code?: string } };\n if (typeof e.message !== \"string\" && typeof e.code !== \"string\" && typeof e.type !== \"string\") {\n return null;\n }\n // Azure hides the interesting code one level down when its content filter\n // fires, and the outer code is the useless `content_filter`.\n const code = e.innererror?.code ?? e.code;\n return { message: e.message, type: e.type, code, param: e.param };\n}\n\n/**\n * Provider failures onto `AgentErrorCode`.\n *\n * The contract an app is buying here is that it can branch on `rate_limited`\n * without knowing whose rate limit it was, and on `retryable` without knowing\n * which of the two APIs answered. So `retryable` is set from what is actually\n * true of the failure — a 429 from a spent quota is not retryable no matter\n * what its status code says, and neither is a request that will be too long\n * again next time.\n */\nexport function normalizeProviderError(error: unknown): AgentError {\n if (error instanceof ProviderTimeoutError) {\n return { code: \"provider_error\", message: error.message, retryable: true };\n }\n\n if (isAbort(error)) {\n return { code: \"aborted\", message: \"The request was aborted.\", retryable: false };\n }\n\n if (error instanceof ProviderHttpError) {\n return fromStatus(error.status, errorBody(error.body), error.message);\n }\n\n // `fetch` rejects with a TypeError for DNS failures, refused connections and\n // dropped sockets. Every one of those is worth another attempt.\n if (error instanceof TypeError) {\n return {\n code: \"provider_error\",\n message: `Could not reach the provider: ${error.message}`,\n retryable: true,\n };\n }\n\n if (error instanceof Error) {\n return { code: \"provider_error\", message: error.message, retryable: false };\n }\n\n return { code: \"unknown\", message: String(error), retryable: false };\n}\n\nfunction fromStatus(\n status: number,\n body: OpenAIErrorBody | null,\n fallbackMessage: string,\n): AgentError {\n const message = body?.message ?? fallbackMessage;\n const code = (body?.code ?? \"\").toLowerCase();\n const type = (body?.type ?? \"\").toLowerCase();\n const haystack = `${code} ${type} ${message}`.toLowerCase();\n\n if (status === 429) {\n // A spent quota answers 429 and will answer 429 for the rest of the month.\n // Retrying it is how a run turns one billing problem into `maxRetries`\n // billing problems and a much later error message.\n const outOfCredit = code === \"insufficient_quota\" || haystack.includes(\"quota\");\n return { code: \"rate_limited\", message, retryable: !outOfCredit };\n }\n\n if (status >= 500 || status === 408 || status === 409) {\n return { code: \"provider_error\", message, retryable: true };\n }\n\n if (isContextLength(haystack)) {\n return { code: \"context_length_exceeded\", message, retryable: false };\n }\n\n if (isContentFilter(haystack)) {\n return { code: \"content_filtered\", message, retryable: false };\n }\n\n // A schema the API refused. It is our request that is wrong, so the run\n // should surface it as a tool problem rather than a generic outage.\n if (body?.param?.startsWith(\"tools\") || haystack.includes(\"invalid schema for function\")) {\n return { code: \"invalid_tool_input\", message, retryable: false };\n }\n\n return { code: \"provider_error\", message, retryable: false };\n}\n\nfunction isContextLength(haystack: string): boolean {\n return (\n haystack.includes(\"context_length_exceeded\") ||\n haystack.includes(\"maximum context length\") ||\n haystack.includes(\"context window\") ||\n haystack.includes(\"reduce the length\")\n );\n}\n\nfunction isContentFilter(haystack: string): boolean {\n return (\n haystack.includes(\"content_filter\") ||\n haystack.includes(\"content_policy_violation\") ||\n haystack.includes(\"responsibleaipolicyviolation\") ||\n haystack.includes(\"content management policy\")\n );\n}\n\nfunction isAbort(error: unknown): boolean {\n if (!error || typeof error !== \"object\") return false;\n const e = error as { name?: string; code?: string };\n return e.name === \"AbortError\" || e.code === \"ABORT_ERR\";\n}\n",
11
- "import { normalizeProviderError, ProviderHttpError, ProviderTimeoutError } from \"./errors\";\n\nexport type FetchLike = (input: string, init: RequestInit) => Promise<Response>;\n\nexport type RequestOptions = {\n maxRetries: number;\n timeoutMs: number;\n signal?: AbortSignal;\n /** Injected by the tests. Nothing here should ever need a real socket to be\n * exercised, and a retry policy that is only tested against a live API is\n * one that gets tested during an outage. */\n fetchImpl?: FetchLike;\n /** Takes the caller's signal so the default can clear its timer rather than\n * hold the process open for a backoff nobody is waiting for any more. An\n * injected sleep may ignore it: the wait is raced against the abort either\n * way. */\n sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;\n random?: () => number;\n now?: () => number;\n};\n\n/** The floor of the backoff. Doubling from here gives 0.5s, 1s, 2s, 4s — long\n * enough to outlast a rate-limit window, short enough that a user watching a\n * stream does not assume it died. */\nconst BASE_DELAY_MS = 500;\nexport const MAX_DELAY_MS = 20_000;\n\n/**\n * `Retry-After` in either of its two legal forms: seconds, or an HTTP date.\n *\n * Honoured rather than ignored because the server knows when its window\n * reopens and we are guessing. A value in the past clamps to zero; a garbage\n * value falls back to the computed backoff, since a header we cannot read is\n * not a reason to give up on the request.\n *\n * Parsed here, bounded at the call site: this returns what the server said.\n */\nexport function parseRetryAfter(value: string | null | undefined, now: number): number | undefined {\n if (!value) return undefined;\n const trimmed = value.trim();\n if (/^\\d+(\\.\\d+)?$/.test(trimmed)) return Math.max(0, Number(trimmed) * 1000);\n const date = Date.parse(trimmed);\n if (Number.isNaN(date)) return undefined;\n return Math.max(0, date - now);\n}\n\n/**\n * Exponential backoff with full jitter. The jitter is the point: without it,\n * every request that got rate limited at the same moment retries at the same\n * moment, and the second wave is the same size as the first.\n */\nexport function backoffDelayMs(attempt: number, random: () => number): number {\n const ceiling = Math.min(MAX_DELAY_MS, BASE_DELAY_MS * 2 ** attempt);\n return Math.round(ceiling * (0.5 + random() * 0.5));\n}\n\nfunction isRetryableStatus(status: number): boolean {\n // 429 and the 5xx range. 409 is in here because OpenAI answers it for a\n // request that raced with something on their side, and it succeeds on the\n // second try.\n return status === 429 || status === 408 || status === 409 || status >= 500;\n}\n\n/**\n * One retry policy, not two.\n *\n * The status table alone cannot answer this: a 429 from a spent quota is a 429\n * every time for the rest of the month, and retrying it turns one billing\n * problem into `maxRetries` of them and a much later error message. Only the\n * body says which 429 this is, and `normalizeProviderError` is where that is\n * already read — so it is asked here rather than left to disagree with a table\n * after the retries have happened.\n *\n * It is a veto, not a second opinion: the table decides which statuses are\n * worth another attempt and the normalized verdict can only take one away.\n * That way a future change to the error mapping cannot start retrying 400s.\n */\nfunction shouldRetry(status: number, error: ProviderHttpError): boolean {\n return isRetryableStatus(status) && normalizeProviderError(error).retryable;\n}\n\nfunction abortReason(signal: AbortSignal): unknown {\n return signal.reason ?? new DOMException(\"The operation was aborted.\", \"AbortError\");\n}\n\nfunction defaultSleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n \"abort\",\n () => {\n clearTimeout(timer);\n reject(abortReason(signal));\n },\n { once: true },\n );\n });\n}\n\n/**\n * One HTTP call, retried.\n *\n * The timeout covers getting a response, not consuming one. A streamed answer\n * legitimately takes minutes, and a timeout that kept running would cut off\n * long completions at exactly the point where they were most expensive to\n * abandon — so the timer is cleared once the headers land, and only the\n * caller's own signal can end the body.\n */\nexport async function requestWithRetry(\n url: string,\n init: RequestInit,\n options: RequestOptions,\n): Promise<Response> {\n const doFetch = options.fetchImpl ?? ((u: string, i: RequestInit) => fetch(u, i));\n const sleep = options.sleep ?? defaultSleep;\n const random = options.random ?? Math.random;\n const now = options.now ?? Date.now;\n const maxRetries = Math.max(0, options.maxRetries);\n\n /**\n * A backoff nobody can interrupt is a `stop()` that does not stop.\n *\n * The wait is where a retrying request spends nearly all of its time — up to\n * 20 seconds of it — and noticing the abort only at the top of the next\n * iteration means the run holds its state, and the user watches a cancelled\n * stream, for the rest of the window. So the sleep loses a race with the\n * signal, and the abort propagates like any other.\n */\n const wait = async (ms: number): Promise<void> => {\n const signal = options.signal;\n if (!signal) return await sleep(ms);\n signal.throwIfAborted();\n let onAbort: (() => void) | undefined;\n try {\n await Promise.race([\n sleep(ms, signal),\n new Promise<never>((_resolve, reject) => {\n onAbort = () => reject(abortReason(signal));\n signal.addEventListener(\"abort\", onAbort, { once: true });\n }),\n ]);\n } finally {\n if (onAbort) signal.removeEventListener(\"abort\", onAbort);\n }\n };\n\n let lastError: unknown;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n options.signal?.throwIfAborted();\n\n const controller = new AbortController();\n const abortOuter = () => controller.abort(options.signal?.reason);\n options.signal?.addEventListener(\"abort\", abortOuter, { once: true });\n\n let timedOut = false;\n const timer =\n options.timeoutMs > 0\n ? setTimeout(() => {\n timedOut = true;\n controller.abort();\n }, options.timeoutMs)\n : undefined;\n\n let response: Response;\n try {\n response = await doFetch(url, { ...init, signal: controller.signal });\n } catch (error) {\n if (timer !== undefined) clearTimeout(timer);\n options.signal?.removeEventListener(\"abort\", abortOuter);\n // The caller's abort wins outright — `stop()` means stop, not \"stop and\n // try three more times\".\n if (options.signal?.aborted) throw error;\n lastError = timedOut ? new ProviderTimeoutError(options.timeoutMs) : error;\n if (attempt === maxRetries) throw lastError;\n await wait(backoffDelayMs(attempt, random));\n continue;\n }\n\n if (timer !== undefined) clearTimeout(timer);\n\n if (response.ok) {\n // Deliberately left attached: the caller is about to read a stream, and\n // its own signal has to keep reaching it.\n return response;\n }\n\n options.signal?.removeEventListener(\"abort\", abortOuter);\n const body = await readErrorBody(response);\n const error = new ProviderHttpError(\n response.status,\n body,\n response.headers.get(\"x-request-id\") ?? undefined,\n );\n\n if (attempt === maxRetries || !shouldRetry(response.status, error)) throw error;\n\n // Bounded, because `Retry-After` is a number the server chooses and a daily\n // quota answers it in hours. Sleeping through that holds the run open for\n // the whole window; waking early costs one request that gets the same 429\n // and a longer, still-bounded backoff after it.\n const retryAfter = parseRetryAfter(response.headers.get(\"retry-after\"), now());\n await wait(\n retryAfter === undefined\n ? backoffDelayMs(attempt, random)\n : Math.min(retryAfter, MAX_DELAY_MS),\n );\n lastError = error;\n }\n\n // Unreachable: the loop either returns or throws. Kept honest rather than\n // asserted away.\n throw lastError ?? new Error(\"Provider request failed with no response.\");\n}\n\nasync function readErrorBody(response: Response): Promise<unknown> {\n let text: string;\n try {\n text = await response.text();\n } catch {\n return null;\n }\n try {\n return JSON.parse(text);\n } catch {\n return text;\n }\n}\n",
13
+ "import { normalizeProviderError, ProviderHttpError, ProviderTimeoutError } from \"./errors\";\n\nexport type FetchLike = (input: string, init: RequestInit) => Promise<Response>;\n\nexport type RequestOptions = {\n maxRetries: number;\n timeoutMs: number;\n signal?: AbortSignal;\n /** Injected by the tests. Nothing here should ever need a real socket to be\n * exercised, and a retry policy that is only tested against a live API is\n * one that gets tested during an outage. */\n fetchImpl?: FetchLike;\n /** Takes the caller's signal so the default can clear its timer rather than\n * hold the process open for a backoff nobody is waiting for any more. An\n * injected sleep may ignore it: the wait is raced against the abort either\n * way. */\n sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;\n random?: () => number;\n now?: () => number;\n /**\n * Whether a timeout is worth another attempt. Defaults to `true`.\n *\n * THE ANSWER DEPENDS ON WHAT THE TIMER BOUNDS, which is why this is a\n * parameter and not a policy. For a streamed call the timer covers getting a\n * response and is cleared once the headers land (see `requestWithRetry`), so a\n * timeout means the provider never started answering: nothing was generated,\n * nothing was billed, and retrying is both cheap and likely to work.\n *\n * For a call that returns its whole result in one body, the same timer bounds\n * the *work*. A timeout there means the work probably happened and we hung up\n * before hearing about it — so a retry pays for it a second time, at the same\n * latency, and most likely times out again.\n *\n * MEASURED, because the margin is what makes this urgent rather than\n * theoretical. One `gpt-image-2` render at `quality: \"high\"`, `1536x1024`,\n * against a real Azure deployment: **117.8 seconds**, against a\n * `DEFAULT_TIMEOUT_MS` of 120_000. Two seconds of headroom, about 2%. Any\n * variance crosses it, and with retries on, one such request becomes three\n * two-minute renders, six minutes of waiting, three images billed, and a\n * `ProviderTimeoutError` returned to the caller. (For scale: the same model at\n * `low`/1024² answers in 15s, and an edit in 14–20s.)\n *\n * So the image path passes `false` and the streaming path leaves it alone. A\n * 429 or a 5xx is still retried in both cases: those say the request did not\n * run, which is the opposite of what a timeout says here.\n */\n retryTimeouts?: boolean;\n};\n\n/** The floor of the backoff. Doubling from here gives 0.5s, 1s, 2s, 4s — long\n * enough to outlast a rate-limit window, short enough that a user watching a\n * stream does not assume it died. */\nconst BASE_DELAY_MS = 500;\nexport const MAX_DELAY_MS = 20_000;\n\n/**\n * `Retry-After` in either of its two legal forms: seconds, or an HTTP date.\n *\n * Honoured rather than ignored because the server knows when its window\n * reopens and we are guessing. A value in the past clamps to zero; a garbage\n * value falls back to the computed backoff, since a header we cannot read is\n * not a reason to give up on the request.\n *\n * Parsed here, bounded at the call site: this returns what the server said.\n */\nexport function parseRetryAfter(value: string | null | undefined, now: number): number | undefined {\n if (!value) return undefined;\n const trimmed = value.trim();\n if (/^\\d+(\\.\\d+)?$/.test(trimmed)) return Math.max(0, Number(trimmed) * 1000);\n const date = Date.parse(trimmed);\n if (Number.isNaN(date)) return undefined;\n return Math.max(0, date - now);\n}\n\n/**\n * Exponential backoff with full jitter. The jitter is the point: without it,\n * every request that got rate limited at the same moment retries at the same\n * moment, and the second wave is the same size as the first.\n */\nexport function backoffDelayMs(attempt: number, random: () => number): number {\n const ceiling = Math.min(MAX_DELAY_MS, BASE_DELAY_MS * 2 ** attempt);\n return Math.round(ceiling * (0.5 + random() * 0.5));\n}\n\nfunction isRetryableStatus(status: number): boolean {\n // 429 and the 5xx range. 409 is in here because OpenAI answers it for a\n // request that raced with something on their side, and it succeeds on the\n // second try.\n return status === 429 || status === 408 || status === 409 || status >= 500;\n}\n\n/**\n * One retry policy, not two.\n *\n * The status table alone cannot answer this: a 429 from a spent quota is a 429\n * every time for the rest of the month, and retrying it turns one billing\n * problem into `maxRetries` of them and a much later error message. Only the\n * body says which 429 this is, and `normalizeProviderError` is where that is\n * already read — so it is asked here rather than left to disagree with a table\n * after the retries have happened.\n *\n * It is a veto, not a second opinion: the table decides which statuses are\n * worth another attempt and the normalized verdict can only take one away.\n * That way a future change to the error mapping cannot start retrying 400s.\n */\nfunction shouldRetry(status: number, error: ProviderHttpError): boolean {\n return isRetryableStatus(status) && normalizeProviderError(error).retryable;\n}\n\nfunction abortReason(signal: AbortSignal): unknown {\n return signal.reason ?? new DOMException(\"The operation was aborted.\", \"AbortError\");\n}\n\nfunction defaultSleep(ms: number, signal?: AbortSignal): Promise<void> {\n return new Promise((resolve, reject) => {\n const timer = setTimeout(resolve, ms);\n signal?.addEventListener(\n \"abort\",\n () => {\n clearTimeout(timer);\n reject(abortReason(signal));\n },\n { once: true },\n );\n });\n}\n\n/**\n * One HTTP call, retried.\n *\n * The timeout covers getting a response, not consuming one. A streamed answer\n * legitimately takes minutes, and a timeout that kept running would cut off\n * long completions at exactly the point where they were most expensive to\n * abandon — so the timer is cleared once the headers land, and only the\n * caller's own signal can end the body.\n */\nexport async function requestWithRetry(\n url: string,\n init: RequestInit,\n options: RequestOptions,\n): Promise<Response> {\n const doFetch = options.fetchImpl ?? ((u: string, i: RequestInit) => fetch(u, i));\n const sleep = options.sleep ?? defaultSleep;\n const random = options.random ?? Math.random;\n const now = options.now ?? Date.now;\n const maxRetries = Math.max(0, options.maxRetries);\n\n /**\n * A backoff nobody can interrupt is a `stop()` that does not stop.\n *\n * The wait is where a retrying request spends nearly all of its time — up to\n * 20 seconds of it — and noticing the abort only at the top of the next\n * iteration means the run holds its state, and the user watches a cancelled\n * stream, for the rest of the window. So the sleep loses a race with the\n * signal, and the abort propagates like any other.\n */\n const wait = async (ms: number): Promise<void> => {\n const signal = options.signal;\n if (!signal) return await sleep(ms);\n signal.throwIfAborted();\n let onAbort: (() => void) | undefined;\n try {\n await Promise.race([\n sleep(ms, signal),\n new Promise<never>((_resolve, reject) => {\n onAbort = () => reject(abortReason(signal));\n signal.addEventListener(\"abort\", onAbort, { once: true });\n }),\n ]);\n } finally {\n if (onAbort) signal.removeEventListener(\"abort\", onAbort);\n }\n };\n\n let lastError: unknown;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n options.signal?.throwIfAborted();\n\n const controller = new AbortController();\n const abortOuter = () => controller.abort(options.signal?.reason);\n options.signal?.addEventListener(\"abort\", abortOuter, { once: true });\n\n let timedOut = false;\n const timer =\n options.timeoutMs > 0\n ? setTimeout(() => {\n timedOut = true;\n controller.abort();\n }, options.timeoutMs)\n : undefined;\n\n let response: Response;\n try {\n response = await doFetch(url, { ...init, signal: controller.signal });\n } catch (error) {\n if (timer !== undefined) clearTimeout(timer);\n options.signal?.removeEventListener(\"abort\", abortOuter);\n // The caller's abort wins outright — `stop()` means stop, not \"stop and\n // try three more times\".\n if (options.signal?.aborted) throw error;\n lastError = timedOut ? new ProviderTimeoutError(options.timeoutMs) : error;\n // Checked before the attempt count, because this is not \"no attempts\n // left\" — it is \"another attempt is the wrong thing to do\". See\n // `RequestOptions.retryTimeouts`.\n if (timedOut && options.retryTimeouts === false) throw lastError;\n if (attempt === maxRetries) throw lastError;\n await wait(backoffDelayMs(attempt, random));\n continue;\n }\n\n if (timer !== undefined) clearTimeout(timer);\n\n if (response.ok) {\n // Deliberately left attached: the caller is about to read a stream, and\n // its own signal has to keep reaching it.\n return response;\n }\n\n options.signal?.removeEventListener(\"abort\", abortOuter);\n const body = await readErrorBody(response);\n const error = new ProviderHttpError(\n response.status,\n body,\n response.headers.get(\"x-request-id\") ?? undefined,\n );\n\n if (attempt === maxRetries || !shouldRetry(response.status, error)) throw error;\n\n // Bounded, because `Retry-After` is a number the server chooses and a daily\n // quota answers it in hours. Sleeping through that holds the run open for\n // the whole window; waking early costs one request that gets the same 429\n // and a longer, still-bounded backoff after it.\n const retryAfter = parseRetryAfter(response.headers.get(\"retry-after\"), now());\n await wait(\n retryAfter === undefined\n ? backoffDelayMs(attempt, random)\n : Math.min(retryAfter, MAX_DELAY_MS),\n );\n lastError = error;\n }\n\n // Unreachable: the loop either returns or throws. Kept honest rather than\n // asserted away.\n throw lastError ?? new Error(\"Provider request failed with no response.\");\n}\n\nasync function readErrorBody(response: Response): Promise<unknown> {\n let text: string;\n try {\n text = await response.text();\n } catch {\n return null;\n }\n try {\n return JSON.parse(text);\n } catch {\n return text;\n }\n}\n",
14
+ "import type { AgentError, Usage } from \"../types\";\nimport type { ProviderTarget } from \"./endpoints\";\nimport { normalizeProviderError } from \"./errors\";\nimport { requestWithRetry, type FetchLike } from \"./http\";\n\n/**\n * One image call, retried where retrying is free and not where it costs a render.\n *\n * MEASURED against a `gpt-image-2` deployment, api-version `preview`, and every\n * decision in this file comes from one of these rather than from documentation —\n * which disagreed with the endpoint on three separate points:\n *\n * 200 {base}/images/generations low, 1024x1024, png 15.0s\n * 200 {base}/images/generations low, 1024x1024, png, transparent 14.6s\n * 400 {base}/images/generations high, 1536x1024, webp\n * → \"Invalid value: 'webp'. Supported values are: 'png' and 'jpeg'.\"\n * 200 {base}/images/generations high, 1536x1024, png 117.8s\n * 200 {base}/images/edits low, one image[], no mask, no size 13.7s\n * → answered size \"1254x1254\", which is neither what was asked for\n * nor a multiple of 16\n * 200 {base}/images/edits low, one image[], mask, 1024x1024 14.0s\n * 200 {base}/images/edits low, two image[], 1024x1024 20.2s\n *\n * The documentation was wrong that `webp` is accepted, wrong that this model\n * cannot do transparency, and wrong about the `usage` field names. The response\n * shape, verbatim:\n *\n * { \"created\": 1790519365, \"background\": \"opaque\", \"output_format\": \"png\",\n * \"quality\": \"low\", \"size\": \"1024x1024\",\n * \"data\": [{ \"b64_json\": \"…\" }],\n * \"usage\": { \"input_tokens\": 15,\n * \"input_tokens_details\": { \"image_tokens\": 0, \"text_tokens\": 15 },\n * \"output_tokens\": 196,\n * \"output_tokens_details\": { \"image_tokens\": 196, \"text_tokens\": 0 },\n * \"total_tokens\": 211 } }\n *\n * There is no `revised_prompt` in any of the five successful responses. See\n * `ImageModel` for why the field is not on the public result type.\n */\nexport type ImagesEndpoint = {\n generationsUrl: string;\n editsUrl: string;\n /** Async for the same reason the Responses endpoint's is: Entra tokens expire\n * mid-conversation, so the credential is read per request. */\n headers: () => Promise<Record<string, string>>;\n timeoutMs: number;\n maxRetries: number;\n fetchImpl?: FetchLike;\n};\n\n/**\n * Longer than the Responses default, because here the timer bounds the work.\n *\n * A `high` / 1536x1024 render measured 117.8 seconds against a 120_000 default —\n * two seconds of headroom. That is not a timeout, it is a coin flip, and losing\n * it used to mean three renders billed (see `RequestOptions.retryTimeouts`). 300\n * seconds is chosen to clear the measured worst case with room for a busier\n * resource and a larger size, and an app that knows its own ceiling sets\n * `timeoutMs` in config.\n */\nexport const DEFAULT_IMAGE_TIMEOUT_MS = 300_000;\n\nexport function imagesEndpoint(target: ProviderTarget): ImagesEndpoint {\n return {\n generationsUrl: `${target.base}/images/generations${target.query}`,\n editsUrl: `${target.base}/images/edits${target.query}`,\n headers: target.headers,\n timeoutMs: target.timeoutMs,\n maxRetries: target.maxRetries,\n };\n}\n\n/**\n * What an image call failed with, as a normalized code rather than a vendor body.\n *\n * A one-shot call throws where the streaming path emits an `error` event — a\n * promise has nowhere else to put it, and a caller awaiting bytes should not have\n * to check a union. The normalized `AgentError` is carried so an app can branch\n * on `rate_limited` exactly as it would for a text call.\n *\n * The vendor's own body is on `cause` and deliberately NOT in `message`. Inside a\n * tool a throw becomes a tool result the model reads (#446), and a provider error\n * body is not something to hand a model verbatim when a sentence will do.\n */\nexport class ImageRequestError extends Error {\n readonly code: AgentError[\"code\"];\n readonly retryable: boolean;\n\n constructor(error: AgentError, options: { cause?: unknown } = {}) {\n super(error.message, options);\n this.name = \"ImageRequestError\";\n this.code = error.code;\n this.retryable = error.retryable;\n }\n}\n\n/** The JSON body of a generation. Vendor field names, built by `ImageModel`. */\nexport type ImageGenerationRequest = {\n model: string;\n prompt: string;\n size?: string;\n quality?: string;\n background?: string;\n output_format?: string;\n output_compression?: number;\n};\n\n/** What every image call answers, before `ImageModel` gives it a public shape. */\nexport type ImageResponse = {\n /** Decoded from `b64_json`. */\n bytes: Uint8Array;\n mimeType: string;\n /** Read off the response, never assumed — an edit with no `size` answered\n * `1254x1254`. */\n size: string;\n background?: string;\n usage: Usage;\n};\n\nconst MIME_BY_FORMAT: Record<string, string> = {\n png: \"image/png\",\n jpeg: \"image/jpeg\",\n};\n\n/**\n * The vendor's `usage` onto gemi's.\n *\n * Separate from `toUsage` in `stream.ts` rather than shared with it. The three\n * totals have the same names, so sharing looked right; the details objects do\n * not. `toUsage` reads `output_tokens_details.reasoning_tokens` and\n * `input_tokens_details.cached_tokens`, and this endpoint sends `image_tokens`\n * and `text_tokens` in both — so reusing it silently discards the only part of\n * this payload that is specific to images, which is the part `imageInputTokens`\n * exists for.\n */\nexport function toImageUsage(raw: any): Usage {\n const inputTokens = Number(raw?.input_tokens ?? 0);\n const outputTokens = Number(raw?.output_tokens ?? 0);\n const usage: Usage = {\n inputTokens,\n outputTokens,\n totalTokens: Number(raw?.total_tokens ?? inputTokens + outputTokens),\n };\n const inImages = raw?.input_tokens_details?.image_tokens;\n if (typeof inImages === \"number\") usage.imageInputTokens = inImages;\n const outImages = raw?.output_tokens_details?.image_tokens;\n if (typeof outImages === \"number\") usage.imageOutputTokens = outImages;\n return usage;\n}\n\nfunction decodeBase64(b64: string): Uint8Array {\n const binary = atob(b64);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return bytes;\n}\n\nfunction parseImageResponse(json: any): ImageResponse {\n const first = json?.data?.[0];\n const b64 = first?.b64_json;\n if (typeof b64 !== \"string\" || b64.length === 0) {\n // Not an assertion for its own sake: the endpoint has a `url` response mode\n // for other models, and a deployment that answered with one would otherwise\n // reach the caller as an empty Blob.\n throw new ImageRequestError({\n code: \"provider_error\",\n message: \"The provider answered an image call with no base64 image data.\",\n retryable: false,\n });\n }\n\n const format = typeof json.output_format === \"string\" ? json.output_format : \"png\";\n return {\n bytes: decodeBase64(b64),\n mimeType: MIME_BY_FORMAT[format] ?? `image/${format}`,\n size: typeof json.size === \"string\" ? json.size : \"\",\n ...(typeof json.background === \"string\" ? { background: json.background } : {}),\n usage: toImageUsage(json.usage),\n };\n}\n\ntype CallOptions = { signal?: AbortSignal };\n\nasync function post(\n url: string,\n init: RequestInit,\n endpoint: ImagesEndpoint,\n options: CallOptions,\n): Promise<ImageResponse> {\n let response: Response;\n try {\n response = await requestWithRetry(url, init, {\n maxRetries: endpoint.maxRetries,\n timeoutMs: endpoint.timeoutMs,\n // The whole point. A timeout here means the render probably happened and\n // we hung up; retrying pays for it again. 429 and 5xx are still retried.\n retryTimeouts: false,\n signal: options.signal,\n fetchImpl: endpoint.fetchImpl,\n });\n } catch (error) {\n // An abort is the caller's own doing and must stay recognisable as one —\n // `stop()` is not a provider failure.\n if (options.signal?.aborted) throw error;\n throw new ImageRequestError(normalizeProviderError(error), { cause: error });\n }\n\n return parseImageResponse(await response.json());\n}\n\nexport async function generateImage(\n endpoint: ImagesEndpoint,\n body: ImageGenerationRequest,\n options: CallOptions = {},\n): Promise<ImageResponse> {\n return await post(\n endpoint.generationsUrl,\n {\n method: \"POST\",\n headers: { ...(await endpoint.headers()), \"content-type\": \"application/json\" },\n body: JSON.stringify(body),\n },\n endpoint,\n options,\n );\n}\n\n/**\n * An edit, as multipart.\n *\n * `image[]` repeated once per input, measured — two parts reported 2048 input\n * image tokens against 1024 for one. No `content-type` header, for the reason\n * `uploadFile` gives: the boundary is generated with the body, and setting the\n * header by hand fails with a parser error that names nothing useful.\n */\nexport async function editImage(\n endpoint: ImagesEndpoint,\n form: FormData,\n options: CallOptions = {},\n): Promise<ImageResponse> {\n return await post(\n endpoint.editsUrl,\n { method: \"POST\", headers: await endpoint.headers(), body: form },\n endpoint,\n options,\n );\n}\n",
15
+ "import {\n azureTarget,\n openAITarget,\n type AzureConfig,\n type ProviderConfig,\n} from \"./providers/endpoints\";\nimport { normalizeProviderError } from \"./providers/errors\";\nimport {\n DEFAULT_IMAGE_TIMEOUT_MS,\n editImage,\n generateImage,\n imagesEndpoint,\n type ImageGenerationRequest,\n type ImageResponse,\n type ImagesEndpoint,\n} from \"./providers/images\";\nimport type { AgentError } from \"./types\";\n\n/**\n * A provider that makes images, separate from the one that makes text.\n *\n * WHY NOT A METHOD ON `AgentProvider`. `provider.generateImage(...)` was the\n * requested shape and it does not survive two facts about this codebase.\n *\n * First, `AgentProvider` is abstract with `stream` and `upload`, and a third\n * abstract member breaks every implementor — including gemi's own `FakeProvider`\n * (duck-typed) and `RecordingProvider` (hand-implements all five members). A\n * capability that only one vendor surface has should not be a hole punched in the\n * interface every other surface has to fill.\n *\n * Second, and this is the real one: an image deployment would have to lie about\n * itself. `capabilities` is computed in the constructor from the model id, and\n * `capabilitiesForModel` deliberately grants an unrecognised id *every*\n * capability — so `AzureOpenAIProvider.model(\"gpt-image-2\")` claims structured\n * output, reasoning and tool search, and exposes a `stream()` that is callable\n * and broken. Splitting the hierarchy makes the wrong call unspellable instead\n * of merely wrong, which is the same reasoning `ScopedAttachments` is built on.\n *\n * What the two hierarchies *do* share is the part worth sharing: which host,\n * which credential, and Azure's api-version fork. That is `providers/endpoints.ts`\n * and neither class reimplements it.\n */\nexport abstract class ImageProvider {\n /** The model, or on Azure the model the deployment serves. */\n abstract readonly model: string;\n\n /** For autocomplete only; any string is accepted, because a model released\n * next Tuesday must not need a gemi release to use. */\n static models(): readonly string[] {\n return [];\n }\n\n protected abstract endpoint(): ImagesEndpoint;\n\n /** What goes in the body's `model`. The deployment, on Azure. */\n protected modelName(): string {\n return this.model;\n }\n\n async generate(params: ImageProviderParams): Promise<ImageResponse> {\n return await generateImage(this.endpoint(), this.body(params), { signal: params.signal });\n }\n\n /**\n * An edit. `images` is required and non-empty — see `ImageModel.edit` for why\n * that is a separate method rather than an optional field on `generate`.\n */\n async edit(params: ImageProviderEditParams): Promise<ImageResponse> {\n const form = new FormData();\n for (const [key, value] of Object.entries(this.body(params))) {\n if (value !== undefined) form.set(key, String(value));\n }\n // Repeated `image[]`, measured: two parts reported 2048 input image tokens\n // against 1024 for one, so each part is read as its own image rather than\n // the last one winning.\n for (const image of params.images) form.append(\"image[]\", image, image.name);\n if (params.mask) form.set(\"mask\", params.mask, params.mask.name);\n return await editImage(this.endpoint(), form, { signal: params.signal });\n }\n\n /**\n * Maps gemi's params onto the vendor's field names.\n *\n * On the base class rather than in each subclass: OpenAI and Azure take the\n * same body, and the only difference is that Azure's `model` names a\n * deployment — which `modelName()` answers.\n */\n protected body(params: ImageProviderParams): ImageGenerationRequest {\n return {\n model: this.modelName(),\n prompt: params.prompt,\n ...(params.size ? { size: params.size } : {}),\n ...(params.quality ? { quality: params.quality } : {}),\n ...(params.background ? { background: params.background } : {}),\n ...(params.format ? { output_format: params.format } : {}),\n ...(params.compression !== undefined ? { output_compression: params.compression } : {}),\n };\n }\n\n /** As `AgentProvider.normalizeError`, so an app branches on `rate_limited`\n * without caring which surface produced it. */\n normalizeError(error: unknown): AgentError {\n return normalizeProviderError(error);\n }\n}\n\nexport type ImageProviderParams = {\n prompt: string;\n size?: string;\n quality?: string;\n background?: string;\n format?: string;\n compression?: number;\n signal?: AbortSignal;\n};\n\nexport type ImageProviderEditParams = ImageProviderParams & {\n images: File[];\n mask?: File;\n};\n\n/**\n * Autocomplete, not a gate — and short on purpose. These are the image models\n * with a Responses-era `/images` surface; `gpt-image-2` is the one measured.\n */\nconst OPENAI_IMAGE_MODELS = [\"gpt-image-2\", \"gpt-image-1.5\", \"gpt-image-1\"] as const;\n\nexport class OpenAIImageProvider extends ImageProvider {\n readonly model: string;\n protected readonly config: ProviderConfig;\n\n constructor(model: string, config: ProviderConfig = {}) {\n super();\n this.model = model;\n this.config = config;\n }\n\n static model(model: string, config?: ProviderConfig): OpenAIImageProvider {\n return new OpenAIImageProvider(model, config);\n }\n\n static models(): readonly string[] {\n return OPENAI_IMAGE_MODELS;\n }\n\n protected endpoint(): ImagesEndpoint {\n return imagesEndpoint(openAITarget(this.config, { timeoutMs: DEFAULT_IMAGE_TIMEOUT_MS }));\n }\n}\n\n/**\n * Azure's image surface, which is the same resource and credential as its chat\n * surface — an app that already configured `AZURE_OPENAI_*` for an agent writes\n * only the deployment name here.\n *\n * `.model()`, not `.deployment()`, for the symmetry `AzureOpenAIProvider` keeps:\n * apps name a model and `AzureConfig.deployment` is the override for a resource\n * whose deployment is called something else.\n */\nexport class AzureOpenAIImageProvider extends ImageProvider {\n readonly model: string;\n protected readonly config: AzureConfig;\n\n constructor(model: string, config: AzureConfig = {}) {\n super();\n this.model = model;\n this.config = config;\n }\n\n static model(model: string, config?: AzureConfig): AzureOpenAIImageProvider {\n return new AzureOpenAIImageProvider(model, config);\n }\n\n static models(): readonly string[] {\n return OPENAI_IMAGE_MODELS;\n }\n\n protected modelName(): string {\n return this.config.deployment ?? this.model;\n }\n\n protected endpoint(): ImagesEndpoint {\n return imagesEndpoint(azureTarget(this.config, { timeoutMs: DEFAULT_IMAGE_TIMEOUT_MS }));\n }\n}\n",
12
16
  "import type { ProviderEvent } from \"../AgentProvider\";\nimport type { Usage } from \"../types\";\n\n/**\n * The Responses SSE stream to `ProviderEvent`.\n *\n * Also a pure function over chunks, for the same reason the request builder is:\n * everything that breaks here breaks on a byte boundary or a malformed frame,\n * and neither needs a network to reproduce. The tests drive it with recorded\n * fixture strings, including one fed a byte at a time.\n */\n\nexport type SSEMessage = { event: string; data: string; id?: string };\n\n/**\n * Chunks to SSE messages.\n *\n * Written out rather than reached for as a one-liner because every shortcut\n * here is a bug that only shows up under load. A frame split across two chunks\n * is the normal case, not the edge case — TCP has no idea what an event is.\n * Multi-line `data:` is spec, comment lines beginning with `:` are what\n * keepalives look like, and a `\\r\\n` stream is what you get through some\n * proxies.\n */\nexport async function* sseMessages(\n chunks: AsyncIterable<string> | Iterable<string>,\n): AsyncGenerator<SSEMessage> {\n let buffer = \"\";\n let event = \"\";\n let data: string[] = [];\n let id: string | undefined;\n\n const dispatch = (): SSEMessage | null => {\n if (data.length === 0 && !event) return null;\n const message: SSEMessage = { event, data: data.join(\"\\n\"), id };\n event = \"\";\n data = [];\n id = undefined;\n // A frame with a name but no data is legal and carries nothing we want.\n return message.data ? message : null;\n };\n\n const handleLine = (rawLine: string): SSEMessage | null => {\n // Trailing CR from a `\\r\\n` stream. Stripping it here rather than\n // normalizing the whole buffer keeps a CR that lands on a chunk boundary\n // from being counted as a line ending of its own.\n const line = rawLine.endsWith(\"\\r\") ? rawLine.slice(0, -1) : rawLine;\n if (line === \"\") return dispatch();\n // Keepalive. Servers send these so idle connections survive proxies, and a\n // parser that treats one as data corrupts the next real frame.\n if (line.startsWith(\":\")) return null;\n\n const colon = line.indexOf(\":\");\n const field = colon === -1 ? line : line.slice(0, colon);\n let value = colon === -1 ? \"\" : line.slice(colon + 1);\n if (value.startsWith(\" \")) value = value.slice(1);\n\n if (field === \"event\") event = value;\n else if (field === \"data\") data.push(value);\n else if (field === \"id\") id = value;\n return null;\n };\n\n for await (const chunk of chunks as AsyncIterable<string>) {\n buffer += chunk;\n let newline = buffer.indexOf(\"\\n\");\n while (newline !== -1) {\n const line = buffer.slice(0, newline);\n buffer = buffer.slice(newline + 1);\n const message = handleLine(line);\n if (message) yield message;\n newline = buffer.indexOf(\"\\n\");\n }\n }\n\n // A stream that ends without its final blank line still has one frame in it.\n if (buffer.length > 0) {\n const message = handleLine(buffer);\n if (message) yield message;\n }\n const last = dispatch();\n if (last) yield last;\n}\n\ntype ParseOptions = {\n /**\n * Set when the agent declared an `output` schema. The same\n * `response.output_text.delta` carries prose in one case and the JSON of the\n * final answer in the other, and only the caller knows which it asked for.\n */\n structuredOutput?: boolean;\n};\n\ntype PendingCall = { callId: string; name: string; namespace?: string; done: boolean };\n\nexport async function* parseResponsesStream(\n chunks: AsyncIterable<string> | Iterable<string>,\n options: ParseOptions = {},\n): AsyncGenerator<ProviderEvent> {\n // Keyed by `item_id`, because the argument deltas name the item and not the\n // call, and the call id is what the rest of gemi pairs results on.\n const calls = new Map<string, PendingCall>();\n const searchesReported = new Set<string>();\n let finished = false;\n\n for await (const message of sseMessages(chunks)) {\n if (message.data === \"[DONE]\") break;\n\n let payload: Record<string, any>;\n try {\n payload = JSON.parse(message.data);\n } catch {\n // A frame we cannot read is not a reason to abandon a stream that is\n // otherwise fine; the terminal event is what decides how this ended.\n continue;\n }\n\n // The event name is duplicated in the payload's own `type`. Preferring the\n // payload means a gateway that drops the `event:` line still parses, and\n // the two never disagree in practice.\n const type: string = typeof payload.type === \"string\" ? payload.type : message.event;\n\n switch (type) {\n case \"response.output_text.delta\": {\n const delta = String(payload.delta ?? \"\");\n if (!delta) break;\n yield options.structuredOutput\n ? { type: \"output-delta\", delta }\n : { type: \"text-delta\", delta };\n break;\n }\n\n // Two names for the same thing across model generations: the summary\n // stream and, on models that expose it, the reasoning text itself.\n case \"response.reasoning_summary_text.delta\":\n case \"response.reasoning_text.delta\": {\n const delta = String(payload.delta ?? \"\");\n if (!delta) break;\n yield { type: \"reasoning-delta\", delta, id: payload.item_id };\n break;\n }\n\n case \"response.output_item.added\": {\n const item = payload.item as Record<string, any> | undefined;\n if (!item) break;\n if (item.type === \"function_call\") {\n const itemId = String(item.id ?? payload.item_id ?? item.call_id ?? \"\");\n calls.set(itemId, {\n callId: String(item.call_id ?? itemId),\n // FLAT, and pinned by a test against the recorded stream. A call to\n // a function inside a namespace comes back as\n // `{name:\"getOrder\", namespace:\"crm\"}` — not `\"crm.getOrder\"` — so\n // the name is already the registry key `Agent` looks tools up by,\n // and qualifying it here would break every lookup. The namespace is\n // carried alongside rather than folded in, because a name that is\n // sometimes qualified is a name nothing can match on.\n name: String(item.name ?? \"\"),\n namespace: typeof item.namespace === \"string\" && item.namespace\n ? item.namespace\n : undefined,\n done: false,\n });\n }\n break;\n }\n\n case \"response.function_call_arguments.delta\": {\n const call = calls.get(String(payload.item_id ?? \"\"));\n if (!call) break;\n const argsDelta = String(payload.delta ?? \"\");\n if (!argsDelta) break;\n yield {\n type: \"tool-call-delta\",\n toolCallId: call.callId,\n name: call.name,\n argsDelta,\n // Spread rather than `namespace: call.namespace`: a flat tool has no\n // namespace, and an explicit `undefined` is a key a consumer has to\n // remember to check for.\n ...(call.namespace ? { namespace: call.namespace } : {}),\n };\n break;\n }\n\n case \"response.function_call_arguments.done\": {\n const itemId = String(payload.item_id ?? \"\");\n const call = calls.get(itemId);\n if (!call) break;\n call.done = true;\n yield {\n type: \"tool-call\",\n toolCallId: call.callId,\n name: call.name,\n args: String(payload.arguments ?? \"\"),\n ...(call.namespace ? { namespace: call.namespace } : {}),\n };\n break;\n }\n\n case \"response.output_item.done\": {\n const item = payload.item as Record<string, any> | undefined;\n if (!item) break;\n\n if (item.type === \"function_call\") {\n const itemId = String(item.id ?? payload.item_id ?? item.call_id ?? \"\");\n const call = calls.get(itemId);\n // Belt and braces: a call whose arguments never got a `done` event\n // still has to reach the agent, or the loop waits for a step that\n // will not arrive.\n if (call?.done) break;\n const namespace =\n (typeof item.namespace === \"string\" ? item.namespace : \"\") || call?.namespace;\n yield {\n type: \"tool-call\",\n toolCallId: String(item.call_id ?? itemId),\n name: String(item.name ?? call?.name ?? \"\"),\n args: String(item.arguments ?? \"\"),\n ...(namespace ? { namespace } : {}),\n };\n if (call) call.done = true;\n break;\n }\n\n if (item.type === \"tool_search_call\" || item.type === \"tool_search_output\") {\n // The call and its output are one thing to a user — \"went looking,\n // found these\" — so they collapse into one event. Two mechanisms do\n // that, and which one fires depends on what the server sent:\n //\n // 1. THE PAIR IS LINKED. The documented shape puts\n // `tool_search_call_id` on the output item, naming the call item's\n // id, so both sides key the same and the second one is dropped.\n //\n // 2. THE PAIR IS NOT LINKED, which is what the live API actually\n // sends. In `__fixtures__/openai-tool-search.sse` the call is\n // `tsc_08945…`, the output is `tso_08945…`, `call_id` is null on\n // both and neither carries `tool_search_call_id` — so there is\n // nothing to pair them on. What collapses them there is the\n // `found` check below: only the OUTPUT item carries a `tools`\n // array, and the call item carries the query it ran\n // (`arguments.paths`), which is not a result and is not reported\n // as one.\n //\n // That second one used to be load-bearing and unwritten — the dedup\n // key was doing nothing and the length check was doing all the work\n // by accident. Both are tested now: \"the tool_search call and its\n // output collapse into one event\" in `stream.test.ts` covers (1), and\n // \"nothing in the recording links the search call to its output\"\n // plus \"reports one event from the real stream, despite that\" in\n // `recordings.test.ts` cover (2).\n const found = toolSearchReport(item);\n if (found.loaded.length === 0 && found.namespaces.length === 0) break;\n const key = String(item.tool_search_call_id ?? item.id ?? \"\");\n if (searchesReported.has(key)) break;\n searchesReported.add(key);\n yield { type: \"tool-search\", ...found };\n }\n break;\n }\n\n // A refusal is the model declining, and it arrives as its own item rather\n // than as an HTTP status. Reporting it as text would put the refusal in\n // the transcript as if it were an answer.\n case \"response.refusal.done\": {\n yield {\n type: \"error\",\n error: {\n code: \"content_filtered\",\n message: String(payload.refusal ?? \"The model refused to answer.\"),\n retryable: false,\n },\n };\n break;\n }\n\n case \"response.completed\": {\n finished = true;\n yield { type: \"finish\", reason: \"stop\", usage: toUsage(payload.response?.usage) };\n break;\n }\n\n /**\n * Two very different endings share this frame, and the old code called\n * both of them \"stop\".\n *\n * `max_output_tokens` is a truncated answer. `content_filter` is Azure\n * blocking the request — verified against\n * `__fixtures__/azure-content-filtered.sse`, where a prompt that trips\n * the filter answers HTTP 200, streams a polite refusal, and ends\n * `response.incomplete` with `incomplete_details.reason` of\n * `content_filter`. Reporting that as a clean stop tells the agent the\n * model finished talking, which is exactly the mistake `content_filtered`\n * exists to prevent: the run reads as a normal answer, the loop takes\n * another step, and nothing anywhere says the content was blocked.\n *\n * The detail comes off Azure's `content_filters` array rather than off\n * the frame, because `incomplete_details` carries the word\n * `content_filter` and nothing else — no category, no severity.\n */\n case \"response.incomplete\": {\n finished = true;\n const usage = toUsage(payload.response?.usage);\n const reason = payload.response?.incomplete_details?.reason;\n if (reason === \"content_filter\") {\n yield {\n type: \"error\",\n error: {\n code: \"content_filtered\",\n message: describeContentFilters(payload.response?.content_filters),\n retryable: false,\n },\n };\n yield { type: \"finish\", reason: \"error\", usage };\n break;\n }\n yield {\n type: \"finish\",\n reason: reason === \"max_output_tokens\" ? \"length\" : \"stop\",\n usage,\n };\n break;\n }\n\n case \"response.failed\":\n case \"error\": {\n finished = true;\n const raw = payload.response?.error ?? payload.error ?? payload;\n yield { type: \"error\", error: normalizeStreamError(raw) };\n yield { type: \"finish\", reason: \"error\", usage: toUsage(payload.response?.usage) };\n break;\n }\n }\n\n if (finished) return;\n }\n\n // The connection closed with no terminal event: a dropped socket, a proxy\n // timing out, a process going away mid-answer. Reporting it as a clean stop\n // would tell the agent the model finished talking, and it did not — so this\n // is an error, and a retryable one, because the same request usually works.\n if (!finished) {\n yield {\n type: \"error\",\n error: {\n code: \"provider_error\",\n message: \"The provider stream ended without a terminal event.\",\n retryable: true,\n },\n };\n yield { type: \"finish\", reason: \"error\", usage: emptyUsage() };\n }\n}\n\n/**\n * What a tool search actually pulled in.\n *\n * The entries of `tool_search_output.tools` are NAMESPACES, not functions:\n *\n * [{type:\"namespace\", name:\"crm\", tools:[{type:\"function\", name:\"listOrders\"},\n * {type:\"function\", name:\"getOrder\"}]}]\n *\n * — verified against `__fixtures__/openai-tool-search.sse`. Reading `name` off\n * the top level, which is what this did, reported `loaded: [\"crm\"]` for a\n * search that loaded `listOrders` and `getOrder`. The names it reported were\n * not names of tools, and nothing downstream could tell, because a namespace\n * name is a plausible tool name.\n *\n * BOTH HALVES ARE REPORTED. \"Searched crm, loaded getOrder\" is the sentence a\n * UI wants, and neither field can be recovered from the other: flattening to\n * `crm.getOrder` would invent a name the model never used (calls come back with\n * a flat `name` — see the `function_call` branch above), and dropping the\n * namespace throws away the only description of the *group*, which is the thing\n * the model actually chose between.\n *\n * The shape is read structurally rather than off `type === \"namespace\"`: an\n * entry with a `tools` array is a group whatever it calls itself, and the\n * hand-written fixtures that predate the recording use `results:[{name}]` with\n * no `type` at all.\n */\ntype ToolSearchReport = { loaded: string[]; namespaces: string[] };\n\nfunction toolSearchReport(item: Record<string, any>): ToolSearchReport {\n const loaded: string[] = [];\n const namespaces: string[] = [];\n collectToolNames(item.results ?? item.tools ?? item.loaded ?? item.output, loaded, namespaces, 0);\n return { loaded: unique(loaded), namespaces: unique(namespaces) };\n}\n\nfunction collectToolNames(\n raw: unknown,\n loaded: string[],\n namespaces: string[],\n depth: number,\n): void {\n // Nesting is one level deep today and a namespace of namespaces is not a\n // thing. The cap is here so a payload that disagrees costs a truncated event\n // rather than a blown stack in the middle of someone's stream.\n if (!Array.isArray(raw) || depth > 4) return;\n for (const entry of raw) {\n if (typeof entry === \"string\") {\n loaded.push(entry);\n continue;\n }\n if (!entry || typeof entry !== \"object\") continue;\n const e = entry as Record<string, any>;\n const name =\n typeof e.name === \"string\" ? e.name : typeof e.tool_name === \"string\" ? e.tool_name : \"\";\n const children = e.tools ?? e.functions;\n if (Array.isArray(children)) {\n if (name) namespaces.push(name);\n collectToolNames(children, loaded, namespaces, depth + 1);\n continue;\n }\n if (name) loaded.push(name);\n }\n}\n\nfunction unique(names: string[]): string[] {\n return names.filter((name, index) => names.indexOf(name) === index);\n}\n\n/**\n * Azure's `content_filters`, read only where it means something.\n *\n * IT DOES NOT MAP ONTO `content_filtered` ON ITS OWN, and that is the decision\n * worth writing down: the array is on EVERY Azure response — `azure-text.sse`,\n * a recording of \"say hi\", carries it on `response.created`,\n * `response.in_progress` and `response.completed`, with `blocked:false` and\n * every category `severity:\"safe\"`. Treating its presence as a filter hit would\n * report every single Azure call as content-filtered, and treating any\n * `filtered:true` inside it as one would report a *warning* as a block. What is\n * authoritative about the outcome is `incomplete_details.reason`; this array is\n * authoritative only about the DETAIL, which is why it is read for a message\n * and for nothing else.\n *\n * OpenAI sends no such array. It signals a block by refusing in-band\n * (`response.refusal.done`, handled above) or by rejecting the request, so\n * nothing here needs a provider flag — an absent array just yields the generic\n * sentence.\n */\nfunction describeContentFilters(raw: unknown): string {\n const generic = \"The provider's content filter blocked this request.\";\n if (!Array.isArray(raw)) return generic;\n const hits: string[] = [];\n for (const entry of raw) {\n if (!entry || typeof entry !== \"object\") continue;\n const e = entry as Record<string, any>;\n const results = e.content_filter_results;\n if (!results || typeof results !== \"object\") continue;\n for (const [category, detail] of Object.entries(results as Record<string, any>)) {\n if (detail && typeof detail === \"object\" && detail.filtered === true) {\n // The source matters as much as the category: a `prompt` hit means the\n // user's own words were blocked and rewording works, a `completion` hit\n // means the model's answer was, and retrying the same prompt will not.\n hits.push(`${category} (${String(e.source_type ?? \"unknown\")})`);\n }\n }\n }\n return hits.length === 0 ? generic : `${generic} Categories: ${unique(hits).join(\", \")}.`;\n}\n\nfunction normalizeStreamError(raw: any) {\n const message = typeof raw?.message === \"string\" ? raw.message : \"The provider stream failed.\";\n const code = String(raw?.code ?? \"\").toLowerCase();\n if (code === \"rate_limit_exceeded\") {\n return { code: \"rate_limited\" as const, message, retryable: true };\n }\n if (code === \"context_length_exceeded\") {\n return { code: \"context_length_exceeded\" as const, message, retryable: false };\n }\n if (code.includes(\"content_filter\")) {\n return { code: \"content_filtered\" as const, message, retryable: false };\n }\n // Mid-stream failures are overwhelmingly transient — the request was accepted,\n // so it was not malformed.\n return { code: \"provider_error\" as const, message, retryable: true };\n}\n\nexport function emptyUsage(): Usage {\n return { inputTokens: 0, outputTokens: 0, totalTokens: 0 };\n}\n\nexport function toUsage(raw: any): Usage {\n if (!raw) return emptyUsage();\n const inputTokens = Number(raw.input_tokens ?? 0);\n const outputTokens = Number(raw.output_tokens ?? 0);\n const usage: Usage = {\n inputTokens,\n outputTokens,\n totalTokens: Number(raw.total_tokens ?? inputTokens + outputTokens),\n };\n const reasoning = raw.output_tokens_details?.reasoning_tokens;\n if (typeof reasoning === \"number\") usage.reasoningTokens = reasoning;\n const cached = raw.input_tokens_details?.cached_tokens;\n if (typeof cached === \"number\") usage.cachedInputTokens = cached;\n return usage;\n}\n\n/** A `ReadableStream` of bytes to the string chunks the parser wants. Split out\n * so the parser never has to know it came from a socket. */\nexport async function* decodeChunks(\n body: ReadableStream<Uint8Array> | null,\n): AsyncGenerator<string> {\n if (!body) return;\n const reader = body.getReader();\n const decoder = new TextDecoder();\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n // `stream: true` matters: a multi-byte character can land across two\n // reads, and decoding each read on its own turns it into two U+FFFDs.\n if (value) yield decoder.decode(value, { stream: true });\n }\n const tail = decoder.decode();\n if (tail) yield tail;\n } finally {\n reader.releaseLock();\n }\n}\n",
13
- "import type { ProviderEvent } from \"../AgentProvider\";\nimport { normalizeProviderError } from \"./errors\";\nimport { requestWithRetry, type FetchLike } from \"./http\";\nimport type { ResponsesRequest } from \"./request\";\nimport { decodeChunks, emptyUsage, parseResponsesStream } from \"./stream\";\n\n/**\n * The parts of a call that differ between OpenAI and Azure, and nothing else.\n *\n * Two classes, one request path: the differences are a URL, a header and when\n * the credential is read, so those are what gets passed in. Everything below\n * this line — retries, decoding, event translation, what an error looks like —\n * is identical, and duplicating it into the Azure class is how the two would\n * start behaving differently by accident.\n */\nexport type ResponsesEndpoint = {\n responsesUrl: string;\n filesUrl: string;\n /** Async because Entra tokens expire mid-conversation, so the credential has\n * to be read per request rather than per provider. */\n headers: () => Promise<Record<string, string>>;\n timeoutMs: number;\n maxRetries: number;\n fetchImpl?: FetchLike;\n};\n\n/**\n * Errors reach the consumer as events, not exceptions.\n *\n * A `ProviderStream` that threw would make every caller wrap its `for await`,\n * and would lose the deltas already yielded — the agent needs the text it got\n * before the socket died, because the user has already read it.\n */\nexport async function* streamResponses(\n endpoint: ResponsesEndpoint,\n body: ResponsesRequest,\n params: { signal?: AbortSignal; structuredOutput: boolean },\n): AsyncGenerator<ProviderEvent> {\n let response: Response;\n try {\n response = await requestWithRetry(\n endpoint.responsesUrl,\n {\n method: \"POST\",\n headers: { ...(await endpoint.headers()), \"content-type\": \"application/json\" },\n body: JSON.stringify(body),\n },\n {\n maxRetries: endpoint.maxRetries,\n timeoutMs: endpoint.timeoutMs,\n signal: params.signal,\n fetchImpl: endpoint.fetchImpl,\n },\n );\n } catch (error) {\n const normalized = normalizeProviderError(error);\n // An abort is not a failure to report: the run was stopped on purpose, and\n // `Agent` is already writing the ending. Saying so twice would put an error\n // in a transcript the user closed themselves.\n if (normalized.code !== \"aborted\") yield { type: \"error\", error: normalized };\n yield {\n type: \"finish\",\n reason: normalized.code === \"aborted\" ? \"aborted\" : \"error\",\n usage: emptyUsage(),\n };\n return;\n }\n\n try {\n yield* parseResponsesStream(decodeChunks(response.body), {\n structuredOutput: params.structuredOutput,\n });\n } catch (error) {\n const normalized = normalizeProviderError(error);\n if (normalized.code === \"aborted\") {\n yield { type: \"finish\", reason: \"aborted\", usage: emptyUsage() };\n return;\n }\n yield { type: \"error\", error: normalized };\n yield { type: \"finish\", reason: \"error\", usage: emptyUsage() };\n }\n}\n\n/**\n * Uploads an attachment and returns the id a `FilePart` carries.\n *\n * `user_data` rather than `assistants`: this is a file a person attached to a\n * message, not a corpus for a retrieval store, and the purpose is what decides\n * which of the two the file can be used for.\n */\nexport async function uploadFile(endpoint: ResponsesEndpoint, file: File): Promise<string> {\n const form = new FormData();\n form.set(\"purpose\", \"user_data\");\n form.set(\"file\", file);\n\n const response = await requestWithRetry(\n endpoint.filesUrl,\n {\n method: \"POST\",\n // No content-type: the boundary is generated with the body, and setting\n // the header by hand is how multipart uploads fail with a parser error\n // that names nothing useful.\n headers: await endpoint.headers(),\n body: form,\n },\n {\n maxRetries: endpoint.maxRetries,\n timeoutMs: endpoint.timeoutMs,\n fetchImpl: endpoint.fetchImpl,\n },\n );\n\n const json = (await response.json()) as { id?: string };\n if (!json?.id) throw new Error(\"The provider accepted the upload but returned no file id.\");\n return json.id;\n}\n",
17
+ "import type { ProviderEvent } from \"../AgentProvider\";\nimport type { ProviderTarget } from \"./endpoints\";\nimport { normalizeProviderError } from \"./errors\";\nimport { requestWithRetry, type FetchLike } from \"./http\";\nimport type { ResponsesRequest } from \"./request\";\nimport { decodeChunks, emptyUsage, parseResponsesStream } from \"./stream\";\n\n/**\n * The parts of a call that differ between OpenAI and Azure, and nothing else.\n *\n * Two classes, one request path: the differences are a URL, a header and when\n * the credential is read, so those are what gets passed in. Everything below\n * this line — retries, decoding, event translation, what an error looks like —\n * is identical, and duplicating it into the Azure class is how the two would\n * start behaving differently by accident.\n */\nexport type ResponsesEndpoint = {\n responsesUrl: string;\n filesUrl: string;\n /** Async because Entra tokens expire mid-conversation, so the credential has\n * to be read per request rather than per provider. */\n headers: () => Promise<Record<string, string>>;\n timeoutMs: number;\n maxRetries: number;\n fetchImpl?: FetchLike;\n};\n\n/**\n * The Responses paths for a resolved vendor.\n *\n * The two URLs are all that distinguishes this from any other call to the same\n * host, which is why the host, the credential and the api-version are worked out\n * by `providers/endpoints.ts` and appended to here rather than resolved twice.\n * Files are resource-scoped rather than deployment-scoped on Azure — an upload\n * is not addressed to a model — and the deployment goes in the body's `model`,\n * which `buildResponsesRequest` already does.\n */\nexport function responsesEndpoint(target: ProviderTarget): ResponsesEndpoint {\n return {\n responsesUrl: `${target.base}/responses${target.query}`,\n filesUrl: `${target.base}/files${target.query}`,\n headers: target.headers,\n timeoutMs: target.timeoutMs,\n maxRetries: target.maxRetries,\n };\n}\n\n/**\n * Errors reach the consumer as events, not exceptions.\n *\n * A `ProviderStream` that threw would make every caller wrap its `for await`,\n * and would lose the deltas already yielded — the agent needs the text it got\n * before the socket died, because the user has already read it.\n */\nexport async function* streamResponses(\n endpoint: ResponsesEndpoint,\n body: ResponsesRequest,\n params: { signal?: AbortSignal; structuredOutput: boolean },\n): AsyncGenerator<ProviderEvent> {\n let response: Response;\n try {\n response = await requestWithRetry(\n endpoint.responsesUrl,\n {\n method: \"POST\",\n headers: { ...(await endpoint.headers()), \"content-type\": \"application/json\" },\n body: JSON.stringify(body),\n },\n {\n maxRetries: endpoint.maxRetries,\n timeoutMs: endpoint.timeoutMs,\n signal: params.signal,\n fetchImpl: endpoint.fetchImpl,\n },\n );\n } catch (error) {\n const normalized = normalizeProviderError(error);\n // An abort is not a failure to report: the run was stopped on purpose, and\n // `Agent` is already writing the ending. Saying so twice would put an error\n // in a transcript the user closed themselves.\n if (normalized.code !== \"aborted\") yield { type: \"error\", error: normalized };\n yield {\n type: \"finish\",\n reason: normalized.code === \"aborted\" ? \"aborted\" : \"error\",\n usage: emptyUsage(),\n };\n return;\n }\n\n try {\n yield* parseResponsesStream(decodeChunks(response.body), {\n structuredOutput: params.structuredOutput,\n });\n } catch (error) {\n const normalized = normalizeProviderError(error);\n if (normalized.code === \"aborted\") {\n yield { type: \"finish\", reason: \"aborted\", usage: emptyUsage() };\n return;\n }\n yield { type: \"error\", error: normalized };\n yield { type: \"finish\", reason: \"error\", usage: emptyUsage() };\n }\n}\n\n/**\n * Uploads an attachment and returns the id a `FilePart` carries.\n *\n * `user_data` rather than `assistants`: this is a file a person attached to a\n * message, not a corpus for a retrieval store, and the purpose is what decides\n * which of the two the file can be used for.\n */\nexport async function uploadFile(endpoint: ResponsesEndpoint, file: File): Promise<string> {\n const form = new FormData();\n form.set(\"purpose\", \"user_data\");\n form.set(\"file\", file);\n\n const response = await requestWithRetry(\n endpoint.filesUrl,\n {\n method: \"POST\",\n // No content-type: the boundary is generated with the body, and setting\n // the header by hand is how multipart uploads fail with a parser error\n // that names nothing useful.\n headers: await endpoint.headers(),\n body: form,\n },\n {\n maxRetries: endpoint.maxRetries,\n timeoutMs: endpoint.timeoutMs,\n fetchImpl: endpoint.fetchImpl,\n },\n );\n\n const json = (await response.json()) as { id?: string };\n if (!json?.id) throw new Error(\"The provider accepted the upload but returned no file id.\");\n return json.id;\n}\n",
14
18
  "import type { ProviderCapabilities } from \"../AgentProvider\";\n\n/**\n * What a model can do, read off its id.\n *\n * Hardcoding `true` was never an option — `reasoning: { effort }` is a 400 on\n * gpt-4o, and `defer_loading` is a 400 on anything that predates tool search —\n * but neither is a table lookup that only answers for ids we shipped knowing\n * about.\n *\n * SO AN UNKNOWN ID GETS EVERY CAPABILITY. That is the whole point: a model\n * released next Tuesday must be usable by writing its name, not by waiting for\n * a gemi release. The default is chosen for how it fails, not for how often it\n * is right. Guessing high fails loudly and once — the API rejects the request\n * and names the parameter it disliked, and the fix is one line of config.\n * Guessing low fails silently and forever: reasoning is dropped, every deferred\n * schema is inlined, and the only symptom is a bigger bill and a worse answer.\n * Unknown ids also skew new rather than old, because nobody invents the name of\n * a model that already shipped.\n *\n * The named families below exist to make the *known-old* cases right, which is\n * the only place a guess can be wrong in the quiet direction.\n *\n * Note what an answer of `false` does and does not buy. `reasoning: false` and\n * `toolSearch: false` change the request, because both are optimizations and\n * the run is identical without them. `structuredOutput: false` does not: it is\n * reported honestly for a caller that wants to branch on it, but the request\n * builder still sends the schema, because an agent that declared an `output`\n * has an app waiting on a typed result and dropping the parameter would answer\n * prose forever with nothing to branch on. See `request.ts`.\n *\n * `fileInput` IS DELIBERATELY STILL ONE FLAG, and this was reopened rather than\n * inherited. Splitting it into \"reads images\" and \"reads documents\" was the\n * obvious answer to #488, so it was measured before it was written: gpt-5.4 and\n * gpt-4o were each sent an uploaded PNG as `input_image` and an uploaded PDF as\n * `input_file`, and all four calls returned 200 with the right answer (the\n * transcript is in `request.ts`, above `IMAGE_EXTENSIONS`). The two abilities\n * did not come apart on either model, so there is nothing here for a second\n * flag to describe.\n *\n * Adding it anyway would be worse than none. Every guess in this file is a\n * guess about an id nobody has run yet, and the guess is high on purpose; a\n * second flag does not describe the world more finely, it doubles the number of\n * ways that guess can be wrong quietly, and the wrong-quiet direction is the\n * one with no error message. The split earns its place the day a model answers\n * 200 to one of those two calls and 400 to the other — and that call is cheap\n * to repeat, which is why the recipe is written down rather than the conclusion\n * alone.\n */\nexport function capabilitiesForModel(model: string): ProviderCapabilities {\n const id = normalizeModelId(model);\n\n // Everything before the tool-era models. Listed by prefix because these names\n // are closed sets now — nothing new will be called `gpt-3.5-*`.\n if (id.startsWith(\"gpt-3.5\") || id.startsWith(\"text-\") || id.startsWith(\"davinci\")) {\n return {\n reasoning: false,\n structuredOutput: false,\n fileInput: false,\n parallelToolCalls: true,\n toolSearch: false,\n };\n }\n\n const family = parseFamily(id);\n\n // o1 reasons but takes its tool calls one at a time; o3/o4 do not have that\n // restriction. Both predate tool search.\n if (family?.kind === \"o\") {\n return {\n reasoning: true,\n structuredOutput: true,\n fileInput: true,\n parallelToolCalls: family.major > 1,\n toolSearch: supportsToolSearch(family),\n };\n }\n\n if (family?.kind === \"gpt\") {\n return {\n // gpt-4 and gpt-4o are strong models with no reasoning parameter at all.\n reasoning: family.major >= 5,\n // Strict `json_schema` landed with gpt-4o; plain gpt-4 only has json_object.\n structuredOutput: family.major > 4 || id.startsWith(\"gpt-4o\") || id.startsWith(\"gpt-4.1\"),\n fileInput: family.major > 4 || id.startsWith(\"gpt-4o\") || id.startsWith(\"gpt-4.1\"),\n parallelToolCalls: true,\n toolSearch: supportsToolSearch(family),\n };\n }\n\n return {\n reasoning: true,\n structuredOutput: true,\n fileInput: true,\n parallelToolCalls: true,\n toolSearch: true,\n };\n}\n\n/**\n * Tool search, by generation.\n *\n * MEASURED, not read off a changelog. Every id below was sent a request\n * carrying `{type:\"tool_search\"}` plus one `namespace` of deferred functions,\n * against `https://api.openai.com/v1/responses`:\n *\n * accepted (200): gpt-5.6-terra, gpt-5.5, gpt-5.4, gpt-5.4-mini,\n * gpt-5.3-codex, gpt-5.2\n * rejected (400): gpt-5.1, gpt-5, gpt-5-mini, gpt-4.1, gpt-4o, o4-mini, o3\n *\n * with the rejection reading, verbatim,\n * `Tool 'tool_search' is not supported with gpt-5.1.` — recorded as\n * `__fixtures__/openai-error-tool-search-unsupported.json`.\n *\n * So the boundary is the *minor* number, and the whole-major rule this used to\n * carry (`major >= 5`) was wrong in the expensive direction for four shipped\n * models: gpt-5, gpt-5-mini and gpt-5.1 would have had `defer_loading` and a\n * `tool_search` tool put in every request and answered 400 on all of them.\n * Reading the minor is the only way to be right here, because gpt-5 and gpt-5.4\n * differ by a decimal point and by this capability.\n *\n * The o-series keeps its `major >= 5` guard rather than being hardcoded false:\n * o1 through o4 are all measured rejections above, and an o5 that does not\n * exist gets the same benefit-of-the-doubt an unknown id gets, for the reason\n * in the module comment.\n */\nfunction supportsToolSearch(family: Family): boolean {\n if (family.kind === \"o\") return family.major >= 5;\n return family.major > 5 || (family.major === 5 && family.minor >= 2);\n}\n\n/**\n * Azure deployment names are chosen by whoever ran the ARM template, so half of\n * them look nothing like a model id. That is not a special case here: an\n * unrecognizable deployment name lands on the same all-true default as an\n * unrecognized model, for the same reason.\n */\nfunction normalizeModelId(model: string): string {\n return model.trim().toLowerCase();\n}\n\nexport type Family = { kind: \"gpt\" | \"o\"; major: number; minor: number };\n\n/**\n * Reads the generation out of `gpt-5.4-mini-2025-01-01` or `o3-mini`.\n *\n * The minor number is read as well as the major, and it is load-bearing: tool\n * search arrived at gpt-5.2, so `gpt-5` and `gpt-5.4` are two different answers\n * to the same question and a major-only reading gets one of them wrong. An id\n * with no minor — `gpt-5`, `gpt-4o`, `o3` — is minor 0, which is what it is.\n *\n * Exported for its own test: the classification is what the guards below are\n * really about, and it is the only place they are observable — two ids can\n * classify differently and still land on the same capability answer today.\n */\nexport function parseFamily(id: string): Family | null {\n const gpt = /^gpt-(\\d+)(?:\\.(\\d+))?/.exec(id);\n if (gpt?.[1]) return { kind: \"gpt\", major: Number(gpt[1]), minor: Number(gpt[2] ?? 0) };\n // `o1`, `o3-mini`, `o4-mini`. The boundary is what keeps an id whose leading\n // digits are not a generation out of this family — `o200k-base` is a\n // tokenizer, not an o-series model, and `/^o(\\d+)/` alone reads it as\n // generation 200. (`omni-moderation` never gets this far: `m` is not a\n // digit.) Both land on the unknown default today, so the boundary is only\n // visible in the classification — which is where a future rule keyed on\n // `major` would read it.\n const o = /^o(\\d+)(?:[-.]|$)/.exec(id);\n // The o-series never had a minor: `o3-mini` is a size, not a point release.\n if (o?.[1]) return { kind: \"o\", major: Number(o[1]), minor: 0 };\n return null;\n}\n",
15
19
  "import type {\n ProviderCapabilities,\n ProviderStreamParams,\n ProviderToolNamespace,\n ProviderToolSpec,\n} from \"../AgentProvider\";\nimport { ATTACHMENT_ID_PREFIX } from \"../store/Attachments\";\nimport type { AgentMessage, FilePart, ToolResultPart } from \"../types\";\n\n/**\n * Building the request body is a pure function, on purpose.\n *\n * Everything hard about this provider is in here — item ordering, tool-call\n * pairing, what a denied call looks like on the wire — and none of it needs a\n * socket to be wrong. So it is separated from the class that posts it, and the\n * tests assert the object rather than mocking `fetch` and reading a string.\n */\n\n// The wire shapes, typed loosely on purpose: these mirror OpenAI's schema, and\n// a precise mirror is a second thing to keep in sync for no checking we would\n// actually get — the API is the authority and it answers in HTTP.\nexport type ResponsesInputItem = Record<string, unknown>;\nexport type ResponsesTool = Record<string, unknown>;\n\nexport type ResponsesRequest = {\n model: string;\n input: ResponsesInputItem[];\n stream: true;\n instructions?: string;\n tools?: ResponsesTool[];\n parallel_tool_calls?: boolean;\n text?: { format: Record<string, unknown> };\n reasoning?: { effort: string; summary: \"auto\" };\n temperature?: number;\n max_output_tokens?: number;\n};\n\nexport function buildResponsesRequest(\n params: ProviderStreamParams,\n ctx: { model: string; capabilities: ProviderCapabilities },\n): ResponsesRequest {\n const { capabilities } = ctx;\n\n const body: ResponsesRequest = {\n model: ctx.model,\n input: toResponsesInput(params.messages, capabilities),\n stream: true,\n };\n\n if (params.systemPrompt) body.instructions = params.systemPrompt;\n\n const tools = toResponsesTools(params.tools, capabilities);\n if (tools.length > 0) {\n body.tools = tools;\n if (!capabilities.parallelToolCalls) body.parallel_tool_calls = false;\n }\n\n // Deliberately NOT gated on `capabilities.structuredOutput`. An agent that\n // declares `output` has a typed result its app is going to read, and dropping\n // the parameter does not degrade that gracefully — it produces prose, which\n // arrives as `text-delta`, so the app sees no output part, no error and\n // nothing to branch on. That is the silent-forever failure `capabilities.ts`\n // argues against: send it and let the API answer 400, which is loud, happens\n // once, and names the parameter it disliked. `reasoning` is dropped below\n // because it is an optimization; an output schema is the answer's shape.\n if (params.output) {\n body.text = {\n format: {\n type: \"json_schema\",\n name: params.output.name,\n schema: params.output.schema,\n strict: params.output.strict,\n },\n };\n }\n\n // Dropped rather than refused: a model that cannot reason should still answer\n // an agent that asked it to, because `reasoning` is the agent's preference\n // and the provider is where preferences meet reality.\n if (params.reasoning && capabilities.reasoning) {\n // `summary: \"auto\"` is not decoration — without it the stream carries no\n // reasoning text at all, and `ReasoningPart` would have nothing to hold.\n body.reasoning = { effort: params.reasoning, summary: \"auto\" };\n }\n\n if (typeof params.temperature === \"number\") body.temperature = params.temperature;\n if (typeof params.maxOutputTokens === \"number\") body.max_output_tokens = params.maxOutputTokens;\n\n return body;\n}\n\n// --- messages ------------------------------------------------------------\n\n/**\n * `AgentMessage[]` to Responses input items.\n *\n * Order is preserved *within* a message, not just between messages: a message\n * that holds reasoning, then a tool call, then its result has to arrive in that\n * order, because the API validates the pairing positionally. So text and file\n * parts are buffered into one message item and that buffer is flushed the\n * moment a non-message item appears, rather than emitting all the text first\n * and all the calls after.\n */\nexport function toResponsesInput(\n messages: AgentMessage[],\n capabilities: ProviderCapabilities,\n): ResponsesInputItem[] {\n const items: ResponsesInputItem[] = [];\n\n for (const message of messages) {\n const role = message.role;\n let buffer: Record<string, unknown>[] = [];\n\n const flush = () => {\n if (buffer.length === 0) return;\n items.push({ type: \"message\", role, content: buffer });\n buffer = [];\n };\n\n for (const part of message.content ?? []) {\n switch (part.type) {\n case \"text\": {\n if (!part.text) break;\n buffer.push(textContent(role, part.text));\n break;\n }\n case \"output\": {\n // A structured answer is still the assistant's text as far as the\n // history is concerned; re-serializing it is what lets a follow-up\n // turn refer to what was decided.\n if (part.partial) break;\n buffer.push(textContent(role, JSON.stringify(part.value)));\n break;\n }\n case \"file\": {\n // Neither `input_file` nor `input_image` is legal on an output role.\n // An assistant message holding a file is a bug upstream, and sending\n // it anyway turns that bug into a 400 halfway through a conversation.\n // The attachment line goes with it: `output_text` saying \"here is a\n // file\" on the assistant's side would be the model told it produced\n // something it did not.\n if (role === \"assistant\") break;\n const attachmentId = attachmentIdOf(part);\n // A model that cannot read files still gets the line. The file is\n // gemi's either way, and a tool can take it by id even though the\n // model cannot look at it — dropping the line too would leave the\n // model unaware the user attached anything, which is the storage-only\n // failure this line exists to fix.\n if (!capabilities.fileInput) {\n if (attachmentId) buffer.push(textContent(role, attachmentLine(part, false)));\n break;\n }\n // NOT A PROVIDER FILE ID. There are two ids now — `POST /chat/files`\n // answers `fileId` (the provider's) and `attachmentId` (ours) — and\n // putting the wrong one here is the mistake the shapes invite. Left\n // alone it is an error from the vendor about a file it has never heard\n // of, arriving mid-conversation and naming nothing a reader can act\n // on. (Which error is NOT MEASURED: no request was made with a bogus\n // `file_id` to find out whether it is a 400 or a 404, or whether the\n // part is dropped and the rest of the turn proceeds. The guard does\n // not depend on the answer — it is worth having on the shapes alone —\n // so this comment names the failure rather than a status code nobody\n // checked.)\n if (part.fileId?.startsWith(ATTACHMENT_ID_PREFIX)) {\n throw new Error(\n `FilePart.fileId holds a gemi attachment id (${part.fileId}). That field is the *provider's* file id, from \\`fileId\\` on the upload response; the \\`attachmentId\\` goes in \\`FilePart.attachmentId\\`, is shown to the model as text, and is resolved by a tool through \\`ctx.attachments\\`.`,\n );\n }\n // A storage-only upload has no provider id, and that is a legal part\n // now: the model is told the file exists and is not shown it. What\n // is still refused is a part with NEITHER id — nothing to show and\n // nothing to name. The emptiness check is `!part.fileId` rather than\n // a prefix test because `undefined?.startsWith` is `undefined`, and\n // `file_id: undefined` reaches the vendor as surely as a wrong string.\n if (!part.fileId && !attachmentId) {\n throw new Error(\n \"FilePart.fileId is empty and there is no `attachmentId` either. `fileId` is the *provider's* file id and `attachmentId` is gemi's (`gemi_att_…`), both from the upload response; an upload always answers at least one, so a part with neither was built from something other than that answer.\",\n );\n }\n if (attachmentId) buffer.push(textContent(role, attachmentLine(part, !!part.fileId)));\n if (part.fileId) buffer.push(fileContent(part));\n break;\n }\n case \"reasoning\": {\n flush();\n const item = reasoningItem(part);\n if (item) items.push(item);\n break;\n }\n case \"tool-call\": {\n // A partial call is UI state — the arguments were still streaming\n // when this was written down, so what it holds is not what the model\n // asked for. Sending it would create the dangling call the API\n // rejects. If it did acquire a result (a stop landing mid-arguments\n // does exactly that), `reconcileToolPairs` drops that half too.\n if (part.partial) break;\n flush();\n items.push({\n type: \"function_call\",\n call_id: part.toolCallId,\n name: String(part.name),\n arguments: JSON.stringify(part.input ?? {}),\n });\n break;\n }\n case \"tool-result\": {\n flush();\n items.push({\n type: \"function_call_output\",\n call_id: part.toolCallId,\n output: toolResultOutput(part),\n });\n break;\n }\n }\n }\n\n flush();\n }\n\n return reconcileToolPairs(items);\n}\n\n/** What is sent for a call whose result never made it into the history. */\nconst NO_RESULT_RECORDED = \"No result was recorded for this tool call. Assume it did not complete.\";\n\n/**\n * Every `function_call` has exactly one `function_call_output`, and no output\n * stands alone.\n *\n * The loop above skips a `tool-call` marked `partial` — its arguments were\n * still streaming, so it is UI state rather than something the model did — but\n * a partial call can still acquire a result: `stop()` gives every call in\n * flight a `denied`/`stopped` result, and a call whose arguments were mid-flight\n * is exactly the one carrying `partial`. Emitting that result on its own is a\n * 400 (\"no tool call found for function call output\"), and because the history\n * is persisted it is a 400 on every subsequent turn — the thread is bricked,\n * which is the failure `denied` exists to prevent.\n *\n * So the invariant is enforced here rather than assumed part-by-part: an\n * unpartnered output is dropped, a repeated one is dropped, and a call left\n * dangling by a crash between the call and its result gets a synthetic output\n * saying so. Fabricating that line is the lesser evil — it is true, the model\n * can read it, and the alternative is a conversation that can never be\n * continued.\n *\n * A repeated *call* is dropped for the same reason in the other direction. The\n * API happens to accept two `function_call`s under one id, so this is not a\n * 400 — it is the model reading the same call twice, on every turn, for the\n * rest of the conversation. The way it arises is a store that appended the\n * amended copy of a message instead of replacing it; the store contract now\n * says upsert, and this is the guard for a store that did not read it.\n */\nfunction reconcileToolPairs(items: ResponsesInputItem[]): ResponsesInputItem[] {\n const called = new Set<string>();\n const answered = new Set<string>();\n const kept: ResponsesInputItem[] = [];\n\n for (const item of items) {\n if (item.type === \"function_call\") {\n const callId = String(item.call_id);\n if (called.has(callId)) continue;\n called.add(callId);\n } else if (item.type === \"function_call_output\") {\n const callId = String(item.call_id);\n // Positional: the output has to come *after* its call, which is what the\n // API checks, so a call seen later in the history does not rescue it.\n if (!called.has(callId) || answered.has(callId)) continue;\n answered.add(callId);\n }\n kept.push(item);\n }\n\n if (answered.size === called.size) return kept;\n\n const out: ResponsesInputItem[] = [];\n for (const item of kept) {\n out.push(item);\n if (item.type !== \"function_call\") continue;\n const callId = String(item.call_id);\n if (answered.has(callId)) continue;\n answered.add(callId);\n out.push({ type: \"function_call_output\", call_id: callId, output: NO_RESULT_RECORDED });\n }\n return out;\n}\n\nfunction textContent(role: AgentMessage[\"role\"], text: string): Record<string, unknown> {\n return { type: role === \"assistant\" ? \"output_text\" : \"input_text\", text };\n}\n\n/**\n * `FilePart.attachmentId`, if it is one.\n *\n * In stateless mode the history arrives from the browser and only the turn\n * itself is checked at the door (`toClientTurn`), so a part posted back as\n * history can hold anything here. A value that is not a gemi id is treated as\n * absent rather than refused: it names nothing a tool could resolve, and\n * refusing it would fail every later turn of a conversation over a field the\n * model could not have used.\n */\nfunction attachmentIdOf(part: FilePart): string | undefined {\n const id = part.attachmentId;\n return typeof id === \"string\" && id.startsWith(ATTACHMENT_ID_PREFIX) ? id : undefined;\n}\n\n/**\n * The line that tells the model an attachment's id, sent just before the file\n * block it labels (or alone, when there is no file block to send).\n *\n * WHY A LINE AT ALL. A model that has seen a user's image has no id to put in\n * a tool's arguments unless it is told one. Whatever it writes there instead\n * is answered by `ctx.attachments.file()` with `AttachmentNotFoundError` —\n * correctly, for an id that was never obtainable. For a storage-only upload\n * this line is all the model gets: without it, it does not know a file exists.\n *\n * WHY THIS FORMAT. `[attachment id=\"…\" name=\"…\" mimeType=\"…\"]`, each value\n * JSON-quoted. The filename is the user's and can hold spaces, quotes, `]` or\n * a newline; JSON quoting means none of them can end a field, end the bracket,\n * or start a second line that reads as another attachment, so the id a model\n * copies out is exactly the id. The unquoted form (`[attachment gemi_att_… my\n * photo.png image/png]`) looks tidier and is ambiguous as soon as a name has a\n * space. A key with no value is left out rather than written as `name=\"\"`.\n *\n * WHY IT SAYS WHEN THE FILE IS NOT SHOWN. A storage-only upload, or any file\n * sent to a model without `fileInput`, has no block beside the line. Left\n * unsaid, a line that names `photo.png` invites the model to describe a\n * picture it never saw.\n */\nfunction attachmentLine(part: FilePart, shown: boolean): string {\n const fields = [`id=${JSON.stringify(part.attachmentId)}`];\n if (part.name) fields.push(`name=${JSON.stringify(part.name)}`);\n if (part.mimeType) fields.push(`mimeType=${JSON.stringify(part.mimeType)}`);\n const line = `attachment ${fields.join(\" \")}`;\n return shown\n ? `[${line}]`\n : `[${line} — its contents are not shown to you; a tool can read the file by this id]`;\n}\n\n/**\n * An attachment onto the content block that can carry it.\n *\n * `input_file` is the document path and `input_image` is the vision one, and\n * they are not interchangeable in either direction — the API refuses the wrong\n * pairing with a 400 rather than degrading. So this branch is not a nicety\n * about how well an image is read; it decides whether the turn happens at all.\n * What was measured, on which models, with the verbatim rejections, is recorded\n * above `IMAGE_EXTENSIONS`.\n *\n * `file_id` is the same field on both blocks, so nothing about the upload\n * changes: `uploadFile` posts once with `purpose: \"user_data\"` and the id it\n * returns is legal in either. `detail` is deliberately not sent on the image\n * block — it is optional, the API defaults it, and a `FilePart` carries no\n * signal that would justify choosing anything but that default.\n *\n * SVG is the case the `image/` prefix gets wrong. `image/svg+xml` is an image\n * MIME type, and .svg is on the API's *document* list and off its image list —\n * so a prefix test alone sends it to the one block that is guaranteed to refuse\n * it. It is routed by what the API calls it, not by what the MIME type calls\n * it.\n *\n * WHEN `mimeType` IS ABSENT OR EMPTY the file name decides, and if that settles\n * nothing the part is sent as `input_file`. Both halves matter.\n *\n * The name branch is NOT a rescue for un-typed legacy rows, and deleting it as\n * one would break a live upload. The browser path has always written the field:\n * `useChat.uploadFile` has stored `data.mimeType ?? file.type` since the module\n * landed (`git log -S mimeType -- ai/useChat.tsx` bottoms out at d940676e), so\n * there is no history of `undefined` MIME types to point at, and a backfill\n * would not make this branch dead. What it is actually for is the two cases\n * that still leave nothing to read. `File.type` is the EMPTY STRING, not\n * absent, for a file the browser cannot type — so `?? file.type` stores `\"\"`\n * and this branch fires on a perfectly current upload — and a `FilePart`\n * assembled by a server-side caller may set neither field, because the type\n * marks both optional.\n *\n * The name is also the right thing to fall back to rather than merely the last\n * thing left: the server classifies by the STORED FILE NAME'S EXTENSION, not by\n * the bytes and not by the upload's `Content-Type` (measured; the transcript is\n * below). And what it rescues is not a cosmetic degradation — an image part\n * that lands on `input_file` 400s, and because that part lives in stored\n * history and goes back up on every request, it 400s every remaining turn of\n * the thread. `useChat.uploadFile` fills `name` from the local `File` and the\n * file is uploaded under that same name, so such a part almost always still\n * says `.png`.\n *\n * Falling back to `input_file` past that is the conservative half — a part with\n * neither a MIME type nor a usable name is much more likely to be the document\n * it has always been sent as than an image, and this way a nameless document\n * keeps working instead of being newly broken to rescue a nameless image.\n *\n * PRECEDENCE, since two orderings are otherwise indistinguishable: the MIME\n * type classifies and the name is consulted only when there is none. A stated\n * type is a stated fact and a name is a convention, so a `.pdf` named\n * `image/png` goes to the image block. `request.test.ts` pins this with a case\n * where the two disagree, because a name-first `fileContent` passes every case\n * where they agree.\n */\nfunction fileContent(part: FilePart): Record<string, unknown> {\n const mimeType = part.mimeType?.trim().toLowerCase() ?? \"\";\n // `\"\"` rather than `undefined` is the shape to expect from a browser that\n // could not type the file: `useChat.uploadFile` writes `data.mimeType ??\n // file.type`, and `File.type` is the empty string, not absent. Both land on\n // the name.\n const isImage = mimeType\n ? mimeType.startsWith(\"image/\") && mimeType !== \"image/svg+xml\"\n : hasImageExtension(part.name);\n return { type: isImage ? \"input_image\" : \"input_file\", file_id: part.fileId };\n}\n\nfunction hasImageExtension(name: string | undefined): boolean {\n const match = /\\.([a-z0-9]+)$/.exec(name?.trim().toLowerCase() ?? \"\");\n return match?.[1] !== undefined && IMAGE_EXTENSIONS.has(match[1]);\n}\n\n/**\n * Which block takes what, and whether the wrong one merely reads badly.\n *\n * MEASURED, not read off the API reference. Three attachments were uploaded to\n * `https://api.openai.com/v1/files` with `purpose: \"user_data\"` — a 64x64 PNG\n * of four solid quadrants (red, green, blue, yellow), a one-page PDF whose only\n * word is BANANA, and a 64x64 SVG — and each was then sent to\n * `https://api.openai.com/v1/responses` in both content blocks:\n *\n * PNG as `{type:\"input_image\", file_id}` — 200. Asked which quadrant was\n * red, gpt-5.4 answered `\"top-left\"` and gpt-4o `\"The red quadrant is the\n * top-left.\"` Both correct. SO AN UPLOADED IMAGE IS REAL VISION INPUT, and\n * `file_id` is all `input_image` needs; sending `detail:\"auto\"` alongside\n * it changed nothing, so it is left off.\n * PNG as `{type:\"input_file\", file_id}` — 400, verbatim: `Invalid input:\n * Expected context stuffing file type to be a supported format: .art, .bat,\n * … .pdf, … .svg, … .yml but got .png.` (~90 extensions, elided.) This is\n * the finding that mattered: what gemi shipped was not \"an image the model\n * reads poorly\", it was a request that never ran.\n * PDF as `{type:\"input_file\", file_id}` — 200, `\"BANANA\"` on gpt-5.4 and\n * gpt-4o both.\n * PDF as `{type:\"input_image\", file_id}` — 400, verbatim: `Invalid input:\n * Expected image type to be a supported format: .jpeg, .jpg, .png, .gif,\n * .webp but got .pdf.` So the refusal is symmetric: neither block tolerates\n * the other's content.\n * SVG as `{type:\"input_image\", file_id}` — 400, `… but got .svg.`\n * SVG as `{type:\"input_file\", file_id}` — 400, but a different one:\n * `You uploaded an invalid file. Please try again with a different file`,\n * with no format list. .svg is on the document list and off the image one,\n * so routing accepted it and the pipeline behind it did not. An SVG fails\n * both ways today; it is sent as `input_file` because that is where the API\n * says it belongs, which is the only branch that can start working without\n * another change here.\n *\n * AND THE DISCRIMINATOR IS THE FILE NAME, NOT THE BYTES AND NOT THE UPLOAD'S\n * `Content-Type`. The same PNG bytes uploaded under the name `quadrants.txt`\n * with `type: \"text/plain\"` were refused as `input_image` with `… but got\n * .txt.`, and as `input_file` with `The file you uploaded is badly formatted or\n * corrupted. Please fix the file and try again.` (code `invalid_file`) — routed\n * by the extension, then failed on the bytes. That is why the name is what a\n * part with no `mimeType` falls back to: it is what the server will judge by.\n *\n * The extensions below are transcribed from the image rejection above.\n *\n * A closed list is right *here* and wrong one line up. This set is only\n * consulted when there is no MIME type to read, which is a guess either way, so\n * it guesses the conservative direction: a format the API adds later goes on\n * being sent as `input_file`, exactly as it is today. The MIME branch stays\n * open (`image/` prefix) for the opposite reason — a declared `image/avif`\n * belongs in the image block the moment the API takes one, and being wrong\n * there is a 400 that names the format, which is loud and fixes itself.\n */\nconst IMAGE_EXTENSIONS = new Set([\"jpeg\", \"jpg\", \"png\", \"gif\", \"webp\"]);\n\n/**\n * Reasoning goes back exactly as it came.\n *\n * Reshaping it — flattening the summary into text, renaming the item, dropping\n * the id — costs two things that are hard to see and expensive to have lost:\n * the prompt cache, which keys on the literal item, and on a reasoning model\n * the thread of the model's own argument across turns.\n *\n * An item with no id is dropped instead. The id is the API's handle on stored\n * reasoning, and an item without one is not a reasoning item the API can\n * resolve — sending a summary under a fabricated id would be worse than\n * sending nothing, because it would look like continuity that is not there.\n */\nfunction reasoningItem(part: { id?: string; text?: string }): ResponsesInputItem | null {\n if (!part.id) return null;\n const item: ResponsesInputItem = { type: \"reasoning\", id: part.id };\n item.summary = part.text ? [{ type: \"summary_text\", text: part.text }] : [];\n return item;\n}\n\n/**\n * Every tool call gets an output, including the ones that never ran.\n *\n * The API rejects a history containing a `function_call` with no matching\n * `function_call_output`, so a denial cannot be expressed by omission — and a\n * conversation where the user said no has to stay continuable, which is the\n * whole reason `denied` exists in the first place.\n *\n * What is sent is prose rather than a status enum, because the reader is a\n * language model: it has to be able to tell \"the user refused this\" from \"this\n * blew up\", and those two lead to genuinely different next moves — apologize\n * and ask, versus try another way.\n *\n * STILL A STRING, AFTER #490, AND THAT IS THE DESIGN. A tool that produces a\n * file the model has to look at does not put it here: the run appends an\n * input-role message carrying a `FilePart` after the call settles, which is the\n * `input_file` shape a user's own upload already takes and which the branch\n * above already builds every day.\n *\n * The alternative was a `function_call_output` whose `output` is a content\n * array with an image block in it, and it was not chosen because NOBODY HAS\n * MEASURED WHETHER THE API ACCEPTS ONE. No request was made with an image block\n * in a `function_call_output`, so whether it is taken, ignored, or a 400 is\n * unknown, and the one thing that is certain is that finding out costs a\n * conversation each time it is wrong — a rejected history is rejected on every\n * subsequent turn, not just the one that built it. Going through a route that\n * is exercised on every vision request costs nothing to be sure of. If someone\n * does measure it and it works, the tool result becomes the tidier home for a\n * file and this comment is the place to say so.\n */\nexport function toolResultOutput(part: ToolResultPart): string {\n if (part.status === \"ok\") {\n return typeof part.output === \"string\" ? part.output : JSON.stringify(part.output ?? null);\n }\n\n if (part.status === \"denied\") {\n const reason = part.reason ? ` Reason given: ${part.reason}` : \"\";\n if (part.cause === \"stopped\") {\n return `The run was stopped before this tool call could complete, so it did not run.${reason}`;\n }\n return `The user declined this tool call, so it did not run.${reason}`;\n }\n\n return `The tool call failed and produced no result. Error (${part.error?.code ?? \"unknown\"}): ${\n part.error?.message ?? \"no message\"\n }`;\n}\n\n// --- tools ---------------------------------------------------------------\n\nfunction isNamespace(\n entry: ProviderToolSpec | ProviderToolNamespace,\n): entry is ProviderToolNamespace {\n return Array.isArray((entry as ProviderToolNamespace).tools);\n}\n\n/**\n * Tools and namespaces onto the Responses `tools` array.\n *\n * Without tool search the grouping has nothing to do — a namespace exists to be\n * searched — so it is flattened away and every schema is sent inline with\n * `deferred` ignored. That is the promise `capabilities.toolSearch` makes:\n * deferral is a token optimization, and an optimization that changed which\n * tools the model can reach would not be one.\n */\nexport function toResponsesTools(\n tools: (ProviderToolSpec | ProviderToolNamespace)[] | undefined,\n capabilities: ProviderCapabilities,\n): ResponsesTool[] {\n if (!tools || tools.length === 0) return [];\n\n if (!capabilities.toolSearch) {\n const flat: ResponsesTool[] = [];\n for (const entry of tools) {\n if (isNamespace(entry)) {\n for (const tool of entry.tools) flat.push(functionTool(tool, false));\n } else {\n flat.push(functionTool(entry, false));\n }\n }\n return flat;\n }\n\n const out: ResponsesTool[] = [];\n let anyDeferred = false;\n\n for (const entry of tools) {\n if (isNamespace(entry)) {\n const inner = entry.tools.map((tool) => {\n if (tool.deferred) anyDeferred = true;\n return functionTool(tool, true);\n });\n out.push({\n type: \"namespace\",\n name: entry.name,\n description: entry.description,\n tools: inner,\n });\n } else {\n if (entry.deferred) anyDeferred = true;\n out.push(functionTool(entry, true));\n }\n }\n\n // Without this the deferred schemas are unreachable: the model is shown a\n // name and a description and given no way to ask for the rest, which is worse\n // than not deferring at all. Added only when something is actually deferred,\n // so an agent that defers nothing does not pay for a tool it cannot use.\n if (anyDeferred) out.push({ type: \"tool_search\" });\n\n return out;\n}\n\nfunction functionTool(tool: ProviderToolSpec, allowDeferred: boolean): ResponsesTool {\n const spec: ResponsesTool = {\n type: \"function\",\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters,\n strict: tool.strict,\n };\n if (allowDeferred && tool.deferred) spec.defer_loading = true;\n return spec;\n}\n",
16
- "import type { ReasoningEffort } from \"./Agent\";\nimport { streamResponses, uploadFile, type ResponsesEndpoint } from \"./providers/call\";\nimport { capabilitiesForModel } from \"./providers/capabilities\";\nimport { normalizeProviderError } from \"./providers/errors\";\nimport { buildResponsesRequest } from \"./providers/request\";\nimport type { JSONSchema } from \"./Schema\";\nimport type { AgentError, AgentMessage, FinishReason, Usage } from \"./types\";\n\n/**\n * A provider makes one model call. It does not run the tool loop.\n *\n * The split matters: approvals, `maxSteps`, deferred tools, skill loading and\n * persistence are all provider-independent, and putting them in the provider\n * would mean writing them again for the second provider. So the provider's\n * whole job is to translate gemi's messages into a request, and the response\n * stream back into `ProviderEvent`s. Everything above that lives in `Agent`.\n *\n * v1 targets OpenAI's Responses API — native reasoning items and strict\n * structured output without reassembling them by hand. The interface is kept\n * free of anything Responses-specific so a Chat Completions provider (for older\n * Azure deployments and OpenAI-compatible gateways) can be added later without\n * touching Agent, Controller or the client.\n */\n\n/** What a provider will actually honour, so `Agent` can drop the rest rather\n * than have a request rejected at runtime. */\nexport type ProviderCapabilities = {\n reasoning: boolean;\n structuredOutput: boolean;\n fileInput: boolean;\n parallelToolCalls: boolean;\n /**\n * Tool search, and with it deferred loading. Only recent models have it, so a\n * provider that answers `false` is sent every schema inline and the agent\n * runs identically — deferral is a token optimization, and an optimization\n * that changed behaviour when unavailable would not be one.\n */\n toolSearch: boolean;\n};\n\n/** A tool as the model is shown it: schema only, no implementation. */\nexport type ProviderToolSpec = {\n name: string;\n description: string;\n parameters: JSONSchema;\n strict: boolean;\n /** `defer_loading`: send the name and description, withhold the schema until\n * the model searches for it. Ignored when `capabilities.toolSearch` is\n * false. */\n deferred?: boolean;\n};\n\n/** Tools grouped for search. Flattened back to a list by a provider without\n * tool search, since the grouping exists to be searched. */\nexport type ProviderToolNamespace = {\n name: string;\n description: string;\n tools: ProviderToolSpec[];\n};\n\nexport interface ProviderStreamParams {\n messages: AgentMessage[];\n systemPrompt?: string;\n tools?: (ProviderToolSpec | ProviderToolNamespace)[];\n /** Set when the agent declares an `output` schema; the provider turns it into\n * whatever its own strict-JSON parameter is. `strict` comes from the schema —\n * false when it contains an `s.json()` node, which strict mode cannot\n * express — and is required rather than defaulted so that a new caller has to\n * answer it instead of inheriting a 400. */\n output?: { name: string; schema: JSONSchema; strict: boolean };\n /** Optional: silently dropped by a provider whose `capabilities.reasoning`\n * is false, since a model that cannot reason should not fail a request. */\n reasoning?: ReasoningEffort;\n temperature?: number;\n maxOutputTokens?: number;\n signal?: AbortSignal;\n}\n\n/**\n * The events of a single model call. Deliberately smaller than\n * `AgentStreamEvent`: no run, message, tool-result or approval events, because\n * a provider knows about none of those.\n */\nexport type ProviderEvent =\n | { type: \"text-delta\"; delta: string }\n | { type: \"reasoning-delta\"; delta: string; id?: string }\n /** Arguments arrive as JSON fragments; the provider passes them through and\n * `Agent` assembles and validates against the tool's schema. */\n | {\n type: \"tool-call-delta\";\n toolCallId: string;\n name: string;\n argsDelta: string;\n namespace?: string;\n }\n /**\n * `name` IS FLAT AND STAYS FLAT. This was an open question and the API has\n * answered it: a call to a function that lives inside a namespace comes back\n * as `{name: \"getOrder\", namespace: \"crm\"}`, not as `\"crm.getOrder\"` —\n * recorded in `providers/__fixtures__/openai-tool-search.sse` and pinned by\n * a test that reads that file. So `name` is already the key `Agent`'s tool\n * registry is built on, which is what makes tool names having to be globally\n * unique within an agent (see `ToolNamespace`) the right rule rather than an\n * inconvenience.\n *\n * `namespace` is carried beside it, absent for a tool that was listed bare.\n * It is provenance, not identity: it says which group the model chose to\n * look in, which is worth recording next to the call and is worthless for\n * finding the tool. Folding it into `name` would make a name that is\n * sometimes qualified and sometimes not, and nothing could match on that.\n */\n | { type: \"tool-call\"; toolCallId: string; name: string; args: string; namespace?: string }\n /**\n * The model went looking for a deferred tool and pulled its schema in. Worth\n * surfacing rather than swallowing: it is a step the user paid for, and the\n * pause before it is otherwise unexplained.\n *\n * TWO FIELDS, not one. `loaded` is the function names — `[\"listOrders\",\n * \"getOrder\"]` — and `namespaces` is the groups they came out of —\n * `[\"crm\"]`. Search results arrive as a tree of namespaces containing\n * functions, so a single flat list has to pick one level and throw the other\n * away, and both levels are worth saying: \"searched crm, loaded getOrder\"\n * reads better than either half, and the group is the thing the model\n * actually chose between.\n *\n * `namespaces` is required rather than optional because the parser always\n * knows the answer, and an optional field would let a future provider forget\n * to fill it in silently. Empty means the search returned bare functions.\n */\n | { type: \"tool-search\"; loaded: string[]; namespaces: string[] }\n | { type: \"output-delta\"; delta: string }\n | { type: \"finish\"; reason: FinishReason; usage: Usage }\n | { type: \"error\"; error: AgentError };\n\nexport type ProviderStream = AsyncIterable<ProviderEvent>;\n\nexport type ProviderConfig = {\n apiKey?: string;\n baseURL?: string;\n timeoutMs?: number;\n maxRetries?: number;\n headers?: Record<string, string>;\n};\n\n\n/** Long enough for a reasoning model to think before it says anything, short\n * enough that a hung connection is not mistaken for a slow one. Only covers\n * getting a response; the stream that follows has no deadline. */\nconst DEFAULT_TIMEOUT_MS = 120_000;\nconst DEFAULT_MAX_RETRIES = 2;\n\nfunction env(name: string): string | undefined {\n return typeof process === \"undefined\" ? undefined : process.env?.[name];\n}\n\nexport abstract class AgentProvider {\n abstract readonly model: string;\n abstract readonly capabilities: ProviderCapabilities;\n\n /** The model ids this provider knows about — for autocomplete only; any\n * string is still accepted, because a new model must not require a gemi\n * release to use. */\n static models(): readonly string[] {\n return [];\n }\n\n abstract stream(params: ProviderStreamParams): ProviderStream;\n\n /**\n * Uploads a file and returns the id a `FilePart` carries. Message history\n * therefore holds provider file ids, which is the trade for getting vision\n * and PDF input without gemi owning a storage story in v1.\n */\n abstract upload(file: File): Promise<string>;\n\n /**\n * Maps a provider's error body onto the normalized codes, so an app can\n * branch on `rate_limited` without knowing whose rate limit it was.\n *\n * Shared rather than abstract-in-practice: Azure answers the same error\n * envelope as OpenAI, and the one place it differs — the content filter's\n * code, buried in `innererror` — is handled by reading both.\n */\n normalizeError(error: unknown): AgentError {\n return normalizeProviderError(error);\n }\n}\n\n/**\n * Autocomplete, not a gate. Every id here was confirmed present in\n * `GET https://api.openai.com/v1/models`; any other string is still accepted,\n * because a model released next Tuesday must not need a gemi release to use —\n * see `capabilitiesForModel` for what an unrecognized id is assumed to do.\n *\n * Ordered newest first, and deliberately short. `/v1/models` answers with\n * ninety-odd chat ids once the dated snapshots and the `-codex`, `-pro`,\n * `-chat-latest`, `-search-api` and `-nano` variants are counted; a list that\n * tried to be complete would be stale within the month and would bury the\n * handful of names anyone actually types. Snapshot-pinned ids\n * (`gpt-5.4-2026-03-05`) are left out for the same reason and work identically.\n *\n * The Azure provider returns this same list, which is a small lie it has always\n * told: a resource serves the deployments someone created, not the catalogue.\n * `AzureConfig.deployment` is the escape hatch, and an unrecognized deployment\n * name lands on the same capable default as an unrecognized model.\n */\nconst OPENAI_MODELS = [\n \"gpt-5.5\",\n \"gpt-5.4\",\n \"gpt-5.4-mini\",\n \"gpt-5.4-nano\",\n \"gpt-5.2\",\n \"gpt-5.1\",\n \"gpt-5\",\n \"gpt-5-mini\",\n \"gpt-5-nano\",\n \"gpt-4.1\",\n \"gpt-4.1-mini\",\n \"gpt-4.1-nano\",\n \"gpt-4o\",\n \"gpt-4o-mini\",\n \"o4-mini\",\n \"o3\",\n \"o3-mini\",\n] as const;\n\nexport class OpenAIProvider extends AgentProvider {\n readonly model: string;\n readonly capabilities: ProviderCapabilities;\n protected readonly config: ProviderConfig;\n\n constructor(model: string, config: ProviderConfig = {}) {\n super();\n this.model = model;\n this.capabilities = capabilitiesForModel(model);\n this.config = config;\n }\n\n /** Config defaults come from gemi's config (`ai.openai`), so an app that has\n * set `OPENAI_API_KEY` writes only the model name. */\n static model(model: string, config?: ProviderConfig): OpenAIProvider {\n return new OpenAIProvider(model, config);\n }\n\n static models(): readonly string[] {\n return OPENAI_MODELS;\n }\n\n stream(params: ProviderStreamParams): ProviderStream {\n const body = buildResponsesRequest(params, {\n model: this.model,\n capabilities: this.capabilities,\n });\n return streamResponses(this.endpoint(), body, {\n signal: params.signal,\n // The request carries the schema whenever the agent declared one, so the\n // parser has to read the answer as one too — a model that ignored the\n // parameter answers 400, not prose.\n structuredOutput: Boolean(params.output),\n });\n }\n\n upload(file: File): Promise<string> {\n return uploadFile(this.endpoint(), file);\n }\n\n protected baseURL(): string {\n return (this.config.baseURL ?? env(\"OPENAI_BASE_URL\") ?? \"https://api.openai.com/v1\").replace(\n /\\/+$/,\n \"\",\n );\n }\n\n protected endpoint(): ResponsesEndpoint {\n const base = this.baseURL();\n const config = this.config;\n return {\n responsesUrl: `${base}/responses`,\n filesUrl: `${base}/files`,\n headers: async () => {\n const apiKey = config.apiKey ?? env(\"OPENAI_API_KEY\");\n return {\n ...(apiKey ? { authorization: `Bearer ${apiKey}` } : {}),\n ...config.headers,\n };\n },\n timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n };\n }\n}\n\nexport type AzureConfig = ProviderConfig & {\n /**\n * The resource host, with or without a trailing `/openai`. Both spellings\n * work — `https://<resource>.cognitiveservices.azure.com` and\n * `https://<resource>.openai.azure.com` — and neither is rewritten, because\n * only one of them exists for a resource that was not created as\n * kind=OpenAI. See `azureBase` for what is done to it.\n */\n endpoint?: string;\n /**\n * Just the resource name, when there is no endpoint to hand. `<name>` is\n * expanded to `https://<name>.cognitiveservices.azure.com/openai`.\n */\n resourceName?: string;\n apiVersion?: string;\n /**\n * The deployment to call, when it is not named after the model. Azure lets\n * whoever ran the template call it anything, and plenty of them are called\n * `prod` — this is the override the class comment promises.\n */\n deployment?: string;\n /**\n * For Entra ID instead of a key. A function, not a token, because these\n * expire mid-conversation.\n */\n getToken?: () => Promise<string>;\n};\n\n/**\n * `preview` selects Azure's `/openai/v1` surface, which is the OpenAI-shaped\n * one — same request body, same SSE frames, same `model` field naming the\n * deployment — and it is the only surface the Responses API has that gemi's\n * request builder can talk to unchanged. It takes no dated version: sending\n * `api-version=2025-04-01-preview` to `/openai/v1/responses` answers\n * `400 {\"code\":\"BadRequest\",\"message\":\"API version not supported\"}`.\n *\n * A dated version is still honoured, and routes to the older\n * `/openai/responses` path instead — see `azurePath`. So an app that pinned\n * one keeps working, which is the promise the old comment here made and could\n * not keep once the paths diverged.\n */\nconst AZURE_API_VERSION = \"preview\";\n\n/**\n * Where an Azure Responses call goes, worked out live rather than from docs.\n *\n * THE PROBLEM THIS SOLVES. `AZURE_OPENAI_ENDPOINT` is conventionally written\n * with `/openai` already on the end, and the old code appended `/openai` again\n * and then a deployment path, producing\n * `…/openai/openai/deployments/<dep>/responses` — a 404 on every request, for\n * every app that configured the provider the documented way. Two separate\n * mistakes were stacked there, and only measuring told them apart.\n *\n * WHAT WAS MEASURED, against a real resource, POSTing a Responses body:\n *\n * 404 {endpoint}/openai/deployments/gpt-5.4/responses?api-version=2025-04-01-preview\n * 404 {host}/openai/deployments/gpt-5.4/responses?api-version=2025-04-01-preview\n * 404 {host}/openai/deployments/gpt-5.4/responses?api-version=preview\n * 400 {host}/openai/v1/responses?api-version=2025-04-01-preview (\"API version not supported\")\n * 200 {host}/openai/v1/responses?api-version=preview\n * 200 {host}/openai/v1/responses (no api-version at all)\n * 200 {host}/openai/responses?api-version=2025-04-01-preview\n *\n * for {host} in BOTH `https://<resource>.cognitiveservices.azure.com` and\n * `https://<resource>.openai.azure.com` — both spellings answered identically,\n * so the host was never the variable. The deployment-in-the-URL path is the\n * Chat Completions shape and the Responses API does not serve it at all; the\n * deployment goes in the body's `model`, which is what `buildResponsesRequest`\n * already puts there.\n *\n * `/openai/v1/files?api-version=preview` and\n * `/openai/files?api-version=2025-04-01-preview` were both checked too (200,\n * empty list), so uploads follow the same fork.\n */\nfunction azureBase(raw: string): string {\n const trimmed = raw.trim().replace(/\\/+$/, \"\");\n if (!trimmed) return \"\";\n // A configured endpoint may or may not already carry `/openai`, and may even\n // carry `/openai/v1` if someone copied a full URL. Normalize down to the\n // resource base and put exactly one `/openai` back, rather than appending\n // blind — appending blind is the bug.\n const base = trimmed.replace(/\\/openai(?:\\/v1)?$/i, \"\");\n return `${base}/openai`;\n}\n\n/** Dated versions belong to the older path; `preview` (and anything that is not\n * a date) belongs to `/v1`. Both were verified above. */\nfunction azurePath(apiVersion: string): string {\n return /^\\d{4}-\\d{2}-\\d{2}/.test(apiVersion) ? \"\" : \"/v1\";\n}\n\n/**\n * Its own class rather than a flag on `OpenAIProvider`: Azure names the\n * deployment rather than the model, pins an api-version, authenticates with an\n * `api-key` header or an Entra token, and puts the resource in the host. One\n * class carrying both shapes means every field is conditionally meaningful.\n *\n * (It used to put the deployment in the URL as well. It does not: the Responses\n * API serves no such path — see `azureBase` for the measurements.)\n *\n * The API stays symmetrical — `.model()`, not `.deployment()`. Apps name a\n * model; mapping that onto a deployment is this class's problem, and an app\n * that named its deployment differently overrides it in config.\n */\nexport class AzureOpenAIProvider extends AgentProvider {\n readonly model: string;\n readonly capabilities: ProviderCapabilities;\n protected readonly config: AzureConfig;\n\n constructor(model: string, config: AzureConfig = {}) {\n super();\n this.model = model;\n // Read off the model, not the deployment: a deployment called `prod` says\n // nothing, and an unrecognized name lands on the all-capabilities default\n // anyway. See `capabilitiesForModel`.\n this.capabilities = capabilitiesForModel(model);\n this.config = config;\n }\n\n /** Defaults from gemi's config (`ai.azure`). */\n static model(model: string, config?: AzureConfig): AzureOpenAIProvider {\n return new AzureOpenAIProvider(model, config);\n }\n\n static models(): readonly string[] {\n return OPENAI_MODELS;\n }\n\n stream(params: ProviderStreamParams): ProviderStream {\n const body = buildResponsesRequest(params, {\n model: this.deployment(),\n capabilities: this.capabilities,\n });\n return streamResponses(this.endpoint(), body, {\n signal: params.signal,\n // The request carries the schema whenever the agent declared one, so the\n // parser has to read the answer as one too — a model that ignored the\n // parameter answers 400, not prose.\n structuredOutput: Boolean(params.output),\n });\n }\n\n upload(file: File): Promise<string> {\n return uploadFile(this.endpoint(), file);\n }\n\n protected deployment(): string {\n return this.config.deployment ?? this.model;\n }\n\n /**\n * The resource base, `<host>/openai`, from whichever of the three ways it was\n * configured. A bare resource name expands to the `cognitiveservices` host\n * rather than the `openai.azure.com` one: both answer for a resource created\n * as kind=OpenAI, only `cognitiveservices` answers for an AI Foundry or\n * multi-service resource, so it is the spelling that is right more often. An\n * app on the other one sets `endpoint` and nothing rewrites it.\n */\n protected base(): string {\n const config = this.config;\n const configured = config.baseURL ?? config.endpoint ?? env(\"AZURE_OPENAI_ENDPOINT\");\n if (configured) return azureBase(configured);\n const resource =\n config.resourceName ?? env(\"AZURE_OPENAI_RESOURCE_NAME\") ?? env(\"AZURE_RESOURCE_NAME\");\n return resource ? azureBase(`https://${resource.trim()}.cognitiveservices.azure.com`) : \"\";\n }\n\n protected endpoint(): ResponsesEndpoint {\n const config = this.config;\n const base = this.base();\n const apiVersion = config.apiVersion ?? env(\"AZURE_OPENAI_API_VERSION\") ?? AZURE_API_VERSION;\n const path = azurePath(apiVersion);\n const query = `?api-version=${encodeURIComponent(apiVersion)}`;\n return {\n // No deployment in the URL: the Responses API does not serve that path,\n // and `buildResponsesRequest` already sends the deployment as `model`.\n responsesUrl: `${base}${path}/responses${query}`,\n // Files are resource-scoped, not deployment-scoped: an upload is not\n // addressed to a model.\n filesUrl: `${base}${path}/files${query}`,\n headers: async () => {\n // Called per request, not per provider: an Entra token minted when the\n // app booted is expired by the time a long conversation reaches step\n // nine, and that failure looks like a random 401 in the middle of a\n // working feature.\n if (config.getToken) {\n return { authorization: `Bearer ${await config.getToken()}`, ...config.headers };\n }\n const apiKey = config.apiKey ?? env(\"AZURE_OPENAI_API_KEY\");\n return { ...(apiKey ? { \"api-key\": apiKey } : {}), ...config.headers };\n },\n timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n };\n }\n}\n",
20
+ "import type { ReasoningEffort } from \"./Agent\";\nimport {\n responsesEndpoint,\n streamResponses,\n uploadFile,\n type ResponsesEndpoint,\n} from \"./providers/call\";\nimport { capabilitiesForModel } from \"./providers/capabilities\";\nimport {\n azureTarget,\n openAITarget,\n type AzureConfig,\n type ProviderConfig,\n} from \"./providers/endpoints\";\nimport { normalizeProviderError } from \"./providers/errors\";\nimport { buildResponsesRequest } from \"./providers/request\";\nimport type { JSONSchema } from \"./Schema\";\nimport type { AgentError, AgentMessage, FinishReason, Usage } from \"./types\";\n\n/**\n * A provider makes one model call. It does not run the tool loop.\n *\n * The split matters: approvals, `maxSteps`, deferred tools, skill loading and\n * persistence are all provider-independent, and putting them in the provider\n * would mean writing them again for the second provider. So the provider's\n * whole job is to translate gemi's messages into a request, and the response\n * stream back into `ProviderEvent`s. Everything above that lives in `Agent`.\n *\n * v1 targets OpenAI's Responses API — native reasoning items and strict\n * structured output without reassembling them by hand. The interface is kept\n * free of anything Responses-specific so a Chat Completions provider (for older\n * Azure deployments and OpenAI-compatible gateways) can be added later without\n * touching Agent, Controller or the client.\n */\n\n/** What a provider will actually honour, so `Agent` can drop the rest rather\n * than have a request rejected at runtime. */\nexport type ProviderCapabilities = {\n reasoning: boolean;\n structuredOutput: boolean;\n fileInput: boolean;\n parallelToolCalls: boolean;\n /**\n * Tool search, and with it deferred loading. Only recent models have it, so a\n * provider that answers `false` is sent every schema inline and the agent\n * runs identically — deferral is a token optimization, and an optimization\n * that changed behaviour when unavailable would not be one.\n */\n toolSearch: boolean;\n};\n\n/** A tool as the model is shown it: schema only, no implementation. */\nexport type ProviderToolSpec = {\n name: string;\n description: string;\n parameters: JSONSchema;\n strict: boolean;\n /** `defer_loading`: send the name and description, withhold the schema until\n * the model searches for it. Ignored when `capabilities.toolSearch` is\n * false. */\n deferred?: boolean;\n};\n\n/** Tools grouped for search. Flattened back to a list by a provider without\n * tool search, since the grouping exists to be searched. */\nexport type ProviderToolNamespace = {\n name: string;\n description: string;\n tools: ProviderToolSpec[];\n};\n\nexport interface ProviderStreamParams {\n messages: AgentMessage[];\n systemPrompt?: string;\n tools?: (ProviderToolSpec | ProviderToolNamespace)[];\n /** Set when the agent declares an `output` schema; the provider turns it into\n * whatever its own strict-JSON parameter is. `strict` comes from the schema —\n * false when it contains an `s.json()` node, which strict mode cannot\n * express — and is required rather than defaulted so that a new caller has to\n * answer it instead of inheriting a 400. */\n output?: { name: string; schema: JSONSchema; strict: boolean };\n /** Optional: silently dropped by a provider whose `capabilities.reasoning`\n * is false, since a model that cannot reason should not fail a request. */\n reasoning?: ReasoningEffort;\n temperature?: number;\n maxOutputTokens?: number;\n signal?: AbortSignal;\n}\n\n/**\n * The events of a single model call. Deliberately smaller than\n * `AgentStreamEvent`: no run, message, tool-result or approval events, because\n * a provider knows about none of those.\n */\nexport type ProviderEvent =\n | { type: \"text-delta\"; delta: string }\n | { type: \"reasoning-delta\"; delta: string; id?: string }\n /** Arguments arrive as JSON fragments; the provider passes them through and\n * `Agent` assembles and validates against the tool's schema. */\n | {\n type: \"tool-call-delta\";\n toolCallId: string;\n name: string;\n argsDelta: string;\n namespace?: string;\n }\n /**\n * `name` IS FLAT AND STAYS FLAT. This was an open question and the API has\n * answered it: a call to a function that lives inside a namespace comes back\n * as `{name: \"getOrder\", namespace: \"crm\"}`, not as `\"crm.getOrder\"` —\n * recorded in `providers/__fixtures__/openai-tool-search.sse` and pinned by\n * a test that reads that file. So `name` is already the key `Agent`'s tool\n * registry is built on, which is what makes tool names having to be globally\n * unique within an agent (see `ToolNamespace`) the right rule rather than an\n * inconvenience.\n *\n * `namespace` is carried beside it, absent for a tool that was listed bare.\n * It is provenance, not identity: it says which group the model chose to\n * look in, which is worth recording next to the call and is worthless for\n * finding the tool. Folding it into `name` would make a name that is\n * sometimes qualified and sometimes not, and nothing could match on that.\n */\n | { type: \"tool-call\"; toolCallId: string; name: string; args: string; namespace?: string }\n /**\n * The model went looking for a deferred tool and pulled its schema in. Worth\n * surfacing rather than swallowing: it is a step the user paid for, and the\n * pause before it is otherwise unexplained.\n *\n * TWO FIELDS, not one. `loaded` is the function names — `[\"listOrders\",\n * \"getOrder\"]` — and `namespaces` is the groups they came out of —\n * `[\"crm\"]`. Search results arrive as a tree of namespaces containing\n * functions, so a single flat list has to pick one level and throw the other\n * away, and both levels are worth saying: \"searched crm, loaded getOrder\"\n * reads better than either half, and the group is the thing the model\n * actually chose between.\n *\n * `namespaces` is required rather than optional because the parser always\n * knows the answer, and an optional field would let a future provider forget\n * to fill it in silently. Empty means the search returned bare functions.\n */\n | { type: \"tool-search\"; loaded: string[]; namespaces: string[] }\n | { type: \"output-delta\"; delta: string }\n | { type: \"finish\"; reason: FinishReason; usage: Usage }\n | { type: \"error\"; error: AgentError };\n\nexport type ProviderStream = AsyncIterable<ProviderEvent>;\n\n/**\n * Re-exported rather than declared here. What these configure is the *vendor*,\n * not the Responses API, and the images path resolves the same host, credential\n * and api-version from the same fields — see `providers/endpoints.ts` for why\n * that resolution lives in one place and what happens when it does not.\n */\nexport type { AzureConfig, ProviderConfig } from \"./providers/endpoints\";\n\nexport abstract class AgentProvider {\n abstract readonly model: string;\n abstract readonly capabilities: ProviderCapabilities;\n\n /** The model ids this provider knows about — for autocomplete only; any\n * string is still accepted, because a new model must not require a gemi\n * release to use. */\n static models(): readonly string[] {\n return [];\n }\n\n abstract stream(params: ProviderStreamParams): ProviderStream;\n\n /**\n * Uploads a file and returns the id a `FilePart` carries. Message history\n * therefore holds provider file ids, which is the trade for getting vision\n * and PDF input without gemi owning a storage story in v1.\n */\n abstract upload(file: File): Promise<string>;\n\n /**\n * Maps a provider's error body onto the normalized codes, so an app can\n * branch on `rate_limited` without knowing whose rate limit it was.\n *\n * Shared rather than abstract-in-practice: Azure answers the same error\n * envelope as OpenAI, and the one place it differs — the content filter's\n * code, buried in `innererror` — is handled by reading both.\n */\n normalizeError(error: unknown): AgentError {\n return normalizeProviderError(error);\n }\n}\n\n/**\n * Autocomplete, not a gate. Every id here was confirmed present in\n * `GET https://api.openai.com/v1/models`; any other string is still accepted,\n * because a model released next Tuesday must not need a gemi release to use —\n * see `capabilitiesForModel` for what an unrecognized id is assumed to do.\n *\n * Ordered newest first, and deliberately short. `/v1/models` answers with\n * ninety-odd chat ids once the dated snapshots and the `-codex`, `-pro`,\n * `-chat-latest`, `-search-api` and `-nano` variants are counted; a list that\n * tried to be complete would be stale within the month and would bury the\n * handful of names anyone actually types. Snapshot-pinned ids\n * (`gpt-5.4-2026-03-05`) are left out for the same reason and work identically.\n *\n * The Azure provider returns this same list, which is a small lie it has always\n * told: a resource serves the deployments someone created, not the catalogue.\n * `AzureConfig.deployment` is the escape hatch, and an unrecognized deployment\n * name lands on the same capable default as an unrecognized model.\n */\nconst OPENAI_MODELS = [\n \"gpt-5.5\",\n \"gpt-5.4\",\n \"gpt-5.4-mini\",\n \"gpt-5.4-nano\",\n \"gpt-5.2\",\n \"gpt-5.1\",\n \"gpt-5\",\n \"gpt-5-mini\",\n \"gpt-5-nano\",\n \"gpt-4.1\",\n \"gpt-4.1-mini\",\n \"gpt-4.1-nano\",\n \"gpt-4o\",\n \"gpt-4o-mini\",\n \"o4-mini\",\n \"o3\",\n \"o3-mini\",\n] as const;\n\nexport class OpenAIProvider extends AgentProvider {\n readonly model: string;\n readonly capabilities: ProviderCapabilities;\n protected readonly config: ProviderConfig;\n\n constructor(model: string, config: ProviderConfig = {}) {\n super();\n this.model = model;\n this.capabilities = capabilitiesForModel(model);\n this.config = config;\n }\n\n /** Config defaults come from gemi's config (`ai.openai`), so an app that has\n * set `OPENAI_API_KEY` writes only the model name. */\n static model(model: string, config?: ProviderConfig): OpenAIProvider {\n return new OpenAIProvider(model, config);\n }\n\n static models(): readonly string[] {\n return OPENAI_MODELS;\n }\n\n stream(params: ProviderStreamParams): ProviderStream {\n const body = buildResponsesRequest(params, {\n model: this.model,\n capabilities: this.capabilities,\n });\n return streamResponses(this.endpoint(), body, {\n signal: params.signal,\n // The request carries the schema whenever the agent declared one, so the\n // parser has to read the answer as one too — a model that ignored the\n // parameter answers 400, not prose.\n structuredOutput: Boolean(params.output),\n });\n }\n\n upload(file: File): Promise<string> {\n return uploadFile(this.endpoint(), file);\n }\n\n protected endpoint(): ResponsesEndpoint {\n return responsesEndpoint(openAITarget(this.config));\n }\n}\n\n/**\n * Its own class rather than a flag on `OpenAIProvider`: Azure names the\n * deployment rather than the model, pins an api-version, authenticates with an\n * `api-key` header or an Entra token, and puts the resource in the host. One\n * class carrying both shapes means every field is conditionally meaningful.\n *\n * (It used to put the deployment in the URL as well. It does not: the Responses\n * API serves no such path — see `azureBase` for the measurements.)\n *\n * The API stays symmetrical — `.model()`, not `.deployment()`. Apps name a\n * model; mapping that onto a deployment is this class's problem, and an app\n * that named its deployment differently overrides it in config.\n */\nexport class AzureOpenAIProvider extends AgentProvider {\n readonly model: string;\n readonly capabilities: ProviderCapabilities;\n protected readonly config: AzureConfig;\n\n constructor(model: string, config: AzureConfig = {}) {\n super();\n this.model = model;\n // Read off the model, not the deployment: a deployment called `prod` says\n // nothing, and an unrecognized name lands on the all-capabilities default\n // anyway. See `capabilitiesForModel`.\n this.capabilities = capabilitiesForModel(model);\n this.config = config;\n }\n\n /** Defaults from gemi's config (`ai.azure`). */\n static model(model: string, config?: AzureConfig): AzureOpenAIProvider {\n return new AzureOpenAIProvider(model, config);\n }\n\n static models(): readonly string[] {\n return OPENAI_MODELS;\n }\n\n stream(params: ProviderStreamParams): ProviderStream {\n const body = buildResponsesRequest(params, {\n model: this.deployment(),\n capabilities: this.capabilities,\n });\n return streamResponses(this.endpoint(), body, {\n signal: params.signal,\n // The request carries the schema whenever the agent declared one, so the\n // parser has to read the answer as one too — a model that ignored the\n // parameter answers 400, not prose.\n structuredOutput: Boolean(params.output),\n });\n }\n\n upload(file: File): Promise<string> {\n return uploadFile(this.endpoint(), file);\n }\n\n protected deployment(): string {\n return this.config.deployment ?? this.model;\n }\n\n protected endpoint(): ResponsesEndpoint {\n return responsesEndpoint(azureTarget(this.config));\n }\n}\n",
17
21
  "import type { AgentRun } from \"../Agent\";\nimport type { LiveRuns } from \"../AgentController\";\nimport type { AgentStreamEvent, AgentStreamFrame } from \"../types\";\n\n/** Long enough that a refresh, a tab restore or a flaky mobile connection still\n * lands on the tail; short enough that a finished run is not resident for the\n * rest of the day. */\nconst DEFAULT_TTL_MS = 60 * 1000;\n\n/**\n * How many frames are kept per run.\n *\n * The window has to be bounded or it is a memory leak with a long fuse: a\n * server that never restarts holding every token of every conversation it ever\n * streamed.\n *\n * It was 512 on the reasoning that a reattaching client only needs seconds of\n * catch-up. That is true of the *reattaching* client and false of the one that\n * never left: a frame is roughly a token, so 512 is a four-hundred-word answer,\n * and a reader that falls behind a longer one by more than that is cut off with\n * a 410 mid-stream. On a slow connection — the case reattachment exists for —\n * that is not exotic.\n *\n * 4096 covers essentially any single answer. It is affordable because of two\n * measured facts, and it would not have been before either:\n *\n * - `drain` no longer copies the window per wake, so a reader costs the same\n * whatever the window's size. Measured at 64 readers on a 2000-frame run:\n * 1106 ms of CPU at 4096 before, 425 ms after, and 468 ms at 512 — i.e. the\n * size stopped being a term in the cost.\n * - the entries are pointers to frames the run is holding anyway, so the\n * window costs 8 bytes each, not the frame. Eight times more of them is\n * ~28 KB per run, and a thousand concurrent runs is tens of megabytes.\n *\n * Both would change if the run's own buffer ever became bounded — then this\n * window owns the frames, and its size is their size.\n */\nconst DEFAULT_MAX_FRAMES = 4096;\n\n/**\n * A cursor older than anything still buffered.\n *\n * Deliberately an error rather than \"here is the tail I still have\". A client\n * that asked for frame 12 and silently got frame 300 onwards has a transcript\n * with a hole in it and no way to know — it will render a half-message, or an\n * `awaiting-input` for a tool call it never saw. Saying so lets the client do\n * the only correct thing, which is to reload the thread from the store.\n */\nexport class FrameCursorEvictedError extends Error {\n readonly code = \"frame_cursor_evicted\";\n\n constructor(\n readonly runId: string,\n readonly requested: number,\n readonly oldest: number,\n ) {\n super(\n `Frame ${requested} of run ${runId} has been evicted; the oldest frame ` +\n `still buffered is ${oldest}. Reload the thread instead of resuming.`,\n );\n }\n}\n\n/**\n * No run under that id in *this* process.\n *\n * Which is the honest answer, and the one worth being loud about: the run may\n * well be alive on the box next door. See the note on `MemoryLiveRuns` — behind\n * a round-robin load balancer this is what a refresh hits roughly (n-1)/n of\n * the time, and an explicit miss is the difference between a bug someone finds\n * in an hour and one that presents as \"reattach sometimes does nothing\".\n */\nexport class LiveRunNotFoundError extends Error {\n readonly code = \"live_run_not_found\";\n\n constructor(readonly runId: string) {\n super(`No live run ${runId} in this process.`);\n }\n}\n\nexport type RegisterParams = {\n threadId?: string;\n /**\n * The client's own name for this run, minted before the run had one.\n *\n * `runId` does not reach the client until `run-start`, and a stateless first\n * turn has no `threadId` either, so for the length of a network round trip\n * plus the provider's time to first token there is nothing for `/stop` to\n * name — which is exactly the window a user cancels in. `useChat` sends a\n * `clientRunId` with every turn it starts; recording it here is what makes\n * that window stoppable.\n */\n clientRunId?: string;\n /**\n * Called once per frame, in order, off the buffering path.\n *\n * The controller's `on*` hooks hang off this. It is a callback rather than a\n * second `run.frames()` subscription because every extra subscriber is\n * another consumer of a generator whose multi-subscriber behaviour we do not\n * own, and one pump is one thing to reason about.\n */\n onEvent?: (event: AgentStreamEvent) => void | Promise<void>;\n /** Reported failures: a hook that threw, or a run whose frame iterator did.\n * Injectable so tests can assert on it instead of reading stderr. */\n onInternalError?: (error: unknown) => void;\n};\n\ntype Entry = {\n run: AgentRun;\n threadId?: string;\n clientRunId?: string;\n /** A contiguous window of the run's frames, oldest first. */\n frames: AgentStreamFrame[];\n lastSeq: number;\n /**\n * The lowest `seq` still obtainable, or 0 while nothing has been dropped.\n *\n * Not derivable from `frames[0].seq`, which is what this used to compare\n * against. `seq` starts at 1, so a run that has evicted nothing still has an\n * oldest frame of 1, and a cursor of 0 — the \"I have no transcript yet\"\n * cursor that `replay` itself picks for a run registered but not yet pumped —\n * read as older than the buffer. A ten-frame run in a five-hundred-frame\n * window answered a refresh with 410, which is both false and unactionable:\n * refreshing right after sending is the case `/attach` exists to serve.\n */\n lostBefore: number;\n ended: boolean;\n evictAt: ReturnType<typeof setTimeout> | null;\n wake: Set<() => void>;\n /**\n * Bumped on every push and on the end.\n *\n * A reader cannot just park on \"wake me when something happens\": it suspends\n * at every `yield` while its consumer reads, and anything that arrives during\n * that suspension notifies an empty waiter set — so the reader parks *after*\n * the event it was waiting for and never hears another. The version it read\n * before scanning is what closes that window: if it moved, there is more to\n * scan and the wait is skipped.\n */\n version: number;\n};\n\n/**\n * The runs currently in flight in this process, and their recent frames.\n *\n * PER-PROCESS IS NOT AN IMPLEMENTATION SHORTCUT THAT A BETTER STORE FIXES. A\n * running generator lives in one process, and a second server cannot attach to\n * it — no amount of Redis moves an in-flight async iterator across a socket.\n * Reattachment therefore needs the request to land where the run is: one\n * server, sticky routing, or a proxy that forwards by `runId`. Worth saying out\n * loud, because the failure mode behind a round-robin load balancer is a\n * refresh that usually works.\n *\n * `find` and `replay` are built so that failure is an explicit miss — a 404\n * naming the run, a 410 naming the cursor — and never an SSE stream that opens,\n * says nothing and closes. An empty stream is indistinguishable from a run that\n * finished quietly, which is exactly the confusion this is supposed to avoid.\n */\nexport class MemoryLiveRuns implements LiveRuns {\n ttlMs: number;\n readonly maxFrames: number;\n\n private runs = new Map<string, Entry>();\n /** Thread to the most recently registered run for it. */\n private byThread = new Map<string, string>();\n /** The client's pre-`run-start` name for a run, to the run. */\n private byClientRun = new Map<string, string>();\n\n constructor(params: { ttlMs?: number; maxFrames?: number } = {}) {\n this.ttlMs = params.ttlMs ?? DEFAULT_TTL_MS;\n this.maxFrames = params.maxFrames ?? DEFAULT_MAX_FRAMES;\n }\n\n /**\n * Takes ownership of a run: starts buffering its frames and holds it until\n * `ttlMs` past the end.\n *\n * Settles once the run's last frame has been buffered and every `onEvent` it\n * led to has settled, and never rejects. The hooks run on a chain of their\n * own (see `pump`), so a frame's hook can be called well after the frame\n * arrived — after the run, too — and whoever needs to wait for the app's\n * code has only this to wait on: by the time a hook is called it is too late\n * to start waiting for it.\n */\n register(run: AgentRun, params: RegisterParams = {}): Promise<void> {\n const entry: Entry = {\n run: run as AgentRun,\n threadId: params.threadId,\n clientRunId: params.clientRunId,\n frames: [],\n lastSeq: -1,\n lostBefore: 0,\n ended: false,\n evictAt: null,\n wake: new Set(),\n version: 0,\n };\n this.runs.set(run.runId, entry);\n if (params.threadId) {\n this.byThread.set(params.threadId, run.runId);\n }\n if (params.clientRunId) {\n this.byClientRun.set(params.clientRunId, run.runId);\n }\n return this.pump(entry, params);\n }\n\n /**\n * The run a client named before the server had named it.\n *\n * Deliberately not folded into `find`, whose parameter is part of the\n * read-side `LiveRuns` interface an app may already implement — widening that\n * parameter would break every such implementation, and this lookup is only\n * ever asked by `/stop`.\n */\n findByClientRunId(clientRunId: string): string | null {\n const runId = this.byClientRun.get(clientRunId);\n return runId && this.runs.has(runId) ? runId : null;\n }\n\n /** What the client asks after a refresh: is anything still going here? */\n async find(params: { threadId: string }): Promise<{ runId: string; seq: number } | null> {\n const runId = this.byThread.get(params.threadId);\n if (!runId) {\n return null;\n }\n const entry = this.runs.get(runId);\n if (!entry) {\n return null;\n }\n // `seq` is the last frame emitted, not the next one: it is the same number\n // the transport puts in `Last-Event-ID`, so a client can compare the two\n // without knowing which end of the range each one means.\n return { runId, seq: entry.lastSeq };\n }\n\n get(runId: string): AgentRun | null {\n return this.runs.get(runId)?.run ?? null;\n }\n\n /**\n * The buffered frames from `from` onwards, followed by live ones until the\n * run ends.\n *\n * Throws before returning anything, so an evicted cursor and an unknown run\n * are still HTTP statuses rather than events on a stream that already\n * committed to a 200.\n *\n * `from` omitted means \"start wherever you still can\", not \"start at 0\".\n * These are genuinely different requests: a client that names a cursor is\n * telling us where its transcript ends, and handing it a later frame leaves\n * an invisible hole — that is the 410. A client with no cursor at all — the\n * browser reattaching on mount, which is the case `/attach` exists for — has\n * no transcript to put a hole in, and refusing it the tail because the run is\n * older than the buffer would 410 every run past `maxFrames`, i.e. every run\n * long enough to be worth reattaching to.\n */\n replay(runId: string, from?: number): AsyncIterable<AgentStreamFrame> {\n const entry = this.runs.get(runId);\n if (!entry) {\n throw new LiveRunNotFoundError(runId);\n }\n if (from !== undefined && from < entry.lostBefore) {\n throw new FrameCursorEvictedError(runId, from, entry.lostBefore);\n }\n const oldest = entry.frames[0]?.seq;\n // With nothing buffered — a run registered but not yet pumped — `lastSeq`\n // is -1 and this is 0, which is the same answer by another route.\n return this.drain(entry, runId, from ?? oldest ?? entry.lastSeq + 1);\n }\n\n /** Test seam: drops everything and cancels the pending eviction timers. */\n clear(): void {\n for (const entry of this.runs.values()) {\n if (entry.evictAt) {\n clearTimeout(entry.evictAt);\n }\n // Marked ended before waking: a reader parked on `wait` would otherwise\n // come back, find the entry unfinished, and park again forever.\n entry.ended = true;\n this.notify(entry);\n }\n this.runs.clear();\n this.byThread.clear();\n this.byClientRun.clear();\n }\n\n get size(): number {\n return this.runs.size;\n }\n\n private async pump(entry: Entry, params: RegisterParams): Promise<void> {\n // Hooks run on their own chain: they stay in order relative to each other,\n // and a slow one never stalls the buffer a reattaching client reads from.\n let hooks = Promise.resolve();\n try {\n for await (const frame of entry.run.frames()) {\n entry.frames.push(frame);\n entry.lastSeq = frame.seq;\n if (entry.frames.length > this.maxFrames) {\n const dropped = entry.frames.shift();\n if (dropped) entry.lostBefore = dropped.seq + 1;\n }\n this.notify(entry);\n const onEvent = params.onEvent;\n if (onEvent) {\n hooks = hooks.then(() => onEvent(frame.event)).catch((err) => report(params, err));\n }\n }\n } catch (err) {\n // The run's own iterator failed. There is nothing left to replay, so the\n // entry ends here; whoever is attached sees the stream close.\n report(params, err);\n } finally {\n entry.ended = true;\n this.notify(entry);\n // Eviction is scheduled off the ttl clock, BEFORE the hook chain is\n // awaited. `hooks` is app code — `onAwaitingInput` is documented as the\n // place to notify an approver, i.e. network I/O — and a `fetch` with no\n // timeout never rejects, it just never settles. Awaiting it first made\n // retention conditional on app code: one hanging hook pinned its entry,\n // its 512 frames, the run and everything the run closes over in the map\n // for the life of the process, and `find` kept answering with a run that\n // ended hours ago. Retention is this class's job and belongs on its own\n // clock. A hook still pending when the timer fires simply outlives the\n // entry, which is fine — it holds no reference the map needed.\n this.scheduleEviction(entry);\n await hooks;\n }\n }\n\n private async *drain(\n entry: Entry,\n runId: string,\n from: number,\n ): AsyncGenerator<AgentStreamFrame, void, void> {\n let cursor = from;\n while (true) {\n const seen = entry.version;\n // The window is contiguous and ordered, so a reader's position in it is\n // arithmetic, not a search. This used to copy the whole window on every\n // wake and scan it for frames past the cursor, which is O(window) per\n // frame per reader — invisible at a 512-frame cap and the reason the cap\n // could not be raised. Indexing makes the window's size stop mattering.\n for (;;) {\n if (cursor < entry.lostBefore) {\n // The window rolled past this reader mid-stream. Same reasoning as\n // the pre-flight check: a gap the client cannot see is worse than a\n // stream that stops and says why. Re-checked inside the loop rather\n // than once per wake, because a yield suspends this reader for as\n // long as its consumer takes and the pump keeps running.\n throw new FrameCursorEvictedError(runId, cursor, entry.lostBefore);\n }\n const frames = entry.frames;\n const oldest = frames[0]?.seq;\n if (oldest === undefined) {\n break;\n }\n // Clamped rather than treated as a gap: a cursor below the first seq\n // that ever existed is \"from the beginning\", not a lost position.\n const index = Math.max(0, cursor - oldest);\n if (index >= frames.length) {\n break;\n }\n const frame = frames[index]!;\n yield frame;\n cursor = frame.seq + 1;\n }\n if (entry.ended && cursor > entry.lastSeq) {\n return;\n }\n await this.wait(entry, seen);\n }\n }\n\n private wait(entry: Entry, seen: number): Promise<void> {\n if (entry.version !== seen) {\n return Promise.resolve();\n }\n return new Promise<void>((resolve) => entry.wake.add(resolve));\n }\n\n private notify(entry: Entry): void {\n entry.version++;\n const waiters = Array.from(entry.wake);\n entry.wake.clear();\n for (const resolve of waiters) {\n resolve();\n }\n }\n\n private scheduleEviction(entry: Entry): void {\n if (entry.evictAt) {\n return;\n }\n const timer = setTimeout(() => {\n const runId = entry.run.runId;\n this.runs.delete(runId);\n if (entry.threadId && this.byThread.get(entry.threadId) === runId) {\n this.byThread.delete(entry.threadId);\n }\n if (entry.clientRunId && this.byClientRun.get(entry.clientRunId) === runId) {\n this.byClientRun.delete(entry.clientRunId);\n }\n // Anyone still draining is holding an ended entry; wake them so they see\n // `ended` and finish rather than hanging on a promise nothing resolves.\n this.notify(entry);\n }, this.ttlMs);\n // A finished run must not be the reason a process stays up.\n (timer as { unref?: () => void }).unref?.();\n entry.evictAt = timer;\n }\n}\n\n/**\n * The process-wide default, shared by every `AgentController` that does not\n * bring its own. One map per process is the whole point — see the class note.\n */\nexport const liveRuns = new MemoryLiveRuns();\n\n/**\n * Hands `err` to `onInternalError`, which is app code too: one that throws is\n * dropped rather than rejecting the hook chain, and with it `register`'s\n * promise, which says it never rejects.\n */\nfunction report(params: RegisterParams, err: unknown): void {\n try {\n params.onInternalError?.(err);\n } catch {\n // Nothing is left to tell.\n }\n}\n",
18
22
  "import type { AgentStore } from \"../AgentController\";\nimport type { AgentMessage } from \"../types\";\n\n/** A day. Long enough that a conversation survives a lunch break, short enough\n * that a chat nobody came back to is not still resident a week later. */\nconst DEFAULT_TTL_MS = 24 * 60 * 60 * 1000;\n\n/** How often the map is walked looking for expired threads. */\nconst SWEEP_INTERVAL_MS = 60 * 1000;\n\ntype Thread = {\n messages: AgentMessage[];\n /** Bumped by every read and every write: a conversation someone is still\n * having must not expire out from under them mid-turn. */\n touchedAt: number;\n};\n\n/**\n * The default store: conversations last as long as the process.\n *\n * It exists so that `threadId` works out of the box, not so that anything is\n * durable — a restart loses every thread, and a second server never had them.\n * That is the honest default for a framework store, and it is why stateless is\n * still the mode an app gets without asking: the client carrying its own\n * history survives a deploy, and this does not.\n *\n * Expiry is swept lazily rather than on a timer. A `setTimeout` per thread is a\n * timer per conversation and a reference the GC cannot collect, and an interval\n * running forever keeps a process alive that has nothing else to do — so the\n * sweep happens on access, at most once a minute, and an untouched process\n * simply stops sweeping.\n *\n * A thread the store does not have is `null` from `loadThread` and an error\n * from `appendMessages`, never an empty conversation. It used to be the other\n * way, and the three ways a thread goes missing — it expired, the id was\n * mistyped, it lived on an instance that was scaled in — all read as a fresh\n * chat: the history was gone with no signal, and the next turn was persisted\n * under the dead id as though it were the first. `clientOwnedIds` is the one\n * setup where an unknown id is not a lost thread, because the client minted it.\n */\nexport class MemoryAgentStore implements AgentStore {\n readonly ttlMs: number;\n /** The ids come from the client, not from `createThread`, so an id this\n * store has never seen is a conversation starting rather than one lost. */\n readonly clientOwnedIds: boolean;\n\n private threads = new Map<string, Thread>();\n private lastSweep = 0;\n\n constructor(params: { ttlMs?: number; clientOwnedIds?: boolean } = {}) {\n this.ttlMs = params.ttlMs ?? DEFAULT_TTL_MS;\n this.clientOwnedIds = params.clientOwnedIds ?? false;\n }\n\n async createThread(params: { userId?: string | number }): Promise<{ threadId: string }> {\n // The user id is not part of the id and not stored: this store cannot\n // authorize anything, and an id that looked like a key would invite an app\n // to treat it as one. Ownership belongs to the app's own table.\n void params;\n const threadId = crypto.randomUUID();\n this.threads.set(threadId, { messages: [], touchedAt: Date.now() });\n this.sweep();\n return { threadId };\n }\n\n async loadThread(threadId: string): Promise<AgentMessage[] | null> {\n this.sweep();\n const thread = this.threads.get(threadId);\n if (!thread) {\n // With client-owned ids the first turn arrives before anything has been\n // written under it, so unknown is empty. Otherwise unknown is a thread\n // that expired or never existed, and the controller has to be able to\n // tell: `[]` here is the silent fresh conversation this store used to\n // hand out in place of the user's history.\n return this.clientOwnedIds ? [] : null;\n }\n thread.touchedAt = Date.now();\n // Copied, so a caller that sorts or splices the result does not edit the\n // stored history in place.\n return thread.messages.slice();\n }\n\n async appendMessages(threadId: string, messages: AgentMessage[]): Promise<void> {\n this.sweep();\n let thread = this.threads.get(threadId);\n if (!thread) {\n if (!this.clientOwnedIds) {\n // Creating the thread here would persist a turn under an id the client\n // believes holds a longer conversation, and hide from the app that the\n // conversation is gone. The controller checks before the run and never\n // reaches this; a store-level caller that does gets told.\n throw new Error(`Thread ${threadId} does not exist here, or has expired.`);\n }\n // The client owns the id, so an append to one the store has never seen is\n // the first turn, and refusing it would lose a turn that already happened.\n thread = { messages: [], touchedAt: Date.now() };\n this.threads.set(threadId, thread);\n }\n // Upsert, not push. A turn that resolves a pending call hands back the\n // assistant message that made the call under the id it already had, now\n // with the result attached; pushing it would leave the thread holding both\n // versions, and the model would read the same call twice on every turn\n // that follows. Replacing in place keeps the message where the\n // conversation put it.\n for (const message of messages) {\n const at = thread.messages.findIndex((held) => held.id === message.id);\n if (at === -1) {\n thread.messages.push(message);\n } else {\n thread.messages[at] = message;\n }\n }\n thread.touchedAt = Date.now();\n }\n\n /** Test seam, and a way for an app to drop a conversation on request. */\n delete(threadId: string): void {\n this.threads.delete(threadId);\n }\n\n get size(): number {\n return this.threads.size;\n }\n\n sweep(now = Date.now()): void {\n if (now - this.lastSweep < SWEEP_INTERVAL_MS) {\n return;\n }\n this.lastSweep = now;\n for (const [threadId, thread] of this.threads) {\n if (now - thread.touchedAt > this.ttlMs) {\n this.threads.delete(threadId);\n }\n }\n }\n}\n\n/**\n * The process-wide default every `AgentController` uses unless it is given\n * another.\n *\n * It has to be a shared instance, not a field initializer. A gemi controller is\n * constructed per request — `RouteHandler.run()` does `new Controller()` every\n * time — so `store = new MemoryAgentStore()` written in a controller field is a\n * brand new, empty store on every turn, and a threaded conversation would read\n * back nothing while looking like it was configured correctly. Anything\n * process-lived that a controller holds has to be created outside it.\n */\nexport const defaultAgentStore = new MemoryAgentStore();\n",
19
23
  "import { Storage } from \"../facades/Storage\";\nimport { Controller } from \"../http/Controller\";\nimport { HttpRequest } from \"../http/HttpRequest\";\nimport type { MiddlewareInput } from \"../http/middlewareList\";\nimport type { AgentRun, AgentRunResult, AnyAgent, ToolShapesOf } from \"./Agent\";\nimport {\n type Attachment,\n ATTACHMENT_ID_PREFIX,\n type AttachmentDestination,\n attachmentObjectName,\n type AttachmentScope,\n type AttachmentStorage,\n type AttachmentStore,\n defaultAttachmentStore,\n newAttachmentId,\n ScopedAttachments,\n} from \"./store/Attachments\";\nimport {\n FrameCursorEvictedError,\n liveRuns as defaultLiveRuns,\n LiveRunNotFoundError,\n MemoryLiveRuns,\n} from \"./store/LiveRuns\";\nimport { defaultAgentStore, MemoryAgentStore } from \"./store/MemoryAgentStore\";\nimport { sseResponse } from \"./store/sse\";\nimport type {\n AgentError,\n AgentMessage,\n AgentStreamEvent,\n ClientTurn,\n PendingToolCall,\n ToolShapes,\n} from \"./types\";\n\n// --- storage -------------------------------------------------------------\n\n/**\n * Where conversations live.\n *\n * Stateless is the default and nothing here is required to hold a conversation:\n * the client can carry its own history, and a pending approval travels in it\n * safely because the server signed it. What a store buys is a conversation that\n * survives the browser — and, with `/attach`, one whose interrupted turn is\n * still there after a refresh.\n */\nexport interface AgentStore {\n /**\n * Where a `threadId` comes from. `ApiRouter.agent()` mounts no route for\n * this: the app writes one, calls it here, and hands the id to `useChat` —\n * the mount is the app's because it is where ownership gets recorded, and a\n * store that holds no user cannot do that for it. An id that did not come out\n * of here is `null` from `loadThread` and a 404 from the controller, unless\n * the store's ids are the client's by design (`MemoryAgentStore`'s\n * `clientOwnedIds`).\n */\n createThread(params: { userId?: string | number }): Promise<{ threadId: string }>;\n /**\n * The history, or `null` for a thread the store does not have.\n *\n * `null` and `[]` are different answers and the controller acts on the\n * difference: an empty array is a conversation with nothing in it yet, and a\n * turn on it runs; `null` is a 404 before anything runs. A store that answers\n * `[]` for an id it has never seen turns an expired or mistyped thread into a\n * fresh conversation with no signal, and persists the next turn under the\n * dead id. Only a store whose ids are minted by the client should do that,\n * and then on purpose — see `MemoryAgentStore`'s `clientOwnedIds`.\n */\n loadThread(threadId: string): Promise<AgentMessage[] | null>;\n /**\n * Upsert by message id: replace a message the thread already holds, append\n * one it does not, and keep the order the thread had. Called with a thread\n * `loadThread` just found; must not create one.\n *\n * The name says append because that is what almost every call is, but a\n * turn that resolves a pending call reports the earlier assistant message\n * again — same id, now with the result attached — and a store that only\n * appends ends up with both versions. A table keyed by message id has the\n * lookup already but still needs an insert-or-update rather than a plain\n * insert, which would fail on the key; a store that is a list has to look\n * before it pushes.\n */\n appendMessages(threadId: string, messages: AgentMessage[]): Promise<void>;\n}\n\n/** The default: conversations last as long as the process. */\nexport { defaultAgentStore, MemoryAgentStore };\n\n/** The attachment half of the same story. See `store/Attachments.ts`. */\nexport {\n type Attachment,\n type AttachmentDestination,\n AttachmentNotFoundError,\n type AttachmentScope,\n type AttachmentStorage,\n type AttachmentStore,\n defaultAttachmentStore,\n InvalidAttachmentScopeError,\n MemoryAttachmentStore,\n type PutAttachmentParams,\n ScopedAttachments,\n type ToolAttachmentPut,\n type ToolAttachmentRecord,\n type ToolAttachments,\n} from \"./store/Attachments\";\n\n/**\n * The runs currently in flight, and their frames.\n *\n * A run outlives the request that started it, so something has to hold it while\n * no one is listening, and hold what it emitted meanwhile so a returning client\n * can catch up rather than start over. That is all this is: a per-process map\n * plus a bounded buffer.\n *\n * Per-process is not an implementation shortcut that a better store fixes — a\n * running generator lives in one process, and a second server cannot attach to\n * it. Reattachment therefore needs the request to land where the run is: one\n * server, sticky routing, or a proxy that forwards by `runId`. Worth saying out\n * loud, because the failure mode behind a round-robin load balancer is a\n * refresh that usually works.\n *\n * This is the read side, which is all `attach` and `stop` need. Registering a\n * run and replaying its buffer are on `MemoryLiveRuns`, the only implementation\n * there can be — see the note there for why a second one would not help.\n */\nexport interface LiveRuns {\n /** What the client asks after a refresh: is anything still going here? */\n find(params: { threadId: string }): Promise<{ runId: string; seq: number } | null>;\n get(runId: string): AgentRun | null;\n /** Kept for a short while after `run-end`, so a refresh a second late still\n * sees the tail instead of an empty screen. */\n ttlMs: number;\n}\n\nexport {\n FrameCursorEvictedError,\n LiveRunNotFoundError,\n MemoryLiveRuns,\n defaultLiveRuns as liveRuns,\n};\n\n// --- controller ----------------------------------------------------------\n\nexport type AgentHookContext = {\n req: HttpRequest<any, any>;\n runId: string;\n threadId?: string;\n};\n\n/**\n * What `POST /<path>/files` answers.\n *\n * TWO IDS, AND THEY ARE NOT INTERCHANGEABLE. `fileId` is the *provider's* id and\n * means exactly what it always meant — the value a `FilePart` carries so the\n * model can see the file. `attachmentId` is *gemi's*, and is the handle a tool\n * resolves through `ScopedAttachments` to get the bytes back. A file can have\n * both, and usually does.\n *\n * `fileId` stays first and stays named that on purpose: `useChat.uploadFile`\n * already reads it, apps already put it in a `FilePart`, and renaming it or\n * folding the two into one id would break vision for every existing app while\n * looking like a tidier API. The two ids exist because there are two systems\n * holding the file.\n *\n * Both are optional, and `destination` says which to expect: `provider` has no\n * `attachmentId` (nothing was kept), `storage` has no `fileId` (the provider\n * never saw it, so there is nothing for a `FilePart` to point at), `both` has\n * both.\n *\n * `destination` ALONE IS NOT ENOUGH TO SAY WHY AN ID IS MISSING, which is why\n * `downgraded` is here. An upload that reached a route with no authentication\n * and no `threadId` answers `destination: \"provider\"` — truthfully, that is\n * where the bytes went — and so does a controller that deliberately returns\n * `\"provider\"` from `attachmentDestination`. Those two answers were byte\n * identical, and they are the opposite situations: one is the app's policy\n * working, the other is the app's policy silently not applying because the\n * server could not tell who was calling. The server logs the second one once\n * per process; the client saw nothing at all. `downgraded` is the difference,\n * carried in the answer so a client — or an integration test — can assert on\n * it.\n */\nexport type UploadResult = {\n /** The provider's id, for a `FilePart`. Absent when the file never went there. */\n fileId?: string;\n /** gemi's id, for a tool. Absent when nothing was kept — see `attachmentScope`. */\n attachmentId?: string;\n name: string;\n mimeType: string;\n size: number;\n destination: AttachmentDestination;\n /**\n * Set only when the app's policy asked for a copy and the request had no\n * subject to file it under, so the upload fell back to the provider alone.\n * Absent when `destination` is what the app actually chose.\n *\n * A string rather than `true` because there is exactly one reason today and\n * there will be more (a store that refused the write, say), and a boolean\n * that later needs a reason beside it is two fields where one would have\n * done.\n */\n downgraded?: \"no_scope\";\n};\n\n/**\n * `Controller.kind` is typed as the literal `\"controller\"`, so a subclass that\n * declares a `kind` of its own fails the static-side check (TS2417). Widening\n * it belongs in `http/Controller.ts`, which this slice does not own; erasing\n * the static side of the base here is the smaller change and costs nothing —\n * `Controller.kind` has no reader, in this package or out of it.\n */\nconst ControllerBase = Controller as new () => Controller;\n\n/**\n * The thread a `stream` is setting up on, to the setup ahead of it. Module\n * level because the controller is constructed per request, so nothing on it\n * can be seen by the next request; keyed by thread rather than held on\n * `liveRuns` because it guards the controller's protocol, not the frames. An\n * entry is removed once the last setup queued on it releases.\n */\nconst threadLocks = new Map<string, Promise<void>>();\n\n/**\n * Each run to the promise that its transcript has been stored. Weak, so a run\n * the map has evicted is not kept alive here for a promise nobody will ask for.\n */\nconst persisted = new WeakMap<AgentRun, Promise<void>>();\n\n/**\n * Turns read but not yet registered, by the client's name for them.\n *\n * A run can be stopped from `register` on, and named from `run-start` on. What\n * `/stop` could not reach was a turn still waiting its place on the thread —\n * which is where a user who sent twice and thought better of it presses stop,\n * and the wait is now as long as the old run's unwind. Falling through to\n * `threadId` there found the old run, already stopping, said `stopped: true`,\n * and the queued turn started anyway: unwatched, billing, and with no handle\n * left on the client that had let go of it. The entry is marked rather than\n * removed, because the turn itself is what answers once its wait is over.\n */\nconst pendingTurns = new Map<string, { cancelled: boolean }>();\n\n/**\n * The key that carries `Body` on the instance type.\n *\n * A `declare const` unique symbol, so it exists only in the type layer — the\n * same device `Schema.ts` uses for its output type, and for the same reason: a\n * real property would be one an app could see, serialise or depend on.\n */\ndeclare const BODY: unique symbol;\n\nexport abstract class AgentController<\n A extends AnyAgent = AnyAgent,\n /**\n * The extra fields `useChat`'s `body` option sends with every turn, as they\n * arrive in `instructions()` and on `ctx.body`.\n *\n * Declaring it also types the client: `useChat` reads it back off the route\n * through `AgentRouteRPC`, so a `body` the controller does not expect is a\n * compile error at the call site rather than an `undefined` three layers\n * into a tool. It types the shape, not the contents — see `instructions`.\n *\n * Constrained to `object`, not `Record<string, unknown>`: an `interface` gets\n * no implicit index signature, so the tighter bound refused\n * `interface PageBody { pageId: string }` with a `TS2344` that names the\n * constraint and not the reason. An interface is how most apps write a request\n * shape, and turning that into a puzzle about `type` versus `interface` is a\n * poor first impression of the feature.\n */\n Body extends object = Record<string, unknown>,\n> extends ControllerBase {\n static kind = \"agent-controller\" as const;\n\n /**\n * `Body`, in a position nothing can shorten away.\n *\n * It has to appear on the instance type for `AgentRouteRPC` to infer it, and\n * `instructions`'s second parameter is not a safe place to leave that job:\n * TypeScript's parameter-wise inference stops at the shorter signature, so a\n * controller overriding `instructions(req)` — which is every override written\n * before this existed, since that was the only signature — supplied no\n * candidate and silently fell back to the open record. The client-side\n * checking then vanished on the most ordinary upgrade path, with nothing to\n * read.\n *\n * `declare` so it emits no field: there is no value here, only a type for\n * `infer B` to find. Optional and never assigned, so no subclass has to\n * mention it.\n */\n declare readonly [BODY]?: Body;\n\n /** The agent this controller serves. A property rather than a constructor\n * argument so `Router.agent(ChatController)` can take the class, matching how\n * every other controller is mounted. */\n abstract agent: A;\n\n /**\n * Defaults to the process-wide `MemoryAgentStore`.\n *\n * Whatever you put here, make it something that outlives the request: this\n * controller is constructed fresh for every call, so `store = new\n * MemoryAgentStore()` written here is an empty store on every turn and a\n * threaded conversation silently reads back nothing. Assign a module-level\n * instance, or a store whose state is somewhere else entirely.\n */\n store: AgentStore = defaultAgentStore;\n\n /**\n * Defaults to the process-wide map. Overridable so a test — or an app running\n * two agents that must not see each other's runs — can hold its own; not so\n * that it can be moved off the process, which is not a thing that can be\n * done. See `MemoryLiveRuns`.\n */\n liveRuns: MemoryLiveRuns = defaultLiveRuns;\n\n /**\n * Where attachment *records* live — the row that says which bytes, whose, and\n * under what id. Defaults to the process-wide `MemoryAttachmentStore`, with\n * the same warning `store` carries: assign something that outlives the\n * request, because this controller is constructed per call.\n */\n attachments: AttachmentStore = defaultAttachmentStore;\n\n /**\n * Where an attachment's *bytes* live. Defaults to the app's configured\n * `FileStorage` through the `Storage` facade, which resolves the driver per\n * call out of the container — so an app that configured S3 gets S3 here\n * without saying so twice.\n *\n * Overridable mostly for tests and for an app that keeps user uploads in a\n * different bucket from everything else.\n */\n attachmentStorage: AttachmentStorage = Storage;\n\n /**\n * How long the request that started a run is held open for its hooks, in\n * milliseconds, once the run has settled. The run itself is not bounded\n * here — the request is held for as long as it runs — only the app's code\n * still going after it, from `onMessage` to an `onAwaitingInput` queued\n * behind a slow `onToolCall`.\n *\n * The hooks read the user from that request (`ctx.req.ctx().user`,\n * `Auth.user()`, a policied query), so the request stays open until they are\n * done; but a hook that never settles — a `fetch` with no timeout — would\n * then hold the store, the user and `onRequestEnd` forever, one per turn. Past\n * this the request ends anyway: `onRequestEnd` runs, the user is released,\n * and the hook runs on without one. Thirty seconds is far past a healthy\n * write or notification, so a hook that reaches it is hung rather than slow.\n * Raise it for a hook that is legitimately longer; `Infinity` holds the\n * request until the hooks are done, however long that is.\n */\n protected hookHoldMs = 30_000;\n\n /**\n * Appended to the agent's static instructions for this request — the user's\n * name, tenant, today's date.\n *\n * `body` is what `useChat`'s `body` option sent, with the four keys the wire\n * format owns removed. It is the only way to read it here: this controller\n * consumes the request body itself, so `await req.input()` answers `Body\n * already used` and takes the whole turn down with it.\n *\n * It is client-controlled, exactly like any request body. `Body` types what\n * arrives, it does not check it — a client can send anything, and a tenant id\n * read from here is the client's claim about which tenant it wants, not the\n * middleware's finding about who it is.\n */\n instructions(req: HttpRequest<any, any>, extra: { body: Body }): string | Promise<string> | void {\n void req;\n void extra;\n }\n\n /**\n * Refuse a turn that arrives without a `threadId`, with a 400\n * `thread_required`, before anything runs.\n *\n * A stateless turn is otherwise open to any caller the route's middleware\n * lets through: the client carries the history, so the model runs, and is\n * billed, as a general-purpose chat. An app whose tools resolve their subject\n * from the thread can make them refuse such a turn, but the model has still\n * been paid for by then.\n *\n * It governs `stream` only. `upload` can still reach the provider without a\n * thread (a `provider` destination); refuse that in `authorizeRequest`.\n */\n protected requireThread = false;\n\n /**\n * Decides whether this caller may use this route, on every one of the four:\n * `stream`, `attach`, `stop` and `upload` (#542). Throw a request breaker\n * (`InsufficientPermissionsError`, say) to refuse; return to let it through.\n *\n * It runs before the route does any work of its own: on `stream`, ahead of\n * the thread's lock and load, `instructions()` and the provider, so a refused\n * turn is not charged for, waits behind no run and does not learn whether its\n * thread exists; on `attach`, before a live run's frames are read.\n *\n * `threadId` is the one the client sent, taken on trust and not yet checked\n * against the store — which is what makes this the place to check that the\n * caller owns it. A thread-scoped app that checks it here on every route\n * keeps a thread's live answer, its stop button and its attachments to its\n * owner. It is `undefined` when the request names no thread: a stateless\n * turn, an upload without one, a stop by `runId` or `clientRunId`.\n */\n protected authorizeRequest(\n req: HttpRequest<any, any>,\n params: { route: \"stream\" | \"attach\" | \"stop\" | \"upload\"; threadId?: string },\n ): void | Promise<void> {\n void req;\n void params;\n }\n\n /**\n * `POST /<path>` — one route for every client turn. A first message, an\n * approval, an answer to a question and a client tool's result are all just\n * the next turn, so none of them gets an endpoint of its own.\n */\n async stream(req: HttpRequest<any, any> = new HttpRequest()): Promise<Response> {\n const parsed = await readJsonBody(req);\n if (parsed.error) {\n // Before anything is registered or charged for. A run started off a body\n // we could not read would answer nothing, at the user's expense.\n return invalidRequest(parsed);\n }\n const body = parsed.body;\n const threadId = typeof body.threadId === \"string\" ? body.threadId : undefined;\n const parsedTurn = toClientTurn(body);\n if (parsedTurn.error) {\n return invalidRequest({ body: {}, error: parsedTurn.error });\n }\n const turn = parsedTurn.turn;\n const clientRunId = typeof body.clientRunId === \"string\" ? body.clientRunId : undefined;\n\n // Before `pendingTurns`, so a refused turn leaves nothing for `/stop` to\n // find. It does not yield, unlike `authorizeRequest` below.\n if (this.requireThread && !threadId) {\n return jsonResponse(400, {\n code: \"thread_required\",\n message: \"This agent takes a turn only on a thread: send a threadId.\",\n });\n }\n\n // From here until `register`, the only thing `/stop` can find this turn by.\n // See `pendingTurns`.\n const pending = clientRunId ? { cancelled: false } : null;\n if (pending) {\n pendingTurns.set(clientRunId, pending);\n }\n\n // The whole of the stateless/threaded difference, in one expression: with a\n // thread the server owns the history, without one the client carries it.\n // Nothing else below branches on it, which is why stateless keeps working\n // even for an app that never configures a store.\n //\n // The `threadId` is the client's, and nothing here checks that this caller\n // owns it — `AgentStore` cannot, since it holds no user. A `threadId` is\n // therefore a capability and has to be unguessable (`createThread` mints a\n // uuid) and, for anything that matters, checked: override `authorizeRequest()`\n // or give it a `store` that scopes by `req.user`. The framework's job is to make\n // sure the route is behind the router's middleware in the first place,\n // which `ApiRouter.agent()` now does.\n //\n // Everything from the load on happens inside `start`, which on a thread\n // runs under the thread's lock once the previous run's transcript is in the\n // store — the load has to come after that, or it reads a history the old\n // answer is missing from. `instructions()` follows the load in there rather\n // than running ahead of the lock, so that a dead thread is still a 404\n // before the app's own work is spent on it; the cost is that a turn queued\n // behind this one waits for `instructions()` as well.\n const start = async (): Promise<Response> => {\n let messages: AgentMessage[];\n if (threadId) {\n const history = await this.store.loadThread(threadId);\n if (!history) {\n // Before `instructions()` and before the run: nothing has been\n // charged for, and the client has to learn that its thread is gone —\n // expired, mistyped, or on an instance that no longer exists — rather\n // than see it answered as an empty conversation and have this turn\n // persisted under the dead id.\n //\n // That ordering is deliberate. An ownership check belongs in\n // `authorizeRequest()`, which has already run; one left in\n // `instructions()` has not, so a caller holding a uuid learns whether\n // it is live here without the app's say. The id is unguessable, so\n // the load stays above `instructions()`, whose own work (a database\n // read, typically) would otherwise be spent on a thread that is gone.\n // Move it below and that work is spent on every dead id instead.\n return jsonResponse(404, {\n code: \"thread_not_found\",\n message: `Thread ${threadId} does not exist here, or has expired.`,\n });\n }\n messages = history;\n } else {\n messages = Array.isArray(body.messages) ? (body.messages as AgentMessage[]) : [];\n }\n\n const extraBody = appBody<Body>(body);\n const instructions = (await this.instructions(req, { body: extraBody })) || undefined;\n // Above the cancel check, not below it, and that placement is the whole\n // reason this is a separate statement rather than an argument on the call\n // below: the comment on that check says nothing yields between it and\n // `register`, and an `await` for the app's `attachmentScope()` — a\n // database read, in any app that has one — is exactly the yield a stop\n // would land in and be missed.\n const attachments = await this.attachmentsFor(req, threadId);\n\n if (pending?.cancelled) {\n // Stopped while it waited. Nothing has been asked of the model and\n // nothing registered, so there is no run to end and nothing charged\n // for. Checked after the last `await` above, so that a stop landing\n // during it is not missed: from here to `register` nothing yields.\n return stoppedBeforeStart();\n }\n\n const run = this.agent.stream({\n messages,\n turn,\n req,\n threadId,\n instructions,\n // Resolved once, above, and handed to every tool of the run as\n // `ctx.attachments`. It is derived from the request — the user the\n // middleware authenticated, or the thread — and never from anything the\n // model can write, which is the property that makes an attachment id\n // safe to give a model at all. `null` for a request with no subject:\n // tools still get an object, and it throws with a sentence naming\n // `attachmentScope()`.\n attachments,\n // The same object `instructions()` was handed, so the two cannot\n // disagree about what the client sent — and carried on the run rather\n // than read from `ctx.req`, because a run outlives the request that\n // started it. By the time a tool executes, the body is long gone.\n // Cast because `Body` is constrained to `object` so an `interface` can be\n // used, and an interface has no index signature to satisfy\n // `Record<string, unknown>`. The value is a parsed JSON object either\n // way; the constraint is about what an app may declare, not about what\n // arrives.\n body: extraBody as Record<string, unknown>,\n }) as AgentRun;\n\n const ctx: AgentHookContext = { req, runId: run.runId, threadId };\n\n // Registered before the response is built: the run is now owned by the\n // process rather than by this request, which is the property `/attach`\n // depends on and the reason a dropped connection no longer cancels\n // anything.\n const eventHooks = this.liveRuns.register(run, {\n threadId,\n // The client's handle on a run it started, which is the only one that\n // exists before `run-start` reaches it. See `RegisterParams`.\n clientRunId,\n onEvent: (event) => this.dispatchEvent(event, ctx),\n onInternalError: (err) => this.reportHookFailure(err),\n });\n\n // Kept, not just fired: the next turn on this thread has to know when\n // this one's transcript is in the store. See `withThread`. The request\n // waits for more than the thread does — the hooks as well — so that\n // `onMessage` still finds the user who sent the message; the thread\n // waits only for the store, so a slow hook is not a slow next turn.\n const { stored, hooks } = this.persistRun(run, ctx, eventHooks);\n persisted.set(run, stored);\n // Registered now, while the request is certainly open: `waitUntil` is\n // ignored once it has ended, and the run settling can end it before a\n // single hook has been called.\n req.ctx?.()?.waitUntil(hooks);\n\n return run.toResponse();\n };\n\n try {\n // After `pending` is registered, not before: the app's check is a yield —\n // a database read, typically — and a stop pressed during it has to find\n // this turn. Before the thread's lock, which ends the thread's previous\n // run, so a refused or stopped turn does not end it either.\n await this.authorizeRequest(req, { route: \"stream\", threadId });\n if (pending?.cancelled) {\n return stoppedBeforeStart();\n }\n return threadId ? await this.withThread(threadId, start) : await start();\n } finally {\n if (pending && pendingTurns.get(clientRunId) === pending) {\n pendingTurns.delete(clientRunId);\n }\n }\n }\n\n /**\n * A thread holds one run at a time; a new turn on it ends the old one first.\n *\n * Since a dropped connection no longer stops a run, a user who sends again\n * mid-answer used to leave the first run going. It could not see the new\n * turn, the new run's `loadThread` could not see its answer, and both\n * appended when they finished — in whichever order the model returned them,\n * so the thread read `user2, assistant2, user1, assistant1`. `byThread` then\n * named the second run while the first was still live and unstoppable by\n * `threadId`.\n *\n * Stopping the old run and waiting for its transcript to land is chosen over\n * refusing the new turn with a 409, because sending again *is* the stop: it\n * is what `useChat.send` means, and a client that has to poll `/stop` until\n * the run is really gone before it may post the turn it has already shown is\n * a worse client for no better thread. The cost is that the new turn waits\n * for the old run to unwind, which is as long as its slowest tool in flight —\n * and that wait is the thing that puts `assistant1` before `user2`.\n *\n * The wait is on `persistRun`'s `stored`, not on `run.result()`. `result()`\n * settling is the transcript being final, not stored: `appendMessages` runs\n * after it, and a `loadThread` in that gap reads a history the old answer is\n * missing from, which is the original bug by a shorter route. It is also\n * *only* that: `stored` settles once the transcript is stored, and the app's\n * hooks run on in `hooks`, which holds the old run's request and not the\n * thread, so a slow `onMessage` is not a slow thread and a hung one is not a\n * hung thread.\n *\n * The lock around it is what makes a *third* turn wait for the second rather\n * than for the first. Two turns arriving together both see the same live\n * run, both stop it, both wait for it, and both start — the same race, one\n * message later. Under the lock the later one finds the earlier one\n * registered and stops that instead. Per process, like `LiveRuns`, and for\n * the same reason: the run it guards lives here.\n */\n private async withThread<T>(threadId: string, fn: () => Promise<T>): Promise<T> {\n const previous = threadLocks.get(threadId) ?? Promise.resolve();\n let release!: () => void;\n const held = new Promise<void>((resolve) => {\n release = resolve;\n });\n const tail = previous.then(() => held);\n threadLocks.set(threadId, tail);\n await previous;\n try {\n const live = await this.liveRuns.find({ threadId });\n const run = live ? this.liveRuns.get(live.runId) : null;\n if (run) {\n // A run that already ended is still `find`-able for `ttlMs`; stopping\n // it is a no-op and waiting on it is the append it may still be doing.\n run.stop({ reason: \"superseded by a later turn on this thread\" });\n await persisted.get(run);\n }\n return await fn();\n } finally {\n release();\n if (threadLocks.get(threadId) === tail) {\n threadLocks.delete(threadId);\n }\n }\n }\n\n /**\n * `POST /<path>/attach` — subscribe to a run already in progress, from a\n * cursor. This is a read of a live run, not a continuation of a stopped one:\n * the work never paused, the listener changed.\n *\n * The cursor is `from`, else the client's `cursor`, else `Last-Event-ID`,\n * else the oldest frame still buffered — and it is honoured only for the run\n * the client's `runId` names. A cursor the buffer has dropped is a 410 naming\n * what survives; *no* cursor is the tail, because a page reattaching on mount\n * has no transcript to leave a hole in. See `resolveCursor`.\n */\n async attach(req: HttpRequest<any, any> = new HttpRequest()): Promise<Response> {\n const parsed = await readJsonBody(req);\n if (parsed.error) {\n return invalidRequest(parsed);\n }\n const body = parsed.body;\n const threadId =\n typeof body.threadId === \"string\" ? body.threadId : searchParam(req, \"threadId\");\n\n if (!threadId) {\n return jsonResponse(400, {\n code: \"invalid_request\",\n message: \"attach needs a threadId: it is the handle that survives a refresh.\",\n });\n }\n\n // Before the lookup: a refused caller learns neither whether a run is live\n // on the thread nor any frame of it.\n await this.authorizeRequest(req, { route: \"attach\", threadId });\n\n // The store is not consulted. This route answers whether a run is in\n // flight here, and a run it finds was started on a thread `stream` had\n // already loaded; a miss is `no_live_run` whether or not the thread exists,\n // because a client that gets one re-reads the thread anyway (see\n // `onAttachMiss`) and learns there. The thread's own 404 is `stream`'s,\n // where a turn would otherwise be persisted under it.\n const live = await this.liveRuns.find({ threadId });\n if (!live) {\n // An explicit miss, not a 200 with an empty stream. See `MemoryLiveRuns`:\n // behind a round-robin load balancer this is the common case, and it has\n // to be visible as one.\n return jsonResponse(404, {\n code: \"no_live_run\",\n message: `No run in progress for thread ${threadId} in this process.`,\n });\n }\n\n // A cursor is only a number if you know which run it counts within: `seq`\n // restarts at zero in every run, so honouring a cursor from run_1 against a\n // live run_2 would skip that many frames off the head of a run this client\n // has seen nothing of. `runId` is the client saying which run its cursor\n // means; naming one that is not live forfeits the cursor and takes the\n // tail, which is as close to the start as the buffer can offer.\n //\n // Only a MISMATCH forfeits. An absent `runId` is not a wrong answer, it is\n // an older question: `from` and `Last-Event-ID` predate the pairing and\n // carry no run, and an `EventSource` reconnecting on its own will never\n // grow one. Those keep resuming exactly as before.\n const cursorRunId = typeof body.runId === \"string\" ? body.runId : undefined;\n const cursor = cursorRunId && cursorRunId !== live.runId ? undefined : resolveCursor(req, body);\n\n try {\n return sseResponse(this.liveRuns.replay(live.runId, cursor));\n } catch (err) {\n if (err instanceof FrameCursorEvictedError) {\n // 410 rather than 404: the run is there, the position is not. A client\n // that gets this reloads the thread instead of resuming into a hole.\n return jsonResponse(410, {\n code: err.code,\n message: err.message,\n oldestSeq: err.oldest,\n });\n }\n if (err instanceof LiveRunNotFoundError) {\n // Lost the race with eviction between `find` and `replay`.\n return jsonResponse(404, { code: err.code, message: err.message });\n }\n throw err;\n }\n }\n\n /**\n * `POST /<path>/stop` — the explicit cancel. Since a dropped connection no\n * longer stops a run, this and a later turn on the same thread are the only\n * things that do, and it is why stopping cannot be a client-side concern:\n * the tool loop is here, and a client that stops reading has not stopped\n * step four from charging a card.\n *\n * Returns as soon as the run is aborted, not when it has finished unwinding.\n * The terminal events — the stopped tool results, the aborted message — go\n * out on the run's own stream, so whoever is watching it sees the ending,\n * and `onMessage` records it whether anyone is watching or not.\n *\n * Answers `{ stopped }` normally, and a `Response` only to reject a request\n * it could not read — the union is the error, not a second success shape.\n */\n async stop(\n req: HttpRequest<any, any> = new HttpRequest(),\n ): Promise<{ stopped: boolean } | Response> {\n const parsed = await readJsonBody(req);\n if (parsed.error) {\n // `{ stopped: false }` would be the wrong answer as well as the wrong\n // status: it means \"there was nothing to stop\", and the truth is that we\n // could not tell what to stop. A client that believes the first one stops\n // asking, and the run keeps going.\n return invalidRequest(parsed);\n }\n const body = parsed.body;\n\n await this.authorizeRequest(req, {\n route: \"stop\",\n threadId: typeof body.threadId === \"string\" ? body.threadId : undefined,\n });\n\n // Three handles, most specific first, because which ones the client has\n // depends on how far the run got. `runId` is the server's own and settles\n // it. `clientRunId` covers the window before `run-start`, when the client\n // has nothing else for a stateless first turn — and, before that, a turn\n // that has not become a run yet because it is waiting its place on the\n // thread, which is ended where it waits. `threadId` is the fallback for a\n // client that did not start this run at all — one that attached to it,\n // whose replayed tail carried no `run-start`.\n let runId = typeof body.runId === \"string\" ? body.runId : undefined;\n if (!runId && typeof body.clientRunId === \"string\") {\n runId = this.liveRuns.findByClientRunId(body.clientRunId) ?? undefined;\n if (!runId) {\n const pending = pendingTurns.get(body.clientRunId);\n if (pending) {\n // Not on to `threadId`: that names the run this turn is queued\n // behind, which is already stopping, and answering for it would\n // leave this one to start.\n pending.cancelled = true;\n return { stopped: true };\n }\n }\n }\n if (!runId && typeof body.threadId === \"string\") {\n runId = (await this.liveRuns.find({ threadId: body.threadId }))?.runId;\n }\n\n const run = runId ? this.liveRuns.get(runId) : null;\n if (!run) {\n // Already finished, already evicted, or never here. Not an error: the\n // caller wanted the run stopped and it is not running.\n return { stopped: false };\n }\n\n run.stop({ reason: typeof body.reason === \"string\" ? body.reason : undefined });\n\n // Deliberately not awaiting `run.result()`. Unwinding means letting tools\n // in flight settle into `denied` results and finalizing the assistant\n // message, which can take as long as the slowest tool — and a stop button\n // that spins for twenty seconds is a stop button people press twice.\n return { stopped: true };\n }\n\n /**\n * WHO A SCOPE BELONGS TO, and the one question in this file whose wrong\n * answer is a data leak rather than a bug.\n *\n * An attachment id given to the model comes back *from* the model, and what\n * the model writes is a function of everything it read — the user's text, a\n * tool's output, a document someone uploaded. So the id is untrusted in\n * exactly the way a request body is, and the scope it resolves under must come\n * from the server's own knowledge of the request. Nothing that reaches this\n * method may be readable or writable by the model: `req` is the HTTP request,\n * and `threadId` is the client's handle, not the model's.\n *\n * THE DEFAULT IS THE AUTHENTICATED USER, falling back to the thread, falling\n * back to `null`.\n *\n * - `user:<id>` when the request is authenticated. `req.ctx().user` is what\n * `AuthenticationMiddleware` and the `Auth` facade put there, so an agent\n * route behind `middlewares = [\"auth\"]` — which `ApiRouter.agent()` makes\n * the default posture — has a subject without the app writing anything.\n * - `thread:<id>` for an unauthenticated chat that has a thread. A `threadId`\n * is minted by `createThread` as a uuid and is documented as a capability,\n * and `upload` only passes one here that `store.loadThread` finds;\n * an anonymous support widget has nothing better, and scoping to the\n * conversation is strictly narrower than scoping to nothing.\n * - `null` otherwise, which means no attachment id is minted at all. There is\n * no third fallback on purpose. `sessionId()` is the tempting one and it is\n * the wrong one: it is a cookie the visitor writes, documented as *not a\n * credential*, so scoping to it is scoping to a value the caller chooses —\n * which is not a scope, it is a lookup key with extra steps.\n *\n * WHAT THIS DEFAULT GETS WRONG, since it will get something wrong for someone:\n * it is per user, not per conversation, so a user's own upload from one thread\n * resolves in another thread of theirs. That is the loose direction, and it is\n * chosen because the tight one breaks the flow the framework actually ships —\n * `useChat.uploadFile` posts a file before any thread exists, so a per-thread\n * default would make the first upload of every conversation unresolvable. An\n * app that wants per-conversation isolation returns\n * `{ key: \\`user:${id}:thread:${threadId}\\` }` here and has it; an app with\n * tenants returns `{ key: \\`org:${orgId}\\` }` and has that. What the default\n * must never be is broader than a user, and it is not.\n *\n * WHATEVER THIS READS MUST BE PRESENT ON EVERY AGENT ROUTE, and the default\n * reads `req.ctx().user`, which is present only where authentication\n * middleware ran. `agent()` mounts four routes and `.middleware()` guards\n * them individually, so `middleware({ stream: \"auth\" })` — the example in\n * `ApiRouter.agent`'s own doc — leaves `POST /chat/files` unauthenticated.\n * The upload then files the record under `thread:<id>` (or nothing at all)\n * while the run that follows, on the guarded route, resolves under\n * `user:<id>`, and every id minted on upload is a miss for the rest of the\n * conversation. It fails as `AttachmentNotFoundError`, worded identically to\n * an id the model invented, which is the point of that error and is also why\n * this particular misconfiguration is invisible. Guard `upload` and `stream`\n * together, or derive the key from something both of them have.\n *\n * gemi does not detect the skew for you, and the reason is the module's whole\n * premise: noticing that an id exists under a *different* scope requires\n * looking it up without one, and an unscoped read is the thing that must not\n * exist here — not even behind a `console.warn`. So the answer is the\n * documentation you are reading and the identical error, not a probe.\n *\n * The key is opaque and compared with `===`. It is never sent to the client\n * and never shown to the model.\n */\n protected attachmentScope(\n req: HttpRequest<any, any>,\n threadId?: string,\n ): AttachmentScope | null | Promise<AttachmentScope | null> {\n const user = req.ctx()?.user;\n const userId = user?.id ?? user?.publicId;\n if (userId !== undefined && userId !== null && String(userId) !== \"\") {\n return { key: `user:${String(userId)}` };\n }\n if (threadId) {\n return { key: `thread:${threadId}` };\n }\n return null;\n }\n\n /**\n * WHERE ONE FILE'S BYTES SHOULD GO. Called once per upload, with the file in\n * hand.\n *\n * The model needs to perceive a file only for vision or document reading; a\n * tool needs the bytes only to forward them. Those are different needs and\n * they were being answered the same way, because `upload` sent everything to\n * the vendor unconditionally — which is a cost and a data-minimisation problem\n * as much as a capability one. A CSV that exists to be imported belongs in\n * storage and not at the provider; an image the model must look at and no tool\n * will forward is the reverse.\n *\n * THE DEFAULT IS `\"both\"`, AND THE JUSTIFICATION IS THE FAILURE MODE, not the\n * hit rate. A mimeType table was the obvious alternative — images and PDFs to\n * the provider, everything else to storage — and it is wrong in the quiet\n * direction. Getting `\"both\"` wrong sends a file to a vendor that did not need\n * it: it costs money, it is visible on an invoice and in the vendor's file\n * list, and it is corrected by overriding one method. Getting `\"storage\"`\n * wrong drops the file out of the model's view with no error anywhere; the\n * model answers as though the image were not there, which is indistinguishable\n * from a bad prompt, and the developer debugs their instructions for an\n * afternoon. Being wrong on the invoice beats being wrong in a way that\n * presents as \"the AI is stupid\".\n *\n * `\"both\"` is also what keeps this change additive: every upload that reached\n * the provider before still reaches it, and storage is added underneath. An\n * app that never asks for an attachment id sees no difference except a second\n * copy it owns.\n *\n * A client may ask, per file, for `\"storage\"` under a `\"both\"` policy — the\n * one direction that only ever removes the vendor. It may not ask for\n * anything else, including `\"provider\"`, which would let a form field decide\n * that the app does not keep its own copy. See `narrowDestination`.\n */\n protected attachmentDestination(\n file: File,\n req: HttpRequest<any, any>,\n ): AttachmentDestination | Promise<AttachmentDestination> {\n void file;\n void req;\n return \"both\";\n }\n\n /**\n * The attachment handle for this request — the object a tool is given.\n *\n * `null` when `attachmentScope` says the request has no subject, and a caller\n * that gets `null` must not fall back to anything: there is no unscoped handle\n * to fall back to, which is the point.\n *\n * Issue #490 hangs `ctx.attachments` off this.\n */\n protected async attachmentsFor(\n req: HttpRequest<any, any>,\n threadId?: string,\n ): Promise<ScopedAttachments | null> {\n const scope = await this.attachmentScope(req, threadId);\n if (!scope) {\n return null;\n }\n return new ScopedAttachments(this.attachments, this.attachmentStorage, scope);\n }\n\n /**\n * `POST /<path>/files` — takes an attachment and answers the handles for it.\n *\n * `fileId` IS STILL IN THE ANSWER AND STILL MEANS THE SAME THING: the\n * provider's id for the file, the thing a `FilePart` carries, unchanged. It\n * has to stay, because it is what makes vision work and what\n * `useChat.uploadFile` already reads; `attachmentId` is added beside it rather\n * than in place of it. The two are not interchangeable, and swapping them\n * would reach the vendor as a file id it has never issued — so\n * `toResponsesInput` rejects a `gemi_att_` prefix in `FilePart.fileId` with a\n * sentence saying so, rather than leaving it to whatever the vendor answers\n * (not measured).\n *\n * `fileId` is absent exactly when the file did not go to the provider, which\n * only happens when the app or the client asked for that. `attachmentId` is\n * absent when there was no scope to file the record under — see\n * `attachmentScope` — and the answer then carries `downgraded: \"no_scope\"`,\n * which is the only thing separating that case from a controller that chose\n * `\"provider\"` on purpose. Both answer `destination: \"provider\"`, because both\n * are true about where the bytes went.\n *\n * ORDER: storage first, then the provider. If the second one fails the request\n * fails either way, so the only question is where the orphan is left, and an\n * orphan in our own bucket is ours to sweep on a schedule we set, while an\n * orphan at the vendor sits under a retention policy that is not ours.\n */\n async upload(req: HttpRequest<any, any> = new HttpRequest()): Promise<UploadResult> {\n const form = await req.rawRequest.formData();\n const file = form.get(\"file\");\n if (!(file instanceof Blob)) {\n throw new Error(\"upload expects a multipart body with a `file` field.\");\n }\n const name = file instanceof File && file.name ? file.name : \"upload\";\n const mimeType = file.type || \"application/octet-stream\";\n\n // The client's thread, read here only so an unauthenticated chat has\n // something to scope to. It is the client's own handle — `stream` already\n // takes it on trust for the whole conversation — and it is not the model's:\n // this is a form field on an HTTP request the user's browser made, not a\n // tool argument.\n //\n // It is only passed on if the store knows it. `stream` answers\n // `thread_not_found` for an id it has never seen, and this route must not\n // be looser: an invented id would otherwise be a scope, and an anonymous\n // caller could keep writing to storage by sending a fresh one each time.\n const threadField = form.get(\"threadId\");\n // The field as sent, before the store is asked about it, so a refused\n // caller does not learn whether the thread exists.\n await this.authorizeRequest(req, {\n route: \"upload\",\n threadId: typeof threadField === \"string\" && threadField.length > 0 ? threadField : undefined,\n });\n const threadId =\n typeof threadField === \"string\" &&\n threadField.length > 0 &&\n (await this.store.loadThread(threadField)) !== null\n ? threadField\n : undefined;\n\n const policy = await this.attachmentDestination(file as File, req);\n const destination = narrowDestination(policy, form.get(\"destination\"));\n\n const scope = await this.attachmentScope(req, threadId);\n\n // The app's policy wanted a copy of these bytes and the request has no\n // subject to file them under. Recorded rather than merely warned about,\n // because the answer has to be able to tell the client that what it is\n // getting is not the policy — see `UploadResult.downgraded`.\n const downgraded = destination !== \"provider\" && !scope;\n if (downgraded) {\n if (destination === \"storage\") {\n // Bytes were asked to be kept and there is nobody to keep them for.\n // Storing them anyway files them under a scope every caller shares, and\n // answering nothing is an upload that silently did not happen. Neither\n // is an answer; this is.\n throw new Error(\n 'This upload was routed to storage, but `attachmentScope()` returned null for the request, so there is no subject to file the attachment under. Guard the route (`this.agent(Chat).middleware({ upload: \"auth\" })`), send a `threadId` with the upload, or override `attachmentScope()`.',\n );\n }\n // Policy said `both` and the request has no subject: fall through to the\n // provider alone, which is exactly what this route did before attachments\n // existed. Downgrading rather than throwing is what keeps an\n // unauthenticated chat working across the upgrade — but a downgrade nobody\n // is told about is the failure mode this file keeps arguing against, so it\n // is warned once per process on the server and marked `downgraded` in the\n // answer, so the client can tell this apart from a deliberate\n // provider-only policy.\n warnUnscopedUploadOnce();\n }\n\n if (destination === \"provider\" || !scope) {\n // Identical to the pre-attachment behaviour, down to the answer's\n // `fileId` — plus `downgraded` when this path was not the app's choice.\n const fileId = await this.agent.provider.upload(file as File);\n const answer: UploadResult = {\n fileId,\n name,\n mimeType,\n size: file.size,\n destination: \"provider\",\n };\n if (downgraded) {\n answer.downgraded = \"no_scope\";\n }\n return answer;\n }\n\n const attachmentId = newAttachmentId();\n const objectName = await this.attachmentStorage.put({\n name: attachmentObjectName(attachmentId, name),\n body: file,\n contentType: mimeType,\n });\n\n const fileId =\n destination === \"both\" ? await this.agent.provider.upload(file as File) : undefined;\n\n const record: Attachment = {\n id: attachmentId,\n scopeKey: scope.key,\n fileId,\n objectName,\n name,\n mimeType,\n size: file.size,\n createdAt: new Date().toISOString(),\n destination,\n };\n await this.attachments.put(scope, record);\n\n return { fileId, attachmentId, name, mimeType, size: file.size, destination };\n }\n\n /**\n * Protected, not private: these exist to be overridden. `onMessage` fires for\n * every completed message, user and assistant alike, and is the intended\n * persistence point for an app that is not using `store`.\n *\n * Every hook runs inside the request that started the run, which is held\n * open for them — after the run, and after a client that left — so\n * `ctx.req.ctx().user` is the user who sent the turn. See `hookHoldMs` for\n * how long a hook that does not finish keeps it.\n */\n protected onMessage(message: AgentMessage, ctx: AgentHookContext): void | Promise<void> {\n void message;\n void ctx;\n }\n\n protected onToolCall(\n call: { toolCallId: string; name: string; input: unknown },\n ctx: AgentHookContext,\n ): void | Promise<void> {\n void call;\n void ctx;\n }\n\n /** Fires before the stream ends `awaiting-input` — where to notify whoever\n * has to approve, if they are not the person watching the stream. */\n protected onAwaitingInput(\n pending: PendingToolCall[],\n ctx: AgentHookContext,\n ): void | Promise<void> {\n void pending;\n void ctx;\n }\n\n protected onError(error: AgentError, ctx: AgentHookContext): void | Promise<void> {\n void error;\n void ctx;\n }\n\n protected onStreamComplete(\n result: AgentRunResult<ToolShapesOf<A[\"tools\"]>, any>,\n ctx: AgentHookContext,\n ): void | Promise<void> {\n void result;\n void ctx;\n }\n\n /**\n * WHAT HAPPENS WHEN A HOOK THROWS: it is reported and the run carries on.\n *\n * The alternative is to fail the run, and that trade is not close. These\n * hooks are an app's persistence and notification points; the run is a model\n * call the user has already been charged for and whose tools may already have\n * charged a card. Letting a failed `INSERT` in `onMessage` abort a generation\n * mid-sentence loses the answer as well as the row, and the user cannot\n * retry into a better outcome. So the answer survives and the failure is\n * logged.\n *\n * It is logged rather than routed to `onError`: `onError` is itself a hook,\n * and a hook that throws inside the handler for hooks that throw is a loop.\n * Override this to send it somewhere with a pager attached.\n */\n protected reportHookFailure(error: unknown): void {\n console.error(\"[gemi/ai] agent controller hook failed\", error);\n }\n\n private async dispatchEvent(event: AgentStreamEvent, ctx: AgentHookContext): Promise<void> {\n switch (event.type) {\n case \"tool-call\":\n // Skipped while the arguments are still streaming: a hook that fires\n // per token would fire with a half-parsed input, which is worse than\n // firing late. And skipped for a re-sent frame, which carries a parked\n // sub-run's record on a call this hook has already seen — once per call\n // is the contract, and a run that re-parks would otherwise fire it for\n // a call some earlier run made.\n if (!event.part.partial && !event.resent) {\n await this.onToolCall(\n {\n toolCallId: event.part.toolCallId,\n name: String(event.part.name),\n input: event.part.input,\n },\n ctx,\n );\n }\n return;\n case \"awaiting-input\":\n await this.onAwaitingInput(event.pending as PendingToolCall[], ctx);\n return;\n case \"error\":\n await this.onError(event.error, ctx);\n return;\n default:\n return;\n }\n }\n\n /**\n * Runs after the stream is over, whether or not anyone was still watching it.\n *\n * The messages come from `result()` rather than from the event stream because\n * assembling a message out of deltas is the agent's job and doing it twice is\n * how the two copies drift. It also means a stopped run persists the same way\n * a finished one does: `stop()` finalizes the transcript, so by the time this\n * resolves there is a valid history to store.\n *\n * `stored` settles when the transcript is in the store, not when the app is\n * done with it. The next turn on this thread waits on that (see\n * `withThread`), and the hooks are the app's: an `onMessage` that writes to\n * something slow would make every later turn wait for it, once per message\n * of the old run, and one that never settles — which `safely` cannot catch —\n * would hold the thread, and every turn queued on it, behind an open\n * connection each. So the hooks run on after it on their own, reported the\n * same way.\n *\n * `hooks` settles once they have too, and every event-stream hook the run\n * led to (`eventHooks`), or after `hookHoldMs` of them, and is what the\n * request that started the run is held open for. Separate from\n * `stored` for the reason above, and it has to exist from the start: by the\n * time the hooks are called the run has settled, and the request with it,\n * unless something was already holding it. Neither rejects.\n */\n private persistRun(\n run: AgentRun,\n ctx: AgentHookContext,\n eventHooks: Promise<void>,\n ): { stored: Promise<void>; hooks: Promise<void> } {\n // Assigned before `stored` settles, on every path, so `hooks` below reads\n // the chain this run actually started.\n let notified: Promise<void> = Promise.resolve();\n\n const stored = (async () => {\n let result: AgentRunResult<ToolShapes, unknown>;\n try {\n result = await run.result();\n } catch (err) {\n notified = this.safely(() =>\n this.onError(\n {\n code: \"unknown\",\n message: err instanceof Error ? err.message : String(err),\n retryable: false,\n },\n ctx,\n ),\n );\n return;\n }\n\n const messages = result.messages as AgentMessage[];\n\n if (ctx.threadId && messages.length > 0) {\n try {\n await this.store.appendMessages(ctx.threadId, messages);\n } catch (err) {\n this.reportHookFailure(err);\n }\n }\n\n notified = this.notifyRun(result, messages, ctx);\n })();\n\n // The bound starts once the run has settled, not with the run: a long run\n // is not a hung hook. `eventHooks` is in it because the event-stream hooks\n // are chained (see `MemoryLiveRuns.register`): an `onAwaitingInput` queued\n // behind a slow `onToolCall` is called after the run, and after every hook\n // above, and has nothing else to hold the request for it.\n const hooks = stored.then(() =>\n within(\n Promise.all([notified, eventHooks]).then(() => {}),\n this.hookHoldMs,\n ),\n );\n return { stored, hooks };\n }\n\n /** The hooks on a finished run, in order. Never rejects: see `safely`. */\n private async notifyRun(\n result: AgentRunResult<ToolShapes, unknown>,\n messages: AgentMessage[],\n ctx: AgentHookContext,\n ): Promise<void> {\n for (const message of messages) {\n await this.safely(() => this.onMessage(message, ctx));\n }\n\n await this.safely(() => this.onStreamComplete(result as any, ctx));\n }\n\n private async safely(fn: () => void | Promise<void>): Promise<void> {\n try {\n await fn();\n } catch (err) {\n this.reportHookFailure(err);\n }\n }\n}\n\n// --- request plumbing ----------------------------------------------------\n\n/**\n * The keys of the turn envelope itself, which `useChat` puts in the same JSON\n * object as an app's `body`.\n *\n * Listed so `appBody` can take them back out. Sharing one object was the right\n * wire choice — a nested `body` field would be a second envelope for every\n * client, including the Swift and Kotlin ones — but it does mean the app's\n * fields and the framework's are mixed together on arrival, and handing an app\n * `turn` and `messages` would invite reading them. They are this module's to\n * change; an app that depended on their shape would break on a release that\n * never mentioned them.\n */\nconst ENVELOPE_KEYS = [\"turn\", \"clientRunId\", \"threadId\", \"messages\"] as const;\n\n/**\n * The turn's own fields, which `toClientTurn` reads off the top level when the\n * body carries no `turn` object. Reserved in that form and only that form —\n * with an envelope present, a top-level `text` is the app's.\n */\nconst BARE_TURN_KEYS = [\"text\", \"files\", \"toolResults\"] as const;\n\n/**\n * An app's own fields, out of the parsed turn body.\n *\n * A fresh object rather than a `delete` on the parsed one. Nothing reads the\n * body after this today — `turn`, `threadId`, `clientRunId` and `messages` are\n * all pulled out above — so this is not load-bearing and no test pins it. It is\n * how a helper that takes a parsed body should behave regardless: the next\n * reader to move a line above this one should not have to know that the body\n * was quietly hollowed out.\n */\nfunction appBody<Body extends object>(body: Record<string, any>): Body {\n const reserved: readonly string[] = hasTurnEnvelope(body)\n ? ENVELOPE_KEYS\n : [...ENVELOPE_KEYS, ...BARE_TURN_KEYS];\n const extra: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(body)) {\n if (reserved.includes(key)) continue;\n // `JSON.parse` produces `__proto__` as an own property, and `Object.entries`\n // hands it over like any other. Assigning it with `[]` goes through\n // `Object.prototype`'s setter and changes the prototype of the object an app\n // is about to read — from a value the client chose. `defineProperty` writes\n // the key itself, which is what \"the fields the client sent\" means.\n Object.defineProperty(extra, key, {\n value,\n writable: true,\n enumerable: true,\n configurable: true,\n });\n }\n return extra as Body;\n}\n\n/**\n * A body, or the reason there is not one.\n *\n * Deliberately one shape with an optional `error` rather than a discriminated\n * union on `ok`: this package compiles with `strict: false`, and without\n * `strictNullChecks` TypeScript will not narrow a union by a boolean\n * discriminant — `if (!parsed.ok)` leaves `parsed.message` an error. A field\n * that is either set or not needs no narrowing to read.\n */\ntype ParsedBody = {\n body: Record<string, any>;\n error?: string;\n /** Which HTTP error the rejection is. A missing one is the 400. */\n status?: number;\n code?: string;\n};\n\n/**\n * The body, as JSON — or a reason it is not.\n *\n * Read off the raw request rather than through `req.input()`: that path matches\n * `Content-Type` exactly, so `application/json; charset=utf-8` — which several\n * HTTP clients send by default — parses as an empty body, and an agent turn\n * that silently loses its text is a bad way to find that out.\n *\n * Matching the type by prefix is not the same as ignoring it. A body that\n * arrives as anything other than `application/json` is a 415, and the reason\n * is not tidiness: a cross-site `<form enctype=\"text/plain\">` can be made to\n * concatenate its one field into valid JSON, and a JSON parser that reads\n * whatever it is handed turns that form into a turn on someone else's\n * conversation. Insisting on `application/json` is what makes these routes\n * non-simple requests — the browser will not send one cross-origin without a\n * preflight — and that holds whether or not the app's cookies are `SameSite`\n * and whether or not `CSRFMiddleware` is mounted. Only a request that carries a\n * body is held to this; a bodiless POST has no type to check and stays `{}`.\n *\n * The three cases are kept apart deliberately. NO body is a real request — a\n * reattach, or a turn that just lets the model continue — and reads as `{}`. A\n * body that will not parse, or that parses to something other than an object,\n * is a failed request and has to say so: folding it into `{}` made a truncated\n * proxy response or a mis-serialized client indistinguishable from an empty\n * turn, so `stream` billed a model call with no history and no turn and threw\n * the user's actual message away. That presents as the model hallucinating\n * rather than as an error, which is the expensive way to debug it. Pre-flight\n * failures stay ordinary HTTP errors and never reach the event stream.\n *\n * An array counts as malformed even though `typeof [] === \"object\"`: nothing\n * downstream reads a positional body, so `[1,2,3]` could only ever have run as\n * an empty turn.\n */\nasync function readJsonBody(req: HttpRequest<any, any>): Promise<ParsedBody> {\n const raw = req?.rawRequest;\n if (!raw || raw.method === \"GET\" || raw.method === \"HEAD\" || !raw.body) {\n return { body: {} };\n }\n\n if (!isJsonContentType(raw.headers.get(\"Content-Type\"))) {\n // Before the body is read: nothing in a body about to be refused is worth\n // the bytes, and the refusal must not depend on what they were.\n return {\n body: {},\n status: 415,\n code: \"unsupported_media_type\",\n error: \"The request body must be sent as application/json.\",\n };\n }\n\n let text: string;\n try {\n text = await raw.text();\n } catch {\n // A body that stopped arriving mid-flight. Same class of failure as one\n // that arrived truncated, and the same answer.\n return { body: {}, error: \"The request body could not be read.\" };\n }\n\n if (!text.trim()) {\n return { body: {} };\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch {\n return { body: {}, error: \"The request body is not valid JSON.\" };\n }\n\n if (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n return { body: {}, error: \"The request body must be a JSON object.\" };\n }\n\n return { body: parsed as Record<string, any> };\n}\n\n/**\n * Media types compare case-insensitively and may carry parameters, so the\n * comparison is on the lowercased type with its parameters cut off, and\n * `Application/JSON; charset=utf-8` passes. It is an equality on that type\n * rather than a prefix, so `application/json-seq` and the other types that\n * merely begin with those bytes do not. None of the three types a browser will\n * send without a preflight — `text/plain`, `application/x-www-form-urlencoded`,\n * `multipart/form-data` — does either, and neither does no type at all, which\n * is what a hand-written `fetch` with a string body and no header ends up\n * sending as `text/plain`.\n */\nfunction isJsonContentType(value: string | null): boolean {\n if (typeof value !== \"string\") return false;\n const [type] = value.split(\";\", 1);\n return type.trim().toLowerCase() === \"application/json\";\n}\n\nfunction invalidRequest(parsed: ParsedBody): Response {\n return jsonResponse(parsed.status ?? 400, {\n code: parsed.code ?? \"invalid_request\",\n message: parsed.error,\n });\n}\n\n/**\n * Whether the turn arrived in a `turn` object of its own, rather than spread\n * across the top level of the body.\n *\n * Both forms are accepted, and which one it is decides what belongs to the app:\n * a top-level `text` is the user's message in the bare form and an app's own\n * field in the enveloped one. `toClientTurn` and `appBody` have to agree about\n * that, so they ask the same question here rather than each testing it.\n */\nfunction hasTurnEnvelope(body: Record<string, any>): boolean {\n return Boolean(body.turn) && typeof body.turn === \"object\";\n}\n\n/**\n * The client's turn, accepting both `{ turn: {...} }` and the flattened\n * `{ text, files, toolResults }` — the second is what a hand-written `fetch`\n * writes, and refusing it buys nothing.\n *\n * `turn` is `undefined` for an empty turn, which is a real request:\n * reattaching to a conversation and letting the model continue is a turn with\n * nothing in it. `error` is a turn that is refused with a 400 before anything\n * runs — see `toTurnFiles`.\n */\nfunction toClientTurn(body: Record<string, any>): { turn?: ClientTurn; error?: string } {\n const source = hasTurnEnvelope(body) ? body.turn : body;\n const turn: ClientTurn = {};\n if (typeof source.text === \"string\") {\n turn.text = source.text;\n }\n if (Array.isArray(source.files)) {\n const files = toTurnFiles(source.files);\n if (typeof files === \"string\") {\n return { error: files };\n }\n turn.files = files;\n }\n if (Array.isArray(source.toolResults)) {\n turn.toolResults = source.toolResults;\n }\n return Object.keys(turn).length > 0 ? { turn } : {};\n}\n\n/**\n * `turn.files`, checked field by field, or the sentence refusing it.\n *\n * REFUSED, NOT FILTERED. Every entry becomes a `FilePart` in a user message,\n * and on a thread that message is stored before the provider sees it. An entry\n * the request builder cannot send — no id at all, or a gemi id in `fileId` —\n * then throws from `toResponsesInput` on this turn and on every turn after it,\n * because every later turn re-sends the history: one bad upload bricks the\n * thread. Dropping the entry instead is quieter and worse — the model answers\n * about a file it was never given, with nothing saying so. A 400 here is the\n * one place the client can still do something about it.\n *\n * Scoping is what makes a foreign `attachmentId` harmless (`ScopedAttachments`\n * answers the same not-found for another tenant's id as for an invented one),\n * so the id is not signed or looked up here. What this checks is the shape a\n * crash would come from: strings where strings go, the prefix on\n * `attachmentId`, at least one id. Unknown fields are dropped rather than\n * refused, since `attach()` answers more than a `FilePart` holds (`downgraded`)\n * and passing its answer on whole is the documented use. `null` and `\"\"` read\n * as absent, for every field: `null` is what a serializer that writes\n * `undefined` as `null` means, and `\"\"` is what a form field left blank sends.\n * Neither can be an id, so refusing `attachmentId: \"\"` for its prefix would\n * turn a blank into an error the entry's `fileId` does not deserve.\n */\nfunction toTurnFiles(raw: unknown[]): NonNullable<ClientTurn[\"files\"]> | string {\n const files: NonNullable<ClientTurn[\"files\"]> = [];\n for (const [index, entry] of raw.entries()) {\n const where = `turn.files[${index}]`;\n if (!entry || typeof entry !== \"object\" || Array.isArray(entry)) {\n return `${where} must be an object.`;\n }\n const fields: Record<string, string> = {};\n for (const key of [\"fileId\", \"attachmentId\", \"name\", \"mimeType\"] as const) {\n const value = (entry as Record<string, unknown>)[key];\n if (value === undefined || value === null || value === \"\") continue;\n if (typeof value !== \"string\") {\n return `${where}.${key} must be a string.`;\n }\n fields[key] = value;\n }\n const { fileId, attachmentId, name, mimeType } = fields;\n if (fileId?.startsWith(ATTACHMENT_ID_PREFIX)) {\n return `${where}.fileId holds a gemi attachment id (${fileId}). \\`fileId\\` is the provider's id from the upload response; put this one in \\`attachmentId\\`.`;\n }\n if (attachmentId !== undefined && !attachmentId.startsWith(ATTACHMENT_ID_PREFIX)) {\n return `${where}.attachmentId is not a gemi attachment id: it must start with \"${ATTACHMENT_ID_PREFIX}\".`;\n }\n if (!fileId && !attachmentId) {\n return `${where} has neither a \\`fileId\\` nor an \\`attachmentId\\`. Send the ids the upload response answered.`;\n }\n files.push({\n ...(fileId ? { fileId } : {}),\n ...(attachmentId ? { attachmentId } : {}),\n ...(name !== undefined ? { name } : {}),\n ...(mimeType !== undefined ? { mimeType } : {}),\n });\n }\n return files;\n}\n\n/**\n * Where to resume from, or `undefined` for \"wherever you still can\".\n *\n * An explicit `from` wins, because a client that tracked its own position knows\n * something the transport does not. Otherwise `Last-Event-ID` — the browser\n * sends it on its own when an `EventSource` reconnects, and the frames put\n * `seq` in `id:` precisely so that header is already the right question. It\n * names the last event *received*, so the resume point is one past it.\n *\n * With neither, the answer is `undefined` and NOT 0. A client reattaching on\n * mount is a fresh POST from a page that has just loaded: no `Last-Event-ID`,\n * and no cursor of its own, because the only handle it was given is the\n * `threadId`. Reading that as \"resume from frame 0\" asked `replay` for a frame\n * every run longer than `maxFrames` has already evicted, so the mount-time\n * reattach — the one case `/attach` exists for — 410'd on exactly the long runs\n * it was meant to rescue, and the documented remedy of reloading the thread\n * does not help because the store is only written at run end. `undefined` means\n * the tail, which is all such a client can use anyway.\n */\nfunction resolveCursor(req: HttpRequest<any, any>, body: Record<string, any>): number | undefined {\n if (typeof body.from === \"number\" && Number.isFinite(body.from)) {\n return Math.max(0, Math.floor(body.from));\n }\n // `cursor` is what `useChat` actually sends, and it counts the other way:\n // `from` is the frame to resume AT, `cursor` the last frame the client\n // APPLIED — the same convention as `Last-Event-ID`, and the same `+ 1`. A\n // client that has applied nothing sends -1, which is not a position but the\n // absence of one, so it falls through to the tail rather than asking for\n // frame 0 and 410ing on precisely the long runs `/attach` exists to rescue.\n if (typeof body.cursor === \"number\" && Number.isFinite(body.cursor)) {\n const applied = Math.floor(body.cursor);\n if (applied >= 0) {\n return applied + 1;\n }\n return undefined;\n }\n const header = req?.rawRequest?.headers?.get(\"Last-Event-ID\");\n if (header) {\n const seq = Number.parseInt(header, 10);\n if (Number.isFinite(seq)) {\n return Math.max(0, seq + 1);\n }\n }\n return undefined;\n}\n\nfunction searchParam(req: HttpRequest<any, any>, key: string): string | undefined {\n const value = req?.search?.get(key);\n return typeof value === \"string\" ? value : undefined;\n}\n\n/** A turn `/stop` ended while it waited, before anything ran or was charged. */\nfunction stoppedBeforeStart(): Response {\n return jsonResponse(409, {\n code: \"stopped\",\n message: \"The turn was stopped before it started.\",\n });\n}\n\nfunction jsonResponse(status: number, error: Record<string, unknown>): Response {\n return new Response(JSON.stringify({ error }), {\n status,\n headers: { \"Content-Type\": \"application/json\" },\n });\n}\n\n// --- attachments ---------------------------------------------------------\n\n/**\n * Reads the client's `destination` field and applies it to the app's policy.\n *\n * THE ONLY THING A CLIENT MAY ASK FOR IS \"DO NOT SEND THIS TO THE VENDOR\", and\n * that is the entire permitted vocabulary: a `storage` hint under a `both`\n * policy, or a hint that agrees with the policy and changes nothing. Everything\n * else throws.\n *\n * The rule here used to be \"a hint may narrow, never widen\", counting\n * destinations, and counting is what made it wrong in one direction.\n * `both → provider` is fewer destinations and reads as narrowing, but what it\n * removes is *gemi's own copy*, while the third party still gets the file. That\n * is not a client declining exposure, it is a client overriding the app's\n * retention decision from a field in a multipart body: an app that keeps every\n * upload because a tool has to forward it — or because an auditor asked — loses\n * the copy, mints no `attachmentId`, records nothing, and gets no warning\n * either, since the bytes did reach the provider exactly as policy said. The\n * two directions are not alike, because only one of them reduces who ends up\n * holding the file.\n *\n * What is left is defensible on its own terms: `storage` keeps bytes off a\n * vendor the app was willing to send them to, which is a thing the client is\n * entitled to ask for and which cannot hurt anyone if the client is lying. A UI\n * that knows this CSV exists to be imported and will never be read by the model\n * is the case that makes a hint worth having at all. The policy hook is the\n * ceiling; the hint may only take the vendor out from under it.\n *\n * A hint the policy does not permit throws rather than being ignored. Ignoring\n * it is the shape of this bug that never gets found: the client believes it kept\n * a file off the vendor, the file went anyway, and nothing anywhere says so.\n */\nfunction narrowDestination(\n policy: AttachmentDestination,\n hint: FormDataEntryValue | null,\n): AttachmentDestination {\n if (typeof hint !== \"string\" || hint.length === 0) {\n return policy;\n }\n if (hint !== \"both\" && hint !== \"provider\" && hint !== \"storage\") {\n throw new Error(\n `Unknown attachment destination \"${hint}\". Expected \"both\", \"provider\" or \"storage\".`,\n );\n }\n if (hint === policy || (policy === \"both\" && hint === \"storage\")) {\n return hint;\n }\n if (policy === \"both\" && hint === \"provider\") {\n throw new Error(\n 'This upload asked for the \"provider\" destination under a \"both\" policy. A client may ask for \"storage\", which keeps a file away from the model provider, but it may not ask the server to stop keeping its own copy while the vendor still gets the file — that is the app\\'s retention decision. Change `attachmentDestination()` if this file should not be kept.',\n );\n }\n throw new Error(\n `This upload asked for the \"${hint}\" destination, but the server's policy for it is \"${policy}\", and a client hint may only ask for \"storage\" under a \"both\" policy. Change \\`attachmentDestination()\\` if the file really should go there.`,\n );\n}\n\n/**\n * Said once per process, not once per upload.\n *\n * The condition is a property of how the app is wired — an agent route with no\n * authentication and no `threadId` on its uploads — so it is either true for\n * every request or false for every request. Logging it per request would put a\n * line in the log for every file anyone ever uploads, which is how a warning\n * stops being read.\n */\nlet warnedAboutUnscopedUpload = false;\nfunction warnUnscopedUploadOnce(): void {\n if (warnedAboutUnscopedUpload) return;\n warnedAboutUnscopedUpload = true;\n console.warn(\n \"[gemi/ai] An upload had no attachment scope, so its bytes were sent to the model provider only and no attachment id was minted — a tool cannot forward this file. Guard the route with auth, send a `threadId` with the upload, or override `attachmentScope()`.\",\n );\n}\n\n// --- routing -------------------------------------------------------------\n//\n// `agent()` itself belongs on ApiRouter next to `resource()`, which is the\n// method it works like: one call, several routes, all of them the controller's.\n// The types are declared here because they are the ai module's contract, not\n// the router's.\n//\n// class Api extends ApiRouter {\n// routes = {\n// \"/chat\": this.agent(ChatAgentController),\n// };\n// }\n//\n// POST /chat → stream every client turn\n// POST /chat/attach → attach reattach to a run in progress\n// POST /chat/stop → stop explicit cancel\n// POST /chat/files → upload attachments\n//\n// Mounting under a single path is what gives the client one key to name. The\n// agent's tool types ride along on `AgentRoute`, so `useChat(\"/chat\")` gets them\n// out of the existing `RPC` interface — no second augmentation to generate, and\n// renaming the route moves the client key with it.\n\nexport type AgentRouteMethod = \"stream\" | \"attach\" | \"stop\" | \"upload\";\n\nexport type AgentMiddlewareConfig = Partial<Record<AgentRouteMethod, MiddlewareInput>>;\n\nexport type AgentRoute<T extends new () => AgentController<any, any>> = {\n __internal_brand: \"AgentRoute\";\n controller: T;\n middleware(config: AgentMiddlewareConfig): AgentRoute<T>;\n};\n\n/** What `CreateRPC` should produce for an agent route: enough for the client to\n * type its messages, and nothing that drags server code into the bundle. */\nexport type AgentRouteRPC<T extends new () => AgentController<any, any>> = {\n __agent: true;\n tools: InstanceType<T>[\"agent\"] extends AnyAgent\n ? ToolShapesOf<InstanceType<T>[\"agent\"][\"tools\"]>\n : ToolShapes;\n output: unknown;\n /**\n * The controller's `Body`, so `useChat` can check what it sends against what\n * the controller declared.\n *\n * Read off the phantom key rather than by matching the class, which is what\n * made this fail: `Body` used to reach the instance type only through\n * `instructions`'s second parameter, and a controller overriding\n * `instructions(req)` — the pre-existing signature, so every existing\n * override — gave inference nothing to work with and got the open record.\n */\n body: InstanceType<T> extends { [BODY]?: infer B } ? B : Record<string, unknown>;\n};\n\n/** The longest delay `setTimeout` keeps; past it the runtime fires in ~1ms. */\nconst MAX_TIMEOUT_MS = 2 ** 31 - 1;\n\n/**\n * Settles when `work` does or after `ms`, whichever is first, and never\n * rejects. For how long the request waits on the app's code, not for the code\n * itself, which runs on either way.\n *\n * An `ms` a timer cannot hold — `Infinity` is what \"no bound\" reads as — is\n * no bound. Handed to `setTimeout` it would fire at once, and the request\n * would end before the hooks it was raised for.\n */\nfunction within(work: Promise<void>, ms: number): Promise<void> {\n const settled = work.then(\n () => {},\n () => {},\n );\n if (!(ms <= MAX_TIMEOUT_MS)) {\n return settled;\n }\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timeout = new Promise<void>((resolve) =>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