feat: rewrite the Command Code provider on pi's native provider API
Replace the previous implementation with one that registers the Provider API catalog through pi's own provider layer instead of shipping a custom transport, cache file, and hand-maintained pricing table. - models: derive the catalog from the published command-code CLI package (context windows, reasoning efforts, image input, output limits, rates) and keep it as the offline baseline; scripts/sync-catalog.mjs regenerates it and supports --check - refresh: use refreshModels plus context.publish so pi persists the live /provider/v1/models listing in models-store.json and restores it offline - auth: /login browser transfer through a localhost callback server with a pasted-key fallback; $COMMAND_CODE_API_KEY, --api-key and auth.json keep working - streaming: pi's native openai-completions and anthropic-messages adapters; the generate-transport fallback and Oh My Pi branches are gone - keep the context-overflow rewrite that enables pi's compaction retry and the /commandcode-quota command - tests: 51 cases under tests/<module>/ covering models, catalog sync, auth, the callback server, overflow handling, quota, and the extension factory Verified against the live API: chat, tool round trip, image input and --thinking max on deepseek/deepseek-v4.1-flash, quota output, and catalog persistence in an interactive session.
This commit is contained in:
@@ -1,222 +1,137 @@
|
||||
/**
|
||||
* Command Code provider for pi.
|
||||
*
|
||||
* Uses Command Code's documented Provider API:
|
||||
* https://api.commandcode.ai/provider/v1
|
||||
* Registers the Provider API catalog as a pi provider and layers pi's native
|
||||
* auth, model persistence (`models-store.json`), and OpenAI/Anthropic stream
|
||||
* adapters on top of it. Chat requests therefore use the same code paths as
|
||||
* built-in providers and need no custom transport.
|
||||
*
|
||||
* Provider API reference: https://api.commandcode.ai/provider/v1
|
||||
*/
|
||||
|
||||
import { AssistantMessageEventStream } from "@earendil-works/pi-ai"
|
||||
import * as piAiCompat from "@earendil-works/pi-ai/compat"
|
||||
import { streamSimple as streamNativeProvider } from "@earendil-works/pi-ai/compat"
|
||||
import {
|
||||
getAgentDir,
|
||||
type ExtensionAPI,
|
||||
type ExtensionCommandContext,
|
||||
type ProviderConfig,
|
||||
} from "@earendil-works/pi-coding-agent"
|
||||
import { join } from "node:path"
|
||||
import type { Api, Model, RefreshModelsContext } from "@earendil-works/pi-ai"
|
||||
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent"
|
||||
|
||||
import { getConfiguredApiKey } from "./src/api-key.ts"
|
||||
import { pickCommandCodeApiKey, withResolvedCommandCodeApiKey } from "./src/converters.ts"
|
||||
import { createStreamCommandCode } from "./src/core.ts"
|
||||
import { calculateCommandCodeCost } from "./src/cost.ts"
|
||||
import { discoverApiKey } from "./src/api-key.ts"
|
||||
import { getApiKey, login, refreshToken } from "./src/auth.ts"
|
||||
import {
|
||||
apiForModelId,
|
||||
baseUrlForModel,
|
||||
DEFAULT_MODELS_URL,
|
||||
DEFAULT_PROVIDER_API_BASE,
|
||||
accountApiBase,
|
||||
fetchLiveCatalog,
|
||||
getModelsTimeoutMs,
|
||||
inputModalitiesForModel,
|
||||
loadCachedCommandCodeModels,
|
||||
loadCommandCodeModels,
|
||||
MODEL_EFFORTS,
|
||||
thinkingMetadataForModel,
|
||||
modelsFromCatalog,
|
||||
modelsFromLive,
|
||||
modelsUrl,
|
||||
PROVIDER_ID,
|
||||
providerBaseUrl,
|
||||
providerHeaders,
|
||||
toProviderModel,
|
||||
type CommandCodeModel,
|
||||
} from "./src/models.ts"
|
||||
import { getApiKey as getOAuthApiKey, login, refreshToken } from "./src/oauth.ts"
|
||||
import { normalizeCommandCodeMessage } from "./src/overflow.ts"
|
||||
import { MODEL_COSTS, ZERO_MODEL_COST } from "./src/pricing.ts"
|
||||
import { registerCommandCodeQuota } from "./src/quota-command.ts"
|
||||
import { createCommandCodeRuntime } from "./src/runtime.ts"
|
||||
import { createCommandCodeTransportRouter } from "./src/transport.ts"
|
||||
import { fetchCommandCodeQuota } from "./src/quota.ts"
|
||||
import { formatQuota } from "./src/quota-format.ts"
|
||||
|
||||
const COMMAND_CODE_API = "commandcode-custom"
|
||||
const COMPAT_SOURCE_ID = "pi-commandcode-provider"
|
||||
|
||||
type CompatStreamFunction = (
|
||||
model: Parameters<typeof streamNativeProvider>[0],
|
||||
context: Parameters<typeof streamNativeProvider>[1],
|
||||
options?: Parameters<typeof streamNativeProvider>[2],
|
||||
) => AssistantMessageEventStream
|
||||
type ProviderModelConfig = NonNullable<ProviderConfig["models"]>[number]
|
||||
|
||||
/**
|
||||
* pi's compat entrypoint exposes `registerApiProvider`; Oh My Pi maps
|
||||
* `@earendil-works/pi-ai/compat` onto its own pi-ai, which lacks that export
|
||||
* and registers custom APIs itself inside `registerProvider`. Resolve the
|
||||
* function at runtime so the extension loads on both hosts.
|
||||
* pi resolves this env template itself; leaving it unresolved means "not
|
||||
* configured", so /login credentials and --api-key keep working.
|
||||
*/
|
||||
function compatApiProviderRegistrar(): ((...args: unknown[]) => unknown) | undefined {
|
||||
const register = (piAiCompat as { registerApiProvider?: unknown }).registerApiProvider
|
||||
return typeof register === "function" ? (register as (...args: unknown[]) => unknown) : undefined
|
||||
}
|
||||
const API_KEY_ENV_REFERENCE = "$COMMAND_CODE_API_KEY"
|
||||
|
||||
function registerCompatApiProvider(stream: CompatStreamFunction): void {
|
||||
compatApiProviderRegistrar()?.(
|
||||
{ api: COMMAND_CODE_API, stream, streamSimple: stream },
|
||||
COMPAT_SOURCE_ID,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The `apiKey` handed to `registerProvider` means different things per host.
|
||||
*
|
||||
* pi parses `$COMMAND_CODE_API_KEY` as an env template: unresolved means
|
||||
* "not configured", so `/login` credentials and `--api-key` take over, and
|
||||
* the entry keeps the API-key auth method registered next to OAuth. Without
|
||||
* it pi composes an OAuth-only provider and drops stored `api_key`
|
||||
* credentials and `--api-key`.
|
||||
*
|
||||
* Oh My Pi has no template notion: an unresolved value stays a literal config
|
||||
* override that shadows its `/login` credential store and is sent verbatim as
|
||||
* `Authorization: Bearer $COMMAND_CODE_API_KEY`. There, omit `apiKey` unless
|
||||
* a real key is configured; OMP then reads env keys and stored credentials
|
||||
* itself.
|
||||
*
|
||||
* Hosts are told apart by the same `registerApiProvider` probe used for the
|
||||
* compat registry: pi exports it, OMP does not.
|
||||
*/
|
||||
function providerApiKey(): string | undefined {
|
||||
const configured = pickCommandCodeApiKey(getConfiguredApiKey(), undefined)
|
||||
if (configured) return configured
|
||||
return compatApiProviderRegistrar() ? "$COMMAND_CODE_API_KEY" : undefined
|
||||
}
|
||||
|
||||
function commandCodeHeaders(): Record<string, string> | undefined {
|
||||
if (process.env.CMD_ZDR === "1" || process.env.COMMANDCODE_ZDR === "1") {
|
||||
return { "x-cmd-zdr": "1" }
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
function createProviderConfig(
|
||||
models: readonly CommandCodeModel[],
|
||||
apiBase: string,
|
||||
streamCommandCode: ProviderConfig["streamSimple"],
|
||||
): ProviderConfig {
|
||||
const headers = commandCodeHeaders()
|
||||
/** Models are stored in pi's own catalog cache, which expects full models. */
|
||||
function toStoredModel(model: CommandCodeModel, apiBase: string): Model<"openai-completions" | "anthropic-messages"> {
|
||||
const config = toProviderModel(model, apiBase)
|
||||
return {
|
||||
...config,
|
||||
api: model.api,
|
||||
provider: PROVIDER_ID,
|
||||
baseUrl: config.baseUrl ?? apiBase,
|
||||
} as Model<"openai-completions" | "anthropic-messages">
|
||||
}
|
||||
|
||||
export default function commandCodeProvider(pi: ExtensionAPI): void {
|
||||
const apiBase = providerBaseUrl()
|
||||
const catalogUrl = modelsUrl()
|
||||
const catalogTimeoutMs = getModelsTimeoutMs()
|
||||
const headers = providerHeaders()
|
||||
const baseline = modelsFromCatalog()
|
||||
|
||||
pi.registerProvider(PROVIDER_ID, {
|
||||
name: "Command Code",
|
||||
baseUrl: apiBase,
|
||||
apiKey: providerApiKey(),
|
||||
api: COMMAND_CODE_API,
|
||||
streamSimple: streamCommandCode,
|
||||
headers,
|
||||
apiKey: API_KEY_ENV_REFERENCE,
|
||||
api: "openai-completions",
|
||||
...(headers ? { headers } : {}),
|
||||
models: baseline.map((model) => toProviderModel(model, apiBase)),
|
||||
oauth: {
|
||||
name: "Command Code",
|
||||
isSubscription: true,
|
||||
login,
|
||||
refreshToken,
|
||||
getApiKey: getOAuthApiKey,
|
||||
getApiKey,
|
||||
},
|
||||
models: models.map((model) => ({
|
||||
id: model.id,
|
||||
name: model.name,
|
||||
api: COMMAND_CODE_API,
|
||||
baseUrl: baseUrlForModel(apiBase, model.api),
|
||||
reasoning: model.reasoning,
|
||||
...(thinkingMetadataForModel(model.id) ?? {}),
|
||||
input: [...inputModalitiesForModel(model.id)],
|
||||
cost: MODEL_COSTS[model.id] ?? ZERO_MODEL_COST,
|
||||
contextWindow: model.contextWindow,
|
||||
maxTokens: model.maxTokens,
|
||||
headers,
|
||||
compat:
|
||||
model.api === "openai-completions"
|
||||
? {
|
||||
supportsStore: false,
|
||||
supportsDeveloperRole: false,
|
||||
supportsReasoningEffort: MODEL_EFFORTS[model.id] !== undefined,
|
||||
maxTokensField: "max_tokens",
|
||||
}
|
||||
: {
|
||||
supportsEagerToolInputStreaming: false,
|
||||
supportsLongCacheRetention: false,
|
||||
supportsCacheControlOnTools: false,
|
||||
supportsToolReferences: false,
|
||||
...(model.reasoning ? { forceAdaptiveThinking: true } : {}),
|
||||
refreshModels: async (context: RefreshModelsContext): Promise<ProviderModelConfig[]> => {
|
||||
const stored = (context.stored?.models ?? []).filter(
|
||||
(model: Model<Api>) => model.provider === PROVIDER_ID,
|
||||
)
|
||||
|
||||
if (context.allowNetwork && !context.signal.aborted) {
|
||||
try {
|
||||
const live = await fetchLiveCatalog({
|
||||
url: catalogUrl,
|
||||
timeoutMs: catalogTimeoutMs,
|
||||
signal: context.signal,
|
||||
})
|
||||
const models = modelsFromLive(live)
|
||||
await context.publish({
|
||||
persist: {
|
||||
models: models.map((model) => toStoredModel(model, apiBase)),
|
||||
checkedAt: Date.now(),
|
||||
},
|
||||
})),
|
||||
}
|
||||
}
|
||||
})
|
||||
return models.map((model) => toProviderModel(model, apiBase))
|
||||
} catch {
|
||||
// An unreachable endpoint keeps the persisted or generated catalog.
|
||||
}
|
||||
}
|
||||
|
||||
function legacyApiBase(providerApiBase: string): string {
|
||||
return providerApiBase.replace(/\/provider\/v1\/?$/, "")
|
||||
}
|
||||
|
||||
export default async function (pi: ExtensionAPI) {
|
||||
const apiBase = process.env.COMMANDCODE_API_BASE ?? DEFAULT_PROVIDER_API_BASE
|
||||
const modelsUrl = process.env.COMMANDCODE_MODELS_URL ?? DEFAULT_MODELS_URL
|
||||
const modelsTimeoutMs = getModelsTimeoutMs()
|
||||
const modelsCachePath =
|
||||
process.env.COMMANDCODE_MODELS_CACHE ?? join(getAgentDir(), "commandcode-models.json")
|
||||
const streamGenerate = createStreamCommandCode({
|
||||
createStream: () => new AssistantMessageEventStream(),
|
||||
calculateCost: calculateCommandCodeCost,
|
||||
apiBase: legacyApiBase(apiBase),
|
||||
})
|
||||
const resolveStreamOptions = (options?: Parameters<typeof streamNativeProvider>[2]) =>
|
||||
withResolvedCommandCodeApiKey(options, getConfiguredApiKey())
|
||||
const transport = createCommandCodeTransportRouter({
|
||||
createStream: () => new AssistantMessageEventStream(),
|
||||
streamProvider: (model, context, options) =>
|
||||
streamNativeProvider(
|
||||
{ ...model, api: apiForModelId(model.id), compat: model.compatConfig ?? model.compat },
|
||||
context,
|
||||
resolveStreamOptions(options),
|
||||
),
|
||||
streamGenerate: (model, context, options) =>
|
||||
streamGenerate(model, context, resolveStreamOptions(options)),
|
||||
if (stored.length > 0) return stored as ProviderModelConfig[]
|
||||
return baseline.map((model) => toProviderModel(model, apiBase))
|
||||
},
|
||||
})
|
||||
|
||||
// pi dispatches the main chat through the registered provider, but sibling
|
||||
// extensions that call `streamSimple` from `@earendil-works/pi-ai/compat`
|
||||
// with a Command Code model resolve `model.api` through the compat
|
||||
// api-registry, which knows nothing about extension providers. Register the
|
||||
// custom api there so those calls reach the same transport. The registry
|
||||
// resolves no credentials for extension providers, so fall back to the
|
||||
// configured key when the caller passes none or a placeholder.
|
||||
const compatStream: CompatStreamFunction = (model, context, options) =>
|
||||
transport.stream(model, context, resolveStreamOptions(options)) as AssistantMessageEventStream
|
||||
registerCompatApiProvider(compatStream)
|
||||
|
||||
pi.on("message_end", async (event, ctx) => {
|
||||
// pi only auto-compacts when it recognizes the overflow wording; Command Code
|
||||
// reports context limits in its own phrasing.
|
||||
pi.on("message_end", (event, ctx) => {
|
||||
if (event.message.role !== "assistant") return
|
||||
const normalized = normalizeCommandCodeMessage(event.message, ctx.model?.provider)
|
||||
return normalized ? { message: normalized.message } : undefined
|
||||
})
|
||||
|
||||
registerCommandCodeQuota(pi, {
|
||||
apiBase: legacyApiBase(apiBase),
|
||||
headers: commandCodeHeaders(),
|
||||
})
|
||||
pi.registerCommand("commandcode-quota", {
|
||||
description: "Show Command Code account usage and quota",
|
||||
handler: async (_args, ctx) => {
|
||||
await ctx.waitForIdle()
|
||||
const apiKey = discoverApiKey()
|
||||
if (!apiKey) {
|
||||
ctx.ui.notify(
|
||||
"No Command Code API key found. Run /login and select Command Code, or set COMMAND_CODE_API_KEY.",
|
||||
"warning",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
const runtime = createCommandCodeRuntime<ProviderConfig, ExtensionCommandContext>(pi, {
|
||||
endpoint: modelsUrl,
|
||||
cachePath: modelsCachePath,
|
||||
loadModels: (signal) =>
|
||||
loadCommandCodeModels({
|
||||
url: modelsUrl,
|
||||
cachePath: modelsCachePath,
|
||||
timeoutMs: modelsTimeoutMs,
|
||||
signal,
|
||||
}),
|
||||
loadCachedModels: () => loadCachedCommandCodeModels(modelsCachePath),
|
||||
createProviderConfig: (models) => createProviderConfig(models, apiBase, transport.stream),
|
||||
getTransport: transport.getTransport,
|
||||
const result = await fetchCommandCodeQuota({
|
||||
apiKey,
|
||||
baseUrl: accountApiBase(apiBase),
|
||||
...(headers ? { headers } : {}),
|
||||
})
|
||||
if (!result.ok) {
|
||||
ctx.ui.notify(result.error, "error")
|
||||
return
|
||||
}
|
||||
ctx.ui.notify(formatQuota(result.quota), "info")
|
||||
},
|
||||
})
|
||||
|
||||
pi.on("session_shutdown", () => {
|
||||
runtime.dispose()
|
||||
})
|
||||
|
||||
await runtime.initialize()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user