@providerprotocol/ai 0.0.20 → 0.0.22
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/dist/anthropic/index.d.ts +184 -14
- package/dist/anthropic/index.js +306 -107
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-P5IRTEM5.js → chunk-7WYBJPJJ.js} +2 -2
- package/dist/chunk-I2VHCGQE.js +49 -0
- package/dist/chunk-I2VHCGQE.js.map +1 -0
- package/dist/{chunk-UMKWXGO3.js → chunk-M4BMM5IB.js} +86 -2
- package/dist/chunk-M4BMM5IB.js.map +1 -0
- package/dist/{chunk-SKY2JLA7.js → chunk-MKDLXV4O.js} +1 -1
- package/dist/chunk-MKDLXV4O.js.map +1 -0
- package/dist/{chunk-Z7RBRCRN.js → chunk-NWS5IKNR.js} +37 -11
- package/dist/chunk-NWS5IKNR.js.map +1 -0
- package/dist/{chunk-U3FZWV4U.js → chunk-RFWLEFAB.js} +100 -43
- package/dist/chunk-RFWLEFAB.js.map +1 -0
- package/dist/{chunk-U4JJC2YX.js → chunk-RS7C25LS.js} +36 -11
- package/dist/chunk-RS7C25LS.js.map +1 -0
- package/dist/google/index.d.ts +35 -24
- package/dist/google/index.js +273 -99
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +4 -4
- package/dist/index.d.ts +103 -38
- package/dist/index.js +346 -153
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +14 -16
- package/dist/ollama/index.js +68 -16
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +25 -133
- package/dist/openai/index.js +208 -122
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +28 -53
- package/dist/openrouter/index.js +179 -72
- package/dist/openrouter/index.js.map +1 -1
- package/dist/provider-DWEAzeM5.d.ts +1329 -0
- package/dist/proxy/index.d.ts +2 -3
- package/dist/proxy/index.js +174 -17
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-DR7YRJDz.d.ts → retry-DmPmqZL6.d.ts} +12 -3
- package/dist/{stream-DRHy6q1a.d.ts → stream-DbkLOIbJ.d.ts} +15 -5
- package/dist/xai/index.d.ts +16 -88
- package/dist/xai/index.js +167 -86
- package/dist/xai/index.js.map +1 -1
- package/package.json +4 -1
- package/dist/chunk-MSR5P65T.js +0 -39
- package/dist/chunk-MSR5P65T.js.map +0 -1
- package/dist/chunk-SKY2JLA7.js.map +0 -1
- package/dist/chunk-U3FZWV4U.js.map +0 -1
- package/dist/chunk-U4JJC2YX.js.map +0 -1
- package/dist/chunk-UMKWXGO3.js.map +0 -1
- package/dist/chunk-Z7RBRCRN.js.map +0 -1
- package/dist/content-DEl3z_W2.d.ts +0 -276
- package/dist/image-Dhq-Yuq4.d.ts +0 -456
- package/dist/provider-BBMBZuGn.d.ts +0 -570
- /package/dist/{chunk-P5IRTEM5.js.map → chunk-7WYBJPJJ.js.map} +0 -0
package/dist/anthropic/index.js
CHANGED
|
@@ -1,36 +1,164 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-
|
|
2
|
+
parseJsonResponse
|
|
3
|
+
} from "../chunk-I2VHCGQE.js";
|
|
4
4
|
import {
|
|
5
5
|
AssistantMessage,
|
|
6
|
+
createProvider,
|
|
7
|
+
generateId,
|
|
6
8
|
isAssistantMessage,
|
|
7
9
|
isToolResultMessage,
|
|
8
10
|
isUserMessage
|
|
9
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-M4BMM5IB.js";
|
|
10
12
|
import {
|
|
11
13
|
parseSSEStream
|
|
12
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-NWS5IKNR.js";
|
|
13
15
|
import {
|
|
14
16
|
resolveApiKey
|
|
15
|
-
} from "../chunk-
|
|
17
|
+
} from "../chunk-7WYBJPJJ.js";
|
|
16
18
|
import {
|
|
17
19
|
UPPError,
|
|
18
20
|
doFetch,
|
|
19
21
|
doStreamFetch,
|
|
20
|
-
normalizeHttpError
|
|
21
|
-
|
|
22
|
+
normalizeHttpError,
|
|
23
|
+
toError
|
|
24
|
+
} from "../chunk-RFWLEFAB.js";
|
|
25
|
+
|
|
26
|
+
// src/providers/anthropic/types.ts
|
|
27
|
+
var betas = {
|
|
28
|
+
// Structured Outputs
|
|
29
|
+
/** Guaranteed JSON schema conformance for responses. Available for Claude Sonnet 4.5+. */
|
|
30
|
+
structuredOutputs: "structured-outputs-2025-11-13",
|
|
31
|
+
// Extended Thinking / Reasoning
|
|
32
|
+
/** Enables Claude to think between tool calls in Claude 4 models. */
|
|
33
|
+
interleavedThinking: "interleaved-thinking-2025-05-14",
|
|
34
|
+
/** Developer mode for full thinking output visibility. */
|
|
35
|
+
devFullThinking: "dev-full-thinking-2025-05-14",
|
|
36
|
+
/** Effort parameter for Claude Opus 4.5 - controls response thoroughness vs efficiency. */
|
|
37
|
+
effort: "effort-2025-11-24",
|
|
38
|
+
// Computer Use
|
|
39
|
+
/** Legacy computer use tool (Claude 3.x models). */
|
|
40
|
+
computerUseLegacy: "computer-use-2024-10-22",
|
|
41
|
+
/** Computer use tool for Claude 4 models (mouse, keyboard, screenshots). */
|
|
42
|
+
computerUse: "computer-use-2025-01-24",
|
|
43
|
+
/** Computer use tool for Claude Opus 4.5 with additional commands. */
|
|
44
|
+
computerUseOpus: "computer-use-2025-11-24",
|
|
45
|
+
// Extended Output / Context
|
|
46
|
+
/** Enables up to 8,192 output tokens from Claude Sonnet 3.5. */
|
|
47
|
+
maxTokens35Sonnet: "max-tokens-3-5-sonnet-2024-07-15",
|
|
48
|
+
/** Enables 128K token output length. */
|
|
49
|
+
output128k: "output-128k-2025-02-19",
|
|
50
|
+
/** Enables 1 million token context window for Claude Sonnet 4. */
|
|
51
|
+
context1m: "context-1m-2025-08-07",
|
|
52
|
+
// Token Efficiency
|
|
53
|
+
/** Reduces output token consumption by up to 70% for tool calls. */
|
|
54
|
+
tokenEfficientTools: "token-efficient-tools-2025-02-19",
|
|
55
|
+
/** Streams tool use parameters without buffering/JSON validation. */
|
|
56
|
+
fineGrainedToolStreaming: "fine-grained-tool-streaming-2025-05-14",
|
|
57
|
+
// Code Execution
|
|
58
|
+
/** Code execution tool for running Python/Bash in secure sandbox. */
|
|
59
|
+
codeExecution: "code-execution-2025-08-25",
|
|
60
|
+
// Advanced Tool Use
|
|
61
|
+
/** Advanced tool use: Tool Search, Programmatic Tool Calling, Tool Use Examples. */
|
|
62
|
+
advancedToolUse: "advanced-tool-use-2025-11-20",
|
|
63
|
+
// Files & Documents
|
|
64
|
+
/** Files API for uploading and managing files. */
|
|
65
|
+
filesApi: "files-api-2025-04-14",
|
|
66
|
+
/** PDF document support. */
|
|
67
|
+
pdfs: "pdfs-2024-09-25",
|
|
68
|
+
// MCP (Model Context Protocol)
|
|
69
|
+
/** MCP connector to connect to remote MCP servers. */
|
|
70
|
+
mcpClient: "mcp-client-2025-04-04",
|
|
71
|
+
/** Updated MCP client. */
|
|
72
|
+
mcpClientLatest: "mcp-client-2025-11-20",
|
|
73
|
+
// Caching
|
|
74
|
+
/** Prompt caching for reduced latency and costs. Now works automatically with cache_control. */
|
|
75
|
+
promptCaching: "prompt-caching-2024-07-31",
|
|
76
|
+
/** Enables 1-hour cache TTL (vs default 5-minute). */
|
|
77
|
+
extendedCacheTtl: "extended-cache-ttl-2025-04-11",
|
|
78
|
+
// Context Management
|
|
79
|
+
/** Automatic tool call clearing for context management. */
|
|
80
|
+
contextManagement: "context-management-2025-06-27",
|
|
81
|
+
/** Handling for when model context window is exceeded. */
|
|
82
|
+
modelContextWindowExceeded: "model-context-window-exceeded-2025-08-26",
|
|
83
|
+
// Message Batches (generally available but may still need header)
|
|
84
|
+
/** Message Batches API for async processing at 50% cost. */
|
|
85
|
+
messageBatches: "message-batches-2024-09-24",
|
|
86
|
+
// Token Counting (generally available)
|
|
87
|
+
/** Token counting endpoint. */
|
|
88
|
+
tokenCounting: "token-counting-2024-11-01",
|
|
89
|
+
// Skills
|
|
90
|
+
/** Agent Skills for specialized tasks (PowerPoint, Excel, Word, PDF). */
|
|
91
|
+
skills: "skills-2025-10-02"
|
|
92
|
+
};
|
|
93
|
+
function webSearchTool(options) {
|
|
94
|
+
return {
|
|
95
|
+
type: "web_search_20250305",
|
|
96
|
+
name: "web_search",
|
|
97
|
+
...options
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function computerTool(options) {
|
|
101
|
+
const { version = "20250124", ...rest } = options;
|
|
102
|
+
return {
|
|
103
|
+
type: version === "20251124" ? "computer_20251124" : "computer_20250124",
|
|
104
|
+
name: "computer",
|
|
105
|
+
...rest
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function textEditorTool(options) {
|
|
109
|
+
const version = options?.version ?? "20250728";
|
|
110
|
+
return {
|
|
111
|
+
type: version === "20250728" ? "text_editor_20250728" : "text_editor_20250124",
|
|
112
|
+
name: version === "20250728" ? "str_replace_based_edit_tool" : "str_replace_editor",
|
|
113
|
+
...options?.max_characters !== void 0 && { max_characters: options.max_characters }
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function bashTool() {
|
|
117
|
+
return {
|
|
118
|
+
type: "bash_20250124",
|
|
119
|
+
name: "bash"
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function codeExecutionTool() {
|
|
123
|
+
return {
|
|
124
|
+
type: "code_execution_20250825",
|
|
125
|
+
name: "code_execution"
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function toolSearchTool(options) {
|
|
129
|
+
const mode = options?.mode ?? "regex";
|
|
130
|
+
return {
|
|
131
|
+
type: mode === "regex" ? "tool_search_tool_regex_20251119" : "tool_search_tool_bm25_20251119",
|
|
132
|
+
name: mode === "regex" ? "tool_search_tool_regex" : "tool_search_tool_bm25"
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
var tools = {
|
|
136
|
+
/** Creates a web search tool configuration */
|
|
137
|
+
webSearch: webSearchTool,
|
|
138
|
+
/** Creates a computer use tool configuration */
|
|
139
|
+
computer: computerTool,
|
|
140
|
+
/** Creates a text editor tool configuration */
|
|
141
|
+
textEditor: textEditorTool,
|
|
142
|
+
/** Creates a bash tool configuration */
|
|
143
|
+
bash: bashTool,
|
|
144
|
+
/** Creates a code execution tool configuration */
|
|
145
|
+
codeExecution: codeExecutionTool,
|
|
146
|
+
/** Creates a tool search tool configuration */
|
|
147
|
+
toolSearch: toolSearchTool
|
|
148
|
+
};
|
|
22
149
|
|
|
23
150
|
// src/providers/anthropic/transform.ts
|
|
24
|
-
function transformRequest(request, modelId) {
|
|
151
|
+
function transformRequest(request, modelId, useNativeStructuredOutput = false) {
|
|
25
152
|
const params = request.params ?? {};
|
|
26
|
-
const { builtInTools, ...restParams } = params;
|
|
153
|
+
const { tools: builtInTools, ...restParams } = params;
|
|
27
154
|
const anthropicRequest = {
|
|
28
155
|
...restParams,
|
|
29
156
|
model: modelId,
|
|
30
157
|
messages: request.messages.map(transformMessage)
|
|
31
158
|
};
|
|
32
|
-
|
|
33
|
-
|
|
159
|
+
const normalizedSystem = normalizeSystem(request.system);
|
|
160
|
+
if (normalizedSystem !== void 0) {
|
|
161
|
+
anthropicRequest.system = normalizedSystem;
|
|
34
162
|
}
|
|
35
163
|
const allTools = [];
|
|
36
164
|
if (request.tools && request.tools.length > 0) {
|
|
@@ -44,20 +172,85 @@ function transformRequest(request, modelId) {
|
|
|
44
172
|
anthropicRequest.tool_choice = { type: "auto" };
|
|
45
173
|
}
|
|
46
174
|
if (request.structure) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
175
|
+
if (useNativeStructuredOutput) {
|
|
176
|
+
const outputFormat = {
|
|
177
|
+
type: "json_schema",
|
|
178
|
+
schema: {
|
|
179
|
+
type: "object",
|
|
180
|
+
properties: request.structure.properties,
|
|
181
|
+
required: request.structure.required,
|
|
182
|
+
additionalProperties: false
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
anthropicRequest.output_format = outputFormat;
|
|
186
|
+
} else {
|
|
187
|
+
const structuredTool = {
|
|
188
|
+
name: "json_response",
|
|
189
|
+
description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
|
|
190
|
+
input_schema: {
|
|
191
|
+
type: "object",
|
|
192
|
+
properties: request.structure.properties,
|
|
193
|
+
required: request.structure.required,
|
|
194
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : {}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
|
|
198
|
+
anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
|
|
199
|
+
}
|
|
58
200
|
}
|
|
59
201
|
return anthropicRequest;
|
|
60
202
|
}
|
|
203
|
+
function normalizeSystem(system) {
|
|
204
|
+
if (system === void 0 || system === null) return void 0;
|
|
205
|
+
if (typeof system === "string") return system;
|
|
206
|
+
if (!Array.isArray(system)) {
|
|
207
|
+
throw new UPPError(
|
|
208
|
+
"System prompt must be a string or an array of text blocks",
|
|
209
|
+
"INVALID_REQUEST",
|
|
210
|
+
"anthropic",
|
|
211
|
+
"llm"
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
const blocks = [];
|
|
215
|
+
for (const block of system) {
|
|
216
|
+
if (!block || typeof block !== "object") {
|
|
217
|
+
throw new UPPError(
|
|
218
|
+
'System prompt array must contain objects with type "text"',
|
|
219
|
+
"INVALID_REQUEST",
|
|
220
|
+
"anthropic",
|
|
221
|
+
"llm"
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const candidate = block;
|
|
225
|
+
if (candidate.type !== "text" || typeof candidate.text !== "string") {
|
|
226
|
+
throw new UPPError(
|
|
227
|
+
'Anthropic system blocks must be of type "text" with a string text field',
|
|
228
|
+
"INVALID_REQUEST",
|
|
229
|
+
"anthropic",
|
|
230
|
+
"llm"
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
if (candidate.cache_control !== void 0 && !isValidCacheControl(candidate.cache_control)) {
|
|
234
|
+
throw new UPPError(
|
|
235
|
+
"Invalid cache_control for Anthropic system prompt",
|
|
236
|
+
"INVALID_REQUEST",
|
|
237
|
+
"anthropic",
|
|
238
|
+
"llm"
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
blocks.push(block);
|
|
242
|
+
}
|
|
243
|
+
return blocks.length > 0 ? blocks : void 0;
|
|
244
|
+
}
|
|
245
|
+
function isValidCacheControl(value) {
|
|
246
|
+
if (!value || typeof value !== "object") return false;
|
|
247
|
+
const candidate = value;
|
|
248
|
+
if (candidate.type !== "ephemeral") return false;
|
|
249
|
+
if (candidate.ttl !== void 0 && candidate.ttl !== "5m" && candidate.ttl !== "1h") {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
61
254
|
function filterValidContent(content) {
|
|
62
255
|
return content.filter((c) => c && typeof c.type === "string");
|
|
63
256
|
}
|
|
@@ -177,18 +370,25 @@ function transformTool(tool) {
|
|
|
177
370
|
input_schema: {
|
|
178
371
|
type: "object",
|
|
179
372
|
properties: tool.parameters.properties,
|
|
180
|
-
required: tool.parameters.required
|
|
373
|
+
required: tool.parameters.required,
|
|
374
|
+
...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
|
|
181
375
|
},
|
|
182
376
|
...cacheControl ? { cache_control: cacheControl } : {}
|
|
183
377
|
};
|
|
184
378
|
}
|
|
185
|
-
function transformResponse(data) {
|
|
379
|
+
function transformResponse(data, useNativeStructuredOutput = false) {
|
|
186
380
|
const textContent = [];
|
|
187
381
|
const toolCalls = [];
|
|
188
382
|
let structuredData;
|
|
189
383
|
for (const block of data.content) {
|
|
190
384
|
if (block.type === "text") {
|
|
191
385
|
textContent.push({ type: "text", text: block.text });
|
|
386
|
+
if (useNativeStructuredOutput && structuredData === void 0) {
|
|
387
|
+
try {
|
|
388
|
+
structuredData = JSON.parse(block.text);
|
|
389
|
+
} catch {
|
|
390
|
+
}
|
|
391
|
+
}
|
|
192
392
|
} else if (block.type === "tool_use") {
|
|
193
393
|
if (block.name === "json_response") {
|
|
194
394
|
structuredData = block.input;
|
|
@@ -255,6 +455,7 @@ function createStreamState() {
|
|
|
255
455
|
};
|
|
256
456
|
}
|
|
257
457
|
function transformStreamEvent(event, state) {
|
|
458
|
+
const events = [];
|
|
258
459
|
switch (event.type) {
|
|
259
460
|
case "message_start":
|
|
260
461
|
state.messageId = event.message.id;
|
|
@@ -262,7 +463,8 @@ function transformStreamEvent(event, state) {
|
|
|
262
463
|
state.inputTokens = event.message.usage.input_tokens;
|
|
263
464
|
state.cacheReadTokens = event.message.usage.cache_read_input_tokens ?? 0;
|
|
264
465
|
state.cacheWriteTokens = event.message.usage.cache_creation_input_tokens ?? 0;
|
|
265
|
-
|
|
466
|
+
events.push({ type: "message_start", index: 0, delta: {} });
|
|
467
|
+
break;
|
|
266
468
|
case "content_block_start":
|
|
267
469
|
if (event.content_block.type === "text") {
|
|
268
470
|
state.content[event.index] = { type: "text", text: "" };
|
|
@@ -295,24 +497,26 @@ function transformStreamEvent(event, state) {
|
|
|
295
497
|
fileContent: resultBlock.content?.content ?? ""
|
|
296
498
|
};
|
|
297
499
|
}
|
|
298
|
-
|
|
500
|
+
events.push({ type: "content_block_start", index: event.index, delta: {} });
|
|
501
|
+
break;
|
|
299
502
|
case "content_block_delta": {
|
|
300
503
|
const delta = event.delta;
|
|
301
504
|
if (delta.type === "text_delta") {
|
|
302
505
|
if (state.content[event.index]) {
|
|
303
506
|
state.content[event.index].text = (state.content[event.index].text ?? "") + delta.text;
|
|
304
507
|
}
|
|
305
|
-
|
|
508
|
+
events.push({
|
|
306
509
|
type: "text_delta",
|
|
307
510
|
index: event.index,
|
|
308
511
|
delta: { text: delta.text }
|
|
309
|
-
};
|
|
512
|
+
});
|
|
513
|
+
break;
|
|
310
514
|
}
|
|
311
515
|
if (delta.type === "input_json_delta") {
|
|
312
516
|
if (state.content[event.index]) {
|
|
313
517
|
state.content[event.index].input = (state.content[event.index].input ?? "") + delta.partial_json;
|
|
314
518
|
}
|
|
315
|
-
|
|
519
|
+
events.push({
|
|
316
520
|
type: "tool_call_delta",
|
|
317
521
|
index: event.index,
|
|
318
522
|
delta: {
|
|
@@ -320,33 +524,38 @@ function transformStreamEvent(event, state) {
|
|
|
320
524
|
toolCallId: state.content[event.index]?.id,
|
|
321
525
|
toolName: state.content[event.index]?.name
|
|
322
526
|
}
|
|
323
|
-
};
|
|
527
|
+
});
|
|
528
|
+
break;
|
|
324
529
|
}
|
|
325
530
|
if (delta.type === "thinking_delta") {
|
|
326
|
-
|
|
531
|
+
events.push({
|
|
327
532
|
type: "reasoning_delta",
|
|
328
533
|
index: event.index,
|
|
329
534
|
delta: { text: delta.thinking }
|
|
330
|
-
};
|
|
535
|
+
});
|
|
536
|
+
break;
|
|
331
537
|
}
|
|
332
|
-
|
|
538
|
+
break;
|
|
333
539
|
}
|
|
334
540
|
case "content_block_stop":
|
|
335
|
-
|
|
541
|
+
events.push({ type: "content_block_stop", index: event.index, delta: {} });
|
|
542
|
+
break;
|
|
336
543
|
case "message_delta":
|
|
337
544
|
state.stopReason = event.delta.stop_reason;
|
|
338
545
|
state.outputTokens = event.usage.output_tokens;
|
|
339
|
-
return
|
|
546
|
+
return [];
|
|
340
547
|
case "message_stop":
|
|
341
|
-
|
|
548
|
+
events.push({ type: "message_stop", index: 0, delta: {} });
|
|
549
|
+
break;
|
|
342
550
|
case "ping":
|
|
343
551
|
case "error":
|
|
344
|
-
return
|
|
552
|
+
return [];
|
|
345
553
|
default:
|
|
346
|
-
|
|
554
|
+
break;
|
|
347
555
|
}
|
|
556
|
+
return events;
|
|
348
557
|
}
|
|
349
|
-
function buildResponseFromState(state) {
|
|
558
|
+
function buildResponseFromState(state, useNativeStructuredOutput = false) {
|
|
350
559
|
const textContent = [];
|
|
351
560
|
const toolCalls = [];
|
|
352
561
|
let structuredData;
|
|
@@ -354,6 +563,12 @@ function buildResponseFromState(state) {
|
|
|
354
563
|
if (!block) continue;
|
|
355
564
|
if (block.type === "text" && block.text) {
|
|
356
565
|
textContent.push({ type: "text", text: block.text });
|
|
566
|
+
if (useNativeStructuredOutput && structuredData === void 0) {
|
|
567
|
+
try {
|
|
568
|
+
structuredData = JSON.parse(block.text);
|
|
569
|
+
} catch {
|
|
570
|
+
}
|
|
571
|
+
}
|
|
357
572
|
} else if (block.type === "tool_use" && block.id && block.name) {
|
|
358
573
|
let args = {};
|
|
359
574
|
if (block.input) {
|
|
@@ -382,11 +597,12 @@ ${block.fileContent}\`\`\`
|
|
|
382
597
|
` });
|
|
383
598
|
}
|
|
384
599
|
}
|
|
600
|
+
const messageId = state.messageId || generateId();
|
|
385
601
|
const message = new AssistantMessage(
|
|
386
602
|
textContent,
|
|
387
603
|
toolCalls.length > 0 ? toolCalls : void 0,
|
|
388
604
|
{
|
|
389
|
-
id:
|
|
605
|
+
id: messageId,
|
|
390
606
|
metadata: {
|
|
391
607
|
anthropic: {
|
|
392
608
|
stop_reason: state.stopReason,
|
|
@@ -411,6 +627,16 @@ ${block.fileContent}\`\`\`
|
|
|
411
627
|
}
|
|
412
628
|
|
|
413
629
|
// src/providers/anthropic/llm.ts
|
|
630
|
+
function shouldUseNativeStructuredOutput(config, structure) {
|
|
631
|
+
if (!structure) {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
const betaHeader = config.headers?.["anthropic-beta"];
|
|
635
|
+
if (!betaHeader) {
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
return betaHeader.includes(betas.structuredOutputs);
|
|
639
|
+
}
|
|
414
640
|
var ANTHROPIC_API_URL = "https://api.anthropic.com/v1/messages";
|
|
415
641
|
var ANTHROPIC_VERSION = "2023-06-01";
|
|
416
642
|
var ANTHROPIC_CAPABILITIES = {
|
|
@@ -449,8 +675,12 @@ function createLLMHandler() {
|
|
|
449
675
|
"anthropic",
|
|
450
676
|
"llm"
|
|
451
677
|
);
|
|
678
|
+
const useNativeStructuredOutput = shouldUseNativeStructuredOutput(
|
|
679
|
+
request.config,
|
|
680
|
+
request.structure
|
|
681
|
+
);
|
|
452
682
|
const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;
|
|
453
|
-
const body = transformRequest(request, modelId);
|
|
683
|
+
const body = transformRequest(request, modelId, useNativeStructuredOutput);
|
|
454
684
|
const headers = {
|
|
455
685
|
"Content-Type": "application/json",
|
|
456
686
|
"x-api-key": apiKey,
|
|
@@ -475,11 +705,15 @@ function createLLMHandler() {
|
|
|
475
705
|
"anthropic",
|
|
476
706
|
"llm"
|
|
477
707
|
);
|
|
478
|
-
const data = await response
|
|
479
|
-
return transformResponse(data);
|
|
708
|
+
const data = await parseJsonResponse(response, "anthropic", "llm");
|
|
709
|
+
return transformResponse(data, useNativeStructuredOutput);
|
|
480
710
|
},
|
|
481
711
|
stream(request) {
|
|
482
712
|
const state = createStreamState();
|
|
713
|
+
const useNativeStructuredOutput = shouldUseNativeStructuredOutput(
|
|
714
|
+
request.config,
|
|
715
|
+
request.structure
|
|
716
|
+
);
|
|
483
717
|
let responseResolve;
|
|
484
718
|
let responseReject;
|
|
485
719
|
const responsePromise = new Promise((resolve, reject) => {
|
|
@@ -495,12 +729,13 @@ function createLLMHandler() {
|
|
|
495
729
|
"llm"
|
|
496
730
|
);
|
|
497
731
|
const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;
|
|
498
|
-
const body = transformRequest(request, modelId);
|
|
732
|
+
const body = transformRequest(request, modelId, useNativeStructuredOutput);
|
|
499
733
|
body.stream = true;
|
|
500
734
|
const headers = {
|
|
501
735
|
"Content-Type": "application/json",
|
|
502
736
|
"x-api-key": apiKey,
|
|
503
|
-
"anthropic-version": request.config.apiVersion ?? ANTHROPIC_VERSION
|
|
737
|
+
"anthropic-version": request.config.apiVersion ?? ANTHROPIC_VERSION,
|
|
738
|
+
Accept: "text/event-stream"
|
|
504
739
|
};
|
|
505
740
|
if (request.config.headers) {
|
|
506
741
|
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
@@ -549,16 +784,17 @@ function createLLMHandler() {
|
|
|
549
784
|
responseReject(error);
|
|
550
785
|
throw error;
|
|
551
786
|
}
|
|
552
|
-
const
|
|
553
|
-
|
|
787
|
+
const uppEvents = transformStreamEvent(event, state);
|
|
788
|
+
for (const uppEvent of uppEvents) {
|
|
554
789
|
yield uppEvent;
|
|
555
790
|
}
|
|
556
791
|
}
|
|
557
792
|
}
|
|
558
|
-
responseResolve(buildResponseFromState(state));
|
|
793
|
+
responseResolve(buildResponseFromState(state, useNativeStructuredOutput));
|
|
559
794
|
} catch (error) {
|
|
560
|
-
|
|
561
|
-
|
|
795
|
+
const err = toError(error);
|
|
796
|
+
responseReject(err);
|
|
797
|
+
throw err;
|
|
562
798
|
}
|
|
563
799
|
}
|
|
564
800
|
return {
|
|
@@ -574,74 +810,37 @@ function createLLMHandler() {
|
|
|
574
810
|
};
|
|
575
811
|
}
|
|
576
812
|
|
|
577
|
-
// src/providers/anthropic/
|
|
578
|
-
function
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
};
|
|
584
|
-
}
|
|
585
|
-
function computerTool(options) {
|
|
586
|
-
const { version = "20250124", ...rest } = options;
|
|
587
|
-
return {
|
|
588
|
-
type: version === "20251124" ? "computer_20251124" : "computer_20250124",
|
|
589
|
-
name: "computer",
|
|
590
|
-
...rest
|
|
591
|
-
};
|
|
592
|
-
}
|
|
593
|
-
function textEditorTool(options) {
|
|
594
|
-
const version = options?.version ?? "20250728";
|
|
595
|
-
return {
|
|
596
|
-
type: version === "20250728" ? "text_editor_20250728" : "text_editor_20250124",
|
|
597
|
-
name: version === "20250728" ? "str_replace_based_edit_tool" : "str_replace_editor",
|
|
598
|
-
...options?.max_characters !== void 0 && { max_characters: options.max_characters }
|
|
599
|
-
};
|
|
600
|
-
}
|
|
601
|
-
function bashTool() {
|
|
602
|
-
return {
|
|
603
|
-
type: "bash_20250124",
|
|
604
|
-
name: "bash"
|
|
605
|
-
};
|
|
606
|
-
}
|
|
607
|
-
function codeExecutionTool() {
|
|
608
|
-
return {
|
|
609
|
-
type: "code_execution_20250825",
|
|
610
|
-
name: "code_execution"
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
function toolSearchTool(options) {
|
|
614
|
-
const mode = options?.mode ?? "regex";
|
|
813
|
+
// src/providers/anthropic/index.ts
|
|
814
|
+
function createProviderConfig(options) {
|
|
815
|
+
if (!options?.betas || options.betas.length === 0) {
|
|
816
|
+
return void 0;
|
|
817
|
+
}
|
|
818
|
+
const betaHeader = options.betas.join(",");
|
|
615
819
|
return {
|
|
616
|
-
|
|
617
|
-
|
|
820
|
+
headers: {
|
|
821
|
+
"anthropic-beta": betaHeader
|
|
822
|
+
}
|
|
618
823
|
};
|
|
619
824
|
}
|
|
620
|
-
var tools = {
|
|
621
|
-
/** Creates a web search tool configuration */
|
|
622
|
-
webSearch: webSearchTool,
|
|
623
|
-
/** Creates a computer use tool configuration */
|
|
624
|
-
computer: computerTool,
|
|
625
|
-
/** Creates a text editor tool configuration */
|
|
626
|
-
textEditor: textEditorTool,
|
|
627
|
-
/** Creates a bash tool configuration */
|
|
628
|
-
bash: bashTool,
|
|
629
|
-
/** Creates a code execution tool configuration */
|
|
630
|
-
codeExecution: codeExecutionTool,
|
|
631
|
-
/** Creates a tool search tool configuration */
|
|
632
|
-
toolSearch: toolSearchTool
|
|
633
|
-
};
|
|
634
|
-
|
|
635
|
-
// src/providers/anthropic/index.ts
|
|
636
825
|
var anthropic = createProvider({
|
|
637
826
|
name: "anthropic",
|
|
638
827
|
version: "1.0.0",
|
|
639
|
-
|
|
828
|
+
handlers: {
|
|
640
829
|
llm: createLLMHandler()
|
|
830
|
+
},
|
|
831
|
+
createModelReference: (modelId, options, provider) => {
|
|
832
|
+
const providerConfig = createProviderConfig(options);
|
|
833
|
+
return {
|
|
834
|
+
modelId,
|
|
835
|
+
provider,
|
|
836
|
+
options,
|
|
837
|
+
...providerConfig && { providerConfig }
|
|
838
|
+
};
|
|
641
839
|
}
|
|
642
840
|
});
|
|
643
841
|
export {
|
|
644
842
|
anthropic,
|
|
843
|
+
betas,
|
|
645
844
|
tools
|
|
646
845
|
};
|
|
647
846
|
//# sourceMappingURL=index.js.map
|