fix(clarification): 修复 options 字符串被逐字符渲染为有序列表项
- 为 ask_clarification 增加 options 归一化处理 - 支持 JSON 数组字符串解析为选项列表 - 非 JSON 字符串降级为单个选项,避免逐字编号
This commit is contained in:
parent
08e8de5e3e
commit
927edfb610
|
|
@ -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}")
|
||||
|
|
|
|||
Loading…
Reference in New Issue