@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.
Files changed (173) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/LICENSE +6 -6
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/auth.cjs +82 -116
  5. package/dist/auth.cjs.map +1 -0
  6. package/dist/auth.d.cts +46 -0
  7. package/dist/auth.d.cts.map +1 -0
  8. package/dist/auth.d.ts +41 -36
  9. package/dist/auth.d.ts.map +1 -0
  10. package/dist/auth.js +80 -110
  11. package/dist/auth.js.map +1 -0
  12. package/dist/chat_models.cjs +264 -466
  13. package/dist/chat_models.cjs.map +1 -0
  14. package/dist/chat_models.d.cts +109 -0
  15. package/dist/chat_models.d.cts.map +1 -0
  16. package/dist/chat_models.d.ts +98 -73
  17. package/dist/chat_models.d.ts.map +1 -0
  18. package/dist/chat_models.js +258 -457
  19. package/dist/chat_models.js.map +1 -0
  20. package/dist/connection.cjs +321 -466
  21. package/dist/connection.cjs.map +1 -0
  22. package/dist/connection.d.cts +109 -0
  23. package/dist/connection.d.cts.map +1 -0
  24. package/dist/connection.d.ts +98 -91
  25. package/dist/connection.d.ts.map +1 -0
  26. package/dist/connection.js +317 -459
  27. package/dist/connection.js.map +1 -0
  28. package/dist/embeddings.cjs +135 -186
  29. package/dist/embeddings.cjs.map +1 -0
  30. package/dist/embeddings.d.cts +44 -0
  31. package/dist/embeddings.d.cts.map +1 -0
  32. package/dist/embeddings.d.ts +38 -32
  33. package/dist/embeddings.d.ts.map +1 -0
  34. package/dist/embeddings.js +133 -181
  35. package/dist/embeddings.js.map +1 -0
  36. package/dist/experimental/media.cjs +380 -482
  37. package/dist/experimental/media.cjs.map +1 -0
  38. package/dist/experimental/media.d.cts +198 -0
  39. package/dist/experimental/media.d.cts.map +1 -0
  40. package/dist/experimental/media.d.ts +190 -202
  41. package/dist/experimental/media.d.ts.map +1 -0
  42. package/dist/experimental/media.js +369 -468
  43. package/dist/experimental/media.js.map +1 -0
  44. package/dist/experimental/utils/media_core.cjs +403 -517
  45. package/dist/experimental/utils/media_core.cjs.map +1 -0
  46. package/dist/experimental/utils/media_core.d.cts +215 -0
  47. package/dist/experimental/utils/media_core.d.cts.map +1 -0
  48. package/dist/experimental/utils/media_core.d.ts +171 -165
  49. package/dist/experimental/utils/media_core.d.ts.map +1 -0
  50. package/dist/experimental/utils/media_core.js +395 -506
  51. package/dist/experimental/utils/media_core.js.map +1 -0
  52. package/dist/index.cjs +58 -27
  53. package/dist/index.d.cts +13 -0
  54. package/dist/index.d.ts +13 -11
  55. package/dist/index.js +13 -11
  56. package/dist/llms.cjs +157 -244
  57. package/dist/llms.cjs.map +1 -0
  58. package/dist/llms.d.cts +72 -0
  59. package/dist/llms.d.cts.map +1 -0
  60. package/dist/llms.d.ts +64 -54
  61. package/dist/llms.d.ts.map +1 -0
  62. package/dist/llms.js +154 -238
  63. package/dist/llms.js.map +1 -0
  64. package/dist/output_parsers.cjs +148 -173
  65. package/dist/output_parsers.cjs.map +1 -0
  66. package/dist/output_parsers.d.cts +53 -0
  67. package/dist/output_parsers.d.cts.map +1 -0
  68. package/dist/output_parsers.d.ts +46 -42
  69. package/dist/output_parsers.d.ts.map +1 -0
  70. package/dist/output_parsers.js +146 -168
  71. package/dist/output_parsers.js.map +1 -0
  72. package/dist/profiles.cjs +219 -0
  73. package/dist/profiles.cjs.map +1 -0
  74. package/dist/profiles.js +218 -0
  75. package/dist/profiles.js.map +1 -0
  76. package/dist/types-anthropic.d.cts +229 -0
  77. package/dist/types-anthropic.d.cts.map +1 -0
  78. package/dist/types-anthropic.d.ts +221 -215
  79. package/dist/types-anthropic.d.ts.map +1 -0
  80. package/dist/types.cjs +51 -62
  81. package/dist/types.cjs.map +1 -0
  82. package/dist/types.d.cts +748 -0
  83. package/dist/types.d.cts.map +1 -0
  84. package/dist/types.d.ts +669 -656
  85. package/dist/types.d.ts.map +1 -0
  86. package/dist/types.js +46 -45
  87. package/dist/types.js.map +1 -0
  88. package/dist/utils/anthropic.cjs +598 -821
  89. package/dist/utils/anthropic.cjs.map +1 -0
  90. package/dist/utils/anthropic.js +597 -818
  91. package/dist/utils/anthropic.js.map +1 -0
  92. package/dist/utils/common.cjs +130 -211
  93. package/dist/utils/common.cjs.map +1 -0
  94. package/dist/utils/common.d.cts +13 -0
  95. package/dist/utils/common.d.cts.map +1 -0
  96. package/dist/utils/common.d.ts +12 -7
  97. package/dist/utils/common.d.ts.map +1 -0
  98. package/dist/utils/common.js +128 -207
  99. package/dist/utils/common.js.map +1 -0
  100. package/dist/utils/failed_handler.cjs +28 -30
  101. package/dist/utils/failed_handler.cjs.map +1 -0
  102. package/dist/utils/failed_handler.d.cts +9 -0
  103. package/dist/utils/failed_handler.d.cts.map +1 -0
  104. package/dist/utils/failed_handler.d.ts +8 -2
  105. package/dist/utils/failed_handler.d.ts.map +1 -0
  106. package/dist/utils/failed_handler.js +28 -28
  107. package/dist/utils/failed_handler.js.map +1 -0
  108. package/dist/utils/gemini.cjs +1020 -1488
  109. package/dist/utils/gemini.cjs.map +1 -0
  110. package/dist/utils/gemini.d.cts +51 -0
  111. package/dist/utils/gemini.d.cts.map +1 -0
  112. package/dist/utils/gemini.d.ts +51 -48
  113. package/dist/utils/gemini.d.ts.map +1 -0
  114. package/dist/utils/gemini.js +1015 -1479
  115. package/dist/utils/gemini.js.map +1 -0
  116. package/dist/utils/index.cjs +38 -23
  117. package/dist/utils/index.d.cts +8 -0
  118. package/dist/utils/index.d.ts +8 -7
  119. package/dist/utils/index.js +8 -7
  120. package/dist/utils/palm.d.cts +11 -0
  121. package/dist/utils/palm.d.cts.map +1 -0
  122. package/dist/utils/palm.d.ts +9 -4
  123. package/dist/utils/palm.d.ts.map +1 -0
  124. package/dist/utils/safety.cjs +13 -22
  125. package/dist/utils/safety.cjs.map +1 -0
  126. package/dist/utils/safety.d.cts +12 -0
  127. package/dist/utils/safety.d.cts.map +1 -0
  128. package/dist/utils/safety.d.ts +10 -4
  129. package/dist/utils/safety.d.ts.map +1 -0
  130. package/dist/utils/safety.js +13 -19
  131. package/dist/utils/safety.js.map +1 -0
  132. package/dist/utils/stream.cjs +296 -475
  133. package/dist/utils/stream.cjs.map +1 -0
  134. package/dist/utils/stream.d.cts +165 -0
  135. package/dist/utils/stream.d.cts.map +1 -0
  136. package/dist/utils/stream.d.ts +156 -131
  137. package/dist/utils/stream.d.ts.map +1 -0
  138. package/dist/utils/stream.js +293 -469
  139. package/dist/utils/stream.js.map +1 -0
  140. package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
  141. package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
  142. package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
  143. package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
  144. package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
  145. package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
  146. package/dist/utils/zod_to_gemini_parameters.js +40 -76
  147. package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
  148. package/package.json +72 -85
  149. package/dist/types-anthropic.cjs +0 -2
  150. package/dist/types-anthropic.js +0 -1
  151. package/dist/utils/anthropic.d.ts +0 -4
  152. package/dist/utils/palm.cjs +0 -2
  153. package/dist/utils/palm.js +0 -1
  154. package/experimental/media.cjs +0 -1
  155. package/experimental/media.d.cts +0 -1
  156. package/experimental/media.d.ts +0 -1
  157. package/experimental/media.js +0 -1
  158. package/experimental/utils/media_core.cjs +0 -1
  159. package/experimental/utils/media_core.d.cts +0 -1
  160. package/experimental/utils/media_core.d.ts +0 -1
  161. package/experimental/utils/media_core.js +0 -1
  162. package/index.cjs +0 -1
  163. package/index.d.cts +0 -1
  164. package/index.d.ts +0 -1
  165. package/index.js +0 -1
  166. package/types.cjs +0 -1
  167. package/types.d.cts +0 -1
  168. package/types.d.ts +0 -1
  169. package/types.js +0 -1
  170. package/utils.cjs +0 -1
  171. package/utils.d.cts +0 -1
  172. package/utils.d.ts +0 -1
  173. 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, } 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
- get computedApiVersion() {
319
- return "v1beta";
320
- }
321
- async buildUrl() {
322
- return `https://generativelanguage.googleapis.com/upload/${this.apiVersion}/files`;
323
- }
324
- }
325
- export class AIStudioFileDownloadConnection extends GoogleDownloadConnection {
326
- constructor(fields, caller, client) {
327
- super(fields, caller, client);
328
- Object.defineProperty(this, "method", {
329
- enumerable: true,
330
- configurable: true,
331
- writable: true,
332
- value: void 0
333
- });
334
- Object.defineProperty(this, "name", {
335
- enumerable: true,
336
- configurable: true,
337
- writable: true,
338
- value: void 0
339
- });
340
- this.method = fields.method;
341
- this.name = fields.name;
342
- }
343
- get computedApiVersion() {
344
- return "v1beta";
345
- }
346
- buildMethod() {
347
- return this.method;
348
- }
349
- async buildUrl() {
350
- return `https://generativelanguage.googleapis.com/${this.apiVersion}/files/${this.name}`;
351
- }
352
- }
353
- export class BlobStoreAIStudioFileBase extends BlobStoreGoogle {
354
- constructor(fields) {
355
- const params = {
356
- defaultStoreOptions: {
357
- pathPrefix: "https://generativelanguage.googleapis.com/v1beta/files/",
358
- actionIfInvalid: "removePath",
359
- },
360
- ...fields,
361
- };
362
- super(params);
363
- Object.defineProperty(this, "params", {
364
- enumerable: true,
365
- configurable: true,
366
- writable: true,
367
- value: void 0
368
- });
369
- Object.defineProperty(this, "retryTime", {
370
- enumerable: true,
371
- configurable: true,
372
- writable: true,
373
- value: 1000
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