Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

plugin: a request is told the provider info built once per account, not built again per fetch

yetonee3c09f4f36fe
diff --git a/internal/plugin/host.js b/internal/plugin/host.js
index 353fc2e39..8656dd20d 100644
--- a/internal/plugin/host.js
+++ b/internal/plugin/host.js
@@ -285,6 +285,22 @@ const hooks = [] // {spec, hooks}
const gatewayHooks = [] // {spec, options, onRequest}
const loaded = [] // {spec, id, error}
const loaders = new Map() // account → options the auth loader returned
+// infos are info's answers a request is given, by account: built once, not
+// for every fetch, which asked the plugin's models hook (its vendor, once
+// its own list's time was up) before each request went. One goes with its
+// account's sign-in (forgetAccount), and all with the options the user set
+// (setProviderOptions) or a new models.dev catalog; a listing (providers)
+// puts its own in.
+const infos = new Map() // account → {gen, md, info}
+let infoGen = 0
+
+// forgetAccount drops what was made of the sign-in at key: its loader's
+// options and its info, which the next request makes again.
+function forgetAccount(key) {
+ loaders.delete(key)
+ infos.delete(key)
+ infoGen++
+}
const sessions = new Map() // oauth sign-in in progress → its authorize result
const inflight = new Map() // fetch id → its lifecycle and owned resources
const renewing = new Map() // account → its sign-in's renewal under way
@@ -886,7 +902,7 @@ function setAuth(key, info) {
changeAuth((all) => {
all[key] = info
})
- loaders.delete(key)
+ forgetAccount(key)
send({ event: "auth", provider: providerOf(key), account: key })
}
@@ -894,7 +910,7 @@ function removeAuth(key) {
changeAuth((all) => {
delete all[key]
})
- loaders.delete(key)
+ forgetAccount(key)
send({ event: "auth", provider: providerOf(key), account: key })
}
@@ -984,8 +1000,8 @@ function settle(provider, key) {
return false
})
if (k) {
- loaders.delete(k)
- loaders.delete(key)
+ forgetAccount(k)
+ forgetAccount(key)
send({ event: "auth", provider, account: k })
return k
}
@@ -1023,7 +1039,7 @@ function makeClient() {
all[key] = body
})
if (kept !== false) {
- loaders.delete(key)
+ forgetAccount(key)
send({ event: "auth", provider: providerOf(key), account: key })
}
}
@@ -1709,6 +1725,26 @@ async function info(id, key, strict) {
return out
}
+// infoOf is info(id, key) as a request has it: the one built for the
+// account, until its sign-in or the options change. What it gives is
+// shared: the caller must not change it.
+async function infoOf(id, key) {
+ const md = mdev()
+ const had = infos.get(key)
+ if (had && had.gen === infoGen && had.md === md) return had.info
+ const gen = infoGen
+ const p = await info(id, key)
+ // a sign-in changed while it was built: the next request builds it again
+ if (gen === infoGen) infos.set(key, { gen, md, info: p })
+ return p
+}
+
+// keepInfo keeps p, info(id, key) just built by a listing, for the
+// account's requests.
+function keepInfo(key, p, gen) {
+ if (gen === infoGen && !p.failed) infos.set(key, { gen, md: mdev(), info: p })
+}
+
// shown is a plugin's string as magpie shows it: trimmed and at most n
// characters, "" for anything else.
const shown = (v, n) => (typeof v === "string" ? v.trim().slice(0, n) : "")
@@ -1827,12 +1863,14 @@ async function providers({ proxies } = {}) {
for (const [id, a] of auths()) {
const keys = accountsOf(stored, id)
const through = (k) => proxies?.[id]?.[k ?? keys[0] ?? ""] ?? proxies?.[id]?.[""] ?? ""
+ const gen = infoGen
const p = await via.run(through(), () => info(id))
+ keepInfo(keys[0] ?? id, p, gen)
// each account's own models, as the built-ins read each account's: a
// plan may serve fewer, or others, than the first account's. One that
// can't be read is taken to have them all, or the ones it was last
// told to have, as a built-in account whose fetch failed keeps its own.
- const own = await Promise.all(keys.slice(1).map((k) => via.run(through(k), () => info(id, k, true)).catch((e) => ({ failed: true, listError: e?.message ?? String(e) }))))
+ const own = await Promise.all(keys.slice(1).map((k) => via.run(through(k), () => info(id, k, true)).then((q) => (keepInfo(k, q, gen), q)).catch((e) => ({ failed: true, listError: e?.message ?? String(e) }))))
const models = { ...p.models }
for (const q of own) for (const [k, m] of Object.entries(q?.models ?? {})) models[k] ??= m
const ids = (q) => Object.values(q.models).filter((m) => m.status !== "deprecated").map((m) => m.id)
@@ -2106,7 +2144,7 @@ async function renew(provider, key, a) {
})
if (kept === false) return
unrenewed.delete(key)
- loaders.delete(key)
+ forgetAccount(key)
send({ event: "auth", provider, account: key })
send({ event: "signIn", provider, account: key, said: "renewed" })
}
@@ -2123,7 +2161,7 @@ async function options(provider, key) {
const stored = readAuth()[key]
let opts = {}
if (a?.loader && stored) {
- const p = await info(provider, key)
+ const p = await infoOf(provider, key)
opts = (await inScope(provider, key, () => a.loader(async () => readAuth()[key], JSON.parse(JSON.stringify(p))))) ?? {}
}
if (!opts.apiKey && stored?.type === "api") opts = { ...opts, apiKey: stored.key }
@@ -2180,7 +2218,7 @@ async function usageOf(provider, account) {
const key = accountKey(provider, account)
if (!readAuth()[key]) throw new Error("not signed in")
await fresh(provider, key)
- const p = await info(provider, key)
+ const p = await infoOf(provider, key)
const u = (await inScope(provider, key, () => a.usage(async () => readAuth()[key], JSON.parse(JSON.stringify(p))))) ?? {}
const when = (v) => {
if (v === undefined || v === null || v === "") return ""
@@ -2255,7 +2293,7 @@ async function checkinOf(provider, account) {
const key = accountKey(provider, account)
if (!readAuth()[key]) throw new Error("not signed in")
await fresh(provider, key)
- const p = await info(provider, key)
+ const p = await infoOf(provider, key)
const r = (await inScope(provider, key, () => a.checkin(async () => readAuth()[key], JSON.parse(JSON.stringify(p))))) ?? {}
const num = (v) => (typeof v === "number" && isFinite(v) ? Math.max(0, v) : 0)
const message = typeof r.message === "string" ? r.message : ""
@@ -2301,13 +2339,19 @@ async function fetchAs(id, record, key, params) {
for (const [k, v] of Object.entries(sdkHeaders(npm, o.apiKey))) h.set(k, v)
for (const [k, v] of Object.entries(o.headers ?? {})) h.set(k, String(v))
for (const [k, v] of Object.entries(headers ?? {})) h.set(k, v)
- // chat.headers: what the plugins add to the request, theirs winning
- const p = await info(provider, key)
- checkFetch(record)
- const m = p.models[model] ?? { id: model, providerID: provider, api: { id: model, npm } }
- for (const x of hooks) {
+ // chat.headers: what the plugins add to the request, theirs winning.
+ // They are told the provider as info builds it once for the account
+ // (infoOf), the one object every request is told, as OpenCode tells
+ // them the provider it keeps
+ const said = hooks.filter((x) => typeof x.hooks["chat.headers"] === "function")
+ let p, m
+ if (said.length) {
+ p = await infoOf(provider, key)
+ checkFetch(record)
+ m = p.models[model] ?? { id: model, providerID: provider, api: { id: model, npm } }
+ }
+ for (const x of said) {
const fn = x.hooks["chat.headers"]
- if (typeof fn !== "function") continue
const out = { headers: {} }
try {
await fn(
@@ -2419,6 +2463,9 @@ const handlers = {
// plugin reads them when it next asks (#2038)
setProviderOptions(p) {
chosen = p?.options ?? {}
+ // a models hook may list by them
+ infos.clear()
+ infoGen++
return null
},
// check tries one account as a request would: its loader, then its
@@ -2464,7 +2511,7 @@ const handlers = {
if (!Object.keys(out).length) return false
})
for (const k of Object.keys(out)) {
- loaders.delete(k)
+ forgetAccount(k)
send({ event: "auth", provider: providerOf(k), account: k })
}
return { auths: out }
@@ -2476,7 +2523,7 @@ const handlers = {
return null
},
reload(p) {
- for (const k of p.account ? [p.account] : accountsOf(readAuth(), p.provider)) loaders.delete(k)
+ for (const k of p.account ? [p.account] : accountsOf(readAuth(), p.provider)) forgetAccount(k)
return null
},
}
diff --git a/internal/plugin/host_flow_test.go b/internal/plugin/host_flow_test.go
index 54d8eaedc..93ba2e6ba 100644
--- a/internal/plugin/host_flow_test.go
+++ b/internal/plugin/host_flow_test.go
@@ -84,7 +84,7 @@ const context = vm.createContext({
process: { stdout, stdin: {}, exit: () => { throw Error('unexpected exit') } },
readline: { createInterface: () => rl },
options: async () => ({ fetch: async () => context.reply }),
- info: async () => ({ models: {} }),
+ infoOf: async () => ({ models: {} }),
sdkHeaders: () => ({}), bodyOf: () => undefined,
accountKey: () => '',
via: { run: (_, fn) => fn() }, inScope: (_, key, fn) => fn(),
@@ -225,10 +225,13 @@ async function main() {
if (stage === 'options') { entered = true; await barrier }
return { fetch: async () => { vendorCalls++; return context.reply } }
}
- context.info = async () => {
+ context.infoOf = async () => {
if (stage === 'info') { entered = true; await barrier }
return { models: {} }
}
+ // a request is told the provider only for a chat.headers hook
+ run('hooks.length = 0')
+ if (stage === 'info') run('hooks.push({ spec: "t", hooks: { "chat.headers": async () => {} } })')
fetchOne()
await until(() => entered, 'plugin setup entered its deferred await')
assert.equal(run('inflight.get(1).state'), 'setup')
@@ -239,7 +242,7 @@ async function main() {
await until(idle, 'cancelled setup finishes')
assert.equal(vendorCalls, 0, 'cancelled setup never starts vendor')
context.options = async () => ({ fetch: async () => { vendorCalls++; return context.reply } })
- context.info = async () => ({ models: {} })
+ context.infoOf = async () => ({ models: {} })
fetchOne()
await until(idle, 'healthy fetch after cancelled setup')
assert.equal(vendorCalls, 1)
diff --git a/internal/plugin/info_once_test.go b/internal/plugin/info_once_test.go
new file mode 100644
index 000000000..e3ceb50b2
--- /dev/null
+++ b/internal/plugin/info_once_test.go
@@ -0,0 +1,101 @@
+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)
+ }
+}
diff --git a/internal/plugin/testdata/fake/index.js b/internal/plugin/testdata/fake/index.js
index 9981f08a0..7a40501e8 100644
--- a/internal/plugin/testdata/fake/index.js
+++ b/internal/plugin/testdata/fake/index.js
@@ -240,6 +240,8 @@ export const FakePlugin = async ({ client }) => ({
provider: {
id: ID,
models: async (p, { auth }) => {
+ // $FAKE_MODELS_LOG: each time the hook is asked is a line there
+ if (process.env.FAKE_MODELS_LOG) (await import("node:fs")).appendFileSync(process.env.FAKE_MODELS_LOG, "asked\n")
if (auth?.refresh === "r-dead" || auth?.key === "dead") throw new Error("the vendor refused the sign-in")
if (auth?.refresh === "r-models-gone") throw Object.assign(new Error("the vendor refused the sign-in"), { signIn: "expired" })
// a sign-in past its time, said in words only, as Grok's plugin says it