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