@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,524 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MediaManager = exports.DataBlobStore = exports.SimpleWebBlobStore = exports.ReadThroughBlobStore = exports.BackedBlobStore = exports.BlobStore = exports.MediaBlob = void 0;
4
+ const uuid_1 = require("uuid"); // FIXME - it is importing the wrong uuid, so v6 and v7 aren't implemented
5
+ const stores_1 = require("@langchain/core/stores");
6
+ const serializable_1 = require("@langchain/core/load/serializable");
7
+ function bytesToString(dataArray) {
8
+ // Need to handle the array in smaller chunks to deal with stack size limits
9
+ let ret = "";
10
+ const chunkSize = 102400;
11
+ for (let i = 0; i < dataArray.length; i += chunkSize) {
12
+ const chunk = dataArray.subarray(i, i + chunkSize);
13
+ ret += String.fromCharCode(...chunk);
14
+ }
15
+ return ret;
16
+ }
17
+ /**
18
+ * Represents a chunk of data that can be identified by the path where the
19
+ * data is (or will be) located, along with optional metadata about the data.
20
+ */
21
+ class MediaBlob extends serializable_1.Serializable {
22
+ constructor(params) {
23
+ super(params);
24
+ Object.defineProperty(this, "lc_serializable", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: true
29
+ });
30
+ Object.defineProperty(this, "lc_namespace", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: [
35
+ "langchain",
36
+ "google_common",
37
+ "experimental",
38
+ "utils",
39
+ "media_core",
40
+ ]
41
+ });
42
+ Object.defineProperty(this, "data", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: {
47
+ value: "",
48
+ type: "text/plain",
49
+ }
50
+ });
51
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
52
+ Object.defineProperty(this, "metadata", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: void 0
57
+ });
58
+ Object.defineProperty(this, "path", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: void 0
63
+ });
64
+ this.data = params.data ?? this.data;
65
+ this.metadata = params.metadata;
66
+ this.path = params.path;
67
+ }
68
+ get size() {
69
+ return this.asBytes.length;
70
+ }
71
+ get dataType() {
72
+ return this.data?.type ?? "";
73
+ }
74
+ get encoding() {
75
+ const charsetEquals = this.dataType.indexOf("charset=");
76
+ return charsetEquals === -1
77
+ ? "utf-8"
78
+ : this.dataType.substring(charsetEquals + 8);
79
+ }
80
+ get mimetype() {
81
+ const semicolon = this.dataType.indexOf(";");
82
+ return semicolon === -1
83
+ ? this.dataType
84
+ : this.dataType.substring(0, semicolon);
85
+ }
86
+ get asBytes() {
87
+ if (!this.data) {
88
+ return Uint8Array.from([]);
89
+ }
90
+ const binString = atob(this.data?.value);
91
+ const ret = new Uint8Array(binString.length);
92
+ for (let co = 0; co < binString.length; co += 1) {
93
+ ret[co] = binString.charCodeAt(co);
94
+ }
95
+ return ret;
96
+ }
97
+ async asString() {
98
+ return bytesToString(this.asBytes);
99
+ }
100
+ async asBase64() {
101
+ return this.data?.value ?? "";
102
+ }
103
+ async asDataUrl() {
104
+ return `data:${this.mimetype};base64,${await this.asBase64()}`;
105
+ }
106
+ async asUri() {
107
+ return this.path ?? (await this.asDataUrl());
108
+ }
109
+ async encode() {
110
+ const dataUrl = await this.asDataUrl();
111
+ const comma = dataUrl.indexOf(",");
112
+ const encoded = dataUrl.substring(comma + 1);
113
+ const encoding = dataUrl.indexOf("base64") > -1 ? "base64" : "8bit";
114
+ return {
115
+ encoded,
116
+ encoding,
117
+ };
118
+ }
119
+ static fromDataUrl(url) {
120
+ if (!url.startsWith("data:")) {
121
+ throw new Error("Not a data: URL");
122
+ }
123
+ const colon = url.indexOf(":");
124
+ const semicolon = url.indexOf(";");
125
+ const mimeType = url.substring(colon + 1, semicolon);
126
+ const comma = url.indexOf(",");
127
+ const base64Data = url.substring(comma + 1);
128
+ const data = {
129
+ type: mimeType,
130
+ value: base64Data,
131
+ };
132
+ return new MediaBlob({
133
+ data,
134
+ path: url,
135
+ });
136
+ }
137
+ static async fromBlob(blob, other) {
138
+ const valueBuffer = await blob.arrayBuffer();
139
+ const valueArray = new Uint8Array(valueBuffer);
140
+ const valueStr = bytesToString(valueArray);
141
+ const value = btoa(valueStr);
142
+ return new MediaBlob({
143
+ ...other,
144
+ data: {
145
+ value,
146
+ type: blob.type,
147
+ },
148
+ });
149
+ }
150
+ }
151
+ exports.MediaBlob = MediaBlob;
152
+ /**
153
+ * A specialized Store that is designed to handle MediaBlobs and use the
154
+ * key that is included in the blob to determine exactly how it is stored.
155
+ *
156
+ * The full details of a MediaBlob may be changed when it is stored.
157
+ * For example, it may get additional or different Metadata. This should be
158
+ * what is returned when the store() method is called.
159
+ *
160
+ * Although BlobStore extends BaseStore, not all of the methods from
161
+ * BaseStore may be implemented (or even possible). Those that are not
162
+ * implemented should be documented and throw an Error if called.
163
+ */
164
+ class BlobStore extends stores_1.BaseStore {
165
+ constructor(opts) {
166
+ super(opts);
167
+ Object.defineProperty(this, "lc_namespace", {
168
+ enumerable: true,
169
+ configurable: true,
170
+ writable: true,
171
+ value: ["langchain", "google-common"]
172
+ }); // FIXME - What should this be? And why?
173
+ Object.defineProperty(this, "defaultStoreOptions", {
174
+ enumerable: true,
175
+ configurable: true,
176
+ writable: true,
177
+ value: void 0
178
+ });
179
+ Object.defineProperty(this, "defaultFetchOptions", {
180
+ enumerable: true,
181
+ configurable: true,
182
+ writable: true,
183
+ value: void 0
184
+ });
185
+ this.defaultStoreOptions = opts?.defaultStoreOptions ?? {};
186
+ this.defaultFetchOptions = opts?.defaultFetchOptions ?? {};
187
+ }
188
+ async _realKey(key) {
189
+ return typeof key === "string" ? key : await key.asUri();
190
+ }
191
+ /**
192
+ * Is the path supported by this BlobStore?
193
+ *
194
+ * Although this is async, this is expected to be a relatively fast operation
195
+ * (ie - you shouldn't make network calls).
196
+ *
197
+ * @param path The path to check
198
+ * @param opts Any options (if needed) that may be used to determine if it is valid
199
+ * @return If the path is supported
200
+ */
201
+ hasValidPath(path, opts) {
202
+ const prefix = opts?.pathPrefix ?? "";
203
+ const isPrefixed = typeof path !== "undefined" && path.startsWith(prefix);
204
+ return Promise.resolve(isPrefixed);
205
+ }
206
+ _blobPathSuffix(blob) {
207
+ // Get the path currently set and make sure we treat it as a string
208
+ const blobPath = `${blob.path}`;
209
+ // Advance past the first set of /
210
+ let pathStart = blobPath.indexOf("/") + 1;
211
+ while (blobPath.charAt(pathStart) === "/") {
212
+ pathStart += 1;
213
+ }
214
+ // We will use the rest as the path for a replacement
215
+ return blobPath.substring(pathStart);
216
+ }
217
+ async _newBlob(oldBlob, newPath) {
218
+ const oldPath = oldBlob.path;
219
+ const metadata = oldBlob?.metadata ?? {};
220
+ metadata.langchainOldPath = oldPath;
221
+ const newBlob = new MediaBlob({
222
+ ...oldBlob,
223
+ metadata,
224
+ });
225
+ if (newPath) {
226
+ newBlob.path = newPath;
227
+ }
228
+ else if (newBlob.path) {
229
+ delete newBlob.path;
230
+ }
231
+ return newBlob;
232
+ }
233
+ async _validBlobPrefixPath(blob, opts) {
234
+ const prefix = opts?.pathPrefix ?? "";
235
+ const suffix = this._blobPathSuffix(blob);
236
+ const newPath = `${prefix}${suffix}`;
237
+ return this._newBlob(blob, newPath);
238
+ }
239
+ _validBlobPrefixUuidFunction(name) {
240
+ switch (name) {
241
+ case "prefixUuid1":
242
+ return (0, uuid_1.v1)();
243
+ case "prefixUuid4":
244
+ return (0, uuid_1.v4)();
245
+ // case "prefixUuid6": return v6();
246
+ // case "prefixUuid7": return v7();
247
+ default:
248
+ throw new Error(`Unknown uuid function: ${name}`);
249
+ }
250
+ }
251
+ async _validBlobPrefixUuid(blob, opts) {
252
+ const prefix = opts?.pathPrefix ?? "";
253
+ const suffix = this._validBlobPrefixUuidFunction(opts?.actionIfInvalid ?? "prefixUuid4");
254
+ const newPath = `${prefix}${suffix}`;
255
+ return this._newBlob(blob, newPath);
256
+ }
257
+ async _validBlobRemovePath(blob, _opts) {
258
+ return this._newBlob(blob, undefined);
259
+ }
260
+ /**
261
+ * Based on the blob and options, return a blob that has a valid path
262
+ * that can be saved.
263
+ * @param blob
264
+ * @param opts
265
+ */
266
+ async _validStoreBlob(blob, opts) {
267
+ if (await this.hasValidPath(blob.path, opts)) {
268
+ return blob;
269
+ }
270
+ switch (opts?.actionIfInvalid) {
271
+ case "ignore":
272
+ return blob;
273
+ case "prefixPath":
274
+ return this._validBlobPrefixPath(blob, opts);
275
+ case "prefixUuid1":
276
+ case "prefixUuid4":
277
+ case "prefixUuid6":
278
+ case "prefixUuid7":
279
+ return this._validBlobPrefixUuid(blob, opts);
280
+ case "removePath":
281
+ return this._validBlobRemovePath(blob, opts);
282
+ default:
283
+ return undefined;
284
+ }
285
+ }
286
+ async store(blob, opts = {}) {
287
+ const allOpts = {
288
+ ...this.defaultStoreOptions,
289
+ ...opts,
290
+ };
291
+ const validBlob = await this._validStoreBlob(blob, allOpts);
292
+ if (typeof validBlob !== "undefined") {
293
+ const validKey = await validBlob.asUri();
294
+ await this.mset([[validKey, validBlob]]);
295
+ const savedKey = await validBlob.asUri();
296
+ return await this.fetch(savedKey);
297
+ }
298
+ return undefined;
299
+ }
300
+ async _missingFetchBlobEmpty(path, _opts) {
301
+ return new MediaBlob({ path });
302
+ }
303
+ async _missingFetchBlob(path, opts) {
304
+ switch (opts?.actionIfBlobMissing) {
305
+ case "emptyBlob":
306
+ return this._missingFetchBlobEmpty(path, opts);
307
+ default:
308
+ return undefined;
309
+ }
310
+ }
311
+ async fetch(key, opts = {}) {
312
+ const allOpts = {
313
+ ...this.defaultFetchOptions,
314
+ ...opts,
315
+ };
316
+ const realKey = await this._realKey(key);
317
+ const ret = await this.mget([realKey]);
318
+ return ret?.[0] ?? (await this._missingFetchBlob(realKey, allOpts));
319
+ }
320
+ }
321
+ exports.BlobStore = BlobStore;
322
+ class BackedBlobStore extends BlobStore {
323
+ constructor(opts) {
324
+ super(opts);
325
+ Object.defineProperty(this, "backingStore", {
326
+ enumerable: true,
327
+ configurable: true,
328
+ writable: true,
329
+ value: void 0
330
+ });
331
+ this.backingStore = opts.backingStore;
332
+ }
333
+ mdelete(keys) {
334
+ return this.backingStore.mdelete(keys);
335
+ }
336
+ mget(keys) {
337
+ return this.backingStore.mget(keys);
338
+ }
339
+ mset(keyValuePairs) {
340
+ return this.backingStore.mset(keyValuePairs);
341
+ }
342
+ yieldKeys(prefix) {
343
+ return this.backingStore.yieldKeys(prefix);
344
+ }
345
+ }
346
+ exports.BackedBlobStore = BackedBlobStore;
347
+ class ReadThroughBlobStore extends BlobStore {
348
+ constructor(opts) {
349
+ super(opts);
350
+ Object.defineProperty(this, "baseStore", {
351
+ enumerable: true,
352
+ configurable: true,
353
+ writable: true,
354
+ value: void 0
355
+ });
356
+ Object.defineProperty(this, "backingStore", {
357
+ enumerable: true,
358
+ configurable: true,
359
+ writable: true,
360
+ value: void 0
361
+ });
362
+ this.baseStore = opts.baseStore;
363
+ this.backingStore = opts.backingStore;
364
+ }
365
+ async store(blob, opts = {}) {
366
+ const originalUri = await blob.asUri();
367
+ const newBlob = await this.backingStore.store(blob, opts);
368
+ if (newBlob) {
369
+ await this.baseStore.mset([[originalUri, newBlob]]);
370
+ }
371
+ return newBlob;
372
+ }
373
+ mdelete(keys) {
374
+ return this.baseStore.mdelete(keys);
375
+ }
376
+ mget(keys) {
377
+ return this.baseStore.mget(keys);
378
+ }
379
+ mset(_keyValuePairs) {
380
+ throw new Error("Do not call ReadThroughBlobStore.mset directly");
381
+ }
382
+ yieldKeys(prefix) {
383
+ return this.baseStore.yieldKeys(prefix);
384
+ }
385
+ }
386
+ exports.ReadThroughBlobStore = ReadThroughBlobStore;
387
+ class SimpleWebBlobStore extends BlobStore {
388
+ _notImplementedException() {
389
+ throw new Error("Not implemented for SimpleWebBlobStore");
390
+ }
391
+ async hasValidPath(path, _opts) {
392
+ return ((await super.hasValidPath(path, { pathPrefix: "https://" })) ||
393
+ (await super.hasValidPath(path, { pathPrefix: "http://" })));
394
+ }
395
+ async _fetch(url) {
396
+ const ret = new MediaBlob({
397
+ path: url,
398
+ });
399
+ const metadata = {};
400
+ const fetchOptions = {
401
+ method: "GET",
402
+ };
403
+ const res = await fetch(url, fetchOptions);
404
+ metadata.status = res.status;
405
+ const headers = {};
406
+ for (const [key, value] of res.headers.entries()) {
407
+ headers[key] = value;
408
+ }
409
+ metadata.headers = headers;
410
+ metadata.ok = res.ok;
411
+ if (res.ok) {
412
+ const resMediaBlob = await MediaBlob.fromBlob(await res.blob());
413
+ ret.data = resMediaBlob.data;
414
+ }
415
+ ret.metadata = metadata;
416
+ return ret;
417
+ }
418
+ async mget(keys) {
419
+ const blobMap = keys.map(this._fetch);
420
+ return await Promise.all(blobMap);
421
+ }
422
+ async mdelete(_keys) {
423
+ this._notImplementedException();
424
+ }
425
+ async mset(_keyValuePairs) {
426
+ this._notImplementedException();
427
+ }
428
+ async *yieldKeys(_prefix) {
429
+ this._notImplementedException();
430
+ yield "";
431
+ }
432
+ }
433
+ exports.SimpleWebBlobStore = SimpleWebBlobStore;
434
+ /**
435
+ * A blob "store" that works with data: URLs that will turn the URL into
436
+ * a blob.
437
+ */
438
+ class DataBlobStore extends BlobStore {
439
+ _notImplementedException() {
440
+ throw new Error("Not implemented for DataBlobStore");
441
+ }
442
+ hasValidPath(path, _opts) {
443
+ return super.hasValidPath(path, { pathPrefix: "data:" });
444
+ }
445
+ _fetch(url) {
446
+ return MediaBlob.fromDataUrl(url);
447
+ }
448
+ async mget(keys) {
449
+ const blobMap = keys.map(this._fetch);
450
+ return blobMap;
451
+ }
452
+ async mdelete(_keys) {
453
+ this._notImplementedException();
454
+ }
455
+ async mset(_keyValuePairs) {
456
+ this._notImplementedException();
457
+ }
458
+ async *yieldKeys(_prefix) {
459
+ this._notImplementedException();
460
+ yield "";
461
+ }
462
+ }
463
+ exports.DataBlobStore = DataBlobStore;
464
+ /**
465
+ * Responsible for converting a URI (typically a web URL) into a MediaBlob.
466
+ * Allows for aliasing / caching of the requested URI and what it resolves to.
467
+ * This MediaBlob is expected to be usable to provide to an LLM, either
468
+ * through the Base64 of the media or through a canonical URI that the LLM
469
+ * supports.
470
+ */
471
+ class MediaManager {
472
+ constructor(config) {
473
+ Object.defineProperty(this, "store", {
474
+ enumerable: true,
475
+ configurable: true,
476
+ writable: true,
477
+ value: void 0
478
+ });
479
+ Object.defineProperty(this, "resolvers", {
480
+ enumerable: true,
481
+ configurable: true,
482
+ writable: true,
483
+ value: void 0
484
+ });
485
+ this.store = config.store;
486
+ this.resolvers = config.resolvers;
487
+ }
488
+ defaultResolvers() {
489
+ return [new DataBlobStore({}), new SimpleWebBlobStore({})];
490
+ }
491
+ async _isInvalid(blob) {
492
+ return typeof blob === "undefined";
493
+ }
494
+ /**
495
+ * Given the public URI, load what is at this URI and save it
496
+ * in the store.
497
+ * @param uri The URI to resolve using the resolver
498
+ * @return A canonical MediaBlob for this URI
499
+ */
500
+ async _resolveAndSave(uri) {
501
+ let resolvedBlob;
502
+ const resolvers = this.resolvers || this.defaultResolvers();
503
+ for (let co = 0; co < resolvers.length; co += 1) {
504
+ const resolver = resolvers[co];
505
+ if (await resolver.hasValidPath(uri)) {
506
+ resolvedBlob = await resolver.fetch(uri);
507
+ }
508
+ }
509
+ if (resolvedBlob) {
510
+ return await this.store.store(resolvedBlob);
511
+ }
512
+ else {
513
+ return new MediaBlob({});
514
+ }
515
+ }
516
+ async getMediaBlob(uri) {
517
+ const aliasBlob = await this.store.fetch(uri);
518
+ const ret = (await this._isInvalid(aliasBlob))
519
+ ? await this._resolveAndSave(uri)
520
+ : aliasBlob;
521
+ return ret;
522
+ }
523
+ }
524
+ exports.MediaManager = MediaManager;