When n=1, the stream processor locks onto the first image received
as target. If a different image gets its final version first,
completed >= n breaks the WebSocket loop, but the processor saves
the target's medium-quality data as "final".
Now checks if the target actually has is_final=True before selecting
it. If not, falls back to the highest quality image available.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Implements prompt-based tool call emulation to support the OpenAI
function calling API (tools / tool_calls / tool_choice), fixing #219.
Changes:
- New module app/services/grok/utils/tool_call.py
- build_tool_prompt(): inject tool definitions into system prompt
using <tool_call> XML tag format
- parse_tool_calls(): regex-extract <tool_call> blocks and convert
to OpenAI-format tool_calls list
- format_tool_history(): convert multi-turn tool messages to text
for Grok's single-message API
- build_tool_overrides(): experimental passthrough mode helper
- app/api/v1/chat.py
- Add tool_calls, tool_call_id, name fields to MessageItem
- Add tools, tool_choice, parallel_tool_calls to ChatCompletionRequest
- Add "tool" to VALID_ROLES
- Validate tool role messages (require tool_call_id)
- Validate tools format and tool_choice values
- app/services/grok/services/chat.py
- MessageExtractor: preprocess tool history and inject tool prompt
- StreamProcessor: buffer tokens when tools active, emit tool_calls
chunk with finish_reason="tool_calls" after stream ends
- CollectProcessor: parse tool_calls from final content
- GrokChatService/ChatService: forward tools params through the stack
- config.defaults.toml: add tool_call_mode = "prompt" config key
(supports "prompt" default and experimental "passthrough" mode)
Backward compatible: requests without tools are completely unaffected.
Both asyncpg and aiomysql reject SSL-related query parameters (sslmode,
ssl-mode, ssl) when passed through SQLAlchemy URLs. This extracts SSL
mode from the URL, normalizes it per backend, and passes proper
connect_args instead.
- Postgres: passes libpq-style mode string via connect_args={"ssl": mode}
- MySQL: builds an SSLContext matching the requested verification level
- Supports cross-dialect aliases (e.g. verify-full works for both backends)
- Adds mariadb+aiomysql:// prefix handling
- Raises ValueError on unrecognized SSL modes
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Standardize non-streaming response from 'data' to 'choices' format
- Add chat_format parameter to image services
- Support SSE streaming with OpenAI chat.completion.chunk format
- Fix partial_image events to use chat format
- Pass chat_format to _stream_ws in _stream_retry
- Wrap image content in markdown format for chat completions
- Fix an issue where WebSocket connections (e.g., for image generation) failed with "semaphore timeout" because proxy settings were not applied.
- Root cause: WebSocketClient was fetching proxy config during module-level instantiation, which occurred before config.load() finished.
- Modified WebSocketClient to dynamically resolve proxy settings at connection time.
- Added debug logging for proxy resolution and connector types.