openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
156 lines (155 loc) • 5.46 kB
JavaScript
import { j as resolveTimerTimeoutMs } from "./number-coercion-CJQ8TR--.js";
import "./number-runtime-DBLVDypr.js";
import { i as ZAI_CODING_GLOBAL_BASE_URL, n as ZAI_CODING_CN_BASE_URL, o as ZAI_DEFAULT_MODEL_ID, r as ZAI_CODING_DEFAULT_MODEL_ID, s as ZAI_GLOBAL_BASE_URL, t as ZAI_CN_BASE_URL } from "./model-definitions-B7gQmjzX.js";
//#region extensions/zai/detect.ts
const UNSUPPORTED_MODEL_ERROR_CODES = new Set(["1211", "1311"]);
function isUnsupportedModelResult(result) {
if (result.ok) return false;
if (result.status === 404) return true;
if (result.errorCode && UNSUPPORTED_MODEL_ERROR_CODES.has(result.errorCode)) return true;
if (result.status !== 400) return false;
const detail = `${result.errorCode ?? ""} ${result.errorMessage ?? ""}`.toLowerCase();
return /\bmodel\b.*\b(not found|unavailable|unsupported|does not exist)\b/.test(detail) || /模型.*(不存在|不支持|不可用)/.test(detail);
}
async function fetchWithTimeoutLocal(fetchFn, url, init, timeoutMs) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
return await fetchFn(url, {
...init,
signal: controller.signal
});
} finally {
clearTimeout(timeout);
}
}
async function probeZaiChatCompletions(params) {
try {
const res = await fetchWithTimeoutLocal(params.fetchFn ?? globalThis.fetch, `${params.baseUrl}/chat/completions`, {
method: "POST",
headers: {
authorization: `Bearer ${params.apiKey}`,
"content-type": "application/json"
},
body: JSON.stringify({
model: params.modelId,
stream: false,
max_tokens: 1,
messages: [{
role: "user",
content: "ping"
}]
})
}, params.timeoutMs);
if (res.ok) return { ok: true };
let errorCode;
let errorMessage;
try {
const json = await res.json();
const code = json?.error?.code ?? json?.code;
const msg = json?.error?.message ?? json?.msg ?? json?.message;
if (typeof code === "string") errorCode = code;
else if (typeof code === "number") errorCode = String(code);
if (typeof msg === "string") errorMessage = msg;
} catch {}
return {
ok: false,
status: res.status,
errorCode,
errorMessage
};
} catch {
return { ok: false };
}
}
async function detectZaiEndpoint(params) {
if (process.env.VITEST && !params.fetchFn) return null;
const timeoutMs = resolveTimerTimeoutMs(params.timeoutMs, 5e3);
const probeCandidates = (() => {
const general = [{
endpoint: "global",
baseUrl: ZAI_GLOBAL_BASE_URL,
modelId: ZAI_DEFAULT_MODEL_ID,
note: "Verified GLM-5.1 on global endpoint."
}, {
endpoint: "cn",
baseUrl: ZAI_CN_BASE_URL,
modelId: ZAI_DEFAULT_MODEL_ID,
note: "Verified GLM-5.1 on cn endpoint."
}];
const codingModels = [
{
endpoint: "coding-global",
baseUrl: ZAI_CODING_GLOBAL_BASE_URL,
modelId: ZAI_CODING_DEFAULT_MODEL_ID,
note: "Verified GLM-5.2 on coding-global endpoint."
},
{
endpoint: "coding-global",
baseUrl: ZAI_CODING_GLOBAL_BASE_URL,
modelId: "glm-5.1",
note: "Verified GLM-5.1 on coding-global endpoint; GLM-5.2 is unavailable.",
fallback: true
},
{
endpoint: "coding-cn",
baseUrl: ZAI_CODING_CN_BASE_URL,
modelId: ZAI_CODING_DEFAULT_MODEL_ID,
note: "Verified GLM-5.2 on coding-cn endpoint."
},
{
endpoint: "coding-cn",
baseUrl: ZAI_CODING_CN_BASE_URL,
modelId: "glm-5.1",
note: "Verified GLM-5.1 on coding-cn endpoint; GLM-5.2 is unavailable.",
fallback: true
}
];
const codingFallback = [{
endpoint: "coding-global",
baseUrl: ZAI_CODING_GLOBAL_BASE_URL,
modelId: "glm-4.7",
note: "Coding Plan endpoint verified, but this key/plan does not expose GLM-5.2 or GLM-5.1 there. Defaulting to GLM-4.7.",
fallback: true
}, {
endpoint: "coding-cn",
baseUrl: ZAI_CODING_CN_BASE_URL,
modelId: "glm-4.7",
note: "Coding Plan CN endpoint verified, but this key/plan does not expose GLM-5.2 or GLM-5.1 there. Defaulting to GLM-4.7.",
fallback: true
}];
switch (params.endpoint) {
case "global": return general.filter((candidate) => candidate.endpoint === "global");
case "cn": return general.filter((candidate) => candidate.endpoint === "cn");
case "coding-global": return [...codingModels.filter((candidate) => candidate.endpoint === "coding-global"), ...codingFallback.filter((candidate) => candidate.endpoint === "coding-global")];
case "coding-cn": return [...codingModels.filter((candidate) => candidate.endpoint === "coding-cn"), ...codingFallback.filter((candidate) => candidate.endpoint === "coding-cn")];
default: return [
...general,
...codingModels,
...codingFallback
];
}
})();
const resultsByEndpoint = /* @__PURE__ */ new Map();
for (const candidate of probeCandidates) {
const priorResults = resultsByEndpoint.get(candidate.endpoint) ?? [];
if (candidate.fallback && (priorResults.length === 0 || !priorResults.every(isUnsupportedModelResult))) continue;
const result = await probeZaiChatCompletions({
baseUrl: candidate.baseUrl,
apiKey: params.apiKey,
modelId: candidate.modelId,
timeoutMs,
fetchFn: params.fetchFn
});
if (result.ok) return {
endpoint: candidate.endpoint,
baseUrl: candidate.baseUrl,
modelId: candidate.modelId,
note: candidate.note
};
resultsByEndpoint.set(candidate.endpoint, [...priorResults, result]);
}
return null;
}
//#endregion
export { detectZaiEndpoint as t };