@axiom-lattice/pg-stores 1.0.21 → 1.0.23
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 +17 -0
- package/dist/index.d.mts +126 -58
- package/dist/index.d.ts +126 -58
- package/dist/index.js +551 -135
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +544 -135
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +7 -0
- package/src/migrations/assistant_migrations.ts +4 -2
- package/src/migrations/assistant_pk_migration.ts +101 -0
- package/src/migrations/assistant_tenant_migration.ts +29 -0
- package/src/migrations/schedule_tenant_migration.ts +75 -0
- package/src/migrations/skill_pk_migration.ts +97 -0
- package/src/migrations/skill_tenant_migration.ts +55 -0
- package/src/migrations/thread_pk_migration.ts +99 -0
- package/src/migrations/thread_tenant_migration.ts +55 -0
- package/src/stores/PostgreSQLAssistantStore.ts +55 -22
- package/src/stores/PostgreSQLScheduleStorage.ts +16 -0
- package/src/stores/PostgreSQLSkillStore.ts +146 -103
- package/src/stores/PostgreSQLThreadStore.ts +40 -30
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/stores/PostgreSQLThreadStore.ts","../src/migrations/migration.ts","../src/migrations/thread_migrations.ts","../src/stores/PostgreSQLAssistantStore.ts","../src/migrations/assistant_migrations.ts","../src/stores/PostgreSQLScheduleStorage.ts","../src/migrations/schedule_migrations.ts","../src/stores/PostgreSQLSkillStore.ts","../src/migrations/skill_migrations.ts","../src/stores/PostgreSQLDatabaseConfigStore.ts","../src/migrations/database_config_migrations.ts","../src/stores/PostgreSQLMetricsServerConfigStore.ts","../src/migrations/metrics_config_migrations.ts","../src/stores/PostgreSQLMcpServerConfigStore.ts","../src/migrations/mcp_server_config_migrations.ts","../src/stores/PostgreSQLWorkspaceStore.ts","../src/migrations/workspace_migrations.ts","../src/stores/PostgreSQLProjectStore.ts","../src/migrations/project_migrations.ts","../src/stores/PostgreSQLUserStore.ts","../src/migrations/user_migrations.ts","../src/migrations/user_status_migration.ts","../src/stores/PostgreSQLTenantStore.ts","../src/migrations/tenant_migrations.ts","../src/stores/PostgreSQLUserTenantLinkStore.ts","../src/migrations/user_tenant_link_migrations.ts"],"sourcesContent":["/**\n * PostgreSQL Stores Package\n *\n * Provides PostgreSQL implementations for various stores\n */\n\nexport * from \"./stores/PostgreSQLThreadStore\";\nexport * from \"./stores/PostgreSQLAssistantStore\";\nexport * from \"./stores/PostgreSQLScheduleStorage\";\nexport * from \"./stores/PostgreSQLSkillStore\";\nexport * from \"./stores/PostgreSQLDatabaseConfigStore\";\nexport * from \"./stores/PostgreSQLMetricsServerConfigStore\";\nexport * from \"./stores/PostgreSQLMcpServerConfigStore\";\nexport * from \"./stores/PostgreSQLWorkspaceStore\";\nexport * from \"./stores/PostgreSQLProjectStore\";\nexport * from \"./stores/PostgreSQLUserStore\";\nexport * from \"./stores/PostgreSQLTenantStore\";\nexport * from \"./stores/PostgreSQLUserTenantLinkStore\";\nexport * from \"./migrations/migration\";\nexport * from \"./migrations/thread_migrations\";\nexport * from \"./migrations/assistant_migrations\";\nexport * from \"./migrations/schedule_migrations\";\nexport * from \"./migrations/skill_migrations\";\nexport * from \"./migrations/database_config_migrations\";\nexport * from \"./migrations/metrics_config_migrations\";\nexport * from \"./migrations/mcp_server_config_migrations\";\nexport * from \"./migrations/workspace_migrations\";\nexport * from \"./migrations/project_migrations\";\nexport * from \"./migrations/user_migrations\";\nexport * from \"./migrations/tenant_migrations\";\n\n// Re-export for convenience\nexport { PostgreSQLThreadStore } from \"./stores/PostgreSQLThreadStore\";\nexport { PostgreSQLAssistantStore } from \"./stores/PostgreSQLAssistantStore\";\nexport { PostgreSQLScheduleStorage } from \"./stores/PostgreSQLScheduleStorage\";\nexport { PostgreSQLSkillStore } from \"./stores/PostgreSQLSkillStore\";\nexport { PostgreSQLDatabaseConfigStore } from \"./stores/PostgreSQLDatabaseConfigStore\";\nexport { PostgreSQLMetricsServerConfigStore } from \"./stores/PostgreSQLMetricsServerConfigStore\";\nexport { PostgreSQLMcpServerConfigStore } from \"./stores/PostgreSQLMcpServerConfigStore\";\nexport { PostgreSQLWorkspaceStore } from \"./stores/PostgreSQLWorkspaceStore\";\nexport { PostgreSQLProjectStore } from \"./stores/PostgreSQLProjectStore\";\nexport { PostgreSQLUserStore } from \"./stores/PostgreSQLUserStore\";\nexport { PostgreSQLTenantStore } from \"./stores/PostgreSQLTenantStore\";\nexport { PostgreSQLUserTenantLinkStore } from \"./stores/PostgreSQLUserTenantLinkStore\";\n\n// Re-export types from protocols\nexport type {\n ThreadStore,\n Thread,\n CreateThreadRequest,\n AssistantStore,\n Assistant,\n CreateAssistantRequest,\n ScheduleStorage,\n ScheduledTaskDefinition,\n ScheduledTaskStatus,\n ScheduleExecutionType,\n SkillStore,\n Skill,\n CreateSkillRequest,\n DatabaseConfigStore,\n DatabaseConfigEntry,\n CreateDatabaseConfigRequest,\n UpdateDatabaseConfigRequest,\n DatabaseConfig,\n DatabaseType,\n MetricsServerConfigStore,\n MetricsServerConfigEntry,\n CreateMetricsServerConfigRequest,\n UpdateMetricsServerConfigRequest,\n MetricsServerConfig,\n McpServerConfigStore,\n McpServerConfigEntry,\n CreateMcpServerConfigRequest,\n UpdateMcpServerConfigRequest,\n WorkspaceStore,\n Workspace,\n CreateWorkspaceRequest,\n UpdateWorkspaceRequest,\n StorageType,\n ProjectStore,\n Project,\n CreateProjectRequest,\n UpdateProjectRequest,\n UserStore,\n User,\n CreateUserRequest,\n UpdateUserRequest,\n TenantStore,\n Tenant,\n TenantStatus,\n CreateTenantRequest,\n UpdateTenantRequest,\n UserTenantLinkStore,\n UserTenantLink,\n UserTenantRole,\n CreateUserTenantLinkRequest,\n UpdateUserTenantLinkRequest,\n} from \"@axiom-lattice/protocols\";\n","/**\n * PostgreSQL implementation of ThreadStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from \"pg\";\nimport {\n ThreadStore,\n Thread,\n CreateThreadRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createThreadsTable } from \"../migrations/thread_migrations\";\n\n/**\n * PostgreSQL ThreadStore options\n */\nexport interface PostgreSQLThreadStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of ThreadStore\n */\nexport class PostgreSQLThreadStore implements ThreadStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n private initPromise: Promise<void> | null = null; // Promise-based lock to prevent concurrent initialization\n\n constructor(options: PostgreSQLThreadStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createThreadsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLThreadStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n // If already initialized, return immediately\n if (this.initialized) {\n return;\n }\n\n // If initialization is in progress, wait for it\n if (this.initPromise) {\n return this.initPromise;\n }\n\n // Start initialization and store the promise\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n // Clear the promise after completion (success or failure)\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all threads for a specific assistant\n */\n async getThreadsByAssistantId(assistantId: string): Promise<Thread[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n assistant_id: string;\n metadata: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, assistant_id, metadata, created_at, updated_at\n FROM lattice_threads\n WHERE assistant_id = $1\n ORDER BY created_at DESC\n `,\n [assistantId]\n );\n\n return result.rows.map(this.mapRowToThread);\n }\n\n /**\n * Get a thread by ID for a specific assistant\n */\n async getThreadById(\n assistantId: string,\n threadId: string\n ): Promise<Thread | undefined> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n assistant_id: string;\n metadata: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, assistant_id, metadata, created_at, updated_at\n FROM lattice_threads\n WHERE id = $1 AND assistant_id = $2\n `,\n [threadId, assistantId]\n );\n\n if (result.rows.length === 0) {\n return undefined;\n }\n\n return this.mapRowToThread(result.rows[0]);\n }\n\n /**\n * Create a new thread for an assistant\n */\n async createThread(\n assistantId: string,\n threadId: string,\n data: CreateThreadRequest\n ): Promise<Thread> {\n await this.ensureInitialized();\n\n const now = new Date();\n const metadata = data.metadata || {};\n\n await this.pool.query(\n `\n INSERT INTO lattice_threads (id, assistant_id, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (id, assistant_id) DO UPDATE SET\n metadata = EXCLUDED.metadata,\n updated_at = EXCLUDED.updated_at\n `,\n [threadId, assistantId, JSON.stringify(metadata), now, now]\n );\n\n return {\n id: threadId,\n assistantId,\n metadata,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing thread\n */\n async updateThread(\n assistantId: string,\n threadId: string,\n updates: Partial<CreateThreadRequest>\n ): Promise<Thread | null> {\n await this.ensureInitialized();\n\n // Get existing thread\n const existing = await this.getThreadById(assistantId, threadId);\n if (!existing) {\n return null;\n }\n\n // Merge metadata\n const updatedMetadata = {\n ...existing.metadata,\n ...(updates.metadata || {}),\n };\n\n const now = new Date();\n\n await this.pool.query(\n `\n UPDATE lattice_threads\n SET metadata = $1, updated_at = $2\n WHERE id = $3 AND assistant_id = $4\n `,\n [JSON.stringify(updatedMetadata), now, threadId, assistantId]\n );\n\n return {\n ...existing,\n metadata: updatedMetadata,\n updatedAt: now,\n };\n }\n\n /**\n * Delete a thread by ID\n */\n async deleteThread(assistantId: string, threadId: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_threads\n WHERE id = $1 AND assistant_id = $2\n `,\n [threadId, assistantId]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if thread exists\n */\n async hasThread(assistantId: string, threadId: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_threads\n WHERE id = $1 AND assistant_id = $2\n LIMIT 1\n `,\n [threadId, assistantId]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Thread object\n */\n private mapRowToThread(row: {\n id: string;\n assistant_id: string;\n metadata: any;\n created_at: Date;\n updated_at: Date;\n }): Thread {\n return {\n id: row.id,\n assistantId: row.assistant_id,\n metadata:\n typeof row.metadata === \"string\"\n ? JSON.parse(row.metadata)\n : row.metadata || {},\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n}\n","/**\n * Migration system for database schema management\n */\n\nimport { Pool, PoolClient } from \"pg\";\n\n/**\n * Migration record stored in database\n */\ninterface MigrationRecord {\n version: number;\n name: string;\n applied_at: Date;\n}\n\n/**\n * Migration definition\n */\nexport interface Migration {\n version: number;\n name: string;\n up: (client: PoolClient) => Promise<void>;\n down?: (client: PoolClient) => Promise<void>;\n}\n\n/**\n * Migration manager\n */\nexport class MigrationManager {\n private pool: Pool;\n private migrations: Migration[] = [];\n\n constructor(pool: Pool) {\n this.pool = pool;\n }\n\n /**\n * Register a migration\n */\n register(migration: Migration): void {\n this.migrations.push(migration);\n // Sort migrations by version\n this.migrations.sort((a, b) => a.version - b.version);\n }\n\n /**\n * Initialize migrations table if it doesn't exist\n */\n private async ensureMigrationsTable(client: PoolClient): Promise<void> {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_schema_migrations (\n version INTEGER PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n }\n\n /**\n * Get applied migrations from database\n */\n private async getAppliedMigrations(\n client: PoolClient\n ): Promise<MigrationRecord[]> {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_schema_migrations (\n version INTEGER PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n const result = await client.query<MigrationRecord>(\n \"SELECT version, name, applied_at FROM lattice_schema_migrations ORDER BY version\"\n );\n return result.rows;\n }\n\n /**\n * Apply pending migrations\n * Uses PostgreSQL advisory locks to prevent concurrent migrations\n */\n async migrate(): Promise<void> {\n const client = await this.pool.connect();\n try {\n // Use advisory lock to prevent concurrent migrations\n // Lock ID: 123456789 (arbitrary but consistent)\n const lockId = 123456789;\n \n // Acquire advisory lock (blocks until lock is available)\n // This ensures only one migration runs at a time across all instances\n await client.query(\"SELECT pg_advisory_lock($1)\", [lockId]);\n \n try {\n await client.query(\"BEGIN\");\n\n await this.ensureMigrationsTable(client);\n const appliedMigrations = await this.getAppliedMigrations(client);\n const appliedVersions = new Set(appliedMigrations.map((m) => m.version));\n\n // Find pending migrations\n const pendingMigrations = this.migrations.filter(\n (m) => !appliedVersions.has(m.version)\n );\n\n if (pendingMigrations.length === 0) {\n console.log(\"No pending migrations\");\n await client.query(\"COMMIT\");\n return;\n }\n\n // Apply pending migrations\n for (const migration of pendingMigrations) {\n console.log(\n `Applying migration ${migration.version}: ${migration.name}`\n );\n await migration.up(client);\n // Use ON CONFLICT DO NOTHING to handle race conditions gracefully\n await client.query(\n \"INSERT INTO lattice_schema_migrations (version, name) VALUES ($1, $2) ON CONFLICT (version) DO NOTHING\",\n [migration.version, migration.name]\n );\n }\n\n await client.query(\"COMMIT\");\n console.log(`Applied ${pendingMigrations.length} migration(s)`);\n } catch (error) {\n await client.query(\"ROLLBACK\");\n throw error;\n } finally {\n // Release advisory lock\n await client.query(\"SELECT pg_advisory_unlock($1)\", [lockId]);\n }\n } catch (error) {\n throw error;\n } finally {\n client.release();\n }\n }\n\n /**\n * Rollback last migration\n */\n async rollback(): Promise<void> {\n const client = await this.pool.connect();\n try {\n await client.query(\"BEGIN\");\n\n const appliedMigrations = await this.getAppliedMigrations(client);\n if (appliedMigrations.length === 0) {\n console.log(\"No migrations to rollback\");\n await client.query(\"COMMIT\");\n return;\n }\n\n const lastMigration = appliedMigrations[appliedMigrations.length - 1];\n const migration = this.migrations.find(\n (m) => m.version === lastMigration.version\n );\n\n if (!migration || !migration.down) {\n throw new Error(\n `Migration ${lastMigration.version} does not have a down migration`\n );\n }\n\n console.log(\n `Rolling back migration ${lastMigration.version}: ${lastMigration.name}`\n );\n await migration.down(client);\n await client.query(\n \"DELETE FROM lattice_schema_migrations WHERE version = $1\",\n [lastMigration.version]\n );\n\n await client.query(\"COMMIT\");\n console.log(\"Rollback completed\");\n } catch (error) {\n await client.query(\"ROLLBACK\");\n throw error;\n } finally {\n client.release();\n }\n }\n\n /**\n * Get current migration version\n */\n async getCurrentVersion(): Promise<number> {\n const client = await this.pool.connect();\n try {\n const appliedMigrations = await this.getAppliedMigrations(client);\n if (appliedMigrations.length === 0) {\n return 0;\n }\n return Math.max(...appliedMigrations.map((m) => m.version));\n } finally {\n client.release();\n }\n }\n}\n","/**\n * Thread table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create threads table\n */\nexport const createThreadsTable: Migration = {\n version: 1,\n name: \"create_threads_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_threads (\n id VARCHAR(255) NOT NULL,\n assistant_id VARCHAR(255) NOT NULL,\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (id, assistant_id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_threads_assistant_id \n ON lattice_threads(assistant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_threads_created_at \n ON lattice_threads(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_threads_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_threads_assistant_id\");\n await client.query(\"DROP TABLE IF EXISTS lattice_threads\");\n },\n};\n","/**\n * PostgreSQL implementation of AssistantStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from \"pg\";\nimport {\n AssistantStore,\n Assistant,\n CreateAssistantRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createAssistantsTable } from \"../migrations/assistant_migrations\";\n\n/**\n * PostgreSQL AssistantStore options\n */\nexport interface PostgreSQLAssistantStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of AssistantStore\n */\nexport class PostgreSQLAssistantStore implements AssistantStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLAssistantStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createAssistantsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLAssistantStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Get all assistants\n */\n async getAllAssistants(): Promise<Assistant[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string | null;\n graph_definition: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, graph_definition, created_at, updated_at\n FROM lattice_assistants\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map(this.mapRowToAssistant);\n }\n\n /**\n * Get assistant by ID\n */\n async getAssistantById(id: string): Promise<Assistant | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string | null;\n graph_definition: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, graph_definition, created_at, updated_at\n FROM lattice_assistants\n WHERE id = $1\n `,\n [id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToAssistant(result.rows[0]);\n }\n\n /**\n * Create a new assistant\n */\n async createAssistant(\n id: string,\n data: CreateAssistantRequest\n ): Promise<Assistant> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_assistants (id, name, description, graph_definition, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n graph_definition = EXCLUDED.graph_definition,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n data.name,\n data.description || null,\n JSON.stringify(data.graphDefinition),\n now,\n now,\n ]\n );\n\n return {\n id,\n name: data.name,\n description: data.description,\n graphDefinition: data.graphDefinition,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing assistant\n */\n async updateAssistant(\n id: string,\n updates: Partial<CreateAssistantRequest>\n ): Promise<Assistant | null> {\n await this.ensureInitialized();\n\n // Get existing assistant\n const existing = await this.getAssistantById(id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updates.graphDefinition !== undefined) {\n updateFields.push(`graph_definition = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.graphDefinition));\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id for WHERE clause\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_assistants\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex}\n `,\n updateValues\n );\n\n // Return updated assistant\n return await this.getAssistantById(id);\n }\n\n /**\n * Delete an assistant by ID\n */\n async deleteAssistant(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_assistants\n WHERE id = $1\n `,\n [id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if assistant exists\n */\n async hasAssistant(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_assistants\n WHERE id = $1\n LIMIT 1\n `,\n [id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Assistant object\n */\n private mapRowToAssistant(row: {\n id: string;\n name: string;\n description: string | null;\n graph_definition: any;\n created_at: Date;\n updated_at: Date;\n }): Assistant {\n return {\n id: row.id,\n name: row.name,\n description: row.description || undefined,\n graphDefinition:\n typeof row.graph_definition === \"string\"\n ? JSON.parse(row.graph_definition)\n : row.graph_definition,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n}\n","/**\n * Assistant table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create assistants table\n */\nexport const createAssistantsTable: Migration = {\n version: 3,\n name: \"create_assistants_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_assistants (\n id VARCHAR(255) PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n graph_definition JSONB NOT NULL,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_assistants_name \n ON lattice_assistants(name)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_assistants_created_at \n ON lattice_assistants(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\n \"DROP INDEX IF EXISTS idx_lattice_assistants_created_at\"\n );\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_assistants_name\");\n await client.query(\"DROP TABLE IF EXISTS lattice_assistants\");\n },\n};\n","/**\n * PostgreSQL implementation of ScheduleStorage\n *\n * Provides persistent storage for scheduled tasks\n * Data survives service restarts\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n ScheduleStorage,\n ScheduledTaskDefinition,\n ScheduledTaskStatus,\n ScheduleExecutionType,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createScheduledTasksTable } from \"../migrations/schedule_migrations\";\n\n/**\n * PostgreSQL ScheduleStorage options\n */\nexport interface PostgreSQLScheduleStorageOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * Database row type for scheduled tasks\n */\ninterface ScheduledTaskRow {\n task_id: string;\n task_type: string;\n payload: string | Record<string, any>;\n assistant_id: string | null;\n thread_id: string | null;\n execution_type: string;\n execute_at: Date | null;\n delay_ms: number | null;\n cron_expression: string | null;\n timezone: string | null;\n next_run_at: Date | null;\n last_run_at: Date | null;\n status: string;\n run_count: number;\n max_runs: number | null;\n retry_count: number;\n max_retries: number;\n last_error: string | null;\n created_at: Date;\n updated_at: Date;\n expires_at: Date | null;\n metadata: string | Record<string, any> | null;\n}\n\n/**\n * PostgreSQL implementation of ScheduleStorage\n */\nexport class PostgreSQLScheduleStorage implements ScheduleStorage {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n\n constructor(options: PostgreSQLScheduleStorageOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createScheduledTasksTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLScheduleStorage:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n if (this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Save a new task\n */\n async save(task: ScheduledTaskDefinition): Promise<void> {\n await this.ensureInitialized();\n\n await this.pool.query(\n `\n INSERT INTO lattice_scheduled_tasks (\n task_id, task_type, payload, assistant_id, thread_id, execution_type,\n execute_at, delay_ms, cron_expression, timezone,\n next_run_at, last_run_at, status, run_count, max_runs,\n retry_count, max_retries, last_error,\n created_at, updated_at, expires_at, metadata\n ) VALUES (\n $1, $2, $3, $4, $5, $6,\n $7, $8, $9, $10,\n $11, $12, $13, $14, $15,\n $16, $17, $18,\n $19, $20, $21, $22\n )\n ON CONFLICT (task_id) DO UPDATE SET\n task_type = EXCLUDED.task_type,\n payload = EXCLUDED.payload,\n assistant_id = EXCLUDED.assistant_id,\n thread_id = EXCLUDED.thread_id,\n execution_type = EXCLUDED.execution_type,\n execute_at = EXCLUDED.execute_at,\n delay_ms = EXCLUDED.delay_ms,\n cron_expression = EXCLUDED.cron_expression,\n timezone = EXCLUDED.timezone,\n next_run_at = EXCLUDED.next_run_at,\n last_run_at = EXCLUDED.last_run_at,\n status = EXCLUDED.status,\n run_count = EXCLUDED.run_count,\n max_runs = EXCLUDED.max_runs,\n retry_count = EXCLUDED.retry_count,\n max_retries = EXCLUDED.max_retries,\n last_error = EXCLUDED.last_error,\n updated_at = EXCLUDED.updated_at,\n expires_at = EXCLUDED.expires_at,\n metadata = EXCLUDED.metadata\n `,\n [\n task.taskId,\n task.taskType,\n JSON.stringify(task.payload),\n task.assistantId ?? null,\n task.threadId ?? null,\n task.executionType,\n task.executeAt ? new Date(task.executeAt) : null,\n task.delayMs ?? null,\n task.cronExpression ?? null,\n task.timezone ?? null,\n task.nextRunAt ? new Date(task.nextRunAt) : null,\n task.lastRunAt ? new Date(task.lastRunAt) : null,\n task.status,\n task.runCount,\n task.maxRuns ?? null,\n task.retryCount,\n task.maxRetries,\n task.lastError ?? null,\n new Date(task.createdAt),\n new Date(task.updatedAt),\n task.expiresAt ? new Date(task.expiresAt) : null,\n task.metadata ? JSON.stringify(task.metadata) : null,\n ]\n );\n }\n\n /**\n * Get task by ID\n */\n async get(taskId: string): Promise<ScheduledTaskDefinition | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE task_id = $1`,\n [taskId]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToTask(result.rows[0]);\n }\n\n /**\n * Update task\n */\n async update(\n taskId: string,\n updates: Partial<ScheduledTaskDefinition>\n ): Promise<void> {\n await this.ensureInitialized();\n\n const setClauses: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n // Build dynamic UPDATE query\n if (updates.taskType !== undefined) {\n setClauses.push(`task_type = $${paramIndex++}`);\n values.push(updates.taskType);\n }\n if (updates.payload !== undefined) {\n setClauses.push(`payload = $${paramIndex++}`);\n values.push(JSON.stringify(updates.payload));\n }\n if (updates.assistantId !== undefined) {\n setClauses.push(`assistant_id = $${paramIndex++}`);\n values.push(updates.assistantId);\n }\n if (updates.threadId !== undefined) {\n setClauses.push(`thread_id = $${paramIndex++}`);\n values.push(updates.threadId);\n }\n if (updates.executionType !== undefined) {\n setClauses.push(`execution_type = $${paramIndex++}`);\n values.push(updates.executionType);\n }\n if (updates.executeAt !== undefined) {\n setClauses.push(`execute_at = $${paramIndex++}`);\n values.push(updates.executeAt ? new Date(updates.executeAt) : null);\n }\n if (updates.delayMs !== undefined) {\n setClauses.push(`delay_ms = $${paramIndex++}`);\n values.push(updates.delayMs);\n }\n if (updates.cronExpression !== undefined) {\n setClauses.push(`cron_expression = $${paramIndex++}`);\n values.push(updates.cronExpression);\n }\n if (updates.timezone !== undefined) {\n setClauses.push(`timezone = $${paramIndex++}`);\n values.push(updates.timezone);\n }\n if (updates.nextRunAt !== undefined) {\n setClauses.push(`next_run_at = $${paramIndex++}`);\n values.push(updates.nextRunAt ? new Date(updates.nextRunAt) : null);\n }\n if (updates.lastRunAt !== undefined) {\n setClauses.push(`last_run_at = $${paramIndex++}`);\n values.push(updates.lastRunAt ? new Date(updates.lastRunAt) : null);\n }\n if (updates.status !== undefined) {\n setClauses.push(`status = $${paramIndex++}`);\n values.push(updates.status);\n }\n if (updates.runCount !== undefined) {\n setClauses.push(`run_count = $${paramIndex++}`);\n values.push(updates.runCount);\n }\n if (updates.maxRuns !== undefined) {\n setClauses.push(`max_runs = $${paramIndex++}`);\n values.push(updates.maxRuns);\n }\n if (updates.retryCount !== undefined) {\n setClauses.push(`retry_count = $${paramIndex++}`);\n values.push(updates.retryCount);\n }\n if (updates.maxRetries !== undefined) {\n setClauses.push(`max_retries = $${paramIndex++}`);\n values.push(updates.maxRetries);\n }\n if (updates.lastError !== undefined) {\n setClauses.push(`last_error = $${paramIndex++}`);\n values.push(updates.lastError);\n }\n if (updates.expiresAt !== undefined) {\n setClauses.push(`expires_at = $${paramIndex++}`);\n values.push(updates.expiresAt ? new Date(updates.expiresAt) : null);\n }\n if (updates.metadata !== undefined) {\n setClauses.push(`metadata = $${paramIndex++}`);\n values.push(updates.metadata ? JSON.stringify(updates.metadata) : null);\n }\n\n // Always update updated_at\n setClauses.push(`updated_at = $${paramIndex++}`);\n values.push(new Date());\n\n // Add taskId as the last parameter\n values.push(taskId);\n\n if (setClauses.length === 0) {\n return;\n }\n\n await this.pool.query(\n `UPDATE lattice_scheduled_tasks SET ${setClauses.join(\n \", \"\n )} WHERE task_id = $${paramIndex}`,\n values\n );\n }\n\n /**\n * Delete task\n */\n async delete(taskId: string): Promise<void> {\n await this.ensureInitialized();\n\n await this.pool.query(\n `DELETE FROM lattice_scheduled_tasks WHERE task_id = $1`,\n [taskId]\n );\n }\n\n /**\n * Get all active tasks (pending or paused)\n */\n async getActiveTasks(): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE status IN ($1, $2) ORDER BY created_at ASC`,\n [ScheduledTaskStatus.PENDING, ScheduledTaskStatus.PAUSED]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by type\n */\n async getTasksByType(taskType: string): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE task_type = $1 ORDER BY created_at DESC`,\n [taskType]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by status\n */\n async getTasksByStatus(\n status: ScheduledTaskStatus\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE status = $1 ORDER BY created_at DESC`,\n [status]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by execution type\n */\n async getTasksByExecutionType(\n executionType: ScheduleExecutionType\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE execution_type = $1 ORDER BY created_at DESC`,\n [executionType]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by assistant ID\n */\n async getTasksByAssistantId(\n assistantId: string\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE assistant_id = $1 ORDER BY created_at DESC`,\n [assistantId]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by thread ID\n */\n async getTasksByThreadId(\n threadId: string\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE thread_id = $1 ORDER BY created_at DESC`,\n [threadId]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get all tasks with optional filters\n */\n async getAllTasks(filters?: {\n status?: ScheduledTaskStatus;\n executionType?: ScheduleExecutionType;\n taskType?: string;\n assistantId?: string;\n threadId?: string;\n limit?: number;\n offset?: number;\n }): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const whereClauses: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n if (filters?.status !== undefined) {\n whereClauses.push(`status = $${paramIndex++}`);\n values.push(filters.status);\n }\n if (filters?.executionType !== undefined) {\n whereClauses.push(`execution_type = $${paramIndex++}`);\n values.push(filters.executionType);\n }\n if (filters?.taskType !== undefined) {\n whereClauses.push(`task_type = $${paramIndex++}`);\n values.push(filters.taskType);\n }\n if (filters?.assistantId !== undefined) {\n whereClauses.push(`assistant_id = $${paramIndex++}`);\n values.push(filters.assistantId);\n }\n if (filters?.threadId !== undefined) {\n whereClauses.push(`thread_id = $${paramIndex++}`);\n values.push(filters.threadId);\n }\n\n let query = `SELECT * FROM lattice_scheduled_tasks`;\n if (whereClauses.length > 0) {\n query += ` WHERE ${whereClauses.join(\" AND \")}`;\n }\n query += ` ORDER BY created_at DESC`;\n\n if (filters?.limit !== undefined) {\n query += ` LIMIT $${paramIndex++}`;\n values.push(filters.limit);\n }\n if (filters?.offset !== undefined) {\n query += ` OFFSET $${paramIndex++}`;\n values.push(filters.offset);\n }\n\n const result = await this.pool.query<ScheduledTaskRow>(query, values);\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Count tasks with optional filters\n */\n async countTasks(filters?: {\n status?: ScheduledTaskStatus;\n executionType?: ScheduleExecutionType;\n taskType?: string;\n assistantId?: string;\n threadId?: string;\n }): Promise<number> {\n await this.ensureInitialized();\n\n const whereClauses: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n if (filters?.status !== undefined) {\n whereClauses.push(`status = $${paramIndex++}`);\n values.push(filters.status);\n }\n if (filters?.executionType !== undefined) {\n whereClauses.push(`execution_type = $${paramIndex++}`);\n values.push(filters.executionType);\n }\n if (filters?.taskType !== undefined) {\n whereClauses.push(`task_type = $${paramIndex++}`);\n values.push(filters.taskType);\n }\n if (filters?.assistantId !== undefined) {\n whereClauses.push(`assistant_id = $${paramIndex++}`);\n values.push(filters.assistantId);\n }\n if (filters?.threadId !== undefined) {\n whereClauses.push(`thread_id = $${paramIndex++}`);\n values.push(filters.threadId);\n }\n\n let query = `SELECT COUNT(*) as count FROM lattice_scheduled_tasks`;\n if (whereClauses.length > 0) {\n query += ` WHERE ${whereClauses.join(\" AND \")}`;\n }\n\n const result = await this.pool.query<{ count: string }>(query, values);\n\n return parseInt(result.rows[0].count, 10);\n }\n\n /**\n * Delete completed/cancelled/failed tasks older than specified time\n */\n async deleteOldTasks(olderThanMs: number): Promise<number> {\n await this.ensureInitialized();\n\n const cutoff = new Date(Date.now() - olderThanMs);\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_scheduled_tasks \n WHERE status IN ($1, $2, $3) \n AND updated_at < $4\n `,\n [\n ScheduledTaskStatus.COMPLETED,\n ScheduledTaskStatus.CANCELLED,\n ScheduledTaskStatus.FAILED,\n cutoff,\n ]\n );\n\n return result.rowCount ?? 0;\n }\n\n /**\n * Map database row to ScheduledTaskDefinition\n */\n private mapRowToTask(row: ScheduledTaskRow): ScheduledTaskDefinition {\n return {\n taskId: row.task_id,\n taskType: row.task_type,\n payload:\n typeof row.payload === \"string\"\n ? JSON.parse(row.payload)\n : row.payload || {},\n assistantId: row.assistant_id ?? undefined,\n threadId: row.thread_id ?? undefined,\n executionType: row.execution_type as ScheduleExecutionType,\n executeAt: row.execute_at ? row.execute_at.getTime() : undefined,\n delayMs: row.delay_ms ?? undefined,\n cronExpression: row.cron_expression ?? undefined,\n timezone: row.timezone ?? undefined,\n nextRunAt: row.next_run_at ? row.next_run_at.getTime() : undefined,\n lastRunAt: row.last_run_at ? row.last_run_at.getTime() : undefined,\n status: row.status as ScheduledTaskStatus,\n runCount: row.run_count,\n maxRuns: row.max_runs ?? undefined,\n retryCount: row.retry_count,\n maxRetries: row.max_retries,\n lastError: row.last_error ?? undefined,\n createdAt: row.created_at.getTime(),\n updatedAt: row.updated_at.getTime(),\n expiresAt: row.expires_at ? row.expires_at.getTime() : undefined,\n metadata:\n row.metadata === null\n ? undefined\n : typeof row.metadata === \"string\"\n ? JSON.parse(row.metadata)\n : row.metadata,\n };\n }\n}\n","/**\n * PostgreSQL migrations for scheduled tasks table\n */\n\nimport { Migration } from \"./migration\";\n\n/**\n * Create the scheduled_tasks table\n */\nexport const createScheduledTasksTable: Migration = {\n name: \"create_scheduled_tasks_table\",\n version: 2,\n\n up: async (client) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_scheduled_tasks (\n -- Primary key\n task_id VARCHAR(255) PRIMARY KEY,\n \n -- Task type (maps to handler)\n task_type VARCHAR(255) NOT NULL,\n \n -- Payload (JSON-serializable data)\n payload JSONB NOT NULL DEFAULT '{}',\n \n -- Context fields for querying\n assistant_id VARCHAR(255),\n thread_id VARCHAR(255),\n \n -- Execution type: 'once' or 'cron'\n execution_type VARCHAR(50) NOT NULL,\n \n -- For ONCE type\n execute_at TIMESTAMPTZ,\n delay_ms BIGINT,\n \n -- For CRON type\n cron_expression VARCHAR(255),\n timezone VARCHAR(100),\n next_run_at TIMESTAMPTZ,\n last_run_at TIMESTAMPTZ,\n \n -- Status\n status VARCHAR(50) NOT NULL DEFAULT 'pending',\n \n -- Execution tracking\n run_count INTEGER NOT NULL DEFAULT 0,\n max_runs INTEGER,\n \n -- Error handling\n retry_count INTEGER NOT NULL DEFAULT 0,\n max_retries INTEGER NOT NULL DEFAULT 0,\n last_error TEXT,\n \n -- Timestamps\n created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n expires_at TIMESTAMPTZ,\n \n -- Metadata\n metadata JSONB\n );\n\n -- Index for querying active tasks\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_status \n ON lattice_scheduled_tasks (status);\n \n -- Index for querying by task type\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_task_type \n ON lattice_scheduled_tasks (task_type);\n \n -- Index for querying by execution type\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_execution_type \n ON lattice_scheduled_tasks (execution_type);\n \n -- Index for querying by assistant ID\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_assistant_id \n ON lattice_scheduled_tasks (assistant_id)\n WHERE assistant_id IS NOT NULL;\n \n -- Index for querying by thread ID\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_thread_id \n ON lattice_scheduled_tasks (thread_id)\n WHERE thread_id IS NOT NULL;\n \n -- Index for querying pending tasks by next run time\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_next_run \n ON lattice_scheduled_tasks (next_run_at) \n WHERE status = 'pending';\n \n -- Index for querying pending one-time tasks by execute time\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_execute_at \n ON lattice_scheduled_tasks (execute_at) \n WHERE status = 'pending' AND execution_type = 'once';\n \n -- Index for cleanup queries (old completed/cancelled tasks)\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_cleanup \n ON lattice_scheduled_tasks (updated_at) \n WHERE status IN ('completed', 'cancelled', 'failed');\n `);\n },\n\n down: async (client) => {\n await client.query(`\n DROP INDEX IF EXISTS idx_scheduled_tasks_cleanup;\n DROP INDEX IF EXISTS idx_scheduled_tasks_execute_at;\n DROP INDEX IF EXISTS idx_scheduled_tasks_next_run;\n DROP INDEX IF EXISTS idx_scheduled_tasks_thread_id;\n DROP INDEX IF EXISTS idx_scheduled_tasks_assistant_id;\n DROP INDEX IF EXISTS idx_scheduled_tasks_execution_type;\n DROP INDEX IF EXISTS idx_scheduled_tasks_task_type;\n DROP INDEX IF EXISTS idx_scheduled_tasks_status;\n DROP TABLE IF EXISTS lattice_scheduled_tasks;\n `);\n },\n};\n","/**\n * PostgreSQL implementation of SkillStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n SkillStore,\n Skill,\n CreateSkillRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createSkillsTable } from \"../migrations/skill_migrations\";\n\n/**\n * PostgreSQL SkillStore options\n */\nexport interface PostgreSQLSkillStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of SkillStore\n */\nexport class PostgreSQLSkillStore implements SkillStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLSkillStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createSkillsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLSkillStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Skill object\n */\n private mapRowToSkill(row: {\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }): Skill {\n return {\n id: row.id,\n name: row.name,\n description: row.description,\n license: row.license || undefined,\n compatibility: row.compatibility || undefined,\n metadata: row.metadata || {},\n content: row.content || undefined,\n subSkills: Array.isArray(row.sub_skills) && row.sub_skills.length > 0 \n ? row.sub_skills \n : undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all skills\n */\n async getAllSkills(): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Get skill by ID\n */\n async getSkillById(id: string): Promise<Skill | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE id = $1\n `,\n [id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToSkill(result.rows[0]);\n }\n\n /**\n * Create a new skill\n */\n async createSkill(id: string, data: CreateSkillRequest): Promise<Skill> {\n await this.ensureInitialized();\n\n const now = new Date();\n const metadata = data.metadata || {};\n\n await this.pool.query(\n `\n INSERT INTO lattice_skills (id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n license = EXCLUDED.license,\n compatibility = EXCLUDED.compatibility,\n metadata = EXCLUDED.metadata,\n content = EXCLUDED.content,\n sub_skills = EXCLUDED.sub_skills,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n data.name,\n data.description,\n data.license || null,\n data.compatibility || null,\n JSON.stringify(metadata),\n data.content || null,\n JSON.stringify(data.subSkills || []),\n now,\n now,\n ]\n );\n\n return this.getSkillById(id) as Promise<Skill>;\n }\n\n /**\n * Update an existing skill\n */\n async updateSkill(\n id: string,\n updates: Partial<CreateSkillRequest>\n ): Promise<Skill | null> {\n await this.ensureInitialized();\n\n const updateFields: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n values.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n values.push(updates.description);\n }\n\n if (updates.license !== undefined) {\n updateFields.push(`license = $${paramIndex++}`);\n values.push(updates.license || null);\n }\n\n if (updates.compatibility !== undefined) {\n updateFields.push(`compatibility = $${paramIndex++}`);\n values.push(updates.compatibility || null);\n }\n\n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n values.push(JSON.stringify(updates.metadata || {}));\n }\n\n if (updates.content !== undefined) {\n updateFields.push(`content = $${paramIndex++}`);\n values.push(updates.content || null);\n }\n\n if (updates.subSkills !== undefined) {\n updateFields.push(`sub_skills = $${paramIndex++}`);\n values.push(JSON.stringify(updates.subSkills || []));\n }\n\n if (updateFields.length === 0) {\n return this.getSkillById(id);\n }\n\n updateFields.push(`updated_at = $${paramIndex++}`);\n values.push(new Date());\n values.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_skills\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex}\n `,\n values\n );\n\n return this.getSkillById(id);\n }\n\n /**\n * Delete a skill by ID\n */\n async deleteSkill(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_skills\n WHERE id = $1\n `,\n [id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if skill exists\n */\n async hasSkill(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{ count: string }>(\n `\n SELECT COUNT(*) as count\n FROM lattice_skills\n WHERE id = $1\n `,\n [id]\n );\n\n return parseInt(result.rows[0].count, 10) > 0;\n }\n\n /**\n * Search skills by metadata\n */\n async searchByMetadata(\n metadataKey: string,\n metadataValue: string\n ): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE metadata->>$1 = $2\n ORDER BY created_at DESC\n `,\n [metadataKey, metadataValue]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Filter skills by compatibility\n */\n async filterByCompatibility(compatibility: string): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE compatibility = $1\n ORDER BY created_at DESC\n `,\n [compatibility]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Filter skills by license\n */\n async filterByLicense(license: string): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE license = $1\n ORDER BY created_at DESC\n `,\n [license]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Get sub-skills of a parent skill\n */\n async getSubSkills(parentSkillName: string): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const parentSkill = await this.getSkillById(parentSkillName);\n if (!parentSkill || !parentSkill.subSkills || parentSkill.subSkills.length === 0) {\n return [];\n }\n\n // Query all sub-skills in one query\n const placeholders = parentSkill.subSkills.map((_, index) => `$${index + 1}`).join(\", \");\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE name IN (${placeholders})\n ORDER BY created_at DESC\n `,\n parentSkill.subSkills\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n}\n","/**\n * Skill table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create skills table\n */\nexport const createSkillsTable: Migration = {\n version: 4,\n name: \"create_skills_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_skills (\n id VARCHAR(255) PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n description TEXT NOT NULL,\n license VARCHAR(255),\n compatibility VARCHAR(255),\n metadata JSONB DEFAULT '{}',\n content TEXT,\n sub_skills JSONB DEFAULT '[]',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_name \n ON lattice_skills(name)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_license \n ON lattice_skills(license)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_compatibility \n ON lattice_skills(compatibility)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_created_at \n ON lattice_skills(created_at DESC)\n `);\n\n // Create GIN index for metadata JSONB queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_metadata \n ON lattice_skills USING GIN (metadata)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_metadata\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_compatibility\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_license\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_name\");\n await client.query(\"DROP TABLE IF EXISTS lattice_skills\");\n },\n};\n","/**\n * PostgreSQL implementation of DatabaseConfigStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from 'pg';\nimport {\n DatabaseConfigStore,\n DatabaseConfigEntry,\n CreateDatabaseConfigRequest,\n UpdateDatabaseConfigRequest,\n DatabaseConfig,\n} from '@axiom-lattice/protocols';\nimport { MigrationManager } from '../migrations/migration';\nimport { createDatabaseConfigsTable } from '../migrations/database_config_migrations';\nimport { encrypt, decrypt } from '@axiom-lattice/core';\n\n/**\n * PostgreSQL DatabaseConfigStore options\n */\nexport interface PostgreSQLDatabaseConfigStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of DatabaseConfigStore\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n * - Automatic password encryption/decryption\n * - Unique constraint on (tenant_id, key)\n */\nexport class PostgreSQLDatabaseConfigStore implements DatabaseConfigStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLDatabaseConfigStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === 'string') {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createDatabaseConfigsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error('Failed to initialize PostgreSQLDatabaseConfigStore:', error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all database configurations for a tenant\n */\n async getAllConfigs(tenantId: string): Promise<DatabaseConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get all database configurations across all tenants\n */\n async getAllConfigsWithoutTenant(): Promise<DatabaseConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get database configuration by ID\n */\n async getConfigById(tenantId: string, id: string): Promise<DatabaseConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Get database configuration by business key\n */\n async getConfigByKey(tenantId: string, key: string): Promise<DatabaseConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n WHERE tenant_id = $1 AND key = $2\n `,\n [tenantId, key]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Create a new database configuration\n */\n async createConfig(\n tenantId: string,\n id: string,\n data: CreateDatabaseConfigRequest\n ): Promise<DatabaseConfigEntry> {\n await this.ensureInitialized();\n\n const now = new Date();\n const nowString = now.toISOString();\n const configWithEncryptedPassword = this.encryptPasswordInConfig(data.config);\n\n await this.pool.query(\n `\n INSERT INTO lattice_database_configs (id, tenant_id, key, name, description, config, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7::timestamp, $8::timestamp)\n ON CONFLICT (tenant_id, id) DO UPDATE SET\n key = EXCLUDED.key,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n config = EXCLUDED.config,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.key,\n data.name || null,\n data.description || null,\n JSON.stringify(configWithEncryptedPassword),\n nowString,\n nowString,\n ]\n );\n\n return {\n id,\n tenantId,\n key: data.key,\n config: data.config,\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing database configuration\n */\n async updateConfig(\n tenantId: string,\n id: string,\n updates: Partial<UpdateDatabaseConfigRequest>\n ): Promise<DatabaseConfigEntry | null> {\n await this.ensureInitialized();\n\n const existing = await this.getConfigById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update fields and values as key-value pairs for clarity\n const updateData: Record<string, any> = {};\n\n if (updates.key !== undefined) {\n updateData.key = updates.key;\n }\n\n if (updates.name !== undefined) {\n updateData.name = updates.name || null;\n }\n\n if (updates.description !== undefined) {\n updateData.description = updates.description || null;\n }\n\n if (updates.config !== undefined) {\n const configWithEncryptedPassword = this.encryptPasswordInConfig(updates.config);\n updateData.config = JSON.stringify(configWithEncryptedPassword);\n }\n\n if (Object.keys(updateData).length === 0) {\n return existing;\n }\n\n // Always update the updated_at timestamp\n updateData.updated_at = new Date().toISOString();\n\n // Build SQL with tenantId and id at fixed positions at the end\n const fields = Object.keys(updateData);\n const values = Object.values(updateData);\n \n // Add WHERE clause values at the end\n values.push(tenantId);\n values.push(id);\n\n // Build SET clause with numbered parameters\n const setClauses = fields.map((field, index) => {\n if (field === 'updated_at') {\n return `${field} = $${index + 1}::timestamp`;\n }\n return `${field} = $${index + 1}`;\n });\n\n // WHERE clause parameters are at the end\n const whereTenantIndex = fields.length + 1;\n const whereIdIndex = fields.length + 2;\n\n const sql = `\n UPDATE lattice_database_configs\n SET ${setClauses.join(', ')}\n WHERE tenant_id = $${whereTenantIndex} AND id = $${whereIdIndex}\n `;\n\n await this.pool.query(sql, values);\n\n return await this.getConfigById(tenantId, id);\n }\n\n /**\n * Delete a database configuration by ID\n */\n async deleteConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_database_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if configuration exists\n */\n async hasConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_database_configs\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to DatabaseConfigEntry object\n * Automatically decrypts password if present\n */\n private mapRowToEntry(row: {\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }): DatabaseConfigEntry {\n const config: DatabaseConfig = typeof row.config === 'string'\n ? JSON.parse(row.config)\n : row.config;\n\n // Decrypt password if present\n if (config.password) {\n try {\n config.password = decrypt(config.password);\n } catch (error) {\n console.error('Failed to decrypt database password:', error);\n throw new Error('Failed to decrypt database configuration');\n }\n }\n\n return {\n id: row.id,\n tenantId: row.tenant_id,\n key: row.key,\n config,\n name: row.name || undefined,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Encrypt password in config before storing\n */\n private encryptPasswordInConfig(config: DatabaseConfig): DatabaseConfig {\n const configCopy = { ...config };\n \n if (configCopy.password) {\n configCopy.password = encrypt(configCopy.password);\n }\n \n return configCopy;\n }\n}\n","/**\n * Database configuration table migrations\n */\n\nimport { PoolClient } from 'pg';\nimport { Migration } from './migration';\n\n/**\n * Initial migration: Create database configs table\n */\nexport const createDatabaseConfigsTable: Migration = {\n version: 4,\n name: 'create_database_configs_table',\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_database_configs (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n key VARCHAR(255) NOT NULL,\n name VARCHAR(255),\n description TEXT,\n config JSONB NOT NULL,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n \n PRIMARY KEY (tenant_id, id),\n CONSTRAINT uk_lattice_database_configs_tenant_key UNIQUE (tenant_id, key)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_database_configs_tenant_id \n ON lattice_database_configs(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_database_configs_key \n ON lattice_database_configs(key)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query('DROP INDEX IF EXISTS idx_lattice_database_configs_key');\n await client.query('DROP INDEX IF EXISTS idx_lattice_database_configs_tenant_id');\n await client.query('DROP TABLE IF EXISTS lattice_database_configs');\n },\n};\n","/**\n * PostgreSQL implementation of MetricsServerConfigStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from 'pg';\nimport {\n MetricsServerConfigStore,\n MetricsServerConfigEntry,\n CreateMetricsServerConfigRequest,\n UpdateMetricsServerConfigRequest,\n MetricsServerConfig,\n} from '@axiom-lattice/protocols';\nimport { MigrationManager } from '../migrations/migration';\nimport { createMetricsConfigsTable } from '../migrations/metrics_config_migrations';\nimport { encrypt, decrypt } from '@axiom-lattice/core';\n\n/**\n * PostgreSQL MetricsServerConfigStore options\n */\nexport interface PostgreSQLMetricsServerConfigStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of MetricsServerConfigStore\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n * - Automatic apiKey and password encryption/decryption\n * - Unique constraint on (tenant_id, key)\n */\nexport class PostgreSQLMetricsServerConfigStore implements MetricsServerConfigStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLMetricsServerConfigStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === 'string') {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createMetricsConfigsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error('Failed to initialize PostgreSQLMetricsServerConfigStore:', error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all metrics server configurations for a tenant\n */\n async getAllConfigs(tenantId: string): Promise<MetricsServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get all metrics server configurations across all tenants\n */\n async getAllConfigsWithoutTenant(): Promise<MetricsServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get metrics server configuration by ID\n */\n async getConfigById(tenantId: string, id: string): Promise<MetricsServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Get metrics server configuration by business key\n */\n async getConfigByKey(tenantId: string, key: string): Promise<MetricsServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND key = $2\n `,\n [tenantId, key]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Create a new metrics server configuration\n */\n async createConfig(\n tenantId: string,\n id: string,\n data: CreateMetricsServerConfigRequest\n ): Promise<MetricsServerConfigEntry> {\n await this.ensureInitialized();\n\n const now = new Date();\n const nowString = now.toISOString();\n const configWithEncryptedSecrets = this.encryptSecretsInConfig(data.config);\n\n await this.pool.query(\n `\n INSERT INTO lattice_metrics_configs (id, tenant_id, key, name, description, config, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7::timestamp, $8::timestamp)\n ON CONFLICT (tenant_id, id) DO UPDATE SET\n key = EXCLUDED.key,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n config = EXCLUDED.config,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.key,\n data.name || null,\n data.description || null,\n JSON.stringify(configWithEncryptedSecrets),\n nowString,\n nowString,\n ]\n );\n\n return {\n id,\n tenantId,\n key: data.key,\n config: data.config,\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing metrics server configuration\n */\n async updateConfig(\n tenantId: string,\n id: string,\n updates: Partial<UpdateMetricsServerConfigRequest>\n ): Promise<MetricsServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const existing = await this.getConfigById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update fields and values as key-value pairs for clarity\n const updateData: Record<string, any> = {};\n\n if (updates.key !== undefined) {\n updateData.key = updates.key;\n }\n\n if (updates.name !== undefined) {\n updateData.name = updates.name || null;\n }\n\n if (updates.description !== undefined) {\n updateData.description = updates.description || null;\n }\n\n if (updates.config !== undefined) {\n const configWithEncryptedSecrets = this.encryptSecretsInConfig(updates.config);\n updateData.config = JSON.stringify(configWithEncryptedSecrets);\n }\n\n if (Object.keys(updateData).length === 0) {\n return existing;\n }\n\n // Always update the updated_at timestamp\n updateData.updated_at = new Date().toISOString();\n\n // Build SQL with tenantId and id at fixed positions at the end\n const fields = Object.keys(updateData);\n const values = Object.values(updateData);\n \n // Add WHERE clause values at the end\n values.push(tenantId);\n values.push(id);\n\n // Build SET clause with numbered parameters\n const setClauses = fields.map((field, index) => {\n if (field === 'updated_at') {\n return `${field} = $${index + 1}::timestamp`;\n }\n return `${field} = $${index + 1}`;\n });\n\n // WHERE clause parameters are at the end\n const whereTenantIndex = fields.length + 1;\n const whereIdIndex = fields.length + 2;\n\n const sql = `\n UPDATE lattice_metrics_configs\n SET ${setClauses.join(', ')}\n WHERE tenant_id = $${whereTenantIndex} AND id = $${whereIdIndex}\n `;\n\n await this.pool.query(sql, values);\n\n return await this.getConfigById(tenantId, id);\n }\n\n /**\n * Delete a metrics server configuration by ID\n */\n async deleteConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if configuration exists\n */\n async hasConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to MetricsServerConfigEntry object\n * Automatically decrypts apiKey and password if present\n */\n private mapRowToEntry(row: {\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }): MetricsServerConfigEntry {\n const config: MetricsServerConfig = typeof row.config === 'string'\n ? JSON.parse(row.config)\n : row.config;\n\n // Decrypt apiKey if present\n if (config.apiKey) {\n try {\n config.apiKey = decrypt(config.apiKey);\n } catch (error) {\n console.error('Failed to decrypt apiKey:', error);\n throw new Error('Failed to decrypt metrics server configuration');\n }\n }\n\n // Decrypt password if present\n if (config.password) {\n try {\n config.password = decrypt(config.password);\n } catch (error) {\n console.error('Failed to decrypt password:', error);\n throw new Error('Failed to decrypt metrics server configuration');\n }\n }\n\n return {\n id: row.id,\n tenantId: row.tenant_id,\n key: row.key,\n config,\n name: row.name || undefined,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Encrypt apiKey and password in config before storing\n */\n private encryptSecretsInConfig(config: MetricsServerConfig): MetricsServerConfig {\n const configCopy = { ...config };\n \n if (configCopy.apiKey) {\n configCopy.apiKey = encrypt(configCopy.apiKey);\n }\n \n if (configCopy.password) {\n configCopy.password = encrypt(configCopy.password);\n }\n \n return configCopy;\n }\n}\n","/**\n * Metrics server configuration table migrations\n */\n\nimport { PoolClient } from 'pg';\nimport { Migration } from './migration';\n\n/**\n * Initial migration: Create metrics configs table\n */\nexport const createMetricsConfigsTable: Migration = {\n version: 12,\n name: 'create_metrics_configs_table',\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_metrics_configs (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n key VARCHAR(255) NOT NULL,\n name VARCHAR(255),\n description TEXT,\n config JSONB NOT NULL,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n \n PRIMARY KEY (tenant_id, id),\n CONSTRAINT uk_lattice_metrics_configs_tenant_key UNIQUE (tenant_id, key)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_metrics_configs_tenant_id \n ON lattice_metrics_configs(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_metrics_configs_key \n ON lattice_metrics_configs(key)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query('DROP INDEX IF EXISTS idx_lattice_metrics_configs_key');\n await client.query('DROP INDEX IF EXISTS idx_lattice_metrics_configs_tenant_id');\n await client.query('DROP TABLE IF EXISTS lattice_metrics_configs');\n },\n};\n","/**\n * PostgreSQL implementation of McpServerConfigStore\n */\n\nimport { Pool, PoolConfig } from 'pg';\nimport {\n McpServerConfigStore,\n McpServerConfigEntry,\n CreateMcpServerConfigRequest,\n UpdateMcpServerConfigRequest,\n McpServerConfig,\n} from '@axiom-lattice/protocols';\nimport { MigrationManager } from '../migrations/migration';\nimport { createMcpServerConfigsTable } from '../migrations/mcp_server_config_migrations';\nimport { encrypt, decrypt } from '@axiom-lattice/core';\n\n/**\n * PostgreSQL McpServerConfigStore options\n */\nexport interface PostgreSQLMcpServerConfigStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of McpServerConfigStore\n *\n * Features:\n * - Multi-tenant isolation via tenant_id\n * - Automatic env encryption/decryption\n * - Unique constraint on (tenant_id, key)\n */\nexport class PostgreSQLMcpServerConfigStore implements McpServerConfigStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLMcpServerConfigStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === 'string') {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createMcpServerConfigsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error('Failed to initialize PostgreSQLMcpServerConfigStore:', error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all MCP server configurations for a tenant\n */\n async getAllConfigs(tenantId: string): Promise<McpServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get all MCP server configurations across all tenants\n */\n async getAllConfigsWithoutTenant(): Promise<McpServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get MCP server configuration by ID\n */\n async getConfigById(tenantId: string, id: string): Promise<McpServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Get MCP server configuration by business key\n */\n async getConfigByKey(tenantId: string, key: string): Promise<McpServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND key = $2\n `,\n [tenantId, key]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Create a new MCP server configuration\n */\n async createConfig(\n tenantId: string,\n id: string,\n data: CreateMcpServerConfigRequest\n ): Promise<McpServerConfigEntry> {\n await this.ensureInitialized();\n\n const now = new Date();\n const nowString = now.toISOString();\n const { config: configWithEncryptedEnv, isEnvEncrypted } = this.encryptEnvInConfig(data.config);\n\n await this.pool.query(\n `\n INSERT INTO lattice_mcp_server_configs (id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::timestamp, $11::timestamp)\n ON CONFLICT (tenant_id, id) DO UPDATE SET\n key = EXCLUDED.key,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n config = EXCLUDED.config,\n selected_tools = EXCLUDED.selected_tools,\n is_env_encrypted = EXCLUDED.is_env_encrypted,\n status = EXCLUDED.status,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.key,\n data.name || null,\n data.description || null,\n JSON.stringify(configWithEncryptedEnv),\n data.selectedTools || [],\n isEnvEncrypted,\n 'disconnected',\n nowString,\n nowString,\n ]\n );\n\n return {\n id,\n tenantId,\n key: data.key,\n config: data.config,\n selectedTools: data.selectedTools || [],\n isEnvEncrypted,\n status: 'disconnected',\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing MCP server configuration\n */\n async updateConfig(\n tenantId: string,\n id: string,\n updates: Partial<UpdateMcpServerConfigRequest>\n ): Promise<McpServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const existing = await this.getConfigById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update fields and values as key-value pairs for clarity\n const updateData: Record<string, any> = {};\n\n if (updates.key !== undefined) {\n updateData.key = updates.key;\n }\n\n if (updates.name !== undefined) {\n updateData.name = updates.name || null;\n }\n\n if (updates.description !== undefined) {\n updateData.description = updates.description || null;\n }\n\n if (updates.config !== undefined) {\n const { config: configWithEncryptedEnv, isEnvEncrypted } = this.encryptEnvInConfig(updates.config);\n updateData.config = JSON.stringify(configWithEncryptedEnv);\n updateData.is_env_encrypted = isEnvEncrypted;\n }\n\n if (updates.selectedTools !== undefined) {\n updateData.selected_tools = updates.selectedTools;\n }\n\n if (updates.status !== undefined) {\n updateData.status = updates.status;\n }\n\n if (Object.keys(updateData).length === 0) {\n return existing;\n }\n\n // Always update the updated_at timestamp\n updateData.updated_at = new Date().toISOString();\n\n // Build SQL with tenantId and id at fixed positions at the end\n const fields = Object.keys(updateData);\n const values = Object.values(updateData);\n\n // Add WHERE clause values at the end\n values.push(tenantId);\n values.push(id);\n\n // Build SET clause with numbered parameters\n const setClauses = fields.map((field, index) => {\n if (field === 'updated_at') {\n return `${field} = $${index + 1}::timestamp`;\n }\n return `${field} = $${index + 1}`;\n });\n\n // WHERE clause parameters are at the end\n const whereTenantIndex = fields.length + 1;\n const whereIdIndex = fields.length + 2;\n\n const sql = `\n UPDATE lattice_mcp_server_configs\n SET ${setClauses.join(', ')}\n WHERE tenant_id = $${whereTenantIndex} AND id = $${whereIdIndex}\n `;\n\n await this.pool.query(sql, values);\n\n return await this.getConfigById(tenantId, id);\n }\n\n /**\n * Delete a MCP server configuration by ID\n */\n async deleteConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if configuration exists\n */\n async hasConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to McpServerConfigEntry object\n * Automatically decrypts env values if present\n */\n private mapRowToEntry(row: {\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }): McpServerConfigEntry {\n const config: McpServerConfig = typeof row.config === 'string'\n ? JSON.parse(row.config)\n : row.config;\n\n // Decrypt env values if present and encrypted\n if (config.env && row.is_env_encrypted) {\n try {\n const decryptedEnv: Record<string, string> = {};\n for (const [key, value] of Object.entries(config.env)) {\n decryptedEnv[key] = decrypt(value);\n }\n config.env = decryptedEnv;\n } catch (error) {\n console.error('Failed to decrypt MCP server env:', error);\n throw new Error('Failed to decrypt MCP server configuration');\n }\n }\n\n return {\n id: row.id,\n tenantId: row.tenant_id,\n key: row.key,\n config,\n selectedTools: row.selected_tools || [],\n isEnvEncrypted: row.is_env_encrypted,\n status: row.status as 'disconnected' | 'connected' | 'error',\n name: row.name || undefined,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Encrypt env values in config before storing\n */\n private encryptEnvInConfig(config: McpServerConfig): { config: McpServerConfig; isEnvEncrypted: boolean } {\n const configCopy = { ...config };\n let isEnvEncrypted = false;\n\n if (configCopy.env && Object.keys(configCopy.env).length > 0) {\n const encryptedEnv: Record<string, string> = {};\n for (const [key, value] of Object.entries(configCopy.env)) {\n encryptedEnv[key] = encrypt(value);\n }\n configCopy.env = encryptedEnv;\n isEnvEncrypted = true;\n }\n\n return { config: configCopy, isEnvEncrypted };\n }\n}\n","/**\n * MCP Server configuration table migrations\n */\n\nimport { PoolClient } from 'pg';\nimport { Migration } from './migration';\n\n/**\n * Migration: Create MCP server configs table\n */\nexport const createMcpServerConfigsTable: Migration = {\n version: 13, // Use next available version number\n name: 'create_mcp_server_configs_table',\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_mcp_server_configs (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n key VARCHAR(255) NOT NULL,\n name VARCHAR(255),\n description TEXT,\n config JSONB NOT NULL,\n selected_tools JSONB NOT NULL DEFAULT '[]',\n is_env_encrypted BOOLEAN NOT NULL DEFAULT FALSE,\n status VARCHAR(50) NOT NULL DEFAULT 'disconnected',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n \n PRIMARY KEY (tenant_id, id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_mcp_server_configs_tenant_id \n ON lattice_mcp_server_configs(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_mcp_server_configs_key \n ON lattice_mcp_server_configs(key)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_mcp_server_configs_status \n ON lattice_mcp_server_configs(status)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query('DROP INDEX IF EXISTS idx_lattice_mcp_server_configs_status');\n await client.query('DROP INDEX IF EXISTS idx_lattice_mcp_server_configs_key');\n await client.query('DROP INDEX IF EXISTS idx_lattice_mcp_server_configs_tenant_id');\n await client.query('DROP TABLE IF EXISTS lattice_mcp_server_configs');\n },\n};\n","/**\n * PostgreSQL implementation of WorkspaceStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n WorkspaceStore,\n Workspace,\n CreateWorkspaceRequest,\n UpdateWorkspaceRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createWorkspacesTable } from \"../migrations/workspace_migrations\";\n\n/**\n * PostgreSQL WorkspaceStore options\n */\nexport interface PostgreSQLWorkspaceStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of WorkspaceStore\n */\nexport class PostgreSQLWorkspaceStore implements WorkspaceStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLWorkspaceStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createWorkspacesTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLWorkspaceStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Workspace object\n */\n private mapRowToWorkspace(row: {\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n storage_type: string;\n created_at: Date;\n updated_at: Date;\n }): Workspace {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n name: row.name,\n description: row.description || undefined,\n storageType: row.storage_type as Workspace[\"storageType\"],\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all workspaces for a tenant\n */\n async getAllWorkspaces(tenantId: string): Promise<Workspace[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n storage_type: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, storage_type, created_at, updated_at\n FROM lattice_workspaces\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map(this.mapRowToWorkspace);\n }\n\n /**\n * Get a workspace by ID for a specific tenant\n */\n async getWorkspaceById(tenantId: string, id: string): Promise<Workspace | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n storage_type: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, storage_type, created_at, updated_at\n FROM lattice_workspaces\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToWorkspace(result.rows[0]);\n }\n\n /**\n * Create a new workspace\n */\n async createWorkspace(\n tenantId: string,\n id: string,\n data: CreateWorkspaceRequest\n ): Promise<Workspace> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_workspaces (id, tenant_id, name, description, storage_type, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id, tenant_id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n storage_type = EXCLUDED.storage_type,\n updated_at = EXCLUDED.updated_at\n `,\n [id, tenantId, data.name, data.description || null, data.storageType, now, now]\n );\n\n return {\n id,\n tenantId,\n name: data.name,\n description: data.description,\n storageType: data.storageType,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing workspace\n */\n async updateWorkspace(\n tenantId: string,\n id: string,\n updates: UpdateWorkspaceRequest\n ): Promise<Workspace | null> {\n await this.ensureInitialized();\n\n // Get existing workspace\n const existing = await this.getWorkspaceById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updates.storageType !== undefined) {\n updateFields.push(`storage_type = $${paramIndex++}`);\n updateValues.push(updates.storageType);\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id and tenant_id for WHERE clause\n updateValues.push(id);\n updateValues.push(tenantId);\n\n await this.pool.query(\n `\n UPDATE lattice_workspaces\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex} AND tenant_id = $${paramIndex + 1}\n `,\n updateValues\n );\n\n // Return updated workspace\n return await this.getWorkspaceById(tenantId, id);\n }\n\n /**\n * Delete a workspace by ID\n */\n async deleteWorkspace(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_workspaces\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * Workspace table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create workspaces table\n */\nexport const createWorkspacesTable: Migration = {\n version: 5,\n name: \"create_workspaces_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_workspaces (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n storage_type VARCHAR(50) NOT NULL DEFAULT 'sandbox',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (id, tenant_id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_workspaces_tenant_id \n ON lattice_workspaces(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_workspaces_created_at \n ON lattice_workspaces(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_workspaces_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_workspaces_tenant_id\");\n await client.query(\"DROP TABLE IF EXISTS lattice_workspaces\");\n },\n};\n","/**\n * PostgreSQL implementation of ProjectStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n ProjectStore,\n Project,\n CreateProjectRequest,\n UpdateProjectRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createProjectsTable } from \"../migrations/project_migrations\";\n\n/**\n * PostgreSQL ProjectStore options\n */\nexport interface PostgreSQLProjectStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of ProjectStore\n */\nexport class PostgreSQLProjectStore implements ProjectStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLProjectStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createProjectsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLProjectStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Project object\n */\n private mapRowToProject(row: {\n id: string;\n tenant_id: string;\n workspace_id: string;\n name: string;\n description: string | null;\n created_at: Date;\n updated_at: Date;\n }): Project {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n workspaceId: row.workspace_id,\n name: row.name,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all projects for a specific workspace\n */\n async getProjectsByWorkspace(tenantId: string, workspaceId: string): Promise<Project[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n workspace_id: string;\n name: string;\n description: string | null;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, workspace_id, name, description, created_at, updated_at\n FROM lattice_projects\n WHERE tenant_id = $1 AND workspace_id = $2\n ORDER BY created_at DESC\n `,\n [tenantId, workspaceId]\n );\n\n return result.rows.map(this.mapRowToProject);\n }\n\n /**\n * Get a project by ID for a specific tenant\n */\n async getProjectById(tenantId: string, id: string): Promise<Project | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n workspace_id: string;\n name: string;\n description: string | null;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, workspace_id, name, description, created_at, updated_at\n FROM lattice_projects\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToProject(result.rows[0]);\n }\n\n /**\n * Create a new project\n */\n async createProject(\n tenantId: string,\n workspaceId: string,\n id: string,\n data: CreateProjectRequest\n ): Promise<Project> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_projects (id, tenant_id, workspace_id, name, description, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id, tenant_id) DO UPDATE SET\n workspace_id = EXCLUDED.workspace_id,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n updated_at = EXCLUDED.updated_at\n `,\n [id, tenantId, workspaceId, data.name, data.description || null, now, now]\n );\n\n return {\n id,\n tenantId,\n workspaceId,\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing project\n */\n async updateProject(\n tenantId: string,\n id: string,\n updates: UpdateProjectRequest\n ): Promise<Project | null> {\n await this.ensureInitialized();\n\n // Get existing project\n const existing = await this.getProjectById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id and tenant_id for WHERE clause\n updateValues.push(id);\n updateValues.push(tenantId);\n\n await this.pool.query(\n `\n UPDATE lattice_projects\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex} AND tenant_id = $${paramIndex + 1}\n `,\n updateValues\n );\n\n // Return updated project\n return await this.getProjectById(tenantId, id);\n }\n\n /**\n * Delete a project by ID\n */\n async deleteProject(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_projects\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * Project table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create projects table\n */\nexport const createProjectsTable: Migration = {\n version: 6,\n name: \"create_projects_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_projects (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n workspace_id VARCHAR(255) NOT NULL,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (id, tenant_id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_projects_tenant_id \n ON lattice_projects(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_projects_workspace_id \n ON lattice_projects(workspace_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_projects_created_at \n ON lattice_projects(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_projects_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_projects_workspace_id\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_projects_tenant_id\");\n await client.query(\"DROP TABLE IF EXISTS lattice_projects\");\n },\n};\n","import { Pool, PoolConfig } from \"pg\";\nimport { UserStore, User, CreateUserRequest, UpdateUserRequest } from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createUsersTable } from \"../migrations/user_migrations\";\nimport { addUserStatusColumn } from \"../migrations/user_status_migration\";\n\nexport interface PostgreSQLUserStoreOptions {\n poolConfig: string | PoolConfig;\n autoMigrate?: boolean;\n}\n\nexport class PostgreSQLUserStore implements UserStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n\n constructor(options: PostgreSQLUserStoreOptions) {\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createUsersTable);\n this.migrationManager.register(addUserStatusColumn);\n\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLUserStore:\", error);\n throw error;\n });\n }\n }\n\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) return;\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) await this.initialize();\n }\n\n private mapRowToUser(row: {\n id: string;\n email: string;\n name: string;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }): User {\n return {\n id: row.id,\n email: row.email,\n name: row.name,\n status: row.status as import(\"@axiom-lattice/protocols\").UserStatus,\n metadata: row.metadata,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n async getAllUsers(): Promise<User[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT id, email, name, status, metadata, created_at, updated_at\n FROM lattice_users\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map(this.mapRowToUser);\n }\n\n async getUserById(id: string): Promise<User | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT id, email, name, status, metadata, created_at, updated_at\n FROM lattice_users\n WHERE id = $1\n `,\n [id]\n );\n\n if (result.rows.length === 0) return null;\n return this.mapRowToUser(result.rows[0]);\n }\n\n async getUserByEmail(email: string): Promise<User | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT id, email, name, status, metadata, created_at, updated_at\n FROM lattice_users\n WHERE email = $1\n `,\n [email]\n );\n\n if (result.rows.length === 0) return null;\n return this.mapRowToUser(result.rows[0]);\n }\n\n async createUser(id: string, data: CreateUserRequest): Promise<User> {\n await this.ensureInitialized();\n\n const now = new Date();\n const metadata = data.metadata || {};\n const status = data.status || \"pending\";\n\n await this.pool.query(\n `\n INSERT INTO lattice_users (id, email, name, status, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id) DO UPDATE SET\n email = EXCLUDED.email,\n name = EXCLUDED.name,\n status = EXCLUDED.status,\n metadata = EXCLUDED.metadata,\n updated_at = EXCLUDED.updated_at\n `,\n [id, data.email, data.name, status, JSON.stringify(metadata), now, now]\n );\n\n return {\n id,\n email: data.email,\n name: data.name,\n status,\n metadata: data.metadata,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n async updateUser(id: string, updates: UpdateUserRequest): Promise<User | null> {\n await this.ensureInitialized();\n\n const existing = await this.getUserById(id);\n if (!existing) return null;\n\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.email !== undefined) {\n updateFields.push(`email = $${paramIndex++}`);\n updateValues.push(updates.email);\n }\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.status !== undefined) {\n updateFields.push(`status = $${paramIndex++}`);\n updateValues.push(updates.status);\n }\n\n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.metadata));\n }\n\n if (updateFields.length === 0) return existing;\n\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_users\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex}\n `,\n updateValues\n );\n\n return this.getUserById(id);\n }\n\n async deleteUser(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `DELETE FROM lattice_users WHERE id = $1`,\n [id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * User table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Create users table (multi-tenant refactor: removed tenant_id)\n */\nexport const createUsersTable: Migration = {\n version: 7,\n name: \"create_users_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_users (\n id VARCHAR(255) PRIMARY KEY,\n email VARCHAR(255) NOT NULL UNIQUE,\n name VARCHAR(255) NOT NULL,\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_users_email \n ON lattice_users(email)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_users_created_at \n ON lattice_users(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_users_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_users_email\");\n await client.query(\"DROP TABLE IF EXISTS lattice_users\");\n },\n};\n","import { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration to create lattice_users table with status support\n * Version 9: Adds status column to support user approval workflow\n */\nexport const addUserStatusColumn: Migration = {\n version: 9,\n name: \"add_user_status_column\",\n up: async (client: PoolClient) => {\n // Add status column if not exists\n await client.query(`\n ALTER TABLE lattice_users \n ADD COLUMN IF NOT EXISTS status VARCHAR(50) NOT NULL DEFAULT 'pending'\n `);\n \n // Create index on status for filtering\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_users_status \n ON lattice_users(tenant_id, status)\n `);\n \n // Migrate existing users to active status\n await client.query(`\n UPDATE lattice_users \n SET status = 'active' \n WHERE status = 'pending'\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(`\n DROP INDEX IF EXISTS idx_lattice_users_status\n `);\n await client.query(`\n ALTER TABLE lattice_users \n DROP COLUMN IF EXISTS status\n `);\n },\n};\n","/**\n * PostgreSQL implementation of TenantStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n TenantStore,\n Tenant,\n TenantStatus,\n CreateTenantRequest,\n UpdateTenantRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createTenantsTable } from \"../migrations/tenant_migrations\";\n\n/**\n * PostgreSQL TenantStore options\n */\nexport interface PostgreSQLTenantStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of TenantStore\n * Tenant is a top-level entity with single column primary key\n */\nexport class PostgreSQLTenantStore implements TenantStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLTenantStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createTenantsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLTenantStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Tenant object\n */\n private mapRowToTenant(row: {\n id: string;\n name: string;\n description: string | null;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }): Tenant {\n return {\n id: row.id,\n name: row.name,\n description: row.description || undefined,\n status: row.status as TenantStatus,\n metadata: row.metadata || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all tenants\n */\n async getAllTenants(): Promise<Tenant[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string | null;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }>(`\n SELECT id, name, description, status, metadata, created_at, updated_at\n FROM lattice_tenants\n ORDER BY created_at DESC\n `);\n\n return result.rows.map(this.mapRowToTenant);\n }\n\n /**\n * Get a tenant by ID\n */\n async getTenantById(id: string): Promise<Tenant | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string | null;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }>(`\n SELECT id, name, description, status, metadata, created_at, updated_at\n FROM lattice_tenants\n WHERE id = $1\n `, [id]);\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToTenant(result.rows[0]);\n }\n\n /**\n * Create a new tenant\n */\n async createTenant(\n id: string,\n data: CreateTenantRequest\n ): Promise<Tenant> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_tenants (id, name, description, status, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n status = EXCLUDED.status,\n metadata = EXCLUDED.metadata,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n data.name,\n data.description || null,\n data.status || \"active\",\n data.metadata || {},\n now,\n now,\n ]\n );\n\n return {\n id,\n name: data.name,\n description: data.description,\n status: data.status || \"active\",\n metadata: data.metadata,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing tenant\n */\n async updateTenant(\n id: string,\n updates: UpdateTenantRequest\n ): Promise<Tenant | null> {\n await this.ensureInitialized();\n\n // Get existing tenant\n const existing = await this.getTenantById(id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updates.status !== undefined) {\n updateFields.push(`status = $${paramIndex++}`);\n updateValues.push(updates.status);\n }\n\n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(updates.metadata);\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id for WHERE clause\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_tenants\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex}\n `,\n updateValues\n );\n\n // Return updated tenant\n return await this.getTenantById(id);\n }\n\n /**\n * Delete a tenant by ID\n */\n async deleteTenant(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_tenants\n WHERE id = $1\n `,\n [id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * Tenant table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Create tenants table\n * Tenant is a top-level entity with single column primary key\n */\nexport const createTenantsTable: Migration = {\n version: 8,\n name: \"create_tenants_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_tenants (\n id VARCHAR(255) PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n status VARCHAR(50) NOT NULL DEFAULT 'active',\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_tenants_status \n ON lattice_tenants(status)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_tenants_created_at \n ON lattice_tenants(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_tenants_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_tenants_status\");\n await client.query(\"DROP TABLE IF EXISTS lattice_tenants\");\n },\n};\n","import { Pool, PoolConfig } from \"pg\";\nimport {\n UserTenantLinkStore,\n UserTenantLink,\n CreateUserTenantLinkRequest,\n UpdateUserTenantLinkRequest,\n UserTenantRole,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createUserTenantLinkTable, migrateRemoveTenantIdFromUsers } from \"../migrations/user_tenant_link_migrations\";\n\nexport interface PostgreSQLUserTenantLinkStoreOptions {\n poolConfig: string | PoolConfig;\n autoMigrate?: boolean;\n}\n\nexport class PostgreSQLUserTenantLinkStore implements UserTenantLinkStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n\n constructor(options: PostgreSQLUserTenantLinkStoreOptions) {\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createUserTenantLinkTable);\n this.migrationManager.register(migrateRemoveTenantIdFromUsers);\n\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLUserTenantLinkStore:\", error);\n throw error;\n });\n }\n }\n\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) return;\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) await this.initialize();\n }\n\n private mapRowToLink(row: {\n user_id: string;\n tenant_id: string;\n role: string;\n joined_at: Date;\n metadata: Record<string, any>;\n }): UserTenantLink {\n return {\n userId: row.user_id,\n tenantId: row.tenant_id,\n role: row.role as UserTenantRole,\n joinedAt: row.joined_at,\n metadata: row.metadata,\n };\n }\n\n async getTenantsByUser(userId: string): Promise<UserTenantLink[]> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT user_id, tenant_id, role, joined_at, metadata\n FROM lattice_user_tenant_links\n WHERE user_id = $1\n ORDER BY joined_at DESC\n `,\n [userId]\n );\n \n return result.rows.map(this.mapRowToLink);\n }\n\n async getUsersByTenant(tenantId: string): Promise<UserTenantLink[]> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT user_id, tenant_id, role, joined_at, metadata\n FROM lattice_user_tenant_links\n WHERE tenant_id = $1\n ORDER BY joined_at DESC\n `,\n [tenantId]\n );\n \n return result.rows.map(this.mapRowToLink);\n }\n\n async getLink(userId: string, tenantId: string): Promise<UserTenantLink | null> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT user_id, tenant_id, role, joined_at, metadata\n FROM lattice_user_tenant_links\n WHERE user_id = $1 AND tenant_id = $2\n `,\n [userId, tenantId]\n );\n \n if (result.rows.length === 0) return null;\n return this.mapRowToLink(result.rows[0]);\n }\n\n async createLink(data: CreateUserTenantLinkRequest): Promise<UserTenantLink> {\n await this.ensureInitialized();\n \n const now = new Date();\n const role = data.role || \"member\";\n const metadata = data.metadata || {};\n \n await this.pool.query(\n `\n INSERT INTO lattice_user_tenant_links (user_id, tenant_id, role, joined_at, metadata)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (user_id, tenant_id) DO UPDATE SET\n role = EXCLUDED.role,\n metadata = EXCLUDED.metadata\n `,\n [data.userId, data.tenantId, role, now, JSON.stringify(metadata)]\n );\n \n return {\n userId: data.userId,\n tenantId: data.tenantId,\n role,\n joinedAt: now,\n metadata: data.metadata,\n };\n }\n\n async updateLink(\n userId: string,\n tenantId: string,\n updates: UpdateUserTenantLinkRequest\n ): Promise<UserTenantLink | null> {\n await this.ensureInitialized();\n \n const existing = await this.getLink(userId, tenantId);\n if (!existing) return null;\n \n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n \n if (updates.role !== undefined) {\n updateFields.push(`role = $${paramIndex++}`);\n updateValues.push(updates.role);\n }\n \n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.metadata));\n }\n \n if (updateFields.length === 0) return existing;\n \n updateValues.push(userId, tenantId);\n \n await this.pool.query(\n `\n UPDATE lattice_user_tenant_links\n SET ${updateFields.join(\", \")}\n WHERE user_id = $${paramIndex} AND tenant_id = $${paramIndex + 1}\n `,\n updateValues\n );\n \n return this.getLink(userId, tenantId);\n }\n\n async deleteLink(userId: string, tenantId: string): Promise<boolean> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n DELETE FROM lattice_user_tenant_links\n WHERE user_id = $1 AND tenant_id = $2\n `,\n [userId, tenantId]\n );\n \n return result.rowCount !== null && result.rowCount > 0;\n }\n\n async hasLink(userId: string, tenantId: string): Promise<boolean> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_user_tenant_links\n WHERE user_id = $1 AND tenant_id = $2\n LIMIT 1\n `,\n [userId, tenantId]\n );\n \n return result.rows.length > 0;\n }\n}\n","import { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\nexport const createUserTenantLinkTable: Migration = {\n version: 10,\n name: \"create_user_tenant_link_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_user_tenant_links (\n user_id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n role VARCHAR(50) NOT NULL DEFAULT 'member',\n joined_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n metadata JSONB DEFAULT '{}',\n PRIMARY KEY (user_id, tenant_id)\n )\n `);\n \n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_user_tenant_links_user_id \n ON lattice_user_tenant_links(user_id)\n `);\n \n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_user_tenant_links_tenant_id \n ON lattice_user_tenant_links(tenant_id)\n `);\n \n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_user_tenant_links_role \n ON lattice_user_tenant_links(tenant_id, role)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(`DROP TABLE IF EXISTS lattice_user_tenant_links`);\n },\n};\n\nexport const migrateRemoveTenantIdFromUsers: Migration = {\n version: 11,\n name: \"migrate_remove_tenant_id_from_users\",\n up: async (client: PoolClient) => {\n const hasTenantId = await client.query(`\n SELECT column_name \n FROM information_schema.columns \n WHERE table_name = 'lattice_users' AND column_name = 'tenant_id'\n `);\n \n if (hasTenantId.rows.length > 0) {\n await client.query(`\n INSERT INTO lattice_user_tenant_links (user_id, tenant_id, role, joined_at)\n SELECT id, tenant_id, 'member', created_at\n FROM lattice_users\n WHERE tenant_id IS NOT NULL\n ON CONFLICT (user_id, tenant_id) DO NOTHING\n `);\n \n await client.query(`\n ALTER TABLE lattice_users DROP COLUMN IF EXISTS tenant_id\n `);\n \n await client.query(`\n ALTER TABLE lattice_users DROP CONSTRAINT IF EXISTS lattice_users_pkey\n `);\n \n await client.query(`\n ALTER TABLE lattice_users ADD PRIMARY KEY (id)\n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C7B,QAAI,aAAa;AAEjB,QAAI,QAAQ,SAAS,QAAW;AAC9B,mBAAa,KAAK,WAAW,YAAY,EAAE;AAC3C,mBAAa,KAAK,QAAQ,IAAI;AAAA,IAChC;AAEA,QAAI,QAAQ,aAAa,QAAW;AAClC,mBAAa,KAAK,eAAe,YAAY,EAAE;AAC/C,mBAAa,KAAK,KAAK,UAAU,QAAQ,QAAQ,CAAC;AAAA,IACpD;AAEA,QAAI,aAAa,WAAW,EAAG,QAAO;AAEtC,iBAAa,KAAK,QAAQ,QAAQ;AAElC,UAAM,KAAK,KAAK;AAAA,MACd;AAAA;AAAA,cAEQ,aAAa,KAAK,IAAI,CAAC;AAAA,2BACV,UAAU,qBAAqB,aAAa,CAAC;AAAA;AAAA,MAElE;AAAA,IACF;AAEA,WAAO,KAAK,QAAQ,QAAQ,QAAQ;AAAA,EACtC;AAAA,EAEA,MAAM,WAAW,QAAgB,UAAoC;AACnE,UAAM,KAAK,kBAAkB;AAE7B,UAAM,SAAS,MAAM,KAAK,KAAK;AAAA,MAC7B;AAAA;AAAA;AAAA;AAAA,MAIA,CAAC,QAAQ,QAAQ;AAAA,IACnB;AAEA,WAAO,OAAO,aAAa,QAAQ,OAAO,WAAW;AAAA,EACvD;AAAA,EAEA,MAAM,QAAQ,QAAgB,UAAoC;AAChE,UAAM,KAAK,kBAAkB;AAE7B,UAAM,SAAS,MAAM,KAAK,KAAK;AAAA,MAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,MAKA,CAAC,QAAQ,QAAQ;AAAA,IACnB;AAEA,WAAO,OAAO,KAAK,SAAS;AAAA,EAC9B;AACF;","names":["import_pg","import_pg","import_pg","import_pg","import_pg","import_core","import_pg","import_core","import_pg","import_pg","import_pg","import_pg","import_pg"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/stores/PostgreSQLThreadStore.ts","../src/migrations/migration.ts","../src/migrations/thread_migrations.ts","../src/migrations/thread_tenant_migration.ts","../src/migrations/thread_pk_migration.ts","../src/stores/PostgreSQLAssistantStore.ts","../src/migrations/assistant_migrations.ts","../src/migrations/assistant_tenant_migration.ts","../src/migrations/assistant_pk_migration.ts","../src/stores/PostgreSQLScheduleStorage.ts","../src/migrations/schedule_migrations.ts","../src/migrations/schedule_tenant_migration.ts","../src/stores/PostgreSQLSkillStore.ts","../src/migrations/skill_migrations.ts","../src/migrations/skill_tenant_migration.ts","../src/migrations/skill_pk_migration.ts","../src/stores/PostgreSQLDatabaseConfigStore.ts","../src/migrations/database_config_migrations.ts","../src/stores/PostgreSQLMetricsServerConfigStore.ts","../src/migrations/metrics_config_migrations.ts","../src/stores/PostgreSQLMcpServerConfigStore.ts","../src/migrations/mcp_server_config_migrations.ts","../src/stores/PostgreSQLWorkspaceStore.ts","../src/migrations/workspace_migrations.ts","../src/stores/PostgreSQLProjectStore.ts","../src/migrations/project_migrations.ts","../src/stores/PostgreSQLUserStore.ts","../src/migrations/user_migrations.ts","../src/migrations/user_status_migration.ts","../src/stores/PostgreSQLTenantStore.ts","../src/migrations/tenant_migrations.ts","../src/stores/PostgreSQLUserTenantLinkStore.ts","../src/migrations/user_tenant_link_migrations.ts"],"sourcesContent":["/**\n * PostgreSQL Stores Package\n *\n * Provides PostgreSQL implementations for various stores\n */\n\nexport * from \"./stores/PostgreSQLThreadStore\";\nexport * from \"./stores/PostgreSQLAssistantStore\";\nexport * from \"./stores/PostgreSQLScheduleStorage\";\nexport * from \"./stores/PostgreSQLSkillStore\";\nexport * from \"./stores/PostgreSQLDatabaseConfigStore\";\nexport * from \"./stores/PostgreSQLMetricsServerConfigStore\";\nexport * from \"./stores/PostgreSQLMcpServerConfigStore\";\nexport * from \"./stores/PostgreSQLWorkspaceStore\";\nexport * from \"./stores/PostgreSQLProjectStore\";\nexport * from \"./stores/PostgreSQLUserStore\";\nexport * from \"./stores/PostgreSQLTenantStore\";\nexport * from \"./stores/PostgreSQLUserTenantLinkStore\";\nexport * from \"./migrations/migration\";\nexport * from \"./migrations/thread_migrations\";\nexport * from \"./migrations/thread_tenant_migration\";\nexport * from \"./migrations/thread_pk_migration\";\nexport * from \"./migrations/assistant_migrations\";\nexport * from \"./migrations/assistant_tenant_migration\";\nexport * from \"./migrations/assistant_pk_migration\";\nexport * from \"./migrations/schedule_migrations\";\nexport * from \"./migrations/schedule_tenant_migration\";\nexport * from \"./migrations/skill_migrations\";\nexport * from \"./migrations/skill_tenant_migration\";\nexport * from \"./migrations/skill_pk_migration\";\nexport * from \"./migrations/database_config_migrations\";\nexport * from \"./migrations/metrics_config_migrations\";\nexport * from \"./migrations/mcp_server_config_migrations\";\nexport * from \"./migrations/workspace_migrations\";\nexport * from \"./migrations/project_migrations\";\nexport * from \"./migrations/user_migrations\";\nexport * from \"./migrations/tenant_migrations\";\n\n// Re-export for convenience\nexport { PostgreSQLThreadStore } from \"./stores/PostgreSQLThreadStore\";\nexport { PostgreSQLAssistantStore } from \"./stores/PostgreSQLAssistantStore\";\nexport { PostgreSQLScheduleStorage } from \"./stores/PostgreSQLScheduleStorage\";\nexport { PostgreSQLSkillStore } from \"./stores/PostgreSQLSkillStore\";\nexport { PostgreSQLDatabaseConfigStore } from \"./stores/PostgreSQLDatabaseConfigStore\";\nexport { PostgreSQLMetricsServerConfigStore } from \"./stores/PostgreSQLMetricsServerConfigStore\";\nexport { PostgreSQLMcpServerConfigStore } from \"./stores/PostgreSQLMcpServerConfigStore\";\nexport { PostgreSQLWorkspaceStore } from \"./stores/PostgreSQLWorkspaceStore\";\nexport { PostgreSQLProjectStore } from \"./stores/PostgreSQLProjectStore\";\nexport { PostgreSQLUserStore } from \"./stores/PostgreSQLUserStore\";\nexport { PostgreSQLTenantStore } from \"./stores/PostgreSQLTenantStore\";\nexport { PostgreSQLUserTenantLinkStore } from \"./stores/PostgreSQLUserTenantLinkStore\";\n\n// Re-export types from protocols\nexport type {\n ThreadStore,\n Thread,\n CreateThreadRequest,\n AssistantStore,\n Assistant,\n CreateAssistantRequest,\n ScheduleStorage,\n ScheduledTaskDefinition,\n ScheduledTaskStatus,\n ScheduleExecutionType,\n SkillStore,\n Skill,\n CreateSkillRequest,\n DatabaseConfigStore,\n DatabaseConfigEntry,\n CreateDatabaseConfigRequest,\n UpdateDatabaseConfigRequest,\n DatabaseConfig,\n DatabaseType,\n MetricsServerConfigStore,\n MetricsServerConfigEntry,\n CreateMetricsServerConfigRequest,\n UpdateMetricsServerConfigRequest,\n MetricsServerConfig,\n McpServerConfigStore,\n McpServerConfigEntry,\n CreateMcpServerConfigRequest,\n UpdateMcpServerConfigRequest,\n WorkspaceStore,\n Workspace,\n CreateWorkspaceRequest,\n UpdateWorkspaceRequest,\n StorageType,\n ProjectStore,\n Project,\n CreateProjectRequest,\n UpdateProjectRequest,\n UserStore,\n User,\n CreateUserRequest,\n UpdateUserRequest,\n TenantStore,\n Tenant,\n TenantStatus,\n CreateTenantRequest,\n UpdateTenantRequest,\n UserTenantLinkStore,\n UserTenantLink,\n UserTenantRole,\n CreateUserTenantLinkRequest,\n UpdateUserTenantLinkRequest,\n} from \"@axiom-lattice/protocols\";\n","/**\n * PostgreSQL implementation of ThreadStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from \"pg\";\nimport {\n ThreadStore,\n Thread,\n CreateThreadRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createThreadsTable } from \"../migrations/thread_migrations\";\nimport { addThreadTenantId } from \"../migrations/thread_tenant_migration\";\nimport { changeThreadPrimaryKey } from \"../migrations/thread_pk_migration\";\n\n/**\n * PostgreSQL ThreadStore options\n */\nexport interface PostgreSQLThreadStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of ThreadStore\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n */\nexport class PostgreSQLThreadStore implements ThreadStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLThreadStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createThreadsTable);\n this.migrationManager.register(addThreadTenantId);\n this.migrationManager.register(changeThreadPrimaryKey);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLThreadStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all threads for a specific tenant and assistant\n */\n async getThreadsByAssistantId(tenantId: string, assistantId: string): Promise<Thread[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n assistant_id: string;\n metadata: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, assistant_id, metadata, created_at, updated_at\n FROM lattice_threads\n WHERE tenant_id = $1 AND assistant_id = $2\n ORDER BY created_at DESC\n `,\n [tenantId, assistantId]\n );\n\n return result.rows.map(this.mapRowToThread);\n }\n\n /**\n * Get a thread by ID for a specific tenant\n */\n async getThreadById(\n tenantId: string,\n threadId: string\n ): Promise<Thread | undefined> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n assistant_id: string;\n metadata: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, assistant_id, metadata, created_at, updated_at\n FROM lattice_threads\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, threadId]\n );\n\n if (result.rows.length === 0) {\n return undefined;\n }\n\n return this.mapRowToThread(result.rows[0]);\n }\n\n /**\n * Create a new thread for a tenant and assistant\n */\n async createThread(\n tenantId: string,\n assistantId: string,\n threadId: string,\n data: CreateThreadRequest\n ): Promise<Thread> {\n await this.ensureInitialized();\n\n const now = new Date();\n const metadata = data.metadata || {};\n\n await this.pool.query(\n `\n INSERT INTO lattice_threads (id, tenant_id, assistant_id, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6)\n ON CONFLICT (id, tenant_id) DO UPDATE SET\n assistant_id = EXCLUDED.assistant_id,\n metadata = EXCLUDED.metadata,\n updated_at = EXCLUDED.updated_at\n `,\n [threadId, tenantId, assistantId, JSON.stringify(metadata), now, now]\n );\n\n return {\n id: threadId,\n tenantId,\n assistantId,\n metadata,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing thread\n */\n async updateThread(\n tenantId: string,\n threadId: string,\n updates: Partial<CreateThreadRequest>\n ): Promise<Thread | null> {\n await this.ensureInitialized();\n\n // Get existing thread\n const existing = await this.getThreadById(tenantId, threadId);\n if (!existing) {\n return null;\n }\n\n // Merge metadata\n const updatedMetadata = {\n ...existing.metadata,\n ...(updates.metadata || {}),\n };\n\n const now = new Date();\n\n await this.pool.query(\n `\n UPDATE lattice_threads\n SET metadata = $1, updated_at = $2\n WHERE tenant_id = $3 AND id = $4\n `,\n [JSON.stringify(updatedMetadata), now, tenantId, threadId]\n );\n\n return {\n ...existing,\n metadata: updatedMetadata,\n updatedAt: now,\n };\n }\n\n /**\n * Delete a thread by ID\n */\n async deleteThread(tenantId: string, threadId: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_threads\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, threadId]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if thread exists\n */\n async hasThread(tenantId: string, threadId: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_threads\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, threadId]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Thread object\n */\n private mapRowToThread(row: {\n id: string;\n tenant_id: string;\n assistant_id: string;\n metadata: any;\n created_at: Date;\n updated_at: Date;\n }): Thread {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n assistantId: row.assistant_id,\n metadata:\n typeof row.metadata === \"string\"\n ? JSON.parse(row.metadata)\n : row.metadata || {},\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n}\n","/**\n * Migration system for database schema management\n */\n\nimport { Pool, PoolClient } from \"pg\";\n\n/**\n * Migration record stored in database\n */\ninterface MigrationRecord {\n version: number;\n name: string;\n applied_at: Date;\n}\n\n/**\n * Migration definition\n */\nexport interface Migration {\n version: number;\n name: string;\n up: (client: PoolClient) => Promise<void>;\n down?: (client: PoolClient) => Promise<void>;\n}\n\n/**\n * Migration manager\n */\nexport class MigrationManager {\n private pool: Pool;\n private migrations: Migration[] = [];\n\n constructor(pool: Pool) {\n this.pool = pool;\n }\n\n /**\n * Register a migration\n */\n register(migration: Migration): void {\n this.migrations.push(migration);\n // Sort migrations by version\n this.migrations.sort((a, b) => a.version - b.version);\n }\n\n /**\n * Initialize migrations table if it doesn't exist\n */\n private async ensureMigrationsTable(client: PoolClient): Promise<void> {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_schema_migrations (\n version INTEGER PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n }\n\n /**\n * Get applied migrations from database\n */\n private async getAppliedMigrations(\n client: PoolClient\n ): Promise<MigrationRecord[]> {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_schema_migrations (\n version INTEGER PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n const result = await client.query<MigrationRecord>(\n \"SELECT version, name, applied_at FROM lattice_schema_migrations ORDER BY version\"\n );\n return result.rows;\n }\n\n /**\n * Apply pending migrations\n * Uses PostgreSQL advisory locks to prevent concurrent migrations\n */\n async migrate(): Promise<void> {\n const client = await this.pool.connect();\n try {\n // Use advisory lock to prevent concurrent migrations\n // Lock ID: 123456789 (arbitrary but consistent)\n const lockId = 123456789;\n \n // Acquire advisory lock (blocks until lock is available)\n // This ensures only one migration runs at a time across all instances\n await client.query(\"SELECT pg_advisory_lock($1)\", [lockId]);\n \n try {\n await client.query(\"BEGIN\");\n\n await this.ensureMigrationsTable(client);\n const appliedMigrations = await this.getAppliedMigrations(client);\n const appliedVersions = new Set(appliedMigrations.map((m) => m.version));\n\n // Find pending migrations\n const pendingMigrations = this.migrations.filter(\n (m) => !appliedVersions.has(m.version)\n );\n\n if (pendingMigrations.length === 0) {\n console.log(\"No pending migrations\");\n await client.query(\"COMMIT\");\n return;\n }\n\n // Apply pending migrations\n for (const migration of pendingMigrations) {\n console.log(\n `Applying migration ${migration.version}: ${migration.name}`\n );\n await migration.up(client);\n // Use ON CONFLICT DO NOTHING to handle race conditions gracefully\n await client.query(\n \"INSERT INTO lattice_schema_migrations (version, name) VALUES ($1, $2) ON CONFLICT (version) DO NOTHING\",\n [migration.version, migration.name]\n );\n }\n\n await client.query(\"COMMIT\");\n console.log(`Applied ${pendingMigrations.length} migration(s)`);\n } catch (error) {\n await client.query(\"ROLLBACK\");\n throw error;\n } finally {\n // Release advisory lock\n await client.query(\"SELECT pg_advisory_unlock($1)\", [lockId]);\n }\n } catch (error) {\n throw error;\n } finally {\n client.release();\n }\n }\n\n /**\n * Rollback last migration\n */\n async rollback(): Promise<void> {\n const client = await this.pool.connect();\n try {\n await client.query(\"BEGIN\");\n\n const appliedMigrations = await this.getAppliedMigrations(client);\n if (appliedMigrations.length === 0) {\n console.log(\"No migrations to rollback\");\n await client.query(\"COMMIT\");\n return;\n }\n\n const lastMigration = appliedMigrations[appliedMigrations.length - 1];\n const migration = this.migrations.find(\n (m) => m.version === lastMigration.version\n );\n\n if (!migration || !migration.down) {\n throw new Error(\n `Migration ${lastMigration.version} does not have a down migration`\n );\n }\n\n console.log(\n `Rolling back migration ${lastMigration.version}: ${lastMigration.name}`\n );\n await migration.down(client);\n await client.query(\n \"DELETE FROM lattice_schema_migrations WHERE version = $1\",\n [lastMigration.version]\n );\n\n await client.query(\"COMMIT\");\n console.log(\"Rollback completed\");\n } catch (error) {\n await client.query(\"ROLLBACK\");\n throw error;\n } finally {\n client.release();\n }\n }\n\n /**\n * Get current migration version\n */\n async getCurrentVersion(): Promise<number> {\n const client = await this.pool.connect();\n try {\n const appliedMigrations = await this.getAppliedMigrations(client);\n if (appliedMigrations.length === 0) {\n return 0;\n }\n return Math.max(...appliedMigrations.map((m) => m.version));\n } finally {\n client.release();\n }\n }\n}\n","/**\n * Thread table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create threads table\n */\nexport const createThreadsTable: Migration = {\n version: 1,\n name: \"create_threads_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_threads (\n id VARCHAR(255) NOT NULL,\n assistant_id VARCHAR(255) NOT NULL,\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (id, assistant_id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_threads_assistant_id \n ON lattice_threads(assistant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_threads_created_at \n ON lattice_threads(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_threads_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_threads_assistant_id\");\n await client.query(\"DROP TABLE IF EXISTS lattice_threads\");\n },\n};\n","/**\n * Thread table tenant isolation migration\n * Adds tenant_id column to lattice_threads table\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Add tenant_id column to threads table\n */\nexport const addThreadTenantId: Migration = {\n version: 15,\n name: \"add_thread_tenant_id\",\n up: async (client: PoolClient) => {\n // Add tenant_id column\n await client.query(`\n ALTER TABLE lattice_threads\n ADD COLUMN IF NOT EXISTS tenant_id VARCHAR(255) DEFAULT 'default'\n `);\n\n // Update existing records to have tenant_id\n await client.query(`\n UPDATE lattice_threads \n SET tenant_id = 'default' \n WHERE tenant_id IS NULL\n `);\n\n // Make tenant_id NOT NULL after setting defaults\n await client.query(`\n ALTER TABLE lattice_threads\n ALTER COLUMN tenant_id SET NOT NULL\n `);\n\n // Drop the default constraint\n await client.query(`\n ALTER TABLE lattice_threads\n ALTER COLUMN tenant_id DROP DEFAULT\n `);\n\n // Create index for tenant-based queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_threads_tenant_id \n ON lattice_threads(tenant_id)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\n \"DROP INDEX IF EXISTS idx_lattice_threads_tenant_id\"\n );\n await client.query(\n \"ALTER TABLE lattice_threads DROP COLUMN IF EXISTS tenant_id\"\n );\n },\n};\n","/**\n * Thread table primary key migration\n * Changes primary key from (id, assistant_id) to (id, tenant_id) for multi-tenancy\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Change primary key to support multi-tenancy\n */\nexport const changeThreadPrimaryKey: Migration = {\n version: 19,\n name: \"change_thread_primary_key\",\n up: async (client: PoolClient) => {\n // Check if table exists\n const tableExists = await client.query(`\n SELECT EXISTS (\n SELECT FROM information_schema.tables \n WHERE table_name = 'lattice_threads'\n )\n `);\n\n if (!tableExists.rows[0].exists) {\n // Table doesn't exist, it will be created with correct schema\n return;\n }\n\n // Check current primary key\n const pkResult = await client.query(`\n SELECT a.attname as column_name\n FROM pg_index i\n JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)\n WHERE i.indrelid = 'lattice_threads'::regclass\n AND i.indisprimary\n `);\n\n const pkColumns = pkResult.rows.map(row => row.column_name);\n \n // If primary key is already (id, tenant_id), nothing to do\n if (pkColumns.includes('tenant_id') && pkColumns.includes('id') && pkColumns.length === 2) {\n console.log('Primary key already correct (id, tenant_id)');\n return;\n }\n\n // If primary key is (id, assistant_id), we need to change it\n if (pkColumns.includes('id') && pkColumns.includes('assistant_id')) {\n console.log('Changing primary key from (id, assistant_id) to (id, tenant_id)');\n \n // Check if tenant_id column exists\n const tenantIdExists = await client.query(`\n SELECT EXISTS (\n SELECT FROM information_schema.columns \n WHERE table_name = 'lattice_threads' \n AND column_name = 'tenant_id'\n )\n `);\n\n if (!tenantIdExists.rows[0].exists) {\n // Add tenant_id column if it doesn't exist\n await client.query(`\n ALTER TABLE lattice_threads\n ADD COLUMN tenant_id VARCHAR(255) DEFAULT 'default'\n `);\n }\n\n // Update existing records to have tenant_id\n await client.query(`\n UPDATE lattice_threads \n SET tenant_id = 'default' \n WHERE tenant_id IS NULL\n `);\n\n // Make tenant_id NOT NULL\n await client.query(`\n ALTER TABLE lattice_threads\n ALTER COLUMN tenant_id SET NOT NULL\n `);\n\n // Drop the old primary key\n await client.query(`\n ALTER TABLE lattice_threads \n DROP CONSTRAINT IF EXISTS lattice_threads_pkey\n `);\n\n // Add new composite primary key\n await client.query(`\n ALTER TABLE lattice_threads \n ADD PRIMARY KEY (id, tenant_id)\n `);\n\n console.log('Primary key changed successfully to (id, tenant_id)');\n }\n },\n down: async (client: PoolClient) => {\n // Reverting this is complex and potentially dangerous\n console.warn('Down migration for changeThreadPrimaryKey is not supported');\n },\n};\n","/**\n * PostgreSQL implementation of AssistantStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from \"pg\";\nimport {\n AssistantStore,\n Assistant,\n CreateAssistantRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createAssistantsTable } from \"../migrations/assistant_migrations\";\nimport { addAssistantTenantId } from \"../migrations/assistant_tenant_migration\";\nimport { changeAssistantPrimaryKey } from \"../migrations/assistant_pk_migration\";\n\n/**\n * PostgreSQL AssistantStore options\n */\nexport interface PostgreSQLAssistantStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of AssistantStore\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n */\nexport class PostgreSQLAssistantStore implements AssistantStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLAssistantStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createAssistantsTable);\n this.migrationManager.register(addAssistantTenantId);\n this.migrationManager.register(changeAssistantPrimaryKey);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLAssistantStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all assistants for a tenant\n */\n async getAllAssistants(tenantId: string): Promise<Assistant[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n graph_definition: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, graph_definition, created_at, updated_at\n FROM lattice_assistants\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map(this.mapRowToAssistant);\n }\n\n /**\n * Get assistant by ID\n */\n async getAssistantById(tenantId: string, id: string): Promise<Assistant | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n graph_definition: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, graph_definition, created_at, updated_at\n FROM lattice_assistants\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToAssistant(result.rows[0]);\n }\n\n /**\n * Create a new assistant\n */\n async createAssistant(\n tenantId: string,\n id: string,\n data: CreateAssistantRequest\n ): Promise<Assistant> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_assistants (id, tenant_id, name, description, graph_definition, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id) DO UPDATE SET\n tenant_id = EXCLUDED.tenant_id,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n graph_definition = EXCLUDED.graph_definition,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.name,\n data.description || null,\n JSON.stringify(data.graphDefinition),\n now,\n now,\n ]\n );\n\n return {\n id,\n tenantId,\n name: data.name,\n description: data.description,\n graphDefinition: data.graphDefinition,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing assistant\n */\n async updateAssistant(\n tenantId: string,\n id: string,\n updates: Partial<CreateAssistantRequest>\n ): Promise<Assistant | null> {\n await this.ensureInitialized();\n\n // Get existing assistant\n const existing = await this.getAssistantById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updates.graphDefinition !== undefined) {\n updateFields.push(`graph_definition = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.graphDefinition));\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add tenant_id and id for WHERE clause\n updateValues.push(tenantId);\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_assistants\n SET ${updateFields.join(\", \")}\n WHERE tenant_id = $${paramIndex++} AND id = $${paramIndex}\n `,\n updateValues\n );\n\n // Return updated assistant\n return await this.getAssistantById(tenantId, id);\n }\n\n /**\n * Delete an assistant by ID\n */\n async deleteAssistant(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_assistants\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if assistant exists\n */\n async hasAssistant(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_assistants\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Assistant object\n */\n private mapRowToAssistant(row: {\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n graph_definition: any;\n created_at: Date;\n updated_at: Date;\n }): Assistant {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n name: row.name,\n description: row.description || undefined,\n graphDefinition:\n typeof row.graph_definition === \"string\"\n ? JSON.parse(row.graph_definition)\n : row.graph_definition,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n}\n","/**\n * Assistant table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create assistants table\n */\nexport const createAssistantsTable: Migration = {\n version: 3,\n name: \"create_assistants_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_assistants (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL DEFAULT 'default',\n name VARCHAR(255) NOT NULL,\n description TEXT,\n graph_definition JSONB NOT NULL,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (tenant_id, id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_assistants_name \n ON lattice_assistants(name)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_assistants_created_at \n ON lattice_assistants(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\n \"DROP INDEX IF EXISTS idx_lattice_assistants_created_at\"\n );\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_assistants_name\");\n await client.query(\"DROP TABLE IF EXISTS lattice_assistants\");\n },\n};\n","/**\n * Assistant table tenant isolation migration\n * Adds tenant_id column to lattice_assistants table\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Add tenant_id column to assistants table\n */\nexport const addAssistantTenantId: Migration = {\n version: 14,\n name: \"add_assistant_tenant_id\",\n up: async (client: PoolClient) => {\n // tenant_id column is now created in the initial migration\n // This migration is kept for backward compatibility but does nothing\n // Create index for tenant-based queries if not exists\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_assistants_tenant_id \n ON lattice_assistants(tenant_id)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\n \"DROP INDEX IF EXISTS idx_lattice_assistants_tenant_id\"\n );\n },\n};\n","/**\n * Assistant table primary key migration\n * Changes primary key from (id) to (tenant_id, id) for multi-tenancy\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Change primary key to support multi-tenancy\n */\nexport const changeAssistantPrimaryKey: Migration = {\n version: 15,\n name: \"change_assistant_primary_key\",\n up: async (client: PoolClient) => {\n // Check if table exists and has the old primary key structure\n const tableExists = await client.query(`\n SELECT EXISTS (\n SELECT FROM information_schema.tables \n WHERE table_name = 'lattice_assistants'\n )\n `);\n\n if (!tableExists.rows[0].exists) {\n // Table doesn't exist, it will be created by createAssistantsTable migration\n // with the correct primary key (tenant_id, id)\n return;\n }\n\n // Check current primary key\n const pkResult = await client.query(`\n SELECT a.attname as column_name\n FROM pg_index i\n JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)\n WHERE i.indrelid = 'lattice_assistants'::regclass\n AND i.indisprimary\n `);\n\n const pkColumns = pkResult.rows.map(row => row.column_name);\n \n // If primary key is already (tenant_id, id), nothing to do\n if (pkColumns.includes('tenant_id') && pkColumns.includes('id')) {\n console.log('Primary key already correct (tenant_id, id)');\n return;\n }\n\n // If primary key is just (id), we need to change it\n if (pkColumns.length === 1 && pkColumns[0] === 'id') {\n console.log('Changing primary key from (id) to (tenant_id, id)');\n \n // Check if tenant_id column exists\n const tenantIdExists = await client.query(`\n SELECT EXISTS (\n SELECT FROM information_schema.columns \n WHERE table_name = 'lattice_assistants' \n AND column_name = 'tenant_id'\n )\n `);\n\n if (!tenantIdExists.rows[0].exists) {\n // Add tenant_id column if it doesn't exist\n await client.query(`\n ALTER TABLE lattice_assistants\n ADD COLUMN tenant_id VARCHAR(255) DEFAULT 'default'\n `);\n }\n\n // Update existing records to have tenant_id\n await client.query(`\n UPDATE lattice_assistants \n SET tenant_id = 'default' \n WHERE tenant_id IS NULL\n `);\n\n // Make tenant_id NOT NULL\n await client.query(`\n ALTER TABLE lattice_assistants\n ALTER COLUMN tenant_id SET NOT NULL\n `);\n\n // Drop the old primary key\n await client.query(`\n ALTER TABLE lattice_assistants \n DROP CONSTRAINT IF EXISTS lattice_assistants_pkey\n `);\n\n // Add new composite primary key\n await client.query(`\n ALTER TABLE lattice_assistants \n ADD PRIMARY KEY (tenant_id, id)\n `);\n\n console.log('Primary key changed successfully to (tenant_id, id)');\n }\n },\n down: async (client: PoolClient) => {\n // Reverting this is complex and potentially dangerous\n // as it would require ensuring no duplicate IDs across tenants\n console.warn('Down migration for changeAssistantPrimaryKey is not supported');\n },\n};\n","/**\n * PostgreSQL implementation of ScheduleStorage\n *\n * Provides persistent storage for scheduled tasks\n * Data survives service restarts\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n ScheduleStorage,\n ScheduledTaskDefinition,\n ScheduledTaskStatus,\n ScheduleExecutionType,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createScheduledTasksTable } from \"../migrations/schedule_migrations\";\nimport { addScheduleTenantId } from \"../migrations/schedule_tenant_migration\";\n\n/**\n * PostgreSQL ScheduleStorage options\n */\nexport interface PostgreSQLScheduleStorageOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * Database row type for scheduled tasks\n */\ninterface ScheduledTaskRow {\n task_id: string;\n task_type: string;\n payload: string | Record<string, any>;\n assistant_id: string | null;\n thread_id: string | null;\n execution_type: string;\n execute_at: Date | null;\n delay_ms: number | null;\n cron_expression: string | null;\n timezone: string | null;\n next_run_at: Date | null;\n last_run_at: Date | null;\n status: string;\n run_count: number;\n max_runs: number | null;\n retry_count: number;\n max_retries: number;\n last_error: string | null;\n created_at: Date;\n updated_at: Date;\n expires_at: Date | null;\n metadata: string | Record<string, any> | null;\n}\n\n/**\n * PostgreSQL implementation of ScheduleStorage\n */\nexport class PostgreSQLScheduleStorage implements ScheduleStorage {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n\n constructor(options: PostgreSQLScheduleStorageOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createScheduledTasksTable);\n this.migrationManager.register(addScheduleTenantId);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLScheduleStorage:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n if (this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Save a new task\n */\n async save(task: ScheduledTaskDefinition): Promise<void> {\n await this.ensureInitialized();\n\n await this.pool.query(\n `\n INSERT INTO lattice_scheduled_tasks (\n task_id, task_type, payload, assistant_id, thread_id, execution_type,\n execute_at, delay_ms, cron_expression, timezone,\n next_run_at, last_run_at, status, run_count, max_runs,\n retry_count, max_retries, last_error,\n created_at, updated_at, expires_at, metadata\n ) VALUES (\n $1, $2, $3, $4, $5, $6,\n $7, $8, $9, $10,\n $11, $12, $13, $14, $15,\n $16, $17, $18,\n $19, $20, $21, $22\n )\n ON CONFLICT (task_id) DO UPDATE SET\n task_type = EXCLUDED.task_type,\n payload = EXCLUDED.payload,\n assistant_id = EXCLUDED.assistant_id,\n thread_id = EXCLUDED.thread_id,\n execution_type = EXCLUDED.execution_type,\n execute_at = EXCLUDED.execute_at,\n delay_ms = EXCLUDED.delay_ms,\n cron_expression = EXCLUDED.cron_expression,\n timezone = EXCLUDED.timezone,\n next_run_at = EXCLUDED.next_run_at,\n last_run_at = EXCLUDED.last_run_at,\n status = EXCLUDED.status,\n run_count = EXCLUDED.run_count,\n max_runs = EXCLUDED.max_runs,\n retry_count = EXCLUDED.retry_count,\n max_retries = EXCLUDED.max_retries,\n last_error = EXCLUDED.last_error,\n updated_at = EXCLUDED.updated_at,\n expires_at = EXCLUDED.expires_at,\n metadata = EXCLUDED.metadata\n `,\n [\n task.taskId,\n task.taskType,\n JSON.stringify(task.payload),\n task.assistantId ?? null,\n task.threadId ?? null,\n task.executionType,\n task.executeAt ? new Date(task.executeAt) : null,\n task.delayMs ?? null,\n task.cronExpression ?? null,\n task.timezone ?? null,\n task.nextRunAt ? new Date(task.nextRunAt) : null,\n task.lastRunAt ? new Date(task.lastRunAt) : null,\n task.status,\n task.runCount,\n task.maxRuns ?? null,\n task.retryCount,\n task.maxRetries,\n task.lastError ?? null,\n new Date(task.createdAt),\n new Date(task.updatedAt),\n task.expiresAt ? new Date(task.expiresAt) : null,\n task.metadata ? JSON.stringify(task.metadata) : null,\n ]\n );\n }\n\n /**\n * Get task by ID\n */\n async get(taskId: string): Promise<ScheduledTaskDefinition | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE task_id = $1`,\n [taskId]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToTask(result.rows[0]);\n }\n\n /**\n * Update task\n */\n async update(\n taskId: string,\n updates: Partial<ScheduledTaskDefinition>\n ): Promise<void> {\n await this.ensureInitialized();\n\n const setClauses: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n // Build dynamic UPDATE query\n if (updates.taskType !== undefined) {\n setClauses.push(`task_type = $${paramIndex++}`);\n values.push(updates.taskType);\n }\n if (updates.payload !== undefined) {\n setClauses.push(`payload = $${paramIndex++}`);\n values.push(JSON.stringify(updates.payload));\n }\n if (updates.assistantId !== undefined) {\n setClauses.push(`assistant_id = $${paramIndex++}`);\n values.push(updates.assistantId);\n }\n if (updates.threadId !== undefined) {\n setClauses.push(`thread_id = $${paramIndex++}`);\n values.push(updates.threadId);\n }\n if (updates.executionType !== undefined) {\n setClauses.push(`execution_type = $${paramIndex++}`);\n values.push(updates.executionType);\n }\n if (updates.executeAt !== undefined) {\n setClauses.push(`execute_at = $${paramIndex++}`);\n values.push(updates.executeAt ? new Date(updates.executeAt) : null);\n }\n if (updates.delayMs !== undefined) {\n setClauses.push(`delay_ms = $${paramIndex++}`);\n values.push(updates.delayMs);\n }\n if (updates.cronExpression !== undefined) {\n setClauses.push(`cron_expression = $${paramIndex++}`);\n values.push(updates.cronExpression);\n }\n if (updates.timezone !== undefined) {\n setClauses.push(`timezone = $${paramIndex++}`);\n values.push(updates.timezone);\n }\n if (updates.nextRunAt !== undefined) {\n setClauses.push(`next_run_at = $${paramIndex++}`);\n values.push(updates.nextRunAt ? new Date(updates.nextRunAt) : null);\n }\n if (updates.lastRunAt !== undefined) {\n setClauses.push(`last_run_at = $${paramIndex++}`);\n values.push(updates.lastRunAt ? new Date(updates.lastRunAt) : null);\n }\n if (updates.status !== undefined) {\n setClauses.push(`status = $${paramIndex++}`);\n values.push(updates.status);\n }\n if (updates.runCount !== undefined) {\n setClauses.push(`run_count = $${paramIndex++}`);\n values.push(updates.runCount);\n }\n if (updates.maxRuns !== undefined) {\n setClauses.push(`max_runs = $${paramIndex++}`);\n values.push(updates.maxRuns);\n }\n if (updates.retryCount !== undefined) {\n setClauses.push(`retry_count = $${paramIndex++}`);\n values.push(updates.retryCount);\n }\n if (updates.maxRetries !== undefined) {\n setClauses.push(`max_retries = $${paramIndex++}`);\n values.push(updates.maxRetries);\n }\n if (updates.lastError !== undefined) {\n setClauses.push(`last_error = $${paramIndex++}`);\n values.push(updates.lastError);\n }\n if (updates.expiresAt !== undefined) {\n setClauses.push(`expires_at = $${paramIndex++}`);\n values.push(updates.expiresAt ? new Date(updates.expiresAt) : null);\n }\n if (updates.metadata !== undefined) {\n setClauses.push(`metadata = $${paramIndex++}`);\n values.push(updates.metadata ? JSON.stringify(updates.metadata) : null);\n }\n\n // Always update updated_at\n setClauses.push(`updated_at = $${paramIndex++}`);\n values.push(new Date());\n\n // Add taskId as the last parameter\n values.push(taskId);\n\n if (setClauses.length === 0) {\n return;\n }\n\n await this.pool.query(\n `UPDATE lattice_scheduled_tasks SET ${setClauses.join(\n \", \"\n )} WHERE task_id = $${paramIndex}`,\n values\n );\n }\n\n /**\n * Delete task\n */\n async delete(taskId: string): Promise<void> {\n await this.ensureInitialized();\n\n await this.pool.query(\n `DELETE FROM lattice_scheduled_tasks WHERE task_id = $1`,\n [taskId]\n );\n }\n\n /**\n * Get all active tasks (pending or paused)\n */\n async getActiveTasks(): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE status IN ($1, $2) ORDER BY created_at ASC`,\n [ScheduledTaskStatus.PENDING, ScheduledTaskStatus.PAUSED]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by type\n */\n async getTasksByType(taskType: string): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE task_type = $1 ORDER BY created_at DESC`,\n [taskType]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by status\n */\n async getTasksByStatus(\n status: ScheduledTaskStatus\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE status = $1 ORDER BY created_at DESC`,\n [status]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by execution type\n */\n async getTasksByExecutionType(\n executionType: ScheduleExecutionType\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE execution_type = $1 ORDER BY created_at DESC`,\n [executionType]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by assistant ID\n */\n async getTasksByAssistantId(\n assistantId: string\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE assistant_id = $1 ORDER BY created_at DESC`,\n [assistantId]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get tasks by thread ID\n */\n async getTasksByThreadId(\n threadId: string\n ): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<ScheduledTaskRow>(\n `SELECT * FROM lattice_scheduled_tasks WHERE thread_id = $1 ORDER BY created_at DESC`,\n [threadId]\n );\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Get all tasks with optional filters\n */\n async getAllTasks(filters?: {\n tenantId?: string;\n status?: ScheduledTaskStatus;\n executionType?: ScheduleExecutionType;\n taskType?: string;\n assistantId?: string;\n threadId?: string;\n limit?: number;\n offset?: number;\n }): Promise<ScheduledTaskDefinition[]> {\n await this.ensureInitialized();\n\n const whereClauses: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n // Always filter by tenantId if provided\n if (filters?.tenantId !== undefined) {\n whereClauses.push(`tenant_id = $${paramIndex++}`);\n values.push(filters.tenantId);\n }\n\n if (filters?.status !== undefined) {\n whereClauses.push(`status = $${paramIndex++}`);\n values.push(filters.status);\n }\n if (filters?.executionType !== undefined) {\n whereClauses.push(`execution_type = $${paramIndex++}`);\n values.push(filters.executionType);\n }\n if (filters?.taskType !== undefined) {\n whereClauses.push(`task_type = $${paramIndex++}`);\n values.push(filters.taskType);\n }\n if (filters?.assistantId !== undefined) {\n whereClauses.push(`assistant_id = $${paramIndex++}`);\n values.push(filters.assistantId);\n }\n if (filters?.threadId !== undefined) {\n whereClauses.push(`thread_id = $${paramIndex++}`);\n values.push(filters.threadId);\n }\n\n let query = `SELECT * FROM lattice_scheduled_tasks`;\n if (whereClauses.length > 0) {\n query += ` WHERE ${whereClauses.join(\" AND \")}`;\n }\n query += ` ORDER BY created_at DESC`;\n\n if (filters?.limit !== undefined) {\n query += ` LIMIT $${paramIndex++}`;\n values.push(filters.limit);\n }\n if (filters?.offset !== undefined) {\n query += ` OFFSET $${paramIndex++}`;\n values.push(filters.offset);\n }\n\n const result = await this.pool.query<ScheduledTaskRow>(query, values);\n\n return result.rows.map((row) => this.mapRowToTask(row));\n }\n\n /**\n * Count tasks with optional filters\n */\n async countTasks(filters?: {\n tenantId?: string;\n status?: ScheduledTaskStatus;\n executionType?: ScheduleExecutionType;\n taskType?: string;\n assistantId?: string;\n threadId?: string;\n }): Promise<number> {\n await this.ensureInitialized();\n\n const whereClauses: string[] = [];\n const values: any[] = [];\n let paramIndex = 1;\n\n // Always filter by tenantId if provided\n if (filters?.tenantId !== undefined) {\n whereClauses.push(`tenant_id = $${paramIndex++}`);\n values.push(filters.tenantId);\n }\n\n if (filters?.status !== undefined) {\n whereClauses.push(`status = $${paramIndex++}`);\n values.push(filters.status);\n }\n if (filters?.executionType !== undefined) {\n whereClauses.push(`execution_type = $${paramIndex++}`);\n values.push(filters.executionType);\n }\n if (filters?.taskType !== undefined) {\n whereClauses.push(`task_type = $${paramIndex++}`);\n values.push(filters.taskType);\n }\n if (filters?.assistantId !== undefined) {\n whereClauses.push(`assistant_id = $${paramIndex++}`);\n values.push(filters.assistantId);\n }\n if (filters?.threadId !== undefined) {\n whereClauses.push(`thread_id = $${paramIndex++}`);\n values.push(filters.threadId);\n }\n\n let query = `SELECT COUNT(*) as count FROM lattice_scheduled_tasks`;\n if (whereClauses.length > 0) {\n query += ` WHERE ${whereClauses.join(\" AND \")}`;\n }\n\n const result = await this.pool.query<{ count: string }>(query, values);\n\n return parseInt(result.rows[0].count, 10);\n }\n\n /**\n * Delete completed/cancelled/failed tasks older than specified time\n */\n async deleteOldTasks(olderThanMs: number): Promise<number> {\n await this.ensureInitialized();\n\n const cutoff = new Date(Date.now() - olderThanMs);\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_scheduled_tasks \n WHERE status IN ($1, $2, $3) \n AND updated_at < $4\n `,\n [\n ScheduledTaskStatus.COMPLETED,\n ScheduledTaskStatus.CANCELLED,\n ScheduledTaskStatus.FAILED,\n cutoff,\n ]\n );\n\n return result.rowCount ?? 0;\n }\n\n /**\n * Map database row to ScheduledTaskDefinition\n */\n private mapRowToTask(row: ScheduledTaskRow): ScheduledTaskDefinition {\n return {\n taskId: row.task_id,\n taskType: row.task_type,\n payload:\n typeof row.payload === \"string\"\n ? JSON.parse(row.payload)\n : row.payload || {},\n assistantId: row.assistant_id ?? undefined,\n threadId: row.thread_id ?? undefined,\n executionType: row.execution_type as ScheduleExecutionType,\n executeAt: row.execute_at ? row.execute_at.getTime() : undefined,\n delayMs: row.delay_ms ?? undefined,\n cronExpression: row.cron_expression ?? undefined,\n timezone: row.timezone ?? undefined,\n nextRunAt: row.next_run_at ? row.next_run_at.getTime() : undefined,\n lastRunAt: row.last_run_at ? row.last_run_at.getTime() : undefined,\n status: row.status as ScheduledTaskStatus,\n runCount: row.run_count,\n maxRuns: row.max_runs ?? undefined,\n retryCount: row.retry_count,\n maxRetries: row.max_retries,\n lastError: row.last_error ?? undefined,\n createdAt: row.created_at.getTime(),\n updatedAt: row.updated_at.getTime(),\n expiresAt: row.expires_at ? row.expires_at.getTime() : undefined,\n metadata:\n row.metadata === null\n ? undefined\n : typeof row.metadata === \"string\"\n ? JSON.parse(row.metadata)\n : row.metadata,\n };\n }\n}\n","/**\n * PostgreSQL migrations for scheduled tasks table\n */\n\nimport { Migration } from \"./migration\";\n\n/**\n * Create the scheduled_tasks table\n */\nexport const createScheduledTasksTable: Migration = {\n name: \"create_scheduled_tasks_table\",\n version: 2,\n\n up: async (client) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_scheduled_tasks (\n -- Primary key\n task_id VARCHAR(255) PRIMARY KEY,\n \n -- Task type (maps to handler)\n task_type VARCHAR(255) NOT NULL,\n \n -- Payload (JSON-serializable data)\n payload JSONB NOT NULL DEFAULT '{}',\n \n -- Context fields for querying\n assistant_id VARCHAR(255),\n thread_id VARCHAR(255),\n \n -- Execution type: 'once' or 'cron'\n execution_type VARCHAR(50) NOT NULL,\n \n -- For ONCE type\n execute_at TIMESTAMPTZ,\n delay_ms BIGINT,\n \n -- For CRON type\n cron_expression VARCHAR(255),\n timezone VARCHAR(100),\n next_run_at TIMESTAMPTZ,\n last_run_at TIMESTAMPTZ,\n \n -- Status\n status VARCHAR(50) NOT NULL DEFAULT 'pending',\n \n -- Execution tracking\n run_count INTEGER NOT NULL DEFAULT 0,\n max_runs INTEGER,\n \n -- Error handling\n retry_count INTEGER NOT NULL DEFAULT 0,\n max_retries INTEGER NOT NULL DEFAULT 0,\n last_error TEXT,\n \n -- Timestamps\n created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n expires_at TIMESTAMPTZ,\n \n -- Metadata\n metadata JSONB\n );\n\n -- Index for querying active tasks\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_status \n ON lattice_scheduled_tasks (status);\n \n -- Index for querying by task type\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_task_type \n ON lattice_scheduled_tasks (task_type);\n \n -- Index for querying by execution type\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_execution_type \n ON lattice_scheduled_tasks (execution_type);\n \n -- Index for querying by assistant ID\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_assistant_id \n ON lattice_scheduled_tasks (assistant_id)\n WHERE assistant_id IS NOT NULL;\n \n -- Index for querying by thread ID\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_thread_id \n ON lattice_scheduled_tasks (thread_id)\n WHERE thread_id IS NOT NULL;\n \n -- Index for querying pending tasks by next run time\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_next_run \n ON lattice_scheduled_tasks (next_run_at) \n WHERE status = 'pending';\n \n -- Index for querying pending one-time tasks by execute time\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_execute_at \n ON lattice_scheduled_tasks (execute_at) \n WHERE status = 'pending' AND execution_type = 'once';\n \n -- Index for cleanup queries (old completed/cancelled tasks)\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_cleanup \n ON lattice_scheduled_tasks (updated_at) \n WHERE status IN ('completed', 'cancelled', 'failed');\n `);\n },\n\n down: async (client) => {\n await client.query(`\n DROP INDEX IF EXISTS idx_scheduled_tasks_cleanup;\n DROP INDEX IF EXISTS idx_scheduled_tasks_execute_at;\n DROP INDEX IF EXISTS idx_scheduled_tasks_next_run;\n DROP INDEX IF EXISTS idx_scheduled_tasks_thread_id;\n DROP INDEX IF EXISTS idx_scheduled_tasks_assistant_id;\n DROP INDEX IF EXISTS idx_scheduled_tasks_execution_type;\n DROP INDEX IF EXISTS idx_scheduled_tasks_task_type;\n DROP INDEX IF EXISTS idx_scheduled_tasks_status;\n DROP TABLE IF EXISTS lattice_scheduled_tasks;\n `);\n },\n};\n","/**\n * Schedule table tenant isolation migration\n * Adds tenant_id column to lattice_scheduled_tasks table\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Add tenant_id column to scheduled tasks table\n */\nexport const addScheduleTenantId: Migration = {\n version: 18,\n name: \"add_schedule_tenant_id\",\n up: async (client: PoolClient) => {\n // Add tenant_id column\n await client.query(`\n ALTER TABLE lattice_scheduled_tasks\n ADD COLUMN IF NOT EXISTS tenant_id VARCHAR(255) DEFAULT 'default'\n `);\n\n // Update existing records to have tenant_id\n await client.query(`\n UPDATE lattice_scheduled_tasks \n SET tenant_id = 'default' \n WHERE tenant_id IS NULL\n `);\n\n // Make tenant_id NOT NULL after setting defaults\n await client.query(`\n ALTER TABLE lattice_scheduled_tasks\n ALTER COLUMN tenant_id SET NOT NULL\n `);\n\n // Drop the default constraint\n await client.query(`\n ALTER TABLE lattice_scheduled_tasks\n ALTER COLUMN tenant_id DROP DEFAULT\n `);\n\n // Create index for tenant-based queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_tenant_id \n ON lattice_scheduled_tasks(tenant_id)\n `);\n\n // Create composite index for common tenant+assistant queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_tenant_assistant \n ON lattice_scheduled_tasks(tenant_id, assistant_id)\n WHERE assistant_id IS NOT NULL\n `);\n\n // Create composite index for common tenant+thread queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_scheduled_tasks_tenant_thread \n ON lattice_scheduled_tasks(tenant_id, thread_id)\n WHERE thread_id IS NOT NULL\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\n \"DROP INDEX IF EXISTS idx_scheduled_tasks_tenant_thread\"\n );\n await client.query(\n \"DROP INDEX IF EXISTS idx_scheduled_tasks_tenant_assistant\"\n );\n await client.query(\n \"DROP INDEX IF EXISTS idx_scheduled_tasks_tenant_id\"\n );\n await client.query(\n \"ALTER TABLE lattice_scheduled_tasks DROP COLUMN IF EXISTS tenant_id\"\n );\n },\n};\n","/**\n * PostgreSQL implementation of SkillStore with tenant isolation\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n SkillStore,\n Skill,\n CreateSkillRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createSkillsTable } from \"../migrations/skill_migrations\";\nimport { addSkillTenantId } from \"../migrations/skill_tenant_migration\";\nimport { changeSkillPrimaryKey } from \"../migrations/skill_pk_migration\";\n\nexport interface PostgreSQLSkillStoreOptions {\n poolConfig: string | PoolConfig;\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of SkillStore with tenant isolation\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n */\nexport class PostgreSQLSkillStore implements SkillStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLSkillStoreOptions) {\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createSkillsTable);\n this.migrationManager.register(addSkillTenantId);\n this.migrationManager.register(changeSkillPrimaryKey);\n\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLSkillStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Skill object\n */\n private mapRowToSkill(row: {\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }): Skill {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n name: row.name,\n description: row.description,\n license: row.license || undefined,\n compatibility: row.compatibility || undefined,\n metadata: row.metadata || {},\n content: row.content || undefined,\n subSkills: Array.isArray(row.sub_skills) ? row.sub_skills : undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all skills for a tenant\n */\n async getAllSkills(tenantId: string): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Get skill by ID for a tenant\n */\n async getSkillById(tenantId: string, id: string): Promise<Skill | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToSkill(result.rows[0]);\n }\n\n /**\n * Create a new skill for a tenant\n */\n async createSkill(\n tenantId: string,\n id: string,\n data: CreateSkillRequest\n ): Promise<Skill> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_skills (id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)\n ON CONFLICT (id) DO UPDATE SET\n tenant_id = EXCLUDED.tenant_id,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n license = EXCLUDED.license,\n compatibility = EXCLUDED.compatibility,\n metadata = EXCLUDED.metadata,\n content = EXCLUDED.content,\n sub_skills = EXCLUDED.sub_skills,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.name,\n data.description,\n data.license || null,\n data.compatibility || null,\n JSON.stringify(data.metadata || {}),\n data.content || null,\n JSON.stringify(data.subSkills || []),\n now,\n now,\n ]\n );\n\n return {\n id,\n tenantId,\n name: data.name,\n description: data.description,\n license: data.license,\n compatibility: data.compatibility,\n metadata: data.metadata || {},\n content: data.content,\n subSkills: data.subSkills,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing skill for a tenant\n */\n async updateSkill(\n tenantId: string,\n id: string,\n updates: Partial<CreateSkillRequest>\n ): Promise<Skill | null> {\n await this.ensureInitialized();\n\n // Get existing skill\n const existing = await this.getSkillById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description);\n }\n\n if (updates.license !== undefined) {\n updateFields.push(`license = $${paramIndex++}`);\n updateValues.push(updates.license || null);\n }\n\n if (updates.compatibility !== undefined) {\n updateFields.push(`compatibility = $${paramIndex++}`);\n updateValues.push(updates.compatibility || null);\n }\n\n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.metadata || {}));\n }\n\n if (updates.content !== undefined) {\n updateFields.push(`content = $${paramIndex++}`);\n updateValues.push(updates.content || null);\n }\n\n if (updates.subSkills !== undefined) {\n updateFields.push(`sub_skills = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.subSkills || []));\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add tenant_id and id for WHERE clause\n updateValues.push(tenantId);\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_skills\n SET ${updateFields.join(\", \")}\n WHERE tenant_id = $${paramIndex++} AND id = $${paramIndex}\n `,\n updateValues\n );\n\n // Return updated skill\n return await this.getSkillById(tenantId, id);\n }\n\n /**\n * Delete a skill by ID for a tenant\n */\n async deleteSkill(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_skills\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if skill exists for a tenant\n */\n async hasSkill(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_skills\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Search skills by metadata within a tenant\n */\n async searchByMetadata(\n tenantId: string,\n metadataKey: string,\n metadataValue: string\n ): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE tenant_id = $1 AND metadata ->> $2 = $3\n ORDER BY created_at DESC\n `,\n [tenantId, metadataKey, metadataValue]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Filter skills by compatibility within a tenant\n */\n async filterByCompatibility(\n tenantId: string,\n compatibility: string\n ): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE tenant_id = $1 AND compatibility = $2\n ORDER BY created_at DESC\n `,\n [tenantId, compatibility]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Filter skills by license within a tenant\n */\n async filterByLicense(\n tenantId: string,\n license: string\n ): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE tenant_id = $1 AND license = $2\n ORDER BY created_at DESC\n `,\n [tenantId, license]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n\n /**\n * Get sub-skills of a parent skill within a tenant\n * Note: This searches for skills that have the parent skill name in their subSkills array\n */\n async getSubSkills(\n tenantId: string,\n parentSkillName: string\n ): Promise<Skill[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string;\n license: string | null;\n compatibility: string | null;\n metadata: any;\n content: string | null;\n sub_skills: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at\n FROM lattice_skills\n WHERE tenant_id = $1 AND sub_skills @> to_jsonb($2::text)\n ORDER BY created_at DESC\n `,\n [tenantId, JSON.stringify([parentSkillName])]\n );\n\n return result.rows.map(this.mapRowToSkill);\n }\n}\n","/**\n * Skill table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create skills table\n */\nexport const createSkillsTable: Migration = {\n version: 4,\n name: \"create_skills_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_skills (\n id VARCHAR(255) PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n description TEXT NOT NULL,\n license VARCHAR(255),\n compatibility VARCHAR(255),\n metadata JSONB DEFAULT '{}',\n content TEXT,\n sub_skills JSONB DEFAULT '[]',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_name \n ON lattice_skills(name)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_license \n ON lattice_skills(license)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_compatibility \n ON lattice_skills(compatibility)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_created_at \n ON lattice_skills(created_at DESC)\n `);\n\n // Create GIN index for metadata JSONB queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_metadata \n ON lattice_skills USING GIN (metadata)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_metadata\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_compatibility\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_license\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_skills_name\");\n await client.query(\"DROP TABLE IF EXISTS lattice_skills\");\n },\n};\n","/**\n * Skill table tenant isolation migration\n * Adds tenant_id column to lattice_skills table\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Add tenant_id column to skills table\n */\nexport const addSkillTenantId: Migration = {\n version: 16,\n name: \"add_skill_tenant_id\",\n up: async (client: PoolClient) => {\n // Add tenant_id column\n await client.query(`\n ALTER TABLE lattice_skills\n ADD COLUMN IF NOT EXISTS tenant_id VARCHAR(255) DEFAULT 'default'\n `);\n\n // Update existing records to have tenant_id\n await client.query(`\n UPDATE lattice_skills \n SET tenant_id = 'default' \n WHERE tenant_id IS NULL\n `);\n\n // Make tenant_id NOT NULL after setting defaults\n await client.query(`\n ALTER TABLE lattice_skills\n ALTER COLUMN tenant_id SET NOT NULL\n `);\n\n // Drop the default constraint\n await client.query(`\n ALTER TABLE lattice_skills\n ALTER COLUMN tenant_id DROP DEFAULT\n `);\n\n // Create index for tenant-based queries\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_skills_tenant_id \n ON lattice_skills(tenant_id)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\n \"DROP INDEX IF EXISTS idx_lattice_skills_tenant_id\"\n );\n await client.query(\n \"ALTER TABLE lattice_skills DROP COLUMN IF EXISTS tenant_id\"\n );\n },\n};\n","/**\n * Skill table primary key migration\n * Changes primary key from (id) to (tenant_id, id) for multi-tenancy\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Change skill table primary key to support multi-tenancy\n */\nexport const changeSkillPrimaryKey: Migration = {\n version: 17,\n name: \"change_skill_primary_key\",\n up: async (client: PoolClient) => {\n // Check if table exists\n const tableExists = await client.query(`\n SELECT EXISTS (\n SELECT FROM information_schema.tables \n WHERE table_name = 'lattice_skills'\n )\n `);\n\n if (!tableExists.rows[0].exists) {\n return;\n }\n\n // Check current primary key\n const pkResult = await client.query(`\n SELECT a.attname as column_name\n FROM pg_index i\n JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)\n WHERE i.indrelid = 'lattice_skills'::regclass\n AND i.indisprimary\n `);\n\n const pkColumns = pkResult.rows.map(row => row.column_name);\n \n // If primary key is already (tenant_id, id), nothing to do\n if (pkColumns.includes('tenant_id') && pkColumns.includes('id')) {\n console.log('Skill table primary key already correct (tenant_id, id)');\n return;\n }\n\n // If primary key is just (id), we need to change it\n if (pkColumns.length === 1 && pkColumns[0] === 'id') {\n console.log('Changing skill table primary key from (id) to (tenant_id, id)');\n \n // Check if tenant_id column exists\n const tenantIdExists = await client.query(`\n SELECT EXISTS (\n SELECT FROM information_schema.columns \n WHERE table_name = 'lattice_skills' \n AND column_name = 'tenant_id'\n )\n `);\n\n if (!tenantIdExists.rows[0].exists) {\n // Add tenant_id column if it doesn't exist\n await client.query(`\n ALTER TABLE lattice_skills\n ADD COLUMN tenant_id VARCHAR(255) DEFAULT 'default'\n `);\n }\n\n // Update existing records to have tenant_id\n await client.query(`\n UPDATE lattice_skills \n SET tenant_id = 'default' \n WHERE tenant_id IS NULL\n `);\n\n // Make tenant_id NOT NULL\n await client.query(`\n ALTER TABLE lattice_skills\n ALTER COLUMN tenant_id SET NOT NULL\n `);\n\n // Drop the old primary key\n await client.query(`\n ALTER TABLE lattice_skills \n DROP CONSTRAINT IF EXISTS lattice_skills_pkey\n `);\n\n // Add new composite primary key\n await client.query(`\n ALTER TABLE lattice_skills \n ADD PRIMARY KEY (tenant_id, id)\n `);\n\n console.log('Skill table primary key changed successfully to (tenant_id, id)');\n }\n },\n down: async (client: PoolClient) => {\n console.warn('Down migration for changeSkillPrimaryKey is not supported');\n },\n};\n","/**\n * PostgreSQL implementation of DatabaseConfigStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from 'pg';\nimport {\n DatabaseConfigStore,\n DatabaseConfigEntry,\n CreateDatabaseConfigRequest,\n UpdateDatabaseConfigRequest,\n DatabaseConfig,\n} from '@axiom-lattice/protocols';\nimport { MigrationManager } from '../migrations/migration';\nimport { createDatabaseConfigsTable } from '../migrations/database_config_migrations';\nimport { encrypt, decrypt } from '@axiom-lattice/core';\n\n/**\n * PostgreSQL DatabaseConfigStore options\n */\nexport interface PostgreSQLDatabaseConfigStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of DatabaseConfigStore\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n * - Automatic password encryption/decryption\n * - Unique constraint on (tenant_id, key)\n */\nexport class PostgreSQLDatabaseConfigStore implements DatabaseConfigStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLDatabaseConfigStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === 'string') {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createDatabaseConfigsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error('Failed to initialize PostgreSQLDatabaseConfigStore:', error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all database configurations for a tenant\n */\n async getAllConfigs(tenantId: string): Promise<DatabaseConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get all database configurations across all tenants\n */\n async getAllConfigsWithoutTenant(): Promise<DatabaseConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get database configuration by ID\n */\n async getConfigById(tenantId: string, id: string): Promise<DatabaseConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Get database configuration by business key\n */\n async getConfigByKey(tenantId: string, key: string): Promise<DatabaseConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_database_configs\n WHERE tenant_id = $1 AND key = $2\n `,\n [tenantId, key]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Create a new database configuration\n */\n async createConfig(\n tenantId: string,\n id: string,\n data: CreateDatabaseConfigRequest\n ): Promise<DatabaseConfigEntry> {\n await this.ensureInitialized();\n\n const now = new Date();\n const nowString = now.toISOString();\n const configWithEncryptedPassword = this.encryptPasswordInConfig(data.config);\n\n await this.pool.query(\n `\n INSERT INTO lattice_database_configs (id, tenant_id, key, name, description, config, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7::timestamp, $8::timestamp)\n ON CONFLICT (tenant_id, id) DO UPDATE SET\n key = EXCLUDED.key,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n config = EXCLUDED.config,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.key,\n data.name || null,\n data.description || null,\n JSON.stringify(configWithEncryptedPassword),\n nowString,\n nowString,\n ]\n );\n\n return {\n id,\n tenantId,\n key: data.key,\n config: data.config,\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing database configuration\n */\n async updateConfig(\n tenantId: string,\n id: string,\n updates: Partial<UpdateDatabaseConfigRequest>\n ): Promise<DatabaseConfigEntry | null> {\n await this.ensureInitialized();\n\n const existing = await this.getConfigById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update fields and values as key-value pairs for clarity\n const updateData: Record<string, any> = {};\n\n if (updates.key !== undefined) {\n updateData.key = updates.key;\n }\n\n if (updates.name !== undefined) {\n updateData.name = updates.name || null;\n }\n\n if (updates.description !== undefined) {\n updateData.description = updates.description || null;\n }\n\n if (updates.config !== undefined) {\n const configWithEncryptedPassword = this.encryptPasswordInConfig(updates.config);\n updateData.config = JSON.stringify(configWithEncryptedPassword);\n }\n\n if (Object.keys(updateData).length === 0) {\n return existing;\n }\n\n // Always update the updated_at timestamp\n updateData.updated_at = new Date().toISOString();\n\n // Build SQL with tenantId and id at fixed positions at the end\n const fields = Object.keys(updateData);\n const values = Object.values(updateData);\n \n // Add WHERE clause values at the end\n values.push(tenantId);\n values.push(id);\n\n // Build SET clause with numbered parameters\n const setClauses = fields.map((field, index) => {\n if (field === 'updated_at') {\n return `${field} = $${index + 1}::timestamp`;\n }\n return `${field} = $${index + 1}`;\n });\n\n // WHERE clause parameters are at the end\n const whereTenantIndex = fields.length + 1;\n const whereIdIndex = fields.length + 2;\n\n const sql = `\n UPDATE lattice_database_configs\n SET ${setClauses.join(', ')}\n WHERE tenant_id = $${whereTenantIndex} AND id = $${whereIdIndex}\n `;\n\n await this.pool.query(sql, values);\n\n return await this.getConfigById(tenantId, id);\n }\n\n /**\n * Delete a database configuration by ID\n */\n async deleteConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_database_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if configuration exists\n */\n async hasConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_database_configs\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to DatabaseConfigEntry object\n * Automatically decrypts password if present\n */\n private mapRowToEntry(row: {\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }): DatabaseConfigEntry {\n const config: DatabaseConfig = typeof row.config === 'string'\n ? JSON.parse(row.config)\n : row.config;\n\n // Decrypt password if present\n if (config.password) {\n try {\n config.password = decrypt(config.password);\n } catch (error) {\n console.error('Failed to decrypt database password:', error);\n throw new Error('Failed to decrypt database configuration');\n }\n }\n\n return {\n id: row.id,\n tenantId: row.tenant_id,\n key: row.key,\n config,\n name: row.name || undefined,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Encrypt password in config before storing\n */\n private encryptPasswordInConfig(config: DatabaseConfig): DatabaseConfig {\n const configCopy = { ...config };\n \n if (configCopy.password) {\n configCopy.password = encrypt(configCopy.password);\n }\n \n return configCopy;\n }\n}\n","/**\n * Database configuration table migrations\n */\n\nimport { PoolClient } from 'pg';\nimport { Migration } from './migration';\n\n/**\n * Initial migration: Create database configs table\n */\nexport const createDatabaseConfigsTable: Migration = {\n version: 4,\n name: 'create_database_configs_table',\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_database_configs (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n key VARCHAR(255) NOT NULL,\n name VARCHAR(255),\n description TEXT,\n config JSONB NOT NULL,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n \n PRIMARY KEY (tenant_id, id),\n CONSTRAINT uk_lattice_database_configs_tenant_key UNIQUE (tenant_id, key)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_database_configs_tenant_id \n ON lattice_database_configs(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_database_configs_key \n ON lattice_database_configs(key)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query('DROP INDEX IF EXISTS idx_lattice_database_configs_key');\n await client.query('DROP INDEX IF EXISTS idx_lattice_database_configs_tenant_id');\n await client.query('DROP TABLE IF EXISTS lattice_database_configs');\n },\n};\n","/**\n * PostgreSQL implementation of MetricsServerConfigStore\n */\n\nimport { Pool, PoolClient, PoolConfig } from 'pg';\nimport {\n MetricsServerConfigStore,\n MetricsServerConfigEntry,\n CreateMetricsServerConfigRequest,\n UpdateMetricsServerConfigRequest,\n MetricsServerConfig,\n} from '@axiom-lattice/protocols';\nimport { MigrationManager } from '../migrations/migration';\nimport { createMetricsConfigsTable } from '../migrations/metrics_config_migrations';\nimport { encrypt, decrypt } from '@axiom-lattice/core';\n\n/**\n * PostgreSQL MetricsServerConfigStore options\n */\nexport interface PostgreSQLMetricsServerConfigStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of MetricsServerConfigStore\n * \n * Features:\n * - Multi-tenant isolation via tenant_id\n * - Automatic apiKey and password encryption/decryption\n * - Unique constraint on (tenant_id, key)\n */\nexport class PostgreSQLMetricsServerConfigStore implements MetricsServerConfigStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLMetricsServerConfigStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === 'string') {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createMetricsConfigsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error('Failed to initialize PostgreSQLMetricsServerConfigStore:', error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all metrics server configurations for a tenant\n */\n async getAllConfigs(tenantId: string): Promise<MetricsServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get all metrics server configurations across all tenants\n */\n async getAllConfigsWithoutTenant(): Promise<MetricsServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get metrics server configuration by ID\n */\n async getConfigById(tenantId: string, id: string): Promise<MetricsServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Get metrics server configuration by business key\n */\n async getConfigByKey(tenantId: string, key: string): Promise<MetricsServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, created_at, updated_at\n FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND key = $2\n `,\n [tenantId, key]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Create a new metrics server configuration\n */\n async createConfig(\n tenantId: string,\n id: string,\n data: CreateMetricsServerConfigRequest\n ): Promise<MetricsServerConfigEntry> {\n await this.ensureInitialized();\n\n const now = new Date();\n const nowString = now.toISOString();\n const configWithEncryptedSecrets = this.encryptSecretsInConfig(data.config);\n\n await this.pool.query(\n `\n INSERT INTO lattice_metrics_configs (id, tenant_id, key, name, description, config, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7::timestamp, $8::timestamp)\n ON CONFLICT (tenant_id, id) DO UPDATE SET\n key = EXCLUDED.key,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n config = EXCLUDED.config,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.key,\n data.name || null,\n data.description || null,\n JSON.stringify(configWithEncryptedSecrets),\n nowString,\n nowString,\n ]\n );\n\n return {\n id,\n tenantId,\n key: data.key,\n config: data.config,\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing metrics server configuration\n */\n async updateConfig(\n tenantId: string,\n id: string,\n updates: Partial<UpdateMetricsServerConfigRequest>\n ): Promise<MetricsServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const existing = await this.getConfigById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update fields and values as key-value pairs for clarity\n const updateData: Record<string, any> = {};\n\n if (updates.key !== undefined) {\n updateData.key = updates.key;\n }\n\n if (updates.name !== undefined) {\n updateData.name = updates.name || null;\n }\n\n if (updates.description !== undefined) {\n updateData.description = updates.description || null;\n }\n\n if (updates.config !== undefined) {\n const configWithEncryptedSecrets = this.encryptSecretsInConfig(updates.config);\n updateData.config = JSON.stringify(configWithEncryptedSecrets);\n }\n\n if (Object.keys(updateData).length === 0) {\n return existing;\n }\n\n // Always update the updated_at timestamp\n updateData.updated_at = new Date().toISOString();\n\n // Build SQL with tenantId and id at fixed positions at the end\n const fields = Object.keys(updateData);\n const values = Object.values(updateData);\n \n // Add WHERE clause values at the end\n values.push(tenantId);\n values.push(id);\n\n // Build SET clause with numbered parameters\n const setClauses = fields.map((field, index) => {\n if (field === 'updated_at') {\n return `${field} = $${index + 1}::timestamp`;\n }\n return `${field} = $${index + 1}`;\n });\n\n // WHERE clause parameters are at the end\n const whereTenantIndex = fields.length + 1;\n const whereIdIndex = fields.length + 2;\n\n const sql = `\n UPDATE lattice_metrics_configs\n SET ${setClauses.join(', ')}\n WHERE tenant_id = $${whereTenantIndex} AND id = $${whereIdIndex}\n `;\n\n await this.pool.query(sql, values);\n\n return await this.getConfigById(tenantId, id);\n }\n\n /**\n * Delete a metrics server configuration by ID\n */\n async deleteConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if configuration exists\n */\n async hasConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_metrics_configs\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to MetricsServerConfigEntry object\n * Automatically decrypts apiKey and password if present\n */\n private mapRowToEntry(row: {\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n created_at: Date;\n updated_at: Date;\n }): MetricsServerConfigEntry {\n const config: MetricsServerConfig = typeof row.config === 'string'\n ? JSON.parse(row.config)\n : row.config;\n\n // Decrypt apiKey if present\n if (config.apiKey) {\n try {\n config.apiKey = decrypt(config.apiKey);\n } catch (error) {\n console.error('Failed to decrypt apiKey:', error);\n throw new Error('Failed to decrypt metrics server configuration');\n }\n }\n\n // Decrypt password if present\n if (config.password) {\n try {\n config.password = decrypt(config.password);\n } catch (error) {\n console.error('Failed to decrypt password:', error);\n throw new Error('Failed to decrypt metrics server configuration');\n }\n }\n\n return {\n id: row.id,\n tenantId: row.tenant_id,\n key: row.key,\n config,\n name: row.name || undefined,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Encrypt apiKey and password in config before storing\n */\n private encryptSecretsInConfig(config: MetricsServerConfig): MetricsServerConfig {\n const configCopy = { ...config };\n \n if (configCopy.apiKey) {\n configCopy.apiKey = encrypt(configCopy.apiKey);\n }\n \n if (configCopy.password) {\n configCopy.password = encrypt(configCopy.password);\n }\n \n return configCopy;\n }\n}\n","/**\n * Metrics server configuration table migrations\n */\n\nimport { PoolClient } from 'pg';\nimport { Migration } from './migration';\n\n/**\n * Initial migration: Create metrics configs table\n */\nexport const createMetricsConfigsTable: Migration = {\n version: 12,\n name: 'create_metrics_configs_table',\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_metrics_configs (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n key VARCHAR(255) NOT NULL,\n name VARCHAR(255),\n description TEXT,\n config JSONB NOT NULL,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n \n PRIMARY KEY (tenant_id, id),\n CONSTRAINT uk_lattice_metrics_configs_tenant_key UNIQUE (tenant_id, key)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_metrics_configs_tenant_id \n ON lattice_metrics_configs(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_metrics_configs_key \n ON lattice_metrics_configs(key)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query('DROP INDEX IF EXISTS idx_lattice_metrics_configs_key');\n await client.query('DROP INDEX IF EXISTS idx_lattice_metrics_configs_tenant_id');\n await client.query('DROP TABLE IF EXISTS lattice_metrics_configs');\n },\n};\n","/**\n * PostgreSQL implementation of McpServerConfigStore\n */\n\nimport { Pool, PoolConfig } from 'pg';\nimport {\n McpServerConfigStore,\n McpServerConfigEntry,\n CreateMcpServerConfigRequest,\n UpdateMcpServerConfigRequest,\n McpServerConfig,\n} from '@axiom-lattice/protocols';\nimport { MigrationManager } from '../migrations/migration';\nimport { createMcpServerConfigsTable } from '../migrations/mcp_server_config_migrations';\nimport { encrypt, decrypt } from '@axiom-lattice/core';\n\n/**\n * PostgreSQL McpServerConfigStore options\n */\nexport interface PostgreSQLMcpServerConfigStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of McpServerConfigStore\n *\n * Features:\n * - Multi-tenant isolation via tenant_id\n * - Automatic env encryption/decryption\n * - Unique constraint on (tenant_id, key)\n */\nexport class PostgreSQLMcpServerConfigStore implements McpServerConfigStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private initPromise: Promise<void> | null = null;\n\n constructor(options: PostgreSQLMcpServerConfigStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === 'string') {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createMcpServerConfigsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error('Failed to initialize PostgreSQLMcpServerConfigStore:', error);\n throw error;\n });\n }\n }\n\n /**\n * Initialize the store and run migrations\n * Uses a promise-based lock to prevent concurrent initialization\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n if (this.initPromise) {\n return this.initPromise;\n }\n\n this.initPromise = (async () => {\n try {\n await this.migrationManager.migrate();\n this.initialized = true;\n } finally {\n this.initPromise = null;\n }\n })();\n\n return this.initPromise;\n }\n\n /**\n * Get all MCP server configurations for a tenant\n */\n async getAllConfigs(tenantId: string): Promise<McpServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get all MCP server configurations across all tenants\n */\n async getAllConfigsWithoutTenant(): Promise<McpServerConfigEntry[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map((row) => this.mapRowToEntry(row));\n }\n\n /**\n * Get MCP server configuration by ID\n */\n async getConfigById(tenantId: string, id: string): Promise<McpServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Get MCP server configuration by business key\n */\n async getConfigByKey(tenantId: string, key: string): Promise<McpServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at\n FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND key = $2\n `,\n [tenantId, key]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToEntry(result.rows[0]);\n }\n\n /**\n * Create a new MCP server configuration\n */\n async createConfig(\n tenantId: string,\n id: string,\n data: CreateMcpServerConfigRequest\n ): Promise<McpServerConfigEntry> {\n await this.ensureInitialized();\n\n const now = new Date();\n const nowString = now.toISOString();\n const { config: configWithEncryptedEnv, isEnvEncrypted } = this.encryptEnvInConfig(data.config);\n\n await this.pool.query(\n `\n INSERT INTO lattice_mcp_server_configs (id, tenant_id, key, name, description, config, selected_tools, is_env_encrypted, status, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10::timestamp, $11::timestamp)\n ON CONFLICT (tenant_id, id) DO UPDATE SET\n key = EXCLUDED.key,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n config = EXCLUDED.config,\n selected_tools = EXCLUDED.selected_tools,\n is_env_encrypted = EXCLUDED.is_env_encrypted,\n status = EXCLUDED.status,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n tenantId,\n data.key,\n data.name || null,\n data.description || null,\n JSON.stringify(configWithEncryptedEnv),\n data.selectedTools || [],\n isEnvEncrypted,\n 'disconnected',\n nowString,\n nowString,\n ]\n );\n\n return {\n id,\n tenantId,\n key: data.key,\n config: data.config,\n selectedTools: data.selectedTools || [],\n isEnvEncrypted,\n status: 'disconnected',\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing MCP server configuration\n */\n async updateConfig(\n tenantId: string,\n id: string,\n updates: Partial<UpdateMcpServerConfigRequest>\n ): Promise<McpServerConfigEntry | null> {\n await this.ensureInitialized();\n\n const existing = await this.getConfigById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update fields and values as key-value pairs for clarity\n const updateData: Record<string, any> = {};\n\n if (updates.key !== undefined) {\n updateData.key = updates.key;\n }\n\n if (updates.name !== undefined) {\n updateData.name = updates.name || null;\n }\n\n if (updates.description !== undefined) {\n updateData.description = updates.description || null;\n }\n\n if (updates.config !== undefined) {\n const { config: configWithEncryptedEnv, isEnvEncrypted } = this.encryptEnvInConfig(updates.config);\n updateData.config = JSON.stringify(configWithEncryptedEnv);\n updateData.is_env_encrypted = isEnvEncrypted;\n }\n\n if (updates.selectedTools !== undefined) {\n updateData.selected_tools = updates.selectedTools;\n }\n\n if (updates.status !== undefined) {\n updateData.status = updates.status;\n }\n\n if (Object.keys(updateData).length === 0) {\n return existing;\n }\n\n // Always update the updated_at timestamp\n updateData.updated_at = new Date().toISOString();\n\n // Build SQL with tenantId and id at fixed positions at the end\n const fields = Object.keys(updateData);\n const values = Object.values(updateData);\n\n // Add WHERE clause values at the end\n values.push(tenantId);\n values.push(id);\n\n // Build SET clause with numbered parameters\n const setClauses = fields.map((field, index) => {\n if (field === 'updated_at') {\n return `${field} = $${index + 1}::timestamp`;\n }\n return `${field} = $${index + 1}`;\n });\n\n // WHERE clause parameters are at the end\n const whereTenantIndex = fields.length + 1;\n const whereIdIndex = fields.length + 2;\n\n const sql = `\n UPDATE lattice_mcp_server_configs\n SET ${setClauses.join(', ')}\n WHERE tenant_id = $${whereTenantIndex} AND id = $${whereIdIndex}\n `;\n\n await this.pool.query(sql, values);\n\n return await this.getConfigById(tenantId, id);\n }\n\n /**\n * Delete a MCP server configuration by ID\n */\n async deleteConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND id = $2\n `,\n [tenantId, id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n\n /**\n * Check if configuration exists\n */\n async hasConfig(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_mcp_server_configs\n WHERE tenant_id = $1 AND id = $2\n LIMIT 1\n `,\n [tenantId, id]\n );\n\n return result.rows.length > 0;\n }\n\n /**\n * Dispose resources and close the connection pool\n */\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to McpServerConfigEntry object\n * Automatically decrypts env values if present\n */\n private mapRowToEntry(row: {\n id: string;\n tenant_id: string;\n key: string;\n name: string | null;\n description: string | null;\n config: any;\n selected_tools: string[];\n is_env_encrypted: boolean;\n status: string;\n created_at: Date;\n updated_at: Date;\n }): McpServerConfigEntry {\n const config: McpServerConfig = typeof row.config === 'string'\n ? JSON.parse(row.config)\n : row.config;\n\n // Decrypt env values if present and encrypted\n if (config.env && row.is_env_encrypted) {\n try {\n const decryptedEnv: Record<string, string> = {};\n for (const [key, value] of Object.entries(config.env)) {\n decryptedEnv[key] = decrypt(value);\n }\n config.env = decryptedEnv;\n } catch (error) {\n console.error('Failed to decrypt MCP server env:', error);\n throw new Error('Failed to decrypt MCP server configuration');\n }\n }\n\n return {\n id: row.id,\n tenantId: row.tenant_id,\n key: row.key,\n config,\n selectedTools: row.selected_tools || [],\n isEnvEncrypted: row.is_env_encrypted,\n status: row.status as 'disconnected' | 'connected' | 'error',\n name: row.name || undefined,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Encrypt env values in config before storing\n */\n private encryptEnvInConfig(config: McpServerConfig): { config: McpServerConfig; isEnvEncrypted: boolean } {\n const configCopy = { ...config };\n let isEnvEncrypted = false;\n\n if (configCopy.env && Object.keys(configCopy.env).length > 0) {\n const encryptedEnv: Record<string, string> = {};\n for (const [key, value] of Object.entries(configCopy.env)) {\n encryptedEnv[key] = encrypt(value);\n }\n configCopy.env = encryptedEnv;\n isEnvEncrypted = true;\n }\n\n return { config: configCopy, isEnvEncrypted };\n }\n}\n","/**\n * MCP Server configuration table migrations\n */\n\nimport { PoolClient } from 'pg';\nimport { Migration } from './migration';\n\n/**\n * Migration: Create MCP server configs table\n */\nexport const createMcpServerConfigsTable: Migration = {\n version: 13, // Use next available version number\n name: 'create_mcp_server_configs_table',\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_mcp_server_configs (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n key VARCHAR(255) NOT NULL,\n name VARCHAR(255),\n description TEXT,\n config JSONB NOT NULL,\n selected_tools JSONB NOT NULL DEFAULT '[]',\n is_env_encrypted BOOLEAN NOT NULL DEFAULT FALSE,\n status VARCHAR(50) NOT NULL DEFAULT 'disconnected',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n \n PRIMARY KEY (tenant_id, id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_mcp_server_configs_tenant_id \n ON lattice_mcp_server_configs(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_mcp_server_configs_key \n ON lattice_mcp_server_configs(key)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_mcp_server_configs_status \n ON lattice_mcp_server_configs(status)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query('DROP INDEX IF EXISTS idx_lattice_mcp_server_configs_status');\n await client.query('DROP INDEX IF EXISTS idx_lattice_mcp_server_configs_key');\n await client.query('DROP INDEX IF EXISTS idx_lattice_mcp_server_configs_tenant_id');\n await client.query('DROP TABLE IF EXISTS lattice_mcp_server_configs');\n },\n};\n","/**\n * PostgreSQL implementation of WorkspaceStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n WorkspaceStore,\n Workspace,\n CreateWorkspaceRequest,\n UpdateWorkspaceRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createWorkspacesTable } from \"../migrations/workspace_migrations\";\n\n/**\n * PostgreSQL WorkspaceStore options\n */\nexport interface PostgreSQLWorkspaceStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of WorkspaceStore\n */\nexport class PostgreSQLWorkspaceStore implements WorkspaceStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLWorkspaceStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createWorkspacesTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLWorkspaceStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Workspace object\n */\n private mapRowToWorkspace(row: {\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n storage_type: string;\n created_at: Date;\n updated_at: Date;\n }): Workspace {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n name: row.name,\n description: row.description || undefined,\n storageType: row.storage_type as Workspace[\"storageType\"],\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all workspaces for a tenant\n */\n async getAllWorkspaces(tenantId: string): Promise<Workspace[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n storage_type: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, storage_type, created_at, updated_at\n FROM lattice_workspaces\n WHERE tenant_id = $1\n ORDER BY created_at DESC\n `,\n [tenantId]\n );\n\n return result.rows.map(this.mapRowToWorkspace);\n }\n\n /**\n * Get a workspace by ID for a specific tenant\n */\n async getWorkspaceById(tenantId: string, id: string): Promise<Workspace | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n name: string;\n description: string | null;\n storage_type: string;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, name, description, storage_type, created_at, updated_at\n FROM lattice_workspaces\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToWorkspace(result.rows[0]);\n }\n\n /**\n * Create a new workspace\n */\n async createWorkspace(\n tenantId: string,\n id: string,\n data: CreateWorkspaceRequest\n ): Promise<Workspace> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_workspaces (id, tenant_id, name, description, storage_type, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id, tenant_id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n storage_type = EXCLUDED.storage_type,\n updated_at = EXCLUDED.updated_at\n `,\n [id, tenantId, data.name, data.description || null, data.storageType, now, now]\n );\n\n return {\n id,\n tenantId,\n name: data.name,\n description: data.description,\n storageType: data.storageType,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing workspace\n */\n async updateWorkspace(\n tenantId: string,\n id: string,\n updates: UpdateWorkspaceRequest\n ): Promise<Workspace | null> {\n await this.ensureInitialized();\n\n // Get existing workspace\n const existing = await this.getWorkspaceById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updates.storageType !== undefined) {\n updateFields.push(`storage_type = $${paramIndex++}`);\n updateValues.push(updates.storageType);\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id and tenant_id for WHERE clause\n updateValues.push(id);\n updateValues.push(tenantId);\n\n await this.pool.query(\n `\n UPDATE lattice_workspaces\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex} AND tenant_id = $${paramIndex + 1}\n `,\n updateValues\n );\n\n // Return updated workspace\n return await this.getWorkspaceById(tenantId, id);\n }\n\n /**\n * Delete a workspace by ID\n */\n async deleteWorkspace(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_workspaces\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * Workspace table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create workspaces table\n */\nexport const createWorkspacesTable: Migration = {\n version: 5,\n name: \"create_workspaces_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_workspaces (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n storage_type VARCHAR(50) NOT NULL DEFAULT 'sandbox',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (id, tenant_id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_workspaces_tenant_id \n ON lattice_workspaces(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_workspaces_created_at \n ON lattice_workspaces(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_workspaces_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_workspaces_tenant_id\");\n await client.query(\"DROP TABLE IF EXISTS lattice_workspaces\");\n },\n};\n","/**\n * PostgreSQL implementation of ProjectStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n ProjectStore,\n Project,\n CreateProjectRequest,\n UpdateProjectRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createProjectsTable } from \"../migrations/project_migrations\";\n\n/**\n * PostgreSQL ProjectStore options\n */\nexport interface PostgreSQLProjectStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of ProjectStore\n */\nexport class PostgreSQLProjectStore implements ProjectStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLProjectStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createProjectsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLProjectStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Project object\n */\n private mapRowToProject(row: {\n id: string;\n tenant_id: string;\n workspace_id: string;\n name: string;\n description: string | null;\n created_at: Date;\n updated_at: Date;\n }): Project {\n return {\n id: row.id,\n tenantId: row.tenant_id,\n workspaceId: row.workspace_id,\n name: row.name,\n description: row.description || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all projects for a specific workspace\n */\n async getProjectsByWorkspace(tenantId: string, workspaceId: string): Promise<Project[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n workspace_id: string;\n name: string;\n description: string | null;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, workspace_id, name, description, created_at, updated_at\n FROM lattice_projects\n WHERE tenant_id = $1 AND workspace_id = $2\n ORDER BY created_at DESC\n `,\n [tenantId, workspaceId]\n );\n\n return result.rows.map(this.mapRowToProject);\n }\n\n /**\n * Get a project by ID for a specific tenant\n */\n async getProjectById(tenantId: string, id: string): Promise<Project | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n tenant_id: string;\n workspace_id: string;\n name: string;\n description: string | null;\n created_at: Date;\n updated_at: Date;\n }>(\n `\n SELECT id, tenant_id, workspace_id, name, description, created_at, updated_at\n FROM lattice_projects\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToProject(result.rows[0]);\n }\n\n /**\n * Create a new project\n */\n async createProject(\n tenantId: string,\n workspaceId: string,\n id: string,\n data: CreateProjectRequest\n ): Promise<Project> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_projects (id, tenant_id, workspace_id, name, description, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id, tenant_id) DO UPDATE SET\n workspace_id = EXCLUDED.workspace_id,\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n updated_at = EXCLUDED.updated_at\n `,\n [id, tenantId, workspaceId, data.name, data.description || null, now, now]\n );\n\n return {\n id,\n tenantId,\n workspaceId,\n name: data.name,\n description: data.description,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing project\n */\n async updateProject(\n tenantId: string,\n id: string,\n updates: UpdateProjectRequest\n ): Promise<Project | null> {\n await this.ensureInitialized();\n\n // Get existing project\n const existing = await this.getProjectById(tenantId, id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id and tenant_id for WHERE clause\n updateValues.push(id);\n updateValues.push(tenantId);\n\n await this.pool.query(\n `\n UPDATE lattice_projects\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex} AND tenant_id = $${paramIndex + 1}\n `,\n updateValues\n );\n\n // Return updated project\n return await this.getProjectById(tenantId, id);\n }\n\n /**\n * Delete a project by ID\n */\n async deleteProject(tenantId: string, id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_projects\n WHERE id = $1 AND tenant_id = $2\n `,\n [id, tenantId]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * Project table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Initial migration: Create projects table\n */\nexport const createProjectsTable: Migration = {\n version: 6,\n name: \"create_projects_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_projects (\n id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n workspace_id VARCHAR(255) NOT NULL,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (id, tenant_id)\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_projects_tenant_id \n ON lattice_projects(tenant_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_projects_workspace_id \n ON lattice_projects(workspace_id)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_projects_created_at \n ON lattice_projects(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_projects_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_projects_workspace_id\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_projects_tenant_id\");\n await client.query(\"DROP TABLE IF EXISTS lattice_projects\");\n },\n};\n","import { Pool, PoolConfig } from \"pg\";\nimport { UserStore, User, CreateUserRequest, UpdateUserRequest } from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createUsersTable } from \"../migrations/user_migrations\";\nimport { addUserStatusColumn } from \"../migrations/user_status_migration\";\n\nexport interface PostgreSQLUserStoreOptions {\n poolConfig: string | PoolConfig;\n autoMigrate?: boolean;\n}\n\nexport class PostgreSQLUserStore implements UserStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n\n constructor(options: PostgreSQLUserStoreOptions) {\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createUsersTable);\n this.migrationManager.register(addUserStatusColumn);\n\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLUserStore:\", error);\n throw error;\n });\n }\n }\n\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) return;\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) await this.initialize();\n }\n\n private mapRowToUser(row: {\n id: string;\n email: string;\n name: string;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }): User {\n return {\n id: row.id,\n email: row.email,\n name: row.name,\n status: row.status as import(\"@axiom-lattice/protocols\").UserStatus,\n metadata: row.metadata,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n async getAllUsers(): Promise<User[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT id, email, name, status, metadata, created_at, updated_at\n FROM lattice_users\n ORDER BY created_at DESC\n `\n );\n\n return result.rows.map(this.mapRowToUser);\n }\n\n async getUserById(id: string): Promise<User | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT id, email, name, status, metadata, created_at, updated_at\n FROM lattice_users\n WHERE id = $1\n `,\n [id]\n );\n\n if (result.rows.length === 0) return null;\n return this.mapRowToUser(result.rows[0]);\n }\n\n async getUserByEmail(email: string): Promise<User | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n SELECT id, email, name, status, metadata, created_at, updated_at\n FROM lattice_users\n WHERE email = $1\n `,\n [email]\n );\n\n if (result.rows.length === 0) return null;\n return this.mapRowToUser(result.rows[0]);\n }\n\n async createUser(id: string, data: CreateUserRequest): Promise<User> {\n await this.ensureInitialized();\n\n const now = new Date();\n const metadata = data.metadata || {};\n const status = data.status || \"pending\";\n\n await this.pool.query(\n `\n INSERT INTO lattice_users (id, email, name, status, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id) DO UPDATE SET\n email = EXCLUDED.email,\n name = EXCLUDED.name,\n status = EXCLUDED.status,\n metadata = EXCLUDED.metadata,\n updated_at = EXCLUDED.updated_at\n `,\n [id, data.email, data.name, status, JSON.stringify(metadata), now, now]\n );\n\n return {\n id,\n email: data.email,\n name: data.name,\n status,\n metadata: data.metadata,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n async updateUser(id: string, updates: UpdateUserRequest): Promise<User | null> {\n await this.ensureInitialized();\n\n const existing = await this.getUserById(id);\n if (!existing) return null;\n\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.email !== undefined) {\n updateFields.push(`email = $${paramIndex++}`);\n updateValues.push(updates.email);\n }\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.status !== undefined) {\n updateFields.push(`status = $${paramIndex++}`);\n updateValues.push(updates.status);\n }\n\n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.metadata));\n }\n\n if (updateFields.length === 0) return existing;\n\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_users\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex}\n `,\n updateValues\n );\n\n return this.getUserById(id);\n }\n\n async deleteUser(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `DELETE FROM lattice_users WHERE id = $1`,\n [id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * User table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Create users table (multi-tenant refactor: removed tenant_id)\n */\nexport const createUsersTable: Migration = {\n version: 7,\n name: \"create_users_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_users (\n id VARCHAR(255) PRIMARY KEY,\n email VARCHAR(255) NOT NULL UNIQUE,\n name VARCHAR(255) NOT NULL,\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_users_email \n ON lattice_users(email)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_users_created_at \n ON lattice_users(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_users_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_users_email\");\n await client.query(\"DROP TABLE IF EXISTS lattice_users\");\n },\n};\n","import { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration to create lattice_users table with status support\n * Version 9: Adds status column to support user approval workflow\n */\nexport const addUserStatusColumn: Migration = {\n version: 9,\n name: \"add_user_status_column\",\n up: async (client: PoolClient) => {\n // Add status column if not exists\n await client.query(`\n ALTER TABLE lattice_users \n ADD COLUMN IF NOT EXISTS status VARCHAR(50) NOT NULL DEFAULT 'pending'\n `);\n \n // Create index on status for filtering\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_users_status \n ON lattice_users(tenant_id, status)\n `);\n \n // Migrate existing users to active status\n await client.query(`\n UPDATE lattice_users \n SET status = 'active' \n WHERE status = 'pending'\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(`\n DROP INDEX IF EXISTS idx_lattice_users_status\n `);\n await client.query(`\n ALTER TABLE lattice_users \n DROP COLUMN IF EXISTS status\n `);\n },\n};\n","/**\n * PostgreSQL implementation of TenantStore\n */\n\nimport { Pool, PoolConfig } from \"pg\";\nimport {\n TenantStore,\n Tenant,\n TenantStatus,\n CreateTenantRequest,\n UpdateTenantRequest,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createTenantsTable } from \"../migrations/tenant_migrations\";\n\n/**\n * PostgreSQL TenantStore options\n */\nexport interface PostgreSQLTenantStoreOptions {\n /**\n * PostgreSQL connection pool configuration\n * Can be a connection string or PoolConfig object\n */\n poolConfig: string | PoolConfig;\n\n /**\n * Whether to run migrations automatically on initialization\n * @default true\n */\n autoMigrate?: boolean;\n}\n\n/**\n * PostgreSQL implementation of TenantStore\n * Tenant is a top-level entity with single column primary key\n */\nexport class PostgreSQLTenantStore implements TenantStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n private ownsPool: boolean = true;\n\n constructor(options: PostgreSQLTenantStoreOptions) {\n // Create Pool from config\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createTenantsTable);\n\n // Auto-migrate by default\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLTenantStore:\", error);\n throw error;\n });\n }\n }\n\n /**\n * Dispose resources and close the connection pool\n * Should be called when the store is no longer needed\n */\n async dispose(): Promise<void> {\n if (this.ownsPool && this.pool) {\n await this.pool.end();\n }\n }\n\n /**\n * Initialize the store and run migrations\n */\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n /**\n * Ensure store is initialized\n */\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) {\n await this.initialize();\n }\n }\n\n /**\n * Map database row to Tenant object\n */\n private mapRowToTenant(row: {\n id: string;\n name: string;\n description: string | null;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }): Tenant {\n return {\n id: row.id,\n name: row.name,\n description: row.description || undefined,\n status: row.status as TenantStatus,\n metadata: row.metadata || undefined,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n }\n\n /**\n * Get all tenants\n */\n async getAllTenants(): Promise<Tenant[]> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string | null;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }>(`\n SELECT id, name, description, status, metadata, created_at, updated_at\n FROM lattice_tenants\n ORDER BY created_at DESC\n `);\n\n return result.rows.map(this.mapRowToTenant);\n }\n\n /**\n * Get a tenant by ID\n */\n async getTenantById(id: string): Promise<Tenant | null> {\n await this.ensureInitialized();\n\n const result = await this.pool.query<{\n id: string;\n name: string;\n description: string | null;\n status: string;\n metadata: Record<string, any>;\n created_at: Date;\n updated_at: Date;\n }>(`\n SELECT id, name, description, status, metadata, created_at, updated_at\n FROM lattice_tenants\n WHERE id = $1\n `, [id]);\n\n if (result.rows.length === 0) {\n return null;\n }\n\n return this.mapRowToTenant(result.rows[0]);\n }\n\n /**\n * Create a new tenant\n */\n async createTenant(\n id: string,\n data: CreateTenantRequest\n ): Promise<Tenant> {\n await this.ensureInitialized();\n\n const now = new Date();\n\n await this.pool.query(\n `\n INSERT INTO lattice_tenants (id, name, description, status, metadata, created_at, updated_at)\n VALUES ($1, $2, $3, $4, $5, $6, $7)\n ON CONFLICT (id) DO UPDATE SET\n name = EXCLUDED.name,\n description = EXCLUDED.description,\n status = EXCLUDED.status,\n metadata = EXCLUDED.metadata,\n updated_at = EXCLUDED.updated_at\n `,\n [\n id,\n data.name,\n data.description || null,\n data.status || \"active\",\n data.metadata || {},\n now,\n now,\n ]\n );\n\n return {\n id,\n name: data.name,\n description: data.description,\n status: data.status || \"active\",\n metadata: data.metadata,\n createdAt: now,\n updatedAt: now,\n };\n }\n\n /**\n * Update an existing tenant\n */\n async updateTenant(\n id: string,\n updates: UpdateTenantRequest\n ): Promise<Tenant | null> {\n await this.ensureInitialized();\n\n // Get existing tenant\n const existing = await this.getTenantById(id);\n if (!existing) {\n return null;\n }\n\n // Build update query dynamically based on provided fields\n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n\n if (updates.name !== undefined) {\n updateFields.push(`name = $${paramIndex++}`);\n updateValues.push(updates.name);\n }\n\n if (updates.description !== undefined) {\n updateFields.push(`description = $${paramIndex++}`);\n updateValues.push(updates.description || null);\n }\n\n if (updates.status !== undefined) {\n updateFields.push(`status = $${paramIndex++}`);\n updateValues.push(updates.status);\n }\n\n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(updates.metadata);\n }\n\n if (updateFields.length === 0) {\n // No fields to update\n return existing;\n }\n\n // Always update updated_at\n updateFields.push(`updated_at = $${paramIndex++}`);\n updateValues.push(new Date());\n\n // Add id for WHERE clause\n updateValues.push(id);\n\n await this.pool.query(\n `\n UPDATE lattice_tenants\n SET ${updateFields.join(\", \")}\n WHERE id = $${paramIndex}\n `,\n updateValues\n );\n\n // Return updated tenant\n return await this.getTenantById(id);\n }\n\n /**\n * Delete a tenant by ID\n */\n async deleteTenant(id: string): Promise<boolean> {\n await this.ensureInitialized();\n\n const result = await this.pool.query(\n `\n DELETE FROM lattice_tenants\n WHERE id = $1\n `,\n [id]\n );\n\n return result.rowCount !== null && result.rowCount > 0;\n }\n}\n","/**\n * Tenant table migrations\n */\n\nimport { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\n/**\n * Migration: Create tenants table\n * Tenant is a top-level entity with single column primary key\n */\nexport const createTenantsTable: Migration = {\n version: 8,\n name: \"create_tenants_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_tenants (\n id VARCHAR(255) PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n description TEXT,\n status VARCHAR(50) NOT NULL DEFAULT 'active',\n metadata JSONB DEFAULT '{}',\n created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP\n )\n `);\n\n // Create indexes for better query performance\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_tenants_status \n ON lattice_tenants(status)\n `);\n\n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_lattice_tenants_created_at \n ON lattice_tenants(created_at DESC)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_tenants_created_at\");\n await client.query(\"DROP INDEX IF EXISTS idx_lattice_tenants_status\");\n await client.query(\"DROP TABLE IF EXISTS lattice_tenants\");\n },\n};\n","import { Pool, PoolConfig } from \"pg\";\nimport {\n UserTenantLinkStore,\n UserTenantLink,\n CreateUserTenantLinkRequest,\n UpdateUserTenantLinkRequest,\n UserTenantRole,\n} from \"@axiom-lattice/protocols\";\nimport { MigrationManager } from \"../migrations/migration\";\nimport { createUserTenantLinkTable, migrateRemoveTenantIdFromUsers } from \"../migrations/user_tenant_link_migrations\";\n\nexport interface PostgreSQLUserTenantLinkStoreOptions {\n poolConfig: string | PoolConfig;\n autoMigrate?: boolean;\n}\n\nexport class PostgreSQLUserTenantLinkStore implements UserTenantLinkStore {\n private pool: Pool;\n private migrationManager: MigrationManager;\n private initialized: boolean = false;\n\n constructor(options: PostgreSQLUserTenantLinkStoreOptions) {\n if (typeof options.poolConfig === \"string\") {\n this.pool = new Pool({ connectionString: options.poolConfig });\n } else {\n this.pool = new Pool(options.poolConfig);\n }\n\n this.migrationManager = new MigrationManager(this.pool);\n this.migrationManager.register(createUserTenantLinkTable);\n this.migrationManager.register(migrateRemoveTenantIdFromUsers);\n\n if (options.autoMigrate !== false) {\n this.initialize().catch((error) => {\n console.error(\"Failed to initialize PostgreSQLUserTenantLinkStore:\", error);\n throw error;\n });\n }\n }\n\n async dispose(): Promise<void> {\n await this.pool.end();\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) return;\n await this.migrationManager.migrate();\n this.initialized = true;\n }\n\n private async ensureInitialized(): Promise<void> {\n if (!this.initialized) await this.initialize();\n }\n\n private mapRowToLink(row: {\n user_id: string;\n tenant_id: string;\n role: string;\n joined_at: Date;\n metadata: Record<string, any>;\n }): UserTenantLink {\n return {\n userId: row.user_id,\n tenantId: row.tenant_id,\n role: row.role as UserTenantRole,\n joinedAt: row.joined_at,\n metadata: row.metadata,\n };\n }\n\n async getTenantsByUser(userId: string): Promise<UserTenantLink[]> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT user_id, tenant_id, role, joined_at, metadata\n FROM lattice_user_tenant_links\n WHERE user_id = $1\n ORDER BY joined_at DESC\n `,\n [userId]\n );\n \n return result.rows.map(this.mapRowToLink);\n }\n\n async getUsersByTenant(tenantId: string): Promise<UserTenantLink[]> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT user_id, tenant_id, role, joined_at, metadata\n FROM lattice_user_tenant_links\n WHERE tenant_id = $1\n ORDER BY joined_at DESC\n `,\n [tenantId]\n );\n \n return result.rows.map(this.mapRowToLink);\n }\n\n async getLink(userId: string, tenantId: string): Promise<UserTenantLink | null> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT user_id, tenant_id, role, joined_at, metadata\n FROM lattice_user_tenant_links\n WHERE user_id = $1 AND tenant_id = $2\n `,\n [userId, tenantId]\n );\n \n if (result.rows.length === 0) return null;\n return this.mapRowToLink(result.rows[0]);\n }\n\n async createLink(data: CreateUserTenantLinkRequest): Promise<UserTenantLink> {\n await this.ensureInitialized();\n \n const now = new Date();\n const role = data.role || \"member\";\n const metadata = data.metadata || {};\n \n await this.pool.query(\n `\n INSERT INTO lattice_user_tenant_links (user_id, tenant_id, role, joined_at, metadata)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (user_id, tenant_id) DO UPDATE SET\n role = EXCLUDED.role,\n metadata = EXCLUDED.metadata\n `,\n [data.userId, data.tenantId, role, now, JSON.stringify(metadata)]\n );\n \n return {\n userId: data.userId,\n tenantId: data.tenantId,\n role,\n joinedAt: now,\n metadata: data.metadata,\n };\n }\n\n async updateLink(\n userId: string,\n tenantId: string,\n updates: UpdateUserTenantLinkRequest\n ): Promise<UserTenantLink | null> {\n await this.ensureInitialized();\n \n const existing = await this.getLink(userId, tenantId);\n if (!existing) return null;\n \n const updateFields: string[] = [];\n const updateValues: any[] = [];\n let paramIndex = 1;\n \n if (updates.role !== undefined) {\n updateFields.push(`role = $${paramIndex++}`);\n updateValues.push(updates.role);\n }\n \n if (updates.metadata !== undefined) {\n updateFields.push(`metadata = $${paramIndex++}`);\n updateValues.push(JSON.stringify(updates.metadata));\n }\n \n if (updateFields.length === 0) return existing;\n \n updateValues.push(userId, tenantId);\n \n await this.pool.query(\n `\n UPDATE lattice_user_tenant_links\n SET ${updateFields.join(\", \")}\n WHERE user_id = $${paramIndex} AND tenant_id = $${paramIndex + 1}\n `,\n updateValues\n );\n \n return this.getLink(userId, tenantId);\n }\n\n async deleteLink(userId: string, tenantId: string): Promise<boolean> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n DELETE FROM lattice_user_tenant_links\n WHERE user_id = $1 AND tenant_id = $2\n `,\n [userId, tenantId]\n );\n \n return result.rowCount !== null && result.rowCount > 0;\n }\n\n async hasLink(userId: string, tenantId: string): Promise<boolean> {\n await this.ensureInitialized();\n \n const result = await this.pool.query(\n `\n SELECT 1 FROM lattice_user_tenant_links\n WHERE user_id = $1 AND tenant_id = $2\n LIMIT 1\n `,\n [userId, tenantId]\n );\n \n return result.rows.length > 0;\n }\n}\n","import { PoolClient } from \"pg\";\nimport { Migration } from \"./migration\";\n\nexport const createUserTenantLinkTable: Migration = {\n version: 10,\n name: \"create_user_tenant_link_table\",\n up: async (client: PoolClient) => {\n await client.query(`\n CREATE TABLE IF NOT EXISTS lattice_user_tenant_links (\n user_id VARCHAR(255) NOT NULL,\n tenant_id VARCHAR(255) NOT NULL,\n role VARCHAR(50) NOT NULL DEFAULT 'member',\n joined_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n metadata JSONB DEFAULT '{}',\n PRIMARY KEY (user_id, tenant_id)\n )\n `);\n \n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_user_tenant_links_user_id \n ON lattice_user_tenant_links(user_id)\n `);\n \n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_user_tenant_links_tenant_id \n ON lattice_user_tenant_links(tenant_id)\n `);\n \n await client.query(`\n CREATE INDEX IF NOT EXISTS idx_user_tenant_links_role \n ON lattice_user_tenant_links(tenant_id, role)\n `);\n },\n down: async (client: PoolClient) => {\n await client.query(`DROP TABLE IF EXISTS lattice_user_tenant_links`);\n },\n};\n\nexport const migrateRemoveTenantIdFromUsers: Migration = {\n version: 11,\n name: \"migrate_remove_tenant_id_from_users\",\n up: async (client: PoolClient) => {\n const hasTenantId = await client.query(`\n SELECT column_name \n FROM information_schema.columns \n WHERE table_name = 'lattice_users' AND column_name = 'tenant_id'\n `);\n \n if (hasTenantId.rows.length > 0) {\n await client.query(`\n INSERT INTO lattice_user_tenant_links (user_id, tenant_id, role, joined_at)\n SELECT id, tenant_id, 'member', created_at\n FROM lattice_users\n WHERE tenant_id IS NOT NULL\n ON CONFLICT (user_id, tenant_id) DO NOTHING\n `);\n \n await client.query(`\n ALTER TABLE lattice_users DROP COLUMN IF EXISTS tenant_id\n `);\n \n await client.query(`\n ALTER TABLE lattice_users DROP CONSTRAINT IF EXISTS lattice_users_pkey\n `);\n \n await client.query(`\n ALTER TABLE lattice_users ADD PRIMARY KEY (id)\n `);\n }\n },\n down: async (client: PoolClient) => {\n await client.query(`\n ALTER TABLE lattice_users ADD COLUMN IF NOT EXISTS tenant_id VARCHAR(255)\n `);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIA,gBAA6C;;;ACwBtC,IAAM,mBAAN,MAAuB;AAAA,EAI5B,YAAY,MAAY;AAFxB,SAAQ,aAA0B,CAAC;AAGjC,SAAK,OAAO;AAAA,EACd;AAAA;AAAA;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