Commit Graph

67 Commits

Author SHA1 Message Date
Luis Pater
e4119f83b4 fix(xai): use chat base url resolution for image and video requests
- Route image and video generation requests through `xaiChatBaseURL` so OAuth credentials default to the CLI chat proxy endpoint.
- Update `using_api` and base URL definitions to apply to both HTTP chat and media requests.

Closes: #5335
2026-08-30 14:01:18 +08:00
Luis Pater
9b88808fc7 fix(xai): fold namespace tools and restore dispatcher tool calls
- Introduce `xaiNamespaceRestorer` to track and restore folded dispatcher tool calls across SSE and WebSocket response events.
- Support unwrapping dispatcher tool calls and arguments in `output_item.added` and `function_call_arguments.done` events.
- Normalize historical input namespace tool calls to dispatcher format when namespace folding is active.

Closes: #5214
2026-08-27 01:08:35 +08:00
Luis Pater
92d96e0b72 fix(cliproxy): support base_URL-only config credentials and skip stale auth headers
- Preserve API key entries with empty `api_key` when `base_url` is configured, and extend config dedupe/ID logic to include base URL, proxy, prefix, and headers so identities are stable.
- Update config/auth handling so `auth_kind=apikey` is treated as config API-key auth even without an `api_key` field, enabling base_url-only credential records.
- Ensure Gemini/Codex/XAI request path clears `Authorization`/provider auth headers when token is empty to avoid leaking unrelated auth state.
2026-08-20 15:19:07 +08:00
Luis Pater
e400d7191d feat(xai): bump client version to 0.2.120 and include Grok Shell auth headers on XAI requests 2026-08-05 07:28:55 +08:00
Luis Pater
8423cce2d1 feat(executor): add configurable injection of x_search tool for xAI requests
- Introduced `InjectXSearch` in `XAIConfig` to enable automatic injection of the native `x_search` tool when not explicitly declared.
- Updated `XAIExecutor` to honor the `InjectXSearch` configuration, ensuring consistent tool availability.
- Enhanced configuration handling with support for dynamic diffing to track changes in `InjectXSearch`.
- Added comprehensive tests to validate `InjectXSearch` behavior, including preparation and tool choice synchronization.
- Updated example config and documentation to outline `InjectXSearch` usage.

Closes: #4339
2026-07-27 04:41:24 +08:00
Luis Pater
fe4ae4989c chore(pluginhost): refactor and remove unused interceptors and executor methods
- Removed deprecated interceptor and executor-related methods, including `callRequestInterceptor`, `callResponseInterceptor`, and `callStreamChunkInterceptor`.
- Consolidated unused logic and pruned redundant imports to streamline `adapters.go`.
- No functional changes.
2026-07-26 14:31:45 +08:00
Luis Pater
f6c32ec3ff feat(runtime): implement derived session identity features and tests
- Added utilities for generating stable session identities, including `DerivedSessionID`, `DerivedSessionUUID`, and `ProviderSessionUUID`.
- Introduced support for Antigravity's negative decimal `DerivedAntigravitySessionID` mapping.
- Enhanced metadata handling for provider-scoped UUID stabilization and consistent session identity inference.
- Added comprehensive tests for stability, namespace isolation, and preferred execution session prioritization.
2026-07-26 03:07:21 +08:00
Luis Pater
41f6ea8950 feat(runtime): enhance response preparation for xAI executor output controls
- Added logic to `preserveXAIResponsesOutputControls` for retaining supported controls (`max_output_tokens`, `temperature`, `top_p`, `top_k`) during translation.
- Removed unsupported fields such as `stop` where necessary to align with xAI's Responses API.
- Updated tests to validate the preservation and removal of fields across various input scenarios.
- Improved payload configuration handling with additional unit tests for ensuring compatibility with model-specific overrides.

Closes: #4464
2026-07-26 00:56:57 +08:00
Luis Pater
84bf9376e5 feat(executor): replace sdktranslator.TranslateRequest with helps.TranslateRequestWithCodexMultiAgentV2
- Updated Kimi and Gemini executors to use `TranslateRequestWithCodexMultiAgentV2` for improved multi-agent v2 optimization.
- Enhanced configuration and API server to support `CodexOptimizeMultiAgentV2`.
- Added codex multi-agent V2 optimizations in `config`, `executor`, and `watcher` modules.
2026-07-25 00:32:37 +08:00
Luis Pater
f3e36f19c0 feat(executor): add Claude input token state handling and token estimation logic
- Introduced `ClaudeInputTokenState` to track and estimate input token usage for Claude message_start events.
- Added `TranslateStreamWithClaudeInputTokens` for token-aware stream translation.
- Updated executors (XAI, Kimi, Gemini) to leverage the new logic.
- Included robust test cases for token counting, JSON validation, and concurrent tokenization scenarios.
2026-07-22 05:21:14 +08:00
Luis Pater
cb110ad4fa perf(executor): improve token counting by introducing countXAIInputTokens
- Refactored token counting logic into a reusable `countXAIInputTokens` function for better readability and maintainability.
- Replaced direct encoder usage with structured input handling, including JSON parsing and segment collection.
- Switched tokenizer from `Cl100kBase` to `O200kBase` to support larger contexts.

Closes: #4492
2026-07-22 04:00:51 +08:00
Luis Pater
8b4fd28c95 perf(executor): replace sjson.SetBytes with optimized helpers for conditional payload updates
- Introduced `SetStringIfDifferent`, `SetBoolIfDifferent`, and similar helpers in `payload_helpers` to avoid unnecessary writes during JSON modifications.
- Simplified message patching in `kimi_executor` for reasoning and tool call adjustments.
- Updated all executors (Gemini, Codex, XAI, Antigravity, etc.) to use the new helpers, enhancing readability and potentially reducing overhead.
- Removed obsolete `filterKimiEmptyAssistantMessages` function in `kimi_executor`.
2026-07-20 13:57:14 +08:00
Luis Pater
f175c084d3 feat(executor): promote additional_tools to top-level tools in XAI requests
- Added `promoteXAIAdditionalTools` to normalize and migrate `additional_tools` to the top-level `tools` array in XAI request bodies.
- Updated test cases to validate the promotion logic and ensure unsupported `additional_tools` items are excluded from input payloads.

Closes: #4434
2026-07-19 16:32:47 +08:00
Luis Pater
2f80cb9ae8 feat(executor): add root union type normalization and extended tests
- Introduced `normalizeXAIObjectRootUnionBranchTypes` for ensuring object-only branches in root-level unions.
- Updated XAI executor logic to handle additional schema adjustments, ensuring compatibility with stricter validations.
- Extended test coverage to validate schema normalization, including both functional and custom tool scenarios.

Closes: #4397
2026-07-18 03:34:30 +08:00
Luis Pater
baffbe2cc7 feat(executor): implement agent-scoped cache key isolation and enhance replay handling
- Introduced agent-specific cache key isolation to ensure distinct prompt caching across agents.
- Improved reasoning replay handling for Claude models, including cumulative tool turn restoration and validation.
- Enhanced HTTP and WebSocket session consistency during execution scope and cache replay synchronization.
- Added new unit tests and extended coverage for prompt caching, agent isolation, and replay cache functionality.

Closes: #4352
2026-07-18 02:18:09 +08:00
Luis Pater
9f4f53ca5a fix(xai): update compact request handling to use dedicated base URL
- Switched from `xaiChatBaseURL` to `xaiCompactBaseURL` for compact requests to avoid 404 errors from CLI chat-proxy.
- Updated headers to use standard API headers for compact endpoints.
- Added `xaiCompactBaseURL` helper function for dedicated compact request base URL resolution.
- Adjusted comments to clarify handling of compact and websocket transports.

Closes: #4376
2026-07-17 17:52:53 +08:00
Luis Pater
f583414fd9 fix(xai): normalize image refs with special JSON keys 2026-07-17 16:13:54 +08:00
jellyfish-p
f82123a3ef fix(xai): rewrite image.image_url to image.url before upstream send
Normalize OpenAI-style image refs across images, videos, and Responses so Grok receives {image:{url:...}} instead of image_url.
2026-07-17 14:42:12 +08:00
Luis Pater
466cee6e16 fix: complete xAI image usage reporting 2026-07-16 02:32:56 +08:00
Luis Pater
3cb2d27d98 Merge remote-tracking branch 'origin/pr-4344' into dev 2026-07-16 02:29:45 +08:00
Luis Pater
b6ce0beecd feat(executor): simplify xAI function schema handling and rejection logic
- Refactored `xaiFunctionParametersNeedSimplification` to identify schemas xAI cannot process, including those with non-object types in unions.
- Limited parameter simplifications to problematic schemas instead of specific tools or namespaces.
- Clarified and updated debug messages to reflect schema rejection and hang prevention.

Closes: #4343
2026-07-16 02:05:50 +08:00
Matt Van Horn
9f500aef7a fix: publish usage event for xAI images/generations path 2026-07-15 08:45:44 -07:00
Luis Pater
a9831c841a fix(xai): sync allowed_tools and prune orphaned choices for x_search inject
Normalize and drop tool_choice entries that reference tools removed by
normalizeXAITools before injecting native x_search, then allow x_search in
allowed_tools without duplicates so Grok can select the injected tool.
2026-07-14 05:03:19 +08:00
Luis Pater
a5577cc67d feat(xai): enhance response filtering with client-declared tool support to distinguish internal X Search traces
- Added `clientDeclaredTools` to `xaiInternalXSearchResponseFilter` for improved filtering precision.
- Implemented `collectXAIClientDeclaredToolKeys` to register client-declared tools with effective normalization to function calls.
- Prevented leakage of internal traces (e.g., `xs_call-*`) while preserving client-declared tools with overlapping names.
- Introduced multiple test cases to ensure filter correctness and preserve expected WebSocket response outputs.
- Enhanced completed output handling in response filtering.

Closes: #4282 / PR #4284.
2026-07-14 03:38:29 +08:00
vvanglro
4651c3705d perf(xai): reuse parsed input array 2026-07-14 00:44:14 +08:00
vvanglro
caa93a7fc6 fix(xai): filter internal X search calls 2026-07-14 00:19:49 +08:00
vvanglro
7bb8132817 fix(xai): normalize custom tool call history 2026-07-13 23:45:44 +08:00
Luis Pater
e674f19129 Fix xAI allowed tool namespace choices 2026-07-13 14:58:03 +08:00
Luis Pater
19fb0f0774 Merge pull request #4251 from huahai0202/fix/xai-namespace-tool-routing 2026-07-13 14:48:55 +08:00
huahai0202
4123e27583 fix(xai): handle additional namespace tools 2026-07-13 10:39:39 +08:00
sususu
18d239d567 fix(xai): clear replay after compaction 2026-07-12 23:59:39 +08:00
huahai0202
eb6d169407 fix(xai): qualify namespaced tool choices 2026-07-12 23:58:16 +08:00
huahai0202
c4eda81b93 fix(xai): address namespace routing review feedback
Use exact namespace separator matching, normalize replay-inserted calls, preserve payloads on JSON update failures, and remove redundant completed-response restoration.
2026-07-12 23:33:53 +08:00
huahai0202
0e3a3e6199 fix(xai): preserve namespace tool routing
xAI Responses does not accept namespace tool containers, so the executor flattens nested tools before forwarding them upstream. The original namespace support in 8b3670b8 moved children to the top level but discarded their namespace identity; ca67caf later retained namespace context only for targeted schema simplification.

Qualify flattened names to avoid collisions, normalize namespaced function-call history, and restore name plus namespace on HTTP and WebSocket responses. This keeps Codex MCP routing intact without changing the existing xAI tool_search filtering policy.
2026-07-12 23:16:57 +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
0ba5fab529 Merge pull request #4240 from BG-QWQ/fix/xai-cli-user-agent-4233
fix(executor): identify Grok CLI OAuth requests
2026-07-12 20:27:51 +08:00
Luis Pater
6c70996e43 feat(executor): add logging for resolved xAI base URL and enhance test coverage
- Introduced `logXAIResolvedBaseURL` for consistent logging of resolved upstream base URLs.
- Added helper `xaiBaseURLSource` to classify base URL origins (default, CLI chat proxy, custom).
- Enhanced test suite with `TestXAIBaseURLSource` to cover base URL source classification logic.
2026-07-12 17:23:56 +08:00
1502538344
1a0bbe09c6 fix(executor): identify Grok CLI OAuth requests 2026-07-12 16:40:08 +08:00
Luis Pater
e99a2056ba feat(executor): add using_api attribute support for API path toggle and enhance test coverage
- Introduced `using_api` attribute in xAI auth for toggling between official API and CLI chat-proxy paths.
- Updated `xaiChatBaseURL` and `applyXAIChatHeaders` to conditionally apply the appropriate logic based on the `using_api` value.
- Added robust handling of attribute and metadata parsing for `using_api`.
- Extended test suite to cover `using_api` scenarios, including defaults, overrides, and header behaviors.
2026-07-11 12:15:55 +08:00
Luis Pater
dc162b938e feat(executor): refactor XAI header application logic and extend test coverage
- Extracted `applyXAIDefaultHeaders` and `applyXAICustomHeaders` for cleaner separation of concerns in API header application.
- Updated `applyXAIChatHeaders` to ensure proper header overrides for CLI chat-proxy-specific configurations.
- Added test case to validate custom headers' ability to override CLI chat-proxy defaults.
2026-07-11 02:12:57 +08:00
Luis Pater
3533484a1f feat(executor): add chat-proxy-specific headers and improve base URL handling
- Introduced `xaiChatBaseURL` helper to normalize base URLs for non-media chat requests.
- Added `applyXAIChatHeaders` to handle CLI chat-proxy-specific headers.
- Updated logic to differentiate between default, chat-proxy, and custom gateway base URLs.
- Extended test coverage to validate behavior of headers and base URL handling across scenarios.

Closes: #4182
2026-07-11 02:07:01 +08:00
Luis Pater
ca67caf087 **fix(xai): improve namespace-specific tool parameter handling and simplify Codex automation schema**
- Refined tool normalization logic to pass namespace context (`namespaceName`) to `normalizeXAITool`.
- Limited schema simplification to `codex_app.automation_update` to avoid unintended modifications to unrelated tools or namespaces.
- Updated `xaiFunctionParametersNeedSimplification` to match tools by exact namespace and name.
- Enhanced tests to validate correct schema simplification for `codex_app.automation_update` while preserving unrelated tool definitions.
2026-07-10 13:28:35 +08:00
Wraient
0df267adbb fix(xai): prevent Desktop hang on complex tool schemas and set free-usage Retry-After
Codex Desktop injects codex_app.automation_update with a large oneOf+$ref
schema. On xAI free/build Responses, that request accepts HTTP but never
emits SSE, so clients hang until cancel.

- Simplify automation_update (and similar large oneOf+$ref schemas) to a
  permissive object schema before upstream xAI
- Map subscription free-usage-exhausted 429s to a 24h RetryAfter for
  account rotation/cooldown
- Add unit tests for both behaviors
2026-07-09 21:49:01 +05:30
Luis Pater
3fd189262d feat(executor): integrate model registry for reasoning effort support in XAI
- Replaced hardcoded model allowlist with dynamic model registry metadata lookup.
- Enhanced `xaiSupportsReasoningEffort` to utilize metadata for reasoning capability validation.
- Updated `sanitizeXAIResponsesBody` to log reasoning effort stripping for unsupported models.
- Added unit tests to verify registry-based reasoning effort handling across models, including Grok 4.5.

Closes: #4147
2026-07-09 03:37:11 +08:00
sususu98
05d1792d43 feat(xai): replay Grok reasoning for Claude messages (#3962)
Add Grok-native reasoning replay for Claude-sourced xAI Responses requests. Replay state is scoped by model and session, normalized to reasoning/function_call/custom_tool_call items, and injected with the same tool-call alignment semantics used by Codex.

Include the minimal Grok encrypted_content validator needed on upstream/dev so replay cache entries cannot accept Codex/GPT, Gemini, or Claude signature shapes. Cache completed output items from HTTP, streaming, and WebSocket xAI responses.

Cover two-turn Claude replay, tool_result continuity, valid Grok cache storage, and rejection of Codex-shaped encrypted_content.
2026-06-23 14:57:53 +08:00
sususu98
53a21dfb0b [codex] Drop foreign encrypted_content before xAI Grok upstream (#3961)
* Drop foreign encrypted_content before xAI Grok upstream

xAI Grok accepts provider-native encrypted_content as opaque replay state, but GPT/Codex reasoning signatures, Gemini thoughtSignature blobs, and Claude thinking signatures can all travel through OpenAI Responses-style reasoning.encrypted_content while remaining incompatible with xAI. Forwarding those foreign blobs to Grok causes upstream validation failures, especially when the foreign value is high-entropy enough to look ciphertext-like.

Add a Grok encrypted_content transport validator that stays conservative and shape-oriented:

- require unpadded standard base64 with no foreign characters
- reject obvious GPT/Codex gAAAA reasoning signatures before decode
- reject strict Claude thinking signatures in both official E-form and Antigravity R-form
- reject known Gemini thoughtSignature envelopes by reusing the central Gemini validator, covering Gemini 2.5 field-1 and Gemini 3.x field-2 shapes
- require decoded payloads to be long enough and high-entropy enough to look like native Grok ciphertext
- avoid decrypting, protobuf-parsing, or otherwise interpreting native Grok payloads on the hot path

Wire the validator into the xAI Responses request preparation path for reasoning and compaction input items. Invalid encrypted_content fields are deleted before the request is sent upstream, while the surrounding item is preserved and debug logging records only redacted metadata.

Extend coverage with native Grok corpus preservation, Gemini field-1/field-2 rejection, Claude E-form and R-form rejection, invalid-blob sanitizer tests, and compact/websocket replay preservation. The foreign-provider checks are deliberately narrow so high-entropy Grok blobs are not rejected merely because they look random.

* fix(xai): harden encrypted content sanitizer
2026-06-23 14:33:27 +08:00
Luis Pater
7c390a7a2e feat(runtime): add Claude Code session handling with caching and tests
- Introduced `ClaudeCodeSessionID` resolution logic, preferring headers over payload metadata.
- Added `ClaudeCodePromptCache` to map sessions to stable prompt cache keys.
- Refactored existing logic to integrate `ClaudeCodePromptCache` for session-based handling.
- Included extensive unit tests to validate session ID extraction, cache reuse, and header prioritization.
2026-06-23 13:19:13 +08:00
Luis Pater
28e2f9798c feat(executor): add session isolation for grok-composer models
- Introduced `xaiRequiresIsolatedConversation` to enforce session ID generation for `grok-composer` models.
- Updated request preparation logic to handle isolated conversations by setting `prompt_cache_key` and `x-grok-conv-id`.
- Added unit tests with coverage for session isolation, stateless models, and explicit `prompt_cache_key` scenarios.

Closes: #3750
2026-06-20 10:54:16 +08:00
Luis Pater
96a8b0cfe2 feat(executor): normalize reasoning text events and enhance handling logic
- Introduced `xaiNormalizeReasoningSummaryData` and related functions to normalize `reasoning_text` events into `reasoning_summary` shapes for standardization.
- Updated WebSocket and streaming logic to process normalized reasoning summary events correctly.
- Enhanced tests to validate normalization, order of events, and output structure in both stream and non-stream scenarios.
2026-06-17 13:00:00 +08:00
Luis Pater
529d9e92c9 feat(executor): add support for compact response handling in XAIExecutor
- Introduced `executeCompact` to handle non-streaming compact responses via the `/responses/compact` endpoint.
- Added `executeCompactionTriggerStream` for streaming responses triggered by `compaction_trigger`.
- Enhanced request preparation with `prepareResponsesRequestTo` for dynamic response formats.
- Updated logic to bypass streaming for `/responses/compact` and added fallback behaviors.
- Added comprehensive tests for compact response handling and event streaming validations.
2026-06-15 00:29:38 +08:00