@langchain/google-common 0.0.27 → 0.1.1

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.
@@ -0,0 +1,210 @@
1
+ import { AsyncCaller, AsyncCallerCallOptions, AsyncCallerParams } from "@langchain/core/utils/async_caller";
2
+ import { MediaBlob, BlobStore, BlobStoreOptions } from "./utils/media_core.js";
3
+ import { GoogleConnectionParams, GoogleRawResponse, GoogleResponse } from "../types.js";
4
+ import { GoogleHostConnection, GoogleRawConnection } from "../connection.js";
5
+ import { GoogleAbstractedClient, GoogleAbstractedClientOpsMethod } from "../auth.js";
6
+ export interface GoogleUploadConnectionParams<AuthOptions> extends GoogleConnectionParams<AuthOptions> {
7
+ }
8
+ export declare abstract class GoogleMultipartUploadConnection<CallOptions extends AsyncCallerCallOptions, ResponseType extends GoogleResponse, AuthOptions> extends GoogleHostConnection<CallOptions, ResponseType, AuthOptions> {
9
+ constructor(fields: GoogleConnectionParams<AuthOptions> | undefined, caller: AsyncCaller, client: GoogleAbstractedClient);
10
+ _body(separator: string, data: MediaBlob, metadata: Record<string, unknown>): Promise<string>;
11
+ request(data: MediaBlob, metadata: Record<string, unknown>, options: CallOptions): Promise<ResponseType>;
12
+ }
13
+ export declare abstract class GoogleDownloadConnection<CallOptions extends AsyncCallerCallOptions, ResponseType extends GoogleResponse, AuthOptions> extends GoogleHostConnection<CallOptions, ResponseType, AuthOptions> {
14
+ request(options: CallOptions): Promise<ResponseType>;
15
+ }
16
+ export declare abstract class GoogleDownloadRawConnection<CallOptions extends AsyncCallerCallOptions, AuthOptions> extends GoogleRawConnection<CallOptions, AuthOptions> {
17
+ buildMethod(): GoogleAbstractedClientOpsMethod;
18
+ request(options: CallOptions): Promise<GoogleRawResponse>;
19
+ }
20
+ export interface BlobStoreGoogleParams<AuthOptions> extends GoogleConnectionParams<AuthOptions>, AsyncCallerParams, BlobStoreOptions {
21
+ }
22
+ export declare abstract class BlobStoreGoogle<ResponseType extends GoogleResponse, AuthOptions> extends BlobStore {
23
+ caller: AsyncCaller;
24
+ client: GoogleAbstractedClient;
25
+ constructor(fields?: BlobStoreGoogleParams<AuthOptions>);
26
+ abstract buildClient(fields?: BlobStoreGoogleParams<AuthOptions>): GoogleAbstractedClient;
27
+ abstract buildSetMetadata([key, blob]: [string, MediaBlob]): Record<string, unknown>;
28
+ abstract buildSetConnection([key, blob]: [
29
+ string,
30
+ MediaBlob
31
+ ]): GoogleMultipartUploadConnection<AsyncCallerCallOptions, ResponseType, AuthOptions>;
32
+ _set(keyValuePair: [string, MediaBlob]): Promise<ResponseType>;
33
+ mset(keyValuePairs: [string, MediaBlob][]): Promise<void>;
34
+ abstract buildGetMetadataConnection(key: string): GoogleDownloadConnection<AsyncCallerCallOptions, ResponseType, AuthOptions>;
35
+ _getMetadata(key: string): Promise<Record<string, unknown>>;
36
+ abstract buildGetDataConnection(key: string): GoogleDownloadRawConnection<AsyncCallerCallOptions, AuthOptions>;
37
+ _getData(key: string): Promise<Blob>;
38
+ _getMimetypeFromMetadata(metadata: Record<string, unknown>): string;
39
+ _get(key: string): Promise<MediaBlob | undefined>;
40
+ mget(keys: string[]): Promise<(MediaBlob | undefined)[]>;
41
+ abstract buildDeleteConnection(key: string): GoogleDownloadConnection<AsyncCallerCallOptions, GoogleResponse, AuthOptions>;
42
+ _del(key: string): Promise<void>;
43
+ mdelete(keys: string[]): Promise<void>;
44
+ yieldKeys(_prefix: string | undefined): AsyncGenerator<string>;
45
+ }
46
+ /**
47
+ * Based on https://cloud.google.com/storage/docs/json_api/v1/objects#resource
48
+ */
49
+ export interface GoogleCloudStorageObject extends Record<string, unknown> {
50
+ id?: string;
51
+ name?: string;
52
+ contentType?: string;
53
+ metadata?: Record<string, unknown>;
54
+ }
55
+ export interface GoogleCloudStorageResponse extends GoogleResponse {
56
+ data: GoogleCloudStorageObject;
57
+ }
58
+ export type BucketAndPath = {
59
+ bucket: string;
60
+ path: string;
61
+ };
62
+ export declare class GoogleCloudStorageUri {
63
+ static uriRegexp: RegExp;
64
+ bucket: string;
65
+ path: string;
66
+ constructor(uri: string);
67
+ get uri(): string;
68
+ get isValid(): boolean;
69
+ static uriToBucketAndPath(uri: string): BucketAndPath;
70
+ static isValidUri(uri: string): boolean;
71
+ }
72
+ export interface GoogleCloudStorageConnectionParams {
73
+ uri: string;
74
+ }
75
+ export interface GoogleCloudStorageUploadConnectionParams<AuthOptions> extends GoogleUploadConnectionParams<AuthOptions>, GoogleCloudStorageConnectionParams {
76
+ }
77
+ export declare class GoogleCloudStorageUploadConnection<AuthOptions> extends GoogleMultipartUploadConnection<AsyncCallerCallOptions, GoogleCloudStorageResponse, AuthOptions> {
78
+ uri: GoogleCloudStorageUri;
79
+ constructor(fields: GoogleCloudStorageUploadConnectionParams<AuthOptions>, caller: AsyncCaller, client: GoogleAbstractedClient);
80
+ buildUrl(): Promise<string>;
81
+ }
82
+ export interface GoogleCloudStorageDownloadConnectionParams<AuthOptions> extends GoogleCloudStorageConnectionParams, GoogleConnectionParams<AuthOptions> {
83
+ method: GoogleAbstractedClientOpsMethod;
84
+ alt: "media" | undefined;
85
+ }
86
+ export declare class GoogleCloudStorageDownloadConnection<ResponseType extends GoogleResponse, AuthOptions> extends GoogleDownloadConnection<AsyncCallerCallOptions, ResponseType, AuthOptions> {
87
+ uri: GoogleCloudStorageUri;
88
+ method: GoogleAbstractedClientOpsMethod;
89
+ alt: "media" | undefined;
90
+ constructor(fields: GoogleCloudStorageDownloadConnectionParams<AuthOptions>, caller: AsyncCaller, client: GoogleAbstractedClient);
91
+ buildMethod(): GoogleAbstractedClientOpsMethod;
92
+ buildUrl(): Promise<string>;
93
+ }
94
+ export interface GoogleCloudStorageRawConnectionParams<AuthOptions> extends GoogleCloudStorageConnectionParams, GoogleConnectionParams<AuthOptions> {
95
+ }
96
+ export declare class GoogleCloudStorageRawConnection<AuthOptions> extends GoogleDownloadRawConnection<AsyncCallerCallOptions, AuthOptions> {
97
+ uri: GoogleCloudStorageUri;
98
+ constructor(fields: GoogleCloudStorageRawConnectionParams<AuthOptions>, caller: AsyncCaller, client: GoogleAbstractedClient);
99
+ buildUrl(): Promise<string>;
100
+ }
101
+ export interface BlobStoreGoogleCloudStorageBaseParams<AuthOptions> extends BlobStoreGoogleParams<AuthOptions> {
102
+ uriPrefix: GoogleCloudStorageUri;
103
+ }
104
+ export declare abstract class BlobStoreGoogleCloudStorageBase<AuthOptions> extends BlobStoreGoogle<GoogleCloudStorageResponse, AuthOptions> {
105
+ params: BlobStoreGoogleCloudStorageBaseParams<AuthOptions>;
106
+ constructor(fields: BlobStoreGoogleCloudStorageBaseParams<AuthOptions>);
107
+ buildSetConnection([key, _blob]: [
108
+ string,
109
+ MediaBlob
110
+ ]): GoogleMultipartUploadConnection<AsyncCallerCallOptions, GoogleCloudStorageResponse, AuthOptions>;
111
+ buildSetMetadata([key, blob]: [string, MediaBlob]): Record<string, unknown>;
112
+ buildGetMetadataConnection(key: string): GoogleDownloadConnection<AsyncCallerCallOptions, GoogleCloudStorageResponse, AuthOptions>;
113
+ buildGetDataConnection(key: string): GoogleDownloadRawConnection<AsyncCallerCallOptions, AuthOptions>;
114
+ buildDeleteConnection(key: string): GoogleDownloadConnection<AsyncCallerCallOptions, GoogleResponse, AuthOptions>;
115
+ }
116
+ export type AIStudioFileState = "PROCESSING" | "ACTIVE" | "FAILED" | "STATE_UNSPECIFIED";
117
+ export type AIStudioFileVideoMetadata = {
118
+ videoMetadata: {
119
+ videoDuration: string;
120
+ };
121
+ };
122
+ export type AIStudioFileMetadata = AIStudioFileVideoMetadata;
123
+ export interface AIStudioFileObject {
124
+ name?: string;
125
+ displayName?: string;
126
+ mimeType?: string;
127
+ sizeBytes?: string;
128
+ createTime?: string;
129
+ updateTime?: string;
130
+ expirationTime?: string;
131
+ sha256Hash?: string;
132
+ uri?: string;
133
+ state?: AIStudioFileState;
134
+ error?: {
135
+ code: number;
136
+ message: string;
137
+ details: Record<string, unknown>[];
138
+ };
139
+ metadata?: AIStudioFileMetadata;
140
+ }
141
+ export declare class AIStudioMediaBlob extends MediaBlob {
142
+ _valueAsDate(value: string): Date;
143
+ _metadataFieldAsDate(field: string): Date;
144
+ get createDate(): Date;
145
+ get updateDate(): Date;
146
+ get expirationDate(): Date;
147
+ get isExpired(): boolean;
148
+ }
149
+ export interface AIStudioFileGetResponse extends GoogleResponse {
150
+ data: AIStudioFileObject;
151
+ }
152
+ export interface AIStudioFileSaveResponse extends GoogleResponse {
153
+ data: {
154
+ file: AIStudioFileObject;
155
+ };
156
+ }
157
+ export interface AIStudioFileListResponse extends GoogleResponse {
158
+ data: {
159
+ files: AIStudioFileObject[];
160
+ nextPageToken: string;
161
+ };
162
+ }
163
+ export type AIStudioFileResponse = AIStudioFileGetResponse | AIStudioFileSaveResponse | AIStudioFileListResponse;
164
+ export interface AIStudioFileConnectionParams {
165
+ }
166
+ export interface AIStudioFileUploadConnectionParams<AuthOptions> extends GoogleUploadConnectionParams<AuthOptions>, AIStudioFileConnectionParams {
167
+ }
168
+ export declare class AIStudioFileUploadConnection<AuthOptions> extends GoogleMultipartUploadConnection<AsyncCallerCallOptions, AIStudioFileSaveResponse, AuthOptions> {
169
+ apiVersion: string;
170
+ buildUrl(): Promise<string>;
171
+ }
172
+ export interface AIStudioFileDownloadConnectionParams<AuthOptions> extends AIStudioFileConnectionParams, GoogleConnectionParams<AuthOptions> {
173
+ method: GoogleAbstractedClientOpsMethod;
174
+ name: string;
175
+ }
176
+ export declare class AIStudioFileDownloadConnection<ResponseType extends GoogleResponse, AuthOptions> extends GoogleDownloadConnection<AsyncCallerCallOptions, ResponseType, AuthOptions> {
177
+ method: GoogleAbstractedClientOpsMethod;
178
+ name: string;
179
+ apiVersion: string;
180
+ constructor(fields: AIStudioFileDownloadConnectionParams<AuthOptions>, caller: AsyncCaller, client: GoogleAbstractedClient);
181
+ buildMethod(): GoogleAbstractedClientOpsMethod;
182
+ buildUrl(): Promise<string>;
183
+ }
184
+ export interface BlobStoreAIStudioFileBaseParams<AuthOptions> extends BlobStoreGoogleParams<AuthOptions> {
185
+ retryTime?: number;
186
+ }
187
+ export declare abstract class BlobStoreAIStudioFileBase<AuthOptions> extends BlobStoreGoogle<AIStudioFileResponse, AuthOptions> {
188
+ params?: BlobStoreAIStudioFileBaseParams<AuthOptions>;
189
+ retryTime: number;
190
+ constructor(fields?: BlobStoreAIStudioFileBaseParams<AuthOptions>);
191
+ _pathToName(path: string): string;
192
+ abstract buildAbstractedClient(fields?: BlobStoreGoogleParams<AuthOptions>): GoogleAbstractedClient;
193
+ buildApiKeyClient(apiKey: string): GoogleAbstractedClient;
194
+ buildApiKey(fields?: BlobStoreGoogleParams<AuthOptions>): string | undefined;
195
+ buildClient(fields?: BlobStoreGoogleParams<AuthOptions>): GoogleAbstractedClient;
196
+ _regetMetadata(key: string): Promise<AIStudioFileObject>;
197
+ _set([key, blob]: [
198
+ string,
199
+ MediaBlob
200
+ ]): Promise<AIStudioFileSaveResponse>;
201
+ buildSetConnection([_key, _blob]: [
202
+ string,
203
+ MediaBlob
204
+ ]): GoogleMultipartUploadConnection<AsyncCallerCallOptions, AIStudioFileResponse, AuthOptions>;
205
+ buildSetMetadata([_key, _blob]: [string, MediaBlob]): Record<string, unknown>;
206
+ buildGetMetadataConnection(key: string): GoogleDownloadConnection<AsyncCallerCallOptions, AIStudioFileResponse, AuthOptions>;
207
+ buildGetDataConnection(_key: string): GoogleDownloadRawConnection<AsyncCallerCallOptions, AuthOptions>;
208
+ _get(key: string): Promise<MediaBlob | undefined>;
209
+ buildDeleteConnection(key: string): GoogleDownloadConnection<AsyncCallerCallOptions, AIStudioFileResponse, AuthOptions>;
210
+ }
@@ -0,0 +1,478 @@
1
+ import { AsyncCaller, } from "@langchain/core/utils/async_caller";
2
+ import { getEnvironmentVariable } from "@langchain/core/utils/env";
3
+ import { MediaBlob, BlobStore, } from "./utils/media_core.js";
4
+ import { GoogleHostConnection, GoogleRawConnection } from "../connection.js";
5
+ import { ApiKeyGoogleAuth, } from "../auth.js";
6
+ export class GoogleMultipartUploadConnection extends GoogleHostConnection {
7
+ constructor(fields, caller, client) {
8
+ super(fields, caller, client);
9
+ }
10
+ async _body(separator, data, metadata) {
11
+ const contentType = data.mimetype;
12
+ const { encoded, encoding } = await data.encode();
13
+ const body = [
14
+ `--${separator}`,
15
+ "Content-Type: application/json; charset=UTF-8",
16
+ "",
17
+ JSON.stringify(metadata),
18
+ "",
19
+ `--${separator}`,
20
+ `Content-Type: ${contentType}`,
21
+ `Content-Transfer-Encoding: ${encoding}`,
22
+ "",
23
+ encoded,
24
+ `--${separator}--`,
25
+ ];
26
+ return body.join("\n");
27
+ }
28
+ async request(data, metadata, options) {
29
+ const separator = `separator-${Date.now()}`;
30
+ const body = await this._body(separator, data, metadata);
31
+ const requestHeaders = {
32
+ "Content-Type": `multipart/related; boundary=${separator}`,
33
+ "X-Goog-Upload-Protocol": "multipart",
34
+ };
35
+ const response = this._request(body, options, requestHeaders);
36
+ return response;
37
+ }
38
+ }
39
+ export class GoogleDownloadConnection extends GoogleHostConnection {
40
+ async request(options) {
41
+ return this._request(undefined, options);
42
+ }
43
+ }
44
+ export class GoogleDownloadRawConnection extends GoogleRawConnection {
45
+ buildMethod() {
46
+ return "GET";
47
+ }
48
+ async request(options) {
49
+ return this._request(undefined, options);
50
+ }
51
+ }
52
+ export class BlobStoreGoogle extends BlobStore {
53
+ constructor(fields) {
54
+ super(fields);
55
+ Object.defineProperty(this, "caller", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ Object.defineProperty(this, "client", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
67
+ this.caller = new AsyncCaller(fields ?? {});
68
+ this.client = this.buildClient(fields);
69
+ }
70
+ async _set(keyValuePair) {
71
+ const [, blob] = keyValuePair;
72
+ const setMetadata = this.buildSetMetadata(keyValuePair);
73
+ const metadata = setMetadata;
74
+ const options = {};
75
+ const connection = this.buildSetConnection(keyValuePair);
76
+ const response = await connection.request(blob, metadata, options);
77
+ return response;
78
+ }
79
+ async mset(keyValuePairs) {
80
+ const ret = keyValuePairs.map((keyValue) => this._set(keyValue));
81
+ await Promise.all(ret);
82
+ }
83
+ async _getMetadata(key) {
84
+ const connection = this.buildGetMetadataConnection(key);
85
+ const options = {};
86
+ const response = await connection.request(options);
87
+ return response.data;
88
+ }
89
+ async _getData(key) {
90
+ const connection = this.buildGetDataConnection(key);
91
+ const options = {};
92
+ const response = await connection.request(options);
93
+ return response.data;
94
+ }
95
+ _getMimetypeFromMetadata(metadata) {
96
+ return metadata.contentType;
97
+ }
98
+ async _get(key) {
99
+ const metadata = await this._getMetadata(key);
100
+ const data = await this._getData(key);
101
+ if (data && metadata) {
102
+ const ret = await MediaBlob.fromBlob(data, { metadata, path: key });
103
+ return ret;
104
+ }
105
+ else {
106
+ return undefined;
107
+ }
108
+ }
109
+ async mget(keys) {
110
+ const ret = keys.map((key) => this._get(key));
111
+ return await Promise.all(ret);
112
+ }
113
+ async _del(key) {
114
+ const connection = this.buildDeleteConnection(key);
115
+ const options = {};
116
+ await connection.request(options);
117
+ }
118
+ async mdelete(keys) {
119
+ const ret = keys.map((key) => this._del(key));
120
+ await Promise.all(ret);
121
+ }
122
+ // eslint-disable-next-line require-yield
123
+ async *yieldKeys(_prefix) {
124
+ // TODO: Implement. Most have an implementation that uses nextToken.
125
+ throw new Error("yieldKeys is not implemented");
126
+ }
127
+ }
128
+ export class GoogleCloudStorageUri {
129
+ constructor(uri) {
130
+ Object.defineProperty(this, "bucket", {
131
+ enumerable: true,
132
+ configurable: true,
133
+ writable: true,
134
+ value: void 0
135
+ });
136
+ Object.defineProperty(this, "path", {
137
+ enumerable: true,
138
+ configurable: true,
139
+ writable: true,
140
+ value: void 0
141
+ });
142
+ const bucketAndPath = GoogleCloudStorageUri.uriToBucketAndPath(uri);
143
+ this.bucket = bucketAndPath.bucket;
144
+ this.path = bucketAndPath.path;
145
+ }
146
+ get uri() {
147
+ return `gs://${this.bucket}/${this.path}`;
148
+ }
149
+ get isValid() {
150
+ return (typeof this.bucket !== "undefined" && typeof this.path !== "undefined");
151
+ }
152
+ static uriToBucketAndPath(uri) {
153
+ const match = this.uriRegexp.exec(uri);
154
+ if (!match) {
155
+ throw new Error(`Invalid gs:// URI: ${uri}`);
156
+ }
157
+ return {
158
+ bucket: match[1],
159
+ path: match[2],
160
+ };
161
+ }
162
+ static isValidUri(uri) {
163
+ return this.uriRegexp.test(uri);
164
+ }
165
+ }
166
+ Object.defineProperty(GoogleCloudStorageUri, "uriRegexp", {
167
+ enumerable: true,
168
+ configurable: true,
169
+ writable: true,
170
+ value: /gs:\/\/([a-z0-9][a-z0-9._-]+[a-z0-9])\/(.*)/
171
+ });
172
+ export class GoogleCloudStorageUploadConnection extends GoogleMultipartUploadConnection {
173
+ constructor(fields, caller, client) {
174
+ super(fields, caller, client);
175
+ Object.defineProperty(this, "uri", {
176
+ enumerable: true,
177
+ configurable: true,
178
+ writable: true,
179
+ value: void 0
180
+ });
181
+ this.uri = new GoogleCloudStorageUri(fields.uri);
182
+ }
183
+ async buildUrl() {
184
+ return `https://storage.googleapis.com/upload/storage/${this.apiVersion}/b/${this.uri.bucket}/o?uploadType=multipart`;
185
+ }
186
+ }
187
+ export class GoogleCloudStorageDownloadConnection extends GoogleDownloadConnection {
188
+ constructor(fields, caller, client) {
189
+ super(fields, caller, client);
190
+ Object.defineProperty(this, "uri", {
191
+ enumerable: true,
192
+ configurable: true,
193
+ writable: true,
194
+ value: void 0
195
+ });
196
+ Object.defineProperty(this, "method", {
197
+ enumerable: true,
198
+ configurable: true,
199
+ writable: true,
200
+ value: void 0
201
+ });
202
+ Object.defineProperty(this, "alt", {
203
+ enumerable: true,
204
+ configurable: true,
205
+ writable: true,
206
+ value: void 0
207
+ });
208
+ this.uri = new GoogleCloudStorageUri(fields.uri);
209
+ this.method = fields.method;
210
+ this.alt = fields.alt;
211
+ }
212
+ buildMethod() {
213
+ return this.method;
214
+ }
215
+ async buildUrl() {
216
+ const path = encodeURIComponent(this.uri.path);
217
+ const ret = `https://storage.googleapis.com/storage/${this.apiVersion}/b/${this.uri.bucket}/o/${path}`;
218
+ return this.alt ? `${ret}?alt=${this.alt}` : ret;
219
+ }
220
+ }
221
+ export class GoogleCloudStorageRawConnection extends GoogleDownloadRawConnection {
222
+ constructor(fields, caller, client) {
223
+ super(fields, caller, client);
224
+ Object.defineProperty(this, "uri", {
225
+ enumerable: true,
226
+ configurable: true,
227
+ writable: true,
228
+ value: void 0
229
+ });
230
+ this.uri = new GoogleCloudStorageUri(fields.uri);
231
+ }
232
+ async buildUrl() {
233
+ const path = encodeURIComponent(this.uri.path);
234
+ const ret = `https://storage.googleapis.com/storage/${this.apiVersion}/b/${this.uri.bucket}/o/${path}?alt=media`;
235
+ return ret;
236
+ }
237
+ }
238
+ export class BlobStoreGoogleCloudStorageBase extends BlobStoreGoogle {
239
+ constructor(fields) {
240
+ super(fields);
241
+ Object.defineProperty(this, "params", {
242
+ enumerable: true,
243
+ configurable: true,
244
+ writable: true,
245
+ value: void 0
246
+ });
247
+ this.params = fields;
248
+ this.defaultStoreOptions = {
249
+ ...this.defaultStoreOptions,
250
+ pathPrefix: fields.uriPrefix.uri,
251
+ };
252
+ }
253
+ buildSetConnection([key, _blob]) {
254
+ const params = {
255
+ ...this.params,
256
+ uri: key,
257
+ };
258
+ return new GoogleCloudStorageUploadConnection(params, this.caller, this.client);
259
+ }
260
+ buildSetMetadata([key, blob]) {
261
+ const uri = new GoogleCloudStorageUri(key);
262
+ const ret = {
263
+ name: uri.path,
264
+ metadata: blob.metadata,
265
+ contentType: blob.mimetype,
266
+ };
267
+ return ret;
268
+ }
269
+ buildGetMetadataConnection(key) {
270
+ const params = {
271
+ uri: key,
272
+ method: "GET",
273
+ alt: undefined,
274
+ };
275
+ return new GoogleCloudStorageDownloadConnection(params, this.caller, this.client);
276
+ }
277
+ buildGetDataConnection(key) {
278
+ const params = {
279
+ uri: key,
280
+ };
281
+ return new GoogleCloudStorageRawConnection(params, this.caller, this.client);
282
+ }
283
+ buildDeleteConnection(key) {
284
+ const params = {
285
+ uri: key,
286
+ method: "DELETE",
287
+ alt: undefined,
288
+ };
289
+ return new GoogleCloudStorageDownloadConnection(params, this.caller, this.client);
290
+ }
291
+ }
292
+ export class AIStudioMediaBlob extends MediaBlob {
293
+ _valueAsDate(value) {
294
+ if (!value) {
295
+ return new Date(0);
296
+ }
297
+ return new Date(value);
298
+ }
299
+ _metadataFieldAsDate(field) {
300
+ return this._valueAsDate(this.metadata?.[field]);
301
+ }
302
+ get createDate() {
303
+ return this._metadataFieldAsDate("createTime");
304
+ }
305
+ get updateDate() {
306
+ return this._metadataFieldAsDate("updateTime");
307
+ }
308
+ get expirationDate() {
309
+ return this._metadataFieldAsDate("expirationTime");
310
+ }
311
+ get isExpired() {
312
+ const now = new Date().toISOString();
313
+ const exp = this.metadata?.expirationTime ?? now;
314
+ return exp <= now;
315
+ }
316
+ }
317
+ export class AIStudioFileUploadConnection extends GoogleMultipartUploadConnection {
318
+ constructor() {
319
+ super(...arguments);
320
+ Object.defineProperty(this, "apiVersion", {
321
+ enumerable: true,
322
+ configurable: true,
323
+ writable: true,
324
+ value: "v1beta"
325
+ });
326
+ }
327
+ async buildUrl() {
328
+ return `https://generativelanguage.googleapis.com/upload/${this.apiVersion}/files`;
329
+ }
330
+ }
331
+ export class AIStudioFileDownloadConnection extends GoogleDownloadConnection {
332
+ constructor(fields, caller, client) {
333
+ super(fields, caller, client);
334
+ Object.defineProperty(this, "method", {
335
+ enumerable: true,
336
+ configurable: true,
337
+ writable: true,
338
+ value: void 0
339
+ });
340
+ Object.defineProperty(this, "name", {
341
+ enumerable: true,
342
+ configurable: true,
343
+ writable: true,
344
+ value: void 0
345
+ });
346
+ Object.defineProperty(this, "apiVersion", {
347
+ enumerable: true,
348
+ configurable: true,
349
+ writable: true,
350
+ value: "v1beta"
351
+ });
352
+ this.method = fields.method;
353
+ this.name = fields.name;
354
+ }
355
+ buildMethod() {
356
+ return this.method;
357
+ }
358
+ async buildUrl() {
359
+ return `https://generativelanguage.googleapis.com/${this.apiVersion}/files/${this.name}`;
360
+ }
361
+ }
362
+ export class BlobStoreAIStudioFileBase extends BlobStoreGoogle {
363
+ constructor(fields) {
364
+ const params = {
365
+ defaultStoreOptions: {
366
+ pathPrefix: "https://generativelanguage.googleapis.com/v1beta/files/",
367
+ actionIfInvalid: "removePath",
368
+ },
369
+ ...fields,
370
+ };
371
+ super(params);
372
+ Object.defineProperty(this, "params", {
373
+ enumerable: true,
374
+ configurable: true,
375
+ writable: true,
376
+ value: void 0
377
+ });
378
+ Object.defineProperty(this, "retryTime", {
379
+ enumerable: true,
380
+ configurable: true,
381
+ writable: true,
382
+ value: 1000
383
+ });
384
+ this.params = params;
385
+ this.retryTime = params?.retryTime ?? this.retryTime ?? 1000;
386
+ }
387
+ _pathToName(path) {
388
+ return path.split("/").pop() ?? path;
389
+ }
390
+ buildApiKeyClient(apiKey) {
391
+ return new ApiKeyGoogleAuth(apiKey);
392
+ }
393
+ buildApiKey(fields) {
394
+ return fields?.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
395
+ }
396
+ buildClient(fields) {
397
+ const apiKey = this.buildApiKey(fields);
398
+ if (apiKey) {
399
+ return this.buildApiKeyClient(apiKey);
400
+ }
401
+ else {
402
+ // TODO: Test that you can use OAuth to access
403
+ return this.buildAbstractedClient(fields);
404
+ }
405
+ }
406
+ async _regetMetadata(key) {
407
+ // Sleep for some time period
408
+ // eslint-disable-next-line no-promise-executor-return
409
+ await new Promise((resolve) => setTimeout(resolve, this.retryTime));
410
+ // Fetch the latest metadata
411
+ return this._getMetadata(key);
412
+ }
413
+ async _set([key, blob]) {
414
+ const response = (await super._set([
415
+ key,
416
+ blob,
417
+ ]));
418
+ let file = response.data?.file ?? { state: "FAILED" };
419
+ while (file.state === "PROCESSING" && file.uri && this.retryTime > 0) {
420
+ file = await this._regetMetadata(file.uri);
421
+ }
422
+ // The response should contain the name (and valid URI), so we need to
423
+ // update the blob with this. We can't return a new blob, since mset()
424
+ // doesn't return anything.
425
+ /* eslint-disable no-param-reassign */
426
+ blob.path = file.uri;
427
+ blob.metadata = {
428
+ ...blob.metadata,
429
+ ...file,
430
+ };
431
+ /* eslint-enable no-param-reassign */
432
+ return response;
433
+ }
434
+ buildSetConnection([_key, _blob]) {
435
+ return new AIStudioFileUploadConnection(this.params, this.caller, this.client);
436
+ }
437
+ buildSetMetadata([_key, _blob]) {
438
+ return {};
439
+ }
440
+ buildGetMetadataConnection(key) {
441
+ const params = {
442
+ ...this.params,
443
+ method: "GET",
444
+ name: this._pathToName(key),
445
+ };
446
+ return new AIStudioFileDownloadConnection(params, this.caller, this.client);
447
+ }
448
+ buildGetDataConnection(_key) {
449
+ throw new Error("AI Studio File API does not provide data");
450
+ }
451
+ async _get(key) {
452
+ const metadata = await this._getMetadata(key);
453
+ if (metadata) {
454
+ const contentType = metadata?.mimeType ?? "application/octet-stream";
455
+ // TODO - Get the actual data (and other metadata) from an optional backing store
456
+ const data = {
457
+ value: "",
458
+ type: contentType,
459
+ };
460
+ return new MediaBlob({
461
+ path: key,
462
+ data,
463
+ metadata,
464
+ });
465
+ }
466
+ else {
467
+ return undefined;
468
+ }
469
+ }
470
+ buildDeleteConnection(key) {
471
+ const params = {
472
+ ...this.params,
473
+ method: "DELETE",
474
+ name: this._pathToName(key),
475
+ };
476
+ return new AIStudioFileDownloadConnection(params, this.caller, this.client);
477
+ }
478
+ }