Add Anthropic Messages endpoint (/v1/messages), stream + non-stream

- api::anthropic: AnthropicRequest -> internal ChatRequest (system, text/image/
  thinking/tool_use/tool_result blocks, tools input_schema->parameters,
  tool_choice, sampling, stop_sequences); ChatResponse -> Anthropic message
  (thinking/text/tool_use blocks, stop_reason end_turn/max_tokens/tool_use, usage).
- AnthropicStream translator: message_start / content_block_start/delta/stop /
  message_delta / message_stop, with block-index state.
- Refactor service.stream to yield semantic ChatChunk (+ prompt_len); SSE
  serialization and the terminal [DONE] moved to the OpenAI route. ChatChunk
  carries a #[serde(skip)] usage field for presenters (invisible on the OpenAI
  wire). The same engine-driving (abort/stop/disconnect) feeds both formats.
- Route POST /v1/messages; the OpenAI wire is unchanged.

7 new tests (5 conversion unit + 2 HTTP end-to-end); 87 green + 2 ignored;
clippy clean; cargo fmt applied. README updated.
Core cases faithful; deferred: defer_loading/tool_addition/tool_reference,
thinking budget_tokens, prompt-cache token accounting.
This commit is contained in:
adm committed 2026-11-24 19:58:29 +01:00
1 parent b6619d30de
commit 9e6d44fa1a
7 files changed
+758 -24

No files matched your search

+13 -2
View File
@@ -47,6 +47,15 @@ OpenAI-compatible endpoints:
- `POST /v1/chat/completions` — streaming (SSE) and non-streaming.
- `GET /v1/models`, `GET /health`.
Anthropic-compatible endpoint:
- `POST /v1/messages` — the Anthropic Messages API (streaming + non-streaming),
translated onto the same engine: `system`, text/image/thinking/tool_use/
tool_result content blocks, tools (`input_schema`), `tool_choice`, sampling and
`stop_sequences`. Responses use Anthropic content blocks and `stop_reason`
(`end_turn` / `max_tokens` / `tool_use`); streaming emits the
`message_start` / `content_block_*` / `message_delta` / `message_stop` sequence.
Behavior implemented and validated against the real Inkling engine:
- **Chain-of-thought separation** — thinking is streamed live as
@@ -121,14 +130,16 @@ is preserved untouched and reported as ignored — unknown keys never break star
## Status
Working and tested (80 tests, plus 2 real-file tests run on demand). Text, images
Working and tested (87 tests, plus 2 real-file tests run on demand). Text, images
and the full client-side tool loop have been validated live against the real
Inkling engine.
Not yet implemented (honest gaps): forced `tool_choice` (`required` / named function
returns a clear 400), multi-slot batching, logprobs / structured output, automatic
engine restart after a crash, a full SSRF guard against DNS rebinding, and the
Anthropic / Responses native endpoints.
OpenAI Responses native endpoint. The Anthropic adapter covers the core cases but
not yet the `defer_loading` / `tool_addition` / `tool_reference` tool machinery, the
thinking `budget_tokens` mapping, or prompt-cache token accounting.
## License and credits