From 7a6adfa97edd3413ef8972e598ce9cf139ba12f3 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Wed, 27 Aug 2025 22:29:08 +0800 Subject: [PATCH] Suppress debug logs for model routing and ignore empty tools arrays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Comment out verbose routing logs in the API server to reduce noise. - Remove the `tools` field from Qwen client requests when it is an empty array. - Add guards in Claude, Codex, Gemini‑CLI, and Gemini translators to skip tool conversion when the `tools` array is empty, preventing unnecessary payload modifications. --- internal/api/server.go | 4 ++-- internal/client/qwen_client.go | 5 +++++ internal/translator/claude/openai/claude_openai_request.go | 2 +- internal/translator/codex/openai/codex_openai_request.go | 2 +- internal/translator/gemini-cli/openai/cli_openai_request.go | 2 +- internal/translator/gemini/openai/gemini_openai_request.go | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/internal/api/server.go b/internal/api/server.go index 0c636cc7..381c765e 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -140,10 +140,10 @@ func (s *Server) unifiedModelsHandler(openaiHandler *openai.OpenAIAPIHandler, cl // Route to Claude handler if User-Agent starts with "claude-cli" if strings.HasPrefix(userAgent, "claude-cli") { - log.Debugf("Routing /v1/models to Claude handler for User-Agent: %s", userAgent) + // log.Debugf("Routing /v1/models to Claude handler for User-Agent: %s", userAgent) claudeHandler.ClaudeModels(c) } else { - log.Debugf("Routing /v1/models to OpenAI handler for User-Agent: %s", userAgent) + // log.Debugf("Routing /v1/models to OpenAI handler for User-Agent: %s", userAgent) openaiHandler.OpenAIModels(c) } } diff --git a/internal/client/qwen_client.go b/internal/client/qwen_client.go index 86d456ea..84cbc078 100644 --- a/internal/client/qwen_client.go +++ b/internal/client/qwen_client.go @@ -329,6 +329,11 @@ func (c *QwenClient) APIRequest(ctx context.Context, modelName, endpoint string, } } + toolsResult := gjson.GetBytes(jsonBody, "tools") + if toolsResult.IsArray() && len(toolsResult.Array()) == 0 { + jsonBody, _ = sjson.DeleteBytes(jsonBody, "tools") + } + streamResult := gjson.GetBytes(jsonBody, "stream") if streamResult.Exists() && streamResult.Type == gjson.True { jsonBody, _ = sjson.SetBytes(jsonBody, "stream_options.include_usage", true) diff --git a/internal/translator/claude/openai/claude_openai_request.go b/internal/translator/claude/openai/claude_openai_request.go index 6e3243d3..b65334bf 100644 --- a/internal/translator/claude/openai/claude_openai_request.go +++ b/internal/translator/claude/openai/claude_openai_request.go @@ -244,7 +244,7 @@ func ConvertOpenAIRequestToClaude(modelName string, rawJSON []byte, stream bool) } // Tools mapping: OpenAI tools -> Claude Code tools - if tools := root.Get("tools"); tools.Exists() && tools.IsArray() { + if tools := root.Get("tools"); tools.Exists() && tools.IsArray() && len(tools.Array()) > 0 { var anthropicTools []interface{} tools.ForEach(func(_, tool gjson.Result) bool { if tool.Get("type").String() == "function" { diff --git a/internal/translator/codex/openai/codex_openai_request.go b/internal/translator/codex/openai/codex_openai_request.go index 9d029ea7..da953d6b 100644 --- a/internal/translator/codex/openai/codex_openai_request.go +++ b/internal/translator/codex/openai/codex_openai_request.go @@ -231,7 +231,7 @@ func ConvertOpenAIRequestToCodex(modelName string, rawJSON []byte, stream bool) // Map tools (flatten function fields) tools := gjson.GetBytes(rawJSON, "tools") - if tools.IsArray() { + if tools.IsArray() && len(tools.Array()) > 0 { out, _ = sjson.SetRaw(out, "tools", `[]`) arr := tools.Array() for i := 0; i < len(arr); i++ { diff --git a/internal/translator/gemini-cli/openai/cli_openai_request.go b/internal/translator/gemini-cli/openai/cli_openai_request.go index 315d5fa4..b21a0f8f 100644 --- a/internal/translator/gemini-cli/openai/cli_openai_request.go +++ b/internal/translator/gemini-cli/openai/cli_openai_request.go @@ -215,7 +215,7 @@ func ConvertOpenAIRequestToGeminiCLI(modelName string, rawJSON []byte, _ bool) [ // tools -> request.tools[0].functionDeclarations tools := gjson.GetBytes(rawJSON, "tools") - if tools.IsArray() { + if tools.IsArray() && len(tools.Array()) > 0 { out, _ = sjson.SetRawBytes(out, "request.tools", []byte(`[{"functionDeclarations":[]}]`)) fdPath := "request.tools.0.functionDeclarations" for _, t := range tools.Array() { diff --git a/internal/translator/gemini/openai/gemini_openai_request.go b/internal/translator/gemini/openai/gemini_openai_request.go index f1be1e97..71590fdd 100644 --- a/internal/translator/gemini/openai/gemini_openai_request.go +++ b/internal/translator/gemini/openai/gemini_openai_request.go @@ -215,7 +215,7 @@ func ConvertOpenAIRequestToGemini(modelName string, rawJSON []byte, _ bool) []by // tools -> tools[0].functionDeclarations tools := gjson.GetBytes(rawJSON, "tools") - if tools.IsArray() { + if tools.IsArray() && len(tools.Array()) > 0 { out, _ = sjson.SetRawBytes(out, "tools", []byte(`[{"functionDeclarations":[]}]`)) fdPath := "tools.0.functionDeclarations" for _, t := range tools.Array() {