@herbertgao/pi-extensions 2026.11.16 → 2026.11.18
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 +26 -0
- package/node_modules/@herbertgao/pi-agency/CHANGELOG.md +6 -0
- package/node_modules/@herbertgao/pi-agency/README.md +3 -1
- package/node_modules/@herbertgao/pi-agency/package.json +1 -1
- package/node_modules/@herbertgao/pi-agency/src/index.ts +151 -9
- package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +12 -0
- 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 +25 -2
- 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 +12 -0
- package/node_modules/@herbertgao/pi-subagents/docs/rpc.md +13 -1
- package/node_modules/@herbertgao/pi-subagents/package.json +1 -1
- package/node_modules/@herbertgao/pi-subagents/src/agent-color.ts +6 -2
- package/node_modules/@herbertgao/pi-subagents/src/agent-types.ts +87 -1
- package/node_modules/@herbertgao/pi-subagents/src/cross-extension-rpc.ts +142 -2
- package/node_modules/@herbertgao/pi-subagents/src/index.ts +33 -3
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @herbertgao/pi-extensions
|
|
2
2
|
|
|
3
|
+
## 2026.11.18
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#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.
|
|
8
|
+
|
|
9
|
+
- [#401](https://github.com/HerbertGao/pi-extensions/pull/401) [`09bed8a`](https://github.com/HerbertGao/pi-extensions/commit/09bed8a789c603958995746d073982b92a1d92fe) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Pin graphifyy 0.9.113. The CLI stays outside the npm aggregate. Warm AST caches rebuild. Python is now >=3.11.5,<3.15. The Pi skill path is unchanged.
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`f0109df`](https://github.com/HerbertGao/pi-extensions/commit/f0109dfd7bb213fe26260a0c882e45d2bf83935d), [`f7e030f`](https://github.com/HerbertGao/pi-extensions/commit/f7e030f578bb73f401d74fa4bac2b67947bc997a)]:
|
|
12
|
+
- @herbertgao/pi-cc-extensions@1.0.21
|
|
13
|
+
- @herbertgao/pi-subagents@0.26.1
|
|
14
|
+
- @herbertgao/pi-inline-skill-commands@0.2.0
|
|
15
|
+
|
|
16
|
+
## 2026.11.17
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- [#398](https://github.com/HerbertGao/pi-extensions/pull/398) [`1eccc1a`](https://github.com/HerbertGao/pi-extensions/commit/1eccc1ab99247b9b1f1772ea027f3b8c48cf23ba) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Raise the promoted dependency floors to what the bundled packages require: `nanoid@^5.1.16`, `turndown@^7.2.4`, and `undici@^8.12.3`. The installed versions already meet these floors, so this changes no resolved package today; it removes ranges that could resolve below a bundled package's own requirement, including the patched `undici` floor `pi-antigravity` needs.
|
|
21
|
+
|
|
22
|
+
- [#397](https://github.com/HerbertGao/pi-extensions/pull/397) [`42647d1`](https://github.com/HerbertGao/pi-extensions/commit/42647d16b171f766da6135fc937bd087e005a161) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Label an `Agent` run by identity: a registered type (Code Reviewer) keeps its type name while a named generic run shows its own `name`, in both the ccstyle transcript row and the pi-subagents badges. The `name` parameter accepts any characters — labels show it as given, `@handles` are slugged. `agency_register` seats catalog personas as session-scoped agent types over the new `subagents:rpc:register-agents` bus channels: the persona body becomes the system prompt, the card name and color become the label and badge, and the types vanish at the next session start without ever shadowing file-loaded agents.
|
|
23
|
+
|
|
24
|
+
- Updated dependencies [[`42647d1`](https://github.com/HerbertGao/pi-extensions/commit/42647d16b171f766da6135fc937bd087e005a161)]:
|
|
25
|
+
- @herbertgao/pi-agency@0.1.4
|
|
26
|
+
- @herbertgao/pi-cc-extensions@1.0.21
|
|
27
|
+
- @herbertgao/pi-subagents@0.26.1
|
|
28
|
+
|
|
3
29
|
## 2026.11.16
|
|
4
30
|
|
|
5
31
|
### Patch Changes
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @herbertgao/pi-agency
|
|
2
2
|
|
|
3
|
+
## 0.1.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#397](https://github.com/HerbertGao/pi-extensions/pull/397) [`42647d1`](https://github.com/HerbertGao/pi-extensions/commit/42647d16b171f766da6135fc937bd087e005a161) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Label an `Agent` run by identity: a registered type (Code Reviewer) keeps its type name while a named generic run shows its own `name`, in both the ccstyle transcript row and the pi-subagents badges. The `name` parameter accepts any characters — labels show it as given, `@handles` are slugged. `agency_register` seats catalog personas as session-scoped agent types over the new `subagents:rpc:register-agents` bus channels: the persona body becomes the system prompt, the card name and color become the label and badge, and the types vanish at the next session start without ever shadowing file-loaded agents.
|
|
8
|
+
|
|
3
9
|
## 0.1.3
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -24,7 +24,9 @@ The catalog location is fixed at `~/.agency-agents`. `agency_dispatch` also need
|
|
|
24
24
|
- `max_turns` is omitted unless you pass it, so a run decides when it is done; set it only to bound a runaway.
|
|
25
25
|
- `record_dir` must be absolute and used by one dispatch at a time. It receives `<id>.prompt.txt` and `<id>.return.txt` with mode `0600`. The call fails before any dispatch if one of those files already exists.
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
`agency_register({ personas, tools? })` registers catalog personas as session-scoped pi-subagents agent types over its RPC bus and returns the type names to pass as `subagent_type`. The persona body becomes the type's system prompt, the card name becomes the label, and the card color becomes the badge — a seat shows as `MCP Builder` with its color instead of `general-purpose`. `tools: "none"` gives the type no tools at all; the default `"builtin"` keeps pi's built-in tools. Types vanish at the next session start, never shadow file-loaded agents, and need `@herbertgao/pi-subagents` on the event bus — an older or upstream pi-subagents answers nothing and the call fails, so the seat falls back to carrying the persona file's absolute path.
|
|
28
|
+
|
|
29
|
+
All three tools return `structuredContent`, so codemode scripts receive objects instead of text.
|
|
28
30
|
|
|
29
31
|
```js
|
|
30
32
|
const { results } = await tools.agency_search({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createHash } from "node:crypto"
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto"
|
|
2
2
|
import {
|
|
3
3
|
existsSync,
|
|
4
4
|
mkdirSync,
|
|
@@ -129,8 +129,11 @@ export function searchCatalog(
|
|
|
129
129
|
.slice(0, limit)
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
/** A catalog persona
|
|
133
|
-
export function
|
|
132
|
+
/** A catalog persona, frontmatter and body, path-guarded like every loader here. */
|
|
133
|
+
export function loadPersonaEntry(
|
|
134
|
+
root: string,
|
|
135
|
+
persona: string,
|
|
136
|
+
): { name: string; description: string; color?: string; body: string } {
|
|
134
137
|
const base = realRoot(root)
|
|
135
138
|
const file = realpathSync(isAbsolute(persona) ? persona : join(base, persona))
|
|
136
139
|
if (!file.startsWith(base + sep) || !file.endsWith(".md")) {
|
|
@@ -143,7 +146,21 @@ export function loadPersona(root: string, persona: string): string {
|
|
|
143
146
|
if (/<\/?persona>/i.test(body)) {
|
|
144
147
|
throw new Error(`${persona} contains a reserved <persona> delimiter`)
|
|
145
148
|
}
|
|
146
|
-
return
|
|
149
|
+
return {
|
|
150
|
+
name: frontmatter.name,
|
|
151
|
+
description:
|
|
152
|
+
typeof frontmatter.description === "string"
|
|
153
|
+
? frontmatter.description
|
|
154
|
+
: `${persona} has no persona description`,
|
|
155
|
+
color:
|
|
156
|
+
typeof frontmatter.color === "string" ? frontmatter.color : undefined,
|
|
157
|
+
body,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** A catalog persona's body without frontmatter. */
|
|
162
|
+
export function loadPersona(root: string, persona: string): string {
|
|
163
|
+
return loadPersonaEntry(root, persona).body
|
|
147
164
|
}
|
|
148
165
|
|
|
149
166
|
/** Fills `{{persona}}` and `{{shared}}` in one pass, so inserted text is never rescanned or `$`-expanded. */
|
|
@@ -200,7 +217,53 @@ export function dropPlaceholders(args: unknown): any {
|
|
|
200
217
|
)
|
|
201
218
|
}
|
|
202
219
|
|
|
203
|
-
|
|
220
|
+
/** The slice of pi's event bus the RPC client needs — same shape pi-subagents handles. */
|
|
221
|
+
export interface AgencyEvents {
|
|
222
|
+
on(event: string, handler: (data: unknown) => void): () => void
|
|
223
|
+
emit(event: string, data: unknown): void
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export type AgencyToolsOptions = {
|
|
227
|
+
/** pi.events; agency_register needs it to reach pi-subagents' RPC bus. */
|
|
228
|
+
events?: AgencyEvents
|
|
229
|
+
/** Reply timeout for the register RPC; tests shrink it. */
|
|
230
|
+
rpcTimeoutMs?: number
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Call pi-subagents' register-agents RPC and await its reply envelope. */
|
|
234
|
+
export async function rpcRegisterAgents(
|
|
235
|
+
events: AgencyEvents,
|
|
236
|
+
agents: Array<Record<string, string>>,
|
|
237
|
+
tools: "none" | "builtin",
|
|
238
|
+
timeoutMs: number,
|
|
239
|
+
): Promise<void> {
|
|
240
|
+
const requestId = randomUUID()
|
|
241
|
+
const channel = `subagents:rpc:register-agents:reply:${requestId}`
|
|
242
|
+
return new Promise((resolve, reject) => {
|
|
243
|
+
let timer: ReturnType<typeof setTimeout>
|
|
244
|
+
const unsub = events.on(channel, (raw) => {
|
|
245
|
+
clearTimeout(timer)
|
|
246
|
+
unsub()
|
|
247
|
+
const reply = raw as { success: true } | { success: false; error: string }
|
|
248
|
+
if (reply.success) resolve()
|
|
249
|
+
else reject(new Error(reply.error))
|
|
250
|
+
})
|
|
251
|
+
timer = setTimeout(() => {
|
|
252
|
+
unsub()
|
|
253
|
+
reject(
|
|
254
|
+
new Error(
|
|
255
|
+
`pi-subagents did not answer register-agents within ${timeoutMs}ms; it may be missing or too old for ephemeral agent types`,
|
|
256
|
+
),
|
|
257
|
+
)
|
|
258
|
+
}, timeoutMs)
|
|
259
|
+
events.emit("subagents:rpc:register-agents", { requestId, agents, tools })
|
|
260
|
+
})
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function agencyTools(
|
|
264
|
+
root = CATALOG_ROOT,
|
|
265
|
+
options: AgencyToolsOptions = {},
|
|
266
|
+
) {
|
|
204
267
|
const search = defineTool({
|
|
205
268
|
name: "agency_search",
|
|
206
269
|
label: "Agency Search",
|
|
@@ -397,7 +460,7 @@ export function agencyTools(root = CATALOG_ROOT) {
|
|
|
397
460
|
for (const run of runs) record(`${run.id}.prompt.txt`, run.prompt)
|
|
398
461
|
}
|
|
399
462
|
|
|
400
|
-
const
|
|
463
|
+
const spawnOptions = Object.fromEntries(
|
|
401
464
|
Object.entries({
|
|
402
465
|
model: params.model,
|
|
403
466
|
thinking: params.thinking,
|
|
@@ -414,7 +477,7 @@ export function agencyTools(root = CATALOG_ROOT) {
|
|
|
414
477
|
prompt: run.prompt,
|
|
415
478
|
run_in_background: false,
|
|
416
479
|
isolated: params.isolated ?? true,
|
|
417
|
-
...
|
|
480
|
+
...spawnOptions,
|
|
418
481
|
}),
|
|
419
482
|
),
|
|
420
483
|
)
|
|
@@ -475,11 +538,90 @@ export function agencyTools(root = CATALOG_ROOT) {
|
|
|
475
538
|
},
|
|
476
539
|
})
|
|
477
540
|
|
|
478
|
-
|
|
541
|
+
const register = defineTool({
|
|
542
|
+
name: "agency_register",
|
|
543
|
+
label: "Agency Register",
|
|
544
|
+
description:
|
|
545
|
+
"Register catalog personas as session-scoped pi-subagents agent types and return the type names to pass as `subagent_type` to Agent. The persona's body becomes the type's system prompt, its card name becomes the agent's label, and its card color becomes the badge — so a seat shows as its persona instead of general-purpose. `tools: \"none\"` gives the type no tools at all (advisory seats); the default `\"builtin\"` keeps pi's built-in tools. Types vanish at the next session start and never shadow agents loaded from files. Needs pi-subagents on the session's event bus; if it is missing or too old the call fails and a seat can still be given the persona file's absolute path instead.",
|
|
546
|
+
promptSnippet: "Seat catalog personas as labeled, colored agent types",
|
|
547
|
+
prepareArguments: dropPlaceholders,
|
|
548
|
+
promptGuidelines: [
|
|
549
|
+
"For Agency Agents personas, use agency_search to pick the seat, agency_register to turn it into a session-scoped agent type, and one Agent call per seat with that type; without registration the call carries the persona's absolute path instead. Reach for agency_dispatch only for bulk work that does not need a run per row.",
|
|
550
|
+
],
|
|
551
|
+
parameters: Type.Object({
|
|
552
|
+
personas: Type.Array(Type.String(), {
|
|
553
|
+
minItems: 1,
|
|
554
|
+
description:
|
|
555
|
+
"Persona files relative to the catalog, e.g. security/security-appsec-engineer.md, or absolute paths inside it.",
|
|
556
|
+
}),
|
|
557
|
+
tools: Type.Optional(
|
|
558
|
+
Type.Union([Type.Literal("none"), Type.Literal("builtin")], {
|
|
559
|
+
description:
|
|
560
|
+
'The type\'s tool surface: "none" for tool-less advisory seats, "builtin" (default) for pi\'s built-in tools.',
|
|
561
|
+
}),
|
|
562
|
+
),
|
|
563
|
+
}),
|
|
564
|
+
outputSchema: Type.Object({
|
|
565
|
+
results: Type.Array(
|
|
566
|
+
Type.Object({
|
|
567
|
+
persona: Type.String(),
|
|
568
|
+
type: Type.String(),
|
|
569
|
+
color: NullableString,
|
|
570
|
+
tools: Type.Union([Type.Literal("none"), Type.Literal("builtin")]),
|
|
571
|
+
}),
|
|
572
|
+
),
|
|
573
|
+
}),
|
|
574
|
+
async execute(_toolCallId, params) {
|
|
575
|
+
const events = options.events
|
|
576
|
+
if (!events) {
|
|
577
|
+
throw new Error("agency_register needs the pi.events bus")
|
|
578
|
+
}
|
|
579
|
+
const tools = params.tools ?? "builtin"
|
|
580
|
+
const entries = params.personas.map((persona) => {
|
|
581
|
+
const card = loadPersonaEntry(root, persona)
|
|
582
|
+
return { persona, card }
|
|
583
|
+
})
|
|
584
|
+
await rpcRegisterAgents(
|
|
585
|
+
events,
|
|
586
|
+
entries.map(({ card }) => ({
|
|
587
|
+
name: card.name,
|
|
588
|
+
description: card.description,
|
|
589
|
+
systemPrompt: card.body,
|
|
590
|
+
...(card.color ? { color: card.color } : {}),
|
|
591
|
+
})),
|
|
592
|
+
tools,
|
|
593
|
+
options.rpcTimeoutMs ?? 2000,
|
|
594
|
+
)
|
|
595
|
+
const results = entries.map(({ persona, card }) => ({
|
|
596
|
+
persona,
|
|
597
|
+
type: card.name,
|
|
598
|
+
color: card.color ?? null,
|
|
599
|
+
tools,
|
|
600
|
+
}))
|
|
601
|
+
const text =
|
|
602
|
+
results
|
|
603
|
+
.map(
|
|
604
|
+
(result) =>
|
|
605
|
+
`- ${result.persona} → subagent_type: ${result.type}${result.color ? ` (${result.color})` : ""} [${result.tools}]`,
|
|
606
|
+
)
|
|
607
|
+
.join("\n") +
|
|
608
|
+
"\n\nTypes vanish at the next session start; after a session switch or resume, register again before spawning."
|
|
609
|
+
return {
|
|
610
|
+
content: [{ type: "text", text }],
|
|
611
|
+
details: {},
|
|
612
|
+
structuredContent: { results },
|
|
613
|
+
}
|
|
614
|
+
},
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
return { search, dispatch, register }
|
|
479
618
|
}
|
|
480
619
|
|
|
481
620
|
export default function piAgency(pi: ExtensionAPI): void {
|
|
482
|
-
const { search, dispatch } = agencyTools(
|
|
621
|
+
const { search, dispatch, register } = agencyTools(CATALOG_ROOT, {
|
|
622
|
+
events: pi.events,
|
|
623
|
+
})
|
|
483
624
|
pi.registerTool(search)
|
|
484
625
|
pi.registerTool(dispatch)
|
|
626
|
+
pi.registerTool(register)
|
|
485
627
|
}
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @herbertgao/pi-cc-extensions
|
|
2
2
|
|
|
3
|
+
## 1.0.21-local.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#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.
|
|
8
|
+
|
|
9
|
+
## 1.0.21-local.2
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#397](https://github.com/HerbertGao/pi-extensions/pull/397) [`42647d1`](https://github.com/HerbertGao/pi-extensions/commit/42647d16b171f766da6135fc937bd087e005a161) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Label an `Agent` run by identity: a registered type (Code Reviewer) keeps its type name while a named generic run shows its own `name`, in both the ccstyle transcript row and the pi-subagents badges. The `name` parameter accepts any characters — labels show it as given, `@handles` are slugged. `agency_register` seats catalog personas as session-scoped agent types over the new `subagents:rpc:register-agents` bus channels: the persona body becomes the system prompt, the card name and color become the label and badge, and the types vanish at the next session start without ever shadowing file-loaded agents.
|
|
14
|
+
|
|
3
15
|
## 1.0.21-local.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -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
|
};
|