adaptor.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package vertex
  2. import (
  3. "bytes"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "one-api/model"
  10. "regexp"
  11. "strings"
  12. "github.com/gin-gonic/gin"
  13. "one-api/constant"
  14. "one-api/dto"
  15. "one-api/relay/channel"
  16. vertexcore "one-api/relay/channel/vertex"
  17. relaycommon "one-api/relay/common"
  18. "one-api/service"
  19. )
  20. // ============================
  21. // Request / Response structures
  22. // ============================
  23. type requestPayload struct {
  24. Instances []map[string]any `json:"instances"`
  25. Parameters map[string]any `json:"parameters,omitempty"`
  26. }
  27. type submitResponse struct {
  28. Name string `json:"name"`
  29. }
  30. type operationVideo struct {
  31. MimeType string `json:"mimeType"`
  32. BytesBase64Encoded string `json:"bytesBase64Encoded"`
  33. Encoding string `json:"encoding"`
  34. }
  35. type operationResponse struct {
  36. Name string `json:"name"`
  37. Done bool `json:"done"`
  38. Response struct {
  39. Type string `json:"@type"`
  40. RaiMediaFilteredCount int `json:"raiMediaFilteredCount"`
  41. Videos []operationVideo `json:"videos"`
  42. BytesBase64Encoded string `json:"bytesBase64Encoded"`
  43. Encoding string `json:"encoding"`
  44. Video string `json:"video"`
  45. } `json:"response"`
  46. Error struct {
  47. Message string `json:"message"`
  48. } `json:"error"`
  49. }
  50. // ============================
  51. // Adaptor implementation
  52. // ============================
  53. type TaskAdaptor struct {
  54. ChannelType int
  55. apiKey string
  56. baseURL string
  57. }
  58. func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) {
  59. a.ChannelType = info.ChannelType
  60. a.baseURL = info.ChannelBaseUrl
  61. a.apiKey = info.ApiKey
  62. }
  63. // ValidateRequestAndSetAction parses body, validates fields and sets default action.
  64. func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) {
  65. // Use the standard validation method for TaskSubmitReq
  66. return relaycommon.ValidateBasicTaskRequest(c, info, constant.TaskActionTextGenerate)
  67. }
  68. // BuildRequestURL constructs the upstream URL.
  69. func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) {
  70. adc := &vertexcore.Credentials{}
  71. if err := json.Unmarshal([]byte(a.apiKey), adc); err != nil {
  72. return "", fmt.Errorf("failed to decode credentials: %w", err)
  73. }
  74. modelName := info.OriginModelName
  75. if modelName == "" {
  76. modelName = "veo-3.0-generate-001"
  77. }
  78. region := vertexcore.GetModelRegion(info.ApiVersion, modelName)
  79. if strings.TrimSpace(region) == "" {
  80. region = "global"
  81. }
  82. if region == "global" {
  83. return fmt.Sprintf(
  84. "https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:predictLongRunning",
  85. adc.ProjectID,
  86. modelName,
  87. ), nil
  88. }
  89. return fmt.Sprintf(
  90. "https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:predictLongRunning",
  91. region,
  92. adc.ProjectID,
  93. region,
  94. modelName,
  95. ), nil
  96. }
  97. // BuildRequestHeader sets required headers.
  98. func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
  99. req.Header.Set("Content-Type", "application/json")
  100. req.Header.Set("Accept", "application/json")
  101. adc := &vertexcore.Credentials{}
  102. if err := json.Unmarshal([]byte(a.apiKey), adc); err != nil {
  103. return fmt.Errorf("failed to decode credentials: %w", err)
  104. }
  105. token, err := vertexcore.AcquireAccessToken(*adc, "")
  106. if err != nil {
  107. return fmt.Errorf("failed to acquire access token: %w", err)
  108. }
  109. req.Header.Set("Authorization", "Bearer "+token)
  110. req.Header.Set("x-goog-user-project", adc.ProjectID)
  111. return nil
  112. }
  113. // BuildRequestBody converts request into Vertex specific format.
  114. func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) {
  115. v, ok := c.Get("task_request")
  116. if !ok {
  117. return nil, fmt.Errorf("request not found in context")
  118. }
  119. req := v.(relaycommon.TaskSubmitReq)
  120. body := requestPayload{
  121. Instances: []map[string]any{{"prompt": req.Prompt}},
  122. Parameters: map[string]any{},
  123. }
  124. if req.Metadata != nil {
  125. if v, ok := req.Metadata["storageUri"]; ok {
  126. body.Parameters["storageUri"] = v
  127. }
  128. if v, ok := req.Metadata["sampleCount"]; ok {
  129. body.Parameters["sampleCount"] = v
  130. }
  131. }
  132. if _, ok := body.Parameters["sampleCount"]; !ok {
  133. body.Parameters["sampleCount"] = 1
  134. }
  135. data, err := json.Marshal(body)
  136. if err != nil {
  137. return nil, err
  138. }
  139. return bytes.NewReader(data), nil
  140. }
  141. // DoRequest delegates to common helper.
  142. func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
  143. return channel.DoTaskApiRequest(a, c, info, requestBody)
  144. }
  145. // DoResponse handles upstream response, returns taskID etc.
  146. func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) {
  147. responseBody, err := io.ReadAll(resp.Body)
  148. if err != nil {
  149. return "", nil, service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
  150. }
  151. _ = resp.Body.Close()
  152. var s submitResponse
  153. if err := json.Unmarshal(responseBody, &s); err != nil {
  154. return "", nil, service.TaskErrorWrapper(err, "unmarshal_response_failed", http.StatusInternalServerError)
  155. }
  156. if strings.TrimSpace(s.Name) == "" {
  157. return "", nil, service.TaskErrorWrapper(fmt.Errorf("missing operation name"), "invalid_response", http.StatusInternalServerError)
  158. }
  159. localID := encodeLocalTaskID(s.Name)
  160. c.JSON(http.StatusOK, gin.H{"task_id": localID})
  161. return localID, responseBody, nil
  162. }
  163. func (a *TaskAdaptor) GetModelList() []string { return []string{"veo-3.0-generate-001"} }
  164. func (a *TaskAdaptor) GetChannelName() string { return "vertex" }
  165. // FetchTask fetch task status
  166. func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http.Response, error) {
  167. taskID, ok := body["task_id"].(string)
  168. if !ok {
  169. return nil, fmt.Errorf("invalid task_id")
  170. }
  171. upstreamName, err := decodeLocalTaskID(taskID)
  172. if err != nil {
  173. return nil, fmt.Errorf("decode task_id failed: %w", err)
  174. }
  175. region := extractRegionFromOperationName(upstreamName)
  176. if region == "" {
  177. region = "us-central1"
  178. }
  179. project := extractProjectFromOperationName(upstreamName)
  180. modelName := extractModelFromOperationName(upstreamName)
  181. if project == "" || modelName == "" {
  182. return nil, fmt.Errorf("cannot extract project/model from operation name")
  183. }
  184. var url string
  185. if region == "global" {
  186. url = fmt.Sprintf("https://aiplatform.googleapis.com/v1/projects/%s/locations/global/publishers/google/models/%s:fetchPredictOperation", project, modelName)
  187. } else {
  188. url = fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/google/models/%s:fetchPredictOperation", region, project, region, modelName)
  189. }
  190. payload := map[string]string{"operationName": upstreamName}
  191. data, err := json.Marshal(payload)
  192. if err != nil {
  193. return nil, err
  194. }
  195. adc := &vertexcore.Credentials{}
  196. if err := json.Unmarshal([]byte(key), adc); err != nil {
  197. return nil, fmt.Errorf("failed to decode credentials: %w", err)
  198. }
  199. token, err := vertexcore.AcquireAccessToken(*adc, "")
  200. if err != nil {
  201. return nil, fmt.Errorf("failed to acquire access token: %w", err)
  202. }
  203. req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
  204. if err != nil {
  205. return nil, err
  206. }
  207. req.Header.Set("Content-Type", "application/json")
  208. req.Header.Set("Accept", "application/json")
  209. req.Header.Set("Authorization", "Bearer "+token)
  210. req.Header.Set("x-goog-user-project", adc.ProjectID)
  211. return service.GetHttpClient().Do(req)
  212. }
  213. func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) {
  214. var op operationResponse
  215. if err := json.Unmarshal(respBody, &op); err != nil {
  216. return nil, fmt.Errorf("unmarshal operation response failed: %w", err)
  217. }
  218. ti := &relaycommon.TaskInfo{}
  219. if op.Error.Message != "" {
  220. ti.Status = model.TaskStatusFailure
  221. ti.Reason = op.Error.Message
  222. ti.Progress = "100%"
  223. return ti, nil
  224. }
  225. if !op.Done {
  226. ti.Status = model.TaskStatusInProgress
  227. ti.Progress = "50%"
  228. return ti, nil
  229. }
  230. ti.Status = model.TaskStatusSuccess
  231. ti.Progress = "100%"
  232. if len(op.Response.Videos) > 0 {
  233. v0 := op.Response.Videos[0]
  234. if v0.BytesBase64Encoded != "" {
  235. mime := strings.TrimSpace(v0.MimeType)
  236. if mime == "" {
  237. enc := strings.TrimSpace(v0.Encoding)
  238. if enc == "" {
  239. enc = "mp4"
  240. }
  241. if strings.Contains(enc, "/") {
  242. mime = enc
  243. } else {
  244. mime = "video/" + enc
  245. }
  246. }
  247. ti.Url = "data:" + mime + ";base64," + v0.BytesBase64Encoded
  248. return ti, nil
  249. }
  250. }
  251. if op.Response.BytesBase64Encoded != "" {
  252. enc := strings.TrimSpace(op.Response.Encoding)
  253. if enc == "" {
  254. enc = "mp4"
  255. }
  256. mime := enc
  257. if !strings.Contains(enc, "/") {
  258. mime = "video/" + enc
  259. }
  260. ti.Url = "data:" + mime + ";base64," + op.Response.BytesBase64Encoded
  261. return ti, nil
  262. }
  263. if op.Response.Video != "" { // some variants use `video` as base64
  264. enc := strings.TrimSpace(op.Response.Encoding)
  265. if enc == "" {
  266. enc = "mp4"
  267. }
  268. mime := enc
  269. if !strings.Contains(enc, "/") {
  270. mime = "video/" + enc
  271. }
  272. ti.Url = "data:" + mime + ";base64," + op.Response.Video
  273. return ti, nil
  274. }
  275. return ti, nil
  276. }
  277. // ============================
  278. // helpers
  279. // ============================
  280. func encodeLocalTaskID(name string) string {
  281. return base64.RawURLEncoding.EncodeToString([]byte(name))
  282. }
  283. func decodeLocalTaskID(local string) (string, error) {
  284. b, err := base64.RawURLEncoding.DecodeString(local)
  285. if err != nil {
  286. return "", err
  287. }
  288. return string(b), nil
  289. }
  290. var regionRe = regexp.MustCompile(`locations/([a-z0-9-]+)/`)
  291. func extractRegionFromOperationName(name string) string {
  292. m := regionRe.FindStringSubmatch(name)
  293. if len(m) == 2 {
  294. return m[1]
  295. }
  296. return ""
  297. }
  298. var modelRe = regexp.MustCompile(`models/([^/]+)/operations/`)
  299. func extractModelFromOperationName(name string) string {
  300. m := modelRe.FindStringSubmatch(name)
  301. if len(m) == 2 {
  302. return m[1]
  303. }
  304. idx := strings.Index(name, "models/")
  305. if idx >= 0 {
  306. s := name[idx+len("models/"):]
  307. if p := strings.Index(s, "/operations/"); p > 0 {
  308. return s[:p]
  309. }
  310. }
  311. return ""
  312. }
  313. var projectRe = regexp.MustCompile(`projects/([^/]+)/locations/`)
  314. func extractProjectFromOperationName(name string) string {
  315. m := projectRe.FindStringSubmatch(name)
  316. if len(m) == 2 {
  317. return m[1]
  318. }
  319. return ""
  320. }