-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunTurnUseCase.ts
More file actions
582 lines (521 loc) · 19.4 KB
/
Copy pathrunTurnUseCase.ts
File metadata and controls
582 lines (521 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
import { logger, Filters, type IAdminForth } from "adminforth";
import { randomUUID } from "crypto";
import { VegaLiteStreamBuffer } from "../domain/vegaLiteStreamBuffer.js";
import { getErrorMessage, isAbortError } from "../shared/errors.js";
import type { DetectedLanguage, PreviousUserMessage } from "../domain/languageDetect.js";
import type { AgentSessionStore } from "../persistence/sessionStore.js";
import type { SteerBuffer } from "../domain/steerBuffer.js";
import type { PluginOptions } from "../types.js";
import type { LlmPort } from "./ports.js";
import type {
AgentMessage,
AgentTurnContext,
AgentTurnObservability,
BaseAgentTurnInput,
DebugSink,
HandleTurnInput,
HandleEditTurnInput,
PendingInterrupt,
RunAndPersistAgentResponseInput,
RunAndPersistAgentResponseResult,
} from "../domain/turnTypes.js";
type AgentMode = PluginOptions["modes"][number];
type PreparedTurn = {
prompt: string;
sessionId: string;
turnId: string;
previousUserMessages: PreviousUserMessage[];
mode: AgentMode;
context: AgentTurnContext;
observability: AgentTurnObservability;
resume?: { decision: "approve" | "reject"; interrupts?: PendingInterrupt[] };
initialResponse?: string;
branchFromCheckpointId?: string;
};
function getApprovalDecision(input: BaseAgentTurnInput) {
return "approvalDecision" in input
&& (input.approvalDecision === "approve" || input.approvalDecision === "reject")
? input.approvalDecision
: undefined;
}
function buildHitlDecision(decision: "approve" | "reject", prompt?: string) {
if (decision === "approve") {
return { type: "approve" as const };
}
return {
type: "reject" as const,
message: prompt
? `User rejected the pending tool execution and sent a new instruction instead: ${prompt}`
: "User rejected executing this tool",
};
}
function buildHitlResumeValue(input: {
decision: "approve" | "reject";
count: number;
prompt?: string;
}) {
return {
decisions: Array.from({ length: input.count }, () => (
buildHitlDecision(input.decision, input.prompt)
)),
};
}
/**
* Build the provider-agnostic resume payload for a human-in-the-loop turn.
* Exported for unit testing; the LLM port wraps it into a LangGraph Command.
*/
export function buildResumeValue(input: {
decision: "approve" | "reject";
interrupts?: PendingInterrupt[];
prompt?: string;
}) {
const interrupts = input.interrupts ?? [];
if (interrupts.length === 0) {
throw new Error("No pending approval interrupt found for resume.");
}
if (interrupts.length === 1) {
return buildHitlResumeValue({
decision: input.decision,
count: interrupts[0].count,
prompt: input.prompt,
});
}
return Object.fromEntries(
interrupts.map((interrupt) => [
interrupt.id,
buildHitlResumeValue({
decision: input.decision,
count: interrupt.count,
prompt: input.prompt,
}),
]),
);
}
export type RunTurnUseCaseDeps = {
llm: LlmPort;
sessions: AgentSessionStore;
/** Per-session buffer of mid-turn steering instructions; cleared when a turn ends unconsumed. */
steerBuffer: SteerBuffer;
modes: PluginOptions["modes"];
getAdminforth: () => IAdminForth;
getAgentSystemPrompt: () => Promise<string>;
/** True when a persistent checkpointer is configured (not the in-memory MemorySaver). */
hasPersistentCheckpointer: boolean;
/**
* True when `turnResource.checkpointIdField` is configured — the tip checkpoint id
* is then recorded per turn, enabling message editing / branching.
*/
turnCheckpointsEnabled: boolean;
/** Resource config used for the edit ownership check + turn lookups. */
sessionResource: PluginOptions["sessionResource"];
/** Factory for the per-turn debug sink; the concrete impl lives in the llm layer. */
createDebugSink: () => DebugSink;
};
/**
* Single entry point for running an agent turn: prepare (start/resume) → build
* the prompt → stream from the LLM port → consume the typed stream into SSE
* events → persist the result. Replaces the former AgentTurnService plus the
* TurnLifecycle/Context/Prompt/Persistence/StreamConsumer + ModeResolver stack.
*/
export class RunTurnUseCase {
private readonly pendingInterrupts = new Map<string, PendingInterrupt[]>();
private readonly lastDetectedLanguage = new Map<string, DetectedLanguage | null>();
constructor(private readonly deps: RunTurnUseCaseDeps) {}
private resolveMode(modeName?: string | null): AgentMode {
return this.deps.modes.find((mode) => mode.name === modeName) ?? this.deps.modes[0];
}
private buildContext(input: BaseAgentTurnInput, turnId: string): AgentTurnContext {
return {
adminUser: input.adminUser,
sessionId: input.sessionId,
turnId,
abortSignal: input.abortSignal,
currentPage: input.currentPage,
chatSurface: input.chatSurface,
userTimeZone: input.userTimeZone ?? "UTC",
adminPublicOrigin:
input.adminPublicOrigin ?? this.deps.getAdminforth().config.baseUrlSlashed,
};
}
/**
* Resolve the pending HITL interrupts for a resume. With a persistent checkpointer the
* checkpoint is authoritative (correct across instances and restarts); with the in-memory
* MemorySaver the in-process cache is authoritative (single process, cannot be stale).
*/
private async resolvePendingInterrupts(sessionId: string, mode: AgentMode): Promise<PendingInterrupt[]> {
if (this.deps.hasPersistentCheckpointer) {
// The in-process cache can be stale after a cross-instance resume, so it is intentionally
// NOT consulted here. A failure to read the checkpoint is surfaced as a real error — never
// masked as "no pending approval".
try {
return await this.deps.llm.getPendingInterrupts({
completionAdapter: mode.completionAdapter,
sessionId,
});
} catch (error) {
logger.error(
`Failed to read pending approval state from checkpoint for session "${sessionId}": ${getErrorMessage(error)}`,
);
throw error;
}
}
return this.pendingInterrupts.get(sessionId) ?? [];
}
private requiresSessionOwnership(input: BaseAgentTurnInput) {
return !input.chatSurface;
}
/** Verify the session exists and belongs to the requesting admin user. */
public async assertSessionOwnership(sessionId: string, adminUser: BaseAgentTurnInput["adminUser"]) {
const s = this.deps.sessionResource;
const session = await this.deps.getAdminforth().resource(s.resourceId).get(
[Filters.EQ(s.idField, sessionId)],
);
if (!session) {
throw new Error(`Session "${sessionId}" not found.`);
}
if (session[s.askerIdField] !== adminUser.pk) {
throw new Error(`Session "${sessionId}" does not belong to the current user.`);
}
}
/**
* Prepare an edit/branch turn: resolve the fork checkpoint from the previous turn,
* truncate the conversation after the edited turn, and reuse the edited turn's id
* for the regenerated response. Ownership is already asserted by `prepareTurn`.
*/
private async prepareEditTurn(input: RunAndPersistAgentResponseInput): Promise<PreparedTurn> {
const sequenceDebugSink = this.deps.createDebugSink();
const mode = this.resolveMode(input.modeName);
const editTurnId = input.editTurnId!;
if (!this.deps.turnCheckpointsEnabled) {
throw new Error("Edit/fork requires checkpointIdField to be configured on turnResource.");
}
const agentTurns = await this.deps.sessions.getAgentTurns(input.sessionId);
const targetIndex = agentTurns.findIndex((turn) => turn.id === editTurnId);
if (targetIndex === -1) {
throw new Error(`Turn "${editTurnId}" not found in session "${input.sessionId}".`);
}
const previous = agentTurns[targetIndex - 1];
let branchFromCheckpointId: string | undefined;
if (previous) {
if (!previous.checkpointId) {
throw new Error(
`Cannot edit this message: the previous turn has no stored checkpoint to branch from.`,
);
}
branchFromCheckpointId = previous.checkpointId;
} else {
// No earlier turn to fork from — drop the whole thread and start fresh.
await this.deps.llm.resetThreadCheckpoints({
completionAdapter: mode.completionAdapter,
sessionId: input.sessionId,
});
}
// Language-detection context: the kept user messages just before the edited turn.
const previousUserMessages: PreviousUserMessage[] = agentTurns
.slice(Math.max(0, targetIndex - 2), targetIndex)
.map((turn) => ({ text: turn.prompt }));
// Editing supersedes any in-flight turn state for this session.
this.deps.steerBuffer.clear(input.sessionId);
this.pendingInterrupts.delete(input.sessionId);
this.lastDetectedLanguage.delete(input.sessionId);
// Apply the edit + truncate (deletes later turns first, then rewrites the edited turn).
await this.deps.sessions.editTurnAndTruncateAfter({
sessionId: input.sessionId,
turnId: editTurnId,
newPrompt: input.prompt,
});
await this.deps.sessions.touchSession(input.sessionId);
return {
prompt: input.prompt,
sessionId: input.sessionId,
turnId: editTurnId,
previousUserMessages,
mode,
context: this.buildContext(input, editTurnId),
observability: { emit: input.emit, sequenceDebugSink },
branchFromCheckpointId,
};
}
private async prepareTurn(input: RunAndPersistAgentResponseInput): Promise<PreparedTurn> {
const sequenceDebugSink = this.deps.createDebugSink();
const mode = this.resolveMode(input.modeName);
const approvalDecision = getApprovalDecision(input);
const shouldResume = Boolean(approvalDecision);
if (this.requiresSessionOwnership(input)) {
await this.assertSessionOwnership(input.sessionId, input.adminUser);
}
if (input.editTurnId && !shouldResume) {
return this.prepareEditTurn(input);
}
let turnId: string;
let previousUserMessages: PreviousUserMessage[] = [];
let initialResponse: string | undefined;
let resumeInterrupts: PendingInterrupt[] | undefined;
if (shouldResume) {
resumeInterrupts = await this.resolvePendingInterrupts(input.sessionId, mode);
if (resumeInterrupts.length === 0) {
throw new Error(`No pending approval interrupt found for session "${input.sessionId}".`);
}
const resumeState = await this.deps.sessions.getResumeState(input.sessionId);
turnId = resumeState.turnId;
initialResponse = resumeState.initialResponse;
} else {
previousUserMessages = await this.deps.sessions.getPreviousUserMessages(input.sessionId);
turnId = await this.deps.sessions.createNewTurn(input.sessionId, input.prompt);
await this.deps.sessions.touchSession(input.sessionId);
}
return {
prompt: input.prompt,
sessionId: input.sessionId,
turnId,
previousUserMessages,
mode,
context: this.buildContext(input, turnId),
observability: {
emit: input.emit,
sequenceDebugSink,
},
resume: shouldResume
? { decision: approvalDecision!, interrupts: resumeInterrupts }
: undefined,
initialResponse,
};
}
private async resolveUserLanguage(prepared: PreparedTurn): Promise<DetectedLanguage | null> {
if (prepared.resume) {
return this.lastDetectedLanguage.get(prepared.sessionId) ?? null;
}
const detected = await this.deps.llm
.detectLanguage({
completionAdapter: prepared.mode.completionAdapter,
prompt: prepared.prompt,
previousUserMessages: prepared.previousUserMessages,
})
.catch((error) => {
if (prepared.context.abortSignal?.aborted || isAbortError(error)) {
throw error;
}
logger.warn(`Failed to detect user language: ${getErrorMessage(error)}`);
return null;
});
this.lastDetectedLanguage.set(prepared.sessionId, detected);
return detected;
}
private buildMessages(prepared: PreparedTurn): AgentMessage[] {
return [{ role: "user", content: prepared.prompt }];
}
private async handleInterrupt(
prepared: PreparedTurn,
interrupt: unknown,
descriptors: PendingInterrupt[],
) {
if (!this.deps.hasPersistentCheckpointer) {
const existing = this.pendingInterrupts.get(prepared.sessionId) ?? [];
const merged = new Map(existing.map((item) => [item.id, item.count]));
for (const item of descriptors) {
merged.set(item.id, item.count);
}
this.pendingInterrupts.set(
prepared.sessionId,
[...merged.entries()].map(([id, count]) => ({ id, count })),
);
}
await prepared.observability.emit?.({
type: "interrupt",
sessionId: prepared.sessionId,
interrupt,
});
}
private async runAgentTurn(prepared: PreparedTurn): Promise<{ text: string }> {
const userLanguage = await this.resolveUserLanguage(prepared);
const streamInput = prepared.resume
? {
resume: buildResumeValue({
decision: prepared.resume.decision,
interrupts: prepared.resume.interrupts,
prompt: prepared.prompt,
}),
}
: { messages: this.buildMessages(prepared) };
const stream = await this.deps.llm.streamTurn({
completionAdapter: prepared.mode.completionAdapter,
systemPrompt: await this.deps.getAgentSystemPrompt(),
input: streamInput,
context: { ...prepared.context, userLanguage },
observability: prepared.observability,
branchFromCheckpointId: prepared.branchFromCheckpointId,
});
const { emit } = prepared.observability;
const abortSignal = prepared.context.abortSignal;
const textBuffer = new VegaLiteStreamBuffer();
let fullResponse = "";
let interrupted = false;
try {
for await (const chunk of stream) {
if (abortSignal?.aborted) {
throw new DOMException("This operation was aborted", "AbortError");
}
if (chunk.kind === "interrupt") {
interrupted = true;
await this.handleInterrupt(prepared, chunk.interrupt, chunk.descriptors);
continue;
}
if (chunk.kind === "reasoning") {
if (chunk.delta) {
await emit?.({ type: "reasoning-delta", delta: chunk.delta });
}
continue;
}
if (chunk.delta) {
fullResponse += chunk.delta;
await textBuffer.push(chunk.delta, emit);
}
}
await textBuffer.flush(emit);
return { text: fullResponse };
} finally {
// When the turn is interrupted for HITL approval it will resume, so any steers
// buffered meanwhile must survive to be applied on resume. Otherwise the turn is
// done and leftover (unconsumed) steers must not leak into the next turn.
if (!interrupted) {
this.deps.steerBuffer.clear(prepared.sessionId);
// The detected language is only needed to bridge an interrupt -> resume; once
// the turn is done the next one re-detects.
this.lastDetectedLanguage.delete(prepared.sessionId);
}
if (!this.deps.hasPersistentCheckpointer && !interrupted) {
this.pendingInterrupts.delete(prepared.sessionId);
}
}
}
async runAndPersistAgentResponse(
input: RunAndPersistAgentResponseInput,
): Promise<RunAndPersistAgentResponseResult> {
const prepared = await this.prepareTurn(input);
await prepared.observability.emit?.({
type: "turn-persisted",
sessionId: prepared.sessionId,
turnId: prepared.turnId,
});
let fullResponse = prepared.initialResponse ?? "";
let aborted = false;
let failed = false;
try {
const agentResponse = await this.runAgentTurn(prepared);
fullResponse += agentResponse.text;
} catch (error) {
if (input.abortSignal?.aborted || isAbortError(error)) {
aborted = true;
logger.info(input.abortLogMessage);
} else {
failed = true;
fullResponse = getErrorMessage(error);
logger.error(`${input.failureLogMessage}:\n${fullResponse}`);
}
}
// Record the turn's tip checkpoint id ONLY on success, so message editing can
// fork from it. On abort/failure leave the previous value untouched (undefined =
// don't overwrite). Never let a checkpoint read break response persistence.
let checkpointId: string | null | undefined;
if (!aborted && !failed && this.deps.turnCheckpointsEnabled) {
try {
checkpointId = await this.deps.llm.getLatestCheckpointId({
completionAdapter: prepared.mode.completionAdapter,
sessionId: prepared.sessionId,
});
} catch (error) {
logger.warn(`Failed to read latest checkpoint id for turn "${prepared.turnId}": ${getErrorMessage(error)}`);
checkpointId = undefined;
}
}
prepared.observability.sequenceDebugSink.flush();
await this.deps.sessions.saveTurnResponse({
turnId: prepared.turnId,
responseText: fullResponse,
debugHistory: prepared.observability.sequenceDebugSink.getHistory(),
checkpointId,
});
return {
text: fullResponse,
turnId: prepared.turnId,
aborted,
failed,
};
}
async handleTurn(input: HandleTurnInput) {
await input.emit({
type: "turn-started",
messageId: randomUUID(),
});
const agentResponse = await this.runAndPersistAgentResponse({
prompt: input.prompt,
sessionId: input.sessionId,
modeName: input.modeName,
userTimeZone: input.userTimeZone,
currentPage: input.currentPage,
chatSurface: input.chatSurface,
adminPublicOrigin: input.adminPublicOrigin,
approvalDecision: input.approvalDecision,
abortSignal: input.abortSignal,
adminUser: input.adminUser,
emit: input.emit,
failureLogMessage: input.failureLogMessage ?? "Agent response failed",
abortLogMessage: input.abortLogMessage ?? "Agent response aborted",
});
if (agentResponse.failed) {
await input.emit({
type: "error",
error: agentResponse.text,
});
} else if (!agentResponse.aborted) {
await input.emit({
type: "response",
text: agentResponse.text,
sessionId: input.sessionId,
turnId: agentResponse.turnId,
});
}
await input.emit({
type: "finish",
});
return agentResponse;
}
async handleEditTurn(input: HandleEditTurnInput) {
await input.emit({
type: "turn-started",
messageId: randomUUID(),
});
const agentResponse = await this.runAndPersistAgentResponse({
prompt: input.prompt,
sessionId: input.sessionId,
modeName: input.modeName,
userTimeZone: input.userTimeZone,
currentPage: input.currentPage,
chatSurface: input.chatSurface,
adminPublicOrigin: input.adminPublicOrigin,
editTurnId: input.turnId,
abortSignal: input.abortSignal,
adminUser: input.adminUser,
emit: input.emit,
failureLogMessage: input.failureLogMessage ?? "Agent message edit failed",
abortLogMessage: input.abortLogMessage ?? "Agent message edit aborted",
});
if (agentResponse.failed) {
await input.emit({
type: "error",
error: agentResponse.text,
});
} else if (!agentResponse.aborted) {
await input.emit({
type: "response",
text: agentResponse.text,
sessionId: input.sessionId,
turnId: agentResponse.turnId,
});
}
await input.emit({
type: "finish",
});
return agentResponse;
}
}