@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/jump.ts
CHANGED
|
@@ -1,229 +1,229 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* jump.ts
|
|
3
|
-
*
|
|
4
|
-
* 跳转:把 transcript 滚动到某条用户提问。
|
|
5
|
-
* 本仓库里所有依赖 pi TUI 内部实现的代码都集中在这个文件;pi 升级后跳转失效,只需要查这里。
|
|
6
|
-
* test/contract.test.mjs 用真实的 pi 包验证这里的全部假设。
|
|
7
|
-
*
|
|
8
|
-
* pi 未公开暴露 transcript 组件,这里对 TUI 内部布局对象做运行时特征检测:
|
|
9
|
-
* currentLayout → 含 scrollContentLines 的 box → ScrollView.scrollTo(row)。
|
|
10
|
-
* 首选行定位利用 transcript 的 OSC 133;A 标记(UserMessageComponent 与无工具调用的
|
|
11
|
-
* AssistantMessageComponent 首行都会写入,与内置 previousPrompt/nextPrompt 跳转同源)。
|
|
12
|
-
* 标记法失败时退回文本匹配:遍历 transcript 组件树找 UserMessageComponent,
|
|
13
|
-
* 比较归一化文本 + 渲染首行在文档中的出现序(不再依赖标记规则)。
|
|
14
|
-
*/
|
|
15
|
-
import { stripTerminalSequences, type TUI } from "@earendil-works/pi-tui";
|
|
16
|
-
import { normalizeMatchText } from "./logic.ts";
|
|
17
|
-
|
|
18
|
-
// ---------------------------------------------------------------------------
|
|
19
|
-
// 跳转:运行时特征检测 pi 内部布局(失败优雅降级)
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
/** 用户/纯文本 assistant 消息首行的标记(OSC 133;A)。 */
|
|
23
|
-
const OSC133_A = /^\x1b\]133;A/;
|
|
24
|
-
|
|
25
|
-
/** 剥颜色/光标等序列与 OSC 133 区域标记,只用于行内容比较(pi-tui 公开导出)。 */
|
|
26
|
-
const stripLineAnsi = stripTerminalSequences;
|
|
27
|
-
|
|
28
|
-
interface ScrollViewLike {
|
|
29
|
-
scrollTo(row: number, opts?: { disableFollow?: boolean }): void;
|
|
30
|
-
/** ScrollView 的唯一子组件(transcript 文档根) */
|
|
31
|
-
child?: unknown;
|
|
32
|
-
getContentWidth?(width: number): number;
|
|
33
|
-
}
|
|
34
|
-
interface LayoutBoxLike {
|
|
35
|
-
scrollView?: unknown;
|
|
36
|
-
scrollContentLines?: readonly string[];
|
|
37
|
-
children?: LayoutBoxLike[];
|
|
38
|
-
}
|
|
39
|
-
interface TuiInternals {
|
|
40
|
-
requestRender?: () => void;
|
|
41
|
-
/** 渲染帧:{ root: 根盒, lines, primaryScrollView? }(getScrollViewBox 同源结构) */
|
|
42
|
-
currentLayout?: ({ primaryScrollView?: unknown; root?: LayoutBoxLike } & LayoutBoxLike) | undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** 在布局树里找到 transcript 的滚动视图与全部内容行。 */
|
|
46
|
-
export function findTranscript(tui: TUI): { scrollView: ScrollViewLike; lines: readonly string[] } | null {
|
|
47
|
-
// SAFETY: tui 是 pi 传入的 TuiAltScreen 实例,currentLayout 属性在运行时存在且未被混淆;
|
|
48
|
-
// 此处仅读取布局数据,不触内部方法,字段缺失时逐层判空返回 null。
|
|
49
|
-
const internals = tui as unknown as TuiInternals;
|
|
50
|
-
const layout = internals.currentLayout;
|
|
51
|
-
// 渲染帧形状:{ root: 根盒, lines, primaryScrollView? },盒子树挂在 root 上
|
|
52
|
-
const rootBox: LayoutBoxLike | undefined = layout?.root ?? layout;
|
|
53
|
-
if (!rootBox) return null;
|
|
54
|
-
|
|
55
|
-
const boxes: LayoutBoxLike[] = [];
|
|
56
|
-
const visit = (box: LayoutBoxLike): void => {
|
|
57
|
-
boxes.push(box);
|
|
58
|
-
for (const child of box.children ?? []) visit(child);
|
|
59
|
-
};
|
|
60
|
-
visit(rootBox);
|
|
61
|
-
|
|
62
|
-
// transcript = 滚动内容里带 OSC 133;A 标记的 scroll box
|
|
63
|
-
for (const box of boxes) {
|
|
64
|
-
const lines = box.scrollContentLines;
|
|
65
|
-
if (!Array.isArray(lines) || lines.length === 0) continue;
|
|
66
|
-
if (!lines.some((line) => OSC133_A.test(line ?? ""))) continue;
|
|
67
|
-
|
|
68
|
-
const scrollView = (box.scrollView ?? layout?.primaryScrollView) as ScrollViewLike | undefined;
|
|
69
|
-
if (!scrollView || typeof scrollView.scrollTo !== "function") return null;
|
|
70
|
-
return { scrollView, lines };
|
|
71
|
-
}
|
|
72
|
-
return null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** 目标行上方留一行上下文,禁用「跟随最新输出」后刷新。 */
|
|
76
|
-
function scrollToRow(tui: TUI, scrollView: ScrollViewLike, row: number): void {
|
|
77
|
-
scrollView.scrollTo(Math.max(0, row - 1), { disableFollow: true });
|
|
78
|
-
// SAFETY: 仅调用公开的 requestRender(可选属性,不存在则跳过)
|
|
79
|
-
(tui as unknown as TuiInternals).requestRender?.();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** 标记行(组件首行,可能是空白内边距)起的第一行非空文字,已剥 ANSI/标记。 */
|
|
83
|
-
function firstTextFromRow(lines: readonly string[], row: number): string {
|
|
84
|
-
for (let r = row; r < lines.length; r++) {
|
|
85
|
-
const text = stripLineAnsi(lines[r] ?? "").trim();
|
|
86
|
-
if (text) return text;
|
|
87
|
-
}
|
|
88
|
-
return "";
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 滚动到标记序列中第 ordinal 个标记行。返回是否成功。
|
|
93
|
-
* expectedFirstLine:目标提问的首行摘要。标记行的实际文字与它不符,说明标记序号已错位
|
|
94
|
-
* (pi 改了标记规则),此时不滚动、返回 false,让调用方退回文本匹配,避免静默跳错。
|
|
95
|
-
*/
|
|
96
|
-
export function jumpToMarker(tui: TUI, ordinal: number, expectedFirstLine?: string): boolean {
|
|
97
|
-
const found = findTranscript(tui);
|
|
98
|
-
if (!found) return false;
|
|
99
|
-
|
|
100
|
-
let seen = -1;
|
|
101
|
-
for (let row = 0; row < found.lines.length; row++) {
|
|
102
|
-
if (!OSC133_A.test(found.lines[row] ?? "")) continue;
|
|
103
|
-
seen++;
|
|
104
|
-
if (seen !== ordinal) continue;
|
|
105
|
-
if (expectedFirstLine !== undefined) {
|
|
106
|
-
// 标记行的文字可能被渲染截断,但不会比原文更长:只接受「实际文字是提问首行的前缀」。
|
|
107
|
-
// 反方向(实际文字更长)会放过指向另一条以相同开头的消息的错位序号。
|
|
108
|
-
const actual = normalizeMatchText(firstTextFromRow(found.lines, row));
|
|
109
|
-
const expected = normalizeMatchText(expectedFirstLine);
|
|
110
|
-
if (!actual || !expected.startsWith(actual)) return false;
|
|
111
|
-
// 实际文字很短时前缀没有区分度("a" 是几乎所有文字的前缀),要求整行相同
|
|
112
|
-
if (actual.length < 8 && actual !== expected) return false;
|
|
113
|
-
}
|
|
114
|
-
scrollToRow(tui, found.scrollView, row);
|
|
115
|
-
return true;
|
|
116
|
-
}
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// ---------------------------------------------------------------------------
|
|
121
|
-
// 跳转兜底:文本匹配(pi 改动标记规则时仍可用)
|
|
122
|
-
// ---------------------------------------------------------------------------
|
|
123
|
-
|
|
124
|
-
interface TextualComponent {
|
|
125
|
-
text?: unknown;
|
|
126
|
-
rebuild?: unknown;
|
|
127
|
-
child?: unknown;
|
|
128
|
-
children?: unknown[];
|
|
129
|
-
render?(width: number): string[];
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** UserMessageComponent 的特征:原始 text + rebuild() + render()。 */
|
|
133
|
-
function isUserMessageComponent(c: TextualComponent): boolean {
|
|
134
|
-
return typeof c.text === "string" && typeof c.rebuild === "function" && typeof c.render === "function";
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
interface DocIndex {
|
|
138
|
-
users: TextualComponent[];
|
|
139
|
-
/** 每个 UserMessageComponent 在文档行里的起始偏移(按各组件 render 的行数累加) */
|
|
140
|
-
offsets: Map<unknown, number>;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* 遍历组件树,按渲染顺序累加每个组件的行数,记录每个 UserMessageComponent 的起始行偏移。
|
|
145
|
-
* 定位不依赖字符串匹配:即使 assistant 回复与提问首行相同、或中间有 Spacer/DynamicBorder,偏移也是对的。
|
|
146
|
-
* 前提:容器(有 children 数组)占的行数等于其 children 占行数之和;叶子组件占 render().length 行。
|
|
147
|
-
*/
|
|
148
|
-
function indexDocument(root: unknown, width: number): DocIndex {
|
|
149
|
-
const users: TextualComponent[] = [];
|
|
150
|
-
const offsets = new Map<unknown, number>();
|
|
151
|
-
const seen = new Set<unknown>();
|
|
152
|
-
let offset = 0;
|
|
153
|
-
const visit = (node: unknown): void => {
|
|
154
|
-
if (!node || typeof node !== "object" || seen.has(node)) return;
|
|
155
|
-
seen.add(node);
|
|
156
|
-
const c = node as TextualComponent;
|
|
157
|
-
if (isUserMessageComponent(c)) {
|
|
158
|
-
users.push(c);
|
|
159
|
-
offsets.set(node, offset);
|
|
160
|
-
offset += (c.render?.(width) ?? []).length;
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
if (Array.isArray(c.children)) {
|
|
164
|
-
for (const child of c.children) visit(child);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
offset += (c.render?.(width) ?? []).length;
|
|
168
|
-
};
|
|
169
|
-
visit(root);
|
|
170
|
-
return { users, offsets };
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* 文本匹配兜底:在 transcript 组件树里找第 occurrence 个文本相同的 UserMessageComponent,
|
|
175
|
-
* 再按它在文档里的渲染偏移定位行号。
|
|
176
|
-
*
|
|
177
|
-
* 用组件树偏移而非字符串匹配:`found.lines` 含所有组件(含 assistant 回复)的行,
|
|
178
|
-
* 同一首行字符串可能出现在 assistant 回复中;按渲染顺序累加各组件行数得到的偏移不会误配。
|
|
179
|
-
* occurrence = 目标提问之前、文本归一化后相同的提问数量(由调用方按 collectQuestions 计算)。
|
|
180
|
-
*/
|
|
181
|
-
export function jumpToQuestionText(tui: TUI, questionText: string, occurrence: number): boolean {
|
|
182
|
-
const found = findTranscript(tui);
|
|
183
|
-
if (!found) return false;
|
|
184
|
-
const scrollView = found.scrollView;
|
|
185
|
-
const doc = scrollView.child;
|
|
186
|
-
if (!doc) return false;
|
|
187
|
-
|
|
188
|
-
const columns = tui.terminal?.columns ?? 80;
|
|
189
|
-
const width = Math.max(1, scrollView.getContentWidth?.(columns) ?? columns);
|
|
190
|
-
const { users, offsets } = indexDocument(doc, width);
|
|
191
|
-
|
|
192
|
-
const targetNorm = normalizeMatchText(questionText);
|
|
193
|
-
const target = users.filter((c) => normalizeMatchText(c.text as string) === targetNorm)[occurrence];
|
|
194
|
-
if (!target) return false;
|
|
195
|
-
|
|
196
|
-
const start = offsets.get(target);
|
|
197
|
-
if (start === undefined) return false;
|
|
198
|
-
|
|
199
|
-
const targetLines = (target.render?.(width) ?? []).map(stripLineAnsi);
|
|
200
|
-
const firstNonEmpty = targetLines.findIndex((l) => l.trim().length > 0);
|
|
201
|
-
if (firstNonEmpty < 0) return false;
|
|
202
|
-
|
|
203
|
-
const row = start + firstNonEmpty;
|
|
204
|
-
if (row >= found.lines.length) return false;
|
|
205
|
-
scrollToRow(tui, scrollView, row);
|
|
206
|
-
return true;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// ---------------------------------------------------------------------------
|
|
210
|
-
// 统一入口:标记法优先,失败退回文本匹配
|
|
211
|
-
// ---------------------------------------------------------------------------
|
|
212
|
-
|
|
213
|
-
export interface JumpTarget {
|
|
214
|
-
/** 目标提问在 OSC 133;A 标记序列中的序号;无法推算时为 null(直接走文本匹配) */
|
|
215
|
-
markerOrdinal: number | null;
|
|
216
|
-
/** 完整提问文本 */
|
|
217
|
-
text: string;
|
|
218
|
-
/** 首行摘要,用于校验标记法的落点 */
|
|
219
|
-
summary: string;
|
|
220
|
-
/** 目标之前归一化文本相同的提问数量 */
|
|
221
|
-
occurrence: number;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/** 滚动到目标提问。返回是否成功;失败时调用方负责提示用户。 */
|
|
225
|
-
export function jumpToQuestion(tui: TUI, target: JumpTarget): boolean {
|
|
226
|
-
if (target.markerOrdinal !== null && jumpToMarker(tui, target.markerOrdinal, target.summary)) return true;
|
|
227
|
-
// 标记法失效或落点与提问不符(例如 pi 调整了 OSC 133;A 规则)→ 文本匹配兜底
|
|
228
|
-
return jumpToQuestionText(tui, target.text, target.occurrence);
|
|
229
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* jump.ts
|
|
3
|
+
*
|
|
4
|
+
* 跳转:把 transcript 滚动到某条用户提问。
|
|
5
|
+
* 本仓库里所有依赖 pi TUI 内部实现的代码都集中在这个文件;pi 升级后跳转失效,只需要查这里。
|
|
6
|
+
* test/contract.test.mjs 用真实的 pi 包验证这里的全部假设。
|
|
7
|
+
*
|
|
8
|
+
* pi 未公开暴露 transcript 组件,这里对 TUI 内部布局对象做运行时特征检测:
|
|
9
|
+
* currentLayout → 含 scrollContentLines 的 box → ScrollView.scrollTo(row)。
|
|
10
|
+
* 首选行定位利用 transcript 的 OSC 133;A 标记(UserMessageComponent 与无工具调用的
|
|
11
|
+
* AssistantMessageComponent 首行都会写入,与内置 previousPrompt/nextPrompt 跳转同源)。
|
|
12
|
+
* 标记法失败时退回文本匹配:遍历 transcript 组件树找 UserMessageComponent,
|
|
13
|
+
* 比较归一化文本 + 渲染首行在文档中的出现序(不再依赖标记规则)。
|
|
14
|
+
*/
|
|
15
|
+
import { stripTerminalSequences, type TUI } from "@earendil-works/pi-tui";
|
|
16
|
+
import { normalizeMatchText } from "./logic.ts";
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// 跳转:运行时特征检测 pi 内部布局(失败优雅降级)
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
/** 用户/纯文本 assistant 消息首行的标记(OSC 133;A)。 */
|
|
23
|
+
const OSC133_A = /^\x1b\]133;A/;
|
|
24
|
+
|
|
25
|
+
/** 剥颜色/光标等序列与 OSC 133 区域标记,只用于行内容比较(pi-tui 公开导出)。 */
|
|
26
|
+
const stripLineAnsi = stripTerminalSequences;
|
|
27
|
+
|
|
28
|
+
interface ScrollViewLike {
|
|
29
|
+
scrollTo(row: number, opts?: { disableFollow?: boolean }): void;
|
|
30
|
+
/** ScrollView 的唯一子组件(transcript 文档根) */
|
|
31
|
+
child?: unknown;
|
|
32
|
+
getContentWidth?(width: number): number;
|
|
33
|
+
}
|
|
34
|
+
interface LayoutBoxLike {
|
|
35
|
+
scrollView?: unknown;
|
|
36
|
+
scrollContentLines?: readonly string[];
|
|
37
|
+
children?: LayoutBoxLike[];
|
|
38
|
+
}
|
|
39
|
+
interface TuiInternals {
|
|
40
|
+
requestRender?: () => void;
|
|
41
|
+
/** 渲染帧:{ root: 根盒, lines, primaryScrollView? }(getScrollViewBox 同源结构) */
|
|
42
|
+
currentLayout?: ({ primaryScrollView?: unknown; root?: LayoutBoxLike } & LayoutBoxLike) | undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 在布局树里找到 transcript 的滚动视图与全部内容行。 */
|
|
46
|
+
export function findTranscript(tui: TUI): { scrollView: ScrollViewLike; lines: readonly string[] } | null {
|
|
47
|
+
// SAFETY: tui 是 pi 传入的 TuiAltScreen 实例,currentLayout 属性在运行时存在且未被混淆;
|
|
48
|
+
// 此处仅读取布局数据,不触内部方法,字段缺失时逐层判空返回 null。
|
|
49
|
+
const internals = tui as unknown as TuiInternals;
|
|
50
|
+
const layout = internals.currentLayout;
|
|
51
|
+
// 渲染帧形状:{ root: 根盒, lines, primaryScrollView? },盒子树挂在 root 上
|
|
52
|
+
const rootBox: LayoutBoxLike | undefined = layout?.root ?? layout;
|
|
53
|
+
if (!rootBox) return null;
|
|
54
|
+
|
|
55
|
+
const boxes: LayoutBoxLike[] = [];
|
|
56
|
+
const visit = (box: LayoutBoxLike): void => {
|
|
57
|
+
boxes.push(box);
|
|
58
|
+
for (const child of box.children ?? []) visit(child);
|
|
59
|
+
};
|
|
60
|
+
visit(rootBox);
|
|
61
|
+
|
|
62
|
+
// transcript = 滚动内容里带 OSC 133;A 标记的 scroll box
|
|
63
|
+
for (const box of boxes) {
|
|
64
|
+
const lines = box.scrollContentLines;
|
|
65
|
+
if (!Array.isArray(lines) || lines.length === 0) continue;
|
|
66
|
+
if (!lines.some((line) => OSC133_A.test(line ?? ""))) continue;
|
|
67
|
+
|
|
68
|
+
const scrollView = (box.scrollView ?? layout?.primaryScrollView) as ScrollViewLike | undefined;
|
|
69
|
+
if (!scrollView || typeof scrollView.scrollTo !== "function") return null;
|
|
70
|
+
return { scrollView, lines };
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 目标行上方留一行上下文,禁用「跟随最新输出」后刷新。 */
|
|
76
|
+
function scrollToRow(tui: TUI, scrollView: ScrollViewLike, row: number): void {
|
|
77
|
+
scrollView.scrollTo(Math.max(0, row - 1), { disableFollow: true });
|
|
78
|
+
// SAFETY: 仅调用公开的 requestRender(可选属性,不存在则跳过)
|
|
79
|
+
(tui as unknown as TuiInternals).requestRender?.();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 标记行(组件首行,可能是空白内边距)起的第一行非空文字,已剥 ANSI/标记。 */
|
|
83
|
+
function firstTextFromRow(lines: readonly string[], row: number): string {
|
|
84
|
+
for (let r = row; r < lines.length; r++) {
|
|
85
|
+
const text = stripLineAnsi(lines[r] ?? "").trim();
|
|
86
|
+
if (text) return text;
|
|
87
|
+
}
|
|
88
|
+
return "";
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 滚动到标记序列中第 ordinal 个标记行。返回是否成功。
|
|
93
|
+
* expectedFirstLine:目标提问的首行摘要。标记行的实际文字与它不符,说明标记序号已错位
|
|
94
|
+
* (pi 改了标记规则),此时不滚动、返回 false,让调用方退回文本匹配,避免静默跳错。
|
|
95
|
+
*/
|
|
96
|
+
export function jumpToMarker(tui: TUI, ordinal: number, expectedFirstLine?: string): boolean {
|
|
97
|
+
const found = findTranscript(tui);
|
|
98
|
+
if (!found) return false;
|
|
99
|
+
|
|
100
|
+
let seen = -1;
|
|
101
|
+
for (let row = 0; row < found.lines.length; row++) {
|
|
102
|
+
if (!OSC133_A.test(found.lines[row] ?? "")) continue;
|
|
103
|
+
seen++;
|
|
104
|
+
if (seen !== ordinal) continue;
|
|
105
|
+
if (expectedFirstLine !== undefined) {
|
|
106
|
+
// 标记行的文字可能被渲染截断,但不会比原文更长:只接受「实际文字是提问首行的前缀」。
|
|
107
|
+
// 反方向(实际文字更长)会放过指向另一条以相同开头的消息的错位序号。
|
|
108
|
+
const actual = normalizeMatchText(firstTextFromRow(found.lines, row));
|
|
109
|
+
const expected = normalizeMatchText(expectedFirstLine);
|
|
110
|
+
if (!actual || !expected.startsWith(actual)) return false;
|
|
111
|
+
// 实际文字很短时前缀没有区分度("a" 是几乎所有文字的前缀),要求整行相同
|
|
112
|
+
if (actual.length < 8 && actual !== expected) return false;
|
|
113
|
+
}
|
|
114
|
+
scrollToRow(tui, found.scrollView, row);
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ---------------------------------------------------------------------------
|
|
121
|
+
// 跳转兜底:文本匹配(pi 改动标记规则时仍可用)
|
|
122
|
+
// ---------------------------------------------------------------------------
|
|
123
|
+
|
|
124
|
+
interface TextualComponent {
|
|
125
|
+
text?: unknown;
|
|
126
|
+
rebuild?: unknown;
|
|
127
|
+
child?: unknown;
|
|
128
|
+
children?: unknown[];
|
|
129
|
+
render?(width: number): string[];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** UserMessageComponent 的特征:原始 text + rebuild() + render()。 */
|
|
133
|
+
function isUserMessageComponent(c: TextualComponent): boolean {
|
|
134
|
+
return typeof c.text === "string" && typeof c.rebuild === "function" && typeof c.render === "function";
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
interface DocIndex {
|
|
138
|
+
users: TextualComponent[];
|
|
139
|
+
/** 每个 UserMessageComponent 在文档行里的起始偏移(按各组件 render 的行数累加) */
|
|
140
|
+
offsets: Map<unknown, number>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 遍历组件树,按渲染顺序累加每个组件的行数,记录每个 UserMessageComponent 的起始行偏移。
|
|
145
|
+
* 定位不依赖字符串匹配:即使 assistant 回复与提问首行相同、或中间有 Spacer/DynamicBorder,偏移也是对的。
|
|
146
|
+
* 前提:容器(有 children 数组)占的行数等于其 children 占行数之和;叶子组件占 render().length 行。
|
|
147
|
+
*/
|
|
148
|
+
function indexDocument(root: unknown, width: number): DocIndex {
|
|
149
|
+
const users: TextualComponent[] = [];
|
|
150
|
+
const offsets = new Map<unknown, number>();
|
|
151
|
+
const seen = new Set<unknown>();
|
|
152
|
+
let offset = 0;
|
|
153
|
+
const visit = (node: unknown): void => {
|
|
154
|
+
if (!node || typeof node !== "object" || seen.has(node)) return;
|
|
155
|
+
seen.add(node);
|
|
156
|
+
const c = node as TextualComponent;
|
|
157
|
+
if (isUserMessageComponent(c)) {
|
|
158
|
+
users.push(c);
|
|
159
|
+
offsets.set(node, offset);
|
|
160
|
+
offset += (c.render?.(width) ?? []).length;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (Array.isArray(c.children)) {
|
|
164
|
+
for (const child of c.children) visit(child);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
offset += (c.render?.(width) ?? []).length;
|
|
168
|
+
};
|
|
169
|
+
visit(root);
|
|
170
|
+
return { users, offsets };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 文本匹配兜底:在 transcript 组件树里找第 occurrence 个文本相同的 UserMessageComponent,
|
|
175
|
+
* 再按它在文档里的渲染偏移定位行号。
|
|
176
|
+
*
|
|
177
|
+
* 用组件树偏移而非字符串匹配:`found.lines` 含所有组件(含 assistant 回复)的行,
|
|
178
|
+
* 同一首行字符串可能出现在 assistant 回复中;按渲染顺序累加各组件行数得到的偏移不会误配。
|
|
179
|
+
* occurrence = 目标提问之前、文本归一化后相同的提问数量(由调用方按 collectQuestions 计算)。
|
|
180
|
+
*/
|
|
181
|
+
export function jumpToQuestionText(tui: TUI, questionText: string, occurrence: number): boolean {
|
|
182
|
+
const found = findTranscript(tui);
|
|
183
|
+
if (!found) return false;
|
|
184
|
+
const scrollView = found.scrollView;
|
|
185
|
+
const doc = scrollView.child;
|
|
186
|
+
if (!doc) return false;
|
|
187
|
+
|
|
188
|
+
const columns = tui.terminal?.columns ?? 80;
|
|
189
|
+
const width = Math.max(1, scrollView.getContentWidth?.(columns) ?? columns);
|
|
190
|
+
const { users, offsets } = indexDocument(doc, width);
|
|
191
|
+
|
|
192
|
+
const targetNorm = normalizeMatchText(questionText);
|
|
193
|
+
const target = users.filter((c) => normalizeMatchText(c.text as string) === targetNorm)[occurrence];
|
|
194
|
+
if (!target) return false;
|
|
195
|
+
|
|
196
|
+
const start = offsets.get(target);
|
|
197
|
+
if (start === undefined) return false;
|
|
198
|
+
|
|
199
|
+
const targetLines = (target.render?.(width) ?? []).map(stripLineAnsi);
|
|
200
|
+
const firstNonEmpty = targetLines.findIndex((l) => l.trim().length > 0);
|
|
201
|
+
if (firstNonEmpty < 0) return false;
|
|
202
|
+
|
|
203
|
+
const row = start + firstNonEmpty;
|
|
204
|
+
if (row >= found.lines.length) return false;
|
|
205
|
+
scrollToRow(tui, scrollView, row);
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
// 统一入口:标记法优先,失败退回文本匹配
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
export interface JumpTarget {
|
|
214
|
+
/** 目标提问在 OSC 133;A 标记序列中的序号;无法推算时为 null(直接走文本匹配) */
|
|
215
|
+
markerOrdinal: number | null;
|
|
216
|
+
/** 完整提问文本 */
|
|
217
|
+
text: string;
|
|
218
|
+
/** 首行摘要,用于校验标记法的落点 */
|
|
219
|
+
summary: string;
|
|
220
|
+
/** 目标之前归一化文本相同的提问数量 */
|
|
221
|
+
occurrence: number;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** 滚动到目标提问。返回是否成功;失败时调用方负责提示用户。 */
|
|
225
|
+
export function jumpToQuestion(tui: TUI, target: JumpTarget): boolean {
|
|
226
|
+
if (target.markerOrdinal !== null && jumpToMarker(tui, target.markerOrdinal, target.summary)) return true;
|
|
227
|
+
// 标记法失效或落点与提问不符(例如 pi 调整了 OSC 133;A 规则)→ 文本匹配兜底
|
|
228
|
+
return jumpToQuestionText(tui, target.text, target.occurrence);
|
|
229
|
+
}
|