Files
magpie/internal/provider/workbuddy_models.go
T
yetone 33acb6bca0 providers: each model's credit rate shows by it in the provider's model list and an agent's model picker, Qoder's 0.5× and WorkBuddy's 0.03×, a discount's price before it struck through (01huadalang on Discord: qcoder 或者 workbuddy 能不能实时抓取或者按一段时间抓取能展示列表模型的积分消耗倍率,这样不用打开软件查看,能选择到便宜的模型) — magpie read each model's price only to tell free from not: Qoder's price_factor (06584e15) and WorkBuddy's "credits" ("x0.03", wbFreeCredits), so the lists said FREE or nothing, and picking a cheap model meant opening Qoder or WorkBuddy. catalog.Model now keeps the price, Rate (the credits a request costs, as a multiple; 0 is not listed, or Free) and RateWas (the price before a discount running now), and carries both through provider.Entry, agent.Option and the providers page's models. Qoder (and Qoder CN): Rate is the price_factor; RateWas an active promotion's before_promotion_price_factor, else an original_price_factor above the price (Qwen3.8-Flash: free, 0.1× struck through), and a promotion's 0 that isn't free is its price before times its discount_factor. WorkBuddy and WorkBuddy AI (the CodeBuddy plan; both builds read the same /v3/config): Rate is the credits (wbCredits; x0.00 stays FREE, an unreadable or infinite rate is none). A plugin's model gives it as it gives free: rate and rateWas on the model its provider.models hook lists, a number (0.5) or as the vendor writes it ("x0.03", "0.5×"), which host.js reads (rateOf) into plugin.Model.Rate/RateWas and pluginCatalog keeps; the moved Qoder and WorkBuddy users get it once their plugins send it. The badge is small and grey (the context badge's colours, no uppercase), beside FREE where that shows, its title saying what a request costs (每次请求消耗 0.5× 积分;折扣前 / 限时免费,原价). Rates are as of the provider's last list: a built-in's is fetched at start-up when it has none, on Refresh and when its key changes, a plugin's when its plugins are asked again (start-up, a sign-in, a plugin change); nothing asks on a timer. Kiro, Cursor, ZCode and Devin list no per-model rate magpie reads, so they show none. TestParseModelsRate (qoder) and TestWBCreditsRate (provider) are new and don't build without the change; TestFakePlugin now checks a plugin's rate given as a number and as "x0.03" (0 for both with host.js not reading them). model-rate.test.cjs (WebKit, en and zh: the chips' badges and titles, struck-through prices, grey not green, no stripe, a click picks and scrolls nothing, the picker's badges) fails without the change and passes with it; click-scroll, models-free-only, model-pick and favorite-star pass in WebKit. No request was made to Qoder, WorkBuddy or any vendor. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME, go test ./internal/gui pass.
2026-10-02 22:48:45 +08:00

166 lines
5.5 KiB
Go

package provider
// PLUGIN-SERVED (see AGENTS.md): WorkBuddy ("workbuddy" and "workbuddy-ai")
// is a deprecated built-in subscription served by its plugin,
// @magpie-community/opencode-workbuddy-auth, once moved onto it
// (provider.Moved; the default for a new sign-in). A moved one's sign-ins,
// models, requests and usage are all the plugin's, never this code's (only
// the move, in migrate*.go, still reads its accounts). A fix here alone
// doesn't reach those users; fix the plugin
// (github.com/magpie-community/plugins, packages/workbuddy) and raise the
// mover's min in internal/provider/migrate_workbuddy.go.
// A WorkBuddy plan's models are the ones WorkBuddy's product config gives
// its CLI agent: GET /v3/config, signed as a chat is, answers with the
// product's agents (the "cli" one's models are WorkBuddy's picker) and each
// model's details. Which product answers goes by the User-Agent: WorkBuddy
// sends "CLI/<version> WorkBuddy/<version>", and a bare WorkBuddy/<version>
// gets CodeBuddy IDE's config, with no cli agent.
import (
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"github.com/yetone/magpie/internal/catalog"
)
type wbProductConfig struct {
Agents []struct {
Name string `json:"name"`
Models []string `json:"models"`
} `json:"agents"`
Models []struct {
ID string `json:"id"`
Name string `json:"name"`
MaxInputTokens int `json:"maxInputTokens"`
MaxOutputTokens int `json:"maxOutputTokens"`
SupportsImages *bool `json:"supportsImages"`
OnlyReasoning bool `json:"onlyReasoning"`
// what a request costs of the plan's credits, as WorkBuddy's
// picker shows it: "x0.00" (free), "x0.03", "x1.00"
Credits json.RawMessage `json:"credits"`
Reasoning struct {
SupportedEfforts []string `json:"supportedEfforts"`
CanDisableThinking *bool `json:"canDisableThinking"`
} `json:"reasoning"`
} `json:"models"`
}
// wbFetchModels asks WorkBuddy's product config for the plan's models,
// with sign signing the request as the account's chats are.
func wbFetchModels(ctx context.Context, w *wbSite, sign func(context.Context, *http.Request, []byte) error) ([]catalog.Model, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, w.api()+"/v3/config", nil)
if err != nil {
return nil, err
}
if err := sign(ctx, req, nil); err != nil {
return nil, err
}
req.Header.Set("User-Agent", "CLI/"+wbAppVersion+" WorkBuddy/"+wbUAVersion)
req.Header.Set("X-Requested-With", "XMLHttpRequest")
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 8<<20))
var env struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data wbProductConfig `json:"data"`
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("WorkBuddy's config: %s", APIError(b, res.Status))
}
if err := json.Unmarshal(b, &env); err != nil {
return nil, fmt.Errorf("WorkBuddy's config: %w", err)
}
if env.Code != 0 {
return nil, fmt.Errorf("WorkBuddy's config: %s (%d)", env.Msg, env.Code)
}
ms := env.Data.cliModels()
if len(ms) == 0 {
return nil, fmt.Errorf("WorkBuddy's config lists no models for its CLI agent")
}
return ms, nil
}
func (c wbProductConfig) cliModels() []catalog.Model {
var out []catalog.Model
for _, a := range c.Agents {
if a.Name != "cli" {
continue
}
for _, id := range a.Models {
m := catalog.Model{ID: id, Name: id}
for _, d := range c.Models {
if d.ID != id {
continue
}
if d.Name != "" {
m.Name = d.Name
}
m.Context, m.Output = d.MaxInputTokens, d.MaxOutputTokens
if r, ok := wbCredits(d.Credits); ok {
m.Free, m.Rate = r == 0, r
}
if d.SupportsImages != nil {
m.Images, m.ImageInput = *d.SupportsImages, d.SupportsImages
}
// the levels WorkBuddy offers the model, and off when it
// offers that; a model it gives no levels takes any
if es := d.Reasoning.SupportedEfforts; len(es) > 0 {
if !d.OnlyReasoning && (d.Reasoning.CanDisableThinking == nil || *d.Reasoning.CanDisableThinking) {
m.Efforts = append(m.Efforts, "none")
}
m.Efforts = append(m.Efforts, es...)
}
break
}
out = append(out, m)
}
}
return wbDistinctNames(out)
}
// wbCredits reads a model's credits, the multiple WorkBuddy's picker shows
// by it: "x0.03", "0" or 0; "x0.00" is free. A rate it can't read (or
// none) is not ok, and the model is neither free nor rated.
func wbCredits(raw json.RawMessage) (float64, bool) {
var s string
if json.Unmarshal(raw, &s) != nil {
s = string(raw)
}
s = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(s)), "x")
f, err := strconv.ParseFloat(s, 64)
return f, err == nil && f >= 0 && !math.IsInf(f, 1)
}
// wbDistinctNames tells apart models the config gives the same name:
// WorkBuddy AI lists deepseek-v4.1-flash (free) and deepseek-v4.1-flash-sg
// (Singapore, x0.03 credits) both as "Deepseek-V4.1-Flash". A later one is
// named for what its id adds to the first's ("… (SG)"), or else its id.
func wbDistinctNames(ms []catalog.Model) []catalog.Model {
first := map[string]string{} // name → the id that has it
for i, m := range ms {
id, ok := first[m.Name]
if !ok {
first[m.Name] = m.ID
continue
}
tag := m.ID
if rest, ok := strings.CutPrefix(m.ID, id+"-"); ok && rest != "" {
tag = strings.ToUpper(rest)
}
ms[i].Name = m.Name + " (" + tag + ")"
}
return ms
}