@ai-sdk/anthropic 2.0.0-canary.1 → 2.0.0-canary.11
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/CHANGELOG.md +109 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +190 -170
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +192 -172
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +4 -4
- package/{internal/dist → dist/internal}/index.d.ts +4 -4
- package/{internal/dist → dist/internal}/index.js +187 -169
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +189 -171
- package/dist/internal/index.mjs.map +1 -0
- package/internal.d.ts +1 -0
- package/package.json +16 -14
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -51,13 +51,15 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
|
|
|
51
51
|
|
|
52
52
|
// src/anthropic-prepare-tools.ts
|
|
53
53
|
var import_provider = require("@ai-sdk/provider");
|
|
54
|
-
function prepareTools(
|
|
55
|
-
|
|
56
|
-
|
|
54
|
+
function prepareTools({
|
|
55
|
+
tools,
|
|
56
|
+
toolChoice
|
|
57
|
+
}) {
|
|
58
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
57
59
|
const toolWarnings = [];
|
|
58
60
|
const betas = /* @__PURE__ */ new Set();
|
|
59
61
|
if (tools == null) {
|
|
60
|
-
return { tools: void 0,
|
|
62
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
61
63
|
}
|
|
62
64
|
const anthropicTools2 = [];
|
|
63
65
|
for (const tool of tools) {
|
|
@@ -129,11 +131,10 @@ function prepareTools(mode) {
|
|
|
129
131
|
break;
|
|
130
132
|
}
|
|
131
133
|
}
|
|
132
|
-
const toolChoice = mode.toolChoice;
|
|
133
134
|
if (toolChoice == null) {
|
|
134
135
|
return {
|
|
135
136
|
tools: anthropicTools2,
|
|
136
|
-
|
|
137
|
+
toolChoice: void 0,
|
|
137
138
|
toolWarnings,
|
|
138
139
|
betas
|
|
139
140
|
};
|
|
@@ -143,30 +144,30 @@ function prepareTools(mode) {
|
|
|
143
144
|
case "auto":
|
|
144
145
|
return {
|
|
145
146
|
tools: anthropicTools2,
|
|
146
|
-
|
|
147
|
+
toolChoice: { type: "auto" },
|
|
147
148
|
toolWarnings,
|
|
148
149
|
betas
|
|
149
150
|
};
|
|
150
151
|
case "required":
|
|
151
152
|
return {
|
|
152
153
|
tools: anthropicTools2,
|
|
153
|
-
|
|
154
|
+
toolChoice: { type: "any" },
|
|
154
155
|
toolWarnings,
|
|
155
156
|
betas
|
|
156
157
|
};
|
|
157
158
|
case "none":
|
|
158
|
-
return { tools: void 0,
|
|
159
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
159
160
|
case "tool":
|
|
160
161
|
return {
|
|
161
162
|
tools: anthropicTools2,
|
|
162
|
-
|
|
163
|
+
toolChoice: { type: "tool", name: toolChoice.toolName },
|
|
163
164
|
toolWarnings,
|
|
164
165
|
betas
|
|
165
166
|
};
|
|
166
167
|
default: {
|
|
167
168
|
const _exhaustiveCheck = type;
|
|
168
169
|
throw new import_provider.UnsupportedFunctionalityError({
|
|
169
|
-
functionality: `
|
|
170
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
170
171
|
});
|
|
171
172
|
}
|
|
172
173
|
}
|
|
@@ -175,12 +176,12 @@ function prepareTools(mode) {
|
|
|
175
176
|
// src/convert-to-anthropic-messages-prompt.ts
|
|
176
177
|
var import_provider2 = require("@ai-sdk/provider");
|
|
177
178
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
178
|
-
function convertToAnthropicMessagesPrompt({
|
|
179
|
+
async function convertToAnthropicMessagesPrompt({
|
|
179
180
|
prompt,
|
|
180
181
|
sendReasoning,
|
|
181
182
|
warnings
|
|
182
183
|
}) {
|
|
183
|
-
var _a, _b, _c
|
|
184
|
+
var _a, _b, _c;
|
|
184
185
|
const betas = /* @__PURE__ */ new Set();
|
|
185
186
|
const blocks = groupIntoBlocks(prompt);
|
|
186
187
|
let system = void 0;
|
|
@@ -202,10 +203,10 @@ function convertToAnthropicMessagesPrompt({
|
|
|
202
203
|
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
203
204
|
});
|
|
204
205
|
}
|
|
205
|
-
system = block.messages.map(({ content,
|
|
206
|
+
system = block.messages.map(({ content, providerOptions }) => ({
|
|
206
207
|
type: "text",
|
|
207
208
|
text: content,
|
|
208
|
-
cache_control: getCacheControl(
|
|
209
|
+
cache_control: getCacheControl(providerOptions)
|
|
209
210
|
}));
|
|
210
211
|
break;
|
|
211
212
|
}
|
|
@@ -218,7 +219,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
218
219
|
for (let j = 0; j < content.length; j++) {
|
|
219
220
|
const part = content[j];
|
|
220
221
|
const isLastPart = j === content.length - 1;
|
|
221
|
-
const cacheControl = (_a = getCacheControl(part.
|
|
222
|
+
const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
222
223
|
switch (part.type) {
|
|
223
224
|
case "text": {
|
|
224
225
|
anthropicContent.push({
|
|
@@ -228,42 +229,39 @@ function convertToAnthropicMessagesPrompt({
|
|
|
228
229
|
});
|
|
229
230
|
break;
|
|
230
231
|
}
|
|
231
|
-
case "image": {
|
|
232
|
-
anthropicContent.push({
|
|
233
|
-
type: "image",
|
|
234
|
-
source: part.image instanceof URL ? {
|
|
235
|
-
type: "url",
|
|
236
|
-
url: part.image.toString()
|
|
237
|
-
} : {
|
|
238
|
-
type: "base64",
|
|
239
|
-
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
240
|
-
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(part.image)
|
|
241
|
-
},
|
|
242
|
-
cache_control: cacheControl
|
|
243
|
-
});
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
232
|
case "file": {
|
|
247
|
-
if (part.
|
|
248
|
-
|
|
249
|
-
|
|
233
|
+
if (part.mediaType.startsWith("image/")) {
|
|
234
|
+
anthropicContent.push({
|
|
235
|
+
type: "image",
|
|
236
|
+
source: part.data instanceof URL ? {
|
|
237
|
+
type: "url",
|
|
238
|
+
url: part.data.toString()
|
|
239
|
+
} : {
|
|
240
|
+
type: "base64",
|
|
241
|
+
media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
|
|
242
|
+
data: (0, import_provider_utils2.convertToBase64)(part.data)
|
|
243
|
+
},
|
|
244
|
+
cache_control: cacheControl
|
|
250
245
|
});
|
|
251
|
-
}
|
|
252
|
-
|
|
246
|
+
} else if (part.mediaType === "application/pdf") {
|
|
247
|
+
betas.add("pdfs-2024-09-25");
|
|
248
|
+
anthropicContent.push({
|
|
249
|
+
type: "document",
|
|
250
|
+
source: part.data instanceof URL ? {
|
|
251
|
+
type: "url",
|
|
252
|
+
url: part.data.toString()
|
|
253
|
+
} : {
|
|
254
|
+
type: "base64",
|
|
255
|
+
media_type: "application/pdf",
|
|
256
|
+
data: (0, import_provider_utils2.convertToBase64)(part.data)
|
|
257
|
+
},
|
|
258
|
+
cache_control: cacheControl
|
|
259
|
+
});
|
|
260
|
+
} else {
|
|
253
261
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
254
|
-
functionality:
|
|
262
|
+
functionality: `media type: ${part.mediaType}`
|
|
255
263
|
});
|
|
256
264
|
}
|
|
257
|
-
betas.add("pdfs-2024-09-25");
|
|
258
|
-
anthropicContent.push({
|
|
259
|
-
type: "document",
|
|
260
|
-
source: {
|
|
261
|
-
type: "base64",
|
|
262
|
-
media_type: "application/pdf",
|
|
263
|
-
data: part.data
|
|
264
|
-
},
|
|
265
|
-
cache_control: cacheControl
|
|
266
|
-
});
|
|
267
265
|
break;
|
|
268
266
|
}
|
|
269
267
|
}
|
|
@@ -274,7 +272,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
274
272
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
275
273
|
const part = content[i2];
|
|
276
274
|
const isLastPart = i2 === content.length - 1;
|
|
277
|
-
const cacheControl = (
|
|
275
|
+
const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
278
276
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
279
277
|
var _a2;
|
|
280
278
|
switch (part2.type) {
|
|
@@ -289,7 +287,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
289
287
|
type: "image",
|
|
290
288
|
source: {
|
|
291
289
|
type: "base64",
|
|
292
|
-
media_type: (_a2 = part2.
|
|
290
|
+
media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
|
|
293
291
|
data: part2.data
|
|
294
292
|
},
|
|
295
293
|
cache_control: void 0
|
|
@@ -324,7 +322,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
324
322
|
for (let k = 0; k < content.length; k++) {
|
|
325
323
|
const part = content[k];
|
|
326
324
|
const isLastContentPart = k === content.length - 1;
|
|
327
|
-
const cacheControl = (
|
|
325
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
328
326
|
switch (part.type) {
|
|
329
327
|
case "text": {
|
|
330
328
|
anthropicContent.push({
|
|
@@ -341,12 +339,37 @@ function convertToAnthropicMessagesPrompt({
|
|
|
341
339
|
}
|
|
342
340
|
case "reasoning": {
|
|
343
341
|
if (sendReasoning) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
cache_control: cacheControl
|
|
342
|
+
const reasoningMetadata = await (0, import_provider_utils2.parseProviderOptions)({
|
|
343
|
+
provider: "anthropic",
|
|
344
|
+
providerOptions: part.providerOptions,
|
|
345
|
+
schema: anthropicReasoningMetadataSchema
|
|
349
346
|
});
|
|
347
|
+
if (reasoningMetadata != null) {
|
|
348
|
+
if (reasoningMetadata.signature != null) {
|
|
349
|
+
anthropicContent.push({
|
|
350
|
+
type: "thinking",
|
|
351
|
+
thinking: part.text,
|
|
352
|
+
signature: reasoningMetadata.signature,
|
|
353
|
+
cache_control: cacheControl
|
|
354
|
+
});
|
|
355
|
+
} else if (reasoningMetadata.redactedData != null) {
|
|
356
|
+
anthropicContent.push({
|
|
357
|
+
type: "redacted_thinking",
|
|
358
|
+
data: reasoningMetadata.redactedData,
|
|
359
|
+
cache_control: cacheControl
|
|
360
|
+
});
|
|
361
|
+
} else {
|
|
362
|
+
warnings.push({
|
|
363
|
+
type: "other",
|
|
364
|
+
message: "unsupported reasoning metadata"
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
warnings.push({
|
|
369
|
+
type: "other",
|
|
370
|
+
message: "unsupported reasoning metadata"
|
|
371
|
+
});
|
|
372
|
+
}
|
|
350
373
|
} else {
|
|
351
374
|
warnings.push({
|
|
352
375
|
type: "other",
|
|
@@ -355,14 +378,6 @@ function convertToAnthropicMessagesPrompt({
|
|
|
355
378
|
}
|
|
356
379
|
break;
|
|
357
380
|
}
|
|
358
|
-
case "redacted-reasoning": {
|
|
359
|
-
anthropicContent.push({
|
|
360
|
-
type: "redacted_thinking",
|
|
361
|
-
data: part.data,
|
|
362
|
-
cache_control: cacheControl
|
|
363
|
-
});
|
|
364
|
-
break;
|
|
365
|
-
}
|
|
366
381
|
case "tool-call": {
|
|
367
382
|
anthropicContent.push({
|
|
368
383
|
type: "tool_use",
|
|
@@ -381,7 +396,7 @@ function convertToAnthropicMessagesPrompt({
|
|
|
381
396
|
}
|
|
382
397
|
default: {
|
|
383
398
|
const _exhaustiveCheck = type;
|
|
384
|
-
throw new Error(`
|
|
399
|
+
throw new Error(`content type: ${_exhaustiveCheck}`);
|
|
385
400
|
}
|
|
386
401
|
}
|
|
387
402
|
}
|
|
@@ -456,21 +471,23 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
456
471
|
var AnthropicMessagesLanguageModel = class {
|
|
457
472
|
constructor(modelId, settings, config) {
|
|
458
473
|
this.specificationVersion = "v2";
|
|
459
|
-
this.defaultObjectGenerationMode = "tool";
|
|
460
474
|
this.modelId = modelId;
|
|
461
475
|
this.settings = settings;
|
|
462
476
|
this.config = config;
|
|
463
477
|
}
|
|
478
|
+
supportsUrl(url) {
|
|
479
|
+
return url.protocol === "https:";
|
|
480
|
+
}
|
|
464
481
|
get provider() {
|
|
465
482
|
return this.config.provider;
|
|
466
483
|
}
|
|
467
|
-
|
|
468
|
-
|
|
484
|
+
async getSupportedUrls() {
|
|
485
|
+
var _a, _b, _c;
|
|
486
|
+
return (_c = (_b = (_a = this.config).getSupportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
469
487
|
}
|
|
470
488
|
async getArgs({
|
|
471
|
-
mode,
|
|
472
489
|
prompt,
|
|
473
|
-
|
|
490
|
+
maxOutputTokens = 4096,
|
|
474
491
|
// 4096: max model output tokens TODO update default in v5
|
|
475
492
|
temperature,
|
|
476
493
|
topP,
|
|
@@ -480,10 +497,11 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
480
497
|
stopSequences,
|
|
481
498
|
responseFormat,
|
|
482
499
|
seed,
|
|
483
|
-
|
|
500
|
+
tools,
|
|
501
|
+
toolChoice,
|
|
502
|
+
providerOptions
|
|
484
503
|
}) {
|
|
485
504
|
var _a, _b, _c;
|
|
486
|
-
const type = mode.type;
|
|
487
505
|
const warnings = [];
|
|
488
506
|
if (frequencyPenalty != null) {
|
|
489
507
|
warnings.push({
|
|
@@ -510,12 +528,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
510
528
|
details: "JSON response format is not supported."
|
|
511
529
|
});
|
|
512
530
|
}
|
|
513
|
-
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
|
|
531
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
|
|
514
532
|
prompt,
|
|
515
533
|
sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
|
|
516
534
|
warnings
|
|
517
535
|
});
|
|
518
|
-
const anthropicOptions = (0, import_provider_utils3.parseProviderOptions)({
|
|
536
|
+
const anthropicOptions = await (0, import_provider_utils3.parseProviderOptions)({
|
|
519
537
|
provider: "anthropic",
|
|
520
538
|
providerOptions,
|
|
521
539
|
schema: anthropicProviderOptionsSchema
|
|
@@ -526,7 +544,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
526
544
|
// model id:
|
|
527
545
|
model: this.modelId,
|
|
528
546
|
// standardized settings:
|
|
529
|
-
max_tokens:
|
|
547
|
+
max_tokens: maxOutputTokens,
|
|
530
548
|
temperature,
|
|
531
549
|
top_k: topK,
|
|
532
550
|
top_p: topP,
|
|
@@ -569,44 +587,23 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
569
587
|
details: "topP is not supported when thinking is enabled"
|
|
570
588
|
});
|
|
571
589
|
}
|
|
572
|
-
baseArgs.max_tokens =
|
|
573
|
-
}
|
|
574
|
-
switch (type) {
|
|
575
|
-
case "regular": {
|
|
576
|
-
const {
|
|
577
|
-
tools,
|
|
578
|
-
tool_choice,
|
|
579
|
-
toolWarnings,
|
|
580
|
-
betas: toolsBetas
|
|
581
|
-
} = prepareTools(mode);
|
|
582
|
-
return {
|
|
583
|
-
args: { ...baseArgs, tools, tool_choice },
|
|
584
|
-
warnings: [...warnings, ...toolWarnings],
|
|
585
|
-
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
586
|
-
};
|
|
587
|
-
}
|
|
588
|
-
case "object-json": {
|
|
589
|
-
throw new import_provider3.UnsupportedFunctionalityError({
|
|
590
|
-
functionality: "json-mode object generation"
|
|
591
|
-
});
|
|
592
|
-
}
|
|
593
|
-
case "object-tool": {
|
|
594
|
-
const { name, description, parameters } = mode.tool;
|
|
595
|
-
return {
|
|
596
|
-
args: {
|
|
597
|
-
...baseArgs,
|
|
598
|
-
tools: [{ name, description, input_schema: parameters }],
|
|
599
|
-
tool_choice: { type: "tool", name }
|
|
600
|
-
},
|
|
601
|
-
warnings,
|
|
602
|
-
betas: messagesBetas
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
default: {
|
|
606
|
-
const _exhaustiveCheck = type;
|
|
607
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
608
|
-
}
|
|
590
|
+
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
609
591
|
}
|
|
592
|
+
const {
|
|
593
|
+
tools: anthropicTools2,
|
|
594
|
+
toolChoice: anthropicToolChoice,
|
|
595
|
+
toolWarnings,
|
|
596
|
+
betas: toolsBetas
|
|
597
|
+
} = prepareTools({ tools, toolChoice });
|
|
598
|
+
return {
|
|
599
|
+
args: {
|
|
600
|
+
...baseArgs,
|
|
601
|
+
tools: anthropicTools2,
|
|
602
|
+
tool_choice: anthropicToolChoice
|
|
603
|
+
},
|
|
604
|
+
warnings: [...warnings, ...toolWarnings],
|
|
605
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
606
|
+
};
|
|
610
607
|
}
|
|
611
608
|
async getHeaders({
|
|
612
609
|
betas,
|
|
@@ -644,56 +641,62 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
644
641
|
abortSignal: options.abortSignal,
|
|
645
642
|
fetch: this.config.fetch
|
|
646
643
|
});
|
|
647
|
-
const
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
644
|
+
const content = [];
|
|
645
|
+
for (const part of response.content) {
|
|
646
|
+
switch (part.type) {
|
|
647
|
+
case "text": {
|
|
648
|
+
content.push({ type: "text", text: part.text });
|
|
649
|
+
break;
|
|
650
|
+
}
|
|
651
|
+
case "thinking": {
|
|
652
|
+
content.push({
|
|
653
|
+
type: "reasoning",
|
|
654
|
+
text: part.thinking,
|
|
655
|
+
providerMetadata: {
|
|
656
|
+
anthropic: {
|
|
657
|
+
signature: part.signature
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
case "redacted_thinking": {
|
|
664
|
+
content.push({
|
|
665
|
+
type: "reasoning",
|
|
666
|
+
text: "",
|
|
667
|
+
providerMetadata: {
|
|
668
|
+
anthropic: {
|
|
669
|
+
redactedData: part.data
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
case "tool_use": {
|
|
676
|
+
content.push({
|
|
677
|
+
type: "tool-call",
|
|
660
678
|
toolCallType: "function",
|
|
661
|
-
toolCallId:
|
|
662
|
-
toolName:
|
|
663
|
-
args: JSON.stringify(
|
|
679
|
+
toolCallId: part.id,
|
|
680
|
+
toolName: part.name,
|
|
681
|
+
args: JSON.stringify(part.input)
|
|
664
682
|
});
|
|
683
|
+
break;
|
|
665
684
|
}
|
|
666
685
|
}
|
|
667
686
|
}
|
|
668
|
-
const reasoning = response.content.filter(
|
|
669
|
-
(content) => content.type === "redacted_thinking" || content.type === "thinking"
|
|
670
|
-
).map(
|
|
671
|
-
(content) => content.type === "thinking" ? {
|
|
672
|
-
type: "text",
|
|
673
|
-
text: content.thinking,
|
|
674
|
-
signature: content.signature
|
|
675
|
-
} : {
|
|
676
|
-
type: "redacted",
|
|
677
|
-
data: content.data
|
|
678
|
-
}
|
|
679
|
-
);
|
|
680
687
|
return {
|
|
681
|
-
|
|
682
|
-
reasoning: reasoning.length > 0 ? reasoning : void 0,
|
|
683
|
-
toolCalls,
|
|
688
|
+
content,
|
|
684
689
|
finishReason: mapAnthropicStopReason(response.stop_reason),
|
|
685
690
|
usage: {
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
},
|
|
689
|
-
rawCall: { rawPrompt, rawSettings },
|
|
690
|
-
rawResponse: {
|
|
691
|
-
headers: responseHeaders,
|
|
692
|
-
body: rawResponse
|
|
691
|
+
inputTokens: response.usage.input_tokens,
|
|
692
|
+
outputTokens: response.usage.output_tokens
|
|
693
693
|
},
|
|
694
|
+
request: { body: args },
|
|
694
695
|
response: {
|
|
695
696
|
id: (_a = response.id) != null ? _a : void 0,
|
|
696
|
-
modelId: (_b = response.model) != null ? _b : void 0
|
|
697
|
+
modelId: (_b = response.model) != null ? _b : void 0,
|
|
698
|
+
headers: responseHeaders,
|
|
699
|
+
body: rawResponse
|
|
697
700
|
},
|
|
698
701
|
warnings,
|
|
699
702
|
providerMetadata: {
|
|
@@ -701,8 +704,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
701
704
|
cacheCreationInputTokens: (_c = response.usage.cache_creation_input_tokens) != null ? _c : null,
|
|
702
705
|
cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
|
|
703
706
|
}
|
|
704
|
-
}
|
|
705
|
-
request: { body: JSON.stringify(args) }
|
|
707
|
+
}
|
|
706
708
|
};
|
|
707
709
|
}
|
|
708
710
|
async doStream(options) {
|
|
@@ -719,11 +721,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
719
721
|
abortSignal: options.abortSignal,
|
|
720
722
|
fetch: this.config.fetch
|
|
721
723
|
});
|
|
722
|
-
const { messages: rawPrompt, ...rawSettings } = args;
|
|
723
724
|
let finishReason = "unknown";
|
|
724
725
|
const usage = {
|
|
725
|
-
|
|
726
|
-
|
|
726
|
+
inputTokens: void 0,
|
|
727
|
+
outputTokens: void 0
|
|
727
728
|
};
|
|
728
729
|
const toolCallContentBlocks = {};
|
|
729
730
|
let providerMetadata = void 0;
|
|
@@ -731,6 +732,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
731
732
|
return {
|
|
732
733
|
stream: response.pipeThrough(
|
|
733
734
|
new TransformStream({
|
|
735
|
+
start(controller) {
|
|
736
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
737
|
+
},
|
|
734
738
|
transform(chunk, controller) {
|
|
735
739
|
var _a, _b, _c, _d;
|
|
736
740
|
if (!chunk.success) {
|
|
@@ -752,9 +756,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
752
756
|
}
|
|
753
757
|
case "redacted_thinking": {
|
|
754
758
|
controller.enqueue({
|
|
755
|
-
type: "
|
|
756
|
-
|
|
759
|
+
type: "reasoning",
|
|
760
|
+
text: "",
|
|
761
|
+
providerMetadata: {
|
|
762
|
+
anthropic: {
|
|
763
|
+
redactedData: value.content_block.data
|
|
764
|
+
}
|
|
765
|
+
}
|
|
757
766
|
});
|
|
767
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
|
758
768
|
return;
|
|
759
769
|
}
|
|
760
770
|
case "tool_use": {
|
|
@@ -793,24 +803,30 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
793
803
|
switch (deltaType) {
|
|
794
804
|
case "text_delta": {
|
|
795
805
|
controller.enqueue({
|
|
796
|
-
type: "text
|
|
797
|
-
|
|
806
|
+
type: "text",
|
|
807
|
+
text: value.delta.text
|
|
798
808
|
});
|
|
799
809
|
return;
|
|
800
810
|
}
|
|
801
811
|
case "thinking_delta": {
|
|
802
812
|
controller.enqueue({
|
|
803
813
|
type: "reasoning",
|
|
804
|
-
|
|
814
|
+
text: value.delta.thinking
|
|
805
815
|
});
|
|
806
816
|
return;
|
|
807
817
|
}
|
|
808
818
|
case "signature_delta": {
|
|
809
819
|
if (blockType === "thinking") {
|
|
810
820
|
controller.enqueue({
|
|
811
|
-
type: "reasoning
|
|
812
|
-
|
|
821
|
+
type: "reasoning",
|
|
822
|
+
text: "",
|
|
823
|
+
providerMetadata: {
|
|
824
|
+
anthropic: {
|
|
825
|
+
signature: value.delta.signature
|
|
826
|
+
}
|
|
827
|
+
}
|
|
813
828
|
});
|
|
829
|
+
controller.enqueue({ type: "reasoning-part-finish" });
|
|
814
830
|
}
|
|
815
831
|
return;
|
|
816
832
|
}
|
|
@@ -835,8 +851,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
835
851
|
}
|
|
836
852
|
}
|
|
837
853
|
case "message_start": {
|
|
838
|
-
usage.
|
|
839
|
-
usage.
|
|
854
|
+
usage.inputTokens = value.message.usage.input_tokens;
|
|
855
|
+
usage.outputTokens = value.message.usage.output_tokens;
|
|
840
856
|
providerMetadata = {
|
|
841
857
|
anthropic: {
|
|
842
858
|
cacheCreationInputTokens: (_a = value.message.usage.cache_creation_input_tokens) != null ? _a : null,
|
|
@@ -851,7 +867,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
851
867
|
return;
|
|
852
868
|
}
|
|
853
869
|
case "message_delta": {
|
|
854
|
-
usage.
|
|
870
|
+
usage.outputTokens = value.usage.output_tokens;
|
|
855
871
|
finishReason = mapAnthropicStopReason(value.delta.stop_reason);
|
|
856
872
|
return;
|
|
857
873
|
}
|
|
@@ -876,10 +892,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
876
892
|
}
|
|
877
893
|
})
|
|
878
894
|
),
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
warnings,
|
|
882
|
-
request: { body: JSON.stringify(body) }
|
|
895
|
+
request: { body },
|
|
896
|
+
response: { headers: responseHeaders }
|
|
883
897
|
};
|
|
884
898
|
}
|
|
885
899
|
};
|
|
@@ -1006,6 +1020,10 @@ var anthropicProviderOptionsSchema = import_zod2.z.object({
|
|
|
1006
1020
|
budgetTokens: import_zod2.z.number().optional()
|
|
1007
1021
|
}).optional()
|
|
1008
1022
|
});
|
|
1023
|
+
var anthropicReasoningMetadataSchema = import_zod2.z.object({
|
|
1024
|
+
signature: import_zod2.z.string().optional(),
|
|
1025
|
+
redactedData: import_zod2.z.string().optional()
|
|
1026
|
+
});
|
|
1009
1027
|
|
|
1010
1028
|
// src/anthropic-tools.ts
|
|
1011
1029
|
var import_zod3 = require("zod");
|
|
@@ -1172,7 +1190,9 @@ function createAnthropic(options = {}) {
|
|
|
1172
1190
|
baseURL,
|
|
1173
1191
|
headers: getHeaders,
|
|
1174
1192
|
fetch: options.fetch,
|
|
1175
|
-
|
|
1193
|
+
getSupportedUrls: async () => ({
|
|
1194
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1195
|
+
})
|
|
1176
1196
|
});
|
|
1177
1197
|
const provider = function(modelId, settings) {
|
|
1178
1198
|
if (new.target) {
|