feat: add pi-condense with default enablement

This commit is contained in:
云服务部-叶林立
2026-08-19 15:06:40 +08:00
parent 410c50a3e5
commit 6ce932df60
7 changed files with 134 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
export type CondenseDefaultResult = "updated" | "unchanged" | "skipped-invalid";
function isObject(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
/** Enable pi-condense only when the user has not already chosen an enabled state. */
export function ensureCondenseEnabledDefault(agentDir: string): CondenseDefaultResult {
const targetPath = join(agentDir, "settings.json");
let settings: Record<string, unknown> = {};
try {
const parsed: unknown = JSON.parse(readFileSync(targetPath, "utf8"));
if (!isObject(parsed)) return "skipped-invalid";
settings = parsed;
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") return "skipped-invalid";
}
const existing = settings.contextPrune;
if (existing !== undefined && !isObject(existing)) return "skipped-invalid";
if (isObject(existing) && Object.hasOwn(existing, "enabled")) return "unchanged";
settings.contextPrune = { ...(existing ?? {}), enabled: true };
mkdirSync(dirname(targetPath), { recursive: true });
const temporaryPath = `${targetPath}.my-pi.tmp`;
writeFileSync(temporaryPath, `${JSON.stringify(settings, null, 2)}\n`, "utf8");
renameSync(temporaryPath, targetPath);
return "updated";
}