mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-03-30 03:43:39 +00:00
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
160 lines
3.8 KiB
Go
160 lines
3.8 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/QuantumNous/new-api/relay"
|
|
)
|
|
|
|
func getGeminiVideoURL(channel *model.Channel, task *model.Task, apiKey string) (string, error) {
|
|
if channel == nil || task == nil {
|
|
return "", fmt.Errorf("invalid channel or task")
|
|
}
|
|
|
|
if url := extractGeminiVideoURLFromTaskData(task); url != "" {
|
|
return ensureAPIKey(url, apiKey), nil
|
|
}
|
|
|
|
baseURL := constant.ChannelBaseURLs[channel.Type]
|
|
if channel.GetBaseURL() != "" {
|
|
baseURL = channel.GetBaseURL()
|
|
}
|
|
|
|
adaptor := relay.GetTaskAdaptor(constant.TaskPlatform(strconv.Itoa(channel.Type)))
|
|
if adaptor == nil {
|
|
return "", fmt.Errorf("gemini task adaptor not found")
|
|
}
|
|
|
|
if apiKey == "" {
|
|
return "", fmt.Errorf("api key not available for task")
|
|
}
|
|
|
|
proxy := channel.GetSetting().Proxy
|
|
resp, err := adaptor.FetchTask(baseURL, apiKey, map[string]any{
|
|
"task_id": task.GetUpstreamTaskID(),
|
|
"action": task.Action,
|
|
}, proxy)
|
|
if err != nil {
|
|
return "", fmt.Errorf("fetch task failed: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read task response failed: %w", err)
|
|
}
|
|
|
|
taskInfo, parseErr := adaptor.ParseTaskResult(body)
|
|
if parseErr == nil && taskInfo != nil && taskInfo.RemoteUrl != "" {
|
|
return ensureAPIKey(taskInfo.RemoteUrl, apiKey), nil
|
|
}
|
|
|
|
if url := extractGeminiVideoURLFromPayload(body); url != "" {
|
|
return ensureAPIKey(url, apiKey), nil
|
|
}
|
|
|
|
if parseErr != nil {
|
|
return "", fmt.Errorf("parse task result failed: %w", parseErr)
|
|
}
|
|
|
|
return "", fmt.Errorf("gemini video url not found")
|
|
}
|
|
|
|
func extractGeminiVideoURLFromTaskData(task *model.Task) string {
|
|
if task == nil || len(task.Data) == 0 {
|
|
return ""
|
|
}
|
|
var payload map[string]any
|
|
if err := common.Unmarshal(task.Data, &payload); err != nil {
|
|
return ""
|
|
}
|
|
return extractGeminiVideoURLFromMap(payload)
|
|
}
|
|
|
|
func extractGeminiVideoURLFromPayload(body []byte) string {
|
|
var payload map[string]any
|
|
if err := common.Unmarshal(body, &payload); err != nil {
|
|
return ""
|
|
}
|
|
return extractGeminiVideoURLFromMap(payload)
|
|
}
|
|
|
|
func extractGeminiVideoURLFromMap(payload map[string]any) string {
|
|
if payload == nil {
|
|
return ""
|
|
}
|
|
if uri, ok := payload["uri"].(string); ok && uri != "" {
|
|
return uri
|
|
}
|
|
if resp, ok := payload["response"].(map[string]any); ok {
|
|
if uri := extractGeminiVideoURLFromResponse(resp); uri != "" {
|
|
return uri
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func extractGeminiVideoURLFromResponse(resp map[string]any) string {
|
|
if resp == nil {
|
|
return ""
|
|
}
|
|
if gvr, ok := resp["generateVideoResponse"].(map[string]any); ok {
|
|
if uri := extractGeminiVideoURLFromGeneratedSamples(gvr); uri != "" {
|
|
return uri
|
|
}
|
|
}
|
|
if videos, ok := resp["videos"].([]any); ok {
|
|
for _, video := range videos {
|
|
if vm, ok := video.(map[string]any); ok {
|
|
if uri, ok := vm["uri"].(string); ok && uri != "" {
|
|
return uri
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if uri, ok := resp["video"].(string); ok && uri != "" {
|
|
return uri
|
|
}
|
|
if uri, ok := resp["uri"].(string); ok && uri != "" {
|
|
return uri
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func extractGeminiVideoURLFromGeneratedSamples(gvr map[string]any) string {
|
|
if gvr == nil {
|
|
return ""
|
|
}
|
|
if samples, ok := gvr["generatedSamples"].([]any); ok {
|
|
for _, sample := range samples {
|
|
if sm, ok := sample.(map[string]any); ok {
|
|
if video, ok := sm["video"].(map[string]any); ok {
|
|
if uri, ok := video["uri"].(string); ok && uri != "" {
|
|
return uri
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func ensureAPIKey(uri, key string) string {
|
|
if key == "" || uri == "" {
|
|
return uri
|
|
}
|
|
if strings.Contains(uri, "key=") {
|
|
return uri
|
|
}
|
|
if strings.Contains(uri, "?") {
|
|
return fmt.Sprintf("%s&key=%s", uri, key)
|
|
}
|
|
return fmt.Sprintf("%s?key=%s", uri, key)
|
|
}
|