@mastra/pg 1.0.0-beta.5 → 1.0.0-beta.7
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/CHANGELOG.md +76 -0
- package/dist/index.cjs +440 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +441 -33
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/agents/index.d.ts +25 -0
- package/dist/storage/domains/agents/index.d.ts.map +1 -0
- package/dist/storage/domains/workflows/index.d.ts +4 -0
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +5 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +31 -3
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/types.d.ts +10 -0
- package/dist/vector/types.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -506,12 +506,14 @@ var PgVector = class extends vector.MastraVector {
|
|
|
506
506
|
pool;
|
|
507
507
|
describeIndexCache = /* @__PURE__ */ new Map();
|
|
508
508
|
createdIndexes = /* @__PURE__ */ new Map();
|
|
509
|
+
indexVectorTypes = /* @__PURE__ */ new Map();
|
|
509
510
|
mutexesByName = /* @__PURE__ */ new Map();
|
|
510
511
|
schema;
|
|
511
512
|
setupSchemaPromise = null;
|
|
512
513
|
installVectorExtensionPromise = null;
|
|
513
514
|
vectorExtensionInstalled = void 0;
|
|
514
515
|
vectorExtensionSchema = null;
|
|
516
|
+
vectorExtensionVersion = null;
|
|
515
517
|
schemaSetupComplete = void 0;
|
|
516
518
|
cacheWarmupPromise = null;
|
|
517
519
|
constructor(config) {
|
|
@@ -564,9 +566,11 @@ var PgVector = class extends vector.MastraVector {
|
|
|
564
566
|
indexName,
|
|
565
567
|
metric: info.metric,
|
|
566
568
|
dimension: info.dimension,
|
|
567
|
-
type: info.type
|
|
569
|
+
type: info.type,
|
|
570
|
+
vectorType: info.vectorType
|
|
568
571
|
});
|
|
569
572
|
this.createdIndexes.set(indexName, key);
|
|
573
|
+
this.indexVectorTypes.set(indexName, info.vectorType);
|
|
570
574
|
})
|
|
571
575
|
);
|
|
572
576
|
} catch (error) {
|
|
@@ -592,12 +596,12 @@ var PgVector = class extends vector.MastraVector {
|
|
|
592
596
|
return this.mutexesByName.get(indexName);
|
|
593
597
|
}
|
|
594
598
|
/**
|
|
595
|
-
* Detects which schema contains the vector extension
|
|
599
|
+
* Detects which schema contains the vector extension and its version
|
|
596
600
|
*/
|
|
597
601
|
async detectVectorExtensionSchema(client) {
|
|
598
602
|
try {
|
|
599
603
|
const result = await client.query(`
|
|
600
|
-
SELECT n.nspname as schema_name
|
|
604
|
+
SELECT n.nspname as schema_name, e.extversion as version
|
|
601
605
|
FROM pg_extension e
|
|
602
606
|
JOIN pg_namespace n ON e.extnamespace = n.oid
|
|
603
607
|
WHERE e.extname = 'vector'
|
|
@@ -605,7 +609,11 @@ var PgVector = class extends vector.MastraVector {
|
|
|
605
609
|
`);
|
|
606
610
|
if (result.rows.length > 0) {
|
|
607
611
|
this.vectorExtensionSchema = result.rows[0].schema_name;
|
|
608
|
-
this.
|
|
612
|
+
this.vectorExtensionVersion = result.rows[0].version;
|
|
613
|
+
this.logger.debug("Vector extension found", {
|
|
614
|
+
schema: this.vectorExtensionSchema,
|
|
615
|
+
version: this.vectorExtensionVersion
|
|
616
|
+
});
|
|
609
617
|
return this.vectorExtensionSchema;
|
|
610
618
|
}
|
|
611
619
|
return null;
|
|
@@ -614,18 +622,52 @@ var PgVector = class extends vector.MastraVector {
|
|
|
614
622
|
return null;
|
|
615
623
|
}
|
|
616
624
|
}
|
|
625
|
+
/**
|
|
626
|
+
* Checks if the installed pgvector version supports halfvec type.
|
|
627
|
+
* halfvec was introduced in pgvector 0.7.0.
|
|
628
|
+
*/
|
|
629
|
+
supportsHalfvec() {
|
|
630
|
+
if (!this.vectorExtensionVersion) {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
const parts = this.vectorExtensionVersion.split(".");
|
|
634
|
+
const major = parseInt(parts[0] ?? "", 10);
|
|
635
|
+
const minor = parseInt(parts[1] ?? "", 10);
|
|
636
|
+
if (isNaN(major) || isNaN(minor)) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
return major > 0 || major === 0 && minor >= 7;
|
|
640
|
+
}
|
|
617
641
|
/**
|
|
618
642
|
* Gets the properly qualified vector type name
|
|
643
|
+
* @param vectorType - The type of vector storage ('vector' or 'halfvec')
|
|
619
644
|
*/
|
|
620
|
-
getVectorTypeName() {
|
|
645
|
+
getVectorTypeName(vectorType = "vector") {
|
|
621
646
|
if (this.vectorExtensionSchema) {
|
|
622
647
|
if (this.vectorExtensionSchema === "pg_catalog") {
|
|
623
|
-
return
|
|
648
|
+
return vectorType;
|
|
624
649
|
}
|
|
625
650
|
const validatedSchema = utils.parseSqlIdentifier(this.vectorExtensionSchema, "vector extension schema");
|
|
626
|
-
return `${validatedSchema}
|
|
651
|
+
return `${validatedSchema}.${vectorType}`;
|
|
652
|
+
}
|
|
653
|
+
return vectorType;
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Gets the operator class for index creation based on metric and vector type.
|
|
657
|
+
* pgvector uses different operator classes for vector vs halfvec types.
|
|
658
|
+
*/
|
|
659
|
+
getMetricOperatorClass(metric, vectorType) {
|
|
660
|
+
const prefix = vectorType === "halfvec" ? "halfvec" : "vector";
|
|
661
|
+
switch (metric) {
|
|
662
|
+
case "cosine":
|
|
663
|
+
return `${prefix}_cosine_ops`;
|
|
664
|
+
case "euclidean":
|
|
665
|
+
return `${prefix}_l2_ops`;
|
|
666
|
+
case "dotproduct":
|
|
667
|
+
return `${prefix}_ip_ops`;
|
|
668
|
+
default:
|
|
669
|
+
return `${prefix}_cosine_ops`;
|
|
627
670
|
}
|
|
628
|
-
return "vector";
|
|
629
671
|
}
|
|
630
672
|
getTableName(indexName) {
|
|
631
673
|
const parsedIndexName = utils.parseSqlIdentifier(indexName, "index name");
|
|
@@ -698,12 +740,12 @@ var PgVector = class extends vector.MastraVector {
|
|
|
698
740
|
await client.query(`SET LOCAL ivfflat.probes = ${probes}`);
|
|
699
741
|
}
|
|
700
742
|
const { tableName } = this.getTableName(indexName);
|
|
701
|
-
const
|
|
743
|
+
const qualifiedVectorType = this.getVectorTypeName(indexInfo.vectorType);
|
|
702
744
|
const query = `
|
|
703
745
|
WITH vector_scores AS (
|
|
704
746
|
SELECT
|
|
705
747
|
vector_id as id,
|
|
706
|
-
1 - (embedding <=> '${vectorStr}'::${
|
|
748
|
+
1 - (embedding <=> '${vectorStr}'::${qualifiedVectorType}) as score,
|
|
707
749
|
metadata
|
|
708
750
|
${includeVector ? ", embedding" : ""}
|
|
709
751
|
FROM ${tableName}
|
|
@@ -767,14 +809,15 @@ var PgVector = class extends vector.MastraVector {
|
|
|
767
809
|
}
|
|
768
810
|
}
|
|
769
811
|
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
770
|
-
const
|
|
812
|
+
const indexInfo = await this.getIndexInfo({ indexName });
|
|
813
|
+
const qualifiedVectorType = this.getVectorTypeName(indexInfo.vectorType);
|
|
771
814
|
for (let i = 0; i < vectors.length; i++) {
|
|
772
815
|
const query = `
|
|
773
816
|
INSERT INTO ${tableName} (vector_id, embedding, metadata)
|
|
774
|
-
VALUES ($1, $2::${
|
|
817
|
+
VALUES ($1, $2::${qualifiedVectorType}, $3::jsonb)
|
|
775
818
|
ON CONFLICT (vector_id)
|
|
776
819
|
DO UPDATE SET
|
|
777
|
-
embedding = $2::${
|
|
820
|
+
embedding = $2::${qualifiedVectorType},
|
|
778
821
|
metadata = $3::jsonb
|
|
779
822
|
RETURNING embedding::text
|
|
780
823
|
`;
|
|
@@ -833,9 +876,10 @@ var PgVector = class extends vector.MastraVector {
|
|
|
833
876
|
indexName,
|
|
834
877
|
dimension,
|
|
835
878
|
metric,
|
|
836
|
-
type
|
|
879
|
+
type,
|
|
880
|
+
vectorType = "vector"
|
|
837
881
|
}) {
|
|
838
|
-
const input = indexName + dimension + metric + (type || "ivfflat");
|
|
882
|
+
const input = indexName + dimension + metric + (type || "ivfflat") + vectorType;
|
|
839
883
|
return (await this.hasher).h32(input);
|
|
840
884
|
}
|
|
841
885
|
cachedIndexExists(indexName, newKey) {
|
|
@@ -888,7 +932,8 @@ var PgVector = class extends vector.MastraVector {
|
|
|
888
932
|
dimension,
|
|
889
933
|
metric = "cosine",
|
|
890
934
|
indexConfig = {},
|
|
891
|
-
buildIndex = true
|
|
935
|
+
buildIndex = true,
|
|
936
|
+
vectorType = "vector"
|
|
892
937
|
}) {
|
|
893
938
|
const { tableName } = this.getTableName(indexName);
|
|
894
939
|
try {
|
|
@@ -898,6 +943,9 @@ var PgVector = class extends vector.MastraVector {
|
|
|
898
943
|
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
899
944
|
throw new Error("Dimension must be a positive integer");
|
|
900
945
|
}
|
|
946
|
+
if (vectorType !== "vector" && vectorType !== "halfvec") {
|
|
947
|
+
throw new Error('vectorType must be "vector" or "halfvec"');
|
|
948
|
+
}
|
|
901
949
|
} catch (error$1) {
|
|
902
950
|
const mastraError = new error.MastraError(
|
|
903
951
|
{
|
|
@@ -913,7 +961,13 @@ var PgVector = class extends vector.MastraVector {
|
|
|
913
961
|
this.logger?.trackException(mastraError);
|
|
914
962
|
throw mastraError;
|
|
915
963
|
}
|
|
916
|
-
const indexCacheKey = await this.getIndexCacheKey({
|
|
964
|
+
const indexCacheKey = await this.getIndexCacheKey({
|
|
965
|
+
indexName,
|
|
966
|
+
dimension,
|
|
967
|
+
type: indexConfig.type,
|
|
968
|
+
metric,
|
|
969
|
+
vectorType
|
|
970
|
+
});
|
|
917
971
|
if (this.cachedIndexExists(indexName, indexCacheKey)) {
|
|
918
972
|
return;
|
|
919
973
|
}
|
|
@@ -926,24 +980,40 @@ var PgVector = class extends vector.MastraVector {
|
|
|
926
980
|
try {
|
|
927
981
|
await this.setupSchema(client);
|
|
928
982
|
await this.installVectorExtension(client);
|
|
983
|
+
if (vectorType === "halfvec" && !this.supportsHalfvec()) {
|
|
984
|
+
throw new error.MastraError({
|
|
985
|
+
id: storage.createVectorErrorId("PG", "CREATE_INDEX", "HALFVEC_NOT_SUPPORTED"),
|
|
986
|
+
text: `halfvec type requires pgvector >= 0.7.0, but version ${this.vectorExtensionVersion || "unknown"} is installed. Either upgrade pgvector or use vectorType: 'vector' (which supports up to 2000 dimensions for indexes).`,
|
|
987
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
988
|
+
category: error.ErrorCategory.USER,
|
|
989
|
+
details: {
|
|
990
|
+
indexName,
|
|
991
|
+
requestedVectorType: vectorType,
|
|
992
|
+
pgvectorVersion: this.vectorExtensionVersion || "unknown",
|
|
993
|
+
requiredVersion: "0.7.0"
|
|
994
|
+
}
|
|
995
|
+
});
|
|
996
|
+
}
|
|
929
997
|
if (this.schema && this.vectorExtensionSchema && this.schema !== this.vectorExtensionSchema && this.vectorExtensionSchema !== "pg_catalog") {
|
|
930
998
|
await client.query(`SET search_path TO ${this.getSchemaName()}, "${this.vectorExtensionSchema}"`);
|
|
931
999
|
}
|
|
932
|
-
const
|
|
1000
|
+
const qualifiedVectorType = this.getVectorTypeName(vectorType);
|
|
933
1001
|
await client.query(`
|
|
934
1002
|
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
935
1003
|
id SERIAL PRIMARY KEY,
|
|
936
1004
|
vector_id TEXT UNIQUE NOT NULL,
|
|
937
|
-
embedding ${
|
|
1005
|
+
embedding ${qualifiedVectorType}(${dimension}),
|
|
938
1006
|
metadata JSONB DEFAULT '{}'::jsonb
|
|
939
1007
|
);
|
|
940
1008
|
`);
|
|
941
1009
|
this.createdIndexes.set(indexName, indexCacheKey);
|
|
1010
|
+
this.indexVectorTypes.set(indexName, vectorType);
|
|
942
1011
|
if (buildIndex) {
|
|
943
|
-
await this.setupIndex({ indexName, metric, indexConfig }, client);
|
|
1012
|
+
await this.setupIndex({ indexName, metric, indexConfig, vectorType }, client);
|
|
944
1013
|
}
|
|
945
1014
|
} catch (error) {
|
|
946
1015
|
this.createdIndexes.delete(indexName);
|
|
1016
|
+
this.indexVectorTypes.delete(indexName);
|
|
947
1017
|
throw error;
|
|
948
1018
|
} finally {
|
|
949
1019
|
client.release();
|
|
@@ -986,7 +1056,7 @@ var PgVector = class extends vector.MastraVector {
|
|
|
986
1056
|
client.release();
|
|
987
1057
|
}
|
|
988
1058
|
}
|
|
989
|
-
async setupIndex({ indexName, metric, indexConfig }, client) {
|
|
1059
|
+
async setupIndex({ indexName, metric, indexConfig, vectorType = "vector" }, client) {
|
|
990
1060
|
const mutex = this.getMutexByName(`build-${indexName}`);
|
|
991
1061
|
await mutex.runExclusive(async () => {
|
|
992
1062
|
const isConfigEmpty = !indexConfig || Object.keys(indexConfig).length === 0 || !indexConfig.type && !indexConfig.ivf && !indexConfig.hnsw;
|
|
@@ -1008,9 +1078,11 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1008
1078
|
indexName,
|
|
1009
1079
|
dimension,
|
|
1010
1080
|
type: existingIndexInfo.type,
|
|
1011
|
-
metric: existingIndexInfo.metric
|
|
1081
|
+
metric: existingIndexInfo.metric,
|
|
1082
|
+
vectorType: existingIndexInfo.vectorType
|
|
1012
1083
|
});
|
|
1013
1084
|
this.createdIndexes.set(indexName, cacheKey);
|
|
1085
|
+
this.indexVectorTypes.set(indexName, existingIndexInfo.vectorType);
|
|
1014
1086
|
return;
|
|
1015
1087
|
}
|
|
1016
1088
|
}
|
|
@@ -1028,9 +1100,11 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1028
1100
|
indexName,
|
|
1029
1101
|
dimension,
|
|
1030
1102
|
type: existingIndexInfo.type,
|
|
1031
|
-
metric: existingIndexInfo.metric
|
|
1103
|
+
metric: existingIndexInfo.metric,
|
|
1104
|
+
vectorType: existingIndexInfo.vectorType
|
|
1032
1105
|
});
|
|
1033
1106
|
this.createdIndexes.set(indexName, cacheKey);
|
|
1107
|
+
this.indexVectorTypes.set(indexName, existingIndexInfo.vectorType);
|
|
1034
1108
|
return;
|
|
1035
1109
|
}
|
|
1036
1110
|
this.logger?.info(`Index ${vectorIndexName} configuration changed, rebuilding index`);
|
|
@@ -1043,7 +1117,8 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1043
1117
|
this.describeIndexCache.delete(indexName);
|
|
1044
1118
|
return;
|
|
1045
1119
|
}
|
|
1046
|
-
const
|
|
1120
|
+
const effectiveVectorType = existingIndexInfo?.vectorType ?? vectorType;
|
|
1121
|
+
const metricOp = this.getMetricOperatorClass(metric, effectiveVectorType);
|
|
1047
1122
|
let indexSQL;
|
|
1048
1123
|
if (indexType === "hnsw") {
|
|
1049
1124
|
const m = indexConfig.hnsw?.m ?? 8;
|
|
@@ -1093,6 +1168,12 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1093
1168
|
if (this.schema && this.schema !== "public") {
|
|
1094
1169
|
try {
|
|
1095
1170
|
await client.query(`CREATE EXTENSION IF NOT EXISTS vector SCHEMA ${this.getSchemaName()}`);
|
|
1171
|
+
const installedSchema2 = await this.detectVectorExtensionSchema(client);
|
|
1172
|
+
if (installedSchema2) {
|
|
1173
|
+
this.vectorExtensionInstalled = true;
|
|
1174
|
+
this.logger.info(`Vector extension installed in schema: ${installedSchema2}`);
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1096
1177
|
this.vectorExtensionInstalled = true;
|
|
1097
1178
|
this.vectorExtensionSchema = this.schema;
|
|
1098
1179
|
this.logger.info(`Vector extension installed in schema: ${this.schema}`);
|
|
@@ -1155,7 +1236,7 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1155
1236
|
WHERE c.table_schema = t.table_schema
|
|
1156
1237
|
AND c.table_name = t.table_name
|
|
1157
1238
|
AND c.column_name = 'embedding'
|
|
1158
|
-
AND c.udt_name
|
|
1239
|
+
AND c.udt_name IN ('vector', 'halfvec')
|
|
1159
1240
|
)
|
|
1160
1241
|
AND EXISTS (
|
|
1161
1242
|
SELECT 1
|
|
@@ -1194,17 +1275,18 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1194
1275
|
try {
|
|
1195
1276
|
const { tableName } = this.getTableName(indexName);
|
|
1196
1277
|
const tableExistsQuery = `
|
|
1197
|
-
SELECT
|
|
1278
|
+
SELECT udt_name
|
|
1198
1279
|
FROM information_schema.columns
|
|
1199
1280
|
WHERE table_schema = $1
|
|
1200
1281
|
AND table_name = $2
|
|
1201
|
-
AND udt_name
|
|
1282
|
+
AND udt_name IN ('vector', 'halfvec')
|
|
1202
1283
|
LIMIT 1;
|
|
1203
1284
|
`;
|
|
1204
1285
|
const tableExists = await client.query(tableExistsQuery, [this.schema || "public", indexName]);
|
|
1205
1286
|
if (tableExists.rows.length === 0) {
|
|
1206
1287
|
throw new Error(`Vector table ${tableName} does not exist`);
|
|
1207
1288
|
}
|
|
1289
|
+
const vectorType = tableExists.rows[0].udt_name === "halfvec" ? "halfvec" : "vector";
|
|
1208
1290
|
const dimensionQuery = `
|
|
1209
1291
|
SELECT atttypmod as dimension
|
|
1210
1292
|
FROM pg_attribute
|
|
@@ -1254,6 +1336,7 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1254
1336
|
count: parseInt(countResult.rows[0].count),
|
|
1255
1337
|
metric,
|
|
1256
1338
|
type: index_method,
|
|
1339
|
+
vectorType,
|
|
1257
1340
|
config
|
|
1258
1341
|
};
|
|
1259
1342
|
} catch (e) {
|
|
@@ -1281,6 +1364,8 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1281
1364
|
const { tableName } = this.getTableName(indexName);
|
|
1282
1365
|
await client.query(`DROP TABLE IF EXISTS ${tableName} CASCADE`);
|
|
1283
1366
|
this.createdIndexes.delete(indexName);
|
|
1367
|
+
this.indexVectorTypes.delete(indexName);
|
|
1368
|
+
this.describeIndexCache.delete(indexName);
|
|
1284
1369
|
} catch (error$1) {
|
|
1285
1370
|
await client.query("ROLLBACK");
|
|
1286
1371
|
const mastraError = new error.MastraError(
|
|
@@ -1369,12 +1454,13 @@ var PgVector = class extends vector.MastraVector {
|
|
|
1369
1454
|
}
|
|
1370
1455
|
client = await this.pool.connect();
|
|
1371
1456
|
const { tableName } = this.getTableName(indexName);
|
|
1372
|
-
const
|
|
1457
|
+
const indexInfo = await this.getIndexInfo({ indexName });
|
|
1458
|
+
const qualifiedVectorType = this.getVectorTypeName(indexInfo.vectorType);
|
|
1373
1459
|
let updateParts = [];
|
|
1374
1460
|
let values = [];
|
|
1375
1461
|
let valueIndex = 1;
|
|
1376
1462
|
if (update.vector) {
|
|
1377
|
-
updateParts.push(`embedding = $${valueIndex}::${
|
|
1463
|
+
updateParts.push(`embedding = $${valueIndex}::${qualifiedVectorType}`);
|
|
1378
1464
|
values.push(`[${update.vector.join(",")}]`);
|
|
1379
1465
|
valueIndex++;
|
|
1380
1466
|
}
|
|
@@ -1663,7 +1749,302 @@ function transformFromSqlRow({
|
|
|
1663
1749
|
return result;
|
|
1664
1750
|
}
|
|
1665
1751
|
|
|
1666
|
-
// src/storage/domains/
|
|
1752
|
+
// src/storage/domains/agents/index.ts
|
|
1753
|
+
var AgentsPG = class extends storage.AgentsStorage {
|
|
1754
|
+
client;
|
|
1755
|
+
schema;
|
|
1756
|
+
constructor({ client, schema }) {
|
|
1757
|
+
super();
|
|
1758
|
+
this.client = client;
|
|
1759
|
+
this.schema = schema;
|
|
1760
|
+
}
|
|
1761
|
+
parseJson(value, fieldName) {
|
|
1762
|
+
if (!value) return void 0;
|
|
1763
|
+
if (typeof value !== "string") return value;
|
|
1764
|
+
try {
|
|
1765
|
+
return JSON.parse(value);
|
|
1766
|
+
} catch (error$1) {
|
|
1767
|
+
const details = {
|
|
1768
|
+
value: value.length > 100 ? value.substring(0, 100) + "..." : value
|
|
1769
|
+
};
|
|
1770
|
+
if (fieldName) {
|
|
1771
|
+
details.field = fieldName;
|
|
1772
|
+
}
|
|
1773
|
+
throw new error.MastraError(
|
|
1774
|
+
{
|
|
1775
|
+
id: storage.createStorageErrorId("PG", "PARSE_JSON", "INVALID_JSON"),
|
|
1776
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1777
|
+
category: error.ErrorCategory.SYSTEM,
|
|
1778
|
+
text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error$1 instanceof Error ? error$1.message : "Unknown error"}`,
|
|
1779
|
+
details
|
|
1780
|
+
},
|
|
1781
|
+
error$1
|
|
1782
|
+
);
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
parseRow(row) {
|
|
1786
|
+
return {
|
|
1787
|
+
id: row.id,
|
|
1788
|
+
name: row.name,
|
|
1789
|
+
description: row.description,
|
|
1790
|
+
instructions: row.instructions,
|
|
1791
|
+
model: this.parseJson(row.model, "model"),
|
|
1792
|
+
tools: this.parseJson(row.tools, "tools"),
|
|
1793
|
+
defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
|
|
1794
|
+
workflows: this.parseJson(row.workflows, "workflows"),
|
|
1795
|
+
agents: this.parseJson(row.agents, "agents"),
|
|
1796
|
+
inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
|
|
1797
|
+
outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
|
|
1798
|
+
memory: this.parseJson(row.memory, "memory"),
|
|
1799
|
+
scorers: this.parseJson(row.scorers, "scorers"),
|
|
1800
|
+
metadata: this.parseJson(row.metadata, "metadata"),
|
|
1801
|
+
createdAt: row.createdAtZ || row.createdAt,
|
|
1802
|
+
updatedAt: row.updatedAtZ || row.updatedAt
|
|
1803
|
+
};
|
|
1804
|
+
}
|
|
1805
|
+
async getAgentById({ id }) {
|
|
1806
|
+
try {
|
|
1807
|
+
const tableName = getTableName({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
|
|
1808
|
+
const result = await this.client.oneOrNone(`SELECT * FROM ${tableName} WHERE id = $1`, [id]);
|
|
1809
|
+
if (!result) {
|
|
1810
|
+
return null;
|
|
1811
|
+
}
|
|
1812
|
+
return this.parseRow(result);
|
|
1813
|
+
} catch (error$1) {
|
|
1814
|
+
throw new error.MastraError(
|
|
1815
|
+
{
|
|
1816
|
+
id: storage.createStorageErrorId("PG", "GET_AGENT_BY_ID", "FAILED"),
|
|
1817
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1818
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1819
|
+
details: { agentId: id }
|
|
1820
|
+
},
|
|
1821
|
+
error$1
|
|
1822
|
+
);
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
async createAgent({ agent }) {
|
|
1826
|
+
try {
|
|
1827
|
+
const tableName = getTableName({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
|
|
1828
|
+
const now = /* @__PURE__ */ new Date();
|
|
1829
|
+
const nowIso = now.toISOString();
|
|
1830
|
+
await this.client.none(
|
|
1831
|
+
`INSERT INTO ${tableName} (
|
|
1832
|
+
id, name, description, instructions, model, tools,
|
|
1833
|
+
"defaultOptions", workflows, agents, "inputProcessors", "outputProcessors", memory, scorers, metadata,
|
|
1834
|
+
"createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
|
|
1835
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)`,
|
|
1836
|
+
[
|
|
1837
|
+
agent.id,
|
|
1838
|
+
agent.name,
|
|
1839
|
+
agent.description ?? null,
|
|
1840
|
+
agent.instructions,
|
|
1841
|
+
JSON.stringify(agent.model),
|
|
1842
|
+
agent.tools ? JSON.stringify(agent.tools) : null,
|
|
1843
|
+
agent.defaultOptions ? JSON.stringify(agent.defaultOptions) : null,
|
|
1844
|
+
agent.workflows ? JSON.stringify(agent.workflows) : null,
|
|
1845
|
+
agent.agents ? JSON.stringify(agent.agents) : null,
|
|
1846
|
+
agent.inputProcessors ? JSON.stringify(agent.inputProcessors) : null,
|
|
1847
|
+
agent.outputProcessors ? JSON.stringify(agent.outputProcessors) : null,
|
|
1848
|
+
agent.memory ? JSON.stringify(agent.memory) : null,
|
|
1849
|
+
agent.scorers ? JSON.stringify(agent.scorers) : null,
|
|
1850
|
+
agent.metadata ? JSON.stringify(agent.metadata) : null,
|
|
1851
|
+
nowIso,
|
|
1852
|
+
nowIso,
|
|
1853
|
+
nowIso,
|
|
1854
|
+
nowIso
|
|
1855
|
+
]
|
|
1856
|
+
);
|
|
1857
|
+
return {
|
|
1858
|
+
...agent,
|
|
1859
|
+
createdAt: now,
|
|
1860
|
+
updatedAt: now
|
|
1861
|
+
};
|
|
1862
|
+
} catch (error$1) {
|
|
1863
|
+
throw new error.MastraError(
|
|
1864
|
+
{
|
|
1865
|
+
id: storage.createStorageErrorId("PG", "CREATE_AGENT", "FAILED"),
|
|
1866
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1867
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1868
|
+
details: { agentId: agent.id }
|
|
1869
|
+
},
|
|
1870
|
+
error$1
|
|
1871
|
+
);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
async updateAgent({ id, ...updates }) {
|
|
1875
|
+
try {
|
|
1876
|
+
const tableName = getTableName({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
|
|
1877
|
+
const existingAgent = await this.getAgentById({ id });
|
|
1878
|
+
if (!existingAgent) {
|
|
1879
|
+
throw new error.MastraError({
|
|
1880
|
+
id: storage.createStorageErrorId("PG", "UPDATE_AGENT", "NOT_FOUND"),
|
|
1881
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1882
|
+
category: error.ErrorCategory.USER,
|
|
1883
|
+
text: `Agent ${id} not found`,
|
|
1884
|
+
details: { agentId: id }
|
|
1885
|
+
});
|
|
1886
|
+
}
|
|
1887
|
+
const setClauses = [];
|
|
1888
|
+
const values = [];
|
|
1889
|
+
let paramIndex = 1;
|
|
1890
|
+
if (updates.name !== void 0) {
|
|
1891
|
+
setClauses.push(`name = $${paramIndex++}`);
|
|
1892
|
+
values.push(updates.name);
|
|
1893
|
+
}
|
|
1894
|
+
if (updates.description !== void 0) {
|
|
1895
|
+
setClauses.push(`description = $${paramIndex++}`);
|
|
1896
|
+
values.push(updates.description);
|
|
1897
|
+
}
|
|
1898
|
+
if (updates.instructions !== void 0) {
|
|
1899
|
+
setClauses.push(`instructions = $${paramIndex++}`);
|
|
1900
|
+
values.push(updates.instructions);
|
|
1901
|
+
}
|
|
1902
|
+
if (updates.model !== void 0) {
|
|
1903
|
+
setClauses.push(`model = $${paramIndex++}`);
|
|
1904
|
+
values.push(JSON.stringify(updates.model));
|
|
1905
|
+
}
|
|
1906
|
+
if (updates.tools !== void 0) {
|
|
1907
|
+
setClauses.push(`tools = $${paramIndex++}`);
|
|
1908
|
+
values.push(JSON.stringify(updates.tools));
|
|
1909
|
+
}
|
|
1910
|
+
if (updates.defaultOptions !== void 0) {
|
|
1911
|
+
setClauses.push(`"defaultOptions" = $${paramIndex++}`);
|
|
1912
|
+
values.push(JSON.stringify(updates.defaultOptions));
|
|
1913
|
+
}
|
|
1914
|
+
if (updates.workflows !== void 0) {
|
|
1915
|
+
setClauses.push(`workflows = $${paramIndex++}`);
|
|
1916
|
+
values.push(JSON.stringify(updates.workflows));
|
|
1917
|
+
}
|
|
1918
|
+
if (updates.agents !== void 0) {
|
|
1919
|
+
setClauses.push(`agents = $${paramIndex++}`);
|
|
1920
|
+
values.push(JSON.stringify(updates.agents));
|
|
1921
|
+
}
|
|
1922
|
+
if (updates.inputProcessors !== void 0) {
|
|
1923
|
+
setClauses.push(`"inputProcessors" = $${paramIndex++}`);
|
|
1924
|
+
values.push(JSON.stringify(updates.inputProcessors));
|
|
1925
|
+
}
|
|
1926
|
+
if (updates.outputProcessors !== void 0) {
|
|
1927
|
+
setClauses.push(`"outputProcessors" = $${paramIndex++}`);
|
|
1928
|
+
values.push(JSON.stringify(updates.outputProcessors));
|
|
1929
|
+
}
|
|
1930
|
+
if (updates.memory !== void 0) {
|
|
1931
|
+
setClauses.push(`memory = $${paramIndex++}`);
|
|
1932
|
+
values.push(JSON.stringify(updates.memory));
|
|
1933
|
+
}
|
|
1934
|
+
if (updates.scorers !== void 0) {
|
|
1935
|
+
setClauses.push(`scorers = $${paramIndex++}`);
|
|
1936
|
+
values.push(JSON.stringify(updates.scorers));
|
|
1937
|
+
}
|
|
1938
|
+
if (updates.metadata !== void 0) {
|
|
1939
|
+
const mergedMetadata = { ...existingAgent.metadata, ...updates.metadata };
|
|
1940
|
+
setClauses.push(`metadata = $${paramIndex++}`);
|
|
1941
|
+
values.push(JSON.stringify(mergedMetadata));
|
|
1942
|
+
}
|
|
1943
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1944
|
+
setClauses.push(`"updatedAt" = $${paramIndex++}`);
|
|
1945
|
+
values.push(now);
|
|
1946
|
+
setClauses.push(`"updatedAtZ" = $${paramIndex++}`);
|
|
1947
|
+
values.push(now);
|
|
1948
|
+
values.push(id);
|
|
1949
|
+
if (setClauses.length > 2) {
|
|
1950
|
+
await this.client.none(`UPDATE ${tableName} SET ${setClauses.join(", ")} WHERE id = $${paramIndex}`, values);
|
|
1951
|
+
}
|
|
1952
|
+
const updatedAgent = await this.getAgentById({ id });
|
|
1953
|
+
if (!updatedAgent) {
|
|
1954
|
+
throw new error.MastraError({
|
|
1955
|
+
id: storage.createStorageErrorId("PG", "UPDATE_AGENT", "NOT_FOUND_AFTER_UPDATE"),
|
|
1956
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1957
|
+
category: error.ErrorCategory.SYSTEM,
|
|
1958
|
+
text: `Agent ${id} not found after update`,
|
|
1959
|
+
details: { agentId: id }
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
1962
|
+
return updatedAgent;
|
|
1963
|
+
} catch (error$1) {
|
|
1964
|
+
if (error$1 instanceof error.MastraError) {
|
|
1965
|
+
throw error$1;
|
|
1966
|
+
}
|
|
1967
|
+
throw new error.MastraError(
|
|
1968
|
+
{
|
|
1969
|
+
id: storage.createStorageErrorId("PG", "UPDATE_AGENT", "FAILED"),
|
|
1970
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1971
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1972
|
+
details: { agentId: id }
|
|
1973
|
+
},
|
|
1974
|
+
error$1
|
|
1975
|
+
);
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
async deleteAgent({ id }) {
|
|
1979
|
+
try {
|
|
1980
|
+
const tableName = getTableName({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
|
|
1981
|
+
await this.client.none(`DELETE FROM ${tableName} WHERE id = $1`, [id]);
|
|
1982
|
+
} catch (error$1) {
|
|
1983
|
+
throw new error.MastraError(
|
|
1984
|
+
{
|
|
1985
|
+
id: storage.createStorageErrorId("PG", "DELETE_AGENT", "FAILED"),
|
|
1986
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1987
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1988
|
+
details: { agentId: id }
|
|
1989
|
+
},
|
|
1990
|
+
error$1
|
|
1991
|
+
);
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
async listAgents(args) {
|
|
1995
|
+
const { page = 0, perPage: perPageInput, orderBy } = args || {};
|
|
1996
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1997
|
+
if (page < 0) {
|
|
1998
|
+
throw new error.MastraError(
|
|
1999
|
+
{
|
|
2000
|
+
id: storage.createStorageErrorId("PG", "LIST_AGENTS", "INVALID_PAGE"),
|
|
2001
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2002
|
+
category: error.ErrorCategory.USER,
|
|
2003
|
+
details: { page }
|
|
2004
|
+
},
|
|
2005
|
+
new Error("page must be >= 0")
|
|
2006
|
+
);
|
|
2007
|
+
}
|
|
2008
|
+
const perPage = storage.normalizePerPage(perPageInput, 100);
|
|
2009
|
+
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
2010
|
+
try {
|
|
2011
|
+
const tableName = getTableName({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName(this.schema) });
|
|
2012
|
+
const countResult = await this.client.one(`SELECT COUNT(*) as count FROM ${tableName}`);
|
|
2013
|
+
const total = parseInt(countResult.count, 10);
|
|
2014
|
+
if (total === 0) {
|
|
2015
|
+
return {
|
|
2016
|
+
agents: [],
|
|
2017
|
+
total: 0,
|
|
2018
|
+
page,
|
|
2019
|
+
perPage: perPageForResponse,
|
|
2020
|
+
hasMore: false
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
2024
|
+
const dataResult = await this.client.manyOrNone(
|
|
2025
|
+
`SELECT * FROM ${tableName} ORDER BY "${field}" ${direction} LIMIT $1 OFFSET $2`,
|
|
2026
|
+
[limitValue, offset]
|
|
2027
|
+
);
|
|
2028
|
+
const agents = (dataResult || []).map((row) => this.parseRow(row));
|
|
2029
|
+
return {
|
|
2030
|
+
agents,
|
|
2031
|
+
total,
|
|
2032
|
+
page,
|
|
2033
|
+
perPage: perPageForResponse,
|
|
2034
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
2035
|
+
};
|
|
2036
|
+
} catch (error$1) {
|
|
2037
|
+
throw new error.MastraError(
|
|
2038
|
+
{
|
|
2039
|
+
id: storage.createStorageErrorId("PG", "LIST_AGENTS", "FAILED"),
|
|
2040
|
+
domain: error.ErrorDomain.STORAGE,
|
|
2041
|
+
category: error.ErrorCategory.THIRD_PARTY
|
|
2042
|
+
},
|
|
2043
|
+
error$1
|
|
2044
|
+
);
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
};
|
|
1667
2048
|
var MemoryPG = class extends storage.MemoryStorage {
|
|
1668
2049
|
client;
|
|
1669
2050
|
schema;
|
|
@@ -4079,6 +4460,27 @@ var WorkflowsPG = class extends storage.WorkflowsStorage {
|
|
|
4079
4460
|
);
|
|
4080
4461
|
}
|
|
4081
4462
|
}
|
|
4463
|
+
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
4464
|
+
try {
|
|
4465
|
+
await this.client.none(
|
|
4466
|
+
`DELETE FROM ${getTableName({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: this.schema })} WHERE run_id = $1 AND workflow_name = $2`,
|
|
4467
|
+
[runId, workflowName]
|
|
4468
|
+
);
|
|
4469
|
+
} catch (error$1) {
|
|
4470
|
+
throw new error.MastraError(
|
|
4471
|
+
{
|
|
4472
|
+
id: storage.createStorageErrorId("PG", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
|
|
4473
|
+
domain: error.ErrorDomain.STORAGE,
|
|
4474
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
4475
|
+
details: {
|
|
4476
|
+
runId,
|
|
4477
|
+
workflowName
|
|
4478
|
+
}
|
|
4479
|
+
},
|
|
4480
|
+
error$1
|
|
4481
|
+
);
|
|
4482
|
+
}
|
|
4483
|
+
}
|
|
4082
4484
|
async listWorkflowRuns({
|
|
4083
4485
|
workflowName,
|
|
4084
4486
|
fromDate,
|
|
@@ -4214,12 +4616,14 @@ var PostgresStore = class extends storage.MastraStorage {
|
|
|
4214
4616
|
const workflows = new WorkflowsPG({ client: this.#db, operations, schema: this.schema });
|
|
4215
4617
|
const memory = new MemoryPG({ client: this.#db, schema: this.schema, operations });
|
|
4216
4618
|
const observability = new ObservabilityPG({ client: this.#db, operations, schema: this.schema });
|
|
4619
|
+
const agents = new AgentsPG({ client: this.#db, schema: this.schema });
|
|
4217
4620
|
this.stores = {
|
|
4218
4621
|
operations,
|
|
4219
4622
|
scores,
|
|
4220
4623
|
workflows,
|
|
4221
4624
|
memory,
|
|
4222
|
-
observability
|
|
4625
|
+
observability,
|
|
4626
|
+
agents
|
|
4223
4627
|
};
|
|
4224
4628
|
} catch (e) {
|
|
4225
4629
|
throw new error.MastraError(
|
|
@@ -4271,7 +4675,8 @@ var PostgresStore = class extends storage.MastraStorage {
|
|
|
4271
4675
|
deleteMessages: true,
|
|
4272
4676
|
observabilityInstance: true,
|
|
4273
4677
|
indexManagement: true,
|
|
4274
|
-
listScoresBySpan: true
|
|
4678
|
+
listScoresBySpan: true,
|
|
4679
|
+
agents: true
|
|
4275
4680
|
};
|
|
4276
4681
|
}
|
|
4277
4682
|
async createTable({
|
|
@@ -4390,6 +4795,9 @@ var PostgresStore = class extends storage.MastraStorage {
|
|
|
4390
4795
|
}) {
|
|
4391
4796
|
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
4392
4797
|
}
|
|
4798
|
+
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
4799
|
+
return this.stores.workflows.deleteWorkflowRunById({ runId, workflowName });
|
|
4800
|
+
}
|
|
4393
4801
|
async close() {
|
|
4394
4802
|
this.pgp.end();
|
|
4395
4803
|
}
|