Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions pkgs/mcpx/internal/daemon/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ func (s *Server) modelLayer(ctx context.Context, session string) (model.Provider
DefaultModel: pol.ModelDefault,
Timeout: pol.ModelTimeout,
Sampler: s.brokerSampler(session),
PluginSampler: s.pluginSampler(session),
})
}

Expand Down Expand Up @@ -1292,6 +1293,18 @@ func (p modelRepairProvider) Complete(ctx context.Context, req scripting.Complet
// unavailable provider, not a hang: the broker's expiry answer lands as
// an error here and the caller falls back deterministically.
func (s *Server) brokerSampler(session string) model.SampleFunc {
return s.brokerSamplerFor(session, "sampling")
}

// pluginSampler is the plugin backend's binding over the same broker
// (#1925): identical plumbing, but the question and the answer carry
// the plugin provider's name, so a plugin answerer can tell its
// questions from sampling's and callers see which backend answered.
func (s *Server) pluginSampler(session string) model.SampleFunc {
return s.brokerSamplerFor(session, "plugin")
}

func (s *Server) brokerSamplerFor(session string, provider string) model.SampleFunc {
if s.reg == nil || s.reg.broker == nil {
return nil
}
Expand All @@ -1311,7 +1324,7 @@ func (s *Server) brokerSampler(session string) model.SampleFunc {
question, err := broker.OpenRequest(elicit.SampleAsk(elicit.Ask{
Server: "mcpx",
Session: session,
Message: "mcpx asks for a model completion (model.provider=sampling)",
Message: "mcpx asks for a model completion (model.provider=" + provider + ")",
TTL: ttl,
}, params))
if err != nil {
Expand Down Expand Up @@ -1340,13 +1353,13 @@ func (s *Server) brokerSampler(session string) model.SampleFunc {
s.reg.publish(events.Event{Kind: events.SampleAnswered, Server: question.Server,
Data: mustJSON(elicitAnsweredSummary(question, ans))})
if ans.Action != elicit.Accept {
return model.Response{}, fmt.Errorf("model: sampling %s: %w", ans.Action, model.ErrUnavailable)
return model.Response{}, fmt.Errorf("model: %s %s: %w", provider, ans.Action, model.ErrUnavailable)
}
text, answered := parseSampleResult(ans.Content)
if strings.TrimSpace(text) == "" {
return model.Response{}, fmt.Errorf("model: sampling answered with no text: %w", model.ErrUnavailable)
return model.Response{}, fmt.Errorf("model: %s answered with no text: %w", provider, model.ErrUnavailable)
}
return model.Response{Text: text, Model: answered, Provider: "sampling"}, nil
return model.Response{Text: text, Model: answered, Provider: provider}, nil
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkgs/mcpx/internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// Backends in this slice: an OpenAI-compatible configured provider (any
// base URL, so OpenAI, LiteLLM, Ollama and LM Studio all work through it)
// and sampling through the broker (the pre-existing path, kept as a
// backend rather than replaced). The opencode-plugin backend is a later
// slice; it depends on #47.
// backend rather than replaced), plus the plugin backend: a second
// sampler binding over the same broker, labelled for the plugin
// answerer (#1925).
//
// The scope guard from #1925 binds every caller: no mcpx operation may
// call a model on a path where a deterministic answer exists, and on a
Expand Down
42 changes: 41 additions & 1 deletion pkgs/mcpx/internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,49 @@ func TestResolveAuto(t *testing.T) {
}
}

func TestResolvePluginWithInstalledPath(t *testing.T) {
// #1925 (task 13246): with the plugin sampler binding installed,
// model.provider=plugin resolves to the plugin backend and
// generates through that path.
sampler := func(_ context.Context, req Request) (Response, error) {
return Response{Text: "from the plugin answerer", Model: req.Model}, nil
}
p, err := Resolve(Config{Provider: "plugin", PluginSampler: sampler})
if err != nil {
t.Fatalf("Resolve(plugin with path): %v", err)
}
if p == nil || p.Name() != "plugin" {
t.Fatalf("Resolve(plugin with path) = %v, want the plugin provider", p)
}
resp, err := p.Generate(context.Background(), Request{Model: "some-model"})
if err != nil {
t.Fatalf("plugin Generate: %v", err)
}
if resp.Text != "from the plugin answerer" || resp.Provider != "plugin" {
t.Errorf("plugin Generate = %+v, want the sampler's text stamped provider=plugin", resp)
}
// The bare provider with no function installed is unavailable,
// never a panic.
if _, err := NewPluginProvider(nil).Generate(context.Background(), Request{}); !errors.Is(err, ErrUnavailable) {
t.Errorf("nil plugin provider: err = %v, want ErrUnavailable", err)
}
// A profile chain can also name the plugin backend.
p, err = Resolve(Config{
ProfilesJSON: `{"p":{"models":["plugin/answerer"]}}`,
PluginSampler: sampler,
})
if err != nil {
t.Fatalf("Resolve(profile naming plugin): %v", err)
}
resp, err = p.Generate(context.Background(), Request{Model: "p"})
if err != nil || resp.Text != "from the plugin answerer" {
t.Errorf("profile plugin Generate = %+v, %v; want the plugin answerer's text", resp, err)
}
}

func TestResolvePluginAndSamplingWithoutPath(t *testing.T) {
if _, err := Resolve(Config{Provider: "plugin"}); !errors.Is(err, ErrUnavailable) {
t.Errorf("plugin: err = %v, want ErrUnavailable (backend is a later slice, #47)", err)
t.Errorf("plugin without a plugin sampler: err = %v, want ErrUnavailable (no plugin answerer path installed)", err)
}
if _, err := Resolve(Config{Provider: "sampling"}); !errors.Is(err, ErrUnavailable) {
t.Errorf("sampling without a sampler: err = %v, want ErrUnavailable", err)
Expand Down
47 changes: 47 additions & 0 deletions pkgs/mcpx/internal/model/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package model

import (
"context"
"fmt"
)

// PluginProvider is the opencode-plugin backend: a Provider whose
// generations are answered by the plugin answerer through the same
// elicitation broker the sampling backend uses (#1925). Triage on
// #1925 (task 13246) verified the broker already carries
// daemon-initiated model requests to an answerer -- brokerSampler
// opens an elicit.SampleAsk question, publishes sample.opened before
// awaiting, and maps a non-accept answer to ErrUnavailable -- so the
// plugin backend is a different sampler binding (its questions are
// labelled model.provider=plugin and its answers are stamped with
// this provider's name), not a new transport. The daemon installs
// the binding as Config.PluginSampler; the v2 plugin work it was
// once said to depend on ("#47") was a stale reference -- #47 in
// this repo is an unrelated Dependabot PR, and the plugin-port docs
// it meant landed via #1855.
type PluginProvider struct {
fn SampleFunc
}

// NewPluginProvider wraps fn, the daemon's plugin sampler binding. A
// nil fn is a provider that is never available, which is the honest
// state of a daemon with no plugin answerer path installed.
func NewPluginProvider(fn SampleFunc) *PluginProvider {
return &PluginProvider{fn: fn}
}

func (p *PluginProvider) Name() string { return "plugin" }

func (p *PluginProvider) Generate(ctx context.Context, req Request) (Response, error) {
if p.fn == nil {
return Response{}, fmt.Errorf("model: plugin: %w (no plugin answerer path installed)", ErrUnavailable)
}
resp, err := p.fn(ctx, req)
if err != nil {
return Response{}, err
}
if resp.Provider == "" {
resp.Provider = p.Name()
}
return resp, nil
}
29 changes: 21 additions & 8 deletions pkgs/mcpx/internal/model/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type Config struct {
Timeout time.Duration
// Sampler is the broker sampling path, when the daemon has one.
Sampler SampleFunc
// PluginSampler is the plugin sampler binding over the same
// broker, when the daemon has one: the path model.provider=plugin
// resolves through. Nil means no plugin answerer path is
// installed, and plugin resolves to a precise ErrUnavailable.
PluginSampler SampleFunc
// LookupEnv reads the environment; nil means os.Getenv. A seam for
// tests, which must not depend on the machine's real variables.
LookupEnv func(string) string
Expand Down Expand Up @@ -74,8 +79,8 @@ var Providers = []string{"auto", "sampling", "plugin", "openai", "anthropic", "g
// none (the daemon default), so nothing calls a model and the caller
// stays on its deterministic path -- no behaviour change for a user who
// configures nothing. An error means a provider was named but cannot be
// built (the plugin backend is not built yet; sampling was asked for
// with no sampling path installed); callers treat it as unavailable and
// built (sampling or plugin was asked for with no answerer path
// installed); callers treat it as unavailable and
// fall back the same way.
func Resolve(cfg Config) (Provider, error) {
lookup := cfg.LookupEnv
Expand Down Expand Up @@ -106,11 +111,16 @@ func Resolve(cfg Config) (Provider, error) {
}
return NewSamplingProvider(cfg.Sampler), nil
case "plugin":
// The opencode-plugin backend answers through the v2 plugin's
// ctx.generate.text and depends on #47; it is a later slice of
// #1925, named in the enum now so configurations written for it
// fail with this sentence rather than a bare "unknown provider".
return nil, fmt.Errorf("model: provider plugin: %w (the opencode-plugin backend is not built yet; it depends on #47)", ErrUnavailable)
// The plugin backend is a sampler binding over the elicitation
// broker (#1925, task 13246): the daemon installs it as
// PluginSampler when it can carry questions to a plugin
// answerer. Without that path the honest answer is a precise
// unavailable, not the stale "#47" dependency this case used
// to cite -- #47 is an unrelated closed Dependabot PR.
if cfg.PluginSampler == nil {
return nil, fmt.Errorf("model: provider plugin: %w (no plugin answerer path installed)", ErrUnavailable)
}
return NewPluginProvider(cfg.PluginSampler), nil
case "openai", "anthropic", "google":
name := strings.ToLower(strings.TrimSpace(cfg.Provider))
return configured(cfg, name, lookup), nil
Expand Down Expand Up @@ -238,7 +248,10 @@ func (r *profileRouter) provider(name string) (Provider, error) {
return NewSamplingProvider(r.cfg.Sampler), nil
}
if name == "plugin" {
return nil, fmt.Errorf("model: provider plugin: %w", ErrUnavailable)
if r.cfg.PluginSampler == nil {
return nil, fmt.Errorf("model: provider plugin: %w (no plugin answerer path installed)", ErrUnavailable)
}
return NewPluginProvider(r.cfg.PluginSampler), nil
}
var settings providerSettings
configured := false
Expand Down
7 changes: 4 additions & 3 deletions pkgs/mcpx/internal/settings/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func modelSettings() []Setting {
"declare it). openai, anthropic and google are configured " +
"providers reached through one OpenAI-compatible client, so " +
"model.baseURL can point them at a gateway, LiteLLM, Ollama or " +
"LM Studio instead. plugin is the opencode-plugin backend, a " +
"later slice that depends on #47; naming it resolves to " +
"unavailable until it lands. auto prefers a live sampling " +
"LM Studio instead. plugin is the plugin backend: a sampler " +
"binding over the same broker, labelled for the plugin " +
"answerer; naming it resolves to unavailable when the daemon " +
"has no plugin answerer path installed. auto prefers a live sampling " +
"path, then a configured provider, then fails cleanly.",
},
{
Expand Down
Loading