* 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>
178 lines
4.8 KiB
TypeScript
178 lines
4.8 KiB
TypeScript
import { type StructuredPatchHunk, structuredPatch } from 'diff'
|
|
import { logEvent } from 'src/services/analytics/index.js'
|
|
import { getLocCounter } from '../bootstrap/state.js'
|
|
import { addToTotalLinesChanged } from '../cost-tracker.js'
|
|
import type { FileEdit } from '@claude-code-best/builtin-tools/tools/FileEditTool/types.js'
|
|
import { count } from './array.js'
|
|
import { convertLeadingTabsToSpaces } from './file.js'
|
|
|
|
export const CONTEXT_LINES = 3
|
|
export const DIFF_TIMEOUT_MS = 5_000
|
|
|
|
/**
|
|
* Shifts hunk line numbers by offset. Use when getPatchForDisplay received
|
|
* a slice of the file (e.g. readEditContext) rather than the whole file —
|
|
* callers pass `ctx.lineOffset - 1` to convert slice-relative to file-relative.
|
|
*/
|
|
export function adjustHunkLineNumbers(
|
|
hunks: StructuredPatchHunk[],
|
|
offset: number,
|
|
): StructuredPatchHunk[] {
|
|
if (offset === 0) return hunks
|
|
return hunks.map(h => ({
|
|
...h,
|
|
oldStart: h.oldStart + offset,
|
|
newStart: h.newStart + offset,
|
|
}))
|
|
}
|
|
|
|
// For some reason, & confuses the diff library, so we replace it with a token,
|
|
// then substitute it back in after the diff is computed.
|
|
const AMPERSAND_TOKEN = '<<:AMPERSAND_TOKEN:>>'
|
|
|
|
const DOLLAR_TOKEN = '<<:DOLLAR_TOKEN:>>'
|
|
|
|
function escapeForDiff(s: string): string {
|
|
return s.replaceAll('&', AMPERSAND_TOKEN).replaceAll('$', DOLLAR_TOKEN)
|
|
}
|
|
|
|
function unescapeFromDiff(s: string): string {
|
|
return s.replaceAll(AMPERSAND_TOKEN, '&').replaceAll(DOLLAR_TOKEN, '$')
|
|
}
|
|
|
|
/**
|
|
* Count lines added and removed in a patch and update the total
|
|
* For new files, pass the content string as the second parameter
|
|
* @param patch Array of diff hunks
|
|
* @param newFileContent Optional content string for new files
|
|
*/
|
|
export function countLinesChanged(
|
|
patch: StructuredPatchHunk[],
|
|
newFileContent?: string,
|
|
): void {
|
|
let numAdditions = 0
|
|
let numRemovals = 0
|
|
|
|
if (patch.length === 0 && newFileContent) {
|
|
// For new files, count all lines as additions
|
|
numAdditions = newFileContent.split(/\r?\n/).length
|
|
} else {
|
|
numAdditions = patch.reduce(
|
|
(acc, hunk) => acc + count(hunk.lines, _ => _.startsWith('+')),
|
|
0,
|
|
)
|
|
numRemovals = patch.reduce(
|
|
(acc, hunk) => acc + count(hunk.lines, _ => _.startsWith('-')),
|
|
0,
|
|
)
|
|
}
|
|
|
|
addToTotalLinesChanged(numAdditions, numRemovals)
|
|
|
|
getLocCounter()?.add(numAdditions, { type: 'added' })
|
|
getLocCounter()?.add(numRemovals, { type: 'removed' })
|
|
|
|
logEvent('tengu_file_changed', {
|
|
lines_added: numAdditions,
|
|
lines_removed: numRemovals,
|
|
})
|
|
}
|
|
|
|
export function getPatchFromContents({
|
|
filePath,
|
|
oldContent,
|
|
newContent,
|
|
ignoreWhitespace = false,
|
|
singleHunk = false,
|
|
}: {
|
|
filePath: string
|
|
oldContent: string
|
|
newContent: string
|
|
ignoreWhitespace?: boolean
|
|
singleHunk?: boolean
|
|
}): StructuredPatchHunk[] {
|
|
const result = structuredPatch(
|
|
filePath,
|
|
filePath,
|
|
escapeForDiff(oldContent),
|
|
escapeForDiff(newContent),
|
|
undefined,
|
|
undefined,
|
|
{
|
|
ignoreWhitespace,
|
|
context: singleHunk ? 100_000 : CONTEXT_LINES,
|
|
timeout: DIFF_TIMEOUT_MS,
|
|
},
|
|
)
|
|
if (!result) {
|
|
return []
|
|
}
|
|
return result.hunks.map(_ => ({
|
|
..._,
|
|
lines: _.lines.map(unescapeFromDiff),
|
|
}))
|
|
}
|
|
|
|
/**
|
|
* Get a patch for display with edits applied
|
|
* @param filePath The path to the file
|
|
* @param fileContents The contents of the file
|
|
* @param edits An array of edits to apply to the file
|
|
* @param ignoreWhitespace Whether to ignore whitespace changes
|
|
* @returns An array of hunks representing the diff
|
|
*
|
|
* NOTE: This function will return the diff with all leading tabs
|
|
* rendered as spaces for display
|
|
*/
|
|
|
|
export function getPatchForDisplay({
|
|
filePath,
|
|
fileContents,
|
|
edits,
|
|
ignoreWhitespace = false,
|
|
}: {
|
|
filePath: string
|
|
fileContents: string
|
|
edits: FileEdit[]
|
|
ignoreWhitespace?: boolean
|
|
}): StructuredPatchHunk[] {
|
|
const preparedFileContents = escapeForDiff(
|
|
convertLeadingTabsToSpaces(fileContents),
|
|
)
|
|
const result = structuredPatch(
|
|
filePath,
|
|
filePath,
|
|
preparedFileContents,
|
|
edits.reduce((p, edit) => {
|
|
const { old_string, new_string } = edit
|
|
const replace_all = 'replace_all' in edit ? edit.replace_all : false
|
|
const escapedOldString = escapeForDiff(
|
|
convertLeadingTabsToSpaces(old_string),
|
|
)
|
|
const escapedNewString = escapeForDiff(
|
|
convertLeadingTabsToSpaces(new_string),
|
|
)
|
|
|
|
if (replace_all) {
|
|
return p.replaceAll(escapedOldString, () => escapedNewString)
|
|
} else {
|
|
return p.replace(escapedOldString, () => escapedNewString)
|
|
}
|
|
}, preparedFileContents),
|
|
undefined,
|
|
undefined,
|
|
{
|
|
context: CONTEXT_LINES,
|
|
ignoreWhitespace,
|
|
timeout: DIFF_TIMEOUT_MS,
|
|
},
|
|
)
|
|
if (!result) {
|
|
return []
|
|
}
|
|
return result.hunks.map(_ => ({
|
|
..._,
|
|
lines: _.lines.map(unescapeFromDiff),
|
|
}))
|
|
}
|