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
d904f9b4
authored
Aug 25, 2025
by
iszcz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
task_relay_info
parent
5d384fa4
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 @
d904f9b4
...
@@ -3,7 +3,6 @@ package controller
...
@@ -3,7 +3,6 @@ package controller
import
(
import
(
"bytes"
"bytes"
"fmt"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"io"
"io"
"log"
"log"
"net/http"
"net/http"
...
@@ -22,6 +21,8 @@ import (
...
@@ -22,6 +21,8 @@ import (
"one-api/types"
"one-api/types"
"strings"
"strings"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/gorilla/websocket"
)
)
...
@@ -383,11 +384,14 @@ func RelayNotFound(c *gin.Context) {
...
@@ -383,11 +384,14 @@ func RelayNotFound(c *gin.Context) {
func
RelayTask
(
c
*
gin
.
Context
)
{
func
RelayTask
(
c
*
gin
.
Context
)
{
retryTimes
:=
common
.
RetryTimes
retryTimes
:=
common
.
RetryTimes
channelId
:=
c
.
GetInt
(
"channel_id"
)
channelId
:=
c
.
GetInt
(
"channel_id"
)
relayMode
:=
c
.
GetInt
(
"relay_mode"
)
group
:=
c
.
GetString
(
"group"
)
group
:=
c
.
GetString
(
"group"
)
originalModel
:=
c
.
GetString
(
"original_model"
)
originalModel
:=
c
.
GetString
(
"original_model"
)
c
.
Set
(
"use_channel"
,
[]
string
{
fmt
.
Sprintf
(
"%d"
,
channelId
)})
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
{
if
taskErr
==
nil
{
retryTimes
=
0
retryTimes
=
0
}
}
...
@@ -407,7 +411,7 @@ func RelayTask(c *gin.Context) {
...
@@ -407,7 +411,7 @@ func RelayTask(c *gin.Context) {
requestBody
,
_
:=
common
.
GetRequestBody
(
c
)
requestBody
,
_
:=
common
.
GetRequestBody
(
c
)
c
.
Request
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
requestBody
))
c
.
Request
.
Body
=
io
.
NopCloser
(
bytes
.
NewBuffer
(
requestBody
))
taskErr
=
taskRelayHandler
(
c
,
relay
Mode
)
taskErr
=
taskRelayHandler
(
c
,
relay
Info
)
}
}
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
useChannel
:=
c
.
GetStringSlice
(
"use_channel"
)
if
len
(
useChannel
)
>
1
{
if
len
(
useChannel
)
>
1
{
...
@@ -422,13 +426,13 @@ func RelayTask(c *gin.Context) {
...
@@ -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
var
err
*
dto
.
TaskError
switch
relayMode
{
switch
relay
Info
.
Relay
Mode
{
case
relayconstant
.
RelayModeSunoFetch
,
relayconstant
.
RelayModeSunoFetchByID
,
relayconstant
.
RelayModeVideoFetchByID
:
case
relayconstant
.
RelayModeSunoFetch
,
relayconstant
.
RelayModeSunoFetchByID
,
relayconstant
.
RelayModeVideoFetchByID
:
err
=
relay
.
RelayTaskFetch
(
c
,
relayMode
)
err
=
relay
.
RelayTaskFetch
(
c
,
relay
Info
.
Relay
Mode
)
default
:
default
:
err
=
relay
.
RelayTaskSubmit
(
c
,
relay
Mode
)
err
=
relay
.
RelayTaskSubmit
(
c
,
relay
Info
)
}
}
return
err
return
err
}
}
...
...
model/task.go
View file @
d904f9b4
...
@@ -77,7 +77,7 @@ type SyncTaskQueryParams struct {
...
@@ -77,7 +77,7 @@ type SyncTaskQueryParams struct {
UserIDs
[]
int
UserIDs
[]
int
}
}
func
InitTask
(
platform
constant
.
TaskPlatform
,
relayInfo
*
commonRelay
.
Task
RelayInfo
)
*
Task
{
func
InitTask
(
platform
constant
.
TaskPlatform
,
relayInfo
*
commonRelay
.
RelayInfo
)
*
Task
{
t
:=
&
Task
{
t
:=
&
Task
{
UserId
:
relayInfo
.
UserId
,
UserId
:
relayInfo
.
UserId
,
SubmitTime
:
time
.
Now
()
.
Unix
(),
SubmitTime
:
time
.
Now
()
.
Unix
(),
...
...
relay/channel/adapter.go
View file @
d904f9b4
...
@@ -30,16 +30,16 @@ type Adaptor interface {
...
@@ -30,16 +30,16 @@ type Adaptor interface {
}
}
type
TaskAdaptor
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
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
Task
RelayInfo
)
error
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
RelayInfo
)
error
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
Task
RelayInfo
)
(
io
.
Reader
,
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
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
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
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
err
*
dto
.
TaskError
)
GetModelList
()
[]
string
GetModelList
()
[]
string
GetChannelName
()
string
GetChannelName
()
string
...
...
relay/channel/api_request.go
View file @
d904f9b4
...
@@ -277,7 +277,7 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http
...
@@ -277,7 +277,7 @@ func doRequest(c *gin.Context, req *http.Request, info *common.RelayInfo) (*http
return
resp
,
nil
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
)
fullRequestURL
,
err
:=
a
.
BuildRequestURL
(
info
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -294,7 +294,7 @@ func DoTaskApiRequest(a TaskAdaptor, c *gin.Context, info *common.TaskRelayInfo,
...
@@ -294,7 +294,7 @@ func DoTaskApiRequest(a TaskAdaptor, c *gin.Context, info *common.TaskRelayInfo,
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"setup request header failed: %w"
,
err
)
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
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"do request failed: %w"
,
err
)
return
nil
,
fmt
.
Errorf
(
"do request failed: %w"
,
err
)
}
}
...
...
relay/channel/task/jimeng/adaptor.go
View file @
d904f9b4
...
@@ -74,7 +74,7 @@ type TaskAdaptor struct {
...
@@ -74,7 +74,7 @@ type TaskAdaptor struct {
baseURL
string
baseURL
string
}
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
a
.
baseURL
=
info
.
ChannelBaseUrl
...
@@ -87,7 +87,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
...
@@ -87,7 +87,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
}
}
// ValidateRequestAndSetAction parses body, validates fields and sets default action.
// 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.
// Accept only POST /v1/video/generations as "generate" action.
action
:=
constant
.
TaskActionGenerate
action
:=
constant
.
TaskActionGenerate
info
.
Action
=
action
info
.
Action
=
action
...
@@ -108,19 +108,19 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
...
@@ -108,19 +108,19 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
}
}
// BuildRequestURL constructs the upstream URL.
// 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
return
fmt
.
Sprintf
(
"%s/?Action=CVSync2AsyncSubmitTask&Version=2022-08-31"
,
a
.
baseURL
),
nil
}
}
// BuildRequestHeader sets required headers.
// 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
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
return
a
.
signRequest
(
req
,
a
.
accessKey
,
a
.
secretKey
)
return
a
.
signRequest
(
req
,
a
.
accessKey
,
a
.
secretKey
)
}
}
// BuildRequestBody converts request into Jimeng specific format.
// 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"
)
v
,
exists
:=
c
.
Get
(
"task_request"
)
if
!
exists
{
if
!
exists
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
@@ -139,12 +139,12 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
...
@@ -139,12 +139,12 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
}
}
// DoRequest delegates to common helper.
// 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
)
return
channel
.
DoTaskApiRequest
(
a
,
c
,
info
,
requestBody
)
}
}
// DoResponse handles upstream response, returns taskID etc.
// 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
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/channel/task/kling/adaptor.go
View file @
d904f9b4
...
@@ -4,13 +4,14 @@ import (
...
@@ -4,13 +4,14 @@ import (
"bytes"
"bytes"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"github.com/samber/lo"
"io"
"io"
"net/http"
"net/http"
"one-api/model"
"one-api/model"
"strings"
"strings"
"time"
"time"
"github.com/samber/lo"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
"github.com/golang-jwt/jwt"
"github.com/pkg/errors"
"github.com/pkg/errors"
...
@@ -79,7 +80,7 @@ type TaskAdaptor struct {
...
@@ -79,7 +80,7 @@ type TaskAdaptor struct {
baseURL
string
baseURL
string
}
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
a
.
baseURL
=
info
.
ChannelBaseUrl
a
.
apiKey
=
info
.
ApiKey
a
.
apiKey
=
info
.
ApiKey
...
@@ -88,7 +89,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
...
@@ -88,7 +89,7 @@ func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
}
}
// ValidateRequestAndSetAction parses body, validates fields and sets default action.
// 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.
// Accept only POST /v1/video/generations as "generate" action.
action
:=
constant
.
TaskActionGenerate
action
:=
constant
.
TaskActionGenerate
info
.
Action
=
action
info
.
Action
=
action
...
@@ -109,13 +110,13 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
...
@@ -109,13 +110,13 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
}
}
// BuildRequestURL constructs the upstream URL.
// 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"
)
path
:=
lo
.
Ternary
(
info
.
Action
==
constant
.
TaskActionGenerate
,
"/v1/videos/image2video"
,
"/v1/videos/text2video"
)
return
fmt
.
Sprintf
(
"%s%s"
,
a
.
baseURL
,
path
),
nil
return
fmt
.
Sprintf
(
"%s%s"
,
a
.
baseURL
,
path
),
nil
}
}
// BuildRequestHeader sets required headers.
// 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
()
token
,
err
:=
a
.
createJWTToken
()
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to create JWT token: %w"
,
err
)
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
...
@@ -129,7 +130,7 @@ func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info
}
}
// BuildRequestBody converts request into Kling specific format.
// 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"
)
v
,
exists
:=
c
.
Get
(
"task_request"
)
if
!
exists
{
if
!
exists
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
@@ -148,7 +149,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
...
@@ -148,7 +149,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
}
}
// DoRequest delegates to common helper.
// 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
!=
""
{
if
action
:=
c
.
GetString
(
"action"
);
action
!=
""
{
info
.
Action
=
action
info
.
Action
=
action
}
}
...
@@ -156,7 +157,7 @@ func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.TaskRelayInfo,
...
@@ -156,7 +157,7 @@ func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.TaskRelayInfo,
}
}
// DoResponse handles upstream response, returns taskID etc.
// 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
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/channel/task/suno/adaptor.go
View file @
d904f9b4
...
@@ -5,7 +5,6 @@ import (
...
@@ -5,7 +5,6 @@ import (
"context"
"context"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"github.com/gin-gonic/gin"
"io"
"io"
"net/http"
"net/http"
"one-api/common"
"one-api/common"
...
@@ -16,6 +15,8 @@ import (
...
@@ -16,6 +15,8 @@ import (
"one-api/service"
"one-api/service"
"strings"
"strings"
"time"
"time"
"github.com/gin-gonic/gin"
)
)
type
TaskAdaptor
struct
{
type
TaskAdaptor
struct
{
...
@@ -26,11 +27,11 @@ func (a *TaskAdaptor) ParseTaskResult([]byte) (*relaycommon.TaskInfo, error) {
...
@@ -26,11 +27,11 @@ func (a *TaskAdaptor) ParseTaskResult([]byte) (*relaycommon.TaskInfo, error) {
return
nil
,
fmt
.
Errorf
(
"not implement"
)
// todo implement this method if needed
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
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"
))
action
:=
strings
.
ToUpper
(
c
.
Param
(
"action"
))
var
sunoRequest
*
dto
.
SunoSubmitReq
var
sunoRequest
*
dto
.
SunoSubmitReq
...
@@ -58,20 +59,20 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
...
@@ -58,20 +59,20 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
return
nil
return
nil
}
}
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
Task
RelayInfo
)
(
string
,
error
)
{
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
baseURL
:=
info
.
ChannelBaseUrl
baseURL
:=
info
.
ChannelBaseUrl
fullRequestURL
:=
fmt
.
Sprintf
(
"%s%s"
,
baseURL
,
"/suno/submit/"
+
info
.
Action
)
fullRequestURL
:=
fmt
.
Sprintf
(
"%s%s"
,
baseURL
,
"/suno/submit/"
+
info
.
Action
)
return
fullRequestURL
,
nil
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
(
"Content-Type"
,
c
.
Request
.
Header
.
Get
(
"Content-Type"
))
req
.
Header
.
Set
(
"Accept"
,
c
.
Request
.
Header
.
Get
(
"Accept"
))
req
.
Header
.
Set
(
"Accept"
,
c
.
Request
.
Header
.
Get
(
"Accept"
))
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
info
.
ApiKey
)
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
info
.
ApiKey
)
return
nil
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"
)
sunoRequest
,
ok
:=
c
.
Get
(
"task_request"
)
if
!
ok
{
if
!
ok
{
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
sunoRequest
)
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
sunoRequest
)
...
@@ -86,11 +87,11 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
...
@@ -86,11 +87,11 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRel
return
bytes
.
NewReader
(
data
),
nil
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
)
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
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/channel/task/vidu/adaptor.go
View file @
d904f9b4
...
@@ -84,12 +84,12 @@ type TaskAdaptor struct {
...
@@ -84,12 +84,12 @@ type TaskAdaptor struct {
baseURL
string
baseURL
string
}
}
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
Task
RelayInfo
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
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
var
req
SubmitReq
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
if
err
:=
c
.
ShouldBindJSON
(
&
req
);
err
!=
nil
{
return
service
.
TaskErrorWrapper
(
err
,
"invalid_request_body"
,
http
.
StatusBadRequest
)
return
service
.
TaskErrorWrapper
(
err
,
"invalid_request_body"
,
http
.
StatusBadRequest
)
...
@@ -109,7 +109,7 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
...
@@ -109,7 +109,7 @@ func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycom
return
nil
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"
)
v
,
exists
:=
c
.
Get
(
"task_request"
)
if
!
exists
{
if
!
exists
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
@@ -132,7 +132,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayI
...
@@ -132,7 +132,7 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayI
return
bytes
.
NewReader
(
data
),
nil
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
var
path
string
switch
info
.
Action
{
switch
info
.
Action
{
case
constant
.
TaskActionGenerate
:
case
constant
.
TaskActionGenerate
:
...
@@ -143,21 +143,21 @@ func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string,
...
@@ -143,21 +143,21 @@ func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string,
return
fmt
.
Sprintf
(
"%s/ent/v2%s"
,
a
.
baseURL
,
path
),
nil
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
(
"Content-Type"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
req
.
Header
.
Set
(
"Accept"
,
"application/json"
)
req
.
Header
.
Set
(
"Authorization"
,
"Token "
+
info
.
ApiKey
)
req
.
Header
.
Set
(
"Authorization"
,
"Token "
+
info
.
ApiKey
)
return
nil
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
!=
""
{
if
action
:=
c
.
GetString
(
"action"
);
action
!=
""
{
info
.
Action
=
action
info
.
Action
=
action
}
}
return
channel
.
DoTaskApiRequest
(
a
,
c
,
info
,
requestBody
)
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
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
...
relay/common/relay_info.go
View file @
d904f9b4
...
@@ -116,6 +116,7 @@ type RelayInfo struct {
...
@@ -116,6 +116,7 @@ type RelayInfo struct {
*
RerankerInfo
*
RerankerInfo
*
ResponsesUsageInfo
*
ResponsesUsageInfo
*
ChannelMeta
*
ChannelMeta
*
TaskRelayInfo
}
}
func
(
info
*
RelayInfo
)
InitChannelMeta
(
c
*
gin
.
Context
)
{
func
(
info
*
RelayInfo
)
InitChannelMeta
(
c
*
gin
.
Context
)
{
...
@@ -400,6 +401,10 @@ func genBaseRelayInfo(c *gin.Context, request dto.Request) *RelayInfo {
...
@@ -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"
)
{
if
strings
.
HasPrefix
(
c
.
Request
.
URL
.
Path
,
"/pg"
)
{
info
.
IsPlayground
=
true
info
.
IsPlayground
=
true
info
.
RequestURLPath
=
strings
.
TrimPrefix
(
info
.
RequestURLPath
,
"/pg"
)
info
.
RequestURLPath
=
strings
.
TrimPrefix
(
info
.
RequestURLPath
,
"/pg"
)
...
@@ -465,25 +470,12 @@ func (info *RelayInfo) HasSendResponse() bool {
...
@@ -465,25 +470,12 @@ func (info *RelayInfo) HasSendResponse() bool {
}
}
type
TaskRelayInfo
struct
{
type
TaskRelayInfo
struct
{
*
RelayInfo
Action
string
Action
string
OriginTaskID
string
OriginTaskID
string
ConsumeQuota
bool
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
{
type
TaskSubmitReq
struct
{
Prompt
string
`json:"prompt"`
Prompt
string
`json:"prompt"`
Model
string
`json:"model,omitempty"`
Model
string
`json:"model,omitempty"`
...
...
relay/relay_task.go
View file @
d904f9b4
...
@@ -22,31 +22,27 @@ import (
...
@@ -22,31 +22,27 @@ import (
/*
/*
Task 任务通过平台、Action 区分任务
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"
))
platform
:=
constant
.
TaskPlatform
(
c
.
GetString
(
"platform"
))
if
platform
==
""
{
if
platform
==
""
{
platform
=
GetTaskPlatform
(
c
)
platform
=
GetTaskPlatform
(
c
)
}
}
relayInfo
,
err
:=
relaycommon
.
GenTaskRelayInfo
(
c
)
if
err
!=
nil
{
return
service
.
TaskErrorWrapper
(
err
,
"gen_relay_info_failed"
,
http
.
StatusInternalServerError
)
}
adaptor
:=
GetTaskAdaptor
(
platform
)
adaptor
:=
GetTaskAdaptor
(
platform
)
if
adaptor
==
nil
{
if
adaptor
==
nil
{
return
service
.
TaskErrorWrapperLocal
(
fmt
.
Errorf
(
"invalid api platform: %s"
,
platform
),
"invalid_api_platform"
,
http
.
StatusBadRequest
)
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 获取并验证文本请求
// get & validate taskRequest 获取并验证文本请求
taskErr
=
adaptor
.
ValidateRequestAndSetAction
(
c
,
relayI
nfo
)
taskErr
=
adaptor
.
ValidateRequestAndSetAction
(
c
,
i
nfo
)
if
taskErr
!=
nil
{
if
taskErr
!=
nil
{
return
return
}
}
modelName
:=
relayI
nfo
.
OriginModelName
modelName
:=
i
nfo
.
OriginModelName
if
modelName
==
""
{
if
modelName
==
""
{
modelName
=
service
.
CoverTaskActionToModelName
(
platform
,
relayI
nfo
.
Action
)
modelName
=
service
.
CoverTaskActionToModelName
(
platform
,
i
nfo
.
Action
)
}
}
modelPrice
,
success
:=
ratio_setting
.
GetModelPrice
(
modelName
,
true
)
modelPrice
,
success
:=
ratio_setting
.
GetModelPrice
(
modelName
,
true
)
if
!
success
{
if
!
success
{
...
@@ -59,15 +55,15 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
...
@@ -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
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
{
if
hasUserGroupRatio
{
ratio
=
modelPrice
*
userGroupRatio
ratio
=
modelPrice
*
userGroupRatio
}
else
{
}
else
{
ratio
=
modelPrice
*
groupRatio
ratio
=
modelPrice
*
groupRatio
}
}
userQuota
,
err
:=
model
.
GetUserQuota
(
relayI
nfo
.
UserId
,
false
)
userQuota
,
err
:=
model
.
GetUserQuota
(
i
nfo
.
UserId
,
false
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
return
...
@@ -78,8 +74,8 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
...
@@ -78,8 +74,8 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
return
return
}
}
if
relayI
nfo
.
OriginTaskID
!=
""
{
if
i
nfo
.
OriginTaskID
!=
""
{
originTask
,
exist
,
err
:=
model
.
GetByTaskId
(
relayInfo
.
UserId
,
relayI
nfo
.
OriginTaskID
)
originTask
,
exist
,
err
:=
model
.
GetByTaskId
(
info
.
UserId
,
i
nfo
.
OriginTaskID
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"get_origin_task_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"get_origin_task_failed"
,
http
.
StatusInternalServerError
)
return
return
...
@@ -88,7 +84,7 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
...
@@ -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
)
taskErr
=
service
.
TaskErrorWrapperLocal
(
errors
.
New
(
"task_origin_not_exist"
),
"task_not_exist"
,
http
.
StatusBadRequest
)
return
return
}
}
if
originTask
.
ChannelId
!=
relayI
nfo
.
ChannelId
{
if
originTask
.
ChannelId
!=
i
nfo
.
ChannelId
{
channel
,
err
:=
model
.
GetChannelById
(
originTask
.
ChannelId
,
true
)
channel
,
err
:=
model
.
GetChannelById
(
originTask
.
ChannelId
,
true
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapperLocal
(
err
,
"channel_not_found"
,
http
.
StatusBadRequest
)
taskErr
=
service
.
TaskErrorWrapperLocal
(
err
,
"channel_not_found"
,
http
.
StatusBadRequest
)
...
@@ -101,19 +97,19 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
...
@@ -101,19 +97,19 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
c
.
Set
(
"channel_id"
,
originTask
.
ChannelId
)
c
.
Set
(
"channel_id"
,
originTask
.
ChannelId
)
c
.
Request
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
channel
.
Key
))
c
.
Request
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
channel
.
Key
))
relayI
nfo
.
ChannelBaseUrl
=
channel
.
GetBaseURL
()
i
nfo
.
ChannelBaseUrl
=
channel
.
GetBaseURL
()
relayI
nfo
.
ChannelId
=
originTask
.
ChannelId
i
nfo
.
ChannelId
=
originTask
.
ChannelId
}
}
}
}
// build body
// build body
requestBody
,
err
:=
adaptor
.
BuildRequestBody
(
c
,
relayI
nfo
)
requestBody
,
err
:=
adaptor
.
BuildRequestBody
(
c
,
i
nfo
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"build_request_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"build_request_failed"
,
http
.
StatusInternalServerError
)
return
return
}
}
// do request
// do request
resp
,
err
:=
adaptor
.
DoRequest
(
c
,
relayI
nfo
,
requestBody
)
resp
,
err
:=
adaptor
.
DoRequest
(
c
,
i
nfo
,
requestBody
)
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"do_request_failed"
,
http
.
StatusInternalServerError
)
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"do_request_failed"
,
http
.
StatusInternalServerError
)
return
return
...
@@ -127,9 +123,9 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
...
@@ -127,9 +123,9 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
defer
func
()
{
defer
func
()
{
// release quota
// 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
{
if
err
!=
nil
{
common
.
SysLog
(
"error consuming token remain quota: "
+
err
.
Error
())
common
.
SysLog
(
"error consuming token remain quota: "
+
err
.
Error
())
}
}
...
@@ -139,40 +135,40 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
...
@@ -139,40 +135,40 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) {
if
hasUserGroupRatio
{
if
hasUserGroupRatio
{
gRatio
=
userGroupRatio
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
:=
make
(
map
[
string
]
interface
{})
other
[
"model_price"
]
=
modelPrice
other
[
"model_price"
]
=
modelPrice
other
[
"group_ratio"
]
=
groupRatio
other
[
"group_ratio"
]
=
groupRatio
if
hasUserGroupRatio
{
if
hasUserGroupRatio
{
other
[
"user_group_ratio"
]
=
userGroupRatio
other
[
"user_group_ratio"
]
=
userGroupRatio
}
}
model
.
RecordConsumeLog
(
c
,
relayI
nfo
.
UserId
,
model
.
RecordConsumeLogParams
{
model
.
RecordConsumeLog
(
c
,
i
nfo
.
UserId
,
model
.
RecordConsumeLogParams
{
ChannelId
:
relayI
nfo
.
ChannelId
,
ChannelId
:
i
nfo
.
ChannelId
,
ModelName
:
modelName
,
ModelName
:
modelName
,
TokenName
:
tokenName
,
TokenName
:
tokenName
,
Quota
:
quota
,
Quota
:
quota
,
Content
:
logContent
,
Content
:
logContent
,
TokenId
:
relayI
nfo
.
TokenId
,
TokenId
:
i
nfo
.
TokenId
,
Group
:
relayI
nfo
.
UsingGroup
,
Group
:
i
nfo
.
UsingGroup
,
Other
:
other
,
Other
:
other
,
})
})
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayI
nfo
.
UserId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
i
nfo
.
UserId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayI
nfo
.
ChannelId
,
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
{
if
taskErr
!=
nil
{
return
return
}
}
relayI
nfo
.
ConsumeQuota
=
true
i
nfo
.
ConsumeQuota
=
true
// insert task
// insert task
task
:=
model
.
InitTask
(
platform
,
relayI
nfo
)
task
:=
model
.
InitTask
(
platform
,
i
nfo
)
task
.
TaskID
=
taskID
task
.
TaskID
=
taskID
task
.
Quota
=
quota
task
.
Quota
=
quota
task
.
Data
=
taskData
task
.
Data
=
taskData
task
.
Action
=
relayI
nfo
.
Action
task
.
Action
=
i
nfo
.
Action
err
=
task
.
Insert
()
err
=
task
.
Insert
()
if
err
!=
nil
{
if
err
!=
nil
{
taskErr
=
service
.
TaskErrorWrapper
(
err
,
"insert_task_failed"
,
http
.
StatusInternalServerError
)
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