diff --git a/internal/runtime/executor/claude_executor_execute.go b/internal/runtime/executor/claude_executor_execute.go index c1c99f411c..b3fa62a9e5 100644 --- a/internal/runtime/executor/claude_executor_execute.go +++ b/internal/runtime/executor/claude_executor_execute.go @@ -80,6 +80,9 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r ctx = helps.WithClaudeSessionID(ctx, claudeSessionID) } + webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) + ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL) + isCompat := helps.APIKeyModelIsCompat(req) originalTranslated, body, err := helps.TranslateRequestPairReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, req.Payload, upstreamStream, isCompat) if err != nil { @@ -92,7 +95,6 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil { return resp, err } - webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { rebuildMidSystemMessagesToTopLevelDoc(doc) diff --git a/internal/runtime/executor/claude_executor_stream.go b/internal/runtime/executor/claude_executor_stream.go index aa42c0303d..6d383cf690 100644 --- a/internal/runtime/executor/claude_executor_stream.go +++ b/internal/runtime/executor/claude_executor_stream.go @@ -85,6 +85,9 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A ctx = helps.WithClaudeSessionID(ctx, claudeSessionID) } + webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) + ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL) + isCompat := helps.APIKeyModelIsCompat(req) originalTranslated, body, err := helps.TranslateRequestPairReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, originalPayload, req.Payload, true, isCompat) if err != nil { @@ -97,7 +100,6 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A if err = helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); err != nil { return nil, err } - webSearchSessionKey, webSearchTTL, webSearchLimit := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { rebuildMidSystemMessagesToTopLevelDoc(doc) diff --git a/internal/runtime/executor/claude_executor_tokens.go b/internal/runtime/executor/claude_executor_tokens.go index 8db878fa52..5fd9fd6b96 100644 --- a/internal/runtime/executor/claude_executor_tokens.go +++ b/internal/runtime/executor/claude_executor_tokens.go @@ -37,6 +37,13 @@ func (e *ClaudeExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts) to := sdktranslator.FromString("claude") + originalPayload := req.Payload + if len(opts.OriginalRequest) > 0 { + originalPayload = opts.OriginalRequest + } + webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) + ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL) + // Use streaming translation to preserve function calling, except for claude. stream := from != to body, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, stream, helps.APIKeyModelIsCompat(req)) @@ -53,11 +60,6 @@ func (e *ClaudeExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut if errThinking := helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); errThinking != nil { return cliproxyexecutor.Response{}, errThinking } - originalPayload := req.Payload - if len(opts.OriginalRequest) > 0 { - originalPayload = opts.OriginalRequest - } - webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { rebuildMidSystemMessagesToTopLevelDoc(doc) @@ -164,6 +166,8 @@ func (e *ClaudeExecutor) countTokensUpstream(ctx context.Context, auth *cliproxy if fp.ProfileClaudeCodeCLI { claudeSessionID = helps.ClaudeAgentSessionUUIDForRequest(incomingHeaders, originalPayload, req.Payload, confirmedClaudeCode, opts.Metadata, req.Metadata) } + webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) + ctx = helps.WithClaudeWebSearchResultRestore(ctx, webSearchSessionKey, webSearchTTL) // Use streaming translation to preserve function calling, except for claude. stream := from != to body, err := helps.TranslateRequestReturningError(ctx, opts.Headers, e.cfg, from, to, baseModel, req.Payload, stream, helps.APIKeyModelIsCompat(req)) @@ -180,7 +184,6 @@ func (e *ClaudeExecutor) countTokensUpstream(ctx context.Context, auth *cliproxy if errThinking := helps.ApplyRequestThinkingDoc(doc, req, opts, from.String(), to.String(), e.Identifier()); errThinking != nil { return cliproxyexecutor.Response{}, errThinking } - webSearchSessionKey, webSearchTTL, _ := e.claudeWebSearchResultCacheScope(ctx, auth, from, req, opts, originalPayload) helps.RestoreClaudeWebSearchResultsContextDoc(ctx, doc, webSearchSessionKey, webSearchTTL) if rebuildMidSystemMessageEnabled(e.cfg, auth) { rebuildMidSystemMessagesToTopLevelDoc(doc) diff --git a/internal/runtime/executor/claude_executor_web_search_results_test.go b/internal/runtime/executor/claude_executor_web_search_results_test.go index 4337cc70a1..98b2057c6e 100644 --- a/internal/runtime/executor/claude_executor_web_search_results_test.go +++ b/internal/runtime/executor/claude_executor_web_search_results_test.go @@ -143,10 +143,11 @@ func runClaudeWebSearchScenario(t *testing.T, cfg *config.Config, stream bool, r const ( claudeWebSearchExecutorUser = `{"type":"message","role":"user","content":[{"type":"input_text","text":"search for q"}]}` - // The history as Codex stores it: the web_search_call has no results. + // The history as Codex stores it: the web_search_call has no results, while + // the assistant message keeps its web_search_result_location annotations. claudeWebSearchExecutorHistory = claudeWebSearchExecutorUser + `,{"type":"web_search_call","id":"ws_srvtoolu_01abc","status":"completed","action":{"type":"search","query":"q"}},` + - `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"found two pages"}]},` + + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"found two pages","annotations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"found two pages","encrypted_index":"IDX_A"}]}]},` + `{"type":"message","role":"user","content":[{"type":"input_text","text":"how many results?"}]}` ) @@ -185,12 +186,21 @@ func TestClaudeExecutor_RestoresWebSearchResultsDroppedByResponsesClient(t *test if got := claudeWebSearchResultContent(t, bodies[1]); got != claudeWebSearchExecutorResults { t.Fatalf("same session: web_search_tool_result content = %s, want original results", got) } + if got := ojson.GetBytes(bodies[1], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" { + t.Fatalf("same session: expected restored search results to keep citations, got %q in %s", got, bodies[1]) + } if got := claudeWebSearchResultContent(t, bodies[2]); got != "[]" { t.Fatalf("other session: web_search_tool_result content = %s, want []", got) } + if citations := ojson.GetBytes(bodies[2], "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("other session: expected unbacked citations to be dropped, got %s", citations.Raw) + } if got := claudeWebSearchResultContent(t, bodies[3]); got != "[]" { t.Fatalf("no session identity: web_search_tool_result content = %s, want []", got) } + if citations := ojson.GetBytes(bodies[3], "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("no session identity: expected unbacked citations to be dropped, got %s", citations.Raw) + } }) } } @@ -208,6 +218,12 @@ func TestClaudeExecutor_WebSearchResultCacheDisabledByDefault(t *testing.T) { if got := claudeWebSearchResultContent(t, bodies[1]); got != "[]" { t.Fatalf("disabled cache: web_search_tool_result content = %s, want []", got) } + if citations := ojson.GetBytes(bodies[1], "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("disabled cache: expected unbacked citations to be dropped, got %s", citations.Raw) + } + if got := ojson.GetBytes(bodies[1], "messages.1.content.2.text").String(); got != "found two pages" { + t.Fatalf("disabled cache: expected text to remain intact, got %q", got) + } }) } } @@ -267,9 +283,15 @@ func TestClaudeExecutor_WebSearchResultCacheHonorsConfiguredLimit(t *testing.T) if got := claudeWebSearchResultContent(t, bodies[2]); got != "[]" { t.Fatalf("evicted session-1: web_search_tool_result content = %s, want []", got) } + if citations := ojson.GetBytes(bodies[2], "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("evicted session-1: expected citations to be dropped, got %s", citations.Raw) + } if got := claudeWebSearchResultContent(t, bodies[3]); got != claudeWebSearchExecutorResults { t.Fatalf("retained session-2: web_search_tool_result content = %s, want original results", got) } + if got := ojson.GetBytes(bodies[3], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" { + t.Fatalf("retained session-2: expected citations to be kept, got %q", got) + } } func TestClaudeExecutor_CountTokensRestoresWebSearchResultsWhenEnabled(t *testing.T) { @@ -351,15 +373,27 @@ func TestClaudeExecutor_WebSearchResultCacheScopesByCredential(t *testing.T) { if got := claudeWebSearchResultContent(t, bodies[1]); got != "[]" { t.Fatalf("different credential ID in same session: web_search_tool_result content = %s, want []", got) } + if citations := ojson.GetBytes(bodies[1], "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("different credential ID in same session: expected citations to be dropped, got %s", citations.Raw) + } if got := claudeWebSearchResultContent(t, bodies[2]); got != claudeWebSearchExecutorResults { t.Fatalf("original credential ID in same session: web_search_tool_result content = %s, want original results", got) } + if got := ojson.GetBytes(bodies[2], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" { + t.Fatalf("original credential ID in same session: expected citations to be kept, got %q", got) + } if got := claudeWebSearchResultContent(t, bodies[4]); got != "[]" { t.Fatalf("different API key in same session: web_search_tool_result content = %s, want []", got) } + if citations := ojson.GetBytes(bodies[4], "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("different API key in same session: expected citations to be dropped, got %s", citations.Raw) + } if got := claudeWebSearchResultContent(t, bodies[5]); got != claudeWebSearchExecutorResults { t.Fatalf("original API key in same session: web_search_tool_result content = %s, want original results", got) } + if got := ojson.GetBytes(bodies[5], "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" { + t.Fatalf("original API key in same session: expected citations to be kept, got %q", got) + } }) } } diff --git a/internal/runtime/executor/helps/claude_web_search_results.go b/internal/runtime/executor/helps/claude_web_search_results.go index 0efad12457..34b88b6950 100644 --- a/internal/runtime/executor/helps/claude_web_search_results.go +++ b/internal/runtime/executor/helps/claude_web_search_results.go @@ -9,11 +9,32 @@ import ( "github.com/MoeForge/moego/ojson" "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" + responsesclaude "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/claude/openai/responses" + translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" log "github.com/sirupsen/logrus" ) -var claudeWebSearchToolResultMarker = []byte(`"web_search_tool_result"`) +var ( + claudeWebSearchToolResultMarker = []byte(`"web_search_tool_result"`) + claudeWebSearchCitationMarker = []byte(`"web_search_result_location"`) +) + +// WithClaudeWebSearchResultRestore attaches a request-scoped cache lookup so +// Responses -> Claude request translation can restore web_search_tool_result +// blocks before deciding whether assistant citations remain backed by results. +func WithClaudeWebSearchResultRestore(ctx context.Context, sessionKey string, ttl ...time.Duration) context.Context { + if strings.TrimSpace(sessionKey) == "" { + return ctx + } + return responsesclaude.WithWebSearchResultLookup(ctx, func(toolUseID string) ([]byte, bool) { + cached, ok, errGet := cache.GetClaudeWebSearchResultRequired(ctx, sessionKey, toolUseID, ttl...) + if errGet != nil || !ok || len(cached) == 0 { + return nil, false + } + return cached, true + }) +} // RecordClaudeWebSearchResults stores the web_search_tool_result blocks of a // Claude response for sessionKey. data is either a complete Messages response @@ -77,7 +98,10 @@ func RestoreClaudeWebSearchResultsDoc(doc *ojson.Doc, sessionKey string, ttl ... // blocks whose content is missing or empty with the content stored for // sessionKey using ctx for Home KV operations. func RestoreClaudeWebSearchResultsContext(ctx context.Context, body []byte, sessionKey string, ttl ...time.Duration) []byte { - if strings.TrimSpace(sessionKey) == "" || !bytes.Contains(body, claudeWebSearchToolResultMarker) { + if !bytes.Contains(body, claudeWebSearchToolResultMarker) { + return body + } + if strings.TrimSpace(sessionKey) == "" && !bytes.Contains(body, claudeWebSearchCitationMarker) { return body } doc := ojson.NewDoc(body) @@ -89,42 +113,109 @@ func RestoreClaudeWebSearchResultsContext(ctx context.Context, body []byte, sess // RestoreClaudeWebSearchResultsContextDoc fills assistant web_search_tool_result // blocks in doc whose content is missing or empty with the content stored for -// sessionKey using ctx for Home KV operations. +// sessionKey using ctx for Home KV operations, and drops web_search_result_location +// citations from assistant turns whose web_search_tool_result remains empty. func RestoreClaudeWebSearchResultsContextDoc(ctx context.Context, doc *ojson.Doc, sessionKey string, ttl ...time.Duration) bool { - if doc == nil || doc.Len() == 0 || strings.TrimSpace(sessionKey) == "" { + if doc == nil || doc.Len() == 0 { return false } messages := doc.Get("messages") if !messages.IsArray() { return false } + trimmedSessionKey := strings.TrimSpace(sessionKey) restored := 0 + droppedCitations := 0 + turnHasEmptyWebSearchResult := false for i, message := range messages.Array() { - if message.Get("role").String() != "assistant" { + role := message.Get("role").String() + if role != "assistant" { + if claudeUserMessageIsTurnBoundary(message) { + turnHasEmptyWebSearchResult = false + } continue } - for j, block := range message.Get("content").Array() { + blocks := message.Get("content").Array() + for j, block := range blocks { if block.Get("type").String() != "web_search_tool_result" || !claudeWebSearchResultContentEmpty(block.Get("content")) { continue } - cached, ok, errGet := cache.GetClaudeWebSearchResultRequired(ctx, sessionKey, block.Get("tool_use_id").String(), ttl...) - if errGet != nil || !ok { + if trimmedSessionKey != "" { + cached, ok, errGet := cache.GetClaudeWebSearchResultRequired(ctx, trimmedSessionKey, block.Get("tool_use_id").String(), ttl...) + if errGet == nil && ok && len(cached) > 0 { + if errSet := doc.SetRawBytes(fmt.Sprintf("messages.%d.content.%d.content", i, j), cached); errSet == nil { + restored++ + continue + } else { + log.Debugf("claude web search results: restore messages.%d.content.%d failed: %v", i, j, errSet) + } + } + } + turnHasEmptyWebSearchResult = true + } + if !turnHasEmptyWebSearchResult { + continue + } + for j, block := range blocks { + if block.Get("type").String() != "text" { continue } - if errSet := doc.SetRawBytes(fmt.Sprintf("messages.%d.content.%d.content", i, j), cached); errSet != nil { - log.Debugf("claude web search results: restore messages.%d.content.%d failed: %v", i, j, errSet) - continue - } - restored++ + dropped := stripClaudeWebSearchCitationsDoc(doc, fmt.Sprintf("messages.%d.content.%d", i, j), block.Get("citations")) + droppedCitations += dropped } } if restored > 0 { log.Debugf("claude web search results: restored %d result block(s) from cache", restored) + } + if droppedCitations > 0 { + log.Debugf("claude web search results: dropped %d web_search_result_location citation(s) from assistant turn without search results", droppedCitations) + } + return restored > 0 || droppedCitations > 0 +} + +func claudeUserMessageIsTurnBoundary(message ojson.Result) bool { + content := message.Get("content") + if !content.IsArray() { return true } + for _, block := range content.Array() { + if block.Get("type").String() != "tool_result" { + return true + } + } return false } +func stripClaudeWebSearchCitationsDoc(doc *ojson.Doc, blockPath string, citations ojson.Result) int { + if !citations.Exists() || !citations.IsArray() { + return 0 + } + var kept [][]byte + dropped := 0 + citations.ForEach(func(_, citation ojson.Result) bool { + if citation.Get("type").String() == "web_search_result_location" { + dropped++ + return true + } + kept = append(kept, []byte(citation.Raw)) + return true + }) + if dropped == 0 { + return 0 + } + citationsPath := blockPath + ".citations" + if len(kept) == 0 { + if errDelete := doc.Delete(citationsPath); errDelete != nil { + return 0 + } + return dropped + } + if errSet := doc.SetRawBytes(citationsPath, translatorcommon.JoinRawArray(kept)); errSet != nil { + return 0 + } + return dropped +} + func claudeWebSearchResultContentEmpty(content ojson.Result) bool { if !content.Exists() || content.Type == ojson.Null { return true diff --git a/internal/runtime/executor/helps/claude_web_search_results_test.go b/internal/runtime/executor/helps/claude_web_search_results_test.go index cd939aaec4..3f2ffb0483 100644 --- a/internal/runtime/executor/helps/claude_web_search_results_test.go +++ b/internal/runtime/executor/helps/claude_web_search_results_test.go @@ -83,3 +83,40 @@ func TestRecordClaudeWebSearchResultsIgnoresEmptyResults(t *testing.T) { t.Fatal("empty result content was cached") } } + +func TestRestoreClaudeWebSearchResultsDropsCitationsWhenResultRemainsEmpty(t *testing.T) { + resetClaudeWebSearchResults(t) + event := []byte(`data: {"type":"content_block_start","index":1,"content_block":{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":` + claudeWebSearchTestResults + `}}`) + RecordClaudeWebSearchResults("session-1", event, 30*time.Minute, 16) + + citedBody := []byte(`{"model":"claude-opus-5-5","messages":[` + + `{"role":"user","content":[{"type":"text","text":"search"}]},` + + `{"role":"assistant","content":[` + + `{"type":"server_tool_use","id":"srvtoolu_1","name":"web_search","input":{"query":"q"}},` + + `{"type":"web_search_tool_result","tool_use_id":"srvtoolu_1","content":[]},` + + `{"type":"text","text":"answer","citations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"answer","encrypted_index":"IDX_A"}]}]},` + + `{"role":"user","content":[{"type":"text","text":"how many results?"}]}]}`) + + restored := RestoreClaudeWebSearchResults(citedBody, "session-1", 30*time.Minute) + if got := ojson.GetBytes(restored, "messages.1.content.1.content").Raw; got != claudeWebSearchTestResults { + t.Fatalf("restored content = %s, want %s", got, claudeWebSearchTestResults) + } + if got := ojson.GetBytes(restored, "messages.1.content.2.citations.0.encrypted_index").String(); got != "IDX_A" { + t.Fatalf("restored turn citation encrypted_index = %q, want IDX_A: %s", got, restored) + } + + for name, sessionKey := range map[string]string{ + "cache miss": "session-2", + "empty scope": "", + } { + t.Run(name, func(t *testing.T) { + missed := RestoreClaudeWebSearchResults(citedBody, sessionKey, 30*time.Minute) + if got := ojson.GetBytes(missed, "messages.1.content.2.text").String(); got != "answer" { + t.Fatalf("text = %q, want answer", got) + } + if citations := ojson.GetBytes(missed, "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("expected citations to be dropped on %s, got: %s", name, citations.Raw) + } + }) + } +} diff --git a/internal/translator/claude/openai/responses/claude_openai-responses_request.go b/internal/translator/claude/openai/responses/claude_openai-responses_request.go index 9f95763095..22c94b7329 100644 --- a/internal/translator/claude/openai/responses/claude_openai-responses_request.go +++ b/internal/translator/claude/openai/responses/claude_openai-responses_request.go @@ -35,20 +35,20 @@ const ( // - max_output_tokens -> max_tokens // - stream passthrough via parameter func ConvertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte, stream bool) ([]byte, error) { - return convertOpenAIResponsesRequestToClaude(modelName, inputRawJSON, stream, false, ClientToolSearchEnabled()) + return convertOpenAIResponsesRequestToClaude(context.Background(), modelName, inputRawJSON, stream, false, ClientToolSearchEnabled()) } // ConvertOpenAIResponsesRequestToClaudeWithCompat preserves reasoning items // whose encrypted content is empty for configured compatibility endpoints. func ConvertOpenAIResponsesRequestToClaudeWithCompat(modelName string, inputRawJSON []byte, stream bool) ([]byte, error) { - return convertOpenAIResponsesRequestToClaude(modelName, inputRawJSON, stream, true, ClientToolSearchEnabled()) + return convertOpenAIResponsesRequestToClaude(context.Background(), modelName, inputRawJSON, stream, true, ClientToolSearchEnabled()) } // ConvertOpenAIResponsesRequestEnvelopeToClaude translates with the // upstream.claude.tool-search value pinned in ctx, if any. func ConvertOpenAIResponsesRequestEnvelopeToClaude(ctx context.Context, req sdktranslator.RequestEnvelope) sdktranslator.RequestEnvelope { - req.Body, req.Err = convertOpenAIResponsesRequestToClaude(req.Model, req.Body, req.Stream, false, ClientToolSearchEnabledFor(ctx)) + req.Body, req.Err = convertOpenAIResponsesRequestToClaude(ctx, req.Model, req.Body, req.Stream, false, ClientToolSearchEnabledFor(ctx)) return req } @@ -56,12 +56,12 @@ func ConvertOpenAIResponsesRequestEnvelopeToClaude(ctx context.Context, req sdkt // ConvertOpenAIResponsesRequestToClaudeWithCompat with the // upstream.claude.tool-search value pinned in ctx, if any. func ConvertOpenAIResponsesRequestToClaudeWithCompatContext(ctx context.Context, modelName string, inputRawJSON []byte, stream bool) ([]byte, error) { - return convertOpenAIResponsesRequestToClaude(modelName, inputRawJSON, stream, true, ClientToolSearchEnabledFor(ctx)) + return convertOpenAIResponsesRequestToClaude(ctx, modelName, inputRawJSON, stream, true, ClientToolSearchEnabledFor(ctx)) } // toolSearch carries upstream.claude.tool-search for the whole translation so // a config reload cannot mix the two behaviors within one request. -func convertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte, stream, preserveEmptyThinkingBlocks, toolSearch bool) ([]byte, error) { +func convertOpenAIResponsesRequestToClaude(ctx context.Context, modelName string, inputRawJSON []byte, stream, preserveEmptyThinkingBlocks, toolSearch bool) ([]byte, error) { rawJSON := normalizeCodexAgentMessages(inputRawJSON) var drops common.UserTurnDrops @@ -232,6 +232,7 @@ func convertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte var pendingRole string var pendingParts [][]byte var pendingToolUseParts [][]byte + var turnHasEmptyWebSearchResult bool appendMessage := func(msg []byte) { messageBlocks = append(messageBlocks, msg) } @@ -250,6 +251,13 @@ func convertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte combined = append(combined, pendingToolUseParts...) parts = combined } + if pendingRole == "assistant" && (turnHasEmptyWebSearchResult || claudeBlocksHaveEmptyWebSearchResult(parts)) { + var droppedCitations int + parts, droppedCitations = stripClaudeWebSearchCitationsFromParts(parts) + if droppedCitations > 0 { + log.Debugf("responses->claude: dropped %d web_search_result_location citation(s) from assistant turn without search results", droppedCitations) + } + } if len(parts) > 0 { msgDoc := ojson.NewDoc([]byte(`{"role":"","content":[]}`)) _ = msgDoc.Set("role", pendingRole) @@ -503,12 +511,18 @@ func convertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte partsJSON[lastIdx] = common.AttachCacheControl(partsJSON[lastIdx], item) } appendParts(role, partsJSON...) + if role == "user" { + turnHasEmptyWebSearchResult = false + } } case "web_search_call": // Rebuild the Claude server-side search pair so the replayed turn // still shows the search and its hits. - if blocks := webSearchHistory.claudeBlocks(itemIndex, item); len(blocks) > 0 { + if blocks := webSearchHistory.claudeBlocksWithContext(ctx, itemIndex, item); len(blocks) > 0 { + if claudeBlocksHaveEmptyWebSearchResult(blocks) { + turnHasEmptyWebSearchResult = true + } appendParts("assistant", blocks...) } @@ -575,6 +589,7 @@ func convertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte // same Claude id are not mistaken for the same call. if _, paired := emittedRawToolUses[rawID]; rawID == "" || !paired { appendParts("user", convertResponsesStandaloneToolOutputToClaudeText(output)...) + turnHasEmptyWebSearchResult = false continue } callID := util.SanitizeClaudeToolID(rawID) @@ -619,6 +634,7 @@ func convertOpenAIResponsesRequestToClaude(modelName string, inputRawJSON []byte text := []byte(`{"type":"text","text":""}`) text, _ = ojson.SetBytes(text, "text", responsesToolSearchOutputText(item, names)) appendParts("user", text) + turnHasEmptyWebSearchResult = false continue } toolResultDoc := ojson.NewDoc([]byte(`{"type":"tool_result","tool_use_id":"","content":""}`)) diff --git a/internal/translator/claude/openai/responses/claude_openai-responses_server_tool_test.go b/internal/translator/claude/openai/responses/claude_openai-responses_server_tool_test.go index 42fc5b574f..c5ff50c6a9 100644 --- a/internal/translator/claude/openai/responses/claude_openai-responses_server_tool_test.go +++ b/internal/translator/claude/openai/responses/claude_openai-responses_server_tool_test.go @@ -535,4 +535,119 @@ func TestTextSearchTextOrderPreservedInStreamingAndReplay(t *testing.T) { if strings.Join(replayedBlocks, ",") != strings.Join(wantBlocks, ",") { t.Fatalf("replayed block types = %v, want %v", replayedBlocks, wantBlocks) } + if got := ojson.GetBytesNoCopy(replayed, "messages.0.content.3.citations.0.encrypted_index").String(); got != "IDX_ORD" { + t.Fatalf("replayed citation encrypted_index with results = %q, want IDX_ORD", got) + } +} + +func TestWebSearchCallWithoutResultsDropsAssistantCitations(t *testing.T) { + sig := mustTestSignature(t) + t.Run("codex replay without results drops citations and keeps text", func(t *testing.T) { + raw := responsesRequestFromItems( + `{"type":"message","role":"user","content":[{"type":"input_text","text":"earlier context"}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"search question"}]}`, + `{"type":"reasoning","id":"rs_1","summary":[{"type":"summary_text","text":"thinking 1"}],"encrypted_content":"`+sig+`"}`, + `{"type":"web_search_call","id":"ws_srvtoolu_1","status":"completed","action":{"type":"search","query":"q"}}`, + `{"type":"reasoning","id":"rs_2","summary":[{"type":"summary_text","text":"thinking 2"}],"encrypted_content":"`+sig+`"}`, + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Cited answer.","annotations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"Cited answer.","encrypted_index":"IDX_A"}]}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"Reply with OK"}]}`, + ) + out, errConvert := ConvertOpenAIResponsesRequestToClaude("claude-opus-5-5", raw, false) + if errConvert != nil { + t.Fatalf("ConvertOpenAIResponsesRequestToClaude() error = %v", errConvert) + } + assistant := ojson.GetBytesNoCopy(out, "messages.1.content") + if got := assistant.Get("2.type").String(); got != "web_search_tool_result" { + t.Fatalf("messages.1.content.2.type = %q, want web_search_tool_result: %s", got, assistant.Raw) + } + if got := assistant.Get("2.content.#").Int(); got != 0 { + t.Fatalf("messages.1.content.2.content.# = %d, want 0", got) + } + textBlock := assistant.Get("4") + if got := textBlock.Get("type").String(); got != "text" { + t.Fatalf("messages.1.content.4.type = %q, want text: %s", got, assistant.Raw) + } + if got := textBlock.Get("text").String(); got != "Cited answer." { + t.Fatalf("messages.1.content.4.text = %q, want %q", got, "Cited answer.") + } + if citations := textBlock.Get("citations"); citations.Exists() { + t.Fatalf("expected citations to be dropped when web_search_tool_result has no results, got: %s", citations.Raw) + } + }) + + t.Run("results without encrypted_content also drop citations", func(t *testing.T) { + raw := responsesRequestFromItems( + `{"type":"message","role":"user","content":[{"type":"input_text","text":"question"}]}`, + `{"type":"web_search_call","id":"ws_srvtoolu_1","status":"completed","action":{"type":"search","query":"q"},"results":[{"title":"T","url":"https://example.com/a"}]}`, + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Cited answer.","annotations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"Cited answer.","encrypted_index":"IDX_A"}]}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"follow up"}]}`, + ) + out, _ := ConvertOpenAIResponsesRequestToClaude("claude-opus-5-5", raw, false) + textBlock := ojson.GetBytesNoCopy(out, "messages.1.content.2") + if got := textBlock.Get("text").String(); got != "Cited answer." { + t.Fatalf("messages.1.content.2.text = %q, want %q", got, "Cited answer.") + } + if citations := textBlock.Get("citations"); citations.Exists() { + t.Fatalf("expected citations to be dropped when results degrade to empty, got: %s", citations.Raw) + } + }) + + t.Run("interleaved client tool call within same assistant turn drops unbacked citations", func(t *testing.T) { + raw := responsesRequestFromItems( + `{"type":"message","role":"user","content":[{"type":"input_text","text":"question"}]}`, + `{"type":"web_search_call","id":"ws_srvtoolu_1","status":"completed","action":{"type":"search","query":"q"}}`, + `{"type":"function_call","call_id":"call_1","name":"exec","arguments":"{}"}`, + `{"type":"function_call_output","call_id":"call_1","output":"ok"}`, + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"After tool.","annotations":[{"type":"web_search_result_location","url":"https://example.com/a","title":"A","cited_text":"After tool.","encrypted_index":"IDX_A"}]}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"follow up"}]}`, + ) + out, _ := ConvertOpenAIResponsesRequestToClaude("claude-opus-5-5", raw, false) + msg3Content := ojson.GetBytesNoCopy(out, "messages.3.content") + text := msg3Content.String() + if msg3Content.IsArray() { + text = msg3Content.Get("0.text").String() + } + if text != "After tool." { + t.Fatalf("messages.3 text = %q, want %q: %s", text, "After tool.", ojson.GetBytesNoCopy(out, "messages.3").Raw) + } + if citations := msg3Content.Get("0.citations"); citations.Exists() { + t.Fatalf("expected citations after interleaved tool_result to be dropped, got: %s", citations.Raw) + } + }) + + t.Run("later turn with results keeps its citations after earlier turn dropped theirs", func(t *testing.T) { + raw := responsesRequestFromItems( + `{"type":"message","role":"user","content":[{"type":"input_text","text":"q1"}]}`, + `{"type":"web_search_call","id":"ws_srvtoolu_1","status":"completed","action":{"type":"search","query":"q1"}}`, + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Turn 1 answer.","annotations":[{"type":"web_search_result_location","url":"https://example.com/1","title":"1","cited_text":"Turn 1 answer.","encrypted_index":"IDX_1"}]}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"q2"}]}`, + `{"type":"web_search_call","id":"ws_srvtoolu_2","status":"completed","action":{"type":"search","query":"q2"},"results":[{"title":"2","url":"https://example.com/2","encrypted_content":"ENC_2"}]}`, + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Turn 2 answer.","annotations":[{"type":"web_search_result_location","url":"https://example.com/2","title":"2","cited_text":"Turn 2 answer.","encrypted_index":"IDX_2"}]}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"q3"}]}`, + ) + out, _ := ConvertOpenAIResponsesRequestToClaude("claude-opus-5-5", raw, false) + if citations := ojson.GetBytesNoCopy(out, "messages.1.content.2.citations"); citations.Exists() { + t.Fatalf("turn 1 citations must be dropped, got: %s", citations.Raw) + } + if got := ojson.GetBytesNoCopy(out, "messages.3.content.2.citations.0.encrypted_index").String(); got != "IDX_2" { + t.Fatalf("turn 2 citation encrypted_index = %q, want IDX_2: %s", got, ojson.GetBytesNoCopy(out, "messages.3").Raw) + } + }) + + t.Run("multiple searches in one turn drop citations when any search lacks results", func(t *testing.T) { + raw := responsesRequestFromItems( + `{"type":"message","role":"user","content":[{"type":"input_text","text":"q"}]}`, + `{"type":"web_search_call","id":"ws_srvtoolu_1","status":"completed","action":{"type":"search","query":"q1"},"results":[{"title":"1","url":"https://example.com/1","encrypted_content":"ENC_1"}]}`, + `{"type":"web_search_call","id":"ws_srvtoolu_2","status":"completed","action":{"type":"search","query":"q2"}}`, + `{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Combined answer.","annotations":[{"type":"web_search_result_location","url":"https://example.com/2","title":"2","cited_text":"Combined answer.","encrypted_index":"IDX_2"}]}]}`, + `{"type":"message","role":"user","content":[{"type":"input_text","text":"next"}]}`, + ) + out, _ := ConvertOpenAIResponsesRequestToClaude("claude-opus-5-5", raw, false) + if citations := ojson.GetBytesNoCopy(out, "messages.1.content.4.citations"); citations.Exists() { + t.Fatalf("expected citations to be dropped when one search in turn has no results, got: %s", citations.Raw) + } + if got := ojson.GetBytesNoCopy(out, "messages.1.content.4.text").String(); got != "Combined answer." { + t.Fatalf("expected text to be preserved, got %q", got) + } + }) } diff --git a/internal/translator/claude/openai/responses/claude_openai-responses_web_search.go b/internal/translator/claude/openai/responses/claude_openai-responses_web_search.go index b4d1a4e931..b469655315 100644 --- a/internal/translator/claude/openai/responses/claude_openai-responses_web_search.go +++ b/internal/translator/claude/openai/responses/claude_openai-responses_web_search.go @@ -1,11 +1,13 @@ package responses import ( + "context" "regexp" "strings" "github.com/MoeForge/moego/ojson" translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common" + "github.com/router-for-me/CLIProxyAPI/v8/internal/util" ) // Claude reports server-side web search as a pair of assistant content blocks: a @@ -111,6 +113,36 @@ func buildResponsesWebSearchCallItemWithStatus(claudeToolUseID, query, status st return itemDoc.Bytes() } +type webSearchResultLookupContextKey struct{} + +// WithWebSearchResultLookup attaches a request-scoped lookup that restores +// cached Claude web_search_tool_result content for a given server_tool_use id +// when a Responses web_search_call item arrives without results. +func WithWebSearchResultLookup(ctx context.Context, lookup func(toolUseID string) ([]byte, bool)) context.Context { + if lookup == nil { + return ctx + } + if ctx == nil { + ctx = context.Background() + } + return context.WithValue(ctx, webSearchResultLookupContextKey{}, lookup) +} + +func lookupWebSearchResultFor(ctx context.Context, toolUseID string) []byte { + if ctx == nil || toolUseID == "" { + return nil + } + lookup, ok := ctx.Value(webSearchResultLookupContextKey{}).(func(string) ([]byte, bool)) + if !ok || lookup == nil { + return nil + } + content, found := lookup(toolUseID) + if !found || len(content) == 0 { + return nil + } + return content +} + // convertResponsesWebSearchCallToClaudeBlocks is the inverse of // buildResponsesWebSearchCallItem: it rebuilds the Claude block pair so a // replayed turn still shows that the search happened and what it returned. @@ -122,6 +154,10 @@ func convertResponsesWebSearchCallToClaudeBlocks(item ojson.Result) [][]byte { // block, using fallbackQuery when the item carries none, and the result block // only when withResult is set. func convertResponsesWebSearchCallToClaudeBlocksWithQuery(item ojson.Result, fallbackQuery string, withResult bool) [][]byte { + return convertResponsesWebSearchCallToClaudeBlocksWithContext(context.Background(), item, fallbackQuery, withResult) +} + +func convertResponsesWebSearchCallToClaudeBlocksWithContext(ctx context.Context, item ojson.Result, fallbackQuery string, withResult bool) [][]byte { toolUseID := claudeWebSearchToolUseID(strings.TrimSpace(item.Get("id").String())) if toolUseID == "" { return nil @@ -143,7 +179,11 @@ func convertResponsesWebSearchCallToClaudeBlocksWithQuery(item ojson.Result, fal resultDoc := ojson.NewDocString(`{"type":"web_search_tool_result","tool_use_id":"","content":[]}`) _ = resultDoc.Set("tool_use_id", toolUseID) - if content := responsesWebSearchResultsToClaude(item.Get("results")); len(content) > 0 { + content := responsesWebSearchResultsToClaude(item.Get("results")) + if len(content) == 0 { + content = lookupWebSearchResultFor(ctx, toolUseID) + } + if len(content) > 0 { _ = resultDoc.SetRawBytes("content", content) } return [][]byte{useDoc.Bytes(), resultDoc.Bytes()} @@ -226,15 +266,19 @@ func scanResponsesWebSearchHistory(items []ojson.Result) responsesWebSearchHisto } func (h responsesWebSearchHistory) claudeBlocks(index int, item ojson.Result) [][]byte { + return h.claudeBlocksWithContext(context.Background(), index, item) +} + +func (h responsesWebSearchHistory) claudeBlocksWithContext(ctx context.Context, index int, item ojson.Result) [][]byte { if h.skip[index] { return nil } query := h.pausedQuery[index] tail := index >= len(h.boundaryAfter) || !h.boundaryAfter[index] if responsesWebSearchItemIsPaused(item) && tail { - return convertResponsesWebSearchCallToClaudeBlocksWithQuery(item, query, false) + return convertResponsesWebSearchCallToClaudeBlocksWithContext(ctx, item, query, false) } - return convertResponsesWebSearchCallToClaudeBlocksWithQuery(item, query, true) + return convertResponsesWebSearchCallToClaudeBlocksWithContext(ctx, item, query, true) } // responsesWebSearchCallQuery reads the query from a Responses web_search_call, @@ -305,3 +349,68 @@ func attachClaudeCitations(textBlock []byte, annotations ojson.Result) []byte { } return updated } + +func claudeBlocksHaveEmptyWebSearchResult(blocks [][]byte) bool { + for _, raw := range blocks { + block := util.ParseGJSONBytesNoCopy(raw) + if block.Get("type").String() != "web_search_tool_result" { + continue + } + content := block.Get("content") + if !content.Exists() || content.Type == ojson.Null || (content.IsArray() && len(content.Array()) == 0) { + return true + } + } + return false +} + +// stripClaudeWebSearchCitationsFromParts removes web_search_result_location +// citations from text blocks when a web_search_tool_result in the same +// assistant turn ended up without content. Anthropic intermittently rejects +// citations whose encrypted_index has no matching search result in the turn. +func stripClaudeWebSearchCitationsFromParts(parts [][]byte) ([][]byte, int) { + droppedTotal := 0 + for i, raw := range parts { + part := util.ParseGJSONBytesNoCopy(raw) + if part.Get("type").String() != "text" { + continue + } + updated, dropped := stripClaudeWebSearchCitationsFromTextBlock(raw, part.Get("citations")) + if dropped > 0 { + parts[i] = updated + droppedTotal += dropped + } + } + return parts, droppedTotal +} + +func stripClaudeWebSearchCitationsFromTextBlock(textBlock []byte, citations ojson.Result) ([]byte, int) { + if !citations.Exists() || !citations.IsArray() { + return textBlock, 0 + } + var kept [][]byte + dropped := 0 + citations.ForEach(func(_, citation ojson.Result) bool { + if citation.Get("type").String() == "web_search_result_location" { + dropped++ + return true + } + kept = append(kept, []byte(citation.Raw)) + return true + }) + if dropped == 0 { + return textBlock, 0 + } + if len(kept) == 0 { + updated, errDelete := ojson.DeleteBytes(textBlock, "citations") + if errDelete != nil { + return textBlock, 0 + } + return updated, dropped + } + updated, errSet := ojson.SetRawBytes(textBlock, "citations", translatorcommon.JoinRawArray(kept)) + if errSet != nil { + return textBlock, 0 + } + return updated, dropped +}