- Removed unnecessary blank lines and improved formatting for better readability in index.ts and converters.ts. - Updated package-lock.json to downgrade several dependencies for compatibility, including @protobufjs/eventemitter, @protobufjs/fetch, and others. - Enhanced error message in core.ts to clarify configuration options for the Command Code API key. - Added a test to ensure the correct handling of environment variable values in test-stream.ts.
66 lines
2.2 KiB
TypeScript
66 lines
2.2 KiB
TypeScript
/**
|
|
* Command Code provider for pi.
|
|
*
|
|
* Connects pi to Command Code's API (https://api.commandcode.ai/alpha/generate).
|
|
*
|
|
* Authentication (pick one):
|
|
* 1. Run `/login`, then select Command Code — opens browser to commandcode.ai, auto-stores API key
|
|
* 2. Set COMMANDCODE_API_KEY environment variable
|
|
* 3. Place API key in `~/.commandcode/auth.json` or `~/.pi/agent/auth.json`
|
|
* as {"apiKey": "user_..."} or {"commandcode": "user_..."}
|
|
*
|
|
* Models are fetched from Command Code's Provider API at startup.
|
|
*/
|
|
|
|
import { AssistantMessageEventStream, calculateCost } from "@mariozechner/pi-ai"
|
|
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"
|
|
|
|
import { COMMAND_CODE_CLI_VERSION, createStreamCommandCode, DEFAULT_API_BASE } from "./src/core.ts"
|
|
import { DEFAULT_MODELS_URL, fetchCommandCodeModels } from "./src/models.ts"
|
|
import { getApiKey, login, refreshToken } from "./src/oauth.ts"
|
|
|
|
const API_BASE = process.env.COMMANDCODE_API_BASE ?? DEFAULT_API_BASE
|
|
const MODELS_URL = process.env.COMMANDCODE_MODELS_URL ?? DEFAULT_MODELS_URL
|
|
|
|
const streamCommandCode = createStreamCommandCode({
|
|
createStream: () => new AssistantMessageEventStream(),
|
|
calculateCost,
|
|
apiBase: API_BASE,
|
|
})
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Extension entry point
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export default async function (pi: ExtensionAPI) {
|
|
const models = await fetchCommandCodeModels({ url: MODELS_URL })
|
|
|
|
pi.registerProvider("commandcode", {
|
|
name: "Command Code",
|
|
baseUrl: API_BASE,
|
|
apiKey: "COMMANDCODE_API_KEY",
|
|
authHeader: true,
|
|
api: "commandcode-custom",
|
|
streamSimple: streamCommandCode,
|
|
headers: {
|
|
"x-command-code-version": COMMAND_CODE_CLI_VERSION,
|
|
"x-cli-environment": "production",
|
|
},
|
|
oauth: {
|
|
name: "Command Code",
|
|
login,
|
|
refreshToken,
|
|
getApiKey,
|
|
},
|
|
models: models.map((model) => ({
|
|
id: model.id,
|
|
name: model.name,
|
|
reasoning: model.reasoning,
|
|
input: ["text"] as const,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
contextWindow: model.contextWindow,
|
|
maxTokens: model.maxTokens,
|
|
})),
|
|
})
|
|
}
|