mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-23 00:35:51 +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:
131
packages/builtin-tools/src/tools/TaskStopTool/TaskStopTool.ts
Normal file
131
packages/builtin-tools/src/tools/TaskStopTool/TaskStopTool.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import { z } from 'zod/v4'
|
||||
import type { TaskStateBase } from 'src/Task.js'
|
||||
import { buildTool, type ToolDef } from 'src/Tool.js'
|
||||
import { stopTask } from 'src/tasks/stopTask.js'
|
||||
import { lazySchema } from 'src/utils/lazySchema.js'
|
||||
import { jsonStringify } from 'src/utils/slowOperations.js'
|
||||
import { DESCRIPTION, TASK_STOP_TOOL_NAME } from './prompt.js'
|
||||
import { renderToolResultMessage, renderToolUseMessage } from './UI.js'
|
||||
|
||||
const inputSchema = lazySchema(() =>
|
||||
z.strictObject({
|
||||
task_id: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('The ID of the background task to stop'),
|
||||
// shell_id is accepted for backward compatibility with the deprecated KillShell tool
|
||||
shell_id: z.string().optional().describe('Deprecated: use task_id instead'),
|
||||
}),
|
||||
)
|
||||
type InputSchema = ReturnType<typeof inputSchema>
|
||||
|
||||
const outputSchema = lazySchema(() =>
|
||||
z.object({
|
||||
message: z.string().describe('Status message about the operation'),
|
||||
task_id: z.string().describe('The ID of the task that was stopped'),
|
||||
task_type: z.string().describe('The type of the task that was stopped'),
|
||||
// Optional: tool outputs are persisted to transcripts and replayed on --resume
|
||||
// without re-validation, so sessions from before this field was added lack it.
|
||||
command: z
|
||||
.string()
|
||||
.optional()
|
||||
.describe('The command or description of the stopped task'),
|
||||
}),
|
||||
)
|
||||
type OutputSchema = ReturnType<typeof outputSchema>
|
||||
|
||||
export type Output = z.infer<OutputSchema>
|
||||
|
||||
export const TaskStopTool = buildTool({
|
||||
name: TASK_STOP_TOOL_NAME,
|
||||
searchHint: 'kill a running background task',
|
||||
// KillShell is the deprecated name - kept as alias for backward compatibility
|
||||
// with existing transcripts and SDK users
|
||||
aliases: ['KillShell'],
|
||||
maxResultSizeChars: 100_000,
|
||||
userFacingName: () => (process.env.USER_TYPE === 'ant' ? '' : 'Stop Task'),
|
||||
get inputSchema(): InputSchema {
|
||||
return inputSchema()
|
||||
},
|
||||
get outputSchema(): OutputSchema {
|
||||
return outputSchema()
|
||||
},
|
||||
shouldDefer: true,
|
||||
isConcurrencySafe() {
|
||||
return true
|
||||
},
|
||||
toAutoClassifierInput(input) {
|
||||
return input.task_id ?? input.shell_id ?? ''
|
||||
},
|
||||
async validateInput({ task_id, shell_id }, { getAppState }) {
|
||||
// Support both task_id and shell_id (deprecated KillShell compat)
|
||||
const id = task_id ?? shell_id
|
||||
if (!id) {
|
||||
return {
|
||||
result: false,
|
||||
message: 'Missing required parameter: task_id',
|
||||
errorCode: 1,
|
||||
}
|
||||
}
|
||||
|
||||
const appState = getAppState()
|
||||
const task = appState.tasks?.[id] as TaskStateBase | undefined
|
||||
|
||||
if (!task) {
|
||||
return {
|
||||
result: false,
|
||||
message: `No task found with ID: ${id}`,
|
||||
errorCode: 1,
|
||||
}
|
||||
}
|
||||
|
||||
if (task.status !== 'running') {
|
||||
return {
|
||||
result: false,
|
||||
message: `Task ${id} is not running (status: ${task.status})`,
|
||||
errorCode: 3,
|
||||
}
|
||||
}
|
||||
|
||||
return { result: true }
|
||||
},
|
||||
async description() {
|
||||
return `Stop a running background task by ID`
|
||||
},
|
||||
async prompt() {
|
||||
return DESCRIPTION
|
||||
},
|
||||
mapToolResultToToolResultBlockParam(output, toolUseID) {
|
||||
return {
|
||||
tool_use_id: toolUseID,
|
||||
type: 'tool_result',
|
||||
content: jsonStringify(output),
|
||||
}
|
||||
},
|
||||
renderToolUseMessage,
|
||||
renderToolResultMessage,
|
||||
async call(
|
||||
{ task_id, shell_id },
|
||||
{ getAppState, setAppState, abortController },
|
||||
) {
|
||||
// Support both task_id and shell_id (deprecated KillShell compat)
|
||||
const id = task_id ?? shell_id
|
||||
if (!id) {
|
||||
throw new Error('Missing required parameter: task_id')
|
||||
}
|
||||
|
||||
const result = await stopTask(id, {
|
||||
getAppState,
|
||||
setAppState,
|
||||
})
|
||||
|
||||
return {
|
||||
data: {
|
||||
message: `Successfully stopped task: ${result.taskId} (${result.command})`,
|
||||
task_id: result.taskId,
|
||||
task_type: result.taskType,
|
||||
command: result.command,
|
||||
},
|
||||
}
|
||||
},
|
||||
} satisfies ToolDef<InputSchema, Output>)
|
||||
50
packages/builtin-tools/src/tools/TaskStopTool/UI.tsx
Normal file
50
packages/builtin-tools/src/tools/TaskStopTool/UI.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
import React from 'react'
|
||||
import { MessageResponse } from 'src/components/MessageResponse.js'
|
||||
import { Text, stringWidth } from '@anthropic/ink'
|
||||
import { truncateToWidthNoEllipsis } from 'src/utils/format.js'
|
||||
import type { Output } from './TaskStopTool.js'
|
||||
|
||||
export function renderToolUseMessage(): React.ReactNode {
|
||||
return ''
|
||||
}
|
||||
|
||||
const MAX_COMMAND_DISPLAY_LINES = 2
|
||||
const MAX_COMMAND_DISPLAY_CHARS = 160
|
||||
|
||||
function truncateCommand(command: string): string {
|
||||
const lines = command.split('\n')
|
||||
let truncated = command
|
||||
|
||||
if (lines.length > MAX_COMMAND_DISPLAY_LINES) {
|
||||
truncated = lines.slice(0, MAX_COMMAND_DISPLAY_LINES).join('\n')
|
||||
}
|
||||
|
||||
if (stringWidth(truncated) > MAX_COMMAND_DISPLAY_CHARS) {
|
||||
truncated = truncateToWidthNoEllipsis(truncated, MAX_COMMAND_DISPLAY_CHARS)
|
||||
}
|
||||
|
||||
return truncated.trim()
|
||||
}
|
||||
|
||||
export function renderToolResultMessage(
|
||||
output: Output,
|
||||
_progressMessagesForMessage: unknown[],
|
||||
{ verbose }: { verbose: boolean },
|
||||
): React.ReactNode {
|
||||
if (process.env.USER_TYPE === 'ant') {
|
||||
return null
|
||||
}
|
||||
|
||||
const rawCommand = output.command ?? ''
|
||||
const command = verbose ? rawCommand : truncateCommand(rawCommand)
|
||||
const suffix = command !== rawCommand ? '… · stopped' : ' · stopped'
|
||||
|
||||
return (
|
||||
<MessageResponse>
|
||||
<Text>
|
||||
{command}
|
||||
{suffix}
|
||||
</Text>
|
||||
</MessageResponse>
|
||||
)
|
||||
}
|
||||
8
packages/builtin-tools/src/tools/TaskStopTool/prompt.ts
Normal file
8
packages/builtin-tools/src/tools/TaskStopTool/prompt.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export const TASK_STOP_TOOL_NAME = 'TaskStop'
|
||||
|
||||
export const DESCRIPTION = `
|
||||
- Stops a running background task by its ID
|
||||
- Takes a task_id parameter identifying the task to stop
|
||||
- Returns a success or failure status
|
||||
- Use this tool when you need to terminate a long-running task
|
||||
`
|
||||
Reference in New Issue
Block a user