opencode-worker 0.1.3 → 0.1.4
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/README.md +3 -2
- package/lib/mcp.mjs +43 -11
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,8 +48,9 @@ The worker's report is not proof: verify `diff` and rerun your own checks.
|
|
|
48
48
|
|
|
49
49
|
## MCP
|
|
50
50
|
|
|
51
|
-
Tools: `worker_start` (returns `jobId` immediately), `worker_status`, `worker_result`, `worker_cancel`.
|
|
52
|
-
|
|
51
|
+
Tools: `worker_start` (returns `jobId` immediately), `worker_wait`, `worker_status`, `worker_result`, `worker_cancel`.
|
|
52
|
+
|
|
53
|
+
Recommended flow: `worker_start` → `worker_wait` (repeat while it returns `status: "running"`). `worker_wait` blocks up to `timeoutSec` (default 50 s, max 120 s) and returns the full result once the job finishes, so clients don't burn model turns polling `worker_status`. The 50 s default stays under common 60 s client tool timeouts (e.g. Codex `tool_timeout_sec`).
|
|
53
54
|
|
|
54
55
|
```json
|
|
55
56
|
{
|
package/lib/mcp.mjs
CHANGED
|
@@ -50,19 +50,52 @@ const runSchema = {
|
|
|
50
50
|
required: ["dir", "prompt"],
|
|
51
51
|
};
|
|
52
52
|
const idSchema = { type: "object", properties: { jobId: { type: "string" } }, required: ["jobId"] };
|
|
53
|
+
const WAIT_DEFAULT_SEC = 50; // under common 60 s client tool timeouts (e.g. Codex)
|
|
54
|
+
const WAIT_MAX_SEC = 120;
|
|
55
|
+
const waitSchema = {
|
|
56
|
+
type: "object",
|
|
57
|
+
properties: {
|
|
58
|
+
jobId: { type: "string" },
|
|
59
|
+
timeoutSec: { type: "number", description: `Max seconds to block (default ${WAIT_DEFAULT_SEC}, max ${WAIT_MAX_SEC})` },
|
|
60
|
+
},
|
|
61
|
+
required: ["jobId"],
|
|
62
|
+
};
|
|
53
63
|
|
|
54
64
|
const TOOLS = [
|
|
55
|
-
{ name: "worker_start", description: "Start an isolated OpenCode worker job (free model by default). Returns jobId immediately.", inputSchema: runSchema },
|
|
56
|
-
{ name: "
|
|
57
|
-
{ name: "
|
|
65
|
+
{ name: "worker_start", description: "Start an isolated OpenCode worker job (free model by default). Returns jobId immediately. Then call worker_wait.", inputSchema: runSchema },
|
|
66
|
+
{ name: "worker_wait", description: "Block until the job finishes or timeoutSec elapses. Finished: returns the full result (same as worker_result). Still running: returns {status: \"running\"}; call worker_wait again. Prefer this over polling worker_status.", inputSchema: waitSchema },
|
|
67
|
+
{ name: "worker_status", description: "Get job status without waiting: running | done | failed | cancelled | lost (server died mid-run). Works across server restarts.", inputSchema: idSchema },
|
|
68
|
+
{ name: "worker_result", description: "Get the JSON result (diff, tool outputs, cost, tokens, actual model) of a finished job. Works across server restarts.", inputSchema: idSchema },
|
|
58
69
|
{ name: "worker_cancel", description: "Cancel a running job owned by this server (SIGTERM, then SIGKILL).", inputSchema: idSchema },
|
|
59
70
|
];
|
|
60
71
|
|
|
72
|
+
function resultOf(job) {
|
|
73
|
+
if (job.status === "running") return textResult({ jobId: job.jobId, status: "running" }, true);
|
|
74
|
+
if (job.status === "lost") return textResult({ ok: false, errors: [{ type: "lost", detail: "server exited before the job finished; inspect <dir>/.opencode-worker/runs" }] }, true);
|
|
75
|
+
return textResult({ jobId: job.jobId, status: job.status, ...(job.result ?? { ok: false, errors: [job.error] }) }, !job.result?.ok);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function waitFor(jobId, timeoutSec) {
|
|
79
|
+
const sec = Math.min(Math.max(Number(timeoutSec) || WAIT_DEFAULT_SEC, 1), WAIT_MAX_SEC);
|
|
80
|
+
const deadline = Date.now() + sec * 1000;
|
|
81
|
+
const live = jobs.get(jobId);
|
|
82
|
+
if (live) await Promise.race([live.done, new Promise((r) => setTimeout(r, sec * 1000).unref())]);
|
|
83
|
+
// Jobs owned by another server process: watch the persisted record.
|
|
84
|
+
let job = load(jobId);
|
|
85
|
+
while (job?.status === "running" && !live && Date.now() < deadline) {
|
|
86
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
87
|
+
job = load(jobId);
|
|
88
|
+
}
|
|
89
|
+
if (!job) return textResult({ error: "unknown jobId" }, true);
|
|
90
|
+
if (job.status === "running") return textResult({ jobId, status: "running", waitedSec: sec, hint: "call worker_wait again" });
|
|
91
|
+
return resultOf(job);
|
|
92
|
+
}
|
|
93
|
+
|
|
61
94
|
function textResult(obj, isError = false) {
|
|
62
95
|
return { content: [{ type: "text", text: JSON.stringify(obj, null, 2) }], isError };
|
|
63
96
|
}
|
|
64
97
|
|
|
65
|
-
function callTool(name, args, opts) {
|
|
98
|
+
async function callTool(name, args, opts) {
|
|
66
99
|
if (name === "worker_start") {
|
|
67
100
|
const record = {
|
|
68
101
|
jobId: randomUUID(), status: "running", serverPid: process.pid,
|
|
@@ -81,15 +114,12 @@ function callTool(name, args, opts) {
|
|
|
81
114
|
jobs.set(record.jobId, { record, controller, done });
|
|
82
115
|
return textResult({ jobId: record.jobId, status: "running" });
|
|
83
116
|
}
|
|
117
|
+
if (name === "worker_wait") return waitFor(args?.jobId, args?.timeoutSec);
|
|
84
118
|
const job = load(args?.jobId);
|
|
85
119
|
if (!job) return textResult({ error: "unknown jobId" }, true);
|
|
86
120
|
const { jobId, status, startedAt, finishedAt, dir } = job;
|
|
87
121
|
if (name === "worker_status") return textResult({ jobId, status, startedAt, finishedAt, dir });
|
|
88
|
-
if (name === "worker_result")
|
|
89
|
-
if (status === "running") return textResult({ jobId, status }, true);
|
|
90
|
-
if (status === "lost") return textResult({ ok: false, errors: [{ type: "lost", detail: "server exited before the job finished; inspect <dir>/.opencode-worker/runs" }] }, true);
|
|
91
|
-
return textResult(job.result ?? { ok: false, errors: [job.error] }, !job.result?.ok);
|
|
92
|
-
}
|
|
122
|
+
if (name === "worker_result") return resultOf(job);
|
|
93
123
|
if (name === "worker_cancel") {
|
|
94
124
|
const live = jobs.get(jobId);
|
|
95
125
|
live?.controller.abort();
|
|
@@ -121,8 +151,10 @@ export function serveMcp(opts = {}) {
|
|
|
121
151
|
if (method === "ping") return reply({});
|
|
122
152
|
if (method === "tools/list") return reply({ tools: TOOLS });
|
|
123
153
|
if (method === "tools/call") {
|
|
124
|
-
|
|
125
|
-
|
|
154
|
+
return callTool(params?.name, params?.arguments ?? {}, opts).then(
|
|
155
|
+
(r) => (r ? reply(r) : fail(-32602, `unknown tool: ${params?.name}`)),
|
|
156
|
+
(e) => fail(-32603, e.message),
|
|
157
|
+
);
|
|
126
158
|
}
|
|
127
159
|
fail(-32601, `method not found: ${method}`);
|
|
128
160
|
});
|