openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
160 lines (159 loc) • 6.38 kB
JavaScript
import "./model-ref-shared-DqE2RqZv.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { b as resolveNativeModelPrimary, f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { Kc as validateModelsProbeParams } from "./src-BX8OAe1J.mjs";
import { i as DEFAULT_PROVIDER, n as DEFAULT_MODEL } from "./defaults-BWyoTPqn.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { b as readUtilityModelSetting, h as resolveConfiguredModelRef, i as buildModelAliasIndex, y as resolveModelRefFromString } from "./model-selection-shared-9Kz63Hhm.mjs";
import { r as modelKey } from "./provider-registry-index-DhPtwFh_.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { n as resolveConfiguredModelFallbacks } from "./model-selection-resolve-CyMfPqoi.mjs";
import { t as formatForLog } from "./ws-log-CbOV6uhk.mjs";
import { t as assertValidParams } from "./validation-DwNk6A-B.mjs";
import { t as redactStatusSecrets } from "./status-redaction-D9OVqm2x.mjs";
import { r as runAuthProbes } from "./list.probe-CKaf6Gqv.mjs";
import { t as resolveModelAuthAgentScope } from "./model-auth-agent-scope-D5aeJRSp.mjs";
//#region src/gateway/server-methods/models-probe.ts
const DEFAULT_TIMEOUT_MS = 2e4;
const MIN_TIMEOUT_MS = 5e3;
const MAX_TIMEOUT_MS = 6e4;
const PROBE_CONCURRENCY = 2;
const PROBE_MAX_TOKENS = 8;
const FAILURE_PRIORITY = [
"auth",
"billing",
"rate_limit",
"timeout",
"format",
"no_model",
"unknown"
];
const PROBE_ERROR_MESSAGES = {
auth: "Authentication failed.",
rate_limit: "The provider rate limit was reached.",
billing: "The provider reported a billing problem.",
timeout: "The connection timed out.",
format: "The provider rejected the model or request format.",
unknown: "The connection check failed.",
no_model: "No model is available for this provider."
};
function safeProbeError(e) {
return e === "ok" ? void 0 : PROBE_ERROR_MESSAGES[e];
}
const PROBE_REASON_MESSAGES = {
excluded_by_auth_order: "This profile is excluded by the provider auth order. Update the order or choose another profile, then retry.",
missing_credential: "This credential is missing. Add it or remove the stale configuration, then retry.",
expired: "This credential has expired. Sign in again or replace it, then retry.",
invalid_expires: "This credential has invalid expiry metadata. Sign in again or replace it, then retry.",
unresolved_ref: "The configured credential could not be resolved. Update or remove it, then retry.",
ineligible_profile: "This profile is not compatible with the provider configuration. Choose another profile, then retry.",
no_model: "No model is available for this provider. Configure a model, then retry."
};
function safeProbeTargetLabel(e) {
const t = e.source === "profile" ? `Profile ${e.label}` : e.source === "models.json" ? e.label === "config" ? "Configured credential" : "Provider configuration" : `Environment credential (${e.label})`;
return e.model ? `${t} · ${e.model}` : t;
}
function safeProbeTargetError(e) {
if (e.status === "ok") return;
return (e.reasonCode && PROBE_REASON_MESSAGES[e.reasonCode]) ?? safeProbeError(e.status);
}
function modelCandidatesFromConfig(e, t) {
const n = {
cfg: e,
agentId: t,
defaultProvider: DEFAULT_PROVIDER,
allowPluginNormalization: false
};
const r = buildModelAliasIndex(n);
const i = resolveNativeModelPrimary(e, t) ? resolveConfiguredModelRef({
...n,
defaultModel: DEFAULT_MODEL
}) : void 0;
const a = readUtilityModelSetting(e, t);
const o = [
...resolveConfiguredModelFallbacks(n),
...a.kind === "explicit" ? [a.modelRef] : [],
...a.kind !== "disabled" ? a.fallbacks : []
].flatMap((e) => {
const t = resolveModelRefFromString({
...n,
raw: e,
aliasIndex: r
});
return t ? [modelKey(t.ref.provider, t.ref.model)] : [];
});
return i ? [modelKey(i.provider, i.model), ...o] : o;
}
function selectRollupStatus(e) {
if (e.some((e) => e.status === "ok")) return "ok";
return FAILURE_PRIORITY.find((t) => e.some((e) => e.status === t)) ?? "unknown";
}
function mapProbeResult(e, t) {
const n = selectRollupStatus(t);
const r = t.filter((e) => e.status === n).map((e) => e.latencyMs).filter((e) => typeof e === "number").toSorted((e, t) => e - t)[0];
const i = safeProbeError(n);
return {
provider: e,
status: n,
...r !== void 0 ? { latencyMs: r } : {},
...i ? { error: i } : {},
results: t.map((e) => {
const t = safeProbeTargetError(e);
return {
...e.profileId ? { profileId: e.profileId } : {},
label: safeProbeTargetLabel(e),
status: e.status,
...e.latencyMs !== void 0 ? { latencyMs: e.latencyMs } : {},
...t ? { error: t } : {}
};
})
};
}
const modelsProbeHandlers = { "models.probe": async ({ params: e, respond: t, context: n }) => {
if (!assertValidParams(e, validateModelsProbeParams, "models.probe", t)) return;
const r = normalizeProviderId(e.provider);
const i = e.profileId?.trim();
if (!r || e.profileId !== void 0 && !i) {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "provider and profileId must not be blank"));
return;
}
const a = Math.min(MAX_TIMEOUT_MS, Math.max(MIN_TIMEOUT_MS, e.timeoutMs ?? DEFAULT_TIMEOUT_MS));
try {
const o = n.getRuntimeConfig();
const s = resolveModelAuthAgentScope(o, e.agentId);
if (!s.ok) {
t(false, void 0, s.error);
return;
}
const c = resolveAgentWorkspaceDir(o, s.agentId);
const l = mapProbeResult(r, (await runAuthProbes({
cfg: o,
agentId: s.agentId,
agentDir: s.agentDir,
workspaceDir: c,
providers: [r],
modelCandidates: modelCandidatesFromConfig(o, s.agentId),
options: {
provider: r,
...i ? { profileIds: [i] } : {},
...!i ? { includeDirectKeys: true } : {},
timeoutMs: a,
concurrency: PROBE_CONCURRENCY,
maxTokens: PROBE_MAX_TOKENS
}
})).results);
if (l.results.length === 0) l.error = "No check targets are available for this provider.";
t(true, l, void 0);
} catch (e) {
n.logGateway.warn("Model connection check failed.", {
event: "models_probe_failed",
provider: r,
timeoutMs: a,
error: redactStatusSecrets(formatForLog(e))
});
t(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "Connection check failed."));
}
} };
//#endregion
export { modelsProbeHandlers };