WorkBuddy's model list read "GLM-5.3:workbuddy/glm-5.3" with Provider in
model names Off. WorkBuddy (5.5.6 renderer, getModelDisplayName) shows
every custom model as "name:id" unless the name is empty or equal to
the id, and magpie wrote the catalog id (provider/model) as the id. So
the provider came back after the colon whatever the names said.
- agent (buddyIDs): for WorkBuddy and WorkBuddy AI under Off or Own,
each magpie entry in models.json gets the name as its id too, so the
list shows "GLM-5.3" alone. The catalog id stays when two models
share a name, when the name is another model's id, when the entries
point at a magpie on another machine or in WSL, and in On mode.
A model turned off in WorkBuddy, its default effort and a reply limit
typed in carry across the change of id: an old id is mapped back
through every label form (Label, PlainName, the list's name).
- provider.AgentNamed: the catalog id of the one model an agent's list
names so.
- gateway (namedFor): a request with WorkBuddy's or WorkBuddy AI's key
for such a name goes to that model before any other resolution, so a
bare id that another provider serves first is not asked instead. The
reply keeps the name that was asked for. Other agents' keys are not
mapped.
Siblings: CodeBuddy Code shares the writer and keeps catalog ids. Its
settings.json names the model by the catalog id (codebuddy.go), and its
CLI picker shows the name alone (no name:id template in its bundle).
What users meet: a change of mode, or of a model's name, changes the
id. WorkBuddy then picks another model for a session that had the old
one, as it does for any model that leaves its list.
Run:
- Unit tests: TestWorkBuddyNamesAreIDsWithoutProvider covers Off, Own,
back to On, WorkBuddy AI and CodeBuddy. TestWorkBuddyAsksByName uses
the real handler: a name equal to a bare id that a provider listed
first serves (hidden from WorkBuddy) reaches the named model, and
OpenCode's key for the same name does not.
- Broken by hand (on mica), each red:
- buddyIDs always returning the catalog id: the test sees
id deepseek/pro under Off.
- namedFor's mapping skipped: the request went to provider "first".
- The old-id mapping skipped: Own lost disabled.
- Sandbox HOME, built binary:
- magpie provider add zai (fake upstream) and magpie workbuddy
provider magpie wrote "zai/glm-5.3 | GLM-5.3 · Z.ai".
- magpie model suffix workbuddy off rewrote the file to
"GLM-5.3 | GLM-5.3".
- magpie serve on a spare port, then a curl with magpie-workbuddy for
"GLM-5.2" reached upstream as glm-5.2 (reply model GLM-5.2).
- WorkBuddy 5.5.6's own CLI (app.asar.unpacked/cli/bin/codebuddy -p
--model GLM-5.3), with HOME set to the sandbox, answered "pong", and
upstream saw glm-5.3, streamed.
- Full suite on mote at this tree (go test -count=1 -timeout 45m ./...):
every package ok (agent 369s, gateway 1062s, provider 230s,
sessions 254s), log kept whole. The doc line in agent-wiring.md was
added after the run; no test reads that file. gofmt -l is empty,
and go vet ./... passes. On mica, GOOS=linux go vet -tags nogui
./..., GOOS=windows go vet ./..., and the linux nogui and windows
builds pass.
- That suite ran at base ac0b5c287. The change was then rebased onto
5211fd3ba (usage currency; no file in common). At the pushed head,
on mica: go build, go vet of agent, gateway and provider, and
-run "WorkBuddy|CodeBuddy|AgentSuffix|Labels" in those three
packages all pass.
Not tried: WorkBuddy's window itself (the name:id rule is read from
its renderer source, not seen on screen), and WorkBuddy AI's app.
99 lines
3.6 KiB
Go
99 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// With the provider left out of WorkBuddy's names, magpie gives it each
|
|
// model's name as its id (buddyIDs in internal/agent), as WorkBuddy shows
|
|
// a custom model "name:id" (elis_pina on X). WorkBuddy asks for the model
|
|
// by that name, which reaches the model it names, before a model another
|
|
// provider serves under the same bare id. Any
|
|
// other agent's request for that name is not taken to it.
|
|
func TestWorkBuddyAsksByName(t *testing.T) {
|
|
fresh(t)
|
|
var mu sync.Mutex
|
|
got := map[string][]string{}
|
|
upstream := func(name string) *httptest.Server {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
mu.Lock()
|
|
got[name] = append(got[name], gjson.GetBytes(b, "model").String())
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{"id":"c","model":"x","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}`)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
first := upstream("first").URL + "/v1"
|
|
if err := provider.Save(provider.Provider{ID: "first", Name: "First", Key: "k", Models: []string{"other"}, Chat: first}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "second", Name: "Second", Key: "k", Models: []string{"glm-5.3"}, Chat: upstream("second").URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SetAgentSuffix("workbuddy", provider.SuffixOff); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
shown, _ := provider.CatalogFor("workbuddy")
|
|
var name string
|
|
for i, l := range provider.LabelsFor("workbuddy", shown) {
|
|
if shown[i].ID == "second/glm-5.3" {
|
|
name = l
|
|
}
|
|
}
|
|
if name == "" || strings.Contains(name, "Second") {
|
|
t.Fatalf("name %q", name)
|
|
}
|
|
// the provider listed first serves a model under that same bare id,
|
|
// hidden from WorkBuddy's list, which a bare id finds first
|
|
if err := provider.Save(provider.Provider{ID: "first", Name: "First", Key: "k", Models: []string{name}, Chat: first}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SetHiddenModels("workbuddy", []string{"first/" + name}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if p, _, ok := provider.Resolve(name); !ok || p.ID != "first" {
|
|
t.Fatalf("setup: a bare %q goes to %q, not the provider listed first", name, p.ID)
|
|
}
|
|
if id, ok := provider.AgentNamed("workbuddy", name); !ok || id != "second/glm-5.3" {
|
|
t.Fatalf("AgentNamed(%q) = %q, %v", name, id, ok)
|
|
}
|
|
handler := lanGuard(New().Handler())
|
|
send := func(key string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+name+`","messages":[{"role":"user","content":"hi"}]}`))
|
|
req.RemoteAddr = "127.0.0.1:5000"
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("User-Agent", "CLI/2.161.0 WorkBuddy/2.161.0")
|
|
req.Header.Set("Authorization", "Bearer "+key)
|
|
rec := httptest.NewRecorder()
|
|
handler.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
rec := send(TokenFor("workbuddy"))
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status %d: %s", rec.Code, rec.Body)
|
|
}
|
|
mu.Lock()
|
|
if len(got["second"]) != 1 || got["second"][0] != "glm-5.3" || len(got["first"]) != 0 {
|
|
t.Fatalf("asked for %q, upstreams saw %v", name, got)
|
|
}
|
|
mu.Unlock()
|
|
// another agent's key: the name is no id of its list's
|
|
send(TokenFor("opencode"))
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(got["second"]) != 1 || len(got["first"]) != 1 {
|
|
t.Fatalf("another agent's request was taken by WorkBuddy's names: %v", got)
|
|
}
|
|
}
|