@each1024/pi-mini-mode 0.9.0
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/LICENSE +21 -0
- package/README.md +214 -0
- package/README.zh-CN.md +210 -0
- package/extensions/footer-status.ts +1612 -0
- package/lib/agent-view.ts +518 -0
- package/lib/file-links.ts +80 -0
- package/lib/footer-tidy.ts +75 -0
- package/lib/input-enhancements.ts +622 -0
- package/lib/minimal-markdown.ts +181 -0
- package/lib/minimal-theme.ts +48 -0
- package/lib/settings-web.ts +119 -0
- package/lib/settings.html +209 -0
- package/lib/sticky-tool.ts +100 -0
- package/lib/title-plain.ts +57 -0
- package/lib/transcript-adapter.ts +578 -0
- package/package.json +61 -0
- package/themes/README.md +5 -0
- package/themes/cc-dark.json +91 -0
- package/themes/cc-light.json +91 -0
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import { Markdown, Text, truncateToWidth, visibleWidth, type Component } from "@earendil-works/pi-tui";
|
|
2
|
+
import { getMarkdownTheme, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { diagramMarkdown, isMarkdownProse, renderMinimalMarkdown } from "./minimal-markdown.ts";
|
|
4
|
+
import { stripVTControlCharacters } from "node:util";
|
|
5
|
+
import { attachStickyTool } from "./sticky-tool.ts";
|
|
6
|
+
|
|
7
|
+
const record = (value: unknown): Record<string, unknown> | undefined => value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : undefined;
|
|
8
|
+
const clean = (text: string) => stripVTControlCharacters(text).replace(/[\x00-\x1f\x7f]/g, " ").replace(/\s+/g, " ").trim();
|
|
9
|
+
const renderMarkdownBody = (body: string, width: number, theme?: ExtensionContext["ui"]["theme"]) => isMarkdownProse(body)
|
|
10
|
+
? renderMinimalMarkdown(body, Math.max(1, width), getMarkdownTheme(), theme?.getBgAnsi?.("userMessageBg") ?? "", undefined, diagramMarkdown)
|
|
11
|
+
: new Text(body, 0, 0).render(Math.max(1, width));
|
|
12
|
+
const isSupervisorReply = (message: unknown) => record(message)?.customType === "subagent_supervisor_reply";
|
|
13
|
+
|
|
14
|
+
/** Structured interview questions are the prompt; IDs and reply hints stay out. */
|
|
15
|
+
export function formatInterview(interview: unknown): string {
|
|
16
|
+
const data = record(interview);
|
|
17
|
+
if (!data) return typeof interview === "string" ? clean(interview) : "";
|
|
18
|
+
const lines: string[] = [];
|
|
19
|
+
if (typeof data.title === "string" && data.title.trim()) lines.push(clean(data.title));
|
|
20
|
+
const questions = Array.isArray(data.questions) ? data.questions : [];
|
|
21
|
+
questions.forEach((question, index) => {
|
|
22
|
+
if (typeof question === "string") {
|
|
23
|
+
if (question.trim()) lines.push(`${index + 1}. ${clean(question)}`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const item = record(question);
|
|
27
|
+
if (!item) return;
|
|
28
|
+
const prompt = [item.prompt, item.question, item.text, item.label].find(value => typeof value === "string" && String(value).trim());
|
|
29
|
+
if (typeof prompt === "string") lines.push(`${index + 1}. ${clean(prompt)}`);
|
|
30
|
+
const options = Array.isArray(item.options) ? item.options : Array.isArray(item.choices) ? item.choices : [];
|
|
31
|
+
for (const option of options) {
|
|
32
|
+
const label = typeof option === "string" ? option : typeof record(option)?.label === "string" ? String(record(option)!.label) : "";
|
|
33
|
+
if (label.trim()) lines.push(` - ${clean(label)}`);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
return lines.join("\n");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function withInterview(body: string, interview: unknown): string {
|
|
40
|
+
const questions = formatInterview(interview);
|
|
41
|
+
if (!questions) return body;
|
|
42
|
+
return body ? `${body}\n${questions}` : questions;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function extractNoticeBody(message: unknown): string {
|
|
46
|
+
const item = record(message);
|
|
47
|
+
if (!item) return typeof message === "string" ? clean(message) : "";
|
|
48
|
+
const details = record(item.details);
|
|
49
|
+
const interview = details?.interview;
|
|
50
|
+
if (details) {
|
|
51
|
+
const event = record(details.event);
|
|
52
|
+
const body = typeof details.requestBody === "string"
|
|
53
|
+
? details.requestBody
|
|
54
|
+
: typeof event?.message === "string"
|
|
55
|
+
? event.message
|
|
56
|
+
: typeof details.message === "string"
|
|
57
|
+
? details.message
|
|
58
|
+
: undefined;
|
|
59
|
+
if (body) {
|
|
60
|
+
let cleanBody = stripVTControlCharacters(body).replace(/^UPDATE:\s*/i, "").trim();
|
|
61
|
+
const failureReason = typeof event?.reason === "string" && ["tool_failures", "completion_guard"].includes(event.reason)
|
|
62
|
+
? event.reason === "tool_failures" ? "工具执行失败" : "任务完成受阻"
|
|
63
|
+
: undefined;
|
|
64
|
+
if (failureReason && !cleanBody.includes(failureReason)) cleanBody = `[${failureReason}] ${cleanBody}`;
|
|
65
|
+
return withInterview(cleanBody, interview);
|
|
66
|
+
}
|
|
67
|
+
const questions = formatInterview(interview);
|
|
68
|
+
if (questions) return questions;
|
|
69
|
+
}
|
|
70
|
+
const raw = typeof item.content === "string" ? item.content : "";
|
|
71
|
+
if (raw) {
|
|
72
|
+
const filtered = stripVTControlCharacters(raw)
|
|
73
|
+
.split(/\r?\n/)
|
|
74
|
+
.map(line => line.trim())
|
|
75
|
+
.filter(line => line
|
|
76
|
+
&& !/^(?:Subagent (?:progress update|needs a supervisor decision|control notice)|Supervisor (?:progress update|decision request|interview request))\b/i.test(line)
|
|
77
|
+
&& !/^(?:Run|Agent|Child index|Child target|Request ID|Live guidance|Reply with|Request|Interview shape):\s*/i.test(line)
|
|
78
|
+
&& !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(line)
|
|
79
|
+
)
|
|
80
|
+
.join("\n")
|
|
81
|
+
.replace(/^UPDATE:\s*/i, "")
|
|
82
|
+
.trim();
|
|
83
|
+
if (filtered) return withInterview(filtered, interview);
|
|
84
|
+
}
|
|
85
|
+
const questions = formatInterview(interview);
|
|
86
|
+
if (questions) return questions;
|
|
87
|
+
return typeof item.content === "string" ? item.content : JSON.stringify(item.content ?? item, null, 2);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Only pi-subagents' known envelopes carry state; prose is never a status signal. */
|
|
91
|
+
export function supervisorNotice(message: unknown) {
|
|
92
|
+
const item = record(message);
|
|
93
|
+
if (item?.customType === "subagent_supervisor_reply") {
|
|
94
|
+
const data = record(item.data);
|
|
95
|
+
if (item.type !== "custom" || !data
|
|
96
|
+
|| typeof data.requestId !== "string" || typeof data.runId !== "string"
|
|
97
|
+
|| typeof data.agent !== "string" || typeof data.message !== "string"
|
|
98
|
+
|| typeof data.childIndex !== "number" || !Number.isFinite(data.childIndex)
|
|
99
|
+
|| typeof data.createdAt !== "number" || !Number.isFinite(data.createdAt)
|
|
100
|
+
|| (data.reason !== undefined && (typeof data.reason !== "string" || !["need_decision", "interview_request", "progress_update"].includes(data.reason)))
|
|
101
|
+
|| (data.childTarget !== undefined && typeof data.childTarget !== "string")) return;
|
|
102
|
+
return {
|
|
103
|
+
key: data.runId && Number.isInteger(data.childIndex) && data.childIndex >= 0
|
|
104
|
+
&& !data.nestedRunId && !data.nestingPath ? JSON.stringify([data.runId, data.childIndex]) : undefined,
|
|
105
|
+
runId: data.runId || undefined,
|
|
106
|
+
agent: data.agent,
|
|
107
|
+
internal: true,
|
|
108
|
+
alert: false,
|
|
109
|
+
state: "内部协作",
|
|
110
|
+
color: "muted" as const,
|
|
111
|
+
summary: "代理间沟通已收纳",
|
|
112
|
+
body: stripVTControlCharacters(data.message).trim(),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (item?.type === "custom") return;
|
|
116
|
+
if (item?.customType === "subagent-incremental-child-notify") {
|
|
117
|
+
const content = typeof item.content === "string" ? item.content : undefined;
|
|
118
|
+
const header = content?.match(/^Workflow child (completed|failed):\s+\*\*([^*\n]+)\*\*\s*$/m);
|
|
119
|
+
if (!content || !header) return;
|
|
120
|
+
const failed = header[1] === "failed";
|
|
121
|
+
const error = content.match(/^Error:\s*([\s\S]*?)(?=^Status:|(?![\s\S]))/m)?.[1]
|
|
122
|
+
.split(/\r?\n/)
|
|
123
|
+
.map(line => line.trim())
|
|
124
|
+
.filter(line => line && !/^Run fan-out:/i.test(line))
|
|
125
|
+
.join(" ");
|
|
126
|
+
const workflowRunId = content.match(/^Workflow run:\s*([0-9a-f-]{36})\s*$/mi)?.[1];
|
|
127
|
+
const childRunId = content.match(/^Child run:\s*([0-9a-f-]{36})\s*$/mi)?.[1];
|
|
128
|
+
const runIds = [...new Set([childRunId, workflowRunId].filter((id): id is string => !!id))];
|
|
129
|
+
return {
|
|
130
|
+
key: childRunId ? JSON.stringify(["incremental-child", childRunId])
|
|
131
|
+
: workflowRunId ? JSON.stringify(["incremental-child", workflowRunId, header[2]]) : undefined,
|
|
132
|
+
runId: childRunId || workflowRunId,
|
|
133
|
+
runIds,
|
|
134
|
+
internal: false,
|
|
135
|
+
alert: failed,
|
|
136
|
+
state: failed ? "执行失败" : "已完成",
|
|
137
|
+
color: failed ? "error" as const : "muted" as const,
|
|
138
|
+
summary: error || (failed ? "子任务失败" : "子任务已完成"),
|
|
139
|
+
label: clean(header[2]),
|
|
140
|
+
showLabel: true,
|
|
141
|
+
body: content,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (item?.customType === "subagent-notify") {
|
|
145
|
+
const content = typeof item.content === "string" ? item.content : undefined;
|
|
146
|
+
const header = content?.match(/^(?:Background task|Detached foreground task) (completed|failed|paused|stopped):\s+\*\*([^\*\n]+)\*\*/m);
|
|
147
|
+
if (!content || !header) return;
|
|
148
|
+
const status = header[1];
|
|
149
|
+
const failed = status === "failed" || status === "stopped"
|
|
150
|
+
|| /^- key=\S+ run=[0-9a-f-]{36} status=failed\s*$/mi.test(content)
|
|
151
|
+
|| /^Child runs:.*\(failed\)/mi.test(content);
|
|
152
|
+
const attention = status === "paused";
|
|
153
|
+
const workflowRunId = content.match(/^Workflow run:\s*([0-9a-f-]{36})\s*$/mi)?.[1]
|
|
154
|
+
?? content.match(/^Workflow receipt:\s*.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/mi)?.[1];
|
|
155
|
+
const childRunIds = content.match(/^Child runs:\s*(.+)$/mi)?.[1]
|
|
156
|
+
?.match(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi) ?? [];
|
|
157
|
+
const directoryRunId = content.match(/^Retention-managed async directory:[ \t]*(?:\r?\n)?[^\r\n]*\/async-subagent-runs\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\/?[ \t]*\r?$/mi)?.[1];
|
|
158
|
+
const runIds = [...new Set([workflowRunId, directoryRunId, ...childRunIds].filter((id): id is string => !!id))];
|
|
159
|
+
const runId = workflowRunId || directoryRunId || childRunIds[0];
|
|
160
|
+
const metadata = /^(?:Workflow (?:receipt|run):|Child runs:|Session(?: file| share error)?:|Retention-managed|Reconciled detached|Parallel handoff:|Watchdog blockers:)/i;
|
|
161
|
+
let preview = "";
|
|
162
|
+
for (const line of content.split("\n").slice(1)) {
|
|
163
|
+
const text = line.trim();
|
|
164
|
+
if (!text || text === `${header[2]}:` || text.startsWith("Workflow receipt:")) continue;
|
|
165
|
+
if (metadata.test(text)) break;
|
|
166
|
+
preview = text;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
key: runId ? JSON.stringify(["subagent-notify", runId]) : undefined,
|
|
171
|
+
runId,
|
|
172
|
+
runIds,
|
|
173
|
+
internal: false,
|
|
174
|
+
alert: failed || attention,
|
|
175
|
+
state: failed ? "执行失败" : attention ? "需要关注" : "已完成",
|
|
176
|
+
color: failed ? "error" as const : attention ? "warning" as const : "muted" as const,
|
|
177
|
+
summary: failed && status === "completed" ? "工作流包含失败子任务"
|
|
178
|
+
: clean(preview) || (failed ? "子任务失败" : attention ? "等待回复" : "子任务已完成"),
|
|
179
|
+
label: clean(header[2]),
|
|
180
|
+
showLabel: true,
|
|
181
|
+
body: content,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const details = record(item?.details);
|
|
185
|
+
if (!details) return;
|
|
186
|
+
const event = record(details.event);
|
|
187
|
+
const request = item?.customType === "subagent_supervisor_request";
|
|
188
|
+
if (request ? !["progress_update", "need_decision", "interview_request"].includes(String(details.reason))
|
|
189
|
+
: item?.customType !== "subagent_control_notice" || !event || !["needs_attention", "active_long_running"].includes(String(event.type))) return;
|
|
190
|
+
const data = request ? details : event!;
|
|
191
|
+
const body = request
|
|
192
|
+
? (typeof details.requestBody === "string" ? details.requestBody : "")
|
|
193
|
+
: data.message;
|
|
194
|
+
if (typeof body !== "string" || typeof data.agent !== "string") return;
|
|
195
|
+
const ask = request && (details.reason === "need_decision" || details.reason === "interview_request");
|
|
196
|
+
// Replies and duplicate "waiting for supervisor" control notices stay compact.
|
|
197
|
+
// Blocking asks are the main-session prompt; progress remains a status line.
|
|
198
|
+
const internal = request
|
|
199
|
+
? (!ask && (details.reason !== "progress_update" || details.expectsReply === true))
|
|
200
|
+
: data.reason === "supervisor_request";
|
|
201
|
+
const failed = !request && ["completion_guard", "tool_failures"].includes(String(data.reason));
|
|
202
|
+
const attention = ask || (!request && data.type === "needs_attention" && !internal);
|
|
203
|
+
const question = clean(body).replace(/^UPDATE:\s*/i, "");
|
|
204
|
+
const interviewTitle = typeof record(details.interview)?.title === "string" ? clean(String(record(details.interview)!.title)) : "";
|
|
205
|
+
const index = request ? data.childIndex : data.index;
|
|
206
|
+
const runId = typeof data.runId === "string" && data.runId ? data.runId : undefined;
|
|
207
|
+
// ponytail: aggregate only unambiguous run+child envelopes; nested/missing
|
|
208
|
+
// addresses stay separate until the producer provides a shared child identity.
|
|
209
|
+
const key = runId && Number.isInteger(index) && Number(index) >= 0
|
|
210
|
+
&& !data.nestedRunId && !data.nestingPath ? JSON.stringify([runId, index]) : undefined;
|
|
211
|
+
return { key, runId, internal, alert: failed || attention,
|
|
212
|
+
state: failed ? "执行失败" : ask ? (details.reason === "interview_request" ? "需要提问" : "需要裁决")
|
|
213
|
+
: attention ? "需要关注" : internal ? "内部协作" : "进度",
|
|
214
|
+
color: failed ? "error" as const : attention ? "warning" as const : "muted" as const,
|
|
215
|
+
summary: internal ? "代理间沟通已收纳" : question || (ask ? interviewTitle || "等待回复" : "暂无摘要"),
|
|
216
|
+
label: typeof data.label === "string" ? clean(data.label) : typeof data.taskPreview === "string" ? clean(data.taskPreview) : undefined,
|
|
217
|
+
body: question,
|
|
218
|
+
agent: typeof data.agent === "string" ? data.agent : undefined,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Progress is a status preview, not detail prose; decisions and failures remain inspectable. */
|
|
223
|
+
export function supervisorNoticeBody(message: unknown): string {
|
|
224
|
+
const item = record(message);
|
|
225
|
+
if (item?.customType === "subagent_supervisor_request" && record(item.details)?.reason === "progress_update") return "";
|
|
226
|
+
const notice = supervisorNotice(message);
|
|
227
|
+
if (item?.type === "custom" && item.customType === "subagent_supervisor_reply") return notice?.body ?? "";
|
|
228
|
+
return notice?.alert || notice?.internal ? extractNoticeBody(message) : "";
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function compactGoalCard(entry: unknown, theme: ExtensionContext["ui"]["theme"], width: number, expanded: boolean): string[] | undefined {
|
|
232
|
+
const item = record(entry);
|
|
233
|
+
if (item?.customType !== "pi-codex-goal") return;
|
|
234
|
+
const data = record(item.data);
|
|
235
|
+
const goal = record(data?.goal);
|
|
236
|
+
const objective = typeof goal?.objective === "string" ? clean(goal.objective) : undefined;
|
|
237
|
+
// Usage snapshots are rendered by the goal extension too, but are not cards
|
|
238
|
+
// worth adding to the compact transcript.
|
|
239
|
+
if (!objective || width <= 0) return;
|
|
240
|
+
const status = typeof goal?.status === "string" ? clean(goal.status) : "active";
|
|
241
|
+
const control = truncateToWidth(expanded ? "[收起]" : "[详情]", width, "");
|
|
242
|
+
const title = theme.fg(expanded ? "accent" : "muted", `${control} ◉ Goal · ${status}`);
|
|
243
|
+
const hint = theme.fg(expanded ? "accent" : "muted", " · Ctrl+O");
|
|
244
|
+
const summary = truncateToWidth(objective, Math.max(0, width - visibleWidth(title) - visibleWidth(hint) - 3), "…");
|
|
245
|
+
const rows = [truncateToWidth(`${title} · ${theme.fg(expanded ? "accent" : "text", summary)}${hint}`, width, "")];
|
|
246
|
+
if (expanded) rows.push(...renderMarkdownBody(objective, width, theme));
|
|
247
|
+
return rows;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function compactSupervisorNotice(message: unknown, theme: ExtensionContext["ui"]["theme"], width: number, expanded: boolean, label = "", history: unknown[] = [message]): string[] | undefined {
|
|
251
|
+
const notice = supervisorNotice(message);
|
|
252
|
+
if (!notice) return;
|
|
253
|
+
if (width <= 0) return [];
|
|
254
|
+
const { state, color, summary } = notice;
|
|
255
|
+
const effectiveLabel = label || (notice.showLabel ? notice.label ?? "" : "");
|
|
256
|
+
const control = truncateToWidth(expanded ? "[收起]" : "[详情]", width, "");
|
|
257
|
+
const hint = " · Ctrl+O";
|
|
258
|
+
let heading: string;
|
|
259
|
+
let summaryText: string;
|
|
260
|
+
if (expanded) {
|
|
261
|
+
const statusGlyph = color === "error" ? theme.fg("error", "× " + state)
|
|
262
|
+
: color === "warning" ? theme.fg("warning", "⚠ " + state)
|
|
263
|
+
: theme.fg("accent", "ℹ " + state);
|
|
264
|
+
const taskLabel = effectiveLabel ? theme.fg("accent", ` · ${truncateToWidth(effectiveLabel, 20)}`) : "";
|
|
265
|
+
heading = theme.fg("accent", control) + " " + statusGlyph + taskLabel;
|
|
266
|
+
summaryText = theme.fg("accent", summary);
|
|
267
|
+
} else {
|
|
268
|
+
const title = theme.fg(color, `${color === "error" ? "×" : color === "warning" ? "⚠" : "ℹ"} ${state}${effectiveLabel ? ` · ${truncateToWidth(effectiveLabel, 20)}` : ""}`);
|
|
269
|
+
heading = theme.fg("muted", control) + " " + title;
|
|
270
|
+
summaryText = theme.fg(color, summary);
|
|
271
|
+
}
|
|
272
|
+
const hintStyled = theme.fg(expanded ? "accent" : "muted", hint);
|
|
273
|
+
const available = width - visibleWidth(heading) - visibleWidth(hint) - 3;
|
|
274
|
+
const rows = available >= 24
|
|
275
|
+
? [heading + " · " + truncateToWidth(summaryText, available) + hintStyled]
|
|
276
|
+
: [truncateToWidth(heading, width, ""), truncateToWidth(summaryText, Math.max(0, width - visibleWidth(hint)), "") + truncateToWidth(hintStyled, width, "")];
|
|
277
|
+
if (expanded) {
|
|
278
|
+
const bodies: string[] = [];
|
|
279
|
+
for (const original of history) {
|
|
280
|
+
const body = supervisorNoticeBody(original);
|
|
281
|
+
if (body && !bodies.includes(body)) bodies.push(body);
|
|
282
|
+
}
|
|
283
|
+
for (const body of bodies) {
|
|
284
|
+
rows.push(...renderMarkdownBody(body, width, theme));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return rows;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
interface ContainerLike extends Component { children: Component[] }
|
|
291
|
+
function isContainer(value: unknown): value is ContainerLike {
|
|
292
|
+
const node = value as Partial<ContainerLike> | null;
|
|
293
|
+
return !!node && Array.isArray(node.children) && typeof node.render === "function" && typeof node.invalidate === "function";
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function supervisorEnvelope(child: Component): unknown {
|
|
297
|
+
if (child.constructor.name === "CustomMessageComponent") return (child as unknown as { message?: unknown }).message;
|
|
298
|
+
if (child.constructor.name === "CustomEntryComponent") {
|
|
299
|
+
const entry = record((child as unknown as { entry?: unknown }).entry);
|
|
300
|
+
if (entry?.type === "custom" && entry.customType === "subagent_supervisor_reply") return entry;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Pi 0.85.x private layout adapter. Never mutates the stored messages or child tree. */
|
|
305
|
+
export type NoticeRows = string[] & { handleMouse?: Component["handleMouse"] };
|
|
306
|
+
export type TurnNotices = ReadonlyMap<number, NoticeRows>;
|
|
307
|
+
interface TranscriptView extends Component {
|
|
308
|
+
render(width: number, notices?: TurnNotices): string[];
|
|
309
|
+
clearHover?(): boolean;
|
|
310
|
+
pinnedTool?(): { id: string; y: number; line: string } | undefined;
|
|
311
|
+
unpinTool?(): void;
|
|
312
|
+
toggleTool?(id: string): void;
|
|
313
|
+
pinnedSubagent?(): { id: string; y: number; line: string; autoScroll?: boolean } | undefined;
|
|
314
|
+
unpinSubagent?(): void;
|
|
315
|
+
toggleSubagent?(id: string): void;
|
|
316
|
+
}
|
|
317
|
+
interface NoticeOptions {
|
|
318
|
+
turnCount?: () => number;
|
|
319
|
+
supervisor?: {
|
|
320
|
+
theme: ExtensionContext["ui"]["theme"];
|
|
321
|
+
expanded: () => boolean;
|
|
322
|
+
handledRunIds?: () => Set<string>;
|
|
323
|
+
onNotices?: (groups: Map<string | Component, { first: Component; messages: unknown[]; selected: unknown; notice: NonNullable<ReturnType<typeof supervisorNotice>> }>) => void;
|
|
324
|
+
};
|
|
325
|
+
/** Blocking extension prompts are native UI, never auto-expiring notices. */
|
|
326
|
+
isPrompt?: (text: string) => boolean;
|
|
327
|
+
isTransient?: (text: string) => boolean;
|
|
328
|
+
now?: () => number;
|
|
329
|
+
schedule?: (callback: () => void, delay: number) => () => void;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Keep expiry across minimal-mode remounts without retaining discarded sessions.
|
|
333
|
+
const noticeTimes = new WeakMap<Component, { text: string; until: number }>();
|
|
334
|
+
export function attachTranscript(tui: unknown, view: TranscriptView, options: NoticeOptions = {}): (() => void) | undefined {
|
|
335
|
+
const host = tui as { children?: unknown[]; mode?: string; viewportTop?: number };
|
|
336
|
+
// Regular mode owns terminal scrollback. Replacing its whole document means a
|
|
337
|
+
// streamed update to an older row forces TuiMainScreen's scrollback-clearing redraw.
|
|
338
|
+
// Keep native transcript diffing there; fullscreen's ScrollView is safe to adapt.
|
|
339
|
+
if (host.mode === "regular") return;
|
|
340
|
+
// Both layouts retain this seven-container tree. Fail closed on unknown layouts.
|
|
341
|
+
if (!Array.isArray(host.children) || host.children.length !== 7 || !host.children.every(isContainer)) return;
|
|
342
|
+
const document = host.children[0] as ContainerLike;
|
|
343
|
+
if (document.children.length !== 3 || !document.children.every(isContainer)) return;
|
|
344
|
+
const [header, resources, chat] = document.children as ContainerLike[];
|
|
345
|
+
// During /reload Pi replaces the editor with a reload notice until AFTER
|
|
346
|
+
// session_start. The validated document/container layout remains unchanged.
|
|
347
|
+
// Custom editors also need not expose getText; do not use editor contents
|
|
348
|
+
// as a transcript identity check.
|
|
349
|
+
const renderDescriptor = Object.getOwnPropertyDescriptor(document, "render");
|
|
350
|
+
const mouseDescriptor = Object.getOwnPropertyDescriptor(document, "handleMouse");
|
|
351
|
+
const originalRender = document.render;
|
|
352
|
+
const originalMouse = document.handleMouse;
|
|
353
|
+
const now = options.now ?? Date.now;
|
|
354
|
+
const schedule = options.schedule ?? ((callback, delay) => {
|
|
355
|
+
const timer = setTimeout(callback, delay);
|
|
356
|
+
timer.unref();
|
|
357
|
+
return () => clearTimeout(timer);
|
|
358
|
+
});
|
|
359
|
+
let cancelExpiry: (() => void) | undefined;
|
|
360
|
+
const expandedNotices = new Map<string | Component, boolean>();
|
|
361
|
+
let globalExpanded = options.supervisor?.expanded() ?? false;
|
|
362
|
+
let prefixNotices: NoticeRows = [];
|
|
363
|
+
let prefixNoticeOffset = 0;
|
|
364
|
+
let viewOffset = 0;
|
|
365
|
+
let viewHeight = 0;
|
|
366
|
+
let stickyMinHeight = 0;
|
|
367
|
+
let viewportTop = host.viewportTop;
|
|
368
|
+
const render: Component["render"] = (width) => {
|
|
369
|
+
cancelExpiry?.();
|
|
370
|
+
cancelExpiry = undefined;
|
|
371
|
+
const notices = new Map<number, NoticeRows>();
|
|
372
|
+
const expanded = options.supervisor?.expanded() ?? false;
|
|
373
|
+
if (expanded !== globalExpanded) { expandedNotices.clear(); globalExpanded = expanded; }
|
|
374
|
+
const groups = new Map<string | Component, { first: Component; messages: unknown[]; selected: unknown; notice: NonNullable<ReturnType<typeof supervisorNotice>> }>();
|
|
375
|
+
if (options.supervisor) for (const child of chat.children) {
|
|
376
|
+
const message = supervisorEnvelope(child);
|
|
377
|
+
const notice = supervisorNotice(message);
|
|
378
|
+
if (!notice) continue;
|
|
379
|
+
const key = notice.key ?? child;
|
|
380
|
+
const group = groups.get(key);
|
|
381
|
+
if (!group) groups.set(key, { first: child, messages: [message], selected: message, notice });
|
|
382
|
+
else {
|
|
383
|
+
group.messages.push(message);
|
|
384
|
+
// Failures stay sticky. A matching supervisor reply is the only
|
|
385
|
+
// resolution for a blocking ask; progress and duplicate wait notices
|
|
386
|
+
// must not bury the prompt or clear a failure.
|
|
387
|
+
const resolved = isSupervisorReply(message) && group.notice.alert && group.notice.color !== "error";
|
|
388
|
+
if (notice.color === "error" || (notice.alert && group.notice.color !== "error") || resolved
|
|
389
|
+
|| (!group.notice.alert && (!notice.internal || group.notice.internal))) {
|
|
390
|
+
group.selected = message;
|
|
391
|
+
group.notice = notice;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
// String keys belong to aggregated supervisor notices; component keys belong
|
|
396
|
+
// to independently rendered custom entries such as pi-codex-goal.
|
|
397
|
+
for (const key of expandedNotices.keys()) if (typeof key === "string" && !groups.has(key)) expandedNotices.delete(key);
|
|
398
|
+
options.supervisor?.onNotices?.(groups);
|
|
399
|
+
const groupKeys = [...groups.keys()];
|
|
400
|
+
const controls = new Map<number, Array<{ y: number; width: number; key: string | Component; expanded: boolean }>>();
|
|
401
|
+
const nativeTurns = chat.children.filter(child => child.constructor.name === "UserMessageComponent").length;
|
|
402
|
+
const totalTurns = options.turnCount?.() ?? nativeTurns;
|
|
403
|
+
// ponytail: Pi rebuilds a retained suffix after compaction; explicit entry IDs
|
|
404
|
+
// are needed if native history ever becomes a non-contiguous projection.
|
|
405
|
+
if (!Number.isInteger(totalTurns) || totalTurns < nativeTurns) {
|
|
406
|
+
prefixNotices = [];
|
|
407
|
+
prefixNoticeOffset = viewOffset = viewHeight = 0;
|
|
408
|
+
view.clearHover?.();
|
|
409
|
+
return originalRender.call(document, width);
|
|
410
|
+
}
|
|
411
|
+
let turn = totalTurns - nativeTurns - 1;
|
|
412
|
+
let nextExpiry = Infinity;
|
|
413
|
+
const time = now();
|
|
414
|
+
for (const child of chat.children) {
|
|
415
|
+
if (child.constructor.name === "UserMessageComponent") turn++;
|
|
416
|
+
// Goal state is stored as a custom entry, not a user message. Adapt only
|
|
417
|
+
// the known pi-codex-goal envelope so ordinary extension cards stay native.
|
|
418
|
+
// Pi rebuilds this collapsed native component after every successful compaction.
|
|
419
|
+
// Keep its one-line success marker at the current user turn without persisting another entry.
|
|
420
|
+
if (child.constructor.name === "CompactionSummaryMessageComponent") {
|
|
421
|
+
const rows = notices.get(turn) ?? [];
|
|
422
|
+
rows.push(...child.render(width));
|
|
423
|
+
notices.set(turn, rows);
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (child.constructor.name === "CustomEntryComponent"
|
|
427
|
+
&& !(options.supervisor && supervisorNotice(supervisorEnvelope(child)))) {
|
|
428
|
+
const goalRows = options.supervisor && compactGoalCard((child as unknown as { entry?: unknown }).entry, options.supervisor.theme, width, expandedNotices.get(child) ?? false);
|
|
429
|
+
const rows = notices.get(turn) ?? [];
|
|
430
|
+
if (!goalRows) {
|
|
431
|
+
rows.push(...child.render(width));
|
|
432
|
+
notices.set(turn, rows);
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const open = expandedNotices.get(child) ?? false;
|
|
436
|
+
const targets = controls.get(turn) ?? [];
|
|
437
|
+
targets.push({ y: rows.length, width, key: child, expanded: open });
|
|
438
|
+
controls.set(turn, targets);
|
|
439
|
+
rows.push(...goalRows);
|
|
440
|
+
notices.set(turn, rows);
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
// Known reply entries share notice grouping; unknown cards retain Pi's renderer.
|
|
444
|
+
if (child.constructor.name === "CustomMessageComponent" || child.constructor.name === "CustomEntryComponent") {
|
|
445
|
+
const rows = notices.get(turn) ?? [];
|
|
446
|
+
const message = supervisorEnvelope(child);
|
|
447
|
+
const notice = options.supervisor && supervisorNotice(message);
|
|
448
|
+
const key = notice ? notice.key ?? child : child;
|
|
449
|
+
const group = groups.get(key);
|
|
450
|
+
if (group && group.first !== child) continue;
|
|
451
|
+
if (group && options.supervisor) {
|
|
452
|
+
const handled = options.supervisor.handledRunIds?.();
|
|
453
|
+
const noticeIds = [notice?.runId, ...((notice as { runIds?: string[] } | undefined)?.runIds ?? [])].filter((id): id is string => !!id);
|
|
454
|
+
if (handled && noticeIds.some(id => handled.has(id))) continue;
|
|
455
|
+
const open = expandedNotices.get(key) ?? expanded;
|
|
456
|
+
const label = group.notice.showLabel
|
|
457
|
+
? group.notice.label ?? ""
|
|
458
|
+
: groups.size > 1 ? group.notice.label || `任务 ${groupKeys.indexOf(key) + 1}` : "";
|
|
459
|
+
const targets = controls.get(turn) ?? [];
|
|
460
|
+
targets.push({ y: rows.length, width, key, expanded: open });
|
|
461
|
+
controls.set(turn, targets);
|
|
462
|
+
rows.push(...compactSupervisorNotice(group.selected, options.supervisor.theme, width, open, label, group.messages)!);
|
|
463
|
+
} else rows.push(...child.render(width));
|
|
464
|
+
notices.set(turn, rows);
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (child.constructor.name !== "Text") continue;
|
|
468
|
+
// Pi 0.85 Text stores the unwrapped styled source in text. Width changes
|
|
469
|
+
// must not restart the timeout; setText updates must restart it.
|
|
470
|
+
const source = (child as unknown as { text?: unknown }).text;
|
|
471
|
+
if (typeof source === "string" && !options.isPrompt?.(source) && options.isTransient?.(source)) {
|
|
472
|
+
let state = noticeTimes.get(child);
|
|
473
|
+
if (!state || state.text !== source) {
|
|
474
|
+
state = { text: source, until: time + 5000 };
|
|
475
|
+
noticeTimes.set(child, state);
|
|
476
|
+
}
|
|
477
|
+
if (time >= state.until) continue;
|
|
478
|
+
nextExpiry = Math.min(nextExpiry, state.until);
|
|
479
|
+
}
|
|
480
|
+
const rows = notices.get(turn) ?? [];
|
|
481
|
+
rows.push(...child.render(width));
|
|
482
|
+
notices.set(turn, rows);
|
|
483
|
+
}
|
|
484
|
+
if (Number.isFinite(nextExpiry)) {
|
|
485
|
+
cancelExpiry = schedule(() => {
|
|
486
|
+
cancelExpiry = undefined;
|
|
487
|
+
(tui as { requestRender?: () => void }).requestRender?.();
|
|
488
|
+
}, Math.max(0, nextExpiry - time));
|
|
489
|
+
}
|
|
490
|
+
for (const [turnIndex, targets] of controls) {
|
|
491
|
+
notices.get(turnIndex)!.handleMouse = event => {
|
|
492
|
+
if (event.button !== "left" || event.shift || event.ctrl || event.alt || (event.clickCount ?? 1) !== 1) return;
|
|
493
|
+
const target = targets.find(target => event.y === target.y && event.x >= 0 && event.x < target.width);
|
|
494
|
+
if (!target) return;
|
|
495
|
+
if (event.type === "press") return { handled: true };
|
|
496
|
+
if (event.type === "click") {
|
|
497
|
+
expandedNotices.set(target.key, !target.expanded);
|
|
498
|
+
return { handled: true, render: true };
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
const prefix = [...header.render(width), ...resources.render(width)];
|
|
503
|
+
prefixNoticeOffset = prefix.length;
|
|
504
|
+
prefixNotices = notices.get(-1) ?? [];
|
|
505
|
+
prefix.push(...prefixNotices);
|
|
506
|
+
if (viewportTop !== host.viewportTop || viewOffset !== prefix.length) view.clearHover?.();
|
|
507
|
+
viewportTop = host.viewportTop;
|
|
508
|
+
const rows = view.render(width, notices);
|
|
509
|
+
viewOffset = prefix.length;
|
|
510
|
+
viewHeight = rows.length;
|
|
511
|
+
const result = [...prefix, ...rows];
|
|
512
|
+
while (result.length < stickyMinHeight) result.push("");
|
|
513
|
+
return result;
|
|
514
|
+
};
|
|
515
|
+
// Do not dispatch pointer events into invisible native message components.
|
|
516
|
+
// Unhandled events still reach the parent ScrollView / selection machinery.
|
|
517
|
+
const mouse: NonNullable<Component["handleMouse"]> = event => {
|
|
518
|
+
const cleared = (event.type === "wheel" || event.y < viewOffset || event.y >= viewOffset + viewHeight) && view.clearHover?.();
|
|
519
|
+
if (cleared && event.type === "move") return { handled: true, render: true };
|
|
520
|
+
if (event.y >= prefixNoticeOffset && event.y < viewOffset) return prefixNotices.handleMouse?.({ ...event, y: event.y - prefixNoticeOffset });
|
|
521
|
+
if (event.y < viewOffset || event.y >= viewOffset + viewHeight) return;
|
|
522
|
+
return view.handleMouse?.({ ...event, y: event.y - viewOffset, height: viewHeight });
|
|
523
|
+
};
|
|
524
|
+
document.render = render;
|
|
525
|
+
document.handleMouse = mouse;
|
|
526
|
+
// Dock events never reach the document. Observe normalized events without
|
|
527
|
+
// stealing wheel/selection or changing Pi's mouse protocol/listener order.
|
|
528
|
+
const restoreDock = view.clearHover ? host.children.slice(1).map(child => {
|
|
529
|
+
const dock = child as Component;
|
|
530
|
+
const descriptor = Object.getOwnPropertyDescriptor(dock, "handleMouse");
|
|
531
|
+
const original = dock.handleMouse;
|
|
532
|
+
const handle: NonNullable<Component["handleMouse"]> = event => {
|
|
533
|
+
const cleared = view.clearHover?.();
|
|
534
|
+
const result = original?.call(dock, event);
|
|
535
|
+
if (result) return cleared ? { ...result, render: true } : result;
|
|
536
|
+
return cleared && event.type === "move" ? { handled: true, render: true } : undefined;
|
|
537
|
+
};
|
|
538
|
+
dock.handleMouse = handle;
|
|
539
|
+
return () => {
|
|
540
|
+
if (dock.handleMouse !== handle) return;
|
|
541
|
+
if (descriptor) Object.defineProperty(dock, "handleMouse", descriptor);
|
|
542
|
+
else delete dock.handleMouse;
|
|
543
|
+
};
|
|
544
|
+
}) : [];
|
|
545
|
+
const hasStickyTool = view.pinnedTool && view.unpinTool && view.toggleTool;
|
|
546
|
+
const hasStickySubagent = view.pinnedSubagent && view.unpinSubagent && view.toggleSubagent;
|
|
547
|
+
const restoreSticky = hasStickyTool || hasStickySubagent ? attachStickyTool(tui, document, {
|
|
548
|
+
pinnedTool: () => {
|
|
549
|
+
const target = view.pinnedSubagent?.() ?? view.pinnedTool?.();
|
|
550
|
+
if (!target) return;
|
|
551
|
+
const kind = view.pinnedSubagent?.() ? "subagent" : "tool";
|
|
552
|
+
return { ...target, id: `${kind}:${target.id}`, y: target.y + viewOffset };
|
|
553
|
+
},
|
|
554
|
+
unpinTool: () => {
|
|
555
|
+
view.unpinSubagent?.();
|
|
556
|
+
view.unpinTool?.();
|
|
557
|
+
},
|
|
558
|
+
toggleTool: id => {
|
|
559
|
+
const [kind, targetId] = id.split(":", 2);
|
|
560
|
+
if (kind === "subagent") view.toggleSubagent?.(targetId);
|
|
561
|
+
else if (kind === "tool") view.toggleTool?.(targetId);
|
|
562
|
+
},
|
|
563
|
+
}, height => { stickyMinHeight = height; }) : undefined;
|
|
564
|
+
return () => {
|
|
565
|
+
restoreSticky?.();
|
|
566
|
+
cancelExpiry?.();
|
|
567
|
+
view.clearHover?.();
|
|
568
|
+
for (const restore of restoreDock) restore();
|
|
569
|
+
if (document.render === render) {
|
|
570
|
+
if (renderDescriptor) Object.defineProperty(document, "render", renderDescriptor);
|
|
571
|
+
else { delete (document as Partial<Component>).render; if (document.render !== originalRender) document.render = originalRender; }
|
|
572
|
+
}
|
|
573
|
+
if (document.handleMouse === mouse) {
|
|
574
|
+
if (mouseDescriptor) Object.defineProperty(document, "handleMouse", mouseDescriptor);
|
|
575
|
+
else { delete document.handleMouse; if (document.handleMouse !== originalMouse) document.handleMouse = originalMouse; }
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@each1024/pi-mini-mode",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "A compact Pi footer with live usage metrics, theme-aware settings previews, and configurable progress bars.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"check": "tsc --noEmit -p tsconfig.json",
|
|
8
|
+
"test": "node test/settings-web.mjs && node test/footer-tidy.mjs && node test/footer-status-self-check.mjs && node test/settings-interaction.mjs && node test/transcript-adapter.mjs && node test/prompt-folding.mjs && node test/tool-folding.mjs && node test/sticky-tool.mjs && node test/minimal-markdown.mjs && node test/agent-view.mjs && node test/publish-self-check.mjs && node test/compaction-transcript.mjs && node test/input-enhancements.mjs && node test/user-message-surface.mjs"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "eachann1024",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/eachann1024/pi-mini-mode.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/eachann1024/pi-mini-mode/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/eachann1024/pi-mini-mode#readme",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"pi-package",
|
|
22
|
+
"pi-coding-agent",
|
|
23
|
+
"pi-extension",
|
|
24
|
+
"footer",
|
|
25
|
+
"tui"
|
|
26
|
+
],
|
|
27
|
+
"pi": {
|
|
28
|
+
"video": "https://raw.githubusercontent.com/eachann1024/pi-mini-mode/main/assets/pi-mini-mode-settings.mp4",
|
|
29
|
+
"extensions": [
|
|
30
|
+
"./extensions/footer-status.ts"
|
|
31
|
+
],
|
|
32
|
+
"themes": [
|
|
33
|
+
"./themes/cc-dark.json",
|
|
34
|
+
"./themes/cc-light.json"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@earendil-works/pi-coding-agent": ">=0.84.0",
|
|
39
|
+
"@earendil-works/pi-tui": "*"
|
|
40
|
+
},
|
|
41
|
+
"files": [
|
|
42
|
+
"extensions",
|
|
43
|
+
"lib",
|
|
44
|
+
"themes",
|
|
45
|
+
"README.md",
|
|
46
|
+
"LICENSE"
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@earendil-works/pi-coding-agent": "^0.85.0",
|
|
53
|
+
"@earendil-works/pi-server": "0.85.0",
|
|
54
|
+
"@earendil-works/pi-tui": "^0.85.0",
|
|
55
|
+
"@types/node": "^26.4.1",
|
|
56
|
+
"typescript": "^5.9.3"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"grok-mermaid": "0.2.2"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/themes/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Themes
|
|
2
|
+
|
|
3
|
+
`cc-dark` and `cc-light` are vendored from the npm package [pi-cc-extensions](https://github.com/minuque/pi-cc-extensions) (MIT).
|
|
4
|
+
|
|
5
|
+
They are included here so this package can provide the `cc-light/cc-dark` theme pair without depending on `pi-cc-extensions` at runtime.
|