pi-commandcode-provider 0.5.0 → 0.6.0
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/CHANGELOG.md +39 -0
- package/CONTRIBUTING.md +10 -0
- package/README.md +38 -12
- package/index.ts +73 -41
- package/package.json +13 -4
- package/scripts/live-e2e-profile.mjs +87 -0
- package/scripts/pi-authenticated.mjs +1 -0
- package/scripts/pi-isolated.mjs +1 -0
- package/src/api-key.ts +69 -0
- package/src/auth-server.ts +7 -0
- package/src/commandcode-catalog.ts +141 -0
- package/src/converters.ts +114 -23
- package/src/core.ts +121 -31
- package/src/models.ts +60 -50
- package/src/oauth.ts +84 -22
- package/src/pricing.ts +53 -45
- package/src/quota-command.ts +66 -0
- package/src/quota-format.ts +111 -0
- package/src/quota-types.ts +47 -0
- package/src/quota.ts +335 -0
- package/src/runtime.ts +10 -5
- package/src/transport.ts +140 -0
- package/src/types.ts +9 -0
package/src/models.ts
CHANGED
|
@@ -1,51 +1,39 @@
|
|
|
1
1
|
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"
|
|
2
2
|
import { dirname } from "node:path"
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
import {
|
|
5
|
+
MODEL_EFFORTS,
|
|
6
|
+
MODEL_INPUT_MODALITIES,
|
|
7
|
+
MODEL_MAX_OUTPUT_TOKENS,
|
|
8
|
+
MODEL_REASONING,
|
|
9
|
+
type CommandCodeInputType,
|
|
10
|
+
type CommandCodeReasoningEffort,
|
|
11
|
+
} from "./commandcode-catalog.ts"
|
|
12
|
+
|
|
13
|
+
export { MODEL_EFFORTS, MODEL_INPUT_MODALITIES, MODEL_MAX_OUTPUT_TOKENS, MODEL_REASONING }
|
|
14
|
+
export type { CommandCodeInputType }
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_PROVIDER_API_BASE = "https://api.commandcode.ai/provider/v1"
|
|
17
|
+
export const DEFAULT_MODELS_URL = `${DEFAULT_PROVIDER_API_BASE}/models`
|
|
5
18
|
export const DEFAULT_MODELS_TIMEOUT_MS = 10_000
|
|
6
19
|
|
|
7
20
|
const DEFAULT_MAX_OUTPUT_TOKENS = 65_536
|
|
8
21
|
const MODEL_CACHE_VERSION = 1
|
|
9
22
|
|
|
10
|
-
export type
|
|
23
|
+
export type CommandCodeApi = "openai-completions" | "anthropic-messages"
|
|
24
|
+
|
|
25
|
+
const TEXT_INPUT_ONLY = ["text"] as const
|
|
26
|
+
|
|
27
|
+
export function inputModalitiesForModel(modelId: string): readonly CommandCodeInputType[] {
|
|
28
|
+
return MODEL_INPUT_MODALITIES[modelId] ?? TEXT_INPUT_ONLY
|
|
29
|
+
}
|
|
11
30
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Per-model reasoning efforts supported by Command Code's generate endpoint.
|
|
16
|
-
*
|
|
17
|
-
* The Provider API does not expose reasoning metadata. This is an exact
|
|
18
|
-
* snapshot of `reasoningEfforts` from the command-code@1.14.1 model catalog
|
|
19
|
-
* (`packages/shared/src/model-catalog.ts`, also published in the generated
|
|
20
|
-
* `dist/bundled/command-code-knowledge/reference/models.md`). Models omitted
|
|
21
|
-
* here let Command Code choose their reasoning depth, matching the CLI.
|
|
22
|
-
*/
|
|
23
|
-
export const MODEL_EFFORTS: Readonly<Record<string, readonly CommandCodeReasoningEffort[]>> = {
|
|
24
|
-
"Qwen/Qwen3.8-Max": ["low", "medium", "xhigh"],
|
|
25
|
-
"claude-fable-5": ["low", "medium", "high", "xhigh", "max"],
|
|
26
|
-
"claude-opus-4-7": ["low", "medium", "high", "xhigh", "max"],
|
|
27
|
-
"claude-opus-4-8": ["low", "medium", "high", "xhigh", "max"],
|
|
28
|
-
"claude-opus-5": ["low", "medium", "high", "xhigh", "max"],
|
|
29
|
-
"claude-sonnet-4-6": ["low", "medium", "high", "xhigh", "max"],
|
|
30
|
-
"claude-sonnet-5": ["low", "medium", "high", "xhigh", "max"],
|
|
31
|
-
"deepseek/deepseek-v4-flash": ["high", "max"],
|
|
32
|
-
"deepseek/deepseek-v4-pro": ["high", "max"],
|
|
33
|
-
"gpt-5.3-codex": ["low", "medium", "high", "xhigh"],
|
|
34
|
-
"gpt-5.4": ["low", "medium", "high", "xhigh"],
|
|
35
|
-
"gpt-5.4-mini": ["low", "medium", "high"],
|
|
36
|
-
"gpt-5.5": ["low", "medium", "high", "xhigh"],
|
|
37
|
-
"gpt-5.6-luna": ["low", "medium", "high", "xhigh", "max"],
|
|
38
|
-
"gpt-5.6-sol": ["low", "medium", "high", "xhigh", "max"],
|
|
39
|
-
"gpt-5.6-terra": ["low", "medium", "high", "xhigh", "max"],
|
|
40
|
-
"google/gemini-3.1-flash-lite": ["low", "medium", "high"],
|
|
41
|
-
"google/gemini-3.5-flash": ["low", "medium", "high"],
|
|
42
|
-
"google/gemini-3.5-flash-lite": ["low", "medium", "high"],
|
|
43
|
-
"google/gemini-3.6-flash": ["low", "medium", "high"],
|
|
44
|
-
"sakana/fugu-ultra": ["high", "xhigh"],
|
|
45
|
-
"xai/grok-4.5": ["low", "medium", "high"],
|
|
46
|
-
"zai-org/GLM-5.2": ["high", "max"],
|
|
31
|
+
export function modelSupportsImageInput(modelId: string): boolean {
|
|
32
|
+
return inputModalitiesForModel(modelId).includes("image")
|
|
47
33
|
}
|
|
48
34
|
|
|
35
|
+
export type PiThinkingLevel = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max"
|
|
36
|
+
|
|
49
37
|
const PI_THINKING_LEVELS: readonly PiThinkingLevel[] = [
|
|
50
38
|
"off",
|
|
51
39
|
"minimal",
|
|
@@ -69,7 +57,7 @@ export function thinkingLevelMapForEfforts(
|
|
|
69
57
|
|
|
70
58
|
export interface ThinkingMetadata {
|
|
71
59
|
thinkingLevelMap: Partial<Record<PiThinkingLevel, string | null>>
|
|
72
|
-
thinking
|
|
60
|
+
thinking?: {
|
|
73
61
|
mode: "effort"
|
|
74
62
|
effortMap: Partial<Record<CommandCodeReasoningEffort, string>>
|
|
75
63
|
efforts: readonly CommandCodeReasoningEffort[]
|
|
@@ -78,19 +66,26 @@ export interface ThinkingMetadata {
|
|
|
78
66
|
|
|
79
67
|
export function thinkingMetadataForModel(modelId: string): ThinkingMetadata | undefined {
|
|
80
68
|
const efforts = MODEL_EFFORTS[modelId]
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
69
|
+
if (efforts) {
|
|
70
|
+
return {
|
|
71
|
+
thinkingLevelMap: thinkingLevelMapForEfforts(efforts),
|
|
72
|
+
thinking: {
|
|
73
|
+
mode: "effort",
|
|
74
|
+
effortMap: Object.fromEntries(efforts.map((effort) => [effort, effort])),
|
|
75
|
+
efforts,
|
|
76
|
+
},
|
|
77
|
+
}
|
|
89
78
|
}
|
|
79
|
+
if (!isReasoningModel(modelId)) return undefined
|
|
80
|
+
return { thinkingLevelMap: thinkingLevelMapForEfforts([]) }
|
|
90
81
|
}
|
|
91
82
|
|
|
92
83
|
function isReasoningModel(modelId: string): boolean {
|
|
93
|
-
return
|
|
84
|
+
return MODEL_REASONING[modelId] === true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function maxOutputTokensForModel(modelId: string, contextLength: number): number {
|
|
88
|
+
return Math.min(contextLength, MODEL_MAX_OUTPUT_TOKENS[modelId] ?? DEFAULT_MAX_OUTPUT_TOKENS)
|
|
94
89
|
}
|
|
95
90
|
|
|
96
91
|
interface ApiModel {
|
|
@@ -102,11 +97,22 @@ interface ApiModel {
|
|
|
102
97
|
export interface CommandCodeModel {
|
|
103
98
|
id: string
|
|
104
99
|
name: string
|
|
100
|
+
api: CommandCodeApi
|
|
105
101
|
reasoning: boolean
|
|
106
102
|
contextWindow: number
|
|
107
103
|
maxTokens: number
|
|
108
104
|
}
|
|
109
105
|
|
|
106
|
+
export function apiForModelId(id: string): CommandCodeApi {
|
|
107
|
+
return id.startsWith("claude-") ? "anthropic-messages" : "openai-completions"
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function baseUrlForModel(apiBase: string, api: CommandCodeApi): string {
|
|
111
|
+
const normalized = apiBase.replace(/\/+$/g, "")
|
|
112
|
+
if (api !== "anthropic-messages") return normalized
|
|
113
|
+
return normalized.endsWith("/v1") ? normalized.slice(0, -3) : normalized
|
|
114
|
+
}
|
|
115
|
+
|
|
110
116
|
interface FetchCommandCodeModelsOptions {
|
|
111
117
|
url?: string
|
|
112
118
|
fetchImpl?: typeof fetch
|
|
@@ -165,12 +171,15 @@ function parseCachedModel(value: unknown): CommandCodeModel {
|
|
|
165
171
|
|
|
166
172
|
const id = stringField(value, "id")
|
|
167
173
|
booleanField(value, "reasoning")
|
|
174
|
+
positiveNumberField(value, "maxTokens")
|
|
175
|
+
const contextWindow = positiveNumberField(value, "contextWindow")
|
|
168
176
|
return {
|
|
169
177
|
id,
|
|
170
178
|
name: stringField(value, "name"),
|
|
179
|
+
api: apiForModelId(id),
|
|
171
180
|
reasoning: isReasoningModel(id),
|
|
172
|
-
contextWindow
|
|
173
|
-
maxTokens:
|
|
181
|
+
contextWindow,
|
|
182
|
+
maxTokens: maxOutputTokensForModel(id, contextWindow),
|
|
174
183
|
}
|
|
175
184
|
}
|
|
176
185
|
|
|
@@ -272,9 +281,10 @@ export function commandCodeModelsFromApiResponse(value: unknown): readonly Comma
|
|
|
272
281
|
return data.map(parseApiModel).map((model) => ({
|
|
273
282
|
id: model.id,
|
|
274
283
|
name: `${model.name} (CC)`,
|
|
284
|
+
api: apiForModelId(model.id),
|
|
275
285
|
reasoning: isReasoningModel(model.id),
|
|
276
286
|
contextWindow: model.contextLength,
|
|
277
|
-
maxTokens:
|
|
287
|
+
maxTokens: maxOutputTokensForModel(model.id, model.contextLength),
|
|
278
288
|
}))
|
|
279
289
|
}
|
|
280
290
|
|
package/src/oauth.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Command Code OAuth provider for pi's /login flow.
|
|
3
3
|
*
|
|
4
|
-
* Implements
|
|
5
|
-
* 1.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
4
|
+
* Implements two API key retrieval flows:
|
|
5
|
+
* 1. Browser-assisted login opens Command Code Studio and waits for the
|
|
6
|
+
* website to POST the API key back to a local callback server.
|
|
7
|
+
* 2. Direct API key login prompts the user to paste a Studio API key.
|
|
8
|
+
*
|
|
9
|
+
* If browser transfer fails, the user can still paste the API key manually.
|
|
10
|
+
* The API key is stored in pi's auth.json as OAuth credentials.
|
|
11
11
|
*
|
|
12
12
|
* Since Command Code API keys don't expire, we store them as
|
|
13
13
|
* OAuth credentials with a far-future expiry.
|
|
@@ -18,7 +18,8 @@ import { startAuthServer } from "./auth-server.ts"
|
|
|
18
18
|
|
|
19
19
|
const STUDIO_BASE_URL = "https://commandcode.ai"
|
|
20
20
|
const TEN_YEARS_MS = 10 * 365 * 24 * 60 * 60 * 1000 // API keys don't expire
|
|
21
|
-
const DEFAULT_AUTH_TIMEOUT_MS =
|
|
21
|
+
const DEFAULT_AUTH_TIMEOUT_MS = 120_000
|
|
22
|
+
const DEFAULT_API_BASE = "https://api.commandcode.ai"
|
|
22
23
|
|
|
23
24
|
export interface OAuthLoginCallbacks {
|
|
24
25
|
onAuth(params: { url: string }): void
|
|
@@ -95,22 +96,70 @@ export function sanitizeApiKey(input: string): string {
|
|
|
95
96
|
.trim()
|
|
96
97
|
}
|
|
97
98
|
|
|
99
|
+
export async function validateApiKey(
|
|
100
|
+
apiKey: string,
|
|
101
|
+
options: { fetchImpl?: typeof fetch; apiBase?: string } = {},
|
|
102
|
+
): Promise<void> {
|
|
103
|
+
let response: Response
|
|
104
|
+
try {
|
|
105
|
+
response = await (options.fetchImpl ?? fetch)(
|
|
106
|
+
`${options.apiBase ?? DEFAULT_API_BASE}/alpha/whoami`,
|
|
107
|
+
{
|
|
108
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
109
|
+
},
|
|
110
|
+
)
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`Could not validate the Command Code API key: ${error instanceof Error ? error.message : String(error)}`,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (response.status === 401) throw new Error("Invalid Command Code API key")
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
throw new Error(`Could not validate the Command Code API key (${response.status})`)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
98
123
|
async function promptForApiKey(callbacks: OAuthLoginCallbacks, message: string) {
|
|
99
124
|
const apiKey = sanitizeApiKey(await callbacks.onPrompt({ message }))
|
|
100
125
|
if (!apiKey) throw new Error("No Command Code API key provided")
|
|
126
|
+
await validateApiKey(apiKey)
|
|
101
127
|
return credentialsFromApiKey(apiKey)
|
|
102
128
|
}
|
|
103
129
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
130
|
+
type LoginChoice = { type: "browser" } | { type: "prompt" } | { type: "apiKey"; apiKey: string }
|
|
131
|
+
|
|
132
|
+
async function chooseLoginFlow(callbacks: OAuthLoginCallbacks): Promise<LoginChoice> {
|
|
133
|
+
const input = sanitizeApiKey(
|
|
134
|
+
await callbacks.onPrompt({
|
|
135
|
+
message:
|
|
136
|
+
"Command Code login: press Enter for browser login, type 'key' to paste an API key, or paste the API key directly:",
|
|
137
|
+
}),
|
|
138
|
+
)
|
|
139
|
+
const normalized = input.toLowerCase()
|
|
140
|
+
|
|
141
|
+
if (!input || normalized === "1" || normalized === "b" || normalized === "browser") {
|
|
142
|
+
return { type: "browser" }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (
|
|
146
|
+
normalized === "2" ||
|
|
147
|
+
normalized === "k" ||
|
|
148
|
+
normalized === "key" ||
|
|
149
|
+
normalized === "api" ||
|
|
150
|
+
normalized === "paste"
|
|
151
|
+
) {
|
|
152
|
+
return { type: "prompt" }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return { type: "apiKey", apiKey: input }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function browserLogin(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
|
159
|
+
const stateToken = generateStateToken()
|
|
111
160
|
let authServer
|
|
112
161
|
try {
|
|
113
|
-
authServer = await startAuthServer()
|
|
162
|
+
authServer = await startAuthServer({ expectedState: stateToken })
|
|
114
163
|
} catch {
|
|
115
164
|
return promptForApiKey(
|
|
116
165
|
callbacks,
|
|
@@ -118,7 +167,6 @@ export async function login(callbacks: OAuthLoginCallbacks): Promise<OAuthCreden
|
|
|
118
167
|
)
|
|
119
168
|
}
|
|
120
169
|
|
|
121
|
-
const stateToken = generateStateToken()
|
|
122
170
|
const callbackUrl = `http://localhost:${authServer.port}/callback`
|
|
123
171
|
const authUrl = `${STUDIO_BASE_URL}/studio/auth/cli?callback=${encodeURIComponent(callbackUrl)}&state=${encodeURIComponent(stateToken)}`
|
|
124
172
|
|
|
@@ -142,13 +190,27 @@ export async function login(callbacks: OAuthLoginCallbacks): Promise<OAuthCreden
|
|
|
142
190
|
throw error
|
|
143
191
|
}
|
|
144
192
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
193
|
+
return credentialsFromApiKey(callback.apiKey)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Starts the login flow for Command Code.
|
|
198
|
+
*
|
|
199
|
+
* Returns OAuth credentials where access == refresh == the user's API key.
|
|
200
|
+
* The keys don't expire, so we set a far-future expiry.
|
|
201
|
+
*/
|
|
202
|
+
export async function login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
|
203
|
+
const choice = await chooseLoginFlow(callbacks)
|
|
204
|
+
|
|
205
|
+
if (choice.type === "apiKey") {
|
|
206
|
+
await validateApiKey(choice.apiKey)
|
|
207
|
+
return credentialsFromApiKey(choice.apiKey)
|
|
208
|
+
}
|
|
209
|
+
if (choice.type === "prompt") {
|
|
210
|
+
return promptForApiKey(callbacks, "Paste your Command Code API key:")
|
|
149
211
|
}
|
|
150
212
|
|
|
151
|
-
return
|
|
213
|
+
return browserLogin(callbacks)
|
|
152
214
|
}
|
|
153
215
|
|
|
154
216
|
/**
|
package/src/pricing.ts
CHANGED
|
@@ -20,7 +20,7 @@ export interface TemporaryPricing {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export const PRICING_SOURCE_URL = "https://commandcode.ai/docs/resources/pricing-limits"
|
|
23
|
-
export const PRICING_LAST_VERIFIED = "2026-08-
|
|
23
|
+
export const PRICING_LAST_VERIFIED = "2026-08-25"
|
|
24
24
|
|
|
25
25
|
export const ZERO_MODEL_COST: CommandCodeModelCost = {
|
|
26
26
|
input: 0,
|
|
@@ -40,7 +40,7 @@ export const ZERO_MODEL_COST: CommandCodeModelCost = {
|
|
|
40
40
|
export const MODEL_COSTS: Readonly<Record<string, CommandCodeModelCost>> = {
|
|
41
41
|
// Free models
|
|
42
42
|
"poolside/laguna-s-2.1-free": { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
43
|
-
"
|
|
43
|
+
"stealth/ox-alpha": { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
44
44
|
|
|
45
45
|
// Open and open-weight models
|
|
46
46
|
"tencent/hy3-paid": { input: 0.14, output: 0.58, cacheRead: 0.035, cacheWrite: 0 },
|
|
@@ -54,6 +54,7 @@ export const MODEL_COSTS: Readonly<Record<string, CommandCodeModelCost>> = {
|
|
|
54
54
|
},
|
|
55
55
|
"moonshotai/Kimi-K2.6": { input: 0.95, output: 4, cacheRead: 0.16, cacheWrite: 0 },
|
|
56
56
|
"moonshotai/Kimi-K2.5": { input: 0.6, output: 3, cacheRead: 0.1, cacheWrite: 0 },
|
|
57
|
+
"zai-org/GLM-5.3": { input: 1.4, output: 4.4, cacheRead: 0.26, cacheWrite: 0 },
|
|
57
58
|
"zai-org/GLM-5.2": { input: 1.4, output: 4.4, cacheRead: 0.26, cacheWrite: 0 },
|
|
58
59
|
"zai-org/GLM-5.2-Fast": { input: 3, output: 10.25, cacheRead: 0.5, cacheWrite: 0 },
|
|
59
60
|
"zai-org/GLM-5.1": { input: 1.4, output: 4.4, cacheRead: 0.26, cacheWrite: 0 },
|
|
@@ -61,20 +62,28 @@ export const MODEL_COSTS: Readonly<Record<string, CommandCodeModelCost>> = {
|
|
|
61
62
|
"MiniMaxAI/MiniMax-M3": { input: 0.3, output: 1.2, cacheRead: 0.06, cacheWrite: 0 },
|
|
62
63
|
"MiniMaxAI/MiniMax-M2.7": { input: 0.3, output: 1.2, cacheRead: 0.06, cacheWrite: 0 },
|
|
63
64
|
"MiniMaxAI/MiniMax-M2.5": { input: 0.3, output: 1.2, cacheRead: 0.03, cacheWrite: 0 },
|
|
64
|
-
//
|
|
65
|
+
// DeepSeek V4 uses time-dependent rates. Display the documented off-peak
|
|
66
|
+
// rates, which apply for 17 hours per day; the Usage page remains authoritative.
|
|
65
67
|
"deepseek/deepseek-v4-pro": {
|
|
66
|
-
input: 0.
|
|
67
|
-
output:
|
|
68
|
-
cacheRead: 0.
|
|
68
|
+
input: 0.66,
|
|
69
|
+
output: 1.98,
|
|
70
|
+
cacheRead: 0.022,
|
|
69
71
|
cacheWrite: 0,
|
|
70
72
|
},
|
|
71
73
|
"deepseek/deepseek-v4-flash": {
|
|
72
|
-
input: 0.
|
|
73
|
-
output: 0.
|
|
74
|
-
cacheRead: 0.
|
|
74
|
+
input: 0.22,
|
|
75
|
+
output: 0.66,
|
|
76
|
+
cacheRead: 0.007,
|
|
77
|
+
cacheWrite: 0,
|
|
78
|
+
},
|
|
79
|
+
"deepseek/deepseek-v4-flash-vision-exp": {
|
|
80
|
+
input: 0.22,
|
|
81
|
+
output: 0.66,
|
|
82
|
+
cacheRead: 0.007,
|
|
75
83
|
cacheWrite: 0,
|
|
76
84
|
},
|
|
77
85
|
"Qwen/Qwen3.8-Max": { input: 2, output: 6, cacheRead: 0.25, cacheWrite: 2.5 },
|
|
86
|
+
"Qwen/Qwen3.8-27B": { input: 0.4, output: 3, cacheRead: 0.04, cacheWrite: 0 },
|
|
78
87
|
"Qwen/Qwen3.7-Max": { input: 2.5, output: 7.5, cacheRead: 0.5, cacheWrite: 3.13 },
|
|
79
88
|
"Qwen/Qwen3.7-Plus": {
|
|
80
89
|
input: 0.4,
|
|
@@ -140,6 +149,13 @@ export const MODEL_COSTS: Readonly<Record<string, CommandCodeModelCost>> = {
|
|
|
140
149
|
cacheWrite: 0,
|
|
141
150
|
},
|
|
142
151
|
"meta/muse-spark-1.1": { input: 1.25, output: 4.25, cacheRead: 0.15, cacheWrite: 0 },
|
|
152
|
+
"meta/muse-spark-1.2": { input: 1.25, output: 4.25, cacheRead: 0.15, cacheWrite: 0 },
|
|
153
|
+
"meta/muse-spark-1.2-contributor": {
|
|
154
|
+
input: 0.1,
|
|
155
|
+
output: 0.2,
|
|
156
|
+
cacheRead: 0.002,
|
|
157
|
+
cacheWrite: 0,
|
|
158
|
+
},
|
|
143
159
|
|
|
144
160
|
// Anthropic
|
|
145
161
|
// Introductory pricing through 2026-08-31.
|
|
@@ -158,43 +174,20 @@ export const MODEL_COSTS: Readonly<Record<string, CommandCodeModelCost>> = {
|
|
|
158
174
|
|
|
159
175
|
// OpenAI
|
|
160
176
|
"gpt-5.6-sol": { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
161
|
-
|
|
162
|
-
"gpt-5.6-
|
|
163
|
-
input: 1,
|
|
164
|
-
output: 6,
|
|
165
|
-
cacheRead: 0.1,
|
|
166
|
-
cacheWrite: 1.25,
|
|
167
|
-
tiers: [
|
|
168
|
-
{
|
|
169
|
-
inputTokensAbove: 272_000,
|
|
170
|
-
input: 2,
|
|
171
|
-
output: 9,
|
|
172
|
-
cacheRead: 0.2,
|
|
173
|
-
cacheWrite: 2.5,
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
},
|
|
177
|
-
"gpt-5.6-luna": {
|
|
178
|
-
input: 0.1,
|
|
179
|
-
output: 0.6,
|
|
180
|
-
cacheRead: 0.01,
|
|
181
|
-
cacheWrite: 0.125,
|
|
182
|
-
tiers: [
|
|
183
|
-
{
|
|
184
|
-
inputTokensAbove: 272_000,
|
|
185
|
-
input: 0.2,
|
|
186
|
-
output: 0.9,
|
|
187
|
-
cacheRead: 0.02,
|
|
188
|
-
cacheWrite: 0.25,
|
|
189
|
-
},
|
|
190
|
-
],
|
|
191
|
-
},
|
|
177
|
+
"gpt-5.6-terra": { input: 2, output: 12, cacheRead: 0.2, cacheWrite: 2.5 },
|
|
178
|
+
"gpt-5.6-luna": { input: 0.2, output: 1.2, cacheRead: 0.02, cacheWrite: 0.25 },
|
|
192
179
|
"gpt-5.5": { input: 5, output: 30, cacheRead: 0.5, cacheWrite: 0 },
|
|
193
180
|
"gpt-5.4": { input: 2.5, output: 15, cacheRead: 0.25, cacheWrite: 0 },
|
|
194
181
|
"gpt-5.3-codex": { input: 2, output: 8, cacheRead: 0.5, cacheWrite: 0 },
|
|
195
182
|
"gpt-5.4-mini": { input: 0.75, output: 4.5, cacheRead: 0.075, cacheWrite: 0 },
|
|
196
183
|
|
|
197
184
|
// Google and xAI
|
|
185
|
+
"google/gemini-3.7-flash": {
|
|
186
|
+
input: 0.75,
|
|
187
|
+
output: 3.75,
|
|
188
|
+
cacheRead: 0.075,
|
|
189
|
+
cacheWrite: 0.04167,
|
|
190
|
+
},
|
|
198
191
|
"google/gemini-3.6-flash": { input: 1.5, output: 7.5, cacheRead: 0.15, cacheWrite: 0 },
|
|
199
192
|
"google/gemini-3.5-flash": { input: 1.5, output: 9, cacheRead: 0.15, cacheWrite: 0 },
|
|
200
193
|
"google/gemini-3.5-flash-lite": {
|
|
@@ -210,17 +203,32 @@ export const MODEL_COSTS: Readonly<Record<string, CommandCodeModelCost>> = {
|
|
|
210
203
|
cacheWrite: 0,
|
|
211
204
|
},
|
|
212
205
|
"xai/grok-4.5": { input: 2, output: 6, cacheRead: 0.5, cacheWrite: 0 },
|
|
206
|
+
"xai/grok-4.6": {
|
|
207
|
+
input: 2,
|
|
208
|
+
output: 6,
|
|
209
|
+
cacheRead: 0.5,
|
|
210
|
+
cacheWrite: 0,
|
|
211
|
+
tiers: [
|
|
212
|
+
{
|
|
213
|
+
inputTokensAbove: 200_000,
|
|
214
|
+
input: 4,
|
|
215
|
+
output: 12,
|
|
216
|
+
cacheRead: 1,
|
|
217
|
+
cacheWrite: 0,
|
|
218
|
+
},
|
|
219
|
+
],
|
|
220
|
+
},
|
|
213
221
|
}
|
|
214
222
|
|
|
215
223
|
export const TEMPORARY_PRICING: readonly TemporaryPricing[] = [
|
|
216
|
-
{
|
|
217
|
-
models: ["gpt-5.6-terra", "gpt-5.6-luna"],
|
|
218
|
-
expiresOn: "2026-08-14",
|
|
219
|
-
description: "50% promotional rates",
|
|
220
|
-
},
|
|
221
224
|
{
|
|
222
225
|
models: ["claude-sonnet-5"],
|
|
223
226
|
expiresOn: "2026-08-31",
|
|
224
227
|
description: "introductory pricing",
|
|
225
228
|
},
|
|
229
|
+
{
|
|
230
|
+
models: ["google/gemini-3.7-flash"],
|
|
231
|
+
expiresOn: "2026-12-31",
|
|
232
|
+
description: "50% promotional pricing",
|
|
233
|
+
},
|
|
226
234
|
]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { getConfiguredApiKey } from "./api-key.ts"
|
|
2
|
+
import { pickCommandCodeApiKey } from "./converters.ts"
|
|
3
|
+
import { fetchCommandCodeQuota, redactValue } from "./quota.ts"
|
|
4
|
+
import { formatQuota } from "./quota-format.ts"
|
|
5
|
+
|
|
6
|
+
export interface QuotaCommandContext {
|
|
7
|
+
waitForIdle?: () => Promise<void>
|
|
8
|
+
modelRegistry?: {
|
|
9
|
+
getApiKeyForProvider?: (provider: string) => Promise<string | undefined>
|
|
10
|
+
}
|
|
11
|
+
ui: {
|
|
12
|
+
notify(message: string, type?: "info" | "warning" | "error"): void
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface QuotaCommandApi {
|
|
17
|
+
registerCommand(
|
|
18
|
+
name: string,
|
|
19
|
+
options: {
|
|
20
|
+
description: string
|
|
21
|
+
handler: (args: string, ctx: QuotaCommandContext) => Promise<void>
|
|
22
|
+
},
|
|
23
|
+
): void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface RegisterQuotaCommandOptions {
|
|
27
|
+
apiBase: string
|
|
28
|
+
headers?: Record<string, string>
|
|
29
|
+
getConfiguredKey?: () => string | undefined
|
|
30
|
+
fetchQuota?: typeof fetchCommandCodeQuota
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function registerCommandCodeQuota(
|
|
34
|
+
pi: QuotaCommandApi,
|
|
35
|
+
options: RegisterQuotaCommandOptions,
|
|
36
|
+
): void {
|
|
37
|
+
const getConfiguredKey = options.getConfiguredKey ?? getConfiguredApiKey
|
|
38
|
+
const fetchQuota = options.fetchQuota ?? fetchCommandCodeQuota
|
|
39
|
+
|
|
40
|
+
pi.registerCommand("commandcode-quota", {
|
|
41
|
+
description: "Show Command Code account usage and quota",
|
|
42
|
+
handler: async (_args, ctx) => {
|
|
43
|
+
await ctx.waitForIdle?.()
|
|
44
|
+
const registryKey = await ctx.modelRegistry?.getApiKeyForProvider?.("commandcode")
|
|
45
|
+
const apiKey = pickCommandCodeApiKey(registryKey, getConfiguredKey())
|
|
46
|
+
if (!apiKey) {
|
|
47
|
+
ctx.ui.notify(
|
|
48
|
+
"Command Code quota requires an API key. Run /login and select Command Code, or set COMMAND_CODE_API_KEY.",
|
|
49
|
+
"warning",
|
|
50
|
+
)
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const result = await fetchQuota({
|
|
55
|
+
apiKey,
|
|
56
|
+
baseUrl: options.apiBase,
|
|
57
|
+
extraHeaders: options.headers,
|
|
58
|
+
})
|
|
59
|
+
if (!result.ok) {
|
|
60
|
+
ctx.ui.notify(redactValue(result.error.message), "error")
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
ctx.ui.notify(formatQuota(result.quota), "info")
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CommandCodeCredits,
|
|
3
|
+
CommandCodeQuota,
|
|
4
|
+
CommandCodeSubscription,
|
|
5
|
+
CommandCodeWindowLimit,
|
|
6
|
+
} from "./quota-types.ts"
|
|
7
|
+
|
|
8
|
+
export function formatWindowLimits(
|
|
9
|
+
limits: readonly CommandCodeWindowLimit[],
|
|
10
|
+
now: () => number = Date.now,
|
|
11
|
+
): string[] {
|
|
12
|
+
const labels: Record<CommandCodeWindowLimit["window"], string> = {
|
|
13
|
+
fiveHour: "5-hour",
|
|
14
|
+
weekly: "Weekly",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return limits.map((limit) => {
|
|
18
|
+
const used = limit.used.toFixed(2)
|
|
19
|
+
const cap = limit.cap.toFixed(2)
|
|
20
|
+
const percent = limit.cap > 0 ? Math.round((limit.used / limit.cap) * 100) : 0
|
|
21
|
+
const reset = limit.resetAt === null ? "" : ` (resets ${formatResetClock(limit.resetAt, now)})`
|
|
22
|
+
return `${labels[limit.window]}: ${used} / ${cap} credits (${percent}% used)${reset}`
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function formatResetClock(resetAtSeconds: number, now: () => number): string {
|
|
27
|
+
const date = new Date(resetAtSeconds * 1000)
|
|
28
|
+
if (Number.isNaN(date.getTime())) return "unknown"
|
|
29
|
+
const diffMs = date.getTime() - now()
|
|
30
|
+
if (diffMs <= 0) return "soon"
|
|
31
|
+
const minutes = Math.ceil(diffMs / 60_000)
|
|
32
|
+
if (minutes < 60) return `in ${minutes}m`
|
|
33
|
+
const hours = Math.floor(minutes / 60)
|
|
34
|
+
const remainingMinutes = minutes % 60
|
|
35
|
+
if (hours < 24) {
|
|
36
|
+
return remainingMinutes > 0 ? `in ${hours}h ${remainingMinutes}m` : `in ${hours}h`
|
|
37
|
+
}
|
|
38
|
+
const days = Math.floor(hours / 24)
|
|
39
|
+
return days === 1 ? "in 1 day" : `in ${days} days`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function creditsDetail(credits: CommandCodeCredits | null): string | undefined {
|
|
43
|
+
if (!credits) return undefined
|
|
44
|
+
const parts = [
|
|
45
|
+
`monthly $${credits.monthlyCredits.toFixed(2)}`,
|
|
46
|
+
`purchased $${credits.purchasedCredits.toFixed(2)}`,
|
|
47
|
+
]
|
|
48
|
+
if (credits.freeCredits > 0) parts.push(`free $${credits.freeCredits.toFixed(2)}`)
|
|
49
|
+
return `Sources: ${parts.join(" / ")}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function subscriptionLine(subscription: CommandCodeSubscription): string {
|
|
53
|
+
const plan = (subscription.planId ?? "Unknown").replace(/[_-]+/g, " ").trim()
|
|
54
|
+
const status = subscription.status ? ` (${subscription.status})` : ""
|
|
55
|
+
return `Plan: ${plan}${status}`
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function formatTokens(tokens: number): string {
|
|
59
|
+
if (tokens >= 1_000_000_000) return `${(tokens / 1_000_000_000).toFixed(1)}B`
|
|
60
|
+
if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`
|
|
61
|
+
if (tokens >= 1_000) return `${(tokens / 1_000).toFixed(1)}k`
|
|
62
|
+
return String(tokens)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function formatQuota(quota: CommandCodeQuota, now: () => number = Date.now): string {
|
|
66
|
+
const lines: string[] = []
|
|
67
|
+
const remaining = quota.credits?.remainingCredits ?? 0
|
|
68
|
+
const spent = quota.summary?.totalCost ?? 0
|
|
69
|
+
const pool = remaining + spent
|
|
70
|
+
|
|
71
|
+
if (quota.credits || quota.summary) {
|
|
72
|
+
lines.push("Credits")
|
|
73
|
+
lines.push(` Remaining: $${remaining.toFixed(2)} of $${pool.toFixed(2)}`)
|
|
74
|
+
lines.push(` Used: $${spent.toFixed(2)}`)
|
|
75
|
+
lines.push(` ${pool > 0 ? Math.round((spent / pool) * 100) : 0}% used`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const detail = creditsDetail(quota.credits)
|
|
79
|
+
if (detail) lines.push(detail)
|
|
80
|
+
if (quota.subscription) lines.push(subscriptionLine(quota.subscription))
|
|
81
|
+
|
|
82
|
+
if (quota.summary) {
|
|
83
|
+
lines.push("")
|
|
84
|
+
lines.push(quota.subscription?.currentPeriodStart ? "Usage (billing period)" : "Usage")
|
|
85
|
+
lines.push(` Cost: $${quota.summary.totalCost.toFixed(2)}`)
|
|
86
|
+
lines.push(` Requests: ${quota.summary.totalCount.toLocaleString("en-US")}`)
|
|
87
|
+
if (quota.summary.totalTokens !== undefined) {
|
|
88
|
+
lines.push(` Tokens: ${formatTokens(quota.summary.totalTokens)}`)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
lines.push("")
|
|
93
|
+
lines.push("Account")
|
|
94
|
+
lines.push(` ${quota.account.keyName ?? quota.account.login}`)
|
|
95
|
+
|
|
96
|
+
const limits = quota.credits?.windowLimits ?? []
|
|
97
|
+
if (limits.length > 0) {
|
|
98
|
+
lines.push("")
|
|
99
|
+
lines.push("Usage windows:")
|
|
100
|
+
lines.push(...formatWindowLimits(limits, now).map((line) => ` ${line}`))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if ((quota.unavailable?.length ?? 0) > 0) {
|
|
104
|
+
lines.push("")
|
|
105
|
+
lines.push(`Unavailable: ${quota.unavailable?.join(", ")}`)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
lines.push("")
|
|
109
|
+
lines.push("Full detail: https://commandcode.ai/usage")
|
|
110
|
+
return lines.join("\n")
|
|
111
|
+
}
|