Add bundled pi default and session token reporting

This commit is contained in:
Peter Steinberger
2025-12-05 22:33:09 +01:00
parent fe87160b19
commit 690113dd73
15 changed files with 427 additions and 6 deletions

34
src/agents/context.ts Normal file
View File

@@ -0,0 +1,34 @@
// 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.
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);
}
}
}
} catch {
// If pi-ai isn't available, leave cache empty; lookup will fall back.
}
})();
export function lookupContextTokens(modelId?: string): number | undefined {
if (!modelId) return undefined;
// Best-effort: kick off loading, but don't block.
void loadPromise;
return MODEL_CACHE.get(modelId);
}

5
src/agents/defaults.ts Normal file
View File

@@ -0,0 +1,5 @@
// Defaults for agent metadata when upstream does not supply them.
// Model id uses pi-ai's built-in Anthropic catalog.
export const DEFAULT_MODEL = "claude-opus-4-5";
// Context window: Opus 4.5 supports ~200k tokens (per pi-ai models.generated.ts).
export const DEFAULT_CONTEXT_TOKENS = 200_000;

26
src/agents/pi-path.ts Normal file
View File

@@ -0,0 +1,26 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
// Resolve the bundled pi/tau binary path from the installed dependency.
export function resolveBundledPiBinary(): string | null {
try {
const require = createRequire(import.meta.url);
const pkgPath = require.resolve(
"@mariozechner/pi-coding-agent/package.json",
);
const pkgDir = path.dirname(pkgPath);
// Prefer compiled binary if present, else fall back to dist/cli.js (has shebang).
const binCandidates = [
path.join(pkgDir, "dist", "pi"),
path.join(pkgDir, "dist", "cli.js"),
path.join(pkgDir, "bin", "tau-dev.mjs"),
];
for (const candidate of binCandidates) {
if (fs.existsSync(candidate)) return candidate;
}
} catch {
// Dependency missing or resolution failed.
}
return null;
}