@axiom-lattice/local-stores 5.2.3 → 5.3.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 +34 -0
- package/dist/index.d.mts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +105 -53
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +57 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/LocalOpenAuditStore.test.ts +43 -0
- package/src/__tests__/LocalThreadMessageQueueStore.monitor.test.ts +31 -0
- package/src/__tests__/LocalThreadStore.monitor.test.ts +29 -0
- package/src/stores/LocalOpenAuditStore.ts +30 -1
- package/src/stores/LocalThreadMessageQueueStore.ts +3 -2
- package/src/stores/LocalThreadStore.ts +47 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axiom-lattice/local-stores",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.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": "13.0
|
|
29
|
-
"@axiom-lattice/protocols": "7.
|
|
28
|
+
"@axiom-lattice/core": "13.1.0",
|
|
29
|
+
"@axiom-lattice/protocols": "7.7.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/jest": "^29.5.14",
|
|
@@ -114,3 +114,46 @@ describe("LocalOpenAuditStore.query", () => {
|
|
|
114
114
|
expect(limited).toHaveLength(1);
|
|
115
115
|
});
|
|
116
116
|
});
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
describe("LocalOpenAuditStore.get and filters", () => {
|
|
120
|
+
let store: LocalOpenAuditStore;
|
|
121
|
+
let db: DatabaseWrapper;
|
|
122
|
+
|
|
123
|
+
beforeEach(async () => {
|
|
124
|
+
db = await initDatabase({ dbPath: nextDbPath() });
|
|
125
|
+
store = new LocalOpenAuditStore(db);
|
|
126
|
+
await store.append({
|
|
127
|
+
tenantId: "t1",
|
|
128
|
+
credentialId: "k1",
|
|
129
|
+
credentialKind: "api_key",
|
|
130
|
+
projectId: "p1",
|
|
131
|
+
domain: "sandbox",
|
|
132
|
+
action: "shell_exec",
|
|
133
|
+
args: {},
|
|
134
|
+
status: "cancelled",
|
|
135
|
+
durationMs: 1,
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
afterEach(() => closeDatabase());
|
|
140
|
+
|
|
141
|
+
it("gets a record by tenant and id and rejects another tenant", async () => {
|
|
142
|
+
const [row] = await store.query({ tenantId: "t1" });
|
|
143
|
+
expect(await store.get("t1", row.id)).toMatchObject({
|
|
144
|
+
id: row.id,
|
|
145
|
+
status: "cancelled",
|
|
146
|
+
});
|
|
147
|
+
expect(await store.get("t2", row.id)).toBeNull();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("filters by project, action and cancelled status", async () => {
|
|
151
|
+
const rows = await store.query({
|
|
152
|
+
tenantId: "t1",
|
|
153
|
+
projectId: "p1",
|
|
154
|
+
action: "shell_exec",
|
|
155
|
+
status: "cancelled",
|
|
156
|
+
});
|
|
157
|
+
expect(rows).toHaveLength(1);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { tmpdir } from "os";
|
|
3
|
+
import { closeDatabase, initDatabase } from "../database";
|
|
4
|
+
import { LocalThreadMessageQueueStore } from "../stores/LocalThreadMessageQueueStore";
|
|
5
|
+
|
|
6
|
+
describe("LocalThreadMessageQueueStore tenant filter", () => {
|
|
7
|
+
it("filters pending threads by tenant when asked", async () => {
|
|
8
|
+
const dbPath = path.join(
|
|
9
|
+
tmpdir(),
|
|
10
|
+
`lt-queue-monitor-${Date.now()}-${Math.random().toString(36).slice(2)}.db`,
|
|
11
|
+
);
|
|
12
|
+
const db = await initDatabase({ dbPath });
|
|
13
|
+
const store = new LocalThreadMessageQueueStore(db);
|
|
14
|
+
await store.addMessage({
|
|
15
|
+
threadId: "thread-a",
|
|
16
|
+
tenantId: "tenant-a",
|
|
17
|
+
assistantId: "agent-1",
|
|
18
|
+
content: "a",
|
|
19
|
+
});
|
|
20
|
+
await store.addMessage({
|
|
21
|
+
threadId: "thread-b",
|
|
22
|
+
tenantId: "tenant-b",
|
|
23
|
+
assistantId: "agent-1",
|
|
24
|
+
content: "b",
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const rows = await store.getThreadsWithPendingMessages("tenant-a");
|
|
28
|
+
expect(rows.map((row) => row.threadId)).toEqual(["thread-a"]);
|
|
29
|
+
closeDatabase();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { tmpdir } from "os";
|
|
3
|
+
import { closeDatabase, initDatabase } from "../database";
|
|
4
|
+
import { LocalThreadStore } from "../stores/LocalThreadStore";
|
|
5
|
+
|
|
6
|
+
describe("LocalThreadStore.listThreadsByTenant", () => {
|
|
7
|
+
it("lists the tenant's threads with kind filtering", async () => {
|
|
8
|
+
const dbPath = path.join(
|
|
9
|
+
tmpdir(),
|
|
10
|
+
`lt-threads-monitor-${Date.now()}-${Math.random().toString(36).slice(2)}.db`,
|
|
11
|
+
);
|
|
12
|
+
const db = await initDatabase({ dbPath });
|
|
13
|
+
const store = new LocalThreadStore(db);
|
|
14
|
+
await store.createThread("tenant-1", "agent-1", "t-chat", { metadata: {} });
|
|
15
|
+
await store.createThread("tenant-1", "agent-1", "t-room", {
|
|
16
|
+
metadata: { source: "project_room", kind: "room_presence" },
|
|
17
|
+
});
|
|
18
|
+
await store.createThread("tenant-2", "agent-1", "t-other", { metadata: {} });
|
|
19
|
+
|
|
20
|
+
const all = await store.listThreadsByTenant("tenant-1");
|
|
21
|
+
expect(all.total).toBe(2);
|
|
22
|
+
const rooms = await store.listThreadsByTenant("tenant-1", {
|
|
23
|
+
kind: "room_presence",
|
|
24
|
+
});
|
|
25
|
+
expect(rooms.records).toHaveLength(1);
|
|
26
|
+
expect(rooms.records[0].id).toBe("t-room");
|
|
27
|
+
closeDatabase();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -37,6 +37,27 @@ export class LocalOpenAuditStore implements OpenAuditStore {
|
|
|
37
37
|
ensureTable(db, DDL);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
async get(tenantId: string, id: string): Promise<OpenAuditRecord | null> {
|
|
41
|
+
const row = this.db
|
|
42
|
+
.prepare("SELECT * FROM lt_open_audit WHERE tenant_id = ? AND id = ?")
|
|
43
|
+
.get(tenantId, id) as Record<string, unknown> | undefined;
|
|
44
|
+
if (!row) return null;
|
|
45
|
+
return {
|
|
46
|
+
id: row.id as string,
|
|
47
|
+
tenantId: row.tenant_id as string,
|
|
48
|
+
credentialId: row.credential_id as string,
|
|
49
|
+
credentialKind: row.credential_kind as OpenAuditRecord["credentialKind"],
|
|
50
|
+
projectId: row.project_id as string,
|
|
51
|
+
domain: row.domain as string,
|
|
52
|
+
action: row.action as string,
|
|
53
|
+
args: row.args ? JSON.parse(row.args as string) : undefined,
|
|
54
|
+
status: row.status as OpenAuditRecord["status"],
|
|
55
|
+
errorCode: (row.error_code as string) ?? undefined,
|
|
56
|
+
durationMs: row.duration_ms as number,
|
|
57
|
+
createdAt: parseISO(row.created_at as string),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
40
61
|
async query(params: OpenAuditQuery): Promise<OpenAuditRecord[]> {
|
|
41
62
|
const clauses = ["tenant_id = ?"];
|
|
42
63
|
const values: unknown[] = [params.tenantId];
|
|
@@ -44,10 +65,18 @@ export class LocalOpenAuditStore implements OpenAuditStore {
|
|
|
44
65
|
clauses.push("credential_id = ?");
|
|
45
66
|
values.push(params.credentialId);
|
|
46
67
|
}
|
|
68
|
+
if (params.projectId) {
|
|
69
|
+
clauses.push("project_id = ?");
|
|
70
|
+
values.push(params.projectId);
|
|
71
|
+
}
|
|
47
72
|
if (params.domain) {
|
|
48
73
|
clauses.push("domain = ?");
|
|
49
74
|
values.push(params.domain);
|
|
50
75
|
}
|
|
76
|
+
if (params.action) {
|
|
77
|
+
clauses.push("action = ?");
|
|
78
|
+
values.push(params.action);
|
|
79
|
+
}
|
|
51
80
|
if (params.status) {
|
|
52
81
|
clauses.push("status = ?");
|
|
53
82
|
values.push(params.status);
|
|
@@ -78,7 +107,7 @@ export class LocalOpenAuditStore implements OpenAuditStore {
|
|
|
78
107
|
domain: row.domain as string,
|
|
79
108
|
action: row.action as string,
|
|
80
109
|
args: row.args ? JSON.parse(row.args as string) : undefined,
|
|
81
|
-
status: row.status as "
|
|
110
|
+
status: row.status as OpenAuditRecord["status"],
|
|
82
111
|
errorCode: (row.error_code as string) ?? undefined,
|
|
83
112
|
durationMs: row.duration_ms as number,
|
|
84
113
|
createdAt: parseISO(row.created_at as string),
|
|
@@ -252,15 +252,16 @@ export class LocalThreadMessageQueueStore implements IMessageQueueStore {
|
|
|
252
252
|
return row.count;
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
-
async getThreadsWithPendingMessages(): Promise<ThreadInfo[]> {
|
|
255
|
+
async getThreadsWithPendingMessages(tenantId?: string): Promise<ThreadInfo[]> {
|
|
256
256
|
const rows = this.db.prepare(
|
|
257
257
|
`SELECT tenant_id, assistant_id, thread_id, workspace_id, project_id,
|
|
258
258
|
MAX(status = 'pending') AS has_pending
|
|
259
259
|
FROM lt_thread_message_queue
|
|
260
260
|
WHERE status IN ('pending', 'processing')
|
|
261
|
+
${tenantId ? "AND tenant_id = ?" : ""}
|
|
261
262
|
GROUP BY tenant_id, assistant_id, thread_id, workspace_id, project_id
|
|
262
263
|
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 }[];
|
|
264
|
+
).all(...(tenantId ? [tenantId] : [])) as { tenant_id: string; assistant_id: string; thread_id: string; workspace_id: string | null; project_id: string | null; has_pending: number }[];
|
|
264
265
|
return rows.map((r) => ({
|
|
265
266
|
tenantId: r.tenant_id,
|
|
266
267
|
assistantId: r.assistant_id,
|
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { DatabaseWrapper } from "../database";
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
import {
|
|
7
|
+
classifyThreadMetadata,
|
|
8
|
+
toThreadMonitorRecord,
|
|
9
|
+
type ThreadStore,
|
|
10
|
+
type Thread,
|
|
11
|
+
type CreateThreadRequest,
|
|
12
|
+
type ThreadMonitorQuery,
|
|
13
|
+
type ThreadMonitorRecord,
|
|
10
14
|
} from "@axiom-lattice/protocols";
|
|
11
15
|
import { nowISO, parseISO } from "../database";
|
|
12
16
|
import { MigrationManager } from "../migrations/migration";
|
|
@@ -82,6 +86,45 @@ export class LocalThreadStore implements ThreadStore {
|
|
|
82
86
|
return threads;
|
|
83
87
|
}
|
|
84
88
|
|
|
89
|
+
async listThreadsByTenant(
|
|
90
|
+
tenantId: string,
|
|
91
|
+
query: ThreadMonitorQuery = {},
|
|
92
|
+
): Promise<{ records: ThreadMonitorRecord[]; total: number }> {
|
|
93
|
+
const limit = Math.min(200, Math.max(1, Math.floor(query.limit ?? 50)));
|
|
94
|
+
const offset = Math.max(0, Math.floor(query.offset ?? 0));
|
|
95
|
+
const createdAfter = query.createdAfter
|
|
96
|
+
? Date.parse(query.createdAfter)
|
|
97
|
+
: undefined;
|
|
98
|
+
|
|
99
|
+
const rows = this.db.prepare(
|
|
100
|
+
`SELECT * FROM lt_threads WHERE tenant_id = ? ORDER BY created_at DESC, id DESC`,
|
|
101
|
+
).all(tenantId) as unknown as ThreadRow[];
|
|
102
|
+
|
|
103
|
+
const filtered = rows
|
|
104
|
+
.map(mapRowToThread)
|
|
105
|
+
.filter((thread) => {
|
|
106
|
+
if (query.assistantId && thread.assistantId !== query.assistantId) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
createdAfter !== undefined &&
|
|
111
|
+
Number.isFinite(createdAfter) &&
|
|
112
|
+
thread.createdAt.getTime() < createdAfter
|
|
113
|
+
) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
if (query.kind && classifyThreadMetadata(thread.metadata) !== query.kind) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
records: filtered.slice(offset, offset + limit).map(toThreadMonitorRecord),
|
|
124
|
+
total: filtered.length,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
85
128
|
async getThreadById(tenantId: string, threadId: string): Promise<Thread | undefined> {
|
|
86
129
|
const row = this.db.prepare(
|
|
87
130
|
`SELECT * FROM lt_threads WHERE tenant_id = ? AND id = ?`,
|