This commit is contained in:
+23
-6
@@ -1,7 +1,7 @@
|
||||
import axios from 'axios';
|
||||
import { getLLMConfig, getOpenCodeHttpsAgent } from './config.js';
|
||||
import { recordUsage } from './usage.js';
|
||||
import { line, error } from './log.js';
|
||||
import { line } from './log.js';
|
||||
|
||||
/**
|
||||
* 將模型識別字串解析為 OpenCode API 所需的 provider 與 model 識別碼。
|
||||
@@ -49,6 +49,23 @@ function extractOpenCodeContent(data) {
|
||||
.join('');
|
||||
}
|
||||
|
||||
function summarizeErrorResponse(data) {
|
||||
if (data == null) return '';
|
||||
if (typeof data === 'string') return data.slice(0, 500);
|
||||
try {
|
||||
return JSON.stringify(data).slice(0, 500);
|
||||
} catch {
|
||||
return String(data).slice(0, 500);
|
||||
}
|
||||
}
|
||||
|
||||
function formatOpenCodeError(e) {
|
||||
const status = e.response?.status;
|
||||
const response = summarizeErrorResponse(e.response?.data);
|
||||
const statusText = status ? `HTTP ${status}` : e.message;
|
||||
return response ? `${statusText}: ${response}` : statusText;
|
||||
}
|
||||
|
||||
/**
|
||||
* 對 OpenCode server 執行一次完整對話:建立 session 後送出訊息並回傳結果。
|
||||
*
|
||||
@@ -91,8 +108,8 @@ async function chatOpenCode(baseURL, model, systemPrompt, userContent, headers)
|
||||
* 對 OpenCode server 送出一次對話請求並回傳模型純文字回應。
|
||||
*
|
||||
* 從設定取得 provider/baseURL/model;未設定 provider 時拋錯。成功時記錄
|
||||
* usage 並回傳內容。OpenCode 呼叫失敗時會記錄錯誤並以 `process.exit(1)`
|
||||
* 終止整個行程(不會回傳)。
|
||||
* usage 並回傳內容。OpenCode 呼叫失敗時會記錄錯誤並向外拋出,讓呼叫端
|
||||
* 決定是否降級、略過單一角色或終止整體流程。
|
||||
*
|
||||
* @param {string} systemPrompt - 系統提示詞。
|
||||
* @param {string} userContent - 使用者輸入內容。
|
||||
@@ -112,10 +129,10 @@ export async function chat(systemPrompt, userContent) {
|
||||
recordUsage(data);
|
||||
return content;
|
||||
} catch (e) {
|
||||
line(`[LLM] OpenCode 呼叫失敗: ${e.message}`);
|
||||
const message = formatOpenCodeError(e);
|
||||
line(`[LLM] OpenCode 呼叫失敗: ${message}`);
|
||||
throw new Error(message);
|
||||
}
|
||||
error('[LLM] OpenCode 呼叫失敗,終止流程');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user