feat: 圆桌第3步大屏多页改用专职智能体 + 健壮 report-json 抽取
修复「大屏多页抽取 json 又失败」:旧实现复用画 HTML 的 roundtable-report 智能体 + rfind 围栏 + 单次 json.loads 的简单粗暴抽取,弱模型跑偏/格式偏差即失败。 - 新内置专职单例 roundtable-dashboard「大屏多页数据智能体」:SOUL 专讲逐席位 分析 → report-json 契约,禁工具/禁文件/禁 HTML;新 run policy dashboard 禁用一切工具 - harness 新增 report_json.py:多策略抽取(report-json/json/裸 JSON/截断)+ schema 归一化(移植前端 extractReportData/coerceReportData),返回可直接渲染的归一化 JSON - 后台 run_dashboard_data 改用专职 agent + dashboard policy + 健壮抽取 - 新增 /api/multi-agent/dashboard/init;前端 initDashboardThread + useStep3Dashboard 改用之 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
bb05f7b1de
commit
ed887f762e
9 files changed
+452
-29
No files matched your search
+271
@@ -0,0 +1,271 @@
|
||||
"""Step3「大屏多页」report-json 的健壮抽取 + 归一化(schema coercion)。
|
||||
|
||||
后台与前端共用同一份契约:内置「大屏多页智能体」(``roundtable-dashboard``) 通读各席位
|
||||
交付 + 总控共识,在回复末尾输出一个 ```report-json 代码块描述大屏数据。
|
||||
|
||||
模型实际输出常有偏差——用 ```json 而非 ```report-json、裸 JSON 无围栏、流式截断没闭合、
|
||||
尾逗号 / 单引号等。旧版后台用单一 ``rfind("```report-json")`` + ``json.loads`` 抽取,
|
||||
任何偏差都直接失败(即用户反馈的「大屏多页抽取 json 又失败」)。
|
||||
|
||||
本模块把前端 ``report-template.ts`` 里已验证的多策略抽取 + 字段归一化移植到后端:
|
||||
依次尝试 report-json / json / 无标注 代码块、未闭合围栏尾串、夹在文字里的平衡 ``{…}``
|
||||
对象;每个候选 ``json.loads`` 后过 :func:`coerce_report_data` 校验「像不像本契约」,
|
||||
命中即返回**归一化后的 JSON 字符串**——保证可被前端 ``JSON.parse`` 且字段齐全、
|
||||
``seats`` 的 ``dependsOn`` 只引用真实存在的席位 id。
|
||||
|
||||
纯逻辑、无 app 依赖,放在 harness 供进程内网关与测试复用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
_FENCE = "```report-json"
|
||||
# 契约关键键:用来判断一段 JSON「像不像」本大屏契约,避免把无关 {} 误当数据。
|
||||
_LOOKS_LIKE_RE = re.compile(r'"(seats|consensus|kpis|charts|timeline)"\s*:')
|
||||
# 收集所有 ```lang\n...``` 代码块(lang 可空)。
|
||||
_FENCE_RE = re.compile(r"```([^\n`]*)\r?\n(.*?)```", re.DOTALL)
|
||||
|
||||
|
||||
# ── 标量/容器归一化小工具(对齐前端 str/num/arr) ───────────────────────────────
|
||||
|
||||
|
||||
def _as_str(v: Any, fallback: str = "") -> str:
|
||||
return v if isinstance(v, str) else fallback
|
||||
|
||||
|
||||
def _as_num(v: Any) -> float | int:
|
||||
if isinstance(v, bool): # bool 是 int 子类,单独挡掉
|
||||
return 0
|
||||
if isinstance(v, (int, float)):
|
||||
n = v
|
||||
else:
|
||||
try:
|
||||
n = float(v)
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
if isinstance(n, float) and n.is_integer():
|
||||
return int(n)
|
||||
return n
|
||||
|
||||
|
||||
def _as_list(v: Any) -> list[Any]:
|
||||
return v if isinstance(v, list) else []
|
||||
|
||||
|
||||
def _looks_like_report_json(s: str) -> bool:
|
||||
return bool(_LOOKS_LIKE_RE.search(s))
|
||||
|
||||
|
||||
def _collect_fenced_blocks(text: str) -> list[tuple[str, str]]:
|
||||
return [(m.group(1).strip().lower(), m.group(2).strip()) for m in _FENCE_RE.finditer(text)]
|
||||
|
||||
|
||||
def _balanced_json_objects(text: str) -> list[str]:
|
||||
"""扫描出所有顶层平衡的 ``{…}`` 对象(字符串内的花括号不计),用于从夹杂文字里捞裸 JSON。"""
|
||||
out: list[str] = []
|
||||
depth = 0
|
||||
start = -1
|
||||
in_str = False
|
||||
esc = False
|
||||
for i, ch in enumerate(text):
|
||||
if in_str:
|
||||
if esc:
|
||||
esc = False
|
||||
elif ch == "\\":
|
||||
esc = True
|
||||
elif ch == '"':
|
||||
in_str = False
|
||||
continue
|
||||
if ch == '"':
|
||||
in_str = True
|
||||
elif ch == "{":
|
||||
if depth == 0:
|
||||
start = i
|
||||
depth += 1
|
||||
elif ch == "}" and depth > 0:
|
||||
depth -= 1
|
||||
if depth == 0 and start >= 0:
|
||||
out.append(text[start : i + 1])
|
||||
start = -1
|
||||
return out
|
||||
|
||||
|
||||
# ── schema coercion(对齐前端 coerceReportData) ───────────────────────────────
|
||||
|
||||
|
||||
def coerce_report_data(raw: Any) -> dict | None:
|
||||
"""把已 ``json.loads`` 的对象归一化成大屏契约 dict(缺字段给安全默认;非法 → None)。
|
||||
|
||||
规则与前端 ``coerceReportData`` 一致:seats 至少要有 name 才保留;dependsOn 仅保留
|
||||
真实存在且非自指的席位 id;kpis/charts/timeline 过滤空项并限量;整体既无 seats 又无
|
||||
最终结论 headline 时判为「不像本契约」→ None。
|
||||
"""
|
||||
if not isinstance(raw, dict):
|
||||
return None
|
||||
|
||||
seats: list[dict] = []
|
||||
for i, s in enumerate(_as_list(raw.get("seats"))):
|
||||
so = s if isinstance(s, dict) else {}
|
||||
name = _as_str(so.get("name")).strip()
|
||||
if not name:
|
||||
continue
|
||||
sid = _as_str(so.get("id")).strip() or f"seat-{i + 1}"
|
||||
sections = []
|
||||
for sec in _as_list(so.get("sections")):
|
||||
co = sec if isinstance(sec, dict) else {}
|
||||
heading = _as_str(co.get("heading"))
|
||||
bullets = [b for b in (_as_str(x) for x in _as_list(co.get("bullets"))) if b]
|
||||
if heading or bullets:
|
||||
sections.append({"heading": heading, "bullets": bullets})
|
||||
metrics = []
|
||||
for m in _as_list(so.get("metrics")):
|
||||
mo = m if isinstance(m, dict) else {}
|
||||
label = _as_str(mo.get("label"))
|
||||
if label:
|
||||
metrics.append({"label": label, "value": min(100, max(0, _as_num(mo.get("value"))))})
|
||||
risks = []
|
||||
for r in _as_list(so.get("risks")):
|
||||
ro = r if isinstance(r, dict) else {}
|
||||
label = _as_str(ro.get("label"))
|
||||
if not label:
|
||||
continue
|
||||
level = _as_str(ro.get("level"))
|
||||
risks.append({"label": label, "level": level if level in ("high", "mid", "low") else "mid"})
|
||||
depends_on = [d for d in (_as_str(x).strip() for x in _as_list(so.get("dependsOn"))) if d]
|
||||
seats.append(
|
||||
{
|
||||
"id": sid,
|
||||
"name": name,
|
||||
"summary": _as_str(so.get("summary")),
|
||||
"sections": sections,
|
||||
"metrics": metrics,
|
||||
"risks": risks,
|
||||
"dependsOn": depends_on,
|
||||
}
|
||||
)
|
||||
|
||||
# dependsOn 只保留真实存在的席位 id(模型可能编 id),且不自指。
|
||||
id_set = {s["id"] for s in seats}
|
||||
for s in seats:
|
||||
s["dependsOn"] = [d for d in s["dependsOn"] if d in id_set and d != s["id"]]
|
||||
|
||||
consensus_raw = raw.get("consensus") if isinstance(raw.get("consensus"), dict) else {}
|
||||
consensus = {
|
||||
"headline": _as_str(consensus_raw.get("headline")),
|
||||
"points": [p for p in (_as_str(x) for x in _as_list(consensus_raw.get("points"))) if p],
|
||||
}
|
||||
|
||||
kpis = []
|
||||
for k in _as_list(raw.get("kpis")):
|
||||
ko = k if isinstance(k, dict) else {}
|
||||
label = _as_str(ko.get("label"))
|
||||
value = _as_str(ko.get("value"), str(ko.get("value")) if ko.get("value") is not None else "")
|
||||
if not (label and value):
|
||||
continue
|
||||
item: dict[str, Any] = {"label": label, "value": value}
|
||||
unit = _as_str(ko.get("unit"))
|
||||
if unit:
|
||||
item["unit"] = unit
|
||||
trend = _as_str(ko.get("trend"))
|
||||
if trend in ("up", "down", "flat"):
|
||||
item["trend"] = trend
|
||||
kpis.append(item)
|
||||
if len(kpis) >= 6:
|
||||
break
|
||||
|
||||
charts = []
|
||||
for c in _as_list(raw.get("charts")):
|
||||
co = c if isinstance(c, dict) else {}
|
||||
ctype = _as_str(co.get("type"))
|
||||
items = []
|
||||
for it in _as_list(co.get("items")):
|
||||
io = it if isinstance(it, dict) else {}
|
||||
label = _as_str(io.get("label"))
|
||||
if label:
|
||||
items.append({"label": label, "value": _as_num(io.get("value"))})
|
||||
if not items:
|
||||
continue
|
||||
charts.append(
|
||||
{
|
||||
"type": ctype if ctype in ("bar", "donut", "radar", "line") else "bar",
|
||||
"title": _as_str(co.get("title")),
|
||||
"items": items,
|
||||
}
|
||||
)
|
||||
if len(charts) >= 3:
|
||||
break
|
||||
|
||||
timeline = []
|
||||
for t in _as_list(raw.get("timeline")):
|
||||
to = t if isinstance(t, dict) else {}
|
||||
label = _as_str(to.get("label"))
|
||||
if label:
|
||||
timeline.append({"label": label, "desc": _as_str(to.get("desc"))})
|
||||
|
||||
data = {
|
||||
"title": _as_str(raw.get("title"), "方案研讨大屏"),
|
||||
"subtitle": _as_str(raw.get("subtitle")),
|
||||
"kpis": kpis,
|
||||
"consensus": consensus,
|
||||
"charts": charts,
|
||||
"timeline": timeline,
|
||||
"seats": seats,
|
||||
}
|
||||
if not data["seats"] and not data["consensus"]["headline"]:
|
||||
return None
|
||||
return data
|
||||
|
||||
|
||||
# ── 多策略抽取 ───────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def extract_report_json(text: str | None) -> str | None:
|
||||
"""从模型回复里健壮抽取大屏 report-json,返回**归一化后的 JSON 字符串**(失败 → None)。
|
||||
|
||||
候选顺序(对齐前端 ``extractReportData``):
|
||||
1. 代码块,按 lang 优先级 report-json > json/jsonc > 无标注 > 其它,同级取靠后的;
|
||||
2. 未闭合围栏(流式截断):最后一个 ```report-json 之后到结尾;
|
||||
3. 夹在文字里的所有平衡 ``{…}`` 对象,契约形状的优先、靠后的先试。
|
||||
每个候选 ``json.loads`` 成功后过 :func:`coerce_report_data`,命中即返回归一化 JSON。
|
||||
"""
|
||||
if not text:
|
||||
return None
|
||||
|
||||
def rank(lang: str) -> int:
|
||||
if lang == "report-json":
|
||||
return 0
|
||||
if lang in ("json", "jsonc"):
|
||||
return 1
|
||||
if lang == "":
|
||||
return 2
|
||||
return 3
|
||||
|
||||
candidates: list[str] = []
|
||||
blocks = list(enumerate(_collect_fenced_blocks(text)))
|
||||
blocks.sort(key=lambda bi: (rank(bi[1][0]), -bi[0]))
|
||||
candidates.extend(body for _, (_, body) in blocks)
|
||||
|
||||
# 未闭合围栏(流式截断):取最后一个 report-json fence 之后到结尾。
|
||||
f = text.rfind(_FENCE)
|
||||
if f >= 0:
|
||||
candidates.append(text[f + len(_FENCE) :].strip())
|
||||
|
||||
# 裸 JSON(无围栏 / 夹在文字里):契约形状的先试,再试其余;都按靠后优先。
|
||||
objs = _balanced_json_objects(text)
|
||||
candidates.extend(o for o in reversed(objs) if _looks_like_report_json(o))
|
||||
candidates.extend(reversed(objs))
|
||||
|
||||
for body in candidates:
|
||||
if not body:
|
||||
continue
|
||||
try:
|
||||
raw = json.loads(body)
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
data = coerce_report_data(raw)
|
||||
if data is not None:
|
||||
return json.dumps(data, ensure_ascii=False)
|
||||
return None
|
||||
Reference in new issue
Block a user