forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
147 lines (131 loc) · 3.61 KB
/
main.ts
File metadata and controls
147 lines (131 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node
import { defineCommand, runMain } from "citty"
import consola from "consola"
import { serve, type ServerHandler } from "srvx"
import { auth } from "./auth"
import { cacheModels } from "./lib/models"
import { ensurePaths } from "./lib/paths"
import { state } from "./lib/state"
import { setupCopilotToken, setupGitHubToken } from "./lib/token"
import { cacheVSCodeVersion } from "./lib/vscode-version"
import { server } from "./server"
interface RunServerOptions {
port: number
verbose: boolean
business: boolean
enterprise: boolean
manual: boolean
rateLimit?: number
rateLimitWait: boolean
githubToken?: string
}
export async function runServer(options: RunServerOptions): Promise<void> {
if (options.verbose) {
consola.level = 5
consola.info("Verbose logging enabled")
}
if (options.business) {
state.accountType = "business"
consola.info("Using business plan GitHub account")
} else if (options.enterprise) {
state.accountType = "enterprise"
consola.info("Using enterprise plan GitHub account")
}
state.manualApprove = options.manual
state.rateLimitSeconds = options.rateLimit
state.rateLimitWait = options.rateLimitWait
await ensurePaths()
await cacheVSCodeVersion()
if (options.githubToken) {
state.githubToken = options.githubToken
consola.info("Using provided GitHub token")
} else {
await setupGitHubToken()
}
await setupCopilotToken()
await cacheModels()
const serverUrl = `http://localhost:${options.port}`
consola.box(`Server started at ${serverUrl}`)
serve({
fetch: server.fetch as ServerHandler,
port: options.port,
})
}
const start = defineCommand({
meta: {
name: "start",
description: "Start the Copilot API server",
},
args: {
port: {
alias: "p",
type: "string",
default: "4141",
description: "Port to listen on",
},
verbose: {
alias: "v",
type: "boolean",
default: false,
description: "Enable verbose logging",
},
business: {
type: "boolean",
default: false,
description: "Use a business plan GitHub Account",
},
enterprise: {
type: "boolean",
default: false,
description: "Use a enterprise plan GitHub Account",
},
manual: {
type: "boolean",
default: false,
description: "Enable manual request approval",
},
"rate-limit": {
alias: "r",
type: "string",
description: "Rate limit in seconds between requests",
},
wait: {
alias: "w",
type: "boolean",
default: false,
description:
"Wait instead of error when rate limit is hit. Has no effect if rate limit is not set",
},
"github-token": {
alias: "g",
type: "string",
description:
"Provide GitHub token directly (must be generated using the `auth` subcommand)",
},
},
run({ args }) {
const rateLimitRaw = args["rate-limit"]
const rateLimit =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
rateLimitRaw === undefined ? undefined : Number.parseInt(rateLimitRaw, 10)
const port = Number.parseInt(args.port, 10)
return runServer({
port,
verbose: args.verbose,
business: args.business,
manual: args.manual,
rateLimit,
rateLimitWait: Boolean(args.wait),
githubToken: args["github-token"],
})
},
})
const main = defineCommand({
meta: {
name: "copilot-api",
description:
"A wrapper around GitHub Copilot API to make it OpenAI compatible, making it usable for other tools.",
},
subCommands: { auth, start },
})
await runMain(main)