feat: add /clear command to reset context and start fresh session

This commit is contained in:
Mario Zechner 2025-11-21 20:59:00 +01:00
parent 59af0fd53a
commit 1b6a70ccb1
6 changed files with 131 additions and 54 deletions

View file

@ -21,17 +21,6 @@ const __dirname = dirname(__filename);
const packageJson = JSON.parse(readFileSync(join(__dirname, "../package.json"), "utf-8"));
const VERSION = packageJson.version;
const envApiKeyMap: Record<KnownProvider, string[]> = {
google: ["GEMINI_API_KEY"],
openai: ["OPENAI_API_KEY"],
anthropic: ["ANTHROPIC_OAUTH_TOKEN", "ANTHROPIC_API_KEY"],
xai: ["XAI_API_KEY"],
groq: ["GROQ_API_KEY"],
cerebras: ["CEREBRAS_API_KEY"],
openrouter: ["OPENROUTER_API_KEY"],
zai: ["ZAI_API_KEY"],
};
const defaultModelPerProvider: Record<KnownProvider, string> = {
anthropic: "claude-sonnet-4-5",
openai: "gpt-5.1-codex",
@ -455,14 +444,9 @@ async function runInteractiveMode(
scopedModels,
);
// Initialize TUI
// Initialize TUI (subscribes to agent events internally)
await renderer.init();
// Set interrupt callback
renderer.setInterruptCallback(() => {
agent.abort();
});
// Render any existing messages (from --continue mode)
renderer.renderInitialMessages(agent.state);
@ -471,12 +455,6 @@ async function runInteractiveMode(
renderer.showWarning(modelFallbackMessage);
}
// Subscribe to agent events
agent.subscribe(async (event) => {
// Pass all events to the renderer
await renderer.handleEvent(event, agent.state);
});
// Interactive loop
while (true) {
const userInput = await renderer.getUserInput();
@ -683,11 +661,6 @@ export async function main(args: string[]) {
// Load previous messages if continuing or resuming
// This may update initialModel if restoring from session
if (parsed.continue || parsed.resume) {
const messages = sessionManager.loadMessages();
if (messages.length > 0 && shouldPrintMessages) {
console.log(chalk.dim(`Loaded ${messages.length} messages from previous session`));
}
// Load and restore model (overrides initialModel if found and has API key)
const savedModel = sessionManager.loadModel();
if (savedModel) {
@ -831,9 +804,6 @@ export async function main(args: string[]) {
}
}
// Note: Session will be started lazily after first user+assistant message exchange
// (unless continuing/resuming, in which case it's already initialized)
// Log loaded context files (they're already in the system prompt)
if (shouldPrintMessages && !parsed.continue && !parsed.resume) {
const contextFiles = loadProjectContextFiles();
@ -845,19 +815,6 @@ export async function main(args: string[]) {
}
}
// Subscribe to agent events to save messages
agent.subscribe((event) => {
// Save messages on completion
if (event.type === "message_end") {
sessionManager.saveMessage(event.message);
// Check if we should initialize session now (after first user+assistant exchange)
if (sessionManager.shouldInitializeSession(agent.state.messages)) {
sessionManager.startSession(agent.state);
}
}
});
// Route to appropriate mode
if (mode === "rpc") {
// RPC mode - headless operation
@ -890,8 +847,6 @@ export async function main(args: string[]) {
}
} else {
// Parse current and last versions
const currentParts = VERSION.split(".").map(Number);
const current = { major: currentParts[0] || 0, minor: currentParts[1] || 0, patch: currentParts[2] || 0 };
const changelogPath = getChangelogPath();
const entries = parseChangelog(changelogPath);
const newEntries = getNewEntries(entries, lastVersion);