adaptor.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package kling
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/samber/lo"
  8. "io"
  9. "net/http"
  10. "strings"
  11. "time"
  12. "github.com/gin-gonic/gin"
  13. "github.com/golang-jwt/jwt"
  14. "github.com/pkg/errors"
  15. "one-api/common"
  16. "one-api/dto"
  17. "one-api/relay/channel"
  18. relaycommon "one-api/relay/common"
  19. "one-api/service"
  20. )
  21. // ============================
  22. // Request / Response structures
  23. // ============================
  24. type SubmitReq struct {
  25. Prompt string `json:"prompt"`
  26. Model string `json:"model,omitempty"`
  27. Mode string `json:"mode,omitempty"`
  28. Image string `json:"image,omitempty"`
  29. Size string `json:"size,omitempty"`
  30. Duration int `json:"duration,omitempty"`
  31. Metadata map[string]interface{} `json:"metadata,omitempty"`
  32. }
  33. type requestPayload struct {
  34. Prompt string `json:"prompt,omitempty"`
  35. Image string `json:"image,omitempty"`
  36. Mode string `json:"mode,omitempty"`
  37. Duration string `json:"duration,omitempty"`
  38. AspectRatio string `json:"aspect_ratio,omitempty"`
  39. ModelName string `json:"model_name,omitempty"`
  40. CfgScale float64 `json:"cfg_scale,omitempty"`
  41. }
  42. type responsePayload struct {
  43. Code int `json:"code"`
  44. Message string `json:"message"`
  45. Data struct {
  46. TaskID string `json:"task_id"`
  47. } `json:"data"`
  48. }
  49. // ============================
  50. // Adaptor implementation
  51. // ============================
  52. type TaskAdaptor struct {
  53. ChannelType int
  54. accessKey string
  55. secretKey string
  56. baseURL string
  57. }
  58. func (a *TaskAdaptor) Init(info *relaycommon.TaskRelayInfo) {
  59. a.ChannelType = info.ChannelType
  60. a.baseURL = info.BaseUrl
  61. // apiKey format: "access_key|secret_key"
  62. keyParts := strings.Split(info.ApiKey, "|")
  63. if len(keyParts) == 2 {
  64. a.accessKey = strings.TrimSpace(keyParts[0])
  65. a.secretKey = strings.TrimSpace(keyParts[1])
  66. }
  67. }
  68. // ValidateRequestAndSetAction parses body, validates fields and sets default action.
  69. func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.TaskRelayInfo) (taskErr *dto.TaskError) {
  70. // Accept only POST /v1/video/generations as "generate" action.
  71. action := "generate"
  72. info.Action = action
  73. var req SubmitReq
  74. if err := common.UnmarshalBodyReusable(c, &req); err != nil {
  75. taskErr = service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest)
  76. return
  77. }
  78. if strings.TrimSpace(req.Prompt) == "" {
  79. taskErr = service.TaskErrorWrapperLocal(fmt.Errorf("prompt is required"), "invalid_request", http.StatusBadRequest)
  80. return
  81. }
  82. // Store into context for later usage
  83. c.Set("kling_request", req)
  84. return nil
  85. }
  86. // BuildRequestURL constructs the upstream URL.
  87. func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.TaskRelayInfo) (string, error) {
  88. path := lo.Ternary(info.Action == "generate", "/v1/videos/image2video", "/v1/videos/text2video")
  89. return fmt.Sprintf("%s%s", a.baseURL, path), nil
  90. }
  91. // BuildRequestHeader sets required headers.
  92. func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.TaskRelayInfo) error {
  93. token, err := a.createJWTToken()
  94. if err != nil {
  95. return fmt.Errorf("failed to create JWT token: %w", err)
  96. }
  97. req.Header.Set("Content-Type", "application/json")
  98. req.Header.Set("Accept", "application/json")
  99. req.Header.Set("Authorization", "Bearer "+token)
  100. req.Header.Set("User-Agent", "kling-sdk/1.0")
  101. return nil
  102. }
  103. // BuildRequestBody converts request into Kling specific format.
  104. func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.TaskRelayInfo) (io.Reader, error) {
  105. v, exists := c.Get("kling_request")
  106. if !exists {
  107. return nil, fmt.Errorf("request not found in context")
  108. }
  109. req := v.(SubmitReq)
  110. body, err := a.convertToRequestPayload(&req)
  111. if err != nil {
  112. return nil, err
  113. }
  114. data, err := json.Marshal(body)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return bytes.NewReader(data), nil
  119. }
  120. // DoRequest delegates to common helper.
  121. func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.TaskRelayInfo, requestBody io.Reader) (*http.Response, error) {
  122. if action := c.GetString("action"); action != "" {
  123. info.Action = action
  124. }
  125. return channel.DoTaskApiRequest(a, c, info, requestBody)
  126. }
  127. // DoResponse handles upstream response, returns taskID etc.
  128. func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.TaskRelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) {
  129. responseBody, err := io.ReadAll(resp.Body)
  130. if err != nil {
  131. taskErr = service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
  132. return
  133. }
  134. // Attempt Kling response parse first.
  135. var kResp responsePayload
  136. if err := json.Unmarshal(responseBody, &kResp); err == nil && kResp.Code == 0 {
  137. c.JSON(http.StatusOK, gin.H{"task_id": kResp.Data.TaskID})
  138. return kResp.Data.TaskID, responseBody, nil
  139. }
  140. // Fallback generic task response.
  141. var generic dto.TaskResponse[string]
  142. if err := json.Unmarshal(responseBody, &generic); err != nil {
  143. taskErr = service.TaskErrorWrapper(errors.Wrapf(err, "body: %s", responseBody), "unmarshal_response_body_failed", http.StatusInternalServerError)
  144. return
  145. }
  146. if !generic.IsSuccess() {
  147. taskErr = service.TaskErrorWrapper(fmt.Errorf(generic.Message), generic.Code, http.StatusInternalServerError)
  148. return
  149. }
  150. c.JSON(http.StatusOK, gin.H{"task_id": generic.Data})
  151. return generic.Data, responseBody, nil
  152. }
  153. // FetchTask fetch task status
  154. func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http.Response, error) {
  155. taskID, ok := body["task_id"].(string)
  156. if !ok {
  157. return nil, fmt.Errorf("invalid task_id")
  158. }
  159. action, ok := body["action"].(string)
  160. if !ok {
  161. return nil, fmt.Errorf("invalid action")
  162. }
  163. path := lo.Ternary(action == "generate", "/v1/videos/image2video", "/v1/videos/text2video")
  164. url := fmt.Sprintf("%s%s/%s", baseUrl, path, taskID)
  165. req, err := http.NewRequest(http.MethodGet, url, nil)
  166. if err != nil {
  167. return nil, err
  168. }
  169. token, err := a.createJWTTokenWithKey(key)
  170. if err != nil {
  171. token = key
  172. }
  173. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  174. defer cancel()
  175. req = req.WithContext(ctx)
  176. req.Header.Set("Accept", "application/json")
  177. req.Header.Set("Authorization", "Bearer "+token)
  178. req.Header.Set("User-Agent", "kling-sdk/1.0")
  179. return service.GetHttpClient().Do(req)
  180. }
  181. func (a *TaskAdaptor) GetModelList() []string {
  182. return []string{"kling-v1", "kling-v1-6", "kling-v2-master"}
  183. }
  184. func (a *TaskAdaptor) GetChannelName() string {
  185. return "kling"
  186. }
  187. // ============================
  188. // helpers
  189. // ============================
  190. func (a *TaskAdaptor) convertToRequestPayload(req *SubmitReq) (*requestPayload, error) {
  191. r := requestPayload{
  192. Prompt: req.Prompt,
  193. Image: req.Image,
  194. Mode: defaultString(req.Mode, "std"),
  195. Duration: fmt.Sprintf("%d", defaultInt(req.Duration, 5)),
  196. AspectRatio: a.getAspectRatio(req.Size),
  197. ModelName: req.Model,
  198. CfgScale: 0.5,
  199. }
  200. if r.ModelName == "" {
  201. r.ModelName = "kling-v1"
  202. }
  203. metadata := req.Metadata
  204. medaBytes, err := json.Marshal(metadata)
  205. if err != nil {
  206. return nil, errors.Wrap(err, "metadata marshal metadata failed")
  207. }
  208. err = json.Unmarshal(medaBytes, &r)
  209. if err != nil {
  210. return nil, errors.Wrap(err, "unmarshal metadata failed")
  211. }
  212. return &r, nil
  213. }
  214. func (a *TaskAdaptor) getAspectRatio(size string) string {
  215. switch size {
  216. case "1024x1024", "512x512":
  217. return "1:1"
  218. case "1280x720", "1920x1080":
  219. return "16:9"
  220. case "720x1280", "1080x1920":
  221. return "9:16"
  222. default:
  223. return "1:1"
  224. }
  225. }
  226. func defaultString(s, def string) string {
  227. if strings.TrimSpace(s) == "" {
  228. return def
  229. }
  230. return s
  231. }
  232. func defaultInt(v int, def int) int {
  233. if v == 0 {
  234. return def
  235. }
  236. return v
  237. }
  238. // ============================
  239. // JWT helpers
  240. // ============================
  241. func (a *TaskAdaptor) createJWTToken() (string, error) {
  242. return a.createJWTTokenWithKeys(a.accessKey, a.secretKey)
  243. }
  244. func (a *TaskAdaptor) createJWTTokenWithKey(apiKey string) (string, error) {
  245. parts := strings.Split(apiKey, "|")
  246. if len(parts) != 2 {
  247. return "", fmt.Errorf("invalid API key format, expected 'access_key,secret_key'")
  248. }
  249. return a.createJWTTokenWithKeys(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
  250. }
  251. func (a *TaskAdaptor) createJWTTokenWithKeys(accessKey, secretKey string) (string, error) {
  252. if accessKey == "" || secretKey == "" {
  253. return "", fmt.Errorf("access key and secret key are required")
  254. }
  255. now := time.Now().Unix()
  256. claims := jwt.MapClaims{
  257. "iss": accessKey,
  258. "exp": now + 1800, // 30 minutes
  259. "nbf": now - 5,
  260. }
  261. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  262. token.Header["typ"] = "JWT"
  263. return token.SignedString([]byte(secretKey))
  264. }
  265. // ParseResultUrl 提取视频任务结果的 url
  266. func (a *TaskAdaptor) ParseResultUrl(resp map[string]any) (string, error) {
  267. data, ok := resp["data"].(map[string]any)
  268. if !ok {
  269. return "", fmt.Errorf("data field not found or invalid")
  270. }
  271. taskResult, ok := data["task_result"].(map[string]any)
  272. if !ok {
  273. return "", fmt.Errorf("task_result field not found or invalid")
  274. }
  275. videos, ok := taskResult["videos"].([]interface{})
  276. if !ok || len(videos) == 0 {
  277. return "", fmt.Errorf("videos field not found or empty")
  278. }
  279. video, ok := videos[0].(map[string]interface{})
  280. if !ok {
  281. return "", fmt.Errorf("video item invalid")
  282. }
  283. url, ok := video["url"].(string)
  284. if !ok || url == "" {
  285. return "", fmt.Errorf("url field not found or invalid")
  286. }
  287. return url, nil
  288. }