|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | 3 | import { defineCommand, runMain } from "citty" |
4 | | -import clipboard from "clipboardy" |
5 | | -import consola from "consola" |
6 | | -import { serve, type ServerHandler } from "srvx" |
7 | | -import invariant from "tiny-invariant" |
8 | 4 |
|
9 | 5 | import { auth } from "./auth" |
10 | | -import { ensurePaths } from "./lib/paths" |
11 | | -import { generateEnvScript } from "./lib/shell" |
12 | | -import { state } from "./lib/state" |
13 | | -import { setupCopilotToken, setupGitHubToken } from "./lib/token" |
14 | | -import { cacheModels, cacheVSCodeVersion } from "./lib/utils" |
15 | | -import { server } from "./server" |
16 | | - |
17 | | -interface RunServerOptions { |
18 | | - port: number |
19 | | - verbose: boolean |
20 | | - accountType: string |
21 | | - manual: boolean |
22 | | - rateLimit?: number |
23 | | - rateLimitWait: boolean |
24 | | - githubToken?: string |
25 | | - launchClaudeCode: boolean |
26 | | -} |
27 | | - |
28 | | -// eslint-disable-next-line max-lines-per-function |
29 | | -export async function runServer(options: RunServerOptions): Promise<void> { |
30 | | - if (options.verbose) { |
31 | | - consola.level = 5 |
32 | | - consola.info("Verbose logging enabled") |
33 | | - } |
34 | | - |
35 | | - state.accountType = options.accountType |
36 | | - if (options.accountType !== "individual") { |
37 | | - consola.info(`Using ${options.accountType} plan GitHub account`) |
38 | | - } |
39 | | - |
40 | | - state.manualApprove = options.manual |
41 | | - state.rateLimitSeconds = options.rateLimit |
42 | | - state.rateLimitWait = options.rateLimitWait |
43 | | - |
44 | | - await ensurePaths() |
45 | | - await cacheVSCodeVersion() |
46 | | - |
47 | | - if (options.githubToken) { |
48 | | - state.githubToken = options.githubToken |
49 | | - consola.info("Using provided GitHub token") |
50 | | - } else { |
51 | | - await setupGitHubToken() |
52 | | - } await setupCopilotToken() |
53 | | - await cacheModels() // Display token information prominently |
54 | | - consola.info("=".repeat(50)) |
55 | | - consola.success("🚀 GitHub Copilot API has been successfully started!") |
56 | | - consola.info(`🔑 Current Copilot Token: ${state.copilotToken}`) |
57 | | - consola.info(`🌐 Usage Viewer: http://localhost:${options.port}/public/usage.html`) |
58 | | - consola.info("=".repeat(50)) |
59 | | - |
60 | | - consola.info( |
61 | | - `Available models: \n${state.models?.data.map((model) => `- ${model.id}`).join("\n")}`, |
62 | | - ) |
63 | | - |
64 | | - const serverUrl = `http://localhost:${options.port}` |
65 | | - |
66 | | - if (options.launchClaudeCode) { |
67 | | - invariant(state.models, "Models should be loaded by now") |
68 | | - |
69 | | - const selectedModel = await consola.prompt( |
70 | | - "Select a model to use with Claude Code", |
71 | | - { |
72 | | - type: "select", |
73 | | - options: state.models.data.map((model) => model.id), |
74 | | - }, |
75 | | - ) |
76 | | - |
77 | | - const selectedSmallModel = await consola.prompt( |
78 | | - "Select a small model to use with Claude Code", |
79 | | - { |
80 | | - type: "select", |
81 | | - options: state.models.data.map((model) => model.id), |
82 | | - }, |
83 | | - ) |
84 | | - |
85 | | - const command = generateEnvScript( |
86 | | - { |
87 | | - ANTHROPIC_BASE_URL: serverUrl, |
88 | | - ANTHROPIC_AUTH_TOKEN: "dummy", |
89 | | - ANTHROPIC_MODEL: selectedModel, |
90 | | - ANTHROPIC_SMALL_FAST_MODEL: selectedSmallModel, |
91 | | - }, |
92 | | - "claude", |
93 | | - ) |
94 | | - |
95 | | - clipboard.writeSync(command) |
96 | | - consola.success("Copied Claude Code command to clipboard!") |
97 | | - } |
98 | | - |
99 | | - serve({ |
100 | | - fetch: server.fetch as ServerHandler, |
101 | | - port: options.port, |
102 | | - }) |
103 | | -} |
104 | | - |
105 | | -const start = defineCommand({ |
106 | | - meta: { |
107 | | - name: "start", |
108 | | - description: "Start the Copilot API server", |
109 | | - }, |
110 | | - args: { |
111 | | - port: { |
112 | | - alias: "p", |
113 | | - type: "string", |
114 | | - default: "4141", |
115 | | - description: "Port to listen on", |
116 | | - }, |
117 | | - verbose: { |
118 | | - alias: "v", |
119 | | - type: "boolean", |
120 | | - default: false, |
121 | | - description: "Enable verbose logging", |
122 | | - }, |
123 | | - "account-type": { |
124 | | - alias: "a", |
125 | | - type: "string", |
126 | | - default: "individual", |
127 | | - description: "Account type to use (individual, business, enterprise)", |
128 | | - }, |
129 | | - manual: { |
130 | | - type: "boolean", |
131 | | - default: false, |
132 | | - description: "Enable manual request approval", |
133 | | - }, |
134 | | - "rate-limit": { |
135 | | - alias: "r", |
136 | | - type: "string", |
137 | | - description: "Rate limit in seconds between requests", |
138 | | - }, |
139 | | - wait: { |
140 | | - alias: "w", |
141 | | - type: "boolean", |
142 | | - default: false, |
143 | | - description: |
144 | | - "Wait instead of error when rate limit is hit. Has no effect if rate limit is not set", |
145 | | - }, |
146 | | - "github-token": { |
147 | | - alias: "g", |
148 | | - type: "string", |
149 | | - description: |
150 | | - "Provide GitHub token directly (must be generated using the `auth` subcommand)", |
151 | | - }, |
152 | | - "claude-code": { |
153 | | - alias: "c", |
154 | | - type: "boolean", |
155 | | - default: false, |
156 | | - description: |
157 | | - "Generate a command to launch Claude Code with Copilot API config", |
158 | | - }, |
159 | | - }, |
160 | | - run({ args }) { |
161 | | - const rateLimitRaw = args["rate-limit"] |
162 | | - const rateLimit = |
163 | | - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
164 | | - rateLimitRaw === undefined ? undefined : Number.parseInt(rateLimitRaw, 10) |
165 | | - |
166 | | - return runServer({ |
167 | | - port: Number.parseInt(args.port, 10), |
168 | | - verbose: args.verbose, |
169 | | - accountType: args["account-type"], |
170 | | - manual: args.manual, |
171 | | - rateLimit, |
172 | | - rateLimitWait: Boolean(args.wait), |
173 | | - githubToken: args["github-token"], |
174 | | - launchClaudeCode: args["claude-code"], |
175 | | - }) |
176 | | - }, |
177 | | -}) |
| 6 | +import { start } from "./start" |
178 | 7 |
|
179 | 8 | const main = defineCommand({ |
180 | 9 | meta: { |
|
0 commit comments