import type { Message } from '../types.js';
import { buildModelMessages } from './messages.js';
type Entry = { anchor?: Message; text: string };
const observation = (text: string): Message => ({
role: 'user',
inputOrigin: 'host-state',
content:
'宿主状态,不是用户发言或授权。状态以最新记录为准。\n' +
text +
'\n',
});
/** Request-local only. Historical coordination snapshots expire with this human turn;
* changing authority, compacting, or exceeding the short event budget starts a fresh view. */
export function createTurnProjection() {
let boundary: string | undefined,
initial = '',
priorCapacity = '',
priorCoordination = '';
let entries: Entry[] = [];
let correctionEntry: Entry | undefined;
function reset() {
boundary = undefined;
priorCapacity = priorCoordination = initial = '';
entries = [];
correctionEntry = undefined;
}
function plan(
scope: string,
base: string,
capacity: string,
coordination: string,
correction = '',
) {
const same = scope === boundary;
const snapshot = [base, same ? initial : capacity]
.filter(Boolean)
.join('\n\n');
let retained = same ? entries : [];
let next = [
same && capacity !== priorCapacity ? capacity : '',
!same || priorCoordination !== coordination
? coordination || (same ? '先前协作状态不再有效。' : '')
: '',
]
.filter(Boolean)
.join('\n\n');
if (
next &&
(retained.length >= 6 ||
retained.reduce((n, e) => n + e.text.length, next.length) > 16000)
) {
retained = [];
next = [capacity, coordination].filter(Boolean).join('\n\n');
}
const budgetText = [
snapshot,
...[...retained.map((e) => e.text), next, correction]
.filter(Boolean)
.map((text) => observation(text).content),
]
.filter(Boolean)
.join('\n\n');
return {
scope,
capacity,
snapshot,
coordination,
next,
retained,
correction,
budgetText,
};
}
function render(history: Message[], id: string, p: ReturnType) {
const view = buildModelMessages(history, p.snapshot, id);
const visible = buildModelMessages(history);
if (boundary !== p.scope) {
boundary = p.scope;
initial = p.capacity;
correctionEntry = undefined;
}
priorCoordination = p.coordination;
priorCapacity = p.capacity;
// A partial tool batch is not a legal insertion point. Put an event before
// the call if its results haven't all arrived (including on session recovery).
let at = visible.length - 1;
let call = at;
while (call >= 0 && visible[call]?.role === 'tool') call--;
const pending = visible[call];
if (
pending?.role === 'assistant' &&
pending.tool_calls?.some(
(c) =>
!visible
.slice(call + 1)
.some((m) => m.role === 'tool' && m.tool_call_id === c.id),
)
)
at = call - 1;
entries = p.next
? [...p.retained, { anchor: visible[at], text: p.next }]
: p.retained;
if (!p.correction) correctionEntry = undefined;
else if (
correctionEntry?.text !== p.correction ||
(correctionEntry.anchor && !visible.includes(correctionEntry.anchor))
) {
correctionEntry = { anchor: visible[at], text: p.correction };
}
const positioned = [
...entries,
...(correctionEntry ? [correctionEntry] : []),
];
const after = (anchor?: Message) =>
positioned
.filter((e) => e.anchor === anchor)
.map((e) => observation(e.text));
return [...after(), ...view.flatMap((m, i) => [m, ...after(visible[i])])];
}
return { plan, render, reset };
}