forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpricing-sync-cmd.ts
More file actions
168 lines (151 loc) · 4.43 KB
/
pricing-sync-cmd.ts
File metadata and controls
168 lines (151 loc) · 4.43 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { defineCommand } from "citty"
import consola from "consola"
import { serve, type ServerHandler } from "srvx"
import { loadAccounts, parseGithubTokenArgs } from "./lib/accounts-loader"
import { initDb } from "./lib/db"
import { ensurePaths, PATHS } from "./lib/paths"
import { runPricingSync } from "./lib/pricing-sync-runner"
import { initProxyFromEnv } from "./lib/proxy"
import { state } from "./lib/state"
import { setupCopilotTokenFor, setupGitHubToken } from "./lib/token"
import { cacheModels, cacheVSCodeVersion } from "./lib/utils"
import { createServer } from "./server"
interface RunPricingSyncCmdOptions {
port: number
syncModel?: string
githubToken?: string
accountsFile?: string
accountType: string
dbPath: string
proxyEnv: boolean
verbose: boolean
}
function normalizeGithubToken(
raw: string | Array<string> | undefined,
): string | undefined {
if (!raw) return undefined
return Array.isArray(raw) ? raw.join(",") : raw
}
async function bootstrapServer(
options: RunPricingSyncCmdOptions,
): Promise<void> {
if (options.proxyEnv) {
initProxyFromEnv()
}
if (options.verbose) {
consola.level = 5
}
state.accountType = options.accountType
await ensurePaths()
initDb(options.dbPath)
await cacheVSCodeVersion()
let legacyToken: string | undefined
let multiTokenEntries: ReturnType<typeof parseGithubTokenArgs> | undefined
if (options.githubToken && !options.accountsFile) {
multiTokenEntries = parseGithubTokenArgs(options.githubToken)
}
if (!multiTokenEntries?.length && !options.accountsFile) {
legacyToken = await setupGitHubToken()
}
const loaded = await loadAccounts({
accountsFile: options.accountsFile,
legacyTokens: multiTokenEntries,
legacyToken,
defaultAccountType: options.accountType,
})
if (loaded.length === 0) {
throw new Error("No accounts available.")
}
state.pool = undefined as never // not needed for sync
await Promise.all(loaded.map((a) => setupCopilotTokenFor(a)))
await cacheModels()
}
function startTempServer(port: number): void {
serve({
fetch: createServer().fetch as ServerHandler,
port,
})
}
export async function runPricingSyncCmd(
options: RunPricingSyncCmdOptions,
): Promise<void> {
await bootstrapServer(options)
startTempServer(options.port)
consola.info("Running one-off pricing sync…")
const result = await runPricingSync({
port: options.port,
syncModel: options.syncModel,
})
if (result.status === "ok") {
consola.success(`Pricing sync complete: ${result.updated} model(s) updated`)
} else if (result.status === "rejected") {
consola.warn(
`Pricing sync rejected (sanity check): ${result.rejected} model(s)`,
)
} else {
consola.error(`Pricing sync failed: ${result.error ?? "unknown error"}`)
}
process.exit(result.status === "ok" ? 0 : 1)
}
export const pricingSyncCmd = defineCommand({
meta: {
name: "pricing-sync",
description: "Run a one-off pricing sync against Azure and Anthropic",
},
args: {
port: {
alias: "p",
type: "string",
default: "4141",
description: "Port for the temporary server (needed for LLM self-call)",
},
"sync-model": {
type: "string",
description: "Model to use for the LLM extraction step",
},
"github-token": {
alias: "g",
type: "string",
description:
"GitHub token(s). Supports comma-separated multi-token format: name:type:token",
},
"accounts-file": {
type: "string",
description: "Path to accounts JSON file",
},
"account-type": {
alias: "a",
type: "string",
default: "individual",
description: "Account type",
},
"db-path": {
type: "string",
default: PATHS.USAGE_DB_PATH,
description: "Path to the usage SQLite database",
},
"proxy-env": {
type: "boolean",
default: false,
description: "Initialize proxy from environment variables",
},
verbose: {
alias: "v",
type: "boolean",
default: false,
description: "Enable verbose logging",
},
},
run({ args }) {
return runPricingSyncCmd({
port: Number.parseInt(args.port, 10),
syncModel: args["sync-model"],
githubToken: normalizeGithubToken(args["github-token"]),
accountsFile: args["accounts-file"],
accountType: args["account-type"],
dbPath: args["db-path"],
proxyEnv: args["proxy-env"],
verbose: args.verbose,
})
},
})