monater (Discord, #general): an OpenAI-format custom provider answered
gpt-6.1-sol with HTTP 200 carrying "Rate limit reached for gpt-6.1-sol
... on tokens per min (TPM) ... Please try again in 6ms." failure(200, …)
skipped every 429 case and fell to `status < 500 && usedUpWords`
("limit reached"), so the try failed as quota and rested 15 minutes,
shown as "200 quota used up". The same text as a real 429 has been a
rate limit since v0.1.1748.
One classifier: carriedStatus maps a 2xx/3xx body through streamStatus,
the reading already used for an error inside a stream, in failure,
failureOf and restAfterMarked. Words streamStatus doesn't know (its 502)
keep the 200 and read as before. insufficient_quota maps to 402 and
stays credit, as at 429.
Siblings:
- non-streamed 200 whose body is only an error (OpenAI/Gemini
{"error":…}, Anthropic type:error, a failed Responses object):
errorReply, read in holdWriter.settle, fails the try with that status,
so it is retried or handed on instead of going to the agent as an
answer; unheld, the passthrough sniffer books it failed (settleWhole),
so the Requests list shows it red with its words.
- translated route (an Anthropic/Responses/Gemini client to a chat
provider, plugins included): an error event with no status went out as
502 and failed as "other"; it now takes streamStatus's status.
- Streamed passthrough with content then an error (the reporter's path)
is covered by the failureOf/restAfter change.
Not changed: Codex's ChatGPT backend (always streams; its error events
already go through the same failure()), Claude Code's bridgeFailed and
tool_loop's relayStatus (own statuses).
The Requests list needed no change: a row with err is shown failed
(ledFailed_), "200 · <err_type>" with the reason as its tooltip.
Tests (carried_error_test.go, the reporter's literal text): failure and
failureOf at 200 equal 429's for it and for minuteLimitBodies,
restAfter at 200 is a rate limit's minute at 429, errorReply, the
sniffer, and end to end: a chat 200 error body and a Responses failed
object retried as rate and answered, a chat stream with content then
the error resting as rate at 429, and an Anthropic request translated
from a chat stream failing as rate.
Broken by hand: carriedStatus returning the status unchanged turns
the unit, rest and stream tests red (quota, 14m59s, Status 200: the
reported behaviour); errorReply off in settle turns both retried-body
tests red (the error went to the agent as a 200); translate back to
502 turns the translated test red ("other"); settleWhole a no-op
turns the sniffer test red.
Run (mote): go test -tags nogui -timeout 45m ./... at d8c70504d plus
this change: EXIT 0, 76 packages ok, internal/gateway 660s. Rebased on
origin/main (no gateway change in between); the new tests and gofmt -l
again at the pushed head. GOOS=linux and GOOS=windows go vet
./internal/gateway ok.
Not tried: a real relay answering 200 with this error; no GUI test run
(no GUI change).
296 lines
7.5 KiB
Go
296 lines
7.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// usageSniffer picks the usage block out of a provider reply that is being
|
|
// copied to the client untouched. Streams are read line by line as they
|
|
// pass; a plain JSON body is kept and parsed at the end.
|
|
type usageSniffer struct {
|
|
proto provider.Protocol
|
|
sse bool
|
|
buf []byte
|
|
data []byte // SSE data lines of the event in progress
|
|
over bool // the JSON body outgrew the cap; give up on it
|
|
u Usage
|
|
// served: the model the reply says answered it, the last it named
|
|
served string
|
|
// ended: the stream's last event went by (message_stop, [DONE],
|
|
// response.completed …) or an error that ends it
|
|
ended bool
|
|
// failed: what an explicit error event said; a terminal event can end
|
|
// the stream successfully or with a failure despite its HTTP 200.
|
|
failed string
|
|
// tail: what a Chat stream's chunks say of its end (chatTail)
|
|
tail chatTail
|
|
}
|
|
|
|
func newSniffer(proto provider.Protocol, contentType string) *usageSniffer {
|
|
return &usageSniffer{proto: proto, sse: strings.HasPrefix(contentType, "text/event-stream")}
|
|
}
|
|
|
|
func (s *usageSniffer) write(b []byte) {
|
|
if !s.sse {
|
|
if len(s.buf)+len(b) > 8<<20 {
|
|
s.over = true
|
|
return
|
|
}
|
|
s.buf = append(s.buf, b...)
|
|
return
|
|
}
|
|
s.buf = append(s.buf, b...)
|
|
for {
|
|
i := bytes.IndexByte(s.buf, '\n')
|
|
if i < 0 {
|
|
break
|
|
}
|
|
s.line(bytes.TrimSuffix(s.buf[:i], []byte{'\r'}))
|
|
s.buf = s.buf[i+1:]
|
|
}
|
|
if len(s.buf) > 1<<20 { // a runaway line is not one we can use
|
|
s.buf = nil
|
|
s.data = nil
|
|
}
|
|
}
|
|
|
|
func (s *usageSniffer) line(line []byte) {
|
|
if len(line) == 0 {
|
|
s.flushEvent()
|
|
return
|
|
}
|
|
if rest, ok := bytes.CutPrefix(line, []byte("event:")); ok && lastEvent(string(bytes.TrimSpace(rest))) {
|
|
s.ended = true // the name says so even when the data is too big to read
|
|
switch string(bytes.TrimSpace(rest)) {
|
|
case "error", "response.failed":
|
|
s.failed = "upstream stream failed"
|
|
}
|
|
}
|
|
if rest, ok := bytes.CutPrefix(line, []byte("data:")); ok {
|
|
rest = bytes.TrimPrefix(rest, []byte{' '})
|
|
if len(s.data)+len(rest)+1 > 1<<20 {
|
|
s.data = nil // never let an unbounded event accumulate
|
|
return
|
|
}
|
|
if len(s.data) > 0 {
|
|
s.data = append(s.data, '\n')
|
|
}
|
|
s.data = append(s.data, rest...)
|
|
// Relays also send one JSON event per data line without blank separators.
|
|
// Parse a complete value now, but keep incomplete multiline JSON.
|
|
if json.Valid(bytes.TrimSpace(s.data)) {
|
|
s.flushEvent()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *usageSniffer) flushEvent() {
|
|
if len(s.data) > 0 {
|
|
s.parse(bytes.TrimSpace(s.data))
|
|
s.data = nil
|
|
}
|
|
}
|
|
|
|
func (s *usageSniffer) parse(b []byte) {
|
|
if s.sse && string(b) == "[DONE]" {
|
|
s.ended = true
|
|
return
|
|
}
|
|
var t struct {
|
|
Type string `json:"type"`
|
|
Error any `json:"error"`
|
|
Response struct {
|
|
Error any `json:"error"`
|
|
} `json:"response"`
|
|
}
|
|
if s.sse && json.Unmarshal(b, &t) == nil {
|
|
switch {
|
|
case lastEvent(t.Type):
|
|
s.ended = true
|
|
case t.Type == "":
|
|
s.ended = s.ended || t.Error != nil // a Chat stream's error
|
|
}
|
|
if t.Type == "error" || t.Type == "response.failed" || t.Type == "" && t.Error != nil {
|
|
body := b
|
|
if t.Type == "response.failed" {
|
|
body = nil
|
|
if t.Response.Error != nil {
|
|
body, _ = json.Marshal(map[string]any{"error": t.Response.Error})
|
|
}
|
|
}
|
|
s.failed = provider.APIError(body, "upstream stream failed")
|
|
s.u.ErrType = provider.ErrorType(body)
|
|
}
|
|
}
|
|
if up := upstreamOf(b); up != "" {
|
|
s.u.Upstream = up
|
|
}
|
|
if st := upstreamStop(b); st != "" {
|
|
s.u.Stop = st
|
|
}
|
|
if t := upstreamTier(b); t != "" {
|
|
s.u.Tier = t
|
|
}
|
|
switch s.proto {
|
|
case provider.Gemini:
|
|
// a Gemini stream names no type and sends no [DONE]: its end is a
|
|
// candidate's finishReason (geminiEnds, #1644)
|
|
if s.sse && geminiEnds(b) {
|
|
s.ended = true
|
|
}
|
|
// Factory's generateContent chunks, the same usageMetadata Code
|
|
// Assist wraps. Relaying one used to count nothing.
|
|
var v struct {
|
|
ResponseID string `json:"responseId"`
|
|
ModelVersion string `json:"modelVersion"`
|
|
UsageMetadata *struct {
|
|
Prompt int `json:"promptTokenCount"`
|
|
Candidates int `json:"candidatesTokenCount"`
|
|
Thoughts int `json:"thoughtsTokenCount"`
|
|
Cached int `json:"cachedContentTokenCount"`
|
|
} `json:"usageMetadata"`
|
|
}
|
|
if json.Unmarshal(b, &v) == nil {
|
|
s.saw(v.ModelVersion)
|
|
s.u.add(Usage{ResponseID: v.ResponseID})
|
|
if u := v.UsageMetadata; u != nil {
|
|
s.u.add(Usage{Input: max(u.Prompt-u.Cached, 0), CacheRead: u.Cached,
|
|
Output: u.Candidates + u.Thoughts, Reasoning: u.Thoughts})
|
|
}
|
|
}
|
|
case provider.Chat:
|
|
s.tail.see(b)
|
|
var v struct {
|
|
ID string `json:"id"`
|
|
Error any `json:"error"`
|
|
Model string `json:"model"`
|
|
Usage *cUsage `json:"usage"`
|
|
}
|
|
if json.Unmarshal(b, &v) == nil {
|
|
s.saw(v.Model)
|
|
if v.Error == nil {
|
|
s.u.add(Usage{ResponseID: v.ID})
|
|
}
|
|
if v.Usage != nil {
|
|
s.u.add(v.Usage.usage())
|
|
}
|
|
}
|
|
case provider.Responses:
|
|
var v struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Model string `json:"model"`
|
|
Usage *rUsage `json:"usage"`
|
|
Response struct {
|
|
ID string `json:"id"`
|
|
Model string `json:"model"`
|
|
Usage *rUsage `json:"usage"`
|
|
} `json:"response"`
|
|
}
|
|
if json.Unmarshal(b, &v) == nil {
|
|
if !s.sse {
|
|
s.u.add(Usage{ResponseID: v.ID})
|
|
} else if strings.HasPrefix(v.Type, "response.") {
|
|
s.u.add(Usage{ResponseID: v.Response.ID})
|
|
}
|
|
s.saw(v.Response.Model)
|
|
s.saw(v.Model)
|
|
if v.Response.Usage != nil {
|
|
s.u.add(v.Response.Usage.usage())
|
|
} else if v.Usage != nil {
|
|
s.u.add(v.Usage.usage())
|
|
}
|
|
}
|
|
default:
|
|
var v struct {
|
|
ID string `json:"id"`
|
|
Type string `json:"type"`
|
|
Model string `json:"model"`
|
|
Usage *aUsage `json:"usage"`
|
|
Message struct {
|
|
ID string `json:"id"`
|
|
Model string `json:"model"`
|
|
Usage *aUsage `json:"usage"`
|
|
} `json:"message"`
|
|
}
|
|
if json.Unmarshal(b, &v) == nil {
|
|
if !s.sse {
|
|
s.u.add(Usage{ResponseID: v.ID})
|
|
} else if v.Type == "message_start" {
|
|
s.u.add(Usage{ResponseID: v.Message.ID})
|
|
}
|
|
s.saw(v.Message.Model)
|
|
s.saw(v.Model)
|
|
if v.Message.Usage != nil {
|
|
s.u.add(v.Message.Usage.usage())
|
|
}
|
|
if v.Usage != nil {
|
|
s.u.add(v.Usage.usage())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// saw keeps a model the reply named.
|
|
func (s *usageSniffer) saw(model string) {
|
|
if model != "" {
|
|
s.served = model
|
|
}
|
|
}
|
|
|
|
// lastEvent reports whether an event of this type ends a stream.
|
|
func lastEvent(t string) bool {
|
|
switch t {
|
|
case "message_stop", "error", "response.completed", "response.incomplete", "response.failed":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// whole reports whether the stream got to its last event; call it once
|
|
// the body has ended.
|
|
func (s *usageSniffer) whole() bool {
|
|
s.drain()
|
|
return s.ended
|
|
}
|
|
|
|
func (s *usageSniffer) drain() {
|
|
if s.sse {
|
|
if len(s.buf) > 0 {
|
|
s.line(bytes.TrimSuffix(s.buf, []byte{'\r'}))
|
|
s.buf = nil
|
|
}
|
|
s.flushEvent()
|
|
}
|
|
}
|
|
|
|
// settleWhole reads a reply that came whole, once it has ended, for an
|
|
// error it carried under its success status (errorReply): told as failed,
|
|
// with what it said, not counted as an answer.
|
|
func (s *usageSniffer) settleWhole() {
|
|
if s.sse || s.over || s.failed != "" {
|
|
return
|
|
}
|
|
if _, msg, ok := errorReply(s.buf); ok {
|
|
s.failed = msg
|
|
s.u.ErrType = provider.ErrorType(bytes.TrimSpace(s.buf))
|
|
}
|
|
}
|
|
|
|
// usage is what the reply reported, the model it named in Served; call it
|
|
// once the body has ended.
|
|
func (s *usageSniffer) usage() Usage {
|
|
s.drain()
|
|
if !s.sse && !s.over && len(s.buf) > 0 {
|
|
s.parse(bytes.TrimSpace(s.buf))
|
|
s.buf = nil
|
|
}
|
|
u := s.u
|
|
u.Served = s.served
|
|
return u
|
|
}
|