mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
A completion no provider could serve answered HTTP 200 with an error envelope. The error already carried 503; the call sites wrote the message and dropped the status, so every OpenAI-shaped client read success and then found no choices in the body. The status a failure carries is now the status it is written with. Beside it, a name a program can branch on. A message is written for a person and gets rewritten, and one status answers several questions at once, so neither can be switched on: 402 insufficient_balance is the caller's wallet and their remedy, 503 providers_exhausted is ours. Shapes that need telling apart carry a name; the rest carry nothing rather than something invented. A free route is free BECAUSE the vendor keeps what it carried — asking for one while denying collection asks for the price without the terms. So the route's price states the terms: zero-priced calls go out allowing collection, priced calls denying it, and the answer reports which applied so a consent notice never has to infer it from a price list. Stated in the family's own dialect, so a vendor is never sent a field it did not define, and written over the caller's own preferences rather than replacing them. Co-authored-by: Hanzo Dev <[email protected]>
54 lines
2.0 KiB
Go
54 lines
2.0 KiB
Go
// Copyright 2026 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 (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/ai/object"
|
|
)
|
|
|
|
// A refusal a client must act on carries a name it can branch on, and the two
|
|
// refusals that read alike to a person are the two that must not read alike to a
|
|
// program: "we could not serve this" is ours to fix, "your balance is empty" is
|
|
// theirs, and only one of them is answered by adding credits.
|
|
func TestARefusalNamesItself(t *testing.T) {
|
|
out := exhausted("some-model", []attempt{{provider: "openrouter", status: 402, err: &apiError{status: 402, msg: "Insufficient credits."}}})
|
|
if got := statusOf(out); got != http.StatusServiceUnavailable {
|
|
t.Errorf("every provider refused → status %d, want 503", got)
|
|
}
|
|
if got := codeOf(out); got != codeExhausted {
|
|
t.Errorf("every provider refused → code %q, want %q", got, codeExhausted)
|
|
}
|
|
|
|
none := exhausted("some-model", nil)
|
|
if got := codeOf(none); got != codeExhausted {
|
|
t.Errorf("no provider configured → code %q, want %q", got, codeExhausted)
|
|
}
|
|
|
|
pay := billingError("balance is $%.2f", 0.0)
|
|
if got := statusOf(pay); got != http.StatusPaymentRequired {
|
|
t.Errorf("spend denial → status %d, want 402", got)
|
|
}
|
|
if got := codeOf(pay); got != object.CodeInsufficientBalance {
|
|
t.Errorf("spend denial → code %q, want %q", got, object.CodeInsufficientBalance)
|
|
}
|
|
|
|
if got := codeOf(authError("who are you")); got != "" {
|
|
t.Errorf("a failure with no stable name carries %q, want none", got)
|
|
}
|
|
}
|