Skip to content

Commit 400c6bb

Browse files
committed
fix: robust isWebSearchRequest
- Implement robust checking for web search requests by first looking for the special marker added by the translator - Add fallback to check for exactly one tool with type matching /^web_search/ - Import encoding/json for JSON parsing functionality This implementation is more robust than the previous simple byte search approach, while maintaining compatibility with the existing marker-based detection.
1 parent d5d2e53 commit 400c6bb

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

internal/runtime/executor/openai_compat_executor.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"bytes"
66
"context"
7+
"encoding/json"
78
"fmt"
89
"io"
910
"net/http"
@@ -423,9 +424,30 @@ func (e statusErr) Error() string {
423424
func (e statusErr) StatusCode() int { return e.code }
424425

425426
// isWebSearchRequest checks if the translated request is a web search request
426-
// by looking for the special marker we add in ConvertClaudeRequestToOpenAI
427+
// by checking if it has exactly one tool that matches /^web_search/ or if it has the special marker
427428
func isWebSearchRequest(translated []byte) bool {
428-
// Check if the translated request has the web search marker
429-
// This looks for the "_web_search_request":true field we add
430-
return bytes.Contains(translated, []byte("\"_web_search_request\":true"))
429+
// First check for the special marker that the translator adds
430+
if bytes.Contains(translated, []byte("\"_web_search_request\":true")) {
431+
return true
432+
}
433+
434+
var req map[string]interface{}
435+
if err := json.Unmarshal(translated, &req); err != nil {
436+
return false
437+
}
438+
439+
// Check if tools exist and is an array
440+
tools, ok := req["tools"].([]interface{})
441+
if !ok || len(tools) != 1 {
442+
return false
443+
}
444+
445+
// Check if the single tool has a type that matches /^web_search/
446+
if tool, ok := tools[0].(map[string]interface{}); ok {
447+
if toolType, ok := tool["type"].(string); ok {
448+
return strings.HasPrefix(toolType, "web_search")
449+
}
450+
}
451+
452+
return false
431453
}

0 commit comments

Comments
 (0)