MCPcopy Create free account
hub / github.com/PatchMon/PatchMon / Complete

Method Complete

server-source-code/internal/handler/ai.go:315–356  ·  view source on GitHub ↗

Complete handles command completion requests.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

313
314// Complete handles command completion requests.
315func (h *AIHandler) Complete(w http.ResponseWriter, r *http.Request) {
316 userID, _ := r.Context().Value(middleware.UserIDKey).(string)
317 if !h.checkRateLimit(r.Context(), userID) {
318 Error(w, http.StatusTooManyRequests, "Rate limit exceeded")
319 return
320 }
321
322 s, err := h.settings.GetFirst(r.Context())
323 if err != nil {
324 Error(w, http.StatusInternalServerError, "Failed to load settings")
325 return
326 }
327 if !s.AiEnabled {
328 Error(w, http.StatusBadRequest, "AI assistant is not enabled")
329 return
330 }
331 if s.AiAPIKey == nil || *s.AiAPIKey == "" {
332 Error(w, http.StatusBadRequest, "AI API key not configured")
333 return
334 }
335
336 var req CompleteRequest
337 if err := decodeJSON(r, &req); err != nil {
338 Error(w, http.StatusBadRequest, "Invalid request body")
339 return
340 }
341 req.Input = strings.TrimSpace(req.Input)
342 if len(req.Input) < 2 || len(req.Input) > 500 {
343 Error(w, http.StatusBadRequest, "input must be 2-500 characters")
344 return
345 }
346 if len(req.Context) > 5000 {
347 req.Context = req.Context[:5000]
348 }
349
350 completion, err := h.aiSvc.GetCompletion(s, req.Input, req.Context)
351 if err != nil {
352 Error(w, http.StatusInternalServerError, "Completion request failed: "+err.Error())
353 return
354 }
355 JSON(w, http.StatusOK, map[string]interface{}{"completion": completion})
356}

Callers

nothing calls this directly

Calls 8

checkRateLimitMethod · 0.95
ErrorFunction · 0.85
decodeJSONFunction · 0.85
GetFirstMethod · 0.80
GetCompletionMethod · 0.80
ErrorMethod · 0.80
JSONFunction · 0.70
ValueMethod · 0.45

Tested by

no test coverage detected