indusagi-coding-agent 0.1.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 (240) hide show
  1. package/CHANGELOG.md +2249 -0
  2. package/README.md +546 -0
  3. package/dist/cli/args.js +282 -0
  4. package/dist/cli/config-selector.js +30 -0
  5. package/dist/cli/file-processor.js +78 -0
  6. package/dist/cli/list-models.js +91 -0
  7. package/dist/cli/session-picker.js +31 -0
  8. package/dist/cli.js +10 -0
  9. package/dist/config.js +158 -0
  10. package/dist/core/agent-session.js +2097 -0
  11. package/dist/core/auth-storage.js +278 -0
  12. package/dist/core/bash-executor.js +211 -0
  13. package/dist/core/compaction/branch-summarization.js +241 -0
  14. package/dist/core/compaction/compaction.js +606 -0
  15. package/dist/core/compaction/index.js +6 -0
  16. package/dist/core/compaction/utils.js +137 -0
  17. package/dist/core/diagnostics.js +1 -0
  18. package/dist/core/event-bus.js +24 -0
  19. package/dist/core/exec.js +70 -0
  20. package/dist/core/export-html/ansi-to-html.js +248 -0
  21. package/dist/core/export-html/index.js +221 -0
  22. package/dist/core/export-html/template.css +905 -0
  23. package/dist/core/export-html/template.html +54 -0
  24. package/dist/core/export-html/template.js +1549 -0
  25. package/dist/core/export-html/tool-renderer.js +56 -0
  26. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  27. package/dist/core/export-html/vendor/marked.min.js +6 -0
  28. package/dist/core/extensions/index.js +8 -0
  29. package/dist/core/extensions/loader.js +395 -0
  30. package/dist/core/extensions/runner.js +499 -0
  31. package/dist/core/extensions/types.js +31 -0
  32. package/dist/core/extensions/wrapper.js +101 -0
  33. package/dist/core/footer-data-provider.js +133 -0
  34. package/dist/core/index.js +8 -0
  35. package/dist/core/keybindings.js +140 -0
  36. package/dist/core/messages.js +122 -0
  37. package/dist/core/model-registry.js +454 -0
  38. package/dist/core/model-resolver.js +309 -0
  39. package/dist/core/package-manager.js +1142 -0
  40. package/dist/core/prompt-templates.js +250 -0
  41. package/dist/core/resource-loader.js +569 -0
  42. package/dist/core/sdk.js +225 -0
  43. package/dist/core/session-manager.js +1078 -0
  44. package/dist/core/settings-manager.js +430 -0
  45. package/dist/core/skills.js +339 -0
  46. package/dist/core/system-prompt.js +136 -0
  47. package/dist/core/timings.js +24 -0
  48. package/dist/core/tools/bash.js +226 -0
  49. package/dist/core/tools/edit-diff.js +242 -0
  50. package/dist/core/tools/edit.js +145 -0
  51. package/dist/core/tools/find.js +205 -0
  52. package/dist/core/tools/grep.js +238 -0
  53. package/dist/core/tools/index.js +60 -0
  54. package/dist/core/tools/ls.js +117 -0
  55. package/dist/core/tools/path-utils.js +52 -0
  56. package/dist/core/tools/read.js +165 -0
  57. package/dist/core/tools/truncate.js +204 -0
  58. package/dist/core/tools/write.js +77 -0
  59. package/dist/index.js +41 -0
  60. package/dist/main.js +565 -0
  61. package/dist/migrations.js +260 -0
  62. package/dist/modes/index.js +7 -0
  63. package/dist/modes/interactive/components/armin.js +328 -0
  64. package/dist/modes/interactive/components/assistant-message.js +86 -0
  65. package/dist/modes/interactive/components/bash-execution.js +155 -0
  66. package/dist/modes/interactive/components/bordered-loader.js +47 -0
  67. package/dist/modes/interactive/components/branch-summary-message.js +41 -0
  68. package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
  69. package/dist/modes/interactive/components/config-selector.js +458 -0
  70. package/dist/modes/interactive/components/countdown-timer.js +27 -0
  71. package/dist/modes/interactive/components/custom-editor.js +61 -0
  72. package/dist/modes/interactive/components/custom-message.js +80 -0
  73. package/dist/modes/interactive/components/diff.js +132 -0
  74. package/dist/modes/interactive/components/dynamic-border.js +19 -0
  75. package/dist/modes/interactive/components/extension-editor.js +96 -0
  76. package/dist/modes/interactive/components/extension-input.js +54 -0
  77. package/dist/modes/interactive/components/extension-selector.js +70 -0
  78. package/dist/modes/interactive/components/footer.js +213 -0
  79. package/dist/modes/interactive/components/index.js +31 -0
  80. package/dist/modes/interactive/components/keybinding-hints.js +60 -0
  81. package/dist/modes/interactive/components/login-dialog.js +138 -0
  82. package/dist/modes/interactive/components/model-selector.js +253 -0
  83. package/dist/modes/interactive/components/oauth-selector.js +91 -0
  84. package/dist/modes/interactive/components/scoped-models-selector.js +262 -0
  85. package/dist/modes/interactive/components/session-selector-search.js +145 -0
  86. package/dist/modes/interactive/components/session-selector.js +698 -0
  87. package/dist/modes/interactive/components/settings-selector.js +250 -0
  88. package/dist/modes/interactive/components/show-images-selector.js +33 -0
  89. package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
  90. package/dist/modes/interactive/components/theme-selector.js +43 -0
  91. package/dist/modes/interactive/components/thinking-selector.js +45 -0
  92. package/dist/modes/interactive/components/tool-execution.js +608 -0
  93. package/dist/modes/interactive/components/tree-selector.js +892 -0
  94. package/dist/modes/interactive/components/user-message-selector.js +109 -0
  95. package/dist/modes/interactive/components/user-message.js +15 -0
  96. package/dist/modes/interactive/components/visual-truncate.js +32 -0
  97. package/dist/modes/interactive/interactive-mode.js +3576 -0
  98. package/dist/modes/interactive/theme/dark.json +85 -0
  99. package/dist/modes/interactive/theme/light.json +84 -0
  100. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  101. package/dist/modes/interactive/theme/theme.js +938 -0
  102. package/dist/modes/print-mode.js +96 -0
  103. package/dist/modes/rpc/rpc-client.js +390 -0
  104. package/dist/modes/rpc/rpc-mode.js +448 -0
  105. package/dist/modes/rpc/rpc-types.js +7 -0
  106. package/dist/utils/changelog.js +86 -0
  107. package/dist/utils/clipboard-image.js +116 -0
  108. package/dist/utils/clipboard.js +58 -0
  109. package/dist/utils/frontmatter.js +25 -0
  110. package/dist/utils/git.js +5 -0
  111. package/dist/utils/image-convert.js +34 -0
  112. package/dist/utils/image-resize.js +180 -0
  113. package/dist/utils/mime.js +25 -0
  114. package/dist/utils/photon.js +120 -0
  115. package/dist/utils/shell.js +164 -0
  116. package/dist/utils/sleep.js +16 -0
  117. package/dist/utils/tools-manager.js +186 -0
  118. package/docs/compaction.md +390 -0
  119. package/docs/custom-provider.md +538 -0
  120. package/docs/development.md +69 -0
  121. package/docs/extensions.md +1733 -0
  122. package/docs/images/doom-extension.png +0 -0
  123. package/docs/images/interactive-mode.png +0 -0
  124. package/docs/images/tree-view.png +0 -0
  125. package/docs/json.md +79 -0
  126. package/docs/keybindings.md +162 -0
  127. package/docs/models.md +193 -0
  128. package/docs/packages.md +163 -0
  129. package/docs/prompt-templates.md +67 -0
  130. package/docs/providers.md +147 -0
  131. package/docs/rpc.md +1048 -0
  132. package/docs/sdk.md +957 -0
  133. package/docs/session.md +412 -0
  134. package/docs/settings.md +216 -0
  135. package/docs/shell-aliases.md +13 -0
  136. package/docs/skills.md +226 -0
  137. package/docs/terminal-setup.md +65 -0
  138. package/docs/themes.md +295 -0
  139. package/docs/tree.md +219 -0
  140. package/docs/tui.md +887 -0
  141. package/docs/windows.md +17 -0
  142. package/examples/README.md +25 -0
  143. package/examples/extensions/README.md +192 -0
  144. package/examples/extensions/antigravity-image-gen.ts +414 -0
  145. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  146. package/examples/extensions/bookmark.ts +50 -0
  147. package/examples/extensions/claude-rules.ts +86 -0
  148. package/examples/extensions/confirm-destructive.ts +59 -0
  149. package/examples/extensions/custom-compaction.ts +115 -0
  150. package/examples/extensions/custom-footer.ts +65 -0
  151. package/examples/extensions/custom-header.ts +73 -0
  152. package/examples/extensions/custom-provider-anthropic/index.ts +605 -0
  153. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  154. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  155. package/examples/extensions/custom-provider-gitlab-duo/index.ts +350 -0
  156. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  157. package/examples/extensions/custom-provider-gitlab-duo/test.ts +83 -0
  158. package/examples/extensions/dirty-repo-guard.ts +56 -0
  159. package/examples/extensions/doom-overlay/README.md +46 -0
  160. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  161. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  162. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  163. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  164. package/examples/extensions/doom-overlay/doom-component.ts +133 -0
  165. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  166. package/examples/extensions/doom-overlay/doom-keys.ts +105 -0
  167. package/examples/extensions/doom-overlay/index.ts +74 -0
  168. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  169. package/examples/extensions/event-bus.ts +43 -0
  170. package/examples/extensions/file-trigger.ts +41 -0
  171. package/examples/extensions/git-checkpoint.ts +53 -0
  172. package/examples/extensions/handoff.ts +151 -0
  173. package/examples/extensions/hello.ts +25 -0
  174. package/examples/extensions/inline-bash.ts +94 -0
  175. package/examples/extensions/input-transform.ts +43 -0
  176. package/examples/extensions/interactive-shell.ts +196 -0
  177. package/examples/extensions/mac-system-theme.ts +47 -0
  178. package/examples/extensions/message-renderer.ts +60 -0
  179. package/examples/extensions/modal-editor.ts +86 -0
  180. package/examples/extensions/model-status.ts +31 -0
  181. package/examples/extensions/notify.ts +25 -0
  182. package/examples/extensions/overlay-qa-tests.ts +882 -0
  183. package/examples/extensions/overlay-test.ts +151 -0
  184. package/examples/extensions/permission-gate.ts +34 -0
  185. package/examples/extensions/pirate.ts +47 -0
  186. package/examples/extensions/plan-mode/README.md +65 -0
  187. package/examples/extensions/plan-mode/index.ts +341 -0
  188. package/examples/extensions/plan-mode/utils.ts +168 -0
  189. package/examples/extensions/preset.ts +399 -0
  190. package/examples/extensions/protected-paths.ts +30 -0
  191. package/examples/extensions/qna.ts +120 -0
  192. package/examples/extensions/question.ts +265 -0
  193. package/examples/extensions/questionnaire.ts +428 -0
  194. package/examples/extensions/rainbow-editor.ts +88 -0
  195. package/examples/extensions/sandbox/index.ts +318 -0
  196. package/examples/extensions/sandbox/package-lock.json +92 -0
  197. package/examples/extensions/sandbox/package.json +19 -0
  198. package/examples/extensions/send-user-message.ts +97 -0
  199. package/examples/extensions/session-name.ts +27 -0
  200. package/examples/extensions/shutdown-command.ts +63 -0
  201. package/examples/extensions/snake.ts +344 -0
  202. package/examples/extensions/space-invaders.ts +561 -0
  203. package/examples/extensions/ssh.ts +220 -0
  204. package/examples/extensions/status-line.ts +40 -0
  205. package/examples/extensions/subagent/README.md +172 -0
  206. package/examples/extensions/subagent/agents/planner.md +37 -0
  207. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  208. package/examples/extensions/subagent/agents/scout.md +50 -0
  209. package/examples/extensions/subagent/agents/worker.md +24 -0
  210. package/examples/extensions/subagent/agents.ts +127 -0
  211. package/examples/extensions/subagent/index.ts +964 -0
  212. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  213. package/examples/extensions/subagent/prompts/implement.md +10 -0
  214. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  215. package/examples/extensions/summarize.ts +196 -0
  216. package/examples/extensions/timed-confirm.ts +70 -0
  217. package/examples/extensions/todo.ts +300 -0
  218. package/examples/extensions/tool-override.ts +144 -0
  219. package/examples/extensions/tools.ts +147 -0
  220. package/examples/extensions/trigger-compact.ts +40 -0
  221. package/examples/extensions/truncated-tool.ts +193 -0
  222. package/examples/extensions/widget-placement.ts +17 -0
  223. package/examples/extensions/with-deps/index.ts +36 -0
  224. package/examples/extensions/with-deps/package-lock.json +31 -0
  225. package/examples/extensions/with-deps/package.json +22 -0
  226. package/examples/sdk/01-minimal.ts +22 -0
  227. package/examples/sdk/02-custom-model.ts +50 -0
  228. package/examples/sdk/03-custom-prompt.ts +55 -0
  229. package/examples/sdk/04-skills.ts +46 -0
  230. package/examples/sdk/05-tools.ts +56 -0
  231. package/examples/sdk/06-extensions.ts +88 -0
  232. package/examples/sdk/07-context-files.ts +40 -0
  233. package/examples/sdk/08-prompt-templates.ts +47 -0
  234. package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
  235. package/examples/sdk/10-settings.ts +38 -0
  236. package/examples/sdk/11-sessions.ts +48 -0
  237. package/examples/sdk/12-full-control.ts +82 -0
  238. package/examples/sdk/13-codex-oauth.ts +37 -0
  239. package/examples/sdk/README.md +144 -0
  240. package/package.json +85 -0
@@ -0,0 +1,892 @@
1
+ import { Container, getEditorKeybindings, Input, matchesKey, Spacer, Text, TruncatedText, truncateToWidth, } from "indusagi/tui";
2
+ import { theme } from "../theme/theme.js";
3
+ import { DynamicBorder } from "./dynamic-border.js";
4
+ import { keyHint } from "./keybinding-hints.js";
5
+ class TreeList {
6
+ constructor(tree, currentLeafId, maxVisibleLines, initialSelectedId) {
7
+ this.flatNodes = [];
8
+ this.filteredNodes = [];
9
+ this.selectedIndex = 0;
10
+ this.filterMode = "default";
11
+ this.searchQuery = "";
12
+ this.toolCallMap = new Map();
13
+ this.multipleRoots = false;
14
+ this.activePathIds = new Set();
15
+ this.currentLeafId = currentLeafId;
16
+ this.maxVisibleLines = maxVisibleLines;
17
+ this.multipleRoots = tree.length > 1;
18
+ this.flatNodes = this.flattenTree(tree);
19
+ this.buildActivePath();
20
+ this.applyFilter();
21
+ // Start with initialSelectedId if provided, otherwise current leaf
22
+ const targetId = initialSelectedId ?? currentLeafId;
23
+ const targetIndex = this.filteredNodes.findIndex((n) => n.node.entry.id === targetId);
24
+ if (targetIndex !== -1) {
25
+ this.selectedIndex = targetIndex;
26
+ }
27
+ else {
28
+ this.selectedIndex = Math.max(0, this.filteredNodes.length - 1);
29
+ }
30
+ }
31
+ /** Build the set of entry IDs on the path from root to current leaf */
32
+ buildActivePath() {
33
+ this.activePathIds.clear();
34
+ if (!this.currentLeafId)
35
+ return;
36
+ // Build a map of id -> entry for parent lookup
37
+ const entryMap = new Map();
38
+ for (const flatNode of this.flatNodes) {
39
+ entryMap.set(flatNode.node.entry.id, flatNode);
40
+ }
41
+ // Walk from leaf to root
42
+ let currentId = this.currentLeafId;
43
+ while (currentId) {
44
+ this.activePathIds.add(currentId);
45
+ const node = entryMap.get(currentId);
46
+ if (!node)
47
+ break;
48
+ currentId = node.node.entry.parentId ?? null;
49
+ }
50
+ }
51
+ flattenTree(roots) {
52
+ const result = [];
53
+ this.toolCallMap.clear();
54
+ const stack = [];
55
+ // Determine which subtrees contain the active leaf (to sort current branch first)
56
+ // Use iterative post-order traversal to avoid stack overflow
57
+ const containsActive = new Map();
58
+ const leafId = this.currentLeafId;
59
+ {
60
+ // Build list in pre-order, then process in reverse for post-order effect
61
+ const allNodes = [];
62
+ const preOrderStack = [...roots];
63
+ while (preOrderStack.length > 0) {
64
+ const node = preOrderStack.pop();
65
+ allNodes.push(node);
66
+ // Push children in reverse so they're processed left-to-right
67
+ for (let i = node.children.length - 1; i >= 0; i--) {
68
+ preOrderStack.push(node.children[i]);
69
+ }
70
+ }
71
+ // Process in reverse (post-order): children before parents
72
+ for (let i = allNodes.length - 1; i >= 0; i--) {
73
+ const node = allNodes[i];
74
+ let has = leafId !== null && node.entry.id === leafId;
75
+ for (const child of node.children) {
76
+ if (containsActive.get(child)) {
77
+ has = true;
78
+ }
79
+ }
80
+ containsActive.set(node, has);
81
+ }
82
+ }
83
+ // Add roots in reverse order, prioritizing the one containing the active leaf
84
+ // If multiple roots, treat them as children of a virtual root that branches
85
+ const multipleRoots = roots.length > 1;
86
+ const orderedRoots = [...roots].sort((a, b) => Number(containsActive.get(b)) - Number(containsActive.get(a)));
87
+ for (let i = orderedRoots.length - 1; i >= 0; i--) {
88
+ const isLast = i === orderedRoots.length - 1;
89
+ stack.push([orderedRoots[i], multipleRoots ? 1 : 0, multipleRoots, multipleRoots, isLast, [], multipleRoots]);
90
+ }
91
+ while (stack.length > 0) {
92
+ const [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop();
93
+ // Extract tool calls from assistant messages for later lookup
94
+ const entry = node.entry;
95
+ if (entry.type === "message" && entry.message.role === "assistant") {
96
+ const content = entry.message.content;
97
+ if (Array.isArray(content)) {
98
+ for (const block of content) {
99
+ if (typeof block === "object" && block !== null && "type" in block && block.type === "toolCall") {
100
+ const tc = block;
101
+ this.toolCallMap.set(tc.id, { name: tc.name, arguments: tc.arguments });
102
+ }
103
+ }
104
+ }
105
+ }
106
+ result.push({ node, indent, showConnector, isLast, gutters, isVirtualRootChild });
107
+ const children = node.children;
108
+ const multipleChildren = children.length > 1;
109
+ // Order children so the branch containing the active leaf comes first
110
+ const orderedChildren = (() => {
111
+ const prioritized = [];
112
+ const rest = [];
113
+ for (const child of children) {
114
+ if (containsActive.get(child)) {
115
+ prioritized.push(child);
116
+ }
117
+ else {
118
+ rest.push(child);
119
+ }
120
+ }
121
+ return [...prioritized, ...rest];
122
+ })();
123
+ // Calculate child indent
124
+ let childIndent;
125
+ if (multipleChildren) {
126
+ // Parent branches: children get +1
127
+ childIndent = indent + 1;
128
+ }
129
+ else if (justBranched && indent > 0) {
130
+ // First generation after a branch: +1 for visual grouping
131
+ childIndent = indent + 1;
132
+ }
133
+ else {
134
+ // Single-child chain: stay flat
135
+ childIndent = indent;
136
+ }
137
+ // Build gutters for children
138
+ // If this node showed a connector, add a gutter entry for descendants
139
+ // Only add gutter if connector is actually displayed (not suppressed for virtual root children)
140
+ const connectorDisplayed = showConnector && !isVirtualRootChild;
141
+ // When connector is displayed, add a gutter entry at the connector's position
142
+ // Connector is at position (displayIndent - 1), so gutter should be there too
143
+ const currentDisplayIndent = this.multipleRoots ? Math.max(0, indent - 1) : indent;
144
+ const connectorPosition = Math.max(0, currentDisplayIndent - 1);
145
+ const childGutters = connectorDisplayed
146
+ ? [...gutters, { position: connectorPosition, show: !isLast }]
147
+ : gutters;
148
+ // Add children in reverse order
149
+ for (let i = orderedChildren.length - 1; i >= 0; i--) {
150
+ const childIsLast = i === orderedChildren.length - 1;
151
+ stack.push([
152
+ orderedChildren[i],
153
+ childIndent,
154
+ multipleChildren,
155
+ multipleChildren,
156
+ childIsLast,
157
+ childGutters,
158
+ false,
159
+ ]);
160
+ }
161
+ }
162
+ return result;
163
+ }
164
+ applyFilter() {
165
+ // Remember currently selected node to preserve cursor position
166
+ const previouslySelectedId = this.filteredNodes[this.selectedIndex]?.node.entry.id;
167
+ const searchTokens = this.searchQuery.toLowerCase().split(/\s+/).filter(Boolean);
168
+ this.filteredNodes = this.flatNodes.filter((flatNode) => {
169
+ const entry = flatNode.node.entry;
170
+ const isCurrentLeaf = entry.id === this.currentLeafId;
171
+ // Skip assistant messages with only tool calls (no text) unless error/aborted
172
+ // Always show current leaf so active position is visible
173
+ if (entry.type === "message" && entry.message.role === "assistant" && !isCurrentLeaf) {
174
+ const msg = entry.message;
175
+ const hasText = this.hasTextContent(msg.content);
176
+ const isErrorOrAborted = msg.stopReason && msg.stopReason !== "stop" && msg.stopReason !== "toolUse";
177
+ // Only hide if no text AND not an error/aborted message
178
+ if (!hasText && !isErrorOrAborted) {
179
+ return false;
180
+ }
181
+ }
182
+ // Apply filter mode
183
+ let passesFilter = true;
184
+ // Entry types hidden in default view (settings/bookkeeping)
185
+ const isSettingsEntry = entry.type === "label" ||
186
+ entry.type === "custom" ||
187
+ entry.type === "model_change" ||
188
+ entry.type === "thinking_level_change";
189
+ switch (this.filterMode) {
190
+ case "user-only":
191
+ // Just user messages
192
+ passesFilter = entry.type === "message" && entry.message.role === "user";
193
+ break;
194
+ case "no-tools":
195
+ // Default minus tool results
196
+ passesFilter = !isSettingsEntry && !(entry.type === "message" && entry.message.role === "toolResult");
197
+ break;
198
+ case "labeled-only":
199
+ // Just labeled entries
200
+ passesFilter = flatNode.node.label !== undefined;
201
+ break;
202
+ case "all":
203
+ // Show everything
204
+ passesFilter = true;
205
+ break;
206
+ default:
207
+ // Default mode: hide settings/bookkeeping entries
208
+ passesFilter = !isSettingsEntry;
209
+ break;
210
+ }
211
+ if (!passesFilter)
212
+ return false;
213
+ // Apply search filter
214
+ if (searchTokens.length > 0) {
215
+ const nodeText = this.getSearchableText(flatNode.node).toLowerCase();
216
+ return searchTokens.every((token) => nodeText.includes(token));
217
+ }
218
+ return true;
219
+ });
220
+ // Recalculate visual structure (indent, connectors, gutters) based on visible tree
221
+ this.recalculateVisualStructure();
222
+ // Try to preserve cursor on the same node after filtering
223
+ if (previouslySelectedId) {
224
+ const newIndex = this.filteredNodes.findIndex((n) => n.node.entry.id === previouslySelectedId);
225
+ if (newIndex !== -1) {
226
+ this.selectedIndex = newIndex;
227
+ return;
228
+ }
229
+ }
230
+ // Fall back: clamp index if out of bounds
231
+ if (this.selectedIndex >= this.filteredNodes.length) {
232
+ this.selectedIndex = Math.max(0, this.filteredNodes.length - 1);
233
+ }
234
+ }
235
+ /**
236
+ * Recompute indentation/connectors for the filtered view
237
+ *
238
+ * Filtering can hide intermediate entries; descendants attach to the nearest visible ancestor.
239
+ * Keep indentation semantics aligned with flattenTree() so single-child chains don't drift right.
240
+ */
241
+ recalculateVisualStructure() {
242
+ if (this.filteredNodes.length === 0)
243
+ return;
244
+ const visibleIds = new Set(this.filteredNodes.map((n) => n.node.entry.id));
245
+ // Build entry map for efficient parent lookup (using full tree)
246
+ const entryMap = new Map();
247
+ for (const flatNode of this.flatNodes) {
248
+ entryMap.set(flatNode.node.entry.id, flatNode);
249
+ }
250
+ // Find nearest visible ancestor for a node
251
+ const findVisibleAncestor = (nodeId) => {
252
+ let currentId = entryMap.get(nodeId)?.node.entry.parentId ?? null;
253
+ while (currentId !== null) {
254
+ if (visibleIds.has(currentId)) {
255
+ return currentId;
256
+ }
257
+ currentId = entryMap.get(currentId)?.node.entry.parentId ?? null;
258
+ }
259
+ return null;
260
+ };
261
+ // Build visible tree structure:
262
+ // - visibleParent: nodeId → nearest visible ancestor (or null for roots)
263
+ // - visibleChildren: parentId → list of visible children (in filteredNodes order)
264
+ const visibleParent = new Map();
265
+ const visibleChildren = new Map();
266
+ visibleChildren.set(null, []); // root-level nodes
267
+ for (const flatNode of this.filteredNodes) {
268
+ const nodeId = flatNode.node.entry.id;
269
+ const ancestorId = findVisibleAncestor(nodeId);
270
+ visibleParent.set(nodeId, ancestorId);
271
+ if (!visibleChildren.has(ancestorId)) {
272
+ visibleChildren.set(ancestorId, []);
273
+ }
274
+ visibleChildren.get(ancestorId).push(nodeId);
275
+ }
276
+ // Update multipleRoots based on visible roots
277
+ const visibleRootIds = visibleChildren.get(null);
278
+ this.multipleRoots = visibleRootIds.length > 1;
279
+ // Build a map for quick lookup: nodeId → FlatNode
280
+ const filteredNodeMap = new Map();
281
+ for (const flatNode of this.filteredNodes) {
282
+ filteredNodeMap.set(flatNode.node.entry.id, flatNode);
283
+ }
284
+ const stack = [];
285
+ // Add visible roots in reverse order (to process in forward order via stack)
286
+ for (let i = visibleRootIds.length - 1; i >= 0; i--) {
287
+ const isLast = i === visibleRootIds.length - 1;
288
+ stack.push([
289
+ visibleRootIds[i],
290
+ this.multipleRoots ? 1 : 0,
291
+ this.multipleRoots,
292
+ this.multipleRoots,
293
+ isLast,
294
+ [],
295
+ this.multipleRoots,
296
+ ]);
297
+ }
298
+ while (stack.length > 0) {
299
+ const [nodeId, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop();
300
+ const flatNode = filteredNodeMap.get(nodeId);
301
+ if (!flatNode)
302
+ continue;
303
+ // Update this node's visual properties
304
+ flatNode.indent = indent;
305
+ flatNode.showConnector = showConnector;
306
+ flatNode.isLast = isLast;
307
+ flatNode.gutters = gutters;
308
+ flatNode.isVirtualRootChild = isVirtualRootChild;
309
+ // Get visible children of this node
310
+ const children = visibleChildren.get(nodeId) || [];
311
+ const multipleChildren = children.length > 1;
312
+ // Child indent follows flattenTree(): branch points (and first generation after a branch) shift +1
313
+ let childIndent;
314
+ if (multipleChildren) {
315
+ childIndent = indent + 1;
316
+ }
317
+ else if (justBranched && indent > 0) {
318
+ childIndent = indent + 1;
319
+ }
320
+ else {
321
+ childIndent = indent;
322
+ }
323
+ // Child gutters follow flattenTree() connector/gutter rules
324
+ const connectorDisplayed = showConnector && !isVirtualRootChild;
325
+ const currentDisplayIndent = this.multipleRoots ? Math.max(0, indent - 1) : indent;
326
+ const connectorPosition = Math.max(0, currentDisplayIndent - 1);
327
+ const childGutters = connectorDisplayed
328
+ ? [...gutters, { position: connectorPosition, show: !isLast }]
329
+ : gutters;
330
+ // Add children in reverse order (to process in forward order via stack)
331
+ for (let i = children.length - 1; i >= 0; i--) {
332
+ const childIsLast = i === children.length - 1;
333
+ stack.push([
334
+ children[i],
335
+ childIndent,
336
+ multipleChildren,
337
+ multipleChildren,
338
+ childIsLast,
339
+ childGutters,
340
+ false,
341
+ ]);
342
+ }
343
+ }
344
+ }
345
+ /** Get searchable text content from a node */
346
+ getSearchableText(node) {
347
+ const entry = node.entry;
348
+ const parts = [];
349
+ if (node.label) {
350
+ parts.push(node.label);
351
+ }
352
+ switch (entry.type) {
353
+ case "message": {
354
+ const msg = entry.message;
355
+ parts.push(msg.role);
356
+ if ("content" in msg && msg.content) {
357
+ parts.push(this.extractContent(msg.content));
358
+ }
359
+ if (msg.role === "bashExecution") {
360
+ const bashMsg = msg;
361
+ if (bashMsg.command)
362
+ parts.push(bashMsg.command);
363
+ }
364
+ break;
365
+ }
366
+ case "custom_message": {
367
+ parts.push(entry.customType);
368
+ if (typeof entry.content === "string") {
369
+ parts.push(entry.content);
370
+ }
371
+ else {
372
+ parts.push(this.extractContent(entry.content));
373
+ }
374
+ break;
375
+ }
376
+ case "compaction":
377
+ parts.push("compaction");
378
+ break;
379
+ case "branch_summary":
380
+ parts.push("branch summary", entry.summary);
381
+ break;
382
+ case "model_change":
383
+ parts.push("model", entry.modelId);
384
+ break;
385
+ case "thinking_level_change":
386
+ parts.push("thinking", entry.thinkingLevel);
387
+ break;
388
+ case "custom":
389
+ parts.push("custom", entry.customType);
390
+ break;
391
+ case "label":
392
+ parts.push("label", entry.label ?? "");
393
+ break;
394
+ }
395
+ return parts.join(" ");
396
+ }
397
+ invalidate() { }
398
+ getSearchQuery() {
399
+ return this.searchQuery;
400
+ }
401
+ getSelectedNode() {
402
+ return this.filteredNodes[this.selectedIndex]?.node;
403
+ }
404
+ updateNodeLabel(entryId, label) {
405
+ for (const flatNode of this.flatNodes) {
406
+ if (flatNode.node.entry.id === entryId) {
407
+ flatNode.node.label = label;
408
+ break;
409
+ }
410
+ }
411
+ }
412
+ getFilterLabel() {
413
+ switch (this.filterMode) {
414
+ case "no-tools":
415
+ return " [no-tools]";
416
+ case "user-only":
417
+ return " [user]";
418
+ case "labeled-only":
419
+ return " [labeled]";
420
+ case "all":
421
+ return " [all]";
422
+ default:
423
+ return "";
424
+ }
425
+ }
426
+ render(width) {
427
+ const lines = [];
428
+ if (this.filteredNodes.length === 0) {
429
+ lines.push(truncateToWidth(theme.fg("muted", " No entries found"), width));
430
+ lines.push(truncateToWidth(theme.fg("muted", ` (0/0)${this.getFilterLabel()}`), width));
431
+ return lines;
432
+ }
433
+ const startIndex = Math.max(0, Math.min(this.selectedIndex - Math.floor(this.maxVisibleLines / 2), this.filteredNodes.length - this.maxVisibleLines));
434
+ const endIndex = Math.min(startIndex + this.maxVisibleLines, this.filteredNodes.length);
435
+ for (let i = startIndex; i < endIndex; i++) {
436
+ const flatNode = this.filteredNodes[i];
437
+ const entry = flatNode.node.entry;
438
+ const isSelected = i === this.selectedIndex;
439
+ // Build line: cursor + prefix + path marker + label + content
440
+ const cursor = isSelected ? theme.fg("accent", "› ") : " ";
441
+ // If multiple roots, shift display (roots at 0, not 1)
442
+ const displayIndent = this.multipleRoots ? Math.max(0, flatNode.indent - 1) : flatNode.indent;
443
+ // Build prefix with gutters at their correct positions
444
+ // Each gutter has a position (displayIndent where its connector was shown)
445
+ const connector = flatNode.showConnector && !flatNode.isVirtualRootChild ? (flatNode.isLast ? "└─ " : "├─ ") : "";
446
+ const connectorPosition = connector ? displayIndent - 1 : -1;
447
+ // Build prefix char by char, placing gutters and connector at their positions
448
+ const totalChars = displayIndent * 3;
449
+ const prefixChars = [];
450
+ for (let i = 0; i < totalChars; i++) {
451
+ const level = Math.floor(i / 3);
452
+ const posInLevel = i % 3;
453
+ // Check if there's a gutter at this level
454
+ const gutter = flatNode.gutters.find((g) => g.position === level);
455
+ if (gutter) {
456
+ if (posInLevel === 0) {
457
+ prefixChars.push(gutter.show ? "│" : " ");
458
+ }
459
+ else {
460
+ prefixChars.push(" ");
461
+ }
462
+ }
463
+ else if (connector && level === connectorPosition) {
464
+ // Connector at this level
465
+ if (posInLevel === 0) {
466
+ prefixChars.push(flatNode.isLast ? "└" : "├");
467
+ }
468
+ else if (posInLevel === 1) {
469
+ prefixChars.push("─");
470
+ }
471
+ else {
472
+ prefixChars.push(" ");
473
+ }
474
+ }
475
+ else {
476
+ prefixChars.push(" ");
477
+ }
478
+ }
479
+ const prefix = prefixChars.join("");
480
+ // Active path marker - shown right before the entry text
481
+ const isOnActivePath = this.activePathIds.has(entry.id);
482
+ const pathMarker = isOnActivePath ? theme.fg("accent", "• ") : "";
483
+ const label = flatNode.node.label ? theme.fg("warning", `[${flatNode.node.label}] `) : "";
484
+ const content = this.getEntryDisplayText(flatNode.node, isSelected);
485
+ let line = cursor + theme.fg("dim", prefix) + pathMarker + label + content;
486
+ if (isSelected) {
487
+ line = theme.bg("selectedBg", line);
488
+ }
489
+ lines.push(truncateToWidth(line, width));
490
+ }
491
+ lines.push(truncateToWidth(theme.fg("muted", ` (${this.selectedIndex + 1}/${this.filteredNodes.length})${this.getFilterLabel()}`), width));
492
+ return lines;
493
+ }
494
+ getEntryDisplayText(node, isSelected) {
495
+ const entry = node.entry;
496
+ let result;
497
+ const normalize = (s) => s.replace(/[\n\t]/g, " ").trim();
498
+ switch (entry.type) {
499
+ case "message": {
500
+ const msg = entry.message;
501
+ const role = msg.role;
502
+ if (role === "user") {
503
+ const msgWithContent = msg;
504
+ const content = normalize(this.extractContent(msgWithContent.content));
505
+ result = theme.fg("accent", "user: ") + content;
506
+ }
507
+ else if (role === "assistant") {
508
+ const msgWithContent = msg;
509
+ const textContent = normalize(this.extractContent(msgWithContent.content));
510
+ if (textContent) {
511
+ result = theme.fg("success", "assistant: ") + textContent;
512
+ }
513
+ else if (msgWithContent.stopReason === "aborted") {
514
+ result = theme.fg("success", "assistant: ") + theme.fg("muted", "(aborted)");
515
+ }
516
+ else if (msgWithContent.errorMessage) {
517
+ const errMsg = normalize(msgWithContent.errorMessage).slice(0, 80);
518
+ result = theme.fg("success", "assistant: ") + theme.fg("error", errMsg);
519
+ }
520
+ else {
521
+ result = theme.fg("success", "assistant: ") + theme.fg("muted", "(no content)");
522
+ }
523
+ }
524
+ else if (role === "toolResult") {
525
+ const toolMsg = msg;
526
+ const toolCall = toolMsg.toolCallId ? this.toolCallMap.get(toolMsg.toolCallId) : undefined;
527
+ if (toolCall) {
528
+ result = theme.fg("muted", this.formatToolCall(toolCall.name, toolCall.arguments));
529
+ }
530
+ else {
531
+ result = theme.fg("muted", `[${toolMsg.toolName ?? "tool"}]`);
532
+ }
533
+ }
534
+ else if (role === "bashExecution") {
535
+ const bashMsg = msg;
536
+ result = theme.fg("dim", `[bash]: ${normalize(bashMsg.command ?? "")}`);
537
+ }
538
+ else {
539
+ result = theme.fg("dim", `[${role}]`);
540
+ }
541
+ break;
542
+ }
543
+ case "custom_message": {
544
+ const content = typeof entry.content === "string"
545
+ ? entry.content
546
+ : entry.content
547
+ .filter((c) => c.type === "text")
548
+ .map((c) => c.text)
549
+ .join("");
550
+ result = theme.fg("customMessageLabel", `[${entry.customType}]: `) + normalize(content);
551
+ break;
552
+ }
553
+ case "compaction": {
554
+ const tokens = Math.round(entry.tokensBefore / 1000);
555
+ result = theme.fg("borderAccent", `[compaction: ${tokens}k tokens]`);
556
+ break;
557
+ }
558
+ case "branch_summary":
559
+ result = theme.fg("warning", `[branch summary]: `) + normalize(entry.summary);
560
+ break;
561
+ case "model_change":
562
+ result = theme.fg("dim", `[model: ${entry.modelId}]`);
563
+ break;
564
+ case "thinking_level_change":
565
+ result = theme.fg("dim", `[thinking: ${entry.thinkingLevel}]`);
566
+ break;
567
+ case "custom":
568
+ result = theme.fg("dim", `[custom: ${entry.customType}]`);
569
+ break;
570
+ case "label":
571
+ result = theme.fg("dim", `[label: ${entry.label ?? "(cleared)"}]`);
572
+ break;
573
+ default:
574
+ result = "";
575
+ }
576
+ return isSelected ? theme.bold(result) : result;
577
+ }
578
+ extractContent(content) {
579
+ const maxLen = 200;
580
+ if (typeof content === "string")
581
+ return content.slice(0, maxLen);
582
+ if (Array.isArray(content)) {
583
+ let result = "";
584
+ for (const c of content) {
585
+ if (typeof c === "object" && c !== null && "type" in c && c.type === "text") {
586
+ result += c.text;
587
+ if (result.length >= maxLen)
588
+ return result.slice(0, maxLen);
589
+ }
590
+ }
591
+ return result;
592
+ }
593
+ return "";
594
+ }
595
+ hasTextContent(content) {
596
+ if (typeof content === "string")
597
+ return content.trim().length > 0;
598
+ if (Array.isArray(content)) {
599
+ for (const c of content) {
600
+ if (typeof c === "object" && c !== null && "type" in c && c.type === "text") {
601
+ const text = c.text;
602
+ if (text && text.trim().length > 0)
603
+ return true;
604
+ }
605
+ }
606
+ }
607
+ return false;
608
+ }
609
+ formatToolCall(name, args) {
610
+ const shortenPath = (p) => {
611
+ const home = process.env.HOME || process.env.USERPROFILE || "";
612
+ if (home && p.startsWith(home))
613
+ return `~${p.slice(home.length)}`;
614
+ return p;
615
+ };
616
+ switch (name) {
617
+ case "read": {
618
+ const path = shortenPath(String(args.path || args.file_path || ""));
619
+ const offset = args.offset;
620
+ const limit = args.limit;
621
+ let display = path;
622
+ if (offset !== undefined || limit !== undefined) {
623
+ const start = offset ?? 1;
624
+ const end = limit !== undefined ? start + limit - 1 : "";
625
+ display += `:${start}${end ? `-${end}` : ""}`;
626
+ }
627
+ return `[read: ${display}]`;
628
+ }
629
+ case "write": {
630
+ const path = shortenPath(String(args.path || args.file_path || ""));
631
+ return `[write: ${path}]`;
632
+ }
633
+ case "edit": {
634
+ const path = shortenPath(String(args.path || args.file_path || ""));
635
+ return `[edit: ${path}]`;
636
+ }
637
+ case "bash": {
638
+ const rawCmd = String(args.command || "");
639
+ const cmd = rawCmd
640
+ .replace(/[\n\t]/g, " ")
641
+ .trim()
642
+ .slice(0, 50);
643
+ return `[bash: ${cmd}${rawCmd.length > 50 ? "..." : ""}]`;
644
+ }
645
+ case "grep": {
646
+ const pattern = String(args.pattern || "");
647
+ const path = shortenPath(String(args.path || "."));
648
+ return `[grep: /${pattern}/ in ${path}]`;
649
+ }
650
+ case "find": {
651
+ const pattern = String(args.pattern || "");
652
+ const path = shortenPath(String(args.path || "."));
653
+ return `[find: ${pattern} in ${path}]`;
654
+ }
655
+ case "ls": {
656
+ const path = shortenPath(String(args.path || "."));
657
+ return `[ls: ${path}]`;
658
+ }
659
+ default: {
660
+ // Custom tool - show name and truncated JSON args
661
+ const argsStr = JSON.stringify(args).slice(0, 40);
662
+ return `[${name}: ${argsStr}${JSON.stringify(args).length > 40 ? "..." : ""}]`;
663
+ }
664
+ }
665
+ }
666
+ handleInput(keyData) {
667
+ const kb = getEditorKeybindings();
668
+ if (kb.matches(keyData, "selectUp")) {
669
+ this.selectedIndex = this.selectedIndex === 0 ? this.filteredNodes.length - 1 : this.selectedIndex - 1;
670
+ }
671
+ else if (kb.matches(keyData, "selectDown")) {
672
+ this.selectedIndex = this.selectedIndex === this.filteredNodes.length - 1 ? 0 : this.selectedIndex + 1;
673
+ }
674
+ else if (kb.matches(keyData, "cursorLeft")) {
675
+ // Page up
676
+ this.selectedIndex = Math.max(0, this.selectedIndex - this.maxVisibleLines);
677
+ }
678
+ else if (kb.matches(keyData, "cursorRight")) {
679
+ // Page down
680
+ this.selectedIndex = Math.min(this.filteredNodes.length - 1, this.selectedIndex + this.maxVisibleLines);
681
+ }
682
+ else if (kb.matches(keyData, "selectConfirm")) {
683
+ const selected = this.filteredNodes[this.selectedIndex];
684
+ if (selected && this.onSelect) {
685
+ this.onSelect(selected.node.entry.id);
686
+ }
687
+ }
688
+ else if (kb.matches(keyData, "selectCancel")) {
689
+ if (this.searchQuery) {
690
+ this.searchQuery = "";
691
+ this.applyFilter();
692
+ }
693
+ else {
694
+ this.onCancel?.();
695
+ }
696
+ }
697
+ else if (matchesKey(keyData, "ctrl+d")) {
698
+ // Direct filter: default
699
+ this.filterMode = "default";
700
+ this.applyFilter();
701
+ }
702
+ else if (matchesKey(keyData, "ctrl+t")) {
703
+ // Toggle filter: no-tools ↔ default
704
+ this.filterMode = this.filterMode === "no-tools" ? "default" : "no-tools";
705
+ this.applyFilter();
706
+ }
707
+ else if (matchesKey(keyData, "ctrl+u")) {
708
+ // Toggle filter: user-only ↔ default
709
+ this.filterMode = this.filterMode === "user-only" ? "default" : "user-only";
710
+ this.applyFilter();
711
+ }
712
+ else if (matchesKey(keyData, "ctrl+l")) {
713
+ // Toggle filter: labeled-only ↔ default
714
+ this.filterMode = this.filterMode === "labeled-only" ? "default" : "labeled-only";
715
+ this.applyFilter();
716
+ }
717
+ else if (matchesKey(keyData, "ctrl+a")) {
718
+ // Toggle filter: all ↔ default
719
+ this.filterMode = this.filterMode === "all" ? "default" : "all";
720
+ this.applyFilter();
721
+ }
722
+ else if (matchesKey(keyData, "shift+ctrl+o")) {
723
+ // Cycle filter backwards
724
+ const modes = ["default", "no-tools", "user-only", "labeled-only", "all"];
725
+ const currentIndex = modes.indexOf(this.filterMode);
726
+ this.filterMode = modes[(currentIndex - 1 + modes.length) % modes.length];
727
+ this.applyFilter();
728
+ }
729
+ else if (matchesKey(keyData, "ctrl+o")) {
730
+ // Cycle filter forwards: default → no-tools → user-only → labeled-only → all → default
731
+ const modes = ["default", "no-tools", "user-only", "labeled-only", "all"];
732
+ const currentIndex = modes.indexOf(this.filterMode);
733
+ this.filterMode = modes[(currentIndex + 1) % modes.length];
734
+ this.applyFilter();
735
+ }
736
+ else if (kb.matches(keyData, "deleteCharBackward")) {
737
+ if (this.searchQuery.length > 0) {
738
+ this.searchQuery = this.searchQuery.slice(0, -1);
739
+ this.applyFilter();
740
+ }
741
+ }
742
+ else if (matchesKey(keyData, "shift+l")) {
743
+ const selected = this.filteredNodes[this.selectedIndex];
744
+ if (selected && this.onLabelEdit) {
745
+ this.onLabelEdit(selected.node.entry.id, selected.node.label);
746
+ }
747
+ }
748
+ else {
749
+ const hasControlChars = [...keyData].some((ch) => {
750
+ const code = ch.charCodeAt(0);
751
+ return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
752
+ });
753
+ if (!hasControlChars && keyData.length > 0) {
754
+ this.searchQuery += keyData;
755
+ this.applyFilter();
756
+ }
757
+ }
758
+ }
759
+ }
760
+ /** Component that displays the current search query */
761
+ class SearchLine {
762
+ constructor(treeList) {
763
+ this.treeList = treeList;
764
+ }
765
+ invalidate() { }
766
+ render(width) {
767
+ const query = this.treeList.getSearchQuery();
768
+ if (query) {
769
+ return [truncateToWidth(` ${theme.fg("muted", "Type to search:")} ${theme.fg("accent", query)}`, width)];
770
+ }
771
+ return [truncateToWidth(` ${theme.fg("muted", "Type to search:")}`, width)];
772
+ }
773
+ handleInput(_keyData) { }
774
+ }
775
+ /** Label input component shown when editing a label */
776
+ class LabelInput {
777
+ get focused() {
778
+ return this._focused;
779
+ }
780
+ set focused(value) {
781
+ this._focused = value;
782
+ this.input.focused = value;
783
+ }
784
+ constructor(entryId, currentLabel) {
785
+ // Focusable implementation - propagate to input for IME cursor positioning
786
+ this._focused = false;
787
+ this.entryId = entryId;
788
+ this.input = new Input();
789
+ if (currentLabel) {
790
+ this.input.setValue(currentLabel);
791
+ }
792
+ }
793
+ invalidate() { }
794
+ render(width) {
795
+ const lines = [];
796
+ const indent = " ";
797
+ const availableWidth = width - indent.length;
798
+ lines.push(truncateToWidth(`${indent}${theme.fg("muted", "Label (empty to remove):")}`, width));
799
+ lines.push(...this.input.render(availableWidth).map((line) => truncateToWidth(`${indent}${line}`, width)));
800
+ lines.push(truncateToWidth(`${indent}${keyHint("selectConfirm", "save")} ${keyHint("selectCancel", "cancel")}`, width));
801
+ return lines;
802
+ }
803
+ handleInput(keyData) {
804
+ const kb = getEditorKeybindings();
805
+ if (kb.matches(keyData, "selectConfirm")) {
806
+ const value = this.input.getValue().trim();
807
+ this.onSubmit?.(this.entryId, value || undefined);
808
+ }
809
+ else if (kb.matches(keyData, "selectCancel")) {
810
+ this.onCancel?.();
811
+ }
812
+ else {
813
+ this.input.handleInput(keyData);
814
+ }
815
+ }
816
+ }
817
+ /**
818
+ * Component that renders a session tree selector for navigation
819
+ */
820
+ export class TreeSelectorComponent extends Container {
821
+ get focused() {
822
+ return this._focused;
823
+ }
824
+ set focused(value) {
825
+ this._focused = value;
826
+ // Propagate to labelInput when it's active
827
+ if (this.labelInput) {
828
+ this.labelInput.focused = value;
829
+ }
830
+ }
831
+ constructor(tree, currentLeafId, terminalHeight, onSelect, onCancel, onLabelChange, initialSelectedId) {
832
+ super();
833
+ this.labelInput = null;
834
+ // Focusable implementation - propagate to labelInput when active for IME cursor positioning
835
+ this._focused = false;
836
+ this.onLabelChangeCallback = onLabelChange;
837
+ const maxVisibleLines = Math.max(5, Math.floor(terminalHeight / 2));
838
+ this.treeList = new TreeList(tree, currentLeafId, maxVisibleLines, initialSelectedId);
839
+ this.treeList.onSelect = onSelect;
840
+ this.treeList.onCancel = onCancel;
841
+ this.treeList.onLabelEdit = (entryId, currentLabel) => this.showLabelInput(entryId, currentLabel);
842
+ this.treeContainer = new Container();
843
+ this.treeContainer.addChild(this.treeList);
844
+ this.labelInputContainer = new Container();
845
+ this.addChild(new Spacer(1));
846
+ this.addChild(new DynamicBorder());
847
+ this.addChild(new Text(theme.bold(" Session Tree"), 1, 0));
848
+ this.addChild(new TruncatedText(theme.fg("muted", " ↑/↓: move. ←/→: page. Shift+L: label. ") +
849
+ theme.fg("muted", "^D/^T/^U/^L/^A: filters (^O/⇧^O cycle)"), 0, 0));
850
+ this.addChild(new SearchLine(this.treeList));
851
+ this.addChild(new DynamicBorder());
852
+ this.addChild(new Spacer(1));
853
+ this.addChild(this.treeContainer);
854
+ this.addChild(this.labelInputContainer);
855
+ this.addChild(new Spacer(1));
856
+ this.addChild(new DynamicBorder());
857
+ if (tree.length === 0) {
858
+ setTimeout(() => onCancel(), 100);
859
+ }
860
+ }
861
+ showLabelInput(entryId, currentLabel) {
862
+ this.labelInput = new LabelInput(entryId, currentLabel);
863
+ this.labelInput.onSubmit = (id, label) => {
864
+ this.treeList.updateNodeLabel(id, label);
865
+ this.onLabelChangeCallback?.(id, label);
866
+ this.hideLabelInput();
867
+ };
868
+ this.labelInput.onCancel = () => this.hideLabelInput();
869
+ // Propagate current focused state to the new labelInput
870
+ this.labelInput.focused = this._focused;
871
+ this.treeContainer.clear();
872
+ this.labelInputContainer.clear();
873
+ this.labelInputContainer.addChild(this.labelInput);
874
+ }
875
+ hideLabelInput() {
876
+ this.labelInput = null;
877
+ this.labelInputContainer.clear();
878
+ this.treeContainer.clear();
879
+ this.treeContainer.addChild(this.treeList);
880
+ }
881
+ handleInput(keyData) {
882
+ if (this.labelInput) {
883
+ this.labelInput.handleInput(keyData);
884
+ }
885
+ else {
886
+ this.treeList.handleInput(keyData);
887
+ }
888
+ }
889
+ getTreeList() {
890
+ return this.treeList;
891
+ }
892
+ }