|
| 1 | +import fs from "node:fs" |
| 2 | + |
| 3 | +import { PATHS } from "./paths" |
| 4 | + |
| 5 | +export interface AppConfig { |
| 6 | + extraPrompt: string |
| 7 | +} |
| 8 | + |
| 9 | +const defaultConfig: AppConfig = { |
| 10 | + extraPrompt: ` |
| 11 | +## Tool use |
| 12 | +- You have access to many tools. If a tool exists to perform a specific task, you MUST use that tool instead of running a terminal command to perform that task. |
| 13 | +### Bash tool |
| 14 | +When using the Bash tool, follow these rules: |
| 15 | +- always run_in_background set to false, unless you are running a long-running command (e.g., a server or a watch command). |
| 16 | +### BashOutput tool |
| 17 | +When using the BashOutput tool, follow these rules: |
| 18 | +- Only Bash Tool run_in_background set to true, Use BashOutput to read the output later |
| 19 | +### TodoWrite tool |
| 20 | +When using the TodoWrite tool, follow these rules: |
| 21 | +- Skip using the TodoWrite tool for tasks with three or fewer steps. |
| 22 | +- Do not make single-step todo lists. |
| 23 | +- When you made a todo, update it after having performed one of the sub-tasks that you shared on the todo list. |
| 24 | +## Special user requests |
| 25 | +- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as 'date'), you should do so. |
| 26 | +`, |
| 27 | +} |
| 28 | + |
| 29 | +let cachedConfig: AppConfig | null = null |
| 30 | + |
| 31 | +function ensureConfigFile(): void { |
| 32 | + try { |
| 33 | + fs.accessSync(PATHS.CONFIG_PATH, fs.constants.R_OK | fs.constants.W_OK) |
| 34 | + } catch { |
| 35 | + fs.writeFileSync( |
| 36 | + PATHS.CONFIG_PATH, |
| 37 | + `${JSON.stringify(defaultConfig, null, 2)}\n`, |
| 38 | + "utf8", |
| 39 | + ) |
| 40 | + try { |
| 41 | + fs.chmodSync(PATHS.CONFIG_PATH, 0o600) |
| 42 | + } catch { |
| 43 | + return |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function readConfigFromDisk(): AppConfig { |
| 49 | + ensureConfigFile() |
| 50 | + try { |
| 51 | + const raw = fs.readFileSync(PATHS.CONFIG_PATH, "utf8") |
| 52 | + if (!raw.trim()) { |
| 53 | + fs.writeFileSync( |
| 54 | + PATHS.CONFIG_PATH, |
| 55 | + `${JSON.stringify(defaultConfig, null, 2)}\n`, |
| 56 | + "utf8", |
| 57 | + ) |
| 58 | + return defaultConfig |
| 59 | + } |
| 60 | + const parsed = JSON.parse(raw) as AppConfig |
| 61 | + return parsed |
| 62 | + } catch { |
| 63 | + fs.writeFileSync( |
| 64 | + PATHS.CONFIG_PATH, |
| 65 | + `${JSON.stringify(defaultConfig, null, 2)}\n`, |
| 66 | + "utf8", |
| 67 | + ) |
| 68 | + return defaultConfig |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export function getConfig(): AppConfig { |
| 73 | + if (!cachedConfig) { |
| 74 | + cachedConfig = readConfigFromDisk() |
| 75 | + } |
| 76 | + return cachedConfig |
| 77 | +} |
| 78 | + |
| 79 | +export function reloadConfig(): AppConfig { |
| 80 | + cachedConfig = null |
| 81 | + return getConfig() |
| 82 | +} |
| 83 | + |
| 84 | +export function getExtraPrompt(): string { |
| 85 | + return getConfig().extraPrompt |
| 86 | +} |
0 commit comments