@georgewuw/pi-chat-navi 0.1.0 → 0.1.2
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 +96 -92
- package/package.json +2 -2
- package/src/index.ts +149 -149
- package/src/jump.ts +229 -229
- package/src/logic.ts +333 -329
- package/src/panel.ts +534 -534
package/src/logic.ts
CHANGED
|
@@ -1,329 +1,333 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* logic.ts
|
|
3
|
-
*
|
|
4
|
-
* 纯数据逻辑:会话条目 → 提问列表 / OSC 133;A 标记序号 / 回合分析数据。
|
|
5
|
-
* 该文件没有任何运行时 import(只用局部类型),因此可以被 node:test 直接加载测试。
|
|
6
|
-
*
|
|
7
|
-
* 关键前提:pi 的 transcript 只渲染 `sessionManager.buildContextEntries()` 的结果
|
|
8
|
-
* (压缩后旧消息不在屏幕上)。因此「提问列表」和「标记序号」必须基于同一份活动上下文,
|
|
9
|
-
* 否则压缩后序号会整体偏移,跳转会静默落到错误位置。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
export interface SessionEntryLike {
|
|
13
|
-
type?: string;
|
|
14
|
-
id?: string;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
function
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return false;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
*
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
if (
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
if (
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
continue;
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
1
|
+
/**
|
|
2
|
+
* logic.ts
|
|
3
|
+
*
|
|
4
|
+
* 纯数据逻辑:会话条目 → 提问列表 / OSC 133;A 标记序号 / 回合分析数据。
|
|
5
|
+
* 该文件没有任何运行时 import(只用局部类型),因此可以被 node:test 直接加载测试。
|
|
6
|
+
*
|
|
7
|
+
* 关键前提:pi 的 transcript 只渲染 `sessionManager.buildContextEntries()` 的结果
|
|
8
|
+
* (压缩后旧消息不在屏幕上)。因此「提问列表」和「标记序号」必须基于同一份活动上下文,
|
|
9
|
+
* 否则压缩后序号会整体偏移,跳转会静默落到错误位置。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface SessionEntryLike {
|
|
13
|
+
type?: string;
|
|
14
|
+
id?: string;
|
|
15
|
+
/** 压缩条目的保留起点 */
|
|
16
|
+
firstKeptEntryId?: string;
|
|
17
|
+
/** 压缩条目的摘要文本(logic.ts 不读,但条目里确实存在) */
|
|
18
|
+
summary?: string;
|
|
19
|
+
message?: {
|
|
20
|
+
role?: string;
|
|
21
|
+
content?: unknown;
|
|
22
|
+
stopReason?: string;
|
|
23
|
+
timestamp?: number;
|
|
24
|
+
toolCallId?: string;
|
|
25
|
+
isError?: boolean;
|
|
26
|
+
command?: string;
|
|
27
|
+
output?: string;
|
|
28
|
+
exitCode?: number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface QuestionItem {
|
|
33
|
+
/** 活动上下文中的提问序号(第几条可跳转的用户提问,从 0 开始) */
|
|
34
|
+
index: number;
|
|
35
|
+
/** 会话条目 id;压缩/追加后据此在新列表里找回同一条提问 */
|
|
36
|
+
entryId?: string;
|
|
37
|
+
/** 完整提问文本(用于文本匹配兜底跳转) */
|
|
38
|
+
text: string;
|
|
39
|
+
/** 首行摘要 */
|
|
40
|
+
summary: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type TurnNode =
|
|
44
|
+
| { kind: "thinking"; id: number; text: string }
|
|
45
|
+
| { kind: "text"; id: number; text: string; isFinal: boolean }
|
|
46
|
+
| {
|
|
47
|
+
kind: "tool";
|
|
48
|
+
id: number;
|
|
49
|
+
name: string;
|
|
50
|
+
args: unknown;
|
|
51
|
+
result: string | null;
|
|
52
|
+
isError: boolean;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export interface TurnData {
|
|
56
|
+
question: QuestionItem;
|
|
57
|
+
/** 思考→工具→…→输出 的线性节点链 */
|
|
58
|
+
nodes: TurnNode[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 提取 content 里的 text part(content 可能是 string 或 parts 数组),按 separator 拼接。 */
|
|
62
|
+
function joinTextParts(content: unknown, separator: string): string {
|
|
63
|
+
if (typeof content === "string") return content;
|
|
64
|
+
if (!Array.isArray(content)) return "";
|
|
65
|
+
const parts: string[] = [];
|
|
66
|
+
for (const part of content) {
|
|
67
|
+
if (part && typeof part === "object" && (part as { type?: string }).type === "text") {
|
|
68
|
+
const t = (part as { text?: string }).text;
|
|
69
|
+
if (typeof t === "string") parts.push(t);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return parts.join(separator);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 用户消息文本。镜像 pi 的 getUserMessageText:多段 text 直接拼接(不加分隔符)。 */
|
|
76
|
+
export function extractUserText(content: unknown): string {
|
|
77
|
+
return joinTextParts(content, "");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 镜像 pi 的 parseSkillBlock:skill 调用消息 `<skill name=".." location="..">\n..\n</skill>[\n\n用户附加文本]`。 */
|
|
81
|
+
const SKILL_BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 用户消息在 transcript 里由 UserMessageComponent 渲染的文本;空串表示没有该组件(也就没有 OSC 标记)。
|
|
85
|
+
* - 普通消息:全部 text part 的拼接
|
|
86
|
+
* - skill 调用:pi 渲染 SkillInvocationMessageComponent(不写标记)+ 仅当有附加文本时的 UserMessageComponent,
|
|
87
|
+
* 所以只取附加文本;skill-only 消息返回空串
|
|
88
|
+
*/
|
|
89
|
+
export function renderedUserText(content: unknown): string {
|
|
90
|
+
const text = extractUserText(content);
|
|
91
|
+
if (!text) return "";
|
|
92
|
+
const skill = SKILL_BLOCK.exec(text);
|
|
93
|
+
if (!skill) return text;
|
|
94
|
+
return skill[4]?.trim() ?? "";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function firstLine(text: string): string {
|
|
98
|
+
for (const line of text.split("\n")) {
|
|
99
|
+
const t = line.trim();
|
|
100
|
+
if (t) return t;
|
|
101
|
+
}
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 用于文本匹配的归一化:NFKC + 小写 + 空白压缩。 */
|
|
106
|
+
export function normalizeMatchText(text: string): string {
|
|
107
|
+
return text.normalize("NFKC").toLocaleLowerCase().replace(/\s+/g, " ").trim();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function hasToolCall(content: unknown): boolean {
|
|
111
|
+
const parts = Array.isArray(content) ? content : [];
|
|
112
|
+
return parts.some((p) => p && typeof p === "object" && (p as { type?: string }).type === "toolCall");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* assistant 消息是否会写 OSC 133;A 标记。镜像 pi AssistantMessageComponent:
|
|
117
|
+
* - 含 toolCall:不写标记
|
|
118
|
+
* - 否则只有渲染出至少一行才写(组件首行带标记;0 行时直接返回)
|
|
119
|
+
* - 会渲染的内容:非空白 text、非空白 thinking,或 aborted/error 的错误提示行
|
|
120
|
+
* 内容为空/纯空白的消息不渲染,也就没有标记(此前误算一个,会让之后所有序号偏移)。
|
|
121
|
+
*/
|
|
122
|
+
function assistantWritesMarker(msg: { content?: unknown; stopReason?: string }): boolean {
|
|
123
|
+
if (hasToolCall(msg.content)) return false;
|
|
124
|
+
if (msg.stopReason === "aborted" || msg.stopReason === "error" || msg.stopReason === "length") return true;
|
|
125
|
+
const parts = Array.isArray(msg.content) ? msg.content : [];
|
|
126
|
+
return parts.some((p) => {
|
|
127
|
+
if (!p || typeof p !== "object") return false;
|
|
128
|
+
const part = p as { type?: string; text?: unknown; thinking?: unknown };
|
|
129
|
+
if (part.type === "text") return typeof part.text === "string" && part.text.trim().length > 0;
|
|
130
|
+
if (part.type === "thinking") return typeof part.thinking === "string" && part.thinking.trim().length > 0;
|
|
131
|
+
return false;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 活动上下文条目的兜底实现(当 sessionManager.buildContextEntries() 不可用时)。
|
|
137
|
+
* 与 pi `buildContextEntries()` 语义一致:最新 compaction + firstKeptEntryId 起的保留区间
|
|
138
|
+
* (跳过 system 消息)+ compaction 之后的条目。
|
|
139
|
+
*/
|
|
140
|
+
export function activeContextEntries(branch: SessionEntryLike[]): SessionEntryLike[] {
|
|
141
|
+
let compactionIndex = -1;
|
|
142
|
+
for (let i = 0; i < branch.length; i++) {
|
|
143
|
+
if (branch[i]?.type === "compaction") compactionIndex = i;
|
|
144
|
+
}
|
|
145
|
+
if (compactionIndex < 0) return branch;
|
|
146
|
+
|
|
147
|
+
const compaction = branch[compactionIndex]!;
|
|
148
|
+
const keptIndex = branch.findIndex((entry) => entry.id === compaction.firstKeptEntryId);
|
|
149
|
+
const out: SessionEntryLike[] = [compaction];
|
|
150
|
+
if (keptIndex >= 0 && keptIndex < compactionIndex) {
|
|
151
|
+
for (const entry of branch.slice(keptIndex, compactionIndex)) {
|
|
152
|
+
if (entry.type === "message" && entry.message?.role === "system") continue;
|
|
153
|
+
out.push(entry);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
out.push(...branch.slice(compactionIndex + 1));
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 工具结果文本:多段 text 按行拼接(仅用于回合分析展示,与 pi 的标记规则无关)。 */
|
|
161
|
+
function toolResultText(content: unknown): string {
|
|
162
|
+
return joinTextParts(content, "\n");
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** 由用户消息条目构造提问项;该消息不渲染 UserMessageComponent(无标记)时返回 null。 */
|
|
166
|
+
function questionFromEntry(entry: SessionEntryLike, index: number): QuestionItem | null {
|
|
167
|
+
const text = renderedUserText(entry.message?.content);
|
|
168
|
+
if (!text) return null;
|
|
169
|
+
return {
|
|
170
|
+
index,
|
|
171
|
+
...(entry.id ? { entryId: entry.id } : {}),
|
|
172
|
+
text,
|
|
173
|
+
summary: firstLine(text) || "[empty message]",
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 从活动上下文条目收集可跳转的用户提问(不渲染 UserMessageComponent 的消息没有标记,排除)。 */
|
|
178
|
+
export function collectQuestionsFromEntries(entries: SessionEntryLike[]): QuestionItem[] {
|
|
179
|
+
const out: QuestionItem[] = [];
|
|
180
|
+
for (const entry of entries) {
|
|
181
|
+
if (entry.type !== "message" || entry.message?.role !== "user") continue;
|
|
182
|
+
const question = questionFromEntry(entry, out.length);
|
|
183
|
+
if (question) out.push(question);
|
|
184
|
+
}
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** 目标之前、归一化文本相同的提问数量(文本匹配兜底用)。 */
|
|
189
|
+
export function occurrenceOf(questions: readonly { text: string }[], index: number): number {
|
|
190
|
+
const normalized = normalizeMatchText(questions[index]!.text);
|
|
191
|
+
return questions.slice(0, index).filter((q) => normalizeMatchText(q.text) === normalized).length;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 面板打开时的提问列表(snapshot)里第 index 条,在当前列表(current)里的下标;找不到返回 -1。
|
|
196
|
+
* 面板开着时 agent 可能继续输出或发生压缩,序号会变。
|
|
197
|
+
* - 有会话条目 id:按 id 精确找回;id 不在当前列表里说明它已被压缩掉,返回 -1,不去猜同文本的另一条
|
|
198
|
+
* - 没有 id(旧版 pi 兜底):按文本匹配。压缩只会删掉靠前的条目,所以保持「它之后还有几条同文本」不变,
|
|
199
|
+
* 由此从后往前定位,避免重复提问时 nth 超出当前数量
|
|
200
|
+
*/
|
|
201
|
+
export function remapQuestionIndex(
|
|
202
|
+
snapshot: readonly QuestionItem[],
|
|
203
|
+
index: number,
|
|
204
|
+
current: readonly QuestionItem[],
|
|
205
|
+
): number {
|
|
206
|
+
const item = snapshot[index];
|
|
207
|
+
if (!item) return -1;
|
|
208
|
+
if (item.entryId) return current.findIndex((q) => q.entryId === item.entryId);
|
|
209
|
+
// 纯追加(快照是当前列表的前缀):下标不变
|
|
210
|
+
const sameTextAt = (a: QuestionItem | undefined, b: QuestionItem) =>
|
|
211
|
+
a !== undefined && normalizeMatchText(a.text) === normalizeMatchText(b.text);
|
|
212
|
+
if (snapshot.every((q, i) => sameTextAt(current[i], q))) return index;
|
|
213
|
+
|
|
214
|
+
const target = normalizeMatchText(item.text);
|
|
215
|
+
const same: number[] = [];
|
|
216
|
+
current.forEach((q, i) => {
|
|
217
|
+
if (normalizeMatchText(q.text) === target) same.push(i);
|
|
218
|
+
});
|
|
219
|
+
if (same.length === 0) return -1;
|
|
220
|
+
// 压缩删掉靠前的重复项:从后往前数的位置保持不变,所以取「快照里它之后还有几条同文本」对应的位置
|
|
221
|
+
const laterInSnapshot = snapshot.slice(index + 1).filter((q) => normalizeMatchText(q.text) === target).length;
|
|
222
|
+
const fromEnd = same.length - 1 - laterInSnapshot;
|
|
223
|
+
// 此前的同文本项都已被压缩掉:这条提问也已不在活动上下文里
|
|
224
|
+
return fromEnd < 0 ? -1 : same[fromEnd]!;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 推算「第 target 条可跳转提问」在 transcript OSC 133;A 标记序列中的序号(0 起)。
|
|
229
|
+
* 标记产生规则(均按 UserMessageComponent / AssistantMessageComponent 的 render):
|
|
230
|
+
* - user 消息:有文本(会渲染组件)时恰好一个标记;无文本则没有
|
|
231
|
+
* - assistant 消息:见 assistantWritesMarker(无 toolCall 且会渲染出内容时一个标记)
|
|
232
|
+
* - 其他 role(toolResult/custom/bashExecution/…):不产生标记
|
|
233
|
+
*/
|
|
234
|
+
export function markerOrdinalFromEntries(entries: SessionEntryLike[], target: number): number | null {
|
|
235
|
+
if (target < 0) return null;
|
|
236
|
+
let markerOrdinal = 0;
|
|
237
|
+
let questionOrdinal = 0;
|
|
238
|
+
for (const entry of entries) {
|
|
239
|
+
if (entry.type !== "message") continue;
|
|
240
|
+
const msg = entry.message;
|
|
241
|
+
if (!msg) continue;
|
|
242
|
+
|
|
243
|
+
if (msg.role === "user") {
|
|
244
|
+
if (!questionFromEntry(entry, 0)) continue;
|
|
245
|
+
if (questionOrdinal === target) return markerOrdinal;
|
|
246
|
+
questionOrdinal++;
|
|
247
|
+
markerOrdinal++;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (msg.role === "assistant") {
|
|
252
|
+
if (assistantWritesMarker(msg)) markerOrdinal++;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** 构建第 ordinal 条提问(0 起)的回合分析数据。找不到提问时返回 null。 */
|
|
259
|
+
export function buildTurnFromEntries(entries: SessionEntryLike[], ordinal: number): TurnData | null {
|
|
260
|
+
let questionOrdinal = 0;
|
|
261
|
+
let question: QuestionItem | null = null;
|
|
262
|
+
const nodes: TurnNode[] = [];
|
|
263
|
+
const pendingTools = new Map<string, TurnNode & { kind: "tool" }>();
|
|
264
|
+
|
|
265
|
+
for (const entry of entries) {
|
|
266
|
+
if (entry.type !== "message") continue;
|
|
267
|
+
const msg = entry.message;
|
|
268
|
+
if (!msg) continue;
|
|
269
|
+
|
|
270
|
+
// 到达下一条用户消息:回合结束
|
|
271
|
+
if (msg.role === "user") {
|
|
272
|
+
if (question) break;
|
|
273
|
+
const candidate = questionFromEntry(entry, ordinal);
|
|
274
|
+
if (!candidate) continue;
|
|
275
|
+
if (questionOrdinal === ordinal) question = candidate;
|
|
276
|
+
questionOrdinal++;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (!question) continue;
|
|
280
|
+
|
|
281
|
+
if (msg.role === "assistant") {
|
|
282
|
+
const parts = Array.isArray(msg.content) ? msg.content : [];
|
|
283
|
+
for (const part of parts) {
|
|
284
|
+
const p = part as { type?: string };
|
|
285
|
+
if (p.type === "thinking") {
|
|
286
|
+
const t = (part as { thinking?: string }).thinking ?? "";
|
|
287
|
+
nodes.push({ kind: "thinking", id: nodes.length, text: t });
|
|
288
|
+
} else if (p.type === "toolCall") {
|
|
289
|
+
const tc = part as { id?: string; name?: string; arguments?: unknown };
|
|
290
|
+
const node: TurnNode & { kind: "tool" } = {
|
|
291
|
+
kind: "tool",
|
|
292
|
+
id: nodes.length,
|
|
293
|
+
name: tc.name ?? "(unknown)",
|
|
294
|
+
args: tc.arguments,
|
|
295
|
+
result: null,
|
|
296
|
+
isError: false,
|
|
297
|
+
};
|
|
298
|
+
nodes.push(node);
|
|
299
|
+
if (tc.id) pendingTools.set(tc.id, node);
|
|
300
|
+
} else if (p.type === "text") {
|
|
301
|
+
const t = ((part as { text?: string }).text ?? "").trim();
|
|
302
|
+
if (t) nodes.push({ kind: "text", id: nodes.length, text: t, isFinal: false });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
} else if (msg.role === "toolResult") {
|
|
306
|
+
const node = msg.toolCallId ? pendingTools.get(msg.toolCallId) : undefined;
|
|
307
|
+
if (node) {
|
|
308
|
+
node.result = toolResultText(msg.content).trim() || "(empty)";
|
|
309
|
+
node.isError = msg.isError === true;
|
|
310
|
+
pendingTools.delete(msg.toolCallId!);
|
|
311
|
+
}
|
|
312
|
+
} else if (msg.role === "bashExecution") {
|
|
313
|
+
nodes.push({
|
|
314
|
+
kind: "tool",
|
|
315
|
+
id: nodes.length,
|
|
316
|
+
name: "!bash",
|
|
317
|
+
args: msg.command ?? "",
|
|
318
|
+
result: (msg.output ?? "").trim() || "(empty)",
|
|
319
|
+
isError: msg.exitCode !== undefined && msg.exitCode !== 0,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (!question) return null;
|
|
325
|
+
// 回合内最后一个文本节点标记为「输出」
|
|
326
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
327
|
+
if (nodes[i].kind === "text") {
|
|
328
|
+
(nodes[i] as { isFinal: boolean }).isFinal = true;
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return { question, nodes };
|
|
333
|
+
}
|