@ai-sdk/anthropic 3.0.133 → 3.0.135

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.
@@ -2,6 +2,7 @@ import {
2
2
  UnsupportedFunctionalityError,
3
3
  type SharedV3Warning,
4
4
  type LanguageModelV3DataContent,
5
+ type LanguageModelV3FilePart,
5
6
  type LanguageModelV3Message,
6
7
  type LanguageModelV3Prompt,
7
8
  type SharedV3ProviderMetadata,
@@ -19,6 +20,7 @@ import {
19
20
  import {
20
21
  anthropicReasoningMetadataSchema,
21
22
  type AnthropicAssistantMessage,
23
+ type AnthropicImageContent,
22
24
  type AnthropicMessagesPrompt,
23
25
  type AnthropicSystemMessage,
24
26
  type AnthropicTextContent,
@@ -26,6 +28,7 @@ import {
26
28
  type AnthropicToolCallCaller,
27
29
  type AnthropicToolResultContent,
28
30
  type AnthropicUserMessage,
31
+ type AnthropicVideoContent,
29
32
  type AnthropicWebFetchToolResultContent,
30
33
  type Citation,
31
34
  } from './anthropic-messages-api';
@@ -109,6 +112,14 @@ async function extractErrorValue(
109
112
  return {};
110
113
  }
111
114
 
115
+ /** Return undefined to use the default file conversion. */
116
+ export type AnthropicMediaPartConverter = (
117
+ part: LanguageModelV3FilePart,
118
+ ) => PromiseLike<
119
+ | Omit<AnthropicImageContent | AnthropicVideoContent, 'cache_control'>
120
+ | undefined
121
+ >;
122
+
112
123
  export async function convertToAnthropicMessagesPrompt({
113
124
  prompt,
114
125
  sendReasoning,
@@ -116,12 +127,14 @@ export async function convertToAnthropicMessagesPrompt({
116
127
  cacheControlValidator,
117
128
  toolNameMapping,
118
129
  toolsetNames = {},
130
+ convertMediaPart,
119
131
  }: {
120
132
  prompt: LanguageModelV3Prompt;
121
133
  sendReasoning: boolean;
122
134
  warnings: SharedV3Warning[];
123
135
  cacheControlValidator?: CacheControlValidator;
124
136
  toolNameMapping: ToolNameMapping;
137
+ convertMediaPart?: AnthropicMediaPartConverter;
125
138
 
126
139
  /**
127
140
  * Maps custom tool names of toolset tools (e.g. the computer toolset) to
@@ -336,6 +349,15 @@ export async function convertToAnthropicMessagesPrompt({
336
349
  }
337
350
 
338
351
  case 'file': {
352
+ const customContent = await convertMediaPart?.(part);
353
+ if (customContent != null) {
354
+ anthropicContent.push({
355
+ ...customContent,
356
+ cache_control: cacheControl,
357
+ });
358
+ break;
359
+ }
360
+
339
361
  if (part.mediaType.startsWith('image/')) {
340
362
  anthropicContent.push({
341
363
  type: 'image',
@@ -11,11 +11,18 @@ const MAX_CACHE_BREAKPOINTS = 4;
11
11
  // Allows both cacheControl and cache_control for flexibility
12
12
  function getCacheControl(
13
13
  providerMetadata: SharedV3ProviderMetadata | undefined,
14
+ providerOptionsName: string,
14
15
  ): AnthropicCacheControl | undefined {
16
+ const custom = providerMetadata?.[providerOptionsName];
15
17
  const anthropic = providerMetadata?.anthropic;
16
18
 
17
- // allow both cacheControl and cache_control:
18
- const cacheControlValue = anthropic?.cacheControl ?? anthropic?.cache_control;
19
+ // Prefer the configured namespace, retaining the canonical namespace as a fallback.
20
+ // Allow both cacheControl and cache_control.
21
+ const cacheControlValue =
22
+ custom?.cacheControl ??
23
+ custom?.cache_control ??
24
+ anthropic?.cacheControl ??
25
+ anthropic?.cache_control;
19
26
 
20
27
  // Pass through value assuming it is of the correct type.
21
28
  // The Anthropic API will validate the value.
@@ -26,11 +33,16 @@ export class CacheControlValidator {
26
33
  private breakpointCount = 0;
27
34
  private warnings: SharedV3Warning[] = [];
28
35
 
36
+ constructor(private readonly providerOptionsName = 'anthropic') {}
37
+
29
38
  getCacheControl(
30
39
  providerMetadata: SharedV3ProviderMetadata | undefined,
31
40
  context: { type: string; canCache: boolean },
32
41
  ): AnthropicCacheControl | undefined {
33
- const cacheControlValue = getCacheControl(providerMetadata);
42
+ const cacheControlValue = getCacheControl(
43
+ providerMetadata,
44
+ this.providerOptionsName,
45
+ );
34
46
 
35
47
  if (!cacheControlValue) {
36
48
  return undefined;