From a688e6e25e8a6f18da6cb76220c5badeb7e37ed1 Mon Sep 17 00:00:00 2001 From: qixinbo Date: Tue, 17 Mar 2026 16:13:33 +0800 Subject: [PATCH] revert nanobot --- backend/app/core/nanobot.py | 20 +++++++++++++++++++- nanobot/nanobot/providers/base.py | 9 --------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/backend/app/core/nanobot.py b/backend/app/core/nanobot.py index 5b01907..2decb1e 100644 --- a/backend/app/core/nanobot.py +++ b/backend/app/core/nanobot.py @@ -2,7 +2,7 @@ import asyncio import sys import os from pathlib import Path -from typing import List, Callable, Awaitable +from typing import List, Callable, Awaitable, Any # Add project root to sys.path to allow importing nanobot # Assuming backend/app/core/nanobot.py -> backend/app/core -> backend/app -> backend -> root @@ -255,6 +255,12 @@ class NanobotIntegration: # Append user message after skills full_message = f"{skill_context}\n\n{message}" + session = agent_to_use.sessions.get_or_create(session_id) + normalized_messages = self._normalize_session_messages(session.messages) + if len(normalized_messages) != len(session.messages): + session.messages = normalized_messages + agent_to_use.sessions.save(session) + response = await agent_to_use.process_direct( full_message, session_key=session_id, @@ -264,4 +270,16 @@ class NanobotIntegration: ) return response + def _normalize_session_messages(self, messages: List[Any]) -> List[dict[str, Any]]: + normalized: List[dict[str, Any]] = [] + stack: List[Any] = list(messages) + while stack: + current = stack.pop(0) + if isinstance(current, dict): + normalized.append(current) + continue + if isinstance(current, list): + stack = list(current) + stack + return normalized + nanobot_service = NanobotIntegration() diff --git a/nanobot/nanobot/providers/base.py b/nanobot/nanobot/providers/base.py index edc501b..f3729b7 100644 --- a/nanobot/nanobot/providers/base.py +++ b/nanobot/nanobot/providers/base.py @@ -95,15 +95,6 @@ class LLMProvider(ABC): """Keep only provider-safe message keys and normalize assistant content.""" sanitized = [] for msg in messages: - if isinstance(msg, list): - for nested in msg: - if not isinstance(nested, dict): - continue - clean = {k: v for k, v in nested.items() if k in allowed_keys} - if clean.get("role") == "assistant" and "content" not in clean: - clean["content"] = None - sanitized.append(clean) - continue if not isinstance(msg, dict): continue clean = {k: v for k, v in msg.items() if k in allowed_keys}