@codemem/opencode-plugin 0.47.5 → 0.48.1
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/.opencode/lib/child-command.js +130 -0
- package/.opencode/lib/host-contract.js +2 -0
- package/.opencode/lib/memory-tool-contracts.json +1712 -0
- package/.opencode/lib/memory-tool-schema.js +105 -0
- package/.opencode/lib/memory-tools.js +404 -0
- package/.opencode/lib/opencode-v2-adapter.js +272 -113
- package/.opencode/lib/raw-event-delivery.js +29 -1
- package/.opencode/lib/raw-event-spool.d.ts +104 -0
- package/.opencode/lib/raw-event-spool.js +633 -26
- package/.opencode/lib/runtime.js +139 -144
- package/.opencode/package.json +1 -1
- package/.opencode/plugins/codemem.js +60 -3
- package/package.json +4 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
const KILL_GRACE_MS = 5_000;
|
|
4
|
+
|
|
5
|
+
const collectOutput = (proc) => {
|
|
6
|
+
const output = { stdout: "", stderr: "" };
|
|
7
|
+
// Guard stdio access: a hard spawn failure (ENOENT) can hand back null
|
|
8
|
+
// streams, and a synchronous null-deref here would reject the promise
|
|
9
|
+
// that callers await for a resolved { exitCode, stdout, stderr }.
|
|
10
|
+
if (proc.stdout) proc.stdout.on("data", (chunk) => { output.stdout += chunk; });
|
|
11
|
+
if (proc.stderr) proc.stderr.on("data", (chunk) => { output.stderr += chunk; });
|
|
12
|
+
// Absorb async stream errors (e.g. EPIPE when the child exits mid-write,
|
|
13
|
+
// or a spawn failure surfacing on a pipe). Without these handlers an
|
|
14
|
+
// unhandled stream "error" event would crash the host process. The
|
|
15
|
+
// child-level proc.once("error") still resolves the promise.
|
|
16
|
+
for (const stream of [proc.stdin, proc.stdout, proc.stderr]) {
|
|
17
|
+
if (stream && typeof stream.on === "function") stream.on("error", () => {});
|
|
18
|
+
}
|
|
19
|
+
return output;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** Write and close stdin; returns a failure result, or null when stdin is closed. */
|
|
23
|
+
const writeStdin = (proc, stdinText) => {
|
|
24
|
+
const fail = (step, error) => {
|
|
25
|
+
try { proc.kill(); } catch { /* ignore */ }
|
|
26
|
+
return { exitCode: 1, stdout: "", stderr: `stdin ${step} failed: ${String(error)}` };
|
|
27
|
+
};
|
|
28
|
+
if (typeof stdinText === "string") {
|
|
29
|
+
try {
|
|
30
|
+
proc.stdin.write(stdinText);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return fail("write", error);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
proc.stdin.end();
|
|
37
|
+
} catch (error) {
|
|
38
|
+
return fail("close", error);
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** SIGTERM now, SIGKILL after a grace period; `clear` cancels the pending SIGKILL. */
|
|
44
|
+
const createTerminator = (proc) => {
|
|
45
|
+
let killTimer = null;
|
|
46
|
+
return {
|
|
47
|
+
terminate: () => {
|
|
48
|
+
try { proc.kill("SIGTERM"); } catch { /* ignore */ }
|
|
49
|
+
if (killTimer) return;
|
|
50
|
+
killTimer = setTimeout(() => {
|
|
51
|
+
try { proc.kill("SIGKILL"); } catch { /* ignore */ }
|
|
52
|
+
}, KILL_GRACE_MS);
|
|
53
|
+
if (killTimer.unref) killTimer.unref();
|
|
54
|
+
},
|
|
55
|
+
clear: () => {
|
|
56
|
+
if (killTimer) clearTimeout(killTimer);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const settleOnExit = (proc, { output, signal, timeoutMs, resolve }) => {
|
|
62
|
+
const terminator = createTerminator(proc);
|
|
63
|
+
const state = { timedOut: false, aborted: false, settled: false };
|
|
64
|
+
let timer = null;
|
|
65
|
+
const onAbort = () => {
|
|
66
|
+
state.aborted = true;
|
|
67
|
+
terminator.terminate();
|
|
68
|
+
};
|
|
69
|
+
const finish = (result) => {
|
|
70
|
+
if (state.settled) return;
|
|
71
|
+
state.settled = true;
|
|
72
|
+
if (timer) clearTimeout(timer);
|
|
73
|
+
terminator.clear();
|
|
74
|
+
signal?.removeEventListener("abort", onAbort);
|
|
75
|
+
resolve(result);
|
|
76
|
+
};
|
|
77
|
+
if (Number.isFinite(timeoutMs) && timeoutMs > 0) {
|
|
78
|
+
timer = setTimeout(() => {
|
|
79
|
+
state.timedOut = true;
|
|
80
|
+
terminator.terminate();
|
|
81
|
+
}, timeoutMs);
|
|
82
|
+
}
|
|
83
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
84
|
+
proc.once("exit", (exitCode) => {
|
|
85
|
+
if (state.aborted) {
|
|
86
|
+
finish({ exitCode: null, stdout: output.stdout, stderr: "aborted", aborted: true });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
finish({
|
|
90
|
+
exitCode: state.timedOut ? null : exitCode,
|
|
91
|
+
stdout: output.stdout,
|
|
92
|
+
stderr: state.timedOut ? "timeout" : output.stderr,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
proc.once("error", (err) => {
|
|
96
|
+
finish({ exitCode: 1, stdout: "", stderr: String(err) });
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Run one command and resolve `{ exitCode, stdout, stderr }`; never rejects.
|
|
102
|
+
* `exitCode` is null after a timeout (`stderr: "timeout"`) or an abort
|
|
103
|
+
* (`stderr: "aborted"`, `aborted: true`). Abort and timeout send SIGTERM,
|
|
104
|
+
* then SIGKILL if the child is still running five seconds later. `defaults`
|
|
105
|
+
* supplies cwd and timeoutMs when `options` leaves them undefined.
|
|
106
|
+
*/
|
|
107
|
+
export const runChildCommand = (cmd, options = {}, defaults = {}) => {
|
|
108
|
+
const {
|
|
109
|
+
cwd = defaults.cwd,
|
|
110
|
+
env = process.env,
|
|
111
|
+
signal = null,
|
|
112
|
+
stdinText = null,
|
|
113
|
+
timeoutMs = defaults.timeoutMs,
|
|
114
|
+
} = options;
|
|
115
|
+
const [command, ...args] = cmd;
|
|
116
|
+
return new Promise((resolve) => {
|
|
117
|
+
if (signal?.aborted) {
|
|
118
|
+
resolve({ exitCode: null, stdout: "", stderr: "aborted", aborted: true });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const proc = nodeSpawn(command, args, { cwd, env, stdio: ["pipe", "pipe", "pipe"] });
|
|
122
|
+
const output = collectOutput(proc);
|
|
123
|
+
const stdinFailure = writeStdin(proc, stdinText);
|
|
124
|
+
if (stdinFailure) {
|
|
125
|
+
resolve(stdinFailure);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
settleOnExit(proc, { output, signal, timeoutMs, resolve });
|
|
129
|
+
});
|
|
130
|
+
};
|
|
@@ -75,6 +75,8 @@ export const V2_ADAPTER_DIAGNOSTICS = Object.freeze({
|
|
|
75
75
|
* @property {(input: CodememToolResult, output: object) => Promise<void>} handleToolResult
|
|
76
76
|
* @property {(code: string) => Promise<void>} reportDiagnostic
|
|
77
77
|
* @property {Record<string, {description: string, args: object, execute: Function}>} tools
|
|
78
|
+
* @property {{contracts: ReadonlyArray<{name: string, description: string, input: object}>, execute: Function, shouldRegister: Function} | null} memoryTools
|
|
79
|
+
* Native memory tools, or null unless CODEMEM_OPENCODE_NATIVE_TOOLS enables them.
|
|
78
80
|
*/
|
|
79
81
|
|
|
80
82
|
export const createRuntimeLocation = ({ project, directory, worktree }) => ({
|