Complete handles command completion requests.
(w http.ResponseWriter, r *http.Request)
| 313 | |
| 314 | // Complete handles command completion requests. |
| 315 | func (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 | } |
nothing calls this directly
no test coverage detected