providers: another vendor's base isn't parsed to look for Tencent or OpenCode Go
The Providers page asks tencentPlanRegion and openCodeGateway of every
provider on each list. Both url.Parse'd every base, a heap profile of 40
/api/providers asks put 3MB of url.Parse there (2.5MB Tencent, 0.5MB
OpenCode). Every host either takes holds "tencentmaas.com" or
"opencode.ai", so a base without it (case folded; ToLower copies nothing
for an already lower-case base) returns at once.
TestOtherVendorsBasesAreNotParsed: 0 allocations for another vendor's
provider and base, and mixed-case Tencent and OpenCode Go bases still
resolve (ap-guangzhou, go). Red on the old code (3 and 1 allocations).
Run on mote at 682a1e392: go test ./internal/provider ./internal/gui ok;
vet provider for darwin, linux and windows ok.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package provider
|
||||
|
||||
import "testing"
|
||||
|
||||
// The Providers page asks every provider whether it is a Tencent Token
|
||||
// Plan or OpenCode's gateway; a base of another vendor's is told apart
|
||||
// without parsing it.
|
||||
func TestOtherVendorsBasesAreNotParsed(t *testing.T) {
|
||||
p := Provider{ID: "x", Chat: "https://api.example.com/v1", Responses: "https://api.example.com/v1", Anthropic: "https://api.example.com/anthropic"}
|
||||
if n := testing.AllocsPerRun(50, func() { tencentPlanRegion(p) }); n != 0 {
|
||||
t.Errorf("tencentPlanRegion of another vendor's provider allocated %v times a call", n)
|
||||
}
|
||||
if n := testing.AllocsPerRun(50, func() { openCodeGateway(p.Chat) }); n != 0 {
|
||||
t.Errorf("openCodeGateway of another vendor's base allocated %v times a call", n)
|
||||
}
|
||||
// and still what they are about, whatever its case
|
||||
if r, ok := tencentPlanRegion(Provider{Chat: "https://TokenHub.TencentMaaS.com/plan/v3"}); !ok || r != "ap-guangzhou" {
|
||||
t.Errorf("Tencent's plan base: %q %v", r, ok)
|
||||
}
|
||||
if g := openCodeGateway("https://OpenCode.ai/zen/go/v1"); g != "go" {
|
||||
t.Errorf("OpenCode Go's base: %q", g)
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,10 @@ func openCodeRoot(base string) (root, catalogID string) {
|
||||
// workspace's pay-as-you-go credits), "" for any other, a provider of the
|
||||
// user's own at …/inference/custom among them, which its vendor bills.
|
||||
func openCodeGateway(base string) string {
|
||||
// every host openCodeHost takes holds it: another base isn't parsed
|
||||
if !strings.Contains(strings.ToLower(base), "opencode.ai") {
|
||||
return ""
|
||||
}
|
||||
u, err := url.Parse(strings.TrimSpace(base))
|
||||
if err != nil || !openCodeHost(u.Hostname()) {
|
||||
return ""
|
||||
|
||||
@@ -95,6 +95,12 @@ func tencentPlanRegion(p Provider) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
for _, base := range []string{p.Chat, p.Responses, p.Anthropic} {
|
||||
// both hosts below hold it: every other base, asked for each
|
||||
// provider on each Providers page, isn't parsed (ToLower makes
|
||||
// no copy of a base already lower-case)
|
||||
if !strings.Contains(strings.ToLower(base), "tencentmaas.com") {
|
||||
continue
|
||||
}
|
||||
u, err := url.Parse(base)
|
||||
if err != nil || !strings.HasPrefix(strings.TrimSuffix(u.Path, "/")+"/", "/plan/") {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user