run_knowledge_manager.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Knowledge Manager Agent - 启动脚本
  3. 作为独立的 IM Client 运行,监听并处理知识库查询和保存请求。
  4. """
  5. import asyncio
  6. import json
  7. import logging
  8. import os
  9. import sys
  10. from pathlib import Path
  11. # 添加项目根目录到 Python 路径
  12. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  13. from dotenv import load_dotenv
  14. load_dotenv()
  15. logging.basicConfig(
  16. level=logging.INFO,
  17. format="%(asctime)s [KM] %(message)s"
  18. )
  19. logger = logging.getLogger(__name__)
  20. async def main():
  21. """启动 Knowledge Manager Agent"""
  22. # 配置
  23. contact_id = os.getenv("KNOWLEDGE_MANAGER_CONTACT_ID", "knowledge_manager")
  24. server_url = os.getenv("IM_SERVER_URL", "ws://localhost:8005")
  25. chat_id = "main"
  26. logger.info(f"正在启动 Knowledge Manager...")
  27. logger.info(f" - Contact ID: {contact_id}")
  28. logger.info(f" - Server: {server_url}")
  29. logger.info(f" - Chat ID: {chat_id}")
  30. # 导入 IM Client
  31. try:
  32. sys.path.insert(0, str(Path(__file__).parent.parent.parent / "im-client"))
  33. from client import IMClient
  34. except ImportError as e:
  35. logger.error(f"无法导入 IM Client: {e}")
  36. return
  37. # 创建 IM Client
  38. client = IMClient(
  39. contact_id=contact_id,
  40. server_url=server_url,
  41. data_dir=Path.home() / ".knowhub" / "im_data"
  42. )
  43. # 连接到服务器
  44. try:
  45. await client.connect(chat_id=chat_id)
  46. logger.info("✅ 已连接到 IM Server")
  47. except Exception as e:
  48. logger.error(f"连接失败: {e}")
  49. return