@dudousxd/nestjs-agent-store-drizzle 0.20.2 → 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
@@ -23,17 +23,22 @@ var index_exports = {};
23
23
  __export(index_exports, {
24
24
  DrizzleAgentStore: () => DrizzleAgentStore,
25
25
  DrizzleAgentStoreModule: () => DrizzleAgentStoreModule,
26
+ DrizzleConfirmTokenStore: () => DrizzleConfirmTokenStore,
26
27
  DrizzleGovernanceQueries: () => DrizzleGovernanceQueries,
27
28
  DrizzleMemoryProvider: () => DrizzleMemoryProvider,
28
29
  DrizzlePricingStore: () => DrizzlePricingStore,
29
30
  DrizzleRagIngestionLog: () => DrizzleRagIngestionLog,
31
+ DrizzleStreamFrameTable: () => DrizzleStreamFrameTable,
32
+ DrizzleTokenStreamSink: () => DrizzleTokenStreamSink,
30
33
  RAG_INGESTION_LOG_PAGE_ORDER: () => RAG_INGESTION_LOG_PAGE_ORDER,
34
+ agentConfirmToken: () => agentConfirmToken,
31
35
  agentMemory: () => agentMemory,
32
36
  agentMessage: () => agentMessage,
33
37
  agentModelPricing: () => agentModelPricing,
34
38
  agentQueuedMessage: () => agentQueuedMessage,
35
39
  agentRun: () => agentRun,
36
40
  agentSchema: () => agentSchema,
41
+ agentStreamFrame: () => agentStreamFrame,
37
42
  agentThread: () => agentThread,
38
43
  agentTokenUsage: () => agentTokenUsage,
39
44
  agentToolCall: () => agentToolCall,
@@ -307,6 +312,29 @@ var ragIngestionLog = (0, import_sqlite_core.sqliteTable)("rag_ingestion_log", {
307
312
  }, (table) => [
308
313
  (0, import_sqlite_core.index)("rag_ingestion_log_collection_idx").on(table.collection, table.updatedAt)
309
314
  ]);
315
+ var agentConfirmToken = (0, import_sqlite_core.sqliteTable)("agent_confirm_token", {
316
+ hash: (0, import_sqlite_core.text)("hash").primaryKey(),
317
+ actorRef: (0, import_sqlite_core.text)("actor_ref").notNull(),
318
+ tool: (0, import_sqlite_core.text)("tool").notNull(),
319
+ expiresAt: (0, import_sqlite_core.integer)("expires_at").notNull(),
320
+ createdAt: (0, import_sqlite_core.integer)("created_at").notNull()
321
+ }, (table) => [
322
+ (0, import_sqlite_core.index)("agent_confirm_token_expires_idx").on(table.expiresAt)
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
+ ]);
310
338
  var agentSchema = {
311
339
  agentThread,
312
340
  agentMessage,
@@ -316,7 +344,8 @@ var agentSchema = {
316
344
  agentModelPricing,
317
345
  agentRun,
318
346
  agentMemory,
319
- ragIngestionLog
347
+ ragIngestionLog,
348
+ agentConfirmToken
320
349
  };
321
350
 
322
351
  // src/ensure-schema.ts
@@ -471,7 +500,24 @@ var statements = [
471
500
  updated_at INTEGER NOT NULL
472
501
  )`,
473
502
  `CREATE INDEX IF NOT EXISTS rag_ingestion_log_collection_idx
474
- ON rag_ingestion_log (collection, updated_at)`
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
+ )`,
512
+ `CREATE TABLE IF NOT EXISTS agent_confirm_token (
513
+ hash TEXT PRIMARY KEY NOT NULL,
514
+ actor_ref TEXT NOT NULL,
515
+ tool TEXT NOT NULL,
516
+ expires_at INTEGER NOT NULL,
517
+ created_at INTEGER NOT NULL
518
+ )`,
519
+ `CREATE INDEX IF NOT EXISTS agent_confirm_token_expires_idx
520
+ ON agent_confirm_token (expires_at)`
475
521
  ];
476
522
  var additiveColumns = [
477
523
  {
@@ -2172,10 +2218,106 @@ var DrizzlePricingStore = class {
2172
2218
  }
2173
2219
  };
2174
2220
 
2221
+ // src/drizzle-token-stream-sink.ts
2222
+ var import_nestjs_agent_core4 = require("@dudousxd/nestjs-agent-core");
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");
2286
+ var DrizzleConfirmTokenStore = class {
2287
+ static {
2288
+ __name(this, "DrizzleConfirmTokenStore");
2289
+ }
2290
+ db;
2291
+ constructor(db) {
2292
+ this.db = db;
2293
+ }
2294
+ async claim(input) {
2295
+ const inserted = await this.db.insert(agentConfirmToken).values({
2296
+ hash: input.hash,
2297
+ actorRef: input.actorRef,
2298
+ tool: input.tool,
2299
+ expiresAt: input.expiresAt,
2300
+ createdAt: Date.now()
2301
+ }).onConflictDoNothing().returning({
2302
+ hash: agentConfirmToken.hash
2303
+ });
2304
+ return inserted.length === 1;
2305
+ }
2306
+ async release(hash) {
2307
+ await this.db.delete(agentConfirmToken).where((0, import_drizzle_orm7.eq)(agentConfirmToken.hash, hash));
2308
+ }
2309
+ async purgeExpired(now = Date.now()) {
2310
+ const purged = await this.db.delete(agentConfirmToken).where((0, import_drizzle_orm7.lt)(agentConfirmToken.expiresAt, now)).returning({
2311
+ hash: agentConfirmToken.hash
2312
+ });
2313
+ return purged.length;
2314
+ }
2315
+ };
2316
+
2175
2317
  // src/drizzle-rag-ingestion-log.ts
2176
2318
  var import_node_diagnostics_channel = require("diagnostics_channel");
2177
2319
  var import_common = require("@nestjs/common");
2178
- var import_drizzle_orm6 = require("drizzle-orm");
2320
+ var import_drizzle_orm8 = require("drizzle-orm");
2179
2321
  function _ts_decorate(decorators, target, key, desc4) {
2180
2322
  var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key) : desc4, d;
2181
2323
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
@@ -2223,8 +2365,8 @@ function num(value) {
2223
2365
  }
2224
2366
  __name(num, "num");
2225
2367
  var RAG_INGESTION_LOG_PAGE_ORDER = [
2226
- (0, import_drizzle_orm6.desc)(ragIngestionLog.updatedAt),
2227
- (0, import_drizzle_orm6.asc)(ragIngestionLog.documentId)
2368
+ (0, import_drizzle_orm8.desc)(ragIngestionLog.updatedAt),
2369
+ (0, import_drizzle_orm8.asc)(ragIngestionLog.documentId)
2228
2370
  ];
2229
2371
  var DEFAULT_PAGE_SIZE = 200;
2230
2372
  var DEFAULT_BATCH_SIZE = 200;
@@ -2288,7 +2430,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2288
2430
  */
2289
2431
  async listPage(query = {}) {
2290
2432
  const [totalRow] = await this.db.select({
2291
- value: (0, import_drizzle_orm6.count)()
2433
+ value: (0, import_drizzle_orm8.count)()
2292
2434
  }).from(ragIngestionLog).where(this.where(query));
2293
2435
  return {
2294
2436
  rows: await this.list(query),
@@ -2367,19 +2509,19 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2367
2509
  }
2368
2510
  /** The latest recorded outcome for one document, or null if it was never attempted. */
2369
2511
  async get(documentId) {
2370
- const [row] = await this.db.select().from(ragIngestionLog).where((0, import_drizzle_orm6.eq)(ragIngestionLog.documentId, documentId));
2512
+ const [row] = await this.db.select().from(ragIngestionLog).where((0, import_drizzle_orm8.eq)(ragIngestionLog.documentId, documentId));
2371
2513
  return row ?? null;
2372
2514
  }
2373
2515
  /** Forget one document's record. Returns whether a row was actually removed. */
2374
2516
  async remove(documentId) {
2375
- const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm6.eq)(ragIngestionLog.documentId, documentId)).returning({
2517
+ const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm8.eq)(ragIngestionLog.documentId, documentId)).returning({
2376
2518
  documentId: ragIngestionLog.documentId
2377
2519
  });
2378
2520
  return removed.length > 0;
2379
2521
  }
2380
2522
  /** Forget every record for a collection — for when the collection itself is deleted. */
2381
2523
  async removeByCollection(collection) {
2382
- const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm6.eq)(ragIngestionLog.collection, collection)).returning({
2524
+ const removed = await this.db.delete(ragIngestionLog).where((0, import_drizzle_orm8.eq)(ragIngestionLog.collection, collection)).returning({
2383
2525
  documentId: ragIngestionLog.documentId
2384
2526
  });
2385
2527
  return removed.length;
@@ -2411,7 +2553,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2411
2553
  }
2412
2554
  }
2413
2555
  where(query) {
2414
- return (0, import_drizzle_orm6.and)(query.collection !== void 0 ? (0, import_drizzle_orm6.eq)(ragIngestionLog.collection, query.collection) : void 0, query.status !== void 0 ? (0, import_drizzle_orm6.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);
2415
2557
  }
2416
2558
  /**
2417
2559
  * The coordinates filter AND'd with "strictly after `cursor` in
@@ -2426,7 +2568,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
2426
2568
  if (cursor === void 0) {
2427
2569
  return base;
2428
2570
  }
2429
- return (0, import_drizzle_orm6.and)(base, (0, import_drizzle_orm6.or)((0, import_drizzle_orm6.lt)(ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm6.and)((0, import_drizzle_orm6.eq)(ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm6.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))));
2430
2572
  }
2431
2573
  /**
2432
2574
  * Upsert one outcome. Keyed by document id so the row always reflects the *current* state — a
@@ -2502,7 +2644,7 @@ DrizzleRagIngestionLog = _ts_decorate([
2502
2644
  ], DrizzleRagIngestionLog);
2503
2645
 
2504
2646
  // src/drizzle-agent-store.module.ts
2505
- var import_nestjs_agent_core4 = require("@dudousxd/nestjs-agent-core");
2647
+ var import_nestjs_agent_core5 = require("@dudousxd/nestjs-agent-core");
2506
2648
  var import_common2 = require("@nestjs/common");
2507
2649
  function _ts_decorate2(decorators, target, key, desc4) {
2508
2650
  var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key) : desc4, d;
@@ -2539,7 +2681,7 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2539
2681
  useFactory: /* @__PURE__ */ __name(() => new DrizzleAgentStore(options.db), "useFactory")
2540
2682
  },
2541
2683
  {
2542
- provide: import_nestjs_agent_core4.AGENT_STORE,
2684
+ provide: import_nestjs_agent_core5.AGENT_STORE,
2543
2685
  useExisting: DrizzleAgentStore
2544
2686
  },
2545
2687
  // Exported, never bound to AGENT_MEMORY: that token's PRESENCE is what turns memory on, so
@@ -2556,22 +2698,31 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2556
2698
  // AGENT_PRICING_STORE: the host's own store when supplied, else the default Drizzle one.
2557
2699
  // DrizzleGovernanceQueries prices usage against whatever is bound here.
2558
2700
  options.pricingStore ? {
2559
- provide: import_nestjs_agent_core4.AGENT_PRICING_STORE,
2701
+ provide: import_nestjs_agent_core5.AGENT_PRICING_STORE,
2560
2702
  useClass: options.pricingStore
2561
2703
  } : {
2562
- provide: import_nestjs_agent_core4.AGENT_PRICING_STORE,
2704
+ provide: import_nestjs_agent_core5.AGENT_PRICING_STORE,
2563
2705
  useExisting: DrizzlePricingStore
2564
2706
  },
2565
2707
  {
2566
2708
  provide: DrizzleGovernanceQueries,
2567
2709
  useFactory: /* @__PURE__ */ __name((pricingStore) => new DrizzleGovernanceQueries(options.db, pricingStore), "useFactory"),
2568
2710
  inject: [
2569
- import_nestjs_agent_core4.AGENT_PRICING_STORE
2711
+ import_nestjs_agent_core5.AGENT_PRICING_STORE
2570
2712
  ]
2571
2713
  },
2572
2714
  {
2573
- provide: import_nestjs_agent_core4.AGENT_GOVERNANCE_QUERIES,
2715
+ provide: import_nestjs_agent_core5.AGENT_GOVERNANCE_QUERIES,
2574
2716
  useExisting: DrizzleGovernanceQueries
2717
+ },
2718
+ // What makes a `defineConfirmedTool` confirmation single use across replicas.
2719
+ {
2720
+ provide: DrizzleConfirmTokenStore,
2721
+ useFactory: /* @__PURE__ */ __name(() => new DrizzleConfirmTokenStore(options.db), "useFactory")
2722
+ },
2723
+ {
2724
+ provide: import_nestjs_agent_core5.AGENT_CONFIRM_TOKEN_STORE,
2725
+ useExisting: DrizzleConfirmTokenStore
2575
2726
  }
2576
2727
  ],
2577
2728
  exports: [
@@ -2579,12 +2730,14 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
2579
2730
  DrizzleRagIngestionLog
2580
2731
  ],
2581
2732
  DrizzleAgentStore,
2582
- import_nestjs_agent_core4.AGENT_STORE,
2733
+ import_nestjs_agent_core5.AGENT_STORE,
2583
2734
  DrizzleMemoryProvider,
2584
2735
  DrizzleGovernanceQueries,
2585
- import_nestjs_agent_core4.AGENT_GOVERNANCE_QUERIES,
2736
+ import_nestjs_agent_core5.AGENT_GOVERNANCE_QUERIES,
2586
2737
  DrizzlePricingStore,
2587
- import_nestjs_agent_core4.AGENT_PRICING_STORE
2738
+ import_nestjs_agent_core5.AGENT_PRICING_STORE,
2739
+ DrizzleConfirmTokenStore,
2740
+ import_nestjs_agent_core5.AGENT_CONFIRM_TOKEN_STORE
2588
2741
  ]
2589
2742
  };
2590
2743
  }
@@ -2596,17 +2749,22 @@ DrizzleAgentStoreModule = _ts_decorate2([
2596
2749
  0 && (module.exports = {
2597
2750
  DrizzleAgentStore,
2598
2751
  DrizzleAgentStoreModule,
2752
+ DrizzleConfirmTokenStore,
2599
2753
  DrizzleGovernanceQueries,
2600
2754
  DrizzleMemoryProvider,
2601
2755
  DrizzlePricingStore,
2602
2756
  DrizzleRagIngestionLog,
2757
+ DrizzleStreamFrameTable,
2758
+ DrizzleTokenStreamSink,
2603
2759
  RAG_INGESTION_LOG_PAGE_ORDER,
2760
+ agentConfirmToken,
2604
2761
  agentMemory,
2605
2762
  agentMessage,
2606
2763
  agentModelPricing,
2607
2764
  agentQueuedMessage,
2608
2765
  agentRun,
2609
2766
  agentSchema,
2767
+ agentStreamFrame,
2610
2768
  agentThread,
2611
2769
  agentTokenUsage,
2612
2770
  agentToolCall,