@anvia/core 1.0.2 → 1.0.3
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/README.md +26 -0
- package/dist/agent/index.js +4 -3
- package/dist/chunk-4EWTDOLR.js +412 -0
- package/dist/chunk-4EWTDOLR.js.map +1 -0
- package/dist/{chunk-PH6OTIPZ.js → chunk-5TIXGU5U.js} +3 -3
- package/dist/{chunk-YFJ4NHV6.js → chunk-LZD36ZAP.js} +86 -1
- package/dist/chunk-LZD36ZAP.js.map +1 -0
- package/dist/{chunk-EO2GECYH.js → chunk-OIB3URVG.js} +2 -2
- package/dist/{chunk-IWJK7JB5.js → chunk-VSTJNHOZ.js} +2 -2
- package/dist/documents/index.d.ts +2 -0
- package/dist/documents/index.js +6 -335
- package/dist/documents/index.js.map +1 -1
- package/dist/index.js +5 -4
- package/dist/internal/agent.js +4 -3
- package/dist/internal/agent.js.map +1 -1
- package/dist/skills/index.js +4 -3
- package/dist/text-document-B-X7Vxq1.d.ts +32 -0
- package/dist/tool/index.js +3 -2
- package/dist/vector-store/index.d.ts +25 -5
- package/dist/vector-store/index.js +6 -1
- package/package.json +1 -1
- package/dist/chunk-YFJ4NHV6.js.map +0 -1
- /package/dist/{chunk-PH6OTIPZ.js.map → chunk-5TIXGU5U.js.map} +0 -0
- /package/dist/{chunk-EO2GECYH.js.map → chunk-OIB3URVG.js.map} +0 -0
- /package/dist/{chunk-IWJK7JB5.js.map → chunk-VSTJNHOZ.js.map} +0 -0
package/README.md
CHANGED
|
@@ -538,6 +538,32 @@ slice. PDF pages are one-based, and the parser task is disposed before extractio
|
|
|
538
538
|
If parsing or abort handling fails together with parser cleanup, extraction rejects with an
|
|
539
539
|
`AggregateError` containing the operation failure first and the cleanup failure second.
|
|
540
540
|
|
|
541
|
+
For the common vector ingestion path, embed and upsert raw text with one call:
|
|
542
|
+
|
|
543
|
+
```ts
|
|
544
|
+
import { ingestVectorText } from "@anvia/core/vector-store";
|
|
545
|
+
|
|
546
|
+
await ingestVectorText({
|
|
547
|
+
store,
|
|
548
|
+
document: {
|
|
549
|
+
id: "incident-42",
|
|
550
|
+
text,
|
|
551
|
+
metadata: { tenant: "acme" },
|
|
552
|
+
},
|
|
553
|
+
embeddingModel,
|
|
554
|
+
chunking: {
|
|
555
|
+
strategy: "recursive",
|
|
556
|
+
maxSize: 1_000,
|
|
557
|
+
overlap: 100,
|
|
558
|
+
separators: ["\n\n", "\n", " "],
|
|
559
|
+
},
|
|
560
|
+
});
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
`ingestVectorDocuments()` accepts the same document shape for batches. Without `chunking`, each
|
|
564
|
+
source document is embedded as one chunk. Chunk embeddings remain grouped under the source document
|
|
565
|
+
ID so re-ingestion replaces its complete vector representation instead of leaving stale chunks.
|
|
566
|
+
|
|
541
567
|
## Media
|
|
542
568
|
|
|
543
569
|
Media helpers follow the same one-object API and share `providerOptions`, `retries`, and
|
package/dist/agent/index.js
CHANGED
|
@@ -2,14 +2,15 @@ import {
|
|
|
2
2
|
Agent,
|
|
3
3
|
createVectorContext,
|
|
4
4
|
isVectorContext
|
|
5
|
-
} from "../chunk-
|
|
5
|
+
} from "../chunk-5TIXGU5U.js";
|
|
6
6
|
import "../chunk-OJBFDBLG.js";
|
|
7
7
|
import "../chunk-J6LVLV6P.js";
|
|
8
8
|
import "../chunk-XMVOBX43.js";
|
|
9
9
|
import "../chunk-AR2K73CJ.js";
|
|
10
10
|
import "../chunk-YK4WAAS4.js";
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
11
|
+
import "../chunk-OIB3URVG.js";
|
|
12
|
+
import "../chunk-LZD36ZAP.js";
|
|
13
|
+
import "../chunk-4EWTDOLR.js";
|
|
13
14
|
import {
|
|
14
15
|
AgentRunBlockedError,
|
|
15
16
|
AgentRunCancelledError,
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
// src/documents/chunk-text.ts
|
|
2
|
+
function chunkText(options) {
|
|
3
|
+
const validated = validateOptions(options);
|
|
4
|
+
if (validated.text.length === 0) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
if (validated.strategy === "fixed") {
|
|
8
|
+
return chunkFixed(validated.text, validated.maxSize, validated.overlap);
|
|
9
|
+
}
|
|
10
|
+
const ranges = resolveRecursiveRanges(
|
|
11
|
+
validated.text,
|
|
12
|
+
{ start: 0, end: validated.text.length },
|
|
13
|
+
validated.maxSize,
|
|
14
|
+
validated.separators
|
|
15
|
+
);
|
|
16
|
+
return applyRecursiveOverlap(validated.text, ranges, validated.maxSize, validated.overlap);
|
|
17
|
+
}
|
|
18
|
+
function validateOptions(options) {
|
|
19
|
+
if (typeof options !== "object" || options === null) {
|
|
20
|
+
throw new TypeError("chunkText options must be an object");
|
|
21
|
+
}
|
|
22
|
+
const value = options;
|
|
23
|
+
if (typeof value.text !== "string") {
|
|
24
|
+
throw new TypeError("chunkText text must be a string");
|
|
25
|
+
}
|
|
26
|
+
if (!Number.isSafeInteger(value.maxSize) || value.maxSize <= 0) {
|
|
27
|
+
throw new RangeError("chunkText maxSize must be a positive safe integer");
|
|
28
|
+
}
|
|
29
|
+
const maxSize = value.maxSize;
|
|
30
|
+
const overlap = value.overlap === void 0 ? 0 : value.overlap;
|
|
31
|
+
if (!Number.isSafeInteger(overlap) || overlap < 0 || overlap >= maxSize) {
|
|
32
|
+
throw new RangeError(
|
|
33
|
+
"chunkText overlap must be a non-negative safe integer smaller than maxSize"
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if (value.strategy === "fixed") {
|
|
37
|
+
if ("separators" in value) {
|
|
38
|
+
throw new TypeError("chunkText fixed strategy does not accept separators");
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
text: value.text,
|
|
42
|
+
strategy: "fixed",
|
|
43
|
+
maxSize,
|
|
44
|
+
overlap
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (value.strategy !== "recursive") {
|
|
48
|
+
throw new TypeError('chunkText strategy must be either "fixed" or "recursive"');
|
|
49
|
+
}
|
|
50
|
+
if (!Array.isArray(value.separators) || value.separators.length === 0) {
|
|
51
|
+
throw new TypeError("chunkText recursive separators must be a non-empty array");
|
|
52
|
+
}
|
|
53
|
+
const separators = value.separators;
|
|
54
|
+
const seen = /* @__PURE__ */ new Set();
|
|
55
|
+
for (const separator of separators) {
|
|
56
|
+
if (typeof separator !== "string" || separator.length === 0) {
|
|
57
|
+
throw new TypeError("chunkText recursive separators must contain non-empty strings");
|
|
58
|
+
}
|
|
59
|
+
if (seen.has(separator)) {
|
|
60
|
+
throw new TypeError("chunkText recursive separators must not contain duplicates");
|
|
61
|
+
}
|
|
62
|
+
seen.add(separator);
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
text: value.text,
|
|
66
|
+
strategy: "recursive",
|
|
67
|
+
maxSize,
|
|
68
|
+
overlap,
|
|
69
|
+
separators
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function chunkFixed(text, maxSize, overlap) {
|
|
73
|
+
const chunks = [];
|
|
74
|
+
let start = 0;
|
|
75
|
+
while (start < text.length) {
|
|
76
|
+
const end = Math.min(start + maxSize, text.length);
|
|
77
|
+
chunks.push(createChunk(text, chunks.length, start, end));
|
|
78
|
+
if (end === text.length) {
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
start = end - overlap;
|
|
82
|
+
}
|
|
83
|
+
return chunks;
|
|
84
|
+
}
|
|
85
|
+
function resolveRecursiveRanges(text, initialRange, maxSize, separators) {
|
|
86
|
+
const resolved = [];
|
|
87
|
+
const pending = [
|
|
88
|
+
{ range: initialRange, separatorIndex: 0 }
|
|
89
|
+
];
|
|
90
|
+
while (pending.length > 0) {
|
|
91
|
+
const current = pending.pop();
|
|
92
|
+
if (current === void 0) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
if (current.range.end - current.range.start <= maxSize) {
|
|
96
|
+
resolved.push(current.range);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
let pieces;
|
|
100
|
+
let nextSeparatorIndex = separators.length;
|
|
101
|
+
for (let index = current.separatorIndex; index < separators.length; index += 1) {
|
|
102
|
+
const candidate = splitAfterSeparator(text, current.range, separators[index]);
|
|
103
|
+
if (candidate.length > 1) {
|
|
104
|
+
pieces = candidate;
|
|
105
|
+
nextSeparatorIndex = index + 1;
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (pieces === void 0) {
|
|
110
|
+
resolved.push(...hardSplit(current.range, maxSize));
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
for (let index = pieces.length - 1; index >= 0; index -= 1) {
|
|
114
|
+
pending.push({ range: pieces[index], separatorIndex: nextSeparatorIndex });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return resolved;
|
|
118
|
+
}
|
|
119
|
+
function splitAfterSeparator(text, range, separator) {
|
|
120
|
+
const ranges = [];
|
|
121
|
+
let start = range.start;
|
|
122
|
+
let searchFrom = range.start;
|
|
123
|
+
while (searchFrom < range.end) {
|
|
124
|
+
const separatorStart = text.indexOf(separator, searchFrom);
|
|
125
|
+
if (separatorStart < 0 || separatorStart + separator.length > range.end) {
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
const end = separatorStart + separator.length;
|
|
129
|
+
if (end > start) {
|
|
130
|
+
ranges.push({ start, end });
|
|
131
|
+
}
|
|
132
|
+
start = end;
|
|
133
|
+
searchFrom = end;
|
|
134
|
+
}
|
|
135
|
+
if (start < range.end) {
|
|
136
|
+
ranges.push({ start, end: range.end });
|
|
137
|
+
}
|
|
138
|
+
return ranges;
|
|
139
|
+
}
|
|
140
|
+
function hardSplit(range, maxSize) {
|
|
141
|
+
const ranges = [];
|
|
142
|
+
for (let start = range.start; start < range.end; start += maxSize) {
|
|
143
|
+
ranges.push({ start, end: Math.min(start + maxSize, range.end) });
|
|
144
|
+
}
|
|
145
|
+
return ranges;
|
|
146
|
+
}
|
|
147
|
+
function applyRecursiveOverlap(text, ranges, maxSize, overlap) {
|
|
148
|
+
const boundaries = ranges.map((range) => range.end);
|
|
149
|
+
const chunks = [];
|
|
150
|
+
let start = 0;
|
|
151
|
+
let previousEnd = 0;
|
|
152
|
+
while (start < text.length) {
|
|
153
|
+
const limit = Math.min(start + maxSize, text.length);
|
|
154
|
+
const boundary = findLastBoundaryAtMost(boundaries, limit);
|
|
155
|
+
const end = boundary !== void 0 && boundary > previousEnd ? boundary : limit;
|
|
156
|
+
chunks.push(createChunk(text, chunks.length, start, end));
|
|
157
|
+
if (end === text.length) {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
previousEnd = end;
|
|
161
|
+
start = Math.max(0, end - overlap);
|
|
162
|
+
}
|
|
163
|
+
return chunks;
|
|
164
|
+
}
|
|
165
|
+
function findLastBoundaryAtMost(boundaries, limit) {
|
|
166
|
+
let low = 0;
|
|
167
|
+
let high = boundaries.length;
|
|
168
|
+
while (low < high) {
|
|
169
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
170
|
+
if (boundaries[middle] <= limit) {
|
|
171
|
+
low = middle + 1;
|
|
172
|
+
} else {
|
|
173
|
+
high = middle;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return boundaries[low - 1];
|
|
177
|
+
}
|
|
178
|
+
function createChunk(text, index, start, end) {
|
|
179
|
+
return { index, text: text.slice(start, end), start, end };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/documents/pdf.ts
|
|
183
|
+
async function extractPdfText(options) {
|
|
184
|
+
if (typeof options !== "object" || options === null) {
|
|
185
|
+
throw new TypeError("extractPdfText options must be an object");
|
|
186
|
+
}
|
|
187
|
+
if (!(options.data instanceof Uint8Array)) {
|
|
188
|
+
throw new TypeError("extractPdfText data must be a Uint8Array");
|
|
189
|
+
}
|
|
190
|
+
if (options.abortSignal !== void 0 && !isAbortSignal(options.abortSignal)) {
|
|
191
|
+
throw new TypeError("extractPdfText abortSignal must be an AbortSignal");
|
|
192
|
+
}
|
|
193
|
+
const { abortSignal } = options;
|
|
194
|
+
abortSignal?.throwIfAborted();
|
|
195
|
+
const data = new Uint8Array(options.data);
|
|
196
|
+
const pdfjs = await loadPdfjs();
|
|
197
|
+
abortSignal?.throwIfAborted();
|
|
198
|
+
const loadingTask = pdfjs.getDocument({ data });
|
|
199
|
+
let destroyPromise;
|
|
200
|
+
const destroy = () => {
|
|
201
|
+
destroyPromise ??= loadingTask.destroy();
|
|
202
|
+
return destroyPromise;
|
|
203
|
+
};
|
|
204
|
+
const abort = createAbortPromise(abortSignal, destroy);
|
|
205
|
+
return runWithCleanup(
|
|
206
|
+
async () => {
|
|
207
|
+
const document = await abort.wait(loadingTask.promise);
|
|
208
|
+
const pages = [];
|
|
209
|
+
for (let pageNumber = 1; pageNumber <= document.numPages; pageNumber += 1) {
|
|
210
|
+
abortSignal?.throwIfAborted();
|
|
211
|
+
const page = await abort.wait(document.getPage(pageNumber));
|
|
212
|
+
const content = await abort.wait(page.getTextContent());
|
|
213
|
+
pages.push({ pageNumber, text: extractPageText(content.items) });
|
|
214
|
+
}
|
|
215
|
+
return { pages };
|
|
216
|
+
},
|
|
217
|
+
async () => {
|
|
218
|
+
abort.dispose();
|
|
219
|
+
await destroy();
|
|
220
|
+
}
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
async function loadPdfjs() {
|
|
224
|
+
try {
|
|
225
|
+
return await import("pdfjs-dist/legacy/build/pdf.mjs");
|
|
226
|
+
} catch (cause) {
|
|
227
|
+
if (!isMissingPdfjsDependency(cause)) {
|
|
228
|
+
throw cause;
|
|
229
|
+
}
|
|
230
|
+
throw new Error(
|
|
231
|
+
'PDF extraction requires the optional "pdfjs-dist" package. Install it in your application to use extractPdfText().',
|
|
232
|
+
{ cause }
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
function isMissingPdfjsDependency(error) {
|
|
237
|
+
const seen = /* @__PURE__ */ new Set();
|
|
238
|
+
let current = error;
|
|
239
|
+
while (current instanceof Error && !seen.has(current)) {
|
|
240
|
+
seen.add(current);
|
|
241
|
+
if (current.message.includes("pdfjs-dist") && "code" in current && current.code === "ERR_MODULE_NOT_FOUND") {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
current = "cause" in current ? current.cause : void 0;
|
|
245
|
+
}
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
async function runWithCleanup(operation, cleanup) {
|
|
249
|
+
let outcome;
|
|
250
|
+
try {
|
|
251
|
+
outcome = { ok: true, value: await operation() };
|
|
252
|
+
} catch (error) {
|
|
253
|
+
outcome = { ok: false, error };
|
|
254
|
+
}
|
|
255
|
+
let cleanupError;
|
|
256
|
+
let cleanupFailed = false;
|
|
257
|
+
try {
|
|
258
|
+
await cleanup();
|
|
259
|
+
} catch (error) {
|
|
260
|
+
cleanupFailed = true;
|
|
261
|
+
cleanupError = error;
|
|
262
|
+
}
|
|
263
|
+
if (!outcome.ok) {
|
|
264
|
+
if (cleanupFailed) {
|
|
265
|
+
throw new AggregateError(
|
|
266
|
+
[outcome.error, cleanupError],
|
|
267
|
+
"PDF extraction and cleanup both failed",
|
|
268
|
+
{ cause: outcome.error }
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
throw outcome.error;
|
|
272
|
+
}
|
|
273
|
+
if (cleanupFailed) {
|
|
274
|
+
throw cleanupError;
|
|
275
|
+
}
|
|
276
|
+
return outcome.value;
|
|
277
|
+
}
|
|
278
|
+
function isAbortSignal(value) {
|
|
279
|
+
if (typeof value !== "object" || value === null) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
const candidate = value;
|
|
283
|
+
return typeof candidate.aborted === "boolean" && typeof candidate.addEventListener === "function" && typeof candidate.removeEventListener === "function" && typeof candidate.throwIfAborted === "function";
|
|
284
|
+
}
|
|
285
|
+
function extractPageText(items) {
|
|
286
|
+
let text = "";
|
|
287
|
+
for (const item of items) {
|
|
288
|
+
if (typeof item !== "object" || item === null) {
|
|
289
|
+
throw new TypeError("PDF text content contains a malformed item");
|
|
290
|
+
}
|
|
291
|
+
if (!("str" in item)) {
|
|
292
|
+
if ("type" in item && (item.type === "beginMarkedContent" || item.type === "beginMarkedContentProps" || item.type === "endMarkedContent")) {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
throw new TypeError("PDF text content contains an unknown non-text item");
|
|
296
|
+
}
|
|
297
|
+
if (typeof item.str !== "string") {
|
|
298
|
+
throw new TypeError("PDF text content item str must be a string");
|
|
299
|
+
}
|
|
300
|
+
if (!("hasEOL" in item) || typeof item.hasEOL !== "boolean") {
|
|
301
|
+
throw new TypeError("PDF text content item hasEOL must be a boolean");
|
|
302
|
+
}
|
|
303
|
+
text += item.str;
|
|
304
|
+
if (item.hasEOL) {
|
|
305
|
+
text += "\n";
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return text;
|
|
309
|
+
}
|
|
310
|
+
function createAbortPromise(abortSignal, destroy) {
|
|
311
|
+
if (abortSignal === void 0) {
|
|
312
|
+
return {
|
|
313
|
+
wait: (promise) => promise,
|
|
314
|
+
dispose: () => void 0
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
let rejectAbort;
|
|
318
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
319
|
+
rejectAbort = reject;
|
|
320
|
+
});
|
|
321
|
+
const onAbort = () => {
|
|
322
|
+
void destroy();
|
|
323
|
+
rejectAbort?.(
|
|
324
|
+
abortSignal.reason ?? new DOMException("The operation was aborted", "AbortError")
|
|
325
|
+
);
|
|
326
|
+
};
|
|
327
|
+
abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
328
|
+
if (abortSignal.aborted) {
|
|
329
|
+
onAbort();
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
wait: (promise) => Promise.race([promise, aborted]),
|
|
333
|
+
dispose: () => abortSignal.removeEventListener("abort", onAbort)
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// src/documents/text-document.ts
|
|
338
|
+
function chunkTextDocuments(options) {
|
|
339
|
+
if (typeof options !== "object" || options === null) {
|
|
340
|
+
throw new TypeError("chunkTextDocuments options must contain a documents array");
|
|
341
|
+
}
|
|
342
|
+
const documents = options.documents;
|
|
343
|
+
if (!Array.isArray(documents)) {
|
|
344
|
+
throw new TypeError("chunkTextDocuments options must contain a documents array");
|
|
345
|
+
}
|
|
346
|
+
const ids = /* @__PURE__ */ new Set();
|
|
347
|
+
const chunks = [];
|
|
348
|
+
for (const document of documents) {
|
|
349
|
+
validateDocument(document, ids);
|
|
350
|
+
chunks.push(...chunkDocument(document, options.chunking));
|
|
351
|
+
}
|
|
352
|
+
return chunks;
|
|
353
|
+
}
|
|
354
|
+
function validateDocument(document, ids) {
|
|
355
|
+
if (typeof document !== "object" || document === null) {
|
|
356
|
+
throw new TypeError("Text documents must be objects");
|
|
357
|
+
}
|
|
358
|
+
if (typeof document.id !== "string" || document.id.length === 0) {
|
|
359
|
+
throw new TypeError("Text document ids must be non-empty strings");
|
|
360
|
+
}
|
|
361
|
+
if (ids.has(document.id)) {
|
|
362
|
+
throw new TypeError(`Duplicate text document id: ${document.id}`);
|
|
363
|
+
}
|
|
364
|
+
if (typeof document.text !== "string" || document.text.length === 0) {
|
|
365
|
+
throw new TypeError(`Text document ${document.id} must contain non-empty text`);
|
|
366
|
+
}
|
|
367
|
+
ids.add(document.id);
|
|
368
|
+
}
|
|
369
|
+
function chunkDocument(document, chunking) {
|
|
370
|
+
if (chunking === void 0 || chunking.strategy === "none") {
|
|
371
|
+
return [createDocumentChunk(document, 0, document.text)];
|
|
372
|
+
}
|
|
373
|
+
const textChunks = chunkDocumentText(document.text, chunking);
|
|
374
|
+
return textChunks.map((chunk) => createDocumentChunk(document, chunk.index, chunk.text));
|
|
375
|
+
}
|
|
376
|
+
function chunkDocumentText(text, chunking) {
|
|
377
|
+
const overlap = chunking.overlap ?? 0;
|
|
378
|
+
if (chunking.strategy === "fixed") {
|
|
379
|
+
return chunkText({
|
|
380
|
+
text,
|
|
381
|
+
strategy: "fixed",
|
|
382
|
+
maxSize: chunking.maxSize,
|
|
383
|
+
overlap
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
return chunkText({
|
|
387
|
+
text,
|
|
388
|
+
strategy: "recursive",
|
|
389
|
+
maxSize: chunking.maxSize,
|
|
390
|
+
overlap,
|
|
391
|
+
separators: chunking.separators
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function createDocumentChunk(document, index, text) {
|
|
395
|
+
const chunk = {
|
|
396
|
+
id: `${document.id}:chunk:${index}`,
|
|
397
|
+
documentId: document.id,
|
|
398
|
+
index,
|
|
399
|
+
text
|
|
400
|
+
};
|
|
401
|
+
if (document.metadata === void 0) {
|
|
402
|
+
return chunk;
|
|
403
|
+
}
|
|
404
|
+
return { ...chunk, metadata: document.metadata };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export {
|
|
408
|
+
chunkText,
|
|
409
|
+
extractPdfText,
|
|
410
|
+
chunkTextDocuments
|
|
411
|
+
};
|
|
412
|
+
//# sourceMappingURL=chunk-4EWTDOLR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/documents/chunk-text.ts","../src/documents/pdf.ts","../src/documents/text-document.ts"],"sourcesContent":["export type TextChunk = Readonly<{\n index: number;\n text: string;\n start: number;\n end: number;\n}>;\n\nexport type ChunkTextOptions =\n | Readonly<{\n text: string;\n strategy: \"fixed\";\n maxSize: number;\n overlap?: number;\n }>\n | Readonly<{\n text: string;\n strategy: \"recursive\";\n maxSize: number;\n overlap?: number;\n separators: readonly string[];\n }>;\n\ntype TextRange = Readonly<{\n start: number;\n end: number;\n}>;\n\nexport function chunkText(options: ChunkTextOptions): readonly TextChunk[] {\n const validated = validateOptions(options);\n if (validated.text.length === 0) {\n return [];\n }\n\n if (validated.strategy === \"fixed\") {\n return chunkFixed(validated.text, validated.maxSize, validated.overlap);\n }\n\n const ranges = resolveRecursiveRanges(\n validated.text,\n { start: 0, end: validated.text.length },\n validated.maxSize,\n validated.separators,\n );\n return applyRecursiveOverlap(validated.text, ranges, validated.maxSize, validated.overlap);\n}\n\nfunction validateOptions(options: ChunkTextOptions):\n | Readonly<{\n text: string;\n strategy: \"fixed\";\n maxSize: number;\n overlap: number;\n }>\n | Readonly<{\n text: string;\n strategy: \"recursive\";\n maxSize: number;\n overlap: number;\n separators: readonly string[];\n }> {\n if (typeof options !== \"object\" || options === null) {\n throw new TypeError(\"chunkText options must be an object\");\n }\n\n const value = options as unknown as Record<string, unknown>;\n if (typeof value.text !== \"string\") {\n throw new TypeError(\"chunkText text must be a string\");\n }\n if (!Number.isSafeInteger(value.maxSize) || (value.maxSize as number) <= 0) {\n throw new RangeError(\"chunkText maxSize must be a positive safe integer\");\n }\n\n const maxSize = value.maxSize as number;\n const overlap = value.overlap === undefined ? 0 : value.overlap;\n if (!Number.isSafeInteger(overlap) || (overlap as number) < 0 || (overlap as number) >= maxSize) {\n throw new RangeError(\n \"chunkText overlap must be a non-negative safe integer smaller than maxSize\",\n );\n }\n\n if (value.strategy === \"fixed\") {\n if (\"separators\" in value) {\n throw new TypeError(\"chunkText fixed strategy does not accept separators\");\n }\n return {\n text: value.text,\n strategy: \"fixed\",\n maxSize,\n overlap: overlap as number,\n };\n }\n\n if (value.strategy !== \"recursive\") {\n throw new TypeError('chunkText strategy must be either \"fixed\" or \"recursive\"');\n }\n if (!Array.isArray(value.separators) || value.separators.length === 0) {\n throw new TypeError(\"chunkText recursive separators must be a non-empty array\");\n }\n\n const separators = value.separators as unknown[];\n const seen = new Set<string>();\n for (const separator of separators) {\n if (typeof separator !== \"string\" || separator.length === 0) {\n throw new TypeError(\"chunkText recursive separators must contain non-empty strings\");\n }\n if (seen.has(separator)) {\n throw new TypeError(\"chunkText recursive separators must not contain duplicates\");\n }\n seen.add(separator);\n }\n\n return {\n text: value.text,\n strategy: \"recursive\",\n maxSize,\n overlap: overlap as number,\n separators: separators as string[],\n };\n}\n\nfunction chunkFixed(text: string, maxSize: number, overlap: number): readonly TextChunk[] {\n const chunks: TextChunk[] = [];\n let start = 0;\n\n while (start < text.length) {\n const end = Math.min(start + maxSize, text.length);\n chunks.push(createChunk(text, chunks.length, start, end));\n if (end === text.length) {\n break;\n }\n start = end - overlap;\n }\n\n return chunks;\n}\n\nfunction resolveRecursiveRanges(\n text: string,\n initialRange: TextRange,\n maxSize: number,\n separators: readonly string[],\n): readonly TextRange[] {\n const resolved: TextRange[] = [];\n const pending: Array<Readonly<{ range: TextRange; separatorIndex: number }>> = [\n { range: initialRange, separatorIndex: 0 },\n ];\n\n while (pending.length > 0) {\n const current = pending.pop();\n if (current === undefined) {\n break;\n }\n if (current.range.end - current.range.start <= maxSize) {\n resolved.push(current.range);\n continue;\n }\n\n let pieces: readonly TextRange[] | undefined;\n let nextSeparatorIndex = separators.length;\n for (let index = current.separatorIndex; index < separators.length; index += 1) {\n const candidate = splitAfterSeparator(text, current.range, separators[index] as string);\n if (candidate.length > 1) {\n pieces = candidate;\n nextSeparatorIndex = index + 1;\n break;\n }\n }\n\n if (pieces === undefined) {\n resolved.push(...hardSplit(current.range, maxSize));\n continue;\n }\n\n for (let index = pieces.length - 1; index >= 0; index -= 1) {\n pending.push({ range: pieces[index] as TextRange, separatorIndex: nextSeparatorIndex });\n }\n }\n\n return resolved;\n}\n\nfunction splitAfterSeparator(text: string, range: TextRange, separator: string): TextRange[] {\n const ranges: TextRange[] = [];\n let start = range.start;\n let searchFrom = range.start;\n\n while (searchFrom < range.end) {\n const separatorStart = text.indexOf(separator, searchFrom);\n if (separatorStart < 0 || separatorStart + separator.length > range.end) {\n break;\n }\n const end = separatorStart + separator.length;\n if (end > start) {\n ranges.push({ start, end });\n }\n start = end;\n searchFrom = end;\n }\n\n if (start < range.end) {\n ranges.push({ start, end: range.end });\n }\n return ranges;\n}\n\nfunction hardSplit(range: TextRange, maxSize: number): readonly TextRange[] {\n const ranges: TextRange[] = [];\n for (let start = range.start; start < range.end; start += maxSize) {\n ranges.push({ start, end: Math.min(start + maxSize, range.end) });\n }\n return ranges;\n}\n\nfunction applyRecursiveOverlap(\n text: string,\n ranges: readonly TextRange[],\n maxSize: number,\n overlap: number,\n): readonly TextChunk[] {\n const boundaries = ranges.map((range) => range.end);\n const chunks: TextChunk[] = [];\n let start = 0;\n let previousEnd = 0;\n\n while (start < text.length) {\n const limit = Math.min(start + maxSize, text.length);\n const boundary = findLastBoundaryAtMost(boundaries, limit);\n const end = boundary !== undefined && boundary > previousEnd ? boundary : limit;\n\n chunks.push(createChunk(text, chunks.length, start, end));\n if (end === text.length) {\n break;\n }\n previousEnd = end;\n start = Math.max(0, end - overlap);\n }\n\n return chunks;\n}\n\nfunction findLastBoundaryAtMost(boundaries: readonly number[], limit: number): number | undefined {\n let low = 0;\n let high = boundaries.length;\n while (low < high) {\n const middle = low + Math.floor((high - low) / 2);\n if ((boundaries[middle] as number) <= limit) {\n low = middle + 1;\n } else {\n high = middle;\n }\n }\n return boundaries[low - 1];\n}\n\nfunction createChunk(text: string, index: number, start: number, end: number): TextChunk {\n return { index, text: text.slice(start, end), start, end };\n}\n","export type PdfTextPage = Readonly<{\n pageNumber: number;\n text: string;\n}>;\n\nexport type ExtractPdfTextOptions = Readonly<{\n data: Uint8Array;\n abortSignal?: AbortSignal;\n}>;\n\nexport type ExtractPdfTextResult = Readonly<{\n pages: readonly PdfTextPage[];\n}>;\n\nexport async function extractPdfText(\n options: ExtractPdfTextOptions,\n): Promise<ExtractPdfTextResult> {\n if (typeof options !== \"object\" || options === null) {\n throw new TypeError(\"extractPdfText options must be an object\");\n }\n if (!(options.data instanceof Uint8Array)) {\n throw new TypeError(\"extractPdfText data must be a Uint8Array\");\n }\n if (options.abortSignal !== undefined && !isAbortSignal(options.abortSignal)) {\n throw new TypeError(\"extractPdfText abortSignal must be an AbortSignal\");\n }\n\n const { abortSignal } = options;\n abortSignal?.throwIfAborted();\n const data = new Uint8Array(options.data);\n const pdfjs = await loadPdfjs();\n abortSignal?.throwIfAborted();\n\n const loadingTask = pdfjs.getDocument({ data });\n let destroyPromise: Promise<void> | undefined;\n const destroy = (): Promise<void> => {\n destroyPromise ??= loadingTask.destroy();\n return destroyPromise;\n };\n const abort = createAbortPromise(abortSignal, destroy);\n\n return runWithCleanup(\n async () => {\n const document = await abort.wait(loadingTask.promise);\n const pages: PdfTextPage[] = [];\n for (let pageNumber = 1; pageNumber <= document.numPages; pageNumber += 1) {\n abortSignal?.throwIfAborted();\n const page = await abort.wait(document.getPage(pageNumber));\n const content = await abort.wait(page.getTextContent());\n pages.push({ pageNumber, text: extractPageText(content.items) });\n }\n return { pages };\n },\n async () => {\n abort.dispose();\n await destroy();\n },\n );\n}\n\nasync function loadPdfjs(): Promise<typeof import(\"pdfjs-dist/legacy/build/pdf.mjs\")> {\n try {\n return await import(\"pdfjs-dist/legacy/build/pdf.mjs\");\n } catch (cause) {\n if (!isMissingPdfjsDependency(cause)) {\n throw cause;\n }\n throw new Error(\n 'PDF extraction requires the optional \"pdfjs-dist\" package. Install it in your application to use extractPdfText().',\n { cause },\n );\n }\n}\n\nfunction isMissingPdfjsDependency(error: unknown): boolean {\n const seen = new Set<unknown>();\n let current = error;\n while (current instanceof Error && !seen.has(current)) {\n seen.add(current);\n if (\n current.message.includes(\"pdfjs-dist\") &&\n \"code\" in current &&\n current.code === \"ERR_MODULE_NOT_FOUND\"\n ) {\n return true;\n }\n current = \"cause\" in current ? current.cause : undefined;\n }\n return false;\n}\n\nasync function runWithCleanup<Result>(\n operation: () => Promise<Result>,\n cleanup: () => Promise<void>,\n): Promise<Result> {\n let outcome: { ok: true; value: Result } | { ok: false; error: unknown };\n try {\n outcome = { ok: true, value: await operation() };\n } catch (error) {\n outcome = { ok: false, error };\n }\n\n let cleanupError: unknown;\n let cleanupFailed = false;\n try {\n await cleanup();\n } catch (error) {\n cleanupFailed = true;\n cleanupError = error;\n }\n\n if (!outcome.ok) {\n if (cleanupFailed) {\n throw new AggregateError(\n [outcome.error, cleanupError],\n \"PDF extraction and cleanup both failed\",\n { cause: outcome.error },\n );\n }\n throw outcome.error;\n }\n if (cleanupFailed) {\n throw cleanupError;\n }\n return outcome.value;\n}\n\nfunction isAbortSignal(value: unknown): value is AbortSignal {\n if (typeof value !== \"object\" || value === null) {\n return false;\n }\n const candidate = value as Partial<AbortSignal>;\n return (\n typeof candidate.aborted === \"boolean\" &&\n typeof candidate.addEventListener === \"function\" &&\n typeof candidate.removeEventListener === \"function\" &&\n typeof candidate.throwIfAborted === \"function\"\n );\n}\n\nfunction extractPageText(items: readonly unknown[]): string {\n let text = \"\";\n for (const item of items) {\n if (typeof item !== \"object\" || item === null) {\n throw new TypeError(\"PDF text content contains a malformed item\");\n }\n if (!(\"str\" in item)) {\n if (\n \"type\" in item &&\n (item.type === \"beginMarkedContent\" ||\n item.type === \"beginMarkedContentProps\" ||\n item.type === \"endMarkedContent\")\n ) {\n continue;\n }\n throw new TypeError(\"PDF text content contains an unknown non-text item\");\n }\n if (typeof item.str !== \"string\") {\n throw new TypeError(\"PDF text content item str must be a string\");\n }\n if (!(\"hasEOL\" in item) || typeof item.hasEOL !== \"boolean\") {\n throw new TypeError(\"PDF text content item hasEOL must be a boolean\");\n }\n text += item.str;\n if (item.hasEOL) {\n text += \"\\n\";\n }\n }\n return text;\n}\n\nfunction createAbortPromise(\n abortSignal: AbortSignal | undefined,\n destroy: () => Promise<void>,\n): {\n wait<T>(promise: Promise<T>): Promise<T>;\n dispose(): void;\n} {\n if (abortSignal === undefined) {\n return {\n wait: (promise) => promise,\n dispose: () => undefined,\n };\n }\n\n let rejectAbort: ((reason?: unknown) => void) | undefined;\n const aborted = new Promise<never>((_resolve, reject) => {\n rejectAbort = reject;\n });\n const onAbort = () => {\n void destroy();\n rejectAbort?.(\n abortSignal.reason ?? new DOMException(\"The operation was aborted\", \"AbortError\"),\n );\n };\n abortSignal.addEventListener(\"abort\", onAbort, { once: true });\n if (abortSignal.aborted) {\n onAbort();\n }\n\n return {\n wait: (promise) => Promise.race([promise, aborted]),\n dispose: () => abortSignal.removeEventListener(\"abort\", onAbort),\n };\n}\n","import { chunkText } from \"./chunk-text\";\n\nexport type TextDocumentMetadata = Record<string, string | number | boolean>;\n\nexport type TextDocument<Metadata = TextDocumentMetadata> = Readonly<{\n id: string;\n text: string;\n metadata?: Metadata | undefined;\n}>;\n\nexport type TextDocumentChunk<Metadata = TextDocumentMetadata> = Readonly<{\n id: string;\n documentId: string;\n index: number;\n text: string;\n metadata?: Metadata | undefined;\n}>;\n\nexport type TextDocumentChunkingOptions =\n | Readonly<{ strategy: \"none\" }>\n | Readonly<{\n strategy: \"fixed\";\n maxSize: number;\n overlap?: number | undefined;\n }>\n | Readonly<{\n strategy: \"recursive\";\n maxSize: number;\n overlap?: number | undefined;\n separators: readonly string[];\n }>;\n\nexport type ChunkTextDocumentsOptions<Metadata = TextDocumentMetadata> = Readonly<{\n documents: readonly TextDocument<Metadata>[];\n chunking?: TextDocumentChunkingOptions | undefined;\n}>;\n\nexport function chunkTextDocuments<Metadata>(\n options: ChunkTextDocumentsOptions<Metadata>,\n): readonly TextDocumentChunk<Metadata>[] {\n if (typeof options !== \"object\" || options === null) {\n throw new TypeError(\"chunkTextDocuments options must contain a documents array\");\n }\n const documents = options.documents;\n if (!Array.isArray(documents)) {\n throw new TypeError(\"chunkTextDocuments options must contain a documents array\");\n }\n\n const ids = new Set<string>();\n const chunks: TextDocumentChunk<Metadata>[] = [];\n for (const document of documents as readonly TextDocument<Metadata>[]) {\n validateDocument(document, ids);\n chunks.push(...chunkDocument(document, options.chunking));\n }\n return chunks;\n}\n\nfunction validateDocument<Metadata>(document: TextDocument<Metadata>, ids: Set<string>): void {\n if (typeof document !== \"object\" || document === null) {\n throw new TypeError(\"Text documents must be objects\");\n }\n if (typeof document.id !== \"string\" || document.id.length === 0) {\n throw new TypeError(\"Text document ids must be non-empty strings\");\n }\n if (ids.has(document.id)) {\n throw new TypeError(`Duplicate text document id: ${document.id}`);\n }\n if (typeof document.text !== \"string\" || document.text.length === 0) {\n throw new TypeError(`Text document ${document.id} must contain non-empty text`);\n }\n ids.add(document.id);\n}\n\nfunction chunkDocument<Metadata>(\n document: TextDocument<Metadata>,\n chunking: TextDocumentChunkingOptions | undefined,\n): readonly TextDocumentChunk<Metadata>[] {\n if (chunking === undefined || chunking.strategy === \"none\") {\n return [createDocumentChunk(document, 0, document.text)];\n }\n\n const textChunks = chunkDocumentText(document.text, chunking);\n return textChunks.map((chunk) => createDocumentChunk(document, chunk.index, chunk.text));\n}\n\nfunction chunkDocumentText(\n text: string,\n chunking: Exclude<TextDocumentChunkingOptions, Readonly<{ strategy: \"none\" }>>,\n) {\n const overlap = chunking.overlap ?? 0;\n if (chunking.strategy === \"fixed\") {\n return chunkText({\n text,\n strategy: \"fixed\",\n maxSize: chunking.maxSize,\n overlap,\n });\n }\n return chunkText({\n text,\n strategy: \"recursive\",\n maxSize: chunking.maxSize,\n overlap,\n separators: chunking.separators,\n });\n}\n\nfunction createDocumentChunk<Metadata>(\n document: TextDocument<Metadata>,\n index: number,\n text: string,\n): TextDocumentChunk<Metadata> {\n const chunk = {\n id: `${document.id}:chunk:${index}`,\n documentId: document.id,\n index,\n text,\n };\n if (document.metadata === undefined) {\n return chunk;\n }\n return { ...chunk, metadata: document.metadata };\n}\n"],"mappings":";AA2BO,SAAS,UAAU,SAAiD;AACzE,QAAM,YAAY,gBAAgB,OAAO;AACzC,MAAI,UAAU,KAAK,WAAW,GAAG;AAC/B,WAAO,CAAC;AAAA,EACV;AAEA,MAAI,UAAU,aAAa,SAAS;AAClC,WAAO,WAAW,UAAU,MAAM,UAAU,SAAS,UAAU,OAAO;AAAA,EACxE;AAEA,QAAM,SAAS;AAAA,IACb,UAAU;AAAA,IACV,EAAE,OAAO,GAAG,KAAK,UAAU,KAAK,OAAO;AAAA,IACvC,UAAU;AAAA,IACV,UAAU;AAAA,EACZ;AACA,SAAO,sBAAsB,UAAU,MAAM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";
|
|
27
27
|
import {
|
|
28
28
|
ToolNotFoundError,
|
|
29
29
|
ToolOutput,
|
|
@@ -38,7 +38,7 @@ import {
|
|
|
38
38
|
retrieveDocuments,
|
|
39
39
|
toolMayRequireApproval,
|
|
40
40
|
toolResultContentToText
|
|
41
|
-
} from "./chunk-
|
|
41
|
+
} from "./chunk-LZD36ZAP.js";
|
|
42
42
|
import {
|
|
43
43
|
AgentRunBlockedError,
|
|
44
44
|
AgentRunCancelledError,
|
|
@@ -4772,4 +4772,4 @@ export {
|
|
|
4772
4772
|
markResolvedAgentOptions,
|
|
4773
4773
|
Agent
|
|
4774
4774
|
};
|
|
4775
|
-
//# sourceMappingURL=chunk-
|
|
4775
|
+
//# sourceMappingURL=chunk-5TIXGU5U.js.map
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chunkTextDocuments
|
|
3
|
+
} from "./chunk-4EWTDOLR.js";
|
|
1
4
|
import {
|
|
2
5
|
cosineSimilarity,
|
|
6
|
+
embedDocuments,
|
|
3
7
|
embedSparseQuery,
|
|
4
8
|
embedText
|
|
5
9
|
} from "./chunk-K5L7R7XM.js";
|
|
@@ -432,6 +436,85 @@ function resolveRetries(setting) {
|
|
|
432
436
|
return setting === void 0 || setting === false ? void 0 : resolveRetryOptions(setting);
|
|
433
437
|
}
|
|
434
438
|
|
|
439
|
+
// src/vector-store/ingest.ts
|
|
440
|
+
async function ingestVectorText(options) {
|
|
441
|
+
return ingestVectorDocuments({
|
|
442
|
+
store: options.store,
|
|
443
|
+
documents: [options.document],
|
|
444
|
+
embeddingModel: options.embeddingModel,
|
|
445
|
+
chunking: options.chunking,
|
|
446
|
+
retries: options.retries,
|
|
447
|
+
concurrency: options.concurrency,
|
|
448
|
+
providerOptions: options.providerOptions,
|
|
449
|
+
abortSignal: options.abortSignal
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
async function ingestVectorDocuments(options) {
|
|
453
|
+
for (const document of options.documents) {
|
|
454
|
+
validateVectorMetadata(document.metadata, document.id);
|
|
455
|
+
}
|
|
456
|
+
const chunks = chunkTextDocuments({
|
|
457
|
+
documents: options.documents,
|
|
458
|
+
chunking: options.chunking
|
|
459
|
+
});
|
|
460
|
+
const chunksByDocument = groupChunksByDocument(chunks);
|
|
461
|
+
const embedded = await embedDocuments({
|
|
462
|
+
model: options.embeddingModel,
|
|
463
|
+
documents: [...options.documents],
|
|
464
|
+
id: (document) => document.id,
|
|
465
|
+
content: (document) => {
|
|
466
|
+
const documentChunks = chunksByDocument.get(document.id);
|
|
467
|
+
if (documentChunks === void 0) {
|
|
468
|
+
throw new TypeError(`Text document ${document.id} has no chunks`);
|
|
469
|
+
}
|
|
470
|
+
return documentChunks.map((chunk) => chunk.text);
|
|
471
|
+
},
|
|
472
|
+
metadata: (document) => document.metadata,
|
|
473
|
+
retries: options.retries,
|
|
474
|
+
concurrency: options.concurrency,
|
|
475
|
+
abortSignal: options.abortSignal
|
|
476
|
+
});
|
|
477
|
+
await options.store.upsert({
|
|
478
|
+
documents: embedded.documents,
|
|
479
|
+
providerOptions: options.providerOptions
|
|
480
|
+
});
|
|
481
|
+
return { documents: embedded.documents };
|
|
482
|
+
}
|
|
483
|
+
function validateVectorMetadata(metadata, documentId) {
|
|
484
|
+
if (metadata === void 0) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
if (typeof metadata !== "object" || metadata === null || Array.isArray(metadata)) {
|
|
488
|
+
throw new TypeError(`Text document ${documentId} metadata must be an object`);
|
|
489
|
+
}
|
|
490
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
491
|
+
validateVectorMetadataValue(value, `Text document ${documentId} metadata.${key}`);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
function validateVectorMetadataValue(value, label) {
|
|
495
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
499
|
+
throw new TypeError(`${label} must be a finite vector metadata value`);
|
|
500
|
+
}
|
|
501
|
+
if (Number.isInteger(value) && !Number.isSafeInteger(value)) {
|
|
502
|
+
throw new TypeError(`${label} must be a safe integer`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
function groupChunksByDocument(chunks) {
|
|
506
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
507
|
+
for (const chunk of chunks) {
|
|
508
|
+
const documentChunks = grouped.get(chunk.documentId);
|
|
509
|
+
if (documentChunks === void 0) {
|
|
510
|
+
grouped.set(chunk.documentId, [chunk]);
|
|
511
|
+
} else {
|
|
512
|
+
documentChunks.push(chunk);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return grouped;
|
|
516
|
+
}
|
|
517
|
+
|
|
435
518
|
// src/vector-store/index.ts
|
|
436
519
|
var InMemoryVectorStore = class _InMemoryVectorStore {
|
|
437
520
|
documents = /* @__PURE__ */ new Map();
|
|
@@ -661,7 +744,9 @@ export {
|
|
|
661
744
|
vectorFilter,
|
|
662
745
|
matchesVectorFilter,
|
|
663
746
|
retrieveDocuments,
|
|
747
|
+
ingestVectorText,
|
|
748
|
+
ingestVectorDocuments,
|
|
664
749
|
InMemoryVectorStore,
|
|
665
750
|
createVectorSearchTool
|
|
666
751
|
};
|
|
667
|
-
//# sourceMappingURL=chunk-
|
|
752
|
+
//# sourceMappingURL=chunk-LZD36ZAP.js.map
|