|
| 1 | +import { Session } from "../../session" |
| 2 | +import { MessageV2 } from "../../session/message-v2" |
| 3 | +import { createMarkdownRenderer } from "./markdown" |
| 4 | +import { wrap } from "./wrap" |
| 5 | +import { fg, style, clear, cursor, write } from "./terminal" |
| 6 | + |
| 7 | +const PAD = " " |
| 8 | +const MAX_WIDTH = Math.min(100, (process.stdout.columns || 80) - 4) |
| 9 | + |
| 10 | +interface PanelState { |
| 11 | + view: "parent" | "child" |
| 12 | + childIndex: number |
| 13 | + children: string[] |
| 14 | + parentSessionID: string | null |
| 15 | +} |
| 16 | + |
| 17 | +let state: PanelState = { |
| 18 | + view: "parent", |
| 19 | + childIndex: 0, |
| 20 | + children: [], |
| 21 | + parentSessionID: null, |
| 22 | +} |
| 23 | + |
| 24 | +let navigationMode = false |
| 25 | + |
| 26 | +export function setParentSession(sessionID: string | null) { |
| 27 | + state.parentSessionID = sessionID |
| 28 | + state.children = [] |
| 29 | + state.childIndex = 0 |
| 30 | + state.view = "parent" |
| 31 | +} |
| 32 | + |
| 33 | +let loading = false |
| 34 | + |
| 35 | +async function loadChildSessions() { |
| 36 | + if (!state.parentSessionID) return |
| 37 | + if (loading) return |
| 38 | + |
| 39 | + loading = true |
| 40 | + try { |
| 41 | + const childSessions = await Session.children(state.parentSessionID) |
| 42 | + const fetched = childSessions.map((s) => s.id) |
| 43 | + |
| 44 | + const merged = [...state.children] |
| 45 | + for (const id of fetched) { |
| 46 | + if (!merged.includes(id)) merged.push(id) |
| 47 | + } |
| 48 | + |
| 49 | + const currentChild = state.children[state.childIndex] |
| 50 | + state.children = merged |
| 51 | + |
| 52 | + if (currentChild) { |
| 53 | + const newIndex = merged.indexOf(currentChild) |
| 54 | + state.childIndex = newIndex >= 0 ? newIndex : Math.max(0, Math.min(state.childIndex, merged.length - 1)) |
| 55 | + } |
| 56 | + |
| 57 | + const current = state.children[state.childIndex] |
| 58 | + if (state.view === "child" && !current) { |
| 59 | + state.view = "parent" |
| 60 | + state.childIndex = 0 |
| 61 | + } |
| 62 | + } catch { |
| 63 | + state.children = [] |
| 64 | + if (state.view === "child") { |
| 65 | + state.view = "parent" |
| 66 | + state.childIndex = 0 |
| 67 | + } |
| 68 | + } finally { |
| 69 | + loading = false |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export function addChild(sessionID: string) { |
| 74 | + if (state.children.includes(sessionID)) return |
| 75 | + state.children.push(sessionID) |
| 76 | + if (state.children.length > 50) { |
| 77 | + state.children.shift() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export function enterNavigationMode() { |
| 82 | + navigationMode = true |
| 83 | +} |
| 84 | + |
| 85 | +export function exitNavigationMode() { |
| 86 | + navigationMode = false |
| 87 | +} |
| 88 | + |
| 89 | +export function isInNavigationMode(): boolean { |
| 90 | + return navigationMode |
| 91 | +} |
| 92 | + |
| 93 | +export function navigate(direction: "left" | "right" | "up"): boolean { |
| 94 | + if (!direction) return false |
| 95 | + |
| 96 | + if (direction === "up") { |
| 97 | + state.view = "parent" |
| 98 | + return true |
| 99 | + } |
| 100 | + |
| 101 | + if (state.children.length === 0) { |
| 102 | + return false |
| 103 | + } |
| 104 | + |
| 105 | + const wasInParent = state.view === "parent" |
| 106 | + state.view = "child" |
| 107 | + |
| 108 | + if (!wasInParent) { |
| 109 | + if (direction === "right") { |
| 110 | + state.childIndex = (state.childIndex + 1) % state.children.length |
| 111 | + return true |
| 112 | + } |
| 113 | + if (direction === "left") { |
| 114 | + state.childIndex = (state.childIndex - 1 + state.children.length) % state.children.length |
| 115 | + return true |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return true |
| 120 | +} |
| 121 | + |
| 122 | +export function getCurrentSessionID(): string | null { |
| 123 | + if (state.view === "parent") return state.parentSessionID |
| 124 | + return state.children[state.childIndex] || null |
| 125 | +} |
| 126 | + |
| 127 | +function padLines(text: string): string { |
| 128 | + return text |
| 129 | + .split("\n") |
| 130 | + .map((line) => (line ? `${PAD}${line}` : line)) |
| 131 | + .join("\n") |
| 132 | +} |
| 133 | + |
| 134 | +async function renderSessionMessages(sessionID: string): Promise<string> { |
| 135 | + try { |
| 136 | + const messages = await Session.messages({ sessionID }) |
| 137 | + const md = createMarkdownRenderer() |
| 138 | + |
| 139 | + for (const msg of messages) { |
| 140 | + if (!msg.info) continue |
| 141 | + |
| 142 | + if (msg.info.role === "user") { |
| 143 | + const parts = msg.parts.filter((p) => p.type === "text") |
| 144 | + if (parts.length > 0) { |
| 145 | + const userText = parts.map((p) => p.text).join("\n") |
| 146 | + const rendered = md.render(userText) |
| 147 | + const wrapped = wrap(rendered, MAX_WIDTH) |
| 148 | + write(padLines(wrapped) + "\n") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (msg.info.role === "assistant") { |
| 153 | + const parts = msg.parts |
| 154 | + for (const part of parts) { |
| 155 | + if (part.type === "text" && part.text) { |
| 156 | + const rendered = md.render(part.text) |
| 157 | + const wrapped = wrap(rendered, MAX_WIDTH) |
| 158 | + write(padLines(wrapped) + "\n") |
| 159 | + } |
| 160 | + if (part.type === "tool" && part.tool && part.state) { |
| 161 | + const summary = String(part.state.input).slice(0, 50).replace(/\s+/g, " ") |
| 162 | + const icon = |
| 163 | + part.state.status === "completed" |
| 164 | + ? `${fg.green}✓${style.reset}` |
| 165 | + : part.state.status === "error" |
| 166 | + ? `${fg.red}✗${style.reset}` |
| 167 | + : `${fg.gray}◇${style.reset}` |
| 168 | + write(`${PAD}${icon} ${part.tool}${summary ? `: ${summary}` : ""}${style.reset}\n`) |
| 169 | + } |
| 170 | + if (part.type === "subtask") { |
| 171 | + write(`${PAD}${fg.cyan}Subtask: ${part.description}${style.reset}\n`) |
| 172 | + } |
| 173 | + if (part.type === "reasoning" && part.text) { |
| 174 | + const rendered = md.render(part.text) |
| 175 | + const wrapped = wrap(rendered, MAX_WIDTH) |
| 176 | + write(padLines(`${fg.gray}${wrapped}${style.reset}\n`)) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return "" |
| 182 | + } catch { |
| 183 | + return " (failed to load session)\n" |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +export async function renderPanel() { |
| 188 | + if (state.view === "parent") { |
| 189 | + return |
| 190 | + } |
| 191 | + |
| 192 | + if (state.children.length === 0) { |
| 193 | + write(`${fg.gray}No child sessions${style.reset}\n\n`) |
| 194 | + return |
| 195 | + } |
| 196 | + |
| 197 | + await loadChildSessions() |
| 198 | + |
| 199 | + const childID = getCurrentSessionID() |
| 200 | + if (!childID) { |
| 201 | + write(`${fg.gray}No child session selected${style.reset}\n\n`) |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + write(clear.screen) |
| 206 | + write(cursor.home) |
| 207 | + |
| 208 | + const title = |
| 209 | + state.children.length > 0 ? `${fg.brightCyan}[${state.childIndex + 1}/${state.children.length}]${style.reset}` : "" |
| 210 | + |
| 211 | + write(`${title} ${fg.gray}Child Session: ${childID.slice(0, 8)}${style.reset}\n`) |
| 212 | + const rule = `${style.dim}${"─".repeat(Math.min(process.stdout.columns || 80, 80))}${style.reset}\n` |
| 213 | + write(rule) |
| 214 | + |
| 215 | + const result = await renderSessionMessages(childID) |
| 216 | + if (result) { |
| 217 | + write(`${fg.gray}${result}${style.reset}`) |
| 218 | + } |
| 219 | + |
| 220 | + write("\n") |
| 221 | +} |
| 222 | + |
| 223 | +export function getHint(): string { |
| 224 | + if (state.view === "parent") { |
| 225 | + return "" |
| 226 | + } |
| 227 | + |
| 228 | + const hasChildren = state.children.length > 0 |
| 229 | + const prevNext = hasChildren ? `Ctrl+X h (prev) Ctrl+X l (next)` : "" |
| 230 | + const parent = "Ctrl+X k (back to parent)" |
| 231 | + |
| 232 | + const parts = [prevNext, parent].filter(Boolean) |
| 233 | + return parts.length > 0 ? `${fg.gray}${parts.join(" · ")}${style.reset}` : "" |
| 234 | +} |
| 235 | + |
| 236 | +export function reset() { |
| 237 | + state = { |
| 238 | + view: "parent", |
| 239 | + childIndex: 0, |
| 240 | + children: [], |
| 241 | + parentSessionID: null, |
| 242 | + } |
| 243 | + navigationMode = false |
| 244 | + loading = false |
| 245 | +} |
| 246 | + |
| 247 | +export function hasChildren(): boolean { |
| 248 | + return state.children.length > 0 |
| 249 | +} |
0 commit comments