@dudousxd/nestjs-agent-store-drizzle 0.29.12 → 0.30.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.js CHANGED
@@ -193,6 +193,37 @@ var agentTokenUsage = sqliteTable("agent_token_usage", {
193
193
  }, (table) => [
194
194
  index("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt)
195
195
  ]);
196
+ var agentGenuiCall = sqliteTable("agent_genui_call", {
197
+ id: text("id").primaryKey(),
198
+ runId: text("run_id").notNull(),
199
+ threadId: text("thread_id").notNull().references(() => agentThread.id, {
200
+ onDelete: "cascade"
201
+ }),
202
+ actorRef: text("actor_ref").notNull(),
203
+ agentName: text("agent_name"),
204
+ step: integer("step").notNull(),
205
+ toolName: text("tool_name").notNull(),
206
+ channel: text("channel").notNull(),
207
+ mode: text("mode").$type().notNull(),
208
+ modelId: text("model_id").notNull(),
209
+ attempt: integer("attempt").notNull(),
210
+ outcome: text("outcome").$type().notNull(),
211
+ validity: text("validity").$type().notNull(),
212
+ errorCategory: text("error_category").$type(),
213
+ errorMessage: text("error_message"),
214
+ inputTokens: integer("input_tokens").notNull(),
215
+ outputTokens: integer("output_tokens").notNull(),
216
+ costUsd: real("cost_usd"),
217
+ costSource: text("cost_source").$type(),
218
+ firstFrameMs: integer("first_frame_ms"),
219
+ completeFrameMs: integer("complete_frame_ms"),
220
+ payloadBytes: integer("payload_bytes").notNull(),
221
+ payload: text("payload"),
222
+ createdAt: integer("created_at").notNull()
223
+ }, (table) => [
224
+ index("agent_genui_call_created_idx").on(table.createdAt),
225
+ index("agent_genui_call_run_idx").on(table.runId)
226
+ ]);
196
227
  var agentModelPricing = sqliteTable("agent_model_pricing", {
197
228
  id: text("id").primaryKey(),
198
229
  modelId: text("model_id").notNull(),
@@ -357,6 +388,7 @@ var agentSchema = {
357
388
  agentQueuedMessage,
358
389
  agentToolCall,
359
390
  agentTokenUsage,
391
+ agentGenuiCall,
360
392
  agentModelPricing,
361
393
  agentRun,
362
394
  agentMemory,
@@ -498,6 +530,45 @@ var agentTokenUsage2 = mysqlTable("agent_token_usage", {
498
530
  }, (table) => [
499
531
  index2("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt)
500
532
  ]);
533
+ var agentGenuiCall2 = mysqlTable("agent_genui_call", {
534
+ id: key("id").primaryKey(),
535
+ runId: key("run_id").notNull(),
536
+ threadId: key("thread_id").notNull().references(() => agentThread2.id, {
537
+ onDelete: "cascade"
538
+ }),
539
+ actorRef: key("actor_ref").notNull(),
540
+ agentName: key("agent_name"),
541
+ step: int("step").notNull(),
542
+ toolName: key("tool_name").notNull(),
543
+ channel: key("channel").notNull(),
544
+ mode: key("mode").$type().notNull(),
545
+ modelId: key("model_id").notNull(),
546
+ attempt: int("attempt").notNull(),
547
+ outcome: key("outcome").$type().notNull(),
548
+ validity: key("validity").$type().notNull(),
549
+ errorCategory: key("error_category").$type(),
550
+ errorMessage: longtext("error_message"),
551
+ inputTokens: int("input_tokens").notNull(),
552
+ outputTokens: int("output_tokens").notNull(),
553
+ costUsd: double("cost_usd"),
554
+ costSource: varchar("cost_source", {
555
+ length: 16
556
+ }).$type(),
557
+ firstFrameMs: bigint("first_frame_ms", {
558
+ mode: "number"
559
+ }),
560
+ completeFrameMs: bigint("complete_frame_ms", {
561
+ mode: "number"
562
+ }),
563
+ payloadBytes: int("payload_bytes").notNull(),
564
+ payload: longtext("payload"),
565
+ createdAt: bigint("created_at", {
566
+ mode: "number"
567
+ }).notNull()
568
+ }, (table) => [
569
+ index2("agent_genui_call_created_idx").on(table.createdAt),
570
+ index2("agent_genui_call_run_idx").on(table.runId)
571
+ ]);
501
572
  var agentModelPricing2 = mysqlTable("agent_model_pricing", {
502
573
  id: key("id").primaryKey(),
503
574
  modelId: key("model_id").notNull(),
@@ -653,6 +724,7 @@ var mysqlAgentSchema = {
653
724
  agentQueuedMessage: agentQueuedMessage2,
654
725
  agentToolCall: agentToolCall2,
655
726
  agentTokenUsage: agentTokenUsage2,
727
+ agentGenuiCall: agentGenuiCall2,
656
728
  agentModelPricing: agentModelPricing2,
657
729
  agentRun: agentRun2,
658
730
  agentMemory: agentMemory2,
@@ -664,7 +736,7 @@ var mysqlAgentSchema = {
664
736
 
665
737
  // src/schema-pg.ts
666
738
  import { canonicalActionProposalJson as canonicalActionProposalJson2 } from "@dudousxd/nestjs-agent-core";
667
- import { bigint as bigint2, boolean as boolean2, customType as customType2, doublePrecision, index as index3, integer as integer2, jsonb, pgTable, primaryKey as primaryKey3, text as text2, timestamp, uniqueIndex as uniqueIndex3 } from "drizzle-orm/pg-core";
739
+ import { bigint as bigint2, boolean as boolean2, customType as customType2, doublePrecision, index as index3, integer as integer2, jsonb, pgTable, primaryKey as primaryKey3, text as text2, timestamp, uniqueIndex as uniqueIndex3, varchar as varchar2 } from "drizzle-orm/pg-core";
668
740
  var ts2 = /* @__PURE__ */ __name((name) => timestamp(name, {
669
741
  withTimezone: true,
670
742
  precision: 3,
@@ -781,6 +853,45 @@ var agentTokenUsage3 = pgTable("agent_token_usage", {
781
853
  }, (table) => [
782
854
  index3("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt)
783
855
  ]);
856
+ var agentGenuiCall3 = pgTable("agent_genui_call", {
857
+ id: text2("id").primaryKey(),
858
+ runId: text2("run_id").notNull(),
859
+ threadId: text2("thread_id").notNull().references(() => agentThread3.id, {
860
+ onDelete: "cascade"
861
+ }),
862
+ actorRef: text2("actor_ref").notNull(),
863
+ agentName: text2("agent_name"),
864
+ step: integer2("step").notNull(),
865
+ toolName: text2("tool_name").notNull(),
866
+ channel: text2("channel").notNull(),
867
+ mode: text2("mode").$type().notNull(),
868
+ modelId: text2("model_id").notNull(),
869
+ attempt: integer2("attempt").notNull(),
870
+ outcome: text2("outcome").$type().notNull(),
871
+ validity: text2("validity").$type().notNull(),
872
+ errorCategory: text2("error_category").$type(),
873
+ errorMessage: text2("error_message"),
874
+ inputTokens: integer2("input_tokens").notNull(),
875
+ outputTokens: integer2("output_tokens").notNull(),
876
+ costUsd: doublePrecision("cost_usd"),
877
+ costSource: varchar2("cost_source", {
878
+ length: 16
879
+ }).$type(),
880
+ firstFrameMs: bigint2("first_frame_ms", {
881
+ mode: "number"
882
+ }),
883
+ completeFrameMs: bigint2("complete_frame_ms", {
884
+ mode: "number"
885
+ }),
886
+ payloadBytes: integer2("payload_bytes").notNull(),
887
+ payload: text2("payload"),
888
+ createdAt: bigint2("created_at", {
889
+ mode: "number"
890
+ }).notNull()
891
+ }, (table) => [
892
+ index3("agent_genui_call_created_idx").on(table.createdAt),
893
+ index3("agent_genui_call_run_idx").on(table.runId)
894
+ ]);
784
895
  var agentModelPricing3 = pgTable("agent_model_pricing", {
785
896
  id: text2("id").primaryKey(),
786
897
  modelId: text2("model_id").notNull(),
@@ -934,6 +1045,7 @@ var pgAgentSchema = {
934
1045
  agentQueuedMessage: agentQueuedMessage3,
935
1046
  agentToolCall: agentToolCall3,
936
1047
  agentTokenUsage: agentTokenUsage3,
1048
+ agentGenuiCall: agentGenuiCall3,
937
1049
  agentModelPricing: agentModelPricing3,
938
1050
  agentRun: agentRun3,
939
1051
  agentMemory: agentMemory3,
@@ -2538,6 +2650,44 @@ var DrizzleAgentStore = class {
2538
2650
  createdAt: /* @__PURE__ */ new Date()
2539
2651
  });
2540
2652
  }
2653
+ /**
2654
+ * Generative-UI telemetry: one `agent_genui_call` row per UI tool call, upserted by its tool call
2655
+ * id — a replayed record replaces the row (keeping the first `created_at`) rather than adding one.
2656
+ */
2657
+ async recordGenuiCalls(rows) {
2658
+ const table = this.t.agentGenuiCall;
2659
+ for (const row of rows) {
2660
+ const columns = {
2661
+ runId: row.runId,
2662
+ threadId: row.threadId,
2663
+ actorRef: row.actorRef,
2664
+ agentName: row.agentName ?? null,
2665
+ step: row.step,
2666
+ toolName: row.toolName,
2667
+ channel: row.channel,
2668
+ mode: row.mode,
2669
+ modelId: row.modelId,
2670
+ attempt: row.attempt,
2671
+ outcome: row.outcome,
2672
+ validity: row.validity,
2673
+ errorCategory: row.errorCategory ?? null,
2674
+ errorMessage: row.errorMessage ?? null,
2675
+ inputTokens: row.inputTokens,
2676
+ outputTokens: row.outputTokens,
2677
+ costUsd: row.costUsd ?? null,
2678
+ costSource: row.costSource ?? null,
2679
+ firstFrameMs: row.firstFrameMs ?? null,
2680
+ completeFrameMs: row.completeFrameMs ?? null,
2681
+ payloadBytes: row.payloadBytes,
2682
+ payload: row.payload ?? null
2683
+ };
2684
+ await upsert(this.dialect, this.db.insert(table).values({
2685
+ id: row.toolCallId,
2686
+ ...columns,
2687
+ createdAt: Date.now()
2688
+ }), table.id, columns);
2689
+ }
2690
+ }
2541
2691
  async quotaToday(actorRef, day) {
2542
2692
  return this.usageBetween(actorRef, day, day);
2543
2693
  }
@@ -2651,7 +2801,7 @@ function toQueuedMessage(row) {
2651
2801
  __name(toQueuedMessage, "toQueuedMessage");
2652
2802
 
2653
2803
  // src/drizzle-governance-queries.ts
2654
- import { bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, dayBoundsUtc, rollupThreadUsage, truncateDetailContent } from "@dudousxd/nestjs-agent-core";
2804
+ import { bucketByActor, bucketByModel, bucketByThread, bucketUsageTrend, dayBoundsUtc, rollupThreadUsage, summarizeGenuiCalls, truncateDetailContent } from "@dudousxd/nestjs-agent-core";
2655
2805
  import { and as and3, asc as asc3, count, desc as desc2, eq as eq3, gte as gte2, inArray as inArray2, isNull as isNull3, like, lte as lte3, sql as sql4, sum } from "drizzle-orm";
2656
2806
  var UNCLASSIFIED_ERROR_CODE = "unknown";
2657
2807
  var DEFAULT_AGENT_BUCKET = "(default)";
@@ -2710,6 +2860,51 @@ function emptyPage(query) {
2710
2860
  };
2711
2861
  }
2712
2862
  __name(emptyPage, "emptyPage");
2863
+ function toGenuiCallRow(row) {
2864
+ return {
2865
+ toolCallId: row.id,
2866
+ runId: row.runId,
2867
+ threadId: row.threadId,
2868
+ actorRef: row.actorRef,
2869
+ ...row.agentName !== null ? {
2870
+ agentName: row.agentName
2871
+ } : {},
2872
+ step: Number(row.step),
2873
+ toolName: row.toolName,
2874
+ channel: row.channel,
2875
+ mode: row.mode,
2876
+ modelId: row.modelId,
2877
+ attempt: Number(row.attempt),
2878
+ outcome: row.outcome,
2879
+ validity: row.validity,
2880
+ ...row.errorCategory !== null ? {
2881
+ errorCategory: row.errorCategory
2882
+ } : {},
2883
+ ...row.errorMessage !== null ? {
2884
+ errorMessage: row.errorMessage
2885
+ } : {},
2886
+ inputTokens: Number(row.inputTokens),
2887
+ outputTokens: Number(row.outputTokens),
2888
+ ...row.costUsd !== null ? {
2889
+ costUsd: Number(row.costUsd)
2890
+ } : {},
2891
+ ...row.costSource !== null ? {
2892
+ costSource: row.costSource
2893
+ } : {},
2894
+ ...row.firstFrameMs !== null ? {
2895
+ firstFrameMs: Number(row.firstFrameMs)
2896
+ } : {},
2897
+ ...row.completeFrameMs !== null ? {
2898
+ completeFrameMs: Number(row.completeFrameMs)
2899
+ } : {},
2900
+ payloadBytes: Number(row.payloadBytes),
2901
+ ...row.payload !== null ? {
2902
+ payload: row.payload
2903
+ } : {},
2904
+ createdAt: new Date(Number(row.createdAt))
2905
+ };
2906
+ }
2907
+ __name(toGenuiCallRow, "toGenuiCallRow");
2713
2908
  function toRecentRunRow(run) {
2714
2909
  return {
2715
2910
  runId: run.id,
@@ -3295,6 +3490,13 @@ var DrizzleGovernanceQueries = class {
3295
3490
  })
3296
3491
  };
3297
3492
  }
3493
+ /** Generative-UI stats over the `agent_genui_call` rows created in the (UTC, inclusive) day range. */
3494
+ async genuiStats(range) {
3495
+ const { start, end } = dayBoundsUtc(range);
3496
+ const table = this.t.agentGenuiCall;
3497
+ const rows = await this.db.select().from(table).where(and3(gte2(table.createdAt, start.getTime()), lte3(table.createdAt, end.getTime())));
3498
+ return summarizeGenuiCalls(rows.map(toGenuiCallRow));
3499
+ }
3298
3500
  /** Tool-call counts for a set of messages in ONE grouped query — the detail view's no-N+1 guarantee. */
3299
3501
  async toolCallCountsByMessage(messageIds) {
3300
3502
  const counts = /* @__PURE__ */ new Map();
@@ -4140,6 +4342,7 @@ export {
4140
4342
  agentChannelState,
4141
4343
  agentConfirmToken,
4142
4344
  agentDialectOf,
4345
+ agentGenuiCall,
4143
4346
  agentMemory,
4144
4347
  agentMessage,
4145
4348
  agentModelPricing,