@nanocollective/roster 0.1.0-beta.13 → 0.1.0-beta.15
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/dist/agents-NMGVORU3.js +232 -0
- package/dist/chunk-E3EVPK4I.js +338 -0
- package/dist/chunk-QCKEGMXD.js +562 -0
- package/dist/cli.js +1731 -830
- package/dist/compose-56JIIIHA.js +15 -0
- package/dist/connectors-ZVEPXSAR.js +23 -0
- package/dist/worker/assets/assets/css/connectors.css +50 -0
- package/dist/worker/assets/assets/js/api.js +3 -0
- package/dist/worker/assets/assets/js/app.js +4 -1
- package/dist/worker/assets/assets/js/icons.js +2 -0
- package/dist/worker/assets/assets/js/state.js +11 -1
- package/dist/worker/assets/assets/js/views/connectors.js +290 -0
- package/dist/worker/assets/index.html +4 -0
- package/dist/worker/index.js +2786 -1028
- package/docs/README.md +1 -0
- package/docs/architecture.md +1 -1
- package/docs/commands.md +37 -1
- package/docs/connectors.md +157 -0
- package/docs/cost.md +1 -1
- package/docs/developing.md +2 -2
- package/docs/doctor-codes.md +6 -2
- package/docs/export.md +2 -2
- package/docs/extending.md +1 -1
- package/docs/hosting.md +14 -1
- package/docs/memory.md +1 -1
- package/docs/org-yaml.md +33 -1
- package/docs/portal.md +18 -1
- package/docs/prompts.md +3 -2
- package/docs/security.md +17 -1
- package/docs/session-workflow.md +23 -5
- package/docs/staff-yaml.md +1 -1
- package/docs/troubleshooting.md +1 -1
- package/docs/upgrading.md +1 -1
- package/docs/writing-a-charter.md +1 -1
- package/package.json +2 -1
- package/templates/brain/.github/workflows/%%STAFF%%-daily.yaml +8 -6
- package/templates/brain/.github/workflows/%%STAFF%%-mention.yaml +8 -6
- package/templates/ops/.github/workflows/session.yaml +52 -11
- package/templates/ops/agents.mjs +38 -11
- package/templates/ops/compose.mjs +20 -2
- package/templates/ops/connectors.mjs +722 -0
- package/templates/ops/prompts/_identity.md +20 -0
- package/templates/portal/css/connectors.css +50 -0
- package/templates/portal/index.html +4 -0
- package/templates/portal/js/api.js +3 -0
- package/templates/portal/js/app.js +4 -1
- package/templates/portal/js/icons.js +2 -0
- package/templates/portal/js/state.js +11 -1
- package/templates/portal/js/views/connectors.js +290 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
parseYaml
|
|
4
|
+
} from "./chunk-E3EVPK4I.js";
|
|
5
|
+
|
|
6
|
+
// templates/ops/agents.mjs
|
|
7
|
+
import { readFileSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
var CLAUDE_TOOLS = {
|
|
10
|
+
full: '--allowedTools "Bash,Read,Write,Edit,Glob,Grep,WebFetch,WebSearch"',
|
|
11
|
+
workspace: '--allowedTools "Bash,Read,Write,Edit,Glob,Grep"',
|
|
12
|
+
"read-only": '--allowedTools "Read,Glob,Grep,WebFetch,WebSearch"'
|
|
13
|
+
};
|
|
14
|
+
function shellArg(v) {
|
|
15
|
+
return `'${String(v).replace(/'/g, "'\\''")}'`;
|
|
16
|
+
}
|
|
17
|
+
var PRESETS = {
|
|
18
|
+
// The reference runner, and the default. Uses Anthropic's own action, which handles tool
|
|
19
|
+
// permissions and output for us.
|
|
20
|
+
"claude-code-action": {
|
|
21
|
+
kind: "action",
|
|
22
|
+
token_env: "CLAUDE_CODE_OAUTH_TOKEN",
|
|
23
|
+
model: "claude-opus-5-5",
|
|
24
|
+
permissions: CLAUDE_TOOLS,
|
|
25
|
+
option: (k, v) => `--${k} ${shellArg(v)}`
|
|
26
|
+
},
|
|
27
|
+
// The same agent through its plain CLI, for anyone who would rather not depend on the action.
|
|
28
|
+
claude: {
|
|
29
|
+
kind: "cli",
|
|
30
|
+
install: "npm install -g @anthropic-ai/claude-code",
|
|
31
|
+
// JSON rather than text so the run record can read turns and cost off it. The log still
|
|
32
|
+
// carries the answer, inside the `result` field.
|
|
33
|
+
run: 'claude -p --model "$AGENT_MODEL" $AGENT_FLAGS --output-format json < "$AGENT_PROMPT_FILE" | tee "$AGENT_RESULT_FILE"',
|
|
34
|
+
token_env: "CLAUDE_CODE_OAUTH_TOKEN",
|
|
35
|
+
model: "claude-opus-5-5",
|
|
36
|
+
permissions: CLAUDE_TOOLS,
|
|
37
|
+
option: (k, v) => `--${k} ${shellArg(v)}`
|
|
38
|
+
},
|
|
39
|
+
codex: {
|
|
40
|
+
kind: "cli",
|
|
41
|
+
install: "npm install -g @openai/codex",
|
|
42
|
+
// `exec -` reads the prompt from stdin. The sandbox has to be opened up because the whole
|
|
43
|
+
// point of a session is that it edits the checkout and pushes.
|
|
44
|
+
run: 'codex exec - --model "$AGENT_MODEL" $AGENT_FLAGS < "$AGENT_PROMPT_FILE"',
|
|
45
|
+
token_env: "CODEX_API_KEY",
|
|
46
|
+
model: "gpt-5-codex",
|
|
47
|
+
/* Codex spells freedom as a sandbox plus an approval policy, and both have to be said:
|
|
48
|
+
a sandbox that allows writes still stops to ask by default, and a run that stops to ask
|
|
49
|
+
at 07:00 is a run that times out having done nothing. */
|
|
50
|
+
permissions: {
|
|
51
|
+
full: '--sandbox danger-full-access -c approval_policy="never"',
|
|
52
|
+
workspace: '--sandbox workspace-write -c approval_policy="never"',
|
|
53
|
+
"read-only": '--sandbox read-only -c approval_policy="never"'
|
|
54
|
+
},
|
|
55
|
+
// `-c key=value` is its highest-precedence override, so anything else goes through it.
|
|
56
|
+
option: (k, v) => `-c ${k}=${shellArg(JSON.stringify(v))}`
|
|
57
|
+
},
|
|
58
|
+
nanocoder: {
|
|
59
|
+
kind: "cli",
|
|
60
|
+
install: "npm install -g @nanocollective/nanocoder",
|
|
61
|
+
/* `run` is its non-interactive mode; --trust-directory skips the first-run prompt that
|
|
62
|
+
would otherwise hang a runner, and --plain avoids the TUI. The prompt is an argument
|
|
63
|
+
here rather than stdin, so it is read out of the file.
|
|
64
|
+
|
|
65
|
+
NANOCODER_PROVIDERS_FILE is the part that makes it work unattended. Nanocoder is a
|
|
66
|
+
client, not a model: it reads its providers from `agents.config.json` found in the
|
|
67
|
+
working directory. In a session that directory is the workspace root — the place the
|
|
68
|
+
repos are checked out *into* — which belongs to no repo, so a committed config would
|
|
69
|
+
never be found. Pointing at the ops repo's copy gives every staff member the same
|
|
70
|
+
providers from a file that is version controlled. A missing file is ignored, so this is
|
|
71
|
+
safe when somebody has configured it another way. */
|
|
72
|
+
run: 'NANOCODER_PROVIDERS_FILE="${NANOCODER_PROVIDERS_FILE:-roster-ops/agents.config.json}" nanocoder --model "$AGENT_MODEL" $AGENT_FLAGS --trust-directory --plain run "$(cat "$AGENT_PROMPT_FILE")"',
|
|
73
|
+
token_env: "NANOCODER_API_KEY",
|
|
74
|
+
model: "",
|
|
75
|
+
/* Its development modes. `plan` is genuinely read-only: it reasons and proposes and edits
|
|
76
|
+
nothing, which is the right answer for a staff member you are not ready to trust yet. */
|
|
77
|
+
permissions: {
|
|
78
|
+
full: "--mode yolo",
|
|
79
|
+
workspace: "--mode auto-accept",
|
|
80
|
+
"read-only": "--mode plan"
|
|
81
|
+
},
|
|
82
|
+
option: (k, v) => `--${k} ${shellArg(v)}`,
|
|
83
|
+
/* A client rather than a model, so it cannot run until it has been told whose model to
|
|
84
|
+
call. Written on init and reported by doctor when it is missing, because the failure
|
|
85
|
+
without it is a run that installs, starts, finds no provider and exits. */
|
|
86
|
+
config: {
|
|
87
|
+
path: "agents.config.json",
|
|
88
|
+
contents: {
|
|
89
|
+
nanocoder: {
|
|
90
|
+
providers: [
|
|
91
|
+
{
|
|
92
|
+
name: "openrouter",
|
|
93
|
+
baseUrl: "https://openrouter.ai/api/v1",
|
|
94
|
+
apiKey: "${NANOCODER_API_KEY}",
|
|
95
|
+
models: ["FILL IN: a model this provider serves, and set it as `model` in org.yaml"]
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
function resolveAgent(org, staff = {}, extra = {}) {
|
|
104
|
+
const asked = staff.agent ?? org.agent ?? {};
|
|
105
|
+
const spec = typeof asked === "string" ? { id: asked } : asked;
|
|
106
|
+
const id = spec.id ?? "claude-code-action";
|
|
107
|
+
const preset = PRESETS[id];
|
|
108
|
+
if (!preset && !(spec.install && spec.run)) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`unknown agent "${id}", and no install/run given.
|
|
111
|
+
Known: ${Object.keys(PRESETS).join(", ")}
|
|
112
|
+
Or describe your own in org.yaml:
|
|
113
|
+
agent:
|
|
114
|
+
id: ${id}
|
|
115
|
+
install: <shell>
|
|
116
|
+
run: <shell>
|
|
117
|
+
token_env: <VAR>`
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
const merged = { kind: "cli", ...preset ?? {}, ...strip(spec) };
|
|
121
|
+
if (merged.kind === "cli" && !merged.run) throw new Error(`agent "${id}" has no run command`);
|
|
122
|
+
if (!merged.token_env) throw new Error(`agent "${id}" does not say which env var carries its credential`);
|
|
123
|
+
return {
|
|
124
|
+
id,
|
|
125
|
+
kind: merged.kind,
|
|
126
|
+
install: merged.install ?? "",
|
|
127
|
+
run: merged.run ?? "",
|
|
128
|
+
token_env: merged.token_env,
|
|
129
|
+
// The staff member's own model wins; then the agent's default. Empty means "the agent's".
|
|
130
|
+
model: staff.model ?? merged.model ?? "",
|
|
131
|
+
flags: flagsFor(merged, spec, org, staff, extra),
|
|
132
|
+
config: merged.config ?? null
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
var LEVELS = ["read-only", "workspace", "full"];
|
|
136
|
+
function flagsFor(merged, spec, org, staff, extra = {}) {
|
|
137
|
+
const out = [];
|
|
138
|
+
const asked = permissionOf(org, staff);
|
|
139
|
+
const tools = staff.allowed_tools ?? org.defaults?.allowed_tools;
|
|
140
|
+
const table = merged.permissions;
|
|
141
|
+
const more = (extra.tools ?? []).filter(Boolean);
|
|
142
|
+
if (table === CLAUDE_TOOLS) {
|
|
143
|
+
const base = tools ? (Array.isArray(tools) ? tools.join(",") : String(tools)).replace(/\s+/g, "") : CLAUDE_TOOLS[asked].match(/"([^"]*)"/)[1];
|
|
144
|
+
out.push(`--allowedTools "${[base, ...more].filter(Boolean).join(",")}"`);
|
|
145
|
+
} else if (table) {
|
|
146
|
+
out.push(table[asked]);
|
|
147
|
+
}
|
|
148
|
+
if (extra.flags) out.push(extra.flags);
|
|
149
|
+
const options = { ...spec.options ?? {}, ...staff.options ?? {} };
|
|
150
|
+
const speller = merged.option;
|
|
151
|
+
for (const [k, v] of Object.entries(options)) {
|
|
152
|
+
if (v === void 0 || v === null || v === "") continue;
|
|
153
|
+
if (!speller) {
|
|
154
|
+
throw new Error(
|
|
155
|
+
`agent "${merged.id ?? "custom"}" has options but no way to spell them.
|
|
156
|
+
A custom agent takes its options in its own \`run\` command.`
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
out.push(speller(k, v));
|
|
160
|
+
}
|
|
161
|
+
return out.filter(Boolean).join(" ");
|
|
162
|
+
}
|
|
163
|
+
function permissionOf(org, staff = {}) {
|
|
164
|
+
const agent = staff.agent ?? org.agent;
|
|
165
|
+
const spec = agent && typeof agent === "object" ? agent : {};
|
|
166
|
+
const level = staff.permissions ?? spec.permissions ?? org.permissions ?? "full";
|
|
167
|
+
if (!LEVELS.includes(level)) {
|
|
168
|
+
throw new Error(`unknown permissions "${level}". One of: ${LEVELS.join(", ")}`);
|
|
169
|
+
}
|
|
170
|
+
return level;
|
|
171
|
+
}
|
|
172
|
+
function strip(o) {
|
|
173
|
+
const out = {};
|
|
174
|
+
for (const [k, v] of Object.entries(o)) if (v !== void 0 && v !== null && v !== "") out[k] = v;
|
|
175
|
+
delete out.id;
|
|
176
|
+
delete out.permissions;
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
180
|
+
const args = process.argv.slice(2);
|
|
181
|
+
const at = (flag) => {
|
|
182
|
+
const i = args.indexOf(flag);
|
|
183
|
+
return i === -1 ? void 0 : args[i + 1];
|
|
184
|
+
};
|
|
185
|
+
const opsDir = at("--ops") ?? "roster-ops";
|
|
186
|
+
const brainsDir = at("--brains") ?? ".";
|
|
187
|
+
const handle = at("--staff");
|
|
188
|
+
const org = parseYaml(readFileSync(join(opsDir, "org.yaml"), "utf8"), "org.yaml");
|
|
189
|
+
const entry = (org.staff ?? []).find((s) => s.handle === handle);
|
|
190
|
+
if (!entry) {
|
|
191
|
+
process.stderr.write(`agents: no staff member "${handle}" in org.yaml
|
|
192
|
+
`);
|
|
193
|
+
process.exit(2);
|
|
194
|
+
}
|
|
195
|
+
let manifest = {};
|
|
196
|
+
try {
|
|
197
|
+
const dir = entry.dir ?? entry.handle;
|
|
198
|
+
manifest = parseYaml(readFileSync(join(brainsDir, dir, "staff.yaml"), "utf8"), "staff.yaml");
|
|
199
|
+
} catch {
|
|
200
|
+
}
|
|
201
|
+
let wiring = {};
|
|
202
|
+
if (process.env.ROSTER_CONNECTORS) {
|
|
203
|
+
try {
|
|
204
|
+
wiring = JSON.parse(readFileSync(process.env.ROSTER_CONNECTORS, "utf8"));
|
|
205
|
+
} catch {
|
|
206
|
+
process.stderr.write("agents: the connectors' wiring could not be read, so none are used\n");
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const agent = resolveAgent(org, manifest, wiring);
|
|
210
|
+
const out = [
|
|
211
|
+
`kind=${agent.kind}`,
|
|
212
|
+
`id=${agent.id}`,
|
|
213
|
+
`token_env=${agent.token_env}`,
|
|
214
|
+
`model=${agent.model}`,
|
|
215
|
+
`install<<AGENT_EOF_9c1f
|
|
216
|
+
${agent.install}
|
|
217
|
+
AGENT_EOF_9c1f`,
|
|
218
|
+
`run<<AGENT_EOF_9c1f
|
|
219
|
+
${agent.run}
|
|
220
|
+
AGENT_EOF_9c1f`,
|
|
221
|
+
`flags<<AGENT_EOF_9c1f
|
|
222
|
+
${agent.flags}
|
|
223
|
+
AGENT_EOF_9c1f`
|
|
224
|
+
].join("\n");
|
|
225
|
+
process.stdout.write(out + "\n");
|
|
226
|
+
}
|
|
227
|
+
export {
|
|
228
|
+
LEVELS,
|
|
229
|
+
PRESETS,
|
|
230
|
+
permissionOf,
|
|
231
|
+
resolveAgent
|
|
232
|
+
};
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// templates/ops/compose.mjs
|
|
2
|
+
import * as nodeFs from "fs";
|
|
3
|
+
import { join, dirname, resolve } from "path";
|
|
4
|
+
import { fileURLToPath } from "url";
|
|
5
|
+
var { connectorsOf } = await import("./connectors-ZVEPXSAR.js").catch(() => ({ connectorsOf: () => [] }));
|
|
6
|
+
function parseYaml(text, file = "<yaml>") {
|
|
7
|
+
const lines = [];
|
|
8
|
+
text.split("\n").forEach((raw, i) => {
|
|
9
|
+
const noComment = stripComment(raw);
|
|
10
|
+
if (!noComment.trim()) return;
|
|
11
|
+
if (noComment.trim() === "---") return;
|
|
12
|
+
lines.push({ n: i + 1, indent: raw.match(/^ */)[0].length, text: noComment.trimEnd() });
|
|
13
|
+
});
|
|
14
|
+
let pos = 0;
|
|
15
|
+
const fail = (msg, line) => {
|
|
16
|
+
throw new Error(`${file}:${line?.n ?? "?"} ${msg}`);
|
|
17
|
+
};
|
|
18
|
+
function parseBlock(indent) {
|
|
19
|
+
const first = lines[pos];
|
|
20
|
+
if (!first) return null;
|
|
21
|
+
if (first.text.trim().startsWith("- ")) return parseList(indent);
|
|
22
|
+
return parseMap(indent);
|
|
23
|
+
}
|
|
24
|
+
function parseMap(indent) {
|
|
25
|
+
const out = {};
|
|
26
|
+
while (pos < lines.length) {
|
|
27
|
+
const line = lines[pos];
|
|
28
|
+
if (line.indent < indent) break;
|
|
29
|
+
if (line.indent > indent) fail("unexpected indentation", line);
|
|
30
|
+
const body = line.text.trim();
|
|
31
|
+
if (body.startsWith("- ")) break;
|
|
32
|
+
const m = body.match(/^([A-Za-z0-9_.-]+):\s*(.*)$/);
|
|
33
|
+
if (!m) fail(`expected "key: value", got ${JSON.stringify(body)}`, line);
|
|
34
|
+
const [, key, rest] = m;
|
|
35
|
+
pos++;
|
|
36
|
+
if (rest === "") {
|
|
37
|
+
const next = lines[pos];
|
|
38
|
+
if (next && next.indent > indent) out[key] = parseBlock(next.indent);
|
|
39
|
+
else out[key] = null;
|
|
40
|
+
} else {
|
|
41
|
+
out[key] = parseScalar(rest, line);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
function parseList(indent) {
|
|
47
|
+
const out = [];
|
|
48
|
+
while (pos < lines.length) {
|
|
49
|
+
const line = lines[pos];
|
|
50
|
+
if (line.indent < indent) break;
|
|
51
|
+
const body = line.text.trim();
|
|
52
|
+
if (!body.startsWith("- ")) break;
|
|
53
|
+
if (line.indent > indent) fail("unexpected indentation in list", line);
|
|
54
|
+
const item = body.slice(2).trim();
|
|
55
|
+
pos++;
|
|
56
|
+
if (item.startsWith("{") && item.endsWith("}")) {
|
|
57
|
+
out.push(parseInlineMap(item, line));
|
|
58
|
+
} else if (/^[A-Za-z0-9_.-]+:/.test(item)) {
|
|
59
|
+
const sub = { ...parseInlinePair(item, line) };
|
|
60
|
+
while (pos < lines.length && lines[pos].indent > indent) {
|
|
61
|
+
const c = lines[pos];
|
|
62
|
+
const cm = c.text.trim().match(/^([A-Za-z0-9_.-]+):\s*(.*)$/);
|
|
63
|
+
if (!cm) fail(`expected "key: value" in list item, got ${JSON.stringify(c.text.trim())}`, c);
|
|
64
|
+
pos++;
|
|
65
|
+
if (cm[2] === "") {
|
|
66
|
+
const next = lines[pos];
|
|
67
|
+
sub[cm[1]] = next && next.indent > c.indent ? parseBlock(next.indent) : null;
|
|
68
|
+
} else {
|
|
69
|
+
sub[cm[1]] = parseScalar(cm[2], c);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
out.push(sub);
|
|
73
|
+
} else {
|
|
74
|
+
out.push(parseScalar(item, line));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
function parseInlinePair(item, line) {
|
|
80
|
+
const m = item.match(/^([A-Za-z0-9_.-]+):\s*(.*)$/);
|
|
81
|
+
if (!m) fail("bad list item", line);
|
|
82
|
+
return { [m[1]]: m[2] === "" ? null : parseScalar(m[2], line) };
|
|
83
|
+
}
|
|
84
|
+
function parseInlineMap(item, line) {
|
|
85
|
+
const out = {};
|
|
86
|
+
for (const part of splitTopLevel(item.slice(1, -1))) {
|
|
87
|
+
if (!part.trim()) continue;
|
|
88
|
+
const m = part.match(/^\s*([A-Za-z0-9_.-]+)\s*:\s*(.*)$/);
|
|
89
|
+
if (!m) fail(`bad inline map entry ${JSON.stringify(part)}`, line);
|
|
90
|
+
out[m[1]] = parseScalar(m[2].trim(), line);
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
const root = parseBlock(lines.length ? lines[0].indent : 0);
|
|
95
|
+
if (pos < lines.length) fail("could not parse to end of file", lines[pos]);
|
|
96
|
+
return root ?? {};
|
|
97
|
+
}
|
|
98
|
+
function stripComment(raw) {
|
|
99
|
+
let out = "";
|
|
100
|
+
let quote = null;
|
|
101
|
+
for (let i = 0; i < raw.length; i++) {
|
|
102
|
+
const c = raw[i];
|
|
103
|
+
if (quote) {
|
|
104
|
+
out += c;
|
|
105
|
+
if (c === quote && raw[i - 1] !== "\\") quote = null;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (c === '"' || c === "'") {
|
|
109
|
+
quote = c;
|
|
110
|
+
out += c;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (c === "#" && (i === 0 || /\s/.test(raw[i - 1]))) break;
|
|
114
|
+
out += c;
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
function splitTopLevel(s) {
|
|
119
|
+
const parts = [];
|
|
120
|
+
let depth = 0;
|
|
121
|
+
let cur = "";
|
|
122
|
+
for (const c of s) {
|
|
123
|
+
if (c === "{" || c === "[") depth++;
|
|
124
|
+
if (c === "}" || c === "]") depth--;
|
|
125
|
+
if (c === "," && depth === 0) {
|
|
126
|
+
parts.push(cur);
|
|
127
|
+
cur = "";
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
cur += c;
|
|
131
|
+
}
|
|
132
|
+
parts.push(cur);
|
|
133
|
+
return parts;
|
|
134
|
+
}
|
|
135
|
+
function parseScalar(raw, line) {
|
|
136
|
+
const v = raw.trim();
|
|
137
|
+
if (v.startsWith("[") && v.endsWith("]")) {
|
|
138
|
+
const inner = v.slice(1, -1).trim();
|
|
139
|
+
return inner === "" ? [] : splitTopLevel(inner).map((s) => parseScalar(s, line));
|
|
140
|
+
}
|
|
141
|
+
if (v.startsWith('"') && v.endsWith('"') || v.startsWith("'") && v.endsWith("'")) {
|
|
142
|
+
return v.slice(1, -1);
|
|
143
|
+
}
|
|
144
|
+
if (v === "true") return true;
|
|
145
|
+
if (v === "false") return false;
|
|
146
|
+
if (v === "null" || v === "~") return null;
|
|
147
|
+
if (/^-?\d+$/.test(v)) return Number(v);
|
|
148
|
+
return v;
|
|
149
|
+
}
|
|
150
|
+
var MAX_INCLUDE_DEPTH = 8;
|
|
151
|
+
var FENCE = "\0";
|
|
152
|
+
function render(template, ctx, readPartial, depth = 0) {
|
|
153
|
+
if (depth > MAX_INCLUDE_DEPTH) throw new Error("include depth exceeded; a partial probably includes itself");
|
|
154
|
+
let out = template.replace(
|
|
155
|
+
/\{\{#if\s+([A-Za-z0-9_.]+)\s*\}\}\n?([\s\S]*?)\{\{\/if\}\}\n?/g,
|
|
156
|
+
(_, path, body) => truthy(lookup(ctx, path)) ? body : ""
|
|
157
|
+
);
|
|
158
|
+
out = out.replace(/\{\{>(\??)\s*([^}\s]+)\s*\}\}/g, (_, optional, rel) => {
|
|
159
|
+
const partial = readPartial(rel);
|
|
160
|
+
if (partial === null) {
|
|
161
|
+
if (optional) return "";
|
|
162
|
+
throw new Error(`partial not found: ${rel}`);
|
|
163
|
+
}
|
|
164
|
+
return render(partial, ctx, readPartial, depth + 1).replace(/\n+$/, "");
|
|
165
|
+
});
|
|
166
|
+
out = out.replace(/\{\{\s*([A-Za-z0-9_.]+)\s*\}\}/g, (_, path) => {
|
|
167
|
+
const v = lookup(ctx, path);
|
|
168
|
+
if (v === void 0 || v === null) {
|
|
169
|
+
if (path.startsWith("event.")) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
`{{${path}}} needs trigger context, which arrives as ROSTER_CONTEXT.
|
|
172
|
+
A "mention" prompt is written for the comment that woke it, so it cannot
|
|
173
|
+
be composed without one. To see it locally:
|
|
174
|
+
ROSTER_CONTEXT='{"issue_number":"1","comment_id":"1","repo":"o/r"}' \\
|
|
175
|
+
node compose.mjs --staff <handle> --kind <kind>`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
throw new Error(`unknown or empty placeholder: {{${path}}}`);
|
|
179
|
+
}
|
|
180
|
+
return String(v).replace(/\{\{/g, FENCE);
|
|
181
|
+
});
|
|
182
|
+
return depth === 0 ? out.replaceAll(FENCE, "{{") : out;
|
|
183
|
+
}
|
|
184
|
+
function lookup(ctx, path) {
|
|
185
|
+
return path.split(".").reduce((acc, k) => acc == null ? void 0 : acc[k], ctx);
|
|
186
|
+
}
|
|
187
|
+
function truthy(v) {
|
|
188
|
+
return !(v === void 0 || v === null || v === false || v === "" || Array.isArray(v) && v.length === 0);
|
|
189
|
+
}
|
|
190
|
+
function readHumans(org) {
|
|
191
|
+
const listed = Array.isArray(org?.humans) ? org.humans : org?.humans ? [org.humans] : [];
|
|
192
|
+
const out = [];
|
|
193
|
+
for (const entry of [...listed, org?.human].filter(Boolean)) {
|
|
194
|
+
const github = String(entry.github ?? entry.login ?? "").trim();
|
|
195
|
+
const name = String(entry.name ?? github).trim();
|
|
196
|
+
if (!github && !name) continue;
|
|
197
|
+
if (github && out.some((h) => h.github.toLowerCase() === github.toLowerCase())) continue;
|
|
198
|
+
out.push({
|
|
199
|
+
github,
|
|
200
|
+
name: name || github,
|
|
201
|
+
marker: String(entry.marker ?? "").trim() || defaultMarker(name || github),
|
|
202
|
+
role: entry.role ?? void 0,
|
|
203
|
+
...entry.view_only === true ? { viewOnly: true } : {}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
function humanSentence(humans) {
|
|
209
|
+
const parts = humans.map((h) => h.github ? `${h.name} (@${h.github})` : h.name);
|
|
210
|
+
if (parts.length < 2) return parts[0] ?? "";
|
|
211
|
+
return `${parts.slice(0, -1).join(", ")} and ${parts[parts.length - 1]}`;
|
|
212
|
+
}
|
|
213
|
+
function defaultMarker(name) {
|
|
214
|
+
return String(name).trim().split(/[\s-]+/)[0].toLowerCase().replace(/[^a-z0-9]/g, "") || "human";
|
|
215
|
+
}
|
|
216
|
+
function compose({
|
|
217
|
+
opsDir,
|
|
218
|
+
brainsDir,
|
|
219
|
+
staff,
|
|
220
|
+
kind,
|
|
221
|
+
runDir,
|
|
222
|
+
fs = nodeFs,
|
|
223
|
+
context = process.env.ROSTER_CONTEXT
|
|
224
|
+
}) {
|
|
225
|
+
const { readFileSync, existsSync } = fs;
|
|
226
|
+
const org = parseYaml(readFileSync(join(opsDir, "org.yaml"), "utf8"), "org.yaml");
|
|
227
|
+
const entry = (org.staff ?? []).find((s) => s.handle === staff);
|
|
228
|
+
if (!entry) {
|
|
229
|
+
const known = (org.staff ?? []).map((s) => s.handle).join(", ") || "none";
|
|
230
|
+
throw new Error(`unknown staff handle "${staff}". org.yaml knows: ${known}`);
|
|
231
|
+
}
|
|
232
|
+
const brainDir = resolve(brainsDir, entry.dir ?? entry.handle);
|
|
233
|
+
const staffFile = join(brainDir, "staff.yaml");
|
|
234
|
+
if (!existsSync(staffFile)) throw new Error(`no staff.yaml at ${staffFile}`);
|
|
235
|
+
const self2 = parseYaml(readFileSync(staffFile, "utf8"), `${entry.handle}/staff.yaml`);
|
|
236
|
+
const peers = (org.staff ?? []).filter((s) => s.handle !== staff).map((s) => ({ ...s, ...s.dir ? {} : { dir: s.handle } }));
|
|
237
|
+
let event = {};
|
|
238
|
+
if (context) {
|
|
239
|
+
try {
|
|
240
|
+
event = JSON.parse(context);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
throw new Error(`ROSTER_CONTEXT is not valid JSON: ${err.message}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (Object.keys(event).length) event = { ...event, no_comment: !event.comment_id };
|
|
246
|
+
const trigger = String(event.trigger ?? "");
|
|
247
|
+
event = {
|
|
248
|
+
...event,
|
|
249
|
+
from_peer: trigger === "peer",
|
|
250
|
+
from_human: trigger !== "peer",
|
|
251
|
+
follow_on: trigger === "follow-on"
|
|
252
|
+
};
|
|
253
|
+
const inflightFile = join(runDir ?? join(brainsDir, ".roster-run"), "inflight.md");
|
|
254
|
+
const inflight = existsSync(inflightFile) ? readFileSync(inflightFile, "utf8").trim() : "";
|
|
255
|
+
const everyone = readHumans(org);
|
|
256
|
+
const humans = everyone.filter((h) => !h.viewOnly);
|
|
257
|
+
const ctx = {
|
|
258
|
+
org,
|
|
259
|
+
event,
|
|
260
|
+
// The first is who the prose addresses; `humans` is everybody the mention gate accepts.
|
|
261
|
+
human: humans[0] ?? org.human,
|
|
262
|
+
humans,
|
|
263
|
+
ops: { dir: org.ops_dir ?? "roster-ops" },
|
|
264
|
+
// `dir` lives in the org registry (it is where the checkout lands), everything
|
|
265
|
+
// else lives in the staff member's own manifest.
|
|
266
|
+
staff: {
|
|
267
|
+
...self2,
|
|
268
|
+
dir: entry.dir ?? entry.handle,
|
|
269
|
+
// The repo this role contributes to but does not own. Named explicitly in the
|
|
270
|
+
// prompt because "a repo you do not own" is vaguer than an agent needs.
|
|
271
|
+
product: (self2.works_in ?? [])[0] ?? null,
|
|
272
|
+
// Absent from manifests written before it existed; the callers fall back to the same.
|
|
273
|
+
max_runs_per_day: self2.max_runs_per_day ?? 6,
|
|
274
|
+
// One staff member drafts next month's org/priorities.md, so there is one draft rather
|
|
275
|
+
// than one per person: whoever org.yaml lists first.
|
|
276
|
+
drafts_priorities: (org.staff ?? [])[0]?.handle === staff
|
|
277
|
+
},
|
|
278
|
+
peers,
|
|
279
|
+
peer: peers[0] ?? null,
|
|
280
|
+
kind,
|
|
281
|
+
// Convenience strings the fragments lean on, computed once here so a
|
|
282
|
+
// fragment never has to do string work.
|
|
283
|
+
peer_list: peers.map((p) => `- \`${p.dir}/\` - the ${p.name}'s brain`).join("\n"),
|
|
284
|
+
// Everybody who can wake this staff member and rule on their work, and the same list
|
|
285
|
+
// without the one the prose already names. Empty when there is only one, which is what
|
|
286
|
+
// makes `{{#if humans_extra}}` the right way to mention the others at all.
|
|
287
|
+
human_list: humanSentence(humans),
|
|
288
|
+
inflight,
|
|
289
|
+
humans_extra: humanSentence(humans.slice(1)),
|
|
290
|
+
// Who can read along and cannot rule. Empty, and the paragraph gone, when nobody is.
|
|
291
|
+
humans_viewing: humanSentence(everyone.filter((h) => h.viewOnly)),
|
|
292
|
+
// The services this staff member may use, as a list for the prompt. Empty, and the section
|
|
293
|
+
// gone, when they have none. Whether each one's key is present is only known in the run.
|
|
294
|
+
connectors: connectorsOf(org, staff).filter((c) => !c.problem).map((c) => {
|
|
295
|
+
const ro = c.readOnly || self2.permissions === "read-only";
|
|
296
|
+
return `- **${c.name}**, as the \`${c.id}\` MCP server${ro ? ", read only" : ""}`;
|
|
297
|
+
}).join("\n")
|
|
298
|
+
};
|
|
299
|
+
const readPartial = (rel) => {
|
|
300
|
+
const p = rel.startsWith("staff:") ? join(brainDir, rel.slice(6)) : join(opsDir, rel);
|
|
301
|
+
return existsSync(p) ? readFileSync(p, "utf8") : null;
|
|
302
|
+
};
|
|
303
|
+
const kindFile = join(opsDir, "prompts", `${kind}.md`);
|
|
304
|
+
if (!existsSync(kindFile)) throw new Error(`no prompt for kind "${kind}" at ${kindFile}`);
|
|
305
|
+
const body = render(readFileSync(kindFile, "utf8"), ctx, readPartial);
|
|
306
|
+
return body.replace(/\n{3,}/g, "\n\n").trimEnd() + "\n";
|
|
307
|
+
}
|
|
308
|
+
function main(argv) {
|
|
309
|
+
const args = {};
|
|
310
|
+
for (let i = 0; i < argv.length; i += 2) {
|
|
311
|
+
if (!argv[i].startsWith("--")) throw new Error(`unexpected argument ${argv[i]}`);
|
|
312
|
+
args[argv[i].slice(2)] = argv[i + 1];
|
|
313
|
+
}
|
|
314
|
+
if (!args.staff || !args.kind) {
|
|
315
|
+
console.error("usage: node compose.mjs --staff <handle> --kind <daily|mention> [--ops DIR] [--brains DIR]");
|
|
316
|
+
process.exit(2);
|
|
317
|
+
}
|
|
318
|
+
const opsDir = resolve(args.ops ?? dirname(fileURLToPath(import.meta.url)));
|
|
319
|
+
const brainsDir = resolve(args.brains ?? join(opsDir, ".."));
|
|
320
|
+
process.stdout.write(compose({ opsDir, brainsDir, staff: args.staff, kind: args.kind }));
|
|
321
|
+
}
|
|
322
|
+
var self = import.meta.url ? fileURLToPath(import.meta.url) : "";
|
|
323
|
+
if (process.argv[1] && self.endsWith("compose.mjs") && resolve(process.argv[1]) === resolve(self)) {
|
|
324
|
+
try {
|
|
325
|
+
main(process.argv.slice(2));
|
|
326
|
+
} catch (err) {
|
|
327
|
+
console.error(`compose: ${err.message}`);
|
|
328
|
+
process.exit(1);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export {
|
|
333
|
+
parseYaml,
|
|
334
|
+
render,
|
|
335
|
+
readHumans,
|
|
336
|
+
humanSentence,
|
|
337
|
+
compose
|
|
338
|
+
};
|