847
/** Builds the compacted, model-facing session-context payload from a snapshot. */
848
export function serializeSessionContext(session: URI, chatId: string | undefined, snapshot: IChatContextSnapshot, detail: SessionContextDetail, transcriptLimit: number): string {
850
>
let truncated = false;
851
>
const trunc = (text: string, max: number): string | undefined => {
852
>
if (max <= 0 || !text) {
853
return undefined;
854
}
856
>
truncated = truncated || result.truncated;
857
>
return result.text || undefined;
858
>
};
859
>
860
>
const entries: { message: Message; parts: readonly ResponsePart[]; state: TurnState | 'inProgress' }[] =
861
>
snapshot.turns.map(t => ({ message: t.message, parts: t.responseParts, state: t.state }));
862
>
if (snapshot.activeTurn) {
863
entries.push({ message: snapshot.activeTurn.message, parts: snapshot.activeTurn.responseParts, state: 'inProgress' });
864
}
866
truncated = true;
867
}
869
>
const windowed = entries.slice(windowStart);
870
>
871
>
const transcript: ISerializedContextTurn[] = windowed.map((entry, index): ISerializedContextTurn => {
872
>
const user = trunc(entry.message.text, caps.user);
873
>
const assistant = trunc(assistantTextOf(entry.parts), caps.assistant);
874
>
const toolCalls = toolCallsOf(entry.parts);
875
>
let serializedToolCalls: (string | { name: string; input?: string })[] | undefined;
876
>
if (detail !== 'summary' && toolCalls.length > 0) {
877
serializedToolCalls = toolCalls.map(tc => {
878
if (caps.toolInput > 0) {