2026-04-02 20:20:35 +08:00
//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package models
import (
"bytes"
2026-06-02 03:27:26 -04:00
"context"
2026-05-25 16:50:06 -10:00
"encoding/base64"
2026-04-02 20:20:35 +08:00
"encoding/json"
"fmt"
"io"
2026-05-21 17:53:18 -10:00
"mime/multipart"
2026-04-02 20:20:35 +08:00
"net/http"
2026-05-21 17:53:18 -10:00
"os"
"path/filepath"
2026-05-06 10:41:58 +08:00
"ragflow/internal/common"
2026-04-03 18:11:23 +08:00
"strings"
2026-04-02 20:20:35 +08:00
)
2026-04-03 18:11:23 +08:00
// ZhipuAIModel implements ModelDriver for Zhipu AI
2026-04-02 20:20:35 +08:00
type ZhipuAIModel struct {
2026-06-03 16:33:58 +08:00
baseModel BaseModel
2026-04-02 20:20:35 +08:00
}
// NewZhipuAIModel creates a new Zhipu AI model instance
2026-04-20 15:31:12 +08:00
func NewZhipuAIModel ( baseURL map [ string ] string , urlSuffix URLSuffix ) * ZhipuAIModel {
2026-04-02 20:20:35 +08:00
return & ZhipuAIModel {
2026-06-03 16:33:58 +08:00
baseModel : BaseModel {
2026-06-11 05:20:12 -06:00
BaseURL : baseURL ,
URLSuffix : urlSuffix ,
httpClient : NewDriverHTTPClient ( ) ,
2026-04-03 18:11:23 +08:00
} ,
2026-04-02 20:20:35 +08:00
}
}
2026-04-29 17:05:08 +08:00
func ( z * ZhipuAIModel ) NewInstance ( baseURL map [ string ] string ) ModelDriver {
2026-06-04 17:50:22 +08:00
return NewZhipuAIModel ( baseURL , z . baseModel . URLSuffix )
2026-04-29 17:05:08 +08:00
}
2026-04-21 21:31:50 +08:00
func ( z * ZhipuAIModel ) Name ( ) string {
return "zhipu"
}
2026-04-30 15:25:01 +08:00
// ChatWithMessages sends multiple messages with roles and returns response
2026-04-30 19:33:57 +08:00
func ( z * ZhipuAIModel ) ChatWithMessages ( modelName string , messages [ ] Message , apiConfig * APIConfig , chatModelConfig * ChatConfig ) ( * ChatResponse , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return nil , err
2026-04-30 15:25:01 +08:00
}
if len ( messages ) == 0 {
return nil , fmt . Errorf ( "messages is empty" )
2026-04-02 20:20:35 +08:00
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , err
2026-04-21 16:52:32 +08:00
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , z . baseModel . URLSuffix . Chat )
2026-04-02 20:20:35 +08:00
2026-04-30 15:25:01 +08:00
// Convert messages to the format expected by API
apiMessages := make ( [ ] map [ string ] interface { } , len ( messages ) )
for i , msg := range messages {
apiMessages [ i ] = map [ string ] interface { } {
"role" : msg . Role ,
"content" : msg . Content ,
}
}
2026-04-02 20:20:35 +08:00
// Build request body
reqBody := map [ string ] interface { } {
2026-04-30 15:25:01 +08:00
"model" : modelName ,
"messages" : apiMessages ,
2026-04-02 20:20:35 +08:00
"stream" : false ,
"temperature" : 1 ,
}
2026-04-30 15:25:01 +08:00
if chatModelConfig != nil {
if chatModelConfig . Stream != nil {
reqBody [ "stream" ] = * chatModelConfig . Stream
}
2026-04-20 15:31:12 +08:00
2026-04-30 15:25:01 +08:00
if chatModelConfig . MaxTokens != nil {
reqBody [ "max_tokens" ] = * chatModelConfig . MaxTokens
}
2026-04-20 15:31:12 +08:00
2026-04-30 15:25:01 +08:00
if chatModelConfig . Temperature != nil {
reqBody [ "temperature" ] = * chatModelConfig . Temperature
}
2026-04-21 16:52:32 +08:00
2026-04-30 15:25:01 +08:00
if chatModelConfig . TopP != nil {
reqBody [ "top_p" ] = * chatModelConfig . TopP
}
2026-04-21 16:52:32 +08:00
2026-04-30 15:25:01 +08:00
if chatModelConfig . Stop != nil {
reqBody [ "stop" ] = * chatModelConfig . Stop
}
2026-04-21 16:52:32 +08:00
2026-04-30 15:25:01 +08:00
if chatModelConfig . Thinking != nil {
if * chatModelConfig . Thinking {
reqBody [ "thinking" ] = map [ string ] interface { } {
"type" : "enabled" ,
}
} else {
reqBody [ "thinking" ] = map [ string ] interface { } {
"type" : "disabled" ,
}
2026-04-21 16:52:32 +08:00
}
2026-04-02 20:20:35 +08:00
}
}
jsonData , err := json . Marshal ( reqBody )
if err != nil {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "failed to marshal request: %w" , err )
2026-04-02 20:20:35 +08:00
}
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , nonStreamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , "POST" , url , bytes . NewBuffer ( jsonData ) )
2026-04-02 20:20:35 +08:00
if err != nil {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "failed to create request: %w" , err )
2026-04-02 20:20:35 +08:00
}
req . Header . Set ( "Content-Type" , "application/json" )
2026-04-21 16:52:32 +08:00
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-04-02 20:20:35 +08:00
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-04-02 20:20:35 +08:00
if err != nil {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "failed to send request: %w" , err )
2026-04-02 20:20:35 +08:00
}
defer resp . Body . Close ( )
body , err := io . ReadAll ( resp . Body )
if err != nil {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "failed to read response: %w" , err )
2026-04-02 20:20:35 +08:00
}
if resp . StatusCode != http . StatusOK {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "API request failed with status %d: %s" , resp . StatusCode , string ( body ) )
2026-04-02 20:20:35 +08:00
}
// Parse response
var result map [ string ] interface { }
2026-05-12 17:17:44 +08:00
if err = json . Unmarshal ( body , & result ) ; err != nil {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "failed to parse response: %w" , err )
2026-04-02 20:20:35 +08:00
}
choices , ok := result [ "choices" ] . ( [ ] interface { } )
if ! ok || len ( choices ) == 0 {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "no choices in response" )
2026-04-02 20:20:35 +08:00
}
firstChoice , ok := choices [ 0 ] . ( map [ string ] interface { } )
if ! ok {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "invalid choice format" )
2026-04-02 20:20:35 +08:00
}
messageMap , ok := firstChoice [ "message" ] . ( map [ string ] interface { } )
if ! ok {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "invalid message format" )
2026-04-02 20:20:35 +08:00
}
content , ok := messageMap [ "content" ] . ( string )
if ! ok {
2026-04-21 16:52:32 +08:00
return nil , fmt . Errorf ( "invalid content format" )
2026-04-02 20:20:35 +08:00
}
2026-04-21 16:52:32 +08:00
var reasonContent string
2026-04-30 15:25:01 +08:00
if chatModelConfig != nil && chatModelConfig . Thinking != nil && * chatModelConfig . Thinking {
2026-04-21 16:52:32 +08:00
reasonContent , ok = messageMap [ "reasoning_content" ] . ( string )
if ! ok {
return nil , fmt . Errorf ( "invalid content format" )
}
// if first char of reasonContent is \n remove the '\n'
if reasonContent != "" && reasonContent [ 0 ] == '\n' {
reasonContent = reasonContent [ 1 : ]
}
}
chatResponse := & ChatResponse {
Answer : & content ,
ReasonContent : & reasonContent ,
}
return chatResponse , nil
2026-04-02 20:20:35 +08:00
}
2026-04-30 19:33:57 +08:00
// ChatStreamlyWithSender sends messages and streams response via sender function (best performance, no channel)
func ( z * ZhipuAIModel ) ChatStreamlyWithSender ( modelName string , messages [ ] Message , apiConfig * APIConfig , chatModelConfig * ChatConfig , sender func ( * string , * string ) error ) error {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return err
}
2026-04-30 19:33:57 +08:00
if len ( messages ) == 0 {
return fmt . Errorf ( "messages is empty" )
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return err
2026-04-03 18:11:23 +08:00
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , z . baseModel . URLSuffix . Chat )
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
// Convert messages to API format
apiMessages := make ( [ ] map [ string ] interface { } , len ( messages ) )
for i , msg := range messages {
apiMessages [ i ] = map [ string ] interface { } {
"role" : msg . Role ,
"content" : msg . Content ,
}
}
2026-04-03 18:11:23 +08:00
// Build request body with streaming enabled
reqBody := map [ string ] interface { } {
2026-04-30 19:33:57 +08:00
"model" : modelName ,
"messages" : apiMessages ,
"stream" : true ,
2026-04-03 18:11:23 +08:00
"temperature" : 1 ,
}
2026-04-30 19:33:57 +08:00
if chatModelConfig != nil {
if chatModelConfig . Stream != nil {
reqBody [ "stream" ] = * chatModelConfig . Stream
}
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
if chatModelConfig . MaxTokens != nil {
reqBody [ "max_tokens" ] = * chatModelConfig . MaxTokens
}
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
if chatModelConfig . Temperature != nil {
reqBody [ "temperature" ] = * chatModelConfig . Temperature
}
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
if chatModelConfig . DoSample != nil {
reqBody [ "do_sample" ] = * chatModelConfig . DoSample
}
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
if chatModelConfig . TopP != nil {
reqBody [ "top_p" ] = * chatModelConfig . TopP
}
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
if chatModelConfig . Stop != nil {
reqBody [ "stop" ] = * chatModelConfig . Stop
}
2026-04-03 18:11:23 +08:00
2026-04-30 19:33:57 +08:00
if chatModelConfig . Thinking != nil {
if * chatModelConfig . Thinking {
reqBody [ "thinking" ] = map [ string ] interface { } {
"type" : "enabled" ,
}
} else {
reqBody [ "thinking" ] = map [ string ] interface { } {
"type" : "disabled" ,
}
2026-04-03 18:11:23 +08:00
}
}
}
jsonData , err := json . Marshal ( reqBody )
if err != nil {
return fmt . Errorf ( "failed to marshal request: %w" , err )
}
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , streamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , "POST" , url , bytes . NewBuffer ( jsonData ) )
2026-04-03 18:11:23 +08:00
if err != nil {
return fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
2026-04-21 16:52:32 +08:00
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-04-03 18:11:23 +08:00
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-04-03 18:11:23 +08:00
if err != nil {
return fmt . Errorf ( "failed to send request: %w" , err )
}
defer resp . Body . Close ( )
if resp . StatusCode != http . StatusOK {
body , _ := io . ReadAll ( resp . Body )
return fmt . Errorf ( "API request failed with status %d: %s" , resp . StatusCode , string ( body ) )
}
// SSE parsing: read line by line
2026-06-11 05:20:12 -06:00
if _ , err := ParseSSEStream [ map [ string ] interface { } ] ( resp . Body , func ( event map [ string ] interface { } ) error {
common . Info ( fmt . Sprintf ( "%v" , event ) )
2026-04-03 18:11:23 +08:00
choices , ok := event [ "choices" ] . ( [ ] interface { } )
if ! ok || len ( choices ) == 0 {
2026-06-11 05:20:12 -06:00
return nil
2026-04-03 18:11:23 +08:00
}
firstChoice , ok := choices [ 0 ] . ( map [ string ] interface { } )
if ! ok {
2026-06-11 05:20:12 -06:00
return nil
2026-04-03 18:11:23 +08:00
}
delta , ok := firstChoice [ "delta" ] . ( map [ string ] interface { } )
if ! ok {
2026-06-11 05:20:12 -06:00
return nil
2026-04-03 18:11:23 +08:00
}
2026-04-27 20:35:47 +08:00
reasoningContent , ok := delta [ "reasoning_content" ] . ( string )
if ok && reasoningContent != "" {
if err := sender ( nil , & reasoningContent ) ; err != nil {
2026-04-03 18:11:23 +08:00
return err
}
}
2026-04-27 20:35:47 +08:00
content , ok := delta [ "content" ] . ( string )
if ok && content != "" {
if err := sender ( & content , nil ) ; err != nil {
2026-04-03 18:11:23 +08:00
return err
}
}
2026-06-11 05:20:12 -06:00
return nil
} ) ; err != nil {
return fmt . Errorf ( "failed to scan response body: %w" , err )
2026-04-03 18:11:23 +08:00
}
// Send [DONE] marker for OpenAI compatibility
endOfStream := "[DONE]"
2026-04-21 21:31:50 +08:00
if err = sender ( & endOfStream , nil ) ; err != nil {
2026-04-03 18:11:23 +08:00
return err
}
2026-06-11 05:20:12 -06:00
return nil
2026-04-03 18:11:23 +08:00
}
2026-05-11 14:45:30 +08:00
type zhipuEmbeddingResponse struct {
Data [ ] zhipuEmbeddingData ` json:"data" `
Model string ` json:"model" `
Object string ` json:"object" `
Usage zhipuUsage ` json:"usage" `
}
type zhipuEmbeddingData struct {
Embedding [ ] float64 ` json:"embedding" `
Index int ` json:"index" `
Object string ` json:"object" `
}
type zhipuUsage struct {
CompletionTokens int ` json:"completion_tokens" `
PromptTokens int ` json:"prompt_tokens" `
TotalTokens int ` json:"total_tokens" `
}
2026-04-28 18:07:42 +08:00
// Encode encodes a list of texts into embeddings
2026-05-11 14:45:30 +08:00
func ( z * ZhipuAIModel ) Embed ( modelName * string , texts [ ] string , apiConfig * APIConfig , embeddingConfig * EmbeddingConfig ) ( [ ] EmbeddingData , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
return nil , err
2026-05-11 14:45:30 +08:00
}
2026-06-04 17:50:22 +08:00
if len ( texts ) == 0 {
return [ ] EmbeddingData { } , nil
2026-05-11 14:45:30 +08:00
}
if modelName == nil || * modelName == "" {
return nil , fmt . Errorf ( "model name is required" )
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , err
2026-04-20 15:31:12 +08:00
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , z . baseModel . URLSuffix . Embedding )
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
reqBody := map [ string ] interface { } { }
reqBody [ "model" ] = modelName
reqBody [ "input" ] = texts
if embeddingConfig . Dimension > 0 {
reqBody [ "dimensions" ] = embeddingConfig . Dimension
}
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
jsonData , err := json . Marshal ( reqBody )
if err != nil {
return nil , fmt . Errorf ( "failed to marshal request: %w" , err )
}
2026-04-02 20:20:35 +08:00
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , nonStreamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , "POST" , url , bytes . NewBuffer ( jsonData ) )
2026-05-11 14:45:30 +08:00
if err != nil {
return nil , fmt . Errorf ( "failed to create request: %w" , err )
}
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
req . Header . Set ( "Content-Type" , "application/json" )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-04-02 20:20:35 +08:00
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-05-11 14:45:30 +08:00
if err != nil {
return nil , fmt . Errorf ( "failed to send request: %w" , err )
}
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
body , err := io . ReadAll ( resp . Body )
resp . Body . Close ( )
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
if err != nil {
return nil , fmt . Errorf ( "failed to read response: %w" , err )
}
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
if resp . StatusCode != http . StatusOK {
return nil , fmt . Errorf ( "API request failed with status %d: %s" , resp . StatusCode , string ( body ) )
}
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
// Parse response
var zhipuResp zhipuEmbeddingResponse
if err = json . Unmarshal ( body , & zhipuResp ) ; err != nil {
return nil , fmt . Errorf ( "failed to parse response: %w" , err )
}
2026-04-02 20:20:35 +08:00
2026-05-11 14:45:30 +08:00
var embeddings [ ] EmbeddingData
for _ , dataElem := range zhipuResp . Data {
var embeddingData EmbeddingData
embeddingData . Embedding = dataElem . Embedding
embeddingData . Index = dataElem . Index
embeddings = append ( embeddings , embeddingData )
2026-04-02 20:20:35 +08:00
}
return embeddings , nil
}
2026-04-21 16:52:32 +08:00
2026-06-09 19:01:00 +08:00
func ( z * ZhipuAIModel ) ListModels ( apiConfig * APIConfig ) ( [ ] ListModelResponse , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return nil , err
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , err
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , z . baseModel . URLSuffix . Models )
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , nonStreamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , "GET" , url , nil )
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
if err != nil {
return nil , fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
2026-06-04 17:50:22 +08:00
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
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 ) )
}
2026-06-11 13:32:50 +08:00
// Parse response
var modelList ModelList
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 <api_key>` 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)
2026-05-13 10:39:14 +02:00
if err = json . Unmarshal ( body , & modelList ) ; err != nil {
return nil , fmt . Errorf ( "failed to parse response: %w" , err )
}
2026-06-11 13:32:50 +08:00
return ParseListModel ( modelList ) , nil
2026-04-21 21:31:50 +08:00
}
func ( z * ZhipuAIModel ) Balance ( apiConfig * APIConfig ) ( map [ string ] interface { } , error ) {
return nil , fmt . Errorf ( "%s, no such method" , z . Name ( ) )
2026-04-21 16:52:32 +08:00
}
2026-04-23 10:16:20 +08:00
func ( z * ZhipuAIModel ) CheckConnection ( apiConfig * APIConfig ) error {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return err
2026-04-23 10:16:20 +08:00
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return err
}
url := fmt . Sprintf ( "%s/%s" , baseURL , z . baseModel . URLSuffix . Files )
2026-04-23 10:16:20 +08:00
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , nonStreamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , "GET" , url , nil )
2026-04-23 10:16:20 +08:00
if err != nil {
return fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-04-23 10:16:20 +08:00
if err != nil {
return fmt . Errorf ( "failed to send request: %w" , err )
}
defer resp . Body . Close ( )
body , err := io . ReadAll ( resp . Body )
if err != nil {
return fmt . Errorf ( "failed to read response: %w" , err )
}
if resp . StatusCode != http . StatusOK {
return fmt . Errorf ( "API request failed with status %d: %s" , resp . StatusCode , string ( body ) )
}
return nil
}
2026-04-28 12:59:01 +08:00
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
// zhipuRerankRequest is the request body for the ZhipuAI rerank
// endpoint. The shape matches the standard OpenAI-compatible rerank
// API also used by SiliconFlow.
type zhipuRerankRequest struct {
Model string ` json:"model" `
Query string ` json:"query" `
Documents [ ] string ` json:"documents" `
TopN int ` json:"top_n" `
ReturnDocuments bool ` json:"return_documents" `
}
// zhipuRerankResponse is the response shape for the ZhipuAI rerank
// endpoint.
type zhipuRerankResponse struct {
2026-05-09 17:41:54 +08:00
Created int64 ` json:"created" `
ID string ` json:"id" `
RequestID string ` json:"request_id" `
Usage struct {
CompletionTokens int ` json:"completion_tokens" `
PromptTokens int ` json:"prompt_tokens" `
TotalTokens int ` json:"total_tokens" `
} ` json:"usage" `
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
Results [ ] struct {
Index int ` json:"index" `
RelevanceScore float64 ` json:"relevance_score" `
} ` json:"results" `
}
2026-05-25 16:50:06 -10:00
type zhipuOCRResponse struct {
MarkdownResults * string ` json:"md_results" `
}
2026-05-09 17:41:54 +08:00
// Rerank calculates similarity scores between query and documents using
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
// the ZhipuAI /rerank endpoint (e.g. glm-rerank). The result is one
2026-05-09 17:41:54 +08:00
// score per input text, in the same order the documents were given.
func ( z * ZhipuAIModel ) Rerank ( modelName * string , query string , documents [ ] string , apiConfig * APIConfig , rerankConfig * RerankConfig ) ( * RerankResponse , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
return nil , err
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
}
2026-06-04 17:50:22 +08:00
if len ( documents ) == 0 {
return & RerankResponse { } , nil
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
}
if modelName == nil || * modelName == "" {
return nil , fmt . Errorf ( "model name is required" )
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , err
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , z . baseModel . URLSuffix . Rerank )
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
2026-05-09 17:41:54 +08:00
var topN = rerankConfig . TopN
if rerankConfig . TopN == 0 {
topN = len ( documents )
}
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
reqBody := zhipuRerankRequest {
Model : * modelName ,
Query : query ,
2026-05-09 17:41:54 +08:00
Documents : documents ,
TopN : topN ,
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
ReturnDocuments : false ,
}
jsonData , err := json . Marshal ( reqBody )
if err != nil {
return nil , fmt . Errorf ( "failed to marshal request: %w" , err )
}
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , nonStreamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , "POST" , url , bytes . NewBuffer ( jsonData ) )
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
if err != nil {
return nil , fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
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 rerank API error: %s, body: %s" , resp . Status , string ( body ) )
}
2026-05-09 17:41:54 +08:00
var zhipuRerankResp zhipuRerankResponse
if err = json . Unmarshal ( body , & zhipuRerankResp ) ; err != nil {
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
return nil , fmt . Errorf ( "failed to parse response: %w" , err )
}
2026-05-09 17:41:54 +08:00
var rerankResponse RerankResponse
for _ , result := range zhipuRerankResp . Results {
rerankResult := RerankResult {
Index : result . Index ,
RelevanceScore : result . RelevanceScore ,
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
}
2026-05-09 17:41:54 +08:00
rerankResponse . Data = append ( rerankResponse . Data , rerankResult )
Go: implement Rerank in ZhipuAI driver (#14608)
### What problem does this PR solve?
The ZhipuAI Go driver had a stub Rerank method that returned "not
implemented", even though conf/models/zhipu-ai.json already ships
glm-rerank as a rerank model and the rerank URL suffix is already wired
in url_suffix:
```json
"url_suffix": {
...
"rerank": "rerank"
},
"models": [
{"name": "glm-rerank", "model_types": ["rerank"]},
...
]
```
So the config was ready but the driver was not. A tenant who picked
glm-rerank in the Go layer could not actually run a rerank call. This PR
fills the gap so the listed model works end to end.
### What this PR includes
- `internal/entity/models/zhipu-ai.go`: real implementation of
`ZhipuAIModel.Rerank`, plus two small local types (`zhipuRerankRequest`,
`zhipuRerankResponse`) that mirror the standard OpenAI-compatible rerank
shape used by SiliconFlow.
No factory change. No JSON change. No interface change.
### How the driver works
- POST to `${BaseURL}/${URLSuffix.Rerank}` (resolves to
`https://open.bigmodel.cn/api/paas/v4/rerank` with the default config),
reusing the existing httpClient on the driver.
- Validate apiConfig and the API key, validate the model name, and
resolve the region. Return a clear local error before any HTTP call when
something is missing.
- Send `{model, query, documents, top_n, return_documents: false}` in
the body, the same shape the SiliconFlow driver already uses.
- Walk `results[*].relevance_score` and copy each score into the output
slice indexed by `results[*].index`, so the output order matches the
input order even if the API returns results in a different order.
- Empty `texts` input returns an empty `[]float64` with no HTTP call.
- Non-200 responses propagate the upstream status line and body.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
### How was this tested?
- `go build ./internal/entity/models/...` in a clean go 1.25 image (the
go.mod minimum) returns exit 0.
- The full method set on `ZhipuAIModel` still matches the `ModelDriver`
interface (NewInstance, Name, ChatWithMessages, ChatStreamlyWithSender,
Encode, ListModels, Balance, CheckConnection, Rerank).
- Pattern parity with the existing SiliconFlow Rerank implementation
(`internal/entity/models/siliconflow.go`).
Closes #14607
2026-05-07 11:56:30 +02:00
}
2026-05-09 17:41:54 +08:00
return & rerankResponse , nil
2026-04-28 12:59:01 +08:00
}
2026-05-12 17:17:44 +08:00
// TranscribeAudio transcribe audio
2026-05-21 17:53:18 -10:00
func ( z * ZhipuAIModel ) TranscribeAudio ( modelName * string , file * string , apiConfig * APIConfig , asrConfig * ASRConfig ) ( * ASRResponse , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return nil , err
2026-05-21 17:53:18 -10:00
}
2026-06-03 16:33:58 +08:00
2026-05-21 17:53:18 -10:00
if modelName == nil || * modelName == "" {
return nil , fmt . Errorf ( "model name is required" )
}
if file == nil || * file == "" {
return nil , fmt . Errorf ( "file is required" )
}
2026-06-03 16:33:58 +08:00
if z . baseModel . URLSuffix . ASR == "" {
2026-05-21 17:53:18 -10:00
return nil , fmt . Errorf ( "zhipu-ai: ASR URL suffix is not configured" )
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , err
2026-05-21 17:53:18 -10:00
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , strings . TrimLeft ( z . baseModel . URLSuffix . ASR , "/" ) )
2026-05-21 17:53:18 -10:00
var body bytes . Buffer
writer := multipart . NewWriter ( & body )
if err := writer . WriteField ( "model" , * modelName ) ; err != nil {
return nil , fmt . Errorf ( "failed to write model field: %w" , err )
}
if err := writer . WriteField ( "stream" , "false" ) ; err != nil {
return nil , fmt . Errorf ( "failed to write stream field: %w" , err )
}
if err := writeZhipuASRParams ( writer , asrConfig ) ; err != nil {
return nil , err
}
fix(codeql): close remaining 44 CodeQL alerts post-merge (#16408)
## Summary
After #16407 merged, 44 of the original 93 CodeQL alerts were still open
on the default branch. This PR closes the remaining ones by:
1. **Moving 32 existing `// codeql[...]` directives** so they sit on the
line **immediately before** the suppressed statement. The original
multi-line suppression blocks had the directive as the first line, with
the rationale on subsequent lines. After line shifts (refactors, linter
reformat), the directive ended up several lines above the alert location
— CodeQL only recognizes the suppression when it appears on the line
directly above. (32 alerts across 27 files.)
2. **Adding 9 new `// codeql[...]` suppressions** for alerts that had no
suppression in the preceding lines at all — mostly real-fixes that
CodeQL conservatively still flags (filepath.Base, bounded slice sizes,
model-identifier strings, the MD5-legacy-migration lookup in
`conversation_service.py`).
## Files changed
- `api/db/services/conversation_service.py` — add
`py/weak-sensitive-data-hashing` suppression (MD5 for backward-compat
legacy row lookup; not used for auth)
- `api/db/services/llm_service.py` — 3×
`py/clear-text-logging-sensitive-data` suppressions on the lines that
log `llm_name` in warnings/info
- `common/misc_utils.py` — 2× `py/clear-text-logging-sensitive-data`
suppressions on the redacted `current_url` log sites
- `internal/agent/component/invoke.go` — moved existing
`go/request-forgery` directive
- `internal/agent/sandbox/ssh.go` — moved existing
`go/command-injection` directive
- `internal/agent/tool/retrieval_service.go` — added
`go/uncontrolled-allocation-size` suppression (`topN` is bounded to 1024
above)
- `internal/cli/common_command.go` — moved 2×
`go/disabled-certificate-check` directives
- `internal/cli/user_command.go` — added `go/clear-text-logging`
suppression (filepath.Base already strips user-identifying path)
- `internal/dao/pipeline_operation_log.go` — moved 2× `go/sql-injection`
directives
- `internal/dao/user_canvas.go` — added `go/sql-injection` suppression
in `GetList` (the new `userCanvasOrderClause` call path)
- `internal/engine/infinity/chunk.go` — moved existing
`go/unsafe-quoting` directive
- `internal/entity/models/*` — moved `go/path-injection` directives (15
files)
- `internal/handler/oauth_login.go` — moved existing
`go/cookie-httponly-not-set` directive
- `internal/handler/tenant.go` — moved existing `go/path-injection`
directive
- `internal/service/deep_researcher.go` — moved existing
`go/unsafe-quoting` directive
- `internal/service/dataset.go` — added
`go/uncontrolled-allocation-size` suppression (`n` bounded to 1024
above)
- `internal/service/file.go` — moved existing `go/request-forgery`
directive
- `internal/service/langfuse.go` — moved 2× `go/request-forgery`
directives
- `internal/utility/mcp_client.go` — moved 3× `go/request-forgery`
directives
- `internal/utility/smtp.go` — moved existing `go/email-injection`
directive
- `rag/prompts/generator.py` — added
`py/clear-text-logging-sensitive-data` suppression
- `web/.../use-provider-fields.tsx` — added
`js/prototype-pollution-utility` suppression (FORBIDDEN_KEYS guard is on
the line above)
## Why the previous PR left alerts open
`// codeql[query-id] explanation` must be on the line **immediately
before** the suppressed statement per the [GitHub CodeQL suppression
spec](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning-with-codeql/suppressing-code-scanning-alerts).
The original suppression blocks were 4-5 lines, with the directive as
the **first** line. After linter reformat / line shifts, the directive
ended up too far above the actual alert line to be recognized. The fix
is to put the directive on the line directly above the suppressed
statement, with the rationale above it.
## Test plan
- All 9 modified Python files `ast.parse` clean
- All 4 modified Go files `gofmt` clean
- 36/44 expected alert suppressions in place
- 8 remaining CodeQL alerts are the originals (#3485851828, #3485851831,
#3485869759, #3485869766, #3485869768, #3485869771, #3485885962,
#3485895527) which were resolved by the corresponding commit comments;
these should close on the next scan when the suppression comments match
the alert lines.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
2026-06-27 20:49:06 +08:00
// codeql[go/path-injection] False positive: *file is the audio file path the caller passes in to upload. The user (or operator-supplied pipeline) explicitly chose this path, and the OS access check enforces permissions anyway.
2026-05-21 17:53:18 -10:00
audioFile , err := os . Open ( * file )
if err != nil {
return nil , fmt . Errorf ( "failed to open audio file: %w" , err )
}
defer audioFile . Close ( )
part , err := writer . CreateFormFile ( "file" , filepath . Base ( * file ) )
if err != nil {
return nil , fmt . Errorf ( "failed to create multipart file: %w" , err )
}
if _ , err = io . Copy ( part , audioFile ) ; err != nil {
return nil , fmt . Errorf ( "failed to copy audio data: %w" , err )
}
if err = writer . Close ( ) ; err != nil {
return nil , fmt . Errorf ( "failed to close multipart writer: %w" , err )
}
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , longOpCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , http . MethodPost , url , & body )
2026-05-21 17:53:18 -10:00
if err != nil {
return nil , fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
req . Header . Set ( "Content-Type" , writer . FormDataContentType ( ) )
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-05-21 17:53:18 -10:00
if err != nil {
return nil , fmt . Errorf ( "failed to send request: %w" , err )
}
defer resp . Body . Close ( )
respBody , err := io . ReadAll ( resp . Body )
if err != nil {
return nil , fmt . Errorf ( "failed to read response body: %w" , err )
}
if resp . StatusCode != http . StatusOK {
return nil , fmt . Errorf ( "ZhipuAI ASR API error: %s, body: %s" , resp . Status , string ( respBody ) )
}
var result struct {
Text string ` json:"text" `
}
if err = json . Unmarshal ( respBody , & result ) ; err != nil {
return nil , fmt . Errorf ( "failed to parse response: %w" , err )
}
return & ASRResponse { Text : result . Text } , nil
}
func writeZhipuASRParams ( writer * multipart . Writer , asrConfig * ASRConfig ) error {
if asrConfig == nil || asrConfig . Params == nil {
return nil
}
for key , value := range asrConfig . Params {
switch key {
case "model" , "stream" , "file" , "file_base64" :
continue
}
if err := writeZhipuASRField ( writer , key , value ) ; err != nil {
return err
}
}
return nil
}
func writeZhipuASRField ( writer * multipart . Writer , key string , value interface { } ) error {
switch v := value . ( type ) {
case nil :
return nil
case [ ] string :
for _ , item := range v {
if err := writer . WriteField ( key , item ) ; err != nil {
return fmt . Errorf ( "failed to write field %s: %w" , key , err )
}
}
return nil
case [ ] interface { } :
for _ , item := range v {
if err := writer . WriteField ( key , fmt . Sprint ( item ) ) ; err != nil {
return fmt . Errorf ( "failed to write field %s: %w" , key , err )
}
}
return nil
default :
if err := writer . WriteField ( key , fmt . Sprint ( v ) ) ; err != nil {
return fmt . Errorf ( "failed to write field %s: %w" , key , err )
}
return nil
}
2026-05-12 17:17:44 +08:00
}
func ( z * ZhipuAIModel ) TranscribeAudioWithSender ( modelName * string , file * string , apiConfig * APIConfig , asrConfig * ASRConfig , sender func ( * string , * string ) error ) error {
return fmt . Errorf ( "%s, no such method" , z . Name ( ) )
}
2026-05-15 18:41:43 +08:00
// AudioSpeech convert text to audio
2026-05-21 17:53:18 -10:00
func ( z * ZhipuAIModel ) AudioSpeech ( modelName * string , audioContent * string , apiConfig * APIConfig , ttsConfig * TTSConfig ) ( * TTSResponse , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return nil , err
}
2026-05-21 17:53:18 -10:00
reqBody , url , err := z . buildTTSRequest ( modelName , audioContent , apiConfig , ttsConfig , false )
if err != nil {
return nil , err
}
jsonData , err := json . Marshal ( reqBody )
if err != nil {
return nil , fmt . Errorf ( "failed to marshal request: %w" , err )
}
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , longOpCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , http . MethodPost , url , bytes . NewBuffer ( jsonData ) )
2026-05-21 17:53:18 -10:00
if err != nil {
return nil , fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-05-21 17:53:18 -10:00
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 body: %w" , err )
}
if resp . StatusCode != http . StatusOK {
return nil , fmt . Errorf ( "ZhipuAI TTS API error: %s, body: %s" , resp . Status , string ( body ) )
}
return & TTSResponse { Audio : body } , nil
2026-05-12 17:17:44 +08:00
}
func ( z * ZhipuAIModel ) AudioSpeechWithSender ( modelName * string , audioContent * string , apiConfig * APIConfig , ttsConfig * TTSConfig , sender func ( * string , * string ) error ) error {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return err
}
2026-05-21 17:53:18 -10:00
if sender == nil {
return fmt . Errorf ( "sender is required" )
}
reqBody , url , err := z . buildTTSRequest ( modelName , audioContent , apiConfig , ttsConfig , true )
if err != nil {
return err
}
jsonData , err := json . Marshal ( reqBody )
if err != nil {
return fmt . Errorf ( "failed to marshal request: %w" , err )
}
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , streamCallTimeout )
defer cancel ( )
req , err := http . NewRequestWithContext ( ctx , http . MethodPost , url , bytes . NewBuffer ( jsonData ) )
2026-05-21 17:53:18 -10:00
if err != nil {
return fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-05-21 17:53:18 -10:00
if err != nil {
return fmt . Errorf ( "failed to send request: %w" , err )
}
defer resp . Body . Close ( )
if resp . StatusCode != http . StatusOK {
body , _ := io . ReadAll ( resp . Body )
return fmt . Errorf ( "ZhipuAI stream TTS API error: %s, body: %s" , resp . Status , string ( body ) )
}
buf := make ( [ ] byte , 32 * 1024 )
for {
n , err := resp . Body . Read ( buf )
if n > 0 {
chunk := string ( buf [ : n ] )
if errSend := sender ( & chunk , nil ) ; errSend != nil {
return errSend
}
}
if err != nil {
if err == io . EOF {
break
}
return fmt . Errorf ( "error reading ZhipuAI binary audio stream: %w" , err )
}
}
return nil
}
func ( z * ZhipuAIModel ) buildTTSRequest ( modelName * string , audioContent * string , apiConfig * APIConfig , ttsConfig * TTSConfig , stream bool ) ( map [ string ] interface { } , string , error ) {
2026-06-03 16:33:58 +08:00
2026-05-21 17:53:18 -10:00
if modelName == nil || * modelName == "" {
return nil , "" , fmt . Errorf ( "model name is required" )
}
if audioContent == nil || * audioContent == "" {
return nil , "" , fmt . Errorf ( "audio content is empty" )
}
2026-06-03 16:33:58 +08:00
if z . baseModel . URLSuffix . TTS == "" {
2026-05-21 17:53:18 -10:00
return nil , "" , fmt . Errorf ( "zhipu-ai: TTS URL suffix is not configured" )
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , "" , err
2026-05-21 17:53:18 -10:00
}
reqBody := map [ string ] interface { } {
"model" : * modelName ,
"input" : * audioContent ,
"stream" : stream ,
}
if ttsConfig != nil {
for key , value := range ttsConfig . Params {
switch key {
case "model" , "input" , "stream" , "response_format" :
continue
}
reqBody [ key ] = value
}
if ttsConfig . Format != "" {
reqBody [ "response_format" ] = ttsConfig . Format
}
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , strings . TrimLeft ( z . baseModel . URLSuffix . TTS , "/" ) )
2026-05-21 17:53:18 -10:00
return reqBody , url , nil
2026-05-12 17:17:44 +08:00
}
// OCRFile OCR file
2026-05-25 16:50:06 -10:00
func ( z * ZhipuAIModel ) OCRFile ( modelName * string , content [ ] byte , fileURL * string , apiConfig * APIConfig , ocrConfig * OCRConfig ) ( * OCRFileResponse , error ) {
2026-06-04 17:50:22 +08:00
if err := z . baseModel . APIConfigCheck ( apiConfig ) ; err != nil {
2026-06-03 16:33:58 +08:00
return nil , err
2026-05-25 16:50:06 -10:00
}
if modelName == nil || * modelName == "" {
return nil , fmt . Errorf ( "model name is required" )
}
if ( fileURL == nil || * fileURL == "" ) && len ( content ) == 0 {
return nil , fmt . Errorf ( "file url or content is required" )
}
2026-06-03 16:33:58 +08:00
baseURL , err := z . baseModel . GetBaseURL ( apiConfig )
if err != nil {
return nil , err
2026-05-25 16:50:06 -10:00
}
2026-06-03 16:33:58 +08:00
if z . baseModel . URLSuffix . OCR == "" {
2026-05-25 16:50:06 -10:00
return nil , fmt . Errorf ( "zhipu-ai: no OCR URL suffix configured" )
}
file := ""
if fileURL != nil && * fileURL != "" {
file = * fileURL
} else {
mimeType := http . DetectContentType ( content )
if len ( content ) > 4 && string ( content [ : 4 ] ) == "%PDF" {
mimeType = "application/pdf"
}
file = fmt . Sprintf ( "data:%s;base64,%s" , mimeType , base64 . StdEncoding . EncodeToString ( content ) )
}
reqBody := map [ string ] interface { } {
"model" : * modelName ,
"file" : file ,
}
jsonData , err := json . Marshal ( reqBody )
if err != nil {
return nil , fmt . Errorf ( "failed to marshal request: %w" , err )
}
2026-06-03 16:33:58 +08:00
url := fmt . Sprintf ( "%s/%s" , baseURL , strings . TrimPrefix ( z . baseModel . URLSuffix . OCR , "/" ) )
2026-06-02 03:27:26 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , longOpCallTimeout )
defer cancel ( )
2026-06-03 16:33:58 +08:00
req , err := http . NewRequestWithContext ( ctx , "POST" , url , bytes . NewBuffer ( jsonData ) )
2026-05-25 16:50:06 -10:00
if err != nil {
return nil , fmt . Errorf ( "failed to create request: %w" , err )
}
req . Header . Set ( "Content-Type" , "application/json" )
req . Header . Set ( "Authorization" , fmt . Sprintf ( "Bearer %s" , * apiConfig . ApiKey ) )
2026-06-03 16:33:58 +08:00
resp , err := z . baseModel . httpClient . Do ( req )
2026-05-25 16:50:06 -10:00
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 OCR API error: %s, body: %s" , resp . Status , string ( body ) )
}
var zhipuResp zhipuOCRResponse
if err = json . Unmarshal ( body , & zhipuResp ) ; err != nil {
return nil , fmt . Errorf ( "failed to parse response: %w" , err )
}
if zhipuResp . MarkdownResults == nil {
return nil , fmt . Errorf ( "ZhipuAI OCR API response missing md_results" )
}
return & OCRFileResponse { Text : zhipuResp . MarkdownResults } , nil
2026-05-12 17:17:44 +08:00
}
2026-05-15 12:29:52 +08:00
// ParseFile parse file
func ( z * ZhipuAIModel ) ParseFile ( modelName * string , content [ ] byte , url * string , apiConfig * APIConfig , parseFileConfig * ParseFileConfig ) ( * ParseFileResponse , error ) {
return nil , fmt . Errorf ( "%s, no such method" , z . Name ( ) )
}
func ( z * ZhipuAIModel ) ListTasks ( apiConfig * APIConfig ) ( [ ] ListTaskStatus , error ) {
return nil , fmt . Errorf ( "%s, no such method" , z . Name ( ) )
}
func ( z * ZhipuAIModel ) ShowTask ( taskID string , apiConfig * APIConfig ) ( * TaskResponse , error ) {
return nil , fmt . Errorf ( "%s, no such method" , z . Name ( ) )
}