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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. package/CHANGELOG.md +1228 -0
  2. package/README.md +1041 -0
  3. package/docs/compaction.md +403 -0
  4. package/docs/custom-tools.md +541 -0
  5. package/docs/extension-loading.md +1004 -0
  6. package/docs/hooks.md +867 -0
  7. package/docs/rpc.md +1040 -0
  8. package/docs/sdk.md +994 -0
  9. package/docs/session-tree-plan.md +441 -0
  10. package/docs/session.md +240 -0
  11. package/docs/skills.md +290 -0
  12. package/docs/theme.md +637 -0
  13. package/docs/tree.md +197 -0
  14. package/docs/tui.md +341 -0
  15. package/examples/README.md +21 -0
  16. package/examples/custom-tools/README.md +124 -0
  17. package/examples/custom-tools/hello/index.ts +20 -0
  18. package/examples/custom-tools/question/index.ts +84 -0
  19. package/examples/custom-tools/subagent/README.md +172 -0
  20. package/examples/custom-tools/subagent/agents/planner.md +37 -0
  21. package/examples/custom-tools/subagent/agents/reviewer.md +35 -0
  22. package/examples/custom-tools/subagent/agents/scout.md +50 -0
  23. package/examples/custom-tools/subagent/agents/worker.md +24 -0
  24. package/examples/custom-tools/subagent/agents.ts +156 -0
  25. package/examples/custom-tools/subagent/commands/implement-and-review.md +10 -0
  26. package/examples/custom-tools/subagent/commands/implement.md +10 -0
  27. package/examples/custom-tools/subagent/commands/scout-and-plan.md +9 -0
  28. package/examples/custom-tools/subagent/index.ts +1002 -0
  29. package/examples/custom-tools/todo/index.ts +212 -0
  30. package/examples/hooks/README.md +56 -0
  31. package/examples/hooks/auto-commit-on-exit.ts +49 -0
  32. package/examples/hooks/confirm-destructive.ts +59 -0
  33. package/examples/hooks/custom-compaction.ts +116 -0
  34. package/examples/hooks/dirty-repo-guard.ts +52 -0
  35. package/examples/hooks/file-trigger.ts +41 -0
  36. package/examples/hooks/git-checkpoint.ts +53 -0
  37. package/examples/hooks/handoff.ts +150 -0
  38. package/examples/hooks/permission-gate.ts +34 -0
  39. package/examples/hooks/protected-paths.ts +30 -0
  40. package/examples/hooks/qna.ts +119 -0
  41. package/examples/hooks/snake.ts +343 -0
  42. package/examples/hooks/status-line.ts +40 -0
  43. package/examples/sdk/01-minimal.ts +22 -0
  44. package/examples/sdk/02-custom-model.ts +49 -0
  45. package/examples/sdk/03-custom-prompt.ts +44 -0
  46. package/examples/sdk/04-skills.ts +44 -0
  47. package/examples/sdk/05-tools.ts +90 -0
  48. package/examples/sdk/06-hooks.ts +61 -0
  49. package/examples/sdk/07-context-files.ts +36 -0
  50. package/examples/sdk/08-slash-commands.ts +42 -0
  51. package/examples/sdk/09-api-keys-and-oauth.ts +55 -0
  52. package/examples/sdk/10-settings.ts +38 -0
  53. package/examples/sdk/11-sessions.ts +48 -0
  54. package/examples/sdk/12-full-control.ts +95 -0
  55. package/examples/sdk/README.md +154 -0
  56. package/package.json +81 -0
  57. package/src/cli/args.ts +246 -0
  58. package/src/cli/file-processor.ts +72 -0
  59. package/src/cli/list-models.ts +104 -0
  60. package/src/cli/plugin-cli.ts +650 -0
  61. package/src/cli/session-picker.ts +41 -0
  62. package/src/cli.ts +10 -0
  63. package/src/commands/init.md +20 -0
  64. package/src/config.ts +159 -0
  65. package/src/core/agent-session.ts +1900 -0
  66. package/src/core/auth-storage.ts +236 -0
  67. package/src/core/bash-executor.ts +196 -0
  68. package/src/core/compaction/branch-summarization.ts +343 -0
  69. package/src/core/compaction/compaction.ts +742 -0
  70. package/src/core/compaction/index.ts +7 -0
  71. package/src/core/compaction/utils.ts +154 -0
  72. package/src/core/custom-tools/index.ts +21 -0
  73. package/src/core/custom-tools/loader.ts +248 -0
  74. package/src/core/custom-tools/types.ts +169 -0
  75. package/src/core/custom-tools/wrapper.ts +28 -0
  76. package/src/core/exec.ts +129 -0
  77. package/src/core/export-html/index.ts +211 -0
  78. package/src/core/export-html/template.css +781 -0
  79. package/src/core/export-html/template.html +54 -0
  80. package/src/core/export-html/template.js +1185 -0
  81. package/src/core/export-html/vendor/highlight.min.js +1213 -0
  82. package/src/core/export-html/vendor/marked.min.js +6 -0
  83. package/src/core/hooks/index.ts +16 -0
  84. package/src/core/hooks/loader.ts +312 -0
  85. package/src/core/hooks/runner.ts +434 -0
  86. package/src/core/hooks/tool-wrapper.ts +99 -0
  87. package/src/core/hooks/types.ts +773 -0
  88. package/src/core/index.ts +52 -0
  89. package/src/core/mcp/client.ts +158 -0
  90. package/src/core/mcp/config.ts +154 -0
  91. package/src/core/mcp/index.ts +45 -0
  92. package/src/core/mcp/loader.ts +68 -0
  93. package/src/core/mcp/manager.ts +181 -0
  94. package/src/core/mcp/tool-bridge.ts +148 -0
  95. package/src/core/mcp/transports/http.ts +316 -0
  96. package/src/core/mcp/transports/index.ts +6 -0
  97. package/src/core/mcp/transports/stdio.ts +252 -0
  98. package/src/core/mcp/types.ts +220 -0
  99. package/src/core/messages.ts +189 -0
  100. package/src/core/model-registry.ts +317 -0
  101. package/src/core/model-resolver.ts +393 -0
  102. package/src/core/plugins/doctor.ts +59 -0
  103. package/src/core/plugins/index.ts +38 -0
  104. package/src/core/plugins/installer.ts +189 -0
  105. package/src/core/plugins/loader.ts +338 -0
  106. package/src/core/plugins/manager.ts +672 -0
  107. package/src/core/plugins/parser.ts +105 -0
  108. package/src/core/plugins/paths.ts +32 -0
  109. package/src/core/plugins/types.ts +190 -0
  110. package/src/core/sdk.ts +760 -0
  111. package/src/core/session-manager.ts +1128 -0
  112. package/src/core/settings-manager.ts +443 -0
  113. package/src/core/skills.ts +437 -0
  114. package/src/core/slash-commands.ts +248 -0
  115. package/src/core/system-prompt.ts +439 -0
  116. package/src/core/timings.ts +25 -0
  117. package/src/core/tools/ask.ts +211 -0
  118. package/src/core/tools/bash-interceptor.ts +120 -0
  119. package/src/core/tools/bash.ts +250 -0
  120. package/src/core/tools/context.ts +32 -0
  121. package/src/core/tools/edit-diff.ts +475 -0
  122. package/src/core/tools/edit.ts +208 -0
  123. package/src/core/tools/exa/company.ts +59 -0
  124. package/src/core/tools/exa/index.ts +64 -0
  125. package/src/core/tools/exa/linkedin.ts +59 -0
  126. package/src/core/tools/exa/logger.ts +56 -0
  127. package/src/core/tools/exa/mcp-client.ts +368 -0
  128. package/src/core/tools/exa/render.ts +196 -0
  129. package/src/core/tools/exa/researcher.ts +90 -0
  130. package/src/core/tools/exa/search.ts +337 -0
  131. package/src/core/tools/exa/types.ts +168 -0
  132. package/src/core/tools/exa/websets.ts +248 -0
  133. package/src/core/tools/find.ts +261 -0
  134. package/src/core/tools/grep.ts +555 -0
  135. package/src/core/tools/index.ts +202 -0
  136. package/src/core/tools/ls.ts +140 -0
  137. package/src/core/tools/lsp/client.ts +605 -0
  138. package/src/core/tools/lsp/config.ts +147 -0
  139. package/src/core/tools/lsp/edits.ts +101 -0
  140. package/src/core/tools/lsp/index.ts +804 -0
  141. package/src/core/tools/lsp/render.ts +447 -0
  142. package/src/core/tools/lsp/rust-analyzer.ts +145 -0
  143. package/src/core/tools/lsp/types.ts +463 -0
  144. package/src/core/tools/lsp/utils.ts +486 -0
  145. package/src/core/tools/notebook.ts +229 -0
  146. package/src/core/tools/path-utils.ts +61 -0
  147. package/src/core/tools/read.ts +240 -0
  148. package/src/core/tools/renderers.ts +540 -0
  149. package/src/core/tools/task/agents.ts +153 -0
  150. package/src/core/tools/task/artifacts.ts +114 -0
  151. package/src/core/tools/task/bundled-agents/browser.md +71 -0
  152. package/src/core/tools/task/bundled-agents/explore.md +82 -0
  153. package/src/core/tools/task/bundled-agents/plan.md +54 -0
  154. package/src/core/tools/task/bundled-agents/reviewer.md +59 -0
  155. package/src/core/tools/task/bundled-agents/task.md +53 -0
  156. package/src/core/tools/task/bundled-commands/architect-plan.md +10 -0
  157. package/src/core/tools/task/bundled-commands/implement-with-critic.md +11 -0
  158. package/src/core/tools/task/bundled-commands/implement.md +11 -0
  159. package/src/core/tools/task/commands.ts +213 -0
  160. package/src/core/tools/task/discovery.ts +208 -0
  161. package/src/core/tools/task/executor.ts +367 -0
  162. package/src/core/tools/task/index.ts +388 -0
  163. package/src/core/tools/task/model-resolver.ts +115 -0
  164. package/src/core/tools/task/parallel.ts +38 -0
  165. package/src/core/tools/task/render.ts +232 -0
  166. package/src/core/tools/task/types.ts +99 -0
  167. package/src/core/tools/truncate.ts +265 -0
  168. package/src/core/tools/web-fetch.ts +2370 -0
  169. package/src/core/tools/web-search/auth.ts +193 -0
  170. package/src/core/tools/web-search/index.ts +537 -0
  171. package/src/core/tools/web-search/providers/anthropic.ts +198 -0
  172. package/src/core/tools/web-search/providers/exa.ts +302 -0
  173. package/src/core/tools/web-search/providers/perplexity.ts +195 -0
  174. package/src/core/tools/web-search/render.ts +182 -0
  175. package/src/core/tools/web-search/types.ts +180 -0
  176. package/src/core/tools/write.ts +99 -0
  177. package/src/index.ts +176 -0
  178. package/src/main.ts +464 -0
  179. package/src/migrations.ts +135 -0
  180. package/src/modes/index.ts +43 -0
  181. package/src/modes/interactive/components/armin.ts +382 -0
  182. package/src/modes/interactive/components/assistant-message.ts +86 -0
  183. package/src/modes/interactive/components/bash-execution.ts +196 -0
  184. package/src/modes/interactive/components/bordered-loader.ts +41 -0
  185. package/src/modes/interactive/components/branch-summary-message.ts +42 -0
  186. package/src/modes/interactive/components/compaction-summary-message.ts +45 -0
  187. package/src/modes/interactive/components/custom-editor.ts +122 -0
  188. package/src/modes/interactive/components/diff.ts +147 -0
  189. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  190. package/src/modes/interactive/components/footer.ts +381 -0
  191. package/src/modes/interactive/components/hook-editor.ts +117 -0
  192. package/src/modes/interactive/components/hook-input.ts +64 -0
  193. package/src/modes/interactive/components/hook-message.ts +96 -0
  194. package/src/modes/interactive/components/hook-selector.ts +91 -0
  195. package/src/modes/interactive/components/model-selector.ts +247 -0
  196. package/src/modes/interactive/components/oauth-selector.ts +120 -0
  197. package/src/modes/interactive/components/plugin-settings.ts +479 -0
  198. package/src/modes/interactive/components/queue-mode-selector.ts +56 -0
  199. package/src/modes/interactive/components/session-selector.ts +204 -0
  200. package/src/modes/interactive/components/settings-selector.ts +453 -0
  201. package/src/modes/interactive/components/show-images-selector.ts +45 -0
  202. package/src/modes/interactive/components/theme-selector.ts +62 -0
  203. package/src/modes/interactive/components/thinking-selector.ts +64 -0
  204. package/src/modes/interactive/components/tool-execution.ts +675 -0
  205. package/src/modes/interactive/components/tree-selector.ts +866 -0
  206. package/src/modes/interactive/components/user-message-selector.ts +159 -0
  207. package/src/modes/interactive/components/user-message.ts +18 -0
  208. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  209. package/src/modes/interactive/components/welcome.ts +183 -0
  210. package/src/modes/interactive/interactive-mode.ts +2516 -0
  211. package/src/modes/interactive/theme/dark.json +101 -0
  212. package/src/modes/interactive/theme/light.json +98 -0
  213. package/src/modes/interactive/theme/theme-schema.json +308 -0
  214. package/src/modes/interactive/theme/theme.ts +998 -0
  215. package/src/modes/print-mode.ts +128 -0
  216. package/src/modes/rpc/rpc-client.ts +527 -0
  217. package/src/modes/rpc/rpc-mode.ts +483 -0
  218. package/src/modes/rpc/rpc-types.ts +203 -0
  219. package/src/utils/changelog.ts +99 -0
  220. package/src/utils/clipboard.ts +265 -0
  221. package/src/utils/fuzzy.ts +108 -0
  222. package/src/utils/mime.ts +30 -0
  223. package/src/utils/shell.ts +276 -0
  224. package/src/utils/tools-manager.ts +274 -0
@@ -0,0 +1,1185 @@
1
+ (function() {
2
+ 'use strict';
3
+
4
+ // ============================================================
5
+ // DATA LOADING
6
+ // ============================================================
7
+
8
+ const base64 = document.getElementById('session-data').textContent;
9
+ const binary = atob(base64);
10
+ const bytes = new Uint8Array(binary.length);
11
+ for (let i = 0; i < binary.length; i++) {
12
+ bytes[i] = binary.charCodeAt(i);
13
+ }
14
+ const data = JSON.parse(new TextDecoder('utf-8').decode(bytes));
15
+ const { header, entries, leafId, systemPrompt, tools } = data;
16
+
17
+ // ============================================================
18
+ // DATA STRUCTURES
19
+ // ============================================================
20
+
21
+ // Entry lookup by ID
22
+ const byId = new Map();
23
+ for (const entry of entries) {
24
+ byId.set(entry.id, entry);
25
+ }
26
+
27
+ // Tool call lookup (toolCallId -> {name, arguments})
28
+ const toolCallMap = new Map();
29
+ for (const entry of entries) {
30
+ if (entry.type === 'message' && entry.message.role === 'assistant') {
31
+ const content = entry.message.content;
32
+ if (Array.isArray(content)) {
33
+ for (const block of content) {
34
+ if (block.type === 'toolCall') {
35
+ toolCallMap.set(block.id, { name: block.name, arguments: block.arguments });
36
+ }
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ // Label lookup (entryId -> label string)
43
+ // Labels are stored in 'label' entries that reference their target via parentId
44
+ const labelMap = new Map();
45
+ for (const entry of entries) {
46
+ if (entry.type === 'label' && entry.parentId && entry.label) {
47
+ labelMap.set(entry.parentId, entry.label);
48
+ }
49
+ }
50
+
51
+ // ============================================================
52
+ // TREE DATA PREPARATION (no DOM, pure data)
53
+ // ============================================================
54
+
55
+ /**
56
+ * Build tree structure from flat entries.
57
+ * Returns array of root nodes, each with { entry, children, label }.
58
+ */
59
+ function buildTree() {
60
+ const nodeMap = new Map();
61
+ const roots = [];
62
+
63
+ // Create nodes
64
+ for (const entry of entries) {
65
+ nodeMap.set(entry.id, {
66
+ entry,
67
+ children: [],
68
+ label: labelMap.get(entry.id)
69
+ });
70
+ }
71
+
72
+ // Build parent-child relationships
73
+ for (const entry of entries) {
74
+ const node = nodeMap.get(entry.id);
75
+ if (entry.parentId === null || entry.parentId === undefined || entry.parentId === entry.id) {
76
+ roots.push(node);
77
+ } else {
78
+ const parent = nodeMap.get(entry.parentId);
79
+ if (parent) {
80
+ parent.children.push(node);
81
+ } else {
82
+ roots.push(node);
83
+ }
84
+ }
85
+ }
86
+
87
+ // Sort children by timestamp
88
+ function sortChildren(node) {
89
+ node.children.sort((a, b) =>
90
+ new Date(a.entry.timestamp).getTime() - new Date(b.entry.timestamp).getTime()
91
+ );
92
+ node.children.forEach(sortChildren);
93
+ }
94
+ roots.forEach(sortChildren);
95
+
96
+ return roots;
97
+ }
98
+
99
+ /**
100
+ * Build set of entry IDs on path from root to target.
101
+ */
102
+ function buildActivePathIds(targetId) {
103
+ const ids = new Set();
104
+ let current = byId.get(targetId);
105
+ while (current) {
106
+ ids.add(current.id);
107
+ // Stop if no parent or self-referencing (root)
108
+ if (!current.parentId || current.parentId === current.id) {
109
+ break;
110
+ }
111
+ current = byId.get(current.parentId);
112
+ }
113
+ return ids;
114
+ }
115
+
116
+ /**
117
+ * Get array of entries from root to target (the conversation path).
118
+ */
119
+ function getPath(targetId) {
120
+ const path = [];
121
+ let current = byId.get(targetId);
122
+ while (current) {
123
+ path.unshift(current);
124
+ // Stop if no parent or self-referencing (root)
125
+ if (!current.parentId || current.parentId === current.id) {
126
+ break;
127
+ }
128
+ current = byId.get(current.parentId);
129
+ }
130
+ return path;
131
+ }
132
+
133
+ /**
134
+ * Flatten tree into list with indentation and connector info.
135
+ * Returns array of { node, indent, showConnector, isLast, gutters, isVirtualRootChild, multipleRoots }.
136
+ * Matches tree-selector.ts logic exactly.
137
+ */
138
+ function flattenTree(roots, activePathIds) {
139
+ const result = [];
140
+ const multipleRoots = roots.length > 1;
141
+
142
+ // Mark which subtrees contain the active leaf
143
+ const containsActive = new Map();
144
+ function markActive(node) {
145
+ let has = activePathIds.has(node.entry.id);
146
+ for (const child of node.children) {
147
+ if (markActive(child)) has = true;
148
+ }
149
+ containsActive.set(node, has);
150
+ return has;
151
+ }
152
+ roots.forEach(markActive);
153
+
154
+ // Stack: [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild]
155
+ const stack = [];
156
+
157
+ // Add roots (prioritize branch containing active leaf)
158
+ const orderedRoots = [...roots].sort((a, b) =>
159
+ Number(containsActive.get(b)) - Number(containsActive.get(a))
160
+ );
161
+ for (let i = orderedRoots.length - 1; i >= 0; i--) {
162
+ const isLast = i === orderedRoots.length - 1;
163
+ stack.push([orderedRoots[i], multipleRoots ? 1 : 0, multipleRoots, multipleRoots, isLast, [], multipleRoots]);
164
+ }
165
+
166
+ while (stack.length > 0) {
167
+ const [node, indent, justBranched, showConnector, isLast, gutters, isVirtualRootChild] = stack.pop();
168
+
169
+ result.push({ node, indent, showConnector, isLast, gutters, isVirtualRootChild, multipleRoots });
170
+
171
+ const children = node.children;
172
+ const multipleChildren = children.length > 1;
173
+
174
+ // Order children (active branch first)
175
+ const orderedChildren = [...children].sort((a, b) =>
176
+ Number(containsActive.get(b)) - Number(containsActive.get(a))
177
+ );
178
+
179
+ // Calculate child indent (matches tree-selector.ts)
180
+ let childIndent;
181
+ if (multipleChildren) {
182
+ // Parent branches: children get +1
183
+ childIndent = indent + 1;
184
+ } else if (justBranched && indent > 0) {
185
+ // First generation after a branch: +1 for visual grouping
186
+ childIndent = indent + 1;
187
+ } else {
188
+ // Single-child chain: stay flat
189
+ childIndent = indent;
190
+ }
191
+
192
+ // Build gutters for children
193
+ const connectorDisplayed = showConnector && !isVirtualRootChild;
194
+ const currentDisplayIndent = multipleRoots ? Math.max(0, indent - 1) : indent;
195
+ const connectorPosition = Math.max(0, currentDisplayIndent - 1);
196
+ const childGutters = connectorDisplayed
197
+ ? [...gutters, { position: connectorPosition, show: !isLast }]
198
+ : gutters;
199
+
200
+ // Add children in reverse order for stack
201
+ for (let i = orderedChildren.length - 1; i >= 0; i--) {
202
+ const childIsLast = i === orderedChildren.length - 1;
203
+ stack.push([orderedChildren[i], childIndent, multipleChildren, multipleChildren, childIsLast, childGutters, false]);
204
+ }
205
+ }
206
+
207
+ return result;
208
+ }
209
+
210
+ /**
211
+ * Build ASCII prefix string for tree node.
212
+ */
213
+ function buildTreePrefix(flatNode) {
214
+ const { indent, showConnector, isLast, gutters, isVirtualRootChild, multipleRoots } = flatNode;
215
+ const displayIndent = multipleRoots ? Math.max(0, indent - 1) : indent;
216
+ const connector = showConnector && !isVirtualRootChild ? (isLast ? '└─ ' : '├─ ') : '';
217
+ const connectorPosition = connector ? displayIndent - 1 : -1;
218
+
219
+ const totalChars = displayIndent * 3;
220
+ const prefixChars = [];
221
+ for (let i = 0; i < totalChars; i++) {
222
+ const level = Math.floor(i / 3);
223
+ const posInLevel = i % 3;
224
+
225
+ const gutter = gutters.find(g => g.position === level);
226
+ if (gutter) {
227
+ prefixChars.push(posInLevel === 0 ? (gutter.show ? '│' : ' ') : ' ');
228
+ } else if (connector && level === connectorPosition) {
229
+ if (posInLevel === 0) {
230
+ prefixChars.push(isLast ? '└' : '├');
231
+ } else if (posInLevel === 1) {
232
+ prefixChars.push('─');
233
+ } else {
234
+ prefixChars.push(' ');
235
+ }
236
+ } else {
237
+ prefixChars.push(' ');
238
+ }
239
+ }
240
+ return prefixChars.join('');
241
+ }
242
+
243
+ // ============================================================
244
+ // FILTERING (pure data)
245
+ // ============================================================
246
+
247
+ let filterMode = 'default';
248
+ let searchQuery = '';
249
+
250
+ function hasTextContent(content) {
251
+ if (typeof content === 'string') return content.trim().length > 0;
252
+ if (Array.isArray(content)) {
253
+ for (const c of content) {
254
+ if (c.type === 'text' && c.text && c.text.trim().length > 0) return true;
255
+ }
256
+ }
257
+ return false;
258
+ }
259
+
260
+ function extractContent(content) {
261
+ if (typeof content === 'string') return content;
262
+ if (Array.isArray(content)) {
263
+ return content
264
+ .filter(c => c.type === 'text' && c.text)
265
+ .map(c => c.text)
266
+ .join('');
267
+ }
268
+ return '';
269
+ }
270
+
271
+ function getSearchableText(entry, label) {
272
+ const parts = [];
273
+ if (label) parts.push(label);
274
+
275
+ switch (entry.type) {
276
+ case 'message': {
277
+ const msg = entry.message;
278
+ parts.push(msg.role);
279
+ if (msg.content) parts.push(extractContent(msg.content));
280
+ if (msg.role === 'bashExecution' && msg.command) parts.push(msg.command);
281
+ break;
282
+ }
283
+ case 'custom_message':
284
+ parts.push(entry.customType);
285
+ parts.push(typeof entry.content === 'string' ? entry.content : extractContent(entry.content));
286
+ break;
287
+ case 'compaction':
288
+ parts.push('compaction');
289
+ break;
290
+ case 'branch_summary':
291
+ parts.push('branch summary', entry.summary);
292
+ break;
293
+ case 'model_change':
294
+ parts.push('model', entry.modelId);
295
+ break;
296
+ case 'thinking_level_change':
297
+ parts.push('thinking', entry.thinkingLevel);
298
+ break;
299
+ }
300
+
301
+ return parts.join(' ').toLowerCase();
302
+ }
303
+
304
+ /**
305
+ * Filter flat nodes based on current filterMode and searchQuery.
306
+ */
307
+ function filterNodes(flatNodes, currentLeafId) {
308
+ const searchTokens = searchQuery.toLowerCase().split(/\s+/).filter(Boolean);
309
+
310
+ return flatNodes.filter(flatNode => {
311
+ const entry = flatNode.node.entry;
312
+ const label = flatNode.node.label;
313
+ const isCurrentLeaf = entry.id === currentLeafId;
314
+
315
+ // Always show current leaf
316
+ if (isCurrentLeaf) return true;
317
+
318
+ // Hide assistant messages with only tool calls (no text) unless error/aborted
319
+ if (entry.type === 'message' && entry.message.role === 'assistant') {
320
+ const msg = entry.message;
321
+ const hasText = hasTextContent(msg.content);
322
+ const isErrorOrAborted = msg.stopReason && msg.stopReason !== 'stop' && msg.stopReason !== 'toolUse';
323
+ if (!hasText && !isErrorOrAborted) return false;
324
+ }
325
+
326
+ // Apply filter mode
327
+ const isSettingsEntry = ['label', 'custom', 'model_change', 'thinking_level_change'].includes(entry.type);
328
+ let passesFilter = true;
329
+
330
+ switch (filterMode) {
331
+ case 'user-only':
332
+ passesFilter = entry.type === 'message' && entry.message.role === 'user';
333
+ break;
334
+ case 'no-tools':
335
+ passesFilter = !isSettingsEntry && !(entry.type === 'message' && entry.message.role === 'toolResult');
336
+ break;
337
+ case 'labeled-only':
338
+ passesFilter = label !== undefined;
339
+ break;
340
+ case 'all':
341
+ passesFilter = true;
342
+ break;
343
+ default: // 'default'
344
+ passesFilter = !isSettingsEntry;
345
+ break;
346
+ }
347
+
348
+ if (!passesFilter) return false;
349
+
350
+ // Apply search filter
351
+ if (searchTokens.length > 0) {
352
+ const nodeText = getSearchableText(entry, label);
353
+ if (!searchTokens.every(t => nodeText.includes(t))) return false;
354
+ }
355
+
356
+ return true;
357
+ });
358
+ }
359
+
360
+ // ============================================================
361
+ // TREE DISPLAY TEXT (pure data -> string)
362
+ // ============================================================
363
+
364
+ function shortenPath(p) {
365
+ if (p.startsWith('/Users/')) {
366
+ const parts = p.split('/');
367
+ if (parts.length > 2) return '~' + p.slice(('/Users/' + parts[2]).length);
368
+ }
369
+ if (p.startsWith('/home/')) {
370
+ const parts = p.split('/');
371
+ if (parts.length > 2) return '~' + p.slice(('/home/' + parts[2]).length);
372
+ }
373
+ return p;
374
+ }
375
+
376
+ function formatToolCall(name, args) {
377
+ switch (name) {
378
+ case 'read': {
379
+ const path = shortenPath(String(args.path || args.file_path || ''));
380
+ const offset = args.offset;
381
+ const limit = args.limit;
382
+ let display = path;
383
+ if (offset !== undefined || limit !== undefined) {
384
+ const start = offset ?? 1;
385
+ const end = limit !== undefined ? start + limit - 1 : '';
386
+ display += `:${start}${end ? `-${end}` : ''}`;
387
+ }
388
+ return `[read: ${display}]`;
389
+ }
390
+ case 'write':
391
+ return `[write: ${shortenPath(String(args.path || args.file_path || ''))}]`;
392
+ case 'edit':
393
+ return `[edit: ${shortenPath(String(args.path || args.file_path || ''))}]`;
394
+ case 'bash': {
395
+ const rawCmd = String(args.command || '');
396
+ const cmd = rawCmd.replace(/[\n\t]/g, ' ').trim().slice(0, 50);
397
+ return `[bash: ${cmd}${rawCmd.length > 50 ? '...' : ''}]`;
398
+ }
399
+ case 'grep':
400
+ return `[grep: /${args.pattern || ''}/ in ${shortenPath(String(args.path || '.'))}]`;
401
+ case 'find':
402
+ return `[find: ${args.pattern || ''} in ${shortenPath(String(args.path || '.'))}]`;
403
+ case 'ls':
404
+ return `[ls: ${shortenPath(String(args.path || '.'))}]`;
405
+ default: {
406
+ const argsStr = JSON.stringify(args).slice(0, 40);
407
+ return `[${name}: ${argsStr}${JSON.stringify(args).length > 40 ? '...' : ''}]`;
408
+ }
409
+ }
410
+ }
411
+
412
+ function escapeHtml(text) {
413
+ const div = document.createElement('div');
414
+ div.textContent = text;
415
+ return div.innerHTML;
416
+ }
417
+
418
+ /**
419
+ * Truncate string to maxLen chars, append "..." if truncated.
420
+ */
421
+ function truncate(s, maxLen = 100) {
422
+ if (s.length <= maxLen) return s;
423
+ return s.slice(0, maxLen) + '...';
424
+ }
425
+
426
+ /**
427
+ * Get display text for tree node (returns HTML string).
428
+ */
429
+ function getTreeNodeDisplayHtml(entry, label) {
430
+ const normalize = s => s.replace(/[\n\t]/g, ' ').trim();
431
+ const labelHtml = label ? `<span class="tree-label">[${escapeHtml(label)}]</span> ` : '';
432
+
433
+ switch (entry.type) {
434
+ case 'message': {
435
+ const msg = entry.message;
436
+ if (msg.role === 'user') {
437
+ const content = truncate(normalize(extractContent(msg.content)));
438
+ return labelHtml + `<span class="tree-role-user">user:</span> ${escapeHtml(content)}`;
439
+ }
440
+ if (msg.role === 'assistant') {
441
+ const textContent = truncate(normalize(extractContent(msg.content)));
442
+ if (textContent) {
443
+ return labelHtml + `<span class="tree-role-assistant">assistant:</span> ${escapeHtml(textContent)}`;
444
+ }
445
+ if (msg.stopReason === 'aborted') {
446
+ return labelHtml + `<span class="tree-role-assistant">assistant:</span> <span class="tree-muted">(aborted)</span>`;
447
+ }
448
+ if (msg.errorMessage) {
449
+ return labelHtml + `<span class="tree-role-assistant">assistant:</span> <span class="tree-error">${escapeHtml(truncate(msg.errorMessage))}</span>`;
450
+ }
451
+ return labelHtml + `<span class="tree-role-assistant">assistant:</span> <span class="tree-muted">(no text)</span>`;
452
+ }
453
+ if (msg.role === 'toolResult') {
454
+ const toolCall = msg.toolCallId ? toolCallMap.get(msg.toolCallId) : null;
455
+ if (toolCall) {
456
+ return labelHtml + `<span class="tree-role-tool">${escapeHtml(formatToolCall(toolCall.name, toolCall.arguments))}</span>`;
457
+ }
458
+ return labelHtml + `<span class="tree-role-tool">[${msg.toolName || 'tool'}]</span>`;
459
+ }
460
+ if (msg.role === 'bashExecution') {
461
+ const cmd = truncate(normalize(msg.command || ''));
462
+ return labelHtml + `<span class="tree-role-tool">[bash]:</span> ${escapeHtml(cmd)}`;
463
+ }
464
+ return labelHtml + `<span class="tree-muted">[${msg.role}]</span>`;
465
+ }
466
+ case 'compaction':
467
+ return labelHtml + `<span class="tree-compaction">[compaction: ${Math.round(entry.tokensBefore/1000)}k tokens]</span>`;
468
+ case 'branch_summary': {
469
+ const summary = truncate(normalize(entry.summary || ''));
470
+ return labelHtml + `<span class="tree-branch-summary">[branch summary]:</span> ${escapeHtml(summary)}`;
471
+ }
472
+ case 'custom_message': {
473
+ const content = typeof entry.content === 'string' ? entry.content : extractContent(entry.content);
474
+ return labelHtml + `<span class="tree-custom">[${escapeHtml(entry.customType)}]:</span> ${escapeHtml(truncate(normalize(content)))}`;
475
+ }
476
+ case 'model_change':
477
+ return labelHtml + `<span class="tree-muted">[model: ${entry.modelId}]</span>`;
478
+ case 'thinking_level_change':
479
+ return labelHtml + `<span class="tree-muted">[thinking: ${entry.thinkingLevel}]</span>`;
480
+ default:
481
+ return labelHtml + `<span class="tree-muted">[${entry.type}]</span>`;
482
+ }
483
+ }
484
+
485
+ // ============================================================
486
+ // TREE RENDERING (DOM manipulation)
487
+ // ============================================================
488
+
489
+ let currentLeafId = leafId;
490
+ let treeRendered = false;
491
+
492
+ function renderTree() {
493
+ const tree = buildTree();
494
+ const activePathIds = buildActivePathIds(currentLeafId);
495
+ const flatNodes = flattenTree(tree, activePathIds);
496
+ const filtered = filterNodes(flatNodes, currentLeafId);
497
+ const container = document.getElementById('tree-container');
498
+
499
+ // Full render only on first call or when filter/search changes
500
+ if (!treeRendered) {
501
+ container.innerHTML = '';
502
+
503
+ for (const flatNode of filtered) {
504
+ const entry = flatNode.node.entry;
505
+ const isOnPath = activePathIds.has(entry.id);
506
+ const isLeaf = entry.id === currentLeafId;
507
+
508
+ const div = document.createElement('div');
509
+ div.className = 'tree-node';
510
+ if (isOnPath) div.classList.add('in-path');
511
+ if (isLeaf) div.classList.add('active');
512
+ div.dataset.id = entry.id;
513
+
514
+ const prefix = buildTreePrefix(flatNode);
515
+ const prefixSpan = document.createElement('span');
516
+ prefixSpan.className = 'tree-prefix';
517
+ prefixSpan.textContent = prefix;
518
+
519
+ const marker = document.createElement('span');
520
+ marker.className = 'tree-marker';
521
+ marker.textContent = isOnPath ? '•' : ' ';
522
+
523
+ const content = document.createElement('span');
524
+ content.className = 'tree-content';
525
+ content.innerHTML = getTreeNodeDisplayHtml(entry, flatNode.node.label);
526
+
527
+ div.appendChild(prefixSpan);
528
+ div.appendChild(marker);
529
+ div.appendChild(content);
530
+ div.addEventListener('click', () => navigateTo(entry.id));
531
+
532
+ container.appendChild(div);
533
+ }
534
+
535
+ treeRendered = true;
536
+ } else {
537
+ // Just update markers and classes
538
+ const nodes = container.querySelectorAll('.tree-node');
539
+ for (const node of nodes) {
540
+ const id = node.dataset.id;
541
+ const isOnPath = activePathIds.has(id);
542
+ const isLeaf = id === currentLeafId;
543
+
544
+ node.classList.toggle('in-path', isOnPath);
545
+ node.classList.toggle('active', isLeaf);
546
+
547
+ const marker = node.querySelector('.tree-marker');
548
+ if (marker) {
549
+ marker.textContent = isOnPath ? '•' : ' ';
550
+ }
551
+ }
552
+ }
553
+
554
+ document.getElementById('tree-status').textContent = `${filtered.length} / ${flatNodes.length} entries`;
555
+
556
+ // Scroll active node into view after layout
557
+ setTimeout(() => {
558
+ const activeNode = container.querySelector('.tree-node.active');
559
+ if (activeNode) {
560
+ activeNode.scrollIntoView({ block: 'nearest' });
561
+ }
562
+ }, 0);
563
+ }
564
+
565
+ function forceTreeRerender() {
566
+ treeRendered = false;
567
+ renderTree();
568
+ }
569
+
570
+ // ============================================================
571
+ // MESSAGE RENDERING
572
+ // ============================================================
573
+
574
+ function formatTokens(count) {
575
+ if (count < 1000) return count.toString();
576
+ if (count < 10000) return (count / 1000).toFixed(1) + 'k';
577
+ if (count < 1000000) return Math.round(count / 1000) + 'k';
578
+ return (count / 1000000).toFixed(1) + 'M';
579
+ }
580
+
581
+ function formatTimestamp(ts) {
582
+ if (!ts) return '';
583
+ const date = new Date(ts);
584
+ return date.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit' });
585
+ }
586
+
587
+ function replaceTabs(text) {
588
+ return text.replace(/\t/g, ' ');
589
+ }
590
+
591
+ function getLanguageFromPath(filePath) {
592
+ const ext = filePath.split('.').pop()?.toLowerCase();
593
+ const extToLang = {
594
+ ts: 'typescript', tsx: 'typescript', js: 'javascript', jsx: 'javascript',
595
+ py: 'python', rb: 'ruby', rs: 'rust', go: 'go', java: 'java',
596
+ c: 'c', cpp: 'cpp', h: 'c', hpp: 'cpp', cs: 'csharp',
597
+ php: 'php', sh: 'bash', bash: 'bash', zsh: 'bash',
598
+ sql: 'sql', html: 'html', css: 'css', scss: 'scss',
599
+ json: 'json', yaml: 'yaml', yml: 'yaml', xml: 'xml',
600
+ md: 'markdown', dockerfile: 'dockerfile'
601
+ };
602
+ return extToLang[ext];
603
+ }
604
+
605
+ function findToolResult(toolCallId) {
606
+ for (const entry of entries) {
607
+ if (entry.type === 'message' && entry.message.role === 'toolResult') {
608
+ if (entry.message.toolCallId === toolCallId) {
609
+ return entry.message;
610
+ }
611
+ }
612
+ }
613
+ return null;
614
+ }
615
+
616
+ function formatExpandableOutput(text, maxLines, lang) {
617
+ text = replaceTabs(text);
618
+ const lines = text.split('\n');
619
+ const displayLines = lines.slice(0, maxLines);
620
+ const remaining = lines.length - maxLines;
621
+
622
+ if (lang) {
623
+ let highlighted;
624
+ try {
625
+ highlighted = hljs.highlight(text, { language: lang }).value;
626
+ } catch {
627
+ highlighted = escapeHtml(text);
628
+ }
629
+
630
+ if (remaining > 0) {
631
+ const previewCode = displayLines.join('\n');
632
+ let previewHighlighted;
633
+ try {
634
+ previewHighlighted = hljs.highlight(previewCode, { language: lang }).value;
635
+ } catch {
636
+ previewHighlighted = escapeHtml(previewCode);
637
+ }
638
+
639
+ return `<div class="tool-output expandable" onclick="this.classList.toggle('expanded')">
640
+ <div class="output-preview"><pre><code class="hljs">${previewHighlighted}</code></pre>
641
+ <div class="expand-hint">... (${remaining} more lines)</div></div>
642
+ <div class="output-full"><pre><code class="hljs">${highlighted}</code></pre></div></div>`;
643
+ }
644
+
645
+ return `<div class="tool-output"><pre><code class="hljs">${highlighted}</code></pre></div>`;
646
+ }
647
+
648
+ // Plain text output
649
+ if (remaining > 0) {
650
+ let out = '<div class="tool-output expandable" onclick="this.classList.toggle(\'expanded\')">';
651
+ out += '<div class="output-preview">';
652
+ for (const line of displayLines) {
653
+ out += `<div>${escapeHtml(replaceTabs(line))}</div>`;
654
+ }
655
+ out += `<div class="expand-hint">... (${remaining} more lines)</div></div>`;
656
+ out += '<div class="output-full">';
657
+ for (const line of lines) {
658
+ out += `<div>${escapeHtml(replaceTabs(line))}</div>`;
659
+ }
660
+ out += '</div></div>';
661
+ return out;
662
+ }
663
+
664
+ let out = '<div class="tool-output">';
665
+ for (const line of displayLines) {
666
+ out += `<div>${escapeHtml(replaceTabs(line))}</div>`;
667
+ }
668
+ out += '</div>';
669
+ return out;
670
+ }
671
+
672
+ function renderToolCall(call) {
673
+ const result = findToolResult(call.id);
674
+ const isError = result?.isError || false;
675
+ const statusClass = result ? (isError ? 'error' : 'success') : 'pending';
676
+
677
+ const getResultText = () => {
678
+ if (!result) return '';
679
+ const textBlocks = result.content.filter(c => c.type === 'text');
680
+ return textBlocks.map(c => c.text).join('\n');
681
+ };
682
+
683
+ const getResultImages = () => {
684
+ if (!result) return [];
685
+ return result.content.filter(c => c.type === 'image');
686
+ };
687
+
688
+ const renderResultImages = () => {
689
+ const images = getResultImages();
690
+ if (images.length === 0) return '';
691
+ return '<div class="tool-images">' +
692
+ images.map(img => `<img src="data:${img.mimeType};base64,${img.data}" class="tool-image" />`).join('') +
693
+ '</div>';
694
+ };
695
+
696
+ let html = `<div class="tool-execution ${statusClass}">`;
697
+ const args = call.arguments || {};
698
+ const name = call.name;
699
+
700
+ switch (name) {
701
+ case 'bash': {
702
+ const command = args.command || '';
703
+ html += `<div class="tool-command">$ ${escapeHtml(command)}</div>`;
704
+ if (result) {
705
+ const output = getResultText().trim();
706
+ if (output) html += formatExpandableOutput(output, 5);
707
+ }
708
+ break;
709
+ }
710
+ case 'read': {
711
+ const filePath = args.file_path || args.path || '';
712
+ const offset = args.offset;
713
+ const limit = args.limit;
714
+ const lang = getLanguageFromPath(filePath);
715
+
716
+ let pathHtml = escapeHtml(shortenPath(filePath));
717
+ if (offset !== undefined || limit !== undefined) {
718
+ const startLine = offset ?? 1;
719
+ const endLine = limit !== undefined ? startLine + limit - 1 : '';
720
+ pathHtml += `<span class="line-numbers">:${startLine}${endLine ? '-' + endLine : ''}</span>`;
721
+ }
722
+
723
+ html += `<div class="tool-header"><span class="tool-name">read</span> <span class="tool-path">${pathHtml}</span></div>`;
724
+ if (result) {
725
+ html += renderResultImages();
726
+ const output = getResultText();
727
+ if (output) html += formatExpandableOutput(output, 10, lang);
728
+ }
729
+ break;
730
+ }
731
+ case 'write': {
732
+ const filePath = args.file_path || args.path || '';
733
+ const content = args.content || '';
734
+ const lines = content.split('\n');
735
+ const lang = getLanguageFromPath(filePath);
736
+
737
+ html += `<div class="tool-header"><span class="tool-name">write</span> <span class="tool-path">${escapeHtml(shortenPath(filePath))}</span>`;
738
+ if (lines.length > 10) html += ` <span class="line-count">(${lines.length} lines)</span>`;
739
+ html += '</div>';
740
+
741
+ if (content) html += formatExpandableOutput(content, 10, lang);
742
+ if (result) {
743
+ const output = getResultText().trim();
744
+ if (output) html += `<div class="tool-output"><div>${escapeHtml(output)}</div></div>`;
745
+ }
746
+ break;
747
+ }
748
+ case 'edit': {
749
+ const filePath = args.file_path || args.path || '';
750
+ html += `<div class="tool-header"><span class="tool-name">edit</span> <span class="tool-path">${escapeHtml(shortenPath(filePath))}</span></div>`;
751
+
752
+ if (result?.details?.diff) {
753
+ const diffLines = result.details.diff.split('\n');
754
+ html += '<div class="tool-diff">';
755
+ for (const line of diffLines) {
756
+ const cls = line.match(/^\+/) ? 'diff-added' : line.match(/^-/) ? 'diff-removed' : 'diff-context';
757
+ html += `<div class="${cls}">${escapeHtml(replaceTabs(line))}</div>`;
758
+ }
759
+ html += '</div>';
760
+ } else if (result) {
761
+ const output = getResultText().trim();
762
+ if (output) html += `<div class="tool-output"><pre>${escapeHtml(output)}</pre></div>`;
763
+ }
764
+ break;
765
+ }
766
+ default: {
767
+ html += `<div class="tool-header"><span class="tool-name">${escapeHtml(name)}</span></div>`;
768
+ html += `<div class="tool-output"><pre>${escapeHtml(JSON.stringify(args, null, 2))}</pre></div>`;
769
+ if (result) {
770
+ const output = getResultText();
771
+ if (output) html += formatExpandableOutput(output, 10);
772
+ }
773
+ }
774
+ }
775
+
776
+ html += '</div>';
777
+ return html;
778
+ }
779
+
780
+ function renderEntry(entry) {
781
+ const ts = formatTimestamp(entry.timestamp);
782
+ const tsHtml = ts ? `<div class="message-timestamp">${ts}</div>` : '';
783
+ const entryId = `entry-${entry.id}`;
784
+
785
+ if (entry.type === 'message') {
786
+ const msg = entry.message;
787
+
788
+ if (msg.role === 'user') {
789
+ let html = `<div class="user-message" id="${entryId}">${tsHtml}`;
790
+ const content = msg.content;
791
+
792
+ if (Array.isArray(content)) {
793
+ const images = content.filter(c => c.type === 'image');
794
+ if (images.length > 0) {
795
+ html += '<div class="message-images">';
796
+ for (const img of images) {
797
+ html += `<img src="data:${img.mimeType};base64,${img.data}" class="message-image" />`;
798
+ }
799
+ html += '</div>';
800
+ }
801
+ }
802
+
803
+ const text = typeof content === 'string' ? content :
804
+ content.filter(c => c.type === 'text').map(c => c.text).join('\n');
805
+ if (text.trim()) {
806
+ html += `<div class="markdown-content">${safeMarkedParse(text)}</div>`;
807
+ }
808
+ html += '</div>';
809
+ return html;
810
+ }
811
+
812
+ if (msg.role === 'assistant') {
813
+ let html = `<div class="assistant-message" id="${entryId}">${tsHtml}`;
814
+
815
+ for (const block of msg.content) {
816
+ if (block.type === 'text' && block.text.trim()) {
817
+ html += `<div class="assistant-text markdown-content">${safeMarkedParse(block.text)}</div>`;
818
+ } else if (block.type === 'thinking' && block.thinking.trim()) {
819
+ html += `<div class="thinking-block">
820
+ <div class="thinking-text">${escapeHtml(block.thinking)}</div>
821
+ <div class="thinking-collapsed">Thinking ...</div>
822
+ </div>`;
823
+ }
824
+ }
825
+
826
+ for (const block of msg.content) {
827
+ if (block.type === 'toolCall') {
828
+ html += renderToolCall(block);
829
+ }
830
+ }
831
+
832
+ if (msg.stopReason === 'aborted') {
833
+ html += '<div class="error-text">Aborted</div>';
834
+ } else if (msg.stopReason === 'error') {
835
+ html += `<div class="error-text">Error: ${escapeHtml(msg.errorMessage || 'Unknown error')}</div>`;
836
+ }
837
+
838
+ html += '</div>';
839
+ return html;
840
+ }
841
+
842
+ if (msg.role === 'bashExecution') {
843
+ const isError = msg.cancelled || (msg.exitCode !== 0 && msg.exitCode !== null);
844
+ let html = `<div class="tool-execution ${isError ? 'error' : 'success'}" id="${entryId}">${tsHtml}`;
845
+ html += `<div class="tool-command">$ ${escapeHtml(msg.command)}</div>`;
846
+ if (msg.output) html += formatExpandableOutput(msg.output, 10);
847
+ if (msg.cancelled) {
848
+ html += '<div style="color: var(--warning)">(cancelled)</div>';
849
+ } else if (msg.exitCode !== 0 && msg.exitCode !== null) {
850
+ html += `<div style="color: var(--error)">(exit ${msg.exitCode})</div>`;
851
+ }
852
+ html += '</div>';
853
+ return html;
854
+ }
855
+
856
+ if (msg.role === 'toolResult') return '';
857
+ }
858
+
859
+ if (entry.type === 'model_change') {
860
+ return `<div class="model-change" id="${entryId}">${tsHtml}Switched to model: <span class="model-name">${escapeHtml(entry.provider)}/${escapeHtml(entry.modelId)}</span></div>`;
861
+ }
862
+
863
+ if (entry.type === 'compaction') {
864
+ return `<div class="compaction" id="${entryId}" onclick="this.classList.toggle('expanded')">
865
+ <div class="compaction-label">[compaction]</div>
866
+ <div class="compaction-collapsed">Compacted from ${entry.tokensBefore.toLocaleString()} tokens</div>
867
+ <div class="compaction-content"><strong>Compacted from ${entry.tokensBefore.toLocaleString()} tokens</strong>\n\n${escapeHtml(entry.summary)}</div>
868
+ </div>`;
869
+ }
870
+
871
+ if (entry.type === 'branch_summary') {
872
+ return `<div class="branch-summary" id="${entryId}">${tsHtml}
873
+ <div class="branch-summary-header">Branch Summary</div>
874
+ <div class="markdown-content">${safeMarkedParse(entry.summary)}</div>
875
+ </div>`;
876
+ }
877
+
878
+ if (entry.type === 'custom_message' && entry.display) {
879
+ return `<div class="hook-message" id="${entryId}">${tsHtml}
880
+ <div class="hook-type">[${escapeHtml(entry.customType)}]</div>
881
+ <div class="markdown-content">${safeMarkedParse(typeof entry.content === 'string' ? entry.content : JSON.stringify(entry.content))}</div>
882
+ </div>`;
883
+ }
884
+
885
+ return '';
886
+ }
887
+
888
+ // ============================================================
889
+ // HEADER / STATS
890
+ // ============================================================
891
+
892
+ function computeStats(entryList) {
893
+ let userMessages = 0, assistantMessages = 0, toolResults = 0;
894
+ let customMessages = 0, compactions = 0, branchSummaries = 0, toolCalls = 0;
895
+ const tokens = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
896
+ const cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
897
+ const models = new Set();
898
+
899
+ for (const entry of entryList) {
900
+ if (entry.type === 'message') {
901
+ const msg = entry.message;
902
+ if (msg.role === 'user') userMessages++;
903
+ if (msg.role === 'assistant') {
904
+ assistantMessages++;
905
+ if (msg.model) models.add(msg.provider ? `${msg.provider}/${msg.model}` : msg.model);
906
+ if (msg.usage) {
907
+ tokens.input += msg.usage.input || 0;
908
+ tokens.output += msg.usage.output || 0;
909
+ tokens.cacheRead += msg.usage.cacheRead || 0;
910
+ tokens.cacheWrite += msg.usage.cacheWrite || 0;
911
+ if (msg.usage.cost) {
912
+ cost.input += msg.usage.cost.input || 0;
913
+ cost.output += msg.usage.cost.output || 0;
914
+ cost.cacheRead += msg.usage.cost.cacheRead || 0;
915
+ cost.cacheWrite += msg.usage.cost.cacheWrite || 0;
916
+ }
917
+ }
918
+ toolCalls += msg.content.filter(c => c.type === 'toolCall').length;
919
+ }
920
+ if (msg.role === 'toolResult') toolResults++;
921
+ } else if (entry.type === 'compaction') {
922
+ compactions++;
923
+ } else if (entry.type === 'branch_summary') {
924
+ branchSummaries++;
925
+ } else if (entry.type === 'custom_message') {
926
+ customMessages++;
927
+ }
928
+ }
929
+
930
+ return { userMessages, assistantMessages, toolResults, customMessages, compactions, branchSummaries, toolCalls, tokens, cost, models: Array.from(models) };
931
+ }
932
+
933
+ const globalStats = computeStats(entries);
934
+
935
+ function renderHeader() {
936
+ const totalCost = globalStats.cost.input + globalStats.cost.output + globalStats.cost.cacheRead + globalStats.cost.cacheWrite;
937
+
938
+ const tokenParts = [];
939
+ if (globalStats.tokens.input) tokenParts.push(`↑${formatTokens(globalStats.tokens.input)}`);
940
+ if (globalStats.tokens.output) tokenParts.push(`↓${formatTokens(globalStats.tokens.output)}`);
941
+ if (globalStats.tokens.cacheRead) tokenParts.push(`R${formatTokens(globalStats.tokens.cacheRead)}`);
942
+ if (globalStats.tokens.cacheWrite) tokenParts.push(`W${formatTokens(globalStats.tokens.cacheWrite)}`);
943
+
944
+ const msgParts = [];
945
+ if (globalStats.userMessages) msgParts.push(`${globalStats.userMessages} user`);
946
+ if (globalStats.assistantMessages) msgParts.push(`${globalStats.assistantMessages} assistant`);
947
+ if (globalStats.toolResults) msgParts.push(`${globalStats.toolResults} tool results`);
948
+ if (globalStats.customMessages) msgParts.push(`${globalStats.customMessages} custom`);
949
+ if (globalStats.compactions) msgParts.push(`${globalStats.compactions} compactions`);
950
+ if (globalStats.branchSummaries) msgParts.push(`${globalStats.branchSummaries} branch summaries`);
951
+
952
+ let html = `
953
+ <div class="header">
954
+ <h1>Session: ${escapeHtml(header?.id || 'unknown')}</h1>
955
+ <div class="help-bar">Ctrl+T toggle thinking · Ctrl+O toggle tools</div>
956
+ <div class="header-info">
957
+ <div class="info-item"><span class="info-label">Date:</span><span class="info-value">${header?.timestamp ? new Date(header.timestamp).toLocaleString() : 'unknown'}</span></div>
958
+ <div class="info-item"><span class="info-label">Models:</span><span class="info-value">${globalStats.models.join(', ') || 'unknown'}</span></div>
959
+ <div class="info-item"><span class="info-label">Messages:</span><span class="info-value">${msgParts.join(', ') || '0'}</span></div>
960
+ <div class="info-item"><span class="info-label">Tool Calls:</span><span class="info-value">${globalStats.toolCalls}</span></div>
961
+ <div class="info-item"><span class="info-label">Tokens:</span><span class="info-value">${tokenParts.join(' ') || '0'}</span></div>
962
+ <div class="info-item"><span class="info-label">Cost:</span><span class="info-value">$${totalCost.toFixed(3)}</span></div>
963
+ </div>
964
+ </div>`;
965
+
966
+ if (systemPrompt) {
967
+ html += `<div class="system-prompt">
968
+ <div class="system-prompt-header">System Prompt</div>
969
+ <div class="system-prompt-content">${escapeHtml(systemPrompt)}</div>
970
+ </div>`;
971
+ }
972
+
973
+ if (tools && tools.length > 0) {
974
+ html += `<div class="tools-list">
975
+ <div class="tools-header">Available Tools</div>
976
+ <div class="tools-content">
977
+ ${tools.map(t => `<div class="tool-item"><span class="tool-item-name">${escapeHtml(t.name)}</span> - <span class="tool-item-desc">${escapeHtml(t.description)}</span></div>`).join('')}
978
+ </div>
979
+ </div>`;
980
+ }
981
+
982
+ return html;
983
+ }
984
+
985
+ // ============================================================
986
+ // NAVIGATION
987
+ // ============================================================
988
+
989
+ // Cache for rendered entry DOM nodes
990
+ const entryCache = new Map();
991
+
992
+ function renderEntryToNode(entry) {
993
+ // Check cache first
994
+ if (entryCache.has(entry.id)) {
995
+ return entryCache.get(entry.id).cloneNode(true);
996
+ }
997
+
998
+ // Render to HTML string, then parse to node
999
+ const html = renderEntry(entry);
1000
+ if (!html) return null;
1001
+
1002
+ const template = document.createElement('template');
1003
+ template.innerHTML = html;
1004
+ const node = template.content.firstElementChild;
1005
+
1006
+ // Cache the node
1007
+ if (node) {
1008
+ entryCache.set(entry.id, node.cloneNode(true));
1009
+ }
1010
+ return node;
1011
+ }
1012
+
1013
+ function navigateTo(targetId, scrollMode = 'target') {
1014
+ currentLeafId = targetId;
1015
+ const path = getPath(targetId);
1016
+
1017
+ renderTree();
1018
+
1019
+ document.getElementById('header-container').innerHTML = renderHeader();
1020
+
1021
+ // Build messages using cached DOM nodes
1022
+ const messagesEl = document.getElementById('messages');
1023
+ const fragment = document.createDocumentFragment();
1024
+
1025
+ for (const entry of path) {
1026
+ const node = renderEntryToNode(entry);
1027
+ if (node) {
1028
+ fragment.appendChild(node);
1029
+ }
1030
+ }
1031
+
1032
+ messagesEl.innerHTML = '';
1033
+ messagesEl.appendChild(fragment);
1034
+
1035
+ // Use setTimeout(0) to ensure DOM is fully laid out before scrolling
1036
+ setTimeout(() => {
1037
+ const content = document.getElementById('content');
1038
+ if (scrollMode === 'bottom') {
1039
+ content.scrollTop = content.scrollHeight;
1040
+ } else if (scrollMode === 'target') {
1041
+ const targetEl = document.getElementById(`entry-${targetId}`);
1042
+ if (targetEl) {
1043
+ targetEl.scrollIntoView({ block: 'center' });
1044
+ }
1045
+ }
1046
+ }, 0);
1047
+ }
1048
+
1049
+ // ============================================================
1050
+ // INITIALIZATION
1051
+ // ============================================================
1052
+
1053
+ // Escape HTML tags in text (but not code blocks)
1054
+ function escapeHtmlTags(text) {
1055
+ return text.replace(/<(?=[a-zA-Z\/])/g, '&lt;');
1056
+ }
1057
+
1058
+ // Configure marked with syntax highlighting and HTML escaping for text
1059
+ marked.use({
1060
+ breaks: true,
1061
+ gfm: true,
1062
+ renderer: {
1063
+ // Code blocks: syntax highlight, no HTML escaping
1064
+ code(token) {
1065
+ const code = token.text;
1066
+ const lang = token.lang;
1067
+ let highlighted;
1068
+ if (lang && hljs.getLanguage(lang)) {
1069
+ try {
1070
+ highlighted = hljs.highlight(code, { language: lang }).value;
1071
+ } catch {
1072
+ highlighted = escapeHtml(code);
1073
+ }
1074
+ } else {
1075
+ // Auto-detect language if not specified
1076
+ try {
1077
+ highlighted = hljs.highlightAuto(code).value;
1078
+ } catch {
1079
+ highlighted = escapeHtml(code);
1080
+ }
1081
+ }
1082
+ return `<pre><code class="hljs">${highlighted}</code></pre>`;
1083
+ },
1084
+ // Text content: escape HTML tags
1085
+ text(token) {
1086
+ return escapeHtmlTags(escapeHtml(token.text));
1087
+ },
1088
+ // Inline code: escape HTML
1089
+ codespan(token) {
1090
+ return `<code>${escapeHtml(token.text)}</code>`;
1091
+ }
1092
+ }
1093
+ });
1094
+
1095
+ // Simple marked parse (escaping handled in renderers)
1096
+ function safeMarkedParse(text) {
1097
+ return marked.parse(text);
1098
+ }
1099
+
1100
+ // Search input
1101
+ const searchInput = document.getElementById('tree-search');
1102
+ searchInput.addEventListener('input', (e) => {
1103
+ searchQuery = e.target.value;
1104
+ forceTreeRerender();
1105
+ });
1106
+
1107
+ // Filter buttons
1108
+ document.querySelectorAll('.filter-btn').forEach(btn => {
1109
+ btn.addEventListener('click', () => {
1110
+ document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
1111
+ btn.classList.add('active');
1112
+ filterMode = btn.dataset.filter;
1113
+ forceTreeRerender();
1114
+ });
1115
+ });
1116
+
1117
+ // Sidebar toggle
1118
+ const sidebar = document.getElementById('sidebar');
1119
+ const overlay = document.getElementById('sidebar-overlay');
1120
+ const hamburger = document.getElementById('hamburger');
1121
+
1122
+ hamburger.addEventListener('click', () => {
1123
+ sidebar.classList.add('open');
1124
+ overlay.classList.add('open');
1125
+ hamburger.style.display = 'none';
1126
+ });
1127
+
1128
+ const closeSidebar = () => {
1129
+ sidebar.classList.remove('open');
1130
+ overlay.classList.remove('open');
1131
+ hamburger.style.display = '';
1132
+ };
1133
+
1134
+ overlay.addEventListener('click', closeSidebar);
1135
+ document.getElementById('sidebar-close').addEventListener('click', closeSidebar);
1136
+
1137
+ // Toggle states
1138
+ let thinkingExpanded = true;
1139
+ let toolOutputsExpanded = false;
1140
+
1141
+ const toggleThinking = () => {
1142
+ thinkingExpanded = !thinkingExpanded;
1143
+ document.querySelectorAll('.thinking-text').forEach(el => {
1144
+ el.style.display = thinkingExpanded ? '' : 'none';
1145
+ });
1146
+ document.querySelectorAll('.thinking-collapsed').forEach(el => {
1147
+ el.style.display = thinkingExpanded ? 'none' : 'block';
1148
+ });
1149
+ };
1150
+
1151
+ const toggleToolOutputs = () => {
1152
+ toolOutputsExpanded = !toolOutputsExpanded;
1153
+ document.querySelectorAll('.tool-output.expandable').forEach(el => {
1154
+ el.classList.toggle('expanded', toolOutputsExpanded);
1155
+ });
1156
+ document.querySelectorAll('.compaction').forEach(el => {
1157
+ el.classList.toggle('expanded', toolOutputsExpanded);
1158
+ });
1159
+ };
1160
+
1161
+ // Keyboard shortcuts
1162
+ document.addEventListener('keydown', (e) => {
1163
+ if (e.key === 'Escape') {
1164
+ searchInput.value = '';
1165
+ searchQuery = '';
1166
+ navigateTo(leafId, 'bottom');
1167
+ }
1168
+ if (e.ctrlKey && e.key === 't') {
1169
+ e.preventDefault();
1170
+ toggleThinking();
1171
+ }
1172
+ if (e.ctrlKey && e.key === 'o') {
1173
+ e.preventDefault();
1174
+ toggleToolOutputs();
1175
+ }
1176
+ });
1177
+
1178
+ // Initial render - don't scroll, stay at top
1179
+ if (leafId) {
1180
+ navigateTo(leafId, 'none');
1181
+ } else if (entries.length > 0) {
1182
+ // Fallback: use last entry if no leafId
1183
+ navigateTo(entries[entries.length - 1].id, 'none');
1184
+ }
1185
+ })();