File size: 4,268 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 | /**
* External CLI auth discovery mode helpers.
* Converts provider/config lookup contexts into scoped discovery options for
* auth profile store loading.
*/
import { normalizeTrimmedStringList } from "@openclaw/normalization-core/string-normalization";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import {
resolveExternalCliAuthScopeFromConfig,
type ExternalCliAuthScope,
} from "./external-cli-scope.js";
/** External CLI auth discovery mode used while loading auth profile stores. */
export type ExternalCliAuthDiscovery =
| {
mode: "none";
allowKeychainPrompt?: false;
config?: OpenClawConfig;
}
| {
mode: "existing";
allowKeychainPrompt?: boolean;
config?: OpenClawConfig;
}
| {
mode: "scoped";
allowKeychainPrompt?: boolean;
config?: OpenClawConfig;
providerIds?: Iterable<string>;
profileIds?: Iterable<string>;
};
type ProviderAuthDiscoveryParams = {
cfg?: OpenClawConfig;
provider: string;
profileId?: string;
preferredProfile?: string;
allowKeychainPrompt?: boolean;
};
type ConfigStatusDiscoveryParams = {
cfg: OpenClawConfig;
allowKeychainPrompt?: false;
};
type ProviderSetDiscoveryParams = {
cfg?: OpenClawConfig;
providers: Iterable<string>;
allowKeychainPrompt?: false;
};
function normalizeStringList(values: Iterable<string | undefined>): string[] {
return normalizeTrimmedStringList([...values]);
}
/** Disables external CLI auth discovery. */
function externalCliDiscoveryNone(params?: { config?: OpenClawConfig }): ExternalCliAuthDiscovery {
return {
mode: "none",
allowKeychainPrompt: false,
...(params?.config ? { config: params.config } : {}),
};
}
/** Allows external CLI auth discovery for specific providers and/or profiles. */
export function externalCliDiscoveryScoped(params: {
config?: OpenClawConfig;
providerIds?: Iterable<string>;
profileIds?: Iterable<string>;
allowKeychainPrompt?: boolean;
}): ExternalCliAuthDiscovery {
return {
mode: "scoped",
...(params.allowKeychainPrompt !== undefined
? { allowKeychainPrompt: params.allowKeychainPrompt }
: {}),
...(params.config ? { config: params.config } : {}),
...(params.providerIds ? { providerIds: params.providerIds } : {}),
...(params.profileIds ? { profileIds: params.profileIds } : {}),
};
}
/** Builds external CLI discovery options for a provider auth lookup. */
export function externalCliDiscoveryForProviderAuth(
params: ProviderAuthDiscoveryParams,
): ExternalCliAuthDiscovery {
const profileIds = normalizeStringList([params.profileId, params.preferredProfile]);
return externalCliDiscoveryScoped({
config: params.cfg,
allowKeychainPrompt: params.allowKeychainPrompt ?? false,
providerIds: [params.provider],
...(profileIds.length > 0 ? { profileIds } : {}),
});
}
/** Builds external CLI discovery options for config status checks. */
export function externalCliDiscoveryForConfigStatus(
params: ConfigStatusDiscoveryParams,
): ExternalCliAuthDiscovery {
const scope = resolveExternalCliAuthScopeFromConfig(params.cfg);
return externalCliDiscoveryFromScope({
cfg: params.cfg,
scope,
allowKeychainPrompt: params.allowKeychainPrompt ?? false,
});
}
/** Builds external CLI discovery options for a provider set. */
export function externalCliDiscoveryForProviders(
params: ProviderSetDiscoveryParams,
): ExternalCliAuthDiscovery {
const providers = normalizeStringList(params.providers);
if (providers.length === 0) {
return externalCliDiscoveryNone({ config: params.cfg });
}
return externalCliDiscoveryScoped({
config: params.cfg,
allowKeychainPrompt: params.allowKeychainPrompt ?? false,
providerIds: providers,
});
}
function externalCliDiscoveryFromScope(params: {
cfg: OpenClawConfig;
scope: ExternalCliAuthScope | undefined;
allowKeychainPrompt: false;
}): ExternalCliAuthDiscovery {
if (!params.scope) {
return externalCliDiscoveryNone({ config: params.cfg });
}
return externalCliDiscoveryScoped({
config: params.cfg,
allowKeychainPrompt: params.allowKeychainPrompt,
providerIds: params.scope.providerIds,
profileIds: params.scope.profileIds,
});
}
|