mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Two refusals wear the same 402 and mean opposite things. "Your wallet is
empty" is the caller's to fix and has to reach them. "We cannot pay our
vendor" is ours: the caller owes nothing, has no action that helps, and a
client that acts on a 402 goes and tops up a balance that is already funded.
Our own daily cash breaker answered billingError — 402 with the machine code
a client shows a billing prompt for — and quoted our spend and ceiling in the
sentence a customer reads. It now answers supplyError: 503, supply_unavailable,
and our cash figures stay on our side of the line.
The raw proxies asked nothing at all. A request carrying tools or images skips
the completion pipeline because that pipeline is text-only, so the surface that
carries most of the traffic reached the vendor directly and wrote back whatever
status came home — then read usage off the error body, found none, tokenized
the refusal and billed it as a success. relay() asks the question the failover
loop and the family pipe already ask, once, ahead of the stream/buffered split
and ahead of every billing line: ours -> 503, theirs -> unchanged.
An operator learns from cloud_supply_refused{provider,reason} on the
DefaultRegistry that GET /v1/metrics already serves, incremented where the
refusal is already announced, so the log line and the series cannot disagree.
99 lines
4.1 KiB
Go
99 lines
4.1 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 (
|
|
"errors"
|
|
"net/http"
|
|
|
|
openai "github.com/hanzoai/go-openai"
|
|
)
|
|
|
|
// Upstream failures carry an HTTP status. This is the ONE place that extracts
|
|
// it, and the ONE place (wrapUpstreamError) that types it — as the existing sum
|
|
// type apiError — so every downstream decision (retry / classify / render) folds
|
|
// over the same typed value instead of sniffing three copies of the same
|
|
// substring list. Decomplected: the status is a property of the error, not a
|
|
// guess re-derived at each call site.
|
|
//
|
|
// The go-openai SDK returns *openai.APIError with an HTTPStatusCode field for
|
|
// every provider HTTP failure (429 "Platform overloaded", 403 agreement-gated,
|
|
// 5xx, …). Without this wrapper those statuses are lost into a plain error
|
|
// string — which is how a transient upstream 429 used to become a hard,
|
|
// untyped, fatal-looking 500 at the client edge (the "it stops" failure).
|
|
|
|
// upstreamHTTPStatus returns the HTTP status carried by a provider/SDK error, or
|
|
// 0 for a transport/decode error that carries none.
|
|
//
|
|
// It reads both spellings of the same fact — the SDK's *openai.APIError and the
|
|
// *apiError wrapUpstreamError restates it as — so a status survives being
|
|
// wrapped and every reader gets the same answer whichever side of the wrapper it
|
|
// stands on.
|
|
//
|
|
// Distinct from statusOf, which answers a different question ("what should the
|
|
// CLIENT see") and therefore defaults to 401 for an unrecognised error. That
|
|
// default is correct for rendering and catastrophic for attribution: it would
|
|
// file every connection reset as an auth failure, and faultOf would refuse to
|
|
// fail over. Absence is reported as 0 here so the caller can tell "no status"
|
|
// from "some status".
|
|
func upstreamHTTPStatus(err error) int {
|
|
var oe *openai.APIError
|
|
if errors.As(err, &oe) {
|
|
return oe.HTTPStatusCode
|
|
}
|
|
var ae *apiError
|
|
if errors.As(err, &ae) {
|
|
return ae.status
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// wrapUpstreamError types a provider/SDK error with its HTTP status as an
|
|
// apiError (the package's existing error sum type), so statusOf, isRetryableError
|
|
// and the wire error-mappers all read ONE typed value. A transport error with no
|
|
// status is returned unwrapped (statusOf then fails secure per its contract).
|
|
func wrapUpstreamError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if status := upstreamHTTPStatus(err); status != 0 {
|
|
return &apiError{status: status, msg: err.Error()}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// statusForModelError is the status a CLIENT should see for a model-call
|
|
// failure. Upstream HTTP failures keep their real status (a 429 stays a 429 so
|
|
// the client retries; a 403 stays a 403). A model-call error is never an auth
|
|
// failure FROM THE CLIENT'S PERSPECTIVE — its own credential was already
|
|
// validated upstream — so statusOf's auth-fail-secure default (401) is mapped to
|
|
// 500 here. This is the one place that distinction is made.
|
|
//
|
|
// 402 is the same argument about money instead of identity, and it is the more
|
|
// expensive one to get wrong. A vendor's 402 says OUR account with that vendor is
|
|
// spent; forwarded, it tells a funded customer they are out of credit and sends
|
|
// every client that acts on a 402 to fix a bill that is not theirs. 503 is the
|
|
// true statement, and the same one exhausted() makes.
|
|
func statusForModelError(err error) int {
|
|
switch s := statusOf(err); s {
|
|
case http.StatusUnauthorized:
|
|
return http.StatusInternalServerError
|
|
case http.StatusPaymentRequired:
|
|
return http.StatusServiceUnavailable
|
|
default:
|
|
return s
|
|
}
|
|
}
|