Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

159 строки
4.3 KiB

  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/constant"
  8. "github.com/QuantumNous/new-api/dto"
  9. "github.com/QuantumNous/new-api/model"
  10. relaycommon "github.com/QuantumNous/new-api/relay/common"
  11. "github.com/QuantumNous/new-api/types"
  12. "github.com/gin-gonic/gin"
  13. )
  14. func convertAipingNativeVideoRequest(body []byte) (relaycommon.TaskSubmitReq, error) {
  15. var raw map[string]any
  16. if err := common.Unmarshal(body, &raw); err != nil {
  17. return relaycommon.TaskSubmitReq{}, err
  18. }
  19. modelName, _ := raw["model"].(string)
  20. if strings.TrimSpace(modelName) == "" {
  21. return relaycommon.TaskSubmitReq{}, fmt.Errorf("model field is required")
  22. }
  23. metadata := map[string]any{}
  24. for key, value := range raw {
  25. if key == "model" || key == "content" {
  26. continue
  27. }
  28. metadata[key] = value
  29. }
  30. var passthroughContent []any
  31. if rawContent, ok := raw["content"].([]any); ok {
  32. for _, item := range rawContent {
  33. itemMap, ok := item.(map[string]any)
  34. if !ok {
  35. passthroughContent = append(passthroughContent, item)
  36. continue
  37. }
  38. passthroughContent = append(passthroughContent, itemMap)
  39. }
  40. }
  41. if len(passthroughContent) > 0 {
  42. metadata["content"] = passthroughContent
  43. }
  44. return relaycommon.TaskSubmitReq{
  45. Model: strings.TrimSpace(modelName),
  46. Metadata: metadata,
  47. }, nil
  48. }
  49. func prepareAipingNativeVideoSubmit(c *gin.Context, info *relaycommon.RelayInfo) error {
  50. bodyStorage, err := common.GetBodyStorage(c)
  51. if err != nil {
  52. return err
  53. }
  54. body, err := bodyStorage.Bytes()
  55. if err != nil {
  56. return err
  57. }
  58. req, err := convertAipingNativeVideoRequest(body)
  59. if err != nil {
  60. return err
  61. }
  62. info.OriginModelName = req.Model
  63. info.Action = constant.TaskActionGenerate
  64. relaycommon.StoreTaskRequest(c, info, constant.TaskActionGenerate, req)
  65. return nil
  66. }
  67. func AipingNativeVideoSubmit(c *gin.Context) {
  68. relayInfo, err := relaycommon.GenRelayInfo(c, types.RelayFormatTask, nil, nil)
  69. if err != nil {
  70. c.JSON(http.StatusInternalServerError, &dto.TaskError{
  71. Code: "gen_relay_info_failed",
  72. Message: err.Error(),
  73. StatusCode: http.StatusInternalServerError,
  74. })
  75. return
  76. }
  77. if relayInfo.ChannelMeta == nil {
  78. relayInfo.ChannelMeta = &relaycommon.ChannelMeta{}
  79. }
  80. if err := prepareAipingNativeVideoSubmit(c, relayInfo); err != nil {
  81. c.JSON(http.StatusBadRequest, &dto.TaskError{
  82. Code: "invalid_request",
  83. Message: err.Error(),
  84. StatusCode: http.StatusBadRequest,
  85. })
  86. return
  87. }
  88. relayTaskWithInfo(c, relayInfo)
  89. }
  90. func buildAipingNativeFetchResponse(task *model.Task) ([]byte, error) {
  91. payload := map[string]any{}
  92. if len(task.Data) > 0 {
  93. if err := common.Unmarshal(task.Data, &payload); err != nil {
  94. return nil, err
  95. }
  96. }
  97. delete(payload, "aiping_id")
  98. if _, ok := payload["error"]; !ok {
  99. payload["error"] = nil
  100. }
  101. payload["id"] = task.TaskID
  102. if _, ok := payload["model"]; !ok {
  103. payload["model"] = task.Properties.OriginModelName
  104. }
  105. if _, ok := payload["status"]; !ok {
  106. payload["status"] = mapAipingNativeStatus(task.Status)
  107. }
  108. if _, ok := payload["created_at"]; !ok {
  109. payload["created_at"] = task.CreatedAt
  110. }
  111. if _, ok := payload["updated_at"]; !ok {
  112. payload["updated_at"] = task.UpdatedAt
  113. }
  114. return common.Marshal(payload)
  115. }
  116. func mapAipingNativeStatus(status model.TaskStatus) string {
  117. switch status {
  118. case model.TaskStatusQueued, model.TaskStatusSubmitted:
  119. return "queued"
  120. case model.TaskStatusInProgress:
  121. return "running"
  122. case model.TaskStatusSuccess:
  123. return "succeeded"
  124. case model.TaskStatusFailure:
  125. return "failed"
  126. default:
  127. return "running"
  128. }
  129. }
  130. func AipingNativeVideoFetch(c *gin.Context) {
  131. taskID := c.Param("task_id")
  132. task, exist, err := model.GetByTaskId(c.GetInt("id"), taskID)
  133. if err != nil {
  134. c.JSON(http.StatusInternalServerError, &dto.TaskError{Code: "get_task_failed", Message: err.Error(), StatusCode: http.StatusInternalServerError})
  135. return
  136. }
  137. if !exist {
  138. c.JSON(http.StatusNotFound, gin.H{"error": gin.H{"code": "not_found", "message": "task not found"}})
  139. return
  140. }
  141. data, err := buildAipingNativeFetchResponse(task)
  142. if err != nil {
  143. c.JSON(http.StatusInternalServerError, &dto.TaskError{Code: "build_response_failed", Message: err.Error(), StatusCode: http.StatusInternalServerError})
  144. return
  145. }
  146. c.Data(http.StatusOK, "application/json", data)
  147. }