@@ -7,15 +7,13 @@ import {
77 } from "@mariozechner/pi-ai";
88 import {
99 getOAuthApiKey,
10+ getOAuthProvider,
1011 getOAuthProviders,
11- loginOpenAICodex,
1212 type OAuthProviderId,
1313 } from "@mariozechner/pi-ai/oauth";
1414 import { AUTH_PATH as AUTH_FILE } from "./shared";
1515 import type { CliOptions, SavedOAuthCreds } from "./types";
1616
17-// const AUTH_FILE = "./.auth.json";
18-
1917 export function isOAuthProvider(provider: string): boolean {
2018 return getOAuthProviders().some(
2119 (oauthProvider) => oauthProvider.id === provider,
@@ -44,36 +42,34 @@ export async function getAvailableProviders(): Promise<KnownProvider[]> {
4442 }
4543
4644 export async function loginOAuth(provider: OAuthProviderId) {
45+ const oauthProvider = getOAuthProvider(provider);
46+ if (!oauthProvider) throw new Error(`Unknown OAuth provider: ${provider}`);
47+
4748 const rl = readline.createInterface({
4849 input: process.stdin,
4950 output: process.stdout,
5051 });
5152
52- switch (provider) {
53- // TODO: other providers :)
54- case "openai-codex": {
55- const creds = await loginOpenAICodex({
56- onAuth: ({ url, instructions }) => {
57- console.log(`Open: ${url}`);
58- if (instructions) console.log(instructions);
59- },
60- onPrompt: async (prompt) => {
61- let answer: string = "";
62- await rl.question(prompt.message, (a) => (answer = a));
63- return answer;
64- },
65- onProgress: (message) => console.log(message),
66- });
67-
68- await writeCreds({ "openai-codex": { type: "oauth", ...creds } });
69- break;
70- }
71- default:
72- throw new Error("Unknown provider");
53+ try {
54+ const creds = await oauthProvider.login({
55+ onAuth: ({ url, instructions }) => {
56+ console.log(`Open: ${url}`);
57+ if (instructions) console.log(instructions);
58+ },
59+ onPrompt: async (prompt) => {
60+ let answer: string = "";
61+ await rl.question(prompt.message, (a) => (answer = a));
62+ return answer;
63+ },
64+ onProgress: (message) => console.log(message),
65+ });
66+
67+ await writeCreds({ [provider]: { type: "oauth", ...creds } });
68+ } finally {
69+ rl.close();
7370 }
7471
75- const savedCreds = await readCreds();
76- return savedCreds;
72+ return await readCreds();
7773 }
7874
7975 export async function getApiKey(options: CliOptions) {