@mastra/pg 1.25.0-alpha.1 → 1.25.0-alpha.2

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/index.js CHANGED
@@ -13183,6 +13183,27 @@ var NotificationsPG = class NotificationsPG extends NotificationsStorage {
13183
13183
  if (!updated) throw new Error(`Notification ${input.id} was not found for thread ${input.threadId}`);
13184
13184
  return updated;
13185
13185
  }
13186
+ async updateNotificationsStatus(input) {
13187
+ const ids = Array.from(new Set(input.ids));
13188
+ if (ids.length === 0) return [];
13189
+ const now = /* @__PURE__ */ new Date();
13190
+ const assignments = {
13191
+ status: input.status,
13192
+ ...statusTimestamp(input.status, now),
13193
+ updatedAt: now
13194
+ };
13195
+ const columns = Object.keys(assignments);
13196
+ const setClause = columns.map((column, index) => `"${parseSqlIdentifier(column, "column name")}" = $${index + 1}`).join(", ");
13197
+ const tableName = getTableName$5({
13198
+ indexName: TABLE_NOTIFICATIONS,
13199
+ schemaName: getSchemaName$5(this.#schema)
13200
+ });
13201
+ return (await this.#db.client.manyOrNone(`UPDATE ${tableName} SET ${setClause} WHERE "threadId" = $${columns.length + 1} AND "id" = ANY($${columns.length + 2}::text[]) RETURNING *`, [
13202
+ ...Object.values(assignments),
13203
+ input.threadId,
13204
+ ids
13205
+ ])).map(rowToNotification);
13206
+ }
13186
13207
  async findCoalescable(input) {
13187
13208
  if (!input.dedupeKey && !input.coalesceKey) return void 0;
13188
13209
  const tableName = getTableName$5({
@@ -17337,6 +17358,13 @@ function collectRelationCollections(predicate, collections = /* @__PURE__ */ new
17337
17358
  else if (predicate.type === "not") collectRelationCollections(predicate.arg, collections);
17338
17359
  return collections;
17339
17360
  }
17361
+ function collectThreadRelationCollections(predicate, collections) {
17362
+ if (!predicate) return collections;
17363
+ if (predicate.type === "relation") collectRelationCollections(predicate.predicate, collections);
17364
+ else if (predicate.type === "boolean") for (const arg of predicate.args) collectThreadRelationCollections(arg, collections);
17365
+ else collectThreadRelationCollections(predicate.arg, collections);
17366
+ return collections;
17367
+ }
17340
17368
  function compilePredicate(predicate, parameterOffset) {
17341
17369
  if (predicate.type === "relation") {
17342
17370
  const compiled = predicate.collection === "feedback" ? compileFeedbackScalarPredicate(predicate.predicate, parameterOffset) : compileScalarPredicate(predicate.predicate, predicate.collection === "spans" ? SPAN_FIELDS : SCORE_FIELDS, parameterOffset);
@@ -17371,12 +17399,41 @@ function compilePredicate(predicate, parameterOffset) {
17371
17399
  }
17372
17400
  return compileScalarPredicate(predicate, TRACE_FIELDS, parameterOffset, true);
17373
17401
  }
17374
- function compilePostgresTraceQuery(schema, plan) {
17402
+ function compileThreadPredicate(predicate, parameterOffset) {
17403
+ if (predicate.type === "relation") {
17404
+ const compiled = compilePredicate(predicate.predicate, parameterOffset);
17405
+ const existence = `EXISTS (
17406
+ SELECT 1 FROM eligible_roots r
17407
+ WHERE r."threadId" = t."threadId"
17408
+ AND (${compiled.sql})
17409
+ )`;
17410
+ return {
17411
+ sql: predicate.quantifier === "some" ? existence : `NOT ${existence}`,
17412
+ values: compiled.values
17413
+ };
17414
+ }
17415
+ if (predicate.type === "boolean") {
17416
+ const values = [];
17417
+ return {
17418
+ sql: predicate.args.map((arg) => {
17419
+ const compiled = compileThreadPredicate(arg, parameterOffset + values.length);
17420
+ values.push(...compiled.values);
17421
+ return `(${compiled.sql})`;
17422
+ }).join(predicate.operator === "and" ? " AND " : " OR "),
17423
+ values
17424
+ };
17425
+ }
17426
+ const compiled = compileThreadPredicate(predicate.arg, parameterOffset);
17427
+ return {
17428
+ sql: `NOT (${compiled.sql})`,
17429
+ values: compiled.values
17430
+ };
17431
+ }
17432
+ function compilePostgresTraceScope(schema, selection, relationCollections) {
17375
17433
  const spanTable = qualifiedTable(schema, TABLE_SPAN_EVENTS);
17376
17434
  const scoreTable = qualifiedTable(schema, TABLE_SCORE_EVENTS);
17377
17435
  const feedbackTable = qualifiedTable(schema, TABLE_FEEDBACK_EVENTS);
17378
- const values = [plan.timeRange.from, plan.timeRange.to];
17379
- const relationCollections = collectRelationCollections(plan.where);
17436
+ const values = [selection.timeRange.from, selection.timeRange.to];
17380
17437
  const ctes = [`root_scope AS MATERIALIZED (
17381
17438
  SELECT *
17382
17439
  FROM ${spanTable} r
@@ -17451,6 +17508,13 @@ function compilePostgresTraceQuery(schema, plan) {
17451
17508
  AND s."traceId" IN (SELECT "traceId" FROM root_scope)
17452
17509
  AND ${latestFeedbackPredicate(feedbackTable)}
17453
17510
  )`);
17511
+ return {
17512
+ ctes,
17513
+ values
17514
+ };
17515
+ }
17516
+ function compilePostgresTraceQuery(schema, plan) {
17517
+ const { ctes, values } = compilePostgresTraceScope(schema, plan, collectRelationCollections(plan.where));
17454
17518
  let predicateSql = "TRUE";
17455
17519
  if (plan.where) {
17456
17520
  const predicate = compilePredicate(plan.where, values.length + 1);
@@ -17498,6 +17562,51 @@ LIMIT $${values.length}`,
17498
17562
  values
17499
17563
  };
17500
17564
  }
17565
+ function compilePostgresThreadQuery(schema, plan) {
17566
+ const relationCollections = collectRelationCollections(plan.traces.where);
17567
+ collectThreadRelationCollections(plan.where, relationCollections);
17568
+ const { ctes, values } = compilePostgresTraceScope(schema, plan.traces, relationCollections);
17569
+ let eligibilitySql = "TRUE";
17570
+ if (plan.traces.where) {
17571
+ const eligibility = compilePredicate(plan.traces.where, values.length + 1);
17572
+ eligibilitySql = eligibility.sql;
17573
+ values.push(...eligibility.values);
17574
+ }
17575
+ ctes.push(`eligible_roots AS MATERIALIZED (
17576
+ SELECT *
17577
+ FROM root_scope r
17578
+ WHERE ${eligibilitySql}
17579
+ )`);
17580
+ ctes.push(`thread_ids AS (
17581
+ SELECT "threadId" COLLATE "C" AS "threadId"
17582
+ FROM eligible_roots
17583
+ WHERE "threadId" IS NOT NULL
17584
+ GROUP BY "threadId" COLLATE "C"
17585
+ )`);
17586
+ let threadPredicateSql = "TRUE";
17587
+ if (plan.where) {
17588
+ const predicate = compileThreadPredicate(plan.where, values.length + 1);
17589
+ threadPredicateSql = predicate.sql;
17590
+ values.push(...predicate.values);
17591
+ }
17592
+ ctes.push(`qualified_threads AS (
17593
+ SELECT t."threadId"
17594
+ FROM thread_ids t
17595
+ WHERE ${threadPredicateSql}
17596
+ )`);
17597
+ const pageCondition = plan.cursor ? `WHERE "threadId" > $${values.length + 1}` : "";
17598
+ if (plan.cursor) values.push(plan.cursor.threadId);
17599
+ values.push(plan.limit + 1);
17600
+ return {
17601
+ text: `WITH ${ctes.join(",\n")}
17602
+ SELECT "threadId"
17603
+ FROM qualified_threads
17604
+ ${pageCondition}
17605
+ ORDER BY "threadId" ASC
17606
+ LIMIT $${values.length}`,
17607
+ values
17608
+ };
17609
+ }
17501
17610
  function asIsoTimestamp$1(value) {
17502
17611
  if (value === null || value === void 0) throw new Error("Trace query returned a null timestamp");
17503
17612
  return value instanceof Date ? value.toISOString() : new Date(value).toISOString();
@@ -17556,6 +17665,19 @@ async function queryTraces(client, schema, plan, timeoutMs) {
17556
17665
  }) : null }
17557
17666
  });
17558
17667
  }
17668
+ async function queryThreads(client, schema, plan, timeoutMs) {
17669
+ const query = compilePostgresThreadQuery(schema, plan);
17670
+ const rows = await runWithPostgresTraceQueryTimeout(client, timeoutMs, (transaction) => transaction.any(query.text, query.values));
17671
+ const threads = rows.slice(0, plan.limit).map((row) => ({ threadId: String(row.threadId) }));
17672
+ const last = threads.at(-1);
17673
+ return coreStorage.queryThreadsResultSchema.parse({
17674
+ threads,
17675
+ page: { next: rows.length > plan.limit && last ? coreStorage.encodeTraceQueryCursor(plan, {
17676
+ result: "threads",
17677
+ threadId: last.threadId
17678
+ }) : null }
17679
+ });
17680
+ }
17559
17681
  //#endregion
17560
17682
  //#region src/storage/domains/observability/v-next/traces.ts
17561
17683
  /**
@@ -18451,13 +18573,15 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18451
18573
  if (!deltaPollingFeatureEnabled()) return [
18452
18574
  "metrics",
18453
18575
  "logs",
18454
- "trace-query"
18576
+ "trace-query",
18577
+ "thread-query"
18455
18578
  ];
18456
18579
  return [
18457
18580
  "metrics",
18458
18581
  "logs",
18459
18582
  "delta-polling",
18460
- "trace-query"
18583
+ "trace-query",
18584
+ "thread-query"
18461
18585
  ];
18462
18586
  }
18463
18587
  async #run(op, fn, details) {
@@ -18503,6 +18627,9 @@ var ObservabilityStoragePostgresVNext = class ObservabilityStoragePostgresVNext
18503
18627
  async queryTraces(plan) {
18504
18628
  return this.#run("QUERY_TRACES", () => queryTraces(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18505
18629
  }
18630
+ async queryThreads(plan) {
18631
+ return this.#run("QUERY_THREADS", () => queryThreads(this.#readClient, this.#schema, plan, this.#traceQueryTimeoutMs));
18632
+ }
18506
18633
  async listBranches(args) {
18507
18634
  return this.#run("LIST_BRANCHES", () => listBranches(this.#readClient, this.#schema, args));
18508
18635
  }