510 Commits
Author SHA1 Message Date
Luis Pater 3ad6dfe30e perf(translator): optimize tool handling in OpenAI translator requests
- Moved `parallel_tool_calls` and `tool_choice` logic inside conditional handling for `chatCompletionsTools`.
- Eliminated redundant field updates to streamline payload processing.

Closes: #4491
2026-07-22 03:43:50 +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 e47ffda75b feat(translator): normalize and extract OpenAI file content with MIME type
- Added `NormalizeOpenAIFileData` utility to parse and validate file content, ensuring proper MIME type handling and base64 extraction.
- Refactored translation logic to use normalized file data for Gemini, Antigravity, and Interactions processors.
- Introduced comprehensive unit tests for both valid and invalid file data scenarios across all translators.

Fixed: #4416
2026-07-19 18:29:05 +08:00
Luis Pater 07f8354912 feat(translator): qualify function names with namespace in OpenAI response handling
- Updated `ConvertOpenAIResponsesRequestToOpenAIChatCompletions` to include namespaces when generating function names for tool calls.
- Added test case to validate namespace qualification logic for function call histories.

Fixed: #4423
2026-07-19 17:52:02 +08:00
Luis Pater cd98e9d74d feat(translator): handle empty response content for Claude and adjust JSON structure
- Added conditional logic to append `content_block_start` for empty responses, ensuring correct event flow.
- Updated default `responseJSON` structure to use an empty array (`[]`) for the `content` field instead of `null`.

Fixed: #4431
2026-07-19 17:32:54 +08:00
Luis Pater b7299a3d86 feat(translator): implement output indexing for OpenAI response generation
- Refactored state management to introduce deterministic output indexing for reasoning, messages, and function calls.
- Added new structures `MessageItems` and `ReasoningItems` to aggregate responses with consistent indices.
- Updated OpenAI response generation to include `output_index` for all outputs, ensuring correct order and no gaps.
- Enhanced tests to validate contiguous output indices and response event lifecycle.

Fixed: #4429
2026-07-19 17:15:04 +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 910ab83bb2 Merge pull request #4329 from mazzz1y/perf/executor
perf(translator): build input and messages arrays in a single pass
2026-07-19 04:06:09 +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
Dmitry Rubtsov 3c6c7b736e perf(translator): build claude system and messages arrays in a single pass
- Replaced per-item `sjson.SetRawBytes(out, "system.-1"/"messages.-1", ...)` appends, which reparsed and rewrote the growing output document on every conversation item, causing O(n^2) work on long histories.
- Collected system and message blocks into slices and wrote each array once via `common.JoinRawArray`, moving the cache_control post-processing and system-only fallback turn onto the collected slices.
2026-07-15 18:21:00 +06:00
Dmitry Rubtsov 613255e0d0 perf(translator): build input and messages arrays in a single pass
- Replaced per-item `sjson.SetRawBytes(out, "input.-1"/"messages.-1", ...)` appends, which reparsed and rewrote the growing output document on every conversation item, causing O(n^2) work on long histories.
- Applied the same approach to both interactions translation directions and the OpenAI Responses to Chat Completions path.
2026-07-15 16:34:47 +06:00
Luis Pater c8803713c9 feat(translator): disambiguate and deduplicate tool names in request/response workflows
- Introduced `SanitizedFunctionNameMap` and `DisambiguatedToolNameMap` for consistent mapping between sanitized and original tool names.
- Added collision handling with deterministic hash suffixes for name disambiguation within character limits.
- Implemented logic to deduplicate and map tool and function names across translator components, including Interactions, Claude, and Gemini.
- Updated test cases to validate name restoration, deduplication, disambiguation, and mapping behaviors.

Closes: #4244
2026-07-15 04:49:30 +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
Luis Pater 3d46ede431 feat(translator): handle reasoning content and improve request serialization
- Added support for reasoning content in assistant messages with proper serialization of `thought`, `thoughtSignature`, and content parts.
- Ensured empty assistant messages and invalid function calls are skipped during request generation.
- Enhanced compatibility by adding test cases for preserving reasoning content, tool calls, and visible assistant content in `ConvertOpenAIRequestToGemini` and `ConvertOpenAIRequestToAntigravity`.

Closes: #4304
2026-07-14 22:19:07 +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 f4a8aee695 feat(translator): support namespace and custom tool handling in OpenAI responses
- Enhanced `convertResponsesCustomToolToOpenAIChat` to handle custom tool overrides with qualified names.
- Added namespace child consolidation logic for tools.
- Updated `responsesCustomToolNames` to include namespace-aware tool extraction.
- Introduced new test cases to validate namespace and custom tool transformations in both streaming and non-streaming responses.
2026-07-12 21:36:12 +08:00
Luis Pater dc39f44547 fix(translator): stabilize Responses Lite tool events 2026-07-12 20:39:56 +08:00
辰ing e9d3dfbce1 fix(translator): 工具输出与 custom input 解包的健壮性改进
采纳 review 意见:responsesToolOutputText 支持纯字符串数组元素,
避免非 content-parts 形态的输出被静默丢弃;unwrapCustomToolInput
在 input 为对象/数组时以原始 JSON 解包,不再仅限字符串。

Ref #4219
2026-07-12 20:39:56 +08:00
辰ing bd2aafb8fc feat(translator): custom 工具调用以 custom_tool_call item 回放给客户端
上游模型对 custom(freeform)工具的调用经 chat completions 返回为
function call(arguments 为 {"input": ...}),但 Codex Desktop 仅
识别 custom_tool_call item 形态。

响应方向按原始请求中声明为 type=custom 的工具名(顶层 tools 与
additional_tools,流开始时缓存于状态)区分处理:

- 流式:output_item.added(type=custom_tool_call)→
  custom_tool_call_input.done → output_item.done;JSON arguments
  增量无法映射为 freeform input 增量,故 custom 工具不发
  arguments delta,完整 input 随 done 事件一次性下发
- response.completed 与非流式输出同样产出 custom_tool_call item,
  input 从 {"input": ...} 包装中解出

Ref #4219
2026-07-12 20:39:55 +08:00
辰ing bd7cc64732 feat(translator): 支持 Codex additional_tools 工具下发与 custom 工具历史回放
Codex Desktop(Responses Lite,请求头 X-Openai-Internal-Codex-Responses-Lite)
的请求顶层 tools 为 null:工具定义经 input 中的 additional_tools item
下发,历史工具调用为 custom_tool_call / custom_tool_call_output item。
原转换仅识别顶层 tools 与 message/reasoning/function_call/
function_call_output,导致发往 openai-compatibility 上游的请求既无
工具定义也无工具调用历史,模型据此答复"没有可用工具"。

请求方向:
- 工具收集合并顶层 tools 与所有 additional_tools item(function/
  namespace/custom 共用同一转换)
- custom(freeform)工具定义映射为带 {"input": string} 参数的
  function 工具,与 Codex function 形态 apply_patch 的形状一致
- custom_tool_call 回放为 assistant tool_calls,原始 input 包装为
  {"input": ...} arguments;custom_tool_call_output 回放为 tool
  消息,content-parts 数组拍平为文本,并纳入 outputCallIDs 预扫描
  以维持工具消息相邻性

同时引入响应方向所需的 responsesCustomToolNames /
unwrapCustomToolInput 辅助函数。

Ref #4219
2026-07-12 20:39:55 +08:00
辰ing 07455ecba7 fix(translator): 上游流式 tool_calls 缺少 id 时合成 call_id 保证事件链完整
部分 OpenAI 兼容上游(自建 vLLM/sglang、部分中转网关)在流式 delta 中
不携带 tool_calls[].id。原逻辑要求非空 id 才发出
response.output_item.added/done 事件链,导致 Codex 侧完全收不到
函数调用(表现为工具全部不可用)。非流式路径同样产出 call_id 为空
的残缺 item。

缺失 id 时按 call_<responseID>_<choiceIdx>_<toolIndex> 合成确定性
call_id;chat completions 上游无状态,call_id 仅需单请求内自洽,
合成 id 可安全往返。

Ref #4219
2026-07-12 20:39:54 +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 d899c96239 feat(translator): map OpenAI max_tokens to Gemini's maxOutputTokens
- Added logic to convert OpenAI `max_tokens` and `max_completion_tokens` to `generationConfig.maxOutputTokens`.
- Implemented unit tests to validate proper mapping behavior, including precedence of `max_tokens` over `max_completion_tokens`.

Closes: #4108
2026-07-09 04:17:28 +08:00
Luis Pater bea9567051 feat(translator): add cache control handling for responses and messages
- Introduced `AttachCacheControl` and `AttachMessageCacheControl` utilities for consistent cache control injection in content parts and messages.
- Updated Claude translator modules to preserve and apply cache control metadata for responses, messages, tools, and parts.
- Added unit tests to validate proper cache control behavior across multiple scenarios.

Closes: #4146
2026-07-09 04:02:51 +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 14b139661d refactor(translator): simplify response logic and enhance thinking compatibility handling
- Consolidated thinking content block handling for improved clarity and efficiency.
- Unified `generationConfig` handling, including normalization of `thinkingConfig` settings.
- Added `applyOpenAIThinkingCompatibilityToAntigravity` for consistent reasoning and thought configuration compatibility.

Closes: #4067
2026-07-08 04:37:16 +08:00
Luis Pater dc77bf4db3 feat(translator): enhance Claude tool response handling with structured content parsing
- Added `applyResponsesToolResultContent` to process and structure tool response content.
- Introduced conversion logic for text, image, and file parts in tool responses.
- Updated tests to validate correct handling of data URL images in tool response outputs.

Closes: #4116
2026-07-07 15:38:35 +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 5afc0f1d5e fix(translator): remove temperature parameter handling in Claude request transformations
Closes: #4071
2026-07-04 00:36:58 +08:00
TooYoungTooSimp 611d65eacc Improve reasoning content handling in response logic
Enhance reasoning content retrieval by falling back to 'reasoning' if 'reasoning_content' is absent or empty.
In some selfhosted openai-like server serving deepseek, the response.delta may contains reasoning rather than reasoning_content. So the client like codex does not recognize the thinking process.
2026-07-01 10:28:36 +08:00
sususu98 3648bc155e fix(translator): align reasoning merge with Responses visible text rules
Recognize model-visible output_text even when message.role is user, accept
assistant string content, and preserve raw merged text without trimming.
Always merge the adjacent visible item into native Gemini reasoning turns so
trailing output is not dropped by prefill stripping.
2026-06-29 13:56:57 +08:00
sususu98 4b51f85c4c fix(translator): map OpenAI Responses reasoning to Gemini two-part signatures
Build Gemini-native model contents for reasoning items when the routed model
uses the Gemini signature provider: thought summary on a thought part and the
replay signature on the adjacent visible part. Merge following assistant
visible text when it is not trailing prefill, and keep reasoning history when
stripping trailing model-authored prefill.

Extend Responses translator tests for signature compatibility and Antigravity
wrapper coverage; clarify stale replay signature expectations in executor tests.
2026-06-29 13:40:09 +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 bd646819ed test(translator, runtime): ensure empty text parts are skipped without null values
- Added tests for `ConvertOpenAIRequestToAntigravity` and `ConvertOpenAIRequestToGemini` to verify skipping of empty text parts while maintaining non-null structure.
- Refactored part index logic to correctly handle placement of replay parts in `antigravity_reasoning_replay`.
- Introduced helper functions `antigravityExistingReplayPartPath` and `antigravityReplayPartWritePath` for consistent replay part path management.
2026-06-23 08:07:03 +08:00
Luis Pater 35c3d80ab9 feat(translator): add support for handling video URLs in Gemini requests
- Updated `ConvertOpenAIRequestToGemini` to process `video_url` message types, extracting and embedding video metadata (MIME type and base64 data) into Gemini request parts.
- Added corresponding unit tests to validate `video_url` handling, ensuring proper extraction and data preservation.

Closes: #3920
2026-06-20 14:46:39 +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 011ffe1dd5 feat(translator): enforce FIFO order in tool call ID consumption for Gemini requests
- Updated `ConvertGeminiRequestToOpenAI` to consume `tool_call_id`s in FIFO order, ensuring consistent mapping between calls and responses.
- Added fallback logic for unmatched or extra function responses to generate unique `tool_call_id`s with a `call_` prefix.
- Introduced comprehensive unit tests to validate order enforcement, distinct ID assignment, and fallback behavior.

Closes: #3874
2026-06-20 14:01:50 +08:00
Luis Pater b4bec344f6 feat(translator): sanitize parametersJsonSchema in OpenAI to Gemini request handling
- Applied `CleanJSONSchemaForGemini` to ensure compatibility by removing unsupported fields and cleaning schema requirements.
- Added test cases to validate schema transformation, ensuring only necessary `required` fields are retained.
- Enhanced both request and response flows to consistently clean and sanitize tool parameter schemas.

Closes: #3863
2026-06-20 13:34:05 +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 bc652c7bf0 feat(translator): add support for text.format conversion in OpenAI to Gemini requests
- Implemented `applyOpenAIResponsesTextFormatToGemini` to handle `json_object` and `json_schema` formats.
- Updated generation config to set appropriate `responseMimeType` and optional `responseJsonSchema`.
- Introduced unit tests to validate correct handling of `text.format` conversion scenarios.

Closes: #3721
2026-06-20 10:14:39 +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 1b849b6d8f feat(translator): attach reasoning_content to assistant and tool messages in OpenAI response conversion
- Enhanced `ConvertOpenAIResponsesRequestToOpenAIChatCompletions` to include `reasoning_content` in assistant and tool call messages.
- Introduced `collectOpenAIResponsesReasoningContent` for aggregating reasoning summaries.
- Added tests to validate reasoning attachment in various scenarios, including empty reasoning, tool calls, and reasoning followed by user messages.

Closes: #3397
2026-06-19 21:36:55 +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