@e2edev/e2e 0.10.0 → 0.12.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 (269) hide show
  1. package/README.md +14 -8
  2. package/dist/agent/act-validation.d.ts +2 -0
  3. package/dist/agent/act-validation.d.ts.map +1 -1
  4. package/dist/agent/act-validation.js +1 -1
  5. package/dist/agent/act-validation.js.map +1 -1
  6. package/dist/agent/act.d.ts +30 -2
  7. package/dist/agent/act.d.ts.map +1 -1
  8. package/dist/agent/act.js +18 -4
  9. package/dist/agent/act.js.map +1 -1
  10. package/dist/agent/ai-sdk.d.ts +2 -2
  11. package/dist/agent/ai-sdk.d.ts.map +1 -1
  12. package/dist/agent/ai-sdk.js +3 -4
  13. package/dist/agent/ai-sdk.js.map +1 -1
  14. package/dist/agent/default-agent.d.ts +33 -9
  15. package/dist/agent/default-agent.d.ts.map +1 -1
  16. package/dist/agent/default-agent.js +46 -31
  17. package/dist/agent/default-agent.js.map +1 -1
  18. package/dist/agent/error.d.ts.map +1 -1
  19. package/dist/agent/error.js +1 -0
  20. package/dist/agent/error.js.map +1 -1
  21. package/dist/agent/executor.d.ts +13 -0
  22. package/dist/agent/executor.d.ts.map +1 -1
  23. package/dist/agent/executor.js.map +1 -1
  24. package/dist/agent/interactive-step.d.ts +38 -0
  25. package/dist/agent/interactive-step.d.ts.map +1 -0
  26. package/dist/agent/interactive-step.js +131 -0
  27. package/dist/agent/interactive-step.js.map +1 -0
  28. package/dist/agent/model/adapter.d.ts +3 -0
  29. package/dist/agent/model/adapter.d.ts.map +1 -1
  30. package/dist/agent/model/adapter.js.map +1 -1
  31. package/dist/agent/model/overflow.d.ts +20 -0
  32. package/dist/agent/model/overflow.d.ts.map +1 -0
  33. package/dist/agent/model/overflow.js +82 -0
  34. package/dist/agent/model/overflow.js.map +1 -0
  35. package/dist/agent/model/prompt-cache.d.ts +47 -0
  36. package/dist/agent/model/prompt-cache.d.ts.map +1 -0
  37. package/dist/agent/model/prompt-cache.js +78 -0
  38. package/dist/agent/model/prompt-cache.js.map +1 -0
  39. package/dist/agent/model/sdk.d.ts +41 -24
  40. package/dist/agent/model/sdk.d.ts.map +1 -1
  41. package/dist/agent/model/sdk.js +64 -91
  42. package/dist/agent/model/sdk.js.map +1 -1
  43. package/dist/agent/model/tool-choice.d.ts +21 -0
  44. package/dist/agent/model/tool-choice.d.ts.map +1 -0
  45. package/dist/agent/model/tool-choice.js +49 -0
  46. package/dist/agent/model/tool-choice.js.map +1 -0
  47. package/dist/agent/primitives.d.ts.map +1 -1
  48. package/dist/agent/primitives.js +2 -1
  49. package/dist/agent/primitives.js.map +1 -1
  50. package/dist/agent/public.d.ts +1 -1
  51. package/dist/agent/public.d.ts.map +1 -1
  52. package/dist/agent/public.js.map +1 -1
  53. package/dist/agent/screen-update.d.ts +8 -3
  54. package/dist/agent/screen-update.d.ts.map +1 -1
  55. package/dist/agent/screen-update.js +18 -5
  56. package/dist/agent/screen-update.js.map +1 -1
  57. package/dist/agent/tool-loop.d.ts +4 -4
  58. package/dist/agent/tool-loop.d.ts.map +1 -1
  59. package/dist/agent/tool-loop.js +241 -48
  60. package/dist/agent/tool-loop.js.map +1 -1
  61. package/dist/agent/tool-output.d.ts +31 -0
  62. package/dist/agent/tool-output.d.ts.map +1 -0
  63. package/dist/agent/tool-output.js +60 -0
  64. package/dist/agent/tool-output.js.map +1 -0
  65. package/dist/agent/tool.d.ts +9 -3
  66. package/dist/agent/tool.d.ts.map +1 -1
  67. package/dist/agent/tool.js +2 -2
  68. package/dist/agent/tool.js.map +1 -1
  69. package/dist/agent/usage.d.ts +7 -0
  70. package/dist/agent/usage.d.ts.map +1 -1
  71. package/dist/agent/usage.js +11 -0
  72. package/dist/agent/usage.js.map +1 -1
  73. package/dist/cli/docs-url.d.ts +1 -1
  74. package/dist/cli/docs-url.d.ts.map +1 -1
  75. package/dist/cli/docs-url.js +1 -1
  76. package/dist/cli/docs-url.js.map +1 -1
  77. package/dist/cli/index.d.ts.map +1 -1
  78. package/dist/cli/index.js +139 -35
  79. package/dist/cli/index.js.map +1 -1
  80. package/dist/cli/init/engine-versions.json +1 -1
  81. package/dist/cli/init/engines.d.ts +17 -16
  82. package/dist/cli/init/engines.d.ts.map +1 -1
  83. package/dist/cli/init/engines.js +26 -26
  84. package/dist/cli/init/engines.js.map +1 -1
  85. package/dist/cli/init/gateways.d.ts +22 -0
  86. package/dist/cli/init/gateways.d.ts.map +1 -0
  87. package/dist/cli/init/gateways.js +50 -0
  88. package/dist/cli/init/gateways.js.map +1 -0
  89. package/dist/cli/init/mcp-config.d.ts +31 -0
  90. package/dist/cli/init/mcp-config.d.ts.map +1 -0
  91. package/dist/cli/init/mcp-config.js +72 -0
  92. package/dist/cli/init/mcp-config.js.map +1 -0
  93. package/dist/cli/init/scaffold.d.ts +8 -2
  94. package/dist/cli/init/scaffold.d.ts.map +1 -1
  95. package/dist/cli/init/scaffold.js +22 -11
  96. package/dist/cli/init/scaffold.js.map +1 -1
  97. package/dist/cli/init.d.ts.map +1 -1
  98. package/dist/cli/init.js +109 -29
  99. package/dist/cli/init.js.map +1 -1
  100. package/dist/cli/mcp.d.ts +15 -0
  101. package/dist/cli/mcp.d.ts.map +1 -0
  102. package/dist/cli/mcp.js +63 -0
  103. package/dist/cli/mcp.js.map +1 -0
  104. package/dist/collect/collect.d.ts +7 -0
  105. package/dist/collect/collect.d.ts.map +1 -1
  106. package/dist/collect/collect.js +16 -0
  107. package/dist/collect/collect.js.map +1 -1
  108. package/dist/collect/registry.d.ts.map +1 -1
  109. package/dist/collect/registry.js +33 -6
  110. package/dist/collect/registry.js.map +1 -1
  111. package/dist/collect/select.d.ts +9 -2
  112. package/dist/collect/select.d.ts.map +1 -1
  113. package/dist/collect/select.js +57 -11
  114. package/dist/collect/select.js.map +1 -1
  115. package/dist/config/agent.d.ts +13 -32
  116. package/dist/config/agent.d.ts.map +1 -1
  117. package/dist/config/agent.js +32 -123
  118. package/dist/config/agent.js.map +1 -1
  119. package/dist/config/resolve.d.ts +8 -5
  120. package/dist/config/resolve.d.ts.map +1 -1
  121. package/dist/config/resolve.js +11 -9
  122. package/dist/config/resolve.js.map +1 -1
  123. package/dist/credentials.d.ts +6 -0
  124. package/dist/credentials.d.ts.map +1 -1
  125. package/dist/credentials.js +9 -0
  126. package/dist/credentials.js.map +1 -1
  127. package/dist/explore/body.d.ts +36 -0
  128. package/dist/explore/body.d.ts.map +1 -0
  129. package/dist/explore/body.js +168 -0
  130. package/dist/explore/body.js.map +1 -0
  131. package/dist/explore/executor.d.ts +25 -0
  132. package/dist/explore/executor.d.ts.map +1 -0
  133. package/dist/explore/executor.js +86 -0
  134. package/dist/explore/executor.js.map +1 -0
  135. package/dist/explore/index.d.ts +64 -0
  136. package/dist/explore/index.d.ts.map +1 -0
  137. package/dist/explore/index.js +185 -0
  138. package/dist/explore/index.js.map +1 -0
  139. package/dist/explore/plan.d.ts +74 -0
  140. package/dist/explore/plan.d.ts.map +1 -0
  141. package/dist/explore/plan.js +149 -0
  142. package/dist/explore/plan.js.map +1 -0
  143. package/dist/explore/reporter.d.ts +11 -0
  144. package/dist/explore/reporter.d.ts.map +1 -0
  145. package/dist/explore/reporter.js +54 -0
  146. package/dist/explore/reporter.js.map +1 -0
  147. package/dist/explore/state.d.ts +58 -0
  148. package/dist/explore/state.d.ts.map +1 -0
  149. package/dist/explore/state.js +147 -0
  150. package/dist/explore/state.js.map +1 -0
  151. package/dist/internal/ai-trace.d.ts +2 -0
  152. package/dist/internal/ai-trace.d.ts.map +1 -1
  153. package/dist/internal/ai-trace.js +3 -0
  154. package/dist/internal/ai-trace.js.map +1 -1
  155. package/dist/internal/ids.d.ts +2 -2
  156. package/dist/internal/ids.d.ts.map +1 -1
  157. package/dist/internal/ids.js +3 -3
  158. package/dist/internal/ids.js.map +1 -1
  159. package/dist/internal/package-manager.d.ts +4 -0
  160. package/dist/internal/package-manager.d.ts.map +1 -1
  161. package/dist/internal/package-manager.js +26 -0
  162. package/dist/internal/package-manager.js.map +1 -1
  163. package/dist/mcp/catalog.d.ts +50 -0
  164. package/dist/mcp/catalog.d.ts.map +1 -0
  165. package/dist/mcp/catalog.js +172 -0
  166. package/dist/mcp/catalog.js.map +1 -0
  167. package/dist/mcp/config.d.ts +11 -0
  168. package/dist/mcp/config.d.ts.map +1 -0
  169. package/dist/mcp/config.js +16 -0
  170. package/dist/mcp/config.js.map +1 -0
  171. package/dist/mcp/server.d.ts +29 -0
  172. package/dist/mcp/server.d.ts.map +1 -0
  173. package/dist/mcp/server.js +86 -0
  174. package/dist/mcp/server.js.map +1 -0
  175. package/dist/mcp/session.d.ts +68 -0
  176. package/dist/mcp/session.d.ts.map +1 -0
  177. package/dist/mcp/session.js +317 -0
  178. package/dist/mcp/session.js.map +1 -0
  179. package/dist/mcp/tools.d.ts +77 -0
  180. package/dist/mcp/tools.d.ts.map +1 -0
  181. package/dist/mcp/tools.js +179 -0
  182. package/dist/mcp/tools.js.map +1 -0
  183. package/dist/report/build.d.ts +61 -0
  184. package/dist/report/build.d.ts.map +1 -1
  185. package/dist/report/build.js +13 -2
  186. package/dist/report/build.js.map +1 -1
  187. package/dist/report/debug-steps.d.ts.map +1 -1
  188. package/dist/report/debug-steps.js +15 -0
  189. package/dist/report/debug-steps.js.map +1 -1
  190. package/dist/report/format.d.ts +30 -1
  191. package/dist/report/format.d.ts.map +1 -1
  192. package/dist/report/format.js +57 -3
  193. package/dist/report/format.js.map +1 -1
  194. package/dist/report/junit.d.ts.map +1 -1
  195. package/dist/report/junit.js +4 -1
  196. package/dist/report/junit.js.map +1 -1
  197. package/dist/report/list.d.ts +10 -1
  198. package/dist/report/list.d.ts.map +1 -1
  199. package/dist/report/list.js +35 -13
  200. package/dist/report/list.js.map +1 -1
  201. package/dist/run/events.d.ts +13 -4
  202. package/dist/run/events.d.ts.map +1 -1
  203. package/dist/run/events.js.map +1 -1
  204. package/dist/run/execute.d.ts +2 -2
  205. package/dist/run/execute.d.ts.map +1 -1
  206. package/dist/run/execute.js +19 -14
  207. package/dist/run/execute.js.map +1 -1
  208. package/dist/run/fixtures.d.ts +10 -4
  209. package/dist/run/fixtures.d.ts.map +1 -1
  210. package/dist/run/fixtures.js +23 -23
  211. package/dist/run/fixtures.js.map +1 -1
  212. package/dist/run/in-process.d.ts +8 -0
  213. package/dist/run/in-process.d.ts.map +1 -1
  214. package/dist/run/in-process.js +7 -6
  215. package/dist/run/in-process.js.map +1 -1
  216. package/dist/run/provision.d.ts +51 -0
  217. package/dist/run/provision.d.ts.map +1 -0
  218. package/dist/run/provision.js +94 -0
  219. package/dist/run/provision.js.map +1 -0
  220. package/dist/run/records.d.ts +4 -0
  221. package/dist/run/records.d.ts.map +1 -1
  222. package/dist/run/runner.d.ts +25 -3
  223. package/dist/run/runner.d.ts.map +1 -1
  224. package/dist/run/runner.js +38 -81
  225. package/dist/run/runner.js.map +1 -1
  226. package/dist/run/scheduler.d.ts +5 -2
  227. package/dist/run/scheduler.d.ts.map +1 -1
  228. package/dist/run/scheduler.js +13 -10
  229. package/dist/run/scheduler.js.map +1 -1
  230. package/dist/run/serial.d.ts +1 -1
  231. package/dist/run/serial.d.ts.map +1 -1
  232. package/dist/run/serial.js +7 -2
  233. package/dist/run/serial.js.map +1 -1
  234. package/dist/run/standalone.d.ts +63 -0
  235. package/dist/run/standalone.d.ts.map +1 -0
  236. package/dist/run/standalone.js +152 -0
  237. package/dist/run/standalone.js.map +1 -0
  238. package/dist/run/steps.d.ts +7 -0
  239. package/dist/run/steps.d.ts.map +1 -1
  240. package/dist/run/steps.js.map +1 -1
  241. package/dist/run/units.d.ts +6 -3
  242. package/dist/run/units.d.ts.map +1 -1
  243. package/dist/run/units.js +0 -0
  244. package/dist/run/units.js.map +1 -1
  245. package/dist/run/worker/entry.js +2 -2
  246. package/dist/run/worker/entry.js.map +1 -1
  247. package/dist/run/worker/protocol.d.ts +5 -0
  248. package/dist/run/worker/protocol.d.ts.map +1 -1
  249. package/dist/run/worker/protocol.js.map +1 -1
  250. package/dist/run/worker/session.d.ts +3 -4
  251. package/dist/run/worker/session.d.ts.map +1 -1
  252. package/dist/run/worker/session.js +5 -4
  253. package/dist/run/worker/session.js.map +1 -1
  254. package/dist/run/worker-models.d.ts +4 -4
  255. package/dist/run/worker-models.js +6 -13
  256. package/dist/run/worker-models.js.map +1 -1
  257. package/dist/telemetry/events.d.ts.map +1 -1
  258. package/dist/telemetry/events.js +17 -2
  259. package/dist/telemetry/events.js.map +1 -1
  260. package/dist/types.d.ts +20 -20
  261. package/dist/types.d.ts.map +1 -1
  262. package/package.json +9 -6
  263. package/skills/e2e/SKILL.md +16 -6
  264. package/skills/e2e/references/agent.md +21 -16
  265. package/skills/e2e/references/debugging.md +2 -1
  266. package/skills/e2e/references/explore.md +83 -0
  267. package/skills/e2e/references/mcp.md +88 -0
  268. package/skills/e2e/references/running.md +7 -5
  269. package/skills/e2e/references/setup.md +17 -13
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Prompt-cache hints: what a request carries so the provider can serve the
3
+ * repeated prefix (system prompt, tool definitions, the conversation so far)
4
+ * from its cache instead of reading it again at full price.
5
+ *
6
+ * Anthropic caches only up to an explicit breakpoint, so the system prompt
7
+ * carries one (it covers the tool definitions ahead of it) and the newest
8
+ * message carries the other: the next turn's request then matches everything
9
+ * up to it. OpenAI caches prefixes on its own and takes a routing key; one
10
+ * key per system prompt sends every call of a run to the same cache. Other
11
+ * providers get nothing extra, and the request is exactly what it was.
12
+ */
13
+ import type { ModelMessage, SystemModelMessage } from 'ai';
14
+ import type { ProviderOptions } from '../../types.ts';
15
+ export type CacheFamily = 'anthropic' | 'openai';
16
+ /** What the hints need to know about the model: the provider and model id the SDK reports. */
17
+ export interface CacheModelRef {
18
+ readonly provider?: string | undefined;
19
+ readonly modelId?: string | undefined;
20
+ }
21
+ /**
22
+ * The provider family a model routes to. A gateway model names its upstream
23
+ * in the model id (`anthropic/claude-…`); a direct provider names it in the
24
+ * provider (`anthropic.messages`, `openai.responses`).
25
+ */
26
+ export declare function cacheFamily(model: CacheModelRef | undefined): CacheFamily | undefined;
27
+ export interface PromptCacheHints {
28
+ readonly family: CacheFamily | undefined;
29
+ /** The system prompt as the request's instructions, carrying a breakpoint where the provider needs one. */
30
+ instructions(system: string): string | SystemModelMessage;
31
+ /**
32
+ * Request-level provider options: the caller's own, plus a prompt-cache
33
+ * routing key where the provider takes one. The caller's values win.
34
+ */
35
+ providerOptions(base: ProviderOptions | undefined, system: string): ProviderOptions | undefined;
36
+ /**
37
+ * Moves the conversation breakpoint to the newest message. Every other
38
+ * message loses its breakpoint, so a history that carries forward across
39
+ * turns never accumulates more than the provider allows.
40
+ */
41
+ markLatest(messages: ModelMessage[]): ModelMessage[];
42
+ }
43
+ /** The hints for one model; a no-op for providers without a cache the request can address. */
44
+ export declare function promptCacheHints(model: CacheModelRef | undefined): PromptCacheHints;
45
+ /** A stable key for one system prompt, so every call sharing the prefix routes alike. */
46
+ export declare function promptCacheKey(system: string): string;
47
+ //# sourceMappingURL=prompt-cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prompt-cache.d.ts","sourceRoot":"","sources":["../../../src/agent/model/prompt-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,IAAI,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEtD,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,QAAQ,CAAC;AAEjD,8FAA8F;AAC9F,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACvC;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,aAAa,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,CAMrF;AAID,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IACzC,2GAA2G;IAC3G,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB,CAAC;IAC1D;;;OAGG;IACH,eAAe,CAAC,IAAI,EAAE,eAAe,GAAG,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAAC;IAChG;;;;OAIG;IACH,UAAU,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,YAAY,EAAE,CAAC;CACtD;AAED,8FAA8F;AAC9F,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,aAAa,GAAG,SAAS,GAAG,gBAAgB,CAiBnF;AAED,yFAAyF;AACzF,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAErD"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Prompt-cache hints: what a request carries so the provider can serve the
3
+ * repeated prefix (system prompt, tool definitions, the conversation so far)
4
+ * from its cache instead of reading it again at full price.
5
+ *
6
+ * Anthropic caches only up to an explicit breakpoint, so the system prompt
7
+ * carries one (it covers the tool definitions ahead of it) and the newest
8
+ * message carries the other: the next turn's request then matches everything
9
+ * up to it. OpenAI caches prefixes on its own and takes a routing key; one
10
+ * key per system prompt sends every call of a run to the same cache. Other
11
+ * providers get nothing extra, and the request is exactly what it was.
12
+ */
13
+ import { createHash } from 'node:crypto';
14
+ /**
15
+ * The provider family a model routes to. A gateway model names its upstream
16
+ * in the model id (`anthropic/claude-…`); a direct provider names it in the
17
+ * provider (`anthropic.messages`, `openai.responses`).
18
+ */
19
+ export function cacheFamily(model) {
20
+ const provider = (model?.provider ?? '').toLowerCase();
21
+ const modelId = (model?.modelId ?? '').toLowerCase();
22
+ if (modelId.startsWith('anthropic/') || provider.startsWith('anthropic'))
23
+ return 'anthropic';
24
+ if (modelId.startsWith('openai/') || provider.startsWith('openai'))
25
+ return 'openai';
26
+ return undefined;
27
+ }
28
+ const ANTHROPIC_BREAKPOINT = { cacheControl: { type: 'ephemeral' } };
29
+ /** The hints for one model; a no-op for providers without a cache the request can address. */
30
+ export function promptCacheHints(model) {
31
+ const family = cacheFamily(model);
32
+ return {
33
+ family,
34
+ instructions: (system) => family === 'anthropic'
35
+ ? { role: 'system', content: system, providerOptions: { anthropic: ANTHROPIC_BREAKPOINT } }
36
+ : system,
37
+ providerOptions: (base, system) => {
38
+ if (family !== 'openai')
39
+ return base;
40
+ return {
41
+ ...base,
42
+ openai: { promptCacheKey: promptCacheKey(system), ...base?.['openai'] },
43
+ };
44
+ },
45
+ markLatest: (messages) => (family === 'anthropic' ? moveBreakpoint(messages) : messages),
46
+ };
47
+ }
48
+ /** A stable key for one system prompt, so every call sharing the prefix routes alike. */
49
+ export function promptCacheKey(system) {
50
+ return `e2e-${createHash('sha256').update(system).digest('hex').slice(0, 16)}`;
51
+ }
52
+ function moveBreakpoint(messages) {
53
+ if (messages.length === 0)
54
+ return messages;
55
+ const last = messages.length - 1;
56
+ return messages.map((message, index) => (index === last ? withBreakpoint(message) : withoutBreakpoint(message)));
57
+ }
58
+ function withBreakpoint(message) {
59
+ const anthropic = message.providerOptions?.['anthropic'];
60
+ return {
61
+ ...message,
62
+ providerOptions: { ...message.providerOptions, anthropic: { ...anthropic, ...ANTHROPIC_BREAKPOINT } },
63
+ };
64
+ }
65
+ function withoutBreakpoint(message) {
66
+ const anthropic = message.providerOptions?.['anthropic'];
67
+ if (anthropic === undefined || !('cacheControl' in anthropic))
68
+ return message;
69
+ const { cacheControl: _dropped, ...rest } = anthropic;
70
+ const { anthropic: _anthropic, ...others } = message.providerOptions ?? {};
71
+ const providerOptions = Object.keys(rest).length === 0 ? others : { ...others, anthropic: rest };
72
+ if (Object.keys(providerOptions).length === 0) {
73
+ const { providerOptions: _none, ...bare } = message;
74
+ return bare;
75
+ }
76
+ return { ...message, providerOptions };
77
+ }
78
+ //# sourceMappingURL=prompt-cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3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OAAO,CAAC,eAAe,EAAE,CAAC,WAAW,CAAC,CAAC;IACzD,IAAI,SAAS,KAAK,SAAS,IAAI,CAAC,CAAC,cAAc,IAAI,SAAS,CAAC;QAAE,OAAO,OAAO,CAAC;IAC9E,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,SAAS,CAAC;IACtD,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC,eAAe,IAAI,EAAE,CAAC;IAC3E,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC;IACjG,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9C,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QACpD,OAAO,IAAoB,CAAC;IAC9B,CAAC;IACD,OAAO,EAAE,GAAG,OAAO,EAAE,eAAe,EAAE,CAAC;AACzC,CAAC"}
@@ -1,12 +1,12 @@
1
1
  /**
2
- * AI SDK adapter. One implementation serves every
3
- * provider: a `provider/model-id` reference resolves through the AI Gateway,
4
- * and a caller-supplied AI SDK model instance (`openai('gpt-4o')`, a local
5
- * provider, a scripted test model) is used directly. Everything after model
6
- * construction — bounded requests, closed-grammar validation, usage and error
7
- * translation — is provider-independent.
2
+ * AI SDK adapter. One implementation serves every provider: the
3
+ * caller-supplied AI SDK model instance (`gateway('openai/gpt-5.4-mini')`,
4
+ * `openrouter(...)`, `openai('gpt-4o')`, a local provider, a scripted test
5
+ * model) is used as is. Everything after the model — bounded requests,
6
+ * closed-grammar validation, usage and error translation — is
7
+ * provider-independent.
8
8
  */
9
- import { type ResolvedModel, type SdkLanguageModel } from '../../config/agent.ts';
9
+ import type { ResolvedModel, SdkLanguageModel } from '../../config/agent.ts';
10
10
  import { type ModelAdapter } from './adapter.ts';
11
11
  /**
12
12
  * Provider transport retries; distinct from runner-owned model-call budget.
@@ -23,30 +23,47 @@ export declare const TRANSPORT_RETRIES = 5;
23
23
  /** Creates the adapter for one resolved model, or fails with MODEL_UNAVAILABLE. */
24
24
  export declare function createModelAdapter(model: ResolvedModel | undefined): ModelAdapter;
25
25
  /**
26
- * Builds the bare AI SDK language model for one resolved model reference, for
27
- * callers that drive the SDK directly (the default step executor) rather than
28
- * through the adapter. Fails with MODEL_UNAVAILABLE exactly like the adapter.
29
- * Gateway references require the AI SDK to be loaded (loadAiSdk) first; model
30
- * instances never touch it.
26
+ * The bare AI SDK language model of one resolved model, for callers that
27
+ * drive the SDK directly (the default step executor) rather than through the
28
+ * adapter.
31
29
  */
32
30
  export declare function instantiateLanguageModel(model: ResolvedModel): SdkLanguageModel;
31
+ /** The AI SDK usage fields the runner reads, structurally. */
32
+ export interface SdkUsage {
33
+ readonly inputTokens?: number | undefined;
34
+ readonly outputTokens?: number | undefined;
35
+ readonly inputTokenDetails?: {
36
+ readonly cacheReadTokens?: number | undefined;
37
+ readonly cacheWriteTokens?: number | undefined;
38
+ } | undefined;
39
+ }
33
40
  /**
34
- * The AI Gateway reports per-request cost in provider metadata when available;
35
- * other providers simply lack the key. `cost` is what the gateway bills; BYOK
36
- * routes bill the provider key directly and report `cost: "0"`, so fall back
37
- * to `marketCost`, the list-price estimate of the same request.
41
+ * The prompt-cache split of one call's input, when the provider reported it.
42
+ * Providers that cache nothing report zeros; providers that do not report the
43
+ * split leave the fields undefined, and so does the report.
44
+ */
45
+ export declare function cacheTokenFields(usage: SdkUsage | undefined): {
46
+ cacheReadTokens?: number;
47
+ cacheWriteTokens?: number;
48
+ };
49
+ /**
50
+ * Per-request cost, where the provider reports one in its metadata. The
51
+ * Vercel AI Gateway's `cost` is what it bills; BYOK routes bill the provider
52
+ * key directly and report `cost: "0"`, so fall back to `marketCost`, the
53
+ * list-price estimate of the same request. OpenRouter reports `usage.cost`
54
+ * when its provider is asked to account usage. Other providers simply lack
55
+ * the key.
38
56
  */
39
57
  export declare function readCost(metadata: Readonly<Record<string, Readonly<Record<string, unknown>>>> | undefined): number | undefined;
40
58
  /**
41
- * A rejected credential names the variable the runner read it from, whether
42
- * that was `E2E_MODEL_API_KEY`, the gateway's own `AI_GATEWAY_API_KEY`, or
43
- * the one `apiKeyEnv` chose. The gateway's own text only ever says
44
- * `AI_GATEWAY_API_KEY`. Takes the resolved model (the adapter path) or the SDK
45
- * model object (the tool loop path); a model that brought its own credential
46
- * has no variable to name. Requires the SDK to be loaded, which every caller
47
- * has done by the time a provider call has failed.
59
+ * A rejected credential is named for what it is. The model instance owns its
60
+ * credential, read from the provider package's own variable
61
+ * (`AI_GATEWAY_API_KEY`, `OPENROUTER_API_KEY`, ...) or passed at
62
+ * construction, so the runner can only say that the provider refused it.
63
+ * Requires the SDK to be loaded, which every caller has done by the time a
64
+ * provider call has failed.
48
65
  */
49
- export declare function credentialHint(cause: unknown, model?: ResolvedModel | SdkLanguageModel | string): string;
66
+ export declare function credentialHint(cause: unknown): string;
50
67
  /**
51
68
  * True when a provider call ended by abort or timeout rather than by a
52
69
  * provider answer, including a retry chain the deadline cut short. Requires
@@ -1 +1 @@
1
- {"version":3,"file":"sdk.d.ts","sourceRoot":"","sources":["../../../src/agent/model/sdk.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAuB,KAAK,aAAa,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAIvG,OAAO,EAIL,KAAK,YAAY,EAKlB,MAAM,cAAc,CAAC;AAOtB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC,mFAAmF;AACnF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,aAAa,GAAG,SAAS,GAAG,YAAY,CAuFjF;AAsBD;;;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AAC;QAC9C,QAAQ,CAAC,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAChD,GACD,SAAS,CAAC;CACf;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,QAAQ,GAAG,SAAS,GAC1B;IAAE,eAAe,CAAC,EAAE,MAAM,CAAC;IAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAE,CAMzD;AAqCD;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,GAChF,MAAM,GAAG,SAAS,CAOpB;AA6DD;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAYrD;AAaD;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAI/C"}
@@ -1,20 +1,18 @@
1
1
  /**
2
- * AI SDK adapter. One implementation serves every
3
- * provider: a `provider/model-id` reference resolves through the AI Gateway,
4
- * and a caller-supplied AI SDK model instance (`openai('gpt-4o')`, a local
5
- * provider, a scripted test model) is used directly. Everything after model
6
- * construction — bounded requests, closed-grammar validation, usage and error
7
- * translation — is provider-independent.
2
+ * AI SDK adapter. One implementation serves every provider: the
3
+ * caller-supplied AI SDK model instance (`gateway('openai/gpt-5.4-mini')`,
4
+ * `openrouter(...)`, `openai('gpt-4o')`, a local provider, a scripted test
5
+ * model) is used as is. Everything after the model — bounded requests,
6
+ * closed-grammar validation, usage and error translation — is
7
+ * provider-independent.
8
8
  */
9
9
  import { withHint } from '../../internal/errors.js';
10
- import { GATEWAY_API_KEY_ENV } from '../../config/agent.js';
11
10
  import { aiSdk, loadAiSdk } from '../ai-sdk.js';
12
11
  import { packageVersion } from '../../internal/package-version.js';
13
12
  import { AgentError } from '../error.js';
13
+ import { isContextOverflow } from './overflow.js';
14
+ import { promptCacheHints } from './prompt-cache.js';
14
15
  import { imageTokenUpperBound, ModelOutputInvalidError, tokenUpperBound, } from './adapter.js';
15
- /** Default AI Gateway base URL used when no endpoint override is configured. */
16
- const DEFAULT_GATEWAY_ENDPOINT = 'https://ai-gateway.vercel.sh/v4/ai';
17
- const PROVIDER_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
18
16
  /**
19
17
  * Provider transport retries; distinct from runner-owned model-call budget.
20
18
  *
@@ -30,23 +28,22 @@ export const TRANSPORT_RETRIES = 5;
30
28
  /** Creates the adapter for one resolved model, or fails with MODEL_UNAVAILABLE. */
31
29
  export function createModelAdapter(model) {
32
30
  if (model === undefined) {
33
- throw new AgentError('MODEL_UNAVAILABLE', 'the agent fixture requires a model: pass one to createAgent({ model }), set agent.model, or set E2E_MODEL');
31
+ throw new AgentError('MODEL_UNAVAILABLE', 'the agent fixture requires a model: pass an AI SDK model instance to createAgent({ model }) or set agent.model');
34
32
  }
35
- const { endpoint, flavor } = validateModel(model);
36
33
  const adapterVersion = packageVersion(import.meta.url, '../../../package.json', '0.0.0');
37
- // The AI SDK is an optional peer: the language model is constructed on the
38
- // first call, after the loader has resolved (or clearly refused) it.
39
- let languageModel;
34
+ const languageModel = model.model;
40
35
  return {
41
36
  provenance: {
42
37
  provider: model.provider,
43
38
  model: model.id,
44
- endpoint,
45
- adapterVersion: `${flavor}/${adapterVersion}`,
39
+ // The instance owns its transport; the report records that the endpoint
40
+ // is whatever the provider package was configured with.
41
+ endpoint: 'provider-default',
42
+ adapterVersion: `ai-sdk/${adapterVersion}`,
46
43
  },
47
44
  async generate(call) {
48
45
  const { generateText, jsonSchema, Output } = await loadAiSdk();
49
- languageModel ??= instantiate(model).languageModel;
46
+ const cache = promptCacheHints(languageModel);
50
47
  const images = call.images ?? [];
51
48
  const inputBound = tokenUpperBound(call.system) +
52
49
  tokenUpperBound(call.prompt) +
@@ -56,9 +53,12 @@ export function createModelAdapter(model) {
56
53
  }
57
54
  let issue;
58
55
  const schema = call.schema;
56
+ // The policy prefix is what every judgment call of a run shares, so it
57
+ // is where the prompt cache is addressed; the prompt itself is one-off.
58
+ const providerOptions = cache.providerOptions(call.providerOptions, call.system);
59
59
  const settings = {
60
60
  model: languageModel,
61
- system: call.system,
61
+ instructions: cache.instructions(call.system),
62
62
  // Text-only calls keep the plain prompt form; images require the
63
63
  // multi-part message form, and both must carry the same instruction
64
64
  // text in the same position relative to the system policy.
@@ -66,9 +66,7 @@ export function createModelAdapter(model) {
66
66
  ? { prompt: call.prompt }
67
67
  : { messages: [userMessage(call.prompt, images)] }),
68
68
  maxOutputTokens: call.maxOutputTokens,
69
- ...(call.providerOptions === undefined
70
- ? {}
71
- : { providerOptions: call.providerOptions }),
69
+ ...(providerOptions === undefined ? {} : { providerOptions: providerOptions }),
72
70
  maxRetries: TRANSPORT_RETRIES,
73
71
  abortSignal: call.signal,
74
72
  timeout: call.timeoutMs,
@@ -105,7 +103,7 @@ export function createModelAdapter(model) {
105
103
  return { value: output, usage: readUsage(result, inputBound) };
106
104
  }
107
105
  catch (cause) {
108
- throw translateModelError(cause, issue, call.signal, model);
106
+ throw translateModelError(cause, issue, call.signal);
109
107
  }
110
108
  },
111
109
  };
@@ -130,61 +128,37 @@ function userMessage(prompt, images) {
130
128
  };
131
129
  }
132
130
  /**
133
- * Builds the bare AI SDK language model for one resolved model reference, for
134
- * callers that drive the SDK directly (the default step executor) rather than
135
- * through the adapter. Fails with MODEL_UNAVAILABLE exactly like the adapter.
136
- * Gateway references require the AI SDK to be loaded (loadAiSdk) first; model
137
- * instances never touch it.
131
+ * The bare AI SDK language model of one resolved model, for callers that
132
+ * drive the SDK directly (the default step executor) rather than through the
133
+ * adapter.
138
134
  */
139
135
  export function instantiateLanguageModel(model) {
140
- return instantiate(model).languageModel;
136
+ return model.model;
141
137
  }
142
138
  /**
143
- * Validates one resolved model and derives its report provenance without
144
- * touching the AI SDK, so an adapter can be constructed and refuse clearly —
145
- * before the optional peer dependency is ever loaded.
139
+ * The prompt-cache split of one call's input, when the provider reported it.
140
+ * Providers that cache nothing report zeros; providers that do not report the
141
+ * split leave the fields undefined, and so does the report.
146
142
  */
147
- function validateModel(model) {
148
- if (model.kind === 'instance') {
149
- // The instance owns its transport; the report records that the endpoint is
150
- // whatever the provider package defaults to.
151
- return { endpoint: 'provider-default', flavor: 'ai-sdk' };
152
- }
153
- if (!PROVIDER_PATTERN.test(model.provider)) {
154
- throw new AgentError('MODEL_UNAVAILABLE', `unknown model provider "${model.provider}"; expected a gateway provider ID or an AI SDK model instance`);
155
- }
156
- if (model.apiKey === undefined) {
157
- throw new AgentError('MODEL_UNAVAILABLE', `no model credential: set ${model.apiKeyEnv} or ${GATEWAY_API_KEY_ENV}`);
158
- }
159
- return { endpoint: model.endpoint ?? DEFAULT_GATEWAY_ENDPOINT, flavor: 'ai-gateway' };
160
- }
161
- /**
162
- * The variable each gateway language model's credential was read from, keyed
163
- * by the model object the SDK is handed. The tool loop sees only that object,
164
- * and this is how a rejected credential is still reported by its source. A
165
- * name, never a value.
166
- */
167
- const credentialSources = new WeakMap();
168
- /** Builds the AI SDK language model for one validated resolved model. */
169
- function instantiate(model) {
170
- if (model.kind === 'instance')
171
- return { languageModel: model.model };
172
- const { endpoint } = validateModel(model);
173
- const gateway = aiSdk().createGateway({ apiKey: model.apiKey, baseURL: endpoint });
174
- const languageModel = gateway.languageModel(`${model.provider}/${model.id}`);
175
- credentialSources.set(languageModel, model.apiKeySource ?? model.apiKeyEnv);
176
- return { languageModel };
143
+ export function cacheTokenFields(usage) {
144
+ const details = usage?.inputTokenDetails;
145
+ return {
146
+ ...(typeof details?.cacheReadTokens === 'number' ? { cacheReadTokens: details.cacheReadTokens } : {}),
147
+ ...(typeof details?.cacheWriteTokens === 'number' ? { cacheWriteTokens: details.cacheWriteTokens } : {}),
148
+ };
177
149
  }
178
150
  function readUsage(result, inputBound) {
179
151
  const inputTokens = result.usage?.inputTokens;
180
152
  const outputTokens = result.usage?.outputTokens;
181
153
  const estimatedCostUsd = readCost(result.providerMetadata);
154
+ const cache = cacheTokenFields(result.usage);
182
155
  if (typeof inputTokens === 'number' && typeof outputTokens === 'number') {
183
- return { inputTokens, outputTokens, accounting: 'provider', estimatedCostUsd };
156
+ return { inputTokens, outputTokens, ...cache, accounting: 'provider', estimatedCostUsd };
184
157
  }
185
158
  return {
186
159
  inputTokens: inputBound,
187
160
  outputTokens: tokenUpperBound(result.text ?? ''),
161
+ ...cache,
188
162
  accounting: 'adapter-upper-bound',
189
163
  estimatedCostUsd,
190
164
  };
@@ -208,16 +182,22 @@ function parseJsonObject(text) {
208
182
  }
209
183
  }
210
184
  /**
211
- * The AI Gateway reports per-request cost in provider metadata when available;
212
- * other providers simply lack the key. `cost` is what the gateway bills; BYOK
213
- * routes bill the provider key directly and report `cost: "0"`, so fall back
214
- * to `marketCost`, the list-price estimate of the same request.
185
+ * Per-request cost, where the provider reports one in its metadata. The
186
+ * Vercel AI Gateway's `cost` is what it bills; BYOK routes bill the provider
187
+ * key directly and report `cost: "0"`, so fall back to `marketCost`, the
188
+ * list-price estimate of the same request. OpenRouter reports `usage.cost`
189
+ * when its provider is asked to account usage. Other providers simply lack
190
+ * the key.
215
191
  */
216
192
  export function readCost(metadata) {
217
193
  const billed = parseCost(metadata?.['gateway']?.['cost']);
218
194
  if (billed !== undefined && billed > 0)
219
195
  return billed;
220
- return parseCost(metadata?.['gateway']?.['marketCost']) ?? billed;
196
+ const market = parseCost(metadata?.['gateway']?.['marketCost']) ?? billed;
197
+ if (market !== undefined)
198
+ return market;
199
+ const openrouter = metadata?.['openrouter']?.['usage'];
200
+ return typeof openrouter === 'object' && openrouter !== null ? parseCost(openrouter['cost']) : undefined;
221
201
  }
222
202
  function parseCost(raw) {
223
203
  const cost = typeof raw === 'string' ? Number(raw) : raw;
@@ -228,7 +208,7 @@ function parseCost(raw) {
228
208
  * only after `generate` loaded the SDK, so the cached module is available for
229
209
  * the error-class checks.
230
210
  */
231
- function translateModelError(rawCause, issue, signal, model) {
211
+ function translateModelError(rawCause, issue, signal) {
232
212
  const { APICallError, NoObjectGeneratedError } = aiSdk();
233
213
  const cause = unwrapRetry(rawCause);
234
214
  if (cause instanceof AgentError)
@@ -244,25 +224,29 @@ function translateModelError(rawCause, issue, signal, model) {
244
224
  if (NoObjectGeneratedError.isInstance(cause)) {
245
225
  return new ModelOutputInvalidError(issue ?? 'provider response did not match the closed response grammar', { ...(cause.text !== undefined ? { rawText: cause.text } : {}), cause });
246
226
  }
227
+ // Too big a request is not a provider outage: the observation budget or
228
+ // the step is what needs to shrink, and the code says so.
229
+ if (isContextOverflow(cause)) {
230
+ return new AgentError('CONTEXT_OVERFLOW', `the model request exceeded the context window: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
231
+ }
247
232
  if (APICallError.isInstance(cause)) {
248
- return new AgentError('MODEL_PROVIDER_FAILED', withHint(`model provider failed: ${cause.message}`, credentialHint(cause, model)), { cause });
233
+ return new AgentError('MODEL_PROVIDER_FAILED', withHint(`model provider failed: ${cause.message}`, credentialHint(cause)), { cause });
249
234
  }
250
235
  return new AgentError('MODEL_PROVIDER_FAILED', `model provider failed: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
251
236
  }
252
237
  /**
253
- * A rejected credential names the variable the runner read it from, whether
254
- * that was `E2E_MODEL_API_KEY`, the gateway's own `AI_GATEWAY_API_KEY`, or
255
- * the one `apiKeyEnv` chose. The gateway's own text only ever says
256
- * `AI_GATEWAY_API_KEY`. Takes the resolved model (the adapter path) or the SDK
257
- * model object (the tool loop path); a model that brought its own credential
258
- * has no variable to name. Requires the SDK to be loaded, which every caller
259
- * has done by the time a provider call has failed.
238
+ * A rejected credential is named for what it is. The model instance owns its
239
+ * credential, read from the provider package's own variable
240
+ * (`AI_GATEWAY_API_KEY`, `OPENROUTER_API_KEY`, ...) or passed at
241
+ * construction, so the runner can only say that the provider refused it.
242
+ * Requires the SDK to be loaded, which every caller has done by the time a
243
+ * provider call has failed.
260
244
  */
261
- export function credentialHint(cause, model) {
245
+ export function credentialHint(cause) {
262
246
  const failure = unwrapRetry(cause);
263
247
  if (!(failure instanceof Error))
264
248
  return '';
265
- // The gateway raises its own authentication error class and a raw provider
249
+ // Gateways raise their own authentication error classes and a raw provider
266
250
  // an APICallError; both carry the HTTP status, so that is what is read.
267
251
  const statusCode = failure.statusCode;
268
252
  const rejected = statusCode === 401 ||
@@ -270,18 +254,7 @@ export function credentialHint(cause, model) {
270
254
  /unauthenticated|unauthorized|authentication/i.test(`${failure.name} ${failure.message}`);
271
255
  if (!rejected)
272
256
  return '';
273
- const source = credentialSource(model);
274
- if (source === undefined)
275
- return 'the provider rejected the credential the model instance was created with';
276
- return `the gateway rejected the credential read from ${source}: check that the key is complete and belongs to the AI Gateway (https://vercel.com/docs/ai-gateway)`;
277
- }
278
- /** The variable a gateway model's credential came from; undefined for a model that owns its own. */
279
- function credentialSource(model) {
280
- if (model === undefined || typeof model !== 'object')
281
- return undefined;
282
- if ('kind' in model)
283
- return model.kind === 'gateway' ? (model.apiKeySource ?? model.apiKeyEnv) : undefined;
284
- return credentialSources.get(model);
257
+ return 'the provider rejected the credential the model instance was created with: check the variable the provider package reads, or the key passed at construction';
285
258
  }
286
259
  /**
287
260
  * Unwraps a spent transport retry chain to the attempt that actually failed,
@@ -1 +1 @@
1
- {"version":3,"file":"sdk.js","sourceRoot":"","sources":["../../../src/agent/model/sdk.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAA6C,MAAM,uBAAuB,CAAC;AACvG,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,oBAAoB,EACpB,uBAAuB,EACv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AAK,GAAG;QAClB,8CAA8C,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IAC5F,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAC;IACzB,OAAO,4JAA4J,CAAC;AACtK,CAAC;AAED;;;;GAIG;AACH,SAAS,WAAW,CAAC,KAAc;IACjC,MAAM,EAAE,UAAU,EAAE,GAAG,KAAK,EAAE,CAAC;IAC/B,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,OAAO;QAAE,OAAO,KAAK,CAAC;IAC5E,OAAO,KAAK,CAAC,SAAS,IAAI,KAAK,CAAC;AAClC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,KAAc;IACpC,MAAM,EAAE,UAAU,EAAE,GAAG,KAAK,EAAE,CAAC;IAC/B,IAAI,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,OAAO;QAAE,OAAO,IAAI,CAAC;IAC1E,OAAO,KAAK,YAAY,KAAK,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,YAAY,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,CAAC,CAAC;AAClG,CAAC"}
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Recognizes a provider refusing a forced tool choice. The act loop asks for
3
+ * a tool call on every turn (`toolChoice: 'required'`) and names
4
+ * `complete_step` on the final ones; some models reject that request shape
5
+ * outright (Anthropic's Claude Fable 5.1 answers HTTP 400 with
6
+ * `tool_choice: type "tool" and "any" are not supported for this model.`),
7
+ * and a gateway forwards the provider's words.
8
+ *
9
+ * The loop reacts to this refusal differently from any other provider
10
+ * failure: the same request with `toolChoice: 'auto'` and an instruction to
11
+ * answer with tool calls can still succeed, where a 5xx needs only time.
12
+ */
13
+ /**
14
+ * Whether a provider failure says the model does not accept a forced tool
15
+ * choice. Reads the message and the raw response body of the error and of
16
+ * everything it wraps (`cause`, or the last attempt of a spent retry chain).
17
+ * Only a 4xx counts: a gateway or proxy that fails for its own reasons while
18
+ * quoting the request never reads as a model limitation.
19
+ */
20
+ export declare function isForcedToolChoiceRejected(error: unknown): boolean;
21
+ //# sourceMappingURL=tool-choice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-choice.d.ts","sourceRoot":"","sources":["../../../src/agent/model/tool-choice.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAYH;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CASlE"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Recognizes a provider refusing a forced tool choice. The act loop asks for
3
+ * a tool call on every turn (`toolChoice: 'required'`) and names
4
+ * `complete_step` on the final ones; some models reject that request shape
5
+ * outright (Anthropic's Claude Fable 5.1 answers HTTP 400 with
6
+ * `tool_choice: type "tool" and "any" are not supported for this model.`),
7
+ * and a gateway forwards the provider's words.
8
+ *
9
+ * The loop reacts to this refusal differently from any other provider
10
+ * failure: the same request with `toolChoice: 'auto'` and an instruction to
11
+ * answer with tool calls can still succeed, where a 5xx needs only time.
12
+ */
13
+ const FORCED_CHOICE_PATTERNS = [
14
+ /tool_choice.*not supported/i, // Anthropic
15
+ /tool_choice.*(?:unsupported|is not allowed|cannot be)/i, // OpenAI-compatible proxies
16
+ /(?:forced|required) tool (?:choice|call|use).*not supported/i, // generic
17
+ /does not support (?:forced|required) tool/i, // generic
18
+ ];
19
+ /** Longest cause chain walked; a wrapped error rarely nests deeper. */
20
+ const MAX_CAUSE_DEPTH = 8;
21
+ /**
22
+ * Whether a provider failure says the model does not accept a forced tool
23
+ * choice. Reads the message and the raw response body of the error and of
24
+ * everything it wraps (`cause`, or the last attempt of a spent retry chain).
25
+ * Only a 4xx counts: a gateway or proxy that fails for its own reasons while
26
+ * quoting the request never reads as a model limitation.
27
+ */
28
+ export function isForcedToolChoiceRejected(error) {
29
+ let current = error;
30
+ for (let depth = 0; depth < MAX_CAUSE_DEPTH && current !== undefined && current !== null; depth += 1) {
31
+ if (describesRejection(current))
32
+ return true;
33
+ if (typeof current !== 'object')
34
+ return false;
35
+ const wrapper = current;
36
+ current = wrapper.cause ?? wrapper.lastError;
37
+ }
38
+ return false;
39
+ }
40
+ function describesRejection(error) {
41
+ if (typeof error !== 'object' || error === null)
42
+ return false;
43
+ const record = error;
44
+ const status = record.statusCode ?? record.status;
45
+ if (typeof status === 'number' && (status < 400 || status >= 500))
46
+ return false;
47
+ return [record.message, record.responseBody].some((text) => typeof text === 'string' && FORCED_CHOICE_PATTERNS.some((pattern) => pattern.test(text)));
48
+ }
49
+ //# sourceMappingURL=tool-choice.js.map