()
| 11 | import { openUrl } from '#/utils/open-url'; |
| 12 | |
| 13 | export async function runLoginFlow(): Promise<never> { |
| 14 | const identity = createKimiCodeHostIdentity(); |
| 15 | const harness = createKimiHarness({ |
| 16 | identity, |
| 17 | uiMode: 'cli', |
| 18 | }); |
| 19 | const controller = new AbortController(); |
| 20 | process.once('SIGINT', () => { |
| 21 | controller.abort(); |
| 22 | }); |
| 23 | try { |
| 24 | const result = await harness.auth.login(undefined, { |
| 25 | signal: controller.signal, |
| 26 | onDeviceCode: (data) => { |
| 27 | const url = data.verificationUriComplete || data.verificationUri; |
| 28 | // Print the manual fallback before attempting to open the user's |
| 29 | // browser so headless/browser-opener failures never hide the URL |
| 30 | // and code needed to complete login. |
| 31 | process.stderr.write( |
| 32 | [ |
| 33 | '', |
| 34 | `Opening browser for Kimi device login: ${url}`, |
| 35 | `If the browser did not open, paste the URL above and enter code: ${data.userCode}`, |
| 36 | data.expiresIn !== null && data.expiresIn !== undefined |
| 37 | ? `Code expires in ${data.expiresIn}s.` |
| 38 | : undefined, |
| 39 | 'Waiting for authorization to complete...', |
| 40 | '', |
| 41 | ] |
| 42 | .filter((line): line is string => line !== undefined) |
| 43 | .join('\n'), |
| 44 | ); |
| 45 | try { |
| 46 | openUrl(url); |
| 47 | } catch { |
| 48 | // Best effort only: the manual fallback has already been printed. |
| 49 | } |
| 50 | }, |
| 51 | }); |
| 52 | process.stderr.write(`Logged in to ${result.providerName}.\n`); |
| 53 | process.exit(0); |
| 54 | } catch (error) { |
| 55 | if (controller.signal.aborted) { |
| 56 | process.stderr.write('Login cancelled.\n'); |
| 57 | } else { |
| 58 | const message = error instanceof Error ? error.message : String(error); |
| 59 | process.stderr.write(`Login failed: ${message}\n`); |
| 60 | } |
| 61 | process.exit(1); |
| 62 | } |
| 63 | } |
no test coverage detected