@mariozechner/pi-coding-agent 0.12.11 → 0.12.13
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 +128 -267
- package/README.md +12 -0
- package/dist/fuzzy.d.ts +7 -0
- package/dist/fuzzy.d.ts.map +1 -0
- package/dist/fuzzy.js +64 -0
- package/dist/fuzzy.js.map +1 -0
- package/dist/fuzzy.test.d.ts +2 -0
- package/dist/fuzzy.test.d.ts.map +1 -0
- package/dist/fuzzy.test.js +76 -0
- package/dist/fuzzy.test.js.map +1 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +11 -14
- package/dist/main.js.map +1 -1
- package/dist/tui/footer.d.ts.map +1 -1
- package/dist/tui/footer.js +6 -2
- package/dist/tui/footer.js.map +1 -1
- package/dist/tui/model-selector.d.ts.map +1 -1
- package/dist/tui/model-selector.js +2 -13
- package/dist/tui/model-selector.js.map +1 -1
- package/dist/tui/session-selector.d.ts.map +1 -1
- package/dist/tui/session-selector.js +2 -14
- package/dist/tui/session-selector.js.map +1 -1
- package/dist/tui/tui-renderer.d.ts +6 -2
- package/dist/tui/tui-renderer.d.ts.map +1 -1
- package/dist/tui/tui-renderer.js +113 -12
- package/dist/tui/tui-renderer.js.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tui-renderer.d.ts","sourceRoot":"","sources":["../../src/tui/tui-renderer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAc,UAAU,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAChG,OAAO,KAAK,EAA6B,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAuB5E,OAAO,EAGN,KAAK,cAAc,EAGnB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAiB9D;;GAEG;AACH,qBAAa,WAAW;IACvB,OAAO,CAAC,EAAE,CAAM;IAChB,OAAO,CAAC,aAAa,CAAY;IACjC,OAAO,CAAC,wBAAwB,CAAY;IAC5C,OAAO,CAAC,eAAe,CAAY;IACnC,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,eAAe,CAAY;IACnC,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,cAAc,CAAiB;IACvC,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,eAAe,CAAC,CAAyB;IACjD,OAAO,CAAC,gBAAgB,CAAuB;IAE/C,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,iBAAiB,CAAuB;IAChD,OAAO,CAAC,UAAU,CAAuB;IAGzC,OAAO,CAAC,cAAc,CAAgB;IAGtC,OAAO,CAAC,kBAAkB,CAA0C;IAGpE,OAAO,CAAC,YAAY,CAA6C;IAGjE,OAAO,CAAC,gBAAgB,CAA0C;IAGlE,OAAO,CAAC,iBAAiB,CAA2C;IAGpE,OAAO,CAAC,aAAa,CAAuC;IAG5D,OAAO,CAAC,aAAa,CAAuC;IAG5D,OAAO,CAAC,mBAAmB,CAA6C;IAGxE,OAAO,CAAC,aAAa,CAAoB;IAGzC,OAAO,CAAC,kBAAkB,CAAQ;IAGlC,OAAO,CAAC,YAAY,CAAkE;IAGtF,OAAO,CAAC,kBAAkB,CAAS;IAGnC,OAAO,CAAC,iBAAiB,CAAS;IAGlC,OAAO,CAAC,WAAW,CAAC,CAAa;IAGjC,OAAO,CAAC,YAAY,CAA0B;IAE9C,YACC,KAAK,EAAE,KAAK,EACZ,cAAc,EAAE,cAAc,EAC9B,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,MAAM,EACf,iBAAiB,GAAE,MAAM,GAAG,IAAW,EACvC,UAAU,GAAE,MAAM,GAAG,IAAW,EAChC,YAAY,GAAE,KAAK,CAAC;QAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;QAAC,aAAa,EAAE,aAAa,CAAA;KAAE,CAAM,EAC7E,MAAM,GAAE,MAAM,GAAG,IAAW,EA6H5B;IAEK,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAoT1B;IAED,OAAO,CAAC,gBAAgB;YAsBV,mBAAmB;YA4BnB,WAAW;IA4KzB,OAAO,CAAC,gBAAgB;IAwBxB,qBAAqB,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAkF7C;IAEK,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAOpC;IAED,OAAO,CAAC,uBAAuB;IA0D/B,OAAO,CAAC,WAAW;IAgBnB,OAAO,CAAC,uBAAuB;IAM/B,OAAO,CAAC,kBAAkB;YA+BZ,UAAU;IA4GxB,OAAO,CAAC,yBAAyB;IAejC,OAAO,CAAC,6BAA6B;IAsBrC,WAAW,IAAI,IAAI,CAGlB;IAED,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAKpC;IAED,WAAW,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI,CAKxC;IAED,OAAO,CAAC,oBAAoB;IAsC5B,OAAO,CAAC,oBAAoB;IAQ5B,OAAO,CAAC,qBAAqB;IAkC7B,OAAO,CAAC,qBAAqB;IAQ7B,OAAO,CAAC,iBAAiB;IA0DzB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,iBAAiB;IAoCzB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,uBAAuB;IAuF/B,OAAO,CAAC,uBAAuB;YAQjB,iBAAiB;IAkH/B,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,mBAAmB;IAuB3B,OAAO,CAAC,iBAAiB;IAwCzB,OAAO,CAAC,oBAAoB;IAwE5B,OAAO,CAAC,sBAAsB;YAuBhB,kBAAkB;IAuChC,OAAO,CAAC,kBAAkB;IAgC1B,OAAO,CAAC,yBAAyB,CAAgC;YAMnD,iBAAiB;YAmGjB,oBAAoB;IAalC,OAAO,CAAC,wBAAwB;IAYhC,OAAO,CAAC,4BAA4B;IAapC,IAAI,IAAI,IAAI,CAUX;CACD","sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type { Agent, AgentEvent, AgentState, ThinkingLevel } from \"@mariozechner/pi-agent-core\";\nimport type { AssistantMessage, Message, Model } from \"@mariozechner/pi-ai\";\nimport type { SlashCommand } from \"@mariozechner/pi-tui\";\nimport {\n\tCombinedAutocompleteProvider,\n\tContainer,\n\tInput,\n\tLoader,\n\tMarkdown,\n\tProcessTerminal,\n\tSpacer,\n\tText,\n\tTruncatedText,\n\tTUI,\n\tvisibleWidth,\n} from \"@mariozechner/pi-tui\";\nimport { exec } from \"child_process\";\nimport { getChangelogPath, parseChangelog } from \"../changelog.js\";\nimport { copyToClipboard } from \"../clipboard.js\";\nimport { calculateContextTokens, compact, shouldCompact } from \"../compaction.js\";\nimport { APP_NAME, getDebugLogPath, getModelsPath, getOAuthPath } from \"../config.js\";\nimport { exportSessionToHtml } from \"../export-html.js\";\nimport { getApiKeyForModel, getAvailableModels, invalidateOAuthCache } from \"../model-config.js\";\nimport { listOAuthProviders, login, logout, type SupportedOAuthProvider } from \"../oauth/index.js\";\nimport {\n\tgetLatestCompactionEntry,\n\tloadSessionFromEntries,\n\ttype SessionManager,\n\tSUMMARY_PREFIX,\n\tSUMMARY_SUFFIX,\n} from \"../session-manager.js\";\nimport type { SettingsManager } from \"../settings-manager.js\";\nimport { expandSlashCommand, type FileSlashCommand, loadSlashCommands } from \"../slash-commands.js\";\nimport { getEditorTheme, getMarkdownTheme, onThemeChange, setTheme, theme } from \"../theme/theme.js\";\nimport { AssistantMessageComponent } from \"./assistant-message.js\";\nimport { CompactionComponent } from \"./compaction.js\";\nimport { CustomEditor } from \"./custom-editor.js\";\nimport { DynamicBorder } from \"./dynamic-border.js\";\nimport { FooterComponent } from \"./footer.js\";\nimport { ModelSelectorComponent } from \"./model-selector.js\";\nimport { OAuthSelectorComponent } from \"./oauth-selector.js\";\nimport { QueueModeSelectorComponent } from \"./queue-mode-selector.js\";\nimport { ThemeSelectorComponent } from \"./theme-selector.js\";\nimport { ThinkingSelectorComponent } from \"./thinking-selector.js\";\nimport { ToolExecutionComponent } from \"./tool-execution.js\";\nimport { UserMessageComponent } from \"./user-message.js\";\nimport { UserMessageSelectorComponent } from \"./user-message-selector.js\";\n\n/**\n * TUI renderer for the coding agent\n */\nexport class TuiRenderer {\n\tprivate ui: TUI;\n\tprivate chatContainer: Container;\n\tprivate pendingMessagesContainer: Container;\n\tprivate statusContainer: Container;\n\tprivate editor: CustomEditor;\n\tprivate editorContainer: Container; // Container to swap between editor and selector\n\tprivate footer: FooterComponent;\n\tprivate agent: Agent;\n\tprivate sessionManager: SessionManager;\n\tprivate settingsManager: SettingsManager;\n\tprivate version: string;\n\tprivate isInitialized = false;\n\tprivate onInputCallback?: (text: string) => void;\n\tprivate loadingAnimation: Loader | null = null;\n\n\tprivate lastSigintTime = 0;\n\tprivate changelogMarkdown: string | null = null;\n\tprivate newVersion: string | null = null;\n\n\t// Message queueing\n\tprivate queuedMessages: string[] = [];\n\n\t// Streaming message tracking\n\tprivate streamingComponent: AssistantMessageComponent | null = null;\n\n\t// Tool execution tracking: toolCallId -> component\n\tprivate pendingTools = new Map<string, ToolExecutionComponent>();\n\n\t// Thinking level selector\n\tprivate thinkingSelector: ThinkingSelectorComponent | null = null;\n\n\t// Queue mode selector\n\tprivate queueModeSelector: QueueModeSelectorComponent | null = null;\n\n\t// Theme selector\n\tprivate themeSelector: ThemeSelectorComponent | null = null;\n\n\t// Model selector\n\tprivate modelSelector: ModelSelectorComponent | null = null;\n\n\t// User message selector (for branching)\n\tprivate userMessageSelector: UserMessageSelectorComponent | null = null;\n\n\t// OAuth selector\n\tprivate oauthSelector: any | null = null;\n\n\t// Track if this is the first user message (to skip spacer)\n\tprivate isFirstUserMessage = true;\n\n\t// Model scope for quick cycling\n\tprivate scopedModels: Array<{ model: Model<any>; thinkingLevel: ThinkingLevel }> = [];\n\n\t// Tool output expansion state\n\tprivate toolOutputExpanded = false;\n\n\t// Thinking block visibility state\n\tprivate hideThinkingBlock = false;\n\n\t// Agent subscription unsubscribe function\n\tprivate unsubscribe?: () => void;\n\n\t// File-based slash commands\n\tprivate fileCommands: FileSlashCommand[] = [];\n\n\tconstructor(\n\t\tagent: Agent,\n\t\tsessionManager: SessionManager,\n\t\tsettingsManager: SettingsManager,\n\t\tversion: string,\n\t\tchangelogMarkdown: string | null = null,\n\t\tnewVersion: string | null = null,\n\t\tscopedModels: Array<{ model: Model<any>; thinkingLevel: ThinkingLevel }> = [],\n\t\tfdPath: string | null = null,\n\t) {\n\t\tthis.agent = agent;\n\t\tthis.sessionManager = sessionManager;\n\t\tthis.settingsManager = settingsManager;\n\t\tthis.version = version;\n\t\tthis.newVersion = newVersion;\n\t\tthis.changelogMarkdown = changelogMarkdown;\n\t\tthis.scopedModels = scopedModels;\n\t\tthis.ui = new TUI(new ProcessTerminal());\n\t\tthis.chatContainer = new Container();\n\t\tthis.pendingMessagesContainer = new Container();\n\t\tthis.statusContainer = new Container();\n\t\tthis.editor = new CustomEditor(getEditorTheme());\n\t\tthis.editorContainer = new Container(); // Container to hold editor or selector\n\t\tthis.editorContainer.addChild(this.editor); // Start with editor\n\t\tthis.footer = new FooterComponent(agent.state);\n\t\tthis.footer.setAutoCompactEnabled(this.settingsManager.getCompactionEnabled());\n\n\t\t// Define slash commands\n\t\tconst thinkingCommand: SlashCommand = {\n\t\t\tname: \"thinking\",\n\t\t\tdescription: \"Select reasoning level (opens selector UI)\",\n\t\t};\n\n\t\tconst modelCommand: SlashCommand = {\n\t\t\tname: \"model\",\n\t\t\tdescription: \"Select model (opens selector UI)\",\n\t\t};\n\n\t\tconst exportCommand: SlashCommand = {\n\t\t\tname: \"export\",\n\t\t\tdescription: \"Export session to HTML file\",\n\t\t};\n\n\t\tconst copyCommand: SlashCommand = {\n\t\t\tname: \"copy\",\n\t\t\tdescription: \"Copy last agent message to clipboard\",\n\t\t};\n\n\t\tconst sessionCommand: SlashCommand = {\n\t\t\tname: \"session\",\n\t\t\tdescription: \"Show session info and stats\",\n\t\t};\n\n\t\tconst changelogCommand: SlashCommand = {\n\t\t\tname: \"changelog\",\n\t\t\tdescription: \"Show changelog entries\",\n\t\t};\n\n\t\tconst branchCommand: SlashCommand = {\n\t\t\tname: \"branch\",\n\t\t\tdescription: \"Create a new branch from a previous message\",\n\t\t};\n\n\t\tconst loginCommand: SlashCommand = {\n\t\t\tname: \"login\",\n\t\t\tdescription: \"Login with OAuth provider\",\n\t\t};\n\n\t\tconst logoutCommand: SlashCommand = {\n\t\t\tname: \"logout\",\n\t\t\tdescription: \"Logout from OAuth provider\",\n\t\t};\n\n\t\tconst queueCommand: SlashCommand = {\n\t\t\tname: \"queue\",\n\t\t\tdescription: \"Select message queue mode (opens selector UI)\",\n\t\t};\n\n\t\tconst themeCommand: SlashCommand = {\n\t\t\tname: \"theme\",\n\t\t\tdescription: \"Select color theme (opens selector UI)\",\n\t\t};\n\n\t\tconst clearCommand: SlashCommand = {\n\t\t\tname: \"clear\",\n\t\t\tdescription: \"Clear context and start a fresh session\",\n\t\t};\n\n\t\tconst compactCommand: SlashCommand = {\n\t\t\tname: \"compact\",\n\t\t\tdescription: \"Manually compact the session context\",\n\t\t};\n\n\t\tconst autocompactCommand: SlashCommand = {\n\t\t\tname: \"autocompact\",\n\t\t\tdescription: \"Toggle automatic context compaction\",\n\t\t};\n\n\t\t// Load hide thinking block setting\n\t\tthis.hideThinkingBlock = settingsManager.getHideThinkingBlock();\n\n\t\t// Load file-based slash commands\n\t\tthis.fileCommands = loadSlashCommands();\n\n\t\t// Convert file commands to SlashCommand format\n\t\tconst fileSlashCommands: SlashCommand[] = this.fileCommands.map((cmd) => ({\n\t\t\tname: cmd.name,\n\t\t\tdescription: cmd.description,\n\t\t}));\n\n\t\t// Setup autocomplete for file paths and slash commands\n\t\tconst autocompleteProvider = new CombinedAutocompleteProvider(\n\t\t\t[\n\t\t\t\tthinkingCommand,\n\t\t\t\tmodelCommand,\n\t\t\t\tthemeCommand,\n\t\t\t\texportCommand,\n\t\t\t\tcopyCommand,\n\t\t\t\tsessionCommand,\n\t\t\t\tchangelogCommand,\n\t\t\t\tbranchCommand,\n\t\t\t\tloginCommand,\n\t\t\t\tlogoutCommand,\n\t\t\t\tqueueCommand,\n\t\t\t\tclearCommand,\n\t\t\t\tcompactCommand,\n\t\t\t\tautocompactCommand,\n\t\t\t\t...fileSlashCommands,\n\t\t\t],\n\t\t\tprocess.cwd(),\n\t\t\tfdPath,\n\t\t);\n\t\tthis.editor.setAutocompleteProvider(autocompleteProvider);\n\t}\n\n\tasync init(): Promise<void> {\n\t\tif (this.isInitialized) return;\n\n\t\t// Add header with logo and instructions\n\t\tconst logo = theme.bold(theme.fg(\"accent\", APP_NAME)) + theme.fg(\"dim\", ` v${this.version}`);\n\t\tconst instructions =\n\t\t\ttheme.fg(\"dim\", \"esc\") +\n\t\t\ttheme.fg(\"muted\", \" to interrupt\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+c\") +\n\t\t\ttheme.fg(\"muted\", \" to clear\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+c twice\") +\n\t\t\ttheme.fg(\"muted\", \" to exit\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+k\") +\n\t\t\ttheme.fg(\"muted\", \" to delete line\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"shift+tab\") +\n\t\t\ttheme.fg(\"muted\", \" to cycle thinking\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+p\") +\n\t\t\ttheme.fg(\"muted\", \" to cycle models\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+o\") +\n\t\t\ttheme.fg(\"muted\", \" to expand tools\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+t\") +\n\t\t\ttheme.fg(\"muted\", \" to toggle thinking\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"/\") +\n\t\t\ttheme.fg(\"muted\", \" for commands\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"drop files\") +\n\t\t\ttheme.fg(\"muted\", \" to attach\");\n\t\tconst header = new Text(logo + \"\\n\" + instructions, 1, 0);\n\n\t\t// Setup UI layout\n\t\tthis.ui.addChild(new Spacer(1));\n\t\tthis.ui.addChild(header);\n\t\tthis.ui.addChild(new Spacer(1));\n\n\t\t// Add new version notification if available\n\t\tif (this.newVersion) {\n\t\t\tthis.ui.addChild(new DynamicBorder((text) => theme.fg(\"warning\", text)));\n\t\t\tthis.ui.addChild(\n\t\t\t\tnew Text(\n\t\t\t\t\ttheme.bold(theme.fg(\"warning\", \"Update Available\")) +\n\t\t\t\t\t\t\"\\n\" +\n\t\t\t\t\t\ttheme.fg(\"muted\", `New version ${this.newVersion} is available. Run: `) +\n\t\t\t\t\t\ttheme.fg(\"accent\", \"npm install -g @mariozechner/pi-coding-agent\"),\n\t\t\t\t\t1,\n\t\t\t\t\t0,\n\t\t\t\t),\n\t\t\t);\n\t\t\tthis.ui.addChild(new DynamicBorder((text) => theme.fg(\"warning\", text)));\n\t\t}\n\n\t\t// Add changelog if provided\n\t\tif (this.changelogMarkdown) {\n\t\t\tthis.ui.addChild(new DynamicBorder());\n\t\t\tthis.ui.addChild(new Text(theme.bold(theme.fg(\"accent\", \"What's New\")), 1, 0));\n\t\t\tthis.ui.addChild(new Spacer(1));\n\t\t\tthis.ui.addChild(new Markdown(this.changelogMarkdown.trim(), 1, 0, getMarkdownTheme()));\n\t\t\tthis.ui.addChild(new Spacer(1));\n\t\t\tthis.ui.addChild(new DynamicBorder());\n\t\t}\n\n\t\tthis.ui.addChild(this.chatContainer);\n\t\tthis.ui.addChild(this.pendingMessagesContainer);\n\t\tthis.ui.addChild(this.statusContainer);\n\t\tthis.ui.addChild(new Spacer(1));\n\t\tthis.ui.addChild(this.editorContainer); // Use container that can hold editor or selector\n\t\tthis.ui.addChild(this.footer);\n\t\tthis.ui.setFocus(this.editor);\n\n\t\t// Set up custom key handlers on the editor\n\t\tthis.editor.onEscape = () => {\n\t\t\t// Intercept Escape key when processing\n\t\t\tif (this.loadingAnimation) {\n\t\t\t\t// Get all queued messages\n\t\t\t\tconst queuedText = this.queuedMessages.join(\"\\n\\n\");\n\n\t\t\t\t// Get current editor text\n\t\t\t\tconst currentText = this.editor.getText();\n\n\t\t\t\t// Combine: queued messages + current editor text\n\t\t\t\tconst combinedText = [queuedText, currentText].filter((t) => t.trim()).join(\"\\n\\n\");\n\n\t\t\t\t// Put back in editor\n\t\t\t\tthis.editor.setText(combinedText);\n\n\t\t\t\t// Clear queued messages\n\t\t\t\tthis.queuedMessages = [];\n\t\t\t\tthis.updatePendingMessagesDisplay();\n\n\t\t\t\t// Clear agent's queue too\n\t\t\t\tthis.agent.clearMessageQueue();\n\n\t\t\t\t// Abort\n\t\t\t\tthis.agent.abort();\n\t\t\t}\n\t\t};\n\n\t\tthis.editor.onCtrlC = () => {\n\t\t\tthis.handleCtrlC();\n\t\t};\n\n\t\tthis.editor.onShiftTab = () => {\n\t\t\tthis.cycleThinkingLevel();\n\t\t};\n\n\t\tthis.editor.onCtrlP = () => {\n\t\t\tthis.cycleModel();\n\t\t};\n\n\t\tthis.editor.onCtrlO = () => {\n\t\t\tthis.toggleToolOutputExpansion();\n\t\t};\n\n\t\tthis.editor.onCtrlT = () => {\n\t\t\tthis.toggleThinkingBlockVisibility();\n\t\t};\n\n\t\t// Handle editor submission\n\t\tthis.editor.onSubmit = async (text: string) => {\n\t\t\ttext = text.trim();\n\t\t\tif (!text) return;\n\n\t\t\t// Check for /thinking command\n\t\t\tif (text === \"/thinking\") {\n\t\t\t\t// Show thinking level selector\n\t\t\t\tthis.showThinkingSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /model command\n\t\t\tif (text === \"/model\") {\n\t\t\t\t// Show model selector\n\t\t\t\tthis.showModelSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /export command\n\t\t\tif (text.startsWith(\"/export\")) {\n\t\t\t\tthis.handleExportCommand(text);\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /copy command\n\t\t\tif (text === \"/copy\") {\n\t\t\t\tthis.handleCopyCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /session command\n\t\t\tif (text === \"/session\") {\n\t\t\t\tthis.handleSessionCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /changelog command\n\t\t\tif (text === \"/changelog\") {\n\t\t\t\tthis.handleChangelogCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /branch command\n\t\t\tif (text === \"/branch\") {\n\t\t\t\tthis.showUserMessageSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /login command\n\t\t\tif (text === \"/login\") {\n\t\t\t\tthis.showOAuthSelector(\"login\");\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /logout command\n\t\t\tif (text === \"/logout\") {\n\t\t\t\tthis.showOAuthSelector(\"logout\");\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /queue command\n\t\t\tif (text === \"/queue\") {\n\t\t\t\tthis.showQueueModeSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /theme command\n\t\t\tif (text === \"/theme\") {\n\t\t\t\tthis.showThemeSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /clear command\n\t\t\tif (text === \"/clear\") {\n\t\t\t\tthis.handleClearCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /compact command\n\t\t\tif (text === \"/compact\" || text.startsWith(\"/compact \")) {\n\t\t\t\tconst customInstructions = text.startsWith(\"/compact \") ? text.slice(9).trim() : undefined;\n\t\t\t\tthis.handleCompactCommand(customInstructions);\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /autocompact command\n\t\t\tif (text === \"/autocompact\") {\n\t\t\t\tthis.handleAutocompactCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /debug command\n\t\t\tif (text === \"/debug\") {\n\t\t\t\tthis.handleDebugCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for file-based slash commands\n\t\t\ttext = expandSlashCommand(text, this.fileCommands);\n\n\t\t\t// Normal message submission - validate model and API key first\n\t\t\tconst currentModel = this.agent.state.model;\n\t\t\tif (!currentModel) {\n\t\t\t\tthis.showError(\n\t\t\t\t\t\"No model selected.\\n\\n\" +\n\t\t\t\t\t\t\"Set an API key (ANTHROPIC_API_KEY, OPENAI_API_KEY, etc.)\\n\" +\n\t\t\t\t\t\t`or create ${getModelsPath()}\\n\\n` +\n\t\t\t\t\t\t\"Then use /model to select a model.\",\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Validate API key (async)\n\t\t\tconst apiKey = await getApiKeyForModel(currentModel);\n\t\t\tif (!apiKey) {\n\t\t\t\tthis.showError(\n\t\t\t\t\t`No API key found for ${currentModel.provider}.\\n\\n` +\n\t\t\t\t\t\t`Set the appropriate environment variable or update ${getModelsPath()}`,\n\t\t\t\t);\n\t\t\t\tthis.editor.setText(text);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check if agent is currently streaming\n\t\t\tif (this.agent.state.isStreaming) {\n\t\t\t\t// Queue the message instead of submitting\n\t\t\t\tthis.queuedMessages.push(text);\n\n\t\t\t\t// Queue in agent\n\t\t\t\tawait this.agent.queueMessage({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tcontent: [{ type: \"text\", text }],\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t});\n\n\t\t\t\t// Update pending messages display\n\t\t\t\tthis.updatePendingMessagesDisplay();\n\n\t\t\t\t// Clear editor\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// All good, proceed with submission\n\t\t\tif (this.onInputCallback) {\n\t\t\t\tthis.onInputCallback(text);\n\t\t\t}\n\t\t};\n\n\t\t// Start the UI\n\t\tthis.ui.start();\n\t\tthis.isInitialized = true;\n\n\t\t// Subscribe to agent events for UI updates and session saving\n\t\tthis.subscribeToAgent();\n\n\t\t// Set up theme file watcher for live reload\n\t\tonThemeChange(() => {\n\t\t\tthis.ui.invalidate();\n\t\t\tthis.updateEditorBorderColor();\n\t\t\tthis.ui.requestRender();\n\t\t});\n\n\t\t// Set up git branch watcher\n\t\tthis.footer.watchBranch(() => {\n\t\t\tthis.ui.requestRender();\n\t\t});\n\t}\n\n\tprivate subscribeToAgent(): void {\n\t\tthis.unsubscribe = this.agent.subscribe(async (event) => {\n\t\t\t// Handle UI updates\n\t\t\tawait this.handleEvent(event, this.agent.state);\n\n\t\t\t// Save messages to session\n\t\t\tif (event.type === \"message_end\") {\n\t\t\t\tthis.sessionManager.saveMessage(event.message);\n\n\t\t\t\t// Check if we should initialize session now (after first user+assistant exchange)\n\t\t\t\tif (this.sessionManager.shouldInitializeSession(this.agent.state.messages)) {\n\t\t\t\t\tthis.sessionManager.startSession(this.agent.state);\n\t\t\t\t}\n\n\t\t\t\t// Check for auto-compaction after assistant messages\n\t\t\t\tif (event.message.role === \"assistant\") {\n\t\t\t\t\tawait this.checkAutoCompaction();\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate async checkAutoCompaction(): Promise<void> {\n\t\tconst settings = this.settingsManager.getCompactionSettings();\n\t\tif (!settings.enabled) return;\n\n\t\t// Get last non-aborted assistant message from agent state\n\t\tconst messages = this.agent.state.messages;\n\t\tlet lastAssistant: AssistantMessage | null = null;\n\t\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\t\tconst msg = messages[i];\n\t\t\tif (msg.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\t\tif (assistantMsg.stopReason !== \"aborted\") {\n\t\t\t\t\tlastAssistant = assistantMsg;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (!lastAssistant) return;\n\n\t\tconst contextTokens = calculateContextTokens(lastAssistant.usage);\n\t\tconst contextWindow = this.agent.state.model.contextWindow;\n\n\t\tif (!shouldCompact(contextTokens, contextWindow, settings)) return;\n\n\t\t// Trigger auto-compaction\n\t\tawait this.executeCompaction(undefined, true);\n\t}\n\n\tprivate async handleEvent(event: AgentEvent, state: AgentState): Promise<void> {\n\t\tif (!this.isInitialized) {\n\t\t\tawait this.init();\n\t\t}\n\n\t\t// Update footer with current stats\n\t\tthis.footer.updateState(state);\n\n\t\tswitch (event.type) {\n\t\t\tcase \"agent_start\":\n\t\t\t\t// Show loading animation\n\t\t\t\t// Note: Don't disable submit - we handle queuing in onSubmit callback\n\t\t\t\t// Stop old loader before clearing\n\t\t\t\tif (this.loadingAnimation) {\n\t\t\t\t\tthis.loadingAnimation.stop();\n\t\t\t\t}\n\t\t\t\tthis.statusContainer.clear();\n\t\t\t\tthis.loadingAnimation = new Loader(\n\t\t\t\t\tthis.ui,\n\t\t\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\t\t\"Working... (esc to interrupt)\",\n\t\t\t\t);\n\t\t\t\tthis.statusContainer.addChild(this.loadingAnimation);\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\tbreak;\n\n\t\t\tcase \"message_start\":\n\t\t\t\tif (event.message.role === \"user\") {\n\t\t\t\t\t// Check if this is a queued message\n\t\t\t\t\tconst userMsg = event.message;\n\t\t\t\t\tconst textBlocks =\n\t\t\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\t\t\tconst messageText = textBlocks.map((c) => c.text).join(\"\");\n\n\t\t\t\t\tconst queuedIndex = this.queuedMessages.indexOf(messageText);\n\t\t\t\t\tif (queuedIndex !== -1) {\n\t\t\t\t\t\t// Remove from queued messages\n\t\t\t\t\t\tthis.queuedMessages.splice(queuedIndex, 1);\n\t\t\t\t\t\tthis.updatePendingMessagesDisplay();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Show user message immediately and clear editor\n\t\t\t\t\tthis.addMessageToChat(event.message);\n\t\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t} else if (event.message.role === \"assistant\") {\n\t\t\t\t\t// Create assistant component for streaming\n\t\t\t\t\tthis.streamingComponent = new AssistantMessageComponent(undefined, this.hideThinkingBlock);\n\t\t\t\t\tthis.chatContainer.addChild(this.streamingComponent);\n\t\t\t\t\tthis.streamingComponent.updateContent(event.message as AssistantMessage);\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\tcase \"message_update\":\n\t\t\t\t// Update streaming component\n\t\t\t\tif (this.streamingComponent && event.message.role === \"assistant\") {\n\t\t\t\t\tconst assistantMsg = event.message as AssistantMessage;\n\t\t\t\t\tthis.streamingComponent.updateContent(assistantMsg);\n\n\t\t\t\t\t// Create tool execution components as soon as we see tool calls\n\t\t\t\t\tfor (const content of assistantMsg.content) {\n\t\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\t\t// Only create if we haven't created it yet\n\t\t\t\t\t\t\tif (!this.pendingTools.has(content.id)) {\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Text(\"\", 0, 0));\n\t\t\t\t\t\t\t\tconst component = new ToolExecutionComponent(content.name, content.arguments);\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t\t\t\tthis.pendingTools.set(content.id, component);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Update existing component with latest arguments as they stream\n\t\t\t\t\t\t\t\tconst component = this.pendingTools.get(content.id);\n\t\t\t\t\t\t\t\tif (component) {\n\t\t\t\t\t\t\t\t\tcomponent.updateArgs(content.arguments);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\tcase \"message_end\":\n\t\t\t\t// Skip user messages (already shown in message_start)\n\t\t\t\tif (event.message.role === \"user\") {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (this.streamingComponent && event.message.role === \"assistant\") {\n\t\t\t\t\tconst assistantMsg = event.message as AssistantMessage;\n\n\t\t\t\t\t// Update streaming component with final message (includes stopReason)\n\t\t\t\t\tthis.streamingComponent.updateContent(assistantMsg);\n\n\t\t\t\t\t// If message was aborted or errored, mark all pending tool components as failed\n\t\t\t\t\tif (assistantMsg.stopReason === \"aborted\" || assistantMsg.stopReason === \"error\") {\n\t\t\t\t\t\tconst errorMessage =\n\t\t\t\t\t\t\tassistantMsg.stopReason === \"aborted\" ? \"Operation aborted\" : assistantMsg.errorMessage || \"Error\";\n\t\t\t\t\t\tfor (const [toolCallId, component] of this.pendingTools.entries()) {\n\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: errorMessage }],\n\t\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.pendingTools.clear();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Keep the streaming component - it's now the final assistant message\n\t\t\t\t\tthis.streamingComponent = null;\n\n\t\t\t\t\t// Invalidate footer cache to refresh git branch (in case agent executed git commands)\n\t\t\t\t\tthis.footer.invalidate();\n\t\t\t\t}\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\tbreak;\n\n\t\t\tcase \"tool_execution_start\": {\n\t\t\t\t// Component should already exist from message_update, but create if missing\n\t\t\t\tif (!this.pendingTools.has(event.toolCallId)) {\n\t\t\t\t\tconst component = new ToolExecutionComponent(event.toolName, event.args);\n\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\tthis.pendingTools.set(event.toolCallId, component);\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase \"tool_execution_end\": {\n\t\t\t\t// Update the existing tool component with the result\n\t\t\t\tconst component = this.pendingTools.get(event.toolCallId);\n\t\t\t\tif (component) {\n\t\t\t\t\t// Convert result to the format expected by updateResult\n\t\t\t\t\tconst resultData =\n\t\t\t\t\t\ttypeof event.result === \"string\"\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: event.result }],\n\t\t\t\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t\t\t\t\tisError: event.isError,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\tcontent: event.result.content,\n\t\t\t\t\t\t\t\t\tdetails: event.result.details,\n\t\t\t\t\t\t\t\t\tisError: event.isError,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\tcomponent.updateResult(resultData);\n\t\t\t\t\tthis.pendingTools.delete(event.toolCallId);\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase \"agent_end\":\n\t\t\t\t// Stop loading animation\n\t\t\t\tif (this.loadingAnimation) {\n\t\t\t\t\tthis.loadingAnimation.stop();\n\t\t\t\t\tthis.loadingAnimation = null;\n\t\t\t\t\tthis.statusContainer.clear();\n\t\t\t\t}\n\t\t\t\tif (this.streamingComponent) {\n\t\t\t\t\tthis.chatContainer.removeChild(this.streamingComponent);\n\t\t\t\t\tthis.streamingComponent = null;\n\t\t\t\t}\n\t\t\t\tthis.pendingTools.clear();\n\t\t\t\t// Note: Don't need to re-enable submit - we never disable it\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\tprivate addMessageToChat(message: Message): void {\n\t\tif (message.role === \"user\") {\n\t\t\tconst userMsg = message;\n\t\t\t// Extract text content from content blocks\n\t\t\tconst textBlocks =\n\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\tif (textContent) {\n\t\t\t\tconst userComponent = new UserMessageComponent(textContent, this.isFirstUserMessage);\n\t\t\t\tthis.chatContainer.addChild(userComponent);\n\t\t\t\tthis.isFirstUserMessage = false;\n\t\t\t}\n\t\t} else if (message.role === \"assistant\") {\n\t\t\tconst assistantMsg = message;\n\n\t\t\t// Add assistant message component\n\t\t\tconst assistantComponent = new AssistantMessageComponent(assistantMsg, this.hideThinkingBlock);\n\t\t\tthis.chatContainer.addChild(assistantComponent);\n\t\t}\n\t\t// Note: tool calls and results are now handled via tool_execution_start/end events\n\t}\n\n\trenderInitialMessages(state: AgentState): void {\n\t\t// Render all existing messages (for --continue mode)\n\t\t// Reset first user message flag for initial render\n\t\tthis.isFirstUserMessage = true;\n\n\t\t// Update footer with loaded state\n\t\tthis.footer.updateState(state);\n\n\t\t// Update editor border color based on current thinking level\n\t\tthis.updateEditorBorderColor();\n\n\t\t// Get compaction info if any\n\t\tconst compactionEntry = getLatestCompactionEntry(this.sessionManager.loadEntries());\n\n\t\t// Render messages\n\t\tfor (let i = 0; i < state.messages.length; i++) {\n\t\t\tconst message = state.messages[i];\n\n\t\t\tif (message.role === \"user\") {\n\t\t\t\tconst userMsg = message;\n\t\t\t\tconst textBlocks =\n\t\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\t\tif (textContent) {\n\t\t\t\t\t// Check if this is a compaction summary message\n\t\t\t\t\tif (textContent.startsWith(SUMMARY_PREFIX) && compactionEntry) {\n\t\t\t\t\t\tconst summary = textContent.slice(SUMMARY_PREFIX.length, -SUMMARY_SUFFIX.length);\n\t\t\t\t\t\tconst component = new CompactionComponent(compactionEntry.tokensBefore, summary);\n\t\t\t\t\t\tcomponent.setExpanded(this.toolOutputExpanded);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconst userComponent = new UserMessageComponent(textContent, this.isFirstUserMessage);\n\t\t\t\t\t\tthis.chatContainer.addChild(userComponent);\n\t\t\t\t\t\tthis.isFirstUserMessage = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\tconst assistantComponent = new AssistantMessageComponent(assistantMsg, this.hideThinkingBlock);\n\t\t\t\tthis.chatContainer.addChild(assistantComponent);\n\n\t\t\t\t// Create tool execution components for any tool calls\n\t\t\t\tfor (const content of assistantMsg.content) {\n\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\tconst component = new ToolExecutionComponent(content.name, content.arguments);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\n\t\t\t\t\t\t// If message was aborted/errored, immediately mark tool as failed\n\t\t\t\t\t\tif (assistantMsg.stopReason === \"aborted\" || assistantMsg.stopReason === \"error\") {\n\t\t\t\t\t\t\tconst errorMessage =\n\t\t\t\t\t\t\t\tassistantMsg.stopReason === \"aborted\"\n\t\t\t\t\t\t\t\t\t? \"Operation aborted\"\n\t\t\t\t\t\t\t\t\t: assistantMsg.errorMessage || \"Error\";\n\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: errorMessage }],\n\t\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Store in map so we can update with results later\n\t\t\t\t\t\t\tthis.pendingTools.set(content.id, component);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"toolResult\") {\n\t\t\t\t// Update existing tool execution component with results\t\t\t\t;\n\t\t\t\tconst component = this.pendingTools.get(message.toolCallId);\n\t\t\t\tif (component) {\n\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\tcontent: message.content,\n\t\t\t\t\t\tdetails: message.details,\n\t\t\t\t\t\tisError: message.isError,\n\t\t\t\t\t});\n\t\t\t\t\t// Remove from pending map since it's complete\n\t\t\t\t\tthis.pendingTools.delete(message.toolCallId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// Clear pending tools after rendering initial messages\n\t\tthis.pendingTools.clear();\n\t\tthis.ui.requestRender();\n\t}\n\n\tasync getUserInput(): Promise<string> {\n\t\treturn new Promise((resolve) => {\n\t\t\tthis.onInputCallback = (text: string) => {\n\t\t\t\tthis.onInputCallback = undefined;\n\t\t\t\tresolve(text);\n\t\t\t};\n\t\t});\n\t}\n\n\tprivate rebuildChatFromMessages(): void {\n\t\t// Reset state and re-render messages from agent state\n\t\tthis.isFirstUserMessage = true;\n\t\tthis.pendingTools.clear();\n\n\t\t// Get compaction info if any\n\t\tconst compactionEntry = getLatestCompactionEntry(this.sessionManager.loadEntries());\n\n\t\tfor (const message of this.agent.state.messages) {\n\t\t\tif (message.role === \"user\") {\n\t\t\t\tconst userMsg = message;\n\t\t\t\tconst textBlocks =\n\t\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\t\tif (textContent) {\n\t\t\t\t\t// Check if this is a compaction summary message\n\t\t\t\t\tif (textContent.startsWith(SUMMARY_PREFIX) && compactionEntry) {\n\t\t\t\t\t\tconst summary = textContent.slice(SUMMARY_PREFIX.length, -SUMMARY_SUFFIX.length);\n\t\t\t\t\t\tconst component = new CompactionComponent(compactionEntry.tokensBefore, summary);\n\t\t\t\t\t\tcomponent.setExpanded(this.toolOutputExpanded);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconst userComponent = new UserMessageComponent(textContent, this.isFirstUserMessage);\n\t\t\t\t\t\tthis.chatContainer.addChild(userComponent);\n\t\t\t\t\t\tthis.isFirstUserMessage = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message;\n\t\t\t\tconst assistantComponent = new AssistantMessageComponent(assistantMsg, this.hideThinkingBlock);\n\t\t\t\tthis.chatContainer.addChild(assistantComponent);\n\n\t\t\t\tfor (const content of assistantMsg.content) {\n\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\tconst component = new ToolExecutionComponent(content.name, content.arguments);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t\tthis.pendingTools.set(content.id, component);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"toolResult\") {\n\t\t\t\tconst component = this.pendingTools.get(message.toolCallId);\n\t\t\t\tif (component) {\n\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\tcontent: message.content,\n\t\t\t\t\t\tdetails: message.details,\n\t\t\t\t\t\tisError: message.isError,\n\t\t\t\t\t});\n\t\t\t\t\tthis.pendingTools.delete(message.toolCallId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis.pendingTools.clear();\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleCtrlC(): void {\n\t\t// Handle Ctrl+C double-press logic\n\t\tconst now = Date.now();\n\t\tconst timeSinceLastCtrlC = now - this.lastSigintTime;\n\n\t\tif (timeSinceLastCtrlC < 500) {\n\t\t\t// Second Ctrl+C within 500ms - exit\n\t\t\tthis.stop();\n\t\t\tprocess.exit(0);\n\t\t} else {\n\t\t\t// First Ctrl+C - clear the editor\n\t\t\tthis.clearEditor();\n\t\t\tthis.lastSigintTime = now;\n\t\t}\n\t}\n\n\tprivate updateEditorBorderColor(): void {\n\t\tconst level = this.agent.state.thinkingLevel || \"off\";\n\t\tthis.editor.borderColor = theme.getThinkingBorderColor(level);\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate cycleThinkingLevel(): void {\n\t\t// Only cycle if model supports thinking\n\t\tif (!this.agent.state.model?.reasoning) {\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Current model does not support thinking\"), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\tconst levels: ThinkingLevel[] = [\"off\", \"minimal\", \"low\", \"medium\", \"high\"];\n\t\tconst currentLevel = this.agent.state.thinkingLevel || \"off\";\n\t\tconst currentIndex = levels.indexOf(currentLevel);\n\t\tconst nextIndex = (currentIndex + 1) % levels.length;\n\t\tconst nextLevel = levels[nextIndex];\n\n\t\t// Apply the new thinking level\n\t\tthis.agent.setThinkingLevel(nextLevel);\n\n\t\t// Save thinking level change to session and settings\n\t\tthis.sessionManager.saveThinkingLevelChange(nextLevel);\n\t\tthis.settingsManager.setDefaultThinkingLevel(nextLevel);\n\n\t\t// Update border color\n\t\tthis.updateEditorBorderColor();\n\n\t\t// Show brief notification\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Thinking level: ${nextLevel}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate async cycleModel(): Promise<void> {\n\t\t// Use scoped models if available, otherwise all available models\n\t\tif (this.scopedModels.length > 0) {\n\t\t\t// Use scoped models with thinking levels\n\t\t\tif (this.scopedModels.length === 1) {\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Only one model in scope\"), 1, 0));\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst currentModel = this.agent.state.model;\n\t\t\tlet currentIndex = this.scopedModels.findIndex(\n\t\t\t\t(sm) => sm.model.id === currentModel?.id && sm.model.provider === currentModel?.provider,\n\t\t\t);\n\n\t\t\t// If current model not in scope, start from first\n\t\t\tif (currentIndex === -1) {\n\t\t\t\tcurrentIndex = 0;\n\t\t\t}\n\n\t\t\tconst nextIndex = (currentIndex + 1) % this.scopedModels.length;\n\t\t\tconst nextEntry = this.scopedModels[nextIndex];\n\t\t\tconst nextModel = nextEntry.model;\n\t\t\tconst nextThinking = nextEntry.thinkingLevel;\n\n\t\t\t// Validate API key\n\t\t\tconst apiKey = await getApiKeyForModel(nextModel);\n\t\t\tif (!apiKey) {\n\t\t\t\tthis.showError(`No API key for ${nextModel.provider}/${nextModel.id}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Switch model\n\t\t\tthis.agent.setModel(nextModel);\n\n\t\t\t// Save model change to session and settings\n\t\t\tthis.sessionManager.saveModelChange(nextModel.provider, nextModel.id);\n\t\t\tthis.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);\n\n\t\t\t// Apply thinking level (silently use \"off\" if model doesn't support thinking)\n\t\t\tconst effectiveThinking = nextModel.reasoning ? nextThinking : \"off\";\n\t\t\tthis.agent.setThinkingLevel(effectiveThinking);\n\t\t\tthis.sessionManager.saveThinkingLevelChange(effectiveThinking);\n\t\t\tthis.settingsManager.setDefaultThinkingLevel(effectiveThinking);\n\t\t\tthis.updateEditorBorderColor();\n\n\t\t\t// Show notification\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tconst thinkingStr = nextModel.reasoning && nextThinking !== \"off\" ? ` (thinking: ${nextThinking})` : \"\";\n\t\t\tthis.chatContainer.addChild(\n\t\t\t\tnew Text(theme.fg(\"dim\", `Switched to ${nextModel.name || nextModel.id}${thinkingStr}`), 1, 0),\n\t\t\t);\n\t\t\tthis.ui.requestRender();\n\t\t} else {\n\t\t\t// Fallback to all available models (no thinking level changes)\n\t\t\tconst { models: availableModels, error } = await getAvailableModels();\n\t\t\tif (error) {\n\t\t\t\tthis.showError(`Failed to load models: ${error}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (availableModels.length === 0) {\n\t\t\t\tthis.showError(\"No models available to cycle\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (availableModels.length === 1) {\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Only one model available\"), 1, 0));\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst currentModel = this.agent.state.model;\n\t\t\tlet currentIndex = availableModels.findIndex(\n\t\t\t\t(m) => m.id === currentModel?.id && m.provider === currentModel?.provider,\n\t\t\t);\n\n\t\t\t// If current model not in scope, start from first\n\t\t\tif (currentIndex === -1) {\n\t\t\t\tcurrentIndex = 0;\n\t\t\t}\n\n\t\t\tconst nextIndex = (currentIndex + 1) % availableModels.length;\n\t\t\tconst nextModel = availableModels[nextIndex];\n\n\t\t\t// Validate API key\n\t\t\tconst apiKey = await getApiKeyForModel(nextModel);\n\t\t\tif (!apiKey) {\n\t\t\t\tthis.showError(`No API key for ${nextModel.provider}/${nextModel.id}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Switch model\n\t\t\tthis.agent.setModel(nextModel);\n\n\t\t\t// Save model change to session and settings\n\t\t\tthis.sessionManager.saveModelChange(nextModel.provider, nextModel.id);\n\t\t\tthis.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);\n\n\t\t\t// Show notification\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Switched to ${nextModel.name || nextModel.id}`), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t}\n\t}\n\n\tprivate toggleToolOutputExpansion(): void {\n\t\tthis.toolOutputExpanded = !this.toolOutputExpanded;\n\n\t\t// Update all tool execution and compaction components\n\t\tfor (const child of this.chatContainer.children) {\n\t\t\tif (child instanceof ToolExecutionComponent) {\n\t\t\t\tchild.setExpanded(this.toolOutputExpanded);\n\t\t\t} else if (child instanceof CompactionComponent) {\n\t\t\t\tchild.setExpanded(this.toolOutputExpanded);\n\t\t\t}\n\t\t}\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate toggleThinkingBlockVisibility(): void {\n\t\tthis.hideThinkingBlock = !this.hideThinkingBlock;\n\t\tthis.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);\n\n\t\t// Update all assistant message components and rebuild their content\n\t\tfor (const child of this.chatContainer.children) {\n\t\t\tif (child instanceof AssistantMessageComponent) {\n\t\t\t\tchild.setHideThinkingBlock(this.hideThinkingBlock);\n\t\t\t}\n\t\t}\n\n\t\t// Rebuild chat to apply visibility change\n\t\tthis.chatContainer.clear();\n\t\tthis.rebuildChatFromMessages();\n\n\t\t// Show brief notification\n\t\tconst status = this.hideThinkingBlock ? \"hidden\" : \"visible\";\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Thinking blocks: ${status}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tclearEditor(): void {\n\t\tthis.editor.setText(\"\");\n\t\tthis.ui.requestRender();\n\t}\n\n\tshowError(errorMessage: string): void {\n\t\t// Show error message in the chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"error\", `Error: ${errorMessage}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tshowWarning(warningMessage: string): void {\n\t\t// Show warning message in the chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"warning\", `Warning: ${warningMessage}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate showThinkingSelector(): void {\n\t\t// Create thinking selector with current level\n\t\tthis.thinkingSelector = new ThinkingSelectorComponent(\n\t\t\tthis.agent.state.thinkingLevel,\n\t\t\t(level) => {\n\t\t\t\t// Apply the selected thinking level\n\t\t\t\tthis.agent.setThinkingLevel(level);\n\n\t\t\t\t// Save thinking level change to session and settings\n\t\t\t\tthis.sessionManager.saveThinkingLevelChange(level);\n\t\t\t\tthis.settingsManager.setDefaultThinkingLevel(level);\n\n\t\t\t\t// Update border color\n\t\t\t\tthis.updateEditorBorderColor();\n\n\t\t\t\t// Show confirmation message with proper spacing\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Thinking level: ${level}`), 1, 0);\n\t\t\t\tthis.chatContainer.addChild(confirmText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideThinkingSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideThinkingSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.thinkingSelector);\n\t\tthis.ui.setFocus(this.thinkingSelector.getSelectList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideThinkingSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.thinkingSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showQueueModeSelector(): void {\n\t\t// Create queue mode selector with current mode\n\t\tthis.queueModeSelector = new QueueModeSelectorComponent(\n\t\t\tthis.agent.getQueueMode(),\n\t\t\t(mode) => {\n\t\t\t\t// Apply the selected queue mode\n\t\t\t\tthis.agent.setQueueMode(mode);\n\n\t\t\t\t// Save queue mode to settings\n\t\t\t\tthis.settingsManager.setQueueMode(mode);\n\n\t\t\t\t// Show confirmation message with proper spacing\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Queue mode: ${mode}`), 1, 0);\n\t\t\t\tthis.chatContainer.addChild(confirmText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideQueueModeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideQueueModeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.queueModeSelector);\n\t\tthis.ui.setFocus(this.queueModeSelector.getSelectList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideQueueModeSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.queueModeSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showThemeSelector(): void {\n\t\t// Get current theme from settings\n\t\tconst currentTheme = this.settingsManager.getTheme() || \"dark\";\n\n\t\t// Create theme selector\n\t\tthis.themeSelector = new ThemeSelectorComponent(\n\t\t\tcurrentTheme,\n\t\t\t(themeName) => {\n\t\t\t\t// Apply the selected theme\n\t\t\t\tconst result = setTheme(themeName);\n\n\t\t\t\t// Save theme to settings\n\t\t\t\tthis.settingsManager.setTheme(themeName);\n\n\t\t\t\t// Invalidate all components to clear cached rendering\n\t\t\t\tthis.ui.invalidate();\n\n\t\t\t\t// Show confirmation or error message\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tif (result.success) {\n\t\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Theme: ${themeName}`), 1, 0);\n\t\t\t\t\tthis.chatContainer.addChild(confirmText);\n\t\t\t\t} else {\n\t\t\t\t\tconst errorText = new Text(\n\t\t\t\t\t\ttheme.fg(\"error\", `Failed to load theme \"${themeName}\": ${result.error}\\nFell back to dark theme.`),\n\t\t\t\t\t\t1,\n\t\t\t\t\t\t0,\n\t\t\t\t\t);\n\t\t\t\t\tthis.chatContainer.addChild(errorText);\n\t\t\t\t}\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideThemeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideThemeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t(themeName) => {\n\t\t\t\t// Preview theme on selection change\n\t\t\t\tconst result = setTheme(themeName);\n\t\t\t\tif (result.success) {\n\t\t\t\t\tthis.ui.invalidate();\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\t// If failed, theme already fell back to dark, just don't re-render\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.themeSelector);\n\t\tthis.ui.setFocus(this.themeSelector.getSelectList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideThemeSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.themeSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showModelSelector(): void {\n\t\t// Create model selector with current model\n\t\tthis.modelSelector = new ModelSelectorComponent(\n\t\t\tthis.ui,\n\t\t\tthis.agent.state.model,\n\t\t\tthis.settingsManager,\n\t\t\t(model) => {\n\t\t\t\t// Apply the selected model\n\t\t\t\tthis.agent.setModel(model);\n\n\t\t\t\t// Save model change to session\n\t\t\t\tthis.sessionManager.saveModelChange(model.provider, model.id);\n\n\t\t\t\t// Show confirmation message with proper spacing\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Model: ${model.id}`), 1, 0);\n\t\t\t\tthis.chatContainer.addChild(confirmText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideModelSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideModelSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.modelSelector);\n\t\tthis.ui.setFocus(this.modelSelector);\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideModelSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.modelSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showUserMessageSelector(): void {\n\t\t// Read from session file directly to see ALL historical user messages\n\t\t// (including those before compaction events)\n\t\tconst entries = this.sessionManager.loadEntries();\n\t\tconst userMessages: Array<{ index: number; text: string }> = [];\n\n\t\tconst getUserMessageText = (content: string | Array<{ type: string; text?: string }>): string => {\n\t\t\tif (typeof content === \"string\") return content;\n\t\t\tif (Array.isArray(content)) {\n\t\t\t\treturn content\n\t\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t\t.map((c) => c.text)\n\t\t\t\t\t.join(\"\");\n\t\t\t}\n\t\t\treturn \"\";\n\t\t};\n\n\t\tfor (let i = 0; i < entries.length; i++) {\n\t\t\tconst entry = entries[i];\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tif (entry.message.role !== \"user\") continue;\n\n\t\t\tconst textContent = getUserMessageText(entry.message.content);\n\t\t\tif (textContent) {\n\t\t\t\tuserMessages.push({ index: i, text: textContent });\n\t\t\t}\n\t\t}\n\n\t\t// Don't show selector if there are no messages or only one message\n\t\tif (userMessages.length <= 1) {\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"No messages to branch from\"), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\t// Create user message selector\n\t\tthis.userMessageSelector = new UserMessageSelectorComponent(\n\t\t\tuserMessages,\n\t\t\t(entryIndex) => {\n\t\t\t\t// Get the selected user message text to put in the editor\n\t\t\t\tconst selectedEntry = entries[entryIndex];\n\t\t\t\tif (selectedEntry.type !== \"message\") return;\n\t\t\t\tif (selectedEntry.message.role !== \"user\") return;\n\n\t\t\t\tconst selectedText = getUserMessageText(selectedEntry.message.content);\n\n\t\t\t\t// Create a branched session by copying entries up to (but not including) the selected entry\n\t\t\t\tconst newSessionFile = this.sessionManager.createBranchedSessionFromEntries(entries, entryIndex);\n\n\t\t\t\t// Set the new session file as active\n\t\t\t\tthis.sessionManager.setSessionFile(newSessionFile);\n\n\t\t\t\t// Reload the session\n\t\t\t\tconst loaded = loadSessionFromEntries(this.sessionManager.loadEntries());\n\t\t\t\tthis.agent.replaceMessages(loaded.messages);\n\n\t\t\t\t// Clear and re-render the chat\n\t\t\t\tthis.chatContainer.clear();\n\t\t\t\tthis.isFirstUserMessage = true;\n\t\t\t\tthis.renderInitialMessages(this.agent.state);\n\n\t\t\t\t// Show confirmation message\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Branched to new session\"), 1, 0));\n\n\t\t\t\t// Put the selected message in the editor\n\t\t\t\tthis.editor.setText(selectedText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideUserMessageSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideUserMessageSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.userMessageSelector);\n\t\tthis.ui.setFocus(this.userMessageSelector.getMessageList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideUserMessageSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.userMessageSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate async showOAuthSelector(mode: \"login\" | \"logout\"): Promise<void> {\n\t\t// For logout mode, filter to only show logged-in providers\n\t\tlet providersToShow: string[] = [];\n\t\tif (mode === \"logout\") {\n\t\t\tconst loggedInProviders = listOAuthProviders();\n\t\t\tif (loggedInProviders.length === 0) {\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\tnew Text(theme.fg(\"dim\", \"No OAuth providers logged in. Use /login first.\"), 1, 0),\n\t\t\t\t);\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tprovidersToShow = loggedInProviders;\n\t\t}\n\n\t\t// Create OAuth selector\n\t\tthis.oauthSelector = new OAuthSelectorComponent(\n\t\t\tmode,\n\t\t\tasync (providerId: string) => {\n\t\t\t\t// Hide selector first\n\t\t\t\tthis.hideOAuthSelector();\n\n\t\t\t\tif (mode === \"login\") {\n\t\t\t\t\t// Handle login\n\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Logging in to ${providerId}...`), 1, 0));\n\t\t\t\t\tthis.ui.requestRender();\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait login(\n\t\t\t\t\t\t\tproviderId as SupportedOAuthProvider,\n\t\t\t\t\t\t\t(url: string) => {\n\t\t\t\t\t\t\t\t// Show auth URL to user\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"accent\", \"Opening browser to:\"), 1, 0));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"accent\", url), 1, 0));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\t\t\tnew Text(theme.fg(\"warning\", \"Paste the authorization code below:\"), 1, 0),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tthis.ui.requestRender();\n\n\t\t\t\t\t\t\t\t// Open URL in browser\n\t\t\t\t\t\t\t\tconst openCmd =\n\t\t\t\t\t\t\t\t\tprocess.platform === \"darwin\" ? \"open\" : process.platform === \"win32\" ? \"start\" : \"xdg-open\";\n\t\t\t\t\t\t\t\texec(`${openCmd} \"${url}\"`);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tasync () => {\n\t\t\t\t\t\t\t\t// Prompt for code with a simple Input\n\t\t\t\t\t\t\t\treturn new Promise<string>((resolve) => {\n\t\t\t\t\t\t\t\t\tconst codeInput = new Input();\n\t\t\t\t\t\t\t\t\tcodeInput.onSubmit = () => {\n\t\t\t\t\t\t\t\t\t\tconst code = codeInput.getValue();\n\t\t\t\t\t\t\t\t\t\t// Restore editor\n\t\t\t\t\t\t\t\t\t\tthis.editorContainer.clear();\n\t\t\t\t\t\t\t\t\t\tthis.editorContainer.addChild(this.editor);\n\t\t\t\t\t\t\t\t\t\tthis.ui.setFocus(this.editor);\n\t\t\t\t\t\t\t\t\t\tresolve(code);\n\t\t\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\t\t\tthis.editorContainer.clear();\n\t\t\t\t\t\t\t\t\tthis.editorContainer.addChild(codeInput);\n\t\t\t\t\t\t\t\t\tthis.ui.setFocus(codeInput);\n\t\t\t\t\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\t// Success - invalidate OAuth cache so footer updates\n\t\t\t\t\t\tinvalidateOAuthCache();\n\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\tnew Text(theme.fg(\"success\", `✓ Successfully logged in to ${providerId}`), 1, 0),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Tokens saved to ${getOAuthPath()}`), 1, 0));\n\t\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t\t} catch (error: any) {\n\t\t\t\t\t\tthis.showError(`Login failed: ${error.message}`);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Handle logout\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait logout(providerId as SupportedOAuthProvider);\n\n\t\t\t\t\t\t// Invalidate OAuth cache so footer updates\n\t\t\t\t\t\tinvalidateOAuthCache();\n\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\tnew Text(theme.fg(\"success\", `✓ Successfully logged out of ${providerId}`), 1, 0),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\tnew Text(theme.fg(\"dim\", `Credentials removed from ${getOAuthPath()}`), 1, 0),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t\t} catch (error: any) {\n\t\t\t\t\t\tthis.showError(`Logout failed: ${error.message}`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Cancel - just hide the selector\n\t\t\t\tthis.hideOAuthSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.oauthSelector);\n\t\tthis.ui.setFocus(this.oauthSelector);\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideOAuthSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.oauthSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate handleExportCommand(text: string): void {\n\t\t// Parse optional filename from command: /export [filename]\n\t\tconst parts = text.split(/\\s+/);\n\t\tconst outputPath = parts.length > 1 ? parts[1] : undefined;\n\n\t\ttry {\n\t\t\t// Export session to HTML\n\t\t\tconst filePath = exportSessionToHtml(this.sessionManager, this.agent.state, outputPath);\n\n\t\t\t// Show success message in chat - matching thinking level style\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Session exported to: ${filePath}`), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t} catch (error: any) {\n\t\t\t// Show error message in chat\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(\n\t\t\t\tnew Text(theme.fg(\"error\", `Failed to export session: ${error.message || \"Unknown error\"}`), 1, 0),\n\t\t\t);\n\t\t\tthis.ui.requestRender();\n\t\t}\n\t}\n\n\tprivate handleCopyCommand(): void {\n\t\t// Find the last assistant message\n\t\tconst lastAssistantMessage = this.agent.state.messages\n\t\t\t.slice()\n\t\t\t.reverse()\n\t\t\t.find((m) => m.role === \"assistant\");\n\n\t\tif (!lastAssistantMessage) {\n\t\t\tthis.showError(\"No agent messages to copy yet.\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Extract raw text content from all text blocks\n\t\tlet textContent = \"\";\n\n\t\tfor (const content of lastAssistantMessage.content) {\n\t\t\tif (content.type === \"text\") {\n\t\t\t\ttextContent += content.text;\n\t\t\t}\n\t\t}\n\n\t\tif (!textContent.trim()) {\n\t\t\tthis.showError(\"Last agent message contains no text content.\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Copy to clipboard using cross-platform compatible method\n\t\ttry {\n\t\t\tcopyToClipboard(textContent);\n\t\t} catch (error) {\n\t\t\tthis.showError(error instanceof Error ? error.message : String(error));\n\t\t\treturn;\n\t\t}\n\n\t\t// Show confirmation message\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Copied last agent message to clipboard\"), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleSessionCommand(): void {\n\t\t// Get session info\n\t\tconst sessionFile = this.sessionManager.getSessionFile();\n\t\tconst state = this.agent.state;\n\n\t\t// Count messages\n\t\tconst userMessages = state.messages.filter((m) => m.role === \"user\").length;\n\t\tconst assistantMessages = state.messages.filter((m) => m.role === \"assistant\").length;\n\t\tconst toolResults = state.messages.filter((m) => m.role === \"toolResult\").length;\n\t\tconst totalMessages = state.messages.length;\n\n\t\t// Count tool calls from assistant messages\n\t\tlet toolCalls = 0;\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\ttoolCalls += assistantMsg.content.filter((c) => c.type === \"toolCall\").length;\n\t\t\t}\n\t\t}\n\n\t\t// Calculate cumulative usage from all assistant messages (same as footer)\n\t\tlet totalInput = 0;\n\t\tlet totalOutput = 0;\n\t\tlet totalCacheRead = 0;\n\t\tlet totalCacheWrite = 0;\n\t\tlet totalCost = 0;\n\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\ttotalInput += assistantMsg.usage.input;\n\t\t\t\ttotalOutput += assistantMsg.usage.output;\n\t\t\t\ttotalCacheRead += assistantMsg.usage.cacheRead;\n\t\t\t\ttotalCacheWrite += assistantMsg.usage.cacheWrite;\n\t\t\t\ttotalCost += assistantMsg.usage.cost.total;\n\t\t\t}\n\t\t}\n\n\t\tconst totalTokens = totalInput + totalOutput + totalCacheRead + totalCacheWrite;\n\n\t\t// Build info text\n\t\tlet info = `${theme.bold(\"Session Info\")}\\n\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"File:\")} ${sessionFile}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"ID:\")} ${this.sessionManager.getSessionId()}\\n\\n`;\n\t\tinfo += `${theme.bold(\"Messages\")}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"User:\")} ${userMessages}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Assistant:\")} ${assistantMessages}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Tool Calls:\")} ${toolCalls}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Tool Results:\")} ${toolResults}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Total:\")} ${totalMessages}\\n\\n`;\n\t\tinfo += `${theme.bold(\"Tokens\")}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Input:\")} ${totalInput.toLocaleString()}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Output:\")} ${totalOutput.toLocaleString()}\\n`;\n\t\tif (totalCacheRead > 0) {\n\t\t\tinfo += `${theme.fg(\"dim\", \"Cache Read:\")} ${totalCacheRead.toLocaleString()}\\n`;\n\t\t}\n\t\tif (totalCacheWrite > 0) {\n\t\t\tinfo += `${theme.fg(\"dim\", \"Cache Write:\")} ${totalCacheWrite.toLocaleString()}\\n`;\n\t\t}\n\t\tinfo += `${theme.fg(\"dim\", \"Total:\")} ${totalTokens.toLocaleString()}\\n`;\n\n\t\tif (totalCost > 0) {\n\t\t\tinfo += `\\n${theme.bold(\"Cost\")}\\n`;\n\t\t\tinfo += `${theme.fg(\"dim\", \"Total:\")} ${totalCost.toFixed(4)}`;\n\t\t}\n\n\t\t// Show info in chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(info, 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleChangelogCommand(): void {\n\t\tconst changelogPath = getChangelogPath();\n\t\tconst allEntries = parseChangelog(changelogPath);\n\n\t\t// Show all entries in reverse order (oldest first, newest last)\n\t\tconst changelogMarkdown =\n\t\t\tallEntries.length > 0\n\t\t\t\t? allEntries\n\t\t\t\t\t\t.reverse()\n\t\t\t\t\t\t.map((e) => e.content)\n\t\t\t\t\t\t.join(\"\\n\\n\")\n\t\t\t\t: \"No changelog entries found.\";\n\n\t\t// Display in chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new DynamicBorder());\n\t\tthis.ui.addChild(new Text(theme.bold(theme.fg(\"accent\", \"What's New\")), 1, 0));\n\t\tthis.ui.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, getMarkdownTheme()));\n\t\tthis.chatContainer.addChild(new DynamicBorder());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate async handleClearCommand(): Promise<void> {\n\t\t// Unsubscribe first to prevent processing abort events\n\t\tthis.unsubscribe?.();\n\n\t\t// Abort and wait for completion\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\n\t\t// Stop loading animation\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.statusContainer.clear();\n\n\t\t// Reset agent and session\n\t\tthis.agent.reset();\n\t\tthis.sessionManager.reset();\n\n\t\t// Resubscribe to agent\n\t\tthis.subscribeToAgent();\n\n\t\t// Clear UI state\n\t\tthis.chatContainer.clear();\n\t\tthis.pendingMessagesContainer.clear();\n\t\tthis.queuedMessages = [];\n\t\tthis.streamingComponent = null;\n\t\tthis.pendingTools.clear();\n\t\tthis.isFirstUserMessage = true;\n\n\t\t// Show confirmation\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(\n\t\t\tnew Text(theme.fg(\"accent\", \"✓ Context cleared\") + \"\\n\" + theme.fg(\"muted\", \"Started fresh session\"), 1, 1),\n\t\t);\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleDebugCommand(): void {\n\t\t// Force a render and capture all lines with their widths\n\t\tconst width = this.ui.terminal.columns;\n\t\tconst allLines = this.ui.render(width);\n\n\t\tconst debugLogPath = getDebugLogPath();\n\t\tconst debugData = [\n\t\t\t`Debug output at ${new Date().toISOString()}`,\n\t\t\t`Terminal width: ${width}`,\n\t\t\t`Total lines: ${allLines.length}`,\n\t\t\t\"\",\n\t\t\t\"=== All rendered lines with visible widths ===\",\n\t\t\t...allLines.map((line, idx) => {\n\t\t\t\tconst vw = visibleWidth(line);\n\t\t\t\tconst escaped = JSON.stringify(line);\n\t\t\t\treturn `[${idx}] (w=${vw}) ${escaped}`;\n\t\t\t}),\n\t\t\t\"\",\n\t\t].join(\"\\n\");\n\n\t\tfs.mkdirSync(path.dirname(debugLogPath), { recursive: true });\n\t\tfs.writeFileSync(debugLogPath, debugData);\n\n\t\t// Show confirmation\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(\n\t\t\tnew Text(theme.fg(\"accent\", \"✓ Debug log written\") + \"\\n\" + theme.fg(\"muted\", debugLogPath), 1, 1),\n\t\t);\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate compactionAbortController: AbortController | null = null;\n\n\t/**\n\t * Shared logic to execute context compaction.\n\t * Handles aborting agent, showing loader, performing compaction, updating session/UI.\n\t */\n\tprivate async executeCompaction(customInstructions?: string, isAuto = false): Promise<void> {\n\t\t// Unsubscribe first to prevent processing events during compaction\n\t\tthis.unsubscribe?.();\n\n\t\t// Abort and wait for completion\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\n\t\t// Stop loading animation\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.statusContainer.clear();\n\n\t\t// Create abort controller for compaction\n\t\tthis.compactionAbortController = new AbortController();\n\n\t\t// Set up escape handler during compaction\n\t\tconst originalOnEscape = this.editor.onEscape;\n\t\tthis.editor.onEscape = () => {\n\t\t\tif (this.compactionAbortController) {\n\t\t\t\tthis.compactionAbortController.abort();\n\t\t\t}\n\t\t};\n\n\t\t// Show compacting status with loader\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tconst label = isAuto ? \"Auto-compacting context... (esc to cancel)\" : \"Compacting context... (esc to cancel)\";\n\t\tconst compactingLoader = new Loader(\n\t\t\tthis.ui,\n\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\tlabel,\n\t\t);\n\t\tthis.statusContainer.addChild(compactingLoader);\n\t\tthis.ui.requestRender();\n\n\t\ttry {\n\t\t\t// Get API key for current model\n\t\t\tconst apiKey = await getApiKeyForModel(this.agent.state.model);\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for ${this.agent.state.model.provider}`);\n\t\t\t}\n\n\t\t\t// Perform compaction with abort signal\n\t\t\tconst entries = this.sessionManager.loadEntries();\n\t\t\tconst settings = this.settingsManager.getCompactionSettings();\n\t\t\tconst compactionEntry = await compact(\n\t\t\t\tentries,\n\t\t\t\tthis.agent.state.model,\n\t\t\t\tsettings,\n\t\t\t\tapiKey,\n\t\t\t\tthis.compactionAbortController.signal,\n\t\t\t\tcustomInstructions,\n\t\t\t);\n\n\t\t\t// Check if aborted after compact returned\n\t\t\tif (this.compactionAbortController.signal.aborted) {\n\t\t\t\tthrow new Error(\"Compaction cancelled\");\n\t\t\t}\n\n\t\t\t// Save compaction to session\n\t\t\tthis.sessionManager.saveCompaction(compactionEntry);\n\n\t\t\t// Reload session\n\t\t\tconst loaded = loadSessionFromEntries(this.sessionManager.loadEntries());\n\t\t\tthis.agent.replaceMessages(loaded.messages);\n\n\t\t\t// Rebuild UI\n\t\t\tthis.chatContainer.clear();\n\t\t\tthis.rebuildChatFromMessages();\n\n\t\t\t// Add compaction component at current position so user can see/expand the summary\n\t\t\tconst compactionComponent = new CompactionComponent(compactionEntry.tokensBefore, compactionEntry.summary);\n\t\t\tcompactionComponent.setExpanded(this.toolOutputExpanded);\n\t\t\tthis.chatContainer.addChild(compactionComponent);\n\n\t\t\t// Update footer with new state (fixes context % display)\n\t\t\tthis.footer.updateState(this.agent.state);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\tif (message === \"Compaction cancelled\" || (error instanceof Error && error.name === \"AbortError\")) {\n\t\t\t\tthis.showError(\"Compaction cancelled\");\n\t\t\t} else {\n\t\t\t\tthis.showError(`Compaction failed: ${message}`);\n\t\t\t}\n\t\t} finally {\n\t\t\t// Clean up\n\t\t\tcompactingLoader.stop();\n\t\t\tthis.statusContainer.clear();\n\t\t\tthis.compactionAbortController = null;\n\t\t\tthis.editor.onEscape = originalOnEscape;\n\t\t}\n\n\t\t// Resubscribe to agent\n\t\tthis.subscribeToAgent();\n\t}\n\n\tprivate async handleCompactCommand(customInstructions?: string): Promise<void> {\n\t\t// Check if there are any messages to compact\n\t\tconst entries = this.sessionManager.loadEntries();\n\t\tconst messageCount = entries.filter((e) => e.type === \"message\").length;\n\n\t\tif (messageCount < 2) {\n\t\t\tthis.showWarning(\"Nothing to compact (no messages yet)\");\n\t\t\treturn;\n\t\t}\n\n\t\tawait this.executeCompaction(customInstructions, false);\n\t}\n\n\tprivate handleAutocompactCommand(): void {\n\t\tconst currentEnabled = this.settingsManager.getCompactionEnabled();\n\t\tconst newState = !currentEnabled;\n\t\tthis.settingsManager.setCompactionEnabled(newState);\n\t\tthis.footer.setAutoCompactEnabled(newState);\n\n\t\t// Show brief notification (same style as thinking level toggle)\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Auto-compaction: ${newState ? \"on\" : \"off\"}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate updatePendingMessagesDisplay(): void {\n\t\tthis.pendingMessagesContainer.clear();\n\n\t\tif (this.queuedMessages.length > 0) {\n\t\t\tthis.pendingMessagesContainer.addChild(new Spacer(1));\n\n\t\t\tfor (const message of this.queuedMessages) {\n\t\t\t\tconst queuedText = theme.fg(\"dim\", \"Queued: \" + message);\n\t\t\t\tthis.pendingMessagesContainer.addChild(new TruncatedText(queuedText, 1, 0));\n\t\t\t}\n\t\t}\n\t}\n\n\tstop(): void {\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.footer.dispose();\n\t\tif (this.isInitialized) {\n\t\t\tthis.ui.stop();\n\t\t\tthis.isInitialized = false;\n\t\t}\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"tui-renderer.d.ts","sourceRoot":"","sources":["../../src/tui/tui-renderer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAc,UAAU,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAChG,OAAO,KAAK,EAA6B,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAuB5E,OAAO,EAGN,KAAK,cAAc,EAGnB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAkB9D;;GAEG;AACH,qBAAa,WAAW;IACvB,OAAO,CAAC,EAAE,CAAM;IAChB,OAAO,CAAC,aAAa,CAAY;IACjC,OAAO,CAAC,wBAAwB,CAAY;IAC5C,OAAO,CAAC,eAAe,CAAY;IACnC,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,eAAe,CAAY;IACnC,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,cAAc,CAAiB;IACvC,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,eAAe,CAAC,CAAyB;IACjD,OAAO,CAAC,gBAAgB,CAAuB;IAE/C,OAAO,CAAC,cAAc,CAAK;IAC3B,OAAO,CAAC,iBAAiB,CAAuB;IAGhD,OAAO,CAAC,cAAc,CAAgB;IAGtC,OAAO,CAAC,kBAAkB,CAA0C;IAGpE,OAAO,CAAC,YAAY,CAA6C;IAGjE,OAAO,CAAC,gBAAgB,CAA0C;IAGlE,OAAO,CAAC,iBAAiB,CAA2C;IAGpE,OAAO,CAAC,aAAa,CAAuC;IAG5D,OAAO,CAAC,aAAa,CAAuC;IAG5D,OAAO,CAAC,mBAAmB,CAA6C;IAGxE,OAAO,CAAC,eAAe,CAAyC;IAGhE,OAAO,CAAC,aAAa,CAAoB;IAGzC,OAAO,CAAC,kBAAkB,CAAQ;IAGlC,OAAO,CAAC,YAAY,CAAkE;IAGtF,OAAO,CAAC,kBAAkB,CAAS;IAGnC,OAAO,CAAC,iBAAiB,CAAS;IAGlC,OAAO,CAAC,WAAW,CAAC,CAAa;IAGjC,OAAO,CAAC,YAAY,CAA0B;IAE9C,YACC,KAAK,EAAE,KAAK,EACZ,cAAc,EAAE,cAAc,EAC9B,eAAe,EAAE,eAAe,EAChC,OAAO,EAAE,MAAM,EACf,iBAAiB,GAAE,MAAM,GAAG,IAAW,EACvC,YAAY,GAAE,KAAK,CAAC;QAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;QAAC,aAAa,EAAE,aAAa,CAAA;KAAE,CAAM,EAC7E,MAAM,GAAE,MAAM,GAAG,IAAW,EAkI5B;IAEK,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAiT1B;IAED,OAAO,CAAC,gBAAgB;YAsBV,mBAAmB;YA4BnB,WAAW;IA4KzB,OAAO,CAAC,gBAAgB;IAwBxB,qBAAqB,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAkG7C;IAEK,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAOpC;IAED,OAAO,CAAC,uBAAuB;IA0D/B,OAAO,CAAC,WAAW;IAgBnB,OAAO,CAAC,uBAAuB;IAM/B,OAAO,CAAC,kBAAkB;YA+BZ,UAAU;IA4GxB,OAAO,CAAC,yBAAyB;IAejC,OAAO,CAAC,6BAA6B;IAsBrC,WAAW,IAAI,IAAI,CAGlB;IAED,SAAS,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAKpC;IAED,WAAW,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI,CAKxC;IAED,0BAA0B,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAgBnD;IAED,OAAO,CAAC,oBAAoB;IAsC5B,OAAO,CAAC,oBAAoB;IAQ5B,OAAO,CAAC,qBAAqB;IAkC7B,OAAO,CAAC,qBAAqB;IAQ7B,OAAO,CAAC,iBAAiB;IA0DzB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,iBAAiB;IAoCzB,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,uBAAuB;IAuF/B,OAAO,CAAC,uBAAuB;IAQ/B,OAAO,CAAC,mBAAmB;YAsBb,mBAAmB;IA2DjC,OAAO,CAAC,mBAAmB;YAQb,iBAAiB;IAkH/B,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,mBAAmB;IAuB3B,OAAO,CAAC,iBAAiB;IAwCzB,OAAO,CAAC,oBAAoB;IAwE5B,OAAO,CAAC,sBAAsB;YAuBhB,kBAAkB;IAuChC,OAAO,CAAC,kBAAkB;IAgC1B,OAAO,CAAC,yBAAyB,CAAgC;YAMnD,iBAAiB;YAmGjB,oBAAoB;IAalC,OAAO,CAAC,wBAAwB;IAYhC,OAAO,CAAC,4BAA4B;IAapC,IAAI,IAAI,IAAI,CAUX;CACD","sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport type { Agent, AgentEvent, AgentState, ThinkingLevel } from \"@mariozechner/pi-agent-core\";\nimport type { AssistantMessage, Message, Model } from \"@mariozechner/pi-ai\";\nimport type { SlashCommand } from \"@mariozechner/pi-tui\";\nimport {\n\tCombinedAutocompleteProvider,\n\tContainer,\n\tInput,\n\tLoader,\n\tMarkdown,\n\tProcessTerminal,\n\tSpacer,\n\tText,\n\tTruncatedText,\n\tTUI,\n\tvisibleWidth,\n} from \"@mariozechner/pi-tui\";\nimport { exec } from \"child_process\";\nimport { getChangelogPath, parseChangelog } from \"../changelog.js\";\nimport { copyToClipboard } from \"../clipboard.js\";\nimport { calculateContextTokens, compact, shouldCompact } from \"../compaction.js\";\nimport { APP_NAME, getDebugLogPath, getModelsPath, getOAuthPath } from \"../config.js\";\nimport { exportSessionToHtml } from \"../export-html.js\";\nimport { getApiKeyForModel, getAvailableModels, invalidateOAuthCache } from \"../model-config.js\";\nimport { listOAuthProviders, login, logout, type SupportedOAuthProvider } from \"../oauth/index.js\";\nimport {\n\tgetLatestCompactionEntry,\n\tloadSessionFromEntries,\n\ttype SessionManager,\n\tSUMMARY_PREFIX,\n\tSUMMARY_SUFFIX,\n} from \"../session-manager.js\";\nimport type { SettingsManager } from \"../settings-manager.js\";\nimport { expandSlashCommand, type FileSlashCommand, loadSlashCommands } from \"../slash-commands.js\";\nimport { getEditorTheme, getMarkdownTheme, onThemeChange, setTheme, theme } from \"../theme/theme.js\";\nimport { AssistantMessageComponent } from \"./assistant-message.js\";\nimport { CompactionComponent } from \"./compaction.js\";\nimport { CustomEditor } from \"./custom-editor.js\";\nimport { DynamicBorder } from \"./dynamic-border.js\";\nimport { FooterComponent } from \"./footer.js\";\nimport { ModelSelectorComponent } from \"./model-selector.js\";\nimport { OAuthSelectorComponent } from \"./oauth-selector.js\";\nimport { QueueModeSelectorComponent } from \"./queue-mode-selector.js\";\nimport { SessionSelectorComponent } from \"./session-selector.js\";\nimport { ThemeSelectorComponent } from \"./theme-selector.js\";\nimport { ThinkingSelectorComponent } from \"./thinking-selector.js\";\nimport { ToolExecutionComponent } from \"./tool-execution.js\";\nimport { UserMessageComponent } from \"./user-message.js\";\nimport { UserMessageSelectorComponent } from \"./user-message-selector.js\";\n\n/**\n * TUI renderer for the coding agent\n */\nexport class TuiRenderer {\n\tprivate ui: TUI;\n\tprivate chatContainer: Container;\n\tprivate pendingMessagesContainer: Container;\n\tprivate statusContainer: Container;\n\tprivate editor: CustomEditor;\n\tprivate editorContainer: Container; // Container to swap between editor and selector\n\tprivate footer: FooterComponent;\n\tprivate agent: Agent;\n\tprivate sessionManager: SessionManager;\n\tprivate settingsManager: SettingsManager;\n\tprivate version: string;\n\tprivate isInitialized = false;\n\tprivate onInputCallback?: (text: string) => void;\n\tprivate loadingAnimation: Loader | null = null;\n\n\tprivate lastSigintTime = 0;\n\tprivate changelogMarkdown: string | null = null;\n\n\t// Message queueing\n\tprivate queuedMessages: string[] = [];\n\n\t// Streaming message tracking\n\tprivate streamingComponent: AssistantMessageComponent | null = null;\n\n\t// Tool execution tracking: toolCallId -> component\n\tprivate pendingTools = new Map<string, ToolExecutionComponent>();\n\n\t// Thinking level selector\n\tprivate thinkingSelector: ThinkingSelectorComponent | null = null;\n\n\t// Queue mode selector\n\tprivate queueModeSelector: QueueModeSelectorComponent | null = null;\n\n\t// Theme selector\n\tprivate themeSelector: ThemeSelectorComponent | null = null;\n\n\t// Model selector\n\tprivate modelSelector: ModelSelectorComponent | null = null;\n\n\t// User message selector (for branching)\n\tprivate userMessageSelector: UserMessageSelectorComponent | null = null;\n\n\t// Session selector (for resume)\n\tprivate sessionSelector: SessionSelectorComponent | null = null;\n\n\t// OAuth selector\n\tprivate oauthSelector: any | null = null;\n\n\t// Track if this is the first user message (to skip spacer)\n\tprivate isFirstUserMessage = true;\n\n\t// Model scope for quick cycling\n\tprivate scopedModels: Array<{ model: Model<any>; thinkingLevel: ThinkingLevel }> = [];\n\n\t// Tool output expansion state\n\tprivate toolOutputExpanded = false;\n\n\t// Thinking block visibility state\n\tprivate hideThinkingBlock = false;\n\n\t// Agent subscription unsubscribe function\n\tprivate unsubscribe?: () => void;\n\n\t// File-based slash commands\n\tprivate fileCommands: FileSlashCommand[] = [];\n\n\tconstructor(\n\t\tagent: Agent,\n\t\tsessionManager: SessionManager,\n\t\tsettingsManager: SettingsManager,\n\t\tversion: string,\n\t\tchangelogMarkdown: string | null = null,\n\t\tscopedModels: Array<{ model: Model<any>; thinkingLevel: ThinkingLevel }> = [],\n\t\tfdPath: string | null = null,\n\t) {\n\t\tthis.agent = agent;\n\t\tthis.sessionManager = sessionManager;\n\t\tthis.settingsManager = settingsManager;\n\t\tthis.version = version;\n\t\tthis.changelogMarkdown = changelogMarkdown;\n\t\tthis.scopedModels = scopedModels;\n\t\tthis.ui = new TUI(new ProcessTerminal());\n\t\tthis.chatContainer = new Container();\n\t\tthis.pendingMessagesContainer = new Container();\n\t\tthis.statusContainer = new Container();\n\t\tthis.editor = new CustomEditor(getEditorTheme());\n\t\tthis.editorContainer = new Container(); // Container to hold editor or selector\n\t\tthis.editorContainer.addChild(this.editor); // Start with editor\n\t\tthis.footer = new FooterComponent(agent.state);\n\t\tthis.footer.setAutoCompactEnabled(this.settingsManager.getCompactionEnabled());\n\n\t\t// Define slash commands\n\t\tconst thinkingCommand: SlashCommand = {\n\t\t\tname: \"thinking\",\n\t\t\tdescription: \"Select reasoning level (opens selector UI)\",\n\t\t};\n\n\t\tconst modelCommand: SlashCommand = {\n\t\t\tname: \"model\",\n\t\t\tdescription: \"Select model (opens selector UI)\",\n\t\t};\n\n\t\tconst exportCommand: SlashCommand = {\n\t\t\tname: \"export\",\n\t\t\tdescription: \"Export session to HTML file\",\n\t\t};\n\n\t\tconst copyCommand: SlashCommand = {\n\t\t\tname: \"copy\",\n\t\t\tdescription: \"Copy last agent message to clipboard\",\n\t\t};\n\n\t\tconst sessionCommand: SlashCommand = {\n\t\t\tname: \"session\",\n\t\t\tdescription: \"Show session info and stats\",\n\t\t};\n\n\t\tconst changelogCommand: SlashCommand = {\n\t\t\tname: \"changelog\",\n\t\t\tdescription: \"Show changelog entries\",\n\t\t};\n\n\t\tconst branchCommand: SlashCommand = {\n\t\t\tname: \"branch\",\n\t\t\tdescription: \"Create a new branch from a previous message\",\n\t\t};\n\n\t\tconst loginCommand: SlashCommand = {\n\t\t\tname: \"login\",\n\t\t\tdescription: \"Login with OAuth provider\",\n\t\t};\n\n\t\tconst logoutCommand: SlashCommand = {\n\t\t\tname: \"logout\",\n\t\t\tdescription: \"Logout from OAuth provider\",\n\t\t};\n\n\t\tconst queueCommand: SlashCommand = {\n\t\t\tname: \"queue\",\n\t\t\tdescription: \"Select message queue mode (opens selector UI)\",\n\t\t};\n\n\t\tconst themeCommand: SlashCommand = {\n\t\t\tname: \"theme\",\n\t\t\tdescription: \"Select color theme (opens selector UI)\",\n\t\t};\n\n\t\tconst clearCommand: SlashCommand = {\n\t\t\tname: \"clear\",\n\t\t\tdescription: \"Clear context and start a fresh session\",\n\t\t};\n\n\t\tconst compactCommand: SlashCommand = {\n\t\t\tname: \"compact\",\n\t\t\tdescription: \"Manually compact the session context\",\n\t\t};\n\n\t\tconst autocompactCommand: SlashCommand = {\n\t\t\tname: \"autocompact\",\n\t\t\tdescription: \"Toggle automatic context compaction\",\n\t\t};\n\n\t\tconst resumeCommand: SlashCommand = {\n\t\t\tname: \"resume\",\n\t\t\tdescription: \"Resume a different session\",\n\t\t};\n\n\t\t// Load hide thinking block setting\n\t\tthis.hideThinkingBlock = settingsManager.getHideThinkingBlock();\n\n\t\t// Load file-based slash commands\n\t\tthis.fileCommands = loadSlashCommands();\n\n\t\t// Convert file commands to SlashCommand format\n\t\tconst fileSlashCommands: SlashCommand[] = this.fileCommands.map((cmd) => ({\n\t\t\tname: cmd.name,\n\t\t\tdescription: cmd.description,\n\t\t}));\n\n\t\t// Setup autocomplete for file paths and slash commands\n\t\tconst autocompleteProvider = new CombinedAutocompleteProvider(\n\t\t\t[\n\t\t\t\tthinkingCommand,\n\t\t\t\tmodelCommand,\n\t\t\t\tthemeCommand,\n\t\t\t\texportCommand,\n\t\t\t\tcopyCommand,\n\t\t\t\tsessionCommand,\n\t\t\t\tchangelogCommand,\n\t\t\t\tbranchCommand,\n\t\t\t\tloginCommand,\n\t\t\t\tlogoutCommand,\n\t\t\t\tqueueCommand,\n\t\t\t\tclearCommand,\n\t\t\t\tcompactCommand,\n\t\t\t\tautocompactCommand,\n\t\t\t\tresumeCommand,\n\t\t\t\t...fileSlashCommands,\n\t\t\t],\n\t\t\tprocess.cwd(),\n\t\t\tfdPath,\n\t\t);\n\t\tthis.editor.setAutocompleteProvider(autocompleteProvider);\n\t}\n\n\tasync init(): Promise<void> {\n\t\tif (this.isInitialized) return;\n\n\t\t// Add header with logo and instructions\n\t\tconst logo = theme.bold(theme.fg(\"accent\", APP_NAME)) + theme.fg(\"dim\", ` v${this.version}`);\n\t\tconst instructions =\n\t\t\ttheme.fg(\"dim\", \"esc\") +\n\t\t\ttheme.fg(\"muted\", \" to interrupt\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+c\") +\n\t\t\ttheme.fg(\"muted\", \" to clear\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+c twice\") +\n\t\t\ttheme.fg(\"muted\", \" to exit\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+k\") +\n\t\t\ttheme.fg(\"muted\", \" to delete line\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"shift+tab\") +\n\t\t\ttheme.fg(\"muted\", \" to cycle thinking\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+p\") +\n\t\t\ttheme.fg(\"muted\", \" to cycle models\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+o\") +\n\t\t\ttheme.fg(\"muted\", \" to expand tools\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"ctrl+t\") +\n\t\t\ttheme.fg(\"muted\", \" to toggle thinking\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"/\") +\n\t\t\ttheme.fg(\"muted\", \" for commands\") +\n\t\t\t\"\\n\" +\n\t\t\ttheme.fg(\"dim\", \"drop files\") +\n\t\t\ttheme.fg(\"muted\", \" to attach\");\n\t\tconst header = new Text(logo + \"\\n\" + instructions, 1, 0);\n\n\t\t// Setup UI layout\n\t\tthis.ui.addChild(new Spacer(1));\n\t\tthis.ui.addChild(header);\n\t\tthis.ui.addChild(new Spacer(1));\n\n\t\t// Add changelog if provided\n\t\tif (this.changelogMarkdown) {\n\t\t\tthis.ui.addChild(new DynamicBorder());\n\t\t\tthis.ui.addChild(new Text(theme.bold(theme.fg(\"accent\", \"What's New\")), 1, 0));\n\t\t\tthis.ui.addChild(new Spacer(1));\n\t\t\tthis.ui.addChild(new Markdown(this.changelogMarkdown.trim(), 1, 0, getMarkdownTheme()));\n\t\t\tthis.ui.addChild(new Spacer(1));\n\t\t\tthis.ui.addChild(new DynamicBorder());\n\t\t}\n\n\t\tthis.ui.addChild(this.chatContainer);\n\t\tthis.ui.addChild(this.pendingMessagesContainer);\n\t\tthis.ui.addChild(this.statusContainer);\n\t\tthis.ui.addChild(new Spacer(1));\n\t\tthis.ui.addChild(this.editorContainer); // Use container that can hold editor or selector\n\t\tthis.ui.addChild(this.footer);\n\t\tthis.ui.setFocus(this.editor);\n\n\t\t// Set up custom key handlers on the editor\n\t\tthis.editor.onEscape = () => {\n\t\t\t// Intercept Escape key when processing\n\t\t\tif (this.loadingAnimation) {\n\t\t\t\t// Get all queued messages\n\t\t\t\tconst queuedText = this.queuedMessages.join(\"\\n\\n\");\n\n\t\t\t\t// Get current editor text\n\t\t\t\tconst currentText = this.editor.getText();\n\n\t\t\t\t// Combine: queued messages + current editor text\n\t\t\t\tconst combinedText = [queuedText, currentText].filter((t) => t.trim()).join(\"\\n\\n\");\n\n\t\t\t\t// Put back in editor\n\t\t\t\tthis.editor.setText(combinedText);\n\n\t\t\t\t// Clear queued messages\n\t\t\t\tthis.queuedMessages = [];\n\t\t\t\tthis.updatePendingMessagesDisplay();\n\n\t\t\t\t// Clear agent's queue too\n\t\t\t\tthis.agent.clearMessageQueue();\n\n\t\t\t\t// Abort\n\t\t\t\tthis.agent.abort();\n\t\t\t}\n\t\t};\n\n\t\tthis.editor.onCtrlC = () => {\n\t\t\tthis.handleCtrlC();\n\t\t};\n\n\t\tthis.editor.onShiftTab = () => {\n\t\t\tthis.cycleThinkingLevel();\n\t\t};\n\n\t\tthis.editor.onCtrlP = () => {\n\t\t\tthis.cycleModel();\n\t\t};\n\n\t\tthis.editor.onCtrlO = () => {\n\t\t\tthis.toggleToolOutputExpansion();\n\t\t};\n\n\t\tthis.editor.onCtrlT = () => {\n\t\t\tthis.toggleThinkingBlockVisibility();\n\t\t};\n\n\t\t// Handle editor submission\n\t\tthis.editor.onSubmit = async (text: string) => {\n\t\t\ttext = text.trim();\n\t\t\tif (!text) return;\n\n\t\t\t// Check for /thinking command\n\t\t\tif (text === \"/thinking\") {\n\t\t\t\t// Show thinking level selector\n\t\t\t\tthis.showThinkingSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /model command\n\t\t\tif (text === \"/model\") {\n\t\t\t\t// Show model selector\n\t\t\t\tthis.showModelSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /export command\n\t\t\tif (text.startsWith(\"/export\")) {\n\t\t\t\tthis.handleExportCommand(text);\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /copy command\n\t\t\tif (text === \"/copy\") {\n\t\t\t\tthis.handleCopyCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /session command\n\t\t\tif (text === \"/session\") {\n\t\t\t\tthis.handleSessionCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /changelog command\n\t\t\tif (text === \"/changelog\") {\n\t\t\t\tthis.handleChangelogCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /branch command\n\t\t\tif (text === \"/branch\") {\n\t\t\t\tthis.showUserMessageSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /login command\n\t\t\tif (text === \"/login\") {\n\t\t\t\tthis.showOAuthSelector(\"login\");\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /logout command\n\t\t\tif (text === \"/logout\") {\n\t\t\t\tthis.showOAuthSelector(\"logout\");\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /queue command\n\t\t\tif (text === \"/queue\") {\n\t\t\t\tthis.showQueueModeSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /theme command\n\t\t\tif (text === \"/theme\") {\n\t\t\t\tthis.showThemeSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /clear command\n\t\t\tif (text === \"/clear\") {\n\t\t\t\tthis.handleClearCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /compact command\n\t\t\tif (text === \"/compact\" || text.startsWith(\"/compact \")) {\n\t\t\t\tconst customInstructions = text.startsWith(\"/compact \") ? text.slice(9).trim() : undefined;\n\t\t\t\tthis.handleCompactCommand(customInstructions);\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /autocompact command\n\t\t\tif (text === \"/autocompact\") {\n\t\t\t\tthis.handleAutocompactCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /debug command\n\t\t\tif (text === \"/debug\") {\n\t\t\t\tthis.handleDebugCommand();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for /resume command\n\t\t\tif (text === \"/resume\") {\n\t\t\t\tthis.showSessionSelector();\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check for file-based slash commands\n\t\t\ttext = expandSlashCommand(text, this.fileCommands);\n\n\t\t\t// Normal message submission - validate model and API key first\n\t\t\tconst currentModel = this.agent.state.model;\n\t\t\tif (!currentModel) {\n\t\t\t\tthis.showError(\n\t\t\t\t\t\"No model selected.\\n\\n\" +\n\t\t\t\t\t\t\"Set an API key (ANTHROPIC_API_KEY, OPENAI_API_KEY, etc.)\\n\" +\n\t\t\t\t\t\t`or create ${getModelsPath()}\\n\\n` +\n\t\t\t\t\t\t\"Then use /model to select a model.\",\n\t\t\t\t);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Validate API key (async)\n\t\t\tconst apiKey = await getApiKeyForModel(currentModel);\n\t\t\tif (!apiKey) {\n\t\t\t\tthis.showError(\n\t\t\t\t\t`No API key found for ${currentModel.provider}.\\n\\n` +\n\t\t\t\t\t\t`Set the appropriate environment variable or update ${getModelsPath()}`,\n\t\t\t\t);\n\t\t\t\tthis.editor.setText(text);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Check if agent is currently streaming\n\t\t\tif (this.agent.state.isStreaming) {\n\t\t\t\t// Queue the message instead of submitting\n\t\t\t\tthis.queuedMessages.push(text);\n\n\t\t\t\t// Queue in agent\n\t\t\t\tawait this.agent.queueMessage({\n\t\t\t\t\trole: \"user\",\n\t\t\t\t\tcontent: [{ type: \"text\", text }],\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t});\n\n\t\t\t\t// Update pending messages display\n\t\t\t\tthis.updatePendingMessagesDisplay();\n\n\t\t\t\t// Add to history for up/down arrow navigation\n\t\t\t\tthis.editor.addToHistory(text);\n\n\t\t\t\t// Clear editor\n\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// All good, proceed with submission\n\t\t\tif (this.onInputCallback) {\n\t\t\t\tthis.onInputCallback(text);\n\t\t\t}\n\n\t\t\t// Add to history for up/down arrow navigation\n\t\t\tthis.editor.addToHistory(text);\n\t\t};\n\n\t\t// Start the UI\n\t\tthis.ui.start();\n\t\tthis.isInitialized = true;\n\n\t\t// Subscribe to agent events for UI updates and session saving\n\t\tthis.subscribeToAgent();\n\n\t\t// Set up theme file watcher for live reload\n\t\tonThemeChange(() => {\n\t\t\tthis.ui.invalidate();\n\t\t\tthis.updateEditorBorderColor();\n\t\t\tthis.ui.requestRender();\n\t\t});\n\n\t\t// Set up git branch watcher\n\t\tthis.footer.watchBranch(() => {\n\t\t\tthis.ui.requestRender();\n\t\t});\n\t}\n\n\tprivate subscribeToAgent(): void {\n\t\tthis.unsubscribe = this.agent.subscribe(async (event) => {\n\t\t\t// Handle UI updates\n\t\t\tawait this.handleEvent(event, this.agent.state);\n\n\t\t\t// Save messages to session\n\t\t\tif (event.type === \"message_end\") {\n\t\t\t\tthis.sessionManager.saveMessage(event.message);\n\n\t\t\t\t// Check if we should initialize session now (after first user+assistant exchange)\n\t\t\t\tif (this.sessionManager.shouldInitializeSession(this.agent.state.messages)) {\n\t\t\t\t\tthis.sessionManager.startSession(this.agent.state);\n\t\t\t\t}\n\n\t\t\t\t// Check for auto-compaction after assistant messages\n\t\t\t\tif (event.message.role === \"assistant\") {\n\t\t\t\t\tawait this.checkAutoCompaction();\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate async checkAutoCompaction(): Promise<void> {\n\t\tconst settings = this.settingsManager.getCompactionSettings();\n\t\tif (!settings.enabled) return;\n\n\t\t// Get last non-aborted assistant message from agent state\n\t\tconst messages = this.agent.state.messages;\n\t\tlet lastAssistant: AssistantMessage | null = null;\n\t\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\t\tconst msg = messages[i];\n\t\t\tif (msg.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\t\tif (assistantMsg.stopReason !== \"aborted\") {\n\t\t\t\t\tlastAssistant = assistantMsg;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (!lastAssistant) return;\n\n\t\tconst contextTokens = calculateContextTokens(lastAssistant.usage);\n\t\tconst contextWindow = this.agent.state.model.contextWindow;\n\n\t\tif (!shouldCompact(contextTokens, contextWindow, settings)) return;\n\n\t\t// Trigger auto-compaction\n\t\tawait this.executeCompaction(undefined, true);\n\t}\n\n\tprivate async handleEvent(event: AgentEvent, state: AgentState): Promise<void> {\n\t\tif (!this.isInitialized) {\n\t\t\tawait this.init();\n\t\t}\n\n\t\t// Update footer with current stats\n\t\tthis.footer.updateState(state);\n\n\t\tswitch (event.type) {\n\t\t\tcase \"agent_start\":\n\t\t\t\t// Show loading animation\n\t\t\t\t// Note: Don't disable submit - we handle queuing in onSubmit callback\n\t\t\t\t// Stop old loader before clearing\n\t\t\t\tif (this.loadingAnimation) {\n\t\t\t\t\tthis.loadingAnimation.stop();\n\t\t\t\t}\n\t\t\t\tthis.statusContainer.clear();\n\t\t\t\tthis.loadingAnimation = new Loader(\n\t\t\t\t\tthis.ui,\n\t\t\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\t\t\"Working... (esc to interrupt)\",\n\t\t\t\t);\n\t\t\t\tthis.statusContainer.addChild(this.loadingAnimation);\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\tbreak;\n\n\t\t\tcase \"message_start\":\n\t\t\t\tif (event.message.role === \"user\") {\n\t\t\t\t\t// Check if this is a queued message\n\t\t\t\t\tconst userMsg = event.message;\n\t\t\t\t\tconst textBlocks =\n\t\t\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\t\t\tconst messageText = textBlocks.map((c) => c.text).join(\"\");\n\n\t\t\t\t\tconst queuedIndex = this.queuedMessages.indexOf(messageText);\n\t\t\t\t\tif (queuedIndex !== -1) {\n\t\t\t\t\t\t// Remove from queued messages\n\t\t\t\t\t\tthis.queuedMessages.splice(queuedIndex, 1);\n\t\t\t\t\t\tthis.updatePendingMessagesDisplay();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Show user message immediately and clear editor\n\t\t\t\t\tthis.addMessageToChat(event.message);\n\t\t\t\t\tthis.editor.setText(\"\");\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t} else if (event.message.role === \"assistant\") {\n\t\t\t\t\t// Create assistant component for streaming\n\t\t\t\t\tthis.streamingComponent = new AssistantMessageComponent(undefined, this.hideThinkingBlock);\n\t\t\t\t\tthis.chatContainer.addChild(this.streamingComponent);\n\t\t\t\t\tthis.streamingComponent.updateContent(event.message as AssistantMessage);\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\tcase \"message_update\":\n\t\t\t\t// Update streaming component\n\t\t\t\tif (this.streamingComponent && event.message.role === \"assistant\") {\n\t\t\t\t\tconst assistantMsg = event.message as AssistantMessage;\n\t\t\t\t\tthis.streamingComponent.updateContent(assistantMsg);\n\n\t\t\t\t\t// Create tool execution components as soon as we see tool calls\n\t\t\t\t\tfor (const content of assistantMsg.content) {\n\t\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\t\t// Only create if we haven't created it yet\n\t\t\t\t\t\t\tif (!this.pendingTools.has(content.id)) {\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Text(\"\", 0, 0));\n\t\t\t\t\t\t\t\tconst component = new ToolExecutionComponent(content.name, content.arguments);\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t\t\t\tthis.pendingTools.set(content.id, component);\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// Update existing component with latest arguments as they stream\n\t\t\t\t\t\t\t\tconst component = this.pendingTools.get(content.id);\n\t\t\t\t\t\t\t\tif (component) {\n\t\t\t\t\t\t\t\t\tcomponent.updateArgs(content.arguments);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\tcase \"message_end\":\n\t\t\t\t// Skip user messages (already shown in message_start)\n\t\t\t\tif (event.message.role === \"user\") {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tif (this.streamingComponent && event.message.role === \"assistant\") {\n\t\t\t\t\tconst assistantMsg = event.message as AssistantMessage;\n\n\t\t\t\t\t// Update streaming component with final message (includes stopReason)\n\t\t\t\t\tthis.streamingComponent.updateContent(assistantMsg);\n\n\t\t\t\t\t// If message was aborted or errored, mark all pending tool components as failed\n\t\t\t\t\tif (assistantMsg.stopReason === \"aborted\" || assistantMsg.stopReason === \"error\") {\n\t\t\t\t\t\tconst errorMessage =\n\t\t\t\t\t\t\tassistantMsg.stopReason === \"aborted\" ? \"Operation aborted\" : assistantMsg.errorMessage || \"Error\";\n\t\t\t\t\t\tfor (const [toolCallId, component] of this.pendingTools.entries()) {\n\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: errorMessage }],\n\t\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthis.pendingTools.clear();\n\t\t\t\t\t}\n\n\t\t\t\t\t// Keep the streaming component - it's now the final assistant message\n\t\t\t\t\tthis.streamingComponent = null;\n\n\t\t\t\t\t// Invalidate footer cache to refresh git branch (in case agent executed git commands)\n\t\t\t\t\tthis.footer.invalidate();\n\t\t\t\t}\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\tbreak;\n\n\t\t\tcase \"tool_execution_start\": {\n\t\t\t\t// Component should already exist from message_update, but create if missing\n\t\t\t\tif (!this.pendingTools.has(event.toolCallId)) {\n\t\t\t\t\tconst component = new ToolExecutionComponent(event.toolName, event.args);\n\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\tthis.pendingTools.set(event.toolCallId, component);\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase \"tool_execution_end\": {\n\t\t\t\t// Update the existing tool component with the result\n\t\t\t\tconst component = this.pendingTools.get(event.toolCallId);\n\t\t\t\tif (component) {\n\t\t\t\t\t// Convert result to the format expected by updateResult\n\t\t\t\t\tconst resultData =\n\t\t\t\t\t\ttypeof event.result === \"string\"\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: event.result }],\n\t\t\t\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t\t\t\t\tisError: event.isError,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\t\tcontent: event.result.content,\n\t\t\t\t\t\t\t\t\tdetails: event.result.details,\n\t\t\t\t\t\t\t\t\tisError: event.isError,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\tcomponent.updateResult(resultData);\n\t\t\t\t\tthis.pendingTools.delete(event.toolCallId);\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcase \"agent_end\":\n\t\t\t\t// Stop loading animation\n\t\t\t\tif (this.loadingAnimation) {\n\t\t\t\t\tthis.loadingAnimation.stop();\n\t\t\t\t\tthis.loadingAnimation = null;\n\t\t\t\t\tthis.statusContainer.clear();\n\t\t\t\t}\n\t\t\t\tif (this.streamingComponent) {\n\t\t\t\t\tthis.chatContainer.removeChild(this.streamingComponent);\n\t\t\t\t\tthis.streamingComponent = null;\n\t\t\t\t}\n\t\t\t\tthis.pendingTools.clear();\n\t\t\t\t// Note: Don't need to re-enable submit - we never disable it\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\tbreak;\n\t\t}\n\t}\n\n\tprivate addMessageToChat(message: Message): void {\n\t\tif (message.role === \"user\") {\n\t\t\tconst userMsg = message;\n\t\t\t// Extract text content from content blocks\n\t\t\tconst textBlocks =\n\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\tif (textContent) {\n\t\t\t\tconst userComponent = new UserMessageComponent(textContent, this.isFirstUserMessage);\n\t\t\t\tthis.chatContainer.addChild(userComponent);\n\t\t\t\tthis.isFirstUserMessage = false;\n\t\t\t}\n\t\t} else if (message.role === \"assistant\") {\n\t\t\tconst assistantMsg = message;\n\n\t\t\t// Add assistant message component\n\t\t\tconst assistantComponent = new AssistantMessageComponent(assistantMsg, this.hideThinkingBlock);\n\t\t\tthis.chatContainer.addChild(assistantComponent);\n\t\t}\n\t\t// Note: tool calls and results are now handled via tool_execution_start/end events\n\t}\n\n\trenderInitialMessages(state: AgentState): void {\n\t\t// Render all existing messages (for --continue mode)\n\t\t// Reset first user message flag for initial render\n\t\tthis.isFirstUserMessage = true;\n\n\t\t// Update footer with loaded state\n\t\tthis.footer.updateState(state);\n\n\t\t// Update editor border color based on current thinking level\n\t\tthis.updateEditorBorderColor();\n\n\t\t// Get compaction info if any\n\t\tconst compactionEntry = getLatestCompactionEntry(this.sessionManager.loadEntries());\n\n\t\t// Render messages\n\t\tfor (let i = 0; i < state.messages.length; i++) {\n\t\t\tconst message = state.messages[i];\n\n\t\t\tif (message.role === \"user\") {\n\t\t\t\tconst userMsg = message;\n\t\t\t\tconst textBlocks =\n\t\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\t\tif (textContent) {\n\t\t\t\t\t// Check if this is a compaction summary message\n\t\t\t\t\tif (textContent.startsWith(SUMMARY_PREFIX) && compactionEntry) {\n\t\t\t\t\t\tconst summary = textContent.slice(SUMMARY_PREFIX.length, -SUMMARY_SUFFIX.length);\n\t\t\t\t\t\tconst component = new CompactionComponent(compactionEntry.tokensBefore, summary);\n\t\t\t\t\t\tcomponent.setExpanded(this.toolOutputExpanded);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconst userComponent = new UserMessageComponent(textContent, this.isFirstUserMessage);\n\t\t\t\t\t\tthis.chatContainer.addChild(userComponent);\n\t\t\t\t\t\tthis.isFirstUserMessage = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\tconst assistantComponent = new AssistantMessageComponent(assistantMsg, this.hideThinkingBlock);\n\t\t\t\tthis.chatContainer.addChild(assistantComponent);\n\n\t\t\t\t// Create tool execution components for any tool calls\n\t\t\t\tfor (const content of assistantMsg.content) {\n\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\tconst component = new ToolExecutionComponent(content.name, content.arguments);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\n\t\t\t\t\t\t// If message was aborted/errored, immediately mark tool as failed\n\t\t\t\t\t\tif (assistantMsg.stopReason === \"aborted\" || assistantMsg.stopReason === \"error\") {\n\t\t\t\t\t\t\tconst errorMessage =\n\t\t\t\t\t\t\t\tassistantMsg.stopReason === \"aborted\"\n\t\t\t\t\t\t\t\t\t? \"Operation aborted\"\n\t\t\t\t\t\t\t\t\t: assistantMsg.errorMessage || \"Error\";\n\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: errorMessage }],\n\t\t\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Store in map so we can update with results later\n\t\t\t\t\t\t\tthis.pendingTools.set(content.id, component);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"toolResult\") {\n\t\t\t\t// Update existing tool execution component with results\t\t\t\t;\n\t\t\t\tconst component = this.pendingTools.get(message.toolCallId);\n\t\t\t\tif (component) {\n\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\tcontent: message.content,\n\t\t\t\t\t\tdetails: message.details,\n\t\t\t\t\t\tisError: message.isError,\n\t\t\t\t\t});\n\t\t\t\t\t// Remove from pending map since it's complete\n\t\t\t\t\tthis.pendingTools.delete(message.toolCallId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t// Clear pending tools after rendering initial messages\n\t\tthis.pendingTools.clear();\n\n\t\t// Populate editor history with user messages from the session (oldest first so newest is at index 0)\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"user\") {\n\t\t\t\tconst textBlocks =\n\t\t\t\t\ttypeof message.content === \"string\"\n\t\t\t\t\t\t? [{ type: \"text\", text: message.content }]\n\t\t\t\t\t\t: message.content.filter((c) => c.type === \"text\");\n\t\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\t\t// Skip compaction summary messages\n\t\t\t\tif (textContent && !textContent.startsWith(SUMMARY_PREFIX)) {\n\t\t\t\t\tthis.editor.addToHistory(textContent);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tasync getUserInput(): Promise<string> {\n\t\treturn new Promise((resolve) => {\n\t\t\tthis.onInputCallback = (text: string) => {\n\t\t\t\tthis.onInputCallback = undefined;\n\t\t\t\tresolve(text);\n\t\t\t};\n\t\t});\n\t}\n\n\tprivate rebuildChatFromMessages(): void {\n\t\t// Reset state and re-render messages from agent state\n\t\tthis.isFirstUserMessage = true;\n\t\tthis.pendingTools.clear();\n\n\t\t// Get compaction info if any\n\t\tconst compactionEntry = getLatestCompactionEntry(this.sessionManager.loadEntries());\n\n\t\tfor (const message of this.agent.state.messages) {\n\t\t\tif (message.role === \"user\") {\n\t\t\t\tconst userMsg = message;\n\t\t\t\tconst textBlocks =\n\t\t\t\t\ttypeof userMsg.content === \"string\"\n\t\t\t\t\t\t? [{ type: \"text\", text: userMsg.content }]\n\t\t\t\t\t\t: userMsg.content.filter((c) => c.type === \"text\");\n\t\t\t\tconst textContent = textBlocks.map((c) => c.text).join(\"\");\n\t\t\t\tif (textContent) {\n\t\t\t\t\t// Check if this is a compaction summary message\n\t\t\t\t\tif (textContent.startsWith(SUMMARY_PREFIX) && compactionEntry) {\n\t\t\t\t\t\tconst summary = textContent.slice(SUMMARY_PREFIX.length, -SUMMARY_SUFFIX.length);\n\t\t\t\t\t\tconst component = new CompactionComponent(compactionEntry.tokensBefore, summary);\n\t\t\t\t\t\tcomponent.setExpanded(this.toolOutputExpanded);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconst userComponent = new UserMessageComponent(textContent, this.isFirstUserMessage);\n\t\t\t\t\t\tthis.chatContainer.addChild(userComponent);\n\t\t\t\t\t\tthis.isFirstUserMessage = false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message;\n\t\t\t\tconst assistantComponent = new AssistantMessageComponent(assistantMsg, this.hideThinkingBlock);\n\t\t\t\tthis.chatContainer.addChild(assistantComponent);\n\n\t\t\t\tfor (const content of assistantMsg.content) {\n\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\tconst component = new ToolExecutionComponent(content.name, content.arguments);\n\t\t\t\t\t\tthis.chatContainer.addChild(component);\n\t\t\t\t\t\tthis.pendingTools.set(content.id, component);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (message.role === \"toolResult\") {\n\t\t\t\tconst component = this.pendingTools.get(message.toolCallId);\n\t\t\t\tif (component) {\n\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\tcontent: message.content,\n\t\t\t\t\t\tdetails: message.details,\n\t\t\t\t\t\tisError: message.isError,\n\t\t\t\t\t});\n\t\t\t\t\tthis.pendingTools.delete(message.toolCallId);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis.pendingTools.clear();\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleCtrlC(): void {\n\t\t// Handle Ctrl+C double-press logic\n\t\tconst now = Date.now();\n\t\tconst timeSinceLastCtrlC = now - this.lastSigintTime;\n\n\t\tif (timeSinceLastCtrlC < 500) {\n\t\t\t// Second Ctrl+C within 500ms - exit\n\t\t\tthis.stop();\n\t\t\tprocess.exit(0);\n\t\t} else {\n\t\t\t// First Ctrl+C - clear the editor\n\t\t\tthis.clearEditor();\n\t\t\tthis.lastSigintTime = now;\n\t\t}\n\t}\n\n\tprivate updateEditorBorderColor(): void {\n\t\tconst level = this.agent.state.thinkingLevel || \"off\";\n\t\tthis.editor.borderColor = theme.getThinkingBorderColor(level);\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate cycleThinkingLevel(): void {\n\t\t// Only cycle if model supports thinking\n\t\tif (!this.agent.state.model?.reasoning) {\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Current model does not support thinking\"), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\tconst levels: ThinkingLevel[] = [\"off\", \"minimal\", \"low\", \"medium\", \"high\"];\n\t\tconst currentLevel = this.agent.state.thinkingLevel || \"off\";\n\t\tconst currentIndex = levels.indexOf(currentLevel);\n\t\tconst nextIndex = (currentIndex + 1) % levels.length;\n\t\tconst nextLevel = levels[nextIndex];\n\n\t\t// Apply the new thinking level\n\t\tthis.agent.setThinkingLevel(nextLevel);\n\n\t\t// Save thinking level change to session and settings\n\t\tthis.sessionManager.saveThinkingLevelChange(nextLevel);\n\t\tthis.settingsManager.setDefaultThinkingLevel(nextLevel);\n\n\t\t// Update border color\n\t\tthis.updateEditorBorderColor();\n\n\t\t// Show brief notification\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Thinking level: ${nextLevel}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate async cycleModel(): Promise<void> {\n\t\t// Use scoped models if available, otherwise all available models\n\t\tif (this.scopedModels.length > 0) {\n\t\t\t// Use scoped models with thinking levels\n\t\t\tif (this.scopedModels.length === 1) {\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Only one model in scope\"), 1, 0));\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst currentModel = this.agent.state.model;\n\t\t\tlet currentIndex = this.scopedModels.findIndex(\n\t\t\t\t(sm) => sm.model.id === currentModel?.id && sm.model.provider === currentModel?.provider,\n\t\t\t);\n\n\t\t\t// If current model not in scope, start from first\n\t\t\tif (currentIndex === -1) {\n\t\t\t\tcurrentIndex = 0;\n\t\t\t}\n\n\t\t\tconst nextIndex = (currentIndex + 1) % this.scopedModels.length;\n\t\t\tconst nextEntry = this.scopedModels[nextIndex];\n\t\t\tconst nextModel = nextEntry.model;\n\t\t\tconst nextThinking = nextEntry.thinkingLevel;\n\n\t\t\t// Validate API key\n\t\t\tconst apiKey = await getApiKeyForModel(nextModel);\n\t\t\tif (!apiKey) {\n\t\t\t\tthis.showError(`No API key for ${nextModel.provider}/${nextModel.id}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Switch model\n\t\t\tthis.agent.setModel(nextModel);\n\n\t\t\t// Save model change to session and settings\n\t\t\tthis.sessionManager.saveModelChange(nextModel.provider, nextModel.id);\n\t\t\tthis.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);\n\n\t\t\t// Apply thinking level (silently use \"off\" if model doesn't support thinking)\n\t\t\tconst effectiveThinking = nextModel.reasoning ? nextThinking : \"off\";\n\t\t\tthis.agent.setThinkingLevel(effectiveThinking);\n\t\t\tthis.sessionManager.saveThinkingLevelChange(effectiveThinking);\n\t\t\tthis.settingsManager.setDefaultThinkingLevel(effectiveThinking);\n\t\t\tthis.updateEditorBorderColor();\n\n\t\t\t// Show notification\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tconst thinkingStr = nextModel.reasoning && nextThinking !== \"off\" ? ` (thinking: ${nextThinking})` : \"\";\n\t\t\tthis.chatContainer.addChild(\n\t\t\t\tnew Text(theme.fg(\"dim\", `Switched to ${nextModel.name || nextModel.id}${thinkingStr}`), 1, 0),\n\t\t\t);\n\t\t\tthis.ui.requestRender();\n\t\t} else {\n\t\t\t// Fallback to all available models (no thinking level changes)\n\t\t\tconst { models: availableModels, error } = await getAvailableModels();\n\t\t\tif (error) {\n\t\t\t\tthis.showError(`Failed to load models: ${error}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (availableModels.length === 0) {\n\t\t\t\tthis.showError(\"No models available to cycle\");\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (availableModels.length === 1) {\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Only one model available\"), 1, 0));\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst currentModel = this.agent.state.model;\n\t\t\tlet currentIndex = availableModels.findIndex(\n\t\t\t\t(m) => m.id === currentModel?.id && m.provider === currentModel?.provider,\n\t\t\t);\n\n\t\t\t// If current model not in scope, start from first\n\t\t\tif (currentIndex === -1) {\n\t\t\t\tcurrentIndex = 0;\n\t\t\t}\n\n\t\t\tconst nextIndex = (currentIndex + 1) % availableModels.length;\n\t\t\tconst nextModel = availableModels[nextIndex];\n\n\t\t\t// Validate API key\n\t\t\tconst apiKey = await getApiKeyForModel(nextModel);\n\t\t\tif (!apiKey) {\n\t\t\t\tthis.showError(`No API key for ${nextModel.provider}/${nextModel.id}`);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Switch model\n\t\t\tthis.agent.setModel(nextModel);\n\n\t\t\t// Save model change to session and settings\n\t\t\tthis.sessionManager.saveModelChange(nextModel.provider, nextModel.id);\n\t\t\tthis.settingsManager.setDefaultModelAndProvider(nextModel.provider, nextModel.id);\n\n\t\t\t// Show notification\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Switched to ${nextModel.name || nextModel.id}`), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t}\n\t}\n\n\tprivate toggleToolOutputExpansion(): void {\n\t\tthis.toolOutputExpanded = !this.toolOutputExpanded;\n\n\t\t// Update all tool execution and compaction components\n\t\tfor (const child of this.chatContainer.children) {\n\t\t\tif (child instanceof ToolExecutionComponent) {\n\t\t\t\tchild.setExpanded(this.toolOutputExpanded);\n\t\t\t} else if (child instanceof CompactionComponent) {\n\t\t\t\tchild.setExpanded(this.toolOutputExpanded);\n\t\t\t}\n\t\t}\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate toggleThinkingBlockVisibility(): void {\n\t\tthis.hideThinkingBlock = !this.hideThinkingBlock;\n\t\tthis.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);\n\n\t\t// Update all assistant message components and rebuild their content\n\t\tfor (const child of this.chatContainer.children) {\n\t\t\tif (child instanceof AssistantMessageComponent) {\n\t\t\t\tchild.setHideThinkingBlock(this.hideThinkingBlock);\n\t\t\t}\n\t\t}\n\n\t\t// Rebuild chat to apply visibility change\n\t\tthis.chatContainer.clear();\n\t\tthis.rebuildChatFromMessages();\n\n\t\t// Show brief notification\n\t\tconst status = this.hideThinkingBlock ? \"hidden\" : \"visible\";\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Thinking blocks: ${status}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tclearEditor(): void {\n\t\tthis.editor.setText(\"\");\n\t\tthis.ui.requestRender();\n\t}\n\n\tshowError(errorMessage: string): void {\n\t\t// Show error message in the chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"error\", `Error: ${errorMessage}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tshowWarning(warningMessage: string): void {\n\t\t// Show warning message in the chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"warning\", `Warning: ${warningMessage}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tshowNewVersionNotification(newVersion: string): void {\n\t\t// Show new version notification in the chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new DynamicBorder((text) => theme.fg(\"warning\", text)));\n\t\tthis.chatContainer.addChild(\n\t\t\tnew Text(\n\t\t\t\ttheme.bold(theme.fg(\"warning\", \"Update Available\")) +\n\t\t\t\t\t\"\\n\" +\n\t\t\t\t\ttheme.fg(\"muted\", `New version ${newVersion} is available. Run: `) +\n\t\t\t\t\ttheme.fg(\"accent\", \"npm install -g @mariozechner/pi-coding-agent\"),\n\t\t\t\t1,\n\t\t\t\t0,\n\t\t\t),\n\t\t);\n\t\tthis.chatContainer.addChild(new DynamicBorder((text) => theme.fg(\"warning\", text)));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate showThinkingSelector(): void {\n\t\t// Create thinking selector with current level\n\t\tthis.thinkingSelector = new ThinkingSelectorComponent(\n\t\t\tthis.agent.state.thinkingLevel,\n\t\t\t(level) => {\n\t\t\t\t// Apply the selected thinking level\n\t\t\t\tthis.agent.setThinkingLevel(level);\n\n\t\t\t\t// Save thinking level change to session and settings\n\t\t\t\tthis.sessionManager.saveThinkingLevelChange(level);\n\t\t\t\tthis.settingsManager.setDefaultThinkingLevel(level);\n\n\t\t\t\t// Update border color\n\t\t\t\tthis.updateEditorBorderColor();\n\n\t\t\t\t// Show confirmation message with proper spacing\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Thinking level: ${level}`), 1, 0);\n\t\t\t\tthis.chatContainer.addChild(confirmText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideThinkingSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideThinkingSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.thinkingSelector);\n\t\tthis.ui.setFocus(this.thinkingSelector.getSelectList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideThinkingSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.thinkingSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showQueueModeSelector(): void {\n\t\t// Create queue mode selector with current mode\n\t\tthis.queueModeSelector = new QueueModeSelectorComponent(\n\t\t\tthis.agent.getQueueMode(),\n\t\t\t(mode) => {\n\t\t\t\t// Apply the selected queue mode\n\t\t\t\tthis.agent.setQueueMode(mode);\n\n\t\t\t\t// Save queue mode to settings\n\t\t\t\tthis.settingsManager.setQueueMode(mode);\n\n\t\t\t\t// Show confirmation message with proper spacing\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Queue mode: ${mode}`), 1, 0);\n\t\t\t\tthis.chatContainer.addChild(confirmText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideQueueModeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideQueueModeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.queueModeSelector);\n\t\tthis.ui.setFocus(this.queueModeSelector.getSelectList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideQueueModeSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.queueModeSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showThemeSelector(): void {\n\t\t// Get current theme from settings\n\t\tconst currentTheme = this.settingsManager.getTheme() || \"dark\";\n\n\t\t// Create theme selector\n\t\tthis.themeSelector = new ThemeSelectorComponent(\n\t\t\tcurrentTheme,\n\t\t\t(themeName) => {\n\t\t\t\t// Apply the selected theme\n\t\t\t\tconst result = setTheme(themeName);\n\n\t\t\t\t// Save theme to settings\n\t\t\t\tthis.settingsManager.setTheme(themeName);\n\n\t\t\t\t// Invalidate all components to clear cached rendering\n\t\t\t\tthis.ui.invalidate();\n\n\t\t\t\t// Show confirmation or error message\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tif (result.success) {\n\t\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Theme: ${themeName}`), 1, 0);\n\t\t\t\t\tthis.chatContainer.addChild(confirmText);\n\t\t\t\t} else {\n\t\t\t\t\tconst errorText = new Text(\n\t\t\t\t\t\ttheme.fg(\"error\", `Failed to load theme \"${themeName}\": ${result.error}\\nFell back to dark theme.`),\n\t\t\t\t\t\t1,\n\t\t\t\t\t\t0,\n\t\t\t\t\t);\n\t\t\t\t\tthis.chatContainer.addChild(errorText);\n\t\t\t\t}\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideThemeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideThemeSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t(themeName) => {\n\t\t\t\t// Preview theme on selection change\n\t\t\t\tconst result = setTheme(themeName);\n\t\t\t\tif (result.success) {\n\t\t\t\t\tthis.ui.invalidate();\n\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t}\n\t\t\t\t// If failed, theme already fell back to dark, just don't re-render\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.themeSelector);\n\t\tthis.ui.setFocus(this.themeSelector.getSelectList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideThemeSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.themeSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showModelSelector(): void {\n\t\t// Create model selector with current model\n\t\tthis.modelSelector = new ModelSelectorComponent(\n\t\t\tthis.ui,\n\t\t\tthis.agent.state.model,\n\t\t\tthis.settingsManager,\n\t\t\t(model) => {\n\t\t\t\t// Apply the selected model\n\t\t\t\tthis.agent.setModel(model);\n\n\t\t\t\t// Save model change to session\n\t\t\t\tthis.sessionManager.saveModelChange(model.provider, model.id);\n\n\t\t\t\t// Show confirmation message with proper spacing\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tconst confirmText = new Text(theme.fg(\"dim\", `Model: ${model.id}`), 1, 0);\n\t\t\t\tthis.chatContainer.addChild(confirmText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideModelSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideModelSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.modelSelector);\n\t\tthis.ui.setFocus(this.modelSelector);\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideModelSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.modelSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showUserMessageSelector(): void {\n\t\t// Read from session file directly to see ALL historical user messages\n\t\t// (including those before compaction events)\n\t\tconst entries = this.sessionManager.loadEntries();\n\t\tconst userMessages: Array<{ index: number; text: string }> = [];\n\n\t\tconst getUserMessageText = (content: string | Array<{ type: string; text?: string }>): string => {\n\t\t\tif (typeof content === \"string\") return content;\n\t\t\tif (Array.isArray(content)) {\n\t\t\t\treturn content\n\t\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t\t.map((c) => c.text)\n\t\t\t\t\t.join(\"\");\n\t\t\t}\n\t\t\treturn \"\";\n\t\t};\n\n\t\tfor (let i = 0; i < entries.length; i++) {\n\t\t\tconst entry = entries[i];\n\t\t\tif (entry.type !== \"message\") continue;\n\t\t\tif (entry.message.role !== \"user\") continue;\n\n\t\t\tconst textContent = getUserMessageText(entry.message.content);\n\t\t\tif (textContent) {\n\t\t\t\tuserMessages.push({ index: i, text: textContent });\n\t\t\t}\n\t\t}\n\n\t\t// Don't show selector if there are no messages or only one message\n\t\tif (userMessages.length <= 1) {\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"No messages to branch from\"), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\t// Create user message selector\n\t\tthis.userMessageSelector = new UserMessageSelectorComponent(\n\t\t\tuserMessages,\n\t\t\t(entryIndex) => {\n\t\t\t\t// Get the selected user message text to put in the editor\n\t\t\t\tconst selectedEntry = entries[entryIndex];\n\t\t\t\tif (selectedEntry.type !== \"message\") return;\n\t\t\t\tif (selectedEntry.message.role !== \"user\") return;\n\n\t\t\t\tconst selectedText = getUserMessageText(selectedEntry.message.content);\n\n\t\t\t\t// Create a branched session by copying entries up to (but not including) the selected entry\n\t\t\t\tconst newSessionFile = this.sessionManager.createBranchedSessionFromEntries(entries, entryIndex);\n\n\t\t\t\t// Set the new session file as active\n\t\t\t\tthis.sessionManager.setSessionFile(newSessionFile);\n\n\t\t\t\t// Reload the session\n\t\t\t\tconst loaded = loadSessionFromEntries(this.sessionManager.loadEntries());\n\t\t\t\tthis.agent.replaceMessages(loaded.messages);\n\n\t\t\t\t// Clear and re-render the chat\n\t\t\t\tthis.chatContainer.clear();\n\t\t\t\tthis.isFirstUserMessage = true;\n\t\t\t\tthis.renderInitialMessages(this.agent.state);\n\n\t\t\t\t// Show confirmation message\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Branched to new session\"), 1, 0));\n\n\t\t\t\t// Put the selected message in the editor\n\t\t\t\tthis.editor.setText(selectedText);\n\n\t\t\t\t// Hide selector and show editor again\n\t\t\t\tthis.hideUserMessageSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideUserMessageSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.userMessageSelector);\n\t\tthis.ui.setFocus(this.userMessageSelector.getMessageList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideUserMessageSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.userMessageSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate showSessionSelector(): void {\n\t\t// Create session selector\n\t\tthis.sessionSelector = new SessionSelectorComponent(\n\t\t\tthis.sessionManager,\n\t\t\tasync (sessionPath) => {\n\t\t\t\tthis.hideSessionSelector();\n\t\t\t\tawait this.handleResumeSession(sessionPath);\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Just hide the selector\n\t\t\t\tthis.hideSessionSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.sessionSelector);\n\t\tthis.ui.setFocus(this.sessionSelector.getSessionList());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate async handleResumeSession(sessionPath: string): Promise<void> {\n\t\t// Unsubscribe first to prevent processing events during transition\n\t\tthis.unsubscribe?.();\n\n\t\t// Abort and wait for completion\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\n\t\t// Stop loading animation\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.statusContainer.clear();\n\n\t\t// Clear UI state\n\t\tthis.queuedMessages = [];\n\t\tthis.pendingMessagesContainer.clear();\n\t\tthis.streamingComponent = null;\n\t\tthis.pendingTools.clear();\n\n\t\t// Set the selected session as active\n\t\tthis.sessionManager.setSessionFile(sessionPath);\n\n\t\t// Reload the session\n\t\tconst loaded = loadSessionFromEntries(this.sessionManager.loadEntries());\n\t\tthis.agent.replaceMessages(loaded.messages);\n\n\t\t// Restore model if saved in session\n\t\tconst savedModel = this.sessionManager.loadModel();\n\t\tif (savedModel) {\n\t\t\tconst availableModels = (await getAvailableModels()).models;\n\t\t\tconst match = availableModels.find((m) => m.provider === savedModel.provider && m.id === savedModel.modelId);\n\t\t\tif (match) {\n\t\t\t\tthis.agent.setModel(match);\n\t\t\t}\n\t\t}\n\n\t\t// Restore thinking level if saved in session\n\t\tconst savedThinking = this.sessionManager.loadThinkingLevel();\n\t\tif (savedThinking) {\n\t\t\tthis.agent.setThinkingLevel(savedThinking as ThinkingLevel);\n\t\t}\n\n\t\t// Resubscribe to agent\n\t\tthis.subscribeToAgent();\n\n\t\t// Clear and re-render the chat\n\t\tthis.chatContainer.clear();\n\t\tthis.isFirstUserMessage = true;\n\t\tthis.renderInitialMessages(this.agent.state);\n\n\t\t// Show confirmation message\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Resumed session\"), 1, 0));\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideSessionSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.sessionSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate async showOAuthSelector(mode: \"login\" | \"logout\"): Promise<void> {\n\t\t// For logout mode, filter to only show logged-in providers\n\t\tlet providersToShow: string[] = [];\n\t\tif (mode === \"logout\") {\n\t\t\tconst loggedInProviders = listOAuthProviders();\n\t\t\tif (loggedInProviders.length === 0) {\n\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\tnew Text(theme.fg(\"dim\", \"No OAuth providers logged in. Use /login first.\"), 1, 0),\n\t\t\t\t);\n\t\t\t\tthis.ui.requestRender();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tprovidersToShow = loggedInProviders;\n\t\t}\n\n\t\t// Create OAuth selector\n\t\tthis.oauthSelector = new OAuthSelectorComponent(\n\t\t\tmode,\n\t\t\tasync (providerId: string) => {\n\t\t\t\t// Hide selector first\n\t\t\t\tthis.hideOAuthSelector();\n\n\t\t\t\tif (mode === \"login\") {\n\t\t\t\t\t// Handle login\n\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Logging in to ${providerId}...`), 1, 0));\n\t\t\t\t\tthis.ui.requestRender();\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait login(\n\t\t\t\t\t\t\tproviderId as SupportedOAuthProvider,\n\t\t\t\t\t\t\t(url: string) => {\n\t\t\t\t\t\t\t\t// Show auth URL to user\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"accent\", \"Opening browser to:\"), 1, 0));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"accent\", url), 1, 0));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\t\t\tnew Text(theme.fg(\"warning\", \"Paste the authorization code below:\"), 1, 0),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tthis.ui.requestRender();\n\n\t\t\t\t\t\t\t\t// Open URL in browser\n\t\t\t\t\t\t\t\tconst openCmd =\n\t\t\t\t\t\t\t\t\tprocess.platform === \"darwin\" ? \"open\" : process.platform === \"win32\" ? \"start\" : \"xdg-open\";\n\t\t\t\t\t\t\t\texec(`${openCmd} \"${url}\"`);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tasync () => {\n\t\t\t\t\t\t\t\t// Prompt for code with a simple Input\n\t\t\t\t\t\t\t\treturn new Promise<string>((resolve) => {\n\t\t\t\t\t\t\t\t\tconst codeInput = new Input();\n\t\t\t\t\t\t\t\t\tcodeInput.onSubmit = () => {\n\t\t\t\t\t\t\t\t\t\tconst code = codeInput.getValue();\n\t\t\t\t\t\t\t\t\t\t// Restore editor\n\t\t\t\t\t\t\t\t\t\tthis.editorContainer.clear();\n\t\t\t\t\t\t\t\t\t\tthis.editorContainer.addChild(this.editor);\n\t\t\t\t\t\t\t\t\t\tthis.ui.setFocus(this.editor);\n\t\t\t\t\t\t\t\t\t\tresolve(code);\n\t\t\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\t\t\tthis.editorContainer.clear();\n\t\t\t\t\t\t\t\t\tthis.editorContainer.addChild(codeInput);\n\t\t\t\t\t\t\t\t\tthis.ui.setFocus(codeInput);\n\t\t\t\t\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\t// Success - invalidate OAuth cache so footer updates\n\t\t\t\t\t\tinvalidateOAuthCache();\n\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\tnew Text(theme.fg(\"success\", `✓ Successfully logged in to ${providerId}`), 1, 0),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Tokens saved to ${getOAuthPath()}`), 1, 0));\n\t\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t\t} catch (error: any) {\n\t\t\t\t\t\tthis.showError(`Login failed: ${error.message}`);\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Handle logout\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait logout(providerId as SupportedOAuthProvider);\n\n\t\t\t\t\t\t// Invalidate OAuth cache so footer updates\n\t\t\t\t\t\tinvalidateOAuthCache();\n\t\t\t\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\tnew Text(theme.fg(\"success\", `✓ Successfully logged out of ${providerId}`), 1, 0),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthis.chatContainer.addChild(\n\t\t\t\t\t\t\tnew Text(theme.fg(\"dim\", `Credentials removed from ${getOAuthPath()}`), 1, 0),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthis.ui.requestRender();\n\t\t\t\t\t} catch (error: any) {\n\t\t\t\t\t\tthis.showError(`Logout failed: ${error.message}`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t\t() => {\n\t\t\t\t// Cancel - just hide the selector\n\t\t\t\tthis.hideOAuthSelector();\n\t\t\t\tthis.ui.requestRender();\n\t\t\t},\n\t\t);\n\n\t\t// Replace editor with selector\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.oauthSelector);\n\t\tthis.ui.setFocus(this.oauthSelector);\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate hideOAuthSelector(): void {\n\t\t// Replace selector with editor in the container\n\t\tthis.editorContainer.clear();\n\t\tthis.editorContainer.addChild(this.editor);\n\t\tthis.oauthSelector = null;\n\t\tthis.ui.setFocus(this.editor);\n\t}\n\n\tprivate handleExportCommand(text: string): void {\n\t\t// Parse optional filename from command: /export [filename]\n\t\tconst parts = text.split(/\\s+/);\n\t\tconst outputPath = parts.length > 1 ? parts[1] : undefined;\n\n\t\ttry {\n\t\t\t// Export session to HTML\n\t\t\tconst filePath = exportSessionToHtml(this.sessionManager, this.agent.state, outputPath);\n\n\t\t\t// Show success message in chat - matching thinking level style\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Session exported to: ${filePath}`), 1, 0));\n\t\t\tthis.ui.requestRender();\n\t\t} catch (error: any) {\n\t\t\t// Show error message in chat\n\t\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\t\tthis.chatContainer.addChild(\n\t\t\t\tnew Text(theme.fg(\"error\", `Failed to export session: ${error.message || \"Unknown error\"}`), 1, 0),\n\t\t\t);\n\t\t\tthis.ui.requestRender();\n\t\t}\n\t}\n\n\tprivate handleCopyCommand(): void {\n\t\t// Find the last assistant message\n\t\tconst lastAssistantMessage = this.agent.state.messages\n\t\t\t.slice()\n\t\t\t.reverse()\n\t\t\t.find((m) => m.role === \"assistant\");\n\n\t\tif (!lastAssistantMessage) {\n\t\t\tthis.showError(\"No agent messages to copy yet.\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Extract raw text content from all text blocks\n\t\tlet textContent = \"\";\n\n\t\tfor (const content of lastAssistantMessage.content) {\n\t\t\tif (content.type === \"text\") {\n\t\t\t\ttextContent += content.text;\n\t\t\t}\n\t\t}\n\n\t\tif (!textContent.trim()) {\n\t\t\tthis.showError(\"Last agent message contains no text content.\");\n\t\t\treturn;\n\t\t}\n\n\t\t// Copy to clipboard using cross-platform compatible method\n\t\ttry {\n\t\t\tcopyToClipboard(textContent);\n\t\t} catch (error) {\n\t\t\tthis.showError(error instanceof Error ? error.message : String(error));\n\t\t\treturn;\n\t\t}\n\n\t\t// Show confirmation message\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", \"Copied last agent message to clipboard\"), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleSessionCommand(): void {\n\t\t// Get session info\n\t\tconst sessionFile = this.sessionManager.getSessionFile();\n\t\tconst state = this.agent.state;\n\n\t\t// Count messages\n\t\tconst userMessages = state.messages.filter((m) => m.role === \"user\").length;\n\t\tconst assistantMessages = state.messages.filter((m) => m.role === \"assistant\").length;\n\t\tconst toolResults = state.messages.filter((m) => m.role === \"toolResult\").length;\n\t\tconst totalMessages = state.messages.length;\n\n\t\t// Count tool calls from assistant messages\n\t\tlet toolCalls = 0;\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\ttoolCalls += assistantMsg.content.filter((c) => c.type === \"toolCall\").length;\n\t\t\t}\n\t\t}\n\n\t\t// Calculate cumulative usage from all assistant messages (same as footer)\n\t\tlet totalInput = 0;\n\t\tlet totalOutput = 0;\n\t\tlet totalCacheRead = 0;\n\t\tlet totalCacheWrite = 0;\n\t\tlet totalCost = 0;\n\n\t\tfor (const message of state.messages) {\n\t\t\tif (message.role === \"assistant\") {\n\t\t\t\tconst assistantMsg = message as AssistantMessage;\n\t\t\t\ttotalInput += assistantMsg.usage.input;\n\t\t\t\ttotalOutput += assistantMsg.usage.output;\n\t\t\t\ttotalCacheRead += assistantMsg.usage.cacheRead;\n\t\t\t\ttotalCacheWrite += assistantMsg.usage.cacheWrite;\n\t\t\t\ttotalCost += assistantMsg.usage.cost.total;\n\t\t\t}\n\t\t}\n\n\t\tconst totalTokens = totalInput + totalOutput + totalCacheRead + totalCacheWrite;\n\n\t\t// Build info text\n\t\tlet info = `${theme.bold(\"Session Info\")}\\n\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"File:\")} ${sessionFile}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"ID:\")} ${this.sessionManager.getSessionId()}\\n\\n`;\n\t\tinfo += `${theme.bold(\"Messages\")}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"User:\")} ${userMessages}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Assistant:\")} ${assistantMessages}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Tool Calls:\")} ${toolCalls}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Tool Results:\")} ${toolResults}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Total:\")} ${totalMessages}\\n\\n`;\n\t\tinfo += `${theme.bold(\"Tokens\")}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Input:\")} ${totalInput.toLocaleString()}\\n`;\n\t\tinfo += `${theme.fg(\"dim\", \"Output:\")} ${totalOutput.toLocaleString()}\\n`;\n\t\tif (totalCacheRead > 0) {\n\t\t\tinfo += `${theme.fg(\"dim\", \"Cache Read:\")} ${totalCacheRead.toLocaleString()}\\n`;\n\t\t}\n\t\tif (totalCacheWrite > 0) {\n\t\t\tinfo += `${theme.fg(\"dim\", \"Cache Write:\")} ${totalCacheWrite.toLocaleString()}\\n`;\n\t\t}\n\t\tinfo += `${theme.fg(\"dim\", \"Total:\")} ${totalTokens.toLocaleString()}\\n`;\n\n\t\tif (totalCost > 0) {\n\t\t\tinfo += `\\n${theme.bold(\"Cost\")}\\n`;\n\t\t\tinfo += `${theme.fg(\"dim\", \"Total:\")} ${totalCost.toFixed(4)}`;\n\t\t}\n\n\t\t// Show info in chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(info, 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleChangelogCommand(): void {\n\t\tconst changelogPath = getChangelogPath();\n\t\tconst allEntries = parseChangelog(changelogPath);\n\n\t\t// Show all entries in reverse order (oldest first, newest last)\n\t\tconst changelogMarkdown =\n\t\t\tallEntries.length > 0\n\t\t\t\t? allEntries\n\t\t\t\t\t\t.reverse()\n\t\t\t\t\t\t.map((e) => e.content)\n\t\t\t\t\t\t.join(\"\\n\\n\")\n\t\t\t\t: \"No changelog entries found.\";\n\n\t\t// Display in chat\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new DynamicBorder());\n\t\tthis.ui.addChild(new Text(theme.bold(theme.fg(\"accent\", \"What's New\")), 1, 0));\n\t\tthis.ui.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, getMarkdownTheme()));\n\t\tthis.chatContainer.addChild(new DynamicBorder());\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate async handleClearCommand(): Promise<void> {\n\t\t// Unsubscribe first to prevent processing abort events\n\t\tthis.unsubscribe?.();\n\n\t\t// Abort and wait for completion\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\n\t\t// Stop loading animation\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.statusContainer.clear();\n\n\t\t// Reset agent and session\n\t\tthis.agent.reset();\n\t\tthis.sessionManager.reset();\n\n\t\t// Resubscribe to agent\n\t\tthis.subscribeToAgent();\n\n\t\t// Clear UI state\n\t\tthis.chatContainer.clear();\n\t\tthis.pendingMessagesContainer.clear();\n\t\tthis.queuedMessages = [];\n\t\tthis.streamingComponent = null;\n\t\tthis.pendingTools.clear();\n\t\tthis.isFirstUserMessage = true;\n\n\t\t// Show confirmation\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(\n\t\t\tnew Text(theme.fg(\"accent\", \"✓ Context cleared\") + \"\\n\" + theme.fg(\"muted\", \"Started fresh session\"), 1, 1),\n\t\t);\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate handleDebugCommand(): void {\n\t\t// Force a render and capture all lines with their widths\n\t\tconst width = this.ui.terminal.columns;\n\t\tconst allLines = this.ui.render(width);\n\n\t\tconst debugLogPath = getDebugLogPath();\n\t\tconst debugData = [\n\t\t\t`Debug output at ${new Date().toISOString()}`,\n\t\t\t`Terminal width: ${width}`,\n\t\t\t`Total lines: ${allLines.length}`,\n\t\t\t\"\",\n\t\t\t\"=== All rendered lines with visible widths ===\",\n\t\t\t...allLines.map((line, idx) => {\n\t\t\t\tconst vw = visibleWidth(line);\n\t\t\t\tconst escaped = JSON.stringify(line);\n\t\t\t\treturn `[${idx}] (w=${vw}) ${escaped}`;\n\t\t\t}),\n\t\t\t\"\",\n\t\t].join(\"\\n\");\n\n\t\tfs.mkdirSync(path.dirname(debugLogPath), { recursive: true });\n\t\tfs.writeFileSync(debugLogPath, debugData);\n\n\t\t// Show confirmation\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(\n\t\t\tnew Text(theme.fg(\"accent\", \"✓ Debug log written\") + \"\\n\" + theme.fg(\"muted\", debugLogPath), 1, 1),\n\t\t);\n\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate compactionAbortController: AbortController | null = null;\n\n\t/**\n\t * Shared logic to execute context compaction.\n\t * Handles aborting agent, showing loader, performing compaction, updating session/UI.\n\t */\n\tprivate async executeCompaction(customInstructions?: string, isAuto = false): Promise<void> {\n\t\t// Unsubscribe first to prevent processing events during compaction\n\t\tthis.unsubscribe?.();\n\n\t\t// Abort and wait for completion\n\t\tthis.agent.abort();\n\t\tawait this.agent.waitForIdle();\n\n\t\t// Stop loading animation\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.statusContainer.clear();\n\n\t\t// Create abort controller for compaction\n\t\tthis.compactionAbortController = new AbortController();\n\n\t\t// Set up escape handler during compaction\n\t\tconst originalOnEscape = this.editor.onEscape;\n\t\tthis.editor.onEscape = () => {\n\t\t\tif (this.compactionAbortController) {\n\t\t\t\tthis.compactionAbortController.abort();\n\t\t\t}\n\t\t};\n\n\t\t// Show compacting status with loader\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tconst label = isAuto ? \"Auto-compacting context... (esc to cancel)\" : \"Compacting context... (esc to cancel)\";\n\t\tconst compactingLoader = new Loader(\n\t\t\tthis.ui,\n\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\tlabel,\n\t\t);\n\t\tthis.statusContainer.addChild(compactingLoader);\n\t\tthis.ui.requestRender();\n\n\t\ttry {\n\t\t\t// Get API key for current model\n\t\t\tconst apiKey = await getApiKeyForModel(this.agent.state.model);\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for ${this.agent.state.model.provider}`);\n\t\t\t}\n\n\t\t\t// Perform compaction with abort signal\n\t\t\tconst entries = this.sessionManager.loadEntries();\n\t\t\tconst settings = this.settingsManager.getCompactionSettings();\n\t\t\tconst compactionEntry = await compact(\n\t\t\t\tentries,\n\t\t\t\tthis.agent.state.model,\n\t\t\t\tsettings,\n\t\t\t\tapiKey,\n\t\t\t\tthis.compactionAbortController.signal,\n\t\t\t\tcustomInstructions,\n\t\t\t);\n\n\t\t\t// Check if aborted after compact returned\n\t\t\tif (this.compactionAbortController.signal.aborted) {\n\t\t\t\tthrow new Error(\"Compaction cancelled\");\n\t\t\t}\n\n\t\t\t// Save compaction to session\n\t\t\tthis.sessionManager.saveCompaction(compactionEntry);\n\n\t\t\t// Reload session\n\t\t\tconst loaded = loadSessionFromEntries(this.sessionManager.loadEntries());\n\t\t\tthis.agent.replaceMessages(loaded.messages);\n\n\t\t\t// Rebuild UI\n\t\t\tthis.chatContainer.clear();\n\t\t\tthis.rebuildChatFromMessages();\n\n\t\t\t// Add compaction component at current position so user can see/expand the summary\n\t\t\tconst compactionComponent = new CompactionComponent(compactionEntry.tokensBefore, compactionEntry.summary);\n\t\t\tcompactionComponent.setExpanded(this.toolOutputExpanded);\n\t\t\tthis.chatContainer.addChild(compactionComponent);\n\n\t\t\t// Update footer with new state (fixes context % display)\n\t\t\tthis.footer.updateState(this.agent.state);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\tif (message === \"Compaction cancelled\" || (error instanceof Error && error.name === \"AbortError\")) {\n\t\t\t\tthis.showError(\"Compaction cancelled\");\n\t\t\t} else {\n\t\t\t\tthis.showError(`Compaction failed: ${message}`);\n\t\t\t}\n\t\t} finally {\n\t\t\t// Clean up\n\t\t\tcompactingLoader.stop();\n\t\t\tthis.statusContainer.clear();\n\t\t\tthis.compactionAbortController = null;\n\t\t\tthis.editor.onEscape = originalOnEscape;\n\t\t}\n\n\t\t// Resubscribe to agent\n\t\tthis.subscribeToAgent();\n\t}\n\n\tprivate async handleCompactCommand(customInstructions?: string): Promise<void> {\n\t\t// Check if there are any messages to compact\n\t\tconst entries = this.sessionManager.loadEntries();\n\t\tconst messageCount = entries.filter((e) => e.type === \"message\").length;\n\n\t\tif (messageCount < 2) {\n\t\t\tthis.showWarning(\"Nothing to compact (no messages yet)\");\n\t\t\treturn;\n\t\t}\n\n\t\tawait this.executeCompaction(customInstructions, false);\n\t}\n\n\tprivate handleAutocompactCommand(): void {\n\t\tconst currentEnabled = this.settingsManager.getCompactionEnabled();\n\t\tconst newState = !currentEnabled;\n\t\tthis.settingsManager.setCompactionEnabled(newState);\n\t\tthis.footer.setAutoCompactEnabled(newState);\n\n\t\t// Show brief notification (same style as thinking level toggle)\n\t\tthis.chatContainer.addChild(new Spacer(1));\n\t\tthis.chatContainer.addChild(new Text(theme.fg(\"dim\", `Auto-compaction: ${newState ? \"on\" : \"off\"}`), 1, 0));\n\t\tthis.ui.requestRender();\n\t}\n\n\tprivate updatePendingMessagesDisplay(): void {\n\t\tthis.pendingMessagesContainer.clear();\n\n\t\tif (this.queuedMessages.length > 0) {\n\t\t\tthis.pendingMessagesContainer.addChild(new Spacer(1));\n\n\t\t\tfor (const message of this.queuedMessages) {\n\t\t\t\tconst queuedText = theme.fg(\"dim\", \"Queued: \" + message);\n\t\t\t\tthis.pendingMessagesContainer.addChild(new TruncatedText(queuedText, 1, 0));\n\t\t\t}\n\t\t}\n\t}\n\n\tstop(): void {\n\t\tif (this.loadingAnimation) {\n\t\t\tthis.loadingAnimation.stop();\n\t\t\tthis.loadingAnimation = null;\n\t\t}\n\t\tthis.footer.dispose();\n\t\tif (this.isInitialized) {\n\t\t\tthis.ui.stop();\n\t\t\tthis.isInitialized = false;\n\t\t}\n\t}\n}\n"]}
|
package/dist/tui/tui-renderer.js
CHANGED
|
@@ -20,6 +20,7 @@ import { FooterComponent } from "./footer.js";
|
|
|
20
20
|
import { ModelSelectorComponent } from "./model-selector.js";
|
|
21
21
|
import { OAuthSelectorComponent } from "./oauth-selector.js";
|
|
22
22
|
import { QueueModeSelectorComponent } from "./queue-mode-selector.js";
|
|
23
|
+
import { SessionSelectorComponent } from "./session-selector.js";
|
|
23
24
|
import { ThemeSelectorComponent } from "./theme-selector.js";
|
|
24
25
|
import { ThinkingSelectorComponent } from "./thinking-selector.js";
|
|
25
26
|
import { ToolExecutionComponent } from "./tool-execution.js";
|
|
@@ -45,7 +46,6 @@ export class TuiRenderer {
|
|
|
45
46
|
loadingAnimation = null;
|
|
46
47
|
lastSigintTime = 0;
|
|
47
48
|
changelogMarkdown = null;
|
|
48
|
-
newVersion = null;
|
|
49
49
|
// Message queueing
|
|
50
50
|
queuedMessages = [];
|
|
51
51
|
// Streaming message tracking
|
|
@@ -62,6 +62,8 @@ export class TuiRenderer {
|
|
|
62
62
|
modelSelector = null;
|
|
63
63
|
// User message selector (for branching)
|
|
64
64
|
userMessageSelector = null;
|
|
65
|
+
// Session selector (for resume)
|
|
66
|
+
sessionSelector = null;
|
|
65
67
|
// OAuth selector
|
|
66
68
|
oauthSelector = null;
|
|
67
69
|
// Track if this is the first user message (to skip spacer)
|
|
@@ -76,12 +78,11 @@ export class TuiRenderer {
|
|
|
76
78
|
unsubscribe;
|
|
77
79
|
// File-based slash commands
|
|
78
80
|
fileCommands = [];
|
|
79
|
-
constructor(agent, sessionManager, settingsManager, version, changelogMarkdown = null,
|
|
81
|
+
constructor(agent, sessionManager, settingsManager, version, changelogMarkdown = null, scopedModels = [], fdPath = null) {
|
|
80
82
|
this.agent = agent;
|
|
81
83
|
this.sessionManager = sessionManager;
|
|
82
84
|
this.settingsManager = settingsManager;
|
|
83
85
|
this.version = version;
|
|
84
|
-
this.newVersion = newVersion;
|
|
85
86
|
this.changelogMarkdown = changelogMarkdown;
|
|
86
87
|
this.scopedModels = scopedModels;
|
|
87
88
|
this.ui = new TUI(new ProcessTerminal());
|
|
@@ -150,6 +151,10 @@ export class TuiRenderer {
|
|
|
150
151
|
name: "autocompact",
|
|
151
152
|
description: "Toggle automatic context compaction",
|
|
152
153
|
};
|
|
154
|
+
const resumeCommand = {
|
|
155
|
+
name: "resume",
|
|
156
|
+
description: "Resume a different session",
|
|
157
|
+
};
|
|
153
158
|
// Load hide thinking block setting
|
|
154
159
|
this.hideThinkingBlock = settingsManager.getHideThinkingBlock();
|
|
155
160
|
// Load file-based slash commands
|
|
@@ -175,6 +180,7 @@ export class TuiRenderer {
|
|
|
175
180
|
clearCommand,
|
|
176
181
|
compactCommand,
|
|
177
182
|
autocompactCommand,
|
|
183
|
+
resumeCommand,
|
|
178
184
|
...fileSlashCommands,
|
|
179
185
|
], process.cwd(), fdPath);
|
|
180
186
|
this.editor.setAutocompleteProvider(autocompleteProvider);
|
|
@@ -218,15 +224,6 @@ export class TuiRenderer {
|
|
|
218
224
|
this.ui.addChild(new Spacer(1));
|
|
219
225
|
this.ui.addChild(header);
|
|
220
226
|
this.ui.addChild(new Spacer(1));
|
|
221
|
-
// Add new version notification if available
|
|
222
|
-
if (this.newVersion) {
|
|
223
|
-
this.ui.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
224
|
-
this.ui.addChild(new Text(theme.bold(theme.fg("warning", "Update Available")) +
|
|
225
|
-
"\n" +
|
|
226
|
-
theme.fg("muted", `New version ${this.newVersion} is available. Run: `) +
|
|
227
|
-
theme.fg("accent", "npm install -g @mariozechner/pi-coding-agent"), 1, 0));
|
|
228
|
-
this.ui.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
229
|
-
}
|
|
230
227
|
// Add changelog if provided
|
|
231
228
|
if (this.changelogMarkdown) {
|
|
232
229
|
this.ui.addChild(new DynamicBorder());
|
|
@@ -377,6 +374,12 @@ export class TuiRenderer {
|
|
|
377
374
|
this.editor.setText("");
|
|
378
375
|
return;
|
|
379
376
|
}
|
|
377
|
+
// Check for /resume command
|
|
378
|
+
if (text === "/resume") {
|
|
379
|
+
this.showSessionSelector();
|
|
380
|
+
this.editor.setText("");
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
380
383
|
// Check for file-based slash commands
|
|
381
384
|
text = expandSlashCommand(text, this.fileCommands);
|
|
382
385
|
// Normal message submission - validate model and API key first
|
|
@@ -408,6 +411,8 @@ export class TuiRenderer {
|
|
|
408
411
|
});
|
|
409
412
|
// Update pending messages display
|
|
410
413
|
this.updatePendingMessagesDisplay();
|
|
414
|
+
// Add to history for up/down arrow navigation
|
|
415
|
+
this.editor.addToHistory(text);
|
|
411
416
|
// Clear editor
|
|
412
417
|
this.editor.setText("");
|
|
413
418
|
this.ui.requestRender();
|
|
@@ -417,6 +422,8 @@ export class TuiRenderer {
|
|
|
417
422
|
if (this.onInputCallback) {
|
|
418
423
|
this.onInputCallback(text);
|
|
419
424
|
}
|
|
425
|
+
// Add to history for up/down arrow navigation
|
|
426
|
+
this.editor.addToHistory(text);
|
|
420
427
|
};
|
|
421
428
|
// Start the UI
|
|
422
429
|
this.ui.start();
|
|
@@ -725,6 +732,19 @@ export class TuiRenderer {
|
|
|
725
732
|
}
|
|
726
733
|
// Clear pending tools after rendering initial messages
|
|
727
734
|
this.pendingTools.clear();
|
|
735
|
+
// Populate editor history with user messages from the session (oldest first so newest is at index 0)
|
|
736
|
+
for (const message of state.messages) {
|
|
737
|
+
if (message.role === "user") {
|
|
738
|
+
const textBlocks = typeof message.content === "string"
|
|
739
|
+
? [{ type: "text", text: message.content }]
|
|
740
|
+
: message.content.filter((c) => c.type === "text");
|
|
741
|
+
const textContent = textBlocks.map((c) => c.text).join("");
|
|
742
|
+
// Skip compaction summary messages
|
|
743
|
+
if (textContent && !textContent.startsWith(SUMMARY_PREFIX)) {
|
|
744
|
+
this.editor.addToHistory(textContent);
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
}
|
|
728
748
|
this.ui.requestRender();
|
|
729
749
|
}
|
|
730
750
|
async getUserInput() {
|
|
@@ -967,6 +987,17 @@ export class TuiRenderer {
|
|
|
967
987
|
this.chatContainer.addChild(new Text(theme.fg("warning", `Warning: ${warningMessage}`), 1, 0));
|
|
968
988
|
this.ui.requestRender();
|
|
969
989
|
}
|
|
990
|
+
showNewVersionNotification(newVersion) {
|
|
991
|
+
// Show new version notification in the chat
|
|
992
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
993
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
994
|
+
this.chatContainer.addChild(new Text(theme.bold(theme.fg("warning", "Update Available")) +
|
|
995
|
+
"\n" +
|
|
996
|
+
theme.fg("muted", `New version ${newVersion} is available. Run: `) +
|
|
997
|
+
theme.fg("accent", "npm install -g @mariozechner/pi-coding-agent"), 1, 0));
|
|
998
|
+
this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
|
|
999
|
+
this.ui.requestRender();
|
|
1000
|
+
}
|
|
970
1001
|
showThinkingSelector() {
|
|
971
1002
|
// Create thinking selector with current level
|
|
972
1003
|
this.thinkingSelector = new ThinkingSelectorComponent(this.agent.state.thinkingLevel, (level) => {
|
|
@@ -1196,6 +1227,76 @@ export class TuiRenderer {
|
|
|
1196
1227
|
this.userMessageSelector = null;
|
|
1197
1228
|
this.ui.setFocus(this.editor);
|
|
1198
1229
|
}
|
|
1230
|
+
showSessionSelector() {
|
|
1231
|
+
// Create session selector
|
|
1232
|
+
this.sessionSelector = new SessionSelectorComponent(this.sessionManager, async (sessionPath) => {
|
|
1233
|
+
this.hideSessionSelector();
|
|
1234
|
+
await this.handleResumeSession(sessionPath);
|
|
1235
|
+
}, () => {
|
|
1236
|
+
// Just hide the selector
|
|
1237
|
+
this.hideSessionSelector();
|
|
1238
|
+
this.ui.requestRender();
|
|
1239
|
+
});
|
|
1240
|
+
// Replace editor with selector
|
|
1241
|
+
this.editorContainer.clear();
|
|
1242
|
+
this.editorContainer.addChild(this.sessionSelector);
|
|
1243
|
+
this.ui.setFocus(this.sessionSelector.getSessionList());
|
|
1244
|
+
this.ui.requestRender();
|
|
1245
|
+
}
|
|
1246
|
+
async handleResumeSession(sessionPath) {
|
|
1247
|
+
// Unsubscribe first to prevent processing events during transition
|
|
1248
|
+
this.unsubscribe?.();
|
|
1249
|
+
// Abort and wait for completion
|
|
1250
|
+
this.agent.abort();
|
|
1251
|
+
await this.agent.waitForIdle();
|
|
1252
|
+
// Stop loading animation
|
|
1253
|
+
if (this.loadingAnimation) {
|
|
1254
|
+
this.loadingAnimation.stop();
|
|
1255
|
+
this.loadingAnimation = null;
|
|
1256
|
+
}
|
|
1257
|
+
this.statusContainer.clear();
|
|
1258
|
+
// Clear UI state
|
|
1259
|
+
this.queuedMessages = [];
|
|
1260
|
+
this.pendingMessagesContainer.clear();
|
|
1261
|
+
this.streamingComponent = null;
|
|
1262
|
+
this.pendingTools.clear();
|
|
1263
|
+
// Set the selected session as active
|
|
1264
|
+
this.sessionManager.setSessionFile(sessionPath);
|
|
1265
|
+
// Reload the session
|
|
1266
|
+
const loaded = loadSessionFromEntries(this.sessionManager.loadEntries());
|
|
1267
|
+
this.agent.replaceMessages(loaded.messages);
|
|
1268
|
+
// Restore model if saved in session
|
|
1269
|
+
const savedModel = this.sessionManager.loadModel();
|
|
1270
|
+
if (savedModel) {
|
|
1271
|
+
const availableModels = (await getAvailableModels()).models;
|
|
1272
|
+
const match = availableModels.find((m) => m.provider === savedModel.provider && m.id === savedModel.modelId);
|
|
1273
|
+
if (match) {
|
|
1274
|
+
this.agent.setModel(match);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
// Restore thinking level if saved in session
|
|
1278
|
+
const savedThinking = this.sessionManager.loadThinkingLevel();
|
|
1279
|
+
if (savedThinking) {
|
|
1280
|
+
this.agent.setThinkingLevel(savedThinking);
|
|
1281
|
+
}
|
|
1282
|
+
// Resubscribe to agent
|
|
1283
|
+
this.subscribeToAgent();
|
|
1284
|
+
// Clear and re-render the chat
|
|
1285
|
+
this.chatContainer.clear();
|
|
1286
|
+
this.isFirstUserMessage = true;
|
|
1287
|
+
this.renderInitialMessages(this.agent.state);
|
|
1288
|
+
// Show confirmation message
|
|
1289
|
+
this.chatContainer.addChild(new Spacer(1));
|
|
1290
|
+
this.chatContainer.addChild(new Text(theme.fg("dim", "Resumed session"), 1, 0));
|
|
1291
|
+
this.ui.requestRender();
|
|
1292
|
+
}
|
|
1293
|
+
hideSessionSelector() {
|
|
1294
|
+
// Replace selector with editor in the container
|
|
1295
|
+
this.editorContainer.clear();
|
|
1296
|
+
this.editorContainer.addChild(this.editor);
|
|
1297
|
+
this.sessionSelector = null;
|
|
1298
|
+
this.ui.setFocus(this.editor);
|
|
1299
|
+
}
|
|
1199
1300
|
async showOAuthSelector(mode) {
|
|
1200
1301
|
// For logout mode, filter to only show logged-in providers
|
|
1201
1302
|
let providersToShow = [];
|