@ai-sdk/anthropic 2.0.0-canary.1 → 2.0.0-canary.3

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.
@@ -31,13 +31,15 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
31
31
  import {
32
32
  UnsupportedFunctionalityError
33
33
  } from "@ai-sdk/provider";
34
- function prepareTools(mode) {
35
- var _a;
36
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
34
+ function prepareTools({
35
+ tools,
36
+ toolChoice
37
+ }) {
38
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
37
39
  const toolWarnings = [];
38
40
  const betas = /* @__PURE__ */ new Set();
39
41
  if (tools == null) {
40
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
42
+ return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
41
43
  }
42
44
  const anthropicTools2 = [];
43
45
  for (const tool of tools) {
@@ -109,11 +111,10 @@ function prepareTools(mode) {
109
111
  break;
110
112
  }
111
113
  }
112
- const toolChoice = mode.toolChoice;
113
114
  if (toolChoice == null) {
114
115
  return {
115
116
  tools: anthropicTools2,
116
- tool_choice: void 0,
117
+ toolChoice: void 0,
117
118
  toolWarnings,
118
119
  betas
119
120
  };
@@ -123,30 +124,30 @@ function prepareTools(mode) {
123
124
  case "auto":
124
125
  return {
125
126
  tools: anthropicTools2,
126
- tool_choice: { type: "auto" },
127
+ toolChoice: { type: "auto" },
127
128
  toolWarnings,
128
129
  betas
129
130
  };
130
131
  case "required":
131
132
  return {
132
133
  tools: anthropicTools2,
133
- tool_choice: { type: "any" },
134
+ toolChoice: { type: "any" },
134
135
  toolWarnings,
135
136
  betas
136
137
  };
137
138
  case "none":
138
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
139
+ return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
139
140
  case "tool":
140
141
  return {
141
142
  tools: anthropicTools2,
142
- tool_choice: { type: "tool", name: toolChoice.toolName },
143
+ toolChoice: { type: "tool", name: toolChoice.toolName },
143
144
  toolWarnings,
144
145
  betas
145
146
  };
146
147
  default: {
147
148
  const _exhaustiveCheck = type;
148
149
  throw new UnsupportedFunctionalityError({
149
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
150
+ functionality: `tool choice type: ${_exhaustiveCheck}`
150
151
  });
151
152
  }
152
153
  }
@@ -156,13 +157,12 @@ function prepareTools(mode) {
156
157
  import {
157
158
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
158
159
  } from "@ai-sdk/provider";
159
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
160
160
  function convertToAnthropicMessagesPrompt({
161
161
  prompt,
162
162
  sendReasoning,
163
163
  warnings
164
164
  }) {
165
- var _a, _b, _c, _d;
165
+ var _a, _b, _c;
166
166
  const betas = /* @__PURE__ */ new Set();
167
167
  const blocks = groupIntoBlocks(prompt);
168
168
  let system = void 0;
@@ -184,10 +184,10 @@ function convertToAnthropicMessagesPrompt({
184
184
  functionality: "Multiple system messages that are separated by user/assistant messages"
185
185
  });
186
186
  }
187
- system = block.messages.map(({ content, providerMetadata }) => ({
187
+ system = block.messages.map(({ content, providerOptions }) => ({
188
188
  type: "text",
189
189
  text: content,
190
- cache_control: getCacheControl(providerMetadata)
190
+ cache_control: getCacheControl(providerOptions)
191
191
  }));
192
192
  break;
193
193
  }
@@ -200,7 +200,7 @@ function convertToAnthropicMessagesPrompt({
200
200
  for (let j = 0; j < content.length; j++) {
201
201
  const part = content[j];
202
202
  const isLastPart = j === content.length - 1;
203
- const cacheControl = (_a = getCacheControl(part.providerMetadata)) != null ? _a : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
203
+ const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
204
204
  switch (part.type) {
205
205
  case "text": {
206
206
  anthropicContent.push({
@@ -210,42 +210,39 @@ function convertToAnthropicMessagesPrompt({
210
210
  });
211
211
  break;
212
212
  }
213
- case "image": {
214
- anthropicContent.push({
215
- type: "image",
216
- source: part.image instanceof URL ? {
217
- type: "url",
218
- url: part.image.toString()
219
- } : {
220
- type: "base64",
221
- media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
222
- data: convertUint8ArrayToBase64(part.image)
223
- },
224
- cache_control: cacheControl
225
- });
226
- break;
227
- }
228
213
  case "file": {
229
- if (part.data instanceof URL) {
230
- throw new UnsupportedFunctionalityError2({
231
- functionality: "Image URLs in user messages"
214
+ if (part.mediaType.startsWith("image/")) {
215
+ anthropicContent.push({
216
+ type: "image",
217
+ source: part.data instanceof URL ? {
218
+ type: "url",
219
+ url: part.data.toString()
220
+ } : {
221
+ type: "base64",
222
+ media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
223
+ data: part.data
224
+ },
225
+ cache_control: cacheControl
232
226
  });
233
- }
234
- if (part.mimeType !== "application/pdf") {
227
+ } else if (part.mediaType === "application/pdf") {
228
+ betas.add("pdfs-2024-09-25");
229
+ anthropicContent.push({
230
+ type: "document",
231
+ source: part.data instanceof URL ? {
232
+ type: "url",
233
+ url: part.data.toString()
234
+ } : {
235
+ type: "base64",
236
+ media_type: "application/pdf",
237
+ data: part.data
238
+ },
239
+ cache_control: cacheControl
240
+ });
241
+ } else {
235
242
  throw new UnsupportedFunctionalityError2({
236
- functionality: "Non-PDF files in user messages"
243
+ functionality: `media type: ${part.mediaType}`
237
244
  });
238
245
  }
239
- betas.add("pdfs-2024-09-25");
240
- anthropicContent.push({
241
- type: "document",
242
- source: {
243
- type: "base64",
244
- media_type: "application/pdf",
245
- data: part.data
246
- },
247
- cache_control: cacheControl
248
- });
249
246
  break;
250
247
  }
251
248
  }
@@ -256,7 +253,7 @@ function convertToAnthropicMessagesPrompt({
256
253
  for (let i2 = 0; i2 < content.length; i2++) {
257
254
  const part = content[i2];
258
255
  const isLastPart = i2 === content.length - 1;
259
- const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
256
+ const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
260
257
  const toolResultContent = part.content != null ? part.content.map((part2) => {
261
258
  var _a2;
262
259
  switch (part2.type) {
@@ -271,7 +268,7 @@ function convertToAnthropicMessagesPrompt({
271
268
  type: "image",
272
269
  source: {
273
270
  type: "base64",
274
- media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
271
+ media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
275
272
  data: part2.data
276
273
  },
277
274
  cache_control: void 0
@@ -306,7 +303,7 @@ function convertToAnthropicMessagesPrompt({
306
303
  for (let k = 0; k < content.length; k++) {
307
304
  const part = content[k];
308
305
  const isLastContentPart = k === content.length - 1;
309
- const cacheControl = (_d = getCacheControl(part.providerMetadata)) != null ? _d : isLastContentPart ? getCacheControl(message.providerMetadata) : void 0;
306
+ const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
310
307
  switch (part.type) {
311
308
  case "text": {
312
309
  anthropicContent.push({
@@ -363,7 +360,7 @@ function convertToAnthropicMessagesPrompt({
363
360
  }
364
361
  default: {
365
362
  const _exhaustiveCheck = type;
366
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
363
+ throw new Error(`content type: ${_exhaustiveCheck}`);
367
364
  }
368
365
  }
369
366
  }
@@ -443,6 +440,9 @@ var AnthropicMessagesLanguageModel = class {
443
440
  this.settings = settings;
444
441
  this.config = config;
445
442
  }
443
+ supportsUrl(url) {
444
+ return url.protocol === "https:";
445
+ }
446
446
  get provider() {
447
447
  return this.config.provider;
448
448
  }
@@ -450,7 +450,6 @@ var AnthropicMessagesLanguageModel = class {
450
450
  return this.config.supportsImageUrls;
451
451
  }
452
452
  async getArgs({
453
- mode,
454
453
  prompt,
455
454
  maxTokens = 4096,
456
455
  // 4096: max model output tokens TODO update default in v5
@@ -462,10 +461,11 @@ var AnthropicMessagesLanguageModel = class {
462
461
  stopSequences,
463
462
  responseFormat,
464
463
  seed,
465
- providerMetadata: providerOptions
464
+ tools,
465
+ toolChoice,
466
+ providerOptions
466
467
  }) {
467
468
  var _a, _b, _c;
468
- const type = mode.type;
469
469
  const warnings = [];
470
470
  if (frequencyPenalty != null) {
471
471
  warnings.push({
@@ -553,42 +553,21 @@ var AnthropicMessagesLanguageModel = class {
553
553
  }
554
554
  baseArgs.max_tokens = maxTokens + thinkingBudget;
555
555
  }
556
- switch (type) {
557
- case "regular": {
558
- const {
559
- tools,
560
- tool_choice,
561
- toolWarnings,
562
- betas: toolsBetas
563
- } = prepareTools(mode);
564
- return {
565
- args: { ...baseArgs, tools, tool_choice },
566
- warnings: [...warnings, ...toolWarnings],
567
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
568
- };
569
- }
570
- case "object-json": {
571
- throw new UnsupportedFunctionalityError3({
572
- functionality: "json-mode object generation"
573
- });
574
- }
575
- case "object-tool": {
576
- const { name, description, parameters } = mode.tool;
577
- return {
578
- args: {
579
- ...baseArgs,
580
- tools: [{ name, description, input_schema: parameters }],
581
- tool_choice: { type: "tool", name }
582
- },
583
- warnings,
584
- betas: messagesBetas
585
- };
586
- }
587
- default: {
588
- const _exhaustiveCheck = type;
589
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
590
- }
591
- }
556
+ const {
557
+ tools: anthropicTools2,
558
+ toolChoice: anthropicToolChoice,
559
+ toolWarnings,
560
+ betas: toolsBetas
561
+ } = prepareTools({ tools, toolChoice });
562
+ return {
563
+ args: {
564
+ ...baseArgs,
565
+ tools: anthropicTools2,
566
+ tool_choice: anthropicToolChoice
567
+ },
568
+ warnings: [...warnings, ...toolWarnings],
569
+ betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
570
+ };
592
571
  }
593
572
  async getHeaders({
594
573
  betas,