| 161 | } |
| 162 | |
| 163 | func serveTaskPluginProtocol( |
| 164 | c *gin.Context, |
| 165 | pinned pluginruntime.PinnedEndpoint, |
| 166 | deps pluginProtocolBridgeDeps, |
| 167 | ) { |
| 168 | deps = deps.withDefaults() |
| 169 | generation := uint64(0) |
| 170 | if pinned.Generation != nil { |
| 171 | generation = pinned.Generation.Number |
| 172 | } |
| 173 | pluginKey := "" |
| 174 | if pinned.Plugin != nil { |
| 175 | pluginKey = pinned.Plugin.Meta.Key |
| 176 | } |
| 177 | logger.LogDebug( |
| 178 | c, |
| 179 | "task_plugin subsystem=protocol event=bridge_start generation=%d plugin=%q protocol=%q model=%q", |
| 180 | generation, |
| 181 | pluginKey, |
| 182 | pinned.Protocol, |
| 183 | c.GetString("resolved_task_model"), |
| 184 | ) |
| 185 | if !pluginruntime.SupportsHostProtocol(pinned.Protocol) { |
| 186 | logger.LogDebug(c, "task_plugin subsystem=protocol event=bridge_rejected generation=%d plugin=%q reason=unsupported_protocol", generation, pluginKey) |
| 187 | respondPluginProtocolError(c, http.StatusNotImplemented, "task_protocol_not_available", "Task protocol bridge is not available") |
| 188 | return |
| 189 | } |
| 190 | requestValue, exists := c.Get(pluginruntime.ContextKeyProtocolRequest) |
| 191 | protocolRequest, ok := requestValue.(pluginruntime.ProtocolRequestContext) |
| 192 | if !exists || !ok || protocolRequest.Protocol != pinned.Protocol { |
| 193 | logger.LogDebug(c, "task_plugin subsystem=protocol event=bridge_rejected generation=%d plugin=%q reason=invalid_protocol_context", generation, pluginKey) |
| 194 | respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed") |
| 195 | return |
| 196 | } |
| 197 | if definition, known := pluginruntime.HostProtocol(pinned.Protocol); known && len(definition.DefinedModes()) > 0 && pinned.Plugin != nil { |
| 198 | background := false |
| 199 | if body, ok := protocolRequest.Body.(map[string]any); ok && body["kind"] == string(pluginruntime.BodyJSON) { |
| 200 | if requestBody, ok := body["value"].(map[string]any); ok { |
| 201 | background, _ = requestBody["background"].(bool) |
| 202 | } |
| 203 | } |
| 204 | missing := false |
| 205 | if protocolRequest.Stream && !pinned.Plugin.Meta.ProtocolSupports(pinned.Protocol, "stream") { |
| 206 | missing = true |
| 207 | } |
| 208 | if background && !pinned.Plugin.Meta.ProtocolSupports(pinned.Protocol, "background") { |
| 209 | missing = true |
| 210 | } |
| 211 | if !protocolRequest.Stream && !background && !pinned.Plugin.Meta.ProtocolSupports(pinned.Protocol, "sync") { |
| 212 | missing = true |
| 213 | } |
| 214 | if missing { |
| 215 | logger.LogError(c, common.LogText("pinned task plugin does not support the requested protocol form")) |
| 216 | respondPluginProtocolError(c, http.StatusInternalServerError, "task_protocol_error", "Task protocol request failed") |
| 217 | return |
| 218 | } |
| 219 | } |
| 220 | logger.LogDebug( |