-
Notifications
You must be signed in to change notification settings - Fork 11.3k
feat: add jimeng video official api #1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "github.com/gin-gonic/gin" | ||
| "io" | ||
| "net/http" | ||
| "one-api/common" | ||
| "one-api/constant" | ||
| relayconstant "one-api/relay/constant" | ||
| ) | ||
|
|
||
| func JimengRequestConvert() func(c *gin.Context) { | ||
| return func(c *gin.Context) { | ||
| action := c.Query("Action") | ||
| if action == "" { | ||
| abortWithOpenAiMessage(c, http.StatusBadRequest, "Action query parameter is required") | ||
| return | ||
| } | ||
|
|
||
| // Handle Jimeng official API request | ||
| var originalReq map[string]interface{} | ||
| if err := common.UnmarshalBodyReusable(c, &originalReq); err != nil { | ||
| abortWithOpenAiMessage(c, http.StatusBadRequest, "Invalid request body") | ||
| return | ||
| } | ||
| model, _ := originalReq["req_key"].(string) | ||
| prompt, _ := originalReq["prompt"].(string) | ||
|
|
||
| unifiedReq := map[string]interface{}{ | ||
| "model": model, | ||
| "prompt": prompt, | ||
| "metadata": originalReq, | ||
| } | ||
|
|
||
|
Comment on lines
+28
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConfirm mapping of req_key to model You populate model from req_key. Verify req_key indeed corresponds to the routing model slug used by Distribute/channel selection; otherwise model-based channel selection may fail. If req_key is not a model slug, introduce a mapping or set a stable Jimeng model name. Would you like me to add a mapping layer or set a default Jimeng model slug? 🏁 Script executed: #!/bin/bash
# Inspect whether a Jimeng platform/model slug exists and how model is used for channel selection.
rg -n --line-number $'TaskPlatformJimeng|Jimeng' || true
rg -n --line-number $'RelayModeVideoSubmit|RelayModeVideoFetchByID' relay || true
rg -n --line-number $'CoverTaskActionToModelName' one-api | sed -n '1,200p'
rg -n --line-number $'CacheGetRandomSatisfiedChannel|GetModelPrice|FormatMatchingModelName' one-api || trueLength of output: 2939 Ensure Please update one of the following:
[middleware/jimeng_adapter.go:28–36] 🤖 Prompt for AI Agents |
||
| jsonData, err := json.Marshal(unifiedReq) | ||
| if err != nil { | ||
| abortWithOpenAiMessage(c, http.StatusInternalServerError, "Failed to marshal request body") | ||
| return | ||
| } | ||
|
|
||
| // Update request body | ||
| c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonData)) | ||
| c.Set(common.KeyRequestBody, jsonData) | ||
|
|
||
| if image, ok := originalReq["image"]; !ok || image == "" { | ||
| c.Set("action", constant.TaskActionTextGenerate) | ||
| } | ||
|
Comment on lines
+47
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainAvoid setting a text-generation action for a video task Setting action to TaskActionTextGenerate may misclassify video submissions and skew action→model resolution. Prefer a video-specific action (e.g., text2video/image2video) if available, or let the adaptor set it during validation. Please confirm available video action constants and usages: 🏁 Script executed: #!/bin/bash
rg -n --line-number $'TaskAction.*Video|TaskActionTextGenerate' one-api || true
rg -n --line-number $'CoverTaskActionToModelName\\(.*\\)' one-api || trueLength of output: 247 Here’s a broader search across the entire repo to locate any video‐related action constants and the mapping function: 🏁 Script executed: #!/bin/bash
# 1. Find where TaskActionTextGenerate is defined or used
rg -n 'TaskActionTextGenerate'
# 2. Look for any TaskAction constants that include “Video”
rg -n 'TaskAction.*Video'
# 3. Locate the CoverTaskActionToModelName function (definition and calls)
rg -n 'CoverTaskActionToModelName'Length of output: 831 Let’s pull the exact list of 🏁 Script executed: #!/bin/bash
# 1. List all TaskAction constants
rg -n 'TaskAction[A-Za-z0-9_]*' constant/task.go
# 2. Show the body of CoverTaskActionToModelName for mapping logic
rg -n -A20 'func CoverTaskActionToModelName' service/task.goLength of output: 368 Introduce or handle a video-specific action constant in the adapter Currently,
No video-specific action exists, so setting c.Set("action", constant.TaskActionTextGenerate)for a payload containing CoverTaskActionToModelName(platform, action)and will misroute video tasks to text models. Please update the code as follows: • In // TaskActionTextToVideo triggers text→video generation
TaskActionTextToVideo = "textToVideo"• In - if image, ok := originalReq["image"]; !ok || image == "" {
- c.Set("action", constant.TaskActionTextGenerate)
- }
+ if _, hasVideo := originalReq["video"]; hasVideo {
+ c.Set("action", constant.TaskActionTextToVideo)
+ } else if image, ok := originalReq["image"]; !ok || image == "" {
+ c.Set("action", constant.TaskActionTextGenerate)
+ }This ensures 🤖 Prompt for AI Agents |
||
|
|
||
| c.Request.URL.Path = "/v1/video/generations" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainSet platform context to ensure correct adaptor selection Make the platform explicit so RelayTaskSubmit picks the Jimeng adaptor without relying on heuristics. c.Request.URL.Path = "/v1/video/generations"
+ // Ensure adaptor selection picks Jimeng implementation
+ // Note: confirm the constant name in your codebase.
+ // c.Set("platform", string(constant.TaskPlatformJimeng))Verify the platform constant exists: 🏁 Script executed: #!/bin/bash
rg -n --line-number $'TaskPlatformJimeng' one-api || trueLength of output: 103 The previous search targeted a non-existent directory. Let’s search the entire repository for the Jimeng constant: 🏁 Script executed: #!/bin/bash
# 1. Look specifically for TaskPlatformJimeng
rg -n "TaskPlatformJimeng" || echo ">> No exact match for TaskPlatformJimeng"
# 2. List all TaskPlatform constants to find the correct name
rg -n "TaskPlatform" -A 2Length of output: 4887 Define and apply Our search indicates
Confirm that 🤖 Prompt for AI Agents |
||
|
|
||
| if action == "CVSync2AsyncGetResult" { | ||
| taskId, ok := originalReq["task_id"].(string) | ||
| if !ok || taskId == "" { | ||
| abortWithOpenAiMessage(c, http.StatusBadRequest, "task_id is required for CVSync2AsyncGetResult") | ||
| return | ||
| } | ||
| c.Request.URL.Path = "/v1/video/generations/" + taskId | ||
| c.Request.Method = http.MethodGet | ||
| c.Set("task_id", taskId) | ||
| c.Set("relay_mode", relayconstant.RelayModeVideoFetchByID) | ||
| } | ||
| c.Next() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Validate Action against allowed values (and optionally Version)
Restrict Action to the official set to fail fast on unsupported requests. Optionally validate Version if the API requires strict versioning.
action := c.Query("Action") if action == "" { abortWithOpenAiMessage(c, http.StatusBadRequest, "Action query parameter is required") return } + allowed := map[string]bool{ + "CVSync2AsyncSubmitTask": true, + "CVSync2AsyncGetResult": true, + } + if !allowed[action] { + abortWithOpenAiMessage(c, http.StatusBadRequest, "unsupported Action") + return + } + // Optionally enforce version compatibility + // if v := c.Query("Version"); v != "2022-08-31" { + // abortWithOpenAiMessage(c, http.StatusBadRequest, "unsupported Version") + // return + // }📝 Committable suggestion
🤖 Prompt for AI Agents