@providerkit/core 0.30.0 → 0.32.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.
- package/README.md +101 -5
- package/THIRD_PARTY_NOTICES +144 -0
- package/dist/auth/chatgpt-models.js.map +1 -1
- package/dist/auth/oauth.d.ts.map +1 -1
- package/dist/auth/oauth.js.map +1 -1
- package/dist/capability.js.map +1 -1
- package/dist/context.d.ts +4 -3
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +19 -5
- package/dist/context.js.map +1 -1
- package/dist/decisions.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/fallback.d.ts +3 -0
- package/dist/fallback.d.ts.map +1 -1
- package/dist/fallback.js +9 -5
- package/dist/fallback.js.map +1 -1
- package/dist/gate.js.map +1 -1
- package/dist/hosted/anthropic.d.ts +30 -0
- package/dist/hosted/anthropic.d.ts.map +1 -0
- package/dist/hosted/anthropic.js +465 -0
- package/dist/hosted/anthropic.js.map +1 -0
- package/dist/hosted/gemini.d.ts +22 -0
- package/dist/hosted/gemini.d.ts.map +1 -0
- package/dist/hosted/gemini.js +440 -0
- package/dist/hosted/gemini.js.map +1 -0
- package/dist/hosted/openai.d.ts +33 -0
- package/dist/hosted/openai.d.ts.map +1 -0
- package/dist/hosted/openai.js +107 -0
- package/dist/hosted/openai.js.map +1 -0
- package/dist/hosted/responses.d.ts +28 -0
- package/dist/hosted/responses.d.ts.map +1 -0
- package/dist/hosted/responses.js +358 -0
- package/dist/hosted/responses.js.map +1 -0
- package/dist/hosted/shared.d.ts +107 -0
- package/dist/hosted/shared.d.ts.map +1 -0
- package/dist/hosted/shared.js +264 -0
- package/dist/hosted/shared.js.map +1 -0
- package/dist/hosted/types.d.ts +191 -0
- package/dist/hosted/types.d.ts.map +1 -0
- package/dist/hosted/types.js +10 -0
- package/dist/hosted/types.js.map +1 -0
- package/dist/hosted.d.ts +33 -0
- package/dist/hosted.d.ts.map +1 -0
- package/dist/hosted.js +61 -0
- package/dist/hosted.js.map +1 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +1 -1
- package/dist/key-pool.d.ts.map +1 -1
- package/dist/key-pool.js.map +1 -1
- package/dist/otel.d.ts +26 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +263 -0
- package/dist/otel.js.map +1 -0
- package/dist/presets.d.ts +2 -2
- package/dist/presets.d.ts.map +1 -1
- package/dist/providers/anthropic.d.ts +75 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +207 -135
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/factory.d.ts +10 -0
- package/dist/providers/factory.d.ts.map +1 -1
- package/dist/providers/factory.js +31 -23
- package/dist/providers/factory.js.map +1 -1
- package/dist/providers/gemini.d.ts +123 -1
- package/dist/providers/gemini.d.ts.map +1 -1
- package/dist/providers/gemini.js +114 -54
- package/dist/providers/gemini.js.map +1 -1
- package/dist/providers/hosted-codecs.d.ts +10 -0
- package/dist/providers/hosted-codecs.d.ts.map +1 -0
- package/dist/providers/hosted-codecs.js +8 -0
- package/dist/providers/hosted-codecs.js.map +1 -0
- package/dist/providers/openai.d.ts +69 -0
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +38 -14
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/responses.d.ts +123 -0
- package/dist/providers/responses.d.ts.map +1 -1
- package/dist/providers/responses.js +57 -22
- package/dist/providers/responses.js.map +1 -1
- package/dist/providers/speech-factory.js.map +1 -1
- package/dist/registry.d.ts +3 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +8 -4
- package/dist/registry.js.map +1 -1
- package/dist/retry.js.map +1 -1
- package/dist/speech-core.d.ts.map +1 -1
- package/dist/speech-core.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js.map +1 -1
- package/dist/transcription-core.d.ts.map +1 -1
- package/dist/transcription-core.js.map +1 -1
- package/dist/transport.js.map +1 -1
- package/dist/turn.d.ts +10 -1
- package/dist/turn.d.ts.map +1 -1
- package/dist/turn.js +53 -1
- package/dist/turn.js.map +1 -1
- package/dist/types.d.ts +51 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/usage.d.ts +10 -1
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +40 -2
- package/dist/usage.js.map +1 -1
- package/dist/watchdog.d.ts +6 -0
- package/dist/watchdog.d.ts.map +1 -1
- package/dist/watchdog.js +10 -2
- package/dist/watchdog.js.map +1 -1
- package/package.json +19 -5
- package/src/context.ts +21 -5
- package/src/fallback.ts +21 -6
- package/src/hosted/anthropic.ts +512 -0
- package/src/hosted/gemini.ts +510 -0
- package/src/hosted/openai.ts +137 -0
- package/src/hosted/responses.ts +420 -0
- package/src/hosted/shared.ts +341 -0
- package/src/hosted/types.ts +214 -0
- package/src/hosted.ts +101 -0
- package/src/index.ts +64 -7
- package/src/otel.ts +306 -0
- package/src/providers/anthropic.ts +252 -143
- package/src/providers/factory.ts +127 -82
- package/src/providers/gemini.ts +184 -60
- package/src/providers/hosted-codecs.ts +29 -0
- package/src/providers/openai.ts +52 -15
- package/src/providers/responses.ts +93 -24
- package/src/registry.ts +14 -4
- package/src/turn.ts +55 -1
- package/src/types.ts +62 -1
- package/src/usage.ts +32 -2
- package/src/watchdog.ts +12 -2
- package/dist/jev.d.ts +0 -123
- package/dist/jev.d.ts.map +0 -1
- package/dist/jev.js +0 -278
- package/dist/jev.js.map +0 -1
|
@@ -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 {
|
|
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`
|
|
213
|
-
*
|
|
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:
|
|
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
|
|
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 } =
|
|
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
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
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
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
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
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
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
|
-
|
|
619
|
-
|
|
620
|
-
|
|
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
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
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
|
-
|
|
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
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
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
|
|
786
|
+
return configuredFallbacks(provider, config, codecs);
|
|
678
787
|
}
|