Commit Graph

160 Commits

Author SHA1 Message Date
Luis Pater
fecebcca59 refactor(translator): overhaul function call handling in Codex response conversion
- Replaced `pendingCodexFunctionCall` with `codexFunctionCallStream` for enhanced function call tracking.
- Introduced `DeferredStreamEvents` to handle deferred event processing.
- Simplified and standardized codex function call state management with consolidated methods.
- Enhanced reasoning and thinking block handling to ensure proper closure and new block initiation.
- Removed redundant methods, improving maintainability.

Closes: #4655
2026-07-29 23:33:11 +08:00
Luis Pater
181242b1c0 feat(translator): enhance tool output parsing with support for input_image types
- Added handling for `input_image` in `toolOutputContentPart` and updated related parsing logic.
- Introduced `hasToolOutputImagePart` to check content for image-related types.
- Improved `setToolCallOutputContent` to handle structured content with image parts recursively.
- Refined detail extraction and type mapping for `input_image` and `image_url` cases.

Closes: #4458
2026-07-27 17:54:50 +08:00
Luis Pater
6491ce3991 refactor(translator): improve tool name handling and resolve conflicts
- Enhanced handling of unique and ambiguous tool names for `custom` and `function` types.
- Consolidated name validation and shortening logic for clarity.
- Refactored `resolveToolCall` to standardize tool call processing.
- Improved support for tool name disambiguation and metadata mapping.

Closes: #4208
2026-07-27 02:33:38 +08:00
Luis Pater
58ede93e33 refactor(translator): enhance tool call handling for streaming responses
- Introduced `toolCallStreamState` to manage tool call state transitions.
- Improved support for `custom_tool_call` types with fallback handling.
- Refactored argument emission logic for better clarity and flexibility.
- Added utility methods for managing and retrieving tool call states.

Closes: #4078
2026-07-26 23:51:19 +08:00
sususu98
94cf4674d7 fix(translator/codex): keep single thinking block per reasoning item in streaming translator (#4581)
Keep a single Claude thinking block open across multiple reasoning summary
parts for one Codex reasoning item, and finalize it only when output_item.done
delivers the item's final encrypted_content.

Previously, each summary part closed the preceding thinking block with the
pre-content encrypted_content snapshot captured at output_item.added. This
emitted N thinking blocks with placeholder signatures for one reasoning item,
causing client replay to append redundant placeholder reasoning items into
conversation history.

Add regression tests verifying that multi-part summary reasoning items produce
exactly one thinking block signed with the final encrypted_content.
2026-07-26 12:21:37 +08:00
Luis Pater
64291120e7 perf(translator): add unit tests for payload reuse and normalization across translators
- Added tests to ensure no-copy payload reuse for various translators (Codex, Gemini, OpenAI, Claude, etc.).
- Covered scenarios for field normalization, tool parameter cleaning, and thought signature sanitization.
- Included a benchmark for SanitizeGeminiRequestThoughtSignatures with large normalized payloads.
2026-07-20 03:50:35 +08:00
Luis Pater
0f52284e6c perf(translator): add normalization and reuse logic for Codex request payloads
- Introduced field normalization functions (`setCodexRequiredBool`, `setCodexRequiredInclude`) to ensure correct Codex input structure.
- Added tests for normalized payload reuse and field validation in `ConvertOpenAIResponsesRequestToCodex`.
- Implemented `deleteCodexRequestFields` utility to remove unsupported fields dynamically.
- Added benchmarks for normalized payload processing to measure performance across different payload sizes.

Closes: #4362
2026-07-20 02:23:15 +08:00
Luis Pater
7c61e982e4 perf(translator): optimize array allocation logic and replace JoinRawArray with SetRawArrayItems methods 2026-07-19 15:01:37 +08:00
Luis Pater
3c2010bcf3 test(translator): add benchmarks for request translation performance across large histories 2026-07-19 06:02:38 +08:00
Luis Pater
9f1e890419 Merge pull request #4366 from Johnnybyzhang/fix/claude-codex-allocation-fix
perf(translator): 降低 Claude → Codex 请求转换的内存分配
2026-07-19 04:29:52 +08:00
Luis Pater
da087d7fad feat(translator): prioritize service tier for fast responses in Claude requests
- Updated `codex_claude_request` to set the service tier as "priority" when `speed` is "fast".
- Enhanced request template generation logic to reflect dynamic service tier evaluation.

Closes: #4349
2026-07-18 00:54:50 +08:00
Johnnybyzhang
e1e2357a0b perf(translator): avoid redundant gjson array parsing 2026-07-16 16:00:53 +08:00
Johnnybyzhang
b3ffb11666 fix(translator): reduce Claude to Codex allocations 2026-07-16 15:43:29 +08:00
Luis Pater
09da52ad50 feat(translator): improve error handling and response conversion for incomplete statuses
- Enhanced response handling to support `response.incomplete` events across translator components and executors.
- Refactored `resultErrorFromError` to centralize error conversion and consolidate repeated logic for constructing custom errors.
- Updated handling of `max_output_tokens` and similar terminal reasons in OpenAI and Gemini translator workflows.
- Introduced `IsRequestScoped` and `IsRequestScopedError` to differentiate between request-scoped and non-request-scoped errors.
- Added comprehensive test cases for incomplete responses, terminal failures, and error propagation in both streaming and non-streaming conditions.

Closes: #3055
2026-07-16 04:38:19 +08:00
Luis Pater
9f62c8df28 feat(translator): enhance handling of custom tool calls and improve tool call batching logic
- Introduced support for custom tool calls in request serialization, including preservation of call IDs, input, and output.
- Enhanced tool call batching logic to handle ambiguous, missing, and reused call IDs across assistant messages.
- Updated processing of tool call outputs to ensure proper matching with pending calls and introduced synthesized IDs when necessary.
- Added extensive test cases to validate behavior for custom tool call histories, mixed call types, and edge cases with ambiguous or orphaned IDs.

Closes: #4256
2026-07-15 03:14:05 +08:00
sususu
041816c2fd feat(xai): encrypted reasoning replay for Responses/Claude with hardened cache
Preserve include=reasoning.encrypted_content, cache reasoning+assistant message
for store:false multi-turn, and accept Grok thinking signatures on Claude→Codex
for grok targets.

Harden replay cache: blob-level reasoning filter, never double-inject assistant
when history has a last message, isolate session keys by downstream API key,
clear cache on non-replayable completed turns, and normalize refusal parts.
2026-07-12 22:31:19 +08:00
Luis Pater
abb5224880 feat(translator): add support for cache_write_tokens in Codex response usage details
- Introduced handling for `cache_write_tokens` in `input_tokens_details` and mapped to `cached_creation_tokens` in output.
- Added comprehensive tests to validate correct parsing, preservation, and omission of `cache_write_tokens` in both streaming and non-streaming responses.

Closes: #4197
2026-07-10 23:03:25 +08:00
Luis Pater
cdccc72dfe fix(translator): resolve pending codex tool calls on terminal response 2026-07-08 05:25:34 +08:00
Luis Pater
8b9c4da245 feat(interactions): add support for Google Interactions
- Introduced API handlers and executor logic for Google Interactions
- Added request and response transformations for OpenAI and Claude Interactions.
- Integrated Gemini API with Interactions support.
- Updated tests to validate Interactions request parsing and error handling.
- Refactored translator logic for Interactions data flows.
2026-07-07 02:39:13 +08:00
Luis Pater
f1ed8912bb feat(translator): wrap message-level system roles as user-visible reminders
- Added `ClaudeMessageSystemReminderText` to convert system content into user-readable reminder text.
- Updated translators (Antigravity, Gemini, Codex, OpenAI) to wrap `system` roles as reminders with `<system-reminder>` tags.
- Adjusted related test cases to validate reminder wrapping logic.
2026-06-23 13:43:14 +08:00
Luis Pater
57e1bf97a5 feat(translator): ensure preservation of tool and call IDs in Gemini request and response translations
- Updated `ConvertGeminiRequestToClaude`, `ConvertGeminiRequestToCodex`, and their respective response counterparts to include logic for retaining and using tool/call IDs when present from gateway-provided inputs.
- Enhanced pairing logic between function calls and responses to handle custom and auto-generated IDs consistently.
- Introduced tests validating ID preservation and proper behavior in both streaming and non-streaming flows.

Closes: #3878
2026-06-20 14:12:14 +08:00
Luis Pater
379167c992 feat(translator): add benchmarking for convertSystemRoleToDeveloper with large inputs
- Introduced `BenchmarkConvertSystemRoleToDeveloperLargeInput` to evaluate performance on various input sizes and configurations.
- Improved `convertSystemRoleToDeveloper` logic to optimize JSON rebuilding and reduce unnecessary operations.
- Created helper `makeLargeResponsesInputForBenchmark` to generate test data for performance scenarios.

Closes: #3751
2026-06-20 11:10:53 +08:00
Luis Pater
34639c3cf9 feat(translator): defer Codex function call starts until function name is available
- Updated `ConvertCodexResponseToClaude` to delay emitting `function_call` start events until the `name` field is resolved.
- Introduced `pendingCodexFunctionCall` for buffering incomplete function calls.
- Added tests to ensure proper behavior for deferred starts, including argument buffering and finalization.

Closes: #3471
2026-06-20 00:11:12 +08:00
Luis Pater
51aa5ba925 feat(translator): preserve input_audio fields in OpenAI request conversions
- Updated `ConvertOpenAIRequestToGemini` and `ConvertOpenAIRequestToCodex` to handle `input_audio`, retaining `data` and `format` fields.
- Added helper `openAIInputAudioMimeType` for determining MIME types from audio formats.
- Introduced unit tests to validate correct preservation of `input_audio` data and format.

Closes: #3447
2026-06-20 00:03:54 +08:00
Luis Pater
041a065b2f Merge branch 'remove-gemini-cli' into dev
# Conflicts:
#	internal/api/handlers/management/auth_files.go
#	internal/thinking/provider/geminicli/apply.go
2026-06-19 14:40:29 +08:00
Luis Pater
893412e965 feat(translator): normalize service_tier in Codex requests and add tests
- Added `normalizeCodexServiceTier` to standardize `service_tier` values (`fast` to `priority`, omit unsupported tiers).
- Updated `ConvertClaudeRequestToCodex` to apply normalization logic.
- Introduced `TestConvertClaudeRequestToCodex_ServiceTier` to validate behavior across various `service_tier` inputs.

Closes: #3276 #3294
2026-06-19 03:58:55 +08:00
Luis Pater
78ba8ba731 chore: remove Gemini CLI-related translator packages and logic
- Deleted `geminicli` provider and related `Apply` logic.
- Removed all translator packages specific to Gemini CLI (Claude, Codex integrations).
- Purged associated test files for Gemini CLI translation.
- Removed `GeminiAuthenticator` and all associated authentication logic (OAuth flows, token handling, refresh logic).
- Deleted internal/executor Gemini OAuth support, including bearer token handling and runtime API logic.
- Purged all tests, configs, and command-line flags specific to Gemini OAuth flows.
- Updated documentation and aliases to reflect Gemini removal.
- Renamed `parseRetryDelay` to `ParseRetryDelay` and `deleteJSONField` to `DeleteJSONField`.
- Updated references in `antigravity_executor` and tests to use the new `helps` package.
- Adjusted import paths and test cases to ensure compatibility with the new location.
- Updated README files to reflect changes in the retry logic references.
- Updated `.github/ISSUE_TEMPLATE/bug_report.md` to remove deprecated Gemini CLI mention.
2026-06-18 13:33:10 +08:00
Luis Pater
a5cb88323d feat(translator): enhance content block handling and add stream-specific test
- Refactored content block start/stop logic into `startCodexTextBlock` and `stopCodexTextBlock` for better readability and reusability.
- Updated logic to ensure proper handling of "output_text" block events to avoid ghost stop emissions.
- Added `TestConvertCodexResponseToClaude_StreamTextBeforeToolCallsDoesNotEmitGhostStop` to validate content block start/stop behavior in streamed responses.
2026-06-17 00:41:08 +08:00
sususu98
30dc2e7f34 fix(translator): emit Claude server tool blocks for Codex web_search_call streams (#3868)
* fix(translator): emit Claude server tool blocks for Codex web_search_call streams

Map Codex Responses streaming web_search_call events to Claude SSE
server_tool_use and web_search_tool_result blocks, with deduplication
and a focused stream regression test.

* fix(translator): stabilize Codex web_search fallback tool_use IDs

Reuse the active fallback web_search tool_use ID across later stream
events so tool_result blocks stay paired when upstream omits item IDs.
This is defensive hardening; live Codex streams already provide ws_* IDs.

* fix(translator): emit Codex web_search blocks from populated items

Wait for output_item.done before emitting Claude web_search tool_use
and tool_result blocks, and avoid deduping early added/completed events
that arrive before action.query is available. Matches live Responses
stream ordering seen in local tmux verification.

* fix(translator): map Codex web_search_call items in non-stream Claude responses

Emit server_tool_use and web_search_tool_result blocks from completed
response.output web_search_call items, matching the streaming translator.

* fix(translator): keep non-stream web_search on end_turn and dedupe output items

Do not treat server web_search_call items as client tool_use for stop_reason.
Skip duplicate or query-less open_page web_search output items in non-stream
translation, matching spark live behavior.
2026-06-16 23:07:08 +08:00
sususu98
702295d73a fix: translate codex stream errors for claude 2026-06-08 21:07:21 +08:00
Luis Pater
365415c87a fix(translator): add fallback for system_instruction key in Gemini request parsing
- Added fallback to handle cases where `system_instruction.parts` is absent by checking for `systemInstruction.parts`.
2026-06-08 08:10:41 +08:00
Luis Pater
0e3c809ceb fix(codex): handle non-empty reasoning and content items, add test for trailing empty messages
Closes: #3683
2026-06-03 06:28:51 +08:00
sususu98
aee7a5fbc5 feat: intercept incompatible signature replay 2026-05-29 15:22:57 +08:00
Luis Pater
33f4904b25 fix(translator): handle system role as developer in Claude request conversion
- Updated `ConvertClaudeRequestToGemini` logic to treat `system` role as `developer`.
- Added unit test case to validate the behavior.

Closes: #3510
2026-05-22 12:04:27 +08:00
sususu98
8bc2eff58a fix: shorten claude codex tool call ids 2026-05-18 17:49:42 +08:00
Mad Wiki
d606faa99c fix: strip Claude Code attribution from non-Anthropic translations 2026-05-17 04:21:53 +08:00
Luis Pater
e50cabac4b chore: upgrade CLIProxyAPI dependency to v7 across the project
- Updated all references from v6 to v7 for `github.com/router-for-me/CLIProxyAPI`.
- Ensured consistency in imports within core libraries, tests, and integration tests.
- Added missing tests for new features in Redis Protocol integration.
2026-05-08 11:46:46 +08:00
Luis Pater
17be6442a8 fix(translator): improve tool response handling for non-string content
- Added `setToolCallOutputContent` to process various content types, including arrays and fallback cases.
- Implemented robust handling for specific tool output types like text, image URLs, and files, ensuring proper serialization.
- Improved fallback logic to handle unexpected or missing data.

Fixed: #2313
Closes: #2349
2026-05-04 05:50:01 +08:00
Luis Pater
a1487b0958 fix(translator): handle non-string types in tools result processing
- Skip setting values for non-string `type` fields to prevent runtime errors.

Closes: #2226
2026-05-04 05:08:31 +08:00
sususu98
0d107dd566 fix: respect declared claude web search tool names 2026-04-29 19:24:53 +08:00
sususu98
1c0c426b85 fix: align claude codex translation 2026-04-29 18:47:03 +08:00
sususu
c523101439 Preserve Codex reasoning signatures for Claude 2026-04-27 16:51:35 +08:00
Luis Pater
d85e13b044 fix(codex): include content field in reasoning item initialization 2026-04-27 16:41:23 +08:00
Luis Pater
01e16a8509 feat(codex): handle thinking-signature conversion for reasoning content
- Implemented `appendReasoningContent` to support processing of `thinking` signature and text as reasoning input.
- Added test cases to validate reasoning content conversion with and without text.
2026-04-27 16:31:26 +08:00
Luis Pater
86c856f56f feat(translator): add partial and full image generation support in Codex-GPT and Codex-Gemini flows
- Introduced `LastImageHashByItemID` in Codex-GPT and `LastImageHashByID` in Codex-Gemini for deduplication of generated images.
- Added support for handling `partial_image` and `image_generation_call` types, with inline data embedding for Gemini and URL payload conversion for GPT.
- Extended unit tests to verify image handling in both streaming and non-streaming modes.
2026-04-19 03:21:59 +08:00
Luis Pater
fcc59d606d fix(translator): add unit tests to validate output_item.done fallback logic for Gemini and Claude 2026-04-08 03:54:15 +08:00
Luis Pater
b85120873b Merge pull request #2332 from RaviTharuma/fix/claude-thinking-signature
fix: preserve Claude thinking signatures in Codex translator
2026-04-06 09:25:06 +08:00
0oAstro
ec77f4a4f5 fix(codex): set finish_reason to tool_calls in non-streaming response when tool calls are present 2026-03-31 14:12:15 +05:30
Ravi Tharuma
5fc2bd393e fix: retain codex thinking signature until item done 2026-03-28 14:41:25 +01:00
Ravi Tharuma
66eb12294a fix: clear stale thinking signature when no block is open 2026-03-28 14:08:31 +01:00