(config: Config)
| 25 | } |
| 26 | |
| 27 | export async function ensureAuth(config: Config): Promise<void> { |
| 28 | if (config.apiKey || config.fileApiKey) return; |
| 29 | if (await loadCredentials()) return; |
| 30 | |
| 31 | const envKey = process.env.MINIMAX_API_KEY; |
| 32 | if (envKey) { |
| 33 | if (!isInteractive({ nonInteractive: config.nonInteractive })) { |
| 34 | return; // env key is enough; no prompt |
| 35 | } |
| 36 | const save = await promptConfirm({ |
| 37 | message: `Found MINIMAX_API_KEY in environment (${maskToken(envKey)}). Save to config file?`, |
| 38 | }); |
| 39 | if (save) { |
| 40 | await persistApiKey(config, envKey); |
| 41 | } |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (!isInteractive({ nonInteractive: config.nonInteractive })) { |
| 46 | throw new CLIError( |
| 47 | 'No credentials found.', |
| 48 | ExitCode.AUTH, |
| 49 | 'Log in: mmx auth login\nPass directly: --api-key sk-xxxxx', |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | const choice = await pickAuthMethod(); |
| 54 | if (choice === 'api-key') { |
| 55 | const input = await promptText({ message: 'Enter your MiniMax API key:' }); |
| 56 | if (!input) throw new CLIError('API key is required.', ExitCode.AUTH); |
| 57 | await persistApiKey(config, input); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | await runOAuthLogin(choice === 'oauth-cn' ? 'cn' : 'global'); |
| 62 | } |
| 63 | |
| 64 | export async function pickAuthMethod(): Promise<AuthChoice['value']> { |
| 65 | const { select, isCancel } = await import('@clack/prompts'); |
no test coverage detected