110 lines
4.3 KiB
Python
110 lines
4.3 KiB
Python
"""请求摘要:意图判断和 screen agent 共用,只给组件的识别信息和布局,完整配置按需读取
|
||
|
||
只描述本次请求;会话历史由各使用方另行传入,见 history.py
|
||
"""
|
||
|
||
from app.schemas.base import JsonObject
|
||
from app.schemas.data_file import RequestDataFile
|
||
from app.schemas.input_context import InputContext
|
||
|
||
# 每张表在摘要里最多列出的列名,完整的列用 describe_data 查看
|
||
SUMMARY_COLUMNS = 30
|
||
|
||
|
||
def request_summary(context: InputContext) -> JsonObject:
|
||
"""用户输入、澄清记录、编辑上下文、附件、数据文件和大屏摘要"""
|
||
nodes: list[JsonObject] = []
|
||
for node in context.screen.nodes:
|
||
# 画面上的标题帮助判断用户指的是哪个组件:图表在 option.title,指标卡在 props.title;
|
||
# 格式不符时按无标题处理
|
||
option = node.props.get("option")
|
||
title = option.get("title") if isinstance(option, dict) else None
|
||
titles = title if isinstance(title, list) else [title]
|
||
texts = [
|
||
item["text"]
|
||
for item in titles
|
||
if isinstance(item, dict) and isinstance(item.get("text"), str)
|
||
]
|
||
if isinstance(node.props.get("title"), str) and node.props["title"]:
|
||
texts.append(node.props["title"])
|
||
nodes.append(
|
||
{
|
||
"id": node.id,
|
||
"type": node.type,
|
||
"name": node.name,
|
||
"titles": texts,
|
||
"layout": node.layout.model_dump(mode="json", exclude_unset=True),
|
||
}
|
||
)
|
||
|
||
return {
|
||
"prompt": context.prompt,
|
||
"clarification_history": context.clarification_history,
|
||
"context": context.context.model_dump(mode="json", exclude_unset=True),
|
||
"screen_summary": {
|
||
"canvas": context.screen.canvas.model_dump(
|
||
mode="json",
|
||
include={"width", "height", "theme", "backgroundColor"},
|
||
exclude_none=True,
|
||
),
|
||
"nodes": nodes,
|
||
# 数据源只保留标识、名称和类型
|
||
"data_sources": [
|
||
{key: source[key] for key in ("id", "name", "type") if key in source}
|
||
for source in context.screen.dataSource
|
||
],
|
||
},
|
||
# 数据文件的附件由 data_files 的 attached_this_turn 表示
|
||
"attachments": [
|
||
{key: item[key] for key in ("id", "kind", "name") if key in item}
|
||
for item in context.attachments
|
||
if item.get("kind") != "data_file"
|
||
],
|
||
"data_files": [data_file_summary(item) for item in context.data_files],
|
||
}
|
||
|
||
|
||
def data_file_summary(item: RequestDataFile) -> JsonObject:
|
||
"""每个文件只给表名、label、行数和列名;列概况由 describe_data 按需查看。问答节点也用它
|
||
|
||
上传分析完成时多给一句说明和列的含义(summary、column_meanings),没有分析结果时不出现
|
||
"""
|
||
insights = item.insights
|
||
meanings: dict[tuple[str, str], str] = {}
|
||
if insights is not None:
|
||
for column in insights.columns:
|
||
# 含义和列名一样没有信息量;有单位的写在后面
|
||
if column.label != column.name or column.unit:
|
||
meanings[(column.table, column.name)] = (
|
||
f"{column.label}({column.unit})" if column.unit else column.label
|
||
)
|
||
|
||
tables: list[JsonObject] = []
|
||
for table in item.tables:
|
||
shown = table.columns[:SUMMARY_COLUMNS]
|
||
summary: JsonObject = {
|
||
"table": table.table,
|
||
"label": table.label,
|
||
"rows": table.rows,
|
||
"columns": [column.name for column in shown],
|
||
}
|
||
if len(table.columns) > SUMMARY_COLUMNS:
|
||
summary["column_count"] = len(table.columns)
|
||
column_meanings = {
|
||
column.name: meanings[(table.table, column.name)]
|
||
for column in shown
|
||
if (table.table, column.name) in meanings
|
||
}
|
||
if column_meanings:
|
||
summary["column_meanings"] = column_meanings
|
||
tables.append(summary)
|
||
result: JsonObject = {
|
||
"file_id": item.file_id,
|
||
"name": item.name,
|
||
"attached_this_turn": item.attached_this_turn,
|
||
"tables": tables,
|
||
}
|
||
if insights is not None and insights.summary:
|
||
result["summary"] = insights.summary
|
||
return result
|