feat: initial extract of shared anthropic-compat client + translator
Extracted from Plexum-minimax-provider and Plexum-kimi-provider (both
had near-identical copies under internal/anthropic + internal/translate).
Future provider plugins (Qwen, Doubao, …) import these two packages
instead of re-implementing the protocol.
anthropic/ (~220 LOC + 6 tests):
- minimal HTTP+SSE Anthropic Messages client (POST /v1/messages,
stream:true, parses event:/data: SSE)
- Client.UserAgent + Client.ExtraHeaders fields for per-backend
customization (Kimi needs "claude-code/0.1.0"; MiniMax is flexible)
- non-2xx surfaces as Go error; mid-stream errors as final
Event{Type:"error"}
translate/ (~220 LOC):
- CanonicalToAnthropic: TurnRequest → MessagesRequest
- blockToAnthropic: TextBlock / ToolUseBlock / ToolResultBlock /
ThinkingBlock → loose ContentBlock map; preserves signatures
- Translator: per-turn state machine; anthropic.Event stream →
canonical.TurnEvent stream (text + thinking + tool_use deltas;
signature_delta capture; message_delta stop_reason + usage)
Both MiniMax and Kimi now import from here; their internal/* dirs are
gone. Live verified after refactor — both still answer "ready" via
plexum say.
This commit is contained in:
72
README.md
Normal file
72
README.md
Normal file
@@ -0,0 +1,72 @@
|
||||
# Plexum-anthropic-compat-client
|
||||
|
||||
Shared HTTP+SSE Anthropic Messages API client + canonical ↔ Anthropic
|
||||
translator. Used by Plexum provider plugins that talk to any
|
||||
"Anthropic-compatible" endpoint (the one Anthropic itself, MiniMax,
|
||||
Kimi, Qwen, etc. all expose at `/v1/messages`).
|
||||
|
||||
Each provider plugin imports these two packages and adds:
|
||||
- its own `cmd/plexum-<name>-provider-plugin/main.go` entry
|
||||
- a list of supported model ids in the manifest
|
||||
- an endpoint URL + auth key + optional User-Agent override
|
||||
|
||||
## Packages
|
||||
|
||||
### `anthropic/`
|
||||
|
||||
Minimal HTTP+SSE client. Single entry point:
|
||||
|
||||
```go
|
||||
client := anthropic.New(baseURL, apiKey)
|
||||
client.UserAgent = "claude-code/0.1.0" // optional
|
||||
events, err := client.StreamMessages(ctx, anthropic.MessagesRequest{
|
||||
Model: "MiniMax-M2.7", MaxTokens: 4096,
|
||||
Messages: []anthropic.Message{...},
|
||||
})
|
||||
for ev := range events { /* anthropic.Event */ }
|
||||
```
|
||||
|
||||
Features:
|
||||
- POST `/v1/messages` with `stream: true`
|
||||
- Parses standard SSE frames (`event:` + `data:` + blank line)
|
||||
- Surfaces non-2xx as HTTP error (before channel opens)
|
||||
- Surfaces mid-stream errors as `Event{Type:"error"}` final entry
|
||||
- `UserAgent` + `ExtraHeaders` fields for per-backend customization
|
||||
- 5min per-call timeout
|
||||
|
||||
### `translate/`
|
||||
|
||||
Bidirectional canonical-types ↔ Anthropic-types adapter:
|
||||
|
||||
- `CanonicalToAnthropic(req canonical.TurnRequest, modelID, defaultMaxTokens) → anthropic.MessagesRequest`
|
||||
- `Translator` state machine consumes `<-chan anthropic.Event`,
|
||||
emits `[]canonical.TurnEvent` per event (handles text / thinking /
|
||||
tool_use / signature deltas + per-block close)
|
||||
|
||||
## Usage
|
||||
|
||||
In a provider plugin's `go.mod`:
|
||||
|
||||
```
|
||||
require git.hangman-lab.top/hzhang/Plexum-anthropic-compat-client v0.0.0
|
||||
replace git.hangman-lab.top/hzhang/Plexum-anthropic-compat-client => ../Plexum-anthropic-compat-client
|
||||
```
|
||||
|
||||
In code:
|
||||
|
||||
```go
|
||||
import (
|
||||
"git.hangman-lab.top/hzhang/Plexum-anthropic-compat-client/anthropic"
|
||||
"git.hangman-lab.top/hzhang/Plexum-anthropic-compat-client/translate"
|
||||
)
|
||||
```
|
||||
|
||||
## History
|
||||
|
||||
Extracted from `Plexum-minimax-provider` and `Plexum-kimi-provider` —
|
||||
both had a near-identical copy. New providers (Qwen, Doubao, …) drop in
|
||||
without re-implementing the protocol.
|
||||
|
||||
## License
|
||||
|
||||
Same as Plexum.
|
||||
Reference in New Issue
Block a user