@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.
package/dist/index.mjs CHANGED
@@ -40,13 +40,15 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
40
40
  import {
41
41
  UnsupportedFunctionalityError
42
42
  } from "@ai-sdk/provider";
43
- function prepareTools(mode) {
44
- var _a;
45
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
43
+ function prepareTools({
44
+ tools,
45
+ toolChoice
46
+ }) {
47
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
46
48
  const toolWarnings = [];
47
49
  const betas = /* @__PURE__ */ new Set();
48
50
  if (tools == null) {
49
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
51
+ return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
50
52
  }
51
53
  const anthropicTools2 = [];
52
54
  for (const tool of tools) {
@@ -118,11 +120,10 @@ function prepareTools(mode) {
118
120
  break;
119
121
  }
120
122
  }
121
- const toolChoice = mode.toolChoice;
122
123
  if (toolChoice == null) {
123
124
  return {
124
125
  tools: anthropicTools2,
125
- tool_choice: void 0,
126
+ toolChoice: void 0,
126
127
  toolWarnings,
127
128
  betas
128
129
  };
@@ -132,30 +133,30 @@ function prepareTools(mode) {
132
133
  case "auto":
133
134
  return {
134
135
  tools: anthropicTools2,
135
- tool_choice: { type: "auto" },
136
+ toolChoice: { type: "auto" },
136
137
  toolWarnings,
137
138
  betas
138
139
  };
139
140
  case "required":
140
141
  return {
141
142
  tools: anthropicTools2,
142
- tool_choice: { type: "any" },
143
+ toolChoice: { type: "any" },
143
144
  toolWarnings,
144
145
  betas
145
146
  };
146
147
  case "none":
147
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
148
+ return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
148
149
  case "tool":
149
150
  return {
150
151
  tools: anthropicTools2,
151
- tool_choice: { type: "tool", name: toolChoice.toolName },
152
+ toolChoice: { type: "tool", name: toolChoice.toolName },
152
153
  toolWarnings,
153
154
  betas
154
155
  };
155
156
  default: {
156
157
  const _exhaustiveCheck = type;
157
158
  throw new UnsupportedFunctionalityError({
158
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
159
+ functionality: `tool choice type: ${_exhaustiveCheck}`
159
160
  });
160
161
  }
161
162
  }
@@ -165,13 +166,12 @@ function prepareTools(mode) {
165
166
  import {
166
167
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
167
168
  } from "@ai-sdk/provider";
168
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
169
169
  function convertToAnthropicMessagesPrompt({
170
170
  prompt,
171
171
  sendReasoning,
172
172
  warnings
173
173
  }) {
174
- var _a, _b, _c, _d;
174
+ var _a, _b, _c;
175
175
  const betas = /* @__PURE__ */ new Set();
176
176
  const blocks = groupIntoBlocks(prompt);
177
177
  let system = void 0;
@@ -193,10 +193,10 @@ function convertToAnthropicMessagesPrompt({
193
193
  functionality: "Multiple system messages that are separated by user/assistant messages"
194
194
  });
195
195
  }
196
- system = block.messages.map(({ content, providerMetadata }) => ({
196
+ system = block.messages.map(({ content, providerOptions }) => ({
197
197
  type: "text",
198
198
  text: content,
199
- cache_control: getCacheControl(providerMetadata)
199
+ cache_control: getCacheControl(providerOptions)
200
200
  }));
201
201
  break;
202
202
  }
@@ -209,7 +209,7 @@ function convertToAnthropicMessagesPrompt({
209
209
  for (let j = 0; j < content.length; j++) {
210
210
  const part = content[j];
211
211
  const isLastPart = j === content.length - 1;
212
- const cacheControl = (_a = getCacheControl(part.providerMetadata)) != null ? _a : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
212
+ const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
213
213
  switch (part.type) {
214
214
  case "text": {
215
215
  anthropicContent.push({
@@ -219,42 +219,39 @@ function convertToAnthropicMessagesPrompt({
219
219
  });
220
220
  break;
221
221
  }
222
- case "image": {
223
- anthropicContent.push({
224
- type: "image",
225
- source: part.image instanceof URL ? {
226
- type: "url",
227
- url: part.image.toString()
228
- } : {
229
- type: "base64",
230
- media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
231
- data: convertUint8ArrayToBase64(part.image)
232
- },
233
- cache_control: cacheControl
234
- });
235
- break;
236
- }
237
222
  case "file": {
238
- if (part.data instanceof URL) {
239
- throw new UnsupportedFunctionalityError2({
240
- functionality: "Image URLs in user messages"
223
+ if (part.mediaType.startsWith("image/")) {
224
+ anthropicContent.push({
225
+ type: "image",
226
+ source: part.data instanceof URL ? {
227
+ type: "url",
228
+ url: part.data.toString()
229
+ } : {
230
+ type: "base64",
231
+ media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
232
+ data: part.data
233
+ },
234
+ cache_control: cacheControl
241
235
  });
242
- }
243
- if (part.mimeType !== "application/pdf") {
236
+ } else if (part.mediaType === "application/pdf") {
237
+ betas.add("pdfs-2024-09-25");
238
+ anthropicContent.push({
239
+ type: "document",
240
+ source: part.data instanceof URL ? {
241
+ type: "url",
242
+ url: part.data.toString()
243
+ } : {
244
+ type: "base64",
245
+ media_type: "application/pdf",
246
+ data: part.data
247
+ },
248
+ cache_control: cacheControl
249
+ });
250
+ } else {
244
251
  throw new UnsupportedFunctionalityError2({
245
- functionality: "Non-PDF files in user messages"
252
+ functionality: `media type: ${part.mediaType}`
246
253
  });
247
254
  }
248
- betas.add("pdfs-2024-09-25");
249
- anthropicContent.push({
250
- type: "document",
251
- source: {
252
- type: "base64",
253
- media_type: "application/pdf",
254
- data: part.data
255
- },
256
- cache_control: cacheControl
257
- });
258
255
  break;
259
256
  }
260
257
  }
@@ -265,7 +262,7 @@ function convertToAnthropicMessagesPrompt({
265
262
  for (let i2 = 0; i2 < content.length; i2++) {
266
263
  const part = content[i2];
267
264
  const isLastPart = i2 === content.length - 1;
268
- const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
265
+ const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
269
266
  const toolResultContent = part.content != null ? part.content.map((part2) => {
270
267
  var _a2;
271
268
  switch (part2.type) {
@@ -280,7 +277,7 @@ function convertToAnthropicMessagesPrompt({
280
277
  type: "image",
281
278
  source: {
282
279
  type: "base64",
283
- media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
280
+ media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
284
281
  data: part2.data
285
282
  },
286
283
  cache_control: void 0
@@ -315,7 +312,7 @@ function convertToAnthropicMessagesPrompt({
315
312
  for (let k = 0; k < content.length; k++) {
316
313
  const part = content[k];
317
314
  const isLastContentPart = k === content.length - 1;
318
- const cacheControl = (_d = getCacheControl(part.providerMetadata)) != null ? _d : isLastContentPart ? getCacheControl(message.providerMetadata) : void 0;
315
+ const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
319
316
  switch (part.type) {
320
317
  case "text": {
321
318
  anthropicContent.push({
@@ -372,7 +369,7 @@ function convertToAnthropicMessagesPrompt({
372
369
  }
373
370
  default: {
374
371
  const _exhaustiveCheck = type;
375
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
372
+ throw new Error(`content type: ${_exhaustiveCheck}`);
376
373
  }
377
374
  }
378
375
  }
@@ -452,6 +449,9 @@ var AnthropicMessagesLanguageModel = class {
452
449
  this.settings = settings;
453
450
  this.config = config;
454
451
  }
452
+ supportsUrl(url) {
453
+ return url.protocol === "https:";
454
+ }
455
455
  get provider() {
456
456
  return this.config.provider;
457
457
  }
@@ -459,7 +459,6 @@ var AnthropicMessagesLanguageModel = class {
459
459
  return this.config.supportsImageUrls;
460
460
  }
461
461
  async getArgs({
462
- mode,
463
462
  prompt,
464
463
  maxTokens = 4096,
465
464
  // 4096: max model output tokens TODO update default in v5
@@ -471,10 +470,11 @@ var AnthropicMessagesLanguageModel = class {
471
470
  stopSequences,
472
471
  responseFormat,
473
472
  seed,
474
- providerMetadata: providerOptions
473
+ tools,
474
+ toolChoice,
475
+ providerOptions
475
476
  }) {
476
477
  var _a, _b, _c;
477
- const type = mode.type;
478
478
  const warnings = [];
479
479
  if (frequencyPenalty != null) {
480
480
  warnings.push({
@@ -562,42 +562,21 @@ var AnthropicMessagesLanguageModel = class {
562
562
  }
563
563
  baseArgs.max_tokens = maxTokens + thinkingBudget;
564
564
  }
565
- switch (type) {
566
- case "regular": {
567
- const {
568
- tools,
569
- tool_choice,
570
- toolWarnings,
571
- betas: toolsBetas
572
- } = prepareTools(mode);
573
- return {
574
- args: { ...baseArgs, tools, tool_choice },
575
- warnings: [...warnings, ...toolWarnings],
576
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
577
- };
578
- }
579
- case "object-json": {
580
- throw new UnsupportedFunctionalityError3({
581
- functionality: "json-mode object generation"
582
- });
583
- }
584
- case "object-tool": {
585
- const { name, description, parameters } = mode.tool;
586
- return {
587
- args: {
588
- ...baseArgs,
589
- tools: [{ name, description, input_schema: parameters }],
590
- tool_choice: { type: "tool", name }
591
- },
592
- warnings,
593
- betas: messagesBetas
594
- };
595
- }
596
- default: {
597
- const _exhaustiveCheck = type;
598
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
599
- }
600
- }
565
+ const {
566
+ tools: anthropicTools2,
567
+ toolChoice: anthropicToolChoice,
568
+ toolWarnings,
569
+ betas: toolsBetas
570
+ } = prepareTools({ tools, toolChoice });
571
+ return {
572
+ args: {
573
+ ...baseArgs,
574
+ tools: anthropicTools2,
575
+ tool_choice: anthropicToolChoice
576
+ },
577
+ warnings: [...warnings, ...toolWarnings],
578
+ betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
579
+ };
601
580
  }
602
581
  async getHeaders({
603
582
  betas,