fix(grok): cache Claude Desktop client tools

This commit is contained in:
superman2003 committed 2026-07-19 16:03:18 +08:00
1 parent 9d79b42729
commit eea02c38fc
3 files changed
+308 -3

No files matched your search

@@ -287,6 +287,174 @@ func TestGrokFreeClientToolCacheRequestOptIn(t *testing.T) {
require.Equal(t, "x_search", tools[2].Get("type").String())
}
func TestGrokFreeClientToolCacheClaudeDesktopResponsesAutoOptIn(t *testing.T) {
account := healthyGrokOAuthGatewayTestAccount(90141, "access-token")
account.Credentials["subscription_tier"] = "free"
body := []byte(`{"model":"grok","tools":[{"type":"function","name":"Read","parameters":{"type":"object"}},{"type":"function","name":"Edit","parameters":{"type":"object"}}],"tool_choice":"auto"}`)
for _, xApp := range []string{"cli", "cli-bg"} {
t.Run(xApp, func(t *testing.T) {
c := newGrokCacheTestContext(90141)
// The desktop marker text is intentionally not required; CC Switch and
// Claude Desktop may change the descriptive User-Agent suffix.
c.Request.Header.Set("User-Agent", "claude-cli/2.1.215 (external, future-desktop, agent-sdk/0.3.215)")
c.Request.Header.Set("X-App", xApp)
c.Request.Header.Set("anthropic-client-platform", "desktop_app")
c.Request.Header.Set("X-Claude-Code-Session-Id", "desktop-session-1")
patched, err := applyGrokFreeRequestToolCacheRoute(c, body, body, account, "isolated-id")
require.NoError(t, err)
tools := gjson.GetBytes(patched, "tools").Array()
require.Len(t, tools, 4)
require.Equal(t, "Read", tools[0].Get("name").String())
require.Equal(t, "Edit", tools[1].Get("name").String())
require.Equal(t, "web_search", tools[2].Get("type").String())
require.Equal(t, "x_search", tools[3].Get("type").String())
})
}
}
func TestGrokFreeClientToolCacheClaudeDesktopFingerprintRequiresAllSignals(t *testing.T) {
account := healthyGrokOAuthGatewayTestAccount(90142, "access-token")
account.Credentials["subscription_tier"] = "free"
body := []byte(`{"model":"grok","tools":[{"type":"function","name":"Read","parameters":{"type":"object"}}],"tool_choice":"auto"}`)
tests := []struct {
name string
path string
ua string
xApp string
platform string
session string
}{
{
name: "chat path",
path: "/v1/chat/completions",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
xApp: "cli",
platform: "desktop_app",
session: "desktop-session-1",
},
{
name: "compact responses path",
path: "/v1/responses/compact",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
xApp: "cli",
platform: "desktop_app",
session: "desktop-session-1",
},
{
name: "non claude cli user agent",
path: "/v1/responses",
ua: "Mozilla/5.0 (claude-desktop-3p)",
xApp: "cli",
platform: "desktop_app",
session: "desktop-session-1",
},
{
name: "missing x app",
path: "/v1/responses",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
platform: "desktop_app",
session: "desktop-session-1",
},
{
name: "wrong x app",
path: "/v1/responses",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
xApp: "desktop",
platform: "desktop_app",
session: "desktop-session-1",
},
{
name: "missing client platform",
path: "/v1/responses",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
xApp: "cli",
session: "desktop-session-1",
},
{
name: "wrong client platform",
path: "/v1/responses",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
xApp: "cli",
platform: "web",
session: "desktop-session-1",
},
{
name: "missing session header",
path: "/v1/responses",
ua: "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)",
xApp: "cli",
platform: "desktop_app",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newGrokCacheTestContext(90142)
c.Request.URL.Path = tt.path
if tt.ua != "" {
c.Request.Header.Set("User-Agent", tt.ua)
}
if tt.xApp != "" {
c.Request.Header.Set("X-App", tt.xApp)
}
if tt.platform != "" {
c.Request.Header.Set("anthropic-client-platform", tt.platform)
}
if tt.session != "" {
c.Request.Header.Set("X-Claude-Code-Session-Id", tt.session)
}
patched, err := applyGrokFreeRequestToolCacheRoute(c, body, body, account, "isolated-id")
require.NoError(t, err)
require.JSONEq(t, string(body), string(patched))
})
}
}
func TestGrokFreeClientToolCacheClaudeDesktopExplicitRequestOptOut(t *testing.T) {
account := healthyGrokOAuthGatewayTestAccount(90144, "access-token")
account.Credentials["subscription_tier"] = "free"
account.Extra = map[string]any{grokClientToolCacheOptInExtraKey: true}
body := []byte(`{"model":"grok","tools":[{"type":"function","name":"Read","parameters":{"type":"object"}}],"tool_choice":"auto"}`)
for _, value := range []string{"0", "false", "no", "off"} {
t.Run(value, func(t *testing.T) {
c := newGrokCacheTestContext(90144)
c.Request.Header.Set("User-Agent", "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)")
c.Request.Header.Set("X-App", "cli")
c.Request.Header.Set("anthropic-client-platform", "desktop_app")
c.Request.Header.Set("X-Claude-Code-Session-Id", "desktop-session-1")
c.Request.Header.Set(grokClientToolCacheOptInHeader, value)
patched, err := applyGrokFreeRequestToolCacheRoute(c, body, body, account, "isolated-id")
require.NoError(t, err)
require.JSONEq(t, string(body), string(patched))
})
}
}
func TestGrokFreeClientToolCacheClaudeDesktopAutoOptInDoesNotOverridePaidTier(t *testing.T) {
account := healthyGrokOAuthGatewayTestAccount(90143, "access-token")
account.Credentials["subscription_tier"] = "supergrok"
c := newGrokCacheTestContext(90143)
c.Request.Header.Set("User-Agent", "claude-cli/2.1.215 (external, claude-desktop-3p, agent-sdk/0.3.215)")
c.Request.Header.Set("X-App", "cli")
c.Request.Header.Set("anthropic-client-platform", "desktop_app")
c.Request.Header.Set("X-Claude-Code-Session-Id", "desktop-session-1")
body := []byte(`{"model":"grok","tools":[{"type":"function","name":"Read","parameters":{"type":"object"}}],"tool_choice":"auto"}`)
patched, err := applyGrokFreeRequestToolCacheRoute(c, body, body, account, "isolated-id")
require.NoError(t, err)
require.JSONEq(t, string(body), string(patched))
}
func TestGrokFreeRequestClientSearchFunctionRequiresOptIn(t *testing.T) {
account := healthyGrokOAuthGatewayTestAccount(9015, "access-token")
account.Credentials["subscription_tier"] = "free"