opencode-codebuddy-quota 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +81 -2
- package/config.default.json +41 -0
- package/dist/gui-experimental.cjs +1 -0
- package/package.json +47 -4
- package/rpc.ts +92 -0
- package/server.ts +251 -0
- package/src/gui/battery.tsx +81 -0
- package/src/gui/dialog-helpers.ts +30 -0
- package/src/gui/dialog.css +83 -0
- package/src/gui/dialog.tsx +262 -0
- package/src/gui/pace.ts +76 -0
- package/src/gui/quota.ts +198 -0
- package/src/gui/store.ts +181 -0
- package/src/gui.tsx +111 -0
- package/src/server/catalog.ts +264 -0
- package/src/server/config-writer.ts +621 -0
- package/src/server/config.ts +215 -0
- package/src/server/embedding-local.ts +166 -0
- package/src/server/embedding-mode.ts +14 -0
- package/src/server/gateway-owner.ts +243 -0
- package/src/server/gateway-setup.ts +51 -0
- package/src/server/headers.ts +72 -0
- package/src/server/logger.ts +26 -0
- package/src/server/login-gate.ts +21 -0
- package/src/server/login.ts +206 -0
- package/src/server/model-cache.ts +136 -0
- package/src/server/model-info.ts +109 -0
- package/src/server/model-prewarm.ts +123 -0
- package/src/server/models-dev.ts +77 -0
- package/src/server/models-endpoint.ts +74 -0
- package/src/server/package-spec.ts +42 -0
- package/src/server/package-version.ts +51 -0
- package/src/server/pricing.ts +169 -0
- package/src/server/proxy.ts +564 -0
- package/src/server/quota-source.ts +129 -0
- package/src/server/rpc-handlers.ts +87 -0
- package/src/server/signal.ts +36 -0
- package/src/server/site-select.ts +24 -0
- package/src/server/site-state.ts +58 -0
- package/src/server/stream-collect.ts +166 -0
- package/src/server/token-store.ts +263 -0
- package/src/server/upstream.ts +150 -0
- package/src/server/wire.ts +67 -0
- package/src/shared.ts +20 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CodeBuddy model catalog, read from `/v3/config`.
|
|
3
|
+
*
|
|
4
|
+
* The endpoint returns both a full model table and a per-agent allowlist. Only
|
|
5
|
+
* the `cli` allowlist matters here: it carries exactly the models the current
|
|
6
|
+
* API key may spend credits on, so a model that exists in the table but is
|
|
7
|
+
* absent from the allowlist must not be offered.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Site } from "../shared"
|
|
11
|
+
import { apiHeaders } from "./headers"
|
|
12
|
+
import type { SiteAuth } from "./login"
|
|
13
|
+
import { anySignal, timeoutSignal } from "./signal"
|
|
14
|
+
|
|
15
|
+
export const CONFIG_URL = "https://copilot.tencent.com/v3/config"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The client identity the endpoint keys on. `CLI/` must appear in the
|
|
19
|
+
* User-Agent or the server answers HTTP 200 with a well-formed body that
|
|
20
|
+
* simply omits `data.models` — an empty catalog that looks like success.
|
|
21
|
+
* `CodeBuddy/<version>` must also parse, or the request is rejected outright.
|
|
22
|
+
*/
|
|
23
|
+
export const CLIENT_NAME = "deepseekharness"
|
|
24
|
+
export const CLI_UA_MARKER = "CLI/unknown"
|
|
25
|
+
export const CODEBUDDY_CLI_VERSION = "2.137.1"
|
|
26
|
+
export const REQUEST_USER_AGENT = `${CLIENT_NAME} ${CLI_UA_MARKER} CodeBuddy/${CODEBUDDY_CLI_VERSION}`
|
|
27
|
+
export const PRODUCT_HEADER = "SaaS"
|
|
28
|
+
|
|
29
|
+
const DEFAULT_TIMEOUT_MS = 15_000
|
|
30
|
+
|
|
31
|
+
export interface CatalogModel {
|
|
32
|
+
id: string
|
|
33
|
+
name: string
|
|
34
|
+
/** Credit multiplier from `"x0.29 credits"` as `0.29`; null when the server did not declare one. */
|
|
35
|
+
credits: number | null
|
|
36
|
+
imageInput: boolean
|
|
37
|
+
/** Reasoning effort ids, ordered from least to most. Empty when the model does not reason. */
|
|
38
|
+
efforts: readonly string[]
|
|
39
|
+
defaultEffort: string | null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface FetchCatalogDeps {
|
|
43
|
+
site: Site
|
|
44
|
+
auth: SiteAuth
|
|
45
|
+
signal?: AbortSignal
|
|
46
|
+
fetchImpl?: typeof fetch
|
|
47
|
+
timeoutMs?: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Effort ids the catalog uses, least to most. Unknown ids sort after all of these. */
|
|
51
|
+
const EFFORT_RANK: Readonly<Record<string, number>> = {
|
|
52
|
+
off: 0,
|
|
53
|
+
minimal: 1,
|
|
54
|
+
low: 2,
|
|
55
|
+
medium: 3,
|
|
56
|
+
high: 4,
|
|
57
|
+
xhigh: 5,
|
|
58
|
+
max: 6,
|
|
59
|
+
}
|
|
60
|
+
const UNKNOWN_EFFORT_RANK = Number.MAX_SAFE_INTEGER
|
|
61
|
+
|
|
62
|
+
/** The server does not guarantee ascending order, and downstream code relies on it. */
|
|
63
|
+
function sortEfforts(levels: readonly string[]): string[] {
|
|
64
|
+
return levels
|
|
65
|
+
.map((level, index) => ({ level, index }))
|
|
66
|
+
.sort(
|
|
67
|
+
(a, b) =>
|
|
68
|
+
(EFFORT_RANK[a.level] ?? UNKNOWN_EFFORT_RANK) - (EFFORT_RANK[b.level] ?? UNKNOWN_EFFORT_RANK) ||
|
|
69
|
+
a.index - b.index,
|
|
70
|
+
)
|
|
71
|
+
.map((entry) => entry.level)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** `"x0.29 credits"` → `0.29`. A missing or unparseable value stays null. */
|
|
75
|
+
export function shortCredits(raw: unknown): number | null {
|
|
76
|
+
if (typeof raw !== "string") return null
|
|
77
|
+
const match = /x(\d+(?:\.\d+)?)/i.exec(raw)
|
|
78
|
+
if (!match) return null
|
|
79
|
+
const value = Number(match[1])
|
|
80
|
+
return Number.isFinite(value) ? value : null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** True when any of the values is a positive integer, i.e. the model declares some capacity. */
|
|
84
|
+
function declaresCapacity(...values: readonly unknown[]): boolean {
|
|
85
|
+
return values.some((value) => typeof value === "number" && Number.isSafeInteger(value) && value > 0)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function text(...values: readonly unknown[]): string | null {
|
|
89
|
+
for (const value of values) {
|
|
90
|
+
if (typeof value === "string" && value.length > 0) return value
|
|
91
|
+
}
|
|
92
|
+
return null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Two shapes the server uses for reasoning:
|
|
97
|
+
* `{ supportedEfforts: [...], canDisableThinking, defaultEffort }` for a model
|
|
98
|
+
* with selectable levels, and `{ effort: "high" }` for one locked to a single
|
|
99
|
+
* level. `canDisableThinking: false` means no `off` level.
|
|
100
|
+
*/
|
|
101
|
+
function readEfforts(reasoning: Record<string, unknown> | null): { efforts: string[]; defaultEffort: string | null } {
|
|
102
|
+
if (!reasoning) return { efforts: [], defaultEffort: null }
|
|
103
|
+
|
|
104
|
+
const supported = Array.isArray(reasoning.supportedEfforts)
|
|
105
|
+
? (reasoning.supportedEfforts as unknown[]).filter((l): l is string => typeof l === "string" && l.length > 0)
|
|
106
|
+
: []
|
|
107
|
+
|
|
108
|
+
if (supported.length > 0) {
|
|
109
|
+
const efforts = sortEfforts(supported)
|
|
110
|
+
// Some servers list `off` among the supported efforts and also flag it via
|
|
111
|
+
// `canDisableThinking`; the guard keeps it from shipping as two variants.
|
|
112
|
+
if (reasoning.canDisableThinking !== false && !efforts.includes("off")) efforts.unshift("off")
|
|
113
|
+
return { efforts, defaultEffort: text(reasoning.defaultEffort) }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const fixed = text(reasoning.effort)
|
|
117
|
+
if (fixed) return { efforts: [fixed], defaultEffort: fixed }
|
|
118
|
+
return { efforts: [], defaultEffort: null }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Pure parser for the `/v3/config` body. Returns `[]` rather than throwing when
|
|
123
|
+
* the body carries no models — deciding that emptiness is a failure is
|
|
124
|
+
* `fetchCatalog`'s job, not this function's.
|
|
125
|
+
*/
|
|
126
|
+
/** ids a tier blocks for the current account (paywall tiers WorkBuddy hides). */
|
|
127
|
+
function paywallIds(data: Record<string, unknown>): Set<string> {
|
|
128
|
+
const out = new Set<string>()
|
|
129
|
+
if (!Array.isArray(data.modelTiers)) return out
|
|
130
|
+
for (const tier of data.modelTiers as Array<Record<string, unknown>>) {
|
|
131
|
+
if (tier?.accessMode !== "paywall" || !Array.isArray(tier.modelIds)) continue
|
|
132
|
+
for (const id of tier.modelIds) if (typeof id === "string") out.add(id)
|
|
133
|
+
}
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** ids carrying an active-looking promotion (WorkBuddy keeps the free/featured twin). */
|
|
138
|
+
function promotedIds(data: Record<string, unknown>): Set<string> {
|
|
139
|
+
const out = new Set<string>()
|
|
140
|
+
if (!Array.isArray(data.modelPromotions)) return out
|
|
141
|
+
for (const promo of data.modelPromotions as Array<Record<string, unknown>>) {
|
|
142
|
+
if (!Array.isArray(promo?.modelIds)) continue
|
|
143
|
+
for (const id of promo.modelIds) if (typeof id === "string") out.add(id)
|
|
144
|
+
}
|
|
145
|
+
return out
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function parseModelConfig(body: unknown): readonly CatalogModel[] {
|
|
149
|
+
const data = (body as { data?: unknown })?.data as Record<string, unknown> | undefined
|
|
150
|
+
if (!data) return []
|
|
151
|
+
|
|
152
|
+
// Pro-only filtering applies where the gateway sends tier metadata (intl).
|
|
153
|
+
// Credit labels and free-vs-paid de-dup apply to every site: CN also ships a
|
|
154
|
+
// promoted free twin ("Hy4 flash") alongside its paid id.
|
|
155
|
+
const hasTiers = Array.isArray(data.modelTiers)
|
|
156
|
+
const hasPromotions = Array.isArray(data.modelPromotions)
|
|
157
|
+
const blocked = hasTiers ? paywallIds(data) : new Set<string>()
|
|
158
|
+
const promoted = promotedIds(data)
|
|
159
|
+
|
|
160
|
+
const agents = Array.isArray(data.agents)
|
|
161
|
+
? (data.agents as Array<Record<string, unknown>>)
|
|
162
|
+
: Array.isArray((data.agent as { agents?: unknown })?.agents)
|
|
163
|
+
? ((data.agent as { agents: Array<Record<string, unknown>> }).agents)
|
|
164
|
+
: []
|
|
165
|
+
|
|
166
|
+
const cli = agents.find((agent) => agent?.name === "cli")
|
|
167
|
+
const allowed = Array.isArray(cli?.models) ? (cli.models as unknown[]) : []
|
|
168
|
+
if (allowed.length === 0) return []
|
|
169
|
+
|
|
170
|
+
const byId = new Map<string, Record<string, unknown>>()
|
|
171
|
+
if (Array.isArray(data.models)) {
|
|
172
|
+
for (const model of data.models as Array<Record<string, unknown>>) {
|
|
173
|
+
const id = text(model?.id)
|
|
174
|
+
if (id) byId.set(id, model)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const parsed: CatalogModel[] = []
|
|
179
|
+
for (const raw of allowed) {
|
|
180
|
+
if (typeof raw !== "string" || blocked.has(raw)) continue
|
|
181
|
+
const model = byId.get(raw)
|
|
182
|
+
if (!model) continue
|
|
183
|
+
|
|
184
|
+
// A model that declares no capacity at all is not a usable model; the
|
|
185
|
+
// figures themselves are not imported (see model-info's fixed limit).
|
|
186
|
+
if (!declaresCapacity(model.maxInputTokens, model.maxAllowedSize, model.maxOutputTokens)) continue
|
|
187
|
+
|
|
188
|
+
const reasoning =
|
|
189
|
+
model.supportsReasoning === true && model.reasoning !== null && typeof model.reasoning === "object"
|
|
190
|
+
? (model.reasoning as Record<string, unknown>)
|
|
191
|
+
: null
|
|
192
|
+
const { efforts, defaultEffort } = readEfforts(reasoning)
|
|
193
|
+
|
|
194
|
+
parsed.push({
|
|
195
|
+
id: raw,
|
|
196
|
+
name: text(model.name) ?? raw, // base name; credit suffix added after de-dup
|
|
197
|
+
credits: shortCredits(model.credits),
|
|
198
|
+
imageInput: model.supportsImages === true,
|
|
199
|
+
efforts,
|
|
200
|
+
defaultEffort,
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
let result = parsed
|
|
205
|
+
if (hasPromotions) {
|
|
206
|
+
// Same display name (e.g. two "Hy4 flash" ids): WorkBuddy keeps the one with
|
|
207
|
+
// an active promotion (the free trial) and drops the paid twin. Compare base
|
|
208
|
+
// names; the credit suffix is appended after de-dup.
|
|
209
|
+
const byName = new Map<string, CatalogModel>()
|
|
210
|
+
for (const model of parsed) {
|
|
211
|
+
const existing = byName.get(model.name)
|
|
212
|
+
if (!existing) { byName.set(model.name, model); continue }
|
|
213
|
+
const takeNew = promoted.has(model.id) && !promoted.has(existing.id)
|
|
214
|
+
if (takeNew) byName.set(model.name, model)
|
|
215
|
+
}
|
|
216
|
+
result = [...byName.values()]
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// WorkBuddy shows the credit multiplier in the picker; match its labels on
|
|
220
|
+
// every site (CN and intl) so the provider mirrors what the client displays.
|
|
221
|
+
return result.map((m) =>
|
|
222
|
+
m.credits !== null ? { ...m, name: `${m.name}(x${m.credits.toFixed(2)})` } : m,
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Fetch and parse the catalog for one API key. Throws on anything that would
|
|
228
|
+
* make the returned list untrustworthy — including the silently-empty response,
|
|
229
|
+
* whose error message names the `CLI/` marker so the cause is not a mystery.
|
|
230
|
+
*/
|
|
231
|
+
export async function fetchCatalog(deps: FetchCatalogDeps): Promise<readonly CatalogModel[]> {
|
|
232
|
+
const fetchImpl = deps.fetchImpl ?? fetch
|
|
233
|
+
const timeout = timeoutSignal(deps.timeoutMs ?? DEFAULT_TIMEOUT_MS)
|
|
234
|
+
const signal = deps.signal ? anySignal([deps.signal, timeout]) : timeout
|
|
235
|
+
const url = `${deps.auth.endpoint}/v3/config`
|
|
236
|
+
|
|
237
|
+
const headers = apiHeaders(deps.auth, { accept: "application/json" })
|
|
238
|
+
// The international gateway serves WorkBuddy's catalogue (with tier/promo
|
|
239
|
+
// metadata) only when the caller identifies as the WorkBuddy IDE. Without
|
|
240
|
+
// these headers it returns the narrower CLI catalogue with no Pro-only info.
|
|
241
|
+
if (deps.site === "intl") {
|
|
242
|
+
headers["x-ide-type"] = "WorkBuddy"
|
|
243
|
+
headers["x-product"] = "WorkBuddy"
|
|
244
|
+
headers["user-agent"] = "WorkBuddy/5.11.4"
|
|
245
|
+
}
|
|
246
|
+
const response = await fetchImpl(url, {
|
|
247
|
+
method: "GET",
|
|
248
|
+
headers,
|
|
249
|
+
signal,
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
if (!response.ok) throw new Error(`catalog HTTP ${response.status}`)
|
|
253
|
+
|
|
254
|
+
const body = (await response.json()) as { code?: number; msg?: string }
|
|
255
|
+
if (body?.code !== 0) throw new Error(`catalog code ${body?.code}: ${body?.msg ?? "unknown"}`)
|
|
256
|
+
|
|
257
|
+
const models = parseModelConfig(body)
|
|
258
|
+
if (models.length === 0) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
"catalog returned no CLI models — the request must carry a `CLI/` marker in its User-Agent",
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
return models
|
|
264
|
+
}
|