Skip to content

Conversation

@martinffx
Copy link
Owner

Add Tool Calling Provider Translation

Enables Athena to translate tool calling between Anthropic and OpenRouter formats with
provider-specific handling for DeepSeek, Qwen, and Kimi K2 models.

Changes

  • Provider detection based on model identifiers
  • Qwen dual-format support (OpenAI tool_calls array + Qwen-Agent function_call object)
  • Kimi K2 special token parsing
  • DeepSeek standard OpenAI format (no changes needed)
  • Enhanced streaming support for provider-specific formats
  • Tool call validation ensuring matching responses

Why

Claude Code users can now reliably use alternative models with function calling without
rewriting integration code. Transparent format translation maintains full compatibility with
existing workflows.

Testing

  • Unit tests for all provider formats
  • Streaming integration tests
  • Multi-turn conversation validation

martinffx and others added 18 commits October 5, 2025 13:39
Isolate Kimi K2 provider-specific code into kimi.go following
single-responsibility principle. Extract generic SSE helpers to
streaming.go for reuse across providers.

- Add kimi.go with parseKimiToolCalls + handleKimiStreaming (155 lines)
- Add kimi_test.go with comprehensive test coverage (15 tests, 321 lines)
- Add streaming.go with reusable SSE helpers (39 lines)
- Add streaming_test.go with noopFlusher test helper (6 lines)

This sets clean pattern for Phase 4 (Qwen) implementation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Add support for Qwen models with dual tool calling formats:
- Standard OpenAI tool_calls array (vLLM/OpenRouter)
- Qwen-Agent function_call object format

Extract Qwen parsing logic into dedicated qwen.go file to improve
maintainability and prepare for additional provider-specific parsers.

Changes:
- New qwen.go with parseQwenToolCall supporting both formats
- New qwen_test.go with 8 comprehensive test cases
- Update processStreamDelta to use typed ToolCall structs
- Add 3 streaming integration tests for Qwen formats
- Clean up providers_test.go by removing Qwen tests

All tests passing (100% backward compatible).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Isolate Kimi K2 provider-specific code into kimi.go following
single-responsibility principle. Extract generic SSE helpers to
streaming.go for reuse across providers.

- Add kimi.go with parseKimiToolCalls + handleKimiStreaming (155 lines)
- Add kimi_test.go with comprehensive test coverage (15 tests, 326 lines)
- Add streaming.go with reusable SSE helpers (39 lines)

This sets clean pattern for Phase 4 (Qwen) implementation.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Complete comprehensive error handling across the tool calling pipeline with
proper error propagation, validation, and logging. All transformation functions
now return errors with appropriate HTTP status codes and detailed messages.

Key improvements:
- Added sendStreamError() helper for SSE error event emission
- Modified OpenAIToAnthropic() to return errors with full validation
- Refactored OpenAIToAnthropic() to reduce cyclomatic complexity
  - Extracted: validateOpenAIResponse(), handleKimiFormat(), handleQwenFunctionCall(),
    handleStandardToolCalls(), buildAnthropicResponse()
- Added error handling in HandleNonStreaming() with 502 status codes
- Format detection logging already implemented in routing request logs
- Comprehensive error scenario tests (9 test cases)

Testing:
- All 106 tests passing (transform, server, integration, error scenarios)
- Linter passing (resolved cyclomatic complexity from 31 to acceptable)
- No vulnerabilities found
- Error paths tested: malformed Kimi tokens, invalid responses, buffer exceeded

Implementation details:
- Error types mapped to HTTP status codes (400, 500, 502)
- Streaming errors use proper SSE event format
- Error messages sanitized for client responses
- All error conditions logged at appropriate levels

Progress: Phase 6 complete (26/34 tasks, 76% overall)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Updated CLAUDE.md with comprehensive tool calling documentation:
  - Added provider format detection to Key Components
  - Created Tool Calling Format Support section covering all 4 formats
  - Documented format detection strategy with precedence rules
  - Added configuration examples for tool calling
- Created example configurations for all provider formats:
  - examples/deepseek-tools.yml (Standard OpenAI format)
  - examples/qwen-tools.yml (Dual-format support)
  - examples/kimi-tools.yml (Special token format)
- Updated tasks.md progress tracking to 100% complete

All 28 implementation tasks complete. Feature ready for production.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants