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:
2026-09-14 11:19:33 +08:00
parent 8007a6480c
commit 5947e133de
84 changed files with 6644 additions and 16083 deletions
+116
View File
@@ -0,0 +1,116 @@
import assert from "node:assert/strict"
import { test } from "node:test"
import {
buildCatalog,
parseBundleMetadata,
parseRates,
parseReference,
parseReferenceRow,
sliceModelObject,
} from "../../scripts/sync-catalog.mjs"
/** Mirrors one row of the CLI reference table, including the em-dash placeholders. */
const referenceMarkdown = [
"| Id (use EXACTLY this) | Name | Context | Efforts | $/1M in/out · cache read | Min plan | Best for |",
"|---|---|---|---|---|---|---|",
"| `deepseek/deepseek-v4.1-flash` | DeepSeek V4.1 Flash | 1M | low, high, max | $0.15/$0.6 · cache $0.003 | Go and above | reasoning with vision |",
"| `moonshotai/Kimi-K2.7-Code` | Kimi K2.7 Code | 256K | — | $0.95/$4 · cache $0.19 | Go and above | long-horizon coding |",
"| `claude-sonnet-4-6` | Claude Sonnet 4.6 | 1M | low, medium, high, xhigh, max | $3/$15 · cache $0.3 (write $3.75) | Pro and above | fast and capable |",
].join("\n")
/** Trimmed shape of the minified CLI model literal. */
const cliBundle = [
"var $L={SONNET:{id:\"claude-sonnet-4-6\",inputModalities:[\"text\",\"image\"],provider:\"x\",spec:\"chatComplete\",label:\"Claude Sonnet 4.6\",reasoning:!0,reasoningEfforts:[\"low\",\"medium\",\"high\",\"xhigh\",\"max\"],contextWindow:1e6},",
"FLASH:{id:\"deepseek/deepseek-v4.1-flash\",inputModalities:[\"text\",\"image\"],provider:\"y\",spec:\"chatComplete\",label:\"DeepSeek V4.1 Flash\",reasoning:!0,reasoningEfforts:[\"low\",\"high\",\"max\"],contextWindow:1e6},",
"KIMI:{id:\"moonshotai/Kimi-K2.7-Code\",inputModalities:[\"text\",\"image\"],provider:\"z\",spec:\"chatComplete\",label:\"Kimi K2.7 Code\",reasoning:!0,contextWindow:262144}};",
].join("")
/** Output shape of the generator, asserted field by field below. */
interface CatalogEntry {
id: string
name: string
contextWindow: number
efforts: string[]
reasoning: boolean
input: string[]
maxOutputTokens: number
cost: { input: number; output: number; cacheRead: number; cacheWrite: number }
}
test("parseReference reads ids, names, efforts and rates", () => {
const models = parseReference(referenceMarkdown)
assert.deepEqual(models.map((model) => model.id), [
"deepseek/deepseek-v4.1-flash",
"moonshotai/Kimi-K2.7-Code",
"claude-sonnet-4-6",
])
assert.deepEqual(models[0]?.efforts, ["low", "high", "max"])
assert.deepEqual(models[1]?.efforts, [])
assert.deepEqual(models[2]?.cost, { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 })
})
test("parseReference ignores header rows and rejects unknown efforts", () => {
assert.equal(parseReferenceRow("| Id (use EXACTLY this) | Name | Context | Efforts |"), undefined)
assert.throws(
() => parseReference("| `m` | M | 1M | turbo | $1/$2 · cache $0 | Go |"),
/Unknown effort/,
)
})
test("parseRates handles free and cache-write pricing", () => {
assert.deepEqual(parseRates("$0/$0 · cache $0"), { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 })
assert.deepEqual(parseRates("$2/$6 · cache $0.25 (write $2.5)"), {
input: 2,
output: 6,
cacheRead: 0.25,
cacheWrite: 2.5,
})
})
test("sliceModelObject extracts one nested literal from the minified bundle", () => {
const entry = sliceModelObject(cliBundle, "moonshotai/Kimi-K2.7-Code")
assert.ok(entry?.startsWith('{id:"moonshotai/Kimi-K2.7-Code",inputModalities:'))
assert.ok(entry?.includes('contextWindow:262144}'))
assert.ok(entry?.endsWith('contextWindow:262144}'))
assert.equal(sliceModelObject(cliBundle, "missing/model"), undefined)
})
test("parseBundleMetadata reports modalities, reasoning and limits", () => {
const metadata = parseBundleMetadata(cliBundle, [
"claude-sonnet-4-6",
"moonshotai/Kimi-K2.7-Code",
]) as Map<string, { input: string[]; reasoning: boolean; maxOutputTokens?: number; contextWindow?: number }>
assert.deepEqual(metadata.get("claude-sonnet-4-6")?.input, ["text", "image"])
assert.equal(metadata.get("claude-sonnet-4-6")?.reasoning, true)
assert.equal(metadata.get("claude-sonnet-4-6")?.contextWindow, 1_000_000)
// Reason-only models carry no effort list but still report reasoning.
assert.equal(metadata.get("moonshotai/Kimi-K2.7-Code")?.reasoning, true)
assert.equal(metadata.get("moonshotai/Kimi-K2.7-Code")?.maxOutputTokens, undefined)
})
test("buildCatalog merges the reference table with bundle metadata", () => {
const reference = parseReference(referenceMarkdown)
const metadata = parseBundleMetadata(
cliBundle,
reference.map((model: { id: string }) => model.id),
)
const catalog = buildCatalog("1.54.0", reference, metadata) as CatalogEntry[]
assert.deepEqual(catalog.map((model) => model.id), reference.map((model) => model.id))
assert.deepEqual(catalog[0], {
id: "deepseek/deepseek-v4.1-flash",
name: "DeepSeek V4.1 Flash",
contextWindow: 1_000_000,
efforts: ["low", "high", "max"],
reasoning: true,
input: ["text", "image"],
maxOutputTokens: 0,
cost: { input: 0.15, output: 0.6, cacheRead: 0.003, cacheWrite: 0 },
})
assert.equal(catalog[1]?.reasoning, true)
assert.equal(catalog[2]?.cost.cacheWrite, 3.75)
})