2025-10-08 22:53:02 +08:00
|
|
|
|
package sora
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
feat(task): add model redirection, per-call billing, and multipart retry fix for async tasks
1. Async task model redirection (aligned with sync tasks):
- Integrate ModelMappedHelper in RelayTaskSubmit after model name
determination, populating OriginModelName / UpstreamModelName on RelayInfo.
- All task adaptors now send UpstreamModelName to upstream providers:
- Gemini & Vertex: BuildRequestURL uses UpstreamModelName.
- Doubao & Ali: BuildRequestBody conditionally overwrites body.Model.
- Vidu, Kling, Hailuo, Jimeng: convertToRequestPayload accepts RelayInfo
and unconditionally uses info.UpstreamModelName.
- Sora: BuildRequestBody parses JSON and multipart bodies to replace
the "model" field with UpstreamModelName.
- Frontend log visibility: LogTaskConsumption and taskBillingOther now
emit is_model_mapped / upstream_model_name in the "other" JSON field.
- Billing safety: RecalculateTaskQuotaByTokens reads model name from
BillingContext.OriginModelName (via taskModelName) instead of
task.Data["model"], preventing billing leaks from upstream model names.
2. Per-call billing (TaskPricePatches lifecycle):
- Rename TaskBillingContext.ModelName → OriginModelName; add PerCallBilling
bool field, populated from TaskPricePatches at submission time.
- settleTaskBillingOnComplete short-circuits when PerCallBilling is true,
skipping both adaptor adjustments and token-based recalculation.
- Remove ModelName from TaskSubmitResult; use relayInfo.OriginModelName
consistently in controller/relay.go for billing context and logging.
3. Multipart retry boundary mismatch fix:
- Root cause: after Sora (or OpenAI audio) rebuilds a multipart body with a
new boundary and overwrites c.Request.Header["Content-Type"], subsequent
calls to ParseMultipartFormReusable on retry would parse the cached
original body with the wrong boundary, causing "NextPart: EOF".
- Fix: ParseMultipartFormReusable now caches the original Content-Type in
gin context key "_original_multipart_ct" on first call and reuses it for
all subsequent parses, making multipart parsing retry-safe globally.
- Sora adaptor reverted to the standard pattern (direct header set/get),
which is now safe thanks to the root fix.
4. Tests:
- task_billing_test.go: update makeTask to use OriginModelName; add
PerCallBilling settlement tests (skip adaptor adjust, skip token recalc);
add non-per-call adaptor adjustment test with refund verification.
2026-02-22 15:32:33 +08:00
|
|
|
|
"bytes"
|
2025-10-08 22:53:02 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"io"
|
feat(task): add model redirection, per-call billing, and multipart retry fix for async tasks
1. Async task model redirection (aligned with sync tasks):
- Integrate ModelMappedHelper in RelayTaskSubmit after model name
determination, populating OriginModelName / UpstreamModelName on RelayInfo.
- All task adaptors now send UpstreamModelName to upstream providers:
- Gemini & Vertex: BuildRequestURL uses UpstreamModelName.
- Doubao & Ali: BuildRequestBody conditionally overwrites body.Model.
- Vidu, Kling, Hailuo, Jimeng: convertToRequestPayload accepts RelayInfo
and unconditionally uses info.UpstreamModelName.
- Sora: BuildRequestBody parses JSON and multipart bodies to replace
the "model" field with UpstreamModelName.
- Frontend log visibility: LogTaskConsumption and taskBillingOther now
emit is_model_mapped / upstream_model_name in the "other" JSON field.
- Billing safety: RecalculateTaskQuotaByTokens reads model name from
BillingContext.OriginModelName (via taskModelName) instead of
task.Data["model"], preventing billing leaks from upstream model names.
2. Per-call billing (TaskPricePatches lifecycle):
- Rename TaskBillingContext.ModelName → OriginModelName; add PerCallBilling
bool field, populated from TaskPricePatches at submission time.
- settleTaskBillingOnComplete short-circuits when PerCallBilling is true,
skipping both adaptor adjustments and token-based recalculation.
- Remove ModelName from TaskSubmitResult; use relayInfo.OriginModelName
consistently in controller/relay.go for billing context and logging.
3. Multipart retry boundary mismatch fix:
- Root cause: after Sora (or OpenAI audio) rebuilds a multipart body with a
new boundary and overwrites c.Request.Header["Content-Type"], subsequent
calls to ParseMultipartFormReusable on retry would parse the cached
original body with the wrong boundary, causing "NextPart: EOF".
- Fix: ParseMultipartFormReusable now caches the original Content-Type in
gin context key "_original_multipart_ct" on first call and reuses it for
all subsequent parses, making multipart parsing retry-safe globally.
- Sora adaptor reverted to the standard pattern (direct header set/get),
which is now safe thanks to the root fix.
4. Tests:
- task_billing_test.go: update makeTask to use OriginModelName; add
PerCallBilling settlement tests (skip adaptor adjust, skip token recalc);
add non-per-call adaptor adjustment test with refund verification.
2026-02-22 15:32:33 +08:00
|
|
|
|
"mime/multipart"
|
2025-10-08 22:53:02 +08:00
|
|
|
|
"net/http"
|
2026-02-25 12:51:46 +08:00
|
|
|
|
"net/textproto"
|
2026-02-10 21:15:09 +08:00
|
|
|
|
"strconv"
|
2025-12-11 23:35:23 +08:00
|
|
|
|
"strings"
|
2025-10-11 15:30:09 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
2025-12-11 23:35:23 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/constant"
|
2025-10-11 15:30:09 +08:00
|
|
|
|
"github.com/QuantumNous/new-api/dto"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/model"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/relay/channel"
|
2026-02-10 21:15:09 +08:00
|
|
|
|
taskcommon "github.com/QuantumNous/new-api/relay/channel/task/taskcommon"
|
2025-10-11 15:30:09 +08:00
|
|
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
|
|
|
|
"github.com/QuantumNous/new-api/service"
|
2025-10-08 22:53:02 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/pkg/errors"
|
2026-02-21 23:05:58 +08:00
|
|
|
|
"github.com/tidwall/sjson"
|
2025-10-08 22:53:02 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// Request / Response structures
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
|
|
|
|
|
|
type ContentItem struct {
|
|
|
|
|
|
Type string `json:"type"` // "text" or "image_url"
|
|
|
|
|
|
Text string `json:"text,omitempty"` // for text type
|
|
|
|
|
|
ImageURL *ImageURL `json:"image_url,omitempty"` // for image_url type
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type ImageURL struct {
|
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type responseTask struct {
|
2025-10-09 10:59:05 +08:00
|
|
|
|
ID string `json:"id"`
|
2025-10-10 15:25:29 +08:00
|
|
|
|
TaskID string `json:"task_id,omitempty"` //兼容旧接口
|
2025-10-09 10:59:05 +08:00
|
|
|
|
Object string `json:"object"`
|
|
|
|
|
|
Model string `json:"model"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
Progress int `json:"progress"`
|
|
|
|
|
|
CreatedAt int64 `json:"created_at"`
|
|
|
|
|
|
CompletedAt int64 `json:"completed_at,omitempty"`
|
|
|
|
|
|
ExpiresAt int64 `json:"expires_at,omitempty"`
|
|
|
|
|
|
Seconds string `json:"seconds,omitempty"`
|
|
|
|
|
|
Size string `json:"size,omitempty"`
|
|
|
|
|
|
RemixedFromVideoID string `json:"remixed_from_video_id,omitempty"`
|
|
|
|
|
|
Error *struct {
|
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
|
Code string `json:"code"`
|
|
|
|
|
|
} `json:"error,omitempty"`
|
2025-10-08 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
// Adaptor implementation
|
|
|
|
|
|
// ============================
|
|
|
|
|
|
|
|
|
|
|
|
type TaskAdaptor struct {
|
2026-02-10 21:15:09 +08:00
|
|
|
|
taskcommon.BaseBilling
|
2025-10-08 22:53:02 +08:00
|
|
|
|
ChannelType int
|
|
|
|
|
|
apiKey string
|
|
|
|
|
|
baseURL string
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) {
|
|
|
|
|
|
a.ChannelType = info.ChannelType
|
|
|
|
|
|
a.baseURL = info.ChannelBaseUrl
|
|
|
|
|
|
a.apiKey = info.ApiKey
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 23:35:23 +08:00
|
|
|
|
func validateRemixRequest(c *gin.Context) *dto.TaskError {
|
2026-02-10 21:15:09 +08:00
|
|
|
|
var req relaycommon.TaskSubmitReq
|
2025-12-11 23:35:23 +08:00
|
|
|
|
if err := common.UnmarshalBodyReusable(c, &req); err != nil {
|
|
|
|
|
|
return service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest)
|
|
|
|
|
|
}
|
|
|
|
|
|
if strings.TrimSpace(req.Prompt) == "" {
|
|
|
|
|
|
return service.TaskErrorWrapperLocal(fmt.Errorf("field prompt is required"), "invalid_request", http.StatusBadRequest)
|
|
|
|
|
|
}
|
2026-02-10 21:15:09 +08:00
|
|
|
|
// 存储原始请求到 context,与 ValidateMultipartDirect 路径保持一致
|
|
|
|
|
|
c.Set("task_request", req)
|
2025-12-11 23:35:23 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 22:53:02 +08:00
|
|
|
|
func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) {
|
2025-12-11 23:35:23 +08:00
|
|
|
|
if info.Action == constant.TaskActionRemix {
|
|
|
|
|
|
return validateRemixRequest(c)
|
|
|
|
|
|
}
|
2025-10-08 22:53:02 +08:00
|
|
|
|
return relaycommon.ValidateMultipartDirect(c, info)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-10 21:15:09 +08:00
|
|
|
|
// EstimateBilling 根据用户请求的 seconds 和 size 计算 OtherRatios。
|
|
|
|
|
|
func (a *TaskAdaptor) EstimateBilling(c *gin.Context, info *relaycommon.RelayInfo) map[string]float64 {
|
|
|
|
|
|
// remix 路径的 OtherRatios 已在 ResolveOriginTask 中设置
|
|
|
|
|
|
if info.Action == constant.TaskActionRemix {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req, err := relaycommon.GetTaskRequest(c)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
seconds, _ := strconv.Atoi(req.Seconds)
|
|
|
|
|
|
if seconds == 0 {
|
|
|
|
|
|
seconds = req.Duration
|
|
|
|
|
|
}
|
|
|
|
|
|
if seconds <= 0 {
|
|
|
|
|
|
seconds = 4
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size := req.Size
|
|
|
|
|
|
if size == "" {
|
|
|
|
|
|
size = "720x1280"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ratios := map[string]float64{
|
|
|
|
|
|
"seconds": float64(seconds),
|
|
|
|
|
|
"size": 1,
|
|
|
|
|
|
}
|
|
|
|
|
|
if size == "1792x1024" || size == "1024x1792" {
|
|
|
|
|
|
ratios["size"] = 1.666667
|
|
|
|
|
|
}
|
|
|
|
|
|
return ratios
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 22:53:02 +08:00
|
|
|
|
func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) {
|
2025-12-11 23:35:23 +08:00
|
|
|
|
if info.Action == constant.TaskActionRemix {
|
|
|
|
|
|
return fmt.Sprintf("%s/v1/videos/%s/remix", a.baseURL, info.OriginTaskID), nil
|
|
|
|
|
|
}
|
2025-10-08 22:53:02 +08:00
|
|
|
|
return fmt.Sprintf("%s/v1/videos", a.baseURL), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BuildRequestHeader sets required headers.
|
|
|
|
|
|
func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+a.apiKey)
|
|
|
|
|
|
req.Header.Set("Content-Type", c.Request.Header.Get("Content-Type"))
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) {
|
2026-02-12 01:51:17 +08:00
|
|
|
|
storage, err := common.GetBodyStorage(c)
|
2025-10-08 22:53:02 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrap(err, "get_request_body_failed")
|
|
|
|
|
|
}
|
feat(task): add model redirection, per-call billing, and multipart retry fix for async tasks
1. Async task model redirection (aligned with sync tasks):
- Integrate ModelMappedHelper in RelayTaskSubmit after model name
determination, populating OriginModelName / UpstreamModelName on RelayInfo.
- All task adaptors now send UpstreamModelName to upstream providers:
- Gemini & Vertex: BuildRequestURL uses UpstreamModelName.
- Doubao & Ali: BuildRequestBody conditionally overwrites body.Model.
- Vidu, Kling, Hailuo, Jimeng: convertToRequestPayload accepts RelayInfo
and unconditionally uses info.UpstreamModelName.
- Sora: BuildRequestBody parses JSON and multipart bodies to replace
the "model" field with UpstreamModelName.
- Frontend log visibility: LogTaskConsumption and taskBillingOther now
emit is_model_mapped / upstream_model_name in the "other" JSON field.
- Billing safety: RecalculateTaskQuotaByTokens reads model name from
BillingContext.OriginModelName (via taskModelName) instead of
task.Data["model"], preventing billing leaks from upstream model names.
2. Per-call billing (TaskPricePatches lifecycle):
- Rename TaskBillingContext.ModelName → OriginModelName; add PerCallBilling
bool field, populated from TaskPricePatches at submission time.
- settleTaskBillingOnComplete short-circuits when PerCallBilling is true,
skipping both adaptor adjustments and token-based recalculation.
- Remove ModelName from TaskSubmitResult; use relayInfo.OriginModelName
consistently in controller/relay.go for billing context and logging.
3. Multipart retry boundary mismatch fix:
- Root cause: after Sora (or OpenAI audio) rebuilds a multipart body with a
new boundary and overwrites c.Request.Header["Content-Type"], subsequent
calls to ParseMultipartFormReusable on retry would parse the cached
original body with the wrong boundary, causing "NextPart: EOF".
- Fix: ParseMultipartFormReusable now caches the original Content-Type in
gin context key "_original_multipart_ct" on first call and reuses it for
all subsequent parses, making multipart parsing retry-safe globally.
- Sora adaptor reverted to the standard pattern (direct header set/get),
which is now safe thanks to the root fix.
4. Tests:
- task_billing_test.go: update makeTask to use OriginModelName; add
PerCallBilling settlement tests (skip adaptor adjust, skip token recalc);
add non-per-call adaptor adjustment test with refund verification.
2026-02-22 15:32:33 +08:00
|
|
|
|
cachedBody, err := storage.Bytes()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrap(err, "read_body_bytes_failed")
|
|
|
|
|
|
}
|
|
|
|
|
|
contentType := c.GetHeader("Content-Type")
|
|
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(contentType, "application/json") {
|
|
|
|
|
|
var bodyMap map[string]interface{}
|
|
|
|
|
|
if err := common.Unmarshal(cachedBody, &bodyMap); err == nil {
|
|
|
|
|
|
bodyMap["model"] = info.UpstreamModelName
|
|
|
|
|
|
if newBody, err := common.Marshal(bodyMap); err == nil {
|
|
|
|
|
|
return bytes.NewReader(newBody), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return bytes.NewReader(cachedBody), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if strings.Contains(contentType, "multipart/form-data") {
|
|
|
|
|
|
formData, err := common.ParseMultipartFormReusable(c)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return bytes.NewReader(cachedBody), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
writer := multipart.NewWriter(&buf)
|
|
|
|
|
|
writer.WriteField("model", info.UpstreamModelName)
|
|
|
|
|
|
for key, values := range formData.Value {
|
|
|
|
|
|
if key == "model" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, v := range values {
|
|
|
|
|
|
writer.WriteField(key, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for fieldName, fileHeaders := range formData.File {
|
|
|
|
|
|
for _, fh := range fileHeaders {
|
|
|
|
|
|
f, err := fh.Open()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-02-25 12:51:46 +08:00
|
|
|
|
ct := fh.Header.Get("Content-Type")
|
|
|
|
|
|
if ct == "" || ct == "application/octet-stream" {
|
|
|
|
|
|
buf512 := make([]byte, 512)
|
|
|
|
|
|
n, _ := io.ReadFull(f, buf512)
|
|
|
|
|
|
ct = http.DetectContentType(buf512[:n])
|
|
|
|
|
|
// Re-open after sniffing so the full content is copied below
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
f, err = fh.Open()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
h := make(textproto.MIMEHeader)
|
|
|
|
|
|
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, fieldName, fh.Filename))
|
|
|
|
|
|
h.Set("Content-Type", ct)
|
|
|
|
|
|
part, err := writer.CreatePart(h)
|
feat(task): add model redirection, per-call billing, and multipart retry fix for async tasks
1. Async task model redirection (aligned with sync tasks):
- Integrate ModelMappedHelper in RelayTaskSubmit after model name
determination, populating OriginModelName / UpstreamModelName on RelayInfo.
- All task adaptors now send UpstreamModelName to upstream providers:
- Gemini & Vertex: BuildRequestURL uses UpstreamModelName.
- Doubao & Ali: BuildRequestBody conditionally overwrites body.Model.
- Vidu, Kling, Hailuo, Jimeng: convertToRequestPayload accepts RelayInfo
and unconditionally uses info.UpstreamModelName.
- Sora: BuildRequestBody parses JSON and multipart bodies to replace
the "model" field with UpstreamModelName.
- Frontend log visibility: LogTaskConsumption and taskBillingOther now
emit is_model_mapped / upstream_model_name in the "other" JSON field.
- Billing safety: RecalculateTaskQuotaByTokens reads model name from
BillingContext.OriginModelName (via taskModelName) instead of
task.Data["model"], preventing billing leaks from upstream model names.
2. Per-call billing (TaskPricePatches lifecycle):
- Rename TaskBillingContext.ModelName → OriginModelName; add PerCallBilling
bool field, populated from TaskPricePatches at submission time.
- settleTaskBillingOnComplete short-circuits when PerCallBilling is true,
skipping both adaptor adjustments and token-based recalculation.
- Remove ModelName from TaskSubmitResult; use relayInfo.OriginModelName
consistently in controller/relay.go for billing context and logging.
3. Multipart retry boundary mismatch fix:
- Root cause: after Sora (or OpenAI audio) rebuilds a multipart body with a
new boundary and overwrites c.Request.Header["Content-Type"], subsequent
calls to ParseMultipartFormReusable on retry would parse the cached
original body with the wrong boundary, causing "NextPart: EOF".
- Fix: ParseMultipartFormReusable now caches the original Content-Type in
gin context key "_original_multipart_ct" on first call and reuses it for
all subsequent parses, making multipart parsing retry-safe globally.
- Sora adaptor reverted to the standard pattern (direct header set/get),
which is now safe thanks to the root fix.
4. Tests:
- task_billing_test.go: update makeTask to use OriginModelName; add
PerCallBilling settlement tests (skip adaptor adjust, skip token recalc);
add non-per-call adaptor adjustment test with refund verification.
2026-02-22 15:32:33 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
io.Copy(part, f)
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
writer.Close()
|
|
|
|
|
|
c.Request.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
|
|
return &buf, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-12 01:51:17 +08:00
|
|
|
|
return common.ReaderOnly(storage), nil
|
2025-10-08 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DoRequest delegates to common helper.
|
|
|
|
|
|
func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
|
|
|
|
|
|
return channel.DoTaskApiRequest(a, c, info, requestBody)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DoResponse handles upstream response, returns taskID etc.
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) {
|
2025-10-08 22:53:02 +08:00
|
|
|
|
responseBody, err := io.ReadAll(resp.Body)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
taskErr = service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
// Parse Sora response
|
2025-10-10 15:25:29 +08:00
|
|
|
|
var dResp responseTask
|
2025-10-14 23:03:17 +08:00
|
|
|
|
if err := common.Unmarshal(responseBody, &dResp); err != nil {
|
2025-10-08 22:53:02 +08:00
|
|
|
|
taskErr = service.TaskErrorWrapper(errors.Wrapf(err, "body: %s", responseBody), "unmarshal_response_body_failed", http.StatusInternalServerError)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
upstreamID := dResp.ID
|
|
|
|
|
|
if upstreamID == "" {
|
|
|
|
|
|
upstreamID = dResp.TaskID
|
|
|
|
|
|
}
|
|
|
|
|
|
if upstreamID == "" {
|
|
|
|
|
|
taskErr = service.TaskErrorWrapper(fmt.Errorf("task_id is empty"), "invalid_response", http.StatusInternalServerError)
|
|
|
|
|
|
return
|
2025-10-08 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
// 使用公开 task_xxxx ID 返回给客户端
|
|
|
|
|
|
dResp.ID = info.PublicTaskID
|
|
|
|
|
|
dResp.TaskID = info.PublicTaskID
|
2025-10-10 15:25:29 +08:00
|
|
|
|
c.JSON(http.StatusOK, dResp)
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
return upstreamID, responseBody, nil
|
2025-10-08 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FetchTask fetch task status
|
2025-12-09 11:15:27 +08:00
|
|
|
|
func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any, proxy string) (*http.Response, error) {
|
2025-10-08 22:53:02 +08:00
|
|
|
|
taskID, ok := body["task_id"].(string)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil, fmt.Errorf("invalid task_id")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 10:59:05 +08:00
|
|
|
|
uri := fmt.Sprintf("%s/v1/videos/%s", baseUrl, taskID)
|
2025-10-08 22:53:02 +08:00
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
req.Header.Set("Authorization", "Bearer "+key)
|
|
|
|
|
|
|
2025-12-09 11:15:27 +08:00
|
|
|
|
client, err := service.GetHttpClientWithProxy(proxy)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, fmt.Errorf("new proxy http client failed: %w", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return client.Do(req)
|
2025-10-08 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *TaskAdaptor) GetModelList() []string {
|
|
|
|
|
|
return ModelList
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *TaskAdaptor) GetChannelName() string {
|
|
|
|
|
|
return ChannelName
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) {
|
|
|
|
|
|
resTask := responseTask{}
|
2025-10-14 23:03:17 +08:00
|
|
|
|
if err := common.Unmarshal(respBody, &resTask); err != nil {
|
2025-10-08 22:53:02 +08:00
|
|
|
|
return nil, errors.Wrap(err, "unmarshal task result failed")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
taskResult := relaycommon.TaskInfo{
|
|
|
|
|
|
Code: 0,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch resTask.Status {
|
2025-10-09 10:59:05 +08:00
|
|
|
|
case "queued", "pending":
|
2025-10-08 22:53:02 +08:00
|
|
|
|
taskResult.Status = model.TaskStatusQueued
|
2025-10-09 10:59:05 +08:00
|
|
|
|
case "processing", "in_progress":
|
2025-10-08 22:53:02 +08:00
|
|
|
|
taskResult.Status = model.TaskStatusInProgress
|
2025-10-09 10:59:05 +08:00
|
|
|
|
case "completed":
|
2025-10-08 22:53:02 +08:00
|
|
|
|
taskResult.Status = model.TaskStatusSuccess
|
refactor(task): extract billing and polling logic from controller to service layer
Restructure the task relay system for better separation of concerns:
- Extract task billing into service/task_billing.go with unified settlement flow
- Move task polling loop from controller to service/task_polling.go (supports Suno + video platforms)
- Split RelayTask into fetch/submit paths with dedicated retry logic (taskSubmitWithRetry)
- Add TaskDto, TaskResponse generics, and FetchReq to dto/task.go
- Add taskcommon/helpers.go for shared task adaptor utilities
- Remove controller/task_video.go (logic consolidated into service layer)
- Update all task adaptors (ali, doubao, gemini, hailuo, jimeng, kling, sora, suno, vertex, vidu)
- Simplify frontend task logs to use new TaskDto response format
2026-02-10 20:40:33 +08:00
|
|
|
|
// Url intentionally left empty — the caller constructs the proxy URL using the public task ID
|
2025-10-08 22:53:02 +08:00
|
|
|
|
case "failed", "cancelled":
|
|
|
|
|
|
taskResult.Status = model.TaskStatusFailure
|
2025-10-09 10:59:05 +08:00
|
|
|
|
if resTask.Error != nil {
|
|
|
|
|
|
taskResult.Reason = resTask.Error.Message
|
|
|
|
|
|
} else {
|
|
|
|
|
|
taskResult.Reason = "task failed"
|
|
|
|
|
|
}
|
2025-10-08 22:53:02 +08:00
|
|
|
|
default:
|
2025-10-09 10:59:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
if resTask.Progress > 0 && resTask.Progress < 100 {
|
|
|
|
|
|
taskResult.Progress = fmt.Sprintf("%d%%", resTask.Progress)
|
2025-10-08 22:53:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &taskResult, nil
|
|
|
|
|
|
}
|
2025-10-10 23:27:12 +08:00
|
|
|
|
|
2025-10-14 23:03:17 +08:00
|
|
|
|
func (a *TaskAdaptor) ConvertToOpenAIVideo(task *model.Task) ([]byte, error) {
|
2026-02-21 23:05:58 +08:00
|
|
|
|
data := task.Data
|
|
|
|
|
|
var err error
|
|
|
|
|
|
if data, err = sjson.SetBytes(data, "id", task.TaskID); err != nil {
|
|
|
|
|
|
return nil, errors.Wrap(err, "set id failed")
|
|
|
|
|
|
}
|
|
|
|
|
|
return data, nil
|
2025-10-10 23:27:12 +08:00
|
|
|
|
}
|