Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix 话题回复和 session异常 (#2773)
DuTao82a90898a522
diff --git a/bot/tests/test_openviking_api_key_type.py b/bot/tests/test_openviking_api_key_type.pyindex 04eff0bf0..32055f600 100644--- a/bot/tests/test_openviking_api_key_type.py+++ b/bot/tests/test_openviking_api_key_type.py@@ -1219,6 +1219,35 @@ async def test_viking_client_append_messages_chunks_batches_at_server_limit(monkassert result == {"session_id": "session-1", "added": 101, "message_count": 101}+async def test_viking_client_ensure_session_creates_after_legacy_not_found(monkeypatch):+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))+ client = VikingClient(workspace_id="workspace")++ class NotFoundError(Exception):+ code = "NOT_FOUND"++ created = []++ async def _get_session(_session_id):+ raise NotFoundError("Resource not found")++ async def _create_session(session_id=None, memory_policy=None):+ created.append((session_id, memory_policy))+ return {"session_id": session_id, "memory_policy": memory_policy}++ monkeypatch.setattr(client.client, "get_session", _get_session)+ monkeypatch.setattr(client.client, "create_session", _create_session)++ result = await client.ensure_session(+ "session-1",+ memory_policy={"strategy": "compact"},+ )++ assert result == {"session_id": "session-1", "memory_policy": {"strategy": "compact"}}+ assert created == [("session-1", {"strategy": "compact"})]++@pytest.mark.asyncioasync def test_search_memory_uses_user_namespace(monkeypatch):monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))diff --git a/bot/vikingbot/channels/feishu.py b/bot/vikingbot/channels/feishu.pyindex 0f8be9e15..fdafc8551 100644--- a/bot/vikingbot/channels/feishu.py+++ b/bot/vikingbot/channels/feishu.py@@ -616,14 +616,9 @@ class FeishuChannel(BaseChannel):if reply_to_message_id:# Reply to existing message (quotes the original)- # Only reply in thread if the original message is in a topic (has root_id and is a thread)- should_reply_in_thread = False- if msg.metadata:- root_id = msg.metadata.get("root_id")- # Only use reply_in_thread=True if this is an actual topic group thread- # In Feishu, topic groups have root_id set for messages in threads- # root_id will be set if the message is already part of a thread- should_reply_in_thread = root_id is not None and root_id != reply_to_message_id+ should_reply_in_thread = self._should_reply_in_thread(+ msg.metadata, reply_to_message_id, msg.session_key.chat_id+ )request = (ReplyMessageRequest.builder()@@ -670,6 +665,30 @@ class FeishuChannel(BaseChannel):except Exception as e:logger.exception(f"Error sending Feishu message: {e}")+ @staticmethod+ def _should_reply_in_thread(+ metadata: dict[str, Any] | None,+ reply_to_message_id: str,+ session_chat_id: str | None = None,+ ) -> bool:+ """Return whether Feishu reply API should create a topic-thread reply."""+ if not metadata:+ return False++ if metadata.get("chat_type") != "group":+ return False++ # Normal group quoted replies can also carry root_id/parent_id. Only topic groups+ # should use reply_in_thread, otherwise Feishu turns a normal quoted reply into a thread.+ is_thread_chat = metadata.get("chat_mode") == "thread" or (+ bool(session_chat_id) and "#" in session_chat_id+ )+ if not is_thread_chat:+ return False++ root_id = metadata.get("root_id")+ return bool(root_id and root_id != reply_to_message_id)+def _on_message_sync(self, data: "P2ImMessageReceiveV1") -> None:"""Sync handler for incoming messages (called from WebSocket thread).@@ -1102,6 +1121,7 @@ class FeishuChannel(BaseChannel):# 9. 构建会话ID(处理话题群)reply_to = chat_id if chat_type == "group" else sender_idfinal_chat_id = chat_id+ chat_mode = Noneif chat_type == "group":chat_mode = await self._get_chat_mode(chat_id)@@ -1126,6 +1146,7 @@ class FeishuChannel(BaseChannel):"reply_to": reply_to,"msg_type": msg_type,"root_id": message.root_id,+ "chat_mode": chat_mode,"sender_id": sender_id,},)diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.pyindex 3cfa1fa95..715ce62ec 100644--- a/bot/vikingbot/openviking_mount/ov_server.py+++ b/bot/vikingbot/openviking_mount/ov_server.py@@ -973,13 +973,26 @@ class VikingClient:memory_policy: Optional[Dict[str, Any]] = None,) -> Dict[str, Any]:client = await self._session_client_for_user(user_id)- if await client.session_exists(session_id):+ try:return await client.get_session(session_id)+ except Exception as exc:+ if not self._is_not_found_error(exc):+ raise+return await client.create_session(session_id=session_id,memory_policy=memory_policy,)+ @staticmethod+ def _is_not_found_error(exc: Exception) -> bool:+ code = getattr(exc, "code", None)+ if code == "NOT_FOUND":+ return True+ if exc.__class__.__name__ == "NotFoundError":+ return True+ return "not found" in str(exc).lower()+async def get_session(self, session_id: str, user_id: Optional[str] = None) -> Dict[str, Any]:await self.ensure_session(session_id, user_id=user_id)client = await self._session_client_for_user(user_id)