@axiom-lattice/local-stores 3.2.13 → 4.0.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.
- package/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +99 -0
- package/dist/index.d.mts +21 -2
- package/dist/index.d.ts +21 -2
- package/dist/index.js +312 -108
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +300 -91
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/LocalEvalStore.test.ts +13 -0
- package/src/__tests__/LocalStore.test.ts +151 -0
- package/src/__tests__/LocalTaskStore.test.ts +14 -0
- package/src/__tests__/LocalThreadMessageQueueStore.test.ts +1 -0
- package/src/__tests__/LocalThreadStore.test.ts +51 -0
- package/src/createLocalStoreConfig.ts +2 -0
- package/src/index.ts +1 -0
- package/src/stores/LocalEvalStore.ts +10 -0
- package/src/stores/LocalStore.ts +218 -0
- package/src/stores/LocalTaskStore.ts +1 -0
- package/src/stores/LocalThreadMessageQueueStore.ts +8 -2
- package/src/stores/LocalThreadStore.ts +34 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axiom-lattice/local-stores",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Local SQLite-based stores for Axiom Lattice framework",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"@types/sql.js": "^1.4.11",
|
|
26
26
|
"sql.js": "^1.14.1",
|
|
27
27
|
"uuid": "^14.0.1",
|
|
28
|
-
"@axiom-lattice/core": "
|
|
29
|
-
"@axiom-lattice/protocols": "
|
|
28
|
+
"@axiom-lattice/core": "10.0.0",
|
|
29
|
+
"@axiom-lattice/protocols": "6.0.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/jest": "^29.5.14",
|
|
@@ -132,4 +132,17 @@ describe("LocalEvalStore project name uniqueness", () => {
|
|
|
132
132
|
it("returns false when deleting a project that does not exist", async () => {
|
|
133
133
|
await expect(store.deleteProject("tenant-1", "missing-project")).resolves.toBe(false);
|
|
134
134
|
});
|
|
135
|
+
it("persists the cooperative eval cancel flag", async () => {
|
|
136
|
+
await store.createRun("tenant-1", "project-1", "run-cancel", {
|
|
137
|
+
concurrency: 1,
|
|
138
|
+
totalCases: 1,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
await expect(store.requestCancel?.("tenant-1", "run-cancel")).resolves.toBe(true);
|
|
142
|
+
expect((await store.getRunById("tenant-1", "run-cancel"))?.cancelRequested).toBe(true);
|
|
143
|
+
|
|
144
|
+
await store.updateRunStatus("tenant-1", "run-cancel", { status: "completed" });
|
|
145
|
+
await expect(store.requestCancel?.("tenant-1", "run-cancel")).resolves.toBe(false);
|
|
146
|
+
});
|
|
147
|
+
|
|
135
148
|
});
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
2
|
+
import { tmpdir } from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { closeDatabase, initDatabase } from "../database";
|
|
5
|
+
import { LocalStore } from "../stores/LocalStore";
|
|
6
|
+
|
|
7
|
+
const nextDbPath = () =>
|
|
8
|
+
path.join(tmpdir(), `lt-store-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
|
9
|
+
|
|
10
|
+
describe("LocalStore", () => {
|
|
11
|
+
const tenant = "tenant-1";
|
|
12
|
+
let store: LocalStore;
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
jest.useFakeTimers();
|
|
16
|
+
jest.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
|
|
17
|
+
const db = await initDatabase({ dbPath: nextDbPath() });
|
|
18
|
+
store = new LocalStore(db);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
jest.useRealTimers();
|
|
23
|
+
closeDatabase();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("round-trips items and isolates tenants", async () => {
|
|
27
|
+
await store.put(tenant, { namespace: ["a", "b"], key: "one", value: { n: 1 } });
|
|
28
|
+
|
|
29
|
+
expect(await store.get(tenant, ["a", "b"], "one")).toMatchObject({
|
|
30
|
+
namespace: ["a", "b"],
|
|
31
|
+
key: "one",
|
|
32
|
+
value: { n: 1 },
|
|
33
|
+
});
|
|
34
|
+
expect(await store.get("tenant-2", ["a", "b"], "one")).toBeNull();
|
|
35
|
+
expect(await store.delete(tenant, ["a", "b"], "one")).toBe(true);
|
|
36
|
+
expect(await store.get(tenant, ["a", "b"], "one")).toBeNull();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("searches by namespace prefix, deep filter, and query", async () => {
|
|
40
|
+
await store.put(tenant, {
|
|
41
|
+
namespace: ["app", "a"], key: "one",
|
|
42
|
+
value: { message: "Hello World", nested: { tag: "x" }, list: [1, 2] },
|
|
43
|
+
});
|
|
44
|
+
await store.put(tenant, {
|
|
45
|
+
namespace: ["app", "b"], key: "two",
|
|
46
|
+
value: { message: "other", nested: { tag: "y" }, list: [1, 2] },
|
|
47
|
+
});
|
|
48
|
+
await store.put(tenant, {
|
|
49
|
+
namespace: ["other"], key: "three", value: { message: "Hello other" },
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect((await store.search(tenant, { namespacePrefix: ["app"] })).map((item) => item.key))
|
|
53
|
+
.toEqual(["one", "two"]);
|
|
54
|
+
expect((await store.search(tenant, { filter: { nested: { tag: "x" } } })).map((item) => item.key))
|
|
55
|
+
.toEqual(["one"]);
|
|
56
|
+
expect((await store.search(tenant, { query: "HELLO" })).map((item) => item.key))
|
|
57
|
+
.toEqual(["one", "three"]);
|
|
58
|
+
expect((await store.search(tenant, { filter: { list: [2] } })).map((item) => item.key).sort())
|
|
59
|
+
.toEqual(["one", "two"]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("treats percent and underscore literally in query", async () => {
|
|
63
|
+
await store.put(tenant, { namespace: ["q"], key: "percent", value: { text: "100% done" } });
|
|
64
|
+
await store.put(tenant, { namespace: ["q"], key: "underscore", value: { text: "under_score" } });
|
|
65
|
+
|
|
66
|
+
expect((await store.search(tenant, { query: "%" })).map((item) => item.key))
|
|
67
|
+
.toEqual(["percent"]);
|
|
68
|
+
expect((await store.search(tenant, { query: "_" })).map((item) => item.key))
|
|
69
|
+
.toEqual(["underscore"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("upserts per tenant and does not leak across tenants", async () => {
|
|
73
|
+
await store.put(tenant, { namespace: ["shared"], key: "one", value: { owner: "a" } });
|
|
74
|
+
await store.put("tenant-2", { namespace: ["shared"], key: "one", value: { owner: "b" } });
|
|
75
|
+
|
|
76
|
+
expect((await store.get(tenant, ["shared"], "one"))?.value.owner).toBe("a");
|
|
77
|
+
expect((await store.get("tenant-2", ["shared"], "one"))?.value.owner).toBe("b");
|
|
78
|
+
expect((await store.search(tenant, { namespacePrefix: ["shared"] })).map((item) => item.value.owner))
|
|
79
|
+
.toEqual(["a"]);
|
|
80
|
+
|
|
81
|
+
expect(await store.delete(tenant, ["shared"], "one")).toBe(true);
|
|
82
|
+
expect((await store.get("tenant-2", ["shared"], "one"))?.value.owner).toBe("b");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("clears a previous TTL when upserting without one", async () => {
|
|
86
|
+
await store.put(tenant, {
|
|
87
|
+
namespace: ["ttl"], key: "reset", value: { v: 1 }, ttlMinutes: 1,
|
|
88
|
+
});
|
|
89
|
+
await store.put(tenant, { namespace: ["ttl"], key: "reset", value: { v: 2 } });
|
|
90
|
+
jest.setSystemTime(new Date("2026-01-01T00:02:00.000Z"));
|
|
91
|
+
|
|
92
|
+
expect((await store.get(tenant, ["ttl"], "reset"))?.value.v).toBe(2);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("rejects identity collisions and invalid TTL/pagination at the store", async () => {
|
|
96
|
+
await expect(store.put(tenant, { namespace: ["a\u001fb"], key: "k", value: {} }))
|
|
97
|
+
.rejects.toThrow("reserved separator");
|
|
98
|
+
await expect(store.put(tenant, { namespace: ["a"], key: "b\u001fc", value: {} }))
|
|
99
|
+
.rejects.toThrow("reserved separator");
|
|
100
|
+
await expect(store.put(tenant, {
|
|
101
|
+
namespace: ["a"], key: "k", value: {}, ttlMinutes: 0,
|
|
102
|
+
})).rejects.toThrow("ttlMinutes");
|
|
103
|
+
await expect(store.search(tenant, { limit: -1 })).rejects.toThrow("limit/offset");
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("uses namespace and key as stable tie-breakers", async () => {
|
|
107
|
+
jest.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
|
|
108
|
+
await store.put(tenant, { namespace: ["b"], key: "one", value: {} });
|
|
109
|
+
await store.put(tenant, { namespace: ["a"], key: "two", value: {} });
|
|
110
|
+
await store.put(tenant, { namespace: ["a"], key: "one", value: {} });
|
|
111
|
+
|
|
112
|
+
expect((await store.search(tenant, {})).map((item) => `${item.namespace[0]}:${item.key}`))
|
|
113
|
+
.toEqual(["a:one", "a:two", "b:one"]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("paginates in updated-at descending order", async () => {
|
|
117
|
+
for (const [index, key] of ["one", "two", "three"].entries()) {
|
|
118
|
+
jest.setSystemTime(new Date(Date.UTC(2026, 0, 1, 0, 0, index)));
|
|
119
|
+
await store.put(tenant, { namespace: ["app"], key, value: { index } });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
expect((await store.search(tenant, { limit: 2 })).map((item) => item.key))
|
|
123
|
+
.toEqual(["three", "two"]);
|
|
124
|
+
expect((await store.search(tenant, { limit: 2, offset: 1 })).map((item) => item.key))
|
|
125
|
+
.toEqual(["two", "one"]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("hides expired items and hard-deletes them", async () => {
|
|
129
|
+
await store.put(tenant, {
|
|
130
|
+
namespace: ["ttl"], key: "short", value: { ok: true }, ttlMinutes: 1,
|
|
131
|
+
});
|
|
132
|
+
jest.setSystemTime(new Date("2026-01-01T00:01:01.000Z"));
|
|
133
|
+
|
|
134
|
+
expect(await store.get(tenant, ["ttl"], "short")).toBeNull();
|
|
135
|
+
expect(await store.search(tenant, { namespacePrefix: ["ttl"] })).toEqual([]);
|
|
136
|
+
expect(await store.listNamespaces(tenant)).toEqual([]);
|
|
137
|
+
|
|
138
|
+
expect(await store.deleteExpired(new Date("2026-01-01T00:02:00.000Z"), 10)).toBe(1);
|
|
139
|
+
expect(await store.deleteExpired(new Date("2026-01-01T00:02:00.000Z"), 10)).toBe(0);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("lists distinct sorted namespaces with limit", async () => {
|
|
143
|
+
await store.put(tenant, { namespace: ["b"], key: "1", value: {} });
|
|
144
|
+
await store.put(tenant, { namespace: ["a", "x"], key: "2", value: {} });
|
|
145
|
+
await store.put(tenant, { namespace: ["a", "x"], key: "3", value: {} });
|
|
146
|
+
|
|
147
|
+
expect(await store.listNamespaces(tenant)).toEqual([["a", "x"], ["b"]]);
|
|
148
|
+
expect(await store.listNamespaces(tenant, [], 1)).toEqual([["a", "x"]]);
|
|
149
|
+
expect(await store.listNamespaces(tenant, ["a"])).toEqual([["a", "x"]]);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
@@ -59,6 +59,20 @@ describe("LocalTaskStore metadata filter", () => {
|
|
|
59
59
|
expect(byBoolean).toHaveLength(1);
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
+
it("filters tasks by the isolated run thread in context", async () => {
|
|
63
|
+
await store.create({
|
|
64
|
+
tenantId: "t1", ownerType: "agent", ownerId: "agent-1", title: "Worker",
|
|
65
|
+
context: { thread_id: "thread-worker" },
|
|
66
|
+
} as never);
|
|
67
|
+
await store.create({
|
|
68
|
+
tenantId: "t1", ownerType: "agent", ownerId: "agent-1", title: "Other",
|
|
69
|
+
context: { thread_id: "thread-other" },
|
|
70
|
+
} as never);
|
|
71
|
+
|
|
72
|
+
const tasks = await store.list({ tenantId: "t1", threadId: "thread-worker" } as never);
|
|
73
|
+
expect(tasks.map((task) => task.title)).toEqual(["Worker"]);
|
|
74
|
+
});
|
|
75
|
+
|
|
62
76
|
it("matches numeric metadata regardless of stored string/number form", async () => {
|
|
63
77
|
await store.create({
|
|
64
78
|
tenantId: "t1", ownerType: "agent", ownerId: "a1", title: "num", metadata: { retries: 2 },
|
|
@@ -60,6 +60,7 @@ describe("LocalThreadMessageQueueStore scope isolation", () => {
|
|
|
60
60
|
threadId: "legacy",
|
|
61
61
|
workspaceId: null,
|
|
62
62
|
projectId: null,
|
|
63
|
+
hasPending: true,
|
|
63
64
|
}]);
|
|
64
65
|
expect(await store.getPendingMessages("legacy", scope)).toHaveLength(1);
|
|
65
66
|
await store.markProcessing(message.id, undefined, scope);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
|
2
|
+
import { tmpdir } from "os";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { closeDatabase, initDatabase } from "../database";
|
|
5
|
+
import { LocalThreadStore } from "../stores/LocalThreadStore";
|
|
6
|
+
|
|
7
|
+
const nextDbPath = () =>
|
|
8
|
+
path.join(tmpdir(), `lt-thread-test-${Date.now()}-${Math.random().toString(36).slice(2)}.db`);
|
|
9
|
+
|
|
10
|
+
describe("LocalThreadStore TTL", () => {
|
|
11
|
+
let store: LocalThreadStore;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
const db = await initDatabase({ dbPath: nextDbPath() });
|
|
15
|
+
store = new LocalThreadStore(db);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
closeDatabase();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("round-trips expiresAt through create, get, and update", async () => {
|
|
23
|
+
const expiresAt = new Date("2026-01-01T00:00:00.000Z");
|
|
24
|
+
await store.createThread("t1", "a1", "th1", { expiresAt });
|
|
25
|
+
|
|
26
|
+
expect((await store.getThreadById("t1", "th1"))?.expiresAt).toEqual(expiresAt);
|
|
27
|
+
|
|
28
|
+
const next = new Date("2026-02-01T00:00:00.000Z");
|
|
29
|
+
await store.updateThread("t1", "th1", { expiresAt: next });
|
|
30
|
+
expect((await store.getThreadById("t1", "th1"))?.expiresAt).toEqual(next);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("lists expired threads across tenants, oldest first, up to the limit", async () => {
|
|
34
|
+
const now = new Date("2026-01-01T00:00:00.000Z");
|
|
35
|
+
await store.createThread("t1", "a1", "later", {
|
|
36
|
+
expiresAt: new Date("2025-12-31T23:00:00.000Z"),
|
|
37
|
+
});
|
|
38
|
+
await store.createThread("t2", "a2", "earlier", {
|
|
39
|
+
expiresAt: new Date("2025-12-31T22:00:00.000Z"),
|
|
40
|
+
});
|
|
41
|
+
await store.createThread("t1", "a1", "future", {
|
|
42
|
+
expiresAt: new Date("2026-01-01T01:00:00.000Z"),
|
|
43
|
+
});
|
|
44
|
+
await store.createThread("t1", "a1", "never", {});
|
|
45
|
+
|
|
46
|
+
expect((await store.listExpiredThreads(now, 10)).map((thread) => thread.id))
|
|
47
|
+
.toEqual(["earlier", "later"]);
|
|
48
|
+
expect((await store.listExpiredThreads(now, 1)).map((thread) => thread.id))
|
|
49
|
+
.toEqual(["earlier"]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { initDatabase } from "./database";
|
|
18
18
|
import { SqliteSaver } from "@langchain/langgraph-checkpoint-sqlite";
|
|
19
19
|
import { LocalThreadStore } from "./stores/LocalThreadStore";
|
|
20
|
+
import { LocalStore } from "./stores/LocalStore";
|
|
20
21
|
import { LocalAssistantStore } from "./stores/LocalAssistantStore";
|
|
21
22
|
import { LocalWorkspaceStore } from "./stores/LocalWorkspaceStore";
|
|
22
23
|
import { LocalProjectStore } from "./stores/LocalProjectStore";
|
|
@@ -70,6 +71,7 @@ export async function createLocalStoreConfig(options: LocalStoreConfigOptions =
|
|
|
70
71
|
|
|
71
72
|
return {
|
|
72
73
|
thread: new LocalThreadStore(db),
|
|
74
|
+
store: new LocalStore(db),
|
|
73
75
|
assistant: new LocalAssistantStore(db),
|
|
74
76
|
workspace: new LocalWorkspaceStore(db),
|
|
75
77
|
project: new LocalProjectStore(db),
|
package/src/index.ts
CHANGED
|
@@ -28,6 +28,7 @@ export { createLocalStoreConfig } from "./createLocalStoreConfig";
|
|
|
28
28
|
export type { LocalStoreConfigOptions } from "./createLocalStoreConfig";
|
|
29
29
|
|
|
30
30
|
export { LocalThreadStore } from "./stores/LocalThreadStore";
|
|
31
|
+
export { LocalStore } from "./stores/LocalStore";
|
|
31
32
|
export { LocalAssistantStore } from "./stores/LocalAssistantStore";
|
|
32
33
|
export { LocalWorkspaceStore } from "./stores/LocalWorkspaceStore";
|
|
33
34
|
export { LocalProjectStore } from "./stores/LocalProjectStore";
|
|
@@ -76,6 +76,7 @@ CREATE TABLE IF NOT EXISTS lt_eval_runs (
|
|
|
76
76
|
interrupted_cases INTEGER NOT NULL DEFAULT 0,
|
|
77
77
|
avg_score REAL NOT NULL DEFAULT 0,
|
|
78
78
|
error TEXT,
|
|
79
|
+
cancel_requested INTEGER NOT NULL DEFAULT 0,
|
|
79
80
|
holdout INTEGER NOT NULL DEFAULT 0,
|
|
80
81
|
env_project_id TEXT,
|
|
81
82
|
env_workspace_id TEXT,
|
|
@@ -135,6 +136,7 @@ export class LocalEvalStore implements EvalStore {
|
|
|
135
136
|
this.ensureColumn("lt_eval_run_results", "interrupted", "interrupted INTEGER NOT NULL DEFAULT 0");
|
|
136
137
|
this.ensureColumn("lt_eval_cases", "interrupt_policy", "interrupt_policy TEXT");
|
|
137
138
|
this.ensureColumn("lt_eval_runs", "task_id", "task_id TEXT");
|
|
139
|
+
this.ensureColumn("lt_eval_runs", "cancel_requested", "cancel_requested INTEGER NOT NULL DEFAULT 0");
|
|
138
140
|
// Project name must be unique per tenant (eval-{agent-id} convention).
|
|
139
141
|
// Repoint child rows (suites, runs) of duplicate projects onto the kept
|
|
140
142
|
// project (newest by created_at, tie-break rowid — mirrors the pg
|
|
@@ -455,6 +457,13 @@ export class LocalEvalStore implements EvalStore {
|
|
|
455
457
|
return this.getRunById(tenantId, id);
|
|
456
458
|
}
|
|
457
459
|
|
|
460
|
+
async requestCancel(tenantId: string, id: string): Promise<boolean> {
|
|
461
|
+
const result = this.db.prepare(
|
|
462
|
+
`UPDATE lt_eval_runs SET cancel_requested = 1 WHERE tenant_id = ? AND id = ? AND status = 'running'`,
|
|
463
|
+
).run(tenantId, id);
|
|
464
|
+
return result.changes > 0;
|
|
465
|
+
}
|
|
466
|
+
|
|
458
467
|
async deleteRun(tenantId: string, id: string): Promise<boolean> {
|
|
459
468
|
const result = this.db.prepare(`DELETE FROM lt_eval_runs WHERE tenant_id = ? AND id = ?`).run(tenantId, id);
|
|
460
469
|
return result.changes > 0;
|
|
@@ -612,6 +621,7 @@ export class LocalEvalStore implements EvalStore {
|
|
|
612
621
|
interruptedCases: (row.interrupted_cases as number) ?? 0,
|
|
613
622
|
avgScore: (row.avg_score as number) ?? 0,
|
|
614
623
|
error: row.error as string | undefined,
|
|
624
|
+
cancelRequested: Boolean(row.cancel_requested),
|
|
615
625
|
holdout: row.holdout ? Boolean(row.holdout) : undefined,
|
|
616
626
|
envProjectId: row.env_project_id as string | undefined,
|
|
617
627
|
envWorkspaceId: row.env_workspace_id as string | undefined,
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertStoreIdentity,
|
|
3
|
+
assertStoreNamespacePrefix,
|
|
4
|
+
storeExpiresAt,
|
|
5
|
+
storePaginationValue,
|
|
6
|
+
STORE_IDENTITY_SEPARATOR,
|
|
7
|
+
type StoreItem,
|
|
8
|
+
type StoreProtocol,
|
|
9
|
+
type StorePutInput,
|
|
10
|
+
type StoreSearchInput,
|
|
11
|
+
} from "@axiom-lattice/protocols";
|
|
12
|
+
import { nowISO, parseISO, type DatabaseWrapper } from "../database";
|
|
13
|
+
import { MigrationManager } from "../migrations/migration";
|
|
14
|
+
|
|
15
|
+
const STORE_NAME = "lt_store_items";
|
|
16
|
+
const NAMESPACE_SEPARATOR = STORE_IDENTITY_SEPARATOR;
|
|
17
|
+
|
|
18
|
+
const DDL = `
|
|
19
|
+
CREATE TABLE IF NOT EXISTS lt_store_items (
|
|
20
|
+
tenant_id TEXT NOT NULL,
|
|
21
|
+
namespace_path TEXT NOT NULL,
|
|
22
|
+
key TEXT NOT NULL,
|
|
23
|
+
value TEXT NOT NULL,
|
|
24
|
+
created_at TEXT NOT NULL,
|
|
25
|
+
updated_at TEXT NOT NULL,
|
|
26
|
+
expires_at TEXT,
|
|
27
|
+
PRIMARY KEY (tenant_id, namespace_path, key)
|
|
28
|
+
);
|
|
29
|
+
CREATE INDEX IF NOT EXISTS idx_lt_store_items_tenant_updated
|
|
30
|
+
ON lt_store_items(tenant_id, updated_at DESC);
|
|
31
|
+
CREATE INDEX IF NOT EXISTS idx_lt_store_items_expires
|
|
32
|
+
ON lt_store_items(expires_at) WHERE expires_at IS NOT NULL;
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
interface StoreRow {
|
|
36
|
+
tenant_id: string;
|
|
37
|
+
namespace_path: string;
|
|
38
|
+
key: string;
|
|
39
|
+
value: string;
|
|
40
|
+
created_at: string;
|
|
41
|
+
updated_at: string;
|
|
42
|
+
expires_at: string | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function namespacePath(namespace: string[]): string {
|
|
46
|
+
return namespace.join(NAMESPACE_SEPARATOR);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
50
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function deepContains(actual: unknown, expected: unknown): boolean {
|
|
54
|
+
if (Array.isArray(expected)) {
|
|
55
|
+
return Array.isArray(actual)
|
|
56
|
+
&& expected.every((item) => actual.some((candidate) => deepContains(candidate, item)));
|
|
57
|
+
}
|
|
58
|
+
if (isPlainObject(expected)) {
|
|
59
|
+
if (!isPlainObject(actual)) return false;
|
|
60
|
+
return Object.entries(expected).every(([key, value]) => deepContains(actual[key], value));
|
|
61
|
+
}
|
|
62
|
+
return Object.is(actual, expected);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function matchesFilter(value: Record<string, unknown>, filter: Record<string, unknown>): boolean {
|
|
66
|
+
return deepContains(value, filter);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function hasPrefix(namespace: string[], prefix: string[]): boolean {
|
|
70
|
+
return prefix.length === 0
|
|
71
|
+
|| (namespace.length >= prefix.length
|
|
72
|
+
&& prefix.every((segment, index) => namespace[index] === segment));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function mapRow(row: StoreRow): StoreItem {
|
|
76
|
+
return {
|
|
77
|
+
namespace: row.namespace_path.split(NAMESPACE_SEPARATOR),
|
|
78
|
+
key: row.key,
|
|
79
|
+
value: JSON.parse(row.value) as Record<string, unknown>,
|
|
80
|
+
createdAt: parseISO(row.created_at),
|
|
81
|
+
updatedAt: parseISO(row.updated_at),
|
|
82
|
+
expiresAt: row.expires_at ? parseISO(row.expires_at) : undefined,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Local SQLite tenant-scoped namespaced key-value store.
|
|
88
|
+
*/
|
|
89
|
+
export class LocalStore implements StoreProtocol {
|
|
90
|
+
constructor(private readonly db: DatabaseWrapper) {
|
|
91
|
+
const mm = new MigrationManager(db, STORE_NAME);
|
|
92
|
+
mm.register({
|
|
93
|
+
version: 1,
|
|
94
|
+
name: "create_lt_store_items",
|
|
95
|
+
up: (database) => { database.exec(DDL); },
|
|
96
|
+
});
|
|
97
|
+
mm.migrate();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async put(tenantId: string, input: StorePutInput): Promise<StoreItem> {
|
|
101
|
+
assertStoreIdentity(input.namespace, input.key);
|
|
102
|
+
const now = nowISO();
|
|
103
|
+
const path = namespacePath(input.namespace);
|
|
104
|
+
const existing = this.db.prepare(
|
|
105
|
+
`SELECT created_at FROM lt_store_items WHERE tenant_id = ? AND namespace_path = ? AND key = ?`,
|
|
106
|
+
).get(tenantId, path, input.key) as unknown as { created_at: string } | undefined;
|
|
107
|
+
const expiresAt = storeExpiresAt(input.ttlMinutes)?.toISOString() ?? null;
|
|
108
|
+
this.db.prepare(
|
|
109
|
+
`INSERT INTO lt_store_items
|
|
110
|
+
(tenant_id, namespace_path, key, value, created_at, updated_at, expires_at)
|
|
111
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
112
|
+
ON CONFLICT(tenant_id, namespace_path, key) DO UPDATE SET
|
|
113
|
+
value = excluded.value,
|
|
114
|
+
updated_at = excluded.updated_at,
|
|
115
|
+
expires_at = excluded.expires_at`,
|
|
116
|
+
).run(
|
|
117
|
+
tenantId,
|
|
118
|
+
path,
|
|
119
|
+
input.key,
|
|
120
|
+
JSON.stringify(input.value),
|
|
121
|
+
existing?.created_at ?? now,
|
|
122
|
+
now,
|
|
123
|
+
expiresAt,
|
|
124
|
+
);
|
|
125
|
+
return {
|
|
126
|
+
namespace: [...input.namespace],
|
|
127
|
+
key: input.key,
|
|
128
|
+
value: structuredClone(input.value),
|
|
129
|
+
createdAt: parseISO(existing?.created_at ?? now),
|
|
130
|
+
updatedAt: parseISO(now),
|
|
131
|
+
expiresAt: expiresAt ? parseISO(expiresAt) : undefined,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async get(tenantId: string, namespace: string[], key: string): Promise<StoreItem | null> {
|
|
136
|
+
assertStoreIdentity(namespace, key);
|
|
137
|
+
const now = nowISO();
|
|
138
|
+
const row = this.db.prepare(
|
|
139
|
+
`SELECT * FROM lt_store_items
|
|
140
|
+
WHERE tenant_id = ? AND namespace_path = ? AND key = ?
|
|
141
|
+
AND (expires_at IS NULL OR expires_at > ?)`,
|
|
142
|
+
).get(tenantId, namespacePath(namespace), key, now) as unknown as StoreRow | undefined;
|
|
143
|
+
return row ? mapRow(row) : null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async delete(tenantId: string, namespace: string[], key: string): Promise<boolean> {
|
|
147
|
+
assertStoreIdentity(namespace, key);
|
|
148
|
+
const result = this.db.prepare(
|
|
149
|
+
`DELETE FROM lt_store_items WHERE tenant_id = ? AND namespace_path = ? AND key = ?`,
|
|
150
|
+
).run(tenantId, namespacePath(namespace), key);
|
|
151
|
+
return result.changes > 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async search(tenantId: string, input: StoreSearchInput): Promise<StoreItem[]> {
|
|
155
|
+
const now = nowISO();
|
|
156
|
+
const prefix = input.namespacePrefix ?? [];
|
|
157
|
+
assertStoreNamespacePrefix(prefix);
|
|
158
|
+
const query = input.query?.toLowerCase();
|
|
159
|
+
const limit = storePaginationValue(input.limit, 10);
|
|
160
|
+
const offset = storePaginationValue(input.offset, 0);
|
|
161
|
+
const rows = this.db.prepare(
|
|
162
|
+
`SELECT * FROM lt_store_items
|
|
163
|
+
WHERE tenant_id = ? AND (expires_at IS NULL OR expires_at > ?)`,
|
|
164
|
+
).all(tenantId, now) as unknown as StoreRow[];
|
|
165
|
+
return rows
|
|
166
|
+
.map(mapRow)
|
|
167
|
+
.filter((item) => hasPrefix(item.namespace, prefix))
|
|
168
|
+
.filter((item) => input.filter === undefined || matchesFilter(item.value, input.filter))
|
|
169
|
+
.filter((item) => query === undefined
|
|
170
|
+
|| JSON.stringify(item.value).toLowerCase().includes(query))
|
|
171
|
+
.sort((left, right) =>
|
|
172
|
+
right.updatedAt.getTime() - left.updatedAt.getTime()
|
|
173
|
+
|| namespacePath(left.namespace).localeCompare(namespacePath(right.namespace))
|
|
174
|
+
|| left.key.localeCompare(right.key))
|
|
175
|
+
.slice(offset, offset + limit);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async listNamespaces(
|
|
179
|
+
tenantId: string,
|
|
180
|
+
prefix: string[] = [],
|
|
181
|
+
limit = 100,
|
|
182
|
+
): Promise<string[][]> {
|
|
183
|
+
assertStoreNamespacePrefix(prefix);
|
|
184
|
+
const rows = this.db.prepare(
|
|
185
|
+
`SELECT DISTINCT namespace_path FROM lt_store_items
|
|
186
|
+
WHERE tenant_id = ? AND (expires_at IS NULL OR expires_at > ?)`,
|
|
187
|
+
).all(tenantId, nowISO()) as unknown as Array<{ namespace_path: string }>;
|
|
188
|
+
const namespaces = new Map<string, string[]>();
|
|
189
|
+
for (const row of rows) {
|
|
190
|
+
const namespace = row.namespace_path.split(NAMESPACE_SEPARATOR);
|
|
191
|
+
if (hasPrefix(namespace, prefix)) namespaces.set(row.namespace_path, namespace);
|
|
192
|
+
}
|
|
193
|
+
return Array.from(namespaces.entries())
|
|
194
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
195
|
+
.slice(0, storePaginationValue(limit, 100))
|
|
196
|
+
.map(([, namespace]) => namespace);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async deleteExpired(now: Date, limit: number): Promise<number> {
|
|
200
|
+
const rows = this.db.prepare(
|
|
201
|
+
`SELECT tenant_id, namespace_path, key FROM lt_store_items
|
|
202
|
+
WHERE expires_at IS NOT NULL AND expires_at <= ?
|
|
203
|
+
LIMIT ?`,
|
|
204
|
+
).all(now.toISOString(), storePaginationValue(limit, 0)) as unknown as Array<{
|
|
205
|
+
tenant_id: string;
|
|
206
|
+
namespace_path: string;
|
|
207
|
+
key: string;
|
|
208
|
+
}>;
|
|
209
|
+
const statement = this.db.prepare(
|
|
210
|
+
`DELETE FROM lt_store_items WHERE tenant_id = ? AND namespace_path = ? AND key = ?`,
|
|
211
|
+
);
|
|
212
|
+
let deleted = 0;
|
|
213
|
+
for (const row of rows) {
|
|
214
|
+
deleted += statement.run(row.tenant_id, row.namespace_path, row.key).changes;
|
|
215
|
+
}
|
|
216
|
+
return deleted;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -200,6 +200,7 @@ export class LocalTaskStore implements TaskStore {
|
|
|
200
200
|
params.push(...statuses);
|
|
201
201
|
}
|
|
202
202
|
if (filter.priority) { conditions.push('priority = ?'); params.push(filter.priority); }
|
|
203
|
+
if (filter.threadId) { conditions.push("json_extract(context, '$.thread_id') = ?"); params.push(filter.threadId); }
|
|
203
204
|
if (filter.workspaceId) { conditions.push('workspace_id = ?'); params.push(filter.workspaceId); }
|
|
204
205
|
if (filter.projectId === null) conditions.push("(project_id IS NULL OR project_id = '' OR project_id = 'default')");
|
|
205
206
|
else if (filter.projectId !== undefined) { conditions.push('project_id = ?'); params.push(filter.projectId); }
|
|
@@ -254,14 +254,20 @@ export class LocalThreadMessageQueueStore implements IMessageQueueStore {
|
|
|
254
254
|
|
|
255
255
|
async getThreadsWithPendingMessages(): Promise<ThreadInfo[]> {
|
|
256
256
|
const rows = this.db.prepare(
|
|
257
|
-
`SELECT tenant_id, assistant_id, thread_id, workspace_id, project_id
|
|
258
|
-
|
|
257
|
+
`SELECT tenant_id, assistant_id, thread_id, workspace_id, project_id,
|
|
258
|
+
MAX(status = 'pending') AS has_pending
|
|
259
|
+
FROM lt_thread_message_queue
|
|
260
|
+
WHERE status IN ('pending', 'processing')
|
|
261
|
+
GROUP BY tenant_id, assistant_id, thread_id, workspace_id, project_id
|
|
262
|
+
ORDER BY thread_id`,
|
|
263
|
+
).all() as { tenant_id: string; assistant_id: string; thread_id: string; workspace_id: string | null; project_id: string | null; has_pending: number }[];
|
|
259
264
|
return rows.map((r) => ({
|
|
260
265
|
tenantId: r.tenant_id,
|
|
261
266
|
assistantId: r.assistant_id,
|
|
262
267
|
threadId: r.thread_id,
|
|
263
268
|
workspaceId: r.workspace_id,
|
|
264
269
|
projectId: r.project_id,
|
|
270
|
+
hasPending: r.has_pending === 1,
|
|
265
271
|
}));
|
|
266
272
|
}
|
|
267
273
|
|