@langchain/google-common 0.2.18 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/LICENSE +6 -6
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/auth.cjs +82 -116
- package/dist/auth.cjs.map +1 -0
- package/dist/auth.d.cts +46 -0
- package/dist/auth.d.cts.map +1 -0
- package/dist/auth.d.ts +41 -36
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +80 -110
- package/dist/auth.js.map +1 -0
- package/dist/chat_models.cjs +264 -466
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +109 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +98 -73
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +258 -457
- package/dist/chat_models.js.map +1 -0
- package/dist/connection.cjs +321 -466
- package/dist/connection.cjs.map +1 -0
- package/dist/connection.d.cts +109 -0
- package/dist/connection.d.cts.map +1 -0
- package/dist/connection.d.ts +98 -91
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +317 -459
- package/dist/connection.js.map +1 -0
- package/dist/embeddings.cjs +135 -186
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +44 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +38 -32
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +133 -181
- package/dist/embeddings.js.map +1 -0
- package/dist/experimental/media.cjs +380 -482
- package/dist/experimental/media.cjs.map +1 -0
- package/dist/experimental/media.d.cts +198 -0
- package/dist/experimental/media.d.cts.map +1 -0
- package/dist/experimental/media.d.ts +190 -202
- package/dist/experimental/media.d.ts.map +1 -0
- package/dist/experimental/media.js +369 -468
- package/dist/experimental/media.js.map +1 -0
- package/dist/experimental/utils/media_core.cjs +403 -517
- package/dist/experimental/utils/media_core.cjs.map +1 -0
- package/dist/experimental/utils/media_core.d.cts +215 -0
- package/dist/experimental/utils/media_core.d.cts.map +1 -0
- package/dist/experimental/utils/media_core.d.ts +171 -165
- package/dist/experimental/utils/media_core.d.ts.map +1 -0
- package/dist/experimental/utils/media_core.js +395 -506
- package/dist/experimental/utils/media_core.js.map +1 -0
- package/dist/index.cjs +58 -27
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -11
- package/dist/index.js +13 -11
- package/dist/llms.cjs +157 -244
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +72 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +64 -54
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +154 -238
- package/dist/llms.js.map +1 -0
- package/dist/output_parsers.cjs +148 -173
- package/dist/output_parsers.cjs.map +1 -0
- package/dist/output_parsers.d.cts +53 -0
- package/dist/output_parsers.d.cts.map +1 -0
- package/dist/output_parsers.d.ts +46 -42
- package/dist/output_parsers.d.ts.map +1 -0
- package/dist/output_parsers.js +146 -168
- package/dist/output_parsers.js.map +1 -0
- package/dist/profiles.cjs +219 -0
- package/dist/profiles.cjs.map +1 -0
- package/dist/profiles.js +218 -0
- package/dist/profiles.js.map +1 -0
- package/dist/types-anthropic.d.cts +229 -0
- package/dist/types-anthropic.d.cts.map +1 -0
- package/dist/types-anthropic.d.ts +221 -215
- package/dist/types-anthropic.d.ts.map +1 -0
- package/dist/types.cjs +51 -62
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +748 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +669 -656
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +46 -45
- package/dist/types.js.map +1 -0
- package/dist/utils/anthropic.cjs +598 -821
- package/dist/utils/anthropic.cjs.map +1 -0
- package/dist/utils/anthropic.js +597 -818
- package/dist/utils/anthropic.js.map +1 -0
- package/dist/utils/common.cjs +130 -211
- package/dist/utils/common.cjs.map +1 -0
- package/dist/utils/common.d.cts +13 -0
- package/dist/utils/common.d.cts.map +1 -0
- package/dist/utils/common.d.ts +12 -7
- package/dist/utils/common.d.ts.map +1 -0
- package/dist/utils/common.js +128 -207
- package/dist/utils/common.js.map +1 -0
- package/dist/utils/failed_handler.cjs +28 -30
- package/dist/utils/failed_handler.cjs.map +1 -0
- package/dist/utils/failed_handler.d.cts +9 -0
- package/dist/utils/failed_handler.d.cts.map +1 -0
- package/dist/utils/failed_handler.d.ts +8 -2
- package/dist/utils/failed_handler.d.ts.map +1 -0
- package/dist/utils/failed_handler.js +28 -28
- package/dist/utils/failed_handler.js.map +1 -0
- package/dist/utils/gemini.cjs +1020 -1488
- package/dist/utils/gemini.cjs.map +1 -0
- package/dist/utils/gemini.d.cts +51 -0
- package/dist/utils/gemini.d.cts.map +1 -0
- package/dist/utils/gemini.d.ts +51 -48
- package/dist/utils/gemini.d.ts.map +1 -0
- package/dist/utils/gemini.js +1015 -1479
- package/dist/utils/gemini.js.map +1 -0
- package/dist/utils/index.cjs +38 -23
- package/dist/utils/index.d.cts +8 -0
- package/dist/utils/index.d.ts +8 -7
- package/dist/utils/index.js +8 -7
- package/dist/utils/palm.d.cts +11 -0
- package/dist/utils/palm.d.cts.map +1 -0
- package/dist/utils/palm.d.ts +9 -4
- package/dist/utils/palm.d.ts.map +1 -0
- package/dist/utils/safety.cjs +13 -22
- package/dist/utils/safety.cjs.map +1 -0
- package/dist/utils/safety.d.cts +12 -0
- package/dist/utils/safety.d.cts.map +1 -0
- package/dist/utils/safety.d.ts +10 -4
- package/dist/utils/safety.d.ts.map +1 -0
- package/dist/utils/safety.js +13 -19
- package/dist/utils/safety.js.map +1 -0
- package/dist/utils/stream.cjs +296 -475
- package/dist/utils/stream.cjs.map +1 -0
- package/dist/utils/stream.d.cts +165 -0
- package/dist/utils/stream.d.cts.map +1 -0
- package/dist/utils/stream.d.ts +156 -131
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/stream.js +293 -469
- package/dist/utils/stream.js.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.cjs +43 -81
- package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts +22 -0
- package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.d.ts +21 -6
- package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -0
- package/dist/utils/zod_to_gemini_parameters.js +40 -76
- package/dist/utils/zod_to_gemini_parameters.js.map +1 -0
- package/package.json +72 -85
- package/dist/types-anthropic.cjs +0 -2
- package/dist/types-anthropic.js +0 -1
- package/dist/utils/anthropic.d.ts +0 -4
- package/dist/utils/palm.cjs +0 -2
- package/dist/utils/palm.js +0 -1
- package/experimental/media.cjs +0 -1
- package/experimental/media.d.cts +0 -1
- package/experimental/media.d.ts +0 -1
- package/experimental/media.js +0 -1
- package/experimental/utils/media_core.cjs +0 -1
- package/experimental/utils/media_core.d.cts +0 -1
- package/experimental/utils/media_core.d.ts +0 -1
- package/experimental/utils/media_core.js +0 -1
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
- package/types.cjs +0 -1
- package/types.d.cts +0 -1
- package/types.d.ts +0 -1
- package/types.js +0 -1
- package/utils.cjs +0 -1
- package/utils.d.cts +0 -1
- package/utils.d.ts +0 -1
- package/utils.js +0 -1
|
@@ -1,514 +1,403 @@
|
|
|
1
|
-
import { v1, v4 } from "uuid";
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
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
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
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
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
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
|