UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

170 lines (169 loc) • 6.55 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { r as resolveAgentConfig } from "./agent-scope-config-DmXgFGKP.mjs"; import { s as resolveAgentModelPrimaryValue } from "./model-input-DrM4DWHG.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import { t as listAgentIdsCompatibility } from "./agent-scope-runtime-CDpr-vor.mjs"; import "./provider-onboard-Di5Hihl4.mjs"; import { c as CLAUDE_CLI_PROFILE_ID, t as CLAUDE_CLI_BACKEND_ID } from "./cli-constants-CDevEMvo.mjs"; import { a as resolveKnownAnthropicModelRef, i as resolveClaudeCliAnthropicModelRefs, n as normalizeAnthropicProviderId, r as parseAnthropicModelRef, t as modelEntryWithClaudeCliRuntime } from "./claude-model-refs-CU8jKXtL.mjs"; //#region extensions/anthropic/config-defaults.ts const ANTHROPIC_PROVIDER_API = "anthropic-messages"; const ANTHROPIC_API_KEY_DEFAULT_ALLOWLIST_REFS = [ "anthropic/claude-sonnet-5-5", "anthropic/claude-sonnet-5", "anthropic/claude-sonnet-4-6" ]; function resolveAnthropicDefaultAuthMode(e, t) { if (usesRetiredClaudeCliProviderEntry(e)) return "oauth"; const n = e.auth?.profiles ?? {}; const r = Object.entries(n).filter(([, e]) => e?.provider === "anthropic" || e?.provider === "claude-cli"); const i = [...e.auth?.order?.anthropic ?? [], ...e.auth?.order?.["claude-cli"] ?? []]; for (const e of i) { const t = n[e]; if (!t || t.provider !== "anthropic" && t.provider !== "claude-cli") continue; if (t.provider === "claude-cli") return "oauth"; if (t.mode === "api_key") return "api_key"; if (t.mode === "oauth" || t.mode === "token") return "oauth"; } const a = r.some(([, e]) => e?.provider === "anthropic" && e?.mode === "api_key"); const o = r.some(([, e]) => e?.provider === "claude-cli" || e?.mode === "oauth" || e?.mode === "token"); if (a && !o) return "api_key"; if (o && !a) return "oauth"; if (t.ANTHROPIC_OAUTH_TOKEN?.trim()) return "oauth"; if (t.ANTHROPIC_API_KEY?.trim()) return "api_key"; return null; } function usesRetiredClaudeCliProviderEntry(e) { return Object.entries(e.models?.providers ?? {}).some(([e, t]) => normalizeAnthropicProviderId(e) === "anthropic" && t.apiKey === "anthropic:claude-cli"); } function isAnthropicCacheRetentionTarget(e) { return Boolean(e && (e.provider === "anthropic" || e.provider === "amazon-bedrock" && normalizeLowercaseStringOrEmpty(e.model).includes("anthropic.claude"))); } function usesClaudeCliModelSelection(e) { const t = resolveAgentModelPrimaryValue(e.agents?.defaults?.model); if ((t ? parseAnthropicModelRef(t) : null)?.provider === "claude-cli") return true; return Object.entries(e.agents?.defaults?.models ?? {}).some(([e, t]) => { const n = parseAnthropicModelRef(e); if (n?.provider === "claude-cli") return true; const r = t?.agentRuntime?.id; return n?.provider === "anthropic" && normalizeLowercaseStringOrEmpty(r) === "claude-cli"; }); } function usesSelectedClaudeCliAuthProfile(e) { if (usesRetiredClaudeCliProviderEntry(e)) return true; const t = e.auth?.profiles ?? {}; const n = [...e.auth?.order?.anthropic ?? [], ...e.auth?.order?.["claude-cli"] ?? []]; for (const e of n) { const n = t[e]?.provider; if (n === "claude-cli") return true; if (n === "anthropic") return false; } let r = false; let i = false; for (const e of Object.values(t)) { if (e?.provider === "claude-cli") r = true; if (e?.provider === "anthropic") i = true; } return r && !i; } function collectClaudeCliRuntimeRefsFromConfig(e) { const t = [{ model: e.agents?.defaults?.model, models: e.agents?.defaults?.models }, ...listAgentIdsCompatibility(e).map((t) => { const n = resolveAgentConfig(e, t); return { model: n?.model, models: n?.models }; })]; const n = /* @__PURE__ */ new Set(); for (const { model: e, models: r } of t) { const t = typeof e === "string" ? [e] : [e?.primary, ...e?.fallbacks ?? []]; for (const e of [...t, ...Object.keys(r ?? {})]) { if (typeof e !== "string") continue; for (const t of resolveClaudeCliAnthropicModelRefs(e)?.runtimeRefs ?? []) n.add(t); } } return [...n]; } function normalizeAnthropicProviderConfigForProvider(e) { const { providerConfig: t } = e; const n = normalizeAnthropicProviderId(e.provider); if (n !== "anthropic" && n !== "claude-cli" || t.api || !t.models?.length) return t; return { ...t, api: ANTHROPIC_PROVIDER_API }; } function applyAnthropicConfigDefaults(e) { const t = e.config.agents?.defaults; if (!t) return e.config; const n = resolveAnthropicDefaultAuthMode(e.config, e.env); if (!n) return e.config; let r = false; const i = { ...t }; if (t.contextPruning?.mode === void 0) { i.contextPruning = { ...t.contextPruning, mode: "cache-ttl", ttl: t.contextPruning?.ttl ?? "1h" }; r = true; } const a = { ...t.models }; let o = false; if (n === "api_key") { const e = resolveKnownAnthropicModelRef(resolveAgentModelPrimaryValue(t.model)); const n = e ? parseAnthropicModelRef(e) : null; const r = [...Object.keys(a), ...isAnthropicCacheRetentionTarget(n) ? [`${n.provider}/${n.model}`] : []]; for (const e of r) { if (!isAnthropicCacheRetentionTarget(parseAnthropicModelRef(e))) continue; const t = a[e] ?? {}; const n = t.params ?? {}; if (typeof n.cacheRetention === "string") continue; a[e] = { ...t, params: { ...n, cacheRetention: "short" } }; o = true; } if (Object.keys(a).some((e) => isAnthropicCacheRetentionTarget(parseAnthropicModelRef(e)))) for (const e of ANTHROPIC_API_KEY_DEFAULT_ALLOWLIST_REFS) { if (e in a) continue; a[e] = { params: { cacheRetention: "short" } }; o = true; } } if (n === "oauth" && (usesClaudeCliModelSelection(e.config) || usesSelectedClaudeCliAuthProfile(e.config))) { const t = collectClaudeCliRuntimeRefsFromConfig(e.config); for (const e of t) { const t = a[e]; const n = modelEntryWithClaudeCliRuntime(t); if (n === t) continue; a[e] = n; o = true; } } if (o) { i.models = a; r = true; } if (!r) return e.config; return { ...e.config, agents: { ...e.config.agents, defaults: i } }; } /** Subscription defaults avoid the API-key cadence; existing jobs remain operator-owned. */ function resolveAnthropicProactiveCadenceMs(e) { const t = resolveAnthropicDefaultAuthMode(e.config, e.env); return t === "oauth" ? 36e5 : t === "api_key" ? 18e5 : void 0; } //#endregion export { normalizeAnthropicProviderConfigForProvider as n, resolveAnthropicProactiveCadenceMs as r, applyAnthropicConfigDefaults as t };