master-orchestrator 0.0.0-stage → 0.1.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 +11 -0
- package/LICENSE +21 -0
- package/README.md +174 -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/cursor.ts +242 -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 +225 -0
- package/src/adapters/grok.ts +328 -0
- package/src/adapters/muse.ts +281 -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 +31 -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 +203 -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,284 @@
|
|
|
1
|
+
import { sessionUuidFromRunDir } from "./claude.ts";
|
|
2
|
+
import {
|
|
3
|
+
clip,
|
|
4
|
+
detectByVersion,
|
|
5
|
+
getNumber,
|
|
6
|
+
getString,
|
|
7
|
+
isObject,
|
|
8
|
+
parseJsonAnswer,
|
|
9
|
+
parseJsonLine,
|
|
10
|
+
recordDenied,
|
|
11
|
+
recordSession,
|
|
12
|
+
recordText,
|
|
13
|
+
recordUsage,
|
|
14
|
+
standardFinalize,
|
|
15
|
+
} from "./common.ts";
|
|
16
|
+
import {
|
|
17
|
+
STATUS_PRECEDENCE,
|
|
18
|
+
type AdapterOutcome,
|
|
19
|
+
type AgentAdapter,
|
|
20
|
+
type AuthStatus,
|
|
21
|
+
type HostEnv,
|
|
22
|
+
type Invocation,
|
|
23
|
+
type NormalizedEvent,
|
|
24
|
+
type ParserState,
|
|
25
|
+
type RawRunResult,
|
|
26
|
+
type RunRequest,
|
|
27
|
+
type RunStatus,
|
|
28
|
+
} from "./types.ts";
|
|
29
|
+
|
|
30
|
+
const EFFORT_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
|
|
31
|
+
|
|
32
|
+
/** A stderr line that means a status the shared patterns do not recognise. */
|
|
33
|
+
export interface StderrRule {
|
|
34
|
+
pattern: RegExp;
|
|
35
|
+
status: RunStatus;
|
|
36
|
+
code: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** What differs between pi and the forks that keep its JSON event stream (oh-my-pi). */
|
|
40
|
+
export interface PiFamilyOptions {
|
|
41
|
+
/** Tool error texts that come from a rule, hook or extension that blocked the call. */
|
|
42
|
+
denialPattern: RegExp;
|
|
43
|
+
stderrRules: StderrRule[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface LastAssistant {
|
|
47
|
+
stopReason: string;
|
|
48
|
+
text: string;
|
|
49
|
+
errorMessage?: string;
|
|
50
|
+
errorStatus?: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const PI_OPTIONS: PiFamilyOptions = {
|
|
54
|
+
denialPattern: /^(tool execution was blocked|blocked\b)|\bblocked by\b/i,
|
|
55
|
+
stderrRules: [
|
|
56
|
+
{ pattern: /Unknown provider "[^"]*"/, status: "unavailable", code: "model_unavailable" },
|
|
57
|
+
{ pattern: /No models available|No model selected/, status: "auth_required", code: "auth_required" },
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* pi (`pi --mode json`). Events: a `session` header, then agent events (`message_end`, `tool_execution_*`,
|
|
63
|
+
* `turn_end`, `auto_retry_*`, `agent_settled`). The run succeeded when the last assistant message stopped with
|
|
64
|
+
* `stop`; an API failure is an assistant message with `stopReason: "error"` and the process still exits 0.
|
|
65
|
+
* SIGTERM disposes the session cleanly.
|
|
66
|
+
*/
|
|
67
|
+
export const piAdapter: AgentAdapter = {
|
|
68
|
+
id: "pi",
|
|
69
|
+
displayName: "pi",
|
|
70
|
+
binary: "pi",
|
|
71
|
+
testedVersions: ">=1.4.0 <1.7.0",
|
|
72
|
+
stability: "experimental",
|
|
73
|
+
liveChecked: "1.6.1 on 2026-11-25: analyze, implement, follow-up, cancellation, auth failure",
|
|
74
|
+
stopSignal: "SIGTERM",
|
|
75
|
+
trustFlag: "--approve",
|
|
76
|
+
capabilities: {
|
|
77
|
+
resume: true,
|
|
78
|
+
structuredOutput: false,
|
|
79
|
+
listModels: true,
|
|
80
|
+
effortLevels: EFFORT_LEVELS,
|
|
81
|
+
reportsCost: true,
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
detect(env: HostEnv) {
|
|
85
|
+
return detectByVersion(env, this.testedVersions);
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
/** `--list-models` lists only models whose provider has credentials, and starts no model turn. */
|
|
89
|
+
async checkAuth(env: HostEnv): Promise<AuthStatus> {
|
|
90
|
+
const result = await env.exec(env.bin, ["--list-models"], { cwd: env.cwd, timeoutMs: 60_000 });
|
|
91
|
+
if (result.error) return { state: "unknown", detail: result.error };
|
|
92
|
+
const models = parseModelTable(result.stdout);
|
|
93
|
+
if (models.length > 0) {
|
|
94
|
+
const providers = [...new Set(models.map((model) => model.split("/")[0]))];
|
|
95
|
+
return { state: "ok", detail: `${models.length} models from ${providers.join(", ")}` };
|
|
96
|
+
}
|
|
97
|
+
if (/No models available/i.test(`${result.stdout}\n${result.stderr}`)) {
|
|
98
|
+
return { state: "missing", detail: "no provider credentials (run `pi` and /login, or set a provider API key)" };
|
|
99
|
+
}
|
|
100
|
+
return { state: "unknown", detail: clip(`${result.stdout}\n${result.stderr}`.trim() || "no output", 120) };
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
async listModels(env: HostEnv): Promise<string[]> {
|
|
104
|
+
const result = await env.exec(env.bin, ["--list-models"], { cwd: env.cwd, timeoutMs: 60_000 });
|
|
105
|
+
if (result.error || result.code !== 0) return [];
|
|
106
|
+
return parseModelTable(result.stdout);
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
buildInvocation(req: RunRequest): Invocation {
|
|
110
|
+
// A first run names its session up front (pi creates it and warns "No project session found with id ...,
|
|
111
|
+
// creating"), so the id is known even when the run dies before the session header. pi has no cwd flag: the
|
|
112
|
+
// workspace is the spawn cwd. Model and thinking are restated on every run.
|
|
113
|
+
const args = ["--mode", "json", "--session-id", req.sessionId ?? sessionUuidFromRunDir(req.runDir)];
|
|
114
|
+
if (req.model) args.push("--model", req.model);
|
|
115
|
+
if (req.effort) args.push("--thinking", req.effort);
|
|
116
|
+
// Project-local extensions, skills and settings load only when the user trusts the project.
|
|
117
|
+
args.push(req.projectTrusted ? "--approve" : "--no-approve");
|
|
118
|
+
args.push(...(req.extraArgs ?? []));
|
|
119
|
+
return { args, cwd: req.workspace, env: req.env, stdin: req.prompt, files: [] };
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
parseEvent(line: string, state: ParserState): NormalizedEvent[] {
|
|
123
|
+
return parsePiFamilyEvent(line, state, PI_OPTIONS);
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
finalize(raw, state) {
|
|
127
|
+
return finalizePiFamily(raw, state, PI_OPTIONS);
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** Parses one line of the pi JSON event stream. */
|
|
132
|
+
export function parsePiFamilyEvent(line: string, state: ParserState, options: PiFamilyOptions): NormalizedEvent[] {
|
|
133
|
+
const event = parseJsonLine(line);
|
|
134
|
+
if (!event) {
|
|
135
|
+
if (line.trim()) state.nonJsonLines++;
|
|
136
|
+
return [];
|
|
137
|
+
}
|
|
138
|
+
switch (getString(event, "type")) {
|
|
139
|
+
case "session":
|
|
140
|
+
return recordSession(state, getString(event, "id"));
|
|
141
|
+
case "message_end":
|
|
142
|
+
return parseMessageEnd(event.message, state);
|
|
143
|
+
case "tool_execution_start": {
|
|
144
|
+
const tool = getString(event, "toolName") ?? "tool";
|
|
145
|
+
return [{ type: "activity", kind: "tool", summary: `${tool} ${clip(summarizeArgs(event.args), 80)}`.trim() }];
|
|
146
|
+
}
|
|
147
|
+
case "tool_execution_end": {
|
|
148
|
+
if (event.isError !== true) return [];
|
|
149
|
+
const text = resultText(event.result);
|
|
150
|
+
if (!options.denialPattern.test(text)) return [];
|
|
151
|
+
return [recordDenied(state, `${getString(event, "toolName") ?? "tool"}: ${clip(text, 160)}`)];
|
|
152
|
+
}
|
|
153
|
+
case "turn_end":
|
|
154
|
+
state.usage.turns = (state.usage.turns ?? 0) + 1;
|
|
155
|
+
return [];
|
|
156
|
+
case "auto_retry_start": {
|
|
157
|
+
const message = `retry ${getNumber(event, "attempt") ?? "?"}/${getNumber(event, "maxAttempts") ?? "?"}: ${getString(event, "errorMessage") ?? "error"}`;
|
|
158
|
+
state.warnings.push(message);
|
|
159
|
+
return [{ type: "warning", message: clip(message, 200) }];
|
|
160
|
+
}
|
|
161
|
+
case "auto_retry_end": {
|
|
162
|
+
const finalError = getString(event, "finalError");
|
|
163
|
+
if (event.success === false && finalError) state.errors.push(finalError);
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
case "compaction_end":
|
|
167
|
+
case "auto_compaction_end": {
|
|
168
|
+
const message = getString(event, "errorMessage");
|
|
169
|
+
if (!message) return [];
|
|
170
|
+
state.warnings.push(message);
|
|
171
|
+
return [{ type: "warning", message: clip(`compaction failed: ${message}`, 200) }];
|
|
172
|
+
}
|
|
173
|
+
case "notice": {
|
|
174
|
+
const level = getString(event, "level");
|
|
175
|
+
const message = getString(event, "message");
|
|
176
|
+
if (!message || (level !== "warning" && level !== "error")) return [];
|
|
177
|
+
state.warnings.push(message);
|
|
178
|
+
return [{ type: "warning", message: clip(message, 200) }];
|
|
179
|
+
}
|
|
180
|
+
case "agent_settled":
|
|
181
|
+
if (event.aborted === true) state.extra.aborted = true;
|
|
182
|
+
return [];
|
|
183
|
+
default:
|
|
184
|
+
return [];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Classifies a pi-family run from the last assistant message, the exit code and stderr. */
|
|
189
|
+
export function finalizePiFamily(raw: RawRunResult, state: ParserState, options: PiFamilyOptions): AdapterOutcome {
|
|
190
|
+
const last = state.extra.lastAssistant as LastAssistant | undefined;
|
|
191
|
+
if (last?.stopReason === "stop") {
|
|
192
|
+
state.finalText = last.text;
|
|
193
|
+
state.terminal = { ok: true };
|
|
194
|
+
} else if (last) {
|
|
195
|
+
// `length`, `toolUse`, `error`, `aborted`: the run ended without a complete answer.
|
|
196
|
+
const detail = last.errorMessage || `stopped with ${last.stopReason}`;
|
|
197
|
+
state.terminal = { ok: false, reason: `${last.stopReason}: ${detail}` };
|
|
198
|
+
if (last.errorMessage) state.errors.push(last.errorMessage);
|
|
199
|
+
}
|
|
200
|
+
if (state.extra.aborted === true) state.terminal = { ok: false, reason: "aborted: the agent run was aborted" };
|
|
201
|
+
|
|
202
|
+
const outcome = standardFinalize(raw, state, { finalText: state.finalText ?? "" });
|
|
203
|
+
if (outcome.status === "completed") {
|
|
204
|
+
const structured = parseJsonAnswer(outcome.finalText);
|
|
205
|
+
if (structured !== undefined) outcome.structuredOutput = structured;
|
|
206
|
+
return outcome;
|
|
207
|
+
}
|
|
208
|
+
const status = last?.errorStatus;
|
|
209
|
+
if (status === 401 || status === 403) escalate(outcome, "auth_required", `http_${status}: ${clip(last?.errorMessage ?? "", 160)}`);
|
|
210
|
+
if (status === 402 || status === 429) escalate(outcome, "quota", `http_${status}: ${clip(last?.errorMessage ?? "", 160)}`);
|
|
211
|
+
for (const rule of options.stderrRules) {
|
|
212
|
+
const match = raw.stderr.split(/\r?\n/).find((line) => rule.pattern.test(line));
|
|
213
|
+
if (!match) continue;
|
|
214
|
+
const reason = `${rule.code}: ${clip(match, 160)}`;
|
|
215
|
+
// A failed run keeps its status but gets the more precise reason.
|
|
216
|
+
if (rule.status === "failed" && outcome.status === "failed") outcome.reason = reason;
|
|
217
|
+
else escalate(outcome, rule.status, reason);
|
|
218
|
+
}
|
|
219
|
+
return outcome;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Parses `--list-models` output: a table with the columns provider, model, context, max-out, thinking, images. */
|
|
223
|
+
export function parseModelTable(stdout: string): string[] {
|
|
224
|
+
const models: string[] = [];
|
|
225
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
226
|
+
const match = line.trim().match(/^(\S+)\s+(\S+)\s+\S+\s+\S+\s+(?:yes|no)\s+(?:yes|no)$/);
|
|
227
|
+
if (match) models.push(`${match[1]}/${match[2]}`);
|
|
228
|
+
}
|
|
229
|
+
return models;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function parseMessageEnd(message: unknown, state: ParserState): NormalizedEvent[] {
|
|
233
|
+
if (!isObject(message) || getString(message, "role") !== "assistant") return [];
|
|
234
|
+
const content = Array.isArray(message.content) ? message.content : [];
|
|
235
|
+
const text = content
|
|
236
|
+
.filter((block) => getString(block, "type") === "text")
|
|
237
|
+
.map((block) => getString(block, "text") ?? "")
|
|
238
|
+
.join("\n")
|
|
239
|
+
.trim();
|
|
240
|
+
state.extra.lastAssistant = {
|
|
241
|
+
stopReason: getString(message, "stopReason") ?? "unknown",
|
|
242
|
+
text,
|
|
243
|
+
errorMessage: getString(message, "errorMessage"),
|
|
244
|
+
errorStatus: getNumber(message, "errorStatus"),
|
|
245
|
+
} satisfies LastAssistant;
|
|
246
|
+
const events: NormalizedEvent[] = [...recordText(state, text)];
|
|
247
|
+
const usage = isObject(message.usage) ? message.usage : undefined;
|
|
248
|
+
if (usage) {
|
|
249
|
+
const input = sumDefined([getNumber(usage, "input"), getNumber(usage, "cacheRead"), getNumber(usage, "cacheWrite")]);
|
|
250
|
+
events.push(recordUsage(state, { inputTokens: input, outputTokens: getNumber(usage, "output"), costUsd: getNumber(usage, "cost", "total") }));
|
|
251
|
+
}
|
|
252
|
+
return events;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function resultText(result: unknown): string {
|
|
256
|
+
if (typeof result === "string") return result;
|
|
257
|
+
const content = isObject(result) && Array.isArray(result.content) ? result.content : [];
|
|
258
|
+
return content
|
|
259
|
+
.map((block) => getString(block, "text") ?? "")
|
|
260
|
+
.join("\n")
|
|
261
|
+
.trim();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function summarizeArgs(args: unknown): string {
|
|
265
|
+
if (!isObject(args)) return typeof args === "string" ? args : "";
|
|
266
|
+
for (const key of ["command", "path", "file_path", "pattern", "url", "query"]) {
|
|
267
|
+
const value = getString(args, key);
|
|
268
|
+
if (value) return value;
|
|
269
|
+
}
|
|
270
|
+
return JSON.stringify(args);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Replaces the outcome's status when `status` comes first in the precedence order. */
|
|
274
|
+
function escalate(outcome: AdapterOutcome, status: RunStatus, reason: string): void {
|
|
275
|
+
if (STATUS_PRECEDENCE.indexOf(status) < STATUS_PRECEDENCE.indexOf(outcome.status)) {
|
|
276
|
+
outcome.status = status;
|
|
277
|
+
outcome.reason = reason;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function sumDefined(values: Array<number | undefined>): number | undefined {
|
|
282
|
+
const present = values.filter((value): value is number => value !== undefined);
|
|
283
|
+
return present.length > 0 ? present.reduce((sum, value) => sum + value, 0) : undefined;
|
|
284
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { agyAdapter } from "./agy.ts";
|
|
2
|
+
import { claudeAdapter } from "./claude.ts";
|
|
3
|
+
import { codexAdapter } from "./codex.ts";
|
|
4
|
+
import { cursorAdapter } from "./cursor.ts";
|
|
5
|
+
import { dshAdapter } from "./dsh.ts";
|
|
6
|
+
import { geminiAdapter } from "./gemini.ts";
|
|
7
|
+
import { grokAdapter } from "./grok.ts";
|
|
8
|
+
import { museAdapter } from "./muse.ts";
|
|
9
|
+
import { ompAdapter } from "./omp.ts";
|
|
10
|
+
import { opencodeAdapter } from "./opencode.ts";
|
|
11
|
+
import { piAdapter } from "./pi.ts";
|
|
12
|
+
import type { AgentAdapter } from "./types.ts";
|
|
13
|
+
|
|
14
|
+
/** The adapters that ship with the package, in the order `doctor` lists them. */
|
|
15
|
+
export const BUILTIN_ADAPTERS: readonly AgentAdapter[] = [
|
|
16
|
+
codexAdapter,
|
|
17
|
+
claudeAdapter,
|
|
18
|
+
opencodeAdapter,
|
|
19
|
+
piAdapter,
|
|
20
|
+
ompAdapter,
|
|
21
|
+
agyAdapter,
|
|
22
|
+
grokAdapter,
|
|
23
|
+
museAdapter,
|
|
24
|
+
dshAdapter,
|
|
25
|
+
geminiAdapter,
|
|
26
|
+
cursorAdapter,
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
export function adapterMap(adapters: readonly AgentAdapter[] = BUILTIN_ADAPTERS): Map<string, AgentAdapter> {
|
|
30
|
+
return new Map(adapters.map((adapter) => [adapter.id, adapter]));
|
|
31
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { TSchema } from "typebox";
|
|
2
|
+
|
|
3
|
+
export type Mode = "analyze" | "implement";
|
|
4
|
+
|
|
5
|
+
export type RunStatus =
|
|
6
|
+
| "completed" // final answer present, no denial ended the run
|
|
7
|
+
| "denied" // the run ended because a rule or hook of the user's blocked a required action
|
|
8
|
+
| "failed" // agent or model error, or exit 0 without a result
|
|
9
|
+
| "timeout"
|
|
10
|
+
| "cancelled"
|
|
11
|
+
| "auth_required"
|
|
12
|
+
| "quota"
|
|
13
|
+
| "unavailable"; // binary missing, startup failure, unsupported flag
|
|
14
|
+
|
|
15
|
+
/** When several conditions hold, the first status of this list that applies wins. */
|
|
16
|
+
export const STATUS_PRECEDENCE: readonly RunStatus[] = [
|
|
17
|
+
"cancelled",
|
|
18
|
+
"timeout",
|
|
19
|
+
"unavailable",
|
|
20
|
+
"auth_required",
|
|
21
|
+
"quota",
|
|
22
|
+
"denied",
|
|
23
|
+
"failed",
|
|
24
|
+
"completed",
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/** Statuses after which a role's fallback chain moves to the next profile. */
|
|
28
|
+
export const FALLBACK_STATUSES: readonly RunStatus[] = ["unavailable", "auth_required", "quota"];
|
|
29
|
+
|
|
30
|
+
export interface ExecResult {
|
|
31
|
+
code: number | null;
|
|
32
|
+
stdout: string;
|
|
33
|
+
stderr: string;
|
|
34
|
+
/** Set when the process could not be started (for example ENOENT). */
|
|
35
|
+
error?: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ExecOptions {
|
|
39
|
+
cwd?: string;
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
env?: Record<string, string | undefined>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** What an adapter may use to look at the host. Everything that touches the system goes through here. */
|
|
45
|
+
export interface HostEnv {
|
|
46
|
+
/** Executable to run: the configured `agents.<id>.bin` or the adapter's default binary. */
|
|
47
|
+
bin: string;
|
|
48
|
+
cwd: string;
|
|
49
|
+
env: Record<string, string | undefined>;
|
|
50
|
+
exec(command: string, args: string[], options?: ExecOptions): Promise<ExecResult>;
|
|
51
|
+
/** Absolute path of an executable found on PATH, or undefined. */
|
|
52
|
+
which(command: string): Promise<string | undefined>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface Detection {
|
|
56
|
+
available: boolean;
|
|
57
|
+
path?: string;
|
|
58
|
+
version?: string;
|
|
59
|
+
/** Whether `version` satisfies the adapter's `testedVersions`; undefined when the version is unknown. */
|
|
60
|
+
inTestedRange?: boolean;
|
|
61
|
+
error?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface AuthStatus {
|
|
65
|
+
state: "ok" | "missing" | "unknown";
|
|
66
|
+
detail?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface RunRequest {
|
|
70
|
+
workspace: string; // absolute
|
|
71
|
+
prompt: string; // final text, envelope already applied
|
|
72
|
+
runDir: string; // artifact directory
|
|
73
|
+
projectTrusted: boolean;
|
|
74
|
+
model?: string;
|
|
75
|
+
effort?: string;
|
|
76
|
+
sessionId?: string; // present on follow-ups
|
|
77
|
+
outputSchema?: object;
|
|
78
|
+
options?: Record<string, unknown>; // profile `options`, already validated against optionsSchema
|
|
79
|
+
extraArgs?: string[];
|
|
80
|
+
env?: Record<string, string>;
|
|
81
|
+
/** Adapter-specific configuration that is not a profile option (for example `agents.grok.persistTrust`). */
|
|
82
|
+
agentConfig?: Record<string, unknown>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A file the engine writes before the child starts. Paths are absolute and inside the run directory. */
|
|
86
|
+
export interface InvocationFile {
|
|
87
|
+
path: string;
|
|
88
|
+
content: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface Invocation {
|
|
92
|
+
/** Arguments after the executable. Never a shell string. */
|
|
93
|
+
args: string[];
|
|
94
|
+
cwd: string;
|
|
95
|
+
/** Environment additions. `null` removes a variable inherited from the parent. */
|
|
96
|
+
env?: Record<string, string | null>;
|
|
97
|
+
/** Payload written to stdin. Undefined means stdin is /dev/null. */
|
|
98
|
+
stdin?: string;
|
|
99
|
+
files?: InvocationFile[];
|
|
100
|
+
/** Files the engine reads after the run and passes to `finalize`, by name. */
|
|
101
|
+
outputFiles?: Record<string, string>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export type NormalizedEvent =
|
|
105
|
+
| { type: "session"; sessionId: string }
|
|
106
|
+
| { type: "activity"; kind: "tool" | "text" | "thinking"; summary: string }
|
|
107
|
+
| { type: "denied"; action: string }
|
|
108
|
+
| { type: "usage"; inputTokens?: number; outputTokens?: number; costUsd?: number }
|
|
109
|
+
| { type: "warning"; message: string };
|
|
110
|
+
|
|
111
|
+
export interface Usage {
|
|
112
|
+
inputTokens?: number;
|
|
113
|
+
outputTokens?: number;
|
|
114
|
+
costUsd?: number;
|
|
115
|
+
turns?: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Mutable state shared by `parseEvent` calls of one run and read by `finalize`. */
|
|
119
|
+
export interface ParserState {
|
|
120
|
+
sessionId?: string;
|
|
121
|
+
/** Assistant text in stream order; the last entry is the usual final answer. */
|
|
122
|
+
texts: string[];
|
|
123
|
+
/** Authoritative final answer when the stream carries one (for example a `result` event). */
|
|
124
|
+
finalText?: string;
|
|
125
|
+
denied: string[];
|
|
126
|
+
/** Error messages from the event stream. */
|
|
127
|
+
errors: string[];
|
|
128
|
+
warnings: string[];
|
|
129
|
+
usage: Usage;
|
|
130
|
+
/** Set by an explicit end-of-run event of the stream. */
|
|
131
|
+
terminal?: { ok: boolean; reason?: string };
|
|
132
|
+
/** Number of lines that were not valid JSON. */
|
|
133
|
+
nonJsonLines: number;
|
|
134
|
+
/** Adapter-specific data. */
|
|
135
|
+
extra: Record<string, unknown>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface RawRunResult {
|
|
139
|
+
exitCode: number | null;
|
|
140
|
+
signal: string | null;
|
|
141
|
+
/** Tail of stderr (the full log is in the run directory). */
|
|
142
|
+
stderr: string;
|
|
143
|
+
/** Contents of `Invocation.outputFiles`, undefined for files that do not exist. */
|
|
144
|
+
outputFiles: Record<string, string | undefined>;
|
|
145
|
+
timedOut: boolean;
|
|
146
|
+
cancelled: boolean;
|
|
147
|
+
/** Set when the process could not be started. */
|
|
148
|
+
spawnError?: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface AdapterOutcome {
|
|
152
|
+
status: RunStatus;
|
|
153
|
+
reason?: string; // short machine-readable code plus message, e.g. "empty_result: exit 0 without an answer"
|
|
154
|
+
finalText?: string;
|
|
155
|
+
structuredOutput?: unknown;
|
|
156
|
+
sessionId?: string;
|
|
157
|
+
deniedActions: string[];
|
|
158
|
+
usage?: Usage;
|
|
159
|
+
/** Conditions the user should know about, added to the run's warnings. */
|
|
160
|
+
warnings?: string[];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface AdapterCapabilities {
|
|
164
|
+
resume: boolean;
|
|
165
|
+
structuredOutput: boolean; // native JSON schema support
|
|
166
|
+
listModels: boolean;
|
|
167
|
+
effortLevels?: readonly string[];
|
|
168
|
+
reportsCost: boolean;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface AgentAdapter {
|
|
172
|
+
readonly id: string; // "codex", "claude", ...
|
|
173
|
+
readonly displayName: string;
|
|
174
|
+
readonly binary: string; // default executable name
|
|
175
|
+
readonly testedVersions: string; // semver range the adapter was verified against
|
|
176
|
+
readonly stability: "stable" | "experimental";
|
|
177
|
+
/** What real runs have confirmed so far (version, date, behaviors), shown by `doctor` while the adapter is experimental. */
|
|
178
|
+
readonly liveChecked?: string;
|
|
179
|
+
readonly stopSignal: "SIGINT" | "SIGTERM"; // first signal on cancellation
|
|
180
|
+
readonly optionsSchema?: TSchema; // adapter-specific profile options
|
|
181
|
+
readonly capabilities: AdapterCapabilities;
|
|
182
|
+
/** Per-run trust flag passed when the pi session trusts the project, for `doctor`. */
|
|
183
|
+
readonly trustFlag?: string;
|
|
184
|
+
/**
|
|
185
|
+
* A headless run that waits on an interactive prompt (a login URL or device code) never exits on its own. Called
|
|
186
|
+
* with the stderr received so far; a returned status makes the engine stop the run and report it.
|
|
187
|
+
*/
|
|
188
|
+
watchStderr?(stderr: string): { status: "auth_required" | "quota" | "unavailable"; reason: string } | undefined;
|
|
189
|
+
|
|
190
|
+
detect(env: HostEnv): Promise<Detection>;
|
|
191
|
+
checkAuth?(env: HostEnv): Promise<AuthStatus>;
|
|
192
|
+
listModels?(env: HostEnv): Promise<string[]>;
|
|
193
|
+
|
|
194
|
+
buildInvocation(req: RunRequest): Invocation;
|
|
195
|
+
parseEvent(line: string, state: ParserState): NormalizedEvent[];
|
|
196
|
+
finalize(raw: RawRunResult, state: ParserState): AdapterOutcome;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function createParserState(): ParserState {
|
|
200
|
+
return { texts: [], denied: [], errors: [], warnings: [], usage: {}, nonJsonLines: 0, extra: {} };
|
|
201
|
+
}
|