Files
magpie/internal/agent/jcode.go
T
yetone 9c11abbd00 agent: jcode is an agent magpie connects: a magpie profile with magpie's models, jcode started on the one picked (wani on Discord)
wani asked on Discord for jcode (1jehuang/jcode, a coding agent in Rust)
as an agent magpie connects. There was no earlier request in issues or
history.

What changed:
- internal/agent/jcode.go: a new agent for jcode's config.toml in
  $JCODE_HOME, else ~/.jcode (jcode-storage's jcode_dir). Detected by the
  file, the folder, or `jcode` on PATH.
  - Connecting adds a named profile, [providers.magpie], with the keys
    `jcode provider add` writes: type "openai-compatible", the gateway's
    /v1, an inline api_key naming jcode (magpie-jcode; jcode's requests
    carry nothing else that tells it apart) and requires_api_key = true.
    Without that key, jcode sends no key to a loopback base URL.
  - Its models list has one entry per model of magpie's: context_window
    (128000 when magpie doesn't know it; jcode would assume 200k), input
    text/image, and reasoning = true for a model with effort levels, so
    jcode's /effort offers them.
  - It sets [provider] default_provider = "magpie" and default_model, the
    model picked. What was there is stashed, and Disconnect puts it back.
  - Picking a model of jcode's own while it is on magpie takes magpie
    out: the profile goes and the user's provider comes back with that
    model.
  - Each key is set alone, so the user's other keys and tables stay, as
    do their own keys in magpie's profile (headers, extra_body) and a
    reasoning_effort they set on one of its models while the model has
    that level.
  - jcode writes a profile's models as [[providers.magpie.models]]
    tables when it rewrites the file itself (Config::save,
    to_string_pretty). Those are replaced by the one list.
  - A sync with nothing to change doesn't write.
  - Disconnect drops the profile whole, sub-tables included.
  - Check says when the profile is gone while jcode still starts on it,
    or when its base_url or key is off.
  - While jcode runs, the notice says to restart open jcode sessions,
    since it reads the file when it starts. The notice is a newNotice
    template, translated in i18n.js (zh-CN, zh-TW, ja, de) and listed in
    NOTICES, as TestEveryNoticeIsTranslated asks.
- agents.go, agentenv.go: registers it; JCODE_HOME joins the agent env.
- icons/jcode.svg: jcode's halftone mark (jcode.sh/favicon.svg) as a
  mask icon, every other dot of its lattice.
- docs/reference.md, docs/subsystems/agent-wiring.md, README.md,
  README.zh-CN.md: jcode's row and its place in the agent list.
- WSL isn't covered: jcodeAt reads the place's home and JCODE_HOME, but
  it isn't added to the WSL agent list (another session has wsl.go open).

What was run:
- go test -tags nogui ./internal/agent -run '^TestJcode'. The fixture is
  the bytes jcode 0.94.0 wrote for `jcode provider add`. The tests went
  red when, by hand, I:
  - made a no-op sync write;
  - dropped the user's reasoning_effort;
  - skipped the stash, so Disconnect wasn't byte-identical;
  - left jcode's [[providers.magpie.models]] tables in, which makes
    invalid TOML;
  - dropped requires_api_key.
- Real use:
  - jcode v0.94.0 (release binary, sha256 checked) ran under a sandbox
    HOME with JCODE_HOME set and JCODE_NO_TELEMETRY=1. Its config.toml
    was the one jcode wrote for its own "probe" profile.
  - A sandbox magpie (MAGPIE_ADDR 127.0.0.1:3671) with a fake OpenAI
    upstream on 3672 ran `magpie jcode magpie/fake/org/echo-1`, a model id
    with two slashes.
  - `jcode run 'Say hello in one word.'` answered with the upstream's
    reply, streamed. The upstream saw model org/echo-1, and usage.jsonl
    recorded the turn under agent "jcode".
  - `jcode provider add two …` while connected appended its own profile
    and left magpie's as it was.
  - `magpie jcode default` then gave back the file jcode wrote,
    byte-identical, with jcode's new [providers.two] kept.
- The icon rendered as a mask in Chromium and WebKit. The only GUI
  change is the notice's translations; TestEveryNoticeIsTranslated and
  internal/gui pass, and node --check accepts i18n.js.
- gofmt -l; go vet (darwin, GOOS=linux, GOOS=windows) on internal/agent
  and internal/agentenv; go test -tags nogui ./internal/agent
  ./internal/agentenv ./internal/gui; go test -tags nogui ./... under a
  temp HOME (load ~80): every package passed. After rebasing on
  origin/main, internal/agent and internal/agentenv were run again,
  along with internal/gui, gofmt and the linux/windows vet.
2026-10-11 04:33:53 +08:00

376 lines
11 KiB
Go

package agent
// jcode (1jehuang/jcode, a coding agent in Rust) keeps its settings in
// config.toml in $JCODE_HOME, else ~/.jcode. A provider of the user's own
// making is a named profile, [providers.<name>], which `jcode provider add`
// writes: type "openai-compatible" (jcode's chat completions client), a
// base_url, a key (api_key inline, or api_key_env) and its models as
// [[providers.<name>.models]] entries (id, context_window, input,
// reasoning). jcode starts on [provider] default_provider and default_model,
// a profile's name and one of its models.
//
// magpie adds itself as the profile "magpie": the gateway, with one model
// entry per catalog model, so the catalog is in jcode's /model picker, and
// jcode is started on the model picked. What [provider] named before is
// stashed and put back when magpie steps out. Every other key of the file is
// the user's and stays as written, the user's own keys in magpie's profile
// too (headers, extra_body, a model's reasoning_effort).
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
toml "github.com/pelletier/go-toml/v2"
"github.com/yetone/magpie/internal/edit"
)
// jcodeProfile is magpie's profile's table in jcode's config.toml, and
// jcodeModels its models' array.
const (
jcodeProfile = "providers." + magpieID
jcodeModels = jcodeProfile + ".models"
)
// what jcode says after a change (notice.go)
var noticeJcode = newNotice("jcode reads its settings at start-up — restart open jcode sessions to use this.")
// jcodeContext is what jcode is told of a model whose context magpie
// doesn't know; without one it takes 200k.
const jcodeContext = 128000
// jcodeDir is jcode's folder: $JCODE_HOME, else ~/.jcode (jcode-storage's
// jcode_dir).
func jcodeDir(at place) string {
if d := strings.TrimSpace(at.getenv("JCODE_HOME")); d != "" {
return d
}
return filepath.Join(at.home, ".jcode")
}
// jcodeModel is one [[providers.<name>.models]] entry, as jcode's
// NamedProviderModelConfig reads it.
type jcodeModel struct {
ID string `toml:"id"`
Reasoning *bool `toml:"reasoning,omitempty"`
ReasoningEffort string `toml:"reasoning_effort,omitempty"`
ContextWindow int `toml:"context_window,omitempty"`
Input []string `toml:"input,omitempty"`
}
// jcodeConf is what magpie reads of jcode's config.toml.
type jcodeConf struct {
Provider struct {
DefaultProvider string `toml:"default_provider"`
DefaultModel string `toml:"default_model"`
} `toml:"provider"`
Providers map[string]struct {
BaseURL string `toml:"base_url"`
APIKey string `toml:"api_key"`
Models []jcodeModel `toml:"models"`
} `toml:"providers"`
}
// jcodeRead reads jcode's config.toml; a missing file is an empty one, and
// one that doesn't parse is an error, never taken as empty.
func jcodeRead(path string) (jcodeConf, error) {
var c jcodeConf
b, err := os.ReadFile(path)
if os.IsNotExist(err) {
return c, nil
}
if err != nil {
return c, err
}
if err := toml.Unmarshal(b, &c); err != nil {
return c, fmt.Errorf("jcode's %s isn't TOML magpie can read; it was left as it is: %w", path, err)
}
return c, nil
}
// jcodeWant is magpie's models as jcode is told them: the context window
// (one magpie doesn't know is jcodeContext), whether it takes images, and
// reasoning on for a model with effort levels, so jcode's /effort offers
// them. A reasoning_effort the user set on one of magpie's models stays
// while the model has that level.
func jcodeWant(was []jcodeModel) []jcodeModel {
kept := map[string]string{}
for _, m := range was {
kept[m.ID] = m.ReasoningEffort
}
var out []jcodeModel
for _, m := range magpieModels("jcode") {
ctx := m.Context
if ctx <= 0 {
ctx = jcodeContext
}
e := jcodeModel{ID: m.ID, ContextWindow: ctx, Input: []string{"text"}}
if m.Images {
e.Input = append(e.Input, "image")
}
var levels []string
for _, l := range m.Efforts {
if l != "none" {
levels = append(levels, l)
}
}
if len(levels) > 0 {
yes := true
e.Reasoning = &yes
e.ReasoningEffort = keptEffort(kept[m.ID], levels, "")
}
out = append(out, e)
}
return out
}
// jcodeArray is models as one TOML array of inline tables, one per line.
func jcodeArray(models []jcodeModel) edit.Raw {
if len(models) == 0 {
return "[]"
}
var b strings.Builder
b.WriteString("[\n")
for _, m := range models {
parts := []string{"id = " + strconv.Quote(m.ID)}
if m.ContextWindow > 0 {
parts = append(parts, "context_window = "+strconv.Itoa(m.ContextWindow))
}
if len(m.Input) > 0 {
var q []string
for _, in := range m.Input {
q = append(q, strconv.Quote(in))
}
parts = append(parts, "input = ["+strings.Join(q, ", ")+"]")
}
if m.Reasoning != nil {
parts = append(parts, "reasoning = "+strconv.FormatBool(*m.Reasoning))
}
if m.ReasoningEffort != "" {
parts = append(parts, "reasoning_effort = "+strconv.Quote(m.ReasoningEffort))
}
b.WriteString(" { " + strings.Join(parts, ", ") + " },\n")
}
b.WriteString("]")
return edit.Raw(b.String())
}
// jcodeSame reports whether two model lists say the same to jcode.
func jcodeSame(a, b []jcodeModel) bool {
return slices.EqualFunc(a, b, func(x, y jcodeModel) bool {
return x.ID == y.ID && x.ContextWindow == y.ContextWindow && slices.Equal(x.Input, y.Input) &&
(x.Reasoning == nil) == (y.Reasoning == nil) && (x.Reasoning == nil || *x.Reasoning == *y.Reasoning) &&
x.ReasoningEffort == y.ReasoningEffort
})
}
// jcodePick is what [provider] named before magpie: a profile (or one of
// jcode's providers) and a model, "" each for none.
type jcodePick struct {
Provider string `json:"provider,omitempty"`
Model string `json:"model,omitempty"`
}
func jcode(home string) *Agent { return jcodeAt(here(home)) }
func jcodeAt(at place) *Agent {
dir := jcodeDir(at)
path := filepath.Join(dir, "config.toml")
keyPick := at.key("jcode:" + path + ":default")
key := func() string { return agentKeyAt("jcode", at.gw()) }
read := func() jcodeConf { c, _ := jcodeRead(path); return c }
ours := func(c jcodeConf) bool { return c.Provider.DefaultProvider == magpieID }
get := func() string {
c := read()
if ours(c) && c.Provider.DefaultModel != "" {
return magpieID + "/" + c.Provider.DefaultModel
}
return c.Provider.DefaultModel
}
// setPick names the profile and model jcode starts on, a key taken out
// for ""
setPick := func(p jcodePick) error {
for _, kv := range [][2]string{{"default_provider", p.Provider}, {"default_model", p.Model}} {
var err error
if kv[1] == "" {
err = edit.DelTOMLKey(path, "provider", kv[0])
} else {
err = edit.SetTOMLKey(path, "provider", kv[0], kv[1])
}
if err != nil {
return err
}
}
return nil
}
// wire writes magpie's profile, its own keys set and the user's others
// left as they are, with model, if given, the profile's default; it
// writes nothing when the file has it all already
wire := func(model string) error {
c, err := jcodeRead(path)
if err != nil {
return err
}
have, on := c.Providers[magpieID]
want := jcodeWant(have.Models)
t, err := edit.GetTOMLTable(path, jcodeProfile)
if err != nil {
return err
}
kvs := [][2]any{
{"type", "openai-compatible"},
{"base_url", at.v1()},
{"api_key", key()},
// a gateway on loopback is taken by jcode as needing no key,
// and then it sends none: the key names jcode to the gateway
{"requires_api_key", edit.Raw("true")},
}
if model != "" {
kvs = append(kvs, [2]any{"default_model", model})
}
same := on && t != nil && jcodeSame(have.Models, want)
for _, kv := range kvs {
v := fmt.Sprint(kv[1])
if t[kv[0].(string)] != v {
same = false
}
}
if same {
return nil
}
// jcode writes the models as [[providers.magpie.models]] when it
// saves its settings itself: those go, for the one array below
if err := edit.SetTOMLTablesMatching(path, []string{jcodeModels}, nil, nil); err != nil {
return err
}
for _, kv := range kvs {
if err := edit.SetTOMLKey(path, jcodeProfile, kv[0].(string), kv[1]); err != nil {
return err
}
}
return edit.SetTOMLKey(path, jcodeProfile, "models", jcodeArray(want))
}
// unwire takes magpie's profile out, and jcode back to what it started
// on before — when it still starts on magpie: a pick the user made in
// jcode since is theirs and stands
unwire := func(own string) error {
c, err := jcodeRead(path)
if err != nil {
return err
}
if ours(c) {
var was jcodePick
json.Unmarshal([]byte(unstash(keyPick)), &was)
if own != "" && own != was.Model {
was.Model = own
}
if err := setPick(was); err != nil {
return err
}
} else {
forget(keyPick)
if own != "" {
if err := edit.SetTOMLKey(path, "provider", "default_model", own); err != nil {
return err
}
}
}
// the profile whole: its models' tables and the user's own in it
// (headers, extra_body) are magpie's profile's, and go with it
return edit.SetTOMLTablesMatching(path, []string{jcodeProfile}, []string{jcodeProfile + "."}, nil)
}
return atomic(&Agent{
ID: "jcode", Name: "jcode", Icon: "jcode", Spelled: prefixed,
Bin: "jcode", Dir: dir, Path: path,
Notice: func() string {
if Running(`(^|/)jcode( |$)`) {
return noticeJcode.String()
}
return ""
},
Sync: func() error {
c, err := jcodeRead(path)
if err != nil {
return err
}
if _, on := c.Providers[magpieID]; !on {
return nil
}
return wire("")
},
Check: func() string {
c, err := jcodeRead(path)
if err != nil {
return err.Error()
}
if !ours(c) {
return ""
}
p, on := c.Providers[magpieID]
if !on {
return "jcode's [" + jcodeProfile + "] (config.toml) is gone, so it no longer reaches magpie"
}
return wiringOff("jcode", path, func(k string) (string, bool) {
switch k {
case "base_url":
return p.BaseURL, p.BaseURL != ""
case "api_key":
return p.APIKey, p.APIKey != ""
}
return "", false
}, "base_url", at.v1(), "api_key", key())
},
Fields: []Field{{
// the model jcode starts on: one of magpie's is
// magpie/<provider>/<model>, magpie's profile with the model's
// id in it
Key: "model", Label: "model",
Get: get,
Set: func(v string) error {
c, err := jcodeRead(path)
if err != nil {
return err
}
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !ours(c) {
b, _ := json.Marshal(jcodePick{c.Provider.DefaultProvider, c.Provider.DefaultModel})
stash(map[string]string{keyPick: string(b)})
}
if err := wire(ref); err != nil {
return err
}
return setPick(jcodePick{magpieID, ref})
}
if _, on := c.Providers[magpieID]; on || ours(c) {
return unwire(v)
}
return setPick(jcodePick{c.Provider.DefaultProvider, v})
},
Options: func(cur map[string]string) []Option {
return append(jcodeOwn(path, keyPick, cur["model"]), viaMagpie("jcode", magpieID+"/")...)
},
}},
}, path)
}
// jcodeOwn is the model jcode started on before magpie, or starts on now,
// for the picker: jcode's own providers are its sign-ins, which magpie
// can't list.
func jcodeOwn(path, keyPick, cur string) []Option {
var was jcodePick
json.Unmarshal([]byte(stashLoad()[keyPick]), &was)
var out []Option
for _, m := range []string{was.Model, cur} {
if m == "" || prefixed(m) || slices.ContainsFunc(out, func(o Option) bool { return o.Value == m }) {
continue
}
out = append(out, Option{Value: m, Icon: modelIcon("", m), Note: "jcode's own"})
}
return group("jcode", out)
}