Fix OpenCode backend invocation and diagnostics

This commit is contained in:
William Valentin
2026-02-22 17:51:56 -08:00
parent 9a9375ef5d
commit 7d0d8abec6
4 changed files with 178 additions and 2 deletions
+32
View File
@@ -59,6 +59,25 @@ describe('ExternalCliBackend', () => {
);
});
it('uses inferred run args for opencode backend', async () => {
mockExecFile.mockImplementation((_cmd, _args, _opts, callback) => {
if (typeof callback === 'function') {
callback(null, 'ok', '');
}
return {} as never;
});
const backend = new OpenCodeBackend('/usr/bin/opencode', []);
await backend.process({ prompt: 'hello', history: [] });
expect(mockExecFile).toHaveBeenCalledWith(
'/usr/bin/opencode',
['run', '--format', 'default', 'USER: hello'],
expect.any(Object),
expect.any(Function),
);
});
it('supports {prompt} substitution in configured args', async () => {
mockExecFile.mockImplementation((_cmd, _args, _opts, callback) => {
if (typeof callback === 'function') {
@@ -98,6 +117,19 @@ describe('ExternalCliBackend', () => {
.rejects.toThrow('returned no output');
});
it('includes stdout details when backend process fails', async () => {
mockExecFile.mockImplementation((_cmd, _args, _opts, callback) => {
if (typeof callback === 'function') {
callback(new Error('Command failed: opencode'), 'default agent "code-planner" is a subagent', '');
}
return {} as never;
});
const backend = new OpenCodeBackend('/usr/bin/opencode', []);
await expect(backend.process({ prompt: 'hello', history: [] }))
.rejects.toThrow('default agent "code-planner" is a subagent');
});
it('constructs default commands for opencode and gemini backends', () => {
const opencode = new OpenCodeBackend();
const gemini = new GeminiBackend();
+8 -1
View File
@@ -37,6 +37,9 @@ function inferArgs(name: ExternalBackendName, prompt: string): string[] {
if (name === 'claude_code') {
return ['--print', prompt];
}
if (name === 'opencode') {
return ['run', '--format', 'default', prompt];
}
return ['-p', prompt];
}
@@ -99,7 +102,11 @@ function execFileAsync(command: string, args: string[], timeoutMs: number): Prom
return new Promise((resolve, reject) => {
execFile(command, args, { timeout: timeoutMs, maxBuffer: 1024 * 1024 }, (error, stdout, stderr) => {
if (error) {
reject(new Error(`${error.message}${stderr ? `\n${stderr}` : ''}`));
const details = [stderr, stdout]
.map((entry) => entry.trim())
.filter((entry) => entry.length > 0)
.join('\n');
reject(new Error(details ? `${error.message}\n${details}` : error.message));
return;
}
resolve(stdout || '');