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