master-orchestrator 0.0.0-stage → 0.2.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/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/package.json +56 -4
- package/src/adapters/agy.ts +285 -0
- package/src/adapters/claude.ts +317 -0
- package/src/adapters/codex.ts +162 -0
- package/src/adapters/common.ts +211 -0
- package/src/adapters/dsh.ts +356 -0
- package/src/adapters/fake-agent.mjs +50 -0
- package/src/adapters/fake.ts +88 -0
- package/src/adapters/gemini.ts +239 -0
- package/src/adapters/grok.ts +328 -0
- package/src/adapters/omp.ts +119 -0
- package/src/adapters/opencode.ts +228 -0
- package/src/adapters/pi.ts +284 -0
- package/src/adapters/registry.ts +27 -0
- package/src/adapters/types.ts +201 -0
- package/src/config/load.ts +290 -0
- package/src/config/profiles.ts +64 -0
- package/src/config/schema.ts +158 -0
- package/src/config/write.ts +83 -0
- package/src/engine/artifacts.ts +145 -0
- package/src/engine/baseline.ts +132 -0
- package/src/engine/execute.ts +437 -0
- package/src/engine/git.ts +32 -0
- package/src/engine/hostenv.ts +54 -0
- package/src/engine/registry.ts +280 -0
- package/src/engine/runner.ts +249 -0
- package/src/engine/types.ts +55 -0
- package/src/engine/verify.ts +100 -0
- package/src/index.ts +602 -0
- package/src/prompts/envelope.ts +29 -0
- package/src/prompts/templates.ts +389 -0
- package/src/ui/configure.ts +174 -0
- package/src/ui/dialogs.ts +88 -0
- package/src/ui/doctor.ts +120 -0
- package/src/ui/format.ts +94 -0
- package/src/ui/host.ts +100 -0
- package/src/ui/render.ts +86 -0
- package/src/ui/widget.ts +70 -0
- package/src/workflows/budget.ts +66 -0
- package/src/workflows/common.ts +100 -0
- package/src/workflows/debate.ts +488 -0
- package/src/workflows/host.ts +78 -0
- package/src/workflows/plan-exec.ts +807 -0
- package/src/workflows/plan.ts +407 -0
- package/src/workflows/planfile.ts +380 -0
- package/src/workflows/planstate.ts +229 -0
- package/src/workflows/verdict.ts +74 -0
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Built-in templates. `{{name}}` placeholders are replaced by `render`; unknown placeholders become empty.
|
|
6
|
+
* A directory configured in `prompts.dir` may override any of them with `<name>.md`.
|
|
7
|
+
*/
|
|
8
|
+
export const TEMPLATES = {
|
|
9
|
+
envelope: `{{task}}
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Rules for this run
|
|
14
|
+
|
|
15
|
+
{{modeRules}}
|
|
16
|
+
- Workspace: {{workspace}}
|
|
17
|
+
- Diagnostic work is welcome: you may write and run scripts, probes and experiments. Put them in this scratch directory, which is yours alone: {{scratch}}. Anything temporary you create inside the workspace must be removed before you finish.
|
|
18
|
+
- Nobody answers questions during this run. Do not ask, do not widen the scope, and report blockers in your final answer.
|
|
19
|
+
- Do the work yourself. Do not delegate it and do not start other agents.
|
|
20
|
+
- Git: do not commit, push or publish, and do not change branches or the stash, unless the assignment above explicitly says otherwise.
|
|
21
|
+
- Evidence: {{evidence}} Separate facts you verified from hypotheses.
|
|
22
|
+
- Your final answer is your report: it is the only thing the orchestrator reads.
|
|
23
|
+
`,
|
|
24
|
+
|
|
25
|
+
modeAnalyze: `- Mode: analyze. Investigate and report. Leave the project's files exactly as they were.`,
|
|
26
|
+
|
|
27
|
+
modeImplementScoped: `- Mode: implement. Editable scope (paths and globs relative to the workspace):
|
|
28
|
+
{{scopeList}}
|
|
29
|
+
Do not modify anything outside this scope.`,
|
|
30
|
+
|
|
31
|
+
modeImplementOpen: `- Mode: implement. Change only what the assignment requires.`,
|
|
32
|
+
|
|
33
|
+
evidenceAnalyze: `cite file and line for every finding.`,
|
|
34
|
+
|
|
35
|
+
evidenceImplement: `list every file you changed and every check you ran, with its actual result.`,
|
|
36
|
+
|
|
37
|
+
framingSystem: `You prepare assignments for independent coding agents that see none of this conversation. You write precise, self-contained briefs. You never answer the question yourself.`,
|
|
38
|
+
|
|
39
|
+
framing: `Turn the request below into a self-contained brief for {{audience}}.
|
|
40
|
+
|
|
41
|
+
The brief must contain:
|
|
42
|
+
- the question or goal, stated precisely;
|
|
43
|
+
- the decision to be made or the outcome expected;
|
|
44
|
+
- known constraints and preferences from the conversation;
|
|
45
|
+
- relevant files, symbols and facts already established, with paths;
|
|
46
|
+
- what is out of scope.
|
|
47
|
+
|
|
48
|
+
Write only the brief, in Markdown, without preamble. Workspace: {{workspace}}
|
|
49
|
+
|
|
50
|
+
## Request
|
|
51
|
+
|
|
52
|
+
{{request}}
|
|
53
|
+
|
|
54
|
+
## Recent conversation (for context; may be empty)
|
|
55
|
+
|
|
56
|
+
{{conversation}}
|
|
57
|
+
`,
|
|
58
|
+
|
|
59
|
+
debateOpening: `You are one participant in a structured debate between independent coding agents. Answer the brief below on your own: you will not see the other answers in this round. Investigate the code to ground your answer.
|
|
60
|
+
|
|
61
|
+
## Brief
|
|
62
|
+
|
|
63
|
+
{{brief}}
|
|
64
|
+
|
|
65
|
+
## Your answer
|
|
66
|
+
|
|
67
|
+
Give your recommended answer with its reasoning and evidence (file and line references).
|
|
68
|
+
|
|
69
|
+
{{stanceFormat}}
|
|
70
|
+
`,
|
|
71
|
+
|
|
72
|
+
debateRebuttal: `Round {{round}} of {{maxRounds}} of the debate. Below are the latest positions of the other participants. Check their claims against the code where it matters.
|
|
73
|
+
|
|
74
|
+
State where you agree, where you disagree and on what evidence, and give your updated position. Changing your mind on evidence is expected; do not defer to the others without it.
|
|
75
|
+
|
|
76
|
+
## Brief
|
|
77
|
+
|
|
78
|
+
{{brief}}
|
|
79
|
+
{{ownPrevious}}
|
|
80
|
+
## Other participants
|
|
81
|
+
|
|
82
|
+
{{others}}
|
|
83
|
+
|
|
84
|
+
{{stanceFormat}}
|
|
85
|
+
`,
|
|
86
|
+
|
|
87
|
+
stanceFormat: `End your answer with this fenced JSON block, exactly once:
|
|
88
|
+
|
|
89
|
+
\`\`\`json
|
|
90
|
+
{
|
|
91
|
+
"position": "one-paragraph summary of your recommended answer",
|
|
92
|
+
"confidence": "low | medium | high",
|
|
93
|
+
"changed": false,
|
|
94
|
+
"disagreements": [{ "with": "Participant B", "point": "...", "evidence": "path/file.ts:120" }]
|
|
95
|
+
}
|
|
96
|
+
\`\`\`
|
|
97
|
+
|
|
98
|
+
"changed" is true when your position changed in this round. "disagreements" lists only points still open.`,
|
|
99
|
+
|
|
100
|
+
stanceStructured: `Your final message is one JSON object matching the schema you were given:
|
|
101
|
+
- "answer": your full answer in Markdown, with its reasoning and evidence (file and line references);
|
|
102
|
+
- "position": a one-paragraph summary of your recommended answer;
|
|
103
|
+
- "confidence": "low", "medium" or "high";
|
|
104
|
+
- "changed": true when your position changed in this round;
|
|
105
|
+
- "disagreements": the points still open, each with "with" (the participant), "point" and "evidence".`,
|
|
106
|
+
|
|
107
|
+
debateSynthesis: `The debate "{{title}}" has ended ({{outcome}}). You moderated it and hold no position of your own in it. Write the synthesis for the user now.
|
|
108
|
+
|
|
109
|
+
Cover:
|
|
110
|
+
1. Points of consensus.
|
|
111
|
+
2. Open disagreements: who holds which position and on what evidence.
|
|
112
|
+
3. Which cited evidence you verified in the code (use your tools) and which you could not.
|
|
113
|
+
4. Your recommendation with its reasoning. Say so explicitly when it departs from the majority.
|
|
114
|
+
5. Questions only the user can answer.
|
|
115
|
+
|
|
116
|
+
## Brief
|
|
117
|
+
|
|
118
|
+
{{brief}}
|
|
119
|
+
|
|
120
|
+
## Final positions
|
|
121
|
+
|
|
122
|
+
{{positions}}
|
|
123
|
+
|
|
124
|
+
## Record
|
|
125
|
+
|
|
126
|
+
{{record}}
|
|
127
|
+
`,
|
|
128
|
+
|
|
129
|
+
planFormat: `The plan is ONE Markdown document with exactly this structure:
|
|
130
|
+
|
|
131
|
+
\`\`\`markdown
|
|
132
|
+
---
|
|
133
|
+
id: {{planId}}
|
|
134
|
+
goal: <one line>
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## Context
|
|
138
|
+
Findings from the code that the steps rely on.
|
|
139
|
+
|
|
140
|
+
## Block 1: <title>
|
|
141
|
+
- acceptance: <what must hold when the block ends>
|
|
142
|
+
- validation: <command that checks the block, optional>
|
|
143
|
+
|
|
144
|
+
What this block delivers and why it is one reviewable unit.
|
|
145
|
+
|
|
146
|
+
### Step 1.1: <title>
|
|
147
|
+
- depends: none
|
|
148
|
+
- scope: <path or glob relative to the workspace>, <another>
|
|
149
|
+
- acceptance: <what must hold when the step ends>
|
|
150
|
+
|
|
151
|
+
What to do and why.
|
|
152
|
+
|
|
153
|
+
### Step 1.2: <title>
|
|
154
|
+
- depends: 1.1
|
|
155
|
+
- scope: <paths>
|
|
156
|
+
- acceptance: <criterion>
|
|
157
|
+
|
|
158
|
+
## Risks
|
|
159
|
+
Free text.
|
|
160
|
+
\`\`\`
|
|
161
|
+
|
|
162
|
+
Rules:
|
|
163
|
+
- A step is one assignment for one worker; steps may be small.
|
|
164
|
+
- A block is a group of steps that together form one coherent change a reviewer can judge on its own, and that leaves the workspace consistent (it builds and its validation passes) when it ends.
|
|
165
|
+
- Never make a microtask a block of its own: group small steps into a meaningful block. A substantial step may be the only step of its block.
|
|
166
|
+
- Required per block: title, acceptance, at least one step. Optional: validation.
|
|
167
|
+
- Required per step: title, scope, acceptance. Optional: depends (step ids, comma-separated, or none), role (default implementer), profile, validation.
|
|
168
|
+
- Step ids are <block>.<n>. A step may depend only on steps of its own or earlier blocks.
|
|
169
|
+
- Scope entries are relative to the workspace; never absolute, never outside it.`,
|
|
170
|
+
|
|
171
|
+
planDraft: `You are the planner. Produce an implementation plan for the brief below. Investigate the code first: the plan must rest on what the code actually does.
|
|
172
|
+
|
|
173
|
+
## Brief
|
|
174
|
+
|
|
175
|
+
{{brief}}
|
|
176
|
+
|
|
177
|
+
{{planFormat}}
|
|
178
|
+
|
|
179
|
+
Your final answer must be the plan document and nothing else.
|
|
180
|
+
`,
|
|
181
|
+
|
|
182
|
+
planRepair: `Your plan document could not be parsed:
|
|
183
|
+
|
|
184
|
+
{{errors}}
|
|
185
|
+
|
|
186
|
+
Return the complete corrected plan document, and nothing else, following the required structure:
|
|
187
|
+
|
|
188
|
+
{{planFormat}}
|
|
189
|
+
`,
|
|
190
|
+
|
|
191
|
+
planCritique: `You are a critic of an implementation plan. Check it against the code, not only its wording. Check the grouping too: a block that is a microtask, or one too large to review as a unit, is a finding.
|
|
192
|
+
|
|
193
|
+
## Brief
|
|
194
|
+
|
|
195
|
+
{{brief}}
|
|
196
|
+
|
|
197
|
+
## Plan
|
|
198
|
+
|
|
199
|
+
{{plan}}
|
|
200
|
+
|
|
201
|
+
Report each problem with evidence. End your answer with this fenced JSON block, exactly once:
|
|
202
|
+
|
|
203
|
+
\`\`\`json
|
|
204
|
+
{ "findings": [{ "severity": "blocking", "summary": "...", "evidence": "path/file.ts:120", "block": "2" }] }
|
|
205
|
+
\`\`\`
|
|
206
|
+
|
|
207
|
+
"severity" is "blocking" when the plan must change before it can be executed, otherwise "advisory". An empty list means you found nothing.
|
|
208
|
+
`,
|
|
209
|
+
|
|
210
|
+
planRevise: `The critics reviewed your plan. Revise it.
|
|
211
|
+
|
|
212
|
+
## Findings
|
|
213
|
+
|
|
214
|
+
{{findings}}
|
|
215
|
+
|
|
216
|
+
Return the complete revised plan document, following the required structure, then a fenced JSON block with a disposition for every blocking finding:
|
|
217
|
+
|
|
218
|
+
\`\`\`json
|
|
219
|
+
{ "dispositions": [{ "finding": "F1", "disposition": "fixed", "reason": "..." }] }
|
|
220
|
+
\`\`\`
|
|
221
|
+
|
|
222
|
+
"disposition" is "fixed" or "rejected"; a rejection needs a reason grounded in the code.
|
|
223
|
+
|
|
224
|
+
{{planFormat}}
|
|
225
|
+
`,
|
|
226
|
+
|
|
227
|
+
planUserRevise: `The user asked for a revision of your plan:
|
|
228
|
+
|
|
229
|
+
{{comments}}
|
|
230
|
+
|
|
231
|
+
Return the complete revised plan document, and nothing else, following the required structure.
|
|
232
|
+
|
|
233
|
+
{{planFormat}}
|
|
234
|
+
`,
|
|
235
|
+
|
|
236
|
+
planStep: `You are implementing one step of an approved plan.
|
|
237
|
+
|
|
238
|
+
## Goal of the plan
|
|
239
|
+
|
|
240
|
+
{{goal}}
|
|
241
|
+
|
|
242
|
+
## Plan context
|
|
243
|
+
|
|
244
|
+
{{context}}
|
|
245
|
+
|
|
246
|
+
## Block {{blockId}}: {{blockTitle}}
|
|
247
|
+
|
|
248
|
+
{{blockBody}}
|
|
249
|
+
|
|
250
|
+
Block acceptance: {{blockAcceptance}}
|
|
251
|
+
|
|
252
|
+
## Your step: {{stepId}} {{stepTitle}}
|
|
253
|
+
|
|
254
|
+
{{stepBody}}
|
|
255
|
+
|
|
256
|
+
Step acceptance: {{stepAcceptance}}
|
|
257
|
+
|
|
258
|
+
{{validation}}
|
|
259
|
+
|
|
260
|
+
## Already done
|
|
261
|
+
|
|
262
|
+
{{done}}
|
|
263
|
+
|
|
264
|
+
Implement this step only. Run the validation that applies and report the actual results.
|
|
265
|
+
`,
|
|
266
|
+
|
|
267
|
+
planRetry: `An earlier attempt at this step stopped before finishing ({{reason}}). It changed these files, which are still in the workspace:
|
|
268
|
+
|
|
269
|
+
{{files}}
|
|
270
|
+
|
|
271
|
+
Inspect them and continue from there instead of starting over.
|
|
272
|
+
|
|
273
|
+
{{assignment}}`,
|
|
274
|
+
|
|
275
|
+
planReview: `You are the independent reviewer of one block of an approved plan. Review the change set below against the block's and its steps' acceptance criteria. Run the validation yourself; do not trust the workers' reports.
|
|
276
|
+
|
|
277
|
+
## Goal of the plan
|
|
278
|
+
|
|
279
|
+
{{goal}}
|
|
280
|
+
|
|
281
|
+
## Block {{blockId}}: {{blockTitle}}
|
|
282
|
+
|
|
283
|
+
{{blockBody}}
|
|
284
|
+
|
|
285
|
+
Block acceptance: {{blockAcceptance}}
|
|
286
|
+
|
|
287
|
+
## Steps
|
|
288
|
+
|
|
289
|
+
{{steps}}
|
|
290
|
+
|
|
291
|
+
{{skipped}}
|
|
292
|
+
## Change set (frozen before your review)
|
|
293
|
+
|
|
294
|
+
Changed files:
|
|
295
|
+
{{files}}
|
|
296
|
+
|
|
297
|
+
The full diff is saved at {{diffPath}}.
|
|
298
|
+
|
|
299
|
+
Report each problem with file and line evidence. End your answer with this fenced JSON block, exactly once:
|
|
300
|
+
|
|
301
|
+
\`\`\`json
|
|
302
|
+
{ "findings": [{ "severity": "blocking", "summary": "...", "file": "src/a.ts", "line": 12, "evidence": "..." }] }
|
|
303
|
+
\`\`\`
|
|
304
|
+
|
|
305
|
+
"severity" is "blocking" when the block cannot be accepted as it is, otherwise "advisory".
|
|
306
|
+
`,
|
|
307
|
+
|
|
308
|
+
planFix: `The review of block {{blockId}} ("{{blockTitle}}") found problems that belong to your work. Fix them.
|
|
309
|
+
|
|
310
|
+
## Findings
|
|
311
|
+
|
|
312
|
+
{{findings}}
|
|
313
|
+
|
|
314
|
+
{{context}}
|
|
315
|
+
Editable scope: the block's combined scope. Run the validation that applies and report the actual results.
|
|
316
|
+
`,
|
|
317
|
+
|
|
318
|
+
planVerdict: `Block {{blockId}} of plan {{planId}} ("{{blockTitle}}") needs your verdict.
|
|
319
|
+
|
|
320
|
+
Inspect the diff, run the validation you consider necessary, and end by calling plan_verdict with blockId "{{blockId}}". The workers' reports and the reviewer's opinion inform the verdict; they are not acceptance evidence.
|
|
321
|
+
|
|
322
|
+
## Block acceptance
|
|
323
|
+
|
|
324
|
+
{{blockAcceptance}}
|
|
325
|
+
|
|
326
|
+
## Steps
|
|
327
|
+
|
|
328
|
+
{{steps}}
|
|
329
|
+
|
|
330
|
+
## Changed files
|
|
331
|
+
|
|
332
|
+
{{files}}
|
|
333
|
+
|
|
334
|
+
{{outOfScope}}
|
|
335
|
+
{{reviewerChanges}}
|
|
336
|
+
## Review findings
|
|
337
|
+
|
|
338
|
+
{{findings}}
|
|
339
|
+
|
|
340
|
+
## Worker reports
|
|
341
|
+
|
|
342
|
+
{{reports}}
|
|
343
|
+
`,
|
|
344
|
+
|
|
345
|
+
planVerdictReminder: `Plan {{planId}} is waiting for your verdict on block {{blockId}}. Call plan_verdict with blockId "{{blockId}}" and verdict accepted, needs_fix or blocked. Without the call the plan pauses.`,
|
|
346
|
+
|
|
347
|
+
planReport: `Plan {{planId}} ("{{goal}}") finished with status {{status}}. Report the outcome to the user: what each block delivered, the review findings that remain, skipped work, and the changed files.
|
|
348
|
+
|
|
349
|
+
{{blocks}}
|
|
350
|
+
`,
|
|
351
|
+
} as const;
|
|
352
|
+
|
|
353
|
+
export type TemplateName = keyof typeof TEMPLATES;
|
|
354
|
+
|
|
355
|
+
export function render(template: string, vars: Record<string, string | number | undefined>): string {
|
|
356
|
+
return template.replace(/\{\{(\w+)\}\}/g, (_, name: string) => {
|
|
357
|
+
const value = vars[name];
|
|
358
|
+
return value === undefined ? "" : String(value);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** Templates with per-directory overrides (`<dir>/<name>.md`). */
|
|
363
|
+
export class Templates {
|
|
364
|
+
private readonly dir: string | null;
|
|
365
|
+
private readonly cache = new Map<string, string>();
|
|
366
|
+
|
|
367
|
+
constructor(dir: string | null) {
|
|
368
|
+
this.dir = dir;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
get(name: TemplateName): string {
|
|
372
|
+
const cached = this.cache.get(name);
|
|
373
|
+
if (cached !== undefined) return cached;
|
|
374
|
+
let text: string = TEMPLATES[name];
|
|
375
|
+
if (this.dir) {
|
|
376
|
+
try {
|
|
377
|
+
text = readFileSync(join(this.dir, `${name}.md`), "utf8");
|
|
378
|
+
} catch {
|
|
379
|
+
// No override for this template.
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
this.cache.set(name, text);
|
|
383
|
+
return text;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
render(name: TemplateName, vars: Record<string, string | number | undefined>): string {
|
|
387
|
+
return render(this.get(name), vars);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { isMultiRole } from "../config/profiles.ts";
|
|
3
|
+
import { ROLE_NAMES, type ConfigFile, type OrchestratorConfig, type RoleName } from "../config/schema.ts";
|
|
4
|
+
import { readUserConfig, withRole, writeUserConfig } from "../config/write.ts";
|
|
5
|
+
import type { Engine } from "../engine/execute.ts";
|
|
6
|
+
import { profileChoices } from "./dialogs.ts";
|
|
7
|
+
|
|
8
|
+
const PROFILE_NAME = /^[A-Za-z0-9._-]+$/;
|
|
9
|
+
const AGENT_DEFAULT = "(agent default)";
|
|
10
|
+
|
|
11
|
+
async function save(ctx: ExtensionContext, engine: Engine, agentDir: string, value: ConfigFile, onSaved: () => Promise<void>, what: string): Promise<boolean> {
|
|
12
|
+
try {
|
|
13
|
+
const path = await writeUserConfig(agentDir, value, engine.adapters);
|
|
14
|
+
await onSaved();
|
|
15
|
+
ctx.ui.notify(`Saved ${what} to ${path}.`, "info");
|
|
16
|
+
return true;
|
|
17
|
+
} catch (error) {
|
|
18
|
+
ctx.ui.notify(`Not saved:\n${(error as Error).message}`, "error");
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function editRole(ctx: ExtensionContext, engine: Engine, config: OrchestratorConfig, file: ConfigFile): Promise<ConfigFile | undefined> {
|
|
24
|
+
const roleLabels = ROLE_NAMES.map((role) => `${role}: ${config.roles[role]?.join(" → ") || "(not configured)"}`);
|
|
25
|
+
const pickedRole = await ctx.ui.select("Which role?", roleLabels);
|
|
26
|
+
if (!pickedRole) return undefined;
|
|
27
|
+
const role = pickedRole.split(":")[0] as RoleName;
|
|
28
|
+
const choices = await profileChoices(engine, config);
|
|
29
|
+
const picked: string[] = [];
|
|
30
|
+
const what = isMultiRole(role) ? "every listed profile takes part" : "ordered fallback chain";
|
|
31
|
+
for (;;) {
|
|
32
|
+
const remaining = choices.filter((choice) => !picked.includes(choice.profile.name));
|
|
33
|
+
const options = [
|
|
34
|
+
...remaining.map((choice) => choice.label),
|
|
35
|
+
...(picked.length > 0 ? [`Done (${picked.join(" → ")})`] : []),
|
|
36
|
+
"Clear the role",
|
|
37
|
+
];
|
|
38
|
+
const answer = await ctx.ui.select(`Profiles for ${role} (${what})`, options);
|
|
39
|
+
if (!answer) return undefined;
|
|
40
|
+
if (answer === "Clear the role") {
|
|
41
|
+
const roles = { ...(file.roles ?? {}) } as Record<string, string[]>;
|
|
42
|
+
delete roles[role];
|
|
43
|
+
return { ...file, roles };
|
|
44
|
+
}
|
|
45
|
+
if (answer.startsWith("Done")) break;
|
|
46
|
+
const choice = remaining.find((entry) => entry.label === answer);
|
|
47
|
+
if (choice) picked.push(choice.profile.name);
|
|
48
|
+
if (remaining.length === 1) break;
|
|
49
|
+
}
|
|
50
|
+
return picked.length > 0 ? withRole(file, role, picked) : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function addProfile(ctx: ExtensionContext, engine: Engine, file: ConfigFile): Promise<ConfigFile | undefined> {
|
|
54
|
+
const name = (await ctx.ui.input("Profile name (letters, digits, . _ -)", "codex-deep"))?.trim();
|
|
55
|
+
if (!name) return undefined;
|
|
56
|
+
if (!PROFILE_NAME.test(name)) {
|
|
57
|
+
ctx.ui.notify(`"${name}" is not a valid profile name.`, "error");
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
if (file.profiles?.[name]) {
|
|
61
|
+
ctx.ui.notify(`Profile ${name} already exists; remove it first or edit the file.`, "error");
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const agents: string[] = [];
|
|
65
|
+
for (const [id, adapter] of engine.adapters) {
|
|
66
|
+
if ((await engine.availability(id)).ok) agents.push(`${id} (${adapter.displayName})`);
|
|
67
|
+
}
|
|
68
|
+
if (agents.length === 0) {
|
|
69
|
+
ctx.ui.notify("No agent is available. Run /orchestrator doctor to see why.", "error");
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
const agentLabel = await ctx.ui.select("Agent", agents);
|
|
73
|
+
if (!agentLabel) return undefined;
|
|
74
|
+
const agent = agentLabel.split(" ")[0];
|
|
75
|
+
const adapter = engine.adapters.get(agent);
|
|
76
|
+
if (!adapter) return undefined;
|
|
77
|
+
|
|
78
|
+
let model: string | undefined;
|
|
79
|
+
const models = adapter.listModels ? await adapter.listModels(engine.hostEnv(agent, ctx.cwd)).catch(() => undefined) : undefined;
|
|
80
|
+
if (models && models.length > 0) {
|
|
81
|
+
const picked = await ctx.ui.select("Model", [AGENT_DEFAULT, ...models]);
|
|
82
|
+
if (!picked) return undefined;
|
|
83
|
+
model = picked === AGENT_DEFAULT ? undefined : picked;
|
|
84
|
+
} else {
|
|
85
|
+
model = (await ctx.ui.input("Model id (empty for the agent's default)", ""))?.trim() || undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let effort: string | undefined;
|
|
89
|
+
const levels = adapter.capabilities.effortLevels;
|
|
90
|
+
if (levels && levels.length > 0) {
|
|
91
|
+
const picked = await ctx.ui.select("Effort", [AGENT_DEFAULT, ...levels]);
|
|
92
|
+
if (!picked) return undefined;
|
|
93
|
+
effort = picked === AGENT_DEFAULT ? undefined : picked;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const timeoutText = (await ctx.ui.input("Timeout in minutes (empty for the default)", ""))?.trim();
|
|
97
|
+
const timeoutMinutes = timeoutText ? Number(timeoutText) : undefined;
|
|
98
|
+
if (timeoutMinutes !== undefined && (!Number.isInteger(timeoutMinutes) || timeoutMinutes < 1 || timeoutMinutes > 720)) {
|
|
99
|
+
ctx.ui.notify("The timeout must be a whole number of minutes between 1 and 720.", "error");
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const profile = { agent, ...(model ? { model } : {}), ...(effort ? { effort } : {}), ...(timeoutMinutes ? { timeoutMinutes } : {}) };
|
|
103
|
+
return { ...file, profiles: { ...(file.profiles ?? {}), [name]: profile } } as ConfigFile;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function removeProfile(ctx: ExtensionContext, file: ConfigFile): Promise<ConfigFile | undefined> {
|
|
107
|
+
const names = Object.keys(file.profiles ?? {});
|
|
108
|
+
if (names.length === 0) {
|
|
109
|
+
ctx.ui.notify("The user configuration defines no profiles.", "info");
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
const name = await ctx.ui.select("Remove which profile?", names);
|
|
113
|
+
if (!name) return undefined;
|
|
114
|
+
const usedBy = ROLE_NAMES.filter((role) => file.roles?.[role]?.includes(name));
|
|
115
|
+
const confirmed = await ctx.ui.confirm(
|
|
116
|
+
`Remove profile ${name}?`,
|
|
117
|
+
usedBy.length > 0 ? `It is also removed from the roles ${usedBy.join(", ")}.` : "No role uses it.",
|
|
118
|
+
);
|
|
119
|
+
if (!confirmed) return undefined;
|
|
120
|
+
const profiles = { ...(file.profiles ?? {}) };
|
|
121
|
+
delete profiles[name];
|
|
122
|
+
const roles = { ...(file.roles ?? {}) } as Record<string, string[]>;
|
|
123
|
+
for (const role of usedBy) {
|
|
124
|
+
const remaining = (roles[role] ?? []).filter((entry) => entry !== name);
|
|
125
|
+
if (remaining.length > 0) roles[role] = remaining;
|
|
126
|
+
else delete roles[role];
|
|
127
|
+
}
|
|
128
|
+
return { ...file, profiles, roles } as ConfigFile;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function editAsJson(ctx: ExtensionContext, path: string, file: ConfigFile): Promise<ConfigFile | undefined> {
|
|
132
|
+
const edited = await ctx.ui.editor(`Edit ${path} (save to apply, empty to cancel)`, `${JSON.stringify(file, null, 2)}\n`);
|
|
133
|
+
if (!edited?.trim()) return undefined;
|
|
134
|
+
try {
|
|
135
|
+
return JSON.parse(edited) as ConfigFile;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
ctx.ui.notify(`Not saved: invalid JSON: ${(error as Error).message}`, "error");
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** `/orchestrator config`: edits profiles and roles of the user configuration. */
|
|
143
|
+
export async function configureInteractively(
|
|
144
|
+
ctx: ExtensionContext,
|
|
145
|
+
engine: Engine,
|
|
146
|
+
config: OrchestratorConfig,
|
|
147
|
+
agentDir: string,
|
|
148
|
+
onSaved: () => Promise<void>,
|
|
149
|
+
): Promise<void> {
|
|
150
|
+
const file = await readUserConfig(agentDir);
|
|
151
|
+
if (file.error) {
|
|
152
|
+
ctx.ui.notify(`${file.path} ${file.error}; fix the file by hand first.`, "error");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (file.hasComments) {
|
|
156
|
+
const proceed = await ctx.ui.confirm("Rewrite the configuration?", `${file.path} contains comments; saving from here rewrites it without them.`);
|
|
157
|
+
if (!proceed) return;
|
|
158
|
+
}
|
|
159
|
+
const action = await ctx.ui.select("master-orchestrator configuration", ["Edit a role", "Add a profile", "Remove a profile", "Edit the file as JSON"]);
|
|
160
|
+
let next: ConfigFile | undefined;
|
|
161
|
+
let what = "the configuration";
|
|
162
|
+
if (action === "Edit a role") {
|
|
163
|
+
next = await editRole(ctx, engine, config, file.value);
|
|
164
|
+
what = "the role";
|
|
165
|
+
} else if (action === "Add a profile") {
|
|
166
|
+
next = await addProfile(ctx, engine, file.value);
|
|
167
|
+
what = "the profile";
|
|
168
|
+
} else if (action === "Remove a profile") {
|
|
169
|
+
next = await removeProfile(ctx, file.value);
|
|
170
|
+
} else if (action === "Edit the file as JSON") {
|
|
171
|
+
next = await editAsJson(ctx, file.path, file.value);
|
|
172
|
+
}
|
|
173
|
+
if (next) await save(ctx, engine, agentDir, next, onSaved, what);
|
|
174
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { describeProfile, listProfiles, type ResolvedProfile } from "../config/profiles.ts";
|
|
3
|
+
import { isMultiRole } from "../config/profiles.ts";
|
|
4
|
+
import type { OrchestratorConfig, RoleName } from "../config/schema.ts";
|
|
5
|
+
import { readUserConfig, withRole, writeUserConfig } from "../config/write.ts";
|
|
6
|
+
import type { Engine } from "../engine/execute.ts";
|
|
7
|
+
|
|
8
|
+
export interface ProfileChoice {
|
|
9
|
+
label: string;
|
|
10
|
+
profile: ResolvedProfile;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Selectable profiles whose agent can run, labelled for a picker. */
|
|
14
|
+
export async function profileChoices(engine: Engine, config: OrchestratorConfig): Promise<ProfileChoice[]> {
|
|
15
|
+
const choices: ProfileChoice[] = [];
|
|
16
|
+
for (const profile of listProfiles(config, engine.adapters)) {
|
|
17
|
+
const availability = await engine.availability(profile.agent);
|
|
18
|
+
if (!availability.ok) continue;
|
|
19
|
+
const warning = availability.warnings.length > 0 ? " ⚠" : "";
|
|
20
|
+
choices.push({ label: `${describeProfile(profile)}${warning}`, profile });
|
|
21
|
+
}
|
|
22
|
+
return choices;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function pickProfile(ctx: ExtensionContext, engine: Engine, config: OrchestratorConfig, title: string): Promise<string | undefined> {
|
|
26
|
+
const choices = await profileChoices(engine, config);
|
|
27
|
+
if (choices.length === 0) {
|
|
28
|
+
ctx.ui.notify("No agent is available. Run /orchestrator doctor to see why.", "error");
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
const picked = await ctx.ui.select(title, choices.map((choice) => choice.label));
|
|
32
|
+
return choices.find((choice) => choice.label === picked)?.profile.name;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const DONE = "Done";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Lets the user build the profile list of a role that is not configured, then offers to save it to the user
|
|
39
|
+
* configuration. Chain roles are ordered fallbacks; multi roles (critic, debater) take every listed profile.
|
|
40
|
+
*/
|
|
41
|
+
export async function pickRoleProfiles(
|
|
42
|
+
ctx: ExtensionContext,
|
|
43
|
+
engine: Engine,
|
|
44
|
+
config: OrchestratorConfig,
|
|
45
|
+
agentDir: string,
|
|
46
|
+
role: RoleName,
|
|
47
|
+
onSaved: () => Promise<void>,
|
|
48
|
+
): Promise<string[] | undefined> {
|
|
49
|
+
if (!ctx.hasUI) return undefined;
|
|
50
|
+
const choices = await profileChoices(engine, config);
|
|
51
|
+
if (choices.length === 0) {
|
|
52
|
+
ctx.ui.notify("No agent is available. Run /orchestrator doctor to see why.", "error");
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const multiple = isMultiRole(role);
|
|
56
|
+
const picked: string[] = [];
|
|
57
|
+
const what = multiple ? "every listed profile takes part" : "ordered fallback chain";
|
|
58
|
+
for (;;) {
|
|
59
|
+
const remaining = choices.filter((choice) => !picked.includes(choice.profile.name));
|
|
60
|
+
if (remaining.length === 0) break;
|
|
61
|
+
const options = [...remaining.map((choice) => choice.label), ...(picked.length > 0 ? [`${DONE} (${picked.join(", ")})`] : [])];
|
|
62
|
+
const answer = await ctx.ui.select(`Role "${role}" is not configured: pick a profile (${what})`, options);
|
|
63
|
+
if (answer === undefined) return undefined;
|
|
64
|
+
if (answer.startsWith(DONE)) break;
|
|
65
|
+
const choice = remaining.find((entry) => entry.label === answer);
|
|
66
|
+
if (choice) picked.push(choice.profile.name);
|
|
67
|
+
}
|
|
68
|
+
if (picked.length === 0) return undefined;
|
|
69
|
+
const file = await readUserConfig(agentDir);
|
|
70
|
+
if (file.error) {
|
|
71
|
+
ctx.ui.notify(`Not saving roles.${role}: ${file.path} ${file.error}`, "warning");
|
|
72
|
+
return picked;
|
|
73
|
+
}
|
|
74
|
+
const save = await ctx.ui.confirm(
|
|
75
|
+
`Save roles.${role}?`,
|
|
76
|
+
`Save roles.${role} = [${picked.join(", ")}] to ${file.path}?${file.hasComments ? "\nThe file is rewritten without its comments." : ""}`,
|
|
77
|
+
);
|
|
78
|
+
if (save) {
|
|
79
|
+
try {
|
|
80
|
+
await writeUserConfig(agentDir, withRole(file.value, role, picked), engine.adapters);
|
|
81
|
+
await onSaved();
|
|
82
|
+
ctx.ui.notify(`Saved roles.${role} to ${file.path}.`, "info");
|
|
83
|
+
} catch (error) {
|
|
84
|
+
ctx.ui.notify(`Could not save roles.${role}: ${(error as Error).message}`, "error");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return picked;
|
|
88
|
+
}
|