diff --git a/internal/gateway/cline_client_test.go b/internal/gateway/cline_client_test.go new file mode 100644 index 000000000..66e860ea1 --- /dev/null +++ b/internal/gateway/cline_client_test.go @@ -0,0 +1,67 @@ +package gateway + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// Cline's free models (cline-free/…) answer 403 "only available via Cline +// product surfaces" unless the request says it is Cline's: requests to the +// Cline API carry its desktop app's headers, with the user's key, and those +// to anyone else don't (lml and ARNO on Discord). +func TestClineClientHeaders(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + cline, err := provider.FromPreset("clinepass") + if err != nil { + t.Fatal(err) + } + cline.ID, cline.Key, cline.Models = "cline", "clp_key", []string{"cline-free/deepseek-v4.1-flash"} + for _, p := range []provider.Provider{cline, + {ID: "other", Name: "Other", Key: "k", Chat: "https://other.test/v1", Models: []string{"cline-free/deepseek-v4.1-flash"}}, + } { + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + } + got := map[string]http.Header{} + s := New() + s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) { + got[r.URL.Host] = r.Header.Clone() + return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}}, + Body: io.NopCloser(strings.NewReader(`{"id":"c","object":"chat.completion","model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"pong"},"finish_reason":"stop"}]}`))}, nil + })} + for _, id := range []string{"cline", "other"} { + req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+id+`/cline-free/deepseek-v4.1-flash","messages":[{"role":"user","content":"Say pong"}]}`)) + req.Header.Set("User-Agent", "some-agent/1.0") + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + if rec.Code != 200 { + t.Fatalf("%s: %d %s", id, rec.Code, rec.Body) + } + } + h, o := got["api.cline.bot"], got["other.test"] + if h == nil || o == nil { + t.Fatalf("requests: %v", got) + } + want := map[string]string{ + "HTTP-Referer": "https://cline.bot", "X-Title": "Cline", "X-IS-MULTIROOT": "false", + "X-CLIENT-TYPE": "cline-desktop", "X-CLIENT-VERSION": provider.ClineVersion, + "User-Agent": "Cline/" + provider.ClineVersion, "Authorization": "Bearer clp_key", + } + for k, v := range want { + if h.Get(k) != v { + t.Errorf("cline: %s %q, want %q", k, h.Get(k), v) + } + } + for _, k := range []string{"X-CLIENT-TYPE", "X-Title", "X-IS-MULTIROOT"} { + if o.Get(k) != "" { + t.Errorf("other provider got %s", k) + } + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index f812ad524..0b096be9d 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -1616,6 +1616,11 @@ func (s *Server) forwardOnce(ctx context.Context, p provider.Provider, to provid // as OpenCode itself sends it, which Zen's free tier asks for provider.OpenCodeClient(req.Header, conversationID(in, body)) } + if p.IsCline() { + // as Cline's desktop app sends it: its free models are 403'd + // ("only available via Cline product surfaces") without + provider.ClineClient(req.Header) + } if p.Account != nil && p.Account.Agent == "codex" { // what Codex says about the request goes on as codexUpstream // relays it — a subagent's kind, the turn's metadata, a turn on diff --git a/internal/provider/cline.go b/internal/provider/cline.go new file mode 100644 index 000000000..e1d20a4b1 --- /dev/null +++ b/internal/provider/cline.go @@ -0,0 +1,171 @@ +package provider + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "strings" + "time" + + "github.com/yetone/magpie/internal/catalog" +) + +// ClineVersion is the Cline desktop release magpie says it is to the Cline +// API. +const ClineVersion = "3.5.54" + +// clineHeaders are what Cline's desktop app sends the Cline API (cline/cline +// sdk/packages/llms/src/providers/cline-client-headers.ts, with the +// desktop's client type). The cline-free/ models are answered 403 "only +// available via Cline product surfaces" without them, and X-CLIENT-TYPE is +// what the recommended-models feed keys its free list on: cline-desktop is +// given the desktop's. +var clineHeaders = map[string]string{ + "HTTP-Referer": "https://cline.bot", + "X-Title": "Cline", + "X-IS-MULTIROOT": "false", + "X-CLIENT-TYPE": "cline-desktop", + "X-CLIENT-VERSION": ClineVersion, + "User-Agent": "Cline/" + ClineVersion, +} + +// ClineClient makes a request to the Cline API carry the desktop app's +// headers. +func ClineClient(h http.Header) { + for k, v := range clineHeaders { + h.Set(k, v) + } +} + +// IsCline reports whether the provider is the Cline API: made from the +// ClinePass preset, or at Cline's host. +func (p Provider) IsCline() bool { + if p.Preset == "clinepass" { + return true + } + h := p.Host() + return h == "cline.bot" || strings.HasSuffix(h, ".cline.bot") +} + +// clineFreePrefix is what the ids of Cline's free models start with; one +// the feed's free list names may not (stealth/space-bunny-alpha). +const clineFreePrefix = "cline-free/" + +// clineFeed is the list Cline's own clients take their pickers from: +// GET {base}/ai/cline/recommended-models, asked with the client's headers +// and no key. Its clinePass models are the plan's, its free ones are served +// at no cost and apart from the plan's quota ("Try with limited usage, +// separate from ClinePass quota"). Paid models are left out, as the +// plan's preset leaves them out of the API's list. +func (p Provider) clineFeed(ctx context.Context) ([]catalog.Model, string, error) { + u := strings.TrimRight(strings.TrimSpace(p.Chat), "/") + "/ai/cline/recommended-models" + ctx, cancel := context.WithTimeout(ctx, 8*time.Second) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) + if err != nil { + return nil, u, err + } + req.Header.Set("Accept", "application/json") + ClineClient(req.Header) + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, u, err + } + defer res.Body.Close() + b, _ := io.ReadAll(io.LimitReader(res.Body, 4<<20)) + if res.StatusCode != http.StatusOK { + return nil, u, errorf("%s: HTTP %d", u, res.StatusCode) + } + type entry struct { + ID string `json:"id"` + Name string `json:"name"` + } + var feed struct { + ClinePass []entry `json:"clinePass"` + Free []entry `json:"free"` + } + if err := json.Unmarshal(b, &feed); err != nil { + return nil, u, err + } + var ms []catalog.Model + add := func(e entry, free bool) { + if e.ID == "" { + return + } + for _, m := range ms { + if m.ID == e.ID { + return + } + } + name := e.Name + if name == "" || free && name == e.ID { + name = clineFreeName(e.ID) + } + ms = append(ms, catalog.Model{ID: e.ID, Name: name, Free: free}) + } + for _, e := range feed.ClinePass { + add(e, false) + } + for _, e := range feed.Free { + add(e, true) + } + if len(ms) == 0 { + return nil, u, errors.New(u + ": no models listed") + } + return p.planModels(ms), u, nil +} + +// clineFree are Cline's free models as its desktop app listed them +// (2026-10-02; cline/cline sdk/packages/llms/src/catalog/ +// cline-recommended.generated.ts), for when the feed wasn't had. +var clineFree = []catalog.Model{ + {ID: "cline-free/deepseek-v4.1-flash", Name: "DeepSeek V4.1 Flash"}, + {ID: "stealth/space-bunny-alpha", Name: "Space Bunny Alpha"}, + {ID: "cline-free/mimo-v2.6-flash", Name: "MiMo-V2.6-Flash"}, + {ID: "cline-free/muse-spark-1.3-contributor", Name: "Muse Spark 1.3 Contributor"}, +} + +// clineFreeName is the fallback list's name for a free model, else its id. +func clineFreeName(id string) string { + for _, m := range clineFree { + if m.ID == id { + return m.Name + } + } + return id +} + +// isClineFree reports whether id is a free model by its id alone: a +// cline-free/ one, or one the fallback names. +func isClineFree(id string) bool { + if strings.HasPrefix(id, clineFreePrefix) { + return true + } + for _, m := range clineFree { + if m.ID == id { + return true + } + } + return false +} + +// clineFreeModel reports whether model is one of Cline's free models on +// p: one the fetched list marks free, else one free by its id. +func (p Provider) clineFreeModel(model string) bool { + if !p.IsCline() { + return false + } + if strings.HasPrefix(model, clineFreePrefix) { + return true + } + if ms, _, ok := p.live(); ok { + for _, m := range ms { + if m.ID == model { + return m.Free + } + } + } + return isClineFree(model) +} diff --git a/internal/provider/clinepass_test.go b/internal/provider/clinepass_test.go index fd386267c..464ace302 100644 --- a/internal/provider/clinepass_test.go +++ b/internal/provider/clinepass_test.go @@ -1,7 +1,10 @@ package provider import ( - "strings" + "context" + "net/http" + "net/http/httptest" + "slices" "testing" "github.com/yetone/magpie/internal/catalog" @@ -12,28 +15,30 @@ func TestPlanModels(t *testing.T) { if err != nil { t.Fatal(err) } - // the Cline API's list: its paid models, one of the plan's among them - got := p.planModels([]catalog.Model{{ID: "anthropic/claude-opus-5-5"}, {ID: "cline-pass/glm-5.3"}}) - if len(got) != 1 || got[0].ID != "cline-pass/glm-5.3" { + n := len(Preset("clinepass").Models) + // the Cline API's list: its paid models, one of the plan's among them; + // its ":free" ones aren't what Cline offers free, and are left out + got := p.planModels([]catalog.Model{{ID: "anthropic/claude-opus-5-5"}, {ID: "cline-pass/glm-5.3"}, {ID: "qwen/qwen3.8-27b:free"}}) + if len(got) != 1+len(clineFree) || got[0].ID != "cline-pass/glm-5.3" || got[1].ID != "cline-free/deepseek-v4.1-flash" { t.Fatalf("kept: %+v", got) } - // none of the plan's listed: the plan's own - if got := p.planModels([]catalog.Model{{ID: "x/y"}}); len(got) != len(Preset("clinepass").Models) || got[0].ID != "cline-pass/glm-5.3" { + // none of the plan's listed: the plan's own, then Cline's free models + // as its desktop app lists them (lml on Discord) + got = p.planModels(nil) + if len(got) != n+len(clineFree) || got[0].ID != "cline-pass/glm-5.3" { t.Fatalf("plan's: %+v", got) } - // the Cline API's free models are kept beside the plan's, marked free, - // while its paid ones are still left out (lml on Discord) - got = p.planModels([]catalog.Model{{ID: "anthropic/claude-opus-5-5"}, {ID: "qwen/qwen3.8-27b:free"}, {ID: "google/gemma-4-31b-it:free"}}) - if n := len(Preset("clinepass").Models); len(got) != n+2 || got[0].ID != "cline-pass/glm-5.3" || got[n].ID != "qwen/qwen3.8-27b:free" || got[n+1].ID != "google/gemma-4-31b-it:free" { - t.Fatalf("free: %+v", got) - } + var free []string for _, m := range got { - if m.Free != strings.HasSuffix(m.ID, ":free") { - t.Fatalf("free flag: %+v", m) + if m.Free { + free = append(free, m.ID) } } - // a free model costs nothing, not its tagless model's price - if pr, ok := p.ListPrice("qwen/qwen3.8-27b:free"); !ok || pr != (catalog.Price{}) { + if want := []string{"cline-free/deepseek-v4.1-flash", "stealth/space-bunny-alpha", "cline-free/mimo-v2.6-flash", "cline-free/muse-spark-1.3-contributor"}; !slices.Equal(free, want) { + t.Fatalf("free: %v", free) + } + // a free model costs nothing, not its paid model's price + if pr, ok := p.ListPrice("cline-free/deepseek-v4.1-flash"); !ok || pr != (catalog.Price{}) { t.Fatalf("free price: %+v %v", pr, ok) } // another provider's list is left as it is @@ -42,3 +47,64 @@ func TestPlanModels(t *testing.T) { t.Fatalf("other: %+v", got) } } + +// Cline's clients take their plan and free models from the +// recommended-models feed, asked as the desktop app (whose client type is +// given the desktop's free list) and with no key; ClinePass lists both, +// the free ones marked Free and priced at nothing, and no paid model. +func TestClineFeed(t *testing.T) { + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + var asked *http.Request + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/ai/cline/recommended-models" { + http.NotFound(w, r) + return + } + asked = r + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(`{"recommended":[{"id":"anthropic/claude-opus-5.5","name":"claude-opus-5.5"}], + "free":[{"id":"cline-free/deepseek-v4.1-flash","name":"Deepseek-v4.1-Flash"},{"id":"stealth/space-bunny-alpha","name":"space-bunny-alpha"}, + {"id":"stealth/new-free-one","name":"stealth/new-free-one"}], + "clinePass":[{"id":"cline-pass/glm-5.3","name":"cline-pass/glm-5.3"},{"id":"cline-pass/mimo-v2.6-pro","name":"cline-pass/mimo-v2.6-pro"}]}`)) + })) + defer srv.Close() + p, err := FromPreset("clinepass") + if err != nil { + t.Fatal(err) + } + p.ID, p.Key, p.Chat = "cline-test", "clp_secret", srv.URL+"/api/v1" + ms, err := p.Fetch(context.Background()) + if err != nil { + t.Fatal(err) + } + if asked == nil { + t.Fatal("the feed wasn't asked") + } + if asked.Header.Get("X-CLIENT-TYPE") != "cline-desktop" || asked.Header.Get("User-Agent") != "Cline/"+ClineVersion || asked.Header.Get("Authorization") != "" { + t.Fatalf("feed asked with %v", asked.Header) + } + var ids, free []string + for _, m := range ms { + ids = append(ids, m.ID) + if m.Free { + free = append(free, m.ID) + } + } + if want := []string{"cline-pass/glm-5.3", "cline-pass/mimo-v2.6-pro", "cline-free/deepseek-v4.1-flash", "stealth/space-bunny-alpha", "stealth/new-free-one"}; !slices.Equal(ids, want) { + t.Fatalf("listed %v", ids) + } + if want := []string{"cline-free/deepseek-v4.1-flash", "stealth/space-bunny-alpha", "stealth/new-free-one"}; !slices.Equal(free, want) { + t.Fatalf("free %v", free) + } + // a free model the feed named and no id gives away costs nothing too, + // a plan's model is not called free + for _, id := range []string{"stealth/new-free-one", "stealth/space-bunny-alpha"} { + if pr, ok := p.ListPrice(id); !ok || pr != (catalog.Price{}) { + t.Fatalf("%s: %+v %v", id, pr, ok) + } + } + if p.clineFreeModel("cline-pass/glm-5.3") { + t.Fatal("a plan's model is free") + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index 1f480d014..9b8afb935 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -140,6 +140,13 @@ func (p Provider) Fetch(ctx context.Context) ([]catalog.Model, error) { if pr := Preset(p.Preset); pr != nil && pr.NoList && strings.TrimSpace(p.ModelsURL) == "" && !p.listRegion(pr) { return catalog.Chat(p.planModels(nil)), nil } + // Cline's plan and free models, from the list its own clients take + // theirs from; the API's list has neither, and is asked if that fails + if p.IsCline() && strings.TrimSpace(p.ModelsURL) == "" { + if ms, base, err := p.clineFeed(ctx); err == nil { + return catalog.Chat(ms), catalog.SaveLive(p.ID, base, ms) + } + } // Only keys in use. An off key is not asked, and its list does not // join the catalog or take capabilities off a key that is on. if keys := p.KeysOn(); len(keys) > 1 { @@ -313,8 +320,8 @@ func (p Provider) planModels(ms []catalog.Model) []catalog.Model { switch { case strings.HasPrefix(m.ID, pr.Only): out = append(out, m) - case p.freeTagged(m.ID): - // the vendor's free models, which the key is served too + case p.IsCline() && (m.Free || isClineFree(m.ID)): + // Cline's free models, served apart from the plan's quota m.Free = true free = append(free, m) } @@ -324,26 +331,30 @@ func (p Provider) planModels(ms []catalog.Model) []catalog.Model { out = append(out, catalog.Model{ID: id, Name: id}) } } + if len(free) == 0 && p.IsCline() { + // as Cline's desktop app last listed them + for _, m := range clineFree { + m.Free = true + free = append(free, m) + } + } out = append(out, free...) for i, m := range out { // the vendor's window for its model, as models.dev has it + id := strings.TrimPrefix(m.ID, pr.Only) + if m.Free { + id = m.ID[strings.LastIndex(m.ID, "/")+1:] + } if m.Context == 0 { - out[i].Context = catalog.ContextOf(strings.TrimPrefix(m.ID, pr.Only)) + out[i].Context = catalog.ContextOf(id) } if m.Output == 0 { - out[i].Output = catalog.OutputOf(strings.TrimPrefix(m.ID, pr.Only)) + out[i].Output = catalog.OutputOf(id) } } return out } -// freeTagged reports whether model is one of the vendor's free models p's -// preset keeps beside its plan's (PresetDef.FreeTag): Cline's ":free" ones. -func (p Provider) freeTagged(model string) bool { - pr := Preset(p.Preset) - return pr != nil && pr.Only != "" && pr.FreeTag != "" && strings.HasSuffix(model, pr.FreeTag) -} - // fixV1 adds the /v1 an OpenAI-style base URL was given without, when // the models were found only under it: base/models didn't answer and // base/v1/models did. The list is asked for at both, but a request goes @@ -611,8 +622,8 @@ var makerCatalogs = sync.OnceValue(func() []string { // at gpt-6-astra's or gemini-3.8-flash's maker's price, as a Claude // account is at Anthropic's. func (p Provider) ListPrice(model string) (catalog.Price, bool) { - if p.freeTagged(model) { - // served at no cost: not at the price of the model it is a tag of + if p.clineFreeModel(model) { + // served at no cost: not at the price of the model it is free of return catalog.Price{}, true } for _, m := range pricedNames(model) { diff --git a/internal/provider/presets.go b/internal/provider/presets.go index 2a0fbf2d8..5e6883123 100644 --- a/internal/provider/presets.go +++ b/internal/provider/presets.go @@ -39,10 +39,6 @@ type PresetDef struct { // Models are the plan's, for when the list has none of them. Only string `json:"only,omitempty"` Models []string `json:"models,omitempty"` - // FreeTag, with Only, is what the ids of the vendor's free models end - // in: they are kept beside the plan's, marked Free and priced at - // nothing (Cline's ":free" models, which its key is served at no cost). - FreeTag string `json:"freeTag,omitempty"` // NoList: the vendor has no list of models to ask for (Bedrock's // runtime serves no /models), so Models are its list NoList bool `json:"noList,omitempty"` @@ -289,13 +285,14 @@ var presets = []PresetDef{ Note: "open coding models, $10/month", Website: "https://opencode.ai/docs/go", KeysURL: "https://opencode.ai/auth"}, // Cline's plan for open models, at the Cline API with a Cline key: the - // API lists only its paid models, not the plan's, and its free ones - // (":free"), which the key is served too + // API lists only its paid models, not the plan's; the plan's and + // Cline's free models come from the feed Cline's own clients list + // them from (cline.go) {ID: "clinepass", Name: "ClinePass", Icon: "cline", Kind: KindRelay, Chat: "https://api.cline.bot/api/v1", Note: "open coding models, $9.99/month", Website: "https://docs.cline.bot/getting-started/clinepass", KeysURL: "https://app.cline.bot", - Only: "cline-pass/", FreeTag: ":free", + Only: "cline-pass/", Models: []string{"cline-pass/glm-5.3", "cline-pass/glm-5.3-flash", "cline-pass/kimi-k3", "cline-pass/deepseek-v4-pro", "cline-pass/deepseek-v4.1-flash", "cline-pass/mimo-v2.5", "cline-pass/mimo-v2.5-pro", "cline-pass/minimax-m3", "cline-pass/muse-spark-1.3-contributor", "cline-pass/qwen3.8-max", "cline-pass/qwen3.7-max", "cline-pass/qwen3.7-plus"}}, diff --git a/internal/provider/test.go b/internal/provider/test.go index ed21e75d8..8e6b9ec20 100644 --- a/internal/provider/test.go +++ b/internal/provider/test.go @@ -296,6 +296,9 @@ func probe(ctx context.Context, p Provider, proto Protocol, url string, body []b if p.IsOpenCode() { OpenCodeClient(req.Header, "") } + if p.IsCline() { + ClineClient(req.Header) + } if err := p.Sign(ctx, req, proto, body); err != nil { r.Error = err.Error() return r