@langchain/google-common 0.2.17 → 1.0.0

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