Files
sub2api/backend/internal/service/grok_search_count_test.go
T
Anankke 70ad8365d9 fix(grok): keep xAI server-side search calls out of tool calls
On xAI upstreams a custom_tool_call whose call_id starts with xs_ or as_ is an xAI server-side search call, as the xAI tool usage docs define the prefixes.
The Messages and Chat Completions stream converters and the buffered accumulator no longer return such items as tool_use or tool calls, and stop_reason and finish_reason follow the remaining content.
Grok SearchCount counts each such item once; client custom tools and non-xAI upstreams are unchanged.
2026-11-16 00:40:53 -08:00

105 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCountGrokNativeSearchCallsFromJSONBytes(t *testing.T) {
t.Parallel()
require.Equal(t, 0, countGrokNativeSearchCallsFromJSONBytes(nil))
require.Equal(t, 0, countGrokNativeSearchCallsFromJSONBytes([]byte(`{"output":[]}`)))
body := []byte(`{"output":[
{"type":"web_search_call","id":"ws1","status":"completed"},
{"type":"x_search_call","id":"xs1"},
{"type":"function_call","name":"tool_search","call_id":"ts1"},
{"type":"function_call","name":"lookup","call_id":"other"}
]}`)
require.Equal(t, 3, countGrokNativeSearchCallsFromJSONBytes(body))
}
// xAI runs X Search (call_id xs_) and attachment search (as_) on its own server and reports
// them as custom_tool_call items. A custom tool the client declared keeps its call_ id.
func TestCountGrokNativeSearchCalls_ServerSideSearchCustomToolCall(t *testing.T) {
t.Parallel()
xSearch := `{"type":"custom_tool_call","id":"ctc_1","call_id":"xs_1","name":"x_keyword_search","input":"{}"}`
attachment := `{"type":"custom_tool_call","id":"ctc_2","call_id":"as_1","name":"attachment_search","input":"{}"}`
client := `{"type":"custom_tool_call","id":"ctc_3","call_id":"call_1","name":"exec","input":"dir"}`
require.Equal(t, 2, countGrokNativeSearchCallsFromJSONBytes([]byte(`{"output":[`+xSearch+`,`+attachment+`,`+client+`]}`)))
require.Equal(t, 0, countGrokNativeSearchCallsFromJSONBytes([]byte(`{"output":[`+client+`]}`)))
sse := stringsJoin(
`data: {"type":"response.output_item.done","item":`+xSearch+`}`,
`data: {"type":"response.output_item.done","item":`+client+`}`,
`data: {"type":"response.completed","response":{"output":[`+xSearch+`,`+client+`]}}`,
)
require.Equal(t, 1, countGrokNativeSearchCallsFromSSEBody(sse), "item.done and the terminal output list the same call once")
}
func TestCountGrokNativeSearchCallsFromJSONBytes_PrefersNestedResponse(t *testing.T) {
t.Parallel()
body := []byte(`{"output":[{"type":"web_search_call","id":"duplicate"}],"response":{"output":[{"type":"web_search_call","id":"duplicate"},{"type":"x_search_call","id":"xs1"}]}}`)
require.Equal(t, 2, countGrokNativeSearchCallsFromJSONBytes(body))
}
func TestCountGrokNativeSearchCallsFromJSONBytes_FallsBackWhenNestedOutputNull(t *testing.T) {
t.Parallel()
body := []byte(`{"output":[{"type":"web_search_call","id":"ws1"}],"response":{"output":null}}`)
require.Equal(t, 1, countGrokNativeSearchCallsFromJSONBytes(body))
}
func TestCountGrokNativeSearchCallsFromSSEBodyDedups(t *testing.T) {
t.Parallel()
sse := stringsJoin(
`data: {"type":"response.output_item.done","item":{"type":"web_search_call","id":"ws1","call_id":"c1"}}`,
`data: {"type":"response.output_item.done","item":{"type":"web_search_call","id":"ws1","call_id":"c1"}}`,
`data: {"type":"response.completed","response":{"output":[{"type":"web_search_call","id":"ws1","call_id":"c1"},{"type":"x_search_call","id":"xs1","call_id":"c2"}]}}`,
)
require.Equal(t, 2, countGrokNativeSearchCallsFromSSEBody(sse))
}
func TestCountGrokNativeSearchCallsInSSEDataDedup_LiveStreamPath(t *testing.T) {
t.Parallel()
// Mirrors the live streaming accumulator: item.done then response.completed
// for the same call_id must bill once (regression for ~2× surcharge).
seen := make(map[string]struct{})
done := []byte(`{"type":"response.output_item.done","item":{"type":"web_search_call","id":"ws1","call_id":"c1"}}`)
completed := []byte(`{"type":"response.completed","response":{"output":[{"type":"web_search_call","id":"ws1","call_id":"c1"},{"type":"x_search_call","id":"xs1","call_id":"c2"}]}}`)
require.Equal(t, 1, countGrokNativeSearchCallsInSSEDataDedup(done, seen))
require.Equal(t, 1, countGrokNativeSearchCallsInSSEDataDedup(completed, seen))
// Raw (no-dedup) path still double-counts the same envelope pair.
require.Equal(t, 1, countGrokNativeSearchCallsInSSEData(done))
require.Equal(t, 2, countGrokNativeSearchCallsInSSEData(completed))
}
func TestCountGrokNativeSearchCallsInSSEDataDedup_NoIDStillDedups(t *testing.T) {
t.Parallel()
// Upstream sometimes omits call_id/id; synthetic keys must still prevent 2×.
seen := make(map[string]struct{})
done := []byte(`{"type":"response.output_item.done","item":{"type":"web_search_call"}}`)
completed := []byte(`{"type":"response.completed","response":{"output":[{"type":"web_search_call"}]}}`)
require.Equal(t, 1, countGrokNativeSearchCallsInSSEDataDedup(done, seen))
require.Equal(t, 0, countGrokNativeSearchCallsInSSEDataDedup(completed, seen))
}
func TestCountGrokNativeSearchCallsInSSEDataDedup_MultipleNoIDCalls(t *testing.T) {
t.Parallel()
seen := make(map[string]struct{})
firstDone := []byte(`{"type":"response.output_item.done","item":{"type":"web_search_call"}}`)
secondDone := []byte(`{"type":"response.output_item.done","item":{"type":"web_search_call"}}`)
completed := []byte(`{"type":"response.completed","response":{"output":[{"type":"web_search_call"},{"type":"web_search_call"}]}}`)
require.Equal(t, 1, countGrokNativeSearchCallsInSSEDataDedup(firstDone, seen))
require.Equal(t, 1, countGrokNativeSearchCallsInSSEDataDedup(secondDone, seen))
require.Equal(t, 0, countGrokNativeSearchCallsInSSEDataDedup(completed, seen))
}
func stringsJoin(lines ...string) string {
out := ""
for _, l := range lines {
out += l + "\n\n"
}
return out
}