|
| 1 | +import { AuthAnthropic } from "../../auth/anthropic" |
| 2 | +import { AuthKeys } from "../../auth/keys" |
| 3 | +import { UI } from "../ui" |
| 4 | +import { cmd } from "./cmd" |
| 5 | +import * as prompts from "@clack/prompts" |
| 6 | +import open from "open" |
| 7 | + |
| 8 | +const OPENCODE = [ |
| 9 | + `█▀▀█ █▀▀█ █▀▀ █▀▀▄ █▀▀ █▀▀█ █▀▀▄ █▀▀`, |
| 10 | + `█░░█ █░░█ █▀▀ █░░█ █░░ █░░█ █░░█ █▀▀`, |
| 11 | + `▀▀▀▀ █▀▀▀ ▀▀▀ ▀ ▀ ▀▀▀ ▀▀▀▀ ▀▀▀ ▀▀▀`, |
| 12 | +] |
| 13 | + |
| 14 | +export const ProviderCommand = cmd({ |
| 15 | + command: "provider", |
| 16 | + builder: (yargs) => |
| 17 | + yargs |
| 18 | + .command(ProviderAddCommand) |
| 19 | + .command(ProviderListCommand) |
| 20 | + .demandCommand(), |
| 21 | + describe: "initialize opencode", |
| 22 | + async handler() {}, |
| 23 | +}) |
| 24 | + |
| 25 | +export const ProviderListCommand = cmd({ |
| 26 | + command: "list", |
| 27 | + aliases: ["ls"], |
| 28 | + describe: "list providers", |
| 29 | + async handler() { |
| 30 | + prompts.intro("Configured Providers") |
| 31 | + const keys = await AuthKeys.get() |
| 32 | + for (const key of Object.keys(keys)) { |
| 33 | + prompts.log.success(key) |
| 34 | + } |
| 35 | + prompts.outro("3 providers configured") |
| 36 | + }, |
| 37 | +}) |
| 38 | + |
| 39 | +const ProviderAddCommand = cmd({ |
| 40 | + command: "add", |
| 41 | + describe: "add credentials for various providers", |
| 42 | + async handler() { |
| 43 | + UI.empty() |
| 44 | + for (const row of OPENCODE) { |
| 45 | + UI.print(" ") |
| 46 | + for (let i = 0; i < row.length; i++) { |
| 47 | + const color = |
| 48 | + i < 18 ? Bun.color("white", "ansi") : Bun.color("gray", "ansi") |
| 49 | + const char = row[i] |
| 50 | + UI.print(color + char) |
| 51 | + } |
| 52 | + UI.println() |
| 53 | + } |
| 54 | + UI.empty() |
| 55 | + |
| 56 | + prompts.intro("Setup") |
| 57 | + const keys = await AuthKeys.get() |
| 58 | + const provider = await prompts.select({ |
| 59 | + message: "Configure a provider", |
| 60 | + options: [ |
| 61 | + { |
| 62 | + label: "Anthropic", |
| 63 | + value: "anthropic", |
| 64 | + hint: keys["anthropic"] ? "configured" : "", |
| 65 | + }, |
| 66 | + { |
| 67 | + label: "OpenAI", |
| 68 | + value: "openai", |
| 69 | + hint: keys["openai"] ? "configured" : "", |
| 70 | + }, |
| 71 | + { |
| 72 | + label: "Google", |
| 73 | + value: "google", |
| 74 | + hint: keys["google"] ? "configured" : "", |
| 75 | + }, |
| 76 | + ], |
| 77 | + }) |
| 78 | + if (prompts.isCancel(provider)) return |
| 79 | + |
| 80 | + if (provider === "anthropic") { |
| 81 | + const method = await prompts.select({ |
| 82 | + message: "Login method", |
| 83 | + options: [ |
| 84 | + { |
| 85 | + label: "Claude Pro/Max", |
| 86 | + value: "oauth", |
| 87 | + }, |
| 88 | + { |
| 89 | + label: "API Key", |
| 90 | + value: "api", |
| 91 | + }, |
| 92 | + ], |
| 93 | + }) |
| 94 | + if (prompts.isCancel(method)) return |
| 95 | + |
| 96 | + if (method === "oauth") { |
| 97 | + // some weird bug where program exits without this |
| 98 | + await new Promise((resolve) => setTimeout(resolve, 10)) |
| 99 | + const { url, verifier } = await AuthAnthropic.authorize() |
| 100 | + prompts.note("Opening browser...") |
| 101 | + await open(url) |
| 102 | + prompts.log.info(url) |
| 103 | + |
| 104 | + const code = await prompts.text({ |
| 105 | + message: "Paste the authorization code here: ", |
| 106 | + validate: (x) => (x.length > 0 ? undefined : "Required"), |
| 107 | + }) |
| 108 | + if (prompts.isCancel(code)) return |
| 109 | + await AuthAnthropic.exchange(code, verifier) |
| 110 | + .then(() => { |
| 111 | + prompts.log.success("Login successful") |
| 112 | + }) |
| 113 | + .catch(() => { |
| 114 | + prompts.log.error("Invalid code") |
| 115 | + }) |
| 116 | + prompts.outro("Done") |
| 117 | + return |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + const key = await prompts.password({ |
| 122 | + message: "Enter your API key", |
| 123 | + }) |
| 124 | + if (prompts.isCancel(key)) return |
| 125 | + await AuthKeys.set(provider, key) |
| 126 | + |
| 127 | + prompts.outro("Done") |
| 128 | + }, |
| 129 | +}) |
0 commit comments