UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

265 lines (264 loc) • 8.88 kB
import { c as resolveAgentModelTimeoutMsValue, o as resolveAgentModelFallbackValues, s as resolveAgentModelPrimaryValue } from "./model-input-DrM4DWHG.mjs"; import { i as DEFAULT_PROVIDER, n as DEFAULT_MODEL } from "./defaults-BWyoTPqn.mjs"; import { a as isAuthModeAllowedForModel } from "./model-runtime-policy-Cfudvy7O.mjs"; import { h as resolveConfiguredModelRef } from "./model-selection-shared-9Kz63Hhm.mjs"; import { s as overlayRuntimeExternalOAuthProfiles } from "./oauth-shared-gvo_ItCc.mjs"; import { n as evaluateStoredCredentialEligibility } from "./credential-state-BiAE74F8.mjs"; import "./model-selection-C2A4Wyix.mjs"; import { a as resolveAuthProfileOrder, f as listProfilesForProvider } from "./order-jOvee2J6.mjs"; import { a as resolveExternalCliAuthProfiles } from "./external-cli-sync-_MwEKD82.mjs"; import { m as resolveProviderAuthScope } from "./loader-runtime-load-Ba16vW6K.mjs"; import { t as resolveEnvApiKey } from "./model-auth-env-kbpGo8fU.mjs"; import { c as profileTypeToAuthMode, v as resolveProviderEntryApiKeyProfileReference } from "./model-auth-provider-config-Bnc4upRS.mjs"; import { c as ensureAuthProfileStoreWithoutExternalProfilesAsync } from "./store-runtime-dW0pXLJI.mjs"; import "./auth-profiles-VRLhewIH.mjs"; import { n as hasRuntimeAvailableProviderAuth } from "./model-auth-runtime-BVYiZt5s.mjs"; import "./model-auth-CEsyk_Yg.mjs"; //#region src/agents/tools/model-config.helpers.ts const OPENAI_PROVIDER_ID = "openai"; const CODEX_MEDIA_PROVIDER_ID = "codex"; const OPENAI_RESPONSES_MODEL_API = "openai-responses"; function applyAgentDefaultModelConfig(e, t, n) { if (!e) return; return { ...e, agents: { ...e.agents, defaults: { ...e.agents?.defaults, ...t === "imageModel" ? { imageModel: n } : { mediaModels: { ...e.agents?.defaults?.mediaModels, [t]: n } } } } }; } function hasToolModelConfig(e) { return Boolean(e?.primary?.trim() || (e?.fallbacks ?? []).some((e) => e.trim().length > 0)); } function resolveDefaultModelRef(e) { if (e) { const t = resolveConfiguredModelRef({ cfg: e, defaultProvider: DEFAULT_PROVIDER, defaultModel: DEFAULT_MODEL }); return { provider: t.provider, model: t.model }; } return { provider: DEFAULT_PROVIDER, model: DEFAULT_MODEL }; } function hasAuthForProvider(e) { if (resolveProviderAuthScope({ ...e, config: e.cfg }) === "plugin") return false; if (!e.runtimeLookup && resolveEnvApiKey(e.provider, void 0, { config: e.cfg, workspaceDir: e.workspaceDir })?.apiKey && (!e.capability || isAuthModeAllowedForModel({ provider: e.provider, capability: e.capability, mode: "api-key" }))) return true; return hasAuthProfileForProvider({ provider: e.provider, agentDir: e.agentDir, authStore: e.authStore, capability: e.capability }); } function hasAuthProfileForProvider(e) { const t = e.authStore; if (!t) return false; return listProfilesForProvider(t, e.provider).some((n) => { const r = t.profiles[n]; return r && (!e.type || r.type === e.type) && (!e.capability || isAuthModeAllowedForModel({ provider: e.provider, capability: e.capability, mode: profileTypeToAuthMode(r.type), authFlow: r.type === "oauth" ? r.authFlow : void 0 })); }); } /** Runtime callers prepare one owner snapshot before synchronous tool selection. */ async function prepareToolAuthProfileStore(e) { return e?.authProfileStore ?? (e?.agentDir ? await ensureAuthProfileStoreWithoutExternalProfilesAsync(e.agentDir, { allowKeychainPrompt: false }) : void 0); } function hasProviderAuthForTool(e) { if (resolveProviderAuthScope({ ...e, config: e.cfg }) === "plugin") return false; const t = e.authStore; if (e.capability && t) { const n = resolveProviderEntryApiKeyProfileReference({ ...e, store: t }); if (n.kind === "profile-incompatible") return false; if (n.kind === "profile" && !isAuthModeAllowedForModel({ provider: e.provider, capability: e.capability, mode: n.mode, authFlow: n.credential.type === "oauth" ? n.credential.authFlow : void 0 })) return false; } if (hasRuntimeAvailableProviderAuth({ provider: e.provider, cfg: e.cfg, workspaceDir: e.workspaceDir, allowPluginSyntheticAuth: false, runtimeLookup: e.runtimeLookup, capability: e.capability, store: t })) return true; return hasAuthForProvider(e); } function overlayExternalCliAuthStoreForProvider(e) { const t = resolveExternalCliAuthProfiles(e.authStore, { allowKeychainPrompt: false, providerIds: [e.provider] }); if (t.length === 0) return e.authStore; return overlayRuntimeExternalOAuthProfiles(e.authStore, t); } function hasAuthProfileTypeInStore(e) { const t = Array.isArray(e.type) ? e.type : [e.type]; return resolveAuthProfileOrder({ cfg: e.cfg, store: e.store, provider: e.provider }).some((n) => t.includes(e.store.profiles[n]?.type)); } function hasAuthProfileTypeForProvider(e) { const t = e.authStore; if (t && hasAuthProfileTypeInStore({ ...e, store: t })) return true; if (e.includeExternalCli && e.authStore) { const t = overlayExternalCliAuthStoreForProvider({ provider: e.provider, authStore: e.authStore }); return hasAuthProfileTypeInStore({ ...e, store: t }); } return false; } /** Returns whether a provider has direct API-key-capable auth for model-backed tools. */ function hasDirectProviderApiKeyAuthForTool(e) { const t = resolveDirectProviderEntryAuthFromProfileReference(e); if (t !== void 0) return t; if (hasRuntimeAvailableProviderAuth({ provider: e.provider, cfg: e.cfg, workspaceDir: e.workspaceDir, modelApi: e.modelApi, allowPluginSyntheticAuth: false, store: e.authStore })) return true; return hasAuthProfileTypeForProvider({ provider: e.provider, cfg: e.cfg, authStore: e.authStore, type: "api_key" }); } function resolveDirectProviderEntryAuthFromProfileReference(e) { const resolveFromStore = (t) => { const n = resolveProviderEntryApiKeyProfileReference({ cfg: e.cfg, provider: e.provider, store: t }); if (n.kind === "profile") return n.credential.type === "api_key" && evaluateStoredCredentialEligibility({ credential: n.credential }).eligible; if (n.kind === "profile-incompatible") return false; }; const t = e.authStore; const n = t ? resolveFromStore(t) : void 0; if (n !== void 0) return n; if (e.authStore) return resolveFromStore(overlayExternalCliAuthStoreForProvider({ provider: e.provider, authStore: e.authStore })); } /** Resolves the implicit OpenAI image slot without letting OAuth-only auth pick direct OpenAI. */ function resolveOpenAiImageMediaCandidate(e) { if (resolveProviderAuthScope({ provider: OPENAI_PROVIDER_ID, config: e.cfg, workspaceDir: e.workspaceDir }) === "plugin") return { kind: "drop" }; const t = e.openAiModel.trim(); if (!t) return { kind: "drop" }; if (hasDirectProviderApiKeyAuthForTool({ provider: OPENAI_PROVIDER_ID, cfg: e.cfg, workspaceDir: e.workspaceDir, authStore: e.authStore, modelApi: OPENAI_RESPONSES_MODEL_API })) return { kind: "keep", ref: `${OPENAI_PROVIDER_ID}/${t}` }; if (!hasAuthProfileTypeForProvider({ provider: OPENAI_PROVIDER_ID, cfg: e.cfg, authStore: e.authStore, includeExternalCli: true, type: ["oauth", "token"] })) return { kind: "drop" }; const n = e.resolveCodexMediaRoute?.()?.model.trim(); if (n) return { kind: "substitute", provider: CODEX_MEDIA_PROVIDER_ID, ref: `${CODEX_MEDIA_PROVIDER_ID}/${n}` }; return { kind: "drop" }; } function coerceToolModelConfig(e) { const t = resolveAgentModelPrimaryValue(e); const n = resolveAgentModelFallbackValues(e); const r = resolveAgentModelTimeoutMsValue(e); return { ...t?.trim() ? { primary: t.trim() } : {}, ...n.length > 0 ? { fallbacks: n } : {}, ...r !== void 0 ? { timeoutMs: r } : {} }; } function buildToolModelConfigFromCandidates(e) { if (hasToolModelConfig(e.explicit)) return e.explicit; const t = []; for (const n of e.candidates) { const r = n?.trim(); if (!r || !r.includes("/")) continue; const i = r.slice(0, r.indexOf("/")).trim(); const a = e.isProviderConfigured?.(i) ?? hasProviderAuthForTool({ provider: i, cfg: e.cfg, workspaceDir: e.workspaceDir, agentDir: e.agentDir, authStore: e.authStore }); if (!i || !a) continue; if (!t.includes(r)) t.push(r); } if (t.length === 0) return null; return { primary: t[0], ...t.length > 1 ? { fallbacks: t.slice(1) } : {}, ...e.explicit.timeoutMs !== void 0 ? { timeoutMs: e.explicit.timeoutMs } : {} }; } //#endregion export { hasAuthProfileForProvider as a, prepareToolAuthProfileStore as c, hasAuthForProvider as i, resolveDefaultModelRef as l, buildToolModelConfigFromCandidates as n, hasProviderAuthForTool as o, coerceToolModelConfig as r, hasToolModelConfig as s, applyAgentDefaultModelConfig as t, resolveOpenAiImageMediaCandidate as u };