zen-code 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,462 +0,0 @@
1
- import { B as C, u as v, c as x, T as $, m as I } from "./app-B06vZiE3.mjs";
2
- import { createClient as O, createCluster as N } from "redis";
3
- const W = [
4
- {
5
- index: "checkpoints",
6
- prefix: "checkpoint:",
7
- schema: {
8
- "$.thread_id": {
9
- type: "TAG",
10
- AS: "thread_id"
11
- },
12
- "$.checkpoint_ns": {
13
- type: "TAG",
14
- AS: "checkpoint_ns"
15
- },
16
- "$.checkpoint_id": {
17
- type: "TAG",
18
- AS: "checkpoint_id"
19
- },
20
- "$.parent_checkpoint_id": {
21
- type: "TAG",
22
- AS: "parent_checkpoint_id"
23
- },
24
- "$.checkpoint_ts": {
25
- type: "NUMERIC",
26
- AS: "checkpoint_ts"
27
- },
28
- "$.has_writes": {
29
- type: "TAG",
30
- AS: "has_writes"
31
- },
32
- "$.source": {
33
- type: "TAG",
34
- AS: "source"
35
- },
36
- "$.step": {
37
- type: "NUMERIC",
38
- AS: "step"
39
- }
40
- }
41
- },
42
- {
43
- index: "checkpoint_blobs",
44
- prefix: "checkpoint_blob:",
45
- schema: {
46
- "$.thread_id": {
47
- type: "TAG",
48
- AS: "thread_id"
49
- },
50
- "$.checkpoint_ns": {
51
- type: "TAG",
52
- AS: "checkpoint_ns"
53
- },
54
- "$.checkpoint_id": {
55
- type: "TAG",
56
- AS: "checkpoint_id"
57
- },
58
- "$.channel": {
59
- type: "TAG",
60
- AS: "channel"
61
- },
62
- "$.version": {
63
- type: "TAG",
64
- AS: "version"
65
- },
66
- "$.type": {
67
- type: "TAG",
68
- AS: "type"
69
- }
70
- }
71
- },
72
- {
73
- index: "checkpoint_writes",
74
- prefix: "checkpoint_write:",
75
- schema: {
76
- "$.thread_id": {
77
- type: "TAG",
78
- AS: "thread_id"
79
- },
80
- "$.checkpoint_ns": {
81
- type: "TAG",
82
- AS: "checkpoint_ns"
83
- },
84
- "$.checkpoint_id": {
85
- type: "TAG",
86
- AS: "checkpoint_id"
87
- },
88
- "$.task_id": {
89
- type: "TAG",
90
- AS: "task_id"
91
- },
92
- "$.idx": {
93
- type: "NUMERIC",
94
- AS: "idx"
95
- },
96
- "$.channel": {
97
- type: "TAG",
98
- AS: "channel"
99
- },
100
- "$.type": {
101
- type: "TAG",
102
- AS: "type"
103
- }
104
- }
105
- }
106
- ];
107
- var j = class T extends C {
108
- client;
109
- ttlConfig;
110
- constructor(e, t) {
111
- super(), this.client = e, this.ttlConfig = t;
112
- }
113
- static async fromUrl(e, t) {
114
- const s = O({ url: e });
115
- await s.connect();
116
- const i = new T(s, t);
117
- return await i.ensureIndexes(), i;
118
- }
119
- static async fromCluster(e, t) {
120
- const s = N({ rootNodes: e });
121
- await s.connect();
122
- const i = new T(s, t);
123
- return await i.ensureIndexes(), i;
124
- }
125
- async get(e) {
126
- return (await this.getTuple(e))?.checkpoint;
127
- }
128
- async getTuple(e) {
129
- const t = e.configurable?.thread_id, s = e.configurable?.checkpoint_ns ?? "", i = e.configurable?.checkpoint_id;
130
- if (!t) return;
131
- let c, o;
132
- if (i)
133
- c = `checkpoint:${t}:${s}:${i}`, o = await this.client.json.get(c);
134
- else {
135
- const r = `checkpoint:${t}:${s}:*`, p = await this.client.keys(r);
136
- if (p.length === 0) return;
137
- p.sort(), c = p[p.length - 1], o = await this.client.json.get(c);
138
- }
139
- if (!o) return;
140
- this.ttlConfig?.refreshOnRead && this.ttlConfig?.defaultTTL && await this.applyTTL(c);
141
- const { checkpoint: f, pendingWrites: n } = await this.loadCheckpointWithWrites(o);
142
- return await this.createCheckpointTuple(o, f, n);
143
- }
144
- async put(e, t, s, i) {
145
- await this.ensureIndexes();
146
- const c = e.configurable?.thread_id, o = e.configurable?.checkpoint_ns ?? "", f = e.configurable?.checkpoint_id;
147
- if (!c) throw new Error("thread_id is required");
148
- const n = t.id || v(0), r = `checkpoint:${c}:${o}:${n}`, p = x(t);
149
- if (p.channel_values && i !== void 0) if (Object.keys(i).length === 0) p.channel_values = {};
150
- else {
151
- const u = {};
152
- for (const a of Object.keys(i)) a in p.channel_values && (u[a] = p.channel_values[a]);
153
- p.channel_values = u;
154
- }
155
- const l = {
156
- thread_id: c,
157
- checkpoint_ns: o === "" ? "__empty__" : o,
158
- checkpoint_id: n,
159
- parent_checkpoint_id: f || null,
160
- checkpoint: p,
161
- metadata: s,
162
- checkpoint_ts: Date.now(),
163
- has_writes: "false"
164
- };
165
- return this.addSearchableMetadataFields(l, s), await this.client.json.set(r, "$", l), this.ttlConfig?.defaultTTL && await this.applyTTL(r), { configurable: {
166
- thread_id: c,
167
- checkpoint_ns: o,
168
- checkpoint_id: n
169
- } };
170
- }
171
- async *list(e, t) {
172
- if (await this.ensureIndexes(), t?.filter !== void 0) {
173
- const i = Object.values(t.filter).some((n) => n === null), c = [];
174
- if (e?.configurable?.thread_id) {
175
- const n = e.configurable.thread_id.replace(/[-.@]/g, "\\$&");
176
- c.push(`(@thread_id:{${n}})`);
177
- }
178
- if (e?.configurable?.checkpoint_ns !== void 0) {
179
- const n = e.configurable.checkpoint_ns;
180
- if (n === "") c.push("(@checkpoint_ns:{__empty__})");
181
- else {
182
- const r = n.replace(/[-.@]/g, "\\$&");
183
- c.push(`(@checkpoint_ns:{${r}})`);
184
- }
185
- }
186
- if (!t?.before && t?.filter)
187
- for (const [n, r] of Object.entries(t.filter)) r === void 0 || r === null || (typeof r == "string" ? c.push(`(@${n}:{${r}})`) : typeof r == "number" ? c.push(`(@${n}:[${r} ${r}])`) : typeof r == "object" && Object.keys(r).length);
188
- c.length === 0 && c.push("*");
189
- const o = c.join(" "), f = t?.limit ?? 10;
190
- try {
191
- const n = t?.before && !e?.configurable?.thread_id ? 1e3 : t?.before ? f * 10 : f;
192
- let p = (await this.client.ft.search("checkpoints", o, {
193
- LIMIT: {
194
- from: 0,
195
- size: n
196
- },
197
- SORTBY: {
198
- BY: "checkpoint_ts",
199
- DIRECTION: "DESC"
200
- }
201
- })).documents, l = 0;
202
- for (const u of p) {
203
- if (l >= f) break;
204
- if (t?.before?.configurable?.checkpoint_id) {
205
- const k = u.value.checkpoint_id, d = t.before.configurable.checkpoint_id;
206
- if (k >= d) continue;
207
- }
208
- const a = u.value;
209
- let h = !0;
210
- if ((i || t?.before) && t?.filter) {
211
- for (const [k, d] of Object.entries(t.filter)) if (d === null) {
212
- if (a.metadata?.[k] !== null) {
213
- h = !1;
214
- break;
215
- }
216
- } else if (d !== void 0) {
217
- const b = a.metadata?.[k];
218
- if (typeof d == "object" && d !== null) {
219
- if (g(b) !== g(d)) {
220
- h = !1;
221
- break;
222
- }
223
- } else if (b !== d) {
224
- h = !1;
225
- break;
226
- }
227
- }
228
- if (!h) continue;
229
- }
230
- const { checkpoint: y, pendingWrites: _ } = await this.loadCheckpointWithWrites(a);
231
- yield await this.createCheckpointTuple(a, y, _), l++;
232
- }
233
- return;
234
- } catch (n) {
235
- if (!n.message?.includes("no such index")) throw n;
236
- }
237
- if (e?.configurable?.thread_id) {
238
- const n = e.configurable.thread_id, r = e.configurable.checkpoint_ns ?? "", p = `checkpoint:${n}:${r}:*`, l = await this.client.keys(p);
239
- l.sort().reverse();
240
- let u = l;
241
- if (t?.before?.configurable?.checkpoint_id) {
242
- const y = t.before.configurable.thread_id || n, _ = t.before.configurable.checkpoint_ns ?? r, k = `checkpoint:${y}:${_}:${t.before.configurable.checkpoint_id}`, d = l.indexOf(k);
243
- d > 0 ? u = l.slice(d + 1) : d === 0 && (u = []);
244
- }
245
- const a = t?.limit ?? 10, h = u.slice(0, a);
246
- for (const y of h) {
247
- const _ = await this.client.json.get(y);
248
- if (_) {
249
- let k = !0;
250
- for (const [S, m] of Object.entries(t.filter)) {
251
- const A = _.metadata?.[S];
252
- if (m === null) {
253
- if (A !== null) {
254
- k = !1;
255
- break;
256
- }
257
- } else if (A !== m) {
258
- k = !1;
259
- break;
260
- }
261
- }
262
- if (!k) continue;
263
- const { checkpoint: d, pendingWrites: b } = await this.loadCheckpointWithWrites(_);
264
- yield await this.createCheckpointTuple(_, d, b);
265
- }
266
- }
267
- } else {
268
- const n = e?.configurable?.checkpoint_ns !== void 0 ? `checkpoint:*:${e.configurable.checkpoint_ns === "" ? "__empty__" : e.configurable.checkpoint_ns}:*` : "checkpoint:*", r = await this.client.keys(n), p = [];
269
- for (const a of r) {
270
- const h = await this.client.json.get(a);
271
- h && p.push({
272
- key: a,
273
- doc: h
274
- });
275
- }
276
- p.sort((a, h) => h.doc.checkpoint_ts - a.doc.checkpoint_ts);
277
- let l = 0;
278
- const u = t?.limit ?? 10;
279
- for (const { doc: a } of p) {
280
- if (l >= u) break;
281
- if (t?.before?.configurable?.checkpoint_id) {
282
- const k = a.checkpoint_id, d = t.before.configurable.checkpoint_id;
283
- if (k >= d) continue;
284
- }
285
- let h = !0;
286
- if (t?.filter) {
287
- for (const [k, d] of Object.entries(t.filter)) if (d === null) {
288
- if (a.metadata?.[k] !== null) {
289
- h = !1;
290
- break;
291
- }
292
- } else if (d !== void 0) {
293
- const b = a.metadata?.[k];
294
- if (typeof d == "object" && d !== null) {
295
- if (g(b) !== g(d)) {
296
- h = !1;
297
- break;
298
- }
299
- } else if (b !== d) {
300
- h = !1;
301
- break;
302
- }
303
- }
304
- if (!h) continue;
305
- }
306
- const { checkpoint: y, pendingWrites: _ } = await this.loadCheckpointWithWrites(a);
307
- yield await this.createCheckpointTuple(a, y, _), l++;
308
- }
309
- }
310
- return;
311
- }
312
- const s = {
313
- ...t,
314
- filter: {}
315
- };
316
- yield* this.list(e, s);
317
- }
318
- async putWrites(e, t, s) {
319
- await this.ensureIndexes();
320
- const i = e.configurable?.thread_id, c = e.configurable?.checkpoint_ns ?? "", o = e.configurable?.checkpoint_id;
321
- if (!i || !o) throw new Error("thread_id and checkpoint_id are required");
322
- const f = [], n = performance.now() * 1e3;
323
- for (let l = 0; l < t.length; l++) {
324
- const [u, a] = t[l], h = `checkpoint_write:${i}:${c}:${o}:${s}:${l}`;
325
- f.push(h);
326
- const y = {
327
- thread_id: i,
328
- checkpoint_ns: c,
329
- checkpoint_id: o,
330
- task_id: s,
331
- idx: l,
332
- channel: u,
333
- type: typeof a == "object" ? "json" : "string",
334
- value: a,
335
- timestamp: n,
336
- global_idx: n + l
337
- };
338
- await this.client.json.set(h, "$", y);
339
- }
340
- if (f.length > 0) {
341
- const l = `write_keys_zset:${i}:${c}:${o}`, u = {};
342
- f.forEach((a, h) => {
343
- u[a] = n + h;
344
- }), await this.client.zAdd(l, Object.entries(u).map(([a, h]) => ({
345
- score: h,
346
- value: a
347
- }))), this.ttlConfig?.defaultTTL && await this.applyTTL(...f, l);
348
- }
349
- const r = `checkpoint:${i}:${c}:${o}`;
350
- if (await this.client.exists(r)) {
351
- const l = await this.client.json.get(r);
352
- l && (l.has_writes = "true", await this.client.json.set(r, "$", l));
353
- }
354
- }
355
- async deleteThread(e) {
356
- const t = `checkpoint:${e}:*`, s = await this.client.keys(t);
357
- s.length > 0 && await this.client.del(s);
358
- const i = `writes:${e}:*`, c = await this.client.keys(i);
359
- c.length > 0 && await this.client.del(c);
360
- }
361
- async end() {
362
- await this.client.quit();
363
- }
364
- async loadPendingWrites(e, t, s) {
365
- const i = `checkpoint_write:${e}:${t}:${s}:*`, c = await this.client.keys(i);
366
- if (c.length === 0) return;
367
- const o = [];
368
- for (const n of c) {
369
- const r = await this.client.json.get(n);
370
- r && o.push(r);
371
- }
372
- o.sort((n, r) => (n.global_idx || 0) - (r.global_idx || 0));
373
- const f = [];
374
- for (const n of o) {
375
- const r = await this.serde.loadsTyped("json", JSON.stringify(n.value));
376
- f.push([
377
- n.task_id,
378
- n.channel,
379
- r
380
- ]);
381
- }
382
- return f;
383
- }
384
- async loadCheckpointWithWrites(e) {
385
- const t = await this.serde.loadsTyped("json", JSON.stringify(e.checkpoint));
386
- if (t.v < 4 && e.parent_checkpoint_id != null) {
387
- const i = e.checkpoint_ns === "__empty__" ? "" : e.checkpoint_ns;
388
- await this.migratePendingSends(t, e.thread_id, i, e.parent_checkpoint_id);
389
- }
390
- let s;
391
- if (e.has_writes === "true") {
392
- const i = e.checkpoint_ns === "__empty__" ? "" : e.checkpoint_ns;
393
- s = await this.loadPendingWrites(e.thread_id, i, e.checkpoint_id);
394
- }
395
- return {
396
- checkpoint: t,
397
- pendingWrites: s
398
- };
399
- }
400
- async migratePendingSends(e, t, s, i) {
401
- const c = await this.loadPendingWrites(t, s, i);
402
- if (!c || c.length === 0) return;
403
- const o = c.filter(([, n]) => n === $);
404
- if (o.length === 0) return;
405
- const f = [];
406
- for (const [, , n] of o) f.push(n);
407
- e.channel_values ??= {}, e.channel_values[$] = f, e.channel_versions[$] = Object.keys(e.channel_versions).length > 0 ? I(...Object.values(e.channel_versions)) : 1;
408
- }
409
- async createCheckpointTuple(e, t, s) {
410
- const i = e.checkpoint_ns === "__empty__" ? "" : e.checkpoint_ns, c = await this.serde.loadsTyped("json", JSON.stringify(e.metadata));
411
- return {
412
- config: { configurable: {
413
- thread_id: e.thread_id,
414
- checkpoint_ns: i,
415
- checkpoint_id: e.checkpoint_id
416
- } },
417
- checkpoint: t,
418
- metadata: c,
419
- parentConfig: e.parent_checkpoint_id ? { configurable: {
420
- thread_id: e.thread_id,
421
- checkpoint_ns: i,
422
- checkpoint_id: e.parent_checkpoint_id
423
- } } : void 0,
424
- pendingWrites: s
425
- };
426
- }
427
- addSearchableMetadataFields(e, t) {
428
- t && ("source" in t && (e.source = t.source), "step" in t && (e.step = t.step), "writes" in t && (e.writes = typeof t.writes == "object" ? JSON.stringify(t.writes) : t.writes), "score" in t && (e.score = t.score));
429
- }
430
- async applyTTL(...e) {
431
- if (!this.ttlConfig?.defaultTTL) return;
432
- const t = Math.floor(this.ttlConfig.defaultTTL * 60), s = await Promise.allSettled(e.map((i) => this.client.expire(i, t)));
433
- for (let i = 0; i < s.length; i++) s[i].status === "rejected" && console.warn(`Failed to set TTL for key ${e[i]}:`, s[i].reason);
434
- }
435
- async ensureIndexes() {
436
- for (const e of W) try {
437
- await this.client.ft.create(e.index, e.schema, {
438
- ON: "JSON",
439
- PREFIX: e.prefix
440
- });
441
- } catch (t) {
442
- t.message?.includes("Index already exists") || console.error(`Failed to create index ${e.index}:`, t.message);
443
- }
444
- }
445
- };
446
- function g(w) {
447
- if (w === null || typeof w != "object") return JSON.stringify(w);
448
- if (Array.isArray(w)) return JSON.stringify(w.map((s) => g(s)));
449
- const e = {}, t = Object.keys(w).sort();
450
- for (const s of t) e[s] = w[s];
451
- return JSON.stringify(e, (s, i) => {
452
- if (i !== null && typeof i == "object" && !Array.isArray(i)) {
453
- const c = {}, o = Object.keys(i).sort();
454
- for (const f of o) c[f] = i[f];
455
- return c;
456
- }
457
- return i;
458
- });
459
- }
460
- export {
461
- j as RedisSaver
462
- };