feat: publish sanitized custom.97 source
This commit is contained in:
1022 files changed
+69903
-19207
No files matched your search
@@ -42,8 +42,19 @@ type pluginProtocolBridgeDeps struct {
|
||||
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 {
|
||||
@@ -78,6 +89,9 @@ func defaultPluginProtocolBridgeDeps() pluginProtocolBridgeDeps {
|
||||
admissionTimeout: pluginruntime.DefaultCallTimeout,
|
||||
getByTaskId: model.GetByTaskId,
|
||||
resolvePlugin: resolveTaskPluginForProtocolRetrieve,
|
||||
imagePollInterval: taskPluginImagePollInterval,
|
||||
pollTask: pollTaskPluginImageTask,
|
||||
downloadImage: service.GetImageFromUrl,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +142,15 @@ func (d pluginProtocolBridgeDeps) withDefaults() pluginProtocolBridgeDeps {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -189,7 +212,7 @@ func serveTaskPluginProtocol(
|
||||
missing = true
|
||||
}
|
||||
if missing {
|
||||
logger.LogError(c, "pinned task plugin does not support the requested protocol form")
|
||||
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
|
||||
}
|
||||
@@ -270,7 +293,7 @@ func serveTaskPluginProtocol(
|
||||
}
|
||||
if relayInfoErr != nil {
|
||||
err := relayInfoErr
|
||||
logger.LogError(c, "build task protocol relay info failed: "+err.Error())
|
||||
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
|
||||
@@ -304,7 +327,7 @@ func serveTaskPluginProtocol(
|
||||
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, "task protocol submission returned an invalid durable outcome")
|
||||
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
|
||||
@@ -342,7 +365,7 @@ func serveTaskPluginProtocol(
|
||||
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, "persist task background flag failed: "+err.Error())
|
||||
logger.LogError(c, common.LogText("persist task background flag failed: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
machine.SetBackground(true)
|
||||
@@ -424,7 +447,7 @@ func streamTaskPluginProtocol(
|
||||
if errors.Is(loadContextErr, context.DeadlineExceeded) &&
|
||||
observationContext.Err() == nil &&
|
||||
c.Request.Context().Err() == nil {
|
||||
logger.LogWarn(c, fmt.Sprintf(
|
||||
logger.LogWarn(c, common.LogText(
|
||||
"task protocol database observation overloaded; plugin=%s task=%s",
|
||||
pinned.Plugin.Meta.Key,
|
||||
taskID,
|
||||
@@ -459,7 +482,7 @@ func streamTaskPluginProtocol(
|
||||
return
|
||||
}
|
||||
if loadErr != nil && !errors.Is(loadErr, context.Canceled) {
|
||||
logger.LogError(c, "task protocol database observation failed")
|
||||
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)
|
||||
@@ -485,32 +508,26 @@ func streamTaskPluginProtocol(
|
||||
}
|
||||
view, viewErr := service.BuildTaskPluginView(task)
|
||||
if viewErr != nil {
|
||||
logger.LogError(c, "build task protocol view failed: "+viewErr.Error())
|
||||
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, "encode task protocol view failed: "+viewErr.Error())
|
||||
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, "build task protocol renderer context failed")
|
||||
logger.LogError(c, common.LogText("build task protocol renderer context failed"))
|
||||
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
||||
return
|
||||
}
|
||||
args := []any{rendererContext, viewValue}
|
||||
if previous.Present {
|
||||
previousValue, stateErr := previous.PluginValue()
|
||||
if stateErr != nil {
|
||||
logger.LogError(c, "decode task protocol state failed: "+stateErr.Error())
|
||||
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
||||
return
|
||||
}
|
||||
args = append(args, previousValue)
|
||||
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)
|
||||
@@ -527,13 +544,13 @@ func streamTaskPluginProtocol(
|
||||
}
|
||||
if errors.Is(callErr, pluginruntime.ErrCallAdmissionTimeout) {
|
||||
overloaded = true
|
||||
logger.LogWarn(c, fmt.Sprintf(
|
||||
logger.LogWarn(c, common.LogText(
|
||||
"task protocol render hook overloaded; plugin=%s task=%s",
|
||||
pinned.Plugin.Meta.Key,
|
||||
taskID,
|
||||
))
|
||||
} else {
|
||||
logger.LogError(c, "task protocol render hook failed")
|
||||
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",
|
||||
@@ -548,13 +565,13 @@ func streamTaskPluginProtocol(
|
||||
if !overloaded {
|
||||
result, decodeErr := relay.DecodePluginProtocolEventResult(value, deps.protocolLimits)
|
||||
if decodeErr != nil {
|
||||
logger.LogError(c, "task protocol render result invalid: "+decodeErr.Error())
|
||||
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, "task protocol state transition failed: "+applyErr.Error())
|
||||
logger.LogError(c, common.LogText("task protocol state transition failed: %s", applyErr.Error()))
|
||||
writeTaskPluginProtocolFailure(c, machine, lastStatus)
|
||||
return
|
||||
}
|
||||
@@ -590,7 +607,7 @@ func streamTaskPluginProtocol(
|
||||
delay += deps.tickInterval
|
||||
} else if hookElapsed > deps.tickInterval {
|
||||
delay += deps.tickInterval
|
||||
logger.LogWarn(c, fmt.Sprintf(
|
||||
logger.LogWarn(c, common.LogText(
|
||||
"task protocol render hook slow; plugin=%s task=%s elapsed_ms=%d",
|
||||
pinned.Plugin.Meta.Key,
|
||||
taskID,
|
||||
@@ -675,7 +692,7 @@ func waitTaskPluginProtocol(
|
||||
observationContext.Err() == nil &&
|
||||
c.Request.Context().Err() == nil
|
||||
if loadOverloaded {
|
||||
logger.LogWarn(c, fmt.Sprintf(
|
||||
logger.LogWarn(c, common.LogText(
|
||||
"task protocol database observation overloaded; plugin=%s task=%s",
|
||||
pinned.Plugin.Meta.Key,
|
||||
taskID,
|
||||
@@ -695,7 +712,7 @@ func waitTaskPluginProtocol(
|
||||
return
|
||||
}
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
logger.LogError(c, "task protocol database observation failed")
|
||||
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)
|
||||
@@ -756,13 +773,13 @@ func waitTaskPluginProtocol(
|
||||
}
|
||||
if errors.Is(callErr, pluginruntime.ErrCallAdmissionTimeout) {
|
||||
overloaded = true
|
||||
logger.LogWarn(c, fmt.Sprintf(
|
||||
logger.LogWarn(c, common.LogText(
|
||||
"task protocol final hook overloaded; plugin=%s task=%s",
|
||||
pinned.Plugin.Meta.Key,
|
||||
taskID,
|
||||
))
|
||||
} else {
|
||||
logger.LogError(c, "task protocol final hook failed")
|
||||
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",
|
||||
@@ -939,7 +956,7 @@ func retrieveTaskPluginResponse(c *gin.Context, deps pluginProtocolBridgeDeps) {
|
||||
|
||||
task, exists, err := deps.getByTaskId(userID, taskID)
|
||||
if err != nil {
|
||||
logger.LogError(c, "task protocol retrieve lookup failed")
|
||||
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
|
||||
@@ -948,6 +965,10 @@ func retrieveTaskPluginResponse(c *gin.Context, deps pluginProtocolBridgeDeps) {
|
||||
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 {
|
||||
@@ -1039,7 +1060,7 @@ func retrieveTaskPluginResponse(c *gin.Context, deps pluginProtocolBridgeDeps) {
|
||||
)
|
||||
}
|
||||
if renderErr != nil {
|
||||
logger.LogError(c, "task protocol retrieve render failed")
|
||||
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",
|
||||
@@ -1082,7 +1103,7 @@ func writeTaskPluginProtocolFailure(
|
||||
) {
|
||||
failed, err := machine.FailureEvent(taskStatus)
|
||||
if err != nil {
|
||||
logger.LogError(c, "task protocol failure event failed: "+err.Error())
|
||||
logger.LogError(c, common.LogText("task protocol failure event failed: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
_ = writeTaskPluginProtocolEvent(c, failed)
|
||||
@@ -1095,7 +1116,7 @@ func writeTaskPluginProtocolTimeout(
|
||||
) {
|
||||
incomplete, err := machine.TimeoutEvent(taskStatus)
|
||||
if err != nil {
|
||||
logger.LogError(c, "task protocol timeout event failed: "+err.Error())
|
||||
logger.LogError(c, common.LogText("task protocol timeout event failed: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
_ = writeTaskPluginProtocolEvent(c, incomplete)
|
||||
@@ -1109,7 +1130,7 @@ func writeTaskPluginProtocolFailureResponse(
|
||||
if taskStatus == string(model.TaskStatusFailure) {
|
||||
response, err := machine.FinalResponse(nil, taskStatus)
|
||||
if err != nil {
|
||||
logger.LogError(c, "task protocol terminal failure response failed: "+err.Error())
|
||||
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
|
||||
}
|
||||
@@ -1118,7 +1139,7 @@ func writeTaskPluginProtocolFailureResponse(
|
||||
}
|
||||
response, err := machine.FailureResponse(taskStatus)
|
||||
if err != nil {
|
||||
logger.LogError(c, "task protocol failure response failed: "+err.Error())
|
||||
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
|
||||
}
|
||||
@@ -1132,23 +1153,21 @@ func writeTaskPluginProtocolTimeoutResponse(
|
||||
) {
|
||||
response, err := machine.TimeoutResponse(lastStatus)
|
||||
if err != nil {
|
||||
logger.LogError(c, "task protocol timeout response failed: "+err.Error())
|
||||
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)
|
||||
}
|
||||
|
||||
func taskPluginProtocolJSONValue(value any) (any, error) {
|
||||
encoded, err := common.Marshal(value)
|
||||
// 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
|
||||
}
|
||||
var decoded any
|
||||
if err = common.Unmarshal(encoded, &decoded); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decoded, nil
|
||||
return pluginruntime.RawJSON(encoded), nil
|
||||
}
|
||||
|
||||
func taskPluginProtocolRendererContext(
|
||||
|
||||
Reference in new issue
Block a user