@herbertgao/pi-extensions 2026.11.17 → 2026.11.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +3 -3
- package/THIRD_PARTY_NOTICES.md +2 -23
- package/node_modules/@estebanforge/pi-antigravity-bridge/CHANGELOG.md +956 -0
- package/node_modules/{pi-antigravity → @estebanforge/pi-antigravity-bridge}/LICENSE +1 -1
- package/node_modules/@estebanforge/pi-antigravity-bridge/README.md +240 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/extensions/index.ts +2117 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/package.json +61 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/connection.ts +644 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/driver.ts +1043 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/events.ts +323 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/home.ts +145 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/jsonrpc.ts +243 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/setup.ts +107 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/acp/usage-estimate.ts +61 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agents.ts +64 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-install.ts +93 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-paths.ts +30 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-setup.ts +81 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/agy-version.ts +289 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-detect.ts +161 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-gate.ts +233 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/approval-hook.ts +325 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts-ui.ts +88 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/artifacts.ts +144 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/ask-tool.ts +1221 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/background-watch.ts +225 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/bridge-catalog.ts +197 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/config.ts +523 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/daily-log.ts +196 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/dialog-lock.ts +48 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/diff-render.ts +207 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver-types.ts +168 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/driver.ts +907 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/engine-picker.ts +160 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/frame-guard.ts +38 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/handoff-toast.ts +21 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-registration.ts +376 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/mcp-server.ts +618 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/models.ts +404 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/native-tools.ts +104 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/parked-turn.ts +125 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/patch-cleanup.ts +523 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/poll-fold.ts +55 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/prompt-images.ts +92 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/provider.ts +2214 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/queued-messages.ts +152 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/quota-notice.ts +40 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/redact.ts +27 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/sessions.ts +197 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/skills.ts +207 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-events.ts +161 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/stream-sanitizer.ts +180 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/subagent-roster.ts +245 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/sysprompt-agent.ts +130 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks-ui.ts +90 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/tasks.ts +174 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/usage.ts +174 -0
- package/node_modules/@estebanforge/pi-antigravity-bridge/src/web-tools.ts +320 -0
- package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +12 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/config/panel.ts +1 -1
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/feature/shell/prompt-editor.ts +3 -1
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/index.ts +7 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/agent-badge.ts +244 -0
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/ccstyle-tool.ts +1 -1
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/grouping.ts +10 -3
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/names.ts +18 -0
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +1 -1
- package/node_modules/@herbertgao/pi-inline-skill-commands/README.md +18 -0
- package/node_modules/@herbertgao/pi-inline-skill-commands/package.json +1 -1
- package/node_modules/@herbertgao/pi-inline-skill-commands/src/index.ts +80 -0
- package/node_modules/@herbertgao/pi-subagents/CHANGELOG.md +320 -102
- package/node_modules/@herbertgao/pi-subagents/SECURITY.md +2 -2
- package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +240 -30
- package/node_modules/@herbertgao/pi-subagents/docs/translations.md +63 -0
- package/node_modules/@herbertgao/pi-subagents/docs/workflows.md +1 -1
- package/node_modules/@herbertgao/pi-subagents/package.json +4 -4
- package/node_modules/@herbertgao/pi-subagents/src/agent-file-toggle.ts +2 -0
- package/node_modules/@herbertgao/pi-subagents/src/agent-manager.ts +89 -6
- package/node_modules/@herbertgao/pi-subagents/src/agent-runner.ts +6 -2
- package/node_modules/@herbertgao/pi-subagents/src/agent-types.ts +27 -13
- package/node_modules/@herbertgao/pi-subagents/src/cache-retention.ts +39 -0
- package/node_modules/@herbertgao/pi-subagents/src/completion-outbox.ts +5 -0
- package/node_modules/@herbertgao/pi-subagents/src/cross-extension-rpc.ts +217 -41
- package/node_modules/@herbertgao/pi-subagents/src/custom-agents.ts +7 -0
- package/node_modules/@herbertgao/pi-subagents/src/group-join.ts +9 -0
- package/node_modules/@herbertgao/pi-subagents/src/index.ts +126 -93
- package/node_modules/@herbertgao/pi-subagents/src/mention-clone.ts +3 -2
- package/node_modules/@herbertgao/pi-subagents/src/settings.ts +21 -0
- package/node_modules/@herbertgao/pi-subagents/src/subagent-info.ts +115 -0
- package/node_modules/@herbertgao/pi-subagents/src/types.ts +11 -7
- package/node_modules/@herbertgao/pi-subagents/src/ui/agent-widget.ts +2 -3
- package/node_modules/@herbertgao/pi-subagents/src/ui/conversation-viewer.ts +1 -1
- package/node_modules/@herbertgao/pi-subagents/src/workflow/meta.ts +2 -2
- package/node_modules/@herbertgao/pi-subagents/src/workflow-info.ts +184 -0
- package/node_modules/pi-commandcode-provider/CHANGELOG.md +10 -0
- package/node_modules/pi-commandcode-provider/README.md +3 -2
- package/node_modules/pi-commandcode-provider/index.ts +120 -13
- package/node_modules/pi-commandcode-provider/package.json +4 -3
- package/node_modules/pi-commandcode-provider/src/commandcode-catalog.ts +11 -2
- package/node_modules/pi-commandcode-provider/src/pricing.ts +17 -1
- package/node_modules/pi-commandcode-provider/src/quota-command.ts +3 -2
- package/node_modules/pi-commandcode-provider/src/zdr.ts +163 -0
- package/package.json +12 -15
- package/node_modules/pi-antigravity/README.md +0 -298
- package/node_modules/pi-antigravity/docs/reader-guarantees.md +0 -35
- package/node_modules/pi-antigravity/package.json +0 -87
- package/node_modules/pi-antigravity/src/auth/accounts.ts +0 -327
- package/node_modules/pi-antigravity/src/auth/index.ts +0 -23
- package/node_modules/pi-antigravity/src/auth/oauth.ts +0 -445
- package/node_modules/pi-antigravity/src/client/client.ts +0 -602
- package/node_modules/pi-antigravity/src/client/enterprise.ts +0 -150
- package/node_modules/pi-antigravity/src/client/index.ts +0 -2
- package/node_modules/pi-antigravity/src/diagnostics/diagnostics.ts +0 -104
- package/node_modules/pi-antigravity/src/diagnostics/index.ts +0 -1
- package/node_modules/pi-antigravity/src/features/generate-image.ts +0 -132
- package/node_modules/pi-antigravity/src/features/google-search.ts +0 -72
- package/node_modules/pi-antigravity/src/features/read-url-content.ts +0 -124
- package/node_modules/pi-antigravity/src/image/image.ts +0 -427
- package/node_modules/pi-antigravity/src/image/index.ts +0 -1
- package/node_modules/pi-antigravity/src/image/native.ts +0 -69
- package/node_modules/pi-antigravity/src/index.ts +0 -547
- package/node_modules/pi-antigravity/src/models/discovery.ts +0 -176
- package/node_modules/pi-antigravity/src/models/grouping.ts +0 -487
- package/node_modules/pi-antigravity/src/models/index.ts +0 -3
- package/node_modules/pi-antigravity/src/models/models.ts +0 -647
- package/node_modules/pi-antigravity/src/search/index.ts +0 -1
- package/node_modules/pi-antigravity/src/search/search.ts +0 -967
- package/node_modules/pi-antigravity/src/stream/index.ts +0 -1
- package/node_modules/pi-antigravity/src/stream/stream.ts +0 -1961
- package/node_modules/pi-antigravity/src/types/enums.ts +0 -52
- package/node_modules/pi-antigravity/src/types/index.ts +0 -2
- package/node_modules/pi-antigravity/src/types/types.ts +0 -329
- package/node_modules/pi-antigravity/src/usage/index.ts +0 -1
- package/node_modules/pi-antigravity/src/usage/usage.ts +0 -437
- package/node_modules/pi-antigravity/src/utils/address.ts +0 -180
- package/node_modules/pi-antigravity/src/utils/credits.ts +0 -149
- package/node_modules/pi-antigravity/src/utils/html.ts +0 -31
- package/node_modules/pi-antigravity/src/utils/http.ts +0 -96
- package/node_modules/pi-antigravity/src/utils/index.ts +0 -5
- package/node_modules/pi-antigravity/src/utils/security.ts +0 -278
- package/node_modules/pi-antigravity/src/utils/text.ts +0 -66
- package/node_modules/pi-antigravity/src/utils/util.ts +0 -217
- package/node_modules/pi-antigravity/src/web-fetch/document.ts +0 -83
- package/node_modules/pi-antigravity/src/web-fetch/fences.ts +0 -24
- package/node_modules/pi-antigravity/src/web-fetch/header.ts +0 -97
- package/node_modules/pi-antigravity/src/web-fetch/index.ts +0 -3
- package/node_modules/pi-antigravity/src/web-fetch/inspect.ts +0 -328
- package/node_modules/pi-antigravity/src/web-fetch/limits.ts +0 -4
- package/node_modules/pi-antigravity/src/web-fetch/markdown.ts +0 -744
- package/node_modules/pi-antigravity/src/web-fetch/mdn.ts +0 -164
- package/node_modules/pi-antigravity/src/web-fetch/media.ts +0 -11
- package/node_modules/pi-antigravity/src/web-fetch/metadata.ts +0 -9
- package/node_modules/pi-antigravity/src/web-fetch/representation.ts +0 -93
- package/node_modules/pi-antigravity/src/web-fetch/save.ts +0 -71
- package/node_modules/pi-antigravity/src/web-fetch/sites.ts +0 -138
- package/node_modules/pi-antigravity/src/web-fetch/vendor.d.ts +0 -9
- package/node_modules/pi-antigravity/src/web-fetch/web-fetch.ts +0 -957
- package/node_modules/pi-antigravity/tsconfig.json +0 -21
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Web tools for Pi: agy_web_search + agy_read_url, backed by one-shot
|
|
2
|
+
// search-only `agy` runs. Off by default (config.webTools); their audience is
|
|
3
|
+
// sessions driven by NON-Antigravity providers - inside agy sessions the model
|
|
4
|
+
// already has search_web/read_url_content natively (live-probed 2026-09-23 on
|
|
5
|
+
// both engines).
|
|
6
|
+
//
|
|
7
|
+
// Each call spawns `agy --agent <temporary> --print <query> --mode plan
|
|
8
|
+
// --output-format stream-json` with a temporary agent restricted to
|
|
9
|
+
// search_web + read_url_content (bridge MCP inheritance off), then gates the
|
|
10
|
+
// answer on an observed allowed-tool step. Gate accepts both status spellings
|
|
11
|
+
// the CLI has shipped (driver.ts: "SUCCESS" live, "OK" older builds).
|
|
12
|
+
//
|
|
13
|
+
// Temp agent dirs live in the user-global ~/.gemini/config/agents (the only
|
|
14
|
+
// location agy 1.2.9 discovers; no --agent-dir flag exists). Normal cleanup is
|
|
15
|
+
// the `finally`; SIGKILL can orphan, so sweepStaleWebAgents() removes leftovers
|
|
16
|
+
// at registration - same hygiene doctrine as mcp-registration's pid sweep.
|
|
17
|
+
|
|
18
|
+
import { spawn } from "node:child_process";
|
|
19
|
+
import { randomBytes } from "node:crypto";
|
|
20
|
+
import { mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
21
|
+
import os from "node:os";
|
|
22
|
+
import path from "node:path";
|
|
23
|
+
import { Type } from "typebox";
|
|
24
|
+
import { redactText } from "./redact.js";
|
|
25
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
26
|
+
|
|
27
|
+
export const WEB_AGENT_PREFIX = "pi-bridge-web-";
|
|
28
|
+
|
|
29
|
+
const DEADLINE_MS = 120_000;
|
|
30
|
+
const MAX_OUTPUT_BYTES = 2 * 1024 * 1024;
|
|
31
|
+
const MAX_QUERY_CHARS = 2000;
|
|
32
|
+
const ALLOWED_TOOLS = new Set(["search_web", "read_url_content"]);
|
|
33
|
+
const ORPHAN_GRACE_MS = 24 * 60 * 60 * 1000;
|
|
34
|
+
|
|
35
|
+
/** ~/.gemini/config/agents - the directory agy scans for named agents. */
|
|
36
|
+
export function webAgentsRoot(): string {
|
|
37
|
+
return path.join(os.homedir(), ".gemini", "config", "agents");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Remove leftover pi-bridge agent dirs matching `prefix`: dead-pid markers
|
|
41
|
+
* always, and marker-less dirs only after a grace period (a live sibling
|
|
42
|
+
* process may be between mkdir and its pid write). Never touches foreign
|
|
43
|
+
* agent dirs. The ask-tool reuses this for its reviewer agents. */
|
|
44
|
+
export function sweepStaleWebAgents(
|
|
45
|
+
root: string = webAgentsRoot(),
|
|
46
|
+
now = Date.now(),
|
|
47
|
+
prefix: string = WEB_AGENT_PREFIX,
|
|
48
|
+
): void {
|
|
49
|
+
let entries: string[];
|
|
50
|
+
try {
|
|
51
|
+
entries = readdirSync(root);
|
|
52
|
+
} catch {
|
|
53
|
+
return; // no agents dir yet: nothing to sweep
|
|
54
|
+
}
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
if (!entry.startsWith(prefix)) continue;
|
|
57
|
+
try {
|
|
58
|
+
if (isStaleWebAgentDir(path.join(root, entry), now)) {
|
|
59
|
+
rmSync(path.join(root, entry), { recursive: true, force: true });
|
|
60
|
+
}
|
|
61
|
+
} catch {
|
|
62
|
+
// vanished mid-sweep: nothing to remove
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Stale = a dead-pid marker, or no marker at all and older than the grace
|
|
68
|
+
* period (a live sibling may sit between mkdir and its pid write). */
|
|
69
|
+
function isStaleWebAgentDir(dir: string, now: number): boolean {
|
|
70
|
+
let raw = "";
|
|
71
|
+
try {
|
|
72
|
+
raw = readFileSync(path.join(dir, ".pid"), "utf8").trim();
|
|
73
|
+
} catch {
|
|
74
|
+
raw = ""; // no marker yet: decide by age
|
|
75
|
+
}
|
|
76
|
+
const pid = Number.parseInt(raw, 10);
|
|
77
|
+
if (Number.isInteger(pid) && pid > 0) return !pidAlive(pid);
|
|
78
|
+
try {
|
|
79
|
+
return now - statSync(dir).mtimeMs > ORPHAN_GRACE_MS;
|
|
80
|
+
} catch {
|
|
81
|
+
return true; // dir vanished mid-sweep
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Best effort: a signal-able pid proves *a* live process, not the original
|
|
86
|
+
* owner; pid reuse can mask an orphan until the 24h age bound. Same residual
|
|
87
|
+
* as mcp-registration's sweep. */
|
|
88
|
+
function pidAlive(pid: number): boolean {
|
|
89
|
+
try {
|
|
90
|
+
process.kill(pid, 0);
|
|
91
|
+
return true;
|
|
92
|
+
} catch {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type WebRunResult =
|
|
98
|
+
| { ok: true; response: string }
|
|
99
|
+
| { ok: false; error: string };
|
|
100
|
+
|
|
101
|
+
export interface WebRunOptions {
|
|
102
|
+
/** Query (agy_web_search) or URL-reading instruction (agy_read_url). */
|
|
103
|
+
prompt: string;
|
|
104
|
+
/** The single allowed tool whose DONE step the answer is gated on. */
|
|
105
|
+
gatedTool: "search_web" | "read_url_content";
|
|
106
|
+
/** Server binary; defaults to AGY_BIN or PATH, matching ask-tool. */
|
|
107
|
+
bin?: string;
|
|
108
|
+
/** Agents root override (tests); defaults to the user-global agy dir. */
|
|
109
|
+
agentsRoot?: string;
|
|
110
|
+
/** Working directory handed to agy. */
|
|
111
|
+
cwd?: string;
|
|
112
|
+
timeoutMs?: number;
|
|
113
|
+
signal?: AbortSignal;
|
|
114
|
+
log?: (event: string, data?: unknown, level?: "info" | "warn" | "error") => void;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** agent.md for the one-shot web delegate: search-only, no inheritance. */
|
|
118
|
+
export function webAgentMd(name: string): string {
|
|
119
|
+
return `---\nname: ${name}\ndescription: Temporary Pi web delegate\nmainAgent: true\nsubagent: false\nmodel: inherit\nexcludeDefaultComponents: true\ninheritCustomizations: false\ninheritMcp: false\ncommandExecutionPolicy: auto\ntools:\n - search_web\n - read_url_content\nskills: []\nrules: []\nagents: []\nmcpServers: []\n---\n\nAnswer the user's query using search_web (and read_url_content when a page must be opened). Cite sources. Use no other tools.\n`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** One gated one-shot web run. Creates a unique restricted agent, streams the
|
|
123
|
+
* NDJSON frames, refuses answers with no observed allowed-tool step, and
|
|
124
|
+
* always removes the agent dir. */
|
|
125
|
+
export async function runWebAgent(opts: WebRunOptions): Promise<WebRunResult> {
|
|
126
|
+
const root = opts.agentsRoot ?? webAgentsRoot();
|
|
127
|
+
mkdirSync(root, { recursive: true });
|
|
128
|
+
// Nonce: two parallel tool calls in one process can share a millisecond;
|
|
129
|
+
// a shared dir would let one call's finally delete the other's agent.
|
|
130
|
+
const agentName = `${WEB_AGENT_PREFIX}${process.pid}-${Date.now().toString(36)}-${randomBytes(4).toString("hex")}`;
|
|
131
|
+
const agentDir = path.join(root, agentName);
|
|
132
|
+
mkdirSync(agentDir, { recursive: true, mode: 0o700 });
|
|
133
|
+
// commandExecutionPolicy stays at the live-validated "auto": the template's
|
|
134
|
+
// tools list has no command tool and plan mode blocks edits; an unvalidated
|
|
135
|
+
// stricter enum risks agent discovery failing outright.
|
|
136
|
+
writeFileSync(path.join(agentDir, "agent.md"), webAgentMd(agentName), {
|
|
137
|
+
mode: 0o600,
|
|
138
|
+
});
|
|
139
|
+
writeFileSync(path.join(agentDir, ".pid"), `${process.pid}\n`, { mode: 0o600 });
|
|
140
|
+
|
|
141
|
+
const bin = opts.bin ?? process.env.AGY_BIN ?? "agy";
|
|
142
|
+
const args = [
|
|
143
|
+
"--agent", agentName,
|
|
144
|
+
"--print", opts.prompt,
|
|
145
|
+
"--mode", "plan",
|
|
146
|
+
"--output-format", "stream-json",
|
|
147
|
+
"--disable-slash-commands",
|
|
148
|
+
];
|
|
149
|
+
opts.log?.("web-run-start", { agent: agentName, gatedTool: opts.gatedTool });
|
|
150
|
+
|
|
151
|
+
const child = spawn(bin, args, { cwd: opts.cwd ?? process.cwd(), stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
|
|
152
|
+
const stop = () => {
|
|
153
|
+
if (!child.killed) child.kill();
|
|
154
|
+
};
|
|
155
|
+
const onAbort = () => stop();
|
|
156
|
+
opts.signal?.addEventListener("abort", onAbort, { once: true });
|
|
157
|
+
const timer = setTimeout(stop, opts.timeoutMs ?? DEADLINE_MS);
|
|
158
|
+
|
|
159
|
+
let buffer = "";
|
|
160
|
+
let bytes = 0;
|
|
161
|
+
let stderrTail = "";
|
|
162
|
+
let failed = "";
|
|
163
|
+
let allowedToolUsed = false;
|
|
164
|
+
let response = "";
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
child.stdout?.on("data", (chunk: Buffer) => {
|
|
168
|
+
bytes += chunk.length;
|
|
169
|
+
if (bytes > MAX_OUTPUT_BYTES && !failed) {
|
|
170
|
+
failed = "Antigravity web run output exceeded the 2 MiB limit";
|
|
171
|
+
stop();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
buffer += chunk.toString("utf8");
|
|
175
|
+
let newline = buffer.indexOf("\n");
|
|
176
|
+
while (newline !== -1 && !failed) {
|
|
177
|
+
handleFrame(buffer.slice(0, newline));
|
|
178
|
+
buffer = buffer.slice(newline + 1);
|
|
179
|
+
newline = buffer.indexOf("\n");
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
child.stderr?.on("data", (chunk: Buffer) => {
|
|
183
|
+
stderrTail = (stderrTail + chunk.toString("utf8")).slice(-1000);
|
|
184
|
+
});
|
|
185
|
+
const code = await new Promise<number | null>((resolve, reject) => {
|
|
186
|
+
child.once("error", reject);
|
|
187
|
+
child.once("close", resolve);
|
|
188
|
+
});
|
|
189
|
+
if (!failed && opts.signal?.aborted) failed = "web run aborted";
|
|
190
|
+
if (!failed && code !== 0) failed = `agy exited with code ${code}${stderrTail ? `: ${redactText(stderrTail.trim()).slice(-300)}` : ""}`;
|
|
191
|
+
if (!failed && !allowedToolUsed) failed = `Antigravity returned an answer without an observed ${opts.gatedTool} step; refusing it as unverified`;
|
|
192
|
+
if (!failed && response === "") failed = "Antigravity web run produced no answer";
|
|
193
|
+
if (failed) {
|
|
194
|
+
opts.log?.("web-run-failed", { agent: agentName, error: failed }, "warn");
|
|
195
|
+
return { ok: false, error: failed };
|
|
196
|
+
}
|
|
197
|
+
opts.log?.("web-run-ok", { agent: agentName });
|
|
198
|
+
return { ok: true, response };
|
|
199
|
+
} catch (error) {
|
|
200
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
201
|
+
opts.log?.("web-run-failed", { agent: agentName, error: message }, "warn");
|
|
202
|
+
return { ok: false, error: message };
|
|
203
|
+
} finally {
|
|
204
|
+
clearTimeout(timer);
|
|
205
|
+
opts.signal?.removeEventListener("abort", onAbort);
|
|
206
|
+
stop();
|
|
207
|
+
rmSync(agentDir, { recursive: true, force: true });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function handleFrame(line: string): void {
|
|
211
|
+
if (failed || !line.startsWith("{")) return;
|
|
212
|
+
let frame: unknown;
|
|
213
|
+
try {
|
|
214
|
+
frame = JSON.parse(line);
|
|
215
|
+
} catch {
|
|
216
|
+
return; // non-JSON chatter: ignore, matching the driver's line loop
|
|
217
|
+
}
|
|
218
|
+
if (typeof frame !== "object" || frame === null) return;
|
|
219
|
+
const record = frame as Record<string, unknown>;
|
|
220
|
+
if (record.event === "step_update" && typeof record.step_update === "object" && record.step_update !== null) {
|
|
221
|
+
const step = record.step_update as Record<string, unknown>;
|
|
222
|
+
if (step.step_type !== "tool" || typeof step.tool_name !== "string") return;
|
|
223
|
+
if (ALLOWED_TOOLS.has(step.tool_name)) {
|
|
224
|
+
// Drift tolerance: DONE today, OK on older builds (driver.ts:507).
|
|
225
|
+
if (step.state === "DONE" || step.state === "OK") allowedToolUsed = true;
|
|
226
|
+
} else if (step.state === "ACTIVE" || step.state === "DONE") {
|
|
227
|
+
failed = `Antigravity web agent used a disallowed tool: ${step.tool_name}`;
|
|
228
|
+
stop();
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (record.event === "result" && typeof record.result === "object" && record.result !== null) {
|
|
233
|
+
const result = record.result as Record<string, unknown>;
|
|
234
|
+
if (result.status !== "SUCCESS" && result.status !== "OK") {
|
|
235
|
+
failed = `Antigravity web run reported status ${String(result.status)}`;
|
|
236
|
+
stop();
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (typeof result.response === "string") response = result.response;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const QUOTA_NOTE =
|
|
245
|
+
"Each call spawns a one-shot Antigravity CLI process and consumes your Antigravity quota.";
|
|
246
|
+
|
|
247
|
+
const toolText = (text: string) => ({ content: [{ type: "text" as const, text }], details: {} });
|
|
248
|
+
const toolError = (text: string) => ({ content: [{ type: "text" as const, text }], isError: true as const, details: {} });
|
|
249
|
+
|
|
250
|
+
/** Register agy_web_search + agy_read_url. Call only when config.webTools is
|
|
251
|
+
* on - registration is the opt-in. */
|
|
252
|
+
export function registerWebTools(
|
|
253
|
+
pi: ExtensionAPI,
|
|
254
|
+
opts: { bin?: string; cwd?: string; timeoutMs?: number; log?: WebRunOptions["log"] } = {},
|
|
255
|
+
): void {
|
|
256
|
+
sweepStaleWebAgents();
|
|
257
|
+
|
|
258
|
+
pi.registerTool({
|
|
259
|
+
name: "agy_web_search",
|
|
260
|
+
label: "Antigravity Web Search",
|
|
261
|
+
description: `Search the live web via Google Antigravity. Returns a short cited answer, not raw results. Use when the question needs current information. ${QUOTA_NOTE}`,
|
|
262
|
+
parameters: Type.Object({
|
|
263
|
+
query: Type.String({
|
|
264
|
+
description: `What to search for. Max ${MAX_QUERY_CHARS} characters.`,
|
|
265
|
+
}),
|
|
266
|
+
}),
|
|
267
|
+
async execute(_toolCallId, params, signal) {
|
|
268
|
+
const query = typeof params.query === "string" ? params.query.trim() : "";
|
|
269
|
+
if (query === "") return toolError("query must be a non-empty string");
|
|
270
|
+
if (query.length > MAX_QUERY_CHARS) return toolError(`query exceeds ${MAX_QUERY_CHARS} characters`);
|
|
271
|
+
const run = await runWebAgent({
|
|
272
|
+
prompt: query,
|
|
273
|
+
gatedTool: "search_web",
|
|
274
|
+
bin: opts.bin,
|
|
275
|
+
cwd: opts.cwd,
|
|
276
|
+
timeoutMs: opts.timeoutMs,
|
|
277
|
+
signal,
|
|
278
|
+
log: opts.log,
|
|
279
|
+
});
|
|
280
|
+
if (!run.ok) return toolError(`Web search failed: ${run.error}`);
|
|
281
|
+
return toolText(run.response);
|
|
282
|
+
},
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
pi.registerTool({
|
|
286
|
+
name: "agy_read_url",
|
|
287
|
+
label: "Antigravity Read URL",
|
|
288
|
+
description: `Fetch the content of a public http(s) URL via Google Antigravity and get its main content back as text. Use for pages you need to read. ${QUOTA_NOTE}`,
|
|
289
|
+
parameters: Type.Object({
|
|
290
|
+
url: Type.String({
|
|
291
|
+
description: "Absolute http(s) URL to read.",
|
|
292
|
+
}),
|
|
293
|
+
}),
|
|
294
|
+
async execute(_toolCallId, params, signal) {
|
|
295
|
+
const raw = typeof params.url === "string" ? params.url.trim() : "";
|
|
296
|
+
let url: URL;
|
|
297
|
+
try {
|
|
298
|
+
url = new URL(raw);
|
|
299
|
+
} catch {
|
|
300
|
+
return toolError("url must be an absolute http(s) URL");
|
|
301
|
+
}
|
|
302
|
+
// Allowlist: a URL parameter is untrusted input, and schemes like
|
|
303
|
+
// file: or javascript: must never reach the delegate.
|
|
304
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
305
|
+
return toolError("only http and https URLs are supported");
|
|
306
|
+
}
|
|
307
|
+
const run = await runWebAgent({
|
|
308
|
+
prompt: `Read the content of ${url.toString()} with read_url_content and return its main content as plain text. Keep the structure (headings, lists) but drop navigation and ads.`,
|
|
309
|
+
gatedTool: "read_url_content",
|
|
310
|
+
bin: opts.bin,
|
|
311
|
+
cwd: opts.cwd,
|
|
312
|
+
timeoutMs: opts.timeoutMs,
|
|
313
|
+
signal,
|
|
314
|
+
log: opts.log,
|
|
315
|
+
});
|
|
316
|
+
if (!run.ok) return toolError(`URL read failed: ${run.error}`);
|
|
317
|
+
return toolText(run.response);
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @herbertgao/pi-cc-extensions
|
|
2
2
|
|
|
3
|
+
## 1.0.21-local.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#406](https://github.com/HerbertGao/pi-extensions/pull/406) [`364e65f`](https://github.com/HerbertGao/pi-extensions/commit/364e65f8a9e4bf7a9d79c58c0a54ebedd390dba4) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Fix input editor prefix rendering for Pi 1.6+ `render()` readonly line buffers.
|
|
8
|
+
|
|
9
|
+
## 1.0.21-local.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#402](https://github.com/HerbertGao/pi-extensions/pull/402) [`f0109df`](https://github.com/HerbertGao/pi-extensions/commit/f0109dfd7bb213fe26260a0c882e45d2bf83935d) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Color the ccstyle Agent tool rows with each type's configured badge. pi-subagents exposes the label and color of every spawnable type over a new `subagents:rpc:list-agents` bus channel and announces registry changes with a `subagents:agents-changed` event; pi-cc-extensions pulls the table on `subagents:ready` and renders Agent rows as padded background badges (same palette, hex, 256-color quantization, and contrast rules as pi-subagents' own badges). Without pi-subagents on the bus the rows stay plain text.
|
|
14
|
+
|
|
3
15
|
## 1.0.21-local.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -1253,7 +1253,7 @@ export async function showCcstylePanel(
|
|
|
1253
1253
|
render(width: number): string[] {
|
|
1254
1254
|
const safeWidth = Math.max(0, Math.floor(width));
|
|
1255
1255
|
const rule = renderPanelRule(theme, safeWidth);
|
|
1256
|
-
const body = activeList().render(safeWidth);
|
|
1256
|
+
const body = [...activeList().render(safeWidth)];
|
|
1257
1257
|
// Drop SettingsList's built-in hint — the panel footer below is the single source.
|
|
1258
1258
|
while (body.length > 0 && body[body.length - 1] === "") body.pop();
|
|
1259
1259
|
const listHintIndex = body.findIndex(
|
|
@@ -37,7 +37,9 @@ export class PromptEditor extends CustomEditor {
|
|
|
37
37
|
userPaddingX = saved;
|
|
38
38
|
self.paddingX = saved + PROMPT_COLS;
|
|
39
39
|
try {
|
|
40
|
-
|
|
40
|
+
// Pi 1.6+:`render()` 可能返回缓存的 `readonly string[]`,不得原地改写父级缓冲区。
|
|
41
|
+
const parentLines = super.render(width);
|
|
42
|
+
const lines = [...parentLines];
|
|
41
43
|
// 布局:lines[0] 顶边框,lines[1..n-2] 内容,lines[n-1] 底边框(autocomplete 在底边框后追加,
|
|
42
44
|
// 首内容行恒为 index 1)。开头是 paddingX 个纯空格,前两列换成 ❯+空格,剩余 padding 保留;
|
|
43
45
|
// 续行不动——Claude Code 行为。
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ToolExecutionComponent, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import type { CompactThinkingController } from "../feature/compact-thinking.ts";
|
|
3
|
+
import { initAgentBadges, refreshAgentBadges } from "./tool/agent-badge.ts";
|
|
3
4
|
import { installToolGrouping, type ToolGroupingHooks } from "./tool/grouping.ts";
|
|
4
5
|
import {
|
|
5
6
|
installCompactMode,
|
|
@@ -116,6 +117,9 @@ export default function (
|
|
|
116
117
|
) {
|
|
117
118
|
// 可选 override:集成测试不依赖用户全局配置。
|
|
118
119
|
if (configOverride) setConfig(normalizeConfig({ ...config, ...configOverride }));
|
|
120
|
+
// Agent 行的徽章颜色表:subagents:ready 后拉取、agents-changed 时刷新。
|
|
121
|
+
// pi-subagents 缺席或过旧时缓存为空,行渲染保持纯文本。
|
|
122
|
+
initAgentBadges(pi.events);
|
|
119
123
|
const writeExecutionMetadata = new WriteExecutionMetadataStore();
|
|
120
124
|
const mouseOwner = {};
|
|
121
125
|
let installation:
|
|
@@ -239,6 +243,9 @@ export default function (
|
|
|
239
243
|
});
|
|
240
244
|
|
|
241
245
|
pi.on("session_start", async (event, ctx) => {
|
|
246
|
+
// 徽章表随会话重新拉取:pi-subagents 在场时无论 session_start 处理顺序
|
|
247
|
+
// 先后都会填上;缺席时超时清空,保持纯文本而不是沿用旧颜色。
|
|
248
|
+
refreshAgentBadges(pi.events);
|
|
242
249
|
// 延后一拍再确认 write 归属:其他扩展同样在各自的 session_start 里注册工具,
|
|
243
250
|
// 本 handler 执行时可能还看不到对方,此时注册会与对方撞名(注册表先到先得),
|
|
244
251
|
// 对方的 write 会被静默丢弃。等所有 session_start 跑完,检测结果才是权威的。
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-badge.ts — pi-subagents agent badges on ccstyle tool rows.
|
|
3
|
+
*
|
|
4
|
+
* The color math mirrors pi-subagents/src/agent-color.ts (pure ANSI geometry:
|
|
5
|
+
* palette names, hex, 256-color quantization, WCAG contrast). It is copied
|
|
6
|
+
* rather than imported because the two packages must stay independently
|
|
7
|
+
* installable; keep the copies in sync when that file changes.
|
|
8
|
+
*
|
|
9
|
+
* The type→color table is NOT copied: it lives in pi-subagents' registry, so
|
|
10
|
+
* this module pulls it over the event bus (`subagents:rpc:list-agents`) once
|
|
11
|
+
* per session — on `subagents:ready` — and again on `subagents:agents-changed`
|
|
12
|
+
* (a mid-session ephemeral registration). Without an answer the cache stays
|
|
13
|
+
* empty and rows render exactly as before.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The slice of pi's event bus the list-agents client needs. */
|
|
17
|
+
export interface BadgeEvents {
|
|
18
|
+
on(event: string, handler: (data: unknown) => void): () => void;
|
|
19
|
+
emit(event: string, data: unknown): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Same theme shape pi-subagents' badge renderer takes. */
|
|
23
|
+
export interface BadgeTheme {
|
|
24
|
+
fg(color: string, text: string): string;
|
|
25
|
+
bold?(text: string): string;
|
|
26
|
+
getColorMode?(): "truecolor" | "256color";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ---- Color math (mirrored; see the header) ----
|
|
30
|
+
|
|
31
|
+
export const NAMED_AGENT_COLORS: Readonly<Record<string, string>> = {
|
|
32
|
+
red: "#DC2626",
|
|
33
|
+
blue: "#6A9BCC",
|
|
34
|
+
green: "#16A34A",
|
|
35
|
+
yellow: "#CA8A04",
|
|
36
|
+
purple: "#827DBD",
|
|
37
|
+
orange: "#D97757",
|
|
38
|
+
pink: "#C46686",
|
|
39
|
+
cyan: "#0891B2",
|
|
40
|
+
amber: "#F59E0B",
|
|
41
|
+
teal: "#008080",
|
|
42
|
+
indigo: "#6366F1",
|
|
43
|
+
gold: "#EAB308",
|
|
44
|
+
"neon-green": "#10B981",
|
|
45
|
+
"neon-cyan": "#06B6D4",
|
|
46
|
+
"metallic-blue": "#3B82F6",
|
|
47
|
+
violet: "#8B5CF6",
|
|
48
|
+
rose: "#F43F5E",
|
|
49
|
+
lime: "#84CC16",
|
|
50
|
+
gray: "#6B7280",
|
|
51
|
+
grey: "#6B7280",
|
|
52
|
+
fuchsia: "#D946EF",
|
|
53
|
+
slate: "#64748B",
|
|
54
|
+
navy: "#1E3A8A",
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const CUBE_VALUES = [0, 95, 135, 175, 215, 255];
|
|
58
|
+
const GRAY_VALUES = Array.from({ length: 24 }, (_, i) => 8 + i * 10);
|
|
59
|
+
const BLACK = { r: 0, g: 0, b: 0 };
|
|
60
|
+
const WHITE = { r: 255, g: 255, b: 255 };
|
|
61
|
+
|
|
62
|
+
type Rgb = { r: number; g: number; b: number };
|
|
63
|
+
|
|
64
|
+
export function resolveAgentColor(value: string | undefined): string | undefined {
|
|
65
|
+
if (!value) return undefined;
|
|
66
|
+
const normalized = value.trim().toLowerCase();
|
|
67
|
+
const resolved = NAMED_AGENT_COLORS[normalized] ?? normalized;
|
|
68
|
+
return /^#[0-9a-f]{6}$/i.test(resolved) ? resolved.toUpperCase() : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function parseHex(hex: string): Rgb {
|
|
72
|
+
return {
|
|
73
|
+
r: Number.parseInt(hex.slice(1, 3), 16),
|
|
74
|
+
g: Number.parseInt(hex.slice(3, 5), 16),
|
|
75
|
+
b: Number.parseInt(hex.slice(5, 7), 16),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function nearest(values: readonly number[], value: number): number {
|
|
80
|
+
return values.reduce(
|
|
81
|
+
(best, v, i) => (Math.abs(value - v) < Math.abs(value - values[best]) ? i : best),
|
|
82
|
+
0,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function rgbTo256({ r, g, b }: Rgb): { index: number; rgb: Rgb } {
|
|
87
|
+
const [rIndex, gIndex, bIndex] = [r, g, b].map((channel) => nearest(CUBE_VALUES, channel));
|
|
88
|
+
const distance = ({ r: cr, g: cg, b: cb }: Rgb) =>
|
|
89
|
+
0.299 * (r - cr) ** 2 + 0.587 * (g - cg) ** 2 + 0.114 * (b - cb) ** 2;
|
|
90
|
+
const grayIndex = nearest(GRAY_VALUES, Math.round(0.299 * r + 0.587 * g + 0.114 * b));
|
|
91
|
+
const gray = { r: GRAY_VALUES[grayIndex], g: GRAY_VALUES[grayIndex], b: GRAY_VALUES[grayIndex] };
|
|
92
|
+
const cube = { r: CUBE_VALUES[rIndex], g: CUBE_VALUES[gIndex], b: CUBE_VALUES[bIndex] };
|
|
93
|
+
if (Math.max(r, g, b) - Math.min(r, g, b) < 10 && distance(gray) < distance(cube)) {
|
|
94
|
+
return { index: 232 + grayIndex, rgb: gray };
|
|
95
|
+
}
|
|
96
|
+
return { index: 16 + 36 * rIndex + 6 * gIndex + bIndex, rgb: cube };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function relativeLuminance({ r, g, b }: Rgb): number {
|
|
100
|
+
const linear = (value: number) => {
|
|
101
|
+
const channel = value / 255;
|
|
102
|
+
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
|
|
103
|
+
};
|
|
104
|
+
return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Render one name as a padded background badge; plain text when no valid color. */
|
|
108
|
+
export function renderAgentBadge(
|
|
109
|
+
name: string,
|
|
110
|
+
color: string | undefined,
|
|
111
|
+
theme: BadgeTheme,
|
|
112
|
+
): string {
|
|
113
|
+
const resolved = resolveAgentColor(color);
|
|
114
|
+
if (!resolved) return name;
|
|
115
|
+
const rgb = parseHex(resolved);
|
|
116
|
+
const quantized =
|
|
117
|
+
(theme.getColorMode?.() ?? "truecolor") === "256color" ? rgbTo256(rgb) : undefined;
|
|
118
|
+
const shown = quantized?.rgb ?? rgb;
|
|
119
|
+
const contrasting = relativeLuminance(shown) > 0.179 ? BLACK : WHITE;
|
|
120
|
+
const label = theme.bold ? theme.bold(` ${name} `) : ` ${name} `;
|
|
121
|
+
// fg 与 bg 合并在同一个 SGR、以全量 reset 收尾:ccstyle 的行重绘器
|
|
122
|
+
// (stripBackgroundAnsi)只剥以背景码开头的转义,合并序列原样存活;它又在
|
|
123
|
+
// 每个 \u001b[0m 后补回行底色,所以徽章结尾用 0m 而不是 39m/49m —— 后者的
|
|
124
|
+
// 49m 被剥掉后徽章底色会一直铺到行尾。
|
|
125
|
+
const fgPart = quantized
|
|
126
|
+
? `38;5;${rgbTo256(contrasting).index}`
|
|
127
|
+
: `38;2;${contrasting.r};${contrasting.g};${contrasting.b}`;
|
|
128
|
+
const bgPart = quantized ? `48;5;${quantized.index}` : `48;2;${rgb.r};${rgb.g};${rgb.b}`;
|
|
129
|
+
return `\u001b[${fgPart};${bgPart}m${label}\u001b[0m`;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ---- Cache + bus client ----
|
|
133
|
+
|
|
134
|
+
type AgentBadgeInfo = { displayName?: string; color?: string };
|
|
135
|
+
|
|
136
|
+
/** type (lowercased) → the label and color pi-subagents reported. */
|
|
137
|
+
const badgeCache = new Map<string, AgentBadgeInfo>();
|
|
138
|
+
|
|
139
|
+
function fillCache(agents: Array<Record<string, unknown>>): void {
|
|
140
|
+
badgeCache.clear();
|
|
141
|
+
for (const agent of agents) {
|
|
142
|
+
if (typeof agent?.name !== "string") continue;
|
|
143
|
+
badgeCache.set(agent.name.toLowerCase(), {
|
|
144
|
+
displayName: typeof agent.displayName === "string" ? agent.displayName : undefined,
|
|
145
|
+
color: typeof agent.color === "string" ? agent.color : undefined,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Monotonic pull generation: only the LATEST pull may mutate the cache. A
|
|
151
|
+
* superseded pull is inert — its timeout must not wipe a newer pull's fill (the
|
|
152
|
+
* aggregate loads cc-extensions before pi-subagents, so the session-start pull
|
|
153
|
+
* races subagents:ready), and its late reply must not overwrite it. */
|
|
154
|
+
let fetchGeneration = 0;
|
|
155
|
+
|
|
156
|
+
async function fetchAgentBadges(events: BadgeEvents, timeoutMs = 2000): Promise<void> {
|
|
157
|
+
const generation = ++fetchGeneration;
|
|
158
|
+
const requestId = `ccstyle-badges-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
159
|
+
const channel = `subagents:rpc:list-agents:reply:${requestId}`;
|
|
160
|
+
return new Promise((resolve) => {
|
|
161
|
+
let timer: ReturnType<typeof setTimeout>;
|
|
162
|
+
const superseded = () => generation !== fetchGeneration;
|
|
163
|
+
// Silence or a bad reply from the LATEST pull clears the cache: a session
|
|
164
|
+
// where pi-subagents is filtered out must fall back to plain rows, not
|
|
165
|
+
// keep the previous session's colors for types that can no longer spawn.
|
|
166
|
+
const apply = (mutate: () => void) => {
|
|
167
|
+
if (superseded()) return resolve();
|
|
168
|
+
mutate();
|
|
169
|
+
resolve();
|
|
170
|
+
};
|
|
171
|
+
const clearAndResolve = () => apply(() => badgeCache.clear());
|
|
172
|
+
const unsub = events.on(channel, (raw) => {
|
|
173
|
+
clearTimeout(timer);
|
|
174
|
+
unsub();
|
|
175
|
+
const reply = raw as
|
|
176
|
+
| { success: true; data?: { agents?: Array<Record<string, unknown>> } }
|
|
177
|
+
| { success: false };
|
|
178
|
+
// A malformed envelope is silence, not a crash inside the bus dispatch.
|
|
179
|
+
if (!raw || typeof raw !== "object" || typeof reply.success !== "boolean") {
|
|
180
|
+
clearAndResolve();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (reply.success && Array.isArray(reply.data?.agents))
|
|
184
|
+
apply(() => fillCache(reply.data!.agents!));
|
|
185
|
+
else clearAndResolve();
|
|
186
|
+
});
|
|
187
|
+
timer = setTimeout(() => {
|
|
188
|
+
unsub();
|
|
189
|
+
clearAndResolve();
|
|
190
|
+
}, timeoutMs);
|
|
191
|
+
events.emit("subagents:rpc:list-agents", { requestId });
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
let unsubscribeBadges: (() => void) | undefined;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Subscribe the cache to pi-subagents' lifecycle: pull on ready, re-pull when
|
|
199
|
+
* the spawnable set changes. Idempotent — a second invocation replaces the
|
|
200
|
+
* first subscription, so repeated factory runs (integration tests) never
|
|
201
|
+
* stack listeners. The subscriptions live for the process; subagents:ready
|
|
202
|
+
* re-fires per session.
|
|
203
|
+
*/
|
|
204
|
+
export function initAgentBadges(events: BadgeEvents | undefined, timeoutMs = 2000): () => void {
|
|
205
|
+
// Headless hosts and test harnesses may expose no bus; without one there is
|
|
206
|
+
// nothing to pull and rows simply stay plain.
|
|
207
|
+
if (!events?.on || !events.emit) return () => {};
|
|
208
|
+
unsubscribeBadges?.();
|
|
209
|
+
const pull = () => {
|
|
210
|
+
void fetchAgentBadges(events, timeoutMs);
|
|
211
|
+
};
|
|
212
|
+
const unsubReady = events.on("subagents:ready", pull);
|
|
213
|
+
const unsubChanged = events.on("subagents:agents-changed", pull);
|
|
214
|
+
unsubscribeBadges = () => {
|
|
215
|
+
unsubReady();
|
|
216
|
+
unsubChanged();
|
|
217
|
+
};
|
|
218
|
+
return unsubscribeBadges;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Drop the cached table (tests and the session-start refresh use this). */
|
|
222
|
+
export function clearAgentBadges(): void {
|
|
223
|
+
badgeCache.clear();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Pull the table now. The renderer calls this on its own session_start: a
|
|
227
|
+
* present pi-subagents refills regardless of handler order (its own ready pull
|
|
228
|
+
* may have run before ours), and an absent one leaves the timeout's clear in
|
|
229
|
+
* place — so a filtered-out session starts plain instead of keeping the
|
|
230
|
+
* previous session's colors. */
|
|
231
|
+
export function refreshAgentBadges(events: BadgeEvents | undefined): void {
|
|
232
|
+
if (!events?.on || !events.emit) return;
|
|
233
|
+
void fetchAgentBadges(events);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** A styled badge for a spawnable type, or undefined when its color is unknown. */
|
|
237
|
+
export function agentBadge(type: string, theme: BadgeTheme): string | undefined {
|
|
238
|
+
const info = badgeCache.get(type.toLowerCase());
|
|
239
|
+
// A configured-but-invalid color is "no badge" (same rule as pi-subagents'
|
|
240
|
+
// hasAgentBadge), so the row keeps its normal toolTitle rendering instead of
|
|
241
|
+
// a bare unstyled name.
|
|
242
|
+
if (!info?.color || !resolveAgentColor(info.color)) return undefined;
|
|
243
|
+
return renderAgentBadge(info.displayName || type, info.color, theme);
|
|
244
|
+
}
|
|
@@ -503,7 +503,7 @@ export function createCcstyleTool(
|
|
|
503
503
|
cachedIcon = icon;
|
|
504
504
|
// 路径按最终可用宽度中间截断,避免整行二次截断隐藏文件名。
|
|
505
505
|
cachedLine = truncateToWidth(
|
|
506
|
-
`${lead}${icon} ${renderToolSummary(summary, mainWidth, theme.fg.bind(theme))}${extraStyled}`,
|
|
506
|
+
`${lead}${icon} ${renderToolSummary(summary, mainWidth, theme.fg.bind(theme), theme)}${extraStyled}`,
|
|
507
507
|
viewportWidth,
|
|
508
508
|
"",
|
|
509
509
|
);
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
} from "./codemode.ts";
|
|
24
24
|
import { getToolMouseTui } from "../mouse/scroll.ts";
|
|
25
25
|
import { mcpToolTitle } from "./mcp-title.ts";
|
|
26
|
+
import { type BadgeTheme } from "./agent-badge.ts";
|
|
26
27
|
import { collapseHintText, isToolTuiFullscreen, showMoreHintText } from "./names.ts";
|
|
27
28
|
import {
|
|
28
29
|
contentRowRange,
|
|
@@ -370,12 +371,18 @@ function shellResultTail(
|
|
|
370
371
|
}
|
|
371
372
|
|
|
372
373
|
/** 子工具折叠行正文:与组内同一套摘要。 */
|
|
373
|
-
function toolSummaryText(
|
|
374
|
+
function toolSummaryText(
|
|
375
|
+
tool: any,
|
|
376
|
+
toolStatus: ToolStatus,
|
|
377
|
+
width: number,
|
|
378
|
+
fg: Fg,
|
|
379
|
+
theme?: BadgeTheme,
|
|
380
|
+
): string {
|
|
374
381
|
const summary = askUserResultSummary(tool) ?? codemodeResultSummary(tool) ?? toolSummary(tool);
|
|
375
382
|
const detail = fg("dim", summary.detail);
|
|
376
383
|
const result = agentResultTail(tool, fg) + shellResultTail(tool, fg, toolStatus);
|
|
377
384
|
const mainWidth = Math.max(0, width - visibleWidth(detail) - visibleWidth(result));
|
|
378
|
-
return `${renderToolSummary(summary, mainWidth, fg)}${detail}${result}`;
|
|
385
|
+
return `${renderToolSummary(summary, mainWidth, fg, theme)}${detail}${result}`;
|
|
379
386
|
}
|
|
380
387
|
|
|
381
388
|
/**
|
|
@@ -437,7 +444,7 @@ function toolItem(tool: any): GroupDisplayItem {
|
|
|
437
444
|
name: toolName(tool),
|
|
438
445
|
label: toolTitle(tool),
|
|
439
446
|
status: toolStatus,
|
|
440
|
-
summary: (width, theme) => toolSummaryText(tool, toolStatus, width, themeFg(theme)),
|
|
447
|
+
summary: (width, theme) => toolSummaryText(tool, toolStatus, width, themeFg(theme), theme),
|
|
441
448
|
expandedRows: (width) => expandedToolRows(tool, width),
|
|
442
449
|
hitTarget: tool,
|
|
443
450
|
};
|