Преглед изворни кода

Merge pull request #1309 from feitianbubu/pr/alpha/video-action-constant2

feat: video action to constant
IcedTangerine пре 6 месеци
родитељ
комит
34aca14858

+ 3 - 0
constant/task.go

@@ -12,6 +12,9 @@ const (
 const (
 	SunoActionMusic  = "MUSIC"
 	SunoActionLyrics = "LYRICS"
+
+	TaskActionGenerate     = "generate"
+	TaskActionTextGenerate = "textGenerate"
 )
 
 var SunoModel2Action = map[string]string{

+ 4 - 2
middleware/kling_adapter.go

@@ -3,9 +3,11 @@ package middleware
 import (
 	"bytes"
 	"encoding/json"
-	"github.com/gin-gonic/gin"
 	"io"
 	"one-api/common"
+	"one-api/constant"
+
+	"github.com/gin-gonic/gin"
 )
 
 func KlingRequestConvert() func(c *gin.Context) {
@@ -35,7 +37,7 @@ func KlingRequestConvert() func(c *gin.Context) {
 		c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonData))
 		c.Request.URL.Path = "/v1/video/generations"
 		if image := originalReq["image"]; image == "" {
-			c.Set("action", "textGenerate")
+			c.Set("action", constant.TaskActionTextGenerate)
 		}
 
 		// We have to reset the request body for the next handlers

+ 2 - 1
relay/channel/task/jimeng/adaptor.go

@@ -19,6 +19,7 @@ import (
 	"github.com/pkg/errors"
 
 	"one-api/common"
+	"one-api/constant"
 	"one-api/dto"
 	"one-api/relay/channel"
 	relaycommon "one-api/relay/common"
@@ -88,7 +89,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
 // ValidateRequestAndSetAction parses body, validates fields and sets default action.
 func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.TaskRelayInfo) (taskErr *dto.TaskError) {
 	// Accept only POST /v1/video/generations as "generate" action.
-	action := "generate"
+	action := constant.TaskActionGenerate
 	info.Action = action
 
 	req := relaycommon.TaskSubmitReq{}

+ 4 - 3
relay/channel/task/kling/adaptor.go

@@ -16,6 +16,7 @@ import (
 	"github.com/pkg/errors"
 
 	"one-api/common"
+	"one-api/constant"
 	"one-api/dto"
 	"one-api/relay/channel"
 	relaycommon "one-api/relay/common"
@@ -92,7 +93,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
 // ValidateRequestAndSetAction parses body, validates fields and sets default action.
 func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.TaskRelayInfo) (taskErr *dto.TaskError) {
 	// Accept only POST /v1/video/generations as "generate" action.
-	action := "generate"
+	action := constant.TaskActionGenerate
 	info.Action = action
 
 	var req SubmitReq
@@ -112,7 +113,7 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
 
 // BuildRequestURL constructs the upstream URL.
 func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string, error) {
-	path := lo.Ternary(info.Action == "generate", "/v1/videos/image2video", "/v1/videos/text2video")
+	path := lo.Ternary(info.Action == constant.TaskActionGenerate, "/v1/videos/image2video", "/v1/videos/text2video")
 	return fmt.Sprintf("%s%s", a.baseURL, path), nil
 }
 
@@ -198,7 +199,7 @@ func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http
 	if !ok {
 		return nil, fmt.Errorf("invalid action")
 	}
-	path := lo.Ternary(action == "generate", "/v1/videos/image2video", "/v1/videos/text2video")
+	path := lo.Ternary(action == constant.TaskActionGenerate, "/v1/videos/image2video", "/v1/videos/text2video")
 	url := fmt.Sprintf("%s%s/%s", baseUrl, path, taskID)
 
 	req, err := http.NewRequest(http.MethodGet, url, nil)

+ 4 - 3
web/src/components/table/TaskLogsTable.js

@@ -49,6 +49,7 @@ import {
   IconSearch,
 } from '@douyinfe/semi-icons';
 import { useTableCompactMode } from '../../hooks/useTableCompactMode';
+import { TASK_ACTION_GENERATE, TASK_ACTION_TEXT_GENERATE } from '../../constants/common.constant';
 
 const { Text } = Typography;
 
@@ -207,13 +208,13 @@ const LogsTable = () => {
             {t('生成歌词')}
           </Tag>
         );
-      case 'generate':
+      case TASK_ACTION_GENERATE:
         return (
           <Tag color='blue' size='large' shape='circle' prefixIcon={<Sparkles size={14} />}>
             {t('图生视频')}
           </Tag>
         );
-      case 'textGenerate':
+      case TASK_ACTION_TEXT_GENERATE:
         return (
           <Tag color='blue' size='large' shape='circle' prefixIcon={<Sparkles size={14} />}>
             {t('文生视频')}
@@ -444,7 +445,7 @@ const LogsTable = () => {
       fixed: 'right',
       render: (text, record, index) => {
         // 仅当为视频生成任务且成功,且 fail_reason 是 URL 时显示可点击链接
-        const isVideoTask = record.action === 'generate' || record.action === 'textGenerate';
+        const isVideoTask = record.action === TASK_ACTION_GENERATE || record.action === TASK_ACTION_TEXT_GENERATE;
         const isSuccess = record.status === 'SUCCESS';
         const isUrl = typeof text === 'string' && /^https?:\/\//.test(text);
         if (isSuccess && isVideoTask && isUrl) {

+ 4 - 1
web/src/constants/common.constant.js

@@ -17,4 +17,7 @@ export const API_ENDPOINTS = [
   '/v1/audio/speech',
   '/v1/audio/transcriptions',
   '/v1/audio/translations'
-];
+];
+
+export const TASK_ACTION_GENERATE = 'generate';
+export const TASK_ACTION_TEXT_GENERATE = 'textGenerate';