Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
13d14dc8
authored
Aug 27, 2025
by
creamlike1024
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'alpha' of github.com:iszcz/new-api into iszcz-alpha
parents
d1f3f2c3
d904f9b4
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
88 additions
and
94 deletions
+88
-94
controller/relay.go
+12
-8
model/task.go
+1
-1
relay/channel/adapter.go
+7
-7
relay/channel/api_request.go
+2
-2
relay/channel/task/jimeng/adaptor.go
+7
-7
relay/channel/task/kling/adaptor.go
+9
-8
relay/channel/task/suno/adaptor.go
+9
-8
relay/channel/task/vidu/adaptor.go
+7
-7
relay/common/relay_info.go
+5
-13
relay/relay_task.go
+29
-33
No files found.
controller/relay.go
View file @
13d14dc8
...
...
@@ -3,7 +3,6 @@ package controller
import
(
"bytes"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"io"
"log"
"net/http"
...
...
@@ -22,6 +21,8 @@ import (
"one-api/types"
"strings"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
...
...
@@ -383,11 +384,14 @@ func RelayNotFound(c *gin.Context) {
func
RelayTask
(
c
*
gin
.
Context
)
{
retryTimes
:=
common
.
RetryTimes
channelId
:=
c
.
GetInt
(
"channel_id"
)
relayMode
:=
c
.
GetInt
(
"relay_mode"
)
group
:=
c
.
GetString
(
"group"
)
originalModel
:=
c
.
GetString
(
"original_model"
)
c
.
Set
(
"use_channel"
,
[]
string
{
fmt
.
Sprintf
(
"%d"
,
channelId
)})
taskErr
:=
taskRelayHandler
(
c
,
relayMode
)
relayInfo
,
err
:=
relaycommon
.
GenRelayInfo
(
c
,
types
.
RelayFormatTask
,
nil
,
nil
)
if
err
!=
nil
{
return
}
taskErr
:=
taskRelayHandler
(
c
,
relayInfo
)
if
taskErr
==
nil
{
retryTimes
=
0
}
...
...
@@ -407,7 +411,7 @@ func RelayTask(c *gin.Context) {
requestBody
,
_
:=
common
.
GetRequestBody
(
c
)
c
.
Request
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
requestBody
))
taskErr
=
taskRelayHandler
(
c
,
relay
Mode
)
taskErr
=
taskRelayHandler
(
c
,
relay
Info
)
}
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
if
len
(
useChannel
)
>
1
{
...
...
@@ -422,13 +426,13 @@ func RelayTask(c *gin.Context) {
}
}
func
taskRelayHandler
(
c
*
gin
.
Context
,
relay
Mode
int
)
*
dto
.
TaskError
{
func
taskRelayHandler
(
c
*
gin
.
Context
,
relay
Info
*
relaycommon
.
RelayInfo
)
*
dto
.
TaskError
{
var
err
*
dto
.
TaskError
switch
relayMode
{
switch
relay
Info
.
Relay
Mode
{
case
relayconstant
.
RelayModeSunoFetch
,
relayconstant
.
RelayModeSunoFetchByID
,
relayconstant
.
RelayModeVideoFetchByID
:
err
=
relay
.
RelayTaskFetch
(
c
,
relayMode
)
err
=
relay
.
RelayTaskFetch
(
c
,
relay
Info
.
Relay
Mode
)
default
:
err
=
relay
.
RelayTaskSubmit
(
c
,
relay
Mode
)
err
=
relay
.
RelayTaskSubmit
(
c
,
relay
Info
)
}
return
err
}
...
...
model/task.go
View file @
13d14dc8
...
...
@@ -77,7 +77,7 @@ type SyncTaskQueryParams struct {
UserIDs
[]
int
}
func
InitTask
(
platform
constant
.
TaskPlatform
,
relayInfo
*
commonRelay
.
Task
RelayInfo
)
*
Task
{
func
InitTask
(
platform
constant
.
TaskPlatform
,
relayInfo
*
commonRelay
.
RelayInfo
)
*
Task
{
t
:=
&
Task
{
UserId
:
relayInfo
.
UserId
,
SubmitTime
:
time
.
Now
()
.
Unix
(),
...
...
relay/channel/adapter.go
View file @
13d14dc8
...
...
@@ -30,16 +30,16 @@ type Adaptor interface {
}
type
TaskAdaptor
interface
{
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
Init
(
info
*
relaycommon
.
RelayInfo
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
*
dto
.
TaskError
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
*
dto
.
TaskError
BuildRequestURL
(
info
*
relaycommon
.
Task
RelayInfo
)
(
string
,
error
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
Task
RelayInfo
)
error
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
(
io
.
Reader
,
error
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
io
.
Reader
,
error
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
Task
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
err
*
dto
.
TaskError
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
err
*
dto
.
TaskError
)
GetModelList
()
[]
string
GetChannelName
()
string
...
...
relay/channel/api_request.go
View file @
13d14dc8
...
...
@@ -277,7 +277,7 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http
return
resp
,
nil
}
func
DoTaskApiRequest
(
a
TaskAdaptor
,
c
*
gin
.
Context
,
info
*
common
.
Task
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
func
DoTaskApiRequest
(
a
TaskAdaptor
,
c
*
gin
.
Context
,
info
*
common
.
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
fullRequestURL
,
err
:=
a
.
BuildRequestURL
(
info
)
if
err
!=
nil
{
return
nil
,
err
...
...
@@ -294,7 +294,7 @@ func DoTaskApiRequest(a TaskAdaptor, c *gin.Context, info *common.TaskRelayInfo,
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"setup request header failed: %w"
,
err
)
}
resp
,
err
:=
doRequest
(
c
,
req
,
info
.
RelayInfo
)
resp
,
err
:=
doRequest
(
c
,
req
,
info
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"do request failed: %w"
,
err
)
}
...
...
relay/channel/task/jimeng/adaptor.go
View file @
13d14dc8
...
...
@@ -74,7 +74,7 @@ type TaskAdaptor struct {
baseURL
string
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
...
...
@@ -87,7 +87,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
.
Task
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
// Accept only POST /v1/video/generations as "generate" action.
action
:=
constant
.
TaskActionGenerate
info
.
Action
=
action
...
...
@@ -108,19 +108,19 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
}
// BuildRequestURL constructs the upstream URL.
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
Task
RelayInfo
)
(
string
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
return
fmt
.
Sprintf
(
"%s/?Action=CVSync2AsyncSubmitTask&Version=2022-08-31"
,
a
.
baseURL
),
nil
}
// BuildRequestHeader sets required headers.
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
Task
RelayInfo
)
error
{
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
{
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
return
a
.
signRequest
(
req
,
a
.
accessKey
,
a
.
secretKey
)
}
// BuildRequestBody converts request into Jimeng specific format.
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
(
io
.
Reader
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
io
.
Reader
,
error
)
{
v
,
exists
:=
c
.
Get
(
"task_request"
)
if
!
exists
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
...
@@ -139,12 +139,12 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
}
// DoRequest delegates to common helper.
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
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.
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
Task
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/channel/task/kling/adaptor.go
View file @
13d14dc8
...
...
@@ -4,13 +4,14 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/samber/lo"
"io"
"net/http"
"one-api/model"
"strings"
"time"
"github.com/samber/lo"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
"github.com/pkg/errors"
...
...
@@ -79,7 +80,7 @@ type TaskAdaptor struct {
baseURL
string
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
a
.
apiKey
=
info
.
ApiKey
...
...
@@ -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
.
Task
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
// Accept only POST /v1/video/generations as "generate" action.
action
:=
constant
.
TaskActionGenerate
info
.
Action
=
action
...
...
@@ -109,13 +110,13 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
}
// BuildRequestURL constructs the upstream URL.
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
Task
RelayInfo
)
(
string
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
path
:=
lo
.
Ternary
(
info
.
Action
==
constant
.
TaskActionGenerate
,
"/v1/videos/image2video"
,
"/v1/videos/text2video"
)
return
fmt
.
Sprintf
(
"%s%s"
,
a
.
baseURL
,
path
),
nil
}
// BuildRequestHeader sets required headers.
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
Task
RelayInfo
)
error
{
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
{
token
,
err
:=
a
.
createJWTToken
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to create JWT token: %w"
,
err
)
...
...
@@ -129,7 +130,7 @@ func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info
}
// BuildRequestBody converts request into Kling specific format.
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
(
io
.
Reader
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
io
.
Reader
,
error
)
{
v
,
exists
:=
c
.
Get
(
"task_request"
)
if
!
exists
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
...
@@ -148,7 +149,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
}
// DoRequest delegates to common helper.
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
if
action
:=
c
.
GetString
(
"action"
);
action
!=
""
{
info
.
Action
=
action
}
...
...
@@ -156,7 +157,7 @@ func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.TaskRelayInfo,
}
// DoResponse handles upstream response, returns taskID etc.
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
Task
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/channel/task/suno/adaptor.go
View file @
13d14dc8
...
...
@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"one-api/common"
...
...
@@ -16,6 +15,8 @@ import (
"one-api/service"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type
TaskAdaptor
struct
{
...
...
@@ -26,11 +27,11 @@ func (a *TaskAdaptor) ParseTaskResult([]byte) (*relaycommon.TaskInfo, error) {
return
nil
,
fmt
.
Errorf
(
"not implement"
)
// todo implement this method if needed
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
}
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
action
:=
strings
.
ToUpper
(
c
.
Param
(
"action"
))
var
sunoRequest
*
dto
.
SunoSubmitReq
...
...
@@ -58,20 +59,20 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
return
nil
}
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
Task
RelayInfo
)
(
string
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
baseURL
:=
info
.
ChannelBaseUrl
fullRequestURL
:=
fmt
.
Sprintf
(
"%s%s"
,
baseURL
,
"/suno/submit/"
+
info
.
Action
)
return
fullRequestURL
,
nil
}
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
Task
RelayInfo
)
error
{
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
{
req
.
Header
.
Set
(
"Content-Type"
,
c
.
Request
.
Header
.
Get
(
"Content-Type"
))
req
.
Header
.
Set
(
"Accept"
,
c
.
Request
.
Header
.
Get
(
"Accept"
))
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
info
.
ApiKey
)
return
nil
}
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
(
io
.
Reader
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
io
.
Reader
,
error
)
{
sunoRequest
,
ok
:=
c
.
Get
(
"task_request"
)
if
!
ok
{
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
sunoRequest
)
...
...
@@ -86,11 +87,11 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
return
bytes
.
NewReader
(
data
),
nil
}
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
return
channel
.
DoTaskApiRequest
(
a
,
c
,
info
,
requestBody
)
}
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
Task
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/channel/task/vidu/adaptor.go
View file @
13d14dc8
...
...
@@ -84,12 +84,12 @@ type TaskAdaptor struct {
baseURL
string
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
}
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
*
dto
.
TaskError
{
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
*
dto
.
TaskError
{
var
req
SubmitReq
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
return
service
.
TaskErrorWrapper
(
err
,
"invalid_request_body"
,
http
.
StatusBadRequest
)
...
...
@@ -109,7 +109,7 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
return
nil
}
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
_
*
relaycommon
.
Task
RelayInfo
)
(
io
.
Reader
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
_
*
relaycommon
.
RelayInfo
)
(
io
.
Reader
,
error
)
{
v
,
exists
:=
c
.
Get
(
"task_request"
)
if
!
exists
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
...
@@ -132,7 +132,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayI
return
bytes
.
NewReader
(
data
),
nil
}
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
Task
RelayInfo
)
(
string
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
var
path
string
switch
info
.
Action
{
case
constant
.
TaskActionGenerate
:
...
...
@@ -143,21 +143,21 @@ func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string,
return
fmt
.
Sprintf
(
"%s/ent/v2%s"
,
a
.
baseURL
,
path
),
nil
}
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
Task
RelayInfo
)
error
{
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
{
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
req
.
Header
.
Set
(
"Authorization"
,
"Token "
+
info
.
ApiKey
)
return
nil
}
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
if
action
:=
c
.
GetString
(
"action"
);
action
!=
""
{
info
.
Action
=
action
}
return
channel
.
DoTaskApiRequest
(
a
,
c
,
info
,
requestBody
)
}
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
_
*
relaycommon
.
Task
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
_
*
relaycommon
.
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/common/relay_info.go
View file @
13d14dc8
...
...
@@ -116,6 +116,7 @@ type RelayInfo struct {
*
RerankerInfo
*
ResponsesUsageInfo
*
ChannelMeta
*
TaskRelayInfo
}
func
(
info
*
RelayInfo
)
InitChannelMeta
(
c
*
gin
.
Context
)
{
...
...
@@ -400,6 +401,10 @@ func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
},
}
if
info
.
RelayMode
==
relayconstant
.
RelayModeUnknown
{
info
.
RelayMode
=
c
.
GetInt
(
"relay_mode"
)
}
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/pg"
)
{
info
.
IsPlayground
=
true
info
.
RequestURLPath
=
strings
.
TrimPrefix
(
info
.
RequestURLPath
,
"/pg"
)
...
...
@@ -465,25 +470,12 @@ func (info *RelayInfo) HasSendResponse() bool {
}
type
TaskRelayInfo
struct
{
*
RelayInfo
Action
string
OriginTaskID
string
ConsumeQuota
bool
}
func
GenTaskRelayInfo
(
c
*
gin
.
Context
)
(
*
TaskRelayInfo
,
error
)
{
relayInfo
,
err
:=
GenRelayInfo
(
c
,
types
.
RelayFormatTask
,
nil
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
info
:=
&
TaskRelayInfo
{
RelayInfo
:
relayInfo
,
}
info
.
InitChannelMeta
(
c
)
return
info
,
nil
}
type
TaskSubmitReq
struct
{
Prompt
string
`json:"prompt"`
Model
string
`json:"model,omitempty"`
...
...
relay/relay_task.go
View file @
13d14dc8
...
...
@@ -22,31 +22,27 @@ import (
/*
Task 任务通过平台、Action 区分任务
*/
func
RelayTaskSubmit
(
c
*
gin
.
Context
,
relayMode
int
)
(
taskErr
*
dto
.
TaskError
)
{
func
RelayTaskSubmit
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
info
.
InitChannelMeta
(
c
)
platform
:=
constant
.
TaskPlatform
(
c
.
GetString
(
"platform"
))
if
platform
==
""
{
platform
=
GetTaskPlatform
(
c
)
}
relayInfo
,
err
:=
relaycommon
.
GenTaskRelayInfo
(
c
)
if
err
!=
nil
{
return
service
.
TaskErrorWrapper
(
err
,
"gen_relay_info_failed"
,
http
.
StatusInternalServerError
)
}
adaptor
:=
GetTaskAdaptor
(
platform
)
if
adaptor
==
nil
{
return
service
.
TaskErrorWrapperLocal
(
fmt
.
Errorf
(
"invalid api platform: %s"
,
platform
),
"invalid_api_platform"
,
http
.
StatusBadRequest
)
}
adaptor
.
Init
(
relayI
nfo
)
adaptor
.
Init
(
i
nfo
)
// get & validate taskRequest 获取并验证文本请求
taskErr
=
adaptor
.
ValidateRequestAndSetAction
(
c
,
relayI
nfo
)
taskErr
=
adaptor
.
ValidateRequestAndSetAction
(
c
,
i
nfo
)
if
taskErr
!=
nil
{
return
}
modelName
:=
relayI
nfo
.
OriginModelName
modelName
:=
i
nfo
.
OriginModelName
if
modelName
==
""
{
modelName
=
service
.
CoverTaskActionToModelName
(
platform
,
relayI
nfo
.
Action
)
modelName
=
service
.
CoverTaskActionToModelName
(
platform
,
i
nfo
.
Action
)
}
modelPrice
,
success
:=
ratio_setting
.
GetModelPrice
(
modelName
,
true
)
if
!
success
{
...
...
@@ -59,15 +55,15 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
}
// 预扣
groupRatio
:=
ratio_setting
.
GetGroupRatio
(
relayI
nfo
.
UsingGroup
)
groupRatio
:=
ratio_setting
.
GetGroupRatio
(
i
nfo
.
UsingGroup
)
var
ratio
float64
userGroupRatio
,
hasUserGroupRatio
:=
ratio_setting
.
GetGroupGroupRatio
(
relayInfo
.
UserGroup
,
relayI
nfo
.
UsingGroup
)
userGroupRatio
,
hasUserGroupRatio
:=
ratio_setting
.
GetGroupGroupRatio
(
info
.
UserGroup
,
i
nfo
.
UsingGroup
)
if
hasUserGroupRatio
{
ratio
=
modelPrice
*
userGroupRatio
}
else
{
ratio
=
modelPrice
*
groupRatio
}
userQuota
,
err
:=
model
.
GetUserQuota
(
relayI
nfo
.
UserId
,
false
)
userQuota
,
err
:=
model
.
GetUserQuota
(
i
nfo
.
UserId
,
false
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
...
...
@@ -78,8 +74,8 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
return
}
if
relayI
nfo
.
OriginTaskID
!=
""
{
originTask
,
exist
,
err
:=
model
.
GetByTaskId
(
relayInfo
.
UserId
,
relayI
nfo
.
OriginTaskID
)
if
i
nfo
.
OriginTaskID
!=
""
{
originTask
,
exist
,
err
:=
model
.
GetByTaskId
(
info
.
UserId
,
i
nfo
.
OriginTaskID
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"get_origin_task_failed"
,
http
.
StatusInternalServerError
)
return
...
...
@@ -88,7 +84,7 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
taskErr
=
service
.
TaskErrorWrapperLocal
(
errors
.
New
(
"task_origin_not_exist"
),
"task_not_exist"
,
http
.
StatusBadRequest
)
return
}
if
originTask
.
ChannelId
!=
relayI
nfo
.
ChannelId
{
if
originTask
.
ChannelId
!=
i
nfo
.
ChannelId
{
channel
,
err
:=
model
.
GetChannelById
(
originTask
.
ChannelId
,
true
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapperLocal
(
err
,
"channel_not_found"
,
http
.
StatusBadRequest
)
...
...
@@ -101,19 +97,19 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
c
.
Set
(
"channel_id"
,
originTask
.
ChannelId
)
c
.
Request
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
channel
.
Key
))
relayI
nfo
.
ChannelBaseUrl
=
channel
.
GetBaseURL
()
relayI
nfo
.
ChannelId
=
originTask
.
ChannelId
i
nfo
.
ChannelBaseUrl
=
channel
.
GetBaseURL
()
i
nfo
.
ChannelId
=
originTask
.
ChannelId
}
}
// build body
requestBody
,
err
:=
adaptor
.
BuildRequestBody
(
c
,
relayI
nfo
)
requestBody
,
err
:=
adaptor
.
BuildRequestBody
(
c
,
i
nfo
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"build_request_failed"
,
http
.
StatusInternalServerError
)
return
}
// do request
resp
,
err
:=
adaptor
.
DoRequest
(
c
,
relayI
nfo
,
requestBody
)
resp
,
err
:=
adaptor
.
DoRequest
(
c
,
i
nfo
,
requestBody
)
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"do_request_failed"
,
http
.
StatusInternalServerError
)
return
...
...
@@ -127,9 +123,9 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
defer
func
()
{
// release quota
if
relayI
nfo
.
ConsumeQuota
&&
taskErr
==
nil
{
if
i
nfo
.
ConsumeQuota
&&
taskErr
==
nil
{
err
:=
service
.
PostConsumeQuota
(
relayInfo
.
RelayI
nfo
,
quota
,
0
,
true
)
err
:=
service
.
PostConsumeQuota
(
i
nfo
,
quota
,
0
,
true
)
if
err
!=
nil
{
common
.
SysLog
(
"error consuming token remain quota: "
+
err
.
Error
())
}
...
...
@@ -139,40 +135,40 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
if
hasUserGroupRatio
{
gRatio
=
userGroupRatio
}
logContent
:=
fmt
.
Sprintf
(
"模型固定价格 %.2f,分组倍率 %.2f,操作 %s"
,
modelPrice
,
gRatio
,
relayI
nfo
.
Action
)
logContent
:=
fmt
.
Sprintf
(
"模型固定价格 %.2f,分组倍率 %.2f,操作 %s"
,
modelPrice
,
gRatio
,
i
nfo
.
Action
)
other
:=
make
(
map
[
string
]
interface
{})
other
[
"model_price"
]
=
modelPrice
other
[
"group_ratio"
]
=
groupRatio
if
hasUserGroupRatio
{
other
[
"user_group_ratio"
]
=
userGroupRatio
}
model
.
RecordConsumeLog
(
c
,
relayI
nfo
.
UserId
,
model
.
RecordConsumeLogParams
{
ChannelId
:
relayI
nfo
.
ChannelId
,
model
.
RecordConsumeLog
(
c
,
i
nfo
.
UserId
,
model
.
RecordConsumeLogParams
{
ChannelId
:
i
nfo
.
ChannelId
,
ModelName
:
modelName
,
TokenName
:
tokenName
,
Quota
:
quota
,
Content
:
logContent
,
TokenId
:
relayI
nfo
.
TokenId
,
Group
:
relayI
nfo
.
UsingGroup
,
TokenId
:
i
nfo
.
TokenId
,
Group
:
i
nfo
.
UsingGroup
,
Other
:
other
,
})
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayI
nfo
.
UserId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayI
nfo
.
ChannelId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
i
nfo
.
UserId
,
quota
)
model
.
UpdateChannelUsedQuota
(
i
nfo
.
ChannelId
,
quota
)
}
}
}()
taskID
,
taskData
,
taskErr
:=
adaptor
.
DoResponse
(
c
,
resp
,
relayI
nfo
)
taskID
,
taskData
,
taskErr
:=
adaptor
.
DoResponse
(
c
,
resp
,
i
nfo
)
if
taskErr
!=
nil
{
return
}
relayI
nfo
.
ConsumeQuota
=
true
i
nfo
.
ConsumeQuota
=
true
// insert task
task
:=
model
.
InitTask
(
platform
,
relayI
nfo
)
task
:=
model
.
InitTask
(
platform
,
i
nfo
)
task
.
TaskID
=
taskID
task
.
Quota
=
quota
task
.
Data
=
taskData
task
.
Action
=
relayI
nfo
.
Action
task
.
Action
=
i
nfo
.
Action
err
=
task
.
Insert
()
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"insert_task_failed"
,
http
.
StatusInternalServerError
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment