zen-code 4.6.2 → 4.7.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,35 +1,60 @@
1
- import { Kysely as T, sql as r } from "kysely";
2
- import { B as f, T as l, c as y, m } from "./app-Cynj004J.mjs";
3
- import "./graphBuilder-mpLrx9tk.mjs";
4
- const g = ["source", "step", "parents"];
5
- const E = g;
6
- class w extends f {
1
+ import { Kysely as m, sql as d } from "kysely";
2
+ import { B as b, T as l, c as E, m as g } from "./app-CcnStH9S.mjs";
3
+ import "./graphBuilder-skVEY6RJ.mjs";
4
+ const u = {
5
+ maxRetries: 3,
6
+ baseDelayMs: 100,
7
+ isRetryableError: (w) => {
8
+ const e = w?.message?.toLowerCase() || "";
9
+ return e.includes("sqlite_busy") || e.includes("database is locked") || e.includes("database disk image is malformed") || e === "sqlite_busy" || e === "database is locked";
10
+ }
11
+ };
12
+ async function T(w, e) {
13
+ let i = null;
14
+ for (let n = 0; n < u.maxRetries; n++)
15
+ try {
16
+ return await w();
17
+ } catch (a) {
18
+ if (i = a, !u.isRetryableError(a))
19
+ throw a;
20
+ if (n < u.maxRetries - 1) {
21
+ const c = u.baseDelayMs * Math.pow(2, n);
22
+ console.warn(
23
+ `SQLite lock detected${e ? ` (${e})` : ""}, retrying in ${c}ms (attempt ${n + 1}/${u.maxRetries})`
24
+ ), await new Promise((t) => setTimeout(t, c));
25
+ }
26
+ }
27
+ throw i;
28
+ }
29
+ const A = ["source", "step", "parents"];
30
+ const N = A;
31
+ class f extends b {
7
32
  db;
8
33
  isSetup;
9
- constructor(e, n) {
10
- super(n), this.db = new T({
34
+ constructor(e, i) {
35
+ super(i), this.db = new m({
11
36
  dialect: e
12
37
  }), this.isSetup = !1;
13
38
  }
14
39
  static async fromConnStringAsync(e) {
15
- let n;
40
+ let i;
16
41
  if (globalThis.Bun) {
17
42
  console.log("LG | Using BunWorkerDialect " + e);
18
- const { BunWorkerDialect: o } = await import("kysely-bun-worker");
19
- n = new w(new o({ url: e }));
43
+ const { BunWorkerDialect: n } = await import("kysely-bun-worker");
44
+ i = new f(new n({ url: e }));
20
45
  } else {
21
46
  console.log("LG | Using NodeWasmDialect");
22
- const { default: o } = await import("node-sqlite3-wasm"), { NodeWasmDialect: s } = await import("kysely-wasm");
47
+ const { default: n } = await import("node-sqlite3-wasm"), { NodeWasmDialect: a } = await import("kysely-wasm");
23
48
  console.log(e);
24
- const a = new s({
25
- database: new o.Database(e)
49
+ const c = new a({
50
+ database: new n.Database(e)
26
51
  });
27
- n = new w(a);
52
+ i = new f(c);
28
53
  }
29
- return await n.setup(), n;
54
+ return await i.setup(), i;
30
55
  }
31
56
  async setup() {
32
- this.isSetup || (await r`PRAGMA journal_mode = WAL`.execute(this.db), await r`
57
+ this.isSetup || (await d`PRAGMA busy_timeout = 5000`.execute(this.db), await d`PRAGMA journal_mode = WAL`.execute(this.db), await d`PRAGMA synchronous = NORMAL`.execute(this.db), await d`PRAGMA wal_autocheckpoint = 1000`.execute(this.db), await d`
33
58
  CREATE TABLE IF NOT EXISTS checkpoints (
34
59
  thread_id TEXT NOT NULL,
35
60
  checkpoint_ns TEXT NOT NULL DEFAULT '',
@@ -39,7 +64,7 @@ CREATE TABLE IF NOT EXISTS checkpoints (
39
64
  checkpoint BLOB,
40
65
  metadata BLOB,
41
66
  PRIMARY KEY (thread_id, checkpoint_ns, checkpoint_id)
42
- )`.execute(this.db), await r`
67
+ )`.execute(this.db), await d`
43
68
  CREATE TABLE IF NOT EXISTS writes (
44
69
  thread_id TEXT NOT NULL,
45
70
  checkpoint_ns TEXT NOT NULL DEFAULT '',
@@ -54,8 +79,8 @@ CREATE TABLE IF NOT EXISTS writes (
54
79
  }
55
80
  async getTuple(e) {
56
81
  await this.setup();
57
- const { thread_id: n, checkpoint_ns: o = "", checkpoint_id: s } = e.configurable ?? {};
58
- let a = this.db.selectFrom("checkpoints").select([
82
+ const { thread_id: i, checkpoint_ns: n = "", checkpoint_id: a } = e.configurable ?? {};
83
+ let c = this.db.selectFrom("checkpoints").select([
59
84
  "thread_id",
60
85
  "checkpoint_ns",
61
86
  "checkpoint_id",
@@ -63,7 +88,7 @@ CREATE TABLE IF NOT EXISTS writes (
63
88
  "type",
64
89
  "checkpoint",
65
90
  "metadata",
66
- r`(
91
+ d`(
67
92
  SELECT json_group_array(
68
93
  json_object(
69
94
  'task_id', pw.task_id,
@@ -77,7 +102,7 @@ CREATE TABLE IF NOT EXISTS writes (
77
102
  AND pw.checkpoint_ns = checkpoints.checkpoint_ns
78
103
  AND pw.checkpoint_id = checkpoints.checkpoint_id
79
104
  )`.as("pending_writes"),
80
- r`(
105
+ d`(
81
106
  SELECT json_group_array(
82
107
  json_object(
83
108
  'type', ps.type,
@@ -91,20 +116,20 @@ CREATE TABLE IF NOT EXISTS writes (
91
116
  AND ps.channel = ${l}
92
117
  ORDER BY ps.idx
93
118
  )`.as("pending_sends")
94
- ]).where("thread_id", "=", n).where("checkpoint_ns", "=", o);
95
- s ? a = a.where("checkpoint_id", "=", s) : a = a.orderBy("checkpoint_id", "desc").limit(1);
96
- const t = await a.executeTakeFirst();
119
+ ]).where("thread_id", "=", i).where("checkpoint_ns", "=", n);
120
+ a ? c = c.where("checkpoint_id", "=", a) : c = c.orderBy("checkpoint_id", "desc").limit(1);
121
+ const t = await c.executeTakeFirst();
97
122
  if (!t) return;
98
- let d = e;
99
- if (s || (d = {
123
+ let r = e;
124
+ if (a || (r = {
100
125
  configurable: {
101
126
  thread_id: t.thread_id,
102
- checkpoint_ns: o,
127
+ checkpoint_ns: n,
103
128
  checkpoint_id: t.checkpoint_id
104
129
  }
105
- }), d.configurable?.thread_id === void 0 || d.configurable?.checkpoint_id === void 0)
130
+ }), r.configurable?.thread_id === void 0 || r.configurable?.checkpoint_id === void 0)
106
131
  throw new Error("Missing thread_id or checkpoint_id");
107
- const c = await Promise.all(
132
+ const s = await Promise.all(
108
133
  JSON.parse(t.pending_writes).map(async (h) => [
109
134
  h.task_id,
110
135
  h.channel,
@@ -116,7 +141,7 @@ CREATE TABLE IF NOT EXISTS writes (
116
141
  );
117
142
  return p.v < 4 && t.parent_checkpoint_id != null && await this.migratePendingSends(p, t.thread_id, t.parent_checkpoint_id), {
118
143
  checkpoint: p,
119
- config: d,
144
+ config: r,
120
145
  metadata: await this.serde.loadsTyped(
121
146
  t.type ?? "json",
122
147
  new TextDecoder().decode(t.metadata)
@@ -124,18 +149,18 @@ CREATE TABLE IF NOT EXISTS writes (
124
149
  parentConfig: t.parent_checkpoint_id ? {
125
150
  configurable: {
126
151
  thread_id: t.thread_id,
127
- checkpoint_ns: o,
152
+ checkpoint_ns: n,
128
153
  checkpoint_id: t.parent_checkpoint_id
129
154
  }
130
155
  } : void 0,
131
- pendingWrites: c
156
+ pendingWrites: s
132
157
  };
133
158
  }
134
- async *list(e, n) {
135
- const { limit: o, before: s, filter: a } = n ?? {};
159
+ async *list(e, i) {
160
+ const { limit: n, before: a, filter: c } = i ?? {};
136
161
  await this.setup();
137
- const t = e.configurable?.thread_id, d = e.configurable?.checkpoint_ns;
138
- let c = this.db.selectFrom("checkpoints").select([
162
+ const t = e.configurable?.thread_id, r = e.configurable?.checkpoint_ns;
163
+ let s = this.db.selectFrom("checkpoints").select([
139
164
  "thread_id",
140
165
  "checkpoint_ns",
141
166
  "checkpoint_id",
@@ -143,7 +168,7 @@ CREATE TABLE IF NOT EXISTS writes (
143
168
  "type",
144
169
  "checkpoint",
145
170
  "metadata",
146
- r`(
171
+ d`(
147
172
  SELECT json_group_array(
148
173
  json_object(
149
174
  'task_id', pw.task_id,
@@ -157,7 +182,7 @@ CREATE TABLE IF NOT EXISTS writes (
157
182
  AND pw.checkpoint_ns = checkpoints.checkpoint_ns
158
183
  AND pw.checkpoint_id = checkpoints.checkpoint_id
159
184
  )`.as("pending_writes"),
160
- r`(
185
+ d`(
161
186
  SELECT json_group_array(
162
187
  json_object(
163
188
  'type', ps.type,
@@ -172,147 +197,159 @@ CREATE TABLE IF NOT EXISTS writes (
172
197
  ORDER BY ps.idx
173
198
  )`.as("pending_sends")
174
199
  ]);
175
- t && (c = c.where("thread_id", "=", t)), d != null && (c = c.where("checkpoint_ns", "=", d)), s?.configurable?.checkpoint_id !== void 0 && (c = c.where("checkpoint_id", "<", s.configurable.checkpoint_id));
200
+ t && (s = s.where("thread_id", "=", t)), r != null && (s = s.where("checkpoint_ns", "=", r)), a?.configurable?.checkpoint_id !== void 0 && (s = s.where("checkpoint_id", "<", a.configurable.checkpoint_id));
176
201
  const p = Object.fromEntries(
177
- Object.entries(a ?? {}).filter(
178
- ([i, _]) => _ !== void 0 && E.includes(i)
202
+ Object.entries(c ?? {}).filter(
203
+ ([o, _]) => _ !== void 0 && N.includes(o)
179
204
  )
180
205
  );
181
- for (const [i, _] of Object.entries(p))
182
- c = c.where(
183
- r`json_extract(CAST(metadata AS TEXT), ${r.lit("$." + i)})`,
206
+ for (const [o, _] of Object.entries(p))
207
+ s = s.where(
208
+ d`json_extract(CAST(metadata AS TEXT), ${d.lit("$." + o)})`,
184
209
  "=",
185
- r.lit(JSON.stringify(_))
210
+ d.lit(JSON.stringify(_))
186
211
  );
187
- c = c.orderBy("checkpoint_id", "desc"), o && (c = c.limit(parseInt(o, 10)));
188
- const h = await c.execute();
189
- for (const i of h) {
212
+ s = s.orderBy("checkpoint_id", "desc"), n && (s = s.limit(parseInt(n, 10)));
213
+ const h = await s.execute();
214
+ for (const o of h) {
190
215
  const _ = await Promise.all(
191
- JSON.parse(i.pending_writes).map(async (k) => [
216
+ JSON.parse(o.pending_writes).map(async (k) => [
192
217
  k.task_id,
193
218
  k.channel,
194
219
  await this.serde.loadsTyped(k.type ?? "json", k.value ?? "")
195
220
  ])
196
- ), u = await this.serde.loadsTyped(
197
- i.type ?? "json",
198
- new TextDecoder().decode(i.checkpoint)
221
+ ), y = await this.serde.loadsTyped(
222
+ o.type ?? "json",
223
+ new TextDecoder().decode(o.checkpoint)
199
224
  );
200
- u.v < 4 && i.parent_checkpoint_id != null && await this.migratePendingSends(u, i.thread_id, i.parent_checkpoint_id), yield {
225
+ y.v < 4 && o.parent_checkpoint_id != null && await this.migratePendingSends(y, o.thread_id, o.parent_checkpoint_id), yield {
201
226
  config: {
202
227
  configurable: {
203
- thread_id: i.thread_id,
204
- checkpoint_ns: i.checkpoint_ns,
205
- checkpoint_id: i.checkpoint_id
228
+ thread_id: o.thread_id,
229
+ checkpoint_ns: o.checkpoint_ns,
230
+ checkpoint_id: o.checkpoint_id
206
231
  }
207
232
  },
208
- checkpoint: u,
233
+ checkpoint: y,
209
234
  metadata: await this.serde.loadsTyped(
210
- i.type ?? "json",
211
- new TextDecoder().decode(i.metadata)
235
+ o.type ?? "json",
236
+ new TextDecoder().decode(o.metadata)
212
237
  ),
213
- parentConfig: i.parent_checkpoint_id ? {
238
+ parentConfig: o.parent_checkpoint_id ? {
214
239
  configurable: {
215
- thread_id: i.thread_id,
216
- checkpoint_ns: i.checkpoint_ns,
217
- checkpoint_id: i.parent_checkpoint_id
240
+ thread_id: o.thread_id,
241
+ checkpoint_ns: o.checkpoint_ns,
242
+ checkpoint_id: o.parent_checkpoint_id
218
243
  }
219
244
  } : void 0,
220
245
  pendingWrites: _
221
246
  };
222
247
  }
223
248
  }
224
- async put(e, n, o) {
249
+ async put(e, i, n) {
225
250
  if (await this.setup(), !e.configurable)
226
251
  throw new Error("Empty configuration supplied.");
227
- const s = e.configurable?.thread_id, a = e.configurable?.checkpoint_ns ?? "", t = e.configurable?.checkpoint_id;
228
- if (!s)
252
+ const a = e.configurable?.thread_id, c = e.configurable?.checkpoint_ns ?? "", t = e.configurable?.checkpoint_id;
253
+ if (!a)
229
254
  throw new Error('Missing "thread_id" field in passed "config.configurable".');
230
- const d = y(n), [[c, p], [h, i]] = await Promise.all([
231
- this.serde.dumpsTyped(d),
232
- this.serde.dumpsTyped(o)
255
+ const r = E(i), [[s, p], [h, o]] = await Promise.all([
256
+ this.serde.dumpsTyped(r),
257
+ this.serde.dumpsTyped(n)
233
258
  ]);
234
- if (c !== h)
259
+ if (s !== h)
235
260
  throw new Error("Failed to serialized checkpoint and metadata to the same type.");
236
- return await this.db.insertInto("checkpoints").values({
237
- thread_id: s,
238
- checkpoint_ns: a,
239
- checkpoint_id: n.id,
240
- parent_checkpoint_id: t ?? null,
241
- type: c,
242
- checkpoint: new Uint8Array(Buffer.from(p)),
243
- metadata: new Uint8Array(Buffer.from(i))
244
- }).onConflict(
245
- (_) => _.columns(["thread_id", "checkpoint_ns", "checkpoint_id"]).doUpdateSet({
246
- parent_checkpoint_id: t ?? null,
247
- type: c,
248
- checkpoint: new Uint8Array(Buffer.from(p)),
249
- metadata: new Uint8Array(Buffer.from(i))
250
- })
251
- ).execute(), {
261
+ return await T(
262
+ async () => {
263
+ await this.db.insertInto("checkpoints").values({
264
+ thread_id: a,
265
+ checkpoint_ns: c,
266
+ checkpoint_id: i.id,
267
+ parent_checkpoint_id: t ?? null,
268
+ type: s,
269
+ checkpoint: new Uint8Array(Buffer.from(p)),
270
+ metadata: new Uint8Array(Buffer.from(o))
271
+ }).onConflict(
272
+ (_) => _.columns(["thread_id", "checkpoint_ns", "checkpoint_id"]).doUpdateSet({
273
+ parent_checkpoint_id: t ?? null,
274
+ type: s,
275
+ checkpoint: new Uint8Array(Buffer.from(p)),
276
+ metadata: new Uint8Array(Buffer.from(o))
277
+ })
278
+ ).execute();
279
+ },
280
+ `put(${a}/${i.id})`
281
+ ), {
252
282
  configurable: {
253
- thread_id: s,
254
- checkpoint_ns: a,
255
- checkpoint_id: n.id
283
+ thread_id: a,
284
+ checkpoint_ns: c,
285
+ checkpoint_id: i.id
256
286
  }
257
287
  };
258
288
  }
259
- async putWrites(e, n, o) {
289
+ async putWrites(e, i, n) {
260
290
  if (await this.setup(), !e.configurable)
261
291
  throw new Error("Empty configuration supplied.");
262
292
  if (!e.configurable?.thread_id)
263
293
  throw new Error("Missing thread_id field in config.configurable.");
264
294
  if (!e.configurable?.checkpoint_id)
265
295
  throw new Error("Missing checkpoint_id field in config.configurable.");
266
- const s = await Promise.all(
267
- n.map(async (a, t) => {
268
- const [d, c] = await this.serde.dumpsTyped(a[1]);
296
+ const a = await Promise.all(
297
+ i.map(async (r, s) => {
298
+ const [p, h] = await this.serde.dumpsTyped(r[1]);
269
299
  return {
270
300
  thread_id: e.configurable.thread_id,
271
301
  checkpoint_ns: e.configurable.checkpoint_ns ?? "",
272
302
  checkpoint_id: e.configurable.checkpoint_id,
273
- task_id: o,
274
- idx: t,
275
- channel: a[0],
276
- type: d,
277
- value: new Uint8Array(Buffer.from(c))
303
+ task_id: n,
304
+ idx: s,
305
+ channel: r[0],
306
+ type: p,
307
+ value: new Uint8Array(Buffer.from(h))
278
308
  };
279
309
  })
280
310
  );
281
- s.length > 0 && await this.db.transaction().execute(async (a) => {
282
- for (const t of s)
283
- await a.insertInto("writes").values(t).onConflict(
284
- (d) => d.columns(["thread_id", "checkpoint_ns", "checkpoint_id", "task_id", "idx"]).doUpdateSet({
285
- channel: t.channel,
286
- type: t.type,
287
- value: t.value
288
- })
289
- ).execute();
290
- });
311
+ if (a.length === 0) return;
312
+ const c = e.configurable.thread_id, t = e.configurable.checkpoint_id;
313
+ await T(
314
+ async () => {
315
+ await this.db.transaction().execute(async (r) => {
316
+ await r.deleteFrom("writes").where("thread_id", "=", c).where("checkpoint_ns", "=", a[0].checkpoint_ns).where("checkpoint_id", "=", t).where("task_id", "=", n).execute();
317
+ for (const s of a)
318
+ await r.insertInto("writes").values(s).execute();
319
+ });
320
+ },
321
+ `putWrites(${c}/${t}/${n})`
322
+ );
291
323
  }
292
324
  async deleteThread(e) {
293
- await this.db.transaction().execute(async (n) => {
294
- await n.deleteFrom("checkpoints").where("thread_id", "=", e).execute(), await n.deleteFrom("writes").where("thread_id", "=", e).execute();
295
- });
325
+ await T(
326
+ async () => {
327
+ await this.db.transaction().execute(async (i) => {
328
+ await i.deleteFrom("checkpoints").where("thread_id", "=", e).execute(), await i.deleteFrom("writes").where("thread_id", "=", e).execute();
329
+ });
330
+ },
331
+ `deleteThread(${e})`
332
+ );
296
333
  }
297
- async migratePendingSends(e, n, o) {
298
- const s = await this.db.selectFrom("writes as ps").select([
334
+ async migratePendingSends(e, i, n) {
335
+ const a = await this.db.selectFrom("writes as ps").select([
299
336
  "ps.checkpoint_id",
300
- r`json_group_array(
337
+ d`json_group_array(
301
338
  json_object(
302
339
  'type', ps.type,
303
340
  'value', CAST(ps.value AS TEXT)
304
341
  )
305
342
  )`.as("pending_sends")
306
- ]).where("ps.thread_id", "=", n).where("ps.checkpoint_id", "=", o).where("ps.channel", "=", l).orderBy("ps.idx").executeTakeFirst();
307
- if (!s) return;
308
- const a = e;
309
- a.channel_values ??= {}, a.channel_values[l] = await Promise.all(
310
- JSON.parse(s.pending_sends).map(
311
- ({ type: t, value: d }) => this.serde.loadsTyped(t, d)
343
+ ]).where("ps.thread_id", "=", i).where("ps.checkpoint_id", "=", n).where("ps.channel", "=", l).orderBy("ps.idx").executeTakeFirst();
344
+ if (!a) return;
345
+ const c = e;
346
+ c.channel_values ??= {}, c.channel_values[l] = await Promise.all(
347
+ JSON.parse(a.pending_sends).map(
348
+ ({ type: t, value: r }) => this.serde.loadsTyped(t, r)
312
349
  )
313
- ), a.channel_versions[l] = Object.keys(e.channel_versions).length > 0 ? m(...Object.values(e.channel_versions)) : this.getNextVersion(void 0);
350
+ ), c.channel_versions[l] = Object.keys(e.channel_versions).length > 0 ? g(...Object.values(e.channel_versions)) : this.getNextVersion(void 0);
314
351
  }
315
352
  }
316
353
  export {
317
- w as SqliteSaver
354
+ f as SqliteSaver
318
355
  };
@@ -1,5 +1,5 @@
1
- import { T as l, B as N, W as k, m as y } from "./app-Cynj004J.mjs";
2
- import "./graphBuilder-mpLrx9tk.mjs";
1
+ import { T as l, B as N, W as k, m as y } from "./app-CcnStH9S.mjs";
2
+ import "./graphBuilder-skVEY6RJ.mjs";
3
3
  import w from "pg";
4
4
  const S = (h) => [
5
5
  "checkpoints",