Improve comments

This commit is contained in:
2025-10-26 10:34:36 +01:00
parent 836e918fc5
commit 356c5be2c6
4 changed files with 15 additions and 4 deletions

View File

@@ -12,11 +12,13 @@ import (
"time"
)
// errorResponse represents an error response returned by the API
type errorResponse struct {
Error string `json:"error"`
Details string `json:"details,omitempty"`
}
// writeError writes a JSON error response with the specified HTTP status code
func writeError(w http.ResponseWriter, status int, code, details string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
@@ -25,6 +27,7 @@ func writeError(w http.ResponseWriter, status int, code, details string) {
}
}
// writeJSON writes a JSON response with the specified HTTP status code
func writeJSON(w http.ResponseWriter, status int, data any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
@@ -33,6 +36,7 @@ func writeJSON(w http.ResponseWriter, status int, data any) {
}
}
// writeText writes a plain text response with the specified HTTP status code
func writeText(w http.ResponseWriter, status int, data string) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(status)
@@ -41,12 +45,14 @@ func writeText(w http.ResponseWriter, status int, data string) {
}
}
// Handler provides HTTP handlers for the llamactl server API
type Handler struct {
InstanceManager manager.InstanceManager
cfg config.AppConfig
httpClient *http.Client
}
// NewHandler creates a new Handler instance with the provided instance manager and configuration
func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler {
return &Handler{
InstanceManager: im,
@@ -57,6 +63,7 @@ func NewHandler(im manager.InstanceManager, cfg config.AppConfig) *Handler {
}
}
// getInstance retrieves an instance by name from the request query parameters
func (h *Handler) getInstance(r *http.Request) (*instance.Instance, error) {
name := r.URL.Query().Get("name")
validatedName, err := validation.ValidateInstanceName(name)
@@ -72,6 +79,8 @@ func (h *Handler) getInstance(r *http.Request) (*instance.Instance, error) {
return inst, nil
}
// ensureInstanceRunning ensures the instance is running by starting it if on-demand start is enabled
// It handles LRU eviction when the maximum number of running instances is reached
func (h *Handler) ensureInstanceRunning(inst *instance.Instance) error {
options := inst.GetOptions()
allowOnDemand := options != nil && options.OnDemandStart != nil && *options.OnDemandStart

View File

@@ -10,11 +10,12 @@ import (
"strings"
)
// ParseCommandRequest represents the request body for command parsing
// ParseCommandRequest represents the request body for backend command parsing
type ParseCommandRequest struct {
Command string `json:"command"`
}
// validateLlamaCppInstance validates that the instance specified in the request is a llama.cpp instance
func (h *Handler) validateLlamaCppInstance(r *http.Request) (*instance.Instance, error) {
inst, err := h.getInstance(r)
if err != nil {
@@ -33,6 +34,7 @@ func (h *Handler) validateLlamaCppInstance(r *http.Request) (*instance.Instance,
return inst, nil
}
// stripLlamaCppPrefix removes the llama.cpp proxy prefix from the request URL path
func (h *Handler) stripLlamaCppPrefix(r *http.Request, instName string) {
// Strip the "/llama-cpp/<name>" prefix from the request URL
prefix := fmt.Sprintf("/llama-cpp/%s", instName)

View File

@@ -6,7 +6,7 @@ import (
"github.com/go-chi/chi/v5"
)
// NodeResponse represents a sanitized node configuration for API responses
// NodeResponse represents a node configuration in API responses
type NodeResponse struct {
Address string `json:"address"`
}

View File

@@ -8,13 +8,13 @@ import (
"net/http"
)
// OpenAIListInstancesResponse represents the response structure for listing instances (models) in OpenAI format
// OpenAIListInstancesResponse represents the response structure for listing instances (models) in OpenAI-compatible format
type OpenAIListInstancesResponse struct {
Object string `json:"object"`
Data []OpenAIInstance `json:"data"`
}
// OpenAIInstance represents a single instance (model) in OpenAI format
// OpenAIInstance represents a single instance (model) in OpenAI-compatible format
type OpenAIInstance struct {
ID string `json:"id"`
Object string `json:"object"`