adaptor.go 8.8 KB

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