Refactor LLM verification and update dependencies
- Removed OpenAI and related dependencies from package.json and package-lock.json. - Simplified LLM verification logic to focus on OpenCode server. - Updated tests to reflect changes in LLM provider handling. - Added CI and CD workflows for automated processes in Gitea.
This commit is contained in:
+51
-149
@@ -4,14 +4,7 @@ import axios from 'axios';
|
||||
import { checkRequiredEnv, verifyGiteaToken, verifyCommentToken, verifyLLM, runPreflight } from './preflight.js';
|
||||
|
||||
const LLM_ENV_KEYS = [
|
||||
'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL',
|
||||
'CLAUDE_API_KEY', 'CLAUDE_BASE_URL', 'CLAUDE_MODEL',
|
||||
'GEMINI_API_KEY', 'GEMINI_BASE_URL', 'GEMINI_MODEL',
|
||||
'OLLAMA_BASE_URL', 'OLLAMA_MODEL',
|
||||
'AMAZONQ_API_KEY', 'AMAZONQ_BASE_URL', 'AMAZONQ_MODEL',
|
||||
'OPENCODE_BASE_URL', 'OPENCODE_MODEL', 'OPENCODE_PROVIDER',
|
||||
'OPENCODE_SERVER_USERNAME', 'OPENCODE_SERVER_PASSWORD',
|
||||
'OPENCODE_SKIP_TLS_VERIFY',
|
||||
];
|
||||
|
||||
function clearLLMEnv() {
|
||||
@@ -51,7 +44,9 @@ describe('verifyGiteaToken', () => {
|
||||
capturedOpts = opts;
|
||||
return { data: { full_name: 'owner/repo' } };
|
||||
});
|
||||
|
||||
const result = await verifyGiteaToken('tok', 'owner/repo');
|
||||
|
||||
assert.equal(result.ok, true);
|
||||
assert.ok(capturedUrl.includes('/api/v1/repos/owner/repo'));
|
||||
assert.equal(capturedOpts.headers['Authorization'], 'token tok');
|
||||
@@ -63,7 +58,9 @@ describe('verifyGiteaToken', () => {
|
||||
e.response = { status: 401 };
|
||||
throw e;
|
||||
});
|
||||
|
||||
const result = await verifyGiteaToken('bad', 'owner/repo');
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /HTTP 401/);
|
||||
});
|
||||
@@ -82,7 +79,9 @@ describe('verifyCommentToken', () => {
|
||||
capturedOpts = opts;
|
||||
return { data: { login: 'bot' } };
|
||||
});
|
||||
|
||||
const result = await verifyCommentToken('ctok');
|
||||
|
||||
assert.equal(result.ok, true);
|
||||
assert.ok(capturedUrl.endsWith('/api/v1/user'));
|
||||
assert.equal(capturedOpts.headers['Authorization'], 'token ctok');
|
||||
@@ -94,79 +93,25 @@ describe('verifyCommentToken', () => {
|
||||
e.response = { status: 401 };
|
||||
throw e;
|
||||
});
|
||||
|
||||
const result = await verifyCommentToken('bad');
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /HTTP 401/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('verifyLLM', () => {
|
||||
it('fails when no provider/key configured', async () => {
|
||||
it('fails when OpenCode is not configured', async () => {
|
||||
clearLLMEnv();
|
||||
|
||||
const result = await verifyLLM();
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /未設定/);
|
||||
assert.match(result.error, /OPENCODE_BASE_URL/);
|
||||
});
|
||||
|
||||
it('ok when an OpenAI-compatible key authenticates', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'k1,k2';
|
||||
let capturedUrl, capturedPayload, capturedHeaders;
|
||||
mock.method(axios, 'post', async (url, payload, opts) => {
|
||||
capturedUrl = url;
|
||||
capturedPayload = payload;
|
||||
capturedHeaders = opts.headers;
|
||||
return { data: { choices: [{ message: { content: 'ok' } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'openai');
|
||||
assert.equal(result.keyIndex, 1);
|
||||
assert.equal(result.total, 2);
|
||||
assert.ok(capturedUrl.endsWith('/chat/completions'));
|
||||
assert.equal(capturedPayload.max_tokens, 1);
|
||||
assert.equal(capturedHeaders['Authorization'], 'Bearer k1');
|
||||
});
|
||||
|
||||
it('tries the next key when the first one fails', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'bad,good';
|
||||
let calls = 0;
|
||||
mock.method(axios, 'post', async (_url, _payload, opts) => {
|
||||
calls += 1;
|
||||
if (opts.headers['Authorization'] === 'Bearer bad') throw new Error('401');
|
||||
return { data: { choices: [{ message: { content: 'ok' } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.keyIndex, 2);
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
|
||||
it('fails when all keys fail', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'k1,k2';
|
||||
mock.method(axios, 'post', async () => { throw new Error('401'); });
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /所有 2 把 openai API Key 驗證失敗/);
|
||||
});
|
||||
|
||||
it('sets anthropic-version header for claude', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.CLAUDE_API_KEY = 'ck';
|
||||
let capturedHeaders;
|
||||
mock.method(axios, 'post', async (_url, _payload, opts) => {
|
||||
capturedHeaders = opts.headers;
|
||||
return { data: { choices: [{ message: { content: 'ok' } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'claude');
|
||||
assert.equal(capturedHeaders['anthropic-version'], '2023-06-01');
|
||||
});
|
||||
|
||||
it('checks opencode server provider and model', async () => {
|
||||
it('checks OpenCode server provider and model', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
|
||||
process.env.OPENCODE_PROVIDER = 'google';
|
||||
@@ -177,13 +122,46 @@ describe('verifyLLM', () => {
|
||||
if (url.endsWith('/global/health')) return { data: { healthy: true, version: '1.17.7' } };
|
||||
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
|
||||
});
|
||||
|
||||
const result = await verifyLLM();
|
||||
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'opencode');
|
||||
assert.deepEqual(urls, ['http://opencode.local:4096/global/health', 'http://opencode.local:4096/config/providers']);
|
||||
});
|
||||
|
||||
it('passes an insecure https agent for opencode by default', async () => {
|
||||
it('fails when configured provider is missing', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
|
||||
process.env.OPENCODE_PROVIDER = 'google';
|
||||
mock.method(axios, 'get', async (url) => {
|
||||
if (url.endsWith('/global/health')) return { data: { healthy: true } };
|
||||
return { data: { providers: [{ id: 'anthropic', models: {} }] } };
|
||||
});
|
||||
|
||||
const result = await verifyLLM();
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /未設定 provider=google/);
|
||||
});
|
||||
|
||||
it('fails when configured model is missing', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
|
||||
process.env.OPENCODE_PROVIDER = 'google';
|
||||
process.env.OPENCODE_MODEL = 'gemini-2.5-pro';
|
||||
mock.method(axios, 'get', async (url) => {
|
||||
if (url.endsWith('/global/health')) return { data: { healthy: true } };
|
||||
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
|
||||
});
|
||||
|
||||
const result = await verifyLLM();
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /未列出 model=gemini-2.5-pro/);
|
||||
});
|
||||
|
||||
it('passes an insecure https agent by default', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENCODE_BASE_URL = 'https://opencode.local:4096';
|
||||
const agents = [];
|
||||
@@ -192,106 +170,30 @@ describe('verifyLLM', () => {
|
||||
if (url.endsWith('/global/health')) return { data: { healthy: true } };
|
||||
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
|
||||
});
|
||||
|
||||
const result = await verifyLLM();
|
||||
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(agents.length, 2);
|
||||
assert.equal(agents[0].options.rejectUnauthorized, false);
|
||||
assert.equal(agents[1].options.rejectUnauthorized, false);
|
||||
});
|
||||
|
||||
it('passes an insecure https agent for opencode when TLS skip is any non-false value', async () => {
|
||||
for (const value of ['true', '', '0', 'yes', '1', 'on']) {
|
||||
clearLLMEnv();
|
||||
mock.restoreAll();
|
||||
process.env.OPENCODE_BASE_URL = 'https://opencode.local:4096';
|
||||
process.env.OPENCODE_SKIP_TLS_VERIFY = value;
|
||||
const agents = [];
|
||||
mock.method(axios, 'get', async (url, opts) => {
|
||||
agents.push(opts.httpsAgent);
|
||||
if (url.endsWith('/global/health')) return { data: { healthy: true } };
|
||||
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(agents.length, 2);
|
||||
assert.equal(agents[0].options.rejectUnauthorized, false);
|
||||
assert.equal(agents[1].options.rejectUnauthorized, false);
|
||||
}
|
||||
});
|
||||
|
||||
it('does not pass an insecure https agent for opencode when TLS verification is enabled', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENCODE_BASE_URL = 'https://opencode.local:4096';
|
||||
process.env.OPENCODE_SKIP_TLS_VERIFY = 'false';
|
||||
const agents = [];
|
||||
mock.method(axios, 'get', async (url, opts) => {
|
||||
agents.push(opts.httpsAgent);
|
||||
if (url.endsWith('/global/health')) return { data: { healthy: true } };
|
||||
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.deepEqual(agents, [undefined, undefined]);
|
||||
});
|
||||
|
||||
it('checks openai GPT-5.5 with Responses API', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'sk-test';
|
||||
process.env.OPENAI_MODEL = 'GPT-5.5';
|
||||
let capturedUrl, capturedPayload;
|
||||
mock.method(axios, 'post', async (url, payload) => {
|
||||
capturedUrl = url;
|
||||
capturedPayload = payload;
|
||||
return { data: { output_text: 'o' } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'openai');
|
||||
assert.equal(capturedUrl, 'https://api.openai.com/v1/responses');
|
||||
assert.equal(capturedPayload.model, 'GPT-5.5');
|
||||
assert.equal(capturedPayload.max_output_tokens, 1);
|
||||
});
|
||||
|
||||
it('checks base URL connectivity for ollama (no key)', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OLLAMA_BASE_URL = 'http://ollama.local/v1';
|
||||
let capturedUrl;
|
||||
mock.method(axios, 'get', async (url) => {
|
||||
capturedUrl = url;
|
||||
return { data: { data: [] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'ollama');
|
||||
assert.ok(capturedUrl.endsWith('/models'));
|
||||
});
|
||||
|
||||
it('fails when ollama base URL is unreachable', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OLLAMA_BASE_URL = 'http://ollama.local/v1';
|
||||
mock.method(axios, 'get', async () => { throw new Error('ECONNREFUSED'); });
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /無法連線/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('runPreflight', () => {
|
||||
// Stub deps that all succeed; individual tests override one to fail.
|
||||
function makeDeps(overrides = {}) {
|
||||
return {
|
||||
checkEnv: () => ({ ok: true, missing: [] }),
|
||||
verifyToken: async () => ({ ok: true }),
|
||||
verifyComment: async () => ({ ok: true }),
|
||||
verifyRemote: () => ({ ok: true }),
|
||||
verifyLLMFn: async () => ({ ok: true, provider: 'openai', keyIndex: 1, total: 1 }),
|
||||
verifyLLMFn: async () => ({ ok: true, provider: 'opencode' }),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
it('returns false and stops early when required env is missing', async () => {
|
||||
// Config constants default to empty in the test environment, so the
|
||||
// required-env check fails before any network call is attempted.
|
||||
const result = await runPreflight();
|
||||
assert.equal(result, false);
|
||||
});
|
||||
@@ -337,7 +239,7 @@ describe('runPreflight', () => {
|
||||
|
||||
it('returns false when LLM verification fails', async () => {
|
||||
const result = await runPreflight('/ws', makeDeps({
|
||||
verifyLLMFn: async () => ({ ok: false, error: '所有 key 驗證失敗' }),
|
||||
verifyLLMFn: async () => ({ ok: false, error: 'OpenCode server 驗證失敗' }),
|
||||
}));
|
||||
assert.equal(result, false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user