refactor: 移除消息流中的 diff 渲染,仅保留权限审批页的 diff

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-28 21:23:38 +08:00
parent 2bad8df5d7
commit 51b8ad46bf
4 changed files with 14 additions and 372 deletions

View File

@@ -1,7 +1,5 @@
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
import type { StructuredPatchHunk } from 'diff'
import * as React from 'react'
import { Suspense, use, useState } from 'react'
import { FileEditToolUseRejectedMessage } from 'src/components/FileEditToolUseRejectedMessage.js'
import { MessageResponse } from 'src/components/MessageResponse.js'
import { extractTag } from 'src/utils/messages.js'
@@ -12,19 +10,10 @@ import { Text } from '@anthropic/ink'
import { FilePathLink } from 'src/components/FilePathLink.js'
import type { Tools } from 'src/Tool.js'
import type { Message, ProgressMessage } from 'src/types/message.js'
import { adjustHunkLineNumbers, CONTEXT_LINES } from 'src/utils/diff.js'
import { FILE_NOT_FOUND_CWD_NOTE, getDisplayPath } from 'src/utils/file.js'
import { logError } from 'src/utils/log.js'
import { getPlansDirectory } from 'src/utils/plans.js'
import { readEditContext } from 'src/utils/readEditContext.js'
import { firstLineOf } from 'src/utils/stringUtils.js'
import type { ThemeName } from 'src/utils/theme.js'
import type { FileEditOutput } from './types.js'
import {
findActualString,
getPatchForEdit,
preserveQuoteStyle,
} from './utils.js'
export function userFacingName(
input:
@@ -99,8 +88,6 @@ export function renderToolResultMessage(
<FileEditToolUpdatedMessage
filePath={filePath}
structuredPatch={structuredPatch}
firstLine={originalFile.split('\n')[0] ?? null}
fileContent={originalFile}
style={style}
verbose={verbose}
previewHint={isPlanFile ? '/plan to preview' : undefined}
@@ -116,7 +103,7 @@ export function renderToolUseRejectedMessage(
replace_all?: boolean
edits?: unknown[]
},
options: {
_options: {
columns: number
messages: Message[]
progressMessagesForMessage: ProgressMessage[]
@@ -126,45 +113,14 @@ export function renderToolUseRejectedMessage(
verbose: boolean
},
): React.ReactElement {
const { style, verbose } = options
const { style, verbose } = _options
const filePath = input.file_path
const oldString = input.old_string ?? ''
const newString = input.new_string ?? ''
const replaceAll = input.replace_all ?? false
// Defensive: if input has an unexpected shape, show a simple rejection message
if ('edits' in input && input.edits != null) {
return (
<FileEditToolUseRejectedMessage
file_path={filePath}
operation="update"
firstLine={null}
verbose={verbose}
/>
)
}
const isNewFile = oldString === ''
// For new file creation, show content preview instead of diff
if (isNewFile) {
return (
<FileEditToolUseRejectedMessage
file_path={filePath}
operation="write"
content={newString}
firstLine={firstLineOf(newString)}
verbose={verbose}
/>
)
}
const isNewFile = input.old_string === ''
return (
<EditRejectionDiff
filePath={filePath}
oldString={oldString}
newString={newString}
replaceAll={replaceAll}
<FileEditToolUseRejectedMessage
file_path={filePath}
operation={isNewFile ? 'write' : 'update'}
style={style}
verbose={verbose}
/>
@@ -201,115 +157,3 @@ export function renderToolUseErrorMessage(
}
return <FallbackToolUseErrorMessage result={result} verbose={verbose} />
}
type RejectionDiffData = {
patch: StructuredPatchHunk[]
firstLine: string | null
fileContent: string | undefined
}
function EditRejectionDiff({
filePath,
oldString,
newString,
replaceAll,
style,
verbose,
}: {
filePath: string
oldString: string
newString: string
replaceAll: boolean
style?: 'condensed'
verbose: boolean
}): React.ReactNode {
const [dataPromise] = useState(() =>
loadRejectionDiff(filePath, oldString, newString, replaceAll),
)
return (
<Suspense
fallback={
<FileEditToolUseRejectedMessage
file_path={filePath}
operation="update"
firstLine={null}
verbose={verbose}
/>
}
>
<EditRejectionBody
promise={dataPromise}
filePath={filePath}
style={style}
verbose={verbose}
/>
</Suspense>
)
}
function EditRejectionBody({
promise,
filePath,
style,
verbose,
}: {
promise: Promise<RejectionDiffData>
filePath: string
style?: 'condensed'
verbose: boolean
}): React.ReactNode {
const { patch, firstLine, fileContent } = use(promise)
return (
<FileEditToolUseRejectedMessage
file_path={filePath}
operation="update"
patch={patch}
firstLine={firstLine}
fileContent={fileContent}
style={style}
verbose={verbose}
/>
)
}
async function loadRejectionDiff(
filePath: string,
oldString: string,
newString: string,
replaceAll: boolean,
): Promise<RejectionDiffData> {
try {
// Chunked read — context window around the first occurrence. replaceAll
// still shows matches *within* the window via getPatchForEdit; we accept
// losing the all-occurrences view to keep the read bounded.
const ctx = await readEditContext(filePath, oldString, CONTEXT_LINES)
if (ctx === null || ctx.truncated || ctx.content === '') {
// ENOENT / not found / truncated — diff just the tool inputs.
const { patch } = getPatchForEdit({
filePath,
fileContents: oldString,
oldString,
newString,
})
return { patch, firstLine: null, fileContent: undefined }
}
const actualOld = findActualString(ctx.content, oldString) || oldString
const actualNew = preserveQuoteStyle(oldString, actualOld, newString)
const { patch } = getPatchForEdit({
filePath,
fileContents: ctx.content,
oldString: actualOld,
newString: actualNew,
replaceAll,
})
return {
patch: adjustHunkLineNumbers(patch, ctx.lineOffset - 1),
firstLine: ctx.lineOffset === 1 ? firstLineOf(ctx.content) : null,
fileContent: ctx.content,
}
} catch (e) {
// User may have manually applied the change while the diff was shown.
logError(e as Error)
return { patch: [], firstLine: null, fileContent: undefined }
}
}

View File

@@ -1,8 +1,6 @@
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
import type { StructuredPatchHunk } from 'diff'
import { isAbsolute, relative, resolve } from 'path'
import { relative } from 'path'
import * as React from 'react'
import { Suspense, use, useState } from 'react'
import { MessageResponse } from 'src/components/MessageResponse.js'
import { extractTag } from 'src/utils/messages.js'
import { CtrlOToExpand } from 'src/components/CtrlOToExpand.js'
@@ -17,11 +15,8 @@ import { FilePathLink } from 'src/components/FilePathLink.js'
import type { ToolProgressData } from 'src/Tool.js'
import type { ProgressMessage } from 'src/types/message.js'
import { getCwd } from 'src/utils/cwd.js'
import { getPatchForDisplay } from 'src/utils/diff.js'
import { getDisplayPath } from 'src/utils/file.js'
import { logError } from 'src/utils/log.js'
import { getPlansDirectory } from 'src/utils/plans.js'
import { openForScan, readCapped } from 'src/utils/readEditContext.js'
import type { Output } from './FileWriteTool.js'
const MAX_LINES_TO_RENDER = 10
@@ -137,131 +132,19 @@ export function renderToolUseMessage(
}
export function renderToolUseRejectedMessage(
{ file_path, content }: { file_path: string; content: string },
{ file_path }: { file_path: string; content: string },
{ style, verbose }: { style?: 'condensed'; verbose: boolean },
): React.ReactNode {
return (
<WriteRejectionDiff
filePath={file_path}
content={content}
style={style}
verbose={verbose}
/>
)
}
type RejectionDiffData =
| { type: 'create' }
| { type: 'update'; patch: StructuredPatchHunk[]; oldContent: string }
| { type: 'error' }
function WriteRejectionDiff({
filePath,
content,
style,
verbose,
}: {
filePath: string
content: string
style?: 'condensed'
verbose: boolean
}): React.ReactNode {
const [dataPromise] = useState(() => loadRejectionDiff(filePath, content))
const firstLine = content.split('\n')[0] ?? null
const createFallback = (
<FileEditToolUseRejectedMessage
file_path={filePath}
file_path={file_path}
operation="write"
content={content}
firstLine={firstLine}
verbose={verbose}
/>
)
return (
<Suspense fallback={createFallback}>
<WriteRejectionBody
promise={dataPromise}
filePath={filePath}
firstLine={firstLine}
createFallback={createFallback}
style={style}
verbose={verbose}
/>
</Suspense>
)
}
function WriteRejectionBody({
promise,
filePath,
firstLine,
createFallback,
style,
verbose,
}: {
promise: Promise<RejectionDiffData>
filePath: string
firstLine: string | null
createFallback: React.ReactNode
style?: 'condensed'
verbose: boolean
}): React.ReactNode {
const data = use(promise)
if (data.type === 'create') return createFallback
if (data.type === 'error') {
return (
<MessageResponse>
<Text>(No changes)</Text>
</MessageResponse>
)
}
return (
<FileEditToolUseRejectedMessage
file_path={filePath}
operation="update"
patch={data.patch}
firstLine={firstLine}
fileContent={data.oldContent}
style={style}
verbose={verbose}
/>
)
}
async function loadRejectionDiff(
filePath: string,
content: string,
): Promise<RejectionDiffData> {
try {
const fullFilePath = isAbsolute(filePath)
? filePath
: resolve(getCwd(), filePath)
const handle = await openForScan(fullFilePath)
if (handle === null) return { type: 'create' }
let oldContent: string | null
try {
oldContent = await readCapped(handle)
} finally {
await handle.close()
}
// File exceeds MAX_SCAN_BYTES — fall back to the create view rather than
// OOMing on a diff of a multi-GB file.
if (oldContent === null) return { type: 'create' }
const patch = getPatchForDisplay({
filePath,
fileContents: oldContent,
edits: [
{ old_string: oldContent, new_string: content, replace_all: false },
],
})
return { type: 'update', patch, oldContent }
} catch (e) {
// User may have manually applied the change while the diff was shown.
logError(e as Error)
return { type: 'error' }
}
}
export function renderToolUseErrorMessage(
result: ToolResultBlockParam['content'],
{ verbose }: { verbose: boolean },
@@ -324,8 +207,6 @@ export function renderToolResultMessage(
<FileEditToolUpdatedMessage
filePath={filePath}
structuredPatch={structuredPatch}
firstLine={content.split('\n')[0] ?? null}
fileContent={originalFile ?? undefined}
style={style}
verbose={verbose}
previewHint={isPlanFile ? '/plan to preview' : undefined}