feat(backend): persistent settings store + settingsFile config
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
data/settings.json
|
||||
@@ -3,6 +3,7 @@ export interface AppConfig {
|
||||
authMode: "none" | "mock" | "oidc";
|
||||
defaults: { provider?: string; model?: string; thinking?: string };
|
||||
maxPiProcesses: number;
|
||||
settingsFile: string;
|
||||
}
|
||||
export function loadConfig(env: Record<string, string | undefined>): AppConfig {
|
||||
return {
|
||||
@@ -13,5 +14,6 @@ export function loadConfig(env: Record<string, string | undefined>): AppConfig {
|
||||
authMode: (env.AUTH_MODE as AppConfig["authMode"]) ?? "none",
|
||||
defaults: { provider: env.PI_PROVIDER, model: env.PI_MODEL, thinking: env.PI_THINKING },
|
||||
maxPiProcesses: Number(env.MAX_PI_PROCESSES ?? 4),
|
||||
settingsFile: env.SETTINGS_FILE ?? "data/settings.json",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import type { AppConfig } from "../config.js";
|
||||
|
||||
export interface Settings {
|
||||
workspace?: string;
|
||||
provider?: string;
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
}
|
||||
|
||||
/** Read settings from cfg.settingsFile. Returns {} if missing or invalid. */
|
||||
export function loadSettings(cfg: AppConfig): Settings {
|
||||
try {
|
||||
const raw = readFileSync(cfg.settingsFile, "utf8");
|
||||
const parsed = JSON.parse(raw);
|
||||
if (parsed && typeof parsed === "object") return parsed as Settings;
|
||||
return {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/** Persist settings (pretty JSON). Creates the parent directory if needed. */
|
||||
export function saveSettings(cfg: AppConfig, s: Settings): Settings {
|
||||
mkdirSync(dirname(cfg.settingsFile), { recursive: true });
|
||||
writeFileSync(cfg.settingsFile, JSON.stringify(s, null, 2) + "\n", "utf8");
|
||||
return s;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { test, expect } from "vitest";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadSettings, saveSettings } from "../src/settings/settings-store.js";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
|
||||
function cfgWith(file: string) {
|
||||
return loadConfig({ THT_HARNESS_DIR: "../harness", SETTINGS_FILE: file });
|
||||
}
|
||||
|
||||
test("loadSettings returns {} when the file does not exist", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "tht-set-"));
|
||||
try {
|
||||
expect(loadSettings(cfgWith(join(dir, "settings.json")))).toEqual({});
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("saveSettings writes the file and loadSettings reads it back", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "tht-set-"));
|
||||
try {
|
||||
const cfg = cfgWith(join(dir, "nested", "settings.json"));
|
||||
const saved = saveSettings(cfg, { workspace: "psd", provider: "zai", model: "glm-5.2", thinking: "medium" });
|
||||
expect(saved.model).toBe("glm-5.2");
|
||||
expect(loadSettings(cfg)).toEqual({ workspace: "psd", provider: "zai", model: "glm-5.2", thinking: "medium" });
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("loadSettings returns {} on corrupt JSON (no throw)", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "tht-set-"));
|
||||
try {
|
||||
const file = join(dir, "settings.json");
|
||||
writeFileSync(file, "{ not json");
|
||||
expect(loadSettings(cfgWith(file))).toEqual({});
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("loadConfig sets settingsFile from SETTINGS_FILE, default data/settings.json", () => {
|
||||
expect(loadConfig({}).settingsFile).toBe("data/settings.json");
|
||||
expect(loadConfig({ SETTINGS_FILE: "/x/y.json" }).settingsFile).toBe("/x/y.json");
|
||||
});
|
||||
Reference in New Issue
Block a user