pi-subagent-manager 1.9.0 → 1.11.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/agents/architect.md +4 -4
- package/agents/coder.md +3 -4
- package/agents/researcher.md +2 -2
- package/agents/writer.md +2 -5
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +836 -0
- package/dist/index.js.map +1 -0
- package/dist/orch/checklist-integration.d.ts +48 -0
- package/dist/orch/checklist-integration.d.ts.map +1 -0
- package/dist/orch/checklist-integration.js +204 -0
- package/dist/orch/checklist-integration.js.map +1 -0
- package/dist/orch/context-entries.d.ts +8 -0
- package/dist/orch/context-entries.d.ts.map +1 -0
- package/dist/orch/context-entries.js +8 -0
- package/dist/orch/context-entries.js.map +1 -0
- package/dist/orch/cost-ledger.d.ts +16 -0
- package/dist/orch/cost-ledger.d.ts.map +1 -0
- package/dist/orch/cost-ledger.js +43 -0
- package/dist/orch/cost-ledger.js.map +1 -0
- package/dist/orch/diagnostics.d.ts +13 -0
- package/dist/orch/diagnostics.d.ts.map +1 -0
- package/dist/orch/diagnostics.js +95 -0
- package/dist/orch/diagnostics.js.map +1 -0
- package/dist/orch/extensions.d.ts +15 -0
- package/dist/orch/extensions.d.ts.map +1 -0
- package/dist/orch/extensions.js +80 -0
- package/dist/orch/extensions.js.map +1 -0
- package/dist/orch/housekeeping-usage.d.ts +14 -0
- package/dist/orch/housekeeping-usage.d.ts.map +1 -0
- package/dist/orch/housekeeping-usage.js +27 -0
- package/dist/orch/housekeeping-usage.js.map +1 -0
- package/dist/orch/housekeeping.d.ts +89 -0
- package/dist/orch/housekeeping.d.ts.map +1 -0
- package/dist/orch/housekeeping.js +281 -0
- package/dist/orch/housekeeping.js.map +1 -0
- package/dist/orch/inherited-tools.d.ts +18 -0
- package/dist/orch/inherited-tools.d.ts.map +1 -0
- package/dist/orch/inherited-tools.js +71 -0
- package/dist/orch/inherited-tools.js.map +1 -0
- package/dist/orch/mailbox.d.ts +59 -0
- package/dist/orch/mailbox.d.ts.map +1 -0
- package/dist/orch/mailbox.js +196 -0
- package/dist/orch/mailbox.js.map +1 -0
- package/dist/orch/manager.d.ts +82 -0
- package/dist/orch/manager.d.ts.map +1 -0
- package/dist/orch/manager.js +1010 -0
- package/dist/orch/manager.js.map +1 -0
- package/dist/orch/paths.d.ts +7 -0
- package/dist/orch/paths.d.ts.map +1 -0
- package/dist/orch/paths.js +73 -0
- package/dist/orch/paths.js.map +1 -0
- package/dist/orch/prompt.d.ts +7 -0
- package/dist/orch/prompt.d.ts.map +1 -0
- package/dist/orch/prompt.js +34 -0
- package/dist/orch/prompt.js.map +1 -0
- package/dist/orch/registry.d.ts +49 -0
- package/dist/orch/registry.d.ts.map +1 -0
- package/dist/orch/registry.js +134 -0
- package/dist/orch/registry.js.map +1 -0
- package/dist/orch/runtime.d.ts +11 -0
- package/dist/orch/runtime.d.ts.map +1 -0
- package/dist/orch/runtime.js +882 -0
- package/dist/orch/runtime.js.map +1 -0
- package/dist/orch/tools.d.ts +6 -0
- package/dist/orch/tools.d.ts.map +1 -0
- package/dist/orch/tools.js +114 -0
- package/dist/orch/tools.js.map +1 -0
- package/dist/orch/transcript.d.ts +28 -0
- package/dist/orch/transcript.d.ts.map +1 -0
- package/dist/orch/transcript.js +176 -0
- package/dist/orch/transcript.js.map +1 -0
- package/dist/prefs/agent-import.d.ts +37 -0
- package/dist/prefs/agent-import.d.ts.map +1 -0
- package/dist/prefs/agent-import.js +196 -0
- package/dist/prefs/agent-import.js.map +1 -0
- package/dist/prefs/config.d.ts +108 -0
- package/dist/prefs/config.d.ts.map +1 -0
- package/dist/prefs/config.js +926 -0
- package/dist/prefs/config.js.map +1 -0
- package/dist/prefs/import-discovery.d.ts +28 -0
- package/dist/prefs/import-discovery.d.ts.map +1 -0
- package/dist/prefs/import-discovery.js +562 -0
- package/dist/prefs/import-discovery.js.map +1 -0
- package/dist/prefs/import-instructions.d.ts +3 -0
- package/dist/prefs/import-instructions.d.ts.map +1 -0
- package/{src/prefs/import-instructions.ts → dist/prefs/import-instructions.js} +1 -0
- package/dist/prefs/import-instructions.js.map +1 -0
- package/dist/prefs/models.d.ts +24 -0
- package/dist/prefs/models.d.ts.map +1 -0
- package/dist/prefs/models.js +57 -0
- package/dist/prefs/models.js.map +1 -0
- package/dist/prefs/settings.d.ts +89 -0
- package/dist/prefs/settings.d.ts.map +1 -0
- package/dist/prefs/settings.js +505 -0
- package/dist/prefs/settings.js.map +1 -0
- package/dist/types.d.ts +293 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/ui/agent-loader.d.ts +21 -0
- package/dist/ui/agent-loader.d.ts.map +1 -0
- package/dist/ui/agent-loader.js +73 -0
- package/dist/ui/agent-loader.js.map +1 -0
- package/dist/ui/agent-navigation-editor.d.ts +46 -0
- package/dist/ui/agent-navigation-editor.d.ts.map +1 -0
- package/dist/ui/agent-navigation-editor.js +189 -0
- package/dist/ui/agent-navigation-editor.js.map +1 -0
- package/dist/ui/cost-footer.d.ts +37 -0
- package/dist/ui/cost-footer.d.ts.map +1 -0
- package/dist/ui/cost-footer.js +215 -0
- package/dist/ui/cost-footer.js.map +1 -0
- package/dist/ui/dialog.d.ts +135 -0
- package/dist/ui/dialog.d.ts.map +1 -0
- package/dist/ui/dialog.js +533 -0
- package/dist/ui/dialog.js.map +1 -0
- package/dist/ui/import-picker.d.ts +38 -0
- package/dist/ui/import-picker.d.ts.map +1 -0
- package/dist/ui/import-picker.js +211 -0
- package/dist/ui/import-picker.js.map +1 -0
- package/dist/ui/live-agent-view.d.ts +82 -0
- package/dist/ui/live-agent-view.d.ts.map +1 -0
- package/dist/ui/live-agent-view.js +546 -0
- package/dist/ui/live-agent-view.js.map +1 -0
- package/dist/ui/model-picker.d.ts +109 -0
- package/dist/ui/model-picker.d.ts.map +1 -0
- package/dist/ui/model-picker.js +549 -0
- package/dist/ui/model-picker.js.map +1 -0
- package/dist/ui/settings-ui.d.ts +17 -0
- package/dist/ui/settings-ui.d.ts.map +1 -0
- package/dist/ui/settings-ui.js +679 -0
- package/dist/ui/settings-ui.js.map +1 -0
- package/dist/ui/status-ui.d.ts +67 -0
- package/dist/ui/status-ui.d.ts.map +1 -0
- package/dist/ui/status-ui.js +487 -0
- package/dist/ui/status-ui.js.map +1 -0
- package/dist/ui/thread-tree.d.ts +22 -0
- package/dist/ui/thread-tree.d.ts.map +1 -0
- package/dist/ui/thread-tree.js +153 -0
- package/dist/ui/thread-tree.js.map +1 -0
- package/dist/ui/tool-picker.d.ts +46 -0
- package/dist/ui/tool-picker.d.ts.map +1 -0
- package/dist/ui/tool-picker.js +334 -0
- package/dist/ui/tool-picker.js.map +1 -0
- package/dist/ui/transcript-tool.d.ts +2 -0
- package/dist/ui/transcript-tool.d.ts.map +1 -0
- package/dist/ui/transcript-tool.js +100 -0
- package/dist/ui/transcript-tool.js.map +1 -0
- package/dist/ui/ui.d.ts +41 -0
- package/dist/ui/ui.d.ts.map +1 -0
- package/dist/ui/ui.js +1047 -0
- package/dist/ui/ui.js.map +1 -0
- package/docs/default-agents.md +11 -13
- package/docs/development.md +2 -1
- package/package.json +16 -4
- package/src/index.ts +0 -920
- package/src/orch/checklist-integration.ts +0 -230
- package/src/orch/context-entries.ts +0 -15
- package/src/orch/cost-ledger.ts +0 -56
- package/src/orch/diagnostics.ts +0 -95
- package/src/orch/extensions.ts +0 -83
- package/src/orch/housekeeping-usage.ts +0 -47
- package/src/orch/housekeeping.ts +0 -348
- package/src/orch/inherited-tools.ts +0 -84
- package/src/orch/mailbox.ts +0 -261
- package/src/orch/manager.ts +0 -1084
- package/src/orch/paths.ts +0 -73
- package/src/orch/prompt.ts +0 -39
- package/src/orch/registry.ts +0 -168
- package/src/orch/runtime.ts +0 -1041
- package/src/orch/tools.ts +0 -190
- package/src/orch/transcript.ts +0 -186
- package/src/prefs/agent-import.ts +0 -245
- package/src/prefs/config.ts +0 -1017
- package/src/prefs/import-discovery.ts +0 -643
- package/src/prefs/models.ts +0 -74
- package/src/prefs/settings.ts +0 -614
- package/src/types.ts +0 -271
- package/src/ui/agent-loader.ts +0 -98
- package/src/ui/agent-navigation-editor.ts +0 -233
- package/src/ui/cost-footer.ts +0 -230
- package/src/ui/dialog.ts +0 -678
- package/src/ui/import-picker.ts +0 -231
- package/src/ui/live-agent-view.ts +0 -600
- package/src/ui/model-picker.ts +0 -701
- package/src/ui/settings-ui.ts +0 -830
- package/src/ui/status-ui.ts +0 -628
- package/src/ui/thread-tree.ts +0 -173
- package/src/ui/tool-picker.ts +0 -398
- package/src/ui/transcript-tool.ts +0 -142
- package/src/ui/ui.ts +0 -1407
|
@@ -0,0 +1,926 @@
|
|
|
1
|
+
import { lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { isMap, isScalar, parseDocument, stringify } from "yaml";
|
|
6
|
+
import { getModelPreferences } from "./models.js";
|
|
7
|
+
import { THINKING_LEVELS } from "../types.js";
|
|
8
|
+
// Pi semantic color tokens, resolved as concrete colors for agent name backgrounds.
|
|
9
|
+
export const AGENT_COLORS = [
|
|
10
|
+
"accent",
|
|
11
|
+
"border",
|
|
12
|
+
"borderAccent",
|
|
13
|
+
"borderMuted",
|
|
14
|
+
"success",
|
|
15
|
+
"error",
|
|
16
|
+
"warning",
|
|
17
|
+
"muted",
|
|
18
|
+
"dim",
|
|
19
|
+
"text",
|
|
20
|
+
"thinkingText",
|
|
21
|
+
"scrollbarTrack",
|
|
22
|
+
"scrollbarThumb",
|
|
23
|
+
"searchMatchText",
|
|
24
|
+
"userMessageText",
|
|
25
|
+
"customMessageText",
|
|
26
|
+
"customMessageLabel",
|
|
27
|
+
"toolTitle",
|
|
28
|
+
"toolOutput",
|
|
29
|
+
"mdHeading",
|
|
30
|
+
"mdLink",
|
|
31
|
+
"mdLinkUrl",
|
|
32
|
+
"mdCode",
|
|
33
|
+
"mdCodeBlock",
|
|
34
|
+
"mdCodeBlockBorder",
|
|
35
|
+
"mdQuote",
|
|
36
|
+
"mdQuoteBorder",
|
|
37
|
+
"mdHr",
|
|
38
|
+
"mdListBullet",
|
|
39
|
+
"toolDiffAdded",
|
|
40
|
+
"toolDiffRemoved",
|
|
41
|
+
"toolDiffContext",
|
|
42
|
+
"syntaxComment",
|
|
43
|
+
"syntaxKeyword",
|
|
44
|
+
"syntaxFunction",
|
|
45
|
+
"syntaxVariable",
|
|
46
|
+
"syntaxString",
|
|
47
|
+
"syntaxNumber",
|
|
48
|
+
"syntaxType",
|
|
49
|
+
"syntaxOperator",
|
|
50
|
+
"syntaxPunctuation",
|
|
51
|
+
"thinkingOff",
|
|
52
|
+
"thinkingMinimal",
|
|
53
|
+
"thinkingLow",
|
|
54
|
+
"thinkingMedium",
|
|
55
|
+
"thinkingHigh",
|
|
56
|
+
"thinkingXhigh",
|
|
57
|
+
"thinkingMax",
|
|
58
|
+
"bashMode",
|
|
59
|
+
];
|
|
60
|
+
const NAME = /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/;
|
|
61
|
+
const TOOL = /^[a-zA-Z0-9_-]+$/;
|
|
62
|
+
const FIELDS = new Set([
|
|
63
|
+
"name",
|
|
64
|
+
"description",
|
|
65
|
+
"models",
|
|
66
|
+
"model",
|
|
67
|
+
"modelSuggestions",
|
|
68
|
+
"thinkingLevel",
|
|
69
|
+
"color",
|
|
70
|
+
"icon",
|
|
71
|
+
"tools",
|
|
72
|
+
]);
|
|
73
|
+
const SUBAGENT_MANAGER_DIR = "subagent-manager";
|
|
74
|
+
const AGENTS_DIR = "agents";
|
|
75
|
+
function errorCode(error) {
|
|
76
|
+
return typeof error === "object" && error !== null && "code" in error
|
|
77
|
+
? String(error.code)
|
|
78
|
+
: undefined;
|
|
79
|
+
}
|
|
80
|
+
function lstatIfPresent(path) {
|
|
81
|
+
try {
|
|
82
|
+
return lstatSync(path);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (errorCode(error) === "ENOENT")
|
|
86
|
+
return undefined;
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function assertNotSymlinkPath(path) {
|
|
91
|
+
const stat = lstatIfPresent(path);
|
|
92
|
+
if (stat?.isSymbolicLink())
|
|
93
|
+
throw new Error(`Unsafe symlink path: ${path}`);
|
|
94
|
+
return stat;
|
|
95
|
+
}
|
|
96
|
+
function validatePreservedDestination(filePath, directory, extensions = [".md"]) {
|
|
97
|
+
const resolved = resolve(filePath);
|
|
98
|
+
if (dirname(resolved) !== directory ||
|
|
99
|
+
!extensions.some((extension) => resolved.endsWith(extension)))
|
|
100
|
+
throw new Error(`Unsafe agent destination: ${resolved}`);
|
|
101
|
+
const stat = lstatIfPresent(resolved);
|
|
102
|
+
if (!stat?.isFile())
|
|
103
|
+
throw new Error(`Unsafe agent destination: ${resolved}`);
|
|
104
|
+
return resolved;
|
|
105
|
+
}
|
|
106
|
+
function frontmatter(content) {
|
|
107
|
+
const match = /^(?:\uFEFF)?---\r?\n([\s\S]*?)^---[ \t]*(?:\r?\n|$)/m.exec(content);
|
|
108
|
+
if (!match || match.index !== 0)
|
|
109
|
+
throw new Error("Expected YAML frontmatter enclosed by --- lines");
|
|
110
|
+
return { yaml: match[1], body: content.slice(match[0].length) };
|
|
111
|
+
}
|
|
112
|
+
function record(value, label) {
|
|
113
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
114
|
+
throw new Error(`${label} must be a mapping`);
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
/** Advisory display names only. Empty means no suggestions; never a model pin. */
|
|
118
|
+
function modelSuggestionNames(value) {
|
|
119
|
+
if (!Array.isArray(value))
|
|
120
|
+
throw new Error("modelSuggestions must be an array of nonempty display names");
|
|
121
|
+
const suggestions = value.map((entry, index) => {
|
|
122
|
+
if (typeof entry !== "string")
|
|
123
|
+
throw new Error(`modelSuggestions[${index}] must be a nonempty display name`);
|
|
124
|
+
const name = entry.trim();
|
|
125
|
+
if (!name)
|
|
126
|
+
throw new Error(`modelSuggestions[${index}] must be a nonempty display name`);
|
|
127
|
+
return name;
|
|
128
|
+
});
|
|
129
|
+
if (new Set(suggestions).size !== suggestions.length)
|
|
130
|
+
throw new Error("modelSuggestions contains duplicate display names");
|
|
131
|
+
return [...suggestions];
|
|
132
|
+
}
|
|
133
|
+
function toolNames(value, label) {
|
|
134
|
+
if (!Array.isArray(value) || value.some((name) => typeof name !== "string" || !TOOL.test(name))) {
|
|
135
|
+
throw new Error(`${label} must be an array of exact tool names (no wildcards or patterns)`);
|
|
136
|
+
}
|
|
137
|
+
if (new Set(value).size !== value.length)
|
|
138
|
+
throw new Error(`${label} contains duplicate tool names`);
|
|
139
|
+
return [...value];
|
|
140
|
+
}
|
|
141
|
+
function toolPolicy(value) {
|
|
142
|
+
const mapping = record(value, "tools");
|
|
143
|
+
for (const key of Object.keys(mapping))
|
|
144
|
+
if (key !== "allow" && key !== "block")
|
|
145
|
+
throw new Error(`Unknown tools field: ${key}`);
|
|
146
|
+
const result = {};
|
|
147
|
+
if (Object.hasOwn(mapping, "allow"))
|
|
148
|
+
result.allow = toolNames(mapping.allow, "tools.allow");
|
|
149
|
+
if (Object.hasOwn(mapping, "block"))
|
|
150
|
+
result.block = toolNames(mapping.block, "tools.block");
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
function overrideToolPolicy(value) {
|
|
154
|
+
const mapping = record(value, "tools");
|
|
155
|
+
const result = {};
|
|
156
|
+
for (const key of Object.keys(mapping)) {
|
|
157
|
+
if (key !== "allow" && key !== "block")
|
|
158
|
+
throw new Error(`Unknown tools field: ${key}`);
|
|
159
|
+
result[key] = mapping[key] === null ? null : toolNames(mapping[key], `tools.${key}`);
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
export function parseAgentType(content, filePath) {
|
|
164
|
+
try {
|
|
165
|
+
const { yaml, body } = frontmatter(content);
|
|
166
|
+
const doc = parseDocument(yaml, { uniqueKeys: true });
|
|
167
|
+
if (doc.errors.length)
|
|
168
|
+
throw new Error(doc.errors.map((error) => error.message).join("; "));
|
|
169
|
+
const data = record(doc.toJS({ maxAliasCount: 0 }), "Frontmatter");
|
|
170
|
+
for (const key of Object.keys(data))
|
|
171
|
+
if (!FIELDS.has(key))
|
|
172
|
+
throw new Error(`Unknown frontmatter field: ${key}`);
|
|
173
|
+
if (typeof data.name !== "string" || !NAME.test(data.name))
|
|
174
|
+
throw new Error("name must be a safe identifier using letters, numbers, underscores or hyphens");
|
|
175
|
+
if (typeof data.description !== "string" || !data.description.trim())
|
|
176
|
+
throw new Error("description must be a nonempty string");
|
|
177
|
+
const result = {
|
|
178
|
+
name: data.name,
|
|
179
|
+
description: data.description,
|
|
180
|
+
systemPrompt: body,
|
|
181
|
+
};
|
|
182
|
+
const models = getModelPreferences({
|
|
183
|
+
models: data.models,
|
|
184
|
+
model: data.model,
|
|
185
|
+
});
|
|
186
|
+
if (models !== undefined)
|
|
187
|
+
result.models = models;
|
|
188
|
+
if (Object.hasOwn(data, "modelSuggestions"))
|
|
189
|
+
result.modelSuggestions = modelSuggestionNames(data.modelSuggestions);
|
|
190
|
+
if (Object.hasOwn(data, "thinkingLevel")) {
|
|
191
|
+
if (!THINKING_LEVELS.includes(data.thinkingLevel))
|
|
192
|
+
throw new Error(`thinkingLevel must be one of: ${THINKING_LEVELS.join(", ")}`);
|
|
193
|
+
result.thinkingLevel = data.thinkingLevel;
|
|
194
|
+
}
|
|
195
|
+
if (Object.hasOwn(data, "color")) {
|
|
196
|
+
if (!AGENT_COLORS.includes(data.color))
|
|
197
|
+
throw new Error(`color must be a Pi foreground token: ${AGENT_COLORS.join(", ")}`);
|
|
198
|
+
result.color = data.color;
|
|
199
|
+
}
|
|
200
|
+
if (Object.hasOwn(data, "icon")) {
|
|
201
|
+
if (typeof data.icon !== "string" || !/^\p{Co}$/u.test(data.icon))
|
|
202
|
+
throw new Error("icon must be a single literal Nerd Font glyph (Unicode private-use character)");
|
|
203
|
+
result.icon = data.icon;
|
|
204
|
+
}
|
|
205
|
+
if (Object.hasOwn(data, "tools"))
|
|
206
|
+
result.tools = toolPolicy(data.tools);
|
|
207
|
+
if (filePath !== undefined)
|
|
208
|
+
result.filePath = filePath;
|
|
209
|
+
return result;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
throw new Error(`${filePath ?? "Agent definition"}: ${error instanceof Error ? error.message : String(error)}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
export function serializeAgentType(type) {
|
|
216
|
+
if (typeof type.systemPrompt !== "string")
|
|
217
|
+
throw new Error("systemPrompt must be a string");
|
|
218
|
+
const data = {
|
|
219
|
+
name: type.name,
|
|
220
|
+
description: type.description,
|
|
221
|
+
};
|
|
222
|
+
const models = getModelPreferences(type);
|
|
223
|
+
if (models !== undefined)
|
|
224
|
+
data.models = models;
|
|
225
|
+
if (type.modelSuggestions !== undefined)
|
|
226
|
+
data.modelSuggestions = modelSuggestionNames(type.modelSuggestions);
|
|
227
|
+
for (const key of ["thinkingLevel", "color", "icon", "tools"])
|
|
228
|
+
if (type[key] !== undefined)
|
|
229
|
+
data[key] = type[key];
|
|
230
|
+
const content = `---\n${stringify(data)}---\n${type.systemPrompt}`;
|
|
231
|
+
parseAgentType(content);
|
|
232
|
+
return content;
|
|
233
|
+
}
|
|
234
|
+
export const AGENT_OVERRIDE_EXTENSIONS = [".yml", ".yaml"];
|
|
235
|
+
function overrideBaseName(file) {
|
|
236
|
+
for (const extension of AGENT_OVERRIDE_EXTENSIONS)
|
|
237
|
+
if (file.endsWith(extension))
|
|
238
|
+
return file.slice(0, -extension.length);
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
export function isAgentOverrideFile(file) {
|
|
242
|
+
return overrideBaseName(file) !== undefined;
|
|
243
|
+
}
|
|
244
|
+
function nullableField(value, validate) {
|
|
245
|
+
if (value === null)
|
|
246
|
+
return null;
|
|
247
|
+
return validate(value);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Parse a settings-only `<name>.yml` override. Rejects prompt bodies and
|
|
251
|
+
* unknown fields; validates shared fields with the same rules as full
|
|
252
|
+
* definitions. `expectedName` (the file basename) must match `name` when set.
|
|
253
|
+
*/
|
|
254
|
+
export function parseAgentSettings(content, filePath, expectedName) {
|
|
255
|
+
const label = filePath ?? "Agent settings override";
|
|
256
|
+
try {
|
|
257
|
+
if (/^\uFEFF?---[ \t]*\r?\n/m.test(content) || content.includes("\n---"))
|
|
258
|
+
throw new Error("Settings overrides are plain YAML with no frontmatter and no Markdown body");
|
|
259
|
+
const doc = parseDocument(content, { uniqueKeys: true });
|
|
260
|
+
if (doc.errors.length)
|
|
261
|
+
throw new Error(doc.errors.map((error) => error.message).join("; "));
|
|
262
|
+
const data = record(doc.toJS({ maxAliasCount: 0 }) ?? {}, "Settings override");
|
|
263
|
+
for (const key of Object.keys(data)) {
|
|
264
|
+
if (key === "systemPrompt" || key === "prompt" || key === "body")
|
|
265
|
+
throw new Error(`Settings overrides carry no prompt body; "${key}" belongs in a <name>.md fork`);
|
|
266
|
+
if (!FIELDS.has(key))
|
|
267
|
+
throw new Error(`Unknown settings field: ${key}`);
|
|
268
|
+
}
|
|
269
|
+
const result = {};
|
|
270
|
+
if (Object.hasOwn(data, "name")) {
|
|
271
|
+
if (typeof data.name !== "string" || !NAME.test(data.name))
|
|
272
|
+
throw new Error("name must be a safe identifier using letters, numbers, underscores or hyphens");
|
|
273
|
+
if (expectedName !== undefined && data.name !== expectedName)
|
|
274
|
+
throw new Error(`name ${JSON.stringify(data.name)} does not match override file ${JSON.stringify(expectedName)}`);
|
|
275
|
+
result.name = data.name;
|
|
276
|
+
}
|
|
277
|
+
if (Object.hasOwn(data, "description")) {
|
|
278
|
+
if (typeof data.description !== "string" || !data.description.trim())
|
|
279
|
+
throw new Error("description must be a nonempty string");
|
|
280
|
+
result.description = data.description;
|
|
281
|
+
}
|
|
282
|
+
const hasModels = Object.hasOwn(data, "models");
|
|
283
|
+
const hasModel = Object.hasOwn(data, "model");
|
|
284
|
+
if (hasModels && hasModel)
|
|
285
|
+
throw new Error("Specify either models or model, not both");
|
|
286
|
+
if (hasModels)
|
|
287
|
+
result.models = nullableField(data.models, (value) => getModelPreferences({ models: value }));
|
|
288
|
+
if (hasModel) {
|
|
289
|
+
if (data.model === null)
|
|
290
|
+
result.model = null;
|
|
291
|
+
else
|
|
292
|
+
result.models = getModelPreferences({ model: data.model });
|
|
293
|
+
}
|
|
294
|
+
if (Object.hasOwn(data, "modelSuggestions"))
|
|
295
|
+
result.modelSuggestions = nullableField(data.modelSuggestions, (value) => modelSuggestionNames(value));
|
|
296
|
+
if (Object.hasOwn(data, "thinkingLevel"))
|
|
297
|
+
result.thinkingLevel = nullableField(data.thinkingLevel, (value) => {
|
|
298
|
+
if (!THINKING_LEVELS.includes(value))
|
|
299
|
+
throw new Error(`thinkingLevel must be one of: ${THINKING_LEVELS.join(", ")}`);
|
|
300
|
+
return value;
|
|
301
|
+
});
|
|
302
|
+
if (Object.hasOwn(data, "color"))
|
|
303
|
+
result.color = nullableField(data.color, (value) => {
|
|
304
|
+
if (!AGENT_COLORS.includes(value))
|
|
305
|
+
throw new Error(`color must be a Pi foreground token: ${AGENT_COLORS.join(", ")}`);
|
|
306
|
+
return value;
|
|
307
|
+
});
|
|
308
|
+
if (Object.hasOwn(data, "icon"))
|
|
309
|
+
result.icon = nullableField(data.icon, (value) => {
|
|
310
|
+
if (typeof value !== "string" || !/^\p{Co}$/u.test(value))
|
|
311
|
+
throw new Error("icon must be a single literal Nerd Font glyph (Unicode private-use character)");
|
|
312
|
+
return value;
|
|
313
|
+
});
|
|
314
|
+
if (Object.hasOwn(data, "tools"))
|
|
315
|
+
result.tools = data.tools === null ? null : overrideToolPolicy(data.tools);
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
catch (error) {
|
|
319
|
+
throw new Error(`${label}: ${error instanceof Error ? error.message : String(error)}`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
export function serializeAgentSettings(override) {
|
|
323
|
+
const data = {};
|
|
324
|
+
if (override.name !== undefined)
|
|
325
|
+
data.name = override.name;
|
|
326
|
+
if (override.description !== undefined)
|
|
327
|
+
data.description = override.description;
|
|
328
|
+
const models = getModelPreferences({
|
|
329
|
+
models: override.models ?? undefined,
|
|
330
|
+
model: override.model ?? undefined,
|
|
331
|
+
});
|
|
332
|
+
if (override.models === null || override.model === null)
|
|
333
|
+
data.models = null;
|
|
334
|
+
else if (models !== undefined)
|
|
335
|
+
data.models = models;
|
|
336
|
+
if (override.modelSuggestions !== undefined)
|
|
337
|
+
data.modelSuggestions =
|
|
338
|
+
override.modelSuggestions === null ? null : modelSuggestionNames(override.modelSuggestions);
|
|
339
|
+
for (const key of ["thinkingLevel", "color", "icon", "tools"])
|
|
340
|
+
if (override[key] !== undefined)
|
|
341
|
+
data[key] = override[key];
|
|
342
|
+
const content = `${stringify(data)}`;
|
|
343
|
+
parseAgentSettings(content);
|
|
344
|
+
return content;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Merge a sparse settings override on top of a base definition. Unset fields
|
|
348
|
+
* follow the base; explicit null clears the field. The prompt body always
|
|
349
|
+
* comes from the base.
|
|
350
|
+
*/
|
|
351
|
+
export function mergeAgentSettings(base, override) {
|
|
352
|
+
const merged = structuredClone(base);
|
|
353
|
+
if (override.description !== undefined)
|
|
354
|
+
merged.description = override.description;
|
|
355
|
+
const models = getModelPreferences({
|
|
356
|
+
models: override.models ?? undefined,
|
|
357
|
+
model: override.model ?? undefined,
|
|
358
|
+
});
|
|
359
|
+
if (override.models === null || override.model === null) {
|
|
360
|
+
delete merged.models;
|
|
361
|
+
delete merged.model;
|
|
362
|
+
}
|
|
363
|
+
else if (models !== undefined) {
|
|
364
|
+
merged.models = [...models];
|
|
365
|
+
delete merged.model;
|
|
366
|
+
}
|
|
367
|
+
for (const key of ["modelSuggestions", "thinkingLevel", "color", "icon"]) {
|
|
368
|
+
const value = override[key];
|
|
369
|
+
if (value === undefined)
|
|
370
|
+
continue;
|
|
371
|
+
if (value === null)
|
|
372
|
+
delete merged[key];
|
|
373
|
+
else
|
|
374
|
+
merged[key] = structuredClone(value);
|
|
375
|
+
}
|
|
376
|
+
if (override.tools === null)
|
|
377
|
+
delete merged.tools;
|
|
378
|
+
else if (override.tools !== undefined) {
|
|
379
|
+
merged.tools ??= {};
|
|
380
|
+
for (const key of ["allow", "block"]) {
|
|
381
|
+
const value = override.tools[key];
|
|
382
|
+
if (value === null)
|
|
383
|
+
delete merged.tools[key];
|
|
384
|
+
else if (value !== undefined)
|
|
385
|
+
merged.tools[key] = [...value];
|
|
386
|
+
}
|
|
387
|
+
if (!Object.keys(merged.tools).length)
|
|
388
|
+
delete merged.tools;
|
|
389
|
+
}
|
|
390
|
+
merged.systemPrompt = base.systemPrompt;
|
|
391
|
+
return merged;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Diff a draft against its base: only changed settings land in the override
|
|
395
|
+
* file, so untouched fields keep tracking bundled updates. The filename
|
|
396
|
+
* identifies the agent; an unchanged draft produces an empty mapping.
|
|
397
|
+
*/
|
|
398
|
+
export function diffAgentSettings(base, draft) {
|
|
399
|
+
const override = {};
|
|
400
|
+
if (draft.description !== base.description)
|
|
401
|
+
override.description = draft.description;
|
|
402
|
+
const baseModels = getModelPreferences(base);
|
|
403
|
+
const draftModels = getModelPreferences(draft);
|
|
404
|
+
const sameModels = (baseModels === undefined && draftModels === undefined) ||
|
|
405
|
+
(baseModels !== undefined &&
|
|
406
|
+
draftModels !== undefined &&
|
|
407
|
+
baseModels.length === draftModels.length &&
|
|
408
|
+
baseModels.every((model, index) => model === draftModels[index]));
|
|
409
|
+
if (!sameModels) {
|
|
410
|
+
if (draftModels === undefined)
|
|
411
|
+
override.models = null;
|
|
412
|
+
else
|
|
413
|
+
override.models = [...draftModels];
|
|
414
|
+
}
|
|
415
|
+
for (const key of ["modelSuggestions", "thinkingLevel", "color", "icon"]) {
|
|
416
|
+
const before = base[key];
|
|
417
|
+
const after = draft[key];
|
|
418
|
+
if (sameSetting(before, after))
|
|
419
|
+
continue;
|
|
420
|
+
if (after === undefined)
|
|
421
|
+
override[key] = null;
|
|
422
|
+
else
|
|
423
|
+
override[key] = structuredClone(after);
|
|
424
|
+
}
|
|
425
|
+
for (const key of ["allow", "block"]) {
|
|
426
|
+
if (sameSetting(base.tools?.[key], draft.tools?.[key]))
|
|
427
|
+
continue;
|
|
428
|
+
override.tools ??= {};
|
|
429
|
+
override.tools[key] = draft.tools?.[key] === undefined ? null : [...draft.tools[key]];
|
|
430
|
+
}
|
|
431
|
+
return override;
|
|
432
|
+
}
|
|
433
|
+
function sameSetting(before, after) {
|
|
434
|
+
return JSON.stringify(before ?? null) === JSON.stringify(after ?? null);
|
|
435
|
+
}
|
|
436
|
+
/** Retain existing explicit pins; update only settings actually edited in this draft. */
|
|
437
|
+
function updateAgentSettings(base, before, draft, existing) {
|
|
438
|
+
const result = structuredClone(existing);
|
|
439
|
+
const edits = diffAgentSettings(before, draft);
|
|
440
|
+
const desired = diffAgentSettings(base, draft);
|
|
441
|
+
for (const key of [
|
|
442
|
+
"description",
|
|
443
|
+
"models",
|
|
444
|
+
"modelSuggestions",
|
|
445
|
+
"thinkingLevel",
|
|
446
|
+
"color",
|
|
447
|
+
"icon",
|
|
448
|
+
]) {
|
|
449
|
+
if (!Object.hasOwn(edits, key))
|
|
450
|
+
continue;
|
|
451
|
+
if (desired[key] === undefined)
|
|
452
|
+
delete result[key];
|
|
453
|
+
else
|
|
454
|
+
result[key] = desired[key];
|
|
455
|
+
if (key === "models")
|
|
456
|
+
delete result.model;
|
|
457
|
+
}
|
|
458
|
+
if (edits.tools) {
|
|
459
|
+
// Whole-policy clears must keep clearing the untouched sibling, too.
|
|
460
|
+
if (result.tools === null)
|
|
461
|
+
result.tools = { allow: null, block: null };
|
|
462
|
+
const tools = result.tools ?? {};
|
|
463
|
+
for (const key of ["allow", "block"]) {
|
|
464
|
+
if (!Object.hasOwn(edits.tools, key))
|
|
465
|
+
continue;
|
|
466
|
+
const value = desired.tools?.[key];
|
|
467
|
+
if (value === undefined)
|
|
468
|
+
delete tools[key];
|
|
469
|
+
else
|
|
470
|
+
tools[key] = value;
|
|
471
|
+
}
|
|
472
|
+
if (Object.keys(tools).length)
|
|
473
|
+
result.tools = tools;
|
|
474
|
+
else
|
|
475
|
+
delete result.tools;
|
|
476
|
+
}
|
|
477
|
+
return result;
|
|
478
|
+
}
|
|
479
|
+
export function selectTools(policy, available, mode = "allowed") {
|
|
480
|
+
const names = new Set(available);
|
|
481
|
+
if (mode === "all")
|
|
482
|
+
return [...names];
|
|
483
|
+
// Ignored lists do not participate in validation or tool selection.
|
|
484
|
+
const relevantPolicy = mode === "all-except-blocked"
|
|
485
|
+
? policy?.block === undefined
|
|
486
|
+
? {}
|
|
487
|
+
: { block: policy.block }
|
|
488
|
+
: policy;
|
|
489
|
+
const validated = relevantPolicy === undefined ? {} : toolPolicy(relevantPolicy);
|
|
490
|
+
for (const name of [...(validated.allow ?? []), ...(validated.block ?? [])]) {
|
|
491
|
+
if (!names.has(name))
|
|
492
|
+
throw new Error(`Unavailable tool name: ${name}`);
|
|
493
|
+
}
|
|
494
|
+
const allow = mode === "all-except-blocked" ? names : new Set(validated.allow ?? []);
|
|
495
|
+
const block = new Set(validated.block ?? []);
|
|
496
|
+
return [...names].filter((name) => allow.has(name) && !block.has(name));
|
|
497
|
+
}
|
|
498
|
+
export class ConfigStore {
|
|
499
|
+
diagnostics = [];
|
|
500
|
+
types = new Map();
|
|
501
|
+
bases = new Map();
|
|
502
|
+
scopeBases = new Map();
|
|
503
|
+
scopeOverrides = new Map();
|
|
504
|
+
options;
|
|
505
|
+
constructor(options) {
|
|
506
|
+
this.options = { ...options };
|
|
507
|
+
this.reload();
|
|
508
|
+
}
|
|
509
|
+
reload() {
|
|
510
|
+
this.types.clear();
|
|
511
|
+
this.bases.clear();
|
|
512
|
+
this.scopeBases.clear();
|
|
513
|
+
this.scopeOverrides.clear();
|
|
514
|
+
this.diagnostics = [];
|
|
515
|
+
const layers = [
|
|
516
|
+
[
|
|
517
|
+
this.options.bundledDir ?? fileURLToPath(new URL("../../agents/", import.meta.url)),
|
|
518
|
+
"bundled",
|
|
519
|
+
],
|
|
520
|
+
...this.scopeDirectories("user").map((directory) => [directory, "user"]),
|
|
521
|
+
];
|
|
522
|
+
if (this.options.includeProject)
|
|
523
|
+
layers.push(...this.scopeDirectories("project").map((directory) => [directory, "project"]));
|
|
524
|
+
for (const [directory, source] of layers) {
|
|
525
|
+
if (source !== "bundled") {
|
|
526
|
+
this.scopeBases.set(source, structuredClone(this.types));
|
|
527
|
+
this.scopeOverrides.set(source, new Map());
|
|
528
|
+
}
|
|
529
|
+
let files;
|
|
530
|
+
try {
|
|
531
|
+
const stat = assertNotSymlinkPath(directory);
|
|
532
|
+
if (!stat)
|
|
533
|
+
continue;
|
|
534
|
+
if (!stat.isDirectory())
|
|
535
|
+
throw new Error(`Agent directory must be a directory: ${directory}`);
|
|
536
|
+
files = readdirSync(directory)
|
|
537
|
+
.filter((file) => file.endsWith(".md") || isAgentOverrideFile(file))
|
|
538
|
+
.sort();
|
|
539
|
+
}
|
|
540
|
+
catch (error) {
|
|
541
|
+
this.diagnostics.push(`${directory}: ${String(error)}`);
|
|
542
|
+
// An unreadable override layer cannot safely expose lower-precedence policies.
|
|
543
|
+
this.types.clear();
|
|
544
|
+
this.bases.clear();
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
if (source === "bundled") {
|
|
548
|
+
this.loadBundledLayer(directory, files);
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
this.applyScopeLayer(directory, source, files);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
/** Bundled layer: full `<name>.md` definitions only; overrides are rejected. */
|
|
555
|
+
loadBundledLayer(directory, files) {
|
|
556
|
+
const blocked = new Set();
|
|
557
|
+
const seenNames = new Set();
|
|
558
|
+
for (const file of files) {
|
|
559
|
+
const filePath = join(directory, file);
|
|
560
|
+
if (isAgentOverrideFile(file)) {
|
|
561
|
+
blocked.add(overrideBaseName(file));
|
|
562
|
+
this.diagnostics.push(`${filePath}: Settings overrides (<name>.yml) are not allowed in the bundled layer`);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
let content = "";
|
|
566
|
+
try {
|
|
567
|
+
if (!lstatSync(filePath).isFile())
|
|
568
|
+
throw new Error("Agent definition must be a regular file, not a symlink");
|
|
569
|
+
content = readFileSync(filePath, "utf8");
|
|
570
|
+
const type = parseAgentType(content, filePath);
|
|
571
|
+
if (seenNames.has(type.name)) {
|
|
572
|
+
blocked.add(type.name);
|
|
573
|
+
this.diagnostics.push(`${filePath}: Duplicate agent name in bundled layer: ${type.name}`);
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
seenNames.add(type.name);
|
|
577
|
+
this.types.set(type.name, { ...type, source: "bundled" });
|
|
578
|
+
}
|
|
579
|
+
catch (error) {
|
|
580
|
+
blocked.add(file.slice(0, -3));
|
|
581
|
+
// Extract declared names even if another field or duplicate key is malformed.
|
|
582
|
+
try {
|
|
583
|
+
const doc = parseDocument(frontmatter(content).yaml);
|
|
584
|
+
if (isMap(doc.contents))
|
|
585
|
+
for (const pair of doc.contents.items) {
|
|
586
|
+
if (isScalar(pair.key) &&
|
|
587
|
+
pair.key.value === "name" &&
|
|
588
|
+
isScalar(pair.value) &&
|
|
589
|
+
typeof pair.value.value === "string")
|
|
590
|
+
blocked.add(pair.value.value);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
catch {
|
|
594
|
+
/* Filename still provides a fail-closed tombstone. */
|
|
595
|
+
}
|
|
596
|
+
this.diagnostics.push(`${filePath}: ${String(error)}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
for (const name of blocked) {
|
|
600
|
+
this.types.delete(name);
|
|
601
|
+
this.bases.delete(name);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* User/project layer: `<name>.md` is a full fork (replaces the base),
|
|
606
|
+
* `<name>.yml` is a settings-only override merged on top of the base.
|
|
607
|
+
* Having both for one name, duplicate declarations, malformed files, or an
|
|
608
|
+
* override without a base fails closed for that agent name.
|
|
609
|
+
*/
|
|
610
|
+
applyScopeLayer(directory, scope, files) {
|
|
611
|
+
const blocked = new Set();
|
|
612
|
+
const forks = new Map();
|
|
613
|
+
const overrides = new Map();
|
|
614
|
+
const forkNames = new Set();
|
|
615
|
+
const declareBlocked = (name) => {
|
|
616
|
+
blocked.add(name);
|
|
617
|
+
};
|
|
618
|
+
for (const file of files) {
|
|
619
|
+
const filePath = join(directory, file);
|
|
620
|
+
const overrideName = overrideBaseName(file);
|
|
621
|
+
if (overrideName !== undefined) {
|
|
622
|
+
if (!NAME.test(overrideName)) {
|
|
623
|
+
declareBlocked(overrideName);
|
|
624
|
+
this.diagnostics.push(`${filePath}: Invalid agent name in override filename`);
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
let content = "";
|
|
628
|
+
try {
|
|
629
|
+
if (!lstatSync(filePath).isFile())
|
|
630
|
+
throw new Error("Agent settings override must be a regular file, not a symlink");
|
|
631
|
+
content = readFileSync(filePath, "utf8");
|
|
632
|
+
const override = parseAgentSettings(content, filePath, overrideName);
|
|
633
|
+
const key = override.name ?? overrideName;
|
|
634
|
+
if (forkNames.has(key)) {
|
|
635
|
+
declareBlocked(key);
|
|
636
|
+
this.diagnostics.push(`${filePath}: Use either ${key}.md (full fork) or ${key}.yml (settings override), not both`);
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
639
|
+
if (overrides.has(key)) {
|
|
640
|
+
declareBlocked(key);
|
|
641
|
+
this.diagnostics.push(`${filePath}: Duplicate agent override in ${scope} layer: ${key}`);
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
overrides.set(key, { override, file });
|
|
645
|
+
}
|
|
646
|
+
catch (error) {
|
|
647
|
+
declareBlocked(overrideName);
|
|
648
|
+
try {
|
|
649
|
+
const doc = parseDocument(content);
|
|
650
|
+
if (isMap(doc.contents))
|
|
651
|
+
for (const pair of doc.contents.items) {
|
|
652
|
+
if (isScalar(pair.key) &&
|
|
653
|
+
pair.key.value === "name" &&
|
|
654
|
+
isScalar(pair.value) &&
|
|
655
|
+
typeof pair.value.value === "string")
|
|
656
|
+
declareBlocked(pair.value.value);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
catch {
|
|
660
|
+
/* Filename still provides a fail-closed tombstone. */
|
|
661
|
+
}
|
|
662
|
+
this.diagnostics.push(`${filePath}: ${String(error)}`);
|
|
663
|
+
}
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
let content = "";
|
|
667
|
+
try {
|
|
668
|
+
if (!lstatSync(filePath).isFile())
|
|
669
|
+
throw new Error("Agent definition must be a regular file, not a symlink");
|
|
670
|
+
content = readFileSync(filePath, "utf8");
|
|
671
|
+
const type = parseAgentType(content, filePath);
|
|
672
|
+
if (overrides.has(type.name)) {
|
|
673
|
+
declareBlocked(type.name);
|
|
674
|
+
this.diagnostics.push(`${filePath}: Use either ${type.name}.md (full fork) or ${type.name}.yml (settings override), not both`);
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
if (forkNames.has(type.name)) {
|
|
678
|
+
declareBlocked(type.name);
|
|
679
|
+
this.diagnostics.push(`${filePath}: Duplicate agent name in ${scope} layer: ${type.name}`);
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
forkNames.add(type.name);
|
|
683
|
+
forks.set(type.name, { type, file });
|
|
684
|
+
}
|
|
685
|
+
catch (error) {
|
|
686
|
+
declareBlocked(file.slice(0, -3));
|
|
687
|
+
try {
|
|
688
|
+
const doc = parseDocument(frontmatter(content).yaml);
|
|
689
|
+
if (isMap(doc.contents))
|
|
690
|
+
for (const pair of doc.contents.items) {
|
|
691
|
+
if (isScalar(pair.key) &&
|
|
692
|
+
pair.key.value === "name" &&
|
|
693
|
+
isScalar(pair.value) &&
|
|
694
|
+
typeof pair.value.value === "string")
|
|
695
|
+
declareBlocked(pair.value.value);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
catch {
|
|
699
|
+
/* Filename still provides a fail-closed tombstone. */
|
|
700
|
+
}
|
|
701
|
+
this.diagnostics.push(`${filePath}: ${String(error)}`);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
for (const name of [...forks.keys()].filter((name) => overrides.has(name))) {
|
|
705
|
+
declareBlocked(name);
|
|
706
|
+
this.diagnostics.push(`${join(directory, forks.get(name).file)} and ${join(directory, overrides.get(name).file)}: ` +
|
|
707
|
+
`Use either ${name}.md (full fork) or ${name}.yml (settings override), not both`);
|
|
708
|
+
}
|
|
709
|
+
for (const [name, { type, file }] of forks) {
|
|
710
|
+
if (blocked.has(name))
|
|
711
|
+
continue;
|
|
712
|
+
const filePath = join(directory, file);
|
|
713
|
+
const base = this.types.get(name);
|
|
714
|
+
const fork = {
|
|
715
|
+
...type,
|
|
716
|
+
source: scope,
|
|
717
|
+
filePath,
|
|
718
|
+
customization: { kind: "fork", scope, filePath },
|
|
719
|
+
baseSource: base?.source,
|
|
720
|
+
baseFilePath: base?.filePath,
|
|
721
|
+
};
|
|
722
|
+
this.bases.set(name, base ? structuredClone(base) : structuredClone(fork));
|
|
723
|
+
this.types.set(name, fork);
|
|
724
|
+
}
|
|
725
|
+
for (const [name, { override, file }] of overrides) {
|
|
726
|
+
if (blocked.has(name))
|
|
727
|
+
continue;
|
|
728
|
+
const filePath = join(directory, file);
|
|
729
|
+
const base = this.types.get(name);
|
|
730
|
+
if (!base) {
|
|
731
|
+
declareBlocked(name);
|
|
732
|
+
this.diagnostics.push(`${filePath}: Settings override has no base agent named ${JSON.stringify(name)}; ` +
|
|
733
|
+
`create ${name}.md first or remove the override`);
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
try {
|
|
737
|
+
const merged = mergeAgentSettings(base, { ...override, name });
|
|
738
|
+
this.bases.set(name, structuredClone(base));
|
|
739
|
+
this.scopeOverrides.get(scope).set(name, structuredClone(override));
|
|
740
|
+
this.types.set(name, {
|
|
741
|
+
...merged,
|
|
742
|
+
name,
|
|
743
|
+
source: scope,
|
|
744
|
+
filePath,
|
|
745
|
+
customization: { kind: "override", scope, filePath },
|
|
746
|
+
baseSource: base.source,
|
|
747
|
+
baseFilePath: base.filePath,
|
|
748
|
+
});
|
|
749
|
+
}
|
|
750
|
+
catch (error) {
|
|
751
|
+
declareBlocked(name);
|
|
752
|
+
this.diagnostics.push(`${filePath}: ${String(error)}`);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
for (const name of blocked) {
|
|
756
|
+
this.types.delete(name);
|
|
757
|
+
this.bases.delete(name);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
list() {
|
|
761
|
+
return structuredClone([...this.types.values()].sort((a, b) => a.name.localeCompare(b.name)));
|
|
762
|
+
}
|
|
763
|
+
get(name) {
|
|
764
|
+
const type = this.types.get(name);
|
|
765
|
+
if (!type)
|
|
766
|
+
throw new Error(`Unknown or invalid agent type: ${name}`);
|
|
767
|
+
return structuredClone(type);
|
|
768
|
+
}
|
|
769
|
+
canSaveProject() {
|
|
770
|
+
return this.options.includeProject;
|
|
771
|
+
}
|
|
772
|
+
preferredScopeDirectory(scope) {
|
|
773
|
+
if (scope !== "user" && scope !== "project")
|
|
774
|
+
throw new Error("Invalid agent scope");
|
|
775
|
+
if (scope === "project" && !this.options.includeProject)
|
|
776
|
+
throw new Error("Project agents are not enabled/trusted");
|
|
777
|
+
return scope === "user"
|
|
778
|
+
? resolve(this.options.agentDir, SUBAGENT_MANAGER_DIR, AGENTS_DIR)
|
|
779
|
+
: resolve(this.options.cwd, ".pi", "agent", SUBAGENT_MANAGER_DIR, AGENTS_DIR);
|
|
780
|
+
}
|
|
781
|
+
// Manager-owned storage only. Do not scan legacy Pi agent packages.
|
|
782
|
+
scopeDirectories(scope) {
|
|
783
|
+
return [this.preferredScopeDirectory(scope)];
|
|
784
|
+
}
|
|
785
|
+
destinationBasePaths(scope, directory) {
|
|
786
|
+
if (scope === "user") {
|
|
787
|
+
const base = resolve(this.options.agentDir);
|
|
788
|
+
return [base, join(base, SUBAGENT_MANAGER_DIR), directory];
|
|
789
|
+
}
|
|
790
|
+
const base = resolve(this.options.cwd);
|
|
791
|
+
const projectPi = join(base, ".pi");
|
|
792
|
+
const projectAgent = join(projectPi, "agent");
|
|
793
|
+
return [base, projectPi, projectAgent, join(projectAgent, SUBAGENT_MANAGER_DIR), directory];
|
|
794
|
+
}
|
|
795
|
+
/** Base definition under the top customization, for diffing and read-only display. */
|
|
796
|
+
getBase(name) {
|
|
797
|
+
const base = this.bases.get(name);
|
|
798
|
+
return base ? structuredClone(base) : undefined;
|
|
799
|
+
}
|
|
800
|
+
/** Loaded definitions in cascade order, including lower-precedence scopes. */
|
|
801
|
+
getLayers(name) {
|
|
802
|
+
const current = this.get(name); // Never expose layers of an invalid/blocked agent.
|
|
803
|
+
const layers = new Map();
|
|
804
|
+
for (const type of [
|
|
805
|
+
this.scopeBases.get("user")?.get(name),
|
|
806
|
+
this.scopeBases.get("project")?.get(name),
|
|
807
|
+
current,
|
|
808
|
+
]) {
|
|
809
|
+
if (type)
|
|
810
|
+
layers.set(type.source, type);
|
|
811
|
+
}
|
|
812
|
+
return structuredClone([...layers.values()]);
|
|
813
|
+
}
|
|
814
|
+
/** Saved sparse keys, including explicit clears and pins equal to the base. */
|
|
815
|
+
getSettingsOverride(name, scope) {
|
|
816
|
+
const layer = this.getLayers(name).find((type) => type.source === scope);
|
|
817
|
+
if (layer?.customization?.kind !== "override")
|
|
818
|
+
return undefined;
|
|
819
|
+
const override = this.scopeOverrides.get(scope)?.get(name);
|
|
820
|
+
return override ? structuredClone(override) : undefined;
|
|
821
|
+
}
|
|
822
|
+
/** Definition below a specific save scope; never includes that scope or higher ones. */
|
|
823
|
+
getScopeBase(name, scope) {
|
|
824
|
+
const base = this.scopeBases.get(scope)?.get(name);
|
|
825
|
+
return base ? structuredClone(base) : undefined;
|
|
826
|
+
}
|
|
827
|
+
canPreserveDestination(name, scope, original, kind) {
|
|
828
|
+
if (original?.name !== name ||
|
|
829
|
+
original.source !== scope ||
|
|
830
|
+
typeof original.filePath !== "string" ||
|
|
831
|
+
dirname(resolve(original.filePath)) !== this.preferredScopeDirectory(scope))
|
|
832
|
+
return false;
|
|
833
|
+
const preserved = resolve(original.filePath);
|
|
834
|
+
if (kind === "fork")
|
|
835
|
+
return preserved.endsWith(".md");
|
|
836
|
+
return AGENT_OVERRIDE_EXTENSIONS.some((extension) => preserved.endsWith(extension));
|
|
837
|
+
}
|
|
838
|
+
destination(name, scope, original, kind = "fork") {
|
|
839
|
+
if (!NAME.test(name))
|
|
840
|
+
throw new Error("Unsafe agent name");
|
|
841
|
+
const directory = this.preferredScopeDirectory(scope);
|
|
842
|
+
if (this.canPreserveDestination(name, scope, original, kind) &&
|
|
843
|
+
typeof original?.filePath === "string") {
|
|
844
|
+
return validatePreservedDestination(original.filePath, directory, kind === "fork" ? [".md"] : [...AGENT_OVERRIDE_EXTENSIONS]);
|
|
845
|
+
}
|
|
846
|
+
return resolve(directory, `${name}${kind === "fork" ? ".md" : ".yml"}`);
|
|
847
|
+
}
|
|
848
|
+
writeContent(filePath, name, content) {
|
|
849
|
+
const directory = dirname(filePath);
|
|
850
|
+
mkdirSync(directory, { recursive: true });
|
|
851
|
+
const temporary = join(dirname(filePath), `.${name}.${randomUUID()}.tmp`);
|
|
852
|
+
try {
|
|
853
|
+
writeFileSync(temporary, content, { flag: "wx", mode: 0o600 });
|
|
854
|
+
renameSync(temporary, filePath);
|
|
855
|
+
}
|
|
856
|
+
finally {
|
|
857
|
+
if (lstatIfPresent(temporary))
|
|
858
|
+
unlinkSync(temporary);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
assertWritableDestination(filePath, scope) {
|
|
862
|
+
const directory = dirname(filePath);
|
|
863
|
+
// Reject redirected destination directories and files before writing.
|
|
864
|
+
for (const path of [...this.destinationBasePaths(scope, directory), filePath]) {
|
|
865
|
+
const stat = assertNotSymlinkPath(path);
|
|
866
|
+
if (path === filePath && stat && !stat.isFile())
|
|
867
|
+
throw new Error(`Unsafe agent destination: ${filePath}`);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
/** Save a full `<name>.md` fork. Replaces the base definition entirely. */
|
|
871
|
+
save(type, scope, original) {
|
|
872
|
+
const content = serializeAgentType(type);
|
|
873
|
+
const validated = parseAgentType(content);
|
|
874
|
+
const filePath = this.destination(validated.name, scope, original, "fork");
|
|
875
|
+
this.assertWritableDestination(filePath, scope);
|
|
876
|
+
this.writeContent(filePath, validated.name, content);
|
|
877
|
+
this.reload();
|
|
878
|
+
return this.get(validated.name);
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Save a settings-only `<name>.yml` override merged on top of the base.
|
|
882
|
+
* The prompt body always comes from the base; only changed settings are
|
|
883
|
+
* written so untouched fields keep tracking bundled updates. Existing pins
|
|
884
|
+
* are retained; `original` is the pre-edit snapshot (also used to avoid
|
|
885
|
+
* copying inherited settings into another scope or from a stale draft).
|
|
886
|
+
*/
|
|
887
|
+
saveOverride(name, draft, scope, original) {
|
|
888
|
+
if (!NAME.test(name))
|
|
889
|
+
throw new Error("Unsafe agent name");
|
|
890
|
+
if (draft.name !== name)
|
|
891
|
+
throw new Error("Settings overrides cannot rename the agent; fork it instead");
|
|
892
|
+
const base = this.getScopeBase(name, scope);
|
|
893
|
+
if (!base)
|
|
894
|
+
throw new Error(`Settings override has no base agent named ${JSON.stringify(name)}`);
|
|
895
|
+
const before = original ?? this.get(name);
|
|
896
|
+
const existing = this.scopeOverrides.get(scope)?.get(name) ?? {};
|
|
897
|
+
const override = before.customization?.kind === "fork"
|
|
898
|
+
? diffAgentSettings(base, draft)
|
|
899
|
+
: updateAgentSettings(base, before, draft, existing);
|
|
900
|
+
const content = serializeAgentSettings(override);
|
|
901
|
+
parseAgentSettings(content, undefined, name);
|
|
902
|
+
const filePath = this.destination(name, scope, original, "override");
|
|
903
|
+
this.assertWritableDestination(filePath, scope);
|
|
904
|
+
this.writeContent(filePath, name, content);
|
|
905
|
+
this.reload();
|
|
906
|
+
return this.get(name);
|
|
907
|
+
}
|
|
908
|
+
/** Remove a user/project customization so the base definition shows through. */
|
|
909
|
+
removeCustomization(name, scope) {
|
|
910
|
+
const current = this.types.get(name);
|
|
911
|
+
const customization = current?.customization;
|
|
912
|
+
if (!customization || customization.scope !== scope)
|
|
913
|
+
throw new Error(`No ${scope} customization found for ${JSON.stringify(name)}`);
|
|
914
|
+
const resolved = resolve(customization.filePath);
|
|
915
|
+
if (dirname(resolved) !== this.preferredScopeDirectory(scope) ||
|
|
916
|
+
(!resolved.endsWith(".md") &&
|
|
917
|
+
!AGENT_OVERRIDE_EXTENSIONS.some((extension) => resolved.endsWith(extension))))
|
|
918
|
+
throw new Error(`Unsafe agent destination: ${resolved}`);
|
|
919
|
+
const stat = lstatIfPresent(resolved);
|
|
920
|
+
if (!stat?.isFile())
|
|
921
|
+
throw new Error(`Unsafe agent destination: ${resolved}`);
|
|
922
|
+
unlinkSync(resolved);
|
|
923
|
+
this.reload();
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
//# sourceMappingURL=config.js.map
|