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:
2026-06-25 10:17:08 +00:00
parent 525f6f9350
commit 65f27f544b
11 changed files with 162 additions and 837 deletions
+10 -65
View File
@@ -1,52 +1,13 @@
import axios from 'axios';
import { getLLMConfig, getOpenCodeHttpsAgent } from './config.js';
import { recordUsage, recordRateLimit } from './usage.js';
import { recordUsage } from './usage.js';
import { line, error } from './log.js';
function isOpenAIGpt55(provider, model) {
return provider === 'openai' && /^gpt-5\.5(?:-|$)/i.test(model || '');
}
function chatEndpoint(baseURL, provider, model) {
const base = baseURL.replace(/\/$/, '');
return isOpenAIGpt55(provider, model) ? `${base}/responses` : `${base}/chat/completions`;
}
function chatPayload(provider, model, systemPrompt, userContent) {
if (isOpenAIGpt55(provider, model)) {
return { model, instructions: systemPrompt, input: userContent, temperature: 0.2 };
}
return { model, messages: [{ role: 'system', content: systemPrompt }, { role: 'user', content: userContent }], temperature: 0.2 };
}
function extractContent(provider, model, data) {
if (!isOpenAIGpt55(provider, model)) return data.choices[0].message.content;
if (typeof data.output_text === 'string') return data.output_text;
const parts = data.output?.flatMap(item => item.content || []) || [];
const text = parts
.map(part => {
if (typeof part.text === 'string') return part.text;
if (typeof part.content === 'string') return part.content;
return '';
})
.filter(Boolean)
.join('');
if (text) return text;
return data.choices?.[0]?.message?.content || '';
}
function opencodeModelConfig(model) {
const [providerID, modelID] = model.includes('/') ? model.split('/', 2) : [process.env.OPENCODE_PROVIDER || 'google', model];
return { providerID, modelID };
}
function applyOpenCodeAuth(headers) {
const password = process.env.OPENCODE_SERVER_PASSWORD;
if (!password) return;
const username = process.env.OPENCODE_SERVER_USERNAME || 'opencode';
headers['Authorization'] = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
}
function opencodeAxiosOptions(headers) {
return {
headers,
@@ -86,37 +47,21 @@ async function chatOpenCode(baseURL, model, systemPrompt, userContent, headers)
}
export async function chat(systemPrompt, userContent) {
const { provider, apiKeys, baseURL, model } = getLLMConfig();
if (!provider) throw new Error('未設定任何 LLM API Key');
const { provider, baseURL, model } = getLLMConfig();
if (!provider) throw new Error('未設定 OpenCode server,請設定 OPENCODE_BASE_URL');
line(`[LLM] provider=${provider} model=${model}`);
const headers = { 'Content-Type': 'application/json' };
if (provider === 'claude') headers['anthropic-version'] = '2023-06-01';
const shuffled = [...apiKeys].sort(() => Math.random() - 0.5);
for (let i = 0; i < shuffled.length; i++) {
if (provider !== 'ollama' && provider !== 'opencode') headers['Authorization'] = `Bearer ${shuffled[i]}`;
try {
if (provider === 'opencode') {
applyOpenCodeAuth(headers);
const { content, data } = await chatOpenCode(baseURL, model, systemPrompt, userContent, headers);
recordUsage(data);
return content;
}
const resp = await axios.post(
chatEndpoint(baseURL, provider, model),
chatPayload(provider, model, systemPrompt, userContent),
{ headers }
);
recordUsage(resp.data);
recordRateLimit(resp.headers);
return extractContent(provider, model, resp.data);
} catch (e) {
line(`[LLM] key[${i + 1}/${shuffled.length}] 失敗: ${e.message}`);
}
try {
const { content, data } = await chatOpenCode(baseURL, model, systemPrompt, userContent, headers);
recordUsage(data);
return content;
} catch (e) {
line(`[LLM] OpenCode 呼叫失敗: ${e.message}`);
}
error('[LLM] 所有 API Key 均失敗,終止流程');
error('[LLM] OpenCode 呼叫失敗,終止流程');
process.exit(1);
}