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
30763deb
authored
Sep 13, 2025
by
Seefs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix veo3 adapter
parent
9e17df3f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
35 deletions
+46
-35
relay/channel/task/vertex/adaptor.go
+46
-35
No files found.
relay/channel/task/vertex/adaptor.go
View file @
30763deb
...
@@ -7,12 +7,12 @@ import (
...
@@ -7,12 +7,12 @@ import (
"fmt"
"fmt"
"io"
"io"
"net/http"
"net/http"
"one-api/model"
"regexp"
"regexp"
"strings"
"strings"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
"one-api/common"
"one-api/constant"
"one-api/constant"
"one-api/dto"
"one-api/dto"
"one-api/relay/channel"
"one-api/relay/channel"
...
@@ -21,6 +21,10 @@ import (
...
@@ -21,6 +21,10 @@ import (
"one-api/service"
"one-api/service"
)
)
// ============================
// Request / Response structures
// ============================
type
requestPayload
struct
{
type
requestPayload
struct
{
Instances
[]
map
[
string
]
any
`json:"instances"`
Instances
[]
map
[
string
]
any
`json:"instances"`
Parameters
map
[
string
]
any
`json:"parameters,omitempty"`
Parameters
map
[
string
]
any
`json:"parameters,omitempty"`
...
@@ -52,33 +56,35 @@ type operationResponse struct {
...
@@ -52,33 +56,35 @@ type operationResponse struct {
}
`json:"error"`
}
`json:"error"`
}
}
type
TaskAdaptor
struct
{}
// ============================
// Adaptor implementation
// ============================
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
TaskRelayInfo
)
{}
type
TaskAdaptor
struct
{
ChannelType
int
apiKey
string
baseURL
string
}
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
TaskRelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
info
.
Action
=
constant
.
TaskActionTextGenerate
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
a
.
apiKey
=
info
.
ApiKey
}
req
:=
relaycommon
.
TaskSubmitReq
{}
// ValidateRequestAndSetAction parses body, validates fields and sets default action.
if
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
req
);
err
!=
nil
{
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
return
service
.
TaskErrorWrapperLocal
(
err
,
"invalid_request"
,
http
.
StatusBadRequest
)
// Use the standard validation method for TaskSubmitReq
}
return
relaycommon
.
ValidateBasicTaskRequest
(
c
,
info
,
constant
.
TaskActionTextGenerate
)
if
strings
.
TrimSpace
(
req
.
Prompt
)
==
""
{
return
service
.
TaskErrorWrapperLocal
(
fmt
.
Errorf
(
"prompt is required"
),
"invalid_request"
,
http
.
StatusBadRequest
)
}
c
.
Set
(
"task_request"
,
req
)
return
nil
}
}
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
TaskRelayInfo
)
(
string
,
error
)
{
// BuildRequestURL constructs the upstream URL.
func
(
a
*
TaskAdaptor
)
BuildRequestURL
(
info
*
relaycommon
.
RelayInfo
)
(
string
,
error
)
{
adc
:=
&
vertexcore
.
Credentials
{}
adc
:=
&
vertexcore
.
Credentials
{}
if
err
:=
json
.
Unmarshal
([]
byte
(
info
.
A
piKey
),
adc
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
([]
byte
(
a
.
a
piKey
),
adc
);
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to decode credentials: %w"
,
err
)
return
""
,
fmt
.
Errorf
(
"failed to decode credentials: %w"
,
err
)
}
}
modelName
:=
info
.
OriginModelName
modelName
:=
info
.
OriginModelName
if
v
,
ok
:=
getRequestModelFromContext
(
info
);
ok
{
modelName
=
v
}
if
modelName
==
""
{
if
modelName
==
""
{
modelName
=
"veo-3.0-generate-001"
modelName
=
"veo-3.0-generate-001"
}
}
...
@@ -103,16 +109,17 @@ func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string,
...
@@ -103,16 +109,17 @@ func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string,
),
nil
),
nil
}
}
func
(
a
*
TaskAdaptor
)
BuildRequestHeader
(
c
*
gin
.
Context
,
req
*
http
.
Request
,
info
*
relaycommon
.
TaskRelayInfo
)
error
{
// BuildRequestHeader sets required headers.
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"
)
adc
:=
&
vertexcore
.
Credentials
{}
adc
:=
&
vertexcore
.
Credentials
{}
if
err
:=
json
.
Unmarshal
([]
byte
(
info
.
A
piKey
),
adc
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
([]
byte
(
a
.
a
piKey
),
adc
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to decode credentials: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to decode credentials: %w"
,
err
)
}
}
token
,
err
:=
vertexcore
.
AcquireAccessToken
(
*
adc
,
info
.
ChannelSetting
.
Proxy
)
token
,
err
:=
vertexcore
.
AcquireAccessToken
(
*
adc
,
""
)
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to acquire access token: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to acquire access token: %w"
,
err
)
}
}
...
@@ -121,7 +128,8 @@ func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info
...
@@ -121,7 +128,8 @@ func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info
return
nil
return
nil
}
}
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
_
*
relaycommon
.
TaskRelayInfo
)
(
io
.
Reader
,
error
)
{
// BuildRequestBody converts request into Vertex specific format.
func
(
a
*
TaskAdaptor
)
BuildRequestBody
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
io
.
Reader
,
error
)
{
v
,
ok
:=
c
.
Get
(
"task_request"
)
v
,
ok
:=
c
.
Get
(
"task_request"
)
if
!
ok
{
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
@@ -151,11 +159,13 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayI
...
@@ -151,11 +159,13 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayI
return
bytes
.
NewReader
(
data
),
nil
return
bytes
.
NewReader
(
data
),
nil
}
}
func
(
a
*
TaskAdaptor
)
DoRequest
(
c
*
gin
.
Context
,
info
*
relaycommon
.
TaskRelayInfo
,
requestBody
io
.
Reader
)
(
*
http
.
Response
,
error
)
{
// 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
)
return
channel
.
DoTaskApiRequest
(
a
,
c
,
info
,
requestBody
)
}
}
func
(
a
*
TaskAdaptor
)
DoResponse
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
_
*
relaycommon
.
TaskRelayInfo
)
(
taskID
string
,
taskData
[]
byte
,
taskErr
*
dto
.
TaskError
)
{
// DoResponse handles upstream response, returns taskID etc.
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
{
return
""
,
nil
,
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
return
""
,
nil
,
service
.
TaskErrorWrapper
(
err
,
"read_response_body_failed"
,
http
.
StatusInternalServerError
)
...
@@ -177,6 +187,7 @@ func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, _ *relayco
...
@@ -177,6 +187,7 @@ func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, _ *relayco
func
(
a
*
TaskAdaptor
)
GetModelList
()
[]
string
{
return
[]
string
{
"veo-3.0-generate-001"
}
}
func
(
a
*
TaskAdaptor
)
GetModelList
()
[]
string
{
return
[]
string
{
"veo-3.0-generate-001"
}
}
func
(
a
*
TaskAdaptor
)
GetChannelName
()
string
{
return
"vertex"
}
func
(
a
*
TaskAdaptor
)
GetChannelName
()
string
{
return
"vertex"
}
// FetchTask fetch task status
func
(
a
*
TaskAdaptor
)
FetchTask
(
baseUrl
,
key
string
,
body
map
[
string
]
any
)
(
*
http
.
Response
,
error
)
{
func
(
a
*
TaskAdaptor
)
FetchTask
(
baseUrl
,
key
string
,
body
map
[
string
]
any
)
(
*
http
.
Response
,
error
)
{
taskID
,
ok
:=
body
[
"task_id"
]
.
(
string
)
taskID
,
ok
:=
body
[
"task_id"
]
.
(
string
)
if
!
ok
{
if
!
ok
{
...
@@ -191,15 +202,15 @@ func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http
...
@@ -191,15 +202,15 @@ func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http
region
=
"us-central1"
region
=
"us-central1"
}
}
project
:=
extractProjectFromOperationName
(
upstreamName
)
project
:=
extractProjectFromOperationName
(
upstreamName
)
model
:=
extractModelFromOperationName
(
upstreamName
)
model
Name
:=
extractModelFromOperationName
(
upstreamName
)
if
project
==
""
||
model
==
""
{
if
project
==
""
||
model
Name
==
""
{
return
nil
,
fmt
.
Errorf
(
"cannot extract project/model from operation name"
)
return
nil
,
fmt
.
Errorf
(
"cannot extract project/model from operation name"
)
}
}
var
url
string
var
url
string
if
region
==
"global"
{
if
region
==
"global"
{
url
=
fmt
.
Sprintf
(
"https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:fetchPredictOperation"
,
project
,
model
)
url
=
fmt
.
Sprintf
(
"https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:fetchPredictOperation"
,
project
,
model
Name
)
}
else
{
}
else
{
url
=
fmt
.
Sprintf
(
"https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:fetchPredictOperation"
,
region
,
project
,
region
,
model
)
url
=
fmt
.
Sprintf
(
"https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:fetchPredictOperation"
,
region
,
project
,
region
,
model
Name
)
}
}
payload
:=
map
[
string
]
string
{
"operationName"
:
upstreamName
}
payload
:=
map
[
string
]
string
{
"operationName"
:
upstreamName
}
data
,
err
:=
json
.
Marshal
(
payload
)
data
,
err
:=
json
.
Marshal
(
payload
)
...
@@ -232,17 +243,17 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
...
@@ -232,17 +243,17 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
}
}
ti
:=
&
relaycommon
.
TaskInfo
{}
ti
:=
&
relaycommon
.
TaskInfo
{}
if
op
.
Error
.
Message
!=
""
{
if
op
.
Error
.
Message
!=
""
{
ti
.
Status
=
"FAILURE"
ti
.
Status
=
model
.
TaskStatusFailure
ti
.
Reason
=
op
.
Error
.
Message
ti
.
Reason
=
op
.
Error
.
Message
ti
.
Progress
=
"100%"
ti
.
Progress
=
"100%"
return
ti
,
nil
return
ti
,
nil
}
}
if
!
op
.
Done
{
if
!
op
.
Done
{
ti
.
Status
=
"IN_PROGRESS"
ti
.
Status
=
model
.
TaskStatusInProgress
ti
.
Progress
=
"50%"
ti
.
Progress
=
"50%"
return
ti
,
nil
return
ti
,
nil
}
}
ti
.
Status
=
"SUCCESS"
ti
.
Status
=
model
.
TaskStatusSuccess
ti
.
Progress
=
"100%"
ti
.
Progress
=
"100%"
if
len
(
op
.
Response
.
Videos
)
>
0
{
if
len
(
op
.
Response
.
Videos
)
>
0
{
v0
:=
op
.
Response
.
Videos
[
0
]
v0
:=
op
.
Response
.
Videos
[
0
]
...
@@ -290,9 +301,9 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
...
@@ -290,9 +301,9 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
return
ti
,
nil
return
ti
,
nil
}
}
func
getRequestModelFromContext
(
info
*
relaycommon
.
TaskRelayInfo
)
(
string
,
bool
)
{
// ============================
return
info
.
OriginModelName
,
info
.
OriginModelName
!=
""
// helpers
}
// ============================
func
encodeLocalTaskID
(
name
string
)
string
{
func
encodeLocalTaskID
(
name
string
)
string
{
return
base64
.
RawURLEncoding
.
EncodeToString
([]
byte
(
name
))
return
base64
.
RawURLEncoding
.
EncodeToString
([]
byte
(
name
))
...
...
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