Files
ai-agent/workflow/service/flow/exec_progress.go
T
19904408334 d699f7ce14 feat(workflow): 增加工作流计费与执行生命周期管理
- 新增计费模块:执行开始建单、终态结算/取消/失败处理,支持按条/按秒/按token计费
- 新增执行生命周期跟踪:优雅关停时取消运行中执行并等待落库
- 新增异步任务等待/通知机制(Wait/Notify)
- 重构执行记录落库与进度上报,统一失败分类与重试语义
- 重命名文件:async_task.go→async.go、flow_checkpoint_store.go→exec_checkpoint.go、flow_graph_util.go→exec_record.go
- 更新 .gitignore 与数据库密码配置
2026-09-03 13:22:22 +08:00

47 lines
1.5 KiB
Go

package flow
import (
"context"
wsCommon "gitea.redpowerfuture.com/red-future/common/websocket"
)
// ====================== 进度上报 ======================
type wsProgressCtxKey struct{}
// ProgressReporter 节点执行进度回调接口
type ProgressReporter interface {
ReportStart(nodeId, nodeName string, nodeIndex, nodeCount int)
ReportComplete(nodeId, nodeName string, nodeIndex, nodeCount int)
}
// GetProgressReporter 从context中获取进度上报器
func GetProgressReporter(ctx context.Context) ProgressReporter {
if reporter, ok := ctx.Value(wsProgressCtxKey{}).(ProgressReporter); ok {
return reporter
}
return nil
}
// 进度上报由 exec_hub.go 的 execHub 实现(单执行事件中枢,可多连接订阅);wsProgressCtxKey/ProgressReporter/GetProgressReporter 保留。
// handleCancel 取消工作流执行
func handleCancel(ctx context.Context, conn *wsCommon.WsConnection, _ interface{}) {
if cancel := getExecCancel(conn); cancel != nil {
cancel()
}
_ = writeJSON(conn, &wsCommon.WsPushMsg{Type: "ack", Message: "已取消工作流执行"})
}
// ====================== 工具函数 ======================
func getExecCancel(conn *wsCommon.WsConnection) context.CancelFunc {
cancel, _ := wsCommon.GetMetaT[context.CancelFunc](conn, "execCancel")
return cancel
}
// writeJSON 业务层写入,委托 WsConnection.WriteJSON(共享 writeMu 写锁)
func writeJSON(conn *wsCommon.WsConnection, data interface{}) error {
return conn.WriteJSON(data)
}