@restranger/repi-subagents 0.0.0-stage → 0.0.1-ca9b82c

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.
@@ -0,0 +1,302 @@
1
+ /**
2
+ * Minimal, dependency-free frontmatter reader.
3
+ *
4
+ * Agent declarations only need flat scalars, flow sequences and block sequences, so this keeps
5
+ * the package standalone (no YAML dependency) while still accepting the shapes Claude Code and
6
+ * Pi agent files use in practice. Anything the reader cannot represent is reported as a
7
+ * diagnostic by `parseAgentFile`, never guessed.
8
+ */
9
+
10
+ export type FrontmatterValue = string | string[];
11
+
12
+ export interface FrontmatterIssue {
13
+ readonly level: "warning" | "error";
14
+ readonly message: string;
15
+ }
16
+
17
+ export interface FrontmatterResult {
18
+ readonly data: Record<string, FrontmatterValue>;
19
+ readonly body: string;
20
+ readonly issues: readonly FrontmatterIssue[];
21
+ }
22
+
23
+ const OPENING = /^---\s*$/;
24
+ const CLOSING = /^(?:---|\.\.\.)\s*$/;
25
+ const KEY_LINE = /^([A-Za-z0-9][A-Za-z0-9_.-]*)\s*:\s*(.*)$/;
26
+ const LIST_ITEM = /^\s*-\s*(.*)$/;
27
+
28
+ /** Parse a Markdown file with an optional `---` frontmatter block. */
29
+ export function parseFrontmatter(source: string): FrontmatterResult {
30
+ const text = source.replace(/^\uFEFF/, "").replace(/\r\n?/g, "\n");
31
+ const lines = text.split("\n");
32
+ if (lines.length === 0 || !OPENING.test(lines[0] ?? "")) {
33
+ return { data: Object.create(null), body: text.trim(), issues: [] };
34
+ }
35
+
36
+ let closing = -1;
37
+ for (let index = 1; index < lines.length; index++) {
38
+ if (CLOSING.test(lines[index] ?? "")) {
39
+ closing = index;
40
+ break;
41
+ }
42
+ }
43
+ if (closing === -1) {
44
+ return {
45
+ data: Object.create(null),
46
+ body: "",
47
+ issues: [{ level: "error", message: "Frontmatter starts with `---` but has no closing delimiter." }],
48
+ };
49
+ }
50
+
51
+ const data: Record<string, FrontmatterValue> = Object.create(null);
52
+ const issues: FrontmatterIssue[] = [];
53
+ const block = lines.slice(1, closing);
54
+ let index = 0;
55
+
56
+ while (index < block.length) {
57
+ const raw = block[index] ?? "";
58
+ const line = raw.replace(/\s+$/, "");
59
+ index++;
60
+
61
+ if (line.trim().length === 0 || line.trimStart().startsWith("#")) {
62
+ continue;
63
+ }
64
+ if (/^\s/.test(line)) {
65
+ issues.push({ level: "warning", message: `Ignored indented frontmatter line: ${line.trim()}` });
66
+ continue;
67
+ }
68
+
69
+ const match = KEY_LINE.exec(line);
70
+ if (!match) {
71
+ issues.push({ level: "warning", message: `Ignored malformed frontmatter line: ${line.trim()}` });
72
+ continue;
73
+ }
74
+ const key = match[1] ?? "";
75
+ const inline = (match[2] ?? "").trim();
76
+
77
+ if (Object.hasOwn(data, key)) {
78
+ issues.push({ level: "warning", message: `Duplicate frontmatter field \`${key}\`; the last value wins.` });
79
+ }
80
+
81
+ const blockScalar = /^([|>])([+-]?)$/.exec(inline);
82
+ if (blockScalar) {
83
+ const folded = blockScalar[1] === ">";
84
+ const chomp = blockScalar[2];
85
+ const collected: string[] = [];
86
+ let indentation: number | undefined;
87
+ while (index < block.length) {
88
+ const next = block[index] ?? "";
89
+ if (next.trim().length > 0 && !/^\s/.test(next)) {
90
+ break;
91
+ }
92
+ index++;
93
+ if (next.trim().length === 0) {
94
+ collected.push("");
95
+ continue;
96
+ }
97
+ const width = (next.match(/^\s*/) ?? [""])[0].length;
98
+ indentation = indentation === undefined ? width : Math.min(indentation, width);
99
+ collected.push(next);
100
+ }
101
+ const dedented = collected.map((entry) =>
102
+ indentation === undefined ? entry : entry.slice(Math.min(indentation, entry.length)),
103
+ );
104
+ let value = folded ? foldLines(dedented) : dedented.join("\n");
105
+ if (chomp === "-") {
106
+ value = value.replace(/\n+$/, "");
107
+ }
108
+ data[key] = value;
109
+ continue;
110
+ }
111
+
112
+ if (inline.length > 0) {
113
+ data[key] = parseScalar(inline);
114
+ continue;
115
+ }
116
+
117
+ // `key:` followed by ` - item` lines is a block sequence. A nested mapping (for
118
+ // example another tool's `permission:` block) is opaque here: skip it once instead of
119
+ // reporting every indented line. The consumer reports unsupported keys once.
120
+ const first = nextMeaningfulLine(block, index);
121
+ if (first !== undefined && /^\s/.test(first.line) && !LIST_ITEM.test(first.line)) {
122
+ index = first.index;
123
+ while (index < block.length) {
124
+ const next = block[index] ?? "";
125
+ if (next.trim().length > 0 && !/^\s/.test(next)) {
126
+ break;
127
+ }
128
+ index++;
129
+ }
130
+ issues.push({ level: "warning", message: `Ignored nested block under \`${key}\`.` });
131
+ data[key] = "";
132
+ continue;
133
+ }
134
+
135
+ const items: string[] = [];
136
+ while (index < block.length) {
137
+ const next = block[index] ?? "";
138
+ if (next.trim().length === 0 || next.trimStart().startsWith("#")) {
139
+ index++;
140
+ continue;
141
+ }
142
+ if (!/^\s/.test(next)) {
143
+ break;
144
+ }
145
+ const itemMatch = LIST_ITEM.exec(next);
146
+ index++;
147
+ if (!itemMatch) {
148
+ issues.push({ level: "warning", message: `Ignored non-list line under \`${key}\`: ${next.trim()}` });
149
+ continue;
150
+ }
151
+ const item = unquote((itemMatch[1] ?? "").trim());
152
+ if (item.length > 0) {
153
+ items.push(item);
154
+ }
155
+ }
156
+ data[key] = items.length > 0 ? items : "";
157
+ }
158
+
159
+ return {
160
+ data,
161
+ body: lines
162
+ .slice(closing + 1)
163
+ .join("\n")
164
+ .trim(),
165
+ issues,
166
+ };
167
+ }
168
+
169
+ /** Split a comma-separated scalar or an inline `[a, b]` flow sequence. */
170
+ export function parseListValue(value: FrontmatterValue | undefined): string[] | undefined {
171
+ if (value === undefined) {
172
+ return undefined;
173
+ }
174
+ if (Array.isArray(value)) {
175
+ const items = value.map((entry) => entry.trim()).filter((entry) => entry.length > 0);
176
+ return items.length === 1 && items[0] === "none" ? [] : items;
177
+ }
178
+ const trimmed = value.trim();
179
+ if (trimmed.length === 0 || trimmed === "none") {
180
+ return [];
181
+ }
182
+ const inner = trimmed.startsWith("[") && trimmed.endsWith("]") ? trimmed.slice(1, -1) : trimmed;
183
+ const parts = splitCommaList(inner)
184
+ .map((entry) => unquote(entry.trim()))
185
+ .filter((entry) => entry.length > 0);
186
+ if (parts.length === 1 && /\s/.test(parts[0] ?? "")) {
187
+ return (parts[0] ?? "").split(/\s+/).filter(Boolean);
188
+ }
189
+ return parts;
190
+ }
191
+
192
+ /** Parse a YAML-ish boolean. Returns `undefined` for unrecognized values. */
193
+ export function parseBooleanValue(value: FrontmatterValue | undefined): boolean | undefined {
194
+ if (value === undefined || Array.isArray(value)) {
195
+ return undefined;
196
+ }
197
+ const normalized = value.trim().toLowerCase();
198
+ if (["true", "yes", "on", "1"].includes(normalized)) {
199
+ return true;
200
+ }
201
+ if (["false", "no", "off", "0"].includes(normalized)) {
202
+ return false;
203
+ }
204
+ return undefined;
205
+ }
206
+
207
+ /** Parse a non-negative integer. Returns `undefined` when the value is not an integer. */
208
+ export function parseIntegerValue(value: FrontmatterValue | undefined): number | undefined {
209
+ if (value === undefined || Array.isArray(value)) {
210
+ return undefined;
211
+ }
212
+ const trimmed = value.trim();
213
+ if (!/^\d+$/.test(trimmed)) {
214
+ return undefined;
215
+ }
216
+ const parsed = Number.parseInt(trimmed, 10);
217
+ return Number.isSafeInteger(parsed) ? parsed : undefined;
218
+ }
219
+
220
+ function parseScalar(value: string): string {
221
+ const trimmed = value.trim();
222
+ if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
223
+ return trimmed;
224
+ }
225
+ return unquote(trimmed);
226
+ }
227
+
228
+ function unquote(value: string): string {
229
+ if (value.length < 2) {
230
+ return value;
231
+ }
232
+ const first = value[0];
233
+ const last = value[value.length - 1];
234
+ if ((first === '"' && last === '"') || (first === "'" && last === "'")) {
235
+ const inner = value.slice(1, -1);
236
+ return first === '"' ? inner.replace(/\\"/g, '"').replace(/\\n/g, "\n") : inner.replace(/''/g, "'");
237
+ }
238
+ return value;
239
+ }
240
+
241
+ function nextMeaningfulLine(
242
+ lines: readonly string[],
243
+ from: number,
244
+ ): { readonly index: number; readonly line: string } | undefined {
245
+ for (let index = from; index < lines.length; index++) {
246
+ const line = lines[index] ?? "";
247
+ if (line.trim().length === 0 || line.trimStart().startsWith("#")) {
248
+ continue;
249
+ }
250
+ return { index, line };
251
+ }
252
+ return undefined;
253
+ }
254
+
255
+ function splitCommaList(value: string): string[] {
256
+ const result: string[] = [];
257
+ let current = "";
258
+ let quote: string | undefined;
259
+ for (const character of value) {
260
+ if (quote) {
261
+ current += character;
262
+ if (character === quote) {
263
+ quote = undefined;
264
+ }
265
+ continue;
266
+ }
267
+ if (character === '"' || character === "'") {
268
+ quote = character;
269
+ current += character;
270
+ continue;
271
+ }
272
+ if (character === ",") {
273
+ result.push(current);
274
+ current = "";
275
+ continue;
276
+ }
277
+ current += character;
278
+ }
279
+ if (current.length > 0 || result.length > 0) {
280
+ result.push(current);
281
+ }
282
+ return result;
283
+ }
284
+
285
+ function foldLines(lines: readonly string[]): string {
286
+ const paragraphs: string[] = [];
287
+ let current: string[] = [];
288
+ for (const line of lines) {
289
+ if (line.trim().length === 0) {
290
+ if (current.length > 0) {
291
+ paragraphs.push(current.join(" "));
292
+ current = [];
293
+ }
294
+ continue;
295
+ }
296
+ current.push(line.trim());
297
+ }
298
+ if (current.length > 0) {
299
+ paragraphs.push(current.join(" "));
300
+ }
301
+ return paragraphs.join("\n");
302
+ }
package/src/index.ts ADDED
@@ -0,0 +1,112 @@
1
+ export { parseAgentFile, type ParsedAgentFile } from "./agent-file.js";
2
+ export {
3
+ guardToolCall,
4
+ createSubagentGuardExtension,
5
+ type GuardResult,
6
+ type SubagentGuardOptions,
7
+ } from "./child-guard.js";
8
+ export {
9
+ resolveChildExtensions,
10
+ type ChildBuiltinExtension,
11
+ type ChildExtensionSources,
12
+ } from "./extensions.js";
13
+ export { FleetView, type FleetViewOptions } from "./fleet-ui.js";
14
+ export { FleetStatusWidget, type FleetStatusWidgetOptions } from "./status-widget.js";
15
+ export { DEFAULT_AGENTS } from "./default-agents.js";
16
+ export {
17
+ discoverAgents,
18
+ resolveAgentDirectories,
19
+ type AgentDirectory,
20
+ type DiscoverAgentsOptions,
21
+ } from "./discovery.js";
22
+ export {
23
+ SPINNER_FRAMES,
24
+ displayWidth,
25
+ fleetAgentIdentityColor,
26
+ FLEET_AGENT_IDENTITY_COLORS,
27
+ formatActivity,
28
+ formatAgentCatalog,
29
+ formatDiagnostics,
30
+ formatDuration,
31
+ formatFleetDashboard,
32
+ formatFleetElapsed,
33
+ formatFleetInspector,
34
+ formatFleetInspectorLines,
35
+ formatFleetTokens,
36
+ formatRun,
37
+ formatRunList,
38
+ formatRunProgress,
39
+ formatRuns,
40
+ formatStatusWidget,
41
+ formatTokens,
42
+ paintIdentity,
43
+ sanitize,
44
+ truncate,
45
+ widgetLineBudget,
46
+ } from "./format.js";
47
+ export { parseBooleanValue, parseFrontmatter, parseIntegerValue, parseListValue } from "./frontmatter.js";
48
+ export { SubagentManager, UnknownAgentError, type SubagentManagerOptions } from "./manager.js";
49
+ export {
50
+ applyAgentOverrides,
51
+ loadAgentOverrides,
52
+ resolveOverrideConfigPaths,
53
+ type AgentOverrideContext,
54
+ type AgentOverridePatch,
55
+ type LoadedAgentOverrides,
56
+ type OverrideConfigLayer,
57
+ } from "./overrides.js";
58
+ export { resolveModel, type ModelRegistryLike } from "./model-resolver.js";
59
+ export {
60
+ createSubagentsCommandHandler,
61
+ registerSubagentsCommand,
62
+ type SubagentsCommandOptions,
63
+ type SubagentsCommandSession,
64
+ type SubagentsCommandSessionState,
65
+ } from "./command.js";
66
+ export {
67
+ createSubagentsPlugin,
68
+ createSubagentsRuntime,
69
+ resolveMaxConcurrent,
70
+ type ResolveMaxConcurrentOptions,
71
+ type SubagentsPluginOptions,
72
+ type SubagentsRuntime,
73
+ } from "./plugin.js";
74
+ export {
75
+ ACTIVE_RUNS_SECTION,
76
+ AGENT_SECTION,
77
+ formatActiveRunsPromptSection,
78
+ formatAgentPromptSection,
79
+ formatPromptDuration,
80
+ SUBAGENT_GUIDELINES,
81
+ } from "./prompt.js";
82
+ export { AgentRegistry } from "./registry.js";
83
+ export {
84
+ createChildSessionFactory,
85
+ type ChildSession,
86
+ type ChildSessionFactory,
87
+ type CreateChildSessionOptions,
88
+ } from "./session.js";
89
+ export {
90
+ DEFAULT_CHILD_TOOLS,
91
+ SUBAGENT_TOOL_NAMES,
92
+ describeToolSpec,
93
+ resolveToolAllowlist,
94
+ type ResolvedToolAllowlist,
95
+ } from "./tool-policy.js";
96
+ export { isSubagentSnapshot } from "./types.js";
97
+ export type {
98
+ AgentDefinition,
99
+ AgentDiagnostic,
100
+ AgentDiagnosticLevel,
101
+ AgentPromptMode,
102
+ AgentScanResult,
103
+ AgentSource,
104
+ AgentToolSpec,
105
+ ModelResolution,
106
+ SubagentActivity,
107
+ SubagentRunSnapshot,
108
+ SubagentRunStatus,
109
+ SubagentUsage,
110
+ SpawnSubagentRequest,
111
+ } from "./types.js";
112
+ export { default } from "./plugin.js";