feat(helexa-acp): anthropic-messages provider
Some checks failed
CI / CUDA type-check (push) Failing after 18s
CI / Format (push) Successful in 32s
build-prerelease / Resolve version stamps (push) Successful in 35s
CI / Test (push) Failing after 59s
CI / Clippy (push) Successful in 2m28s
CI / Build cortex SRPM (push) Has been skipped
CI / Publish cortex to COPR (push) Has been skipped
CI / Build neuron SRPM (push) Has been skipped
CI / Publish neuron to COPR (push) Has been skipped
CI / Bump version in source (push) Has been skipped
build-prerelease / Build cortex binary (push) Successful in 4m17s
build-prerelease / Build neuron-blackwell (push) Successful in 5m32s
build-prerelease / Package cortex RPM (push) Successful in 1m21s
build-prerelease / Build neuron-ampere (push) Successful in 7m50s
build-prerelease / Build neuron-ada (push) Successful in 5m55s
build-prerelease / Package helexa-neuron-ada RPM (push) Successful in 2m55s
build-prerelease / Package helexa-neuron-ampere RPM (push) Successful in 3m2s
build-prerelease / Package helexa-neuron-blackwell RPM (push) Successful in 3m52s
build-prerelease / Publish to rpm.lair.cafe (unstable) (push) Successful in 1m4s

Stage 6b. Third provider impl, completing the wire-format trio
(openai-chat, openai-responses, anthropic-messages). Lets a
helexa-acp endpoint configured with `wire_api = "anthropic-messages"`
drive Claude models — either against Anthropic directly or via
cortex's /v1/messages translation surface.

## Encoder (CompletionRequest → Anthropic body)

- System messages flatten to the top-level `system` field
  (concatenated with blank lines when there are multiple).
- User text → `{role:"user", content:"..."}`.
- User MultiPart (text + images) → `content` array with Anthropic's
  distinct image shape: `{type:"image", source:{type:"base64",
  media_type, data}}` — structurally different from OpenAI's
  `image_url` data URI.
- Assistant text → `{role:"assistant", content:"..."}`.
- Assistant tool_calls → `content` array with optional `{type:"text"}`
  block plus one `{type:"tool_use", id, name, input:<parsed json>}`
  per call. The internal arguments JSON string is parsed back to a
  Value before encoding (Anthropic requires the parsed form);
  malformed JSON falls back to a String input so the request body
  still serialises.
- Tool result → `{role:"user", content:[{type:"tool_result",
  tool_use_id, content}]}` per Anthropic's convention (no separate
  `tool` role).
- `max_tokens` is required by Anthropic; defaults to 8192 when the
  request doesn't specify.

## Decoder (Anthropic SSE → CompletionEvent)

Named SSE events:

- `message_start` → captures input_tokens from `usage` for the
  eventual UsageStats.
- `content_block_start` (type=text) → TextDelta (initial text, if any).
- `content_block_start` (type=tool_use) → ToolCallStart; if a
  pre-buffered `input` is present, also emits a single
  ToolCallArgsDelta.
- `content_block_start` (type=thinking, for extended-thinking
  models) → ReasoningDelta.
- `content_block_delta` (text_delta) → TextDelta.
- `content_block_delta` (input_json_delta) → ToolCallArgsDelta,
  correlated by block index.
- `content_block_delta` (thinking_delta) → ReasoningDelta.
- `message_delta` → Usage (final output_tokens) + Finish with
  stop_reason mapped: end_turn/stop_sequence → "stop", max_tokens
  → "length", tool_use → "tool_calls".
- `message_stop` → stream terminates.
- `ping` ignored (Anthropic's keep-alive).
- `error` → yields Err and ends the stream.

## Wiring

- Authentication: `x-api-key` + `anthropic-version: 2023-06-01`
  headers (not Bearer). Both ship when api_key is configured;
  servers that don't care (cortex) ignore them.
- `WireApi::AnthropicMessages` in build_provider now constructs
  the provider instead of erroring "reserved for future".
- `provider::mod.rs` registers the new module.

18 new unit tests: encoder (system collapse, multi-system concat,
default max_tokens, multipart with image, tool_use blocks, tool
results, malformed JSON arg fallback), decoder (text streaming,
tool_use lifecycle, max_tokens→length mapping, empty deltas, ping
events, error events, cancellation, malformed payload skip,
thinking blocks).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-31 14:01:59 +03:00
parent cad7552104
commit 8fa1d1962e
3 changed files with 1204 additions and 5 deletions

View File

@@ -30,7 +30,8 @@ mod tools;
use agent::Agent; use agent::Agent;
use config::{Config, EndpointConfig, WireApi}; use config::{Config, EndpointConfig, WireApi};
use provider::{ use provider::{
Provider, openai_chat::OpenAIChatProvider, openai_responses::OpenAIResponsesProvider, Provider, anthropic_messages::AnthropicMessagesProvider, openai_chat::OpenAIChatProvider,
openai_responses::OpenAIResponsesProvider,
}; };
/// Set up tracing. Logs go to stderr by default — stdout is /// Set up tracing. Logs go to stderr by default — stdout is
@@ -94,10 +95,7 @@ fn build_provider(endpoint: EndpointConfig) -> anyhow::Result<Arc<dyn Provider>>
match endpoint.wire_api { match endpoint.wire_api {
WireApi::OpenAiChat => Ok(Arc::new(OpenAIChatProvider::new(endpoint)?)), WireApi::OpenAiChat => Ok(Arc::new(OpenAIChatProvider::new(endpoint)?)),
WireApi::OpenAiResponses => Ok(Arc::new(OpenAIResponsesProvider::new(endpoint)?)), WireApi::OpenAiResponses => Ok(Arc::new(OpenAIResponsesProvider::new(endpoint)?)),
WireApi::AnthropicMessages => Err(anyhow::anyhow!( WireApi::AnthropicMessages => Ok(Arc::new(AnthropicMessagesProvider::new(endpoint)?)),
"endpoint '{}' wire_api 'anthropic-messages' is reserved for a future provider",
endpoint.name
)),
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -17,6 +17,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
pub mod anthropic_messages;
pub mod openai_chat; pub mod openai_chat;
pub mod openai_responses; pub mod openai_responses;