Files
magpie/internal/provider/cline.go
T
yetone c819e00c4e routing: a ClinePass route reads as its route, with what was asked for (zRain)
zRain (Discord #feedback, 10-24; magpie 0.1.1377, Windows, Codex CLI
0.164.0) asked clinepass/cline-pass/deepseek-v4.1-flash-1017 from Codex
(/backend-api/codex/responses -> /v1/chat/completions, 200). The reply
named vmc/deepseek-v4p1-flash-1017-contributor-fallbacks. Routing showed
"served vmc/..." titled "likely another model", with no upstream.

What was found:
- The pin goes out on Codex's Responses path the same as on a Chat
  client's. The test below checks the sent body on both paths. The
  built-in ClinePass pins only with "DeepSeek and GLM models only from
  their makers' own APIs" ticked. The Cline plugin pins Pass DeepSeek by
  default. zRain's id is the built-in, so with the tick off nothing asked
  for DeepSeek.
- upstreamOf reads finalProvider from choices[].delta/message
  provider_metadata on every path (Codex streamed, chat streamed,
  Responses streamed and not). zRain's rows had none, so the reply most
  likely carried none there. canonicalSlug is not recorded; left alone.
- The contributor-fallbacks name is ClinePass's own route. Its name does
  not mean another model.

Change:
- provider.ClinePassAsked: the upstream a request asks the Cline API for
  (ClinePin with the tick, or deepseek on the plugin's provider).
- Try.Pin records it on the answering try.
- Routing: a cline-pass/ model served as vmc/... is tagged "ClinePass
  route ...". Its title and story say it is the route's name, not another
  model's, and then one of:
  - who answered, when the reply said;
  - that the asked provider wasn't confirmed;
  - that nothing was asked, naming the tick for DeepSeek/GLM.
- The Usage page's Requests title says the route too, without the pin it
  doesn't keep.
- Strings in zh, zh-TW, ja and de.

What was run (rebased on origin/main 7edf5b0a3):
- TestClinePassAsked (provider) and TestClinePassTryKeepsWhatWasAsked
  (gateway: chat and responses, tick on/off, sent body and try pin).
- Broken by hand: the try literal without Pin, and the tick-on cases go
  red ("try pin \"\" (want \"deepseek\")").
- go test -count=1 ./internal/provider in a sandbox HOME: ok (280s).
- ./internal/gateway -run 'Cline|Upstream|Swap|Served|Try|Trace|Route':
  ok.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows on gateway
  and provider.
- go test -run 'Budget|I18n|Strings|Plugin' ./internal/gui: ok.
- GUI: routing-cline-route.test.cjs, new, Chromium+WebKit, en+zh, every
  string in all 4 languages. Pointed at the old assets
  (MAGPIE_ROUTE_ASSETS): 4 of 4 red. Now 4 of 4 green.
- GUI: every routing-*, usage-* and gui-* file, plus i18n-keys,
  panel-routing, remote-group-served and session-identity, at
  concurrency 3, load ~110. Reds: routing-held-dot (WebKit zh 560px,
  timeout; passes alone at the rebased head) and routing-intent-entry
  "Add a rule makes a rule by intent" (also red on origin/main 117937b8d
  in WebKit alone: #3408).

Not tried:
- No real ClinePass account. The raw bytes of a contributor-fallbacks
  reply are unseen, so whether ClinePass puts finalProvider elsewhere for
  that route is unknown. No parser change was made without them.
- Full go test ./... was not run: the machine is at load ~110 with other
  sessions' suites.
- Not run on the Windows box.
2026-10-24 19:21:02 +08:00

272 lines
9.3 KiB
Go

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 := p.liveByID(model); len(ms) > 0 {
return ms[0].Free
}
return isClineFree(model)
}
// ClinePinnable reports whether p's requests reach Cline's API and can be
// pinned to an upstream (PinUpstream): the ClinePass provider, one on
// cline.bot, or the Cline plugin's (@magpie-community/opencode-cline-auth),
// whose requests magpie writes before the plugin sends them on.
func (p Provider) ClinePinnable() bool {
return p.IsCline() || p.PluginProvider() == "cline"
}
// clineMakers maps a model name to its maker's own upstream on the
// router Cline sends it through. Most go through Vercel's AI Gateway,
// pinned by providerOptions.gateway.only with Vercel's provider slug
// (ai-gateway.vercel.sh/v1/models/<model>/endpoints lists "deepseek" for
// deepseek/deepseek-v4-pro and "zai" for zai/glm-5.3). Some go through
// OpenRouter directly, which reads only a top-level provider.only with
// OpenRouter's slug (openrouter.ai/api/v1/providers: Z.AI is "z-ai"):
// sdytman (#feedback, 10-20) found glm-5.3-flash among them, and
// deepseek-v4.1-flash and deepseek-v4.1-flash-1017 going through Vercel.
// An exact entry is a model name; the others match a name's start, the
// first match winning.
var clineMakers = []struct {
name string
exact bool
upstream string
openRouter bool
}{
{name: "glm-5.3-flash", exact: true, upstream: "z-ai", openRouter: true},
{name: "deepseek", upstream: "deepseek"},
{name: "glm", upstream: "zai"}, // sdytman on Discord: GLM only through Z.ai
}
// ClinePin is the upstream a request for model is pinned to on p
// (PinUpstream), and whether Cline sends the model through OpenRouter
// (pinned by provider.only) rather than Vercel's AI Gateway (pinned by
// providerOptions.gateway.only): "deepseek" for a DeepSeek model of the
// Cline API, "zai" for a GLM one, "z-ai" through OpenRouter for
// glm-5.3-flash, "" for anything else. Cline's free models are served
// where Cline puts them, so they never are.
func (p Provider) ClinePin(model string) (upstream string, openRouter bool) {
if !p.PinUpstream || !p.ClinePinnable() || strings.HasPrefix(model, clineFreePrefix) {
return "", false
}
// the Cline plugin pins ClinePass's DeepSeek models itself, by default,
// with ClinePassHas's filter and a way back when the pin can't be
// served (opencode-cline-auth 0.1.11). It keeps a body's own
// providerOptions, so magpie's only alone would stand in its place.
if p.PluginProvider() == "cline" && clinePassDeepSeek(model) {
return "", false
}
name := strings.ToLower(model[strings.LastIndex(model, "/")+1:])
for _, m := range clineMakers {
if m.exact && name == m.name || !m.exact && strings.HasPrefix(name, m.name) {
return m.upstream, m.openRouter
}
}
return "", false
}
// ClinePassAsked is the upstream a request for model asks the Cline API
// to serve it from on p, "" when it asks for none: ClinePin's, sent with
// the Upstream tick, or, on the Cline plugin's provider, DeepSeek for a
// ClinePass DeepSeek model, which opencode-cline-auth 0.1.11 pins by
// itself unless its pinUpstream option is set off. The Routing page says
// it beside a reply that names no provider, so a route ClinePass picked
// reads as asked for DeepSeek or not (zRain on Discord, 10-24).
func (p Provider) ClinePassAsked(model string) string {
if up, _ := p.ClinePin(model); up != "" {
return up
}
if p.PluginProvider() == "cline" && clinePassDeepSeek(model) {
return "deepseek"
}
return ""
}
// clinePassDeepSeek is a ClinePass DeepSeek model: cline-pass/deepseek-…
func clinePassDeepSeek(model string) bool {
return strings.HasPrefix(strings.ToLower(model), "cline-pass/deepseek")
}
// ClinePassHas is the capability filter (providerOptions.gateway.has) a
// pinned request for model carries beside only, or nil. On ClinePass the
// server's own routing for a DeepSeek model wins over only alone, and a
// fake upstream in only was served all the same; with has beside it the
// request went to DeepSeek and a fake upstream was refused "no matching
// upstream" (hy125245 on X, 10-23). The upstream must cache a repeated
// prefix by itself, and, for a model that takes images (images: the
// fetched list says so, or the request carries one), take them: a
// text-only model asked "vision" (deepseek-v4-pro) would have no upstream
// left. The Cline plugin's provider has none here: the plugin writes it.
func (p Provider) ClinePassHas(model string, images bool) []string {
if up, _ := p.ClinePin(model); up == "" || !clinePassDeepSeek(model) {
return nil
}
if !images {
if ms := p.liveByID(model); len(ms) > 0 {
images = ms[0].Images
}
}
if images {
return []string{"vision", "implicit-caching"}
}
return []string{"implicit-caching"}
}