repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/llmiterate/config.ts
RawFile outline
- ThinkingLevel line 15
- NotifyPolicy line 22
- LlmiterateConfig line 24
- enabled line 25
- include line 26
- exclude line 27
- debounceMs line 28
- maxFileBytes line 29
- timeoutMs line 30
- maxWatchDirs line 31
- commentPrefixes line 32
- model line 33
- thinking line 34
- notify line 35
- defaultConfig line 38
- normalizeConfig line 52
- loadConfig line 71
- readSettingsNamespace line 79
- optionalString line 90
- thinkingLevel line 94
- notifyPolicy line 105
- mergeExclude line 114
- mergeCommentPrefixes line 118
- clonePrefixMap line 133
- normalizeExtensionKey line 144
- normalizeCommentPrefixes line 148
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<string, string[]>;
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<string, unknown> {
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<string, string[]> {
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<string, string[]>,
): Record<string, string[]> {
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,
);
}