From bf90c8948afd80f63a17154ccaf7517c8d64b778 Mon Sep 17 00:00:00 2001 From: Panda Dev <56657208+pandadev66@users.noreply.github.com> Date: Wed, 13 May 2026 10:39:14 +0200 Subject: [PATCH] Go: implement ListModels in ZhipuAI driver (#14886) ### What problem does this PR solve? Fixes #14884 The ZhipuAI Go driver in `internal/entity/models/zhipu-ai.go` had a stub `ListModels` method that always returned `"zhipu-ai, no such method"`. The DeepSeek, Gitee, NVIDIA, OpenAI, SiliconFlow, and OpenRouter drivers in the same package already implement `ListModels` against the OpenAI-compatible `/models` endpoint, and the model picker UI relies on it. This PR brings ZhipuAI in line with that pattern. ### Changes - `internal/entity/models/zhipu-ai.go`: implement `ZhipuAIModel.ListModels`. - Resolve region with default fallback. - GET `${BaseURL[region]}/${URLSuffix.Models}` (resolves to `https://open.bigmodel.cn/api/paas/v4/models` with the default region). - Send `Authorization: Bearer ` when an API key is configured. Omit the header when the key is empty, so an unauthenticated caller gets a clear `401` from upstream. - Surface non-200 responses with the upstream status line and body, matching the other Go drivers. - Parse the response via the package-level `DSModelList` / `DSModel` types already used by DeepSeek, Gitee, and SiliconFlow. - When the response includes `owned_by`, render the entry as `id@owned_by`, matching the convention of Gitee and SiliconFlow. - `conf/models/zhipu-ai.json`: add `"models": "models"` to `url_suffix`. ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- conf/models/zhipu-ai.json | 3 +- internal/entity/models/zhipu-ai.go | 58 +++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/conf/models/zhipu-ai.json b/conf/models/zhipu-ai.json index d1bbac649f..b10f18b5d4 100644 --- a/conf/models/zhipu-ai.json +++ b/conf/models/zhipu-ai.json @@ -9,7 +9,8 @@ "async_result": "async-result", "embedding": "embeddings", "rerank": "rerank", - "files": "files" + "files": "files", + "models": "models" }, "class": "glm", "models": [ diff --git a/internal/entity/models/zhipu-ai.go b/internal/entity/models/zhipu-ai.go index a381105534..742314e4c5 100644 --- a/internal/entity/models/zhipu-ai.go +++ b/internal/entity/models/zhipu-ai.go @@ -456,7 +456,63 @@ func (z *ZhipuAIModel) Embed(modelName *string, texts []string, apiConfig *APICo } func (z *ZhipuAIModel) ListModels(apiConfig *APIConfig) ([]string, error) { - return nil, fmt.Errorf("%s, no such method", z.Name()) + region := "default" + if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { + region = *apiConfig.Region + } + + baseURL := z.BaseURL["default"] + if region != "default" { + if regional, ok := z.BaseURL[region]; ok && regional != "" { + baseURL = regional + } + } + if baseURL == "" { + return nil, fmt.Errorf("zhipu-ai: no base URL configured for default region") + } + + url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), z.URLSuffix.Models) + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("Content-Type", "application/json") + if apiConfig != nil && apiConfig.ApiKey != nil && *apiConfig.ApiKey != "" { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey)) + } + + resp, err := z.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to send request: %w", err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response: %w", err) + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("ZhipuAI models API error: %s, body: %s", resp.Status, string(body)) + } + + var modelList DSModelList + if err = json.Unmarshal(body, &modelList); err != nil { + return nil, fmt.Errorf("failed to parse response: %w", err) + } + + models := make([]string, 0, len(modelList.Models)) + for _, model := range modelList.Models { + modelName := model.ID + if model.OwnedBy != "" { + modelName = model.ID + "@" + model.OwnedBy + } + models = append(models, modelName) + } + + return models, nil } func (z *ZhipuAIModel) Balance(apiConfig *APIConfig) (map[string]interface{}, error) {