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 { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
import type { StructuredPatchHunk } from 'diff'
import * as React from 'react' import * as React from 'react'
import { Suspense, use, useState } from 'react'
import { FileEditToolUseRejectedMessage } from 'src/components/FileEditToolUseRejectedMessage.js' import { FileEditToolUseRejectedMessage } from 'src/components/FileEditToolUseRejectedMessage.js'
import { MessageResponse } from 'src/components/MessageResponse.js' import { MessageResponse } from 'src/components/MessageResponse.js'
import { extractTag } from 'src/utils/messages.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 { FilePathLink } from 'src/components/FilePathLink.js'
import type { Tools } from 'src/Tool.js' import type { Tools } from 'src/Tool.js'
import type { Message, ProgressMessage } from 'src/types/message.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 { 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 { 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 { ThemeName } from 'src/utils/theme.js'
import type { FileEditOutput } from './types.js' import type { FileEditOutput } from './types.js'
import {
findActualString,
getPatchForEdit,
preserveQuoteStyle,
} from './utils.js'
export function userFacingName( export function userFacingName(
input: input:
@@ -99,8 +88,6 @@ export function renderToolResultMessage(
<FileEditToolUpdatedMessage <FileEditToolUpdatedMessage
filePath={filePath} filePath={filePath}
structuredPatch={structuredPatch} structuredPatch={structuredPatch}
firstLine={originalFile.split('\n')[0] ?? null}
fileContent={originalFile}
style={style} style={style}
verbose={verbose} verbose={verbose}
previewHint={isPlanFile ? '/plan to preview' : undefined} previewHint={isPlanFile ? '/plan to preview' : undefined}
@@ -116,7 +103,7 @@ export function renderToolUseRejectedMessage(
replace_all?: boolean replace_all?: boolean
edits?: unknown[] edits?: unknown[]
}, },
options: { _options: {
columns: number columns: number
messages: Message[] messages: Message[]
progressMessagesForMessage: ProgressMessage[] progressMessagesForMessage: ProgressMessage[]
@@ -126,45 +113,14 @@ export function renderToolUseRejectedMessage(
verbose: boolean verbose: boolean
}, },
): React.ReactElement { ): React.ReactElement {
const { style, verbose } = options const { style, verbose } = _options
const filePath = input.file_path const filePath = input.file_path
const oldString = input.old_string ?? '' const isNewFile = 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}
/>
)
}
return ( return (
<EditRejectionDiff <FileEditToolUseRejectedMessage
filePath={filePath} file_path={filePath}
oldString={oldString} operation={isNewFile ? 'write' : 'update'}
newString={newString}
replaceAll={replaceAll}
style={style} style={style}
verbose={verbose} verbose={verbose}
/> />
@@ -201,115 +157,3 @@ export function renderToolUseErrorMessage(
} }
return <FallbackToolUseErrorMessage result={result} verbose={verbose} /> 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 { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
import type { StructuredPatchHunk } from 'diff' import { relative } from 'path'
import { isAbsolute, relative, resolve } from 'path'
import * as React from 'react' import * as React from 'react'
import { Suspense, use, useState } from 'react'
import { MessageResponse } from 'src/components/MessageResponse.js' import { MessageResponse } from 'src/components/MessageResponse.js'
import { extractTag } from 'src/utils/messages.js' import { extractTag } from 'src/utils/messages.js'
import { CtrlOToExpand } from 'src/components/CtrlOToExpand.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 { ToolProgressData } from 'src/Tool.js'
import type { ProgressMessage } from 'src/types/message.js' import type { ProgressMessage } from 'src/types/message.js'
import { getCwd } from 'src/utils/cwd.js' import { getCwd } from 'src/utils/cwd.js'
import { getPatchForDisplay } from 'src/utils/diff.js'
import { getDisplayPath } from 'src/utils/file.js' import { getDisplayPath } from 'src/utils/file.js'
import { logError } from 'src/utils/log.js'
import { getPlansDirectory } from 'src/utils/plans.js' import { getPlansDirectory } from 'src/utils/plans.js'
import { openForScan, readCapped } from 'src/utils/readEditContext.js'
import type { Output } from './FileWriteTool.js' import type { Output } from './FileWriteTool.js'
const MAX_LINES_TO_RENDER = 10 const MAX_LINES_TO_RENDER = 10
@@ -137,131 +132,19 @@ export function renderToolUseMessage(
} }
export function renderToolUseRejectedMessage( export function renderToolUseRejectedMessage(
{ file_path, content }: { file_path: string; content: string }, { file_path }: { file_path: string; content: string },
{ style, verbose }: { style?: 'condensed'; verbose: boolean }, { style, verbose }: { style?: 'condensed'; verbose: boolean },
): React.ReactNode { ): React.ReactNode {
return ( 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 <FileEditToolUseRejectedMessage
file_path={filePath} file_path={file_path}
operation="write" 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} style={style}
verbose={verbose} 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( export function renderToolUseErrorMessage(
result: ToolResultBlockParam['content'], result: ToolResultBlockParam['content'],
{ verbose }: { verbose: boolean }, { verbose }: { verbose: boolean },
@@ -324,8 +207,6 @@ export function renderToolResultMessage(
<FileEditToolUpdatedMessage <FileEditToolUpdatedMessage
filePath={filePath} filePath={filePath}
structuredPatch={structuredPatch} structuredPatch={structuredPatch}
firstLine={content.split('\n')[0] ?? null}
fileContent={originalFile ?? undefined}
style={style} style={style}
verbose={verbose} verbose={verbose}
previewHint={isPlanFile ? '/plan to preview' : undefined} previewHint={isPlanFile ? '/plan to preview' : undefined}

View File

@@ -1,16 +1,11 @@
import type { StructuredPatchHunk } from 'diff'
import * as React from 'react' import * as React from 'react'
import { useTerminalSize } from '../hooks/useTerminalSize.js' import { Text } from '@anthropic/ink'
import { Box, Text } from '@anthropic/ink'
import { count } from '../utils/array.js' import { count } from '../utils/array.js'
import { MessageResponse } from './MessageResponse.js' import { MessageResponse } from './MessageResponse.js'
import { StructuredDiffList } from './StructuredDiffList.js'
type Props = { type Props = {
filePath: string filePath: string
structuredPatch: StructuredPatchHunk[] structuredPatch: { lines: string[] }[]
firstLine: string | null
fileContent?: string
style?: 'condensed' style?: 'condensed'
verbose: boolean verbose: boolean
previewHint?: string previewHint?: string
@@ -19,13 +14,10 @@ type Props = {
export function FileEditToolUpdatedMessage({ export function FileEditToolUpdatedMessage({
filePath, filePath,
structuredPatch, structuredPatch,
firstLine,
fileContent,
style, style,
verbose, verbose,
previewHint, previewHint,
}: Props): React.ReactNode { }: Props): React.ReactNode {
const { columns } = useTerminalSize()
const numAdditions = structuredPatch.reduce( const numAdditions = structuredPatch.reduce(
(acc, hunk) => acc + count(hunk.lines, _ => _.startsWith('+')), (acc, hunk) => acc + count(hunk.lines, _ => _.startsWith('+')),
0, 0,
@@ -55,7 +47,7 @@ export function FileEditToolUpdatedMessage({
// Plan files: invert condensed behavior // Plan files: invert condensed behavior
// - Regular mode: just show the hint (user can type /plan to see full content) // - Regular mode: just show the hint (user can type /plan to see full content)
// - Condensed mode (subagent view): show the diff // - Condensed mode (subagent view): show the text
if (previewHint) { if (previewHint) {
if (style !== 'condensed' && !verbose) { if (style !== 'condensed' && !verbose) {
return ( return (
@@ -69,18 +61,6 @@ export function FileEditToolUpdatedMessage({
} }
return ( return (
<MessageResponse> <MessageResponse>{text}</MessageResponse>
<Box flexDirection="column">
<Text>{text}</Text>
<StructuredDiffList
hunks={structuredPatch}
dim={false}
width={columns - 12}
filePath={filePath}
firstLine={firstLine}
fileContent={fileContent}
/>
</Box>
</MessageResponse>
) )
} }

View File

@@ -1,24 +1,12 @@
import type { StructuredPatchHunk } from 'diff'
import { relative } from 'path' import { relative } from 'path'
import * as React from 'react' import * as React from 'react'
import { useTerminalSize } from 'src/hooks/useTerminalSize.js'
import { getCwd } from 'src/utils/cwd.js' import { getCwd } from 'src/utils/cwd.js'
import { Box, Text } from '@anthropic/ink' import { Box, Text } from '@anthropic/ink'
import { HighlightedCode } from './HighlightedCode.js'
import { MessageResponse } from './MessageResponse.js' import { MessageResponse } from './MessageResponse.js'
import { StructuredDiffList } from './StructuredDiffList.js'
const MAX_LINES_TO_RENDER = 10
type Props = { type Props = {
file_path: string file_path: string
operation: 'write' | 'update' operation: 'write' | 'update'
// For updates - show diff
patch?: StructuredPatchHunk[]
firstLine: string | null
fileContent?: string
// For new file creation - show content preview
content?: string
style?: 'condensed' style?: 'condensed'
verbose: boolean verbose: boolean
} }
@@ -26,14 +14,9 @@ type Props = {
export function FileEditToolUseRejectedMessage({ export function FileEditToolUseRejectedMessage({
file_path, file_path,
operation, operation,
patch,
firstLine,
fileContent,
content,
style, style,
verbose, verbose,
}: Props): React.ReactNode { }: Props): React.ReactNode {
const { columns } = useTerminalSize()
const text = ( const text = (
<Box flexDirection="row"> <Box flexDirection="row">
<Text color="subtle">User rejected {operation} to </Text> <Text color="subtle">User rejected {operation} to </Text>
@@ -48,51 +31,5 @@ export function FileEditToolUseRejectedMessage({
return <MessageResponse>{text}</MessageResponse> return <MessageResponse>{text}</MessageResponse>
} }
// For new file creation, show content preview (dimmed) return <MessageResponse>{text}</MessageResponse>
if (operation === 'write' && content !== undefined) {
const lines = content.split('\n')
const numLines = lines.length
const plusLines = numLines - MAX_LINES_TO_RENDER
const truncatedContent = verbose
? content
: lines.slice(0, MAX_LINES_TO_RENDER).join('\n')
return (
<MessageResponse>
<Box flexDirection="column">
{text}
<HighlightedCode
code={truncatedContent || '(No content)'}
filePath={file_path}
width={columns - 12}
dim
/>
{!verbose && plusLines > 0 && (
<Text dimColor> +{plusLines} lines</Text>
)}
</Box>
</MessageResponse>
)
}
// For updates, show diff
if (!patch || patch.length === 0) {
return <MessageResponse>{text}</MessageResponse>
}
return (
<MessageResponse>
<Box flexDirection="column">
{text}
<StructuredDiffList
hunks={patch}
dim
width={columns - 12}
filePath={file_path}
firstLine={firstLine}
fileContent={fileContent}
/>
</Box>
</MessageResponse>
)
} }