feat(ai): add provider connection test (#38)
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -70,10 +71,34 @@ func (p *anthropicProvider) Summarize(ctx context.Context, prompt string, _ GenO
|
||||
return result.Content[0].Text, nil
|
||||
}
|
||||
|
||||
func (p *anthropicProvider) ListModels(_ context.Context) ([]string, error) {
|
||||
return []string{
|
||||
"claude-opus-4-7",
|
||||
"claude-sonnet-4-6",
|
||||
"claude-haiku-4-5-20251001",
|
||||
}, nil
|
||||
// ListModels checks API authentication using discovery, never generation.
|
||||
func (p *anthropicProvider) ListModels(ctx context.Context) ([]string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.anthropic.com/v1/models", nil)
|
||||
if err != nil {
|
||||
return nil, errors.New("model discovery request failed")
|
||||
}
|
||||
req.Header.Set("x-api-key", p.apiKey)
|
||||
req.Header.Set("anthropic-version", "2023-06-01")
|
||||
resp, err := p.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errors.New("model discovery unavailable")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, &summaryHTTPError{status: resp.StatusCode, err: errors.New("model discovery failed")}
|
||||
}
|
||||
var result struct {
|
||||
Data []struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"data"`
|
||||
}
|
||||
// Discovery responses are bounded; malformed bodies never reach callers.
|
||||
if err := json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(&result); err != nil {
|
||||
return nil, errors.New("model discovery response invalid")
|
||||
}
|
||||
names := make([]string, 0, len(result.Data))
|
||||
for _, model := range result.Data {
|
||||
names = append(names, model.ID)
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user