diff --git a/internal/plugin/host.js b/internal/plugin/host.js index 87c1547eb..5530e9ab2 100644 --- a/internal/plugin/host.js +++ b/internal/plugin/host.js @@ -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, diff --git a/internal/plugin/plugin_clash_config_test.go b/internal/plugin/plugin_clash_config_test.go new file mode 100644 index 000000000..62ff34060 --- /dev/null +++ b/internal/plugin/plugin_clash_config_test.go @@ -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)) + } +}