@mikesaintsg/core 0.0.1 → 0.0.2

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/dist/index.js CHANGED
@@ -1,679 +1,125 @@
1
- class O extends Error {
1
+ class x extends Error {
2
2
  cause;
3
+ constructor(t, o) {
4
+ super(t), this.name = this.constructor.name, this.cause = o;
5
+ }
3
6
  }
4
- function Z(r) {
5
- return r instanceof O;
7
+ function F(e) {
8
+ return e instanceof x;
6
9
  }
7
- class E extends Error {
10
+ class A extends Error {
8
11
  code;
9
12
  cause;
10
- constructor(n, f, i) {
11
- super(f), this.name = "CoreError", this.code = n, this.cause = i;
13
+ constructor(t, o, s) {
14
+ super(o), this.name = "CoreError", this.code = t, this.cause = s;
12
15
  }
13
16
  }
14
- function Q(r) {
15
- return r instanceof E;
16
- }
17
- function M(r) {
18
- return { ok: !0, value: r };
19
- }
20
- function I(r) {
21
- return { ok: !1, error: r };
22
- }
23
- function ee(r) {
24
- return r.ok === !0;
25
- }
26
- function te(r) {
27
- return !r.ok;
28
- }
29
- function ne(r, n) {
30
- return r.ok ? r.value : n;
31
- }
32
- function re(r) {
33
- if (r.ok)
34
- return r.value;
35
- throw r.error instanceof Error ? r.error : new Error(String(r.error));
36
- }
37
- function oe(r, n) {
38
- return r.ok ? M(n(r.value)) : r;
39
- }
40
- function ae(r, n) {
41
- return r.ok ? r : I(n(r.error));
42
- }
43
- function se(r, n) {
44
- return r.ok ? n(r.value) : r;
45
- }
46
- async function P(r) {
47
- const f = new TextEncoder().encode(r), i = await crypto.subtle.digest("SHA-256", f);
48
- return Array.from(new Uint8Array(i)).map((e) => e.toString(16).padStart(2, "0")).join("");
49
- }
50
- function R(r) {
51
- return {
52
- ...r,
53
- embedding: Array.from(r.embedding)
54
- };
55
- }
56
- function S(r) {
57
- const n = r.embedding;
58
- return {
59
- ...r,
60
- embedding: new Float32Array(n)
61
- };
17
+ function P(e) {
18
+ return e instanceof A;
62
19
  }
63
- function x(r) {
64
- return r.byteLength;
20
+ function S(e) {
21
+ return { ok: !0, value: e };
65
22
  }
66
- const N = "text-embedding-3-small", F = 1536, k = "https://api.openai.com/v1", L = "voyage-2", U = 1024, B = "https://api.voyageai.com/v1", $ = 1e3, j = 100, q = 50, H = "documents", G = "metadata", v = "vectorstore_metadata", C = 100, D = "_metadata.json", T = "chunk_", J = 3e4, V = 3e4, z = 10, Y = 100, X = "You have unsaved changes. Are you sure you want to leave?";
67
- function ie(r) {
68
- const {
69
- apiKey: n,
70
- model: f = N,
71
- baseURL: i = k,
72
- dimensions: o = F
73
- } = r;
74
- if (!n)
75
- throw new E("ADAPTER_ERROR", "OpenAI API key is required");
76
- const e = {
77
- provider: "openai",
78
- model: f,
79
- dimensions: o
80
- };
81
- return {
82
- async embed(a, t) {
83
- if (a.length === 0)
84
- return [];
85
- const u = `${i}/embeddings`, s = {
86
- method: "POST",
87
- headers: {
88
- "Content-Type": "application/json",
89
- Authorization: `Bearer ${n}`
90
- },
91
- body: JSON.stringify({
92
- input: a,
93
- model: f,
94
- dimensions: o
95
- })
96
- };
97
- t?.signal && (s.signal = t.signal);
98
- const l = await fetch(u, s);
99
- if (!l.ok) {
100
- const d = await l.text().catch(() => "Unknown error");
101
- throw new E(
102
- "ADAPTER_ERROR",
103
- `OpenAI API error (${l.status}): ${d}`
104
- );
105
- }
106
- return [...(await l.json()).data].sort((d, p) => d.index - p.index).map((d) => new Float32Array(d.embedding));
107
- },
108
- getModelMetadata() {
109
- return e;
110
- }
111
- };
23
+ function I(e) {
24
+ return { ok: !1, error: e };
112
25
  }
113
- function ce(r) {
114
- const {
115
- apiKey: n,
116
- model: f = L,
117
- baseURL: i = B
118
- } = r;
119
- if (!n)
120
- throw new E("ADAPTER_ERROR", "Voyage AI API key is required");
121
- const o = {
122
- provider: "anthropic",
123
- model: f,
124
- dimensions: U
125
- };
126
- return {
127
- async embed(e, a) {
128
- if (e.length === 0)
129
- return [];
130
- const t = `${i}/embeddings`, u = {
131
- method: "POST",
132
- headers: {
133
- "Content-Type": "application/json",
134
- Authorization: `Bearer ${n}`
135
- },
136
- body: JSON.stringify({
137
- input: e,
138
- model: f
139
- })
140
- };
141
- a?.signal && (u.signal = a.signal);
142
- const s = await fetch(t, u);
143
- if (!s.ok) {
144
- const c = await s.text().catch(() => "Unknown error");
145
- throw new E(
146
- "ADAPTER_ERROR",
147
- `Voyage AI API error (${s.status}): ${c}`
148
- );
149
- }
150
- return [...(await s.json()).data].sort((c, d) => c.index - d.index).map((c) => new Float32Array(c.embedding));
151
- },
152
- getModelMetadata() {
153
- return o;
154
- }
155
- };
26
+ function b(e) {
27
+ return e.ok === !0;
156
28
  }
157
- function ue(r, n = {}) {
158
- const {
159
- maxBatchSize: f = j,
160
- flushDelayMs: i = q,
161
- deduplicate: o = !0,
162
- cache: e
163
- } = n, a = [];
164
- let t;
165
- function u() {
166
- t === void 0 && (t = setTimeout(() => {
167
- t = void 0, s();
168
- }, i));
169
- }
170
- async function s() {
171
- if (a.length === 0) return;
172
- const l = a.splice(0, a.length), y = [], c = /* @__PURE__ */ new Map();
173
- for (const m of l) {
174
- const h = c.get(m.contentHash);
175
- h ? o ? h.push(m) : (y.push(m.text), c.set(m.contentHash + "_" + y.length, [m])) : (y.push(m.text), c.set(m.contentHash, [m]));
176
- }
177
- const d = [], p = [], A = /* @__PURE__ */ new Map();
178
- for (const m of l) {
179
- if (A.has(m.contentHash)) continue;
180
- const h = e?.get(m.contentHash);
181
- h ? A.set(m.contentHash, h) : p.includes(m.contentHash) || (d.push(m.text), p.push(m.contentHash));
182
- }
183
- try {
184
- let m = [];
185
- if (d.length > 0) {
186
- m = await r.embed(d);
187
- for (let h = 0; h < m.length; h++) {
188
- const g = p[h], w = m[h];
189
- g !== void 0 && w !== void 0 && (e?.set(g, w), A.set(g, w));
190
- }
191
- }
192
- for (const h of l) {
193
- const g = A.get(h.contentHash);
194
- g ? h.resolve(g) : h.reject(new Error(`No embedding found for content hash: ${h.contentHash}`));
195
- }
196
- } catch (m) {
197
- for (const h of l)
198
- h.reject(m);
199
- }
200
- }
201
- return {
202
- // Passthrough methods from base adapter
203
- async embed(l, y) {
204
- return r.embed(l, y);
205
- },
206
- getModelMetadata() {
207
- return r.getModelMetadata();
208
- },
209
- // Batched methods
210
- async queue(l, y) {
211
- const c = await P(l), d = e?.get(c);
212
- return d || new Promise((p, A) => {
213
- a.push({
214
- text: l,
215
- contentHash: c,
216
- resolve: p,
217
- reject: A
218
- }), a.length >= f ? (t !== void 0 && (clearTimeout(t), t = void 0), s()) : u();
219
- });
220
- },
221
- async queueBatch(l, y) {
222
- const c = l.map((d) => this.queue(d, y));
223
- return Promise.all(c);
224
- },
225
- async flush() {
226
- t !== void 0 && (clearTimeout(t), t = void 0), await s();
227
- },
228
- getPendingCount() {
229
- return a.length;
230
- },
231
- getCache() {
232
- return e;
233
- }
234
- };
29
+ function q(e) {
30
+ return !e.ok;
235
31
  }
236
- function de(r = {}) {
237
- const {
238
- maxEntries: n = $,
239
- maxBytes: f,
240
- ttlMs: i,
241
- onEvict: o
242
- } = r, e = /* @__PURE__ */ new Map();
243
- let a = 0, t = 0;
244
- function u() {
245
- let c = 0;
246
- for (const d of e.values())
247
- c += x(d.embedding);
248
- return c;
249
- }
250
- function s(c) {
251
- return i === void 0 ? !1 : Date.now() - c.createdAt > i;
252
- }
253
- function l() {
254
- for (; e.size > n; )
255
- y();
256
- if (f !== void 0)
257
- for (; u() > f && e.size > 0; )
258
- y();
259
- }
260
- function y() {
261
- let c, d = 1 / 0;
262
- for (const [p, A] of e.entries())
263
- A.lastAccess < d && (d = A.lastAccess, c = p);
264
- if (c !== void 0) {
265
- const p = e.get(c);
266
- e.delete(c), p && o && o(c, p.embedding);
267
- }
268
- }
269
- return {
270
- get(c) {
271
- const d = e.get(c);
272
- if (!d) {
273
- t++;
274
- return;
275
- }
276
- if (s(d)) {
277
- e.delete(c), o && o(c, d.embedding), t++;
278
- return;
279
- }
280
- return d.hitCount++, d.lastAccess = Date.now(), a++, d.embedding;
281
- },
282
- set(c, d) {
283
- const p = Date.now();
284
- e.set(c, {
285
- embedding: d,
286
- createdAt: p,
287
- hitCount: 0,
288
- lastAccess: p
289
- }), l();
290
- },
291
- has(c) {
292
- const d = e.get(c);
293
- return d ? s(d) ? (e.delete(c), o && o(c, d.embedding), !1) : !0 : !1;
294
- },
295
- remove(c) {
296
- const d = e.get(c);
297
- return d && o && o(c, d.embedding), e.delete(c);
298
- },
299
- clear() {
300
- if (o)
301
- for (const [c, d] of e.entries())
302
- o(c, d.embedding);
303
- e.clear(), a = 0, t = 0;
304
- },
305
- getStats() {
306
- const c = e.size, d = a + t;
307
- return {
308
- entries: c,
309
- hits: a,
310
- misses: t,
311
- hitRate: d > 0 ? a / d : 0,
312
- estimatedBytes: u()
313
- };
314
- }
315
- };
32
+ function O(e, t) {
33
+ return e.ok ? e.value : t;
316
34
  }
317
- function fe(r = {}) {
318
- return {
319
- formatSchemas(n) {
320
- return n.map((f) => ({
321
- type: "function",
322
- function: {
323
- name: f.name,
324
- description: f.description,
325
- parameters: f.parameters
326
- }
327
- }));
328
- },
329
- parseToolCalls(n) {
330
- if (!n || typeof n != "object")
331
- return [];
332
- const i = n.choices?.[0]?.message?.tool_calls;
333
- if (!i || !Array.isArray(i))
334
- return [];
335
- const o = [];
336
- for (const e of i) {
337
- if (!e || typeof e != "object" || !("id" in e) || !("function" in e)) continue;
338
- const a = e.function;
339
- if (!a || typeof a != "object" || !("name" in a) || !("arguments" in a)) continue;
340
- let t = {};
341
- try {
342
- const u = JSON.parse(String(a.arguments));
343
- u && typeof u == "object" && !Array.isArray(u) && (t = u);
344
- } catch {
345
- }
346
- o.push({
347
- id: String(e.id),
348
- name: String(a.name),
349
- arguments: t
350
- });
351
- }
352
- return o;
353
- },
354
- formatResult(n) {
355
- return {
356
- tool_call_id: n.callId,
357
- role: "tool",
358
- content: n.success ? JSON.stringify(n.value) : JSON.stringify({ error: n.error })
359
- };
360
- }
361
- };
35
+ function U(e) {
36
+ if (e.ok)
37
+ return e.value;
38
+ throw e.error instanceof Error ? e.error : new Error(String(e.error));
362
39
  }
363
- function le(r = {}) {
364
- return {
365
- formatSchemas(n) {
366
- return n.map((f) => ({
367
- name: f.name,
368
- description: f.description,
369
- input_schema: f.parameters
370
- }));
371
- },
372
- parseToolCalls(n) {
373
- if (!n || typeof n != "object")
374
- return [];
375
- const f = n;
376
- if (!f.content || !Array.isArray(f.content))
377
- return [];
378
- const i = [];
379
- for (const o of f.content) {
380
- if (!o || typeof o != "object" || !("type" in o) || o.type !== "tool_use" || !("id" in o) || !("name" in o) || !("input" in o)) continue;
381
- const a = o, t = a.id, u = a.name, s = a.input;
382
- typeof t != "string" || typeof u != "string" || !s || typeof s != "object" || Array.isArray(s) || i.push({
383
- id: t,
384
- name: u,
385
- arguments: s
386
- });
387
- }
388
- return i;
389
- },
390
- formatResult(n) {
391
- return {
392
- type: "tool_result",
393
- tool_use_id: n.callId,
394
- content: n.success ? JSON.stringify(n.value) : JSON.stringify({ error: n.error }),
395
- is_error: !n.success
396
- };
397
- }
398
- };
40
+ function B(e, t) {
41
+ return e.ok ? S(t(e.value)) : e;
399
42
  }
400
- function me(r) {
401
- const {
402
- database: n,
403
- documentsStore: f = H,
404
- metadataStore: i = G
405
- } = r, o = n.store(f), e = n.store(i);
406
- return {
407
- async load() {
408
- return o.all();
409
- },
410
- async loadMetadata() {
411
- return e.get(v);
412
- },
413
- async save(a) {
414
- const t = Array.isArray(a) ? a : [a];
415
- for (const u of t)
416
- await o.set(u, u.id);
417
- },
418
- async saveMetadata(a) {
419
- await e.set(a, v);
420
- },
421
- async remove(a) {
422
- const t = Array.isArray(a) ? a : [a];
423
- for (const u of t)
424
- await o.remove(u);
425
- },
426
- async clear() {
427
- await o.clear(), await e.clear();
428
- },
429
- async isAvailable() {
430
- try {
431
- return await o.all(), !0;
432
- } catch {
433
- return !1;
434
- }
435
- }
436
- };
43
+ function G(e, t) {
44
+ return e.ok ? e : I(t(e.error));
437
45
  }
438
- function ye(r) {
439
- const {
440
- directory: n,
441
- chunkSize: f = C
442
- } = r;
443
- return {
444
- async load() {
445
- const i = await n.listFiles(), o = [];
446
- for (const e of i)
447
- if (e.getName().startsWith(T))
448
- try {
449
- const t = await e.getText(), u = JSON.parse(t);
450
- for (const s of u)
451
- o.push(S(s));
452
- } catch {
453
- }
454
- return o;
455
- },
456
- async loadMetadata() {
457
- const i = await n.getFile(D);
458
- if (i)
459
- try {
460
- const o = await i.getText();
461
- return JSON.parse(o);
462
- } catch {
463
- return;
464
- }
465
- },
466
- async save(i) {
467
- const o = Array.isArray(i) ? i : [i];
468
- if (o.length === 0) return;
469
- const e = await this.load(), a = new Map(e.map((s) => [s.id, s]));
470
- for (const s of o)
471
- a.set(s.id, s);
472
- const t = await n.listFiles();
473
- for (const s of t)
474
- s.getName().startsWith(T) && await n.removeFile(s.getName());
475
- const u = Array.from(a.values());
476
- for (let s = 0; s < u.length; s += f) {
477
- const l = u.slice(s, s + f), y = Math.floor(s / f), c = `${T}${y}.json`;
478
- await (await n.createFile(c)).write(JSON.stringify(l.map(R)));
479
- }
480
- },
481
- async saveMetadata(i) {
482
- await (await n.createFile(D)).write(JSON.stringify(i));
483
- },
484
- async remove(i) {
485
- const o = Array.isArray(i) ? i : [i], e = new Set(o), t = (await this.load()).filter((u) => !e.has(u.id));
486
- await this.clear(), t.length > 0 && await this.save(t);
487
- },
488
- async clear() {
489
- const i = await n.listFiles();
490
- for (const o of i)
491
- await n.removeFile(o.getName());
492
- },
493
- async isAvailable() {
494
- try {
495
- return await n.listFiles(), !0;
496
- } catch {
497
- return !1;
498
- }
499
- }
500
- };
46
+ function $(e, t) {
47
+ return e.ok ? t(e.value) : e;
501
48
  }
502
- function he(r) {
503
- const {
504
- baseURL: n,
505
- headers: f = {},
506
- timeout: i = J
507
- } = r;
508
- async function o(e, a) {
509
- const t = new AbortController(), u = setTimeout(() => t.abort(), i);
510
- try {
511
- return await fetch(e, {
512
- ...a,
513
- signal: t.signal,
514
- headers: {
515
- "Content-Type": "application/json",
516
- ...f,
517
- ...a.headers
518
- }
519
- });
520
- } finally {
521
- clearTimeout(u);
522
- }
523
- }
524
- return {
525
- async load() {
526
- const e = await o(`${n}/documents`, {
527
- method: "GET"
528
- });
529
- if (!e.ok)
530
- throw new E(
531
- "ADAPTER_ERROR",
532
- `HTTP load failed (${e.status})`
533
- );
534
- return (await e.json()).map(S);
535
- },
536
- async loadMetadata() {
537
- try {
538
- const e = await o(`${n}/metadata`, {
539
- method: "GET"
540
- });
541
- if (e.status === 404)
542
- return;
543
- if (!e.ok)
544
- throw new E(
545
- "ADAPTER_ERROR",
546
- `HTTP loadMetadata failed (${e.status})`
547
- );
548
- return await e.json();
549
- } catch (e) {
550
- if (e instanceof E) throw e;
551
- return;
552
- }
553
- },
554
- async save(e) {
555
- const a = Array.isArray(e) ? e : [e], t = await o(`${n}/documents`, {
556
- method: "POST",
557
- body: JSON.stringify(a.map(R))
558
- });
559
- if (!t.ok)
560
- throw new E(
561
- "ADAPTER_ERROR",
562
- `HTTP save failed (${t.status})`
563
- );
564
- },
565
- async saveMetadata(e) {
566
- const a = await o(`${n}/metadata`, {
567
- method: "PUT",
568
- body: JSON.stringify(e)
569
- });
570
- if (!a.ok)
571
- throw new E(
572
- "ADAPTER_ERROR",
573
- `HTTP saveMetadata failed (${a.status})`
574
- );
575
- },
576
- async remove(e) {
577
- const a = Array.isArray(e) ? e : [e], t = await o(`${n}/documents`, {
578
- method: "DELETE",
579
- body: JSON.stringify({ ids: a })
580
- });
581
- if (!t.ok)
582
- throw new E(
583
- "ADAPTER_ERROR",
584
- `HTTP remove failed (${t.status})`
585
- );
586
- },
587
- async clear() {
588
- const e = await o(`${n}/documents`, {
589
- method: "DELETE"
590
- });
591
- if (!e.ok)
592
- throw new E(
593
- "ADAPTER_ERROR",
594
- `HTTP clear failed (${e.status})`
595
- );
596
- },
597
- async isAvailable() {
598
- try {
599
- return (await o(`${n}/health`, {
600
- method: "GET"
601
- })).ok;
602
- } catch {
603
- return !1;
604
- }
605
- }
606
- };
49
+ async function j(e) {
50
+ const o = new TextEncoder().encode(e), s = await crypto.subtle.digest("SHA-256", o);
51
+ return Array.from(new Uint8Array(s)).map((a) => a.toString(16).padStart(2, "0")).join("");
607
52
  }
608
- function pe(r) {
53
+ const k = 3e4, M = 10, R = 100, L = "You have unsaved changes. Are you sure you want to leave?";
54
+ function H(e) {
609
55
  const {
610
- registry: n,
611
- timeout: f = V,
612
- onError: i,
613
- onBeforeExecute: o,
614
- onAfterExecute: e
615
- } = r;
616
- async function a(t) {
617
- if (!n.has(t.name))
56
+ registry: t,
57
+ timeout: o = k,
58
+ onError: s,
59
+ onBeforeExecute: i,
60
+ onAfterExecute: a
61
+ } = e;
62
+ async function u(r) {
63
+ if (!t.has(r.name))
618
64
  return {
619
- callId: t.id,
620
- name: t.name,
65
+ callId: r.id,
66
+ name: r.name,
621
67
  success: !1,
622
- error: `Tool not found: ${t.name}`
68
+ error: `Tool not found: ${r.name}`
623
69
  };
624
- o?.(t);
70
+ i?.(r);
625
71
  try {
626
- const u = new Promise((l, y) => {
72
+ const n = new Promise((f, m) => {
627
73
  setTimeout(() => {
628
- y(new Error(`Tool execution timed out after ${f}ms`));
629
- }, f);
630
- }), s = await Promise.race([
631
- n.execute(t.name, t.arguments),
632
- u
74
+ m(new Error(`Tool execution timed out after ${o}ms`));
75
+ }, o);
76
+ }), c = await Promise.race([
77
+ t.execute(r.name, r.arguments),
78
+ n
633
79
  ]);
634
- return e?.(t, s), {
635
- callId: t.id,
636
- name: t.name,
80
+ return a?.(r, c), {
81
+ callId: r.id,
82
+ name: r.name,
637
83
  success: !0,
638
- value: s
84
+ value: c
639
85
  };
640
- } catch (u) {
641
- i?.(u, t);
642
- const s = u instanceof Error ? u.message : String(u);
86
+ } catch (n) {
87
+ s?.(n, r);
88
+ const c = n instanceof Error ? n.message : String(n);
643
89
  return {
644
- callId: t.id,
645
- name: t.name,
90
+ callId: r.id,
91
+ name: r.name,
646
92
  success: !1,
647
- error: s
93
+ error: c
648
94
  };
649
95
  }
650
96
  }
651
97
  return {
652
- async execute(t) {
653
- return a(t);
98
+ async execute(r) {
99
+ return u(r);
654
100
  },
655
- async executeAll(t) {
101
+ async executeAll(r) {
656
102
  return await Promise.all(
657
- t.map((s) => a(s))
103
+ r.map((c) => u(c))
658
104
  );
659
105
  },
660
- hasTool(t) {
661
- return n.has(t);
106
+ hasTool(r) {
107
+ return t.has(r);
662
108
  }
663
109
  };
664
110
  }
665
- function Ae(r) {
111
+ function N(e) {
666
112
  const {
667
- vectorStore: n,
668
- name: f,
669
- description: i,
670
- defaultLimit: o = z,
671
- maxLimit: e = Y,
672
- scoreThreshold: a,
673
- formatResult: t = K,
674
- extendParameters: u = {},
675
- buildFilter: s
676
- } = r, l = {
113
+ vectorStore: t,
114
+ name: o,
115
+ description: s,
116
+ defaultLimit: i = M,
117
+ maxLimit: a = R,
118
+ scoreThreshold: u,
119
+ formatResult: r = _,
120
+ extendParameters: n = {},
121
+ buildFilter: c
122
+ } = e, f = {
677
123
  type: "object",
678
124
  properties: {
679
125
  query: {
@@ -682,147 +128,119 @@ function Ae(r) {
682
128
  },
683
129
  limit: {
684
130
  type: "integer",
685
- description: `Maximum number of results to return (default: ${o}, max: ${e})`,
131
+ description: `Maximum number of results to return (default: ${i}, max: ${a})`,
686
132
  minimum: 1,
687
- maximum: e,
688
- default: o
133
+ maximum: a,
134
+ default: i
689
135
  },
690
- ...u
136
+ ...n
691
137
  },
692
138
  required: ["query"]
693
- }, y = {
694
- name: f,
695
- description: i,
696
- parameters: l
697
- };
698
- async function c(d) {
699
- const p = d.query;
700
- if (typeof p != "string" || p.length === 0)
139
+ }, m = {
140
+ name: o,
141
+ description: s,
142
+ parameters: f
143
+ };
144
+ async function d(h) {
145
+ const y = h.query;
146
+ if (typeof y != "string" || y.length === 0)
701
147
  return [];
702
- const A = d.limit;
703
- let m = o;
704
- typeof A == "number" && Number.isInteger(A) && (m = Math.min(Math.max(1, A), e));
705
- const h = s?.(d), g = h !== void 0 ? { limit: m, filter: h } : { limit: m }, w = await n.search(p, g);
706
- let _ = w;
707
- return a !== void 0 && (_ = w.filter((b) => b.score >= a)), _.map(t);
148
+ const p = h.limit;
149
+ let E = i;
150
+ typeof p == "number" && Number.isInteger(p) && (E = Math.min(Math.max(1, p), a));
151
+ const l = c?.(h), v = l !== void 0 ? { limit: E, filter: l } : { limit: E }, w = await t.search(y, v);
152
+ let g = w;
153
+ return u !== void 0 && (g = w.filter((T) => T.score >= u)), g.map(r);
708
154
  }
709
- return { schema: y, handler: c };
155
+ return { schema: m, handler: d };
710
156
  }
711
- function K(r) {
157
+ function _(e) {
712
158
  return {
713
- content: r.content,
714
- score: r.score,
715
- metadata: r.metadata
159
+ content: e.content,
160
+ score: e.score,
161
+ metadata: e.metadata
716
162
  };
717
163
  }
718
- function Ee(r) {
164
+ function V(e) {
719
165
  const {
720
- form: n,
721
- confirmFn: f,
722
- message: i = X,
723
- excludePages: o = [],
724
- onlyFromPages: e
725
- } = r;
726
- return async function(t, u) {
727
- return W(t, u, o, e) || !n.isDirty() ? !0 : await Promise.resolve(f(i));
166
+ form: t,
167
+ confirmFn: o,
168
+ message: s = L,
169
+ excludePages: i = [],
170
+ onlyFromPages: a
171
+ } = e;
172
+ return async function(r, n) {
173
+ return D(r, n, i, a) || !t.isDirty() ? !0 : await Promise.resolve(o(s));
728
174
  };
729
175
  }
730
- function W(r, n, f, i) {
731
- return !!(f.includes(r) || i !== void 0 && !i.includes(n));
176
+ function D(e, t, o, s) {
177
+ return !!(o.includes(e) || s !== void 0 && !s.includes(t));
732
178
  }
733
- function ge(r) {
179
+ function Y(e) {
734
180
  const {
735
- database: n,
736
- storeName: f,
737
- autoprune: i,
738
- onSaveError: o
739
- } = r, e = n.store(f);
740
- i !== void 0 && i > 0 && a(i);
741
- async function a(t) {
742
- const s = Date.now() - t, l = await e.all();
743
- let y = 0;
744
- for (const c of l)
745
- c.updatedAt < s && (await e.remove(c.id), y++);
746
- return y;
181
+ database: t,
182
+ storeName: o,
183
+ autoprune: s,
184
+ onSaveError: i
185
+ } = e, a = t.store(o);
186
+ s !== void 0 && s > 0 && u(s);
187
+ async function u(r) {
188
+ const c = Date.now() - r, f = await a.all();
189
+ let m = 0;
190
+ for (const d of f)
191
+ d.updatedAt < c && (await a.remove(d.id), m++);
192
+ return m;
747
193
  }
748
194
  return {
749
- async save(t, u) {
195
+ async save(r, n) {
750
196
  try {
751
- const s = {
752
- id: t,
753
- messages: [...u.getMessages()],
754
- metadata: u.getMetadata(),
197
+ const c = {
198
+ id: r,
199
+ messages: [...n.getMessages()],
200
+ metadata: n.getMetadata(),
755
201
  updatedAt: Date.now()
756
202
  };
757
- await e.set(s, t);
758
- } catch (s) {
759
- throw o?.(s, t), s;
203
+ await a.set(c, r);
204
+ } catch (c) {
205
+ throw i?.(c, r), c;
760
206
  }
761
207
  },
762
- async load(t) {
763
- return e.get(t);
208
+ async load(r) {
209
+ return a.get(r);
764
210
  },
765
- async delete(t) {
766
- await e.remove(t);
211
+ async delete(r) {
212
+ await a.remove(r);
767
213
  },
768
214
  async list() {
769
- return (await e.all()).map((u) => u.id);
215
+ return (await a.all()).map((n) => n.id);
770
216
  },
771
- async prune(t) {
772
- return a(t);
217
+ async prune(r) {
218
+ return u(r);
773
219
  }
774
220
  };
775
221
  }
776
222
  export {
777
- V as BRIDGE_DEFAULT_TIMEOUT,
778
- E as CoreError,
779
- q as EMBEDDING_BATCH_DEFAULT_FLUSH_DELAY_MS,
780
- j as EMBEDDING_BATCH_DEFAULT_MAX_SIZE,
781
- $ as EMBEDDING_CACHE_DEFAULT_MAX_ENTRIES,
782
- O as EcosystemError,
783
- X as FORM_DIRTY_DEFAULT_MESSAGE,
784
- J as HTTP_DEFAULT_TIMEOUT,
785
- H as INDEXEDDB_DEFAULT_DOCUMENTS_STORE,
786
- G as INDEXEDDB_DEFAULT_METADATA_STORE,
787
- k as OPENAI_API_BASE_URL,
788
- F as OPENAI_DEFAULT_EMBEDDING_DIMENSIONS,
789
- N as OPENAI_DEFAULT_EMBEDDING_MODEL,
790
- C as OPFS_DEFAULT_CHUNK_SIZE,
791
- T as OPFS_DOCUMENTS_PREFIX,
792
- D as OPFS_METADATA_FILE,
793
- z as RETRIEVAL_DEFAULT_LIMIT,
794
- Y as RETRIEVAL_MAX_LIMIT,
795
- v as VECTORSTORE_METADATA_KEY,
796
- B as VOYAGE_API_BASE_URL,
797
- U as VOYAGE_DEFAULT_EMBEDDING_DIMENSIONS,
798
- L as VOYAGE_DEFAULT_EMBEDDING_MODEL,
799
- se as chain,
800
- P as computeContentHash,
801
- ce as createAnthropicEmbeddingAdapter,
802
- le as createAnthropicToolFormatAdapter,
803
- ue as createBatchedEmbeddingAdapter,
804
- de as createEmbeddingCache,
805
- Ee as createFormDirtyGuard,
806
- he as createHTTPVectorStorePersistence,
807
- me as createIndexedDBVectorStorePersistence,
808
- ye as createOPFSVectorStorePersistence,
809
- ie as createOpenAIEmbeddingAdapter,
810
- fe as createOpenAIToolFormatAdapter,
811
- Ae as createRetrievalTool,
812
- ge as createSessionPersistence,
813
- pe as createToolCallBridge,
814
- S as deserializeStoredDocument,
223
+ k as BRIDGE_DEFAULT_TIMEOUT,
224
+ A as CoreError,
225
+ x as EcosystemError,
226
+ L as FORM_DIRTY_DEFAULT_MESSAGE,
227
+ M as RETRIEVAL_DEFAULT_LIMIT,
228
+ R as RETRIEVAL_MAX_LIMIT,
229
+ $ as chain,
230
+ j as computeContentHash,
231
+ V as createFormDirtyGuard,
232
+ N as createRetrievalTool,
233
+ Y as createSessionPersistence,
234
+ H as createToolCallBridge,
815
235
  I as err,
816
- x as estimateEmbeddingBytes,
817
- Q as isCoreError,
818
- Z as isEcosystemError,
819
- te as isErr,
820
- ee as isOk,
821
- oe as map,
822
- ae as mapErr,
823
- M as ok,
824
- R as serializeStoredDocument,
825
- ne as unwrap,
826
- re as unwrapOrThrow
236
+ P as isCoreError,
237
+ F as isEcosystemError,
238
+ q as isErr,
239
+ b as isOk,
240
+ B as map,
241
+ G as mapErr,
242
+ S as ok,
243
+ O as unwrap,
244
+ U as unwrapOrThrow
827
245
  };
828
246
  //# sourceMappingURL=index.js.map