fix(backend): inject provider credentials from file

This commit is contained in:
2026-07-12 07:53:22 +02:00
parent ee92ef45ab
commit e40a9d9a56
13 changed files with 280 additions and 15 deletions
+13
View File
@@ -1,3 +1,5 @@
import path from "node:path";
export interface AppConfig {
host: string; port: number; harnessDir: string; thtBin: string; piBin: string;
authMode: "none" | "mock" | "upstream";
@@ -6,6 +8,7 @@ export interface AppConfig {
settingsFile: string;
dataRoot?: string;
ollamaEnsureTimeoutMs: number;
modelApiKeyFile?: string;
}
export function loadConfig(env: Record<string, string | undefined>): AppConfig {
const authMode = env.AUTH_MODE ?? "none";
@@ -15,6 +18,15 @@ export function loadConfig(env: Record<string, string | undefined>): AppConfig {
if (env.THOTH_PUBLIC_EXPOSURE === "true" && authMode !== "upstream") {
throw new Error("public exposure requires AUTH_MODE=upstream behind a trusted proxy");
}
const modelApiKeyFile = env.THT_MODEL_API_KEY_FILE;
if (modelApiKeyFile !== undefined && (
modelApiKeyFile.trim() !== modelApiKeyFile
|| modelApiKeyFile.length === 0
|| modelApiKeyFile.includes("\0")
|| !path.isAbsolute(modelApiKeyFile)
)) {
throw new Error("model credential configuration is invalid");
}
return {
host: env.HOST ?? "127.0.0.1",
port: Number(env.PORT ?? 8787),
@@ -27,5 +39,6 @@ export function loadConfig(env: Record<string, string | undefined>): AppConfig {
settingsFile: env.SETTINGS_FILE ?? "data/settings.json",
dataRoot: env.THT_DATA_ROOT,
ollamaEnsureTimeoutMs: Number(env.OLLAMA_ENSURE_TIMEOUT_MS ?? 60000),
modelApiKeyFile,
};
}
+71 -6
View File
@@ -1,4 +1,5 @@
import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import { closeSync, constants, fstatSync, lstatSync, openSync, readFileSync } from "node:fs";
import type { AppConfig } from "../config.js";
import { RpcClient } from "../rpc/rpc-client.js";
import { SessionBridge } from "../bridge/session-bridge.js";
@@ -17,26 +18,90 @@ type SpawnFn = (
options: { cwd: string; env: NodeJS.ProcessEnv },
) => ChildProcessWithoutNullStreams;
const PROVIDER_KEY_ENV: Readonly<Record<string, string>> = {
anthropic: "ANTHROPIC_API_KEY",
openai: "OPENAI_API_KEY",
gemini: "GEMINI_API_KEY",
google: "GEMINI_API_KEY",
deepseek: "DEEPSEEK_API_KEY",
zai: "ZAI_API_KEY",
groq: "GROQ_API_KEY",
mistral: "MISTRAL_API_KEY",
openrouter: "OPENROUTER_API_KEY",
xai: "XAI_API_KEY",
cerebras: "CEREBRAS_API_KEY",
cohere: "COHERE_API_KEY",
};
const LOCAL_PROVIDERS = new Set(["ollama", "lmstudio", "local", "aritmolab"]);
const PROVIDER_ENV_NAMES = new Set(Object.values(PROVIDER_KEY_ENV));
function normalizedProvider(provider: string | undefined): string | undefined {
const value = provider?.trim().toLowerCase();
return value || undefined;
}
function readModelCredential(file: string): string {
let fd: number | undefined;
try {
const before = lstatSync(file);
if (!before.isFile() || before.isSymbolicLink() || before.nlink !== 1) throw new Error();
fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW);
const info = fstatSync(fd);
const mode = info.mode & 0o777;
const ownedStrict = info.uid === process.getuid?.() && (mode === 0o400 || mode === 0o600);
const dockerSecret = file.startsWith("/run/secrets/") && mode === 0o444;
if (!info.isFile() || info.nlink !== 1 || (!ownedStrict && !dockerSecret) || info.size > 16_384) {
throw new Error();
}
const value = readFileSync(fd, "utf8");
if (!value || /\s/.test(value)) throw new Error();
return value;
} catch {
throw new Error("model provider credential is unavailable");
} finally {
if (fd !== undefined) closeSync(fd);
}
}
export class PiProcessManager {
private runtimes = new Map<string, SessionRuntime>();
private spawnFn: (sessionId: string, author: string) => ChildProcessWithoutNullStreams;
private spawnFn: (
sessionId: string, author: string, provider: string | undefined,
) => ChildProcessWithoutNullStreams;
constructor(private cfg: AppConfig, opts?: { spawnFn?: SpawnFn }) {
if (opts?.spawnFn) {
this.spawnFn = (sessionId: string, author: string) => this.spawnPi(opts.spawnFn!, sessionId, author);
this.spawnFn = (sessionId, author, provider) =>
this.spawnPi(opts.spawnFn!, sessionId, author, provider);
} else {
this.spawnFn = (sessionId: string, author: string) => this.spawnPi(nodeSpawn, sessionId, author);
this.spawnFn = (sessionId, author, provider) =>
this.spawnPi(nodeSpawn, sessionId, author, provider);
}
}
private spawnPi(spawnFn: SpawnFn, sessionId: string, author: string): ChildProcessWithoutNullStreams {
private spawnPi(
spawnFn: SpawnFn, sessionId: string, author: string, provider: string | undefined,
): ChildProcessWithoutNullStreams {
const env: NodeJS.ProcessEnv = {
...process.env,
THT_SESSION: sessionId,
THT_AUTHOR: author,
};
delete env.THT_DATA_ROOT;
delete env.PI_PROVIDER_API_KEY;
delete env.THT_MODEL_API_KEY_FILE;
for (const name of PROVIDER_ENV_NAMES) delete env[name];
if (this.cfg.dataRoot !== undefined) env.THT_DATA_ROOT = this.cfg.dataRoot;
const normalized = normalizedProvider(provider);
if (normalized && !LOCAL_PROVIDERS.has(normalized)) {
const envName = PROVIDER_KEY_ENV[normalized];
if (!envName || !this.cfg.modelApiKeyFile) {
throw new Error("model provider credential is unavailable");
}
env[envName] = readModelCredential(this.cfg.modelApiKeyFile);
} else if (this.cfg.modelApiKeyFile && !normalized) {
throw new Error("model provider credential is unavailable");
}
// pi 0.73 removed `--approve`: rpc mode is headless and its argv is intentionally minimal.
const child = spawnFn(this.cfg.piBin, ["--mode", "rpc"], {
cwd: this.cfg.harnessDir,
@@ -67,7 +132,8 @@ export class PiProcessManager {
throw new Error("max Pi processes reached");
}
const author = o.author ?? "dev@local";
const child = this.spawnFn(sessionId, author);
const provider = o.provider ?? this.cfg.defaults.provider;
const child = this.spawnFn(sessionId, author, provider);
const rpc = new RpcClient(child);
const bridge = new SessionBridge(rpc);
const rt: SessionRuntime = { rpc, bridge, child };
@@ -88,7 +154,6 @@ export class PiProcessManager {
}
});
const provider = o.provider ?? this.cfg.defaults.provider;
const model = o.model ?? this.cfg.defaults.model;
const thinking = o.thinking ?? this.cfg.defaults.thinking;