@providerkit/core 0.30.0 → 0.31.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 (132) hide show
  1. package/README.md +33 -5
  2. package/THIRD_PARTY_NOTICES +144 -0
  3. package/dist/auth/chatgpt-models.js.map +1 -1
  4. package/dist/auth/oauth.d.ts.map +1 -1
  5. package/dist/auth/oauth.js.map +1 -1
  6. package/dist/capability.js.map +1 -1
  7. package/dist/context.d.ts +4 -3
  8. package/dist/context.d.ts.map +1 -1
  9. package/dist/context.js +19 -5
  10. package/dist/context.js.map +1 -1
  11. package/dist/decisions.js.map +1 -1
  12. package/dist/errors.d.ts.map +1 -1
  13. package/dist/errors.js.map +1 -1
  14. package/dist/fallback.d.ts +3 -0
  15. package/dist/fallback.d.ts.map +1 -1
  16. package/dist/fallback.js +9 -5
  17. package/dist/fallback.js.map +1 -1
  18. package/dist/gate.js.map +1 -1
  19. package/dist/hosted/anthropic.d.ts +30 -0
  20. package/dist/hosted/anthropic.d.ts.map +1 -0
  21. package/dist/hosted/anthropic.js +465 -0
  22. package/dist/hosted/anthropic.js.map +1 -0
  23. package/dist/hosted/gemini.d.ts +22 -0
  24. package/dist/hosted/gemini.d.ts.map +1 -0
  25. package/dist/hosted/gemini.js +440 -0
  26. package/dist/hosted/gemini.js.map +1 -0
  27. package/dist/hosted/openai.d.ts +33 -0
  28. package/dist/hosted/openai.d.ts.map +1 -0
  29. package/dist/hosted/openai.js +107 -0
  30. package/dist/hosted/openai.js.map +1 -0
  31. package/dist/hosted/responses.d.ts +28 -0
  32. package/dist/hosted/responses.d.ts.map +1 -0
  33. package/dist/hosted/responses.js +358 -0
  34. package/dist/hosted/responses.js.map +1 -0
  35. package/dist/hosted/shared.d.ts +107 -0
  36. package/dist/hosted/shared.d.ts.map +1 -0
  37. package/dist/hosted/shared.js +264 -0
  38. package/dist/hosted/shared.js.map +1 -0
  39. package/dist/hosted/types.d.ts +191 -0
  40. package/dist/hosted/types.d.ts.map +1 -0
  41. package/dist/hosted/types.js +10 -0
  42. package/dist/hosted/types.js.map +1 -0
  43. package/dist/hosted.d.ts +33 -0
  44. package/dist/hosted.d.ts.map +1 -0
  45. package/dist/hosted.js +61 -0
  46. package/dist/hosted.js.map +1 -0
  47. package/dist/index.d.ts +8 -7
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +8 -7
  50. package/dist/index.js.map +1 -1
  51. package/dist/key-pool.d.ts.map +1 -1
  52. package/dist/key-pool.js.map +1 -1
  53. package/dist/presets.d.ts +2 -2
  54. package/dist/presets.d.ts.map +1 -1
  55. package/dist/providers/anthropic.d.ts +75 -1
  56. package/dist/providers/anthropic.d.ts.map +1 -1
  57. package/dist/providers/anthropic.js +207 -135
  58. package/dist/providers/anthropic.js.map +1 -1
  59. package/dist/providers/factory.d.ts +10 -0
  60. package/dist/providers/factory.d.ts.map +1 -1
  61. package/dist/providers/factory.js +31 -23
  62. package/dist/providers/factory.js.map +1 -1
  63. package/dist/providers/gemini.d.ts +123 -1
  64. package/dist/providers/gemini.d.ts.map +1 -1
  65. package/dist/providers/gemini.js +114 -54
  66. package/dist/providers/gemini.js.map +1 -1
  67. package/dist/providers/hosted-codecs.d.ts +10 -0
  68. package/dist/providers/hosted-codecs.d.ts.map +1 -0
  69. package/dist/providers/hosted-codecs.js +8 -0
  70. package/dist/providers/hosted-codecs.js.map +1 -0
  71. package/dist/providers/openai.d.ts +69 -0
  72. package/dist/providers/openai.d.ts.map +1 -1
  73. package/dist/providers/openai.js +38 -14
  74. package/dist/providers/openai.js.map +1 -1
  75. package/dist/providers/responses.d.ts +123 -0
  76. package/dist/providers/responses.d.ts.map +1 -1
  77. package/dist/providers/responses.js +57 -22
  78. package/dist/providers/responses.js.map +1 -1
  79. package/dist/providers/speech-factory.js.map +1 -1
  80. package/dist/registry.d.ts +3 -0
  81. package/dist/registry.d.ts.map +1 -1
  82. package/dist/registry.js +8 -4
  83. package/dist/registry.js.map +1 -1
  84. package/dist/retry.js.map +1 -1
  85. package/dist/speech-core.d.ts.map +1 -1
  86. package/dist/speech-core.js.map +1 -1
  87. package/dist/tools.d.ts.map +1 -1
  88. package/dist/tools.js.map +1 -1
  89. package/dist/transcription-core.d.ts.map +1 -1
  90. package/dist/transcription-core.js.map +1 -1
  91. package/dist/transport.js.map +1 -1
  92. package/dist/turn.d.ts +10 -1
  93. package/dist/turn.d.ts.map +1 -1
  94. package/dist/turn.js +53 -1
  95. package/dist/turn.js.map +1 -1
  96. package/dist/types.d.ts +51 -0
  97. package/dist/types.d.ts.map +1 -1
  98. package/dist/types.js.map +1 -1
  99. package/dist/usage.d.ts +10 -1
  100. package/dist/usage.d.ts.map +1 -1
  101. package/dist/usage.js +40 -2
  102. package/dist/usage.js.map +1 -1
  103. package/dist/watchdog.d.ts +6 -0
  104. package/dist/watchdog.d.ts.map +1 -1
  105. package/dist/watchdog.js +10 -2
  106. package/dist/watchdog.js.map +1 -1
  107. package/package.json +10 -5
  108. package/src/context.ts +21 -5
  109. package/src/fallback.ts +21 -6
  110. package/src/hosted/anthropic.ts +512 -0
  111. package/src/hosted/gemini.ts +510 -0
  112. package/src/hosted/openai.ts +137 -0
  113. package/src/hosted/responses.ts +420 -0
  114. package/src/hosted/shared.ts +341 -0
  115. package/src/hosted/types.ts +214 -0
  116. package/src/hosted.ts +101 -0
  117. package/src/index.ts +64 -7
  118. package/src/providers/anthropic.ts +252 -143
  119. package/src/providers/factory.ts +127 -82
  120. package/src/providers/gemini.ts +184 -60
  121. package/src/providers/hosted-codecs.ts +29 -0
  122. package/src/providers/openai.ts +52 -15
  123. package/src/providers/responses.ts +93 -24
  124. package/src/registry.ts +14 -4
  125. package/src/turn.ts +55 -1
  126. package/src/types.ts +62 -1
  127. package/src/usage.ts +32 -2
  128. package/src/watchdog.ts +12 -2
  129. package/dist/jev.d.ts +0 -123
  130. package/dist/jev.d.ts.map +0 -1
  131. package/dist/jev.js +0 -278
  132. package/dist/jev.js.map +0 -1
package/src/index.ts CHANGED
@@ -1,8 +1,22 @@
1
1
  export * from "./types.ts";
2
+ export * from "./hosted/types.ts";
2
3
  export * from "./turn.ts";
3
4
  export * from "./errors.ts";
4
5
  export * from "./retry.ts";
5
- export * from "./fallback.ts";
6
+ export {
7
+ DEFAULT_FALLBACK_COOLDOWNS,
8
+ FallbackPool,
9
+ NoAvailableProviderError,
10
+ withConfiguredFallbacks,
11
+ withFallbackProviders,
12
+ type FallbackOptions,
13
+ type FallbackSpec,
14
+ type FallbackCandidate,
15
+ type ProviderFallbackConfig,
16
+ type Cooldown,
17
+ type CooldownStore,
18
+ type FallbackProvider,
19
+ } from "./fallback.ts";
6
20
  export * from "./key-pool.ts";
7
21
  export * from "./watchdog.ts";
8
22
  export * from "./gate.ts";
@@ -31,10 +45,53 @@ export * from "./image.ts";
31
45
  export * from "./capability.ts";
32
46
  export * from "./attribution.ts";
33
47
  export * from "./presets.ts";
34
- export * from "./providers/anthropic.ts";
35
- export * from "./providers/openai.ts";
36
- export * from "./providers/responses.ts";
37
- export * from "./providers/gemini.ts";
48
+ export {
49
+ createAnthropicProvider,
50
+ toAnthropicMessages,
51
+ type AnthropicConfig,
52
+ } from "./providers/anthropic.ts";
53
+ export {
54
+ createOpenAIProvider,
55
+ toOpenAIMessages,
56
+ effortParams,
57
+ openRouterCostUsd,
58
+ openRouterHostFor,
59
+ type OpenAIConfig,
60
+ type EffortDialect,
61
+ } from "./providers/openai.ts";
62
+ export {
63
+ createResponsesProvider,
64
+ toResponsesInput,
65
+ type ResponsesConfig,
66
+ } from "./providers/responses.ts";
67
+ export {
68
+ createGeminiProvider,
69
+ toGeminiContents,
70
+ antigravityUserAgent,
71
+ antigravitySessionId,
72
+ resolveAntigravityWireModel,
73
+ type GeminiConfig,
74
+ type GeminiContent,
75
+ } from "./providers/gemini.ts";
38
76
  export * from "./providers/zai.ts";
39
- export * from "./providers/factory.ts";
40
- export * from "./registry.ts";
77
+ export {
78
+ createPresetProvider,
79
+ presetAdapter,
80
+ OPENCODE_CLI_USER_AGENT,
81
+ OPENCODE_PUBLIC_API_KEY,
82
+ isZenFreeModel,
83
+ forgeOpenCodeSessionId,
84
+ type PresetProviderConfig,
85
+ type PresetAdapterConfig,
86
+ } from "./providers/factory.ts";
87
+ export {
88
+ createRegistry,
89
+ type ConnectionCredential,
90
+ type ConnectionConfig,
91
+ type RegistryCooldownInfo,
92
+ type RegistryOptions,
93
+ type ViewOptions,
94
+ type CandidateStatus,
95
+ type ConnectionStatus,
96
+ type Registry,
97
+ } from "./registry.ts";
@@ -1,4 +1,10 @@
1
1
  // Anthropic-shape adapter — SSE from POST /v1/messages.
2
+ //
3
+ // Hosted tool shapes, betas and result blocks follow Vercel's AI SDK
4
+ // (Apache-2.0, Copyright 2023 Vercel, Inc.: anthropic-prepare-tools.ts,
5
+ // anthropic-language-model.ts, convert-to-anthropic-prompt.ts); the
6
+ // continuation and the citation and container rules follow PydanticAI (MIT).
7
+ // See THIRD_PARTY_NOTICES.
2
8
  import { repairHistory } from "../context.ts";
3
9
  import {
4
10
  fileRefused,
@@ -7,11 +13,13 @@ import {
7
13
  streamError,
8
14
  validateMaxAttempts,
9
15
  } from "../errors.ts";
16
+ import type { HostedToolType } from "../hosted/types.ts";
17
+ import { refuseHostedTools, type HostedCodecs } from "./hosted-codecs.ts";
10
18
  import { schemaPrompt, toAnthropicToolSchema } from "../schema.ts";
11
19
  import { parseToolArgs, settleToolCalls, type ToolCallIdentity } from "../tool-args.ts";
12
20
  import { streamSse, apiUrl, parseFrame, type FetchLike } from "../transport.ts";
13
21
  import { attributionHeaders } from "../attribution.ts";
14
- import { withConfiguredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
22
+ import { configuredFallbacks, type ProviderFallbackConfig } from "../fallback.ts";
15
23
  import type {
16
24
  JsonOutput,
17
25
  ChatMessage,
@@ -78,9 +86,19 @@ export interface AnthropicConfig extends ProviderFallbackConfig {
78
86
  * `CLAUDE_THINKING`), and several reject this marker outright. Claude 4.5
79
87
  * and older accept it, and get it. */
80
88
  explicitNone?: boolean;
89
+ /**
90
+ * For providers imported from `@providerkit/core/hosted`.
91
+ * The hosted tools this endpoint runs. Defaults to web search, web fetch and
92
+ * code execution on api.anthropic.com and to none anywhere else: Bedrock,
93
+ * Vertex and the Anthropic-compatible gateways each run a different set, or
94
+ * none, and one that ignores a tool answers unsearched without saying so.
95
+ * Set it once you have checked the endpoint.
96
+ */
97
+ servesHostedTools?: readonly HostedToolType[];
81
98
  }
82
99
 
83
100
  const DEFAULT_BASE_URL = "https://api.anthropic.com";
101
+
84
102
  const DEFAULT_VERSION = "2023-06-01";
85
103
  /** Anthropic rejects a request without one, so a default is not optional. */
86
104
  const DEFAULT_MAX_TOKENS = 8_192;
@@ -209,9 +227,8 @@ function continuesToolLoop(messages: readonly ChatMessage[]): boolean {
209
227
  * A stated stop, or a throw for one this adapter does not know. An unknown
210
228
  * reason used to end the stream as a clean success with no finish at all.
211
229
  * Nothing says the answer ended on purpose, so it is `overload`: theirs, and
212
- * worth a retry (cc-proxy answers it the same way). `pause_turn` lands there
213
- * too: it is a server-tool turn waiting to be sent back, which this adapter
214
- * never asks for, and a half answer is not a finished one.
230
+ * worth a retry (cc-proxy answers it the same way). `pause_turn` never reaches
231
+ * here: the stream sends the paused turn back and carries on (see below).
215
232
  */
216
233
  function mapStopReason(provider: string, reason: string): FinishReason {
217
234
  switch (reason) {
@@ -325,12 +342,26 @@ function anthropicIds(): (id: string) => string {
325
342
  };
326
343
  }
327
344
 
345
+ export interface AnthropicMessage {
346
+ role: string;
347
+ content: unknown[];
348
+ }
349
+
328
350
  export function toAnthropicMessages(
329
351
  messages: readonly ChatMessage[],
330
352
  provider = "anthropic",
353
+ ): { system?: unknown[]; messages: AnthropicMessage[] } {
354
+ return anthropicMessages(messages, provider);
355
+ }
356
+
357
+ /** @internal Hosted replay stays outside the plain converter. */
358
+ export function anthropicMessages(
359
+ messages: readonly ChatMessage[],
360
+ provider = "anthropic",
361
+ replay?: HostedCodecs["anthropic"]["replay"],
331
362
  ): {
332
363
  system?: unknown[];
333
- messages: unknown[];
364
+ messages: AnthropicMessage[];
334
365
  } {
335
366
  const wireId = anthropicIds();
336
367
  const system = messages
@@ -338,7 +369,13 @@ export function toAnthropicMessages(
338
369
  .map((m) => m.content)
339
370
  .join("\n\n");
340
371
 
341
- const out: { role: string; content: unknown[] }[] = [];
372
+ const toolUse = (call: { id: string; name: string; arguments: string }) => ({
373
+ type: "tool_use",
374
+ id: wireId(call.id),
375
+ name: call.name,
376
+ input: parseToolArgs(call.arguments),
377
+ });
378
+ const out: AnthropicMessage[] = [];
342
379
  const pushBlocks = (role: string, blocks: unknown[]) => {
343
380
  const last = out[out.length - 1];
344
381
  if (last?.role === role) last.content.push(...blocks);
@@ -370,16 +407,19 @@ export function toAnthropicMessages(
370
407
  // assistant. Reasoning is deliberately NOT replayed: Anthropic's thinking
371
408
  // blocks carry signatures we never captured, and a block without its
372
409
  // signature is rejected.
410
+ //
411
+ // A hosted turn this provider made goes back as the blocks it came in, so
412
+ // the model keeps the search results it read and the code it ran, and
413
+ // each text block keeps the citations it was given. Its text and calls
414
+ // come from the message, so an id `repairHistory` renamed still pairs.
415
+ const replayed = replay?.(message, provider, toolUse);
416
+ if (replayed) {
417
+ pushBlocks("assistant", replayed);
418
+ continue;
419
+ }
373
420
  const blocks: unknown[] = [];
374
421
  if (message.content) blocks.push({ type: "text", text: message.content });
375
- for (const call of message.toolCalls ?? []) {
376
- blocks.push({
377
- type: "tool_use",
378
- id: wireId(call.id),
379
- name: call.name,
380
- input: parseToolArgs(call.arguments),
381
- });
382
- }
422
+ for (const call of message.toolCalls ?? []) blocks.push(toolUse(call));
383
423
  if (blocks.length > 0) pushBlocks("assistant", blocks);
384
424
  }
385
425
 
@@ -387,25 +427,44 @@ export function toAnthropicMessages(
387
427
  return { ...(blocks ? { system: blocks } : {}), messages: out };
388
428
  }
389
429
 
390
- interface AnthropicUsage {
430
+ export interface AnthropicUsage {
391
431
  input_tokens?: number;
392
432
  output_tokens?: number;
393
433
  cache_read_input_tokens?: number;
394
434
  cache_creation_input_tokens?: number;
435
+ /** The server tool calls this response billed. */
436
+ server_tool_use?: {
437
+ web_search_requests?: number;
438
+ web_fetch_requests?: number;
439
+ code_execution_requests?: number;
440
+ } | null;
441
+ /** Each model pass of the response, on the betas that report them. */
442
+ iterations?:
443
+ | {
444
+ type?: string;
445
+ input_tokens?: number;
446
+ cache_read_input_tokens?: number;
447
+ cache_creation_input_tokens?: number;
448
+ }[]
449
+ | null;
395
450
  }
396
451
 
397
- interface AnthropicEvent {
452
+ export interface AnthropicEvent {
398
453
  type?: string;
399
454
  message?: {
400
455
  usage?: AnthropicUsage;
456
+ container?: { id?: string } | null;
401
457
  };
402
- content_block?: { type?: string; id?: string; name?: string };
458
+ content_block?: { type?: string; id?: string; name?: string; [field: string]: unknown };
403
459
  delta?: {
404
460
  type?: string;
405
461
  text?: string;
406
462
  thinking?: string;
463
+ signature?: string;
407
464
  partial_json?: string;
465
+ citation?: unknown;
408
466
  stop_reason?: string;
467
+ container?: { id?: string } | null;
409
468
  };
410
469
  usage?: AnthropicUsage;
411
470
  index?: number;
@@ -413,6 +472,11 @@ interface AnthropicEvent {
413
472
  }
414
473
 
415
474
  export function createAnthropicProvider(config: AnthropicConfig): Provider {
475
+ return anthropicProvider(config);
476
+ }
477
+
478
+ /** @internal Shared by the plain and hosted entry points. */
479
+ export function anthropicProvider(config: AnthropicConfig, codecs?: HostedCodecs): Provider {
416
480
  const baseUrl = config.baseUrl ?? DEFAULT_BASE_URL;
417
481
  const id = config.id ?? "anthropic";
418
482
  const instanceSessionId = config.sessionHeader
@@ -429,13 +493,14 @@ export function createAnthropicProvider(config: AnthropicConfig): Provider {
429
493
  opts: StreamOptions = {},
430
494
  ): AsyncIterable<ProviderChunk> {
431
495
  validateMaxAttempts(opts.maxAttempts);
496
+ if (!codecs) refuseHostedTools(id, opts, messages);
432
497
  const model = opts.model ?? config.model;
433
498
  const maxTokens = opts.maxTokens ?? config.maxTokens ?? DEFAULT_MAX_TOKENS;
434
499
  const effort = opts.effort ?? config.effort;
435
500
  // Repaired first: a result with no call, a call with no result, or an id
436
501
  // used twice is a 400 that loses the turn. See repairHistory.
437
502
  const sent = repairHistory(messages).messages;
438
- const { system, messages: body } = toAnthropicMessages(sent, id);
503
+ const { system, messages: body } = anthropicMessages(sent, id, codecs?.anthropic.replay);
439
504
 
440
505
  // Anthropic's own `service_tier` means auto or standard_only, not this.
441
506
  if (opts.serviceTier) {
@@ -468,12 +533,16 @@ export function createAnthropicProvider(config: AnthropicConfig): Provider {
468
533
  const named = typeof opts.toolChoice === "object" ? opts.toolChoice.name : undefined;
469
534
  const offered =
470
535
  unforced && named !== undefined ? tools.filter((tool) => tool.name === named) : tools;
471
- if (offered.length > 0) {
472
- request.tools = offered.map((tool) => ({
473
- name: tool.name,
474
- description: tool.description,
475
- input_schema: toAnthropicToolSchema(tool.inputSchema),
476
- }));
536
+ const hosted = codecs?.anthropic.plan(config, baseUrl, id, offered, opts);
537
+ if (offered.length > 0 || (hosted?.tools.length ?? 0) > 0) {
538
+ request.tools = [
539
+ ...offered.map((tool) => ({
540
+ name: tool.name,
541
+ description: tool.description,
542
+ input_schema: toAnthropicToolSchema(tool.inputSchema),
543
+ })),
544
+ ...(hosted?.tools ?? []),
545
+ ];
477
546
  }
478
547
  const choice =
479
548
  opts.toolChoice && opts.toolChoice !== "auto" && !unforced
@@ -534,145 +603,185 @@ export function createAnthropicProvider(config: AnthropicConfig): Provider {
534
603
  // EXCLUDED from `input_tokens` — where the OpenAI shapes report a cached
535
604
  // subset already inside the prompt count. Reconciling here is what keeps
536
605
  // one usage record meaningful across both, and a cost figure honest.
537
- let freshInputTokens = 0;
538
- let cachedInputTokens = 0;
539
- let cacheWriteTokens = 0;
540
- let outputTokens = 0;
606
+ //
607
+ // Summed over the passes of a paused turn: each pass is a response of its
608
+ // own, and each is billed.
609
+ const billed = { fresh: 0, cached: 0, written: 0, output: 0 };
541
610
  let toolCall: ToolCallIdentity | null = null;
542
611
  let sawToolCall = false;
543
612
  let blockIndex = -1;
544
- // What proves the turn ended rather than the socket: a stated stop
545
- // reason, or `message_stop`. See the check after the loop.
546
- let stopped = false;
547
- const usageChunk = (): ProviderChunk => ({
548
- type: "usage",
549
- usage: {
550
- inputTokens: freshInputTokens + cachedInputTokens + cacheWriteTokens,
551
- cachedInputTokens,
552
- cacheWriteTokens,
553
- outputTokens,
554
- },
555
- });
613
+ const hostedStream = codecs?.anthropic.stream(sent, id);
614
+ const usageChunk = (): ProviderChunk => {
615
+ return {
616
+ type: "usage",
617
+ usage: {
618
+ inputTokens: billed.fresh + billed.cached + billed.written,
619
+ cachedInputTokens: billed.cached,
620
+ cacheWriteTokens: billed.written,
621
+ outputTokens: billed.output,
622
+ ...hostedStream?.usage(),
623
+ },
624
+ };
625
+ };
626
+ const baseHeaders = {
627
+ "anthropic-version": config.version ?? DEFAULT_VERSION,
628
+ ...(config.bearer
629
+ ? { authorization: `Bearer ${config.apiKey}` }
630
+ : { "x-api-key": config.apiKey }),
631
+ ...attributionHeaders(config, config.headers),
632
+ ...(config.sessionHeader
633
+ ? { [config.sessionHeader]: opts.sessionId ?? instanceSessionId }
634
+ : {}),
635
+ ...config.headers,
636
+ };
637
+ const headers = codecs?.anthropic.headers(baseHeaders, hosted?.betas ?? []) ?? baseHeaders;
638
+
639
+ // One pass per response. A server tool loop that runs long ends its
640
+ // response on `pause_turn`, and the turn goes on only once the paused
641
+ // content is sent back as the last assistant message. The reader sees
642
+ // one turn: the next pass's blocks continue at the same offset.
643
+ // (PydanticAI's accumulate loop, MIT, models/_continuation.py:1-24.)
644
+ for (let pauses = 0; ; pauses++) {
645
+ // What proves the pass ended rather than the socket: a stated stop
646
+ // reason, or `message_stop`. See the check after the loop.
647
+ let stopped = false;
648
+ let ended = false;
649
+ let paused = false;
650
+ let fresh = 0;
651
+ let cached = 0;
652
+ let written = 0;
653
+ let output = 0;
654
+ const payload = hostedStream?.payload(request, body) ?? request;
655
+
656
+ read: for await (const data of streamSse({
657
+ url: apiUrl(baseUrl, "/v1/messages"),
658
+ headers,
659
+ body: payload,
660
+ provider: id,
661
+ ...(opts.signal ? { signal: opts.signal } : {}),
662
+ ...(config.fetchImpl ? { fetchImpl: config.fetchImpl } : {}),
663
+ ...(opts.onActivity ? { onActivity: opts.onActivity } : {}),
664
+ ...(opts.onRateLimit ? { onRateLimit: opts.onRateLimit } : {}),
665
+ // The stop reason rides in `message_delta` with the final usage, so a
666
+ // read that dies after it loses nothing but `message_stop`.
667
+ finished: () => stopped,
668
+ })) {
669
+ const event = parseFrame<AnthropicEvent>(data, id);
670
+ if (hostedStream) yield* hostedStream.event(event);
671
+
672
+ switch (event.type) {
673
+ // A failure the backend reports after its headers went out. Classified
674
+ // rather than assumed transient: this shape carries `overloaded_error`
675
+ // most of the time, but a prompt found too long mid-stream arrives the
676
+ // same way, and retrying that one only fails it again more slowly.
677
+ case "error":
678
+ throw streamError(id, event);
679
+
680
+ case "message_start": {
681
+ const usage = event.message?.usage;
682
+ cached = usage?.cache_read_input_tokens ?? 0;
683
+ written = usage?.cache_creation_input_tokens ?? 0;
684
+ fresh = usage?.input_tokens ?? 0;
685
+ break;
686
+ }
556
687
 
557
- for await (const data of streamSse({
558
- url: apiUrl(baseUrl, "/v1/messages"),
559
- headers: {
560
- "anthropic-version": config.version ?? DEFAULT_VERSION,
561
- ...(config.bearer
562
- ? { authorization: `Bearer ${config.apiKey}` }
563
- : { "x-api-key": config.apiKey }),
564
- ...attributionHeaders(config, config.headers),
565
- ...(config.sessionHeader
566
- ? { [config.sessionHeader]: opts.sessionId ?? instanceSessionId }
567
- : {}),
568
- ...config.headers,
569
- },
570
- body: request,
571
- provider: id,
572
- ...(opts.signal ? { signal: opts.signal } : {}),
573
- ...(config.fetchImpl ? { fetchImpl: config.fetchImpl } : {}),
574
- ...(opts.onActivity ? { onActivity: opts.onActivity } : {}),
575
- ...(opts.onRateLimit ? { onRateLimit: opts.onRateLimit } : {}),
576
- // The stop reason rides in `message_delta` with the final usage, so a
577
- // read that dies after it loses nothing but `message_stop`.
578
- finished: () => stopped,
579
- })) {
580
- const event = parseFrame<AnthropicEvent>(data, id);
581
-
582
- switch (event.type) {
583
- // A failure the backend reports after its headers went out. Classified
584
- // rather than assumed transient: this shape carries `overloaded_error`
585
- // most of the time, but a prompt found too long mid-stream arrives the
586
- // same way, and retrying that one only fails it again more slowly.
587
- case "error":
588
- throw streamError(id, event);
589
-
590
- case "message_start": {
591
- const usage = event.message?.usage;
592
- cachedInputTokens = usage?.cache_read_input_tokens ?? 0;
593
- cacheWriteTokens = usage?.cache_creation_input_tokens ?? 0;
594
- freshInputTokens = usage?.input_tokens ?? 0;
595
- break;
596
- }
688
+ case "content_block_start": {
689
+ blockIndex += 1;
690
+ const block: Record<string, unknown> = { ...event.content_block };
691
+ if (block.type === "tool_use") {
692
+ toolCall = {
693
+ index: blockIndex,
694
+ id: typeof block.id === "string" ? block.id : "",
695
+ name: typeof block.name === "string" ? block.name : "",
696
+ };
697
+ // Checked here, because this block is the only place this shape
698
+ // states a call's id and name.
699
+ settleToolCalls(id, [toolCall]);
700
+ sawToolCall = true;
701
+ yield {
702
+ type: "delta",
703
+ toolCalls: [{ index: toolCall.index, id: toolCall.id, name: toolCall.name }],
704
+ };
705
+ }
706
+ break;
707
+ }
597
708
 
598
- case "content_block_start": {
599
- blockIndex += 1;
600
- if (event.content_block?.type === "tool_use") {
601
- toolCall = {
602
- index: blockIndex,
603
- id: event.content_block.id ?? "",
604
- name: event.content_block.name ?? "",
605
- };
606
- // Checked here, because this block is the only place this shape
607
- // states a call's id and name.
608
- settleToolCalls(id, [toolCall]);
609
- sawToolCall = true;
610
- yield {
611
- type: "delta",
612
- toolCalls: [{ index: toolCall.index, id: toolCall.id, name: toolCall.name }],
613
- };
709
+ case "content_block_delta": {
710
+ const delta = event.delta;
711
+ if (delta?.type === "text_delta" && delta.text) {
712
+ yield { type: "delta", content: delta.text };
713
+ } else if (delta?.type === "thinking_delta" && delta.thinking) {
714
+ yield { type: "delta", reasoning: delta.thinking };
715
+ } else if (delta?.type === "input_json_delta") {
716
+ // Buffered per block. A server tool's input is the vendor's to
717
+ // run, and fed into a function call's arguments it corrupts
718
+ // them (TanStack ai-anthropic text.ts:1706-1710).
719
+ if (toolCall) {
720
+ yield {
721
+ type: "delta",
722
+ toolCalls: [{ index: toolCall.index, arguments: delta.partial_json ?? "" }],
723
+ };
724
+ }
725
+ }
726
+ break;
614
727
  }
615
- break;
616
- }
617
728
 
618
- case "content_block_delta": {
619
- const delta = event.delta;
620
- if (delta?.type === "text_delta" && delta.text) {
621
- yield { type: "delta", content: delta.text };
622
- } else if (delta?.type === "thinking_delta" && delta.thinking) {
623
- yield { type: "delta", reasoning: delta.thinking };
624
- } else if (delta?.type === "input_json_delta" && toolCall) {
625
- yield {
626
- type: "delta",
627
- toolCalls: [{ index: toolCall.index, arguments: delta.partial_json ?? "" }],
628
- };
729
+ case "content_block_stop": {
730
+ toolCall = null;
731
+ break;
629
732
  }
630
- break;
631
- }
632
733
 
633
- case "content_block_stop":
634
- toolCall = null;
635
- break;
636
-
637
- case "message_delta": {
638
- // Z.ai starts with zero input usage and reports the real totals here.
639
- // These are cumulative counters, not increments. Native Anthropic
640
- // usually sends only output_tokens here, so absent fields retain theirs.
641
- freshInputTokens = event.usage?.input_tokens ?? freshInputTokens;
642
- cachedInputTokens = event.usage?.cache_read_input_tokens ?? cachedInputTokens;
643
- cacheWriteTokens = event.usage?.cache_creation_input_tokens ?? cacheWriteTokens;
644
- outputTokens = event.usage?.output_tokens ?? outputTokens;
645
- if (event.delta?.stop_reason) {
646
- const stated = mapStopReason(id, event.delta.stop_reason);
734
+ case "message_delta": {
735
+ // Z.ai starts with zero input usage and reports the real totals here.
736
+ // These are cumulative counters, not increments. Native Anthropic
737
+ // usually sends only output_tokens here, so absent fields retain theirs.
738
+ fresh = event.usage?.input_tokens ?? fresh;
739
+ cached = event.usage?.cache_read_input_tokens ?? cached;
740
+ written = event.usage?.cache_creation_input_tokens ?? written;
741
+ output = event.usage?.output_tokens ?? output;
742
+ const reason = event.delta?.stop_reason;
743
+ if (!reason) break;
647
744
  stopped = true;
745
+ if (reason === "pause_turn" && hostedStream) {
746
+ paused = true;
747
+ break;
748
+ }
749
+ const stated = mapStopReason(id, reason);
750
+ if (hostedStream) yield* hostedStream.close();
648
751
  // Anthropic-compatible gateways can end a tool turn on
649
752
  // `end_turn`, and a caller reading that as done never runs the
650
753
  // tool. Only `stop` is promoted: a length cut may be what broke
651
- // the call.
754
+ // the call. Hosted calls never count: the loop runs none of them.
652
755
  const finishReason = stated === "stop" && sawToolCall ? "tool_calls" : stated;
653
756
  yield { type: "finish", finishReason };
757
+ break;
654
758
  }
655
- break;
759
+
760
+ // The pass's last event, so the reader stops here. Some gateways
761
+ // append a `[DONE]` or other bytes after it, and a read that died on
762
+ // those failed an answer that was already whole (Kimi, GLM and
763
+ // OpenCode Go, measured by cc-proxy).
764
+ case "message_stop":
765
+ ended = true;
766
+ break read;
656
767
  }
768
+ }
657
769
 
658
- // The turn's last event, so the reader stops here. Some gateways
659
- // append a `[DONE]` or other bytes after it, and a read that died on
660
- // those failed an answer that was already whole (Kimi, GLM and
661
- // OpenCode Go, measured by cc-proxy).
662
- case "message_stop":
663
- yield usageChunk();
664
- return;
770
+ billed.fresh += fresh;
771
+ billed.cached += cached;
772
+ billed.written += written;
773
+ billed.output += output;
774
+ // A gateway that states its stop reason and then skips `message_stop`,
775
+ // or whose socket dies after the reason, still finished the turn, and
776
+ // still billed it.
777
+ if (!stopped && !ended) throw streamCut(id);
778
+ hostedStream?.endPass(paused, pauses);
779
+ if (!paused) {
780
+ yield usageChunk();
781
+ return;
665
782
  }
666
783
  }
667
- // A gateway that states its stop reason and then skips `message_stop`, or
668
- // whose socket dies after the reason, still finished the turn, and still
669
- // billed it.
670
- if (stopped) {
671
- yield usageChunk();
672
- return;
673
- }
674
- throw streamCut(id);
675
784
  },
676
785
  };
677
- return withConfiguredFallbacks(provider, config);
786
+ return configuredFallbacks(provider, config, codecs);
678
787
  }