File size: 5,180 Bytes
42cbd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6533267
42cbd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6533267
 
 
 
 
42cbd80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""Build bounded, source-backed answer visuals without asking an LLM for numbers."""
from __future__ import annotations

import math
import re
from collections.abc import Mapping, Sequence
from typing import Any, Literal

from pydantic import BaseModel, Field


class ChartPoint(BaseModel):
    label: str
    value: float


class AnswerChart(BaseModel):
    id: str
    type: Literal["bar"] = "bar"
    title: str
    unit: str = ""
    points: list[ChartPoint]
    source: str
    trace_id: str
    as_of: str | None = None
    total_rows: int


class AnswerPresentation(BaseModel):
    version: Literal[1] = 1
    status: Literal["ready", "partial", "empty"]
    successful_tools: int
    total_tools: int
    warnings: list[str] = Field(default_factory=list)
    charts: list[AnswerChart] = Field(default_factory=list)


_IDENTIFIER = re.compile(r"代码|编码|序号|排名|日期|时间|code|(^|_)id$|rank|date|time", re.I)
_LABEL = re.compile(r"简称|名称|名字|日期|name|label|date", re.I)
_NUMBER = re.compile(r"^([+-]?(?:\d+(?:\.\d*)?|\.\d+))\s*(%|亿|万|千|元|股|倍)?$")
_METRICS = ("市盈率", "市净率", "股息率", "营业收入", "营收", "净利润", "市值", "涨跌幅", "成交额", "换手率", "净流入", "毛利率", "收益率")


def _number(value: Any) -> tuple[float, str] | None:
    if isinstance(value, bool) or value is None:
        return None
    if isinstance(value, int | float):
        number = float(value)
        return (number, "") if math.isfinite(number) else None
    if not isinstance(value, str):
        return None
    match = _NUMBER.fullmatch(value.strip().replace(",", "").replace(",", "").replace("%", "%"))
    if not match:
        return None
    number = float(match[1])
    return (number, match[2] or "") if math.isfinite(number) else None


def _charts(call: Mapping[str, Any], limit: int) -> list[AnswerChart]:
    # Only structured financial records are comparable; news text is never
    # mined for numbers, and identifiers are never treated as measurements.
    if call.get("name") != "financial-query" or not call.get("ok"):
        return []
    result = call.get("result")
    if not isinstance(result, dict):
        return []
    data = result.get("data")
    raw_rows = data.get("datas", data.get("rows")) if isinstance(data, dict) else data
    if not isinstance(raw_rows, list):
        return []
    rows = [row for row in raw_rows if isinstance(row, dict)]
    if len(rows) < 2:
        return []
    labels = [key for key in rows[0] if _LABEL.search(key)]
    if not labels:
        return []
    label_key = labels[0]
    selected = rows[:12]
    # Ambiguous labels make the visual misleading. Keep the evidence table.
    names = [str(row.get(label_key) or "").strip() for row in selected]
    if not all(names) or len(set(names)) != len(names):
        return []
    charts: list[AnswerChart] = []
    query = str((call.get("args") or {}).get("query") or "")
    preferred = [key for key in rows[0] if any(metric in query and metric in key for metric in _METRICS)]
    # Vendors often prepend latest price/change columns to every response.
    # When the user asked for a particular metric, visualise that evidence first.
    for key in preferred or list(rows[0]):
        if key == label_key or _IDENTIFIER.search(key):
            continue
        values = [_number(row.get(key)) for row in selected]
        if any(value is None for value in values):
            continue
        valid = [value for value in values if value is not None]
        units = {value[1] for value in valid}
        if len(units) != 1:
            continue
        unit = valid[0][1]
        charts.append(AnswerChart(
            id=f"{call.get('trace_id', 'chart')}:{key}",
            title=key,
            unit=unit,
            points=[ChartPoint(label=name, value=value[0]) for name, value in zip(names, valid, strict=True)],
            source=str(result.get("source") or "问财金融数据"),
            trace_id=str(call.get("trace_id") or ""),
            as_of=result.get("as_of"),
            total_rows=len(rows),
        ))
        if len(charts) >= limit:
            break
    return charts


def build_presentation(
    calls: Sequence[Mapping[str, Any]], end_note: str = "",
) -> dict[str, Any]:
    """Use only successful tool payloads; cap the output at three charts."""
    successful = sum(call.get("ok") is True for call in calls)
    warnings = [
        f"{call.get('name', '数据查询')} 未成功,相关结论缺少该项证据。"
        for call in calls if not call.get("ok")
    ]
    if end_note:
        warnings.append(end_note)
    charts: list[AnswerChart] = []
    for call in calls:
        if len(charts) >= 3:
            break
        charts.extend(_charts(call, 3 - len(charts)))
    status: Literal["ready", "partial", "empty"] = "ready"
    if not successful:
        status = "empty"
    elif warnings:
        status = "partial"
    return AnswerPresentation(
        status=status,
        successful_tools=successful,
        total_tools=len(calls),
        warnings=list(dict.fromkeys(warnings))[:8],
        charts=charts,
    ).model_dump()