kling_adapter.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package middleware
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "one-api/common"
  7. "one-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. model, _ := originalReq["model_name"].(string)
  18. prompt, _ := originalReq["prompt"].(string)
  19. unifiedReq := map[string]interface{}{
  20. "model": model,
  21. "prompt": prompt,
  22. "metadata": originalReq,
  23. }
  24. jsonData, err := json.Marshal(unifiedReq)
  25. if err != nil {
  26. c.Next()
  27. return
  28. }
  29. // Rewrite request body and path
  30. c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonData))
  31. c.Request.URL.Path = "/v1/video/generations"
  32. if image, ok := originalReq["image"]; !ok || image == "" {
  33. c.Set("action", constant.TaskActionTextGenerate)
  34. }
  35. // We have to reset the request body for the next handlers
  36. c.Set(common.KeyRequestBody, jsonData)
  37. c.Next()
  38. }
  39. }