@apart-tech/intelligence-core 1.17.3 → 1.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/ability.d.ts +1 -1
- package/dist/auth/ability.d.ts.map +1 -1
- package/dist/auth/ability.js +4 -0
- package/dist/auth/ability.js.map +1 -1
- package/dist/auth/ability.test.js +14 -3
- package/dist/auth/ability.test.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/services/__tests__/agent-instruction-service.test.d.ts +2 -0
- package/dist/services/__tests__/agent-instruction-service.test.d.ts.map +1 -0
- package/dist/services/__tests__/agent-instruction-service.test.js +408 -0
- package/dist/services/__tests__/agent-instruction-service.test.js.map +1 -0
- package/dist/services/agent-instruction-service.d.ts +82 -0
- package/dist/services/agent-instruction-service.d.ts.map +1 -0
- package/dist/services/agent-instruction-service.js +249 -0
- package/dist/services/agent-instruction-service.js.map +1 -0
- package/dist/services/agent-run-service.d.ts +61 -0
- package/dist/services/agent-run-service.d.ts.map +1 -1
- package/dist/services/agent-run-service.js +72 -0
- package/dist/services/agent-run-service.js.map +1 -1
- package/package.json +1 -1
- package/prisma/schema.prisma +209 -162
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
// ── Service ────────────────────────────────────────────────────────────────
|
|
3
|
+
export class AgentInstructionService {
|
|
4
|
+
db;
|
|
5
|
+
constructor(db) {
|
|
6
|
+
this.db = db;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Compute a SHA-256 hash of the instruction content fields. Deterministic
|
|
10
|
+
* through sorted-key canonical JSON so the same logical content always
|
|
11
|
+
* produces the same hash regardless of property insertion order.
|
|
12
|
+
*/
|
|
13
|
+
static computeHash(input) {
|
|
14
|
+
const canonical = JSON.stringify(input, Object.keys(input).sort());
|
|
15
|
+
return createHash("sha256").update(canonical).digest("hex");
|
|
16
|
+
}
|
|
17
|
+
async create(organizationId, input) {
|
|
18
|
+
const contentHash = AgentInstructionService.computeHash({
|
|
19
|
+
systemPrompt: input.systemPrompt ?? "",
|
|
20
|
+
tools: input.tools ?? [],
|
|
21
|
+
guardrails: input.guardrails ?? {},
|
|
22
|
+
modelConfig: input.modelConfig ?? {},
|
|
23
|
+
});
|
|
24
|
+
const row = await this.db.agentInstruction.create({
|
|
25
|
+
data: {
|
|
26
|
+
organizationId,
|
|
27
|
+
agentTypeId: input.agentTypeId ?? null,
|
|
28
|
+
taskType: input.taskType ?? null,
|
|
29
|
+
version: input.version,
|
|
30
|
+
status: "draft",
|
|
31
|
+
systemPrompt: input.systemPrompt ?? "",
|
|
32
|
+
tools: input.tools ?? [],
|
|
33
|
+
guardrails: input.guardrails ?? {},
|
|
34
|
+
modelConfig: input.modelConfig ?? {},
|
|
35
|
+
contentHash,
|
|
36
|
+
changeNote: input.changeNote ?? null,
|
|
37
|
+
createdBy: input.createdBy,
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
return this.toRecord(row);
|
|
41
|
+
}
|
|
42
|
+
async updateDraft(id, organizationId, input) {
|
|
43
|
+
const existing = await this.db.agentInstruction.findFirst({
|
|
44
|
+
where: { id, organizationId },
|
|
45
|
+
});
|
|
46
|
+
if (!existing)
|
|
47
|
+
throw new Error("Instruction not found");
|
|
48
|
+
if (existing.status !== "draft") {
|
|
49
|
+
throw new Error("Only draft instructions can be updated");
|
|
50
|
+
}
|
|
51
|
+
const merged = {
|
|
52
|
+
systemPrompt: input.systemPrompt ?? existing.systemPrompt,
|
|
53
|
+
tools: input.tools ?? existing.tools,
|
|
54
|
+
guardrails: input.guardrails ?? existing.guardrails,
|
|
55
|
+
modelConfig: input.modelConfig ?? existing.modelConfig,
|
|
56
|
+
};
|
|
57
|
+
const contentHash = AgentInstructionService.computeHash(merged);
|
|
58
|
+
const row = await this.db.agentInstruction.update({
|
|
59
|
+
where: { id },
|
|
60
|
+
data: {
|
|
61
|
+
systemPrompt: merged.systemPrompt,
|
|
62
|
+
tools: merged.tools,
|
|
63
|
+
guardrails: merged.guardrails,
|
|
64
|
+
modelConfig: merged.modelConfig,
|
|
65
|
+
contentHash,
|
|
66
|
+
changeNote: input.changeNote ?? existing.changeNote,
|
|
67
|
+
version: input.version ?? existing.version,
|
|
68
|
+
updatedAt: new Date(),
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
return this.toRecord(row);
|
|
72
|
+
}
|
|
73
|
+
async publish(id, organizationId) {
|
|
74
|
+
const existing = await this.db.agentInstruction.findFirst({
|
|
75
|
+
where: { id, organizationId },
|
|
76
|
+
});
|
|
77
|
+
if (!existing)
|
|
78
|
+
throw new Error("Instruction not found");
|
|
79
|
+
if (existing.status !== "draft") {
|
|
80
|
+
throw new Error("Only draft instructions can be published");
|
|
81
|
+
}
|
|
82
|
+
// Recompute hash on publish to capture any intermediate edits
|
|
83
|
+
const contentHash = AgentInstructionService.computeHash({
|
|
84
|
+
systemPrompt: existing.systemPrompt,
|
|
85
|
+
tools: existing.tools,
|
|
86
|
+
guardrails: existing.guardrails,
|
|
87
|
+
modelConfig: existing.modelConfig,
|
|
88
|
+
});
|
|
89
|
+
const now = new Date();
|
|
90
|
+
// Auto-deprecate the currently published version for the same scope
|
|
91
|
+
const currentPublished = await this.db.agentInstruction.findFirst({
|
|
92
|
+
where: {
|
|
93
|
+
organizationId,
|
|
94
|
+
agentTypeId: existing.agentTypeId,
|
|
95
|
+
taskType: existing.taskType,
|
|
96
|
+
status: "published",
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
if (currentPublished) {
|
|
100
|
+
await this.db.agentInstruction.update({
|
|
101
|
+
where: { id: currentPublished.id },
|
|
102
|
+
data: { status: "deprecated", deprecatedAt: now, updatedAt: now },
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const row = await this.db.agentInstruction.update({
|
|
106
|
+
where: { id },
|
|
107
|
+
data: {
|
|
108
|
+
status: "published",
|
|
109
|
+
contentHash,
|
|
110
|
+
publishedAt: now,
|
|
111
|
+
updatedAt: now,
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
return this.toRecord(row);
|
|
115
|
+
}
|
|
116
|
+
async deprecate(id, organizationId) {
|
|
117
|
+
const existing = await this.db.agentInstruction.findFirst({
|
|
118
|
+
where: { id, organizationId },
|
|
119
|
+
});
|
|
120
|
+
if (!existing)
|
|
121
|
+
throw new Error("Instruction not found");
|
|
122
|
+
if (existing.status !== "published") {
|
|
123
|
+
throw new Error("Only published instructions can be deprecated");
|
|
124
|
+
}
|
|
125
|
+
const now = new Date();
|
|
126
|
+
const row = await this.db.agentInstruction.update({
|
|
127
|
+
where: { id },
|
|
128
|
+
data: { status: "deprecated", deprecatedAt: now, updatedAt: now },
|
|
129
|
+
});
|
|
130
|
+
return this.toRecord(row);
|
|
131
|
+
}
|
|
132
|
+
async getById(id, organizationId) {
|
|
133
|
+
const row = await this.db.agentInstruction.findFirst({
|
|
134
|
+
where: { id, organizationId },
|
|
135
|
+
});
|
|
136
|
+
return row ? this.toRecord(row) : null;
|
|
137
|
+
}
|
|
138
|
+
async list(organizationId, opts) {
|
|
139
|
+
const rows = await this.db.agentInstruction.findMany({
|
|
140
|
+
where: {
|
|
141
|
+
organizationId,
|
|
142
|
+
...(opts?.agentTypeId ? { agentTypeId: opts.agentTypeId } : {}),
|
|
143
|
+
...(opts?.taskType !== undefined ? { taskType: opts.taskType } : {}),
|
|
144
|
+
...(opts?.status ? { status: opts.status } : {}),
|
|
145
|
+
},
|
|
146
|
+
orderBy: { createdAt: "desc" },
|
|
147
|
+
take: opts?.limit ?? 50,
|
|
148
|
+
});
|
|
149
|
+
return rows.map((r) => this.toRecord(r));
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Resolve the active published instruction for a given scope.
|
|
153
|
+
* Resolution order: task-specific published → global published → null.
|
|
154
|
+
*/
|
|
155
|
+
async getPublished(organizationId, agentTypeId, taskType) {
|
|
156
|
+
// Try task-specific first
|
|
157
|
+
if (taskType) {
|
|
158
|
+
const specific = await this.db.agentInstruction.findFirst({
|
|
159
|
+
where: {
|
|
160
|
+
organizationId,
|
|
161
|
+
agentTypeId: agentTypeId ?? null,
|
|
162
|
+
taskType,
|
|
163
|
+
status: "published",
|
|
164
|
+
},
|
|
165
|
+
orderBy: { publishedAt: "desc" },
|
|
166
|
+
});
|
|
167
|
+
if (specific)
|
|
168
|
+
return this.toRecord(specific);
|
|
169
|
+
}
|
|
170
|
+
// Fall back to global (taskType = null)
|
|
171
|
+
const global = await this.db.agentInstruction.findFirst({
|
|
172
|
+
where: {
|
|
173
|
+
organizationId,
|
|
174
|
+
agentTypeId: agentTypeId ?? null,
|
|
175
|
+
taskType: null,
|
|
176
|
+
status: "published",
|
|
177
|
+
},
|
|
178
|
+
orderBy: { publishedAt: "desc" },
|
|
179
|
+
});
|
|
180
|
+
return global ? this.toRecord(global) : null;
|
|
181
|
+
}
|
|
182
|
+
async fork(id, organizationId, newVersion, createdBy) {
|
|
183
|
+
const source = await this.db.agentInstruction.findFirst({
|
|
184
|
+
where: { id, organizationId },
|
|
185
|
+
});
|
|
186
|
+
if (!source)
|
|
187
|
+
throw new Error("Instruction not found");
|
|
188
|
+
const contentHash = AgentInstructionService.computeHash({
|
|
189
|
+
systemPrompt: source.systemPrompt,
|
|
190
|
+
tools: source.tools,
|
|
191
|
+
guardrails: source.guardrails,
|
|
192
|
+
modelConfig: source.modelConfig,
|
|
193
|
+
});
|
|
194
|
+
const row = await this.db.agentInstruction.create({
|
|
195
|
+
data: {
|
|
196
|
+
organizationId,
|
|
197
|
+
agentTypeId: source.agentTypeId,
|
|
198
|
+
taskType: source.taskType,
|
|
199
|
+
version: newVersion,
|
|
200
|
+
status: "draft",
|
|
201
|
+
systemPrompt: source.systemPrompt,
|
|
202
|
+
tools: source.tools,
|
|
203
|
+
guardrails: source.guardrails,
|
|
204
|
+
modelConfig: source.modelConfig,
|
|
205
|
+
contentHash,
|
|
206
|
+
parentVersionId: source.id,
|
|
207
|
+
changeNote: `Forked from v${source.version}`,
|
|
208
|
+
createdBy,
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
return this.toRecord(row);
|
|
212
|
+
}
|
|
213
|
+
async diff(idA, idB, organizationId) {
|
|
214
|
+
const [a, b] = await Promise.all([
|
|
215
|
+
this.getById(idA, organizationId),
|
|
216
|
+
this.getById(idB, organizationId),
|
|
217
|
+
]);
|
|
218
|
+
if (!a)
|
|
219
|
+
throw new Error(`Instruction ${idA} not found`);
|
|
220
|
+
if (!b)
|
|
221
|
+
throw new Error(`Instruction ${idB} not found`);
|
|
222
|
+
return { a, b };
|
|
223
|
+
}
|
|
224
|
+
toRecord(row) {
|
|
225
|
+
return {
|
|
226
|
+
id: row.id,
|
|
227
|
+
organizationId: row.organizationId,
|
|
228
|
+
agentTypeId: row.agentTypeId ?? null,
|
|
229
|
+
taskType: row.taskType ?? null,
|
|
230
|
+
version: row.version,
|
|
231
|
+
status: row.status,
|
|
232
|
+
systemPrompt: row.systemPrompt,
|
|
233
|
+
tools: row.tools,
|
|
234
|
+
guardrails: row.guardrails,
|
|
235
|
+
modelConfig: row.modelConfig,
|
|
236
|
+
contentHash: row.contentHash ?? null,
|
|
237
|
+
parentVersionId: row.parentVersionId ?? null,
|
|
238
|
+
changeNote: row.changeNote ?? null,
|
|
239
|
+
publishedAt: row.publishedAt ?? null,
|
|
240
|
+
deprecatedAt: row.deprecatedAt ?? null,
|
|
241
|
+
effectiveFrom: row.effectiveFrom ?? null,
|
|
242
|
+
effectiveTo: row.effectiveTo ?? null,
|
|
243
|
+
createdBy: row.createdBy,
|
|
244
|
+
createdAt: row.createdAt,
|
|
245
|
+
updatedAt: row.updatedAt,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=agent-instruction-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-instruction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AC;IAEO,QAAQ,CAAC,GAAQ;QACvB,OAAO;YACL,EAAE,EAAE,GAAG,CAAC,EAAE;YACV,cAAc,EAAE,GAAG,CAAC,cAAc;YAClC,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,IAAI;YACpC,QAAQ,EAAE,GAAG,CAAC,QAAQ,IAAI,IAAI;YAC9B,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,MAAM,EAAE,GAAG,CAAC,MAA2B;YACvC,YAAY,EAAE,GAAG,CAAC,YAAY;YAC9B,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,UAAU,EAAE,GAAG,CAAC,UAAU;YAC1B,WAAW,EAAE,GAAG,CAAC,WAAW;YAC5B,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,IAAI;YACpC,eAAe,EAAE,GAAG,CAAC,eAAe,IAAI,IAAI;YAC5C,UAAU,EAAE,GAAG,CAAC,UAAU,IAAI,IAAI;YAClC,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,IAAI;YACpC,YAAY,EAAE,GAAG,CAAC,YAAY,IAAI,IAAI;YACtC,aAAa,EAAE,GAAG,CAAC,aAAa,IAAI,IAAI;YACxC,WAAW,EAAE,GAAG,CAAC,WAAW,IAAI,IAAI;YACpC,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,SAAS,EAAE,GAAG,CAAC,SAAS;YACxB,SAAS,EAAE,GAAG,CAAC,SAAS;SACzB,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -32,6 +32,46 @@ export interface AgentRun {
|
|
|
32
32
|
* before use.
|
|
33
33
|
*/
|
|
34
34
|
capturedAbility: unknown;
|
|
35
|
+
/** FK to `AgentInstruction.id` — the instruction version pinned at spawn time. */
|
|
36
|
+
instructionId: string | null;
|
|
37
|
+
/** Accumulated reasoning trace entries (append-only JSONB array). */
|
|
38
|
+
reasoningTrace: unknown;
|
|
39
|
+
/** Accumulated tool call log entries (append-only JSONB array). */
|
|
40
|
+
toolsCalled: unknown;
|
|
41
|
+
/** Structured decision outcome set at completion. */
|
|
42
|
+
outcome: unknown;
|
|
43
|
+
/** Confidence score (0–1) associated with the outcome. */
|
|
44
|
+
confidence: number | null;
|
|
45
|
+
/** Task type discriminator for instruction resolution. */
|
|
46
|
+
taskType: string | null;
|
|
47
|
+
/** Outcome status (e.g. "approved", "rejected", "escalated"). */
|
|
48
|
+
outcomeStatus: string | null;
|
|
49
|
+
}
|
|
50
|
+
export interface TraceEntry {
|
|
51
|
+
type: string;
|
|
52
|
+
content: string;
|
|
53
|
+
timestamp: string;
|
|
54
|
+
metadata?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
export interface ToolCallEntry {
|
|
57
|
+
tool: string;
|
|
58
|
+
args: unknown;
|
|
59
|
+
result: unknown;
|
|
60
|
+
timestamp: string;
|
|
61
|
+
durationMs?: number;
|
|
62
|
+
}
|
|
63
|
+
export interface OutcomeInput {
|
|
64
|
+
decision: string;
|
|
65
|
+
confidence?: number;
|
|
66
|
+
reasoning?: string;
|
|
67
|
+
alternativesConsidered?: string[];
|
|
68
|
+
}
|
|
69
|
+
export interface AgentRunTrace {
|
|
70
|
+
reasoningTrace: unknown;
|
|
71
|
+
toolsCalled: unknown;
|
|
72
|
+
outcome: unknown;
|
|
73
|
+
confidence: number | null;
|
|
74
|
+
outcomeStatus: string | null;
|
|
35
75
|
}
|
|
36
76
|
export interface AgentRunCreateInput {
|
|
37
77
|
prompt: string;
|
|
@@ -55,6 +95,10 @@ export interface AgentRunCreateInput {
|
|
|
55
95
|
* `OrgAgentPrincipal` path in the middleware grants `manage all`.
|
|
56
96
|
*/
|
|
57
97
|
capturedAbility?: unknown;
|
|
98
|
+
/** FK to `AgentInstruction.id` — the instruction version pinned at spawn time. */
|
|
99
|
+
instructionId?: string | null;
|
|
100
|
+
/** Task type discriminator for instruction resolution. */
|
|
101
|
+
taskType?: string | null;
|
|
58
102
|
}
|
|
59
103
|
export declare class AgentRunService {
|
|
60
104
|
private db;
|
|
@@ -73,6 +117,23 @@ export declare class AgentRunService {
|
|
|
73
117
|
status?: string;
|
|
74
118
|
type?: string;
|
|
75
119
|
}): Promise<AgentRun[]>;
|
|
120
|
+
/**
|
|
121
|
+
* Append a reasoning trace entry to the run's reasoning_trace JSONB array.
|
|
122
|
+
* Uses raw SQL with COALESCE for crash-safe append without read-modify-write.
|
|
123
|
+
*/
|
|
124
|
+
appendTrace(id: string, organizationId: string, entry: TraceEntry): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Append a tool call entry to the run's tools_called JSONB array.
|
|
127
|
+
*/
|
|
128
|
+
appendToolCall(id: string, organizationId: string, entry: ToolCallEntry): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Set the structured outcome and optional confidence/status on a run.
|
|
131
|
+
*/
|
|
132
|
+
setOutcome(id: string, organizationId: string, input: OutcomeInput, outcomeStatus?: string): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Get the full trace, tool calls, outcome, and confidence for a run.
|
|
135
|
+
*/
|
|
136
|
+
getTrace(id: string, organizationId: string): Promise<AgentRunTrace | null>;
|
|
76
137
|
private toAgentRun;
|
|
77
138
|
}
|
|
78
139
|
//# sourceMappingURL=agent-run-service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-run-service.d.ts","sourceRoot":"","sources":["../../src/services/agent-run-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,IAAI,CAAC;IAChB,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;;OAIG;IACH,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB;;;;OAIG;IACH,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B;;;;;;OAMG;IACH,eAAe,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"agent-run-service.d.ts","sourceRoot":"","sources":["../../src/services/agent-run-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,IAAI,CAAC;IAChB,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B;;;;OAIG;IACH,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB;;;;OAIG;IACH,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B;;;;;;OAMG;IACH,eAAe,EAAE,OAAO,CAAC;IACzB,kFAAkF;IAClF,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,qEAAqE;IACrE,cAAc,EAAE,OAAO,CAAC;IACxB,mEAAmE;IACnE,WAAW,EAAE,OAAO,CAAC;IACrB,qDAAqD;IACrD,OAAO,EAAE,OAAO,CAAC;IACjB,0DAA0D;IAC1D,UAAU,EA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V,cAAc,EAAE,MAAM,EACtB,KAAK,EAAE,YAAY,EACnB,aAAa,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,IAAI,CAAC;IAYhB;;OAEG;IACG,QAAQ,CACZ,EAAE,EAAE,MAAM,EACV,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAqBhC,OAAO,CAAC,UAAU;CA0BnB"}
|
|
@@ -24,6 +24,8 @@ export class AgentRunService {
|
|
|
24
24
|
capturedAbility: input.capturedAbility == null
|
|
25
25
|
? undefined
|
|
26
26
|
: input.capturedAbility,
|
|
27
|
+
instructionId: input.instructionId ?? null,
|
|
28
|
+
taskType: input.taskType ?? null,
|
|
27
29
|
},
|
|
28
30
|
});
|
|
29
31
|
return this.toAgentRun(run);
|
|
@@ -61,6 +63,69 @@ export class AgentRunService {
|
|
|
61
63
|
});
|
|
62
64
|
return runs.map((r) => this.toAgentRun(r));
|
|
63
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Append a reasoning trace entry to the run's reasoning_trace JSONB array.
|
|
68
|
+
* Uses raw SQL with COALESCE for crash-safe append without read-modify-write.
|
|
69
|
+
*/
|
|
70
|
+
async appendTrace(id, organizationId, entry) {
|
|
71
|
+
const entryJson = JSON.stringify(entry);
|
|
72
|
+
await this.db.$executeRaw `
|
|
73
|
+
UPDATE agent_runs
|
|
74
|
+
SET reasoning_trace = COALESCE(reasoning_trace, '[]'::jsonb) || ${entryJson}::jsonb,
|
|
75
|
+
updated_at = NOW()
|
|
76
|
+
WHERE id = ${id}::uuid AND organization_id = ${organizationId}::uuid
|
|
77
|
+
`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Append a tool call entry to the run's tools_called JSONB array.
|
|
81
|
+
*/
|
|
82
|
+
async appendToolCall(id, organizationId, entry) {
|
|
83
|
+
const entryJson = JSON.stringify(entry);
|
|
84
|
+
await this.db.$executeRaw `
|
|
85
|
+
UPDATE agent_runs
|
|
86
|
+
SET tools_called = COALESCE(tools_called, '[]'::jsonb) || ${entryJson}::jsonb,
|
|
87
|
+
updated_at = NOW()
|
|
88
|
+
WHERE id = ${id}::uuid AND organization_id = ${organizationId}::uuid
|
|
89
|
+
`;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Set the structured outcome and optional confidence/status on a run.
|
|
93
|
+
*/
|
|
94
|
+
async setOutcome(id, organizationId, input, outcomeStatus) {
|
|
95
|
+
const outcomeJson = JSON.stringify(input);
|
|
96
|
+
await this.db.$executeRaw `
|
|
97
|
+
UPDATE agent_runs
|
|
98
|
+
SET outcome = ${outcomeJson}::jsonb,
|
|
99
|
+
confidence = ${input.confidence ?? null},
|
|
100
|
+
outcome_status = ${outcomeStatus ?? null},
|
|
101
|
+
updated_at = NOW()
|
|
102
|
+
WHERE id = ${id}::uuid AND organization_id = ${organizationId}::uuid
|
|
103
|
+
`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get the full trace, tool calls, outcome, and confidence for a run.
|
|
107
|
+
*/
|
|
108
|
+
async getTrace(id, organizationId) {
|
|
109
|
+
const run = await this.db.agentRun.findFirst({
|
|
110
|
+
where: { id, organizationId },
|
|
111
|
+
select: {
|
|
112
|
+
reasoningTrace: true,
|
|
113
|
+
toolsCalled: true,
|
|
114
|
+
outcome: true,
|
|
115
|
+
confidence: true,
|
|
116
|
+
outcomeStatus: true,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
if (!run)
|
|
120
|
+
return null;
|
|
121
|
+
return {
|
|
122
|
+
reasoningTrace: run.reasoningTrace,
|
|
123
|
+
toolsCalled: run.toolsCalled,
|
|
124
|
+
outcome: run.outcome,
|
|
125
|
+
confidence: run.confidence ? Number(run.confidence) : null,
|
|
126
|
+
outcomeStatus: run.outcomeStatus ?? null,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
64
129
|
toAgentRun(run) {
|
|
65
130
|
return {
|
|
66
131
|
id: run.id,
|
|
@@ -78,6 +143,13 @@ export class AgentRunService {
|
|
|
78
143
|
agentId: run.agentId ?? null,
|
|
79
144
|
invokedByUserId: run.invokedByUserId ?? null,
|
|
80
145
|
capturedAbility: run.capturedAbility ?? null,
|
|
146
|
+
instructionId: run.instructionId ?? null,
|
|
147
|
+
reasoningTrace: run.reasoningTrace ?? null,
|
|
148
|
+
toolsCalled: run.toolsCalled ?? null,
|
|
149
|
+
outcome: run.outcome ?? null,
|
|
150
|
+
confidence: run.confidence != null ? Number(run.confidence) : null,
|
|
151
|
+
taskType: run.taskType ?? null,
|
|
152
|
+
outcomeStatus: run.outcomeStatus ?? null,
|
|
81
153
|
};
|
|
82
154
|
}
|
|
83
155
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-run-service.js","sourceRoot":"","sources":["../../src/services/agent-run-service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"agent-run-service.js","sourceRoot":"","sources":["../../src/services/agent-run-service.ts"],"names":[],"mappings":"AA6GA,MAAM,OAAO,eAAe;IACN;IAApB,YAAoB,EAAgB;QAAhB,OAAE,GAAF,EAAE,CAAc;IAAG,CAAC;IAExC,KAAK,CAAC,MAAM,CACV,cAAsB,EACtB,KAA0B;QAE1B,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC;YACxC,IAAI,EAAE;gBACJ,cAAc;gBACd,MAAM,EAAE,KAAK,CAAC,MAAM;gBACpB,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,YAAY;gBAChC,KAAK,EAAE,KAAK,CAAC,KAAK;gBAClB,MAAM,EAAE,SAAS;gBACjB,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,UAAU,EAAE,KAAK,CAAC,UAAU,IAAI,IAAI;gBACpC,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,IAAI;gBAC9B,eAAe,EAAE,KAAK,CAAC,eAAe,IAAI,IAAI;gBAC9C,+DAA+D;gBAC/D,+D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