feat: 新增xbot agent核心代码
新增agent/app/xbot模块,包含: - agent.py: agent核心逻辑 - config.py: 配置管理 - session.py: 会话管理 - memory.py: 记忆管理 - loop.py: 循环任务 - adapter.py: 适配器 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,9 +6,16 @@ from typing import Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from .loop import AgentLoop
|
||||
from .memory import MemoryConsolidator
|
||||
from .session import SessionManager
|
||||
from .adapter import XBotLLMAdapter, LLMResponse
|
||||
from . import config
|
||||
|
||||
# 尝试导入 simplemem
|
||||
try:
|
||||
from simplemem import SimpleMemSystem
|
||||
HAS_SIMPLEMEM = True
|
||||
except ImportError:
|
||||
HAS_SIMPLEMEM = False
|
||||
|
||||
|
||||
class SimpleToolRegistry:
|
||||
@@ -84,6 +91,8 @@ class XBotAgent:
|
||||
base_url: Optional[str] = None,
|
||||
workspace: Optional[Path] = None,
|
||||
context_window_tokens: int = 200000,
|
||||
embedding_model: Optional[str] = None,
|
||||
embedding_base_url: Optional[str] = None,
|
||||
):
|
||||
"""
|
||||
初始化 XBot Agent
|
||||
@@ -101,9 +110,15 @@ class XBotAgent:
|
||||
self.name = name
|
||||
self.role_description = role_description
|
||||
|
||||
# 创建工作目录
|
||||
# 使用配置文件的默认值
|
||||
if api_key is None:
|
||||
api_key = config.API_KEY
|
||||
if base_url is None:
|
||||
base_url = config.BASE_URL
|
||||
if workspace is None:
|
||||
workspace = Path(os.getenv("XAGENT_WORKSPACE", "./xbot_workspace"))
|
||||
workspace = Path(config.WORKSPACE)
|
||||
|
||||
# 创建工作目录
|
||||
self.workspace = workspace
|
||||
self.workspace.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -130,14 +145,35 @@ class XBotAgent:
|
||||
# 创建会话管理器
|
||||
self.sessions = SessionManager(self.workspace)
|
||||
|
||||
# 创建内存压缩器
|
||||
self.memory = MemoryConsolidator(
|
||||
workspace=self.workspace,
|
||||
provider=self.provider,
|
||||
model=model,
|
||||
sessions=self.sessions,
|
||||
context_window_tokens=context_window_tokens,
|
||||
)
|
||||
# 创建 SimpleMem 记忆系统
|
||||
if HAS_SIMPLEMEM and api_key and config.ENABLE_SIMPLEMEM:
|
||||
# 使用配置文件的 embedding 设置
|
||||
emb_model = embedding_model or config.EMBEDDING_MODEL
|
||||
emb_base = embedding_base_url or config.EMBEDDING_BASE_URL or base_url
|
||||
|
||||
self.memory = SimpleMemSystem(
|
||||
api_key=api_key,
|
||||
base_url=emb_base,
|
||||
model=model,
|
||||
embedding_model=emb_model,
|
||||
db_path=str(self.workspace / "memory_db"),
|
||||
clear_db=False,
|
||||
# 并行处理配置
|
||||
enable_parallel_processing=config.ENABLE_PARALLEL_PROCESSING,
|
||||
max_parallel_workers=config.MAX_PARALLEL_WORKERS,
|
||||
enable_parallel_retrieval=config.ENABLE_PARALLEL_RETRIEVAL,
|
||||
max_retrieval_workers=config.MAX_RETRIEVAL_WORKERS,
|
||||
enable_planning=config.ENABLE_PLANNING,
|
||||
enable_reflection=config.ENABLE_REFLECTION,
|
||||
max_reflection_rounds=config.MAX_REFLECTION_ROUNDS,
|
||||
)
|
||||
self._use_simplemem = True
|
||||
print(f"SimpleMem initialized with embedding: {emb_model}, base_url: {emb_base}")
|
||||
else:
|
||||
self.memory = None
|
||||
self._use_simplemem = False
|
||||
if not api_key:
|
||||
print("Warning: No API key provided, SimpleMem will be disabled")
|
||||
|
||||
def _register_default_tools(self) -> None:
|
||||
"""注册默认工具"""
|
||||
@@ -191,6 +227,17 @@ class XBotAgent:
|
||||
|
||||
请根据用户的问题回答,并使用 Markdown 格式输出。"""
|
||||
|
||||
# 如果使用 SimpleMem,检索相关记忆
|
||||
memory_context = ""
|
||||
if self._use_simplemem and self.memory:
|
||||
try:
|
||||
memory_context = self.memory.ask(user_input)
|
||||
except Exception as e:
|
||||
print(f"Memory retrieval error: {e}")
|
||||
|
||||
if memory_context:
|
||||
system_prompt += f"\n\n相关记忆:\n{memory_context}"
|
||||
|
||||
# 获取历史消息
|
||||
history = session.get_history(max_messages=50)
|
||||
|
||||
@@ -210,8 +257,14 @@ class XBotAgent:
|
||||
session.messages.append(m)
|
||||
self.sessions.save(session)
|
||||
|
||||
# 尝试内存压缩
|
||||
await self.memory.maybe_consolidate_by_tokens(session)
|
||||
# 保存到 SimpleMem 记忆
|
||||
if self._use_simplemem and self.memory and final_content:
|
||||
try:
|
||||
self.memory.add_dialogue("User", user_input, datetime.now().isoformat())
|
||||
self.memory.add_dialogue(self.name, final_content, datetime.now().isoformat())
|
||||
self.memory.finalize()
|
||||
except Exception as e:
|
||||
print(f"Memory save error: {e}")
|
||||
|
||||
return {
|
||||
"content": final_content or "No response",
|
||||
|
||||
Reference in New Issue
Block a user