Files
grok2api/app/api
zxc123developer 1d49377bee feat: add OpenAI-compatible function calling (tools / tool_calls)
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.
2026-02-24 21:49:25 +08:00
..