Skip to content

Commit a0d864e

Browse files
author
Spencer Hawins (mesa)
committed
Add support for edit postToolUse calls
1 parent a20ddad commit a0d864e

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

packages/cli/src/capture.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ async function processCopilotPayload(payload: CopilotPayload, event?: string): P
845845
model: ctx.model,
846846
conversationId,
847847
});
848-
const prompt = payload.initialPrompt;
848+
849849
if (payload.initialPrompt) {
850850
const contentHash = hashPromptContent(payload.initialPrompt);
851851
if (!promptExists(ctx.sessionId, contentHash)) {
@@ -872,17 +872,17 @@ async function processCopilotPayload(payload: CopilotPayload, event?: string): P
872872
// Parse toolArgs JSON string
873873
const filePath = toolArgs?.path;
874874
if (filePath) {
875-
// TODO: get a real conversation ID ?
876-
const conversationId = `copilot-pretooluse-${Date.now()}`;
877-
const ctx = await setupCaptureContext(pathForRepo, copilotModel, conversationId, copilotModel);
878-
if (!ctx) return;
879-
880-
await detectAndRecordHumanEdits(ctx, conversationId, filePath);
881-
await captureFileCheckpoint(ctx.repoRoot, conversationId, filePath);
882-
if (process.env.AGENTBLAME_DEBUG) {
883-
console.error(`[agentblame] copilot preToolUse success: ${filePath}`);
884-
}
875+
// TODO: get a real conversation ID ?
876+
const conversationId = `copilot-pretooluse-${Date.now()}`;
877+
const ctx = await setupCaptureContext(pathForRepo, copilotModel, conversationId, copilotModel);
878+
if (!ctx) return;
879+
880+
await detectAndRecordHumanEdits(ctx, conversationId, filePath);
881+
await captureFileCheckpoint(ctx.repoRoot, conversationId, filePath);
882+
if (process.env.AGENTBLAME_DEBUG) {
883+
console.error(`[agentblame] copilot preToolUse success: ${filePath}`);
885884
}
885+
}
886886
}
887887
// TODO: support multi-file edits? (Will copilot send a multi-edit, or multiple edit events?)
888888

@@ -950,6 +950,28 @@ async function processCopilotPayload(payload: CopilotPayload, event?: string): P
950950
await recordAIDelta(ctx, filePath, "", afterCreateContent);
951951
}
952952
}
953+
954+
// Handle edit tool (editing a file)
955+
if (toolName === "edit") {
956+
const conversationId = `copilot-posttooluse-${Date.now()}`;
957+
const beforeContent = await getBeforeContent(ctx, conversationId, filePath);
958+
const afterContent = readFileContent(absolutePath);
959+
960+
if (process.env.AGENTBLAME_DEBUG) {
961+
console.error(`[agentblame] PostToolUse ${toolName}: ${filePath}`);
962+
console.error(`[agentblame] beforeContent: ${beforeContent ? beforeContent.length + ' chars' : 'null'}`);
963+
console.error(`[agentblame] afterContent: ${afterContent ? afterContent.length + ' chars' : 'null'}`);
964+
}
965+
966+
if (afterContent) {
967+
await recordAIDelta(ctx, filePath, beforeContent, afterContent);
968+
// Update checkpoint to current state so next PreToolUse doesn't
969+
// incorrectly detect this AI edit as a "human edit"
970+
await captureFileCheckpoint(ctx.repoRoot, conversationId, filePath);
971+
} else if (process.env.AGENTBLAME_DEBUG) {
972+
console.error(`[agentblame] SKIPPED: no afterContent for ${filePath}`);
973+
}
974+
}
953975
}
954976

955977
// =============================================================================

0 commit comments

Comments
 (0)