feat(prompt): implement prompt injection slice 3
This commit is contained in:
@@ -13,6 +13,9 @@ import {
|
||||
modeAllowsInjection,
|
||||
discoverContextWindow,
|
||||
buildInjectionPayload,
|
||||
shouldReinjectForEvent,
|
||||
getRootPairMtimes,
|
||||
rootPairChanged,
|
||||
} from "./src/index.js";
|
||||
import { loadConfig } from "./src/config.js";
|
||||
import { createLLMClient } from "./src/llm/llm-client.js";
|
||||
@@ -76,6 +79,7 @@ function renderProgressBar(
|
||||
}
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
let lastRootPairMtimes: import("./src/index.js").RootPairMtimes = {};
|
||||
pi.registerTool({
|
||||
name: "project_map_init",
|
||||
label: "Project Map Init",
|
||||
@@ -373,6 +377,28 @@ export default function (pi: ExtensionAPI) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Slice 3b: detect root-pair artifact changes
|
||||
const currentMtimes = getRootPairMtimes(_ctx.cwd);
|
||||
const hasPrevious =
|
||||
lastRootPairMtimes.mapMtime !== undefined ||
|
||||
lastRootPairMtimes.indexMtime !== undefined;
|
||||
const artifactChanged =
|
||||
hasPrevious && rootPairChanged(currentMtimes, lastRootPairMtimes);
|
||||
lastRootPairMtimes = currentMtimes;
|
||||
|
||||
// Slice 3a/3b: avoid redundant reinjection by scanning outgoing context
|
||||
const eventType = artifactChanged ? "artifact_change" : "agent_start";
|
||||
const decision = shouldReinjectForEvent(
|
||||
{
|
||||
messages: _event?.messages,
|
||||
type: eventType,
|
||||
},
|
||||
config.promptInjectionMode,
|
||||
);
|
||||
if (!decision.needed) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Slice 2: post-init root-pair preload + budgeted expansion
|
||||
const contextWindow = discoverContextWindow(_ctx);
|
||||
const payload = buildInjectionPayload(_ctx.cwd, config, contextWindow);
|
||||
@@ -384,4 +410,51 @@ export default function (pi: ExtensionAPI) {
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// Per-turn context scanning for reinjection in strong/strict modes
|
||||
pi.on("context", async (event: any, ctx: any) => {
|
||||
const config = loadConfig(ctx.cwd);
|
||||
const mapFiles = findPiMapFiles(ctx.cwd);
|
||||
|
||||
if (mapFiles.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!modeAllowsInjection(config.promptInjectionMode)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Slice 3b: detect root-pair artifact changes
|
||||
const currentMtimes = getRootPairMtimes(ctx.cwd);
|
||||
const hasPrevious =
|
||||
lastRootPairMtimes.mapMtime !== undefined ||
|
||||
lastRootPairMtimes.indexMtime !== undefined;
|
||||
const artifactChanged =
|
||||
hasPrevious && rootPairChanged(currentMtimes, lastRootPairMtimes);
|
||||
lastRootPairMtimes = currentMtimes;
|
||||
|
||||
const eventType = artifactChanged ? "artifact_change" : event?.type;
|
||||
|
||||
const decision = shouldReinjectForEvent(
|
||||
{
|
||||
messages: event?.messages,
|
||||
type: eventType,
|
||||
payload: event?.payload,
|
||||
},
|
||||
config.promptInjectionMode,
|
||||
);
|
||||
if (!decision.needed) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const contextWindow = discoverContextWindow(ctx);
|
||||
const payload = buildInjectionPayload(ctx.cwd, config, contextWindow);
|
||||
return {
|
||||
message: {
|
||||
customType: "pi-project-map-hint",
|
||||
content: payload.content,
|
||||
display: payload.display,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user