feat(backend): ephemeral Pi model lister (get_available_models) with TTL cache
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from "node:child_process";
|
||||
import { join } from "node:path";
|
||||
import type { AppConfig } from "../config.js";
|
||||
import { RpcClient } from "../rpc/rpc-client.js";
|
||||
|
||||
export interface PiModel {
|
||||
provider: string;
|
||||
id: string;
|
||||
name: string;
|
||||
reasoning: boolean;
|
||||
}
|
||||
|
||||
interface Opts {
|
||||
spawnFn?: () => ChildProcessWithoutNullStreams;
|
||||
ttlMs?: number;
|
||||
nowMs?: () => number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a function that lists Pi's available models (those with auth
|
||||
* configured) via an ephemeral `pi --mode rpc` process. Result is cached for
|
||||
* `ttlMs`. The returned function rejects on timeout/error; callers degrade.
|
||||
*/
|
||||
export function createPiModelLister(cfg: AppConfig, opts: Opts = {}): () => Promise<PiModel[]> {
|
||||
const ttlMs = opts.ttlMs ?? 60_000;
|
||||
const now = opts.nowMs ?? (() => Date.now());
|
||||
const spawnFn =
|
||||
opts.spawnFn ??
|
||||
(() => {
|
||||
const harnessVenvBin = join(cfg.harnessDir, ".venv", "bin");
|
||||
return nodeSpawn(cfg.piBin, ["--mode", "rpc"], {
|
||||
cwd: cfg.harnessDir,
|
||||
env: { ...process.env, PATH: `${harnessVenvBin}:${process.env.PATH ?? ""}` },
|
||||
}) as ChildProcessWithoutNullStreams;
|
||||
});
|
||||
|
||||
let cache: { at: number; models: PiModel[] } | null = null;
|
||||
|
||||
return async function listModels(): Promise<PiModel[]> {
|
||||
if (cache && now() - cache.at < ttlMs) return cache.models;
|
||||
|
||||
const child = spawnFn();
|
||||
child.stderr.resume();
|
||||
const rpc = new RpcClient(child);
|
||||
try {
|
||||
const resp = await Promise.race([
|
||||
rpc.request({ type: "get_available_models" }),
|
||||
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) => ({
|
||||
provider: String(m.provider ?? ""),
|
||||
id: String(m.id ?? ""),
|
||||
name: String(m.name ?? m.id ?? ""),
|
||||
reasoning: Boolean(m.reasoning),
|
||||
}));
|
||||
cache = { at: now(), models };
|
||||
return models;
|
||||
} finally {
|
||||
child.kill();
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user