Files
UHD_Media_Tool/spikes/pi/compaction.ts
T

22 lines
1.9 KiB
TypeScript

// Manual + threshold compaction with the fake model (summary text is scripted).
import { createModels, fauxProvider, fauxAssistantMessage } from "@earendil-works/pi-ai";
import { AgentHarness, BACKGROUND_CONTEXT as ctx, MemorySessionRepo } from "@earendil-works/pi-agent-core";
const faux = fauxProvider({ provider: "umt-fake", models: [{ id: "tiny", contextWindow: 3000, maxTokens: 500 }] });
const models = createModels(); models.setProvider(faux.provider);
const session = await new MemorySessionRepo().create({}, ctx);
const { harness } = await AgentHarness.create({ session, models, model: faux.getModel(), systemPrompt: "助手",
compaction: { enabled: true, reserveTokens: 600, keepRecentTokens: 200 } }, ctx);
for (const t of ["compaction_start", "compaction_end", "run_end"] as const)
harness.events.on(t as any, (e: any) => console.log(`[${e.type}] reason=${e.reason ?? ""} status=${e.status ?? ""}`));
const lane = await harness.lane("main", ctx);
const long = "目标:收录 Dune 1080p。约束:只用已授权能力。".repeat(Number(process.env.REPEAT ?? 60));
faux.setResponses([fauxAssistantMessage("好的,已记录目标。" + "x".repeat(2000)), fauxAssistantMessage("## 摘要\n目标:收录 Dune 1080p;已完成:无"), fauxAssistantMessage("继续")]);
console.log("run1:", JSON.stringify((await lane.prompt(long, undefined, ctx)) as any).slice(0, 400));
console.log("run2:", JSON.stringify((await lane.prompt("继续", undefined, ctx)) as any).slice(0, 400));
faux.appendResponses([fauxAssistantMessage("## 手动摘要\n目标/约束/证据/已完成/剩余")]);
const c = await lane.compact({ customInstructions: "保留目标、约束、证据引用、已完成动作、剩余事项" }, ctx);
console.log("manual compact:", JSON.stringify(c).slice(0, 200));
const entries = await lane.findEntries(undefined, ctx);
console.log("entry types:", entries.map((e: any) => e.type).join(","), "fake calls:", faux.state.callCount);
await harness.close(ctx);