repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/klaus/src/subscription-prompt.ts
Rawimport { dbg } from "./debug.js";
import type { KlausRequest } from "./protocol.js";
const SUBSTITUTIONS = [
["asks about pi itself", "asks about this harness"],
["pi packages", "harness packages"],
] as const;
export function rewriteSubscriptionPrompt(
request: KlausRequest,
warn: (message: string) => void = () => undefined,
): KlausRequest {
let systemPrompt = request.systemPrompt;
const substitutions: Array<(typeof SUBSTITUTIONS)[number]> = [];
const missing: string[] = [];
for (const substitution of SUBSTITUTIONS) {
const [before, after] = substitution;
const occurrences = systemPrompt.split(before).length - 1;
if (occurrences > 1) {
throw new Error(`Klaus found repeated ${before}.`);
}
if (occurrences === 1) {
systemPrompt = systemPrompt.replace(before, after);
substitutions.push(substitution);
} else {
missing.push(before);
}
}
if (missing.length > 0) {
warn(
`Klaus did not find expected Pi system-prompt phrase${missing.length === 1 ? "" : "s"}: ${missing.map((phrase) => JSON.stringify(phrase)).join(", ")}. Subscription routing may fail after upstream prompt changes.`,
);
}
dbg?.("subscriptionPrompt.rewrite", { substitutions, missing });
return { ...request, systemPrompt };
}