Commit Graph

187 Commits

Author SHA1 Message Date
Luis Pater
942bda9979 fix(translator): preserve unknown thinking signatures in claude to codex compat translation
- Retain assistant thinking blocks with unknown-format string signatures when compatibility translation is enabled.
- Fall back to using the raw signature when provider detection classifies the signature provider as unknown.

Closes: #5710
2026-09-11 04:13:27 +08:00
sususu
37ce368c50 fix(schema): inspect patternProperties keys and avoid Unicode escape fast-path bypass
- Check for \u in fast-path check to prevent JSON Unicode escapes from bypassing inspection.
- Inspect regex keys under patternProperties and drop keys with unsupported Unicode property escapes.
- Add tests covering Unicode escape representations (\u005c, \u0070, \u0050) and patternProperties keys.
2026-09-09 18:03:39 +08:00
sususu
e56abd56f1 fix(translator): strip unsupported unicode property escape patterns from tool schemas
- Add HasUnsupportedUnicodePropertyEscape in internal/util to detect \p{...} / \P{...} escapes that fail Python re compilation.
- Strip incompatible pattern attributes during tool parameter normalization in codex/claude and openai/claude translators.
- Provide schema-aware fallback stripping in codex executor helps to protect downstream Codex requests without mutating non-schema user data.
- Export unified schema keyword lists in internal/util to eliminate duplication.
- Add comprehensive unit tests covering Artifact fixtures, lookaheads, and user data preservation.

Closes: #5644
2026-09-09 17:49:13 +08:00
Luis Pater
7fac6b15bc fix(codex): strip schema dialect keywords from tool parameters
- Recursively strip `$schema` and `$id` dialect keywords from tool parameter schemas.
- Ensure object schemas and union object types retain an empty `properties` map.
- Re-encode normalized parameters without escaping HTML entities.

Closes: #5612
2026-09-09 01:33:09 +08:00
Luis Pater
e365ab0cc9 fix(claude): map codex reasoning tokens to thinking tokens
- Extract `reasoning_tokens` from Codex response usage across streaming and non-streaming responses.
- Map valid non-negative reasoning tokens to `usage.output_tokens_details.thinking_tokens`.
- Clamp reasoning tokens to `output_tokens` and ignore invalid or negative values.

Closes: #5587
2026-09-08 12:17:37 +08:00
Luis Pater
bee20b9940 fix(codex): sanitize invalid tool name characters for upstream compatibility
- Replace characters outside `[a-zA-Z0-9_-]` with underscores during tool name shortening to meet Codex upstream requirements.
- Collect tool names across `tools` declarations, `tool_choice`, and assistant `tool_calls` history in requests to ensure consistent shortening and reverse restoration.

Closes: #5564
2026-09-08 02:53:30 +08:00
Luis Pater
d01516c120 fix(codex): explicitly default function tool strict to false
- Forward `strict` as `false` when omitted in function tools to prevent upstream Responses API from defaulting it to `true`.

Closes: #5555
2026-09-07 22:52:24 +08:00
Luis Pater
8696585cea fix(codex): preserve service tier and cache write tokens in openai responses
- Propagate upstream `service_tier` across streaming chunks and non-stream response bodies.
- Set both `cache_write_tokens` and `cached_creation_tokens` in prompt token details while preserving integer precision.
- Validate `cache_write_tokens` format and reject non-integer or malformed values.

Closes: #5543
2026-09-07 21:35:09 +08:00
Luis Pater
aa36527752 fix(translator/claude): downgrade strict mode when schema misses required properties
- Recursively inspect JSON schema properties to detect optional fields not listed in the `required` array.
- Downgrade `strict` to false when schemas contain optional properties to prevent backend HTTP 400 rejections.

Closes: #5463
2026-09-04 02:02:19 +08:00
Luis Pater
893abbabc2 feat(translator): support cache write tokens in claude responses
- Extract cache write tokens from OpenAI and Codex usage details.
- Map cache write tokens to Claude `cache_creation_input_tokens` for streaming and non-streaming responses.

Closes: #4262
2026-09-01 19:56:40 +08:00
Luis Pater
17a65ee547 fix(codex): support reasoning_text in openai chat completion responses
- Handle `response.reasoning_text.delta` and `response.reasoning_text.done` stream events for delta reasoning content.
- Extract `reasoning_text` content from output reasoning items in non-streaming responses.

Closes: #5378
2026-09-01 18:12:52 +08:00
Luis Pater
9fdc460585 fix(claude): preserve tool pairing across intervening system messages
- Preserve pending tool use IDs across message-level system reminders to maintain tool call and result alignment.
- Merge adjacent Gemini request contents for consecutive user and system reminder turns.
- Relax tool response reordering condition and preserve non-response parts in Antigravity executor.
- Align tool results and buffer pending system reminders in OpenAI translator.

Closes: #5354
2026-08-31 19:27:20 +08:00
Luis Pater
f8c45c30c5 feat(codex): map claude output_config format to text format
- Map Claude `output_config.format` with `json_schema` type to Codex `text.format`.
- Preserve custom schema name and strict configuration with appropriate defaults.

Closes: #5280
2026-08-27 18:19:04 +08:00
Luis Pater
5b232e3e98 fix(gemini): generate deterministic sequential IDs for Gemini tool call pairing in Codex/Claude conversions
Closes: #5107
2026-08-21 03:21:55 +08:00
Luis Pater
2005788fc3 fix(codex): filter out prompt_cache_retention in OpenAI→Codex responses request conversion
Closes: #5068
2026-08-19 21:29:03 +08:00
Luis Pater
8b3b304952 perf(translator): switch response translators to batched raw-array insertion via SetRawArrayItems 2026-08-16 01:53:27 +08:00
Luis Pater
0c58c3c83d perf(translator): batch assemble translation arrays before writing JSON
- Refactor Gemini/OpenAI/Codex/Claude/Antigravity interaction and response translators to collect `parts`, `content`, `steps`, `tool_calls`, and `output` items in slices, then set each array once via `translatorcommon` helpers.
- Replace repeated incremental `sjson.SetRawBytes(..., ".-1", ...)` mutations with single-shot raw-array updates to avoid repeated rewrites.
- Keep existing step/content semantics while reducing ordering/overwrite risks when merging multi-fragment data (including thought/signature and web-search/function-call blocks).
2026-08-16 01:25:28 +08:00
Luis Pater
124dab6cc1 perf(translator): batch assemble response arrays and merge multi-choice parts
- Refactor multiple OpenAI/Gemini/Codex/Claude/Antigravity/Interactions response translators to collect output elements in slices and set arrays once via `translatorcommon.JoinRawArray`.
- Fix non-stream OpenAI→Gemini part handling so fields (`reasoning`, `content`, `functionCall`) from multiple choices overlay correctly on shared parts instead of being overwritten.
- Preserve explicit empty message fields in Gemini→OpenAI chat-completion conversion (`content`/`reasoning_content`) and add tests covering empty-text and cross-choice overlay cases.
2026-08-15 23:15:50 +08:00
Luis Pater
6edf9c4821 fix(translator): strip nested prompt_cache_breakpoint from Codex Responses payloads
- Remove `prompt_cache_breakpoint` from `input[].content[]` items during OpenAI Responses → Codex request conversion to avoid unsupported-field rejection.
- Keep message contents and existing `system`→`developer` role conversion behavior unchanged while applying the cleanup.

Closes: #4951
2026-08-15 00:37:09 +08:00
Luis Pater
8b54db36ae fix(translator): drop Gemini hidden thought parts during request conversion
Closes: #4914
2026-08-13 01:10:04 +08:00
Luis Pater
2ab25eae96 fix(codex): strip unsupported prompt_cache_options from OpenAI Responses requests
Closes: #4915
2026-08-12 17:18:21 +08:00
Luis Pater
e9c44ae256 fix(codex): fall back to current tool-call state when item IDs don’t match
- Update `findToolCallState` to only return `toolCallStates` entries when a non-nil state exists.
- Preserve `currentToolCall` fallback for unknown `item_id`, `id`, or `output_index` lookups to keep streaming tool-call context intact.

Closes: #4901
2026-08-12 01:44:07 +08:00
jizhenggang
c30e60a11b Reduce Codex request amplification for large payloads
Reuse immutable request bytes during input inspection and avoid a redundant websocket clone while preserving the required outbound envelope.

Constraint: Preserve request immutability and one final WebSocket request-body allocation
Rejected: In-place JSON mutation | request buffers are shared across translation and execution stages
Confidence: high
Scope-risk: narrow
Directive: Do not retain no-copy gjson results or mutate their backing payload while results are in use
Tested: GOTOOLCHAIN=local go test ./...; targeted 8 MiB allocation benchmarks
Not-tested: Live Codex upstream network traffic
2026-08-07 19:16:06 +08:00
Luis Pater
dcee14dd3c feat(compat): preserve compat-mode thinking/signature blocks for API-key models
- Added `is-compat` model metadata plumbing from config through executor and helpers, including hash computation.
- Introduced a compatibility-aware translation path (`TranslateRequestWithAPIKeyModelCompatibility`) and wired it into Claude/Gemini/Codex/Interactions request flows.
- Updated Claude message sanitization/translation behavior to keep empty-thinking compatibility blocks (including signatures) when `is-compat` is enabled, while keeping default behavior unchanged.
2026-08-06 17:19:24 +08:00
Luis Pater
9b8d97441e fix(responses): preserve original request model on response.created/response.in_progress payloads 2026-08-04 18:37:01 +08:00
Luis Pater
44d5e0bebc fix(codex): preserve base64 PDF document blocks when converting Claude requests to Codex
Closes: #4765
2026-08-04 03:07:00 +08:00
sususu
b3046d29b9 feat(thinking): preserve cross-protocol summary visibility 2026-07-30 21:53:30 +08:00
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