(c echo.Context)
| 208 | } |
| 209 | |
| 210 | func (re *RequestExtractor) SetOpenAIRequest(c echo.Context) error { |
| 211 | input, ok := c.Get(CONTEXT_LOCALS_KEY_LOCALAI_REQUEST).(*schema.OpenAIRequest) |
| 212 | if !ok || input.Model == "" { |
| 213 | return echo.ErrBadRequest |
| 214 | } |
| 215 | |
| 216 | cfg, ok := c.Get(CONTEXT_LOCALS_KEY_MODEL_CONFIG).(*config.ModelConfig) |
| 217 | if !ok || cfg == nil { |
| 218 | return echo.ErrBadRequest |
| 219 | } |
| 220 | |
| 221 | // Extract or generate the correlation ID |
| 222 | correlationID := c.Request().Header.Get("X-Correlation-ID") |
| 223 | if correlationID == "" { |
| 224 | correlationID = uuid.New().String() |
| 225 | } |
| 226 | c.Response().Header().Set("X-Correlation-ID", correlationID) |
| 227 | |
| 228 | // Use the request context directly - Echo properly supports context cancellation! |
| 229 | // No need for workarounds like handleConnectionCancellation |
| 230 | reqCtx := c.Request().Context() |
| 231 | c1, cancel := context.WithCancel(re.applicationConfig.Context) |
| 232 | |
| 233 | // Cancel when request context is cancelled (client disconnects) |
| 234 | go func() { |
| 235 | select { |
| 236 | case <-reqCtx.Done(): |
| 237 | cancel() |
| 238 | case <-c1.Done(): |
| 239 | // Already cancelled |
| 240 | } |
| 241 | }() |
| 242 | |
| 243 | // Add the correlation ID to the new context |
| 244 | ctxWithCorrelationID := context.WithValue(c1, CorrelationIDKey, correlationID) |
| 245 | ctxWithCorrelationID = distributedhdr.Inherit(ctxWithCorrelationID, reqCtx) |
| 246 | |
| 247 | input.Context = ctxWithCorrelationID |
| 248 | input.Cancel = cancel |
| 249 | |
| 250 | err := mergeOpenAIRequestAndModelConfig(cfg, input) |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | |
| 255 | if cfg.Model == "" { |
| 256 | xlog.Debug("replacing empty cfg.Model with input value", "input.Model", input.Model) |
| 257 | cfg.Model = input.Model |
| 258 | } |
| 259 | |
| 260 | c.Set(CONTEXT_LOCALS_KEY_LOCALAI_REQUEST, input) |
| 261 | c.Set(CONTEXT_LOCALS_KEY_MODEL_CONFIG, cfg) |
| 262 | |
| 263 | return nil |
| 264 | } |
| 265 | |
| 266 | // extractToolChoiceFunctionName parses a tool_choice map and returns the |
| 267 | // specific function name. Accepts both the OpenAI-spec nested shape |
no test coverage detected