@georgewuw/pi-chat-navi 0.1.0 → 0.1.1
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 +16 -16
- package/LICENSE +23 -23
- package/README.md +92 -92
- package/package.json +1 -1
- package/src/index.ts +149 -149
- package/src/jump.ts +229 -229
- package/src/logic.ts +329 -329
- package/src/panel.ts +534 -534
package/src/panel.ts
CHANGED
|
@@ -1,534 +1,534 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* panel.ts
|
|
3
|
-
*
|
|
4
|
-
* 「用户提问列表」面板组件(列表视图 + 回合分析视图)。只负责状态、键鼠和渲染,
|
|
5
|
-
* 不碰会话数据和 transcript:数据由 index.ts 传入,跳转通过回调完成。
|
|
6
|
-
* - 键盘:↑↓ 选择、回车跳转(作用于当前选中项)、→/d 回合分析、Esc 关闭(分析内先返回列表)
|
|
7
|
-
* - 鼠标:仅回合分析详情区的滚轮;单击/双击/悬停均无效果
|
|
8
|
-
*/
|
|
9
|
-
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
10
|
-
import { getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
11
|
-
import {
|
|
12
|
-
Markdown,
|
|
13
|
-
type Component,
|
|
14
|
-
type TUI,
|
|
15
|
-
type TuiMouseEvent,
|
|
16
|
-
type TuiMouseEventResult,
|
|
17
|
-
matchesKey,
|
|
18
|
-
truncateToWidth,
|
|
19
|
-
visibleWidth,
|
|
20
|
-
wrapTextWithAnsi,
|
|
21
|
-
} from "@earendil-works/pi-tui";
|
|
22
|
-
import { firstLine, type QuestionItem, type TurnData, type TurnNode } from "./logic.ts";
|
|
23
|
-
|
|
24
|
-
/** 工具参数摘要(单行、限宽) */
|
|
25
|
-
function briefArgs(v: unknown, max = 36): string {
|
|
26
|
-
let s: string;
|
|
27
|
-
try {
|
|
28
|
-
s = typeof v === "string" ? v : (JSON.stringify(v) ?? "null");
|
|
29
|
-
} catch {
|
|
30
|
-
s = String(v);
|
|
31
|
-
}
|
|
32
|
-
s = s.replace(/\s+/g, " ").trim();
|
|
33
|
-
return s ? truncateToWidth(s, max, "…") : "{}";
|
|
34
|
-
}
|
|
35
|
-
// ---------------------------------------------------------------------------
|
|
36
|
-
// 面板组件
|
|
37
|
-
// ---------------------------------------------------------------------------
|
|
38
|
-
|
|
39
|
-
const MAX_VISIBLE_ROWS = 18;
|
|
40
|
-
/** 面板最大高度占终端行数的比例(与 overlayOptions.maxHeight 保持一致) */
|
|
41
|
-
export const PANEL_HEIGHT_RATIO = 0.7;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 重线框绘制:列表视图与回合分析视图共用。
|
|
45
|
-
* 与 pi-session-nav 一致:border 颜色、内容两侧各留 1 格。
|
|
46
|
-
*/
|
|
47
|
-
class Frame {
|
|
48
|
-
readonly innerW: number;
|
|
49
|
-
/** 可用内容宽度(已扣除两侧边框与留白) */
|
|
50
|
-
readonly contentW: number;
|
|
51
|
-
|
|
52
|
-
constructor(
|
|
53
|
-
private readonly theme: Theme,
|
|
54
|
-
width: number,
|
|
55
|
-
) {
|
|
56
|
-
this.innerW = Math.max(1, width - 2);
|
|
57
|
-
this.contentW = Math.max(1, this.innerW - 2);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
border(text: string): string {
|
|
61
|
-
return this.theme.fg("border", text);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** 一行内容:截断并补齐到内容宽度;active 时整行高亮;rightEdge 可替换右边框(详情区用作滚动条) */
|
|
65
|
-
row(body: string, opts: { active?: boolean; rightEdge?: string } = {}): string {
|
|
66
|
-
const fitted = truncateToWidth(body, this.contentW, "…");
|
|
67
|
-
const inner = ` ${fitted}${" ".repeat(Math.max(0, this.contentW - visibleWidth(fitted)))} `;
|
|
68
|
-
return (
|
|
69
|
-
this.border("┃") +
|
|
70
|
-
(opts.active ? this.theme.style(inner, { fg: "accent", bold: true }) : inner) +
|
|
71
|
-
(opts.rightEdge ?? this.border("┃"))
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
top(title: string): string {
|
|
76
|
-
const fitted = truncateToWidth(title, Math.max(1, this.innerW - 1), "…");
|
|
77
|
-
return this.border(`┏${fitted}${"━".repeat(Math.max(0, this.innerW - visibleWidth(fitted)))}┓`);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
separator(): string {
|
|
81
|
-
return this.border("┣" + "━".repeat(this.innerW) + "┫");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
bottom(): string {
|
|
85
|
-
return this.border("┗" + "━".repeat(this.innerW) + "┛");
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** 左右两端对齐的标题行;放不下时退化为截断的单段 */
|
|
89
|
-
header(left: string, right: string): string {
|
|
90
|
-
if (!right) return left;
|
|
91
|
-
const gap = this.contentW - visibleWidth(left) - visibleWidth(right);
|
|
92
|
-
return gap >= 1 ? left + " ".repeat(gap) + right : truncateToWidth(`${left} ${right}`, this.contentW, "…");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export class QuestionPanelComponent implements Component {
|
|
97
|
-
private readonly tui: TUI;
|
|
98
|
-
private readonly theme: Theme;
|
|
99
|
-
private readonly items: QuestionItem[];
|
|
100
|
-
private readonly jump: (index: number) => void;
|
|
101
|
-
private readonly onClose: () => void;
|
|
102
|
-
|
|
103
|
-
private readonly turnLoader: (ordinal: number) => TurnData | null;
|
|
104
|
-
|
|
105
|
-
private selected = 0;
|
|
106
|
-
/** 列表滚动偏移(条目数) */
|
|
107
|
-
private offset = 0;
|
|
108
|
-
private version = 0;
|
|
109
|
-
|
|
110
|
-
// 回合分析视图状态
|
|
111
|
-
private view: "list" | "turn" = "list";
|
|
112
|
-
private turn: TurnData | null = null;
|
|
113
|
-
/** 仅显示思维链 */
|
|
114
|
-
private chainOnly = false;
|
|
115
|
-
/** 选中节点在 visibleNodes 中的序号 */
|
|
116
|
-
private nodeSel = 0;
|
|
117
|
-
private treeOffset = 0;
|
|
118
|
-
private detailOffset = 0;
|
|
119
|
-
/** 上次渲染的详情总行数(用于滚轮 clamp) */
|
|
120
|
-
private detailLen = 0;
|
|
121
|
-
|
|
122
|
-
/** 详情内容行范围(含端点) */
|
|
123
|
-
private detailTop = -1;
|
|
124
|
-
private detailBottom = -1;
|
|
125
|
-
/** Markdown 组件缓存(输出节点详情) */
|
|
126
|
-
private mdCache: { key: string; theme: unknown; md: Markdown } | null = null;
|
|
127
|
-
|
|
128
|
-
// 渲染缓存
|
|
129
|
-
private cachedLines: string[] = [];
|
|
130
|
-
private cachedWidth = 0;
|
|
131
|
-
private cachedRows = -1;
|
|
132
|
-
private cachedVersion = -1;
|
|
133
|
-
|
|
134
|
-
constructor(
|
|
135
|
-
tui: TUI,
|
|
136
|
-
theme: Theme,
|
|
137
|
-
items: QuestionItem[],
|
|
138
|
-
jump: (index: number) => void,
|
|
139
|
-
turnLoader: (ordinal: number) => TurnData | null,
|
|
140
|
-
onClose: () => void,
|
|
141
|
-
) {
|
|
142
|
-
this.tui = tui;
|
|
143
|
-
this.theme = theme;
|
|
144
|
-
this.items = items;
|
|
145
|
-
this.jump = jump;
|
|
146
|
-
this.turnLoader = turnLoader;
|
|
147
|
-
this.onClose = onClose;
|
|
148
|
-
this.selected = items.length - 1;
|
|
149
|
-
this.ensureSelectedVisible();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
close(): void {
|
|
153
|
-
this.onClose();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
invalidate(): void {
|
|
157
|
-
this.cachedWidth = 0;
|
|
158
|
-
this.cachedRows = -1;
|
|
159
|
-
this.mdCache = null;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
private bump(): void {
|
|
163
|
-
this.version++;
|
|
164
|
-
this.tui.requestRender();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
private visibleCount(): number {
|
|
168
|
-
// 列表固定行:顶/底边框、上下空行、提示行(5)+ 上/下 more 提示(≤2)
|
|
169
|
-
const terminalRows = this.tui.terminal?.rows ?? 40;
|
|
170
|
-
const budget = Math.max(3, Math.floor(terminalRows * PANEL_HEIGHT_RATIO) - 7);
|
|
171
|
-
return Math.min(this.items.length, MAX_VISIBLE_ROWS, budget);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
private ensureSelectedVisible(): void {
|
|
175
|
-
const count = this.visibleCount();
|
|
176
|
-
if (this.selected < this.offset) this.offset = this.selected;
|
|
177
|
-
if (this.selected > this.offset + count - 1) this.offset = this.selected - count + 1;
|
|
178
|
-
// 终端被放大(可见行数变多)后,offset 不能让列表尾部留空
|
|
179
|
-
this.offset = Math.max(0, Math.min(Math.max(0, this.items.length - count), this.offset));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
private jumpTo(index: number): void {
|
|
183
|
-
this.selected = index;
|
|
184
|
-
this.jump(index);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** 当前树视图可见节点(chainOnly 时仅思考) */
|
|
188
|
-
private visibleNodes(): TurnNode[] {
|
|
189
|
-
if (!this.turn) return [];
|
|
190
|
-
return this.chainOnly ? this.turn.nodes.filter((n) => n.kind === "thinking") : this.turn.nodes;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* 回合分析可用行数:按终端高度分配节点区/详情区。
|
|
195
|
-
* 固定行:顶/底边框、提问行、3 条分隔线、2 个区块标题、提示行(共 9 行)。
|
|
196
|
-
*/
|
|
197
|
-
private turnLayout(): { treeRows: number; detailRows: number } {
|
|
198
|
-
const terminalRows = this.tui.terminal?.rows ?? 40;
|
|
199
|
-
const panelRows = Math.max(12, Math.floor(terminalRows * PANEL_HEIGHT_RATIO));
|
|
200
|
-
const contentRows = Math.max(4, panelRows - 9);
|
|
201
|
-
const treeRows = Math.max(2, Math.min(8, Math.round(contentRows * 0.4)));
|
|
202
|
-
return { treeRows, detailRows: Math.max(2, contentRows - treeRows) };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
private ensureNodeVisible(): void {
|
|
206
|
-
const total = this.visibleNodes().length;
|
|
207
|
-
const view = Math.min(total, this.turnLayout().treeRows);
|
|
208
|
-
if (this.nodeSel < this.treeOffset) this.treeOffset = this.nodeSel;
|
|
209
|
-
if (this.nodeSel > this.treeOffset + view - 1) this.treeOffset = this.nodeSel - view + 1;
|
|
210
|
-
this.treeOffset = Math.max(0, Math.min(Math.max(0, total - view), this.treeOffset));
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private selectNode(pos: number): void {
|
|
214
|
-
const total = this.visibleNodes().length;
|
|
215
|
-
if (total === 0) return;
|
|
216
|
-
this.nodeSel = Math.max(0, Math.min(total - 1, pos));
|
|
217
|
-
this.detailOffset = 0;
|
|
218
|
-
this.ensureNodeVisible();
|
|
219
|
-
this.bump();
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/** 列表 d/→:进入该提问的回合分析 */
|
|
223
|
-
private enterTurn(index: number): void {
|
|
224
|
-
const turn = this.turnLoader(this.items[index]!.index);
|
|
225
|
-
if (!turn) return;
|
|
226
|
-
this.selected = index;
|
|
227
|
-
this.turn = turn;
|
|
228
|
-
this.view = "turn";
|
|
229
|
-
this.chainOnly = false;
|
|
230
|
-
this.nodeSel = 0;
|
|
231
|
-
this.treeOffset = 0;
|
|
232
|
-
this.detailOffset = 0;
|
|
233
|
-
this.bump();
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
private backToList(): void {
|
|
237
|
-
this.view = "list";
|
|
238
|
-
this.turn = null;
|
|
239
|
-
this.mdCache = null;
|
|
240
|
-
this.bump();
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
handleInput(data: string): boolean {
|
|
244
|
-
if (this.view === "turn") {
|
|
245
|
-
if (matchesKey(data, "escape") || matchesKey(data, "left")) {
|
|
246
|
-
this.backToList();
|
|
247
|
-
return true;
|
|
248
|
-
}
|
|
249
|
-
if (matchesKey(data, "up")) {
|
|
250
|
-
this.selectNode(this.nodeSel - 1);
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
if (matchesKey(data, "down")) {
|
|
254
|
-
this.selectNode(this.nodeSel + 1);
|
|
255
|
-
return true;
|
|
256
|
-
}
|
|
257
|
-
if (data === "t" || data === "T") {
|
|
258
|
-
this.chainOnly = !this.chainOnly;
|
|
259
|
-
this.nodeSel = 0;
|
|
260
|
-
this.treeOffset = 0;
|
|
261
|
-
this.detailOffset = 0;
|
|
262
|
-
this.bump();
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
if (matchesKey(data, "return")) {
|
|
266
|
-
// 回车:定位到该提问在 transcript 的位置
|
|
267
|
-
this.jump(this.turn!.question.index);
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
return true;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// 列表视图
|
|
274
|
-
if (matchesKey(data, "escape")) {
|
|
275
|
-
this.close();
|
|
276
|
-
return true;
|
|
277
|
-
}
|
|
278
|
-
if (matchesKey(data, "up")) {
|
|
279
|
-
if (this.selected > 0) {
|
|
280
|
-
this.selected--;
|
|
281
|
-
this.ensureSelectedVisible();
|
|
282
|
-
this.bump();
|
|
283
|
-
}
|
|
284
|
-
return true;
|
|
285
|
-
}
|
|
286
|
-
if (matchesKey(data, "down")) {
|
|
287
|
-
if (this.selected < this.items.length - 1) {
|
|
288
|
-
this.selected++;
|
|
289
|
-
this.ensureSelectedVisible();
|
|
290
|
-
this.bump();
|
|
291
|
-
}
|
|
292
|
-
return true;
|
|
293
|
-
}
|
|
294
|
-
if (matchesKey(data, "return") || data === " ") {
|
|
295
|
-
// 回车/空格:跳转到当前选中项
|
|
296
|
-
this.jumpTo(this.selected);
|
|
297
|
-
return true;
|
|
298
|
-
}
|
|
299
|
-
if (data === "d" || data === "D" || matchesKey(data, "right")) {
|
|
300
|
-
this.enterTurn(this.selected);
|
|
301
|
-
return true;
|
|
302
|
-
}
|
|
303
|
-
return true;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
308
|
-
if (event.type === "wheel" && this.view === "turn") {
|
|
309
|
-
// 仅保留回合分析详情区的滚轮滚动(列表与节点树不响应滚轮)
|
|
310
|
-
if (event.y >= this.detailTop && event.y <= this.detailBottom) {
|
|
311
|
-
const step = Math.max(-3, Math.min(3, event.wheelDelta ?? -1));
|
|
312
|
-
const maxOffset = Math.max(0, this.detailLen - this.turnLayout().detailRows);
|
|
313
|
-
const next = Math.max(0, Math.min(maxOffset, this.detailOffset + step));
|
|
314
|
-
if (next !== this.detailOffset) {
|
|
315
|
-
this.detailOffset = next;
|
|
316
|
-
this.bump();
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
return { handled: true, render: true };
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return { handled: true, render: false };
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
render(width: number): string[] {
|
|
326
|
-
// 终端高度变化也会改变可见行数,必须纳入缓存键
|
|
327
|
-
const rows = this.tui.terminal?.rows ?? 0;
|
|
328
|
-
if (width === this.cachedWidth && rows === this.cachedRows && this.cachedVersion === this.version) {
|
|
329
|
-
return this.cachedLines;
|
|
330
|
-
}
|
|
331
|
-
// 渲染前先让滚动偏移适应当前可见行数(终端放大后不留空行/不隐藏前面的条目)
|
|
332
|
-
if (this.view === "turn") this.ensureNodeVisible();
|
|
333
|
-
else this.ensureSelectedVisible();
|
|
334
|
-
const lines = this.view === "turn" ? this.renderTurn(width) : this.renderList(width);
|
|
335
|
-
this.cachedLines = lines;
|
|
336
|
-
this.cachedWidth = width;
|
|
337
|
-
this.cachedRows = rows;
|
|
338
|
-
this.cachedVersion = this.version;
|
|
339
|
-
return lines;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
private renderList(width: number): string[] {
|
|
343
|
-
const theme = this.theme;
|
|
344
|
-
const f = new Frame(theme, width);
|
|
345
|
-
const row = (body: string): string => f.row(body);
|
|
346
|
-
const lines: string[] = [f.top(` Navigate conversation · ${this.items.length} messages `), row("")];
|
|
347
|
-
|
|
348
|
-
// 列表上方的省略提示行(仅展示,不可选中)
|
|
349
|
-
const count = this.visibleCount();
|
|
350
|
-
if (this.offset > 0) lines.push(row(theme.fg("dim", " ↑ more")));
|
|
351
|
-
|
|
352
|
-
// 条目行
|
|
353
|
-
for (let rowIdx = 0; rowIdx < count; rowIdx++) {
|
|
354
|
-
const idx = this.offset + rowIdx;
|
|
355
|
-
if (idx >= this.items.length) break;
|
|
356
|
-
const item = this.items[idx]!;
|
|
357
|
-
const active = this.selected === idx;
|
|
358
|
-
const marker = active ? "> " : " ";
|
|
359
|
-
const num = String(idx + 1).padStart(2, " ");
|
|
360
|
-
lines.push(f.row(`${marker}${num} ${item.summary}`, { active }));
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// 列表下方的省略提示行(仅展示,不可选中)
|
|
364
|
-
if (this.offset + count < this.items.length) lines.push(row(theme.fg("dim", " ↓ more")));
|
|
365
|
-
|
|
366
|
-
// 底部:空行 + dim 提示 + 底边框(与 pi-session-nav 一致)
|
|
367
|
-
lines.push(row(""));
|
|
368
|
-
const hint = `↑↓ select · Enter jump · →/d analyze · Esc close`;
|
|
369
|
-
lines.push(row(theme.fg("dim", hint)));
|
|
370
|
-
lines.push(f.bottom());
|
|
371
|
-
|
|
372
|
-
return lines;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
// -----------------------------------------------------------------------
|
|
376
|
-
// 回合分析视图
|
|
377
|
-
// -----------------------------------------------------------------------
|
|
378
|
-
|
|
379
|
-
private renderTurn(width: number): string[] {
|
|
380
|
-
const theme = this.theme;
|
|
381
|
-
const turn = this.turn!;
|
|
382
|
-
const f = new Frame(theme, width);
|
|
383
|
-
const contentW = f.contentW;
|
|
384
|
-
const row = (body: string): string => f.row(body);
|
|
385
|
-
const separator = (): string => f.separator();
|
|
386
|
-
const header = (left: string, right: string): string => f.header(left, right);
|
|
387
|
-
const { treeRows, detailRows } = this.turnLayout();
|
|
388
|
-
|
|
389
|
-
const lines: string[] = [f.top(` Analysis · Q${turn.question.index + 1} `)];
|
|
390
|
-
|
|
391
|
-
// 提问行
|
|
392
|
-
lines.push(row(`${theme.fg("accent", "Question:")} ${turn.question.summary}`));
|
|
393
|
-
lines.push(separator());
|
|
394
|
-
|
|
395
|
-
// 节点区:标题行(左:选中序号/总数,右:可视范围)+ 固定行数的节点列表
|
|
396
|
-
const nodes = this.visibleNodes();
|
|
397
|
-
const view = Math.min(nodes.length, treeRows);
|
|
398
|
-
const selectedLabel = nodes.length === 0 ? "0/0" : `${this.nodeSel + 1}/${nodes.length}`;
|
|
399
|
-
const nodeRange =
|
|
400
|
-
nodes.length > view
|
|
401
|
-
? `${this.treeOffset + 1}-${Math.min(nodes.length, this.treeOffset + view)}/${nodes.length}`
|
|
402
|
-
: "";
|
|
403
|
-
const nodeHeader = `${theme.fg("accent", "Nodes")}${theme.fg("dim", ` · ${selectedLabel}${this.chainOnly ? " · thinking only" : ""}`)}`;
|
|
404
|
-
lines.push(row(header(nodeHeader, nodeRange ? theme.fg("dim", nodeRange) : "")));
|
|
405
|
-
|
|
406
|
-
for (let r = 0; r < treeRows; r++) {
|
|
407
|
-
const pos = this.treeOffset + r;
|
|
408
|
-
const node = pos < nodes.length ? nodes[pos] : undefined;
|
|
409
|
-
if (!node) {
|
|
410
|
-
lines.push(row(r === 0 && nodes.length === 0 ? theme.fg("dim", "(no nodes)") : ""));
|
|
411
|
-
continue;
|
|
412
|
-
}
|
|
413
|
-
const active = pos === this.nodeSel;
|
|
414
|
-
let label: string;
|
|
415
|
-
let summary: string;
|
|
416
|
-
switch (node.kind) {
|
|
417
|
-
case "thinking":
|
|
418
|
-
label = "Thinking";
|
|
419
|
-
summary = firstLine(node.text) || "(Empty)";
|
|
420
|
-
break;
|
|
421
|
-
case "text":
|
|
422
|
-
label = node.isFinal ? "Output" : "Text";
|
|
423
|
-
summary = firstLine(node.text);
|
|
424
|
-
break;
|
|
425
|
-
case "tool": {
|
|
426
|
-
label = "Tool";
|
|
427
|
-
const status =
|
|
428
|
-
node.result === null ? "·no result" : node.isError ? "·error" : `·${node.result.split("\n").length} lines`;
|
|
429
|
-
summary = `${node.name} ${briefArgs(node.args)} ${status}`;
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
const marker = active ? "› " : " ";
|
|
434
|
-
const body = `${marker}${label.padEnd(9)}${summary}`;
|
|
435
|
-
lines.push(f.row(body, { active }));
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
lines.push(separator());
|
|
439
|
-
|
|
440
|
-
// 详情区:标题行(左:类型,右:可视范围)+ 内容(右边框列兼作滚动条)
|
|
441
|
-
const selNode = nodes[this.nodeSel];
|
|
442
|
-
const kindLabel = selNode
|
|
443
|
-
? selNode.kind === "thinking"
|
|
444
|
-
? "Thinking"
|
|
445
|
-
: selNode.kind === "tool"
|
|
446
|
-
? `Tool ${selNode.name}${selNode.isError ? " (error)" : ""}`
|
|
447
|
-
: selNode.isFinal
|
|
448
|
-
? "Output (Markdown)"
|
|
449
|
-
: "Text"
|
|
450
|
-
: "(no node)";
|
|
451
|
-
const detail = selNode ? this.detailLines(selNode, contentW) : ["(no node)"];
|
|
452
|
-
this.detailLen = detail.length;
|
|
453
|
-
const maxDetailOffset = Math.max(0, detail.length - detailRows);
|
|
454
|
-
const start = Math.max(0, Math.min(this.detailOffset, maxDetailOffset));
|
|
455
|
-
const scrollable = detail.length > detailRows;
|
|
456
|
-
const detailRange = scrollable
|
|
457
|
-
? `${start + 1}-${Math.min(detail.length, start + detailRows)}/${detail.length}`
|
|
458
|
-
: "";
|
|
459
|
-
const detailHeader = `${theme.fg("accent", "Detail")}${theme.fg("dim", ` · ${kindLabel}`)}`;
|
|
460
|
-
lines.push(row(header(detailHeader, detailRange ? theme.fg("dim", detailRange) : "")));
|
|
461
|
-
|
|
462
|
-
const thumbSize = scrollable
|
|
463
|
-
? Math.max(1, Math.min(detailRows, Math.round((detailRows * detailRows) / detail.length)))
|
|
464
|
-
: 0;
|
|
465
|
-
const thumbTop =
|
|
466
|
-
scrollable && maxDetailOffset > 0 ? Math.round((start / maxDetailOffset) * (detailRows - thumbSize)) : 0;
|
|
467
|
-
this.detailTop = lines.length;
|
|
468
|
-
for (let r = 0; r < detailRows; r++) {
|
|
469
|
-
const idx = start + r;
|
|
470
|
-
const text = idx < detail.length ? detail[idx]! : "";
|
|
471
|
-
const rightEdge = !scrollable
|
|
472
|
-
? undefined
|
|
473
|
-
: r >= thumbTop && r < thumbTop + thumbSize
|
|
474
|
-
? theme.fg("borderAccent", "█")
|
|
475
|
-
: theme.fg("borderMuted", "│");
|
|
476
|
-
lines.push(f.row(text, { rightEdge }));
|
|
477
|
-
}
|
|
478
|
-
this.detailBottom = lines.length - 1;
|
|
479
|
-
|
|
480
|
-
lines.push(separator());
|
|
481
|
-
const hint = `↑↓ select · ←/Esc back · t ${this.chainOnly ? "thinking-only" : "all"} · wheel detail`;
|
|
482
|
-
lines.push(row(theme.fg("dim", hint)));
|
|
483
|
-
lines.push(f.bottom());
|
|
484
|
-
|
|
485
|
-
return lines;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
/** 节点详情行(已按宽度 wrap,可直接进详情区) */
|
|
489
|
-
private detailLines(node: TurnNode, dw: number): string[] {
|
|
490
|
-
const theme = this.theme;
|
|
491
|
-
const wrap = (text: string): string[] => {
|
|
492
|
-
const out: string[] = [];
|
|
493
|
-
for (const line of text.split("\n")) {
|
|
494
|
-
const wrapped = wrapTextWithAnsi(line, dw);
|
|
495
|
-
out.push(...(wrapped.length > 0 ? wrapped : [""]));
|
|
496
|
-
}
|
|
497
|
-
return out.length > 0 ? out : ["(Empty)"];
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
if (node.kind === "thinking") return wrap(node.text);
|
|
501
|
-
|
|
502
|
-
if (node.kind === "text") {
|
|
503
|
-
if (!node.isFinal) return wrap(node.text);
|
|
504
|
-
// 最终输出:Markdown 渲染(按节点+宽度缓存,主题变化时重建;
|
|
505
|
-
// 主题未初始化等异常时退回纯文本 wrap)
|
|
506
|
-
try {
|
|
507
|
-
const mdTheme = getMarkdownTheme();
|
|
508
|
-
const key = `${node.id}:${dw}`;
|
|
509
|
-
if (!this.mdCache || this.mdCache.key !== key || this.mdCache.theme !== mdTheme) {
|
|
510
|
-
this.mdCache = { key, theme: mdTheme, md: new Markdown(node.text, 0, 0, mdTheme) };
|
|
511
|
-
}
|
|
512
|
-
return [...this.mdCache.md.render(dw)];
|
|
513
|
-
} catch {
|
|
514
|
-
return wrap(node.text);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
// 工具节点:参数 + 结果(名称/错误状态已在详情标题里)
|
|
519
|
-
const head: string[] = [theme.fg("dim", "Args:")];
|
|
520
|
-
let argsLines: string[];
|
|
521
|
-
try {
|
|
522
|
-
const s = typeof node.args === "string" ? node.args : (JSON.stringify(node.args, null, 2) ?? String(node.args));
|
|
523
|
-
argsLines = wrap(s);
|
|
524
|
-
} catch {
|
|
525
|
-
argsLines = wrap(String(node.args));
|
|
526
|
-
}
|
|
527
|
-
const resHead =
|
|
528
|
-
node.result === null
|
|
529
|
-
? ["", theme.fg("dim", "Result: (not returned)")]
|
|
530
|
-
: ["", theme.fg("dim", `Result${node.isError ? " (error)" : ""}:`)];
|
|
531
|
-
const resLines = node.result === null ? [] : wrap(node.result);
|
|
532
|
-
return [...head, ...argsLines, ...resHead, ...resLines];
|
|
533
|
-
}
|
|
534
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* panel.ts
|
|
3
|
+
*
|
|
4
|
+
* 「用户提问列表」面板组件(列表视图 + 回合分析视图)。只负责状态、键鼠和渲染,
|
|
5
|
+
* 不碰会话数据和 transcript:数据由 index.ts 传入,跳转通过回调完成。
|
|
6
|
+
* - 键盘:↑↓ 选择、回车跳转(作用于当前选中项)、→/d 回合分析、Esc 关闭(分析内先返回列表)
|
|
7
|
+
* - 鼠标:仅回合分析详情区的滚轮;单击/双击/悬停均无效果
|
|
8
|
+
*/
|
|
9
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import { getMarkdownTheme } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
import {
|
|
12
|
+
Markdown,
|
|
13
|
+
type Component,
|
|
14
|
+
type TUI,
|
|
15
|
+
type TuiMouseEvent,
|
|
16
|
+
type TuiMouseEventResult,
|
|
17
|
+
matchesKey,
|
|
18
|
+
truncateToWidth,
|
|
19
|
+
visibleWidth,
|
|
20
|
+
wrapTextWithAnsi,
|
|
21
|
+
} from "@earendil-works/pi-tui";
|
|
22
|
+
import { firstLine, type QuestionItem, type TurnData, type TurnNode } from "./logic.ts";
|
|
23
|
+
|
|
24
|
+
/** 工具参数摘要(单行、限宽) */
|
|
25
|
+
function briefArgs(v: unknown, max = 36): string {
|
|
26
|
+
let s: string;
|
|
27
|
+
try {
|
|
28
|
+
s = typeof v === "string" ? v : (JSON.stringify(v) ?? "null");
|
|
29
|
+
} catch {
|
|
30
|
+
s = String(v);
|
|
31
|
+
}
|
|
32
|
+
s = s.replace(/\s+/g, " ").trim();
|
|
33
|
+
return s ? truncateToWidth(s, max, "…") : "{}";
|
|
34
|
+
}
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// 面板组件
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
const MAX_VISIBLE_ROWS = 18;
|
|
40
|
+
/** 面板最大高度占终端行数的比例(与 overlayOptions.maxHeight 保持一致) */
|
|
41
|
+
export const PANEL_HEIGHT_RATIO = 0.7;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 重线框绘制:列表视图与回合分析视图共用。
|
|
45
|
+
* 与 pi-session-nav 一致:border 颜色、内容两侧各留 1 格。
|
|
46
|
+
*/
|
|
47
|
+
class Frame {
|
|
48
|
+
readonly innerW: number;
|
|
49
|
+
/** 可用内容宽度(已扣除两侧边框与留白) */
|
|
50
|
+
readonly contentW: number;
|
|
51
|
+
|
|
52
|
+
constructor(
|
|
53
|
+
private readonly theme: Theme,
|
|
54
|
+
width: number,
|
|
55
|
+
) {
|
|
56
|
+
this.innerW = Math.max(1, width - 2);
|
|
57
|
+
this.contentW = Math.max(1, this.innerW - 2);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
border(text: string): string {
|
|
61
|
+
return this.theme.fg("border", text);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 一行内容:截断并补齐到内容宽度;active 时整行高亮;rightEdge 可替换右边框(详情区用作滚动条) */
|
|
65
|
+
row(body: string, opts: { active?: boolean; rightEdge?: string } = {}): string {
|
|
66
|
+
const fitted = truncateToWidth(body, this.contentW, "…");
|
|
67
|
+
const inner = ` ${fitted}${" ".repeat(Math.max(0, this.contentW - visibleWidth(fitted)))} `;
|
|
68
|
+
return (
|
|
69
|
+
this.border("┃") +
|
|
70
|
+
(opts.active ? this.theme.style(inner, { fg: "accent", bold: true }) : inner) +
|
|
71
|
+
(opts.rightEdge ?? this.border("┃"))
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
top(title: string): string {
|
|
76
|
+
const fitted = truncateToWidth(title, Math.max(1, this.innerW - 1), "…");
|
|
77
|
+
return this.border(`┏${fitted}${"━".repeat(Math.max(0, this.innerW - visibleWidth(fitted)))}┓`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
separator(): string {
|
|
81
|
+
return this.border("┣" + "━".repeat(this.innerW) + "┫");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
bottom(): string {
|
|
85
|
+
return this.border("┗" + "━".repeat(this.innerW) + "┛");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 左右两端对齐的标题行;放不下时退化为截断的单段 */
|
|
89
|
+
header(left: string, right: string): string {
|
|
90
|
+
if (!right) return left;
|
|
91
|
+
const gap = this.contentW - visibleWidth(left) - visibleWidth(right);
|
|
92
|
+
return gap >= 1 ? left + " ".repeat(gap) + right : truncateToWidth(`${left} ${right}`, this.contentW, "…");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export class QuestionPanelComponent implements Component {
|
|
97
|
+
private readonly tui: TUI;
|
|
98
|
+
private readonly theme: Theme;
|
|
99
|
+
private readonly items: QuestionItem[];
|
|
100
|
+
private readonly jump: (index: number) => void;
|
|
101
|
+
private readonly onClose: () => void;
|
|
102
|
+
|
|
103
|
+
private readonly turnLoader: (ordinal: number) => TurnData | null;
|
|
104
|
+
|
|
105
|
+
private selected = 0;
|
|
106
|
+
/** 列表滚动偏移(条目数) */
|
|
107
|
+
private offset = 0;
|
|
108
|
+
private version = 0;
|
|
109
|
+
|
|
110
|
+
// 回合分析视图状态
|
|
111
|
+
private view: "list" | "turn" = "list";
|
|
112
|
+
private turn: TurnData | null = null;
|
|
113
|
+
/** 仅显示思维链 */
|
|
114
|
+
private chainOnly = false;
|
|
115
|
+
/** 选中节点在 visibleNodes 中的序号 */
|
|
116
|
+
private nodeSel = 0;
|
|
117
|
+
private treeOffset = 0;
|
|
118
|
+
private detailOffset = 0;
|
|
119
|
+
/** 上次渲染的详情总行数(用于滚轮 clamp) */
|
|
120
|
+
private detailLen = 0;
|
|
121
|
+
|
|
122
|
+
/** 详情内容行范围(含端点) */
|
|
123
|
+
private detailTop = -1;
|
|
124
|
+
private detailBottom = -1;
|
|
125
|
+
/** Markdown 组件缓存(输出节点详情) */
|
|
126
|
+
private mdCache: { key: string; theme: unknown; md: Markdown } | null = null;
|
|
127
|
+
|
|
128
|
+
// 渲染缓存
|
|
129
|
+
private cachedLines: string[] = [];
|
|
130
|
+
private cachedWidth = 0;
|
|
131
|
+
private cachedRows = -1;
|
|
132
|
+
private cachedVersion = -1;
|
|
133
|
+
|
|
134
|
+
constructor(
|
|
135
|
+
tui: TUI,
|
|
136
|
+
theme: Theme,
|
|
137
|
+
items: QuestionItem[],
|
|
138
|
+
jump: (index: number) => void,
|
|
139
|
+
turnLoader: (ordinal: number) => TurnData | null,
|
|
140
|
+
onClose: () => void,
|
|
141
|
+
) {
|
|
142
|
+
this.tui = tui;
|
|
143
|
+
this.theme = theme;
|
|
144
|
+
this.items = items;
|
|
145
|
+
this.jump = jump;
|
|
146
|
+
this.turnLoader = turnLoader;
|
|
147
|
+
this.onClose = onClose;
|
|
148
|
+
this.selected = items.length - 1;
|
|
149
|
+
this.ensureSelectedVisible();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
close(): void {
|
|
153
|
+
this.onClose();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
invalidate(): void {
|
|
157
|
+
this.cachedWidth = 0;
|
|
158
|
+
this.cachedRows = -1;
|
|
159
|
+
this.mdCache = null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private bump(): void {
|
|
163
|
+
this.version++;
|
|
164
|
+
this.tui.requestRender();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private visibleCount(): number {
|
|
168
|
+
// 列表固定行:顶/底边框、上下空行、提示行(5)+ 上/下 more 提示(≤2)
|
|
169
|
+
const terminalRows = this.tui.terminal?.rows ?? 40;
|
|
170
|
+
const budget = Math.max(3, Math.floor(terminalRows * PANEL_HEIGHT_RATIO) - 7);
|
|
171
|
+
return Math.min(this.items.length, MAX_VISIBLE_ROWS, budget);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private ensureSelectedVisible(): void {
|
|
175
|
+
const count = this.visibleCount();
|
|
176
|
+
if (this.selected < this.offset) this.offset = this.selected;
|
|
177
|
+
if (this.selected > this.offset + count - 1) this.offset = this.selected - count + 1;
|
|
178
|
+
// 终端被放大(可见行数变多)后,offset 不能让列表尾部留空
|
|
179
|
+
this.offset = Math.max(0, Math.min(Math.max(0, this.items.length - count), this.offset));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private jumpTo(index: number): void {
|
|
183
|
+
this.selected = index;
|
|
184
|
+
this.jump(index);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 当前树视图可见节点(chainOnly 时仅思考) */
|
|
188
|
+
private visibleNodes(): TurnNode[] {
|
|
189
|
+
if (!this.turn) return [];
|
|
190
|
+
return this.chainOnly ? this.turn.nodes.filter((n) => n.kind === "thinking") : this.turn.nodes;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 回合分析可用行数:按终端高度分配节点区/详情区。
|
|
195
|
+
* 固定行:顶/底边框、提问行、3 条分隔线、2 个区块标题、提示行(共 9 行)。
|
|
196
|
+
*/
|
|
197
|
+
private turnLayout(): { treeRows: number; detailRows: number } {
|
|
198
|
+
const terminalRows = this.tui.terminal?.rows ?? 40;
|
|
199
|
+
const panelRows = Math.max(12, Math.floor(terminalRows * PANEL_HEIGHT_RATIO));
|
|
200
|
+
const contentRows = Math.max(4, panelRows - 9);
|
|
201
|
+
const treeRows = Math.max(2, Math.min(8, Math.round(contentRows * 0.4)));
|
|
202
|
+
return { treeRows, detailRows: Math.max(2, contentRows - treeRows) };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private ensureNodeVisible(): void {
|
|
206
|
+
const total = this.visibleNodes().length;
|
|
207
|
+
const view = Math.min(total, this.turnLayout().treeRows);
|
|
208
|
+
if (this.nodeSel < this.treeOffset) this.treeOffset = this.nodeSel;
|
|
209
|
+
if (this.nodeSel > this.treeOffset + view - 1) this.treeOffset = this.nodeSel - view + 1;
|
|
210
|
+
this.treeOffset = Math.max(0, Math.min(Math.max(0, total - view), this.treeOffset));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private selectNode(pos: number): void {
|
|
214
|
+
const total = this.visibleNodes().length;
|
|
215
|
+
if (total === 0) return;
|
|
216
|
+
this.nodeSel = Math.max(0, Math.min(total - 1, pos));
|
|
217
|
+
this.detailOffset = 0;
|
|
218
|
+
this.ensureNodeVisible();
|
|
219
|
+
this.bump();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** 列表 d/→:进入该提问的回合分析 */
|
|
223
|
+
private enterTurn(index: number): void {
|
|
224
|
+
const turn = this.turnLoader(this.items[index]!.index);
|
|
225
|
+
if (!turn) return;
|
|
226
|
+
this.selected = index;
|
|
227
|
+
this.turn = turn;
|
|
228
|
+
this.view = "turn";
|
|
229
|
+
this.chainOnly = false;
|
|
230
|
+
this.nodeSel = 0;
|
|
231
|
+
this.treeOffset = 0;
|
|
232
|
+
this.detailOffset = 0;
|
|
233
|
+
this.bump();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private backToList(): void {
|
|
237
|
+
this.view = "list";
|
|
238
|
+
this.turn = null;
|
|
239
|
+
this.mdCache = null;
|
|
240
|
+
this.bump();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
handleInput(data: string): boolean {
|
|
244
|
+
if (this.view === "turn") {
|
|
245
|
+
if (matchesKey(data, "escape") || matchesKey(data, "left")) {
|
|
246
|
+
this.backToList();
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
if (matchesKey(data, "up")) {
|
|
250
|
+
this.selectNode(this.nodeSel - 1);
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
if (matchesKey(data, "down")) {
|
|
254
|
+
this.selectNode(this.nodeSel + 1);
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
if (data === "t" || data === "T") {
|
|
258
|
+
this.chainOnly = !this.chainOnly;
|
|
259
|
+
this.nodeSel = 0;
|
|
260
|
+
this.treeOffset = 0;
|
|
261
|
+
this.detailOffset = 0;
|
|
262
|
+
this.bump();
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
if (matchesKey(data, "return")) {
|
|
266
|
+
// 回车:定位到该提问在 transcript 的位置
|
|
267
|
+
this.jump(this.turn!.question.index);
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// 列表视图
|
|
274
|
+
if (matchesKey(data, "escape")) {
|
|
275
|
+
this.close();
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
if (matchesKey(data, "up")) {
|
|
279
|
+
if (this.selected > 0) {
|
|
280
|
+
this.selected--;
|
|
281
|
+
this.ensureSelectedVisible();
|
|
282
|
+
this.bump();
|
|
283
|
+
}
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
if (matchesKey(data, "down")) {
|
|
287
|
+
if (this.selected < this.items.length - 1) {
|
|
288
|
+
this.selected++;
|
|
289
|
+
this.ensureSelectedVisible();
|
|
290
|
+
this.bump();
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
if (matchesKey(data, "return") || data === " ") {
|
|
295
|
+
// 回车/空格:跳转到当前选中项
|
|
296
|
+
this.jumpTo(this.selected);
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
if (data === "d" || data === "D" || matchesKey(data, "right")) {
|
|
300
|
+
this.enterTurn(this.selected);
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
308
|
+
if (event.type === "wheel" && this.view === "turn") {
|
|
309
|
+
// 仅保留回合分析详情区的滚轮滚动(列表与节点树不响应滚轮)
|
|
310
|
+
if (event.y >= this.detailTop && event.y <= this.detailBottom) {
|
|
311
|
+
const step = Math.max(-3, Math.min(3, event.wheelDelta ?? -1));
|
|
312
|
+
const maxOffset = Math.max(0, this.detailLen - this.turnLayout().detailRows);
|
|
313
|
+
const next = Math.max(0, Math.min(maxOffset, this.detailOffset + step));
|
|
314
|
+
if (next !== this.detailOffset) {
|
|
315
|
+
this.detailOffset = next;
|
|
316
|
+
this.bump();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return { handled: true, render: true };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return { handled: true, render: false };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
render(width: number): string[] {
|
|
326
|
+
// 终端高度变化也会改变可见行数,必须纳入缓存键
|
|
327
|
+
const rows = this.tui.terminal?.rows ?? 0;
|
|
328
|
+
if (width === this.cachedWidth && rows === this.cachedRows && this.cachedVersion === this.version) {
|
|
329
|
+
return this.cachedLines;
|
|
330
|
+
}
|
|
331
|
+
// 渲染前先让滚动偏移适应当前可见行数(终端放大后不留空行/不隐藏前面的条目)
|
|
332
|
+
if (this.view === "turn") this.ensureNodeVisible();
|
|
333
|
+
else this.ensureSelectedVisible();
|
|
334
|
+
const lines = this.view === "turn" ? this.renderTurn(width) : this.renderList(width);
|
|
335
|
+
this.cachedLines = lines;
|
|
336
|
+
this.cachedWidth = width;
|
|
337
|
+
this.cachedRows = rows;
|
|
338
|
+
this.cachedVersion = this.version;
|
|
339
|
+
return lines;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private renderList(width: number): string[] {
|
|
343
|
+
const theme = this.theme;
|
|
344
|
+
const f = new Frame(theme, width);
|
|
345
|
+
const row = (body: string): string => f.row(body);
|
|
346
|
+
const lines: string[] = [f.top(` Navigate conversation · ${this.items.length} messages `), row("")];
|
|
347
|
+
|
|
348
|
+
// 列表上方的省略提示行(仅展示,不可选中)
|
|
349
|
+
const count = this.visibleCount();
|
|
350
|
+
if (this.offset > 0) lines.push(row(theme.fg("dim", " ↑ more")));
|
|
351
|
+
|
|
352
|
+
// 条目行
|
|
353
|
+
for (let rowIdx = 0; rowIdx < count; rowIdx++) {
|
|
354
|
+
const idx = this.offset + rowIdx;
|
|
355
|
+
if (idx >= this.items.length) break;
|
|
356
|
+
const item = this.items[idx]!;
|
|
357
|
+
const active = this.selected === idx;
|
|
358
|
+
const marker = active ? "> " : " ";
|
|
359
|
+
const num = String(idx + 1).padStart(2, " ");
|
|
360
|
+
lines.push(f.row(`${marker}${num} ${item.summary}`, { active }));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// 列表下方的省略提示行(仅展示,不可选中)
|
|
364
|
+
if (this.offset + count < this.items.length) lines.push(row(theme.fg("dim", " ↓ more")));
|
|
365
|
+
|
|
366
|
+
// 底部:空行 + dim 提示 + 底边框(与 pi-session-nav 一致)
|
|
367
|
+
lines.push(row(""));
|
|
368
|
+
const hint = `↑↓ select · Enter jump · →/d analyze · Esc close`;
|
|
369
|
+
lines.push(row(theme.fg("dim", hint)));
|
|
370
|
+
lines.push(f.bottom());
|
|
371
|
+
|
|
372
|
+
return lines;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// -----------------------------------------------------------------------
|
|
376
|
+
// 回合分析视图
|
|
377
|
+
// -----------------------------------------------------------------------
|
|
378
|
+
|
|
379
|
+
private renderTurn(width: number): string[] {
|
|
380
|
+
const theme = this.theme;
|
|
381
|
+
const turn = this.turn!;
|
|
382
|
+
const f = new Frame(theme, width);
|
|
383
|
+
const contentW = f.contentW;
|
|
384
|
+
const row = (body: string): string => f.row(body);
|
|
385
|
+
const separator = (): string => f.separator();
|
|
386
|
+
const header = (left: string, right: string): string => f.header(left, right);
|
|
387
|
+
const { treeRows, detailRows } = this.turnLayout();
|
|
388
|
+
|
|
389
|
+
const lines: string[] = [f.top(` Analysis · Q${turn.question.index + 1} `)];
|
|
390
|
+
|
|
391
|
+
// 提问行
|
|
392
|
+
lines.push(row(`${theme.fg("accent", "Question:")} ${turn.question.summary}`));
|
|
393
|
+
lines.push(separator());
|
|
394
|
+
|
|
395
|
+
// 节点区:标题行(左:选中序号/总数,右:可视范围)+ 固定行数的节点列表
|
|
396
|
+
const nodes = this.visibleNodes();
|
|
397
|
+
const view = Math.min(nodes.length, treeRows);
|
|
398
|
+
const selectedLabel = nodes.length === 0 ? "0/0" : `${this.nodeSel + 1}/${nodes.length}`;
|
|
399
|
+
const nodeRange =
|
|
400
|
+
nodes.length > view
|
|
401
|
+
? `${this.treeOffset + 1}-${Math.min(nodes.length, this.treeOffset + view)}/${nodes.length}`
|
|
402
|
+
: "";
|
|
403
|
+
const nodeHeader = `${theme.fg("accent", "Nodes")}${theme.fg("dim", ` · ${selectedLabel}${this.chainOnly ? " · thinking only" : ""}`)}`;
|
|
404
|
+
lines.push(row(header(nodeHeader, nodeRange ? theme.fg("dim", nodeRange) : "")));
|
|
405
|
+
|
|
406
|
+
for (let r = 0; r < treeRows; r++) {
|
|
407
|
+
const pos = this.treeOffset + r;
|
|
408
|
+
const node = pos < nodes.length ? nodes[pos] : undefined;
|
|
409
|
+
if (!node) {
|
|
410
|
+
lines.push(row(r === 0 && nodes.length === 0 ? theme.fg("dim", "(no nodes)") : ""));
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const active = pos === this.nodeSel;
|
|
414
|
+
let label: string;
|
|
415
|
+
let summary: string;
|
|
416
|
+
switch (node.kind) {
|
|
417
|
+
case "thinking":
|
|
418
|
+
label = "Thinking";
|
|
419
|
+
summary = firstLine(node.text) || "(Empty)";
|
|
420
|
+
break;
|
|
421
|
+
case "text":
|
|
422
|
+
label = node.isFinal ? "Output" : "Text";
|
|
423
|
+
summary = firstLine(node.text);
|
|
424
|
+
break;
|
|
425
|
+
case "tool": {
|
|
426
|
+
label = "Tool";
|
|
427
|
+
const status =
|
|
428
|
+
node.result === null ? "·no result" : node.isError ? "·error" : `·${node.result.split("\n").length} lines`;
|
|
429
|
+
summary = `${node.name} ${briefArgs(node.args)} ${status}`;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const marker = active ? "› " : " ";
|
|
434
|
+
const body = `${marker}${label.padEnd(9)}${summary}`;
|
|
435
|
+
lines.push(f.row(body, { active }));
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
lines.push(separator());
|
|
439
|
+
|
|
440
|
+
// 详情区:标题行(左:类型,右:可视范围)+ 内容(右边框列兼作滚动条)
|
|
441
|
+
const selNode = nodes[this.nodeSel];
|
|
442
|
+
const kindLabel = selNode
|
|
443
|
+
? selNode.kind === "thinking"
|
|
444
|
+
? "Thinking"
|
|
445
|
+
: selNode.kind === "tool"
|
|
446
|
+
? `Tool ${selNode.name}${selNode.isError ? " (error)" : ""}`
|
|
447
|
+
: selNode.isFinal
|
|
448
|
+
? "Output (Markdown)"
|
|
449
|
+
: "Text"
|
|
450
|
+
: "(no node)";
|
|
451
|
+
const detail = selNode ? this.detailLines(selNode, contentW) : ["(no node)"];
|
|
452
|
+
this.detailLen = detail.length;
|
|
453
|
+
const maxDetailOffset = Math.max(0, detail.length - detailRows);
|
|
454
|
+
const start = Math.max(0, Math.min(this.detailOffset, maxDetailOffset));
|
|
455
|
+
const scrollable = detail.length > detailRows;
|
|
456
|
+
const detailRange = scrollable
|
|
457
|
+
? `${start + 1}-${Math.min(detail.length, start + detailRows)}/${detail.length}`
|
|
458
|
+
: "";
|
|
459
|
+
const detailHeader = `${theme.fg("accent", "Detail")}${theme.fg("dim", ` · ${kindLabel}`)}`;
|
|
460
|
+
lines.push(row(header(detailHeader, detailRange ? theme.fg("dim", detailRange) : "")));
|
|
461
|
+
|
|
462
|
+
const thumbSize = scrollable
|
|
463
|
+
? Math.max(1, Math.min(detailRows, Math.round((detailRows * detailRows) / detail.length)))
|
|
464
|
+
: 0;
|
|
465
|
+
const thumbTop =
|
|
466
|
+
scrollable && maxDetailOffset > 0 ? Math.round((start / maxDetailOffset) * (detailRows - thumbSize)) : 0;
|
|
467
|
+
this.detailTop = lines.length;
|
|
468
|
+
for (let r = 0; r < detailRows; r++) {
|
|
469
|
+
const idx = start + r;
|
|
470
|
+
const text = idx < detail.length ? detail[idx]! : "";
|
|
471
|
+
const rightEdge = !scrollable
|
|
472
|
+
? undefined
|
|
473
|
+
: r >= thumbTop && r < thumbTop + thumbSize
|
|
474
|
+
? theme.fg("borderAccent", "█")
|
|
475
|
+
: theme.fg("borderMuted", "│");
|
|
476
|
+
lines.push(f.row(text, { rightEdge }));
|
|
477
|
+
}
|
|
478
|
+
this.detailBottom = lines.length - 1;
|
|
479
|
+
|
|
480
|
+
lines.push(separator());
|
|
481
|
+
const hint = `↑↓ select · ←/Esc back · t ${this.chainOnly ? "thinking-only" : "all"} · wheel detail`;
|
|
482
|
+
lines.push(row(theme.fg("dim", hint)));
|
|
483
|
+
lines.push(f.bottom());
|
|
484
|
+
|
|
485
|
+
return lines;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** 节点详情行(已按宽度 wrap,可直接进详情区) */
|
|
489
|
+
private detailLines(node: TurnNode, dw: number): string[] {
|
|
490
|
+
const theme = this.theme;
|
|
491
|
+
const wrap = (text: string): string[] => {
|
|
492
|
+
const out: string[] = [];
|
|
493
|
+
for (const line of text.split("\n")) {
|
|
494
|
+
const wrapped = wrapTextWithAnsi(line, dw);
|
|
495
|
+
out.push(...(wrapped.length > 0 ? wrapped : [""]));
|
|
496
|
+
}
|
|
497
|
+
return out.length > 0 ? out : ["(Empty)"];
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
if (node.kind === "thinking") return wrap(node.text);
|
|
501
|
+
|
|
502
|
+
if (node.kind === "text") {
|
|
503
|
+
if (!node.isFinal) return wrap(node.text);
|
|
504
|
+
// 最终输出:Markdown 渲染(按节点+宽度缓存,主题变化时重建;
|
|
505
|
+
// 主题未初始化等异常时退回纯文本 wrap)
|
|
506
|
+
try {
|
|
507
|
+
const mdTheme = getMarkdownTheme();
|
|
508
|
+
const key = `${node.id}:${dw}`;
|
|
509
|
+
if (!this.mdCache || this.mdCache.key !== key || this.mdCache.theme !== mdTheme) {
|
|
510
|
+
this.mdCache = { key, theme: mdTheme, md: new Markdown(node.text, 0, 0, mdTheme) };
|
|
511
|
+
}
|
|
512
|
+
return [...this.mdCache.md.render(dw)];
|
|
513
|
+
} catch {
|
|
514
|
+
return wrap(node.text);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
// 工具节点:参数 + 结果(名称/错误状态已在详情标题里)
|
|
519
|
+
const head: string[] = [theme.fg("dim", "Args:")];
|
|
520
|
+
let argsLines: string[];
|
|
521
|
+
try {
|
|
522
|
+
const s = typeof node.args === "string" ? node.args : (JSON.stringify(node.args, null, 2) ?? String(node.args));
|
|
523
|
+
argsLines = wrap(s);
|
|
524
|
+
} catch {
|
|
525
|
+
argsLines = wrap(String(node.args));
|
|
526
|
+
}
|
|
527
|
+
const resHead =
|
|
528
|
+
node.result === null
|
|
529
|
+
? ["", theme.fg("dim", "Result: (not returned)")]
|
|
530
|
+
: ["", theme.fg("dim", `Result${node.isError ? " (error)" : ""}:`)];
|
|
531
|
+
const resLines = node.result === null ? [] : wrap(node.result);
|
|
532
|
+
return [...head, ...argsLines, ...resHead, ...resLines];
|
|
533
|
+
}
|
|
534
|
+
}
|