fix(ai-review model validation): 驗證 model 並支援自動選模
This commit is contained in:
+18
-6
@@ -42,6 +42,16 @@ export const LLM_PROVIDER = 'cliproxyapi';
|
||||
|
||||
export const FINDINGS_PATH = '.gitea/ai-review/findings.json';
|
||||
export const EXCLUSIONS_PATH = '.gitea/ai-review/exclusions.json';
|
||||
const MODEL_NAME_RE = /^[A-Za-z0-9._-]+$/;
|
||||
|
||||
function normalizeModelName(raw) {
|
||||
const model = String(raw || '').trim();
|
||||
if (!model) return { model: null, modelError: null };
|
||||
if (!MODEL_NAME_RE.test(model)) {
|
||||
return { model: null, modelError: '無效的 model 參數,僅允許英數字、點、底線與連字號' };
|
||||
}
|
||||
return { model, modelError: null };
|
||||
}
|
||||
|
||||
let _insecureHttpsAgent = null;
|
||||
/**
|
||||
@@ -67,26 +77,28 @@ export const getOpenCodeHttpsAgent = getInsecureHttpsAgent;
|
||||
* 優先讀取 `INPUT_CLI_PROXY_API` / `CLI_PROXY_API` 作為 base URL(會 trim 並移除結尾斜線),
|
||||
* `INPUT_MODEL` / `CLI_PROXY_API_MODEL` / `MODEL` / `OPENCODE_MODEL`(依序 fallback,
|
||||
* 相容 action input、舊 OpenCode 設定與環境變數)作為可選模型名稱;若未提供,
|
||||
* 則交由 CLIProxyAPI 自動選擇模型,`INPUT_CLI_PROXY_API_KEY` / `CLI_PROXY_API_KEY`
|
||||
* 作為存取金鑰(會 trim)。
|
||||
* 則交由 CLIProxyAPI 自動選擇模型。若提供的名稱含非法字元,會被視為無效並於
|
||||
* `modelError` 回報,`INPUT_CLI_PROXY_API_KEY` / `CLI_PROXY_API_KEY` 作為存取金鑰(會 trim)。
|
||||
*
|
||||
* 若 base URL 無法解析出任何值,視為沒有可用的 proxy 設定:`provider`/`baseURL` 回傳 `null`、
|
||||
* `apiKeys` 回傳空陣列,但 `model`(若有解析到)仍會回傳,不會被清空。
|
||||
*
|
||||
* @returns {{ provider: ('cliproxyapi'|null), apiKeys: string[], baseURL: (string|null), model: (string|null), command: null }}
|
||||
* @returns {{ provider: ('cliproxyapi'|null), apiKeys: string[], baseURL: (string|null), model: (string|null), modelError: (string|null), command: null }}
|
||||
* 設定物件;`provider` 為 `null` 表示沒有可用的 proxy 設定。
|
||||
*/
|
||||
export function getLLMConfig() {
|
||||
const baseURL = String(process.env.INPUT_CLI_PROXY_API || process.env.CLI_PROXY_API || '').trim().replace(/\/$/, '');
|
||||
const model = process.env.INPUT_MODEL || process.env.CLI_PROXY_API_MODEL || process.env.MODEL || process.env.OPENCODE_MODEL || '';
|
||||
const rawModel = process.env.INPUT_MODEL || process.env.CLI_PROXY_API_MODEL || process.env.MODEL || process.env.OPENCODE_MODEL || '';
|
||||
const { model, modelError } = normalizeModelName(rawModel);
|
||||
const apiKey = String(process.env.INPUT_CLI_PROXY_API_KEY || process.env.CLI_PROXY_API_KEY || '').trim();
|
||||
if (!baseURL) return { provider: null, apiKeys: [], baseURL: null, model: model || null, command: null };
|
||||
if (!baseURL) return { provider: null, apiKeys: [], baseURL: null, model, modelError, command: null };
|
||||
|
||||
return {
|
||||
provider: LLM_PROVIDER,
|
||||
apiKeys: apiKey ? [apiKey] : [],
|
||||
baseURL,
|
||||
model: model || null,
|
||||
model,
|
||||
modelError,
|
||||
command: null,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user