File size: 12,044 Bytes
39e315a | 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | import { c as _c } from "react-compiler-runtime";
import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs';
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 '../../components/CtrlOToExpand.js';
import { FallbackToolUseErrorMessage } from '../../components/FallbackToolUseErrorMessage.js';
import { FileEditToolUpdatedMessage } from '../../components/FileEditToolUpdatedMessage.js';
import { FileEditToolUseRejectedMessage } from '../../components/FileEditToolUseRejectedMessage.js';
import { FilePathLink } from '../../components/FilePathLink.js';
import { HighlightedCode } from '../../components/HighlightedCode.js';
import { useTerminalSize } from '../../hooks/useTerminalSize.js';
import { Box, Text } from '../../ink.js';
import type { ToolProgressData } from '../../Tool.js';
import type { ProgressMessage } from '../../types/message.js';
import { getCwd } from '../../utils/cwd.js';
import { getPatchForDisplay } from '../../utils/diff.js';
import { getDisplayPath } from '../../utils/file.js';
import { logError } from '../../utils/log.js';
import { getPlansDirectory } from '../../utils/plans.js';
import { openForScan, readCapped } from '../../utils/readEditContext.js';
import type { Output } from './FileWriteTool.js';
const MAX_LINES_TO_RENDER = 10;
// Model output uses \n regardless of platform, so always split on \n.
// os.EOL is \r\n on Windows, which would give numLines=1 for all files.
const EOL = '\n';
/**
* Count visible lines in file content. A trailing newline is treated as a
* line terminator (not a new empty line), matching editor line numbering.
*/
export function countLines(content: string): number {
const parts = content.split(EOL);
return content.endsWith(EOL) ? parts.length - 1 : parts.length;
}
function FileWriteToolCreatedMessage(t0) {
const $ = _c(25);
const {
filePath,
content,
verbose
} = t0;
const {
columns
} = useTerminalSize();
const contentWithFallback = content || "(No content)";
const numLines = countLines(content);
const plusLines = numLines - MAX_LINES_TO_RENDER;
let t1;
if ($[0] !== numLines) {
t1 = <Text bold={true}>{numLines}</Text>;
$[0] = numLines;
$[1] = t1;
} else {
t1 = $[1];
}
let t2;
if ($[2] !== filePath || $[3] !== verbose) {
t2 = verbose ? filePath : relative(getCwd(), filePath);
$[2] = filePath;
$[3] = verbose;
$[4] = t2;
} else {
t2 = $[4];
}
let t3;
if ($[5] !== t2) {
t3 = <Text bold={true}>{t2}</Text>;
$[5] = t2;
$[6] = t3;
} else {
t3 = $[6];
}
let t4;
if ($[7] !== t1 || $[8] !== t3) {
t4 = <Text>Wrote {t1} lines to{" "}{t3}</Text>;
$[7] = t1;
$[8] = t3;
$[9] = t4;
} else {
t4 = $[9];
}
let t5;
if ($[10] !== contentWithFallback || $[11] !== verbose) {
t5 = verbose ? contentWithFallback : contentWithFallback.split("\n").slice(0, MAX_LINES_TO_RENDER).join("\n");
$[10] = contentWithFallback;
$[11] = verbose;
$[12] = t5;
} else {
t5 = $[12];
}
const t6 = columns - 12;
let t7;
if ($[13] !== filePath || $[14] !== t5 || $[15] !== t6) {
t7 = <Box flexDirection="column"><HighlightedCode code={t5} filePath={filePath} width={t6} /></Box>;
$[13] = filePath;
$[14] = t5;
$[15] = t6;
$[16] = t7;
} else {
t7 = $[16];
}
let t8;
if ($[17] !== numLines || $[18] !== plusLines || $[19] !== verbose) {
t8 = !verbose && plusLines > 0 && <Text dimColor={true}>… +{plusLines} {plusLines === 1 ? "line" : "lines"}{" "}{numLines > 0 && <CtrlOToExpand />}</Text>;
$[17] = numLines;
$[18] = plusLines;
$[19] = verbose;
$[20] = t8;
} else {
t8 = $[20];
}
let t9;
if ($[21] !== t4 || $[22] !== t7 || $[23] !== t8) {
t9 = <MessageResponse><Box flexDirection="column">{t4}{t7}{t8}</Box></MessageResponse>;
$[21] = t4;
$[22] = t7;
$[23] = t8;
$[24] = t9;
} else {
t9 = $[24];
}
return t9;
}
export function userFacingName(input: Partial<{
file_path: string;
content: string;
}> | undefined): string {
if (input?.file_path?.startsWith(getPlansDirectory())) {
return 'Updated plan';
}
return 'Write';
}
/** Gates fullscreen click-to-expand. Only `create` truncates (to
* MAX_LINES_TO_RENDER); `update` renders the full diff regardless of verbose.
* Called per visible message on hover/scroll, so early-exit after finding the
* (MAX+1)th line instead of splitting the whole (possibly huge) content. */
export function isResultTruncated({
type,
content
}: Output): boolean {
if (type !== 'create') return false;
let pos = 0;
for (let i = 0; i < MAX_LINES_TO_RENDER; i++) {
pos = content.indexOf(EOL, pos);
if (pos === -1) return false;
pos++;
}
// countLines treats a trailing EOL as a terminator, not a new line
return pos < content.length;
}
export function getToolUseSummary(input: Partial<{
file_path: string;
content: string;
}> | undefined): string | null {
if (!input?.file_path) {
return null;
}
return getDisplayPath(input.file_path);
}
export function renderToolUseMessage(input: Partial<{
file_path: string;
content: string;
}>, {
verbose
}: {
verbose: boolean;
}): React.ReactNode {
if (!input.file_path) {
return null;
}
// For plan files, path is already in userFacingName
if (input.file_path.startsWith(getPlansDirectory())) {
return '';
}
return <FilePathLink filePath={input.file_path}>
{verbose ? input.file_path : getDisplayPath(input.file_path)}
</FilePathLink>;
}
export function renderToolUseRejectedMessage({
file_path,
content
}: {
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(t0) {
const $ = _c(20);
const {
filePath,
content,
style,
verbose
} = t0;
let t1;
if ($[0] !== content || $[1] !== filePath) {
t1 = () => loadRejectionDiff(filePath, content);
$[0] = content;
$[1] = filePath;
$[2] = t1;
} else {
t1 = $[2];
}
const [dataPromise] = useState(t1);
let t2;
if ($[3] !== content) {
t2 = content.split("\n")[0] ?? null;
$[3] = content;
$[4] = t2;
} else {
t2 = $[4];
}
const firstLine = t2;
let t3;
if ($[5] !== content || $[6] !== filePath || $[7] !== firstLine || $[8] !== verbose) {
t3 = <FileEditToolUseRejectedMessage file_path={filePath} operation="write" content={content} firstLine={firstLine} verbose={verbose} />;
$[5] = content;
$[6] = filePath;
$[7] = firstLine;
$[8] = verbose;
$[9] = t3;
} else {
t3 = $[9];
}
const createFallback = t3;
let t4;
if ($[10] !== createFallback || $[11] !== dataPromise || $[12] !== filePath || $[13] !== firstLine || $[14] !== style || $[15] !== verbose) {
t4 = <WriteRejectionBody promise={dataPromise} filePath={filePath} firstLine={firstLine} createFallback={createFallback} style={style} verbose={verbose} />;
$[10] = createFallback;
$[11] = dataPromise;
$[12] = filePath;
$[13] = firstLine;
$[14] = style;
$[15] = verbose;
$[16] = t4;
} else {
t4 = $[16];
}
let t5;
if ($[17] !== createFallback || $[18] !== t4) {
t5 = <Suspense fallback={createFallback}>{t4}</Suspense>;
$[17] = createFallback;
$[18] = t4;
$[19] = t5;
} else {
t5 = $[19];
}
return t5;
}
function WriteRejectionBody(t0) {
const $ = _c(8);
const {
promise,
filePath,
firstLine,
createFallback,
style,
verbose
} = t0;
const data = use(promise);
if (data.type === "create") {
return createFallback;
}
if (data.type === "error") {
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <MessageResponse><Text>(No changes)</Text></MessageResponse>;
$[0] = t1;
} else {
t1 = $[0];
}
return t1;
}
let t1;
if ($[1] !== data.oldContent || $[2] !== data.patch || $[3] !== filePath || $[4] !== firstLine || $[5] !== style || $[6] !== verbose) {
t1 = <FileEditToolUseRejectedMessage file_path={filePath} operation="update" patch={data.patch} firstLine={firstLine} fileContent={data.oldContent} style={style} verbose={verbose} />;
$[1] = data.oldContent;
$[2] = data.patch;
$[3] = filePath;
$[4] = firstLine;
$[5] = style;
$[6] = verbose;
$[7] = t1;
} else {
t1 = $[7];
}
return t1;
}
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;
}): React.ReactNode {
if (!verbose && typeof result === 'string' && extractTag(result, 'tool_use_error')) {
return <MessageResponse>
<Text color="error">Error writing file</Text>
</MessageResponse>;
}
return <FallbackToolUseErrorMessage result={result} verbose={verbose} />;
}
export function renderToolResultMessage({
filePath,
content,
structuredPatch,
type,
originalFile
}: Output, _progressMessagesForMessage: ProgressMessage<ToolProgressData>[], {
style,
verbose
}: {
style?: 'condensed';
verbose: boolean;
}): React.ReactNode {
switch (type) {
case 'create':
{
const isPlanFile = filePath.startsWith(getPlansDirectory());
// Plan files: invert condensed behavior
// - Regular mode: just show hint (user can type /plan to see full content)
// - Condensed mode (subagent view): show full content
if (isPlanFile && !verbose) {
if (style !== 'condensed') {
return <MessageResponse>
<Text dimColor>/plan to preview</Text>
</MessageResponse>;
}
} else if (style === 'condensed' && !verbose) {
const numLines = countLines(content);
return <Text>
Wrote <Text bold>{numLines}</Text> lines to{' '}
<Text bold>{relative(getCwd(), filePath)}</Text>
</Text>;
}
return <FileWriteToolCreatedMessage filePath={filePath} content={content} verbose={verbose} />;
}
case 'update':
{
const isPlanFile = filePath.startsWith(getPlansDirectory());
return <FileEditToolUpdatedMessage filePath={filePath} structuredPatch={structuredPatch} firstLine={content.split('\n')[0] ?? null} fileContent={originalFile ?? undefined} style={style} verbose={verbose} previewHint={isPlanFile ? '/plan to preview' : undefined} />;
}
}
}
|