UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

169 lines (168 loc) • 7.24 kB
import { r as normalizeProviderId } from "./provider-id-D1VImdKk.mjs"; import { d as resolveAgentWorkspaceDir } from "./agent-scope-config-BEk6-ThS.mjs"; import "./agent-scope-bJOokm5r.mjs"; import { n as withPluginRuntimeGenerationScope } from "./generation-scope-DnORwJwz.mjs"; import { a as resolveProviderModelRoutes } from "./provider-model-routes-Cg2A4PAY.mjs"; import { y as resolveModelRefFromString } from "./model-selection-shared-CHVNQnp1.mjs"; import { a as projectProviderModelRouteConfig } from "./provider-model-route-B0Fj9zGu.mjs"; import { a as resolveModelCatalogIdentityKey } from "./openai-model-routes-QgeWIyIu.mjs"; import { t as resolveDefaultModelForAgent } from "./model-selection-config-BC0CegJ7.mjs"; import { t as resolveAgentHarnessPolicy } from "./policy-wq2RlDKJ.mjs"; import "./model-selection-Cpoq0B2R.mjs"; import { d as readSessionEntryInWorker } from "./session-entry-read-runtime-BdnQjoL6.mjs"; import { n as createModelVisibilityPolicy, t as RUNTIME_MODEL_VISIBILITY_NORMALIZATION } from "./model-visibility-policy-JItm7p_F.mjs"; import { r as prepareSimpleCompletionModel } from "./simple-completion-runtime-C71S2ucI.mjs"; import { t as resolveTranscriptPolicy } from "./transcript-policy-BeXZSZLE.mjs"; import { n as resolveSessionAuthSelection } from "./session-override-ERbkZ0dT.mjs"; import { isAnthropicOAuthApiKey } from "@openclaw/ai/internal/anthropic"; //#region src/gateway/worker-environments/inference-model.ts async function resolveApprovedWorkerModelSelection(params) { const { target, modelRef, runtimeSnapshot } = params; const sessionEntry = target.sessionEntry ?? await readSessionEntryInWorker(target, params.assertCurrent); if (sessionEntry?.sessionId !== target.sessionId) return; params.assertCurrent(); const lifecycleConfig = runtimeSnapshot.config; const agentDir = runtimeSnapshot.agentDir; const workspaceDir = runtimeSnapshot.workspaceDir ?? resolveAgentWorkspaceDir(lifecycleConfig, target.agentId); const selection = { cfg: lifecycleConfig, agentId: target.agentId, manifestPlugins: runtimeSnapshot.metadataSnapshot, ...RUNTIME_MODEL_VISIBILITY_NORMALIZATION }; const defaultModel = resolveDefaultModelForAgent(selection); const policy = createModelVisibilityPolicy({ ...selection, catalog: runtimeSnapshot.modelCatalog.entries, defaultProvider: defaultModel.provider, defaultModel }); const resolved = resolveModelRefFromString({ ...selection, raw: `${modelRef.provider}/${modelRef.model}`, defaultProvider: defaultModel.provider, aliasIndex: policy.selectionAliasIndex }); if (!resolved || normalizeProviderId(resolved.ref.provider) !== normalizeProviderId(modelRef.provider)) return; const resolvedKey = resolveModelCatalogIdentityKey({ provider: resolved.ref.provider, id: resolved.ref.model }); if (!(policy.allowedCatalog.some((entry) => resolvedKey === resolveModelCatalogIdentityKey(entry)) || policy.retainedKeys.has(resolvedKey)) || !policy.allows(resolved.ref)) return; return { sessionEntry, lifecycleConfig, agentDir, workspaceDir, resolved, harnessPolicy: resolveAgentHarnessPolicy({ provider: resolved.ref.provider, modelId: resolved.ref.model, config: lifecycleConfig, agentId: target.agentId, sessionKey: target.sessionKey }) }; } async function resolveApprovedWorkerLocalModel(params) { const { runtimeSnapshot } = params; return await withPluginRuntimeGenerationScope(runtimeSnapshot, async () => { const approved = await resolveApprovedWorkerModelSelection(params); if (!approved) return; const { lifecycleConfig, workspaceDir, resolved } = approved; const model = runtimeSnapshot.findConfiguredRuntimeModel(resolved.ref.provider, resolved.ref.model); if (!model) return { error: `Worker-local inference model ${resolved.ref.provider}/${resolved.ref.model} is missing from the Gateway model catalog. Add its metadata under models.providers in the Gateway openclaw.json; keep its endpoint and credentials only on the node.` }; return { transcriptPolicy: resolveTranscriptPolicy({ provider: resolved.ref.provider, modelId: resolved.ref.model, modelApi: model.api, model, config: lifecycleConfig, workspaceDir, directApiKey: false }), model }; }); } async function resolveApprovedWorkerModel(params) { const { target, signal, runtimeSnapshot } = params; return await withPluginRuntimeGenerationScope(runtimeSnapshot, async () => { const approved = await resolveApprovedWorkerModelSelection(params); if (!approved) return; const { sessionEntry, lifecycleConfig, agentDir, workspaceDir, resolved, harnessPolicy } = approved; const agentRuntimeId = harnessPolicy.runtimeSource !== "implicit" || lifecycleConfig.plugins?.entries?.codex?.enabled === true ? harnessPolicy.runtime : void 0; const sessionSelection = await resolveSessionAuthSelection({ cfg: lifecycleConfig, provider: resolved.ref.provider, modelId: resolved.ref.model, agentId: target.agentId, harnessRuntime: harnessPolicy.runtime, agentDir, sessionEntry, sessionStore: { [target.sessionKey]: sessionEntry }, sessionKey: target.sessionKey, storePath: target.storePath, assertCommitAllowed: params.assertCurrent, isNewSession: false }); params.assertCurrent(); const selectedProfileId = sessionSelection?.profileId; const routeRequirement = sessionSelection?.routeRequirement; let modelConfig = lifecycleConfig; const routeResolution = routeRequirement ? resolveProviderModelRoutes({ provider: resolved.ref.provider, modelId: resolved.ref.model, config: lifecycleConfig }) : void 0; const route = routeResolution?.kind === "routes" ? routeResolution.routes.find((candidate) => candidate.authRequirement === routeRequirement) : void 0; if (route) modelConfig = projectProviderModelRouteConfig({ provider: resolved.ref.provider, config: lifecycleConfig, route }); const prepared = await prepareSimpleCompletionModel({ workerInferenceAuthority: { assertCurrent: params.assertCurrent }, cfg: modelConfig, transport: "provider-stream", agentId: target.agentId, provider: resolved.ref.provider, modelId: resolved.ref.model, agentDir, modelIdSource: "selected", ...selectedProfileId ? { profileId: selectedProfileId, preferredProfile: selectedProfileId, bindAuthOwner: true } : {}, allowMissingApiKeyModes: ["aws-sdk"], allowBundledStaticCatalogFallback: true, signal, preparedModelRuntime: runtimeSnapshot, workspaceDir, ...agentRuntimeId ? { agentRuntimeId } : {} }); params.assertCurrent(); if ("error" in prepared) return prepared; return { transcriptPolicy: resolveTranscriptPolicy({ provider: resolved.ref.provider, modelId: resolved.ref.model, modelApi: prepared.model.api, model: prepared.model, config: lifecycleConfig, workspaceDir, directApiKey: Boolean(prepared.auth.apiKey) && !isAnthropicOAuthApiKey(prepared.auth.apiKey) }), provider: resolved.ref.provider, model: resolved.ref.model, config: lifecycleConfig, agentDir, workspaceDir, prepared }; }); } //#endregion export { resolveApprovedWorkerModel as n, resolveApprovedWorkerLocalModel as t };