+82
-63
@@ -24,7 +24,7 @@ const VOICE_COMPACT_PROMPT =
|
||||
'1. Latest user request\n' +
|
||||
'2. Facts from tools\n' +
|
||||
'3. What was already answered\n' +
|
||||
'4. Open follow-ups\n' +
|
||||
'4. Open follow-ups, original goal, user preferences and corrections\n' +
|
||||
'Be specific. Do not greet. Do not say the conversation was compacted.';
|
||||
|
||||
function contentChars(content) {
|
||||
@@ -75,16 +75,19 @@ function estimateTokens(messages, tools) {
|
||||
return conversationTokens(messages) + toolTokens(tools);
|
||||
}
|
||||
|
||||
function historyBudget(ctxSize, tools, attempt) {
|
||||
function historyBudget(ctxSize, tools, attempt, extraTokens = 0) {
|
||||
const cap = ctxSize > 0 ? Number(ctxSize) : 8192;
|
||||
const toolTok = toolTokens(tools);
|
||||
const reserve = Math.max(384, Math.floor(cap * (0.18 + (Number(attempt) || 0) * 0.08)));
|
||||
return Math.max(240, Math.floor(cap * 0.72) - toolTok - reserve);
|
||||
return Math.max(240, Math.floor(cap * 0.72) - toolTok - reserve - extraTokens);
|
||||
}
|
||||
|
||||
function shouldCompact(messages, tools, ctxSize) {
|
||||
if (nonSystemCount(messages) < MIN_COMPACT_MESSAGES) return false;
|
||||
return conversationTokens(messages) > historyBudget(ctxSize, tools, 0);
|
||||
function shouldCompact(messages, tools, ctxSize, extraTokens = 0) {
|
||||
const budget = historyBudget(ctxSize, tools, 0, extraTokens);
|
||||
const tokens = conversationTokens(messages);
|
||||
// Large first requests or tool results can overflow before four messages.
|
||||
// A small greeting must not be discarded merely because schemas are large.
|
||||
return tokens > budget && (nonSystemCount(messages) >= MIN_COMPACT_MESSAGES || conversationTokens((messages || []).filter((m) => m.role !== 'system')) > 1024);
|
||||
}
|
||||
|
||||
function isOverflowError(err) {
|
||||
@@ -129,29 +132,31 @@ function isRealUser(m) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Keep native tool exchanges atomic: results must never outlive their calls.
|
||||
function messageGroups(messages) {
|
||||
const groups = [];
|
||||
for (const message of messages) {
|
||||
const previous = groups[groups.length - 1];
|
||||
if (message.role === 'tool') {
|
||||
const calls = previous && previous[0].tool_calls;
|
||||
if (calls && calls.some((call) => call.id === message.tool_call_id)) previous.push(message);
|
||||
continue;
|
||||
}
|
||||
groups.push([message]);
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
function rebuildHistory(messages, summary) {
|
||||
const list = Array.isArray(messages) ? messages : [];
|
||||
const sys =
|
||||
list[0] && list[0].role === 'system'
|
||||
? { role: 'system', content: list[0].content }
|
||||
: { role: 'system', content: '' };
|
||||
let lastUser = null;
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
if (isRealUser(list[i])) {
|
||||
lastUser = { role: 'user', content: list[i].content };
|
||||
break;
|
||||
}
|
||||
}
|
||||
const tail = [];
|
||||
for (let i = list.length - 1; i >= 1 && tail.length < 6; i--) {
|
||||
const m = list[i];
|
||||
if (!m || m.role === 'system') continue;
|
||||
if (lastUser && m.role === 'user' && m.content === lastUser.content) continue;
|
||||
tail.unshift({ role: m.role, content: m.content, name: m.name, tool_call_id: m.tool_call_id });
|
||||
}
|
||||
const out = [sys, { role: 'user', content: '[conversation summary]\n' + String(summary || '').trim() }];
|
||||
if (lastUser) out.push(lastUser);
|
||||
return out.concat(tail);
|
||||
const systems = list.filter((m) => m.role === 'system');
|
||||
const lastUser = lastRealUserIndex(list);
|
||||
// Older turns belong in the summary, never after the latest request.
|
||||
const tail = lastUser >= 0 ? list.slice(lastUser) : list.filter((m) => m.role !== 'system');
|
||||
return systems.concat(
|
||||
{ role: 'user', content: '[conversation summary]\n' + String(summary || '').trim() },
|
||||
messageGroups(tail.filter((m) => m.role !== 'system')).flat()
|
||||
);
|
||||
}
|
||||
|
||||
function truncateMsg(m, maxChars) {
|
||||
@@ -186,44 +191,55 @@ function overHistoryBudget(keep, budget) {
|
||||
function heuristicCompact(messages, opts) {
|
||||
opts = opts || {};
|
||||
const budget = opts.budgetTokens || 6000;
|
||||
const aggressive = !!opts.aggressive;
|
||||
if (!aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES) {
|
||||
return (messages || []).slice();
|
||||
const list = messages || [];
|
||||
const tokens = conversationTokens(list);
|
||||
if (tokens <= budget || (!opts.aggressive && nonSystemCount(list) < MIN_COMPACT_MESSAGES && conversationTokens(list.filter((m) => m.role !== 'system')) <= 1024))
|
||||
return list.slice();
|
||||
|
||||
const systems = list.filter((m) => m.role === 'system');
|
||||
const lastUser = lastRealUserIndex(list);
|
||||
const request = lastUser >= 0 ? list[lastUser] : null;
|
||||
const older = list.slice(0, lastUser >= 0 ? lastUser : 0).filter((m) => m.role !== 'system');
|
||||
const tail = list.slice(lastUser >= 0 ? lastUser + 1 : 0).filter((m) => m.role !== 'system');
|
||||
let groups = messageGroups(tail);
|
||||
// Preserve an existing summary across repeated compactions. The fallback
|
||||
// carries real excerpts rather than replacing memory with a generic notice.
|
||||
const memory = transcript(older.concat(tail));
|
||||
const fixed = systems.concat(request ? [request] : []);
|
||||
const spareChars = Math.max(0, (budget - conversationTokens(fixed) - 12) * CHAR_PER_TOKEN);
|
||||
const summaryCap = Math.min(2400, Math.floor(spareChars * (groups.length ? 0.4 : 1)));
|
||||
let summary = memory && summaryCap >= 64
|
||||
? { role: 'user', content: '[conversation summary]\n' + boundedExcerpt(memory, summaryCap) }
|
||||
: null;
|
||||
const assemble = () => systems.concat(summary ? [summary] : [], request ? [request] : [], groups.flat());
|
||||
let keep = assemble();
|
||||
// Shrink bulky results first; preserve call ids, arguments and ordering.
|
||||
for (const cap of [1600, 600, 180]) {
|
||||
if (!overHistoryBudget(keep, budget)) break;
|
||||
groups = groups.map((group) => group.map((m) => truncateMsg(m, cap)));
|
||||
keep = assemble();
|
||||
}
|
||||
let keep = messages.slice();
|
||||
while (overHistoryBudget(keep, budget) && keep.length > 4) {
|
||||
let idx = keep.findIndex((m, i) => i > 0 && m.role === 'tool');
|
||||
if (idx < 0) idx = keep.findIndex((m, i) => i > 1 && m.role === 'assistant');
|
||||
if (idx < 0) break;
|
||||
keep.splice(idx, 1);
|
||||
while (overHistoryBudget(keep, budget) && groups.length) {
|
||||
groups.shift();
|
||||
keep = assemble();
|
||||
}
|
||||
const maxMsg = aggressive ? 1200 : 3200;
|
||||
if (overHistoryBudget(keep, budget)) {
|
||||
const lastUser = lastRealUserIndex(keep);
|
||||
keep = keep.map((m, i) => (i === 0 || i === lastUser ? m : truncateMsg(m, maxMsg)));
|
||||
}
|
||||
if (overHistoryBudget(keep, budget) && keep.length > 3) {
|
||||
const head = keep[0];
|
||||
const lastUserIdx = lastRealUserIndex(keep);
|
||||
const lastUser = lastUserIdx >= 0 ? keep[lastUserIdx] : null;
|
||||
const tail = [];
|
||||
for (let i = keep.length - 1; i >= 1 && tail.length < (aggressive ? 2 : 4); i--) {
|
||||
if (i === lastUserIdx) continue;
|
||||
tail.unshift(keep[i]);
|
||||
}
|
||||
keep = [head, { role: 'user', content: '[conversation summary]\nEarlier turns were compacted to fit the model window.' }];
|
||||
if (lastUser) keep.push(lastUser);
|
||||
keep = keep.concat(tail);
|
||||
keep = keep.map((m, i) => (i === 0 ? m : truncateMsg(m, aggressive ? 700 : 1800)));
|
||||
}
|
||||
while (overHistoryBudget(keep, budget) && keep.length > 3) {
|
||||
const dropAt = keep.findIndex((m, i) => i > 1 && !isRealUser(m));
|
||||
if (dropAt < 0) break;
|
||||
keep.splice(dropAt, 1);
|
||||
if (overHistoryBudget(keep, budget) && summary) {
|
||||
summary = null;
|
||||
keep = assemble();
|
||||
}
|
||||
// The system instructions and latest request are never silently truncated.
|
||||
// If these alone exceed the window, the caller reports the overflow.
|
||||
return keep;
|
||||
}
|
||||
|
||||
function boundedExcerpt(text, maxChars) {
|
||||
if (text.length <= maxChars) return text;
|
||||
const marker = '\n[earlier details omitted]\n';
|
||||
const remaining = Math.max(0, maxChars - marker.length);
|
||||
const head = Math.floor(remaining * 0.6);
|
||||
return text.slice(0, head) + marker + text.slice(-(remaining - head));
|
||||
}
|
||||
|
||||
function compact(messages, opts) {
|
||||
return heuristicCompact(messages, opts);
|
||||
}
|
||||
@@ -233,7 +249,10 @@ function transcript(messages) {
|
||||
.map((m) => {
|
||||
const role = m && m.role ? m.role : 'unknown';
|
||||
const name = m && m.name ? ' ' + m.name : '';
|
||||
return role + name + ':\n' + String(m && m.content != null ? m.content : '');
|
||||
const content = m && m.content != null ? m.content : '';
|
||||
const body = typeof content === 'string' ? content : JSON.stringify(content);
|
||||
if (body.startsWith('[conversation summary]')) return body.slice('[conversation summary]'.length).trim();
|
||||
return role + name + ':\n' + body + (m && m.tool_calls ? '\nTool calls: ' + JSON.stringify(m.tool_calls) : '');
|
||||
})
|
||||
.join('\n\n');
|
||||
}
|
||||
@@ -275,7 +294,7 @@ function compactReminder(opts) {
|
||||
async function compactWithLlm(messages, opts) {
|
||||
opts = opts || {};
|
||||
const fallback = () => heuristicCompact(messages, opts);
|
||||
if (!opts.aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES) {
|
||||
if (!opts.aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES && conversationTokens((messages || []).filter((m) => m.role !== 'system')) <= 1024) {
|
||||
return (messages || []).slice();
|
||||
}
|
||||
const complete = opts.complete;
|
||||
@@ -284,7 +303,7 @@ async function compactWithLlm(messages, opts) {
|
||||
const cap =
|
||||
opts.maxTranscriptChars ||
|
||||
Math.max(1500, Math.min(24000, Math.floor((opts.budgetTokens || 4000) * CHAR_PER_TOKEN * 0.45)));
|
||||
const body = truncate.truncateWithMarker(transcript(messages), cap);
|
||||
const body = truncate.truncateWithMarker(transcript((messages || []).filter((m) => m.role !== 'system')), cap);
|
||||
const prompt = voice ? VOICE_COMPACT_PROMPT : COMPACT_PROMPT;
|
||||
const sys = voice
|
||||
? 'Reply with the four summary sections only. No tools. Do not greet.'
|
||||
@@ -299,7 +318,7 @@ async function compactWithLlm(messages, opts) {
|
||||
});
|
||||
const summary = result && result.text;
|
||||
if (isDegenerate(summary)) return fallback();
|
||||
return rebuildHistory(messages, summary);
|
||||
return heuristicCompact(rebuildHistory(messages, summary), Object.assign({}, opts, { aggressive: true }));
|
||||
} catch (_) {
|
||||
return fallback();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user