host.js ran info(provider, key) on every fetch, to hand chat.headers
hooks the provider. Each build rebuilt the model list from models.dev and
config, read and parsed plugin-auth.json twice, JSON-cloned the list and
asked the plugin's provider.models hook, which real plugins (cursor, zed
and others) answer by asking the vendor once their own TTL runs out. It ran
even when no plugin had a chat.headers hook.
infoOf(id, key) now keeps the info per account. Any change to the
account's sign-in (setAuth, removeAuth, settle, client.auth.set, renew,
take, reload, all through forgetAccount), new provider options, or a new
models.dev file builds it again, once. providers() seeds the cache with
what it built. The loader, usage and check-in read it too; check's strict
build stays direct. chat.headers gets the shared object, as OpenCode hands
plugins the provider it keeps, and the request asks for it only when a
plugin has a chat.headers hook.
Measured (TestPerfScratch harness, not committed; fake plugin, sandbox
HOME, loaded machine, two runs each):
- {} body, 1-event reply, 400 fetches: models hook asked 400 -> 0 times.
- same, a plugin with 300 config models: 0.995-1.17 ms -> 0.27-0.29 ms
wall per fetch, host CPU 1.23-1.33 ms -> 0.30 ms per fetch, host RSS
87-89 MB -> ~54 MB.
- 246 KB body, 2 MB SSE reply, 40 fetches: host CPU 10.25 -> 8.25 ms per
fetch, models hook asked 40 -> 0 times.
- idle host CPU ~10-20 ms per 10 s before and after.
Tests: TestFetchBuildsInfoOnce counts the fake's models hook (new
FAKE_MODELS_LOG): 20 fetches ask it 0 more times, a new sign-in and new
options each ask it exactly once, and chat.headers and the loader still
see fake-1 and every model. Broken by hand twice, red both times:
returning no cached info ("20 requests asked the models hook 20 more
times, want 0"), and not forgetting it on sign-in ("after a new sign-in
... 0 times, want 1"). host_flow_test's mid-setup case now stubs infoOf
and registers a chat.headers hook for its "info" stage, which red on
this change without it (the stage was never entered).
Run: go test ./internal/plugin -count=1 ok (159.5s);
TestFlowAbortDuringSetup|TestFetchBuildsInfoOnce -count=2 ok.
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A request is told the provider as info builds it once for its account,
|
|
// not built again for every fetch: each build asked the plugin's models
|
|
// hook, and with it the vendor once the plugin's own list was due, before
|
|
// the request went. A new sign-in or new options build it again, once.
|
|
func TestFetchBuildsInfoOnce(t *testing.T) {
|
|
sandbox(t)
|
|
var mu sync.Mutex
|
|
var told []string // x-plugin-model, as chat.headers set it, and x-models, as the loader was told them
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
told = append(told, r.Header.Get("x-plugin-model")+" "+r.Header.Get("x-models"))
|
|
mu.Unlock()
|
|
io.WriteString(w, "ok")
|
|
}))
|
|
defer srv.Close()
|
|
t.Setenv("FAKE_BASE", srv.URL+"/v1")
|
|
log := filepath.Join(t.TempDir(), "models.log")
|
|
t.Setenv("FAKE_MODELS_LOG", log)
|
|
asked := func() int {
|
|
b, _ := os.ReadFile(log)
|
|
return strings.Count(string(b), "asked")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("testdata/fake/index.js")
|
|
if _, err := Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := APIKey(ctx, "fakeco", 0, nil, "k1", NewAccount); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() {
|
|
t.Helper()
|
|
res, err := Fetch(ctx, FetchRequest{Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
|
|
URL: srv.URL + "/v1/chat/completions", Method: "POST", Body: []byte(`{}`)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := io.ReadAll(res.Body)
|
|
res.Body.Close()
|
|
if string(b) != "ok" {
|
|
t.Fatalf("answered %q", b)
|
|
}
|
|
}
|
|
fetch()
|
|
n := asked()
|
|
for range 20 {
|
|
fetch()
|
|
}
|
|
if got := asked() - n; got != 0 {
|
|
t.Fatalf("20 requests asked the models hook %d more times, want 0", got)
|
|
}
|
|
mu.Lock()
|
|
for _, m := range told {
|
|
if m != "fake-1 fake-1,fake-claude,fake-gemini" {
|
|
t.Fatalf("chat.headers and the loader were told %q, want fake-1 and every model", m)
|
|
}
|
|
}
|
|
mu.Unlock()
|
|
|
|
// a new sign-in for the account: built again, once
|
|
if err := Restore(ctx, "fakeco", "fakeco", map[string]any{"type": "api", "key": "k2"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n = asked()
|
|
for range 5 {
|
|
fetch()
|
|
}
|
|
if got := asked() - n; got != 1 {
|
|
t.Fatalf("after a new sign-in 5 requests asked the models hook %d times, want 1", got)
|
|
}
|
|
// new options: built again, once
|
|
if err := SetProviderOptions(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n = asked()
|
|
for range 5 {
|
|
fetch()
|
|
}
|
|
if got := asked() - n; got != 1 {
|
|
t.Fatalf("after new options 5 requests asked the models hook %d times, want 1", got)
|
|
}
|
|
}
|