Skip to content

Commit b5810ed

Browse files
committed
tidy git ai integration
1 parent 06fef97 commit b5810ed

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

extensions/cli/src/services/GitAiIntegrationService.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { exec, spawn } from "child_process";
33
import { getCurrentSession, getSessionFilePath } from "../session.js";
44
import { logger } from "../util/logger.js";
55

6+
import { PreprocessedToolCall } from "src/tools/types.js";
67
import { BaseService } from "./BaseService.js";
78
import { serviceContainer } from "./ServiceContainer.js";
89
import type { ModelServiceState } from "./types.js";
@@ -110,6 +111,25 @@ export class GitAiIntegrationService extends BaseService<GitAiIntegrationService
110111
});
111112
}
112113

114+
async trackToolUse(toolCall: PreprocessedToolCall, hookEventName: "PreToolUse" | "PostToolUse"): Promise<void> {
115+
if (!this.currentState.isEnabled) {
116+
return;
117+
}
118+
const isFileEdit = ["Edit", "MultiEdit", "Write"].includes(toolCall.name);
119+
if (!isFileEdit) {
120+
return;
121+
}
122+
123+
const filePath = this.extractFilePathFromToolCall(toolCall);
124+
if (filePath) {
125+
if (hookEventName === "PreToolUse") {
126+
await this.beforeFileEdit(filePath);
127+
} else if (hookEventName === "PostToolUse") {
128+
await this.afterFileEdit(filePath);
129+
}
130+
}
131+
}
132+
113133
async beforeFileEdit(filePath: string): Promise<void> {
114134
if (!this.currentState.isEnabled) {
115135
return;
@@ -199,4 +219,24 @@ export class GitAiIntegrationService extends BaseService<GitAiIntegrationService
199219
setEnabled(enabled: boolean): void {
200220
this.setState({ isEnabled: enabled });
201221
}
222+
223+
extractFilePathFromToolCall(
224+
toolCall: PreprocessedToolCall,
225+
): string | null {
226+
const preprocessed = toolCall.preprocessResult;
227+
if (!preprocessed?.args) return null;
228+
229+
const args = preprocessed.args;
230+
231+
// Extract file path based on tool type
232+
if (toolCall.name === "Edit" && args.resolvedPath) {
233+
return args.resolvedPath;
234+
} else if (toolCall.name === "MultiEdit" && args.file_path) {
235+
return args.file_path;
236+
} else if (toolCall.name === "Write" && args.filepath) {
237+
return args.filepath;
238+
}
239+
240+
return null;
241+
}
202242
}

extensions/cli/src/tools/index.tsx

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -189,46 +189,19 @@ export function convertMcpToolToContinueTool(mcpTool: MCPTool): Tool {
189189
};
190190
}
191191

192-
function extractFilePathFromToolCall(
193-
toolCall: PreprocessedToolCall,
194-
): string | null {
195-
const preprocessed = toolCall.preprocessResult;
196-
if (!preprocessed?.args) return null;
197-
198-
const args = preprocessed.args;
199-
200-
// Extract file path based on tool type
201-
if (toolCall.name === "Edit" && args.resolvedPath) {
202-
return args.resolvedPath;
203-
} else if (toolCall.name === "MultiEdit" && args.file_path) {
204-
return args.file_path;
205-
} else if (toolCall.name === "Write" && args.filepath) {
206-
return args.filepath;
207-
}
208-
209-
return null;
210-
}
211-
212192
export async function executeToolCall(
213193
toolCall: PreprocessedToolCall,
214194
): Promise<string> {
215195
const startTime = Date.now();
216-
const FILE_EDIT_TOOLS = ["Edit", "MultiEdit", "Write"];
217-
const isFileEdit = FILE_EDIT_TOOLS.includes(toolCall.name);
218196

219197
try {
220198
logger.debug("Executing tool", {
221199
toolName: toolCall.name,
222200
arguments: toolCall.arguments,
223201
});
224202

225-
// GIT-AI CHECKPOINT: Call before file editing (BLOCKING)
226-
if (isFileEdit) {
227-
const filePath = extractFilePathFromToolCall(toolCall);
228-
if (filePath) {
229-
await services.gitAiIntegration.beforeFileEdit(filePath);
230-
}
231-
}
203+
// Track edits if Git AI is enabled (no-op if not enabled)
204+
await services.gitAiIntegration.trackToolUse(toolCall, "PreToolUse");
232205

233206
// IMPORTANT: if preprocessed args are present, uses preprocessed args instead of original args
234207
// Preprocessed arg names may be different
@@ -237,14 +210,8 @@ export async function executeToolCall(
237210
);
238211
const duration = Date.now() - startTime;
239212

240-
// GIT-AI CHECKPOINT: Call after file editing (NON-BLOCKING)
241-
if (isFileEdit) {
242-
const filePath = extractFilePathFromToolCall(toolCall);
243-
if (filePath) {
244-
// Don't await - run in background to avoid blocking
245-
void services.gitAiIntegration.afterFileEdit(filePath);
246-
}
247-
}
213+
// Track edits if Git AI is enabled (no-op if not enabled)
214+
await services.gitAiIntegration.trackToolUse(toolCall, "PostToolUse");
248215

249216
telemetryService.logToolResult({
250217
toolName: toolCall.name,

0 commit comments

Comments
 (0)