mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:08:25 +00:00
refactor(commands): dedupe moonshot api key prompt
This commit is contained in:
@@ -127,6 +127,35 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ensureMoonshotApiKeyCredential(promptMessage: string): Promise<void> {
|
||||||
|
let hasCredential = false;
|
||||||
|
|
||||||
|
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "moonshot") {
|
||||||
|
await setMoonshotApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const envKey = resolveEnvApiKey("moonshot");
|
||||||
|
if (envKey) {
|
||||||
|
const useExisting = await params.prompter.confirm({
|
||||||
|
message: `Use existing MOONSHOT_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
|
||||||
|
initialValue: true,
|
||||||
|
});
|
||||||
|
if (useExisting) {
|
||||||
|
await setMoonshotApiKey(envKey.apiKey, params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasCredential) {
|
||||||
|
const key = await params.prompter.text({
|
||||||
|
message: promptMessage,
|
||||||
|
validate: validateApiKeyInput,
|
||||||
|
});
|
||||||
|
await setMoonshotApiKey(normalizeApiKeyInput(String(key ?? "")), params.agentDir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (authChoice === "openrouter-api-key") {
|
if (authChoice === "openrouter-api-key") {
|
||||||
return applyAuthChoiceOpenRouter(params);
|
return applyAuthChoiceOpenRouter(params);
|
||||||
}
|
}
|
||||||
@@ -346,31 +375,7 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (authChoice === "moonshot-api-key") {
|
if (authChoice === "moonshot-api-key") {
|
||||||
let hasCredential = false;
|
await ensureMoonshotApiKeyCredential("Enter Moonshot API key");
|
||||||
|
|
||||||
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "moonshot") {
|
|
||||||
await setMoonshotApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
|
||||||
hasCredential = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const envKey = resolveEnvApiKey("moonshot");
|
|
||||||
if (envKey) {
|
|
||||||
const useExisting = await params.prompter.confirm({
|
|
||||||
message: `Use existing MOONSHOT_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
|
|
||||||
initialValue: true,
|
|
||||||
});
|
|
||||||
if (useExisting) {
|
|
||||||
await setMoonshotApiKey(envKey.apiKey, params.agentDir);
|
|
||||||
hasCredential = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hasCredential) {
|
|
||||||
const key = await params.prompter.text({
|
|
||||||
message: "Enter Moonshot API key",
|
|
||||||
validate: validateApiKeyInput,
|
|
||||||
});
|
|
||||||
await setMoonshotApiKey(normalizeApiKeyInput(String(key ?? "")), params.agentDir);
|
|
||||||
}
|
|
||||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||||
profileId: "moonshot:default",
|
profileId: "moonshot:default",
|
||||||
provider: "moonshot",
|
provider: "moonshot",
|
||||||
@@ -393,31 +398,7 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (authChoice === "moonshot-api-key-cn") {
|
if (authChoice === "moonshot-api-key-cn") {
|
||||||
let hasCredential = false;
|
await ensureMoonshotApiKeyCredential("Enter Moonshot API key (.cn)");
|
||||||
|
|
||||||
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "moonshot") {
|
|
||||||
await setMoonshotApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
|
||||||
hasCredential = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const envKey = resolveEnvApiKey("moonshot");
|
|
||||||
if (envKey) {
|
|
||||||
const useExisting = await params.prompter.confirm({
|
|
||||||
message: `Use existing MOONSHOT_API_KEY (${envKey.source}, ${formatApiKeyPreview(envKey.apiKey)})?`,
|
|
||||||
initialValue: true,
|
|
||||||
});
|
|
||||||
if (useExisting) {
|
|
||||||
await setMoonshotApiKey(envKey.apiKey, params.agentDir);
|
|
||||||
hasCredential = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!hasCredential) {
|
|
||||||
const key = await params.prompter.text({
|
|
||||||
message: "Enter Moonshot API key (.cn)",
|
|
||||||
validate: validateApiKeyInput,
|
|
||||||
});
|
|
||||||
await setMoonshotApiKey(normalizeApiKeyInput(String(key ?? "")), params.agentDir);
|
|
||||||
}
|
|
||||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||||
profileId: "moonshot:default",
|
profileId: "moonshot:default",
|
||||||
provider: "moonshot",
|
provider: "moonshot",
|
||||||
|
|||||||
Reference in New Issue
Block a user