@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.
Files changed (183) hide show
  1. package/CHANGELOG.md +808 -0
  2. package/CONTRIBUTING.md +72 -0
  3. package/LICENSE +21 -0
  4. package/README.md +1034 -0
  5. package/SECURITY.md +95 -0
  6. package/dist/abortable.d.ts +12 -0
  7. package/dist/abortable.js +42 -0
  8. package/dist/agent-color.d.ts +35 -0
  9. package/dist/agent-color.js +123 -0
  10. package/dist/agent-file-toggle.d.ts +125 -0
  11. package/dist/agent-file-toggle.js +260 -0
  12. package/dist/agent-manager.d.ts +472 -0
  13. package/dist/agent-manager.js +1338 -0
  14. package/dist/agent-runner.d.ts +312 -0
  15. package/dist/agent-runner.js +1034 -0
  16. package/dist/agent-types.d.ts +119 -0
  17. package/dist/agent-types.js +286 -0
  18. package/dist/child-context.d.ts +2 -0
  19. package/dist/child-context.js +12 -0
  20. package/dist/context.d.ts +12 -0
  21. package/dist/context.js +56 -0
  22. package/dist/cross-extension-rpc.d.ts +66 -0
  23. package/dist/cross-extension-rpc.js +138 -0
  24. package/dist/custom-agents.d.ts +54 -0
  25. package/dist/custom-agents.js +316 -0
  26. package/dist/default-agents.d.ts +7 -0
  27. package/dist/default-agents.js +122 -0
  28. package/dist/enabled-models.d.ts +49 -0
  29. package/dist/enabled-models.js +145 -0
  30. package/dist/env.d.ts +6 -0
  31. package/dist/env.js +28 -0
  32. package/dist/group-join.d.ts +32 -0
  33. package/dist/group-join.js +116 -0
  34. package/dist/index.d.ts +50 -0
  35. package/dist/index.js +3682 -0
  36. package/dist/invocation-config.d.ts +107 -0
  37. package/dist/invocation-config.js +83 -0
  38. package/dist/memory.d.ts +53 -0
  39. package/dist/memory.js +165 -0
  40. package/dist/mention-clone.d.ts +87 -0
  41. package/dist/mention-clone.js +153 -0
  42. package/dist/mention.d.ts +81 -0
  43. package/dist/mention.js +131 -0
  44. package/dist/model-resolver.d.ts +36 -0
  45. package/dist/model-resolver.js +95 -0
  46. package/dist/model-scope.d.ts +49 -0
  47. package/dist/model-scope.js +48 -0
  48. package/dist/nested-tools.d.ts +55 -0
  49. package/dist/nested-tools.js +299 -0
  50. package/dist/output-file.d.ts +43 -0
  51. package/dist/output-file.js +142 -0
  52. package/dist/prompts.d.ts +55 -0
  53. package/dist/prompts.js +91 -0
  54. package/dist/schedule-store.d.ts +38 -0
  55. package/dist/schedule-store.js +155 -0
  56. package/dist/schedule.d.ts +109 -0
  57. package/dist/schedule.js +359 -0
  58. package/dist/settings.d.ts +360 -0
  59. package/dist/settings.js +251 -0
  60. package/dist/skill-loader.d.ts +24 -0
  61. package/dist/skill-loader.js +93 -0
  62. package/dist/status-note.d.ts +61 -0
  63. package/dist/status-note.js +85 -0
  64. package/dist/structured-output.d.ts +61 -0
  65. package/dist/structured-output.js +112 -0
  66. package/dist/types.d.ts +371 -0
  67. package/dist/types.js +5 -0
  68. package/dist/ui/agent-mention.d.ts +82 -0
  69. package/dist/ui/agent-mention.js +187 -0
  70. package/dist/ui/agent-widget.d.ts +219 -0
  71. package/dist/ui/agent-widget.js +592 -0
  72. package/dist/ui/conversation-viewer.d.ts +120 -0
  73. package/dist/ui/conversation-viewer.js +578 -0
  74. package/dist/ui/fleet-list.d.ts +195 -0
  75. package/dist/ui/fleet-list.js +471 -0
  76. package/dist/ui/schedule-menu.d.ts +16 -0
  77. package/dist/ui/schedule-menu.js +94 -0
  78. package/dist/ui/select-item.d.ts +27 -0
  79. package/dist/ui/select-item.js +34 -0
  80. package/dist/ui/viewer-keys.d.ts +20 -0
  81. package/dist/ui/viewer-keys.js +17 -0
  82. package/dist/ui/workflow-card.d.ts +175 -0
  83. package/dist/ui/workflow-card.js +332 -0
  84. package/dist/ui/workflow-dialog.d.ts +305 -0
  85. package/dist/ui/workflow-dialog.js +843 -0
  86. package/dist/ui/workflow-menu.d.ts +60 -0
  87. package/dist/ui/workflow-menu.js +147 -0
  88. package/dist/usage.d.ts +135 -0
  89. package/dist/usage.js +120 -0
  90. package/dist/workflow/collisions.d.ts +95 -0
  91. package/dist/workflow/collisions.js +88 -0
  92. package/dist/workflow/entry.d.ts +32 -0
  93. package/dist/workflow/entry.js +29 -0
  94. package/dist/workflow/host.d.ts +62 -0
  95. package/dist/workflow/host.js +362 -0
  96. package/dist/workflow/journal.d.ts +97 -0
  97. package/dist/workflow/journal.js +120 -0
  98. package/dist/workflow/json-schema.d.ts +51 -0
  99. package/dist/workflow/json-schema.js +111 -0
  100. package/dist/workflow/meta.d.ts +67 -0
  101. package/dist/workflow/meta.js +317 -0
  102. package/dist/workflow/progress.d.ts +224 -0
  103. package/dist/workflow/progress.js +361 -0
  104. package/dist/workflow/runtime.d.ts +334 -0
  105. package/dist/workflow/runtime.js +830 -0
  106. package/dist/workflow/saved.d.ts +90 -0
  107. package/dist/workflow/saved.js +203 -0
  108. package/dist/workflow/task.d.ts +136 -0
  109. package/dist/workflow/task.js +207 -0
  110. package/dist/workflow/tool-description.d.ts +38 -0
  111. package/dist/workflow/tool-description.js +199 -0
  112. package/dist/workflow/worker-source.d.ts +47 -0
  113. package/dist/workflow/worker-source.js +778 -0
  114. package/dist/worktree.d.ts +52 -0
  115. package/dist/worktree.js +164 -0
  116. package/dist/xml.d.ts +10 -0
  117. package/dist/xml.js +12 -0
  118. package/docs/rpc.md +183 -0
  119. package/docs/workflows.md +437 -0
  120. package/examples/agent-tool-description.md +42 -0
  121. package/examples/workflows/compose.js +51 -0
  122. package/examples/workflows/fan-out-audit.js +47 -0
  123. package/examples/workflows/gated-fix.js +60 -0
  124. package/examples/workflows/lib/count-child.js +27 -0
  125. package/examples/workflows/review-panel.js +63 -0
  126. package/examples/workflows/structured-findings.js +78 -0
  127. package/package.json +68 -0
  128. package/src/abortable.ts +43 -0
  129. package/src/agent-color.ts +161 -0
  130. package/src/agent-file-toggle.ts +270 -0
  131. package/src/agent-manager.ts +1581 -0
  132. package/src/agent-runner.ts +1286 -0
  133. package/src/agent-types.ts +346 -0
  134. package/src/child-context.ts +15 -0
  135. package/src/context.ts +58 -0
  136. package/src/cross-extension-rpc.ts +198 -0
  137. package/src/custom-agents.ts +333 -0
  138. package/src/default-agents.ts +126 -0
  139. package/src/enabled-models.ts +180 -0
  140. package/src/env.ts +33 -0
  141. package/src/group-join.ts +141 -0
  142. package/src/index.ts +3991 -0
  143. package/src/invocation-config.ts +155 -0
  144. package/src/memory.ts +179 -0
  145. package/src/mention-clone.ts +196 -0
  146. package/src/mention.ts +141 -0
  147. package/src/model-resolver.ts +118 -0
  148. package/src/model-scope.ts +70 -0
  149. package/src/nested-tools.ts +422 -0
  150. package/src/output-file.ts +155 -0
  151. package/src/prompts.ts +142 -0
  152. package/src/schedule-store.ts +153 -0
  153. package/src/schedule.ts +386 -0
  154. package/src/settings.ts +587 -0
  155. package/src/skill-loader.ts +102 -0
  156. package/src/status-note.ts +90 -0
  157. package/src/structured-output.ts +130 -0
  158. package/src/types.ts +384 -0
  159. package/src/ui/agent-mention.ts +216 -0
  160. package/src/ui/agent-widget.ts +664 -0
  161. package/src/ui/conversation-viewer.ts +589 -0
  162. package/src/ui/fleet-list.ts +543 -0
  163. package/src/ui/schedule-menu.ts +105 -0
  164. package/src/ui/select-item.ts +45 -0
  165. package/src/ui/viewer-keys.ts +39 -0
  166. package/src/ui/workflow-card.ts +470 -0
  167. package/src/ui/workflow-dialog.ts +1115 -0
  168. package/src/ui/workflow-menu.ts +193 -0
  169. package/src/usage.ts +167 -0
  170. package/src/workflow/collisions.ts +123 -0
  171. package/src/workflow/entry.ts +47 -0
  172. package/src/workflow/host.ts +403 -0
  173. package/src/workflow/journal.ts +164 -0
  174. package/src/workflow/json-schema.ts +128 -0
  175. package/src/workflow/meta.ts +325 -0
  176. package/src/workflow/progress.ts +550 -0
  177. package/src/workflow/runtime.ts +1219 -0
  178. package/src/workflow/saved.ts +217 -0
  179. package/src/workflow/task.ts +302 -0
  180. package/src/workflow/tool-description.ts +200 -0
  181. package/src/workflow/worker-source.ts +781 -0
  182. package/src/worktree.ts +205 -0
  183. package/src/xml.ts +13 -0
package/SECURITY.md ADDED
@@ -0,0 +1,95 @@
1
+ # Security Policy
2
+
3
+ This document explains the security model behind `@tintinweb/pi-subagents` and
4
+ where the boundaries are.
5
+
6
+ `pi-subagents` is a [pi](https://pi.dev) extension. It spawns and orchestrates
7
+ autonomous sub-agents that run locally within the same security boundary as the
8
+ user running pi, and inherit pi's trust model. It is the responsibility of the
9
+ user to monitor those agents' operations or to contain them within a container,
10
+ virtual machine, or other sandbox solution.
11
+
12
+ Sub-agents run with the local user account's privileges and can use the tools
13
+ they are granted (reading and writing files, running commands, network access,
14
+ etc.). They treat the local user account and files writable by that account as
15
+ inside the same trust boundary as the pi process itself. If an attacker can
16
+ modify files under the user's home directory, workspace, shell startup files,
17
+ environment, pi configuration, or this extension's configuration, they can
18
+ generally influence pi, its sub-agents, or other local developer tools. Reports
19
+ that depend on such prior local write access are not security vulnerabilities
20
+ unless they demonstrate how `pi-subagents` grants that write access or crosses an
21
+ operating-system privilege boundary.
22
+
23
+ `pi-subagents` relies on the user only loading trustworthy agent definitions
24
+ (`.pi/agents/*.md`, `.agents/agents/*.md`, and global agents), skills, and
25
+ tools, and only using pi within trusted repositories. Files like `AGENTS.md`,
26
+ custom agent frontmatter/system prompts, preloaded skills, or instructions
27
+ embedded in repository content and comments can be used to prompt-inject the
28
+ coding agent and its sub-agents trivially, and this cannot be protected against.
29
+
30
+ ## Reporting a Vulnerability
31
+
32
+ If you believe you found a security vulnerability in `pi-subagents`, please
33
+ report it privately by opening a draft advisory through
34
+ [GitHub Security Advisories](https://github.com/tintinweb/pi-subagents/security/advisories/new)
35
+ for this repository.
36
+
37
+ Please include:
38
+
39
+ - A description of the issue and its impact
40
+ - Steps to reproduce, proof of concept, or relevant logs
41
+ - Affected version, commit, or configuration
42
+ - Any known mitigations
43
+
44
+ Do not open a public issue for security-sensitive reports. Reports will be
45
+ reviewed and disclosure coordinated as appropriate.
46
+
47
+ ## Scope
48
+
49
+ Security issues in the published npm package and the code in this repository are
50
+ in scope — for example, a flaw in `pi-subagents` that crosses an
51
+ operating-system privilege boundary, or that causes the extension to bypass a
52
+ tool restriction, denylist, or agent boundary it claims to enforce.
53
+
54
+ ## Out Of Scope
55
+
56
+ - Local code execution or sandboxing behavior (sub-agents intentionally do not
57
+ have a sandbox and run with the user's privileges)
58
+ - Behavior of pi itself, or of other pi extensions, skills, or tools installed by
59
+ the user (report those to their respective projects)
60
+ - Risks from working in untrusted repositories
61
+ - Risks from installing or loading untrusted agent definitions, skills,
62
+ extensions, packages, or tools
63
+ - Issues caused by non-trustworthy MITM proxies
64
+ - Public internet exposure of a pi installation
65
+ - Prompt injection attacks (including via `AGENTS.md`, agent frontmatter, custom
66
+ system prompts, preloaded skills, repository content, or context inheritance)
67
+ - Exposed secrets that are third-party/user-controlled credentials
68
+ - Reports requiring the ability to create, modify, delete, or replace files,
69
+ directories, symlinks, environment variables, shell configuration, or other
70
+ user-controlled local state on the target machine. This includes `.pi/agents/`,
71
+ `.agents/agents/`, agent and extension configuration, persistent agent memory,
72
+ workspace files, `AGENTS.md`, skills, dotfiles, and files synchronized through NFS, roaming
73
+ profiles, or dotfile managers, unless the report shows how `pi-subagents`
74
+ itself grants that access.
75
+ - Issues caused by intentionally weakened user configuration
76
+ - Resource/DOS claims that require trusted local input/config
77
+ - Reports about malicious model output
78
+ - User-approved or user-initiated local actions presented as vulnerabilities
79
+
80
+ ## Notes for Reporters
81
+
82
+ The most useful reports show a current, reproducible security boundary bypass
83
+ with demonstrated impact. Reports that only show expected local-agent behavior,
84
+ prompt injection, or a malicious trusted agent definition/skill are not security
85
+ vulnerabilities under this model.
86
+
87
+ For example, a report showing that malicious contents written to a trusted agent
88
+ definition or extension configuration cause a sub-agent to execute commands, load
89
+ attacker-controlled tools, or send credentials to an attacker-controlled endpoint
90
+ is out of scope.
91
+
92
+ When possible, include the exact affected path, package version or commit SHA,
93
+ configuration, and a proof of concept against the latest release or latest
94
+ `master`. For dependency reports, include evidence that the shipped dependency is
95
+ affected and that the issue is reachable through `pi-subagents`.
@@ -0,0 +1,12 @@
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
+ /** Await a promise until it settles or the caller cancels, without aborting the underlying work. */
12
+ export declare function abortable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T>;
@@ -0,0 +1,42 @@
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
+ /** Await a promise until it settles or the caller cancels, without aborting the underlying work. */
12
+ export function abortable(promise, signal) {
13
+ if (!signal)
14
+ return promise;
15
+ if (signal.aborted)
16
+ return Promise.reject(signal.reason);
17
+ return new Promise((resolve, reject) => {
18
+ let settled = false;
19
+ const cleanup = () => signal.removeEventListener("abort", onAbort);
20
+ const onAbort = () => {
21
+ if (settled)
22
+ return;
23
+ settled = true;
24
+ cleanup();
25
+ reject(signal.reason);
26
+ };
27
+ signal.addEventListener("abort", onAbort, { once: true });
28
+ promise.then((value) => {
29
+ if (settled)
30
+ return;
31
+ settled = true;
32
+ cleanup();
33
+ resolve(value);
34
+ }, (error) => {
35
+ if (settled)
36
+ return;
37
+ settled = true;
38
+ cleanup();
39
+ reject(error);
40
+ });
41
+ });
42
+ }
@@ -0,0 +1,35 @@
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
+ type ColorMode = "truecolor" | "256color";
10
+ export interface AgentNameTheme {
11
+ fg(color: string, text: string): string;
12
+ bold(text: string): string;
13
+ getColorMode?(): ColorMode;
14
+ }
15
+ export interface AgentNameStyle {
16
+ /** Existing theme foreground used when no valid agent color is configured. */
17
+ fallbackColor?: string;
18
+ /** Reapply an enclosing background after the badge instead of resetting it. */
19
+ restoreBackground?: string;
20
+ bold?: boolean;
21
+ }
22
+ /** Resolve Claude Code/Agency Agents color syntax to normalized #RRGGBB. */
23
+ export declare function resolveAgentColor(value: string | undefined): string | undefined;
24
+ /**
25
+ * Render one name as a padded background badge when `color` is valid. Claude
26
+ * Code uses one inverse color for every badge's text; black or white is picked
27
+ * by WCAG contrast here instead, so each palette entry stays readable. Invalid
28
+ * or omitted colors preserve the caller's existing theme styling.
29
+ */
30
+ export declare function renderAgentNameLabel(name: string, color: string | undefined, theme: AgentNameTheme, style?: AgentNameStyle): string;
31
+ /** Whether an agent renders as a badge — i.e. it has a valid configured color. */
32
+ export declare function hasAgentBadge(type: string | undefined): boolean;
33
+ /** Render a registered agent's display name with its configured color. */
34
+ export declare function renderAgentName(type: string | undefined, theme: AgentNameTheme, style?: AgentNameStyle): string;
35
+ export {};
@@ -0,0 +1,123 @@
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
+ import { getConfig } from "./agent-types.js";
10
+ const NAMED_AGENT_COLORS = {
11
+ // Claude Code's eight subagent colors, as its default theme renders them.
12
+ red: "#DC2626",
13
+ blue: "#6A9BCC",
14
+ green: "#16A34A",
15
+ yellow: "#CA8A04",
16
+ purple: "#827DBD",
17
+ orange: "#D97757",
18
+ pink: "#C46686",
19
+ cyan: "#0891B2",
20
+ // Agency Agents palette aliases.
21
+ amber: "#F59E0B",
22
+ teal: "#008080",
23
+ indigo: "#6366F1",
24
+ gold: "#EAB308",
25
+ "neon-green": "#10B981",
26
+ "neon-cyan": "#06B6D4",
27
+ "metallic-blue": "#3B82F6",
28
+ violet: "#8B5CF6",
29
+ rose: "#F43F5E",
30
+ lime: "#84CC16",
31
+ gray: "#6B7280",
32
+ grey: "#6B7280",
33
+ fuchsia: "#D946EF",
34
+ slate: "#64748B",
35
+ navy: "#1E3A8A",
36
+ };
37
+ const CUBE_VALUES = [0, 95, 135, 175, 215, 255];
38
+ const GRAY_VALUES = Array.from({ length: 24 }, (_, i) => 8 + i * 10);
39
+ const BLACK = { r: 0, g: 0, b: 0 };
40
+ const WHITE = { r: 255, g: 255, b: 255 };
41
+ /** Resolve Claude Code/Agency Agents color syntax to normalized #RRGGBB. */
42
+ export function resolveAgentColor(value) {
43
+ if (!value)
44
+ return undefined;
45
+ const normalized = value.trim().toLowerCase();
46
+ const resolved = NAMED_AGENT_COLORS[normalized] ?? normalized;
47
+ return /^#[0-9a-f]{6}$/i.test(resolved) ? resolved.toUpperCase() : undefined;
48
+ }
49
+ function parseHex(hex) {
50
+ return {
51
+ r: Number.parseInt(hex.slice(1, 3), 16),
52
+ g: Number.parseInt(hex.slice(3, 5), 16),
53
+ b: Number.parseInt(hex.slice(5, 7), 16),
54
+ };
55
+ }
56
+ /** Index of the entry in `values` closest to `value`. */
57
+ function nearest(values, value) {
58
+ return values.reduce((best, v, i) => (Math.abs(value - v) < Math.abs(value - values[best]) ? i : best), 0);
59
+ }
60
+ /**
61
+ * Quantize to the xterm-256 palette the way pi's own theme does, returning both
62
+ * the index to emit and the color the terminal will actually show — badge
63
+ * contrast is judged against the latter.
64
+ */
65
+ function rgbTo256({ r, g, b }) {
66
+ const [rIndex, gIndex, bIndex] = [r, g, b].map((channel) => nearest(CUBE_VALUES, channel));
67
+ const distance = ({ r: cr, g: cg, b: cb }) => 0.299 * (r - cr) ** 2 + 0.587 * (g - cg) ** 2 + 0.114 * (b - cb) ** 2;
68
+ const grayIndex = nearest(GRAY_VALUES, Math.round(0.299 * r + 0.587 * g + 0.114 * b));
69
+ const gray = { r: GRAY_VALUES[grayIndex], g: GRAY_VALUES[grayIndex], b: GRAY_VALUES[grayIndex] };
70
+ const cube = { r: CUBE_VALUES[rIndex], g: CUBE_VALUES[gIndex], b: CUBE_VALUES[bIndex] };
71
+ // Only near-neutral colors may take the gray ramp; anything else keeps its tint.
72
+ if (Math.max(r, g, b) - Math.min(r, g, b) < 10 && distance(gray) < distance(cube)) {
73
+ return { index: 232 + grayIndex, rgb: gray };
74
+ }
75
+ return { index: 16 + 36 * rIndex + 6 * gIndex + bIndex, rgb: cube };
76
+ }
77
+ function ansiColor(layer, color) {
78
+ const code = layer === "foreground" ? 38 : 48;
79
+ return typeof color === "number"
80
+ ? `\u001b[${code};5;${color}m`
81
+ : `\u001b[${code};2;${color.r};${color.g};${color.b}m`;
82
+ }
83
+ function relativeLuminance({ r, g, b }) {
84
+ const linear = (value) => {
85
+ const channel = value / 255;
86
+ return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
87
+ };
88
+ return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
89
+ }
90
+ /**
91
+ * Render one name as a padded background badge when `color` is valid. Claude
92
+ * Code uses one inverse color for every badge's text; black or white is picked
93
+ * by WCAG contrast here instead, so each palette entry stays readable. Invalid
94
+ * or omitted colors preserve the caller's existing theme styling.
95
+ */
96
+ export function renderAgentNameLabel(name, color, theme, style = {}) {
97
+ const resolved = resolveAgentColor(color);
98
+ if (!resolved) {
99
+ const text = style.bold ? theme.bold(name) : name;
100
+ return style.fallbackColor ? theme.fg(style.fallbackColor, text) : text;
101
+ }
102
+ const rgb = parseHex(resolved);
103
+ const quantized = (theme.getColorMode?.() ?? "truecolor") === "256color" ? rgbTo256(rgb) : undefined;
104
+ const shown = quantized?.rgb ?? rgb;
105
+ const contrasting = relativeLuminance(shown) > 0.179 ? BLACK : WHITE;
106
+ const label = style.bold ? theme.bold(` ${name} `) : ` ${name} `;
107
+ return ansiColor("background", quantized?.index ?? rgb)
108
+ + ansiColor("foreground", quantized ? rgbTo256(contrasting).index : contrasting)
109
+ + label
110
+ + "\u001b[39m"
111
+ + (style.restoreBackground ?? "\u001b[49m");
112
+ }
113
+ /** Whether an agent renders as a badge — i.e. it has a valid configured color. */
114
+ export function hasAgentBadge(type) {
115
+ return type !== undefined && resolveAgentColor(getConfig(type).color) !== undefined;
116
+ }
117
+ /** Render a registered agent's display name with its configured color. */
118
+ export function renderAgentName(type, theme, style = {}) {
119
+ if (!type)
120
+ return renderAgentNameLabel("Agent", undefined, theme, style);
121
+ const config = getConfig(type);
122
+ return renderAgentNameLabel(config.displayName, config.color, theme, style);
123
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * agent-file-toggle.ts — Pure helpers for the `/agents` file-editing operations:
3
+ * locating an agent's .md file, toggling its `enabled:` frontmatter flag, and
4
+ * serializing an AgentConfig back to frontmatter for eject.
5
+ *
6
+ * These live outside src/index.ts so they can be tested directly: the `/agents`
7
+ * command handler is an ~890-line closure reached only through `registerCommand`,
8
+ * which every test mocks.
9
+ *
10
+ * The read side of this data (src/custom-agents.ts) parses frontmatter with a
11
+ * real YAML parser, so it honors `enabled: false` at any position in the block.
12
+ * This module must agree with it, and splits the work accordingly:
13
+ *
14
+ * - Deciding whether a file is disabled is a *read*, so it calls that same parser
15
+ * (`isDisabledContent`) instead of mirroring it. A mirror has to be right about
16
+ * YAML's boolean spellings and about pi's fence scan, and a regex was wrong
17
+ * about both.
18
+ * - *Editing* cannot go through the parser, because re-serializing a parsed
19
+ * document would reformat a file the README tells users to hand-author —
20
+ * discarding their comments, key order, and quoting. So the edits are line-wise
21
+ * and preserve everything they don't touch.
22
+ *
23
+ * That leaves removal best-effort: it recognizes a lowercase bare `false`, and
24
+ * reports `changed: false` for the spellings it cannot rewrite, so the caller
25
+ * refuses honestly rather than announcing a change it did not make.
26
+ */
27
+ import type { AgentConfig } from "./types.js";
28
+ export type AgentFileLocation = "project" | "workspace" | "personal";
29
+ export declare const projectAgentsDir: (cwd?: string) => string;
30
+ export declare const workspaceAgentsDir: (cwd?: string) => string;
31
+ export declare const personalAgentsDir: () => string;
32
+ /**
33
+ * Find the file path of a custom agent by name, in discovery-precedence order
34
+ * (project, workspace, then global). Mirrors the load-side precedence in
35
+ * src/custom-agents.ts — if the two drift, `/agents` edits a file the loader
36
+ * isn't reading.
37
+ */
38
+ export declare function findAgentFile(name: string, cwd?: string): {
39
+ path: string;
40
+ location: AgentFileLocation;
41
+ } | undefined;
42
+ /**
43
+ * Find the file behind a *loaded* agent, preferring the path the loader
44
+ * actually read (`AgentConfig.sourcePath`) over the `<type>.md` guess.
45
+ *
46
+ * An agent's type comes from its frontmatter `name:` now, so the two can
47
+ * disagree: `reviewer.md` declaring `name: code-reviewer` is loaded as
48
+ * `code-reviewer`, and probing for `code-reviewer.md` finds nothing. That is
49
+ * not a harmless miss — `/agents → Disable` would then take the no-file branch
50
+ * and write a NEW `code-reviewer.md` stub, which loses to `reviewer.md` on
51
+ * load, leaving the agent enabled while reporting success.
52
+ *
53
+ * The probe stays as the fallback: a built-in that was never ejected has no
54
+ * `sourcePath`, and a path can go stale between a load and this call.
55
+ */
56
+ export declare function locateAgentFile(name: string, sourcePath: string | undefined, cwd?: string): {
57
+ path: string;
58
+ location: AgentFileLocation;
59
+ } | undefined;
60
+ export type DisableOutcome = "disabled" | "already-disabled" | "no-frontmatter";
61
+ /**
62
+ * Does the loader consider this file disabled?
63
+ *
64
+ * Detection is a READ operation, so it asks the same parser the loader uses
65
+ * rather than mirroring it with a regex — that mirror has to be right about
66
+ * YAML's boolean spellings (`False`, `FALSE`, a trailing `# comment`, a quoted
67
+ * key) *and* about pi's fence scan, which closes the block on any line starting
68
+ * `---` and so ends it early on `----`. A throw means the file is already
69
+ * unparseable, which is what the loader sees too: it skips the agent, so there
70
+ * is no "disabled" state to report.
71
+ */
72
+ export declare function isDisabledContent(content: string): boolean;
73
+ /**
74
+ * Add `enabled: false` to a file's frontmatter.
75
+ *
76
+ * `outcome` distinguishes a real edit from a no-op so the caller can report
77
+ * honestly instead of unconditionally claiming success.
78
+ */
79
+ export declare function disableInContent(content: string): {
80
+ content: string;
81
+ outcome: DisableOutcome;
82
+ };
83
+ /**
84
+ * Remove `enabled: false` from a file's frontmatter, wherever it appears in the
85
+ * block — the loader honors the key at any position, so the two must agree or a
86
+ * hand-authored agent can be disabled and never re-enabled.
87
+ *
88
+ * `changed` is false when the key wasn't found, so the caller can avoid
89
+ * reporting "Enabled <name>" for a write that did nothing.
90
+ */
91
+ export declare function enableInContent(content: string): {
92
+ content: string;
93
+ changed: boolean;
94
+ };
95
+ /** Is this the empty stub `/agents` writes when disabling a built-in default? */
96
+ export declare function isEmptyStub(content: string): boolean;
97
+ /** The answers `/agents → Create agent → Manual` collects, before serialization. */
98
+ export interface NewAgentInput {
99
+ description: string;
100
+ /** Already-resolved `tools:` value ("none", "all", or a CSV of tool names). */
101
+ tools: string;
102
+ /** `provider/modelId`, or undefined to inherit the parent's model. */
103
+ model?: string;
104
+ /** A pi thinking level, or undefined to inherit. */
105
+ thinking?: string;
106
+ systemPrompt: string;
107
+ }
108
+ /**
109
+ * Build the .md file the create wizard writes.
110
+ *
111
+ * `description` and `model` come straight from a free-text prompt, so they are
112
+ * quoted rather than interpolated — `serializeAgentFile` above quotes the
113
+ * description for the same reason. An unquoted YAML scalar mishandles ordinary
114
+ * input in two ways, and both are silent: a colon ("Scout: find things") makes
115
+ * the file unparseable, and since #212 an unparseable agent file is *skipped*,
116
+ * so the wizard reports success for an agent that does not exist; a `#`
117
+ * ("audit #security") opens a comment and truncates the value. `model` can
118
+ * carry a colon too — pi accepts a `provider/model:thinking` suffix.
119
+ *
120
+ * `tools` and `thinking` are not quoted: both are chosen from fixed menus, and
121
+ * `tools` is a CSV that must stay a bare scalar for the loader's parser.
122
+ */
123
+ export declare function buildNewAgentFile(input: NewAgentInput): string;
124
+ /** Serialize an AgentConfig to a full .md file (frontmatter + system prompt) for eject. */
125
+ export declare function serializeAgentFile(cfg: AgentConfig): string;