File size: 6,813 Bytes
e249c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | /**
* External CLI auth selection scoping.
* Narrows CLI discovery to the provider/profile selected by model auth routing
* so runtime auth setup avoids broad CLI probing.
*/
import {
findNormalizedProviderValue,
normalizeProviderId,
} from "@openclaw/model-catalog-core/provider-id";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { resolveCliRuntimeExecutionProvider } from "../model-runtime-aliases.js";
import { resolveProviderIdForAuth } from "../provider-auth-aliases.js";
import { CLAUDE_CLI_PROFILE_ID } from "./constants.js";
import type { AuthProfileStore } from "./types.js";
const CLAUDE_CLI_PROVIDER_ID = "claude-cli";
/** Resolve external CLI overlay scope from the user's auth/model selection. */
export function resolveExternalCliAuthOverlayScopeFromSelection(params: {
provider: string;
cfg?: OpenClawConfig;
agentId?: string;
modelId?: string;
workspaceDir?: string;
store?: AuthProfileStore;
userPinnedAuthProfileId?: string;
}): {
providerIds?: readonly string[];
ignoreAutoPreferredProfile: boolean;
} {
const authScope = resolveExternalCliAuthScopeFromAuthSelection({
provider: params.provider,
cfg: params.cfg,
workspaceDir: params.workspaceDir,
store: params.store,
userPinnedAuthProfileId: params.userPinnedAuthProfileId,
});
const selectedRuntimeProvider =
resolveCliRuntimeExecutionProvider({
provider: params.provider,
cfg: params.cfg,
agentId: params.agentId,
modelId: params.modelId,
authProfileId: params.userPinnedAuthProfileId,
}) || (params.provider === CLAUDE_CLI_PROVIDER_ID ? CLAUDE_CLI_PROVIDER_ID : undefined);
const selectedProvider =
authScope.selectedProviderId ??
(selectedRuntimeProvider === CLAUDE_CLI_PROVIDER_ID ? CLAUDE_CLI_PROVIDER_ID : undefined);
const providerIds = [
...new Set([
...authScope.providerIds,
...(selectedRuntimeProvider === CLAUDE_CLI_PROVIDER_ID ? [CLAUDE_CLI_PROVIDER_ID] : []),
]),
];
return {
...(providerIds.length > 0 ? { providerIds } : {}),
ignoreAutoPreferredProfile:
// Claude CLI should not auto-prefer a profile when runtime selection has
// already chosen Claude CLI and the user did not pin a profile.
!params.userPinnedAuthProfileId && selectedProvider === CLAUDE_CLI_PROVIDER_ID,
};
}
function resolveExternalCliAuthScopeFromAuthSelection(params: {
provider: string;
cfg?: OpenClawConfig;
workspaceDir?: string;
store?: AuthProfileStore;
userPinnedAuthProfileId?: string;
}): {
providerIds: string[];
selectedProviderId?: string;
} {
const providerIds: string[] = [];
const orderedProfileIds = resolveConfiguredAuthProfileOrder(params);
const allProfileIds = [
...new Set([
...Object.keys(params.cfg?.auth?.profiles ?? {}),
...Object.keys(params.store?.profiles ?? {}),
]),
];
const discoveredProfileIds = orderedProfileIds.length > 0 ? orderedProfileIds : allProfileIds;
const profileIds = params.userPinnedAuthProfileId
? [
params.userPinnedAuthProfileId,
...discoveredProfileIds.filter((profileId) => profileId !== params.userPinnedAuthProfileId),
]
: discoveredProfileIds;
let sawCompatibleOrderedProfile = false;
let selectedProviderId: string | undefined;
let compatibleProfileCount = 0;
for (const profileId of profileIds) {
const resolved = resolveExternalCliProviderIdForCompatibleAuthProfile({
...params,
profileId,
});
if (!resolved.compatible) {
continue;
}
compatibleProfileCount += 1;
if (!sawCompatibleOrderedProfile) {
selectedProviderId = resolved.externalCliProviderId;
sawCompatibleOrderedProfile = true;
}
if (resolved.externalCliProviderId) {
providerIds.push(resolved.externalCliProviderId);
}
}
if (params.userPinnedAuthProfileId || orderedProfileIds.length > 0) {
return {
providerIds: [...new Set(providerIds)],
...(selectedProviderId ? { selectedProviderId } : {}),
};
}
const uniqueProviderIds = [...new Set(providerIds)];
return {
providerIds: uniqueProviderIds,
...(compatibleProfileCount === 1 && uniqueProviderIds[0]
? // Without explicit order, select only when compatibility is unambiguous.
{ selectedProviderId: uniqueProviderIds[0] }
: {}),
};
}
function resolveConfiguredAuthProfileOrder(params: {
provider: string;
cfg?: OpenClawConfig;
workspaceDir?: string;
store?: AuthProfileStore;
}): string[] {
const providerAuthKey = resolveProviderIdForAuth(params.provider, {
config: params.cfg,
workspaceDir: params.workspaceDir,
});
const orderedProfileIds =
resolveAuthProfileOrderEntries({
order: params.store?.order,
provider: params.provider,
providerAuthKey,
}) ??
resolveAuthProfileOrderEntries({
order: params.cfg?.auth?.order,
provider: params.provider,
providerAuthKey,
}) ??
[];
return [
...new Set(
orderedProfileIds
.map((profileId) => profileId?.trim())
.filter((profileId): profileId is string => Boolean(profileId)),
),
];
}
function resolveAuthProfileOrderEntries(params: {
order?: Record<string, string[]>;
provider: string;
providerAuthKey: string;
}): string[] | undefined {
return (
findNormalizedProviderValue(params.order, params.providerAuthKey) ??
(normalizeProviderId(params.providerAuthKey) === normalizeProviderId(params.provider)
? undefined
: findNormalizedProviderValue(params.order, params.provider))
);
}
function resolveExternalCliProviderIdForCompatibleAuthProfile(params: {
provider: string;
cfg?: OpenClawConfig;
workspaceDir?: string;
store?: AuthProfileStore;
profileId: string;
}): {
compatible: boolean;
externalCliProviderId?: string;
} {
const profile = params.cfg?.auth?.profiles?.[params.profileId];
const credential = params.store?.profiles?.[params.profileId];
const profileProvider =
profile?.provider ??
credential?.provider ??
(params.profileId === CLAUDE_CLI_PROFILE_ID ? CLAUDE_CLI_PROVIDER_ID : undefined);
if (!profileProvider) {
return { compatible: false };
}
const authAliasParams = {
config: params.cfg,
workspaceDir: params.workspaceDir,
};
const providerAuthKey = resolveProviderIdForAuth(params.provider, authAliasParams);
const profileAuthKey = resolveProviderIdForAuth(profileProvider, {
...authAliasParams,
storedCredential: true,
});
if (!providerAuthKey || profileAuthKey !== providerAuthKey) {
return { compatible: false };
}
return {
compatible: true,
...(normalizeProviderId(profileProvider) === CLAUDE_CLI_PROVIDER_ID
? { externalCliProviderId: CLAUDE_CLI_PROVIDER_ID }
: {}),
};
}
|