plugin: a provider's models come from the plugin that serves it
Two plugins can sign in to one provider id. On the owner's MacBook Air,
tianpeng-dev's opencode-doubao-work-auth 1.1.6 was installed before
@magpie-community/opencode-doubao-work-auth 0.1.2. Both name
doubao-work. The third party's config hook ran first and wrote
cfg.provider["doubao-work"] with models: {}. Ours writes its entry with
??=, so it added nothing. Ours served the id, being the last to name it,
but info() read the shared config.provider entry: the provider showed
the other plugin's name and api and 0 models.
The config hooks still share one config object. Before each hook runs,
every provider it signs in to is set to the user's own entry, or to
this plugin's entry from the last load, never to another plugin's.
What the hook leaves there is kept as that plugin's (ownConfig).
info() reads the serving plugin's entry (providerConfig), so Prefer
moves the name, api and models with the provider.
Run (mote, at c056b7c8c plus this change):
- go test ./internal/plugin/ ./internal/provider/ ./internal/gateway/
-count=1 -timeout 45m: ok (plugin 136s, provider 159s, gateway 879s)
- GOOS=linux and GOOS=windows go vet ./internal/plugin/: ok
- gofmt -l: clean
Broken by hand:
- With host.js as at c056b7c8c,
TestServingPluginsConfigIsTheProviders is red with the Air's case:
served by mine, but named "Theirs", with theirs' api and no models.
- With info() alone back on config.provider[id], it is red after
Prefer(theirs): theirs serves, but with mine's name and models.
Not tried: the Air itself, before the release reaches it.
This commit is contained in:
+28
-1
@@ -1546,17 +1546,44 @@ async function loadPlugins(list) {
|
||||
if (!isPi) loaded.push({ spec: p.spec })
|
||||
}
|
||||
config = { provider: {}, ...structuredClone(userConfig) }
|
||||
ownConfig = new Map()
|
||||
for (const h of hooks) {
|
||||
if (typeof h.hooks.config !== "function") continue
|
||||
// a provider the plugin signs in to is built from the user's own
|
||||
// entry, or what this plugin made of it before, never from another
|
||||
// plugin's that signs in to it too: a third party's that set it
|
||||
// first (models: {}) left the other's `??=` nothing to add, and the
|
||||
// provider no models (豆包工作, two plugins of doubao-work)
|
||||
const ids = signsIn(h.spec)
|
||||
for (const id of ids) {
|
||||
const own = ownConfig.get(ownKey(h.spec, id)) ?? structuredClone(userConfig.provider?.[id])
|
||||
if (own === undefined) delete config.provider[id]
|
||||
else config.provider[id] = own
|
||||
}
|
||||
try {
|
||||
// one at a time, as each may build on the last, but none for long
|
||||
await inTime(() => h.hooks.config(config), loadWait, "config hook")
|
||||
} catch (e) {
|
||||
send({ event: "log", level: "error", message: `${h.spec}: config hook: ${e?.message ?? e}` })
|
||||
}
|
||||
for (const id of ids) ownConfig.set(ownKey(h.spec, id), config.provider?.[id])
|
||||
}
|
||||
}
|
||||
|
||||
// ownConfig is each provider's config entry as the config hook of each
|
||||
// plugin that signs in to it made it, by ownKey(spec, id). The plugin
|
||||
// serving the provider (auths) is the one whose entry counts.
|
||||
let ownConfig = new Map()
|
||||
const ownKey = (spec, id) => spec + "\0" + id
|
||||
|
||||
// providerConfig is the config entry of provider id: the serving plugin's
|
||||
// own, or what the config hooks made of it when no plugin signs in to it.
|
||||
function providerConfig(id) {
|
||||
const server = auths().get(id)?.spec
|
||||
const k = server && ownKey(server, id)
|
||||
return k && ownConfig.has(k) ? ownConfig.get(k) : config.provider?.[id]
|
||||
}
|
||||
|
||||
// A bounded await detaches its listener even when the plugin ignores the
|
||||
// signal. The rejected promise is observed, so late failures cannot escape.
|
||||
function bounded(promise, signal) {
|
||||
@@ -2094,7 +2121,7 @@ function fromModelsDev(p, m) {
|
||||
// its own options' signal too (#4678).
|
||||
async function info(id, key, strict, limit, quiet, signal) {
|
||||
const md = mdev()[id]
|
||||
const cfg = config.provider?.[id]
|
||||
const cfg = providerConfig(id)
|
||||
const out = {
|
||||
id,
|
||||
name: cfg?.name ?? md?.name ?? id,
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package plugin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Two plugins that sign in to one provider id, as on the owner's MacBook
|
||||
// Air: a third party's doubao-work (tianpeng-dev's, added first) sets
|
||||
// the provider with models: {} in its config hook and lists models
|
||||
// itself with its own sign-in; @magpie-community's, added after it,
|
||||
// serves doubao-work and writes its models with `??=`. Its models are
|
||||
// the provider's, not the first plugin's empty list, and Prefer hands
|
||||
// the provider, with its config, to the other.
|
||||
func TestServingPluginsConfigIsTheProviders(t *testing.T) {
|
||||
sandbox(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
dir := t.TempDir()
|
||||
write := func(name, src string) string {
|
||||
f := filepath.Join(dir, name, "index.js")
|
||||
if err := os.MkdirAll(filepath.Dir(f), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(f, []byte(src), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
theirs := write("theirs", `export default async () => ({
|
||||
async config(cfg) {
|
||||
cfg.provider ??= {}
|
||||
cfg.provider["dup"] ??= { name: "Theirs", npm: "@ai-sdk/openai-compatible", api: "https://theirs.invalid/v1", models: {} }
|
||||
},
|
||||
auth: { provider: "dup", methods: [{ type: "api", label: "Key" }] },
|
||||
})
|
||||
`)
|
||||
mine := write("mine", `export default async () => ({
|
||||
async config(cfg) {
|
||||
cfg.provider ??= {}
|
||||
cfg.provider["dup"] ??= { name: "Mine", npm: "@ai-sdk/openai-compatible", api: "https://mine.invalid/v1",
|
||||
models: { auto: { name: "Auto" }, pro: { name: "Pro" } } }
|
||||
},
|
||||
auth: { provider: "dup", methods: [{ type: "api", label: "Key" }] },
|
||||
})
|
||||
`)
|
||||
row := func() Provider {
|
||||
t.Helper()
|
||||
ps, err := Providers(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range ps {
|
||||
if p.ID == "dup" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
t.Fatalf("no dup in %+v", ps)
|
||||
return Provider{}
|
||||
}
|
||||
ids := func(p Provider) []string {
|
||||
var out []string
|
||||
for _, m := range p.Models {
|
||||
out = append(out, m.ID)
|
||||
}
|
||||
return out
|
||||
}
|
||||
if _, err := Add(ctx, theirs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := Add(ctx, mine); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p := row(); p.Spec != mine || p.Name != "Mine" || p.API != "https://mine.invalid/v1" || !reflect.DeepEqual(ids(p), []string{"auto", "pro"}) {
|
||||
t.Fatalf("dup = %s %q %s %v, want mine's: Mine, its api and models auto, pro", p.Spec, p.Name, p.API, ids(p))
|
||||
}
|
||||
if err := Prefer(theirs, "dup"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p := row(); p.Spec != theirs || p.Name != "Theirs" || len(p.Models) != 0 {
|
||||
t.Fatalf("after Prefer(theirs), dup = %s %q %v, want theirs' with no models", p.Spec, p.Name, ids(p))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user