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
523ee96b
authored
Sep 13, 2025
by
Calcium-Ion
Committed by
GitHub
Sep 13, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1794 from seefs001/fix/veo3
fix veo3 adapter
parents
9e17df3f
30763deb
Hide 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 @
523ee96b
...
...
@@ -7,12 +7,12 @@ import (
"fmt"
"io"
"net/http"
"one-api/model"
"regexp"
"strings"
"github.com/gin-gonic/gin"
"one-api/common"
"one-api/constant"
"one-api/dto"
"one-api/relay/channel"
...
...
@@ -21,6 +21,10 @@ import (
"one-api/service"
)
// ============================
// Request / Response structures
// ============================
type
requestPayload
struct
{
Instances
[]
map
[
string
]
any
`json:"instances"`
Parameters
map
[
string
]
any
`json:"parameters,omitempty"`
...
...
@@ -52,33 +56,35 @@ type operationResponse struct {
}
`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
)
{
info
.
Action
=
constant
.
TaskActionTextGenerate
func
(
a
*
TaskAdaptor
)
Init
(
info
*
relaycommon
.
RelayInfo
)
{
a
.
ChannelType
=
info
.
ChannelType
a
.
baseURL
=
info
.
ChannelBaseUrl
a
.
apiKey
=
info
.
ApiKey
}
req
:=
relaycommon
.
TaskSubmitReq
{}
if
err
:=
common
.
UnmarshalBodyReusable
(
c
,
&
req
);
err
!=
nil
{
return
service
.
TaskErrorWrapperLocal
(
err
,
"invalid_request"
,
http
.
StatusBadRequest
)
}
if
strings
.
TrimSpace
(
req
.
Prompt
)
==
""
{
return
service
.
TaskErrorWrapperLocal
(
fmt
.
Errorf
(
"prompt is required"
),
"invalid_request"
,
http
.
StatusBadRequest
)
}
c
.
Set
(
"task_request"
,
req
)
return
nil
// ValidateRequestAndSetAction parses body, validates fields and sets default action.
func
(
a
*
TaskAdaptor
)
ValidateRequestAndSetAction
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
)
(
taskErr
*
dto
.
TaskError
)
{
// Use the standard validation method for TaskSubmitReq
return
relaycommon
.
ValidateBasicTaskRequest
(
c
,
info
,
constant
.
TaskActionTextGenerate
)
}
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
{}
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
)
}
modelName
:=
info
.
OriginModelName
if
v
,
ok
:=
getRequestModelFromContext
(
info
);
ok
{
modelName
=
v
}
if
modelName
==
""
{
modelName
=
"veo-3.0-generate-001"
}
...
...
@@ -103,16 +109,17 @@ func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string,
),
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
(
"Accept"
,
"application/json"
)
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
)
}
token
,
err
:=
vertexcore
.
AcquireAccessToken
(
*
adc
,
info
.
ChannelSetting
.
Proxy
)
token
,
err
:=
vertexcore
.
AcquireAccessToken
(
*
adc
,
""
)
if
err
!=
nil
{
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
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"
)
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"request not found in context"
)
...
...
@@ -151,11 +159,13 @@ func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, _ *relaycommon.TaskRelayI
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
)
}
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
)
if
err
!=
nil
{
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
func
(
a
*
TaskAdaptor
)
GetModelList
()
[]
string
{
return
[]
string
{
"veo-3.0-generate-001"
}
}
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
)
{
taskID
,
ok
:=
body
[
"task_id"
]
.
(
string
)
if
!
ok
{
...
...
@@ -191,15 +202,15 @@ func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http
region
=
"us-central1"
}
project
:=
extractProjectFromOperationName
(
upstreamName
)
model
:=
extractModelFromOperationName
(
upstreamName
)
if
project
==
""
||
model
==
""
{
model
Name
:=
extractModelFromOperationName
(
upstreamName
)
if
project
==
""
||
model
Name
==
""
{
return
nil
,
fmt
.
Errorf
(
"cannot extract project/model from operation name"
)
}
var
url
string
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
{
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
}
data
,
err
:=
json
.
Marshal
(
payload
)
...
...
@@ -232,17 +243,17 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
}
ti
:=
&
relaycommon
.
TaskInfo
{}
if
op
.
Error
.
Message
!=
""
{
ti
.
Status
=
"FAILURE"
ti
.
Status
=
model
.
TaskStatusFailure
ti
.
Reason
=
op
.
Error
.
Message
ti
.
Progress
=
"100%"
return
ti
,
nil
}
if
!
op
.
Done
{
ti
.
Status
=
"IN_PROGRESS"
ti
.
Status
=
model
.
TaskStatusInProgress
ti
.
Progress
=
"50%"
return
ti
,
nil
}
ti
.
Status
=
"SUCCESS"
ti
.
Status
=
model
.
TaskStatusSuccess
ti
.
Progress
=
"100%"
if
len
(
op
.
Response
.
Videos
)
>
0
{
v0
:=
op
.
Response
.
Videos
[
0
]
...
...
@@ -290,9 +301,9 @@ func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, e
return
ti
,
nil
}
func
getRequestModelFromContext
(
info
*
relaycommon
.
TaskRelayInfo
)
(
string
,
bool
)
{
return
info
.
OriginModelName
,
info
.
OriginModelName
!=
""
}
// ============================
// helpers
// ============================
func
encodeLocalTaskID
(
name
string
)
string
{
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