mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 00:03:11 +00:00
* feat(plugins): add provider plugins, request plugins and a community marketplace - Provider plugins turn an upstream vendor into a channel type: routes, protocol conversion, models, static keys, sign-in flows with token refresh, balance queries, per-channel config and a scheduled hook. - Request plugins run on requests and replies (ingress, request, outbound, egress, reply, report), with instances by scope, model ranges, dry runs and per-log records. Built-ins: request filter, request redaction, parameter override, auto reasoning effort, image compression and content moderation. Sensitive words move into a request filter instance on first start. - Node sync: a system_sync_topics table lets every node reload settings, channels, plugins and other data within seconds, and the system info page shows each node's state. - Marketplace: a community source next to the official one, lookup by plugin code for official and community plugins, key rules for community plugins, install counts and takedown warnings. * docs(plugins): name request protocols and host-only members in the plugin API - Protocol names: ctx.format and samples use relaykit's protocol names. The spec lists the client paths and body of each name, so every host passes the same names to plugins. - Host-specific members: channel, group, user, token, channelTypes, channelOther and utils.paramOverride exist only on new-api. The types mark them optional. - Relay routes: the spec now writes out the route keys and the converters each incoming path accepts. The schema lists the converter names. * feat(plugins): show recommended plugins from the official list too The site now marks official plugins as recommended in its index. The built-in official and community lists both get a Recommended group; lists added by the administrator still cannot recommend plugins. * feat(plugins): fall back to www.newapi.pro when www.newapi.ai does not answer Plugin lists, plugin files and logos from www.newapi.ai are downloaded from www.newapi.pro, the mainland China site, when newapi.ai gives no answer within 10 seconds; the browser remembers this for a day. The marketplace toolbar has a download site picker: Auto, or Mainland China to always use www.newapi.pro. Saved source addresses stay on newapi.ai, and every file is still checked against its sha256. Community install counts go to newapi.ai first and to newapi.pro when newapi.ai cannot be reached. * fix(plugins): leaving the page does not switch the plugin site to www.newapi.pro Reloading or leaving the marketplace cancels requests still running. That was read as www.newapi.ai not answering, so the browser moved to www.newapi.pro for a day although newapi.ai was reachable. Requests cut off by leaving the page are no longer counted. * feat(plugins): choose the plugin download site by hand The marketplace no longer switches between plugin sites by itself. The toolbar menu lists the main site www.newapi.ai, the backup site www.newapi.pro and the other plugin lists; picking an entry is the only thing that changes where plugins are downloaded from, and the main site can always be picked again. A browser that the earlier automatic switch had moved to www.newapi.pro opens on the main site again. The download site, the source choice and managing sources now share one menu in the list header. * feat(plugins): show plugin usage guides and require README and changelog Every marketplace plugin version now ships a usage guide, README.md, and a changelog, CHANGELOG.md, next to plugin.js. The install dialog shows the guide in the reader's language when a translation is listed, falls back to English, and links to the source file. A guide or changelog that breaks the format is reported with its file, line and rule instead of being shown. The plugin API document describes both formats, the translations they may list, and the checks the community marketplace runs before it accepts a version: readme_missing, readme_invalid, changelog_missing and changelog_invalid. Versions accepted before these checks are not checked again. * fix(plugins): address review findings on plugin parts, limits and access - Only the root user reads request plugin instance configs; an admin who may bind instances to channels gets them without configs. - Outbound hooks that grow the body are held to MAX_REQUEST_BODY_MB, as ingress hooks are, and answer 413 without a retry. - An admin who cannot read plugin options keeps the balance and model list actions on plugin channels; the server answers whether they work. - Reading the parts of a plugin source gives up when the source does not parse or code elsewhere refers to meta, as the meta preview does. * fix(plugins): address review findings on request processing, documents and node sync - PDF checks resolve indirect parameters and bound every value the parser allocates or loops on: predictor columns, object stream counts, nested object streams, cross-reference streams and ToUnicode ranges. Font and CMap work checks the deadline. - A WebP frame must match its declared canvas, and keyword search stops at its match limit. - Request processing treats a body as JSON whenever the gateway parses it as JSON, keeps Gemini reply parts in place, scopes instances by the settled auto group, hides channel header overrides from plugin param overrides, and captures replies only for report instances in scope. - Request processing no longer follows the plugin master switch, which now covers plugin tasks and channels only. - json.patch copies stop at MAX_REQUEST_BODY_MB, and provider prepareRequest effects past it answer 413. - Image billing reads the count from the final upstream body, including the count members of SiliconFlow, Imagen and Replicate and the Imagen default. - Automatic channel status changes no longer reload every node's channel cache. A credential refresh holds no channel lock while it waits on the vendor. A key that needs login is disabled and the request can retry. - Balance requests must go to the channel host or allowedHosts, and provider stream events a plugin drops keep the stream alive. - Node sync: OAuth provider writes bump inside their transaction, a failed startup load is retried, and each node is judged by its own topics. - request-filter checks the text the old sensitive-word check did, request-redact and content-moderation handle large inputs, and image-compress lossless mode leaves JPEG and WebP alone. * fix(plugins): address the second review on balances, egress and routes - Channel other_info writes (status changes, balance queries and manual multi-key actions) only land while other_info is still the value read, and retry otherwise, so a write on one node no longer overwrites another node's balance or status reason. They write only their own columns. - On the Responses path egress reads the body after the channel finalizes it, which is the body sent upstream. - The Vertex AI plugin picks the region by the model the client asked for, as the built-in Vertex AI channel does. - /request-processing and /security are reserved route namespaces, and a test checks that every console page is reserved. - The plugin API docs say what egress reads on a provider plugin channel. * feat(channels): run the balance update as a system task with progress "Update all balances" now queues a channel_balance system task instead of starting a goroutine guarded by an in-process flag. The task row holds the progress (enabled channels queried of total), so any node can answer it, and its active key keeps one run at a time across nodes. The CHANNEL_UPDATE_FREQUENCY schedule becomes the same task's schedule, so manual and scheduled runs no longer overlap. GET /api/channel/update_balance answers with the task id, and with the running one when a run is already queued. GET /api/channel/update_balance/task/:task_id, under the same channel permission, returns the status, progress and failure of balance update tasks only. The channel list follows the task, reloads as more channels are queried and once more at the end, and reports how the run ended. * feat(system): sync nodes through system tasks and clean up task history Node sync no longer needs its own table. A write to synced data adds a pending sync_<topic> system task. Every node polls the newest task of each topic it keeps every 3 s and reloads when that task is newer than the one it applied. Custom OAuth providers still add their task inside the write transaction, and lock the newest task of the topic first, so tasks commit in id order. The master marks a sync task succeeded once every online node that keeps the topic has applied it. Until then the task shows how many nodes have applied it and why the others have not. The system_sync_topics table is gone. A composite (type, id) index answers the newest task of a type from the index. The latest-per-type and earliest-pending queries now read ids first, because MySQL 5.7 scanned the whole table for them. A daily system_task_cleanup task deletes finished system tasks older than system_task_setting.retention_days (default 30, at least 1), in batches. It never deletes active tasks or the newest task of any type. The retention is set under Operations > Log maintenance.
1035 lines
40 KiB
Go
1035 lines
40 KiB
Go
package controller
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
taskdto "github.com/QuantumNous/new-api/dto"
|
|
"github.com/QuantumNous/new-api/i18n"
|
|
"github.com/QuantumNous/new-api/logger"
|
|
"github.com/QuantumNous/new-api/middleware"
|
|
"github.com/QuantumNous/new-api/model"
|
|
pluginruntime "github.com/QuantumNous/new-api/pkg/jsplugin"
|
|
perfmetrics "github.com/QuantumNous/new-api/pkg/perf_metrics"
|
|
"github.com/QuantumNous/new-api/relay"
|
|
"github.com/QuantumNous/new-api/relay/channel/task/taskcommon"
|
|
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
|
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
|
"github.com/QuantumNous/new-api/relay/helper"
|
|
"github.com/QuantumNous/new-api/relay/requestplugin"
|
|
"github.com/QuantumNous/new-api/relaykit/dto"
|
|
"github.com/QuantumNous/new-api/relaykit/types"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/QuantumNous/new-api/setting/operation_setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
func relayHandler(c *gin.Context, info *relaycommon.RelayInfo) *types.NewAPIError {
|
|
var err *types.NewAPIError
|
|
switch info.RelayMode {
|
|
case relayconstant.RelayModeImagesGenerations, relayconstant.RelayModeImagesEdits:
|
|
err = relay.ImageHelper(c, info)
|
|
case relayconstant.RelayModeAudioSpeech:
|
|
fallthrough
|
|
case relayconstant.RelayModeAudioTranslation:
|
|
fallthrough
|
|
case relayconstant.RelayModeAudioTranscription:
|
|
err = relay.AudioHelper(c, info)
|
|
case relayconstant.RelayModeRerank:
|
|
err = relay.RerankHelper(c, info)
|
|
case relayconstant.RelayModeEmbeddings:
|
|
err = relay.EmbeddingHelper(c, info)
|
|
case relayconstant.RelayModeResponses, relayconstant.RelayModeResponsesCompact:
|
|
err = relay.ResponsesHelper(c, info)
|
|
case relayconstant.RelayModeAlphaSearch:
|
|
err = relay.AlphaSearchHelper(c, info)
|
|
case relayconstant.RelayModeToolsSearch, relayconstant.RelayModeToolsFetch:
|
|
err = relay.DeepSeekToolsHelper(c, info)
|
|
default:
|
|
err = relay.TextHelper(c, info)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func geminiRelayHandler(c *gin.Context, info *relaycommon.RelayInfo) *types.NewAPIError {
|
|
var err *types.NewAPIError
|
|
if strings.Contains(c.Request.URL.Path, "embed") {
|
|
err = relay.GeminiEmbeddingHandler(c, info)
|
|
} else {
|
|
err = relay.GeminiHelper(c, info)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func Relay(c *gin.Context, relayFormat types.RelayFormat) {
|
|
|
|
requestId := c.GetString(common.RequestIdKey)
|
|
//group := common.GetContextKeyString(c, constant.ContextKeyUsingGroup)
|
|
//originalModel := common.GetContextKeyString(c, constant.ContextKeyOriginalModel)
|
|
|
|
var (
|
|
newAPIError *types.NewAPIError
|
|
ws *websocket.Conn
|
|
)
|
|
|
|
if relayFormat == types.RelayFormatOpenAIRealtime {
|
|
var err error
|
|
ws, err = upgrader.Upgrade(c.Writer, c.Request, nil)
|
|
if err != nil {
|
|
helper.WssError(c, ws, types.NewError(err, types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry()).ToOpenAIError())
|
|
return
|
|
}
|
|
defer ws.Close()
|
|
}
|
|
|
|
var relayInfo *relaycommon.RelayInfo
|
|
var ingressRecord []relaycommon.RequestPluginRecord
|
|
// Registered first so that it runs last: the request plugins' report
|
|
// hooks receive the request once the client has the whole response and
|
|
// the request has settled.
|
|
defer func() {
|
|
requestplugin.Report(c, relayFormat, relayInfo, ingressRecord, newAPIError)
|
|
}()
|
|
|
|
defer func() {
|
|
if newAPIError != nil {
|
|
service.RecordRequestPolicyTermination(c, newAPIError)
|
|
logger.LogError(c, common.LogText("relay error: %s", common.LocalLogPreview(newAPIError.Error())))
|
|
newAPIError.SetMessage(common.MessageWithRequestId(newAPIError.Error(), requestId))
|
|
upstreamURLs := common.UpstreamAddresses(c)
|
|
if relayFormat != types.RelayFormatOpenAIRealtime && c.Writer.Written() &&
|
|
strings.HasPrefix(c.Writer.Header().Get("Content-Type"), "text/event-stream") {
|
|
writeRelayStreamError(c, relayFormat, newAPIError)
|
|
return
|
|
}
|
|
if relayFormat != types.RelayFormatOpenAIRealtime {
|
|
c.Header("Content-Type", "application/json; charset=utf-8")
|
|
}
|
|
switch relayFormat {
|
|
case types.RelayFormatOpenAIRealtime:
|
|
helper.WssError(c, ws, newAPIError.ToOpenAIError(upstreamURLs...))
|
|
case types.RelayFormatClaude:
|
|
c.JSON(newAPIError.StatusCode, gin.H{
|
|
"type": "error",
|
|
"error": newAPIError.ToClaudeError(upstreamURLs...),
|
|
})
|
|
default:
|
|
c.JSON(newAPIError.StatusCode, gin.H{
|
|
"error": newAPIError.ToOpenAIError(upstreamURLs...),
|
|
})
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Ingress takes the request plugin plan and may patch the stored body
|
|
// before it is parsed. A Realtime session runs its instances on its
|
|
// events instead.
|
|
var ingressSteps []relaycommon.RequestPluginStep
|
|
if relayFormat == types.RelayFormatOpenAIRealtime {
|
|
requestplugin.StartRealtime(c)
|
|
} else if ingressSteps, ingressRecord, newAPIError = requestplugin.Ingress(c, relayFormat); newAPIError != nil {
|
|
// The request reached no channel; its error log shows what the
|
|
// plugins did.
|
|
ingress := &relaycommon.RelayInfo{}
|
|
ingress.SetRequestPluginIngress(ingressSteps, ingressRecord)
|
|
service.RecordRelayErrorLog(c, 0, newAPIError, ingress)
|
|
return
|
|
}
|
|
|
|
request, err := helper.GetAndValidateRequest(c, relayFormat)
|
|
if err != nil {
|
|
// Map "request body too large" to 413 so clients can handle it correctly
|
|
if common.IsRequestBodyTooLargeError(err) || errors.Is(err, common.ErrRequestBodyTooLarge) {
|
|
newAPIError = types.NewErrorWithStatusCode(err, types.ErrorCodeReadRequestBodyFailed, http.StatusRequestEntityTooLarge, types.ErrOptionWithSkipRetry())
|
|
} else {
|
|
newAPIError = types.NewError(err, types.ErrorCodeInvalidRequest, types.ErrOptionWithStatusCode(http.StatusBadRequest), types.ErrOptionWithSkipRetry())
|
|
}
|
|
return
|
|
}
|
|
|
|
relayInfo, err = relaycommon.GenRelayInfo(c, relayFormat, request, ws)
|
|
if err != nil {
|
|
newAPIError = types.NewError(err, types.ErrorCodeGenRelayInfoFailed)
|
|
return
|
|
}
|
|
relayInfo.SetRequestPluginIngress(ingressSteps, ingressRecord)
|
|
|
|
defer func() {
|
|
recovered := recover()
|
|
resultErr := newAPIError
|
|
if recovered != nil {
|
|
resultErr = types.NewError(fmt.Errorf("relay panic: %v", recovered), types.ErrorCodeBadResponse)
|
|
}
|
|
if relayFormat != types.RelayFormatOpenAIRealtime {
|
|
perfmetrics.RecordRelayResult(c.Request.Context(), relayInfo, resultErr)
|
|
}
|
|
if recovered != nil {
|
|
panic(recovered)
|
|
}
|
|
}()
|
|
|
|
generalSettings := operation_setting.GetGeneralSetting()
|
|
if relayFormat == types.RelayFormatClaude && relayInfo.IsStream && generalSettings.PingIntervalEnabled {
|
|
stopKeepAlive := helper.StartStreamKeepAlive(c, time.Duration(generalSettings.PingIntervalSeconds)*time.Second)
|
|
defer stopKeepAlive()
|
|
}
|
|
|
|
if newAPIError = relay.PrepareRequestBilling(c, relayInfo); newAPIError != nil {
|
|
return
|
|
}
|
|
defer func() {
|
|
newAPIError = relay.RefundFailedRequestBilling(c, relayInfo, newAPIError)
|
|
}()
|
|
|
|
retryParam := &service.RetryParam{
|
|
Ctx: c,
|
|
TokenGroup: relayInfo.TokenGroup,
|
|
ModelName: relayInfo.OriginModelName,
|
|
RequestPath: c.Request.URL.Path,
|
|
Retry: common.GetPointer(0),
|
|
}
|
|
relayInfo.RetryIndex = 0
|
|
relayInfo.LastError = nil
|
|
|
|
for ; retryParam.GetRetry() <= common.RetryTimes; retryParam.IncreaseRetry() {
|
|
relayInfo.StreamStatus = nil
|
|
relayInfo.PerformanceBusinessRejection = false
|
|
relayInfo.PerformanceOutputTokens = 0
|
|
relayInfo.RetryIndex = retryParam.GetRetry()
|
|
channel, channelErr := getChannel(c, relayInfo, retryParam)
|
|
if channelErr != nil {
|
|
logger.LogError(c, channelErr.Error())
|
|
newAPIError = channelErr
|
|
break
|
|
}
|
|
service.AppendUsedChannel(c, channel.Id)
|
|
if billingErr := service.PrepareTieredBillingForSelectedGroup(c, relayInfo); billingErr != nil {
|
|
newAPIError = billingErr
|
|
break
|
|
}
|
|
|
|
bodyStorage, bodyErr := common.GetBodyStorage(c)
|
|
if bodyErr != nil {
|
|
// Ensure consistent 413 for oversized bodies even when error occurs later (e.g., retry path)
|
|
if common.IsRequestBodyTooLargeError(bodyErr) || errors.Is(bodyErr, common.ErrRequestBodyTooLarge) {
|
|
newAPIError = types.NewErrorWithStatusCode(bodyErr, types.ErrorCodeReadRequestBodyFailed, http.StatusRequestEntityTooLarge, types.ErrOptionWithSkipRetry())
|
|
} else {
|
|
newAPIError = types.NewErrorWithStatusCode(bodyErr, types.ErrorCodeReadRequestBodyFailed, http.StatusBadRequest, types.ErrOptionWithSkipRetry())
|
|
}
|
|
break
|
|
}
|
|
c.Request.Body = io.NopCloser(bodyStorage)
|
|
|
|
// The request plugins' reply checks read the answer this attempt
|
|
// writes; without a reply instance nothing changes.
|
|
finishReply := requestplugin.Reply(c, relayFormat, relayInfo)
|
|
switch relayFormat {
|
|
case types.RelayFormatOpenAIRealtime:
|
|
newAPIError = relay.WssHelper(c, relayInfo)
|
|
case types.RelayFormatClaude:
|
|
newAPIError = relay.ClaudeHelper(c, relayInfo)
|
|
case types.RelayFormatGemini:
|
|
newAPIError = geminiRelayHandler(c, relayInfo)
|
|
default:
|
|
newAPIError = relayHandler(c, relayInfo)
|
|
}
|
|
finishReply()
|
|
|
|
if newAPIError == nil {
|
|
service.MarkRequestPolicySuccess(c, relayInfo.StreamStatus)
|
|
relayInfo.LastError = nil
|
|
return
|
|
}
|
|
|
|
newAPIError = service.NormalizeViolationFeeError(newAPIError)
|
|
relayInfo.LastError = newAPIError
|
|
|
|
decision := service.DecideRelayRetry(c, newAPIError, common.RetryTimes-retryParam.GetRetry())
|
|
service.RecordPolicyFailure(c, channel.Id, newAPIError, decision)
|
|
processChannelError(c, *types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey, common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()), newAPIError, relayInfo)
|
|
|
|
if decision.Action != "retry" {
|
|
break
|
|
}
|
|
}
|
|
|
|
useChannel := c.GetStringSlice("use_channel")
|
|
if len(useChannel) > 1 {
|
|
retryLogStr := common.LogText("retry: %s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
|
|
logger.LogInfo(c, retryLogStr)
|
|
}
|
|
}
|
|
|
|
func writeRelayStreamError(c *gin.Context, format types.RelayFormat, apiError *types.NewAPIError) {
|
|
// 心跳提交响应头后,最终错误继续通过同一个流返回。
|
|
status, _ := common.GetContextKeyType[*relaycommon.StreamStatus](c, constant.ContextKeyResponseStreamStatus)
|
|
if status == nil {
|
|
status = relaycommon.NewStreamStatus()
|
|
common.SetContextKey(c, constant.ContextKeyResponseStreamStatus, status)
|
|
}
|
|
status.MarkFailed(string(apiError.GetErrorCode()), string(apiError.GetErrorType()), apiError.StatusCode)
|
|
upstreamURLs := common.UpstreamAddresses(c)
|
|
if format == types.RelayFormatClaude {
|
|
_ = helper.ClaudeData(c, dto.ClaudeResponse{Type: "error", Error: apiError.ToClaudeError(upstreamURLs...)})
|
|
return
|
|
}
|
|
_ = helper.ObjectData(c, gin.H{"error": apiError.ToOpenAIError(upstreamURLs...)})
|
|
helper.Done(c)
|
|
}
|
|
|
|
// CountClaudeTokens implements Anthropic's token-counting utility endpoint.
|
|
// It deliberately skips upstream generation and billing; callers use this
|
|
// endpoint to size prompts before creating a Message.
|
|
func CountClaudeTokens(c *gin.Context) {
|
|
request, err := helper.GetAndValidateClaudeRequest(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"type": "error",
|
|
"error": gin.H{
|
|
"type": "invalid_request_error",
|
|
"message": common.MessageWithRequestId(err.Error(), c.GetString(common.RequestIdKey)),
|
|
},
|
|
})
|
|
return
|
|
}
|
|
|
|
info := relaycommon.GenRelayInfoClaude(c, request)
|
|
inputTokens, err := service.CountRequestToken(c, request.GetTokenCountMeta(), info)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"type": "error",
|
|
"error": gin.H{
|
|
"type": "api_error",
|
|
"message": common.MessageWithRequestId(err.Error(), c.GetString(common.RequestIdKey)),
|
|
},
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"input_tokens": inputTokens})
|
|
}
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
Subprotocols: []string{"realtime", "responses"}, // WS 握手支持的协议,如果有使用 Sec-WebSocket-Protocol,则必须在此声明对应的 Protocol
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
return true // 允许跨域
|
|
},
|
|
}
|
|
|
|
func getChannel(c *gin.Context, info *relaycommon.RelayInfo, retryParam *service.RetryParam) (*model.Channel, *types.NewAPIError) {
|
|
if info.ChannelMeta == nil {
|
|
autoBan := c.GetBool("auto_ban")
|
|
autoBanInt := 1
|
|
if !autoBan {
|
|
autoBanInt = 0
|
|
}
|
|
channel := &model.Channel{
|
|
Id: c.GetInt("channel_id"),
|
|
Type: c.GetInt("channel_type"),
|
|
Name: c.GetString("channel_name"),
|
|
AutoBan: &autoBanInt,
|
|
}
|
|
service.RequestPolicy(c).BeginAttempt(channel, info.UsingGroup)
|
|
return channel, nil
|
|
}
|
|
channel, selectGroup, err := service.CacheGetRandomSatisfiedChannel(retryParam)
|
|
if err != nil {
|
|
return nil, types.NewError(errors.New(i18n.T(c, i18n.MsgRelayRetryGetChannelFailed, map[string]any{"Group": selectGroup, "Model": info.OriginModelName, "Error": err.Error()})), types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
|
|
}
|
|
if channel == nil {
|
|
return nil, types.NewError(errors.New(i18n.T(c, i18n.MsgRelayRetryNoAvailableChannel, map[string]any{"Group": selectGroup, "Model": info.OriginModelName})), types.ErrorCodeGetChannelFailed, types.ErrOptionWithSkipRetry())
|
|
}
|
|
|
|
info.PriceData.GroupRatioInfo = helper.HandleGroupRatio(c, info)
|
|
|
|
service.RequestPolicy(c).BeginAttempt(channel, selectGroup)
|
|
newAPIError := middleware.SetupContextForSelectedChannel(c, channel, info.OriginModelName)
|
|
if newAPIError != nil {
|
|
return nil, newAPIError
|
|
}
|
|
return channel, nil
|
|
}
|
|
|
|
func processChannelError(c *gin.Context, channelError types.ChannelError, err *types.NewAPIError, relayInfo *relaycommon.RelayInfo) {
|
|
service.ProcessChannelError(c, channelError, err, relayInfo)
|
|
}
|
|
|
|
func RelayMidjourney(c *gin.Context) {
|
|
policy := service.RequestPolicy(c)
|
|
defer func() {
|
|
if policy.Attempts > 0 && !policy.Successful {
|
|
service.RecordRequestPolicyTermination(c, types.NewErrorWithStatusCode(errors.New("Midjourney submission failed"), types.ErrorCodeBadResponseStatusCode, http.StatusBadGateway, types.ErrOptionWithSkipRetry()))
|
|
}
|
|
}()
|
|
var relayInfo *relaycommon.RelayInfo
|
|
var reportErr *types.NewAPIError
|
|
// Registered first so that it runs last: the request plugins' report
|
|
// hooks receive a submission once the client has the response.
|
|
defer func() {
|
|
requestplugin.Report(c, types.RelayFormatMjProxy, relayInfo, nil, reportErr)
|
|
}()
|
|
relayInfo, err := relaycommon.GenRelayInfo(c, types.RelayFormatMjProxy, nil, nil)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"description": fmt.Sprintf("failed to generate relay info: %s", err.Error()),
|
|
"type": "upstream_error",
|
|
"code": 4,
|
|
})
|
|
return
|
|
}
|
|
|
|
var mjErr *taskdto.MidjourneyResponse
|
|
switch relayInfo.RelayMode {
|
|
case relayconstant.RelayModeMidjourneyNotify:
|
|
mjErr = relay.RelayMidjourneyNotify(c)
|
|
case relayconstant.RelayModeMidjourneyTaskFetch, relayconstant.RelayModeMidjourneyTaskFetchByCondition:
|
|
mjErr = relay.RelayMidjourneyTask(c, relayInfo.RelayMode)
|
|
case relayconstant.RelayModeMidjourneyTaskImageSeed:
|
|
mjErr = relay.RelayMidjourneyTaskImageSeed(c)
|
|
default:
|
|
if reportErr = midjourneyRequestPlugins(c, relayInfo); reportErr != nil {
|
|
code := constant.MjRequestError
|
|
if reportErr.StatusCode >= http.StatusInternalServerError {
|
|
code = constant.MjErrorUnknown
|
|
}
|
|
c.JSON(reportErr.StatusCode, gin.H{
|
|
"description": common.MessageWithRequestId(reportErr.Error(), c.GetString(common.RequestIdKey)),
|
|
"type": string(reportErr.GetErrorCode()),
|
|
"code": code,
|
|
})
|
|
return
|
|
}
|
|
if relayInfo.RelayMode == relayconstant.RelayModeSwapFace {
|
|
mjErr = relay.RelaySwapFace(c, relayInfo)
|
|
} else {
|
|
mjErr = relay.RelayMidjourneySubmit(c, relayInfo)
|
|
}
|
|
}
|
|
//err = relayMidjourneySubmit(c, relayMode)
|
|
log.Println(mjErr)
|
|
if mjErr != nil {
|
|
policy.Successful = false
|
|
statusCode := http.StatusBadRequest
|
|
if mjErr.Code == 30 {
|
|
mjErr.Result = i18n.T(c, i18n.MsgRelayGroupSaturated)
|
|
statusCode = http.StatusTooManyRequests
|
|
}
|
|
reportErr = types.NewErrorWithStatusCode(fmt.Errorf("%s %s", mjErr.Description, mjErr.Result), types.ErrorCodeBadResponseStatusCode, statusCode)
|
|
c.JSON(statusCode, gin.H{
|
|
"description": fmt.Sprintf("%s %s", mjErr.Description, mjErr.Result),
|
|
"type": "upstream_error",
|
|
"code": mjErr.Code,
|
|
})
|
|
channelId := c.GetInt("channel_id")
|
|
logger.LogError(c, common.LogText("relay error (channel #%d, status code %d): %s", channelId, statusCode, fmt.Sprintf("%s %s", mjErr.Description, mjErr.Result)))
|
|
}
|
|
}
|
|
|
|
// midjourneyRequestPlugins runs the request plugins of a Midjourney
|
|
// submission on its client body before the body is read: the request's
|
|
// ingress, then the late ingress of its channel. A Midjourney submission
|
|
// has one attempt, so the late record stays with the request's. A stop is
|
|
// written to the error log.
|
|
func midjourneyRequestPlugins(c *gin.Context, relayInfo *relaycommon.RelayInfo) *types.NewAPIError {
|
|
_, steps, record, apiErr := requestplugin.SubmitIngress(c, types.RelayFormatMjProxy, common.GetContextKeyString(c, constant.ContextKeyOriginalModel))
|
|
relayInfo.SetRequestPluginIngress(steps, record)
|
|
channelID := 0
|
|
if apiErr == nil {
|
|
relayInfo.InitChannelMeta(c)
|
|
channelID = relayInfo.ChannelId
|
|
_, apiErr = requestplugin.TaskAttempt(c, relayInfo)
|
|
relayInfo.SetRequestPluginIngress(relayInfo.RequestPluginSteps, relayInfo.RequestPluginRecord)
|
|
}
|
|
if apiErr != nil {
|
|
service.RecordRelayErrorLog(c, channelID, apiErr, relayInfo)
|
|
}
|
|
return apiErr
|
|
}
|
|
|
|
func RelayNotImplemented(c *gin.Context) {
|
|
err := types.OpenAIError{
|
|
Message: "API not implemented",
|
|
Type: "new_api_error",
|
|
Param: "",
|
|
Code: "api_not_implemented",
|
|
}
|
|
c.JSON(http.StatusNotImplemented, gin.H{
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
func RelayNotFound(c *gin.Context) {
|
|
// The web fallback may already have applied static-asset cache headers.
|
|
// A missing API or asset can appear after an upgrade; never cache its 404.
|
|
c.Header("Cache-Control", "no-store, no-cache, must-revalidate, private, max-age=0")
|
|
c.Header("Pragma", "no-cache")
|
|
c.Header("Expires", "0")
|
|
err := types.OpenAIError{
|
|
Message: fmt.Sprintf("Invalid URL (%s %s)", c.Request.Method, c.Request.URL.Path),
|
|
Type: "invalid_request_error",
|
|
Param: "",
|
|
Code: "",
|
|
}
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": err,
|
|
})
|
|
}
|
|
|
|
// RelayTaskPluginEndpoint keeps unclaimed shared-endpoint traffic on its
|
|
// existing handler while claimed requests enter the generation-pinned
|
|
// host-owned protocol bridge.
|
|
func RelayTaskPluginEndpoint(c *gin.Context, fallback gin.HandlerFunc) {
|
|
pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedEndpoint)
|
|
if !exists {
|
|
fallback(c)
|
|
return
|
|
}
|
|
pinned, ok := pinnedValue.(pluginruntime.PinnedEndpoint)
|
|
if !ok || pinned.Plugin == nil || pinned.Generation == nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": gin.H{
|
|
"message": "Task protocol request failed",
|
|
"type": "new_api_error",
|
|
"code": "task_protocol_error",
|
|
},
|
|
})
|
|
return
|
|
}
|
|
switch pinned.Protocol {
|
|
case "openai_responses":
|
|
serveTaskPluginProtocol(c, pinned, defaultPluginProtocolBridgeDeps())
|
|
case pluginruntime.ProtocolOpenAIImage:
|
|
serveTaskPluginImageProtocol(c, pinned, defaultPluginProtocolBridgeDeps())
|
|
default:
|
|
fallback(c)
|
|
}
|
|
}
|
|
|
|
func RelayTaskFetch(c *gin.Context) {
|
|
relayInfo, err := relaycommon.GenRelayInfo(c, types.RelayFormatTask, nil, nil)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, &taskdto.TaskError{
|
|
Code: "gen_relay_info_failed",
|
|
Message: err.Error(),
|
|
StatusCode: http.StatusInternalServerError,
|
|
})
|
|
return
|
|
}
|
|
if taskErr := relay.RelayTaskFetch(c, relayInfo.RelayMode); taskErr != nil {
|
|
respondTaskError(c, taskErr)
|
|
}
|
|
}
|
|
|
|
type taskSubmissionOutcome struct {
|
|
Result *relay.TaskSubmitResult
|
|
Task *model.Task
|
|
RelayInfo *relaycommon.RelayInfo
|
|
}
|
|
|
|
func RelayTask(c *gin.Context) {
|
|
var relayInfo *relaycommon.RelayInfo
|
|
var taskErr *taskdto.TaskError
|
|
// Registered first so that it runs last: the request plugins' report
|
|
// hooks receive the submission once the client has the response.
|
|
defer func() {
|
|
requestplugin.Report(c, types.RelayFormatTask, relayInfo, nil, taskReportError(taskErr))
|
|
}()
|
|
relayInfo, err := relaycommon.GenRelayInfo(c, types.RelayFormatTask, nil, nil)
|
|
if err != nil {
|
|
respondTaskSubmissionError(c, &taskdto.TaskError{
|
|
Code: "gen_relay_info_failed",
|
|
Message: err.Error(),
|
|
StatusCode: http.StatusInternalServerError,
|
|
})
|
|
return
|
|
}
|
|
if action := c.GetString("task_action"); action != "" {
|
|
relayInfo.Action = action
|
|
}
|
|
|
|
if taskErr = relay.ResolveOriginTask(c, relayInfo); taskErr != nil {
|
|
respondTaskSubmissionError(c, taskErr)
|
|
return
|
|
}
|
|
if taskErr = relay.ApplyOriginTaskAffinity(c, relayInfo); taskErr != nil {
|
|
respondTaskSubmissionError(c, taskErr)
|
|
return
|
|
}
|
|
|
|
var outcome *taskSubmissionOutcome
|
|
outcome, taskErr = executeTaskSubmission(c, relayInfo)
|
|
if taskErr != nil {
|
|
respondTaskSubmissionError(c, taskErr)
|
|
return
|
|
}
|
|
presentTaskSubmission(c, outcome)
|
|
}
|
|
|
|
// taskReportError is the error a task submission's client received, as the
|
|
// request plugins' report record shows it; nil when it succeeded.
|
|
func taskReportError(taskErr *taskdto.TaskError) *types.NewAPIError {
|
|
if taskErr == nil {
|
|
return nil
|
|
}
|
|
err := taskErr.Error
|
|
if err == nil {
|
|
err = errors.New(taskErr.Message)
|
|
}
|
|
return types.NewErrorWithStatusCode(err, types.ErrorCode(taskErr.Code), taskErr.StatusCode)
|
|
}
|
|
|
|
// executeTaskSubmission owns the retry, billing, and persistence lifecycle.
|
|
// It deliberately performs no client response writes so JSON and protocol
|
|
// presenters share the same durable task barrier. Its cancellation semantics
|
|
// come from c.Request.Context: native task endpoints use the client context,
|
|
// while the Responses bridge supplies an independently bounded context.
|
|
func executeTaskSubmission(c *gin.Context, relayInfo *relaycommon.RelayInfo) (*taskSubmissionOutcome, *taskdto.TaskError) {
|
|
return executeTaskSubmissionWith(c, relayInfo, relay.RelayTaskSubmit)
|
|
}
|
|
|
|
type taskSubmitAttempt func(*gin.Context, *relaycommon.RelayInfo) (*relay.TaskSubmitResult, *taskdto.TaskError)
|
|
|
|
func executeTaskSubmissionWith(
|
|
c *gin.Context,
|
|
relayInfo *relaycommon.RelayInfo,
|
|
submit taskSubmitAttempt,
|
|
) (*taskSubmissionOutcome, *taskdto.TaskError) {
|
|
policy := service.RequestPolicy(c)
|
|
diagnostics := newTaskPluginSubmitDiagnostics(c)
|
|
diagnostics.start(relayInfo)
|
|
var result *relay.TaskSubmitResult
|
|
var taskErr *taskdto.TaskError
|
|
durable := false
|
|
stage := "start"
|
|
defer func() {
|
|
// The client of a relayed event stream already holds the vendor's
|
|
// work, so the submission is billed instead of refunded.
|
|
if !durable && relayInfo.Billing != nil && !c.Writer.Written() {
|
|
diagnostics.refund(stage)
|
|
relayInfo.Billing.Refund(c)
|
|
}
|
|
}()
|
|
stage = "before_attempt"
|
|
if requestErr := c.Request.Context().Err(); requestErr != nil {
|
|
diagnostics.cancelled("before_attempt", 0)
|
|
return nil, service.TaskErrorWrapperLocal(requestErr, "request_cancelled", http.StatusRequestTimeout)
|
|
}
|
|
|
|
// The request plugins' ingress runs once, on the client body, before the
|
|
// submission is validated, priced or reserved; a changed body is decoded
|
|
// again.
|
|
stage = "request_plugins"
|
|
var format types.RelayFormat = types.RelayFormatTask
|
|
if pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedEndpoint); exists {
|
|
if pinned, ok := pinnedValue.(pluginruntime.PinnedEndpoint); ok && (pinned.Protocol == string(types.RelayFormatOpenAIResponses) || pinned.Protocol == pluginruntime.ProtocolOpenAIImage) {
|
|
format = types.RelayFormat(pinned.Protocol)
|
|
}
|
|
}
|
|
ingressModel := relayInfo.OriginModelName
|
|
if ingressModel == "" {
|
|
ingressModel = common.GetContextKeyString(c, constant.ContextKeyOriginalModel)
|
|
}
|
|
changed, ingressSteps, ingressRecord, ingressErr := requestplugin.SubmitIngress(c, format, ingressModel)
|
|
relayInfo.SetRequestPluginIngress(ingressSteps, ingressRecord)
|
|
if ingressErr != nil {
|
|
// The submission reached no channel and reserved nothing; its error
|
|
// log shows what the plugins did.
|
|
service.RecordRelayErrorLog(c, 0, ingressErr, relayInfo)
|
|
taskErr = requestplugin.TaskError(ingressErr)
|
|
diagnostics.failed(stage, "request_plugin", taskErr, false)
|
|
return nil, taskErr
|
|
}
|
|
if changed {
|
|
if taskErr = middleware.RefreshTaskPluginRequest(c); taskErr != nil {
|
|
diagnostics.failed(stage, "request_decode", taskErr, false)
|
|
return nil, taskErr
|
|
}
|
|
}
|
|
|
|
retryParam := &service.RetryParam{
|
|
Ctx: c,
|
|
TokenGroup: relayInfo.TokenGroup,
|
|
ModelName: relayInfo.OriginModelName,
|
|
RequestPath: c.Request.URL.Path,
|
|
Retry: common.GetPointer(0),
|
|
}
|
|
|
|
for ; retryParam.GetRetry() <= common.RetryTimes; retryParam.IncreaseRetry() {
|
|
stage = "select_channel"
|
|
if requestErr := c.Request.Context().Err(); requestErr != nil {
|
|
diagnostics.cancelled("before_attempt", retryParam.GetRetry()+1)
|
|
taskErr = service.TaskErrorWrapperLocal(requestErr, "request_cancelled", http.StatusRequestTimeout)
|
|
break
|
|
}
|
|
var channel *model.Channel
|
|
|
|
if lockedCh, ok := relayInfo.LockedChannel.(*model.Channel); ok && lockedCh != nil {
|
|
channel = lockedCh
|
|
policy.BeginAttempt(channel, relayInfo.UsingGroup)
|
|
if retryParam.GetRetry() > 0 {
|
|
if setupErr := middleware.SetupContextForSelectedChannel(c, channel, relayInfo.OriginModelName); setupErr != nil {
|
|
taskErr = service.TaskErrorWrapperLocal(setupErr.Err, "setup_locked_channel_failed", http.StatusInternalServerError)
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
var channelErr *types.NewAPIError
|
|
channel, channelErr = getChannel(c, relayInfo, retryParam)
|
|
if channelErr != nil {
|
|
logger.LogError(c, channelErr.Error())
|
|
taskErr = service.TaskErrorWrapperLocal(channelErr.Err, "get_channel_failed", channelErr.StatusCode)
|
|
break
|
|
}
|
|
}
|
|
diagnostics.attempt(retryParam.GetRetry()+1, channel, relayInfo.LockedChannel != nil)
|
|
|
|
service.AppendUsedChannel(c, channel.Id)
|
|
bodyStorage, bodyErr := common.GetBodyStorage(c)
|
|
if bodyErr != nil {
|
|
stage = "read_body"
|
|
if common.IsRequestBodyTooLargeError(bodyErr) || errors.Is(bodyErr, common.ErrRequestBodyTooLarge) {
|
|
taskErr = service.TaskErrorWrapperLocal(bodyErr, "read_request_body_failed", http.StatusRequestEntityTooLarge)
|
|
} else {
|
|
taskErr = service.TaskErrorWrapperLocal(bodyErr, "read_request_body_failed", http.StatusBadRequest)
|
|
}
|
|
break
|
|
}
|
|
c.Request.Body = io.NopCloser(bodyStorage)
|
|
|
|
stage = "submit"
|
|
result, taskErr = submit(c, relayInfo)
|
|
if requestErr := c.Request.Context().Err(); requestErr != nil {
|
|
diagnostics.cancelled("after_submit", retryParam.GetRetry()+1)
|
|
taskErr = service.TaskErrorWrapperLocal(requestErr, "request_cancelled", http.StatusRequestTimeout)
|
|
break
|
|
}
|
|
if taskErr == nil {
|
|
diagnostics.attemptSucceeded(retryParam.GetRetry()+1, result)
|
|
break
|
|
}
|
|
|
|
taskAPIError := taskSubmissionAPIError(taskErr)
|
|
relayInfo.LastError = taskAPIError
|
|
decision := decideTaskRetry(c, taskErr, common.RetryTimes-retryParam.GetRetry())
|
|
service.RecordPolicyFailure(c, channel.Id, taskAPIError, decision)
|
|
if !taskErr.LocalError {
|
|
processChannelError(c,
|
|
*types.NewChannelError(channel.Id, channel.Type, channel.Name, channel.ChannelInfo.IsMultiKey,
|
|
common.GetContextKeyString(c, constant.ContextKeyChannelKey), channel.GetAutoBan()),
|
|
taskAPIError,
|
|
relayInfo)
|
|
} else if requestplugin.Stopped(taskErr) {
|
|
// A request plugin stopped the attempt; its error log shows what
|
|
// the plugins did.
|
|
service.RecordRelayErrorLog(c, channel.Id, taskReportError(taskErr), relayInfo)
|
|
}
|
|
|
|
willRetry := decision.Action == "retry"
|
|
diagnostics.attemptFailed(retryParam.GetRetry()+1, channel, taskErr, willRetry)
|
|
if !willRetry {
|
|
break
|
|
}
|
|
}
|
|
|
|
useChannel := c.GetStringSlice("use_channel")
|
|
if len(useChannel) > 1 {
|
|
retryLogStr := common.LogText("retry: %s", strings.Trim(strings.Join(strings.Fields(fmt.Sprint(useChannel)), "->"), "[]"))
|
|
logger.LogInfo(c, retryLogStr)
|
|
}
|
|
|
|
if taskErr != nil {
|
|
diagnostics.failed(stage, "task_error", taskErr, false)
|
|
return nil, taskErr
|
|
}
|
|
if result == nil {
|
|
taskErr = service.TaskErrorWrapperLocal(errors.New("task submission returned no result"), "task_submit_failed", http.StatusInternalServerError)
|
|
diagnostics.failed("submit", "missing_result", taskErr, false)
|
|
return nil, taskErr
|
|
}
|
|
if requestErr := c.Request.Context().Err(); requestErr != nil {
|
|
diagnostics.cancelled("before_reserve", retryParam.GetRetry()+1)
|
|
return nil, service.TaskErrorWrapperLocal(requestErr, "request_cancelled", http.StatusRequestTimeout)
|
|
}
|
|
|
|
// Reserve any submit-time upward billing adjustment before persistence.
|
|
// This keeps insertion failures fully refundable while ensuring settlement
|
|
// after the barrier normally has a zero positive delta. A relayed event
|
|
// stream is billed even when this reserve fails: settlement then charges
|
|
// past the available quota, as it does for any usage above its reservation.
|
|
if relayInfo.Billing != nil {
|
|
stage = "reserve"
|
|
diagnostics.reserve("reserve_start", result.Quota)
|
|
if reserveErr := relayInfo.Billing.Reserve(result.Quota); reserveErr != nil {
|
|
common.SysError(common.LogText("reserve adjusted task billing error: %s", reserveErr.Error()))
|
|
if !c.Writer.Written() {
|
|
taskErr = service.TaskErrorWrapperLocal(errors.New("insufficient quota for adjusted task cost"), string(types.ErrorCodeInsufficientUserQuota), http.StatusForbidden)
|
|
diagnostics.failed("reserve", "insufficient_quota", taskErr, false)
|
|
return nil, taskErr
|
|
}
|
|
} else {
|
|
diagnostics.reserve("reserve_complete", result.Quota)
|
|
}
|
|
}
|
|
if requestErr := c.Request.Context().Err(); requestErr != nil {
|
|
diagnostics.cancelled("before_insert", retryParam.GetRetry()+1)
|
|
return nil, service.TaskErrorWrapperLocal(requestErr, "request_cancelled", http.StatusRequestTimeout)
|
|
}
|
|
|
|
stage = "insert"
|
|
task := model.InitTask(result.Platform, relayInfo)
|
|
task.PrivateData.Execution = service.TaskExecutionSnapshotFromContext(c)
|
|
task.PrivateData.UpstreamTaskID = result.UpstreamTaskID
|
|
task.PrivateData.BillingSource = relayInfo.BillingSource
|
|
task.PrivateData.SubscriptionId = relayInfo.SubscriptionId
|
|
task.PrivateData.TokenId = relayInfo.TokenId
|
|
task.PrivateData.NodeName = common.NodeName
|
|
task.PrivateData.BillingContext = &model.TaskBillingContext{
|
|
ModelPrice: relayInfo.PriceData.ModelPrice,
|
|
GroupRatio: relayInfo.PriceData.GroupRatioInfo.GroupRatio,
|
|
ModelRatio: relayInfo.PriceData.ModelRatio,
|
|
OtherRatios: relayInfo.PriceData.OtherRatios(),
|
|
OriginModelName: relayInfo.OriginModelName,
|
|
PerCallBilling: common.StringsContains(constant.TaskPricePatches, relayInfo.OriginModelName) || relayInfo.PriceData.UsePrice,
|
|
TieredSnapshot: relayInfo.TieredBillingSnapshot,
|
|
}
|
|
task.Quota = result.Quota
|
|
task.Data = result.TaskData
|
|
if len(result.PluginState) > 0 {
|
|
task.PrivateData.PluginState = result.PluginState
|
|
}
|
|
// A request plugin instance that may report the task gets it once, at
|
|
// its final state.
|
|
task.PrivateData.RequestPlugins = requestplugin.TaskReport(c, relayInfo)
|
|
task.Action = relayInfo.Action
|
|
if immediate := result.Immediate; immediate != nil {
|
|
task.Status = model.TaskStatus(immediate.Status)
|
|
task.Progress = immediate.Progress
|
|
if immediate.Status == model.TaskStatusSuccess || immediate.Status == model.TaskStatusFailure {
|
|
task.FinishTime = time.Now().Unix()
|
|
}
|
|
if immediate.Status == model.TaskStatusFailure {
|
|
task.FailReason = immediate.Reason
|
|
}
|
|
if immediate.Url != "" {
|
|
task.PrivateData.ResultURL = immediate.Url
|
|
} else if immediate.Status == model.TaskStatusSuccess {
|
|
task.PrivateData.ResultURL = taskcommon.BuildProxyURL(task.TaskID)
|
|
}
|
|
}
|
|
// A native submit route may declare retainResult: false. It applies only
|
|
// to immediate terminal results: the client receives the complete response
|
|
// once, the upstream snapshot is never persisted, and the task is not
|
|
// retrievable afterwards. An asynchronous result on such a route keeps its
|
|
// snapshot because polling and retrieval need it. The OpenAI Images
|
|
// protocol delivers its images inline in the same HTTP response, so its
|
|
// immediate results follow the same rule; an asynchronous image task is
|
|
// expected there and is polled inside the request.
|
|
var insertOmits []string
|
|
immediateTerminal := result.Immediate != nil && (result.Immediate.Status == model.TaskStatusSuccess || result.Immediate.Status == model.TaskStatusFailure)
|
|
if pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedRoute); exists {
|
|
pinned, ok := pinnedValue.(pluginruntime.PinnedRoute)
|
|
if ok && pinned.Route.RetainResult != nil && !*pinned.Route.RetainResult {
|
|
if immediateTerminal {
|
|
task.PrivateData.ResultDiscarded = true
|
|
insertOmits = append(insertOmits, "data")
|
|
} else {
|
|
logger.LogWarn(c, common.LogText("task plugin route %s %s declares retainResult: false but returned an asynchronous result; retaining task %s", pinned.Route.Method, pinned.Route.Path, task.TaskID))
|
|
}
|
|
}
|
|
}
|
|
if pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedEndpoint); exists && immediateTerminal {
|
|
if pinned, ok := pinnedValue.(pluginruntime.PinnedEndpoint); ok && pinned.Protocol == pluginruntime.ProtocolOpenAIImage {
|
|
task.PrivateData.ResultDiscarded = true
|
|
insertOmits = append(insertOmits, "data")
|
|
}
|
|
}
|
|
diagnostics.insertStart(task)
|
|
if insertErr := task.InsertWithContext(c.Request.Context(), insertOmits...); insertErr != nil {
|
|
common.SysError(common.LogText("insert task error: %s", insertErr.Error()))
|
|
taskErr = service.TaskErrorWrapperLocal(errors.New("failed to persist task"), "task_insert_failed", http.StatusInternalServerError)
|
|
diagnostics.failed("insert", "database_error", taskErr, false)
|
|
// The client of a relayed event stream already holds the vendor's
|
|
// work, so it is settled and logged without a task row.
|
|
if c.Writer.Written() {
|
|
if settleErr := service.SettleBilling(c, relayInfo, result.Quota); settleErr != nil {
|
|
common.SysError(common.LogText("settle task billing error: %s", settleErr.Error()))
|
|
return nil, taskErr
|
|
}
|
|
service.LogTaskConsumption(c, relayInfo, task)
|
|
}
|
|
return nil, taskErr
|
|
}
|
|
durable = true
|
|
requestplugin.TaskCreated(c)
|
|
if immediateTerminal {
|
|
// Reported once settled, whatever the settlement did.
|
|
defer requestplugin.ReportTask(task)
|
|
}
|
|
stage = "settle"
|
|
diagnostics.durable(task)
|
|
diagnostics.settleStart(task, result.Quota)
|
|
|
|
if settleErr := service.SettleBilling(c, relayInfo, result.Quota); settleErr != nil {
|
|
common.SysError(common.LogText("settle task billing error: %s", settleErr.Error()))
|
|
taskErr = service.TaskErrorWrapperLocal(errors.New("failed to settle task billing"), "task_billing_settlement_failed", http.StatusInternalServerError)
|
|
diagnostics.failed("settle", "billing_error", taskErr, true)
|
|
return nil, taskErr
|
|
}
|
|
perfmetrics.RecordImmediateTaskResult(relayInfo, result.Immediate)
|
|
if task.Status != model.TaskStatusFailure {
|
|
service.MarkRequestPolicySuccess(c, nil)
|
|
} else {
|
|
policy.AddEvent(service.PolicyEvent{Decision: service.PolicyDecision{Action: "stop", Reason: "task_failed", Source: "upstream"}})
|
|
}
|
|
service.LogTaskConsumption(c, relayInfo, task)
|
|
diagnostics.complete(task, result.Quota)
|
|
|
|
return &taskSubmissionOutcome{Result: result, Task: task, RelayInfo: relayInfo}, nil
|
|
}
|
|
|
|
func presentTaskSubmission(c *gin.Context, outcome *taskSubmissionOutcome) {
|
|
diagnostics := newTaskPluginSubmitDiagnostics(c)
|
|
// A native route that relayed the vendor's event stream has answered.
|
|
if c.Writer.Written() {
|
|
diagnostics.present(outcome.Task, "native_stream")
|
|
return
|
|
}
|
|
otherRatios := outcome.RelayInfo.PriceData.OtherRatios()
|
|
if otherRatios == nil {
|
|
otherRatios = map[string]float64{}
|
|
}
|
|
if ratiosJSON, err := common.Marshal(otherRatios); err == nil {
|
|
c.Header("X-New-Api-Other-Ratios", string(ratiosJSON))
|
|
}
|
|
if pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedRoute); exists {
|
|
if pinned, ok := pinnedValue.(pluginruntime.PinnedRoute); ok && pinned.Plugin != nil && pinned.Route.Render != "" {
|
|
view, err := service.BuildTaskPluginView(outcome.Task)
|
|
requestValue, _ := c.Get(pluginruntime.ContextKeyRouteRequest)
|
|
requestContext, _ := requestValue.(pluginruntime.RouteRequestContext)
|
|
if err == nil {
|
|
viewValue, valueErr := taskPluginProtocolJSONValue(view)
|
|
if valueErr == nil {
|
|
if body, callErr := pinned.Plugin.Engine.CallPath(c.Request.Context(), "native", []string{pinned.Route.Render}, requestContext.JSValue(), viewValue); callErr == nil {
|
|
if replyErr := middleware.WriteNativeReply(c, http.StatusOK, body); replyErr == nil {
|
|
diagnostics.present(outcome.Task, "native_presenter")
|
|
return
|
|
} else {
|
|
logger.LogError(c, common.LogText("task plugin native submit presenter failed: %s", replyErr.Error()))
|
|
}
|
|
} else {
|
|
logger.LogError(c, common.LogText("task plugin native submit presenter failed: %s", callErr.Error()))
|
|
}
|
|
} else {
|
|
logger.LogError(c, common.LogText("encode task plugin native submit view failed: %s", valueErr.Error()))
|
|
}
|
|
} else {
|
|
logger.LogError(c, common.LogText("build task plugin native submit view failed: %s", err.Error()))
|
|
}
|
|
}
|
|
}
|
|
if pinnedValue, exists := c.Get(pluginruntime.ContextKeyPinnedEndpoint); exists {
|
|
if pinned, ok := pinnedValue.(pluginruntime.PinnedEndpoint); ok && pinned.Protocol == "openai_video" && pinned.Operation.Name == "create" {
|
|
diagnostics.present(outcome.Task, "openai_video_create")
|
|
c.JSON(http.StatusOK, outcome.Task.ToOpenAIVideo())
|
|
return
|
|
}
|
|
}
|
|
createdAt := outcome.Task.CreatedAt
|
|
if createdAt == 0 {
|
|
createdAt = outcome.Task.SubmitTime
|
|
}
|
|
diagnostics.present(outcome.Task, "host_fallback")
|
|
c.JSON(http.StatusOK, map[string]any{
|
|
"id": outcome.Task.TaskID,
|
|
"task_id": outcome.Task.TaskID,
|
|
"status": "queued",
|
|
"model": outcome.RelayInfo.OriginModelName,
|
|
"created_at": createdAt,
|
|
})
|
|
}
|
|
|
|
func respondTaskSubmissionError(c *gin.Context, taskErr *taskdto.TaskError) {
|
|
service.RecordRequestPolicyTermination(c, taskSubmissionAPIError(taskErr))
|
|
newTaskPluginSubmitDiagnostics(c).presentError(taskErr)
|
|
// The client of a relayed event stream already has the vendor's events,
|
|
// including any error event; nothing can follow them.
|
|
if c.Writer.Written() {
|
|
return
|
|
}
|
|
if middleware.RespondTaskPluginError(c, taskErr) {
|
|
return
|
|
}
|
|
respondTaskError(c, taskErr)
|
|
}
|
|
|
|
// respondTaskError 统一输出 Task 错误响应(含 429 限流提示改写)
|
|
func respondTaskError(c *gin.Context, taskErr *taskdto.TaskError) {
|
|
if taskErr.StatusCode == http.StatusTooManyRequests {
|
|
taskErr.Message = i18n.T(c, i18n.MsgRelayGroupUpstreamSaturated)
|
|
}
|
|
c.JSON(taskErr.StatusCode, taskErr)
|
|
}
|
|
|
|
// taskSubmissionAPIError adapts a task error for the shared relay error paths.
|
|
// TaskError.Error is nil for many local rejections, so fall back to the message.
|
|
func taskSubmissionAPIError(taskErr *taskdto.TaskError) *types.NewAPIError {
|
|
err := taskErr.Error
|
|
if err == nil {
|
|
err = errors.New(taskErr.Message)
|
|
}
|
|
return types.NewOpenAIError(err, types.ErrorCodeBadResponseStatusCode, taskErr.StatusCode)
|
|
}
|
|
|
|
// decideTaskRetry is the single retry decision for task submissions. The
|
|
// reason is recorded in the request policy decision events of the log details.
|
|
func decideTaskRetry(c *gin.Context, taskErr *taskdto.TaskError, retryTimes int) service.PolicyDecision {
|
|
stop := service.PolicyDecision{Action: "stop", Source: "system"}
|
|
retry := service.PolicyDecision{Action: "retry", Reason: "retry_status_matched", Source: "system"}
|
|
switch {
|
|
case taskErr == nil:
|
|
stop.Reason = "request_completed"
|
|
case taskErr.NoRetry:
|
|
stop.Reason = "task_accepted"
|
|
case requestplugin.Stopped(taskErr):
|
|
stop.Reason, stop.Source = "local_rejection", "plugin"
|
|
case service.ShouldSkipRetryAfterChannelAffinityFailure(c):
|
|
stop.Reason, stop.Source = "strict_session", "session_rule"
|
|
if source := service.RequestPolicy(c).SessionModeSource; source != "" {
|
|
stop.Source = source
|
|
}
|
|
case retryTimes <= 0:
|
|
stop.Reason, stop.Source = "attempt_budget_exhausted", "global"
|
|
case service.GetChannelConstraints(c).SuppressesRetry():
|
|
stop.Reason, stop.Source = "pinned_channel", "channel_constraint"
|
|
case taskErr.StatusCode == http.StatusTooManyRequests, taskErr.StatusCode == 307:
|
|
return retry
|
|
case taskErr.StatusCode/100 == 5:
|
|
// 超时不重试
|
|
if operation_setting.IsAlwaysSkipRetryStatusCode(taskErr.StatusCode) {
|
|
stop.Reason = "system_retry_exclusion"
|
|
break
|
|
}
|
|
return retry
|
|
case taskErr.StatusCode == http.StatusBadRequest, taskErr.StatusCode == 408:
|
|
// azure处理超时不重试
|
|
stop.Reason = "status_not_retryable"
|
|
case taskErr.LocalError:
|
|
stop.Reason = "local_rejection"
|
|
case taskErr.StatusCode/100 == 2:
|
|
stop.Reason = "system_retry_exclusion"
|
|
default:
|
|
return retry
|
|
}
|
|
return stop
|
|
}
|