-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine-profiles.ts
More file actions
319 lines (289 loc) · 9.87 KB
/
Copy pathengine-profiles.ts
File metadata and controls
319 lines (289 loc) · 9.87 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import fs from "node:fs";
import path from "node:path";
/** Minimum orchestrator timeout for agent CLI runs (15 minutes). */
export const DEFAULT_AGENT_TIMEOUT_MS = 900_000;
export const SUPPORTED_ENGINES = [
"claude",
"cline",
"aider",
"openhands",
"openhands-sdk",
"opencode",
"antigravity",
"copilot",
"cursor",
"openclaw",
] as const;
export type AgentEngine = (typeof SUPPORTED_ENGINES)[number];
export interface EngineCommandOptions {
workspace?: string;
/** Per-agent task timeout passed to engines that support it (e.g. Cline `-t`). */
timeoutSeconds?: number;
/** Dual-agent loop role; used for phase-specific file attachments. */
phase?: "developer" | "auditor";
}
export const LOOP_TOOL_HINT =
"\n\nIMPORTANT: Execute the agent prompt immediately. Write your complete JSON response to the Write Target file named in the prompt. Do not only reply in chat or stdout.";
export const PIPE_TASK = "Execute the piped agent prompt immediately." + LOOP_TOOL_HINT;
type PromptDelivery = "pipe-file" | "inline-prompt" | "prompt-file-arg" | "script";
type WorkspaceBinding =
| { mode: "none" }
| { mode: "cwd-only" }
| { mode: "flag"; flag: string };
export interface PhaseFileBinding {
readTargets: { developer: string[]; auditor: string[] };
writeFlag: string;
readFlag: string;
}
export interface EngineProfile {
id: AgentEngine;
binary: string | { win32: string; default: string };
delivery: PromptDelivery;
argvPrefix?: string[];
promptFlag?: string;
/** Args placed immediately before the prompt flag/body (e.g. cursor `-p --trust`). */
promptPrefixArgs?: string[];
/** When piping stdin, prefix the tail task with this flag (e.g. `-p`). */
pipePromptFlag?: string;
promptSuffixArgs?: string[];
headlessArgs?: string[];
autoApproveArgs?: string[];
workspace?: WorkspaceBinding;
timeoutFlag?: string;
phaseFiles?: PhaseFileBinding;
env?: Record<string, string>;
scriptPath?: string;
doctor?: { mode: "path" } | { mode: "python-import"; module: string };
}
export function resolveBinary(profile: EngineProfile): string {
if (typeof profile.binary === "string") return profile.binary;
return process.platform === "win32" ? profile.binary.win32 : profile.binary.default;
}
function shellQuote(value: string): string {
if (process.platform === "win32") {
return `"${value.replace(/"/g, '\\"')}"`;
}
return `'${value.replace(/'/g, `'\\''`)}'`;
}
function workspacePath(workspace: string | undefined, name: string): string {
return workspace ? path.join(workspace, name) : name;
}
function phaseWriteTarget(phase: "developer" | "auditor"): string {
return phase === "developer" ? "developer_output.json" : "auditor_output.json";
}
function pipePrefix(promptPath: string): string {
return process.platform === "win32"
? `type ${shellQuote(promptPath)} | `
: `cat ${shellQuote(promptPath)} | `;
}
function envPrefix(env: Record<string, string>): string {
if (process.platform === "win32") {
return `${Object.entries(env)
.map(([key, value]) => `set ${key}=${value}`)
.join("&&")}&& `;
}
return `${Object.entries(env)
.map(([key, value]) => `${key}=${shellQuote(value)}`)
.join(" ")} `;
}
function workspaceArgs(profile: EngineProfile, workspace: string | undefined): string[] {
if (!profile.workspace || profile.workspace.mode === "none" || !workspace) return [];
if (profile.workspace.mode === "cwd-only") return [];
return [`${profile.workspace.flag} ${shellQuote(path.resolve(workspace))}`];
}
function phaseFileArgs(
binding: PhaseFileBinding,
phase: "developer" | "auditor",
workspace: string | undefined,
): string[] {
const target = phaseWriteTarget(phase);
const reads = phase === "developer" ? binding.readTargets.developer : binding.readTargets.auditor;
return [
`${binding.writeFlag} ${shellQuote(workspacePath(workspace, target))}`,
...reads.map((file) => `${binding.readFlag} ${shellQuote(workspacePath(workspace, file))}`),
];
}
function timeoutArgs(profile: EngineProfile, timeoutSeconds: number): string[] {
if (!profile.timeoutFlag) return [];
return [`${profile.timeoutFlag} ${timeoutSeconds}`];
}
function pipeTail(profile: EngineProfile): string[] {
if (profile.pipePromptFlag) {
return [profile.pipePromptFlag, shellQuote(PIPE_TASK)];
}
return [shellQuote(PIPE_TASK)];
}
export function buildFromProfile(
profile: EngineProfile,
promptPath: string,
options: EngineCommandOptions,
packageRoot: string,
): string {
const workspace = options.workspace ? path.resolve(options.workspace) : undefined;
const phase = options.phase ?? "developer";
const timeoutSeconds = options.timeoutSeconds ?? 900;
const binary = resolveBinary(profile);
if (profile.delivery === "script") {
if (!profile.scriptPath) {
throw new Error(`Engine profile "${profile.id}" is missing scriptPath`);
}
const python = process.platform === "win32" ? "python" : "python3";
const script = path.join(packageRoot, profile.scriptPath);
return `${python} ${shellQuote(script)} --prompt-file ${shellQuote(promptPath)} --workspace ${shellQuote(workspace ?? process.cwd())}`;
}
const prompt = fs.readFileSync(promptPath, "utf8");
const promptWithHint = `${prompt}${LOOP_TOOL_HINT}`;
const parts: string[] = [];
if (profile.env) parts.push(envPrefix(profile.env));
if (profile.delivery === "pipe-file") parts.push(pipePrefix(promptPath));
parts.push(binary);
if (profile.argvPrefix?.length) parts.push(...profile.argvPrefix);
parts.push(...workspaceArgs(profile, workspace));
if (profile.headlessArgs?.length) parts.push(...profile.headlessArgs);
if (profile.autoApproveArgs?.length) parts.push(...profile.autoApproveArgs);
parts.push(...timeoutArgs(profile, timeoutSeconds));
if (profile.phaseFiles) parts.push(...phaseFileArgs(profile.phaseFiles, phase, workspace));
switch (profile.delivery) {
case "pipe-file":
parts.push(...pipeTail(profile));
break;
case "prompt-file-arg":
parts.push("-f", shellQuote(promptPath));
if (profile.promptSuffixArgs?.length) parts.push(...profile.promptSuffixArgs);
break;
case "inline-prompt":
if (profile.promptPrefixArgs?.length) parts.push(...profile.promptPrefixArgs);
if (profile.promptFlag) {
parts.push(profile.promptFlag, shellQuote(promptWithHint));
} else {
parts.push(shellQuote(promptWithHint));
}
if (profile.promptSuffixArgs?.length) parts.push(...profile.promptSuffixArgs);
break;
default:
break;
}
return parts.join(" ");
}
const LOOP_PHASE_FILES: PhaseFileBinding = {
readTargets: {
developer: ["shared_context.txt", "auditor_output.json"],
auditor: ["shared_context.txt", "developer_output.json"],
},
writeFlag: "--file",
readFlag: "--read",
};
const ATTACH_PHASE_FILES: PhaseFileBinding = {
readTargets: {
developer: ["shared_context.txt", "auditor_output.json"],
auditor: ["shared_context.txt", "developer_output.json"],
},
writeFlag: "-f",
readFlag: "-f",
};
/** Declarative profiles — extend this table to add engines without new builder code. */
export const ENGINE_PROFILES: Record<AgentEngine, EngineProfile> = {
claude: {
id: "claude",
binary: "claude",
delivery: "inline-prompt",
argvPrefix: ["--bare"],
promptFlag: "-p",
autoApproveArgs: [
'--allowedTools "Read,Edit,Write,Glob,Grep"',
"--permission-mode acceptEdits",
],
doctor: { mode: "path" },
},
cline: {
id: "cline",
binary: "cline",
delivery: "pipe-file",
headlessArgs: ["--json"],
autoApproveArgs: ["--auto-approve", "true"],
workspace: { mode: "flag", flag: "-c" },
timeoutFlag: "-t",
doctor: { mode: "path" },
},
aider: {
id: "aider",
binary: "aider",
delivery: "inline-prompt",
promptFlag: "--message",
autoApproveArgs: [
"--yes-always",
"--no-auto-commits",
"--no-show-release-notes",
"--no-stream",
"--no-git",
"--skip-sanity-check-repo",
"--no-suggest-shell-commands",
],
workspace: { mode: "cwd-only" },
phaseFiles: LOOP_PHASE_FILES,
doctor: { mode: "path" },
},
openhands: {
id: "openhands",
binary: "openhands",
delivery: "inline-prompt",
promptFlag: "-t",
headlessArgs: ["--headless", "--override-with-envs", "--exit-without-confirmation"],
env: { OPENHANDS_SUPPRESS_BANNER: "1" },
doctor: { mode: "path" },
},
"openhands-sdk": {
id: "openhands-sdk",
binary: process.platform === "win32" ? "python" : "python3",
delivery: "script",
scriptPath: "scripts/openhands-loop.py",
doctor: { mode: "python-import", module: "openhands.sdk" },
},
opencode: {
id: "opencode",
binary: "opencode",
delivery: "inline-prompt",
argvPrefix: ["run"],
headlessArgs: ["--dangerously-skip-permissions"],
workspace: { mode: "flag", flag: "--dir" },
phaseFiles: ATTACH_PHASE_FILES,
doctor: { mode: "path" },
},
antigravity: {
id: "antigravity",
binary: "agy",
delivery: "inline-prompt",
promptFlag: "-p",
autoApproveArgs: ["--dangerously-skip-permissions"],
doctor: { mode: "path" },
},
copilot: {
id: "copilot",
binary: "copilot",
delivery: "inline-prompt",
promptFlag: "-p",
autoApproveArgs: ["--allow-all-tools"],
workspace: { mode: "flag", flag: "--add-dir" },
doctor: { mode: "path" },
},
cursor: {
id: "cursor",
binary: { win32: "cursor-agent.cmd", default: "cursor-agent" },
delivery: "inline-prompt",
promptPrefixArgs: ["-p", "--trust", "--model", "auto"],
promptFlag: "-f",
doctor: { mode: "path" },
},
openclaw: {
id: "openclaw",
binary: "openclaw",
delivery: "inline-prompt",
argvPrefix: ["agent"],
promptFlag: "--message",
headlessArgs: ["--json", "--timeout", "60"],
doctor: { mode: "path" },
},
};
export function getEngineProfile(engine: AgentEngine): EngineProfile {
return ENGINE_PROFILES[engine];
}