File size: 9,867 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | import { randomUUID } from 'crypto'
import { basename } from 'path'
import { useEffect, useMemo, useRef, useState } from 'react'
import { logEvent } from 'src/services/analytics/index.js'
import { readFileSync } from 'src/utils/fileRead.js'
import { expandPath } from 'src/utils/path.js'
import type { PermissionOption } from '../components/permissions/FilePermissionDialog/permissionOptions.js'
import type {
MCPServerConnection,
McpSSEIDEServerConfig,
McpWebSocketIDEServerConfig,
} from '../services/mcp/types.js'
import type { ToolUseContext } from '../Tool.js'
import type { FileEdit } from '../tools/FileEditTool/types.js'
import {
getEditsForPatch,
getPatchForEdits,
} from '../tools/FileEditTool/utils.js'
import { getGlobalConfig } from '../utils/config.js'
import { getPatchFromContents } from '../utils/diff.js'
import { isENOENT } from '../utils/errors.js'
import {
callIdeRpc,
getConnectedIdeClient,
getConnectedIdeName,
hasAccessToIDEExtensionDiffFeature,
} from '../utils/ide.js'
import { WindowsToWSLConverter } from '../utils/idePathConversion.js'
import { logError } from '../utils/log.js'
import { getPlatform } from '../utils/platform.js'
type Props = {
onChange(
option: PermissionOption,
input: {
file_path: string
edits: FileEdit[]
},
): void
toolUseContext: ToolUseContext
filePath: string
edits: FileEdit[]
editMode: 'single' | 'multiple'
}
export function useDiffInIDE({
onChange,
toolUseContext,
filePath,
edits,
editMode,
}: Props): {
closeTabInIDE: () => void
showingDiffInIDE: boolean
ideName: string
hasError: boolean
} {
const isUnmounted = useRef(false)
const [hasError, setHasError] = useState(false)
const sha = useMemo(() => randomUUID().slice(0, 6), [])
const tabName = useMemo(
() => `✻ [Claude Code] ${basename(filePath)} (${sha}) ⧉`,
[filePath, sha],
)
const shouldShowDiffInIDE =
hasAccessToIDEExtensionDiffFeature(toolUseContext.options.mcpClients) &&
getGlobalConfig().diffTool === 'auto' &&
// Diffs should only be for file edits.
// File writes may come through here but are not supported for diffs.
!filePath.endsWith('.ipynb')
const ideName =
getConnectedIdeName(toolUseContext.options.mcpClients) ?? 'IDE'
async function showDiff(): Promise<void> {
if (!shouldShowDiffInIDE) {
return
}
try {
logEvent('tengu_ext_will_show_diff', {})
const { oldContent, newContent } = await showDiffInIDE(
filePath,
edits,
toolUseContext,
tabName,
)
// Skip if component has been unmounted
if (isUnmounted.current) {
return
}
logEvent('tengu_ext_diff_accepted', {})
const newEdits = computeEditsFromContents(
filePath,
oldContent,
newContent,
editMode,
)
if (newEdits.length === 0) {
// No changes -- edit was rejected (eg. reverted)
logEvent('tengu_ext_diff_rejected', {})
// We close the tab here because 'no' no longer auto-closes
const ideClient = getConnectedIdeClient(
toolUseContext.options.mcpClients,
)
if (ideClient) {
// Close the tab in the IDE
await closeTabInIDE(tabName, ideClient)
}
onChange(
{ type: 'reject' },
{
file_path: filePath,
edits: edits,
},
)
return
}
// File was modified - edit was accepted
onChange(
{ type: 'accept-once' },
{
file_path: filePath,
edits: newEdits,
},
)
} catch (error) {
logError(error as Error)
setHasError(true)
}
}
useEffect(() => {
void showDiff()
// Set flag on unmount
return () => {
isUnmounted.current = true
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return {
closeTabInIDE() {
const ideClient = getConnectedIdeClient(toolUseContext.options.mcpClients)
if (!ideClient) {
return Promise.resolve()
}
return closeTabInIDE(tabName, ideClient)
},
showingDiffInIDE: shouldShowDiffInIDE && !hasError,
ideName: ideName,
hasError,
}
}
/**
* Re-computes the edits from the old and new contents. This is necessary
* to apply any edits the user may have made to the new contents.
*/
export function computeEditsFromContents(
filePath: string,
oldContent: string,
newContent: string,
editMode: 'single' | 'multiple',
): FileEdit[] {
// Use unformatted patches, otherwise the edits will be formatted.
const singleHunk = editMode === 'single'
const patch = getPatchFromContents({
filePath,
oldContent,
newContent,
singleHunk,
})
if (patch.length === 0) {
return []
}
// For single edit mode, verify we only got one hunk
if (singleHunk && patch.length > 1) {
logError(
new Error(
`Unexpected number of hunks: ${patch.length}. Expected 1 hunk.`,
),
)
}
// Re-compute the edits to match the patch
return getEditsForPatch(patch)
}
/**
* Done if:
*
* 1. Tab is closed in IDE
* 2. Tab is saved in IDE (we then close the tab)
* 3. User selected an option in IDE
* 4. User selected an option in terminal (or hit esc)
*
* Resolves with the new file content.
*
* TODO: Time out after 5 mins of inactivity?
* TODO: Update auto-approval UI when IDE exits
* TODO: Close the IDE tab when the approval prompt is unmounted
*/
async function showDiffInIDE(
file_path: string,
edits: FileEdit[],
toolUseContext: ToolUseContext,
tabName: string,
): Promise<{ oldContent: string; newContent: string }> {
let isCleanedUp = false
const oldFilePath = expandPath(file_path)
let oldContent = ''
try {
oldContent = readFileSync(oldFilePath)
} catch (e: unknown) {
if (!isENOENT(e)) {
throw e
}
}
async function cleanup() {
// Careful to avoid race conditions, since this
// function can be called from multiple places.
if (isCleanedUp) {
return
}
isCleanedUp = true
// Don't fail if this fails
try {
await closeTabInIDE(tabName, ideClient)
} catch (e) {
logError(e as Error)
}
process.off('beforeExit', cleanup)
toolUseContext.abortController.signal.removeEventListener('abort', cleanup)
}
// Cleanup if the user hits esc to cancel the tool call - or on exit
toolUseContext.abortController.signal.addEventListener('abort', cleanup)
process.on('beforeExit', cleanup)
// Open the diff in the IDE
const ideClient = getConnectedIdeClient(toolUseContext.options.mcpClients)
try {
const { updatedFile } = getPatchForEdits({
filePath: oldFilePath,
fileContents: oldContent,
edits,
})
if (!ideClient || ideClient.type !== 'connected') {
throw new Error('IDE client not available')
}
let ideOldPath = oldFilePath
// Only convert paths if we're in WSL and IDE is on Windows
const ideRunningInWindows =
(ideClient.config as McpSSEIDEServerConfig | McpWebSocketIDEServerConfig)
.ideRunningInWindows === true
if (
getPlatform() === 'wsl' &&
ideRunningInWindows &&
process.env.WSL_DISTRO_NAME
) {
const converter = new WindowsToWSLConverter(process.env.WSL_DISTRO_NAME)
ideOldPath = converter.toIDEPath(oldFilePath)
}
const rpcResult = await callIdeRpc(
'openDiff',
{
old_file_path: ideOldPath,
new_file_path: ideOldPath,
new_file_contents: updatedFile,
tab_name: tabName,
},
ideClient,
)
// Convert the raw RPC result to a ToolCallResponse format
const data = Array.isArray(rpcResult) ? rpcResult : [rpcResult]
// If the user saved the file then take the new contents and resolve with that.
if (isSaveMessage(data)) {
void cleanup()
return {
oldContent: oldContent,
newContent: data[1].text,
}
} else if (isClosedMessage(data)) {
void cleanup()
return {
oldContent: oldContent,
newContent: updatedFile,
}
} else if (isRejectedMessage(data)) {
void cleanup()
return {
oldContent: oldContent,
newContent: oldContent,
}
}
// Indicates that the tool call completed with none of the expected
// results. Did the user close the IDE?
throw new Error('Not accepted')
} catch (error) {
logError(error as Error)
void cleanup()
throw error
}
}
async function closeTabInIDE(
tabName: string,
ideClient?: MCPServerConnection | undefined,
): Promise<void> {
try {
if (!ideClient || ideClient.type !== 'connected') {
throw new Error('IDE client not available')
}
// Use direct RPC to close the tab
await callIdeRpc('close_tab', { tab_name: tabName }, ideClient)
} catch (error) {
logError(error as Error)
// Don't throw - this is a cleanup operation
}
}
function isClosedMessage(data: unknown): data is { text: 'TAB_CLOSED' } {
return (
Array.isArray(data) &&
typeof data[0] === 'object' &&
data[0] !== null &&
'type' in data[0] &&
data[0].type === 'text' &&
'text' in data[0] &&
data[0].text === 'TAB_CLOSED'
)
}
function isRejectedMessage(data: unknown): data is { text: 'DIFF_REJECTED' } {
return (
Array.isArray(data) &&
typeof data[0] === 'object' &&
data[0] !== null &&
'type' in data[0] &&
data[0].type === 'text' &&
'text' in data[0] &&
data[0].text === 'DIFF_REJECTED'
)
}
function isSaveMessage(
data: unknown,
): data is [{ text: 'FILE_SAVED' }, { text: string }] {
return (
Array.isArray(data) &&
data[0]?.type === 'text' &&
data[0].text === 'FILE_SAVED' &&
typeof data[1].text === 'string'
)
}
|