mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-11-28 03:07:08 +08:00
fix(openai): keep xAI server-side search out of rebuilt buffered tool calls
A buffered request on a Grok account rebuilds an empty terminal output from the done items and renders its custom_tool_call items as tool_use or tool_calls. An item that apicompat.IsServerSideSearchCall accepts is a search xAI ran itself, so it is no longer rendered as a client tool call. The item stays in the output, so Grok SearchCount still counts it, and stop_reason and finish_reason follow the remaining content. A new test covers an empty terminal on /v1/messages and /v1/chat/completions, with and without a client tool next to the search.
This commit is contained in:
1 parent
7f1ecfaa2d
commit
413f1cb08a
6 files changed
+77
-3
No files matched your search
@@ -55,7 +55,7 @@ func ResponsesToAnthropic(resp *ResponsesResponse, model string) *AnthropicRespo
|
||||
Input: anthropicToolUseInput(item.Type, item.Name, item.Arguments),
|
||||
})
|
||||
case "custom_tool_call":
|
||||
if !resp.RenderCustomToolCalls {
|
||||
if !resp.RenderCustomToolCalls || (resp.XAIServerSideSearch && IsServerSideSearchCall(item.Type, item.CallID)) {
|
||||
continue
|
||||
}
|
||||
blocks = append(blocks, AnthropicContentBlock{
|
||||
|
||||
@@ -57,7 +57,7 @@ func ResponsesToChatCompletions(resp *ResponsesResponse, model string) *ChatComp
|
||||
},
|
||||
})
|
||||
case "custom_tool_call":
|
||||
if !resp.RenderCustomToolCalls {
|
||||
if !resp.RenderCustomToolCalls || (resp.XAIServerSideSearch && IsServerSideSearchCall(item.Type, item.CallID)) {
|
||||
continue
|
||||
}
|
||||
toolCalls = append(toolCalls, ChatToolCall{
|
||||
|
||||
@@ -389,6 +389,11 @@ type ResponsesResponse struct {
|
||||
// the accumulator has always handed custom_tool_call items to the converters
|
||||
// as tool calls. An output the upstream delivered keeps them unrendered.
|
||||
RenderCustomToolCalls bool `json:"-"`
|
||||
|
||||
// XAIServerSideSearch is set with RenderCustomToolCalls when the upstream is
|
||||
// xAI. A custom_tool_call that IsServerSideSearchCall then stays in the
|
||||
// output but is not rendered as a tool call, because xAI runs it itself.
|
||||
XAIServerSideSearch bool `json:"-"`
|
||||
}
|
||||
|
||||
// ResponsesError describes an error in a failed response.
|
||||
|
||||
@@ -80,6 +80,9 @@ type openAICompatOutputSlot struct {
|
||||
// so that an empty terminal output can be rebuilt from them.
|
||||
type openAICompatOutputSlots struct {
|
||||
slots []*openAICompatOutputSlot
|
||||
// xaiServerSideSearch is set when the upstream is xAI, so that the rebuilt
|
||||
// output keeps a server-side search call out of the client tool calls.
|
||||
xaiServerSideSearch bool
|
||||
}
|
||||
|
||||
func openAICompatIsToolCallType(itemType string) bool {
|
||||
@@ -291,6 +294,7 @@ func (o *openAICompatOutputSlots) fillTerminalOutput(eventType string, response
|
||||
response.Output = append(response.Output, item)
|
||||
if item.Type == "custom_tool_call" {
|
||||
response.RenderCustomToolCalls = true
|
||||
response.XAIServerSideSearch = o.xaiServerSideSearch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// A nonstreaming request rebuilds an empty terminal output from the output_item.done items and
|
||||
// renders its custom_tool_call items as tool calls. On xAI an xs_ / as_ item is a search the
|
||||
// server ran, so it is not a tool call but still counts as a Grok search.
|
||||
func TestGrokBufferedRebuiltOutputKeepsServerSideSearchOutOfToolCalls(t *testing.T) {
|
||||
account := healthyGrokOAuthGatewayTestAccount(4242, "access-token")
|
||||
for _, path := range []string{"/v1/messages", "/v1/chat/completions"} {
|
||||
t.Run(path+" search only", func(t *testing.T) {
|
||||
sse := grokCustomToolCallStream([]string{grokXSearchItemJSON}, `[]`)
|
||||
got := forwardGrokCustomToolCallStream(t, path, false, account, sse)
|
||||
|
||||
require.Equal(t, 1, got.searchCount)
|
||||
require.NotContains(t, got.body, "tool_use", got.body)
|
||||
require.NotContains(t, got.body, "tool_calls", got.body)
|
||||
require.NotContains(t, got.body, "x_keyword_search", got.body)
|
||||
require.Contains(t, got.body, "found it", got.body)
|
||||
if path == "/v1/messages" {
|
||||
require.Contains(t, got.body, `"stop_reason":"end_turn"`, got.body)
|
||||
} else {
|
||||
require.Contains(t, got.body, `"finish_reason":"stop"`, got.body)
|
||||
}
|
||||
})
|
||||
t.Run(path+" search next to a client tool", func(t *testing.T) {
|
||||
sse := grokCustomToolCallStream([]string{grokXSearchItemJSON, grokClientToolJSON}, `[]`)
|
||||
got := forwardGrokCustomToolCallStream(t, path, false, account, sse)
|
||||
|
||||
require.Equal(t, 1, got.searchCount)
|
||||
require.NotContains(t, got.body, "x_keyword_search", got.body)
|
||||
require.Contains(t, got.body, "exec", got.body)
|
||||
if path == "/v1/messages" {
|
||||
require.Contains(t, got.body, `"stop_reason":"tool_use"`, got.body)
|
||||
} else {
|
||||
require.Contains(t, got.body, `"finish_reason":"tool_calls"`, got.body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Off xAI the same call_id is an ordinary custom tool call and stays one.
|
||||
func TestOpenAIBufferedRebuiltOutputKeepsXSCallIDAsToolCall(t *testing.T) {
|
||||
account := rawChatCompletionsTestAccount()
|
||||
for _, path := range []string{"/v1/messages", "/v1/chat/completions"} {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
sse := grokCustomToolCallStream([]string{grokXSearchItemJSON}, `[]`)
|
||||
got := forwardGrokCustomToolCallStream(t, path, false, account, sse)
|
||||
|
||||
require.Equal(t, 0, got.searchCount)
|
||||
require.Contains(t, got.body, "x_keyword_search", got.body)
|
||||
if path == "/v1/messages" {
|
||||
require.Contains(t, got.body, "tool_use", got.body)
|
||||
} else {
|
||||
require.Contains(t, got.body, "tool_calls", got.body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -835,7 +835,7 @@ func (s *OpenAIGatewayService) readOpenAICompatBufferedTerminal(
|
||||
) (*apicompat.ResponsesResponse, OpenAIUsage, *apicompat.BufferedResponseAccumulator, openAICompatPriorModelAudit, error) {
|
||||
acc := apicompat.NewBufferedResponseAccumulator()
|
||||
acc.XAIServerSideSearch = account != nil && account.IsGrok()
|
||||
outputSlots := &openAICompatOutputSlots{}
|
||||
outputSlots := &openAICompatOutputSlots{xaiServerSideSearch: acc.XAIServerSideSearch}
|
||||
var usage OpenAIUsage
|
||||
if resp == nil || resp.Body == nil {
|
||||
return nil, usage, acc, openAICompatPriorModelAudit{}, errors.New("upstream response body is nil")
|
||||
|
||||
Reference in new issue
Block a user