kling_adapter.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func KlingRequestConvert() func(c *gin.Context) {
  11. return func(c *gin.Context) {
  12. var originalReq map[string]interface{}
  13. if err := common.UnmarshalBodyReusable(c, &originalReq); err != nil {
  14. c.Next()
  15. return
  16. }
  17. // Support both model_name and model fields
  18. model, _ := originalReq["model_name"].(string)
  19. if model == "" {
  20. model, _ = originalReq["model"].(string)
  21. }
  22. prompt, _ := originalReq["prompt"].(string)
  23. unifiedReq := map[string]interface{}{
  24. "model": model,
  25. "prompt": prompt,
  26. "metadata": originalReq,
  27. }
  28. jsonData, err := json.Marshal(unifiedReq)
  29. if err != nil {
  30. c.Next()
  31. return
  32. }
  33. // Rewrite request body and path
  34. c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonData))
  35. c.Request.URL.Path = "/v1/video/generations"
  36. if image, ok := originalReq["image"]; !ok || image == "" {
  37. c.Set("action", constant.TaskActionTextGenerate)
  38. }
  39. // We have to reset the request body for the next handlers
  40. c.Set(common.KeyRequestBody, jsonData)
  41. c.Next()
  42. }
  43. }