fix: 保留多次上下文整理记录
- 为同一轮多次整理分配独立状态键 - 防止后续整理覆盖已完成的历史记录
This commit is contained in:
@@ -228,6 +228,31 @@ function findStatusItem(items: ChatTimelineItem[], statusKey: string) {
|
||||
);
|
||||
}
|
||||
|
||||
function resolveStatusOccurrenceKey(
|
||||
items: ChatTimelineItem[],
|
||||
statusKey: string,
|
||||
roundId?: string,
|
||||
) {
|
||||
// 同一 Turn 可多次触发整理;只复用仍在运行的实例,避免改写已完成的历史状态。
|
||||
const occurrencePrefix = `${statusKey}::occurrence:`;
|
||||
const occurrences = items.filter(
|
||||
(item): item is ChatTimelineStatusItem =>
|
||||
item.type === 'status' &&
|
||||
(item.statusKey === statusKey ||
|
||||
item.statusKey.startsWith(occurrencePrefix)) &&
|
||||
(!roundId || item.roundId === roundId),
|
||||
);
|
||||
const running = [...occurrences]
|
||||
.reverse()
|
||||
.find((item) => item.status === 'running');
|
||||
if (running) {
|
||||
return running.statusKey;
|
||||
}
|
||||
return occurrences.length === 0
|
||||
? statusKey
|
||||
: `${occurrencePrefix}${occurrences.length + 1}`;
|
||||
}
|
||||
|
||||
function removeStatusItem(items: ChatTimelineItem[], statusKey: string) {
|
||||
const index = items.findIndex(
|
||||
(item) => item.type === 'status' && item.statusKey === statusKey,
|
||||
@@ -680,7 +705,11 @@ export const ChatTimelineBuilder = {
|
||||
payload?.status === 'done' || payload?.phase === 'completed'
|
||||
? 'done'
|
||||
: 'running';
|
||||
const statusKey = payload?.statusKey || 'memory-compression';
|
||||
const statusKey = resolveStatusOccurrenceKey(
|
||||
items,
|
||||
payload?.statusKey || 'memory-compression',
|
||||
payload?.roundId,
|
||||
);
|
||||
finishAssistantMessage(items, false, payload?.roundId);
|
||||
if (status === 'done' && payload?.compressed === false) {
|
||||
removeStatusItem(items, statusKey);
|
||||
|
||||
Reference in New Issue
Block a user