mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
- opener_eaten (models.yaml, per route and fallback): content is reasoning</think>answer, so everything to the first </think> is reasoning; no </think> is the answer, whole. Every other route keeps the strict leading-<think> rule. - /v1/messages (HTTP and ZAP) strips the reasoning, then lifts: one order (readAnswer); the translator no longer strips. A cut upstream ends in an error event, not end_turn. - ZAP gateway frames: X-Hanzo-Strict refused as the HTTP twin refuses it; anthropic-version and anthropic-beta reach a native vendor. - markup: a repeated JSON name in any form is no call; a blockquote is never lifted from; prose after a coder, DSML or calls opener is let go inside 256 bytes. Co-authored-by: Hanzo Dev <[email protected]>
878 lines
28 KiB
Go
878 lines
28 KiB
Go
// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
openai "github.com/hanzoai/go-openai"
|
|
)
|
|
|
|
// ── Anthropic ⇄ OpenAI translation ───────────────────────────────────────────
|
|
//
|
|
// The Anthropic-compatible surface (/v1/messages) accepts requests carrying
|
|
// tools, tool_result blocks, images and thinking, and must return Anthropic SSE
|
|
// events — never raw OpenAI chunks. This file is the single, total translation
|
|
// between the two shapes, in BOTH directions:
|
|
//
|
|
// request : Anthropic messages/tools → OpenAI ChatCompletionRequest
|
|
// response : OpenAI (stream or JSON) → Anthropic SSE events / JSON
|
|
//
|
|
// It is pure (no controller/HTTP state): the streaming translator writes to an
|
|
// injected io.Writer. Every OpenAI chunk is converted — there is no passthrough.
|
|
|
|
// anthropicBlock is a fully-typed Anthropic content block. Unlike the display-
|
|
// only AnthropicContentBlock (text only), this captures every field we must
|
|
// translate: tool_use (id/name/input), tool_result (tool_use_id/content),
|
|
// image (source), and thinking (thinking/signature).
|
|
type anthropicBlock struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text,omitempty"`
|
|
|
|
// tool_use (assistant → OpenAI assistant.tool_calls)
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Input json.RawMessage `json:"input,omitempty"`
|
|
|
|
// tool_result (user → OpenAI role:"tool")
|
|
ToolUseID string `json:"tool_use_id,omitempty"`
|
|
Content json.RawMessage `json:"content,omitempty"`
|
|
IsError bool `json:"is_error,omitempty"`
|
|
|
|
// image (→ OpenAI image_url)
|
|
Source *anthropicImageSource `json:"source,omitempty"`
|
|
|
|
// thinking (extended thinking) — dropped when translating history to OpenAI
|
|
Thinking string `json:"thinking,omitempty"`
|
|
}
|
|
|
|
// anthropicImageSource is an Anthropic image block source: base64 or url.
|
|
type anthropicImageSource struct {
|
|
Type string `json:"type"` // "base64" | "url"
|
|
MediaType string `json:"media_type"` // e.g. "image/png"
|
|
Data string `json:"data"` // base64 payload (base64 source)
|
|
URL string `json:"url"` // url source
|
|
}
|
|
|
|
// parseAnthropicBlocks decodes a message's raw content into blocks. The Anthropic
|
|
// Messages API accepts content as either a plain string ("hi") or an array of
|
|
// blocks. Returns (blocks, isPlainString); a plain string yields one text block.
|
|
func parseAnthropicBlocks(raw json.RawMessage) ([]anthropicBlock, bool) {
|
|
if len(raw) == 0 {
|
|
return nil, false
|
|
}
|
|
var s string
|
|
if err := json.Unmarshal(raw, &s); err == nil {
|
|
return []anthropicBlock{{Type: "text", Text: s}}, true
|
|
}
|
|
var blocks []anthropicBlock
|
|
if err := json.Unmarshal(raw, &blocks); err == nil {
|
|
return blocks, false
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
// anthropicImageToDataURL renders an Anthropic image source as an OpenAI
|
|
// image_url value (a data: URI for base64, or the URL verbatim).
|
|
func anthropicImageToDataURL(s *anthropicImageSource) string {
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
switch s.Type {
|
|
case "base64":
|
|
if s.Data == "" {
|
|
return ""
|
|
}
|
|
mt := s.MediaType
|
|
if mt == "" {
|
|
mt = "image/png"
|
|
}
|
|
return "data:" + mt + ";base64," + s.Data
|
|
case "url":
|
|
return s.URL
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// toolResultText flattens an Anthropic tool_result content (string, or array of
|
|
// text/image blocks) into the plain string an OpenAI role:"tool" message wants.
|
|
func toolResultText(raw json.RawMessage) string {
|
|
if len(raw) == 0 {
|
|
return ""
|
|
}
|
|
var s string
|
|
if err := json.Unmarshal(raw, &s); err == nil {
|
|
return s
|
|
}
|
|
blocks, _ := parseAnthropicBlocks(raw)
|
|
var parts []string
|
|
for _, b := range blocks {
|
|
if b.Text != "" {
|
|
parts = append(parts, b.Text)
|
|
}
|
|
}
|
|
if len(parts) > 0 {
|
|
return strings.Join(parts, "\n")
|
|
}
|
|
// Unknown/structured content — pass through as JSON so the model sees it.
|
|
return string(raw)
|
|
}
|
|
|
|
// anthropicToOpenAIMessages converts a full Anthropic request (system + messages
|
|
// with text/image/tool_use/tool_result/thinking) into OpenAI chat messages. A
|
|
// user turn carrying tool_result blocks expands into one OpenAI role:"tool"
|
|
// message per result (kept ahead of any user text, per the OpenAI contract).
|
|
func anthropicToOpenAIMessages(req *AnthropicRequest) []openai.ChatCompletionMessage {
|
|
out := make([]openai.ChatCompletionMessage, 0, len(req.Messages)+1)
|
|
if sys := req.SystemText(); sys != "" {
|
|
out = append(out, openai.ChatCompletionMessage{Role: "system", Content: sys})
|
|
}
|
|
for _, m := range req.Messages {
|
|
blocks, isStr := parseAnthropicBlocks(m.Content)
|
|
if isStr {
|
|
out = append(out, openai.ChatCompletionMessage{Role: m.Role, Content: blocks[0].Text})
|
|
continue
|
|
}
|
|
if m.Role == "assistant" {
|
|
out = append(out, assistantBlocksToOpenAI(blocks))
|
|
continue
|
|
}
|
|
out = append(out, userBlocksToOpenAI(blocks)...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// assistantBlocksToOpenAI folds an assistant turn's thinking + text + tool_use
|
|
// blocks into one OpenAI assistant message. The thinking block maps to
|
|
// reasoning_content so the chain-of-thought round-trips: inside a tool-call loop
|
|
// DeepSeek V4 rejects an assistant turn that omits the prior reasoning_content
|
|
// (HTTP 400), and Kimi loses reasoning continuity. The upstream selects which
|
|
// parts it needs. See hip-00NN.
|
|
func assistantBlocksToOpenAI(blocks []anthropicBlock) openai.ChatCompletionMessage {
|
|
msg := openai.ChatCompletionMessage{Role: "assistant"}
|
|
var text, reasoning strings.Builder
|
|
for _, b := range blocks {
|
|
switch b.Type {
|
|
case "thinking":
|
|
reasoning.WriteString(b.Thinking)
|
|
case "text":
|
|
text.WriteString(b.Text)
|
|
case "tool_use":
|
|
args := strings.TrimSpace(string(b.Input))
|
|
if args == "" || args == "null" {
|
|
args = "{}"
|
|
}
|
|
msg.ToolCalls = append(msg.ToolCalls, openai.ToolCall{
|
|
ID: b.ID,
|
|
Type: openai.ToolTypeFunction,
|
|
Function: openai.FunctionCall{
|
|
Name: b.Name,
|
|
Arguments: args,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
msg.Content = text.String()
|
|
msg.ReasoningContent = reasoning.String() // omitempty: absent when no thinking
|
|
return msg
|
|
}
|
|
|
|
// userBlocksToOpenAI expands a user turn. tool_result blocks become standalone
|
|
// OpenAI role:"tool" messages (emitted first, so they immediately follow the
|
|
// prior assistant tool_calls); text/image blocks become a single user message
|
|
// (MultiContent when any image is present, plain Content otherwise).
|
|
func userBlocksToOpenAI(blocks []anthropicBlock) []openai.ChatCompletionMessage {
|
|
var toolMsgs []openai.ChatCompletionMessage
|
|
var parts []openai.ChatMessagePart
|
|
var text strings.Builder
|
|
hasImage := false
|
|
|
|
for _, b := range blocks {
|
|
switch b.Type {
|
|
case "tool_result":
|
|
toolMsgs = append(toolMsgs, openai.ChatCompletionMessage{
|
|
Role: "tool",
|
|
ToolCallID: b.ToolUseID,
|
|
Content: toolResultText(b.Content),
|
|
})
|
|
case "text":
|
|
text.WriteString(b.Text)
|
|
parts = append(parts, openai.ChatMessagePart{
|
|
Type: openai.ChatMessagePartTypeText,
|
|
Text: b.Text,
|
|
})
|
|
case "image":
|
|
if url := anthropicImageToDataURL(b.Source); url != "" {
|
|
hasImage = true
|
|
parts = append(parts, openai.ChatMessagePart{
|
|
Type: openai.ChatMessagePartTypeImageURL,
|
|
ImageURL: &openai.ChatMessageImageURL{URL: url},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
out := toolMsgs
|
|
switch {
|
|
case hasImage:
|
|
out = append(out, openai.ChatCompletionMessage{Role: "user", MultiContent: parts})
|
|
case text.Len() > 0:
|
|
out = append(out, openai.ChatCompletionMessage{Role: "user", Content: text.String()})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// anthropicToolsToOpenAI converts Anthropic tool definitions to OpenAI function
|
|
// tools. An absent/empty input_schema becomes an empty object schema.
|
|
func anthropicToolsToOpenAI(tools []AnthropicTool) []openai.Tool {
|
|
if len(tools) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]openai.Tool, 0, len(tools))
|
|
for _, t := range tools {
|
|
var params any
|
|
if len(t.InputSchema) > 0 {
|
|
_ = json.Unmarshal(t.InputSchema, ¶ms)
|
|
}
|
|
if params == nil {
|
|
params = map[string]any{"type": "object", "properties": map[string]any{}}
|
|
}
|
|
out = append(out, openai.Tool{
|
|
Type: openai.ToolTypeFunction,
|
|
Function: &openai.FunctionDefinition{
|
|
Name: t.Name,
|
|
Description: t.Description,
|
|
Parameters: params,
|
|
},
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// anthropicToolChoiceToOpenAI maps an Anthropic tool_choice to the OpenAI form:
|
|
// auto→"auto", any→"required", none→"none", tool→{type:function,function:{name}}.
|
|
func anthropicToolChoiceToOpenAI(raw json.RawMessage) any {
|
|
if len(raw) == 0 {
|
|
return nil
|
|
}
|
|
var tc struct {
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.Unmarshal(raw, &tc); err != nil {
|
|
return nil
|
|
}
|
|
switch tc.Type {
|
|
case "auto":
|
|
return "auto"
|
|
case "any":
|
|
return "required"
|
|
case "none":
|
|
return "none"
|
|
case "tool":
|
|
if tc.Name != "" {
|
|
return openai.ToolChoice{
|
|
Type: openai.ToolTypeFunction,
|
|
Function: openai.ToolFunction{Name: tc.Name},
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// anthropicThinkingToReasoningEffort is the ONE Anthropic→upstream adapter for
|
|
// the extended-thinking config. Anthropic models thinking as a token BUDGET
|
|
// ({"type":"enabled","budget_tokens":N}); every upstream reasons by a coarse
|
|
// EFFORT ordinal — but the ordinals DIFFER by provider, so the functor is
|
|
// parameterized by the target vocabulary (the codomain category). The shapes
|
|
// are not isomorphic, so this is a lossy, monotone collapse: deeper budget →
|
|
// harder effort. "" means "don't set reasoning_effort" — disabled or absent
|
|
// thinking leaves the target at its native default, and every vocabulary below
|
|
// reasons by default, so omitting the field is never a correctness break, only a
|
|
// lost budget hint. Targets that ignore the field are unaffected.
|
|
//
|
|
// Two vocabularies, each the exact value set its targets accept:
|
|
// - "glm": "max"|"high". Emitting "low"/"medium" here would be REJECTED
|
|
// upstream, so this vocabulary must never produce them.
|
|
// - "openai": "low"|"medium"|"high".
|
|
//
|
|
// Which vocabulary a SKU takes is a property of the catalog (HIP-0039), resolved
|
|
// at the call site rather than listed here. A target whose thinking control is an
|
|
// OBJECT rather than a reasoning_effort string is served by neither vocabulary:
|
|
// the fork's ChatCompletionRequest carries no such field, so it keeps its native
|
|
// default and reasons on. Carrying the object form is a follow-on.
|
|
func anthropicThinkingToReasoningEffort(raw json.RawMessage, vocab string) string {
|
|
if len(raw) == 0 {
|
|
return ""
|
|
}
|
|
var t struct {
|
|
Type string `json:"type"`
|
|
BudgetTokens int `json:"budget_tokens"`
|
|
}
|
|
if err := json.Unmarshal(raw, &t); err != nil {
|
|
return ""
|
|
}
|
|
if t.Type != "enabled" || t.BudgetTokens <= 0 {
|
|
return ""
|
|
}
|
|
// deep = the heaviest CC request (ultrathink ~32k); light = "think" (~4k).
|
|
deep := t.BudgetTokens >= 16384
|
|
switch vocab {
|
|
case "openai":
|
|
// OpenAI o-series: three tiers. Medium collapses "think hard" (~10k).
|
|
if t.BudgetTokens < 4096 {
|
|
return "low"
|
|
}
|
|
if !deep {
|
|
return "medium"
|
|
}
|
|
return "high"
|
|
case "glm":
|
|
// GLM-5.* and DeepSeek V4: two tiers only. Deep→max, else→high.
|
|
if deep {
|
|
return "max"
|
|
}
|
|
return "high"
|
|
default:
|
|
// Upstreams that take no reasoning_effort string — qwen (native
|
|
// enable_thinking), kimi (native thinking.type), and models with no
|
|
// reasoning control. Forward none; leave the upstream at its default.
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// thinkingVocabularyForUpstream returns the reasoning-effort vocabulary the
|
|
// given upstream model accepts, keyed by the upstream model id (the ONE place
|
|
// this mapping lives). Only families that take an OpenAI-style reasoning_effort
|
|
// string are named — openai (low|medium|high) and glm (max|high, which
|
|
// GLM-5.* and DeepSeek V4 share). qwen (native enable_thinking), kimi (native
|
|
// thinking.type), and models with no reasoning control return "" so the
|
|
// adapter forwards no reasoning_effort. Mirrors zen's reasoningVocab; see
|
|
// hip-00NN.
|
|
func thinkingVocabularyForUpstream(upstream string) string {
|
|
m := strings.ToLower(upstream)
|
|
switch {
|
|
case strings.HasPrefix(m, "gpt-") || strings.HasPrefix(m, "openai-gpt") ||
|
|
strings.HasPrefix(m, "o1") || strings.HasPrefix(m, "o3") || strings.HasPrefix(m, "o4"):
|
|
return "openai"
|
|
case strings.HasPrefix(m, "glm-5") || strings.HasPrefix(m, "deepseek"):
|
|
return "glm"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// mapFinishReason maps an OpenAI finish_reason to an Anthropic stop_reason.
|
|
// tool_calls → tool_use is load-bearing: Claude Code reads it to know it must
|
|
// run a tool and continue the loop.
|
|
func mapFinishReason(fr string) string {
|
|
switch fr {
|
|
case "tool_calls", "function_call":
|
|
return "tool_use"
|
|
case "length":
|
|
return "max_tokens"
|
|
case "content_filter":
|
|
return "end_turn"
|
|
case "stop":
|
|
return "end_turn"
|
|
default:
|
|
return "end_turn"
|
|
}
|
|
}
|
|
|
|
// ── OpenAI streaming chunk (wire subset) ─────────────────────────────────────
|
|
|
|
type openaiStreamChunk struct {
|
|
ID string `json:"id"`
|
|
Model string `json:"model"`
|
|
Choices []struct {
|
|
Delta struct {
|
|
Content string `json:"content"`
|
|
ReasoningContent string `json:"reasoning_content"`
|
|
ToolCalls []openaiStreamToolCall `json:"tool_calls"`
|
|
} `json:"delta"`
|
|
FinishReason string `json:"finish_reason"`
|
|
} `json:"choices"`
|
|
Usage *chatUsage `json:"usage"`
|
|
}
|
|
|
|
type openaiStreamToolCall struct {
|
|
Index *int `json:"index"`
|
|
ID string `json:"id"`
|
|
Function struct {
|
|
Name string `json:"name"`
|
|
Arguments string `json:"arguments"`
|
|
} `json:"function"`
|
|
}
|
|
|
|
// ── Anthropic SSE stream translator ──────────────────────────────────────────
|
|
|
|
// anthropicStreamTranslator converts an upstream OpenAI SSE stream into Anthropic
|
|
// Messages API SSE events, tracking a single content-block index space shared by
|
|
// interleaved thinking, text, and tool_use blocks. It also captures token usage
|
|
// (from the forced include_usage chunk) for billing.
|
|
type anthropicStreamTranslator struct {
|
|
emit func(event string, data any) error
|
|
modelID string
|
|
reqID string
|
|
|
|
started bool // message_start emitted
|
|
blockOpen bool // a content block is currently open
|
|
blockKind string // "thinking" | "text" | "tool"
|
|
nextIndex int // next content-block index to assign
|
|
curIndex int // index of the currently-open block
|
|
|
|
stopReason string
|
|
|
|
prompt, completion, total int
|
|
}
|
|
|
|
func newAnthropicStreamTranslator(emit func(string, any) error, modelID, reqID string) *anthropicStreamTranslator {
|
|
t := &anthropicStreamTranslator{emit: emit, modelID: modelID, reqID: reqID, stopReason: "end_turn"}
|
|
return t
|
|
}
|
|
|
|
// ensureStarted emits message_start exactly once.
|
|
func (t *anthropicStreamTranslator) ensureStarted() error {
|
|
if t.started {
|
|
return nil
|
|
}
|
|
t.started = true
|
|
return t.emit("message_start", map[string]any{
|
|
"type": "message_start",
|
|
"message": map[string]any{
|
|
"id": "msg_" + t.reqID,
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"content": []any{},
|
|
"model": t.modelID,
|
|
"stop_reason": nil,
|
|
"stop_sequence": nil,
|
|
"usage": map[string]any{"input_tokens": 0, "output_tokens": 0},
|
|
},
|
|
})
|
|
}
|
|
|
|
// closeBlock emits content_block_stop for the open block, if any.
|
|
func (t *anthropicStreamTranslator) closeBlock() error {
|
|
if !t.blockOpen {
|
|
return nil
|
|
}
|
|
t.blockOpen = false
|
|
kind := t.blockKind
|
|
t.blockKind = ""
|
|
_ = kind
|
|
return t.emit("content_block_stop", map[string]any{
|
|
"type": "content_block_stop",
|
|
"index": t.curIndex,
|
|
})
|
|
}
|
|
|
|
// openBlock closes any open block, then opens a new one of the given kind at the
|
|
// next index and emits its content_block_start.
|
|
func (t *anthropicStreamTranslator) openBlock(kind string, contentBlock map[string]any) error {
|
|
if err := t.closeBlock(); err != nil {
|
|
return err
|
|
}
|
|
t.curIndex = t.nextIndex
|
|
t.nextIndex++
|
|
t.blockOpen = true
|
|
t.blockKind = kind
|
|
return t.emit("content_block_start", map[string]any{
|
|
"type": "content_block_start",
|
|
"index": t.curIndex,
|
|
"content_block": contentBlock,
|
|
})
|
|
}
|
|
|
|
// ensureKind makes sure a block of the given kind (thinking or text) is open,
|
|
// opening one if the current block is a different kind or none is open.
|
|
func (t *anthropicStreamTranslator) ensureKind(kind string) error {
|
|
if t.blockOpen && t.blockKind == kind {
|
|
return nil
|
|
}
|
|
switch kind {
|
|
case "thinking":
|
|
return t.openBlock("thinking", map[string]any{"type": "thinking", "thinking": ""})
|
|
default: // text
|
|
return t.openBlock("text", map[string]any{"type": "text", "text": ""})
|
|
}
|
|
}
|
|
|
|
// text emits one text delta, opening the text block if it is not the open one.
|
|
func (t *anthropicStreamTranslator) text(text string) error {
|
|
if text == "" {
|
|
return nil
|
|
}
|
|
if err := t.ensureKind("text"); err != nil {
|
|
return err
|
|
}
|
|
return t.emit("content_block_delta", map[string]any{
|
|
"type": "content_block_delta",
|
|
"index": t.curIndex,
|
|
"delta": map[string]any{"type": "text_delta", "text": text},
|
|
})
|
|
}
|
|
|
|
// handleChunk translates one OpenAI chunk into Anthropic SSE events.
|
|
func (t *anthropicStreamTranslator) handleChunk(c *openaiStreamChunk) error {
|
|
if err := t.ensureStarted(); err != nil {
|
|
return err
|
|
}
|
|
if c.Usage != nil {
|
|
if c.Usage.PromptTokens > 0 {
|
|
t.prompt = c.Usage.PromptTokens
|
|
}
|
|
if c.Usage.CompletionTokens > 0 {
|
|
t.completion = c.Usage.CompletionTokens
|
|
}
|
|
if c.Usage.TotalTokens > 0 {
|
|
t.total = c.Usage.TotalTokens
|
|
}
|
|
}
|
|
for i := range c.Choices {
|
|
ch := &c.Choices[i]
|
|
|
|
// reasoning_content → thinking block (never leak the raw OpenAI field).
|
|
if ch.Delta.ReasoningContent != "" {
|
|
if err := t.ensureKind("thinking"); err != nil {
|
|
return err
|
|
}
|
|
if err := t.emit("content_block_delta", map[string]any{
|
|
"type": "content_block_delta",
|
|
"index": t.curIndex,
|
|
"delta": map[string]any{"type": "thinking_delta", "thinking": ch.Delta.ReasoningContent},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// content → text block. The stream arrives with its inline reasoning
|
|
// already removed (answerStream).
|
|
if ch.Delta.Content != "" {
|
|
if err := t.text(ch.Delta.Content); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// tool_calls → tool_use blocks + input_json_delta.
|
|
for j := range ch.Delta.ToolCalls {
|
|
tc := &ch.Delta.ToolCalls[j]
|
|
// A new tool call is signalled by an id and/or a function name.
|
|
if tc.ID != "" || tc.Function.Name != "" {
|
|
if err := t.openBlock("tool", map[string]any{
|
|
"type": "tool_use",
|
|
"id": tc.ID,
|
|
"name": tc.Function.Name,
|
|
"input": map[string]any{},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if tc.Function.Arguments != "" {
|
|
// Arguments stream as a JSON fragment; if no tool block is open
|
|
// (defensive), open an unnamed one so we never drop input.
|
|
if !t.blockOpen || t.blockKind != "tool" {
|
|
if err := t.openBlock("tool", map[string]any{
|
|
"type": "tool_use", "id": tc.ID, "name": tc.Function.Name, "input": map[string]any{},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := t.emit("content_block_delta", map[string]any{
|
|
"type": "content_block_delta",
|
|
"index": t.curIndex,
|
|
"delta": map[string]any{"type": "input_json_delta", "partial_json": tc.Function.Arguments},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
if ch.FinishReason != "" {
|
|
t.stopReason = mapFinishReason(ch.FinishReason)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// finish closes the open block and emits message_delta + message_stop.
|
|
func (t *anthropicStreamTranslator) finish() error {
|
|
if err := t.ensureStarted(); err != nil {
|
|
return err
|
|
}
|
|
if err := t.closeBlock(); err != nil {
|
|
return err
|
|
}
|
|
if err := t.emit("message_delta", map[string]any{
|
|
"type": "message_delta",
|
|
"delta": map[string]any{"stop_reason": t.stopReason, "stop_sequence": nil},
|
|
"usage": map[string]any{"input_tokens": t.prompt, "output_tokens": t.completion},
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return t.emit("message_stop", map[string]any{"type": "message_stop"})
|
|
}
|
|
|
|
// cut ends a stream the upstream broke off: what was relayed is closed, and an
|
|
// error event follows in place of message_stop, which an agent reads as a turn done.
|
|
func (t *anthropicStreamTranslator) cut() error {
|
|
if err := t.ensureStarted(); err != nil {
|
|
return err
|
|
}
|
|
if err := t.closeBlock(); err != nil {
|
|
return err
|
|
}
|
|
return t.emit("error", map[string]any{"type": "error",
|
|
"error": map[string]any{"type": "api_error", "message": "the upstream stream ended before the answer finished"}})
|
|
}
|
|
|
|
// sseLineMax is the longest SSE line read whole. A usage event is a few hundred
|
|
// bytes; a longer line is a document inside a tool result, relayed as it arrives
|
|
// and never parsed.
|
|
const sseLineMax = 1 << 20
|
|
|
|
// sseLines reads an SSE stream to its end or its first read error. whole gets each
|
|
// line no longer than sseLineMax, its line ending removed; part gets each piece of
|
|
// a longer one as it arrives — first on its first piece, last on the one that ends
|
|
// it, which has its line ending removed. No line of any length stops the stream.
|
|
func sseLines(r io.Reader, whole func(line string), part func(piece []byte, first, last bool)) {
|
|
br := bufio.NewReaderSize(r, sseLineMax)
|
|
long := false
|
|
for {
|
|
chunk, err := br.ReadSlice('\n')
|
|
more := err == bufio.ErrBufferFull
|
|
switch {
|
|
case len(chunk) == 0:
|
|
case long || more:
|
|
if !more {
|
|
chunk = bytes.TrimSuffix(bytes.TrimSuffix(chunk, []byte("\n")), []byte("\r"))
|
|
}
|
|
part(chunk, !long, !more)
|
|
long = more
|
|
default:
|
|
whole(strings.TrimSuffix(strings.TrimSuffix(string(chunk), "\n"), "\r"))
|
|
}
|
|
if err != nil && !more {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// messagesEvent is one event of a native Messages stream, as far as the relays read
|
|
// it.
|
|
type messagesEvent struct {
|
|
Type string `json:"type"`
|
|
Index int `json:"index"`
|
|
Message struct {
|
|
Usage AnthropicUsage `json:"usage"`
|
|
} `json:"message"`
|
|
Usage *AnthropicUsage `json:"usage"`
|
|
ContentBlock struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
} `json:"content_block"`
|
|
Delta struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
PartialJSON string `json:"partial_json"`
|
|
Thinking string `json:"thinking"`
|
|
StopReason string `json:"stop_reason"`
|
|
} `json:"delta"`
|
|
Error struct {
|
|
Type string `json:"type"`
|
|
Message string `json:"message"`
|
|
} `json:"error"`
|
|
}
|
|
|
|
// read parses the event on an SSE line, reporting false for a line that carries
|
|
// none, and takes its usage into used. The final count is message_delta's.
|
|
func (e *messagesEvent) read(line string, used *AnthropicUsage, final *bool) bool {
|
|
data, ok := strings.CutPrefix(line, "data:")
|
|
if !ok || json.Unmarshal([]byte(strings.TrimSpace(data)), e) != nil {
|
|
return false
|
|
}
|
|
used.merge(e.Message.Usage)
|
|
if e.Usage != nil {
|
|
used.merge(*e.Usage)
|
|
*final = *final || e.Type == "message_delta"
|
|
}
|
|
return true
|
|
}
|
|
|
|
// streamCaptureAnthropicUsage copies a NATIVE Anthropic SSE stream to w line by line
|
|
// while capturing token usage from message_start (the input side and both cache
|
|
// counts) and message_delta (the output side, and the iterations when compaction
|
|
// ran). final reports that message_delta's count arrived: a stream that ends before
|
|
// it — cut by the upstream, an idle deadline or an error event — was generated and
|
|
// invoiced up to a point this relay never learned.
|
|
//
|
|
// It reads to the upstream's end whatever happens to the client, because the usage
|
|
// is at the end and the vendor bills the answer either way. An SSE comment is a
|
|
// vendor keeping its connection open in its own words: the keep-alive goes on, the
|
|
// words do not.
|
|
func streamCaptureAnthropicUsage(r io.Reader, w io.Writer, flush func()) (used AnthropicUsage, final bool) {
|
|
comment := false
|
|
sseLines(r, func(line string) {
|
|
if strings.HasPrefix(line, ":") {
|
|
line = ":"
|
|
}
|
|
var ev messagesEvent
|
|
ev.read(line, &used, &final)
|
|
_, _ = fmt.Fprintf(w, "%s\n", line)
|
|
flush()
|
|
}, func(piece []byte, first, last bool) {
|
|
if first {
|
|
comment = piece[0] == ':'
|
|
if comment {
|
|
_, _ = io.WriteString(w, ":")
|
|
}
|
|
}
|
|
if !comment {
|
|
_, _ = w.Write(piece)
|
|
}
|
|
if last {
|
|
_, _ = io.WriteString(w, "\n")
|
|
}
|
|
flush()
|
|
})
|
|
return used, final
|
|
}
|
|
|
|
// translateOpenAIStream reads an upstream OpenAI SSE stream and drives the
|
|
// translator, returning captured usage. It never writes raw OpenAI frames.
|
|
func translateOpenAIStream(r io.Reader, emit func(string, any) error, modelID, reqID string) (prompt, completion, total int) {
|
|
t := newAnthropicStreamTranslator(emit, modelID, reqID)
|
|
scanner := bufio.NewScanner(r)
|
|
scanner.Buffer(make([]byte, 0, 256*1024), 1024*1024)
|
|
// done is the answer saying it is done: a finish_reason or [DONE]. A stream that
|
|
// ends without one was cut, and a coding agent must not read a cut as finished.
|
|
var done, gone bool
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
if !strings.HasPrefix(line, "data:") {
|
|
continue
|
|
}
|
|
raw := strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
|
if raw == "[DONE]" {
|
|
done = true
|
|
}
|
|
if raw == "" || raw == "[DONE]" {
|
|
continue
|
|
}
|
|
var chunk openaiStreamChunk
|
|
if err := json.Unmarshal([]byte(raw), &chunk); err != nil {
|
|
continue
|
|
}
|
|
for _, ch := range chunk.Choices {
|
|
done = done || ch.FinishReason != ""
|
|
}
|
|
if err := t.handleChunk(&chunk); err != nil {
|
|
gone = true
|
|
break
|
|
}
|
|
}
|
|
if done || gone {
|
|
_ = t.finish()
|
|
} else {
|
|
_ = t.cut()
|
|
}
|
|
return t.prompt, t.completion, t.total
|
|
}
|
|
|
|
// ── OpenAI non-streaming response → Anthropic JSON ───────────────────────────
|
|
|
|
// openAIResponseToAnthropic converts a non-streaming OpenAI chat completion into
|
|
// an Anthropic Messages response (content blocks + stop_reason + usage).
|
|
func openAIResponseToAnthropic(body []byte, modelID, reqID string) (resp map[string]any, prompt, completion int) {
|
|
var oai struct {
|
|
Choices []struct {
|
|
Message struct {
|
|
Content string `json:"content"`
|
|
ReasoningContent string `json:"reasoning_content"`
|
|
ToolCalls []struct {
|
|
ID string `json:"id"`
|
|
Function struct {
|
|
Name string `json:"name"`
|
|
Arguments string `json:"arguments"`
|
|
} `json:"function"`
|
|
} `json:"tool_calls"`
|
|
} `json:"message"`
|
|
FinishReason string `json:"finish_reason"`
|
|
} `json:"choices"`
|
|
Usage struct {
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
} `json:"usage"`
|
|
}
|
|
_ = json.Unmarshal(body, &oai)
|
|
|
|
content := []any{}
|
|
stopReason := "end_turn"
|
|
if len(oai.Choices) > 0 {
|
|
ch := oai.Choices[0]
|
|
if ch.Message.ReasoningContent != "" {
|
|
content = append(content, map[string]any{"type": "thinking", "thinking": ch.Message.ReasoningContent})
|
|
}
|
|
// The body arrives with its inline reasoning already removed (answerBody).
|
|
text := ch.Message.Content
|
|
if text != "" {
|
|
content = append(content, map[string]any{"type": "text", "text": text})
|
|
}
|
|
for _, tc := range ch.Message.ToolCalls {
|
|
var input any
|
|
if err := json.Unmarshal([]byte(tc.Function.Arguments), &input); err != nil || input == nil {
|
|
input = map[string]any{}
|
|
}
|
|
content = append(content, map[string]any{
|
|
"type": "tool_use",
|
|
"id": tc.ID,
|
|
"name": tc.Function.Name,
|
|
"input": input,
|
|
})
|
|
}
|
|
stopReason = mapFinishReason(ch.FinishReason)
|
|
}
|
|
// A tool_use content block always means stop_reason tool_use, even when the
|
|
// upstream omitted finish_reason.
|
|
for _, b := range content {
|
|
if m, ok := b.(map[string]any); ok && m["type"] == "tool_use" {
|
|
stopReason = "tool_use"
|
|
}
|
|
}
|
|
|
|
resp = map[string]any{
|
|
"id": "msg_" + reqID,
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"model": modelID,
|
|
"content": content,
|
|
"stop_reason": stopReason,
|
|
"stop_sequence": nil,
|
|
"usage": map[string]any{
|
|
"input_tokens": oai.Usage.PromptTokens,
|
|
"output_tokens": oai.Usage.CompletionTokens,
|
|
},
|
|
}
|
|
return resp, oai.Usage.PromptTokens, oai.Usage.CompletionTokens
|
|
}
|