fix: 恢复消息流中 diff 高亮渲染功能

还原 commit 51b8ad46 删除的 diff highlight 显示:FileEdit/FileWrite 工具
执行成功后重新展示 StructuredDiffList,拒绝时重新展示高亮代码预览或
带上下文的 diff 视图。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-05-06 14:38:10 +08:00
parent c7efac6b8d
commit 12f5aedf99
4 changed files with 344 additions and 16 deletions

View File

@@ -1,5 +1,7 @@
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';
@@ -10,10 +12,15 @@ 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:
@@ -84,6 +91,8 @@ 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}
@@ -99,7 +108,7 @@ export function renderToolUseRejectedMessage(
replace_all?: boolean;
edits?: unknown[];
},
_options: {
options: {
columns: number;
messages: Message[];
progressMessagesForMessage: ProgressMessage[];
@@ -109,14 +118,40 @@ export function renderToolUseRejectedMessage(
verbose: boolean;
},
): React.ReactElement {
const { style, verbose } = _options;
const { style, verbose } = options;
const filePath = input.file_path;
const isNewFile = input.old_string === '';
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}
/>
);
}
return (
<FileEditToolUseRejectedMessage
file_path={filePath}
operation={isNewFile ? 'write' : 'update'}
<EditRejectionDiff
filePath={filePath}
oldString={oldString}
newString={newString}
replaceAll={replaceAll}
style={style}
verbose={verbose}
/>
@@ -149,3 +184,103 @@ 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,6 +1,8 @@
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs';
import { relative } from 'path';
import type { StructuredPatchHunk } from 'diff';
import { isAbsolute, relative, resolve } 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';
@@ -15,8 +17,11 @@ 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;
@@ -122,10 +127,115 @@ export function renderToolUseMessage(
}
export function renderToolUseRejectedMessage(
{ file_path }: { file_path: string; content: string },
{ file_path, content }: { file_path: string; content: string },
{ style, verbose }: { style?: 'condensed'; verbose: boolean },
): React.ReactNode {
return <FileEditToolUseRejectedMessage file_path={file_path} operation="write" style={style} verbose={verbose} />;
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}
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(
@@ -179,6 +289,8 @@ 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}