@ai-sdk/anthropic 1.0.3 → 1.0.5
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 +14 -0
- package/dist/index.js +16 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +18 -9
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.d.mts +227 -0
- package/internal/dist/index.d.ts +227 -0
- package/internal/dist/index.js +916 -0
- package/internal/dist/index.js.map +1 -0
- package/internal/dist/index.mjs +900 -0
- package/internal/dist/index.mjs.map +1 -0
- package/package.json +11 -4
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/internal/index.ts
|
|
21
|
+
var internal_exports = {};
|
|
22
|
+
__export(internal_exports, {
|
|
23
|
+
AnthropicMessagesLanguageModel: () => AnthropicMessagesLanguageModel,
|
|
24
|
+
anthropicTools: () => anthropicTools
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(internal_exports);
|
|
27
|
+
|
|
28
|
+
// src/anthropic-messages-language-model.ts
|
|
29
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
30
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
31
|
+
var import_zod2 = require("zod");
|
|
32
|
+
|
|
33
|
+
// src/anthropic-error.ts
|
|
34
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
35
|
+
var import_zod = require("zod");
|
|
36
|
+
var anthropicErrorDataSchema = import_zod.z.object({
|
|
37
|
+
type: import_zod.z.literal("error"),
|
|
38
|
+
error: import_zod.z.object({
|
|
39
|
+
type: import_zod.z.string(),
|
|
40
|
+
message: import_zod.z.string()
|
|
41
|
+
})
|
|
42
|
+
});
|
|
43
|
+
var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
|
44
|
+
errorSchema: anthropicErrorDataSchema,
|
|
45
|
+
errorToMessage: (data) => data.error.message
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// src/convert-to-anthropic-messages-prompt.ts
|
|
49
|
+
var import_provider = require("@ai-sdk/provider");
|
|
50
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
51
|
+
function convertToAnthropicMessagesPrompt({
|
|
52
|
+
prompt,
|
|
53
|
+
cacheControl: isCacheControlEnabled
|
|
54
|
+
}) {
|
|
55
|
+
var _a, _b, _c, _d;
|
|
56
|
+
const betas = /* @__PURE__ */ new Set();
|
|
57
|
+
const blocks = groupIntoBlocks(prompt);
|
|
58
|
+
let system = void 0;
|
|
59
|
+
const messages = [];
|
|
60
|
+
function getCacheControl(providerMetadata) {
|
|
61
|
+
var _a2;
|
|
62
|
+
if (isCacheControlEnabled === false) {
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
66
|
+
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
67
|
+
return cacheControlValue;
|
|
68
|
+
}
|
|
69
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
70
|
+
const block = blocks[i];
|
|
71
|
+
const isLastBlock = i === blocks.length - 1;
|
|
72
|
+
const type = block.type;
|
|
73
|
+
switch (type) {
|
|
74
|
+
case "system": {
|
|
75
|
+
if (system != null) {
|
|
76
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
77
|
+
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
system = block.messages.map(({ content, providerMetadata }) => ({
|
|
81
|
+
type: "text",
|
|
82
|
+
text: content,
|
|
83
|
+
cache_control: getCacheControl(providerMetadata)
|
|
84
|
+
}));
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
case "user": {
|
|
88
|
+
const anthropicContent = [];
|
|
89
|
+
for (const message of block.messages) {
|
|
90
|
+
const { role, content } = message;
|
|
91
|
+
switch (role) {
|
|
92
|
+
case "user": {
|
|
93
|
+
for (let j = 0; j < content.length; j++) {
|
|
94
|
+
const part = content[j];
|
|
95
|
+
const isLastPart = j === content.length - 1;
|
|
96
|
+
const cacheControl = (_a = getCacheControl(part.providerMetadata)) != null ? _a : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
97
|
+
switch (part.type) {
|
|
98
|
+
case "text": {
|
|
99
|
+
anthropicContent.push({
|
|
100
|
+
type: "text",
|
|
101
|
+
text: part.text,
|
|
102
|
+
cache_control: cacheControl
|
|
103
|
+
});
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "image": {
|
|
107
|
+
if (part.image instanceof URL) {
|
|
108
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
109
|
+
functionality: "Image URLs in user messages"
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
anthropicContent.push({
|
|
113
|
+
type: "image",
|
|
114
|
+
source: {
|
|
115
|
+
type: "base64",
|
|
116
|
+
media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
117
|
+
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(part.image)
|
|
118
|
+
},
|
|
119
|
+
cache_control: cacheControl
|
|
120
|
+
});
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "file": {
|
|
124
|
+
if (part.data instanceof URL) {
|
|
125
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
126
|
+
functionality: "Image URLs in user messages"
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (part.mimeType !== "application/pdf") {
|
|
130
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
131
|
+
functionality: "Non-PDF files in user messages"
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
betas.add("pdfs-2024-09-25");
|
|
135
|
+
anthropicContent.push({
|
|
136
|
+
type: "document",
|
|
137
|
+
source: {
|
|
138
|
+
type: "base64",
|
|
139
|
+
media_type: "application/pdf",
|
|
140
|
+
data: part.data
|
|
141
|
+
},
|
|
142
|
+
cache_control: cacheControl
|
|
143
|
+
});
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case "tool": {
|
|
151
|
+
for (let i2 = 0; i2 < content.length; i2++) {
|
|
152
|
+
const part = content[i2];
|
|
153
|
+
const isLastPart = i2 === content.length - 1;
|
|
154
|
+
const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
155
|
+
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
156
|
+
var _a2;
|
|
157
|
+
switch (part2.type) {
|
|
158
|
+
case "text":
|
|
159
|
+
return {
|
|
160
|
+
type: "text",
|
|
161
|
+
text: part2.text,
|
|
162
|
+
cache_control: void 0
|
|
163
|
+
};
|
|
164
|
+
case "image":
|
|
165
|
+
return {
|
|
166
|
+
type: "image",
|
|
167
|
+
source: {
|
|
168
|
+
type: "base64",
|
|
169
|
+
media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
|
|
170
|
+
data: part2.data
|
|
171
|
+
},
|
|
172
|
+
cache_control: void 0
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
}) : JSON.stringify(part.result);
|
|
176
|
+
anthropicContent.push({
|
|
177
|
+
type: "tool_result",
|
|
178
|
+
tool_use_id: part.toolCallId,
|
|
179
|
+
content: toolResultContent,
|
|
180
|
+
is_error: part.isError,
|
|
181
|
+
cache_control: cacheControl
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
default: {
|
|
187
|
+
const _exhaustiveCheck = role;
|
|
188
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
messages.push({ role: "user", content: anthropicContent });
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case "assistant": {
|
|
196
|
+
const anthropicContent = [];
|
|
197
|
+
for (let j = 0; j < block.messages.length; j++) {
|
|
198
|
+
const message = block.messages[j];
|
|
199
|
+
const isLastMessage = j === block.messages.length - 1;
|
|
200
|
+
const { content } = message;
|
|
201
|
+
for (let k = 0; k < content.length; k++) {
|
|
202
|
+
const part = content[k];
|
|
203
|
+
const isLastContentPart = k === content.length - 1;
|
|
204
|
+
const cacheControl = (_d = getCacheControl(part.providerMetadata)) != null ? _d : isLastContentPart ? getCacheControl(message.providerMetadata) : void 0;
|
|
205
|
+
switch (part.type) {
|
|
206
|
+
case "text": {
|
|
207
|
+
anthropicContent.push({
|
|
208
|
+
type: "text",
|
|
209
|
+
text: (
|
|
210
|
+
// trim the last text part if it's the last message in the block
|
|
211
|
+
// because Anthropic does not allow trailing whitespace
|
|
212
|
+
// in pre-filled assistant responses
|
|
213
|
+
isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
|
|
214
|
+
),
|
|
215
|
+
cache_control: cacheControl
|
|
216
|
+
});
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
case "tool-call": {
|
|
220
|
+
anthropicContent.push({
|
|
221
|
+
type: "tool_use",
|
|
222
|
+
id: part.toolCallId,
|
|
223
|
+
name: part.toolName,
|
|
224
|
+
input: part.args,
|
|
225
|
+
cache_control: cacheControl
|
|
226
|
+
});
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
messages.push({ role: "assistant", content: anthropicContent });
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
default: {
|
|
236
|
+
const _exhaustiveCheck = type;
|
|
237
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
prompt: { system, messages },
|
|
243
|
+
betas
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
function groupIntoBlocks(prompt) {
|
|
247
|
+
const blocks = [];
|
|
248
|
+
let currentBlock = void 0;
|
|
249
|
+
for (const message of prompt) {
|
|
250
|
+
const { role } = message;
|
|
251
|
+
switch (role) {
|
|
252
|
+
case "system": {
|
|
253
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") {
|
|
254
|
+
currentBlock = { type: "system", messages: [] };
|
|
255
|
+
blocks.push(currentBlock);
|
|
256
|
+
}
|
|
257
|
+
currentBlock.messages.push(message);
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
case "assistant": {
|
|
261
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") {
|
|
262
|
+
currentBlock = { type: "assistant", messages: [] };
|
|
263
|
+
blocks.push(currentBlock);
|
|
264
|
+
}
|
|
265
|
+
currentBlock.messages.push(message);
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
case "user": {
|
|
269
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
|
|
270
|
+
currentBlock = { type: "user", messages: [] };
|
|
271
|
+
blocks.push(currentBlock);
|
|
272
|
+
}
|
|
273
|
+
currentBlock.messages.push(message);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case "tool": {
|
|
277
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
|
|
278
|
+
currentBlock = { type: "user", messages: [] };
|
|
279
|
+
blocks.push(currentBlock);
|
|
280
|
+
}
|
|
281
|
+
currentBlock.messages.push(message);
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
default: {
|
|
285
|
+
const _exhaustiveCheck = role;
|
|
286
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return blocks;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/map-anthropic-stop-reason.ts
|
|
294
|
+
function mapAnthropicStopReason(finishReason) {
|
|
295
|
+
switch (finishReason) {
|
|
296
|
+
case "end_turn":
|
|
297
|
+
case "stop_sequence":
|
|
298
|
+
return "stop";
|
|
299
|
+
case "tool_use":
|
|
300
|
+
return "tool-calls";
|
|
301
|
+
case "max_tokens":
|
|
302
|
+
return "length";
|
|
303
|
+
default:
|
|
304
|
+
return "unknown";
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/anthropic-prepare-tools.ts
|
|
309
|
+
var import_provider2 = require("@ai-sdk/provider");
|
|
310
|
+
function prepareTools(mode) {
|
|
311
|
+
var _a;
|
|
312
|
+
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
313
|
+
const toolWarnings = [];
|
|
314
|
+
const betas = /* @__PURE__ */ new Set();
|
|
315
|
+
if (tools == null) {
|
|
316
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
317
|
+
}
|
|
318
|
+
const anthropicTools2 = [];
|
|
319
|
+
for (const tool of tools) {
|
|
320
|
+
switch (tool.type) {
|
|
321
|
+
case "function":
|
|
322
|
+
anthropicTools2.push({
|
|
323
|
+
name: tool.name,
|
|
324
|
+
description: tool.description,
|
|
325
|
+
input_schema: tool.parameters
|
|
326
|
+
});
|
|
327
|
+
break;
|
|
328
|
+
case "provider-defined":
|
|
329
|
+
betas.add("computer-use-2024-10-22");
|
|
330
|
+
switch (tool.id) {
|
|
331
|
+
case "anthropic.computer_20241022":
|
|
332
|
+
anthropicTools2.push({
|
|
333
|
+
name: tool.name,
|
|
334
|
+
type: "computer_20241022",
|
|
335
|
+
display_width_px: tool.args.displayWidthPx,
|
|
336
|
+
display_height_px: tool.args.displayHeightPx,
|
|
337
|
+
display_number: tool.args.displayNumber
|
|
338
|
+
});
|
|
339
|
+
break;
|
|
340
|
+
case "anthropic.text_editor_20241022":
|
|
341
|
+
anthropicTools2.push({
|
|
342
|
+
name: tool.name,
|
|
343
|
+
type: "text_editor_20241022"
|
|
344
|
+
});
|
|
345
|
+
break;
|
|
346
|
+
case "anthropic.bash_20241022":
|
|
347
|
+
anthropicTools2.push({
|
|
348
|
+
name: tool.name,
|
|
349
|
+
type: "bash_20241022"
|
|
350
|
+
});
|
|
351
|
+
break;
|
|
352
|
+
default:
|
|
353
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
break;
|
|
357
|
+
default:
|
|
358
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const toolChoice = mode.toolChoice;
|
|
363
|
+
if (toolChoice == null) {
|
|
364
|
+
return {
|
|
365
|
+
tools: anthropicTools2,
|
|
366
|
+
tool_choice: void 0,
|
|
367
|
+
toolWarnings,
|
|
368
|
+
betas
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
const type = toolChoice.type;
|
|
372
|
+
switch (type) {
|
|
373
|
+
case "auto":
|
|
374
|
+
return {
|
|
375
|
+
tools: anthropicTools2,
|
|
376
|
+
tool_choice: { type: "auto" },
|
|
377
|
+
toolWarnings,
|
|
378
|
+
betas
|
|
379
|
+
};
|
|
380
|
+
case "required":
|
|
381
|
+
return {
|
|
382
|
+
tools: anthropicTools2,
|
|
383
|
+
tool_choice: { type: "any" },
|
|
384
|
+
toolWarnings,
|
|
385
|
+
betas
|
|
386
|
+
};
|
|
387
|
+
case "none":
|
|
388
|
+
return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
|
|
389
|
+
case "tool":
|
|
390
|
+
return {
|
|
391
|
+
tools: anthropicTools2,
|
|
392
|
+
tool_choice: { type: "tool", name: toolChoice.toolName },
|
|
393
|
+
toolWarnings,
|
|
394
|
+
betas
|
|
395
|
+
};
|
|
396
|
+
default: {
|
|
397
|
+
const _exhaustiveCheck = type;
|
|
398
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
399
|
+
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/anthropic-messages-language-model.ts
|
|
406
|
+
var AnthropicMessagesLanguageModel = class {
|
|
407
|
+
constructor(modelId, settings, config) {
|
|
408
|
+
this.specificationVersion = "v1";
|
|
409
|
+
this.defaultObjectGenerationMode = "tool";
|
|
410
|
+
this.supportsImageUrls = false;
|
|
411
|
+
this.modelId = modelId;
|
|
412
|
+
this.settings = settings;
|
|
413
|
+
this.config = config;
|
|
414
|
+
}
|
|
415
|
+
get provider() {
|
|
416
|
+
return this.config.provider;
|
|
417
|
+
}
|
|
418
|
+
async getArgs({
|
|
419
|
+
mode,
|
|
420
|
+
prompt,
|
|
421
|
+
maxTokens,
|
|
422
|
+
temperature,
|
|
423
|
+
topP,
|
|
424
|
+
topK,
|
|
425
|
+
frequencyPenalty,
|
|
426
|
+
presencePenalty,
|
|
427
|
+
stopSequences,
|
|
428
|
+
responseFormat,
|
|
429
|
+
seed
|
|
430
|
+
}) {
|
|
431
|
+
var _a;
|
|
432
|
+
const type = mode.type;
|
|
433
|
+
const warnings = [];
|
|
434
|
+
if (frequencyPenalty != null) {
|
|
435
|
+
warnings.push({
|
|
436
|
+
type: "unsupported-setting",
|
|
437
|
+
setting: "frequencyPenalty"
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
if (presencePenalty != null) {
|
|
441
|
+
warnings.push({
|
|
442
|
+
type: "unsupported-setting",
|
|
443
|
+
setting: "presencePenalty"
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
if (seed != null) {
|
|
447
|
+
warnings.push({
|
|
448
|
+
type: "unsupported-setting",
|
|
449
|
+
setting: "seed"
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
if (responseFormat != null && responseFormat.type !== "text") {
|
|
453
|
+
warnings.push({
|
|
454
|
+
type: "unsupported-setting",
|
|
455
|
+
setting: "responseFormat",
|
|
456
|
+
details: "JSON response format is not supported."
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
|
|
460
|
+
prompt,
|
|
461
|
+
cacheControl: (_a = this.settings.cacheControl) != null ? _a : false
|
|
462
|
+
});
|
|
463
|
+
const baseArgs = {
|
|
464
|
+
// model id:
|
|
465
|
+
model: this.modelId,
|
|
466
|
+
// standardized settings:
|
|
467
|
+
max_tokens: maxTokens != null ? maxTokens : 4096,
|
|
468
|
+
// 4096: max model output tokens TODO remove
|
|
469
|
+
temperature,
|
|
470
|
+
top_k: topK,
|
|
471
|
+
top_p: topP,
|
|
472
|
+
stop_sequences: stopSequences,
|
|
473
|
+
// prompt:
|
|
474
|
+
system: messagesPrompt.system,
|
|
475
|
+
messages: messagesPrompt.messages
|
|
476
|
+
};
|
|
477
|
+
switch (type) {
|
|
478
|
+
case "regular": {
|
|
479
|
+
const {
|
|
480
|
+
tools,
|
|
481
|
+
tool_choice,
|
|
482
|
+
toolWarnings,
|
|
483
|
+
betas: toolsBetas
|
|
484
|
+
} = prepareTools(mode);
|
|
485
|
+
return {
|
|
486
|
+
args: { ...baseArgs, tools, tool_choice },
|
|
487
|
+
warnings: [...warnings, ...toolWarnings],
|
|
488
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
case "object-json": {
|
|
492
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
|
493
|
+
functionality: "json-mode object generation"
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
case "object-tool": {
|
|
497
|
+
const { name, description, parameters } = mode.tool;
|
|
498
|
+
return {
|
|
499
|
+
args: {
|
|
500
|
+
...baseArgs,
|
|
501
|
+
tools: [{ name, description, input_schema: parameters }],
|
|
502
|
+
tool_choice: { type: "tool", name }
|
|
503
|
+
},
|
|
504
|
+
warnings,
|
|
505
|
+
betas: messagesBetas
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
default: {
|
|
509
|
+
const _exhaustiveCheck = type;
|
|
510
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
async getHeaders({
|
|
515
|
+
betas,
|
|
516
|
+
headers
|
|
517
|
+
}) {
|
|
518
|
+
if (this.settings.cacheControl) {
|
|
519
|
+
betas.add("prompt-caching-2024-07-31");
|
|
520
|
+
}
|
|
521
|
+
return (0, import_provider_utils3.combineHeaders)(
|
|
522
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
|
523
|
+
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
524
|
+
headers
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
buildRequestUrl(isStreaming) {
|
|
528
|
+
var _a, _b, _c;
|
|
529
|
+
return (_c = (_b = (_a = this.config).buildRequestUrl) == null ? void 0 : _b.call(_a, this.config.baseURL, isStreaming)) != null ? _c : `${this.config.baseURL}/messages`;
|
|
530
|
+
}
|
|
531
|
+
transformRequestBody(args) {
|
|
532
|
+
var _a, _b, _c;
|
|
533
|
+
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
534
|
+
}
|
|
535
|
+
async doGenerate(options) {
|
|
536
|
+
var _a, _b, _c, _d;
|
|
537
|
+
const { args, warnings, betas } = await this.getArgs(options);
|
|
538
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
539
|
+
url: this.buildRequestUrl(false),
|
|
540
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
541
|
+
body: this.transformRequestBody(args),
|
|
542
|
+
failedResponseHandler: anthropicFailedResponseHandler,
|
|
543
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
|
|
544
|
+
anthropicMessagesResponseSchema
|
|
545
|
+
),
|
|
546
|
+
abortSignal: options.abortSignal,
|
|
547
|
+
fetch: this.config.fetch
|
|
548
|
+
});
|
|
549
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
|
550
|
+
let text = "";
|
|
551
|
+
for (const content of response.content) {
|
|
552
|
+
if (content.type === "text") {
|
|
553
|
+
text += content.text;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
let toolCalls = void 0;
|
|
557
|
+
if (response.content.some((content) => content.type === "tool_use")) {
|
|
558
|
+
toolCalls = [];
|
|
559
|
+
for (const content of response.content) {
|
|
560
|
+
if (content.type === "tool_use") {
|
|
561
|
+
toolCalls.push({
|
|
562
|
+
toolCallType: "function",
|
|
563
|
+
toolCallId: content.id,
|
|
564
|
+
toolName: content.name,
|
|
565
|
+
args: JSON.stringify(content.input)
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
text,
|
|
572
|
+
toolCalls,
|
|
573
|
+
finishReason: mapAnthropicStopReason(response.stop_reason),
|
|
574
|
+
usage: {
|
|
575
|
+
promptTokens: response.usage.input_tokens,
|
|
576
|
+
completionTokens: response.usage.output_tokens
|
|
577
|
+
},
|
|
578
|
+
rawCall: { rawPrompt, rawSettings },
|
|
579
|
+
rawResponse: { headers: responseHeaders },
|
|
580
|
+
response: {
|
|
581
|
+
id: (_a = response.id) != null ? _a : void 0,
|
|
582
|
+
modelId: (_b = response.model) != null ? _b : void 0
|
|
583
|
+
},
|
|
584
|
+
warnings,
|
|
585
|
+
providerMetadata: this.settings.cacheControl === true ? {
|
|
586
|
+
anthropic: {
|
|
587
|
+
cacheCreationInputTokens: (_c = response.usage.cache_creation_input_tokens) != null ? _c : null,
|
|
588
|
+
cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
|
|
589
|
+
}
|
|
590
|
+
} : void 0,
|
|
591
|
+
request: { body: JSON.stringify(args) }
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
async doStream(options) {
|
|
595
|
+
const { args, warnings, betas } = await this.getArgs(options);
|
|
596
|
+
const body = { ...args, stream: true };
|
|
597
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
598
|
+
url: this.buildRequestUrl(true),
|
|
599
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
600
|
+
body: this.transformRequestBody(body),
|
|
601
|
+
failedResponseHandler: anthropicFailedResponseHandler,
|
|
602
|
+
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
|
|
603
|
+
anthropicMessagesChunkSchema
|
|
604
|
+
),
|
|
605
|
+
abortSignal: options.abortSignal,
|
|
606
|
+
fetch: this.config.fetch
|
|
607
|
+
});
|
|
608
|
+
const { messages: rawPrompt, ...rawSettings } = args;
|
|
609
|
+
let finishReason = "unknown";
|
|
610
|
+
const usage = {
|
|
611
|
+
promptTokens: Number.NaN,
|
|
612
|
+
completionTokens: Number.NaN
|
|
613
|
+
};
|
|
614
|
+
const toolCallContentBlocks = {};
|
|
615
|
+
let providerMetadata = void 0;
|
|
616
|
+
const self = this;
|
|
617
|
+
return {
|
|
618
|
+
stream: response.pipeThrough(
|
|
619
|
+
new TransformStream({
|
|
620
|
+
transform(chunk, controller) {
|
|
621
|
+
var _a, _b, _c, _d;
|
|
622
|
+
if (!chunk.success) {
|
|
623
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
const value = chunk.value;
|
|
627
|
+
switch (value.type) {
|
|
628
|
+
case "ping": {
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
case "content_block_start": {
|
|
632
|
+
const contentBlockType = value.content_block.type;
|
|
633
|
+
switch (contentBlockType) {
|
|
634
|
+
case "text": {
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
case "tool_use": {
|
|
638
|
+
toolCallContentBlocks[value.index] = {
|
|
639
|
+
toolCallId: value.content_block.id,
|
|
640
|
+
toolName: value.content_block.name,
|
|
641
|
+
jsonText: ""
|
|
642
|
+
};
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
default: {
|
|
646
|
+
const _exhaustiveCheck = contentBlockType;
|
|
647
|
+
throw new Error(
|
|
648
|
+
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
case "content_block_stop": {
|
|
654
|
+
if (toolCallContentBlocks[value.index] != null) {
|
|
655
|
+
const contentBlock = toolCallContentBlocks[value.index];
|
|
656
|
+
controller.enqueue({
|
|
657
|
+
type: "tool-call",
|
|
658
|
+
toolCallType: "function",
|
|
659
|
+
toolCallId: contentBlock.toolCallId,
|
|
660
|
+
toolName: contentBlock.toolName,
|
|
661
|
+
args: contentBlock.jsonText
|
|
662
|
+
});
|
|
663
|
+
delete toolCallContentBlocks[value.index];
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
case "content_block_delta": {
|
|
668
|
+
const deltaType = value.delta.type;
|
|
669
|
+
switch (deltaType) {
|
|
670
|
+
case "text_delta": {
|
|
671
|
+
controller.enqueue({
|
|
672
|
+
type: "text-delta",
|
|
673
|
+
textDelta: value.delta.text
|
|
674
|
+
});
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
case "input_json_delta": {
|
|
678
|
+
const contentBlock = toolCallContentBlocks[value.index];
|
|
679
|
+
controller.enqueue({
|
|
680
|
+
type: "tool-call-delta",
|
|
681
|
+
toolCallType: "function",
|
|
682
|
+
toolCallId: contentBlock.toolCallId,
|
|
683
|
+
toolName: contentBlock.toolName,
|
|
684
|
+
argsTextDelta: value.delta.partial_json
|
|
685
|
+
});
|
|
686
|
+
contentBlock.jsonText += value.delta.partial_json;
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
default: {
|
|
690
|
+
const _exhaustiveCheck = deltaType;
|
|
691
|
+
throw new Error(
|
|
692
|
+
`Unsupported delta type: ${_exhaustiveCheck}`
|
|
693
|
+
);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
case "message_start": {
|
|
698
|
+
usage.promptTokens = value.message.usage.input_tokens;
|
|
699
|
+
usage.completionTokens = value.message.usage.output_tokens;
|
|
700
|
+
if (self.settings.cacheControl === true) {
|
|
701
|
+
providerMetadata = {
|
|
702
|
+
anthropic: {
|
|
703
|
+
cacheCreationInputTokens: (_a = value.message.usage.cache_creation_input_tokens) != null ? _a : null,
|
|
704
|
+
cacheReadInputTokens: (_b = value.message.usage.cache_read_input_tokens) != null ? _b : null
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
controller.enqueue({
|
|
709
|
+
type: "response-metadata",
|
|
710
|
+
id: (_c = value.message.id) != null ? _c : void 0,
|
|
711
|
+
modelId: (_d = value.message.model) != null ? _d : void 0
|
|
712
|
+
});
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
case "message_delta": {
|
|
716
|
+
usage.completionTokens = value.usage.output_tokens;
|
|
717
|
+
finishReason = mapAnthropicStopReason(value.delta.stop_reason);
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
case "message_stop": {
|
|
721
|
+
controller.enqueue({
|
|
722
|
+
type: "finish",
|
|
723
|
+
finishReason,
|
|
724
|
+
usage,
|
|
725
|
+
providerMetadata
|
|
726
|
+
});
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
case "error": {
|
|
730
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
default: {
|
|
734
|
+
const _exhaustiveCheck = value;
|
|
735
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
})
|
|
740
|
+
),
|
|
741
|
+
rawCall: { rawPrompt, rawSettings },
|
|
742
|
+
rawResponse: { headers: responseHeaders },
|
|
743
|
+
warnings,
|
|
744
|
+
request: { body: JSON.stringify(body) }
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
var anthropicMessagesResponseSchema = import_zod2.z.object({
|
|
749
|
+
type: import_zod2.z.literal("message"),
|
|
750
|
+
id: import_zod2.z.string().nullish(),
|
|
751
|
+
model: import_zod2.z.string().nullish(),
|
|
752
|
+
content: import_zod2.z.array(
|
|
753
|
+
import_zod2.z.discriminatedUnion("type", [
|
|
754
|
+
import_zod2.z.object({
|
|
755
|
+
type: import_zod2.z.literal("text"),
|
|
756
|
+
text: import_zod2.z.string()
|
|
757
|
+
}),
|
|
758
|
+
import_zod2.z.object({
|
|
759
|
+
type: import_zod2.z.literal("tool_use"),
|
|
760
|
+
id: import_zod2.z.string(),
|
|
761
|
+
name: import_zod2.z.string(),
|
|
762
|
+
input: import_zod2.z.unknown()
|
|
763
|
+
})
|
|
764
|
+
])
|
|
765
|
+
),
|
|
766
|
+
stop_reason: import_zod2.z.string().nullish(),
|
|
767
|
+
usage: import_zod2.z.object({
|
|
768
|
+
input_tokens: import_zod2.z.number(),
|
|
769
|
+
output_tokens: import_zod2.z.number(),
|
|
770
|
+
cache_creation_input_tokens: import_zod2.z.number().nullish(),
|
|
771
|
+
cache_read_input_tokens: import_zod2.z.number().nullish()
|
|
772
|
+
})
|
|
773
|
+
});
|
|
774
|
+
var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
|
|
775
|
+
import_zod2.z.object({
|
|
776
|
+
type: import_zod2.z.literal("message_start"),
|
|
777
|
+
message: import_zod2.z.object({
|
|
778
|
+
id: import_zod2.z.string().nullish(),
|
|
779
|
+
model: import_zod2.z.string().nullish(),
|
|
780
|
+
usage: import_zod2.z.object({
|
|
781
|
+
input_tokens: import_zod2.z.number(),
|
|
782
|
+
output_tokens: import_zod2.z.number(),
|
|
783
|
+
cache_creation_input_tokens: import_zod2.z.number().nullish(),
|
|
784
|
+
cache_read_input_tokens: import_zod2.z.number().nullish()
|
|
785
|
+
})
|
|
786
|
+
})
|
|
787
|
+
}),
|
|
788
|
+
import_zod2.z.object({
|
|
789
|
+
type: import_zod2.z.literal("content_block_start"),
|
|
790
|
+
index: import_zod2.z.number(),
|
|
791
|
+
content_block: import_zod2.z.discriminatedUnion("type", [
|
|
792
|
+
import_zod2.z.object({
|
|
793
|
+
type: import_zod2.z.literal("text"),
|
|
794
|
+
text: import_zod2.z.string()
|
|
795
|
+
}),
|
|
796
|
+
import_zod2.z.object({
|
|
797
|
+
type: import_zod2.z.literal("tool_use"),
|
|
798
|
+
id: import_zod2.z.string(),
|
|
799
|
+
name: import_zod2.z.string()
|
|
800
|
+
})
|
|
801
|
+
])
|
|
802
|
+
}),
|
|
803
|
+
import_zod2.z.object({
|
|
804
|
+
type: import_zod2.z.literal("content_block_delta"),
|
|
805
|
+
index: import_zod2.z.number(),
|
|
806
|
+
delta: import_zod2.z.discriminatedUnion("type", [
|
|
807
|
+
import_zod2.z.object({
|
|
808
|
+
type: import_zod2.z.literal("input_json_delta"),
|
|
809
|
+
partial_json: import_zod2.z.string()
|
|
810
|
+
}),
|
|
811
|
+
import_zod2.z.object({
|
|
812
|
+
type: import_zod2.z.literal("text_delta"),
|
|
813
|
+
text: import_zod2.z.string()
|
|
814
|
+
})
|
|
815
|
+
])
|
|
816
|
+
}),
|
|
817
|
+
import_zod2.z.object({
|
|
818
|
+
type: import_zod2.z.literal("content_block_stop"),
|
|
819
|
+
index: import_zod2.z.number()
|
|
820
|
+
}),
|
|
821
|
+
import_zod2.z.object({
|
|
822
|
+
type: import_zod2.z.literal("error"),
|
|
823
|
+
error: import_zod2.z.object({
|
|
824
|
+
type: import_zod2.z.string(),
|
|
825
|
+
message: import_zod2.z.string()
|
|
826
|
+
})
|
|
827
|
+
}),
|
|
828
|
+
import_zod2.z.object({
|
|
829
|
+
type: import_zod2.z.literal("message_delta"),
|
|
830
|
+
delta: import_zod2.z.object({ stop_reason: import_zod2.z.string().nullish() }),
|
|
831
|
+
usage: import_zod2.z.object({ output_tokens: import_zod2.z.number() })
|
|
832
|
+
}),
|
|
833
|
+
import_zod2.z.object({
|
|
834
|
+
type: import_zod2.z.literal("message_stop")
|
|
835
|
+
}),
|
|
836
|
+
import_zod2.z.object({
|
|
837
|
+
type: import_zod2.z.literal("ping")
|
|
838
|
+
})
|
|
839
|
+
]);
|
|
840
|
+
|
|
841
|
+
// src/anthropic-tools.ts
|
|
842
|
+
var import_zod3 = require("zod");
|
|
843
|
+
var Bash20241022Parameters = import_zod3.z.object({
|
|
844
|
+
command: import_zod3.z.string(),
|
|
845
|
+
restart: import_zod3.z.boolean().optional()
|
|
846
|
+
});
|
|
847
|
+
function bashTool_20241022(options = {}) {
|
|
848
|
+
return {
|
|
849
|
+
type: "provider-defined",
|
|
850
|
+
id: "anthropic.bash_20241022",
|
|
851
|
+
args: {},
|
|
852
|
+
parameters: Bash20241022Parameters,
|
|
853
|
+
execute: options.execute,
|
|
854
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
var TextEditor20241022Parameters = import_zod3.z.object({
|
|
858
|
+
command: import_zod3.z.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
859
|
+
path: import_zod3.z.string(),
|
|
860
|
+
file_text: import_zod3.z.string().optional(),
|
|
861
|
+
insert_line: import_zod3.z.number().int().optional(),
|
|
862
|
+
new_str: import_zod3.z.string().optional(),
|
|
863
|
+
old_str: import_zod3.z.string().optional(),
|
|
864
|
+
view_range: import_zod3.z.array(import_zod3.z.number().int()).optional()
|
|
865
|
+
});
|
|
866
|
+
function textEditorTool_20241022(options = {}) {
|
|
867
|
+
return {
|
|
868
|
+
type: "provider-defined",
|
|
869
|
+
id: "anthropic.text_editor_20241022",
|
|
870
|
+
args: {},
|
|
871
|
+
parameters: TextEditor20241022Parameters,
|
|
872
|
+
execute: options.execute,
|
|
873
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
var Computer20241022Parameters = import_zod3.z.object({
|
|
877
|
+
action: import_zod3.z.enum([
|
|
878
|
+
"key",
|
|
879
|
+
"type",
|
|
880
|
+
"mouse_move",
|
|
881
|
+
"left_click",
|
|
882
|
+
"left_click_drag",
|
|
883
|
+
"right_click",
|
|
884
|
+
"middle_click",
|
|
885
|
+
"double_click",
|
|
886
|
+
"screenshot",
|
|
887
|
+
"cursor_position"
|
|
888
|
+
]),
|
|
889
|
+
coordinate: import_zod3.z.array(import_zod3.z.number().int()).optional(),
|
|
890
|
+
text: import_zod3.z.string().optional()
|
|
891
|
+
});
|
|
892
|
+
function computerTool_20241022(options) {
|
|
893
|
+
return {
|
|
894
|
+
type: "provider-defined",
|
|
895
|
+
id: "anthropic.computer_20241022",
|
|
896
|
+
args: {
|
|
897
|
+
displayWidthPx: options.displayWidthPx,
|
|
898
|
+
displayHeightPx: options.displayHeightPx,
|
|
899
|
+
displayNumber: options.displayNumber
|
|
900
|
+
},
|
|
901
|
+
parameters: Computer20241022Parameters,
|
|
902
|
+
execute: options.execute,
|
|
903
|
+
experimental_toToolResultContent: options.experimental_toToolResultContent
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
var anthropicTools = {
|
|
907
|
+
bash_20241022: bashTool_20241022,
|
|
908
|
+
textEditor_20241022: textEditorTool_20241022,
|
|
909
|
+
computer_20241022: computerTool_20241022
|
|
910
|
+
};
|
|
911
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
912
|
+
0 && (module.exports = {
|
|
913
|
+
AnthropicMessagesLanguageModel,
|
|
914
|
+
anthropicTools
|
|
915
|
+
});
|
|
916
|
+
//# sourceMappingURL=index.js.map
|