fix(backend): scope Pi model listing to enabled models

This commit is contained in:
User
2026-07-14 18:33:15 +02:00
parent 1b78f72b64
commit c463de8da2
2 changed files with 108 additions and 71 deletions
+24 -7
View File
@@ -2,7 +2,10 @@ import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from "node:ch
import type { AppConfig } from "../config.js";
import { RpcClient } from "../rpc/rpc-client.js";
import { buildPiChildEnv } from "./provider-credentials.js";
import { secretValue } from "../config/secret-bundle.js";
import {
loadPiEnabledModels,
type PiEnabledModelsResult,
} from "./enabled-models.js";
export interface PiModel {
provider: string;
@@ -19,6 +22,8 @@ interface Opts {
) => ChildProcessWithoutNullStreams;
ttlMs?: number;
nowMs?: () => number;
loadEnabledModels?: () => PiEnabledModelsResult;
warn?: (message: string) => void;
}
/**
@@ -36,11 +41,15 @@ export function createPiModelLister(cfg: AppConfig, opts: Opts = {}): () => Prom
return async function listModels(): Promise<PiModel[]> {
if (cache && now() - cache.at < ttlMs) return cache.models;
const env = buildPiChildEnv({
provider: cfg.defaults.provider,
credentialValue: secretValue(cfg, "THT_MODEL_API_KEY"),
credentialFile: cfg.modelApiKeyFile,
});
const enabled = (opts.loadEnabledModels
?? (() => loadPiEnabledModels({ harnessDir: cfg.harnessDir })))();
for (const warning of enabled.warnings) opts.warn?.(warning);
if (enabled.ids.length === 0) {
cache = { at: now(), models: [] };
return [];
}
const env = buildPiChildEnv({});
delete env.THT_DATA_ROOT;
if (cfg.dataRoot !== undefined) env.THT_DATA_ROOT = cfg.dataRoot;
const child = spawnFn(cfg.piBin, ["--mode", "rpc"], { cwd: cfg.harnessDir, env });
@@ -52,12 +61,20 @@ export function createPiModelLister(cfg: AppConfig, opts: Opts = {}): () => Prom
new Promise<never>((_, rej) => setTimeout(() => rej(new Error("pi model list timeout")), 8000)),
]);
const raw = (resp?.data?.models ?? []) as Array<Record<string, unknown>>;
const models: PiModel[] = raw.map((m) => ({
const available: PiModel[] = raw.map((m) => ({
provider: String(m.provider ?? ""),
id: String(m.id ?? ""),
name: String(m.name ?? m.id ?? ""),
reasoning: Boolean(m.reasoning),
}));
const byCompositeId = new Map(
available.map((model) => [`${model.provider}/${model.id}`, model]),
);
const models = enabled.ids.flatMap((id) => {
const model = byCompositeId.get(id);
return model ? [model] : [];
});
if (models.length === 0) opts.warn?.("No Pi-enabled models are currently available");
cache = { at: now(), models };
return models;
} finally {