Files
opantoantro/docs/wiki/AnthropicHandler.md

2.3 KiB
Raw Permalink Blame History

AnthropicHandler

Özet: /anthropic/* route'unu karşılar (handlers/anthropic.go:75). Anthropic Messages API formatını OpenAI Chat Completions formatına çevirir (Bifrost), upstream'e iletir ve yanıtı geri Anthropic formatına dönüştürür.

Kütüphaneler: Fiber v3, GORM, Go encoding/json

Bağlantılar: Main, BifrostTransform, ProxyEngine, RequestLog, Config, Anthropic Flow

Davranış

  1. GET /v1/modelsAnthropicHandler#modelsList
  2. HEAD → 404 (DeepSeek uyumluluğu)
  3. Empty body → varsayılan "optoant gateway ready" yanıtı
  4. x-api-keyAuthorization: Bearer dönüşümü (Claude Code uyumluluğu)
  5. Gerekirse OPENAI_KEY'den auto-inject
  6. BifrostTransform#AnthropicToBifrost ile dönüşüm
  7. Client stream: true gönderdiyse ve STREAMING=true ise → #handleStreaming
  8. ProxyEngine ile OPENAI_BACKEND/v1/chat/completions'a forward
  9. Başarılıysa (200): BifrostTransform#BifrostToAnthropic → Anthropic format
  10. Hata durumunda: raw passthrough
  11. DB log (fire-and-forget)

Endpoint

POST /anthropic/v1/messages
x-api-key: <api-key>
# veya
Authorization: Bearer <api-key>

Modeller (modelsList())

ID Display Name
deepseek-v4-flash DeepSeek V4 Flash
deepseek-v4-pro DeepSeek V4 Pro

Ek Fonksiyonlar

Fonksiyon Açıklama
infoPage() GET /anthropic (boş path) için HTML bilgi sayfası
modelsList() GET /anthropic/v1/models için model listesi

Streaming (handleStreaming())

STREAMING=true ve client stream: true gönderdiğinde SSE streaming devreye girer.

  • bufio.Scanner buffer: 4MB (varsayılan 64KB yerine) — Claude Code tool çağrılarında büyük chunk'lar için
  • Scanner hatası (bufio.ErrTooLong vb.) loglanır ve stream sonlandırılır
  • Write/Flush hataları loglanır

Edge Case'ler

  • Geçersiz Anthropic formatı → uyarı log'u + raw passthrough
  • Upstream hatası502 Bad Gateway
  • Yanıt dönüşüm hatası → raw yanıtı forward et
  • STREAMING=false → client stream: true isteği görmezden gelinir, non-streaming yanıt döner
  • Scanner buffer overflow → [ANTHROPIC] Stream scan error log'u, stream durur