feat(provider): add NEAR AI Cloud provider

This commit is contained in:
Pierre LE GUEN committed 2026-06-12 10:18:34 +00:00
1 parent 413d37494b
commit 7de3d1f7e3
10 files changed
+213 -2

No files matched your search

+52 -1
View File
@@ -734,9 +734,10 @@ type upstreamModel struct {
func fetchUpstreamModels(ctx context.Context, provider, apiBase, apiKey string) ([]upstreamModel, error) {
apiBase = strings.TrimRight(strings.TrimSpace(apiBase), "/")
provider = providers.NormalizeProvider(provider)
var fetchURL string
switch strings.ToLower(provider) {
switch provider {
case "ollama":
// Strip /v1 suffix if present to get the Ollama root
root := apiBase
@@ -746,6 +747,9 @@ func fetchUpstreamModels(ctx context.Context, provider, apiBase, apiKey string)
root = strings.TrimRight(root, "/")
fetchURL = root + "/api/tags"
return fetchOllamaModels(ctx, fetchURL)
case "nearai":
fetchURL = apiBase + "/model/list"
return fetchNearAIModels(ctx, fetchURL, apiKey)
default:
// OpenAI-compatible: /v1/models
fetchURL = apiBase + "/models"
@@ -753,6 +757,53 @@ func fetchUpstreamModels(ctx context.Context, provider, apiBase, apiKey string)
}
}
func fetchNearAIModels(ctx context.Context, fetchURL, apiKey string) ([]upstreamModel, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fetchURL, nil)
if err != nil {
return nil, err
}
if apiKey = strings.TrimSpace(apiKey); apiKey != "" {
req.Header.Set("Authorization", "Bearer "+apiKey)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("nearai returned status %d", resp.StatusCode)
}
var parsed struct {
Models []struct {
ModelID string `json:"modelId"`
OwnedBy string `json:"ownedBy"`
Metadata struct {
OwnedBy string `json:"ownedBy"`
} `json:"metadata"`
} `json:"models"`
}
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
return nil, err
}
models := make([]upstreamModel, 0, len(parsed.Models))
for _, m := range parsed.Models {
id := strings.TrimSpace(m.ModelID)
if id == "" {
continue
}
ownedBy := strings.TrimSpace(m.OwnedBy)
if ownedBy == "" {
ownedBy = strings.TrimSpace(m.Metadata.OwnedBy)
}
models = append(models, upstreamModel{ID: id, OwnedBy: ownedBy})
}
return models, nil
}
func fetchOpenAICompatibleModels(ctx context.Context, fetchURL, apiKey string) ([]upstreamModel, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fetchURL, nil)
if err != nil {
+66
View File
@@ -1961,6 +1961,13 @@ func TestHandleListModels_ReturnsProviderOptionsWithoutPersistingLegacyMigration
"https://api.siliconflow.cn/v1",
)
}
if option, ok := optionsByID["nearai"]; !ok {
t.Fatal("nearai provider option missing")
} else if option.DefaultAPIBase != "https://cloud-api.near.ai/v1" {
t.Fatalf("nearai default_api_base = %q, want %q", option.DefaultAPIBase, "https://cloud-api.near.ai/v1")
} else if !option.SupportsFetch {
t.Fatal("nearai provider option should report supports_fetch")
}
if option, ok := optionsByID["bedrock"]; !ok {
t.Fatal("bedrock provider option missing")
} else if !option.CreateAllowed {
@@ -2592,6 +2599,65 @@ func TestHandleFetchModels_SiliconFlowUsesOpenAICompatibleEndpoint(t *testing.T)
}
}
func TestHandleFetchModels_NearAIUsesPublicModelListEndpoint(t *testing.T) {
configPath, cleanup := setupOAuthTestEnv(t)
defer cleanup()
var gotPath string
var gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
gotAuth = r.Header.Get("Authorization")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"models":[`+
`{"modelId":"zai-org/GLM-5.1-FP8","metadata":{"ownedBy":"nearai"}},`+
`{"modelId":"openai/gpt-oss-120b","metadata":{"ownedBy":"nearai"}},`+
`{"modelId":""}]}`)
}))
defer srv.Close()
h := NewHandler(configPath)
mux := http.NewServeMux()
h.RegisterRoutes(mux)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/api/models/fetch", bytes.NewBufferString(fmt.Sprintf(`{
"provider":"nearai",
"api_key":"nearai-key",
"api_base":"%s"
}`, srv.URL)))
req.Header.Set("Content-Type", "application/json")
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want %d, body=%s", rec.Code, http.StatusOK, rec.Body.String())
}
if gotPath != "/model/list" {
t.Fatalf("path = %q, want %q", gotPath, "/model/list")
}
if gotAuth != "Bearer nearai-key" {
t.Fatalf("Authorization = %q, want %q", gotAuth, "Bearer nearai-key")
}
var resp struct {
Models []upstreamModel `json:"models"`
Total int `json:"total"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("Unmarshal() error = %v", err)
}
if resp.Total != 2 || len(resp.Models) != 2 {
t.Fatalf("response = %+v, want two fetched models", resp)
}
if resp.Models[0].ID != "zai-org/GLM-5.1-FP8" || resp.Models[0].OwnedBy != "nearai" {
t.Fatalf("models[0] = %+v, want GLM model owned by nearai", resp.Models[0])
}
if resp.Models[1].ID != "openai/gpt-oss-120b" || resp.Models[1].OwnedBy != "nearai" {
t.Fatalf("models[1] = %+v, want GPT OSS model owned by nearai", resp.Models[1])
}
}
func TestHandleFetchModels_ModelIndexUsesStoredKey(t *testing.T) {
var gotAuth string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {