@axiom-lattice/pg-stores 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1116,13 +1116,351 @@ var PostgreSQLScheduleStorage = class {
1116
1116
  };
1117
1117
  }
1118
1118
  };
1119
+
1120
+ // src/stores/PostgreSQLSkillStore.ts
1121
+ import { Pool as Pool4 } from "pg";
1122
+
1123
+ // src/migrations/skill_migrations.ts
1124
+ var createSkillsTable = {
1125
+ version: 1,
1126
+ name: "create_skills_table",
1127
+ up: async (client) => {
1128
+ await client.query(`
1129
+ CREATE TABLE IF NOT EXISTS lattice_skills (
1130
+ id VARCHAR(255) PRIMARY KEY,
1131
+ name VARCHAR(255) NOT NULL,
1132
+ description TEXT NOT NULL,
1133
+ license VARCHAR(255),
1134
+ compatibility VARCHAR(255),
1135
+ metadata JSONB DEFAULT '{}',
1136
+ content TEXT,
1137
+ sub_skills JSONB DEFAULT '[]',
1138
+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
1139
+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
1140
+ )
1141
+ `);
1142
+ await client.query(`
1143
+ CREATE INDEX IF NOT EXISTS idx_lattice_skills_name
1144
+ ON lattice_skills(name)
1145
+ `);
1146
+ await client.query(`
1147
+ CREATE INDEX IF NOT EXISTS idx_lattice_skills_license
1148
+ ON lattice_skills(license)
1149
+ `);
1150
+ await client.query(`
1151
+ CREATE INDEX IF NOT EXISTS idx_lattice_skills_compatibility
1152
+ ON lattice_skills(compatibility)
1153
+ `);
1154
+ await client.query(`
1155
+ CREATE INDEX IF NOT EXISTS idx_lattice_skills_created_at
1156
+ ON lattice_skills(created_at DESC)
1157
+ `);
1158
+ await client.query(`
1159
+ CREATE INDEX IF NOT EXISTS idx_lattice_skills_metadata
1160
+ ON lattice_skills USING GIN (metadata)
1161
+ `);
1162
+ },
1163
+ down: async (client) => {
1164
+ await client.query("DROP INDEX IF EXISTS idx_lattice_skills_metadata");
1165
+ await client.query("DROP INDEX IF EXISTS idx_lattice_skills_created_at");
1166
+ await client.query("DROP INDEX IF EXISTS idx_lattice_skills_compatibility");
1167
+ await client.query("DROP INDEX IF EXISTS idx_lattice_skills_license");
1168
+ await client.query("DROP INDEX IF EXISTS idx_lattice_skills_name");
1169
+ await client.query("DROP TABLE IF EXISTS lattice_skills");
1170
+ }
1171
+ };
1172
+
1173
+ // src/stores/PostgreSQLSkillStore.ts
1174
+ var PostgreSQLSkillStore = class {
1175
+ constructor(options) {
1176
+ this.initialized = false;
1177
+ this.ownsPool = true;
1178
+ if (typeof options.poolConfig === "string") {
1179
+ this.pool = new Pool4({ connectionString: options.poolConfig });
1180
+ } else {
1181
+ this.pool = new Pool4(options.poolConfig);
1182
+ }
1183
+ this.migrationManager = new MigrationManager(this.pool);
1184
+ this.migrationManager.register(createSkillsTable);
1185
+ if (options.autoMigrate !== false) {
1186
+ this.initialize().catch((error) => {
1187
+ console.error("Failed to initialize PostgreSQLSkillStore:", error);
1188
+ throw error;
1189
+ });
1190
+ }
1191
+ }
1192
+ /**
1193
+ * Dispose resources and close the connection pool
1194
+ * Should be called when the store is no longer needed
1195
+ */
1196
+ async dispose() {
1197
+ if (this.ownsPool && this.pool) {
1198
+ await this.pool.end();
1199
+ }
1200
+ }
1201
+ /**
1202
+ * Initialize the store and run migrations
1203
+ */
1204
+ async initialize() {
1205
+ if (this.initialized) {
1206
+ return;
1207
+ }
1208
+ await this.migrationManager.migrate();
1209
+ this.initialized = true;
1210
+ }
1211
+ /**
1212
+ * Ensure store is initialized
1213
+ */
1214
+ async ensureInitialized() {
1215
+ if (!this.initialized) {
1216
+ await this.initialize();
1217
+ }
1218
+ }
1219
+ /**
1220
+ * Map database row to Skill object
1221
+ */
1222
+ mapRowToSkill(row) {
1223
+ return {
1224
+ id: row.id,
1225
+ name: row.name,
1226
+ description: row.description,
1227
+ license: row.license || void 0,
1228
+ compatibility: row.compatibility || void 0,
1229
+ metadata: row.metadata || {},
1230
+ content: row.content || void 0,
1231
+ subSkills: Array.isArray(row.sub_skills) && row.sub_skills.length > 0 ? row.sub_skills : void 0,
1232
+ createdAt: row.created_at,
1233
+ updatedAt: row.updated_at
1234
+ };
1235
+ }
1236
+ /**
1237
+ * Get all skills
1238
+ */
1239
+ async getAllSkills() {
1240
+ await this.ensureInitialized();
1241
+ const result = await this.pool.query(
1242
+ `
1243
+ SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at
1244
+ FROM lattice_skills
1245
+ ORDER BY created_at DESC
1246
+ `
1247
+ );
1248
+ return result.rows.map(this.mapRowToSkill);
1249
+ }
1250
+ /**
1251
+ * Get skill by ID
1252
+ */
1253
+ async getSkillById(id) {
1254
+ await this.ensureInitialized();
1255
+ const result = await this.pool.query(
1256
+ `
1257
+ SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at
1258
+ FROM lattice_skills
1259
+ WHERE id = $1
1260
+ `,
1261
+ [id]
1262
+ );
1263
+ if (result.rows.length === 0) {
1264
+ return null;
1265
+ }
1266
+ return this.mapRowToSkill(result.rows[0]);
1267
+ }
1268
+ /**
1269
+ * Create a new skill
1270
+ */
1271
+ async createSkill(id, data) {
1272
+ await this.ensureInitialized();
1273
+ const now = /* @__PURE__ */ new Date();
1274
+ const metadata = data.metadata || {};
1275
+ await this.pool.query(
1276
+ `
1277
+ INSERT INTO lattice_skills (id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at)
1278
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
1279
+ ON CONFLICT (id) DO UPDATE SET
1280
+ name = EXCLUDED.name,
1281
+ description = EXCLUDED.description,
1282
+ license = EXCLUDED.license,
1283
+ compatibility = EXCLUDED.compatibility,
1284
+ metadata = EXCLUDED.metadata,
1285
+ content = EXCLUDED.content,
1286
+ sub_skills = EXCLUDED.sub_skills,
1287
+ updated_at = EXCLUDED.updated_at
1288
+ `,
1289
+ [
1290
+ id,
1291
+ data.name,
1292
+ data.description,
1293
+ data.license || null,
1294
+ data.compatibility || null,
1295
+ JSON.stringify(metadata),
1296
+ data.content || null,
1297
+ JSON.stringify(data.subSkills || []),
1298
+ now,
1299
+ now
1300
+ ]
1301
+ );
1302
+ return this.getSkillById(id);
1303
+ }
1304
+ /**
1305
+ * Update an existing skill
1306
+ */
1307
+ async updateSkill(id, updates) {
1308
+ await this.ensureInitialized();
1309
+ const updateFields = [];
1310
+ const values = [];
1311
+ let paramIndex = 1;
1312
+ if (updates.name !== void 0) {
1313
+ updateFields.push(`name = $${paramIndex++}`);
1314
+ values.push(updates.name);
1315
+ }
1316
+ if (updates.description !== void 0) {
1317
+ updateFields.push(`description = $${paramIndex++}`);
1318
+ values.push(updates.description);
1319
+ }
1320
+ if (updates.license !== void 0) {
1321
+ updateFields.push(`license = $${paramIndex++}`);
1322
+ values.push(updates.license || null);
1323
+ }
1324
+ if (updates.compatibility !== void 0) {
1325
+ updateFields.push(`compatibility = $${paramIndex++}`);
1326
+ values.push(updates.compatibility || null);
1327
+ }
1328
+ if (updates.metadata !== void 0) {
1329
+ updateFields.push(`metadata = $${paramIndex++}`);
1330
+ values.push(JSON.stringify(updates.metadata || {}));
1331
+ }
1332
+ if (updates.content !== void 0) {
1333
+ updateFields.push(`content = $${paramIndex++}`);
1334
+ values.push(updates.content || null);
1335
+ }
1336
+ if (updates.subSkills !== void 0) {
1337
+ updateFields.push(`sub_skills = $${paramIndex++}`);
1338
+ values.push(JSON.stringify(updates.subSkills || []));
1339
+ }
1340
+ if (updateFields.length === 0) {
1341
+ return this.getSkillById(id);
1342
+ }
1343
+ updateFields.push(`updated_at = $${paramIndex++}`);
1344
+ values.push(/* @__PURE__ */ new Date());
1345
+ values.push(id);
1346
+ await this.pool.query(
1347
+ `
1348
+ UPDATE lattice_skills
1349
+ SET ${updateFields.join(", ")}
1350
+ WHERE id = $${paramIndex}
1351
+ `,
1352
+ values
1353
+ );
1354
+ return this.getSkillById(id);
1355
+ }
1356
+ /**
1357
+ * Delete a skill by ID
1358
+ */
1359
+ async deleteSkill(id) {
1360
+ await this.ensureInitialized();
1361
+ const result = await this.pool.query(
1362
+ `
1363
+ DELETE FROM lattice_skills
1364
+ WHERE id = $1
1365
+ `,
1366
+ [id]
1367
+ );
1368
+ return result.rowCount !== null && result.rowCount > 0;
1369
+ }
1370
+ /**
1371
+ * Check if skill exists
1372
+ */
1373
+ async hasSkill(id) {
1374
+ await this.ensureInitialized();
1375
+ const result = await this.pool.query(
1376
+ `
1377
+ SELECT COUNT(*) as count
1378
+ FROM lattice_skills
1379
+ WHERE id = $1
1380
+ `,
1381
+ [id]
1382
+ );
1383
+ return parseInt(result.rows[0].count, 10) > 0;
1384
+ }
1385
+ /**
1386
+ * Search skills by metadata
1387
+ */
1388
+ async searchByMetadata(metadataKey, metadataValue) {
1389
+ await this.ensureInitialized();
1390
+ const result = await this.pool.query(
1391
+ `
1392
+ SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at
1393
+ FROM lattice_skills
1394
+ WHERE metadata->>$1 = $2
1395
+ ORDER BY created_at DESC
1396
+ `,
1397
+ [metadataKey, metadataValue]
1398
+ );
1399
+ return result.rows.map(this.mapRowToSkill);
1400
+ }
1401
+ /**
1402
+ * Filter skills by compatibility
1403
+ */
1404
+ async filterByCompatibility(compatibility) {
1405
+ await this.ensureInitialized();
1406
+ const result = await this.pool.query(
1407
+ `
1408
+ SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at
1409
+ FROM lattice_skills
1410
+ WHERE compatibility = $1
1411
+ ORDER BY created_at DESC
1412
+ `,
1413
+ [compatibility]
1414
+ );
1415
+ return result.rows.map(this.mapRowToSkill);
1416
+ }
1417
+ /**
1418
+ * Filter skills by license
1419
+ */
1420
+ async filterByLicense(license) {
1421
+ await this.ensureInitialized();
1422
+ const result = await this.pool.query(
1423
+ `
1424
+ SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at
1425
+ FROM lattice_skills
1426
+ WHERE license = $1
1427
+ ORDER BY created_at DESC
1428
+ `,
1429
+ [license]
1430
+ );
1431
+ return result.rows.map(this.mapRowToSkill);
1432
+ }
1433
+ /**
1434
+ * Get sub-skills of a parent skill
1435
+ */
1436
+ async getSubSkills(parentSkillName) {
1437
+ await this.ensureInitialized();
1438
+ const parentSkill = await this.getSkillById(parentSkillName);
1439
+ if (!parentSkill || !parentSkill.subSkills || parentSkill.subSkills.length === 0) {
1440
+ return [];
1441
+ }
1442
+ const placeholders = parentSkill.subSkills.map((_, index) => `$${index + 1}`).join(", ");
1443
+ const result = await this.pool.query(
1444
+ `
1445
+ SELECT id, name, description, license, compatibility, metadata, content, sub_skills, created_at, updated_at
1446
+ FROM lattice_skills
1447
+ WHERE name IN (${placeholders})
1448
+ ORDER BY created_at DESC
1449
+ `,
1450
+ parentSkill.subSkills
1451
+ );
1452
+ return result.rows.map(this.mapRowToSkill);
1453
+ }
1454
+ };
1119
1455
  export {
1120
1456
  MigrationManager,
1121
1457
  PostgreSQLAssistantStore,
1122
1458
  PostgreSQLScheduleStorage,
1459
+ PostgreSQLSkillStore,
1123
1460
  PostgreSQLThreadStore,
1124
1461
  createAssistantsTable,
1125
1462
  createScheduledTasksTable,
1463
+ createSkillsTable,
1126
1464
  createThreadsTable
1127
1465
  };
1128
1466
  //# sourceMappingURL=index.mjs.map