Repository navigation
Expand file tree
/
Copy pathinit.js
More file actions
177 lines (156 loc) · 5.54 KB
/
Copy pathinit.js
File metadata and controls
177 lines (156 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { mkdir, copyFile, readFile, writeFile, stat } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
async function exists(p) {
try {
await stat(p);
return true;
} catch {
return false;
}
}
const VALID_PATTERNS = new Set([
'team-agent-registry',
'shared-inbox-hitl',
'hierarchical-delegation',
'agent-clone-fork',
'fleet-budget-guard',
'cross-agent-audit',
]);
const VALID_LOOPS = new Set([
'daily-triage',
'pr-babysitter',
'issue-triage',
'changelog-drafter',
'dependency-sweeper',
'post-merge-cleanup',
]);
const PATTERN_EXTRAS = {
'shared-inbox-hitl': [
['templates/inbox-runbook.md', 'inbox-runbook.md'],
],
'hierarchical-delegation': [
['templates/handoff-schema.json', 'agents/handoff-schema.json'],
],
'agent-clone-fork': [
['templates/fork-policy.md', 'fork-policy.md'],
],
'cross-agent-audit': [
['templates/audit-runbook.md', 'audit-runbook.md'],
],
};
const PATTERN_FLEET_FLAGS = {
'team-agent-registry': ['Team Agent Registry'],
'shared-inbox-hitl': ['Team Agent Registry', 'Shared Inbox HITL'],
'fleet-budget-guard': ['Team Agent Registry', 'Fleet Budget Guard'],
'hierarchical-delegation': ['Team Agent Registry', 'Hierarchical Delegation'],
'agent-clone-fork': ['Team Agent Registry', 'Agent Clone & Fork'],
'cross-agent-audit': ['Team Agent Registry', 'Cross-Agent Audit'],
};
async function ensureDir(p) {
await mkdir(p, { recursive: true });
}
async function copyTemplate(srcRoot, destRoot, srcRel, destRel) {
await ensureDir(path.dirname(path.join(destRoot, destRel)));
await copyFile(path.join(srcRoot, srcRel), path.join(destRoot, destRel));
}
function applyPlaceholders(content, vars) {
let out = content;
for (const [key, value] of Object.entries(vars)) {
out = out.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value);
}
return out;
}
function patchFleetMd(content, pattern) {
const flags = PATTERN_FLEET_FLAGS[pattern] || ['Team Agent Registry'];
let out = content;
for (const flag of flags) {
out = out.replace(`- [ ] ${flag}`, `- [x] ${flag}`);
}
return out;
}
function patchManifestLoops(content, loopPattern) {
if (!loopPattern) return content;
if (/^loops:/m.test(content)) {
return content.replace(/^loops:\s*\n(?:\s+-\s+\S+\n?)+/m, `loops:\n - ${loopPattern}\n`);
}
return content.replace(/^status:/m, `loops:\n - ${loopPattern}\n\nstatus:`);
}
async function resolveSrcRoot(options) {
if (options.srcRoot) return options.srcRoot;
const pkgDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)));
const bundledFleet = path.join(pkgDir, 'starters/minimal-fleet/FLEET.md');
if (await exists(bundledFleet)) return pkgDir;
return path.resolve(pkgDir, '../..');
}
export async function initFleet(target, options = {}) {
const dest = path.resolve(target);
const src = await resolveSrcRoot(options);
const pattern = options.pattern || 'team-agent-registry';
const loopPattern = options.withLoop || null;
const loopTool = options.tool || 'grok';
const date = new Date().toISOString().slice(0, 10);
const team = options.team || 'your-team';
if (!VALID_PATTERNS.has(pattern)) {
throw new Error(`Unknown pattern: ${pattern}. Valid: ${[...VALID_PATTERNS].join(', ')}`);
}
if (loopPattern && !VALID_LOOPS.has(loopPattern)) {
throw new Error(`Unknown loop: ${loopPattern}. Valid: ${[...VALID_LOOPS].join(', ')}`);
}
await ensureDir(dest);
const created = [];
const placeholders = {
TEAM_NAME: team,
DATE: date,
PATTERN: pattern,
LOOP_PATTERN: loopPattern || 'daily-triage',
LOOP_TOOL: loopTool,
};
const copies = [
['starters/minimal-fleet/FLEET.md', 'FLEET.md'],
['templates/FLEET-STATE.md', 'FLEET-STATE.md'],
['templates/fleet-budget.md', 'fleet-budget.md'],
['templates/permissions-model.yaml', 'permissions-model.yaml'],
['templates/AGENT-MANIFEST.yaml', 'agents/manifests/example-agent.yaml'],
['starters/minimal-fleet/.github/workflows/fleet-audit.yml', '.github/workflows/fleet-audit.yml'],
...(PATTERN_EXTRAS[pattern] || []),
];
if (loopPattern) {
copies.push(['templates/LOOP.md', 'LOOP.md']);
}
for (const [from, to] of copies) {
const destPath = path.join(dest, to);
try {
await copyTemplate(src, dest, from, to);
if (/\.(md|ya?ml)$/i.test(to)) {
let content = await readFile(destPath, 'utf8');
content = applyPlaceholders(content, placeholders);
if (to === 'FLEET.md') content = patchFleetMd(content, pattern);
if (to === 'agents/manifests/example-agent.yaml' && loopPattern) {
content = patchManifestLoops(content, loopPattern);
}
await writeFile(destPath, content);
}
created.push(to);
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
throw new Error(`Failed to scaffold ${to} from ${from}: ${msg}`);
}
}
const registry = `# Agent registry — ${date}
# Pattern: ${pattern}${loopPattern ? ` + loop: ${loopPattern}` : ''}
agents:
- id: example-agent
manifest: agents/manifests/example-agent.yaml
owner: ${team}
status: active
pattern: ${pattern}
`;
await ensureDir(path.join(dest, 'agents/manifests'));
await writeFile(path.join(dest, 'agents/registry.yaml'), registry);
created.push('agents/registry.yaml');
const next = loopPattern
? 'npx @cobusgreyling/loop-init . --pattern ' + loopPattern + ' --tool ' + loopTool
: 'npx @cobusgreyling/fleet-audit <path> --suggest';
return { dest, pattern, loopPattern, loopTool, created, next };
}