@mastra/pg 0.3.1-alpha.3 → 0.3.1-alpha.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/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +18 -0
- package/dist/_tsup-dts-rollup.d.cts +20 -12
- package/dist/_tsup-dts-rollup.d.ts +20 -12
- package/dist/index.cjs +210 -48
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +210 -49
- package/package.json +4 -4
- package/src/index.ts +1 -0
- package/src/storage/index.test.ts +216 -45
- package/src/storage/index.ts +143 -66
- package/src/vector/index.ts +1 -0
- package/src/vector/prompt.ts +101 -0
package/src/storage/index.ts
CHANGED
|
@@ -8,7 +8,14 @@ import {
|
|
|
8
8
|
TABLE_WORKFLOW_SNAPSHOT,
|
|
9
9
|
TABLE_EVALS,
|
|
10
10
|
} from '@mastra/core/storage';
|
|
11
|
-
import type {
|
|
11
|
+
import type {
|
|
12
|
+
EvalRow,
|
|
13
|
+
StorageColumn,
|
|
14
|
+
StorageGetMessagesArg,
|
|
15
|
+
TABLE_NAMES,
|
|
16
|
+
WorkflowRun,
|
|
17
|
+
WorkflowRuns,
|
|
18
|
+
} from '@mastra/core/storage';
|
|
12
19
|
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
13
20
|
import pgPromise from 'pg-promise';
|
|
14
21
|
import type { ISSLConfig } from 'pg-promise/typescript/pg-subset';
|
|
@@ -561,7 +568,7 @@ export class PostgresStore extends MastraStorage {
|
|
|
561
568
|
}
|
|
562
569
|
}
|
|
563
570
|
|
|
564
|
-
async getMessages<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T> {
|
|
571
|
+
async getMessages<T = unknown>({ threadId, selectBy }: StorageGetMessagesArg): Promise<T[]> {
|
|
565
572
|
try {
|
|
566
573
|
const messages: any[] = [];
|
|
567
574
|
const limit = typeof selectBy?.last === `number` ? selectBy.last : 40;
|
|
@@ -645,7 +652,7 @@ export class PostgresStore extends MastraStorage {
|
|
|
645
652
|
}
|
|
646
653
|
});
|
|
647
654
|
|
|
648
|
-
return messages as T;
|
|
655
|
+
return messages as T[];
|
|
649
656
|
} catch (error) {
|
|
650
657
|
console.error('Error getting messages:', error);
|
|
651
658
|
throw error;
|
|
@@ -748,96 +755,166 @@ export class PostgresStore extends MastraStorage {
|
|
|
748
755
|
}
|
|
749
756
|
}
|
|
750
757
|
|
|
758
|
+
private async hasColumn(table: string, column: string): Promise<boolean> {
|
|
759
|
+
// Use this.schema to scope the check
|
|
760
|
+
const schema = this.schema || 'public';
|
|
761
|
+
const result = await this.db.oneOrNone(
|
|
762
|
+
`SELECT 1 FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2 AND (column_name = $3 OR column_name = $4)`,
|
|
763
|
+
[schema, table, column, column.toLowerCase()],
|
|
764
|
+
);
|
|
765
|
+
return !!result;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
private parseWorkflowRun(row: any): WorkflowRun {
|
|
769
|
+
let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;
|
|
770
|
+
if (typeof parsedSnapshot === 'string') {
|
|
771
|
+
try {
|
|
772
|
+
parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;
|
|
773
|
+
} catch (e) {
|
|
774
|
+
// If parsing fails, return the raw snapshot string
|
|
775
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
return {
|
|
780
|
+
workflowName: row.workflow_name,
|
|
781
|
+
runId: row.run_id,
|
|
782
|
+
snapshot: parsedSnapshot,
|
|
783
|
+
createdAt: row.createdAt,
|
|
784
|
+
updatedAt: row.updatedAt,
|
|
785
|
+
resourceId: row.resourceId,
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
|
|
751
789
|
async getWorkflowRuns({
|
|
752
790
|
workflowName,
|
|
753
791
|
fromDate,
|
|
754
792
|
toDate,
|
|
755
793
|
limit,
|
|
756
794
|
offset,
|
|
795
|
+
resourceId,
|
|
757
796
|
}: {
|
|
758
797
|
workflowName?: string;
|
|
759
798
|
fromDate?: Date;
|
|
760
799
|
toDate?: Date;
|
|
761
800
|
limit?: number;
|
|
762
801
|
offset?: number;
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
let paramIndex = 1;
|
|
776
|
-
|
|
777
|
-
if (workflowName) {
|
|
778
|
-
conditions.push(`workflow_name = $${paramIndex}`);
|
|
779
|
-
values.push(workflowName);
|
|
780
|
-
paramIndex++;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
if (fromDate) {
|
|
784
|
-
conditions.push(`"createdAt" >= $${paramIndex}`);
|
|
785
|
-
values.push(fromDate);
|
|
786
|
-
paramIndex++;
|
|
787
|
-
}
|
|
802
|
+
resourceId?: string;
|
|
803
|
+
} = {}): Promise<WorkflowRuns> {
|
|
804
|
+
try {
|
|
805
|
+
const conditions: string[] = [];
|
|
806
|
+
const values: any[] = [];
|
|
807
|
+
let paramIndex = 1;
|
|
808
|
+
|
|
809
|
+
if (workflowName) {
|
|
810
|
+
conditions.push(`workflow_name = $${paramIndex}`);
|
|
811
|
+
values.push(workflowName);
|
|
812
|
+
paramIndex++;
|
|
813
|
+
}
|
|
788
814
|
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
815
|
+
if (resourceId) {
|
|
816
|
+
const hasResourceId = await this.hasColumn(TABLE_WORKFLOW_SNAPSHOT, 'resourceId');
|
|
817
|
+
if (hasResourceId) {
|
|
818
|
+
conditions.push(`"resourceId" = $${paramIndex}`);
|
|
819
|
+
values.push(resourceId);
|
|
820
|
+
paramIndex++;
|
|
821
|
+
} else {
|
|
822
|
+
console.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
|
|
823
|
+
}
|
|
824
|
+
}
|
|
794
825
|
|
|
795
|
-
|
|
826
|
+
if (fromDate) {
|
|
827
|
+
conditions.push(`"createdAt" >= $${paramIndex}`);
|
|
828
|
+
values.push(fromDate);
|
|
829
|
+
paramIndex++;
|
|
830
|
+
}
|
|
796
831
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
total =
|
|
805
|
-
|
|
832
|
+
if (toDate) {
|
|
833
|
+
conditions.push(`"createdAt" <= $${paramIndex}`);
|
|
834
|
+
values.push(toDate);
|
|
835
|
+
paramIndex++;
|
|
836
|
+
}
|
|
837
|
+
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
838
|
+
|
|
839
|
+
let total = 0;
|
|
840
|
+
// Only get total count when using pagination
|
|
841
|
+
if (limit !== undefined && offset !== undefined) {
|
|
842
|
+
const countResult = await this.db.one(
|
|
843
|
+
`SELECT COUNT(*) as count FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)} ${whereClause}`,
|
|
844
|
+
values,
|
|
845
|
+
);
|
|
846
|
+
total = Number(countResult.count);
|
|
847
|
+
}
|
|
806
848
|
|
|
807
|
-
|
|
808
|
-
|
|
849
|
+
// Get results
|
|
850
|
+
const query = `
|
|
809
851
|
SELECT * FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)}
|
|
810
852
|
${whereClause}
|
|
811
853
|
ORDER BY "createdAt" DESC
|
|
812
854
|
${limit !== undefined && offset !== undefined ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ''}
|
|
813
855
|
`;
|
|
814
856
|
|
|
815
|
-
|
|
857
|
+
const queryValues = limit !== undefined && offset !== undefined ? [...values, limit, offset] : values;
|
|
816
858
|
|
|
817
|
-
|
|
859
|
+
const result = await this.db.manyOrNone(query, queryValues);
|
|
818
860
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
861
|
+
const runs = (result || []).map(row => {
|
|
862
|
+
return this.parseWorkflowRun(row);
|
|
863
|
+
});
|
|
864
|
+
|
|
865
|
+
// Use runs.length as total when not paginating
|
|
866
|
+
return { runs, total: total || runs.length };
|
|
867
|
+
} catch (error) {
|
|
868
|
+
console.error('Error getting workflow runs:', error);
|
|
869
|
+
throw error;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
async getWorkflowRunById({
|
|
874
|
+
runId,
|
|
875
|
+
workflowName,
|
|
876
|
+
}: {
|
|
877
|
+
runId: string;
|
|
878
|
+
workflowName?: string;
|
|
879
|
+
}): Promise<WorkflowRun | null> {
|
|
880
|
+
try {
|
|
881
|
+
const conditions: string[] = [];
|
|
882
|
+
const values: any[] = [];
|
|
883
|
+
let paramIndex = 1;
|
|
884
|
+
|
|
885
|
+
if (runId) {
|
|
886
|
+
conditions.push(`run_id = $${paramIndex}`);
|
|
887
|
+
values.push(runId);
|
|
888
|
+
paramIndex++;
|
|
828
889
|
}
|
|
829
890
|
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
};
|
|
837
|
-
|
|
891
|
+
if (workflowName) {
|
|
892
|
+
conditions.push(`workflow_name = $${paramIndex}`);
|
|
893
|
+
values.push(workflowName);
|
|
894
|
+
paramIndex++;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
898
|
+
|
|
899
|
+
// Get results
|
|
900
|
+
const query = `
|
|
901
|
+
SELECT * FROM ${this.getTableName(TABLE_WORKFLOW_SNAPSHOT)}
|
|
902
|
+
${whereClause}
|
|
903
|
+
`;
|
|
904
|
+
|
|
905
|
+
const queryValues = values;
|
|
838
906
|
|
|
839
|
-
|
|
840
|
-
|
|
907
|
+
const result = await this.db.oneOrNone(query, queryValues);
|
|
908
|
+
|
|
909
|
+
if (!result) {
|
|
910
|
+
return null;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
return this.parseWorkflowRun(result);
|
|
914
|
+
} catch (error) {
|
|
915
|
+
console.error('Error getting workflow run by ID:', error);
|
|
916
|
+
throw error;
|
|
917
|
+
}
|
|
841
918
|
}
|
|
842
919
|
|
|
843
920
|
async close(): Promise<void> {
|
package/src/vector/index.ts
CHANGED
|
@@ -519,6 +519,7 @@ export class PgVector extends MastraVector {
|
|
|
519
519
|
// Wait for the installation process to complete
|
|
520
520
|
await this.installVectorExtensionPromise;
|
|
521
521
|
}
|
|
522
|
+
|
|
522
523
|
async listIndexes(): Promise<string[]> {
|
|
523
524
|
const client = await this.pool.connect();
|
|
524
525
|
try {
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector store specific prompt that details supported operators and examples.
|
|
3
|
+
* This prompt helps users construct valid filters for PG Vector.
|
|
4
|
+
*/
|
|
5
|
+
export const PGVECTOR_PROMPT = `When querying PG Vector, you can ONLY use the operators listed below. Any other operators will be rejected.
|
|
6
|
+
Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
|
|
7
|
+
If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
|
|
8
|
+
|
|
9
|
+
Basic Comparison Operators:
|
|
10
|
+
- $eq: Exact match (default when using field: value)
|
|
11
|
+
Example: { "category": "electronics" }
|
|
12
|
+
- $ne: Not equal
|
|
13
|
+
Example: { "category": { "$ne": "electronics" } }
|
|
14
|
+
- $gt: Greater than
|
|
15
|
+
Example: { "price": { "$gt": 100 } }
|
|
16
|
+
- $gte: Greater than or equal
|
|
17
|
+
Example: { "price": { "$gte": 100 } }
|
|
18
|
+
- $lt: Less than
|
|
19
|
+
Example: { "price": { "$lt": 100 } }
|
|
20
|
+
- $lte: Less than or equal
|
|
21
|
+
Example: { "price": { "$lte": 100 } }
|
|
22
|
+
|
|
23
|
+
Array Operators:
|
|
24
|
+
- $in: Match any value in array
|
|
25
|
+
Example: { "category": { "$in": ["electronics", "books"] } }
|
|
26
|
+
- $nin: Does not match any value in array
|
|
27
|
+
Example: { "category": { "$nin": ["electronics", "books"] } }
|
|
28
|
+
- $all: Match all values in array
|
|
29
|
+
Example: { "tags": { "$all": ["premium", "sale"] } }
|
|
30
|
+
- $elemMatch: Match array elements that meet all specified conditions
|
|
31
|
+
Example: { "items": { "$elemMatch": { "price": { "$gt": 100 } } } }
|
|
32
|
+
- $contains: Check if array contains value
|
|
33
|
+
Example: { "tags": { "$contains": "premium" } }
|
|
34
|
+
|
|
35
|
+
Logical Operators:
|
|
36
|
+
- $and: Logical AND (implicit when using multiple conditions)
|
|
37
|
+
Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
|
|
38
|
+
- $or: Logical OR
|
|
39
|
+
Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
|
|
40
|
+
- $not: Logical NOT
|
|
41
|
+
Example: { "$not": { "category": "electronics" } }
|
|
42
|
+
- $nor: Logical NOR
|
|
43
|
+
Example: { "$nor": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
|
|
44
|
+
|
|
45
|
+
Element Operators:
|
|
46
|
+
- $exists: Check if field exists
|
|
47
|
+
Example: { "rating": { "$exists": true } }
|
|
48
|
+
|
|
49
|
+
Special Operators:
|
|
50
|
+
- $size: Array length check
|
|
51
|
+
Example: { "tags": { "$size": 2 } }
|
|
52
|
+
|
|
53
|
+
Restrictions:
|
|
54
|
+
- Regex patterns are not supported
|
|
55
|
+
- Direct RegExp patterns will throw an error
|
|
56
|
+
- Nested fields are supported using dot notation
|
|
57
|
+
- Multiple conditions on the same field are supported with both implicit and explicit $and
|
|
58
|
+
- Array operations work on array fields only
|
|
59
|
+
- Basic operators handle array values as JSON strings
|
|
60
|
+
- Empty arrays in conditions are handled gracefully
|
|
61
|
+
- Only logical operators ($and, $or, $not, $nor) can be used at the top level
|
|
62
|
+
- All other operators must be used within a field condition
|
|
63
|
+
Valid: { "field": { "$gt": 100 } }
|
|
64
|
+
Valid: { "$and": [...] }
|
|
65
|
+
Invalid: { "$gt": 100 }
|
|
66
|
+
Invalid: { "$contains": "value" }
|
|
67
|
+
- Logical operators must contain field conditions, not direct operators
|
|
68
|
+
Valid: { "$and": [{ "field": { "$gt": 100 } }] }
|
|
69
|
+
Invalid: { "$and": [{ "$gt": 100 }] }
|
|
70
|
+
- $not operator:
|
|
71
|
+
- Must be an object
|
|
72
|
+
- Cannot be empty
|
|
73
|
+
- Can be used at field level or top level
|
|
74
|
+
- Valid: { "$not": { "field": "value" } }
|
|
75
|
+
- Valid: { "field": { "$not": { "$eq": "value" } } }
|
|
76
|
+
- Other logical operators ($and, $or, $nor):
|
|
77
|
+
- Can only be used at top level or nested within other logical operators
|
|
78
|
+
- Can not be used on a field level, or be nested inside a field
|
|
79
|
+
- Can not be used inside an operator
|
|
80
|
+
- Valid: { "$and": [{ "field": { "$gt": 100 } }] }
|
|
81
|
+
- Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
|
|
82
|
+
- Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
|
|
83
|
+
- Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
|
|
84
|
+
- Invalid: { "field": { "$gt": { "$and": [{...}] } } }
|
|
85
|
+
- $elemMatch requires an object with conditions
|
|
86
|
+
Valid: { "array": { "$elemMatch": { "field": "value" } } }
|
|
87
|
+
Invalid: { "array": { "$elemMatch": "value" } }
|
|
88
|
+
|
|
89
|
+
Example Complex Query:
|
|
90
|
+
{
|
|
91
|
+
"$and": [
|
|
92
|
+
{ "category": { "$in": ["electronics", "computers"] } },
|
|
93
|
+
{ "price": { "$gte": 100, "$lte": 1000 } },
|
|
94
|
+
{ "tags": { "$all": ["premium"] } },
|
|
95
|
+
{ "rating": { "$exists": true, "$gt": 4 } },
|
|
96
|
+
{ "$or": [
|
|
97
|
+
{ "stock": { "$gt": 0 } },
|
|
98
|
+
{ "preorder": true }
|
|
99
|
+
]}
|
|
100
|
+
]
|
|
101
|
+
}`;
|