Skip to content

Commit db94c4a

Browse files
committed
Clean up Responses handler helpers
1 parent 3f137f6 commit db94c4a

1 file changed

Lines changed: 22 additions & 29 deletions

File tree

pkg/responses/handler.go

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,25 @@ func NewHTTPHandler(log logging.Logger, schedulerHTTP http.Handler, allowedOrigi
3333
maxRequestBodyBytes: 10 * 1024 * 1024, // Default to 10MB
3434
}
3535

36-
// Register routes
37-
h.router.HandleFunc("POST "+APIPrefix, h.handleCreate)
38-
h.router.HandleFunc("GET "+APIPrefix+"/{id}", h.handleGet)
39-
h.router.HandleFunc("GET "+APIPrefix+"/{id}/input_items", h.handleListInputItems)
40-
h.router.HandleFunc("DELETE "+APIPrefix+"/{id}", h.handleDelete)
41-
// Also register /v1/responses routes
42-
h.router.HandleFunc("POST /v1"+APIPrefix, h.handleCreate)
43-
h.router.HandleFunc("GET /v1"+APIPrefix+"/{id}", h.handleGet)
44-
h.router.HandleFunc("GET /v1"+APIPrefix+"/{id}/input_items", h.handleListInputItems)
45-
h.router.HandleFunc("DELETE /v1"+APIPrefix+"/{id}", h.handleDelete)
46-
// Also register /engines/responses and /engines/v1/responses routes.
47-
h.router.HandleFunc("POST /engines"+APIPrefix, h.handleCreate)
48-
h.router.HandleFunc("GET /engines"+APIPrefix+"/{id}", h.handleGet)
49-
h.router.HandleFunc("GET /engines"+APIPrefix+"/{id}/input_items", h.handleListInputItems)
50-
h.router.HandleFunc("DELETE /engines"+APIPrefix+"/{id}", h.handleDelete)
51-
h.router.HandleFunc("POST /engines/v1"+APIPrefix, h.handleCreate)
52-
h.router.HandleFunc("GET /engines/v1"+APIPrefix+"/{id}", h.handleGet)
53-
h.router.HandleFunc("GET /engines/v1"+APIPrefix+"/{id}/input_items", h.handleListInputItems)
54-
h.router.HandleFunc("DELETE /engines/v1"+APIPrefix+"/{id}", h.handleDelete)
55-
h.router.HandleFunc("POST /engines/{backend}/v1"+APIPrefix, h.handleCreate)
56-
h.router.HandleFunc("GET /engines/{backend}/v1"+APIPrefix+"/{id}", h.handleGet)
57-
h.router.HandleFunc("GET /engines/{backend}/v1"+APIPrefix+"/{id}/input_items", h.handleListInputItems)
58-
h.router.HandleFunc("DELETE /engines/{backend}/v1"+APIPrefix+"/{id}", h.handleDelete)
36+
h.registerRoutes(APIPrefix)
37+
h.registerRoutes("/v1" + APIPrefix)
38+
h.registerRoutes("/engines" + APIPrefix)
39+
h.registerRoutes("/engines/v1" + APIPrefix)
40+
h.registerRoutes("/engines/{backend}/v1" + APIPrefix)
5941

6042
// Apply CORS middleware
6143
h.httpHandler = middleware.CorsMiddleware(allowedOrigins, h.router)
6244

6345
return h
6446
}
6547

48+
func (h *HTTPHandler) registerRoutes(prefix string) {
49+
h.router.HandleFunc("POST "+prefix, h.handleCreate)
50+
h.router.HandleFunc("GET "+prefix+"/{id}", h.handleGet)
51+
h.router.HandleFunc("GET "+prefix+"/{id}/input_items", h.handleListInputItems)
52+
h.router.HandleFunc("DELETE "+prefix+"/{id}", h.handleDelete)
53+
}
54+
6655
// Close releases resources held by the handler, including the background
6756
// store cleanup goroutine. It should be called when the handler is shut down.
6857
func (h *HTTPHandler) Close() {
@@ -380,13 +369,13 @@ func chatCompletionPathForRequest(r *http.Request) string {
380369

381370
func validateUnsupportedRequestFields(req *CreateRequest) error {
382371
if len(req.Include) > 0 {
383-
return fmt.Errorf("include is not supported by Docker Model Runner Responses compatibility layer")
372+
return unsupportedFieldError("include")
384373
}
385374
if req.StreamOptions != nil && !isNullJSON(req.StreamOptions) {
386-
return fmt.Errorf("stream_options is not supported by Docker Model Runner Responses compatibility layer")
375+
return unsupportedFieldError("stream_options")
387376
}
388377
if req.TopLogprobs != nil {
389-
return fmt.Errorf("top_logprobs is not supported by Docker Model Runner Responses compatibility layer")
378+
return unsupportedFieldError("top_logprobs")
390379
}
391380
switch req.Truncation {
392381
case "", "disabled":
@@ -400,17 +389,21 @@ func validateUnsupportedRequestFields(req *CreateRequest) error {
400389
return fmt.Errorf("conversation is not supported by Docker Model Runner Responses compatibility layer; use previous_response_id instead")
401390
}
402391
if req.Prompt != nil && !isNullJSON(req.Prompt) {
403-
return fmt.Errorf("prompt is not supported by Docker Model Runner Responses compatibility layer")
392+
return unsupportedFieldError("prompt")
404393
}
405394
if req.ServiceTier != "" {
406-
return fmt.Errorf("service_tier is not supported by Docker Model Runner Responses compatibility layer")
395+
return unsupportedFieldError("service_tier")
407396
}
408397
if req.SafetyIdentifier != "" {
409-
return fmt.Errorf("safety_identifier is not supported by Docker Model Runner Responses compatibility layer")
398+
return unsupportedFieldError("safety_identifier")
410399
}
411400
return nil
412401
}
413402

403+
func unsupportedFieldError(field string) error {
404+
return fmt.Errorf("%s is not supported by Docker Model Runner Responses compatibility layer", field)
405+
}
406+
414407
func isNullJSON(raw json.RawMessage) bool {
415408
return strings.TrimSpace(string(raw)) == "null"
416409
}

0 commit comments

Comments
 (0)