feat: avoid duplicate project-map hint injection by checking context
The before_agent_start handler now inspects the current session context and skips injection if a pi-project-map-hint custom message is already present in the active branch. This prevents duplicate hints on every prompt while still re-injecting after compaction or tree navigation. Also removes the unused hooks/on-prompt.ts prompt-text injector.
This commit is contained in:
+24
-2
@@ -50,6 +50,25 @@ function isDirty(content: string): boolean {
|
||||
return content.includes("## dirty") && !content.includes("## dirty\n-");
|
||||
}
|
||||
|
||||
const HINT_CUSTOM_TYPE = "pi-project-map-hint";
|
||||
|
||||
function hintAlreadyInContext(ctx: any): boolean {
|
||||
const manager = ctx?.sessionManager;
|
||||
if (!manager || typeof manager.buildSessionContext !== "function") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const { messages } = manager.buildSessionContext();
|
||||
if (!Array.isArray(messages)) return false;
|
||||
|
||||
return messages.some(
|
||||
(m: any) =>
|
||||
m &&
|
||||
m.role === "custom" &&
|
||||
m.customType === HINT_CUSTOM_TYPE,
|
||||
);
|
||||
}
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
pi.registerTool({
|
||||
name: "project_map_init",
|
||||
@@ -236,14 +255,17 @@ export default function (pi: ExtensionAPI) {
|
||||
}
|
||||
});
|
||||
|
||||
// Inject maintenance instructions before agent starts
|
||||
// Inject maintenance instructions before agent starts, but only once
|
||||
// within the current branch of context. Re-inject after compaction or
|
||||
// tree navigation removes the hint from the active path.
|
||||
pi.on("before_agent_start", async (_event, _ctx) => {
|
||||
const mapFiles = findPiMapFiles(_ctx.cwd);
|
||||
if (mapFiles.length === 0) return {};
|
||||
if (hintAlreadyInContext(_ctx)) return {};
|
||||
|
||||
return {
|
||||
message: {
|
||||
customType: "pi-project-map-hint",
|
||||
customType: HINT_CUSTOM_TYPE,
|
||||
content:
|
||||
"📋 Project map active: If you modify any source file, run `project_map_patch` with the file path. If you suspect staleness, run `project_map_validate`.",
|
||||
display: false,
|
||||
|
||||
Reference in New Issue
Block a user