@each1024/pi-mini-mode 2.0.9 → 2.0.11
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/README.md +11 -8
- package/README.zh-CN.md +8 -10
- package/extensions/footer-status.ts +46 -1189
- package/lib/agent-render.ts +203 -0
- package/lib/agent-state.ts +199 -0
- package/lib/agent-view.ts +5 -519
- package/lib/agent-widgets.ts +129 -0
- package/lib/footer-line.ts +206 -0
- package/lib/input-editor-paths.ts +79 -0
- package/lib/input-editor.ts +184 -0
- package/lib/input-enhancements-types.ts +51 -0
- package/lib/input-enhancements.ts +15 -797
- package/lib/input-hover-preview.ts +149 -0
- package/lib/input-image-graphics.ts +34 -0
- package/lib/input-image-paths.ts +120 -0
- package/lib/input-inline-images.ts +114 -0
- package/lib/input-skill-completion.ts +132 -0
- package/lib/minimal-output.ts +384 -0
- package/lib/minimal-turns.ts +203 -0
- package/lib/settings-web.ts +9 -2
- package/lib/settings.html +7 -6
- package/lib/settings.ts +153 -0
- package/lib/thinking-preview.ts +25 -21
- package/lib/thinking-stream.ts +96 -0
- package/lib/transcript-adapter.ts +15 -501
- package/lib/transcript-cards.ts +67 -0
- package/lib/transcript-notice-render.ts +211 -0
- package/lib/transcript-notices.ts +238 -0
- package/lib/usage.ts +137 -0
- package/package.json +4 -2
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { getMarkdownTheme, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { Text, truncateToWidth, visibleWidth, type TuiMouseEvent } from "@earendil-works/pi-tui";
|
|
3
|
+
import { stripVTControlCharacters } from "node:util";
|
|
4
|
+
import { agentCallDisplay, isAgentTool, liveAgentView, runningGlyph } from "./agent-view.ts";
|
|
5
|
+
import { createAssistantImages, imagePlaceholders } from "./assistant-images.ts";
|
|
6
|
+
import { linkToolFiles } from "./file-links.ts";
|
|
7
|
+
import { diagramMarkdown, isFencedMarkdown, renderMinimalMarkdown } from "./minimal-markdown.ts";
|
|
8
|
+
import { minimalSurface, paintExpandedHeading } from "./minimal-theme.ts";
|
|
9
|
+
import { formatElapsed, formatThinkingElapsed, formatToolElapsed, piLoopSummary, type MinimalTurn } from "./minimal-turns.ts";
|
|
10
|
+
import { DEFAULT_SETTINGS } from "./settings.ts";
|
|
11
|
+
import { thinkingPreview } from "./thinking-preview.ts";
|
|
12
|
+
import type { NoticeRows, TurnNotices } from "./transcript-adapter.ts";
|
|
13
|
+
import { addUsage, EMPTY_USAGE, formatTokens } from "./usage.ts";
|
|
14
|
+
|
|
15
|
+
export function minimalOutputComponent(theme: ExtensionContext["ui"]["theme"], getTurns: () => MinimalTurn[], isExpanded: () => boolean = () => false, showUsage: () => boolean = () => true, subAgentsExpanded: () => boolean = () => false, agentDeadlines = new Map<string, number>(), transformText: (text: string) => string = text => text, imageOptions?: { cwd(): string; requestRender(): void; write?(data: string): void }, getRecordLimit: () => number = () => DEFAULT_SETTINGS["pi-mini-mode-minimal-record-limit"]) {
|
|
16
|
+
const placeholderText = (text: string) => imageOptions ? imagePlaceholders(text, imageOptions.cwd()) : text;
|
|
17
|
+
const markdown = (text: string, width: number, process = false) => renderMinimalMarkdown(text, width, getMarkdownTheme(), theme.getBgAnsi?.("userMessageBg") ?? "",
|
|
18
|
+
{ color: (value) => theme.fg(process ? "muted" : "text", value) }, (source, available) => transformText(placeholderText(diagramMarkdown(source, available))));
|
|
19
|
+
const assistantImages = imageOptions && createAssistantImages(imageOptions.cwd, imageOptions.requestRender, imageOptions.write);
|
|
20
|
+
const assistantMarkdown = (text: string, width: number, owner: string) => assistantImages ? assistantImages.render(text, width, markdown, owner) : markdown(text, width);
|
|
21
|
+
const surface = (rows: string[], width: number, user: boolean, selected = -1, expanded = false) => {
|
|
22
|
+
const padding = Math.min(2, Math.floor((width - 1) / 2));
|
|
23
|
+
return ["", ...rows, ""].map((row, index) => {
|
|
24
|
+
const line = truncateToWidth(" ".repeat(padding) + row, width, "");
|
|
25
|
+
return minimalSurface(theme, line + " ".repeat(Math.max(0, width - visibleWidth(line))), user, expanded && index === selected);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
let agentExpiry = Infinity;
|
|
29
|
+
const expandedPrompts = new Map<number, string>();
|
|
30
|
+
const expandedSubagents = new Set<string>();
|
|
31
|
+
const expandedTools = new Set<string>();
|
|
32
|
+
const expandedThinking = new Set<string>();
|
|
33
|
+
const thinkingPreviews = new Map<string, { text: string; source: string; width: number; rows: number; live: boolean }>();
|
|
34
|
+
const agentModes = new Map<number, "all" | "preview">();
|
|
35
|
+
const runningTurns = new Set<number>();
|
|
36
|
+
let pinnedToolId: string | undefined;
|
|
37
|
+
let pinnedTool: { id: string; y: number; line: string } | undefined;
|
|
38
|
+
let pinnedAgentIndex: number | undefined;
|
|
39
|
+
let pinnedAgent: { index: number; y: number; line: string } | undefined;
|
|
40
|
+
let pinnedSubagentId: string | undefined;
|
|
41
|
+
let pinnedSubagent: { id: string; y: number; line: string; autoScroll?: boolean } | undefined;
|
|
42
|
+
let toolControls: Array<{ id: string; y: number; width: number; title: string }> = [];
|
|
43
|
+
let agentControls: Array<{ index: number; y: number; width: number }> = [];
|
|
44
|
+
let hoveredTool: typeof toolControls[number] | undefined;
|
|
45
|
+
const clearHover = () => { const changed = !!hoveredTool; hoveredTool = undefined; return changed; };
|
|
46
|
+
const toggleTool = (id: string) => {
|
|
47
|
+
if (id.startsWith("thinking:")) {
|
|
48
|
+
const present = getTurns().some((turn, turnIndex) => turn.process.some((entry, processIndex) =>
|
|
49
|
+
entry.startsWith("thinking") && `thinking:${turnIndex}:${processIndex}` === id));
|
|
50
|
+
if (!present) return;
|
|
51
|
+
if (expandedThinking.has(id)) expandedThinking.delete(id);
|
|
52
|
+
else expandedThinking.add(id);
|
|
53
|
+
clearHover();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (id.startsWith("agents:")) {
|
|
57
|
+
const index = Number(id.slice(7));
|
|
58
|
+
if (!Number.isInteger(index) || !getTurns()[index]) return;
|
|
59
|
+
const all = (agentModes.get(index) ?? (isExpanded() ? "all" : "preview")) === "all";
|
|
60
|
+
agentModes.set(index, all ? "preview" : "all");
|
|
61
|
+
if (all) { expandedThinking.clear(); expandedTools.clear(); expandedSubagents.clear(); }
|
|
62
|
+
pinnedAgentIndex = all ? undefined : index;
|
|
63
|
+
clearHover();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!getTurns().some(turn => turn.agentCalls?.some(call => call.id === id && !isAgentTool(call.tool ?? call.name)))) return;
|
|
67
|
+
if (expandedTools.has(id)) {
|
|
68
|
+
expandedTools.delete(id);
|
|
69
|
+
if (pinnedToolId === id) pinnedToolId = undefined;
|
|
70
|
+
} else {
|
|
71
|
+
expandedTools.add(id);
|
|
72
|
+
// Only the latest opened tool owns the sticky heading; others stay open.
|
|
73
|
+
pinnedToolId = id;
|
|
74
|
+
}
|
|
75
|
+
clearHover();
|
|
76
|
+
};
|
|
77
|
+
const toggleSubagent = (runId: string) => {
|
|
78
|
+
if (!subagentControls.some(control => control.runId === runId)) return;
|
|
79
|
+
if (expandedSubagents.has(runId)) {
|
|
80
|
+
expandedSubagents.delete(runId);
|
|
81
|
+
if (pinnedSubagentId === runId) pinnedSubagentId = undefined;
|
|
82
|
+
} else {
|
|
83
|
+
expandedSubagents.add(runId);
|
|
84
|
+
// SubAgent details open in place; sticky heading engages only after scrolling.
|
|
85
|
+
pinnedSubagentId = runId;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
let promptControls: Array<{ index: number; question: string; y: number; x: number; width: number; label: string }> = [];
|
|
89
|
+
let subagentControls: Array<{ runId: string; y: number; width: number; line: string }> = [];
|
|
90
|
+
let noticeRegions: Array<{ y: number; rows: NoticeRows }> = [];
|
|
91
|
+
const togglePrompt = (index: number, question: string) => {
|
|
92
|
+
if (getTurns()[index]?.question !== question) return;
|
|
93
|
+
if (expandedPrompts.get(index) === question) expandedPrompts.delete(index);
|
|
94
|
+
else expandedPrompts.set(index, question);
|
|
95
|
+
};
|
|
96
|
+
return {
|
|
97
|
+
dispose() { assistantImages?.dispose(); },
|
|
98
|
+
invalidate() { clearHover(); },
|
|
99
|
+
resetProcessView() {
|
|
100
|
+
agentModes.clear(); expandedThinking.clear(); expandedTools.clear(); expandedSubagents.clear();
|
|
101
|
+
thinkingPreviews.clear();
|
|
102
|
+
pinnedToolId = undefined; pinnedAgentIndex = undefined; pinnedSubagentId = undefined;
|
|
103
|
+
},
|
|
104
|
+
clearHover,
|
|
105
|
+
pinnedTool: () => pinnedTool,
|
|
106
|
+
unpinTool: () => { pinnedToolId = undefined; pinnedTool = undefined; },
|
|
107
|
+
pinnedAgent: () => pinnedAgent,
|
|
108
|
+
unpinAgent: () => { pinnedAgentIndex = undefined; pinnedAgent = undefined; },
|
|
109
|
+
pinnedSubagent: () => pinnedSubagent,
|
|
110
|
+
unpinSubagent: () => { pinnedSubagentId = undefined; pinnedSubagent = undefined; },
|
|
111
|
+
toolChoices: () => toolControls.map(control => ({ ...control, expanded: expandedTools.has(control.id) || expandedThinking.has(control.id) })),
|
|
112
|
+
toggleTool,
|
|
113
|
+
toggleSubagent,
|
|
114
|
+
agentExpiry: () => agentExpiry,
|
|
115
|
+
promptChoices: () => promptControls.map(control => ({ ...control })),
|
|
116
|
+
togglePrompt,
|
|
117
|
+
handleMouse(event: TuiMouseEvent) {
|
|
118
|
+
const tool = toolControls.find(control => event.y === control.y && event.x >= 2 && event.x < Math.min(5, control.width));
|
|
119
|
+
if (event.type === "move") {
|
|
120
|
+
const next = !event.shift && !event.ctrl && !event.alt ? tool : undefined;
|
|
121
|
+
if (hoveredTool?.id !== next?.id) {
|
|
122
|
+
hoveredTool = next;
|
|
123
|
+
return { handled: true, render: true };
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (event.type === "wheel" || event.type === "drag") { clearHover(); return; }
|
|
128
|
+
if (tool && event.button === "left" && !event.shift && !event.ctrl && !event.alt) {
|
|
129
|
+
if (event.type === "press") return { handled: true };
|
|
130
|
+
if (event.type === "click") {
|
|
131
|
+
toggleTool(tool.id);
|
|
132
|
+
return { handled: true, render: true };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (event.button !== "left" || event.shift || event.ctrl || event.alt) return;
|
|
136
|
+
const agent = agentControls.find(control => event.y === control.y && event.x >= 0 && event.x < control.width);
|
|
137
|
+
if (agent && (event.clickCount ?? 1) === 1) {
|
|
138
|
+
if (event.type === "press") return { handled: true };
|
|
139
|
+
if (event.type === "click") {
|
|
140
|
+
const all = (agentModes.get(agent.index) ?? (isExpanded() ? "all" : "preview")) === "all";
|
|
141
|
+
agentModes.set(agent.index, all ? "preview" : "all");
|
|
142
|
+
if (all) { expandedThinking.clear(); expandedTools.clear(); expandedSubagents.clear(); }
|
|
143
|
+
pinnedAgentIndex = all ? undefined : agent.index;
|
|
144
|
+
return { handled: true, render: true };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const control = promptControls.find(control => event.y === control.y && event.x >= control.x && event.x < control.x + control.width);
|
|
148
|
+
if (control && (event.clickCount ?? 1) === 1) {
|
|
149
|
+
if (event.type === "press") return { handled: true };
|
|
150
|
+
if (event.type === "click") {
|
|
151
|
+
togglePrompt(control.index, control.question);
|
|
152
|
+
return { handled: true, render: true };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const sub = subagentControls.find(control => event.y === control.y && event.x >= 0 && event.x < control.width);
|
|
156
|
+
if (sub) {
|
|
157
|
+
if (event.type === "press") return { handled: true };
|
|
158
|
+
if (event.type === "click") {
|
|
159
|
+
toggleSubagent(sub.runId);
|
|
160
|
+
return { handled: true, render: true };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if ((event.clickCount ?? 1) !== 1) return;
|
|
164
|
+
const region = noticeRegions.find(region => event.y >= region.y && event.y < region.y + region.rows.length);
|
|
165
|
+
return region?.rows.handleMouse?.({ ...event, y: event.y - region.y });
|
|
166
|
+
},
|
|
167
|
+
render(width: number, notices?: TurnNotices): string[] {
|
|
168
|
+
promptControls = [];
|
|
169
|
+
toolControls = [];
|
|
170
|
+
agentControls = [];
|
|
171
|
+
pinnedTool = undefined;
|
|
172
|
+
pinnedAgent = undefined;
|
|
173
|
+
pinnedSubagent = undefined;
|
|
174
|
+
subagentControls = [];
|
|
175
|
+
noticeRegions = [];
|
|
176
|
+
agentExpiry = Infinity;
|
|
177
|
+
if (width <= 0) { clearHover(); return []; }
|
|
178
|
+
const turns = getTurns();
|
|
179
|
+
const toolIds = new Set(turns.flatMap(turn => (turn.agentCalls ?? []).map(call => call.id)));
|
|
180
|
+
for (const id of expandedTools) if (!toolIds.has(id)) expandedTools.delete(id);
|
|
181
|
+
const thinkingIds = new Set(turns.flatMap((turn, turnIndex) => turn.process.flatMap((entry, processIndex) =>
|
|
182
|
+
entry.startsWith("thinking") ? [`thinking:${turnIndex}:${processIndex}`] : [])));
|
|
183
|
+
for (const id of expandedThinking) if (!thinkingIds.has(id)) expandedThinking.delete(id);
|
|
184
|
+
for (const id of thinkingPreviews.keys()) if (!thinkingIds.has(id)) thinkingPreviews.delete(id);
|
|
185
|
+
const inner = Math.max(1, width - 2 * Math.min(2, Math.floor((width - 1) / 2)));
|
|
186
|
+
const lines: string[] = [];
|
|
187
|
+
// An open heading wears the same full-width selectedBg band as an expanded SubAgent row.
|
|
188
|
+
const bandHeading = (row: string, open: boolean) => open
|
|
189
|
+
? paintExpandedHeading(theme, row + " ".repeat(Math.max(0, width - visibleWidth(row)))) : row;
|
|
190
|
+
for (const [index, turn] of turns.entries()) {
|
|
191
|
+
if (index > 0) lines.push("");
|
|
192
|
+
if (expandedPrompts.has(index) && expandedPrompts.get(index) !== turn.question) expandedPrompts.delete(index);
|
|
193
|
+
const noticeRows = notices?.get(index) ?? [];
|
|
194
|
+
const questionRows = markdown(turn.question, inner);
|
|
195
|
+
const loopSummary = piLoopSummary(turn.question);
|
|
196
|
+
const expanded = expandedPrompts.get(index) === turn.question;
|
|
197
|
+
// A fence is one visual unit; slicing it mid-block looks like raw source.
|
|
198
|
+
const keepFence = !loopSummary && isFencedMarkdown(turn.question);
|
|
199
|
+
// Loop wakeups are system-generated user messages. Collapse them even when
|
|
200
|
+
// their short prompt would otherwise fit the normal four-row allowance.
|
|
201
|
+
const userRows = expanded || keepFence ? [...questionRows] : loopSummary ? markdown(loopSummary, inner) : questionRows.slice(0, 4);
|
|
202
|
+
let controlIndex = -1;
|
|
203
|
+
if (!keepFence && (loopSummary || questionRows.length > 4)) {
|
|
204
|
+
const label = expanded ? "▴ 收起" : loopSummary ? "▾ 展开" : `▾ 展开 (${questionRows.length} 行)`;
|
|
205
|
+
const control = truncateToWidth(label, inner, "");
|
|
206
|
+
promptControls.push({ index, question: turn.question, y: lines.length + 1 + userRows.length,
|
|
207
|
+
x: 0, width, label });
|
|
208
|
+
userRows.push(theme.fg("accent", control));
|
|
209
|
+
// The expanded control is this message's heading, so it keeps the band in place of the user surface.
|
|
210
|
+
controlIndex = userRows.length;
|
|
211
|
+
}
|
|
212
|
+
lines.push(...surface(userRows, width, true, controlIndex, expanded));
|
|
213
|
+
const entries = turn.process.flatMap((entry, processIndex) => {
|
|
214
|
+
if (entry.startsWith("call ")) {
|
|
215
|
+
const call = turn.agentCalls?.find(call => call.id === entry.slice(5));
|
|
216
|
+
if (call && isAgentTool(call.tool ?? call.name)) return [];
|
|
217
|
+
const display = call && agentCallDisplay(call);
|
|
218
|
+
return call && display ? [{ title: `${isAgentTool(call.tool ?? call.name) ? "Control" : call.name} ${display.summary}`.trim(), detail: display.detail, state: call.state, id: call.id, thinking: false, processIndex, activeThinking: false }] : [];
|
|
219
|
+
}
|
|
220
|
+
const part = entry.match(/^(tool|output|thinking|skill)(?:\s+|$)([\s\S]*)/);
|
|
221
|
+
const thinking = part?.[1] === "thinking";
|
|
222
|
+
const activeThinking = thinking && turn.running && turn.thinking === processIndex;
|
|
223
|
+
const failedThinking = thinking && /^(error|aborted)$/.test(turn.thinkingOutcomes?.[processIndex] ?? "");
|
|
224
|
+
const label = ({ tool: "Tool", output: "Output", thinking: "Thinking", skill: "Skill" } as Record<string, string>)[part?.[1] ?? ""] ?? "Process";
|
|
225
|
+
return [{ title: `${label} ${part?.[2] ?? entry}`, detail: part?.[2] ?? entry, state: failedThinking ? "error" : activeThinking ? "running" : "done", id: thinking ? `thinking:${index}:${processIndex}` : "", thinking, processIndex, activeThinking }];
|
|
226
|
+
});
|
|
227
|
+
// Older in-memory turns may predate call markers.
|
|
228
|
+
for (const call of turn.agentCalls ?? []) {
|
|
229
|
+
if (isAgentTool(call.tool ?? call.name)) continue;
|
|
230
|
+
const display = agentCallDisplay(call);
|
|
231
|
+
if (!entries.some(entry => entry.id === call.id)) entries.push({ title: `${isAgentTool(call.tool ?? call.name) ? "Control" : call.name} ${display.summary}`, detail: display.detail, state: call.state, id: call.id, thinking: false, processIndex: -1, activeThinking: false });
|
|
232
|
+
}
|
|
233
|
+
const agentTurnControls: Array<{ runId: string; y: number; width: number; line: string }> = [];
|
|
234
|
+
const agents = liveAgentView(turn.subAgents ?? [], theme, width, false, true, agentDeadlines, Date.now(), expandedSubagents, agentTurnControls);
|
|
235
|
+
const active = !!turn.running || agents.running > 0 || entries.some(entry => entry.state === "running") || (turn.waitingTools?.length ?? 0) > 0;
|
|
236
|
+
if (active) runningTurns.add(index);
|
|
237
|
+
else if (runningTurns.delete(index)) {
|
|
238
|
+
// Return to the configured preview once; a later click can expand everything again.
|
|
239
|
+
if (agentModes.get(index) === "all") {
|
|
240
|
+
agentModes.set(index, "preview");
|
|
241
|
+
expandedThinking.clear();
|
|
242
|
+
expandedTools.clear();
|
|
243
|
+
expandedSubagents.clear();
|
|
244
|
+
if (pinnedAgentIndex === index) pinnedAgentIndex = undefined;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (entries.length || active || turn.usage || agents.total) {
|
|
248
|
+
const mode = agentModes.get(index) ?? (isExpanded() ? "all" : "preview");
|
|
249
|
+
const expanded = mode === "all";
|
|
250
|
+
const busy = entries.some(entry => entry.state === "running") || (turn.waitingTools?.length ?? 0) > 0;
|
|
251
|
+
const working = turn.running && !busy && !turn.final;
|
|
252
|
+
const showAgentRows = (expanded || subAgentsExpanded()) && agents.total > 0;
|
|
253
|
+
// Every child keeps its heading; process records share the remaining preview budget.
|
|
254
|
+
const processLimit = Math.max(0, getRecordLimit() - (showAgentRows ? agents.total : 0));
|
|
255
|
+
const shown = expanded ? entries : processLimit ? entries.slice(-processLimit) : [];
|
|
256
|
+
const showWorking = working && (turn.awaitingResponse || expanded || (!shown.length && !agents.total));
|
|
257
|
+
const sparkle = active ? Math.floor(Date.now() / 360) % 2 ? "✧" : "✦" : "✦";
|
|
258
|
+
const usage = addUsage({ ...EMPTY_USAGE, ...turn.usage, cost: turn.usage?.cost ?? 0 }, turn.pendingUsage);
|
|
259
|
+
const hasUsage = usage.totalTokens > 0 || usage.cacheRead > 0;
|
|
260
|
+
const totals = hasUsage
|
|
261
|
+
? theme.fg("muted", "会话 ") + theme.bold(theme.fg("text", formatTokens(usage.totalTokens)))
|
|
262
|
+
+ theme.fg("muted", " · 缓存 ") + theme.bold(theme.fg("text", formatTokens(usage.cacheRead)))
|
|
263
|
+
: "";
|
|
264
|
+
const recordCount = entries.length + agents.total;
|
|
265
|
+
const progressHeader = theme.fg(active ? "accent" : "muted", sparkle + " ") + theme.bold(theme.fg("text", "Agent")) + theme.fg("muted", recordCount ? ` · ${recordCount}` : "");
|
|
266
|
+
const caret = theme.fg("muted", expanded ? " ▾" : " ▸");
|
|
267
|
+
const elapsedHeader = turn.startedAt == null ? "" : theme.fg("muted", " · ") + theme.fg(active ? "success" : "muted", formatElapsed(turn.startedAt, turn.endedAt ?? Date.now()));
|
|
268
|
+
const left = progressHeader + elapsedHeader + caret;
|
|
269
|
+
const showTotals = hasUsage && showUsage() && visibleWidth(left) + 2 + visibleWidth(totals) <= width;
|
|
270
|
+
agentControls.push({ index, y: lines.length + 1, width: Math.min(width, visibleWidth(left)) });
|
|
271
|
+
if (expanded && pinnedAgentIndex === index) pinnedAgent = { index, y: lines.length + 1, line: truncateToWidth(left, width) };
|
|
272
|
+
lines.push("", showTotals
|
|
273
|
+
? left + " ".repeat(width - visibleWidth(left) - visibleWidth(totals)) + totals
|
|
274
|
+
: truncateToWidth(left, width));
|
|
275
|
+
shown.forEach((entry, row) => {
|
|
276
|
+
const title = entry.title;
|
|
277
|
+
const text = title.split(/\r?\n/).find(line => line.trim())?.replace(/\s+/g, " ").trim() ?? "";
|
|
278
|
+
const split = text.indexOf(" ");
|
|
279
|
+
const noThinkingBody = entry.thinking && !stripVTControlCharacters(entry.detail).trim();
|
|
280
|
+
const label = split < 0 ? text : text.slice(0, split);
|
|
281
|
+
const body = split < 0 ? "" : text.slice(split + 1);
|
|
282
|
+
const activeThinking = !!entry.activeThinking;
|
|
283
|
+
const elapsed = entry.thinking ? formatThinkingElapsed(turn, entry.processIndex, activeThinking) : "";
|
|
284
|
+
const thinkingElapsed = elapsed ? theme.fg(activeThinking ? "success" : "muted", ` ${elapsed}`) : "";
|
|
285
|
+
const call = turn.agentCalls?.find(call => call.id === entry.id && !isAgentTool(call.tool ?? call.name));
|
|
286
|
+
const controlId = call?.id ?? (entry.thinking && !noThinkingBody || entry.id.startsWith("agents:") ? entry.id : "");
|
|
287
|
+
const open = call ? expandedTools.has(call.id) : entry.thinking && !noThinkingBody && expandedThinking.has(entry.id);
|
|
288
|
+
const identity = entry.state === "error" ? "error" : open || activeThinking ? "accent" : "text";
|
|
289
|
+
const duration = call ? formatToolElapsed(call.startedAt,
|
|
290
|
+
call.endedAt ?? (call.state === "running" ? Date.now() : undefined)) : "";
|
|
291
|
+
const toolElapsed = duration ? theme.fg(entry.state === "error" ? "error" : call?.state === "running" ? "accent" : "muted", ` ${duration}`) : "";
|
|
292
|
+
const summary = theme.fg(identity, theme.bold(label)) + thinkingElapsed + toolElapsed + " ";
|
|
293
|
+
if (controlId && width >= 4) toolControls.push({ id: controlId, y: lines.length, width, title: text });
|
|
294
|
+
if (hoveredTool?.id === controlId && (hoveredTool.y !== lines.length || hoveredTool.width !== width)) clearHover();
|
|
295
|
+
const status = entry.state === "error" ? "✕" : entry.state === "running" ? runningGlyph() : "●";
|
|
296
|
+
const arrow = !!controlId && (open || hoveredTool?.id === controlId);
|
|
297
|
+
const keepStatus = entry.state === "running" || (open && entry.state === "error");
|
|
298
|
+
const glyph = arrow ? (open ? "▼" : "▶") + (keepStatus ? ` ${status}` : "") : status;
|
|
299
|
+
const glyphColor = entry.state === "error" ? "error" : entry.state === "running" || arrow ? "accent" : "muted";
|
|
300
|
+
const last = row === shown.length - 1 && !showWorking && !agents.rows.length;
|
|
301
|
+
const showGlyph = entry.state === "running" || entry.state === "error" || arrow;
|
|
302
|
+
const prefix = theme.fg(open ? "accent" : "dim", "│ ") + (showGlyph ? theme.fg(glyphColor, glyph) + " " : "") + summary;
|
|
303
|
+
if (label === "Output") {
|
|
304
|
+
// Wrapped output hangs under the body column and keeps the tree rail to the next sibling.
|
|
305
|
+
const indent = visibleWidth(prefix);
|
|
306
|
+
const rail = !last && indent > 0 ? theme.fg("dim", "\u2502") + " ".repeat(indent - 1) : " ".repeat(indent);
|
|
307
|
+
const wrapped = markdown(body, Math.max(1, width - indent), true);
|
|
308
|
+
lines.push(truncateToWidth(prefix + (wrapped[0] ?? ""), width, ""));
|
|
309
|
+
for (const extra of wrapped.slice(1)) lines.push(truncateToWidth(rail + extra, width, ""));
|
|
310
|
+
} else {
|
|
311
|
+
const available = Math.max(0, width - visibleWidth(prefix));
|
|
312
|
+
if (entry.thinking) {
|
|
313
|
+
const cached = thinkingPreviews.get(entry.id);
|
|
314
|
+
const previewRows = open ? 1 : 2;
|
|
315
|
+
const reuse = cached?.source === entry.detail && cached.width === available && cached.rows === previewRows && cached.live === activeThinking;
|
|
316
|
+
const display = reuse ? cached.text : thinkingPreview(entry.detail, available, previewRows, activeThinking);
|
|
317
|
+
if (!reuse) thinkingPreviews.set(entry.id, { text: display, source: entry.detail, width: available, rows: previewRows, live: activeThinking });
|
|
318
|
+
const outcome = turn.thinkingOutcomes?.[entry.processIndex];
|
|
319
|
+
const fallback = activeThinking ? "正在思考,等待可显示摘要"
|
|
320
|
+
: outcome === "aborted" ? "思考已中断,未返回可显示摘要"
|
|
321
|
+
: outcome === "error" ? "思考失败,未返回可显示摘要" : "思考已结束,未返回可显示摘要";
|
|
322
|
+
const color = entry.state === "error" ? "error" : activeThinking ? "accent" : "muted";
|
|
323
|
+
const rows = available > 0
|
|
324
|
+
? renderMinimalMarkdown(display || fallback, available, getMarkdownTheme(), theme.getBgAnsi?.("userMessageBg") ?? "",
|
|
325
|
+
{ color: value => theme.fg(color, value) }, (source, avail) => transformText(placeholderText(diagramMarkdown(source, avail))))
|
|
326
|
+
.slice(0, previewRows)
|
|
327
|
+
: [];
|
|
328
|
+
lines.push(bandHeading(truncateToWidth(prefix + (rows[0] ?? ""), width, ""), open));
|
|
329
|
+
const rail = theme.fg(last ? color : "dim", last ? " " : "│") + " ".repeat(Math.max(0, visibleWidth(prefix) - 1));
|
|
330
|
+
for (const extra of rows.slice(1)) lines.push(truncateToWidth(rail + extra, width, ""));
|
|
331
|
+
} else {
|
|
332
|
+
const displayBody = placeholderText(body);
|
|
333
|
+
const compactBody = linkToolFiles(displayBody, imageOptions?.cwd() ?? process.cwd(), !open);
|
|
334
|
+
const fittedBody = truncateToWidth(compactBody, available, "…");
|
|
335
|
+
// Always close a hyperlink before subsequent rows.
|
|
336
|
+
const styledBody = theme.fg(entry.state === "error" ? "error" : open ? "accent" : "muted", fittedBody + "\x1b]8;;\x07");
|
|
337
|
+
lines.push(bandHeading(truncateToWidth(prefix + styledBody, width, "…"), open));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (open && call && call.id === pinnedToolId) pinnedTool = { id: call.id, y: lines.length - 1, line: lines.at(-1)! };
|
|
341
|
+
if (open && call) {
|
|
342
|
+
// ponytail: saved text only; native view retains images, args and custom renderers.
|
|
343
|
+
const output = stripVTControlCharacters(call.output ?? "").replace(/\r\n?/g, "\n").replace(/[\x00-\x08\x0b-\x1f\x7f]/g, "");
|
|
344
|
+
const rail = width > 5 ? (last ? " " : theme.fg("accent", "│ ")) : "";
|
|
345
|
+
const detailWidth = Math.max(1, width - visibleWidth(rail));
|
|
346
|
+
if (call.task && (/\r|\n/.test(call.task) || visibleWidth(call.task) > width - visibleWidth(prefix))) {
|
|
347
|
+
const input = stripVTControlCharacters(call.task).replace(/\r\n?/g, "\n").replace(/[\x00-\x08\x0b-\x1f\x7f]/g, "");
|
|
348
|
+
lines.push(...new Text(`输入:\n${input}`, 0, 0).render(detailWidth).map(line => truncateToWidth(rail + theme.fg("muted", line), width, "")));
|
|
349
|
+
lines.push(truncateToWidth(rail + theme.fg("muted", "输出:"), width, ""));
|
|
350
|
+
}
|
|
351
|
+
const details = new Text(output || (entry.state === "running" ? "等待工具文本结果…" : "无文本结果"), 0, 0).render(detailWidth);
|
|
352
|
+
lines.push(...details.map(line => truncateToWidth(rail + theme.fg(entry.state === "error" ? "error" : "muted", line), width, "")));
|
|
353
|
+
} else if (open && entry.thinking) {
|
|
354
|
+
const rail = width > 5 ? (last ? " " : theme.fg("accent", "│ ")) : "";
|
|
355
|
+
const detailWidth = Math.max(1, width - visibleWidth(rail));
|
|
356
|
+
lines.push(...markdown(entry.detail, detailWidth, true).map(line => truncateToWidth(rail + line, width, "")));
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
if (showAgentRows) {
|
|
360
|
+
const agentStartY = lines.length;
|
|
361
|
+
for (const c of agentTurnControls) {
|
|
362
|
+
const control = { ...c, y: agentStartY + c.y };
|
|
363
|
+
subagentControls.push(control);
|
|
364
|
+
if (c.runId === pinnedSubagentId) pinnedSubagent = { id: c.runId, y: control.y, line: c.line, autoScroll: false };
|
|
365
|
+
}
|
|
366
|
+
lines.push(...agents.rows);
|
|
367
|
+
}
|
|
368
|
+
if (showWorking) {
|
|
369
|
+
const label = turn.awaitingResponse ? "Waiting" : "Working";
|
|
370
|
+
const prefix = theme.fg("dim", "└─ ") + theme.fg("accent", `${runningGlyph()} `) + theme.fg("text", theme.bold(label)) + " ";
|
|
371
|
+
lines.push(truncateToWidth(prefix + theme.fg("muted", turn.awaitingResponse ? "等待模型响应" : "等待下一步执行"), width, ""));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
for (const [replyIndex, reply] of (turn.replies ?? []).entries()) lines.push("", ...assistantMarkdown(reply, width, `reply:${index}:${replyIndex}`));
|
|
375
|
+
if (turn.final) lines.push("", ...assistantMarkdown(turn.final, width, `final:${index}`));
|
|
376
|
+
noticeRegions.push({ y: lines.length, rows: noticeRows });
|
|
377
|
+
lines.push(...noticeRows);
|
|
378
|
+
}
|
|
379
|
+
if (hoveredTool && !toolControls.some(control => control.id === hoveredTool!.id)) clearHover();
|
|
380
|
+
for (const deadline of agentDeadlines.values()) if (deadline > Date.now()) agentExpiry = Math.min(agentExpiry, deadline);
|
|
381
|
+
return lines;
|
|
382
|
+
},
|
|
383
|
+
};
|
|
384
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { agentCall, type AgentCall } from "./agent-view.ts";
|
|
2
|
+
import { addUsage, EMPTY_USAGE, type SessionUsage, type UsageLike } from "./usage.ts";
|
|
3
|
+
import { stripVTControlCharacters } from "node:util";
|
|
4
|
+
|
|
5
|
+
export interface MinimalTurn {
|
|
6
|
+
question: string;
|
|
7
|
+
usage?: SessionUsage;
|
|
8
|
+
pendingUsage?: UsageLike;
|
|
9
|
+
agentCalls?: AgentCall[];
|
|
10
|
+
subAgents?: Record<string, unknown>[];
|
|
11
|
+
process: string[];
|
|
12
|
+
final?: string;
|
|
13
|
+
replies?: string[];
|
|
14
|
+
running?: boolean;
|
|
15
|
+
/** Wall-clock start of this user-request execution; retained across tool and thinking turns. */
|
|
16
|
+
startedAt?: number;
|
|
17
|
+
endedAt?: number;
|
|
18
|
+
thinking?: number;
|
|
19
|
+
/** Per-process-index Thinking clocks. Live rows count; completed rows keep endedAt. */
|
|
20
|
+
thinkingClocks?: Array<ThinkingClock | undefined>;
|
|
21
|
+
thinkingOutcomes?: Array<ThinkingOutcome | undefined>;
|
|
22
|
+
awaitingResponse?: boolean;
|
|
23
|
+
waitingTools?: Array<{ id: string; name: string; startedAt: number }>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ThinkingClock {
|
|
27
|
+
startedAt: number;
|
|
28
|
+
/** Last thinking token, excluding delayed end frames and subsequent output. */
|
|
29
|
+
lastUpdatedAt?: number;
|
|
30
|
+
endedAt?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ThinkingOutcome = "done" | "error" | "aborted";
|
|
34
|
+
|
|
35
|
+
export function contentText(content: unknown): string {
|
|
36
|
+
if (typeof content === "string") return content.trim();
|
|
37
|
+
if (content && typeof content === "object" && "content" in content) return contentText(content.content);
|
|
38
|
+
if (!Array.isArray(content)) return "";
|
|
39
|
+
return content
|
|
40
|
+
.filter((item): item is { type?: string; text?: string } => Boolean(item && typeof item === "object"))
|
|
41
|
+
.filter((item) => item.type === "text" && typeof item.text === "string")
|
|
42
|
+
.map((item) => item.text!.trim())
|
|
43
|
+
.filter(Boolean)
|
|
44
|
+
.join("\n");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function processText(value: unknown): string {
|
|
48
|
+
return contentText(value) || (value === undefined ? "" : JSON.stringify(value) ?? "");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Format an execution duration without losing hours once a long run crosses one. */
|
|
52
|
+
export function formatElapsed(startedAt: number | undefined, now = Date.now()): string {
|
|
53
|
+
const seconds = Math.max(0, Math.floor((now - (startedAt ?? now)) / 1_000));
|
|
54
|
+
const minutes = Math.floor(seconds / 60);
|
|
55
|
+
const remainder = String(seconds % 60).padStart(2, "0");
|
|
56
|
+
return minutes >= 60 ? `${Math.floor(minutes / 60)}:${String(minutes % 60).padStart(2, "0")}:${remainder}` : `${minutes}:${remainder}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Show whole seconds only when both execution boundaries are known. */
|
|
60
|
+
export function formatToolElapsed(startedAt: number | undefined, endedAt: number | undefined): string {
|
|
61
|
+
if (startedAt === undefined || endedAt === undefined || !Number.isFinite(startedAt) || !Number.isFinite(endedAt)) return "";
|
|
62
|
+
const seconds = Math.max(0, Math.floor((endedAt - startedAt) / 1_000));
|
|
63
|
+
if (!seconds) return "";
|
|
64
|
+
const hours = Math.floor(seconds / 3_600);
|
|
65
|
+
const minutes = Math.floor(seconds / 60) % 60;
|
|
66
|
+
const remainder = seconds % 60;
|
|
67
|
+
return `${hours ? `${hours}h` : ""}${minutes ? `${minutes}m` : ""}${remainder ? `${remainder}s` : ""}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function ensureThinkingClock(turn: MinimalTurn, index: number, startedAt = Date.now()): ThinkingClock {
|
|
71
|
+
const clocks = turn.thinkingClocks ??= [];
|
|
72
|
+
return clocks[index] ??= { startedAt };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function freezeThinkingClock(turn: MinimalTurn | undefined, index: number | undefined, endedAt = Date.now(), outcome: ThinkingOutcome = "done"): void {
|
|
76
|
+
if (!turn || index === undefined) return;
|
|
77
|
+
const clock = turn.thinkingClocks?.[index];
|
|
78
|
+
if (clock && clock.endedAt === undefined) clock.endedAt = Math.max(clock.startedAt, Math.min(endedAt, clock.lastUpdatedAt ?? endedAt));
|
|
79
|
+
(turn.thinkingOutcomes ??= [])[index] ??= outcome;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function freezeOpenThinkingClocks(turn: MinimalTurn | undefined, endedAt = Date.now(), outcome: ThinkingOutcome = "done"): void {
|
|
83
|
+
if (!turn?.thinkingClocks) return;
|
|
84
|
+
for (const [index, clock] of turn.thinkingClocks.entries()) {
|
|
85
|
+
if (clock && clock.endedAt === undefined) freezeThinkingClock(turn, index, endedAt, outcome);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Each Thinking row counts from its own start and keeps its frozen stop time. */
|
|
90
|
+
export function formatThinkingElapsed(
|
|
91
|
+
turn: Pick<MinimalTurn, "startedAt" | "thinkingClocks">,
|
|
92
|
+
processIndex: number,
|
|
93
|
+
active: boolean,
|
|
94
|
+
now = Date.now(),
|
|
95
|
+
): string {
|
|
96
|
+
const clock = turn.thinkingClocks?.[processIndex];
|
|
97
|
+
const startedAt = clock?.startedAt ?? (active ? turn.startedAt : undefined);
|
|
98
|
+
if (clock?.endedAt != null) return `${(Math.max(0, clock.endedAt - clock.startedAt) / 1_000).toFixed(1)}s`;
|
|
99
|
+
if (active && startedAt != null) return `${(Math.max(0, now - startedAt) / 1_000).toFixed(1)}s`;
|
|
100
|
+
return "";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function pushProcess(turn: MinimalTurn | undefined, kind: string, value: unknown): void {
|
|
104
|
+
if (!turn) return;
|
|
105
|
+
const text = processText(value);
|
|
106
|
+
const line = text ? `${kind} ${text}` : kind;
|
|
107
|
+
if (turn.process.at(-1) !== line) turn.process.push(line);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function thinkingText(item: Record<string, unknown>): string {
|
|
111
|
+
const readable = (value: unknown) => typeof value === "string"
|
|
112
|
+
? stripVTControlCharacters(value).replace(/\r\n?/g, "\n").replace(/[\x00-\x08\x0b-\x1f\x7f]/g, "").trim() : "";
|
|
113
|
+
const body = readable(item.thinking) || readable(item.text);
|
|
114
|
+
if (body) return body;
|
|
115
|
+
// Recover only the explicitly readable summary. Encrypted/signature data is never prose.
|
|
116
|
+
try {
|
|
117
|
+
const signature = typeof item.thinkingSignature === "string" ? JSON.parse(item.thinkingSignature) : undefined;
|
|
118
|
+
if (signature?.type === "reasoning" && Array.isArray(signature.summary)) {
|
|
119
|
+
return signature.summary.filter((part: { type?: unknown }) => part?.type === "summary_text")
|
|
120
|
+
.map((part: { text?: unknown }) => readable(part.text)).filter(Boolean).join("\n\n");
|
|
121
|
+
}
|
|
122
|
+
} catch { /* Other providers use opaque signatures. */ }
|
|
123
|
+
return "";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function skillNames(text: string): string[] {
|
|
127
|
+
const names = new Set<string>();
|
|
128
|
+
for (const match of text.matchAll(/\/skill:([\w.-]+)/g)) names.add(match[1]);
|
|
129
|
+
for (const match of text.matchAll(/<skill[^>]*?(?:name=["']([^"']+)["']|>\s*<name>([^<]+))/gi)) names.add((match[1] ?? match[2]).trim());
|
|
130
|
+
return [...names];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** pi-loop injects its wakeup as a user message; keep its boilerplate out of the compact card. */
|
|
134
|
+
export function piLoopSummary(question: string): string | undefined {
|
|
135
|
+
if (!question.startsWith("[pi-loop]")) return;
|
|
136
|
+
const lines = question.split(/\r?\n/).map(line => line.trim()).filter(Boolean);
|
|
137
|
+
const title = lines[0] ?? "[pi-loop]";
|
|
138
|
+
const prompt = lines.find(line => line !== title && !line.startsWith("[Self-paced loop:"));
|
|
139
|
+
return prompt ? `${title} · ${prompt}` : title;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Rebuild turns on the selected session branch. */
|
|
143
|
+
export function minimalTurnsFromBranch(branch: readonly unknown[]): MinimalTurn[] {
|
|
144
|
+
const turns: MinimalTurn[] = [];
|
|
145
|
+
let turn: MinimalTurn | undefined;
|
|
146
|
+
for (const rawEntry of branch) {
|
|
147
|
+
const entry = rawEntry as { type?: string; message?: { role?: string; content?: unknown; usage?: UsageLike; stopReason?: string; errorMessage?: string } } | null;
|
|
148
|
+
if (entry?.type !== "message" || !entry.message) continue;
|
|
149
|
+
const { role, content } = entry.message;
|
|
150
|
+
if (role === "user") {
|
|
151
|
+
const question = contentText(content);
|
|
152
|
+
turn = { question: question || "[Attachment]", process: [], running: false };
|
|
153
|
+
for (const name of skillNames(question)) pushProcess(turn, "skill", name);
|
|
154
|
+
turns.push(turn);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (!turn) continue;
|
|
158
|
+
if (role === "assistant" || role === "toolResult") turn.usage = addUsage(turn.usage ?? EMPTY_USAGE, entry.message.usage);
|
|
159
|
+
if (role === "toolResult") {
|
|
160
|
+
const result = entry.message as { toolCallId?: string; isError?: boolean };
|
|
161
|
+
const call = turn.agentCalls?.find(call => call.id === result.toolCallId);
|
|
162
|
+
if (call) {
|
|
163
|
+
call.state = result.isError ? "error" : "done";
|
|
164
|
+
call.output = contentText(content) || "Call returned (no text output)";
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
pushProcess(turn, "output", content);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (role !== "assistant" || !Array.isArray(content)) continue;
|
|
171
|
+
if (turn.final) (turn.replies ??= []).push(turn.final);
|
|
172
|
+
turn.final = undefined;
|
|
173
|
+
for (const [blockIndex, item] of (content as Array<Record<string, unknown>>).entries()) {
|
|
174
|
+
if (!item || typeof item !== "object") continue;
|
|
175
|
+
if (item.type === "thinking") {
|
|
176
|
+
const index = turn.process.push(`thinking ${thinkingText(item)}`) - 1;
|
|
177
|
+
(turn.thinkingOutcomes ??= [])[index] = blockIndex === content.length - 1
|
|
178
|
+
&& (entry.message.stopReason === "error" || entry.message.stopReason === "aborted") ? entry.message.stopReason : "done";
|
|
179
|
+
} else if (item.type === "toolCall") {
|
|
180
|
+
if (item.name) {
|
|
181
|
+
(turn.agentCalls ??= []).push(agentCall(String(item.id), String(item.name), item.arguments ?? item.input));
|
|
182
|
+
pushProcess(turn, "call", String(item.id));
|
|
183
|
+
const path = (item.arguments as { path?: string })?.path;
|
|
184
|
+
if (path && /(?:^|\/)SKILL\.md$/i.test(path)) pushProcess(turn, "skill", path.split("/").at(-2));
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
pushProcess(turn, `tool ${String(item.name ?? "")}`.trim(), item.arguments ?? item.input);
|
|
188
|
+
const path = (item.arguments as { path?: unknown })?.path;
|
|
189
|
+
if (typeof path === "string" && /(?:^|\/)SKILL\.md$/i.test(path)) pushProcess(turn, "skill", path.split("/").at(-2) ?? path);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const text = contentText(content);
|
|
193
|
+
const hasTools = content.some((item) => item?.type === "toolCall");
|
|
194
|
+
if (hasTools) {
|
|
195
|
+
if (text) pushProcess(turn, "output", text);
|
|
196
|
+
turn.final = undefined;
|
|
197
|
+
} else turn.final = text || undefined;
|
|
198
|
+
if (entry.message.stopReason === "error" || entry.message.stopReason === "aborted") {
|
|
199
|
+
turn.final = [entry.message.stopReason === "aborted" ? "Execution aborted" : "Execution failed", entry.message.errorMessage, turn.final].filter(Boolean).join("\n");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return turns;
|
|
203
|
+
}
|
package/lib/settings-web.ts
CHANGED
|
@@ -63,8 +63,15 @@ export async function startSettingsWeb(snapshot: () => unknown, update: (value:
|
|
|
63
63
|
}
|
|
64
64
|
if (req.url === "/" && req.method === "GET") {
|
|
65
65
|
try {
|
|
66
|
-
const html = await
|
|
67
|
-
|
|
66
|
+
const [html, manifest] = await Promise.all([
|
|
67
|
+
readFile(new URL("./settings.html", import.meta.url), "utf8"),
|
|
68
|
+
readFile(new URL("../package.json", import.meta.url), "utf8"),
|
|
69
|
+
]);
|
|
70
|
+
const version = JSON.parse(manifest).version;
|
|
71
|
+
const page = typeof version === "string" && /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(version)
|
|
72
|
+
? html.replace("<html lang=\"zh-CN\">", `<html lang="zh-CN" data-version="${version}">`)
|
|
73
|
+
: html;
|
|
74
|
+
res.setHeader("Content-Type", "text/html; charset=utf-8"); res.end(page);
|
|
68
75
|
} catch {
|
|
69
76
|
res.writeHead(503).end("Settings page unavailable. Reload Pi and reopen settings.");
|
|
70
77
|
}
|