@mastra/pg 1.23.0-alpha.2 → 1.23.0-alpha.4
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/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-tools-vector-query-tool.md +4 -2
- package/dist/index.cjs +25 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +25 -11
- package/dist/index.js.map +1 -1
- package/dist/vector/index.d.ts +10 -0
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/docs/SKILL.md
CHANGED
|
@@ -79,7 +79,7 @@ const queryTool = createVectorQueryTool({
|
|
|
79
79
|
|
|
80
80
|
The tool returns an object with:
|
|
81
81
|
|
|
82
|
-
**relevantContext** (`
|
|
82
|
+
**relevantContext** (`any[]`): Array of metadata objects for the most relevant chunks, in rank order (one entry per result). The chunk text is available at relevantContext\[i].text when it was stored in metadata during ingestion.
|
|
83
83
|
|
|
84
84
|
**sources** (`QueryResult[]`): Array of full retrieval result objects. Each object contains all information needed to reference the original document, chunk, and similarity score.
|
|
85
85
|
|
|
@@ -95,6 +95,8 @@ The tool returns an object with:
|
|
|
95
95
|
}
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
+
`document` is only populated by vector stores whose `query()` returns document content, such as Chroma, Elasticsearch, LanceDB, and MongoDB. For other stores, such as PgVector, it's an empty string. Read the chunk text from `sources[i].metadata.text` (or whichever metadata key you stored it under).
|
|
99
|
+
|
|
98
100
|
## Default tool description
|
|
99
101
|
|
|
100
102
|
The default description focuses on:
|
|
@@ -489,7 +491,7 @@ The tool is created with:
|
|
|
489
491
|
|
|
490
492
|
- **ID**: `VectorQuery {vectorStoreName} {indexName} Tool`
|
|
491
493
|
- **Input Schema**: Requires queryText and filter objects
|
|
492
|
-
- **Output Schema**: Returns relevantContext
|
|
494
|
+
- **Output Schema**: Returns `relevantContext` (array of chunk metadata) and `sources` (array of `QueryResult`)
|
|
493
495
|
|
|
494
496
|
## Related
|
|
495
497
|
|
package/dist/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
21
|
enumerable: true
|
|
22
22
|
}) : target, mod));
|
|
23
23
|
//#endregion
|
|
24
|
+
let crypto$1 = require("crypto");
|
|
24
25
|
let _mastra_core_error = require("@mastra/core/error");
|
|
25
26
|
let _mastra_core_storage = require("@mastra/core/storage");
|
|
26
27
|
_mastra_core_storage = __toESM(_mastra_core_storage, 1);
|
|
@@ -34,7 +35,6 @@ xxhash_wasm = __toESM(xxhash_wasm, 1);
|
|
|
34
35
|
let pg_connection_string = require("pg-connection-string");
|
|
35
36
|
let _mastra_core_vector_filter = require("@mastra/core/vector/filter");
|
|
36
37
|
let _mastra_core_base = require("@mastra/core/base");
|
|
37
|
-
let crypto$1 = require("crypto");
|
|
38
38
|
let _mastra_core_agent = require("@mastra/core/agent");
|
|
39
39
|
let _mastra_core_features = require("@mastra/core/features");
|
|
40
40
|
let _mastra_core_evals = require("@mastra/core/evals");
|
|
@@ -828,6 +828,8 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
828
828
|
AND attnum > 0
|
|
829
829
|
AND NOT attisdropped`, [tableName])).rowCount === 0) return;
|
|
830
830
|
await client.query(`ALTER TABLE ${tableName} ADD COLUMN IF NOT EXISTS namespace VARCHAR(255) NOT NULL DEFAULT '${DEFAULT_NAMESPACE}'`);
|
|
831
|
+
const namespaceIndexName = this.getNamespaceIndexName(parsedIndexName);
|
|
832
|
+
await client.query(`CREATE UNIQUE INDEX IF NOT EXISTS "${namespaceIndexName}" ON ${tableName} (namespace, vector_id)`);
|
|
831
833
|
const legacyConstraints = await client.query(`SELECT c.conname
|
|
832
834
|
FROM pg_constraint c
|
|
833
835
|
WHERE c.conrelid = to_regclass($1)
|
|
@@ -837,8 +839,21 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
837
839
|
const parsedConstraintName = (0, _mastra_core_utils.parseSqlIdentifier)(conname, "constraint name");
|
|
838
840
|
await client.query(`ALTER TABLE ${tableName} DROP CONSTRAINT "${parsedConstraintName}"`);
|
|
839
841
|
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Name of the unique (namespace, vector_id) index for a table.
|
|
845
|
+
*
|
|
846
|
+
* The full `<index>_namespace_vector_id_idx` name is kept whenever it fits so tables that
|
|
847
|
+
* were already migrated keep matching `IF NOT EXISTS`. Longer index names would exceed
|
|
848
|
+
* Postgres' 63-char identifier limit, so those fall back to a truncated prefix plus a hash
|
|
849
|
+
* of the index name to distinguish tables that
|
|
850
|
+
* share a long prefix.
|
|
851
|
+
*/
|
|
852
|
+
getNamespaceIndexName(parsedIndexName) {
|
|
853
|
+
const fullName = `${parsedIndexName}_namespace_vector_id_idx`;
|
|
854
|
+
if (fullName.length <= 63) return fullName;
|
|
855
|
+
const suffix = `_ns_${(0, crypto$1.createHash)("sha256").update(parsedIndexName).digest("hex").slice(0, 32)}_idx`;
|
|
856
|
+
return `${parsedIndexName.slice(0, 63 - suffix.length)}${suffix}`;
|
|
842
857
|
}
|
|
843
858
|
transformFilter(filter) {
|
|
844
859
|
return new PGFilterTranslator().translate(filter);
|
|
@@ -936,12 +951,12 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
936
951
|
client.release();
|
|
937
952
|
}
|
|
938
953
|
}
|
|
954
|
+
const indexInfo = await this.getIndexMetadata({ indexName });
|
|
939
955
|
const client = await this.pool.connect();
|
|
940
956
|
try {
|
|
941
957
|
await this.ensureSearchPath(client);
|
|
942
958
|
await client.query("BEGIN");
|
|
943
959
|
const { sql: filterQuery, values: filterValues } = buildFilterQuery(this.transformFilter(filter), minScore, topK);
|
|
944
|
-
const indexInfo = await this.getIndexMetadata({ indexName });
|
|
945
960
|
const metric = indexInfo.metric ?? "cosine";
|
|
946
961
|
const ops = this.getVectorOps(indexInfo.vectorType, metric);
|
|
947
962
|
const vectorStr = ops.formatVector(queryVector, indexInfo.dimension);
|
|
@@ -1016,6 +1031,7 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1016
1031
|
async upsert({ indexName, vectors, metadata, ids, deleteFilter, namespace = DEFAULT_NAMESPACE }) {
|
|
1017
1032
|
(0, _mastra_core_vector.validateUpsertInput)("PG", vectors, metadata, ids);
|
|
1018
1033
|
const { tableName } = this.getTableName(indexName);
|
|
1034
|
+
const indexInfo = await this.getIndexMetadata({ indexName });
|
|
1019
1035
|
const client = await this.pool.connect();
|
|
1020
1036
|
try {
|
|
1021
1037
|
await this.ensureSearchPath(client);
|
|
@@ -1037,7 +1053,6 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1037
1053
|
}
|
|
1038
1054
|
}
|
|
1039
1055
|
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
1040
|
-
const indexInfo = await this.getIndexMetadata({ indexName });
|
|
1041
1056
|
const qualifiedVectorType = this.getVectorTypeName(indexInfo.vectorType, indexInfo.dimension);
|
|
1042
1057
|
const ops = this.getVectorOps(indexInfo.vectorType, indexInfo.metric ?? "cosine");
|
|
1043
1058
|
if (new Set(vectorIds).size !== vectorIds.length) for (let i = 0; i < vectors.length; i++) {
|
|
@@ -1325,7 +1340,7 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1325
1340
|
let existingIndexInfo = null;
|
|
1326
1341
|
let dimension = 0;
|
|
1327
1342
|
try {
|
|
1328
|
-
existingIndexInfo = await this.
|
|
1343
|
+
existingIndexInfo = await this.describeIndexMetadata({ indexName }, client);
|
|
1329
1344
|
dimension = existingIndexInfo.dimension;
|
|
1330
1345
|
if (isConfigEmpty && existingIndexInfo.metric === metric) if (existingIndexInfo.type === "flat") this.logger?.debug(`No index exists for ${vectorIndexName}, will create default ivfflat index`);
|
|
1331
1346
|
else {
|
|
@@ -1529,8 +1544,8 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1529
1544
|
* {@link describeIndex} it issues no `COUNT(*)`, so its cost does not grow with the
|
|
1530
1545
|
* number of rows in the table.
|
|
1531
1546
|
*/
|
|
1532
|
-
async describeIndexMetadata({ indexName }) {
|
|
1533
|
-
const client = await this.pool.connect();
|
|
1547
|
+
async describeIndexMetadata({ indexName }, existingClient) {
|
|
1548
|
+
const client = existingClient ?? await this.pool.connect();
|
|
1534
1549
|
try {
|
|
1535
1550
|
const { tableName, parsedIndexName } = this.getTableName(indexName);
|
|
1536
1551
|
const tableExists = await client.query(`
|
|
@@ -1590,7 +1605,6 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1590
1605
|
config
|
|
1591
1606
|
};
|
|
1592
1607
|
} catch (e) {
|
|
1593
|
-
await client.query("ROLLBACK");
|
|
1594
1608
|
const mastraError = new _mastra_core_error.MastraError({
|
|
1595
1609
|
id: (0, _mastra_core_storage.createVectorErrorId)("PG", "DESCRIBE_INDEX", "FAILED"),
|
|
1596
1610
|
domain: _mastra_core_error.ErrorDomain.MASTRA_VECTOR,
|
|
@@ -1600,7 +1614,7 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1600
1614
|
this.logger?.trackException(mastraError);
|
|
1601
1615
|
throw mastraError;
|
|
1602
1616
|
} finally {
|
|
1603
|
-
client.release();
|
|
1617
|
+
if (!existingClient) client.release();
|
|
1604
1618
|
}
|
|
1605
1619
|
}
|
|
1606
1620
|
/**
|
|
@@ -1705,10 +1719,10 @@ var PgVector = class extends _mastra_core_vector.MastraVector {
|
|
|
1705
1719
|
category: _mastra_core_error.ErrorCategory.USER,
|
|
1706
1720
|
details: { indexName }
|
|
1707
1721
|
});
|
|
1722
|
+
const indexInfo = await this.getIndexMetadata({ indexName });
|
|
1708
1723
|
client = await this.pool.connect();
|
|
1709
1724
|
await this.ensureSearchPath(client);
|
|
1710
1725
|
const { tableName } = this.getTableName(indexName);
|
|
1711
|
-
const indexInfo = await this.getIndexMetadata({ indexName });
|
|
1712
1726
|
const qualifiedVectorType = this.getVectorTypeName(indexInfo.vectorType, indexInfo.dimension);
|
|
1713
1727
|
const ops = this.getVectorOps(indexInfo.vectorType, indexInfo.metric ?? "cosine");
|
|
1714
1728
|
let updateParts = [];
|