@herbertgao/pi-subagents 0.15.2 → 0.15.4
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 +14 -0
- package/package.json +8 -4
- package/src/agent-color.ts +72 -67
- package/src/agent-file-toggle.ts +255 -0
- package/src/agent-manager.ts +192 -3
- package/src/agent-runner.ts +18 -3
- package/src/index.ts +322 -148
- package/src/nested-tools.ts +26 -35
- package/src/output-file.ts +24 -1
- package/src/prompts.ts +15 -1
- package/src/ui/agent-widget.ts +24 -2
- package/src/ui/schedule-menu.ts +9 -8
- package/src/ui/select-item.ts +48 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.15.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#45](https://github.com/HerbertGao/pi-extensions/pull/45) [`97bad4e`](https://github.com/HerbertGao/pi-extensions/commit/97bad4e05f7398d9910786e8d614ffd4943cac8c) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Link persisted subagent sessions to their parent Pi session and advance the reviewed upstream baseline to 0.15.1.
|
|
8
|
+
|
|
9
|
+
- [#52](https://github.com/HerbertGao/pi-extensions/pull/52) [`b12ab55`](https://github.com/HerbertGao/pi-extensions/commit/b12ab5597059ff2020e2108f8e8de85ce64e49c7) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Sync the applicable pi-subagents 0.16.0 fixes and advance the reviewed upstream baseline to bcbd602.
|
|
10
|
+
|
|
11
|
+
## 0.15.3
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [#27](https://github.com/HerbertGao/pi-extensions/pull/27) [`32bb76c`](https://github.com/HerbertGao/pi-extensions/commit/32bb76c117971de27db9c2b521d19df3ba9ea322) Thanks [@HerbertGao](https://github.com/HerbertGao)! - Keep worktree-isolated agents inside their copy, preserve real Agent tool startup errors, and render unknown or failed Agent results without misleading completion status.
|
|
16
|
+
|
|
3
17
|
## 0.15.2
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@herbertgao/pi-subagents",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.4",
|
|
4
4
|
"description": "Claude Code-style autonomous subagents for Pi, with HerbertGao-maintained UI extensions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|
|
@@ -40,13 +40,17 @@
|
|
|
40
40
|
"scripts": {
|
|
41
41
|
"lint": "biome check src/ test/",
|
|
42
42
|
"typecheck": "tsc --noEmit",
|
|
43
|
-
"test": "vitest run"
|
|
43
|
+
"test": "vitest run",
|
|
44
|
+
"test:coverage": "vitest run --coverage"
|
|
44
45
|
},
|
|
45
46
|
"dependencies": {
|
|
46
47
|
"@sinclair/typebox": "^0.34.49",
|
|
47
48
|
"croner": "^10.0.1",
|
|
48
49
|
"nanoid": "^5.0.0"
|
|
49
50
|
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@vitest/coverage-istanbul": "^4.1.10"
|
|
53
|
+
},
|
|
50
54
|
"peerDependencies": {
|
|
51
55
|
"@earendil-works/pi-ai": ">=0.80.0",
|
|
52
56
|
"@earendil-works/pi-coding-agent": ">=0.80.0",
|
|
@@ -64,8 +68,8 @@
|
|
|
64
68
|
},
|
|
65
69
|
"x-upstream": {
|
|
66
70
|
"package": "@tintinweb/pi-subagents",
|
|
67
|
-
"version": "0.
|
|
71
|
+
"version": "0.16.0",
|
|
68
72
|
"repository": "https://github.com/tintinweb/pi-subagents",
|
|
69
|
-
"commit": "
|
|
73
|
+
"commit": "bcbd602"
|
|
70
74
|
}
|
|
71
75
|
}
|
package/src/agent-color.ts
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* agent-color.ts — Claude Code-compatible agent name
|
|
2
|
+
* agent-color.ts — Claude Code-compatible agent name badges.
|
|
3
3
|
*
|
|
4
|
-
* Claude Code
|
|
5
|
-
*
|
|
6
|
-
*
|
|
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.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import { getConfig } from "./agent-types.js"
|
|
10
11
|
|
|
11
12
|
const NAMED_AGENT_COLORS: Readonly<Record<string, string>> = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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.
|
|
20
23
|
amber: "#F59E0B",
|
|
21
24
|
teal: "#008080",
|
|
22
25
|
indigo: "#6366F1",
|
|
@@ -34,8 +37,10 @@ const NAMED_AGENT_COLORS: Readonly<Record<string, string>> = {
|
|
|
34
37
|
navy: "#1E3A8A",
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
const CUBE_VALUES = [0, 95, 135, 175, 215, 255]
|
|
40
|
+
const CUBE_VALUES = [0, 95, 135, 175, 215, 255]
|
|
38
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 }
|
|
39
44
|
|
|
40
45
|
type Rgb = { r: number; g: number; b: number }
|
|
41
46
|
type ColorMode = "truecolor" | "256color"
|
|
@@ -72,51 +77,48 @@ function parseHex(hex: string): Rgb {
|
|
|
72
77
|
}
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
best
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
/** Index of the entry in `values` closest to `value`. */
|
|
81
|
+
function nearest(values: readonly number[], value: number): number {
|
|
82
|
+
return values.reduce(
|
|
83
|
+
(best, v, i) =>
|
|
84
|
+
Math.abs(value - v) < Math.abs(value - values[best]) ? i : best,
|
|
85
|
+
0,
|
|
86
|
+
)
|
|
82
87
|
}
|
|
83
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Quantize to the xterm-256 palette the way pi's own theme does, returning both
|
|
91
|
+
* the index to emit and the color the terminal will actually show — badge
|
|
92
|
+
* contrast is judged against the latter.
|
|
93
|
+
*/
|
|
84
94
|
function rgbTo256({ r, g, b }: Rgb): { index: number; rgb: Rgb } {
|
|
85
|
-
const rIndex =
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
r
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const dg = g - candidate.g
|
|
96
|
-
const db = b - candidate.b
|
|
97
|
-
return dr * dr * 0.299 + dg * dg * 0.587 + db * db * 0.114
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b)
|
|
101
|
-
let grayIndex = 0
|
|
102
|
-
for (let i = 1; i < GRAY_VALUES.length; i++) {
|
|
103
|
-
if (
|
|
104
|
-
Math.abs(gray - GRAY_VALUES[i]) < Math.abs(gray - GRAY_VALUES[grayIndex])
|
|
105
|
-
)
|
|
106
|
-
grayIndex = i
|
|
107
|
-
}
|
|
108
|
-
const grayRgb = {
|
|
95
|
+
const [rIndex, gIndex, bIndex] = [r, g, b].map((channel) =>
|
|
96
|
+
nearest(CUBE_VALUES, channel),
|
|
97
|
+
)
|
|
98
|
+
const distance = ({ r: cr, g: cg, b: cb }: Rgb) =>
|
|
99
|
+
0.299 * (r - cr) ** 2 + 0.587 * (g - cg) ** 2 + 0.114 * (b - cb) ** 2
|
|
100
|
+
const grayIndex = nearest(
|
|
101
|
+
GRAY_VALUES,
|
|
102
|
+
Math.round(0.299 * r + 0.587 * g + 0.114 * b),
|
|
103
|
+
)
|
|
104
|
+
const gray = {
|
|
109
105
|
r: GRAY_VALUES[grayIndex],
|
|
110
106
|
g: GRAY_VALUES[grayIndex],
|
|
111
107
|
b: GRAY_VALUES[grayIndex],
|
|
112
108
|
}
|
|
109
|
+
const cube = {
|
|
110
|
+
r: CUBE_VALUES[rIndex],
|
|
111
|
+
g: CUBE_VALUES[gIndex],
|
|
112
|
+
b: CUBE_VALUES[bIndex],
|
|
113
|
+
}
|
|
114
|
+
// Only near-neutral colors may take the gray ramp; anything else keeps its tint.
|
|
113
115
|
if (
|
|
114
116
|
Math.max(r, g, b) - Math.min(r, g, b) < 10 &&
|
|
115
|
-
distance(
|
|
117
|
+
distance(gray) < distance(cube)
|
|
116
118
|
) {
|
|
117
|
-
return { index: 232 + grayIndex, rgb:
|
|
119
|
+
return { index: 232 + grayIndex, rgb: gray }
|
|
118
120
|
}
|
|
119
|
-
return { index: 16 + 36 * rIndex + 6 * gIndex + bIndex, rgb:
|
|
121
|
+
return { index: 16 + 36 * rIndex + 6 * gIndex + bIndex, rgb: cube }
|
|
120
122
|
}
|
|
121
123
|
|
|
122
124
|
function ansiColor(
|
|
@@ -140,9 +142,10 @@ function relativeLuminance({ r, g, b }: Rgb): number {
|
|
|
140
142
|
}
|
|
141
143
|
|
|
142
144
|
/**
|
|
143
|
-
* Render one name as a padded background badge when `color` is valid.
|
|
144
|
-
*
|
|
145
|
-
*
|
|
145
|
+
* Render one name as a padded background badge when `color` is valid. Claude
|
|
146
|
+
* Code uses one inverse color for every badge's text; black or white is picked
|
|
147
|
+
* by WCAG contrast here instead, so each palette entry stays readable. Invalid
|
|
148
|
+
* or omitted colors preserve the caller's existing theme styling.
|
|
146
149
|
*/
|
|
147
150
|
export function renderAgentNameLabel(
|
|
148
151
|
name: string,
|
|
@@ -156,32 +159,34 @@ export function renderAgentNameLabel(
|
|
|
156
159
|
return style.fallbackColor ? theme.fg(style.fallbackColor, text) : text
|
|
157
160
|
}
|
|
158
161
|
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
effectiveBackground = quantized.rgb
|
|
167
|
-
}
|
|
168
|
-
const foregroundRgb =
|
|
169
|
-
relativeLuminance(effectiveBackground) > 0.179
|
|
170
|
-
? { r: 0, g: 0, b: 0 }
|
|
171
|
-
: { r: 255, g: 255, b: 255 }
|
|
172
|
-
const foreground =
|
|
173
|
-
mode === "256color" ? rgbTo256(foregroundRgb).index : foregroundRgb
|
|
162
|
+
const rgb = parseHex(resolved)
|
|
163
|
+
const quantized =
|
|
164
|
+
(theme.getColorMode?.() ?? "truecolor") === "256color"
|
|
165
|
+
? rgbTo256(rgb)
|
|
166
|
+
: undefined
|
|
167
|
+
const shown = quantized?.rgb ?? rgb
|
|
168
|
+
const contrasting = relativeLuminance(shown) > 0.179 ? BLACK : WHITE
|
|
174
169
|
const label = style.bold ? theme.bold(` ${name} `) : ` ${name} `
|
|
175
170
|
|
|
176
171
|
return (
|
|
177
|
-
ansiColor("background",
|
|
178
|
-
ansiColor(
|
|
172
|
+
ansiColor("background", quantized?.index ?? rgb) +
|
|
173
|
+
ansiColor(
|
|
174
|
+
"foreground",
|
|
175
|
+
quantized ? rgbTo256(contrasting).index : contrasting,
|
|
176
|
+
) +
|
|
179
177
|
label +
|
|
180
178
|
"\u001b[39m" +
|
|
181
179
|
(style.restoreBackground ?? "\u001b[49m")
|
|
182
180
|
)
|
|
183
181
|
}
|
|
184
182
|
|
|
183
|
+
/** Whether an agent renders as a badge — i.e. it has a valid configured color. */
|
|
184
|
+
export function hasAgentBadge(type: string | undefined): boolean {
|
|
185
|
+
return (
|
|
186
|
+
type !== undefined && resolveAgentColor(getConfig(type).color) !== undefined
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
|
|
185
190
|
/** Render a registered agent's display name with its configured color. */
|
|
186
191
|
export function renderAgentName(
|
|
187
192
|
type: string | undefined,
|
|
@@ -0,0 +1,255 @@
|
|
|
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 the parser's case-insensitive
|
|
24
|
+
* bare `false` spellings and trailing comments, and reports `changed: false`
|
|
25
|
+
* for values it cannot rewrite, so the caller refuses honestly rather than
|
|
26
|
+
* announcing a change it did not make.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { existsSync } from "node:fs"
|
|
30
|
+
import { join } from "node:path"
|
|
31
|
+
import { getAgentDir, parseFrontmatter } from "@earendil-works/pi-coding-agent"
|
|
32
|
+
import type { AgentConfig } from "./types.js"
|
|
33
|
+
|
|
34
|
+
export type AgentFileLocation = "project" | "workspace" | "personal"
|
|
35
|
+
|
|
36
|
+
export const projectAgentsDir = (cwd: string = process.cwd()) =>
|
|
37
|
+
join(cwd, ".pi", "agents")
|
|
38
|
+
export const workspaceAgentsDir = (cwd: string = process.cwd()) =>
|
|
39
|
+
join(cwd, ".agents", "agents")
|
|
40
|
+
export const personalAgentsDir = () => join(getAgentDir(), "agents")
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Find the file path of a custom agent by name, in discovery-precedence order
|
|
44
|
+
* (project, workspace, then global). Mirrors the load-side precedence in
|
|
45
|
+
* src/custom-agents.ts — if the two drift, `/agents` edits a file the loader
|
|
46
|
+
* isn't reading.
|
|
47
|
+
*/
|
|
48
|
+
export function findAgentFile(
|
|
49
|
+
name: string,
|
|
50
|
+
cwd: string = process.cwd(),
|
|
51
|
+
): { path: string; location: AgentFileLocation } | undefined {
|
|
52
|
+
const projectPath = join(projectAgentsDir(cwd), `${name}.md`)
|
|
53
|
+
if (existsSync(projectPath)) return { path: projectPath, location: "project" }
|
|
54
|
+
const workspacePath = join(workspaceAgentsDir(cwd), `${name}.md`)
|
|
55
|
+
if (existsSync(workspacePath))
|
|
56
|
+
return { path: workspacePath, location: "workspace" }
|
|
57
|
+
const personalPath = join(personalAgentsDir(), `${name}.md`)
|
|
58
|
+
if (existsSync(personalPath))
|
|
59
|
+
return { path: personalPath, location: "personal" }
|
|
60
|
+
return undefined
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type DisableOutcome = "disabled" | "already-disabled" | "no-frontmatter"
|
|
64
|
+
|
|
65
|
+
/** A line that sets `enabled: false`, accepting YAML case and comments. */
|
|
66
|
+
const ENABLED_FALSE = /^[ \t]*enabled:[ \t]*false[ \t]*(?:#[^\r\n]*)?$/i
|
|
67
|
+
/** An opening or closing `---` fence line. */
|
|
68
|
+
const FENCE = /^---[ \t]*$/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Split a file into its frontmatter lines and everything else, agreeing with
|
|
72
|
+
* what `parseFrontmatter` (the load side) considers a frontmatter block.
|
|
73
|
+
*
|
|
74
|
+
* Lines keep their terminators, so an edit preserves the file's existing line
|
|
75
|
+
* endings instead of rewriting CRLF to LF. Returns undefined when there is no
|
|
76
|
+
* usable block — notably for a BOM-prefixed file, which the parser also reads
|
|
77
|
+
* as having none, so writing a key into it would change nothing on load.
|
|
78
|
+
*/
|
|
79
|
+
function splitFrontmatter(
|
|
80
|
+
content: string,
|
|
81
|
+
):
|
|
82
|
+
| { lines: string[]; openIdx: number; closeIdx: number; eol: string }
|
|
83
|
+
| undefined {
|
|
84
|
+
const lines = content.split(/(?<=\n)/)
|
|
85
|
+
if (lines.length === 0 || !FENCE.test(lines[0].replace(/\r?\n$/, "")))
|
|
86
|
+
return undefined
|
|
87
|
+
const closeIdx = lines.findIndex(
|
|
88
|
+
(l, i) => i > 0 && FENCE.test(l.replace(/\r?\n$/, "")),
|
|
89
|
+
)
|
|
90
|
+
if (closeIdx === -1) return undefined
|
|
91
|
+
return {
|
|
92
|
+
lines,
|
|
93
|
+
openIdx: 0,
|
|
94
|
+
closeIdx,
|
|
95
|
+
eol: lines[0].endsWith("\r\n") ? "\r\n" : "\n",
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Does the loader consider this file disabled?
|
|
101
|
+
*
|
|
102
|
+
* Detection is a READ operation, so it asks the same parser the loader uses
|
|
103
|
+
* rather than mirroring it with a regex — that mirror has to be right about
|
|
104
|
+
* YAML's boolean spellings (`False`, `FALSE`, a trailing `# comment`, a quoted
|
|
105
|
+
* key) *and* about pi's fence scan, which closes the block on any line starting
|
|
106
|
+
* `---` and so ends it early on `----`. A throw means the file is already
|
|
107
|
+
* unparseable, which is what the loader sees too: it skips the agent, so there
|
|
108
|
+
* is no "disabled" state to report.
|
|
109
|
+
*/
|
|
110
|
+
export function isDisabledContent(content: string): boolean {
|
|
111
|
+
try {
|
|
112
|
+
return (
|
|
113
|
+
parseFrontmatter<Record<string, unknown>>(content).frontmatter.enabled ===
|
|
114
|
+
false
|
|
115
|
+
)
|
|
116
|
+
} catch {
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Add `enabled: false` to a file's frontmatter.
|
|
123
|
+
*
|
|
124
|
+
* `outcome` distinguishes a real edit from a no-op so the caller can report
|
|
125
|
+
* honestly instead of unconditionally claiming success.
|
|
126
|
+
*/
|
|
127
|
+
export function disableInContent(content: string): {
|
|
128
|
+
content: string
|
|
129
|
+
outcome: DisableOutcome
|
|
130
|
+
} {
|
|
131
|
+
const block = splitFrontmatter(content)
|
|
132
|
+
if (!block) return { content, outcome: "no-frontmatter" }
|
|
133
|
+
if (isDisabledContent(content))
|
|
134
|
+
return { content, outcome: "already-disabled" }
|
|
135
|
+
const lines = [...block.lines]
|
|
136
|
+
lines.splice(1, 0, `enabled: false${block.eol}`)
|
|
137
|
+
return { content: lines.join(""), outcome: "disabled" }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Remove `enabled: false` from a file's frontmatter, wherever it appears in the
|
|
142
|
+
* block — the loader honors the key at any position, so the two must agree or a
|
|
143
|
+
* hand-authored agent can be disabled and never re-enabled.
|
|
144
|
+
*
|
|
145
|
+
* `changed` is false when the key wasn't found, so the caller can avoid
|
|
146
|
+
* reporting "Enabled <name>" for a write that did nothing.
|
|
147
|
+
*/
|
|
148
|
+
export function enableInContent(content: string): {
|
|
149
|
+
content: string
|
|
150
|
+
changed: boolean
|
|
151
|
+
} {
|
|
152
|
+
const block = splitFrontmatter(content)
|
|
153
|
+
if (!block) return { content, changed: false }
|
|
154
|
+
const kept = block.lines.filter(
|
|
155
|
+
(l, i) =>
|
|
156
|
+
!(
|
|
157
|
+
i > 0 &&
|
|
158
|
+
i < block.closeIdx &&
|
|
159
|
+
ENABLED_FALSE.test(l.replace(/\r?\n$/, ""))
|
|
160
|
+
),
|
|
161
|
+
)
|
|
162
|
+
if (kept.length === block.lines.length) return { content, changed: false }
|
|
163
|
+
return { content: kept.join(""), changed: true }
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Is this the empty stub `/agents` writes when disabling a built-in default? */
|
|
167
|
+
export function isEmptyStub(content: string): boolean {
|
|
168
|
+
return content.replace(/\r\n/g, "\n").trim() === "---\n---"
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The answers `/agents → Create agent → Manual` collects, before serialization. */
|
|
172
|
+
export interface NewAgentInput {
|
|
173
|
+
description: string
|
|
174
|
+
/** Already-resolved `tools:` value ("none", "all", or a CSV of tool names). */
|
|
175
|
+
tools: string
|
|
176
|
+
/** `provider/modelId`, or undefined to inherit the parent's model. */
|
|
177
|
+
model?: string
|
|
178
|
+
/** A pi thinking level, or undefined to inherit. */
|
|
179
|
+
thinking?: string
|
|
180
|
+
systemPrompt: string
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Build the .md file the create wizard writes.
|
|
185
|
+
*
|
|
186
|
+
* `description` and `model` come straight from a free-text prompt, so they are
|
|
187
|
+
* quoted rather than interpolated — `serializeAgentFile` above quotes the
|
|
188
|
+
* description for the same reason. An unquoted YAML scalar mishandles ordinary
|
|
189
|
+
* input in two ways, and both are silent: a colon ("Scout: find things") makes
|
|
190
|
+
* the file unparseable, and since #212 an unparseable agent file is *skipped*,
|
|
191
|
+
* so the wizard reports success for an agent that does not exist; a `#`
|
|
192
|
+
* ("audit #security") opens a comment and truncates the value. `model` can
|
|
193
|
+
* carry a colon too — pi accepts a `provider/model:thinking` suffix.
|
|
194
|
+
*
|
|
195
|
+
* `tools` and `thinking` are not quoted: both are chosen from fixed menus, and
|
|
196
|
+
* `tools` is a CSV that must stay a bare scalar for the loader's parser.
|
|
197
|
+
*/
|
|
198
|
+
export function buildNewAgentFile(input: NewAgentInput): string {
|
|
199
|
+
const modelLine = input.model ? `\nmodel: ${JSON.stringify(input.model)}` : ""
|
|
200
|
+
const thinkingLine = input.thinking ? `\nthinking: ${input.thinking}` : ""
|
|
201
|
+
return `---
|
|
202
|
+
description: ${JSON.stringify(input.description)}
|
|
203
|
+
tools: ${input.tools}${modelLine}${thinkingLine}
|
|
204
|
+
prompt_mode: replace
|
|
205
|
+
---
|
|
206
|
+
|
|
207
|
+
${input.systemPrompt}
|
|
208
|
+
`
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Render a built-in tool list as a `tools:` frontmatter value. */
|
|
212
|
+
function formatToolsField(tools: string[] | undefined): string {
|
|
213
|
+
if (tools === undefined) return "all"
|
|
214
|
+
if (tools.length === 0) return "none"
|
|
215
|
+
return tools.join(", ")
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Serialize an AgentConfig to a full .md file (frontmatter + system prompt) for eject. */
|
|
219
|
+
export function serializeAgentFile(cfg: AgentConfig): string {
|
|
220
|
+
const fmFields: string[] = []
|
|
221
|
+
fmFields.push(`description: ${JSON.stringify(cfg.description)}`)
|
|
222
|
+
if (cfg.displayName) fmFields.push(`display_name: ${cfg.displayName}`)
|
|
223
|
+
if (cfg.color) fmFields.push(`color: ${JSON.stringify(cfg.color)}`)
|
|
224
|
+
// Absent means "all built-ins"; an EMPTY list means explicitly zero. Writing
|
|
225
|
+
// `all` for both would hand a deliberately tool-less agent the whole toolbox
|
|
226
|
+
// the first time it is ejected.
|
|
227
|
+
fmFields.push(`tools: ${formatToolsField(cfg.builtinToolNames)}`)
|
|
228
|
+
if (cfg.model) fmFields.push(`model: ${cfg.model}`)
|
|
229
|
+
if (cfg.thinking) fmFields.push(`thinking: ${cfg.thinking}`)
|
|
230
|
+
if (cfg.maxTurns) fmFields.push(`max_turns: ${cfg.maxTurns}`)
|
|
231
|
+
if (cfg.allowedSubagents !== undefined) {
|
|
232
|
+
fmFields.push(
|
|
233
|
+
`allowed_subagents: ${cfg.allowedSubagents === "all" ? "all" : cfg.allowedSubagents.join(", ")}`,
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
fmFields.push(`prompt_mode: ${cfg.promptMode}`)
|
|
237
|
+
if (cfg.extensions === false) fmFields.push("extensions: false")
|
|
238
|
+
else if (Array.isArray(cfg.extensions))
|
|
239
|
+
fmFields.push(`extensions: ${cfg.extensions.join(", ")}`)
|
|
240
|
+
if (cfg.excludeExtensions?.length)
|
|
241
|
+
fmFields.push(`exclude_extensions: ${cfg.excludeExtensions.join(", ")}`)
|
|
242
|
+
if (cfg.skills === false) fmFields.push("skills: false")
|
|
243
|
+
else if (Array.isArray(cfg.skills))
|
|
244
|
+
fmFields.push(`skills: ${cfg.skills.join(", ")}`)
|
|
245
|
+
if (cfg.disallowedTools?.length)
|
|
246
|
+
fmFields.push(`disallowed_tools: ${cfg.disallowedTools.join(", ")}`)
|
|
247
|
+
if (cfg.inheritContext) fmFields.push("inherit_context: true")
|
|
248
|
+
if (cfg.runInBackground) fmFields.push("run_in_background: true")
|
|
249
|
+
if (cfg.outputTranscript === false) fmFields.push("output_transcript: false")
|
|
250
|
+
if (cfg.isolated) fmFields.push("isolated: true")
|
|
251
|
+
if (cfg.memory) fmFields.push(`memory: ${cfg.memory}`)
|
|
252
|
+
if (cfg.isolation) fmFields.push(`isolation: ${cfg.isolation}`)
|
|
253
|
+
|
|
254
|
+
return `---\n${fmFields.join("\n")}\n---\n\n${cfg.systemPrompt}\n`
|
|
255
|
+
}
|