agents 0.26.0 → 0.27.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 (233) hide show
  1. package/README.md +8 -9
  2. package/dist/{agent-routing-0DR0Tk2v.d.ts → agent-routing-Bhdbvf7s.d.ts} +13 -8
  3. package/dist/agent-routing.d.ts +1 -1
  4. package/dist/agent-tool-types.d.ts +1 -1
  5. package/dist/{agent-tools-YrATZJu2.d.ts → agent-tools-bJZwBMfq.d.ts} +2 -2
  6. package/dist/agent-tools.d.ts +1 -1
  7. package/dist/ai-sdk-OoGkFy3s.js +2444 -0
  8. package/dist/ai-sdk-OoGkFy3s.js.map +1 -0
  9. package/dist/apply-chunks-1FheRb9W.js +99 -0
  10. package/dist/apply-chunks-1FheRb9W.js.map +1 -0
  11. package/dist/broadcast-state-CEyI4uAK.js +69 -0
  12. package/dist/broadcast-state-CEyI4uAK.js.map +1 -0
  13. package/dist/browser/ai-sdk.d.ts +1 -1
  14. package/dist/browser/ai-sdk.js +2 -2
  15. package/dist/browser/ai-sdk.js.map +1 -1
  16. package/dist/browser/index.d.ts +1 -1
  17. package/dist/browser/tanstack-ai.d.ts +1 -1
  18. package/dist/browser/tanstack-ai.js +1 -1
  19. package/dist/{browser-DE3d-RLk.d.ts → browser-tmkcAAI3.d.ts} +2 -2
  20. package/dist/{browser-tool-DuNdlwZ9.d.ts → browser-tool-Bn-nzJQI.d.ts} +3 -3
  21. package/dist/{browser-tool-CQ10e6Kj.js → browser-tool-CBqs6hqa.js} +58 -18
  22. package/dist/browser-tool-CBqs6hqa.js.map +1 -0
  23. package/dist/capability-B4WbF81e.js.map +1 -1
  24. package/dist/{capability-runner-DBAnGr6L.d.ts → capability-runner-O5VbQzqg.d.ts} +9 -1
  25. package/dist/channel-Bak_6zDP.d.ts +333 -0
  26. package/dist/chat/index.d.ts +11 -9
  27. package/dist/chat/index.js +5 -194
  28. package/dist/chat/index.js.map +1 -1
  29. package/dist/chat/react.d.ts +2 -4
  30. package/dist/chat/react.js +3 -1
  31. package/dist/chat/react.js.map +1 -1
  32. package/dist/chat-sdk/index.d.ts +1 -1
  33. package/dist/chat-sdk/index.js +1 -1
  34. package/dist/cli.js +144 -0
  35. package/dist/{client-fyQrLGkD.js → client-1tEQ1Oqz.js} +27 -2
  36. package/dist/{client-fyQrLGkD.js.map → client-1tEQ1Oqz.js.map} +1 -1
  37. package/dist/{client-je3koD9Z.d.ts → client-DS3Dz9l0.d.ts} +2 -2
  38. package/dist/client-tools-aIBO0Fk7.d.ts +53 -0
  39. package/dist/client.d.ts +1 -1
  40. package/dist/conversations-DH0ut_8n.d.ts +192 -0
  41. package/dist/{current-agent-vAk_5JMt.d.ts → current-agent-C07C5o5Y.d.ts} +3 -3
  42. package/dist/daemon-core-Buubj-y1.js +481 -0
  43. package/dist/daemon-core-Buubj-y1.js.map +1 -0
  44. package/dist/daemon-core-CbohVPAu.d.ts +132 -0
  45. package/dist/{do-oauth-client-provider-Tmf1vgKz.d.ts → do-oauth-client-provider-DfsUcpc3.d.ts} +7 -1
  46. package/dist/{durable-object-lifecycle-HhgOMWq9.d.ts → durable-object-lifecycle-DRqp475_.d.ts} +2 -2
  47. package/dist/{channels/email.d.ts → experimental/channels/email/index.d.ts} +25 -12
  48. package/dist/{channels/email.js → experimental/channels/email/index.js} +13 -35
  49. package/dist/experimental/channels/email/index.js.map +1 -0
  50. package/dist/experimental/channels/index.d.ts +270 -0
  51. package/dist/experimental/channels/index.js +1186 -0
  52. package/dist/experimental/channels/index.js.map +1 -0
  53. package/dist/{channels/slack.d.ts → experimental/channels/slack/index.d.ts} +19 -9
  54. package/dist/experimental/channels/slack/index.js +267 -0
  55. package/dist/experimental/channels/slack/index.js.map +1 -0
  56. package/dist/{channels/telegram.d.ts → experimental/channels/telegram/index.d.ts} +19 -9
  57. package/dist/experimental/channels/telegram/index.js +198 -0
  58. package/dist/experimental/channels/telegram/index.js.map +1 -0
  59. package/dist/experimental/channels/web/ai-sdk.d.ts +55 -0
  60. package/dist/experimental/channels/web/ai-sdk.js +266 -0
  61. package/dist/experimental/channels/web/ai-sdk.js.map +1 -0
  62. package/dist/experimental/channels/web/client.d.ts +97 -0
  63. package/dist/experimental/channels/web/client.js +349 -0
  64. package/dist/experimental/channels/web/client.js.map +1 -0
  65. package/dist/experimental/channels/web/index.d.ts +189 -0
  66. package/dist/experimental/channels/web/index.js +332 -0
  67. package/dist/experimental/channels/web/index.js.map +1 -0
  68. package/dist/gateway-BDC07gYO.d.ts +100 -0
  69. package/dist/handle-zAzh0UW2.d.ts +460 -0
  70. package/dist/harness/ai-sdk/index.d.ts +175 -0
  71. package/dist/harness/ai-sdk/index.js +605 -0
  72. package/dist/harness/ai-sdk/index.js.map +1 -0
  73. package/dist/harness/container/daemon/index.d.ts +76 -0
  74. package/dist/harness/container/daemon/index.js +3 -0
  75. package/dist/harness/container/index.d.ts +740 -0
  76. package/dist/harness/container/index.js +2152 -0
  77. package/dist/harness/container/index.js.map +1 -0
  78. package/dist/harness/container/runtime/index.d.ts +144 -0
  79. package/dist/harness/container/runtime/index.js +1082 -0
  80. package/dist/harness/container/runtime/index.js.map +1 -0
  81. package/dist/harness/opencode/index.d.ts +369 -0
  82. package/dist/harness/opencode/index.js +1072 -0
  83. package/dist/harness/opencode/index.js.map +1 -0
  84. package/dist/harness/pi/index.d.ts +2 -1
  85. package/dist/harness/store/index.d.ts +24 -0
  86. package/dist/harness/store/index.js +2 -0
  87. package/dist/harness/think/index.d.ts +821 -0
  88. package/dist/harness/think/index.js +2529 -0
  89. package/dist/harness/think/index.js.map +1 -0
  90. package/dist/harness-B6B9S3rb.d.ts +203 -0
  91. package/dist/{images-iiTDSyRu.js → images-Ce9SCu3X.js} +2 -248
  92. package/dist/images-Ce9SCu3X.js.map +1 -0
  93. package/dist/{index-BreL0YrQ.d.ts → index-D0A5tbWu.d.ts} +2 -2
  94. package/dist/index-Ib_4dwOo.d.ts +200 -0
  95. package/dist/index.d.ts +5 -5
  96. package/dist/index.js +2 -2
  97. package/dist/ingress-EFbrS547.js +9 -0
  98. package/dist/ingress-EFbrS547.js.map +1 -0
  99. package/dist/input-schema-DNrpTu49.js +23 -0
  100. package/dist/input-schema-DNrpTu49.js.map +1 -0
  101. package/dist/internal-kmw70xwR.js +46 -0
  102. package/dist/internal-kmw70xwR.js.map +1 -0
  103. package/dist/internal_context.d.ts +1 -1
  104. package/dist/lifecycle/index.d.ts +3 -3
  105. package/dist/mcp/client/do-oauth-client-provider.d.ts +1 -1
  106. package/dist/mcp/client/do-oauth-client-provider.js +9 -0
  107. package/dist/mcp/client/do-oauth-client-provider.js.map +1 -1
  108. package/dist/mcp/client/index.d.ts +1 -1
  109. package/dist/mcp/client/index.js +1 -1
  110. package/dist/mcp/client/x402.d.ts +35 -4
  111. package/dist/mcp/client/x402.js +24 -3
  112. package/dist/mcp/client/x402.js.map +1 -1
  113. package/dist/mcp/index.d.ts +1 -1
  114. package/dist/mcp/index.js +2 -2
  115. package/dist/models/ai-sdk/index.d.ts +16 -14
  116. package/dist/models/ai-sdk/index.js +1 -2442
  117. package/dist/models/opencode/index.d.ts +65 -0
  118. package/dist/models/opencode/index.js +285 -0
  119. package/dist/models/opencode/index.js.map +1 -0
  120. package/dist/models/pi-ai/index.d.ts +9 -7
  121. package/dist/models/pi-ai/index.js +2 -1
  122. package/dist/models/pi-ai/index.js.map +1 -1
  123. package/dist/options-pwTNq9z6.d.ts +134 -0
  124. package/dist/protocol-BEgrJYC5.d.ts +331 -0
  125. package/dist/protocol-CukuI73M.d.ts +386 -0
  126. package/dist/protocol-cOklVBBD.js +227 -0
  127. package/dist/protocol-cOklVBBD.js.map +1 -0
  128. package/dist/{protocol-kHsr-f0X.js → protocol-eXeJxhgu.js} +2 -67
  129. package/dist/protocol-eXeJxhgu.js.map +1 -0
  130. package/dist/protocol-y2sSY584.js +148 -0
  131. package/dist/protocol-y2sSY584.js.map +1 -0
  132. package/dist/queue/index.d.ts +1 -1
  133. package/dist/react.d.ts +2 -2
  134. package/dist/routing/index.d.ts +2 -2
  135. package/dist/{scheduler-RdACNXDP.d.ts → scheduler-CrFCnv_J.d.ts} +2 -2
  136. package/dist/schedules/index.d.ts +1 -1
  137. package/dist/serializable.d.ts +1 -1
  138. package/dist/sessions/index.d.ts +11 -434
  139. package/dist/sessions/index.js +1 -2271
  140. package/dist/sessions-B9pNuxdT.js +2272 -0
  141. package/dist/sessions-B9pNuxdT.js.map +1 -0
  142. package/dist/settings-C45hDliX.js +253 -0
  143. package/dist/settings-C45hDliX.js.map +1 -0
  144. package/dist/{transport-PfFnt-Ld.d.ts → settings-NgGwF7Qt.d.ts} +14 -208
  145. package/dist/{src-BZeu-SCD.js → src-DgCbZtIR.js} +4 -4
  146. package/dist/{src-BZeu-SCD.js.map → src-DgCbZtIR.js.map} +1 -1
  147. package/dist/stall-watchdog-BpRF2uwG.js +195 -0
  148. package/dist/stall-watchdog-BpRF2uwG.js.map +1 -0
  149. package/dist/state/index.d.ts +1 -1
  150. package/dist/store-DLeqKuj3.d.ts +310 -0
  151. package/dist/store-vgcPYo8K.js +432 -0
  152. package/dist/store-vgcPYo8K.js.map +1 -0
  153. package/dist/streams/index.d.ts +1 -1
  154. package/dist/streams/index.js +1 -1
  155. package/dist/{streams-DW-UCOUc.js → streams-CITClVer.js} +16 -3
  156. package/dist/streams-CITClVer.js.map +1 -0
  157. package/dist/{streams-Cmkj9QfW.d.ts → streams-fbG61qM8.d.ts} +2 -2
  158. package/dist/sub-routing.d.ts +1 -1
  159. package/dist/tasks/index.d.ts +1 -1
  160. package/dist/tasks/index.js +1 -1
  161. package/dist/{tasks-68DESxf0.js → tasks-CkTZNaV1.js} +2 -1
  162. package/dist/tasks-CkTZNaV1.js.map +1 -0
  163. package/dist/{tasks-9YxYtByh.d.ts → tasks-PtlB4Uyn.d.ts} +2 -2
  164. package/dist/tool-3PA8xjB_.js +421 -0
  165. package/dist/tool-3PA8xjB_.js.map +1 -0
  166. package/dist/tool-BPJEbwaX.d.ts +278 -0
  167. package/dist/transport-k2mFrtL8.d.ts +200 -0
  168. package/dist/turns-DCV3ycDl.js +317 -0
  169. package/dist/turns-DCV3ycDl.js.map +1 -0
  170. package/dist/voice/client.js +1 -1
  171. package/dist/voice/index.d.ts +2 -2
  172. package/dist/voice/index.js +1 -1
  173. package/dist/websearch/index.d.ts +58 -0
  174. package/dist/websearch/index.js +2 -0
  175. package/dist/websearch/tools/ai-sdk.d.ts +77 -0
  176. package/dist/websearch/tools/ai-sdk.js +46 -0
  177. package/dist/websearch/tools/ai-sdk.js.map +1 -0
  178. package/dist/websearch/tools/pi.d.ts +82 -0
  179. package/dist/websearch/tools/pi.js +89 -0
  180. package/dist/websearch/tools/pi.js.map +1 -0
  181. package/dist/websearch/tools/tanstack-ai.d.ts +71 -0
  182. package/dist/websearch/tools/tanstack-ai.js +46 -0
  183. package/dist/websearch/tools/tanstack-ai.js.map +1 -0
  184. package/dist/websockets/index.d.ts +12 -296
  185. package/dist/websockets/index.js +1 -1
  186. package/dist/{websockets-Ci-LZFHH.js → websockets-CmENGb-Z.js} +18 -4
  187. package/dist/websockets-CmENGb-Z.js.map +1 -0
  188. package/dist/{wire-types-CrjKvDyi.d.ts → wire-types-Cv0iwjk5.d.ts} +3 -51
  189. package/dist/workflow-types.d.ts +1 -1
  190. package/dist/workflows.d.ts +1 -1
  191. package/dist/workflows.js +1 -1
  192. package/docs/browse-the-web.md +3 -0
  193. package/docs/harnesses/think.md +215 -0
  194. package/docs/index.md +2 -1
  195. package/docs/lifecycle.md +40 -0
  196. package/docs/search-the-web.md +133 -0
  197. package/package.json +117 -38
  198. package/dist/browser-tool-CQ10e6Kj.js.map +0 -1
  199. package/dist/channel-Bnm4S7T2.d.ts +0 -491
  200. package/dist/channels/ai-sdk.d.ts +0 -46
  201. package/dist/channels/ai-sdk.js +0 -120
  202. package/dist/channels/ai-sdk.js.map +0 -1
  203. package/dist/channels/email.js.map +0 -1
  204. package/dist/channels/index.d.ts +0 -233
  205. package/dist/channels/index.js +0 -608
  206. package/dist/channels/index.js.map +0 -1
  207. package/dist/channels/slack.js +0 -614
  208. package/dist/channels/slack.js.map +0 -1
  209. package/dist/channels/tanstack-ai.d.ts +0 -39
  210. package/dist/channels/tanstack-ai.js +0 -17
  211. package/dist/channels/tanstack-ai.js.map +0 -1
  212. package/dist/channels/telegram.js +0 -427
  213. package/dist/channels/telegram.js.map +0 -1
  214. package/dist/channels/voice.d.ts +0 -45
  215. package/dist/channels/voice.js +0 -122
  216. package/dist/channels/voice.js.map +0 -1
  217. package/dist/images-iiTDSyRu.js.map +0 -1
  218. package/dist/index-BB0kqhIz.d.ts +0 -101
  219. package/dist/ingress-BfetZbMO.js +0 -83
  220. package/dist/ingress-BfetZbMO.js.map +0 -1
  221. package/dist/internal-CYlgHl1l.js +0 -59
  222. package/dist/internal-CYlgHl1l.js.map +0 -1
  223. package/dist/models/ai-sdk/index.js.map +0 -1
  224. package/dist/protocol-kHsr-f0X.js.map +0 -1
  225. package/dist/sessions/index.js.map +0 -1
  226. package/dist/streams-DW-UCOUc.js.map +0 -1
  227. package/dist/surface-bZZJqBka.js +0 -17
  228. package/dist/surface-bZZJqBka.js.map +0 -1
  229. package/dist/tasks-68DESxf0.js.map +0 -1
  230. package/dist/tool-schema-CBjGPrsQ.js +0 -31
  231. package/dist/tool-schema-CBjGPrsQ.js.map +0 -1
  232. package/dist/websockets-Ci-LZFHH.js.map +0 -1
  233. package/docs/channels.md +0 -323
@@ -0,0 +1 @@
1
+ {"version":3,"file":"images-Ce9SCu3X.js","names":["isRecord","isRecord","stripEchoSuffix"],"sources":["../src/models/core/errors.ts","../src/models/core/transport.ts","../src/models/core/gateway-providers.ts","../src/models/core/chat-completions/quirks.ts","../src/models/core/chat-completions/request.ts","../src/models/core/chat-completions/response.ts","../src/models/core/chat-completions/stream.ts","../src/models/core/sse.ts","../src/models/core/images.ts"],"sourcesContent":["/**\n * Coarse classification of a failed Cloudflare AI call.\n *\n * @experimental This surface is experimental and may change.\n */\nexport type CloudflareAIErrorCode =\n | \"auth\"\n | \"rate-limit\"\n | \"not-found\"\n | \"bad-request\"\n | \"provider-error\"\n | \"gateway-error\"\n | \"unknown\";\n\n/**\n * One leg of a {@link CloudflareAIError} fallback chain.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface CloudflareAIAttempt {\n /** The model id that was tried. */\n model: string;\n /** The error that leg failed with. */\n error: unknown;\n}\n\n/**\n * Error thrown for every failed Cloudflare AI call, whether it came back as a\n * non-2xx `Response` or as a thrown Workers AI binding error. Framework\n * modules wrap it into their own error type at their boundary (the AI SDK\n * module into an `APICallError`, so `ai`'s retry loop honours\n * {@link CloudflareAIError.isRetryable}).\n *\n * @experimental This surface is experimental and may change.\n */\nexport class CloudflareAIError extends Error {\n /** HTTP status, when the failure carried one. */\n readonly status: number | undefined;\n /** Coarse classification used for routing and fallback decisions. */\n readonly code: CloudflareAIErrorCode;\n /** `cf-aig-log-id` of the failed request, when the response carried one. */\n readonly logId: string | undefined;\n /** The model id that produced this error. */\n readonly model: string;\n /** Which binding path the request went down, for error reporting. */\n readonly url: string;\n /** The request body that failed. */\n readonly requestBodyValues: unknown;\n /** Response headers, when there was a response. */\n readonly responseHeaders: Record<string, string> | undefined;\n /** Raw response body text, when there was a response. */\n readonly responseBody: string | undefined;\n /** Whether a retry is worth attempting. */\n readonly isRetryable: boolean;\n /** Parsed error envelope, or other structured detail. */\n readonly data: unknown;\n /** Every leg tried, when this error ends a fallback chain. */\n readonly attempts: CloudflareAIAttempt[] | undefined;\n override readonly cause: unknown;\n\n constructor(options: {\n message: string;\n model: string;\n url: string;\n requestBodyValues: unknown;\n status?: number;\n code: CloudflareAIErrorCode;\n logId?: string;\n responseHeaders?: Record<string, string>;\n responseBody?: string;\n isRetryable?: boolean;\n data?: unknown;\n cause?: unknown;\n attempts?: CloudflareAIAttempt[];\n }) {\n super(options.message);\n this.name = \"CloudflareAIError\";\n this.status = options.status;\n this.code = options.code;\n this.logId = options.logId;\n this.model = options.model;\n this.url = options.url;\n this.requestBodyValues = options.requestBodyValues;\n this.responseHeaders = options.responseHeaders;\n this.responseBody = options.responseBody;\n this.isRetryable = options.isRetryable ?? isRetryableStatus(options.status);\n this.data = options.data;\n this.cause = options.cause;\n this.attempts = options.attempts;\n }\n}\n\n/**\n * Whether a status is worth retrying. Missing status means a transport-level\n * failure (connection reset, DNS, abort-adjacent), which is retryable.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function isRetryableStatus(status: number | undefined): boolean {\n if (status === undefined) return true;\n return status === 408 || status === 409 || status === 429 || status >= 500;\n}\n\n/**\n * Classifies an HTTP status into a {@link CloudflareAIErrorCode}. The\n * `AiGatewayError` envelope name catches gateway-level statuses that are not\n * otherwise meaningful (notably 402 \"insufficient balance\").\n */\nexport function classifyStatus(\n status: number | undefined,\n envelopeName?: string\n): CloudflareAIErrorCode {\n if (status === 401 || status === 403) return \"auth\";\n if (status === 429) return \"rate-limit\";\n if (status === 404) return \"not-found\";\n if (status === 400 || status === 422) return \"bad-request\";\n if (status !== undefined && status >= 500) return \"provider-error\";\n if (envelopeName === \"AiGatewayError\") return \"gateway-error\";\n return \"unknown\";\n}\n\n/**\n * Workers AI binding error codes that map onto an HTTP status.\n *\n * @see https://developers.cloudflare.com/workers-ai/platform/errors/\n */\nconst BINDING_CODE_STATUS: Record<number, number> = {\n 3003: 400,\n 3006: 413,\n 3007: 408,\n 3008: 408,\n 3023: 403,\n 3036: 429,\n 3039: 400,\n 3040: 429,\n 3041: 403,\n 3042: 404,\n 5004: 400,\n 5005: 405,\n 5007: 400,\n 5016: 403,\n 5018: 403,\n 5019: 405\n};\n\n/** Abort-shaped errors must propagate untouched so callers can detect them. */\nexport function isAbortError(error: unknown): boolean {\n const name =\n error instanceof Error || error instanceof DOMException\n ? error.name\n : undefined;\n return (\n name === \"AbortError\" ||\n name === \"ResponseAborted\" ||\n name === \"TimeoutError\"\n );\n}\n\nfunction messageOf(error: unknown): string {\n if (typeof error === \"string\") return error;\n if (error instanceof Error) return error.message;\n if (error !== null && typeof error === \"object\" && \"message\" in error) {\n const message = (error as { message: unknown }).message;\n if (typeof message === \"string\") return message;\n }\n return String(error);\n}\n\n/**\n * Extracts a Workers AI internal error code from a thrown binding error. The\n * binding reports codes inside the message text (`\"3040: Capacity temporarily\n * exceeded\"`) rather than as a status, so only known codes are accepted — an\n * unrelated leading number must not be misread as a code.\n */\nfunction bindingErrorCode(error: unknown): number | undefined {\n if (error !== null && typeof error === \"object\" && \"code\" in error) {\n const code = (error as { code: unknown }).code;\n if (typeof code === \"number\" && code in BINDING_CODE_STATUS) return code;\n }\n const matches = messageOf(error).matchAll(/\\b(\\d{3,5})\\s*:/g);\n for (const match of matches) {\n const code = Number(match[1]);\n if (code in BINDING_CODE_STATUS) return code;\n }\n return undefined;\n}\n\n/**\n * Normalizes a thrown Workers AI binding error into a {@link CloudflareAIError}.\n * Abort errors and errors that are already {@link CloudflareAIError}s pass through.\n */\nexport function normalizeThrownError(\n error: unknown,\n context: {\n model: string;\n url: string;\n requestBodyValues: unknown;\n }\n): unknown {\n if (isAbortError(error)) return error;\n if (error instanceof CloudflareAIError) return error;\n\n const code = bindingErrorCode(error);\n const status = code === undefined ? undefined : BINDING_CODE_STATUS[code];\n return new CloudflareAIError({\n cause: error,\n code: classifyStatus(status),\n data: code === undefined ? undefined : { code },\n // A recognised code decides for itself. Everything else is not worth\n // retrying: the binding is the only backend, so a throw with no status is\n // a programming error rather than a transport failure, and re-running it\n // on every fallback leg helps nobody.\n isRetryable: status === undefined ? false : isRetryableStatus(status),\n message: messageOf(error),\n model: context.model,\n requestBodyValues: context.requestBodyValues,\n status,\n url: context.url\n });\n}\n","import {\n CloudflareAIError,\n classifyStatus,\n normalizeThrownError\n} from \"./errors\";\nimport type { GatewayProviderName } from \"./gateway-providers\";\nimport type { AISettings, ResolvedGateway } from \"./settings\";\n\n/** The reported `url` for a universal call made through the binding. */\nconst UNIVERSAL_BINDING_URL = \"ai-gateway-binding\";\n\n/** One request on the Cloudflare run path. */\nexport interface TransportRequest {\n /** Model id — Workers AI slug or catalog `<author>/<model>`. */\n model: string;\n /** The provider-shaped request body (`input` on the run path). */\n input: Record<string, unknown>;\n /** Gateway options for this request. */\n gateway: ResolvedGateway;\n /** Extra request headers, merged last. */\n headers: Record<string, string>;\n /** Abort signal, forwarded to the binding or to `fetch`. */\n signal?: AbortSignal;\n}\n\n/**\n * One request on the universal AI Gateway path: a vendor's own request, routed\n * by the gateway to that vendor. The body is the vendor's, untouched — nothing\n * here knows the vendor's wire format.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface UniversalRequest {\n /** The gateway provider slug the request is routed to. */\n provider: GatewayProviderName | string;\n /** The vendor path the gateway appends to that provider's base URL. */\n endpoint: string;\n /** The vendor's own request headers, minus its credential header. */\n headers: Record<string, string>;\n /** The vendor's request body, verbatim. */\n query: unknown;\n /** Gateway options for this request. */\n gateway: ResolvedGateway;\n /** Extra headers merged last, outside the vendor's own set. */\n extraHeaders?: Record<string, string>;\n /** Abort signal, forwarded to the binding or to `fetch`. */\n signal?: AbortSignal;\n}\n\n/**\n * The single code path both backends implement. Always resolves to a raw\n * `Response`, so streaming and non-streaming calls share one path and nothing\n * is buffered on the caller's behalf.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface Transport {\n /** A stable identifier for the endpoint, used in error reporting. */\n readonly url: string;\n run(request: TransportRequest): Promise<Response>;\n /**\n * Sends a vendor's own request through the gateway's universal endpoint.\n * Always resolves to the vendor's raw `Response`, error responses included:\n * only the gateway's own failures (a missing gateway, an unpaid account) are\n * lifted into a {@link CloudflareAIError}, because the vendor's provider\n * knows how to read its own errors and we do not.\n */\n universal(request: UniversalRequest): Promise<Response>;\n /**\n * A log id read off the backend itself when the response carried none. The\n * Workers AI binding exposes the last gateway log id as a mutable property,\n * so it is captured per response rather than read late.\n */\n logIdFallback(response: Response): string | undefined;\n}\n\n/**\n * Drops entries whose value is `undefined`. The AI SDK types call headers as\n * `Record<string, string | undefined>`, and an undefined value reaches the\n * binding as the string \"undefined\" rather than as an absent header.\n */\nexport function definedHeaders(\n headers: Record<string, string | undefined> | undefined\n): Record<string, string> {\n const result: Record<string, string> = {};\n for (const [key, value] of Object.entries(headers ?? {})) {\n if (value !== undefined) result[key] = value;\n }\n return result;\n}\n\n/** Copies response headers into a plain object. */\nexport function headersToObject(headers: Headers): Record<string, string> {\n const result: Record<string, string> = {};\n headers.forEach((value, key) => {\n result[key] = value;\n });\n return result;\n}\n\n/**\n * Some run-path responses arrive bare and some wrapped in the Cloudflare API\n * envelope. Unwrap only when the envelope keys are actually present, so a\n * provider payload that happens to have a `result` field is left alone.\n */\nexport function unwrapEnvelope(\n value: unknown,\n context?: { model: string; url: string; status: number }\n): unknown {\n if (value === null || typeof value !== \"object\" || Array.isArray(value)) {\n return value;\n }\n const record = value as Record<string, unknown>;\n // A failure envelope may carry no `result` at all, only its errors.\n const failed =\n record.success === false && firstEnvelopeError(record) !== undefined;\n if (failed || (\"success\" in record && \"result\" in record)) {\n if (record.success === true) return record.result;\n // A failure envelope can arrive with a 2xx status. Parsing it as a\n // provider payload would produce an empty result and no diagnostic.\n throw new CloudflareAIError({\n code: classifyStatus(\n context?.status,\n typeof record.name === \"string\" ? record.name : undefined\n ),\n data: value,\n message: errorMessageFrom(value, context?.status),\n model: context?.model ?? \"\",\n requestBodyValues: undefined,\n status: context?.status,\n url: context?.url ?? \"\"\n });\n }\n return value;\n}\n\nfunction firstEnvelopeError(\n record: Record<string, unknown>\n): { code?: unknown; message?: unknown } | undefined {\n // Live gateway responses use `error` (an array of one); the documented\n // Cloudflare envelope uses `errors`. Accept either.\n for (const key of [\"error\", \"errors\"]) {\n const value = record[key];\n if (Array.isArray(value) && value.length > 0) {\n const first = value[0];\n if (first !== null && typeof first === \"object\") {\n return first as { code?: unknown; message?: unknown };\n }\n }\n if (value !== null && typeof value === \"object\" && !Array.isArray(value)) {\n return value as { code?: unknown; message?: unknown };\n }\n }\n return undefined;\n}\n\n/**\n * Pulls a human message out of any of the error envelopes the platform emits:\n * the gateway envelope (`error: [{ code, message }]`), the Workers AI\n * validation envelope (`message`/`description` only), a bare\n * `{ error: \"...\" }`, and the \"Model ID missing\" internal error, which carries\n * only `description`.\n */\nexport function errorMessageFrom(\n body: unknown,\n status: number | undefined\n): string {\n const record =\n body !== null && typeof body === \"object\" && !Array.isArray(body)\n ? (body as Record<string, unknown>)\n : undefined;\n if (record !== undefined) {\n const envelope = firstEnvelopeError(record);\n if (typeof envelope?.message === \"string\") return envelope.message;\n if (typeof record.error === \"string\") return record.error;\n if (typeof record.message === \"string\") return record.message;\n if (typeof record.description === \"string\") return record.description;\n }\n if (typeof body === \"string\" && body !== \"\") return body;\n return `HTTP ${status ?? \"error\"}`;\n}\n\n/** Builds a {@link CloudflareAIError} from a non-2xx run-path response. */\nexport async function errorFromResponse(\n response: Response,\n context: { model: string; url: string; requestBodyValues: unknown }\n): Promise<CloudflareAIError> {\n const text = await response.text().catch(() => \"\");\n let body: unknown = text;\n try {\n body = text === \"\" ? undefined : JSON.parse(text);\n } catch {\n // Not JSON — the raw text is the best message we have.\n }\n const record =\n body !== null && typeof body === \"object\" && !Array.isArray(body)\n ? (body as Record<string, unknown>)\n : undefined;\n const envelopeName =\n typeof record?.name === \"string\" ? record.name : undefined;\n const headers = headersToObject(response.headers);\n return new CloudflareAIError({\n code: classifyStatus(response.status, envelopeName),\n data: body,\n logId: headers[\"cf-aig-log-id\"],\n message: errorMessageFrom(body, response.status),\n model: context.model,\n requestBodyValues: context.requestBodyValues,\n responseBody: text,\n responseHeaders: headers,\n status: response.status,\n url: context.url\n });\n}\n\n/**\n * The gateway's own options for the universal call. The id is not among them:\n * it selects the gateway (`env.AI.gateway(id)`), and the binding's own `id`\n * field is deprecated.\n */\nfunction universalGatewayOptions(\n gateway: ResolvedGateway\n): UniversalGatewayOptions {\n // `id` is deprecated on this type but still required by it; the gateway is\n // already selected by `env.AI.gateway(id)`, so it repeats that id.\n const options: UniversalGatewayOptions = { id: gateway.id };\n if (gateway.skipCache !== undefined) options.skipCache = gateway.skipCache;\n if (gateway.cacheTtl !== undefined) options.cacheTtl = gateway.cacheTtl;\n if (gateway.cacheKey !== undefined) options.cacheKey = gateway.cacheKey;\n if (gateway.metadata !== undefined) options.metadata = gateway.metadata;\n if (gateway.collectLog !== undefined) options.collectLog = gateway.collectLog;\n if (gateway.eventId !== undefined) options.eventId = gateway.eventId;\n if (gateway.requestTimeoutMs !== undefined) {\n options.requestTimeoutMs = gateway.requestTimeoutMs;\n }\n if (gateway.retries !== undefined) options.retries = gateway.retries;\n return options;\n}\n\n/**\n * Whether a parsed body is the gateway's own error envelope rather than a\n * vendor payload — `{ success: false, error: [{ code, message }] }`, named\n * `AiGatewayError`. The gateway answers this for its own failures (no such\n * gateway, unified billing unpaid, provider not configured); everything else\n * on this path is the vendor's own error, which the vendor's provider reads.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function isGatewayErrorEnvelope(json: unknown): boolean {\n if (json === null || typeof json !== \"object\" || Array.isArray(json)) {\n return false;\n }\n const record = json as Record<string, unknown>;\n if (record.name === \"AiGatewayError\") return true;\n if (record.success !== false) return false;\n const errors = record.error ?? record.errors;\n return (\n Array.isArray(errors) &&\n errors.some(\n (entry) =>\n entry !== null &&\n typeof entry === \"object\" &&\n typeof (entry as { message?: unknown }).message === \"string\"\n )\n );\n}\n\n/**\n * Builds a {@link CloudflareAIError} from the gateway's own error envelope, so\n * a gateway failure on the universal path classifies and retries exactly as\n * one on the Workers AI run path.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function errorFromGatewayEnvelope(\n body: unknown,\n context: {\n model: string;\n url: string;\n requestBodyValues: unknown;\n status?: number;\n responseHeaders?: Record<string, string>;\n responseBody?: string;\n }\n): CloudflareAIError {\n const record =\n body !== null && typeof body === \"object\" && !Array.isArray(body)\n ? (body as Record<string, unknown>)\n : undefined;\n return new CloudflareAIError({\n code: classifyStatus(\n context.status,\n typeof record?.name === \"string\" ? record.name : \"AiGatewayError\"\n ),\n data: body,\n logId: context.responseHeaders?.[\"cf-aig-log-id\"],\n message: errorMessageFrom(body, context.status),\n model: context.model,\n requestBodyValues: context.requestBodyValues,\n responseBody: context.responseBody,\n responseHeaders: context.responseHeaders,\n status: context.status,\n url: context.url\n });\n}\n\n/**\n * The model id a universal request is about, for error reporting. Every vendor\n * body carries `model`; the endpoint is the fallback.\n */\nfunction universalModel(request: UniversalRequest): string {\n const query = request.query;\n if (query !== null && typeof query === \"object\" && !Array.isArray(query)) {\n const model = (query as Record<string, unknown>).model;\n if (typeof model === \"string\") return model;\n }\n return `${request.provider}/${request.endpoint}`;\n}\n\n/**\n * The `Ai` binding narrowed to the raw-response overload. The ambient\n * overloads pick their return type from the model id, and every catalog slug\n * lands on the `Record<string, unknown>` fallback, so the run-path signature\n * is stated explicitly here.\n */\ninterface RawResponseBinding {\n run(\n model: string,\n inputs: Record<string, unknown>,\n options: AiOptions & { returnRawResponse: true }\n ): Promise<Response>;\n}\n\n/** The keyless in-Worker backend: `env.AI.run(...)`. */\nfunction bindingTransport(binding: Ai): Transport {\n const raw = binding as unknown as RawResponseBinding;\n // `aiGatewayLogId` is a mutable property on the binding that every\n // concurrent `run` overwrites, so it is snapshotted against the response it\n // belongs to the moment that response arrives. A run that resolves while\n // another is mid-flight can still read the other's id; the window is one\n // microtask rather than the whole request.\n const logIds = new WeakMap<Response, string>();\n return {\n logIdFallback(response) {\n return logIds.get(response);\n },\n async run(request) {\n try {\n // Called as a method, never detached: the binding reads private\n // fields and throws on a detached reference.\n const response = await raw.run(request.model, request.input, {\n ...(Object.keys(request.headers).length > 0\n ? { extraHeaders: request.headers }\n : {}),\n gateway: request.gateway,\n returnRawResponse: true,\n signal: request.signal\n });\n const logId = binding.aiGatewayLogId;\n if (logId !== null && logId !== undefined) {\n logIds.set(response, logId);\n }\n return response;\n } catch (error) {\n // `returnRawResponse` resolves with a `Response` for upstream errors,\n // so a throw here means the binding itself failed (e.g. code 3040,\n // out of capacity).\n throw normalizeThrownError(error, {\n model: request.model,\n requestBodyValues: request.input,\n url: \"workers-ai-binding\"\n });\n }\n },\n async universal(request) {\n try {\n // Called as a method on the binding, and on the `AiGateway` it\n // returns: both read private fields.\n return await binding.gateway(request.gateway.id).run(\n {\n endpoint: request.endpoint,\n headers: request.headers,\n provider: request.provider,\n query: request.query\n },\n {\n ...(request.extraHeaders === undefined\n ? {}\n : { extraHeaders: request.extraHeaders }),\n gateway: universalGatewayOptions(request.gateway),\n signal: request.signal\n }\n );\n } catch (error) {\n throw normalizeThrownError(error, {\n model: universalModel(request),\n requestBodyValues: request.query,\n url: UNIVERSAL_BINDING_URL\n });\n }\n },\n url: \"workers-ai-binding\"\n };\n}\n\n/**\n * Builds the transport for a set of provider settings. The model id, not the\n * caller, decides the wire format; the transport only decides how the request\n * physically leaves the Worker.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function createTransport(settings: AISettings): Transport {\n if (settings.binding === undefined) {\n throw new Error(\"createAI requires { binding }: the Workers AI binding.\");\n }\n return bindingTransport(settings.binding);\n}\n","/**\n * The AI Gateway provider table: which gateway provider slug a vendor's own\n * base URL belongs to, and what the universal request's `endpoint` is for a\n * given upstream URL.\n *\n * This is platform knowledge, not vendor knowledge — the gateway publishes the\n * list, and every entry here appears in `AIGatewayProviders` from\n * `@cloudflare/workers-types`. Nothing about a vendor's wire format, model ids\n * or capabilities lives here: the vendor's own provider package builds the\n * request, and this table only says where it goes.\n *\n * Adapted from `@cloudflare/ai-gateway-provider`'s `providers.ts`.\n *\n * @experimental This surface is experimental and may change.\n */\n\n/**\n * The gateway provider slugs this table maps onto. A subset of the ambient\n * `AIGatewayProviders` union; {@link GatewayProviderNameCheck} keeps it one.\n *\n * @experimental This surface is experimental and may change.\n */\nexport const GATEWAY_PROVIDER_NAMES = [\n \"workers-ai\",\n \"anthropic\",\n \"openai\",\n \"azure-openai\",\n \"google-ai-studio\",\n \"google-vertex-ai\",\n \"grok\",\n \"mistral\",\n \"perplexity-ai\",\n \"replicate\",\n \"groq\",\n \"deepseek\",\n \"openrouter\",\n \"cerebras\",\n \"cohere\",\n \"huggingface\",\n \"aws-bedrock\",\n \"elevenlabs\",\n \"cartesia\"\n] as const;\n\n/**\n * A gateway provider slug.\n *\n * @experimental This surface is experimental and may change.\n */\nexport type GatewayProviderName = (typeof GATEWAY_PROVIDER_NAMES)[number];\n\n/**\n * Compile-time guard: every slug in this table must exist in the platform's\n * own provider union, so a workers-types bump that renames one fails `tsc`\n * here rather than at runtime with a 404 from the gateway.\n *\n * @internal\n */\nexport type GatewayProviderNameCheck<\n T extends AIGatewayProviders = GatewayProviderName\n> = T;\n\n/**\n * One row of the provider table.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface GatewayProvider {\n /** The gateway provider slug. */\n readonly name: GatewayProviderName;\n /** Matches the upstream host (`URL.host`), not the whole URL. */\n readonly host: RegExp;\n /**\n * Request headers carrying the vendor's own credential. They are stripped\n * before the request is handed to the gateway, which holds the real key\n * (unified billing or BYOK). Lowercase.\n */\n readonly authHeaders: readonly string[];\n /**\n * The universal request's `endpoint` for an upstream URL: everything after\n * the host, query string included, with the provider's fixed prefix removed.\n * `undefined` when the URL does not belong to this provider.\n */\n endpoint(url: string): string | undefined;\n}\n\n/** The default credential header; providers add their own on top. */\nconst AUTHORIZATION = [\"authorization\"] as const;\n\ninterface ProviderSpec {\n name: GatewayProviderName;\n host: RegExp;\n authHeaders?: readonly string[];\n /** A fixed path prefix stripped from the endpoint. */\n pathPrefix?: RegExp;\n /** A full replacement for the default path mapping. */\n endpoint?: (url: URL) => string | undefined;\n}\n\n/** Path after the host, leading slash removed, query string kept. */\nfunction pathOf(url: URL): string {\n return `${url.pathname.replace(/^\\/+/, \"\")}${url.search}`;\n}\n\nfunction buildProvider(spec: ProviderSpec): GatewayProvider {\n const map =\n spec.endpoint ??\n ((url: URL) => {\n const path = pathOf(url);\n return spec.pathPrefix === undefined\n ? path\n : path.replace(spec.pathPrefix, \"\");\n });\n return {\n authHeaders: spec.authHeaders ?? AUTHORIZATION,\n endpoint(url) {\n const parsed = parseUrl(url);\n if (parsed === undefined || !spec.host.test(parsed.host)) {\n return undefined;\n }\n return map(parsed);\n },\n host: spec.host,\n name: spec.name\n };\n}\n\nfunction parseUrl(url: string): URL | undefined {\n try {\n return new URL(url);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Azure names the deployment in the path; the gateway names it in the\n * endpoint as `<resource>/<deployment>/<rest>`.\n */\nfunction azureEndpoint(url: URL): string | undefined {\n const resource = /^([^.]+)\\.openai\\.azure\\.com$/.exec(url.host)?.[1];\n const rest = /^\\/openai\\/deployments\\/([^/]+)\\/(.+)$/.exec(url.pathname);\n if (resource === undefined || rest === null) return undefined;\n return `${resource}/${rest[1]}/${rest[2]}${url.search}`;\n}\n\n/**\n * Bedrock's region and service live in the host; the gateway wants them as the\n * first two endpoint segments.\n */\nfunction bedrockEndpoint(url: URL): string | undefined {\n const match = /^(bedrock[a-z0-9-]*)\\.([a-z0-9-]+)\\.amazonaws\\.com$/.exec(\n url.host\n );\n if (match === null) return undefined;\n return `${match[1]}/${match[2]}/${pathOf(url)}`;\n}\n\nconst SPECS: ProviderSpec[] = [\n {\n // Workers AI's own API host. `env.AI.run` is this package's path for\n // `@cf/` models; this row exists because the gateway lists the provider,\n // so a model object pointed at that host still resolves.\n host: /^api\\.cloudflare\\.com$/,\n name: \"workers-ai\",\n pathPrefix: /^client\\/v4\\/accounts\\/[^/]+\\/ai\\//\n },\n {\n authHeaders: [\"authorization\", \"x-api-key\"],\n host: /^api\\.anthropic\\.com$/,\n name: \"anthropic\"\n },\n { host: /^api\\.openai\\.com$/, name: \"openai\" },\n {\n authHeaders: [\"authorization\", \"api-key\"],\n endpoint: azureEndpoint,\n host: /^[^.]+\\.openai\\.azure\\.com$/,\n name: \"azure-openai\"\n },\n {\n authHeaders: [\"authorization\", \"x-goog-api-key\"],\n host: /^generativelanguage\\.googleapis\\.com$/,\n name: \"google-ai-studio\"\n },\n {\n host: /^(?:[a-z0-9-]+-)?aiplatform\\.googleapis\\.com$/,\n name: \"google-vertex-ai\"\n },\n { host: /^api\\.x\\.ai$/, name: \"grok\" },\n { host: /^api\\.mistral\\.ai$/, name: \"mistral\" },\n { host: /^api\\.perplexity\\.ai$/, name: \"perplexity-ai\" },\n { host: /^api\\.replicate\\.com$/, name: \"replicate\" },\n {\n host: /^api\\.groq\\.com$/,\n name: \"groq\",\n pathPrefix: /^openai\\/v1\\//\n },\n { host: /^api\\.deepseek\\.com$/, name: \"deepseek\" },\n {\n host: /^openrouter\\.ai$/,\n name: \"openrouter\",\n pathPrefix: /^api\\//\n },\n { host: /^api\\.cerebras\\.ai$/, name: \"cerebras\" },\n { host: /^api\\.cohere\\.(?:ai|com)$/, name: \"cohere\" },\n {\n host: /^(?:api-inference|router)\\.huggingface\\.co$/,\n name: \"huggingface\",\n pathPrefix: /^models\\//\n },\n {\n endpoint: bedrockEndpoint,\n host: /^bedrock.*\\.amazonaws\\.com$/,\n name: \"aws-bedrock\"\n },\n { host: /^api\\.elevenlabs\\.io$/, name: \"elevenlabs\" },\n { host: /^api\\.cartesia\\.ai$/, name: \"cartesia\" }\n];\n\n/**\n * The provider table, in declaration order. Hosts do not overlap, so the order\n * only decides which row a future overlapping host would win.\n *\n * @experimental This surface is experimental and may change.\n */\nexport const GATEWAY_PROVIDERS: readonly GatewayProvider[] =\n SPECS.map(buildProvider);\n\n/**\n * A vendor URL mapped onto the universal gateway request.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface ResolvedGatewayProvider {\n /** The gateway provider slug. */\n provider: GatewayProviderName;\n /** The universal request's `endpoint`. */\n endpoint: string;\n /** Request headers to strip before handing the request to the gateway. */\n authHeaders: readonly string[];\n}\n\n/**\n * The table row for a bare host (`api.anthropic.com`) or for anything a `URL`\n * can be built from. pi-ai models carry a `baseUrl` rather than a full request\n * URL, so both spellings resolve.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function gatewayProviderForHost(\n hostOrUrl: string\n): GatewayProvider | undefined {\n const host = parseUrl(hostOrUrl)?.host ?? hostOf(hostOrUrl);\n if (host === undefined) return undefined;\n return GATEWAY_PROVIDERS.find((provider) => provider.host.test(host));\n}\n\n/** Reads a host out of a bare host, `host/path` or `//host` spelling. */\nfunction hostOf(value: string): string | undefined {\n const host = value.replace(/^\\/\\//, \"\").split(\"/\")[0];\n return host === \"\" ? undefined : host;\n}\n\n/**\n * Maps a vendor request URL onto `{ provider, endpoint, authHeaders }` for the\n * universal gateway request, or `undefined` when the host is not a gateway\n * provider.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function resolveGatewayProvider(\n url: string\n): ResolvedGatewayProvider | undefined {\n const parsed = parseUrl(url);\n if (parsed === undefined) return undefined;\n for (const provider of GATEWAY_PROVIDERS) {\n if (!provider.host.test(parsed.host)) continue;\n const endpoint = provider.endpoint(url);\n if (endpoint === undefined) continue;\n return {\n authHeaders: provider.authHeaders,\n endpoint,\n provider: provider.name\n };\n }\n return undefined;\n}\n\n/**\n * {@link resolveGatewayProvider}, throwing a `TypeError` that names the host\n * and the provider list when the URL belongs to no gateway provider.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function requireGatewayProvider(url: string): ResolvedGatewayProvider {\n const resolved = resolveGatewayProvider(url);\n if (resolved !== undefined) return resolved;\n const host = parseUrl(url)?.host ?? url;\n throw new TypeError(\n `AI Gateway has no provider for ${host}. Requests are routed by host; the supported providers are ${GATEWAY_PROVIDER_NAMES.join(\", \")}.`\n );\n}\n\n/** The host a gateway-shaped base URL points at. */\nconst GATEWAY_HOST = \"gateway.ai.cloudflare.com\";\n\n/**\n * The provider slug of a gateway-shaped base URL —\n * `https://gateway.ai.cloudflare.com/v1/{account}/{gateway}/{slug}`. Generated\n * registries write models that way (pi-ai's Cloudflare AI Gateway catalog\n * leaves the account and gateway as literal placeholders), so the slug is\n * already in the path rather than implied by a vendor host. The account and\n * gateway segments are ignored: the binding has already chosen both.\n */\nfunction gatewayShapedSlug(url: URL): string | undefined {\n if (url.host !== GATEWAY_HOST) return undefined;\n const segments = url.pathname.split(\"/\").filter((part) => part !== \"\");\n if (segments[0] !== \"v1\" || segments.length < 4) return undefined;\n return segments[3];\n}\n\n/**\n * Whether a base URL is written in the gateway's own shape\n * (`https://gateway.ai.cloudflare.com/v1/{account}/{gateway}/{slug}`) rather\n * than as a vendor's. A generated Cloudflare registry writes models that way,\n * and the path after the slug is then the vendor's own, so a caller that\n * derives an endpoint from a base URL has to tell the two apart.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function isGatewayShapedUrl(url: string): boolean {\n return parseUrl(url)?.host === GATEWAY_HOST;\n}\n\nfunction isGatewayProviderName(slug: string): slug is GatewayProviderName {\n return (GATEWAY_PROVIDER_NAMES as readonly string[]).includes(slug);\n}\n\n/**\n * The gateway provider slug a model's base URL belongs to. Two spellings\n * resolve, because two kinds of registry write them:\n *\n * - a vendor's own base URL (`https://api.anthropic.com` → `anthropic`), which\n * is what a provider package configured for the vendor carries;\n * - a gateway-shaped base URL\n * (`https://gateway.ai.cloudflare.com/v1/{account}/{gateway}/anthropic` →\n * `anthropic`), which is what a generated Cloudflare registry carries.\n *\n * A base URL neither shape resolves is a wiring mistake rather than a request\n * failure, so it throws a `TypeError` naming what was not recognised.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function gatewaySlugForBaseUrl(baseUrl: string): GatewayProviderName {\n const url = parseUrl(baseUrl);\n const shaped = url === undefined ? undefined : gatewayShapedSlug(url);\n if (shaped !== undefined) {\n if (isGatewayProviderName(shaped)) return shaped;\n throw new TypeError(\n `AI Gateway has no provider \"${shaped}\" (from ${baseUrl}). The supported providers are ${GATEWAY_PROVIDER_NAMES.join(\", \")}.`\n );\n }\n const provider = gatewayProviderForHost(baseUrl);\n if (provider !== undefined) return provider.name;\n throw new TypeError(\n `AI Gateway has no provider for ${url?.host ?? baseUrl}. Requests are routed by the model's base URL; the supported providers are ${GATEWAY_PROVIDER_NAMES.join(\", \")}.`\n );\n}\n","/**\n * Where a Cloudflare catalog model departs from strict OpenAI chat\n * completions, and how. Every entry is backed by a live capture from the\n * conformance probes (`research/40-*.json`, `41-*.json`); the default for an\n * unknown id is strict OpenAI, so a new conformant model needs no code here.\n *\n * Framework-neutral: both the AI SDK and the pi-ai providers consult this.\n */\n\n/** A request feature that had to be changed or dropped for a model. */\nexport interface CompatWarning {\n feature: string;\n message: string;\n}\n\nexport interface ChatCompletionsQuirks {\n /** Which field carries the output-token cap. OpenAI's current models reject `max_tokens`. */\n maxTokensField: \"max_tokens\" | \"max_completion_tokens\";\n /**\n * How reasoning can be turned off. `chat-template` sends\n * `chat_template_kwargs.enable_thinking = false` (vLLM); `unsupported` drops\n * the request with a warning because the model keeps reasoning regardless.\n */\n reasoningOff: \"chat-template\" | \"unsupported\";\n /**\n * Which `reasoning_effort` levels the model accepts. `openai` is the full\n * set (`minimal` … `xhigh`); `basic` is `low | medium | high`, everything\n * else clamped.\n */\n reasoningEffortLevels: \"openai\" | \"basic\";\n /** Whether `chat_template_kwargs` is accepted at all. */\n chatTemplateKwargs: \"supported\" | \"unsupported\";\n /** Whether `response_format: json_schema` is accepted at all. */\n jsonSchema: \"supported\" | \"unsupported\";\n /** Whether the OpenAI-only `strict` flag belongs on the json_schema envelope. */\n jsonSchemaStrict: boolean;\n /**\n * `tool_choice: \"required\"`. `named-fallback` rewrites it to the single\n * named tool when there is exactly one (glm-4.7-flash answers 400 to\n * `required`, a gateway serialization bug), and drops it otherwise.\n */\n toolChoiceRequired: \"supported\" | \"named-fallback\";\n /** Whether image parts may reference a URL, or must carry inline bytes. */\n imageUrls: \"any\" | \"data-only\";\n /** Whether replayed assistant reasoning (`reasoning_content`) is accepted. */\n reasoningReplay: \"reasoning_content\" | \"drop\";\n /**\n * Whether a `user` turn may directly follow a `tool` turn. Mistral rejects\n * it (\"Unexpected role 'user' after role 'tool'\"), so an empty assistant\n * turn is inserted between them.\n */\n requiresAssistantAfterToolResult: boolean;\n /**\n * What the model accepts as a tool-call id. `alnum-9` is Mistral's rule:\n * exactly nine `[A-Za-z0-9]` characters, anything else is a 400.\n */\n toolCallIds: \"any\" | \"alnum-9\";\n /** A suffix the model echoes on its own id that callers never asked for. */\n modelEchoSuffix: string | undefined;\n /** Whether a replayed assistant turn may carry `content: null`. */\n nullAssistantContent: \"allowed\" | \"empty-string\";\n /**\n * Where the answer lands in a stream once reasoning has been turned off.\n * `reasoning` means the model's streaming parser mislabels the whole answer\n * as reasoning (its non-streaming body is fine), so the normalizer moves\n * those deltas back into `content`.\n */\n reasoningOffStreamLabel: \"content\" | \"reasoning\";\n}\n\nconst STRICT_OPENAI: ChatCompletionsQuirks = {\n chatTemplateKwargs: \"unsupported\",\n imageUrls: \"any\",\n jsonSchema: \"supported\",\n jsonSchemaStrict: false,\n maxTokensField: \"max_tokens\",\n modelEchoSuffix: undefined,\n nullAssistantContent: \"allowed\",\n reasoningEffortLevels: \"basic\",\n reasoningOff: \"unsupported\",\n reasoningOffStreamLabel: \"content\",\n reasoningReplay: \"drop\",\n requiresAssistantAfterToolResult: false,\n toolCallIds: \"any\",\n toolChoiceRequired: \"supported\"\n};\n\n/** Workers AI's vLLM front end: what every `@cf/` model has in common. */\nconst WORKERS_AI: ChatCompletionsQuirks = {\n ...STRICT_OPENAI,\n chatTemplateKwargs: \"supported\",\n imageUrls: \"data-only\",\n nullAssistantContent: \"empty-string\",\n reasoningOff: \"chat-template\",\n reasoningReplay: \"reasoning_content\"\n};\n\n/**\n * Longest-prefix overrides. Keep each one evidence-backed (the capture that\n * proves it is named in the comment) and delete it when the platform fixes\n * the deviation.\n */\nconst OVERRIDES: [prefix: string, quirks: Partial<ChatCompletionsQuirks>][] = [\n // 40-demos-...-E1: `tool_choice: \"required\"` + tools → 400 pydantic parse error.\n [\"@cf/zai-org/glm-4.7-flash\", { toolChoiceRequired: \"named-fallback\" }],\n // 40-demos-kimi-k3-G3 / 40-matt-kimi-k2.7-code-G_ctk: enable_thinking=false\n // leaves reasoning on; nothing turns it off.\n [\"@cf/moonshotai/\", { reasoningOff: \"unsupported\" }],\n // 40-demos-gpt-oss-120b-G3: same.\n [\"@cf/openai/\", { reasoningOff: \"unsupported\" }],\n // 40-demos-...-F1/F2/G3/J1: \"chat_template is not supported for Mistral\n // tokenizers\", \"Unexpected role 'user' after role 'tool'\".\n // 40-matt-mistral-small-3.1-24b-instruct-J: \"Tool call id was call_1 but\n // must be a-z, A-Z, 0-9, with a length of 9.\"\n [\n \"@cf/mistralai/\",\n {\n chatTemplateKwargs: \"unsupported\",\n jsonSchema: \"unsupported\",\n reasoningOff: \"unsupported\",\n requiresAssistantAfterToolResult: true,\n toolCallIds: \"alnum-9\"\n }\n ],\n // 40-demos-gemma-4-26b-a4b-it-A: echoes \"@cf/google/gemma-4-26b-a4b-it-external\".\n [\"@cf/google/\", { modelEchoSuffix: \"-external\" }],\n // 41-matt-nemotron-3-120b-a12b-K_emoji / S_usage_s: with enable_thinking=false\n // every streamed token arrives as `delta.reasoning` and `content` stays\n // empty, while the non-streaming body (A0, 40-...-G_ctk) puts the same\n // answer in `content` with `reasoning: null`.\n [\"@cf/nvidia/nemotron-\", { reasoningOffStreamLabel: \"reasoning\" }]\n];\n\n/** The quirks for a catalog id: base profile plus the longest matching override. */\nexport function quirksFor(modelId: string): ChatCompletionsQuirks {\n const base = modelId.startsWith(\"@cf/\") ? WORKERS_AI : STRICT_OPENAI;\n let best: [string, Partial<ChatCompletionsQuirks>] | undefined;\n for (const entry of OVERRIDES) {\n if (\n modelId.startsWith(entry[0]) &&\n (best === undefined || entry[0].length > best[0].length)\n ) {\n best = entry;\n }\n }\n return best === undefined ? base : { ...base, ...best[1] };\n}\n\n/** Whether an id is a Workers AI model rather than a third-party catalog model. */\nexport function isWorkersAI(modelId: string): boolean {\n return modelId.startsWith(\"@cf/\");\n}\n","/**\n * Rewrites a strict OpenAI chat-completions request body into what a given\n * catalog model accepts. Frameworks build the strict body from their own\n * prompt types; everything Cloudflare- or vendor-specific happens here, once.\n *\n * The rewrite never throws: a feature the model cannot take is dropped and\n * reported as a {@link CompatWarning}.\n */\n\nimport { type CompatWarning, quirksFor } from \"./quirks\";\n\nexport interface PreparedRequest {\n body: Record<string, unknown>;\n warnings: CompatWarning[];\n /**\n * Whether the body turns the model's reasoning off through the chat\n * template. The stream normalizer needs this for models whose streaming\n * parser mislabels the answer once thinking is off.\n */\n reasoningOff: boolean;\n}\n\ntype Message = Record<string, unknown>;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\nfunction clampEffort(effort: string): string {\n if (effort === \"minimal\") return \"low\";\n if (effort === \"xhigh\") return \"high\";\n return effort;\n}\n\n/** Drops URL image parts (keeps inline data URLs) from a user message. */\nfunction stripImageUrls(message: Message, warnings: CompatWarning[]): Message {\n if (!Array.isArray(message.content)) return message;\n let dropped = 0;\n const content = message.content.filter((part) => {\n if (!isRecord(part) || part.type !== \"image_url\") return true;\n const image = isRecord(part.image_url) ? part.image_url : undefined;\n const url = typeof image?.url === \"string\" ? image.url : \"\";\n if (url.startsWith(\"data:\")) return true;\n dropped += 1;\n return false;\n });\n if (dropped > 0) {\n warnings.push({\n feature: \"image-url\",\n message: `Workers AI models need image bytes; ${dropped} image URL${dropped === 1 ? \" was\" : \"s were\"} dropped.`\n });\n }\n return { ...message, content };\n}\n\n/** Nine `[A-Za-z0-9]` characters: what Mistral accepts as a tool-call id. */\nconst CONFORMANT_TOOL_CALL_ID = /^[0-9A-Za-z]{9}$/;\n\nconst TOOL_CALL_ID_ALPHABET =\n \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\";\n\n/**\n * A deterministic nine-character id for an original id: the same conversation\n * replayed twice produces the same ids, so a provider cache or a log stays\n * comparable. `salt` is bumped only to break a collision.\n */\nfunction hashedToolCallId(original: string, salt: number): string {\n let state = (0x811c9dc5 ^ salt) >>> 0;\n for (let index = 0; index < original.length; index += 1) {\n state = Math.imul(state ^ original.charCodeAt(index), 0x01000193) >>> 0;\n }\n let id = \"\";\n for (let index = 0; index < 9; index += 1) {\n state = Math.imul(state ^ (state >>> 15), 0x01000193) >>> 0;\n id += TOOL_CALL_ID_ALPHABET[state % TOOL_CALL_ID_ALPHABET.length];\n }\n return id;\n}\n\n/** Every tool-call id a message set mentions, in the order it mentions them. */\nfunction toolCallIdsIn(messages: unknown[]): string[] {\n const ids: string[] = [];\n const seen = new Set<string>();\n const add = (value: unknown) => {\n if (typeof value !== \"string\" || seen.has(value)) return;\n seen.add(value);\n ids.push(value);\n };\n for (const message of messages) {\n if (!isRecord(message)) continue;\n if (Array.isArray(message.tool_calls)) {\n for (const call of message.tool_calls) {\n if (isRecord(call)) add(call.id);\n }\n }\n add(message.tool_call_id);\n }\n return ids;\n}\n\n/**\n * Plans the rewrite: which ids have to change, and to what. Ids that already\n * conform are kept exactly as they are — including as reserved values, so a\n * rewritten id can never collide with one.\n */\nfunction planToolCallIds(messages: unknown[]): Map<string, string> {\n const plan = new Map<string, string>();\n const ids = toolCallIdsIn(messages);\n const taken = new Set(ids.filter((id) => CONFORMANT_TOOL_CALL_ID.test(id)));\n for (const id of ids) {\n if (taken.has(id)) continue;\n let replacement = hashedToolCallId(id, 0);\n for (let salt = 1; taken.has(replacement) && salt <= 64; salt += 1) {\n replacement = hashedToolCallId(id, salt);\n }\n taken.add(replacement);\n plan.set(id, replacement);\n }\n return plan;\n}\n\n/** Applies the plan to an assistant `tool_calls` turn or a `tool` result. */\nfunction rewriteToolCallIds(\n message: Message,\n plan: Map<string, string>\n): Message {\n let rewritten = message;\n if (Array.isArray(rewritten.tool_calls)) {\n rewritten = {\n ...rewritten,\n tool_calls: rewritten.tool_calls.map((call) => {\n if (!isRecord(call) || typeof call.id !== \"string\") return call;\n const replacement = plan.get(call.id);\n return replacement === undefined ? call : { ...call, id: replacement };\n })\n };\n }\n if (typeof rewritten.tool_call_id === \"string\") {\n const replacement = plan.get(rewritten.tool_call_id);\n if (replacement !== undefined) {\n rewritten = { ...rewritten, tool_call_id: replacement };\n }\n }\n return rewritten;\n}\n\n/**\n * Applies the model's quirks to a strict OpenAI chat-completions body.\n *\n * Conventions the strict body follows, which this function translates:\n * - `max_tokens` is the cap; it becomes `max_completion_tokens` where needed.\n * - `reasoning_effort: null` means \"turn reasoning off\".\n * - Replayed assistant reasoning sits in `reasoning_content`.\n * - `response_format.json_schema` is the OpenAI `{ name, schema }` envelope.\n * - `chat_template_kwargs` may be present; it is only meaningful on Workers AI.\n */\nexport function prepareChatCompletionsRequest(\n input: Record<string, unknown>,\n modelId: string\n): PreparedRequest {\n const quirks = quirksFor(modelId);\n const warnings: CompatWarning[] = [];\n const body: Record<string, unknown> = { ...input };\n\n // Output-token cap.\n if (\n quirks.maxTokensField === \"max_completion_tokens\" &&\n \"max_tokens\" in body\n ) {\n body.max_completion_tokens = body.max_tokens;\n delete body.max_tokens;\n }\n\n // Reasoning.\n if (\"reasoning_effort\" in body) {\n const effort = body.reasoning_effort;\n if (effort === null) {\n delete body.reasoning_effort;\n if (quirks.reasoningOff === \"chat-template\") {\n body.chat_template_kwargs = {\n ...(isRecord(body.chat_template_kwargs)\n ? body.chat_template_kwargs\n : {}),\n enable_thinking: false\n };\n } else {\n warnings.push({\n feature: \"reasoning-off\",\n message: `Reasoning cannot be turned off on ${modelId}; the request was sent with the model's default effort.`\n });\n }\n } else if (\n typeof effort === \"string\" &&\n quirks.reasoningEffortLevels === \"basic\"\n ) {\n body.reasoning_effort = clampEffort(effort);\n }\n }\n if (\n \"chat_template_kwargs\" in body &&\n quirks.chatTemplateKwargs === \"unsupported\"\n ) {\n delete body.chat_template_kwargs;\n warnings.push({\n feature: \"chat-template-kwargs\",\n message: `${modelId} does not take chat_template_kwargs; they were dropped.`\n });\n }\n\n // Structured output.\n if (isRecord(body.response_format)) {\n if (quirks.jsonSchema === \"unsupported\") {\n delete body.response_format;\n warnings.push({\n feature: \"response-format\",\n message: `${modelId} does not support response_format; the request was sent without it.`\n });\n } else if (\n body.response_format.type === \"json_schema\" &&\n isRecord(body.response_format.json_schema)\n ) {\n const { strict: _strict, ...envelope } = body.response_format.json_schema;\n body.response_format = {\n ...body.response_format,\n json_schema: quirks.jsonSchemaStrict\n ? { ...envelope, strict: true }\n : envelope\n };\n }\n }\n\n // Tools.\n if (\n body.tool_choice === \"required\" &&\n quirks.toolChoiceRequired === \"named-fallback\"\n ) {\n const tools = Array.isArray(body.tools) ? body.tools : [];\n const only =\n tools.length === 1 && isRecord(tools[0]) ? tools[0] : undefined;\n const name =\n only !== undefined &&\n isRecord(only.function) &&\n typeof only.function.name === \"string\"\n ? only.function.name\n : undefined;\n if (name !== undefined) {\n body.tool_choice = { function: { name }, type: \"function\" };\n } else {\n delete body.tool_choice;\n warnings.push({\n feature: \"tool-choice-required\",\n message: `${modelId} rejects tool_choice \"required\"; the model was left free to choose.`\n });\n }\n }\n\n // Messages.\n if (Array.isArray(body.messages)) {\n // Two passes: the ids are planned over the whole conversation first, so an\n // assistant turn and the tool result answering it are rewritten to the\n // same value however far apart they sit.\n const toolCallIds =\n quirks.toolCallIds === \"alnum-9\"\n ? planToolCallIds(body.messages)\n : new Map<string, string>();\n if (toolCallIds.size > 0) {\n warnings.push({\n feature: \"tool-call-id\",\n message: `${modelId} needs 9-character alphanumeric tool-call ids; ${toolCallIds.size} id${toolCallIds.size === 1 ? \" was\" : \"s were\"} rewritten.`\n });\n }\n const messages: Message[] = [];\n let previousRole: unknown;\n for (const entry of body.messages) {\n if (!isRecord(entry)) continue;\n let message: Message = entry;\n if (toolCallIds.size > 0) {\n message = rewriteToolCallIds(message, toolCallIds);\n }\n if (message.role === \"user\" && quirks.imageUrls === \"data-only\") {\n message = stripImageUrls(message, warnings);\n }\n if (\n message.role === \"assistant\" &&\n message.content === null &&\n quirks.nullAssistantContent === \"empty-string\"\n ) {\n // 41-demos-gpt-oss-120b-O_replay_null: a replayed assistant turn with\n // `content: null` is a 400 on Workers AI; `\"\"` is accepted everywhere.\n message = { ...message, content: \"\" };\n }\n if (message.role === \"assistant\" && \"reasoning_content\" in message) {\n if (quirks.reasoningReplay === \"drop\") {\n const { reasoning_content: _reasoning, ...rest } = message;\n message = rest;\n warnings.push({\n feature: \"assistant-reasoning\",\n message: `${modelId} has no field for replayed reasoning; the assistant reasoning was dropped.`\n });\n }\n }\n if (\n quirks.requiresAssistantAfterToolResult &&\n previousRole === \"tool\" &&\n message.role === \"user\"\n ) {\n messages.push({ content: \"\", role: \"assistant\" });\n }\n messages.push(message);\n previousRole = message.role;\n }\n body.messages = messages;\n }\n\n const reasoningOff =\n isRecord(body.chat_template_kwargs) &&\n body.chat_template_kwargs.enable_thinking === false;\n return { body, reasoningOff, warnings };\n}\n","/**\n * Normalizes a chat-completions response body from the run path into the\n * strict OpenAI `chat.completion` shape, so framework parsers never see\n * Workers AI's native envelope or its field spellings.\n */\n\nimport { quirksFor } from \"./quirks\";\n\ntype Record_ = Record<string, unknown>;\n\nfunction isRecord(value: unknown): value is Record_ {\n return value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\n/** Native `{ name, arguments: object }` → OpenAI `{ id, type, function }`. */\nexport function normalizeToolCall(\n entry: unknown,\n index: number\n): Record_ | undefined {\n if (!isRecord(entry)) return undefined;\n const fn = isRecord(entry.function) ? entry.function : undefined;\n const name =\n typeof fn?.name === \"string\"\n ? fn.name\n : typeof entry.name === \"string\"\n ? entry.name\n : undefined;\n if (name === undefined) return undefined;\n const rawArguments =\n fn !== undefined && \"arguments\" in fn ? fn.arguments : entry.arguments;\n const args =\n typeof rawArguments === \"string\"\n ? rawArguments\n : rawArguments === undefined || rawArguments === null\n ? \"{}\"\n : JSON.stringify(rawArguments);\n return {\n ...(typeof entry.index === \"number\" ? { index: entry.index } : { index }),\n ...(typeof entry.id === \"string\" ? { id: entry.id } : {}),\n function: { arguments: args, name },\n type: \"function\",\n ...(entry.extra_content !== undefined\n ? { extra_content: entry.extra_content }\n : {})\n };\n}\n\n/** Text as the model produced it; structured-output models may return an object. */\nfunction contentText(value: unknown): string | null {\n if (typeof value === \"string\") return value;\n if (value === null || value === undefined) return null;\n return JSON.stringify(value);\n}\n\nfunction stripEchoSuffix(model: unknown, modelId: string): unknown {\n const suffix = quirksFor(modelId).modelEchoSuffix;\n if (typeof model !== \"string\" || suffix === undefined) return model;\n return model.endsWith(suffix) ? model.slice(0, -suffix.length) : model;\n}\n\n/** Puts reasoning under `reasoning_content` whichever spelling the model used. */\nfunction normalizeMessage(message: Record_): Record_ {\n const out: Record_ = { ...message };\n const reasoning =\n typeof out.reasoning_content === \"string\" && out.reasoning_content !== \"\"\n ? out.reasoning_content\n : typeof out.reasoning === \"string\" && out.reasoning !== \"\"\n ? out.reasoning\n : undefined;\n delete out.reasoning;\n if (reasoning !== undefined) out.reasoning_content = reasoning;\n else delete out.reasoning_content;\n if (\"content\" in out) out.content = contentText(out.content);\n if (Array.isArray(out.tool_calls)) {\n const calls = out.tool_calls\n .map((call, index) => normalizeToolCall(call, index))\n .filter((call): call is Record_ => call !== undefined);\n if (calls.length > 0) out.tool_calls = calls;\n else delete out.tool_calls;\n }\n return out;\n}\n\n/**\n * Returns the body in the strict `chat.completion` shape. Already-OpenAI\n * bodies come back with their reasoning spelling unified and the echoed model\n * id cleaned; native Workers AI `{ response, tool_calls, usage }` bodies are\n * lifted into `choices[0]`.\n */\nexport function normalizeChatCompletion(\n body: unknown,\n modelId: string\n): Record_ {\n if (!isRecord(body)) return { choices: [] };\n if (Array.isArray(body.choices)) {\n const choices = body.choices.map((choice) => {\n if (!isRecord(choice)) return choice;\n const message = isRecord(choice.message)\n ? normalizeMessage(choice.message)\n : choice.message;\n const hasToolCalls =\n isRecord(message) &&\n Array.isArray(message.tool_calls) &&\n message.tool_calls.length > 0;\n return {\n ...choice,\n message,\n finish_reason:\n choice.finish_reason ?? (hasToolCalls ? \"tool_calls\" : \"stop\")\n };\n });\n return { ...body, choices, model: stripEchoSuffix(body.model, modelId) };\n }\n if (\"response\" in body || \"tool_calls\" in body) {\n const toolCalls = Array.isArray(body.tool_calls)\n ? body.tool_calls\n .map((call, index) => normalizeToolCall(call, index))\n .filter((call): call is Record_ => call !== undefined)\n : [];\n const { response, tool_calls: _toolCalls, usage, ...rest } = body;\n return {\n ...rest,\n choices: [\n {\n finish_reason:\n typeof body.finish_reason === \"string\"\n ? body.finish_reason\n : toolCalls.length > 0\n ? \"tool_calls\"\n : \"stop\",\n index: 0,\n message: {\n content: contentText(response),\n role: \"assistant\",\n ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {})\n }\n }\n ],\n model: stripEchoSuffix(body.model, modelId),\n ...(usage !== undefined ? { usage } : {})\n };\n }\n return body;\n}\n","/**\n * Normalizes a chat-completions SSE stream from the run path into strict\n * OpenAI `chat.completion.chunk` events.\n *\n * Every Workers AI model streams the same three-part hybrid (live captures,\n * all families): OpenAI-shaped delta chunks that each carry a per-delta\n * `usage`, then a `choices: []` heartbeat, then a native\n * `{ response: \"\", usage }` tail with the cumulative usage, then `[DONE]`.\n * Some models also emit native `{ response: \"<text>\" }` deltas or top-level\n * `tool_calls`. After this transform a consumer sees only:\n *\n * - delta chunks without per-delta usage and with reasoning under\n * `reasoning_content`;\n * - at most one final `choices: []` chunk carrying the cumulative usage\n * (the OpenAI `stream_options.include_usage` shape);\n * - a `finish_reason` on some chunk before the end, synthesized when the\n * model finished cleanly without sending one;\n * - `[DONE]`.\n *\n * Input and output are SSE `data` payloads (strings), so it slots between the\n * SSE decoder and either framework's OpenAI chunk parser.\n */\n\nimport { isWorkersAI, quirksFor } from \"./quirks\";\nimport { normalizeToolCall } from \"./response\";\n\ntype Record_ = Record<string, unknown>;\n\nfunction isRecord(value: unknown): value is Record_ {\n return value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n\nconst NOISE_FIELDS = [\n \"p\",\n \"token_ids\",\n \"prompt_token_ids\",\n \"kv_transfer_params\"\n];\n\n/** Fields the run path adds to every chunk that mean nothing to a consumer. */\nfunction stripNoise(chunk: Record_): Record_ {\n const out = { ...chunk };\n for (const key of NOISE_FIELDS) delete out[key];\n return out;\n}\n\nfunction stripEchoSuffix(chunk: Record_, modelId: string): Record_ {\n const suffix = quirksFor(modelId).modelEchoSuffix;\n if (suffix === undefined || typeof chunk.model !== \"string\") return chunk;\n return chunk.model.endsWith(suffix)\n ? { ...chunk, model: chunk.model.slice(0, -suffix.length) }\n : chunk;\n}\n\nfunction normalizeDelta(delta: Record_, relabelReasoning: boolean): Record_ {\n const out: Record_ = { ...delta };\n const reasoning =\n typeof out.reasoning_content === \"string\" && out.reasoning_content !== \"\"\n ? out.reasoning_content\n : typeof out.reasoning === \"string\" && out.reasoning !== \"\"\n ? out.reasoning\n : undefined;\n delete out.reasoning;\n delete out.reasoning_content;\n if (reasoning !== undefined) {\n if (relabelReasoning) {\n // Reasoning is off, so these tokens are the answer (nemotron-3).\n out.content =\n (typeof out.content === \"string\" ? out.content : \"\") + reasoning;\n } else {\n out.reasoning_content = reasoning;\n }\n }\n if (Array.isArray(out.tool_calls)) {\n out.tool_calls = out.tool_calls.map((call, index) => {\n // Fragments and the null finalizer pass through untouched; only a\n // native complete call needs lifting into the OpenAI shape. A native\n // call names its tool at the top level, where no OpenAI fragment does,\n // and its arguments may be an object or already a string.\n if (!isRecord(call)) return call;\n const fn = isRecord(call.function) ? call.function : undefined;\n const args = fn !== undefined ? fn.arguments : call.arguments;\n const native = fn === undefined && typeof call.name === \"string\";\n return native || isRecord(args)\n ? (normalizeToolCall(call, index) ?? call)\n : call;\n });\n }\n return out;\n}\n\nexport interface StreamNormalizationOptions {\n /** Whether the request turned reasoning off (`PreparedRequest.reasoningOff`). */\n reasoningOff?: boolean;\n}\n\n/**\n * The transform. Backpressure is preserved: nothing is buffered except the\n * single pending usage chunk, which must wait for the tail.\n */\nexport function normalizeChatCompletionsStream(\n modelId: string,\n options: StreamNormalizationOptions = {}\n): TransformStream<string, string> {\n const quirks = quirksFor(modelId);\n const relabelReasoning =\n options.reasoningOff === true &&\n quirks.reasoningOffStreamLabel === \"reasoning\";\n let id: string | undefined;\n let model: string | undefined;\n let created: number | undefined;\n let sawFinishReason = false;\n let sawContent = false;\n let sawToolCalls = false;\n let sawDone = false;\n let finalUsage: unknown;\n let finalUsageTemplate: Record_ | undefined;\n // Workers AI stamps a per-delta usage on every content chunk and sends the\n // cumulative figure in its native tail; other vendors only ever put usage on\n // the final `choices: []` chunk, so theirs is left alone.\n const perDeltaUsage = isWorkersAI(modelId);\n\n const envelope = (extra: Record_): Record_ => ({\n ...(id !== undefined ? { id } : {}),\n object: \"chat.completion.chunk\",\n ...(created !== undefined ? { created } : {}),\n ...(model !== undefined ? { model } : {}),\n ...extra\n });\n\n const emit = (\n controller: TransformStreamDefaultController<string>,\n chunk: Record_\n ) => {\n controller.enqueue(JSON.stringify(chunk));\n };\n\n const emitTail = (controller: TransformStreamDefaultController<string>) => {\n if (!sawFinishReason && sawDone && (sawContent || sawToolCalls)) {\n emit(\n controller,\n envelope({\n choices: [\n {\n delta: {},\n finish_reason: sawToolCalls ? \"tool_calls\" : \"stop\",\n index: 0\n }\n ]\n })\n );\n sawFinishReason = true;\n }\n if (finalUsage !== undefined) {\n emit(controller, {\n ...(finalUsageTemplate ?? envelope({})),\n choices: [],\n usage: finalUsage\n });\n finalUsage = undefined;\n }\n if (sawDone) controller.enqueue(\"[DONE]\");\n };\n\n return new TransformStream<string, string>({\n flush(controller) {\n emitTail(controller);\n },\n transform(data, controller) {\n if (data === \"\") return;\n if (data === \"[DONE]\") {\n // Everything after the tail is emitted at flush, once.\n sawDone = true;\n return;\n }\n let parsed: unknown;\n try {\n parsed = JSON.parse(data);\n } catch {\n // A malformed event is dropped rather than failing the stream.\n return;\n }\n if (!isRecord(parsed)) return;\n const chunk = stripEchoSuffix(stripNoise(parsed), modelId);\n\n if (typeof chunk.id === \"string\") id ??= chunk.id;\n if (typeof chunk.model === \"string\") model ??= chunk.model;\n if (typeof chunk.created === \"number\") created ??= chunk.created;\n\n // An error payload passes through untouched for the consumer to surface;\n // it also ends the generation, so no finish reason is synthesized after it.\n if (isRecord(chunk.error)) {\n sawFinishReason = true;\n controller.enqueue(JSON.stringify(chunk));\n return;\n }\n\n if (Array.isArray(chunk.choices)) {\n if (chunk.choices.length === 0) {\n // Heartbeat, or OpenAI's own final usage chunk: hold the usage\n // until the end so exactly one usage chunk is emitted.\n if (isRecord(chunk.usage)) {\n finalUsage = chunk.usage;\n finalUsageTemplate = { ...chunk, usage: undefined };\n delete finalUsageTemplate.usage;\n }\n return;\n }\n const choices = chunk.choices.map((choice) => {\n if (!isRecord(choice)) return choice;\n if (typeof choice.finish_reason === \"string\") sawFinishReason = true;\n const delta = isRecord(choice.delta)\n ? normalizeDelta(choice.delta, relabelReasoning)\n : choice.delta;\n if (isRecord(delta)) {\n if (typeof delta.content === \"string\" && delta.content !== \"\")\n sawContent = true;\n if (typeof delta.reasoning_content === \"string\") sawContent = true;\n if (Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0)\n sawToolCalls = true;\n }\n if (!perDeltaUsage) return { ...choice, delta };\n const { usage: _perDeltaUsage, ...rest } = choice;\n return { ...rest, delta };\n });\n if (perDeltaUsage) {\n // The cumulative figure comes with the native tail.\n const { usage: _usage, ...rest } = chunk;\n emit(controller, { ...rest, choices });\n } else {\n emit(controller, { ...chunk, choices });\n }\n return;\n }\n\n // Native Workers AI events.\n if (\"response\" in chunk || \"tool_calls\" in chunk) {\n const text = chunk.response;\n const hasText = typeof text === \"string\" && text !== \"\";\n const calls = Array.isArray(chunk.tool_calls)\n ? chunk.tool_calls\n .map((call, index) => normalizeToolCall(call, index))\n .filter((call): call is Record_ => call !== undefined)\n : [];\n if (hasText || calls.length > 0) {\n if (hasText) sawContent = true;\n if (calls.length > 0) sawToolCalls = true;\n emit(\n controller,\n envelope({\n choices: [\n {\n delta: {\n ...(hasText ? { content: text } : {}),\n ...(calls.length > 0 ? { tool_calls: calls } : {})\n },\n finish_reason: null,\n index: 0\n }\n ]\n })\n );\n }\n if (isRecord(chunk.usage)) {\n // The native tail: cumulative usage for the whole stream.\n finalUsage = chunk.usage;\n finalUsageTemplate = undefined;\n }\n if (typeof chunk.finish_reason === \"string\") {\n sawFinishReason = true;\n emit(\n controller,\n envelope({\n choices: [\n { delta: {}, finish_reason: chunk.finish_reason, index: 0 }\n ]\n })\n );\n }\n }\n }\n });\n}\n","/**\n * Decodes a `text/event-stream` byte stream into the `data` payload of each\n * event, as a `TransformStream` so the whole pipeline keeps backpressure and\n * nothing is ever buffered beyond the current event.\n *\n * Handles all three line terminators the SSE spec allows (LF, CRLF and a bare\n * CR), multi-line `data:` fields (joined with `\\n`, per the spec), comment\n * lines (`:` prefixed), and non-`data` fields (`event:`, `id:`, `retry:`),\n * which are ignored. A trailing event that the server never terminated with a\n * blank line is still flushed.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function sseDataStream(): TransformStream<Uint8Array, string> {\n const decoder = new TextDecoder();\n let buffer = \"\";\n let data: string[] = [];\n\n const flushEvent = (controller: TransformStreamDefaultController<string>) => {\n if (data.length === 0) return;\n controller.enqueue(data.join(\"\\n\"));\n data = [];\n };\n\n const consumeLine = (\n line: string,\n controller: TransformStreamDefaultController<string>\n ) => {\n if (line === \"\") {\n flushEvent(controller);\n return;\n }\n if (line.startsWith(\":\")) return;\n const colon = line.indexOf(\":\");\n const field = colon === -1 ? line : line.slice(0, colon);\n if (field !== \"data\") return;\n let value = colon === -1 ? \"\" : line.slice(colon + 1);\n if (value.startsWith(\" \")) value = value.slice(1);\n data.push(value);\n };\n\n /**\n * Consumes whole lines out of the buffer. A buffer ending in a lone `\\r` is\n * held back while more bytes may arrive, because the next byte decides\n * whether that `\\r` was a CRLF pair or a bare-CR terminator.\n */\n const drain = (\n controller: TransformStreamDefaultController<string>,\n final: boolean\n ) => {\n for (;;) {\n const match = /\\r\\n|[\\r\\n]/.exec(buffer);\n if (match === null) break;\n const end = match.index + match[0].length;\n if (!final && match[0] === \"\\r\" && end === buffer.length) break;\n consumeLine(buffer.slice(0, match.index), controller);\n buffer = buffer.slice(end);\n }\n };\n\n return new TransformStream<Uint8Array, string>({\n flush(controller) {\n buffer += decoder.decode();\n drain(controller, true);\n // A server that closed without a final blank line still owes us the\n // last event; emit whatever is left rather than dropping it.\n if (buffer !== \"\") {\n consumeLine(buffer, controller);\n buffer = \"\";\n }\n flushEvent(controller);\n },\n transform(chunk, controller) {\n buffer += decoder.decode(chunk, { stream: true });\n drain(controller, false);\n }\n });\n}\n","/**\n * Workers AI text-to-image requests and answers, shared by both framework\n * modules.\n *\n * Two things about this catalog are easy to get wrong in one module and right\n * in the other, so neither module owns them: the flux-1 models take a two-key\n * subset of the common input shape, and the media type of a JSON answer is in\n * the bytes rather than in the model name (flux-1-schnell answers JPEG while\n * every other model here answers PNG).\n *\n * @experimental This surface is experimental and may change.\n */\n\nimport type { CompatWarning } from \"./chat-completions\";\n\n/**\n * `flux-1-schnell` takes `{ prompt, steps }` and nothing else — no size, no\n * seed, no guidance. Sending those anyway would be silently ignored, so they\n * come back as warnings instead.\n *\n * @see https://developers.cloudflare.com/workers-ai/models/flux-1-schnell/\n */\nconst PROMPT_AND_STEPS_ONLY = /^@cf\\/black-forest-labs\\/flux-1/;\n\n/**\n * The generation knobs Workers AI text-to-image models share, in this\n * package's own spelling. Each module maps its own call options onto them.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface ImageGenerationSettings {\n width?: number;\n height?: number;\n /** Diffusion steps. Sent as `num_steps`, or `steps` on the flux-1 models. */\n steps?: number;\n /** Classifier-free guidance scale. */\n guidance?: number;\n /** How strongly img2img transforms the input image, between 0 and 1. */\n strength?: number;\n negativePrompt?: string;\n seed?: number;\n /**\n * Whether the caller has a source image to attach. The field it goes in\n * differs per module, so only the warning is decided here.\n */\n sourceImage?: boolean;\n /** Whether the caller has an inpainting mask to attach. */\n mask?: boolean;\n}\n\n/**\n * A run-path image body with the knobs the model could not take reported.\n *\n * @experimental This surface is experimental and may change.\n */\nexport interface BuiltImageRequest {\n input: Record<string, unknown>;\n warnings: CompatWarning[];\n /**\n * The model takes `{ prompt, steps }` only, so the caller must not attach a\n * source image or a mask either.\n */\n minimal: boolean;\n}\n\n/**\n * Builds the run-path body for a text-to-image model: `prompt`, `width`,\n * `height`, `num_steps`, `guidance`, `strength`, `negative_prompt` and `seed`,\n * or the flux-1 two-key subset with a warning for everything dropped.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function buildImageInput(\n modelId: string,\n prompt: string,\n settings: ImageGenerationSettings\n): BuiltImageRequest {\n const warnings: CompatWarning[] = [];\n const input: Record<string, unknown> = { prompt };\n\n if (PROMPT_AND_STEPS_ONLY.test(modelId)) {\n if (settings.steps !== undefined) input.steps = settings.steps;\n for (const [feature, value] of [\n [\"size\", settings.width ?? settings.height],\n [\"seed\", settings.seed],\n [\"guidance\", settings.guidance],\n [\"negativePrompt\", settings.negativePrompt],\n [\"strength\", settings.strength],\n [\"files\", settings.sourceImage === true ? true : undefined],\n [\"mask\", settings.mask === true ? true : undefined]\n ] as const) {\n if (value !== undefined) {\n warnings.push({\n feature,\n message: `${modelId} takes only a prompt and steps.`\n });\n }\n }\n return { input, minimal: true, warnings };\n }\n\n if (settings.width !== undefined) input.width = settings.width;\n if (settings.height !== undefined) input.height = settings.height;\n if (settings.seed !== undefined) input.seed = settings.seed;\n if (settings.steps !== undefined) input.num_steps = settings.steps;\n if (settings.guidance !== undefined) input.guidance = settings.guidance;\n if (settings.strength !== undefined) input.strength = settings.strength;\n if (settings.negativePrompt !== undefined) {\n input.negative_prompt = settings.negativePrompt;\n }\n return { input, minimal: false, warnings };\n}\n\nconst JPEG_PREFIX = \"/9j/\";\nconst PNG_PREFIX = \"iVBORw0KGgo\";\nconst WEBP_PREFIX = \"UklGR\";\nconst GIF_PREFIX = \"R0lGOD\";\n\n/**\n * Sniffs an image media type from the head of its base64. The bytes decide,\n * never the model name: `flux-1-schnell` answers JPEG where every other model\n * in this catalog answers PNG.\n *\n * @experimental This surface is experimental and may change.\n */\nexport function imageMediaTypeOf(base64: string): string {\n if (base64.startsWith(JPEG_PREFIX)) return \"image/jpeg\";\n if (base64.startsWith(PNG_PREFIX)) return \"image/png\";\n if (base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-1,7 +1,7 @@
1
1
  import {
2
2
  s as LifecycleCapability,
3
3
  x as Connection
4
- } from "./capability-runner-DBAnGr6L.js";
4
+ } from "./capability-runner-O5VbQzqg.js";
5
5
  //#region src/state/index.d.ts
6
6
  /**
7
7
  * Source of a state change: `"server"` for host code (e.g. `setState()`), or
@@ -75,4 +75,4 @@ declare class State<T = unknown> extends LifecycleCapability {
75
75
  }
76
76
  //#endregion
77
77
  export { StateChangeSource as n, StateOptions as r, State as t };
78
- //# sourceMappingURL=index-BreL0YrQ.d.ts.map
78
+ //# sourceMappingURL=index-D0A5tbWu.d.ts.map
@@ -0,0 +1,200 @@
1
+ import {
2
+ r as CapabilityWebSocketUpgradeContext,
3
+ s as LifecycleCapability,
4
+ x as Connection
5
+ } from "./capability-runner-O5VbQzqg.js";
6
+ import {
7
+ i as WebSocketsOptions,
8
+ n as WebSocketHandlers,
9
+ r as WebSocketMessage
10
+ } from "./options-pwTNq9z6.js";
11
+
12
+ //#region src/websockets/websockets.d.ts
13
+ /**
14
+ * Opt-in WebSocket support for Lifecycle Objects.
15
+ *
16
+ * Lifecycle itself does not model WebSockets — hosts that want them
17
+ * install this capability, which owns the connection subsystem end to
18
+ * end: it claims upgrades, dispatches `onConnect`/`onMessage`/`onClose`
19
+ * inside the host invocation boundary, reciprocates close handshakes,
20
+ * and answers `getConnections()`/`getConnection()`.
21
+ *
22
+ * ```ts
23
+ * class Room extends DurableObject<Env> {
24
+ * readonly webSockets = new WebSockets({
25
+ * handlers: {
26
+ * onConnect: (connection) => connection.send("welcome"),
27
+ * onMessage: (connection, message) => { ... }
28
+ * },
29
+ * callables: new RoomCallables()
30
+ * });
31
+ * readonly lifecycle = Lifecycle.install(this).use(this.webSockets);
32
+ * }
33
+ * ```
34
+ *
35
+ * Connections arrive on one of two wires, chosen by the client:
36
+ *
37
+ * - **cf-websocket** (default): accepted with the Hibernation API. Idle
38
+ * clients stay connected while the Durable Object leaves memory.
39
+ * - **capnweb** (`?__agents_transport=capnweb`): protocol frames through
40
+ * one pipe method on a Cap'n Web session whose root also carries the
41
+ * host's callables natively. Non-hibernating — the object stays pinned
42
+ * while the connection is open.
43
+ *
44
+ * Both wires dispatch the same handlers and appear in `getConnections()`.
45
+ * On both, the capability speaks the Agent protocol a plain host needs
46
+ * for `useAgent` and `AgentClient`: it sends the identity frame on
47
+ * connect, and it serves `callables` — as `rpc` JSON frames on the
48
+ * WebSocket wire, and natively on the Cap'n Web session root, where an
49
+ * `RpcTarget` result becomes a live stub and calls pipeline. `call()`
50
+ * and `stub` work against a plain Durable Object exactly as against an
51
+ * `Agent`. Pass a `State` capability as `state` and the hook's
52
+ * `state`/`setState` work too: the current value is pushed on connect
53
+ * and client updates are validated and applied; the state owner
54
+ * broadcasts changes with `broadcastState()`. Per-connection readonly
55
+ * and no-protocol flags live here as well, for every host.
56
+ *
57
+ * @experimental The API surface may change before stabilizing.
58
+ */
59
+ declare class WebSockets extends LifecycleCapability {
60
+ #private;
61
+ /** Claims every upgrade, so Lifecycle dispatches it after all others. */
62
+ readonly claims = "catch-all";
63
+ constructor(options?: WebSocketsOptions);
64
+ /**
65
+ * Claim every upgrade, never declining: a Cap'n Web transport upgrade
66
+ * becomes a session, everything else a tracked hibernating connection,
67
+ * whether or not handlers or callables are configured. Handlers only add
68
+ * behavior on connect, message, close, and error.
69
+ */
70
+ onWebSocketUpgrade({
71
+ request
72
+ }: CapabilityWebSocketUpgradeContext): Promise<Response>;
73
+ /** Dispatch a platform message wake for a capability-owned socket. */
74
+ onWebSocketMessage(
75
+ ws: WebSocket,
76
+ message: WebSocketMessage
77
+ ): Promise<boolean>;
78
+ /** Dispatch and reciprocate a close wake for an owned socket. */
79
+ onWebSocketClose(
80
+ ws: WebSocket,
81
+ code: number,
82
+ reason: string,
83
+ wasClean: boolean
84
+ ): Promise<boolean>;
85
+ /** Dispatch an error wake for an owned socket. */
86
+ onWebSocketError(ws: WebSocket, error: unknown): Promise<boolean>;
87
+ /**
88
+ * Close every owned connection during explicit host destruction. The
89
+ * capability owns its sockets' lifetimes, so it also owns tearing
90
+ * them down.
91
+ */
92
+ dispose(): void;
93
+ /**
94
+ * Add handlers for another component sharing these connections, such as
95
+ * a protocol of its own. They run before the configured handlers, and
96
+ * their `onMessage` can claim a message by returning true.
97
+ */
98
+ use(handlers: WebSocketHandlers): void;
99
+ /** Open connections on either wire, optionally by tag. */
100
+ getConnections<TState = unknown>(
101
+ tag?: string
102
+ ): IterableIterator<Connection<TState>>;
103
+ /** One connection on either wire, by id. */
104
+ getConnection<TState = unknown>(id: string): Connection<TState> | undefined;
105
+ /**
106
+ * Send the connect sequence: the identity frame, then the current state
107
+ * when one is stored. The identity carries `stateFollows` in that case,
108
+ * so clients resolve `ready` only once the state has landed. Nothing is
109
+ * sent to a no-protocol connection. Identity defaults as for
110
+ * `sendIdentity()`.
111
+ */
112
+ sendConnectFrames(
113
+ connection: Connection,
114
+ identity?: {
115
+ name: string;
116
+ agent: string;
117
+ }
118
+ ): void;
119
+ /**
120
+ * Send the identity frame alone, unless the connection is no-protocol.
121
+ * Clients resolve `ready` on it, so a host that also pushes state on
122
+ * connect should use `sendConnectFrames()`. The defaults are the Durable
123
+ * Object's routed name and host class; a host whose public identity
124
+ * differs — an `Agent` facet, whose routed name is an internal encoding
125
+ * of its logical name — passes its own.
126
+ */
127
+ sendIdentity(
128
+ connection: Connection,
129
+ identity?: {
130
+ name: string;
131
+ agent: string;
132
+ }
133
+ ): void;
134
+ /**
135
+ * Send the current state to one connection, unless nothing is stored
136
+ * or the connection is no-protocol. Reading the state may seed the
137
+ * initial value; see `#connecting`.
138
+ */
139
+ sendState(connection: Connection): void;
140
+ /**
141
+ * Apply a parsed `cf_agent_state` frame from a client. A readonly
142
+ * connection is refused; a change the host's validator rejects is
143
+ * logged in full server-side and answered with a generic
144
+ * `cf_agent_state_error`. Broadcasting the accepted change is the state
145
+ * owner's call, through its `onChanged` hook. Callers that drive the
146
+ * protocol themselves call this inside their own invocation context;
147
+ * the capability's automatic path does so via `runInHostContext`.
148
+ *
149
+ * @returns Whether the frame was a state frame (handled either way).
150
+ */
151
+ applyStateFrame(connection: Connection, frame: unknown): boolean;
152
+ /**
153
+ * Push the current state to every protocol-enabled connection except
154
+ * the one a change came from, which already has the value it sent.
155
+ * Wire a `State`'s `onChanged` to this.
156
+ */
157
+ broadcastState(except?: Connection | "server"): void;
158
+ /** Whether the connection may update host state over the wire. */
159
+ isReadonly(connection: Connection): boolean;
160
+ /** Mark a connection readonly, or writable again. */
161
+ setReadonly(connection: Connection, readonly?: boolean): void;
162
+ /** Whether protocol text frames reach the connection. */
163
+ isProtocolEnabled(connection: Connection): boolean;
164
+ /** Enable or suppress protocol text frames for a connection. */
165
+ setProtocolEnabled(connection: Connection, enabled: boolean): void;
166
+ }
167
+ //#endregion
168
+ //#region src/websockets/connection-flags.d.ts
169
+ /**
170
+ * Internal per-connection flags, stored inside the connection's own state
171
+ * under `_cf_`-prefixed keys so they ride the hibernation attachment (or a
172
+ * bridged connection's synced state) and survive a wake. The state wrapper
173
+ * installed by {@link ensureConnectionWrapped} hides them from
174
+ * `connection.state` and preserves them across user `setState` calls.
175
+ *
176
+ * Owned by the WebSockets capability; `Agent` and framework mixins reach it
177
+ * through the capability or, for their own keys, through
178
+ * {@link registerInternalConnectionKeys}.
179
+ */
180
+ /** A readonly connection may not update host state. */
181
+ declare const CF_READONLY_KEY = "_cf_readonly";
182
+ /**
183
+ * A no-protocol connection receives no protocol text frames (identity,
184
+ * state sync, MCP servers) — neither on connect nor via broadcasts.
185
+ */
186
+ declare const CF_NO_PROTOCOL_KEY = "_cf_no_protocol";
187
+ /**
188
+ * Register additional `_cf_`-prefixed keys a host or mixin stores in
189
+ * connection state, so they are hidden from `connection.state` and kept
190
+ * across user `setState` calls like the capability's own flags.
191
+ */
192
+ declare function registerInternalConnectionKeys(...keys: string[]): void;
193
+ //#endregion
194
+ export {
195
+ WebSockets as i,
196
+ CF_READONLY_KEY as n,
197
+ registerInternalConnectionKeys as r,
198
+ CF_NO_PROTOCOL_KEY as t
199
+ };
200
+ //# sourceMappingURL=index-Ib_4dwOo.d.ts.map
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  S as ConnectionContext,
4
4
  T as WSMessage,
5
5
  x as Connection
6
- } from "./capability-runner-DBAnGr6L.js";
6
+ } from "./capability-runner-O5VbQzqg.js";
7
7
  import {
8
8
  a as isDurableObjectStorageReset,
9
9
  c as isPlatformTransientError,
@@ -11,7 +11,7 @@ import {
11
11
  r as isDurableObjectMemoryLimitReset,
12
12
  t as RetryOptions
13
13
  } from "./retries-CV6AkXmQ.js";
14
- import { i as __DO_NOT_USE_WILL_BREAK__agentContext } from "./current-agent-vAk_5JMt.js";
14
+ import { i as __DO_NOT_USE_WILL_BREAK__agentContext } from "./current-agent-C07C5o5Y.js";
15
15
  import {
16
16
  $ as ChatCapableAgentClass,
17
17
  A as getCurrentAgent,
@@ -86,7 +86,7 @@ import {
86
86
  xn as DynamicAgentStub,
87
87
  y as FiberStatus,
88
88
  z as AgentToolEventState
89
- } from "./agent-routing-0DR0Tk2v.js";
89
+ } from "./agent-routing-Bhdbvf7s.js";
90
90
  import {
91
91
  a as EmailSendBinding,
92
92
  d as createHeaderBasedEmailResolver,
@@ -97,14 +97,14 @@ import {
97
97
  n as AgentsOAuthProvider,
98
98
  r as DurableObjectOAuthClientProvider,
99
99
  t as AgentMcpOAuthProvider
100
- } from "./do-oauth-client-provider-Tmf1vgKz.js";
100
+ } from "./do-oauth-client-provider-DfsUcpc3.js";
101
101
  import { f as TransportType } from "./handler-stateless-DxYpJ_XF.js";
102
102
  import { t as MessageType } from "./types-6Zo2zfoO.js";
103
103
  import {
104
104
  a as ScheduleCriteria,
105
105
  i as Schedule,
106
106
  o as ScheduleOptions
107
- } from "./scheduler-RdACNXDP.js";
107
+ } from "./scheduler-CrFCnv_J.js";
108
108
  import { i as QueueItem } from "./types-BA4anrjI.js";
109
109
  import {
110
110
  n as callable,
package/dist/index.js CHANGED
@@ -7,9 +7,9 @@ import { MessageType } from "./types.js";
7
7
  import { createHeaderBasedEmailResolver } from "./email.js";
8
8
  import { n as withInvocationScope } from "./cloudflare-Dzvc7V2N.js";
9
9
  import { SUB_PREFIX, buildAgentPath, buildAgentUrl, getSubAgentByName, parseSubAgentPath, routeSubAgentRequest } from "./sub-routing.js";
10
- import { a as routeAgentEmail, i as getCurrentAgent, n as DEFAULT_AGENT_STATIC_OPTIONS, r as StreamingResponse, t as Agent } from "./src-BZeu-SCD.js";
10
+ import { a as routeAgentEmail, i as getCurrentAgent, n as DEFAULT_AGENT_STATIC_OPTIONS, r as StreamingResponse, t as Agent } from "./src-DgCbZtIR.js";
11
11
  import { isDurableObjectCodeUpdateReset, isDurableObjectMemoryLimitReset, isDurableObjectStorageReset, isPlatformTransientError } from "./retries.js";
12
12
  import { SqlError } from "./sql-error.js";
13
- import { i as normalizeServerId, n as MCP_SERVER_ID_MAX_LENGTH } from "./client-fyQrLGkD.js";
13
+ import { i as normalizeServerId, n as MCP_SERVER_ID_MAX_LENGTH } from "./client-1tEQ1Oqz.js";
14
14
  import { DurableObjectOAuthClientProvider } from "./mcp/client/do-oauth-client-provider.js";
15
15
  export { AGENT_TOOL_MILESTONE_PART, AGENT_TOOL_PROGRESS_PART, Agent, DEFAULT_AGENT_STATIC_OPTIONS, DurableObjectOAuthClientProvider, MCP_SERVER_ID_MAX_LENGTH, MessageType, SUB_PREFIX, SqlError, StreamingResponse, __DO_NOT_USE_WILL_BREAK__agentContext, withInvocationScope as __DO_NOT_USE_WILL_BREAK__withInvocationScope, buildAgentPath, buildAgentUrl, callable, camelCaseToKebabCase, createHeaderBasedEmailResolver, getAgentByName, getCurrentAgent, getSubAgentByName, isDurableObjectCodeUpdateReset, isDurableObjectMemoryLimitReset, isDurableObjectStorageReset, isPlatformTransientError, normalizeServerId, parseSubAgentPath, routeAgentEmail, routeAgentRequest, routeSubAgentRequest, unstable_callable };
@@ -0,0 +1,9 @@
1
+ //#region src/experimental/channels/ingress.ts
2
+ /** Match an HTTP request to one exact URL pathname. */
3
+ function matchesPath(request, path) {
4
+ return new URL(request.url).pathname === path;
5
+ }
6
+ //#endregion
7
+ export { matchesPath as t };
8
+
9
+ //# sourceMappingURL=ingress-EFbrS547.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ingress-EFbrS547.js","names":[],"sources":["../src/experimental/channels/ingress.ts"],"sourcesContent":["import type { ChannelIdentity, ChannelIdentityInput } from \"./identity\";\nimport type {\n ChannelMessageSurface,\n ChannelMessageSurfaceInput\n} from \"./surface\";\n\n/** Serializable provider attachment facts available after ingress returns. */\nexport type ChannelAttachment = {\n id?: string;\n mediaType?: string;\n name?: string;\n size?: number;\n text?: string;\n url?: string;\n};\n\nexport type ChannelActor = {\n readonly id: string;\n /** Stable Channel identity suitable for explicit application linking. */\n readonly identity?: ChannelIdentity;\n readonly username?: string;\n readonly fullName?: string;\n readonly isBot?: boolean | \"unknown\";\n readonly isSelf?: boolean;\n};\n\n/** An actor before the Host stamps its configured Channel key. */\nexport type ChannelActorInput = Omit<ChannelActor, \"identity\"> & {\n readonly identity?: ChannelIdentityInput;\n};\n\nexport type ChannelEventContext = {\n /** Immutable Channel-scoped identity, independent of application routing. */\n readonly eventId: string;\n readonly thread: {\n readonly id: string;\n readonly isDirectMessage: boolean | \"unknown\";\n };\n /** Exact provider destination for responding to this inbound event. */\n readonly replySurface?: ChannelMessageSurface;\n readonly actor?: ChannelActor;\n};\n\n/** A normalized inbound message from a Channel. */\nexport type ChannelInboundMessage = ChannelEventContext & {\n readonly type: \"message\";\n readonly message: {\n readonly id: string;\n readonly text: string;\n readonly title?: string;\n readonly markdown?: string;\n readonly attachments?: readonly ChannelAttachment[];\n readonly isMention?: boolean;\n readonly reply?: {\n readonly id: string;\n readonly text?: string;\n };\n readonly metadata?: {\n /** ISO 8601 timestamp supplied by the provider. */\n readonly sentAt?: string;\n readonly edited?: boolean;\n /** ISO 8601 timestamp supplied by the provider. */\n readonly editedAt?: string;\n readonly autoReply?: boolean;\n };\n };\n};\n\n/** A provider-normalized response to an external approval request. */\nexport type ChannelApprovalResponse = ChannelEventContext & {\n readonly type: \"approval-response\";\n readonly approvalId: string;\n readonly decision: \"approve\" | \"reject\";\n /** Provider reference for this inbound response. */\n readonly reference: string;\n};\n\nexport type ChannelIngressEvent =\n | ChannelInboundMessage\n | ChannelApprovalResponse;\n\n/** A normalized message before the Host stamps its configured Channel key. */\nexport type ChannelEventContextInput = Omit<\n ChannelEventContext,\n \"replySurface\" | \"actor\"\n> & {\n readonly replySurface?: ChannelMessageSurfaceInput;\n readonly actor?: ChannelActorInput;\n};\n\nexport type ChannelInboundMessageInput = Omit<\n ChannelInboundMessage,\n \"replySurface\" | \"actor\"\n> & {\n readonly replySurface?: ChannelMessageSurfaceInput;\n readonly actor?: ChannelActorInput;\n};\n\n/** A normalized approval before the Host stamps its configured Channel key. */\nexport type ChannelApprovalResponseInput = Omit<\n ChannelApprovalResponse,\n \"replySurface\" | \"actor\"\n> & {\n readonly replySurface?: ChannelMessageSurfaceInput;\n readonly actor?: ChannelActorInput;\n};\n\n/** Authenticated event produced by a Channel before Host dispatch. */\nexport type ChannelIngressEventInput =\n | ChannelInboundMessageInput\n | ChannelApprovalResponseInput;\n\n/** Authenticated Channel output retained only until Host routing completes. */\nexport type ChannelIngressEnvelope<TRaw = unknown> = {\n event: ChannelIngressEventInput;\n raw: TRaw;\n};\n\n/** The normalized envelopes and provider acknowledgement produced by ingress. */\nexport type ChannelIngressResult<TRaw = unknown> = {\n events: readonly ChannelIngressEnvelope<TRaw>[];\n response: Response;\n};\n\n/** Match an HTTP request to one exact URL pathname. */\nexport function matchesPath(request: Request, path: string): boolean {\n return new URL(request.url).pathname === path;\n}\n\n/** Webhook-shaped inbound support owned by a Channel. */\nexport interface ChannelIngress<TRaw = unknown> {\n /** Return null when this Channel does not own the request. */\n receive(request: Request): Promise<ChannelIngressResult<TRaw> | null>;\n}\n\n/** Structural input supported by Workers Email and Agent email handlers. */\nexport type ChannelEmailInput = {\n from: string;\n to: string;\n headers: Headers;\n raw?: ReadableStream<Uint8Array>;\n getRaw?: () => Promise<Uint8Array>;\n};\n\nexport type ChannelEmailIngressResult<TRaw = unknown> = {\n events: readonly ChannelIngressEnvelope<TRaw>[];\n};\n\n/** Non-HTTP ingress for a Workers Email event. */\nexport interface ChannelEmailIngress<TRaw = unknown> {\n /** Return null when this Channel does not own the email. */\n receive(\n email: ChannelEmailInput\n ): Promise<ChannelEmailIngressResult<TRaw> | null>;\n}\n"],"mappings":";;AA6HA,SAAgB,YAAY,SAAkB,MAAuB;CACnE,OAAO,IAAI,IAAI,QAAQ,GAAG,CAAC,CAAC,aAAa;AAC3C"}
@@ -0,0 +1,23 @@
1
+ import { u as MAX_WEB_SEARCH_QUERY_LENGTH } from "./tool-3PA8xjB_.js";
2
+ import { z } from "zod";
3
+ //#region src/websearch/tools/input-schema.ts
4
+ /**
5
+ * The zod input schema shared by the AI SDK and TanStack AI adapters. Kept
6
+ * out of the core so the pi adapter, which uses TypeBox, does not need zod.
7
+ * Internal — not an entry point.
8
+ */
9
+ /**
10
+ * The model's input schema. The description tells the model the host's
11
+ * `limit`; a larger value is not rejected (which would cost a validation
12
+ * round trip) but capped by the core.
13
+ */
14
+ function webSearchInputSchema(maxLimit) {
15
+ return z.object({
16
+ query: z.string().min(1).max(MAX_WEB_SEARCH_QUERY_LENGTH).describe("What to search for."),
17
+ limit: z.number().int().min(1).optional().describe(`How many results to return (at most ${maxLimit}).`)
18
+ });
19
+ }
20
+ //#endregion
21
+ export { webSearchInputSchema as t };
22
+
23
+ //# sourceMappingURL=input-schema-DNrpTu49.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-schema-DNrpTu49.js","names":[],"sources":["../src/websearch/tools/input-schema.ts"],"sourcesContent":["/**\n * The zod input schema shared by the AI SDK and TanStack AI adapters. Kept\n * out of the core so the pi adapter, which uses TypeBox, does not need zod.\n * Internal — not an entry point.\n */\nimport { z } from \"zod\";\nimport {\n MAX_WEB_SEARCH_QUERY_LENGTH,\n type WebSearchToolInput\n} from \"../contract\";\n\n/**\n * The model's input schema. The description tells the model the host's\n * `limit`; a larger value is not rejected (which would cost a validation\n * round trip) but capped by the core.\n */\nexport function webSearchInputSchema(maxLimit: number) {\n return z.object({\n query: z\n .string()\n .min(1)\n .max(MAX_WEB_SEARCH_QUERY_LENGTH)\n .describe(\"What to search for.\"),\n limit: z\n .number()\n .int()\n .min(1)\n .optional()\n .describe(`How many results to return (at most ${maxLimit}).`)\n }) satisfies z.ZodType<WebSearchToolInput>;\n}\n"],"mappings":";;;;;;;;;;;;;AAgBA,SAAgB,qBAAqB,UAAkB;CACrD,OAAO,EAAE,OAAO;EACd,OAAO,EACJ,OAAO,CAAC,CACR,IAAI,CAAC,CAAC,CACN,IAAI,2BAA2B,CAAC,CAChC,SAAS,qBAAqB;EACjC,OAAO,EACJ,OAAO,CAAC,CACR,IAAI,CAAC,CACL,IAAI,CAAC,CAAC,CACN,SAAS,CAAC,CACV,SAAS,uCAAuC,SAAS,GAAG;CACjE,CAAC;AACH"}
@@ -0,0 +1,46 @@
1
+ //#region src/experimental/channels/internal.ts
2
+ const textEncoder = new TextEncoder();
3
+ function encodeUtf8(value) {
4
+ return textEncoder.encode(value);
5
+ }
6
+ function utf8ByteLength(value) {
7
+ return encodeUtf8(value).byteLength;
8
+ }
9
+ function isRecord(value) {
10
+ return value !== null && typeof value === "object" && !Array.isArray(value);
11
+ }
12
+ function emptyIngressResponse(status = 200) {
13
+ return {
14
+ events: [],
15
+ response: new Response(null, { status })
16
+ };
17
+ }
18
+ function unsupported(code, message) {
19
+ return {
20
+ status: "failed",
21
+ retryable: false,
22
+ error: {
23
+ code,
24
+ message
25
+ }
26
+ };
27
+ }
28
+ /** The agent object of a participant's own, namespaced so app routes cannot collide. */
29
+ function participantRoute(participant) {
30
+ return `participant:${participant.id}`;
31
+ }
32
+ /** Check what an application's `participant` callback returned. */
33
+ function toParticipant(result, source) {
34
+ if (result === null) return null;
35
+ if (typeof result === "string") {
36
+ if (result.length > 0) return { id: result };
37
+ } else if (isRecord(result) && typeof result.id === "string" && result.id.length > 0 && (result.name === void 0 || typeof result.name === "string")) return result.name === void 0 ? { id: result.id } : {
38
+ id: result.id,
39
+ name: result.name
40
+ };
41
+ throw new Error(`${source} participant must return a non-empty id, a participant, or null`);
42
+ }
43
+ //#endregion
44
+ export { toParticipant as a, participantRoute as i, encodeUtf8 as n, unsupported as o, isRecord as r, utf8ByteLength as s, emptyIngressResponse as t };
45
+
46
+ //# sourceMappingURL=internal-kmw70xwR.js.map