@axiom-lattice/pg-stores 3.1.12 → 3.1.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axiom-lattice/pg-stores",
3
- "version": "3.1.12",
3
+ "version": "3.1.14",
4
4
  "description": "PG stores implementation for Axiom Lattice framework",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -25,8 +25,8 @@
25
25
  "@langchain/core": "1.1.30",
26
26
  "pg": "^8.16.3",
27
27
  "uuid": "^9.0.1",
28
- "@axiom-lattice/core": "4.2.12",
29
- "@axiom-lattice/protocols": "4.2.3"
28
+ "@axiom-lattice/core": "4.2.14",
29
+ "@axiom-lattice/protocols": "4.3.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/jest": "^29.5.14",
@@ -288,4 +288,22 @@ describe("PostgreSQLAgentWebAppStore", () => {
288
288
  "Invalid agent web app row",
289
289
  );
290
290
  });
291
+
292
+ it("round-trips appearance quickPrompts and rejects malformed entries", async () => {
293
+ const appearance = {
294
+ quickPrompts: [
295
+ { label: "Pricing", text: "How much does it cost?" },
296
+ { label: "Support", text: "How do I contact support?" },
297
+ ],
298
+ };
299
+ const store = new PostgreSQLAgentWebAppStore({ pool });
300
+
301
+ mockQuery.mockResolvedValueOnce({ rows: [{ ...row, appearance }] });
302
+ await expect(store.getById("tenant-1", "webapp_123")).resolves.toMatchObject({ appearance });
303
+
304
+ mockQuery.mockResolvedValueOnce({ rows: [{ ...row, appearance: { quickPrompts: [{ label: "missing text" }] } }] });
305
+ await expect(store.getById("tenant-1", "webapp_123")).rejects.toThrow(
306
+ "Invalid agent web app row",
307
+ );
308
+ });
291
309
  });
@@ -114,4 +114,29 @@ describe("PostgreSQLProjectRoomMessageStore", () => {
114
114
  );
115
115
  warn.mockRestore();
116
116
  });
117
+
118
+ it("deletes one bounded batch of old chat messages and returns the row count", async () => {
119
+ query.mockResolvedValueOnce({ rows: [], rowCount: 3 });
120
+ const store = new PostgreSQLProjectRoomMessageStore({ pool });
121
+
122
+ await expect(store.deleteChatOlderThan({ before: now, limit: 500 })).resolves.toBe(3);
123
+
124
+ const [sql, params] = query.mock.calls[0] as [string, unknown[]];
125
+ expect(sql).toContain("DELETE FROM lattice_project_room_messages");
126
+ expect(sql).toContain("ctid IN");
127
+ expect(sql).toContain("source IN ('user','agent')");
128
+ expect(sql).toContain("ORDER BY created_at ASC");
129
+ expect(params).toEqual([now, 500]);
130
+ });
131
+
132
+ it("rejects an invalid retention horizon and clamps the delete batch", async () => {
133
+ const store = new PostgreSQLProjectRoomMessageStore({ pool });
134
+
135
+ await expect(store.deleteChatOlderThan({ before: new Date("invalid"), limit: 10 }))
136
+ .rejects.toBeInstanceOf(RangeError);
137
+
138
+ query.mockResolvedValueOnce({ rows: [], rowCount: 0 });
139
+ await store.deleteChatOlderThan({ before: now, limit: 99_999 });
140
+ expect(query.mock.calls[0][1]).toEqual([now, 1_000]);
141
+ });
117
142
  });
@@ -87,6 +87,7 @@ import { createAgentWebAppsTable } from "./migrations/agent_web_apps_migration";
87
87
  import { addAgentWebAppsIdentity } from "./migrations/add_agent_web_apps_identity_migration";
88
88
  import { createCapabilityBundlesTable } from "./migrations/capability_bundle_migration";
89
89
  import { createProjectRoomReadStates, createProjectRoomTables } from "./migrations/project_room_migration";
90
+ import { addProjectRoomMessageRetentionIndex } from "./migrations/project_room_message_retention_migration";
90
91
  import { createUserPushSubscriptions } from "./migrations/user_push_subscription_migration";
91
92
  import { PostgreSQLCapabilityBundleStore } from "./stores/PostgreSQLCapabilityBundleStore";
92
93
  import { PostgreSQLProjectRoomStore } from "./stores/PostgreSQLProjectRoomStore";
@@ -167,6 +168,7 @@ export async function createPgStoreConfig(connectionString: string) {
167
168
  mm.register(addAgentWebAppsIdentity); // v176
168
169
  mm.register(createProjectRoomReadStates); // v177
169
170
  mm.register(createUserPushSubscriptions); // v178
171
+ mm.register(addProjectRoomMessageRetentionIndex); // v179
170
172
 
171
173
  await mm.migrate();
172
174
 
@@ -0,0 +1,23 @@
1
+ import type { Migration } from "./migration";
2
+
3
+ /**
4
+ * Adds a partial `created_at` index for the room chat-message retention sweep.
5
+ *
6
+ * The maintenance job deletes `source IN ('user','agent')` rows older than a horizon;
7
+ * the partial index keeps that global (cross-room) scan bounded without affecting
8
+ * task/system event rows.
9
+ */
10
+ export const addProjectRoomMessageRetentionIndex: Migration = {
11
+ version: 179,
12
+ name: "add_project_room_message_retention_index",
13
+ up: async (client) => {
14
+ await client.query(`
15
+ CREATE INDEX IF NOT EXISTS idx_lattice_project_room_messages_chat_created
16
+ ON lattice_project_room_messages (created_at)
17
+ WHERE source IN ('user', 'agent')
18
+ `);
19
+ },
20
+ down: async (client) => {
21
+ await client.query("DROP INDEX IF EXISTS idx_lattice_project_room_messages_chat_created");
22
+ },
23
+ };
@@ -3,6 +3,7 @@ import type {
3
3
  AgentWebApp,
4
4
  AgentWebAppAppearance,
5
5
  AgentWebAppFeatures,
6
+ AgentWebAppQuickPrompt,
6
7
  AgentWebAppScope,
7
8
  AgentWebAppStatus,
8
9
  AgentWebAppStore,
@@ -70,11 +71,19 @@ function isFeatures(value: unknown): value is AgentWebAppFeatures {
70
71
  && (value.files === undefined || typeof value.files === "boolean");
71
72
  }
72
73
 
74
+ function isQuickPrompt(value: unknown): value is AgentWebAppQuickPrompt {
75
+ return isRecord(value)
76
+ && typeof value.label === "string"
77
+ && typeof value.text === "string";
78
+ }
79
+
73
80
  function isAppearance(value: unknown): value is AgentWebAppAppearance {
74
81
  return isRecord(value)
75
82
  && (value.title === undefined || typeof value.title === "string")
76
83
  && (value.welcomeMessage === undefined || typeof value.welcomeMessage === "string")
77
- && (value.primaryColor === undefined || typeof value.primaryColor === "string");
84
+ && (value.primaryColor === undefined || typeof value.primaryColor === "string")
85
+ && (value.quickPrompts === undefined
86
+ || (Array.isArray(value.quickPrompts) && value.quickPrompts.every(isQuickPrompt)));
78
87
  }
79
88
 
80
89
  function isIdentity(value: unknown): boolean {
@@ -205,6 +205,31 @@ export class PostgreSQLProjectRoomMessageStore implements ProjectRoomMessageStor
205
205
  return result.rows[0]?.count ?? 0;
206
206
  }
207
207
 
208
+ /**
209
+ * Deletes one bounded batch of chat messages older than a horizon.
210
+ *
211
+ * @param input Exclusive upper time bound and requested batch size (clamped to 1..1000).
212
+ * @returns The number of deleted rows.
213
+ * @remarks Only `source IN ('user','agent')` rows are eligible; task/system events are retained.
214
+ */
215
+ async deleteChatOlderThan(input: { before: Date; limit: number }): Promise<number> {
216
+ if (!(input.before instanceof Date) || Number.isNaN(input.before.getTime())) {
217
+ throw new RangeError("Project room message retention horizon date is invalid");
218
+ }
219
+ const limit = Math.min(1_000, Math.max(1, Math.trunc(Number.isFinite(input.limit) ? input.limit : 1)));
220
+ const result = await this.pool.query(
221
+ `DELETE FROM lattice_project_room_messages
222
+ WHERE ctid IN (
223
+ SELECT ctid FROM lattice_project_room_messages
224
+ WHERE created_at < $1 AND source IN ('user','agent')
225
+ ORDER BY created_at ASC
226
+ LIMIT $2
227
+ )`,
228
+ [input.before, limit],
229
+ );
230
+ return result.rowCount ?? 0;
231
+ }
232
+
208
233
  private parameters(input: Omit<ProjectRoomMessage, "createdAt">): unknown[] {
209
234
  return [input.id, input.tenantId, input.workspaceId, input.projectId, input.roomId, JSON.stringify(input.author), JSON.stringify(input.content),
210
235
  JSON.stringify(input.mentions), input.replyToMessageId ?? null, input.source, input.sourceId ?? null, input.idempotencyKey ?? null];