@mastra/pg 1.25.0-alpha.1 → 1.25.0

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.
@@ -3,7 +3,7 @@ name: mastra-pg
3
3
  description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/pg"
6
- version: "1.25.0-alpha.1"
6
+ version: "1.25.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.25.0-alpha.1",
2
+ "version": "1.25.0",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -13207,6 +13207,27 @@ var NotificationsPG = class NotificationsPG extends _mastra_core_storage.Notific
13207
13207
  if (!updated) throw new Error(`Notification ${input.id} was not found for thread ${input.threadId}`);
13208
13208
  return updated;
13209
13209
  }
13210
+ async updateNotificationsStatus(input) {
13211
+ const ids = Array.from(new Set(input.ids));
13212
+ if (ids.length === 0) return [];
13213
+ const now = /* @__PURE__ */ new Date();
13214
+ const assignments = {
13215
+ status: input.status,
13216
+ ...statusTimestamp(input.status, now),
13217
+ updatedAt: now
13218
+ };
13219
+ const columns = Object.keys(assignments);
13220
+ const setClause = columns.map((column, index) => `"${(0, _mastra_core_utils.parseSqlIdentifier)(column, "column name")}" = $${index + 1}`).join(", ");
13221
+ const tableName = getTableName$5({
13222
+ indexName: _mastra_core_storage.TABLE_NOTIFICATIONS,
13223
+ schemaName: getSchemaName$5(this.#schema)
13224
+ });
13225
+ return (await this.#db.client.manyOrNone(`UPDATE ${tableName} SET ${setClause} WHERE "threadId" = $${columns.length + 1} AND "id" = ANY($${columns.length + 2}::text[]) RETURNING *`, [
13226
+ ...Object.values(assignments),
13227
+ input.threadId,
13228
+ ids
13229
+ ])).map(rowToNotification);
13230
+ }
13210
13231
  async findCoalescable(input) {
13211
13232
  if (!input.dedupeKey && !input.coalesceKey) return void 0;
13212
13233
  const tableName = getTableName$5({
@@ -17361,6 +17382,13 @@ function collectRelationCollections(predicate, collections = /* @__PURE__ */ new
17361
17382
  else if (predicate.type === "not") collectRelationCollections(predicate.arg, collections);
17362
17383
  return collections;
17363
17384
  }
17385
+ function collectThreadRelationCollections(predicate, collections) {
17386
+ if (!predicate) return collections;
17387
+ if (predicate.type === "relation") collectRelationCollections(predicate.predicate, collections);
17388
+ else if (predicate.type === "boolean") for (const arg of predicate.args) collectThreadRelationCollections(arg, collections);
17389
+ else collectThreadRelationCollections(predicate.arg, collections);
17390
+ return collections;
17391
+ }
17364
17392
  function compilePredicate(predicate, parameterOffset) {
17365
17393
  if (predicate.type === "relation") {
17366
17394
  const compiled = predicate.collection === "feedback" ? compileFeedbackScalarPredicate(predicate.predicate, parameterOffset) : compileScalarPredicate(predicate.predicate, predicate.collection === "spans" ? SPAN_FIELDS : SCORE_FIELDS, parameterOffset);
@@ -17395,12 +17423,41 @@ function compilePredicate(predicate, parameterOffset) {
17395
17423
  }
17396
17424
  return compileScalarPredicate(predicate, TRACE_FIELDS, parameterOffset, true);
17397
17425
  }
17398
- function compilePostgresTraceQuery(schema, plan) {
17426
+ function compileThreadPredicate(predicate, parameterOffset) {
17427
+ if (predicate.type === "relation") {
17428
+ const compiled = compilePredicate(predicate.predicate, parameterOffset);
17429
+ const existence = `EXISTS (
17430
+ SELECT 1 FROM eligible_roots r
17431
+ WHERE r."threadId" = t."threadId"
17432
+ AND (${compiled.sql})
17433
+ )`;
17434
+ return {
17435
+ sql: predicate.quantifier === "some" ? existence : `NOT ${existence}`,
17436
+ values: compiled.values
17437
+ };
17438
+ }
17439
+ if (predicate.type === "boolean") {
17440
+ const values = [];
17441
+ return {
17442
+ sql: predicate.args.map((arg) => {
17443
+ const compiled = compileThreadPredicate(arg, parameterOffset + values.length);
17444
+ values.push(...compiled.values);
17445
+ return `(${compiled.sql})`;
17446
+ }).join(predicate.operator === "and" ? " AND " : " OR "),
17447
+ values
17448
+ };
17449
+ }
17450
+ const compiled = compileThreadPredicate(predicate.arg, parameterOffset);
17451
+ return {
17452
+ sql: `NOT (${compiled.sql})`,
17453
+ values: compiled.values
17454
+ };
17455
+ }
17456
+ function compilePostgresTraceScope(schema, selection, relationCollections) {
17399
17457
  const spanTable = qualifiedTable(schema, TABLE_SPAN_EVENTS);
17400
17458
  const scoreTable = qualifiedTable(schema, TABLE_SCORE_EVENTS);
17401
17459
  const feedbackTable = qualifiedTable(schema, TABLE_FEEDBACK_EVENTS);
17402
- const values = [plan.timeRange.from, plan.timeRange.to];
17403
- const relationCollections = collectRelationCollections(plan.where);
17460
+ const values = [selection.timeRange.from, selection.timeRange.to];
17404
17461
  const ctes = [`root_scope AS MATERIALIZED (
17405
17462
  SELECT *
17406
17463
  FROM ${spanTable} r
@@ -17475,6 +17532,13 @@ function compilePostgresTraceQuery(schema, plan) {
17475
17532
  AND s."traceId" IN (SELECT "traceId" FROM root_scope)
17476
17533
  AND ${latestFeedbackPredicate(feedbackTable)}
17477
17534
  )`);
17535
+ return {
17536
+ ctes,
17537
+ values
17538
+ };
17539
+ }
17540
+ function compilePostgresTraceQuery(schema, plan) {
17541
+ const { ctes, values } = compilePostgresTraceScope(schema, plan, collectRelationCollections(plan.where));
17478
17542
  let predicateSql = "TRUE";
17479
17543
  if (plan.where) {
17480
17544
  const predicate = compilePredicate(plan.where, values.length + 1);
@@ -17522,6 +17586,51 @@ LIMIT $${values.length}`,
17522
17586
  values
17523
17587
  };
17524
17588
  }
17589
+ function compilePostgresThreadQuery(schema, plan) {
17590
+ const relationCollections = collectRelationCollections(plan.traces.where);
17591
+ collectThreadRelationCollections(plan.where, relationCollections);
17592
+ const { ctes, values } = compilePostgresTraceScope(schema, plan.traces, relationCollections);
17593
+ let eligibilitySql = "TRUE";
17594
+ if (plan.traces.where) {
17595
+ const eligibility = compilePredicate(plan.traces.where, values.length + 1);
17596
+ eligibilitySql = eligibility.sql;
17597
+ values.push(...eligibility.values);
17598
+ }
17599
+ ctes.push(`eligible_roots AS MATERIALIZED (
17600
+ SELECT *
17601
+ FROM root_scope r
17602
+ WHERE ${eligibilitySql}
17603
+ )`);
17604
+ ctes.push(`thread_ids AS (
17605
+ SELECT "threadId" COLLATE "C" AS "threadId"
17606
+ FROM eligible_roots
17607
+ WHERE "threadId" IS NOT NULL
17608
+ GROUP BY "threadId" COLLATE "C"
17609
+ )`);
17610
+ let threadPredicateSql = "TRUE";
17611
+ if (plan.where) {
17612
+ const predicate = compileThreadPredicate(plan.where, values.length + 1);
17613
+ threadPredicateSql = predicate.sql;
17614
+ values.push(...predicate.values);
17615
+ }
17616
+ ctes.push(`qualified_threads AS (
17617
+ SELECT t."threadId"
17618
+ FROM thread_ids t
17619
+ WHERE ${threadPredicateSql}
17620
+ )`);
17621
+ const pageCondition = plan.cursor ? `WHERE "threadId" > $${values.length + 1}` : "";
17622
+ if (plan.cursor) values.push(plan.cursor.threadId);
17623
+ values.push(plan.limit + 1);
17624
+ return {
17625
+ text: `WITH ${ctes.join(",\n")}
17626
+ SELECT "threadId"
17627
+ FROM qualified_threads
17628
+ ${pageCondition}
17629
+ ORDER BY "threadId" ASC
17630
+ LIMIT $${values.length}`,
17631
+ values
17632
+ };
17633
+ }
17525
17634
  function asIsoTimestamp$1(value) {
17526
17635
  if (value === null || value === void 0) throw new Error("Trace query returned a null timestamp");
17527
17636
  return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
@@ -17580,6 +17689,19 @@ async function queryTraces(client, schema, plan, timeoutMs) {
17580
17689
  }) : null }
17581
17690
  });
17582
17691
  }
17692
+ async function queryThreads(client, schema, plan, timeoutMs) {
17693
+ const query = compilePostgresThreadQuery(schema, plan);
17694
+ const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
17695
+ const threads = rows.slice(0, plan.limit).map((row) => ({ threadId: String(row.threadId) }));
17696
+ const last = threads.at(-1);
17697
+ return _mastra_core_storage.queryThreadsResultSchema.parse({
17698
+ threads,
17699
+ page: { next: rows.length > plan.limit && last ? _mastra_core_storage.encodeTraceQueryCursor(plan, {
17700
+ result: "threads",
17701
+ threadId: last.threadId
17702
+ }) : null }
17703
+ });
17704
+ }
17583
17705
  //#endregion
17584
17706
  //#region src/storage/domains/observability/v-next/traces.ts
17585
17707
  /**
@@ -18475,13 +18597,15 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18475
18597
  if (!deltaPollingFeatureEnabled()) return [
18476
18598
  "metrics",
18477
18599
  "logs",
18478
- "trace-query"
18600
+ "trace-query",
18601
+ "thread-query"
18479
18602
  ];
18480
18603
  return [
18481
18604
  "metrics",
18482
18605
  "logs",
18483
18606
  "delta-polling",
18484
- "trace-query"
18607
+ "trace-query",
18608
+ "thread-query"
18485
18609
  ];
18486
18610
  }
18487
18611
  async #run(op, fn, details) {
@@ -18527,6 +18651,9 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18527
18651
  async queryTraces(plan) {
18528
18652
  return this.#run("QUERY_TRACES", () => queryTraces(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18529
18653
  }
18654
+ async queryThreads(plan) {
18655
+ return this.#run("QUERY_THREADS", () => queryThreads(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18656
+ }
18530
18657
  async listBranches(args) {
18531
18658
  return this.#run("LIST_BRANCHES", () => listBranches(this.#readClient, this.#schema, args));
18532
18659
  }