mirror of
https://gh-proxy.org/https://github.com/klinsmaya/new-api.git
synced 2026-11-08 01:17:09 +08:00
Plugin polling hooks previously ran against a hollow context: parseTaskResult
and parseBatchResult received {} / nil, buildQueryRequest received a
{task_id, action} map under the misleading name requestBody, and batch hooks
saw only bare task ids. The per-task poller also never looked at the upstream
HTTP status, and every built-in plugin papered over unrecognized bodies with
`|| "IN_PROGRESS"`, so a 404, a revoked key, or a shape the plugin did not
know would sit in IN_PROGRESS for the full 24h TASK_TIMEOUT_MINUTES while
holding the user's pre-charged quota.
Contract (docs/plugin-api v1.d.ts, v1.md, v1.schema.json):
- TaskQueryContext is declared separately from DriverContext and rebuilt from
the persisted Task row: taskId, publicTaskId, action, model, upstreamModel,
baseUrl, apiKey, authHeader, auth, data, state. Query-side requestBody is
removed; the original request is not persisted and hooks that need a
request-derived value must save it into state at submit time.
- parseTaskResult / parseBatchResult receive a third {status, headers}
argument. Batch hooks receive tasks[] with one TaskQueryContext per task.
- NormalizedTaskResult accepts status "UNKNOWN" meaning "I do not recognize
this body". Falling back to IN_PROGRESS for unknown shapes is forbidden;
`plugin lint` warns on the literal.
- parseSubmitResponse / parseTaskResult / parseBatchResult may return `state`.
Task.Data remains a per-round snapshot overwritten on every valid parse;
state is plugin-owned, persisted in TaskPrivateData.PluginState, preserved
when a hook omits it, byte-capped like taskData, and never exposed through
presenter views.
Host (service/task_polling.go, relay/channel/task/jsplugin/adaptor.go):
- TaskPollingAdaptor / BatchTaskPollingAdaptor take *model.Task and the
*http.Response so the adaptor can build the full context; jsplugin is the
only implementation.
- HTTP classification before the plugin sees the body: 2xx -> plugin;
404/410 -> FAILURE and refund; 401/403 -> poll failure plus a channel-scoped
warning, no auto-disable; 429/5xx/transport -> poll failure; other 4xx ->
plugin with the status visible, counted as unrecognized if the plugin still
reports a non-terminal state.
- TaskPrivateData.PollFailures counts consecutive poll failures (transient
HTTP, auth, transport, hook error, UNKNOWN). It is persisted through the
existing UpdateWithStatus CAS so a concurrent terminal transition on another
instance is never clobbered, and reset on any valid 2xx non-terminal parse.
Reaching TASK_POLL_MAX_FAILURES (default 20, <= 0 disables) fails the task
with the last classification and HTTP code in fail_reason and runs the
existing settle/refund chain exactly once. sweepTimedOutTasks and its
1440-minute default are unchanged as the outer backstop.
- Unrecognized bodies are logged at WARN with a bounded redacted copy since
Task.Data is intentionally not overwritten on that path.
Plugins (all ten bumped one patch version):
- jimeng persists the outbound req_key in state and reads it back in
buildQueryRequest, replacing dead reads of ctx.data / ctx.requestBody that
never resolved.
- sunoapi batch hooks read tasks[] instead of the removed requestBody.
- hailuo treats base_resp.status_code != 0 as FAILURE before the status table.
- kling, vidu, sora, alibaba, doubao, hailuo, jimeng return UNKNOWN with the
raw upstream status in reason on table miss.
- google and vertex-ai treat a missing `done` as in-progress: Google
long-running operations omit proto3 default fields, so a running Veo
operation has no `done` key at all. Only a body without an operation name is
UNKNOWN. plugins/veo_poll_test.go locks this so the poll-failure cutoff can
never fail a rendering Veo task.
Tests cover the classification table end to end against a real DB (404
immediate refund, 429xN refund, 401 increments without status change, 2xx
reset, UNKNOWN increments, state preserved vs replaced, PollFailures survives
the CAS write), the query-context shape, UNKNOWN on unrecognized bodies, and
the absence of PluginState/PollFailures from TaskView. Controller tests derive
the kling factory version from the embedded manifest instead of hardcoding it.
93 lines
3.1 KiB
Go
93 lines
3.1 KiB
Go
package plugins_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/pkg/jsplugin"
|
|
builtinplugins "github.com/QuantumNous/new-api/plugins"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestJimengResponsesProtocol(t *testing.T) {
|
|
testVideoResponsesProtocol(t, videoResponsesTestCase{
|
|
pluginKey: "jimeng",
|
|
model: "jimeng_vgfm_t2v_l20",
|
|
requestBody: map[string]any{
|
|
"model": "jimeng_vgfm_t2v_l20",
|
|
"input": "a paper boat on a river",
|
|
"seconds": 10,
|
|
"metadata": map[string]any{
|
|
"aspect_ratio": "16:9",
|
|
},
|
|
},
|
|
wantAction: "text_to_video",
|
|
wantRequest: map[string]any{
|
|
"model": "jimeng_vgfm_t2v_l20",
|
|
"prompt": "a paper boat on a river",
|
|
"duration": float64(10),
|
|
"metadata": map[string]any{"aspect_ratio": "16:9"},
|
|
},
|
|
wantUsageKeys: []string{"product", "seconds"},
|
|
wantVendorName: "jimeng",
|
|
})
|
|
}
|
|
|
|
func loadJimengPlugin(t *testing.T) *jsplugin.LoadedPlugin {
|
|
t.Helper()
|
|
source, err := builtinplugins.Source("jimeng")
|
|
require.NoError(t, err)
|
|
plugin, err := jsplugin.NewRegistry().RegisterFactory(source, jsplugin.Options{Key: "jimeng"})
|
|
require.NoError(t, err)
|
|
return plugin
|
|
}
|
|
|
|
func TestJimengSubmitStateDrivesQueryReqKey(t *testing.T) {
|
|
plugin := loadJimengPlugin(t)
|
|
submitValue, err := plugin.Engine.Call(t.Context(), "parseSubmitResponse", map[string]any{
|
|
"upstreamModel": "jimeng_vgfm_i2v_l20",
|
|
"requestBody": map[string]any{"images": []any{"https://cdn.example/frame.png"}},
|
|
}, map[string]any{"body": map[string]any{"code": 10000, "data": map[string]any{"task_id": "t1"}}})
|
|
require.NoError(t, err)
|
|
encoded, err := common.Marshal(submitValue)
|
|
require.NoError(t, err)
|
|
var submit map[string]any
|
|
require.NoError(t, common.Unmarshal(encoded, &submit))
|
|
state, ok := submit["state"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "jimeng_vgfm_i2v_l20", state["req_key"])
|
|
|
|
queryValue, err := plugin.Engine.Call(t.Context(), "buildQueryRequest", map[string]any{
|
|
"taskId": "t1",
|
|
"action": "text_to_video",
|
|
"baseUrl": "https://jimeng.example",
|
|
"apiKey": "sk-test",
|
|
"state": map[string]any{"req_key": "custom_req_key"},
|
|
})
|
|
require.NoError(t, err)
|
|
queryEncoded, err := common.Marshal(queryValue)
|
|
require.NoError(t, err)
|
|
var query map[string]any
|
|
require.NoError(t, common.Unmarshal(queryEncoded, &query))
|
|
var body map[string]any
|
|
require.NoError(t, common.UnmarshalJsonStr(common.Interface2String(query["body"]), &body))
|
|
assert.Equal(t, "custom_req_key", body["req_key"])
|
|
assert.Equal(t, "t1", body["task_id"])
|
|
}
|
|
|
|
func TestJimengParseTaskResultUnknownStatus(t *testing.T) {
|
|
plugin := loadJimengPlugin(t)
|
|
value, err := plugin.Engine.Call(t.Context(), "parseTaskResult", map[string]any{}, map[string]any{
|
|
"code": 10000,
|
|
"data": map[string]any{"status": "weird"},
|
|
})
|
|
require.NoError(t, err)
|
|
encoded, err := common.Marshal(value)
|
|
require.NoError(t, err)
|
|
var result map[string]any
|
|
require.NoError(t, common.Unmarshal(encoded, &result))
|
|
assert.Equal(t, "UNKNOWN", result["status"])
|
|
assert.Contains(t, common.Interface2String(result["reason"]), "weird")
|
|
}
|