fix: support direct token and provider in auth apply commands (#1485)
This commit is contained in:
@@ -198,10 +198,20 @@ export async function applyAuthChoiceAnthropic(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "apiKey") {
|
if (params.authChoice === "apiKey") {
|
||||||
|
if (params.opts?.tokenProvider && params.opts.tokenProvider !== "anthropic") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
let nextConfig = params.config;
|
let nextConfig = params.config;
|
||||||
let hasCredential = false;
|
let hasCredential = false;
|
||||||
const envKey = process.env.ANTHROPIC_API_KEY?.trim();
|
const envKey = process.env.ANTHROPIC_API_KEY?.trim();
|
||||||
if (envKey) {
|
|
||||||
|
if (params.opts?.token) {
|
||||||
|
await setAnthropicApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasCredential && envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
message: `Use existing ANTHROPIC_API_KEY (env, ${formatApiKeyPreview(envKey)})?`,
|
message: `Use existing ANTHROPIC_API_KEY (env, ${formatApiKeyPreview(envKey)})?`,
|
||||||
initialValue: true,
|
initialValue: true,
|
||||||
|
|||||||
@@ -56,7 +56,33 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (params.authChoice === "openrouter-api-key") {
|
let authChoice = params.authChoice;
|
||||||
|
if (
|
||||||
|
authChoice === "apiKey" &&
|
||||||
|
params.opts?.tokenProvider &&
|
||||||
|
params.opts.tokenProvider !== "anthropic" &&
|
||||||
|
params.opts.tokenProvider !== "openai"
|
||||||
|
) {
|
||||||
|
if (params.opts.tokenProvider === "openrouter") {
|
||||||
|
authChoice = "openrouter-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "vercel-ai-gateway") {
|
||||||
|
authChoice = "ai-gateway-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "moonshot") {
|
||||||
|
authChoice = "moonshot-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "kimi-code") {
|
||||||
|
authChoice = "kimi-code-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "google") {
|
||||||
|
authChoice = "gemini-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "zai") {
|
||||||
|
authChoice = "zai-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "synthetic") {
|
||||||
|
authChoice = "synthetic-api-key";
|
||||||
|
} else if (params.opts.tokenProvider === "opencode") {
|
||||||
|
authChoice = "opencode-zen";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authChoice === "openrouter-api-key") {
|
||||||
const store = ensureAuthProfileStore(params.agentDir, {
|
const store = ensureAuthProfileStore(params.agentDir, {
|
||||||
allowKeychainPrompt: false,
|
allowKeychainPrompt: false,
|
||||||
});
|
});
|
||||||
@@ -82,6 +108,11 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
hasCredential = true;
|
hasCredential = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "openrouter") {
|
||||||
|
await setOpenrouterApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (!hasCredential) {
|
if (!hasCredential) {
|
||||||
const envKey = resolveEnvApiKey("openrouter");
|
const envKey = resolveEnvApiKey("openrouter");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
@@ -129,8 +160,18 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "ai-gateway-api-key") {
|
if (authChoice === "ai-gateway-api-key") {
|
||||||
let hasCredential = false;
|
let hasCredential = false;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!hasCredential &&
|
||||||
|
params.opts?.token &&
|
||||||
|
params.opts?.tokenProvider === "vercel-ai-gateway"
|
||||||
|
) {
|
||||||
|
await setVercelAiGatewayApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
const envKey = resolveEnvApiKey("vercel-ai-gateway");
|
const envKey = resolveEnvApiKey("vercel-ai-gateway");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
@@ -171,8 +212,14 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "moonshot-api-key") {
|
if (authChoice === "moonshot-api-key") {
|
||||||
let hasCredential = false;
|
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");
|
const envKey = resolveEnvApiKey("moonshot");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
@@ -212,7 +259,14 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "kimi-code-api-key") {
|
if (authChoice === "kimi-code-api-key") {
|
||||||
|
let hasCredential = false;
|
||||||
|
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "kimi-code") {
|
||||||
|
await setKimiCodeApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasCredential) {
|
||||||
await params.prompter.note(
|
await params.prompter.note(
|
||||||
[
|
[
|
||||||
"Kimi Code uses a dedicated endpoint and API key.",
|
"Kimi Code uses a dedicated endpoint and API key.",
|
||||||
@@ -220,7 +274,7 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
].join("\n"),
|
].join("\n"),
|
||||||
"Kimi Code",
|
"Kimi Code",
|
||||||
);
|
);
|
||||||
let hasCredential = false;
|
}
|
||||||
const envKey = resolveEnvApiKey("kimi-code");
|
const envKey = resolveEnvApiKey("kimi-code");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
@@ -261,8 +315,14 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "gemini-api-key") {
|
if (authChoice === "gemini-api-key") {
|
||||||
let hasCredential = false;
|
let hasCredential = false;
|
||||||
|
|
||||||
|
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "google") {
|
||||||
|
await setGeminiApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
const envKey = resolveEnvApiKey("google");
|
const envKey = resolveEnvApiKey("google");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
@@ -302,8 +362,14 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "zai-api-key") {
|
if (authChoice === "zai-api-key") {
|
||||||
let hasCredential = false;
|
let hasCredential = false;
|
||||||
|
|
||||||
|
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "zai") {
|
||||||
|
await setZaiApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
const envKey = resolveEnvApiKey("zai");
|
const envKey = resolveEnvApiKey("zai");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
@@ -359,12 +425,16 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "synthetic-api-key") {
|
if (authChoice === "synthetic-api-key") {
|
||||||
|
if (params.opts?.token && params.opts?.tokenProvider === "synthetic") {
|
||||||
|
await setSyntheticApiKey(String(params.opts.token).trim(), params.agentDir);
|
||||||
|
} else {
|
||||||
const key = await params.prompter.text({
|
const key = await params.prompter.text({
|
||||||
message: "Enter Synthetic API key",
|
message: "Enter Synthetic API key",
|
||||||
validate: (value) => (value?.trim() ? undefined : "Required"),
|
validate: (value) => (value?.trim() ? undefined : "Required"),
|
||||||
});
|
});
|
||||||
await setSyntheticApiKey(String(key).trim(), params.agentDir);
|
await setSyntheticApiKey(String(key).trim(), params.agentDir);
|
||||||
|
}
|
||||||
nextConfig = applyAuthProfileConfig(nextConfig, {
|
nextConfig = applyAuthProfileConfig(nextConfig, {
|
||||||
profileId: "synthetic:default",
|
profileId: "synthetic:default",
|
||||||
provider: "synthetic",
|
provider: "synthetic",
|
||||||
@@ -387,7 +457,14 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
return { config: nextConfig, agentModelOverride };
|
return { config: nextConfig, agentModelOverride };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.authChoice === "opencode-zen") {
|
if (authChoice === "opencode-zen") {
|
||||||
|
let hasCredential = false;
|
||||||
|
if (!hasCredential && params.opts?.token && params.opts?.tokenProvider === "opencode") {
|
||||||
|
await setOpencodeZenApiKey(normalizeApiKeyInput(params.opts.token), params.agentDir);
|
||||||
|
hasCredential = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hasCredential) {
|
||||||
await params.prompter.note(
|
await params.prompter.note(
|
||||||
[
|
[
|
||||||
"OpenCode Zen provides access to Claude, GPT, Gemini, and more models.",
|
"OpenCode Zen provides access to Claude, GPT, Gemini, and more models.",
|
||||||
@@ -396,7 +473,7 @@ export async function applyAuthChoiceApiProviders(
|
|||||||
].join("\n"),
|
].join("\n"),
|
||||||
"OpenCode Zen",
|
"OpenCode Zen",
|
||||||
);
|
);
|
||||||
let hasCredential = false;
|
}
|
||||||
const envKey = resolveEnvApiKey("opencode");
|
const envKey = resolveEnvApiKey("opencode");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
|
|||||||
@@ -20,7 +20,12 @@ import {
|
|||||||
export async function applyAuthChoiceOpenAI(
|
export async function applyAuthChoiceOpenAI(
|
||||||
params: ApplyAuthChoiceParams,
|
params: ApplyAuthChoiceParams,
|
||||||
): Promise<ApplyAuthChoiceResult | null> {
|
): Promise<ApplyAuthChoiceResult | null> {
|
||||||
if (params.authChoice === "openai-api-key") {
|
let authChoice = params.authChoice;
|
||||||
|
if (authChoice === "apiKey" && params.opts?.tokenProvider === "openai") {
|
||||||
|
authChoice = "openai-api-key";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authChoice === "openai-api-key") {
|
||||||
const envKey = resolveEnvApiKey("openai");
|
const envKey = resolveEnvApiKey("openai");
|
||||||
if (envKey) {
|
if (envKey) {
|
||||||
const useExisting = await params.prompter.confirm({
|
const useExisting = await params.prompter.confirm({
|
||||||
@@ -43,10 +48,16 @@ export async function applyAuthChoiceOpenAI(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const key = await params.prompter.text({
|
let key: string | undefined;
|
||||||
|
if (params.opts?.token && params.opts?.tokenProvider === "openai") {
|
||||||
|
key = params.opts.token;
|
||||||
|
} else {
|
||||||
|
key = await params.prompter.text({
|
||||||
message: "Enter OpenAI API key",
|
message: "Enter OpenAI API key",
|
||||||
validate: validateApiKeyInput,
|
validate: validateApiKeyInput,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const trimmed = normalizeApiKeyInput(String(key));
|
const trimmed = normalizeApiKeyInput(String(key));
|
||||||
const result = upsertSharedEnvVar({
|
const result = upsertSharedEnvVar({
|
||||||
key: "OPENAI_API_KEY",
|
key: "OPENAI_API_KEY",
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ export type ApplyAuthChoiceParams = {
|
|||||||
agentDir?: string;
|
agentDir?: string;
|
||||||
setDefaultModel: boolean;
|
setDefaultModel: boolean;
|
||||||
agentId?: string;
|
agentId?: string;
|
||||||
|
opts?: {
|
||||||
|
tokenProvider?: string;
|
||||||
|
token?: string;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ApplyAuthChoiceResult = {
|
export type ApplyAuthChoiceResult = {
|
||||||
|
|||||||
@@ -356,6 +356,10 @@ export async function runOnboardingWizard(
|
|||||||
prompter,
|
prompter,
|
||||||
runtime,
|
runtime,
|
||||||
setDefaultModel: true,
|
setDefaultModel: true,
|
||||||
|
opts: {
|
||||||
|
tokenProvider: opts.tokenProvider,
|
||||||
|
token: opts.authChoice === "apiKey" && opts.token ? opts.token : undefined,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
nextConfig = authResult.config;
|
nextConfig = authResult.config;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user