mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-11-30 19:21:36 +08: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.
1280 lines
45 KiB
Go
1280 lines
45 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"hash/fnv"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/QuantumNous/new-api/dto"
|
|
"github.com/QuantumNous/new-api/logger"
|
|
"github.com/QuantumNous/new-api/model"
|
|
pluginruntime "github.com/QuantumNous/new-api/pkg/jsplugin"
|
|
"github.com/QuantumNous/new-api/relay"
|
|
taskjsplugin "github.com/QuantumNous/new-api/relay/channel/task/jsplugin"
|
|
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/types"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type pluginProtocolBridgeDeps struct {
|
|
submit func(*gin.Context, *relaycommon.RelayInfo) (*taskSubmissionOutcome, *dto.TaskError)
|
|
loadTask func(context.Context, int, constant.TaskPlatform, string) (*model.Task, bool, error)
|
|
now func() time.Time
|
|
admissions *pluginProtocolObservationLimiter
|
|
protocolLimits relay.PluginProtocolLimits
|
|
artifactContentURL func(taskID, artifactKey string) (string, error)
|
|
submissionTimeout time.Duration
|
|
observationTimeout time.Duration
|
|
loadTimeout time.Duration
|
|
tickInterval time.Duration
|
|
tickJitter time.Duration
|
|
heartbeatInterval time.Duration
|
|
admissionTimeout time.Duration
|
|
getByTaskId func(int, string) (*model.Task, bool, error)
|
|
resolvePlugin func(constant.TaskPlatform) (*pluginruntime.LoadedPlugin, *pluginruntime.RoutingGeneration, bool)
|
|
// imagePollInterval, pollTask and downloadImage serve the synchronous
|
|
// OpenAI Images protocol: an asynchronous upstream image task is polled
|
|
// inside the request, and b64_json responses download each image URL.
|
|
imagePollInterval time.Duration
|
|
pollTask func(context.Context, *model.Task) error
|
|
downloadImage func(url string) (mimeType string, base64Data string, err error)
|
|
}
|
|
|
|
// taskPluginImagePollInterval paces the in-request polling of asynchronous
|
|
// image tasks. Legacy DashScope text-to-image jobs finish within tens of
|
|
// seconds, so a short interval keeps the synchronous response prompt.
|
|
const taskPluginImagePollInterval = 3 * time.Second
|
|
|
|
func defaultPluginProtocolBridgeDeps() pluginProtocolBridgeDeps {
|
|
timeout := time.Duration(constant.TaskPluginProtocolTimeoutSeconds) * time.Second
|
|
if timeout <= 0 {
|
|
timeout = 10 * time.Minute
|
|
}
|
|
tick := time.Duration(constant.TaskPluginProtocolTickMilliseconds) * time.Millisecond
|
|
if tick <= 0 {
|
|
tick = 2 * time.Second
|
|
}
|
|
jitter := max(time.Duration(constant.TaskPluginProtocolTickJitterMilliseconds)*time.Millisecond, 0)
|
|
heartbeat := time.Duration(constant.TaskPluginProtocolHeartbeatSeconds) * time.Second
|
|
if heartbeat <= 0 {
|
|
heartbeat = 15 * time.Second
|
|
}
|
|
loadTimeout := 5 * time.Second
|
|
if halfHeartbeat := heartbeat / 2; halfHeartbeat > 0 && halfHeartbeat < loadTimeout {
|
|
loadTimeout = halfHeartbeat
|
|
}
|
|
return pluginProtocolBridgeDeps{
|
|
submit: executeTaskSubmission,
|
|
loadTask: model.GetTaskForProtocolObservation,
|
|
now: time.Now,
|
|
admissions: pluginProtocolObservationAdmissions,
|
|
protocolLimits: relay.DefaultPluginProtocolLimits(),
|
|
artifactContentURL: service.BuildTaskArtifactContentURL,
|
|
submissionTimeout: timeout,
|
|
observationTimeout: timeout,
|
|
loadTimeout: loadTimeout,
|
|
tickInterval: tick,
|
|
tickJitter: jitter,
|
|
heartbeatInterval: heartbeat,
|
|
admissionTimeout: pluginruntime.DefaultCallTimeout,
|
|
getByTaskId: model.GetByTaskId,
|
|
resolvePlugin: resolveTaskPluginForProtocolRetrieve,
|
|
imagePollInterval: taskPluginImagePollInterval,
|
|
pollTask: pollTaskPluginImageTask,
|
|
downloadImage: service.GetImageFromUrl,
|
|
}
|
|
}
|
|
|
|
func (d pluginProtocolBridgeDeps) withDefaults() pluginProtocolBridgeDeps {
|
|
defaults := defaultPluginProtocolBridgeDeps()
|
|
if d.submit == nil {
|
|
d.submit = defaults.submit
|
|
}
|
|
if d.loadTask == nil {
|
|
d.loadTask = defaults.loadTask
|
|
}
|
|
if d.now == nil {
|
|
d.now = defaults.now
|
|
}
|
|
if d.admissions == nil {
|
|
d.admissions = defaults.admissions
|
|
}
|
|
if d.artifactContentURL == nil {
|
|
d.artifactContentURL = defaults.artifactContentURL
|
|
}
|
|
if d.submissionTimeout <= 0 {
|
|
d.submissionTimeout = defaults.submissionTimeout
|
|
}
|
|
if d.observationTimeout <= 0 {
|
|
d.observationTimeout = defaults.observationTimeout
|
|
}
|
|
if d.loadTimeout <= 0 {
|
|
d.loadTimeout = defaults.loadTimeout
|
|
}
|
|
if d.tickInterval <= 0 {
|
|
d.tickInterval = defaults.tickInterval
|
|
}
|
|
if d.tickJitter < 0 {
|
|
d.tickJitter = 0
|
|
}
|
|
if d.heartbeatInterval <= 0 {
|
|
d.heartbeatInterval = defaults.heartbeatInterval
|
|
}
|
|
if halfHeartbeat := d.heartbeatInterval / 2; halfHeartbeat > 0 && d.loadTimeout > halfHeartbeat {
|
|
d.loadTimeout = halfHeartbeat
|
|
}
|
|
if d.admissionTimeout <= 0 {
|
|
d.admissionTimeout = defaults.admissionTimeout
|
|
}
|
|
if d.getByTaskId == nil {
|
|
d.getByTaskId = defaults.getByTaskId
|
|
}
|
|
if d.resolvePlugin == nil {
|
|
d.resolvePlugin = defaults.resolvePlugin
|
|
}
|
|
if d.imagePollInterval <= 0 {
|
|
d.imagePollInterval = defaults.imagePollInterval
|
|
}
|
|
if d.pollTask == nil {
|
|
d.pollTask = defaults.pollTask
|
|
}
|
|
if d.downloadImage == nil {
|
|
d.downloadImage = defaults.downloadImage
|
|
}
|
|
return d
|
|
}
|
|
|
|
func resolveTaskPluginForProtocolRetrieve(platform constant.TaskPlatform) (*pluginruntime.LoadedPlugin, *pluginruntime.RoutingGeneration, bool) {
|
|
generation := pluginruntime.DefaultRegistry.Generation()
|
|
plugin, ok := relay.ResolveTaskPluginForPlatform(generation, platform)
|
|
return plugin, generation, ok
|
|
}
|
|
|
|
func serveTaskPluginProtocol(
|
|
c *gin.Context,
|
|
pinned pluginruntime.PinnedEndpoint,
|
|
deps pluginProtocolBridgeDeps,
|
|
) {
|
|
deps = deps.withDefaults()
|
|
generation := uint64(0)
|
|
if pinned.Generation != nil {
|
|
generation = pinned.Generation.Number
|
|
}
|
|
pluginKey := ""
|
|
if pinned.Plugin != nil {
|
|
pluginKey = pinned.Plugin.Meta.Key
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=bridge_start generation=%d plugin=%q protocol=%q model=%q",
|
|
generation,
|
|
pluginKey,
|
|
pinned.Protocol,
|
|
c.GetString("resolved_task_model"),
|
|
)
|
|
if !pluginruntime.SupportsHostProtocol(pinned.Protocol) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=bridge_rejected generation=%d plugin=%q reason=unsupported_protocol", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusNotImplemented, "task_protocol_not_available", "Task protocol bridge is not available")
|
|
return
|
|
}
|
|
requestValue, exists := c.Get(pluginruntime.ContextKeyProtocolRequest)
|
|
protocolRequest, ok := requestValue.(pluginruntime.ProtocolRequestContext)
|
|
if !exists || !ok || protocolRequest.Protocol != pinned.Protocol {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=bridge_rejected generation=%d plugin=%q reason=invalid_protocol_context", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
if definition, known := pluginruntime.HostProtocol(pinned.Protocol); known && len(definition.DefinedModes()) > 0 && pinned.Plugin != nil {
|
|
background := false
|
|
if body, ok := protocolRequest.Body.(map[string]any); ok && body["kind"] == string(pluginruntime.BodyJSON) {
|
|
if requestBody, ok := body["value"].(map[string]any); ok {
|
|
background, _ = requestBody["background"].(bool)
|
|
}
|
|
}
|
|
missing := false
|
|
if protocolRequest.Stream && !pinned.Plugin.Meta.ProtocolSupports(pinned.Protocol, "stream") {
|
|
missing = true
|
|
}
|
|
if background && !pinned.Plugin.Meta.ProtocolSupports(pinned.Protocol, "background") {
|
|
missing = true
|
|
}
|
|
if !protocolRequest.Stream && !background && !pinned.Plugin.Meta.ProtocolSupports(pinned.Protocol, "sync") {
|
|
missing = true
|
|
}
|
|
if missing {
|
|
logger.LogError(c, common.LogText("pinned task plugin does not support the requested protocol form"))
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=request_ready generation=%d plugin=%q protocol=%q stream=%t",
|
|
generation,
|
|
pluginKey,
|
|
protocolRequest.Protocol,
|
|
protocolRequest.Stream,
|
|
)
|
|
|
|
release, admissionErr := deps.admissions.acquire(
|
|
pinned.Plugin.Meta.Key,
|
|
common.GetContextKeyInt(c, constant.ContextKeyUserId),
|
|
common.GetContextKeyInt(c, constant.ContextKeyTokenId),
|
|
)
|
|
if admissionErr != nil {
|
|
if errors.Is(admissionErr, errPluginProtocolObservationLimitExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=admission_rejected generation=%d plugin=%q reason=observation_limit", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusTooManyRequests, "rate_limit_exceeded", "Too many active task observations")
|
|
return
|
|
}
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=admission_rejected generation=%d plugin=%q reason=invalid_identity", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusUnauthorized, "authentication_error", "Authentication failed")
|
|
return
|
|
}
|
|
defer release()
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=admission_acquired generation=%d plugin=%q", generation, pluginKey)
|
|
|
|
clientRequest := c.Request
|
|
var relayInfo *relaycommon.RelayInfo
|
|
var outcome *taskSubmissionOutcome
|
|
var taskErr *dto.TaskError
|
|
var relayInfoErr error
|
|
// 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))
|
|
}()
|
|
submissionStage := "relay_info"
|
|
// A Responses client only observes an asynchronous task. Once admitted,
|
|
// disconnecting that observer must not cancel submission, persistence, or
|
|
// billing settlement; the submission keeps its own bounded lifetime.
|
|
func() {
|
|
submissionContext, cancelSubmission := context.WithTimeout(
|
|
context.WithoutCancel(clientRequest.Context()),
|
|
deps.submissionTimeout,
|
|
)
|
|
c.Request = clientRequest.Clone(submissionContext)
|
|
defer func() {
|
|
c.Request = clientRequest
|
|
cancelSubmission()
|
|
}()
|
|
|
|
relayInfo, relayInfoErr = relaycommon.GenRelayInfo(c, types.RelayFormatTask, nil, nil)
|
|
if relayInfoErr != nil {
|
|
return
|
|
}
|
|
relayInfo.RelayMode = relayconstant.RelayModeVideoSubmit
|
|
relayInfo.IsStream = false
|
|
relayInfo.OriginModelName = c.GetString("resolved_task_model")
|
|
if action := c.GetString("task_action"); action != "" {
|
|
relayInfo.Action = action
|
|
}
|
|
submissionStage = "origin_task"
|
|
if taskErr = relay.ResolveOriginTask(c, relayInfo); taskErr != nil {
|
|
return
|
|
}
|
|
if taskErr = relay.ApplyOriginTaskAffinity(c, relayInfo); taskErr != nil {
|
|
return
|
|
}
|
|
|
|
submissionStage = "submission"
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=submission_start generation=%d plugin=%q protocol=%q stream=%t", generation, pluginKey, protocolRequest.Protocol, protocolRequest.Stream)
|
|
outcome, taskErr = deps.submit(c, relayInfo)
|
|
}()
|
|
|
|
if clientRequest.Context().Err() != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q stage=%s", generation, pluginKey, submissionStage)
|
|
return
|
|
}
|
|
if relayInfoErr != nil {
|
|
err := relayInfoErr
|
|
logger.LogError(c, common.LogText("build task protocol relay info failed: %s", err.Error()))
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=bridge_failed generation=%d plugin=%q stage=relay_info reason=invalid_context", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
if submissionStage == "origin_task" && taskErr != nil {
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=bridge_failed generation=%d plugin=%q stage=origin_task code=%q status=%d",
|
|
generation,
|
|
pluginKey,
|
|
taskErr.Code,
|
|
taskErr.StatusCode,
|
|
)
|
|
respondPluginProtocolSubmissionError(c, taskErr)
|
|
return
|
|
}
|
|
|
|
if taskErr != nil {
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=submission_failed generation=%d plugin=%q code=%q status=%d local=%t",
|
|
generation,
|
|
pluginKey,
|
|
taskErr.Code,
|
|
taskErr.StatusCode,
|
|
taskErr.LocalError,
|
|
)
|
|
respondPluginProtocolSubmissionError(c, taskErr)
|
|
return
|
|
}
|
|
if outcome == nil || outcome.Task == nil || outcome.RelayInfo == nil ||
|
|
outcome.Task.UserId != relayInfo.UserId ||
|
|
outcome.Task.Platform != constant.TaskPlatform(pinned.Plugin.Meta.Key) {
|
|
logger.LogError(c, common.LogText("task protocol submission returned an invalid durable outcome"))
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=submission_failed generation=%d plugin=%q reason=invalid_durable_outcome", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=submission_durable generation=%d plugin=%q public_task_id=%q status=%q stream=%t",
|
|
generation,
|
|
pluginKey,
|
|
outcome.Task.TaskID,
|
|
taskPluginDebugStatus(string(outcome.Task.Status)),
|
|
protocolRequest.Stream,
|
|
)
|
|
|
|
createdAt := outcome.Task.CreatedAt
|
|
if createdAt == 0 {
|
|
createdAt = outcome.Task.SubmitTime
|
|
}
|
|
if createdAt == 0 {
|
|
createdAt = deps.now().Unix()
|
|
}
|
|
machine := relay.NewPluginResponsesMachine(
|
|
outcome.Task.TaskID,
|
|
outcome.RelayInfo.OriginModelName,
|
|
createdAt,
|
|
deps.protocolLimits,
|
|
)
|
|
background := false
|
|
if body, ok := protocolRequest.Body.(map[string]any); ok && body["kind"] == string(pluginruntime.BodyJSON) {
|
|
if requestBody, ok := body["value"].(map[string]any); ok {
|
|
background, _ = requestBody["background"].(bool)
|
|
}
|
|
}
|
|
if background {
|
|
outcome.Task.PrivateData.ResponsesBackground = true
|
|
if outcome.Task.ID != 0 {
|
|
if err := model.DB.Model(outcome.Task).Update("private_data", outcome.Task.PrivateData).Error; err != nil {
|
|
logger.LogError(c, common.LogText("persist task background flag failed: %s", err.Error()))
|
|
}
|
|
}
|
|
machine.SetBackground(true)
|
|
if !protocolRequest.Stream {
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=background_return generation=%d plugin=%q public_task_id=%q status=%q",
|
|
generation,
|
|
pluginKey,
|
|
outcome.Task.TaskID,
|
|
taskPluginDebugStatus(string(outcome.Task.Status)),
|
|
)
|
|
c.JSON(http.StatusOK, machine.PendingResponse(string(outcome.Task.Status)))
|
|
return
|
|
}
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=background_stream generation=%d plugin=%q public_task_id=%q", generation, pluginKey, outcome.Task.TaskID)
|
|
}
|
|
if protocolRequest.Stream {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_enter generation=%d plugin=%q mode=stream public_task_id=%q", generation, pluginKey, outcome.Task.TaskID)
|
|
streamTaskPluginProtocol(c, pinned, protocolRequest, outcome.Task.TaskID, machine, deps)
|
|
return
|
|
}
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_enter generation=%d plugin=%q mode=nonstream public_task_id=%q", generation, pluginKey, outcome.Task.TaskID)
|
|
waitTaskPluginProtocol(c, pinned, protocolRequest, outcome.Task.TaskID, machine, deps)
|
|
}
|
|
|
|
func streamTaskPluginProtocol(
|
|
c *gin.Context,
|
|
pinned pluginruntime.PinnedEndpoint,
|
|
protocolRequest pluginruntime.ProtocolRequestContext,
|
|
taskID string,
|
|
machine *relay.PluginResponsesMachine,
|
|
deps pluginProtocolBridgeDeps,
|
|
) {
|
|
generation := pinned.Generation.Number
|
|
pluginKey := pinned.Plugin.Meta.Key
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_start generation=%d plugin=%q mode=stream public_task_id=%q timeout_ms=%d tick_ms=%d heartbeat_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
taskID,
|
|
deps.observationTimeout.Milliseconds(),
|
|
deps.tickInterval.Milliseconds(),
|
|
deps.heartbeatInterval.Milliseconds(),
|
|
)
|
|
created, err := machine.CreatedEvent()
|
|
if err != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_failed generation=%d plugin=%q mode=stream stage=created_event reason=state_machine_error", generation, pluginKey)
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
helper.SetEventStreamHeaders(c)
|
|
if err = writeTaskPluginProtocolEvent(c, created); err != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_write_failed generation=%d plugin=%q mode=stream stage=created_event", generation, pluginKey)
|
|
return
|
|
}
|
|
|
|
observationContext, cancelObservation := context.WithTimeout(c.Request.Context(), deps.observationTimeout)
|
|
defer cancelObservation()
|
|
heartbeatTicker := time.NewTicker(deps.heartbeatInterval)
|
|
defer heartbeatTicker.Stop()
|
|
|
|
var previous relay.ProtocolState
|
|
tickNumber := uint64(0)
|
|
lastStatus := ""
|
|
for {
|
|
loadStarted := deps.now()
|
|
loadContext, cancelLoad := context.WithTimeout(observationContext, deps.loadTimeout)
|
|
task, exists, loadErr := deps.loadTask(
|
|
loadContext,
|
|
common.GetContextKeyInt(c, constant.ContextKeyUserId),
|
|
constant.TaskPlatform(pinned.Plugin.Meta.Key),
|
|
taskID,
|
|
)
|
|
loadContextErr := loadContext.Err()
|
|
cancelLoad()
|
|
loadElapsed := deps.now().Sub(loadStarted)
|
|
if errors.Is(loadContextErr, context.DeadlineExceeded) &&
|
|
observationContext.Err() == nil &&
|
|
c.Request.Context().Err() == nil {
|
|
logger.LogWarn(c, common.LogText(
|
|
"task protocol database observation overloaded; plugin=%s task=%s",
|
|
pinned.Plugin.Meta.Key,
|
|
taskID,
|
|
))
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_tick generation=%d plugin=%q mode=stream tick=%d load_ms=%d overloaded=true",
|
|
generation,
|
|
pluginKey,
|
|
tickNumber,
|
|
loadElapsed.Milliseconds(),
|
|
)
|
|
delay := pluginProtocolTickDelay(taskID, tickNumber, deps.tickInterval, deps.tickJitter) + deps.tickInterval
|
|
tickNumber++
|
|
if !waitForTaskPluginProtocolTick(c, observationContext, heartbeatTicker, delay) {
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=stream last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeout(c, machine, lastStatus)
|
|
} else if c.Request.Context().Err() != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=stream stage=backoff_wait", generation, pluginKey)
|
|
} else {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_write_failed generation=%d plugin=%q mode=stream stage=heartbeat", generation, pluginKey)
|
|
}
|
|
return
|
|
}
|
|
continue
|
|
}
|
|
if loadErr != nil || !exists || task == nil {
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=stream last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeout(c, machine, lastStatus)
|
|
return
|
|
}
|
|
if loadErr != nil && !errors.Is(loadErr, context.Canceled) {
|
|
logger.LogError(c, common.LogText("task protocol database observation failed"))
|
|
}
|
|
if c.Request.Context().Err() == nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_failed generation=%d plugin=%q mode=stream stage=load reason=task_unavailable", generation, pluginKey)
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
} else {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=stream stage=load", generation, pluginKey)
|
|
}
|
|
return
|
|
}
|
|
previousStatus := lastStatus
|
|
lastStatus = string(task.Status)
|
|
if lastStatus != previousStatus {
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=status_transition generation=%d plugin=%q mode=stream tick=%d previous=%q status=%q load_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
tickNumber,
|
|
taskPluginDebugStatus(previousStatus),
|
|
taskPluginDebugStatus(lastStatus),
|
|
loadElapsed.Milliseconds(),
|
|
)
|
|
}
|
|
view, viewErr := service.BuildTaskPluginView(task)
|
|
if viewErr != nil {
|
|
logger.LogError(c, common.LogText("build task protocol view failed: %s", viewErr.Error()))
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
return
|
|
}
|
|
viewValue, viewErr := taskPluginProtocolJSONValue(view)
|
|
if viewErr != nil {
|
|
logger.LogError(c, common.LogText("encode task protocol view failed: %s", viewErr.Error()))
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
return
|
|
}
|
|
hookStarted := deps.now()
|
|
rendererContext, contextErr := taskPluginProtocolRendererContext(protocolRequest, pinned, task, deps.artifactContentURL)
|
|
if contextErr != nil {
|
|
logger.LogError(c, common.LogText("build task protocol renderer context failed"))
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
return
|
|
}
|
|
args := []any{rendererContext, viewValue}
|
|
if previous.Present {
|
|
args = append(args, previous.PluginValue())
|
|
}
|
|
value, callErr := pinned.Plugin.Engine.CallPathWithAdmissionTimeout(observationContext, deps.admissionTimeout, "protocols", []string{pinned.Protocol, "renderEvents"}, args...)
|
|
hookElapsed := deps.now().Sub(hookStarted)
|
|
overloaded := false
|
|
if callErr != nil {
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=stream stage=render_events last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeout(c, machine, lastStatus)
|
|
return
|
|
}
|
|
if c.Request.Context().Err() != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=stream stage=render_events", generation, pluginKey)
|
|
return
|
|
}
|
|
if errors.Is(callErr, pluginruntime.ErrCallAdmissionTimeout) {
|
|
overloaded = true
|
|
logger.LogWarn(c, common.LogText(
|
|
"task protocol render hook overloaded; plugin=%s task=%s",
|
|
pinned.Plugin.Meta.Key,
|
|
taskID,
|
|
))
|
|
} else {
|
|
logger.LogError(c, common.LogText("task protocol render hook failed"))
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_failed generation=%d plugin=%q mode=stream stage=render_events reason=hook_failed elapsed_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
hookElapsed.Milliseconds(),
|
|
)
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
return
|
|
}
|
|
}
|
|
if !overloaded {
|
|
result, decodeErr := relay.DecodePluginProtocolEventResult(value, deps.protocolLimits)
|
|
if decodeErr != nil {
|
|
logger.LogError(c, common.LogText("task protocol render result invalid: %s", decodeErr.Error()))
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
return
|
|
}
|
|
events, applyErr := machine.ApplyTick(result, lastStatus)
|
|
if applyErr != nil {
|
|
logger.LogError(c, common.LogText("task protocol state transition failed: %s", applyErr.Error()))
|
|
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
|
return
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=render_events generation=%d plugin=%q mode=stream tick=%d status=%q semantic_events=%d wire_events=%d done=%t state_present=%t elapsed_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
tickNumber,
|
|
taskPluginDebugStatus(lastStatus),
|
|
len(result.Events),
|
|
len(events),
|
|
result.Done,
|
|
result.State.Present,
|
|
hookElapsed.Milliseconds(),
|
|
)
|
|
for _, event := range events {
|
|
if err = writeTaskPluginProtocolEvent(c, event); err != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_write_failed generation=%d plugin=%q mode=stream stage=event event_type=%q sequence=%d", generation, pluginKey, event.Type, event.SequenceNumber)
|
|
return
|
|
}
|
|
}
|
|
if taskPluginProtocolEventsTerminal(events) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_complete generation=%d plugin=%q mode=stream reason=terminal status=%q ticks=%d", generation, pluginKey, taskPluginDebugStatus(lastStatus), tickNumber+1)
|
|
return
|
|
}
|
|
previous = result.State
|
|
}
|
|
|
|
delay := pluginProtocolTickDelay(taskID, tickNumber, deps.tickInterval, deps.tickJitter)
|
|
tickNumber++
|
|
if overloaded {
|
|
delay += deps.tickInterval
|
|
} else if hookElapsed > deps.tickInterval {
|
|
delay += deps.tickInterval
|
|
logger.LogWarn(c, common.LogText(
|
|
"task protocol render hook slow; plugin=%s task=%s elapsed_ms=%d",
|
|
pinned.Plugin.Meta.Key,
|
|
taskID,
|
|
hookElapsed.Milliseconds(),
|
|
))
|
|
}
|
|
if !waitForTaskPluginProtocolTick(c, observationContext, heartbeatTicker, delay) {
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=stream last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeout(c, machine, lastStatus)
|
|
} else if c.Request.Context().Err() != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=stream stage=tick_wait", generation, pluginKey)
|
|
} else {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_write_failed generation=%d plugin=%q mode=stream stage=heartbeat", generation, pluginKey)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func waitForTaskPluginProtocolTick(
|
|
c *gin.Context,
|
|
observationContext context.Context,
|
|
heartbeatTicker *time.Ticker,
|
|
delay time.Duration,
|
|
) bool {
|
|
tickTimer := time.NewTimer(delay)
|
|
defer tickTimer.Stop()
|
|
for {
|
|
select {
|
|
case <-c.Request.Context().Done():
|
|
return false
|
|
case <-observationContext.Done():
|
|
return false
|
|
case <-heartbeatTicker.C:
|
|
if err := writeTaskPluginProtocolHeartbeat(c); err != nil {
|
|
return false
|
|
}
|
|
case <-tickTimer.C:
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
func waitTaskPluginProtocol(
|
|
c *gin.Context,
|
|
pinned pluginruntime.PinnedEndpoint,
|
|
protocolRequest pluginruntime.ProtocolRequestContext,
|
|
taskID string,
|
|
machine *relay.PluginResponsesMachine,
|
|
deps pluginProtocolBridgeDeps,
|
|
) {
|
|
generation := pinned.Generation.Number
|
|
pluginKey := pinned.Plugin.Meta.Key
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_start generation=%d plugin=%q mode=nonstream public_task_id=%q timeout_ms=%d tick_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
taskID,
|
|
deps.observationTimeout.Milliseconds(),
|
|
deps.tickInterval.Milliseconds(),
|
|
)
|
|
observationContext, cancelObservation := context.WithTimeout(c.Request.Context(), deps.observationTimeout)
|
|
defer cancelObservation()
|
|
tickNumber := uint64(0)
|
|
lastStatus := ""
|
|
for {
|
|
loadStarted := deps.now()
|
|
loadContext, cancelLoad := context.WithTimeout(observationContext, deps.loadTimeout)
|
|
task, exists, err := deps.loadTask(
|
|
loadContext,
|
|
common.GetContextKeyInt(c, constant.ContextKeyUserId),
|
|
constant.TaskPlatform(pinned.Plugin.Meta.Key),
|
|
taskID,
|
|
)
|
|
loadContextErr := loadContext.Err()
|
|
cancelLoad()
|
|
loadElapsed := deps.now().Sub(loadStarted)
|
|
loadOverloaded := errors.Is(loadContextErr, context.DeadlineExceeded) &&
|
|
observationContext.Err() == nil &&
|
|
c.Request.Context().Err() == nil
|
|
if loadOverloaded {
|
|
logger.LogWarn(c, common.LogText(
|
|
"task protocol database observation overloaded; plugin=%s task=%s",
|
|
pinned.Plugin.Meta.Key,
|
|
taskID,
|
|
))
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_tick generation=%d plugin=%q mode=nonstream tick=%d load_ms=%d overloaded=true",
|
|
generation,
|
|
pluginKey,
|
|
tickNumber,
|
|
loadElapsed.Milliseconds(),
|
|
)
|
|
} else if err != nil || !exists || task == nil {
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=nonstream last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeoutResponse(c, machine, lastStatus)
|
|
return
|
|
}
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
logger.LogError(c, common.LogText("task protocol database observation failed"))
|
|
}
|
|
if c.Request.Context().Err() == nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_failed generation=%d plugin=%q mode=nonstream stage=load reason=task_unavailable", generation, pluginKey)
|
|
writeTaskPluginProtocolFailureResponse(c, machine, lastStatus)
|
|
} else {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=nonstream stage=load", generation, pluginKey)
|
|
}
|
|
return
|
|
}
|
|
overloaded := loadOverloaded
|
|
if !loadOverloaded {
|
|
previousStatus := lastStatus
|
|
lastStatus = string(task.Status)
|
|
if lastStatus != previousStatus {
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=status_transition generation=%d plugin=%q mode=nonstream tick=%d previous=%q status=%q load_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
tickNumber,
|
|
taskPluginDebugStatus(previousStatus),
|
|
taskPluginDebugStatus(lastStatus),
|
|
loadElapsed.Milliseconds(),
|
|
)
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_tick generation=%d plugin=%q mode=nonstream tick=%d status=%q load_ms=%d overloaded=false",
|
|
generation,
|
|
pluginKey,
|
|
tickNumber,
|
|
taskPluginDebugStatus(lastStatus),
|
|
loadElapsed.Milliseconds(),
|
|
)
|
|
}
|
|
if !loadOverloaded && (task.Status == model.TaskStatusSuccess || task.Status == model.TaskStatusFailure) {
|
|
if task.Status == model.TaskStatusFailure {
|
|
writeTaskPluginProtocolFailureResponse(c, machine, string(task.Status))
|
|
return
|
|
}
|
|
response, hookElapsed, callErr := renderTaskPluginProtocolFinalResponse(
|
|
observationContext,
|
|
pinned,
|
|
protocolRequest,
|
|
task,
|
|
machine,
|
|
deps,
|
|
)
|
|
if callErr != nil {
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=nonstream stage=render_final last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeoutResponse(c, machine, lastStatus)
|
|
return
|
|
}
|
|
if c.Request.Context().Err() != nil {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=nonstream stage=render_final", generation, pluginKey)
|
|
return
|
|
}
|
|
if errors.Is(callErr, pluginruntime.ErrCallAdmissionTimeout) {
|
|
overloaded = true
|
|
logger.LogWarn(c, common.LogText(
|
|
"task protocol final hook overloaded; plugin=%s task=%s",
|
|
pinned.Plugin.Meta.Key,
|
|
taskID,
|
|
))
|
|
} else {
|
|
logger.LogError(c, common.LogText("task protocol final hook failed"))
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_failed generation=%d plugin=%q mode=nonstream stage=render_final reason=hook_failed elapsed_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
hookElapsed.Milliseconds(),
|
|
)
|
|
writeTaskPluginProtocolFailureResponse(c, machine, lastStatus)
|
|
return
|
|
}
|
|
} else {
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=render_final generation=%d plugin=%q mode=nonstream status=%q elapsed_ms=%d",
|
|
generation,
|
|
pluginKey,
|
|
taskPluginDebugStatus(lastStatus),
|
|
hookElapsed.Milliseconds(),
|
|
)
|
|
c.JSON(http.StatusOK, response)
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=observation_complete generation=%d plugin=%q mode=nonstream reason=terminal status=%q ticks=%d",
|
|
generation,
|
|
pluginKey,
|
|
taskPluginDebugStatus(lastStatus),
|
|
tickNumber+1,
|
|
)
|
|
return
|
|
}
|
|
}
|
|
|
|
delay := pluginProtocolTickDelay(taskID, tickNumber, deps.tickInterval, deps.tickJitter)
|
|
tickNumber++
|
|
if overloaded {
|
|
delay += deps.tickInterval
|
|
}
|
|
tickTimer := time.NewTimer(delay)
|
|
select {
|
|
case <-c.Request.Context().Done():
|
|
if !tickTimer.Stop() {
|
|
select {
|
|
case <-tickTimer.C:
|
|
default:
|
|
}
|
|
}
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=client_disconnected generation=%d plugin=%q mode=nonstream stage=tick_wait", generation, pluginKey)
|
|
return
|
|
case <-observationContext.Done():
|
|
if !tickTimer.Stop() {
|
|
select {
|
|
case <-tickTimer.C:
|
|
default:
|
|
}
|
|
}
|
|
if errors.Is(observationContext.Err(), context.DeadlineExceeded) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=observation_timeout generation=%d plugin=%q mode=nonstream last_status=%q", generation, pluginKey, taskPluginDebugStatus(lastStatus))
|
|
writeTaskPluginProtocolTimeoutResponse(c, machine, lastStatus)
|
|
}
|
|
return
|
|
case <-tickTimer.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func renderTaskPluginProtocolFinalResponse(
|
|
ctx context.Context,
|
|
pinned pluginruntime.PinnedEndpoint,
|
|
protocolRequest pluginruntime.ProtocolRequestContext,
|
|
task *model.Task,
|
|
machine *relay.PluginResponsesMachine,
|
|
deps pluginProtocolBridgeDeps,
|
|
) (map[string]any, time.Duration, error) {
|
|
view, err := service.BuildTaskPluginView(task)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
viewValue, err := taskPluginProtocolJSONValue(view)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rendererContext, err := taskPluginProtocolRendererContext(
|
|
protocolRequest,
|
|
pinned,
|
|
task,
|
|
deps.artifactContentURL,
|
|
)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
hookStarted := deps.now()
|
|
payload, err := pinned.Plugin.Engine.CallPathWithAdmissionTimeout(
|
|
ctx,
|
|
deps.admissionTimeout,
|
|
"protocols",
|
|
[]string{pinned.Protocol, "renderFinal"},
|
|
rendererContext,
|
|
viewValue,
|
|
)
|
|
hookElapsed := deps.now().Sub(hookStarted)
|
|
if err != nil {
|
|
return nil, hookElapsed, err
|
|
}
|
|
response, err := machine.FinalResponse(payload, string(task.Status))
|
|
if err != nil {
|
|
return nil, hookElapsed, err
|
|
}
|
|
return response, hookElapsed, nil
|
|
}
|
|
|
|
func renderTaskPluginProtocolEventsResponse(
|
|
ctx context.Context,
|
|
pinned pluginruntime.PinnedEndpoint,
|
|
protocolRequest pluginruntime.ProtocolRequestContext,
|
|
task *model.Task,
|
|
machine *relay.PluginResponsesMachine,
|
|
deps pluginProtocolBridgeDeps,
|
|
) (map[string]any, time.Duration, error) {
|
|
view, err := service.BuildTaskPluginView(task)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
viewValue, err := taskPluginProtocolJSONValue(view)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
rendererContext, err := taskPluginProtocolRendererContext(
|
|
protocolRequest,
|
|
pinned,
|
|
task,
|
|
deps.artifactContentURL,
|
|
)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
hookStarted := deps.now()
|
|
value, err := pinned.Plugin.Engine.CallPathWithAdmissionTimeout(
|
|
ctx,
|
|
deps.admissionTimeout,
|
|
"protocols",
|
|
[]string{pinned.Protocol, "renderEvents"},
|
|
rendererContext,
|
|
viewValue,
|
|
)
|
|
hookElapsed := deps.now().Sub(hookStarted)
|
|
if err != nil {
|
|
return nil, hookElapsed, err
|
|
}
|
|
result, err := relay.DecodePluginProtocolEventResult(value, deps.protocolLimits)
|
|
if err != nil {
|
|
return nil, hookElapsed, err
|
|
}
|
|
response, err := machine.FinalFromEvents(result, string(task.Status))
|
|
if err != nil {
|
|
return nil, hookElapsed, err
|
|
}
|
|
return response, hookElapsed, nil
|
|
}
|
|
|
|
func RetrieveTaskPluginResponse(c *gin.Context) {
|
|
retrieveTaskPluginResponse(c, defaultPluginProtocolBridgeDeps())
|
|
}
|
|
|
|
func retrieveTaskPluginResponse(c *gin.Context, deps pluginProtocolBridgeDeps) {
|
|
deps = deps.withDefaults()
|
|
responseID := strings.TrimSpace(c.Param("response_id"))
|
|
if !strings.HasPrefix(responseID, "resp_") {
|
|
writeTaskPluginResponseNotFound(c, responseID, "bad_prefix")
|
|
return
|
|
}
|
|
taskID := "task_" + strings.TrimPrefix(responseID, "resp_")
|
|
userID := common.GetContextKeyInt(c, constant.ContextKeyUserId)
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=retrieve_start response_id=%q public_task_id=%q", responseID, taskID)
|
|
|
|
task, exists, err := deps.getByTaskId(userID, taskID)
|
|
if err != nil {
|
|
logger.LogError(c, common.LogText("task protocol retrieve lookup failed"))
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=retrieve_failed reason=lookup_error public_task_id=%q", taskID)
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
if !exists || task == nil {
|
|
writeTaskPluginResponseNotFound(c, responseID, "missing")
|
|
return
|
|
}
|
|
if !task.ResultRetrievable() {
|
|
writeTaskPluginResponseNotFound(c, responseID, "result_discarded")
|
|
return
|
|
}
|
|
|
|
plugin, generation, ok := deps.resolvePlugin(task.Platform)
|
|
if !ok || plugin == nil {
|
|
writeTaskPluginResponseNotFound(c, responseID, "no_plugin")
|
|
return
|
|
}
|
|
claimsProtocol := false
|
|
for _, claim := range plugin.Meta.Protocols {
|
|
if claim.Name == "openai_responses" {
|
|
claimsProtocol = true
|
|
break
|
|
}
|
|
}
|
|
if !claimsProtocol {
|
|
writeTaskPluginResponseNotFound(c, responseID, "no_claim")
|
|
return
|
|
}
|
|
|
|
generationNumber := uint64(0)
|
|
if generation != nil {
|
|
generationNumber = generation.Number
|
|
}
|
|
createdAt := task.CreatedAt
|
|
if createdAt == 0 {
|
|
createdAt = task.SubmitTime
|
|
}
|
|
if createdAt == 0 {
|
|
createdAt = deps.now().Unix()
|
|
}
|
|
machine := relay.NewPluginResponsesMachine(
|
|
task.TaskID,
|
|
task.Properties.OriginModelName,
|
|
createdAt,
|
|
deps.protocolLimits,
|
|
)
|
|
machine.SetBackground(task.PrivateData.ResponsesBackground)
|
|
pinned := pluginruntime.PinnedEndpoint{
|
|
Generation: generation,
|
|
Plugin: plugin,
|
|
Protocol: "openai_responses",
|
|
Model: task.Properties.OriginModelName,
|
|
}
|
|
protocolRequest := pluginruntime.ProtocolRequestContext{
|
|
RouteRequestContext: pluginruntime.RouteRequestContext{
|
|
Path: c.Request.URL.Path,
|
|
Method: http.MethodGet,
|
|
Params: map[string]string{"response_id": responseID},
|
|
Query: c.Request.URL.Query(),
|
|
Body: map[string]any{"kind": string(pluginruntime.BodyNone)},
|
|
},
|
|
Protocol: "openai_responses",
|
|
Operation: "retrieve",
|
|
Model: task.Properties.OriginModelName,
|
|
}
|
|
|
|
if task.Status == model.TaskStatusFailure {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=retrieve_final generation=%d plugin=%q public_task_id=%q status=%q", generationNumber, plugin.Meta.Key, task.TaskID, taskPluginDebugStatus(string(task.Status)))
|
|
writeTaskPluginProtocolFailureResponse(c, machine, string(task.Status))
|
|
return
|
|
}
|
|
if task.Status != model.TaskStatusSuccess {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=retrieve_pending generation=%d plugin=%q public_task_id=%q status=%q", generationNumber, plugin.Meta.Key, task.TaskID, taskPluginDebugStatus(string(task.Status)))
|
|
c.JSON(http.StatusOK, machine.PendingResponse(string(task.Status)))
|
|
return
|
|
}
|
|
|
|
var (
|
|
response map[string]any
|
|
hookElapsed time.Duration
|
|
renderErr error
|
|
)
|
|
if plugin.Meta.ProtocolSupports("openai_responses", "sync") || plugin.Meta.ProtocolSupports("openai_responses", "background") {
|
|
response, hookElapsed, renderErr = renderTaskPluginProtocolFinalResponse(
|
|
c.Request.Context(),
|
|
pinned,
|
|
protocolRequest,
|
|
task,
|
|
machine,
|
|
deps,
|
|
)
|
|
} else {
|
|
response, hookElapsed, renderErr = renderTaskPluginProtocolEventsResponse(
|
|
c.Request.Context(),
|
|
pinned,
|
|
protocolRequest,
|
|
task,
|
|
machine,
|
|
deps,
|
|
)
|
|
}
|
|
if renderErr != nil {
|
|
logger.LogError(c, common.LogText("task protocol retrieve render failed"))
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=retrieve_failed generation=%d plugin=%q public_task_id=%q stage=render_final elapsed_ms=%d",
|
|
generationNumber,
|
|
plugin.Meta.Key,
|
|
task.TaskID,
|
|
hookElapsed.Milliseconds(),
|
|
)
|
|
writeTaskPluginProtocolFailureResponse(c, machine, string(task.Status))
|
|
return
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=retrieve_final generation=%d plugin=%q public_task_id=%q status=%q elapsed_ms=%d",
|
|
generationNumber,
|
|
plugin.Meta.Key,
|
|
task.TaskID,
|
|
taskPluginDebugStatus(string(task.Status)),
|
|
hookElapsed.Milliseconds(),
|
|
)
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
func writeTaskPluginResponseNotFound(c *gin.Context, responseID, reason string) {
|
|
logger.LogDebug(c, "task_plugin subsystem=protocol event=retrieve_not_found reason=%s response_id=%q", reason, responseID)
|
|
respondPluginProtocolError(c, http.StatusNotFound, "not_found", "No response found with id '"+responseID+"'.")
|
|
}
|
|
|
|
func writeTaskPluginProtocolHeartbeat(c *gin.Context) error {
|
|
if _, err := c.Writer.Write([]byte(": PING\n")); err != nil {
|
|
return err
|
|
}
|
|
return helper.FlushWriter(c)
|
|
}
|
|
|
|
func writeTaskPluginProtocolFailure(
|
|
c *gin.Context,
|
|
machine *relay.PluginResponsesMachine,
|
|
taskStatus string,
|
|
) {
|
|
failed, err := machine.FailureEvent(taskStatus)
|
|
if err != nil {
|
|
logger.LogError(c, common.LogText("task protocol failure event failed: %s", err.Error()))
|
|
return
|
|
}
|
|
_ = writeTaskPluginProtocolEvent(c, failed)
|
|
}
|
|
|
|
func writeTaskPluginProtocolTimeout(
|
|
c *gin.Context,
|
|
machine *relay.PluginResponsesMachine,
|
|
taskStatus string,
|
|
) {
|
|
incomplete, err := machine.TimeoutEvent(taskStatus)
|
|
if err != nil {
|
|
logger.LogError(c, common.LogText("task protocol timeout event failed: %s", err.Error()))
|
|
return
|
|
}
|
|
_ = writeTaskPluginProtocolEvent(c, incomplete)
|
|
}
|
|
|
|
func writeTaskPluginProtocolFailureResponse(
|
|
c *gin.Context,
|
|
machine *relay.PluginResponsesMachine,
|
|
taskStatus string,
|
|
) {
|
|
if taskStatus == string(model.TaskStatusFailure) {
|
|
response, err := machine.FinalResponse(nil, taskStatus)
|
|
if err != nil {
|
|
logger.LogError(c, common.LogText("task protocol terminal failure response failed: %s", err.Error()))
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, response)
|
|
return
|
|
}
|
|
response, err := machine.FailureResponse(taskStatus)
|
|
if err != nil {
|
|
logger.LogError(c, common.LogText("task protocol failure response failed: %s", err.Error()))
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
func writeTaskPluginProtocolTimeoutResponse(
|
|
c *gin.Context,
|
|
machine *relay.PluginResponsesMachine,
|
|
lastStatus string,
|
|
) {
|
|
response, err := machine.TimeoutResponse(lastStatus)
|
|
if err != nil {
|
|
logger.LogError(c, common.LogText("task protocol timeout response failed: %s", err.Error()))
|
|
respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed")
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// taskPluginProtocolJSONValue encodes a task view for a hook, which receives
|
|
// it parsed from the text.
|
|
func taskPluginProtocolJSONValue(view dto.TaskView) (any, error) {
|
|
encoded, err := common.Marshal(view)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return pluginruntime.RawJSON(encoded), nil
|
|
}
|
|
|
|
func taskPluginProtocolRendererContext(
|
|
request pluginruntime.ProtocolRequestContext,
|
|
pinned pluginruntime.PinnedEndpoint,
|
|
task *model.Task,
|
|
artifactContentURL func(taskID, artifactKey string) (string, error),
|
|
) (map[string]any, error) {
|
|
rendererContext := request.JSValue()
|
|
if task == nil || task.Status != model.TaskStatusSuccess {
|
|
return rendererContext, nil
|
|
}
|
|
if pinned.Plugin == nil {
|
|
return nil, errors.New("task artifact projection is unavailable")
|
|
}
|
|
artifacts, err := taskjsplugin.RendererArtifacts(pinned.Plugin, task, artifactContentURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rendererContext["artifacts"] = artifacts
|
|
return rendererContext, nil
|
|
}
|
|
|
|
func pluginProtocolTickDelay(taskID string, tick uint64, base, jitter time.Duration) time.Duration {
|
|
if jitter <= 0 {
|
|
return base
|
|
}
|
|
hash := fnv.New64a()
|
|
_, _ = hash.Write([]byte(taskID))
|
|
_, _ = hash.Write([]byte(":"))
|
|
_, _ = hash.Write([]byte(strconv.FormatUint(tick, 10)))
|
|
return base + time.Duration(hash.Sum64()%uint64(jitter+1))
|
|
}
|
|
|
|
func taskPluginProtocolEventsTerminal(events []dto.PluginResponsesStreamEvent) bool {
|
|
for _, event := range events {
|
|
switch event.Type {
|
|
case "response.completed", "response.failed", "response.incomplete":
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func writeTaskPluginProtocolEvent(c *gin.Context, event dto.PluginResponsesStreamEvent) error {
|
|
encoded, err := common.Marshal(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err = c.Writer.Write([]byte("event: " + event.Type + "\n")); err != nil {
|
|
return err
|
|
}
|
|
if _, err = c.Writer.Write([]byte("data: " + string(encoded) + "\n\n")); err != nil {
|
|
return err
|
|
}
|
|
if err = helper.FlushWriter(c); err != nil {
|
|
return err
|
|
}
|
|
logger.LogDebug(
|
|
c,
|
|
"task_plugin subsystem=protocol event=sse_event_sent event_type=%q sequence=%d",
|
|
event.Type,
|
|
event.SequenceNumber,
|
|
)
|
|
return nil
|
|
}
|
|
|
|
func respondPluginProtocolSubmissionError(c *gin.Context, taskErr *dto.TaskError) {
|
|
status := http.StatusInternalServerError
|
|
if taskErr != nil && taskErr.StatusCode >= 400 && taskErr.StatusCode <= 599 {
|
|
status = taskErr.StatusCode
|
|
}
|
|
if requestplugin.Stopped(taskErr) {
|
|
// A request plugin's stop keeps its code and message, as on chat
|
|
// routes.
|
|
respondPluginProtocolError(c, status, taskErr.Code, common.MessageWithRequestId(taskErr.Message, c.GetString(common.RequestIdKey)))
|
|
return
|
|
}
|
|
switch status {
|
|
case http.StatusBadRequest:
|
|
message := "Invalid task protocol request"
|
|
if taskErr != nil && taskErr.Message != "" && (taskErr.Code == "invalid_request" || strings.HasPrefix(taskErr.Code, "invalid_request")) {
|
|
message = taskErr.Message
|
|
}
|
|
respondPluginProtocolError(c, status, "invalid_request_error", message)
|
|
case http.StatusUnauthorized:
|
|
respondPluginProtocolError(c, status, "authentication_error", "Authentication failed")
|
|
case http.StatusForbidden:
|
|
respondPluginProtocolError(c, status, "permission_denied", "Task protocol request was denied")
|
|
case http.StatusTooManyRequests:
|
|
respondPluginProtocolError(c, status, "rate_limit_exceeded", "Too many requests")
|
|
default:
|
|
respondPluginProtocolError(c, status, "task_protocol_error", "Task protocol request failed")
|
|
}
|
|
}
|
|
|
|
func respondPluginProtocolError(c *gin.Context, status int, code, message string) {
|
|
c.JSON(status, gin.H{
|
|
"error": gin.H{
|
|
"message": message,
|
|
"type": "new_api_error",
|
|
"code": code,
|
|
},
|
|
})
|
|
}
|