@mastra/pg 0.2.7-alpha.2 → 0.2.7-alpha.3

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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/pg@0.2.7-alpha.2 build /home/runner/work/mastra/mastra/stores/pg
2
+ > @mastra/pg@0.2.7-alpha.3 build /home/runner/work/mastra/mastra/stores/pg
3
3
  > tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
4
4
 
5
5
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.4.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 10747ms
9
+ TSC ⚡️ Build success in 10020ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.2
13
13
  Writing package typings: /home/runner/work/mastra/mastra/stores/pg/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.2
15
15
  Writing package typings: /home/runner/work/mastra/mastra/stores/pg/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 11651ms
16
+ DTS ⚡️ Build success in 11079ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- CJS dist/index.cjs 37.38 KB
21
- CJS ⚡️ Build success in 1188ms
22
- ESM dist/index.js 36.97 KB
23
- ESM ⚡️ Build success in 1188ms
20
+ ESM dist/index.js 40.32 KB
21
+ ESM ⚡️ Build success in 1253ms
22
+ CJS dist/index.cjs 40.67 KB
23
+ CJS ⚡️ Build success in 1258ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 0.2.7-alpha.3
4
+
5
+ ### Patch Changes
6
+
7
+ - 88fa727: Added getWorkflowRuns for libsql, pg, clickhouse and upstash as well as added route getWorkflowRunsHandler
8
+ - 1849b61: adds optional ssl property to PostgresConfig
9
+ - 4d67826: Fix eval writes, remove id column
10
+ - 58a4146: Support missing methods in pg and upstash
11
+ - Updated dependencies [5ae0180]
12
+ - Updated dependencies [9bfa12b]
13
+ - Updated dependencies [515ebfb]
14
+ - Updated dependencies [88fa727]
15
+ - Updated dependencies [f37f535]
16
+ - Updated dependencies [4d67826]
17
+ - Updated dependencies [6330967]
18
+ - Updated dependencies [8393832]
19
+ - Updated dependencies [6330967]
20
+ - @mastra/core@0.8.0-alpha.3
21
+
3
22
  ## 0.2.7-alpha.2
4
23
 
5
24
  ### Patch Changes
@@ -6,6 +6,7 @@ import type { CreateIndexParams } from '@mastra/core/vector';
6
6
  import type { ElementOperator } from '@mastra/core/vector/filter';
7
7
  import type { EvalRow } from '@mastra/core/storage';
8
8
  import type { IndexStats } from '@mastra/core/vector';
9
+ import type { ISSLConfig } from 'pg-promise/typescript/pg-subset';
9
10
  import type { LogicalOperator } from '@mastra/core/vector/filter';
10
11
  import { MastraStorage } from '@mastra/core/storage';
11
12
  import { MastraVector } from '@mastra/core/vector';
@@ -232,6 +233,7 @@ declare type PostgresConfig = {
232
233
  database: string;
233
234
  user: string;
234
235
  password: string;
236
+ ssl?: boolean | ISSLConfig;
235
237
  } | {
236
238
  connectionString: string;
237
239
  };
@@ -242,17 +244,19 @@ declare class PostgresStore extends MastraStorage {
242
244
  private db;
243
245
  private pgp;
244
246
  constructor(config: PostgresConfig);
245
- getEvalsByAgentName(_agentName: string, _type?: 'test' | 'live'): Promise<EvalRow[]>;
247
+ getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
248
+ private transformEvalRow;
246
249
  batchInsert({ tableName, records }: {
247
250
  tableName: TABLE_NAMES;
248
251
  records: Record<string, any>[];
249
252
  }): Promise<void>;
250
- getTraces({ name, scope, page, perPage, attributes, }: {
253
+ getTraces({ name, scope, page, perPage, attributes, filters, }: {
251
254
  name?: string;
252
255
  scope?: string;
253
256
  page: number;
254
257
  perPage: number;
255
258
  attributes?: Record<string, string>;
259
+ filters?: Record<string, any>;
256
260
  }): Promise<any[]>;
257
261
  createTable({ tableName, schema, }: {
258
262
  tableName: TABLE_NAMES;
@@ -299,6 +303,22 @@ declare class PostgresStore extends MastraStorage {
299
303
  workflowName: string;
300
304
  runId: string;
301
305
  }): Promise<WorkflowRunState | null>;
306
+ getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, }?: {
307
+ workflowName?: string;
308
+ fromDate?: Date;
309
+ toDate?: Date;
310
+ limit?: number;
311
+ offset?: number;
312
+ }): Promise<{
313
+ runs: Array<{
314
+ workflowName: string;
315
+ runId: string;
316
+ snapshot: WorkflowRunState | string;
317
+ createdAt: Date;
318
+ updatedAt: Date;
319
+ }>;
320
+ total: number;
321
+ }>;
302
322
  close(): Promise<void>;
303
323
  }
304
324
  export { PostgresStore }
@@ -6,6 +6,7 @@ import type { CreateIndexParams } from '@mastra/core/vector';
6
6
  import type { ElementOperator } from '@mastra/core/vector/filter';
7
7
  import type { EvalRow } from '@mastra/core/storage';
8
8
  import type { IndexStats } from '@mastra/core/vector';
9
+ import type { ISSLConfig } from 'pg-promise/typescript/pg-subset';
9
10
  import type { LogicalOperator } from '@mastra/core/vector/filter';
10
11
  import { MastraStorage } from '@mastra/core/storage';
11
12
  import { MastraVector } from '@mastra/core/vector';
@@ -232,6 +233,7 @@ declare type PostgresConfig = {
232
233
  database: string;
233
234
  user: string;
234
235
  password: string;
236
+ ssl?: boolean | ISSLConfig;
235
237
  } | {
236
238
  connectionString: string;
237
239
  };
@@ -242,17 +244,19 @@ declare class PostgresStore extends MastraStorage {
242
244
  private db;
243
245
  private pgp;
244
246
  constructor(config: PostgresConfig);
245
- getEvalsByAgentName(_agentName: string, _type?: 'test' | 'live'): Promise<EvalRow[]>;
247
+ getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]>;
248
+ private transformEvalRow;
246
249
  batchInsert({ tableName, records }: {
247
250
  tableName: TABLE_NAMES;
248
251
  records: Record<string, any>[];
249
252
  }): Promise<void>;
250
- getTraces({ name, scope, page, perPage, attributes, }: {
253
+ getTraces({ name, scope, page, perPage, attributes, filters, }: {
251
254
  name?: string;
252
255
  scope?: string;
253
256
  page: number;
254
257
  perPage: number;
255
258
  attributes?: Record<string, string>;
259
+ filters?: Record<string, any>;
256
260
  }): Promise<any[]>;
257
261
  createTable({ tableName, schema, }: {
258
262
  tableName: TABLE_NAMES;
@@ -299,6 +303,22 @@ declare class PostgresStore extends MastraStorage {
299
303
  workflowName: string;
300
304
  runId: string;
301
305
  }): Promise<WorkflowRunState | null>;
306
+ getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, }?: {
307
+ workflowName?: string;
308
+ fromDate?: Date;
309
+ toDate?: Date;
310
+ limit?: number;
311
+ offset?: number;
312
+ }): Promise<{
313
+ runs: Array<{
314
+ workflowName: string;
315
+ runId: string;
316
+ snapshot: WorkflowRunState | string;
317
+ createdAt: Date;
318
+ updatedAt: Date;
319
+ }>;
320
+ total: number;
321
+ }>;
302
322
  close(): Promise<void>;
303
323
  }
304
324
  export { PostgresStore }
package/dist/index.cjs CHANGED
@@ -708,12 +708,46 @@ var PostgresStore = class extends storage.MastraStorage {
708
708
  port: config.port,
709
709
  database: config.database,
710
710
  user: config.user,
711
- password: config.password
711
+ password: config.password,
712
+ ssl: config.ssl
712
713
  }
713
714
  );
714
715
  }
715
- getEvalsByAgentName(_agentName, _type) {
716
- throw new Error("Method not implemented.");
716
+ getEvalsByAgentName(agentName, type) {
717
+ try {
718
+ const baseQuery = `SELECT * FROM ${storage.TABLE_EVALS} WHERE agent_name = $1`;
719
+ const typeCondition = type === "test" ? " AND test_info IS NOT NULL AND test_info->>'testPath' IS NOT NULL" : type === "live" ? " AND (test_info IS NULL OR test_info->>'testPath' IS NULL)" : "";
720
+ const query = `${baseQuery}${typeCondition} ORDER BY created_at DESC`;
721
+ return this.db.manyOrNone(query, [agentName]).then((rows) => rows?.map((row) => this.transformEvalRow(row)) ?? []);
722
+ } catch (error) {
723
+ if (error instanceof Error && error.message.includes("relation") && error.message.includes("does not exist")) {
724
+ return Promise.resolve([]);
725
+ }
726
+ console.error("Failed to get evals for the specified agent: " + error?.message);
727
+ throw error;
728
+ }
729
+ }
730
+ transformEvalRow(row) {
731
+ let testInfoValue = null;
732
+ if (row.test_info) {
733
+ try {
734
+ testInfoValue = typeof row.test_info === "string" ? JSON.parse(row.test_info) : row.test_info;
735
+ } catch (e) {
736
+ console.warn("Failed to parse test_info:", e);
737
+ }
738
+ }
739
+ return {
740
+ agentName: row.agent_name,
741
+ input: row.input,
742
+ output: row.output,
743
+ result: row.result,
744
+ metricName: row.metric_name,
745
+ instructions: row.instructions,
746
+ testInfo: testInfoValue,
747
+ globalRunId: row.global_run_id,
748
+ runId: row.run_id,
749
+ createdAt: row.created_at
750
+ };
717
751
  }
718
752
  async batchInsert({ tableName, records }) {
719
753
  try {
@@ -733,7 +767,8 @@ var PostgresStore = class extends storage.MastraStorage {
733
767
  scope,
734
768
  page,
735
769
  perPage,
736
- attributes
770
+ attributes,
771
+ filters
737
772
  }) {
738
773
  let idx = 1;
739
774
  const limit = perPage;
@@ -751,6 +786,11 @@ var PostgresStore = class extends storage.MastraStorage {
751
786
  conditions.push(`attributes->>'${key}' = $${idx++}`);
752
787
  });
753
788
  }
789
+ if (filters) {
790
+ Object.entries(filters).forEach(([key, value]) => {
791
+ conditions.push(`${key} = $${idx++}`);
792
+ });
793
+ }
754
794
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
755
795
  if (name) {
756
796
  args.push(name);
@@ -763,15 +803,17 @@ var PostgresStore = class extends storage.MastraStorage {
763
803
  args.push(value);
764
804
  }
765
805
  }
806
+ if (filters) {
807
+ for (const [key, value] of Object.entries(filters)) {
808
+ args.push(value);
809
+ }
810
+ }
766
811
  console.log(
767
812
  "QUERY",
768
- `SELECT * FROM ${storage.MastraStorage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
769
- args
770
- );
771
- const result = await this.db.manyOrNone(
772
- `SELECT * FROM ${storage.MastraStorage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
813
+ `SELECT * FROM ${storage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`,
773
814
  args
774
815
  );
816
+ const result = await this.db.manyOrNone(`SELECT * FROM ${storage.TABLE_TRACES} ${whereClause} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`, args);
775
817
  if (!result) {
776
818
  return [];
777
819
  }
@@ -807,7 +849,7 @@ var PostgresStore = class extends storage.MastraStorage {
807
849
  CREATE TABLE IF NOT EXISTS ${tableName} (
808
850
  ${columns}
809
851
  );
810
- ${tableName === storage.MastraStorage.TABLE_WORKFLOW_SNAPSHOT ? `
852
+ ${tableName === storage.TABLE_WORKFLOW_SNAPSHOT ? `
811
853
  DO $$ BEGIN
812
854
  IF NOT EXISTS (
813
855
  SELECT 1 FROM pg_constraint WHERE conname = 'mastra_workflow_snapshot_workflow_name_run_id_key'
@@ -856,7 +898,7 @@ var PostgresStore = class extends storage.MastraStorage {
856
898
  if (!result) {
857
899
  return null;
858
900
  }
859
- if (tableName === storage.MastraStorage.TABLE_WORKFLOW_SNAPSHOT) {
901
+ if (tableName === storage.TABLE_WORKFLOW_SNAPSHOT) {
860
902
  const snapshot = result;
861
903
  if (typeof snapshot.snapshot === "string") {
862
904
  snapshot.snapshot = JSON.parse(snapshot.snapshot);
@@ -879,7 +921,7 @@ var PostgresStore = class extends storage.MastraStorage {
879
921
  metadata,
880
922
  "createdAt",
881
923
  "updatedAt"
882
- FROM "${storage.MastraStorage.TABLE_THREADS}"
924
+ FROM "${storage.TABLE_THREADS}"
883
925
  WHERE id = $1`,
884
926
  [threadId]
885
927
  );
@@ -907,7 +949,7 @@ var PostgresStore = class extends storage.MastraStorage {
907
949
  metadata,
908
950
  "createdAt",
909
951
  "updatedAt"
910
- FROM "${storage.MastraStorage.TABLE_THREADS}"
952
+ FROM "${storage.TABLE_THREADS}"
911
953
  WHERE "resourceId" = $1`,
912
954
  [resourceId]
913
955
  );
@@ -925,7 +967,7 @@ var PostgresStore = class extends storage.MastraStorage {
925
967
  async saveThread({ thread }) {
926
968
  try {
927
969
  await this.db.none(
928
- `INSERT INTO "${storage.MastraStorage.TABLE_THREADS}" (
970
+ `INSERT INTO "${storage.TABLE_THREADS}" (
929
971
  id,
930
972
  "resourceId",
931
973
  title,
@@ -969,7 +1011,7 @@ var PostgresStore = class extends storage.MastraStorage {
969
1011
  ...metadata
970
1012
  };
971
1013
  const thread = await this.db.one(
972
- `UPDATE "${storage.MastraStorage.TABLE_THREADS}"
1014
+ `UPDATE "${storage.TABLE_THREADS}"
973
1015
  SET title = $1,
974
1016
  metadata = $2,
975
1017
  "updatedAt" = $3
@@ -991,8 +1033,8 @@ var PostgresStore = class extends storage.MastraStorage {
991
1033
  async deleteThread({ threadId }) {
992
1034
  try {
993
1035
  await this.db.tx(async (t) => {
994
- await t.none(`DELETE FROM "${storage.MastraStorage.TABLE_MESSAGES}" WHERE thread_id = $1`, [threadId]);
995
- await t.none(`DELETE FROM "${storage.MastraStorage.TABLE_THREADS}" WHERE id = $1`, [threadId]);
1036
+ await t.none(`DELETE FROM "${storage.TABLE_MESSAGES}" WHERE thread_id = $1`, [threadId]);
1037
+ await t.none(`DELETE FROM "${storage.TABLE_THREADS}" WHERE id = $1`, [threadId]);
996
1038
  });
997
1039
  } catch (error) {
998
1040
  console.error("Error deleting thread:", error);
@@ -1011,7 +1053,7 @@ var PostgresStore = class extends storage.MastraStorage {
1011
1053
  SELECT
1012
1054
  *,
1013
1055
  ROW_NUMBER() OVER (ORDER BY "createdAt" DESC) as row_num
1014
- FROM "${storage.MastraStorage.TABLE_MESSAGES}"
1056
+ FROM "${storage.TABLE_MESSAGES}"
1015
1057
  WHERE thread_id = $1
1016
1058
  )
1017
1059
  SELECT
@@ -1054,7 +1096,7 @@ var PostgresStore = class extends storage.MastraStorage {
1054
1096
  type,
1055
1097
  "createdAt",
1056
1098
  thread_id AS "threadId"
1057
- FROM "${storage.MastraStorage.TABLE_MESSAGES}"
1099
+ FROM "${storage.TABLE_MESSAGES}"
1058
1100
  WHERE thread_id = $1
1059
1101
  AND id != ALL($2)
1060
1102
  ORDER BY "createdAt" DESC
@@ -1092,7 +1134,7 @@ var PostgresStore = class extends storage.MastraStorage {
1092
1134
  await this.db.tx(async (t) => {
1093
1135
  for (const message of messages) {
1094
1136
  await t.none(
1095
- `INSERT INTO "${storage.MastraStorage.TABLE_MESSAGES}" (id, thread_id, content, "createdAt", role, type)
1137
+ `INSERT INTO "${storage.TABLE_MESSAGES}" (id, thread_id, content, "createdAt", role, type)
1096
1138
  VALUES ($1, $2, $3, $4, $5, $6)`,
1097
1139
  [
1098
1140
  message.id,
@@ -1119,7 +1161,7 @@ var PostgresStore = class extends storage.MastraStorage {
1119
1161
  try {
1120
1162
  const now = (/* @__PURE__ */ new Date()).toISOString();
1121
1163
  await this.db.none(
1122
- `INSERT INTO "${storage.MastraStorage.TABLE_WORKFLOW_SNAPSHOT}" (
1164
+ `INSERT INTO "${storage.TABLE_WORKFLOW_SNAPSHOT}" (
1123
1165
  workflow_name,
1124
1166
  run_id,
1125
1167
  snapshot,
@@ -1142,7 +1184,7 @@ var PostgresStore = class extends storage.MastraStorage {
1142
1184
  }) {
1143
1185
  try {
1144
1186
  const result = await this.load({
1145
- tableName: storage.MastraStorage.TABLE_WORKFLOW_SNAPSHOT,
1187
+ tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
1146
1188
  keys: {
1147
1189
  workflow_name: workflowName,
1148
1190
  run_id: runId
@@ -1157,6 +1199,67 @@ var PostgresStore = class extends storage.MastraStorage {
1157
1199
  throw error;
1158
1200
  }
1159
1201
  }
1202
+ async getWorkflowRuns({
1203
+ workflowName,
1204
+ fromDate,
1205
+ toDate,
1206
+ limit,
1207
+ offset
1208
+ } = {}) {
1209
+ const conditions = [];
1210
+ const values = [];
1211
+ let paramIndex = 1;
1212
+ if (workflowName) {
1213
+ conditions.push(`workflow_name = $${paramIndex}`);
1214
+ values.push(workflowName);
1215
+ paramIndex++;
1216
+ }
1217
+ if (fromDate) {
1218
+ conditions.push(`"createdAt" >= $${paramIndex}`);
1219
+ values.push(fromDate);
1220
+ paramIndex++;
1221
+ }
1222
+ if (toDate) {
1223
+ conditions.push(`"createdAt" <= $${paramIndex}`);
1224
+ values.push(toDate);
1225
+ paramIndex++;
1226
+ }
1227
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1228
+ let total = 0;
1229
+ if (limit !== void 0 && offset !== void 0) {
1230
+ const countResult = await this.db.one(
1231
+ `SELECT COUNT(*) as count FROM ${storage.TABLE_WORKFLOW_SNAPSHOT} ${whereClause}`,
1232
+ values
1233
+ );
1234
+ total = Number(countResult.count);
1235
+ }
1236
+ const query = `
1237
+ SELECT * FROM ${storage.TABLE_WORKFLOW_SNAPSHOT}
1238
+ ${whereClause}
1239
+ ORDER BY "createdAt" DESC
1240
+ ${limit !== void 0 && offset !== void 0 ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ""}
1241
+ `;
1242
+ const queryValues = limit !== void 0 && offset !== void 0 ? [...values, limit, offset] : values;
1243
+ const result = await this.db.manyOrNone(query, queryValues);
1244
+ const runs = (result || []).map((row) => {
1245
+ let parsedSnapshot = row.snapshot;
1246
+ if (typeof parsedSnapshot === "string") {
1247
+ try {
1248
+ parsedSnapshot = JSON.parse(row.snapshot);
1249
+ } catch (e) {
1250
+ console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
1251
+ }
1252
+ }
1253
+ return {
1254
+ workflowName: row.workflow_name,
1255
+ runId: row.run_id,
1256
+ snapshot: parsedSnapshot,
1257
+ createdAt: row.createdAt,
1258
+ updatedAt: row.updatedAt
1259
+ };
1260
+ });
1261
+ return { runs, total: total || runs.length };
1262
+ }
1160
1263
  async close() {
1161
1264
  this.pgp.end();
1162
1265
  }