@axiom-lattice/pg-stores 1.0.90 → 1.0.93

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/dist/index.mjs CHANGED
@@ -1254,6 +1254,13 @@ var PostgreSQLConnectionStore = class {
1254
1254
  constructor(db) {
1255
1255
  this.db = db;
1256
1256
  }
1257
+ async listByTenant(tenantId) {
1258
+ const result = await this.db.query(
1259
+ `SELECT * FROM ${TABLE} WHERE tenant_id = $1 ORDER BY created_at`,
1260
+ [tenantId]
1261
+ );
1262
+ return result.rows.map((row) => this.mapRow(row));
1263
+ }
1257
1264
  async listByType(tenantId, type) {
1258
1265
  const result = await this.db.query(
1259
1266
  `SELECT * FROM ${TABLE} WHERE tenant_id = $1 AND type = $2 ORDER BY created_at`,
@@ -2942,8 +2949,8 @@ var PostgreSQLTenantStore = class {
2942
2949
  updateValues.push(updates.status);
2943
2950
  }
2944
2951
  if (updates.metadata !== void 0) {
2945
- updateFields.push(`metadata = $${paramIndex++}`);
2946
- updateValues.push(updates.metadata);
2952
+ updateFields.push(`metadata = COALESCE(lattice_tenants.metadata, '{}'::jsonb) || $${paramIndex++}::jsonb`);
2953
+ updateValues.push(JSON.stringify(updates.metadata));
2947
2954
  }
2948
2955
  if (updateFields.length === 0) {
2949
2956
  return existing;
@@ -3660,7 +3667,7 @@ function mapRowToRunStep(row) {
3660
3667
  edgeTo: row.edge_to,
3661
3668
  edgePurpose: row.edge_purpose,
3662
3669
  input: safeParse(row.input, void 0),
3663
- output: safeParse(row.output, void 0),
3670
+ output: typeof row.output === "string" ? safeParse(row.output, row.output) : row.output ?? void 0,
3664
3671
  status: row.status,
3665
3672
  errorMessage: row.error_message,
3666
3673
  startedAt: row.started_at,
@@ -4555,6 +4562,17 @@ var PostgreSQLEvalStore = class {
4555
4562
  );
4556
4563
  return rows.length ? this.mapRowToRunResult(rows[0]) : null;
4557
4564
  }
4565
+ /** Delete a run result by ID */
4566
+ async deleteRunResult(tenantId, id) {
4567
+ await this.ensureInitialized();
4568
+ const result = await this.pool.query(
4569
+ `DELETE FROM lattice_eval_run_results
4570
+ WHERE id = $1
4571
+ AND run_id IN (SELECT id FROM lattice_eval_runs WHERE tenant_id = $2)`,
4572
+ [id, tenantId]
4573
+ );
4574
+ return (result.rowCount ?? 0) > 0;
4575
+ }
4558
4576
  /** Get a single run result by ID with tenant isolation */
4559
4577
  async getRunResultById(tenantId, id) {
4560
4578
  await this.ensureInitialized();
@@ -6344,6 +6362,34 @@ var createTasksTable = {
6344
6362
  await client.query("DROP TABLE IF EXISTS lattice_tasks");
6345
6363
  }
6346
6364
  };
6365
+ var addTaskFieldsMigration = {
6366
+ version: 139,
6367
+ name: "add_task_fields_require_review_deps_result_failure",
6368
+ up: async (client) => {
6369
+ await client.query(`
6370
+ ALTER TABLE lattice_tasks
6371
+ ADD COLUMN IF NOT EXISTS require_review BOOLEAN DEFAULT FALSE,
6372
+ ADD COLUMN IF NOT EXISTS dependencies JSONB,
6373
+ ADD COLUMN IF NOT EXISTS result TEXT,
6374
+ ADD COLUMN IF NOT EXISTS failure_reason TEXT
6375
+ `);
6376
+ }
6377
+ };
6378
+ var addTaskProjectFieldsMigration = {
6379
+ version: 140,
6380
+ name: "add_task_project_workspace_fields",
6381
+ up: async (client) => {
6382
+ await client.query(`
6383
+ ALTER TABLE lattice_tasks
6384
+ ADD COLUMN IF NOT EXISTS workspace_id TEXT,
6385
+ ADD COLUMN IF NOT EXISTS project_id TEXT
6386
+ `);
6387
+ await client.query(`
6388
+ CREATE INDEX IF NOT EXISTS idx_tasks_project
6389
+ ON lattice_tasks (tenant_id, project_id)
6390
+ `);
6391
+ }
6392
+ };
6347
6393
 
6348
6394
  // src/stores/PostgreSQLTaskStore.ts
6349
6395
  import { v4 as uuidv42 } from "uuid";
@@ -6362,6 +6408,12 @@ function mapRowToTask(row) {
6362
6408
  parentId: row.parent_id ?? void 0,
6363
6409
  sourceId: row.source_id ?? void 0,
6364
6410
  context: row.context ?? void 0,
6411
+ requireReview: row.require_review ?? void 0,
6412
+ dependencies: row.dependencies ?? void 0,
6413
+ result: row.result ?? void 0,
6414
+ failureReason: row.failure_reason ?? void 0,
6415
+ workspaceId: row.workspace_id ?? void 0,
6416
+ projectId: row.project_id ?? void 0,
6365
6417
  createdAt: new Date(row.created_at),
6366
6418
  updatedAt: new Date(row.updated_at)
6367
6419
  };
@@ -6386,6 +6438,8 @@ var PostgreSQLTaskStore = class {
6386
6438
  }
6387
6439
  this.migrationManager = new MigrationManager(this.pool);
6388
6440
  this.migrationManager.register(createTasksTable);
6441
+ this.migrationManager.register(addTaskFieldsMigration);
6442
+ this.migrationManager.register(addTaskProjectFieldsMigration);
6389
6443
  if (options.autoMigrate !== false) {
6390
6444
  this.initialize().catch((error) => {
6391
6445
  console.error("Failed to initialize PostgreSQLTaskStore:", error);
@@ -6415,8 +6469,8 @@ var PostgreSQLTaskStore = class {
6415
6469
  const id = uuidv42();
6416
6470
  const now = (/* @__PURE__ */ new Date()).toISOString();
6417
6471
  await this.pool.query(
6418
- `INSERT INTO lattice_tasks (id, tenant_id, owner_type, owner_id, title, description, status, priority, due_date, metadata, parent_id, source_id, context, created_at, updated_at)
6419
- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)`,
6472
+ `INSERT INTO lattice_tasks (id, tenant_id, owner_type, owner_id, title, description, status, priority, due_date, metadata, parent_id, source_id, context, require_review, dependencies, result, failure_reason, workspace_id, project_id, created_at, updated_at)
6473
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)`,
6420
6474
  [
6421
6475
  id,
6422
6476
  params.tenantId,
@@ -6431,6 +6485,12 @@ var PostgreSQLTaskStore = class {
6431
6485
  params.parentId || null,
6432
6486
  params.sourceId || null,
6433
6487
  params.context ? JSON.stringify(params.context) : null,
6488
+ params.requireReview ?? null,
6489
+ params.dependencies ? JSON.stringify(params.dependencies) : null,
6490
+ params.result ?? null,
6491
+ params.failureReason ?? null,
6492
+ params.workspaceId || null,
6493
+ params.projectId || null,
6434
6494
  now,
6435
6495
  now
6436
6496
  ]
@@ -6460,13 +6520,24 @@ var PostgreSQLTaskStore = class {
6460
6520
  params.push(filter.ownerId);
6461
6521
  }
6462
6522
  if (filter.status) {
6463
- conditions.push(`status = $${paramIndex++}`);
6464
- params.push(filter.status);
6523
+ const statuses = filter.status.split(",").map((s) => s.trim()).filter(Boolean);
6524
+ const placeholders = statuses.map((_, i) => `$${paramIndex + i}`).join(", ");
6525
+ conditions.push(`status IN (${placeholders})`);
6526
+ params.push(...statuses);
6527
+ paramIndex += statuses.length;
6465
6528
  }
6466
6529
  if (filter.priority) {
6467
6530
  conditions.push(`priority = $${paramIndex++}`);
6468
6531
  params.push(filter.priority);
6469
6532
  }
6533
+ if (filter.workspaceId) {
6534
+ conditions.push(`workspace_id = $${paramIndex++}`);
6535
+ params.push(filter.workspaceId);
6536
+ }
6537
+ if (filter.projectId) {
6538
+ conditions.push(`project_id = $${paramIndex++}`);
6539
+ params.push(filter.projectId);
6540
+ }
6470
6541
  if (filter.parentId) {
6471
6542
  conditions.push(`parent_id = $${paramIndex++}`);
6472
6543
  params.push(filter.parentId);
@@ -6544,6 +6615,30 @@ var PostgreSQLTaskStore = class {
6544
6615
  setClauses.push(`owner_id = $${paramIndex++}`);
6545
6616
  params.push(updates.ownerId);
6546
6617
  }
6618
+ if (updates.requireReview !== void 0) {
6619
+ setClauses.push(`require_review = $${paramIndex++}`);
6620
+ params.push(updates.requireReview);
6621
+ }
6622
+ if (updates.dependencies !== void 0) {
6623
+ setClauses.push(`dependencies = $${paramIndex++}`);
6624
+ params.push(JSON.stringify(updates.dependencies));
6625
+ }
6626
+ if (updates.result !== void 0) {
6627
+ setClauses.push(`result = $${paramIndex++}`);
6628
+ params.push(updates.result);
6629
+ }
6630
+ if (updates.failureReason !== void 0) {
6631
+ setClauses.push(`failure_reason = $${paramIndex++}`);
6632
+ params.push(updates.failureReason);
6633
+ }
6634
+ if (updates.workspaceId !== void 0) {
6635
+ setClauses.push(`workspace_id = $${paramIndex++}`);
6636
+ params.push(updates.workspaceId);
6637
+ }
6638
+ if (updates.projectId !== void 0) {
6639
+ setClauses.push(`project_id = $${paramIndex++}`);
6640
+ params.push(updates.projectId);
6641
+ }
6547
6642
  if (setClauses.length === 0) {
6548
6643
  return existing;
6549
6644
  }
@@ -6576,6 +6671,72 @@ var PostgreSQLTaskStore = class {
6576
6671
  }
6577
6672
  };
6578
6673
 
6674
+ // src/stores/PostgreSQLTaskWorkItemStore.ts
6675
+ import { v4 } from "uuid";
6676
+ var PostgreSQLTaskWorkItemStore = class {
6677
+ constructor(pool) {
6678
+ this.pool = pool;
6679
+ }
6680
+ async create(params) {
6681
+ const id = v4();
6682
+ const result = await this.pool.query(
6683
+ `INSERT INTO lattice_task_work_items
6684
+ (id, tenant_id, task_id, action, actor, thread_id, summary, detail, attempt, workspace_id, project_id)
6685
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
6686
+ RETURNING *`,
6687
+ [
6688
+ id,
6689
+ params.tenantId,
6690
+ params.taskId,
6691
+ params.action,
6692
+ params.actor,
6693
+ params.threadId || null,
6694
+ params.summary || null,
6695
+ params.detail ? JSON.stringify(params.detail) : null,
6696
+ params.attempt || null,
6697
+ params.workspaceId || null,
6698
+ params.projectId || null
6699
+ ]
6700
+ );
6701
+ return this.rowToItem(result.rows[0]);
6702
+ }
6703
+ async list(filter) {
6704
+ let query = `SELECT * FROM lattice_task_work_items WHERE tenant_id = $1 AND task_id = $2`;
6705
+ const params = [filter.tenantId, filter.taskId];
6706
+ if (filter.action) {
6707
+ query += ` AND action = $${params.length + 1}`;
6708
+ params.push(filter.action);
6709
+ }
6710
+ query += ` ORDER BY created_at ASC`;
6711
+ if (filter.limit) {
6712
+ query += ` LIMIT $${params.length + 1}`;
6713
+ params.push(filter.limit);
6714
+ }
6715
+ if (filter.offset) {
6716
+ query += ` OFFSET $${params.length + 1}`;
6717
+ params.push(filter.offset);
6718
+ }
6719
+ const result = await this.pool.query(query, params);
6720
+ return result.rows.map((row) => this.rowToItem(row));
6721
+ }
6722
+ rowToItem(row) {
6723
+ return {
6724
+ id: row.id,
6725
+ taskId: row.task_id,
6726
+ tenantId: row.tenant_id,
6727
+ action: row.action,
6728
+ actor: row.actor,
6729
+ threadId: row.thread_id,
6730
+ summary: row.summary,
6731
+ detail: row.detail,
6732
+ attempt: row.attempt,
6733
+ workspaceId: row.workspace_id,
6734
+ projectId: row.project_id,
6735
+ createdAt: new Date(row.created_at)
6736
+ };
6737
+ }
6738
+ };
6739
+
6579
6740
  // src/stores/MenuStore.ts
6580
6741
  import { Pool as Pool19 } from "pg";
6581
6742
 
@@ -7525,6 +7686,52 @@ var createCollectionsTable = {
7525
7686
  }
7526
7687
  };
7527
7688
 
7689
+ // src/migrations/task_work_items_migration.ts
7690
+ var createTaskWorkItemsMigration = {
7691
+ version: 138,
7692
+ name: "create_task_work_items_table",
7693
+ up: async (client) => {
7694
+ await client.query(`
7695
+ CREATE TABLE IF NOT EXISTS lattice_task_work_items (
7696
+ id TEXT NOT NULL,
7697
+ tenant_id TEXT NOT NULL,
7698
+ task_id TEXT NOT NULL,
7699
+ action TEXT NOT NULL,
7700
+ actor TEXT NOT NULL,
7701
+ thread_id TEXT,
7702
+ summary TEXT,
7703
+ detail JSONB,
7704
+ attempt INTEGER,
7705
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
7706
+ PRIMARY KEY (tenant_id, id)
7707
+ )
7708
+ `);
7709
+ await client.query(`
7710
+ CREATE INDEX IF NOT EXISTS idx_task_work_items_task_id
7711
+ ON lattice_task_work_items (tenant_id, task_id)
7712
+ `);
7713
+ await client.query(`
7714
+ CREATE INDEX IF NOT EXISTS idx_task_work_items_action
7715
+ ON lattice_task_work_items (tenant_id, task_id, action)
7716
+ `);
7717
+ }
7718
+ };
7719
+ var addWorkItemProjectFieldsMigration = {
7720
+ version: 140,
7721
+ name: "add_task_work_item_project_fields",
7722
+ up: async (client) => {
7723
+ await client.query(`
7724
+ ALTER TABLE lattice_task_work_items
7725
+ ADD COLUMN IF NOT EXISTS workspace_id TEXT,
7726
+ ADD COLUMN IF NOT EXISTS project_id TEXT
7727
+ `);
7728
+ await client.query(`
7729
+ CREATE INDEX IF NOT EXISTS idx_task_work_items_project
7730
+ ON lattice_task_work_items (tenant_id, project_id)
7731
+ `);
7732
+ }
7733
+ };
7734
+
7528
7735
  // src/createPgStoreConfig.ts
7529
7736
  async function createPgStoreConfig(connectionString) {
7530
7737
  const pool = new Pool23({ connectionString });
@@ -7572,6 +7779,10 @@ async function createPgStoreConfig(connectionString) {
7572
7779
  mm.register(createSharedResourcesTable);
7573
7780
  mm.register(addFileContentType);
7574
7781
  mm.register(createCollectionsTable);
7782
+ mm.register(addTaskFieldsMigration);
7783
+ mm.register(createTaskWorkItemsMigration);
7784
+ mm.register(addTaskProjectFieldsMigration);
7785
+ mm.register(addWorkItemProjectFieldsMigration);
7575
7786
  mm.register(createConnectionConfigsTable);
7576
7787
  mm.register(addWorkflowRunsTenantStatusUpdatedIndex);
7577
7788
  await mm.migrate();
@@ -7580,6 +7791,7 @@ async function createPgStoreConfig(connectionString) {
7580
7791
  console.error("[pg-stores] Failed to setup checkpoint table:", err.message || err);
7581
7792
  });
7582
7793
  const opts = { pool };
7794
+ const taskWorkItemStore = new PostgreSQLTaskWorkItemStore(pool);
7583
7795
  return {
7584
7796
  workspace: new PostgreSQLWorkspaceStore(opts),
7585
7797
  project: new PostgreSQLProjectStore(opts),
@@ -7598,6 +7810,7 @@ async function createPgStoreConfig(connectionString) {
7598
7810
  workflowTracking: new PostgreSQLWorkflowTrackingStore(opts),
7599
7811
  threadMessageQueue: new ThreadMessageQueueStore(opts),
7600
7812
  task: new PostgreSQLTaskStore(opts),
7813
+ taskWorkItem: taskWorkItemStore,
7601
7814
  a2aApiKey: new PostgreSQLA2AApiKeyStore(opts),
7602
7815
  schedule: new PostgreSQLScheduleStorage(opts),
7603
7816
  menu: new MenuStore(opts),