mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 04:41:25 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -26,39 +26,63 @@ export function modelKey(provider: string, model: string) {
|
||||
|
||||
export function normalizeProviderId(provider: string): string {
|
||||
const normalized = provider.trim().toLowerCase();
|
||||
if (normalized === "z.ai" || normalized === "z-ai") return "zai";
|
||||
if (normalized === "opencode-zen") return "opencode";
|
||||
if (normalized === "qwen") return "qwen-portal";
|
||||
if (normalized === "kimi-code") return "kimi-coding";
|
||||
if (normalized === "z.ai" || normalized === "z-ai") {
|
||||
return "zai";
|
||||
}
|
||||
if (normalized === "opencode-zen") {
|
||||
return "opencode";
|
||||
}
|
||||
if (normalized === "qwen") {
|
||||
return "qwen-portal";
|
||||
}
|
||||
if (normalized === "kimi-code") {
|
||||
return "kimi-coding";
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
export function isCliProvider(provider: string, cfg?: OpenClawConfig): boolean {
|
||||
const normalized = normalizeProviderId(provider);
|
||||
if (normalized === "claude-cli") return true;
|
||||
if (normalized === "codex-cli") return true;
|
||||
if (normalized === "claude-cli") {
|
||||
return true;
|
||||
}
|
||||
if (normalized === "codex-cli") {
|
||||
return true;
|
||||
}
|
||||
const backends = cfg?.agents?.defaults?.cliBackends ?? {};
|
||||
return Object.keys(backends).some((key) => normalizeProviderId(key) === normalized);
|
||||
}
|
||||
|
||||
function normalizeAnthropicModelId(model: string): string {
|
||||
const trimmed = model.trim();
|
||||
if (!trimmed) return trimmed;
|
||||
if (!trimmed) {
|
||||
return trimmed;
|
||||
}
|
||||
const lower = trimmed.toLowerCase();
|
||||
if (lower === "opus-4.5") return "claude-opus-4-5";
|
||||
if (lower === "sonnet-4.5") return "claude-sonnet-4-5";
|
||||
if (lower === "opus-4.5") {
|
||||
return "claude-opus-4-5";
|
||||
}
|
||||
if (lower === "sonnet-4.5") {
|
||||
return "claude-sonnet-4-5";
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
function normalizeProviderModelId(provider: string, model: string): string {
|
||||
if (provider === "anthropic") return normalizeAnthropicModelId(model);
|
||||
if (provider === "google") return normalizeGoogleModelId(model);
|
||||
if (provider === "anthropic") {
|
||||
return normalizeAnthropicModelId(model);
|
||||
}
|
||||
if (provider === "google") {
|
||||
return normalizeGoogleModelId(model);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
|
||||
export function parseModelRef(raw: string, defaultProvider: string): ModelRef | null {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return null;
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
const slash = trimmed.indexOf("/");
|
||||
if (slash === -1) {
|
||||
const provider = normalizeProviderId(defaultProvider);
|
||||
@@ -68,7 +92,9 @@ export function parseModelRef(raw: string, defaultProvider: string): ModelRef |
|
||||
const providerRaw = trimmed.slice(0, slash).trim();
|
||||
const provider = normalizeProviderId(providerRaw);
|
||||
const model = trimmed.slice(slash + 1).trim();
|
||||
if (!provider || !model) return null;
|
||||
if (!provider || !model) {
|
||||
return null;
|
||||
}
|
||||
const normalizedModel = normalizeProviderModelId(provider, model);
|
||||
return { provider, model: normalizedModel };
|
||||
}
|
||||
@@ -83,9 +109,13 @@ export function buildModelAliasIndex(params: {
|
||||
const rawModels = params.cfg.agents?.defaults?.models ?? {};
|
||||
for (const [keyRaw, entryRaw] of Object.entries(rawModels)) {
|
||||
const parsed = parseModelRef(String(keyRaw ?? ""), params.defaultProvider);
|
||||
if (!parsed) continue;
|
||||
if (!parsed) {
|
||||
continue;
|
||||
}
|
||||
const alias = String((entryRaw as { alias?: string } | undefined)?.alias ?? "").trim();
|
||||
if (!alias) continue;
|
||||
if (!alias) {
|
||||
continue;
|
||||
}
|
||||
const aliasKey = normalizeAliasKey(alias);
|
||||
byAlias.set(aliasKey, { alias, ref: parsed });
|
||||
const key = modelKey(parsed.provider, parsed.model);
|
||||
@@ -103,7 +133,9 @@ export function resolveModelRefFromString(params: {
|
||||
aliasIndex?: ModelAliasIndex;
|
||||
}): { ref: ModelRef; alias?: string } | null {
|
||||
const trimmed = params.raw.trim();
|
||||
if (!trimmed) return null;
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
if (!trimmed.includes("/")) {
|
||||
const aliasKey = normalizeAliasKey(trimmed);
|
||||
const aliasMatch = params.aliasIndex?.byAlias.get(aliasKey);
|
||||
@@ -112,7 +144,9 @@ export function resolveModelRefFromString(params: {
|
||||
}
|
||||
}
|
||||
const parsed = parseModelRef(trimmed, params.defaultProvider);
|
||||
if (!parsed) return null;
|
||||
if (!parsed) {
|
||||
return null;
|
||||
}
|
||||
return { ref: parsed };
|
||||
}
|
||||
|
||||
@@ -123,7 +157,9 @@ export function resolveConfiguredModelRef(params: {
|
||||
}): ModelRef {
|
||||
const rawModel = (() => {
|
||||
const raw = params.cfg.agents?.defaults?.model as { primary?: string } | string | undefined;
|
||||
if (typeof raw === "string") return raw.trim();
|
||||
if (typeof raw === "string") {
|
||||
return raw.trim();
|
||||
}
|
||||
return raw?.primary?.trim() ?? "";
|
||||
})();
|
||||
if (rawModel) {
|
||||
@@ -135,7 +171,9 @@ export function resolveConfiguredModelRef(params: {
|
||||
if (!trimmed.includes("/")) {
|
||||
const aliasKey = normalizeAliasKey(trimmed);
|
||||
const aliasMatch = aliasIndex.byAlias.get(aliasKey);
|
||||
if (aliasMatch) return aliasMatch.ref;
|
||||
if (aliasMatch) {
|
||||
return aliasMatch.ref;
|
||||
}
|
||||
|
||||
// Default to anthropic if no provider is specified, but warn as this is deprecated.
|
||||
console.warn(
|
||||
@@ -149,7 +187,9 @@ export function resolveConfiguredModelRef(params: {
|
||||
defaultProvider: params.defaultProvider,
|
||||
aliasIndex,
|
||||
});
|
||||
if (resolved) return resolved.ref;
|
||||
if (resolved) {
|
||||
return resolved.ref;
|
||||
}
|
||||
}
|
||||
return { provider: params.defaultProvider, model: params.defaultModel };
|
||||
}
|
||||
@@ -209,7 +249,9 @@ export function buildAllowedModelSet(params: {
|
||||
const catalogKeys = new Set(params.catalog.map((entry) => modelKey(entry.provider, entry.id)));
|
||||
|
||||
if (allowAny) {
|
||||
if (defaultKey) catalogKeys.add(defaultKey);
|
||||
if (defaultKey) {
|
||||
catalogKeys.add(defaultKey);
|
||||
}
|
||||
return {
|
||||
allowAny: true,
|
||||
allowedCatalog: params.catalog,
|
||||
@@ -221,7 +263,9 @@ export function buildAllowedModelSet(params: {
|
||||
const configuredProviders = (params.cfg.models?.providers ?? {}) as Record<string, unknown>;
|
||||
for (const raw of rawAllowlist) {
|
||||
const parsed = parseModelRef(String(raw), params.defaultProvider);
|
||||
if (!parsed) continue;
|
||||
if (!parsed) {
|
||||
continue;
|
||||
}
|
||||
const key = modelKey(parsed.provider, parsed.model);
|
||||
const providerKey = normalizeProviderId(parsed.provider);
|
||||
if (isCliProvider(parsed.provider, params.cfg)) {
|
||||
@@ -244,7 +288,9 @@ export function buildAllowedModelSet(params: {
|
||||
);
|
||||
|
||||
if (allowedCatalog.length === 0 && allowedKeys.size === 0) {
|
||||
if (defaultKey) catalogKeys.add(defaultKey);
|
||||
if (defaultKey) {
|
||||
catalogKeys.add(defaultKey);
|
||||
}
|
||||
return {
|
||||
allowAny: true,
|
||||
allowedCatalog: params.catalog,
|
||||
@@ -296,7 +342,9 @@ export function resolveAllowedModelRef(params: {
|
||||
error: string;
|
||||
} {
|
||||
const trimmed = params.raw.trim();
|
||||
if (!trimmed) return { error: "invalid model: empty" };
|
||||
if (!trimmed) {
|
||||
return { error: "invalid model: empty" };
|
||||
}
|
||||
|
||||
const aliasIndex = buildModelAliasIndex({
|
||||
cfg: params.cfg,
|
||||
@@ -307,7 +355,9 @@ export function resolveAllowedModelRef(params: {
|
||||
defaultProvider: params.defaultProvider,
|
||||
aliasIndex,
|
||||
});
|
||||
if (!resolved) return { error: `invalid model: ${trimmed}` };
|
||||
if (!resolved) {
|
||||
return { error: `invalid model: ${trimmed}` };
|
||||
}
|
||||
|
||||
const status = getModelRefStatus({
|
||||
cfg: params.cfg,
|
||||
@@ -330,11 +380,15 @@ export function resolveThinkingDefault(params: {
|
||||
catalog?: ModelCatalogEntry[];
|
||||
}): ThinkLevel {
|
||||
const configured = params.cfg.agents?.defaults?.thinkingDefault;
|
||||
if (configured) return configured;
|
||||
if (configured) {
|
||||
return configured;
|
||||
}
|
||||
const candidate = params.catalog?.find(
|
||||
(entry) => entry.provider === params.provider && entry.id === params.model,
|
||||
);
|
||||
if (candidate?.reasoning) return "low";
|
||||
if (candidate?.reasoning) {
|
||||
return "low";
|
||||
}
|
||||
return "off";
|
||||
}
|
||||
|
||||
@@ -347,7 +401,9 @@ export function resolveHooksGmailModel(params: {
|
||||
defaultProvider: string;
|
||||
}): ModelRef | null {
|
||||
const hooksModel = params.cfg.hooks?.gmail?.model;
|
||||
if (!hooksModel?.trim()) return null;
|
||||
if (!hooksModel?.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const aliasIndex = buildModelAliasIndex({
|
||||
cfg: params.cfg,
|
||||
|
||||
Reference in New Issue
Block a user