mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-24 17:15:50 +00:00
feat: 工具层及 mcp 大重构 (#252)
* feat: 第一版大重构 * fix: 修复类型问题 * chore: 更新版本到 1.3.2 * Add brave as alternative WebSearchTool * fix: 修正顺序 * fix: 修复对穷鬼模式的 auto dream 和 session memory 越过 * feat: 穷鬼模式去除 session-summary * feat: 创建 builtin-tools 包,搬运所有工具实现 将 src/tools/ 下的全部 60 个工具目录迁移至 packages/builtin-tools/src/tools/, 内部导入路径已更新为 src/ alias 模式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 更新 src/ 中所有工具引用至 builtin-tools 包,删除 src/tools/ - src/tools.ts 及 178 个 src/ 文件的 import 路径从 ./tools/ 改为 builtin-tools/tools/ - 删除 src/tools/ 整个目录(已迁移至 packages/builtin-tools/) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: 添加 builtin-tools 路径别名至 tsconfig,更新 bun.lock - tsconfig.json 新增 builtin-tools/* 和 builtin-tools 路径映射 - 新增 packages/builtin-tools/src 至 include Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: 为 builtin-tools、mcp-client、agent-tools 添加 @claude-code-best 作用域前缀 所有包名及 import 路径统一添加 @claude-code-best/ 前缀: - builtin-tools → @claude-code-best/builtin-tools - mcp-client → @claude-code-best/mcp-client - agent-tools → @claude-code-best/agent-tools Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: 修复 node 环境没有 bun 的问题 --------- Co-authored-by: Eric-Guo <eric.guocz@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
336
packages/builtin-tools/src/tools/FileWriteTool/UI.tsx
Normal file
336
packages/builtin-tools/src/tools/FileWriteTool/UI.tsx
Normal file
@@ -0,0 +1,336 @@
|
||||
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 'src/components/CtrlOToExpand.js'
|
||||
import { FallbackToolUseErrorMessage } from 'src/components/FallbackToolUseErrorMessage.js'
|
||||
import { FileEditToolUpdatedMessage } from 'src/components/FileEditToolUpdatedMessage.js'
|
||||
import { FileEditToolUseRejectedMessage } from 'src/components/FileEditToolUseRejectedMessage.js'
|
||||
|
||||
import { HighlightedCode } from 'src/components/HighlightedCode.js'
|
||||
import { useTerminalSize } from 'src/hooks/useTerminalSize.js'
|
||||
import { Box, Text } from '@anthropic/ink'
|
||||
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
|
||||
// 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({
|
||||
filePath,
|
||||
content,
|
||||
verbose,
|
||||
}: {
|
||||
filePath: string
|
||||
content: string
|
||||
verbose: boolean
|
||||
}): React.ReactNode {
|
||||
const { columns } = useTerminalSize()
|
||||
const contentWithFallback = content || '(No content)'
|
||||
const numLines = countLines(content)
|
||||
const plusLines = numLines - MAX_LINES_TO_RENDER
|
||||
|
||||
return (
|
||||
<MessageResponse>
|
||||
<Box flexDirection="column">
|
||||
<Text>
|
||||
Wrote <Text bold>{numLines}</Text> lines to{' '}
|
||||
<Text bold>{verbose ? filePath : relative(getCwd(), filePath)}</Text>
|
||||
</Text>
|
||||
<Box flexDirection="column">
|
||||
<HighlightedCode
|
||||
code={
|
||||
verbose
|
||||
? contentWithFallback
|
||||
: contentWithFallback
|
||||
.split('\n')
|
||||
.slice(0, MAX_LINES_TO_RENDER)
|
||||
.join('\n')
|
||||
}
|
||||
filePath={filePath}
|
||||
width={columns - 12}
|
||||
/>
|
||||
</Box>
|
||||
{!verbose && plusLines > 0 && (
|
||||
<Text dimColor>
|
||||
… +{plusLines} {plusLines === 1 ? 'line' : 'lines'}{' '}
|
||||
{numLines > 0 && <CtrlOToExpand />}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
</MessageResponse>
|
||||
)
|
||||
}
|
||||
|
||||
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({
|
||||
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(
|
||||
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}
|
||||
/>
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user