fd268664c8
用户实测:报告页输入主题、点生成、看着没反应,切到别的页面再回来,什么都没了。 真相是报告在后端好好地生成完了(26s),是 UI 把它弄丢了、而且永远找不回。 根因:两条提交路径漂移。SubmitTask(POST /tasks) 这一年陆续长出了预算门控/ 计费租户/积分硬拦截/落库/录像五道,而 GenerateReport(POST /reports) 还停在 最初的「发个 NATS」,一道都没有。后果远不止看不到历史: - 不落库 → 运行历史(读 sundynix_task)永远看不到报告。实测修复前该表 report_% 前缀 0 行。 - 无 token/轨迹录像 → SSE 没有回放能力,切走即永久丢失。 - 无 MetaTenantID → 报告用量记不到租户头上 = 漏账。 - 无预算门控/积分硬拦截 → 余额为 0 也能生成,绕过全部成本护栏。 - 路由漏了 RequireTenantRole → **viewer 只读角色能生成报告烧积分**, 而隔壁 /tasks 的注释白纸黑字写着「viewer 只读拦下」。报告一样烧钱。 修法不是把代码抄一份(那只会再漂一次),而是抽两个共用函数: preflight() — 预算 → 计费租户 → 积分硬拦截,返回 billingTenant launch() — 落库 + PublishTask + token/轨迹录像 两条路径都走它们,不可能再各长各的。SubmitTask 行为逐行不变。 live 验证(真账号 blizzardzhang,桌面端实机):生成 report_5dae9155af5cb500 → sundynix_task 建行且带 owner+tenant_id → 出现在运行历史首条 → 点开 完整复盘 8 节点轨迹 + 报告全文。gin 路由表也确认 /reports 中间件数 12 → 13(与 /tasks 齐平)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
87 lines
3.1 KiB
Go
87 lines
3.1 KiB
Go
package handler
|
||
|
||
import (
|
||
"crypto/rand"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"github.com/sundynix/sundynix-shared/contract"
|
||
)
|
||
|
||
// GenerateReport: POST /api/v1/reports —— 触发报告生成。
|
||
// 组装一个 intent=report 的任务发到 NATS,Dispatcher 走专用编排(规划→分章并行→渲染 docx)。
|
||
// 返回 task_id;客户端用 GET /tasks/:id/stream 看实时进度,完成后用 /reports/:id/download 取 Word。
|
||
func (h *Handler) GenerateReport(c *gin.Context) {
|
||
var body struct {
|
||
Topic string `json:"topic"`
|
||
KB string `json:"kb"`
|
||
}
|
||
if err := c.ShouldBindJSON(&body); err != nil || body.Topic == "" {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": "topic required"})
|
||
return
|
||
}
|
||
// 报告和普通任务一样烧钱,必须过同一道关卡(预算/计费租户/积分硬拦截)。
|
||
// 此前这里直接 PublishTask,绕过了全部三项。
|
||
billingTenant, ok := h.preflight(c)
|
||
if !ok {
|
||
return
|
||
}
|
||
id := newReportID()
|
||
graph, _ := json.Marshal(map[string]any{"topic": body.Topic}) // 占位 DSL,报告编排实际读 Meta
|
||
task := &contract.Task{
|
||
ID: id,
|
||
Graph: graph,
|
||
Meta: map[string]any{
|
||
contract.MetaIntent: contract.IntentReport,
|
||
contract.MetaTopic: body.Topic,
|
||
contract.MetaKB: body.KB,
|
||
contract.MetaUserID: userID(c),
|
||
contract.MetaTenantID: billingTenant, // 用量按计费租户扣,此前报告完全没记 → 漏账
|
||
contract.MetaSessionID: sessionID(c),
|
||
},
|
||
}
|
||
// launch 而非裸 PublishTask:报告也是一次「执行」,要落库(→ 进运行历史、可复盘)
|
||
// 并开录像(→ SSE 可回放,切走再回来不丢)。
|
||
if err := h.launch(c, task); err != nil {
|
||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
c.JSON(http.StatusAccepted, gin.H{"task_id": id})
|
||
}
|
||
|
||
// ExportReport: GET /api/v1/reports/:id/export?format=docx|md —— 按需把报告源渲染为指定格式并下载。
|
||
// 生成阶段只存源;此处经 mcp-go report_export 现渲染("导出时再处理")。PDF 由前端打印预览生成。
|
||
func (h *Handler) ExportReport(c *gin.Context) {
|
||
id := c.Param("id")
|
||
format := c.DefaultQuery("format", "docx")
|
||
res, err := h.bus.CallTool(c.Request.Context(), contract.ToolSubjectGo("report_export"),
|
||
&contract.ToolCall{Tool: "report_export", Args: map[string]any{"task_id": id, "format": format}})
|
||
if err != nil || res == nil || !res.OK {
|
||
msg := "报告尚未生成或已过期"
|
||
if res != nil && res.Error != "" {
|
||
msg = res.Error
|
||
}
|
||
c.JSON(http.StatusNotFound, gin.H{"error": msg})
|
||
return
|
||
}
|
||
switch format {
|
||
case "md", "markdown":
|
||
c.Header("Content-Disposition", `attachment; filename="`+id+`.md"`)
|
||
c.Header("Content-Type", "text/markdown; charset=utf-8")
|
||
c.String(http.StatusOK, res.Content)
|
||
default: // docx:res.Content 为 mcp-go 落盘的 .docx 路径
|
||
c.Header("Content-Disposition", `attachment; filename="`+id+`.docx"`)
|
||
c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
||
c.File(res.Content)
|
||
}
|
||
}
|
||
|
||
func newReportID() string {
|
||
var b [8]byte
|
||
_, _ = rand.Read(b[:])
|
||
return "report_" + hex.EncodeToString(b[:])
|
||
}
|