Files
model-gateway/service/model_task_start_service.go
T
19904408334 9d9bd71468 fix: 优化模型网关请求重试与业务字段前置写入
- 流式调用增加可重试错误码检测与指数退避重试
- 业务字段写入改为前置追加,不覆盖已有值
- 请求体按模板元数据递归合并,补充数组/对象字段处理
- 异步任务查询支持请求体映射与占位符替换
- 升级 common 与 gmq 依赖版本,移除本地 replace
2026-08-21 09:47:21 +08:00

87 lines
2.6 KiB
Go

package service
import (
"context"
"encoding/json"
"fmt"
"model-gateway/dao"
"model-gateway/model/dto"
modelUtils "model-gateway/service/utils"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
var ModelTaskStart = &modelTaskStartService{}
type modelTaskStartService struct{}
// CreateTask 创建任务
func (s *modelTaskStartService) CreateTask(ctx context.Context, req *dto.CallModelTaskStartReq) (res *dto.ModelCallRes, err error) {
startTime := time.Now()
id := req.Id
modelInfo := req.ModelInfo
newRequestParams := req.RequestParams
// 6) 模型请求
modelRespBody, err := ModelHttpNormalRequest(ctx, modelInfo.BaseURL, modelInfo.RequestHeadMapping, modelInfo.HttpMethod, newRequestParams)
if err != nil {
return nil, fmt.Errorf("模型请求失败: %v", err)
}
if modelRespBody == nil {
return nil, fmt.Errorf("模型返回参数是空")
}
// 7) 更新视频任务信息(统一字段路径 GetByPath 基于该对象读取)
var respObj map[string]any
if err = json.Unmarshal(modelRespBody, &respObj); err != nil {
return nil, fmt.Errorf("模型返回参数解析失败:%v", err)
}
updateModelReq := dto.UpdateModelTaskStartReq{
Id: id,
OriginalResponseParams: respObj,
}
docMsg := new(dto.ModelCallRes)
docMsg.TaskId = id
errMsg := new(dto.ModelErrorResp)
err = gconv.Struct(modelRespBody, errMsg)
if err != nil {
return nil, fmt.Errorf("模型返回参数解析失败:%v", err)
}
if g.IsEmpty(errMsg.Error.Code) {
errMsg1 := new(dto.ModelError1Resp)
err = gconv.Struct(modelRespBody, errMsg1)
if err != nil {
return nil, fmt.Errorf("模型返回参数解析失败:%v", err)
}
if !g.IsEmpty(errMsg1.Code) && errMsg1.Code != 20000000 {
docMsg.ErrorMsg = errMsg1.Message
updateModelReq.ErrorMsg = docMsg.ErrorMsg
}
} else {
if errMsg.Error.Code != "" {
docMsg.ErrorMsg = errMsg.Error.Message
updateModelReq.ErrorMsg = docMsg.ErrorMsg
}
}
if docMsg.ErrorMsg == "" {
taskIDPath := modelUtils.CleanFieldPath(modelInfo.AsyncTaskMapping.TaskId)
docMsg.Content = map[string]any{
"respBody": modelUtils.GetByPathValue(respObj, taskIDPath),
}
}
if !g.IsEmpty(docMsg.Content) {
updateModelReq.ResponseParams = docMsg.Content
updateModelReq.TaskId = gconv.String(docMsg.Content["respBody"])
}
updateModelReq.DurationSeconds = int64(time.Since(startTime).Seconds())
// 8) 更新模型视频任务信息
_, err = dao.ModelTaskStart.Update(ctx, &updateModelReq)
if err != nil {
return nil, fmt.Errorf("更新模型视频任务信息失败: %v", err)
}
return docMsg, nil
}