|
| 1 | +/** |
| 2 | + * SettingsRoutes |
| 3 | + * |
| 4 | + * API endpoints for reading and writing model preferences from ~/.pilot/config.json. |
| 5 | + * |
| 6 | + * GET /api/settings - Returns current model config with defaults merged in |
| 7 | + * PUT /api/settings - Partial update of model preferences (merge, not replace) |
| 8 | + */ |
| 9 | + |
| 10 | +import express, { type Request, type Response } from "express"; |
| 11 | +import * as fs from "fs"; |
| 12 | +import * as os from "os"; |
| 13 | +import * as path from "path"; |
| 14 | +import { BaseRouteHandler } from "../BaseRouteHandler.js"; |
| 15 | +import { logger } from "../../../../utils/logger.js"; |
| 16 | + |
| 17 | +export const MODEL_CHOICES_FULL: readonly string[] = ["sonnet", "sonnet[1m]", "opus", "opus[1m]"]; |
| 18 | +export const MODEL_CHOICES_AGENT: readonly string[] = ["sonnet", "opus"]; |
| 19 | + |
| 20 | +export interface ModelSettings { |
| 21 | + model: string; |
| 22 | + commands: Record<string, string>; |
| 23 | + agents: Record<string, string>; |
| 24 | +} |
| 25 | + |
| 26 | +export const DEFAULT_SETTINGS: ModelSettings = { |
| 27 | + model: "opus", |
| 28 | + commands: { |
| 29 | + spec: "sonnet", |
| 30 | + "spec-plan": "opus", |
| 31 | + "spec-implement": "sonnet", |
| 32 | + "spec-verify": "opus", |
| 33 | + vault: "sonnet", |
| 34 | + sync: "sonnet", |
| 35 | + learn: "sonnet", |
| 36 | + }, |
| 37 | + agents: { |
| 38 | + "plan-challenger": "sonnet", |
| 39 | + "plan-verifier": "sonnet", |
| 40 | + "spec-reviewer-compliance": "sonnet", |
| 41 | + "spec-reviewer-quality": "opus", |
| 42 | + }, |
| 43 | +}; |
| 44 | + |
| 45 | +export class SettingsRoutes extends BaseRouteHandler { |
| 46 | + private readonly configPath: string; |
| 47 | + |
| 48 | + constructor(configPath?: string) { |
| 49 | + super(); |
| 50 | + this.configPath = configPath ?? path.join(os.homedir(), ".pilot", "config.json"); |
| 51 | + } |
| 52 | + |
| 53 | + setupRoutes(app: express.Application): void { |
| 54 | + app.get("/api/settings", this.wrapHandler(this.handleGet.bind(this))); |
| 55 | + app.put("/api/settings", this.wrapHandler(this.handlePut.bind(this))); |
| 56 | + } |
| 57 | + |
| 58 | + private readConfig(): Record<string, unknown> { |
| 59 | + try { |
| 60 | + const raw = fs.readFileSync(this.configPath, "utf-8"); |
| 61 | + return JSON.parse(raw) as Record<string, unknown>; |
| 62 | + } catch { |
| 63 | + return {}; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private mergeWithDefaults(raw: Record<string, unknown>): ModelSettings { |
| 68 | + const mainModel = |
| 69 | + typeof raw.model === "string" && MODEL_CHOICES_FULL.includes(raw.model) |
| 70 | + ? raw.model |
| 71 | + : DEFAULT_SETTINGS.model; |
| 72 | + |
| 73 | + const rawCommands = raw.commands; |
| 74 | + const mergedCommands: Record<string, string> = { ...DEFAULT_SETTINGS.commands }; |
| 75 | + if (rawCommands && typeof rawCommands === "object" && !Array.isArray(rawCommands)) { |
| 76 | + for (const [k, v] of Object.entries(rawCommands as Record<string, unknown>)) { |
| 77 | + if (typeof v === "string" && MODEL_CHOICES_FULL.includes(v)) { |
| 78 | + mergedCommands[k] = v; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const rawAgents = raw.agents; |
| 84 | + const mergedAgents: Record<string, string> = { ...DEFAULT_SETTINGS.agents }; |
| 85 | + if (rawAgents && typeof rawAgents === "object" && !Array.isArray(rawAgents)) { |
| 86 | + for (const [k, v] of Object.entries(rawAgents as Record<string, unknown>)) { |
| 87 | + if (typeof v === "string" && MODEL_CHOICES_AGENT.includes(v)) { |
| 88 | + mergedAgents[k] = v; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return { model: mainModel, commands: mergedCommands, agents: mergedAgents }; |
| 94 | + } |
| 95 | + |
| 96 | + private validateSettings(body: Record<string, unknown>): string | null { |
| 97 | + if (body.model !== undefined) { |
| 98 | + if (typeof body.model !== "string" || !MODEL_CHOICES_FULL.includes(body.model)) { |
| 99 | + return `Invalid model '${body.model}'; must be one of: ${MODEL_CHOICES_FULL.join(", ")}`; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (body.commands !== undefined) { |
| 104 | + if (typeof body.commands !== "object" || Array.isArray(body.commands)) { |
| 105 | + return "commands must be an object"; |
| 106 | + } |
| 107 | + for (const [cmd, model] of Object.entries(body.commands as Record<string, unknown>)) { |
| 108 | + if (typeof model !== "string" || !MODEL_CHOICES_FULL.includes(model)) { |
| 109 | + return `Invalid model '${model}' for command '${cmd}'; must be one of: ${MODEL_CHOICES_FULL.join(", ")}`; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (body.agents !== undefined) { |
| 115 | + if (typeof body.agents !== "object" || Array.isArray(body.agents)) { |
| 116 | + return "agents must be an object"; |
| 117 | + } |
| 118 | + for (const [agent, model] of Object.entries(body.agents as Record<string, unknown>)) { |
| 119 | + if (typeof model !== "string" || !MODEL_CHOICES_AGENT.includes(model)) { |
| 120 | + return `Invalid model '${model}' for agent '${agent}'; agents can only use: ${MODEL_CHOICES_AGENT.join(", ")} (no 1M context)`; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + private writeConfigAtomic(data: Record<string, unknown>): void { |
| 129 | + const dir = path.dirname(this.configPath); |
| 130 | + fs.mkdirSync(dir, { recursive: true }); |
| 131 | + const tmpPath = this.configPath + ".tmp"; |
| 132 | + fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2), "utf-8"); |
| 133 | + fs.renameSync(tmpPath, this.configPath); |
| 134 | + } |
| 135 | + |
| 136 | + async handleGet(_req: Request, res: Response): Promise<void> { |
| 137 | + const raw = this.readConfig(); |
| 138 | + const settings = this.mergeWithDefaults(raw); |
| 139 | + res.json(settings); |
| 140 | + } |
| 141 | + |
| 142 | + async handlePut(req: Request, res: Response): Promise<void> { |
| 143 | + const body = req.body as Record<string, unknown>; |
| 144 | + |
| 145 | + const error = this.validateSettings(body); |
| 146 | + if (error) { |
| 147 | + this.badRequest(res, error); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + const existing = this.readConfig(); |
| 152 | + |
| 153 | + if (body.model !== undefined) { |
| 154 | + existing.model = body.model; |
| 155 | + } |
| 156 | + if (body.commands !== undefined) { |
| 157 | + const existingCommands = (existing.commands as Record<string, unknown>) ?? {}; |
| 158 | + existing.commands = { ...existingCommands, ...(body.commands as Record<string, unknown>) }; |
| 159 | + } |
| 160 | + if (body.agents !== undefined) { |
| 161 | + const existingAgents = (existing.agents as Record<string, unknown>) ?? {}; |
| 162 | + existing.agents = { ...existingAgents, ...(body.agents as Record<string, unknown>) }; |
| 163 | + } |
| 164 | + |
| 165 | + try { |
| 166 | + this.writeConfigAtomic(existing); |
| 167 | + } catch (err) { |
| 168 | + logger.error("HTTP", "Failed to write settings config", {}, err as Error); |
| 169 | + res.status(500).json({ error: "Failed to save settings" }); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + const updated = this.mergeWithDefaults(existing); |
| 174 | + res.json(updated); |
| 175 | + } |
| 176 | +} |
0 commit comments