@dudousxd/nestjs-agent-store-drizzle 0.27.0 → 0.28.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
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  DrizzleActionProposalStore: () => DrizzleActionProposalStore,
25
25
  DrizzleAgentStore: () => DrizzleAgentStore,
26
26
  DrizzleAgentStoreModule: () => DrizzleAgentStoreModule,
27
+ DrizzleChannelStore: () => DrizzleChannelStore,
27
28
  DrizzleConfirmTokenStore: () => DrizzleConfirmTokenStore,
28
29
  DrizzleGovernanceQueries: () => DrizzleGovernanceQueries,
29
30
  DrizzleMemoryProvider: () => DrizzleMemoryProvider,
@@ -33,6 +34,7 @@ __export(index_exports, {
33
34
  DrizzleTokenStreamSink: () => DrizzleTokenStreamSink,
34
35
  RAG_INGESTION_LOG_PAGE_ORDER: () => RAG_INGESTION_LOG_PAGE_ORDER,
35
36
  agentActionProposal: () => agentActionProposal,
37
+ agentChannelState: () => agentChannelState,
36
38
  agentConfirmToken: () => agentConfirmToken,
37
39
  agentDialectOf: () => agentDialectOf,
38
40
  agentMemory: () => agentMemory,
@@ -342,6 +344,14 @@ var ragIngestionLog = (0, import_sqlite_core.sqliteTable)("rag_ingestion_log", {
342
344
  }, (table) => [
343
345
  (0, import_sqlite_core.index)("rag_ingestion_log_collection_idx").on(table.collection, table.updatedAt)
344
346
  ]);
347
+ var agentChannelState = (0, import_sqlite_core.sqliteTable)("agent_channel_state", {
348
+ key: (0, import_sqlite_core.text)("key").primaryKey(),
349
+ value: (0, import_sqlite_core.text)("value"),
350
+ expiresAt: (0, import_sqlite_core.integer)("expires_at").notNull(),
351
+ createdAt: (0, import_sqlite_core.integer)("created_at").notNull()
352
+ }, (table) => [
353
+ (0, import_sqlite_core.index)("agent_channel_state_expires_idx").on(table.expiresAt)
354
+ ]);
345
355
  var agentConfirmToken = (0, import_sqlite_core.sqliteTable)("agent_confirm_token", {
346
356
  hash: (0, import_sqlite_core.text)("hash").primaryKey(),
347
357
  actorRef: (0, import_sqlite_core.text)("actor_ref").notNull(),
@@ -404,6 +414,7 @@ var agentSchema = {
404
414
  agentMemory,
405
415
  ragIngestionLog,
406
416
  agentConfirmToken,
417
+ agentChannelState,
407
418
  agentStreamFrame
408
419
  };
409
420
 
@@ -600,6 +611,18 @@ var ragIngestionLog2 = (0, import_mysql_core.mysqlTable)("rag_ingestion_log", {
600
611
  }, (table) => [
601
612
  (0, import_mysql_core.index)("rag_ingestion_log_collection_idx").on(table.collection, table.updatedAt)
602
613
  ]);
614
+ var agentChannelState2 = (0, import_mysql_core.mysqlTable)("agent_channel_state", {
615
+ key: key("key").primaryKey(),
616
+ value: (0, import_mysql_core.longtext)("value"),
617
+ expiresAt: (0, import_mysql_core.bigint)("expires_at", {
618
+ mode: "number"
619
+ }).notNull(),
620
+ createdAt: (0, import_mysql_core.bigint)("created_at", {
621
+ mode: "number"
622
+ }).notNull()
623
+ }, (table) => [
624
+ (0, import_mysql_core.index)("agent_channel_state_expires_idx").on(table.expiresAt)
625
+ ]);
603
626
  var agentConfirmToken2 = (0, import_mysql_core.mysqlTable)("agent_confirm_token", {
604
627
  hash: key("hash").primaryKey(),
605
628
  actorRef: key("actor_ref").notNull(),
@@ -685,6 +708,7 @@ var mysqlAgentSchema = {
685
708
  agentMemory: agentMemory2,
686
709
  ragIngestionLog: ragIngestionLog2,
687
710
  agentConfirmToken: agentConfirmToken2,
711
+ agentChannelState: agentChannelState2,
688
712
  agentStreamFrame: agentStreamFrame2
689
713
  };
690
714
 
@@ -868,6 +892,18 @@ var ragIngestionLog3 = (0, import_pg_core.pgTable)("rag_ingestion_log", {
868
892
  }, (table) => [
869
893
  (0, import_pg_core.index)("rag_ingestion_log_collection_idx").on(table.collection, table.updatedAt)
870
894
  ]);
895
+ var agentChannelState3 = (0, import_pg_core.pgTable)("agent_channel_state", {
896
+ key: (0, import_pg_core.text)("key").primaryKey(),
897
+ value: (0, import_pg_core.text)("value"),
898
+ expiresAt: (0, import_pg_core.bigint)("expires_at", {
899
+ mode: "number"
900
+ }).notNull(),
901
+ createdAt: (0, import_pg_core.bigint)("created_at", {
902
+ mode: "number"
903
+ }).notNull()
904
+ }, (table) => [
905
+ (0, import_pg_core.index)("agent_channel_state_expires_idx").on(table.expiresAt)
906
+ ]);
871
907
  var agentConfirmToken3 = (0, import_pg_core.pgTable)("agent_confirm_token", {
872
908
  hash: (0, import_pg_core.text)("hash").primaryKey(),
873
909
  actorRef: (0, import_pg_core.text)("actor_ref").notNull(),
@@ -951,6 +987,7 @@ var pgAgentSchema = {
951
987
  agentMemory: agentMemory3,
952
988
  ragIngestionLog: ragIngestionLog3,
953
989
  agentConfirmToken: agentConfirmToken3,
990
+ agentChannelState: agentChannelState3,
954
991
  agentStreamFrame: agentStreamFrame3
955
992
  };
956
993
 
@@ -3583,10 +3620,82 @@ var DrizzleConfirmTokenStore = class {
3583
3620
  }
3584
3621
  };
3585
3622
 
3623
+ // src/drizzle-channel-store.ts
3624
+ var import_node_crypto2 = require("crypto");
3625
+ var import_drizzle_orm10 = require("drizzle-orm");
3626
+ var channelStateKey = /* @__PURE__ */ __name((key2) => key2.length <= 255 ? key2 : `sha256:${(0, import_node_crypto2.createHash)("sha256").update(key2).digest("hex")}`, "channelStateKey");
3627
+ var DrizzleChannelStore = class {
3628
+ static {
3629
+ __name(this, "DrizzleChannelStore");
3630
+ }
3631
+ db;
3632
+ dialect;
3633
+ t;
3634
+ purgeEveryMs;
3635
+ lastPurge = 0;
3636
+ constructor(db, options = {}) {
3637
+ this.dialect = agentDialectOf(db);
3638
+ this.t = agentTablesFor(this.dialect);
3639
+ this.db = asBuilder(db);
3640
+ this.purgeEveryMs = options.purgeEveryMs ?? 10 * 60 * 1e3;
3641
+ }
3642
+ async claim(key2, ttlMs) {
3643
+ const table = this.t.agentChannelState;
3644
+ const now = Date.now();
3645
+ if (this.purgeEveryMs > 0 && now - this.lastPurge >= this.purgeEveryMs) {
3646
+ this.lastPurge = now;
3647
+ await this.purgeExpired(now);
3648
+ }
3649
+ const id = channelStateKey(key2);
3650
+ await this.db.delete(table).where((0, import_drizzle_orm10.and)((0, import_drizzle_orm10.eq)(table.key, id), (0, import_drizzle_orm10.lte)(table.expiresAt, now)));
3651
+ const row = {
3652
+ key: id,
3653
+ value: null,
3654
+ expiresAt: now + ttlMs,
3655
+ createdAt: now
3656
+ };
3657
+ if (this.dialect === "mysql") {
3658
+ const insert = this.db.insert(table);
3659
+ return mysqlAffectedRows(await insert.ignore().values(row)) === 1;
3660
+ }
3661
+ const inserted = await affectedRows(this.dialect, this.db.insert(table).values(row).onConflictDoNothing(), table.key);
3662
+ return inserted === 1;
3663
+ }
3664
+ async get(key2) {
3665
+ const table = this.t.agentChannelState;
3666
+ const rows = await this.db.select({
3667
+ value: table.value
3668
+ }).from(table).where((0, import_drizzle_orm10.and)((0, import_drizzle_orm10.eq)(table.key, channelStateKey(key2)), (0, import_drizzle_orm10.gt)(table.expiresAt, Date.now())));
3669
+ const value = rows[0]?.value;
3670
+ return typeof value === "string" ? value : null;
3671
+ }
3672
+ async set(key2, value, ttlMs) {
3673
+ const table = this.t.agentChannelState;
3674
+ const now = Date.now();
3675
+ await upsert(this.dialect, this.db.insert(table).values({
3676
+ key: channelStateKey(key2),
3677
+ value,
3678
+ expiresAt: now + ttlMs,
3679
+ createdAt: now
3680
+ }), table.key, {
3681
+ value,
3682
+ expiresAt: now + ttlMs
3683
+ });
3684
+ }
3685
+ async delete(key2) {
3686
+ const table = this.t.agentChannelState;
3687
+ await this.db.delete(table).where((0, import_drizzle_orm10.eq)(table.key, channelStateKey(key2)));
3688
+ }
3689
+ async purgeExpired(now = Date.now()) {
3690
+ const table = this.t.agentChannelState;
3691
+ return affectedRows(this.dialect, this.db.delete(table).where((0, import_drizzle_orm10.lte)(table.expiresAt, now)), table.key);
3692
+ }
3693
+ };
3694
+
3586
3695
  // src/drizzle-rag-ingestion-log.ts
3587
3696
  var import_node_diagnostics_channel = require("diagnostics_channel");
3588
3697
  var import_common = require("@nestjs/common");
3589
- var import_drizzle_orm10 = require("drizzle-orm");
3698
+ var import_drizzle_orm11 = require("drizzle-orm");
3590
3699
  function _ts_decorate(decorators, target, key2, desc4) {
3591
3700
  var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key2) : desc4, d;
3592
3701
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
@@ -3634,8 +3743,8 @@ function num(value) {
3634
3743
  }
3635
3744
  __name(num, "num");
3636
3745
  var RAG_INGESTION_LOG_PAGE_ORDER = [
3637
- (0, import_drizzle_orm10.desc)(ragIngestionLog.updatedAt),
3638
- (0, import_drizzle_orm10.asc)(ragIngestionLog.documentId)
3746
+ (0, import_drizzle_orm11.desc)(ragIngestionLog.updatedAt),
3747
+ (0, import_drizzle_orm11.asc)(ragIngestionLog.documentId)
3639
3748
  ];
3640
3749
  var DEFAULT_PAGE_SIZE = 200;
3641
3750
  var DEFAULT_BATCH_SIZE = 200;
@@ -3706,7 +3815,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
3706
3815
  */
3707
3816
  async listPage(query = {}) {
3708
3817
  const [totalRow] = await this.db.select({
3709
- value: (0, import_drizzle_orm10.count)()
3818
+ value: (0, import_drizzle_orm11.count)()
3710
3819
  }).from(this.t.ragIngestionLog).where(this.where(query));
3711
3820
  return {
3712
3821
  rows: await this.list(query),
@@ -3785,17 +3894,17 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
3785
3894
  }
3786
3895
  /** The latest recorded outcome for one document, or null if it was never attempted. */
3787
3896
  async get(documentId) {
3788
- const [row] = await this.db.select().from(this.t.ragIngestionLog).where((0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.documentId, documentId));
3897
+ const [row] = await this.db.select().from(this.t.ragIngestionLog).where((0, import_drizzle_orm11.eq)(this.t.ragIngestionLog.documentId, documentId));
3789
3898
  return row ?? null;
3790
3899
  }
3791
3900
  /** Forget one document's record. Returns whether a row was actually removed. */
3792
3901
  async remove(documentId) {
3793
- const removed = await affectedRows(this.dialect, this.db.delete(this.t.ragIngestionLog).where((0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.documentId, documentId)), this.t.ragIngestionLog.documentId);
3902
+ const removed = await affectedRows(this.dialect, this.db.delete(this.t.ragIngestionLog).where((0, import_drizzle_orm11.eq)(this.t.ragIngestionLog.documentId, documentId)), this.t.ragIngestionLog.documentId);
3794
3903
  return removed > 0;
3795
3904
  }
3796
3905
  /** Forget every record for a collection — for when the collection itself is deleted. */
3797
3906
  async removeByCollection(collection) {
3798
- const removed = await affectedRows(this.dialect, this.db.delete(this.t.ragIngestionLog).where((0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.collection, collection)), this.t.ragIngestionLog.documentId);
3907
+ const removed = await affectedRows(this.dialect, this.db.delete(this.t.ragIngestionLog).where((0, import_drizzle_orm11.eq)(this.t.ragIngestionLog.collection, collection)), this.t.ragIngestionLog.documentId);
3799
3908
  return removed;
3800
3909
  }
3801
3910
  /**
@@ -3825,7 +3934,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
3825
3934
  }
3826
3935
  }
3827
3936
  where(query) {
3828
- return (0, import_drizzle_orm10.and)(query.collection !== void 0 ? (0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.collection, query.collection) : void 0, query.status !== void 0 ? (0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.status, query.status) : void 0);
3937
+ return (0, import_drizzle_orm11.and)(query.collection !== void 0 ? (0, import_drizzle_orm11.eq)(this.t.ragIngestionLog.collection, query.collection) : void 0, query.status !== void 0 ? (0, import_drizzle_orm11.eq)(this.t.ragIngestionLog.status, query.status) : void 0);
3829
3938
  }
3830
3939
  /**
3831
3940
  * The coordinates filter AND'd with "strictly after `cursor` in
@@ -3840,7 +3949,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
3840
3949
  if (cursor === void 0) {
3841
3950
  return base;
3842
3951
  }
3843
- return (0, import_drizzle_orm10.and)(base, (0, import_drizzle_orm10.or)((0, import_drizzle_orm10.lt)(this.t.ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm10.and)((0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm10.gt)(this.t.ragIngestionLog.documentId, cursor.documentId))));
3952
+ return (0, import_drizzle_orm11.and)(base, (0, import_drizzle_orm11.or)((0, import_drizzle_orm11.lt)(this.t.ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm11.and)((0, import_drizzle_orm11.eq)(this.t.ragIngestionLog.updatedAt, cursor.updatedAt), (0, import_drizzle_orm11.gt)(this.t.ragIngestionLog.documentId, cursor.documentId))));
3844
3953
  }
3845
3954
  /**
3846
3955
  * Upsert one outcome. Keyed by document id so the row always reflects the *current* state — a
@@ -3992,6 +4101,15 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
3992
4101
  {
3993
4102
  provide: import_nestjs_agent_core8.AGENT_CONFIRM_TOKEN_STORE,
3994
4103
  useExisting: DrizzleConfirmTokenStore
4104
+ },
4105
+ // Text channels' state (`@dudousxd/nestjs-agent-channels`), shared by every replica.
4106
+ {
4107
+ provide: DrizzleChannelStore,
4108
+ useFactory: /* @__PURE__ */ __name(() => new DrizzleChannelStore(options.db), "useFactory")
4109
+ },
4110
+ {
4111
+ provide: import_nestjs_agent_core8.AGENT_CHANNEL_STORE,
4112
+ useExisting: DrizzleChannelStore
3995
4113
  }
3996
4114
  ],
3997
4115
  exports: [
@@ -4006,7 +4124,9 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
4006
4124
  DrizzlePricingStore,
4007
4125
  import_nestjs_agent_core8.AGENT_PRICING_STORE,
4008
4126
  DrizzleConfirmTokenStore,
4009
- import_nestjs_agent_core8.AGENT_CONFIRM_TOKEN_STORE
4127
+ import_nestjs_agent_core8.AGENT_CONFIRM_TOKEN_STORE,
4128
+ DrizzleChannelStore,
4129
+ import_nestjs_agent_core8.AGENT_CHANNEL_STORE
4010
4130
  ]
4011
4131
  };
4012
4132
  }
@@ -4019,6 +4139,7 @@ DrizzleAgentStoreModule = _ts_decorate2([
4019
4139
  DrizzleActionProposalStore,
4020
4140
  DrizzleAgentStore,
4021
4141
  DrizzleAgentStoreModule,
4142
+ DrizzleChannelStore,
4022
4143
  DrizzleConfirmTokenStore,
4023
4144
  DrizzleGovernanceQueries,
4024
4145
  DrizzleMemoryProvider,
@@ -4028,6 +4149,7 @@ DrizzleAgentStoreModule = _ts_decorate2([
4028
4149
  DrizzleTokenStreamSink,
4029
4150
  RAG_INGESTION_LOG_PAGE_ORDER,
4030
4151
  agentActionProposal,
4152
+ agentChannelState,
4031
4153
  agentConfirmToken,
4032
4154
  agentDialectOf,
4033
4155
  agentMemory,