@c9up/polaris 0.1.0 → 0.1.1

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 (217) hide show
  1. package/README.md +614 -10
  2. package/dist/Polaris.d.ts +6 -2
  3. package/dist/Polaris.d.ts.map +1 -1
  4. package/dist/Polaris.js +14 -0
  5. package/dist/Polaris.js.map +1 -1
  6. package/dist/PolarisClient.d.ts +51 -3
  7. package/dist/PolarisClient.d.ts.map +1 -1
  8. package/dist/PolarisClient.js +213 -45
  9. package/dist/PolarisClient.js.map +1 -1
  10. package/dist/PolarisProvider.d.ts +7 -2
  11. package/dist/PolarisProvider.d.ts.map +1 -1
  12. package/dist/PolarisProvider.js +34 -2
  13. package/dist/PolarisProvider.js.map +1 -1
  14. package/dist/augmentations.d.ts +4 -1
  15. package/dist/augmentations.d.ts.map +1 -1
  16. package/dist/augmentations.js +1 -1
  17. package/dist/budget.d.ts +39 -6
  18. package/dist/budget.d.ts.map +1 -1
  19. package/dist/budget.js +79 -12
  20. package/dist/budget.js.map +1 -1
  21. package/dist/catalog/catalog.d.ts +37 -0
  22. package/dist/catalog/catalog.d.ts.map +1 -0
  23. package/dist/catalog/catalog.js +145 -0
  24. package/dist/catalog/catalog.js.map +1 -0
  25. package/dist/catalog/discover.d.ts +64 -0
  26. package/dist/catalog/discover.d.ts.map +1 -0
  27. package/dist/catalog/discover.js +148 -0
  28. package/dist/catalog/discover.js.map +1 -0
  29. package/dist/catalog/index.d.ts +8 -0
  30. package/dist/catalog/index.d.ts.map +1 -0
  31. package/dist/catalog/index.js +8 -0
  32. package/dist/catalog/index.js.map +1 -0
  33. package/dist/commands/index.d.ts +82 -0
  34. package/dist/commands/index.d.ts.map +1 -0
  35. package/dist/commands/index.js +278 -0
  36. package/dist/commands/index.js.map +1 -0
  37. package/dist/cost.d.ts +3 -1
  38. package/dist/cost.d.ts.map +1 -1
  39. package/dist/cost.js +8 -3
  40. package/dist/cost.js.map +1 -1
  41. package/dist/drivers/anthropic.d.ts +6 -1
  42. package/dist/drivers/anthropic.d.ts.map +1 -1
  43. package/dist/drivers/anthropic.js +166 -21
  44. package/dist/drivers/anthropic.js.map +1 -1
  45. package/dist/drivers/openai-compatible.d.ts +1 -1
  46. package/dist/drivers/openai-compatible.d.ts.map +1 -1
  47. package/dist/drivers/openai-compatible.js +106 -70
  48. package/dist/drivers/openai-compatible.js.map +1 -1
  49. package/dist/errors.d.ts +24 -0
  50. package/dist/errors.d.ts.map +1 -1
  51. package/dist/errors.js +24 -0
  52. package/dist/errors.js.map +1 -1
  53. package/dist/index.d.ts +6 -4
  54. package/dist/index.d.ts.map +1 -1
  55. package/dist/index.js +3 -1
  56. package/dist/index.js.map +1 -1
  57. package/dist/json-schema.d.ts +33 -0
  58. package/dist/json-schema.d.ts.map +1 -0
  59. package/dist/json-schema.js +508 -0
  60. package/dist/json-schema.js.map +1 -0
  61. package/dist/log.d.ts.map +1 -1
  62. package/dist/log.js +3 -1
  63. package/dist/log.js.map +1 -1
  64. package/dist/mcp/client.d.ts +81 -0
  65. package/dist/mcp/client.d.ts.map +1 -0
  66. package/dist/mcp/client.js +401 -0
  67. package/dist/mcp/client.js.map +1 -0
  68. package/dist/mcp/endpoint.d.ts +54 -0
  69. package/dist/mcp/endpoint.d.ts.map +1 -0
  70. package/dist/mcp/endpoint.js +158 -0
  71. package/dist/mcp/endpoint.js.map +1 -0
  72. package/dist/mcp/host.d.ts +42 -0
  73. package/dist/mcp/host.d.ts.map +1 -0
  74. package/dist/mcp/host.js +111 -0
  75. package/dist/mcp/host.js.map +1 -0
  76. package/dist/mcp/http.d.ts +32 -0
  77. package/dist/mcp/http.d.ts.map +1 -0
  78. package/dist/mcp/http.js +190 -0
  79. package/dist/mcp/http.js.map +1 -0
  80. package/dist/mcp/index.d.ts +23 -0
  81. package/dist/mcp/index.d.ts.map +1 -0
  82. package/dist/mcp/index.js +21 -0
  83. package/dist/mcp/index.js.map +1 -0
  84. package/dist/mcp/manifest.d.ts +36 -0
  85. package/dist/mcp/manifest.d.ts.map +1 -0
  86. package/dist/mcp/manifest.js +40 -0
  87. package/dist/mcp/manifest.js.map +1 -0
  88. package/dist/mcp/rpc.d.ts +39 -0
  89. package/dist/mcp/rpc.d.ts.map +1 -0
  90. package/dist/mcp/rpc.js +149 -0
  91. package/dist/mcp/rpc.js.map +1 -0
  92. package/dist/mcp/stdio.d.ts +24 -0
  93. package/dist/mcp/stdio.d.ts.map +1 -0
  94. package/dist/mcp/stdio.js +157 -0
  95. package/dist/mcp/stdio.js.map +1 -0
  96. package/dist/mcp/worker-main.d.ts +6 -0
  97. package/dist/mcp/worker-main.d.ts.map +1 -0
  98. package/dist/mcp/worker-main.js +17 -0
  99. package/dist/mcp/worker-main.js.map +1 -0
  100. package/dist/mcp/worker.d.ts +54 -0
  101. package/dist/mcp/worker.d.ts.map +1 -0
  102. package/dist/mcp/worker.js +166 -0
  103. package/dist/mcp/worker.js.map +1 -0
  104. package/dist/meter.d.ts +6 -0
  105. package/dist/meter.d.ts.map +1 -1
  106. package/dist/meter.js +6 -3
  107. package/dist/meter.js.map +1 -1
  108. package/dist/redact.d.ts +4 -3
  109. package/dist/redact.d.ts.map +1 -1
  110. package/dist/redact.js +5 -4
  111. package/dist/redact.js.map +1 -1
  112. package/dist/redis-budget.d.ts +45 -0
  113. package/dist/redis-budget.d.ts.map +1 -0
  114. package/dist/redis-budget.js +56 -0
  115. package/dist/redis-budget.js.map +1 -0
  116. package/dist/response.d.ts +21 -3
  117. package/dist/response.d.ts.map +1 -1
  118. package/dist/response.js +71 -7
  119. package/dist/response.js.map +1 -1
  120. package/dist/server-sent.d.ts +9 -0
  121. package/dist/server-sent.d.ts.map +1 -0
  122. package/dist/server-sent.js +74 -0
  123. package/dist/server-sent.js.map +1 -0
  124. package/dist/server-tools.d.ts +12 -0
  125. package/dist/server-tools.d.ts.map +1 -0
  126. package/dist/server-tools.js +71 -0
  127. package/dist/server-tools.js.map +1 -0
  128. package/dist/services/catalog.d.ts +19 -0
  129. package/dist/services/catalog.d.ts.map +1 -0
  130. package/dist/services/catalog.js +37 -0
  131. package/dist/services/catalog.js.map +1 -0
  132. package/dist/skills/folder.d.ts +32 -0
  133. package/dist/skills/folder.d.ts.map +1 -0
  134. package/dist/skills/folder.js +170 -0
  135. package/dist/skills/folder.js.map +1 -0
  136. package/dist/skills/frontmatter.d.ts +15 -0
  137. package/dist/skills/frontmatter.d.ts.map +1 -0
  138. package/dist/skills/frontmatter.js +92 -0
  139. package/dist/skills/frontmatter.js.map +1 -0
  140. package/dist/skills/index.d.ts +18 -0
  141. package/dist/skills/index.d.ts.map +1 -0
  142. package/dist/skills/index.js +18 -0
  143. package/dist/skills/index.js.map +1 -0
  144. package/dist/skills/kit.d.ts +39 -0
  145. package/dist/skills/kit.d.ts.map +1 -0
  146. package/dist/skills/kit.js +124 -0
  147. package/dist/skills/kit.js.map +1 -0
  148. package/dist/skills/skill.d.ts +67 -0
  149. package/dist/skills/skill.d.ts.map +1 -0
  150. package/dist/skills/skill.js +114 -0
  151. package/dist/skills/skill.js.map +1 -0
  152. package/dist/sse.d.ts +43 -6
  153. package/dist/sse.d.ts.map +1 -1
  154. package/dist/sse.js +44 -18
  155. package/dist/sse.js.map +1 -1
  156. package/dist/testing/FakeProvider.d.ts.map +1 -1
  157. package/dist/testing/FakeProvider.js +1 -0
  158. package/dist/testing/FakeProvider.js.map +1 -1
  159. package/dist/tool-modules.d.ts +17 -0
  160. package/dist/tool-modules.d.ts.map +1 -0
  161. package/dist/tool-modules.js +43 -0
  162. package/dist/tool-modules.js.map +1 -0
  163. package/dist/tools.d.ts +69 -5
  164. package/dist/tools.d.ts.map +1 -1
  165. package/dist/tools.js +174 -31
  166. package/dist/tools.js.map +1 -1
  167. package/dist/transport.d.ts +8 -3
  168. package/dist/transport.d.ts.map +1 -1
  169. package/dist/transport.js +19 -4
  170. package/dist/transport.js.map +1 -1
  171. package/dist/types.d.ts +106 -2
  172. package/dist/types.d.ts.map +1 -1
  173. package/package.json +22 -1
  174. package/src/Polaris.ts +25 -1
  175. package/src/PolarisClient.ts +288 -51
  176. package/src/PolarisProvider.ts +40 -2
  177. package/src/augmentations.ts +4 -1
  178. package/src/budget.ts +115 -15
  179. package/src/catalog/catalog.ts +180 -0
  180. package/src/catalog/discover.ts +234 -0
  181. package/src/catalog/index.ts +19 -0
  182. package/src/commands/index.ts +412 -0
  183. package/src/cost.ts +9 -3
  184. package/src/drivers/anthropic.ts +203 -20
  185. package/src/drivers/openai-compatible.ts +143 -76
  186. package/src/errors.ts +41 -0
  187. package/src/index.ts +16 -0
  188. package/src/json-schema.ts +597 -0
  189. package/src/log.ts +3 -1
  190. package/src/mcp/client.ts +532 -0
  191. package/src/mcp/endpoint.ts +229 -0
  192. package/src/mcp/host.ts +140 -0
  193. package/src/mcp/http.ts +276 -0
  194. package/src/mcp/index.ts +51 -0
  195. package/src/mcp/manifest.ts +73 -0
  196. package/src/mcp/rpc.ts +197 -0
  197. package/src/mcp/stdio.ts +190 -0
  198. package/src/mcp/worker-main.ts +18 -0
  199. package/src/mcp/worker.ts +212 -0
  200. package/src/meter.ts +13 -2
  201. package/src/redact.ts +6 -3
  202. package/src/redis-budget.ts +85 -0
  203. package/src/response.ts +86 -8
  204. package/src/server-sent.ts +70 -0
  205. package/src/server-tools.ts +77 -0
  206. package/src/services/catalog.ts +43 -0
  207. package/src/skills/folder.ts +231 -0
  208. package/src/skills/frontmatter.ts +92 -0
  209. package/src/skills/index.ts +38 -0
  210. package/src/skills/kit.ts +163 -0
  211. package/src/skills/skill.ts +171 -0
  212. package/src/sse.ts +88 -22
  213. package/src/testing/FakeProvider.ts +1 -0
  214. package/src/tool-modules.ts +49 -0
  215. package/src/tools.ts +260 -27
  216. package/src/transport.ts +25 -3
  217. package/src/types.ts +108 -2
@@ -12,12 +12,14 @@ import type { BetaMessageStreamParams } from "@anthropic-ai/sdk/resources/beta/m
12
12
  import {
13
13
  E_POLARIS_INVALID_CONFIG,
14
14
  E_POLARIS_INVALID_REQUEST,
15
+ E_POLARIS_PROVIDER_REJECTED,
15
16
  E_POLARIS_PROVIDER_UNREACHABLE,
16
17
  E_POLARIS_RESPONSE_TOO_LARGE,
17
18
  } from "../errors.js";
18
19
  import { MAX_TIMEOUT } from "../PolarisClient.js";
19
20
 
20
21
  import { createResponse } from "../response.js";
22
+ import { serverToolsProblem } from "../server-tools.js";
21
23
  import type {
22
24
  AssistantPart,
23
25
  ChatRequest,
@@ -30,6 +32,7 @@ import type {
30
32
  Origin,
31
33
  PolarisDriver,
32
34
  ProviderFactory,
35
+ ServerTools,
33
36
  StopReason,
34
37
  StreamEvent,
35
38
  ToolCallPart,
@@ -46,6 +49,15 @@ type MessageParam = Anthropic.Beta.BetaMessageParam;
46
49
  const PROVIDER = "anthropic";
47
50
  /** What the server-side refusal fallback needs turned on. */
48
51
  const FALLBACK_BETA = "server-side-fallback-2026-07-01";
52
+ /** What the MCP connector (`mcp_servers`) needs turned on. */
53
+ const MCP_BETA = "mcp-client-2025-11-20";
54
+ /** The code execution tool, which runs skills too. */
55
+ const CODE_EXECUTION = {
56
+ type: "code_execution_20260521",
57
+ name: "code_execution",
58
+ } as const;
59
+ /** The most skills one request may load. */
60
+ const MAX_SKILLS = 20;
49
61
  /** Output limits when the request sets none: the SDK's own guidance. */
50
62
  const DEFAULT_MAX_TOKENS = 16_000;
51
63
  const DEFAULT_STREAM_MAX_TOKENS = 64_000;
@@ -74,12 +86,18 @@ export interface AnthropicConfig {
74
86
  readonly fallbacks?: "default" | readonly { readonly model: string }[];
75
87
  /** Further `anthropic-beta` flags sent with every request. */
76
88
  readonly betas?: readonly string[];
89
+ /**
90
+ * Server-side tools every request gets: code execution, skills, MCP
91
+ * servers. Each one a request's `server` sets replaces this one's.
92
+ */
93
+ readonly server?: Omit<ServerTools, "container">;
77
94
  }
78
95
 
79
96
  const FEATURES: DriverFeatures = {
80
97
  tools: true,
81
98
  structuredOutput: true,
82
99
  images: true,
100
+ serverTools: true,
83
101
  };
84
102
 
85
103
  /** A provider factory for Claude: `providers: { claude: anthropic({ model }) }`. */
@@ -131,6 +149,22 @@ class AnthropicDriver implements PolarisDriver {
131
149
  readonly defaults: DriverDefaults;
132
150
  readonly #config: AnthropicConfig;
133
151
 
152
+ /**
153
+ * The configured key, and the credentials the SDK reads from the
154
+ * environment when there is none, read when an error is scrubbed.
155
+ */
156
+ get secrets(): readonly string[] {
157
+ const env = typeof process === "undefined" ? undefined : process.env;
158
+ return [
159
+ this.#config.apiKey,
160
+ env?.ANTHROPIC_API_KEY,
161
+ env?.ANTHROPIC_AUTH_TOKEN,
162
+ ...(this.#config.server?.mcpServers ?? []).map(
163
+ (mcp) => mcp.authorizationToken,
164
+ ),
165
+ ].filter((secret): secret is string => typeof secret === "string");
166
+ }
167
+
134
168
  constructor(config: AnthropicConfig) {
135
169
  if (typeof config.model !== "string" || config.model.trim() === "") {
136
170
  throw new E_POLARIS_INVALID_CONFIG([
@@ -150,6 +184,13 @@ class AnthropicDriver implements PolarisDriver {
150
184
  "the Anthropic driver's `maxRetries` must be an integer, 0 or more",
151
185
  ]);
152
186
  }
187
+ const problem =
188
+ config.server !== undefined
189
+ ? serverToolsProblem(config.server)
190
+ : undefined;
191
+ if (problem !== undefined) {
192
+ throw new E_POLARIS_INVALID_CONFIG([`the Anthropic driver's ${problem}`]);
193
+ }
153
194
  this.#config = config;
154
195
  this.defaults = {
155
196
  model: config.model,
@@ -233,9 +274,13 @@ class AnthropicDriver implements PolarisDriver {
233
274
  ): Params {
234
275
  const config = this.#config;
235
276
  const extra = providerOptions(request);
277
+ const server = serverParams(config.server, request);
236
278
  const betas = [
237
- ...(config.betas ?? []),
238
- ...(config.fallbacks !== undefined ? [FALLBACK_BETA] : []),
279
+ ...new Set([
280
+ ...(config.betas ?? []),
281
+ ...(config.fallbacks !== undefined ? [FALLBACK_BETA] : []),
282
+ ...server.betas,
283
+ ]),
239
284
  ];
240
285
  const outputConfig: Anthropic.Beta.BetaOutputConfig = {
241
286
  ...(request.effort !== undefined ? { effort: request.effort } : {}),
@@ -248,9 +293,21 @@ class AnthropicDriver implements PolarisDriver {
248
293
  }
249
294
  : {}),
250
295
  };
251
- const tools = request.tools ?? [];
296
+ const tools: Anthropic.Beta.BetaToolUnion[] = [
297
+ ...(request.tools ?? []).map((tool) => ({
298
+ name: tool.name,
299
+ description: tool.description,
300
+ input_schema: inputSchema(tool.name, tool.inputSchema),
301
+ })),
302
+ ...server.tools,
303
+ ];
252
304
 
253
305
  return {
306
+ // Before the raw fields, so `providerOptions.anthropic` can refine it
307
+ // (a 1-hour `ttl`).
308
+ ...(request.cache === true
309
+ ? { cache_control: { type: "ephemeral" as const } }
310
+ : {}),
254
311
  ...extra,
255
312
  model: request.model ?? config.model,
256
313
  max_tokens:
@@ -259,15 +316,8 @@ class AnthropicDriver implements PolarisDriver {
259
316
  (streaming ? DEFAULT_STREAM_MAX_TOKENS : DEFAULT_MAX_TOKENS),
260
317
  messages: toMessageParams(request.messages),
261
318
  ...(request.system !== undefined ? { system: request.system } : {}),
262
- ...(tools.length > 0
263
- ? {
264
- tools: tools.map((tool) => ({
265
- name: tool.name,
266
- description: tool.description,
267
- input_schema: inputSchema(tool.name, tool.inputSchema),
268
- })),
269
- }
270
- : {}),
319
+ ...(tools.length > 0 ? { tools } : {}),
320
+ ...server.fields,
271
321
  ...(request.toolChoice !== undefined
272
322
  ? { tool_choice: { type: request.toolChoice } }
273
323
  : {}),
@@ -292,6 +342,97 @@ class AnthropicDriver implements PolarisDriver {
292
342
  }
293
343
  }
294
344
 
345
+ /** What a request's server-side tools add to it. */
346
+ interface ServerParams {
347
+ readonly tools: Anthropic.Beta.BetaToolUnion[];
348
+ readonly fields: Pick<Params, "container" | "mcp_servers">;
349
+ readonly betas: readonly string[];
350
+ }
351
+
352
+ /**
353
+ * The request fields for the server-side tools: the config's, each one the
354
+ * request sets replacing it. Skills run in the code execution container, so
355
+ * they bring its tool; each MCP server is named by one `mcp_toolset`.
356
+ */
357
+ function serverParams(
358
+ defaults: AnthropicConfig["server"],
359
+ request: ChatRequest,
360
+ ): ServerParams {
361
+ const asked = request.server;
362
+ const skills = asked?.skills ?? defaults?.skills ?? [];
363
+ const mcpServers = asked?.mcpServers ?? defaults?.mcpServers ?? [];
364
+ const codeExecution =
365
+ skills.length > 0 ||
366
+ (asked?.codeExecution ?? defaults?.codeExecution) === true;
367
+ const container = asked?.container;
368
+
369
+ if (skills.length > MAX_SKILLS) {
370
+ throw new E_POLARIS_INVALID_REQUEST([
371
+ `Claude loads at most ${MAX_SKILLS} skills per request, not ${skills.length}`,
372
+ ]);
373
+ }
374
+ if (
375
+ codeExecution &&
376
+ (request.tools ?? []).some((tool) => tool.name === CODE_EXECUTION.name)
377
+ ) {
378
+ throw new E_POLARIS_INVALID_REQUEST([
379
+ `a tool is named '${CODE_EXECUTION.name}', the name of Claude's code execution tool`,
380
+ ]);
381
+ }
382
+
383
+ const tools: Anthropic.Beta.BetaToolUnion[] = [
384
+ ...(codeExecution ? [CODE_EXECUTION] : []),
385
+ ...mcpServers.map(
386
+ (mcp): Anthropic.Beta.BetaMCPToolset => ({
387
+ type: "mcp_toolset",
388
+ mcp_server_name: mcp.name,
389
+ // Only the tools named: the others are turned off.
390
+ ...(mcp.tools !== undefined
391
+ ? {
392
+ default_config: { enabled: false },
393
+ configs: Object.fromEntries(
394
+ mcp.tools.map((tool) => [tool, { enabled: true }]),
395
+ ),
396
+ }
397
+ : {}),
398
+ }),
399
+ ),
400
+ ];
401
+ const fields: ServerParams["fields"] = {
402
+ ...(skills.length > 0
403
+ ? {
404
+ container: {
405
+ ...(container !== undefined ? { id: container } : {}),
406
+ skills: skills.map((skill) => ({
407
+ type: skill.source,
408
+ skill_id: skill.id,
409
+ version: skill.version ?? "latest",
410
+ })),
411
+ },
412
+ }
413
+ : container !== undefined
414
+ ? { container }
415
+ : {}),
416
+ ...(mcpServers.length > 0
417
+ ? {
418
+ mcp_servers: mcpServers.map((mcp) => ({
419
+ type: "url" as const,
420
+ name: mcp.name,
421
+ url: mcp.url,
422
+ ...(mcp.authorizationToken !== undefined
423
+ ? { authorization_token: mcp.authorizationToken }
424
+ : {}),
425
+ })),
426
+ }
427
+ : {}),
428
+ };
429
+ return {
430
+ tools,
431
+ fields,
432
+ betas: mcpServers.length > 0 ? [MCP_BETA] : [],
433
+ };
434
+ }
435
+
295
436
  /** A structured output's schema as Claude takes it; a schema it cannot take is the request's fault. */
296
437
  function outputSchema(
297
438
  strictSchema: Loaded["strictSchema"],
@@ -422,15 +563,54 @@ function toResponse(message: Anthropic.Beta.BetaMessage): ChatResponse {
422
563
  model: message.model,
423
564
  content: message.content.map(toPart),
424
565
  stopReason: toStopReason(message.stop_reason),
425
- usage: {
426
- inputTokens: message.usage.input_tokens,
427
- outputTokens: message.usage.output_tokens,
428
- cacheReadTokens: message.usage.cache_read_input_tokens ?? 0,
429
- cacheWriteTokens: message.usage.cache_creation_input_tokens ?? 0,
430
- },
566
+ // As reported: `createResponse` checks the counts. A gateway speaking the
567
+ // API may leave usage out, whatever the SDK's types say.
568
+ usage:
569
+ typeof message.usage === "object" && message.usage !== null
570
+ ? {
571
+ inputTokens: message.usage.input_tokens,
572
+ outputTokens: message.usage.output_tokens,
573
+ cacheReadTokens: message.usage.cache_read_input_tokens ?? 0,
574
+ cacheWriteTokens: message.usage.cache_creation_input_tokens ?? 0,
575
+ serverToolUse: serverToolUse(message.usage.server_tool_use),
576
+ }
577
+ : undefined,
578
+ ...readContainer(message.container),
431
579
  });
432
580
  }
433
581
 
582
+ /**
583
+ * The server tool counters, renamed: `code_execution_requests` becomes
584
+ * `codeExecutionRequests`. Read whole, the SDK types only some of them.
585
+ */
586
+ function serverToolUse(reported: unknown): Record<string, unknown> | undefined {
587
+ if (typeof reported !== "object" || reported === null) return undefined;
588
+ return Object.fromEntries(
589
+ Object.entries(reported).map(([counter, count]) => [
590
+ counter.replace(/_([a-z0-9])/g, (_, letter: string) =>
591
+ letter.toUpperCase(),
592
+ ),
593
+ count,
594
+ ]),
595
+ );
596
+ }
597
+
598
+ /** The reply's container, when it ran code in one. */
599
+ function readContainer(
600
+ container: unknown,
601
+ ): { container: { id: string; expiresAt?: string } } | Record<string, never> {
602
+ if (typeof container !== "object" || container === null) return {};
603
+ const id: unknown = Reflect.get(container, "id");
604
+ if (typeof id !== "string" || id === "") return {};
605
+ const expiresAt: unknown = Reflect.get(container, "expires_at");
606
+ return {
607
+ container: {
608
+ id,
609
+ ...(typeof expiresAt === "string" ? { expiresAt } : {}),
610
+ },
611
+ };
612
+ }
613
+
434
614
  function toPart(block: Block): AssistantPart {
435
615
  const origin: Origin = { provider: PROVIDER, block };
436
616
  switch (block.type) {
@@ -482,8 +662,11 @@ function toStopReason(
482
662
 
483
663
  /** The polaris error for what the SDK raised. */
484
664
  function toPolarisError(sdk: Sdk, error: unknown): unknown {
485
- // polaris's own bounds, which the SDK wraps when they fire inside `fetch`.
486
- const bound = findCause(error, E_POLARIS_RESPONSE_TOO_LARGE);
665
+ // polaris's own bounds, which the SDK wraps when they fire inside `fetch`:
666
+ // the response size, and a redirect refused.
667
+ const bound =
668
+ findCause(error, E_POLARIS_RESPONSE_TOO_LARGE) ??
669
+ findCause(error, E_POLARIS_PROVIDER_REJECTED);
487
670
  if (bound !== undefined) return bound;
488
671
  // A cancelled call: polaris reports it from the signal.
489
672
  if (error instanceof sdk.APIUserAbortError) return error;