import { existsSync, readFileSync, readdirSync } from "node:fs"; import { hostname } from "node:os"; import { join } from "node:path"; import { agentRelayHome } from "./config"; interface ProviderConfigMigrationPayload { host: string; configs: Record; } /** * Read the host-local per-provider config files (`~/.agent-relay/providers/*.json`) * so the relay can seed the central `provider-config` rows with server authority * (#465). We stay deliberately dumb here — raw parsed JSON, no defaulting or * schema knowledge — because the relay owns provider-config validation/normalization. * `host` matches what the runner keys central reads by: bare `os.hostname()`. */ export function readLocalProviderConfigs(home = agentRelayHome()): ProviderConfigMigrationPayload { const dir = join(home, "providers"); const configs: Record = {}; if (existsSync(dir)) { for (const file of readdirSync(dir)) { if (!file.endsWith(".json")) continue; const provider = file.slice(0, -".json".length); if (!provider) continue; try { const parsed = JSON.parse(readFileSync(join(dir, file), "utf8")); if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) configs[provider] = parsed; } catch { // Skip unparseable files — a malformed local file shouldn't fail the migration. } } } return { host: hostname(), configs }; }