@restranger/repi-subagents 0.0.0-stage → 0.0.1-92401bd
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/README.md +284 -2
- package/package.json +41 -5
- package/src/agent-file.ts +297 -0
- package/src/agent-schema.ts +90 -0
- package/src/child-guard.ts +151 -0
- package/src/command.ts +255 -0
- package/src/default-agents.ts +409 -0
- package/src/discovery.ts +354 -0
- package/src/extensions.ts +119 -0
- package/src/fleet-ui.ts +617 -0
- package/src/format.ts +974 -0
- package/src/frontmatter.ts +302 -0
- package/src/index.ts +112 -0
- package/src/manager.ts +1192 -0
- package/src/model-resolver.ts +58 -0
- package/src/overrides.ts +693 -0
- package/src/plugin.ts +568 -0
- package/src/prompt.ts +184 -0
- package/src/registry.ts +71 -0
- package/src/session.ts +304 -0
- package/src/status-widget.ts +65 -0
- package/src/tool-policy.ts +77 -0
- package/src/tools.ts +554 -0
- package/src/types.ts +163 -0
package/src/overrides.ts
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
3
|
+
import { type PluginConfigLayer, resolveConfigLayers } from "@restranger/repi-lib";
|
|
4
|
+
import { OVERRIDE_ONLY_ALIASES, canonicalAgentField } from "./agent-schema.js";
|
|
5
|
+
import { parseListValue } from "./frontmatter.js";
|
|
6
|
+
import type { AgentDefinition, AgentDiagnostic, AgentPromptMode, AgentToolSpec } from "./types.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* JSON overrides for declarative Markdown agents.
|
|
10
|
+
*
|
|
11
|
+
* Discovery first resolves `*.md` files (project files already outrank user files). The JSON
|
|
12
|
+
* layers are then applied on top:
|
|
13
|
+
*
|
|
14
|
+
* user `~/.config/repi/subagents.json` → project `<cwd>/.repi/subagents.json`
|
|
15
|
+
*
|
|
16
|
+
* Project config wins over user config. `null` removes an override from a lower-priority config
|
|
17
|
+
* layer and falls back to the Markdown value. JSON config never creates agents that do not exist
|
|
18
|
+
* as a Markdown file; it only changes their values.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const CONFIG_NAME = "subagents.json";
|
|
22
|
+
const CONFIG_VERSION = 1;
|
|
23
|
+
const MAX_DESCRIPTION = 1024;
|
|
24
|
+
const THINKING_LEVELS = new Set(["off", "minimal", "low", "medium", "high", "xhigh"]);
|
|
25
|
+
const TOP_LEVEL_KEYS = new Set(["$schema", "version", "agents", "maxConcurrent", "max_concurrent"]);
|
|
26
|
+
|
|
27
|
+
export interface AgentOverrideContext {
|
|
28
|
+
readonly cwd: string;
|
|
29
|
+
/** Defaults to `os.homedir()`; injectable for tests. */
|
|
30
|
+
readonly homeDir?: string;
|
|
31
|
+
/** Defaults to `process.env`; `XDG_CONFIG_HOME` is honored. */
|
|
32
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
33
|
+
/** When false, the project `.repi/subagents.json` is not read. */
|
|
34
|
+
readonly workspaceTrusted?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type OverrideConfigLayer = PluginConfigLayer;
|
|
38
|
+
|
|
39
|
+
export interface AgentOverridePatch {
|
|
40
|
+
/** Canonical field → value. `null` means "remove the lower-priority override". */
|
|
41
|
+
readonly data: ReadonlyMap<string, unknown>;
|
|
42
|
+
/** File that contributed the last patch; used for diagnostics. */
|
|
43
|
+
readonly path: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface LoadedAgentOverrides {
|
|
47
|
+
readonly patches: ReadonlyMap<string, AgentOverridePatch>;
|
|
48
|
+
readonly configFiles: readonly string[];
|
|
49
|
+
readonly diagnostics: readonly AgentDiagnostic[];
|
|
50
|
+
readonly maxConcurrent?: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Resolve config paths from lowest to highest priority (`@restranger/repi-lib` owns the trust rule). */
|
|
54
|
+
export function resolveOverrideConfigPaths(options: AgentOverrideContext): OverrideConfigLayer[] {
|
|
55
|
+
return resolveConfigLayers({
|
|
56
|
+
cwd: options.cwd,
|
|
57
|
+
...(options.homeDir !== undefined ? { homeDir: options.homeDir } : {}),
|
|
58
|
+
...(options.env !== undefined ? { env: options.env } : {}),
|
|
59
|
+
...(options.workspaceTrusted !== undefined ? { workspaceTrusted: options.workspaceTrusted } : {}),
|
|
60
|
+
filename: CONFIG_NAME,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Read and merge the JSON override layers. Discovery calls this after scanning Markdown files. */
|
|
65
|
+
export function loadAgentOverrides(options: AgentOverrideContext): LoadedAgentOverrides {
|
|
66
|
+
const diagnostics: AgentDiagnostic[] = [];
|
|
67
|
+
const patches = new Map<string, AgentOverridePatch>();
|
|
68
|
+
const configFiles: string[] = [];
|
|
69
|
+
let maxConcurrent: number | undefined;
|
|
70
|
+
|
|
71
|
+
for (const layer of resolveOverrideConfigPaths(options)) {
|
|
72
|
+
if (!safeExists(layer.path)) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
configFiles.push(layer.path);
|
|
76
|
+
const root = readConfigFile(layer, diagnostics);
|
|
77
|
+
if (root === undefined) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const layerResult = parseOverrideLayer(root, layer, diagnostics);
|
|
81
|
+
if (!layerResult) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (layerResult.maxConcurrent !== undefined) {
|
|
85
|
+
maxConcurrent = layerResult.maxConcurrent;
|
|
86
|
+
}
|
|
87
|
+
for (const [name, patch] of layerResult.patches) {
|
|
88
|
+
const previous = patches.get(name);
|
|
89
|
+
const data = new Map(previous?.data ?? []);
|
|
90
|
+
for (const [key, value] of patch) {
|
|
91
|
+
if (value === null) {
|
|
92
|
+
data.delete(key);
|
|
93
|
+
} else {
|
|
94
|
+
data.set(key, value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
patches.set(name, { data, path: layer.path });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
patches,
|
|
103
|
+
configFiles,
|
|
104
|
+
diagnostics,
|
|
105
|
+
...(maxConcurrent !== undefined ? { maxConcurrent } : {}),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Apply merged overrides to discovered agents.
|
|
111
|
+
*
|
|
112
|
+
* Returns a new map. Agents disabled by `enabled: false` are omitted. Invalid values are reported
|
|
113
|
+
* as diagnostics and leave the corresponding Markdown value untouched.
|
|
114
|
+
*/
|
|
115
|
+
export function applyAgentOverrides(
|
|
116
|
+
agents: ReadonlyMap<string, AgentDefinition>,
|
|
117
|
+
overrides: LoadedAgentOverrides,
|
|
118
|
+
): { readonly agents: Map<string, AgentDefinition>; readonly diagnostics: readonly AgentDiagnostic[] } {
|
|
119
|
+
const result = new Map(agents);
|
|
120
|
+
const diagnostics: AgentDiagnostic[] = [];
|
|
121
|
+
|
|
122
|
+
for (const [key, patch] of overrides.patches) {
|
|
123
|
+
if (patch.data.size === 0) {
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const agent = result.get(key);
|
|
127
|
+
if (!agent) {
|
|
128
|
+
diagnostics.push({
|
|
129
|
+
path: patch.path,
|
|
130
|
+
level: "warning",
|
|
131
|
+
message: `Override for unknown agent "${key}" ignored; no agent declaration exists.`,
|
|
132
|
+
});
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
const next = applyPatch(agent, patch, diagnostics);
|
|
136
|
+
if (next === undefined) {
|
|
137
|
+
result.delete(key);
|
|
138
|
+
} else {
|
|
139
|
+
result.set(key, next);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return { agents: result, diagnostics };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function readConfigFile(layer: OverrideConfigLayer, diagnostics: AgentDiagnostic[]): unknown | undefined {
|
|
147
|
+
let raw: string;
|
|
148
|
+
try {
|
|
149
|
+
raw = readFileSync(layer.path, "utf8");
|
|
150
|
+
} catch (err) {
|
|
151
|
+
diagnostics.push({
|
|
152
|
+
path: layer.path,
|
|
153
|
+
level: "error",
|
|
154
|
+
message: `Cannot read subagent override config: ${errorMessage(err)}`,
|
|
155
|
+
});
|
|
156
|
+
return undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
return JSON.parse(raw.replace(/^\uFEFF/, "")) as unknown;
|
|
161
|
+
} catch (err) {
|
|
162
|
+
diagnostics.push({
|
|
163
|
+
path: layer.path,
|
|
164
|
+
level: "error",
|
|
165
|
+
message: `Cannot parse subagent override config: ${errorMessage(err)}`,
|
|
166
|
+
});
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
interface ParsedOverrideLayer {
|
|
172
|
+
readonly patches: Map<string, Map<string, unknown>>;
|
|
173
|
+
readonly maxConcurrent?: number;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function parseOverrideLayer(
|
|
177
|
+
root: unknown,
|
|
178
|
+
layer: OverrideConfigLayer,
|
|
179
|
+
diagnostics: AgentDiagnostic[],
|
|
180
|
+
): ParsedOverrideLayer | undefined {
|
|
181
|
+
if (!isRecord(root)) {
|
|
182
|
+
diagnostics.push({
|
|
183
|
+
path: layer.path,
|
|
184
|
+
level: "error",
|
|
185
|
+
message: `Invalid ${layer.source} subagent override config: expected a JSON object.`,
|
|
186
|
+
});
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const key of Object.keys(root)) {
|
|
191
|
+
if (!TOP_LEVEL_KEYS.has(key)) {
|
|
192
|
+
diagnostics.push({
|
|
193
|
+
path: layer.path,
|
|
194
|
+
level: "error",
|
|
195
|
+
message: `Unknown top-level subagent override key "${key}"; ignored.`,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const version = root.version;
|
|
201
|
+
if (version !== undefined && version !== CONFIG_VERSION) {
|
|
202
|
+
diagnostics.push({
|
|
203
|
+
path: layer.path,
|
|
204
|
+
level: "error",
|
|
205
|
+
message: `Unsupported subagent override config version ${JSON.stringify(version)}; expected ${CONFIG_VERSION}. This layer is ignored.`,
|
|
206
|
+
});
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let maxConcurrent: number | undefined;
|
|
211
|
+
const rawMaxConcurrent = root.maxConcurrent ?? root.max_concurrent;
|
|
212
|
+
if (rawMaxConcurrent !== undefined) {
|
|
213
|
+
if (typeof rawMaxConcurrent === "number" && Number.isInteger(rawMaxConcurrent) && rawMaxConcurrent >= 1) {
|
|
214
|
+
maxConcurrent = rawMaxConcurrent;
|
|
215
|
+
} else {
|
|
216
|
+
diagnostics.push({
|
|
217
|
+
path: layer.path,
|
|
218
|
+
level: "error",
|
|
219
|
+
message: `Invalid "maxConcurrent" in subagent override config: expected a positive integer, got ${JSON.stringify(rawMaxConcurrent)}.`,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const rawAgents = root.agents;
|
|
225
|
+
if (rawAgents === undefined) {
|
|
226
|
+
return { patches: new Map(), ...(maxConcurrent !== undefined ? { maxConcurrent } : {}) };
|
|
227
|
+
}
|
|
228
|
+
if (!isRecord(rawAgents)) {
|
|
229
|
+
diagnostics.push({
|
|
230
|
+
path: layer.path,
|
|
231
|
+
level: "error",
|
|
232
|
+
message: 'Invalid "agents" section in subagent override config: expected an object.',
|
|
233
|
+
});
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const result = new Map<string, Map<string, unknown>>();
|
|
238
|
+
for (const [rawName, rawPatch] of Object.entries(rawAgents)) {
|
|
239
|
+
const name = rawName.trim();
|
|
240
|
+
if (name.length === 0) {
|
|
241
|
+
diagnostics.push({
|
|
242
|
+
path: layer.path,
|
|
243
|
+
level: "error",
|
|
244
|
+
message: "Ignored subagent override with an empty agent name.",
|
|
245
|
+
});
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const key = name.toLowerCase();
|
|
249
|
+
if (result.has(key)) {
|
|
250
|
+
diagnostics.push({
|
|
251
|
+
path: layer.path,
|
|
252
|
+
level: "warning",
|
|
253
|
+
message: `Duplicate subagent override for "${name}" (names are case-insensitive); the last entry wins.`,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (!isRecord(rawPatch)) {
|
|
257
|
+
diagnostics.push({
|
|
258
|
+
path: layer.path,
|
|
259
|
+
level: "error",
|
|
260
|
+
message: `Invalid override for agent "${name}": expected an object.`,
|
|
261
|
+
});
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const patch = new Map<string, unknown>();
|
|
266
|
+
for (const [rawKey, value] of Object.entries(rawPatch)) {
|
|
267
|
+
const canonical = canonicalKey(rawKey);
|
|
268
|
+
if (!canonical) {
|
|
269
|
+
diagnostics.push({
|
|
270
|
+
path: layer.path,
|
|
271
|
+
level: "error",
|
|
272
|
+
message: `Unknown override field "${rawKey}" for agent "${name}"; ignored.`,
|
|
273
|
+
});
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (patch.has(canonical)) {
|
|
277
|
+
diagnostics.push({
|
|
278
|
+
path: layer.path,
|
|
279
|
+
level: "warning",
|
|
280
|
+
message: `Duplicate override field "${rawKey}" for agent "${name}"; the last value wins.`,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
patch.set(canonical, value);
|
|
284
|
+
}
|
|
285
|
+
result.set(key, patch);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
patches: result,
|
|
290
|
+
...(maxConcurrent !== undefined ? { maxConcurrent } : {}),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Canonical field for an override key; `systemPrompt` is override-only (the body in Markdown). */
|
|
295
|
+
function canonicalKey(key: string): string | undefined {
|
|
296
|
+
return canonicalAgentField(key) ?? (key === "systemPrompt" ? "systemPrompt" : OVERRIDE_ONLY_ALIASES[key]);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function applyPatch(
|
|
300
|
+
agent: AgentDefinition,
|
|
301
|
+
patch: AgentOverridePatch,
|
|
302
|
+
diagnostics: AgentDiagnostic[],
|
|
303
|
+
): AgentDefinition | undefined {
|
|
304
|
+
const path = patch.path;
|
|
305
|
+
const name = agent.name;
|
|
306
|
+
|
|
307
|
+
if (patch.data.has("enabled")) {
|
|
308
|
+
const enabled = patch.data.get("enabled");
|
|
309
|
+
if (typeof enabled !== "boolean") {
|
|
310
|
+
diagnostics.push({
|
|
311
|
+
path,
|
|
312
|
+
level: "error",
|
|
313
|
+
message: `Override "enabled" for agent "${name}" must be a boolean; ignored.`,
|
|
314
|
+
});
|
|
315
|
+
} else if (!enabled) {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
let description = agent.description;
|
|
321
|
+
if (patch.data.has("description")) {
|
|
322
|
+
const value = patch.data.get("description");
|
|
323
|
+
if (typeof value !== "string") {
|
|
324
|
+
diagnostics.push({
|
|
325
|
+
path,
|
|
326
|
+
level: "error",
|
|
327
|
+
message: `Override "description" for agent "${name}" must be a string; ignored.`,
|
|
328
|
+
});
|
|
329
|
+
} else {
|
|
330
|
+
const normalized = value.replace(/\s+/g, " ").trim();
|
|
331
|
+
if (normalized.length === 0) {
|
|
332
|
+
diagnostics.push({
|
|
333
|
+
path,
|
|
334
|
+
level: "error",
|
|
335
|
+
message: `Override "description" for agent "${name}" is empty; ignored.`,
|
|
336
|
+
});
|
|
337
|
+
} else {
|
|
338
|
+
description = normalized;
|
|
339
|
+
if (description.length > MAX_DESCRIPTION) {
|
|
340
|
+
description = `${description.slice(0, MAX_DESCRIPTION - 1).trimEnd()}…`;
|
|
341
|
+
diagnostics.push({
|
|
342
|
+
path,
|
|
343
|
+
level: "warning",
|
|
344
|
+
message: `Override "description" for agent "${name}" is longer than ${MAX_DESCRIPTION} characters; truncated.`,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
let tools = agent.tools;
|
|
352
|
+
if (patch.data.has("tools")) {
|
|
353
|
+
const parsed = parseToolsOverride(patch.data.get("tools"));
|
|
354
|
+
if (!parsed.ok) {
|
|
355
|
+
diagnostics.push({
|
|
356
|
+
path,
|
|
357
|
+
level: "error",
|
|
358
|
+
message: `Override "tools" for agent "${name}" must be "*", "none", "inherit", a comma-separated string, or an array of strings; ignored.`,
|
|
359
|
+
});
|
|
360
|
+
} else {
|
|
361
|
+
tools = parsed.value;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
let disallowedTools = agent.disallowedTools;
|
|
366
|
+
if (patch.data.has("disallowedTools")) {
|
|
367
|
+
const parsed = parseListOverride(patch.data.get("disallowedTools"));
|
|
368
|
+
if (!parsed.ok) {
|
|
369
|
+
diagnostics.push({
|
|
370
|
+
path,
|
|
371
|
+
level: "error",
|
|
372
|
+
message: `Override "disallowedTools" for agent "${name}" must be a comma-separated string or an array of strings; ignored.`,
|
|
373
|
+
});
|
|
374
|
+
} else {
|
|
375
|
+
disallowedTools = parsed.value;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
let model = agent.model;
|
|
380
|
+
if (patch.data.has("model")) {
|
|
381
|
+
const value = patch.data.get("model");
|
|
382
|
+
if (typeof value !== "string") {
|
|
383
|
+
diagnostics.push({
|
|
384
|
+
path,
|
|
385
|
+
level: "error",
|
|
386
|
+
message: `Override "model" for agent "${name}" must be a string; ignored.`,
|
|
387
|
+
});
|
|
388
|
+
} else {
|
|
389
|
+
model = normalizeModel(value);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
let fallbackModels = agent.fallbackModels;
|
|
394
|
+
if (patch.data.has("fallbackModels")) {
|
|
395
|
+
const raw = patch.data.get("fallbackModels");
|
|
396
|
+
const parsed = parseListOverride(raw);
|
|
397
|
+
if (!parsed.ok) {
|
|
398
|
+
diagnostics.push({
|
|
399
|
+
path,
|
|
400
|
+
level: "error",
|
|
401
|
+
message: `Override "fallbackModels" for agent "${name}" must be a comma-separated string or an array of strings; ignored.`,
|
|
402
|
+
});
|
|
403
|
+
} else {
|
|
404
|
+
fallbackModels = parsed.value.length > 0 ? parsed.value : undefined;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
let thinking = agent.thinking;
|
|
409
|
+
if (patch.data.has("thinking")) {
|
|
410
|
+
const value = patch.data.get("thinking");
|
|
411
|
+
if (typeof value !== "string") {
|
|
412
|
+
diagnostics.push({
|
|
413
|
+
path,
|
|
414
|
+
level: "error",
|
|
415
|
+
message: `Override "thinking" for agent "${name}" must be a string; ignored.`,
|
|
416
|
+
});
|
|
417
|
+
} else {
|
|
418
|
+
const normalized = value.trim().toLowerCase();
|
|
419
|
+
if (normalized === "inherit" || normalized === "default") {
|
|
420
|
+
thinking = undefined;
|
|
421
|
+
} else {
|
|
422
|
+
const level = normalized === "none" ? "off" : normalized;
|
|
423
|
+
if (!THINKING_LEVELS.has(level)) {
|
|
424
|
+
diagnostics.push({
|
|
425
|
+
path,
|
|
426
|
+
level: "error",
|
|
427
|
+
message: `Override "thinking" for agent "${name}" has unknown level "${value}"; ignored.`,
|
|
428
|
+
});
|
|
429
|
+
} else {
|
|
430
|
+
thinking = level;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let maxTurns = agent.maxTurns;
|
|
437
|
+
if (patch.data.has("maxTurns")) {
|
|
438
|
+
const value = patch.data.get("maxTurns");
|
|
439
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
|
|
440
|
+
diagnostics.push({
|
|
441
|
+
path,
|
|
442
|
+
level: "error",
|
|
443
|
+
message: `Override "maxTurns" for agent "${name}" must be a non-negative integer; ignored.`,
|
|
444
|
+
});
|
|
445
|
+
} else if (value === 0) {
|
|
446
|
+
maxTurns = undefined;
|
|
447
|
+
} else if (value < 2) {
|
|
448
|
+
maxTurns = 2;
|
|
449
|
+
diagnostics.push({
|
|
450
|
+
path,
|
|
451
|
+
level: "warning",
|
|
452
|
+
message: `Override "maxTurns" for agent "${name}" is below 2; raised to 2.`,
|
|
453
|
+
});
|
|
454
|
+
} else {
|
|
455
|
+
maxTurns = value;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
let background = agent.background;
|
|
460
|
+
if (patch.data.has("background")) {
|
|
461
|
+
const value = patch.data.get("background");
|
|
462
|
+
if (typeof value !== "boolean") {
|
|
463
|
+
diagnostics.push({
|
|
464
|
+
path,
|
|
465
|
+
level: "error",
|
|
466
|
+
message: `Override "background" for agent "${name}" must be a boolean; ignored.`,
|
|
467
|
+
});
|
|
468
|
+
} else {
|
|
469
|
+
background = value;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
let promptMode = agent.promptMode;
|
|
474
|
+
if (patch.data.has("promptMode")) {
|
|
475
|
+
const value = patch.data.get("promptMode");
|
|
476
|
+
if (typeof value !== "string") {
|
|
477
|
+
diagnostics.push({
|
|
478
|
+
path,
|
|
479
|
+
level: "error",
|
|
480
|
+
message: `Override "promptMode" for agent "${name}" must be "append" or "replace"; ignored.`,
|
|
481
|
+
});
|
|
482
|
+
} else {
|
|
483
|
+
const normalized = value.trim().toLowerCase();
|
|
484
|
+
if (normalized !== "append" && normalized !== "replace") {
|
|
485
|
+
diagnostics.push({
|
|
486
|
+
path,
|
|
487
|
+
level: "error",
|
|
488
|
+
message: `Override "promptMode" for agent "${name}" must be "append" or "replace"; ignored.`,
|
|
489
|
+
});
|
|
490
|
+
} else {
|
|
491
|
+
promptMode = normalized as AgentPromptMode;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
let color = agent.color;
|
|
497
|
+
if (patch.data.has("color")) {
|
|
498
|
+
const value = patch.data.get("color");
|
|
499
|
+
if (typeof value !== "string") {
|
|
500
|
+
diagnostics.push({
|
|
501
|
+
path,
|
|
502
|
+
level: "error",
|
|
503
|
+
message: `Override "color" for agent "${name}" must be a string; ignored.`,
|
|
504
|
+
});
|
|
505
|
+
} else {
|
|
506
|
+
color = value.trim() || undefined;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
let systemPrompt = agent.systemPrompt;
|
|
511
|
+
if (patch.data.has("systemPrompt")) {
|
|
512
|
+
const value = patch.data.get("systemPrompt");
|
|
513
|
+
if (typeof value !== "string") {
|
|
514
|
+
diagnostics.push({
|
|
515
|
+
path,
|
|
516
|
+
level: "error",
|
|
517
|
+
message: `Override "systemPrompt" for agent "${name}" must be a string; ignored.`,
|
|
518
|
+
});
|
|
519
|
+
} else {
|
|
520
|
+
systemPrompt = value.trim();
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
const {
|
|
525
|
+
model: _model,
|
|
526
|
+
fallbackModels: _fallbackModels,
|
|
527
|
+
thinking: _thinking,
|
|
528
|
+
maxTurns: _maxTurns,
|
|
529
|
+
background: _background,
|
|
530
|
+
color: _color,
|
|
531
|
+
...base
|
|
532
|
+
} = agent;
|
|
533
|
+
return {
|
|
534
|
+
...base,
|
|
535
|
+
description,
|
|
536
|
+
tools,
|
|
537
|
+
disallowedTools,
|
|
538
|
+
...(model ? { model } : {}),
|
|
539
|
+
...(fallbackModels ? { fallbackModels } : {}),
|
|
540
|
+
...(thinking ? { thinking } : {}),
|
|
541
|
+
...(maxTurns !== undefined ? { maxTurns } : {}),
|
|
542
|
+
...(background !== undefined ? { background } : {}),
|
|
543
|
+
promptMode,
|
|
544
|
+
...(color ? { color } : {}),
|
|
545
|
+
systemPrompt,
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function parseToolsOverride(
|
|
550
|
+
value: unknown,
|
|
551
|
+
): { readonly ok: true; readonly value: AgentToolSpec } | { readonly ok: false } {
|
|
552
|
+
if (typeof value === "string") {
|
|
553
|
+
const trimmed = value.trim();
|
|
554
|
+
const lower = trimmed.toLowerCase();
|
|
555
|
+
if (trimmed === "*") {
|
|
556
|
+
return { ok: true, value: "*" };
|
|
557
|
+
}
|
|
558
|
+
if (lower === "inherit" || lower === "default") {
|
|
559
|
+
return { ok: true, value: undefined };
|
|
560
|
+
}
|
|
561
|
+
if (trimmed.length === 0 || lower === "none") {
|
|
562
|
+
return { ok: true, value: [] };
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const parsed = parseListOverride(value);
|
|
566
|
+
if (!parsed.ok) {
|
|
567
|
+
return parsed;
|
|
568
|
+
}
|
|
569
|
+
return { ok: true, value: parsed.value.includes("*") ? "*" : parsed.value };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function parseListOverride(
|
|
573
|
+
value: unknown,
|
|
574
|
+
): { readonly ok: true; readonly value: readonly string[] } | { readonly ok: false } {
|
|
575
|
+
if (typeof value === "string") {
|
|
576
|
+
return { ok: true, value: parseListValue(value) ?? [] };
|
|
577
|
+
}
|
|
578
|
+
if (Array.isArray(value) && value.every((entry) => typeof entry === "string")) {
|
|
579
|
+
return { ok: true, value: parseListValue(value) ?? [] };
|
|
580
|
+
}
|
|
581
|
+
return { ok: false };
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function normalizeModel(value: string): string | undefined {
|
|
585
|
+
const trimmed = value.trim();
|
|
586
|
+
if (!trimmed || trimmed.toLowerCase() === "inherit" || trimmed.toLowerCase() === "default") {
|
|
587
|
+
return undefined;
|
|
588
|
+
}
|
|
589
|
+
return trimmed;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function safeExists(path: string): boolean {
|
|
593
|
+
try {
|
|
594
|
+
return existsSync(path);
|
|
595
|
+
} catch {
|
|
596
|
+
return false;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
export interface SaveAgentOverridesOptions {
|
|
601
|
+
/** `false` refuses to write into the project. */
|
|
602
|
+
readonly workspaceTrusted?: boolean;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
export function resolveProjectSubagentsConfigPath(cwd: string): string {
|
|
606
|
+
const directJson = join(resolve(cwd), ".pi", "extensions", "repi-subagents.json");
|
|
607
|
+
const extDir = join(resolve(cwd), ".pi", "extensions", "repi-subagents");
|
|
608
|
+
const inFolderConfig = join(extDir, "config.json");
|
|
609
|
+
const namedInExt = join(extDir, CONFIG_NAME);
|
|
610
|
+
const legacy = join(resolve(cwd), ".repi", CONFIG_NAME);
|
|
611
|
+
if (existsSync(directJson)) return directJson;
|
|
612
|
+
if (existsSync(inFolderConfig)) return inFolderConfig;
|
|
613
|
+
if (existsSync(namedInExt)) return namedInExt;
|
|
614
|
+
if (existsSync(legacy)) return legacy;
|
|
615
|
+
return directJson;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Persist `fallbackModels` for agents into the project override file.
|
|
620
|
+
*
|
|
621
|
+
* Saves to `.pi/extensions/repi-subagents.json` (or existing `.pi/extensions/repi-subagents/config.json`).
|
|
622
|
+
* `@restranger/repi-subagents` owns this file; another plugin must change it through
|
|
623
|
+
* `re:subagents@1` (`setAgentFallbackModels`), never by rewriting the file itself. The write is
|
|
624
|
+
* atomic, and an unreadable existing file surfaces as an error instead of being silently replaced.
|
|
625
|
+
*/
|
|
626
|
+
export function saveAgentFallbackModels(
|
|
627
|
+
cwd: string,
|
|
628
|
+
updates: ReadonlyArray<{ readonly name: string; readonly fallbackModels: readonly string[] }>,
|
|
629
|
+
options: SaveAgentOverridesOptions = {},
|
|
630
|
+
): string {
|
|
631
|
+
if (options.workspaceTrusted === false) {
|
|
632
|
+
throw new Error("Refusing to write project subagent config: the workspace is not trusted.");
|
|
633
|
+
}
|
|
634
|
+
const filePath = resolveProjectSubagentsConfigPath(cwd);
|
|
635
|
+
const existing = readJsonObject(filePath);
|
|
636
|
+
const agents = isRecord(existing.agents) ? existing.agents : {};
|
|
637
|
+
const updated: Record<string, unknown> = { ...agents };
|
|
638
|
+
for (const update of updates) {
|
|
639
|
+
const previous = updated[update.name];
|
|
640
|
+
updated[update.name] = {
|
|
641
|
+
...(isRecord(previous) ? previous : {}),
|
|
642
|
+
fallbackModels: [...update.fallbackModels],
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
writeJsonAtomic(filePath, { ...existing, version: CONFIG_VERSION, agents: updated });
|
|
646
|
+
return filePath;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function readJsonObject(filePath: string): Record<string, unknown> {
|
|
650
|
+
let raw: string;
|
|
651
|
+
try {
|
|
652
|
+
raw = readFileSync(filePath, "utf8");
|
|
653
|
+
} catch (err) {
|
|
654
|
+
if ((err as NodeJS.ErrnoException).code === "ENOENT") {
|
|
655
|
+
return {};
|
|
656
|
+
}
|
|
657
|
+
throw new Error(`Cannot read ${filePath}: ${errorMessage(err)}`);
|
|
658
|
+
}
|
|
659
|
+
let parsed: unknown;
|
|
660
|
+
try {
|
|
661
|
+
parsed = JSON.parse(raw.replace(/^\uFEFF/, ""));
|
|
662
|
+
} catch (err) {
|
|
663
|
+
throw new Error(`Cannot parse ${filePath}: ${errorMessage(err)}. Fix or delete the file, then save again.`);
|
|
664
|
+
}
|
|
665
|
+
if (!isRecord(parsed)) {
|
|
666
|
+
throw new Error(`Cannot use ${filePath}: expected a JSON object.`);
|
|
667
|
+
}
|
|
668
|
+
return parsed;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function writeJsonAtomic(filePath: string, value: unknown): void {
|
|
672
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
673
|
+
const temp = join(dirname(filePath), `.${basename(filePath)}.${process.pid}.${Date.now()}.tmp`);
|
|
674
|
+
try {
|
|
675
|
+
writeFileSync(temp, `${JSON.stringify(value, null, "\t")}\n`, { encoding: "utf8", mode: 0o600 });
|
|
676
|
+
renameSync(temp, filePath);
|
|
677
|
+
} catch (err) {
|
|
678
|
+
try {
|
|
679
|
+
rmSync(temp, { force: true });
|
|
680
|
+
} catch {
|
|
681
|
+
// Best effort: the temp file may never have been created.
|
|
682
|
+
}
|
|
683
|
+
throw err;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
688
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
function errorMessage(error: unknown): string {
|
|
692
|
+
return error instanceof Error ? error.message : String(error);
|
|
693
|
+
}
|