initial git ai integration
This commit is contained in:
parent
16b12305cf
commit
7b2beb5f04
4 changed files with 253 additions and 2 deletions
200
extensions/cli/src/services/GitAiIntegrationService.ts
Normal file
200
extensions/cli/src/services/GitAiIntegrationService.ts
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
import { exec, spawn } from "child_process";
|
||||
|
||||
import { getCurrentSession, getSessionFilePath } from "../session.js";
|
||||
import { logger } from "../util/logger.js";
|
||||
import { BaseService } from "./BaseService.js";
|
||||
import { services } from "./index.js";
|
||||
|
||||
interface GitAiHookInput {
|
||||
session_id: string;
|
||||
transcript_path: string;
|
||||
cwd: string;
|
||||
model?: string;
|
||||
hook_event_name: "PreToolUse" | "PostToolUse";
|
||||
tool_input: {
|
||||
file_path: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GitAiIntegrationServiceState {
|
||||
isEnabled: boolean;
|
||||
isGitAiAvailable: boolean | null; // null = not checked yet
|
||||
}
|
||||
|
||||
export class GitAiIntegrationService extends BaseService<GitAiIntegrationServiceState> {
|
||||
constructor() {
|
||||
super("GitAiIntegrationService", {
|
||||
isEnabled: true,
|
||||
isGitAiAvailable: null,
|
||||
});
|
||||
}
|
||||
|
||||
async doInitialize(): Promise<GitAiIntegrationServiceState> {
|
||||
// Check if git-ai is available on first initialization
|
||||
const isAvailable = await this.checkGitAiAvailable();
|
||||
return {
|
||||
isEnabled: true,
|
||||
isGitAiAvailable: isAvailable,
|
||||
};
|
||||
}
|
||||
|
||||
private async checkGitAiAvailable(): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
exec("git-ai --version", (error) => {
|
||||
if (error) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
resolve(true);
|
||||
});
|
||||
} catch {
|
||||
// Handle edge case where exec throws synchronously
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to call git-ai checkpoint with the given hook input
|
||||
*/
|
||||
private async callGitAiCheckpoint(
|
||||
hookInput: GitAiHookInput,
|
||||
workspaceDirectory: string,
|
||||
): Promise<void> {
|
||||
const hookInputJson = JSON.stringify(hookInput);
|
||||
|
||||
logger.debug("Calling git-ai checkpoint", {
|
||||
hookInput,
|
||||
workspaceDirectory,
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const gitAiProcess = spawn(
|
||||
"git-ai",
|
||||
["checkpoint", "continue-cli", "--hook-input", "stdin"],
|
||||
{ cwd: workspaceDirectory },
|
||||
);
|
||||
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
|
||||
gitAiProcess.stdout?.on("data", (data: Buffer) => {
|
||||
stdout += data.toString();
|
||||
});
|
||||
|
||||
gitAiProcess.stderr?.on("data", (data: Buffer) => {
|
||||
stderr += data.toString();
|
||||
});
|
||||
|
||||
gitAiProcess.on("error", (error: Error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
gitAiProcess.on("close", (code: number | null) => {
|
||||
if (code === 0) {
|
||||
logger.debug("git-ai checkpoint completed", { stdout, stderr });
|
||||
resolve();
|
||||
} else {
|
||||
reject(
|
||||
new Error(`git-ai checkpoint exited with code ${code}: ${stderr}`),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Write JSON to stdin and close
|
||||
gitAiProcess.stdin?.write(hookInputJson);
|
||||
gitAiProcess.stdin?.end();
|
||||
});
|
||||
}
|
||||
|
||||
async beforeFileEdit(filePath: string): Promise<void> {
|
||||
if (!this.currentState.isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip if git-ai is not available
|
||||
if (this.currentState.isGitAiAvailable === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const session = getCurrentSession();
|
||||
const sessionFilePath = getSessionFilePath();
|
||||
|
||||
// Get current model from ModelService
|
||||
const modelState = services.model.getState();
|
||||
console.log("modelState", modelState);
|
||||
const modelName = modelState?.model?.model;
|
||||
|
||||
const hookInput: GitAiHookInput = {
|
||||
session_id: session.sessionId,
|
||||
transcript_path: sessionFilePath,
|
||||
cwd: session.workspaceDirectory,
|
||||
hook_event_name: "PreToolUse",
|
||||
tool_input: {
|
||||
file_path: filePath,
|
||||
},
|
||||
};
|
||||
|
||||
// Only include model if it's available
|
||||
if (modelName) {
|
||||
hookInput.model = modelName;
|
||||
}
|
||||
|
||||
await this.callGitAiCheckpoint(hookInput, session.workspaceDirectory);
|
||||
} catch (error) {
|
||||
logger.warn("git-ai checkpoint (pre-edit) failed", { error, filePath });
|
||||
// Mark as unavailable if command fails
|
||||
this.setState({ isGitAiAvailable: false });
|
||||
// Don't throw - allow file edit to proceed
|
||||
}
|
||||
}
|
||||
|
||||
async afterFileEdit(filePath: string): Promise<void> {
|
||||
if (!this.currentState.isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip if git-ai is not available
|
||||
if (this.currentState.isGitAiAvailable === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const session = getCurrentSession();
|
||||
const sessionFilePath = getSessionFilePath();
|
||||
|
||||
// Get current model from ModelService
|
||||
const modelState = services.model.getState();
|
||||
console.log("modelState", modelState);
|
||||
const modelName = modelState?.model?.model;
|
||||
|
||||
const hookInput: GitAiHookInput = {
|
||||
session_id: session.sessionId,
|
||||
transcript_path: sessionFilePath,
|
||||
cwd: session.workspaceDirectory,
|
||||
hook_event_name: "PostToolUse",
|
||||
tool_input: {
|
||||
file_path: filePath,
|
||||
},
|
||||
};
|
||||
|
||||
// Only include model if it's available
|
||||
if (modelName) {
|
||||
hookInput.model = modelName;
|
||||
}
|
||||
|
||||
await this.callGitAiCheckpoint(hookInput, session.workspaceDirectory);
|
||||
} catch (error) {
|
||||
logger.warn("git-ai checkpoint (post-edit) failed", { error, filePath });
|
||||
// Mark as unavailable if command fails
|
||||
this.setState({ isGitAiAvailable: false });
|
||||
// Don't throw - file edit already completed
|
||||
}
|
||||
}
|
||||
|
||||
setEnabled(enabled: boolean): void {
|
||||
this.setState({ isEnabled: enabled });
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import { AuthService } from "./AuthService.js";
|
|||
import { ChatHistoryService } from "./ChatHistoryService.js";
|
||||
import { ConfigService } from "./ConfigService.js";
|
||||
import { FileIndexService } from "./FileIndexService.js";
|
||||
import { GitAiIntegrationService } from "./GitAiIntegrationService.js";
|
||||
import { MCPService } from "./MCPService.js";
|
||||
import { ModelService } from "./ModelService.js";
|
||||
import { ResourceMonitoringService } from "./ResourceMonitoringService.js";
|
||||
|
|
@ -46,6 +47,7 @@ const agentFileService = new AgentFileService();
|
|||
const toolPermissionService = new ToolPermissionService();
|
||||
const systemMessageService = new SystemMessageService();
|
||||
const artifactUploadService = new ArtifactUploadService();
|
||||
const gitAiIntegrationService = new GitAiIntegrationService();
|
||||
|
||||
/**
|
||||
* Initialize all services and register them with the service container
|
||||
|
|
@ -310,6 +312,12 @@ export async function initializeServices(initOptions: ServiceInitOptions = {}) {
|
|||
[], // No dependencies for now, but could depend on SESSION in future
|
||||
);
|
||||
|
||||
serviceContainer.register(
|
||||
SERVICE_NAMES.GIT_AI_INTEGRATION,
|
||||
() => gitAiIntegrationService.initialize(),
|
||||
[], // No dependencies
|
||||
);
|
||||
|
||||
// Eagerly initialize all services to ensure they're ready when needed
|
||||
// This avoids race conditions and "service not ready" errors
|
||||
await serviceContainer.initializeAll();
|
||||
|
|
@ -372,11 +380,13 @@ export const services = {
|
|||
agentFile: agentFileService,
|
||||
toolPermissions: toolPermissionService,
|
||||
artifactUpload: artifactUploadService,
|
||||
gitAiIntegration: gitAiIntegrationService,
|
||||
} as const;
|
||||
|
||||
// Export the service container for advanced usage
|
||||
export { serviceContainer };
|
||||
|
||||
// Export service names and types
|
||||
export type * from "./types.js";
|
||||
export { SERVICE_NAMES } from "./types.js";
|
||||
export type * from "./types.js";
|
||||
export { SERVICE_NAMES } from "./types.js";
|
||||
|
||||
|
|
|
|||
|
|
@ -132,6 +132,7 @@ export interface ArtifactUploadServiceState {
|
|||
|
||||
export type { ChatHistoryState } from "./ChatHistoryService.js";
|
||||
export type { FileIndexServiceState } from "./FileIndexService.js";
|
||||
export type { GitAiIntegrationServiceState } from "./GitAiIntegrationService.js";
|
||||
|
||||
/**
|
||||
* Service names as constants to prevent typos
|
||||
|
|
@ -151,6 +152,7 @@ export const SERVICE_NAMES = {
|
|||
STORAGE_SYNC: "storageSync",
|
||||
AGENT_FILE: "agentFile",
|
||||
ARTIFACT_UPLOAD: "artifactUpload",
|
||||
GIT_AI_INTEGRATION: "gitAiIntegration",
|
||||
} as const;
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -196,10 +196,32 @@ export function convertMcpToolToContinueTool(mcpTool: MCPTool): Tool {
|
|||
};
|
||||
}
|
||||
|
||||
function extractFilePathFromToolCall(
|
||||
toolCall: PreprocessedToolCall,
|
||||
): string | null {
|
||||
const preprocessed = toolCall.preprocessResult;
|
||||
if (!preprocessed?.args) return null;
|
||||
|
||||
const args = preprocessed.args;
|
||||
|
||||
// Extract file path based on tool type
|
||||
if (toolCall.name === "Edit" && args.resolvedPath) {
|
||||
return args.resolvedPath;
|
||||
} else if (toolCall.name === "MultiEdit" && args.file_path) {
|
||||
return args.file_path;
|
||||
} else if (toolCall.name === "Write" && args.filepath) {
|
||||
return args.filepath;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function executeToolCall(
|
||||
toolCall: PreprocessedToolCall,
|
||||
): Promise<string> {
|
||||
const startTime = Date.now();
|
||||
const FILE_EDIT_TOOLS = ["Edit", "MultiEdit", "Write"];
|
||||
const isFileEdit = FILE_EDIT_TOOLS.includes(toolCall.name);
|
||||
|
||||
try {
|
||||
logger.debug("Executing tool", {
|
||||
|
|
@ -207,6 +229,14 @@ export async function executeToolCall(
|
|||
arguments: toolCall.arguments,
|
||||
});
|
||||
|
||||
// GIT-AI CHECKPOINT: Call before file editing (BLOCKING)
|
||||
if (isFileEdit) {
|
||||
const filePath = extractFilePathFromToolCall(toolCall);
|
||||
if (filePath) {
|
||||
await services.gitAiIntegration.beforeFileEdit(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
// IMPORTANT: if preprocessed args are present, uses preprocessed args instead of original args
|
||||
// Preprocessed arg names may be different
|
||||
const result = await toolCall.tool.run(
|
||||
|
|
@ -214,6 +244,15 @@ export async function executeToolCall(
|
|||
);
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
// GIT-AI CHECKPOINT: Call after file editing (NON-BLOCKING)
|
||||
if (isFileEdit) {
|
||||
const filePath = extractFilePathFromToolCall(toolCall);
|
||||
if (filePath) {
|
||||
// Don't await - run in background to avoid blocking
|
||||
void services.gitAiIntegration.afterFileEdit(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
telemetryService.logToolResult({
|
||||
toolName: toolCall.name,
|
||||
success: true,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue