Files
my-v0-server/app/processor/summary.py
T

81 lines
3.1 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 或标题格式不符时按无标题处理
option = node.props.get("option")
title = option.get("title") if isinstance(option, dict) else None
titles = title if isinstance(title, list) else [title]
nodes.append(
{
"id": node.id,
"type": node.type,
"name": node.name,
"titles": [
item["text"]
for item in titles
if isinstance(item, dict) and isinstance(item.get("text"), str)
],
"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": {
"width": context.screen.canvas.width,
"height": context.screen.canvas.height,
},
"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 按需查看。问答节点也用它"""
tables: list[JsonObject] = []
for table in item.tables:
summary: JsonObject = {
"table": table.table,
"label": table.label,
"rows": table.rows,
"columns": [column.name for column in table.columns[:SUMMARY_COLUMNS]],
}
if len(table.columns) > SUMMARY_COLUMNS:
summary["column_count"] = len(table.columns)
tables.append(summary)
return {
"file_id": item.file_id,
"name": item.name,
"attached_this_turn": item.attached_this_turn,
"tables": tables,
}