-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
71 lines (64 loc) · 2.44 KB
/
Copy pathindex.mjs
File metadata and controls
71 lines (64 loc) · 2.44 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
import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { z } from "zod";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const COMPANION = path.resolve(HERE, "./scripts/polycli-companion.bundle.mjs");
function tool(input) {
return input;
}
tool.schema = z;
// Exit code 2 is the companion's documented soft signal, not a hard failure: `health` with no
// healthy provider and `status --wait` timeouts both exit 2 while still emitting a valid JSON
// envelope on stdout. The adapter must surface that envelope so the opencode agent can reason about
// it (anyHealthy:false / waitTimedOut:true) instead of seeing a thrown tool error. Every other
// non-zero exit (1/4/5/crash) is a real failure and is propagated.
export function isHardCompanionFailure(status) {
return status !== 0 && status !== 2;
}
export function runCompanion(argv, { spawn = spawnSync } = {}) {
const result = spawn(process.execPath, [COMPANION, ...argv], {
cwd: process.cwd(),
encoding: "utf8",
});
if (result.error) {
throw result.error;
}
if (isHardCompanionFailure(result.status)) {
const detail = String(result.stdout || result.stderr || "").trim() || `polycli companion exited with status ${result.status}`;
const error = new Error(detail);
error.status = result.status;
error.stdout = result.stdout;
error.stderr = result.stderr;
throw error;
}
return result.stdout;
}
export const PolycliPlugin = async () => ({
tool: {
polycli_run: tool({
description: "Run a polycli companion subcommand such as setup, health, ask, rescue, review, adversarial-review, status, result, cancel, timing, debug, or sessions.",
args: {
argv: tool.schema.array(tool.schema.string()),
},
async execute(args) {
return runCompanion(args.argv);
},
}),
polycli_timing: tool({
description: "Read stored polycli timing history and aggregate summaries.",
args: {
provider: tool.schema.string().optional(),
history: tool.schema.number().optional(),
json: tool.schema.boolean().optional(),
},
async execute(args) {
const argv = ["timing"];
if (args.provider) argv.push("--provider", args.provider);
if (args.history != null) argv.push("--history", String(args.history));
if (args.json) argv.push("--json");
return runCompanion(argv);
},
}),
},
});