@axiom-lattice/pg-stores 1.0.47 → 1.0.49

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
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { Pool as Pool15 } from "pg";
2
+ import { Pool as Pool16 } from "pg";
3
3
 
4
4
  // src/stores/PostgreSQLThreadStore.ts
5
5
  import { Pool } from "pg";
@@ -4098,6 +4098,79 @@ var createChannelInstallationsTable = {
4098
4098
  }
4099
4099
  };
4100
4100
 
4101
+ // src/migrations/workflow_tracking_migrations.ts
4102
+ var createWorkflowTrackingTables = {
4103
+ name: "create_workflow_tracking_tables",
4104
+ version: 103,
4105
+ up: async (client) => {
4106
+ await client.query(`
4107
+ CREATE TABLE IF NOT EXISTS lattice_workflow_runs (
4108
+ id VARCHAR(255) PRIMARY KEY,
4109
+ tenant_id VARCHAR(255) NOT NULL,
4110
+ assistant_id VARCHAR(255) NOT NULL,
4111
+ thread_id VARCHAR(255) NOT NULL,
4112
+ status VARCHAR(50) NOT NULL DEFAULT 'running',
4113
+ topology_edges JSONB NOT NULL DEFAULT '[]',
4114
+ total_edges INTEGER NOT NULL DEFAULT 0,
4115
+ completed_edges INTEGER NOT NULL DEFAULT 0,
4116
+ error_message TEXT,
4117
+ metadata JSONB DEFAULT '{}',
4118
+ started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
4119
+ completed_at TIMESTAMPTZ,
4120
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
4121
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
4122
+ );
4123
+
4124
+ CREATE TABLE IF NOT EXISTS lattice_workflow_steps (
4125
+ id VARCHAR(255) NOT NULL,
4126
+ run_id VARCHAR(255) NOT NULL REFERENCES lattice_workflow_runs(id) ON DELETE CASCADE,
4127
+ tenant_id VARCHAR(255) NOT NULL,
4128
+ step_type VARCHAR(50) NOT NULL,
4129
+ step_name VARCHAR(255) NOT NULL,
4130
+ edge_from VARCHAR(255),
4131
+ edge_to VARCHAR(255),
4132
+ edge_purpose TEXT,
4133
+ input JSONB,
4134
+ output JSONB,
4135
+ status VARCHAR(50) NOT NULL DEFAULT 'running',
4136
+ error_message TEXT,
4137
+ started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
4138
+ completed_at TIMESTAMPTZ,
4139
+ duration_ms INTEGER,
4140
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
4141
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
4142
+ PRIMARY KEY (id, run_id)
4143
+ );
4144
+
4145
+ CREATE INDEX IF NOT EXISTS idx_workflow_runs_tenant
4146
+ ON lattice_workflow_runs (tenant_id);
4147
+ CREATE INDEX IF NOT EXISTS idx_workflow_runs_thread
4148
+ ON lattice_workflow_runs (tenant_id, thread_id);
4149
+ CREATE INDEX IF NOT EXISTS idx_workflow_runs_assistant
4150
+ ON lattice_workflow_runs (tenant_id, assistant_id);
4151
+ CREATE INDEX IF NOT EXISTS idx_workflow_runs_status
4152
+ ON lattice_workflow_runs (status);
4153
+
4154
+ CREATE INDEX IF NOT EXISTS idx_workflow_steps_run
4155
+ ON lattice_workflow_steps (run_id);
4156
+ CREATE INDEX IF NOT EXISTS idx_workflow_steps_type_status
4157
+ ON lattice_workflow_steps (run_id, step_type, status);
4158
+ `);
4159
+ },
4160
+ down: async (client) => {
4161
+ await client.query(`
4162
+ DROP INDEX IF EXISTS idx_workflow_steps_type_status;
4163
+ DROP INDEX IF EXISTS idx_workflow_steps_run;
4164
+ DROP INDEX IF EXISTS idx_workflow_runs_status;
4165
+ DROP INDEX IF EXISTS idx_workflow_runs_assistant;
4166
+ DROP INDEX IF EXISTS idx_workflow_runs_thread;
4167
+ DROP INDEX IF EXISTS idx_workflow_runs_tenant;
4168
+ DROP TABLE IF EXISTS lattice_workflow_steps;
4169
+ DROP TABLE IF EXISTS lattice_workflow_runs;
4170
+ `);
4171
+ }
4172
+ };
4173
+
4101
4174
  // src/stores/ThreadMessageQueueStore.ts
4102
4175
  import crypto from "crypto";
4103
4176
 
@@ -4793,10 +4866,285 @@ var PostgreSQLChannelInstallationStore = class {
4793
4866
  };
4794
4867
  }
4795
4868
  };
4869
+
4870
+ // src/stores/PostgreSQLWorkflowTrackingStore.ts
4871
+ import { Pool as Pool15 } from "pg";
4872
+ var PostgreSQLWorkflowTrackingStore = class {
4873
+ constructor(options) {
4874
+ this.initialized = false;
4875
+ this.initPromise = null;
4876
+ if (typeof options.poolConfig === "string") {
4877
+ this.pool = new Pool15({ connectionString: options.poolConfig });
4878
+ } else {
4879
+ this.pool = new Pool15(options.poolConfig);
4880
+ }
4881
+ this.migrationManager = new MigrationManager(this.pool);
4882
+ this.migrationManager.register(createWorkflowTrackingTables);
4883
+ if (options.autoMigrate !== false) {
4884
+ this.initialize().catch((error) => {
4885
+ console.error("Failed to initialize PostgreSQLWorkflowTrackingStore:", error);
4886
+ throw error;
4887
+ });
4888
+ }
4889
+ }
4890
+ async dispose() {
4891
+ await this.pool.end();
4892
+ }
4893
+ async initialize() {
4894
+ if (this.initialized) return;
4895
+ if (this.initPromise) return this.initPromise;
4896
+ this.initPromise = (async () => {
4897
+ try {
4898
+ await this.migrationManager.migrate();
4899
+ this.initialized = true;
4900
+ } finally {
4901
+ this.initPromise = null;
4902
+ }
4903
+ })();
4904
+ return this.initPromise;
4905
+ }
4906
+ async ensureInitialized() {
4907
+ if (!this.initialized) await this.initialize();
4908
+ }
4909
+ async createWorkflowRun(request) {
4910
+ await this.ensureInitialized();
4911
+ const now = /* @__PURE__ */ new Date();
4912
+ const id = `${request.threadId}_${Date.now()}`;
4913
+ await this.pool.query(
4914
+ `INSERT INTO lattice_workflow_runs (id, tenant_id, assistant_id, thread_id, status, topology_edges, total_edges, completed_edges, metadata, started_at, created_at, updated_at)
4915
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
4916
+ [
4917
+ id,
4918
+ request.tenantId,
4919
+ request.assistantId,
4920
+ request.threadId,
4921
+ "running",
4922
+ JSON.stringify(request.topologyEdges),
4923
+ request.topologyEdges.length,
4924
+ 0,
4925
+ JSON.stringify(request.metadata || {}),
4926
+ now,
4927
+ now,
4928
+ now
4929
+ ]
4930
+ );
4931
+ return {
4932
+ id,
4933
+ status: "running",
4934
+ startedAt: now,
4935
+ completedAt: void 0,
4936
+ ...request,
4937
+ completedEdges: 0,
4938
+ totalEdges: request.topologyEdges.length,
4939
+ createdAt: now,
4940
+ updatedAt: now
4941
+ };
4942
+ }
4943
+ async getWorkflowRun(runId) {
4944
+ await this.ensureInitialized();
4945
+ const result = await this.pool.query(
4946
+ `SELECT * FROM lattice_workflow_runs WHERE id = $1`,
4947
+ [runId]
4948
+ );
4949
+ if (result.rows.length === 0) return null;
4950
+ return this.mapRowToWorkflowRun(result.rows[0]);
4951
+ }
4952
+ async updateWorkflowRun(runId, updates) {
4953
+ await this.ensureInitialized();
4954
+ const now = /* @__PURE__ */ new Date();
4955
+ const setClauses = ["updated_at = $2"];
4956
+ const values = [runId, now];
4957
+ let paramIdx = 3;
4958
+ if (updates.status !== void 0) {
4959
+ setClauses.push(`status = $${paramIdx++}`);
4960
+ values.push(updates.status);
4961
+ }
4962
+ if (updates.completedEdges !== void 0) {
4963
+ setClauses.push(`completed_edges = $${paramIdx++}`);
4964
+ values.push(updates.completedEdges);
4965
+ }
4966
+ if (updates.errorMessage !== void 0) {
4967
+ setClauses.push(`error_message = $${paramIdx++}`);
4968
+ values.push(updates.errorMessage);
4969
+ }
4970
+ if (updates.completedAt !== void 0) {
4971
+ setClauses.push(`completed_at = $${paramIdx++}`);
4972
+ values.push(updates.completedAt);
4973
+ }
4974
+ if (updates.metadata !== void 0) {
4975
+ setClauses.push(`metadata = $${paramIdx++}`);
4976
+ values.push(JSON.stringify(updates.metadata));
4977
+ }
4978
+ const query = `UPDATE lattice_workflow_runs SET ${setClauses.join(", ")} WHERE id = $1`;
4979
+ await this.pool.query(query, values);
4980
+ return this.getWorkflowRun(runId);
4981
+ }
4982
+ async getWorkflowRunsByThreadId(tenantId, threadId) {
4983
+ await this.ensureInitialized();
4984
+ const result = await this.pool.query(
4985
+ `SELECT * FROM lattice_workflow_runs WHERE tenant_id = $1 AND thread_id = $2 ORDER BY created_at DESC`,
4986
+ [tenantId, threadId]
4987
+ );
4988
+ return result.rows.map(this.mapRowToWorkflowRun);
4989
+ }
4990
+ async getWorkflowRunsByAssistantId(tenantId, assistantId) {
4991
+ await this.ensureInitialized();
4992
+ const result = await this.pool.query(
4993
+ `SELECT * FROM lattice_workflow_runs WHERE tenant_id = $1 AND assistant_id = $2 ORDER BY created_at DESC`,
4994
+ [tenantId, assistantId]
4995
+ );
4996
+ return result.rows.map(this.mapRowToWorkflowRun);
4997
+ }
4998
+ async getWorkflowRunsByTenantId(tenantId) {
4999
+ await this.ensureInitialized();
5000
+ const result = await this.pool.query(
5001
+ `SELECT * FROM lattice_workflow_runs WHERE tenant_id = $1 ORDER BY created_at DESC`,
5002
+ [tenantId]
5003
+ );
5004
+ return result.rows.map(this.mapRowToWorkflowRun);
5005
+ }
5006
+ async createRunStep(request) {
5007
+ await this.ensureInitialized();
5008
+ const now = /* @__PURE__ */ new Date();
5009
+ const id = `step_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
5010
+ await this.pool.query(
5011
+ `INSERT INTO lattice_workflow_steps (id, run_id, tenant_id, step_type, step_name, edge_from, edge_to, edge_purpose, input, status, started_at, created_at, updated_at)
5012
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
5013
+ [
5014
+ id,
5015
+ request.runId,
5016
+ request.tenantId,
5017
+ request.stepType,
5018
+ request.stepName,
5019
+ request.edgeFrom || null,
5020
+ request.edgeTo || null,
5021
+ request.edgePurpose || null,
5022
+ request.input ? JSON.stringify(request.input) : null,
5023
+ "running",
5024
+ now,
5025
+ now,
5026
+ now
5027
+ ]
5028
+ );
5029
+ return {
5030
+ id,
5031
+ runId: request.runId,
5032
+ tenantId: request.tenantId,
5033
+ stepType: request.stepType,
5034
+ stepName: request.stepName,
5035
+ edgeFrom: request.edgeFrom,
5036
+ edgeTo: request.edgeTo,
5037
+ edgePurpose: request.edgePurpose,
5038
+ input: request.input,
5039
+ status: "running",
5040
+ startedAt: now,
5041
+ createdAt: now,
5042
+ updatedAt: now
5043
+ };
5044
+ }
5045
+ async updateRunStep(runId, stepId, updates) {
5046
+ await this.ensureInitialized();
5047
+ const now = /* @__PURE__ */ new Date();
5048
+ const setClauses = ["updated_at = $3"];
5049
+ const values = [runId, stepId, now];
5050
+ let paramIdx = 4;
5051
+ if (updates.status !== void 0) {
5052
+ setClauses.push(`status = $${paramIdx++}`);
5053
+ values.push(updates.status);
5054
+ }
5055
+ if (updates.output !== void 0) {
5056
+ setClauses.push(`output = $${paramIdx++}`);
5057
+ values.push(JSON.stringify(updates.output));
5058
+ }
5059
+ if (updates.errorMessage !== void 0) {
5060
+ setClauses.push(`error_message = $${paramIdx++}`);
5061
+ values.push(updates.errorMessage);
5062
+ }
5063
+ if (updates.completedAt !== void 0) {
5064
+ setClauses.push(`completed_at = $${paramIdx++}`);
5065
+ values.push(updates.completedAt);
5066
+ }
5067
+ if (updates.durationMs !== void 0) {
5068
+ setClauses.push(`duration_ms = $${paramIdx++}`);
5069
+ values.push(updates.durationMs);
5070
+ }
5071
+ const query = `UPDATE lattice_workflow_steps SET ${setClauses.join(", ")} WHERE run_id = $1 AND id = $2`;
5072
+ const result = await this.pool.query(query, values);
5073
+ if (result.rowCount === 0) return null;
5074
+ const row = await this.pool.query(
5075
+ `SELECT * FROM lattice_workflow_steps WHERE run_id = $1 AND id = $2`,
5076
+ [runId, stepId]
5077
+ );
5078
+ return row.rows.length > 0 ? this.mapRowToRunStep(row.rows[0]) : null;
5079
+ }
5080
+ async getRunSteps(runId) {
5081
+ await this.ensureInitialized();
5082
+ const result = await this.pool.query(
5083
+ `SELECT * FROM lattice_workflow_steps WHERE run_id = $1 ORDER BY created_at ASC`,
5084
+ [runId]
5085
+ );
5086
+ return result.rows.map(this.mapRowToRunStep);
5087
+ }
5088
+ async getRunStepsByType(runId, stepType) {
5089
+ await this.ensureInitialized();
5090
+ const result = await this.pool.query(
5091
+ `SELECT * FROM lattice_workflow_steps WHERE run_id = $1 AND step_type = $2 ORDER BY created_at ASC`,
5092
+ [runId, stepType]
5093
+ );
5094
+ return result.rows.map(this.mapRowToRunStep);
5095
+ }
5096
+ async getInterruptedSteps(runId) {
5097
+ await this.ensureInitialized();
5098
+ const result = await this.pool.query(
5099
+ `SELECT * FROM lattice_workflow_steps WHERE run_id = $1 AND status = 'interrupted' ORDER BY created_at ASC`,
5100
+ [runId]
5101
+ );
5102
+ return result.rows.map(this.mapRowToRunStep);
5103
+ }
5104
+ mapRowToWorkflowRun(row) {
5105
+ return {
5106
+ id: row.id,
5107
+ tenantId: row.tenant_id,
5108
+ assistantId: row.assistant_id,
5109
+ threadId: row.thread_id,
5110
+ status: row.status,
5111
+ topologyEdges: typeof row.topology_edges === "string" ? JSON.parse(row.topology_edges) : row.topology_edges || [],
5112
+ totalEdges: row.total_edges,
5113
+ completedEdges: row.completed_edges,
5114
+ errorMessage: row.error_message,
5115
+ metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata || {},
5116
+ startedAt: row.started_at,
5117
+ completedAt: row.completed_at,
5118
+ createdAt: row.created_at,
5119
+ updatedAt: row.updated_at
5120
+ };
5121
+ }
5122
+ mapRowToRunStep(row) {
5123
+ return {
5124
+ id: row.id,
5125
+ runId: row.run_id,
5126
+ tenantId: row.tenant_id,
5127
+ stepType: row.step_type,
5128
+ stepName: row.step_name,
5129
+ edgeFrom: row.edge_from,
5130
+ edgeTo: row.edge_to,
5131
+ edgePurpose: row.edge_purpose,
5132
+ input: typeof row.input === "string" ? JSON.parse(row.input) : row.input,
5133
+ output: typeof row.output === "string" ? JSON.parse(row.output) : row.output,
5134
+ status: row.status,
5135
+ errorMessage: row.error_message,
5136
+ startedAt: row.started_at,
5137
+ completedAt: row.completed_at,
5138
+ durationMs: row.duration_ms,
5139
+ createdAt: row.created_at,
5140
+ updatedAt: row.updated_at
5141
+ };
5142
+ }
5143
+ };
4796
5144
  export {
4797
5145
  ChannelIdentityMappingStore,
4798
5146
  MigrationManager,
4799
- Pool15 as Pool,
5147
+ Pool16 as Pool,
4800
5148
  PostgreSQLAssistantStore,
4801
5149
  PostgreSQLChannelInstallationStore,
4802
5150
  PostgreSQLDatabaseConfigStore,
@@ -4809,6 +5157,7 @@ export {
4809
5157
  PostgreSQLThreadStore,
4810
5158
  PostgreSQLUserStore,
4811
5159
  PostgreSQLUserTenantLinkStore,
5160
+ PostgreSQLWorkflowTrackingStore,
4812
5161
  PostgreSQLWorkspaceStore,
4813
5162
  ThreadMessageQueueStore,
4814
5163
  addAssistantTenantId,
@@ -4831,6 +5180,7 @@ export {
4831
5180
  createThreadMessageQueueTable,
4832
5181
  createThreadsTable,
4833
5182
  createUsersTable,
5183
+ createWorkflowTrackingTables,
4834
5184
  createWorkspacesTable,
4835
5185
  getThreadMessageQueueStore
4836
5186
  };