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:
2026-05-14 09:43:48 +08:00
parent 8a12c30141
commit 30ab74ffb2
149 changed files with 12293 additions and 2812 deletions

View File

@ -1,19 +1,22 @@
"""LLM-driven skill assembler.
这层现在不再自己做规则打分,而是直接把:
这层现在不再自己做规则打分,而是分两步把:
1. task description
2. embedding 召回后的候选 skill 摘要
3. 粗选候选的完整 skill 正文
交给一个模型来决定本轮要激活哪些 skill。
当前目标非常克制:
- 输入尽量简单
- 主 agent 不拿 skill_view也不动态探索技能库
- SkillAssembler 可以在系统侧内部读取候选 skill 正文
- 输出只要 skill 名称
- 没有命中就返回空 skills
"""
from __future__ import annotations
import asyncio
from dataclasses import dataclass, field
import json
from typing import Any
@ -31,6 +34,7 @@ class SkillAssemblyResult:
"""一次装配后真正要注入当前 run 的 skills。"""
activated_skills: list[SkillContext] = field(default_factory=list)
llm_interactions: list[dict[str, Any]] = field(default_factory=list)
class SkillAssembler:
@ -40,9 +44,14 @@ class SkillAssembler:
self,
loader: SkillsLoader,
retriever: SkillEmbeddingRetriever | None = None,
*,
max_detailed_candidates: int = 5,
max_candidate_content_chars: int = 6000,
) -> None:
self.loader = loader
self.retriever = retriever or SkillEmbeddingRetriever()
self.max_detailed_candidates = max(1, max_detailed_candidates)
self.max_candidate_content_chars = max(1000, max_candidate_content_chars)
async def assemble(
self,
@ -51,6 +60,7 @@ class SkillAssembler:
provider: LLMProvider,
model: str,
embedding_runtime: ProviderRuntime | None = None,
thinking_enabled: bool | None = None,
top_k: int = 12,
) -> SkillAssemblyResult:
candidates = self.loader.build_selection_candidates()
@ -71,15 +81,39 @@ class SkillAssembler:
)
if not candidates:
return SkillAssemblyResult()
llm_interactions: list[dict[str, Any]] = []
if len(candidates) <= self.max_detailed_candidates:
shortlisted_names = [item["name"] for item in candidates]
else:
shortlisted_names = await self._select_skill_names(
task_description=task_description,
candidates=candidates,
provider=provider,
model=model,
thinking_enabled=thinking_enabled,
max_selected=self.max_detailed_candidates,
selection_stage="shortlist",
llm_interactions=llm_interactions,
)
if not shortlisted_names:
return SkillAssemblyResult(llm_interactions=llm_interactions)
detailed_candidates = self._build_detailed_candidates(
candidates=candidates,
selected_names=shortlisted_names,
)
selected_names = await self._select_skill_names(
task_description=task_description,
candidates=candidates,
candidates=detailed_candidates,
provider=provider,
model=model,
thinking_enabled=thinking_enabled,
selection_stage="final",
llm_interactions=llm_interactions,
)
if not selected_names:
return SkillAssemblyResult()
return SkillAssemblyResult(llm_interactions=llm_interactions)
activated_skills: list[SkillContext] = []
for name in selected_names:
@ -99,7 +133,7 @@ class SkillAssembler:
)
)
return SkillAssemblyResult(activated_skills=activated_skills)
return SkillAssemblyResult(activated_skills=activated_skills, llm_interactions=llm_interactions)
async def _select_skill_names(
self,
@ -108,17 +142,28 @@ class SkillAssembler:
candidates: list[dict[str, str]],
provider: LLMProvider,
model: str,
thinking_enabled: bool | None = None,
max_selected: int | None = None,
selection_stage: str = "final",
llm_interactions: list[dict[str, Any]] | None = None,
timeout_seconds: float = 8.0,
) -> list[str]:
candidate_summary = self._render_candidates(candidates)
candidate_names = {item["name"] for item in candidates}
selection_instruction = (
f"Return at most {max_selected} names for detailed inspection. "
if max_selected is not None
else "Return the final skill names to activate. "
)
messages = [
{
"role": "system",
"content": (
"You select Beaver skills for a single run. "
"Given a task description and candidate skill summaries, "
"Given a task description and candidate skill information, "
"return only a JSON array of skill names to activate. "
"Do not invent names. If nothing matches, return []."
"Do not invent names. If nothing matches, return []. "
f"Selection stage: {selection_stage}. {selection_instruction}"
),
},
{
@ -130,13 +175,34 @@ class SkillAssembler:
),
},
]
response = await provider.chat(
messages=messages,
tools=None,
model=model,
max_tokens=512,
temperature=0,
)
chat_kwargs: dict[str, Any] = {
"messages": messages,
"tools": None,
"model": model,
"max_tokens": 256,
"temperature": 0,
}
if thinking_enabled is not None:
chat_kwargs["thinking_enabled"] = thinking_enabled
try:
response = await asyncio.wait_for(provider.chat(**chat_kwargs), timeout=timeout_seconds)
except Exception:
return []
if llm_interactions is not None:
llm_interactions.append(
{
"stage": selection_stage,
"model": model,
"messages": messages,
"response": {
"content": response.content,
"finish_reason": response.finish_reason,
"provider_name": response.provider_name,
"model": response.model,
"usage": response.usage,
},
}
)
if response.finish_reason == "error" or not response.content:
return []
@ -149,15 +215,42 @@ class SkillAssembler:
for name in parsed:
if name in candidate_names and name not in filtered:
filtered.append(name)
return filtered
return filtered[:max_selected] if max_selected is not None else filtered
@staticmethod
def _render_candidates(candidates: list[dict[str, str]]) -> str:
lines: list[str] = []
for item in candidates:
lines.append(f"- {item['name']}: {item['description']}")
content = item.get("content")
if content:
lines.append(
f"## {item['name']}\n"
f"Description: {item['description']}\n"
f"Skill content:\n{content}"
)
else:
lines.append(f"- {item['name']}: {item['description']}")
return "\n".join(lines)
def _build_detailed_candidates(
self,
*,
candidates: list[dict[str, str]],
selected_names: list[str],
) -> list[dict[str, str]]:
by_name = {item["name"]: item for item in candidates}
detailed: list[dict[str, str]] = []
for name in selected_names:
candidate = by_name.get(name)
if candidate is None:
continue
raw_content = self.loader.load_published_skill(name)
content = strip_frontmatter(raw_content).strip() if raw_content else ""
if len(content) > self.max_candidate_content_chars:
content = content[: self.max_candidate_content_chars].rstrip() + "\n...[truncated]"
detailed.append({**candidate, "content": content})
return detailed
@staticmethod
def _parse_selected_names(content: str) -> list[str]:
cleaned = content.strip()