mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-05 08:03:01 +00:00
fix: 修复图片任务插件参数覆盖并补充百炼回归测试 (#8027)
* fix: 修复图片任务插件参数覆盖并补充百炼回归测试 * fix(task): isolate channel headers and track retry attempts
This commit is contained in:
1 parent
b2d12187a0
commit
329e899ebe
8 files changed
+458
-1
No files matched your search
@@ -99,6 +99,13 @@ func serveTaskPluginImageProtocol(c *gin.Context, pinned pluginruntime.PinnedEnd
|
||||
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
||||
return
|
||||
}
|
||||
// Submission may retry with a different channel's rules. Render and inline
|
||||
// images using the final attempt's request, not the original client body.
|
||||
if value, exists := c.Get(pluginruntime.ContextKeyEffectiveImageRequest); exists {
|
||||
if effective, ok := value.(pluginruntime.ProtocolRequestContext); ok {
|
||||
protocolRequest = effective
|
||||
}
|
||||
}
|
||||
task := outcome.Task
|
||||
if task.Status != model.TaskStatusSuccess && task.Status != model.TaskStatusFailure {
|
||||
if taskErr = waitTaskPluginImageTask(c, task, deps); taskErr != nil {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
@@ -17,7 +19,9 @@ import (
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
pluginruntime "github.com/QuantumNous/new-api/pkg/jsplugin"
|
||||
"github.com/QuantumNous/new-api/plugins"
|
||||
"github.com/QuantumNous/new-api/relay"
|
||||
taskjsplugin "github.com/QuantumNous/new-api/relay/channel/task/jsplugin"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -1713,6 +1717,308 @@ func TestServeTaskPluginImageProtocolRendersImmediateResultAndInlinesBase64(t *t
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeTaskPluginImageProtocolAppliesChannelOverrides(t *testing.T) {
|
||||
loaded := make(map[string]*pluginruntime.LoadedPlugin)
|
||||
for _, key := range []string{"doubao", "alibaba"} {
|
||||
source, err := plugins.Source(key)
|
||||
require.NoError(t, err)
|
||||
loaded[key], err = pluginruntime.NewRegistry().RegisterFactory(source, pluginruntime.Options{Key: key})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
force := `{"operations":[{"mode":"set","path":"response_format","value":"b64_json","conditions":[{"path":"original_model","mode":"contains","value":"seedream"}]}]}`
|
||||
keep := strings.Replace(force, `"mode":"set"`, `"keep_origin":true,"mode":"set"`, 1)
|
||||
bailian := strings.Replace(force, `"value":"seedream"`, `"value":"bailian"`, 1)
|
||||
for _, tc := range []struct {
|
||||
name, clientFormat string
|
||||
pluginKey string
|
||||
model string
|
||||
overrides []string
|
||||
prompts []string
|
||||
multipart bool
|
||||
async bool
|
||||
wantParameters map[string]any
|
||||
wantB64 bool
|
||||
wantCount float64
|
||||
wantStatus int
|
||||
wantCode string
|
||||
wantNoRetry bool
|
||||
}{
|
||||
{name: "original model fills missing format", overrides: []string{force}, wantB64: true},
|
||||
{name: "force replaces URL", clientFormat: "url", overrides: []string{force}, wantB64: true},
|
||||
{name: "keep origin preserves URL", clientFormat: "url", overrides: []string{keep}},
|
||||
{name: "keep origin fills missing format", overrides: []string{keep}, wantB64: true},
|
||||
{name: "unmatched condition", overrides: []string{strings.Replace(force, `"value":"seedream"`, `"value":"other"`, 1)}},
|
||||
{name: "condition uses mapped model", overrides: []string{strings.Replace(strings.Replace(force, `"original_model"`, `"model"`, 1), `"value":"seedream"`, `"value":"5-0-lite"`, 1)}, wantB64: true},
|
||||
{name: "legacy override", overrides: []string{`{"response_format":"b64_json"}`}, wantB64: true},
|
||||
{name: "multipart edit preserves upload", multipart: true, overrides: []string{`{"response_format":"b64_json","seed":7}`}, wantB64: true},
|
||||
{name: "usage follows overridden group count", overrides: []string{`{"response_format":"b64_json","sequential_image_generation":"auto","sequential_image_generation_options":{"max_images":3}}`}, wantB64: true, wantCount: 3},
|
||||
{name: "retry starts from original request", overrides: []string{
|
||||
`{"response_format":"b64_json","operations":[{"mode":"append","path":"prompt","value":" first"}]}`,
|
||||
`{"operations":[{"mode":"append","path":"prompt","value":" second"}]}`,
|
||||
}, prompts: []string{"a cat first", "a cat second"}},
|
||||
{name: "retry without rules restores client format", clientFormat: "b64_json", overrides: []string{`{"response_format":"url"}`, `{}`}, wantB64: true},
|
||||
{name: "decoder rejects overridden count", overrides: []string{`{"n":129}`}, wantStatus: 400, wantCode: "plugin_request_invalid"},
|
||||
{name: "decoder rejects overridden format", overrides: []string{`{"response_format":"invalid"}`}, wantStatus: 400, wantCode: "plugin_request_invalid"},
|
||||
{name: "model pin cannot change", overrides: []string{`{"model":"another-model"}`}, wantStatus: 400, wantCode: "channel_param_override_invalid", wantNoRetry: true},
|
||||
{name: "invalid rule stops submission", overrides: []string{`{"operations":[{"mode":"invalid","path":"response_format"}]}`}, wantStatus: 400, wantCode: "channel_param_override_invalid", wantNoRetry: true},
|
||||
{name: "return error stops submission", overrides: []string{`{"operations":[{"mode":"return_error","value":{"message":"blocked by image rule","status_code":429,"code":"image_rule_blocked"}}]}`}, wantStatus: 429, wantCode: "image_rule_blocked", wantNoRetry: true},
|
||||
{name: "alibaba/no overrides preserves URL", pluginKey: "alibaba", overrides: []string{`{}`}},
|
||||
{name: "alibaba/client Base64 without overrides", pluginKey: "alibaba", clientFormat: "b64_json", overrides: []string{`{}`}, wantB64: true},
|
||||
{name: "alibaba/Seedream condition does not match", pluginKey: "alibaba", overrides: []string{force}},
|
||||
{name: "alibaba/original alias condition", pluginKey: "alibaba", model: "bailian-image", overrides: []string{bailian}, wantB64: true},
|
||||
{name: "alibaba/mapped model condition", pluginKey: "alibaba", model: "bailian-image", overrides: []string{strings.Replace(strings.Replace(force, `"original_model"`, `"model"`, 1), `"value":"seedream"`, `"value":"qwen-image"`, 1)}, wantB64: true},
|
||||
{name: "alibaba/force replaces URL", pluginKey: "alibaba", clientFormat: "url", overrides: []string{`{"response_format":"b64_json"}`}, wantB64: true},
|
||||
{name: "alibaba/keep origin preserves URL", pluginKey: "alibaba", model: "bailian-image", clientFormat: "url", overrides: []string{strings.Replace(bailian, `"mode":"set"`, `"keep_origin":true,"mode":"set"`, 1)}},
|
||||
{name: "alibaba/Qwen parameters and usage", pluginKey: "alibaba", overrides: []string{`{"response_format":"b64_json","n":2,"size":"1024x1024"}`}, wantB64: true, wantCount: 2, wantParameters: map[string]any{"n": 2, "size": "1024*1024"}},
|
||||
{name: "alibaba/nested provider parameters and usage", pluginKey: "alibaba", overrides: []string{`{"response_format":"b64_json","operations":[{"mode":"set","path":"parameters.n","value":2},{"mode":"set","path":"parameters.size","value":"1024x1024"},{"mode":"set","path":"parameters.watermark","value":false}]}`}, wantB64: true, wantCount: 2, wantParameters: map[string]any{"n": 2, "size": "1024*1024", "watermark": false}},
|
||||
{name: "alibaba/multipart Qwen edit", pluginKey: "alibaba", model: "qwen-image-edit-plus", multipart: true, overrides: []string{`{"response_format":"b64_json","n":2,"seed":7,"watermark":false}`}, wantB64: true, wantCount: 2, wantParameters: map[string]any{"n": 2, "seed": 7, "watermark": false}},
|
||||
{name: "alibaba/legacy Wan async result", pluginKey: "alibaba", model: "wan2.2-t2i-flash", async: true, overrides: []string{`{"response_format":"b64_json","n":2,"size":"1024x1024"}`}, wantB64: true, wantCount: 2, wantParameters: map[string]any{"n": 2, "size": "1024*1024"}},
|
||||
{name: "alibaba/modern Wan async result", pluginKey: "alibaba", model: "wan2.6-t2i", async: true, overrides: []string{`{"response_format":"b64_json","n":2}`}, wantB64: true, wantCount: 2, wantParameters: map[string]any{"n": 2}},
|
||||
{name: "alibaba/provider rejects oversized count", pluginKey: "alibaba", overrides: []string{`{"n":129}`}, wantStatus: 400, wantCode: "plugin_request_invalid"},
|
||||
{name: "alibaba/provider rejects oversized nested count", pluginKey: "alibaba", overrides: []string{`{"operations":[{"mode":"set","path":"parameters.n","value":129}]}`}, wantStatus: 400, wantCode: "plugin_request_invalid"},
|
||||
{name: "alibaba/fixed count model remains restricted", pluginKey: "alibaba", model: "qwen-image-plus", overrides: []string{`{"n":2}`}, wantStatus: 400, wantCode: "plugin_request_invalid"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
pluginKey, clientModel := tc.pluginKey, tc.model
|
||||
if pluginKey == "" {
|
||||
pluginKey = "doubao"
|
||||
}
|
||||
if clientModel == "" {
|
||||
clientModel = "doubao-seedream"
|
||||
if pluginKey == "alibaba" {
|
||||
clientModel = "qwen-image-3.0-pro"
|
||||
}
|
||||
}
|
||||
upstreamModel := "doubao-seedream-5-0-lite-260128"
|
||||
if pluginKey == "alibaba" {
|
||||
upstreamModel = clientModel
|
||||
if clientModel == "bailian-image" {
|
||||
upstreamModel = "qwen-image-3.0-pro"
|
||||
}
|
||||
}
|
||||
plugin := loaded[pluginKey]
|
||||
pinned := pluginruntime.PinnedEndpoint{Plugin: plugin, Model: clientModel, Protocol: pluginruntime.ProtocolOpenAIImage}
|
||||
c, recorder := newImageProtocolTestContext("")
|
||||
c.Set("resolved_task_model", pinned.Model)
|
||||
c.Set(pluginruntime.ContextKeyPinnedEndpoint, pinned)
|
||||
value := map[string]any{"model": pinned.Model, "prompt": "a cat", "n": 1}
|
||||
if tc.clientFormat != "" {
|
||||
value["response_format"] = tc.clientFormat
|
||||
}
|
||||
raw, err := common.Marshal(value)
|
||||
require.NoError(t, err)
|
||||
request := pluginruntime.ProtocolRequestContext{
|
||||
RouteRequestContext: pluginruntime.RouteRequestContext{Body: map[string]any{"kind": "json", "value": value}, BodyText: raw},
|
||||
Protocol: pluginruntime.ProtocolOpenAIImage, Operation: "generate", Model: pinned.Model,
|
||||
}
|
||||
if tc.multipart {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
fields := map[string][]string{"model": {pinned.Model}, "prompt": {"a cat"}, "n": {"1"}}
|
||||
for key, values := range fields {
|
||||
require.NoError(t, writer.WriteField(key, values[0]))
|
||||
}
|
||||
file, err := writer.CreateFormFile("image", "reference.png")
|
||||
require.NoError(t, err)
|
||||
_, err = file.Write([]byte("AAA"))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, writer.Close())
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1/images/edits", &body)
|
||||
c.Request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
request.Operation, request.BodyText = "edit", nil
|
||||
request.Body = map[string]any{"kind": "multipart", "fields": fields, "files": []any{map[string]any{"ref": "request_file:image", "field": "image", "filename": "reference.png", "mimeType": "image/png", "size": 3}}}
|
||||
}
|
||||
original, err := common.Marshal(request)
|
||||
require.NoError(t, err)
|
||||
originalText := string(request.BodyText)
|
||||
c.Set(pluginruntime.ContextKeyProtocolRequest, request)
|
||||
deps := pluginProtocolTestDeps()
|
||||
downloads, built, polls := 0, 0, 0
|
||||
var finalAdaptor *taskjsplugin.TaskAdaptor
|
||||
deps.downloadImage = func(string) (string, string, error) {
|
||||
downloads++
|
||||
return "image/png", "QUFB", nil
|
||||
}
|
||||
deps.submit = func(c *gin.Context, info *relaycommon.RelayInfo) (*taskSubmissionOutcome, *dto.TaskError) {
|
||||
info.PublicTaskID = "task_image"
|
||||
for index, rawOverride := range tc.overrides {
|
||||
var override map[string]any
|
||||
require.NoError(t, common.UnmarshalJsonStr(rawOverride, &override))
|
||||
info.ChannelMeta = &relaycommon.ChannelMeta{
|
||||
ChannelBaseUrl: "https://provider.example", UpstreamModelName: upstreamModel, ParamOverride: override,
|
||||
}
|
||||
adaptor := taskjsplugin.New(plugin)
|
||||
adaptor.Init(info)
|
||||
if taskErr := adaptor.ValidateRequestAndSetAction(c, info); taskErr != nil {
|
||||
assert.Equal(t, tc.wantNoRetry, taskErr.NoRetry)
|
||||
return nil, taskErr
|
||||
}
|
||||
body, err := adaptor.BuildRequestBody(c, info)
|
||||
require.NoError(t, err)
|
||||
var upstream map[string]any
|
||||
require.NoError(t, common.DecodeJson(body, &upstream))
|
||||
built++
|
||||
assert.NotContains(t, upstream, "response_format", "the host owns Base64 conversion")
|
||||
assert.Equal(t, info.UpstreamModelName, upstream["model"])
|
||||
wantPrompt := "a cat"
|
||||
if len(tc.prompts) > 0 {
|
||||
wantPrompt = tc.prompts[index]
|
||||
}
|
||||
if pluginKey == "alibaba" {
|
||||
input := upstream["input"].(map[string]any)
|
||||
parameters := upstream["parameters"].(map[string]any)
|
||||
assert.NotContains(t, parameters, "response_format")
|
||||
assert.EqualValues(t, max(1, tc.wantCount), parameters["n"])
|
||||
for key, expected := range tc.wantParameters {
|
||||
assert.EqualValues(t, expected, parameters[key], key)
|
||||
}
|
||||
if tc.model == "wan2.2-t2i-flash" {
|
||||
assert.Equal(t, wantPrompt, input["prompt"])
|
||||
} else {
|
||||
messages := input["messages"].([]any)
|
||||
require.Len(t, messages, 1)
|
||||
content := messages[0].(map[string]any)["content"].([]any)
|
||||
assert.Equal(t, wantPrompt, content[len(content)-1].(map[string]any)["text"])
|
||||
if tc.multipart {
|
||||
require.Len(t, content, 2)
|
||||
assert.Equal(t, "data:image/png;base64,QUFB", content[0].(map[string]any)["image"])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
assert.Equal(t, wantPrompt, upstream["prompt"])
|
||||
if tc.multipart {
|
||||
assert.Equal(t, []any{"data:image/png;base64,QUFB"}, upstream["image"])
|
||||
assert.EqualValues(t, 7, upstream["seed"])
|
||||
}
|
||||
}
|
||||
ratios, err := adaptor.EstimateBillingValidated(c, info)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, max(1, tc.wantCount), ratios["image_count"], "usage follows the submitted request")
|
||||
finalAdaptor = adaptor
|
||||
}
|
||||
task := &model.Task{TaskID: "task_image", Platform: constant.TaskPlatform(pluginKey), UserId: info.UserId, Status: model.TaskStatusSuccess,
|
||||
Properties: model.Properties{OriginModelName: clientModel, UpstreamModelName: upstreamModel}}
|
||||
task.SetData(map[string]any{"data": []any{map[string]any{"url": "https://cdn.example/image.png"}}})
|
||||
if pluginKey == "alibaba" {
|
||||
result := `{"request_id":"request_image","output":{"choices":[{"message":{"content":[{"image":"https://cdn.example/image.png"}]}}]},"usage":{"output_image_count":1}}`
|
||||
if tc.async {
|
||||
result = `{"output":{"task_id":"upstream_image","task_status":"PENDING"}}`
|
||||
}
|
||||
parsed, taskErr := finalAdaptor.ParseResponse(c, &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader(result))}, info)
|
||||
require.Nil(t, taskErr)
|
||||
require.NotNil(t, parsed)
|
||||
task.PrivateData.UpstreamTaskID, task.Data = parsed.UpstreamTaskID, parsed.TaskData
|
||||
if tc.async {
|
||||
require.Nil(t, parsed.Immediate)
|
||||
assert.Equal(t, "upstream_image", parsed.UpstreamTaskID)
|
||||
task.Status = model.TaskStatusSubmitted
|
||||
} else {
|
||||
require.NotNil(t, parsed.Immediate)
|
||||
assert.Equal(t, "SUCCESS", parsed.Immediate.Status)
|
||||
assert.EqualValues(t, 1, parsed.Immediate.UsageFacts["image_count"], "completion uses the delivered count rather than the overridden reservation")
|
||||
assert.EqualValues(t, 1, finalAdaptor.AdjustBillingOnSubmit(info, parsed.TaskData)["image_count"])
|
||||
}
|
||||
}
|
||||
return &taskSubmissionOutcome{Task: task, RelayInfo: info}, nil
|
||||
}
|
||||
if tc.async {
|
||||
deps.imagePollInterval = time.Nanosecond
|
||||
deps.pollTask = func(_ context.Context, task *model.Task) error {
|
||||
polls++
|
||||
assert.Equal(t, model.TaskStatusSubmitted, string(task.Status))
|
||||
completed := []byte(`{"output":{"task_id":"upstream_image","task_status":"SUCCEEDED","results":[{"url":"https://cdn.example/image.png"}]},"usage":{"image_count":1}}`)
|
||||
result, err := finalAdaptor.ParseTaskResult(task, &http.Response{StatusCode: http.StatusOK}, completed)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "SUCCESS", result.Status)
|
||||
assert.EqualValues(t, 1, result.UsageFacts["image_count"])
|
||||
task.Status, task.Data = model.TaskStatus(result.Status), completed
|
||||
return nil
|
||||
}
|
||||
}
|
||||
serveTaskPluginImageProtocol(c, pinned, deps)
|
||||
current, err := common.Marshal(request)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, string(original), string(current), "original request maps remain unchanged")
|
||||
assert.Equal(t, originalText, string(request.BodyText), "original JSON bytes remain unchanged")
|
||||
wantStatus := tc.wantStatus
|
||||
if wantStatus == 0 {
|
||||
wantStatus = http.StatusOK
|
||||
}
|
||||
require.Equal(t, wantStatus, recorder.Code, recorder.Body.String())
|
||||
if tc.wantCode != "" {
|
||||
var response struct {
|
||||
Error struct{ Code string } `json:"error"`
|
||||
}
|
||||
require.NoError(t, common.Unmarshal(recorder.Body.Bytes(), &response))
|
||||
assert.Equal(t, tc.wantCode, response.Error.Code)
|
||||
assert.Zero(t, built)
|
||||
assert.Zero(t, downloads)
|
||||
return
|
||||
}
|
||||
assert.Equal(t, len(tc.overrides), built)
|
||||
wantPolls := 0
|
||||
if tc.async {
|
||||
wantPolls = 1
|
||||
}
|
||||
assert.Equal(t, wantPolls, polls)
|
||||
var response struct {
|
||||
Data []struct {
|
||||
URL string `json:"url"`
|
||||
B64 string `json:"b64_json"`
|
||||
} `json:"data"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
}
|
||||
require.NoError(t, common.Unmarshal(recorder.Body.Bytes(), &response))
|
||||
require.Len(t, response.Data, 1)
|
||||
wantB64, wantDownloads := "", 0
|
||||
if tc.wantB64 {
|
||||
wantB64, wantDownloads = "QUFB", 1
|
||||
}
|
||||
assert.Equal(t, wantDownloads, downloads)
|
||||
assert.Equal(t, wantB64, response.Data[0].B64)
|
||||
assert.Equal(t, "https://cdn.example/image.png", response.Data[0].URL)
|
||||
if pluginKey == "alibaba" {
|
||||
assert.Contains(t, response.Metadata, "output", "vendor metadata remains available with Base64 output")
|
||||
usage, ok := response.Metadata["usage"].(map[string]any)
|
||||
require.True(t, ok)
|
||||
countKey := "output_image_count"
|
||||
if tc.async {
|
||||
countKey = "image_count"
|
||||
}
|
||||
assert.EqualValues(t, 1, usage[countKey])
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageProtocolOverridesPreservePluginJSONOrder(t *testing.T) {
|
||||
source := strings.Replace(imageProtocolTestPlugin, `protocols:["openai_image"]`, `requiredCapabilities:["json-order@1"],protocols:["openai_image"]`, 1)
|
||||
source = strings.Replace(source, `return {url:ctx.baseUrl+"/images"}`, `return {url:ctx.baseUrl+"/images",body:ctx.requestBody}`, 1)
|
||||
plugin, err := pluginruntime.NewRegistry().Register(source, pluginruntime.Options{})
|
||||
require.NoError(t, err)
|
||||
c, _ := newImageProtocolTestContext("")
|
||||
client := `{"model":"image-model","prompt":"a cat","metadata":{"zeta":1,"alpha":2}}`
|
||||
var value map[string]any
|
||||
require.NoError(t, common.UnmarshalJsonStr(client, &value))
|
||||
c.Set(pluginruntime.ContextKeyProtocolRequest, pluginruntime.ProtocolRequestContext{
|
||||
RouteRequestContext: pluginruntime.RouteRequestContext{Body: map[string]any{"kind": "json", "value": value}, BodyText: []byte(client)},
|
||||
Protocol: pluginruntime.ProtocolOpenAIImage, Model: "image-model",
|
||||
})
|
||||
c.Set(pluginruntime.ContextKeyPinnedEndpoint, pluginruntime.PinnedEndpoint{Plugin: plugin, Protocol: pluginruntime.ProtocolOpenAIImage, Model: "image-model"})
|
||||
info := &relaycommon.RelayInfo{
|
||||
ChannelMeta: &relaycommon.ChannelMeta{ChannelBaseUrl: "https://provider.example", UpstreamModelName: "image-model", ParamOverride: map[string]any{"response_format": "b64_json"}},
|
||||
OriginModelName: "image-model", TaskRelayInfo: &relaycommon.TaskRelayInfo{},
|
||||
}
|
||||
adaptor := taskjsplugin.New(plugin)
|
||||
adaptor.Init(info)
|
||||
require.Nil(t, adaptor.ValidateRequestAndSetAction(c, info))
|
||||
body, err := adaptor.BuildRequestBody(c, info)
|
||||
require.NoError(t, err)
|
||||
raw, err := io.ReadAll(body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"model":"image-model","prompt":"a cat","metadata":{"zeta":1,"alpha":2},"response_format":"b64_json"}`, string(raw))
|
||||
}
|
||||
|
||||
// An asynchronous vendor task is polled inside the request until it is
|
||||
// terminal; a failure and the protocol timeout become OpenAI error envelopes
|
||||
// while the durable task stays with the background poller.
|
||||
|
||||
@@ -665,6 +665,10 @@ func executeTaskSubmissionWith(
|
||||
}
|
||||
|
||||
for ; retryParam.GetRetry() <= common.RetryTimes; retryParam.IncreaseRetry() {
|
||||
relayInfo.RetryIndex = retryParam.GetRetry()
|
||||
relayInfo.RuntimeHeadersOverride = nil
|
||||
relayInfo.UseRuntimeHeadersOverride = false
|
||||
|
||||
stage = "select_channel"
|
||||
if requestErr := c.Request.Context().Err(); requestErr != nil {
|
||||
diagnostics.cancelled("before_attempt", retryParam.GetRetry()+1)
|
||||
|
||||
Vendored
+1
-1
@@ -132,7 +132,7 @@ export declare const native: Record<string, ((ctx: NativeDecodeContext) => TaskI
|
||||
export declare const protocols: {
|
||||
openai_responses?: {decodeRequest(ctx: ProtocolDecodeContext): SubmitIntent; renderEvents?(ctx: unknown, task: TaskView, previousState: unknown): unknown; renderFinal?(ctx: unknown, task: TaskView): unknown};
|
||||
openai_video?: {decodeRequest(ctx: ProtocolDecodeContext): SubmitIntent; render(ctx: unknown, task: TaskView): unknown};
|
||||
/** render returns the OpenAI ImageResponse; the host adds `created` when absent and resolves response_format b64_json. */
|
||||
/** The final decode and render receive the current channel's parameter overrides. render returns the OpenAI ImageResponse; the host adds `created` when absent and resolves the effective response_format b64_json. */
|
||||
openai_image?: {decodeRequest(ctx: ProtocolDecodeContext): SubmitIntent; render(ctx: unknown, task: TaskView): {created?: number; data: readonly ImageResponseEntry[]} & Record<string, unknown>};
|
||||
};
|
||||
export declare function buildSubmitRequest(ctx: DriverContext): RequestDescriptor;
|
||||
|
||||
@@ -804,6 +804,18 @@ The host registry owns these bindings:
|
||||
|
||||
`protocols.openai_image` requires `decodeRequest` and `render` and defines no modes. Both operations are synchronous from the client's point of view. The host extracts and pins `ctx.model` (`ctx.operation` is `generate` or `edit`), the decoder returns the usual submit intent, and `buildSubmitRequest` runs against the vendor exactly as for native routes. A vendor answering synchronously returns a terminal `immediate` from `parseSubmitResponse`; a vendor task API returns a task id, and the host then polls the task inside the client request through the same query hooks and completion settlement the background poller uses (`buildQueryRequest`, `parseTaskResult`, `extractUsageOnComplete`), every few seconds until the task is terminal or the protocol timeout (`TASK_PLUGIN_PROTOCOL_TIMEOUT_SECONDS`, default 600 seconds) elapses. On timeout the client receives 504 `task_timeout`; the task remains durable and settles when the poller finishes it. A client that disconnects while waiting leaves the task to the poller as well. A terminal `FAILURE` is answered with 400 and the task's failure reason. On `SUCCESS` the host calls `render(ctx, task)` with the request context and the terminal `TaskView`; the hook must return an object whose `data` is an array of OpenAI image entries (`{url}` or `{b64_json}`, optionally `revised_prompt`). The host adds `created` when absent and owns `response_format`: for `b64_json` it downloads every `data[].url` and adds `b64_json` (the URL is kept; a failed download logs a warning and leaves the URL). `render` receives no `artifacts`; image URLs come from the vendor result. Billing follows the ordinary task contract: reserve on the requested count from `extractUsage`, settle to the actual count from `extractUsageOnSubmit` (immediate results, legacy pricing), `extractUsageOnComplete` (immediate results and polled results, task expressions); the consume log records the billed count as `other.image_count` when the facts carry `image_count`.
|
||||
|
||||
For `openai_image`, channel parameter overrides apply to the JSON body or
|
||||
multipart text fields before the selected plugin's final `decodeRequest` call.
|
||||
The initial candidate decode still validates the client's request for routing.
|
||||
The final decoder validates the overridden request, and driver and usage hooks
|
||||
receive its normalized result. Conditions use `original_model` for the client's
|
||||
model and `model` / `upstream_model` for the channel-mapped model. An override
|
||||
cannot change or remove that model; use channel model mapping instead. Uploaded
|
||||
file references are preserved. Each retry derives a fresh request from the
|
||||
original body, and `render` and the host's `b64_json` conversion use the final
|
||||
attempt's effective request. The original request stays unchanged, including
|
||||
JSON member order for plugins declaring `json-order@1`.
|
||||
|
||||
Responses protocol media uses host-injected `ctx.artifacts[key].url`. OpenAI Video renderers may return provider fields and URLs from `task.data`; the host preserves those extensions and metadata, but overwrites `id`, `object`, `model`, `status`, `progress`, `created_at`, and `completed_at` and removes legacy `task_id`. Renderers must return a JSON object. Returning `task.data` exposes the latest persisted upstream snapshot, subject to the host's task-ID replacement and inline-video redaction; it does not perform a fresh upstream query or forward HTTP headers/status codes.
|
||||
|
||||
## Persisted data and driver hooks
|
||||
|
||||
@@ -246,6 +246,9 @@ const (
|
||||
ContextKeyPinnedEndpoint = "task_plugin_pinned_endpoint"
|
||||
ContextKeyRouteRequest = "task_plugin_route_request"
|
||||
ContextKeyProtocolRequest = "task_plugin_protocol_request"
|
||||
// ContextKeyEffectiveImageRequest holds the current attempt's image request
|
||||
// after channel overrides. ProtocolRequest remains the immutable retry source.
|
||||
ContextKeyEffectiveImageRequest = "task_plugin_effective_image_request"
|
||||
// ContextKeyRequestBodyText holds, for a plugin that preserves JSON
|
||||
// order, the decoded requestBody as JSON text (json.RawMessage) beside
|
||||
// the Go value in task_request.
|
||||
|
||||
@@ -163,10 +163,25 @@ func New(plugin *pluginruntime.LoadedPlugin) *TaskAdaptor { return &TaskAdaptor{
|
||||
func (a *TaskAdaptor) Init(info *relaycommon.RelayInfo) { a.info = info }
|
||||
|
||||
func (a *TaskAdaptor) ValidateRequestAndSetAction(c *gin.Context, info *relaycommon.RelayInfo) *dto.TaskError {
|
||||
c.Set(pluginruntime.ContextKeyEffectiveImageRequest, nil)
|
||||
if pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedEndpoint); exists {
|
||||
if pinned, ok := pinnedValue.(pluginruntime.PinnedEndpoint); ok && pinned.Plugin == a.plugin {
|
||||
if protocolValue, present := c.Get(pluginruntime.ContextKeyProtocolRequest); present {
|
||||
if protocolContext, valid := protocolValue.(pluginruntime.ProtocolRequestContext); valid {
|
||||
if pinned.Protocol == pluginruntime.ProtocolOpenAIImage {
|
||||
var err error
|
||||
protocolContext, err = applyImageProtocolParamOverride(protocolContext, info)
|
||||
if err != nil {
|
||||
taskErr := service.TaskErrorWrapperLocal(err, "channel_param_override_invalid", http.StatusBadRequest)
|
||||
taskErr.NoRetry = true
|
||||
if fixed, ok := relaycommon.AsParamOverrideReturnError(err); ok {
|
||||
taskErr.Code, taskErr.Message = fixed.Code, fixed.Message
|
||||
taskErr.StatusCode, taskErr.NoRetry = fixed.StatusCode, fixed.SkipRetry
|
||||
}
|
||||
return taskErr
|
||||
}
|
||||
c.Set(pluginruntime.ContextKeyEffectiveImageRequest, protocolContext)
|
||||
}
|
||||
resolvedValue, requestBodyText, callErr := a.plugin.Engine.CallPathWithMemberJSON(
|
||||
context.WithoutCancel(c.Request.Context()), 0, a.plugin.Meta.JSONTextMember("requestBody"),
|
||||
"protocols", []string{pinned.Protocol, "decodeRequest"}, protocolContext.JSValueFor(a.plugin.Meta),
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package jsplugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
pluginruntime "github.com/QuantumNous/new-api/pkg/jsplugin"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
// applyImageProtocolParamOverride derives an attempt-local request before the
|
||||
// plugin's final decoder, so validation, usage and response rendering agree.
|
||||
// The original request and its file references remain available for retries.
|
||||
func applyImageProtocolParamOverride(request pluginruntime.ProtocolRequestContext, info *relaycommon.RelayInfo) (pluginruntime.ProtocolRequestContext, error) {
|
||||
if info == nil || info.ChannelMeta == nil || len(info.ParamOverride) == 0 {
|
||||
return request, nil
|
||||
}
|
||||
body, ok := request.Body.(map[string]any)
|
||||
if !ok {
|
||||
return request, fmt.Errorf("invalid image protocol request body")
|
||||
}
|
||||
var data []byte
|
||||
var err error
|
||||
fields, _ := body["fields"].(map[string][]string)
|
||||
switch body["kind"] {
|
||||
case string(pluginruntime.BodyJSON):
|
||||
if len(request.BodyText) > 0 {
|
||||
data = append([]byte(nil), request.BodyText...)
|
||||
} else {
|
||||
data, err = common.Marshal(body["value"])
|
||||
}
|
||||
case string(pluginruntime.BodyMultipart), string(pluginruntime.BodyForm):
|
||||
values := make(map[string]any, len(fields))
|
||||
for key, entries := range fields {
|
||||
if len(entries) == 1 {
|
||||
values[key] = entries[0]
|
||||
} else {
|
||||
values[key] = entries
|
||||
}
|
||||
}
|
||||
data, err = common.Marshal(values)
|
||||
default:
|
||||
return request, fmt.Errorf("unsupported image protocol request body")
|
||||
}
|
||||
if err != nil {
|
||||
return request, err
|
||||
}
|
||||
// Match model conditions against the mapped name, as ordinary image relay
|
||||
// does. The decoder still receives the pinned client model afterwards.
|
||||
upstreamModel := info.UpstreamModelName
|
||||
if upstreamModel == "" {
|
||||
upstreamModel = request.Model
|
||||
}
|
||||
data, err = sjson.SetBytes(data, "model", upstreamModel)
|
||||
if err != nil {
|
||||
return request, err
|
||||
}
|
||||
data, err = relaycommon.ApplyParamOverrideWithRelayInfo(data, info)
|
||||
if err != nil {
|
||||
return request, err
|
||||
}
|
||||
model := gjson.GetBytes(data, "model")
|
||||
if model.Type != gjson.String || model.String() != upstreamModel {
|
||||
return request, fmt.Errorf("image protocol parameter overrides cannot change the pinned model; use channel model mapping")
|
||||
}
|
||||
data, err = sjson.SetBytes(data, "model", request.Model)
|
||||
if err != nil {
|
||||
return request, err
|
||||
}
|
||||
// Never mutate maps that have already been exposed to a plugin hook.
|
||||
body = maps.Clone(body)
|
||||
if body["kind"] == string(pluginruntime.BodyJSON) {
|
||||
var value map[string]any
|
||||
if err = common.Unmarshal(data, &value); err != nil {
|
||||
return request, err
|
||||
}
|
||||
body["value"] = value
|
||||
request.BodyText = data
|
||||
request.RequestBody = value
|
||||
} else {
|
||||
values := gjson.ParseBytes(data)
|
||||
if !values.IsObject() {
|
||||
return request, fmt.Errorf("image protocol parameter overrides must preserve an object request")
|
||||
}
|
||||
effectiveFields := make(map[string][]string)
|
||||
for key, value := range values.Map() {
|
||||
if value.Type == gjson.String {
|
||||
effectiveFields[key] = []string{value.String()}
|
||||
} else if len(fields[key]) > 1 && value.IsArray() {
|
||||
var entries []string
|
||||
if err = common.Unmarshal([]byte(value.Raw), &entries); err != nil {
|
||||
return request, err
|
||||
}
|
||||
effectiveFields[key] = entries
|
||||
} else {
|
||||
// Multipart scalars, objects and arrays reach the decoder in the
|
||||
// same text form as fields supplied by the client.
|
||||
effectiveFields[key] = []string{value.Raw}
|
||||
}
|
||||
}
|
||||
body["fields"] = effectiveFields
|
||||
request.BodyText = nil
|
||||
request.RequestBody = effectiveFields
|
||||
}
|
||||
request.Body = body
|
||||
return request, nil
|
||||
}
|
||||
Reference in new issue
Block a user