adaptor.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. package kling
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "one-api/model"
  9. "strings"
  10. "time"
  11. "github.com/samber/lo"
  12. "github.com/gin-gonic/gin"
  13. "github.com/golang-jwt/jwt"
  14. "github.com/pkg/errors"
  15. "one-api/common"
  16. "one-api/constant"
  17. "one-api/dto"
  18. "one-api/relay/channel"
  19. relaycommon "one-api/relay/common"
  20. "one-api/service"
  21. )
  22. // ============================
  23. // Request / Response structures
  24. // ============================
  25. type SubmitReq struct {
  26. Prompt string `json:"prompt"`
  27. Model string `json:"model,omitempty"`
  28. Mode string `json:"mode,omitempty"`
  29. Image string `json:"image,omitempty"`
  30. Size string `json:"size,omitempty"`
  31. Duration int `json:"duration,omitempty"`
  32. Metadata map[string]interface{} `json:"metadata,omitempty"`
  33. }
  34. type TrajectoryPoint struct {
  35. X int `json:"x"`
  36. Y int `json:"y"`
  37. }
  38. type DynamicMask struct {
  39. Mask string `json:"mask,omitempty"`
  40. Trajectories []TrajectoryPoint `json:"trajectories,omitempty"`
  41. }
  42. type CameraConfig struct {
  43. Horizontal float64 `json:"horizontal,omitempty"`
  44. Vertical float64 `json:"vertical,omitempty"`
  45. Pan float64 `json:"pan,omitempty"`
  46. Tilt float64 `json:"tilt,omitempty"`
  47. Roll float64 `json:"roll,omitempty"`
  48. Zoom float64 `json:"zoom,omitempty"`
  49. }
  50. type CameraControl struct {
  51. Type string `json:"type,omitempty"`
  52. Config *CameraConfig `json:"config,omitempty"`
  53. }
  54. type requestPayload struct {
  55. Prompt string `json:"prompt,omitempty"`
  56. Image string `json:"image,omitempty"`
  57. ImageTail string `json:"image_tail,omitempty"`
  58. NegativePrompt string `json:"negative_prompt,omitempty"`
  59. Mode string `json:"mode,omitempty"`
  60. Duration string `json:"duration,omitempty"`
  61. AspectRatio string `json:"aspect_ratio,omitempty"`
  62. ModelName string `json:"model_name,omitempty"`
  63. Model string `json:"model,omitempty"` // Compatible with upstreams that only recognize "model"
  64. CfgScale float64 `json:"cfg_scale,omitempty"`
  65. StaticMask string `json:"static_mask,omitempty"`
  66. DynamicMasks []DynamicMask `json:"dynamic_masks,omitempty"`
  67. CameraControl *CameraControl `json:"camera_control,omitempty"`
  68. CallbackUrl string `json:"callback_url,omitempty"`
  69. ExternalTaskId string `json:"external_task_id,omitempty"`
  70. }
  71. type responsePayload struct {
  72. Code int `json:"code"`
  73. Message string `json:"message"`
  74. TaskId string `json:"task_id"`
  75. RequestId string `json:"request_id"`
  76. Data struct {
  77. TaskId string `json:"task_id"`
  78. TaskStatus string `json:"task_status"`
  79. TaskStatusMsg string `json:"task_status_msg"`
  80. TaskResult struct {
  81. Videos []struct {
  82. Id string `json:"id"`
  83. Url string `json:"url"`
  84. Duration string `json:"duration"`
  85. } `json:"videos"`
  86. } `json:"task_result"`
  87. CreatedAt int64 `json:"created_at"`
  88. UpdatedAt int64 `json:"updated_at"`
  89. } `json:"data"`
  90. }
  91. // ============================
  92. // Adaptor implementation
  93. // ============================
  94. type TaskAdaptor struct {
  95. ChannelType int
  96. apiKey string
  97. baseURL string
  98. }
  99. func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) {
  100. a.ChannelType = info.ChannelType
  101. a.baseURL = info.ChannelBaseUrl
  102. a.apiKey = info.ApiKey
  103. // apiKey format: "access_key|secret_key"
  104. }
  105. // ValidateRequestAndSetAction parses body, validates fields and sets default action.
  106. func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) (taskErr *dto.TaskError) {
  107. // Accept only POST /v1/video/generations as "generate" action.
  108. action := constant.TaskActionGenerate
  109. info.Action = action
  110. var req SubmitReq
  111. if err := common.UnmarshalBodyReusable(c, &req); err != nil {
  112. taskErr = service.TaskErrorWrapperLocal(err, "invalid_request", http.StatusBadRequest)
  113. return
  114. }
  115. if strings.TrimSpace(req.Prompt) == "" {
  116. taskErr = service.TaskErrorWrapperLocal(fmt.Errorf("prompt is required"), "invalid_request", http.StatusBadRequest)
  117. return
  118. }
  119. // Store into context for later usage
  120. c.Set("task_request", req)
  121. return nil
  122. }
  123. // BuildRequestURL constructs the upstream URL.
  124. func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) {
  125. path := lo.Ternary(info.Action == constant.TaskActionGenerate, "/v1/videos/image2video", "/v1/videos/text2video")
  126. return fmt.Sprintf("%s%s", a.baseURL, path), nil
  127. }
  128. // BuildRequestHeader sets required headers.
  129. func (a *TaskAdaptor) BuildRequestHeader(c *gin.Context, req *http.Request, info *relaycommon.RelayInfo) error {
  130. token, err := a.createJWTToken()
  131. if err != nil {
  132. return fmt.Errorf("failed to create JWT token: %w", err)
  133. }
  134. req.Header.Set("Content-Type", "application/json")
  135. req.Header.Set("Accept", "application/json")
  136. req.Header.Set("Authorization", "Bearer "+token)
  137. req.Header.Set("User-Agent", "kling-sdk/1.0")
  138. return nil
  139. }
  140. // BuildRequestBody converts request into Kling specific format.
  141. func (a *TaskAdaptor) BuildRequestBody(c *gin.Context, info *relaycommon.RelayInfo) (io.Reader, error) {
  142. v, exists := c.Get("task_request")
  143. if !exists {
  144. return nil, fmt.Errorf("request not found in context")
  145. }
  146. req := v.(SubmitReq)
  147. body, err := a.convertToRequestPayload(&req)
  148. if err != nil {
  149. return nil, err
  150. }
  151. if body.Image == "" && body.ImageTail == "" {
  152. c.Set("action", constant.TaskActionTextGenerate)
  153. }
  154. data, err := json.Marshal(body)
  155. if err != nil {
  156. return nil, err
  157. }
  158. return bytes.NewReader(data), nil
  159. }
  160. // DoRequest delegates to common helper.
  161. func (a *TaskAdaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (*http.Response, error) {
  162. if action := c.GetString("action"); action != "" {
  163. info.Action = action
  164. }
  165. return channel.DoTaskApiRequest(a, c, info, requestBody)
  166. }
  167. // DoResponse handles upstream response, returns taskID etc.
  168. func (a *TaskAdaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (taskID string, taskData []byte, taskErr *dto.TaskError) {
  169. responseBody, err := io.ReadAll(resp.Body)
  170. if err != nil {
  171. taskErr = service.TaskErrorWrapper(err, "read_response_body_failed", http.StatusInternalServerError)
  172. return
  173. }
  174. var kResp responsePayload
  175. err = json.Unmarshal(responseBody, &kResp)
  176. if err != nil {
  177. taskErr = service.TaskErrorWrapper(err, "unmarshal_response_failed", http.StatusInternalServerError)
  178. return
  179. }
  180. if kResp.Code != 0 {
  181. taskErr = service.TaskErrorWrapperLocal(fmt.Errorf(kResp.Message), "task_failed", http.StatusBadRequest)
  182. return
  183. }
  184. kResp.TaskId = kResp.Data.TaskId
  185. c.JSON(http.StatusOK, kResp)
  186. return kResp.Data.TaskId, responseBody, nil
  187. }
  188. // FetchTask fetch task status
  189. func (a *TaskAdaptor) FetchTask(baseUrl, key string, body map[string]any) (*http.Response, error) {
  190. taskID, ok := body["task_id"].(string)
  191. if !ok {
  192. return nil, fmt.Errorf("invalid task_id")
  193. }
  194. action, ok := body["action"].(string)
  195. if !ok {
  196. return nil, fmt.Errorf("invalid action")
  197. }
  198. path := lo.Ternary(action == constant.TaskActionGenerate, "/v1/videos/image2video", "/v1/videos/text2video")
  199. url := fmt.Sprintf("%s%s/%s", baseUrl, path, taskID)
  200. req, err := http.NewRequest(http.MethodGet, url, nil)
  201. if err != nil {
  202. return nil, err
  203. }
  204. token, err := a.createJWTTokenWithKey(key)
  205. if err != nil {
  206. token = key
  207. }
  208. req.Header.Set("Accept", "application/json")
  209. req.Header.Set("Authorization", "Bearer "+token)
  210. req.Header.Set("User-Agent", "kling-sdk/1.0")
  211. return service.GetHttpClient().Do(req)
  212. }
  213. func (a *TaskAdaptor) GetModelList() []string {
  214. return []string{"kling-v1", "kling-v1-6", "kling-v2-master"}
  215. }
  216. func (a *TaskAdaptor) GetChannelName() string {
  217. return "kling"
  218. }
  219. // ============================
  220. // helpers
  221. // ============================
  222. func (a *TaskAdaptor) convertToRequestPayload(req *SubmitReq) (*requestPayload, error) {
  223. r := requestPayload{
  224. Prompt: req.Prompt,
  225. Image: req.Image,
  226. Mode: defaultString(req.Mode, "std"),
  227. Duration: fmt.Sprintf("%d", defaultInt(req.Duration, 5)),
  228. AspectRatio: a.getAspectRatio(req.Size),
  229. ModelName: req.Model,
  230. Model: req.Model, // Keep consistent with model_name, double writing improves compatibility
  231. CfgScale: 0.5,
  232. StaticMask: "",
  233. DynamicMasks: []DynamicMask{},
  234. CameraControl: nil,
  235. CallbackUrl: "",
  236. ExternalTaskId: "",
  237. }
  238. if r.ModelName == "" {
  239. r.ModelName = "kling-v1"
  240. }
  241. metadata := req.Metadata
  242. medaBytes, err := json.Marshal(metadata)
  243. if err != nil {
  244. return nil, errors.Wrap(err, "metadata marshal metadata failed")
  245. }
  246. err = json.Unmarshal(medaBytes, &r)
  247. if err != nil {
  248. return nil, errors.Wrap(err, "unmarshal metadata failed")
  249. }
  250. return &r, nil
  251. }
  252. func (a *TaskAdaptor) getAspectRatio(size string) string {
  253. switch size {
  254. case "1024x1024", "512x512":
  255. return "1:1"
  256. case "1280x720", "1920x1080":
  257. return "16:9"
  258. case "720x1280", "1080x1920":
  259. return "9:16"
  260. default:
  261. return "1:1"
  262. }
  263. }
  264. func defaultString(s, def string) string {
  265. if strings.TrimSpace(s) == "" {
  266. return def
  267. }
  268. return s
  269. }
  270. func defaultInt(v int, def int) int {
  271. if v == 0 {
  272. return def
  273. }
  274. return v
  275. }
  276. // ============================
  277. // JWT helpers
  278. // ============================
  279. func (a *TaskAdaptor) createJWTToken() (string, error) {
  280. return a.createJWTTokenWithKey(a.apiKey)
  281. }
  282. //func (a *TaskAdaptor) createJWTTokenWithKey(apiKey string) (string, error) {
  283. // parts := strings.Split(apiKey, "|")
  284. // if len(parts) != 2 {
  285. // return "", fmt.Errorf("invalid API key format, expected 'access_key,secret_key'")
  286. // }
  287. // return a.createJWTTokenWithKey(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
  288. //}
  289. func (a *TaskAdaptor) createJWTTokenWithKey(apiKey string) (string, error) {
  290. keyParts := strings.Split(apiKey, "|")
  291. accessKey := strings.TrimSpace(keyParts[0])
  292. if len(keyParts) == 1 {
  293. return accessKey, nil
  294. }
  295. secretKey := strings.TrimSpace(keyParts[1])
  296. now := time.Now().Unix()
  297. claims := jwt.MapClaims{
  298. "iss": accessKey,
  299. "exp": now + 1800, // 30 minutes
  300. "nbf": now - 5,
  301. }
  302. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  303. token.Header["typ"] = "JWT"
  304. return token.SignedString([]byte(secretKey))
  305. }
  306. func (a *TaskAdaptor) ParseTaskResult(respBody []byte) (*relaycommon.TaskInfo, error) {
  307. taskInfo := &relaycommon.TaskInfo{}
  308. resPayload := responsePayload{}
  309. err := json.Unmarshal(respBody, &resPayload)
  310. if err != nil {
  311. return nil, errors.Wrap(err, "failed to unmarshal response body")
  312. }
  313. taskInfo.Code = resPayload.Code
  314. taskInfo.TaskID = resPayload.Data.TaskId
  315. taskInfo.Reason = resPayload.Message
  316. //任务状态,枚举值:submitted(已提交)、processing(处理中)、succeed(成功)、failed(失败)
  317. status := resPayload.Data.TaskStatus
  318. switch status {
  319. case "submitted":
  320. taskInfo.Status = model.TaskStatusSubmitted
  321. case "processing":
  322. taskInfo.Status = model.TaskStatusInProgress
  323. case "succeed":
  324. taskInfo.Status = model.TaskStatusSuccess
  325. case "failed":
  326. taskInfo.Status = model.TaskStatusFailure
  327. default:
  328. return nil, fmt.Errorf("unknown task status: %s", status)
  329. }
  330. if videos := resPayload.Data.TaskResult.Videos; len(videos) > 0 {
  331. video := videos[0]
  332. taskInfo.Url = video.Url
  333. }
  334. return taskInfo, nil
  335. }