refactor: migrate embedded pi to sdk

This commit is contained in:
Peter Steinberger
2025-12-22 18:05:44 +01:00
parent 79c0fd27a0
commit 2d7c5f8c53
12 changed files with 276 additions and 386 deletions

View File

@@ -1,24 +1,17 @@
// Lazy-load pi-ai model metadata so we can infer context windows when the agent
// reports a model id. pi-coding-agent depends on @mariozechner/pi-ai, so it
// should be present whenever CLAWDIS is installed from npm.
// Lazy-load pi-coding-agent model metadata so we can infer context windows when
// the agent reports a model id. This includes custom models.json entries.
type ModelEntry = { id: string; contextWindow?: number };
const MODEL_CACHE = new Map<string, number>();
const loadPromise = (async () => {
try {
const piAi = (await import("@mariozechner/pi-ai")) as {
getProviders: () => string[];
getModels: (provider: string) => ModelEntry[];
};
const providers = piAi.getProviders();
for (const p of providers) {
const models = piAi.getModels(p) as ModelEntry[];
for (const m of models) {
if (!m?.id) continue;
if (typeof m.contextWindow === "number" && m.contextWindow > 0) {
MODEL_CACHE.set(m.id, m.contextWindow);
}
const { discoverModels } = await import("@mariozechner/pi-coding-agent");
const models = discoverModels() as ModelEntry[];
for (const m of models) {
if (!m?.id) continue;
if (typeof m.contextWindow === "number" && m.contextWindow > 0) {
MODEL_CACHE.set(m.id, m.contextWindow);
}
}
} catch {