@ai-sdk/anthropic 4.0.96 → 4.0.97

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.
@@ -1,104 +0,0 @@
1
- import type {
2
- FilesV4,
3
- FilesV4UploadFileCallOptions,
4
- FilesV4UploadFileResult,
5
- } from '@ai-sdk/provider';
6
- import {
7
- combineHeaders,
8
- convertInlineFileDataToUint8Array,
9
- createJsonResponseHandler,
10
- lazySchema,
11
- postFormDataToApi,
12
- resolve,
13
- zodSchema,
14
- type FetchFunction,
15
- type Resolvable,
16
- } from '@ai-sdk/provider-utils';
17
- import { z } from 'zod/v4';
18
- import { anthropicFailedResponseHandler } from './anthropic-error';
19
-
20
- const anthropicUploadFileResponseSchema = lazySchema(() =>
21
- zodSchema(
22
- z.object({
23
- id: z.string(),
24
- type: z.literal('file'),
25
- filename: z.string(),
26
- mime_type: z.string(),
27
- size_bytes: z.number(),
28
- created_at: z.string(),
29
- downloadable: z.boolean().nullish(),
30
- }),
31
- ),
32
- );
33
-
34
- interface AnthropicFilesConfig {
35
- provider: string;
36
- baseURL: string;
37
- headers: Resolvable<Record<string, string | undefined>>;
38
- fetch?: FetchFunction;
39
- }
40
-
41
- export class AnthropicFiles implements FilesV4 {
42
- readonly specificationVersion = 'v4';
43
-
44
- get provider(): string {
45
- return this.config.provider;
46
- }
47
-
48
- constructor(private readonly config: AnthropicFilesConfig) {}
49
-
50
- async uploadFile({
51
- data,
52
- mediaType,
53
- filename,
54
- abortSignal,
55
- headers,
56
- }: FilesV4UploadFileCallOptions): Promise<FilesV4UploadFileResult> {
57
- const fileBytes = convertInlineFileDataToUint8Array(data);
58
-
59
- const blob = new Blob([fileBytes as Uint8Array<ArrayBuffer>], {
60
- type: mediaType,
61
- });
62
-
63
- const formData = new FormData();
64
- if (filename != null) {
65
- formData.append('file', blob, filename);
66
- } else {
67
- formData.append('file', blob);
68
- }
69
-
70
- const { value: response } = await postFormDataToApi({
71
- url: `${this.config.baseURL}/files`,
72
- headers: combineHeaders(
73
- await resolve(this.config.headers),
74
- { 'anthropic-beta': 'files-api-2025-04-14' },
75
- headers,
76
- ),
77
- formData,
78
- failedResponseHandler: anthropicFailedResponseHandler,
79
- successfulResponseHandler: createJsonResponseHandler(
80
- anthropicUploadFileResponseSchema,
81
- ),
82
- abortSignal,
83
- fetch: this.config.fetch,
84
- });
85
-
86
- return {
87
- warnings: [],
88
- providerReference: { anthropic: response.id },
89
- mediaType: response.mime_type ?? mediaType,
90
- filename: response.filename ?? filename,
91
- providerMetadata: {
92
- anthropic: {
93
- filename: response.filename,
94
- mimeType: response.mime_type,
95
- sizeBytes: response.size_bytes,
96
- createdAt: response.created_at,
97
- ...(response.downloadable != null
98
- ? { downloadable: response.downloadable }
99
- : {}),
100
- },
101
- },
102
- };
103
- }
104
- }