Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | 1x 1x 1x 1x 1x 23x 1x 21x 21x 27x 27x 27x 12x 15x 15x 21x 1x 28x 28x 157x 20x 28x 13x 13x 13x 1x 1x 13x 3x 3x 13x 13x 28x 13x 13x 13x 1x 1x 13x 1x 1x 13x 1x 1x 13x 28x 14x 14x 14x 1x 1x 14x 5x 5x 14x 28x 15x 15x 15x 2x 2x 15x 3x 3x 15x 15x 28x 1x 1x 1x 1x 1x 28x 4x 4x 4x 4x 5x 2x 2x 4x 28x 18x 20x 28x 1x 22x 22x 22x 22x 22x 48x 48x 10x 10x 9x 1x 13x | /**
* config.ts
*
* Shared config loading for Pi extensions.
*
* Lives here (not in src/index.ts) so extensions can import it without
* creating a circular dependency — src/index.ts imports from extensions,
* so extensions must not import back from src/index.ts.
*/
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
// ─── Type ──────────────────────────────────────────────────────────────────
export interface PiscesConfig {
student?: {
name?: string;
year_of_study?: number;
timezone?: string;
};
explanations?: {
default_depth?: "beginner" | "intermediate" | "advanced";
prefer_visuals?: boolean;
use_analogies?: boolean;
};
integrity?: {
enabled?: boolean;
strictness?: "strict" | "balanced" | "relaxed";
};
productivity?: {
burnout_nudges?: boolean;
session_warning_minutes?: number;
weekly_summary?: boolean;
};
model?: {
default?: string;
quick?: string;
};
workspace?: {
customPaths?: string[];
};
}
// ─── Helpers ──────────────────────────────────────────────────────────────
// Resolves from src/extensions/lib/ (dev) and dist/extensions/lib/ (compiled),
// both of which are three levels below the package root.
const DEFAULTS_PATH = path.join(__dirname, "../../../config/defaults.json");
export function getConfigSearchPaths(): string[] {
return [
path.join(process.cwd(), ".pisces.json"),
path.join(os.homedir(), ".pi", "pisces.json"),
path.join(os.homedir(), ".config", "pisces", "config.json"),
];
}
export function deepMerge<T extends object>(base: T, override: Partial<T>): T {
const result = { ...base };
for (const key of Object.keys(override) as (keyof T)[]) {
const baseVal = base[key];
const overrideVal = override[key];
if (
overrideVal !== null &&
typeof overrideVal === "object" &&
!Array.isArray(overrideVal) &&
typeof baseVal === "object" &&
baseVal !== null
) {
result[key] = deepMerge(
baseVal as Record<string, unknown>,
overrideVal as Record<string, unknown>
) as T[keyof T];
} else if (overrideVal !== undefined) {
result[key] = overrideVal as T[keyof T];
}
}
return result;
}
// ─── Validator ─────────────────────────────────────────────────────────────
/**
* Validates a merged PiscesConfig against the schema constraints.
* Invalid fields are replaced by their default values.
* All violations are reported together via console.warn — never throws.
*/
export function validateConfig(config: PiscesConfig, defaults: PiscesConfig): PiscesConfig {
const issues: string[] = [];
const result: PiscesConfig = {};
// null = user explicitly cleared the field; skip validation, leave as-is.
function set<T>(val: T | null | undefined): val is NonNullable<T> {
return val !== undefined && val !== null;
}
function bad(field: string, expected: string): void {
issues.push(`${field}: expected ${expected}`);
}
// ── student ────────────────────────────────────────────────────────────
if (config.student !== undefined) {
Iif (typeof config.student !== "object" || Array.isArray(config.student)) {
bad("student", "an object");
result.student = defaults.student;
} else {
const s = { ...config.student };
if (set(s.name) && typeof s.name !== "string") {
bad("student.name", "a string");
s.name = defaults.student?.name;
}
if (set(s.year_of_study) &&
(!Number.isInteger(s.year_of_study) || s.year_of_study < 1 || s.year_of_study > 8)) {
bad("student.year_of_study", "an integer between 1 and 8");
s.year_of_study = defaults.student?.year_of_study;
}
Iif (set(s.timezone) && typeof s.timezone !== "string") {
bad("student.timezone", "a string");
s.timezone = defaults.student?.timezone;
}
result.student = s;
}
}
// ── explanations ───────────────────────────────────────────────────────
if (config.explanations !== undefined) {
Iif (typeof config.explanations !== "object" || Array.isArray(config.explanations)) {
bad("explanations", "an object");
result.explanations = defaults.explanations;
} else {
const e = { ...config.explanations };
if (set(e.default_depth) &&
!["beginner", "intermediate", "advanced"].includes(e.default_depth)) {
bad('explanations.default_depth', '"beginner", "intermediate", or "advanced"');
e.default_depth = defaults.explanations?.default_depth;
}
if (set(e.prefer_visuals) && typeof e.prefer_visuals !== "boolean") {
bad("explanations.prefer_visuals", "a boolean");
e.prefer_visuals = defaults.explanations?.prefer_visuals;
}
if (set(e.use_analogies) && typeof e.use_analogies !== "boolean") {
bad("explanations.use_analogies", "a boolean");
e.use_analogies = defaults.explanations?.use_analogies;
}
result.explanations = e;
}
}
// ── integrity ──────────────────────────────────────────────────────────
if (config.integrity !== undefined) {
Iif (typeof config.integrity !== "object" || Array.isArray(config.integrity)) {
bad("integrity", "an object");
result.integrity = defaults.integrity;
} else {
const i = { ...config.integrity };
if (set(i.enabled) && typeof i.enabled !== "boolean") {
bad("integrity.enabled", "a boolean");
i.enabled = defaults.integrity?.enabled;
}
if (set(i.strictness) &&
!["strict", "balanced", "relaxed"].includes(i.strictness)) {
bad('integrity.strictness', '"strict", "balanced", or "relaxed"');
i.strictness = defaults.integrity?.strictness;
}
result.integrity = i;
}
}
// ── productivity ───────────────────────────────────────────────────────
if (config.productivity !== undefined) {
Iif (typeof config.productivity !== "object" || Array.isArray(config.productivity)) {
bad("productivity", "an object");
result.productivity = defaults.productivity;
} else {
const p = { ...config.productivity };
if (set(p.burnout_nudges) && typeof p.burnout_nudges !== "boolean") {
bad("productivity.burnout_nudges", "a boolean");
p.burnout_nudges = defaults.productivity?.burnout_nudges;
}
if (set(p.session_warning_minutes) &&
(!Number.isInteger(p.session_warning_minutes) || p.session_warning_minutes < 30)) {
bad("productivity.session_warning_minutes", "an integer >= 30");
p.session_warning_minutes = defaults.productivity?.session_warning_minutes;
}
Iif (set(p.weekly_summary) && typeof p.weekly_summary !== "boolean") {
bad("productivity.weekly_summary", "a boolean");
p.weekly_summary = defaults.productivity?.weekly_summary;
}
result.productivity = p;
}
}
// ── model ──────────────────────────────────────────────────────────────
if (config.model !== undefined) {
Iif (typeof config.model !== "object" || Array.isArray(config.model)) {
bad("model", "an object");
result.model = defaults.model;
} else {
const m = { ...config.model };
Iif (set(m.default) && typeof m.default !== "string") {
bad("model.default", "a string");
m.default = defaults.model?.default;
}
Iif (set(m.quick) && typeof m.quick !== "string") {
bad("model.quick", "a string");
m.quick = defaults.model?.quick;
}
result.model = m;
}
}
// ── workspace ──────────────────────────────────────────────────────────
if (config.workspace !== undefined) {
Iif (typeof config.workspace !== "object" || Array.isArray(config.workspace)) {
bad("workspace", "an object");
result.workspace = defaults.workspace;
} else {
const w = { ...config.workspace };
if (set(w.customPaths)) {
if (!Array.isArray(w.customPaths) ||
!(w.customPaths as unknown[]).every((p) => typeof p === "string")) {
bad("workspace.customPaths", "an array of strings");
w.customPaths = defaults.workspace?.customPaths;
}
}
result.workspace = w;
}
}
if (issues.length > 0) {
console.warn(
`[Pisces] Config validation: ${issues.length} issue${issues.length === 1 ? "" : "s"} found` +
` — invalid values replaced with defaults:\n` +
issues.map((v) => ` • ${v}`).join("\n")
);
}
return result;
}
// ─── Loader ────────────────────────────────────────────────────────────────
/**
* Loads and merges user config over package defaults.
* Never throws — falls back to defaults on any read/parse error.
*/
export function loadConfig(): PiscesConfig {
let defaults: PiscesConfig = {};
try {
const raw = fs.readFileSync(DEFAULTS_PATH, "utf-8");
defaults = JSON.parse(raw) as PiscesConfig;
} catch {
defaults = {
explanations: { default_depth: "intermediate", prefer_visuals: true, use_analogies: true },
integrity: { enabled: true, strictness: "balanced" },
productivity: { burnout_nudges: true, session_warning_minutes: 180, weekly_summary: true },
};
}
for (const configPath of getConfigSearchPaths()) {
try {
if (fs.existsSync(configPath)) {
const raw = fs.readFileSync(configPath, "utf-8");
const userConfig = JSON.parse(raw) as PiscesConfig;
return validateConfig(deepMerge(defaults, userConfig), defaults);
}
} catch {
continue;
}
}
return defaults;
}
|