-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathcontroller.ts
More file actions
567 lines (524 loc) · 19.5 KB
/
Copy pathcontroller.ts
File metadata and controls
567 lines (524 loc) · 19.5 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
import type { Context, UserMessage } from "@earendil-works/pi-ai";
import type { StreamDebugLogger } from "../../debug/agentDebug";
import type { ProviderId } from "../../settings";
import { type ConversationViewState, getActiveSegment } from "../conversation/conversationState";
import type { TurnCancellation } from "../conversation/turnCancellation";
import type { PendingUploadedFile } from "../messages/uploadedFiles";
import { isAbortLikeError } from "../page/chatPageHelpers";
import { createSyntheticContinueUserMessage, runCompaction } from "./engine";
import {
createCompactionPressure,
decideCompaction,
normalizeCompactionPressure,
notePressureAfterCompaction,
resolvePruneOptions,
shouldPruneBeforeCompaction,
} from "./policy";
import { type PruneConversationResult, pruneConversationState } from "./prune";
import {
buildCompactionRunningStatus,
buildPruneFallbackStatus,
PRUNE_FALLBACK_NOTICE,
} from "./statusText";
import { type CompleteAssistantFn, createCompactionAbortError } from "./summarizer";
import { TokenLedger } from "./tokenLedger";
import type {
CompactionDecision,
CompactionIntent,
CompactionStatus,
CompactionTrigger,
ProviderRuntimeConfig,
} from "./types";
type ContextBuildOptions = {
includeAbortedMessages?: boolean;
includeUploadedFilesMetadata?: boolean;
};
// 所有副作用经由注入的 sinks:ChatPage 提供完整实现,子代理提供轻量子集。
// 全部可选——缺省即 no-op,controller 自身保持纯净可测。
export type CompactionSinks = {
applyState?: (state: ConversationViewState) => void;
// 运行中换底:apply + 清空 live transcript(压缩/prune 结果落地后旧流式内容已过期)。
applyStateMidRun?: (state: ConversationViewState) => void;
publishStatus?: (status: CompactionStatus) => void;
setBridgeToolStatus?: (status: string | null, isCompaction?: boolean) => void;
queueCheckpoint?: (state: ConversationViewState) => void;
persist?: (state: ConversationViewState) => Promise<unknown>;
restoreComposer?: (
composerText: string | undefined,
uploadedFiles: PendingUploadedFile[],
) => void;
persistRollback?: (state: ConversationViewState) => Promise<unknown>;
};
export type CompactionPreSendBinding = {
// 待 checkpoint 的基线状态(不含本轮待发送的用户消息)。
baseState: ConversationViewState;
pendingUserText: string;
composerText?: string;
uploadedFiles?: PendingUploadedFile[];
// 压缩/prune 后如何得到要 apply 的最终状态(如重新附加待发送的用户消息)。
composeAppliedState: (state: ConversationViewState) => ConversationViewState;
};
export type CompactionTurnBinding = {
providerId: ProviderId;
model: string;
runtime: ProviderRuntimeConfig;
cancellation: TurnCancellation;
debugLogger?: StreamDebugLogger;
complete?: CompleteAssistantFn;
sinks: CompactionSinks;
buildPreparedContext: (
state: ConversationViewState,
tools?: Context["tools"],
options?: ContextBuildOptions,
) => Context;
buildResumeContext: (
state: ConversationViewState,
resumeMessage?: UserMessage,
tools?: Context["tools"],
options?: ContextBuildOptions,
) => Context;
presend?: CompactionPreSendBinding;
};
export type CompactionDuringRunResult = {
context: Context | null;
shouldDisableProtection: boolean;
};
type RollbackSnapshot = {
state: ConversationViewState;
composerText?: string;
uploadedFiles?: PendingUploadedFile[];
persistOnRollback?: boolean;
};
/**
* 每会话压缩状态机。跨轮持有压力阶梯与 token 账本;每轮 bindTurn 注入
* 运行时/sinks/取消链。单飞由 inFlight 保证;回滚快照是实例字段,所有
* 终态都经 settle*() 收敛(状态发布与 bridge 状态清理成对,不再散落)。
*/
export class CompactionController {
private pressure = createCompactionPressure();
private readonly ledger = new TokenLedger();
private binding: CompactionTurnBinding | null = null;
private rollbackSnapshot: RollbackSnapshot | null = null;
private inFlight = false;
private statusPhase: CompactionStatus["phase"] = "idle";
private turnMeta = { activeMessageCount: 0, userMessageCount: 0, lastSummaryAt: 0 };
bindTurn(binding: CompactionTurnBinding) {
this.binding = binding;
this.rollbackSnapshot = null;
this.inFlight = false;
}
unbindTurn() {
this.binding = null;
this.rollbackSnapshot = null;
this.inFlight = false;
}
get stats() {
return { compactionsApplied: this.pressure.compactionsApplied };
}
beginRequest(context: Context, state: ConversationViewState) {
this.ledger.rebase(context);
this.updateTurnMeta(state);
}
// O(1):账本读数 + 流式增量估算 + 纯决策,无状态构建、无序列化。
// pendingTokenUnits 由调用方按流式 delta 用 estimateTextTokenUnits 累加。
shouldProtectMidStream(pendingTokenUnits: number): boolean {
if (!this.binding || this.inFlight) return false;
return this.decide("protection", this.ledger.totalWithPendingTokens(pendingTokenUnits))
.shouldCompact;
}
async maybeCompactPreSend(params: {
budgetContext: Context;
tools?: Context["tools"];
includeUploadedFilesMetadata?: boolean;
}): Promise<boolean> {
const binding = this.binding;
const presend = binding?.presend;
if (!binding || !presend) return false;
if (binding.cancellation.userStop.signal.aborted) {
throw createCompactionAbortError();
}
const now = Date.now();
const buildOptions: ContextBuildOptions = {
includeUploadedFilesMetadata: params.includeUploadedFilesMetadata,
};
let workingState = presend.baseState;
let pruned: PruneConversationResult | null = null;
if (shouldPruneBeforeCompaction(this.pressure, now)) {
const attempt = pruneConversationState(workingState, resolvePruneOptions(this.pressure));
if (attempt.applied) {
pruned = attempt;
workingState = attempt.state;
}
}
const budgetContext = pruned
? binding.buildPreparedContext(workingState, params.tools, buildOptions)
: params.budgetContext;
this.ledger.rebase(budgetContext);
this.updateTurnMeta(workingState);
const decision = this.decide("optimization", this.ledger.total(), now);
this.logDecision(decision);
if (!decision.shouldCompact) {
if (pruned) {
binding.sinks.applyState?.(presend.composeAppliedState(pruned.state));
return true;
}
return false;
}
this.rollbackSnapshot = {
state: presend.baseState,
composerText: presend.composerText,
uploadedFiles: presend.uploadedFiles,
};
this.inFlight = true;
this.publishRunning("pre-send", workingState.meta.activeSegmentIndex, decision);
const scope = binding.cancellation.deriveScope();
try {
const outcome = await runCompaction({
state: workingState,
incomingUserText: presend.pendingUserText,
intent: "optimization",
contextTokens: decision.totalTokens,
threshold: decision.threshold,
providerId: binding.providerId,
model: binding.model,
runtime: binding.runtime,
signal: scope.controller.signal,
debugLogger: binding.debugLogger,
complete: binding.complete,
});
await binding.sinks.persist?.(outcome.state);
this.rollbackSnapshot = null;
const appliedState = presend.composeAppliedState(outcome.state);
binding.sinks.applyState?.(appliedState);
this.settleCompleted("pre-send", outcome.newSegmentIndex);
binding.sinks.queueCheckpoint?.(outcome.state);
this.notePostCompactionPressure(
binding.buildPreparedContext(appliedState, params.tools, buildOptions),
appliedState,
decision.threshold,
);
return true;
} catch (error) {
if (this.isAbortOutcome(scope.controller.signal, error)) {
throw error;
}
this.rollbackSnapshot = null;
const fallback =
pruned ?? pruneConversationState(presend.baseState, resolvePruneOptions(this.pressure));
if (fallback.applied) {
binding.sinks.applyState?.(presend.composeAppliedState(fallback.state));
this.settleFailed("pre-send", PRUNE_FALLBACK_NOTICE);
binding.sinks.setBridgeToolStatus?.(buildPruneFallbackStatus(fallback.prunedMessageCount));
return true;
}
console.warn("发送前上下文压缩失败,继续使用原始上下文", error);
this.settleFailed("pre-send", error instanceof Error ? error.message : String(error));
return false;
} finally {
scope.release();
this.inFlight = false;
this.binding?.sinks.setBridgeToolStatus?.(null);
}
}
async compactDuringRun(params: {
trigger: Exclude<CompactionTrigger, "pre-send">;
state: ConversationViewState;
force?: boolean;
budgetContext?: Context;
tools?: Context["tools"];
includeAbortedMessages?: boolean;
includeUploadedFilesMetadata?: boolean;
}): Promise<CompactionDuringRunResult> {
const binding = this.binding;
if (!binding) {
return { context: null, shouldDisableProtection: false };
}
// 覆盖"mid-stream abort 后、summarizer 启动前"用户恰好点停止的间隙。
if (binding.cancellation.userStop.signal.aborted) {
throw createCompactionAbortError();
}
const now = Date.now();
const buildOptions: ContextBuildOptions = {
includeAbortedMessages: params.includeAbortedMessages,
includeUploadedFilesMetadata: params.includeUploadedFilesMetadata,
};
const buildFallbackContext = (state: ConversationViewState): Context => {
if (params.trigger !== "mid-stream") {
return binding.buildPreparedContext(state, params.tools, buildOptions);
}
const messages = getActiveSegment(state)?.messages ?? [];
const lastTimestamp = messages[messages.length - 1]?.timestamp;
const resumeMessage = createSyntheticContinueUserMessage(
typeof lastTimestamp === "number" ? lastTimestamp + 1 : now,
);
return binding.buildResumeContext(state, resumeMessage, params.tools, {
includeUploadedFilesMetadata: params.includeUploadedFilesMetadata,
});
};
let workingState = params.state;
let pruned: PruneConversationResult | null = null;
if (shouldPruneBeforeCompaction(this.pressure, now)) {
const attempt = pruneConversationState(workingState, resolvePruneOptions(this.pressure));
if (attempt.applied) {
pruned = attempt;
workingState = attempt.state;
}
}
const budgetContext =
!pruned && params.budgetContext
? params.budgetContext
: binding.buildPreparedContext(workingState, params.tools, buildOptions);
this.ledger.rebase(budgetContext);
this.updateTurnMeta(workingState);
const decision = this.decide(
params.force ? "optimization" : "protection",
this.ledger.total(),
now,
);
this.logDecision(decision);
if (!params.force && !decision.shouldCompact) {
if (pruned) {
binding.sinks.applyStateMidRun?.(pruned.state);
return {
context: buildFallbackContext(pruned.state),
shouldDisableProtection: false,
};
}
return params.trigger === "mid-stream"
? {
context: buildFallbackContext(workingState),
shouldDisableProtection: true,
}
: { context: null, shouldDisableProtection: false };
}
this.rollbackSnapshot = { state: params.state, persistOnRollback: true };
this.inFlight = true;
this.publishRunning(params.trigger, workingState.meta.activeSegmentIndex, decision);
const scope = binding.cancellation.deriveScope();
try {
const outcome = await runCompaction({
state: workingState,
intent: "protection",
contextTokens: decision.totalTokens,
threshold: decision.threshold,
providerId: binding.providerId,
model: binding.model,
runtime: binding.runtime,
signal: scope.controller.signal,
debugLogger: binding.debugLogger,
complete: binding.complete,
});
await binding.sinks.persist?.(outcome.state);
this.rollbackSnapshot = null;
binding.sinks.applyStateMidRun?.(outcome.state);
this.settleCompleted(params.trigger, outcome.newSegmentIndex);
binding.sinks.queueCheckpoint?.(outcome.state);
const resumeMessage = createSyntheticContinueUserMessage(
(outcome.checkpointMessage.timestamp ?? now) + 1,
);
const resumeContext = binding.buildResumeContext(outcome.state, resumeMessage, params.tools, {
includeUploadedFilesMetadata: params.includeUploadedFilesMetadata,
});
this.notePostCompactionPressure(resumeContext, outcome.state, decision.threshold);
return { context: resumeContext, shouldDisableProtection: false };
} catch (error) {
if (this.isAbortOutcome(scope.controller.signal, error)) {
throw error;
}
this.rollbackSnapshot = null;
const fallback =
pruned ?? pruneConversationState(workingState, resolvePruneOptions(this.pressure));
if (fallback.applied) {
binding.sinks.applyStateMidRun?.(fallback.state);
this.settleFailed(params.trigger, PRUNE_FALLBACK_NOTICE);
binding.sinks.setBridgeToolStatus?.(buildPruneFallbackStatus(fallback.prunedMessageCount));
return {
context: buildFallbackContext(fallback.state),
shouldDisableProtection: false,
};
}
this.settleFailed(
params.trigger,
(error instanceof Error ? error.message : String(error)) || "压缩失败",
);
return params.trigger === "mid-stream"
? {
context: buildFallbackContext(workingState),
shouldDisableProtection: true,
}
: { context: null, shouldDisableProtection: false };
} finally {
scope.release();
this.inFlight = false;
this.binding?.sinks.setBridgeToolStatus?.(null);
}
}
// 用户中止后的统一善后:有快照则回滚(恢复状态/输入框/可选持久化)并返回 true。
/**
* 用户手动触发的上下文压缩(点击输入区上下文用量环 → 确认):临时绑定一轮并复用主压缩流程,
* 强制跳过阈值判断。仅限空闲时调用;若有进行中的回合或绑定则直接返回 false。
*/
async compactNow(params: {
state: ConversationViewState;
providerId: ProviderId;
model: string;
runtime: ProviderRuntimeConfig;
cancellation: TurnCancellation;
debugLogger?: StreamDebugLogger;
sinks: CompactionSinks;
buildPreparedContext: CompactionTurnBinding["buildPreparedContext"];
buildResumeContext: CompactionTurnBinding["buildResumeContext"];
}): Promise<boolean> {
if (this.binding || this.inFlight) return false;
this.bindTurn({ ...params });
try {
const result = await this.compactDuringRun({
trigger: "manual",
state: params.state,
force: true,
});
return result.context !== null;
} finally {
this.unbindTurn();
}
}
async handleTurnAbort(): Promise<boolean> {
const binding = this.binding;
const snapshot = this.rollbackSnapshot;
this.rollbackSnapshot = null;
this.inFlight = false;
if (!binding) return false;
if (!snapshot) {
if (this.statusPhase === "running") {
this.publishStatus({ phase: "idle" });
}
return false;
}
binding.sinks.applyStateMidRun?.(snapshot.state);
binding.sinks.setBridgeToolStatus?.(null, false);
this.publishStatus({ phase: "idle" });
binding.sinks.restoreComposer?.(snapshot.composerText, snapshot.uploadedFiles ?? []);
if (snapshot.persistOnRollback) {
await binding.sinks.persistRollback?.(snapshot.state);
}
return true;
}
private updateTurnMeta(state: ConversationViewState) {
const segment = getActiveSegment(state);
const messages = segment?.messages ?? [];
let userMessageCount = 0;
for (const message of messages) {
if (message.role === "user") userMessageCount += 1;
}
this.turnMeta = {
activeMessageCount: messages.length,
userMessageCount,
lastSummaryAt: segment?.summary?.timestamp ?? 0,
};
}
private decide(intent: CompactionIntent, totalTokens: number, now = Date.now()) {
const binding = this.binding;
if (!binding) {
throw new Error("compaction decision requested without an active turn binding");
}
this.pressure = normalizeCompactionPressure(this.pressure, now);
return decideCompaction({
providerId: binding.providerId,
intent,
totalTokens,
modelConfig: binding.runtime.modelConfig,
activeMessageCount: this.turnMeta.activeMessageCount,
userMessageCount: this.turnMeta.userMessageCount,
lastCompactionAt: Math.max(this.turnMeta.lastSummaryAt, this.pressure.lastCompactionAt),
pressure: this.pressure,
inFlight: this.inFlight,
now,
});
}
private notePostCompactionPressure(
contextAfter: Context,
stateAfter: ConversationViewState,
threshold: number,
) {
this.ledger.rebase(contextAfter);
this.updateTurnMeta(stateAfter);
this.pressure = notePressureAfterCompaction(this.pressure, {
totalTokensAfter: this.ledger.total(),
threshold,
now: Date.now(),
});
}
private isAbortOutcome(scopeSignal: AbortSignal, error: unknown) {
return (
this.binding?.cancellation.userStop.signal.aborted ||
scopeSignal.aborted ||
isAbortLikeError(error)
);
}
private publishStatus(status: CompactionStatus) {
this.statusPhase = status.phase;
this.binding?.sinks.publishStatus?.(status);
}
private publishRunning(
trigger: CompactionTrigger,
sourceSegmentIndex: number,
decision: CompactionDecision,
) {
this.publishStatus({
phase: "running",
trigger,
startedAt: Date.now(),
sourceSegmentIndex,
});
this.binding?.sinks.setBridgeToolStatus?.(
buildCompactionRunningStatus(decision, this.pressure),
true,
);
}
private settleCompleted(trigger: CompactionTrigger, newSegmentIndex: number) {
this.publishStatus({
phase: "completed",
trigger,
newSegmentIndex,
completedAt: Date.now(),
});
}
private settleFailed(trigger: CompactionTrigger, message: string) {
this.publishStatus({ phase: "failed", trigger, failedAt: Date.now(), message });
}
private logDecision(decision: CompactionDecision) {
this.binding?.debugLogger?.logResult({
event: "compaction_decision",
intent: decision.intent,
reason: decision.reason,
shouldCompact: decision.shouldCompact,
totalTokens: decision.totalTokens,
threshold: decision.threshold,
thresholdMode: decision.thresholdMode,
contextWindow: decision.contextWindow,
maxOutputToken: decision.maxOutputToken,
pressure: this.pressure,
ledger: this.ledger.snapshot(),
});
}
}
export type CompactionControllerRegistry = {
get: (conversationId: string) => CompactionController;
dispose: (conversationId: string) => void;
};
export function createCompactionControllerRegistry(): CompactionControllerRegistry {
const controllers = new Map<string, CompactionController>();
return {
get(conversationId: string) {
const key = conversationId.trim();
const existing = controllers.get(key);
if (existing) return existing;
const created = new CompactionController();
controllers.set(key, created);
return created;
},
dispose(conversationId: string) {
controllers.delete(conversationId.trim());
},
};
}