Files
traefik-llmgateway/messages_stream.go
T
lukaszraczyloandGitHub 009de03264 fix: close the run-3 security audit findings (#25)
* fix(admin): list the anonymous user in snapshot views

The auth snapshot built users from the key digest map only. The keyless
principal never appeared in usage, catalog, metrics or fleet views, and
its rejection counter was pruned on a users-file reload. The snapshot now
adds the anonymous principal once, with source anonymous.

* fix(discovery): keep context lengths when one upstream fails metadata

A failing modelBaseUrls upstream dropped its models from the metadata map.
The registry replaced the discovered contexts, so those models lost their
context length. The failure was also not visible. The adapter now keeps the
last good metadata per upstream and reports metadata failures through
upstreamErrors.

* fix(webui): show provider upstream errors

The dashboard ignored upstreamErrors from the admin overview. A provider
with a failing modelBaseUrls upstream looked healthy. The provider panel
now lists each upstream error, and the breaker rows mark such a provider
as degraded.

* fix(passthrough): gate and bill passthrough requests by cost budget

Refusal text stays under 128 bytes, and only models the provider serves get a per-model counter. When the first 64 KiB does not settle the model, the gate reads the body up to the smaller of maxRequestBytes and the passthrough cap, inside a body-admission slot. It refuses with 402 if the model is missing, repeated, or in a malformed or unterminated object, with 413 if the object does not close within the bound, and with 400 if the body read fails. A priced model after 64 KiB passes and the body reaches the upstream unchanged. A 2xx event stream with no completion usage bills one estimated token per data event.

* fix(billing): estimate completion when a stream ends before its usage

A streaming unified request that failed after output reached the client
recorded zero completion tokens. The gateway now estimates them from the
forwarded output bytes and marks the usage as estimated. Streams that
report usage, and streams that end cleanly, are unchanged.

* fix(cache): key /v1/messages entries on the forwarded anthropic-beta set

Two callers that sent different anthropic-beta values shared one cache
entry, although the gateway forwards that header upstream. The key now
includes the normalised beta set when the header is present. Requests
without the header keep their existing key.

* docs: state that /v1/messages responses are cached

The Caching section named chat and embeddings only. It now names
/v1/messages, the beta set in the key, and that entries are shared
across users and groups.

* fix(passthrough): bill Responses API and Gemini streaming usage shapes

Read input_tokens/output_tokens and input_tokens_details.cached_tokens
for OpenAI Responses JSON, and response.usage from response.completed
SSE events. Decode the Gemini streamGenerateContent JSON array and read
usageMetadata from Gemini SSE data events, last one wins.

Field names follow the OpenAI Responses API and Gemini
GenerateContentResponse references. A truncated array yields no usage.

* fix(admin): scrub the re-serialised base URL from provider errors

sanitizeProviderErr now also replaces the URL as net/url writes it.
A provider baseUrl with userinfo, a query or a fragment logs one
construction warning. The config still loads.

* fix(proxy): do not relay a redirect that carries a configured target query

proxyUpstream answers 502 when an upstream replies with a 3xx Location
and the upstream URL holds a query the client did not send. Targets
without a configured query still relay redirects.

* test(passthrough): rename the client reset reader helper

Two packages each declared failingReader in the same test package. The passthrough gate test now uses clientResetReader.

* fix(discovery): bound the number and length of discovered model ids

A provider keeps at most 10000 discovered ids of at most 256 bytes.
The registry drops empty ids, ids with control characters, longer ids
and the overflow, and logs the count once per 10 minutes.

* fix(mcp): keep the target query when it does not parse

mergeQueryStrings now forwards a configured query that ParseQuery
rejects, next to the client query, instead of dropping it.

* fix(messages): keep tool argument deltas on the open block

Blocks that follow an open tool_use block are queued and emitted at
stream end, so every delta targets the one open block.

* docs: correct failOpen, reload and anonymous access text

* chore(webui): regenerate admin assets

Rebuilt with make admin-ui for the dashboard changes in this branch.
2026-11-29 00:29:06 +00:00

525 lines
15 KiB
Go

package traefikllmgateway
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
// sseEventPrefix is the SSE "event:" field prefix.
const sseEventPrefix = "event: "
// anthropicStreamErrorType is the error.type of an in-stream Anthropic error event.
const anthropicStreamErrorType = "api_error"
const (
streamModeUnset = iota
streamModeSSE
streamModeJSON
)
// openAIStreamChunk is the subset of one OpenAI chat.completion.chunk the
// Anthropic translator reads. Null "content" and "finish_reason" decode to "".
type openAIStreamChunk struct {
ID string `json:"id"`
Error json.RawMessage `json:"error"`
Usage *chatStreamUsage `json:"usage"`
Choices []struct {
FinishReason string `json:"finish_reason"`
Delta struct {
Content string `json:"content"`
ToolCalls []struct {
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
ID string `json:"id"`
Index int `json:"index"`
} `json:"tool_calls"`
} `json:"delta"`
} `json:"choices"`
}
type streamToolBlock struct {
queued *queuedBlock
id string
block int
}
// queuedBlock is a block that arrived while a tool_use block was open. It is
// emitted at stream end so every delta targets the one open block.
type queuedBlock struct {
typ string
id string
name string
buf strings.Builder
}
// anthropicStreamWriter is the http.ResponseWriter handed to a provider
// adapter's chatCompletion on a streaming /v1/messages request. The adapter
// writes OpenAI chunk frames; this writer re-emits them as the Anthropic
// Messages event sequence, flushing per event. Reusing the adapter keeps
// upstream calls, retries, timeouts and usage capture identical across
// every non-anthropic provider type.
type anthropicStreamWriter struct {
rw http.ResponseWriter
tools map[int]*streamToolBlock
sse *sseWriter
model string
finishR string
queue []*queuedBlock
raw bytes.Buffer
queueCap int
queued int
mode int
next int
openIdx int
started bool
done bool
hasOpen bool
textOpen bool
toolOpen bool
}
func newAnthropicStreamWriter(rw http.ResponseWriter, model string) *anthropicStreamWriter {
return &anthropicStreamWriter{rw: rw, model: model, tools: map[int]*streamToolBlock{}, queueCap: maxResponseBytes}
}
// Header implements http.ResponseWriter.
func (s *anthropicStreamWriter) Header() http.Header { return s.rw.Header() }
// Flush implements http.Flusher.
func (s *anthropicStreamWriter) Flush() {
if f, ok := s.rw.(http.Flusher); ok {
f.Flush()
}
}
// detectMode runs on the first WriteHeader/Write: an upstream that ignored
// stream=true answers JSON, which finish replays as events.
func (s *anthropicStreamWriter) detectMode() {
if s.mode != streamModeUnset {
return
}
ct := strings.ToLower(s.rw.Header().Get("Content-Type"))
if ct != "" && !strings.Contains(ct, "text/event-stream") {
s.mode = streamModeJSON
return
}
s.mode = streamModeSSE
s.sse = newSSEWriter(s.rw)
}
// WriteHeader implements http.ResponseWriter.
func (s *anthropicStreamWriter) WriteHeader(status int) {
s.detectMode()
if s.mode == streamModeSSE {
s.rw.WriteHeader(status)
}
}
// Write implements http.ResponseWriter. Each call carries whole frames:
// sseWriter emits one frame per Write.
func (s *anthropicStreamWriter) Write(b []byte) (int, error) {
s.detectMode()
if s.mode == streamModeJSON {
return s.raw.Write(b)
}
for _, frame := range bytes.Split(b, []byte("\n\n")) {
data, ok := sseFrameData(frame)
if !ok || string(data) == sseDoneMarker {
continue
}
if err := s.handleChunk(data); err != nil {
return 0, err
}
}
return len(b), nil
}
// sseFrameData joins a frame's "data:" lines; ok is false for a frame with none.
func sseFrameData(frame []byte) (data []byte, ok bool) {
var parts [][]byte
for _, line := range bytes.Split(frame, []byte("\n")) {
if v, found := bytes.CutPrefix(line, []byte("data:")); found {
parts = append(parts, bytes.TrimPrefix(v, []byte(" ")))
}
}
if len(parts) == 0 {
return nil, false
}
return bytes.Join(parts, []byte("\n")), true
}
func (s *anthropicStreamWriter) emit(name string, payload map[string]any) error {
b, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("marshal %s event: %w", name, err)
}
return s.sse.writeEvent(name, b)
}
func (s *anthropicStreamWriter) start(id string, inputTokens int64) error {
if s.started {
return nil
}
s.started = true
return s.emit("message_start", map[string]any{
"type": "message_start",
"message": map[string]any{
"id": id,
"type": "message",
"role": "assistant",
"model": s.model,
"content": []any{},
"stop_reason": nil,
"stop_sequence": nil,
"usage": map[string]any{"input_tokens": inputTokens, "output_tokens": 0},
},
})
}
func (s *anthropicStreamWriter) openBlock(block map[string]any) error {
if err := s.closeBlock(); err != nil {
return err
}
s.hasOpen = true
s.openIdx = s.next
s.next++
return s.emit("content_block_start", map[string]any{"type": "content_block_start", "index": s.openIdx, "content_block": block})
}
func (s *anthropicStreamWriter) closeBlock() error {
if !s.hasOpen {
return nil
}
s.hasOpen = false
s.textOpen = false
s.toolOpen = false
return s.emit("content_block_stop", map[string]any{"type": "content_block_stop", "index": s.openIdx})
}
func (s *anthropicStreamWriter) delta(index int, delta map[string]any) error {
return s.emit("content_block_delta", map[string]any{"type": "content_block_delta", "index": index, "delta": delta})
}
func (s *anthropicStreamWriter) handleChunk(data []byte) error {
var c openAIStreamChunk
if err := json.Unmarshal(data, &c); err != nil {
return fmt.Errorf("%w: decode openai stream chunk: %w", errUpstream, err)
}
if e := bytes.TrimSpace(c.Error); len(e) > 0 && string(e) != "null" {
return fmt.Errorf("%w: upstream stream error: %s", errUpstream, e)
}
var input int64
if c.Usage != nil {
input = c.Usage.PromptTokens
}
if err := s.start(anthropicMessageIDFromOpenAI(c.ID), input); err != nil {
return err
}
if len(c.Choices) == 0 {
return nil
}
ch := c.Choices[0]
if ch.Delta.Content != "" {
if err := s.handleText(ch.Delta.Content); err != nil {
return err
}
}
for _, tc := range ch.Delta.ToolCalls {
if err := s.handleToolCall(tc.Index, tc.ID, tc.Function.Name, tc.Function.Arguments); err != nil {
return err
}
}
if ch.FinishReason != "" {
s.finishR = ch.FinishReason
}
return nil
}
func (s *anthropicStreamWriter) handleText(text string) error {
if s.toolOpen {
tail := len(s.queue) - 1
if tail < 0 || s.queue[tail].typ != "text" {
s.queue = append(s.queue, &queuedBlock{typ: "text"})
tail++
}
return s.appendQueued(s.queue[tail], text)
}
if !s.textOpen {
if err := s.openBlock(map[string]any{"type": "text", "text": ""}); err != nil {
return err
}
s.textOpen = true
}
return s.delta(s.openIdx, map[string]any{"type": "text_delta", "text": text})
}
func (s *anthropicStreamWriter) handleToolCall(index int, id, name, args string) error {
tb, known := s.tools[index]
if !known || (id != "" && tb.id != "" && id != tb.id) {
tb = &streamToolBlock{id: id}
if s.toolOpen {
q := &queuedBlock{typ: "tool_use", id: id, name: name}
if err := s.reserveQueue(len(id) + len(name)); err != nil {
return err
}
s.queue = append(s.queue, q)
tb.queued = q
} else {
err := s.openBlock(map[string]any{"type": "tool_use", "id": id, "name": name, "input": map[string]any{}})
if err != nil {
return err
}
s.toolOpen = true
tb.block = s.openIdx
}
s.tools[index] = tb
}
if args == "" {
return nil
}
if tb.queued != nil {
return s.appendQueued(tb.queued, args)
}
return s.delta(tb.block, map[string]any{"type": "input_json_delta", "partial_json": args})
}
func (s *anthropicStreamWriter) reserveQueue(n int) error {
s.queued += n
if s.queued > s.queueCap {
return fmt.Errorf("%w: queued content blocks exceed %d bytes", errUpstream, s.queueCap)
}
return nil
}
func (s *anthropicStreamWriter) appendQueued(q *queuedBlock, text string) error {
if err := s.reserveQueue(len(text)); err != nil {
return err
}
q.buf.WriteString(text)
return nil
}
// flushQueue emits the blocks held back behind a tool_use block, in arrival order.
func (s *anthropicStreamWriter) flushQueue() error {
queue := s.queue
s.queue = nil
for _, q := range queue {
block := map[string]any{"type": "text", "text": ""}
deltaType, field := "text_delta", "text"
if q.typ == "tool_use" {
block = map[string]any{"type": "tool_use", "id": q.id, "name": q.name, "input": map[string]any{}}
deltaType, field = "input_json_delta", "partial_json"
}
if err := s.openBlock(block); err != nil {
return err
}
if q.buf.Len() > 0 {
if err := s.delta(s.openIdx, map[string]any{"type": deltaType, field: q.buf.String()}); err != nil {
return err
}
}
if err := s.closeBlock(); err != nil {
return err
}
}
return nil
}
// finish closes the open block and writes message_delta and message_stop.
// u is the adapter's final usage. On a JSON-mode upstream it replays the
// buffered response as events. Idempotent.
func (s *anthropicStreamWriter) finish(u usage) error {
s.detectMode()
if s.done {
return nil
}
s.done = true
if s.mode == streamModeJSON {
return s.replayJSON()
}
if err := s.start("", 0); err != nil {
return err
}
return s.stop(anthropicStopReasonFromOpenAI(s.finishR), u.prompt, u.completion)
}
func (s *anthropicStreamWriter) stop(stopReason string, input, output int64) error {
if err := s.closeBlock(); err != nil {
return err
}
if err := s.flushQueue(); err != nil {
return err
}
err := s.emit("message_delta", map[string]any{
"type": "message_delta",
"delta": map[string]any{"stop_reason": stopReason, "stop_sequence": nil},
"usage": map[string]any{"input_tokens": input, "output_tokens": output},
})
if err != nil {
return err
}
return s.emit("message_stop", map[string]any{"type": "message_stop"})
}
// replayJSON turns a buffered non-streaming OpenAI response into the event sequence.
func (s *anthropicStreamWriter) replayJSON() error {
out, err := anthropicResponseFromOpenAI(s.raw.Bytes(), s.model)
if err != nil {
return &responseTranslationError{err: err}
}
s.sse = newSSEWriter(s.rw)
usageMap, _ := out["usage"].(map[string]any)
input, _ := usageMap["input_tokens"].(int64)
output, _ := usageMap["output_tokens"].(int64)
id, _ := out["id"].(string)
if err := s.start(id, input); err != nil {
return err
}
blocks, _ := out["content"].([]any)
for _, b := range blocks {
block, _ := b.(map[string]any)
if block["type"] == "text" {
text, _ := block["text"].(string)
if err := s.openBlock(map[string]any{"type": "text", "text": ""}); err != nil {
return err
}
if err := s.delta(s.openIdx, map[string]any{"type": "text_delta", "text": text}); err != nil {
return err
}
continue
}
args, mErr := json.Marshal(block["input"])
if mErr != nil {
return &responseTranslationError{err: mErr}
}
if err := s.openBlock(map[string]any{"type": "tool_use", "id": block["id"], "name": block["name"], "input": map[string]any{}}); err != nil {
return err
}
if err := s.delta(s.openIdx, map[string]any{"type": "input_json_delta", "partial_json": string(args)}); err != nil {
return err
}
}
stopReason, _ := out["stop_reason"].(string)
return s.stop(stopReason, input, output)
}
// writeAnthropicStreamError emits an Anthropic error event on an already-started stream.
func writeAnthropicStreamError(w http.ResponseWriter, msg string) {
b, err := json.Marshal(map[string]any{
"type": "error",
"error": map[string]any{"type": anthropicStreamErrorType, "message": msg},
})
if err != nil {
return
}
_ = newSSEWriter(w).writeEvent("error", b) // best-effort: the connection may already be gone
}
// rewriteMessageStartModel replaces message.model in a message_start
// payload, leaving every other byte untouched.
func rewriteMessageStartModel(data []byte, model string) ([]byte, error) {
dec := json.NewDecoder(bytes.NewReader(data))
if tok, err := dec.Token(); err != nil || tok != json.Delim('{') {
return nil, fmt.Errorf("message_start is not a JSON object")
}
for dec.More() {
keyTok, err := dec.Token()
if err != nil {
return nil, fmt.Errorf("read message_start key: %w", err)
}
var val json.RawMessage
if derr := dec.Decode(&val); derr != nil {
return nil, fmt.Errorf("read message_start value: %w", derr)
}
if keyTok != "message" {
continue
}
rewritten, err := rewriteModelField(val, model)
if err != nil {
return nil, err
}
end := int(dec.InputOffset())
start := end - len(val)
var out bytes.Buffer
out.Write(data[:start])
out.Write(rewritten)
out.Write(data[end:])
return out.Bytes(), nil
}
return nil, fmt.Errorf("message_start has no message object")
}
// errStreamErrorRelayed marks a stream whose upstream error event already
// reached the client, so the route must not write a second one.
var errStreamErrorRelayed = errors.New("upstream stream error relayed to client")
// relayAnthropicMessagesStream forwards an Anthropic Messages stream event by
// event. Only message_start changes: its message.model becomes the client's
// alias. Usage comes from the same parser the chat route uses for this
// provider type.
func relayAnthropicMessagesStream(w http.ResponseWriter, body io.Reader, model string) (usage, error) {
st := newAnthropicStreamState(model, 0)
sw := newSSEWriter(w)
relayed := false
err := readSSE(body, func(ev sseEvent) error {
data := ev.data
if ev.event == "message_start" {
out, rerr := rewriteMessageStartModel(data, model)
if rerr != nil {
return rerr
}
data = out
}
if werr := sw.writeEvent(ev.event, data); werr != nil {
return werr
}
relayed = ev.event == "error"
_, terr := st.translate(ev)
return terr
})
if err == nil {
return st.usage(), nil
}
if relayed {
return st.usageOnAbort(), fmt.Errorf("%w: %w", errStreamErrorRelayed, err)
}
return st.usageOnAbort(), fmt.Errorf("%w: stream: %w", errUpstream, err)
}
// writeEvent writes one frame with an "event:" line ahead of the data, the
// framing the Anthropic Messages stream uses. event must not contain a line
// terminator; an empty event writes a bare data frame. Every data line is
// split the way writeData splits it, so no raw CR or LF reaches the wire.
func (s *sseWriter) writeEvent(event string, b []byte) error {
var buf []byte
if event != "" {
buf = append(buf, sseEventPrefix...)
buf = append(buf, event...)
buf = append(buf, '\n')
}
for _, line := range splitSSELines(b) {
buf = append(buf, sseDataPrefix...)
buf = append(buf, line...)
buf = append(buf, '\n')
}
buf = append(buf, '\n')
if _, err := s.w.Write(buf); err != nil {
return err
}
if s.f != nil {
s.f.Flush()
}
return nil
}