@dudousxd/nestjs-agent-store-drizzle 0.36.2 → 0.37.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.
package/dist/index.cjs CHANGED
@@ -282,7 +282,9 @@ var agentTokenUsage = (0, import_sqlite_core.sqliteTable)("agent_token_usage", {
282
282
  (0, import_sqlite_core.index)("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt),
283
283
  (0, import_sqlite_core.index)("agent_token_usage_created_idx").on(table.createdAt),
284
284
  (0, import_sqlite_core.index)("agent_token_usage_thread_idx").on(table.threadId),
285
- (0, import_sqlite_core.index)("agent_token_usage_feature_created_idx").on(table.feature, table.createdAt)
285
+ (0, import_sqlite_core.index)("agent_token_usage_feature_created_idx").on(table.feature, table.createdAt),
286
+ // `quota.per: 'tenant' | 'actor-in-tenant'` sums a tenant's window.
287
+ (0, import_sqlite_core.index)("agent_token_usage_tenant_created_idx").on(table.tenantRef, table.createdAt)
286
288
  ]);
287
289
  var agentGenuiCall = (0, import_sqlite_core.sqliteTable)("agent_genui_call", {
288
290
  id: (0, import_sqlite_core.text)("id").primaryKey(),
@@ -755,7 +757,9 @@ var agentTokenUsage2 = (0, import_mysql_core.mysqlTable)("agent_token_usage", {
755
757
  (0, import_mysql_core.index)("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt),
756
758
  (0, import_mysql_core.index)("agent_token_usage_created_idx").on(table.createdAt),
757
759
  (0, import_mysql_core.index)("agent_token_usage_thread_idx").on(table.threadId),
758
- (0, import_mysql_core.index)("agent_token_usage_feature_created_idx").on(table.feature, table.createdAt)
760
+ (0, import_mysql_core.index)("agent_token_usage_feature_created_idx").on(table.feature, table.createdAt),
761
+ // `quota.per: 'tenant' | 'actor-in-tenant'` sums a tenant's window.
762
+ (0, import_mysql_core.index)("agent_token_usage_tenant_created_idx").on(table.tenantRef, table.createdAt)
759
763
  ]);
760
764
  var agentGenuiCall2 = (0, import_mysql_core.mysqlTable)("agent_genui_call", {
761
765
  id: key("id").primaryKey(),
@@ -1242,7 +1246,9 @@ var agentTokenUsage3 = (0, import_pg_core.pgTable)("agent_token_usage", {
1242
1246
  (0, import_pg_core.index)("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt),
1243
1247
  (0, import_pg_core.index)("agent_token_usage_created_idx").on(table.createdAt),
1244
1248
  (0, import_pg_core.index)("agent_token_usage_thread_idx").on(table.threadId),
1245
- (0, import_pg_core.index)("agent_token_usage_feature_created_idx").on(table.feature, table.createdAt)
1249
+ (0, import_pg_core.index)("agent_token_usage_feature_created_idx").on(table.feature, table.createdAt),
1250
+ // `quota.per: 'tenant' | 'actor-in-tenant'` sums a tenant's window.
1251
+ (0, import_pg_core.index)("agent_token_usage_tenant_created_idx").on(table.tenantRef, table.createdAt)
1246
1252
  ]);
1247
1253
  var agentGenuiCall3 = (0, import_pg_core.pgTable)("agent_genui_call", {
1248
1254
  id: (0, import_pg_core.text)("id").primaryKey(),
@@ -2586,7 +2592,15 @@ var DrizzleAgentStore = class {
2586
2592
  return rows.reverse().map((row) => this.toStoredMessage(row));
2587
2593
  }
2588
2594
  async listThreads(actorRef, limit = 50) {
2589
- const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.actorRef, actorRef), (0, import_drizzle_orm4.eq)(this.t.agentThread.transient, false), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt))).orderBy((0, import_drizzle_orm4.desc)(this.t.agentThread.updatedAt)).limit(limit);
2595
+ return this.listThreadsWhere(actorRef, void 0, limit);
2596
+ }
2597
+ async listThreadsForTenant(actorRef, filter, limit = 50) {
2598
+ const column = this.t.agentThread.tenantRef;
2599
+ const visible = filter.tenant === void 0 ? (0, import_drizzle_orm4.isNull)(column) : filter.nulls ? (0, import_drizzle_orm4.or)((0, import_drizzle_orm4.eq)(column, filter.tenant), (0, import_drizzle_orm4.isNull)(column)) : (0, import_drizzle_orm4.eq)(column, filter.tenant);
2600
+ return this.listThreadsWhere(actorRef, visible, limit);
2601
+ }
2602
+ async listThreadsWhere(actorRef, tenant, limit) {
2603
+ const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.actorRef, actorRef), (0, import_drizzle_orm4.eq)(this.t.agentThread.transient, false), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt), tenant)).orderBy((0, import_drizzle_orm4.desc)(this.t.agentThread.updatedAt)).limit(limit);
2590
2604
  const previews = await this.lastMessageContents(threads.map((thread) => thread.id));
2591
2605
  return threads.map((thread) => this.toSummary(thread, previews.get(thread.id)));
2592
2606
  }
@@ -2780,6 +2794,23 @@ var DrizzleAgentStore = class {
2780
2794
  expiresAt: row.expiresAt?.toISOString() ?? null
2781
2795
  };
2782
2796
  }
2797
+ async tenantOf(target) {
2798
+ const thread = this.t.agentThread;
2799
+ if ("toolCallId" in target) {
2800
+ const [row2] = await this.db.select({
2801
+ tenantRef: thread.tenantRef
2802
+ }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm4.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(thread, (0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, thread.id)).where((0, import_drizzle_orm4.eq)(this.t.agentToolCall.id, target.toolCallId));
2803
+ return row2 === void 0 ? null : {
2804
+ tenantRef: row2.tenantRef ?? null
2805
+ };
2806
+ }
2807
+ const [row] = await this.db.select({
2808
+ tenantRef: thread.tenantRef
2809
+ }).from(thread).where((0, import_drizzle_orm4.and)("threadId" in target ? (0, import_drizzle_orm4.eq)(thread.id, target.threadId) : (0, import_drizzle_orm4.eq)(thread.activeStreamId, target.runId), (0, import_drizzle_orm4.isNull)(thread.deletedAt)));
2810
+ return row === void 0 ? null : {
2811
+ tenantRef: row.tenantRef ?? null
2812
+ };
2813
+ }
2783
2814
  async ownerOfActiveStream(runId) {
2784
2815
  const [thread] = await this.db.select({
2785
2816
  actorRef: this.t.agentThread.actorRef
@@ -3375,6 +3406,17 @@ var DrizzleAgentStore = class {
3375
3406
  * it, and no usage row crosses the wire.
3376
3407
  */
3377
3408
  async usageWindows(actorRef, ranges) {
3409
+ return this.sumUsageWindows((0, import_drizzle_orm4.eq)(this.t.agentTokenUsage.actorRef, actorRef), ranges);
3410
+ }
3411
+ /**
3412
+ * {@link usageWindows} over the tenant's rows (every actor's, or `actorRef`'s only), served by the
3413
+ * `(tenant_ref, created_at)` index.
3414
+ */
3415
+ async usageWindowsForTenant(tenantRef, ranges, actorRef) {
3416
+ const usage = this.t.agentTokenUsage;
3417
+ return this.sumUsageWindows((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(usage.tenantRef, tenantRef), actorRef !== void 0 ? (0, import_drizzle_orm4.eq)(usage.actorRef, actorRef) : void 0), ranges);
3418
+ }
3419
+ async sumUsageWindows(owner, ranges) {
3378
3420
  if (ranges.length === 0) {
3379
3421
  return [];
3380
3422
  }
@@ -3392,7 +3434,7 @@ var DrizzleAgentStore = class {
3392
3434
  });
3393
3435
  const from = new Date(Math.min(...bounds.map(({ start }) => start.getTime())));
3394
3436
  const to = new Date(Math.max(...bounds.map(({ end }) => end.getTime())));
3395
- const [row] = await this.db.select(columns).from(usage).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(usage.actorRef, actorRef), within(from, to), (0, import_drizzle_orm4.eq)(usage.countsTowardsQuota, true)));
3437
+ const [row] = await this.db.select(columns).from(usage).where((0, import_drizzle_orm4.and)(owner, within(from, to), (0, import_drizzle_orm4.eq)(usage.countsTowardsQuota, true)));
3396
3438
  return bounds.map((_, index4) => ({
3397
3439
  usedTokens: Number(row?.[`tokens${index4}`] ?? 0),
3398
3440
  costUsd: Number(row?.[`cost${index4}`] ?? 0),
@@ -3686,19 +3728,55 @@ function percentileMs(sortedDurationsMs, p) {
3686
3728
  return sortedDurationsMs[offset] ?? null;
3687
3729
  }
3688
3730
  __name(percentileMs, "percentileMs");
3689
- var DrizzleGovernanceQueries = class {
3731
+ var DrizzleGovernanceQueries = class _DrizzleGovernanceQueries {
3690
3732
  static {
3691
3733
  __name(this, "DrizzleGovernanceQueries");
3692
3734
  }
3735
+ source;
3693
3736
  pricingStore;
3737
+ tenant;
3694
3738
  db;
3695
3739
  dialect;
3696
3740
  t;
3697
- constructor(db, pricingStore) {
3741
+ /**
3742
+ * `tenant`: the rows these queries may see (see {@link forTenant}); absent → every row. Threads by
3743
+ * their `tenant_ref`; runs, messages, tool calls and generative-UI calls by their thread's; usage
3744
+ * by its own `tenant_ref`.
3745
+ */
3746
+ constructor(source, pricingStore, tenant) {
3747
+ this.source = source;
3698
3748
  this.pricingStore = pricingStore;
3699
- this.dialect = agentDialectOf(db);
3749
+ this.tenant = tenant;
3750
+ this.dialect = agentDialectOf(source);
3700
3751
  this.t = agentTablesFor(this.dialect);
3701
- this.db = asBuilder(db);
3752
+ this.db = asBuilder(source);
3753
+ }
3754
+ /** These queries narrowed to the rows `filter` may see — the console's tenant scope. */
3755
+ forTenant(filter) {
3756
+ return new _DrizzleGovernanceQueries(this.source, this.pricingStore, filter);
3757
+ }
3758
+ /** The matching rule on a `tenant_ref` column; `undefined` (no condition) without a scope. */
3759
+ tenantOn(column) {
3760
+ const filter = this.tenant;
3761
+ if (filter === void 0) return void 0;
3762
+ if (filter.tenant === void 0) return (0, import_drizzle_orm5.isNull)(column);
3763
+ return filter.nulls ? (0, import_drizzle_orm5.or)((0, import_drizzle_orm5.eq)(column, filter.tenant), (0, import_drizzle_orm5.isNull)(column)) : (0, import_drizzle_orm5.eq)(column, filter.tenant);
3764
+ }
3765
+ /** Rows keyed by a thread id whose thread the scope may see; `undefined` without a scope. */
3766
+ threadIn(threadId) {
3767
+ const visible = this.tenantOn(this.t.agentThread.tenantRef);
3768
+ if (visible === void 0) return void 0;
3769
+ return (0, import_drizzle_orm5.inArray)(threadId, this.db.select({
3770
+ id: this.t.agentThread.id
3771
+ }).from(this.t.agentThread).where(visible));
3772
+ }
3773
+ /** Tool calls (by `message_id`) on a thread the scope may see; `undefined` without a scope. */
3774
+ toolCallIn(messageId) {
3775
+ const visible = this.threadIn(this.t.agentMessage.threadId);
3776
+ if (visible === void 0) return void 0;
3777
+ return (0, import_drizzle_orm5.inArray)(messageId, this.db.select({
3778
+ id: this.t.agentMessage.id
3779
+ }).from(this.t.agentMessage).where(visible));
3702
3780
  }
3703
3781
  async loadPricing() {
3704
3782
  const prices = await this.pricingStore.listCurrentPrices();
@@ -3739,7 +3817,7 @@ var DrizzleGovernanceQueries = class {
3739
3817
  audioSeconds: usage.audioSeconds,
3740
3818
  characters: usage.characters
3741
3819
  } : {}
3742
- }).from(usage).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentTokenUsage.createdAt, start), (0, import_drizzle_orm5.lt)(this.t.agentTokenUsage.createdAt, end)));
3820
+ }).from(usage).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentTokenUsage.createdAt, start), (0, import_drizzle_orm5.lt)(this.t.agentTokenUsage.createdAt, end), this.tenantOn(usage.tenantRef)));
3743
3821
  return rows.map(toUsageInput);
3744
3822
  }
3745
3823
  async spendByModel(range) {
@@ -3804,7 +3882,7 @@ var DrizzleGovernanceQueries = class {
3804
3882
  threadId: this.t.agentMessage.threadId,
3805
3883
  createdAt: this.t.agentToolCall.createdAt,
3806
3884
  runId: this.t.agentToolCall.runId
3807
- }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm5.desc)(this.t.agentToolCall.id)).limit(limit);
3885
+ }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).where(this.threadIn(this.t.agentMessage.threadId)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm5.desc)(this.t.agentToolCall.id)).limit(limit);
3808
3886
  return rows.map((row) => ({
3809
3887
  toolCallId: row.toolCallId,
3810
3888
  toolName: row.toolName,
@@ -3871,7 +3949,7 @@ var DrizzleGovernanceQueries = class {
3871
3949
  });
3872
3950
  }
3873
3951
  async recentThreads(limit) {
3874
- const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentThread.updatedAt), (0, import_drizzle_orm5.desc)(this.t.agentThread.id)).limit(limit);
3952
+ const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt), this.tenantOn(this.t.agentThread.tenantRef))).orderBy((0, import_drizzle_orm5.desc)(this.t.agentThread.updatedAt), (0, import_drizzle_orm5.desc)(this.t.agentThread.id)).limit(limit);
3875
3953
  return this.toThreadActivityRows(threads);
3876
3954
  }
3877
3955
  /** The range's runs, with only the columns the rollups read. */
@@ -3886,7 +3964,7 @@ var DrizzleGovernanceQueries = class {
3886
3964
  ttftMs: run.ttftMs,
3887
3965
  parentRunId: run.parentRunId,
3888
3966
  startedAt: run.startedAt
3889
- }).from(run).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(run.startedAt, start), (0, import_drizzle_orm5.lte)(run.startedAt, end)));
3967
+ }).from(run).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(run.startedAt, start), (0, import_drizzle_orm5.lte)(run.startedAt, end), this.threadIn(run.threadId)));
3890
3968
  }
3891
3969
  async runMetrics(range) {
3892
3970
  const runs = await this.runsInRange(range);
@@ -3952,7 +4030,7 @@ var DrizzleGovernanceQueries = class {
3952
4030
  const { start, end } = (0, import_nestjs_agent_core5.dayBoundsUtc)(range);
3953
4031
  const runs = await this.db.select({
3954
4032
  errorCode: this.t.agentRun.errorCode
3955
- }).from(this.t.agentRun).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, start), (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, end), (0, import_drizzle_orm5.eq)(this.t.agentRun.status, "failed")));
4033
+ }).from(this.t.agentRun).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, start), (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, end), (0, import_drizzle_orm5.eq)(this.t.agentRun.status, "failed"), this.threadIn(this.t.agentRun.threadId)));
3956
4034
  const byError = /* @__PURE__ */ new Map();
3957
4035
  for (const run of runs) {
3958
4036
  const errorCode = run.errorCode ?? UNCLASSIFIED_ERROR_CODE;
@@ -3992,7 +4070,7 @@ var DrizzleGovernanceQueries = class {
3992
4070
  return result;
3993
4071
  }
3994
4072
  async recentRuns(limit) {
3995
- const runs = await this.db.select().from(this.t.agentRun).orderBy((0, import_drizzle_orm5.desc)(this.t.agentRun.startedAt), (0, import_drizzle_orm5.desc)(this.t.agentRun.id)).limit(limit);
4073
+ const runs = await this.db.select().from(this.t.agentRun).where(this.threadIn(this.t.agentRun.threadId)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentRun.startedAt), (0, import_drizzle_orm5.desc)(this.t.agentRun.id)).limit(limit);
3996
4074
  return runs.map(toRecentRunRow);
3997
4075
  }
3998
4076
  /**
@@ -4011,7 +4089,7 @@ var DrizzleGovernanceQueries = class {
4011
4089
  agentName: this.t.agentMessage.agentName,
4012
4090
  requestedAt: this.t.agentToolCall.createdAt,
4013
4091
  runId: this.t.agentToolCall.runId
4014
- }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, "pending_approval")).orderBy(this.t.agentToolCall.createdAt, this.t.agentToolCall.id).limit(limit);
4092
+ }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, "pending_approval"), this.tenantOn(this.t.agentThread.tenantRef))).orderBy(this.t.agentToolCall.createdAt, this.t.agentToolCall.id).limit(limit);
4015
4093
  return rows.map((row) => ({
4016
4094
  toolCallId: row.toolCallId,
4017
4095
  toolName: row.toolName,
@@ -4038,7 +4116,7 @@ var DrizzleGovernanceQueries = class {
4038
4116
  toolType: this.t.agentToolCall.toolType,
4039
4117
  status: this.t.agentToolCall.status,
4040
4118
  executionMs: this.t.agentToolCall.executionMs
4041
- }).from(this.t.agentToolCall).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, start), (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, end)));
4119
+ }).from(this.t.agentToolCall).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, start), (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, end), this.toolCallIn(this.t.agentToolCall.messageId)));
4042
4120
  const byTool = /* @__PURE__ */ new Map();
4043
4121
  for (const call of calls) {
4044
4122
  const key2 = `${call.toolName} ${call.toolType}`;
@@ -4088,7 +4166,7 @@ var DrizzleGovernanceQueries = class {
4088
4166
  if (filters?.toolType !== void 0 && !isToolKind(filters.toolType)) {
4089
4167
  return emptyPage(query);
4090
4168
  }
4091
- const whereClause = (0, import_drizzle_orm5.and)(filters?.toolName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolName, filters.toolName) : void 0, filters?.toolType !== void 0 && isToolKind(filters.toolType) ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolType, filters.toolType) : void 0, filters?.status !== void 0 && isToolCallStatus(filters.status) ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, filters.status) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, filters.threadId) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, dayEndUtc(filters.toDay)) : void 0);
4169
+ const whereClause = (0, import_drizzle_orm5.and)(filters?.toolName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolName, filters.toolName) : void 0, filters?.toolType !== void 0 && isToolKind(filters.toolType) ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolType, filters.toolType) : void 0, filters?.status !== void 0 && isToolCallStatus(filters.status) ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, filters.status) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, filters.threadId) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, dayEndUtc(filters.toDay)) : void 0, this.threadIn(this.t.agentMessage.threadId));
4092
4170
  const [totalRow] = await this.db.select({
4093
4171
  value: (0, import_drizzle_orm5.count)()
4094
4172
  }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).where(whereClause);
@@ -4124,7 +4202,7 @@ var DrizzleGovernanceQueries = class {
4124
4202
  */
4125
4203
  async threadsPage(query) {
4126
4204
  const filters = query.where;
4127
- const whereClause = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt), filters?.actorRef !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.actorRef, filters.actorRef) : void 0, filters?.title !== void 0 ? (0, import_drizzle_orm5.like)(import_drizzle_orm5.sql`lower(${this.t.agentThread.title})`, `%${filters.title.toLowerCase()}%`) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentThread.updatedAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentThread.updatedAt, dayEndUtc(filters.toDay)) : void 0);
4205
+ const whereClause = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt), filters?.actorRef !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.actorRef, filters.actorRef) : void 0, filters?.title !== void 0 ? (0, import_drizzle_orm5.like)(import_drizzle_orm5.sql`lower(${this.t.agentThread.title})`, `%${filters.title.toLowerCase()}%`) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentThread.updatedAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentThread.updatedAt, dayEndUtc(filters.toDay)) : void 0, this.tenantOn(this.t.agentThread.tenantRef));
4128
4206
  const [totalRow] = await this.db.select({
4129
4207
  value: (0, import_drizzle_orm5.count)()
4130
4208
  }).from(this.t.agentThread).where(whereClause);
@@ -4146,7 +4224,7 @@ var DrizzleGovernanceQueries = class {
4146
4224
  if (filters?.status !== void 0 && !isRunStatus(filters.status)) {
4147
4225
  return emptyPage(query);
4148
4226
  }
4149
- const whereClause = (0, import_drizzle_orm5.and)(filters?.agentName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.agentName, filters.agentName) : void 0, filters?.status !== void 0 && isRunStatus(filters.status) ? (0, import_drizzle_orm5.eq)(this.t.agentRun.status, filters.status) : void 0, filters?.errorCode !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.errorCode, filters.errorCode) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, filters.threadId) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, dayEndUtc(filters.toDay)) : void 0);
4227
+ const whereClause = (0, import_drizzle_orm5.and)(filters?.agentName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.agentName, filters.agentName) : void 0, filters?.status !== void 0 && isRunStatus(filters.status) ? (0, import_drizzle_orm5.eq)(this.t.agentRun.status, filters.status) : void 0, filters?.errorCode !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.errorCode, filters.errorCode) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, filters.threadId) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, dayEndUtc(filters.toDay)) : void 0, this.threadIn(this.t.agentRun.threadId));
4150
4228
  const [totalRow] = await this.db.select({
4151
4229
  value: (0, import_drizzle_orm5.count)()
4152
4230
  }).from(this.t.agentRun).where(whereClause);
@@ -4170,7 +4248,7 @@ var DrizzleGovernanceQueries = class {
4170
4248
  */
4171
4249
  async approvalsPage(query) {
4172
4250
  const filters = query.where;
4173
- const whereClause = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, "pending_approval"), filters?.toolName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolName, filters.toolName) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.id, filters.threadId) : void 0, filters?.actorRef !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.actorRef, filters.actorRef) : void 0, filters?.agentName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentMessage.agentName, filters.agentName) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, dayEndUtc(filters.toDay)) : void 0);
4251
+ const whereClause = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, "pending_approval"), filters?.toolName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolName, filters.toolName) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.id, filters.threadId) : void 0, filters?.actorRef !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.actorRef, filters.actorRef) : void 0, filters?.agentName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentMessage.agentName, filters.agentName) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, dayEndUtc(filters.toDay)) : void 0, this.tenantOn(this.t.agentThread.tenantRef));
4174
4252
  const [totalRow] = await this.db.select({
4175
4253
  value: (0, import_drizzle_orm5.count)()
4176
4254
  }).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where(whereClause);
@@ -4211,7 +4289,7 @@ var DrizzleGovernanceQueries = class {
4211
4289
  const [row] = await this.db.select({
4212
4290
  run: this.t.agentRun,
4213
4291
  thread: this.t.agentThread
4214
- }).from(this.t.agentRun).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm5.eq)(this.t.agentRun.id, runId)).limit(1);
4292
+ }).from(this.t.agentRun).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(this.t.agentRun.id, runId), this.tenantOn(this.t.agentThread.tenantRef))).limit(1);
4215
4293
  if (row === void 0) {
4216
4294
  return null;
4217
4295
  }
@@ -4235,7 +4313,7 @@ var DrizzleGovernanceQueries = class {
4235
4313
  * message. Soft-deleted threads are returned with `deleted: true`; an audit still needs them.
4236
4314
  */
4237
4315
  async threadDetail(query) {
4238
- const [thread] = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.eq)(this.t.agentThread.id, query.threadId)).limit(1);
4316
+ const [thread] = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(this.t.agentThread.id, query.threadId), this.tenantOn(this.t.agentThread.tenantRef))).limit(1);
4239
4317
  if (thread === void 0) {
4240
4318
  return null;
4241
4319
  }
@@ -4287,7 +4365,7 @@ var DrizzleGovernanceQueries = class {
4287
4365
  async genuiStats(range) {
4288
4366
  const { start, end } = (0, import_nestjs_agent_core5.dayBoundsUtc)(range);
4289
4367
  const table = this.t.agentGenuiCall;
4290
- const rows = await this.db.select().from(table).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(table.createdAt, start.getTime()), (0, import_drizzle_orm5.lte)(table.createdAt, end.getTime())));
4368
+ const rows = await this.db.select().from(table).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(table.createdAt, start.getTime()), (0, import_drizzle_orm5.lte)(table.createdAt, end.getTime()), this.threadIn(table.threadId)));
4291
4369
  return (0, import_nestjs_agent_core5.summarizeGenuiCalls)(rows.map(toGenuiCallRow));
4292
4370
  }
4293
4371
  /** Tool-call counts for a set of messages in ONE grouped query — the detail view's no-N+1 guarantee. */