import * as fs from "node:fs"; import * as path from "node:path"; import { getAgentDir } from "@earendil-works/pi-coding-agent"; import { COMMENT_PREFIXES_BY_EXTENSION, DEFAULT_DEBOUNCE_MS, DEFAULT_EXCLUDE, DEFAULT_INCLUDE, DEFAULT_MAX_FILE_BYTES, DEFAULT_MAX_WATCH_DIRS, DEFAULT_TIMEOUT_MS, } from "./constants"; import { isObject, positiveInt, stringArray } from "./util"; export type ThinkingLevel = | "off" | "minimal" | "low" | "medium" | "high" | "xhigh"; export type NotifyPolicy = "off" | "errors" | "done" | "all"; export interface LlmiterateConfig { enabled: boolean; include: string[]; exclude: string[]; debounceMs: number; maxFileBytes: number; timeoutMs: number; maxWatchDirs: number; commentPrefixes: Record; model?: string; thinking?: ThinkingLevel; notify: NotifyPolicy; } export function defaultConfig(): LlmiterateConfig { return { enabled: true, include: [...DEFAULT_INCLUDE], exclude: [...DEFAULT_EXCLUDE], debounceMs: DEFAULT_DEBOUNCE_MS, maxFileBytes: DEFAULT_MAX_FILE_BYTES, timeoutMs: DEFAULT_TIMEOUT_MS, maxWatchDirs: DEFAULT_MAX_WATCH_DIRS, commentPrefixes: clonePrefixMap(COMMENT_PREFIXES_BY_EXTENSION), notify: "errors", }; } export function normalizeConfig(raw: unknown): LlmiterateConfig { const fallback = defaultConfig(); if (!isObject(raw)) return fallback; return { enabled: typeof raw.enabled === "boolean" ? raw.enabled : fallback.enabled, include: stringArray(raw.include, fallback.include), exclude: mergeExclude(raw.exclude), debounceMs: positiveInt(raw.debounceMs, fallback.debounceMs), maxFileBytes: positiveInt(raw.maxFileBytes, fallback.maxFileBytes), timeoutMs: positiveInt(raw.timeoutMs, fallback.timeoutMs), maxWatchDirs: positiveInt(raw.maxWatchDirs, fallback.maxWatchDirs), commentPrefixes: mergeCommentPrefixes(raw.commentPrefixes), model: optionalString(raw.model), thinking: thinkingLevel(raw.thinking), notify: notifyPolicy(raw.notify, fallback.notify), }; } export function loadConfig(cwd: string): LlmiterateConfig { const global = readSettingsNamespace( path.join(getAgentDir(), "settings.json"), ); const project = readSettingsNamespace(path.join(cwd, ".pi", "settings.json")); return normalizeConfig({ ...global, ...project }); } function readSettingsNamespace(settingsPath: string): Record { try { const parsed = JSON.parse(fs.readFileSync(settingsPath, "utf-8")); return isObject(parsed) && isObject(parsed.llmiterate) ? parsed.llmiterate : {}; } catch { return {}; } } function optionalString(value: unknown): string | undefined { return typeof value === "string" && value.trim() ? value.trim() : undefined; } function thinkingLevel(value: unknown): ThinkingLevel | undefined { return value === "off" || value === "minimal" || value === "low" || value === "medium" || value === "high" || value === "xhigh" ? value : undefined; } function notifyPolicy(value: unknown, fallback: NotifyPolicy): NotifyPolicy { return value === "off" || value === "errors" || value === "done" || value === "all" ? value : fallback; } function mergeExclude(value: unknown): string[] { return [...new Set([...DEFAULT_EXCLUDE, ...stringArray(value, [])])]; } function mergeCommentPrefixes(value: unknown): Record { const merged = clonePrefixMap(COMMENT_PREFIXES_BY_EXTENSION); if (!isObject(value)) return merged; for (const [rawKey, rawPrefixes] of Object.entries(value)) { const key = normalizeExtensionKey(rawKey); if (!key) continue; merged[key] = normalizeCommentPrefixes([ ...(merged[key] ?? []), ...stringArray(rawPrefixes, []), ]); } return merged; } function clonePrefixMap( value: Record, ): Record { return Object.fromEntries( Object.entries(value).map(([key, prefixes]) => [ key, normalizeCommentPrefixes(prefixes), ]), ); } function normalizeExtensionKey(key: string): string { return key.trim().toLowerCase().replace(/^\./, ""); } function normalizeCommentPrefixes(prefixes: string[]): string[] { return [...new Set(prefixes.map((p) => p.trim()).filter(Boolean))].sort( (a, b) => b.length - a.length, ); }