@ai-sdk/anthropic 4.0.0-beta.4 → 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.
Files changed (53) hide show
  1. package/CHANGELOG.md +305 -4
  2. package/README.md +2 -0
  3. package/dist/index.d.ts +83 -58
  4. package/dist/index.js +2043 -1356
  5. package/dist/index.js.map +1 -1
  6. package/dist/internal/index.d.ts +85 -58
  7. package/dist/internal/index.js +1804 -1342
  8. package/dist/internal/index.js.map +1 -1
  9. package/docs/05-anthropic.mdx +116 -13
  10. package/package.json +14 -15
  11. package/src/{anthropic-messages-api.ts → anthropic-api.ts} +14 -6
  12. package/src/anthropic-error.ts +1 -1
  13. package/src/anthropic-files.ts +95 -0
  14. package/src/{anthropic-messages-language-model.ts → anthropic-language-model.ts} +263 -78
  15. package/src/anthropic-message-metadata.ts +1 -4
  16. package/src/{anthropic-messages-options.ts → anthropic-options.ts} +68 -11
  17. package/src/anthropic-prepare-tools.ts +14 -7
  18. package/src/anthropic-provider.ts +42 -13
  19. package/src/{convert-anthropic-messages-usage.ts → convert-anthropic-usage.ts} +4 -4
  20. package/src/{convert-to-anthropic-messages-prompt.ts → convert-to-anthropic-prompt.ts} +190 -149
  21. package/src/forward-anthropic-container-id-from-last-step.ts +2 -2
  22. package/src/get-cache-control.ts +5 -2
  23. package/src/index.ts +1 -1
  24. package/src/internal/index.ts +11 -2
  25. package/src/map-anthropic-stop-reason.ts +1 -1
  26. package/src/sanitize-json-schema.ts +203 -0
  27. package/src/skills/anthropic-skills-api.ts +44 -0
  28. package/src/skills/anthropic-skills.ts +132 -0
  29. package/src/tool/bash_20241022.ts +2 -2
  30. package/src/tool/bash_20250124.ts +2 -2
  31. package/src/tool/code-execution_20250522.ts +2 -2
  32. package/src/tool/code-execution_20250825.ts +2 -2
  33. package/src/tool/code-execution_20260120.ts +2 -2
  34. package/src/tool/computer_20241022.ts +2 -2
  35. package/src/tool/computer_20250124.ts +2 -2
  36. package/src/tool/computer_20251124.ts +2 -2
  37. package/src/tool/memory_20250818.ts +2 -2
  38. package/src/tool/text-editor_20241022.ts +2 -2
  39. package/src/tool/text-editor_20250124.ts +2 -2
  40. package/src/tool/text-editor_20250429.ts +2 -2
  41. package/src/tool/text-editor_20250728.ts +6 -3
  42. package/src/tool/tool-search-bm25_20251119.ts +2 -2
  43. package/src/tool/tool-search-regex_20251119.ts +2 -2
  44. package/src/tool/web-fetch-20250910.ts +2 -2
  45. package/src/tool/web-fetch-20260209.ts +2 -2
  46. package/src/tool/web-search_20250305.ts +2 -2
  47. package/src/tool/web-search_20260209.ts +2 -2
  48. package/dist/index.d.mts +0 -1090
  49. package/dist/index.mjs +0 -5244
  50. package/dist/index.mjs.map +0 -1
  51. package/dist/internal/index.d.mts +0 -969
  52. package/dist/internal/index.mjs +0 -5136
  53. package/dist/internal/index.mjs.map +0 -1
@@ -0,0 +1,44 @@
1
+ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
2
+ import { z } from 'zod/v4';
3
+
4
+ export const anthropicSkillResponseSchema = lazySchema(() =>
5
+ zodSchema(
6
+ z.object({
7
+ id: z.string(),
8
+ display_title: z.string().nullish(),
9
+ name: z.string().nullish(),
10
+ description: z.string().nullish(),
11
+ latest_version: z.string().nullish(),
12
+ source: z.string(),
13
+ created_at: z.string(),
14
+ updated_at: z.string(),
15
+ }),
16
+ ),
17
+ );
18
+
19
+ export type AnthropicSkillResponse = ReturnType<
20
+ typeof anthropicSkillResponseSchema
21
+ >['_type'];
22
+
23
+ export const anthropicSkillVersionListResponseSchema = lazySchema(() =>
24
+ zodSchema(
25
+ z.object({
26
+ data: z.array(
27
+ z.object({
28
+ version: z.string(),
29
+ }),
30
+ ),
31
+ }),
32
+ ),
33
+ );
34
+
35
+ export const anthropicSkillVersionResponseSchema = lazySchema(() =>
36
+ zodSchema(
37
+ z.object({
38
+ type: z.string(),
39
+ skill_id: z.string(),
40
+ name: z.string().nullish(),
41
+ description: z.string().nullish(),
42
+ }),
43
+ ),
44
+ );
@@ -0,0 +1,132 @@
1
+ import type { SkillsV4, SharedV4Warning } from '@ai-sdk/provider';
2
+ import {
3
+ combineHeaders,
4
+ convertInlineFileDataToUint8Array,
5
+ createJsonResponseHandler,
6
+ getFromApi,
7
+ postFormDataToApi,
8
+ resolve,
9
+ type FetchFunction,
10
+ type Resolvable,
11
+ } from '@ai-sdk/provider-utils';
12
+ import { anthropicFailedResponseHandler } from '../anthropic-error';
13
+ import {
14
+ anthropicSkillResponseSchema,
15
+ anthropicSkillVersionResponseSchema,
16
+ } from './anthropic-skills-api';
17
+
18
+ interface AnthropicSkillsConfig {
19
+ provider: string;
20
+ baseURL: string;
21
+ headers: Resolvable<Record<string, string | undefined>>;
22
+ fetch?: FetchFunction;
23
+ }
24
+
25
+ export class AnthropicSkills implements SkillsV4 {
26
+ readonly specificationVersion = 'v4';
27
+
28
+ get provider(): string {
29
+ return this.config.provider;
30
+ }
31
+
32
+ constructor(private readonly config: AnthropicSkillsConfig) {}
33
+
34
+ private async getHeaders(): Promise<Record<string, string | undefined>> {
35
+ return combineHeaders(await resolve(this.config.headers), {
36
+ 'anthropic-beta': 'skills-2025-10-02',
37
+ });
38
+ }
39
+
40
+ private async fetchVersionMetadata({
41
+ skillId,
42
+ version,
43
+ headers,
44
+ }: {
45
+ skillId: string;
46
+ version: string;
47
+ headers: Record<string, string | undefined>;
48
+ }): Promise<{ name?: string; description?: string }> {
49
+ const { value: versionResponse } = await getFromApi({
50
+ url: `${this.config.baseURL}/skills/${skillId}/versions/${version}`,
51
+ headers,
52
+ failedResponseHandler: anthropicFailedResponseHandler,
53
+ successfulResponseHandler: createJsonResponseHandler(
54
+ anthropicSkillVersionResponseSchema,
55
+ ),
56
+ fetch: this.config.fetch,
57
+ });
58
+
59
+ return {
60
+ ...(versionResponse.name != null ? { name: versionResponse.name } : {}),
61
+ ...(versionResponse.description != null
62
+ ? { description: versionResponse.description }
63
+ : {}),
64
+ };
65
+ }
66
+
67
+ async uploadSkill(
68
+ params: Parameters<SkillsV4['uploadSkill']>[0],
69
+ ): Promise<Awaited<ReturnType<SkillsV4['uploadSkill']>>> {
70
+ const warnings: SharedV4Warning[] = [];
71
+
72
+ const formData = new FormData();
73
+
74
+ if (params.displayTitle != null) {
75
+ formData.append('display_title', params.displayTitle);
76
+ }
77
+
78
+ for (const file of params.files) {
79
+ const content = convertInlineFileDataToUint8Array(file.data);
80
+ formData.append('files[]', new Blob([content]), file.path);
81
+ }
82
+
83
+ const headers = await this.getHeaders();
84
+
85
+ const { value: response } = await postFormDataToApi({
86
+ url: `${this.config.baseURL}/skills`,
87
+ headers,
88
+ formData,
89
+ failedResponseHandler: anthropicFailedResponseHandler,
90
+ successfulResponseHandler: createJsonResponseHandler(
91
+ anthropicSkillResponseSchema,
92
+ ),
93
+ fetch: this.config.fetch,
94
+ });
95
+
96
+ const versionMetadata =
97
+ response.latest_version != null
98
+ ? await this.fetchVersionMetadata({
99
+ skillId: response.id,
100
+ version: response.latest_version,
101
+ headers,
102
+ })
103
+ : {};
104
+
105
+ const name = versionMetadata.name ?? response.name;
106
+ const description = versionMetadata.description ?? response.description;
107
+
108
+ return {
109
+ providerReference: { anthropic: response.id },
110
+ ...(response.display_title != null
111
+ ? { displayTitle: response.display_title }
112
+ : {}),
113
+ ...(name != null ? { name } : {}),
114
+ ...(description != null ? { description } : {}),
115
+ ...(response.latest_version != null
116
+ ? { latestVersion: response.latest_version }
117
+ : {}),
118
+ providerMetadata: {
119
+ anthropic: {
120
+ ...(response.source != null ? { source: response.source } : {}),
121
+ ...(response.created_at != null
122
+ ? { createdAt: response.created_at }
123
+ : {}),
124
+ ...(response.updated_at != null
125
+ ? { updatedAt: response.updated_at }
126
+ : {}),
127
+ },
128
+ },
129
+ warnings,
130
+ };
131
+ }
132
+ }
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -14,7 +14,7 @@ const bash_20241022InputSchema = lazySchema(() =>
14
14
  ),
15
15
  );
16
16
 
17
- export const bash_20241022 = createProviderToolFactory<
17
+ export const bash_20241022 = createProviderDefinedToolFactory<
18
18
  {
19
19
  /**
20
20
  * The bash command to run. Required unless the tool is being restarted.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -14,7 +14,7 @@ const bash_20250124InputSchema = lazySchema(() =>
14
14
  ),
15
15
  );
16
16
 
17
- export const bash_20250124 = createProviderToolFactory<
17
+ export const bash_20250124 = createProviderDefinedToolFactory<
18
18
  {
19
19
  /**
20
20
  * The bash command to run. Required unless the tool is being restarted.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -33,7 +33,7 @@ const codeExecution_20250522InputSchema = lazySchema(() =>
33
33
  ),
34
34
  );
35
35
 
36
- const factory = createProviderToolFactoryWithOutputSchema<
36
+ const factory = createProviderExecutedToolFactory<
37
37
  {
38
38
  /**
39
39
  * The Python code to execute.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -103,7 +103,7 @@ export const codeExecution_20250825InputSchema = lazySchema(() =>
103
103
  ),
104
104
  );
105
105
 
106
- const factory = createProviderToolFactoryWithOutputSchema<
106
+ const factory = createProviderExecutedToolFactory<
107
107
  | {
108
108
  type: 'programmatic-tool-call';
109
109
  /**
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -117,7 +117,7 @@ export const codeExecution_20260120InputSchema = lazySchema(() =>
117
117
  ),
118
118
  );
119
119
 
120
- const factory = createProviderToolFactoryWithOutputSchema<
120
+ const factory = createProviderExecutedToolFactory<
121
121
  | {
122
122
  type: 'programmatic-tool-call';
123
123
  /**
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -26,7 +26,7 @@ const computer_20241022InputSchema = lazySchema(() =>
26
26
  ),
27
27
  );
28
28
 
29
- export const computer_20241022 = createProviderToolFactory<
29
+ export const computer_20241022 = createProviderDefinedToolFactory<
30
30
  {
31
31
  /**
32
32
  * The action to perform. The available actions are:
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -38,7 +38,7 @@ const computer_20250124InputSchema = lazySchema(() =>
38
38
  ),
39
39
  );
40
40
 
41
- export const computer_20250124 = createProviderToolFactory<
41
+ export const computer_20250124 = createProviderDefinedToolFactory<
42
42
  {
43
43
  /**
44
44
  * - `key`: Press a key or key-combination on the keyboard.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -47,7 +47,7 @@ const computer_20251124InputSchema = lazySchema(() =>
47
47
  ),
48
48
  );
49
49
 
50
- export const computer_20251124 = createProviderToolFactory<
50
+ export const computer_20251124 = createProviderDefinedToolFactory<
51
51
  {
52
52
  /**
53
53
  * - `key`: Press a key or key-combination on the keyboard.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -43,7 +43,7 @@ const memory_20250818InputSchema = lazySchema(() =>
43
43
  ),
44
44
  );
45
45
 
46
- export const memory_20250818 = createProviderToolFactory<
46
+ export const memory_20250818 = createProviderDefinedToolFactory<
47
47
  | { command: 'view'; path: string; view_range?: [number, number] }
48
48
  | { command: 'create'; path: string; file_text: string }
49
49
  | { command: 'str_replace'; path: string; old_str: string; new_str: string }
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -20,7 +20,7 @@ const textEditor_20241022InputSchema = lazySchema(() =>
20
20
  ),
21
21
  );
22
22
 
23
- export const textEditor_20241022 = createProviderToolFactory<
23
+ export const textEditor_20241022 = createProviderDefinedToolFactory<
24
24
  {
25
25
  /**
26
26
  * The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -20,7 +20,7 @@ const textEditor_20250124InputSchema = lazySchema(() =>
20
20
  ),
21
21
  );
22
22
 
23
- export const textEditor_20250124 = createProviderToolFactory<
23
+ export const textEditor_20250124 = createProviderDefinedToolFactory<
24
24
  {
25
25
  /**
26
26
  * The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`, `undo_edit`.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactory,
2
+ createProviderDefinedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -20,7 +20,7 @@ const textEditor_20250429InputSchema = lazySchema(() =>
20
20
  ),
21
21
  );
22
22
 
23
- export const textEditor_20250429 = createProviderToolFactory<
23
+ export const textEditor_20250429 = createProviderDefinedToolFactory<
24
24
  {
25
25
  /**
26
26
  * The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`.
@@ -1,6 +1,9 @@
1
- import { createProviderToolFactory } from '@ai-sdk/provider-utils';
1
+ import {
2
+ createProviderDefinedToolFactory,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
2
6
  import { z } from 'zod/v4';
3
- import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
4
7
 
5
8
  export const textEditor_20250728ArgsSchema = lazySchema(() =>
6
9
  zodSchema(
@@ -25,7 +28,7 @@ const textEditor_20250728InputSchema = lazySchema(() =>
25
28
  ),
26
29
  );
27
30
 
28
- const factory = createProviderToolFactory<
31
+ const factory = createProviderDefinedToolFactory<
29
32
  {
30
33
  /**
31
34
  * The commands to run. Allowed options are: `view`, `create`, `str_replace`, `insert`.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -40,7 +40,7 @@ const toolSearchBm25_20251119InputSchema = lazySchema(() =>
40
40
  ),
41
41
  );
42
42
 
43
- const factory = createProviderToolFactoryWithOutputSchema<
43
+ const factory = createProviderExecutedToolFactory<
44
44
  {
45
45
  /**
46
46
  * A natural language query to search for tools.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -46,7 +46,7 @@ const toolSearchRegex_20251119InputSchema = lazySchema(() =>
46
46
  ),
47
47
  );
48
48
 
49
- const factory = createProviderToolFactoryWithOutputSchema<
49
+ const factory = createProviderExecutedToolFactory<
50
50
  {
51
51
  /**
52
52
  * A regex pattern to search for tools.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -52,7 +52,7 @@ const webFetch_20250910InputSchema = lazySchema(() =>
52
52
  ),
53
53
  );
54
54
 
55
- const factory = createProviderToolFactoryWithOutputSchema<
55
+ const factory = createProviderExecutedToolFactory<
56
56
  {
57
57
  /**
58
58
  * The URL to fetch.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -52,7 +52,7 @@ const webFetch_20260209InputSchema = lazySchema(() =>
52
52
  ),
53
53
  );
54
54
 
55
- const factory = createProviderToolFactoryWithOutputSchema<
55
+ const factory = createProviderExecutedToolFactory<
56
56
  {
57
57
  /**
58
58
  * The URL to fetch.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -46,7 +46,7 @@ const webSearch_20250305InputSchema = lazySchema(() =>
46
46
  ),
47
47
  );
48
48
 
49
- const factory = createProviderToolFactoryWithOutputSchema<
49
+ const factory = createProviderExecutedToolFactory<
50
50
  {
51
51
  /**
52
52
  * The search query to execute.
@@ -1,5 +1,5 @@
1
1
  import {
2
- createProviderToolFactoryWithOutputSchema,
2
+ createProviderExecutedToolFactory,
3
3
  lazySchema,
4
4
  zodSchema,
5
5
  } from '@ai-sdk/provider-utils';
@@ -46,7 +46,7 @@ const webSearch_20260209InputSchema = lazySchema(() =>
46
46
  ),
47
47
  );
48
48
 
49
- const factory = createProviderToolFactoryWithOutputSchema<
49
+ const factory = createProviderExecutedToolFactory<
50
50
  {
51
51
  /**
52
52
  * The search query to execute.