|
@@ -32,11 +32,11 @@ from agent.trace.compaction import (
|
|
|
estimate_tokens,
|
|
estimate_tokens,
|
|
|
needs_level2_compression,
|
|
needs_level2_compression,
|
|
|
build_compression_prompt,
|
|
build_compression_prompt,
|
|
|
- build_reflect_prompt,
|
|
|
|
|
)
|
|
)
|
|
|
-from agent.memory.models import Skill
|
|
|
|
|
-from agent.memory.skill_loader import load_skills_from_dir
|
|
|
|
|
|
|
+from agent.skill.models import Skill
|
|
|
|
|
+from agent.skill.skill_loader import load_skills_from_dir
|
|
|
from agent.tools import ToolRegistry, get_tool_registry
|
|
from agent.tools import ToolRegistry, get_tool_registry
|
|
|
|
|
+from agent.tools.builtin.knowledge import KnowledgeConfig
|
|
|
from agent.core.prompts import (
|
|
from agent.core.prompts import (
|
|
|
DEFAULT_SYSTEM_PREFIX,
|
|
DEFAULT_SYSTEM_PREFIX,
|
|
|
TRUNCATION_HINT,
|
|
TRUNCATION_HINT,
|
|
@@ -46,7 +46,6 @@ from agent.core.prompts import (
|
|
|
TASK_NAME_GENERATION_SYSTEM_PROMPT,
|
|
TASK_NAME_GENERATION_SYSTEM_PROMPT,
|
|
|
TASK_NAME_FALLBACK,
|
|
TASK_NAME_FALLBACK,
|
|
|
SUMMARY_HEADER_TEMPLATE,
|
|
SUMMARY_HEADER_TEMPLATE,
|
|
|
- COMPLETION_REFLECT_PROMPT,
|
|
|
|
|
build_summary_header,
|
|
build_summary_header,
|
|
|
build_tool_interrupted_message,
|
|
build_tool_interrupted_message,
|
|
|
build_agent_continue_hint,
|
|
build_agent_continue_hint,
|
|
@@ -55,60 +54,6 @@ from agent.core.prompts import (
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
-# ===== 知识管理配置 =====
|
|
|
|
|
-
|
|
|
|
|
-@dataclass
|
|
|
|
|
-class KnowledgeConfig:
|
|
|
|
|
- """知识提取与注入的配置"""
|
|
|
|
|
-
|
|
|
|
|
- # 压缩时提取(消息量超阈值触发压缩时,在 Level 1 过滤前用完整 history 反思)
|
|
|
|
|
- enable_extraction: bool = True # 是否在压缩触发时提取知识
|
|
|
|
|
- reflect_prompt: str = "" # 自定义反思 prompt;空则使用默认,见 agent/core/prompts/knowledge.py:REFLECT_PROMPT
|
|
|
|
|
-
|
|
|
|
|
- # agent运行完成后提取(不代表任务完成,agent 可能中途退出等待人工评估)
|
|
|
|
|
- enable_completion_extraction: bool = True # 是否在运行完成后提取知识
|
|
|
|
|
- completion_reflect_prompt: str = "" # 自定义复盘 prompt;空则使用默认,见 agent/core/prompts/knowledge.py:COMPLETION_REFLECT_PROMPT
|
|
|
|
|
-
|
|
|
|
|
- # 知识注入(agent切换当前工作的goal时,自动注入相关知识)
|
|
|
|
|
- enable_injection: bool = True # 是否在 focus goal 时自动注入相关知识
|
|
|
|
|
-
|
|
|
|
|
- # 默认字段(保存/搜索时自动注入)
|
|
|
|
|
- owner: str = "" # 所有者(空则尝试从 git config user.email 获取,再空则用 agent:{agent_id})
|
|
|
|
|
- default_tags: Optional[Dict[str, str]] = None # 默认 tags(会与工具调用参数合并)
|
|
|
|
|
- default_scopes: Optional[List[str]] = None # 默认 scopes(空则用 ["org:cybertogether"])
|
|
|
|
|
- default_search_types: Optional[List[str]] = None # 默认搜索类型过滤
|
|
|
|
|
- default_search_owner: str = "" # 默认搜索 owner 过滤(空则不过滤)
|
|
|
|
|
-
|
|
|
|
|
- def get_reflect_prompt(self) -> str:
|
|
|
|
|
- """压缩时反思 prompt"""
|
|
|
|
|
- return self.reflect_prompt if self.reflect_prompt else build_reflect_prompt()
|
|
|
|
|
-
|
|
|
|
|
- def get_completion_reflect_prompt(self) -> str:
|
|
|
|
|
- """任务完成后复盘 prompt"""
|
|
|
|
|
- return self.completion_reflect_prompt if self.completion_reflect_prompt else COMPLETION_REFLECT_PROMPT
|
|
|
|
|
-
|
|
|
|
|
- def get_owner(self, agent_id: str = "agent") -> str:
|
|
|
|
|
- """获取 owner(优先级:配置 > git email > agent:{agent_id})"""
|
|
|
|
|
- if self.owner:
|
|
|
|
|
- return self.owner
|
|
|
|
|
-
|
|
|
|
|
- # 尝试从 git config 获取
|
|
|
|
|
- try:
|
|
|
|
|
- import subprocess
|
|
|
|
|
- result = subprocess.run(
|
|
|
|
|
- ["git", "config", "user.email"],
|
|
|
|
|
- capture_output=True,
|
|
|
|
|
- text=True,
|
|
|
|
|
- timeout=2,
|
|
|
|
|
- )
|
|
|
|
|
- if result.returncode == 0 and result.stdout.strip():
|
|
|
|
|
- return result.stdout.strip()
|
|
|
|
|
- except Exception:
|
|
|
|
|
- pass
|
|
|
|
|
-
|
|
|
|
|
- return f"agent:{agent_id}"
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
@dataclass
|
|
@dataclass
|
|
|
class ContextUsage:
|
|
class ContextUsage:
|
|
|
"""Context 使用情况"""
|
|
"""Context 使用情况"""
|
|
@@ -988,16 +933,6 @@ class AgentRunner:
|
|
|
args_display = args_str[:100] + "..." if len(args_str) > 100 else args_str
|
|
args_display = args_str[:100] + "..." if len(args_str) > 100 else args_str
|
|
|
logger.info(f"[Tool Call] {tool_name}({args_display})")
|
|
logger.info(f"[Tool Call] {tool_name}({args_display})")
|
|
|
|
|
|
|
|
- # 构建知识管理注入值
|
|
|
|
|
- inject_values = None
|
|
|
|
|
- if config.knowledge:
|
|
|
|
|
- inject_values = {
|
|
|
|
|
- "owner": config.knowledge.get_owner(config.uid or "agent"),
|
|
|
|
|
- "tags": config.knowledge.default_tags,
|
|
|
|
|
- "scopes": config.knowledge.default_scopes,
|
|
|
|
|
- "types": config.knowledge.default_search_types,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
tool_result = await self.tools.execute(
|
|
tool_result = await self.tools.execute(
|
|
|
tool_name,
|
|
tool_name,
|
|
|
tool_args,
|
|
tool_args,
|
|
@@ -1010,7 +945,6 @@ class AgentRunner:
|
|
|
"goal_tree": goal_tree,
|
|
"goal_tree": goal_tree,
|
|
|
"knowledge_config": config.knowledge,
|
|
"knowledge_config": config.knowledge,
|
|
|
},
|
|
},
|
|
|
- inject_values=inject_values,
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
# 如果是 goal 工具,记录执行后的状态
|
|
# 如果是 goal 工具,记录执行后的状态
|
|
@@ -1309,12 +1243,8 @@ class AgentRunner:
|
|
|
context={
|
|
context={
|
|
|
"store": self.trace_store,
|
|
"store": self.trace_store,
|
|
|
"trace_id": trace_id,
|
|
"trace_id": trace_id,
|
|
|
|
|
+ "knowledge_config": config.knowledge,
|
|
|
},
|
|
},
|
|
|
- inject_values={
|
|
|
|
|
- "owner": config.knowledge.get_owner(config.uid or "agent"),
|
|
|
|
|
- "tags": config.knowledge.default_tags,
|
|
|
|
|
- "scopes": config.knowledge.default_scopes,
|
|
|
|
|
- } if config.knowledge else None,
|
|
|
|
|
)
|
|
)
|
|
|
saved_count += 1
|
|
saved_count += 1
|
|
|
except Exception as e:
|
|
except Exception as e:
|