mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 15:03:31 +00:00
perf(coding-agent): avoid loading session file for auto-compaction check
Use agent.state.messages instead of loading entries from disk
This commit is contained in:
parent
c89b1ec3c2
commit
1a97331af1
1 changed files with 16 additions and 6 deletions
|
|
@ -18,7 +18,7 @@ import {
|
|||
} from "@mariozechner/pi-tui";
|
||||
import { exec } from "child_process";
|
||||
import { getChangelogPath, parseChangelog } from "../changelog.js";
|
||||
import { calculateContextTokens, compact, getLastAssistantUsage, shouldCompact } from "../compaction.js";
|
||||
import { calculateContextTokens, compact, shouldCompact } from "../compaction.js";
|
||||
import { APP_NAME, getDebugLogPath, getModelsPath, getOAuthPath } from "../config.js";
|
||||
import { exportSessionToHtml } from "../export-html.js";
|
||||
import { getApiKeyForModel, getAvailableModels, invalidateOAuthCache } from "../model-config.js";
|
||||
|
|
@ -560,12 +560,22 @@ export class TuiRenderer {
|
|||
const settings = this.settingsManager.getCompactionSettings();
|
||||
if (!settings.enabled) return;
|
||||
|
||||
// Get last assistant usage
|
||||
const entries = this.sessionManager.loadEntries();
|
||||
const lastUsage = getLastAssistantUsage(entries);
|
||||
if (!lastUsage) return;
|
||||
// Get last non-aborted assistant message from agent state
|
||||
const messages = this.agent.state.messages;
|
||||
let lastAssistant: AssistantMessage | null = null;
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const msg = messages[i];
|
||||
if (msg.role === "assistant") {
|
||||
const assistantMsg = msg as AssistantMessage;
|
||||
if (assistantMsg.stopReason !== "aborted") {
|
||||
lastAssistant = assistantMsg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!lastAssistant) return;
|
||||
|
||||
const contextTokens = calculateContextTokens(lastUsage);
|
||||
const contextTokens = calculateContextTokens(lastAssistant.usage);
|
||||
const contextWindow = this.agent.state.model.contextWindow;
|
||||
|
||||
if (!shouldCompact(contextTokens, contextWindow, settings)) return;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue