@providerkit/core 0.29.1 → 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.
- package/README.md +58 -9
- 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 +29 -2
- 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/presets.d.ts +36 -5
- package/dist/presets.d.ts.map +1 -1
- package/dist/presets.js +32 -8
- package/dist/presets.js.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/deepgram-transcription.d.ts +24 -0
- package/dist/providers/deepgram-transcription.d.ts.map +1 -0
- package/dist/providers/deepgram-transcription.js +84 -0
- package/dist/providers/deepgram-transcription.js.map +1 -0
- 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-transcription.d.ts +21 -0
- package/dist/providers/openai-transcription.d.ts.map +1 -0
- package/dist/providers/openai-transcription.js +144 -0
- package/dist/providers/openai-transcription.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/providers/transcription-factory.d.ts +30 -0
- package/dist/providers/transcription-factory.d.ts.map +1 -0
- package/dist/providers/transcription-factory.js +38 -0
- package/dist/providers/transcription-factory.js.map +1 -0
- 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 +2 -17
- package/dist/speech-core.d.ts.map +1 -1
- package/dist/speech-core.js +10 -50
- 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 +146 -0
- package/dist/transcription-core.d.ts.map +1 -0
- package/dist/transcription-core.js +193 -0
- package/dist/transcription-core.js.map +1 -0
- package/dist/transcription.d.ts +7 -0
- package/dist/transcription.d.ts.map +1 -0
- package/dist/transcription.js +12 -0
- package/dist/transcription.js.map +1 -0
- package/dist/transport.d.ts +26 -0
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +57 -2
- 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 +14 -5
- package/src/context.ts +21 -5
- package/src/errors.ts +35 -2
- 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/presets.ts +54 -8
- package/src/providers/anthropic.ts +252 -143
- package/src/providers/deepgram-transcription.ts +109 -0
- 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-transcription.ts +177 -0
- package/src/providers/openai.ts +52 -15
- package/src/providers/responses.ts +93 -24
- package/src/providers/transcription-factory.ts +68 -0
- package/src/registry.ts +14 -4
- package/src/speech-core.ts +9 -47
- package/src/transcription-core.ts +345 -0
- package/src/transcription.ts +24 -0
- package/src/transport.ts +77 -3
- package/src/turn.ts +55 -1
- package/src/types.ts +62 -1
- package/src/usage.ts +32 -2
- package/src/watchdog.ts +12 -2
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
// Hosted shapes follow Vercel AI SDK (Apache-2.0) and PydanticAI (MIT).
|
|
2
|
+
// See THIRD_PARTY_NOTICES.
|
|
3
|
+
import { ProviderError } from "../errors.ts";
|
|
4
|
+
import { parseToolArgs } from "../tool-args.ts";
|
|
5
|
+
import {
|
|
6
|
+
hostedCounts,
|
|
7
|
+
hostedReplay,
|
|
8
|
+
HostedRecorder,
|
|
9
|
+
isRecord,
|
|
10
|
+
planHostedTools,
|
|
11
|
+
queryInput,
|
|
12
|
+
servedAt,
|
|
13
|
+
} from "./shared.ts";
|
|
14
|
+
import type {
|
|
15
|
+
Citation,
|
|
16
|
+
HostedCallDelta,
|
|
17
|
+
HostedInput,
|
|
18
|
+
HostedTool,
|
|
19
|
+
HostedToolType,
|
|
20
|
+
} from "./types.ts";
|
|
21
|
+
import type {
|
|
22
|
+
AnthropicConfig,
|
|
23
|
+
AnthropicEvent,
|
|
24
|
+
AnthropicUsage,
|
|
25
|
+
AnthropicMessage,
|
|
26
|
+
} from "../providers/anthropic.ts";
|
|
27
|
+
import type {
|
|
28
|
+
ChatMessage,
|
|
29
|
+
ToolCall,
|
|
30
|
+
ToolDefinition,
|
|
31
|
+
ProviderChunk,
|
|
32
|
+
StreamOptions,
|
|
33
|
+
} from "../types.ts";
|
|
34
|
+
|
|
35
|
+
/** The hosted types this wire has a shape for, and the fields it applies. */
|
|
36
|
+
const ANTHROPIC_FIELDS: Partial<Record<HostedToolType, readonly string[]>> = {
|
|
37
|
+
web_search: ["allowedDomains", "blockedDomains", "maxUses", "userLocation"],
|
|
38
|
+
web_fetch: ["allowedDomains", "blockedDomains", "maxUses", "maxContentTokens"],
|
|
39
|
+
// `fileIds` would ride as container_upload blocks in the prompt, not on the
|
|
40
|
+
// tool; until that is built it is refused rather than dropped.
|
|
41
|
+
code_execution: [],
|
|
42
|
+
};
|
|
43
|
+
const ANTHROPIC_SENDS = Object.keys(ANTHROPIC_FIELDS);
|
|
44
|
+
|
|
45
|
+
/** The names Anthropic's server tools answer to. A function tool under one of
|
|
46
|
+
* them, beside the hosted tool, is a request the API refuses. */
|
|
47
|
+
const RESERVED_NAMES: Partial<Record<HostedToolType, readonly string[]>> = {
|
|
48
|
+
web_search: ["web_search"],
|
|
49
|
+
web_fetch: ["web_fetch"],
|
|
50
|
+
code_execution: ["code_execution", "bash_code_execution", "text_editor_code_execution"],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** A paused turn resumed this many times without finishing is a vendor stuck
|
|
54
|
+
* pausing: PydanticAI's cap (models/_continuation.py:92-106). */
|
|
55
|
+
const MAX_PAUSES = 10;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* One hosted tool on the wire, and the beta header its version needs. Pinned
|
|
59
|
+
* to the versions the model calls directly: the 2026-02-09 and 2026-03-18 web
|
|
60
|
+
* tools route their calls through code execution and come back uncited
|
|
61
|
+
* (PydanticAI anthropic/_citations.py:73-100). Shapes from Vercel's AI SDK
|
|
62
|
+
* (Apache-2.0), anthropic-prepare-tools.ts:167-173,313-330,368-383.
|
|
63
|
+
*/
|
|
64
|
+
function anthropicHostedTool(provider: string, tool: HostedTool): { wire: unknown; beta?: string } {
|
|
65
|
+
switch (tool.type) {
|
|
66
|
+
case "web_search":
|
|
67
|
+
return {
|
|
68
|
+
wire: {
|
|
69
|
+
type: "web_search_20250305",
|
|
70
|
+
name: "web_search",
|
|
71
|
+
max_uses: tool.maxUses,
|
|
72
|
+
allowed_domains: tool.allowedDomains,
|
|
73
|
+
blocked_domains: tool.blockedDomains,
|
|
74
|
+
...(tool.userLocation
|
|
75
|
+
? { user_location: { type: "approximate", ...tool.userLocation } }
|
|
76
|
+
: {}),
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
case "web_fetch":
|
|
80
|
+
return {
|
|
81
|
+
wire: {
|
|
82
|
+
type: "web_fetch_20250910",
|
|
83
|
+
name: "web_fetch",
|
|
84
|
+
max_uses: tool.maxUses,
|
|
85
|
+
allowed_domains: tool.allowedDomains,
|
|
86
|
+
blocked_domains: tool.blockedDomains,
|
|
87
|
+
max_content_tokens: tool.maxContentTokens,
|
|
88
|
+
},
|
|
89
|
+
beta: "web-fetch-2025-09-10",
|
|
90
|
+
};
|
|
91
|
+
case "code_execution":
|
|
92
|
+
return {
|
|
93
|
+
wire: { type: "code_execution_20250825", name: "code_execution" },
|
|
94
|
+
beta: "code-execution-2025-08-25",
|
|
95
|
+
};
|
|
96
|
+
default:
|
|
97
|
+
// planHostedTools only passes the types in ANTHROPIC_FIELDS.
|
|
98
|
+
throw new ProviderError(
|
|
99
|
+
provider,
|
|
100
|
+
"invalid",
|
|
101
|
+
`${provider}: no Anthropic shape for ${tool.type}`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The headers with this request's betas added to any the caller set, as one
|
|
107
|
+
* comma-separated `anthropic-beta`, whatever case the caller wrote it in. */
|
|
108
|
+
function withBetas(
|
|
109
|
+
headers: Record<string, string>,
|
|
110
|
+
betas: readonly string[],
|
|
111
|
+
): Record<string, string> {
|
|
112
|
+
if (betas.length === 0) return headers;
|
|
113
|
+
const out: Record<string, string> = {};
|
|
114
|
+
const merged: string[] = [];
|
|
115
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
116
|
+
if (name.toLowerCase() !== "anthropic-beta") out[name] = value;
|
|
117
|
+
else
|
|
118
|
+
merged.push(
|
|
119
|
+
...value
|
|
120
|
+
.split(",")
|
|
121
|
+
.map((beta) => beta.trim())
|
|
122
|
+
.filter(Boolean),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
for (const beta of betas) if (!merged.includes(beta)) merged.push(beta);
|
|
126
|
+
out["anthropic-beta"] = merged.join(",");
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function str(value: unknown): string | undefined {
|
|
131
|
+
return typeof value === "string" ? value : undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function parseJson(json: string): unknown {
|
|
135
|
+
try {
|
|
136
|
+
return JSON.parse(json);
|
|
137
|
+
} catch {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The seam's type for a server tool name; undefined for the server tools this
|
|
143
|
+
* adapter never asks for (tool search, MCP, the advisor). */
|
|
144
|
+
function serverToolType(name: string): HostedToolType | undefined {
|
|
145
|
+
if (name === "web_search" || name === "web_fetch") return name;
|
|
146
|
+
if (RESERVED_NAMES.code_execution?.includes(name)) return "code_execution";
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** What a server tool was asked to do. Code execution 20250825 runs as two
|
|
151
|
+
* tools, a shell and a file editor, so both read as code runs. */
|
|
152
|
+
function serverToolInput(name: string, input: unknown): HostedInput {
|
|
153
|
+
if (!isRecord(input)) return {};
|
|
154
|
+
switch (name) {
|
|
155
|
+
case "web_search":
|
|
156
|
+
return queryInput([str(input.query)]);
|
|
157
|
+
case "web_fetch": {
|
|
158
|
+
const url = str(input.url);
|
|
159
|
+
return url ? { url } : {};
|
|
160
|
+
}
|
|
161
|
+
case "bash_code_execution": {
|
|
162
|
+
const code = str(input.command);
|
|
163
|
+
return code ? { code, language: "bash" } : {};
|
|
164
|
+
}
|
|
165
|
+
case "text_editor_code_execution": {
|
|
166
|
+
const path = str(input.path);
|
|
167
|
+
const code = str(input.file_text);
|
|
168
|
+
return { ...(path ? { path } : {}), ...(code ? { code } : {}) };
|
|
169
|
+
}
|
|
170
|
+
case "code_execution": {
|
|
171
|
+
const code = str(input.code);
|
|
172
|
+
return code ? { code, language: "python" } : {};
|
|
173
|
+
}
|
|
174
|
+
default:
|
|
175
|
+
return {};
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** A result block: `web_search_tool_result`, `bash_code_execution_tool_result`… */
|
|
180
|
+
function isResultBlock(block: Record<string, unknown>): boolean {
|
|
181
|
+
return typeof block.type === "string" && block.type.endsWith("_tool_result");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** What a server tool's result block says it did. An `*_error` content is a
|
|
185
|
+
* failed call, kept as data with the vendor's code (invariant 7). A shell
|
|
186
|
+
* that exits non-zero still ran: that is the program failing, not the tool. */
|
|
187
|
+
function serverToolResult(block: Record<string, unknown>): Omit<HostedCallDelta, "index"> {
|
|
188
|
+
const { content } = block;
|
|
189
|
+
if (Array.isArray(content)) {
|
|
190
|
+
const sources = content.flatMap((result) => {
|
|
191
|
+
const url = isRecord(result) ? str(result.url) : undefined;
|
|
192
|
+
const title = isRecord(result) ? str(result.title) : undefined;
|
|
193
|
+
return url ? [{ type: "url" as const, url, ...(title ? { title } : {}) }] : [];
|
|
194
|
+
});
|
|
195
|
+
return { status: "completed", output: { sources } };
|
|
196
|
+
}
|
|
197
|
+
if (!isRecord(content)) return { status: "completed" };
|
|
198
|
+
const kind = str(content.type) ?? "";
|
|
199
|
+
if (kind.endsWith("_error")) return { status: "failed", error: str(content.error_code) ?? kind };
|
|
200
|
+
if (kind === "web_fetch_result") {
|
|
201
|
+
const url = str(content.url);
|
|
202
|
+
const title = isRecord(content.content) ? str(content.content.title) : undefined;
|
|
203
|
+
return {
|
|
204
|
+
status: "completed",
|
|
205
|
+
output: url ? { sources: [{ type: "url", url, ...(title ? { title } : {}) }] } : {},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
if (kind === "bash_code_execution_result" || kind === "code_execution_result") {
|
|
209
|
+
const files = (Array.isArray(content.content) ? content.content : []).flatMap((file) => {
|
|
210
|
+
const id = isRecord(file) ? str(file.file_id) : undefined;
|
|
211
|
+
return id ? [{ id }] : [];
|
|
212
|
+
});
|
|
213
|
+
const stdout = str(content.stdout);
|
|
214
|
+
const stderr = str(content.stderr);
|
|
215
|
+
return {
|
|
216
|
+
status: "completed",
|
|
217
|
+
output: {
|
|
218
|
+
...(stdout ? { stdout } : {}),
|
|
219
|
+
...(stderr ? { stderr } : {}),
|
|
220
|
+
...(typeof content.return_code === "number" ? { exitCode: content.return_code } : {}),
|
|
221
|
+
...(files.length > 0 ? { files } : {}),
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const viewed = str(content.content);
|
|
226
|
+
return { status: "completed", ...(viewed ? { output: { stdout: viewed } } : {}) };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A text block's citations as the seam's: one claim per source over the whole
|
|
231
|
+
* block, because Anthropic cites whole blocks (PydanticAI
|
|
232
|
+
* anthropic/_citations.py:197-198). Only web search locations name a page;
|
|
233
|
+
* the document kinds point into the prompt and stay in the vendor's record.
|
|
234
|
+
*/
|
|
235
|
+
function blockCitations(raw: readonly unknown[], start: number, end: number): Citation[] {
|
|
236
|
+
return raw.flatMap((citation): Citation[] => {
|
|
237
|
+
if (!isRecord(citation) || citation.type !== "web_search_result_location") return [];
|
|
238
|
+
const url = str(citation.url);
|
|
239
|
+
if (!url) return [];
|
|
240
|
+
const title = str(citation.title);
|
|
241
|
+
const excerpt = str(citation.cited_text);
|
|
242
|
+
const source = {
|
|
243
|
+
type: "url" as const,
|
|
244
|
+
url,
|
|
245
|
+
...(title ? { title } : {}),
|
|
246
|
+
...(excerpt ? { excerpt } : {}),
|
|
247
|
+
};
|
|
248
|
+
return [{ source, start, end, anchor: "claim" }];
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* The code execution container of the turn still open: everything after the
|
|
254
|
+
* last user message. A request inside that turn has to name the container its
|
|
255
|
+
* earlier responses ran in, or the code it already ran is gone (PydanticAI
|
|
256
|
+
* anthropic/_native_tools.py:128-147).
|
|
257
|
+
*/
|
|
258
|
+
function openTurnContainer(messages: readonly ChatMessage[], provider: string): string | undefined {
|
|
259
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
260
|
+
const message = messages[i]!;
|
|
261
|
+
if (message.role === "user") return undefined;
|
|
262
|
+
if (message.role === "assistant" && message.hostedItems?.provider === provider) {
|
|
263
|
+
if (message.hostedItems.container) return message.hostedItems.container;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** The paused blocks as the last assistant message, which is how Anthropic is
|
|
270
|
+
* told to carry on with the turn it paused. */
|
|
271
|
+
function resumeWith(messages: AnthropicMessage[], blocks: readonly unknown[]): AnthropicMessage[] {
|
|
272
|
+
const last = messages.at(-1);
|
|
273
|
+
return last?.role === "assistant"
|
|
274
|
+
? [...messages.slice(0, -1), { role: "assistant", content: [...last.content, ...blocks] }]
|
|
275
|
+
: [...messages, { role: "assistant", content: [...blocks] }];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** The prompt of a turn's last model pass, cache included, when the vendor
|
|
279
|
+
* lists its passes. A server tool loop bills the sum of its passes, which is
|
|
280
|
+
* more than the window ever held. Ported from PydanticAI (MIT),
|
|
281
|
+
* anthropic/_response.py:180-196. */
|
|
282
|
+
function lastPassTokens(usage: AnthropicUsage | undefined): number | undefined {
|
|
283
|
+
const passes = usage?.iterations ?? [];
|
|
284
|
+
for (let i = passes.length - 1; i >= 0; i--) {
|
|
285
|
+
const pass = passes[i]!;
|
|
286
|
+
if (pass.type === "message" || pass.type === "fallback_message") {
|
|
287
|
+
return (
|
|
288
|
+
(pass.input_tokens ?? 0) +
|
|
289
|
+
(pass.cache_read_input_tokens ?? 0) +
|
|
290
|
+
(pass.cache_creation_input_tokens ?? 0)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** The counts Anthropic states, by the seam's types. */
|
|
298
|
+
const STATED_COUNTS = [
|
|
299
|
+
["web_search_requests", "web_search"],
|
|
300
|
+
["web_fetch_requests", "web_fetch"],
|
|
301
|
+
["code_execution_requests", "code_execution"],
|
|
302
|
+
] as const;
|
|
303
|
+
export const anthropicHosted = {
|
|
304
|
+
plan(
|
|
305
|
+
config: AnthropicConfig,
|
|
306
|
+
baseUrl: string,
|
|
307
|
+
id: string,
|
|
308
|
+
offered: ToolDefinition[],
|
|
309
|
+
opts: StreamOptions,
|
|
310
|
+
) {
|
|
311
|
+
const hosted = planHostedTools(
|
|
312
|
+
id,
|
|
313
|
+
{
|
|
314
|
+
name: "Anthropic",
|
|
315
|
+
serves: (
|
|
316
|
+
config.servesHostedTools ??
|
|
317
|
+
servedAt(baseUrl, "api.anthropic.com", ["web_search", "web_fetch", "code_execution"])
|
|
318
|
+
).filter((type) => ANTHROPIC_SENDS.includes(type)),
|
|
319
|
+
fields: ANTHROPIC_FIELDS,
|
|
320
|
+
refuse(tool) {
|
|
321
|
+
// TanStack's rule (MIT, ai-anthropic tools/web-search-tool.ts:12-18).
|
|
322
|
+
if (
|
|
323
|
+
(tool.type === "web_search" || tool.type === "web_fetch") &&
|
|
324
|
+
tool.allowedDomains !== undefined &&
|
|
325
|
+
tool.blockedDomains !== undefined
|
|
326
|
+
) {
|
|
327
|
+
return "Anthropic takes allowedDomains or blockedDomains, not both. Set one, or mark the tool optional.";
|
|
328
|
+
}
|
|
329
|
+
const taken = offered.find((fn) => RESERVED_NAMES[tool.type]?.includes(fn.name));
|
|
330
|
+
return taken
|
|
331
|
+
? `a function tool is named ${taken.name}, which Anthropic keeps for this hosted tool. Rename the function tool.`
|
|
332
|
+
: undefined;
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
opts,
|
|
336
|
+
);
|
|
337
|
+
const hostedWire = hosted.map((tool) => anthropicHostedTool(id, tool));
|
|
338
|
+
return {
|
|
339
|
+
tools: hostedWire.map(({ wire }) => wire),
|
|
340
|
+
betas: hostedWire.flatMap(({ beta }) => (beta ? [beta] : [])),
|
|
341
|
+
};
|
|
342
|
+
},
|
|
343
|
+
headers: withBetas,
|
|
344
|
+
replay(
|
|
345
|
+
message: Extract<ChatMessage, { role: "assistant" }>,
|
|
346
|
+
provider: string,
|
|
347
|
+
toolUse: (call: ToolCall) => unknown,
|
|
348
|
+
): unknown[] | undefined {
|
|
349
|
+
return hostedReplay(message, provider)?.map((entry) => {
|
|
350
|
+
if ("call" in entry) return toolUse(entry.call);
|
|
351
|
+
if ("item" in entry) return entry.item;
|
|
352
|
+
const { citations } = entry.slot;
|
|
353
|
+
return { type: "text", text: entry.text, ...(citations ? { citations } : {}) };
|
|
354
|
+
});
|
|
355
|
+
},
|
|
356
|
+
stream(sent: readonly ChatMessage[], id: string) {
|
|
357
|
+
const statedCounts: Partial<Record<HostedToolType, number>> = {};
|
|
358
|
+
let contextTokens: number | undefined;
|
|
359
|
+
// The answer's length so far, in JS string units: where a hosted call
|
|
360
|
+
// sits in it, and the span a text block's citations cover.
|
|
361
|
+
let length = 0;
|
|
362
|
+
const recorder = new HostedRecorder();
|
|
363
|
+
// The vendor's record of the turn, for `hostedItems`: its server blocks
|
|
364
|
+
// as they came, its text as `{ length }`, its function calls as `{ call }`.
|
|
365
|
+
const items: unknown[] = [];
|
|
366
|
+
let recordedCalls = 0;
|
|
367
|
+
let container = openTurnContainer(sent, id);
|
|
368
|
+
// Every block of the passes so far, thinking and signatures included, to
|
|
369
|
+
// send back when the turn pauses. Kept for this call only: the signatures
|
|
370
|
+
// are what lets the model pick its own turn back up.
|
|
371
|
+
const resumed: unknown[] = [];
|
|
372
|
+
let open:
|
|
373
|
+
| { block: Record<string, unknown>; json: string; start: number; citations: unknown[] }
|
|
374
|
+
| undefined;
|
|
375
|
+
let blocks: unknown[] = [];
|
|
376
|
+
let servers: AnthropicUsage["server_tool_use"];
|
|
377
|
+
// The end of the turn, before its finish: a hosted call that never got a
|
|
378
|
+
// result fails, and leaves the record, because Anthropic pairs every
|
|
379
|
+
// server block with its result and a lone one is a 400 on the next send.
|
|
380
|
+
function* closeTurn(): Generator<ProviderChunk> {
|
|
381
|
+
const unsettled = recorder.unsettled();
|
|
382
|
+
const failed = recorder.settle();
|
|
383
|
+
if (failed.length > 0) yield { type: "delta", hosted: failed };
|
|
384
|
+
const kept = items.filter(
|
|
385
|
+
(item) =>
|
|
386
|
+
!(isRecord(item) && item.type === "server_tool_use" && unsettled.has(str(item.id) ?? "")),
|
|
387
|
+
);
|
|
388
|
+
if (kept.some((item) => isRecord(item) && "type" in item)) {
|
|
389
|
+
yield {
|
|
390
|
+
type: "delta",
|
|
391
|
+
hostedItems: { provider: id, items: kept, ...(container ? { container } : {}) },
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
payload(request: Record<string, unknown>, body: AnthropicMessage[]) {
|
|
397
|
+
blocks = [];
|
|
398
|
+
open = undefined;
|
|
399
|
+
servers = undefined;
|
|
400
|
+
return {
|
|
401
|
+
...request,
|
|
402
|
+
...(resumed.length > 0 ? { messages: resumeWith(body, resumed) } : {}),
|
|
403
|
+
...(container ? { container } : {}),
|
|
404
|
+
};
|
|
405
|
+
},
|
|
406
|
+
*event(event: AnthropicEvent): Generator<ProviderChunk> {
|
|
407
|
+
const usage = event.type === "message_start" ? event.message?.usage : event.usage;
|
|
408
|
+
servers = usage?.server_tool_use ?? servers;
|
|
409
|
+
contextTokens = lastPassTokens(usage) ?? contextTokens;
|
|
410
|
+
container = event.message?.container?.id ?? event.delta?.container?.id ?? container;
|
|
411
|
+
switch (event.type) {
|
|
412
|
+
case "content_block_start": {
|
|
413
|
+
const block: Record<string, unknown> = { ...event.content_block };
|
|
414
|
+
open = { block, json: "", start: length, citations: [] };
|
|
415
|
+
if (block.type === "server_tool_use") {
|
|
416
|
+
const name = str(block.name) ?? "";
|
|
417
|
+
const type = serverToolType(name);
|
|
418
|
+
if (type) {
|
|
419
|
+
// Whole here on some versions, in deltas of its own on others
|
|
420
|
+
// (Vercel anthropic-language-model.ts:2635-2643).
|
|
421
|
+
const whole = isRecord(block.input) && Object.keys(block.input).length > 0;
|
|
422
|
+
const input = whole ? serverToolInput(name, block.input) : undefined;
|
|
423
|
+
yield {
|
|
424
|
+
type: "delta",
|
|
425
|
+
hosted: [recorder.start(str(block.id), type, length, input)],
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
} else if (isResultBlock(block)) {
|
|
429
|
+
// A result arrives whole, in the block that opens it.
|
|
430
|
+
const done = recorder.end(str(block.tool_use_id), serverToolResult(block));
|
|
431
|
+
if (done) yield { type: "delta", hosted: [done] };
|
|
432
|
+
}
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
case "content_block_delta": {
|
|
436
|
+
const delta = event.delta;
|
|
437
|
+
if (delta?.type === "text_delta" && delta.text) {
|
|
438
|
+
length += delta.text.length;
|
|
439
|
+
if (open) open.block.text = (str(open.block.text) ?? "") + delta.text;
|
|
440
|
+
} else if (delta?.type === "thinking_delta" && delta.thinking) {
|
|
441
|
+
if (open) open.block.thinking = (str(open.block.thinking) ?? "") + delta.thinking;
|
|
442
|
+
} else if (delta?.type === "signature_delta" && open) {
|
|
443
|
+
open.block.signature = delta.signature;
|
|
444
|
+
} else if (delta?.type === "citations_delta" && open) {
|
|
445
|
+
open.citations.push(delta.citation);
|
|
446
|
+
} else if (delta?.type === "input_json_delta" && open) {
|
|
447
|
+
open.json += delta.partial_json ?? "";
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
case "content_block_stop": {
|
|
452
|
+
if (!open) break;
|
|
453
|
+
const { block, json, start, citations } = open;
|
|
454
|
+
open = undefined;
|
|
455
|
+
// An empty text block holds nothing, and sent back it is a 400.
|
|
456
|
+
const size = block.type === "text" ? (str(block.text)?.length ?? 0) : undefined;
|
|
457
|
+
if (size !== undefined) {
|
|
458
|
+
if (citations.length > 0) {
|
|
459
|
+
block.citations = citations;
|
|
460
|
+
const cited = blockCitations(citations, start, length);
|
|
461
|
+
if (cited.length > 0) yield { type: "delta", citations: cited };
|
|
462
|
+
}
|
|
463
|
+
if (size > 0) {
|
|
464
|
+
items.push({ length: size, ...(citations.length > 0 ? { citations } : {}) });
|
|
465
|
+
}
|
|
466
|
+
} else if (block.type === "tool_use") {
|
|
467
|
+
if (json) block.input = parseToolArgs(json);
|
|
468
|
+
items.push({ call: recordedCalls++ });
|
|
469
|
+
} else if (block.type === "server_tool_use") {
|
|
470
|
+
// Kept exactly as the vendor wrote it: this goes back to it.
|
|
471
|
+
if (json) block.input = parseJson(json) ?? block.input;
|
|
472
|
+
const index = recorder.indexOf(str(block.id));
|
|
473
|
+
if (index !== undefined && json) {
|
|
474
|
+
const input = serverToolInput(str(block.name) ?? "", block.input);
|
|
475
|
+
yield { type: "delta", hosted: [{ index, input }] };
|
|
476
|
+
}
|
|
477
|
+
items.push(block);
|
|
478
|
+
} else if (isResultBlock(block)) {
|
|
479
|
+
items.push(block);
|
|
480
|
+
}
|
|
481
|
+
if (size !== 0) blocks.push(block);
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
},
|
|
486
|
+
close: closeTurn,
|
|
487
|
+
endPass(paused: boolean, pauses: number) {
|
|
488
|
+
for (const [field, type] of STATED_COUNTS) {
|
|
489
|
+
const count = servers?.[field];
|
|
490
|
+
if (count !== undefined) statedCounts[type] = (statedCounts[type] ?? 0) + count;
|
|
491
|
+
}
|
|
492
|
+
if (!paused) return;
|
|
493
|
+
if (pauses >= MAX_PAUSES) {
|
|
494
|
+
throw new ProviderError(
|
|
495
|
+
id,
|
|
496
|
+
"overload",
|
|
497
|
+
`${id}: the turn paused ${pauses + 1} times without finishing`,
|
|
498
|
+
{ code: "pause_turn" },
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
resumed.push(...blocks);
|
|
502
|
+
},
|
|
503
|
+
usage() {
|
|
504
|
+
const hostedCalls = hostedCounts(recorder.shown(), statedCounts);
|
|
505
|
+
return {
|
|
506
|
+
...(hostedCalls ? { hostedCalls } : {}),
|
|
507
|
+
...(contextTokens !== undefined ? { contextTokens } : {}),
|
|
508
|
+
};
|
|
509
|
+
},
|
|
510
|
+
};
|
|
511
|
+
},
|
|
512
|
+
};
|