@ai-sdk/anthropic 4.0.0-beta.40 → 4.0.0-beta.41

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.
@@ -109,7 +109,7 @@ declare const anthropicTools: {
109
109
  type: "code_execution_output";
110
110
  file_id: string;
111
111
  }>;
112
- }, {}, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
112
+ }, {}, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
113
113
  code: string;
114
114
  }, {
115
115
  type: "code_execution_result";
@@ -193,7 +193,7 @@ declare const anthropicTools: {
193
193
  new_start: number | null;
194
194
  old_lines: number | null;
195
195
  old_start: number | null;
196
- }, {}, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
196
+ }, {}, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
197
197
  type: "programmatic-tool-call";
198
198
  code: string;
199
199
  } | {
@@ -339,7 +339,7 @@ declare const anthropicTools: {
339
339
  new_start: number | null;
340
340
  old_lines: number | null;
341
341
  old_start: number | null;
342
- }, {}, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
342
+ }, {}, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
343
343
  type: "programmatic-tool-call";
344
344
  code: string;
345
345
  } | {
@@ -592,7 +592,7 @@ declare const anthropicTools: {
592
592
  view_range?: number[];
593
593
  }, {
594
594
  maxCharacters?: number;
595
- }, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
595
+ }, {}>>[0]) => _ai_sdk_provider_utils.ProviderDefinedTool<{
596
596
  command: "view" | "create" | "str_replace" | "insert";
597
597
  path: string;
598
598
  file_text?: string;
@@ -641,7 +641,7 @@ declare const anthropicTools: {
641
641
  enabled: boolean;
642
642
  };
643
643
  maxContentTokens?: number;
644
- }, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
644
+ }, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
645
645
  url: string;
646
646
  }, {
647
647
  type: "web_fetch_result";
@@ -703,7 +703,7 @@ declare const anthropicTools: {
703
703
  enabled: boolean;
704
704
  };
705
705
  maxContentTokens?: number;
706
- }, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
706
+ }, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
707
707
  url: string;
708
708
  }, {
709
709
  type: "web_fetch_result";
@@ -753,7 +753,7 @@ declare const anthropicTools: {
753
753
  country?: string;
754
754
  timezone?: string;
755
755
  };
756
- }, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
756
+ }, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
757
757
  query: string;
758
758
  }, {
759
759
  type: "web_search_result";
@@ -789,7 +789,7 @@ declare const anthropicTools: {
789
789
  country?: string;
790
790
  timezone?: string;
791
791
  };
792
- }, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
792
+ }, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
793
793
  query: string;
794
794
  }, {
795
795
  type: "web_search_result";
@@ -817,7 +817,7 @@ declare const anthropicTools: {
817
817
  }, {
818
818
  type: "tool_reference";
819
819
  toolName: string;
820
- }[], {}, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
820
+ }[], {}, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
821
821
  pattern: string;
822
822
  limit?: number;
823
823
  }, {
@@ -843,7 +843,7 @@ declare const anthropicTools: {
843
843
  }, {
844
844
  type: "tool_reference";
845
845
  toolName: string;
846
- }[], {}, {}>>[0]) => _ai_sdk_provider_utils.Tool<{
846
+ }[], {}, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{
847
847
  query: string;
848
848
  limit?: number;
849
849
  }, {
@@ -44,7 +44,10 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
44
44
  });
45
45
 
46
46
  // src/anthropic-api.ts
47
- import { lazySchema as lazySchema2, zodSchema as zodSchema2 } from "@ai-sdk/provider-utils";
47
+ import {
48
+ lazySchema as lazySchema2,
49
+ zodSchema as zodSchema2
50
+ } from "@ai-sdk/provider-utils";
48
51
  import { z as z2 } from "zod/v4";
49
52
  var anthropicResponseSchema = lazySchema2(
50
53
  () => zodSchema2(
@@ -1055,9 +1058,12 @@ var CacheControlValidator = class {
1055
1058
  };
1056
1059
 
1057
1060
  // src/tool/text-editor_20250728.ts
1058
- import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
1061
+ import {
1062
+ createProviderDefinedToolFactory,
1063
+ lazySchema as lazySchema3,
1064
+ zodSchema as zodSchema3
1065
+ } from "@ai-sdk/provider-utils";
1059
1066
  import { z as z4 } from "zod/v4";
1060
- import { lazySchema as lazySchema3, zodSchema as zodSchema3 } from "@ai-sdk/provider-utils";
1061
1067
  var textEditor_20250728ArgsSchema = lazySchema3(
1062
1068
  () => zodSchema3(
1063
1069
  z4.object({
@@ -1706,8 +1712,9 @@ import {
1706
1712
  import {
1707
1713
  convertBase64ToUint8Array,
1708
1714
  convertToBase64,
1709
- isProviderReference,
1715
+ getTopLevelMediaType,
1710
1716
  parseProviderOptions,
1717
+ resolveFullMediaType,
1711
1718
  resolveProviderReference,
1712
1719
  validateTypes as validateTypes2,
1713
1720
  isNonNullable
@@ -2035,30 +2042,11 @@ var toolSearchRegex_20251119 = (args = {}) => {
2035
2042
  };
2036
2043
 
2037
2044
  // src/convert-to-anthropic-prompt.ts
2038
- function convertToString(data) {
2045
+ function convertBytesDataToString(data) {
2039
2046
  if (typeof data === "string") {
2040
2047
  return new TextDecoder().decode(convertBase64ToUint8Array(data));
2041
2048
  }
2042
- if (data instanceof Uint8Array) {
2043
- return new TextDecoder().decode(data);
2044
- }
2045
- if (data instanceof URL) {
2046
- throw new UnsupportedFunctionalityError2({
2047
- functionality: "URL-based text documents are not supported for citations"
2048
- });
2049
- }
2050
- throw new UnsupportedFunctionalityError2({
2051
- functionality: `unsupported data type for text documents: ${typeof data}`
2052
- });
2053
- }
2054
- function isUrlData(data) {
2055
- return data instanceof URL || isUrlString(data);
2056
- }
2057
- function isUrlString(data) {
2058
- return typeof data === "string" && /^https?:\/\//i.test(data);
2059
- }
2060
- function getUrlString(data) {
2061
- return data instanceof URL ? data.toString() : data;
2049
+ return new TextDecoder().decode(data);
2062
2050
  }
2063
2051
  async function convertToAnthropicPrompt({
2064
2052
  prompt,
@@ -2067,7 +2055,7 @@ async function convertToAnthropicPrompt({
2067
2055
  cacheControlValidator,
2068
2056
  toolNameMapping
2069
2057
  }) {
2070
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
2058
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t;
2071
2059
  const betas = /* @__PURE__ */ new Set();
2072
2060
  const blocks = groupIntoBlocks(prompt);
2073
2061
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -2140,91 +2128,131 @@ async function convertToAnthropicPrompt({
2140
2128
  break;
2141
2129
  }
2142
2130
  case "file": {
2143
- if (isProviderReference(part.data)) {
2144
- const fileId = resolveProviderReference({
2145
- reference: part.data,
2146
- provider: "anthropic"
2147
- });
2148
- betas.add("files-api-2025-04-14");
2149
- if (part.mediaType.startsWith("image/")) {
2150
- anthropicContent.push({
2151
- type: "image",
2152
- source: { type: "file", file_id: fileId },
2153
- cache_control: cacheControl
2131
+ switch (part.data.type) {
2132
+ case "reference": {
2133
+ const fileId = resolveProviderReference({
2134
+ reference: part.data.reference,
2135
+ provider: "anthropic"
2154
2136
  });
2155
- } else {
2137
+ betas.add("files-api-2025-04-14");
2138
+ if (getTopLevelMediaType(part.mediaType) === "image") {
2139
+ anthropicContent.push({
2140
+ type: "image",
2141
+ source: { type: "file", file_id: fileId },
2142
+ cache_control: cacheControl
2143
+ });
2144
+ } else {
2145
+ anthropicContent.push({
2146
+ type: "document",
2147
+ source: { type: "file", file_id: fileId },
2148
+ cache_control: cacheControl
2149
+ });
2150
+ }
2151
+ break;
2152
+ }
2153
+ case "text": {
2154
+ const enableCitations = await shouldEnableCitations(
2155
+ part.providerOptions
2156
+ );
2157
+ const metadata = await getDocumentMetadata(
2158
+ part.providerOptions
2159
+ );
2156
2160
  anthropicContent.push({
2157
2161
  type: "document",
2158
- source: { type: "file", file_id: fileId },
2162
+ source: {
2163
+ type: "text",
2164
+ media_type: "text/plain",
2165
+ data: part.data.text
2166
+ },
2167
+ title: (_b = metadata.title) != null ? _b : part.filename,
2168
+ ...metadata.context && {
2169
+ context: metadata.context
2170
+ },
2171
+ ...enableCitations && {
2172
+ citations: { enabled: true }
2173
+ },
2159
2174
  cache_control: cacheControl
2160
2175
  });
2176
+ break;
2177
+ }
2178
+ case "url":
2179
+ case "data": {
2180
+ const topLevel = getTopLevelMediaType(part.mediaType);
2181
+ if (topLevel === "image") {
2182
+ anthropicContent.push({
2183
+ type: "image",
2184
+ source: part.data.type === "url" ? {
2185
+ type: "url",
2186
+ url: part.data.url.toString()
2187
+ } : {
2188
+ type: "base64",
2189
+ media_type: resolveFullMediaType({ part }),
2190
+ data: convertToBase64(part.data.data)
2191
+ },
2192
+ cache_control: cacheControl
2193
+ });
2194
+ } else if (topLevel === "application" && (part.data.type === "url" ? part.mediaType === "application/pdf" : resolveFullMediaType({ part }) === "application/pdf")) {
2195
+ betas.add("pdfs-2024-09-25");
2196
+ const enableCitations = await shouldEnableCitations(
2197
+ part.providerOptions
2198
+ );
2199
+ const metadata = await getDocumentMetadata(
2200
+ part.providerOptions
2201
+ );
2202
+ anthropicContent.push({
2203
+ type: "document",
2204
+ source: part.data.type === "url" ? {
2205
+ type: "url",
2206
+ url: part.data.url.toString()
2207
+ } : {
2208
+ type: "base64",
2209
+ media_type: "application/pdf",
2210
+ data: convertToBase64(part.data.data)
2211
+ },
2212
+ title: (_c = metadata.title) != null ? _c : part.filename,
2213
+ ...metadata.context && {
2214
+ context: metadata.context
2215
+ },
2216
+ ...enableCitations && {
2217
+ citations: { enabled: true }
2218
+ },
2219
+ cache_control: cacheControl
2220
+ });
2221
+ } else if (part.mediaType === "text/plain") {
2222
+ const enableCitations = await shouldEnableCitations(
2223
+ part.providerOptions
2224
+ );
2225
+ const metadata = await getDocumentMetadata(
2226
+ part.providerOptions
2227
+ );
2228
+ anthropicContent.push({
2229
+ type: "document",
2230
+ source: part.data.type === "url" ? {
2231
+ type: "url",
2232
+ url: part.data.url.toString()
2233
+ } : {
2234
+ type: "text",
2235
+ media_type: "text/plain",
2236
+ data: convertBytesDataToString(
2237
+ part.data.data
2238
+ )
2239
+ },
2240
+ title: (_d = metadata.title) != null ? _d : part.filename,
2241
+ ...metadata.context && {
2242
+ context: metadata.context
2243
+ },
2244
+ ...enableCitations && {
2245
+ citations: { enabled: true }
2246
+ },
2247
+ cache_control: cacheControl
2248
+ });
2249
+ } else {
2250
+ throw new UnsupportedFunctionalityError2({
2251
+ functionality: `media type: ${part.mediaType}`
2252
+ });
2253
+ }
2254
+ break;
2161
2255
  }
2162
- } else if (part.mediaType.startsWith("image/")) {
2163
- anthropicContent.push({
2164
- type: "image",
2165
- source: isUrlData(part.data) ? {
2166
- type: "url",
2167
- url: getUrlString(part.data)
2168
- } : {
2169
- type: "base64",
2170
- media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
2171
- data: convertToBase64(part.data)
2172
- },
2173
- cache_control: cacheControl
2174
- });
2175
- } else if (part.mediaType === "application/pdf") {
2176
- betas.add("pdfs-2024-09-25");
2177
- const enableCitations = await shouldEnableCitations(
2178
- part.providerOptions
2179
- );
2180
- const metadata = await getDocumentMetadata(
2181
- part.providerOptions
2182
- );
2183
- anthropicContent.push({
2184
- type: "document",
2185
- source: isUrlData(part.data) ? {
2186
- type: "url",
2187
- url: getUrlString(part.data)
2188
- } : {
2189
- type: "base64",
2190
- media_type: "application/pdf",
2191
- data: convertToBase64(part.data)
2192
- },
2193
- title: (_b = metadata.title) != null ? _b : part.filename,
2194
- ...metadata.context && { context: metadata.context },
2195
- ...enableCitations && {
2196
- citations: { enabled: true }
2197
- },
2198
- cache_control: cacheControl
2199
- });
2200
- } else if (part.mediaType === "text/plain") {
2201
- const enableCitations = await shouldEnableCitations(
2202
- part.providerOptions
2203
- );
2204
- const metadata = await getDocumentMetadata(
2205
- part.providerOptions
2206
- );
2207
- anthropicContent.push({
2208
- type: "document",
2209
- source: isUrlData(part.data) ? {
2210
- type: "url",
2211
- url: getUrlString(part.data)
2212
- } : {
2213
- type: "text",
2214
- media_type: "text/plain",
2215
- data: convertToString(part.data)
2216
- },
2217
- title: (_c = metadata.title) != null ? _c : part.filename,
2218
- ...metadata.context && { context: metadata.context },
2219
- ...enableCitations && {
2220
- citations: { enabled: true }
2221
- },
2222
- cache_control: cacheControl
2223
- });
2224
- } else {
2225
- throw new UnsupportedFunctionalityError2({
2226
- functionality: `media type: ${part.mediaType}`
2227
- });
2228
2256
  }
2229
2257
  break;
2230
2258
  }
@@ -2239,10 +2267,10 @@ async function convertToAnthropicPrompt({
2239
2267
  continue;
2240
2268
  }
2241
2269
  const isLastPart = i2 === content.length - 1;
2242
- const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
2270
+ const cacheControl = (_e = validator.getCacheControl(part.providerOptions, {
2243
2271
  type: "tool result part",
2244
2272
  canCache: true
2245
- })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
2273
+ })) != null ? _e : isLastPart ? validator.getCacheControl(message.providerOptions, {
2246
2274
  type: "tool result message",
2247
2275
  canCache: true
2248
2276
  }) : void 0;
@@ -2333,7 +2361,7 @@ async function convertToAnthropicPrompt({
2333
2361
  contentValue = output.value;
2334
2362
  break;
2335
2363
  case "execution-denied":
2336
- contentValue = (_e = output.reason) != null ? _e : "Tool call execution denied.";
2364
+ contentValue = (_f = output.reason) != null ? _f : "Tool call execution denied.";
2337
2365
  break;
2338
2366
  case "json":
2339
2367
  case "error-json":
@@ -2370,16 +2398,16 @@ async function convertToAnthropicPrompt({
2370
2398
  for (let k = 0; k < content.length; k++) {
2371
2399
  const part = content[k];
2372
2400
  const isLastContentPart = k === content.length - 1;
2373
- const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
2401
+ const cacheControl = (_g = validator.getCacheControl(part.providerOptions, {
2374
2402
  type: "assistant message part",
2375
2403
  canCache: true
2376
- })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
2404
+ })) != null ? _g : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
2377
2405
  type: "assistant message",
2378
2406
  canCache: true
2379
2407
  }) : void 0;
2380
2408
  switch (part.type) {
2381
2409
  case "text": {
2382
- const textMetadata = (_g = part.providerOptions) == null ? void 0 : _g.anthropic;
2410
+ const textMetadata = (_h = part.providerOptions) == null ? void 0 : _h.anthropic;
2383
2411
  if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
2384
2412
  anthropicContent.push({
2385
2413
  type: "compaction",
@@ -2452,10 +2480,10 @@ async function convertToAnthropicPrompt({
2452
2480
  const providerToolName = toolNameMapping.toProviderToolName(
2453
2481
  part.toolName
2454
2482
  );
2455
- const isMcpToolUse = ((_i = (_h = part.providerOptions) == null ? void 0 : _h.anthropic) == null ? void 0 : _i.type) === "mcp-tool-use";
2483
+ const isMcpToolUse = ((_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.type) === "mcp-tool-use";
2456
2484
  if (isMcpToolUse) {
2457
2485
  mcpToolUseIds.add(part.toolCallId);
2458
- const serverName = (_k = (_j = part.providerOptions) == null ? void 0 : _j.anthropic) == null ? void 0 : _k.serverName;
2486
+ const serverName = (_l = (_k = part.providerOptions) == null ? void 0 : _k.anthropic) == null ? void 0 : _l.serverName;
2459
2487
  if (serverName == null || typeof serverName !== "string") {
2460
2488
  warnings.push({
2461
2489
  type: "other",
@@ -2523,7 +2551,7 @@ async function convertToAnthropicPrompt({
2523
2551
  }
2524
2552
  break;
2525
2553
  }
2526
- const callerOptions = (_l = part.providerOptions) == null ? void 0 : _l.anthropic;
2554
+ const callerOptions = (_m = part.providerOptions) == null ? void 0 : _m.anthropic;
2527
2555
  const caller = (callerOptions == null ? void 0 : callerOptions.caller) ? (callerOptions.caller.type === "code_execution_20250825" || callerOptions.caller.type === "code_execution_20260120") && callerOptions.caller.toolId ? {
2528
2556
  type: callerOptions.caller.type,
2529
2557
  tool_id: callerOptions.caller.toolId
@@ -2576,7 +2604,7 @@ async function convertToAnthropicPrompt({
2576
2604
  tool_use_id: part.toolCallId,
2577
2605
  content: {
2578
2606
  type: "code_execution_tool_result_error",
2579
- error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2607
+ error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2580
2608
  },
2581
2609
  cache_control: cacheControl
2582
2610
  });
@@ -2587,7 +2615,7 @@ async function convertToAnthropicPrompt({
2587
2615
  cache_control: cacheControl,
2588
2616
  content: {
2589
2617
  type: "bash_code_execution_tool_result_error",
2590
- error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2618
+ error_code: (_o = errorInfo.errorCode) != null ? _o : "unknown"
2591
2619
  }
2592
2620
  });
2593
2621
  }
@@ -2620,7 +2648,7 @@ async function convertToAnthropicPrompt({
2620
2648
  stdout: codeExecutionOutput.stdout,
2621
2649
  stderr: codeExecutionOutput.stderr,
2622
2650
  return_code: codeExecutionOutput.return_code,
2623
- content: (_o = codeExecutionOutput.content) != null ? _o : []
2651
+ content: (_p = codeExecutionOutput.content) != null ? _p : []
2624
2652
  },
2625
2653
  cache_control: cacheControl
2626
2654
  });
@@ -2638,7 +2666,7 @@ async function convertToAnthropicPrompt({
2638
2666
  encrypted_stdout: codeExecutionOutput.encrypted_stdout,
2639
2667
  stderr: codeExecutionOutput.stderr,
2640
2668
  return_code: codeExecutionOutput.return_code,
2641
- content: (_p = codeExecutionOutput.content) != null ? _p : []
2669
+ content: (_q = codeExecutionOutput.content) != null ? _q : []
2642
2670
  },
2643
2671
  cache_control: cacheControl
2644
2672
  });
@@ -2657,7 +2685,7 @@ async function convertToAnthropicPrompt({
2657
2685
  stdout: codeExecutionOutput.stdout,
2658
2686
  stderr: codeExecutionOutput.stderr,
2659
2687
  return_code: codeExecutionOutput.return_code,
2660
- content: (_q = codeExecutionOutput.content) != null ? _q : []
2688
+ content: (_r = codeExecutionOutput.content) != null ? _r : []
2661
2689
  },
2662
2690
  cache_control: cacheControl
2663
2691
  });
@@ -2690,7 +2718,7 @@ async function convertToAnthropicPrompt({
2690
2718
  errorValue = output.value;
2691
2719
  }
2692
2720
  } catch (e) {
2693
- const extractedErrorCode = (_r = output.value) == null ? void 0 : _r.errorCode;
2721
+ const extractedErrorCode = (_s = output.value) == null ? void 0 : _s.errorCode;
2694
2722
  errorValue = {
2695
2723
  errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unavailable"
2696
2724
  };
@@ -2700,7 +2728,7 @@ async function convertToAnthropicPrompt({
2700
2728
  tool_use_id: part.toolCallId,
2701
2729
  content: {
2702
2730
  type: "web_fetch_tool_result_error",
2703
- error_code: (_s = errorValue.errorCode) != null ? _s : "unavailable"
2731
+ error_code: (_t = errorValue.errorCode) != null ? _t : "unavailable"
2704
2732
  },
2705
2733
  cache_control: cacheControl
2706
2734
  });