zen-code 4.6.1 → 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.
@@ -0,0 +1,463 @@
1
+ import { B as C, u as v, c as x, T as $, m as I } from "./app-CcnStH9S.mjs";
2
+ import "./graphBuilder-skVEY6RJ.mjs";
3
+ import { createClient as O, createCluster as N } from "redis";
4
+ const W = [
5
+ {
6
+ index: "checkpoints",
7
+ prefix: "checkpoint:",
8
+ schema: {
9
+ "$.thread_id": {
10
+ type: "TAG",
11
+ AS: "thread_id"
12
+ },
13
+ "$.checkpoint_ns": {
14
+ type: "TAG",
15
+ AS: "checkpoint_ns"
16
+ },
17
+ "$.checkpoint_id": {
18
+ type: "TAG",
19
+ AS: "checkpoint_id"
20
+ },
21
+ "$.parent_checkpoint_id": {
22
+ type: "TAG",
23
+ AS: "parent_checkpoint_id"
24
+ },
25
+ "$.checkpoint_ts": {
26
+ type: "NUMERIC",
27
+ AS: "checkpoint_ts"
28
+ },
29
+ "$.has_writes": {
30
+ type: "TAG",
31
+ AS: "has_writes"
32
+ },
33
+ "$.source": {
34
+ type: "TAG",
35
+ AS: "source"
36
+ },
37
+ "$.step": {
38
+ type: "NUMERIC",
39
+ AS: "step"
40
+ }
41
+ }
42
+ },
43
+ {
44
+ index: "checkpoint_blobs",
45
+ prefix: "checkpoint_blob:",
46
+ schema: {
47
+ "$.thread_id": {
48
+ type: "TAG",
49
+ AS: "thread_id"
50
+ },
51
+ "$.checkpoint_ns": {
52
+ type: "TAG",
53
+ AS: "checkpoint_ns"
54
+ },
55
+ "$.checkpoint_id": {
56
+ type: "TAG",
57
+ AS: "checkpoint_id"
58
+ },
59
+ "$.channel": {
60
+ type: "TAG",
61
+ AS: "channel"
62
+ },
63
+ "$.version": {
64
+ type: "TAG",
65
+ AS: "version"
66
+ },
67
+ "$.type": {
68
+ type: "TAG",
69
+ AS: "type"
70
+ }
71
+ }
72
+ },
73
+ {
74
+ index: "checkpoint_writes",
75
+ prefix: "checkpoint_write:",
76
+ schema: {
77
+ "$.thread_id": {
78
+ type: "TAG",
79
+ AS: "thread_id"
80
+ },
81
+ "$.checkpoint_ns": {
82
+ type: "TAG",
83
+ AS: "checkpoint_ns"
84
+ },
85
+ "$.checkpoint_id": {
86
+ type: "TAG",
87
+ AS: "checkpoint_id"
88
+ },
89
+ "$.task_id": {
90
+ type: "TAG",
91
+ AS: "task_id"
92
+ },
93
+ "$.idx": {
94
+ type: "NUMERIC",
95
+ AS: "idx"
96
+ },
97
+ "$.channel": {
98
+ type: "TAG",
99
+ AS: "channel"
100
+ },
101
+ "$.type": {
102
+ type: "TAG",
103
+ AS: "type"
104
+ }
105
+ }
106
+ }
107
+ ];
108
+ var E = class T extends C {
109
+ client;
110
+ ttlConfig;
111
+ constructor(e, t) {
112
+ super(), this.client = e, this.ttlConfig = t;
113
+ }
114
+ static async fromUrl(e, t) {
115
+ const s = O({ url: e });
116
+ await s.connect();
117
+ const i = new T(s, t);
118
+ return await i.ensureIndexes(), i;
119
+ }
120
+ static async fromCluster(e, t) {
121
+ const s = N({ rootNodes: e });
122
+ await s.connect();
123
+ const i = new T(s, t);
124
+ return await i.ensureIndexes(), i;
125
+ }
126
+ async get(e) {
127
+ return (await this.getTuple(e))?.checkpoint;
128
+ }
129
+ async getTuple(e) {
130
+ const t = e.configurable?.thread_id, s = e.configurable?.checkpoint_ns ?? "", i = e.configurable?.checkpoint_id;
131
+ if (!t) return;
132
+ let c, o;
133
+ if (i)
134
+ c = `checkpoint:${t}:${s}:${i}`, o = await this.client.json.get(c);
135
+ else {
136
+ const r = `checkpoint:${t}:${s}:*`, p = await this.client.keys(r);
137
+ if (p.length === 0) return;
138
+ p.sort(), c = p[p.length - 1], o = await this.client.json.get(c);
139
+ }
140
+ if (!o) return;
141
+ this.ttlConfig?.refreshOnRead && this.ttlConfig?.defaultTTL && await this.applyTTL(c);
142
+ const { checkpoint: f, pendingWrites: n } = await this.loadCheckpointWithWrites(o);
143
+ return await this.createCheckpointTuple(o, f, n);
144
+ }
145
+ async put(e, t, s, i) {
146
+ await this.ensureIndexes();
147
+ const c = e.configurable?.thread_id, o = e.configurable?.checkpoint_ns ?? "", f = e.configurable?.checkpoint_id;
148
+ if (!c) throw new Error("thread_id is required");
149
+ const n = t.id || v(0), r = `checkpoint:${c}:${o}:${n}`, p = x(t);
150
+ if (p.channel_values && i !== void 0) if (Object.keys(i).length === 0) p.channel_values = {};
151
+ else {
152
+ const u = {};
153
+ for (const a of Object.keys(i)) a in p.channel_values && (u[a] = p.channel_values[a]);
154
+ p.channel_values = u;
155
+ }
156
+ const l = {
157
+ thread_id: c,
158
+ checkpoint_ns: o === "" ? "__empty__" : o,
159
+ checkpoint_id: n,
160
+ parent_checkpoint_id: f || null,
161
+ checkpoint: p,
162
+ metadata: s,
163
+ checkpoint_ts: Date.now(),
164
+ has_writes: "false"
165
+ };
166
+ return this.addSearchableMetadataFields(l, s), await this.client.json.set(r, "$", l), this.ttlConfig?.defaultTTL && await this.applyTTL(r), { configurable: {
167
+ thread_id: c,
168
+ checkpoint_ns: o,
169
+ checkpoint_id: n
170
+ } };
171
+ }
172
+ async *list(e, t) {
173
+ if (await this.ensureIndexes(), t?.filter !== void 0) {
174
+ const i = Object.values(t.filter).some((n) => n === null), c = [];
175
+ if (e?.configurable?.thread_id) {
176
+ const n = e.configurable.thread_id.replace(/[-.@]/g, "\\$&");
177
+ c.push(`(@thread_id:{${n}})`);
178
+ }
179
+ if (e?.configurable?.checkpoint_ns !== void 0) {
180
+ const n = e.configurable.checkpoint_ns;
181
+ if (n === "") c.push("(@checkpoint_ns:{__empty__})");
182
+ else {
183
+ const r = n.replace(/[-.@]/g, "\\$&");
184
+ c.push(`(@checkpoint_ns:{${r}})`);
185
+ }
186
+ }
187
+ if (!t?.before && t?.filter)
188
+ 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);
189
+ c.length === 0 && c.push("*");
190
+ const o = c.join(" "), f = t?.limit ?? 10;
191
+ try {
192
+ const n = t?.before && !e?.configurable?.thread_id ? 1e3 : t?.before ? f * 10 : f;
193
+ let p = (await this.client.ft.search("checkpoints", o, {
194
+ LIMIT: {
195
+ from: 0,
196
+ size: n
197
+ },
198
+ SORTBY: {
199
+ BY: "checkpoint_ts",
200
+ DIRECTION: "DESC"
201
+ }
202
+ })).documents, l = 0;
203
+ for (const u of p) {
204
+ if (l >= f) break;
205
+ if (t?.before?.configurable?.checkpoint_id) {
206
+ const k = u.value.checkpoint_id, d = t.before.configurable.checkpoint_id;
207
+ if (k >= d) continue;
208
+ }
209
+ const a = u.value;
210
+ let h = !0;
211
+ if ((i || t?.before) && t?.filter) {
212
+ for (const [k, d] of Object.entries(t.filter)) if (d === null) {
213
+ if (a.metadata?.[k] !== null) {
214
+ h = !1;
215
+ break;
216
+ }
217
+ } else if (d !== void 0) {
218
+ const b = a.metadata?.[k];
219
+ if (typeof d == "object" && d !== null) {
220
+ if (g(b) !== g(d)) {
221
+ h = !1;
222
+ break;
223
+ }
224
+ } else if (b !== d) {
225
+ h = !1;
226
+ break;
227
+ }
228
+ }
229
+ if (!h) continue;
230
+ }
231
+ const { checkpoint: y, pendingWrites: _ } = await this.loadCheckpointWithWrites(a);
232
+ yield await this.createCheckpointTuple(a, y, _), l++;
233
+ }
234
+ return;
235
+ } catch (n) {
236
+ if (!n.message?.includes("no such index")) throw n;
237
+ }
238
+ if (e?.configurable?.thread_id) {
239
+ const n = e.configurable.thread_id, r = e.configurable.checkpoint_ns ?? "", p = `checkpoint:${n}:${r}:*`, l = await this.client.keys(p);
240
+ l.sort().reverse();
241
+ let u = l;
242
+ if (t?.before?.configurable?.checkpoint_id) {
243
+ 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);
244
+ d > 0 ? u = l.slice(d + 1) : d === 0 && (u = []);
245
+ }
246
+ const a = t?.limit ?? 10, h = u.slice(0, a);
247
+ for (const y of h) {
248
+ const _ = await this.client.json.get(y);
249
+ if (_) {
250
+ let k = !0;
251
+ for (const [S, m] of Object.entries(t.filter)) {
252
+ const A = _.metadata?.[S];
253
+ if (m === null) {
254
+ if (A !== null) {
255
+ k = !1;
256
+ break;
257
+ }
258
+ } else if (A !== m) {
259
+ k = !1;
260
+ break;
261
+ }
262
+ }
263
+ if (!k) continue;
264
+ const { checkpoint: d, pendingWrites: b } = await this.loadCheckpointWithWrites(_);
265
+ yield await this.createCheckpointTuple(_, d, b);
266
+ }
267
+ }
268
+ } else {
269
+ 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 = [];
270
+ for (const a of r) {
271
+ const h = await this.client.json.get(a);
272
+ h && p.push({
273
+ key: a,
274
+ doc: h
275
+ });
276
+ }
277
+ p.sort((a, h) => h.doc.checkpoint_ts - a.doc.checkpoint_ts);
278
+ let l = 0;
279
+ const u = t?.limit ?? 10;
280
+ for (const { doc: a } of p) {
281
+ if (l >= u) break;
282
+ if (t?.before?.configurable?.checkpoint_id) {
283
+ const k = a.checkpoint_id, d = t.before.configurable.checkpoint_id;
284
+ if (k >= d) continue;
285
+ }
286
+ let h = !0;
287
+ if (t?.filter) {
288
+ for (const [k, d] of Object.entries(t.filter)) if (d === null) {
289
+ if (a.metadata?.[k] !== null) {
290
+ h = !1;
291
+ break;
292
+ }
293
+ } else if (d !== void 0) {
294
+ const b = a.metadata?.[k];
295
+ if (typeof d == "object" && d !== null) {
296
+ if (g(b) !== g(d)) {
297
+ h = !1;
298
+ break;
299
+ }
300
+ } else if (b !== d) {
301
+ h = !1;
302
+ break;
303
+ }
304
+ }
305
+ if (!h) continue;
306
+ }
307
+ const { checkpoint: y, pendingWrites: _ } = await this.loadCheckpointWithWrites(a);
308
+ yield await this.createCheckpointTuple(a, y, _), l++;
309
+ }
310
+ }
311
+ return;
312
+ }
313
+ const s = {
314
+ ...t,
315
+ filter: {}
316
+ };
317
+ yield* this.list(e, s);
318
+ }
319
+ async putWrites(e, t, s) {
320
+ await this.ensureIndexes();
321
+ const i = e.configurable?.thread_id, c = e.configurable?.checkpoint_ns ?? "", o = e.configurable?.checkpoint_id;
322
+ if (!i || !o) throw new Error("thread_id and checkpoint_id are required");
323
+ const f = [], n = performance.now() * 1e3;
324
+ for (let l = 0; l < t.length; l++) {
325
+ const [u, a] = t[l], h = `checkpoint_write:${i}:${c}:${o}:${s}:${l}`;
326
+ f.push(h);
327
+ const y = {
328
+ thread_id: i,
329
+ checkpoint_ns: c,
330
+ checkpoint_id: o,
331
+ task_id: s,
332
+ idx: l,
333
+ channel: u,
334
+ type: typeof a == "object" ? "json" : "string",
335
+ value: a,
336
+ timestamp: n,
337
+ global_idx: n + l
338
+ };
339
+ await this.client.json.set(h, "$", y);
340
+ }
341
+ if (f.length > 0) {
342
+ const l = `write_keys_zset:${i}:${c}:${o}`, u = {};
343
+ f.forEach((a, h) => {
344
+ u[a] = n + h;
345
+ }), await this.client.zAdd(l, Object.entries(u).map(([a, h]) => ({
346
+ score: h,
347
+ value: a
348
+ }))), this.ttlConfig?.defaultTTL && await this.applyTTL(...f, l);
349
+ }
350
+ const r = `checkpoint:${i}:${c}:${o}`;
351
+ if (await this.client.exists(r)) {
352
+ const l = await this.client.json.get(r);
353
+ l && (l.has_writes = "true", await this.client.json.set(r, "$", l));
354
+ }
355
+ }
356
+ async deleteThread(e) {
357
+ const t = `checkpoint:${e}:*`, s = await this.client.keys(t);
358
+ s.length > 0 && await this.client.del(s);
359
+ const i = `writes:${e}:*`, c = await this.client.keys(i);
360
+ c.length > 0 && await this.client.del(c);
361
+ }
362
+ async end() {
363
+ await this.client.quit();
364
+ }
365
+ async loadPendingWrites(e, t, s) {
366
+ const i = `checkpoint_write:${e}:${t}:${s}:*`, c = await this.client.keys(i);
367
+ if (c.length === 0) return;
368
+ const o = [];
369
+ for (const n of c) {
370
+ const r = await this.client.json.get(n);
371
+ r && o.push(r);
372
+ }
373
+ o.sort((n, r) => (n.global_idx || 0) - (r.global_idx || 0));
374
+ const f = [];
375
+ for (const n of o) {
376
+ const r = await this.serde.loadsTyped("json", JSON.stringify(n.value));
377
+ f.push([
378
+ n.task_id,
379
+ n.channel,
380
+ r
381
+ ]);
382
+ }
383
+ return f;
384
+ }
385
+ async loadCheckpointWithWrites(e) {
386
+ const t = await this.serde.loadsTyped("json", JSON.stringify(e.checkpoint));
387
+ if (t.v < 4 && e.parent_checkpoint_id != null) {
388
+ const i = e.checkpoint_ns === "__empty__" ? "" : e.checkpoint_ns;
389
+ await this.migratePendingSends(t, e.thread_id, i, e.parent_checkpoint_id);
390
+ }
391
+ let s;
392
+ if (e.has_writes === "true") {
393
+ const i = e.checkpoint_ns === "__empty__" ? "" : e.checkpoint_ns;
394
+ s = await this.loadPendingWrites(e.thread_id, i, e.checkpoint_id);
395
+ }
396
+ return {
397
+ checkpoint: t,
398
+ pendingWrites: s
399
+ };
400
+ }
401
+ async migratePendingSends(e, t, s, i) {
402
+ const c = await this.loadPendingWrites(t, s, i);
403
+ if (!c || c.length === 0) return;
404
+ const o = c.filter(([, n]) => n === $);
405
+ if (o.length === 0) return;
406
+ const f = [];
407
+ for (const [, , n] of o) f.push(n);
408
+ e.channel_values ??= {}, e.channel_values[$] = f, e.channel_versions[$] = Object.keys(e.channel_versions).length > 0 ? I(...Object.values(e.channel_versions)) : 1;
409
+ }
410
+ async createCheckpointTuple(e, t, s) {
411
+ const i = e.checkpoint_ns === "__empty__" ? "" : e.checkpoint_ns, c = await this.serde.loadsTyped("json", JSON.stringify(e.metadata));
412
+ return {
413
+ config: { configurable: {
414
+ thread_id: e.thread_id,
415
+ checkpoint_ns: i,
416
+ checkpoint_id: e.checkpoint_id
417
+ } },
418
+ checkpoint: t,
419
+ metadata: c,
420
+ parentConfig: e.parent_checkpoint_id ? { configurable: {
421
+ thread_id: e.thread_id,
422
+ checkpoint_ns: i,
423
+ checkpoint_id: e.parent_checkpoint_id
424
+ } } : void 0,
425
+ pendingWrites: s
426
+ };
427
+ }
428
+ addSearchableMetadataFields(e, t) {
429
+ 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));
430
+ }
431
+ async applyTTL(...e) {
432
+ if (!this.ttlConfig?.defaultTTL) return;
433
+ const t = Math.floor(this.ttlConfig.defaultTTL * 60), s = await Promise.allSettled(e.map((i) => this.client.expire(i, t)));
434
+ 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);
435
+ }
436
+ async ensureIndexes() {
437
+ for (const e of W) try {
438
+ await this.client.ft.create(e.index, e.schema, {
439
+ ON: "JSON",
440
+ PREFIX: e.prefix
441
+ });
442
+ } catch (t) {
443
+ t.message?.includes("Index already exists") || console.error(`Failed to create index ${e.index}:`, t.message);
444
+ }
445
+ }
446
+ };
447
+ function g(w) {
448
+ if (w === null || typeof w != "object") return JSON.stringify(w);
449
+ if (Array.isArray(w)) return JSON.stringify(w.map((s) => g(s)));
450
+ const e = {}, t = Object.keys(w).sort();
451
+ for (const s of t) e[s] = w[s];
452
+ return JSON.stringify(e, (s, i) => {
453
+ if (i !== null && typeof i == "object" && !Array.isArray(i)) {
454
+ const c = {}, o = Object.keys(i).sort();
455
+ for (const f of o) c[f] = i[f];
456
+ return c;
457
+ }
458
+ return i;
459
+ });
460
+ }
461
+ export {
462
+ E as RedisSaver
463
+ };
@@ -1,5 +1,5 @@
1
- import { A as s, C as m, F as o, e as c, S as i, a as p, d as h, b as k, h as n, f as g, g as T, T as f, c as l } from "./graphBuilder-B3IJ7dB2.mjs";
2
- import { FileSystemSkillStore as y } from "./FileSystemSkillStore-yvEvcRxB.mjs";
1
+ import { A as s, C as m, F as o, e as c, S as i, a as p, d as h, b as k, h as n, f as g, g as T, T as f, c as l } from "./graphBuilder-skVEY6RJ.mjs";
2
+ import { FileSystemSkillStore as y } from "./FileSystemSkillStore-Dl5briBv.mjs";
3
3
  import { FileSystemPluginStore as F } from "./FileSystemPluginStore-ChortK7z.mjs";
4
4
  import "lowdb";
5
5
  import "lowdb/node";
@@ -22,7 +22,7 @@ function I(t, o) {
22
22
  const n = k(t);
23
23
  if (n.size > Y)
24
24
  return console.warn(`Skipping ${t}: file too large (${n.size} bytes)`), null;
25
- const i = x(t, "utf-8"), e = /^---\s*\n(.*?)\n---\s*\n/s, a = i.match(e);
25
+ const i = x(t, "utf-8"), e = /^---\s*\n([\s\S]*?)\n---\s*\n/, a = i.match(e);
26
26
  if (!a)
27
27
  return console.warn(`Skipping ${t}: no valid YAML frontmatter found`), null;
28
28
  const c = a[1];
@@ -1,5 +1,5 @@
1
- import { l } from "./load-Bh8eo6qE.mjs";
2
- import { SystemMessage as h } from "@langchain/core/messages";
1
+ import { l } from "./load-iYJqYrjn.mjs";
2
+ import { n as h } from "./graphBuilder-skVEY6RJ.mjs";
3
3
  const M = `## Memory System
4
4
 
5
5
  你可以访问一个包含之前任务和经验知识的记忆库。
@@ -1,46 +1,49 @@
1
1
  import { HumanMessage as c } from "langchain";
2
- import { i as d, j as m, k as l } from "./graphBuilder-B3IJ7dB2.mjs";
3
- import "@langgraph-js/pro";
2
+ import { i as m, j as d, k as p } from "./graphBuilder-skVEY6RJ.mjs";
3
+ import "@langchain/langgraph";
4
+ import "zod";
5
+ import "@langchain/openai";
4
6
  import "@langchain/anthropic";
5
7
  import "@langchain/google-genai";
6
8
  import "yaml";
7
- import "@langchain/core/messages";
8
9
  import "@langgraph-js/standard-agent";
9
- import "@langgraph-js/auk";
10
- async function p() {
10
+ import "micromatch";
11
+ import "lowdb";
12
+ import "lowdb/node";
13
+ async function l() {
11
14
  return new Promise((t, n) => {
12
- let r = "";
13
- process.stdin.setEncoding("utf8"), process.stdin.on("data", (e) => {
14
- r += e;
15
+ let e = "";
16
+ process.stdin.setEncoding("utf8"), process.stdin.on("data", (r) => {
17
+ e += r;
15
18
  }), process.stdin.on("end", () => {
16
- t(r);
17
- }), process.stdin.on("error", (e) => {
18
- n(e);
19
+ t(e);
20
+ }), process.stdin.on("error", (r) => {
21
+ n(r);
19
22
  });
20
23
  });
21
24
  }
22
- async function b(t, n = !1) {
23
- await d();
24
- const r = await m();
25
- let e = t || "";
25
+ async function S(t, n = !1) {
26
+ await m();
27
+ const e = await d();
28
+ let r = t || "";
26
29
  if (n)
27
30
  try {
28
- e = (await p()).trim();
31
+ r = (await l()).trim();
29
32
  } catch (o) {
30
33
  throw console.error("❌ 读取 stdin 失败:", o), o;
31
34
  }
32
- e || (console.error("❌ 错误: 未提供输入内容"), console.error(`
35
+ r || (console.error("❌ 错误: 未提供输入内容"), console.error(`
33
36
  用法:`), console.error(' zen-code -p "你的任务描述"'), console.error(' echo "内容" | zen-code'), process.exit(1));
34
37
  try {
35
38
  const o = {
36
- messages: [new c(e)],
37
- provider_id: r.provider_id,
38
- model_id: r.model_id,
39
- enable_thinking: r.enable_thinking
40
- }, s = await l.invoke(o, {
39
+ messages: [new c(r)],
40
+ provider_id: e.provider_id,
41
+ model_id: e.model_id,
42
+ enable_thinking: e.enable_thinking
43
+ }, i = await p.invoke(o, {
41
44
  recursionLimit: 500
42
- }), i = s.messages || [], a = i[i.length - 1];
43
- return a && console.log(a.text), s;
45
+ }), s = i.messages || [], a = s[s.length - 1];
46
+ return a && console.log(a.text), i;
44
47
  } catch (o) {
45
48
  throw console.error(`
46
49
  ❌ 执行失败: ${o instanceof Error ? o.message : String(o)}
@@ -48,5 +51,5 @@ async function b(t, n = !1) {
48
51
  }
49
52
  }
50
53
  export {
51
- b as runNonInteractive
54
+ S as runNonInteractive
52
55
  };
@@ -0,0 +1,60 @@
1
+ import { Kysely as r, PostgresDialect as s, sql as e } from "kysely";
2
+ class i {
3
+ constructor(t) {
4
+ this.pool = t, this.db = new r({
5
+ dialect: new s({
6
+ pool: t
7
+ })
8
+ });
9
+ }
10
+ db;
11
+ dateToDb(t) {
12
+ return t;
13
+ }
14
+ dbToDate(t) {
15
+ return t instanceof Date ? t : new Date(t);
16
+ }
17
+ jsonToDb(t) {
18
+ return t;
19
+ }
20
+ dbToJson(t) {
21
+ return t;
22
+ }
23
+ buildJsonQuery(t, a, T, E) {
24
+ return e`${e.ref(a)}->>${e.lit(T)} = ${e.lit(JSON.stringify(E))}`;
25
+ }
26
+ now() {
27
+ return /* @__PURE__ */ new Date();
28
+ }
29
+ async createTables(t) {
30
+ await e`
31
+ CREATE TABLE IF NOT EXISTS threads (
32
+ thread_id TEXT PRIMARY KEY,
33
+ created_at TIMESTAMP NOT NULL,
34
+ updated_at TIMESTAMP NOT NULL,
35
+ metadata JSONB NOT NULL DEFAULT '{}',
36
+ status TEXT NOT NULL DEFAULT 'idle',
37
+ values JSONB,
38
+ interrupts JSONB NOT NULL DEFAULT '{}'
39
+ )
40
+ `.execute(t), await e`
41
+ CREATE TABLE IF NOT EXISTS runs (
42
+ run_id TEXT PRIMARY KEY,
43
+ thread_id TEXT NOT NULL,
44
+ assistant_id TEXT NOT NULL,
45
+ created_at TIMESTAMP NOT NULL,
46
+ updated_at TIMESTAMP NOT NULL,
47
+ status TEXT NOT NULL DEFAULT 'pending',
48
+ metadata JSONB NOT NULL DEFAULT '{}',
49
+ multitask_strategy TEXT NOT NULL DEFAULT 'reject',
50
+ FOREIGN KEY (thread_id) REFERENCES threads(thread_id) ON DELETE CASCADE
51
+ )
52
+ `.execute(t);
53
+ }
54
+ async createIndexes(t) {
55
+ await e`CREATE INDEX IF NOT EXISTS idx_threads_status ON threads(status)`.execute(t), await e`CREATE INDEX IF NOT EXISTS idx_threads_created_at ON threads(created_at)`.execute(t), await e`CREATE INDEX IF NOT EXISTS idx_threads_updated_at ON threads(updated_at)`.execute(t), await e`CREATE INDEX IF NOT EXISTS idx_runs_thread_id ON runs(thread_id)`.execute(t), await e`CREATE INDEX IF NOT EXISTS idx_runs_status ON runs(status)`.execute(t);
56
+ }
57
+ }
58
+ export {
59
+ i as PostgresAdapter
60
+ };