package llamaswap import ( "context" "fmt" "io" "net/http" ) // Health reports whether the llama-swap instance is reachable (GET // {base}/health, which answers "OK" without touching any model). A nil error // means the proxy itself is up — it says nothing about how long a subsequent // request will take (a cold model swap can still block for minutes). Bound // the probe with a short context deadline; the client has no timeout by // design. func (p *Provider) Health(ctx context.Context) error { if p.baseURL == "" { return fmt.Errorf("llama-swap provider %q: no base URL configured (set one via WithBaseURL or an LLM_* env DSN)", p.name) } req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.baseURL+"/health", nil) if err != nil { return fmt.Errorf("llama-swap: build request: %w", err) } if p.token != "" { req.Header.Set("Authorization", "Bearer "+p.token) } resp, err := p.client.Do(req) if err != nil { return fmt.Errorf("llama-swap: health probe: %w", err) } defer resp.Body.Close() _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<10)) if resp.StatusCode/100 != 2 { return &apiHealthError{status: resp.StatusCode} } return nil } // apiHealthError distinguishes "reachable but unhealthy" from transport // failure without pulling in the llm error taxonomy for a probe. type apiHealthError struct{ status int } func (e *apiHealthError) Error() string { return fmt.Sprintf("llama-swap: health endpoint returned status %d", e.status) }