@each1024/pi-mini-mode 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +178 -0
- package/README.zh-CN.md +178 -0
- package/extensions/footer-status.ts +1027 -0
- package/lib/agent-view.ts +107 -0
- package/lib/minimal-markdown.ts +17 -0
- package/lib/minimal-theme.ts +32 -0
- package/lib/transcript-adapter.ts +96 -0
- package/package.json +56 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { truncateToWidth, type Component } from "@earendil-works/pi-tui";
|
|
3
|
+
|
|
4
|
+
type Theme = ExtensionContext["ui"]["theme"];
|
|
5
|
+
export interface AgentCall {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
task: string;
|
|
9
|
+
state: "running" | "done" | "error";
|
|
10
|
+
output?: string;
|
|
11
|
+
}
|
|
12
|
+
export const isAgentTool = (name: string) => /^(subagent|agents?|get_subagent_result|steer_subagent|bg_wait)$/i.test(name);
|
|
13
|
+
export function agentCall(id: string, tool: string, args: unknown): AgentCall {
|
|
14
|
+
const data = (args && typeof args === "object" ? args : {}) as Record<string, unknown>;
|
|
15
|
+
const name = String(data.agent ?? data.subagent_type ?? data.agent_type ?? tool);
|
|
16
|
+
return { id, name, task: String(data.task ?? data.prompt ?? data.description ?? data.action ?? data.command ?? data.path ?? ""), state: "running" };
|
|
17
|
+
}
|
|
18
|
+
export function agentCallRows(calls: AgentCall[], theme: Theme, width: number, expanded: boolean,
|
|
19
|
+
markdown: (text: string, width: number) => string[]): string[] {
|
|
20
|
+
if (!calls.length || width < 1) return [];
|
|
21
|
+
const running = calls.filter(call => call.state === "running").length;
|
|
22
|
+
const errors = calls.filter(call => call.state === "error").length;
|
|
23
|
+
const done = calls.length - running - errors;
|
|
24
|
+
const header = `${expanded ? "⌄" : "›"} Agent 调用 · ${calls.length} 个 · ${running} 执行中 · ${done} 已返回${errors ? ` · ${errors} 失败` : ""} · Ctrl+O`;
|
|
25
|
+
const rows = [truncateToWidth(theme.fg(errors ? "error" : running ? "accent" : "muted", header), width)];
|
|
26
|
+
if (expanded) calls.forEach((call, index) => {
|
|
27
|
+
const color = call.state === "error" ? "error" : call.state === "done" ? "success" : "accent";
|
|
28
|
+
const glyph = call.state === "error" ? "✗" : call.state === "done" ? "✓" : "●";
|
|
29
|
+
rows.push(truncateToWidth(theme.fg(color, `${index === calls.length - 1 ? "└─" : "├─"} ${glyph} ${call.name}`), width));
|
|
30
|
+
for (const text of [call.task, call.output]) {
|
|
31
|
+
if (text) rows.push(...markdown(text, Math.max(1, width - 3)).map(row => truncateToWidth(` ${row}`, width, "")));
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
return rows;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const plain = (text: string) => text.replace(/\x1b\][^\x07]*(?:\x07|\x1b\\)/g, "").replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, "").trim();
|
|
38
|
+
/** Presentation adapter for pi-subagents' independently updated widget.
|
|
39
|
+
* Unknown widget formats pass through unchanged; no task state is inferred.
|
|
40
|
+
*/
|
|
41
|
+
export function restyleAgentWidget(lines: string[], theme: Theme, width: number, expanded: boolean): string[] {
|
|
42
|
+
const content = lines.map(plain).filter(Boolean);
|
|
43
|
+
if (!content.length || !/^(?:[●○◉⠋-⣿]\s*)?(?:async subagent|Async agents)\b/i.test(content[0])) return lines;
|
|
44
|
+
const details = content.slice(1).map(line => line
|
|
45
|
+
.replace(/(?:[·•]\s*)?Press\s+ctrl\+.*$/i, "")
|
|
46
|
+
.replace(/(?:[·•]\s*)?Ctrl\+Alt\+F\s+Fleet.*$/i, "").trim()).filter(Boolean);
|
|
47
|
+
const isHeading = (line: string) => !/^(?:task|output|error)\s*:|^[└├│]/i.test(line)
|
|
48
|
+
&& /\b(running|queued|complete|completed|failed|paused|stopped|done|error)\b/i.test(line);
|
|
49
|
+
const blocks: string[][] = [];
|
|
50
|
+
for (const line of details) {
|
|
51
|
+
if (!blocks.length || isHeading(line)) blocks.push([line]);
|
|
52
|
+
else blocks[blocks.length - 1].push(line);
|
|
53
|
+
}
|
|
54
|
+
const summary = blocks.map(block => block[0]).join(" · ") || content[0].replace(/async subagent|Async agents/i, "").trim();
|
|
55
|
+
const header = theme.fg("accent", `${expanded ? "⌄" : "›"} Sub Agent`)
|
|
56
|
+
+ theme.fg("muted", `${expanded ? "" : ` · ${summary}`} · Ctrl+S`);
|
|
57
|
+
const rows = [truncateToWidth(header, width)];
|
|
58
|
+
if (!expanded) return rows;
|
|
59
|
+
blocks.forEach((block, index) => {
|
|
60
|
+
if (index) rows.push("");
|
|
61
|
+
rows.push(truncateToWidth(theme.fg("accent", `┌─ ${block[0]}`), width));
|
|
62
|
+
for (const line of block.slice(1)) {
|
|
63
|
+
const color = /^(?:error\s*:)|\bfailed\b/i.test(line) ? "error" : "muted";
|
|
64
|
+
rows.push(truncateToWidth(theme.fg("accent", "│ ") + theme.fg(color, line), width));
|
|
65
|
+
}
|
|
66
|
+
rows.push(truncateToWidth(theme.fg("accent", "└─"), width));
|
|
67
|
+
});
|
|
68
|
+
return rows;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Pi 0.85 widget containers: preserve unrelated widgets and their lifecycle. */
|
|
72
|
+
export function attachAgentWidgets(tui: unknown, theme: Theme, expanded: () => boolean): () => void {
|
|
73
|
+
const host = tui as { children?: Array<Component & { children?: Component[] }> };
|
|
74
|
+
if (host.children?.length !== 7) return () => {};
|
|
75
|
+
const restores: Array<() => void> = [];
|
|
76
|
+
for (const index of [3, 5]) {
|
|
77
|
+
const container = host.children[index];
|
|
78
|
+
if (!Array.isArray(container?.children)) continue;
|
|
79
|
+
const descriptor = Object.getOwnPropertyDescriptor(container, "render");
|
|
80
|
+
const mouseDescriptor = Object.getOwnPropertyDescriptor(container, "handleMouse");
|
|
81
|
+
const render = (width: number) => container.children!.flatMap(child => restyleAgentWidget(child.render(width), theme, width, expanded()));
|
|
82
|
+
const mouse: NonNullable<Component["handleMouse"]> = (event) => {
|
|
83
|
+
let y = 0;
|
|
84
|
+
for (const child of container.children!) {
|
|
85
|
+
const original = child.render(event.width);
|
|
86
|
+
const shown = restyleAgentWidget(original, theme, event.width, expanded());
|
|
87
|
+
if (event.y >= y && event.y < y + shown.length) {
|
|
88
|
+
// Do not route clicks into native controls hidden by the compact view.
|
|
89
|
+
return shown === original ? child.handleMouse?.({ ...event, y: event.y - y, height: shown.length }) : undefined;
|
|
90
|
+
}
|
|
91
|
+
y += shown.length;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
container.render = render;
|
|
95
|
+
container.handleMouse = mouse;
|
|
96
|
+
restores.push(() => {
|
|
97
|
+
if (container.handleMouse === mouse) {
|
|
98
|
+
if (mouseDescriptor) Object.defineProperty(container, "handleMouse", mouseDescriptor);
|
|
99
|
+
else delete container.handleMouse;
|
|
100
|
+
}
|
|
101
|
+
if (container.render !== render) return;
|
|
102
|
+
if (descriptor) Object.defineProperty(container, "render", descriptor);
|
|
103
|
+
else delete (container as Partial<Component>).render;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return () => restores.forEach(restore => restore());
|
|
107
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Marked } from "@earendil-works/pi-tui";
|
|
2
|
+
import { render } from "grok-mermaid";
|
|
3
|
+
|
|
4
|
+
const parser = new Marked();
|
|
5
|
+
/** Keep incomplete, unsupported and over-wide diagrams readable as source. */
|
|
6
|
+
export function diagramMarkdown(source: string, width: number): string {
|
|
7
|
+
return parser.lexer(source).map((token) => {
|
|
8
|
+
if (token.type !== "code" || token.lang?.trim().toLowerCase() !== "mermaid") return token.raw;
|
|
9
|
+
const art = render(token.text);
|
|
10
|
+
if (!art || art.width > width || art.warnings.length) return token.raw;
|
|
11
|
+
return art.plain.map((line) => {
|
|
12
|
+
const content = line || "\u00a0";
|
|
13
|
+
const fence = "`".repeat(Math.max(0, ...Array.from(content.matchAll(/`+/g), (match) => match[0].length)) + 1);
|
|
14
|
+
return `${fence} ${content} ${fence}`;
|
|
15
|
+
}).join(" \n") + "\n\n";
|
|
16
|
+
}).join("");
|
|
17
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
type RGB = [number, number, number];
|
|
4
|
+
function ansiColor(ansi: string): RGB | undefined {
|
|
5
|
+
const rgb = ansi.match(/(?:38|48);2;(\d+);(\d+);(\d+)/);
|
|
6
|
+
if (rgb) return [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])];
|
|
7
|
+
const indexed = ansi.match(/(?:38|48);5;(\d+)/);
|
|
8
|
+
if (!indexed) return;
|
|
9
|
+
const n = Number(indexed[1]);
|
|
10
|
+
if (n >= 232) return [8 + (n - 232) * 10, 8 + (n - 232) * 10, 8 + (n - 232) * 10];
|
|
11
|
+
if (n >= 16) {
|
|
12
|
+
const levels = [0, 95, 135, 175, 215, 255];
|
|
13
|
+
return [levels[Math.floor((n - 16) / 36)], levels[Math.floor((n - 16) / 6) % 6], levels[(n - 16) % 6]];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** User gets the stronger accent surface; process gets its secondary tint. */
|
|
18
|
+
export function minimalSurface(theme: ExtensionContext["ui"]["theme"], text: string, user = false): string {
|
|
19
|
+
const base = ansiColor(theme.getBgAnsi?.("userMessageBg") ?? "");
|
|
20
|
+
const accent = ansiColor(theme.fg("accent", "")) ?? ansiColor(theme.fg("success", ""));
|
|
21
|
+
if (!base || !accent) return theme.bg("userMessageBg", text);
|
|
22
|
+
const weight = user ? 0.22 : 0.07;
|
|
23
|
+
const rgb = base.map((value, i) => Math.round(value * (1 - weight) + accent[i] * weight));
|
|
24
|
+
let prefix = `\x1b[48;2;${rgb.join(";")}m`;
|
|
25
|
+
if (theme.getColorMode?.() === "256color") {
|
|
26
|
+
const levels = [0, 95, 135, 175, 215, 255];
|
|
27
|
+
const indices = rgb.map((value) => levels.reduce((best, level, i) => Math.abs(level - value) < Math.abs(levels[best] - value) ? i : best, 0));
|
|
28
|
+
prefix = `\x1b[48;5;${16 + indices[0] * 36 + indices[1] * 6 + indices[2]}m`;
|
|
29
|
+
}
|
|
30
|
+
// Restore the surface after Markdown's inline reset sequences.
|
|
31
|
+
return prefix + text.replace(/\x1b\[(?:0|49)?m/g, (reset) => reset + prefix) + "\x1b[49m";
|
|
32
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Component } from "@earendil-works/pi-tui";
|
|
2
|
+
|
|
3
|
+
interface ContainerLike extends Component { children: Component[] }
|
|
4
|
+
function isContainer(value: unknown): value is ContainerLike {
|
|
5
|
+
const node = value as Partial<ContainerLike> | null;
|
|
6
|
+
return !!node && Array.isArray(node.children) && typeof node.render === "function" && typeof node.invalidate === "function";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Pi 0.85.x private layout adapter. Never mutates the stored messages or child tree. */
|
|
10
|
+
export type TurnNotices = ReadonlyMap<number, string[]>;
|
|
11
|
+
interface TranscriptView extends Component {
|
|
12
|
+
render(width: number, notices?: TurnNotices): string[];
|
|
13
|
+
}
|
|
14
|
+
interface NoticeOptions {
|
|
15
|
+
isTransient?: (text: string) => boolean;
|
|
16
|
+
now?: () => number;
|
|
17
|
+
schedule?: (callback: () => void, delay: number) => () => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Keep expiry across minimal-mode remounts without retaining discarded sessions.
|
|
21
|
+
const noticeTimes = new WeakMap<Component, { text: string; until: number }>();
|
|
22
|
+
export function attachTranscript(tui: unknown, view: TranscriptView, options: NoticeOptions = {}): (() => void) | undefined {
|
|
23
|
+
const host = tui as { children?: unknown[] };
|
|
24
|
+
// Both regular and fullscreen retain this seven-container tree. Fullscreen's
|
|
25
|
+
// ScrollView renders the same document instance. Fail closed on unknown layouts.
|
|
26
|
+
if (!Array.isArray(host.children) || host.children.length !== 7 || !host.children.every(isContainer)) return;
|
|
27
|
+
const document = host.children[0] as ContainerLike;
|
|
28
|
+
if (document.children.length !== 3 || !document.children.every(isContainer)) return;
|
|
29
|
+
const [header, resources, chat] = document.children as ContainerLike[];
|
|
30
|
+
// During /reload Pi replaces the editor with a reload notice until AFTER
|
|
31
|
+
// session_start. The validated document/container layout remains unchanged.
|
|
32
|
+
// Custom editors also need not expose getText; do not use editor contents
|
|
33
|
+
// as a transcript identity check.
|
|
34
|
+
const renderDescriptor = Object.getOwnPropertyDescriptor(document, "render");
|
|
35
|
+
const mouseDescriptor = Object.getOwnPropertyDescriptor(document, "handleMouse");
|
|
36
|
+
const originalRender = document.render;
|
|
37
|
+
const originalMouse = document.handleMouse;
|
|
38
|
+
const now = options.now ?? Date.now;
|
|
39
|
+
const schedule = options.schedule ?? ((callback, delay) => {
|
|
40
|
+
const timer = setTimeout(callback, delay);
|
|
41
|
+
timer.unref();
|
|
42
|
+
return () => clearTimeout(timer);
|
|
43
|
+
});
|
|
44
|
+
let cancelExpiry: (() => void) | undefined;
|
|
45
|
+
const render: Component["render"] = (width) => {
|
|
46
|
+
cancelExpiry?.();
|
|
47
|
+
cancelExpiry = undefined;
|
|
48
|
+
const notices = new Map<number, string[]>();
|
|
49
|
+
let turn = -1;
|
|
50
|
+
let nextExpiry = Infinity;
|
|
51
|
+
const time = now();
|
|
52
|
+
for (const child of chat.children) {
|
|
53
|
+
if (child.constructor.name === "UserMessageComponent") turn++;
|
|
54
|
+
if (child.constructor.name !== "Text") continue;
|
|
55
|
+
// Pi 0.85 Text stores the unwrapped styled source in text. Width changes
|
|
56
|
+
// must not restart the timeout; setText updates must restart it.
|
|
57
|
+
const source = (child as unknown as { text?: unknown }).text;
|
|
58
|
+
if (typeof source === "string" && options.isTransient?.(source)) {
|
|
59
|
+
let state = noticeTimes.get(child);
|
|
60
|
+
if (!state || state.text !== source) {
|
|
61
|
+
state = { text: source, until: time + 5000 };
|
|
62
|
+
noticeTimes.set(child, state);
|
|
63
|
+
}
|
|
64
|
+
if (time >= state.until) continue;
|
|
65
|
+
nextExpiry = Math.min(nextExpiry, state.until);
|
|
66
|
+
}
|
|
67
|
+
const rows = notices.get(turn) ?? [];
|
|
68
|
+
rows.push(...child.render(width));
|
|
69
|
+
notices.set(turn, rows);
|
|
70
|
+
}
|
|
71
|
+
if (Number.isFinite(nextExpiry)) {
|
|
72
|
+
cancelExpiry = schedule(() => {
|
|
73
|
+
cancelExpiry = undefined;
|
|
74
|
+
(tui as { requestRender?: () => void }).requestRender?.();
|
|
75
|
+
}, Math.max(0, nextExpiry - time));
|
|
76
|
+
}
|
|
77
|
+
return [...header.render(width), ...resources.render(width),
|
|
78
|
+
...(notices.get(-1) ?? []), ...view.render(width, notices)];
|
|
79
|
+
};
|
|
80
|
+
// Do not dispatch pointer events into invisible native message components.
|
|
81
|
+
// Unhandled events still reach the parent ScrollView / selection machinery.
|
|
82
|
+
const mouse: NonNullable<Component["handleMouse"]> = () => undefined;
|
|
83
|
+
document.render = render;
|
|
84
|
+
document.handleMouse = mouse;
|
|
85
|
+
return () => {
|
|
86
|
+
cancelExpiry?.();
|
|
87
|
+
if (document.render === render) {
|
|
88
|
+
if (renderDescriptor) Object.defineProperty(document, "render", renderDescriptor);
|
|
89
|
+
else { delete (document as Partial<Component>).render; if (document.render !== originalRender) document.render = originalRender; }
|
|
90
|
+
}
|
|
91
|
+
if (document.handleMouse === mouse) {
|
|
92
|
+
if (mouseDescriptor) Object.defineProperty(document, "handleMouse", mouseDescriptor);
|
|
93
|
+
else { delete document.handleMouse; if (document.handleMouse !== originalMouse) document.handleMouse = originalMouse; }
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@each1024/pi-mini-mode",
|
|
3
|
+
"version": "1.3.1",
|
|
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/footer-status-self-check.mjs && node test/settings-interaction.mjs && node test/transcript-adapter.mjs && node test/minimal-markdown.mjs && node test/agent-view.mjs && node test/publish-self-check.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/mini-lens-settings.mp4",
|
|
29
|
+
"extensions": [
|
|
30
|
+
"./extensions/footer-status.ts"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@earendil-works/pi-coding-agent": ">=0.84.0",
|
|
35
|
+
"@earendil-works/pi-tui": "*"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"extensions",
|
|
39
|
+
"lib",
|
|
40
|
+
"README.md",
|
|
41
|
+
"README.zh-CN.md",
|
|
42
|
+
"LICENSE"
|
|
43
|
+
],
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@earendil-works/pi-coding-agent": "^0.85.0",
|
|
49
|
+
"@earendil-works/pi-tui": "^0.85.0",
|
|
50
|
+
"@types/node": "^26.4.1",
|
|
51
|
+
"typescript": "^5.9.3"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"grok-mermaid": "0.2.2"
|
|
55
|
+
}
|
|
56
|
+
}
|