Files
ai-service/internal/httpapi/providers.go
Grendgi e0f74c62b0
All checks were successful
CI / test (push) Successful in 12s
Build and Deploy / build-and-deploy (push) Successful in 22s
Remove OpenClaw provider wiring
2026-06-08 13:48:32 +03:00

120 lines
2.9 KiB
Go

package httpapi
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type providerStatus struct {
Name string `json:"name"`
Configured bool `json:"configured"`
OK bool `json:"ok"`
URL string `json:"url,omitempty"`
Model string `json:"model,omitempty"`
LatencyMS int64 `json:"latency_ms,omitempty"`
Error string `json:"error,omitempty"`
}
type providersStatusResponse struct {
At time.Time `json:"at"`
Providers []providerStatus `json:"providers"`
}
func (s *Server) handleProviderStatus(w http.ResponseWriter, r *http.Request) {
ctx, cancel := contextWithTimeout(r, 8*time.Second)
defer cancel()
resp := providersStatusResponse{
At: time.Now().UTC(),
Providers: []providerStatus{
s.checkLLM(ctx),
s.checkWhisperX(ctx),
},
}
writeJSON(w, http.StatusOK, resp)
}
func (s *Server) checkLLM(ctx context.Context) providerStatus {
st := providerStatus{
Name: "llm",
Configured: strings.TrimSpace(s.cfg.LLMBaseURL) != "",
URL: strings.TrimRight(s.cfg.LLMBaseURL, "/"),
Model: s.cfg.LLMModel,
}
if !st.Configured {
return st
}
start := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, st.URL+"/v1/models", nil)
if err != nil {
st.Error = err.Error()
return st
}
if s.cfg.LLMAPIKey != "" {
req.Header.Set("Authorization", "Bearer "+s.cfg.LLMAPIKey)
}
httpClient := &http.Client{Timeout: s.cfg.LLMTimeout}
res, err := httpClient.Do(req)
st.LatencyMS = time.Since(start).Milliseconds()
if err != nil {
st.Error = err.Error()
return st
}
defer res.Body.Close()
if res.StatusCode >= 300 {
st.Error = fmt.Sprintf("http %d: %s", res.StatusCode, readSmallBody(res.Body))
return st
}
st.OK = true
return st
}
func (s *Server) checkWhisperX(ctx context.Context) providerStatus {
baseURL := strings.TrimRight(strings.TrimSpace(s.cfg.WhisperXURL), "/")
st := providerStatus{Name: "whisperx", Configured: baseURL != "", URL: baseURL}
if !st.Configured {
return st
}
start := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/health", nil)
if err != nil {
st.Error = err.Error()
return st
}
res, err := http.DefaultClient.Do(req)
st.LatencyMS = time.Since(start).Milliseconds()
if err != nil {
st.Error = err.Error()
return st
}
defer res.Body.Close()
if res.StatusCode >= 300 {
st.Error = fmt.Sprintf("http %d: %s", res.StatusCode, readSmallBody(res.Body))
return st
}
st.OK = true
return st
}
func readSmallBody(r io.Reader) string {
body, err := io.ReadAll(io.LimitReader(r, 512))
if err != nil {
return err.Error()
}
var parsed map[string]any
if err := json.Unmarshal(body, &parsed); err == nil {
if msg, ok := parsed["error"].(string); ok {
return msg
}
if msg, ok := parsed["detail"].(string); ok {
return msg
}
}
return strings.TrimSpace(string(body))
}