feat(engine): 添加MCP连接管理和工具集成功能
- 集成MCP连接管理器,支持MCP服务器连接 - 添加多种内置工具:ClarifyTool、CronTool、DelegateTool、ExecuteCodeTool、 PatchFileTool、ProcessTool、SendMessageTool、SpawnTool、TerminalTool、 TodoTool、WebFetchTool、WebSearchTool、WriteFileTool等 - 实现工具注册和装配功能 - 添加技能选择上下文参数 - 支持思考模式控制参数thinking_enabled feat(coordinator): 重构任务执行计划器参数命名 - 将learning_candidate_enabled重命名为allow_candidate_generation - 更新TeamGraphScheduler中的参数传递 - 修改LocalAgentRunner中的相关参数处理 - 更新README文档中的相应描述 refactor(context): 标准化工具调用参数格式 - 添加_json导入用于参数序列化 - 实现_provider_tool_calls方法标准化OpenAI兼容的工具调用载荷 - 修复工具调用中参数非字符串类型的序列化问题 refactor(session): 优化消息历史记录过滤逻辑 - 修改get_messages_as_conversation为基于运行状态过滤消息 - 排除未完成、失败或错误结束的运行记录 - 改进对话历史的可见性控制机制 fix(store): 修复FTS索引重建逻辑 - 添加异常处理防止FTS索引创建失败 - 实现_rebuild_fts_index方法重新构建全文搜索索引 - 优化索引触发器和表的维护流程
This commit is contained in:
@ -116,6 +116,25 @@ SEARCH_FILES_PARAMETERS: dict[str, Any] = {
|
||||
"required": ["query"],
|
||||
}
|
||||
|
||||
WRITE_FILE_PARAMETERS: dict[str, Any] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string", "description": "File path relative to the current workspace."},
|
||||
"content": {"type": "string", "description": "Full file content to write."},
|
||||
},
|
||||
"required": ["path", "content"],
|
||||
}
|
||||
|
||||
PATCH_FILE_PARAMETERS: dict[str, Any] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string", "description": "File path relative to the current workspace."},
|
||||
"old_text": {"type": "string", "description": "Exact text to replace."},
|
||||
"new_text": {"type": "string", "description": "Replacement text."},
|
||||
},
|
||||
"required": ["path", "old_text", "new_text"],
|
||||
}
|
||||
|
||||
|
||||
class WorkspacePathError(ValueError):
|
||||
"""Raised when a requested path escapes the configured workspace."""
|
||||
@ -158,6 +177,20 @@ def _resolve_existing_path(workspace: str | None, user_path: str | None) -> tupl
|
||||
return root, resolved
|
||||
|
||||
|
||||
def _resolve_writable_path(workspace: str | None, user_path: str | None) -> tuple[Path, Path]:
|
||||
root = _workspace_root(workspace)
|
||||
if not user_path or not str(user_path).strip():
|
||||
raise WorkspacePathError("path is required")
|
||||
raw_path = Path(str(user_path)).expanduser()
|
||||
candidate = raw_path if raw_path.is_absolute() else root / raw_path
|
||||
parent = candidate.parent.resolve(strict=True)
|
||||
try:
|
||||
parent.relative_to(root)
|
||||
except ValueError as exc:
|
||||
raise WorkspacePathError(f"path escapes workspace: {user_path}") from exc
|
||||
return root, parent / candidate.name
|
||||
|
||||
|
||||
def _relative_path(root: Path, path: Path) -> str:
|
||||
try:
|
||||
return str(path.relative_to(root)) or "."
|
||||
@ -440,3 +473,73 @@ class SearchFilesTool:
|
||||
)
|
||||
except (OSError, WorkspacePathError, ValueError) as exc:
|
||||
return _json_result(False, error=str(exc), path=path)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class WriteFileTool:
|
||||
"""Write a UTF-8 text file inside the current workspace."""
|
||||
|
||||
name: str = "write_file"
|
||||
description: str = (
|
||||
"Write a UTF-8 text file inside the current workspace, replacing the full file. "
|
||||
"Use patch_file for targeted edits. Paths outside the workspace are rejected."
|
||||
)
|
||||
toolset: str = "filesystem"
|
||||
always_available: bool = False
|
||||
workspace: str | None = None
|
||||
parameters: dict[str, Any] = field(default_factory=lambda: dict(WRITE_FILE_PARAMETERS))
|
||||
|
||||
async def execute(self, *, path: str, content: str, workspace: str | None = None) -> str:
|
||||
try:
|
||||
root, resolved = _resolve_writable_path(workspace, path)
|
||||
resolved.parent.mkdir(parents=True, exist_ok=True)
|
||||
resolved.write_text(str(content), encoding="utf-8")
|
||||
return _json_result(True, path=_relative_path(root, resolved), bytes=len(str(content).encode("utf-8")))
|
||||
except (OSError, WorkspacePathError, ValueError) as exc:
|
||||
return _json_result(False, error=str(exc), path=path)
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class PatchFileTool:
|
||||
"""Replace an exact text fragment inside a workspace file."""
|
||||
|
||||
name: str = "patch_file"
|
||||
description: str = (
|
||||
"Replace an exact text fragment inside a UTF-8 workspace file. "
|
||||
"Fails if old_text is missing or ambiguous."
|
||||
)
|
||||
toolset: str = "filesystem"
|
||||
always_available: bool = False
|
||||
workspace: str | None = None
|
||||
parameters: dict[str, Any] = field(default_factory=lambda: dict(PATCH_FILE_PARAMETERS))
|
||||
|
||||
async def execute(
|
||||
self,
|
||||
*,
|
||||
path: str,
|
||||
old_text: str,
|
||||
new_text: str,
|
||||
workspace: str | None = None,
|
||||
) -> str:
|
||||
try:
|
||||
root, resolved = _resolve_existing_path(workspace, path)
|
||||
if not resolved.is_file():
|
||||
return _json_result(False, error="not_a_file", path=path)
|
||||
content = _read_text_file(resolved)
|
||||
occurrences = content.count(old_text)
|
||||
if occurrences == 0:
|
||||
return _json_result(False, error="old_text_not_found", path=path)
|
||||
if occurrences > 1:
|
||||
return _json_result(False, error="old_text_ambiguous", occurrences=occurrences, path=path)
|
||||
updated = content.replace(old_text, new_text, 1)
|
||||
resolved.write_text(updated, encoding="utf-8")
|
||||
return _json_result(
|
||||
True,
|
||||
path=_relative_path(root, resolved),
|
||||
old_bytes=len(old_text.encode("utf-8")),
|
||||
new_bytes=len(new_text.encode("utf-8")),
|
||||
)
|
||||
except UnicodeDecodeError:
|
||||
return _json_result(False, error="file is not valid UTF-8 text", path=path)
|
||||
except (OSError, WorkspacePathError, ValueError) as exc:
|
||||
return _json_result(False, error=str(exc), path=path)
|
||||
|
||||
Reference in New Issue
Block a user