feat(backend): support container runtime paths

This commit is contained in:
2026-07-11 21:32:33 +02:00
parent a0daa319ca
commit c6c00c336a
9 changed files with 144 additions and 38 deletions
+9 -12
View File
@@ -1,5 +1,4 @@
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";
@@ -11,7 +10,11 @@ export interface PiModel {
}
interface Opts {
spawnFn?: () => ChildProcessWithoutNullStreams;
spawnFn?: (
command: string,
args: string[],
options: { cwd: string; env: NodeJS.ProcessEnv },
) => ChildProcessWithoutNullStreams;
ttlMs?: number;
nowMs?: () => number;
}
@@ -24,22 +27,16 @@ interface Opts {
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;
});
const spawnFn = opts.spawnFn ?? nodeSpawn;
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();
const env: NodeJS.ProcessEnv = { ...process.env };
if (cfg.dataRoot !== undefined) env.THT_DATA_ROOT = cfg.dataRoot;
const child = spawnFn(cfg.piBin, ["--mode", "rpc"], { cwd: cfg.harnessDir, env });
child.stderr.resume();
const rpc = new RpcClient(child);
try {