|
| 1 | +import { exec, spawn } from "child_process"; |
| 2 | + |
| 3 | +import { getCurrentSession, getSessionFilePath } from "../session.js"; |
| 4 | +import { logger } from "../util/logger.js"; |
| 5 | +import { BaseService } from "./BaseService.js"; |
| 6 | +import { services } from "./index.js"; |
| 7 | + |
| 8 | +interface GitAiHookInput { |
| 9 | + session_id: string; |
| 10 | + transcript_path: string; |
| 11 | + cwd: string; |
| 12 | + model?: string; |
| 13 | + hook_event_name: "PreToolUse" | "PostToolUse"; |
| 14 | + tool_input: { |
| 15 | + file_path: string; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +export interface GitAiIntegrationServiceState { |
| 20 | + isEnabled: boolean; |
| 21 | + isGitAiAvailable: boolean | null; // null = not checked yet |
| 22 | +} |
| 23 | + |
| 24 | +export class GitAiIntegrationService extends BaseService<GitAiIntegrationServiceState> { |
| 25 | + constructor() { |
| 26 | + super("GitAiIntegrationService", { |
| 27 | + isEnabled: true, |
| 28 | + isGitAiAvailable: null, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + async doInitialize(): Promise<GitAiIntegrationServiceState> { |
| 33 | + // Check if git-ai is available on first initialization |
| 34 | + const isAvailable = await this.checkGitAiAvailable(); |
| 35 | + return { |
| 36 | + isEnabled: true, |
| 37 | + isGitAiAvailable: isAvailable, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + private async checkGitAiAvailable(): Promise<boolean> { |
| 42 | + return new Promise((resolve) => { |
| 43 | + try { |
| 44 | + exec("git-ai --version", (error) => { |
| 45 | + if (error) { |
| 46 | + resolve(false); |
| 47 | + return; |
| 48 | + } |
| 49 | + resolve(true); |
| 50 | + }); |
| 51 | + } catch { |
| 52 | + // Handle edge case where exec throws synchronously |
| 53 | + resolve(false); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Helper function to call git-ai checkpoint with the given hook input |
| 60 | + */ |
| 61 | + private async callGitAiCheckpoint( |
| 62 | + hookInput: GitAiHookInput, |
| 63 | + workspaceDirectory: string, |
| 64 | + ): Promise<void> { |
| 65 | + const hookInputJson = JSON.stringify(hookInput); |
| 66 | + |
| 67 | + logger.debug("Calling git-ai checkpoint", { |
| 68 | + hookInput, |
| 69 | + workspaceDirectory, |
| 70 | + }); |
| 71 | + |
| 72 | + await new Promise<void>((resolve, reject) => { |
| 73 | + const gitAiProcess = spawn( |
| 74 | + "git-ai", |
| 75 | + ["checkpoint", "continue-cli", "--hook-input", "stdin"], |
| 76 | + { cwd: workspaceDirectory }, |
| 77 | + ); |
| 78 | + |
| 79 | + let stdout = ""; |
| 80 | + let stderr = ""; |
| 81 | + |
| 82 | + gitAiProcess.stdout?.on("data", (data: Buffer) => { |
| 83 | + stdout += data.toString(); |
| 84 | + }); |
| 85 | + |
| 86 | + gitAiProcess.stderr?.on("data", (data: Buffer) => { |
| 87 | + stderr += data.toString(); |
| 88 | + }); |
| 89 | + |
| 90 | + gitAiProcess.on("error", (error: Error) => { |
| 91 | + reject(error); |
| 92 | + }); |
| 93 | + |
| 94 | + gitAiProcess.on("close", (code: number | null) => { |
| 95 | + if (code === 0) { |
| 96 | + logger.debug("git-ai checkpoint completed", { stdout, stderr }); |
| 97 | + resolve(); |
| 98 | + } else { |
| 99 | + reject( |
| 100 | + new Error(`git-ai checkpoint exited with code ${code}: ${stderr}`), |
| 101 | + ); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + // Write JSON to stdin and close |
| 106 | + gitAiProcess.stdin?.write(hookInputJson); |
| 107 | + gitAiProcess.stdin?.end(); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + async beforeFileEdit(filePath: string): Promise<void> { |
| 112 | + if (!this.currentState.isEnabled) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // Skip if git-ai is not available |
| 117 | + if (this.currentState.isGitAiAvailable === false) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + const session = getCurrentSession(); |
| 123 | + const sessionFilePath = getSessionFilePath(); |
| 124 | + |
| 125 | + // Get current model from ModelService |
| 126 | + const modelState = services.model.getState(); |
| 127 | + console.log("modelState", modelState); |
| 128 | + const modelName = modelState?.model?.model; |
| 129 | + |
| 130 | + const hookInput: GitAiHookInput = { |
| 131 | + session_id: session.sessionId, |
| 132 | + transcript_path: sessionFilePath, |
| 133 | + cwd: session.workspaceDirectory, |
| 134 | + hook_event_name: "PreToolUse", |
| 135 | + tool_input: { |
| 136 | + file_path: filePath, |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + // Only include model if it's available |
| 141 | + if (modelName) { |
| 142 | + hookInput.model = modelName; |
| 143 | + } |
| 144 | + |
| 145 | + await this.callGitAiCheckpoint(hookInput, session.workspaceDirectory); |
| 146 | + } catch (error) { |
| 147 | + logger.warn("git-ai checkpoint (pre-edit) failed", { error, filePath }); |
| 148 | + // Mark as unavailable if command fails |
| 149 | + this.setState({ isGitAiAvailable: false }); |
| 150 | + // Don't throw - allow file edit to proceed |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + async afterFileEdit(filePath: string): Promise<void> { |
| 155 | + if (!this.currentState.isEnabled) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + // Skip if git-ai is not available |
| 160 | + if (this.currentState.isGitAiAvailable === false) { |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + try { |
| 165 | + const session = getCurrentSession(); |
| 166 | + const sessionFilePath = getSessionFilePath(); |
| 167 | + |
| 168 | + // Get current model from ModelService |
| 169 | + const modelState = services.model.getState(); |
| 170 | + console.log("modelState", modelState); |
| 171 | + const modelName = modelState?.model?.model; |
| 172 | + |
| 173 | + const hookInput: GitAiHookInput = { |
| 174 | + session_id: session.sessionId, |
| 175 | + transcript_path: sessionFilePath, |
| 176 | + cwd: session.workspaceDirectory, |
| 177 | + hook_event_name: "PostToolUse", |
| 178 | + tool_input: { |
| 179 | + file_path: filePath, |
| 180 | + }, |
| 181 | + }; |
| 182 | + |
| 183 | + // Only include model if it's available |
| 184 | + if (modelName) { |
| 185 | + hookInput.model = modelName; |
| 186 | + } |
| 187 | + |
| 188 | + await this.callGitAiCheckpoint(hookInput, session.workspaceDirectory); |
| 189 | + } catch (error) { |
| 190 | + logger.warn("git-ai checkpoint (post-edit) failed", { error, filePath }); |
| 191 | + // Mark as unavailable if command fails |
| 192 | + this.setState({ isGitAiAvailable: false }); |
| 193 | + // Don't throw - file edit already completed |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + setEnabled(enabled: boolean): void { |
| 198 | + this.setState({ isEnabled: enabled }); |
| 199 | + } |
| 200 | +} |
0 commit comments