diff --git a/backend/packages/harness/deerflow/agents/middlewares/clarification_middleware.py b/backend/packages/harness/deerflow/agents/middlewares/clarification_middleware.py index 5074d8d2..dfcc41b2 100644 --- a/backend/packages/harness/deerflow/agents/middlewares/clarification_middleware.py +++ b/backend/packages/harness/deerflow/agents/middlewares/clarification_middleware.py @@ -1,5 +1,6 @@ """Middleware for intercepting clarification requests and presenting them to the user.""" +import json import logging from collections.abc import Callable from typing import override @@ -35,6 +36,28 @@ class ClarificationMiddleware(AgentMiddleware[ClarificationMiddlewareState]): state_schema = ClarificationMiddlewareState + def _normalize_options(self, options: object) -> list[str]: + """Normalize clarification options into a list of display strings.""" + if options is None: + return [] + + if isinstance(options, list): + return [str(option) for option in options] + + if isinstance(options, str): + stripped = options.strip() + if not stripped: + return [] + try: + parsed = json.loads(stripped) + except json.JSONDecodeError: + return [stripped] + if isinstance(parsed, list): + return [str(option) for option in parsed] + return [str(parsed)] + + return [str(options)] + def _is_chinese(self, text: str) -> bool: """Check if text contains Chinese characters. @@ -58,7 +81,7 @@ class ClarificationMiddleware(AgentMiddleware[ClarificationMiddlewareState]): question = args.get("question", "") clarification_type = args.get("clarification_type", "missing_info") context = args.get("context") - options = args.get("options", []) + options = self._normalize_options(args.get("options")) # Type-specific icons type_icons = { @@ -84,7 +107,7 @@ class ClarificationMiddleware(AgentMiddleware[ClarificationMiddlewareState]): message_parts.append(f"{icon} {question}") # Add options in a cleaner format - if options and len(options) > 0: + if options: message_parts.append("") # blank line for spacing for i, option in enumerate(options, 1): message_parts.append(f" {i}. {option}")