@oh-my-pi/pi-coding-agent 1.337.0

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.
Files changed (224) hide show
  1. package/CHANGELOG.md +1228 -0
  2. package/README.md +1041 -0
  3. package/docs/compaction.md +403 -0
  4. package/docs/custom-tools.md +541 -0
  5. package/docs/extension-loading.md +1004 -0
  6. package/docs/hooks.md +867 -0
  7. package/docs/rpc.md +1040 -0
  8. package/docs/sdk.md +994 -0
  9. package/docs/session-tree-plan.md +441 -0
  10. package/docs/session.md +240 -0
  11. package/docs/skills.md +290 -0
  12. package/docs/theme.md +637 -0
  13. package/docs/tree.md +197 -0
  14. package/docs/tui.md +341 -0
  15. package/examples/README.md +21 -0
  16. package/examples/custom-tools/README.md +124 -0
  17. package/examples/custom-tools/hello/index.ts +20 -0
  18. package/examples/custom-tools/question/index.ts +84 -0
  19. package/examples/custom-tools/subagent/README.md +172 -0
  20. package/examples/custom-tools/subagent/agents/planner.md +37 -0
  21. package/examples/custom-tools/subagent/agents/reviewer.md +35 -0
  22. package/examples/custom-tools/subagent/agents/scout.md +50 -0
  23. package/examples/custom-tools/subagent/agents/worker.md +24 -0
  24. package/examples/custom-tools/subagent/agents.ts +156 -0
  25. package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
  26. package/examples/custom-tools/subagent/commands/implement.md +10 -0
  27. package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
  28. package/examples/custom-tools/subagent/index.ts +1002 -0
  29. package/examples/custom-tools/todo/index.ts +212 -0
  30. package/examples/hooks/README.md +56 -0
  31. package/examples/hooks/auto-commit-on-exit.ts +49 -0
  32. package/examples/hooks/confirm-destructive.ts +59 -0
  33. package/examples/hooks/custom-compaction.ts +116 -0
  34. package/examples/hooks/dirty-repo-guard.ts +52 -0
  35. package/examples/hooks/file-trigger.ts +41 -0
  36. package/examples/hooks/git-checkpoint.ts +53 -0
  37. package/examples/hooks/handoff.ts +150 -0
  38. package/examples/hooks/permission-gate.ts +34 -0
  39. package/examples/hooks/protected-paths.ts +30 -0
  40. package/examples/hooks/qna.ts +119 -0
  41. package/examples/hooks/snake.ts +343 -0
  42. package/examples/hooks/status-line.ts +40 -0
  43. package/examples/sdk/01-minimal.ts +22 -0
  44. package/examples/sdk/02-custom-model.ts +49 -0
  45. package/examples/sdk/03-custom-prompt.ts +44 -0
  46. package/examples/sdk/04-skills.ts +44 -0
  47. package/examples/sdk/05-tools.ts +90 -0
  48. package/examples/sdk/06-hooks.ts +61 -0
  49. package/examples/sdk/07-context-files.ts +36 -0
  50. package/examples/sdk/08-slash-commands.ts +42 -0
  51. package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
  52. package/examples/sdk/10-settings.ts +38 -0
  53. package/examples/sdk/11-sessions.ts +48 -0
  54. package/examples/sdk/12-full-control.ts +95 -0
  55. package/examples/sdk/README.md +154 -0
  56. package/package.json +81 -0
  57. package/src/cli/args.ts +246 -0
  58. package/src/cli/file-processor.ts +72 -0
  59. package/src/cli/list-models.ts +104 -0
  60. package/src/cli/plugin-cli.ts +650 -0
  61. package/src/cli/session-picker.ts +41 -0
  62. package/src/cli.ts +10 -0
  63. package/src/commands/init.md +20 -0
  64. package/src/config.ts +159 -0
  65. package/src/core/agent-session.ts +1900 -0
  66. package/src/core/auth-storage.ts +236 -0
  67. package/src/core/bash-executor.ts +196 -0
  68. package/src/core/compaction/branch-summarization.ts +343 -0
  69. package/src/core/compaction/compaction.ts +742 -0
  70. package/src/core/compaction/index.ts +7 -0
  71. package/src/core/compaction/utils.ts +154 -0
  72. package/src/core/custom-tools/index.ts +21 -0
  73. package/src/core/custom-tools/loader.ts +248 -0
  74. package/src/core/custom-tools/types.ts +169 -0
  75. package/src/core/custom-tools/wrapper.ts +28 -0
  76. package/src/core/exec.ts +129 -0
  77. package/src/core/export-html/index.ts +211 -0
  78. package/src/core/export-html/template.css +781 -0
  79. package/src/core/export-html/template.html +54 -0
  80. package/src/core/export-html/template.js +1185 -0
  81. package/src/core/export-html/vendor/highlight.min.js +1213 -0
  82. package/src/core/export-html/vendor/marked.min.js +6 -0
  83. package/src/core/hooks/index.ts +16 -0
  84. package/src/core/hooks/loader.ts +312 -0
  85. package/src/core/hooks/runner.ts +434 -0
  86. package/src/core/hooks/tool-wrapper.ts +99 -0
  87. package/src/core/hooks/types.ts +773 -0
  88. package/src/core/index.ts +52 -0
  89. package/src/core/mcp/client.ts +158 -0
  90. package/src/core/mcp/config.ts +154 -0
  91. package/src/core/mcp/index.ts +45 -0
  92. package/src/core/mcp/loader.ts +68 -0
  93. package/src/core/mcp/manager.ts +181 -0
  94. package/src/core/mcp/tool-bridge.ts +148 -0
  95. package/src/core/mcp/transports/http.ts +316 -0
  96. package/src/core/mcp/transports/index.ts +6 -0
  97. package/src/core/mcp/transports/stdio.ts +252 -0
  98. package/src/core/mcp/types.ts +220 -0
  99. package/src/core/messages.ts +189 -0
  100. package/src/core/model-registry.ts +317 -0
  101. package/src/core/model-resolver.ts +393 -0
  102. package/src/core/plugins/doctor.ts +59 -0
  103. package/src/core/plugins/index.ts +38 -0
  104. package/src/core/plugins/installer.ts +189 -0
  105. package/src/core/plugins/loader.ts +338 -0
  106. package/src/core/plugins/manager.ts +672 -0
  107. package/src/core/plugins/parser.ts +105 -0
  108. package/src/core/plugins/paths.ts +32 -0
  109. package/src/core/plugins/types.ts +190 -0
  110. package/src/core/sdk.ts +760 -0
  111. package/src/core/session-manager.ts +1128 -0
  112. package/src/core/settings-manager.ts +443 -0
  113. package/src/core/skills.ts +437 -0
  114. package/src/core/slash-commands.ts +248 -0
  115. package/src/core/system-prompt.ts +439 -0
  116. package/src/core/timings.ts +25 -0
  117. package/src/core/tools/ask.ts +211 -0
  118. package/src/core/tools/bash-interceptor.ts +120 -0
  119. package/src/core/tools/bash.ts +250 -0
  120. package/src/core/tools/context.ts +32 -0
  121. package/src/core/tools/edit-diff.ts +475 -0
  122. package/src/core/tools/edit.ts +208 -0
  123. package/src/core/tools/exa/company.ts +59 -0
  124. package/src/core/tools/exa/index.ts +64 -0
  125. package/src/core/tools/exa/linkedin.ts +59 -0
  126. package/src/core/tools/exa/logger.ts +56 -0
  127. package/src/core/tools/exa/mcp-client.ts +368 -0
  128. package/src/core/tools/exa/render.ts +196 -0
  129. package/src/core/tools/exa/researcher.ts +90 -0
  130. package/src/core/tools/exa/search.ts +337 -0
  131. package/src/core/tools/exa/types.ts +168 -0
  132. package/src/core/tools/exa/websets.ts +248 -0
  133. package/src/core/tools/find.ts +261 -0
  134. package/src/core/tools/grep.ts +555 -0
  135. package/src/core/tools/index.ts +202 -0
  136. package/src/core/tools/ls.ts +140 -0
  137. package/src/core/tools/lsp/client.ts +605 -0
  138. package/src/core/tools/lsp/config.ts +147 -0
  139. package/src/core/tools/lsp/edits.ts +101 -0
  140. package/src/core/tools/lsp/index.ts +804 -0
  141. package/src/core/tools/lsp/render.ts +447 -0
  142. package/src/core/tools/lsp/rust-analyzer.ts +145 -0
  143. package/src/core/tools/lsp/types.ts +463 -0
  144. package/src/core/tools/lsp/utils.ts +486 -0
  145. package/src/core/tools/notebook.ts +229 -0
  146. package/src/core/tools/path-utils.ts +61 -0
  147. package/src/core/tools/read.ts +240 -0
  148. package/src/core/tools/renderers.ts +540 -0
  149. package/src/core/tools/task/agents.ts +153 -0
  150. package/src/core/tools/task/artifacts.ts +114 -0
  151. package/src/core/tools/task/bundled-agents/browser.md +71 -0
  152. package/src/core/tools/task/bundled-agents/explore.md +82 -0
  153. package/src/core/tools/task/bundled-agents/plan.md +54 -0
  154. package/src/core/tools/task/bundled-agents/reviewer.md +59 -0
  155. package/src/core/tools/task/bundled-agents/task.md +53 -0
  156. package/src/core/tools/task/bundled-commands/architect-plan.md +10 -0
  157. package/src/core/tools/task/bundled-commands/implement-with-critic.md +11 -0
  158. package/src/core/tools/task/bundled-commands/implement.md +11 -0
  159. package/src/core/tools/task/commands.ts +213 -0
  160. package/src/core/tools/task/discovery.ts +208 -0
  161. package/src/core/tools/task/executor.ts +367 -0
  162. package/src/core/tools/task/index.ts +388 -0
  163. package/src/core/tools/task/model-resolver.ts +115 -0
  164. package/src/core/tools/task/parallel.ts +38 -0
  165. package/src/core/tools/task/render.ts +232 -0
  166. package/src/core/tools/task/types.ts +99 -0
  167. package/src/core/tools/truncate.ts +265 -0
  168. package/src/core/tools/web-fetch.ts +2370 -0
  169. package/src/core/tools/web-search/auth.ts +193 -0
  170. package/src/core/tools/web-search/index.ts +537 -0
  171. package/src/core/tools/web-search/providers/anthropic.ts +198 -0
  172. package/src/core/tools/web-search/providers/exa.ts +302 -0
  173. package/src/core/tools/web-search/providers/perplexity.ts +195 -0
  174. package/src/core/tools/web-search/render.ts +182 -0
  175. package/src/core/tools/web-search/types.ts +180 -0
  176. package/src/core/tools/write.ts +99 -0
  177. package/src/index.ts +176 -0
  178. package/src/main.ts +464 -0
  179. package/src/migrations.ts +135 -0
  180. package/src/modes/index.ts +43 -0
  181. package/src/modes/interactive/components/armin.ts +382 -0
  182. package/src/modes/interactive/components/assistant-message.ts +86 -0
  183. package/src/modes/interactive/components/bash-execution.ts +196 -0
  184. package/src/modes/interactive/components/bordered-loader.ts +41 -0
  185. package/src/modes/interactive/components/branch-summary-message.ts +42 -0
  186. package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
  187. package/src/modes/interactive/components/custom-editor.ts +122 -0
  188. package/src/modes/interactive/components/diff.ts +147 -0
  189. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  190. package/src/modes/interactive/components/footer.ts +381 -0
  191. package/src/modes/interactive/components/hook-editor.ts +117 -0
  192. package/src/modes/interactive/components/hook-input.ts +64 -0
  193. package/src/modes/interactive/components/hook-message.ts +96 -0
  194. package/src/modes/interactive/components/hook-selector.ts +91 -0
  195. package/src/modes/interactive/components/model-selector.ts +247 -0
  196. package/src/modes/interactive/components/oauth-selector.ts +120 -0
  197. package/src/modes/interactive/components/plugin-settings.ts +479 -0
  198. package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
  199. package/src/modes/interactive/components/session-selector.ts +204 -0
  200. package/src/modes/interactive/components/settings-selector.ts +453 -0
  201. package/src/modes/interactive/components/show-images-selector.ts +45 -0
  202. package/src/modes/interactive/components/theme-selector.ts +62 -0
  203. package/src/modes/interactive/components/thinking-selector.ts +64 -0
  204. package/src/modes/interactive/components/tool-execution.ts +675 -0
  205. package/src/modes/interactive/components/tree-selector.ts +866 -0
  206. package/src/modes/interactive/components/user-message-selector.ts +159 -0
  207. package/src/modes/interactive/components/user-message.ts +18 -0
  208. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  209. package/src/modes/interactive/components/welcome.ts +183 -0
  210. package/src/modes/interactive/interactive-mode.ts +2516 -0
  211. package/src/modes/interactive/theme/dark.json +101 -0
  212. package/src/modes/interactive/theme/light.json +98 -0
  213. package/src/modes/interactive/theme/theme-schema.json +308 -0
  214. package/src/modes/interactive/theme/theme.ts +998 -0
  215. package/src/modes/print-mode.ts +128 -0
  216. package/src/modes/rpc/rpc-client.ts +527 -0
  217. package/src/modes/rpc/rpc-mode.ts +483 -0
  218. package/src/modes/rpc/rpc-types.ts +203 -0
  219. package/src/utils/changelog.ts +99 -0
  220. package/src/utils/clipboard.ts +265 -0
  221. package/src/utils/fuzzy.ts +108 -0
  222. package/src/utils/mime.ts +30 -0
  223. package/src/utils/shell.ts +276 -0
  224. package/src/utils/tools-manager.ts +274 -0
@@ -0,0 +1,2516 @@
1
+ /**
2
+ * Interactive mode for the coding agent.
3
+ * Handles TUI rendering and user interaction, delegating business logic to AgentSession.
4
+ */
5
+
6
+ import * as fs from "node:fs";
7
+ import * as os from "node:os";
8
+ import * as path from "node:path";
9
+ import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
10
+ import type { AssistantMessage, ImageContent, Message, OAuthProvider } from "@oh-my-pi/pi-ai";
11
+ import type { SlashCommand } from "@oh-my-pi/pi-tui";
12
+ import {
13
+ CombinedAutocompleteProvider,
14
+ type Component,
15
+ Container,
16
+ Input,
17
+ Loader,
18
+ Markdown,
19
+ ProcessTerminal,
20
+ Spacer,
21
+ Text,
22
+ TruncatedText,
23
+ TUI,
24
+ visibleWidth,
25
+ } from "@oh-my-pi/pi-tui";
26
+ import { getAuthPath, getDebugLogPath } from "../../config.js";
27
+ import type { AgentSession, AgentSessionEvent } from "../../core/agent-session.js";
28
+ import type { CustomToolSessionEvent, LoadedCustomTool } from "../../core/custom-tools/index.js";
29
+ import type { HookUIContext } from "../../core/hooks/index.js";
30
+ import { createCompactionSummaryMessage } from "../../core/messages.js";
31
+ import { type SessionContext, SessionManager } from "../../core/session-manager.js";
32
+ import { loadSkills } from "../../core/skills.js";
33
+ import { loadProjectContextFiles } from "../../core/system-prompt.js";
34
+ import type { TruncationResult } from "../../core/tools/truncate.js";
35
+ import { getChangelogPath, parseChangelog } from "../../utils/changelog.js";
36
+ import { copyToClipboard, readImageFromClipboard } from "../../utils/clipboard.js";
37
+ import { ArminComponent } from "./components/armin.js";
38
+ import { AssistantMessageComponent } from "./components/assistant-message.js";
39
+ import { BashExecutionComponent } from "./components/bash-execution.js";
40
+ import { BorderedLoader } from "./components/bordered-loader.js";
41
+ import { BranchSummaryMessageComponent } from "./components/branch-summary-message.js";
42
+ import { CompactionSummaryMessageComponent } from "./components/compaction-summary-message.js";
43
+ import { CustomEditor } from "./components/custom-editor.js";
44
+ import { DynamicBorder } from "./components/dynamic-border.js";
45
+ import { FooterComponent } from "./components/footer.js";
46
+ import { HookEditorComponent } from "./components/hook-editor.js";
47
+ import { HookInputComponent } from "./components/hook-input.js";
48
+ import { HookMessageComponent } from "./components/hook-message.js";
49
+ import { HookSelectorComponent } from "./components/hook-selector.js";
50
+ import { ModelSelectorComponent } from "./components/model-selector.js";
51
+ import { OAuthSelectorComponent } from "./components/oauth-selector.js";
52
+ import { SessionSelectorComponent } from "./components/session-selector.js";
53
+ import { SettingsSelectorComponent } from "./components/settings-selector.js";
54
+ import { ToolExecutionComponent } from "./components/tool-execution.js";
55
+ import { TreeSelectorComponent } from "./components/tree-selector.js";
56
+ import { UserMessageComponent } from "./components/user-message.js";
57
+ import { UserMessageSelectorComponent } from "./components/user-message-selector.js";
58
+ import { WelcomeComponent } from "./components/welcome.js";
59
+ import {
60
+ getAvailableThemes,
61
+ getEditorTheme,
62
+ getMarkdownTheme,
63
+ onThemeChange,
64
+ setTheme,
65
+ type Theme,
66
+ theme,
67
+ } from "./theme/theme.js";
68
+
69
+ /** Interface for components that can be expanded/collapsed */
70
+ interface Expandable {
71
+ setExpanded(expanded: boolean): void;
72
+ }
73
+
74
+ function isExpandable(obj: unknown): obj is Expandable {
75
+ return typeof obj === "object" && obj !== null && "setExpanded" in obj && typeof obj.setExpanded === "function";
76
+ }
77
+
78
+ export class InteractiveMode {
79
+ private session: AgentSession;
80
+ private ui: TUI;
81
+ private chatContainer: Container;
82
+ private pendingMessagesContainer: Container;
83
+ private statusContainer: Container;
84
+ private editor: CustomEditor;
85
+ private editorContainer: Container;
86
+ private footer: FooterComponent;
87
+ private version: string;
88
+ private isInitialized = false;
89
+ private onInputCallback?: (input: { text: string; images?: ImageContent[] }) => void;
90
+ private loadingAnimation: Loader | undefined = undefined;
91
+
92
+ private lastSigintTime = 0;
93
+ private lastEscapeTime = 0;
94
+ private changelogMarkdown: string | undefined = undefined;
95
+
96
+ // Status line tracking (for mutating immediately-sequential status updates)
97
+ private lastStatusSpacer: Spacer | undefined = undefined;
98
+ private lastStatusText: Text | undefined = undefined;
99
+
100
+ // Streaming message tracking
101
+ private streamingComponent: AssistantMessageComponent | undefined = undefined;
102
+ private streamingMessage: AssistantMessage | undefined = undefined;
103
+
104
+ // Tool execution tracking: toolCallId -> component
105
+ private pendingTools = new Map<string, ToolExecutionComponent>();
106
+
107
+ // Tool output expansion state
108
+ private toolOutputExpanded = false;
109
+
110
+ // Thinking block visibility state
111
+ private hideThinkingBlock = false;
112
+
113
+ // Agent subscription unsubscribe function
114
+ private unsubscribe?: () => void;
115
+
116
+ // Track if editor is in bash mode (text starts with !)
117
+ private isBashMode = false;
118
+
119
+ // Track current bash execution component
120
+ private bashComponent: BashExecutionComponent | undefined = undefined;
121
+
122
+ // Track pending bash components (shown in pending area, moved to chat on submit)
123
+ private pendingBashComponents: BashExecutionComponent[] = [];
124
+
125
+ // Track pending images from clipboard paste (attached to next message)
126
+ private pendingImages: ImageContent[] = [];
127
+
128
+ // Auto-compaction state
129
+ private autoCompactionLoader: Loader | undefined = undefined;
130
+ private autoCompactionEscapeHandler?: () => void;
131
+
132
+ // Auto-retry state
133
+ private retryLoader: Loader | undefined = undefined;
134
+ private retryEscapeHandler?: () => void;
135
+
136
+ // Hook UI state
137
+ private hookSelector: HookSelectorComponent | undefined = undefined;
138
+ private hookInput: HookInputComponent | undefined = undefined;
139
+ private hookEditor: HookEditorComponent | undefined = undefined;
140
+
141
+ // Custom tools for custom rendering
142
+ private customTools: Map<string, LoadedCustomTool>;
143
+
144
+ // Convenience accessors
145
+ private get agent() {
146
+ return this.session.agent;
147
+ }
148
+ private get sessionManager() {
149
+ return this.session.sessionManager;
150
+ }
151
+ private get settingsManager() {
152
+ return this.session.settingsManager;
153
+ }
154
+
155
+ constructor(
156
+ session: AgentSession,
157
+ version: string,
158
+ changelogMarkdown: string | undefined = undefined,
159
+ customTools: LoadedCustomTool[] = [],
160
+ private setToolUIContext: (uiContext: HookUIContext, hasUI: boolean) => void = () => {},
161
+ fdPath: string | undefined = undefined,
162
+ ) {
163
+ this.session = session;
164
+ this.version = version;
165
+ this.changelogMarkdown = changelogMarkdown;
166
+ this.customTools = new Map(customTools.map((ct) => [ct.tool.name, ct]));
167
+ this.ui = new TUI(new ProcessTerminal());
168
+ this.chatContainer = new Container();
169
+ this.pendingMessagesContainer = new Container();
170
+ this.statusContainer = new Container();
171
+ this.editor = new CustomEditor(getEditorTheme());
172
+ this.editorContainer = new Container();
173
+ this.editorContainer.addChild(this.editor);
174
+ this.footer = new FooterComponent(session);
175
+ this.footer.setAutoCompactEnabled(session.autoCompactionEnabled);
176
+
177
+ // Define slash commands for autocomplete
178
+ const slashCommands: SlashCommand[] = [
179
+ { name: "settings", description: "Open settings menu" },
180
+ { name: "model", description: "Select model (opens selector UI)" },
181
+ { name: "export", description: "Export session to HTML file" },
182
+ { name: "share", description: "Share session as a secret GitHub gist" },
183
+ { name: "copy", description: "Copy last agent message to clipboard" },
184
+ { name: "session", description: "Show session info and stats" },
185
+ { name: "status", description: "Show loaded extensions (context, skills, tools, hooks)" },
186
+ { name: "changelog", description: "Show changelog entries" },
187
+ { name: "hotkeys", description: "Show all keyboard shortcuts" },
188
+ { name: "branch", description: "Create a new branch from a previous message" },
189
+ { name: "tree", description: "Navigate session tree (switch branches)" },
190
+ { name: "login", description: "Login with OAuth provider" },
191
+ { name: "logout", description: "Logout from OAuth provider" },
192
+ { name: "new", description: "Start a new session" },
193
+ { name: "compact", description: "Manually compact the session context" },
194
+ { name: "resume", description: "Resume a different session" },
195
+ ];
196
+
197
+ // Load hide thinking block setting
198
+ this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
199
+
200
+ // Convert file commands to SlashCommand format
201
+ const fileSlashCommands: SlashCommand[] = this.session.fileCommands.map((cmd) => ({
202
+ name: cmd.name,
203
+ description: cmd.description,
204
+ }));
205
+
206
+ // Convert hook commands to SlashCommand format
207
+ const hookCommands: SlashCommand[] = (this.session.hookRunner?.getRegisteredCommands() ?? []).map((cmd) => ({
208
+ name: cmd.name,
209
+ description: cmd.description ?? "(hook command)",
210
+ }));
211
+
212
+ // Setup autocomplete
213
+ const autocompleteProvider = new CombinedAutocompleteProvider(
214
+ [...slashCommands, ...fileSlashCommands, ...hookCommands],
215
+ process.cwd(),
216
+ fdPath,
217
+ );
218
+ this.editor.setAutocompleteProvider(autocompleteProvider);
219
+ }
220
+
221
+ async init(): Promise<void> {
222
+ if (this.isInitialized) return;
223
+
224
+ // Get current model info for welcome screen
225
+ const modelName = this.session.model?.name ?? "Unknown";
226
+ const providerName = this.session.model?.provider ?? "Unknown";
227
+
228
+ // Add welcome header
229
+ const welcome = new WelcomeComponent(this.version, modelName, providerName);
230
+
231
+ // Setup UI layout
232
+ this.ui.addChild(new Spacer(1));
233
+ this.ui.addChild(welcome);
234
+ this.ui.addChild(new Spacer(1));
235
+
236
+ // Add changelog if provided
237
+ if (this.changelogMarkdown) {
238
+ this.ui.addChild(new DynamicBorder());
239
+ if (this.settingsManager.getCollapseChangelog()) {
240
+ const versionMatch = this.changelogMarkdown.match(/##\s+\[?(\d+\.\d+\.\d+)\]?/);
241
+ const latestVersion = versionMatch ? versionMatch[1] : this.version;
242
+ const condensedText = `Updated to v${latestVersion}. Use ${theme.bold("/changelog")} to view full changelog.`;
243
+ this.ui.addChild(new Text(condensedText, 1, 0));
244
+ } else {
245
+ this.ui.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
246
+ this.ui.addChild(new Spacer(1));
247
+ this.ui.addChild(new Markdown(this.changelogMarkdown.trim(), 1, 0, getMarkdownTheme()));
248
+ this.ui.addChild(new Spacer(1));
249
+ }
250
+ this.ui.addChild(new DynamicBorder());
251
+ }
252
+
253
+ this.ui.addChild(this.chatContainer);
254
+ this.ui.addChild(this.pendingMessagesContainer);
255
+ this.ui.addChild(this.statusContainer);
256
+ this.ui.addChild(new Spacer(1));
257
+ this.ui.addChild(this.editorContainer);
258
+ this.ui.addChild(this.footer);
259
+ this.ui.setFocus(this.editor);
260
+
261
+ this.setupKeyHandlers();
262
+ this.setupEditorSubmitHandler();
263
+
264
+ // Start the UI
265
+ this.ui.start();
266
+ this.isInitialized = true;
267
+
268
+ // Initialize hooks with TUI-based UI context
269
+ await this.initHooksAndCustomTools();
270
+
271
+ // Subscribe to agent events
272
+ this.subscribeToAgent();
273
+
274
+ // Set up theme file watcher
275
+ onThemeChange(() => {
276
+ this.ui.invalidate();
277
+ this.updateEditorBorderColor();
278
+ this.ui.requestRender();
279
+ });
280
+
281
+ // Set up git branch watcher
282
+ this.footer.watchBranch(() => {
283
+ this.ui.requestRender();
284
+ });
285
+ }
286
+
287
+ // =========================================================================
288
+ // Hook System
289
+ // =========================================================================
290
+
291
+ /**
292
+ * Initialize the hook system with TUI-based UI context.
293
+ */
294
+ private async initHooksAndCustomTools(): Promise<void> {
295
+ // Create and set hook & tool UI context
296
+ const uiContext: HookUIContext = {
297
+ select: (title, options) => this.showHookSelector(title, options),
298
+ confirm: (title, message) => this.showHookConfirm(title, message),
299
+ input: (title, placeholder) => this.showHookInput(title, placeholder),
300
+ notify: (message, type) => this.showHookNotify(message, type),
301
+ setStatus: (key, text) => this.setHookStatus(key, text),
302
+ custom: (factory) => this.showHookCustom(factory),
303
+ setEditorText: (text) => this.editor.setText(text),
304
+ getEditorText: () => this.editor.getText(),
305
+ editor: (title, prefill) => this.showHookEditor(title, prefill),
306
+ get theme() {
307
+ return theme;
308
+ },
309
+ };
310
+ this.setToolUIContext(uiContext, true);
311
+
312
+ // Notify custom tools of session start
313
+ await this.emitCustomToolSessionEvent({
314
+ reason: "start",
315
+ previousSessionFile: undefined,
316
+ });
317
+
318
+ const hookRunner = this.session.hookRunner;
319
+ if (!hookRunner) {
320
+ return; // No hooks loaded
321
+ }
322
+
323
+ hookRunner.initialize({
324
+ getModel: () => this.session.model,
325
+ sendMessageHandler: (message, triggerTurn) => {
326
+ const wasStreaming = this.session.isStreaming;
327
+ this.session
328
+ .sendHookMessage(message, triggerTurn)
329
+ .then(() => {
330
+ // For non-streaming cases with display=true, update UI
331
+ // (streaming cases update via message_end event)
332
+ if (!wasStreaming && message.display) {
333
+ this.rebuildChatFromMessages();
334
+ }
335
+ })
336
+ .catch((err) => {
337
+ this.showError(`Hook sendMessage failed: ${err instanceof Error ? err.message : String(err)}`);
338
+ });
339
+ },
340
+ appendEntryHandler: (customType, data) => {
341
+ this.sessionManager.appendCustomEntry(customType, data);
342
+ },
343
+ newSessionHandler: async (options) => {
344
+ // Stop any loading animation
345
+ if (this.loadingAnimation) {
346
+ this.loadingAnimation.stop();
347
+ this.loadingAnimation = undefined;
348
+ }
349
+ this.statusContainer.clear();
350
+
351
+ // Create new session
352
+ const success = await this.session.newSession({ parentSession: options?.parentSession });
353
+ if (!success) {
354
+ return { cancelled: true };
355
+ }
356
+
357
+ // Call setup callback if provided
358
+ if (options?.setup) {
359
+ await options.setup(this.sessionManager);
360
+ }
361
+
362
+ // Clear UI state
363
+ this.chatContainer.clear();
364
+ this.pendingMessagesContainer.clear();
365
+ this.streamingComponent = undefined;
366
+ this.streamingMessage = undefined;
367
+ this.pendingTools.clear();
368
+
369
+ this.chatContainer.addChild(new Spacer(1));
370
+ this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
371
+ this.ui.requestRender();
372
+
373
+ return { cancelled: false };
374
+ },
375
+ branchHandler: async (entryId) => {
376
+ const result = await this.session.branch(entryId);
377
+ if (result.cancelled) {
378
+ return { cancelled: true };
379
+ }
380
+
381
+ // Update UI
382
+ this.chatContainer.clear();
383
+ this.renderInitialMessages();
384
+ this.editor.setText(result.selectedText);
385
+ this.showStatus("Branched to new session");
386
+
387
+ return { cancelled: false };
388
+ },
389
+ navigateTreeHandler: async (targetId, options) => {
390
+ const result = await this.session.navigateTree(targetId, { summarize: options?.summarize });
391
+ if (result.cancelled) {
392
+ return { cancelled: true };
393
+ }
394
+
395
+ // Update UI
396
+ this.chatContainer.clear();
397
+ this.renderInitialMessages();
398
+ if (result.editorText) {
399
+ this.editor.setText(result.editorText);
400
+ }
401
+ this.showStatus("Navigated to selected point");
402
+
403
+ return { cancelled: false };
404
+ },
405
+ isIdle: () => !this.session.isStreaming,
406
+ waitForIdle: () => this.session.agent.waitForIdle(),
407
+ abort: () => {
408
+ this.session.abort();
409
+ },
410
+ hasQueuedMessages: () => this.session.queuedMessageCount > 0,
411
+ uiContext,
412
+ hasUI: true,
413
+ });
414
+
415
+ // Subscribe to hook errors
416
+ hookRunner.onError((error) => {
417
+ this.showHookError(error.hookPath, error.error);
418
+ });
419
+
420
+ // Emit session_start event
421
+ await hookRunner.emit({
422
+ type: "session_start",
423
+ });
424
+ }
425
+
426
+ /**
427
+ * Emit session event to all custom tools.
428
+ */
429
+ private async emitCustomToolSessionEvent(event: CustomToolSessionEvent): Promise<void> {
430
+ for (const { tool } of this.customTools.values()) {
431
+ if (tool.onSession) {
432
+ try {
433
+ await tool.onSession(event, {
434
+ sessionManager: this.session.sessionManager,
435
+ modelRegistry: this.session.modelRegistry,
436
+ model: this.session.model,
437
+ isIdle: () => !this.session.isStreaming,
438
+ hasQueuedMessages: () => this.session.queuedMessageCount > 0,
439
+ abort: () => {
440
+ this.session.abort();
441
+ },
442
+ });
443
+ } catch (err) {
444
+ this.showToolError(tool.name, err instanceof Error ? err.message : String(err));
445
+ }
446
+ }
447
+ }
448
+ }
449
+
450
+ /**
451
+ * Show a tool error in the chat.
452
+ */
453
+ private showToolError(toolName: string, error: string): void {
454
+ const errorText = new Text(theme.fg("error", `Tool "${toolName}" error: ${error}`), 1, 0);
455
+ this.chatContainer.addChild(errorText);
456
+ this.ui.requestRender();
457
+ }
458
+
459
+ /**
460
+ * Set hook status text in the footer.
461
+ */
462
+ private setHookStatus(key: string, text: string | undefined): void {
463
+ this.footer.setHookStatus(key, text);
464
+ this.ui.requestRender();
465
+ }
466
+
467
+ /**
468
+ * Show a selector for hooks.
469
+ */
470
+ private showHookSelector(title: string, options: string[]): Promise<string | undefined> {
471
+ return new Promise((resolve) => {
472
+ this.hookSelector = new HookSelectorComponent(
473
+ title,
474
+ options,
475
+ (option) => {
476
+ this.hideHookSelector();
477
+ resolve(option);
478
+ },
479
+ () => {
480
+ this.hideHookSelector();
481
+ resolve(undefined);
482
+ },
483
+ );
484
+
485
+ this.editorContainer.clear();
486
+ this.editorContainer.addChild(this.hookSelector);
487
+ this.ui.setFocus(this.hookSelector);
488
+ this.ui.requestRender();
489
+ });
490
+ }
491
+
492
+ /**
493
+ * Hide the hook selector.
494
+ */
495
+ private hideHookSelector(): void {
496
+ this.editorContainer.clear();
497
+ this.editorContainer.addChild(this.editor);
498
+ this.hookSelector = undefined;
499
+ this.ui.setFocus(this.editor);
500
+ this.ui.requestRender();
501
+ }
502
+
503
+ /**
504
+ * Show a confirmation dialog for hooks.
505
+ */
506
+ private async showHookConfirm(title: string, message: string): Promise<boolean> {
507
+ const result = await this.showHookSelector(`${title}\n${message}`, ["Yes", "No"]);
508
+ return result === "Yes";
509
+ }
510
+
511
+ /**
512
+ * Show a text input for hooks.
513
+ */
514
+ private showHookInput(title: string, placeholder?: string): Promise<string | undefined> {
515
+ return new Promise((resolve) => {
516
+ this.hookInput = new HookInputComponent(
517
+ title,
518
+ placeholder,
519
+ (value) => {
520
+ this.hideHookInput();
521
+ resolve(value);
522
+ },
523
+ () => {
524
+ this.hideHookInput();
525
+ resolve(undefined);
526
+ },
527
+ );
528
+
529
+ this.editorContainer.clear();
530
+ this.editorContainer.addChild(this.hookInput);
531
+ this.ui.setFocus(this.hookInput);
532
+ this.ui.requestRender();
533
+ });
534
+ }
535
+
536
+ /**
537
+ * Hide the hook input.
538
+ */
539
+ private hideHookInput(): void {
540
+ this.editorContainer.clear();
541
+ this.editorContainer.addChild(this.editor);
542
+ this.hookInput = undefined;
543
+ this.ui.setFocus(this.editor);
544
+ this.ui.requestRender();
545
+ }
546
+
547
+ /**
548
+ * Show a multi-line editor for hooks (with Ctrl+G support).
549
+ */
550
+ private showHookEditor(title: string, prefill?: string): Promise<string | undefined> {
551
+ return new Promise((resolve) => {
552
+ this.hookEditor = new HookEditorComponent(
553
+ this.ui,
554
+ title,
555
+ prefill,
556
+ (value) => {
557
+ this.hideHookEditor();
558
+ resolve(value);
559
+ },
560
+ () => {
561
+ this.hideHookEditor();
562
+ resolve(undefined);
563
+ },
564
+ );
565
+
566
+ this.editorContainer.clear();
567
+ this.editorContainer.addChild(this.hookEditor);
568
+ this.ui.setFocus(this.hookEditor);
569
+ this.ui.requestRender();
570
+ });
571
+ }
572
+
573
+ /**
574
+ * Hide the hook editor.
575
+ */
576
+ private hideHookEditor(): void {
577
+ this.editorContainer.clear();
578
+ this.editorContainer.addChild(this.editor);
579
+ this.hookEditor = undefined;
580
+ this.ui.setFocus(this.editor);
581
+ this.ui.requestRender();
582
+ }
583
+
584
+ /**
585
+ * Show a notification for hooks.
586
+ */
587
+ private showHookNotify(message: string, type?: "info" | "warning" | "error"): void {
588
+ if (type === "error") {
589
+ this.showError(message);
590
+ } else if (type === "warning") {
591
+ this.showWarning(message);
592
+ } else {
593
+ this.showStatus(message);
594
+ }
595
+ }
596
+
597
+ /**
598
+ * Show a custom component with keyboard focus.
599
+ */
600
+ private async showHookCustom<T>(
601
+ factory: (
602
+ tui: TUI,
603
+ theme: Theme,
604
+ done: (result: T) => void,
605
+ ) => (Component & { dispose?(): void }) | Promise<Component & { dispose?(): void }>,
606
+ ): Promise<T> {
607
+ const savedText = this.editor.getText();
608
+
609
+ return new Promise((resolve) => {
610
+ let component: Component & { dispose?(): void };
611
+
612
+ const close = (result: T) => {
613
+ component.dispose?.();
614
+ this.editorContainer.clear();
615
+ this.editorContainer.addChild(this.editor);
616
+ this.editor.setText(savedText);
617
+ this.ui.setFocus(this.editor);
618
+ this.ui.requestRender();
619
+ resolve(result);
620
+ };
621
+
622
+ Promise.resolve(factory(this.ui, theme, close)).then((c) => {
623
+ component = c;
624
+ this.editorContainer.clear();
625
+ this.editorContainer.addChild(component);
626
+ this.ui.setFocus(component);
627
+ this.ui.requestRender();
628
+ });
629
+ });
630
+ }
631
+
632
+ /**
633
+ * Show a hook error in the UI.
634
+ */
635
+ private showHookError(hookPath: string, error: string): void {
636
+ const errorText = new Text(theme.fg("error", `Hook "${hookPath}" error: ${error}`), 1, 0);
637
+ this.chatContainer.addChild(errorText);
638
+ this.ui.requestRender();
639
+ }
640
+
641
+ /**
642
+ * Handle pi.send() from hooks.
643
+ * If streaming, queue the message. Otherwise, start a new agent loop.
644
+ */
645
+ // =========================================================================
646
+ // Key Handlers
647
+ // =========================================================================
648
+
649
+ private setupKeyHandlers(): void {
650
+ this.editor.onEscape = () => {
651
+ if (this.loadingAnimation) {
652
+ // Abort and restore queued messages to editor
653
+ const queuedMessages = this.session.clearQueue();
654
+ const queuedText = queuedMessages.join("\n\n");
655
+ const currentText = this.editor.getText();
656
+ const combinedText = [queuedText, currentText].filter((t) => t.trim()).join("\n\n");
657
+ this.editor.setText(combinedText);
658
+ this.updatePendingMessagesDisplay();
659
+ this.agent.abort();
660
+ } else if (this.session.isBashRunning) {
661
+ this.session.abortBash();
662
+ } else if (this.isBashMode) {
663
+ this.editor.setText("");
664
+ this.isBashMode = false;
665
+ this.updateEditorBorderColor();
666
+ } else if (!this.editor.getText().trim()) {
667
+ // Double-escape with empty editor triggers /branch
668
+ const now = Date.now();
669
+ if (now - this.lastEscapeTime < 500) {
670
+ this.showUserMessageSelector();
671
+ this.lastEscapeTime = 0;
672
+ } else {
673
+ this.lastEscapeTime = now;
674
+ }
675
+ }
676
+ };
677
+
678
+ this.editor.onCtrlC = () => this.handleCtrlC();
679
+ this.editor.onCtrlD = () => this.handleCtrlD();
680
+ this.editor.onCtrlZ = () => this.handleCtrlZ();
681
+ this.editor.onShiftTab = () => this.cycleThinkingLevel();
682
+ this.editor.onCtrlP = () => this.cycleModel("forward");
683
+ this.editor.onShiftCtrlP = () => this.cycleModel("backward");
684
+
685
+ // Global debug handler on TUI (works regardless of focus)
686
+ this.ui.onDebug = () => this.handleDebugCommand();
687
+ this.editor.onCtrlL = () => this.showModelSelector();
688
+ this.editor.onCtrlO = () => this.toggleToolOutputExpansion();
689
+ this.editor.onCtrlT = () => this.toggleThinkingBlockVisibility();
690
+ this.editor.onCtrlG = () => this.openExternalEditor();
691
+ this.editor.onQuestionMark = () => this.handleHotkeysCommand();
692
+ this.editor.onCtrlV = () => this.handleImagePaste();
693
+
694
+ this.editor.onChange = (text: string) => {
695
+ const wasBashMode = this.isBashMode;
696
+ this.isBashMode = text.trimStart().startsWith("!");
697
+ if (wasBashMode !== this.isBashMode) {
698
+ this.updateEditorBorderColor();
699
+ }
700
+ };
701
+ }
702
+
703
+ private setupEditorSubmitHandler(): void {
704
+ this.editor.onSubmit = async (text: string) => {
705
+ text = text.trim();
706
+ if (!text) return;
707
+
708
+ // Handle slash commands
709
+ if (text === "/settings") {
710
+ this.showSettingsSelector();
711
+ this.editor.setText("");
712
+ return;
713
+ }
714
+ if (text === "/model") {
715
+ this.showModelSelector();
716
+ this.editor.setText("");
717
+ return;
718
+ }
719
+ if (text.startsWith("/export")) {
720
+ this.handleExportCommand(text);
721
+ this.editor.setText("");
722
+ return;
723
+ }
724
+ if (text === "/share") {
725
+ await this.handleShareCommand();
726
+ this.editor.setText("");
727
+ return;
728
+ }
729
+ if (text === "/copy") {
730
+ await this.handleCopyCommand();
731
+ this.editor.setText("");
732
+ return;
733
+ }
734
+ if (text === "/session") {
735
+ this.handleSessionCommand();
736
+ this.editor.setText("");
737
+ return;
738
+ }
739
+ if (text === "/changelog") {
740
+ this.handleChangelogCommand();
741
+ this.editor.setText("");
742
+ return;
743
+ }
744
+ if (text === "/hotkeys") {
745
+ this.handleHotkeysCommand();
746
+ this.editor.setText("");
747
+ return;
748
+ }
749
+ if (text === "/status") {
750
+ this.handleStatusCommand();
751
+ this.editor.setText("");
752
+ return;
753
+ }
754
+ if (text === "/branch") {
755
+ this.showUserMessageSelector();
756
+ this.editor.setText("");
757
+ return;
758
+ }
759
+ if (text === "/tree") {
760
+ this.showTreeSelector();
761
+ this.editor.setText("");
762
+ return;
763
+ }
764
+ if (text === "/login") {
765
+ this.showOAuthSelector("login");
766
+ this.editor.setText("");
767
+ return;
768
+ }
769
+ if (text === "/logout") {
770
+ this.showOAuthSelector("logout");
771
+ this.editor.setText("");
772
+ return;
773
+ }
774
+ if (text === "/new") {
775
+ this.editor.setText("");
776
+ await this.handleClearCommand();
777
+ return;
778
+ }
779
+ if (text === "/compact" || text.startsWith("/compact ")) {
780
+ const customInstructions = text.startsWith("/compact ") ? text.slice(9).trim() : undefined;
781
+ this.editor.setText("");
782
+ this.editor.disableSubmit = true;
783
+ try {
784
+ await this.handleCompactCommand(customInstructions);
785
+ } finally {
786
+ this.editor.disableSubmit = false;
787
+ }
788
+ return;
789
+ }
790
+ if (text === "/debug") {
791
+ this.handleDebugCommand();
792
+ this.editor.setText("");
793
+ return;
794
+ }
795
+ if (text === "/arminsayshi") {
796
+ this.handleArminSaysHi();
797
+ this.editor.setText("");
798
+ return;
799
+ }
800
+ if (text === "/resume") {
801
+ this.showSessionSelector();
802
+ this.editor.setText("");
803
+ return;
804
+ }
805
+
806
+ // Handle bash command
807
+ if (text.startsWith("!")) {
808
+ const command = text.slice(1).trim();
809
+ if (command) {
810
+ if (this.session.isBashRunning) {
811
+ this.showWarning("A bash command is already running. Press Esc to cancel it first.");
812
+ this.editor.setText(text);
813
+ return;
814
+ }
815
+ this.editor.addToHistory(text);
816
+ await this.handleBashCommand(command);
817
+ this.isBashMode = false;
818
+ this.updateEditorBorderColor();
819
+ return;
820
+ }
821
+ }
822
+
823
+ // Block input during compaction
824
+ if (this.session.isCompacting) {
825
+ return;
826
+ }
827
+
828
+ // Hook commands always run immediately, even during streaming
829
+ // (if they need to interact with LLM, they use pi.sendMessage which handles queueing)
830
+ if (text.startsWith("/") && this.session.hookRunner) {
831
+ const spaceIndex = text.indexOf(" ");
832
+ const commandName = spaceIndex === -1 ? text.slice(1) : text.slice(1, spaceIndex);
833
+ const command = this.session.hookRunner.getCommand(commandName);
834
+ if (command) {
835
+ this.editor.addToHistory(text);
836
+ this.editor.setText("");
837
+ await this.session.prompt(text);
838
+ return;
839
+ }
840
+ }
841
+
842
+ // Queue regular messages if agent is streaming
843
+ if (this.session.isStreaming) {
844
+ await this.session.queueMessage(text);
845
+ this.updatePendingMessagesDisplay();
846
+ this.editor.addToHistory(text);
847
+ this.editor.setText("");
848
+ this.ui.requestRender();
849
+ return;
850
+ }
851
+
852
+ // Normal message submission
853
+ // First, move any pending bash components to chat
854
+ this.flushPendingBashComponents();
855
+
856
+ if (this.onInputCallback) {
857
+ // Include any pending images from clipboard paste
858
+ const images = this.pendingImages.length > 0 ? [...this.pendingImages] : undefined;
859
+ this.pendingImages = [];
860
+ this.onInputCallback({ text, images });
861
+ }
862
+ this.editor.addToHistory(text);
863
+ };
864
+ }
865
+
866
+ private subscribeToAgent(): void {
867
+ this.unsubscribe = this.session.subscribe(async (event) => {
868
+ await this.handleEvent(event);
869
+ });
870
+ }
871
+
872
+ private async handleEvent(event: AgentSessionEvent): Promise<void> {
873
+ if (!this.isInitialized) {
874
+ await this.init();
875
+ }
876
+
877
+ this.footer.invalidate();
878
+
879
+ switch (event.type) {
880
+ case "agent_start":
881
+ if (this.loadingAnimation) {
882
+ this.loadingAnimation.stop();
883
+ }
884
+ this.statusContainer.clear();
885
+ this.loadingAnimation = new Loader(
886
+ this.ui,
887
+ (spinner) => theme.fg("accent", spinner),
888
+ (text) => theme.fg("muted", text),
889
+ "Working... (esc to interrupt)",
890
+ );
891
+ this.statusContainer.addChild(this.loadingAnimation);
892
+ this.ui.requestRender();
893
+ break;
894
+
895
+ case "message_start":
896
+ if (event.message.role === "hookMessage") {
897
+ this.addMessageToChat(event.message);
898
+ this.ui.requestRender();
899
+ } else if (event.message.role === "user") {
900
+ this.addMessageToChat(event.message);
901
+ this.editor.setText("");
902
+ this.updatePendingMessagesDisplay();
903
+ this.ui.requestRender();
904
+ } else if (event.message.role === "assistant") {
905
+ this.streamingComponent = new AssistantMessageComponent(undefined, this.hideThinkingBlock);
906
+ this.streamingMessage = event.message;
907
+ this.chatContainer.addChild(this.streamingComponent);
908
+ this.streamingComponent.updateContent(this.streamingMessage);
909
+ this.ui.requestRender();
910
+ }
911
+ break;
912
+
913
+ case "message_update":
914
+ if (this.streamingComponent && event.message.role === "assistant") {
915
+ this.streamingMessage = event.message;
916
+ this.streamingComponent.updateContent(this.streamingMessage);
917
+
918
+ for (const content of this.streamingMessage.content) {
919
+ if (content.type === "toolCall") {
920
+ if (!this.pendingTools.has(content.id)) {
921
+ this.chatContainer.addChild(new Text("", 0, 0));
922
+ const component = new ToolExecutionComponent(
923
+ content.name,
924
+ content.arguments,
925
+ {
926
+ showImages: this.settingsManager.getShowImages(),
927
+ },
928
+ this.customTools.get(content.name)?.tool,
929
+ this.ui,
930
+ );
931
+ component.setExpanded(this.toolOutputExpanded);
932
+ this.chatContainer.addChild(component);
933
+ this.pendingTools.set(content.id, component);
934
+ } else {
935
+ const component = this.pendingTools.get(content.id);
936
+ if (component) {
937
+ component.updateArgs(content.arguments);
938
+ }
939
+ }
940
+ }
941
+ }
942
+ this.ui.requestRender();
943
+ }
944
+ break;
945
+
946
+ case "message_end":
947
+ if (event.message.role === "user") break;
948
+ if (this.streamingComponent && event.message.role === "assistant") {
949
+ this.streamingMessage = event.message;
950
+ this.streamingComponent.updateContent(this.streamingMessage);
951
+
952
+ if (this.streamingMessage.stopReason === "aborted" || this.streamingMessage.stopReason === "error") {
953
+ const errorMessage =
954
+ this.streamingMessage.stopReason === "aborted"
955
+ ? "Operation aborted"
956
+ : this.streamingMessage.errorMessage || "Error";
957
+ for (const [, component] of this.pendingTools.entries()) {
958
+ component.updateResult({
959
+ content: [{ type: "text", text: errorMessage }],
960
+ isError: true,
961
+ });
962
+ }
963
+ this.pendingTools.clear();
964
+ } else {
965
+ // Args are now complete - trigger diff computation for edit tools
966
+ for (const [, component] of this.pendingTools.entries()) {
967
+ component.setArgsComplete();
968
+ }
969
+ }
970
+ this.streamingComponent = undefined;
971
+ this.streamingMessage = undefined;
972
+ this.footer.invalidate();
973
+ }
974
+ this.ui.requestRender();
975
+ break;
976
+
977
+ case "tool_execution_start": {
978
+ if (!this.pendingTools.has(event.toolCallId)) {
979
+ const component = new ToolExecutionComponent(
980
+ event.toolName,
981
+ event.args,
982
+ {
983
+ showImages: this.settingsManager.getShowImages(),
984
+ },
985
+ this.customTools.get(event.toolName)?.tool,
986
+ this.ui,
987
+ );
988
+ component.setExpanded(this.toolOutputExpanded);
989
+ this.chatContainer.addChild(component);
990
+ this.pendingTools.set(event.toolCallId, component);
991
+ this.ui.requestRender();
992
+ }
993
+ break;
994
+ }
995
+
996
+ case "tool_execution_update": {
997
+ const component = this.pendingTools.get(event.toolCallId);
998
+ if (component) {
999
+ component.updateResult({ ...event.partialResult, isError: false }, true);
1000
+ this.ui.requestRender();
1001
+ }
1002
+ break;
1003
+ }
1004
+
1005
+ case "tool_execution_end": {
1006
+ const component = this.pendingTools.get(event.toolCallId);
1007
+ if (component) {
1008
+ component.updateResult({ ...event.result, isError: event.isError });
1009
+ this.pendingTools.delete(event.toolCallId);
1010
+ this.ui.requestRender();
1011
+ }
1012
+ break;
1013
+ }
1014
+
1015
+ case "agent_end":
1016
+ if (this.loadingAnimation) {
1017
+ this.loadingAnimation.stop();
1018
+ this.loadingAnimation = undefined;
1019
+ this.statusContainer.clear();
1020
+ }
1021
+ if (this.streamingComponent) {
1022
+ this.chatContainer.removeChild(this.streamingComponent);
1023
+ this.streamingComponent = undefined;
1024
+ this.streamingMessage = undefined;
1025
+ }
1026
+ this.pendingTools.clear();
1027
+ this.ui.requestRender();
1028
+ break;
1029
+
1030
+ case "auto_compaction_start": {
1031
+ // Disable submit to preserve editor text during compaction
1032
+ this.editor.disableSubmit = true;
1033
+ // Set up escape to abort auto-compaction
1034
+ this.autoCompactionEscapeHandler = this.editor.onEscape;
1035
+ this.editor.onEscape = () => {
1036
+ this.session.abortCompaction();
1037
+ };
1038
+ // Show compacting indicator with reason
1039
+ this.statusContainer.clear();
1040
+ const reasonText = event.reason === "overflow" ? "Context overflow detected, " : "";
1041
+ this.autoCompactionLoader = new Loader(
1042
+ this.ui,
1043
+ (spinner) => theme.fg("accent", spinner),
1044
+ (text) => theme.fg("muted", text),
1045
+ `${reasonText}Auto-compacting... (esc to cancel)`,
1046
+ );
1047
+ this.statusContainer.addChild(this.autoCompactionLoader);
1048
+ this.ui.requestRender();
1049
+ break;
1050
+ }
1051
+
1052
+ case "auto_compaction_end": {
1053
+ // Re-enable submit
1054
+ this.editor.disableSubmit = false;
1055
+ // Restore escape handler
1056
+ if (this.autoCompactionEscapeHandler) {
1057
+ this.editor.onEscape = this.autoCompactionEscapeHandler;
1058
+ this.autoCompactionEscapeHandler = undefined;
1059
+ }
1060
+ // Stop loader
1061
+ if (this.autoCompactionLoader) {
1062
+ this.autoCompactionLoader.stop();
1063
+ this.autoCompactionLoader = undefined;
1064
+ this.statusContainer.clear();
1065
+ }
1066
+ // Handle result
1067
+ if (event.aborted) {
1068
+ this.showStatus("Auto-compaction cancelled");
1069
+ } else if (event.result) {
1070
+ // Rebuild chat to show compacted state
1071
+ this.chatContainer.clear();
1072
+ this.rebuildChatFromMessages();
1073
+ // Add compaction component at bottom so user sees it without scrolling
1074
+ this.addMessageToChat({
1075
+ role: "compactionSummary",
1076
+ tokensBefore: event.result.tokensBefore,
1077
+ summary: event.result.summary,
1078
+ timestamp: Date.now(),
1079
+ });
1080
+ this.footer.invalidate();
1081
+ }
1082
+ this.ui.requestRender();
1083
+ break;
1084
+ }
1085
+
1086
+ case "auto_retry_start": {
1087
+ // Set up escape to abort retry
1088
+ this.retryEscapeHandler = this.editor.onEscape;
1089
+ this.editor.onEscape = () => {
1090
+ this.session.abortRetry();
1091
+ };
1092
+ // Show retry indicator
1093
+ this.statusContainer.clear();
1094
+ const delaySeconds = Math.round(event.delayMs / 1000);
1095
+ this.retryLoader = new Loader(
1096
+ this.ui,
1097
+ (spinner) => theme.fg("warning", spinner),
1098
+ (text) => theme.fg("muted", text),
1099
+ `Retrying (${event.attempt}/${event.maxAttempts}) in ${delaySeconds}s... (esc to cancel)`,
1100
+ );
1101
+ this.statusContainer.addChild(this.retryLoader);
1102
+ this.ui.requestRender();
1103
+ break;
1104
+ }
1105
+
1106
+ case "auto_retry_end": {
1107
+ // Restore escape handler
1108
+ if (this.retryEscapeHandler) {
1109
+ this.editor.onEscape = this.retryEscapeHandler;
1110
+ this.retryEscapeHandler = undefined;
1111
+ }
1112
+ // Stop loader
1113
+ if (this.retryLoader) {
1114
+ this.retryLoader.stop();
1115
+ this.retryLoader = undefined;
1116
+ this.statusContainer.clear();
1117
+ }
1118
+ // Show error only on final failure (success shows normal response)
1119
+ if (!event.success) {
1120
+ this.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`);
1121
+ }
1122
+ this.ui.requestRender();
1123
+ break;
1124
+ }
1125
+ }
1126
+ }
1127
+
1128
+ /** Extract text content from a user message */
1129
+ private getUserMessageText(message: Message): string {
1130
+ if (message.role !== "user") return "";
1131
+ const textBlocks =
1132
+ typeof message.content === "string"
1133
+ ? [{ type: "text", text: message.content }]
1134
+ : message.content.filter((c: { type: string }) => c.type === "text");
1135
+ return textBlocks.map((c) => (c as { text: string }).text).join("");
1136
+ }
1137
+
1138
+ /**
1139
+ * Show a status message in the chat.
1140
+ *
1141
+ * If multiple status messages are emitted back-to-back (without anything else being added to the chat),
1142
+ * we update the previous status line instead of appending new ones to avoid log spam.
1143
+ */
1144
+ private showStatus(message: string): void {
1145
+ const children = this.chatContainer.children;
1146
+ const last = children.length > 0 ? children[children.length - 1] : undefined;
1147
+ const secondLast = children.length > 1 ? children[children.length - 2] : undefined;
1148
+
1149
+ if (last && secondLast && last === this.lastStatusText && secondLast === this.lastStatusSpacer) {
1150
+ this.lastStatusText.setText(theme.fg("dim", message));
1151
+ this.ui.requestRender();
1152
+ return;
1153
+ }
1154
+
1155
+ const spacer = new Spacer(1);
1156
+ const text = new Text(theme.fg("dim", message), 1, 0);
1157
+ this.chatContainer.addChild(spacer);
1158
+ this.chatContainer.addChild(text);
1159
+ this.lastStatusSpacer = spacer;
1160
+ this.lastStatusText = text;
1161
+ this.ui.requestRender();
1162
+ }
1163
+
1164
+ private addMessageToChat(message: AgentMessage, options?: { populateHistory?: boolean }): void {
1165
+ switch (message.role) {
1166
+ case "bashExecution": {
1167
+ const component = new BashExecutionComponent(message.command, this.ui);
1168
+ if (message.output) {
1169
+ component.appendOutput(message.output);
1170
+ }
1171
+ component.setComplete(
1172
+ message.exitCode,
1173
+ message.cancelled,
1174
+ message.truncated ? ({ truncated: true } as TruncationResult) : undefined,
1175
+ message.fullOutputPath,
1176
+ );
1177
+ this.chatContainer.addChild(component);
1178
+ break;
1179
+ }
1180
+ case "hookMessage": {
1181
+ if (message.display) {
1182
+ const renderer = this.session.hookRunner?.getMessageRenderer(message.customType);
1183
+ this.chatContainer.addChild(new HookMessageComponent(message, renderer));
1184
+ }
1185
+ break;
1186
+ }
1187
+ case "compactionSummary": {
1188
+ this.chatContainer.addChild(new Spacer(1));
1189
+ const component = new CompactionSummaryMessageComponent(message);
1190
+ component.setExpanded(this.toolOutputExpanded);
1191
+ this.chatContainer.addChild(component);
1192
+ break;
1193
+ }
1194
+ case "branchSummary": {
1195
+ this.chatContainer.addChild(new Spacer(1));
1196
+ const component = new BranchSummaryMessageComponent(message);
1197
+ component.setExpanded(this.toolOutputExpanded);
1198
+ this.chatContainer.addChild(component);
1199
+ break;
1200
+ }
1201
+ case "user": {
1202
+ const textContent = this.getUserMessageText(message);
1203
+ if (textContent) {
1204
+ const userComponent = new UserMessageComponent(textContent);
1205
+ this.chatContainer.addChild(userComponent);
1206
+ if (options?.populateHistory) {
1207
+ this.editor.addToHistory(textContent);
1208
+ }
1209
+ }
1210
+ break;
1211
+ }
1212
+ case "assistant": {
1213
+ const assistantComponent = new AssistantMessageComponent(message, this.hideThinkingBlock);
1214
+ this.chatContainer.addChild(assistantComponent);
1215
+ break;
1216
+ }
1217
+ case "toolResult": {
1218
+ // Tool results are rendered inline with tool calls, handled separately
1219
+ break;
1220
+ }
1221
+ default: {
1222
+ const _exhaustive: never = message;
1223
+ }
1224
+ }
1225
+ }
1226
+
1227
+ /**
1228
+ * Render session context to chat. Used for initial load and rebuild after compaction.
1229
+ * @param sessionContext Session context to render
1230
+ * @param options.updateFooter Update footer state
1231
+ * @param options.populateHistory Add user messages to editor history
1232
+ */
1233
+ private renderSessionContext(
1234
+ sessionContext: SessionContext,
1235
+ options: { updateFooter?: boolean; populateHistory?: boolean } = {},
1236
+ ): void {
1237
+ this.pendingTools.clear();
1238
+
1239
+ if (options.updateFooter) {
1240
+ this.footer.invalidate();
1241
+ this.updateEditorBorderColor();
1242
+ }
1243
+
1244
+ for (const message of sessionContext.messages) {
1245
+ // Assistant messages need special handling for tool calls
1246
+ if (message.role === "assistant") {
1247
+ this.addMessageToChat(message);
1248
+ // Render tool call components
1249
+ for (const content of message.content) {
1250
+ if (content.type === "toolCall") {
1251
+ const component = new ToolExecutionComponent(
1252
+ content.name,
1253
+ content.arguments,
1254
+ { showImages: this.settingsManager.getShowImages() },
1255
+ this.customTools.get(content.name)?.tool,
1256
+ this.ui,
1257
+ );
1258
+ component.setExpanded(this.toolOutputExpanded);
1259
+ this.chatContainer.addChild(component);
1260
+
1261
+ if (message.stopReason === "aborted" || message.stopReason === "error") {
1262
+ const errorMessage =
1263
+ message.stopReason === "aborted" ? "Operation aborted" : message.errorMessage || "Error";
1264
+ component.updateResult({ content: [{ type: "text", text: errorMessage }], isError: true });
1265
+ } else {
1266
+ this.pendingTools.set(content.id, component);
1267
+ }
1268
+ }
1269
+ }
1270
+ } else if (message.role === "toolResult") {
1271
+ // Match tool results to pending tool components
1272
+ const component = this.pendingTools.get(message.toolCallId);
1273
+ if (component) {
1274
+ component.updateResult(message);
1275
+ this.pendingTools.delete(message.toolCallId);
1276
+ }
1277
+ } else {
1278
+ // All other messages use standard rendering
1279
+ this.addMessageToChat(message, options);
1280
+ }
1281
+ }
1282
+
1283
+ this.pendingTools.clear();
1284
+ this.ui.requestRender();
1285
+ }
1286
+
1287
+ renderInitialMessages(): void {
1288
+ // Get aligned messages and entries from session context
1289
+ const context = this.sessionManager.buildSessionContext();
1290
+ this.renderSessionContext(context, {
1291
+ updateFooter: true,
1292
+ populateHistory: true,
1293
+ });
1294
+
1295
+ // Show compaction info if session was compacted
1296
+ const allEntries = this.sessionManager.getEntries();
1297
+ const compactionCount = allEntries.filter((e) => e.type === "compaction").length;
1298
+ if (compactionCount > 0) {
1299
+ const times = compactionCount === 1 ? "1 time" : `${compactionCount} times`;
1300
+ this.showStatus(`Session compacted ${times}`);
1301
+ }
1302
+ }
1303
+
1304
+ async getUserInput(): Promise<{ text: string; images?: ImageContent[] }> {
1305
+ return new Promise((resolve) => {
1306
+ this.onInputCallback = (input) => {
1307
+ this.onInputCallback = undefined;
1308
+ resolve(input);
1309
+ };
1310
+ });
1311
+ }
1312
+
1313
+ private rebuildChatFromMessages(): void {
1314
+ this.chatContainer.clear();
1315
+ const context = this.sessionManager.buildSessionContext();
1316
+ this.renderSessionContext(context);
1317
+ }
1318
+
1319
+ // =========================================================================
1320
+ // Key handlers
1321
+ // =========================================================================
1322
+
1323
+ private handleCtrlC(): void {
1324
+ const now = Date.now();
1325
+ if (now - this.lastSigintTime < 500) {
1326
+ void this.shutdown();
1327
+ } else {
1328
+ this.clearEditor();
1329
+ this.lastSigintTime = now;
1330
+ }
1331
+ }
1332
+
1333
+ private handleCtrlD(): void {
1334
+ // Only called when editor is empty (enforced by CustomEditor)
1335
+ void this.shutdown();
1336
+ }
1337
+
1338
+ /**
1339
+ * Gracefully shutdown the agent.
1340
+ * Emits shutdown event to hooks and tools, then exits.
1341
+ */
1342
+ private async shutdown(): Promise<void> {
1343
+ // Emit shutdown event to hooks
1344
+ const hookRunner = this.session.hookRunner;
1345
+ if (hookRunner?.hasHandlers("session_shutdown")) {
1346
+ await hookRunner.emit({
1347
+ type: "session_shutdown",
1348
+ });
1349
+ }
1350
+
1351
+ // Emit shutdown event to custom tools
1352
+ await this.session.emitCustomToolSessionEvent("shutdown");
1353
+
1354
+ this.stop();
1355
+ process.exit(0);
1356
+ }
1357
+
1358
+ private handleCtrlZ(): void {
1359
+ // Set up handler to restore TUI when resumed
1360
+ process.once("SIGCONT", () => {
1361
+ this.ui.start();
1362
+ this.ui.requestRender(true);
1363
+ });
1364
+
1365
+ // Stop the TUI (restore terminal to normal mode)
1366
+ this.ui.stop();
1367
+
1368
+ // Send SIGTSTP to process group (pid=0 means all processes in group)
1369
+ process.kill(0, "SIGTSTP");
1370
+ }
1371
+
1372
+ /**
1373
+ * Handle Ctrl+V for image paste from clipboard.
1374
+ * Returns true if an image was found and added, false otherwise.
1375
+ */
1376
+ private async handleImagePaste(): Promise<boolean> {
1377
+ try {
1378
+ const image = await readImageFromClipboard();
1379
+ if (image) {
1380
+ this.pendingImages.push({
1381
+ type: "image",
1382
+ data: image.data,
1383
+ mimeType: image.mimeType,
1384
+ });
1385
+ // Insert styled placeholder at cursor like Claude does
1386
+ const imageNum = this.pendingImages.length;
1387
+ const placeholder = theme.bold(theme.underline(`[Image #${imageNum}]`));
1388
+ this.editor.insertText(`${placeholder} `);
1389
+ this.ui.requestRender();
1390
+ return true;
1391
+ }
1392
+ // No image in clipboard - show hint
1393
+ this.showStatus("No image in clipboard (use terminal paste for text)");
1394
+ return false;
1395
+ } catch {
1396
+ this.showStatus("Failed to read clipboard");
1397
+ return false;
1398
+ }
1399
+ }
1400
+
1401
+ private updateEditorBorderColor(): void {
1402
+ if (this.isBashMode) {
1403
+ this.editor.borderColor = theme.getBashModeBorderColor();
1404
+ } else {
1405
+ const level = this.session.thinkingLevel || "off";
1406
+ this.editor.borderColor = theme.getThinkingBorderColor(level);
1407
+ }
1408
+ this.ui.requestRender();
1409
+ }
1410
+
1411
+ private cycleThinkingLevel(): void {
1412
+ const newLevel = this.session.cycleThinkingLevel();
1413
+ if (newLevel === undefined) {
1414
+ this.showStatus("Current model does not support thinking");
1415
+ } else {
1416
+ this.footer.invalidate();
1417
+ this.updateEditorBorderColor();
1418
+ this.showStatus(`Thinking level: ${newLevel}`);
1419
+ }
1420
+ }
1421
+
1422
+ private async cycleModel(direction: "forward" | "backward"): Promise<void> {
1423
+ try {
1424
+ const result = await this.session.cycleModel(direction);
1425
+ if (result === undefined) {
1426
+ const msg = this.session.scopedModels.length > 0 ? "Only one model in scope" : "Only one model available";
1427
+ this.showStatus(msg);
1428
+ } else {
1429
+ this.footer.invalidate();
1430
+ this.updateEditorBorderColor();
1431
+ const thinkingStr =
1432
+ result.model.reasoning && result.thinkingLevel !== "off" ? ` (thinking: ${result.thinkingLevel})` : "";
1433
+ this.showStatus(`Switched to ${result.model.name || result.model.id}${thinkingStr}`);
1434
+ }
1435
+ } catch (error) {
1436
+ this.showError(error instanceof Error ? error.message : String(error));
1437
+ }
1438
+ }
1439
+
1440
+ private toggleToolOutputExpansion(): void {
1441
+ this.toolOutputExpanded = !this.toolOutputExpanded;
1442
+ for (const child of this.chatContainer.children) {
1443
+ if (isExpandable(child)) {
1444
+ child.setExpanded(this.toolOutputExpanded);
1445
+ }
1446
+ }
1447
+ this.ui.requestRender();
1448
+ }
1449
+
1450
+ private toggleThinkingBlockVisibility(): void {
1451
+ this.hideThinkingBlock = !this.hideThinkingBlock;
1452
+ this.settingsManager.setHideThinkingBlock(this.hideThinkingBlock);
1453
+
1454
+ // Rebuild chat from session messages
1455
+ this.chatContainer.clear();
1456
+ this.rebuildChatFromMessages();
1457
+
1458
+ // If streaming, re-add the streaming component with updated visibility and re-render
1459
+ if (this.streamingComponent && this.streamingMessage) {
1460
+ this.streamingComponent.setHideThinkingBlock(this.hideThinkingBlock);
1461
+ this.streamingComponent.updateContent(this.streamingMessage);
1462
+ this.chatContainer.addChild(this.streamingComponent);
1463
+ }
1464
+
1465
+ this.showStatus(`Thinking blocks: ${this.hideThinkingBlock ? "hidden" : "visible"}`);
1466
+ }
1467
+
1468
+ private openExternalEditor(): void {
1469
+ // Determine editor (respect $VISUAL, then $EDITOR)
1470
+ const editorCmd = process.env.VISUAL || process.env.EDITOR;
1471
+ if (!editorCmd) {
1472
+ this.showWarning("No editor configured. Set $VISUAL or $EDITOR environment variable.");
1473
+ return;
1474
+ }
1475
+
1476
+ const currentText = this.editor.getText();
1477
+ const tmpFile = path.join(os.tmpdir(), `pi-editor-${Date.now()}.pi.md`);
1478
+
1479
+ try {
1480
+ // Write current content to temp file
1481
+ fs.writeFileSync(tmpFile, currentText, "utf-8");
1482
+
1483
+ // Stop TUI to release terminal
1484
+ this.ui.stop();
1485
+
1486
+ // Split by space to support editor arguments (e.g., "code --wait")
1487
+ const [editor, ...editorArgs] = editorCmd.split(" ");
1488
+
1489
+ // Spawn editor synchronously with inherited stdio for interactive editing
1490
+ const result = Bun.spawnSync([editor, ...editorArgs, tmpFile], {
1491
+ stdin: "inherit",
1492
+ stdout: "inherit",
1493
+ stderr: "inherit",
1494
+ });
1495
+
1496
+ // On successful exit (exitCode 0), replace editor content
1497
+ if (result.exitCode === 0) {
1498
+ const newContent = fs.readFileSync(tmpFile, "utf-8").replace(/\n$/, "");
1499
+ this.editor.setText(newContent);
1500
+ }
1501
+ // On non-zero exit, keep original text (no action needed)
1502
+ } finally {
1503
+ // Clean up temp file
1504
+ try {
1505
+ fs.unlinkSync(tmpFile);
1506
+ } catch {
1507
+ // Ignore cleanup errors
1508
+ }
1509
+
1510
+ // Restart TUI
1511
+ this.ui.start();
1512
+ this.ui.requestRender();
1513
+ }
1514
+ }
1515
+
1516
+ // =========================================================================
1517
+ // UI helpers
1518
+ // =========================================================================
1519
+
1520
+ clearEditor(): void {
1521
+ this.editor.setText("");
1522
+ this.pendingImages = [];
1523
+ this.ui.requestRender();
1524
+ }
1525
+
1526
+ showError(errorMessage: string): void {
1527
+ this.chatContainer.addChild(new Spacer(1));
1528
+ this.chatContainer.addChild(new Text(theme.fg("error", `Error: ${errorMessage}`), 1, 0));
1529
+ this.ui.requestRender();
1530
+ }
1531
+
1532
+ showWarning(warningMessage: string): void {
1533
+ this.chatContainer.addChild(new Spacer(1));
1534
+ this.chatContainer.addChild(new Text(theme.fg("warning", `Warning: ${warningMessage}`), 1, 0));
1535
+ this.ui.requestRender();
1536
+ }
1537
+
1538
+ showNewVersionNotification(newVersion: string): void {
1539
+ this.chatContainer.addChild(new Spacer(1));
1540
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
1541
+ this.chatContainer.addChild(
1542
+ new Text(
1543
+ theme.bold(theme.fg("warning", "Update Available")) +
1544
+ "\n" +
1545
+ theme.fg("muted", `New version ${newVersion} is available. Run: `) +
1546
+ theme.fg("accent", "npm install -g @oh-my-pi/pi-coding-agent"),
1547
+ 1,
1548
+ 0,
1549
+ ),
1550
+ );
1551
+ this.chatContainer.addChild(new DynamicBorder((text) => theme.fg("warning", text)));
1552
+ this.ui.requestRender();
1553
+ }
1554
+
1555
+ private updatePendingMessagesDisplay(): void {
1556
+ this.pendingMessagesContainer.clear();
1557
+ const queuedMessages = this.session.getQueuedMessages();
1558
+ if (queuedMessages.length > 0) {
1559
+ this.pendingMessagesContainer.addChild(new Spacer(1));
1560
+ for (const message of queuedMessages) {
1561
+ const queuedText = theme.fg("dim", `Queued: ${message}`);
1562
+ this.pendingMessagesContainer.addChild(new TruncatedText(queuedText, 1, 0));
1563
+ }
1564
+ }
1565
+ }
1566
+
1567
+ /** Move pending bash components from pending area to chat */
1568
+ private flushPendingBashComponents(): void {
1569
+ for (const component of this.pendingBashComponents) {
1570
+ this.pendingMessagesContainer.removeChild(component);
1571
+ this.chatContainer.addChild(component);
1572
+ }
1573
+ this.pendingBashComponents = [];
1574
+ }
1575
+
1576
+ // =========================================================================
1577
+ // Selectors
1578
+ // =========================================================================
1579
+
1580
+ /**
1581
+ * Shows a selector component in place of the editor.
1582
+ * @param create Factory that receives a `done` callback and returns the component and focus target
1583
+ */
1584
+ private showSelector(create: (done: () => void) => { component: Component; focus: Component }): void {
1585
+ const done = () => {
1586
+ this.editorContainer.clear();
1587
+ this.editorContainer.addChild(this.editor);
1588
+ this.ui.setFocus(this.editor);
1589
+ };
1590
+ const { component, focus } = create(done);
1591
+ this.editorContainer.clear();
1592
+ this.editorContainer.addChild(component);
1593
+ this.ui.setFocus(focus);
1594
+ this.ui.requestRender();
1595
+ }
1596
+
1597
+ private showSettingsSelector(): void {
1598
+ this.showSelector((done) => {
1599
+ const selector = new SettingsSelectorComponent(
1600
+ {
1601
+ autoCompact: this.session.autoCompactionEnabled,
1602
+ showImages: this.settingsManager.getShowImages(),
1603
+ queueMode: this.session.queueMode,
1604
+ thinkingLevel: this.session.thinkingLevel,
1605
+ availableThinkingLevels: this.session.getAvailableThinkingLevels(),
1606
+ currentTheme: this.settingsManager.getTheme() || "dark",
1607
+ availableThemes: getAvailableThemes(),
1608
+ hideThinkingBlock: this.hideThinkingBlock,
1609
+ collapseChangelog: this.settingsManager.getCollapseChangelog(),
1610
+ cwd: process.cwd(),
1611
+ exa: this.settingsManager.getExaSettings(),
1612
+ },
1613
+ {
1614
+ onAutoCompactChange: (enabled) => {
1615
+ this.session.setAutoCompactionEnabled(enabled);
1616
+ this.footer.setAutoCompactEnabled(enabled);
1617
+ },
1618
+ onShowImagesChange: (enabled) => {
1619
+ this.settingsManager.setShowImages(enabled);
1620
+ for (const child of this.chatContainer.children) {
1621
+ if (child instanceof ToolExecutionComponent) {
1622
+ child.setShowImages(enabled);
1623
+ }
1624
+ }
1625
+ },
1626
+ onQueueModeChange: (mode) => {
1627
+ this.session.setQueueMode(mode);
1628
+ },
1629
+ onThinkingLevelChange: (level) => {
1630
+ this.session.setThinkingLevel(level);
1631
+ this.footer.invalidate();
1632
+ this.updateEditorBorderColor();
1633
+ },
1634
+ onThemeChange: (themeName) => {
1635
+ const result = setTheme(themeName, true);
1636
+ this.settingsManager.setTheme(themeName);
1637
+ this.ui.invalidate();
1638
+ if (!result.success) {
1639
+ this.showError(`Failed to load theme "${themeName}": ${result.error}\nFell back to dark theme.`);
1640
+ }
1641
+ },
1642
+ onThemePreview: (themeName) => {
1643
+ const result = setTheme(themeName, true);
1644
+ if (result.success) {
1645
+ this.ui.invalidate();
1646
+ this.ui.requestRender();
1647
+ }
1648
+ },
1649
+ onHideThinkingBlockChange: (hidden) => {
1650
+ this.hideThinkingBlock = hidden;
1651
+ this.settingsManager.setHideThinkingBlock(hidden);
1652
+ for (const child of this.chatContainer.children) {
1653
+ if (child instanceof AssistantMessageComponent) {
1654
+ child.setHideThinkingBlock(hidden);
1655
+ }
1656
+ }
1657
+ this.chatContainer.clear();
1658
+ this.rebuildChatFromMessages();
1659
+ },
1660
+ onCollapseChangelogChange: (collapsed) => {
1661
+ this.settingsManager.setCollapseChangelog(collapsed);
1662
+ },
1663
+ onPluginsChanged: () => {
1664
+ // Plugin config changed - could trigger reload if needed
1665
+ this.ui.requestRender();
1666
+ },
1667
+ onExaSettingChange: (setting, enabled) => {
1668
+ switch (setting) {
1669
+ case "enabled":
1670
+ this.settingsManager.setExaEnabled(enabled);
1671
+ break;
1672
+ case "enableSearch":
1673
+ this.settingsManager.setExaSearchEnabled(enabled);
1674
+ break;
1675
+ case "enableLinkedin":
1676
+ this.settingsManager.setExaLinkedinEnabled(enabled);
1677
+ break;
1678
+ case "enableCompany":
1679
+ this.settingsManager.setExaCompanyEnabled(enabled);
1680
+ break;
1681
+ case "enableResearcher":
1682
+ this.settingsManager.setExaResearcherEnabled(enabled);
1683
+ break;
1684
+ case "enableWebsets":
1685
+ this.settingsManager.setExaWebsetsEnabled(enabled);
1686
+ break;
1687
+ }
1688
+ },
1689
+ onCancel: () => {
1690
+ done();
1691
+ this.ui.requestRender();
1692
+ },
1693
+ },
1694
+ );
1695
+ return { component: selector, focus: selector };
1696
+ });
1697
+ }
1698
+
1699
+ private showModelSelector(): void {
1700
+ this.showSelector((done) => {
1701
+ const selector = new ModelSelectorComponent(
1702
+ this.ui,
1703
+ this.session.model,
1704
+ this.settingsManager,
1705
+ this.session.modelRegistry,
1706
+ this.session.scopedModels,
1707
+ async (model) => {
1708
+ try {
1709
+ await this.session.setModel(model);
1710
+ this.footer.invalidate();
1711
+ this.updateEditorBorderColor();
1712
+ done();
1713
+ this.showStatus(`Model: ${model.id}`);
1714
+ } catch (error) {
1715
+ done();
1716
+ this.showError(error instanceof Error ? error.message : String(error));
1717
+ }
1718
+ },
1719
+ () => {
1720
+ done();
1721
+ this.ui.requestRender();
1722
+ },
1723
+ );
1724
+ return { component: selector, focus: selector };
1725
+ });
1726
+ }
1727
+
1728
+ private showUserMessageSelector(): void {
1729
+ const userMessages = this.session.getUserMessagesForBranching();
1730
+
1731
+ if (userMessages.length === 0) {
1732
+ this.showStatus("No messages to branch from");
1733
+ return;
1734
+ }
1735
+
1736
+ this.showSelector((done) => {
1737
+ const selector = new UserMessageSelectorComponent(
1738
+ userMessages.map((m) => ({ id: m.entryId, text: m.text })),
1739
+ async (entryId) => {
1740
+ const result = await this.session.branch(entryId);
1741
+ if (result.cancelled) {
1742
+ // Hook cancelled the branch
1743
+ done();
1744
+ this.ui.requestRender();
1745
+ return;
1746
+ }
1747
+
1748
+ this.chatContainer.clear();
1749
+ this.renderInitialMessages();
1750
+ this.editor.setText(result.selectedText);
1751
+ done();
1752
+ this.showStatus("Branched to new session");
1753
+ },
1754
+ () => {
1755
+ done();
1756
+ this.ui.requestRender();
1757
+ },
1758
+ );
1759
+ return { component: selector, focus: selector.getMessageList() };
1760
+ });
1761
+ }
1762
+
1763
+ private showTreeSelector(): void {
1764
+ const tree = this.sessionManager.getTree();
1765
+ const realLeafId = this.sessionManager.getLeafId();
1766
+
1767
+ // Find the visible leaf for display (skip metadata entries like labels)
1768
+ let visibleLeafId = realLeafId;
1769
+ while (visibleLeafId) {
1770
+ const entry = this.sessionManager.getEntry(visibleLeafId);
1771
+ if (!entry) break;
1772
+ if (entry.type !== "label" && entry.type !== "custom") break;
1773
+ visibleLeafId = entry.parentId ?? null;
1774
+ }
1775
+
1776
+ if (tree.length === 0) {
1777
+ this.showStatus("No entries in session");
1778
+ return;
1779
+ }
1780
+
1781
+ this.showSelector((done) => {
1782
+ const selector = new TreeSelectorComponent(
1783
+ tree,
1784
+ visibleLeafId,
1785
+ this.ui.terminal.rows,
1786
+ async (entryId) => {
1787
+ // Selecting the visible leaf is a no-op (already there)
1788
+ if (entryId === visibleLeafId) {
1789
+ done();
1790
+ this.showStatus("Already at this point");
1791
+ return;
1792
+ }
1793
+
1794
+ // Ask about summarization
1795
+ done(); // Close selector first
1796
+
1797
+ const wantsSummary = await this.showHookConfirm(
1798
+ "Summarize branch?",
1799
+ "Create a summary of the branch you're leaving?",
1800
+ );
1801
+
1802
+ // Set up escape handler and loader if summarizing
1803
+ let summaryLoader: Loader | undefined;
1804
+ const originalOnEscape = this.editor.onEscape;
1805
+
1806
+ if (wantsSummary) {
1807
+ this.editor.onEscape = () => {
1808
+ this.session.abortBranchSummary();
1809
+ };
1810
+ this.chatContainer.addChild(new Spacer(1));
1811
+ summaryLoader = new Loader(
1812
+ this.ui,
1813
+ (spinner) => theme.fg("accent", spinner),
1814
+ (text) => theme.fg("muted", text),
1815
+ "Summarizing branch... (esc to cancel)",
1816
+ );
1817
+ this.statusContainer.addChild(summaryLoader);
1818
+ this.ui.requestRender();
1819
+ }
1820
+
1821
+ try {
1822
+ const result = await this.session.navigateTree(entryId, { summarize: wantsSummary });
1823
+
1824
+ if (result.aborted) {
1825
+ // Summarization aborted - re-show tree selector
1826
+ this.showStatus("Branch summarization cancelled");
1827
+ this.showTreeSelector();
1828
+ return;
1829
+ }
1830
+ if (result.cancelled) {
1831
+ this.showStatus("Navigation cancelled");
1832
+ return;
1833
+ }
1834
+
1835
+ // Update UI
1836
+ this.chatContainer.clear();
1837
+ this.renderInitialMessages();
1838
+ if (result.editorText) {
1839
+ this.editor.setText(result.editorText);
1840
+ }
1841
+ this.showStatus("Navigated to selected point");
1842
+ } catch (error) {
1843
+ this.showError(error instanceof Error ? error.message : String(error));
1844
+ } finally {
1845
+ if (summaryLoader) {
1846
+ summaryLoader.stop();
1847
+ this.statusContainer.clear();
1848
+ }
1849
+ this.editor.onEscape = originalOnEscape;
1850
+ }
1851
+ },
1852
+ () => {
1853
+ done();
1854
+ this.ui.requestRender();
1855
+ },
1856
+ (entryId, label) => {
1857
+ this.sessionManager.appendLabelChange(entryId, label);
1858
+ this.ui.requestRender();
1859
+ },
1860
+ );
1861
+ return { component: selector, focus: selector };
1862
+ });
1863
+ }
1864
+
1865
+ private showSessionSelector(): void {
1866
+ this.showSelector((done) => {
1867
+ const sessions = SessionManager.list(this.sessionManager.getCwd(), this.sessionManager.getSessionDir());
1868
+ const selector = new SessionSelectorComponent(
1869
+ sessions,
1870
+ async (sessionPath) => {
1871
+ done();
1872
+ await this.handleResumeSession(sessionPath);
1873
+ },
1874
+ () => {
1875
+ done();
1876
+ this.ui.requestRender();
1877
+ },
1878
+ () => {
1879
+ void this.shutdown();
1880
+ },
1881
+ );
1882
+ return { component: selector, focus: selector.getSessionList() };
1883
+ });
1884
+ }
1885
+
1886
+ private async handleResumeSession(sessionPath: string): Promise<void> {
1887
+ // Stop loading animation
1888
+ if (this.loadingAnimation) {
1889
+ this.loadingAnimation.stop();
1890
+ this.loadingAnimation = undefined;
1891
+ }
1892
+ this.statusContainer.clear();
1893
+
1894
+ // Clear UI state
1895
+ this.pendingMessagesContainer.clear();
1896
+ this.streamingComponent = undefined;
1897
+ this.streamingMessage = undefined;
1898
+ this.pendingTools.clear();
1899
+
1900
+ // Switch session via AgentSession (emits hook and tool session events)
1901
+ await this.session.switchSession(sessionPath);
1902
+
1903
+ // Clear and re-render the chat
1904
+ this.chatContainer.clear();
1905
+ this.renderInitialMessages();
1906
+ this.showStatus("Resumed session");
1907
+ }
1908
+
1909
+ private async showOAuthSelector(mode: "login" | "logout"): Promise<void> {
1910
+ if (mode === "logout") {
1911
+ const providers = this.session.modelRegistry.authStorage.list();
1912
+ const loggedInProviders = providers.filter(
1913
+ (p) => this.session.modelRegistry.authStorage.get(p)?.type === "oauth",
1914
+ );
1915
+ if (loggedInProviders.length === 0) {
1916
+ this.showStatus("No OAuth providers logged in. Use /login first.");
1917
+ return;
1918
+ }
1919
+ }
1920
+
1921
+ this.showSelector((done) => {
1922
+ const selector = new OAuthSelectorComponent(
1923
+ mode,
1924
+ this.session.modelRegistry.authStorage,
1925
+ async (providerId: string) => {
1926
+ done();
1927
+
1928
+ if (mode === "login") {
1929
+ this.showStatus(`Logging in to ${providerId}...`);
1930
+
1931
+ try {
1932
+ await this.session.modelRegistry.authStorage.login(providerId as OAuthProvider, {
1933
+ onAuth: (info: { url: string; instructions?: string }) => {
1934
+ this.chatContainer.addChild(new Spacer(1));
1935
+ // Use OSC 8 hyperlink escape sequence for clickable link
1936
+ const hyperlink = `\x1b]8;;${info.url}\x07Click here to login\x1b]8;;\x07`;
1937
+ this.chatContainer.addChild(new Text(theme.fg("accent", hyperlink), 1, 0));
1938
+ if (info.instructions) {
1939
+ this.chatContainer.addChild(new Spacer(1));
1940
+ this.chatContainer.addChild(new Text(theme.fg("warning", info.instructions), 1, 0));
1941
+ }
1942
+ this.ui.requestRender();
1943
+
1944
+ const openCmd =
1945
+ process.platform === "darwin"
1946
+ ? "open"
1947
+ : process.platform === "win32"
1948
+ ? "start"
1949
+ : "xdg-open";
1950
+ Bun.spawn([openCmd, info.url], { stdin: "ignore", stdout: "ignore", stderr: "ignore" });
1951
+ },
1952
+ onPrompt: async (prompt: { message: string; placeholder?: string }) => {
1953
+ this.chatContainer.addChild(new Spacer(1));
1954
+ this.chatContainer.addChild(new Text(theme.fg("warning", prompt.message), 1, 0));
1955
+ if (prompt.placeholder) {
1956
+ this.chatContainer.addChild(new Text(theme.fg("dim", prompt.placeholder), 1, 0));
1957
+ }
1958
+ this.ui.requestRender();
1959
+
1960
+ return new Promise<string>((resolve) => {
1961
+ const codeInput = new Input();
1962
+ codeInput.onSubmit = () => {
1963
+ const code = codeInput.getValue();
1964
+ this.editorContainer.clear();
1965
+ this.editorContainer.addChild(this.editor);
1966
+ this.ui.setFocus(this.editor);
1967
+ resolve(code);
1968
+ };
1969
+ this.editorContainer.clear();
1970
+ this.editorContainer.addChild(codeInput);
1971
+ this.ui.setFocus(codeInput);
1972
+ this.ui.requestRender();
1973
+ });
1974
+ },
1975
+ onProgress: (message: string) => {
1976
+ this.chatContainer.addChild(new Text(theme.fg("dim", message), 1, 0));
1977
+ this.ui.requestRender();
1978
+ },
1979
+ });
1980
+ // Refresh models to pick up new baseUrl (e.g., github-copilot)
1981
+ this.session.modelRegistry.refresh();
1982
+ this.chatContainer.addChild(new Spacer(1));
1983
+ this.chatContainer.addChild(
1984
+ new Text(theme.fg("success", `✓ Successfully logged in to ${providerId}`), 1, 0),
1985
+ );
1986
+ this.chatContainer.addChild(
1987
+ new Text(theme.fg("dim", `Credentials saved to ${getAuthPath()}`), 1, 0),
1988
+ );
1989
+ this.ui.requestRender();
1990
+ } catch (error: unknown) {
1991
+ this.showError(`Login failed: ${error instanceof Error ? error.message : String(error)}`);
1992
+ }
1993
+ } else {
1994
+ try {
1995
+ this.session.modelRegistry.authStorage.logout(providerId);
1996
+ // Refresh models to reset baseUrl
1997
+ this.session.modelRegistry.refresh();
1998
+ this.chatContainer.addChild(new Spacer(1));
1999
+ this.chatContainer.addChild(
2000
+ new Text(theme.fg("success", `✓ Successfully logged out of ${providerId}`), 1, 0),
2001
+ );
2002
+ this.chatContainer.addChild(
2003
+ new Text(theme.fg("dim", `Credentials removed from ${getAuthPath()}`), 1, 0),
2004
+ );
2005
+ this.ui.requestRender();
2006
+ } catch (error: unknown) {
2007
+ this.showError(`Logout failed: ${error instanceof Error ? error.message : String(error)}`);
2008
+ }
2009
+ }
2010
+ },
2011
+ () => {
2012
+ done();
2013
+ this.ui.requestRender();
2014
+ },
2015
+ );
2016
+ return { component: selector, focus: selector };
2017
+ });
2018
+ }
2019
+
2020
+ // =========================================================================
2021
+ // Command handlers
2022
+ // =========================================================================
2023
+
2024
+ private handleExportCommand(text: string): void {
2025
+ const parts = text.split(/\s+/);
2026
+ const outputPath = parts.length > 1 ? parts[1] : undefined;
2027
+
2028
+ try {
2029
+ const filePath = this.session.exportToHtml(outputPath);
2030
+ this.showStatus(`Session exported to: ${filePath}`);
2031
+ } catch (error: unknown) {
2032
+ this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
2033
+ }
2034
+ }
2035
+
2036
+ private async handleShareCommand(): Promise<void> {
2037
+ // Check if gh is available and logged in
2038
+ try {
2039
+ const authResult = Bun.spawnSync(["gh", "auth", "status"]);
2040
+ if (authResult.exitCode !== 0) {
2041
+ this.showError("GitHub CLI is not logged in. Run 'gh auth login' first.");
2042
+ return;
2043
+ }
2044
+ } catch {
2045
+ this.showError("GitHub CLI (gh) is not installed. Install it from https://cli.github.com/");
2046
+ return;
2047
+ }
2048
+
2049
+ // Export to a temp file
2050
+ const tmpFile = path.join(os.tmpdir(), "session.html");
2051
+ try {
2052
+ this.session.exportToHtml(tmpFile);
2053
+ } catch (error: unknown) {
2054
+ this.showError(`Failed to export session: ${error instanceof Error ? error.message : "Unknown error"}`);
2055
+ return;
2056
+ }
2057
+
2058
+ // Show cancellable loader, replacing the editor
2059
+ const loader = new BorderedLoader(this.ui, theme, "Creating gist...");
2060
+ this.editorContainer.clear();
2061
+ this.editorContainer.addChild(loader);
2062
+ this.ui.setFocus(loader);
2063
+ this.ui.requestRender();
2064
+
2065
+ const restoreEditor = () => {
2066
+ loader.dispose();
2067
+ this.editorContainer.clear();
2068
+ this.editorContainer.addChild(this.editor);
2069
+ this.ui.setFocus(this.editor);
2070
+ try {
2071
+ fs.unlinkSync(tmpFile);
2072
+ } catch {
2073
+ // Ignore cleanup errors
2074
+ }
2075
+ };
2076
+
2077
+ // Create a secret gist asynchronously
2078
+ let proc: ReturnType<typeof Bun.spawn> | null = null;
2079
+
2080
+ loader.onAbort = () => {
2081
+ proc?.kill();
2082
+ restoreEditor();
2083
+ this.showStatus("Share cancelled");
2084
+ };
2085
+
2086
+ try {
2087
+ const result = await new Promise<{ stdout: string; stderr: string; code: number | null }>((resolve) => {
2088
+ proc = Bun.spawn(["gh", "gist", "create", "--public=false", tmpFile], {
2089
+ stdout: "pipe",
2090
+ stderr: "pipe",
2091
+ });
2092
+ let stdout = "";
2093
+ let stderr = "";
2094
+
2095
+ const stdoutReader = (proc.stdout as ReadableStream<Uint8Array>).getReader();
2096
+ const stderrReader = (proc.stderr as ReadableStream<Uint8Array>).getReader();
2097
+ const decoder = new TextDecoder();
2098
+
2099
+ (async () => {
2100
+ try {
2101
+ while (true) {
2102
+ const { done, value } = await stdoutReader.read();
2103
+ if (done) break;
2104
+ stdout += decoder.decode(value);
2105
+ }
2106
+ } catch {}
2107
+ })();
2108
+
2109
+ (async () => {
2110
+ try {
2111
+ while (true) {
2112
+ const { done, value } = await stderrReader.read();
2113
+ if (done) break;
2114
+ stderr += decoder.decode(value);
2115
+ }
2116
+ } catch {}
2117
+ })();
2118
+
2119
+ proc.exited.then((code) => resolve({ stdout, stderr, code }));
2120
+ });
2121
+
2122
+ if (loader.signal.aborted) return;
2123
+
2124
+ restoreEditor();
2125
+
2126
+ if (result.code !== 0) {
2127
+ const errorMsg = result.stderr?.trim() || "Unknown error";
2128
+ this.showError(`Failed to create gist: ${errorMsg}`);
2129
+ return;
2130
+ }
2131
+
2132
+ // Extract gist ID from the URL returned by gh
2133
+ // gh returns something like: https://gist.github.com/username/GIST_ID
2134
+ const gistUrl = result.stdout?.trim();
2135
+ const gistId = gistUrl?.split("/").pop();
2136
+ if (!gistId) {
2137
+ this.showError("Failed to parse gist ID from gh output");
2138
+ return;
2139
+ }
2140
+
2141
+ // Create the preview URL
2142
+ const previewUrl = `https://shittycodingagent.ai/session?${gistId}`;
2143
+ this.showStatus(`Share URL: ${previewUrl}\nGist: ${gistUrl}`);
2144
+ } catch (error: unknown) {
2145
+ if (!loader.signal.aborted) {
2146
+ restoreEditor();
2147
+ this.showError(`Failed to create gist: ${error instanceof Error ? error.message : "Unknown error"}`);
2148
+ }
2149
+ }
2150
+ }
2151
+
2152
+ private async handleCopyCommand(): Promise<void> {
2153
+ const text = this.session.getLastAssistantText();
2154
+ if (!text) {
2155
+ this.showError("No agent messages to copy yet.");
2156
+ return;
2157
+ }
2158
+
2159
+ try {
2160
+ await copyToClipboard(text);
2161
+ this.showStatus("Copied last agent message to clipboard");
2162
+ } catch (error) {
2163
+ this.showError(error instanceof Error ? error.message : String(error));
2164
+ }
2165
+ }
2166
+
2167
+ private handleSessionCommand(): void {
2168
+ const stats = this.session.getSessionStats();
2169
+
2170
+ let info = `${theme.bold("Session Info")}\n\n`;
2171
+ info += `${theme.fg("dim", "File:")} ${stats.sessionFile}\n`;
2172
+ info += `${theme.fg("dim", "ID:")} ${stats.sessionId}\n\n`;
2173
+ info += `${theme.bold("Messages")}\n`;
2174
+ info += `${theme.fg("dim", "User:")} ${stats.userMessages}\n`;
2175
+ info += `${theme.fg("dim", "Assistant:")} ${stats.assistantMessages}\n`;
2176
+ info += `${theme.fg("dim", "Tool Calls:")} ${stats.toolCalls}\n`;
2177
+ info += `${theme.fg("dim", "Tool Results:")} ${stats.toolResults}\n`;
2178
+ info += `${theme.fg("dim", "Total:")} ${stats.totalMessages}\n\n`;
2179
+ info += `${theme.bold("Tokens")}\n`;
2180
+ info += `${theme.fg("dim", "Input:")} ${stats.tokens.input.toLocaleString()}\n`;
2181
+ info += `${theme.fg("dim", "Output:")} ${stats.tokens.output.toLocaleString()}\n`;
2182
+ if (stats.tokens.cacheRead > 0) {
2183
+ info += `${theme.fg("dim", "Cache Read:")} ${stats.tokens.cacheRead.toLocaleString()}\n`;
2184
+ }
2185
+ if (stats.tokens.cacheWrite > 0) {
2186
+ info += `${theme.fg("dim", "Cache Write:")} ${stats.tokens.cacheWrite.toLocaleString()}\n`;
2187
+ }
2188
+ info += `${theme.fg("dim", "Total:")} ${stats.tokens.total.toLocaleString()}\n`;
2189
+
2190
+ if (stats.cost > 0) {
2191
+ info += `\n${theme.bold("Cost")}\n`;
2192
+ info += `${theme.fg("dim", "Total:")} ${stats.cost.toFixed(4)}`;
2193
+ }
2194
+
2195
+ this.chatContainer.addChild(new Spacer(1));
2196
+ this.chatContainer.addChild(new Text(info, 1, 0));
2197
+ this.ui.requestRender();
2198
+ }
2199
+
2200
+ private handleChangelogCommand(): void {
2201
+ const changelogPath = getChangelogPath();
2202
+ const allEntries = parseChangelog(changelogPath);
2203
+
2204
+ const changelogMarkdown =
2205
+ allEntries.length > 0
2206
+ ? allEntries
2207
+ .reverse()
2208
+ .map((e) => e.content)
2209
+ .join("\n\n")
2210
+ : "No changelog entries found.";
2211
+
2212
+ this.chatContainer.addChild(new Spacer(1));
2213
+ this.chatContainer.addChild(new DynamicBorder());
2214
+ this.ui.addChild(new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0));
2215
+ this.ui.addChild(new Spacer(1));
2216
+ this.chatContainer.addChild(new Markdown(changelogMarkdown, 1, 1, getMarkdownTheme()));
2217
+ this.chatContainer.addChild(new DynamicBorder());
2218
+ this.ui.requestRender();
2219
+ }
2220
+
2221
+ private handleHotkeysCommand(): void {
2222
+ const hotkeys = `
2223
+ **Navigation**
2224
+ | Key | Action |
2225
+ |-----|--------|
2226
+ | \`Arrow keys\` | Move cursor / browse history (Up when empty) |
2227
+ | \`Option+Left/Right\` | Move by word |
2228
+ | \`Ctrl+A\` / \`Home\` / \`Cmd+Left\` | Start of line |
2229
+ | \`Ctrl+E\` / \`End\` / \`Cmd+Right\` | End of line |
2230
+
2231
+ **Editing**
2232
+ | Key | Action |
2233
+ |-----|--------|
2234
+ | \`Enter\` | Send message |
2235
+ | \`Shift+Enter\` / \`Alt+Enter\` | New line |
2236
+ | \`Ctrl+W\` / \`Option+Backspace\` | Delete word backwards |
2237
+ | \`Ctrl+U\` | Delete to start of line |
2238
+ | \`Ctrl+K\` | Delete to end of line |
2239
+
2240
+ **Other**
2241
+ | Key | Action |
2242
+ |-----|--------|
2243
+ | \`Tab\` | Path completion / accept autocomplete |
2244
+ | \`Escape\` | Cancel autocomplete / abort streaming |
2245
+ | \`Ctrl+C\` | Clear editor (first) / exit (second) |
2246
+ | \`Ctrl+D\` | Exit (when editor is empty) |
2247
+ | \`Ctrl+Z\` | Suspend to background |
2248
+ | \`Shift+Tab\` | Cycle thinking level |
2249
+ | \`Ctrl+P\` | Cycle models |
2250
+ | \`Ctrl+O\` | Toggle tool output expansion |
2251
+ | \`Ctrl+T\` | Toggle thinking block visibility |
2252
+ | \`Ctrl+G\` | Edit message in external editor |
2253
+ | \`/\` | Slash commands |
2254
+ | \`!\` | Run bash command |
2255
+ `;
2256
+ this.chatContainer.addChild(new Spacer(1));
2257
+ this.chatContainer.addChild(new DynamicBorder());
2258
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "Keyboard Shortcuts")), 1, 0));
2259
+ this.chatContainer.addChild(new Spacer(1));
2260
+ this.chatContainer.addChild(new Markdown(hotkeys.trim(), 1, 1, getMarkdownTheme()));
2261
+ this.chatContainer.addChild(new DynamicBorder());
2262
+ this.ui.requestRender();
2263
+ }
2264
+
2265
+ private handleStatusCommand(): void {
2266
+ const sections: string[] = [];
2267
+
2268
+ // Loaded context files
2269
+ const contextFiles = loadProjectContextFiles();
2270
+ if (contextFiles.length > 0) {
2271
+ sections.push(
2272
+ theme.bold(theme.fg("accent", "Context Files")) +
2273
+ "\n" +
2274
+ contextFiles.map((f) => theme.fg("dim", ` ${f.path}`)).join("\n"),
2275
+ );
2276
+ }
2277
+
2278
+ // Loaded skills
2279
+ const skillsSettings = this.session.skillsSettings;
2280
+ if (skillsSettings?.enabled !== false) {
2281
+ const { skills, warnings: skillWarnings } = loadSkills(skillsSettings ?? {});
2282
+ if (skills.length > 0) {
2283
+ sections.push(
2284
+ theme.bold(theme.fg("accent", "Skills")) +
2285
+ "\n" +
2286
+ skills.map((s) => theme.fg("dim", ` ${s.filePath}`)).join("\n"),
2287
+ );
2288
+ }
2289
+ if (skillWarnings.length > 0) {
2290
+ sections.push(
2291
+ theme.bold(theme.fg("warning", "Skill Warnings")) +
2292
+ "\n" +
2293
+ skillWarnings.map((w) => theme.fg("warning", ` ${w.skillPath}: ${w.message}`)).join("\n"),
2294
+ );
2295
+ }
2296
+ }
2297
+
2298
+ // Loaded custom tools
2299
+ if (this.customTools.size > 0) {
2300
+ sections.push(
2301
+ theme.bold(theme.fg("accent", "Custom Tools")) +
2302
+ "\n" +
2303
+ Array.from(this.customTools.values())
2304
+ .map((ct) => theme.fg("dim", ` ${ct.tool.name} (${ct.path})`))
2305
+ .join("\n"),
2306
+ );
2307
+ }
2308
+
2309
+ // Loaded hooks
2310
+ const hookRunner = this.session.hookRunner;
2311
+ if (hookRunner) {
2312
+ const hookPaths = hookRunner.getHookPaths();
2313
+ if (hookPaths.length > 0) {
2314
+ sections.push(
2315
+ `${theme.bold(theme.fg("accent", "Hooks"))}\n${hookPaths.map((p) => theme.fg("dim", ` ${p}`)).join("\n")}`,
2316
+ );
2317
+ }
2318
+ }
2319
+
2320
+ if (sections.length === 0) {
2321
+ this.chatContainer.addChild(new Spacer(1));
2322
+ this.chatContainer.addChild(new Text(theme.fg("muted", "No extensions loaded."), 1, 0));
2323
+ } else {
2324
+ this.chatContainer.addChild(new Spacer(1));
2325
+ this.chatContainer.addChild(new DynamicBorder());
2326
+ this.chatContainer.addChild(new Text(theme.bold(theme.fg("accent", "Loaded Extensions")), 1, 0));
2327
+ this.chatContainer.addChild(new Spacer(1));
2328
+ for (const section of sections) {
2329
+ this.chatContainer.addChild(new Text(section, 1, 0));
2330
+ this.chatContainer.addChild(new Spacer(1));
2331
+ }
2332
+ this.chatContainer.addChild(new DynamicBorder());
2333
+ }
2334
+ this.ui.requestRender();
2335
+ }
2336
+
2337
+ private async handleClearCommand(): Promise<void> {
2338
+ // Stop loading animation
2339
+ if (this.loadingAnimation) {
2340
+ this.loadingAnimation.stop();
2341
+ this.loadingAnimation = undefined;
2342
+ }
2343
+ this.statusContainer.clear();
2344
+
2345
+ // New session via session (emits hook and tool session events)
2346
+ await this.session.newSession();
2347
+
2348
+ // Clear UI state
2349
+ this.chatContainer.clear();
2350
+ this.pendingMessagesContainer.clear();
2351
+ this.streamingComponent = undefined;
2352
+ this.streamingMessage = undefined;
2353
+ this.pendingTools.clear();
2354
+
2355
+ this.chatContainer.addChild(new Spacer(1));
2356
+ this.chatContainer.addChild(new Text(`${theme.fg("accent", "✓ New session started")}`, 1, 1));
2357
+ this.ui.requestRender();
2358
+ }
2359
+
2360
+ private handleDebugCommand(): void {
2361
+ const width = this.ui.terminal.columns;
2362
+ const allLines = this.ui.render(width);
2363
+
2364
+ const debugLogPath = getDebugLogPath();
2365
+ const debugData = [
2366
+ `Debug output at ${new Date().toISOString()}`,
2367
+ `Terminal width: ${width}`,
2368
+ `Total lines: ${allLines.length}`,
2369
+ "",
2370
+ "=== All rendered lines with visible widths ===",
2371
+ ...allLines.map((line, idx) => {
2372
+ const vw = visibleWidth(line);
2373
+ const escaped = JSON.stringify(line);
2374
+ return `[${idx}] (w=${vw}) ${escaped}`;
2375
+ }),
2376
+ "",
2377
+ "=== Agent messages (JSONL) ===",
2378
+ ...this.session.messages.map((msg) => JSON.stringify(msg)),
2379
+ "",
2380
+ ].join("\n");
2381
+
2382
+ fs.mkdirSync(path.dirname(debugLogPath), { recursive: true });
2383
+ fs.writeFileSync(debugLogPath, debugData);
2384
+
2385
+ this.chatContainer.addChild(new Spacer(1));
2386
+ this.chatContainer.addChild(
2387
+ new Text(`${theme.fg("accent", "✓ Debug log written")}\n${theme.fg("muted", debugLogPath)}`, 1, 1),
2388
+ );
2389
+ this.ui.requestRender();
2390
+ }
2391
+
2392
+ private handleArminSaysHi(): void {
2393
+ this.chatContainer.addChild(new Spacer(1));
2394
+ this.chatContainer.addChild(new ArminComponent(this.ui));
2395
+ this.ui.requestRender();
2396
+ }
2397
+
2398
+ private async handleBashCommand(command: string): Promise<void> {
2399
+ const isDeferred = this.session.isStreaming;
2400
+ this.bashComponent = new BashExecutionComponent(command, this.ui);
2401
+
2402
+ if (isDeferred) {
2403
+ // Show in pending area when agent is streaming
2404
+ this.pendingMessagesContainer.addChild(this.bashComponent);
2405
+ this.pendingBashComponents.push(this.bashComponent);
2406
+ } else {
2407
+ // Show in chat immediately when agent is idle
2408
+ this.chatContainer.addChild(this.bashComponent);
2409
+ }
2410
+ this.ui.requestRender();
2411
+
2412
+ try {
2413
+ const result = await this.session.executeBash(command, (chunk) => {
2414
+ if (this.bashComponent) {
2415
+ this.bashComponent.appendOutput(chunk);
2416
+ this.ui.requestRender();
2417
+ }
2418
+ });
2419
+
2420
+ if (this.bashComponent) {
2421
+ this.bashComponent.setComplete(
2422
+ result.exitCode,
2423
+ result.cancelled,
2424
+ result.truncated ? ({ truncated: true, content: result.output } as TruncationResult) : undefined,
2425
+ result.fullOutputPath,
2426
+ );
2427
+ }
2428
+ } catch (error) {
2429
+ if (this.bashComponent) {
2430
+ this.bashComponent.setComplete(undefined, false);
2431
+ }
2432
+ this.showError(`Bash command failed: ${error instanceof Error ? error.message : "Unknown error"}`);
2433
+ }
2434
+
2435
+ this.bashComponent = undefined;
2436
+ this.ui.requestRender();
2437
+ }
2438
+
2439
+ private async handleCompactCommand(customInstructions?: string): Promise<void> {
2440
+ const entries = this.sessionManager.getEntries();
2441
+ const messageCount = entries.filter((e) => e.type === "message").length;
2442
+
2443
+ if (messageCount < 2) {
2444
+ this.showWarning("Nothing to compact (no messages yet)");
2445
+ return;
2446
+ }
2447
+
2448
+ await this.executeCompaction(customInstructions, false);
2449
+ }
2450
+
2451
+ private async executeCompaction(customInstructions?: string, isAuto = false): Promise<void> {
2452
+ // Stop loading animation
2453
+ if (this.loadingAnimation) {
2454
+ this.loadingAnimation.stop();
2455
+ this.loadingAnimation = undefined;
2456
+ }
2457
+ this.statusContainer.clear();
2458
+
2459
+ // Set up escape handler during compaction
2460
+ const originalOnEscape = this.editor.onEscape;
2461
+ this.editor.onEscape = () => {
2462
+ this.session.abortCompaction();
2463
+ };
2464
+
2465
+ // Show compacting status
2466
+ this.chatContainer.addChild(new Spacer(1));
2467
+ const label = isAuto ? "Auto-compacting context... (esc to cancel)" : "Compacting context... (esc to cancel)";
2468
+ const compactingLoader = new Loader(
2469
+ this.ui,
2470
+ (spinner) => theme.fg("accent", spinner),
2471
+ (text) => theme.fg("muted", text),
2472
+ label,
2473
+ );
2474
+ this.statusContainer.addChild(compactingLoader);
2475
+ this.ui.requestRender();
2476
+
2477
+ try {
2478
+ const result = await this.session.compact(customInstructions);
2479
+
2480
+ // Rebuild UI
2481
+ this.rebuildChatFromMessages();
2482
+
2483
+ // Add compaction component at bottom so user sees it without scrolling
2484
+ const msg = createCompactionSummaryMessage(result.summary, result.tokensBefore, new Date().toISOString());
2485
+ this.addMessageToChat(msg);
2486
+
2487
+ this.footer.invalidate();
2488
+ } catch (error) {
2489
+ const message = error instanceof Error ? error.message : String(error);
2490
+ if (message === "Compaction cancelled" || (error instanceof Error && error.name === "AbortError")) {
2491
+ this.showError("Compaction cancelled");
2492
+ } else {
2493
+ this.showError(`Compaction failed: ${message}`);
2494
+ }
2495
+ } finally {
2496
+ compactingLoader.stop();
2497
+ this.statusContainer.clear();
2498
+ this.editor.onEscape = originalOnEscape;
2499
+ }
2500
+ }
2501
+
2502
+ stop(): void {
2503
+ if (this.loadingAnimation) {
2504
+ this.loadingAnimation.stop();
2505
+ this.loadingAnimation = undefined;
2506
+ }
2507
+ this.footer.dispose();
2508
+ if (this.unsubscribe) {
2509
+ this.unsubscribe();
2510
+ }
2511
+ if (this.isInitialized) {
2512
+ this.ui.stop();
2513
+ this.isInitialized = false;
2514
+ }
2515
+ }
2516
+ }