@bridgebrew/harness-opencode 0.0.2 → 0.0.5
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/api.ts +9 -2
- package/package.json +4 -1
- package/server.ts +44 -8
package/api.ts
CHANGED
|
@@ -92,11 +92,16 @@ export const connect = ({
|
|
|
92
92
|
SILENT_MS,
|
|
93
93
|
);
|
|
94
94
|
};
|
|
95
|
+
// The server answers within `requestMs`, or the subscription fails (a stuck server would hang `open` forever).
|
|
96
|
+
const deadline = setTimeout(
|
|
97
|
+
() => abort.abort(new Error(`OpenCode did not answer its event stream in ${requestMs / 1000} s`)),
|
|
98
|
+
requestMs,
|
|
99
|
+
);
|
|
95
100
|
const response = await fetch(`${base}/api/event`, {
|
|
96
101
|
method: 'GET',
|
|
97
102
|
headers: { authorization, accept: 'text/event-stream' },
|
|
98
103
|
signal: abort.signal,
|
|
99
|
-
});
|
|
104
|
+
}).finally(() => clearTimeout(deadline));
|
|
100
105
|
if (!response.ok || !response.body) {
|
|
101
106
|
abort.abort();
|
|
102
107
|
throw new ApiError(
|
|
@@ -113,7 +118,9 @@ export const connect = ({
|
|
|
113
118
|
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: Node's fetch body is an async iterable of bytes, which its DOM type does not say
|
|
114
119
|
for await (const chunk of response.body as unknown as AsyncIterable<Uint8Array>) {
|
|
115
120
|
heard();
|
|
116
|
-
|
|
121
|
+
// Line endings are normalised on the buffer: a `\r\n` can be split across two chunks. A `\r` at its end
|
|
122
|
+
// waits for the next chunk.
|
|
123
|
+
buffer = (buffer + decoder.decode(chunk, { stream: true })).replace(/\r\n|\r(?!$)/g, '\n');
|
|
117
124
|
let cut: number;
|
|
118
125
|
while ((cut = buffer.indexOf('\n\n')) >= 0) {
|
|
119
126
|
const frame = buffer.slice(0, cut);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bridgebrew/harness-opencode",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lets the harness run OpenCode 2, one server per session.",
|
|
6
6
|
"keywords": [
|
|
@@ -61,5 +61,8 @@
|
|
|
61
61
|
"scripts": {
|
|
62
62
|
"test": "bridge plugin test .",
|
|
63
63
|
"typecheck": "bridge plugin check ."
|
|
64
|
+
},
|
|
65
|
+
"engines": {
|
|
66
|
+
"bridge": ">=0.0.1-nightly.20261114.1"
|
|
64
67
|
}
|
|
65
68
|
}
|
package/server.ts
CHANGED
|
@@ -396,6 +396,14 @@ const outputOf = (content: unknown) => {
|
|
|
396
396
|
return /^<subagent\b[^>]*>\n?([\s\S]*?)\n?<\/subagent>$/.exec(text.trim())?.[1] ?? text;
|
|
397
397
|
};
|
|
398
398
|
|
|
399
|
+
/** How a call that ran ends: `failed` when it failed, or when it ran a command that exited non-zero (`metadata.exit`
|
|
400
|
+
* or `exitCode`: OpenCode reports a shell's exit code as a success). */
|
|
401
|
+
const endOf = (type: string, metadata: unknown) => {
|
|
402
|
+
const { exit, exitCode } = recordOf(metadata);
|
|
403
|
+
const exited = [exit, exitCode].some((code) => typeof code === 'number' && code !== 0);
|
|
404
|
+
return type === 'session.tool.success' && !exited ? 'completed' : 'failed';
|
|
405
|
+
};
|
|
406
|
+
|
|
399
407
|
/** An ask's title: the command for a shell, the files for an edit, the folder for another directory, else the action
|
|
400
408
|
* (an MCP tool's is `<server>_<tool>`) and what it names. */
|
|
401
409
|
export const askTitle = (action: string, resources: string[]) => {
|
|
@@ -420,6 +428,22 @@ const tailOf = (stderr: string) => {
|
|
|
420
428
|
return lines ? `: ${lines}` : '';
|
|
421
429
|
};
|
|
422
430
|
|
|
431
|
+
/** The line of a failed command's error output that says why: the last error Node or the CLI printed (`Error: …`,
|
|
432
|
+
* `TypeError [ERR_…]: …`, `error: …`), else the last line that is neither a stack frame nor Node's `Node.js v…` line,
|
|
433
|
+
* else its last line. Node prints its version after the error, which alone tells nothing. */
|
|
434
|
+
const failureOf = (stderr: string) => {
|
|
435
|
+
const lines = stderr
|
|
436
|
+
.trim()
|
|
437
|
+
.split('\n')
|
|
438
|
+
.map((line) => line.trim());
|
|
439
|
+
return (
|
|
440
|
+
lines.findLast((line) => /^(?:Uncaught )?(?:\w*Error|error)(?: \[\w+\])?: /.test(line)) ??
|
|
441
|
+
lines.findLast((line) => line !== '' && !/^at /.test(line) && !/^Node\.js v\d/.test(line)) ??
|
|
442
|
+
lines.at(-1) ??
|
|
443
|
+
''
|
|
444
|
+
);
|
|
445
|
+
};
|
|
446
|
+
|
|
423
447
|
/** Runs `command args` through `spawn` to its end, or `ms` (then killed): its exit code (null when killed) and output. */
|
|
424
448
|
const runToEnd = async (spawn: Spawn, options: Parameters<Spawn>[0], ms: number) => {
|
|
425
449
|
const handle = await spawn(options);
|
|
@@ -520,13 +544,21 @@ export const opencodeDriver = ({
|
|
|
520
544
|
};
|
|
521
545
|
};
|
|
522
546
|
|
|
523
|
-
/** Reads `path` (a catalog: models) until it lists something or `catalog.forMs` passed.
|
|
547
|
+
/** Reads `path` (a catalog: models) until it lists something or `catalog.forMs` passed. A server still warming up
|
|
548
|
+
* answers 503: read again like an empty list, within the same deadline. */
|
|
524
549
|
const catalogOf = async (api: Api, path: string) => {
|
|
525
550
|
const until = Date.now() + catalog.forMs;
|
|
526
551
|
for (;;) {
|
|
527
|
-
const read = await api.get(path)
|
|
552
|
+
const read = await api.get(path).catch((error: unknown) => {
|
|
553
|
+
if (error instanceof ApiError && error.status === 503) return error;
|
|
554
|
+
throw error;
|
|
555
|
+
});
|
|
528
556
|
const data = isRecord(read) ? listOf(read.data) : [];
|
|
529
|
-
if (data.length
|
|
557
|
+
if (data.length) return data;
|
|
558
|
+
if (Date.now() >= until) {
|
|
559
|
+
if (read instanceof ApiError) throw read; // still warming up past the deadline: not an empty catalog
|
|
560
|
+
return data;
|
|
561
|
+
}
|
|
530
562
|
await new Promise((resolve) => setTimeout(resolve, catalog.everyMs));
|
|
531
563
|
}
|
|
532
564
|
};
|
|
@@ -607,7 +639,7 @@ export const opencodeDriver = ({
|
|
|
607
639
|
const said = ran.stdout.trim().split('\n')[0]?.trim();
|
|
608
640
|
const version = said ? versionOf(said) : undefined;
|
|
609
641
|
if (ran.code !== 0 || !said || !version) {
|
|
610
|
-
const why = ran.stderr
|
|
642
|
+
const why = failureOf(ran.stderr);
|
|
611
643
|
return {
|
|
612
644
|
installed: false,
|
|
613
645
|
message: `${command} --version ${ran.code === null ? 'did not answer' : 'failed'}${why ? `: ${why}` : ''}`,
|
|
@@ -978,7 +1010,8 @@ export const opencodeDriver = ({
|
|
|
978
1010
|
);
|
|
979
1011
|
};
|
|
980
1012
|
|
|
981
|
-
/** The context window of the model a step runs, read once per model from the session's server
|
|
1013
|
+
/** The context window of the model a step runs, read once per model from the session's server (again after a
|
|
1014
|
+
* failed read). */
|
|
982
1015
|
const windows = new Map<string, Promise<number | undefined>>();
|
|
983
1016
|
const windowOf = (ref: unknown) => {
|
|
984
1017
|
const mine = recordOf(ref);
|
|
@@ -997,7 +1030,10 @@ export const opencodeDriver = ({
|
|
|
997
1030
|
const size = sizes.input ?? sizes.context;
|
|
998
1031
|
return typeof size === 'number' && size > 0 ? size : undefined;
|
|
999
1032
|
})
|
|
1000
|
-
.catch(() =>
|
|
1033
|
+
.catch(() => {
|
|
1034
|
+
windows.delete(key); // a failed read is not kept: the next step reads again
|
|
1035
|
+
return undefined;
|
|
1036
|
+
}),
|
|
1001
1037
|
);
|
|
1002
1038
|
return windows.get(key)!;
|
|
1003
1039
|
};
|
|
@@ -1082,7 +1118,7 @@ export const opencodeDriver = ({
|
|
|
1082
1118
|
if (!known) return;
|
|
1083
1119
|
setTool({
|
|
1084
1120
|
...known,
|
|
1085
|
-
status: type
|
|
1121
|
+
status: endOf(type, body.metadata),
|
|
1086
1122
|
output:
|
|
1087
1123
|
type === 'session.tool.success'
|
|
1088
1124
|
? outputOf(body.content)
|
|
@@ -1220,7 +1256,7 @@ export const opencodeDriver = ({
|
|
|
1220
1256
|
: (str(recordOf(body.error).message) ?? 'The tool failed.');
|
|
1221
1257
|
setTool({
|
|
1222
1258
|
...known,
|
|
1223
|
-
status: type
|
|
1259
|
+
status: endOf(type, body.metadata),
|
|
1224
1260
|
output,
|
|
1225
1261
|
});
|
|
1226
1262
|
return;
|