@diousk/pi-subagents-fast 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +808 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/README.md +1034 -0
- package/SECURITY.md +95 -0
- package/dist/abortable.d.ts +12 -0
- package/dist/abortable.js +42 -0
- package/dist/agent-color.d.ts +35 -0
- package/dist/agent-color.js +123 -0
- package/dist/agent-file-toggle.d.ts +125 -0
- package/dist/agent-file-toggle.js +260 -0
- package/dist/agent-manager.d.ts +472 -0
- package/dist/agent-manager.js +1338 -0
- package/dist/agent-runner.d.ts +312 -0
- package/dist/agent-runner.js +1034 -0
- package/dist/agent-types.d.ts +119 -0
- package/dist/agent-types.js +286 -0
- package/dist/child-context.d.ts +2 -0
- package/dist/child-context.js +12 -0
- package/dist/context.d.ts +12 -0
- package/dist/context.js +56 -0
- package/dist/cross-extension-rpc.d.ts +66 -0
- package/dist/cross-extension-rpc.js +138 -0
- package/dist/custom-agents.d.ts +54 -0
- package/dist/custom-agents.js +316 -0
- package/dist/default-agents.d.ts +7 -0
- package/dist/default-agents.js +122 -0
- package/dist/enabled-models.d.ts +49 -0
- package/dist/enabled-models.js +145 -0
- package/dist/env.d.ts +6 -0
- package/dist/env.js +28 -0
- package/dist/group-join.d.ts +32 -0
- package/dist/group-join.js +116 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +3682 -0
- package/dist/invocation-config.d.ts +107 -0
- package/dist/invocation-config.js +83 -0
- package/dist/memory.d.ts +53 -0
- package/dist/memory.js +165 -0
- package/dist/mention-clone.d.ts +87 -0
- package/dist/mention-clone.js +153 -0
- package/dist/mention.d.ts +81 -0
- package/dist/mention.js +131 -0
- package/dist/model-resolver.d.ts +36 -0
- package/dist/model-resolver.js +95 -0
- package/dist/model-scope.d.ts +49 -0
- package/dist/model-scope.js +48 -0
- package/dist/nested-tools.d.ts +55 -0
- package/dist/nested-tools.js +299 -0
- package/dist/output-file.d.ts +43 -0
- package/dist/output-file.js +142 -0
- package/dist/prompts.d.ts +55 -0
- package/dist/prompts.js +91 -0
- package/dist/schedule-store.d.ts +38 -0
- package/dist/schedule-store.js +155 -0
- package/dist/schedule.d.ts +109 -0
- package/dist/schedule.js +359 -0
- package/dist/settings.d.ts +360 -0
- package/dist/settings.js +251 -0
- package/dist/skill-loader.d.ts +24 -0
- package/dist/skill-loader.js +93 -0
- package/dist/status-note.d.ts +61 -0
- package/dist/status-note.js +85 -0
- package/dist/structured-output.d.ts +61 -0
- package/dist/structured-output.js +112 -0
- package/dist/types.d.ts +371 -0
- package/dist/types.js +5 -0
- package/dist/ui/agent-mention.d.ts +82 -0
- package/dist/ui/agent-mention.js +187 -0
- package/dist/ui/agent-widget.d.ts +219 -0
- package/dist/ui/agent-widget.js +592 -0
- package/dist/ui/conversation-viewer.d.ts +120 -0
- package/dist/ui/conversation-viewer.js +578 -0
- package/dist/ui/fleet-list.d.ts +195 -0
- package/dist/ui/fleet-list.js +471 -0
- package/dist/ui/schedule-menu.d.ts +16 -0
- package/dist/ui/schedule-menu.js +94 -0
- package/dist/ui/select-item.d.ts +27 -0
- package/dist/ui/select-item.js +34 -0
- package/dist/ui/viewer-keys.d.ts +20 -0
- package/dist/ui/viewer-keys.js +17 -0
- package/dist/ui/workflow-card.d.ts +175 -0
- package/dist/ui/workflow-card.js +332 -0
- package/dist/ui/workflow-dialog.d.ts +305 -0
- package/dist/ui/workflow-dialog.js +843 -0
- package/dist/ui/workflow-menu.d.ts +60 -0
- package/dist/ui/workflow-menu.js +147 -0
- package/dist/usage.d.ts +135 -0
- package/dist/usage.js +120 -0
- package/dist/workflow/collisions.d.ts +95 -0
- package/dist/workflow/collisions.js +88 -0
- package/dist/workflow/entry.d.ts +32 -0
- package/dist/workflow/entry.js +29 -0
- package/dist/workflow/host.d.ts +62 -0
- package/dist/workflow/host.js +362 -0
- package/dist/workflow/journal.d.ts +97 -0
- package/dist/workflow/journal.js +120 -0
- package/dist/workflow/json-schema.d.ts +51 -0
- package/dist/workflow/json-schema.js +111 -0
- package/dist/workflow/meta.d.ts +67 -0
- package/dist/workflow/meta.js +317 -0
- package/dist/workflow/progress.d.ts +224 -0
- package/dist/workflow/progress.js +361 -0
- package/dist/workflow/runtime.d.ts +334 -0
- package/dist/workflow/runtime.js +830 -0
- package/dist/workflow/saved.d.ts +90 -0
- package/dist/workflow/saved.js +203 -0
- package/dist/workflow/task.d.ts +136 -0
- package/dist/workflow/task.js +207 -0
- package/dist/workflow/tool-description.d.ts +38 -0
- package/dist/workflow/tool-description.js +199 -0
- package/dist/workflow/worker-source.d.ts +47 -0
- package/dist/workflow/worker-source.js +778 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +164 -0
- package/dist/xml.d.ts +10 -0
- package/dist/xml.js +12 -0
- package/docs/rpc.md +183 -0
- package/docs/workflows.md +437 -0
- package/examples/agent-tool-description.md +42 -0
- package/examples/workflows/compose.js +51 -0
- package/examples/workflows/fan-out-audit.js +47 -0
- package/examples/workflows/gated-fix.js +60 -0
- package/examples/workflows/lib/count-child.js +27 -0
- package/examples/workflows/review-panel.js +63 -0
- package/examples/workflows/structured-findings.js +78 -0
- package/package.json +68 -0
- package/src/abortable.ts +43 -0
- package/src/agent-color.ts +161 -0
- package/src/agent-file-toggle.ts +270 -0
- package/src/agent-manager.ts +1581 -0
- package/src/agent-runner.ts +1286 -0
- package/src/agent-types.ts +346 -0
- package/src/child-context.ts +15 -0
- package/src/context.ts +58 -0
- package/src/cross-extension-rpc.ts +198 -0
- package/src/custom-agents.ts +333 -0
- package/src/default-agents.ts +126 -0
- package/src/enabled-models.ts +180 -0
- package/src/env.ts +33 -0
- package/src/group-join.ts +141 -0
- package/src/index.ts +3991 -0
- package/src/invocation-config.ts +155 -0
- package/src/memory.ts +179 -0
- package/src/mention-clone.ts +196 -0
- package/src/mention.ts +141 -0
- package/src/model-resolver.ts +118 -0
- package/src/model-scope.ts +70 -0
- package/src/nested-tools.ts +422 -0
- package/src/output-file.ts +155 -0
- package/src/prompts.ts +142 -0
- package/src/schedule-store.ts +153 -0
- package/src/schedule.ts +386 -0
- package/src/settings.ts +587 -0
- package/src/skill-loader.ts +102 -0
- package/src/status-note.ts +90 -0
- package/src/structured-output.ts +130 -0
- package/src/types.ts +384 -0
- package/src/ui/agent-mention.ts +216 -0
- package/src/ui/agent-widget.ts +664 -0
- package/src/ui/conversation-viewer.ts +589 -0
- package/src/ui/fleet-list.ts +543 -0
- package/src/ui/schedule-menu.ts +105 -0
- package/src/ui/select-item.ts +45 -0
- package/src/ui/viewer-keys.ts +39 -0
- package/src/ui/workflow-card.ts +470 -0
- package/src/ui/workflow-dialog.ts +1115 -0
- package/src/ui/workflow-menu.ts +193 -0
- package/src/usage.ts +167 -0
- package/src/workflow/collisions.ts +123 -0
- package/src/workflow/entry.ts +47 -0
- package/src/workflow/host.ts +403 -0
- package/src/workflow/journal.ts +164 -0
- package/src/workflow/json-schema.ts +128 -0
- package/src/workflow/meta.ts +325 -0
- package/src/workflow/progress.ts +550 -0
- package/src/workflow/runtime.ts +1219 -0
- package/src/workflow/saved.ts +217 -0
- package/src/workflow/task.ts +302 -0
- package/src/workflow/tool-description.ts +200 -0
- package/src/workflow/worker-source.ts +781 -0
- package/src/worktree.ts +205 -0
- package/src/xml.ts +13 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/count-child.js — a nested workflow, invoked by compose.js.
|
|
3
|
+
*
|
|
4
|
+
* A child is an ordinary workflow: it needs its own `export const meta =`
|
|
5
|
+
* declaration, which is exactly what marks a file in a workflows directory as
|
|
6
|
+
* runnable rather than as some unrelated script that happens to live there.
|
|
7
|
+
*
|
|
8
|
+
* args: { root?: string }
|
|
9
|
+
*/
|
|
10
|
+
export const meta = {
|
|
11
|
+
name: 'count-child',
|
|
12
|
+
description: 'Count the source files under a directory',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const root = args?.root ?? 'src/'
|
|
16
|
+
|
|
17
|
+
const found = await agent(`List every source file under ${root}. One path per line, nothing else.`, {
|
|
18
|
+
label: 'scan',
|
|
19
|
+
schema: {
|
|
20
|
+
type: 'object',
|
|
21
|
+
properties: { files: { type: 'array', items: { type: 'string' } } },
|
|
22
|
+
required: ['files'],
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
// A schema call can still return null if the child never complied.
|
|
27
|
+
return found === null ? 0 : found.files.length
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* review-panel.js — the case where a barrier is actually earned.
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates: `parallel` used correctly, `effort` tiering (cheap reviewers,
|
|
5
|
+
* an expensive judge), and a `model` override.
|
|
6
|
+
*
|
|
7
|
+
* Most of the time `pipeline` beats `parallel`, because a barrier idles every
|
|
8
|
+
* fast agent until the slowest finishes. This is the exception: the synthesis
|
|
9
|
+
* prompt interpolates ALL of the reviews, so it genuinely cannot start until
|
|
10
|
+
* every one of them is in. That — a prompt that compares results against each
|
|
11
|
+
* other — is what justifies a barrier.
|
|
12
|
+
*
|
|
13
|
+
* args: { target?: string, lenses?: string[] }
|
|
14
|
+
*
|
|
15
|
+
* Run: ask the model — "run the workflow at examples/workflows/review-panel.js
|
|
16
|
+
* against src/auth.ts".
|
|
17
|
+
*/
|
|
18
|
+
export const meta = {
|
|
19
|
+
name: 'review-panel',
|
|
20
|
+
description: 'Review one thing from several angles, then reconcile the verdicts',
|
|
21
|
+
phases: [{ title: 'Review' }, { title: 'Synthesize' }],
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const target = args?.target ?? 'the changed files'
|
|
25
|
+
const lenses = args?.lenses ?? ['correctness', 'security', 'performance']
|
|
26
|
+
|
|
27
|
+
phase('Review')
|
|
28
|
+
|
|
29
|
+
// Perspective diversity, not redundancy: three reviewers with DIFFERENT briefs
|
|
30
|
+
// catch failure modes that three identical ones cannot.
|
|
31
|
+
const reviews = await parallel(
|
|
32
|
+
lenses.map(lens => () =>
|
|
33
|
+
agent(`Review ${target} through the lens of ${lens} alone. Be specific and brief.`, {
|
|
34
|
+
label: `review:${lens}`,
|
|
35
|
+
// Cheap for the survey work; the judge below gets the depth.
|
|
36
|
+
effort: 'low',
|
|
37
|
+
}),
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
// A thunk that throws becomes null without taking its siblings down.
|
|
42
|
+
const usable = reviews
|
|
43
|
+
.map((text, i) => ({ lens: lenses[i], text }))
|
|
44
|
+
.filter(r => r.text !== null)
|
|
45
|
+
|
|
46
|
+
if (usable.length === 0) {
|
|
47
|
+
log('every reviewer failed — nothing to synthesize')
|
|
48
|
+
return { reviewed: 0, verdict: null }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
phase('Synthesize')
|
|
52
|
+
|
|
53
|
+
// This is the barrier's payoff: one prompt that sees all of them at once and can
|
|
54
|
+
// weigh them against each other.
|
|
55
|
+
const verdict = await agent(
|
|
56
|
+
[
|
|
57
|
+
`Reconcile these reviews of ${target}. Where they disagree, say which is right and why.`,
|
|
58
|
+
...usable.map(r => `\n## ${r.lens}\n${r.text}`),
|
|
59
|
+
].join('\n'),
|
|
60
|
+
{ label: 'synthesize', effort: 'high', agentType: 'Plan' },
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
return { reviewed: usable.length, verdict }
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* structured-findings.js — get objects back, not prose.
|
|
3
|
+
*
|
|
4
|
+
* Demonstrates: `schema` on every agent call, so the script manipulates
|
|
5
|
+
* validated objects instead of parsing text it hopes is well-formed.
|
|
6
|
+
*
|
|
7
|
+
* Reach for this whenever the script has to *do* something with the results —
|
|
8
|
+
* sort, count, filter, compare — rather than hand them straight to you.
|
|
9
|
+
*
|
|
10
|
+
* args: { dimensions?: string[] } — review angles, default bugs + perf
|
|
11
|
+
*
|
|
12
|
+
* Run: ask the model — "run the workflow at
|
|
13
|
+
* examples/workflows/structured-findings.js".
|
|
14
|
+
*/
|
|
15
|
+
export const meta = {
|
|
16
|
+
name: 'structured-findings',
|
|
17
|
+
description: 'Review changed files across dimensions and verify each finding',
|
|
18
|
+
phases: [{ title: 'Review' }, { title: 'Verify' }],
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const FINDINGS = {
|
|
22
|
+
type: 'object',
|
|
23
|
+
properties: {
|
|
24
|
+
findings: {
|
|
25
|
+
type: 'array',
|
|
26
|
+
items: {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
title: { type: 'string' },
|
|
30
|
+
file: { type: 'string' },
|
|
31
|
+
severity: { type: 'string' },
|
|
32
|
+
},
|
|
33
|
+
required: ['title', 'file'],
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
required: ['findings'],
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const VERDICT = {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: { isReal: { type: 'boolean' }, why: { type: 'string' } },
|
|
43
|
+
required: ['isReal'],
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const dimensions = args?.dimensions ?? ['bugs', 'performance']
|
|
47
|
+
|
|
48
|
+
const reviewed = await pipeline(
|
|
49
|
+
dimensions,
|
|
50
|
+
dim => agent(`Review the changed files for ${dim}. Report every finding.`, {
|
|
51
|
+
label: `review:${dim}`,
|
|
52
|
+
phase: 'Review',
|
|
53
|
+
// With a schema the call resolves to the validated object, so `.findings`
|
|
54
|
+
// below is a real array rather than something scraped out of prose.
|
|
55
|
+
schema: FINDINGS,
|
|
56
|
+
}),
|
|
57
|
+
// A barrier is earned here only per-dimension: each dimension's findings are
|
|
58
|
+
// verified concurrently, but dimensions never wait for each other.
|
|
59
|
+
review => parallel(
|
|
60
|
+
review.findings.map(f => () =>
|
|
61
|
+
agent(`Try to REFUTE this finding: ${f.title} (${f.file})`, {
|
|
62
|
+
label: `verify:${f.file}`,
|
|
63
|
+
phase: 'Verify',
|
|
64
|
+
schema: VERDICT,
|
|
65
|
+
}).then(verdict => ({ ...f, verdict })),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
// filter(Boolean) twice: once for a whole dimension that failed, once for an
|
|
71
|
+
// individual verification that did. A schema call can still return null.
|
|
72
|
+
const confirmed = reviewed
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.flat()
|
|
75
|
+
.filter(Boolean)
|
|
76
|
+
.filter(f => f.verdict?.isReal)
|
|
77
|
+
|
|
78
|
+
return { confirmed: confirmed.length, findings: confirmed }
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@diousk/pi-subagents-fast",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "A pi extension that brings Claude Code-like sub-agents and workflow orchestration to pi — parallel execution, live widget, fleet view, custom agent types, mid-run steering, dynamic workflows, Claude Code compatibility, look and feel.",
|
|
5
|
+
"author": "tintinweb",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/diousk/pi-subagents.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/diousk/pi-subagents#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/diousk/pi-subagents/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"pi-package",
|
|
20
|
+
"pi",
|
|
21
|
+
"pi-extension",
|
|
22
|
+
"subagent",
|
|
23
|
+
"agent",
|
|
24
|
+
"autonomous"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"@earendil-works/pi-ai": ">=0.84.0",
|
|
28
|
+
"@earendil-works/pi-coding-agent": ">=0.84.0",
|
|
29
|
+
"@earendil-works/pi-tui": ">=0.84.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@sinclair/typebox": "^0.34.49",
|
|
33
|
+
"croner": "^10.0.1",
|
|
34
|
+
"nanoid": "^5.1.16",
|
|
35
|
+
"typebox": "^1.3.7"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc",
|
|
39
|
+
"prepublishOnly": "npm run lint && npm run typecheck && npm run test && npm run build",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:watch": "vitest",
|
|
42
|
+
"test:e2e": "vitest run e2e --reporter=verbose",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"lint": "biome check src/ test/",
|
|
45
|
+
"lint:fix": "biome check --fix src/ test/",
|
|
46
|
+
"check": "npm run lint && npm run typecheck && npm run test",
|
|
47
|
+
"test:coverage": "vitest run --coverage",
|
|
48
|
+
"bench": "vitest bench",
|
|
49
|
+
"bench:ab": "node test/perf/ab.mjs"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@biomejs/biome": "^2.4.14",
|
|
53
|
+
"@earendil-works/pi-ai": "0.84.2",
|
|
54
|
+
"@earendil-works/pi-coding-agent": "0.84.2",
|
|
55
|
+
"@earendil-works/pi-tui": "0.84.2",
|
|
56
|
+
"@types/node": "^25.5.0",
|
|
57
|
+
"@vitest/coverage-istanbul": "^4.1.10",
|
|
58
|
+
"typescript": "^6.0.0",
|
|
59
|
+
"vitest": "^4.0.18"
|
|
60
|
+
},
|
|
61
|
+
"pi": {
|
|
62
|
+
"extensions": [
|
|
63
|
+
"./src/index.ts"
|
|
64
|
+
],
|
|
65
|
+
"video": "https://github.com/diousk/pi-subagents/raw/master/media/demo.mp4",
|
|
66
|
+
"image": "https://github.com/diousk/pi-subagents/raw/master/media/screenshot.png"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/abortable.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* abortable.ts — race a promise against an AbortSignal without cancelling the
|
|
3
|
+
* underlying work.
|
|
4
|
+
*
|
|
5
|
+
* Used by the `get_subagent_result` wait paths (top-level and nested): pressing
|
|
6
|
+
* Esc cancels only the caller's wait; the background child keeps running and its
|
|
7
|
+
* result stays unconsumed. The listener is removed on every settle path so the
|
|
8
|
+
* signal accumulates no handlers, and a late settlement of the wrapped promise
|
|
9
|
+
* after an abort is absorbed as a no-op (no unhandled rejection).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Await a promise until it settles or the caller cancels, without aborting the underlying work. */
|
|
13
|
+
export function abortable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
|
|
14
|
+
if (!signal) return promise;
|
|
15
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
16
|
+
|
|
17
|
+
return new Promise<T>((resolve, reject) => {
|
|
18
|
+
let settled = false;
|
|
19
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
20
|
+
const onAbort = () => {
|
|
21
|
+
if (settled) return;
|
|
22
|
+
settled = true;
|
|
23
|
+
cleanup();
|
|
24
|
+
reject(signal.reason);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
28
|
+
promise.then(
|
|
29
|
+
(value) => {
|
|
30
|
+
if (settled) return;
|
|
31
|
+
settled = true;
|
|
32
|
+
cleanup();
|
|
33
|
+
resolve(value);
|
|
34
|
+
},
|
|
35
|
+
(error: unknown) => {
|
|
36
|
+
if (settled) return;
|
|
37
|
+
settled = true;
|
|
38
|
+
cleanup();
|
|
39
|
+
reject(error);
|
|
40
|
+
},
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* agent-color.ts — Claude Code-compatible agent name badges.
|
|
3
|
+
*
|
|
4
|
+
* Claude Code renders a subagent's name as a badge: the configured color is the
|
|
5
|
+
* background, the text an inverse foreground. Its eight named colors are
|
|
6
|
+
* reproduced here, along with six-digit hex and the extra palette names Agency
|
|
7
|
+
* Agents uses, so those definitions render as written.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { getConfig } from "./agent-types.js";
|
|
11
|
+
|
|
12
|
+
const NAMED_AGENT_COLORS: Readonly<Record<string, string>> = {
|
|
13
|
+
// Claude Code's eight subagent colors, as its default theme renders them.
|
|
14
|
+
red: "#DC2626",
|
|
15
|
+
blue: "#6A9BCC",
|
|
16
|
+
green: "#16A34A",
|
|
17
|
+
yellow: "#CA8A04",
|
|
18
|
+
purple: "#827DBD",
|
|
19
|
+
orange: "#D97757",
|
|
20
|
+
pink: "#C46686",
|
|
21
|
+
cyan: "#0891B2",
|
|
22
|
+
// Agency Agents palette aliases.
|
|
23
|
+
amber: "#F59E0B",
|
|
24
|
+
teal: "#008080",
|
|
25
|
+
indigo: "#6366F1",
|
|
26
|
+
gold: "#EAB308",
|
|
27
|
+
"neon-green": "#10B981",
|
|
28
|
+
"neon-cyan": "#06B6D4",
|
|
29
|
+
"metallic-blue": "#3B82F6",
|
|
30
|
+
violet: "#8B5CF6",
|
|
31
|
+
rose: "#F43F5E",
|
|
32
|
+
lime: "#84CC16",
|
|
33
|
+
gray: "#6B7280",
|
|
34
|
+
grey: "#6B7280",
|
|
35
|
+
fuchsia: "#D946EF",
|
|
36
|
+
slate: "#64748B",
|
|
37
|
+
navy: "#1E3A8A",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const CUBE_VALUES = [0, 95, 135, 175, 215, 255];
|
|
41
|
+
const GRAY_VALUES = Array.from({ length: 24 }, (_, i) => 8 + i * 10);
|
|
42
|
+
const BLACK = { r: 0, g: 0, b: 0 };
|
|
43
|
+
const WHITE = { r: 255, g: 255, b: 255 };
|
|
44
|
+
|
|
45
|
+
type Rgb = { r: number; g: number; b: number };
|
|
46
|
+
type ColorMode = "truecolor" | "256color";
|
|
47
|
+
|
|
48
|
+
export interface AgentNameTheme {
|
|
49
|
+
fg(color: string, text: string): string;
|
|
50
|
+
bold(text: string): string;
|
|
51
|
+
getColorMode?(): ColorMode;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface AgentNameStyle {
|
|
55
|
+
/** Existing theme foreground used when no valid agent color is configured. */
|
|
56
|
+
fallbackColor?: string;
|
|
57
|
+
/** Reapply an enclosing background after the badge instead of resetting it. */
|
|
58
|
+
restoreBackground?: string;
|
|
59
|
+
bold?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Resolve Claude Code/Agency Agents color syntax to normalized #RRGGBB. */
|
|
63
|
+
export function resolveAgentColor(value: string | undefined): string | undefined {
|
|
64
|
+
if (!value) return undefined;
|
|
65
|
+
const normalized = value.trim().toLowerCase();
|
|
66
|
+
const resolved = NAMED_AGENT_COLORS[normalized] ?? normalized;
|
|
67
|
+
return /^#[0-9a-f]{6}$/i.test(resolved) ? resolved.toUpperCase() : undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parseHex(hex: string): Rgb {
|
|
71
|
+
return {
|
|
72
|
+
r: Number.parseInt(hex.slice(1, 3), 16),
|
|
73
|
+
g: Number.parseInt(hex.slice(3, 5), 16),
|
|
74
|
+
b: Number.parseInt(hex.slice(5, 7), 16),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Index of the entry in `values` closest to `value`. */
|
|
79
|
+
function nearest(values: readonly number[], value: number): number {
|
|
80
|
+
return values.reduce((best, v, i) => (Math.abs(value - v) < Math.abs(value - values[best]) ? i : best), 0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Quantize to the xterm-256 palette the way pi's own theme does, returning both
|
|
85
|
+
* the index to emit and the color the terminal will actually show — badge
|
|
86
|
+
* contrast is judged against the latter.
|
|
87
|
+
*/
|
|
88
|
+
function rgbTo256({ r, g, b }: Rgb): { index: number; rgb: Rgb } {
|
|
89
|
+
const [rIndex, gIndex, bIndex] = [r, g, b].map((channel) => nearest(CUBE_VALUES, channel));
|
|
90
|
+
const distance = ({ r: cr, g: cg, b: cb }: Rgb) => 0.299 * (r - cr) ** 2 + 0.587 * (g - cg) ** 2 + 0.114 * (b - cb) ** 2;
|
|
91
|
+
const grayIndex = nearest(GRAY_VALUES, Math.round(0.299 * r + 0.587 * g + 0.114 * b));
|
|
92
|
+
const gray = { r: GRAY_VALUES[grayIndex], g: GRAY_VALUES[grayIndex], b: GRAY_VALUES[grayIndex] };
|
|
93
|
+
const cube = { r: CUBE_VALUES[rIndex], g: CUBE_VALUES[gIndex], b: CUBE_VALUES[bIndex] };
|
|
94
|
+
// Only near-neutral colors may take the gray ramp; anything else keeps its tint.
|
|
95
|
+
if (Math.max(r, g, b) - Math.min(r, g, b) < 10 && distance(gray) < distance(cube)) {
|
|
96
|
+
return { index: 232 + grayIndex, rgb: gray };
|
|
97
|
+
}
|
|
98
|
+
return { index: 16 + 36 * rIndex + 6 * gIndex + bIndex, rgb: cube };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function ansiColor(layer: "foreground" | "background", color: Rgb | number): string {
|
|
102
|
+
const code = layer === "foreground" ? 38 : 48;
|
|
103
|
+
return typeof color === "number"
|
|
104
|
+
? `\u001b[${code};5;${color}m`
|
|
105
|
+
: `\u001b[${code};2;${color.r};${color.g};${color.b}m`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function relativeLuminance({ r, g, b }: Rgb): number {
|
|
109
|
+
const linear = (value: number) => {
|
|
110
|
+
const channel = value / 255;
|
|
111
|
+
return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
|
|
112
|
+
};
|
|
113
|
+
return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Render one name as a padded background badge when `color` is valid. Claude
|
|
118
|
+
* Code uses one inverse color for every badge's text; black or white is picked
|
|
119
|
+
* by WCAG contrast here instead, so each palette entry stays readable. Invalid
|
|
120
|
+
* or omitted colors preserve the caller's existing theme styling.
|
|
121
|
+
*/
|
|
122
|
+
export function renderAgentNameLabel(
|
|
123
|
+
name: string,
|
|
124
|
+
color: string | undefined,
|
|
125
|
+
theme: AgentNameTheme,
|
|
126
|
+
style: AgentNameStyle = {},
|
|
127
|
+
): string {
|
|
128
|
+
const resolved = resolveAgentColor(color);
|
|
129
|
+
if (!resolved) {
|
|
130
|
+
const text = style.bold ? theme.bold(name) : name;
|
|
131
|
+
return style.fallbackColor ? theme.fg(style.fallbackColor, text) : text;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const rgb = parseHex(resolved);
|
|
135
|
+
const quantized = (theme.getColorMode?.() ?? "truecolor") === "256color" ? rgbTo256(rgb) : undefined;
|
|
136
|
+
const shown = quantized?.rgb ?? rgb;
|
|
137
|
+
const contrasting = relativeLuminance(shown) > 0.179 ? BLACK : WHITE;
|
|
138
|
+
const label = style.bold ? theme.bold(` ${name} `) : ` ${name} `;
|
|
139
|
+
|
|
140
|
+
return ansiColor("background", quantized?.index ?? rgb)
|
|
141
|
+
+ ansiColor("foreground", quantized ? rgbTo256(contrasting).index : contrasting)
|
|
142
|
+
+ label
|
|
143
|
+
+ "\u001b[39m"
|
|
144
|
+
+ (style.restoreBackground ?? "\u001b[49m");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Whether an agent renders as a badge — i.e. it has a valid configured color. */
|
|
148
|
+
export function hasAgentBadge(type: string | undefined): boolean {
|
|
149
|
+
return type !== undefined && resolveAgentColor(getConfig(type).color) !== undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Render a registered agent's display name with its configured color. */
|
|
153
|
+
export function renderAgentName(
|
|
154
|
+
type: string | undefined,
|
|
155
|
+
theme: AgentNameTheme,
|
|
156
|
+
style: AgentNameStyle = {},
|
|
157
|
+
): string {
|
|
158
|
+
if (!type) return renderAgentNameLabel("Agent", undefined, theme, style);
|
|
159
|
+
const config = getConfig(type);
|
|
160
|
+
return renderAgentNameLabel(config.displayName, config.color, theme, style);
|
|
161
|
+
}
|