diff --git a/internal/admin/handler.go b/internal/admin/handler.go index dcb703ad30..d073ca22ef 100644 --- a/internal/admin/handler.go +++ b/internal/admin/handler.go @@ -1163,7 +1163,7 @@ func (h *Handler) ListAllModels(c *gin.Context) { } // List models - models, err := h.service.ListAllModels(page, pageSize) + models, err := h.service.modelProviderService.ListAllModels(page, pageSize) if err != nil { common.RespondError(c, common.CodeServerError, err.Error()) return diff --git a/internal/admin/service.go b/internal/admin/service.go index 8c5c96103c..b9a77e891a 100644 --- a/internal/admin/service.go +++ b/internal/admin/service.go @@ -1575,22 +1575,6 @@ func (s *Service) addTenantForAdmin(userID, nickname string) error { return dao.DB.Create(userTenant).Error } -// ListAllModels list all models -func (s *Service) ListAllModels(pageIndex, pageSize int) ([]map[string]interface{}, error) { - models, err := dao.GetModelProviderManager().ListAllModels() - if err != nil { - return nil, err - } - if err := common.ValidatePagination(&pageIndex, &pageSize, nil); err != nil { - return nil, err - } - start := (pageIndex - 1) * pageSize - if start >= len(models) { - return []map[string]interface{}{}, nil - } - return models[start:min(start+pageSize, len(models))], nil -} - func (s *Service) GetModelByModelName(modelName string) (*modelModule.Model, error) { return dao.GetModelProviderManager().GetModelByNameOrAlias(modelName), nil } diff --git a/internal/cli/common_command.go b/internal/cli/common_command.go index 79c1598f0a..a76ab6f7cb 100644 --- a/internal/cli/common_command.go +++ b/internal/cli/common_command.go @@ -22,9 +22,11 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" "os" "ragflow/internal/cli/utils" "ragflow/internal/common" + "strconv" "strings" "time" @@ -1507,80 +1509,80 @@ func (c *CLI) getDatasetIDByName(datasetName string) (string, error) { } func (c *CLI) getAgentIDByName(agentName string) (string, error) { - response, err := c.APIListAgentsCommand(1, nil) - if err != nil { - return "", err - } - commonResponse, ok := response.(*CommonResponse) - if !ok { - return "", fmt.Errorf("invalid response") - } - for _, agent := range commonResponse.Data { - if agent["name"] == agentName { - return agent["id"].(string), nil - } - } - return "", fmt.Errorf("agent %s not found", agentName) + return c.getResourceIDByName("agent", agentName, "canvas") } func (c *CLI) getSearchIDByName(searchName string) (string, error) { - response, err := c.APIListSearchesCommand(1, nil) - if err != nil { - return "", err - } - searchesResponse, ok := response.(*ListSearchesResponse) - if !ok { - return "", fmt.Errorf("invalid response") - } - searches := searchesResponse.Data["search_apps"].([]interface{}) - for _, search := range searches { - searchMap := search.(map[string]interface{}) - if searchMap["name"] == searchName { - return searchMap["id"].(string), nil - } - } - return "", fmt.Errorf("search %s not found", searchName) + return c.getResourceIDByName("search", searchName, "search_apps") } func (c *CLI) getChatIDByName(chatName string) (string, error) { - response, err := c.APIListChatsCommand(1, nil) - if err != nil { - return "", err - } - commonResponse, ok := response.(*CommonResponse) - if !ok { - return "", fmt.Errorf("invalid response") - } - for _, chat := range commonResponse.Data { - if chat["name"] == chatName { - return chat["id"].(string), nil - } - } - return "", fmt.Errorf("chat %s not found", chatName) + return c.getResourceIDByName("chat", chatName, "chats") } func (c *CLI) getMemoryIDByName(memoryName string) (string, error) { - response, err := c.APIListMemoriesCommand(1, nil) + return c.getResourceIDByName("memory", memoryName, "memory_list") +} + +func (c *CLI) getResourceIDByName(resource, name, listKey string) (string, error) { + httpClient, err := c.apiModeClient() if err != nil { return "", err } - listMemoriesResponse, ok := response.(*ListMemoriesResponse) - if !ok { - return "", fmt.Errorf("invalid response") + path, nameKey, totalKey := "/"+resource+"s", "name", "total" + if resource == "memory" { + path, totalKey = "/memories", "total_count" + } else if resource == "search" { + path = "/searches" } - memories, ok := listMemoriesResponse.Data["memory_list"].([]interface{}) - if !ok { - return "", fmt.Errorf("invalid memory_list in response") + if resource == "agent" { + nameKey = "title" } - for _, memory := range memories { - var memoryMap map[string]interface{} - memoryMap, ok = memory.(map[string]interface{}) - if !ok { - continue + query := url.Values{"page_size": {strconv.Itoa(common.MaxPageSize)}} + var remaining int64 + for page := common.DefaultPage; ; page++ { + query.Set("page", strconv.Itoa(page)) + resp, err := httpClient.Request(1, http.MethodGet, path+"?"+query.Encode(), httpClient.AuthKind(), nil, nil) + if err != nil { + return "", err } - if memoryMap["name"] == memoryName { - return memoryMap["id"].(string), nil + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to list %s: HTTP %d", path[1:], resp.StatusCode) + } + var result struct { + Code int `json:"code"` + Message string `json:"message"` + Data map[string]json.RawMessage `json:"data"` + } + if err := json.Unmarshal(resp.Body, &result); err != nil { + return "", fmt.Errorf("invalid response: %w", err) + } + if result.Code != 0 { + return "", fmt.Errorf("%s", result.Message) + } + if page == common.DefaultPage { + if err := json.Unmarshal(result.Data[totalKey], &remaining); err != nil || remaining < 0 { + return "", fmt.Errorf("invalid %s in response", totalKey) + } + } + var items []map[string]interface{} + if err := json.Unmarshal(result.Data[listKey], &items); err != nil { + return "", fmt.Errorf("invalid %s in response: %w", listKey, err) + } + for _, item := range items { + if item[nameKey] != name || (resource == "agent" && item["type"] == "compilation_template_group") { + continue + } + id, ok := item["id"].(string) + if !ok || id == "" { + return "", fmt.Errorf("invalid %s ID in response", resource) + } + return id, nil + } + remaining -= int64(len(items)) + if remaining <= 0 || len(items) < common.MaxPageSize { + break } } - return "", fmt.Errorf("memory %s not found", memoryName) + return "", fmt.Errorf("%s %s not found", resource, name) } diff --git a/internal/service/model_service.go b/internal/service/model_service.go index 2be8b56038..21b3ce91eb 100644 --- a/internal/service/model_service.go +++ b/internal/service/model_service.go @@ -3117,6 +3117,8 @@ func (m *ModelProviderService) ListAllModels(pageIndex, pageSize int) ([]map[str if start >= len(models) { return []map[string]interface{}{}, nil } + // Models are loaded from a file and kept in memory, so slice-based pagination is appropriate. + // If models are stored in a database, apply pagination in the query instead. return models[start:min(start+pageSize, len(models))], nil }