@ai-sdk/google 4.0.0-beta.3 → 4.0.0-beta.30

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.
@@ -7,8 +7,19 @@ import { z } from 'zod/v4';
7
7
 
8
8
  export type GoogleGenerativeAIEmbeddingModelId =
9
9
  | 'gemini-embedding-001'
10
+ | 'gemini-embedding-2-preview'
10
11
  | (string & {});
11
12
 
13
+ const googleEmbeddingContentPartSchema = z.union([
14
+ z.object({ text: z.string() }),
15
+ z.object({
16
+ inlineData: z.object({
17
+ mimeType: z.string(),
18
+ data: z.string(),
19
+ }),
20
+ }),
21
+ ]);
22
+
12
23
  export const googleEmbeddingModelOptions = lazySchema(() =>
13
24
  zodSchema(
14
25
  z.object({
@@ -42,6 +53,19 @@ export const googleEmbeddingModelOptions = lazySchema(() =>
42
53
  'CODE_RETRIEVAL_QUERY',
43
54
  ])
44
55
  .optional(),
56
+
57
+ /**
58
+ * Optional. Per-value multimodal content parts for embedding non-text
59
+ * content (images, video, PDF, audio). Each entry corresponds to the
60
+ * embedding value at the same index and its parts are merged with the
61
+ * text value in the request. Use `null` for entries that are text-only.
62
+ *
63
+ * The array length must match the number of values being embedded. In
64
+ * the case of a single embedding, the array length must be 1.
65
+ */
66
+ content: z
67
+ .array(z.array(googleEmbeddingContentPartSchema).min(1).nullable())
68
+ .optional(),
45
69
  }),
46
70
  ),
47
71
  );
@@ -0,0 +1,230 @@
1
+ import {
2
+ AISDKError,
3
+ type FilesV4,
4
+ type FilesV4UploadFileCallOptions,
5
+ type FilesV4UploadFileResult,
6
+ type SharedV4Warning,
7
+ } from '@ai-sdk/provider';
8
+ import {
9
+ combineHeaders,
10
+ convertUint8ArrayToBase64,
11
+ createJsonResponseHandler,
12
+ delay,
13
+ type FetchFunction,
14
+ lazySchema,
15
+ parseProviderOptions,
16
+ postJsonToApi,
17
+ zodSchema,
18
+ getFromApi,
19
+ } from '@ai-sdk/provider-utils';
20
+ import { z } from 'zod/v4';
21
+ import { googleFailedResponseHandler } from './google-error';
22
+
23
+ export type GoogleFilesUploadOptions = {
24
+ displayName?: string | null;
25
+ pollIntervalMs?: number | null;
26
+ pollTimeoutMs?: number | null;
27
+
28
+ [key: string]: unknown;
29
+ };
30
+
31
+ interface GoogleGenerativeAIFilesConfig {
32
+ provider: string;
33
+ baseURL: string;
34
+ headers: () => Record<string, string | undefined>;
35
+ fetch?: FetchFunction;
36
+ }
37
+
38
+ export class GoogleGenerativeAIFiles implements FilesV4 {
39
+ readonly specificationVersion = 'v4';
40
+
41
+ get provider(): string {
42
+ return this.config.provider;
43
+ }
44
+
45
+ constructor(private readonly config: GoogleGenerativeAIFilesConfig) {}
46
+
47
+ async uploadFile(
48
+ options: FilesV4UploadFileCallOptions,
49
+ ): Promise<FilesV4UploadFileResult> {
50
+ const googleOptions = (await parseProviderOptions({
51
+ provider: 'google',
52
+ providerOptions: options.providerOptions,
53
+ schema: googleFilesUploadOptionsSchema,
54
+ })) as GoogleFilesUploadOptions | undefined;
55
+
56
+ const resolvedHeaders = this.config.headers();
57
+ const fetchFn = this.config.fetch ?? globalThis.fetch;
58
+
59
+ const warnings: Array<SharedV4Warning> = [];
60
+ if (options.filename != null) {
61
+ warnings.push({ type: 'unsupported', feature: 'filename' });
62
+ }
63
+
64
+ const data = options.data;
65
+ const fileBytes =
66
+ data instanceof Uint8Array
67
+ ? data
68
+ : Uint8Array.from(atob(data), c => c.charCodeAt(0));
69
+
70
+ const mediaType = options.mediaType;
71
+ const displayName = googleOptions?.displayName;
72
+
73
+ const baseOrigin = this.config.baseURL.replace(/\/v1beta$/, '');
74
+
75
+ const initResponse = await fetchFn(`${baseOrigin}/upload/v1beta/files`, {
76
+ method: 'POST',
77
+ headers: {
78
+ ...resolvedHeaders,
79
+ 'X-Goog-Upload-Protocol': 'resumable',
80
+ 'X-Goog-Upload-Command': 'start',
81
+ 'X-Goog-Upload-Header-Content-Length': String(fileBytes.length),
82
+ 'X-Goog-Upload-Header-Content-Type': mediaType,
83
+ 'Content-Type': 'application/json',
84
+ },
85
+ body: JSON.stringify({
86
+ file: {
87
+ ...(displayName != null ? { display_name: displayName } : {}),
88
+ },
89
+ }),
90
+ });
91
+
92
+ if (!initResponse.ok) {
93
+ const errorBody = await initResponse.text();
94
+ throw new AISDKError({
95
+ name: 'GOOGLE_FILES_UPLOAD_ERROR',
96
+ message: `Failed to initiate resumable upload: ${initResponse.status} ${errorBody}`,
97
+ });
98
+ }
99
+
100
+ const uploadUrl = initResponse.headers.get('x-goog-upload-url');
101
+ if (!uploadUrl) {
102
+ throw new AISDKError({
103
+ name: 'GOOGLE_FILES_UPLOAD_ERROR',
104
+ message: 'No upload URL returned from initiation request',
105
+ });
106
+ }
107
+
108
+ const uploadResponse = await fetchFn(uploadUrl, {
109
+ method: 'POST',
110
+ headers: {
111
+ 'Content-Length': String(fileBytes.length),
112
+ 'X-Goog-Upload-Offset': '0',
113
+ 'X-Goog-Upload-Command': 'upload, finalize',
114
+ },
115
+ body: fileBytes,
116
+ });
117
+
118
+ if (!uploadResponse.ok) {
119
+ const errorBody = await uploadResponse.text();
120
+ throw new AISDKError({
121
+ name: 'GOOGLE_FILES_UPLOAD_ERROR',
122
+ message: `Failed to upload file data: ${uploadResponse.status} ${errorBody}`,
123
+ });
124
+ }
125
+
126
+ const uploadResult = (await uploadResponse.json()) as {
127
+ file: GoogleFileResource;
128
+ };
129
+
130
+ let file = uploadResult.file;
131
+
132
+ const pollIntervalMs = googleOptions?.pollIntervalMs ?? 2000;
133
+ const pollTimeoutMs = googleOptions?.pollTimeoutMs ?? 300000;
134
+ const startTime = Date.now();
135
+
136
+ while (file.state === 'PROCESSING') {
137
+ if (Date.now() - startTime > pollTimeoutMs) {
138
+ throw new AISDKError({
139
+ name: 'GOOGLE_FILES_UPLOAD_TIMEOUT',
140
+ message: `File processing timed out after ${pollTimeoutMs}ms`,
141
+ });
142
+ }
143
+
144
+ await delay(pollIntervalMs);
145
+
146
+ const { value: fileStatus } = await getFromApi({
147
+ url: `${this.config.baseURL}/${file.name}`,
148
+ headers: combineHeaders(resolvedHeaders),
149
+ successfulResponseHandler: createJsonResponseHandler(
150
+ googleFileResponseSchema,
151
+ ),
152
+ failedResponseHandler: googleFailedResponseHandler,
153
+ fetch: this.config.fetch,
154
+ });
155
+
156
+ file = fileStatus;
157
+ }
158
+
159
+ if (file.state === 'FAILED') {
160
+ throw new AISDKError({
161
+ name: 'GOOGLE_FILES_UPLOAD_FAILED',
162
+ message: `File processing failed for ${file.name}`,
163
+ });
164
+ }
165
+
166
+ return {
167
+ warnings,
168
+ providerReference: { google: file.uri },
169
+ mediaType: file.mimeType ?? options.mediaType,
170
+ providerMetadata: {
171
+ google: {
172
+ name: file.name,
173
+ displayName: file.displayName,
174
+ mimeType: file.mimeType,
175
+ sizeBytes: file.sizeBytes,
176
+ state: file.state,
177
+ uri: file.uri,
178
+ ...(file.createTime != null ? { createTime: file.createTime } : {}),
179
+ ...(file.updateTime != null ? { updateTime: file.updateTime } : {}),
180
+ ...(file.expirationTime != null
181
+ ? { expirationTime: file.expirationTime }
182
+ : {}),
183
+ ...(file.sha256Hash != null ? { sha256Hash: file.sha256Hash } : {}),
184
+ },
185
+ },
186
+ };
187
+ }
188
+ }
189
+
190
+ type GoogleFileResource = {
191
+ name: string;
192
+ displayName?: string | null;
193
+ mimeType: string;
194
+ sizeBytes?: string | null;
195
+ createTime?: string | null;
196
+ updateTime?: string | null;
197
+ expirationTime?: string | null;
198
+ sha256Hash?: string | null;
199
+ uri: string;
200
+ state: string;
201
+ };
202
+
203
+ const googleFileResponseSchema = lazySchema(() =>
204
+ zodSchema(
205
+ z.object({
206
+ name: z.string(),
207
+ displayName: z.string().nullish(),
208
+ mimeType: z.string(),
209
+ sizeBytes: z.string().nullish(),
210
+ createTime: z.string().nullish(),
211
+ updateTime: z.string().nullish(),
212
+ expirationTime: z.string().nullish(),
213
+ sha256Hash: z.string().nullish(),
214
+ uri: z.string(),
215
+ state: z.string(),
216
+ }),
217
+ ),
218
+ );
219
+
220
+ const googleFilesUploadOptionsSchema = lazySchema(() =>
221
+ zodSchema(
222
+ z
223
+ .object({
224
+ displayName: z.string().nullish(),
225
+ pollIntervalMs: z.number().positive().nullish(),
226
+ pollTimeoutMs: z.number().positive().nullish(),
227
+ })
228
+ .passthrough(),
229
+ ),
230
+ );
@@ -1,7 +1,7 @@
1
1
  import {
2
- ImageModelV3,
3
- LanguageModelV3Prompt,
4
- SharedV3Warning,
2
+ ImageModelV4,
3
+ LanguageModelV4Prompt,
4
+ SharedV4Warning,
5
5
  } from '@ai-sdk/provider';
6
6
  import {
7
7
  combineHeaders,
@@ -37,8 +37,8 @@ interface GoogleGenerativeAIImageModelConfig {
37
37
  };
38
38
  }
39
39
 
40
- export class GoogleGenerativeAIImageModel implements ImageModelV3 {
41
- readonly specificationVersion = 'v3';
40
+ export class GoogleGenerativeAIImageModel implements ImageModelV4 {
41
+ readonly specificationVersion = 'v4';
42
42
 
43
43
  get maxImagesPerCall(): number {
44
44
  if (this.settings.maxImagesPerCall != null) {
@@ -63,8 +63,8 @@ export class GoogleGenerativeAIImageModel implements ImageModelV3 {
63
63
  ) {}
64
64
 
65
65
  async doGenerate(
66
- options: Parameters<ImageModelV3['doGenerate']>[0],
67
- ): Promise<Awaited<ReturnType<ImageModelV3['doGenerate']>>> {
66
+ options: Parameters<ImageModelV4['doGenerate']>[0],
67
+ ): Promise<Awaited<ReturnType<ImageModelV4['doGenerate']>>> {
68
68
  // Gemini image models use the language model API internally
69
69
  if (isGeminiModel(this.modelId)) {
70
70
  return this.doGenerateGemini(options);
@@ -73,8 +73,8 @@ export class GoogleGenerativeAIImageModel implements ImageModelV3 {
73
73
  }
74
74
 
75
75
  private async doGenerateImagen(
76
- options: Parameters<ImageModelV3['doGenerate']>[0],
77
- ): Promise<Awaited<ReturnType<ImageModelV3['doGenerate']>>> {
76
+ options: Parameters<ImageModelV4['doGenerate']>[0],
77
+ ): Promise<Awaited<ReturnType<ImageModelV4['doGenerate']>>> {
78
78
  const {
79
79
  prompt,
80
80
  n = 1,
@@ -87,7 +87,7 @@ export class GoogleGenerativeAIImageModel implements ImageModelV3 {
87
87
  files,
88
88
  mask,
89
89
  } = options;
90
- const warnings: Array<SharedV3Warning> = [];
90
+ const warnings: Array<SharedV4Warning> = [];
91
91
 
92
92
  // Imagen API endpoints do not support image editing
93
93
  if (files != null && files.length > 0) {
@@ -181,8 +181,8 @@ export class GoogleGenerativeAIImageModel implements ImageModelV3 {
181
181
  }
182
182
 
183
183
  private async doGenerateGemini(
184
- options: Parameters<ImageModelV3['doGenerate']>[0],
185
- ): Promise<Awaited<ReturnType<ImageModelV3['doGenerate']>>> {
184
+ options: Parameters<ImageModelV4['doGenerate']>[0],
185
+ ): Promise<Awaited<ReturnType<ImageModelV4['doGenerate']>>> {
186
186
  const {
187
187
  prompt,
188
188
  n,
@@ -195,7 +195,7 @@ export class GoogleGenerativeAIImageModel implements ImageModelV3 {
195
195
  files,
196
196
  mask,
197
197
  } = options;
198
- const warnings: Array<SharedV3Warning> = [];
198
+ const warnings: Array<SharedV4Warning> = [];
199
199
 
200
200
  // Gemini does not support mask-based inpainting
201
201
  if (mask != null) {
@@ -253,7 +253,7 @@ export class GoogleGenerativeAIImageModel implements ImageModelV3 {
253
253
  }
254
254
  }
255
255
 
256
- const languageModelPrompt: LanguageModelV3Prompt = [
256
+ const languageModelPrompt: LanguageModelV4Prompt = [
257
257
  { role: 'user', content: userContent },
258
258
  ];
259
259