@dudousxd/nestjs-agent-store-drizzle 0.23.0 → 0.25.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 +145 -9
- package/dist/index.cjs +1847 -636
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8086 -1744
- package/dist/index.d.ts +8086 -1744
- package/dist/index.js +1800 -595
- package/dist/index.js.map +1 -1
- package/package.json +7 -4
package/dist/index.cjs
CHANGED
|
@@ -10,9 +10,9 @@ var __export = (target, all) => {
|
|
|
10
10
|
};
|
|
11
11
|
var __copyProps = (to, from, except, desc4) => {
|
|
12
12
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let
|
|
14
|
-
if (!__hasOwnProp.call(to,
|
|
15
|
-
__defProp(to,
|
|
13
|
+
for (let key2 of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key2) && key2 !== except)
|
|
15
|
+
__defProp(to, key2, { get: () => from[key2], enumerable: !(desc4 = __getOwnPropDesc(from, key2)) || desc4.enumerable });
|
|
16
16
|
}
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// src/index.ts
|
|
22
22
|
var index_exports = {};
|
|
23
23
|
__export(index_exports, {
|
|
24
|
+
DrizzleActionProposalStore: () => DrizzleActionProposalStore,
|
|
24
25
|
DrizzleAgentStore: () => DrizzleAgentStore,
|
|
25
26
|
DrizzleAgentStoreModule: () => DrizzleAgentStoreModule,
|
|
26
27
|
DrizzleConfirmTokenStore: () => DrizzleConfirmTokenStore,
|
|
@@ -31,18 +32,23 @@ __export(index_exports, {
|
|
|
31
32
|
DrizzleStreamFrameTable: () => DrizzleStreamFrameTable,
|
|
32
33
|
DrizzleTokenStreamSink: () => DrizzleTokenStreamSink,
|
|
33
34
|
RAG_INGESTION_LOG_PAGE_ORDER: () => RAG_INGESTION_LOG_PAGE_ORDER,
|
|
35
|
+
agentActionProposal: () => agentActionProposal,
|
|
34
36
|
agentConfirmToken: () => agentConfirmToken,
|
|
37
|
+
agentDialectOf: () => agentDialectOf,
|
|
35
38
|
agentMemory: () => agentMemory,
|
|
36
39
|
agentMessage: () => agentMessage,
|
|
37
40
|
agentModelPricing: () => agentModelPricing,
|
|
38
41
|
agentQueuedMessage: () => agentQueuedMessage,
|
|
39
42
|
agentRun: () => agentRun,
|
|
40
43
|
agentSchema: () => agentSchema,
|
|
44
|
+
agentSchemaDdl: () => agentSchemaDdl,
|
|
41
45
|
agentStreamFrame: () => agentStreamFrame,
|
|
42
46
|
agentThread: () => agentThread,
|
|
43
47
|
agentTokenUsage: () => agentTokenUsage,
|
|
44
48
|
agentToolCall: () => agentToolCall,
|
|
45
49
|
ensureAgentSchema: () => ensureAgentSchema,
|
|
50
|
+
mysqlAgentSchema: () => mysqlAgentSchema,
|
|
51
|
+
pgAgentSchema: () => pgAgentSchema,
|
|
46
52
|
ragIngestionLog: () => ragIngestionLog
|
|
47
53
|
});
|
|
48
54
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -86,6 +92,7 @@ var agentMessage = (0, import_sqlite_core.sqliteTable)("agent_message", {
|
|
|
86
92
|
onDelete: "cascade"
|
|
87
93
|
}),
|
|
88
94
|
role: (0, import_sqlite_core.text)("role").$type().notNull(),
|
|
95
|
+
actionProposalOutcome: (0, import_sqlite_core.text)("action_proposal_outcome"),
|
|
89
96
|
content: (0, import_sqlite_core.text)("content").notNull(),
|
|
90
97
|
toolCalls: (0, import_sqlite_core.text)("tool_calls", {
|
|
91
98
|
mode: "json"
|
|
@@ -117,6 +124,12 @@ var agentMessage = (0, import_sqlite_core.sqliteTable)("agent_message", {
|
|
|
117
124
|
feedback: (0, import_sqlite_core.text)("feedback", {
|
|
118
125
|
mode: "json"
|
|
119
126
|
}).$type(),
|
|
127
|
+
/**
|
|
128
|
+
* The message's place in its thread, 1-based, assigned on append — what orders a transcript.
|
|
129
|
+
* `created_at` cannot: two messages of one turn routinely share a timestamp, and the uuid that
|
|
130
|
+
* broke the tie is random. `null` on a row from before the column; those sort first.
|
|
131
|
+
*/
|
|
132
|
+
seq: (0, import_sqlite_core.integer)("seq"),
|
|
120
133
|
createdAt: (0, import_sqlite_core.integer)("created_at", {
|
|
121
134
|
mode: "timestamp_ms"
|
|
122
135
|
}).notNull()
|
|
@@ -124,6 +137,7 @@ var agentMessage = (0, import_sqlite_core.sqliteTable)("agent_message", {
|
|
|
124
137
|
(0, import_sqlite_core.index)("agent_message_thread_created_idx").on(table.threadId, table.createdAt)
|
|
125
138
|
]);
|
|
126
139
|
var agentToolCall = (0, import_sqlite_core.sqliteTable)("agent_tool_call", {
|
|
140
|
+
proposalId: (0, import_sqlite_core.text)("proposal_id"),
|
|
127
141
|
id: (0, import_sqlite_core.text)("id").primaryKey(),
|
|
128
142
|
messageId: (0, import_sqlite_core.text)("message_id").notNull().references(() => agentMessage.id, {
|
|
129
143
|
onDelete: "cascade"
|
|
@@ -148,6 +162,10 @@ var agentToolCall = (0, import_sqlite_core.sqliteTable)("agent_tool_call", {
|
|
|
148
162
|
}),
|
|
149
163
|
/** The run (turn) this call belongs to, for a trace deep-link; null for a pre-rollout row. */
|
|
150
164
|
runId: (0, import_sqlite_core.text)("run_id"),
|
|
165
|
+
/** Resolved per-call wording; null falls back to presentation templates. */
|
|
166
|
+
confirmation: (0, import_sqlite_core.text)("confirmation", {
|
|
167
|
+
mode: "json"
|
|
168
|
+
}).$type(),
|
|
151
169
|
/** Who had to approve it (`requester` or a role); null for a call no policy put to anyone. */
|
|
152
170
|
approver: (0, import_sqlite_core.text)("approver"),
|
|
153
171
|
/** When the approval request lapses; null → never. */
|
|
@@ -164,6 +182,9 @@ var agentToolCall = (0, import_sqlite_core.sqliteTable)("agent_tool_call", {
|
|
|
164
182
|
(0, import_sqlite_core.index)("agent_tool_call_message_idx").on(table.messageId)
|
|
165
183
|
]);
|
|
166
184
|
var agentQueuedMessage = (0, import_sqlite_core.sqliteTable)("agent_queued_message", {
|
|
185
|
+
uiCapabilities: (0, import_sqlite_core.text)("ui_capabilities", {
|
|
186
|
+
mode: "json"
|
|
187
|
+
}).$type(),
|
|
167
188
|
id: (0, import_sqlite_core.text)("id").primaryKey(),
|
|
168
189
|
threadId: (0, import_sqlite_core.text)("thread_id").notNull().references(() => agentThread.id, {
|
|
169
190
|
onDelete: "cascade"
|
|
@@ -341,7 +362,35 @@ var agentStreamFrame = (0, import_sqlite_core.sqliteTable)("agent_stream_frame",
|
|
|
341
362
|
]
|
|
342
363
|
})
|
|
343
364
|
]);
|
|
365
|
+
var agentActionProposal = (0, import_sqlite_core.sqliteTable)("agent_action_proposal", {
|
|
366
|
+
id: (0, import_sqlite_core.text)("id").primaryKey(),
|
|
367
|
+
scopeKey: (0, import_sqlite_core.text)("scope_key").notNull(),
|
|
368
|
+
decision: (0, import_sqlite_core.text)("decision").$type().notNull(),
|
|
369
|
+
logicalSort: (0, import_sqlite_core.text)("logical_sort").notNull(),
|
|
370
|
+
createFingerprint: (0, import_sqlite_core.text)("create_fingerprint").notNull(),
|
|
371
|
+
proposal: (0, import_sqlite_core.text)("proposal", {
|
|
372
|
+
mode: "json"
|
|
373
|
+
}).$type().notNull(),
|
|
374
|
+
version: (0, import_sqlite_core.integer)("version").notNull().default(0),
|
|
375
|
+
executionStatus: (0, import_sqlite_core.text)("execution_status").$type(),
|
|
376
|
+
leaseExpiresAt: (0, import_sqlite_core.integer)("lease_expires_at"),
|
|
377
|
+
deliveryStatus: (0, import_sqlite_core.text)("delivery_status"),
|
|
378
|
+
deliveryLeaseExpiresAt: (0, import_sqlite_core.integer)("delivery_lease_expires_at"),
|
|
379
|
+
replacementGroupKey: (0, import_sqlite_core.text)("replacement_group_key"),
|
|
380
|
+
outcomeIdKey: (0, import_sqlite_core.text)("outcome_id_key"),
|
|
381
|
+
proposalExpiresAt: (0, import_sqlite_core.integer)("proposal_expires_at"),
|
|
382
|
+
discoveryIndexVersion: (0, import_sqlite_core.integer)("discovery_index_version").notNull().default(0),
|
|
383
|
+
createdAt: (0, import_sqlite_core.integer)("created_at").notNull()
|
|
384
|
+
}, (table) => [
|
|
385
|
+
(0, import_sqlite_core.index)("agent_action_proposal_execution_idx").on(table.discoveryIndexVersion, table.executionStatus, table.leaseExpiresAt, table.createdAt),
|
|
386
|
+
(0, import_sqlite_core.index)("agent_action_proposal_expiry_idx").on(table.discoveryIndexVersion, table.decision, table.proposalExpiresAt, table.createdAt),
|
|
387
|
+
(0, import_sqlite_core.index)("agent_action_proposal_replacement_idx").on(table.scopeKey, table.replacementGroupKey, table.decision),
|
|
388
|
+
(0, import_sqlite_core.index)("agent_action_proposal_delivery_idx").on(table.deliveryStatus, table.deliveryLeaseExpiresAt, table.createdAt),
|
|
389
|
+
(0, import_sqlite_core.uniqueIndex)("agent_action_proposal_outcome_idx").on(table.outcomeIdKey),
|
|
390
|
+
(0, import_sqlite_core.index)("agent_action_proposal_scope_idx").on(table.scopeKey, table.createdAt, table.id)
|
|
391
|
+
]);
|
|
344
392
|
var agentSchema = {
|
|
393
|
+
agentActionProposal,
|
|
345
394
|
agentThread,
|
|
346
395
|
agentMessage,
|
|
347
396
|
agentQueuedMessage,
|
|
@@ -351,303 +400,1375 @@ var agentSchema = {
|
|
|
351
400
|
agentRun,
|
|
352
401
|
agentMemory,
|
|
353
402
|
ragIngestionLog,
|
|
354
|
-
agentConfirmToken
|
|
403
|
+
agentConfirmToken,
|
|
404
|
+
agentStreamFrame
|
|
355
405
|
};
|
|
356
406
|
|
|
357
407
|
// src/ensure-schema.ts
|
|
408
|
+
var import_drizzle_orm2 = require("drizzle-orm");
|
|
409
|
+
var import_mysql_core3 = require("drizzle-orm/mysql-core");
|
|
410
|
+
var import_pg_core3 = require("drizzle-orm/pg-core");
|
|
411
|
+
var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
|
|
412
|
+
|
|
413
|
+
// src/dialect.ts
|
|
358
414
|
var import_drizzle_orm = require("drizzle-orm");
|
|
359
|
-
var
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
)
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
)
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
)
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
)
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
)
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
)
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
)
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
)
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
)
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
{
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
{
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
},
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
},
|
|
602
|
-
{
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
415
|
+
var import_mysql_core2 = require("drizzle-orm/mysql-core");
|
|
416
|
+
var import_pg_core2 = require("drizzle-orm/pg-core");
|
|
417
|
+
|
|
418
|
+
// src/schema-mysql.ts
|
|
419
|
+
var import_nestjs_agent_core = require("@dudousxd/nestjs-agent-core");
|
|
420
|
+
var import_mysql_core = require("drizzle-orm/mysql-core");
|
|
421
|
+
var ts = /* @__PURE__ */ __name((name) => (0, import_mysql_core.datetime)(name, {
|
|
422
|
+
mode: "date",
|
|
423
|
+
fsp: 3
|
|
424
|
+
}), "ts");
|
|
425
|
+
var key = /* @__PURE__ */ __name((name) => (0, import_mysql_core.varchar)(name, {
|
|
426
|
+
length: 255
|
|
427
|
+
}), "key");
|
|
428
|
+
var agentThread2 = (0, import_mysql_core.mysqlTable)("agent_thread", {
|
|
429
|
+
id: key("id").primaryKey(),
|
|
430
|
+
actorRef: key("actor_ref").notNull(),
|
|
431
|
+
tenantRef: key("tenant_ref"),
|
|
432
|
+
title: key("title").notNull(),
|
|
433
|
+
transient: (0, import_mysql_core.boolean)("transient").notNull().default(false),
|
|
434
|
+
activeStreamId: key("active_stream_id"),
|
|
435
|
+
defaultAgent: key("default_agent"),
|
|
436
|
+
model: key("model"),
|
|
437
|
+
/** The persona a send on this thread runs under when it names none; `null` → the agent's default. */
|
|
438
|
+
persona: key("persona"),
|
|
439
|
+
queuePause: (0, import_mysql_core.json)("queue_pause").$type(),
|
|
440
|
+
createdAt: ts("created_at").notNull(),
|
|
441
|
+
updatedAt: ts("updated_at").notNull(),
|
|
442
|
+
deletedAt: ts("deleted_at")
|
|
443
|
+
}, (table) => [
|
|
444
|
+
(0, import_mysql_core.index)("agent_thread_actor_updated_idx").on(table.actorRef, table.updatedAt)
|
|
445
|
+
]);
|
|
446
|
+
var agentMessage2 = (0, import_mysql_core.mysqlTable)("agent_message", {
|
|
447
|
+
id: key("id").primaryKey(),
|
|
448
|
+
threadId: key("thread_id").notNull().references(() => agentThread2.id, {
|
|
449
|
+
onDelete: "cascade"
|
|
450
|
+
}),
|
|
451
|
+
role: key("role").$type().notNull(),
|
|
452
|
+
actionProposalOutcome: (0, import_mysql_core.longtext)("action_proposal_outcome"),
|
|
453
|
+
content: (0, import_mysql_core.longtext)("content").notNull(),
|
|
454
|
+
toolCalls: (0, import_mysql_core.json)("tool_calls").$type(),
|
|
455
|
+
toolResults: (0, import_mysql_core.json)("tool_results").$type(),
|
|
456
|
+
attachments: (0, import_mysql_core.json)("attachments").$type(),
|
|
457
|
+
followUps: (0, import_mysql_core.json)("follow_ups").$type(),
|
|
458
|
+
usage: (0, import_mysql_core.json)("usage").$type(),
|
|
459
|
+
agentName: key("agent_name"),
|
|
460
|
+
/** The persona the turn that wrote this message ran under; `null` → none. */
|
|
461
|
+
persona: key("persona"),
|
|
462
|
+
runId: key("run_id"),
|
|
463
|
+
reasoning: (0, import_mysql_core.longtext)("reasoning"),
|
|
464
|
+
reasoningMs: (0, import_mysql_core.int)("reasoning_ms"),
|
|
465
|
+
ui: (0, import_mysql_core.json)("ui").$type(),
|
|
466
|
+
feedback: (0, import_mysql_core.json)("feedback").$type(),
|
|
467
|
+
seq: (0, import_mysql_core.int)("seq"),
|
|
468
|
+
createdAt: ts("created_at").notNull()
|
|
469
|
+
}, (table) => [
|
|
470
|
+
(0, import_mysql_core.index)("agent_message_thread_created_idx").on(table.threadId, table.createdAt)
|
|
471
|
+
]);
|
|
472
|
+
var agentToolCall2 = (0, import_mysql_core.mysqlTable)("agent_tool_call", {
|
|
473
|
+
proposalId: key("proposal_id"),
|
|
474
|
+
id: key("id").primaryKey(),
|
|
475
|
+
messageId: key("message_id").notNull().references(() => agentMessage2.id, {
|
|
476
|
+
onDelete: "cascade"
|
|
477
|
+
}),
|
|
478
|
+
toolName: key("tool_name").notNull(),
|
|
479
|
+
toolType: key("tool_type").$type().notNull(),
|
|
480
|
+
input: (0, import_mysql_core.json)("input"),
|
|
481
|
+
output: (0, import_mysql_core.json)("output"),
|
|
482
|
+
status: key("status").$type().notNull(),
|
|
483
|
+
executedByRef: key("executed_by_ref"),
|
|
484
|
+
executionMs: (0, import_mysql_core.int)("execution_ms"),
|
|
485
|
+
error: (0, import_mysql_core.longtext)("error"),
|
|
486
|
+
createdAt: ts("created_at").notNull(),
|
|
487
|
+
executedAt: ts("executed_at"),
|
|
488
|
+
runId: key("run_id"),
|
|
489
|
+
confirmation: (0, import_mysql_core.json)("confirmation").$type(),
|
|
490
|
+
approver: key("approver"),
|
|
491
|
+
expiresAt: ts("expires_at"),
|
|
492
|
+
remember: (0, import_mysql_core.boolean)("remember"),
|
|
493
|
+
decidedVia: key("decided_via")
|
|
494
|
+
}, (table) => [
|
|
495
|
+
(0, import_mysql_core.index)("agent_tool_call_message_idx").on(table.messageId)
|
|
496
|
+
]);
|
|
497
|
+
var agentQueuedMessage2 = (0, import_mysql_core.mysqlTable)("agent_queued_message", {
|
|
498
|
+
uiCapabilities: (0, import_mysql_core.json)("ui_capabilities").$type(),
|
|
499
|
+
id: key("id").primaryKey(),
|
|
500
|
+
threadId: key("thread_id").notNull().references(() => agentThread2.id, {
|
|
501
|
+
onDelete: "cascade"
|
|
502
|
+
}),
|
|
503
|
+
actor: (0, import_mysql_core.json)("actor").$type().notNull(),
|
|
504
|
+
content: (0, import_mysql_core.longtext)("content").notNull(),
|
|
505
|
+
attachments: (0, import_mysql_core.json)("attachments").$type(),
|
|
506
|
+
agentName: key("agent_name"),
|
|
507
|
+
/** The persona the send resolved — what the message starts under. */
|
|
508
|
+
persona: key("persona"),
|
|
509
|
+
model: key("model"),
|
|
510
|
+
pageContext: (0, import_mysql_core.json)("page_context").$type(),
|
|
511
|
+
interrupt: (0, import_mysql_core.boolean)("interrupt").notNull().default(false),
|
|
512
|
+
position: (0, import_mysql_core.int)("position").notNull(),
|
|
513
|
+
createdAt: ts("created_at").notNull(),
|
|
514
|
+
updatedAt: ts("updated_at").notNull()
|
|
515
|
+
}, (table) => [
|
|
516
|
+
(0, import_mysql_core.index)("agent_queued_message_thread_position_idx").on(table.threadId, table.position)
|
|
517
|
+
]);
|
|
518
|
+
var agentTokenUsage2 = (0, import_mysql_core.mysqlTable)("agent_token_usage", {
|
|
519
|
+
id: key("id").primaryKey(),
|
|
520
|
+
threadId: key("thread_id").notNull().references(() => agentThread2.id, {
|
|
521
|
+
onDelete: "cascade"
|
|
522
|
+
}),
|
|
523
|
+
actorRef: key("actor_ref").notNull(),
|
|
524
|
+
messageId: key("message_id"),
|
|
525
|
+
modelId: key("model_id").notNull(),
|
|
526
|
+
purpose: key("purpose").$type().notNull(),
|
|
527
|
+
inputTokens: (0, import_mysql_core.int)("input_tokens").notNull(),
|
|
528
|
+
outputTokens: (0, import_mysql_core.int)("output_tokens").notNull(),
|
|
529
|
+
cacheWriteTokens: (0, import_mysql_core.int)("cache_write_tokens"),
|
|
530
|
+
cacheReadTokens: (0, import_mysql_core.int)("cache_read_tokens"),
|
|
531
|
+
costUsd: (0, import_mysql_core.double)("cost_usd"),
|
|
532
|
+
createdAt: ts("created_at").notNull()
|
|
533
|
+
}, (table) => [
|
|
534
|
+
(0, import_mysql_core.index)("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt)
|
|
535
|
+
]);
|
|
536
|
+
var agentModelPricing2 = (0, import_mysql_core.mysqlTable)("agent_model_pricing", {
|
|
537
|
+
id: key("id").primaryKey(),
|
|
538
|
+
modelId: key("model_id").notNull(),
|
|
539
|
+
inputPricePer1m: (0, import_mysql_core.double)("input_price_per_1m").notNull(),
|
|
540
|
+
outputPricePer1m: (0, import_mysql_core.double)("output_price_per_1m").notNull(),
|
|
541
|
+
cacheWritePricePer1m: (0, import_mysql_core.double)("cache_write_price_per_1m"),
|
|
542
|
+
cacheReadPricePer1m: (0, import_mysql_core.double)("cache_read_price_per_1m"),
|
|
543
|
+
effectiveFrom: ts("effective_from").notNull(),
|
|
544
|
+
isCurrent: (0, import_mysql_core.boolean)("is_current").notNull()
|
|
545
|
+
});
|
|
546
|
+
var agentRun2 = (0, import_mysql_core.mysqlTable)("agent_run", {
|
|
547
|
+
id: key("id").primaryKey(),
|
|
548
|
+
threadId: key("thread_id").notNull().references(() => agentThread2.id, {
|
|
549
|
+
onDelete: "cascade"
|
|
550
|
+
}),
|
|
551
|
+
actorRef: key("actor_ref").notNull(),
|
|
552
|
+
agentName: key("agent_name"),
|
|
553
|
+
status: key("status").$type().notNull(),
|
|
554
|
+
durationMs: (0, import_mysql_core.int)("duration_ms"),
|
|
555
|
+
errorCode: key("error_code"),
|
|
556
|
+
errorMessage: (0, import_mysql_core.longtext)("error_message"),
|
|
557
|
+
retries: (0, import_mysql_core.int)("retries").notNull().default(0),
|
|
558
|
+
startedAt: ts("started_at").notNull(),
|
|
559
|
+
settledAt: ts("settled_at"),
|
|
560
|
+
promptHash: key("prompt_hash"),
|
|
561
|
+
parentRunId: key("parent_run_id")
|
|
562
|
+
}, (table) => [
|
|
563
|
+
(0, import_mysql_core.index)("agent_run_started_idx").on(table.startedAt)
|
|
564
|
+
]);
|
|
565
|
+
var agentMemory2 = (0, import_mysql_core.mysqlTable)("agent_memory", {
|
|
566
|
+
id: key("id").primaryKey(),
|
|
567
|
+
scope: key("scope").notNull(),
|
|
568
|
+
key: key("key").notNull(),
|
|
569
|
+
text: (0, import_mysql_core.longtext)("text").notNull(),
|
|
570
|
+
originAuthor: key("origin_author").$type().notNull(),
|
|
571
|
+
originThreadId: key("origin_thread_id"),
|
|
572
|
+
originRunId: key("origin_run_id"),
|
|
573
|
+
originActorRef: key("origin_actor_ref"),
|
|
574
|
+
pinned: (0, import_mysql_core.boolean)("pinned").notNull().default(false),
|
|
575
|
+
createdAt: ts("created_at").notNull(),
|
|
576
|
+
updatedAt: ts("updated_at").notNull()
|
|
577
|
+
}, (table) => [
|
|
578
|
+
(0, import_mysql_core.uniqueIndex)("agent_memory_scope_key_uq").on(table.scope, table.key)
|
|
579
|
+
]);
|
|
580
|
+
var ragIngestionLog2 = (0, import_mysql_core.mysqlTable)("rag_ingestion_log", {
|
|
581
|
+
documentId: key("document_id").primaryKey(),
|
|
582
|
+
status: key("status").$type().notNull(),
|
|
583
|
+
collection: key("collection"),
|
|
584
|
+
ownerType: key("owner_type"),
|
|
585
|
+
ownerId: key("owner_id"),
|
|
586
|
+
source: (0, import_mysql_core.longtext)("source"),
|
|
587
|
+
mimeType: key("mime_type"),
|
|
588
|
+
size: (0, import_mysql_core.bigint)("size", {
|
|
589
|
+
mode: "number"
|
|
590
|
+
}),
|
|
591
|
+
chunks: (0, import_mysql_core.int)("chunks"),
|
|
592
|
+
reason: key("reason"),
|
|
593
|
+
error: (0, import_mysql_core.longtext)("error"),
|
|
594
|
+
createdAt: ts("created_at").notNull(),
|
|
595
|
+
updatedAt: ts("updated_at").notNull()
|
|
596
|
+
}, (table) => [
|
|
597
|
+
(0, import_mysql_core.index)("rag_ingestion_log_collection_idx").on(table.collection, table.updatedAt)
|
|
598
|
+
]);
|
|
599
|
+
var agentConfirmToken2 = (0, import_mysql_core.mysqlTable)("agent_confirm_token", {
|
|
600
|
+
hash: key("hash").primaryKey(),
|
|
601
|
+
actorRef: key("actor_ref").notNull(),
|
|
602
|
+
tool: key("tool").notNull(),
|
|
603
|
+
expiresAt: (0, import_mysql_core.bigint)("expires_at", {
|
|
604
|
+
mode: "number"
|
|
605
|
+
}).notNull(),
|
|
606
|
+
createdAt: (0, import_mysql_core.bigint)("created_at", {
|
|
607
|
+
mode: "number"
|
|
608
|
+
}).notNull()
|
|
609
|
+
}, (table) => [
|
|
610
|
+
(0, import_mysql_core.index)("agent_confirm_token_expires_idx").on(table.expiresAt)
|
|
611
|
+
]);
|
|
612
|
+
var agentStreamFrame2 = (0, import_mysql_core.mysqlTable)("agent_stream_frame", {
|
|
613
|
+
runId: key("run_id").notNull(),
|
|
614
|
+
seq: (0, import_mysql_core.int)("seq").notNull(),
|
|
615
|
+
frame: (0, import_mysql_core.longtext)("frame"),
|
|
616
|
+
error: (0, import_mysql_core.longtext)("error"),
|
|
617
|
+
createdAt: (0, import_mysql_core.bigint)("created_at", {
|
|
618
|
+
mode: "number"
|
|
619
|
+
}).notNull()
|
|
620
|
+
}, (table) => [
|
|
621
|
+
(0, import_mysql_core.primaryKey)({
|
|
622
|
+
columns: [
|
|
623
|
+
table.runId,
|
|
624
|
+
table.seq
|
|
625
|
+
]
|
|
626
|
+
})
|
|
627
|
+
]);
|
|
628
|
+
var proposalJson = (0, import_mysql_core.customType)({
|
|
629
|
+
dataType: /* @__PURE__ */ __name(() => "longtext", "dataType"),
|
|
630
|
+
toDriver: /* @__PURE__ */ __name((value) => (0, import_nestjs_agent_core.canonicalActionProposalJson)(value), "toDriver"),
|
|
631
|
+
fromDriver: /* @__PURE__ */ __name((value) => JSON.parse(value), "fromDriver")
|
|
632
|
+
});
|
|
633
|
+
var agentActionProposal2 = (0, import_mysql_core.mysqlTable)("agent_action_proposal", {
|
|
634
|
+
id: key("id").primaryKey(),
|
|
635
|
+
scopeKey: key("scope_key").notNull(),
|
|
636
|
+
decision: key("decision").$type().notNull(),
|
|
637
|
+
logicalSort: (0, import_mysql_core.varchar)("logical_sort", {
|
|
638
|
+
length: 1020
|
|
639
|
+
}).notNull(),
|
|
640
|
+
createFingerprint: (0, import_mysql_core.longtext)("create_fingerprint").notNull(),
|
|
641
|
+
proposal: proposalJson("proposal").notNull(),
|
|
642
|
+
version: (0, import_mysql_core.bigint)("version", {
|
|
643
|
+
mode: "number"
|
|
644
|
+
}).notNull().default(0),
|
|
645
|
+
executionStatus: key("execution_status").$type(),
|
|
646
|
+
leaseExpiresAt: (0, import_mysql_core.bigint)("lease_expires_at", {
|
|
647
|
+
mode: "number"
|
|
648
|
+
}),
|
|
649
|
+
deliveryStatus: key("delivery_status"),
|
|
650
|
+
deliveryLeaseExpiresAt: (0, import_mysql_core.bigint)("delivery_lease_expires_at", {
|
|
651
|
+
mode: "number"
|
|
652
|
+
}),
|
|
653
|
+
replacementGroupKey: key("replacement_group_key"),
|
|
654
|
+
outcomeIdKey: key("outcome_id_key"),
|
|
655
|
+
proposalExpiresAt: (0, import_mysql_core.bigint)("proposal_expires_at", {
|
|
656
|
+
mode: "number"
|
|
657
|
+
}),
|
|
658
|
+
discoveryIndexVersion: (0, import_mysql_core.bigint)("discovery_index_version", {
|
|
659
|
+
mode: "number"
|
|
660
|
+
}).notNull().default(0),
|
|
661
|
+
createdAt: (0, import_mysql_core.bigint)("created_at", {
|
|
662
|
+
mode: "number"
|
|
663
|
+
}).notNull()
|
|
664
|
+
}, (table) => [
|
|
665
|
+
(0, import_mysql_core.index)("agent_action_proposal_execution_idx").on(table.discoveryIndexVersion, table.executionStatus, table.leaseExpiresAt, table.createdAt),
|
|
666
|
+
(0, import_mysql_core.index)("agent_action_proposal_expiry_idx").on(table.discoveryIndexVersion, table.decision, table.proposalExpiresAt, table.createdAt),
|
|
667
|
+
(0, import_mysql_core.index)("agent_action_proposal_replacement_idx").on(table.scopeKey, table.replacementGroupKey, table.decision),
|
|
668
|
+
(0, import_mysql_core.index)("agent_action_proposal_delivery_idx").on(table.deliveryStatus, table.deliveryLeaseExpiresAt, table.createdAt),
|
|
669
|
+
(0, import_mysql_core.uniqueIndex)("agent_action_proposal_outcome_idx").on(table.outcomeIdKey),
|
|
670
|
+
(0, import_mysql_core.index)("agent_action_proposal_scope_idx").on(table.scopeKey, table.createdAt, table.id)
|
|
671
|
+
]);
|
|
672
|
+
var mysqlAgentSchema = {
|
|
673
|
+
agentActionProposal: agentActionProposal2,
|
|
674
|
+
agentThread: agentThread2,
|
|
675
|
+
agentMessage: agentMessage2,
|
|
676
|
+
agentQueuedMessage: agentQueuedMessage2,
|
|
677
|
+
agentToolCall: agentToolCall2,
|
|
678
|
+
agentTokenUsage: agentTokenUsage2,
|
|
679
|
+
agentModelPricing: agentModelPricing2,
|
|
680
|
+
agentRun: agentRun2,
|
|
681
|
+
agentMemory: agentMemory2,
|
|
682
|
+
ragIngestionLog: ragIngestionLog2,
|
|
683
|
+
agentConfirmToken: agentConfirmToken2,
|
|
684
|
+
agentStreamFrame: agentStreamFrame2
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
// src/schema-pg.ts
|
|
688
|
+
var import_nestjs_agent_core2 = require("@dudousxd/nestjs-agent-core");
|
|
689
|
+
var import_pg_core = require("drizzle-orm/pg-core");
|
|
690
|
+
var ts2 = /* @__PURE__ */ __name((name) => (0, import_pg_core.timestamp)(name, {
|
|
691
|
+
withTimezone: true,
|
|
692
|
+
precision: 3,
|
|
693
|
+
mode: "date"
|
|
694
|
+
}), "ts");
|
|
695
|
+
var agentThread3 = (0, import_pg_core.pgTable)("agent_thread", {
|
|
696
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
697
|
+
actorRef: (0, import_pg_core.text)("actor_ref").notNull(),
|
|
698
|
+
tenantRef: (0, import_pg_core.text)("tenant_ref"),
|
|
699
|
+
title: (0, import_pg_core.text)("title").notNull(),
|
|
700
|
+
transient: (0, import_pg_core.boolean)("transient").notNull().default(false),
|
|
701
|
+
activeStreamId: (0, import_pg_core.text)("active_stream_id"),
|
|
702
|
+
defaultAgent: (0, import_pg_core.text)("default_agent"),
|
|
703
|
+
model: (0, import_pg_core.text)("model"),
|
|
704
|
+
/** The persona a send on this thread runs under when it names none; `null` → the agent's default. */
|
|
705
|
+
persona: (0, import_pg_core.text)("persona"),
|
|
706
|
+
queuePause: (0, import_pg_core.jsonb)("queue_pause").$type(),
|
|
707
|
+
createdAt: ts2("created_at").notNull(),
|
|
708
|
+
updatedAt: ts2("updated_at").notNull(),
|
|
709
|
+
deletedAt: ts2("deleted_at")
|
|
710
|
+
}, (table) => [
|
|
711
|
+
(0, import_pg_core.index)("agent_thread_actor_updated_idx").on(table.actorRef, table.updatedAt)
|
|
712
|
+
]);
|
|
713
|
+
var agentMessage3 = (0, import_pg_core.pgTable)("agent_message", {
|
|
714
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
715
|
+
threadId: (0, import_pg_core.text)("thread_id").notNull().references(() => agentThread3.id, {
|
|
716
|
+
onDelete: "cascade"
|
|
717
|
+
}),
|
|
718
|
+
role: (0, import_pg_core.text)("role").$type().notNull(),
|
|
719
|
+
actionProposalOutcome: (0, import_pg_core.text)("action_proposal_outcome"),
|
|
720
|
+
content: (0, import_pg_core.text)("content").notNull(),
|
|
721
|
+
toolCalls: (0, import_pg_core.jsonb)("tool_calls").$type(),
|
|
722
|
+
toolResults: (0, import_pg_core.jsonb)("tool_results").$type(),
|
|
723
|
+
attachments: (0, import_pg_core.jsonb)("attachments").$type(),
|
|
724
|
+
followUps: (0, import_pg_core.jsonb)("follow_ups").$type(),
|
|
725
|
+
usage: (0, import_pg_core.jsonb)("usage").$type(),
|
|
726
|
+
agentName: (0, import_pg_core.text)("agent_name"),
|
|
727
|
+
/** The persona the turn that wrote this message ran under; `null` → none. */
|
|
728
|
+
persona: (0, import_pg_core.text)("persona"),
|
|
729
|
+
runId: (0, import_pg_core.text)("run_id"),
|
|
730
|
+
reasoning: (0, import_pg_core.text)("reasoning"),
|
|
731
|
+
reasoningMs: (0, import_pg_core.integer)("reasoning_ms"),
|
|
732
|
+
ui: (0, import_pg_core.jsonb)("ui").$type(),
|
|
733
|
+
feedback: (0, import_pg_core.jsonb)("feedback").$type(),
|
|
734
|
+
seq: (0, import_pg_core.integer)("seq"),
|
|
735
|
+
createdAt: ts2("created_at").notNull()
|
|
736
|
+
}, (table) => [
|
|
737
|
+
(0, import_pg_core.index)("agent_message_thread_created_idx").on(table.threadId, table.createdAt)
|
|
738
|
+
]);
|
|
739
|
+
var agentToolCall3 = (0, import_pg_core.pgTable)("agent_tool_call", {
|
|
740
|
+
proposalId: (0, import_pg_core.text)("proposal_id"),
|
|
741
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
742
|
+
messageId: (0, import_pg_core.text)("message_id").notNull().references(() => agentMessage3.id, {
|
|
743
|
+
onDelete: "cascade"
|
|
744
|
+
}),
|
|
745
|
+
toolName: (0, import_pg_core.text)("tool_name").notNull(),
|
|
746
|
+
toolType: (0, import_pg_core.text)("tool_type").$type().notNull(),
|
|
747
|
+
input: (0, import_pg_core.jsonb)("input"),
|
|
748
|
+
output: (0, import_pg_core.jsonb)("output"),
|
|
749
|
+
status: (0, import_pg_core.text)("status").$type().notNull(),
|
|
750
|
+
executedByRef: (0, import_pg_core.text)("executed_by_ref"),
|
|
751
|
+
executionMs: (0, import_pg_core.integer)("execution_ms"),
|
|
752
|
+
error: (0, import_pg_core.text)("error"),
|
|
753
|
+
createdAt: ts2("created_at").notNull(),
|
|
754
|
+
executedAt: ts2("executed_at"),
|
|
755
|
+
runId: (0, import_pg_core.text)("run_id"),
|
|
756
|
+
confirmation: (0, import_pg_core.jsonb)("confirmation").$type(),
|
|
757
|
+
approver: (0, import_pg_core.text)("approver"),
|
|
758
|
+
expiresAt: ts2("expires_at"),
|
|
759
|
+
remember: (0, import_pg_core.boolean)("remember"),
|
|
760
|
+
decidedVia: (0, import_pg_core.text)("decided_via")
|
|
761
|
+
}, (table) => [
|
|
762
|
+
(0, import_pg_core.index)("agent_tool_call_message_idx").on(table.messageId)
|
|
763
|
+
]);
|
|
764
|
+
var agentQueuedMessage3 = (0, import_pg_core.pgTable)("agent_queued_message", {
|
|
765
|
+
uiCapabilities: (0, import_pg_core.jsonb)("ui_capabilities").$type(),
|
|
766
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
767
|
+
threadId: (0, import_pg_core.text)("thread_id").notNull().references(() => agentThread3.id, {
|
|
768
|
+
onDelete: "cascade"
|
|
769
|
+
}),
|
|
770
|
+
actor: (0, import_pg_core.jsonb)("actor").$type().notNull(),
|
|
771
|
+
content: (0, import_pg_core.text)("content").notNull(),
|
|
772
|
+
attachments: (0, import_pg_core.jsonb)("attachments").$type(),
|
|
773
|
+
agentName: (0, import_pg_core.text)("agent_name"),
|
|
774
|
+
/** The persona the send resolved — what the message starts under. */
|
|
775
|
+
persona: (0, import_pg_core.text)("persona"),
|
|
776
|
+
model: (0, import_pg_core.text)("model"),
|
|
777
|
+
pageContext: (0, import_pg_core.jsonb)("page_context").$type(),
|
|
778
|
+
interrupt: (0, import_pg_core.boolean)("interrupt").notNull().default(false),
|
|
779
|
+
position: (0, import_pg_core.integer)("position").notNull(),
|
|
780
|
+
createdAt: ts2("created_at").notNull(),
|
|
781
|
+
updatedAt: ts2("updated_at").notNull()
|
|
782
|
+
}, (table) => [
|
|
783
|
+
(0, import_pg_core.index)("agent_queued_message_thread_position_idx").on(table.threadId, table.position)
|
|
784
|
+
]);
|
|
785
|
+
var agentTokenUsage3 = (0, import_pg_core.pgTable)("agent_token_usage", {
|
|
786
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
787
|
+
threadId: (0, import_pg_core.text)("thread_id").notNull().references(() => agentThread3.id, {
|
|
788
|
+
onDelete: "cascade"
|
|
789
|
+
}),
|
|
790
|
+
actorRef: (0, import_pg_core.text)("actor_ref").notNull(),
|
|
791
|
+
messageId: (0, import_pg_core.text)("message_id"),
|
|
792
|
+
modelId: (0, import_pg_core.text)("model_id").notNull(),
|
|
793
|
+
purpose: (0, import_pg_core.text)("purpose").$type().notNull(),
|
|
794
|
+
inputTokens: (0, import_pg_core.integer)("input_tokens").notNull(),
|
|
795
|
+
outputTokens: (0, import_pg_core.integer)("output_tokens").notNull(),
|
|
796
|
+
cacheWriteTokens: (0, import_pg_core.integer)("cache_write_tokens"),
|
|
797
|
+
cacheReadTokens: (0, import_pg_core.integer)("cache_read_tokens"),
|
|
798
|
+
costUsd: (0, import_pg_core.doublePrecision)("cost_usd"),
|
|
799
|
+
createdAt: ts2("created_at").notNull()
|
|
800
|
+
}, (table) => [
|
|
801
|
+
(0, import_pg_core.index)("agent_token_usage_actor_created_idx").on(table.actorRef, table.createdAt)
|
|
802
|
+
]);
|
|
803
|
+
var agentModelPricing3 = (0, import_pg_core.pgTable)("agent_model_pricing", {
|
|
804
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
805
|
+
modelId: (0, import_pg_core.text)("model_id").notNull(),
|
|
806
|
+
inputPricePer1m: (0, import_pg_core.doublePrecision)("input_price_per_1m").notNull(),
|
|
807
|
+
outputPricePer1m: (0, import_pg_core.doublePrecision)("output_price_per_1m").notNull(),
|
|
808
|
+
cacheWritePricePer1m: (0, import_pg_core.doublePrecision)("cache_write_price_per_1m"),
|
|
809
|
+
cacheReadPricePer1m: (0, import_pg_core.doublePrecision)("cache_read_price_per_1m"),
|
|
810
|
+
effectiveFrom: ts2("effective_from").notNull(),
|
|
811
|
+
isCurrent: (0, import_pg_core.boolean)("is_current").notNull()
|
|
812
|
+
});
|
|
813
|
+
var agentRun3 = (0, import_pg_core.pgTable)("agent_run", {
|
|
814
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
815
|
+
threadId: (0, import_pg_core.text)("thread_id").notNull().references(() => agentThread3.id, {
|
|
816
|
+
onDelete: "cascade"
|
|
817
|
+
}),
|
|
818
|
+
actorRef: (0, import_pg_core.text)("actor_ref").notNull(),
|
|
819
|
+
agentName: (0, import_pg_core.text)("agent_name"),
|
|
820
|
+
status: (0, import_pg_core.text)("status").$type().notNull(),
|
|
821
|
+
durationMs: (0, import_pg_core.integer)("duration_ms"),
|
|
822
|
+
errorCode: (0, import_pg_core.text)("error_code"),
|
|
823
|
+
errorMessage: (0, import_pg_core.text)("error_message"),
|
|
824
|
+
retries: (0, import_pg_core.integer)("retries").notNull().default(0),
|
|
825
|
+
startedAt: ts2("started_at").notNull(),
|
|
826
|
+
settledAt: ts2("settled_at"),
|
|
827
|
+
promptHash: (0, import_pg_core.text)("prompt_hash"),
|
|
828
|
+
parentRunId: (0, import_pg_core.text)("parent_run_id")
|
|
829
|
+
}, (table) => [
|
|
830
|
+
(0, import_pg_core.index)("agent_run_started_idx").on(table.startedAt)
|
|
831
|
+
]);
|
|
832
|
+
var agentMemory3 = (0, import_pg_core.pgTable)("agent_memory", {
|
|
833
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
834
|
+
scope: (0, import_pg_core.text)("scope").notNull(),
|
|
835
|
+
key: (0, import_pg_core.text)("key").notNull(),
|
|
836
|
+
text: (0, import_pg_core.text)("text").notNull(),
|
|
837
|
+
originAuthor: (0, import_pg_core.text)("origin_author").$type().notNull(),
|
|
838
|
+
originThreadId: (0, import_pg_core.text)("origin_thread_id"),
|
|
839
|
+
originRunId: (0, import_pg_core.text)("origin_run_id"),
|
|
840
|
+
originActorRef: (0, import_pg_core.text)("origin_actor_ref"),
|
|
841
|
+
pinned: (0, import_pg_core.boolean)("pinned").notNull().default(false),
|
|
842
|
+
createdAt: ts2("created_at").notNull(),
|
|
843
|
+
updatedAt: ts2("updated_at").notNull()
|
|
844
|
+
}, (table) => [
|
|
845
|
+
(0, import_pg_core.uniqueIndex)("agent_memory_scope_key_uq").on(table.scope, table.key)
|
|
846
|
+
]);
|
|
847
|
+
var ragIngestionLog3 = (0, import_pg_core.pgTable)("rag_ingestion_log", {
|
|
848
|
+
documentId: (0, import_pg_core.text)("document_id").primaryKey(),
|
|
849
|
+
status: (0, import_pg_core.text)("status").$type().notNull(),
|
|
850
|
+
collection: (0, import_pg_core.text)("collection"),
|
|
851
|
+
ownerType: (0, import_pg_core.text)("owner_type"),
|
|
852
|
+
ownerId: (0, import_pg_core.text)("owner_id"),
|
|
853
|
+
source: (0, import_pg_core.text)("source"),
|
|
854
|
+
mimeType: (0, import_pg_core.text)("mime_type"),
|
|
855
|
+
size: (0, import_pg_core.bigint)("size", {
|
|
856
|
+
mode: "number"
|
|
857
|
+
}),
|
|
858
|
+
chunks: (0, import_pg_core.integer)("chunks"),
|
|
859
|
+
reason: (0, import_pg_core.text)("reason"),
|
|
860
|
+
error: (0, import_pg_core.text)("error"),
|
|
861
|
+
createdAt: ts2("created_at").notNull(),
|
|
862
|
+
updatedAt: ts2("updated_at").notNull()
|
|
863
|
+
}, (table) => [
|
|
864
|
+
(0, import_pg_core.index)("rag_ingestion_log_collection_idx").on(table.collection, table.updatedAt)
|
|
865
|
+
]);
|
|
866
|
+
var agentConfirmToken3 = (0, import_pg_core.pgTable)("agent_confirm_token", {
|
|
867
|
+
hash: (0, import_pg_core.text)("hash").primaryKey(),
|
|
868
|
+
actorRef: (0, import_pg_core.text)("actor_ref").notNull(),
|
|
869
|
+
tool: (0, import_pg_core.text)("tool").notNull(),
|
|
870
|
+
expiresAt: (0, import_pg_core.bigint)("expires_at", {
|
|
871
|
+
mode: "number"
|
|
872
|
+
}).notNull(),
|
|
873
|
+
createdAt: (0, import_pg_core.bigint)("created_at", {
|
|
874
|
+
mode: "number"
|
|
875
|
+
}).notNull()
|
|
876
|
+
}, (table) => [
|
|
877
|
+
(0, import_pg_core.index)("agent_confirm_token_expires_idx").on(table.expiresAt)
|
|
878
|
+
]);
|
|
879
|
+
var agentStreamFrame3 = (0, import_pg_core.pgTable)("agent_stream_frame", {
|
|
880
|
+
runId: (0, import_pg_core.text)("run_id").notNull(),
|
|
881
|
+
seq: (0, import_pg_core.integer)("seq").notNull(),
|
|
882
|
+
frame: (0, import_pg_core.text)("frame"),
|
|
883
|
+
error: (0, import_pg_core.text)("error"),
|
|
884
|
+
createdAt: (0, import_pg_core.bigint)("created_at", {
|
|
885
|
+
mode: "number"
|
|
886
|
+
}).notNull()
|
|
887
|
+
}, (table) => [
|
|
888
|
+
(0, import_pg_core.primaryKey)({
|
|
889
|
+
columns: [
|
|
890
|
+
table.runId,
|
|
891
|
+
table.seq
|
|
892
|
+
]
|
|
893
|
+
})
|
|
894
|
+
]);
|
|
895
|
+
var proposalJson2 = (0, import_pg_core.customType)({
|
|
896
|
+
dataType: /* @__PURE__ */ __name(() => "text", "dataType"),
|
|
897
|
+
toDriver: /* @__PURE__ */ __name((value) => (0, import_nestjs_agent_core2.canonicalActionProposalJson)(value), "toDriver"),
|
|
898
|
+
fromDriver: /* @__PURE__ */ __name((value) => JSON.parse(value), "fromDriver")
|
|
899
|
+
});
|
|
900
|
+
var agentActionProposal3 = (0, import_pg_core.pgTable)("agent_action_proposal", {
|
|
901
|
+
id: (0, import_pg_core.text)("id").primaryKey(),
|
|
902
|
+
scopeKey: (0, import_pg_core.text)("scope_key").notNull(),
|
|
903
|
+
decision: (0, import_pg_core.text)("decision").$type().notNull(),
|
|
904
|
+
logicalSort: (0, import_pg_core.text)("logical_sort").notNull(),
|
|
905
|
+
createFingerprint: (0, import_pg_core.text)("create_fingerprint").notNull(),
|
|
906
|
+
proposal: proposalJson2("proposal").notNull(),
|
|
907
|
+
version: (0, import_pg_core.bigint)("version", {
|
|
908
|
+
mode: "number"
|
|
909
|
+
}).notNull().default(0),
|
|
910
|
+
executionStatus: (0, import_pg_core.text)("execution_status").$type(),
|
|
911
|
+
leaseExpiresAt: (0, import_pg_core.bigint)("lease_expires_at", {
|
|
912
|
+
mode: "number"
|
|
913
|
+
}),
|
|
914
|
+
deliveryStatus: (0, import_pg_core.text)("delivery_status"),
|
|
915
|
+
deliveryLeaseExpiresAt: (0, import_pg_core.bigint)("delivery_lease_expires_at", {
|
|
916
|
+
mode: "number"
|
|
917
|
+
}),
|
|
918
|
+
replacementGroupKey: (0, import_pg_core.text)("replacement_group_key"),
|
|
919
|
+
outcomeIdKey: (0, import_pg_core.text)("outcome_id_key"),
|
|
920
|
+
proposalExpiresAt: (0, import_pg_core.bigint)("proposal_expires_at", {
|
|
921
|
+
mode: "number"
|
|
922
|
+
}),
|
|
923
|
+
discoveryIndexVersion: (0, import_pg_core.bigint)("discovery_index_version", {
|
|
924
|
+
mode: "number"
|
|
925
|
+
}).notNull().default(0),
|
|
926
|
+
createdAt: (0, import_pg_core.bigint)("created_at", {
|
|
927
|
+
mode: "number"
|
|
928
|
+
}).notNull()
|
|
929
|
+
}, (table) => [
|
|
930
|
+
(0, import_pg_core.index)("agent_action_proposal_execution_idx").on(table.discoveryIndexVersion, table.executionStatus, table.leaseExpiresAt, table.createdAt),
|
|
931
|
+
(0, import_pg_core.index)("agent_action_proposal_expiry_idx").on(table.discoveryIndexVersion, table.decision, table.proposalExpiresAt, table.createdAt),
|
|
932
|
+
(0, import_pg_core.index)("agent_action_proposal_replacement_idx").on(table.scopeKey, table.replacementGroupKey, table.decision),
|
|
933
|
+
(0, import_pg_core.index)("agent_action_proposal_delivery_idx").on(table.deliveryStatus, table.deliveryLeaseExpiresAt, table.createdAt),
|
|
934
|
+
(0, import_pg_core.uniqueIndex)("agent_action_proposal_outcome_idx").on(table.outcomeIdKey),
|
|
935
|
+
(0, import_pg_core.index)("agent_action_proposal_scope_idx").on(table.scopeKey, table.createdAt, table.id)
|
|
936
|
+
]);
|
|
937
|
+
var pgAgentSchema = {
|
|
938
|
+
agentActionProposal: agentActionProposal3,
|
|
939
|
+
agentThread: agentThread3,
|
|
940
|
+
agentMessage: agentMessage3,
|
|
941
|
+
agentQueuedMessage: agentQueuedMessage3,
|
|
942
|
+
agentToolCall: agentToolCall3,
|
|
943
|
+
agentTokenUsage: agentTokenUsage3,
|
|
944
|
+
agentModelPricing: agentModelPricing3,
|
|
945
|
+
agentRun: agentRun3,
|
|
946
|
+
agentMemory: agentMemory3,
|
|
947
|
+
ragIngestionLog: ragIngestionLog3,
|
|
948
|
+
agentConfirmToken: agentConfirmToken3,
|
|
949
|
+
agentStreamFrame: agentStreamFrame3
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
// src/dialect.ts
|
|
953
|
+
function agentDialectOf(db) {
|
|
954
|
+
if ((0, import_drizzle_orm.is)(db, import_pg_core2.PgDatabase)) return "postgres";
|
|
955
|
+
if ((0, import_drizzle_orm.is)(db, import_mysql_core2.MySqlDatabase)) return "mysql";
|
|
956
|
+
return "sqlite";
|
|
957
|
+
}
|
|
958
|
+
__name(agentDialectOf, "agentDialectOf");
|
|
959
|
+
function agentTablesFor(dialect) {
|
|
960
|
+
if (dialect === "postgres") return pgAgentSchema;
|
|
961
|
+
if (dialect === "mysql") return mysqlAgentSchema;
|
|
962
|
+
return agentSchema;
|
|
963
|
+
}
|
|
964
|
+
__name(agentTablesFor, "agentTablesFor");
|
|
965
|
+
function asBuilder(db) {
|
|
966
|
+
return db;
|
|
967
|
+
}
|
|
968
|
+
__name(asBuilder, "asBuilder");
|
|
969
|
+
async function affectedRows(dialect, statement, column) {
|
|
970
|
+
if (dialect !== "mysql") {
|
|
971
|
+
return (await statement.returning({
|
|
972
|
+
affected: column
|
|
973
|
+
})).length;
|
|
974
|
+
}
|
|
975
|
+
return mysqlAffectedRows(await statement);
|
|
976
|
+
}
|
|
977
|
+
__name(affectedRows, "affectedRows");
|
|
978
|
+
function mysqlAffectedRows(result) {
|
|
979
|
+
const header = Array.isArray(result) ? result[0] : result;
|
|
980
|
+
const counts = header;
|
|
981
|
+
const count3 = counts?.affectedRows ?? counts?.rowsAffected;
|
|
982
|
+
return typeof count3 === "number" ? count3 : 0;
|
|
983
|
+
}
|
|
984
|
+
__name(mysqlAffectedRows, "mysqlAffectedRows");
|
|
985
|
+
async function runSql(db, dialect, statement) {
|
|
986
|
+
if (dialect === "sqlite") {
|
|
987
|
+
await db.run(statement);
|
|
988
|
+
return;
|
|
616
989
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
await db.run(import_drizzle_orm.sql.raw(statement));
|
|
990
|
+
if (dialect === "postgres") {
|
|
991
|
+
await db.execute(statement);
|
|
992
|
+
return;
|
|
621
993
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
994
|
+
await db.execute(statement);
|
|
995
|
+
}
|
|
996
|
+
__name(runSql, "runSql");
|
|
997
|
+
async function querySql(db, dialect, statement) {
|
|
998
|
+
if (dialect === "sqlite") {
|
|
999
|
+
return db.all(statement);
|
|
1000
|
+
}
|
|
1001
|
+
const result = dialect === "postgres" ? await db.execute(statement) : await db.execute(statement);
|
|
1002
|
+
if (dialect === "mysql") {
|
|
1003
|
+
return Array.isArray(result) ? result[0] : [];
|
|
1004
|
+
}
|
|
1005
|
+
return Array.isArray(result) ? result : result.rows ?? [];
|
|
1006
|
+
}
|
|
1007
|
+
__name(querySql, "querySql");
|
|
1008
|
+
function isUniqueViolation(error) {
|
|
1009
|
+
for (let current = error; current !== null && typeof current === "object"; ) {
|
|
1010
|
+
const { code, errno } = current;
|
|
1011
|
+
if (code === "23505" || code === "ER_DUP_ENTRY" || errno === 1062 || code === "ER_LOCK_DEADLOCK" || errno === 1213 || typeof code === "string" && code.startsWith("SQLITE_CONSTRAINT")) {
|
|
1012
|
+
return true;
|
|
626
1013
|
}
|
|
1014
|
+
current = current.cause;
|
|
1015
|
+
}
|
|
1016
|
+
return false;
|
|
1017
|
+
}
|
|
1018
|
+
__name(isUniqueViolation, "isUniqueViolation");
|
|
1019
|
+
function upsert(dialect, insert, target, set) {
|
|
1020
|
+
if (dialect === "mysql") {
|
|
1021
|
+
return insert.onDuplicateKeyUpdate({
|
|
1022
|
+
set
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
return insert.onConflictDoUpdate({
|
|
1026
|
+
target,
|
|
1027
|
+
set
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
__name(upsert, "upsert");
|
|
1031
|
+
|
|
1032
|
+
// src/ensure-schema.ts
|
|
1033
|
+
async function ensureAgentSchema(db) {
|
|
1034
|
+
const dialect = agentDialectOf(db);
|
|
1035
|
+
if (dialect === "postgres") {
|
|
1036
|
+
await db.transaction(async (tx) => {
|
|
1037
|
+
await tx.execute(import_drizzle_orm2.sql.raw(`select pg_advisory_xact_lock(hashtext('${LOCK_NAME}'))`));
|
|
1038
|
+
await heal(tx, dialect);
|
|
1039
|
+
});
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
if (dialect === "mysql") {
|
|
1043
|
+
await db.transaction(async (tx) => {
|
|
1044
|
+
await tx.execute(import_drizzle_orm2.sql.raw(`select get_lock('${LOCK_NAME}', 30)`));
|
|
1045
|
+
try {
|
|
1046
|
+
await heal(tx, dialect);
|
|
1047
|
+
} finally {
|
|
1048
|
+
await tx.execute(import_drizzle_orm2.sql.raw(`select release_lock('${LOCK_NAME}')`));
|
|
1049
|
+
}
|
|
1050
|
+
});
|
|
1051
|
+
return;
|
|
627
1052
|
}
|
|
1053
|
+
await heal(db, dialect);
|
|
628
1054
|
}
|
|
629
1055
|
__name(ensureAgentSchema, "ensureAgentSchema");
|
|
1056
|
+
var LOCK_NAME = "nestjs_agent_schema";
|
|
1057
|
+
var MYSQL_TABLE_OPTIONS = "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin";
|
|
1058
|
+
function tableSpec(dialect, table) {
|
|
1059
|
+
const config = dialect === "postgres" ? (0, import_pg_core3.getTableConfig)(table) : dialect === "mysql" ? (0, import_mysql_core3.getTableConfig)(table) : (0, import_sqlite_core2.getTableConfig)(table);
|
|
1060
|
+
const columns = config.columns;
|
|
1061
|
+
const compositeKey = config.primaryKeys[0]?.columns.map((column) => column.name);
|
|
1062
|
+
return {
|
|
1063
|
+
name: config.name,
|
|
1064
|
+
columns,
|
|
1065
|
+
primaryKey: compositeKey ?? columns.filter((column) => column.primary).map((c) => c.name),
|
|
1066
|
+
foreignKeys: config.foreignKeys.map((foreignKey) => {
|
|
1067
|
+
const reference = foreignKey.reference();
|
|
1068
|
+
return {
|
|
1069
|
+
columns: reference.columns.map((column) => column.name),
|
|
1070
|
+
table: (0, import_drizzle_orm2.getTableName)(reference.foreignTable),
|
|
1071
|
+
foreignColumns: reference.foreignColumns.map((column) => column.name),
|
|
1072
|
+
...foreignKey.onDelete !== void 0 ? {
|
|
1073
|
+
onDelete: foreignKey.onDelete
|
|
1074
|
+
} : {}
|
|
1075
|
+
};
|
|
1076
|
+
}),
|
|
1077
|
+
indexes: config.indexes.map((index4) => ({
|
|
1078
|
+
name: String(index4.config.name),
|
|
1079
|
+
unique: index4.config.unique === true,
|
|
1080
|
+
columns: index4.config.columns.map((column) => String(column.name))
|
|
1081
|
+
}))
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
__name(tableSpec, "tableSpec");
|
|
1085
|
+
function quote(dialect, identifier) {
|
|
1086
|
+
return dialect === "mysql" ? `\`${identifier}\`` : `"${identifier}"`;
|
|
1087
|
+
}
|
|
1088
|
+
__name(quote, "quote");
|
|
1089
|
+
function literal(dialect, value) {
|
|
1090
|
+
if (typeof value === "boolean") {
|
|
1091
|
+
return dialect === "sqlite" ? value ? "1" : "0" : value ? "true" : "false";
|
|
1092
|
+
}
|
|
1093
|
+
if (typeof value === "number") return String(value);
|
|
1094
|
+
return `'${String(value).replaceAll("'", "''")}'`;
|
|
1095
|
+
}
|
|
1096
|
+
__name(literal, "literal");
|
|
1097
|
+
function columnDefinition(dialect, column) {
|
|
1098
|
+
const parts = [
|
|
1099
|
+
quote(dialect, column.name),
|
|
1100
|
+
column.getSQLType()
|
|
1101
|
+
];
|
|
1102
|
+
if (column.notNull) parts.push("NOT NULL");
|
|
1103
|
+
if (column.hasDefault && column.default !== void 0) {
|
|
1104
|
+
parts.push(`DEFAULT ${literal(dialect, column.default)}`);
|
|
1105
|
+
}
|
|
1106
|
+
return parts.join(" ");
|
|
1107
|
+
}
|
|
1108
|
+
__name(columnDefinition, "columnDefinition");
|
|
1109
|
+
function createTableSql(dialect, spec) {
|
|
1110
|
+
const q = /* @__PURE__ */ __name((identifier) => quote(dialect, identifier), "q");
|
|
1111
|
+
const lines = spec.columns.map((column) => columnDefinition(dialect, column));
|
|
1112
|
+
lines.push(`PRIMARY KEY (${spec.primaryKey.map(q).join(", ")})`);
|
|
1113
|
+
for (const foreignKey of spec.foreignKeys) {
|
|
1114
|
+
lines.push(`FOREIGN KEY (${foreignKey.columns.map(q).join(", ")}) REFERENCES ${q(foreignKey.table)} (${foreignKey.foreignColumns.map(q).join(", ")})${foreignKey.onDelete !== void 0 ? ` ON DELETE ${foreignKey.onDelete.toUpperCase()}` : ""}`);
|
|
1115
|
+
}
|
|
1116
|
+
const options = dialect === "mysql" ? ` ${MYSQL_TABLE_OPTIONS}` : "";
|
|
1117
|
+
return `CREATE TABLE IF NOT EXISTS ${q(spec.name)} (
|
|
1118
|
+
${lines.join(",\n ")}
|
|
1119
|
+
)${options}`;
|
|
1120
|
+
}
|
|
1121
|
+
__name(createTableSql, "createTableSql");
|
|
1122
|
+
function agentSchemaDdl(dialect) {
|
|
1123
|
+
const statements = [];
|
|
1124
|
+
for (const spec of specs(dialect)) {
|
|
1125
|
+
statements.push(createTableSql(dialect, spec));
|
|
1126
|
+
for (const index4 of spec.indexes) {
|
|
1127
|
+
statements.push(createIndexSql(dialect, spec.name, index4));
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
return statements;
|
|
1131
|
+
}
|
|
1132
|
+
__name(agentSchemaDdl, "agentSchemaDdl");
|
|
1133
|
+
function createIndexSql(dialect, table, index4) {
|
|
1134
|
+
const q = /* @__PURE__ */ __name((identifier) => quote(dialect, identifier), "q");
|
|
1135
|
+
const guard = dialect === "mysql" ? "" : "IF NOT EXISTS ";
|
|
1136
|
+
return `CREATE ${index4.unique ? "UNIQUE " : ""}INDEX ${guard}${q(index4.name)} ON ${q(table)} (${index4.columns.map(q).join(", ")})`;
|
|
1137
|
+
}
|
|
1138
|
+
__name(createIndexSql, "createIndexSql");
|
|
1139
|
+
function specs(dialect) {
|
|
1140
|
+
const all = Object.values(agentTablesFor(dialect)).map((table) => tableSpec(dialect, table));
|
|
1141
|
+
const ordered = [];
|
|
1142
|
+
const placed = /* @__PURE__ */ new Set();
|
|
1143
|
+
while (ordered.length < all.length) {
|
|
1144
|
+
const ready = all.filter((spec) => !placed.has(spec.name) && spec.foreignKeys.every((key2) => key2.table === spec.name || placed.has(key2.table)));
|
|
1145
|
+
if (ready.length === 0) throw new Error("agent schema: circular foreign keys");
|
|
1146
|
+
for (const spec of ready) {
|
|
1147
|
+
ordered.push(spec);
|
|
1148
|
+
placed.add(spec.name);
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
return ordered;
|
|
1152
|
+
}
|
|
1153
|
+
__name(specs, "specs");
|
|
1154
|
+
async function existingColumns(db, dialect, table) {
|
|
1155
|
+
const rows = dialect === "sqlite" ? await querySql(db, dialect, import_drizzle_orm2.sql.raw(`PRAGMA table_info("${table}")`)) : await querySql(db, dialect, import_drizzle_orm2.sql`select column_name as name from information_schema.columns where table_schema = ${import_drizzle_orm2.sql.raw(dialect === "mysql" ? "database()" : "current_schema()")} and table_name = ${table}`);
|
|
1156
|
+
return new Set(rows.map((row) => String(row.name)));
|
|
1157
|
+
}
|
|
1158
|
+
__name(existingColumns, "existingColumns");
|
|
1159
|
+
async function indexExists(db, dialect, table, index4) {
|
|
1160
|
+
let query;
|
|
1161
|
+
if (dialect === "sqlite") {
|
|
1162
|
+
query = import_drizzle_orm2.sql`select name from sqlite_master where type = 'index' and name = ${index4}`;
|
|
1163
|
+
} else if (dialect === "postgres") {
|
|
1164
|
+
query = import_drizzle_orm2.sql`select indexname as name from pg_indexes where schemaname = current_schema() and indexname = ${index4}`;
|
|
1165
|
+
} else {
|
|
1166
|
+
query = import_drizzle_orm2.sql`select index_name as name from information_schema.statistics where table_schema = database() and table_name = ${table} and index_name = ${index4}`;
|
|
1167
|
+
}
|
|
1168
|
+
return (await querySql(db, dialect, query)).length > 0;
|
|
1169
|
+
}
|
|
1170
|
+
__name(indexExists, "indexExists");
|
|
1171
|
+
async function heal(db, dialect) {
|
|
1172
|
+
for (const spec of specs(dialect)) {
|
|
1173
|
+
await runSql(db, dialect, import_drizzle_orm2.sql.raw(createTableSql(dialect, spec)));
|
|
1174
|
+
const present = await existingColumns(db, dialect, spec.name);
|
|
1175
|
+
for (const column of spec.columns) {
|
|
1176
|
+
if (!present.has(column.name)) {
|
|
1177
|
+
await runSql(db, dialect, import_drizzle_orm2.sql.raw(`ALTER TABLE ${quote(dialect, spec.name)} ADD COLUMN ${columnDefinition(dialect, column)}`));
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
for (const index4 of spec.indexes) {
|
|
1181
|
+
if (!await indexExists(db, dialect, spec.name, index4.name)) {
|
|
1182
|
+
await runSql(db, dialect, import_drizzle_orm2.sql.raw(createIndexSql(dialect, spec.name, index4)));
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
__name(heal, "heal");
|
|
630
1188
|
|
|
631
1189
|
// src/drizzle-agent-store.ts
|
|
632
|
-
var
|
|
633
|
-
var
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
1190
|
+
var import_nestjs_agent_core4 = require("@dudousxd/nestjs-agent-core");
|
|
1191
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
1192
|
+
|
|
1193
|
+
// src/drizzle-action-proposal-store.ts
|
|
1194
|
+
var import_node_crypto = require("crypto");
|
|
1195
|
+
var import_nestjs_agent_core3 = require("@dudousxd/nestjs-agent-core");
|
|
1196
|
+
var import_drizzle_orm3 = require("drizzle-orm");
|
|
1197
|
+
|
|
1198
|
+
// src/action-proposal-transaction-mode.ts
|
|
1199
|
+
function actionProposalTransactionMode(kind) {
|
|
1200
|
+
if (kind === "BetterSQLite3Database" || kind === "BetterSQLiteTransaction") return "sync";
|
|
1201
|
+
if (kind !== void 0 && [
|
|
1202
|
+
"NodePgDatabase",
|
|
1203
|
+
"NodePgTransaction",
|
|
1204
|
+
"MySql2Database",
|
|
1205
|
+
"MySql2Transaction",
|
|
1206
|
+
"LibSQLDatabase",
|
|
1207
|
+
"LibSQLTransaction"
|
|
1208
|
+
].includes(kind)) return "async";
|
|
1209
|
+
return "unsupported";
|
|
1210
|
+
}
|
|
1211
|
+
__name(actionProposalTransactionMode, "actionProposalTransactionMode");
|
|
1212
|
+
|
|
1213
|
+
// src/drizzle-action-proposal-store.ts
|
|
1214
|
+
var DrizzleActionProposalStore = class {
|
|
1215
|
+
static {
|
|
1216
|
+
__name(this, "DrizzleActionProposalStore");
|
|
1217
|
+
}
|
|
1218
|
+
db;
|
|
1219
|
+
rawDb;
|
|
1220
|
+
actionProposalAdmissionSupported;
|
|
1221
|
+
synchronousSqlite;
|
|
1222
|
+
dialect;
|
|
1223
|
+
table;
|
|
1224
|
+
clock;
|
|
1225
|
+
constructor(db, options = {}) {
|
|
1226
|
+
this.rawDb = db;
|
|
1227
|
+
const kind = db.constructor[import_drizzle_orm3.entityKind];
|
|
1228
|
+
const transactionMode = actionProposalTransactionMode(kind);
|
|
1229
|
+
this.synchronousSqlite = transactionMode === "sync";
|
|
1230
|
+
this.actionProposalAdmissionSupported = transactionMode !== "unsupported";
|
|
1231
|
+
this.dialect = agentDialectOf(db);
|
|
1232
|
+
this.table = agentTablesFor(this.dialect).agentActionProposal;
|
|
1233
|
+
this.db = asBuilder(db);
|
|
1234
|
+
this.clock = options.clock ?? Date.now;
|
|
1235
|
+
}
|
|
1236
|
+
async createActionProposal(input) {
|
|
1237
|
+
const proposal = (0, import_nestjs_agent_core3.initialActionProposal)(input, this.clock());
|
|
1238
|
+
const row = {
|
|
1239
|
+
id: physicalId(input.id),
|
|
1240
|
+
scopeKey: scopeKey(input),
|
|
1241
|
+
decision: proposal.decision,
|
|
1242
|
+
logicalSort: logicalSort(input.id),
|
|
1243
|
+
createFingerprint: (0, import_nestjs_agent_core3.canonicalActionProposalJson)(input),
|
|
1244
|
+
proposal,
|
|
1245
|
+
...discoveryMetadata(proposal),
|
|
1246
|
+
version: 0,
|
|
1247
|
+
createdAt: proposal.createdAt
|
|
1248
|
+
};
|
|
1249
|
+
const table = this.table;
|
|
1250
|
+
const insert = this.db.insert(table);
|
|
1251
|
+
const inserted = this.dialect === "mysql" ? mysqlAffectedRows(await insert.ignore().values(row)) : await affectedRows(this.dialect, insert.values(row).onConflictDoNothing(), table.id);
|
|
1252
|
+
const stored = await this.read(input, input.id);
|
|
1253
|
+
if (!stored) return {
|
|
1254
|
+
status: "conflict"
|
|
1255
|
+
};
|
|
1256
|
+
if (inserted === 1) return {
|
|
1257
|
+
status: "created",
|
|
1258
|
+
proposal: stored.proposal
|
|
1259
|
+
};
|
|
1260
|
+
return {
|
|
1261
|
+
status: stored.createFingerprint === row.createFingerprint ? "unchanged" : "conflict",
|
|
1262
|
+
proposal: stored.proposal
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
createReplacingActionProposal(input) {
|
|
1266
|
+
if (!input.replacementKey) return this.createActionProposal(input);
|
|
1267
|
+
const proposal = (0, import_nestjs_agent_core3.initialActionProposal)(input, this.clock());
|
|
1268
|
+
return this.transactionSteps((db) => this.replacementSteps(db, input, proposal));
|
|
1269
|
+
}
|
|
1270
|
+
async supersedeActionProposal(scope, id, command) {
|
|
1271
|
+
const replacement = await this.getActionProposal(scope, command.replacementProposalId);
|
|
1272
|
+
if (!replacement) return {
|
|
1273
|
+
status: "not_found"
|
|
1274
|
+
};
|
|
1275
|
+
return this.mutate(scope, id, (row, now) => (0, import_nestjs_agent_core3.transitionActionProposalSupersession)(row, replacement, command, now));
|
|
1276
|
+
}
|
|
1277
|
+
*replacementSteps(db, input, proposal) {
|
|
1278
|
+
const table = this.table;
|
|
1279
|
+
const thread = agentTablesFor(this.dialect).agentThread;
|
|
1280
|
+
if (this.dialect === "sqlite") yield {
|
|
1281
|
+
kind: "write",
|
|
1282
|
+
query: db.update(thread).set({
|
|
1283
|
+
updatedAt: import_drizzle_orm3.sql`${thread.updatedAt}`
|
|
1284
|
+
}).where((0, import_drizzle_orm3.eq)(thread.id, input.threadId))
|
|
1285
|
+
};
|
|
1286
|
+
const threads = yield {
|
|
1287
|
+
kind: "read",
|
|
1288
|
+
query: lockQuery(db.select().from(thread).where((0, import_drizzle_orm3.eq)(thread.id, input.threadId)).limit(1), this.dialect)
|
|
1289
|
+
};
|
|
1290
|
+
const owner = threads[0];
|
|
1291
|
+
if (!owner || owner.id !== input.threadId || owner.deletedAt || owner.actorRef !== input.actorRef || owner.tenantRef !== input.tenantRef) return {
|
|
1292
|
+
status: "conflict"
|
|
1293
|
+
};
|
|
1294
|
+
const existingRows = yield {
|
|
1295
|
+
kind: "read",
|
|
1296
|
+
query: db.select().from(table).where((0, import_drizzle_orm3.eq)(table.id, physicalId(input.id))).limit(1)
|
|
1297
|
+
};
|
|
1298
|
+
const existing = existingRows[0];
|
|
1299
|
+
if (existing) {
|
|
1300
|
+
if (existing.proposal.id !== input.id || !(0, import_nestjs_agent_core3.actionProposalScopeMatches)(existing.proposal, input)) return {
|
|
1301
|
+
status: "conflict"
|
|
1302
|
+
};
|
|
1303
|
+
return {
|
|
1304
|
+
status: existing.createFingerprint === (0, import_nestjs_agent_core3.canonicalActionProposalJson)(input) ? "unchanged" : "conflict",
|
|
1305
|
+
proposal: existing.proposal
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
const groupKey = replacementGroupKey(input);
|
|
1309
|
+
if (groupKey === null) throw new Error("Missing explicit replacement identity");
|
|
1310
|
+
const pending = yield {
|
|
1311
|
+
kind: "read",
|
|
1312
|
+
query: lockQuery(db.select().from(table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.scopeKey, scopeKey(input)), (0, import_drizzle_orm3.eq)(table.replacementGroupKey, groupKey), (0, import_drizzle_orm3.eq)(table.decision, "pending"))), this.dialect)
|
|
1313
|
+
};
|
|
1314
|
+
const row = {
|
|
1315
|
+
id: physicalId(input.id),
|
|
1316
|
+
scopeKey: scopeKey(input),
|
|
1317
|
+
decision: proposal.decision,
|
|
1318
|
+
logicalSort: logicalSort(input.id),
|
|
1319
|
+
createFingerprint: (0, import_nestjs_agent_core3.canonicalActionProposalJson)(input),
|
|
1320
|
+
proposal,
|
|
1321
|
+
...discoveryMetadata(proposal),
|
|
1322
|
+
version: 0,
|
|
1323
|
+
createdAt: proposal.createdAt
|
|
1324
|
+
};
|
|
1325
|
+
const insert = db.insert(table);
|
|
1326
|
+
const inserted = yield {
|
|
1327
|
+
kind: "read",
|
|
1328
|
+
query: this.dialect === "mysql" ? insert.ignore().values(row) : insert.values(row).onConflictDoNothing().returning({
|
|
1329
|
+
id: table.id
|
|
1330
|
+
})
|
|
1331
|
+
};
|
|
1332
|
+
const count3 = this.dialect === "mysql" ? mysqlAffectedRows(inserted) : inserted.length;
|
|
1333
|
+
if (count3 !== 1) return {
|
|
1334
|
+
status: "conflict"
|
|
1335
|
+
};
|
|
1336
|
+
for (const old of pending) {
|
|
1337
|
+
const result = (0, import_nestjs_agent_core3.transitionActionProposalSupersession)(old.proposal, proposal, {
|
|
1338
|
+
replacementProposalId: proposal.id,
|
|
1339
|
+
actorRef: proposal.actorRef,
|
|
1340
|
+
via: "replacement"
|
|
1341
|
+
}, this.clock());
|
|
1342
|
+
if (!result.proposal || (0, import_nestjs_agent_core3.canonicalActionProposalJson)(result.proposal) === (0, import_nestjs_agent_core3.canonicalActionProposalJson)(old.proposal)) continue;
|
|
1343
|
+
const next = result.proposal;
|
|
1344
|
+
const update = db.update(table).set({
|
|
1345
|
+
proposal: next,
|
|
1346
|
+
decision: next.decision,
|
|
1347
|
+
...discoveryMetadata(next),
|
|
1348
|
+
version: old.version + 1
|
|
1349
|
+
}).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.id, old.id), (0, import_drizzle_orm3.eq)(table.version, old.version)));
|
|
1350
|
+
const updated = yield {
|
|
1351
|
+
kind: "read",
|
|
1352
|
+
query: this.dialect === "mysql" ? update : update.returning({
|
|
1353
|
+
id: table.id
|
|
1354
|
+
})
|
|
1355
|
+
};
|
|
1356
|
+
if ((this.dialect === "mysql" ? mysqlAffectedRows(updated) : updated.length) !== 1) throw new Error("Replacement lost its locked proposal fence");
|
|
1357
|
+
}
|
|
1358
|
+
return {
|
|
1359
|
+
status: "created",
|
|
1360
|
+
proposal
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
async getActionProposal(scope, id) {
|
|
1364
|
+
return (await this.read(scope, id))?.proposal ?? null;
|
|
1365
|
+
}
|
|
1366
|
+
async listActionProposals(scope, query = {}) {
|
|
1367
|
+
const limit = (0, import_nestjs_agent_core3.validateActionProposalListQuery)(query);
|
|
1368
|
+
const rows = await this.db.select().from(this.table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.table.scopeKey, scopeKey(scope)), query.after === void 0 ? void 0 : (0, import_drizzle_orm3.or)((0, import_drizzle_orm3.gt)(this.table.createdAt, query.after.createdAt), (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.table.createdAt, query.after.createdAt), (0, import_drizzle_orm3.gt)(this.table.logicalSort, logicalSort(query.after.id)))), query.decision === void 0 ? void 0 : (0, import_drizzle_orm3.eq)(this.table.decision, query.decision))).orderBy((0, import_drizzle_orm3.asc)(this.table.createdAt), (0, import_drizzle_orm3.asc)(this.table.logicalSort)).limit(limit);
|
|
1369
|
+
return rows.map((row) => row.proposal).filter((proposal) => (0, import_nestjs_agent_core3.actionProposalScopeMatches)(proposal, scope));
|
|
1370
|
+
}
|
|
1371
|
+
decideActionProposal(scope, id, command) {
|
|
1372
|
+
return this.mutate(scope, id, (row, now) => (0, import_nestjs_agent_core3.transitionActionProposalDecision)(row, command, now));
|
|
1373
|
+
}
|
|
1374
|
+
claimActionProposal(scope, id, command) {
|
|
1375
|
+
return this.mutate(scope, id, (row, now) => (0, import_nestjs_agent_core3.transitionActionProposalClaim)(row, command, now, (0, import_node_crypto.randomUUID)()));
|
|
1376
|
+
}
|
|
1377
|
+
extendActionProposalLease(scope, id, command) {
|
|
1378
|
+
return this.mutate(scope, id, (row, now) => (0, import_nestjs_agent_core3.transitionActionProposalLease)(row, command, now));
|
|
1379
|
+
}
|
|
1380
|
+
settleActionProposal(scope, id, command) {
|
|
1381
|
+
return this.mutate(scope, id, (row, now) => (0, import_nestjs_agent_core3.transitionActionProposalSettlement)(row, command, now));
|
|
1382
|
+
}
|
|
1383
|
+
async claimNextActionProposal(command) {
|
|
1384
|
+
const now = this.clock();
|
|
1385
|
+
(0, import_nestjs_agent_core3.validateActionProposalWorkerClaim)(command, now);
|
|
1386
|
+
const table = this.table;
|
|
1387
|
+
const candidates = await this.db.select().from(table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.discoveryIndexVersion, 1), (0, import_drizzle_orm3.eq)(table.decision, "approved"), (0, import_drizzle_orm3.or)((0, import_drizzle_orm3.eq)(table.executionStatus, "queued"), (0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.executionStatus, "executing"), (0, import_drizzle_orm3.lte)(table.leaseExpiresAt, now))))).orderBy((0, import_drizzle_orm3.asc)(table.createdAt), (0, import_drizzle_orm3.asc)(table.logicalSort)).limit(32);
|
|
1388
|
+
for (const candidate of candidates) {
|
|
1389
|
+
const result = await this.claimActionProposal(candidate.proposal, candidate.proposal.id, command);
|
|
1390
|
+
if (result.status === "applied" && result.proposal) return result.proposal;
|
|
1391
|
+
}
|
|
1392
|
+
return null;
|
|
1393
|
+
}
|
|
1394
|
+
async expireActionProposals(command) {
|
|
1395
|
+
const now = this.clock();
|
|
1396
|
+
(0, import_nestjs_agent_core3.validateActionProposalExpiryBatch)(command, now);
|
|
1397
|
+
const table = this.table;
|
|
1398
|
+
const candidates = await this.db.select().from(table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.discoveryIndexVersion, 1), (0, import_drizzle_orm3.eq)(table.decision, "pending"), (0, import_drizzle_orm3.lte)(table.proposalExpiresAt, now))).orderBy((0, import_drizzle_orm3.asc)(table.createdAt), (0, import_drizzle_orm3.asc)(table.logicalSort)).limit(command.limit);
|
|
1399
|
+
let expired = 0;
|
|
1400
|
+
for (const candidate of candidates) {
|
|
1401
|
+
const result = await this.decideActionProposal(candidate.proposal, candidate.proposal.id, {
|
|
1402
|
+
decision: "expired",
|
|
1403
|
+
actorRef: "system",
|
|
1404
|
+
via: "expiry"
|
|
1405
|
+
});
|
|
1406
|
+
if (result.status === "applied") expired++;
|
|
1407
|
+
}
|
|
1408
|
+
return expired;
|
|
1409
|
+
}
|
|
1410
|
+
async backfillActionProposalDiscoveryIndex(command) {
|
|
1411
|
+
(0, import_nestjs_agent_core3.validateActionProposalDiscoveryIndexBatch)(command);
|
|
1412
|
+
const table = this.table;
|
|
1413
|
+
const candidates = await this.db.select().from(table).where((0, import_drizzle_orm3.eq)(table.discoveryIndexVersion, 0)).orderBy((0, import_drizzle_orm3.asc)(table.createdAt), (0, import_drizzle_orm3.asc)(table.logicalSort)).limit(command.limit);
|
|
1414
|
+
let changed = 0;
|
|
1415
|
+
for (const row of candidates) {
|
|
1416
|
+
changed += await affectedRows(this.dialect, this.db.update(table).set({
|
|
1417
|
+
...discoveryMetadata(row.proposal),
|
|
1418
|
+
version: row.version + 1
|
|
1419
|
+
}).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.id, row.id), (0, import_drizzle_orm3.eq)(table.scopeKey, row.scopeKey), (0, import_drizzle_orm3.eq)(table.version, row.version), (0, import_drizzle_orm3.eq)(table.discoveryIndexVersion, 0))), table.id);
|
|
1420
|
+
}
|
|
1421
|
+
return changed;
|
|
1422
|
+
}
|
|
1423
|
+
async rememberedActionProposalApprovals(scope) {
|
|
1424
|
+
const table = this.table;
|
|
1425
|
+
const rows = await this.db.select().from(table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.scopeKey, scopeKey(scope)), (0, import_drizzle_orm3.eq)(table.decision, "approved"), (0, import_drizzle_orm3.or)((0, import_drizzle_orm3.eq)(table.executionStatus, "succeeded"), (0, import_drizzle_orm3.eq)(table.executionStatus, "failed"))));
|
|
1426
|
+
return [
|
|
1427
|
+
...new Set(rows.filter((row) => (0, import_nestjs_agent_core3.actionProposalScopeMatches)(row.proposal, scope) && row.proposal.decisionAudit?.remember === true).map((row) => row.proposal.toolName))
|
|
1428
|
+
];
|
|
1429
|
+
}
|
|
1430
|
+
async getThreadActionProposalScope(threadId) {
|
|
1431
|
+
const thread = agentTablesFor(this.dialect).agentThread;
|
|
1432
|
+
const [row] = await this.db.select().from(thread).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(thread.id, threadId), (0, import_drizzle_orm3.isNull)(thread.deletedAt))).limit(1);
|
|
1433
|
+
return row?.id === threadId ? {
|
|
1434
|
+
actorRef: row.actorRef,
|
|
1435
|
+
tenantRef: row.tenantRef,
|
|
1436
|
+
threadId: row.id
|
|
1437
|
+
} : null;
|
|
1438
|
+
}
|
|
1439
|
+
async claimNextActionProposalOutcome(command) {
|
|
1440
|
+
const now = this.clock();
|
|
1441
|
+
(0, import_nestjs_agent_core3.validateActionProposalWorkerClaim)(command, now);
|
|
1442
|
+
const table = this.table;
|
|
1443
|
+
const candidates = await this.db.select().from(table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.deliveryStatus, "pending"), (0, import_drizzle_orm3.or)((0, import_drizzle_orm3.isNull)(table.deliveryLeaseExpiresAt), (0, import_drizzle_orm3.lte)(table.deliveryLeaseExpiresAt, now)))).orderBy((0, import_drizzle_orm3.asc)(table.createdAt), (0, import_drizzle_orm3.asc)(table.logicalSort)).limit(32);
|
|
1444
|
+
for (const row of candidates) {
|
|
1445
|
+
const result = await this.mutate(row.proposal, row.proposal.id, (proposal, time) => {
|
|
1446
|
+
const claimed2 = (0, import_nestjs_agent_core3.claimActionProposalOutcome)(proposal, command, time, (0, import_node_crypto.randomUUID)());
|
|
1447
|
+
return claimed2 ? {
|
|
1448
|
+
status: "applied",
|
|
1449
|
+
proposal: claimed2
|
|
1450
|
+
} : {
|
|
1451
|
+
status: "conflict",
|
|
1452
|
+
proposal
|
|
1453
|
+
};
|
|
1454
|
+
});
|
|
1455
|
+
const claimed = result.proposal;
|
|
1456
|
+
if (result.status === "applied" && claimed?.outcome && claimed.outcomeDelivery?.lease) {
|
|
1457
|
+
return {
|
|
1458
|
+
outcome: claimed.outcome,
|
|
1459
|
+
lease: {
|
|
1460
|
+
outcomeId: claimed.outcome.id,
|
|
1461
|
+
token: claimed.outcomeDelivery.lease.token,
|
|
1462
|
+
generation: claimed.outcomeDelivery.generation
|
|
1463
|
+
}
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return null;
|
|
1468
|
+
}
|
|
1469
|
+
async admitActionProposalOutcome(command) {
|
|
1470
|
+
return this.transactionSteps((db) => this.admissionSteps(db, command));
|
|
1471
|
+
}
|
|
1472
|
+
async transactionSteps(factory) {
|
|
1473
|
+
if (!this.actionProposalAdmissionSupported) throw new Error("Independent actions require a certified transactional Drizzle driver (better-sqlite3, node-postgres, mysql2 or libsql)");
|
|
1474
|
+
if (this.synchronousSqlite) {
|
|
1475
|
+
const database2 = this.rawDb;
|
|
1476
|
+
return database2.transaction((tx) => runSync(factory(asBuilder(tx))), {
|
|
1477
|
+
behavior: "immediate"
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
const database = this.rawDb;
|
|
1481
|
+
return database.transaction(async (tx) => runAsync(factory(asBuilder(tx))));
|
|
1482
|
+
}
|
|
1483
|
+
*admissionSteps(db, command) {
|
|
1484
|
+
if (typeof command.outcomeId !== "string" || !command.outcomeId || typeof command.token !== "string" || !command.token || !Number.isSafeInteger(command.generation) || command.generation < 1) throw new TypeError("Invalid outcome fence");
|
|
1485
|
+
const table = this.table;
|
|
1486
|
+
const tables = agentTablesFor(this.dialect);
|
|
1487
|
+
if (this.dialect === "sqlite") yield {
|
|
1488
|
+
kind: "write",
|
|
1489
|
+
query: db.update(table).set({
|
|
1490
|
+
version: import_drizzle_orm3.sql`${table.version}`
|
|
1491
|
+
}).where((0, import_drizzle_orm3.eq)(table.outcomeIdKey, physicalId(command.outcomeId)))
|
|
1492
|
+
};
|
|
1493
|
+
const query = db.select().from(table).where((0, import_drizzle_orm3.eq)(table.outcomeIdKey, physicalId(command.outcomeId))).limit(1);
|
|
1494
|
+
const rows = yield {
|
|
1495
|
+
kind: "read",
|
|
1496
|
+
query: lockQuery(query, this.dialect)
|
|
1497
|
+
};
|
|
1498
|
+
const row = rows[0];
|
|
1499
|
+
if (!row || row.proposal.outcome?.id !== command.outcomeId) return {
|
|
1500
|
+
status: "not_found"
|
|
1501
|
+
};
|
|
1502
|
+
const proposal = row.proposal;
|
|
1503
|
+
const outcome = proposal.outcome;
|
|
1504
|
+
const delivery = proposal.outcomeDelivery;
|
|
1505
|
+
if (!outcome || !delivery) return {
|
|
1506
|
+
status: "not_found"
|
|
1507
|
+
};
|
|
1508
|
+
if (proposal.outcomeDelivery?.status === "admitted") return {
|
|
1509
|
+
status: "unchanged",
|
|
1510
|
+
...proposal.outcomeDelivery.messageId !== void 0 ? {
|
|
1511
|
+
messageId: proposal.outcomeDelivery.messageId
|
|
1512
|
+
} : {}
|
|
1513
|
+
};
|
|
1514
|
+
if (proposal.outcomeDelivery?.status === "discarded") return {
|
|
1515
|
+
status: "discarded"
|
|
1516
|
+
};
|
|
1517
|
+
if (!(0, import_nestjs_agent_core3.actionProposalOutcomeFenceValid)(proposal, command, this.clock())) return {
|
|
1518
|
+
status: "conflict"
|
|
1519
|
+
};
|
|
1520
|
+
const threadQuery = db.select().from(tables.agentThread).where((0, import_drizzle_orm3.eq)(tables.agentThread.id, proposal.threadId)).limit(1);
|
|
1521
|
+
const threads = yield {
|
|
1522
|
+
kind: "read",
|
|
1523
|
+
query: lockQuery(threadQuery, this.dialect)
|
|
1524
|
+
};
|
|
1525
|
+
const thread = threads[0];
|
|
1526
|
+
const now = this.clock();
|
|
1527
|
+
if (!(0, import_nestjs_agent_core3.actionProposalOutcomeFenceValid)(proposal, command, now)) return {
|
|
1528
|
+
status: "conflict"
|
|
1529
|
+
};
|
|
1530
|
+
if (!thread || thread.id !== proposal.threadId || thread.deletedAt || thread.actorRef !== proposal.actorRef || thread.tenantRef !== proposal.tenantRef) {
|
|
1531
|
+
const discarded = {
|
|
1532
|
+
...proposal,
|
|
1533
|
+
outcomeDelivery: {
|
|
1534
|
+
...delivery,
|
|
1535
|
+
status: "discarded",
|
|
1536
|
+
lease: null
|
|
1537
|
+
},
|
|
1538
|
+
updatedAt: now
|
|
1539
|
+
};
|
|
1540
|
+
yield {
|
|
1541
|
+
kind: "write",
|
|
1542
|
+
query: db.update(table).set({
|
|
1543
|
+
proposal: discarded,
|
|
1544
|
+
...discoveryMetadata(discarded),
|
|
1545
|
+
version: row.version + 1
|
|
1546
|
+
}).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.id, row.id), (0, import_drizzle_orm3.eq)(table.version, row.version)))
|
|
1547
|
+
};
|
|
1548
|
+
return {
|
|
1549
|
+
status: "discarded"
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
if (thread.activeStreamId !== null) return {
|
|
1553
|
+
status: "busy"
|
|
1554
|
+
};
|
|
1555
|
+
const seqRows = yield {
|
|
1556
|
+
kind: "read",
|
|
1557
|
+
query: db.select({
|
|
1558
|
+
last: (0, import_drizzle_orm3.max)(tables.agentMessage.seq)
|
|
1559
|
+
}).from(tables.agentMessage).where((0, import_drizzle_orm3.eq)(tables.agentMessage.threadId, thread.id))
|
|
1560
|
+
};
|
|
1561
|
+
const messageId = physicalId(command.outcomeId);
|
|
1562
|
+
yield {
|
|
1563
|
+
kind: "write",
|
|
1564
|
+
query: db.insert(tables.agentMessage).values({
|
|
1565
|
+
id: messageId,
|
|
1566
|
+
threadId: thread.id,
|
|
1567
|
+
role: "assistant",
|
|
1568
|
+
content: (0, import_nestjs_agent_core3.actionProposalOutcomeText)(outcome),
|
|
1569
|
+
actionProposalOutcome: (0, import_nestjs_agent_core3.canonicalActionProposalJson)(outcome),
|
|
1570
|
+
ui: null,
|
|
1571
|
+
seq: Number(seqRows[0]?.last ?? 0) + 1,
|
|
1572
|
+
createdAt: new Date(now)
|
|
1573
|
+
})
|
|
1574
|
+
};
|
|
1575
|
+
yield {
|
|
1576
|
+
kind: "write",
|
|
1577
|
+
query: db.update(tables.agentThread).set({
|
|
1578
|
+
updatedAt: new Date(now)
|
|
1579
|
+
}).where((0, import_drizzle_orm3.eq)(tables.agentThread.id, thread.id))
|
|
1580
|
+
};
|
|
1581
|
+
const admitted = {
|
|
1582
|
+
...proposal,
|
|
1583
|
+
outcomeDelivery: {
|
|
1584
|
+
...delivery,
|
|
1585
|
+
status: "admitted",
|
|
1586
|
+
lease: null,
|
|
1587
|
+
messageId
|
|
1588
|
+
},
|
|
1589
|
+
updatedAt: now
|
|
1590
|
+
};
|
|
1591
|
+
const update = db.update(table).set({
|
|
1592
|
+
proposal: admitted,
|
|
1593
|
+
...discoveryMetadata(admitted),
|
|
1594
|
+
version: row.version + 1
|
|
1595
|
+
}).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(table.id, row.id), (0, import_drizzle_orm3.eq)(table.version, row.version)));
|
|
1596
|
+
const changed = yield {
|
|
1597
|
+
kind: "read",
|
|
1598
|
+
query: this.dialect === "mysql" ? update : update.returning({
|
|
1599
|
+
id: table.id
|
|
1600
|
+
})
|
|
1601
|
+
};
|
|
1602
|
+
const count3 = this.dialect === "mysql" ? mysqlAffectedRows(changed) : changed.length;
|
|
1603
|
+
if (count3 !== 1) throw new Error("Outcome admission lost its locked proposal fence");
|
|
1604
|
+
return {
|
|
1605
|
+
status: "applied",
|
|
1606
|
+
messageId
|
|
1607
|
+
};
|
|
1608
|
+
}
|
|
1609
|
+
async read(scope, id) {
|
|
1610
|
+
const [row] = await this.db.select().from(this.table).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.table.id, physicalId(id)), (0, import_drizzle_orm3.eq)(this.table.scopeKey, scopeKey(scope)))).limit(1);
|
|
1611
|
+
return row && row.proposal.id === id && (0, import_nestjs_agent_core3.actionProposalScopeMatches)(row.proposal, scope) ? row : void 0;
|
|
1612
|
+
}
|
|
1613
|
+
async mutate(scope, id, transition) {
|
|
1614
|
+
for (let attempt = 0; attempt < 32; attempt++) {
|
|
1615
|
+
const row2 = await this.read(scope, id);
|
|
1616
|
+
if (!row2) return {
|
|
1617
|
+
status: "not_found"
|
|
1618
|
+
};
|
|
1619
|
+
const result = transition(row2.proposal, this.clock());
|
|
1620
|
+
if (!result.proposal || (0, import_nestjs_agent_core3.canonicalActionProposalJson)(result.proposal) === (0, import_nestjs_agent_core3.canonicalActionProposalJson)(row2.proposal)) return result;
|
|
1621
|
+
const changed = await affectedRows(this.dialect, this.db.update(this.table).set({
|
|
1622
|
+
proposal: result.proposal,
|
|
1623
|
+
decision: result.proposal.decision,
|
|
1624
|
+
...discoveryMetadata(result.proposal),
|
|
1625
|
+
version: row2.version + 1
|
|
1626
|
+
}).where((0, import_drizzle_orm3.and)((0, import_drizzle_orm3.eq)(this.table.id, physicalId(id)), (0, import_drizzle_orm3.eq)(this.table.scopeKey, scopeKey(scope)), (0, import_drizzle_orm3.eq)(this.table.version, row2.version))), this.table.id);
|
|
1627
|
+
if (changed === 1) return result;
|
|
1628
|
+
}
|
|
1629
|
+
const row = await this.read(scope, id);
|
|
1630
|
+
return row ? {
|
|
1631
|
+
status: "conflict",
|
|
1632
|
+
proposal: row.proposal
|
|
1633
|
+
} : {
|
|
1634
|
+
status: "not_found"
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
643
1637
|
};
|
|
1638
|
+
function scopeKey(scope) {
|
|
1639
|
+
return (0, import_node_crypto.createHash)("sha256").update((0, import_nestjs_agent_core3.canonicalActionProposalJson)([
|
|
1640
|
+
scope.tenantRef,
|
|
1641
|
+
scope.actorRef,
|
|
1642
|
+
scope.threadId
|
|
1643
|
+
])).digest("hex");
|
|
1644
|
+
}
|
|
1645
|
+
__name(scopeKey, "scopeKey");
|
|
1646
|
+
function physicalId(id) {
|
|
1647
|
+
return (0, import_node_crypto.createHash)("sha256").update((0, import_nestjs_agent_core3.canonicalActionProposalJson)(id)).digest("hex");
|
|
1648
|
+
}
|
|
1649
|
+
__name(physicalId, "physicalId");
|
|
1650
|
+
function logicalSort(id) {
|
|
1651
|
+
let sort = "";
|
|
1652
|
+
for (let index4 = 0; index4 < id.length; index4++) sort += id.charCodeAt(index4).toString(16).padStart(4, "0");
|
|
1653
|
+
return sort;
|
|
1654
|
+
}
|
|
1655
|
+
__name(logicalSort, "logicalSort");
|
|
1656
|
+
function discoveryMetadata(proposal) {
|
|
1657
|
+
return {
|
|
1658
|
+
replacementGroupKey: replacementGroupKey(proposal),
|
|
1659
|
+
deliveryStatus: proposal.outcomeDelivery?.status ?? null,
|
|
1660
|
+
deliveryLeaseExpiresAt: proposal.outcomeDelivery?.lease?.expiresAt ?? null,
|
|
1661
|
+
outcomeIdKey: proposal.outcome ? physicalId(proposal.outcome.id) : null,
|
|
1662
|
+
executionStatus: proposal.execution?.status ?? null,
|
|
1663
|
+
leaseExpiresAt: proposal.execution?.lease?.expiresAt ?? null,
|
|
1664
|
+
proposalExpiresAt: proposal.expiresAt,
|
|
1665
|
+
discoveryIndexVersion: 1
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
__name(discoveryMetadata, "discoveryMetadata");
|
|
1669
|
+
function lockQuery(query, dialect) {
|
|
1670
|
+
return dialect === "sqlite" ? query : query.for("update");
|
|
1671
|
+
}
|
|
1672
|
+
__name(lockQuery, "lockQuery");
|
|
1673
|
+
function runSync(steps) {
|
|
1674
|
+
let next = steps.next();
|
|
1675
|
+
while (!next.done) {
|
|
1676
|
+
const { kind, query } = next.value;
|
|
1677
|
+
const statement = query;
|
|
1678
|
+
next = steps.next(kind === "read" ? statement.all() : statement.run());
|
|
1679
|
+
}
|
|
1680
|
+
return next.value;
|
|
1681
|
+
}
|
|
1682
|
+
__name(runSync, "runSync");
|
|
1683
|
+
async function runAsync(steps) {
|
|
1684
|
+
let next = steps.next();
|
|
1685
|
+
while (!next.done) next = steps.next(await next.value.query);
|
|
1686
|
+
return next.value;
|
|
1687
|
+
}
|
|
1688
|
+
__name(runAsync, "runAsync");
|
|
1689
|
+
function replacementGroupKey(proposal) {
|
|
1690
|
+
return proposal.replacementKey ? physicalId((0, import_nestjs_agent_core3.canonicalActionProposalJson)([
|
|
1691
|
+
proposal.toolName,
|
|
1692
|
+
proposal.replacementKey
|
|
1693
|
+
])) : null;
|
|
1694
|
+
}
|
|
1695
|
+
__name(replacementGroupKey, "replacementGroupKey");
|
|
1696
|
+
|
|
1697
|
+
// src/drizzle-agent-store.ts
|
|
1698
|
+
function turnMessageColumns(message) {
|
|
1699
|
+
return {
|
|
1700
|
+
id: message.id,
|
|
1701
|
+
role: message.role,
|
|
1702
|
+
content: message.content,
|
|
1703
|
+
agentName: message.agentName,
|
|
1704
|
+
toolCalls: message.toolCalls,
|
|
1705
|
+
toolResults: message.toolResults,
|
|
1706
|
+
attachments: message.attachments,
|
|
1707
|
+
createdAt: message.createdAt
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1710
|
+
__name(turnMessageColumns, "turnMessageColumns");
|
|
644
1711
|
var DrizzleAgentStore = class {
|
|
645
1712
|
static {
|
|
646
1713
|
__name(this, "DrizzleAgentStore");
|
|
647
1714
|
}
|
|
648
1715
|
db;
|
|
649
|
-
|
|
650
|
-
|
|
1716
|
+
dialect;
|
|
1717
|
+
t;
|
|
1718
|
+
proposals;
|
|
1719
|
+
constructor(db, options = {}) {
|
|
1720
|
+
this.proposals = new DrizzleActionProposalStore(db, options);
|
|
1721
|
+
this.dialect = agentDialectOf(db);
|
|
1722
|
+
this.t = agentTablesFor(this.dialect);
|
|
1723
|
+
this.db = asBuilder(db);
|
|
1724
|
+
}
|
|
1725
|
+
createActionProposal(...args) {
|
|
1726
|
+
return this.proposals.createActionProposal(...args);
|
|
1727
|
+
}
|
|
1728
|
+
getActionProposal(...args) {
|
|
1729
|
+
return this.proposals.getActionProposal(...args);
|
|
1730
|
+
}
|
|
1731
|
+
listActionProposals(...args) {
|
|
1732
|
+
return this.proposals.listActionProposals(...args);
|
|
1733
|
+
}
|
|
1734
|
+
decideActionProposal(...args) {
|
|
1735
|
+
return this.proposals.decideActionProposal(...args);
|
|
1736
|
+
}
|
|
1737
|
+
claimActionProposal(...args) {
|
|
1738
|
+
return this.proposals.claimActionProposal(...args);
|
|
1739
|
+
}
|
|
1740
|
+
extendActionProposalLease(...args) {
|
|
1741
|
+
return this.proposals.extendActionProposalLease(...args);
|
|
1742
|
+
}
|
|
1743
|
+
claimNextActionProposal(...args) {
|
|
1744
|
+
return this.proposals.claimNextActionProposal(...args);
|
|
1745
|
+
}
|
|
1746
|
+
expireActionProposals(...args) {
|
|
1747
|
+
return this.proposals.expireActionProposals(...args);
|
|
1748
|
+
}
|
|
1749
|
+
backfillActionProposalDiscoveryIndex(command) {
|
|
1750
|
+
return this.proposals.backfillActionProposalDiscoveryIndex(command);
|
|
1751
|
+
}
|
|
1752
|
+
settleActionProposal(...args) {
|
|
1753
|
+
return this.proposals.settleActionProposal(...args);
|
|
1754
|
+
}
|
|
1755
|
+
createReplacingActionProposal(...args) {
|
|
1756
|
+
return this.proposals.createReplacingActionProposal(...args);
|
|
1757
|
+
}
|
|
1758
|
+
supersedeActionProposal(...args) {
|
|
1759
|
+
return this.proposals.supersedeActionProposal(...args);
|
|
1760
|
+
}
|
|
1761
|
+
get actionProposalAdmissionSupported() {
|
|
1762
|
+
return this.proposals.actionProposalAdmissionSupported;
|
|
1763
|
+
}
|
|
1764
|
+
getThreadActionProposalScope(threadId) {
|
|
1765
|
+
return this.proposals.getThreadActionProposalScope(threadId);
|
|
1766
|
+
}
|
|
1767
|
+
claimNextActionProposalOutcome(...args) {
|
|
1768
|
+
return this.proposals.claimNextActionProposalOutcome(...args);
|
|
1769
|
+
}
|
|
1770
|
+
admitActionProposalOutcome(...args) {
|
|
1771
|
+
return this.proposals.admitActionProposalOutcome(...args);
|
|
651
1772
|
}
|
|
652
1773
|
async createThread(input) {
|
|
653
1774
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -666,15 +1787,15 @@ var DrizzleAgentStore = class {
|
|
|
666
1787
|
updatedAt: now,
|
|
667
1788
|
deletedAt: null
|
|
668
1789
|
};
|
|
669
|
-
await this.db.insert(agentThread).values(thread);
|
|
1790
|
+
await this.db.insert(this.t.agentThread).values(thread);
|
|
670
1791
|
return this.toSummary(thread);
|
|
671
1792
|
}
|
|
672
1793
|
async getThread(threadId) {
|
|
673
|
-
const [thread] = await this.db.select().from(agentThread).where((0,
|
|
1794
|
+
const [thread] = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
674
1795
|
if (thread === void 0) {
|
|
675
1796
|
return null;
|
|
676
1797
|
}
|
|
677
|
-
const messages = await this.db.select().from(agentMessage).where((0,
|
|
1798
|
+
const messages = await this.db.select().from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, threadId)).orderBy(...this.messageOrder());
|
|
678
1799
|
const last = messages[messages.length - 1];
|
|
679
1800
|
const approvals = await this.approvalsFor(messages.map((message) => message.id));
|
|
680
1801
|
return {
|
|
@@ -707,16 +1828,16 @@ var DrizzleAgentStore = class {
|
|
|
707
1828
|
*/
|
|
708
1829
|
async loadThreadForTurn(query) {
|
|
709
1830
|
const [thread] = await this.db.select({
|
|
710
|
-
title: agentThread.title,
|
|
711
|
-
defaultAgent: agentThread.defaultAgent
|
|
712
|
-
}).from(agentThread).where((0,
|
|
1831
|
+
title: this.t.agentThread.title,
|
|
1832
|
+
defaultAgent: this.t.agentThread.defaultAgent
|
|
1833
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, query.threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
713
1834
|
if (thread === void 0) {
|
|
714
1835
|
return null;
|
|
715
1836
|
}
|
|
716
1837
|
const messages = await this.loadTurnWindow(query.threadId, query.messageLimit);
|
|
717
1838
|
const [answered] = await this.db.select({
|
|
718
|
-
id: agentMessage.id
|
|
719
|
-
}).from(agentMessage).where((0,
|
|
1839
|
+
id: this.t.agentMessage.id
|
|
1840
|
+
}).from(this.t.agentMessage).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, query.threadId), (0, import_drizzle_orm4.eq)(this.t.agentMessage.role, "assistant"))).limit(1);
|
|
720
1841
|
return {
|
|
721
1842
|
title: thread.title,
|
|
722
1843
|
defaultAgent: thread.defaultAgent,
|
|
@@ -732,30 +1853,30 @@ var DrizzleAgentStore = class {
|
|
|
732
1853
|
if (limit !== void 0 && limit <= 0) {
|
|
733
1854
|
return [];
|
|
734
1855
|
}
|
|
735
|
-
const window = this.db.select(
|
|
1856
|
+
const window = this.db.select(turnMessageColumns(this.t.agentMessage)).from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, threadId)).orderBy(...this.messageOrderNewestFirst());
|
|
736
1857
|
const rows = limit === void 0 ? await window : await window.limit(limit);
|
|
737
1858
|
return rows.reverse().map((row) => this.toStoredMessage(row));
|
|
738
1859
|
}
|
|
739
1860
|
async listThreads(actorRef, limit = 50) {
|
|
740
|
-
const threads = await this.db.select().from(agentThread).where((0,
|
|
1861
|
+
const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.actorRef, actorRef), (0, import_drizzle_orm4.eq)(this.t.agentThread.transient, false), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt))).orderBy((0, import_drizzle_orm4.desc)(this.t.agentThread.updatedAt)).limit(limit);
|
|
741
1862
|
const summaries = [];
|
|
742
1863
|
for (const thread of threads) {
|
|
743
|
-
const [last] = await this.db.select().from(agentMessage).where((0,
|
|
1864
|
+
const [last] = await this.db.select().from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, thread.id)).orderBy(...this.messageOrderNewestFirst()).limit(1);
|
|
744
1865
|
summaries.push(this.toSummary(thread, last?.content));
|
|
745
1866
|
}
|
|
746
1867
|
return summaries;
|
|
747
1868
|
}
|
|
748
1869
|
async softDeleteThread(threadId) {
|
|
749
|
-
await this.db.update(agentThread).set({
|
|
1870
|
+
await this.db.update(this.t.agentThread).set({
|
|
750
1871
|
deletedAt: /* @__PURE__ */ new Date()
|
|
751
|
-
}).where((0,
|
|
1872
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
752
1873
|
}
|
|
753
1874
|
async forkThread(threadId, fromMessageId) {
|
|
754
|
-
const [source] = await this.db.select().from(agentThread).where((0,
|
|
1875
|
+
const [source] = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
755
1876
|
if (source === void 0) {
|
|
756
1877
|
throw new Error(`thread ${threadId} not found`);
|
|
757
1878
|
}
|
|
758
|
-
const messages = await this.db.select().from(agentMessage).where((0,
|
|
1879
|
+
const messages = await this.db.select().from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, threadId)).orderBy(...this.messageOrder());
|
|
759
1880
|
const cutoff = messages.findIndex((message) => message.id === fromMessageId);
|
|
760
1881
|
const kept = cutoff >= 0 ? messages.slice(0, cutoff + 1) : messages;
|
|
761
1882
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -775,10 +1896,12 @@ var DrizzleAgentStore = class {
|
|
|
775
1896
|
updatedAt: now,
|
|
776
1897
|
deletedAt: null
|
|
777
1898
|
};
|
|
778
|
-
await this.db.insert(agentThread).values(fork);
|
|
1899
|
+
await this.db.insert(this.t.agentThread).values(fork);
|
|
779
1900
|
if (kept.length > 0) {
|
|
780
|
-
await this.db.insert(agentMessage).values(kept.map((message) => ({
|
|
1901
|
+
await this.db.insert(this.t.agentMessage).values(kept.map((message, index4) => ({
|
|
781
1902
|
id: crypto.randomUUID(),
|
|
1903
|
+
// Numbered afresh in the order just read, so the copy reads back in the original's order.
|
|
1904
|
+
seq: index4 + 1,
|
|
782
1905
|
threadId: fork.id,
|
|
783
1906
|
role: message.role,
|
|
784
1907
|
content: message.content,
|
|
@@ -796,6 +1919,7 @@ var DrizzleAgentStore = class {
|
|
|
796
1919
|
reasoning: message.reasoning,
|
|
797
1920
|
reasoningMs: message.reasoningMs,
|
|
798
1921
|
ui: message.ui,
|
|
1922
|
+
actionProposalOutcome: message.actionProposalOutcome,
|
|
799
1923
|
createdAt: message.createdAt
|
|
800
1924
|
})));
|
|
801
1925
|
}
|
|
@@ -803,21 +1927,21 @@ var DrizzleAgentStore = class {
|
|
|
803
1927
|
}
|
|
804
1928
|
async ownerOfThread(threadId) {
|
|
805
1929
|
const [thread] = await this.db.select({
|
|
806
|
-
actorRef: agentThread.actorRef
|
|
807
|
-
}).from(agentThread).where((0,
|
|
1930
|
+
actorRef: this.t.agentThread.actorRef
|
|
1931
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
808
1932
|
return thread?.actorRef ?? null;
|
|
809
1933
|
}
|
|
810
1934
|
async ownerOfToolCall(toolCallId) {
|
|
811
1935
|
const [row] = await this.db.select({
|
|
812
|
-
actorRef: agentThread.actorRef
|
|
813
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
1936
|
+
actorRef: this.t.agentThread.actorRef
|
|
1937
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm4.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm4.eq)(this.t.agentToolCall.id, toolCallId));
|
|
814
1938
|
return row?.actorRef ?? null;
|
|
815
1939
|
}
|
|
816
1940
|
async runForToolCall(toolCallId) {
|
|
817
1941
|
const [row] = await this.db.select({
|
|
818
|
-
runId: agentToolCall.runId,
|
|
819
|
-
activeStreamId: agentThread.activeStreamId
|
|
820
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
1942
|
+
runId: this.t.agentToolCall.runId,
|
|
1943
|
+
activeStreamId: this.t.agentThread.activeStreamId
|
|
1944
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm4.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm4.eq)(this.t.agentToolCall.id, toolCallId));
|
|
821
1945
|
return row?.runId ?? row?.activeStreamId ?? null;
|
|
822
1946
|
}
|
|
823
1947
|
/**
|
|
@@ -831,18 +1955,19 @@ var DrizzleAgentStore = class {
|
|
|
831
1955
|
return byMessage;
|
|
832
1956
|
}
|
|
833
1957
|
const rows = await this.db.select({
|
|
834
|
-
id: agentToolCall.id,
|
|
835
|
-
messageId: agentToolCall.messageId,
|
|
836
|
-
status: agentToolCall.status,
|
|
837
|
-
approver: agentToolCall.approver,
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
1958
|
+
id: this.t.agentToolCall.id,
|
|
1959
|
+
messageId: this.t.agentToolCall.messageId,
|
|
1960
|
+
status: this.t.agentToolCall.status,
|
|
1961
|
+
approver: this.t.agentToolCall.approver,
|
|
1962
|
+
confirmation: this.t.agentToolCall.confirmation,
|
|
1963
|
+
expiresAt: this.t.agentToolCall.expiresAt,
|
|
1964
|
+
remember: this.t.agentToolCall.remember,
|
|
1965
|
+
executedByRef: this.t.agentToolCall.executedByRef,
|
|
1966
|
+
decidedVia: this.t.agentToolCall.decidedVia,
|
|
1967
|
+
error: this.t.agentToolCall.error
|
|
1968
|
+
}).from(this.t.agentToolCall).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.inArray)(this.t.agentToolCall.messageId, messageIds), (0, import_drizzle_orm4.isNotNull)(this.t.agentToolCall.approver))).orderBy((0, import_drizzle_orm4.asc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm4.asc)(this.t.agentToolCall.id));
|
|
844
1969
|
for (const row of rows) {
|
|
845
|
-
const approval = (0,
|
|
1970
|
+
const approval = (0, import_nestjs_agent_core4.toolCallApprovalFromRow)({
|
|
846
1971
|
...row,
|
|
847
1972
|
toolCallId: row.id
|
|
848
1973
|
});
|
|
@@ -858,22 +1983,29 @@ var DrizzleAgentStore = class {
|
|
|
858
1983
|
/** Tools whose approval someone asked to remember in this thread. */
|
|
859
1984
|
async rememberedApprovals(threadId) {
|
|
860
1985
|
const rows = await this.db.selectDistinct({
|
|
861
|
-
toolName: agentToolCall.toolName
|
|
862
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
863
|
-
|
|
1986
|
+
toolName: this.t.agentToolCall.toolName
|
|
1987
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm4.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, threadId), (0, import_drizzle_orm4.eq)(this.t.agentToolCall.remember, true)));
|
|
1988
|
+
const scope = await this.getThreadActionProposalScope(threadId);
|
|
1989
|
+
const proposalNames = scope ? await this.proposals.rememberedActionProposalApprovals(scope) : [];
|
|
1990
|
+
return [
|
|
1991
|
+
.../* @__PURE__ */ new Set([
|
|
1992
|
+
...rows.map((row) => row.toolName),
|
|
1993
|
+
...proposalNames
|
|
1994
|
+
])
|
|
1995
|
+
];
|
|
864
1996
|
}
|
|
865
1997
|
async toolCallInput(toolCallId) {
|
|
866
1998
|
const [row] = await this.db.select({
|
|
867
|
-
input: agentToolCall.input
|
|
868
|
-
}).from(agentToolCall).where((0,
|
|
1999
|
+
input: this.t.agentToolCall.input
|
|
2000
|
+
}).from(this.t.agentToolCall).where((0, import_drizzle_orm4.eq)(this.t.agentToolCall.id, toolCallId));
|
|
869
2001
|
return row?.input ?? null;
|
|
870
2002
|
}
|
|
871
2003
|
async toolCallApproval(toolCallId) {
|
|
872
2004
|
const [row] = await this.db.select({
|
|
873
|
-
status: agentToolCall.status,
|
|
874
|
-
approver: agentToolCall.approver,
|
|
875
|
-
expiresAt: agentToolCall.expiresAt
|
|
876
|
-
}).from(agentToolCall).where((0,
|
|
2005
|
+
status: this.t.agentToolCall.status,
|
|
2006
|
+
approver: this.t.agentToolCall.approver,
|
|
2007
|
+
expiresAt: this.t.agentToolCall.expiresAt
|
|
2008
|
+
}).from(this.t.agentToolCall).where((0, import_drizzle_orm4.eq)(this.t.agentToolCall.id, toolCallId));
|
|
877
2009
|
if (row === void 0) {
|
|
878
2010
|
return null;
|
|
879
2011
|
}
|
|
@@ -885,27 +2017,67 @@ var DrizzleAgentStore = class {
|
|
|
885
2017
|
}
|
|
886
2018
|
async ownerOfActiveStream(runId) {
|
|
887
2019
|
const [thread] = await this.db.select({
|
|
888
|
-
actorRef: agentThread.actorRef
|
|
889
|
-
}).from(agentThread).where((0,
|
|
2020
|
+
actorRef: this.t.agentThread.actorRef
|
|
2021
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.activeStreamId, runId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
890
2022
|
return thread?.actorRef ?? null;
|
|
891
2023
|
}
|
|
892
2024
|
async setTitle(threadId, title) {
|
|
893
|
-
await this.db.update(agentThread).set({
|
|
2025
|
+
await this.db.update(this.t.agentThread).set({
|
|
894
2026
|
title,
|
|
895
2027
|
updatedAt: /* @__PURE__ */ new Date()
|
|
896
|
-
}).where((0,
|
|
2028
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
897
2029
|
}
|
|
898
2030
|
async promoteThread(threadId) {
|
|
899
|
-
await this.db.update(agentThread).set({
|
|
2031
|
+
await this.db.update(this.t.agentThread).set({
|
|
900
2032
|
transient: false,
|
|
901
2033
|
updatedAt: /* @__PURE__ */ new Date()
|
|
902
|
-
}).where((0,
|
|
2034
|
+
}).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.eq)(this.t.agentThread.transient, true)));
|
|
2035
|
+
}
|
|
2036
|
+
/** Who holds the thread's stream right now, deleted or not. */
|
|
2037
|
+
async activeHolder(threadId) {
|
|
2038
|
+
const [thread] = await this.db.select({
|
|
2039
|
+
activeStreamId: this.t.agentThread.activeStreamId
|
|
2040
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
2041
|
+
return thread?.activeStreamId ?? null;
|
|
2042
|
+
}
|
|
2043
|
+
/**
|
|
2044
|
+
* A thread's messages in the order they were appended: by `seq`, which append assigns. A row from
|
|
2045
|
+
* before `seq` existed has none and counts as 0 — first, since every such row is older than every
|
|
2046
|
+
* numbered one — and those order among themselves by `created_at`, then `id`, as they always did.
|
|
2047
|
+
* `coalesce` rather than `nulls first`, which MySQL cannot spell and Postgres defaults the other way.
|
|
2048
|
+
*/
|
|
2049
|
+
messageOrder() {
|
|
2050
|
+
const message = this.t.agentMessage;
|
|
2051
|
+
return [
|
|
2052
|
+
(0, import_drizzle_orm4.asc)(import_drizzle_orm4.sql`coalesce(${message.seq}, 0)`),
|
|
2053
|
+
(0, import_drizzle_orm4.asc)(message.createdAt),
|
|
2054
|
+
(0, import_drizzle_orm4.asc)(message.id)
|
|
2055
|
+
];
|
|
2056
|
+
}
|
|
2057
|
+
/** {@link messageOrder}, newest first. */
|
|
2058
|
+
messageOrderNewestFirst() {
|
|
2059
|
+
const message = this.t.agentMessage;
|
|
2060
|
+
return [
|
|
2061
|
+
(0, import_drizzle_orm4.desc)(import_drizzle_orm4.sql`coalesce(${message.seq}, 0)`),
|
|
2062
|
+
(0, import_drizzle_orm4.desc)(message.createdAt),
|
|
2063
|
+
(0, import_drizzle_orm4.desc)(message.id)
|
|
2064
|
+
];
|
|
2065
|
+
}
|
|
2066
|
+
/**
|
|
2067
|
+
* The next `seq` in a thread: one past its highest. Two appends racing on ONE thread can draw the
|
|
2068
|
+
* same number — the turn loop never does that, and the tie then falls back to `created_at`, `id`.
|
|
2069
|
+
*/
|
|
2070
|
+
async nextMessageSeq(threadId) {
|
|
2071
|
+
const [row] = await this.db.select({
|
|
2072
|
+
last: (0, import_drizzle_orm4.max)(this.t.agentMessage.seq)
|
|
2073
|
+
}).from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, threadId));
|
|
2074
|
+
return Number(row?.last ?? 0) + 1;
|
|
903
2075
|
}
|
|
904
2076
|
/** The run streaming the thread, or `null` — a projection of the one column. */
|
|
905
2077
|
async activeRunForThread(threadId) {
|
|
906
2078
|
const [thread] = await this.db.select({
|
|
907
|
-
activeStreamId: agentThread.activeStreamId
|
|
908
|
-
}).from(agentThread).where((0,
|
|
2079
|
+
activeStreamId: this.t.agentThread.activeStreamId
|
|
2080
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
909
2081
|
return thread?.activeStreamId ?? null;
|
|
910
2082
|
}
|
|
911
2083
|
/**
|
|
@@ -914,39 +2086,39 @@ var DrizzleAgentStore = class {
|
|
|
914
2086
|
*/
|
|
915
2087
|
async claimActiveStream(threadId, runId, options = {}) {
|
|
916
2088
|
const holders = [
|
|
917
|
-
(0,
|
|
918
|
-
(0,
|
|
2089
|
+
(0, import_drizzle_orm4.isNull)(this.t.agentThread.activeStreamId),
|
|
2090
|
+
(0, import_drizzle_orm4.eq)(this.t.agentThread.activeStreamId, runId),
|
|
919
2091
|
...options.replacing !== void 0 ? [
|
|
920
|
-
(0,
|
|
2092
|
+
(0, import_drizzle_orm4.eq)(this.t.agentThread.activeStreamId, options.replacing)
|
|
921
2093
|
] : []
|
|
922
2094
|
];
|
|
923
|
-
const claimed = await this.db.update(agentThread).set({
|
|
2095
|
+
const claimed = await affectedRows(this.dialect, this.db.update(this.t.agentThread).set({
|
|
924
2096
|
activeStreamId: runId
|
|
925
|
-
}).where((0,
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
2097
|
+
}).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.or)(...holders))), this.t.agentThread.id);
|
|
2098
|
+
if (claimed > 0) {
|
|
2099
|
+
return true;
|
|
2100
|
+
}
|
|
2101
|
+
return await this.activeHolder(threadId) === runId;
|
|
929
2102
|
}
|
|
930
2103
|
async releaseActiveStream(threadId, runId) {
|
|
931
|
-
const released = await this.db.update(agentThread).set({
|
|
2104
|
+
const released = await affectedRows(this.dialect, this.db.update(this.t.agentThread).set({
|
|
932
2105
|
activeStreamId: null
|
|
933
|
-
}).where((0,
|
|
934
|
-
|
|
935
|
-
});
|
|
936
|
-
return released.length > 0;
|
|
2106
|
+
}).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.eq)(this.t.agentThread.activeStreamId, runId))), this.t.agentThread.id);
|
|
2107
|
+
return released > 0;
|
|
937
2108
|
}
|
|
938
2109
|
async enqueueMessage(input) {
|
|
939
2110
|
const now = /* @__PURE__ */ new Date();
|
|
940
2111
|
const [edge] = await this.db.select({
|
|
941
|
-
first: (0,
|
|
942
|
-
last: (0,
|
|
943
|
-
}).from(agentQueuedMessage).where((0,
|
|
2112
|
+
first: (0, import_drizzle_orm4.min)(this.t.agentQueuedMessage.position),
|
|
2113
|
+
last: (0, import_drizzle_orm4.max)(this.t.agentQueuedMessage.position)
|
|
2114
|
+
}).from(this.t.agentQueuedMessage).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.threadId, input.threadId));
|
|
944
2115
|
const position = input.at === "head" ? (edge?.first ?? 1) - 1 : (edge?.last ?? -1) + 1;
|
|
945
2116
|
const row = {
|
|
946
2117
|
id: crypto.randomUUID(),
|
|
947
2118
|
threadId: input.threadId,
|
|
948
2119
|
actor: input.actor,
|
|
949
2120
|
content: input.content,
|
|
2121
|
+
uiCapabilities: input.uiCapabilities ?? null,
|
|
950
2122
|
attachments: input.attachments !== void 0 && input.attachments.length > 0 ? input.attachments : null,
|
|
951
2123
|
agentName: input.agentName ?? null,
|
|
952
2124
|
persona: input.persona ?? null,
|
|
@@ -957,15 +2129,15 @@ var DrizzleAgentStore = class {
|
|
|
957
2129
|
createdAt: now,
|
|
958
2130
|
updatedAt: now
|
|
959
2131
|
};
|
|
960
|
-
await this.db.insert(agentQueuedMessage).values(row);
|
|
2132
|
+
await this.db.insert(this.t.agentQueuedMessage).values(row);
|
|
961
2133
|
return toQueuedMessage(row);
|
|
962
2134
|
}
|
|
963
2135
|
async listQueue(threadId) {
|
|
964
|
-
const rows = await this.db.select().from(agentQueuedMessage).where((0,
|
|
2136
|
+
const rows = await this.db.select().from(this.t.agentQueuedMessage).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.threadId, threadId)).orderBy((0, import_drizzle_orm4.asc)(this.t.agentQueuedMessage.position), (0, import_drizzle_orm4.asc)(this.t.agentQueuedMessage.createdAt));
|
|
965
2137
|
return rows.map(toQueuedMessage);
|
|
966
2138
|
}
|
|
967
2139
|
async getQueuedMessage(id) {
|
|
968
|
-
const [row] = await this.db.select().from(agentQueuedMessage).where((0,
|
|
2140
|
+
const [row] = await this.db.select().from(this.t.agentQueuedMessage).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.id, id));
|
|
969
2141
|
return row === void 0 ? null : toQueuedMessage(row);
|
|
970
2142
|
}
|
|
971
2143
|
async updateQueuedMessage(id, patch) {
|
|
@@ -981,52 +2153,47 @@ var DrizzleAgentStore = class {
|
|
|
981
2153
|
if (patch.interrupt !== void 0) {
|
|
982
2154
|
updates.interrupt = patch.interrupt;
|
|
983
2155
|
}
|
|
984
|
-
await this.db.update(agentQueuedMessage).set(updates).where((0,
|
|
2156
|
+
await this.db.update(this.t.agentQueuedMessage).set(updates).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.id, id));
|
|
985
2157
|
return this.getQueuedMessage(id);
|
|
986
2158
|
}
|
|
987
2159
|
/** Rewrites the thread's positions as 0..n-1 in the new order — a queue is a handful of rows. */
|
|
988
|
-
async moveQueuedMessage(id,
|
|
2160
|
+
async moveQueuedMessage(id, index4) {
|
|
989
2161
|
const moving = await this.getQueuedMessage(id);
|
|
990
2162
|
if (moving === null) {
|
|
991
2163
|
return false;
|
|
992
2164
|
}
|
|
993
2165
|
const order = (await this.listQueue(moving.threadId)).filter((message) => message.id !== id);
|
|
994
|
-
const target = Math.max(0, Math.min(order.length, Math.trunc(
|
|
2166
|
+
const target = Math.max(0, Math.min(order.length, Math.trunc(index4)));
|
|
995
2167
|
order.splice(target, 0, moving);
|
|
996
2168
|
for (const [position, message] of order.entries()) {
|
|
997
|
-
await this.db.update(agentQueuedMessage).set({
|
|
2169
|
+
await this.db.update(this.t.agentQueuedMessage).set({
|
|
998
2170
|
position
|
|
999
|
-
}).where((0,
|
|
2171
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.id, message.id));
|
|
1000
2172
|
}
|
|
1001
2173
|
return true;
|
|
1002
2174
|
}
|
|
1003
2175
|
async removeQueuedMessage(id) {
|
|
1004
|
-
const removed = await this.db.delete(agentQueuedMessage).where((0,
|
|
1005
|
-
|
|
1006
|
-
});
|
|
1007
|
-
return removed.length > 0;
|
|
2176
|
+
const removed = await affectedRows(this.dialect, this.db.delete(this.t.agentQueuedMessage).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.id, id)), this.t.agentQueuedMessage.id);
|
|
2177
|
+
return removed > 0;
|
|
1008
2178
|
}
|
|
1009
2179
|
async clearQueue(threadId) {
|
|
1010
|
-
|
|
1011
|
-
id: agentQueuedMessage.id
|
|
1012
|
-
});
|
|
1013
|
-
return removed.length;
|
|
2180
|
+
return affectedRows(this.dialect, this.db.delete(this.t.agentQueuedMessage).where((0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.threadId, threadId)), this.t.agentQueuedMessage.id);
|
|
1014
2181
|
}
|
|
1015
2182
|
async queuePause(threadId) {
|
|
1016
2183
|
const [thread] = await this.db.select({
|
|
1017
|
-
queuePause: agentThread.queuePause
|
|
1018
|
-
}).from(agentThread).where((0,
|
|
2184
|
+
queuePause: this.t.agentThread.queuePause
|
|
2185
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
1019
2186
|
return thread?.queuePause ?? null;
|
|
1020
2187
|
}
|
|
1021
2188
|
async setQueuePause(threadId, pause) {
|
|
1022
|
-
await this.db.update(agentThread).set({
|
|
2189
|
+
await this.db.update(this.t.agentThread).set({
|
|
1023
2190
|
queuePause: pause
|
|
1024
|
-
}).where((0,
|
|
2191
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
1025
2192
|
}
|
|
1026
2193
|
async setActiveStream(threadId, runId) {
|
|
1027
|
-
await this.db.update(agentThread).set({
|
|
2194
|
+
await this.db.update(this.t.agentThread).set({
|
|
1028
2195
|
activeStreamId: runId
|
|
1029
|
-
}).where((0,
|
|
2196
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
1030
2197
|
}
|
|
1031
2198
|
/**
|
|
1032
2199
|
* Patch a thread's `title` and/or `defaultAgent`. Each field is only touched when PRESENT in
|
|
@@ -1052,7 +2219,7 @@ var DrizzleAgentStore = class {
|
|
|
1052
2219
|
return;
|
|
1053
2220
|
}
|
|
1054
2221
|
updates.updatedAt = /* @__PURE__ */ new Date();
|
|
1055
|
-
await this.db.update(agentThread).set(updates).where((0,
|
|
2222
|
+
await this.db.update(this.t.agentThread).set(updates).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId));
|
|
1056
2223
|
}
|
|
1057
2224
|
/**
|
|
1058
2225
|
* The thread's default agent, projected — the caller wants one nullable scalar to decide which
|
|
@@ -1062,21 +2229,21 @@ var DrizzleAgentStore = class {
|
|
|
1062
2229
|
/** The thread's pinned model, projected like {@link defaultAgentForThread}. */
|
|
1063
2230
|
async modelForThread(threadId) {
|
|
1064
2231
|
const [thread] = await this.db.select({
|
|
1065
|
-
model: agentThread.model
|
|
1066
|
-
}).from(agentThread).where((0,
|
|
2232
|
+
model: this.t.agentThread.model
|
|
2233
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
1067
2234
|
return thread?.model ?? null;
|
|
1068
2235
|
}
|
|
1069
2236
|
/** The thread's pinned persona, projected like {@link defaultAgentForThread}. */
|
|
1070
2237
|
async personaForThread(threadId) {
|
|
1071
2238
|
const [thread] = await this.db.select({
|
|
1072
|
-
persona: agentThread.persona
|
|
1073
|
-
}).from(agentThread).where((0,
|
|
2239
|
+
persona: this.t.agentThread.persona
|
|
2240
|
+
}).from(agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
1074
2241
|
return thread?.persona ?? null;
|
|
1075
2242
|
}
|
|
1076
2243
|
async defaultAgentForThread(threadId) {
|
|
1077
2244
|
const [thread] = await this.db.select({
|
|
1078
|
-
defaultAgent: agentThread.defaultAgent
|
|
1079
|
-
}).from(agentThread).where((0,
|
|
2245
|
+
defaultAgent: this.t.agentThread.defaultAgent
|
|
2246
|
+
}).from(this.t.agentThread).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.id, threadId), (0, import_drizzle_orm4.isNull)(this.t.agentThread.deletedAt)));
|
|
1080
2247
|
return thread?.defaultAgent ?? null;
|
|
1081
2248
|
}
|
|
1082
2249
|
/**
|
|
@@ -1101,7 +2268,7 @@ var DrizzleAgentStore = class {
|
|
|
1101
2268
|
promptHash: run.promptHash ?? null,
|
|
1102
2269
|
parentRunId: run.parentRunId ?? null
|
|
1103
2270
|
};
|
|
1104
|
-
await this.db.insert(agentRun).values(runRow);
|
|
2271
|
+
await this.db.insert(this.t.agentRun).values(runRow);
|
|
1105
2272
|
}
|
|
1106
2273
|
/** Settle a run's outcome. A no-op when the run is unknown (mirrors `setTitle`/`updateThread`). */
|
|
1107
2274
|
async recordRunEnd(end) {
|
|
@@ -1118,16 +2285,16 @@ var DrizzleAgentStore = class {
|
|
|
1118
2285
|
if (end.errorMessage !== void 0) {
|
|
1119
2286
|
updates.errorMessage = end.errorMessage;
|
|
1120
2287
|
}
|
|
1121
|
-
await this.db.update(agentRun).set(updates).where((0,
|
|
2288
|
+
await this.db.update(this.t.agentRun).set(updates).where((0, import_drizzle_orm4.eq)(this.t.agentRun.id, end.runId));
|
|
1122
2289
|
}
|
|
1123
2290
|
/** Bump the run's llm-step retry counter. Atomic `retries = retries + 1`, no read-modify-write. */
|
|
1124
2291
|
async bumpRunRetries(runId) {
|
|
1125
|
-
await this.db.update(agentRun).set({
|
|
1126
|
-
retries:
|
|
1127
|
-
}).where((0,
|
|
2292
|
+
await this.db.update(this.t.agentRun).set({
|
|
2293
|
+
retries: import_drizzle_orm4.sql`${this.t.agentRun.retries} + 1`
|
|
2294
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentRun.id, runId));
|
|
1128
2295
|
}
|
|
1129
2296
|
async appendMessage(input) {
|
|
1130
|
-
const [thread] = await this.db.select().from(agentThread).where((0,
|
|
2297
|
+
const [thread] = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, input.threadId));
|
|
1131
2298
|
if (thread === void 0) {
|
|
1132
2299
|
throw new Error(`thread ${input.threadId} not found`);
|
|
1133
2300
|
}
|
|
@@ -1137,6 +2304,7 @@ var DrizzleAgentStore = class {
|
|
|
1137
2304
|
threadId: input.threadId,
|
|
1138
2305
|
role: input.role,
|
|
1139
2306
|
content: input.content,
|
|
2307
|
+
actionProposalOutcome: input.actionProposalOutcome ? (0, import_nestjs_agent_core4.canonicalActionProposalJson)(input.actionProposalOutcome) : null,
|
|
1140
2308
|
toolCalls: input.toolCalls ?? null,
|
|
1141
2309
|
toolResults: input.toolResults ?? null,
|
|
1142
2310
|
attachments: input.attachments ?? null,
|
|
@@ -1149,48 +2317,50 @@ var DrizzleAgentStore = class {
|
|
|
1149
2317
|
reasoningMs: input.reasoningMs ?? null,
|
|
1150
2318
|
ui: input.ui ?? null,
|
|
1151
2319
|
feedback: null,
|
|
2320
|
+
seq: await this.nextMessageSeq(input.threadId),
|
|
1152
2321
|
createdAt: now
|
|
1153
2322
|
};
|
|
1154
|
-
await this.db.insert(agentMessage).values(message);
|
|
1155
|
-
await this.db.update(agentThread).set({
|
|
2323
|
+
await this.db.insert(this.t.agentMessage).values(message);
|
|
2324
|
+
await this.db.update(this.t.agentThread).set({
|
|
1156
2325
|
updatedAt: now
|
|
1157
|
-
}).where((0,
|
|
2326
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentThread.id, input.threadId));
|
|
1158
2327
|
return this.toStoredMessage(message);
|
|
1159
2328
|
}
|
|
1160
2329
|
async setMessageToolResults(messageId, results) {
|
|
1161
|
-
await this.db.update(agentMessage).set({
|
|
2330
|
+
await this.db.update(this.t.agentMessage).set({
|
|
1162
2331
|
toolResults: results
|
|
1163
|
-
}).where((0,
|
|
2332
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.id, messageId));
|
|
1164
2333
|
}
|
|
1165
2334
|
async setMessageUi(messageId, ui) {
|
|
1166
|
-
await this.db.update(agentMessage).set({
|
|
2335
|
+
await this.db.update(this.t.agentMessage).set({
|
|
1167
2336
|
ui: ui.length > 0 ? ui : null
|
|
1168
|
-
}).where((0,
|
|
2337
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.id, messageId));
|
|
1169
2338
|
}
|
|
1170
2339
|
async threadOfMessage(messageId) {
|
|
1171
2340
|
const [row] = await this.db.select({
|
|
1172
|
-
threadId: agentMessage.threadId
|
|
1173
|
-
}).from(agentMessage).where((0,
|
|
2341
|
+
threadId: this.t.agentMessage.threadId
|
|
2342
|
+
}).from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.id, messageId)).limit(1);
|
|
1174
2343
|
return row?.threadId ?? null;
|
|
1175
2344
|
}
|
|
1176
2345
|
async setMessageFeedback(messageId, feedback) {
|
|
1177
|
-
await this.db.update(agentMessage).set({
|
|
2346
|
+
await this.db.update(this.t.agentMessage).set({
|
|
1178
2347
|
feedback
|
|
1179
|
-
}).where((0,
|
|
2348
|
+
}).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.id, messageId));
|
|
1180
2349
|
}
|
|
1181
2350
|
async truncateFrom(threadId, messageId) {
|
|
1182
|
-
const messages = await this.db.select().from(agentMessage).where((0,
|
|
2351
|
+
const messages = await this.db.select().from(this.t.agentMessage).where((0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, threadId)).orderBy(...this.messageOrder());
|
|
1183
2352
|
const cutoff = messages.findIndex((message) => message.id === messageId);
|
|
1184
2353
|
if (cutoff < 0) {
|
|
1185
2354
|
return;
|
|
1186
2355
|
}
|
|
1187
2356
|
const doomedIds = messages.slice(cutoff).map((message) => message.id);
|
|
1188
|
-
await this.db.delete(agentToolCall).where((0,
|
|
1189
|
-
await this.db.delete(agentMessage).where((0,
|
|
2357
|
+
await this.db.delete(this.t.agentToolCall).where((0, import_drizzle_orm4.inArray)(this.t.agentToolCall.messageId, doomedIds));
|
|
2358
|
+
await this.db.delete(this.t.agentMessage).where((0, import_drizzle_orm4.inArray)(this.t.agentMessage.id, doomedIds));
|
|
1190
2359
|
}
|
|
1191
2360
|
async recordToolCall(input) {
|
|
1192
|
-
await this.db.insert(agentToolCall).values({
|
|
2361
|
+
await this.db.insert(this.t.agentToolCall).values({
|
|
1193
2362
|
id: input.toolCallId,
|
|
2363
|
+
proposalId: input.proposalId ?? null,
|
|
1194
2364
|
messageId: input.messageId,
|
|
1195
2365
|
toolName: input.toolName,
|
|
1196
2366
|
toolType: input.toolType,
|
|
@@ -1204,6 +2374,7 @@ var DrizzleAgentStore = class {
|
|
|
1204
2374
|
executedAt: null,
|
|
1205
2375
|
runId: input.runId ?? null,
|
|
1206
2376
|
approver: input.approver ?? null,
|
|
2377
|
+
confirmation: input.confirmation ?? null,
|
|
1207
2378
|
expiresAt: input.expiresAt !== void 0 ? new Date(input.expiresAt) : null,
|
|
1208
2379
|
remember: null,
|
|
1209
2380
|
decidedVia: null
|
|
@@ -1234,18 +2405,18 @@ var DrizzleAgentStore = class {
|
|
|
1234
2405
|
if (input.status === "executed" || input.status === "auto_executed") {
|
|
1235
2406
|
updates.executedAt = /* @__PURE__ */ new Date();
|
|
1236
2407
|
}
|
|
1237
|
-
await this.db.update(agentToolCall).set(updates).where((0,
|
|
2408
|
+
await this.db.update(this.t.agentToolCall).set(updates).where((0, import_drizzle_orm4.eq)(this.t.agentToolCall.id, input.toolCallId));
|
|
1238
2409
|
}
|
|
1239
2410
|
async toolCallOutcomes(toolCallIds) {
|
|
1240
2411
|
if (toolCallIds.length === 0) {
|
|
1241
2412
|
return [];
|
|
1242
2413
|
}
|
|
1243
2414
|
const rows = await this.db.select({
|
|
1244
|
-
id: agentToolCall.id,
|
|
1245
|
-
status: agentToolCall.status,
|
|
1246
|
-
output: agentToolCall.output,
|
|
1247
|
-
error: agentToolCall.error
|
|
1248
|
-
}).from(agentToolCall).where((0,
|
|
2415
|
+
id: this.t.agentToolCall.id,
|
|
2416
|
+
status: this.t.agentToolCall.status,
|
|
2417
|
+
output: this.t.agentToolCall.output,
|
|
2418
|
+
error: this.t.agentToolCall.error
|
|
2419
|
+
}).from(this.t.agentToolCall).where((0, import_drizzle_orm4.inArray)(this.t.agentToolCall.id, [
|
|
1249
2420
|
...toolCallIds
|
|
1250
2421
|
]));
|
|
1251
2422
|
return rows.map((row) => ({
|
|
@@ -1260,14 +2431,14 @@ var DrizzleAgentStore = class {
|
|
|
1260
2431
|
}));
|
|
1261
2432
|
}
|
|
1262
2433
|
async failUnsettledToolCalls(runId, error) {
|
|
1263
|
-
const pending = (0,
|
|
2434
|
+
const pending = (0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentToolCall.runId, runId), (0, import_drizzle_orm4.eq)(this.t.agentToolCall.status, "pending_approval"));
|
|
1264
2435
|
const rows = await this.db.select({
|
|
1265
|
-
id: agentToolCall.id
|
|
1266
|
-
}).from(agentToolCall).where(pending);
|
|
2436
|
+
id: this.t.agentToolCall.id
|
|
2437
|
+
}).from(this.t.agentToolCall).where(pending);
|
|
1267
2438
|
if (rows.length === 0) {
|
|
1268
2439
|
return 0;
|
|
1269
2440
|
}
|
|
1270
|
-
await this.db.update(agentToolCall).set({
|
|
2441
|
+
await this.db.update(this.t.agentToolCall).set({
|
|
1271
2442
|
status: "failed",
|
|
1272
2443
|
error
|
|
1273
2444
|
}).where(pending);
|
|
@@ -1292,11 +2463,11 @@ var DrizzleAgentStore = class {
|
|
|
1292
2463
|
return [];
|
|
1293
2464
|
}
|
|
1294
2465
|
const rows = await this.db.select({
|
|
1295
|
-
attachments: agentMessage.attachments
|
|
1296
|
-
}).from(agentMessage).innerJoin(agentThread, (0,
|
|
2466
|
+
attachments: this.t.agentMessage.attachments
|
|
2467
|
+
}).from(this.t.agentMessage).innerJoin(this.t.agentThread, (0, import_drizzle_orm4.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.actorRef, actorRef), (0, import_drizzle_orm4.isNotNull)(this.t.agentMessage.attachments)));
|
|
1297
2468
|
const queued = await this.db.select({
|
|
1298
|
-
attachments: agentQueuedMessage.attachments
|
|
1299
|
-
}).from(agentQueuedMessage).innerJoin(agentThread, (0,
|
|
2469
|
+
attachments: this.t.agentQueuedMessage.attachments
|
|
2470
|
+
}).from(this.t.agentQueuedMessage).innerJoin(this.t.agentThread, (0, import_drizzle_orm4.eq)(this.t.agentQueuedMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentThread.actorRef, actorRef), (0, import_drizzle_orm4.isNotNull)(this.t.agentQueuedMessage.attachments)));
|
|
1300
2471
|
const wanted = new Set(mediaIds);
|
|
1301
2472
|
const found = /* @__PURE__ */ new Set();
|
|
1302
2473
|
for (const row of [
|
|
@@ -1314,7 +2485,7 @@ var DrizzleAgentStore = class {
|
|
|
1314
2485
|
].filter((mediaId) => found.has(mediaId));
|
|
1315
2486
|
}
|
|
1316
2487
|
async recordUsage(input) {
|
|
1317
|
-
await this.db.insert(agentTokenUsage).values({
|
|
2488
|
+
await this.db.insert(this.t.agentTokenUsage).values({
|
|
1318
2489
|
id: crypto.randomUUID(),
|
|
1319
2490
|
threadId: input.threadId,
|
|
1320
2491
|
actorRef: input.actorRef,
|
|
@@ -1335,7 +2506,7 @@ var DrizzleAgentStore = class {
|
|
|
1335
2506
|
async usageBetween(actorRef, fromDay, toDay) {
|
|
1336
2507
|
const start = /* @__PURE__ */ new Date(`${fromDay}T00:00:00.000Z`);
|
|
1337
2508
|
const end = /* @__PURE__ */ new Date(`${toDay}T23:59:59.999Z`);
|
|
1338
|
-
const rows = await this.db.select().from(agentTokenUsage).where((0,
|
|
2509
|
+
const rows = await this.db.select().from(this.t.agentTokenUsage).where((0, import_drizzle_orm4.and)((0, import_drizzle_orm4.eq)(this.t.agentTokenUsage.actorRef, actorRef), (0, import_drizzle_orm4.gte)(this.t.agentTokenUsage.createdAt, start), (0, import_drizzle_orm4.lte)(this.t.agentTokenUsage.createdAt, end)));
|
|
1339
2510
|
const usedTokens = rows.reduce((sum2, row) => sum2 + row.inputTokens + row.outputTokens, 0);
|
|
1340
2511
|
const costUsd = rows.reduce((sum2, row) => sum2 + (row.costUsd ?? 0), 0);
|
|
1341
2512
|
return {
|
|
@@ -1361,7 +2532,12 @@ var DrizzleAgentStore = class {
|
|
|
1361
2532
|
};
|
|
1362
2533
|
}
|
|
1363
2534
|
toStoredMessage(message) {
|
|
2535
|
+
const outcome = message.actionProposalOutcome ? JSON.parse(message.actionProposalOutcome) : void 0;
|
|
1364
2536
|
return {
|
|
2537
|
+
...outcome ? {
|
|
2538
|
+
actionProposalOutcome: outcome,
|
|
2539
|
+
ui: outcome.ui
|
|
2540
|
+
} : {},
|
|
1365
2541
|
id: message.id,
|
|
1366
2542
|
role: message.role,
|
|
1367
2543
|
content: message.content,
|
|
@@ -1407,6 +2583,9 @@ var DrizzleAgentStore = class {
|
|
|
1407
2583
|
};
|
|
1408
2584
|
function toQueuedMessage(row) {
|
|
1409
2585
|
return {
|
|
2586
|
+
...row.uiCapabilities !== null ? {
|
|
2587
|
+
uiCapabilities: row.uiCapabilities
|
|
2588
|
+
} : {},
|
|
1410
2589
|
id: row.id,
|
|
1411
2590
|
threadId: row.threadId,
|
|
1412
2591
|
actor: row.actor,
|
|
@@ -1436,8 +2615,8 @@ function toQueuedMessage(row) {
|
|
|
1436
2615
|
__name(toQueuedMessage, "toQueuedMessage");
|
|
1437
2616
|
|
|
1438
2617
|
// src/drizzle-governance-queries.ts
|
|
1439
|
-
var
|
|
1440
|
-
var
|
|
2618
|
+
var import_nestjs_agent_core5 = require("@dudousxd/nestjs-agent-core");
|
|
2619
|
+
var import_drizzle_orm5 = require("drizzle-orm");
|
|
1441
2620
|
var UNCLASSIFIED_ERROR_CODE = "unknown";
|
|
1442
2621
|
var DEFAULT_AGENT_BUCKET = "(default)";
|
|
1443
2622
|
var TOOL_CALL_STATUSES = [
|
|
@@ -1472,14 +2651,14 @@ function isRunStatus(value) {
|
|
|
1472
2651
|
}
|
|
1473
2652
|
__name(isRunStatus, "isRunStatus");
|
|
1474
2653
|
function dayStartUtc(day) {
|
|
1475
|
-
return (0,
|
|
2654
|
+
return (0, import_nestjs_agent_core5.dayBoundsUtc)({
|
|
1476
2655
|
fromDay: day,
|
|
1477
2656
|
toDay: day
|
|
1478
2657
|
}).start;
|
|
1479
2658
|
}
|
|
1480
2659
|
__name(dayStartUtc, "dayStartUtc");
|
|
1481
2660
|
function dayEndUtc(day) {
|
|
1482
|
-
return (0,
|
|
2661
|
+
return (0, import_nestjs_agent_core5.dayBoundsUtc)({
|
|
1483
2662
|
fromDay: day,
|
|
1484
2663
|
toDay: day
|
|
1485
2664
|
}).end;
|
|
@@ -1550,11 +2729,15 @@ var DrizzleGovernanceQueries = class {
|
|
|
1550
2729
|
static {
|
|
1551
2730
|
__name(this, "DrizzleGovernanceQueries");
|
|
1552
2731
|
}
|
|
1553
|
-
db;
|
|
1554
2732
|
pricingStore;
|
|
2733
|
+
db;
|
|
2734
|
+
dialect;
|
|
2735
|
+
t;
|
|
1555
2736
|
constructor(db, pricingStore) {
|
|
1556
|
-
this.db = db;
|
|
1557
2737
|
this.pricingStore = pricingStore;
|
|
2738
|
+
this.dialect = agentDialectOf(db);
|
|
2739
|
+
this.t = agentTablesFor(this.dialect);
|
|
2740
|
+
this.db = asBuilder(db);
|
|
1558
2741
|
}
|
|
1559
2742
|
async loadPricing() {
|
|
1560
2743
|
const prices = await this.pricingStore.listCurrentPrices();
|
|
@@ -1570,17 +2753,17 @@ var DrizzleGovernanceQueries = class {
|
|
|
1570
2753
|
return pricing;
|
|
1571
2754
|
}
|
|
1572
2755
|
async usageInRange(range) {
|
|
1573
|
-
const { start, end } = (0,
|
|
1574
|
-
const rows = await this.db.select().from(agentTokenUsage).where((0,
|
|
2756
|
+
const { start, end } = (0, import_nestjs_agent_core5.dayBoundsUtc)(range);
|
|
2757
|
+
const rows = await this.db.select().from(this.t.agentTokenUsage).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentTokenUsage.createdAt, start), (0, import_drizzle_orm5.lte)(this.t.agentTokenUsage.createdAt, end)));
|
|
1575
2758
|
return rows.map(toUsageInput);
|
|
1576
2759
|
}
|
|
1577
2760
|
async spendByModel(range) {
|
|
1578
2761
|
const pricing = await this.loadPricing();
|
|
1579
|
-
return (0,
|
|
2762
|
+
return (0, import_nestjs_agent_core5.bucketByModel)(await this.usageInRange(range), pricing);
|
|
1580
2763
|
}
|
|
1581
2764
|
async spendByActor(range) {
|
|
1582
2765
|
const pricing = await this.loadPricing();
|
|
1583
|
-
return (0,
|
|
2766
|
+
return (0, import_nestjs_agent_core5.bucketByActor)(await this.usageInRange(range), pricing);
|
|
1584
2767
|
}
|
|
1585
2768
|
/**
|
|
1586
2769
|
* Top threads by spend within the range, highest cost first, capped at `limit`. Usage rows for a
|
|
@@ -1596,7 +2779,7 @@ var DrizzleGovernanceQueries = class {
|
|
|
1596
2779
|
if (threadIds.length === 0) {
|
|
1597
2780
|
return [];
|
|
1598
2781
|
}
|
|
1599
|
-
const threads = await this.db.select().from(agentThread).where((0,
|
|
2782
|
+
const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.inArray)(this.t.agentThread.id, threadIds), (0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt)));
|
|
1600
2783
|
const threadsById = new Map(threads.map((thread) => [
|
|
1601
2784
|
thread.id,
|
|
1602
2785
|
{
|
|
@@ -1604,25 +2787,25 @@ var DrizzleGovernanceQueries = class {
|
|
|
1604
2787
|
actorRef: thread.actorRef
|
|
1605
2788
|
}
|
|
1606
2789
|
]));
|
|
1607
|
-
return (0,
|
|
2790
|
+
return (0, import_nestjs_agent_core5.bucketByThread)(rows, pricing, threadsById, {
|
|
1608
2791
|
limit,
|
|
1609
2792
|
includeUnknownThreads: false
|
|
1610
2793
|
});
|
|
1611
2794
|
}
|
|
1612
2795
|
async usageTrend(range) {
|
|
1613
2796
|
const pricing = await this.loadPricing();
|
|
1614
|
-
return (0,
|
|
2797
|
+
return (0, import_nestjs_agent_core5.bucketUsageTrend)(await this.usageInRange(range), pricing);
|
|
1615
2798
|
}
|
|
1616
2799
|
async recentToolCalls(limit) {
|
|
1617
2800
|
const rows = await this.db.select({
|
|
1618
|
-
toolCallId: agentToolCall.id,
|
|
1619
|
-
toolName: agentToolCall.toolName,
|
|
1620
|
-
toolType: agentToolCall.toolType,
|
|
1621
|
-
status: agentToolCall.status,
|
|
1622
|
-
threadId: agentMessage.threadId,
|
|
1623
|
-
createdAt: agentToolCall.createdAt,
|
|
1624
|
-
runId: agentToolCall.runId
|
|
1625
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
2801
|
+
toolCallId: this.t.agentToolCall.id,
|
|
2802
|
+
toolName: this.t.agentToolCall.toolName,
|
|
2803
|
+
toolType: this.t.agentToolCall.toolType,
|
|
2804
|
+
status: this.t.agentToolCall.status,
|
|
2805
|
+
threadId: this.t.agentMessage.threadId,
|
|
2806
|
+
createdAt: this.t.agentToolCall.createdAt,
|
|
2807
|
+
runId: this.t.agentToolCall.runId
|
|
2808
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm5.desc)(this.t.agentToolCall.id)).limit(limit);
|
|
1626
2809
|
return rows.map((row) => ({
|
|
1627
2810
|
toolCallId: row.toolCallId,
|
|
1628
2811
|
toolName: row.toolName,
|
|
@@ -1650,9 +2833,9 @@ var DrizzleGovernanceQueries = class {
|
|
|
1650
2833
|
return rollups;
|
|
1651
2834
|
}
|
|
1652
2835
|
const messageCounts = await this.db.select({
|
|
1653
|
-
threadId: agentMessage.threadId,
|
|
1654
|
-
value: (0,
|
|
1655
|
-
}).from(agentMessage).where((0,
|
|
2836
|
+
threadId: this.t.agentMessage.threadId,
|
|
2837
|
+
value: (0, import_drizzle_orm5.count)()
|
|
2838
|
+
}).from(this.t.agentMessage).where((0, import_drizzle_orm5.inArray)(this.t.agentMessage.threadId, threadIds)).groupBy(this.t.agentMessage.threadId);
|
|
1656
2839
|
for (const row of messageCounts) {
|
|
1657
2840
|
const bucket = rollups.get(row.threadId);
|
|
1658
2841
|
if (bucket !== void 0) {
|
|
@@ -1660,10 +2843,10 @@ var DrizzleGovernanceQueries = class {
|
|
|
1660
2843
|
}
|
|
1661
2844
|
}
|
|
1662
2845
|
const usageTotals = await this.db.select({
|
|
1663
|
-
threadId: agentTokenUsage.threadId,
|
|
1664
|
-
inputTokens: (0,
|
|
1665
|
-
outputTokens: (0,
|
|
1666
|
-
}).from(agentTokenUsage).where((0,
|
|
2846
|
+
threadId: this.t.agentTokenUsage.threadId,
|
|
2847
|
+
inputTokens: (0, import_drizzle_orm5.sum)(this.t.agentTokenUsage.inputTokens),
|
|
2848
|
+
outputTokens: (0, import_drizzle_orm5.sum)(this.t.agentTokenUsage.outputTokens)
|
|
2849
|
+
}).from(this.t.agentTokenUsage).where((0, import_drizzle_orm5.inArray)(this.t.agentTokenUsage.threadId, threadIds)).groupBy(this.t.agentTokenUsage.threadId);
|
|
1667
2850
|
for (const row of usageTotals) {
|
|
1668
2851
|
const bucket = rollups.get(row.threadId);
|
|
1669
2852
|
if (bucket !== void 0) {
|
|
@@ -1688,12 +2871,12 @@ var DrizzleGovernanceQueries = class {
|
|
|
1688
2871
|
});
|
|
1689
2872
|
}
|
|
1690
2873
|
async recentThreads(limit) {
|
|
1691
|
-
const threads = await this.db.select().from(agentThread).where((0,
|
|
2874
|
+
const threads = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentThread.updatedAt), (0, import_drizzle_orm5.desc)(this.t.agentThread.id)).limit(limit);
|
|
1692
2875
|
return this.toThreadActivityRows(threads);
|
|
1693
2876
|
}
|
|
1694
2877
|
async runsInRange(range) {
|
|
1695
|
-
const { start, end } = (0,
|
|
1696
|
-
return this.db.select().from(agentRun).where((0,
|
|
2878
|
+
const { start, end } = (0, import_nestjs_agent_core5.dayBoundsUtc)(range);
|
|
2879
|
+
return this.db.select().from(this.t.agentRun).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, start), (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, end)));
|
|
1697
2880
|
}
|
|
1698
2881
|
async runMetrics(range) {
|
|
1699
2882
|
const runs = await this.runsInRange(range);
|
|
@@ -1749,8 +2932,8 @@ var DrizzleGovernanceQueries = class {
|
|
|
1749
2932
|
return result;
|
|
1750
2933
|
}
|
|
1751
2934
|
async runErrors(range) {
|
|
1752
|
-
const { start, end } = (0,
|
|
1753
|
-
const runs = await this.db.select().from(agentRun).where((0,
|
|
2935
|
+
const { start, end } = (0, import_nestjs_agent_core5.dayBoundsUtc)(range);
|
|
2936
|
+
const runs = await this.db.select().from(this.t.agentRun).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, start), (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, end), (0, import_drizzle_orm5.eq)(this.t.agentRun.status, "failed")));
|
|
1754
2937
|
const byError = /* @__PURE__ */ new Map();
|
|
1755
2938
|
for (const run of runs) {
|
|
1756
2939
|
const errorCode = run.errorCode ?? UNCLASSIFIED_ERROR_CODE;
|
|
@@ -1790,7 +2973,7 @@ var DrizzleGovernanceQueries = class {
|
|
|
1790
2973
|
return result;
|
|
1791
2974
|
}
|
|
1792
2975
|
async recentRuns(limit) {
|
|
1793
|
-
const runs = await this.db.select().from(agentRun).orderBy((0,
|
|
2976
|
+
const runs = await this.db.select().from(this.t.agentRun).orderBy((0, import_drizzle_orm5.desc)(this.t.agentRun.startedAt), (0, import_drizzle_orm5.desc)(this.t.agentRun.id)).limit(limit);
|
|
1794
2977
|
return runs.map(toRecentRunRow);
|
|
1795
2978
|
}
|
|
1796
2979
|
/**
|
|
@@ -1800,16 +2983,16 @@ var DrizzleGovernanceQueries = class {
|
|
|
1800
2983
|
*/
|
|
1801
2984
|
async pendingApprovals(limit) {
|
|
1802
2985
|
const rows = await this.db.select({
|
|
1803
|
-
toolCallId: agentToolCall.id,
|
|
1804
|
-
toolName: agentToolCall.toolName,
|
|
1805
|
-
input: agentToolCall.input,
|
|
1806
|
-
threadId: agentThread.id,
|
|
1807
|
-
threadTitle: agentThread.title,
|
|
1808
|
-
actorRef: agentThread.actorRef,
|
|
1809
|
-
agentName: agentMessage.agentName,
|
|
1810
|
-
requestedAt: agentToolCall.createdAt,
|
|
1811
|
-
runId: agentToolCall.runId
|
|
1812
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
2986
|
+
toolCallId: this.t.agentToolCall.id,
|
|
2987
|
+
toolName: this.t.agentToolCall.toolName,
|
|
2988
|
+
input: this.t.agentToolCall.input,
|
|
2989
|
+
threadId: this.t.agentThread.id,
|
|
2990
|
+
threadTitle: this.t.agentThread.title,
|
|
2991
|
+
actorRef: this.t.agentThread.actorRef,
|
|
2992
|
+
agentName: this.t.agentMessage.agentName,
|
|
2993
|
+
requestedAt: this.t.agentToolCall.createdAt,
|
|
2994
|
+
runId: this.t.agentToolCall.runId
|
|
2995
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, "pending_approval")).orderBy(this.t.agentToolCall.createdAt, this.t.agentToolCall.id).limit(limit);
|
|
1813
2996
|
return rows.map((row) => ({
|
|
1814
2997
|
toolCallId: row.toolCallId,
|
|
1815
2998
|
toolName: row.toolName,
|
|
@@ -1830,12 +3013,12 @@ var DrizzleGovernanceQueries = class {
|
|
|
1830
3013
|
* `PERCENTILE_CONT` and one portable implementation beats three dialect-specific ones.
|
|
1831
3014
|
*/
|
|
1832
3015
|
async toolStats(range) {
|
|
1833
|
-
const { start, end } = (0,
|
|
1834
|
-
const calls = await this.db.select().from(agentToolCall).where((0,
|
|
3016
|
+
const { start, end } = (0, import_nestjs_agent_core5.dayBoundsUtc)(range);
|
|
3017
|
+
const calls = await this.db.select().from(this.t.agentToolCall).where((0, import_drizzle_orm5.and)((0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, start), (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, end)));
|
|
1835
3018
|
const byTool = /* @__PURE__ */ new Map();
|
|
1836
3019
|
for (const call of calls) {
|
|
1837
|
-
const
|
|
1838
|
-
const bucket = byTool.get(
|
|
3020
|
+
const key2 = `${call.toolName} ${call.toolType}`;
|
|
3021
|
+
const bucket = byTool.get(key2) ?? {
|
|
1839
3022
|
toolName: call.toolName,
|
|
1840
3023
|
toolType: call.toolType,
|
|
1841
3024
|
calls: 0,
|
|
@@ -1849,7 +3032,7 @@ var DrizzleGovernanceQueries = class {
|
|
|
1849
3032
|
if (call.executionMs != null) {
|
|
1850
3033
|
bucket.executionMs.push(call.executionMs);
|
|
1851
3034
|
}
|
|
1852
|
-
byTool.set(
|
|
3035
|
+
byTool.set(key2, bucket);
|
|
1853
3036
|
}
|
|
1854
3037
|
const result = [];
|
|
1855
3038
|
for (const bucket of byTool.values()) {
|
|
@@ -1881,19 +3064,19 @@ var DrizzleGovernanceQueries = class {
|
|
|
1881
3064
|
if (filters?.toolType !== void 0 && !isToolKind(filters.toolType)) {
|
|
1882
3065
|
return emptyPage(query);
|
|
1883
3066
|
}
|
|
1884
|
-
const whereClause = (0,
|
|
3067
|
+
const whereClause = (0, import_drizzle_orm5.and)(filters?.toolName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolName, filters.toolName) : void 0, filters?.toolType !== void 0 && isToolKind(filters.toolType) ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolType, filters.toolType) : void 0, filters?.status !== void 0 && isToolCallStatus(filters.status) ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, filters.status) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, filters.threadId) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, dayEndUtc(filters.toDay)) : void 0);
|
|
1885
3068
|
const [totalRow] = await this.db.select({
|
|
1886
|
-
value: (0,
|
|
1887
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
3069
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3070
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).where(whereClause);
|
|
1888
3071
|
const rows = await this.db.select({
|
|
1889
|
-
toolCallId: agentToolCall.id,
|
|
1890
|
-
toolName: agentToolCall.toolName,
|
|
1891
|
-
toolType: agentToolCall.toolType,
|
|
1892
|
-
status: agentToolCall.status,
|
|
1893
|
-
threadId: agentMessage.threadId,
|
|
1894
|
-
createdAt: agentToolCall.createdAt,
|
|
1895
|
-
runId: agentToolCall.runId
|
|
1896
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
3072
|
+
toolCallId: this.t.agentToolCall.id,
|
|
3073
|
+
toolName: this.t.agentToolCall.toolName,
|
|
3074
|
+
toolType: this.t.agentToolCall.toolType,
|
|
3075
|
+
status: this.t.agentToolCall.status,
|
|
3076
|
+
threadId: this.t.agentMessage.threadId,
|
|
3077
|
+
createdAt: this.t.agentToolCall.createdAt,
|
|
3078
|
+
runId: this.t.agentToolCall.runId
|
|
3079
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).where(whereClause).orderBy((0, import_drizzle_orm5.desc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm5.desc)(this.t.agentToolCall.id)).limit(query.pageSize).offset((query.page - 1) * query.pageSize);
|
|
1897
3080
|
return {
|
|
1898
3081
|
rows: rows.map((row) => ({
|
|
1899
3082
|
toolCallId: row.toolCallId,
|
|
@@ -1917,11 +3100,11 @@ var DrizzleGovernanceQueries = class {
|
|
|
1917
3100
|
*/
|
|
1918
3101
|
async threadsPage(query) {
|
|
1919
3102
|
const filters = query.where;
|
|
1920
|
-
const whereClause = (0,
|
|
3103
|
+
const whereClause = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.isNull)(this.t.agentThread.deletedAt), filters?.actorRef !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.actorRef, filters.actorRef) : void 0, filters?.title !== void 0 ? (0, import_drizzle_orm5.like)(import_drizzle_orm5.sql`lower(${this.t.agentThread.title})`, `%${filters.title.toLowerCase()}%`) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentThread.updatedAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentThread.updatedAt, dayEndUtc(filters.toDay)) : void 0);
|
|
1921
3104
|
const [totalRow] = await this.db.select({
|
|
1922
|
-
value: (0,
|
|
1923
|
-
}).from(agentThread).where(whereClause);
|
|
1924
|
-
const threads = await this.db.select().from(agentThread).where(whereClause).orderBy((0,
|
|
3105
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3106
|
+
}).from(this.t.agentThread).where(whereClause);
|
|
3107
|
+
const threads = await this.db.select().from(this.t.agentThread).where(whereClause).orderBy((0, import_drizzle_orm5.desc)(this.t.agentThread.updatedAt), (0, import_drizzle_orm5.desc)(this.t.agentThread.id)).limit(query.pageSize).offset((query.page - 1) * query.pageSize);
|
|
1925
3108
|
const rows = await this.toThreadActivityRows(threads);
|
|
1926
3109
|
return {
|
|
1927
3110
|
rows,
|
|
@@ -1939,11 +3122,11 @@ var DrizzleGovernanceQueries = class {
|
|
|
1939
3122
|
if (filters?.status !== void 0 && !isRunStatus(filters.status)) {
|
|
1940
3123
|
return emptyPage(query);
|
|
1941
3124
|
}
|
|
1942
|
-
const whereClause = (0,
|
|
3125
|
+
const whereClause = (0, import_drizzle_orm5.and)(filters?.agentName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.agentName, filters.agentName) : void 0, filters?.status !== void 0 && isRunStatus(filters.status) ? (0, import_drizzle_orm5.eq)(this.t.agentRun.status, filters.status) : void 0, filters?.errorCode !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.errorCode, filters.errorCode) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, filters.threadId) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentRun.startedAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentRun.startedAt, dayEndUtc(filters.toDay)) : void 0);
|
|
1943
3126
|
const [totalRow] = await this.db.select({
|
|
1944
|
-
value: (0,
|
|
1945
|
-
}).from(agentRun).where(whereClause);
|
|
1946
|
-
const runs = await this.db.select().from(agentRun).where(whereClause).orderBy((0,
|
|
3127
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3128
|
+
}).from(this.t.agentRun).where(whereClause);
|
|
3129
|
+
const runs = await this.db.select().from(this.t.agentRun).where(whereClause).orderBy((0, import_drizzle_orm5.desc)(this.t.agentRun.startedAt), (0, import_drizzle_orm5.desc)(this.t.agentRun.id)).limit(query.pageSize).offset((query.page - 1) * query.pageSize);
|
|
1947
3130
|
return {
|
|
1948
3131
|
rows: runs.map(toRecentRunRow),
|
|
1949
3132
|
total: totalRow?.value ?? 0,
|
|
@@ -1963,21 +3146,21 @@ var DrizzleGovernanceQueries = class {
|
|
|
1963
3146
|
*/
|
|
1964
3147
|
async approvalsPage(query) {
|
|
1965
3148
|
const filters = query.where;
|
|
1966
|
-
const whereClause = (0,
|
|
3149
|
+
const whereClause = (0, import_drizzle_orm5.and)((0, import_drizzle_orm5.eq)(this.t.agentToolCall.status, "pending_approval"), filters?.toolName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentToolCall.toolName, filters.toolName) : void 0, filters?.threadId !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.id, filters.threadId) : void 0, filters?.actorRef !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentThread.actorRef, filters.actorRef) : void 0, filters?.agentName !== void 0 ? (0, import_drizzle_orm5.eq)(this.t.agentMessage.agentName, filters.agentName) : void 0, filters?.fromDay !== void 0 ? (0, import_drizzle_orm5.gte)(this.t.agentToolCall.createdAt, dayStartUtc(filters.fromDay)) : void 0, filters?.toDay !== void 0 ? (0, import_drizzle_orm5.lte)(this.t.agentToolCall.createdAt, dayEndUtc(filters.toDay)) : void 0);
|
|
1967
3150
|
const [totalRow] = await this.db.select({
|
|
1968
|
-
value: (0,
|
|
1969
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
3151
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3152
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where(whereClause);
|
|
1970
3153
|
const rows = await this.db.select({
|
|
1971
|
-
toolCallId: agentToolCall.id,
|
|
1972
|
-
toolName: agentToolCall.toolName,
|
|
1973
|
-
input: agentToolCall.input,
|
|
1974
|
-
threadId: agentThread.id,
|
|
1975
|
-
threadTitle: agentThread.title,
|
|
1976
|
-
actorRef: agentThread.actorRef,
|
|
1977
|
-
agentName: agentMessage.agentName,
|
|
1978
|
-
requestedAt: agentToolCall.createdAt,
|
|
1979
|
-
runId: agentToolCall.runId
|
|
1980
|
-
}).from(agentToolCall).innerJoin(agentMessage, (0,
|
|
3154
|
+
toolCallId: this.t.agentToolCall.id,
|
|
3155
|
+
toolName: this.t.agentToolCall.toolName,
|
|
3156
|
+
input: this.t.agentToolCall.input,
|
|
3157
|
+
threadId: this.t.agentThread.id,
|
|
3158
|
+
threadTitle: this.t.agentThread.title,
|
|
3159
|
+
actorRef: this.t.agentThread.actorRef,
|
|
3160
|
+
agentName: this.t.agentMessage.agentName,
|
|
3161
|
+
requestedAt: this.t.agentToolCall.createdAt,
|
|
3162
|
+
runId: this.t.agentToolCall.runId
|
|
3163
|
+
}).from(this.t.agentToolCall).innerJoin(this.t.agentMessage, (0, import_drizzle_orm5.eq)(this.t.agentToolCall.messageId, this.t.agentMessage.id)).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, this.t.agentThread.id)).where(whereClause).orderBy((0, import_drizzle_orm5.asc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm5.asc)(this.t.agentToolCall.id)).limit(query.pageSize).offset((query.page - 1) * query.pageSize);
|
|
1981
3164
|
return {
|
|
1982
3165
|
rows: rows.map((row) => ({
|
|
1983
3166
|
toolCallId: row.toolCallId,
|
|
@@ -2002,13 +3185,13 @@ var DrizzleGovernanceQueries = class {
|
|
|
2002
3185
|
*/
|
|
2003
3186
|
async runDetail(runId) {
|
|
2004
3187
|
const [row] = await this.db.select({
|
|
2005
|
-
run: agentRun,
|
|
2006
|
-
thread: agentThread
|
|
2007
|
-
}).from(agentRun).innerJoin(agentThread, (0,
|
|
3188
|
+
run: this.t.agentRun,
|
|
3189
|
+
thread: this.t.agentThread
|
|
3190
|
+
}).from(this.t.agentRun).innerJoin(this.t.agentThread, (0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, this.t.agentThread.id)).where((0, import_drizzle_orm5.eq)(this.t.agentRun.id, runId)).limit(1);
|
|
2008
3191
|
if (row === void 0) {
|
|
2009
3192
|
return null;
|
|
2010
3193
|
}
|
|
2011
|
-
const toolCalls = await this.db.select().from(agentToolCall).where((0,
|
|
3194
|
+
const toolCalls = await this.db.select().from(this.t.agentToolCall).where((0, import_drizzle_orm5.eq)(this.t.agentToolCall.runId, runId)).orderBy((0, import_drizzle_orm5.asc)(this.t.agentToolCall.createdAt), (0, import_drizzle_orm5.asc)(this.t.agentToolCall.id));
|
|
2012
3195
|
return {
|
|
2013
3196
|
run: toRecentRunRow(row.run),
|
|
2014
3197
|
thread: {
|
|
@@ -2027,22 +3210,27 @@ var DrizzleGovernanceQueries = class {
|
|
|
2027
3210
|
* message. Soft-deleted threads are returned with `deleted: true`; an audit still needs them.
|
|
2028
3211
|
*/
|
|
2029
3212
|
async threadDetail(query) {
|
|
2030
|
-
const [thread] = await this.db.select().from(agentThread).where((0,
|
|
3213
|
+
const [thread] = await this.db.select().from(this.t.agentThread).where((0, import_drizzle_orm5.eq)(this.t.agentThread.id, query.threadId)).limit(1);
|
|
2031
3214
|
if (thread === void 0) {
|
|
2032
3215
|
return null;
|
|
2033
3216
|
}
|
|
2034
3217
|
const pricing = await this.loadPricing();
|
|
2035
|
-
const usageRows = await this.db.select().from(agentTokenUsage).where((0,
|
|
2036
|
-
const usage = (0,
|
|
3218
|
+
const usageRows = await this.db.select().from(this.t.agentTokenUsage).where((0, import_drizzle_orm5.eq)(this.t.agentTokenUsage.threadId, thread.id));
|
|
3219
|
+
const usage = (0, import_nestjs_agent_core5.rollupThreadUsage)(usageRows.map(toUsageInput), pricing);
|
|
2037
3220
|
const [messageCountRow] = await this.db.select({
|
|
2038
|
-
value: (0,
|
|
2039
|
-
}).from(agentMessage).where((0,
|
|
2040
|
-
const messages = await this.db.select().from(agentMessage).where((0,
|
|
3221
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3222
|
+
}).from(this.t.agentMessage).where((0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, thread.id));
|
|
3223
|
+
const messages = await this.db.select().from(this.t.agentMessage).where((0, import_drizzle_orm5.eq)(this.t.agentMessage.threadId, thread.id)).orderBy(
|
|
3224
|
+
// Append order (see DrizzleAgentStore's message order), newest first.
|
|
3225
|
+
(0, import_drizzle_orm5.desc)(import_drizzle_orm5.sql`coalesce(${this.t.agentMessage.seq}, 0)`),
|
|
3226
|
+
(0, import_drizzle_orm5.desc)(this.t.agentMessage.createdAt),
|
|
3227
|
+
(0, import_drizzle_orm5.desc)(this.t.agentMessage.id)
|
|
3228
|
+
).limit(query.messageLimit);
|
|
2041
3229
|
const toolCallCounts = await this.toolCallCountsByMessage(messages.map((message) => message.id));
|
|
2042
3230
|
const [runTotalRow] = await this.db.select({
|
|
2043
|
-
value: (0,
|
|
2044
|
-
}).from(agentRun).where((0,
|
|
2045
|
-
const runs = await this.db.select().from(agentRun).where((0,
|
|
3231
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3232
|
+
}).from(this.t.agentRun).where((0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, thread.id));
|
|
3233
|
+
const runs = await this.db.select().from(this.t.agentRun).where((0, import_drizzle_orm5.eq)(this.t.agentRun.threadId, thread.id)).orderBy((0, import_drizzle_orm5.desc)(this.t.agentRun.startedAt), (0, import_drizzle_orm5.desc)(this.t.agentRun.id)).limit(query.runLimit);
|
|
2046
3234
|
return {
|
|
2047
3235
|
thread: {
|
|
2048
3236
|
threadId: thread.id,
|
|
@@ -2057,7 +3245,7 @@ var DrizzleGovernanceQueries = class {
|
|
|
2057
3245
|
runs: runs.map(toRecentRunRow),
|
|
2058
3246
|
runTotal: runTotalRow?.value ?? 0,
|
|
2059
3247
|
messages: messages.map((message) => {
|
|
2060
|
-
const { content, truncated } = (0,
|
|
3248
|
+
const { content, truncated } = (0, import_nestjs_agent_core5.truncateDetailContent)(message.content);
|
|
2061
3249
|
return {
|
|
2062
3250
|
messageId: message.id,
|
|
2063
3251
|
role: message.role,
|
|
@@ -2077,9 +3265,9 @@ var DrizzleGovernanceQueries = class {
|
|
|
2077
3265
|
return counts;
|
|
2078
3266
|
}
|
|
2079
3267
|
const rows = await this.db.select({
|
|
2080
|
-
messageId: agentToolCall.messageId,
|
|
2081
|
-
value: (0,
|
|
2082
|
-
}).from(agentToolCall).where((0,
|
|
3268
|
+
messageId: this.t.agentToolCall.messageId,
|
|
3269
|
+
value: (0, import_drizzle_orm5.count)()
|
|
3270
|
+
}).from(this.t.agentToolCall).where((0, import_drizzle_orm5.inArray)(this.t.agentToolCall.messageId, messageIds)).groupBy(this.t.agentToolCall.messageId);
|
|
2083
3271
|
for (const row of rows) {
|
|
2084
3272
|
counts.set(row.messageId, row.value);
|
|
2085
3273
|
}
|
|
@@ -2088,8 +3276,8 @@ var DrizzleGovernanceQueries = class {
|
|
|
2088
3276
|
};
|
|
2089
3277
|
|
|
2090
3278
|
// src/drizzle-memory-provider.ts
|
|
2091
|
-
var
|
|
2092
|
-
var
|
|
3279
|
+
var import_nestjs_agent_core6 = require("@dudousxd/nestjs-agent-core");
|
|
3280
|
+
var import_drizzle_orm6 = require("drizzle-orm");
|
|
2093
3281
|
function toRecord(row) {
|
|
2094
3282
|
return {
|
|
2095
3283
|
id: row.id,
|
|
@@ -2118,8 +3306,12 @@ var DrizzleMemoryProvider = class {
|
|
|
2118
3306
|
__name(this, "DrizzleMemoryProvider");
|
|
2119
3307
|
}
|
|
2120
3308
|
db;
|
|
3309
|
+
dialect;
|
|
3310
|
+
t;
|
|
2121
3311
|
constructor(db) {
|
|
2122
|
-
this.
|
|
3312
|
+
this.dialect = agentDialectOf(db);
|
|
3313
|
+
this.t = agentTablesFor(this.dialect);
|
|
3314
|
+
this.db = asBuilder(db);
|
|
2123
3315
|
}
|
|
2124
3316
|
/**
|
|
2125
3317
|
* Every memory visible at `scopes` — asked on EVERY turn, so it is one indexed `scope in (…)`
|
|
@@ -2133,7 +3325,7 @@ var DrizzleMemoryProvider = class {
|
|
|
2133
3325
|
if (scopes.length === 0) {
|
|
2134
3326
|
return [];
|
|
2135
3327
|
}
|
|
2136
|
-
const rows = await this.db.select().from(agentMemory).where((0,
|
|
3328
|
+
const rows = await this.db.select().from(this.t.agentMemory).where((0, import_drizzle_orm6.inArray)(this.t.agentMemory.scope, [
|
|
2137
3329
|
...scopes
|
|
2138
3330
|
]));
|
|
2139
3331
|
return rows.map(toRecord);
|
|
@@ -2149,10 +3341,8 @@ var DrizzleMemoryProvider = class {
|
|
|
2149
3341
|
* answer cannot be used to find out which memories exist about other people.
|
|
2150
3342
|
*/
|
|
2151
3343
|
async forget({ id, ctx }) {
|
|
2152
|
-
const deleted = await this.db.delete(agentMemory).where((0,
|
|
2153
|
-
|
|
2154
|
-
});
|
|
2155
|
-
return deleted.length > 0;
|
|
3344
|
+
const deleted = await affectedRows(this.dialect, this.db.delete(this.t.agentMemory).where((0, import_drizzle_orm6.and)((0, import_drizzle_orm6.eq)(this.t.agentMemory.id, id), (0, import_drizzle_orm6.eq)(this.t.agentMemory.scope, (0, import_nestjs_agent_core6.actorScope)(ctx.actor)))), this.t.agentMemory.id);
|
|
3345
|
+
return deleted > 0;
|
|
2156
3346
|
}
|
|
2157
3347
|
/**
|
|
2158
3348
|
* Upsert on (`scope`, `key`) — one statement, against the unique index, rather than a read
|
|
@@ -2165,7 +3355,7 @@ var DrizzleMemoryProvider = class {
|
|
|
2165
3355
|
* rewriting `createdAt` would lose when the belief was first formed; reassigning `id` would
|
|
2166
3356
|
* invalidate the delete handle a person was already shown.
|
|
2167
3357
|
*/
|
|
2168
|
-
async write({ key, text:
|
|
3358
|
+
async write({ key: key2, text: text3, scope, origin, ctx }) {
|
|
2169
3359
|
assertAuthorMayWriteScope({
|
|
2170
3360
|
scope,
|
|
2171
3361
|
origin,
|
|
@@ -2178,28 +3368,40 @@ var DrizzleMemoryProvider = class {
|
|
|
2178
3368
|
originRunId: origin.runId ?? null,
|
|
2179
3369
|
originActorRef: origin.actorRef ?? null
|
|
2180
3370
|
};
|
|
2181
|
-
const
|
|
3371
|
+
const memory = this.t.agentMemory;
|
|
3372
|
+
const set = {
|
|
3373
|
+
text: text3,
|
|
3374
|
+
...columns,
|
|
3375
|
+
updatedAt: now
|
|
3376
|
+
};
|
|
3377
|
+
const insert = this.db.insert(memory).values({
|
|
2182
3378
|
id: crypto.randomUUID(),
|
|
2183
3379
|
scope,
|
|
2184
|
-
key,
|
|
2185
|
-
text:
|
|
3380
|
+
key: key2,
|
|
3381
|
+
text: text3,
|
|
2186
3382
|
...columns,
|
|
2187
3383
|
pinned: false,
|
|
2188
3384
|
createdAt: now,
|
|
2189
3385
|
updatedAt: now
|
|
2190
|
-
})
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
3386
|
+
});
|
|
3387
|
+
let row;
|
|
3388
|
+
if (this.dialect === "mysql") {
|
|
3389
|
+
await upsert(this.dialect, insert, [
|
|
3390
|
+
memory.scope,
|
|
3391
|
+
memory.key
|
|
3392
|
+
], set);
|
|
3393
|
+
[row] = await this.db.select().from(memory).where((0, import_drizzle_orm6.and)((0, import_drizzle_orm6.eq)(memory.scope, scope), (0, import_drizzle_orm6.eq)(memory.key, key2)));
|
|
3394
|
+
} else {
|
|
3395
|
+
[row] = await insert.onConflictDoUpdate({
|
|
3396
|
+
target: [
|
|
3397
|
+
memory.scope,
|
|
3398
|
+
memory.key
|
|
3399
|
+
],
|
|
3400
|
+
set
|
|
3401
|
+
}).returning();
|
|
3402
|
+
}
|
|
2201
3403
|
if (row === void 0) {
|
|
2202
|
-
throw new Error(`DrizzleMemoryProvider: the upsert of "${
|
|
3404
|
+
throw new Error(`DrizzleMemoryProvider: the upsert of "${key2}" at "${scope}" returned no row; the driver does not support RETURNING on conflict.`);
|
|
2203
3405
|
}
|
|
2204
3406
|
return toRecord(row);
|
|
2205
3407
|
}
|
|
@@ -2212,17 +3414,15 @@ var DrizzleMemoryProvider = class {
|
|
|
2212
3414
|
* `false` where there is no such id.
|
|
2213
3415
|
*/
|
|
2214
3416
|
async pin({ id, pinned }) {
|
|
2215
|
-
const updated = await this.db.update(agentMemory).set({
|
|
3417
|
+
const updated = await affectedRows(this.dialect, this.db.update(this.t.agentMemory).set({
|
|
2216
3418
|
pinned,
|
|
2217
3419
|
updatedAt: /* @__PURE__ */ new Date()
|
|
2218
|
-
}).where((0,
|
|
2219
|
-
|
|
2220
|
-
});
|
|
2221
|
-
return updated.length > 0;
|
|
3420
|
+
}).where((0, import_drizzle_orm6.eq)(this.t.agentMemory.id, id)), this.t.agentMemory.id);
|
|
3421
|
+
return updated > 0;
|
|
2222
3422
|
}
|
|
2223
3423
|
};
|
|
2224
3424
|
function assertAuthorMayWriteScope({ scope, origin, ctx }) {
|
|
2225
|
-
const own = (0,
|
|
3425
|
+
const own = (0, import_nestjs_agent_core6.actorScope)(ctx.actor);
|
|
2226
3426
|
if (origin.author !== "human" && scope !== own) {
|
|
2227
3427
|
throw new Error(`DrizzleMemoryProvider: refusing to write an agent-authored memory at "${scope}"; an agent may write only at "${own}"`);
|
|
2228
3428
|
}
|
|
@@ -2230,20 +3430,24 @@ function assertAuthorMayWriteScope({ scope, origin, ctx }) {
|
|
|
2230
3430
|
__name(assertAuthorMayWriteScope, "assertAuthorMayWriteScope");
|
|
2231
3431
|
|
|
2232
3432
|
// src/drizzle-pricing-store.ts
|
|
2233
|
-
var
|
|
3433
|
+
var import_drizzle_orm7 = require("drizzle-orm");
|
|
2234
3434
|
var DrizzlePricingStore = class {
|
|
2235
3435
|
static {
|
|
2236
3436
|
__name(this, "DrizzlePricingStore");
|
|
2237
3437
|
}
|
|
2238
3438
|
db;
|
|
3439
|
+
dialect;
|
|
3440
|
+
t;
|
|
2239
3441
|
constructor(db) {
|
|
2240
|
-
this.
|
|
3442
|
+
this.dialect = agentDialectOf(db);
|
|
3443
|
+
this.t = agentTablesFor(this.dialect);
|
|
3444
|
+
this.db = asBuilder(db);
|
|
2241
3445
|
}
|
|
2242
3446
|
async upsertModelPrice(input) {
|
|
2243
|
-
await this.db.update(agentModelPricing).set({
|
|
3447
|
+
await this.db.update(this.t.agentModelPricing).set({
|
|
2244
3448
|
isCurrent: false
|
|
2245
|
-
}).where((0,
|
|
2246
|
-
await this.db.insert(agentModelPricing).values({
|
|
3449
|
+
}).where((0, import_drizzle_orm7.and)((0, import_drizzle_orm7.eq)(this.t.agentModelPricing.modelId, input.modelId), (0, import_drizzle_orm7.eq)(this.t.agentModelPricing.isCurrent, true)));
|
|
3450
|
+
await this.db.insert(this.t.agentModelPricing).values({
|
|
2247
3451
|
id: crypto.randomUUID(),
|
|
2248
3452
|
modelId: input.modelId,
|
|
2249
3453
|
inputPricePer1m: input.inputPricePer1m,
|
|
@@ -2255,7 +3459,7 @@ var DrizzlePricingStore = class {
|
|
|
2255
3459
|
});
|
|
2256
3460
|
}
|
|
2257
3461
|
async listCurrentPrices() {
|
|
2258
|
-
const rows = await this.db.select().from(agentModelPricing).where((0,
|
|
3462
|
+
const rows = await this.db.select().from(this.t.agentModelPricing).where((0, import_drizzle_orm7.eq)(this.t.agentModelPricing.isCurrent, true));
|
|
2259
3463
|
return rows.map((row) => ({
|
|
2260
3464
|
modelId: row.modelId,
|
|
2261
3465
|
inputPricePer1m: row.inputPricePer1m,
|
|
@@ -2272,60 +3476,55 @@ var DrizzlePricingStore = class {
|
|
|
2272
3476
|
};
|
|
2273
3477
|
|
|
2274
3478
|
// src/drizzle-token-stream-sink.ts
|
|
2275
|
-
var
|
|
2276
|
-
var
|
|
2277
|
-
function isUniqueViolation(error) {
|
|
2278
|
-
for (let current = error; current !== null && typeof current === "object"; ) {
|
|
2279
|
-
const code = current.code;
|
|
2280
|
-
if (typeof code === "string" && code.startsWith("SQLITE_CONSTRAINT")) return true;
|
|
2281
|
-
current = current.cause;
|
|
2282
|
-
}
|
|
2283
|
-
return false;
|
|
2284
|
-
}
|
|
2285
|
-
__name(isUniqueViolation, "isUniqueViolation");
|
|
3479
|
+
var import_nestjs_agent_core7 = require("@dudousxd/nestjs-agent-core");
|
|
3480
|
+
var import_drizzle_orm8 = require("drizzle-orm");
|
|
2286
3481
|
var DrizzleStreamFrameTable = class {
|
|
2287
3482
|
static {
|
|
2288
3483
|
__name(this, "DrizzleStreamFrameTable");
|
|
2289
3484
|
}
|
|
2290
3485
|
db;
|
|
3486
|
+
dialect;
|
|
3487
|
+
t;
|
|
2291
3488
|
constructor(db) {
|
|
2292
|
-
this.
|
|
3489
|
+
this.dialect = agentDialectOf(db);
|
|
3490
|
+
this.t = agentTablesFor(this.dialect);
|
|
3491
|
+
this.db = asBuilder(db);
|
|
2293
3492
|
}
|
|
2294
3493
|
async append(runId, row) {
|
|
2295
|
-
await this.db.
|
|
3494
|
+
await runSql(this.db, this.dialect, import_drizzle_orm8.sql`INSERT INTO agent_stream_frame (run_id, seq, frame, error, created_at)
|
|
2296
3495
|
SELECT ${runId}, COALESCE(MAX(seq), 0) + 1, ${row.frame}, ${row.error}, ${row.createdAt}
|
|
2297
3496
|
FROM agent_stream_frame WHERE run_id = ${runId}`);
|
|
2298
3497
|
}
|
|
2299
3498
|
async read(runId, after, limit) {
|
|
2300
3499
|
return this.db.select({
|
|
2301
|
-
seq: agentStreamFrame.seq,
|
|
2302
|
-
frame: agentStreamFrame.frame,
|
|
2303
|
-
error: agentStreamFrame.error
|
|
2304
|
-
}).from(agentStreamFrame).where((0,
|
|
3500
|
+
seq: this.t.agentStreamFrame.seq,
|
|
3501
|
+
frame: this.t.agentStreamFrame.frame,
|
|
3502
|
+
error: this.t.agentStreamFrame.error
|
|
3503
|
+
}).from(this.t.agentStreamFrame).where((0, import_drizzle_orm8.and)((0, import_drizzle_orm8.eq)(this.t.agentStreamFrame.runId, runId), (0, import_drizzle_orm8.gt)(this.t.agentStreamFrame.seq, after))).orderBy((0, import_drizzle_orm8.asc)(this.t.agentStreamFrame.seq)).limit(limit);
|
|
2305
3504
|
}
|
|
2306
3505
|
async has(runId) {
|
|
2307
3506
|
const rows = await this.db.select({
|
|
2308
|
-
seq: agentStreamFrame.seq
|
|
2309
|
-
}).from(agentStreamFrame).where((0,
|
|
3507
|
+
seq: this.t.agentStreamFrame.seq
|
|
3508
|
+
}).from(this.t.agentStreamFrame).where((0, import_drizzle_orm8.eq)(this.t.agentStreamFrame.runId, runId)).limit(1);
|
|
2310
3509
|
return rows.length > 0;
|
|
2311
3510
|
}
|
|
2312
3511
|
async remove(runIds) {
|
|
2313
3512
|
if (runIds.length === 0) return;
|
|
2314
|
-
await this.db.delete(agentStreamFrame).where((0,
|
|
3513
|
+
await this.db.delete(this.t.agentStreamFrame).where((0, import_drizzle_orm8.inArray)(this.t.agentStreamFrame.runId, [
|
|
2315
3514
|
...runIds
|
|
2316
3515
|
]));
|
|
2317
3516
|
}
|
|
2318
3517
|
async lapsedRuns(cutoff) {
|
|
2319
3518
|
const rows = await this.db.select({
|
|
2320
|
-
runId: agentStreamFrame.runId
|
|
2321
|
-
}).from(agentStreamFrame).groupBy(agentStreamFrame.runId).having((0,
|
|
3519
|
+
runId: this.t.agentStreamFrame.runId
|
|
3520
|
+
}).from(this.t.agentStreamFrame).groupBy(this.t.agentStreamFrame.runId).having((0, import_drizzle_orm8.lt)((0, import_drizzle_orm8.max)(this.t.agentStreamFrame.createdAt), cutoff));
|
|
2322
3521
|
return rows.map((row) => row.runId);
|
|
2323
3522
|
}
|
|
2324
3523
|
isUniqueViolation(error) {
|
|
2325
3524
|
return isUniqueViolation(error);
|
|
2326
3525
|
}
|
|
2327
3526
|
};
|
|
2328
|
-
var DrizzleTokenStreamSink = class extends
|
|
3527
|
+
var DrizzleTokenStreamSink = class extends import_nestjs_agent_core7.SqlTokenStreamSink {
|
|
2329
3528
|
static {
|
|
2330
3529
|
__name(this, "DrizzleTokenStreamSink");
|
|
2331
3530
|
}
|
|
@@ -2335,54 +3534,60 @@ var DrizzleTokenStreamSink = class extends import_nestjs_agent_core4.SqlTokenStr
|
|
|
2335
3534
|
};
|
|
2336
3535
|
|
|
2337
3536
|
// src/drizzle-confirm-token-store.ts
|
|
2338
|
-
var
|
|
3537
|
+
var import_drizzle_orm9 = require("drizzle-orm");
|
|
2339
3538
|
var DrizzleConfirmTokenStore = class {
|
|
2340
3539
|
static {
|
|
2341
3540
|
__name(this, "DrizzleConfirmTokenStore");
|
|
2342
3541
|
}
|
|
2343
3542
|
db;
|
|
3543
|
+
dialect;
|
|
3544
|
+
t;
|
|
2344
3545
|
constructor(db) {
|
|
2345
|
-
this.
|
|
3546
|
+
this.dialect = agentDialectOf(db);
|
|
3547
|
+
this.t = agentTablesFor(this.dialect);
|
|
3548
|
+
this.db = asBuilder(db);
|
|
2346
3549
|
}
|
|
2347
3550
|
async claim(input) {
|
|
2348
|
-
const
|
|
3551
|
+
const row = {
|
|
2349
3552
|
hash: input.hash,
|
|
2350
3553
|
actorRef: input.actorRef,
|
|
2351
3554
|
tool: input.tool,
|
|
2352
3555
|
expiresAt: input.expiresAt,
|
|
2353
3556
|
createdAt: Date.now()
|
|
2354
|
-
}
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
3557
|
+
};
|
|
3558
|
+
const table = this.t.agentConfirmToken;
|
|
3559
|
+
if (this.dialect === "mysql") {
|
|
3560
|
+
const insert = this.db.insert(table);
|
|
3561
|
+
return mysqlAffectedRows(await insert.ignore().values(row)) === 1;
|
|
3562
|
+
}
|
|
3563
|
+
const inserted = await affectedRows(this.dialect, this.db.insert(table).values(row).onConflictDoNothing(), table.hash);
|
|
3564
|
+
return inserted === 1;
|
|
2358
3565
|
}
|
|
2359
3566
|
async release(hash) {
|
|
2360
|
-
await this.db.delete(agentConfirmToken).where((0,
|
|
3567
|
+
await this.db.delete(this.t.agentConfirmToken).where((0, import_drizzle_orm9.eq)(this.t.agentConfirmToken.hash, hash));
|
|
2361
3568
|
}
|
|
2362
3569
|
async purgeExpired(now = Date.now()) {
|
|
2363
|
-
const
|
|
2364
|
-
|
|
2365
|
-
});
|
|
2366
|
-
return purged.length;
|
|
3570
|
+
const table = this.t.agentConfirmToken;
|
|
3571
|
+
return affectedRows(this.dialect, this.db.delete(table).where((0, import_drizzle_orm9.lt)(table.expiresAt, now)), table.hash);
|
|
2367
3572
|
}
|
|
2368
3573
|
};
|
|
2369
3574
|
|
|
2370
3575
|
// src/drizzle-rag-ingestion-log.ts
|
|
2371
3576
|
var import_node_diagnostics_channel = require("diagnostics_channel");
|
|
2372
3577
|
var import_common = require("@nestjs/common");
|
|
2373
|
-
var
|
|
2374
|
-
function _ts_decorate(decorators, target,
|
|
2375
|
-
var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target,
|
|
3578
|
+
var import_drizzle_orm10 = require("drizzle-orm");
|
|
3579
|
+
function _ts_decorate(decorators, target, key2, desc4) {
|
|
3580
|
+
var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key2) : desc4, d;
|
|
2376
3581
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
2377
|
-
r = Reflect.decorate(decorators, target,
|
|
3582
|
+
r = Reflect.decorate(decorators, target, key2, desc4);
|
|
2378
3583
|
} else {
|
|
2379
3584
|
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
2380
3585
|
if (d = decorators[i]) {
|
|
2381
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target,
|
|
3586
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key2, r) : d(target, key2)) || r;
|
|
2382
3587
|
}
|
|
2383
3588
|
}
|
|
2384
3589
|
}
|
|
2385
|
-
return c > 3 && r && Object.defineProperty(target,
|
|
3590
|
+
return c > 3 && r && Object.defineProperty(target, key2, r), r;
|
|
2386
3591
|
}
|
|
2387
3592
|
__name(_ts_decorate, "_ts_decorate");
|
|
2388
3593
|
function _ts_metadata(metadataKey, metadataValue) {
|
|
@@ -2418,8 +3623,8 @@ function num(value) {
|
|
|
2418
3623
|
}
|
|
2419
3624
|
__name(num, "num");
|
|
2420
3625
|
var RAG_INGESTION_LOG_PAGE_ORDER = [
|
|
2421
|
-
(0,
|
|
2422
|
-
(0,
|
|
3626
|
+
(0, import_drizzle_orm10.desc)(ragIngestionLog.updatedAt),
|
|
3627
|
+
(0, import_drizzle_orm10.asc)(ragIngestionLog.documentId)
|
|
2423
3628
|
];
|
|
2424
3629
|
var DEFAULT_PAGE_SIZE = 200;
|
|
2425
3630
|
var DEFAULT_BATCH_SIZE = 200;
|
|
@@ -2428,20 +3633,27 @@ function batches(batchSize) {
|
|
|
2428
3633
|
return Number.isFinite(requested) && requested >= 1 ? requested : DEFAULT_BATCH_SIZE;
|
|
2429
3634
|
}
|
|
2430
3635
|
__name(batches, "batches");
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
3636
|
+
function cursorColumns(log) {
|
|
3637
|
+
return {
|
|
3638
|
+
documentId: log.documentId,
|
|
3639
|
+
updatedAt: log.updatedAt
|
|
3640
|
+
};
|
|
3641
|
+
}
|
|
3642
|
+
__name(cursorColumns, "cursorColumns");
|
|
2435
3643
|
var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
2436
3644
|
static {
|
|
2437
3645
|
__name(this, "DrizzleRagIngestionLog");
|
|
2438
3646
|
}
|
|
2439
|
-
db;
|
|
2440
3647
|
logger = new import_common.Logger(_DrizzleRagIngestionLog.name);
|
|
2441
3648
|
teardowns = [];
|
|
2442
3649
|
inFlight = /* @__PURE__ */ new Set();
|
|
3650
|
+
db;
|
|
3651
|
+
dialect;
|
|
3652
|
+
t;
|
|
2443
3653
|
constructor(db) {
|
|
2444
|
-
this.
|
|
3654
|
+
this.dialect = agentDialectOf(db);
|
|
3655
|
+
this.t = agentTablesFor(this.dialect);
|
|
3656
|
+
this.db = asBuilder(db);
|
|
2445
3657
|
}
|
|
2446
3658
|
onModuleInit() {
|
|
2447
3659
|
for (const channel of CHANNELS) {
|
|
@@ -2470,7 +3682,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2470
3682
|
}
|
|
2471
3683
|
/** The latest recorded outcome per document, newest first. */
|
|
2472
3684
|
async list(query = {}) {
|
|
2473
|
-
const rows = this.db.select().from(ragIngestionLog).where(this.where(query)).orderBy(...RAG_INGESTION_LOG_PAGE_ORDER).limit(query.limit ?? DEFAULT_PAGE_SIZE);
|
|
3685
|
+
const rows = this.db.select().from(this.t.ragIngestionLog).where(this.where(query)).orderBy(...RAG_INGESTION_LOG_PAGE_ORDER).limit(query.limit ?? DEFAULT_PAGE_SIZE);
|
|
2474
3686
|
return query.offset === void 0 ? rows : rows.offset(query.offset);
|
|
2475
3687
|
}
|
|
2476
3688
|
/**
|
|
@@ -2483,8 +3695,8 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2483
3695
|
*/
|
|
2484
3696
|
async listPage(query = {}) {
|
|
2485
3697
|
const [totalRow] = await this.db.select({
|
|
2486
|
-
value: (0,
|
|
2487
|
-
}).from(ragIngestionLog).where(this.where(query));
|
|
3698
|
+
value: (0, import_drizzle_orm10.count)()
|
|
3699
|
+
}).from(this.t.ragIngestionLog).where(this.where(query));
|
|
2488
3700
|
return {
|
|
2489
3701
|
rows: await this.list(query),
|
|
2490
3702
|
total: totalRow?.value ?? 0
|
|
@@ -2537,7 +3749,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2537
3749
|
* sweep is run. A sweep that stops early stops by `break`ing out of the `for await`.
|
|
2538
3750
|
*/
|
|
2539
3751
|
iterate(where = {}, options = {}) {
|
|
2540
|
-
return this.sweep(where, batches(options.batchSize), options.after, (filter, limit) => this.db.select().from(ragIngestionLog).where(filter).orderBy(...RAG_INGESTION_LOG_PAGE_ORDER).limit(limit));
|
|
3752
|
+
return this.sweep(where, batches(options.batchSize), options.after, (filter, limit) => this.db.select().from(this.t.ragIngestionLog).where(filter).orderBy(...RAG_INGESTION_LOG_PAGE_ORDER).limit(limit));
|
|
2541
3753
|
}
|
|
2542
3754
|
/**
|
|
2543
3755
|
* Just the document ids of the matching rows, in {@link RAG_INGESTION_LOG_PAGE_ORDER}.
|
|
@@ -2554,7 +3766,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2554
3766
|
*/
|
|
2555
3767
|
async listDocumentIds(where = {}, options = {}) {
|
|
2556
3768
|
const ids = [];
|
|
2557
|
-
const rows = this.sweep(where, batches(options.batchSize), void 0, (filter, limit) => this.db.select(
|
|
3769
|
+
const rows = this.sweep(where, batches(options.batchSize), void 0, (filter, limit) => this.db.select(cursorColumns(this.t.ragIngestionLog)).from(this.t.ragIngestionLog).where(filter).orderBy(...RAG_INGESTION_LOG_PAGE_ORDER).limit(limit));
|
|
2558
3770
|
for await (const row of rows) {
|
|
2559
3771
|
ids.push(row.documentId);
|
|
2560
3772
|
}
|
|
@@ -2562,22 +3774,18 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2562
3774
|
}
|
|
2563
3775
|
/** The latest recorded outcome for one document, or null if it was never attempted. */
|
|
2564
3776
|
async get(documentId) {
|
|
2565
|
-
const [row] = await this.db.select().from(ragIngestionLog).where((0,
|
|
3777
|
+
const [row] = await this.db.select().from(this.t.ragIngestionLog).where((0, import_drizzle_orm10.eq)(this.t.ragIngestionLog.documentId, documentId));
|
|
2566
3778
|
return row ?? null;
|
|
2567
3779
|
}
|
|
2568
3780
|
/** Forget one document's record. Returns whether a row was actually removed. */
|
|
2569
3781
|
async remove(documentId) {
|
|
2570
|
-
const removed = await this.db.delete(ragIngestionLog).where((0,
|
|
2571
|
-
|
|
2572
|
-
});
|
|
2573
|
-
return removed.length > 0;
|
|
3782
|
+
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);
|
|
3783
|
+
return removed > 0;
|
|
2574
3784
|
}
|
|
2575
3785
|
/** Forget every record for a collection — for when the collection itself is deleted. */
|
|
2576
3786
|
async removeByCollection(collection) {
|
|
2577
|
-
const removed = await this.db.delete(ragIngestionLog).where((0,
|
|
2578
|
-
|
|
2579
|
-
});
|
|
2580
|
-
return removed.length;
|
|
3787
|
+
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);
|
|
3788
|
+
return removed;
|
|
2581
3789
|
}
|
|
2582
3790
|
/**
|
|
2583
3791
|
* The keyset sweep both {@link iterate} and {@link listDocumentIds} are made of. `page` decides
|
|
@@ -2606,7 +3814,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2606
3814
|
}
|
|
2607
3815
|
}
|
|
2608
3816
|
where(query) {
|
|
2609
|
-
return (0,
|
|
3817
|
+
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);
|
|
2610
3818
|
}
|
|
2611
3819
|
/**
|
|
2612
3820
|
* The coordinates filter AND'd with "strictly after `cursor` in
|
|
@@ -2621,7 +3829,7 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2621
3829
|
if (cursor === void 0) {
|
|
2622
3830
|
return base;
|
|
2623
3831
|
}
|
|
2624
|
-
return (0,
|
|
3832
|
+
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))));
|
|
2625
3833
|
}
|
|
2626
3834
|
/**
|
|
2627
3835
|
* Upsert one outcome. Keyed by document id so the row always reflects the *current* state — a
|
|
@@ -2669,14 +3877,11 @@ var DrizzleRagIngestionLog = class _DrizzleRagIngestionLog {
|
|
|
2669
3877
|
size
|
|
2670
3878
|
} : {}
|
|
2671
3879
|
};
|
|
2672
|
-
await this.db.insert(ragIngestionLog).values({
|
|
3880
|
+
await upsert(this.dialect, this.db.insert(this.t.ragIngestionLog).values({
|
|
2673
3881
|
documentId,
|
|
2674
3882
|
createdAt: now,
|
|
2675
3883
|
...outcome
|
|
2676
|
-
}).
|
|
2677
|
-
target: ragIngestionLog.documentId,
|
|
2678
|
-
set: outcome
|
|
2679
|
-
});
|
|
3884
|
+
}), this.t.ragIngestionLog.documentId, outcome);
|
|
2680
3885
|
} catch (error) {
|
|
2681
3886
|
this.logger.warn(`Could not record RAG ingestion outcome for ${documentId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2682
3887
|
}
|
|
@@ -2697,20 +3902,20 @@ DrizzleRagIngestionLog = _ts_decorate([
|
|
|
2697
3902
|
], DrizzleRagIngestionLog);
|
|
2698
3903
|
|
|
2699
3904
|
// src/drizzle-agent-store.module.ts
|
|
2700
|
-
var
|
|
3905
|
+
var import_nestjs_agent_core8 = require("@dudousxd/nestjs-agent-core");
|
|
2701
3906
|
var import_common2 = require("@nestjs/common");
|
|
2702
|
-
function _ts_decorate2(decorators, target,
|
|
2703
|
-
var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target,
|
|
3907
|
+
function _ts_decorate2(decorators, target, key2, desc4) {
|
|
3908
|
+
var c = arguments.length, r = c < 3 ? target : desc4 === null ? desc4 = Object.getOwnPropertyDescriptor(target, key2) : desc4, d;
|
|
2704
3909
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") {
|
|
2705
|
-
r = Reflect.decorate(decorators, target,
|
|
3910
|
+
r = Reflect.decorate(decorators, target, key2, desc4);
|
|
2706
3911
|
} else {
|
|
2707
3912
|
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
2708
3913
|
if (d = decorators[i]) {
|
|
2709
|
-
r = (c < 3 ? d(r) : c > 3 ? d(target,
|
|
3914
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key2, r) : d(target, key2)) || r;
|
|
2710
3915
|
}
|
|
2711
3916
|
}
|
|
2712
3917
|
}
|
|
2713
|
-
return c > 3 && r && Object.defineProperty(target,
|
|
3918
|
+
return c > 3 && r && Object.defineProperty(target, key2, r), r;
|
|
2714
3919
|
}
|
|
2715
3920
|
__name(_ts_decorate2, "_ts_decorate");
|
|
2716
3921
|
var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
|
|
@@ -2734,7 +3939,7 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
|
|
|
2734
3939
|
useFactory: /* @__PURE__ */ __name(() => new DrizzleAgentStore(options.db), "useFactory")
|
|
2735
3940
|
},
|
|
2736
3941
|
{
|
|
2737
|
-
provide:
|
|
3942
|
+
provide: import_nestjs_agent_core8.AGENT_STORE,
|
|
2738
3943
|
useExisting: DrizzleAgentStore
|
|
2739
3944
|
},
|
|
2740
3945
|
// Exported, never bound to AGENT_MEMORY: that token's PRESENCE is what turns memory on, so
|
|
@@ -2751,21 +3956,21 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
|
|
|
2751
3956
|
// AGENT_PRICING_STORE: the host's own store when supplied, else the default Drizzle one.
|
|
2752
3957
|
// DrizzleGovernanceQueries prices usage against whatever is bound here.
|
|
2753
3958
|
options.pricingStore ? {
|
|
2754
|
-
provide:
|
|
3959
|
+
provide: import_nestjs_agent_core8.AGENT_PRICING_STORE,
|
|
2755
3960
|
useClass: options.pricingStore
|
|
2756
3961
|
} : {
|
|
2757
|
-
provide:
|
|
3962
|
+
provide: import_nestjs_agent_core8.AGENT_PRICING_STORE,
|
|
2758
3963
|
useExisting: DrizzlePricingStore
|
|
2759
3964
|
},
|
|
2760
3965
|
{
|
|
2761
3966
|
provide: DrizzleGovernanceQueries,
|
|
2762
3967
|
useFactory: /* @__PURE__ */ __name((pricingStore) => new DrizzleGovernanceQueries(options.db, pricingStore), "useFactory"),
|
|
2763
3968
|
inject: [
|
|
2764
|
-
|
|
3969
|
+
import_nestjs_agent_core8.AGENT_PRICING_STORE
|
|
2765
3970
|
]
|
|
2766
3971
|
},
|
|
2767
3972
|
{
|
|
2768
|
-
provide:
|
|
3973
|
+
provide: import_nestjs_agent_core8.AGENT_GOVERNANCE_QUERIES,
|
|
2769
3974
|
useExisting: DrizzleGovernanceQueries
|
|
2770
3975
|
},
|
|
2771
3976
|
// What makes a `defineConfirmedTool` confirmation single use across replicas.
|
|
@@ -2774,7 +3979,7 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
|
|
|
2774
3979
|
useFactory: /* @__PURE__ */ __name(() => new DrizzleConfirmTokenStore(options.db), "useFactory")
|
|
2775
3980
|
},
|
|
2776
3981
|
{
|
|
2777
|
-
provide:
|
|
3982
|
+
provide: import_nestjs_agent_core8.AGENT_CONFIRM_TOKEN_STORE,
|
|
2778
3983
|
useExisting: DrizzleConfirmTokenStore
|
|
2779
3984
|
}
|
|
2780
3985
|
],
|
|
@@ -2783,14 +3988,14 @@ var DrizzleAgentStoreModule = class _DrizzleAgentStoreModule {
|
|
|
2783
3988
|
DrizzleRagIngestionLog
|
|
2784
3989
|
],
|
|
2785
3990
|
DrizzleAgentStore,
|
|
2786
|
-
|
|
3991
|
+
import_nestjs_agent_core8.AGENT_STORE,
|
|
2787
3992
|
DrizzleMemoryProvider,
|
|
2788
3993
|
DrizzleGovernanceQueries,
|
|
2789
|
-
|
|
3994
|
+
import_nestjs_agent_core8.AGENT_GOVERNANCE_QUERIES,
|
|
2790
3995
|
DrizzlePricingStore,
|
|
2791
|
-
|
|
3996
|
+
import_nestjs_agent_core8.AGENT_PRICING_STORE,
|
|
2792
3997
|
DrizzleConfirmTokenStore,
|
|
2793
|
-
|
|
3998
|
+
import_nestjs_agent_core8.AGENT_CONFIRM_TOKEN_STORE
|
|
2794
3999
|
]
|
|
2795
4000
|
};
|
|
2796
4001
|
}
|
|
@@ -2800,6 +4005,7 @@ DrizzleAgentStoreModule = _ts_decorate2([
|
|
|
2800
4005
|
], DrizzleAgentStoreModule);
|
|
2801
4006
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2802
4007
|
0 && (module.exports = {
|
|
4008
|
+
DrizzleActionProposalStore,
|
|
2803
4009
|
DrizzleAgentStore,
|
|
2804
4010
|
DrizzleAgentStoreModule,
|
|
2805
4011
|
DrizzleConfirmTokenStore,
|
|
@@ -2810,18 +4016,23 @@ DrizzleAgentStoreModule = _ts_decorate2([
|
|
|
2810
4016
|
DrizzleStreamFrameTable,
|
|
2811
4017
|
DrizzleTokenStreamSink,
|
|
2812
4018
|
RAG_INGESTION_LOG_PAGE_ORDER,
|
|
4019
|
+
agentActionProposal,
|
|
2813
4020
|
agentConfirmToken,
|
|
4021
|
+
agentDialectOf,
|
|
2814
4022
|
agentMemory,
|
|
2815
4023
|
agentMessage,
|
|
2816
4024
|
agentModelPricing,
|
|
2817
4025
|
agentQueuedMessage,
|
|
2818
4026
|
agentRun,
|
|
2819
4027
|
agentSchema,
|
|
4028
|
+
agentSchemaDdl,
|
|
2820
4029
|
agentStreamFrame,
|
|
2821
4030
|
agentThread,
|
|
2822
4031
|
agentTokenUsage,
|
|
2823
4032
|
agentToolCall,
|
|
2824
4033
|
ensureAgentSchema,
|
|
4034
|
+
mysqlAgentSchema,
|
|
4035
|
+
pgAgentSchema,
|
|
2825
4036
|
ragIngestionLog
|
|
2826
4037
|
});
|
|
2827
4038
|
//# sourceMappingURL=index.cjs.map
|