@dudousxd/nestjs-agent-store-drizzle 0.25.0 → 0.27.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/README.md CHANGED
@@ -17,7 +17,8 @@ pnpm add @dudousxd/nestjs-agent-store-drizzle drizzle-orm
17
17
 
18
18
  The host app owns the connection and passes in an already-opened Drizzle handle — this module never
19
19
  opens one itself. **SQLite, Postgres and MySQL** all work — CI runs every store suite on
20
- better-sqlite3, node-postgres (Postgres 16) and mysql2 (MySQL 8.4), and the other Drizzle drivers of
20
+ better-sqlite3, node-postgres (Postgres 16) and mysql2 (MySQL 8.4), the independent-approval suites
21
+ also on postgres.js, and the other Drizzle drivers of
21
22
  each dialect speak the same query builder. Build the handle with the schema object for your dialect;
22
23
  every store reads the dialect off the handle.
23
24
 
@@ -146,42 +147,49 @@ EXISTS`, and an explicit add-column pass for columns this package introduced aft
146
147
  since the `CREATE TABLE` guard is inert against a table that already exists. Calling it on boot is
147
148
  enough; it never drops a column or changes a type.
148
149
 
149
- On your own drizzle-kit migrations, those additive statements are yours to write:
150
+ On your own drizzle-kit migrations, those additive statements are yours to write. The simplest way
151
+ to keep them right is to let drizzle-kit write them: re-export the package's schema for your dialect
152
+ (`agentSchema`, `pgAgentSchema` or `mysqlAgentSchema`) from the schema file drizzle-kit reads, and
153
+ `drizzle-kit generate` diffs the agent tables like your own. `agentSchemaDdl(dialect)` returns the
154
+ complete target DDL (tables and indexes) if you would rather compare by hand.
155
+
156
+ For reference, on SQLite, the columns added to the original chat tables since they first shipped
157
+ (all nullable; adjust the types to your dialect):
150
158
 
151
159
  ```sql
152
160
  ALTER TABLE agent_thread ADD COLUMN default_agent TEXT;
153
- ALTER TABLE agent_message ADD COLUMN run_id TEXT;
161
+ ALTER TABLE agent_thread ADD COLUMN model TEXT;
162
+ ALTER TABLE agent_thread ADD COLUMN queue_pause TEXT;
154
163
  ALTER TABLE agent_message ADD COLUMN attachments TEXT;
155
- ALTER TABLE agent_run ADD COLUMN parent_run_id TEXT;
164
+ ALTER TABLE agent_message ADD COLUMN agent_name TEXT;
165
+ ALTER TABLE agent_message ADD COLUMN run_id TEXT;
156
166
  ALTER TABLE agent_message ADD COLUMN reasoning TEXT;
157
167
  ALTER TABLE agent_message ADD COLUMN reasoning_ms INTEGER;
158
168
  ALTER TABLE agent_message ADD COLUMN ui TEXT;
159
169
  ALTER TABLE agent_message ADD COLUMN feedback TEXT;
160
- ALTER TABLE agent_thread ADD COLUMN model TEXT;
170
+ ALTER TABLE agent_message ADD COLUMN seq INTEGER;
171
+ ALTER TABLE agent_message ADD COLUMN action_proposal_outcome TEXT;
172
+ ALTER TABLE agent_tool_call ADD COLUMN run_id TEXT;
173
+ ALTER TABLE agent_tool_call ADD COLUMN confirmation TEXT;
161
174
  ALTER TABLE agent_tool_call ADD COLUMN approver TEXT;
162
175
  ALTER TABLE agent_tool_call ADD COLUMN expires_at INTEGER;
163
176
  ALTER TABLE agent_tool_call ADD COLUMN remember INTEGER;
164
177
  ALTER TABLE agent_tool_call ADD COLUMN decided_via TEXT;
178
+ ALTER TABLE agent_tool_call ADD COLUMN proposal_id TEXT;
179
+ ALTER TABLE agent_token_usage ADD COLUMN cache_write_tokens INTEGER;
180
+ ALTER TABLE agent_token_usage ADD COLUMN cache_read_tokens INTEGER;
181
+ ALTER TABLE agent_token_usage ADD COLUMN cost_usd REAL;
182
+ ALTER TABLE agent_model_pricing ADD COLUMN cache_write_price_per_1m REAL;
183
+ ALTER TABLE agent_model_pricing ADD COLUMN cache_read_price_per_1m REAL;
165
184
  CREATE INDEX agent_tool_call_message_idx ON agent_tool_call (message_id);
166
- -- the chat message queue
167
- ALTER TABLE agent_thread ADD COLUMN queue_pause TEXT;
168
- CREATE TABLE agent_queued_message (
169
- id TEXT PRIMARY KEY NOT NULL,
170
- thread_id TEXT NOT NULL REFERENCES agent_thread(id) ON DELETE CASCADE,
171
- actor TEXT NOT NULL,
172
- content TEXT NOT NULL,
173
- attachments TEXT,
174
- agent_name TEXT,
175
- model TEXT,
176
- page_context TEXT,
177
- interrupt INTEGER NOT NULL DEFAULT 0,
178
- position INTEGER NOT NULL,
179
- created_at INTEGER NOT NULL,
180
- updated_at INTEGER NOT NULL
181
- );
182
- CREATE INDEX agent_queued_message_thread_position_idx ON agent_queued_message (thread_id, position);
183
185
  ```
184
186
 
187
+ Tables added since then are new tables, not alterations. Take their `CREATE TABLE` and
188
+ `CREATE INDEX` statements from `agentSchemaDdl(dialect)`: `agent_queued_message` (the chat message
189
+ queue), `agent_run`, `agent_memory`, `agent_stream_frame`, `agent_confirm_token`,
190
+ `rag_ingestion_log` and `agent_action_proposal`. Later columns on those tables (for example the
191
+ proposal discovery and delivery columns described below) are added the same way.
192
+
185
193
  The index is not optional on Postgres: `agent_tool_call.message_id` carries a foreign key, and
186
194
  Postgres — unlike MySQL — does not index one for you, while both message-scoped reads (the thread
187
195
  reader's `IN (…)` and `truncateFrom`'s delete) filter on it.
@@ -234,17 +242,14 @@ a gateway-reported `costUsd` wins when present, otherwise a cache-aware estimate
234
242
  the input rate). See the [root README](https://github.com/DavideCarvalho/nestjs-agent#cost--governance)
235
243
  for the full model.
236
244
 
237
- ## License
238
-
239
- MIT © Davide Carvalho
240
-
245
+ ## Action confirmations
241
246
 
242
247
  Action preflight confirmations persist in the nullable `agent_tool_call.confirmation` JSON column.
243
248
  The schema helper adds this column to existing databases. Hosts managing their own migrations must
244
249
  add it before upgrading (`JSONB` on PostgreSQL, `JSON` on MySQL, JSON text on SQLite). The stored
245
250
  confirmation is returned in `StoredMessage.approvals` so a reload uses the same wording.
246
251
 
247
- ### Durable action proposals
252
+ ## Durable action proposals
248
253
 
249
254
  `DrizzleAgentStore` also implements the optional core `ActionProposalStore` capability.
250
255
  Its `createActionProposal`, scoped get/list, immutable decision, lease claim/renewal,
@@ -308,11 +313,11 @@ tools. This upgrade does not support mixed old and new writers: old writers cann
308
313
  maintain the new indexes. Discovery deliberately excludes rows awaiting backfill;
309
314
  it never performs an implicit, unbounded migration.
310
315
 
311
- ### Independent approval runtime
316
+ ## Independent approval runtime
312
317
 
313
318
  `actionApprovalMode: 'independent'` requires proposal discovery, atomic replacement,
314
319
  terminal outcome admission and the thread queue on the same database. This adapter
315
- advertises admission support for better-sqlite3, node-postgres, mysql2 and libSQL;
320
+ advertises admission support for better-sqlite3, node-postgres, postgres.js, mysql2 and libSQL;
316
321
  other Drizzle drivers retain blocking behavior and fail independent configuration.
317
322
  Synchronous SQLite transaction callbacks complete synchronously, including both
318
323
  assistant insertion and the delivery compare-and-set. PostgreSQL/MySQL admission
@@ -334,3 +339,7 @@ effects remain at least once; use the stable tool-context idempotency key.
334
339
 
335
340
  See [the shared setup and rollout guide](../../docs/independent-approvals.md) for
336
341
  policies, text/channel decisions, remembered approvals and component capabilities.
342
+
343
+ ## License
344
+
345
+ MIT © Davide Carvalho
package/dist/index.cjs CHANGED
@@ -185,6 +185,9 @@ var agentQueuedMessage = (0, import_sqlite_core.sqliteTable)("agent_queued_messa
185
185
  uiCapabilities: (0, import_sqlite_core.text)("ui_capabilities", {
186
186
  mode: "json"
187
187
  }).$type(),
188
+ hostContext: (0, import_sqlite_core.text)("host_context", {
189
+ mode: "json"
190
+ }).$type(),
188
191
  id: (0, import_sqlite_core.text)("id").primaryKey(),
189
192
  threadId: (0, import_sqlite_core.text)("thread_id").notNull().references(() => agentThread.id, {
190
193
  onDelete: "cascade"
@@ -496,6 +499,7 @@ var agentToolCall2 = (0, import_mysql_core.mysqlTable)("agent_tool_call", {
496
499
  ]);
497
500
  var agentQueuedMessage2 = (0, import_mysql_core.mysqlTable)("agent_queued_message", {
498
501
  uiCapabilities: (0, import_mysql_core.json)("ui_capabilities").$type(),
502
+ hostContext: (0, import_mysql_core.json)("host_context").$type(),
499
503
  id: key("id").primaryKey(),
500
504
  threadId: key("thread_id").notNull().references(() => agentThread2.id, {
501
505
  onDelete: "cascade"
@@ -763,6 +767,7 @@ var agentToolCall3 = (0, import_pg_core.pgTable)("agent_tool_call", {
763
767
  ]);
764
768
  var agentQueuedMessage3 = (0, import_pg_core.pgTable)("agent_queued_message", {
765
769
  uiCapabilities: (0, import_pg_core.jsonb)("ui_capabilities").$type(),
770
+ hostContext: (0, import_pg_core.jsonb)("host_context").$type(),
766
771
  id: (0, import_pg_core.text)("id").primaryKey(),
767
772
  threadId: (0, import_pg_core.text)("thread_id").notNull().references(() => agentThread3.id, {
768
773
  onDelete: "cascade"
@@ -1201,6 +1206,8 @@ function actionProposalTransactionMode(kind) {
1201
1206
  if (kind !== void 0 && [
1202
1207
  "NodePgDatabase",
1203
1208
  "NodePgTransaction",
1209
+ "PostgresJsDatabase",
1210
+ "PostgresJsTransaction",
1204
1211
  "MySql2Database",
1205
1212
  "MySql2Transaction",
1206
1213
  "LibSQLDatabase",
@@ -2119,6 +2126,7 @@ var DrizzleAgentStore = class {
2119
2126
  actor: input.actor,
2120
2127
  content: input.content,
2121
2128
  uiCapabilities: input.uiCapabilities ?? null,
2129
+ hostContext: input.hostContext ?? null,
2122
2130
  attachments: input.attachments !== void 0 && input.attachments.length > 0 ? input.attachments : null,
2123
2131
  agentName: input.agentName ?? null,
2124
2132
  persona: input.persona ?? null,
@@ -2586,6 +2594,9 @@ function toQueuedMessage(row) {
2586
2594
  ...row.uiCapabilities !== null ? {
2587
2595
  uiCapabilities: row.uiCapabilities
2588
2596
  } : {},
2597
+ ...row.hostContext != null ? {
2598
+ hostContext: row.hostContext
2599
+ } : {},
2589
2600
  id: row.id,
2590
2601
  threadId: row.threadId,
2591
2602
  actor: row.actor,