serveTaskPluginImageProtocol answers an OpenAI Images API request that a task plugin claimed. The API is synchronous: the durable submission runs first on a context that survives a client disconnect, an asynchronous upstream task is then polled inside the request until it is terminal or the protocol
(c *gin.Context, pinned pluginruntime.PinnedEndpoint, deps pluginProtocolBridgeDeps)
| 29 | // protocol timeout elapses, and the plugin's render hook finally shapes the |
| 30 | // OpenAI image response from the terminal task. |
| 31 | func serveTaskPluginImageProtocol(c *gin.Context, pinned pluginruntime.PinnedEndpoint, deps pluginProtocolBridgeDeps) { |
| 32 | deps = deps.withDefaults() |
| 33 | requestValue, exists := c.Get(pluginruntime.ContextKeyProtocolRequest) |
| 34 | protocolRequest, ok := requestValue.(pluginruntime.ProtocolRequestContext) |
| 35 | if !exists || !ok || protocolRequest.Protocol != pinned.Protocol || pinned.Plugin == nil { |
| 36 | respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed") |
| 37 | return |
| 38 | } |
| 39 | clientRequest := c.Request |
| 40 | var relayInfo *relaycommon.RelayInfo |
| 41 | var relayInfoErr error |
| 42 | var outcome *taskSubmissionOutcome |
| 43 | var taskErr *dto.TaskError |
| 44 | // The request plugins' report hooks receive the submission once the |
| 45 | // client has the response. |
| 46 | defer func() { |
| 47 | requestplugin.Report(c, types.RelayFormatTask, relayInfo, nil, taskReportError(taskErr)) |
| 48 | }() |
| 49 | // DashScope bills the images as soon as it accepts the upstream call. A |
| 50 | // client that gives up while the submission is in flight must not cancel |
| 51 | // that call, the task row, its settlement, or its consume log, so the |
| 52 | // submission runs on its own bounded context exactly like the Responses |
| 53 | // bridge. Only the in-request wait and the response write observe the |
| 54 | // client connection. |
| 55 | func() { |
| 56 | submissionContext, cancelSubmission := context.WithTimeout( |
| 57 | context.WithoutCancel(clientRequest.Context()), |
| 58 | deps.submissionTimeout, |
| 59 | ) |
| 60 | c.Request = clientRequest.Clone(submissionContext) |
| 61 | defer func() { |
| 62 | c.Request = clientRequest |
| 63 | cancelSubmission() |
| 64 | }() |
| 65 | |
| 66 | relayInfo, relayInfoErr = relaycommon.GenRelayInfo(c, types.RelayFormatTask, nil, nil) |
| 67 | if relayInfoErr != nil { |
| 68 | return |
| 69 | } |
| 70 | relayInfo.IsStream = false |
| 71 | relayInfo.OriginModelName = c.GetString("resolved_task_model") |
| 72 | if action := c.GetString("task_action"); action != "" { |
| 73 | relayInfo.Action = action |
| 74 | } |
| 75 | if taskErr = relay.ResolveOriginTask(c, relayInfo); taskErr != nil { |
| 76 | return |
| 77 | } |
| 78 | if taskErr = relay.ApplyOriginTaskAffinity(c, relayInfo); taskErr != nil { |
| 79 | return |
| 80 | } |
| 81 | outcome, taskErr = deps.submit(c, relayInfo) |
| 82 | }() |
| 83 | if clientRequest.Context().Err() != nil { |
| 84 | // The client is gone. A durable task keeps its settlement and consume |
| 85 | // log; an asynchronous one is finished by the background poller. |
| 86 | return |
| 87 | } |
| 88 | if relayInfoErr != nil { |