Files
magpie/internal/agent/droid.go
T
yetone dd70a4055a wsl: OpenCode, Kimi Code, omp and the other CLIs magpie sets up through a file in the home are set up in WSL distros too (#515: on Windows only Codex, Pi, OmO and Claude Code were found in a distro) — fourteen agents join wslKinds, each found by its folder under the distro's $HOME or its command on the distro's own PATH, and listed as <name> · WSL <distro> (opencode@wsl:Ubuntu…): OpenCode (~/.config/opencode, opencode), MiMo Code (~/.config/mimocode, mimo), Kimi Code (~/.kimi-code, kimi; the old kimi-cli's ~/.kimi where that is all there is, found by the command), omp (~/.omp/agent, omp), Crush (~/.config/crush/crush.json, Linux's place, not Windows' LOCALAPPDATA), Hermes Agent (~/.hermes), Grok Build (~/.grok, by its folder alone, as on this machine), Droid (~/.factory), fx (~/.fx, by its folder alone: fx is also the JSON viewer), Command Code (~/.commandcode), MiniMax Code's mcode (~/.minimax), Muse Code (~/.config/muse), Qoder and Qoder CN (~/.qoder, ~/.qoder-cn). Each is its host constructor at a place (opencodeIn, kimiIn, ompIn, crushIn, hermesIn, grokIn, droidIn, fxIn, commandCodeIn, miniMaxIn, museIn, qoderIn/qoderCNIn; the host's opencode(home, cfg) and the rest wrap them), so it writes the same keys and schema as on this machine, but the gateway as the distro reaches it (place.gw/v1: 127.0.0.1 when mirrored, Windows' address under NAT) in its provider, its Check and its Sync, and its folder at the default, as the distro's variables (OPENCODE_CONFIG_DIR, KIMI_CODE_HOME, HERMES_HOME, FACTORY_HOME_OVERRIDE, MINIMAX_DATA_DIR, QODER_CONFIG_DIR…) aren't read (place.getenv is "" there); OpenChamber, a Windows app, isn't looked at for a distro's OpenCode, and omp there, whose version isn't known, gets xhigh rather than a max an older omp refuses. Their notices are the restart advice of their own, as for Codex. A stopped distro's agent is built without looking at its files (place.cold: exists/isDir are false, so the defaults are taken), its options read none (wslOwnAsleep, kimiOwnOptions/ompOwnOptions/miniMaxOwnOptions without a file), and a pick, once the distro is started, builds the agent again warm, so it writes the file the agent reads — an opencode.jsonc already there, ~/.kimi for kimi-cli — not the default. Left out: Gemini CLI and Antigravity (a key lent through .env, a launch command), dsh (profiles and .env), Devin (no gateway; its model list from the devin command on this machine), Goose (no magpie routing; its name is pressly's goose too), Cursor and Copilot (no magpie routing), and the apps (Cline, Hanako, WorkBuddy, ZCode, Claude Desktop, OpenChamber, Alma, Cindy); and the library's skills, MCP and instructions for the new WSL agents, which wslTargetOf still gives only Claude Code, Codex, Pi and OmO. Nothing ran on Windows or in a real WSL. wsl_more_test.go, on fake distro roots under temp dirs with the host's variables pointed elsewhere: TestWSLProbeFindsMoreAgents (each folder and command in the probe, none for grok and fx, kimi-cli by its command), TestWSLMoreAgentsDiscovered (all fourteen listed, no command, UA or aliases), TestWSLMoreAgentsPick (each agent, mirrored and NAT: its file in the distro names the distro's gateway, no 127.0.0.1 under NAT, Check passes, Sync keeps it, the notice; this machine's home and the folders its variables name untouched), TestWSLMoreAgentsStopped (nothing asked nor opened, the last value shown, options, a pick starts the distro and writes there) fail without the change; TestWSLStoppedPickFindsItsFiles fails without the warm rebuild (the pick wrote a second opencode.json beside the .jsonc). go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and vet ./internal/..., GOOS=windows go test -c ./internal/agent/ ./internal/library/, and go test -tags nogui -count=1 ./... under a temp HOME pass (TestFXInState failed once on its TempDir cleanup race, passes on the rerun; untouched).
2026-10-02 14:16:38 +08:00

293 lines
9.3 KiB
Go

package agent
// Factory's Droid CLI keeps its settings in ~/.factory/settings.json
// ($FACTORY_HOME_OVERRIDE/.factory when set). Models of the user's own
// (BYOK) are the customModels array, each {model, displayName, baseUrl,
// apiKey, provider, …}, provider being "anthropic" (Messages), "openai"
// (Responses) or "generic-chat-completion-api" (Chat Completions); the
// older ~/.factory/config.json custom_models is still read, merged under
// settings.json, and magpie leaves it alone. A custom model is picked as
// "custom:<id>": its "id" when it has one, else its displayName with
// whitespace as dashes and a count of the same names before it
// ("custom:Kimi-K2-[Groq]-0"). The model sessions start on is
// sessionDefaultSettings.model; a top-level "model", as the docs show it,
// is moved there by droid when that is unset.
//
// magpie appends one entry per catalog model, id "custom:magpie/<ref>", so
// the catalog joins droid's /model picker under Custom models, each on the
// API its provider speaks natively so the gateway relays it as it is, and
// sets sessionDefaultSettings.model; the user's entries stay as they were
// and in front, so their own ids don't move, and the default they had is
// stashed and put back when magpie steps out.
import (
"encoding/json"
"path/filepath"
"slices"
"strconv"
"strings"
"unicode"
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
const (
droidModels = "customModels"
droidModel = "sessionDefaultSettings.model"
// droidID is how every id magpie gives a custom model begins
droidID = "custom:" + magpieID + "/"
)
// droidEntry is one custom model magpie writes, in droid's key order.
type droidEntry struct {
Model string `json:"model"`
ID string `json:"id"`
DisplayName string `json:"displayName"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
Provider string `json:"provider"`
MaxContextLimit int `json:"maxContextLimit,omitempty"`
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
// droid takes a model on Chat Completions as seeing no images unless
// told otherwise, so this is always written
NoImageSupport bool `json:"noImageSupport"`
}
// droidEntries are magpie's custom models as the catalog is now.
func droidEntries() []droidEntry { return droidEntriesAt(gateway.URL()) }
// droidEntriesAt are droidEntries for a droid reaching the gateway at gw.
func droidEntriesAt(gw string) []droidEntry {
var out []droidEntry
for _, m := range magpieModels("droid") {
e := droidEntry{Model: m.ID, ID: droidID + m.ID, BaseURL: gw + "/v1", APIKey: gateway.Token,
Provider: "generic-chat-completion-api", MaxContextLimit: m.Context, MaxOutputTokens: maxTokens(m), NoImageSupport: !m.Images}
switch {
case slices.Contains(m.APIs, string(provider.Responses)):
e.Provider = "openai"
case slices.Contains(m.APIs, string(provider.Anthropic)):
// Anthropic's SDK adds the /v1 itself
e.Provider, e.BaseURL = "anthropic", gw
}
// the catalog's own label ("pro · DeepSeek") is what the picker
// shows; droid finds the model by its id, whatever its name
if e.DisplayName = m.Name; e.DisplayName == "" {
e.DisplayName = m.ID
}
out = append(out, e)
}
return out
}
// droidSlug is droid's own spelling of a display name in a model id.
func droidSlug(s string) string {
return strings.Join(strings.FieldsFunc(strings.TrimSpace(s), unicode.IsSpace), "-")
}
// droidCustom is one custom model as it is in a file: its raw JSON, and the
// id droid knows it by.
type droidCustom struct {
raw json.RawMessage
id, name, of string // of: the model asked for
}
// droidCustoms reads the custom models of a settings file (key
// customModels, camelCase) or a legacy config.json (custom_models).
func droidCustoms(path, key string) []droidCustom {
raw, err := edit.Read(path)
if err != nil || len(raw) == 0 {
return nil
}
var out []droidCustom
seen := map[string]int{}
nameKey := "displayName"
if key == "custom_models" {
nameKey = "model_display_name"
}
gjson.GetBytes(jsonc.ToJSONInPlace(raw), key).ForEach(func(_, v gjson.Result) bool {
c := droidCustom{raw: json.RawMessage(v.Raw), of: v.Get("model").String(), name: v.Get(nameKey).String()}
if c.name == "" {
c.name = c.of
}
slug := droidSlug(c.name)
c.id = v.Get("id").String()
if c.id == "" {
c.id = "custom:" + slug + "-" + strconv.Itoa(seen[slug])
}
seen[slug]++
out = append(out, c)
return true
})
return out
}
func droid(home string) *Agent { return droidIn(here(home)) }
// droidIn is Droid at a place: this machine's home, or a WSL distro's (see
// wsl.go), where FACTORY_HOME_OVERRIDE isn't read and its custom models
// name the gateway as the distro reaches it.
func droidIn(at place) *Agent {
home := at.home
if h := at.getenv("FACTORY_HOME_OVERRIDE"); h != "" {
home = h
}
droidEntries := func() []droidEntry { return droidEntriesAt(at.gw()) }
dir := filepath.Join(home, ".factory")
path := filepath.Join(dir, "settings.json")
legacy := filepath.Join(dir, "config.json")
key := "droid:" + path + ":" + droidModel
// raw is the model droid starts on as written: sessionDefaultSettings'
// or, while that is unset, the top-level one it moves there
raw := func() string {
if v, ok := edit.GetJSON(path, droidModel); ok {
return v
}
v, _ := edit.GetJSON(path, "model")
return v
}
// get spells magpie's custom models as the catalog does
get := func() string {
v := raw()
if ref, ok := strings.CutPrefix(v, droidID); ok {
return magpieID + "/" + ref
}
return v
}
// setModels writes customModels with the user's own and, when with,
// magpie's after them; none left takes the key out
setModels := func(with bool) error {
var ms []any
var had bool
for _, c := range droidCustoms(path, droidModels) {
if strings.HasPrefix(c.id, droidID) {
had = true
continue
}
ms = append(ms, c.raw)
}
if with {
for _, e := range droidEntries() {
ms = append(ms, e)
}
} else if !had {
return nil
}
if len(ms) > 0 {
return edit.SetJSON(path, edit.KV{Path: droidModels, Value: ms})
}
return edit.DelJSON(path, droidModels)
}
// put sets the default model to v, or takes it out for ""
put := func(v string) error {
if v != "" {
return edit.SetJSON(path, edit.KV{Path: droidModel, Value: v})
}
if err := edit.DelJSON(path, droidModel); err != nil {
return err
}
if v, ok := edit.GetJSON(path, "sessionDefaultSettings"); ok && strings.TrimSpace(v) == "{}" {
return edit.DelJSON(path, "sessionDefaultSettings")
}
return nil
}
// mine finds a custom model of magpie's by id
mine := func(id string) (droidCustom, bool) {
for _, c := range droidCustoms(path, droidModels) {
if c.id == id {
return c, true
}
}
return droidCustom{}, false
}
return &Agent{
ID: "droid", Name: "Droid", Icon: "factory", Aliases: []string{"factory", "factory-droid"},
// droid's requests to a custom model carry factory-cli/<version>
UA: []string{"factory-cli"},
Bin: "droid", Dir: dir, Path: path,
Sync: func() error {
for _, c := range droidCustoms(path, droidModels) {
if strings.HasPrefix(c.id, droidID) {
return setModels(true)
}
}
return nil
},
Notice: func() string {
if Running(`(^|/)droid( |$)`) {
return "Droid reads its settings at start-up — restart open droid sessions to use this."
}
return ""
},
Check: func() string {
v := raw()
if !strings.HasPrefix(v, droidID) {
return ""
}
c, ok := mine(v)
if !ok {
return "Droid's custom model " + v + " (settings.json) is gone, so it no longer reaches magpie"
}
r := gjson.ParseBytes(c.raw)
base := at.v1()
if r.Get("provider").String() == "anthropic" {
base = at.gw()
}
return wiringOff("Droid", path, func(k string) (string, bool) { g := r.Get(k); return g.String(), g.Exists() },
"baseUrl", base, "apiKey", gateway.Token)
},
Fields: []Field{{
Key: "model", Label: "model",
Get: get,
Set: func(v string) error {
cur := raw()
ours := strings.HasPrefix(cur, droidID)
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !ours {
if sd, ok := edit.GetJSON(path, droidModel); ok {
stash(map[string]string{key: sd})
} else {
forget(key)
}
}
if err := setModels(true); err != nil {
return err
}
return put(droidID + ref)
}
if ours {
// back to what the user had
if v == "" {
v = unstash(key)
} else {
forget(key)
}
}
if err := put(v); err != nil {
return err
}
return setModels(false)
},
Options: func(cur map[string]string) []Option {
var own []Option
seen := map[string]bool{}
for _, c := range append(droidCustoms(path, droidModels), droidCustoms(legacy, "custom_models")...) {
if seen[c.id] || strings.HasPrefix(c.id, droidID) {
continue
}
seen[c.id] = true
own = append(own, Option{Value: c.id, Label: c.name, Icon: modelIcon("", c.of)})
}
if c := cur["model"]; c != "" && !usesMagpie(c) && !seen[c] {
own = append([]Option{{Value: c, Icon: modelIcon("", c)}}, own...)
}
return append(group("Droid", own), viaMagpie("droid", magpieID+"/")...)
},
}},
}
}