@axiom-lattice/local-stores 5.4.1 → 5.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.
- package/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +120 -0
- package/dist/index.d.mts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +52 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +52 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/LocalChannelBindingStore.test.ts +19 -0
- package/src/__tests__/LocalChannelInstallationStore.test.ts +55 -0
- package/src/__tests__/LocalThreadStore.test.ts +58 -1
- package/src/stores/LocalChannelInstallationStore.ts +24 -2
- package/src/stores/LocalThreadStore.ts +34 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axiom-lattice/local-stores",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.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.
|
|
29
|
-
"@axiom-lattice/protocols": "7.
|
|
28
|
+
"@axiom-lattice/core": "13.4.0",
|
|
29
|
+
"@axiom-lattice/protocols": "7.9.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/jest": "^29.5.14",
|
|
@@ -124,6 +124,25 @@ describe("LocalChannelBindingStore protected bindings", () => {
|
|
|
124
124
|
await expect(store.findById("tenant-a", created.id)).resolves.toBeNull();
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
+
it("clears workspace and project scope when updated with empty strings", async () => {
|
|
128
|
+
db = await initDatabase({ dbPath: dbPath() });
|
|
129
|
+
const store = new LocalChannelBindingStore(db);
|
|
130
|
+
const created = await store.create(input({
|
|
131
|
+
channel: "lark", workspaceId: "workspace-1", projectId: "project-1",
|
|
132
|
+
}));
|
|
133
|
+
expect(created.workspaceId).toBe("workspace-1");
|
|
134
|
+
|
|
135
|
+
const cleared = await store.update("tenant-a", created.id, {
|
|
136
|
+
workspaceId: "", projectId: "",
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
expect(cleared.workspaceId).toBeUndefined();
|
|
140
|
+
expect(cleared.projectId).toBeUndefined();
|
|
141
|
+
await expect(store.findById("tenant-a", created.id)).resolves.toMatchObject({
|
|
142
|
+
workspaceId: undefined, projectId: undefined,
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
127
146
|
it("does not rollback after commit when persistence fails", () => {
|
|
128
147
|
const saveError = new Error("disk full");
|
|
129
148
|
const exec = jest.fn();
|
|
@@ -29,6 +29,61 @@ describe("LocalChannelInstallationStore", () => {
|
|
|
29
29
|
});
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
it("persists and updates the fallback execution scope", async () => {
|
|
33
|
+
const created = await store.createInstallation("tenant-a", "install-1", {
|
|
34
|
+
channel: "wechat",
|
|
35
|
+
config: { botToken: "token-1" },
|
|
36
|
+
fallbackAgentId: "pa-1",
|
|
37
|
+
workspaceId: "workspace-1",
|
|
38
|
+
projectId: "project-1",
|
|
39
|
+
});
|
|
40
|
+
expect(created).toMatchObject({
|
|
41
|
+
workspaceId: "workspace-1",
|
|
42
|
+
projectId: "project-1",
|
|
43
|
+
});
|
|
44
|
+
expect(await store.getInstallationById("install-1")).toMatchObject({
|
|
45
|
+
workspaceId: "workspace-1",
|
|
46
|
+
projectId: "project-1",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const updated = await store.updateInstallation("tenant-a", "install-1", {
|
|
50
|
+
projectId: "project-2",
|
|
51
|
+
});
|
|
52
|
+
expect(updated).toMatchObject({
|
|
53
|
+
workspaceId: "workspace-1",
|
|
54
|
+
projectId: "project-2",
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("clears the fallback agent on update", async () => {
|
|
59
|
+
const store = new LocalChannelInstallationStore(db);
|
|
60
|
+
await store.createInstallation("tenant-a", "install-1", {
|
|
61
|
+
channel: "lark",
|
|
62
|
+
config: { appId: "a", appSecret: "s" },
|
|
63
|
+
fallbackAgentId: "agent-1",
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const updated = await store.updateInstallation("tenant-a", "install-1", {
|
|
67
|
+
fallbackAgentId: null,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(updated?.fallbackAgentId).toBeUndefined();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("clears the fallback agent when update sends an empty string", async () => {
|
|
74
|
+
await store.createInstallation("tenant-a", "install-1", {
|
|
75
|
+
channel: "lark",
|
|
76
|
+
config: { appId: "a", appSecret: "s" },
|
|
77
|
+
fallbackAgentId: "agent-1",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const updated = await store.updateInstallation("tenant-a", "install-1", {
|
|
81
|
+
fallbackAgentId: "",
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(updated?.fallbackAgentId).toBeUndefined();
|
|
85
|
+
});
|
|
86
|
+
|
|
32
87
|
it("rejects a duplicate id without changing the original installation", async () => {
|
|
33
88
|
const original = await store.createInstallation("tenant-a", "install-1", {
|
|
34
89
|
channel: "lark",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it } from "@jest/globals";
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
2
2
|
import { tmpdir } from "os";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { closeDatabase, initDatabase } from "../database";
|
|
@@ -48,4 +48,61 @@ describe("LocalThreadStore TTL", () => {
|
|
|
48
48
|
expect((await store.listExpiredThreads(now, 1)).map((thread) => thread.id))
|
|
49
49
|
.toEqual(["earlier"]);
|
|
50
50
|
});
|
|
51
|
+
|
|
52
|
+
it("lists recent channel senders grouped by sender", async () => {
|
|
53
|
+
jest.useFakeTimers();
|
|
54
|
+
try {
|
|
55
|
+
jest.setSystemTime(new Date("2026-01-01T00:00:00.000Z"));
|
|
56
|
+
await store.createThread("t1", "a1", "thread-1", {
|
|
57
|
+
metadata: {
|
|
58
|
+
channel: "lark",
|
|
59
|
+
channelInstallationId: "i1",
|
|
60
|
+
senderId: "s1",
|
|
61
|
+
senderDisplayName: "Alice",
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
jest.setSystemTime(new Date("2026-01-01T00:01:00.000Z"));
|
|
65
|
+
await store.createThread("t1", "a1", "thread-2", {
|
|
66
|
+
metadata: {
|
|
67
|
+
channel: "lark",
|
|
68
|
+
channelInstallationId: "i1",
|
|
69
|
+
senderId: "s2",
|
|
70
|
+
senderDisplayName: "Bob",
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
jest.setSystemTime(new Date("2026-01-01T00:02:00.000Z"));
|
|
74
|
+
await store.createThread("t1", "a1", "thread-3", {
|
|
75
|
+
metadata: { channel: "lark", channelInstallationId: "i1", senderId: "s1" },
|
|
76
|
+
});
|
|
77
|
+
await store.createThread("t1", "a1", "thread-4", {
|
|
78
|
+
metadata: { channel: "lark", channelInstallationId: "i2", senderId: "s3" },
|
|
79
|
+
});
|
|
80
|
+
await store.createThread("t2", "a1", "thread-5", {
|
|
81
|
+
metadata: { channel: "lark", channelInstallationId: "i1", senderId: "s4" },
|
|
82
|
+
});
|
|
83
|
+
await store.createThread("t1", "a1", "thread-6", {
|
|
84
|
+
metadata: { channel: "lark", channelInstallationId: "i1", senderId: "" },
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const senders = await store.listChannelSenders("t1", "i1", 10);
|
|
88
|
+
expect(senders).toEqual([
|
|
89
|
+
{
|
|
90
|
+
senderId: "s1",
|
|
91
|
+
lastSeenAt: new Date("2026-01-01T00:02:00.000Z"),
|
|
92
|
+
lastThreadId: "thread-3",
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
senderId: "s2",
|
|
96
|
+
displayName: "Bob",
|
|
97
|
+
lastSeenAt: new Date("2026-01-01T00:01:00.000Z"),
|
|
98
|
+
lastThreadId: "thread-2",
|
|
99
|
+
},
|
|
100
|
+
]);
|
|
101
|
+
|
|
102
|
+
expect(await store.listChannelSenders("t1", "i1", 0)).toHaveLength(1);
|
|
103
|
+
expect(await store.listChannelSenders("t1", "i1", 1000)).toHaveLength(2);
|
|
104
|
+
} finally {
|
|
105
|
+
jest.useRealTimers();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
51
108
|
});
|
|
@@ -21,6 +21,8 @@ CREATE TABLE IF NOT EXISTS lt_channel_installations (
|
|
|
21
21
|
config TEXT NOT NULL,
|
|
22
22
|
enabled INTEGER NOT NULL DEFAULT 1,
|
|
23
23
|
fallback_agent_id TEXT,
|
|
24
|
+
workspace_id TEXT,
|
|
25
|
+
project_id TEXT,
|
|
24
26
|
reject_when_no_binding INTEGER NOT NULL DEFAULT 0,
|
|
25
27
|
created_at TEXT NOT NULL,
|
|
26
28
|
updated_at TEXT NOT NULL
|
|
@@ -36,6 +38,8 @@ interface Row {
|
|
|
36
38
|
config: string;
|
|
37
39
|
enabled: number;
|
|
38
40
|
fallback_agent_id: string | null;
|
|
41
|
+
workspace_id: string | null;
|
|
42
|
+
project_id: string | null;
|
|
39
43
|
reject_when_no_binding: number;
|
|
40
44
|
created_at: string;
|
|
41
45
|
updated_at: string;
|
|
@@ -47,6 +51,8 @@ export class LocalChannelInstallationStore implements ChannelInstallationStore {
|
|
|
47
51
|
constructor(db: DatabaseWrapper) {
|
|
48
52
|
this.db = db;
|
|
49
53
|
ensureTable(db, DDL);
|
|
54
|
+
this.ensureColumn("lt_channel_installations", "workspace_id", "workspace_id TEXT");
|
|
55
|
+
this.ensureColumn("lt_channel_installations", "project_id", "project_id TEXT");
|
|
50
56
|
}
|
|
51
57
|
|
|
52
58
|
async getInstallationById(installationId: string): Promise<ChannelInstallation | null> {
|
|
@@ -98,13 +104,15 @@ export class LocalChannelInstallationStore implements ChannelInstallationStore {
|
|
|
98
104
|
|
|
99
105
|
this.db.prepare(
|
|
100
106
|
`INSERT INTO lt_channel_installations
|
|
101
|
-
(id, tenant_id, channel, name, config, enabled, fallback_agent_id, reject_when_no_binding, created_at, updated_at)
|
|
102
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
107
|
+
(id, tenant_id, channel, name, config, enabled, fallback_agent_id, workspace_id, project_id, reject_when_no_binding, created_at, updated_at)
|
|
108
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
103
109
|
).run(
|
|
104
110
|
installationId, tenantId, data.channel,
|
|
105
111
|
data.name || null, JSON.stringify(data.config),
|
|
106
112
|
data.enabled !== false ? 1 : 0,
|
|
107
113
|
data.fallbackAgentId || null,
|
|
114
|
+
data.workspaceId || null,
|
|
115
|
+
data.projectId || null,
|
|
108
116
|
data.rejectWhenNoBinding ? 1 : 0,
|
|
109
117
|
now, now,
|
|
110
118
|
);
|
|
@@ -117,6 +125,8 @@ export class LocalChannelInstallationStore implements ChannelInstallationStore {
|
|
|
117
125
|
config: { ...data.config },
|
|
118
126
|
enabled: data.enabled !== false,
|
|
119
127
|
fallbackAgentId: data.fallbackAgentId,
|
|
128
|
+
workspaceId: data.workspaceId,
|
|
129
|
+
projectId: data.projectId,
|
|
120
130
|
rejectWhenNoBinding: data.rejectWhenNoBinding ?? false,
|
|
121
131
|
createdAt: parseISO(now),
|
|
122
132
|
updatedAt: parseISO(now),
|
|
@@ -138,6 +148,8 @@ export class LocalChannelInstallationStore implements ChannelInstallationStore {
|
|
|
138
148
|
if (updates.config !== undefined) { setClauses.push("config = ?"); values.push(JSON.stringify(updates.config)); }
|
|
139
149
|
if (updates.enabled !== undefined) { setClauses.push("enabled = ?"); values.push(updates.enabled ? 1 : 0); }
|
|
140
150
|
if (updates.fallbackAgentId !== undefined) { setClauses.push("fallback_agent_id = ?"); values.push(updates.fallbackAgentId || null); }
|
|
151
|
+
if (updates.workspaceId !== undefined) { setClauses.push("workspace_id = ?"); values.push(updates.workspaceId || null); }
|
|
152
|
+
if (updates.projectId !== undefined) { setClauses.push("project_id = ?"); values.push(updates.projectId || null); }
|
|
141
153
|
if (updates.rejectWhenNoBinding !== undefined) { setClauses.push("reject_when_no_binding = ?"); values.push(updates.rejectWhenNoBinding ? 1 : 0); }
|
|
142
154
|
|
|
143
155
|
if (setClauses.length === 0) return existing;
|
|
@@ -161,6 +173,14 @@ export class LocalChannelInstallationStore implements ChannelInstallationStore {
|
|
|
161
173
|
).run(installationId, tenantId);
|
|
162
174
|
return result.changes > 0;
|
|
163
175
|
}
|
|
176
|
+
|
|
177
|
+
/** Add a column if it does not exist (SQLite version compatible). */
|
|
178
|
+
private ensureColumn(table: string, column: string, ddl: string): void {
|
|
179
|
+
const cols = this.db.prepare(`PRAGMA table_info(${table})`).all() as unknown as Array<{ name: string }>;
|
|
180
|
+
if (!cols.some((c) => c.name === column)) {
|
|
181
|
+
this.db.exec(`ALTER TABLE ${table} ADD COLUMN ${ddl}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
164
184
|
}
|
|
165
185
|
|
|
166
186
|
function mapRow(row: Row): ChannelInstallation {
|
|
@@ -172,6 +192,8 @@ function mapRow(row: Row): ChannelInstallation {
|
|
|
172
192
|
config: JSON.parse(row.config),
|
|
173
193
|
enabled: row.enabled === 1,
|
|
174
194
|
fallbackAgentId: row.fallback_agent_id || undefined,
|
|
195
|
+
workspaceId: row.workspace_id || undefined,
|
|
196
|
+
projectId: row.project_id || undefined,
|
|
175
197
|
rejectWhenNoBinding: row.reject_when_no_binding === 1,
|
|
176
198
|
createdAt: parseISO(row.created_at),
|
|
177
199
|
updatedAt: parseISO(row.updated_at),
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
type CreateThreadRequest,
|
|
12
12
|
type ThreadMonitorQuery,
|
|
13
13
|
type ThreadMonitorRecord,
|
|
14
|
+
type ChannelSenderSummary,
|
|
14
15
|
} from "@axiom-lattice/protocols";
|
|
15
16
|
import { nowISO, parseISO } from "../database";
|
|
16
17
|
import { MigrationManager } from "../migrations/migration";
|
|
@@ -125,6 +126,39 @@ export class LocalThreadStore implements ThreadStore {
|
|
|
125
126
|
};
|
|
126
127
|
}
|
|
127
128
|
|
|
129
|
+
async listChannelSenders(
|
|
130
|
+
tenantId: string,
|
|
131
|
+
channelInstallationId: string,
|
|
132
|
+
limit: number,
|
|
133
|
+
): Promise<ChannelSenderSummary[]> {
|
|
134
|
+
const rows = this.db.prepare(
|
|
135
|
+
`SELECT * FROM lt_threads
|
|
136
|
+
WHERE tenant_id = ?
|
|
137
|
+
AND json_extract(metadata, '$.channelInstallationId') = ?
|
|
138
|
+
ORDER BY updated_at DESC, id DESC`,
|
|
139
|
+
).all(tenantId, channelInstallationId) as unknown as ThreadRow[];
|
|
140
|
+
const bySender = new Map<string, ChannelSenderSummary>();
|
|
141
|
+
for (const row of rows) {
|
|
142
|
+
const thread = mapRowToThread(row);
|
|
143
|
+
const metadata = thread.metadata ?? {};
|
|
144
|
+
if (typeof metadata.senderId !== "string" || !metadata.senderId) continue;
|
|
145
|
+
if (bySender.has(metadata.senderId)) continue;
|
|
146
|
+
bySender.set(metadata.senderId, {
|
|
147
|
+
senderId: metadata.senderId,
|
|
148
|
+
...(typeof metadata.senderDisplayName === "string" &&
|
|
149
|
+
metadata.senderDisplayName
|
|
150
|
+
? { displayName: metadata.senderDisplayName }
|
|
151
|
+
: {}),
|
|
152
|
+
lastSeenAt: thread.updatedAt,
|
|
153
|
+
lastThreadId: thread.id,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return [...bySender.values()].slice(
|
|
157
|
+
0,
|
|
158
|
+
Math.max(1, Math.min(200, Math.floor(limit))),
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
128
162
|
async getThreadById(tenantId: string, threadId: string): Promise<Thread | undefined> {
|
|
129
163
|
const row = this.db.prepare(
|
|
130
164
|
`SELECT * FROM lt_threads WHERE tenant_id = ? AND id = ?`,
|