@dudousxd/nestjs-agent-store-drizzle 0.21.0 → 0.22.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
@@ -28,6 +28,8 @@ __export(index_exports, {
28
28
  DrizzleMemoryProvider: () => DrizzleMemoryProvider,
29
29
  DrizzlePricingStore: () => DrizzlePricingStore,
30
30
  DrizzleRagIngestionLog: () => DrizzleRagIngestionLog,
31
+ DrizzleStreamFrameTable: () => DrizzleStreamFrameTable,
32
+ DrizzleTokenStreamSink: () => DrizzleTokenStreamSink,
31
33
  RAG_INGESTION_LOG_PAGE_ORDER: () => RAG_INGESTION_LOG_PAGE_ORDER,
32
34
  agentConfirmToken: () => agentConfirmToken,
33
35
  agentMemory: () => agentMemory,
@@ -36,6 +38,7 @@ __export(index_exports, {
36
38
  agentQueuedMessage: () => agentQueuedMessage,
37
39
  agentRun: () => agentRun,
38
40
  agentSchema: () => agentSchema,
41
+ agentStreamFrame: () => agentStreamFrame,
39
42
  agentThread: () => agentThread,
40
43
  agentTokenUsage: () => agentTokenUsage,
41
44
  agentToolCall: () => agentToolCall,
@@ -318,6 +321,20 @@ var agentConfirmToken = (0, import_sqlite_core.sqliteTable)("agent_confirm_token
318
321
  }, (table) => [
319
322
  (0, import_sqlite_core.index)("agent_confirm_token_expires_idx").on(table.expiresAt)
320
323
  ]);
324
+ var agentStreamFrame = (0, import_sqlite_core.sqliteTable)("agent_stream_frame", {
325
+ runId: (0, import_sqlite_core.text)("run_id").notNull(),
326
+ seq: (0, import_sqlite_core.integer)("seq").notNull(),
327
+ frame: (0, import_sqlite_core.text)("frame"),
328
+ error: (0, import_sqlite_core.text)("error"),
329
+ createdAt: (0, import_sqlite_core.integer)("created_at").notNull()
330
+ }, (table) => [
331
+ (0, import_sqlite_core.primaryKey)({
332
+ columns: [
333
+ table.runId,
334
+ table.seq
335
+ ]
336
+ })
337
+ ]);
321
338
  var agentSchema = {
322
339
  agentThread,
323
340
  agentMessage,
@@ -484,6 +501,14 @@ var statements = [
484
501
  )`,
485
502
  `CREATE INDEX IF NOT EXISTS rag_ingestion_log_collection_idx
486
503
  ON rag_ingestion_log (collection, updated_at)`,
504
+ `CREATE TABLE IF NOT EXISTS agent_stream_frame (
505
+ run_id TEXT NOT NULL,
506
+ seq INTEGER NOT NULL,
507
+ frame TEXT,
508
+ error TEXT,
509
+ created_at INTEGER NOT NULL,
510
+ PRIMARY KEY (run_id, seq)
511
+ )`,
487
512
  `CREATE TABLE IF NOT EXISTS agent_confirm_token (
488
513
  hash TEXT PRIMARY KEY NOT NULL,
489
514
  actor_ref TEXT NOT NULL,
@@ -2193,8 +2218,71 @@ var DrizzlePricingStore = class {
2193
2218
  }
2194
2219
  };
2195
2220
 
2196
- // src/drizzle-confirm-token-store.ts
2221
+ // src/drizzle-token-stream-sink.ts
2222
+ var import_nestjs_agent_core4 = require("@dudousxd/nestjs-agent-core");
2197
2223
  var import_drizzle_orm6 = require("drizzle-orm");
2224
+ function isUniqueViolation(error) {
2225
+ for (let current = error; current !== null && typeof current === "object"; ) {
2226
+ const code = current.code;
2227
+ if (typeof code === "string" && code.startsWith("SQLITE_CONSTRAINT")) return true;
2228
+ current = current.cause;
2229
+ }
2230
+ return false;
2231
+ }
2232
+ __name(isUniqueViolation, "isUniqueViolation");
2233
+ var DrizzleStreamFrameTable = class {
2234
+ static {
2235
+ __name(this, "DrizzleStreamFrameTable");
2236
+ }
2237
+ db;
2238
+ constructor(db) {
2239
+ this.db = db;
2240
+ }
2241
+ async append(runId, row) {
2242
+ await this.db.run(import_drizzle_orm6.sql`INSERT INTO agent_stream_frame (run_id, seq, frame, error, created_at)
2243
+ SELECT ${runId}, COALESCE(MAX(seq), 0) + 1, ${row.frame}, ${row.error}, ${row.createdAt}
2244
+ FROM agent_stream_frame WHERE run_id = ${runId}`);
2245
+ }
2246
+ async read(runId, after, limit) {
2247
+ return this.db.select({
2248
+ seq: agentStreamFrame.seq,
2249
+ frame: agentStreamFrame.frame,
2250
+ error: agentStreamFrame.error
2251
+ }).from(agentStreamFrame).where((0, import_drizzle_orm6.and)((0, import_drizzle_orm6.eq)(agentStreamFrame.runId, runId), (0, import_drizzle_orm6.gt)(agentStreamFrame.seq, after))).orderBy((0, import_drizzle_orm6.asc)(agentStreamFrame.seq)).limit(limit);
2252
+ }
2253
+ async has(runId) {
2254
+ const rows = await this.db.select({
2255
+ seq: agentStreamFrame.seq
2256
+ }).from(agentStreamFrame).where((0, import_drizzle_orm6.eq)(agentStreamFrame.runId, runId)).limit(1);
2257
+ return rows.length > 0;
2258
+ }
2259
+ async remove(runIds) {
2260
+ if (runIds.length === 0) return;
2261
+ await this.db.delete(agentStreamFrame).where((0, import_drizzle_orm6.inArray)(agentStreamFrame.runId, [
2262
+ ...runIds
2263
+ ]));
2264
+ }
2265
+ async lapsedRuns(cutoff) {
2266
+ const rows = await this.db.select({
2267
+ runId: agentStreamFrame.runId
2268
+ }).from(agentStreamFrame).groupBy(agentStreamFrame.runId).having((0, import_drizzle_orm6.lt)((0, import_drizzle_orm6.max)(agentStreamFrame.createdAt), cutoff));
2269
+ return rows.map((row) => row.runId);
2270
+ }
2271
+ isUniqueViolation(error) {
2272
+ return isUniqueViolation(error);
2273
+ }
2274
+ };
2275
+ var DrizzleTokenStreamSink = class extends import_nestjs_agent_core4.SqlTokenStreamSink {
2276
+ static {
2277
+ __name(this, "DrizzleTokenStreamSink");
2278
+ }
2279
+ constructor(db, options = {}) {
2280
+ super(new DrizzleStreamFrameTable(db), options);
2281
+ }
2282
+ };
2283
+
2284
+ // src/drizzle-confirm-token-store.ts
2285
+ var import_drizzle_orm7 = require("drizzle-orm");
2198
2286
  var DrizzleConfirmTokenStore = class {
2199
2287
  static {
2200
2288
  __name(this, "DrizzleConfirmTokenStore");
@@ -2216,10 +2304,10 @@ var DrizzleConfirmTokenStore = class {
2216
2304
  return inserted.length === 1;
2217
2305
  }
2218
2306
  async release(hash) {
2219
- await this.db.delete(agentConfirmToken).where((0, import_drizzle_orm6.eq)(agentConfirmToken.hash, hash));
2307
+ await this.db.delete(agentConfirmToken).where((0, import_drizzle_orm7.eq)(agentConfirmToken.hash, hash));
2220
2308
  }
2221
2309
  async purgeExpired(now = Date.now()) {
2222
- const purged = await this.db.delete(agentConfirmToken).where((0, import_drizzle_orm6.lt)(agentConfirmToken.expiresAt, now)).returning({
2310
+ const purged = await this.db.delete(agentConfirmToken).where((0, import_drizzle_orm7.lt)(agentConfirmToken.expiresAt, now)).returning({
2223
2311
  hash: agentConfirmToken.hash
2224
2312
  });
2225
2313
  return purged.length;
@@ -2229,7 +2317,7 @@ var DrizzleConfirmTokenStore = class {
2229
2317
  // src/drizzle-rag-ingestion-log.ts
2230
2318
  var import_node_diagnostics_channel = require("diagnostics_channel");
2231
2319
  var import_common = require("@nestjs/common");
2232
- var import_drizzle_orm7 = require("drizzle-orm");
2320
+ var import_drizzle_orm8 = require("drizzle-orm");
2233
2321
  function _ts_decorate(decorators, target, key, desc4) {
2234
2322
  var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key) : desc4, d;
2235
2323
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
@@ -2277,8 +2365,8 @@ function num(value) {
2277
2365
  }
2278
2366
  __name(num, "num");
2279
2367
  var RAG_INGESTION_LOG_PAGE_ORDER = [
2280
- (0, import_drizzle_orm7.desc)(ragIngestionLog.updatedAt),
2281
- (0, import_drizzle_orm7.asc)(ragIngestionLog.documentId)
2368
+ (0, import_drizzle_orm8.desc)(ragIngestionLog.updatedAt),
2369
+ (0, import_drizzle_orm8.asc)(ragIngestionLog.documentId)
2282
2370
  ];
2283
2371
  var DEFAULT_PAGE_SIZE = 200;
2284
2372
  var DEFAULT_BATCH_SIZE = 200;
@@ -2342,7 +2430,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2342
2430
  */
2343
2431
  async listPage(query = {}) {
2344
2432
  const [totalRow] = await this.db.select({
2345
- value: (0, import_drizzle_orm7.count)()
2433
+ value: (0, import_drizzle_orm8.count)()
2346
2434
  }).from(ragIngestionLog).where(this.where(query));
2347
2435
  return {
2348
2436
  rows: await this.list(query),
@@ -2421,19 +2509,19 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2421
2509
  }
2422
2510
  /** The latest recorded outcome for one document, or null if it was never attempted. */
2423
2511
  async get(documentId) {
2424
- const [row] = await this.db.select().from(ragIngestionLog).where((0, import_drizzle_orm7.eq)(ragIngestionLog.documentId, documentId));
2512
+ const [row] = await this.db.select().from(ragIngestionLog).where((0, import_drizzle_orm8.eq)(ragIngestionLog.documentId, documentId));
2425
2513
  return row ?? null;
2426
2514
  }
2427
2515
  /** Forget one document's record. Returns whether a row was actually removed. */
2428
2516
  async remove(documentId) {
2429
- const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm7.eq)(ragIngestionLog.documentId, documentId)).returning({
2517
+ const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm8.eq)(ragIngestionLog.documentId, documentId)).returning({
2430
2518
  documentId: ragIngestionLog.documentId
2431
2519
  });
2432
2520
  return removed.length > 0;
2433
2521
  }
2434
2522
  /** Forget every record for a collection — for when the collection itself is deleted. */
2435
2523
  async removeByCollection(collection) {
2436
- const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm7.eq)(ragIngestionLog.collection, collection)).returning({
2524
+ const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm8.eq)(ragIngestionLog.collection, collection)).returning({
2437
2525
  documentId: ragIngestionLog.documentId
2438
2526
  });
2439
2527
  return removed.length;
@@ -2465,7 +2553,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2465
2553
  }
2466
2554
  }
2467
2555
  where(query) {
2468
- return (0, import_drizzle_orm7.and)(query.collection !== void 0 ? (0, import_drizzle_orm7.eq)(ragIngestionLog.collection, query.collection) : void 0, query.status !== void 0 ? (0, import_drizzle_orm7.eq)(ragIngestionLog.status, query.status) : void 0);
2556
+ return (0, import_drizzle_orm8.and)(query.collection !== void 0 ? (0, import_drizzle_orm8.eq)(ragIngestionLog.collection, query.collection) : void 0, query.status !== void 0 ? (0, import_drizzle_orm8.eq)(ragIngestionLog.status, query.status) : void 0);
2469
2557
  }
2470
2558
  /**
2471
2559
  * The coordinates filter AND'd with "strictly after `cursor` in
@@ -2480,7 +2568,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2480
2568
  if (cursor === void 0) {
2481
2569
  return base;
2482
2570
  }
2483
- return (0, import_drizzle_orm7.and)(base, (0, import_drizzle_orm7.or)((0, import_drizzle_orm7.lt)(ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm7.and)((0, import_drizzle_orm7.eq)(ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm7.gt)(ragIngestionLog.documentId, cursor.documentId))));
2571
+ return (0, import_drizzle_orm8.and)(base, (0, import_drizzle_orm8.or)((0, import_drizzle_orm8.lt)(ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm8.and)((0, import_drizzle_orm8.eq)(ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm8.gt)(ragIngestionLog.documentId, cursor.documentId))));
2484
2572
  }
2485
2573
  /**
2486
2574
  * Upsert one outcome. Keyed by document id so the row always reflects the *current* state — a
@@ -2556,7 +2644,7 @@ DrizzleRagIngestionLog = _ts_decorate([
2556
2644
  ], DrizzleRagIngestionLog);
2557
2645
 
2558
2646
  // src/drizzle-agent-store.module.ts
2559
- var import_nestjs_agent_core4 = require("@dudousxd/nestjs-agent-core");
2647
+ var import_nestjs_agent_core5 = require("@dudousxd/nestjs-agent-core");
2560
2648
  var import_common2 = require("@nestjs/common");
2561
2649
  function _ts_decorate2(decorators, target, key, desc4) {
2562
2650
  var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key) : desc4, d;
@@ -2593,7 +2681,7 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2593
2681
  useFactory: /* @__PURE__ */ __name(() => new DrizzleAgentStore(options.db), "useFactory")
2594
2682
  },
2595
2683
  {
2596
- provide: import_nestjs_agent_core4.AGENT_STORE,
2684
+ provide: import_nestjs_agent_core5.AGENT_STORE,
2597
2685
  useExisting: DrizzleAgentStore
2598
2686
  },
2599
2687
  // Exported, never bound to AGENT_MEMORY: that token's PRESENCE is what turns memory on, so
@@ -2610,21 +2698,21 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2610
2698
  // AGENT_PRICING_STORE: the host's own store when supplied, else the default Drizzle one.
2611
2699
  // DrizzleGovernanceQueries prices usage against whatever is bound here.
2612
2700
  options.pricingStore ? {
2613
- provide: import_nestjs_agent_core4.AGENT_PRICING_STORE,
2701
+ provide: import_nestjs_agent_core5.AGENT_PRICING_STORE,
2614
2702
  useClass: options.pricingStore
2615
2703
  } : {
2616
- provide: import_nestjs_agent_core4.AGENT_PRICING_STORE,
2704
+ provide: import_nestjs_agent_core5.AGENT_PRICING_STORE,
2617
2705
  useExisting: DrizzlePricingStore
2618
2706
  },
2619
2707
  {
2620
2708
  provide: DrizzleGovernanceQueries,
2621
2709
  useFactory: /* @__PURE__ */ __name((pricingStore) => new DrizzleGovernanceQueries(options.db, pricingStore), "useFactory"),
2622
2710
  inject: [
2623
- import_nestjs_agent_core4.AGENT_PRICING_STORE
2711
+ import_nestjs_agent_core5.AGENT_PRICING_STORE
2624
2712
  ]
2625
2713
  },
2626
2714
  {
2627
- provide: import_nestjs_agent_core4.AGENT_GOVERNANCE_QUERIES,
2715
+ provide: import_nestjs_agent_core5.AGENT_GOVERNANCE_QUERIES,
2628
2716
  useExisting: DrizzleGovernanceQueries
2629
2717
  },
2630
2718
  // What makes a `defineConfirmedTool` confirmation single use across replicas.
@@ -2633,7 +2721,7 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2633
2721
  useFactory: /* @__PURE__ */ __name(() => new DrizzleConfirmTokenStore(options.db), "useFactory")
2634
2722
  },
2635
2723
  {
2636
- provide: import_nestjs_agent_core4.AGENT_CONFIRM_TOKEN_STORE,
2724
+ provide: import_nestjs_agent_core5.AGENT_CONFIRM_TOKEN_STORE,
2637
2725
  useExisting: DrizzleConfirmTokenStore
2638
2726
  }
2639
2727
  ],
@@ -2642,14 +2730,14 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2642
2730
  DrizzleRagIngestionLog
2643
2731
  ],
2644
2732
  DrizzleAgentStore,
2645
- import_nestjs_agent_core4.AGENT_STORE,
2733
+ import_nestjs_agent_core5.AGENT_STORE,
2646
2734
  DrizzleMemoryProvider,
2647
2735
  DrizzleGovernanceQueries,
2648
- import_nestjs_agent_core4.AGENT_GOVERNANCE_QUERIES,
2736
+ import_nestjs_agent_core5.AGENT_GOVERNANCE_QUERIES,
2649
2737
  DrizzlePricingStore,
2650
- import_nestjs_agent_core4.AGENT_PRICING_STORE,
2738
+ import_nestjs_agent_core5.AGENT_PRICING_STORE,
2651
2739
  DrizzleConfirmTokenStore,
2652
- import_nestjs_agent_core4.AGENT_CONFIRM_TOKEN_STORE
2740
+ import_nestjs_agent_core5.AGENT_CONFIRM_TOKEN_STORE
2653
2741
  ]
2654
2742
  };
2655
2743
  }
@@ -2666,6 +2754,8 @@ DrizzleAgentStoreModule = _ts_decorate2([
2666
2754
  DrizzleMemoryProvider,
2667
2755
  DrizzlePricingStore,
2668
2756
  DrizzleRagIngestionLog,
2757
+ DrizzleStreamFrameTable,
2758
+ DrizzleTokenStreamSink,
2669
2759
  RAG_INGESTION_LOG_PAGE_ORDER,
2670
2760
  agentConfirmToken,
2671
2761
  agentMemory,
@@ -2674,6 +2764,7 @@ DrizzleAgentStoreModule = _ts_decorate2([
2674
2764
  agentQueuedMessage,
2675
2765
  agentRun,
2676
2766
  agentSchema,
2767
+ agentStreamFrame,
2677
2768
  agentThread,
2678
2769
  agentTokenUsage,
2679
2770
  agentToolCall,