-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfiles.ts
312 lines (293 loc) · 9.47 KB
/
files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
// Code generated by the Google Gen AI SDK generator DO NOT EDIT.
import {ApiClient} from './_api_client';
import * as common from './_common';
import {BaseModule} from './_common';
import * as converters from './converters/_files_converters';
import {PagedItem, Pager} from './pagers';
import * as types from './types';
export class Files extends BaseModule {
constructor(private readonly apiClient: ApiClient) {
super();
}
/**
* Lists all current project files from the service.
*
* @param params - The parameters for the list request
* @return The paginated results of the list of files
*
* @example
* The following code prints the names of all files from the service, the
* size of each page is 10.
*
* ```ts
* const listResponse = await ai.files.list({config: {'pageSize': 10}});
* for await (const file of listResponse) {
* console.log(file.name);
* }
* ```
*/
list = async (
params: types.ListFilesParameters = {},
): Promise<Pager<types.File>> => {
return new Pager<types.File>(
PagedItem.PAGED_ITEM_FILES,
(x: types.ListFilesParameters) => this.listInternal(x),
await this.listInternal(params),
params,
);
};
/**
* Uploads a file asynchronously to the Gemini API.
* This method is not available in Vertex AI.
* Supported upload sources:
* - Node.js: File path (string) or Blob object.
* - Browser: Blob object (e.g., File).
*
* @remarks
* The `mimeType` can be specified in the `config` parameter. If omitted:
* - For file path (string) inputs, the `mimeType` will be inferred from the
* file extension.
* - For Blob object inputs, the `mimeType` will be set to the Blob's `type`
* property.
* Somex eamples for file extension to mimeType mapping:
* .txt -> text/plain
* .json -> application/json
* .jpg -> image/jpeg
* .png -> image/png
* .mp3 -> audio/mpeg
* .mp4 -> video/mp4
*
* This section can contain multiple paragraphs and code examples.
*
* @param params - Optional parameters specified in the
* `types.UploadFileParameters` interface.
* @see {@link types.UploadFileParameters#config} for the optional
* config in the parameters.
* @return A promise that resolves to a `types.File` object.
* @throws An error if called on a Vertex AI client.
* @throws An error if the `mimeType` is not provided and can not be inferred,
* the `mimeType` can be provided in the `params.config` parameter.
* @throws An error occurs if a suitable upload location cannot be established.
*
* @example
* The following code uploads a file to Gemini API.
*
* ```ts
* const file = await ai.files.upload({file: 'file.txt', config: {
* mimeType: 'text/plain',
* }});
* console.log(file.name);
* ```
*/
async upload(params: types.UploadFileParameters): Promise<types.File> {
if (this.apiClient.isVertexAI()) {
throw new Error(
'Vertex AI does not support uploading files. You can share files through a GCS bucket.',
);
}
return this.apiClient
.uploadFile(params.file, params.config)
.then((response) => {
const file = converters.fileFromMldev(this.apiClient, response);
return file as types.File;
});
}
private async listInternal(
params: types.ListFilesParameters,
): Promise<types.ListFilesResponse> {
let response: Promise<types.ListFilesResponse>;
let path: string = '';
let queryParams: Record<string, string> = {};
if (this.apiClient.isVertexAI()) {
throw new Error(
'This method is only supported by the Gemini Developer API.',
);
} else {
const body = converters.listFilesParametersToMldev(
this.apiClient,
params,
);
path = common.formatMap('files', body['_url'] as Record<string, unknown>);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];
response = this.apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.ListFilesResponse>;
return response.then((apiResponse) => {
const resp = converters.listFilesResponseFromMldev(
this.apiClient,
apiResponse,
);
const typedResp = new types.ListFilesResponse();
Object.assign(typedResp, resp);
return typedResp;
});
}
}
private async createInternal(
params: types.CreateFileParameters,
): Promise<types.CreateFileResponse> {
let response: Promise<types.CreateFileResponse>;
let path: string = '';
let queryParams: Record<string, string> = {};
if (this.apiClient.isVertexAI()) {
throw new Error(
'This method is only supported by the Gemini Developer API.',
);
} else {
const body = converters.createFileParametersToMldev(
this.apiClient,
params,
);
path = common.formatMap(
'upload/v1beta/files',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];
response = this.apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.CreateFileResponse>;
return response.then(() => {
const resp = converters.createFileResponseFromMldev();
const typedResp = new types.CreateFileResponse();
Object.assign(typedResp, resp);
return typedResp;
});
}
}
/**
* Retrieves the file information from the service.
*
* @param params - The parameters for the get request
* @return The Promise that resolves to the types.File object requested.
*
* @example
* ```ts
* const config: GetFileParameters = {
* name: fileName,
* };
* file = await ai.files.get(config);
* console.log(file.name);
* ```
*/
async get(params: types.GetFileParameters): Promise<types.File> {
let response: Promise<types.File>;
let path: string = '';
let queryParams: Record<string, string> = {};
if (this.apiClient.isVertexAI()) {
throw new Error(
'This method is only supported by the Gemini Developer API.',
);
} else {
const body = converters.getFileParametersToMldev(this.apiClient, params);
path = common.formatMap(
'files/{file}',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];
response = this.apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.File>;
return response.then((apiResponse) => {
const resp = converters.fileFromMldev(this.apiClient, apiResponse);
return resp as types.File;
});
}
}
/**
* Deletes a remotely stored file.
*
* @param params - The parameters for the delete request.
* @return The DeleteFileResponse, the response for the delete method.
*
* @example
* The following code deletes an example file named "files/mehozpxf877d".
*
* ```ts
* await ai.files.delete({name: file.name});
* ```
*/
async delete(
params: types.DeleteFileParameters,
): Promise<types.DeleteFileResponse> {
let response: Promise<types.DeleteFileResponse>;
let path: string = '';
let queryParams: Record<string, string> = {};
if (this.apiClient.isVertexAI()) {
throw new Error(
'This method is only supported by the Gemini Developer API.',
);
} else {
const body = converters.deleteFileParametersToMldev(
this.apiClient,
params,
);
path = common.formatMap(
'files/{file}',
body['_url'] as Record<string, unknown>,
);
queryParams = body['_query'] as Record<string, string>;
delete body['config'];
delete body['_url'];
delete body['_query'];
response = this.apiClient
.request({
path: path,
queryParams: queryParams,
body: JSON.stringify(body),
httpMethod: 'DELETE',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
}) as Promise<types.DeleteFileResponse>;
return response.then(() => {
const resp = converters.deleteFileResponseFromMldev();
const typedResp = new types.DeleteFileResponse();
Object.assign(typedResp, resp);
return typedResp;
});
}
}
}