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