Files
ai/controllers/upstream_error.go
zeekayandHanzo Dev c264aa6086 refactor(go-openai): import the hanzoai/go-openai fork directly, drop the replace
ai used the fork only through 'replace github.com/sashabaranov/go-openai =>
github.com/hanzoai/go-openai', and Go replace directives do NOT propagate to
consumers — so every downstream repo (cloud, gateway, iam, …) had to copy the
same replace or fail to compile against fork-only fields like
model/local.go's ReasoningContent. The fork now declares its own module path
(github.com/hanzoai/go-openai v1.41.0), so ai requires it DIRECTLY and every
consumer inherits it transitively. One and only one way — no per-repo replace.

22 import sites renamed (package identifier 'openai' unchanged). Upstream
sashabaranov/go-openai remains ONLY as an indirect dep of the go-cosyvoice /
go-openai-realtime TTS chain (a separate upstream we don't bridge types with;
that chain is being force-owned next). model + tts + object build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-17 22:07:39 -07:00

75 lines
2.9 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.
func upstreamHTTPStatus(err error) int {
var oe *openai.APIError
if errors.As(err, &oe) {
return oe.HTTPStatusCode
}
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, 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.
func statusForModelError(err error) int {
switch s := statusOf(err); s {
case http.StatusUnauthorized:
return http.StatusInternalServerError
default:
return s
}
}