openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
171 lines (170 loc) • 6.08 kB
JavaScript
import { _ as resolveDefaultAgentId, a as resolveAgentDir, f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { c as resolveCliRuntimeCanonicalProvider } from "./cli-backends-B8PrIwPC.mjs";
import { c as resolveCliRuntimeExecutionProvider, i as isCliRuntimeAliasForProvider } from "./model-runtime-aliases-DZy9Ea2c.mjs";
import { r as listModelCatalogObservedRoutes, t as createModelCatalogSnapshotView } from "./model-catalog-view-CsclLtvK.mjs";
import { r as resolveAgentHarnessSelectionDecision } from "./selection-decision-Bz5cp9Eg.mjs";
import { n as resolveEmbeddedCliBackendDispatchEligibilityAsync } from "./cli-backend-dispatch-eligibility-CLVop73D.mjs";
import { i as preparedAgentRuntimeProfileAttemptHasCandidate, n as canRunPreparedAgentRuntimeAuthAttempt, r as prepareAgentRuntimeAuth } from "./prepare-auth-Ck7RPVAG.mjs";
//#region src/agents/isolated-completion-route.ts
/** A CLI runtime ref canonicalizes to its model provider but keeps the CLI as its explicit owner. */
function resolveIsolatedCompletionProvider(e) {
const t = resolveCliRuntimeCanonicalProvider({
runtime: e.provider,
config: e.config,
includeSetupRegistry: true
});
const n = e.agentHarnessRuntimeOverride ?? (t ? e.provider : void 0);
return {
provider: t ?? e.provider,
...n ? { runtimeOverride: n } : {}
};
}
/** Harness selection plus the CLI backend, when one owns this completion instead of a harness. */
async function resolveIsolatedCompletionRoute(e) {
const t = resolveAgentHarnessSelectionDecision({
provider: e.provider,
modelId: e.model,
config: e.config,
agentId: e.agentId,
agentHarnessRuntimeOverride: e.runtimeOverride
});
const n = e.runtimeOverride ?? t.policy.runtime;
if (isCliRuntimeAliasForProvider({
runtime: n,
provider: e.provider,
cfg: e.config
})) return {
selection: t,
cliOwner: n
};
if (e.explicitRuntimeOverride) return { selection: t };
const r = resolveCliRuntimeExecutionProvider({
provider: e.provider,
cfg: e.config,
agentId: e.agentId,
modelId: e.model,
authProfileId: e.authProfileId,
preparedAuthStore: e.preparedAuthStore
}) ?? (await resolveEmbeddedCliBackendDispatchEligibilityAsync({
provider: e.provider,
model: e.model,
agentId: e.agentId,
authProfileId: e.authProfileId,
config: e.config,
agentDir: e.agentDir,
workspaceDir: e.workspaceDir,
preparedAuthStore: e.preparedAuthStore
}))?.provider;
return r ? {
selection: t,
cliOwner: r
} : { selection: t };
}
/** Scope profile checks and dispatch to the same single physical auth attempt. */
function selectIsolatedHarnessAuthAttempt(e) {
if (e.kind !== "profile") return e;
return {
...e,
plan: {
...e.plan,
forwardedAuthProfileId: e.profileId,
forwardedAuthProfileCandidateIds: [e.profileId]
}
};
}
/** The auth planner owns this choice for both dispatch and its read-only projection. */
function resolveIsolatedCompletionAuthorizationOwner(e) {
return e?.harnessAuthProvider && e.modelRoute?.authRequirement !== "api-key" ? "harness" : "host";
}
/**
* Planned runtime for an isolated completion in the supplied prepared generation.
* Undefined when no owner or runnable auth attempt can serve the request.
*/
async function resolveIsolatedCompletionRuntime(e) {
const t = e.config ?? {};
try {
const n = e.agentId ?? resolveDefaultAgentId(t);
const { provider: r, runtimeOverride: i } = resolveIsolatedCompletionProvider({
provider: e.provider,
config: t,
agentHarnessRuntimeOverride: e.agentHarnessRuntimeOverride
});
const a = await resolveIsolatedCompletionRoute({
config: t,
provider: r,
model: e.model,
authProfileId: e.authProfileId,
agentId: n,
agentDir: e.agentDir ?? resolveAgentDir(t, n),
workspaceDir: e.workspaceDir ?? resolveAgentWorkspaceDir(t, n),
runtimeOverride: i,
explicitRuntimeOverride: e.agentHarnessRuntimeOverride,
preparedAuthStore: e.preparedAuth?.preparedAuthStore
});
if (a.cliOwner) return {
id: a.cliOwner,
kind: "cli"
};
const { harness: o, selectedHarnessId: s } = a.selection;
if (!o) return {
id: s,
kind: "api"
};
if (!o.runIsolatedCompletionV2 && !o.runIsolatedCompletion) return;
if (o.resolveIsolatedCompletionRuntime) {
let i = "host";
if (o.runIsolatedCompletionV2 && o.authBootstrap === "harness") {
const a = e.preparedAuth;
if (!a) return;
const s = createModelCatalogSnapshotView(t, a.snapshot).variantsOf({
provider: r,
id: e.model
});
const { attempts: c } = prepareAgentRuntimeAuth({
provider: r,
modelId: e.model,
observedRoutes: s ? listModelCatalogObservedRoutes(s) : void 0,
config: t,
agentId: n,
agentDir: e.agentDir,
workspaceDir: e.workspaceDir,
metadataSnapshot: a.metadataSnapshot,
authProfileStore: a.preparedAuthStore,
sessionAuthProfileId: e.authProfileId,
sessionAuthProfileSource: e.authProfileId ? "user" : void 0,
...e.authProfileId ? { allowAuthProfileFallback: false } : {},
harnessId: o.id,
harnessRuntime: o.id,
harnessAuthBootstrap: o.authBootstrap
});
const l = c.find((t) => {
const n = selectIsolatedHarnessAuthAttempt(t);
return canRunPreparedAgentRuntimeAuthAttempt({
attempt: n,
priorProfileAttempted: false
}) && (n.kind !== "profile" || preparedAgentRuntimeProfileAttemptHasCandidate({
attempt: n,
store: a.preparedAuthStore,
modelId: e.model
}));
});
if (!l) return;
i = resolveIsolatedCompletionAuthorizationOwner(l.plan);
}
if (o.resolveIsolatedCompletionRuntime({ authorizationOwner: i }) === "openclaw") return {
id: "openclaw",
kind: "api"
};
}
return {
id: s,
kind: "harness",
harnessLabel: o.label
};
} catch {
return;
}
}
//#endregion
export { selectIsolatedHarnessAuthAttempt as a, resolveIsolatedCompletionRuntime as i, resolveIsolatedCompletionProvider as n, resolveIsolatedCompletionRoute as r, resolveIsolatedCompletionAuthorizationOwner as t };