fix: 修正一个Maibot遇不到但是不能涵盖Toml整个格式的问题

pull/1389/head
Ronifue 2025-11-29 16:16:31 +08:00
parent f68b9aa109
commit 08c0b5929e
1 changed files with 5 additions and 8 deletions

View File

@ -25,14 +25,11 @@ def _format_toml_value(obj: Any, threshold: int, depth: int = 0) -> Any:
# 处理列表类型 (list 或 Array)
if isinstance(obj, (list, Array)):
# 如果包含字典/表,视为 AoT 的列表形式或内联表数组,保持结构递归处理
if obj and isinstance(obj[0], (dict, Table)):
# 如果是纯 list (非 tomlkit Array) 且包含字典/表,视为 AoT 的列表形式
# 保持结构递归处理,避免转换为 Inline Table Array (因为 Inline Table 必须单行,复杂对象不友好)
if isinstance(obj, list) and not isinstance(obj, Array) and obj and isinstance(obj[0], (dict, Table)):
for i, item in enumerate(obj):
# 原地修改或递归处理
if isinstance(obj, list):
obj[i] = _format_toml_value(item, threshold, depth)
else:
_format_toml_value(item, threshold, depth)
obj[i] = _format_toml_value(item, threshold, depth)
return obj
# 决定是否多行:仅在顶层且长度超过阈值时
@ -67,4 +64,4 @@ def save_toml_with_format(data: Any, file_path: str, multiline_threshold: int =
def format_toml_string(data: Any, multiline_threshold: int = 1) -> str:
"""格式化 TOML 数据并返回字符串"""
formatted = _format_toml_value(data, multiline_threshold) if multiline_threshold >= 0 else data
return tomlkit.dumps(formatted)
return tomlkit.dumps(formatted)