Commit Graph

8 Commits

Author SHA1 Message Date
ParthSareen fceafefdce anthropic: fix ToolCallFunctionArguments type after rebase
Update tests and implementation to use the new ordered map-based
ToolCallFunctionArguments type which replaces the previous map[string]any.

- Add mapToArgs helper to convert map[string]any to ToolCallFunctionArguments
- Add testArgs and testProps helpers in tests
- Use cmpopts.IgnoreUnexported for cmp.Diff comparisons
2026-01-05 21:50:50 -08:00
ParthSareen fa42204da8 anthropic: use pointer types for Text and Thinking fields
Use *string instead of string for Text and Thinking fields in ContentBlock
so that omitempty works correctly:
- nil pointer: field omitted from JSON (for blocks that don't use it)
- ptr(""): field present as "" (for SDK streaming accumulation)
- ptr("content"): field present with content

This keeps the JSON output clean (text blocks don't have thinking field,
thinking blocks don't have text field) while still satisfying SDK
requirements for field presence during streaming.
2026-01-05 19:19:34 -08:00
ParthSareen 6188e90aab anthropic: preserve messages with only thinking content
Fix edge case where messages containing only a thinking block (no text,
images, or tool calls) would be dropped. Add thinking != "" to the
condition that creates messages from content blocks.
2026-01-05 19:19:34 -08:00
ParthSareen 5ba2092f0a anthropic: fix streaming with SDK by including empty fields
Remove omitempty from Text and Thinking fields in ContentBlock struct.
The Anthropic SDK requires these fields to be present (even if empty)
in content_block_start events to properly accumulate streaming deltas.
2026-01-05 19:19:34 -08:00
ParthSareen 90cf232df2 anthropic: remove redundant comments
Remove obvious comments that don't add value (e.g., "// Convert messages",
"// Handle done"). Keep godoc comments and those explaining API mappings.
2026-01-05 19:19:34 -08:00
ParthSareen ed1e17bb35 anthropic: fix error handling and update docs
- Add proper error handling for JSON marshal in StreamConverter to
  prevent corrupted streams when tool arguments cannot be serialized
- Add tests for unmarshalable arguments and mixed validity scenarios
- Fix documentation typo and update recommended models to qwen3-coder
2026-01-05 19:19:34 -08:00
ParthSareen 6229df5b90 anthropic: add unit and integration tests
- Unit tests for transformation functions (FromMessagesRequest, ToMessagesResponse)
- Unit tests for error handling and edge cases
- Middleware integration tests with httptest
- Fix lint issues (gofmt)
- Fix unused struct fields in StreamConverter
- Add fallback for crypto/rand errors
2026-01-05 19:19:34 -08:00
ParthSareen f760ae1fdd api: add Anthropic Messages API compatibility layer
Add middleware to support the Anthropic Messages API format at /v1/messages.
This enables tools like Claude Code to work with Ollama models through the
Anthropic API interface.

Features:
- Request/response transformation between Anthropic and internal formats
- Streaming support with SSE events (message_start, content_block_delta, etc.)
- Tool calling support (tool_use and tool_result content blocks)
- Thinking/extended thinking block support
- Image content block support (base64)
- System prompt handling
- Multi-turn conversation support
- Proper stop_reason mapping (end_turn, max_tokens, tool_use)
- Error responses in Anthropic format

New files:
- anthropic/anthropic.go: Types and transformation functions
- middleware/anthropic.go: Request/response middleware
2026-01-05 19:19:34 -08:00