fix(OpenCode 呼叫): 降低並行請求並重試暫時性錯誤
CI / AI Code Review (pull_request) Failing after 3s

This commit is contained in:
2026-06-26 09:36:33 +00:00
parent 6c3e7b9d37
commit 93be261b90
3 changed files with 56 additions and 7 deletions
+23 -1
View File
@@ -4,7 +4,7 @@ import axios from 'axios';
import { extractBalancedJSON, extractJSONText } from '../llm.js';
const ENV_KEYS = [
'OPENCODE_BASE_URL', 'OPENCODE_MODEL', 'OPENCODE_PROVIDER',
'OPENCODE_BASE_URL', 'OPENCODE_MODEL', 'OPENCODE_PROVIDER', 'OPENCODE_RETRY_ATTEMPTS',
];
let saved = {};
@@ -87,6 +87,7 @@ describe('chat - OpenCode', async () => {
it('throws an error when OpenCode fails instead of exiting the process', async () => {
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
process.env.OPENCODE_RETRY_ATTEMPTS = '1';
mock.method(axios, 'post', async () => {
const err = new Error('Request failed with status code 500');
err.response = { status: 500, data: { error: 'provider overloaded' } };
@@ -98,6 +99,27 @@ describe('chat - OpenCode', async () => {
assert.equal(exitMock.mock.calls.length, 0);
});
it('retries transient OpenCode failures before returning content', async () => {
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
process.env.OPENCODE_RETRY_ATTEMPTS = '2';
let messageAttempts = 0;
mock.method(axios, 'post', async (url) => {
if (url.endsWith('/session')) return { data: { id: 'ses_test' } };
messageAttempts += 1;
if (messageAttempts === 1) {
const err = new Error('Request failed with status code 500');
err.response = { status: 500, data: { error: 'temporary failure' } };
throw err;
}
return { data: { parts: [{ type: 'text', text: 'ok after retry' }] } };
});
const result = await chat('sys', 'user');
assert.equal(result, 'ok after retry');
assert.equal(messageAttempts, 2);
});
});
describe('chatJSON', async () => {