Files
traefik-llmgateway/errors.go
T
lukaszraczyloandGitHub 9e886aef89 feat(messages): stream /v1/messages responses (#17)
A request with "stream": true on POST /v1/messages now gets the
Anthropic Messages event stream instead of a 400. Claude Code streams
every call, so the route must stream before it can front an
OpenAI-compatible cluster.

For openai and gemini providers, the gateway asks the upstream for a
stream with include_usage and translates each chunk as it arrives. It
emits message_start, one content block group per text or tool_use
block, then message_delta and message_stop. Tool call arguments arrive
as input_json_delta. The stop reason uses the non-streaming mapping.

For anthropic providers, the gateway relays each upstream event and
rewrites only message.model in message_start. An upstream error event
reaches the client once, as sent.

Billing uses the stream usage. An error before the stream starts gets
the normal JSON error. An error after the start ends the stream with an
error event. Streams are never cached. yaegi-check now drives a
streamed request under the interpreter.
2026-11-27 01:49:52 +00:00

69 lines
2.4 KiB
Go

package traefikllmgateway
import (
"encoding/json"
"net/http"
"runtime/debug"
"strconv"
)
// statusTrackingWriter wraps an http.ResponseWriter to record whether a
// response has already been committed (header written, or body bytes
// written under the net/http implicit-200 rule). recoverPanic uses this to
// avoid writing a second, conflicting response after a handler panics
// mid-stream. Flush delegates to the underlying http.Flusher when present —
// later tasks' SSE streaming depends on Flush passing through.
type statusTrackingWriter struct {
http.ResponseWriter
// sseError, when set, writes a mid-stream failure in the route's own SSE
// error shape instead of the OpenAI one.
sseError func(msg string)
wroteHeader bool
}
// WriteHeader records that the response was committed, then delegates.
func (w *statusTrackingWriter) WriteHeader(status int) {
w.wroteHeader = true
w.ResponseWriter.WriteHeader(status)
}
// Write records an implicit header commit if none happened yet, then
// delegates.
func (w *statusTrackingWriter) Write(b []byte) (int, error) {
w.wroteHeader = true
return w.ResponseWriter.Write(b)
}
// Flush delegates to the underlying ResponseWriter's Flush when it
// implements http.Flusher, and is a no-op otherwise.
func (w *statusTrackingWriter) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// writeOAIError writes an OpenAI-compatible error envelope:
// {"error":{"message":...,"type":...,"code":...}}. code is a JSON string —
// OpenAI-compatible SDKs expect string-or-null, not an integer.
func writeOAIError(w http.ResponseWriter, status int, errType, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{"message": msg, "type": errType, "code": strconv.Itoa(status)},
})
}
// recoverPanic recovers a panic from a handler and logs it. It writes a 500
// error envelope only if the response has not already been committed — a
// handler that panics after writing headers or body bytes (mid-stream) must
// not get a second, conflicting response appended.
func recoverPanic(w *statusTrackingWriter, g *Gateway) {
if rec := recover(); rec != nil {
g.errorf("panic recovered: %v\n%s", rec, debug.Stack())
if w.wroteHeader {
return
}
writeOAIError(w, http.StatusInternalServerError, "server_error", "internal error")
}
}