RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(go): paginate the Anthropic models list (#21899)
alexcastgonz334c6cf99ae7
diff --git a/internal/entity/models/anthropic.go b/internal/entity/models/anthropic.goindex 4d99c1265c..5591b11d70 100644--- a/internal/entity/models/anthropic.go+++ b/internal/entity/models/anthropic.go@@ -24,7 +24,9 @@ import ("fmt""io""net/http"+ "net/url""ragflow/internal/common"+ "strconv""strings")@@ -516,6 +518,14 @@ func parseAnthropicChatResponse(body []byte, _ *ChatConfig) (chatResponseParts,}, nil}+// anthropicModelsPageSize is the largest page the models endpoint allows; the+// default is 20, which silently truncated the list for accounts with more.+const anthropicModelsPageSize = 1000++// anthropicModelsMaxPages bounds the pagination loop so a server that keeps+// returning a fresh cursor cannot make ListModels (and CheckConnection) run forever.+const anthropicModelsMaxPages = 100+func (a *AnthropicModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]ListModelResponse, error) {if err := a.baseModel.APIConfigCheck(apiConfig); err != nil {return nil, err@@ -526,15 +536,58 @@ func (a *AnthropicModel) ListModels(ctx context.Context, apiConfig *APIConfig) (baseURLConfig := &APIConfig{Region: &baseURLRegion}baseURLConfig.BaseURL = apiConfig.BaseURL- url, err := a.baseModel.EndpointURL(baseURLConfig, a.baseModel.URLSuffix.Models)+ endpoint, err := a.baseModel.EndpointURL(baseURLConfig, a.baseModel.URLSuffix.Models)if err != nil {return nil, err}+ models := make([]ListModelResponse, 0)+ afterID := ""+ for pageNum := 0; pageNum < anthropicModelsMaxPages; pageNum++ {+ page, err := a.listModelsPage(ctx, endpoint, apiKey, afterID)+ if err != nil {+ return nil, err+ }+ for _, item := range page.Data {+ if item.ID != "" {+ models = append(models, ListModelResponse{+ Name: item.ID,+ })+ }+ }+ if !page.HasMore || page.LastID == "" || page.LastID == afterID {+ return models, nil+ }+ afterID = page.LastID+ }+ return nil, fmt.Errorf("models list exceeded %d pages", anthropicModelsMaxPages)+}++type anthropicModelsPage struct {+ Data []struct {+ ID string `json:"id"`+ } `json:"data"`+ HasMore bool `json:"has_more"`+ LastID string `json:"last_id"`+}++func (a *AnthropicModel) listModelsPage(ctx context.Context, endpoint, apiKey, afterID string) (*anthropicModelsPage, error) {+ query := url.Values{}+ query.Set("limit", strconv.Itoa(anthropicModelsPageSize))+ if afterID != "" {+ query.Set("after_id", afterID)+ }+ pageURL := endpoint+ if strings.Contains(pageURL, "?") {+ pageURL += "&" + query.Encode()+ } else {+ pageURL += "?" + query.Encode()+ }+ctx, cancel := context.WithTimeout(ctx, nonStreamCallTimeout)defer cancel()- req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, pageURL, nil)if err != nil {return nil, fmt.Errorf("failed to create request: %w", err)}@@ -554,23 +607,11 @@ func (a *AnthropicModel) ListModels(ctx context.Context, apiConfig *APIConfig) (return nil, fmt.Errorf("anthropic models API error: %s, body: %s", resp.Status, string(body))}- var result struct {- Data []struct {- ID string `json:"id"`- } `json:"data"`- }+ var result anthropicModelsPageif err = json.Unmarshal(body, &result); err != nil {return nil, fmt.Errorf("failed to parse response: %w", err)}- models := make([]ListModelResponse, 0, len(result.Data))- for _, item := range result.Data {- if item.ID != "" {- models = append(models, ListModelResponse{- Name: item.ID,- })- }- }- return models, nil+ return &result, nil}func (a *AnthropicModel) CheckConnection(ctx context.Context, apiConfig *APIConfig) error {diff --git a/internal/entity/models/anthropic_test.go b/internal/entity/models/anthropic_test.goindex 9805dd683a..f10bec044c 100644--- a/internal/entity/models/anthropic_test.go+++ b/internal/entity/models/anthropic_test.go@@ -2,6 +2,7 @@ package modelsimport ("encoding/json"+ "fmt""io""net/http""net/http/httptest"@@ -602,6 +603,75 @@ func TestAnthropicListModelsAndCheckConnection(t *testing.T) {}}+func TestAnthropicListModelsFollowsPagination(t *testing.T) {+ withSSRFBypass(t)+ ctx := t.Context()+ var afterIDs []string+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path != "/v1/models" {+ t.Errorf("unexpected path %s", r.URL.Path)+ return+ }+ if got := r.URL.Query().Get("limit"); got != "1000" {+ t.Errorf("expected limit=1000, got %q", got)+ }+ afterID := r.URL.Query().Get("after_id")+ afterIDs = append(afterIDs, afterID)+ switch afterID {+ case "":+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "data": []map[string]interface{}{{"id": "claude-a"}, {"id": "claude-b"}},+ "has_more": true,+ "last_id": "claude-b",+ })+ case "claude-b":+ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "data": []map[string]interface{}{{"id": "claude-c"}},+ "has_more": false,+ "last_id": "claude-c",+ })+ default:+ t.Errorf("unexpected after_id %q", afterID)+ }+ }))+ defer srv.Close()++ apiKey := "test-key"+ models, err := newAnthropicForTest(srv.URL).ListModels(ctx, &APIConfig{ApiKey: &apiKey})+ if err != nil {+ t.Fatalf("ListModels: %v", err)+ }+ if got := joinModelNames(models, ","); got != "claude-a,claude-b,claude-c" {+ t.Errorf("models=%s", got)+ }+ if strings.Join(afterIDs, "|") != "|claude-b" {+ t.Errorf("after_id sequence=%v", afterIDs)+ }+}++func TestAnthropicListModelsStopsOnEndlessCursors(t *testing.T) {+ withSSRFBypass(t)+ ctx := t.Context()+ requests := 0+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ requests+++ _ = json.NewEncoder(w).Encode(map[string]interface{}{+ "data": []map[string]interface{}{{"id": fmt.Sprintf("claude-%d", requests)}},+ "has_more": true,+ "last_id": fmt.Sprintf("claude-%d", requests),+ })+ }))+ defer srv.Close()++ apiKey := "test-key"+ if _, err := newAnthropicForTest(srv.URL).ListModels(ctx, &APIConfig{ApiKey: &apiKey}); err == nil {+ t.Fatal("expected an error when pagination never ends")+ }+ if requests != anthropicModelsMaxPages {+ t.Errorf("requests=%d, want %d", requests, anthropicModelsMaxPages)+ }+}+func TestAnthropicListModelsRejectsProviderError(t *testing.T) {withSSRFBypass(t)ctx := t.Context()