myagent-ai 1.11.1 → 1.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agents/main_agent.py +3 -1
- package/core/context_builder.py +34 -16
- package/package.json +1 -1
- package/web/api_server.py +19 -0
package/agents/main_agent.py
CHANGED
|
@@ -293,7 +293,9 @@ class MainAgent(BaseAgent):
|
|
|
293
293
|
auto_kb_dir.mkdir(parents=True, exist_ok=True)
|
|
294
294
|
|
|
295
295
|
# 使用 session_id 作为文件名(取前8位避免过长)
|
|
296
|
-
|
|
296
|
+
# 注意: session_id 可能包含 '/' (来自 agent_path 如 "coder/python-expert"),
|
|
297
|
+
# 必须替换为安全字符,避免创建意外的子目录
|
|
298
|
+
safe_session = session_id.replace("-", "").replace("/", "_")[:12] if session_id else "default"
|
|
297
299
|
kb_file = auto_kb_dir / f"{safe_session}.md"
|
|
298
300
|
|
|
299
301
|
now_str = datetime.now().strftime("%Y-%m-%d %H:%M")
|
package/core/context_builder.py
CHANGED
|
@@ -69,6 +69,8 @@ class ContextBuilder:
|
|
|
69
69
|
self.skill_registry = skill_registry
|
|
70
70
|
self.knowledge_base_dir = knowledge_base_dir
|
|
71
71
|
self.max_dialog_chars = max_dialog_chars
|
|
72
|
+
# Agent 专属知识库目录(由调用方动态设置,优先于组织知识库)
|
|
73
|
+
self.agent_knowledge_dir: Optional[str] = None
|
|
72
74
|
|
|
73
75
|
# =========================================================================
|
|
74
76
|
# 公共接口
|
|
@@ -248,8 +250,9 @@ class ContextBuilder:
|
|
|
248
250
|
"""
|
|
249
251
|
构建 <knowledge> 段落 —— 知识库 RAG 检索结果。
|
|
250
252
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
+
搜索优先级:
|
|
254
|
+
1. Agent 专属知识库 (agent_knowledge_dir) —— 如果有且非空
|
|
255
|
+
2. 组织知识库 (knowledge_base_dir) —— 兜底
|
|
253
256
|
|
|
254
257
|
Args:
|
|
255
258
|
query: 搜索查询文本(通常为 <get_knowledge> 内容或用户消息)
|
|
@@ -257,31 +260,46 @@ class ContextBuilder:
|
|
|
257
260
|
Returns:
|
|
258
261
|
<knowledge> XML 段落字符串
|
|
259
262
|
"""
|
|
260
|
-
|
|
263
|
+
# 优先搜索 Agent 专属知识库
|
|
264
|
+
if self.agent_knowledge_dir:
|
|
265
|
+
agent_result = self._search_knowledge_dir(self.agent_knowledge_dir, query, top_k=5)
|
|
266
|
+
if agent_result:
|
|
267
|
+
return agent_result
|
|
268
|
+
|
|
269
|
+
# 回退到组织知识库
|
|
270
|
+
if self.knowledge_base_dir:
|
|
271
|
+
org_result = self._search_knowledge_dir(self.knowledge_base_dir, query, top_k=5)
|
|
272
|
+
if org_result:
|
|
273
|
+
return org_result
|
|
274
|
+
|
|
275
|
+
# 都没有配置或都为空
|
|
276
|
+
if not self.knowledge_base_dir and not self.agent_knowledge_dir:
|
|
261
277
|
return "<knowledge>\n(知识库未配置)\n</knowledge>"
|
|
278
|
+
return "<knowledge>\n(未找到相关知识)\n</knowledge>"
|
|
279
|
+
|
|
280
|
+
def _search_knowledge_dir(self, kb_dir: str, query: str, top_k: int = 5) -> str:
|
|
281
|
+
"""在指定知识库目录中执行 RAG 搜索并格式化结果"""
|
|
282
|
+
import os as _os
|
|
262
283
|
|
|
263
284
|
if not query.strip():
|
|
264
|
-
return "
|
|
285
|
+
return ""
|
|
286
|
+
|
|
287
|
+
if not kb_dir or not _os.path.isdir(kb_dir):
|
|
288
|
+
return ""
|
|
265
289
|
|
|
266
290
|
try:
|
|
267
291
|
from knowledge.rag import KnowledgeRAG
|
|
268
|
-
import os as _os
|
|
269
|
-
|
|
270
|
-
kb_path = self.knowledge_base_dir
|
|
271
|
-
if not _os.path.isdir(kb_path):
|
|
272
|
-
logger.debug(f"知识库目录不存在: {kb_path}")
|
|
273
|
-
return "<knowledge>\n(知识库目录不存在)\n</knowledge>"
|
|
274
292
|
|
|
275
|
-
rag = KnowledgeRAG(kb_dir=
|
|
293
|
+
rag = KnowledgeRAG(kb_dir=kb_dir)
|
|
276
294
|
rag.build_index()
|
|
277
295
|
|
|
278
296
|
if rag.total_chunks == 0:
|
|
279
|
-
return "
|
|
297
|
+
return ""
|
|
280
298
|
|
|
281
|
-
results = rag.search(query, top_k=
|
|
299
|
+
results = rag.search(query, top_k=top_k)
|
|
282
300
|
|
|
283
301
|
if not results:
|
|
284
|
-
return "
|
|
302
|
+
return ""
|
|
285
303
|
|
|
286
304
|
lines: List[str] = ["<knowledge>"]
|
|
287
305
|
for i, chunk in enumerate(results, 1):
|
|
@@ -297,8 +315,8 @@ class ContextBuilder:
|
|
|
297
315
|
return "\n".join(lines)
|
|
298
316
|
|
|
299
317
|
except Exception as e:
|
|
300
|
-
logger.warning(f"知识库 RAG
|
|
301
|
-
return "
|
|
318
|
+
logger.warning(f"知识库 RAG 检索失败 ({kb_dir}): {e}")
|
|
319
|
+
return ""
|
|
302
320
|
|
|
303
321
|
def _build_recent_dialog(
|
|
304
322
|
self,
|
package/package.json
CHANGED
package/web/api_server.py
CHANGED
|
@@ -3153,6 +3153,11 @@ class ApiServer:
|
|
|
3153
3153
|
# 标记执行模式(传递给 MainAgent 用于增强 system prompt)
|
|
3154
3154
|
if self.core.main_agent:
|
|
3155
3155
|
self.core.main_agent._chat_mode = chat_mode
|
|
3156
|
+
# 设置 Agent 专属知识库目录
|
|
3157
|
+
if agent_path and self.core.main_agent and self.core.main_agent.context_builder:
|
|
3158
|
+
agent_kb_dir = self._get_agent_knowledge_dir(agent_path)
|
|
3159
|
+
if agent_kb_dir.exists() and any(agent_kb_dir.iterdir()):
|
|
3160
|
+
self.core.main_agent.context_builder.agent_knowledge_dir = str(agent_kb_dir)
|
|
3156
3161
|
|
|
3157
3162
|
try:
|
|
3158
3163
|
response = await self.core.process_message(message, session_id)
|
|
@@ -3161,6 +3166,8 @@ class ApiServer:
|
|
|
3161
3166
|
self.core.main_agent._agent_override_prompt = None
|
|
3162
3167
|
self.core.main_agent._agent_override_path = None
|
|
3163
3168
|
self.core.main_agent._chat_mode = ""
|
|
3169
|
+
if self.core.main_agent.context_builder:
|
|
3170
|
+
self.core.main_agent.context_builder.agent_knowledge_dir = None
|
|
3164
3171
|
|
|
3165
3172
|
# 检查是否成功(如果回复包含错误标记)
|
|
3166
3173
|
if response and not response.startswith("⚠️ LLM 调用失败") and not response.startswith("❌"):
|
|
@@ -3289,6 +3296,15 @@ class ApiServer:
|
|
|
3289
3296
|
_original_exec_mode = agent.executor.execution_mode
|
|
3290
3297
|
agent.executor.set_execution_mode(_exec_mode)
|
|
3291
3298
|
|
|
3299
|
+
# ── 设置 Agent 专属知识库目录(优先于组织知识库)──
|
|
3300
|
+
if agent_path and agent.context_builder:
|
|
3301
|
+
agent_kb_dir = self._get_agent_knowledge_dir(agent_path)
|
|
3302
|
+
if agent_kb_dir.exists() and any(agent_kb_dir.iterdir()):
|
|
3303
|
+
agent.context_builder.agent_knowledge_dir = str(agent_kb_dir)
|
|
3304
|
+
logger.debug(f"[{session_id}] 使用 Agent 专属知识库: {agent_kb_dir}")
|
|
3305
|
+
else:
|
|
3306
|
+
agent.context_builder.agent_knowledge_dir = None
|
|
3307
|
+
|
|
3292
3308
|
# Clear execution events from previous runs
|
|
3293
3309
|
agent.clear_execution_events()
|
|
3294
3310
|
|
|
@@ -3370,6 +3386,9 @@ class ApiServer:
|
|
|
3370
3386
|
# 恢复执行引擎原始模式(防止影响后续 Agent 请求)
|
|
3371
3387
|
if _original_exec_mode is not None and agent.executor:
|
|
3372
3388
|
agent.executor.set_execution_mode(_original_exec_mode)
|
|
3389
|
+
# 清理 Agent 专属知识库目录设置(防止影响其他 Agent 请求)
|
|
3390
|
+
if agent.context_builder:
|
|
3391
|
+
agent.context_builder.agent_knowledge_dir = None
|
|
3373
3392
|
|
|
3374
3393
|
# V2 结束后:如果 task_list_store 中有任务,确保最终推送一次
|
|
3375
3394
|
if chat_mode == "exec" and session_id in self._task_list_store:
|