chore: some more improvements to try and get as close to original as possible
This commit is contained in:
+30
-1
@@ -69,6 +69,32 @@ async function callMcpTool(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When context approaches the limit, truncate the content of old tool-result
|
||||
* messages to free space. Keeps the most recent N tool results intact so the
|
||||
* model still has fresh context; replaces earlier ones with a size notice.
|
||||
* Never touches system/user/assistant messages — only tool messages.
|
||||
*/
|
||||
function pruneToolMessages(messages: Message[], keepRecent = 6): Message[] {
|
||||
const toolIndices: number[] = [];
|
||||
for (let i = 0; i < messages.length; i++) {
|
||||
if (messages[i].role === "tool") toolIndices.push(i);
|
||||
}
|
||||
const pruneCount = Math.max(0, toolIndices.length - keepRecent);
|
||||
if (pruneCount === 0) return messages;
|
||||
|
||||
const toPrune = new Set(toolIndices.slice(0, pruneCount));
|
||||
let pruned = 0;
|
||||
const result = messages.map((msg, i) => {
|
||||
if (!toPrune.has(i)) return msg;
|
||||
const originalLen = typeof msg.content === "string" ? msg.content.length : 0;
|
||||
pruned++;
|
||||
return { ...msg, content: `[pruned: was ${originalLen} chars — context limit approached]` };
|
||||
});
|
||||
log.info(`» pruned ${pruned} old tool message(s) to reduce context`);
|
||||
return result;
|
||||
}
|
||||
|
||||
async function runOllamaLoop(ctx: AgentRunContext): Promise<AgentResult> {
|
||||
const ollamaHost = process.env.OLLAMA_HOST ?? "";
|
||||
if (!ollamaHost) {
|
||||
@@ -89,7 +115,7 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise<AgentResult> {
|
||||
const tools = await getOllamaTools(mcpClient);
|
||||
log.info(`» ${tools.length} tools available`);
|
||||
|
||||
const messages: Message[] = [
|
||||
let messages: Message[] = [
|
||||
{
|
||||
role: "user",
|
||||
content: ctx.instructions.full,
|
||||
@@ -149,6 +175,9 @@ async function runOllamaLoop(ctx: AgentRunContext): Promise<AgentResult> {
|
||||
log.info(
|
||||
` context: ${promptTokens} prompt + ${evalTokens ?? 0} eval = ${total} tokens (${pct}% of limit)`,
|
||||
);
|
||||
if (promptTokens > 200_000) {
|
||||
messages = pruneToolMessages(messages);
|
||||
}
|
||||
}
|
||||
|
||||
const assistantMessage = response.message;
|
||||
|
||||
Reference in New Issue
Block a user