kovence-cli 1.2.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/README.md +66 -6
- package/nd.js +22 -1
- package/package.json +1 -1
- package/providers.js +524 -113
- package/src/agent/history.js +502 -9
- package/src/agent/project_context.js +219 -24
- package/src/agent/request_logger.js +148 -0
- package/src/agent/resilience.js +296 -46
- package/src/agent/streaming_write.js +23 -15
- package/src/agent/subagent.js +655 -0
- package/src/agent/turn.js +1211 -269
- package/src/cli/args.js +9 -1
- package/src/cli/sinks.js +193 -106
- package/src/commands/handlers.js +245 -7
- package/src/config/model_registry.js +39 -2
- package/src/config/sessions.js +19 -2
- package/src/config/skills.js +425 -0
- package/src/permissions/engine.js +22 -6
- package/src/permissions/store.js +4 -1
- package/src/settings/schema.js +298 -9
- package/src/settings/store.js +75 -6
- package/src/tools/browser.js +722 -0
- package/src/tools/edit.js +471 -0
- package/src/tools/exec.js +349 -34
- package/src/tools/patch_git.js +12 -1
- package/src/tools/read_tracker.js +87 -0
- package/src/tools/tasks.js +68 -28
- package/tools.js +784 -171
- package/tui.js +142 -30
- package/ui.js +22 -1
package/src/agent/history.js
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const { lookupModelCapabilities, DEFAULT_MAX_OUTPUT_TOKENS } = require("../config/model_registry");
|
|
4
|
+
|
|
5
|
+
const COMPACTION_SYSTEM_PROMPT = [
|
|
6
|
+
"You compress the earlier part of a coding-agent conversation so the agent can continue the work without it.",
|
|
7
|
+
"Write a dense, factual summary. Do not invent anything. Keep exact file paths, identifiers, commands,",
|
|
8
|
+
"error messages and numbers. Keep decisions together with their reason. If an earlier summary is included,",
|
|
9
|
+
"merge it into your new summary instead of repeating it.",
|
|
10
|
+
"Use exactly these headings:",
|
|
11
|
+
"## Goal",
|
|
12
|
+
"## Instructions (user constraints and preferences that still apply)",
|
|
13
|
+
"## Discoveries",
|
|
14
|
+
"## Decisions",
|
|
15
|
+
"## Accomplished",
|
|
16
|
+
"## In progress and remaining todos",
|
|
17
|
+
"## Open problems and errors",
|
|
18
|
+
"## Relevant files (path - why it matters - current state)",
|
|
19
|
+
].join("\n");
|
|
20
|
+
|
|
21
|
+
const COMPACTION_MARKER_PREFIX = "[Compacted Conversation Summary]";
|
|
22
|
+
const PRUNED_MARKER_PREFIX = "[pruned tool result:";
|
|
23
|
+
|
|
24
|
+
const messageTokenCache = new WeakMap();
|
|
25
|
+
const toolsTokenCache = new WeakMap();
|
|
26
|
+
|
|
3
27
|
/**
|
|
4
28
|
* Estimate token count with multi-byte Unicode & JSON structure adjustment (more accurate than chars/4).
|
|
5
29
|
*/
|
|
@@ -12,20 +36,32 @@ function estimateTextTokens(text) {
|
|
|
12
36
|
if (str.charCodeAt(i) <= 0x7f) ascii += 1;
|
|
13
37
|
else nonAscii += 1;
|
|
14
38
|
}
|
|
15
|
-
// Non-ASCII scripts (Bengali/CJK/emoji)
|
|
39
|
+
// Non-ASCII scripts (Bengali/CJK/emoji) consume ~1-2 tokens per UTF-16 char vs ~0.28 for ASCII
|
|
16
40
|
return Math.ceil(ascii * 0.28 + nonAscii * 1.15);
|
|
17
41
|
}
|
|
18
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Estimate tokens for a single message, cached per message object unless content/tool_calls changed.
|
|
45
|
+
*/
|
|
19
46
|
function estimateMessageTokens(msg) {
|
|
20
47
|
if (!msg) return 0;
|
|
48
|
+
const cached = messageTokenCache.get(msg);
|
|
49
|
+
if (cached && cached.content === msg.content && cached.tool_calls === msg.tool_calls) {
|
|
50
|
+
return cached.tokens;
|
|
51
|
+
}
|
|
21
52
|
let tokens = 6; // per-message framing overhead
|
|
22
|
-
if (msg.content)
|
|
53
|
+
if (msg.content) {
|
|
23
54
|
tokens += estimateTextTokens(typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content));
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(msg.images) && !msg.imagesPruned && msg.images.length > 0) {
|
|
57
|
+
tokens += msg.images.length * 1100;
|
|
58
|
+
}
|
|
24
59
|
if (Array.isArray(msg.tool_calls)) {
|
|
25
60
|
for (const tc of msg.tool_calls) {
|
|
26
61
|
tokens += 8 + estimateTextTokens(tc.function?.name) + estimateTextTokens(tc.function?.arguments);
|
|
27
62
|
}
|
|
28
63
|
}
|
|
64
|
+
messageTokenCache.set(msg, { content: msg.content, tool_calls: msg.tool_calls, tokens });
|
|
29
65
|
return tokens;
|
|
30
66
|
}
|
|
31
67
|
|
|
@@ -33,6 +69,277 @@ function estimateMessagesTokens(messages) {
|
|
|
33
69
|
return (Array.isArray(messages) ? messages : []).reduce((sum, m) => sum + estimateMessageTokens(m), 0);
|
|
34
70
|
}
|
|
35
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Estimate token overhead of tool definitions, cached by array reference + length.
|
|
74
|
+
*/
|
|
75
|
+
function estimateToolsTokens(toolDefinitions) {
|
|
76
|
+
if (!Array.isArray(toolDefinitions) || toolDefinitions.length === 0) return 0;
|
|
77
|
+
const cached = toolsTokenCache.get(toolDefinitions);
|
|
78
|
+
if (cached && cached.length === toolDefinitions.length) {
|
|
79
|
+
return cached.tokens;
|
|
80
|
+
}
|
|
81
|
+
const tokens = estimateTextTokens(JSON.stringify(toolDefinitions)) + toolDefinitions.length * 6;
|
|
82
|
+
toolsTokenCache.set(toolDefinitions, { length: toolDefinitions.length, tokens });
|
|
83
|
+
return tokens;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Resolve effective context budget from model registry + user settings.
|
|
88
|
+
* @param {{ model?: string, settings?: Record<string, any> }} [opts]
|
|
89
|
+
*/
|
|
90
|
+
function resolveContextBudget({ model = "", settings = {} } = {}) {
|
|
91
|
+
const reg = lookupModelCapabilities(model);
|
|
92
|
+
const hasExplicitWindow = Number.isFinite(settings?.contextWindow) && settings.contextWindow > 0;
|
|
93
|
+
const contextWindow = hasExplicitWindow ? settings.contextWindow : reg.known ? reg.contextWindow : 32000;
|
|
94
|
+
const isUnknownModelWindow = !hasExplicitWindow && !reg.known;
|
|
95
|
+
|
|
96
|
+
const modelMaxOut = reg.maxOutputTokens || DEFAULT_MAX_OUTPUT_TOKENS;
|
|
97
|
+
const maxOutputTokens =
|
|
98
|
+
Number.isFinite(settings?.maxTokens) && settings.maxTokens > 0
|
|
99
|
+
? reg.known
|
|
100
|
+
? Math.min(settings.maxTokens, modelMaxOut)
|
|
101
|
+
: settings.maxTokens
|
|
102
|
+
: Math.min(DEFAULT_MAX_OUTPUT_TOKENS, modelMaxOut);
|
|
103
|
+
|
|
104
|
+
const defaultReserve = Math.max(20000, maxOutputTokens);
|
|
105
|
+
let reserve =
|
|
106
|
+
Number.isFinite(settings?.contextReserveTokens) && settings.contextReserveTokens > 0
|
|
107
|
+
? settings.contextReserveTokens
|
|
108
|
+
: defaultReserve;
|
|
109
|
+
|
|
110
|
+
// Guard tiny synthetic windows in tests so usable remains positive
|
|
111
|
+
if (reserve >= contextWindow) {
|
|
112
|
+
reserve = Math.floor(contextWindow * 0.4);
|
|
113
|
+
}
|
|
114
|
+
const usable = Math.max(1024, contextWindow - reserve);
|
|
115
|
+
|
|
116
|
+
const pruneThreshold =
|
|
117
|
+
Number.isFinite(settings?.pruneThreshold) && settings.pruneThreshold > 0 ? settings.pruneThreshold : 0.75;
|
|
118
|
+
const compactThreshold =
|
|
119
|
+
Number.isFinite(settings?.compactThreshold) && settings.compactThreshold > 0 ? settings.compactThreshold : 0.85;
|
|
120
|
+
const pruneProtectTokens =
|
|
121
|
+
Number.isFinite(settings?.pruneProtectTokens) && settings.pruneProtectTokens >= 0
|
|
122
|
+
? settings.pruneProtectTokens
|
|
123
|
+
: 40000;
|
|
124
|
+
const pruneMinSavings =
|
|
125
|
+
Number.isFinite(settings?.pruneMinSavings) && settings.pruneMinSavings >= 0 ? settings.pruneMinSavings : 20000;
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
contextWindow,
|
|
129
|
+
reserve,
|
|
130
|
+
usable,
|
|
131
|
+
maxOutputTokens,
|
|
132
|
+
pruneThreshold,
|
|
133
|
+
compactThreshold,
|
|
134
|
+
pruneProtectTokens,
|
|
135
|
+
pruneMinSavings,
|
|
136
|
+
isUnknownModelWindow,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function createTokenCalibration() {
|
|
141
|
+
return {
|
|
142
|
+
ratio: 1.0,
|
|
143
|
+
samples: 0,
|
|
144
|
+
lastPromptTokens: 0,
|
|
145
|
+
lastRawEstimate: 0,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function clamp(val, min, max) {
|
|
150
|
+
return Math.min(max, Math.max(min, val));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function updateTokenCalibration(cal, rawEstimate, promptTokens) {
|
|
154
|
+
if (
|
|
155
|
+
!cal ||
|
|
156
|
+
!Number.isFinite(promptTokens) ||
|
|
157
|
+
promptTokens <= 0 ||
|
|
158
|
+
!Number.isFinite(rawEstimate) ||
|
|
159
|
+
rawEstimate <= 0
|
|
160
|
+
) {
|
|
161
|
+
return cal;
|
|
162
|
+
}
|
|
163
|
+
const instant = clamp(promptTokens / rawEstimate, 0.5, 2.0);
|
|
164
|
+
cal.ratio = cal.samples === 0 ? instant : clamp(0.6 * cal.ratio + 0.4 * instant, 0.5, 2.0);
|
|
165
|
+
cal.samples += 1;
|
|
166
|
+
cal.lastPromptTokens = promptTokens;
|
|
167
|
+
cal.lastRawEstimate = rawEstimate;
|
|
168
|
+
return cal;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function estimateCalibratedTokens(cal, rawEstimate) {
|
|
172
|
+
const raw = Math.max(0, Number(rawEstimate) || 0);
|
|
173
|
+
if (!cal || cal.samples === 0) return raw;
|
|
174
|
+
const scaled = Math.ceil(raw * cal.ratio);
|
|
175
|
+
if (cal.lastPromptTokens > 0 && raw >= cal.lastRawEstimate) {
|
|
176
|
+
const delta = Math.ceil((raw - cal.lastRawEstimate) * cal.ratio);
|
|
177
|
+
return Math.max(scaled, cal.lastPromptTokens + delta);
|
|
178
|
+
}
|
|
179
|
+
return scaled;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function formatShortToolArgs(rawArgs) {
|
|
183
|
+
if (!rawArgs) return "";
|
|
184
|
+
let obj = rawArgs;
|
|
185
|
+
if (typeof rawArgs === "string") {
|
|
186
|
+
try {
|
|
187
|
+
obj = JSON.parse(rawArgs);
|
|
188
|
+
} catch {
|
|
189
|
+
return String(rawArgs).replace(/\s+/g, " ").trim().slice(0, 60);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (!obj || typeof obj !== "object") return "";
|
|
193
|
+
const first = obj.path || obj.command || obj.query || obj.pattern || obj.url || obj.from || obj.fact || "";
|
|
194
|
+
return first ? String(first).replace(/\s+/g, " ").trim().slice(0, 60) : "";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function buildPrunedMarker(toolName, shortArgs, origChars) {
|
|
198
|
+
const sig = shortArgs ? `${toolName}(${shortArgs})` : `${toolName}()`;
|
|
199
|
+
return `${PRUNED_MARKER_PREFIX} ${sig}, ${origChars} chars removed; call the tool again if you need it]`;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Zone B: Prune old tool outputs in place of content only (never removes or reorders tool_call/tool_result).
|
|
204
|
+
* Protects recent ~40k tokens of tool output and (when earlier turns have enough savings) the last 2 user turns.
|
|
205
|
+
* Prunes large run_command / web_fetch outputs first, then oldest-first.
|
|
206
|
+
*/
|
|
207
|
+
function pruneToolOutputs(
|
|
208
|
+
history,
|
|
209
|
+
{ protectTokens = 40000, minSavings = 20000, targetSavedTokens = Infinity, aggressive = false } = {}
|
|
210
|
+
) {
|
|
211
|
+
const list = Array.isArray(history) ? history : [];
|
|
212
|
+
if (list.length === 0) {
|
|
213
|
+
return { history: list, prunedCount: 0, savedTokens: 0, savedChars: 0, applied: false };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Map tool_call_id -> { name, shortArgs }
|
|
217
|
+
const toolMeta = new Map();
|
|
218
|
+
const userTurnIndices = [];
|
|
219
|
+
for (let i = 0; i < list.length; i++) {
|
|
220
|
+
const m = list[i];
|
|
221
|
+
if (!m) continue;
|
|
222
|
+
if (m.role === "user" && !m.isCompactionSummary) {
|
|
223
|
+
userTurnIndices.push(i);
|
|
224
|
+
}
|
|
225
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls)) {
|
|
226
|
+
for (const tc of m.tool_calls) {
|
|
227
|
+
if (tc?.id) {
|
|
228
|
+
toolMeta.set(tc.id, {
|
|
229
|
+
name: tc.function?.name || "tool",
|
|
230
|
+
shortArgs: formatShortToolArgs(tc.function?.arguments),
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Walk backwards to protect the most recent `protectTokens` of tool output
|
|
238
|
+
const protectedIndices = new Set();
|
|
239
|
+
let recentToolTokens = 0;
|
|
240
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
241
|
+
const m = list[i];
|
|
242
|
+
if (!m) continue;
|
|
243
|
+
if (m.role === "user") {
|
|
244
|
+
protectedIndices.add(i);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
if (m.role === "tool") {
|
|
248
|
+
const tok = estimateMessageTokens(m);
|
|
249
|
+
if (recentToolTokens < protectTokens) {
|
|
250
|
+
protectedIndices.add(i);
|
|
251
|
+
recentToolTokens += tok;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const collectCandidates = (minProtectedIdx) => {
|
|
257
|
+
const items = [];
|
|
258
|
+
for (let i = 0; i < list.length; i++) {
|
|
259
|
+
if (minProtectedIdx !== null && i >= minProtectedIdx) break;
|
|
260
|
+
if (protectedIndices.has(i)) continue;
|
|
261
|
+
const m = list[i];
|
|
262
|
+
if (!m || m.role !== "tool") continue;
|
|
263
|
+
const contentStr = typeof m.content === "string" ? m.content : JSON.stringify(m.content || "");
|
|
264
|
+
if (contentStr.startsWith(PRUNED_MARKER_PREFIX)) continue;
|
|
265
|
+
|
|
266
|
+
const meta = (m.tool_call_id && toolMeta.get(m.tool_call_id)) || {
|
|
267
|
+
name: m.name || "tool",
|
|
268
|
+
shortArgs: "",
|
|
269
|
+
};
|
|
270
|
+
const marker = buildPrunedMarker(meta.name, meta.shortArgs, contentStr.length);
|
|
271
|
+
if (contentStr.length <= marker.length + 20) continue;
|
|
272
|
+
|
|
273
|
+
const origTokens = estimateTextTokens(contentStr);
|
|
274
|
+
const markerTokens = estimateTextTokens(marker);
|
|
275
|
+
const savingTokens = Math.max(0, origTokens - markerTokens);
|
|
276
|
+
if (savingTokens <= 0) continue;
|
|
277
|
+
|
|
278
|
+
items.push({
|
|
279
|
+
index: i,
|
|
280
|
+
name: meta.name,
|
|
281
|
+
marker,
|
|
282
|
+
savingTokens,
|
|
283
|
+
savingChars: contentStr.length - marker.length,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return items;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// Protect the last 2 user turns whenever older turns can supply enough savings (or in aggressive mode if any exist)
|
|
290
|
+
let candidates = [];
|
|
291
|
+
if (userTurnIndices.length > 2) {
|
|
292
|
+
const last2Start = userTurnIndices[userTurnIndices.length - 2];
|
|
293
|
+
const olderCandidates = collectCandidates(last2Start);
|
|
294
|
+
const olderPotential = olderCandidates.reduce((s, c) => s + c.savingTokens, 0);
|
|
295
|
+
if (olderPotential >= minSavings || (aggressive && olderPotential > 0)) {
|
|
296
|
+
candidates = olderCandidates;
|
|
297
|
+
} else {
|
|
298
|
+
candidates = collectCandidates(null);
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
candidates = collectCandidates(null);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const totalPossibleSavings = candidates.reduce((s, c) => s + c.savingTokens, 0);
|
|
305
|
+
if (!aggressive && (totalPossibleSavings < minSavings || totalPossibleSavings === 0)) {
|
|
306
|
+
return { history: list, prunedCount: 0, savedTokens: 0, savedChars: 0, applied: false };
|
|
307
|
+
}
|
|
308
|
+
if (totalPossibleSavings === 0) {
|
|
309
|
+
return { history: list, prunedCount: 0, savedTokens: 0, savedChars: 0, applied: false };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Priority order: large run_command / web_fetch first (largest first), then remaining oldest-first
|
|
313
|
+
const tier1 = candidates
|
|
314
|
+
.filter((c) => c.name === "run_command" || c.name === "web_fetch")
|
|
315
|
+
.sort((a, b) => b.savingTokens - a.savingTokens || a.index - b.index);
|
|
316
|
+
const tier2 = candidates
|
|
317
|
+
.filter((c) => c.name !== "run_command" && c.name !== "web_fetch")
|
|
318
|
+
.sort((a, b) => a.index - b.index);
|
|
319
|
+
const ordered = [...tier1, ...tier2];
|
|
320
|
+
|
|
321
|
+
const next = list.map((m) => (m ? { ...m } : m));
|
|
322
|
+
let savedTokens = 0;
|
|
323
|
+
let savedChars = 0;
|
|
324
|
+
let prunedCount = 0;
|
|
325
|
+
|
|
326
|
+
for (const c of ordered) {
|
|
327
|
+
next[c.index].content = c.marker;
|
|
328
|
+
if (next[c.index].images) {
|
|
329
|
+
delete next[c.index].images;
|
|
330
|
+
next[c.index].imagesPruned = true;
|
|
331
|
+
}
|
|
332
|
+
savedTokens += c.savingTokens;
|
|
333
|
+
savedChars += c.savingChars;
|
|
334
|
+
prunedCount += 1;
|
|
335
|
+
if (!aggressive && savedTokens >= targetSavedTokens && savedTokens >= minSavings) {
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return { history: next, prunedCount, savedTokens, savedChars, applied: prunedCount > 0 };
|
|
341
|
+
}
|
|
342
|
+
|
|
36
343
|
/**
|
|
37
344
|
* Group linear chat history into atomic turns starting at each user message.
|
|
38
345
|
*/
|
|
@@ -48,7 +355,6 @@ function splitHistoryIntoTurns(history) {
|
|
|
48
355
|
continue;
|
|
49
356
|
}
|
|
50
357
|
if (!current) {
|
|
51
|
-
// Orphan leading assistant/tool before first user message — keep attached cleanly or discard if tool-only
|
|
52
358
|
if (msg.role === "tool") continue;
|
|
53
359
|
current = [msg];
|
|
54
360
|
} else {
|
|
@@ -59,14 +365,179 @@ function splitHistoryIntoTurns(history) {
|
|
|
59
365
|
return turns;
|
|
60
366
|
}
|
|
61
367
|
|
|
368
|
+
/**
|
|
369
|
+
* Group a list of messages into atomic assistant+tool units so tool_calls and tool results never split.
|
|
370
|
+
*/
|
|
371
|
+
function groupAtomicSteps(messages) {
|
|
372
|
+
const units = [];
|
|
373
|
+
let i = 0;
|
|
374
|
+
while (i < messages.length) {
|
|
375
|
+
const m = messages[i];
|
|
376
|
+
if (!m) {
|
|
377
|
+
i += 1;
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
381
|
+
const unit = [m];
|
|
382
|
+
let j = i + 1;
|
|
383
|
+
while (j < messages.length && messages[j]?.role === "tool") {
|
|
384
|
+
unit.push(messages[j]);
|
|
385
|
+
j += 1;
|
|
386
|
+
}
|
|
387
|
+
units.push(unit);
|
|
388
|
+
i = j;
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
units.push([m]);
|
|
392
|
+
i += 1;
|
|
393
|
+
}
|
|
394
|
+
return units;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Split history cleanly for Zone C compaction:
|
|
399
|
+
* - Extracts any earlier compaction summary so a second compaction folds it instead of drifting
|
|
400
|
+
* - Keeps the latest user message verbatim and a recent tail (last ~2 user turns, never splitting tool_calls)
|
|
401
|
+
* - Supports mid-tool-loop compaction within a single active user turn
|
|
402
|
+
*/
|
|
403
|
+
function splitHistoryForCompaction(history, { keepUserTurns = 2, manual = false } = {}) {
|
|
404
|
+
const clean = (Array.isArray(history) ? history : []).filter(Boolean);
|
|
405
|
+
let earlierSummary = "";
|
|
406
|
+
const body = [];
|
|
407
|
+
|
|
408
|
+
for (const m of clean) {
|
|
409
|
+
if (m.isCompactionSummary && body.length === 0) {
|
|
410
|
+
const raw = String(m.content || "");
|
|
411
|
+
earlierSummary = raw.startsWith(COMPACTION_MARKER_PREFIX)
|
|
412
|
+
? raw.slice(COMPACTION_MARKER_PREFIX.length).trim()
|
|
413
|
+
: raw.trim();
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
body.push(m);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (body.length === 0) {
|
|
420
|
+
return { earlierSummary, older: [], tail: [], latestUserRequest: "" };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const turns = splitHistoryIntoTurns(body).filter((t) => t[0]?.role === "user");
|
|
424
|
+
const lastTurn = turns[turns.length - 1] || [];
|
|
425
|
+
const latestUserRequest = String(lastTurn[0]?.content || "");
|
|
426
|
+
|
|
427
|
+
// Multi-turn history: keep recent turns verbatim and summarize earlier turns
|
|
428
|
+
if (turns.length >= 3 || (manual && turns.length >= 2)) {
|
|
429
|
+
const keepCount = manual ? 1 : Math.min(keepUserTurns, turns.length - 1);
|
|
430
|
+
const splitAt = turns.length - keepCount;
|
|
431
|
+
const older = turns.slice(0, splitAt).flat();
|
|
432
|
+
const tail = turns.slice(splitAt).flat();
|
|
433
|
+
return { earlierSummary, older, tail, latestUserRequest };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (turns.length === 2) {
|
|
437
|
+
return { earlierSummary, older: turns[0], tail: turns[1], latestUserRequest };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Single-turn (or mid-tool-loop) compaction: keep user prompt verbatim + last 1-2 atomic assistant/tool groups
|
|
441
|
+
const units = groupAtomicSteps(body);
|
|
442
|
+
const userUnit = units[0]?.[0]?.role === "user" ? units[0] : null;
|
|
443
|
+
const stepUnits = userUnit ? units.slice(1) : units;
|
|
444
|
+
if (stepUnits.length <= 1) {
|
|
445
|
+
return { earlierSummary, older: [], tail: body, latestUserRequest };
|
|
446
|
+
}
|
|
447
|
+
const keepAtomicCount = Math.min(2, Math.max(1, stepUnits.length - 1));
|
|
448
|
+
const olderUnits = stepUnits.slice(0, stepUnits.length - keepAtomicCount);
|
|
449
|
+
const tailUnits = stepUnits.slice(stepUnits.length - keepAtomicCount);
|
|
450
|
+
|
|
451
|
+
const older = olderUnits.flat();
|
|
452
|
+
const tail = userUnit ? [...userUnit, ...tailUnits.flat()] : tailUnits.flat();
|
|
453
|
+
return { earlierSummary, older, tail, latestUserRequest };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function serializeMessageForSummary(msg) {
|
|
457
|
+
if (!msg) return "";
|
|
458
|
+
if (msg.role === "user") {
|
|
459
|
+
return `USER:\n${typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)}`;
|
|
460
|
+
}
|
|
461
|
+
if (msg.role === "assistant") {
|
|
462
|
+
const parts = [];
|
|
463
|
+
if (msg.content) parts.push(String(msg.content));
|
|
464
|
+
if (Array.isArray(msg.tool_calls) && msg.tool_calls.length) {
|
|
465
|
+
const calls = msg.tool_calls
|
|
466
|
+
.map((tc) => `${tc.function?.name || "tool"}(${tc.function?.arguments || "{}"})`)
|
|
467
|
+
.join("; ");
|
|
468
|
+
parts.push(`[Tool Calls: ${calls}]`);
|
|
469
|
+
}
|
|
470
|
+
return `ASSISTANT:\n${parts.join("\n")}`;
|
|
471
|
+
}
|
|
472
|
+
if (msg.role === "tool") {
|
|
473
|
+
const label = msg.name ? `TOOL (${msg.name})` : "TOOL";
|
|
474
|
+
return `${label}:\n${typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content || "")}`;
|
|
475
|
+
}
|
|
476
|
+
return `${String(msg.role).toUpperCase()}:\n${String(msg.content || "")}`;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Split older messages into chunks that fit within maxChunkTokens WITHOUT truncating any content.
|
|
481
|
+
*/
|
|
482
|
+
function buildCompactionChunks(olderMessages, maxChunkTokens = 12000) {
|
|
483
|
+
const budget = Math.max(1000, Number(maxChunkTokens) || 12000);
|
|
484
|
+
const chunks = [];
|
|
485
|
+
let currentParts = [];
|
|
486
|
+
let currentTokens = 0;
|
|
487
|
+
|
|
488
|
+
const flush = () => {
|
|
489
|
+
if (currentParts.length) {
|
|
490
|
+
chunks.push(currentParts.join("\n\n"));
|
|
491
|
+
currentParts = [];
|
|
492
|
+
currentTokens = 0;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
for (const msg of Array.isArray(olderMessages) ? olderMessages : []) {
|
|
497
|
+
const serialized = serializeMessageForSummary(msg);
|
|
498
|
+
if (!serialized) continue;
|
|
499
|
+
const tok = estimateTextTokens(serialized);
|
|
500
|
+
if (tok <= budget) {
|
|
501
|
+
if (currentTokens + tok > budget && currentParts.length > 0) {
|
|
502
|
+
flush();
|
|
503
|
+
}
|
|
504
|
+
currentParts.push(serialized);
|
|
505
|
+
currentTokens += tok;
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// Single huge message: split into sub-chunks cleanly without dropping characters
|
|
510
|
+
flush();
|
|
511
|
+
const approxCharsPerChunk = Math.max(1000, Math.floor(serialized.length * (budget / tok)));
|
|
512
|
+
for (let offset = 0; offset < serialized.length; offset += approxCharsPerChunk) {
|
|
513
|
+
chunks.push(serialized.slice(offset, offset + approxCharsPerChunk));
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
flush();
|
|
517
|
+
return chunks;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function buildCompactionMessages({ earlierSummary = "", chunkText = "", latestUserRequest = "" } = {}) {
|
|
521
|
+
const parts = [];
|
|
522
|
+
if (earlierSummary) {
|
|
523
|
+
parts.push(`Earlier summary:\n${earlierSummary}`);
|
|
524
|
+
}
|
|
525
|
+
parts.push(`Older conversation to compress:\n${chunkText}`);
|
|
526
|
+
parts.push(`The user's latest request (do not drop it): ${latestUserRequest || "(none)"}`);
|
|
527
|
+
return [
|
|
528
|
+
{ role: "system", content: COMPACTION_SYSTEM_PROMPT },
|
|
529
|
+
{ role: "user", content: parts.join("\n\n") },
|
|
530
|
+
];
|
|
531
|
+
}
|
|
532
|
+
|
|
62
533
|
/**
|
|
63
534
|
* Shrink oversized earlier tool outputs within an oversized active turn without cutting user request or tool pairing.
|
|
535
|
+
* Used ONLY when the user explicitly sets a hard `maxHistoryMessages` limit.
|
|
64
536
|
*/
|
|
65
537
|
function compressOversizedTurn(turnMessages, maxMessages) {
|
|
66
538
|
if (turnMessages.length <= maxMessages) return turnMessages;
|
|
67
539
|
|
|
68
540
|
const result = turnMessages.map((m) => ({ ...m }));
|
|
69
|
-
// Keep original user request (index 0) and most recent 4 messages intact; prune old tool outputs first
|
|
70
541
|
const keepTailFrom = Math.max(1, result.length - 4);
|
|
71
542
|
for (let i = 1; i < keepTailFrom; i++) {
|
|
72
543
|
if (result[i].role === "tool" && typeof result[i].content === "string" && result[i].content.length > 350) {
|
|
@@ -79,18 +550,19 @@ function compressOversizedTurn(turnMessages, maxMessages) {
|
|
|
79
550
|
|
|
80
551
|
/**
|
|
81
552
|
* Turn-aware valid history slicer:
|
|
553
|
+
* - Unlimited by default (`maxMessages == null` or `<= 0`)
|
|
82
554
|
* - Never drops the active user request in long tool loops (B1)
|
|
83
555
|
* - Never splits assistant.tool_calls from its tool result messages
|
|
84
|
-
* - Respects both message count and token budget relative to contextWindow
|
|
85
556
|
*/
|
|
86
|
-
function sliceValidHistory(history, maxMessages =
|
|
557
|
+
function sliceValidHistory(history, maxMessages = null, maxTokenBudget = 0) {
|
|
87
558
|
const clean = (Array.isArray(history) ? history : []).filter(Boolean);
|
|
88
559
|
if (clean.length === 0) return [];
|
|
89
560
|
|
|
90
561
|
const turns = splitHistoryIntoTurns(clean).filter((t) => t[0]?.role === "user");
|
|
91
562
|
if (turns.length === 0) return [];
|
|
92
563
|
|
|
93
|
-
const
|
|
564
|
+
const hasMsgCap = Number.isFinite(Number(maxMessages)) && Number(maxMessages) > 0;
|
|
565
|
+
const limit = hasMsgCap ? Math.max(2, Number(maxMessages)) : Infinity;
|
|
94
566
|
const chosenTurns = [];
|
|
95
567
|
let totalMsgs = 0;
|
|
96
568
|
let totalTokens = 0;
|
|
@@ -100,12 +572,12 @@ function sliceValidHistory(history, maxMessages = 24, maxTokenBudget = 0) {
|
|
|
100
572
|
let turn = turns[i];
|
|
101
573
|
const isNewestTurn = i === turns.length - 1;
|
|
102
574
|
|
|
103
|
-
if (isNewestTurn && turn.length > limit) {
|
|
575
|
+
if (isNewestTurn && hasMsgCap && turn.length > limit) {
|
|
104
576
|
turn = compressOversizedTurn(turn, limit);
|
|
105
577
|
}
|
|
106
578
|
|
|
107
579
|
const turnTokens = estimateMessagesTokens(turn);
|
|
108
|
-
const fitsMsgs = totalMsgs + turn.length <= limit;
|
|
580
|
+
const fitsMsgs = !hasMsgCap || totalMsgs + turn.length <= limit;
|
|
109
581
|
const fitsTokens = !maxTokenBudget || totalTokens + turnTokens <= maxTokenBudget;
|
|
110
582
|
|
|
111
583
|
if (!isNewestTurn && (!fitsMsgs || !fitsTokens)) {
|
|
@@ -148,10 +620,31 @@ function sliceValidHistory(history, maxMessages = 24, maxTokenBudget = 0) {
|
|
|
148
620
|
return validated;
|
|
149
621
|
}
|
|
150
622
|
|
|
623
|
+
const CONTEXT_LENGTH_ERROR_RE =
|
|
624
|
+
/context_length_exceeded|maximum context length|context window|too many tokens|prompt is too long|input is too long|exceeds(?: the)? maximum(?: number of)? tokens|request too large|max_tokens.*context/i;
|
|
625
|
+
|
|
626
|
+
function isContextLengthError(err) {
|
|
627
|
+
const msg = typeof err === "string" ? err : err?.message || "";
|
|
628
|
+
return CONTEXT_LENGTH_ERROR_RE.test(msg);
|
|
629
|
+
}
|
|
630
|
+
|
|
151
631
|
module.exports = {
|
|
632
|
+
COMPACTION_SYSTEM_PROMPT,
|
|
633
|
+
COMPACTION_MARKER_PREFIX,
|
|
634
|
+
PRUNED_MARKER_PREFIX,
|
|
152
635
|
estimateTextTokens,
|
|
153
636
|
estimateMessageTokens,
|
|
154
637
|
estimateMessagesTokens,
|
|
638
|
+
estimateToolsTokens,
|
|
639
|
+
resolveContextBudget,
|
|
640
|
+
createTokenCalibration,
|
|
641
|
+
updateTokenCalibration,
|
|
642
|
+
estimateCalibratedTokens,
|
|
643
|
+
pruneToolOutputs,
|
|
155
644
|
splitHistoryIntoTurns,
|
|
645
|
+
splitHistoryForCompaction,
|
|
646
|
+
buildCompactionChunks,
|
|
647
|
+
buildCompactionMessages,
|
|
156
648
|
sliceValidHistory,
|
|
649
|
+
isContextLengthError,
|
|
157
650
|
};
|