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.
304 lines
9.3 KiB
Go
304 lines
9.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
toml "github.com/pelletier/go-toml/v2"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// jcodeConfig is a config.toml as jcode 0.94.0 writes it after `jcode
|
|
// provider add deepseek --base-url … --model deepseek-chat --api-key-stdin
|
|
// --set-default` (toml-rs, to_string_pretty), with a comment the user added
|
|
// by hand.
|
|
const jcodeConfig = `[provider]
|
|
# my own pick
|
|
default_provider = "deepseek"
|
|
default_model = "deepseek-chat"
|
|
|
|
[providers.deepseek]
|
|
type = "openai-compatible"
|
|
base_url = "https://api.deepseek.com/v1"
|
|
auth = "bearer"
|
|
api_key_env = "JCODE_PROVIDER_DEEPSEEK_API_KEY"
|
|
env_file = "provider-deepseek.env"
|
|
default_model = "deepseek-chat"
|
|
requires_api_key = true
|
|
|
|
[[providers.deepseek.models]]
|
|
id = "deepseek-chat"
|
|
`
|
|
|
|
// jcodeHome is a HOME with jcode's ~/.jcode holding body as its
|
|
// config.toml, and magpie with a provider of two models, one of them with
|
|
// effort levels.
|
|
func jcodeHome(t *testing.T, body string) (home, path string) {
|
|
t.Helper()
|
|
home = t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("JCODE_HOME", "")
|
|
if err := provider.Save(provider.Provider{ID: "kimi", Name: "Kimi", Key: "k",
|
|
Chat: "https://api.moonshot.cn/v1", Models: []string{"k2", "k2-vision"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("kimi", "https://api.moonshot.cn/v1", []catalog.Model{
|
|
{ID: "k2", Context: 262144, Efforts: []string{"low", "medium", "high"}},
|
|
{ID: "k2-vision", Images: true},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path = filepath.Join(home, ".jcode", "config.toml")
|
|
if body != "" {
|
|
writeFile(t, path, body)
|
|
}
|
|
return home, path
|
|
}
|
|
|
|
func jcodeParse(t *testing.T, path string) map[string]any {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var m map[string]any
|
|
if err := toml.Unmarshal(b, &m); err != nil {
|
|
t.Fatalf("%s is not TOML: %v\n%s", path, err, b)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func jcodeTable(m map[string]any, keys ...string) map[string]any {
|
|
for _, k := range keys {
|
|
m, _ = m[k].(map[string]any)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// jcodeModelsOf is magpie's profile's models, by id.
|
|
func jcodeModelsOf(t *testing.T, m map[string]any) map[string]map[string]any {
|
|
t.Helper()
|
|
out := map[string]map[string]any{}
|
|
list, _ := jcodeTable(m, "providers", "magpie")["models"].([]any)
|
|
for _, x := range list {
|
|
e := x.(map[string]any)
|
|
out[e["id"].(string)] = e
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Connecting jcode adds magpie's profile — the gateway, with a key naming
|
|
// jcode, and every one of magpie's models — and starts jcode on the model
|
|
// picked. The user's own profile and pick stay in the file, and Disconnect
|
|
// gives the file back byte for byte.
|
|
func TestJcode(t *testing.T) {
|
|
home, path := jcodeHome(t, jcodeConfig)
|
|
a := jcode(home)
|
|
if a.Path != path || !a.Detected() {
|
|
t.Fatalf("path %q, detected %v", a.Path, a.Detected())
|
|
}
|
|
f := a.Field("model")
|
|
if got := f.Get(); got != "deepseek-chat" || a.Wired() || a.Check() != "" {
|
|
t.Fatalf("before: %q wired %v check %q", got, a.Wired(), a.Check())
|
|
}
|
|
var own, ours bool
|
|
for _, o := range f.Options(a.Values()) {
|
|
own = own || o.Value == "deepseek-chat"
|
|
ours = ours || o.Value == "magpie/kimi/k2"
|
|
}
|
|
if !own || !ours {
|
|
t.Fatalf("options: own %v magpie %v", own, ours)
|
|
}
|
|
|
|
if err := a.Pick("model", "magpie/kimi/k2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := jcodeParse(t, path)
|
|
if p := jcodeTable(m, "provider"); p["default_provider"] != "magpie" || p["default_model"] != "kimi/k2" {
|
|
t.Fatalf("[provider]: %v", p)
|
|
}
|
|
if !a.Wired() || f.Get() != "magpie/kimi/k2" {
|
|
t.Fatalf("wired %v, model %q", a.Wired(), f.Get())
|
|
}
|
|
if c := a.Check(); c != "" {
|
|
t.Fatalf("check: %s", c)
|
|
}
|
|
p := jcodeTable(m, "providers", "magpie")
|
|
if p["type"] != "openai-compatible" || p["base_url"] != here(home).v1() || p["requires_api_key"] != true ||
|
|
p["api_key"] != agentKeyAt("jcode", here(home).gw()) || p["default_model"] != "kimi/k2" {
|
|
t.Fatalf("magpie's profile: %v", p)
|
|
}
|
|
models := jcodeModelsOf(t, m)
|
|
k2, vision := models["kimi/k2"], models["kimi/k2-vision"]
|
|
if len(models) != 2 || k2 == nil || vision == nil {
|
|
t.Fatalf("models: %v", models)
|
|
}
|
|
if k2["context_window"] != int64(262144) || k2["reasoning"] != true || k2["reasoning_effort"] != nil {
|
|
t.Fatalf("k2: %v", k2)
|
|
}
|
|
if vision["context_window"] != int64(jcodeContext) || !reflect.DeepEqual(vision["input"], []any{"text", "image"}) ||
|
|
vision["reasoning"] != nil {
|
|
t.Fatalf("k2-vision: %v", vision)
|
|
}
|
|
b, _ := os.ReadFile(path)
|
|
own0 := jcodeConfig[strings.Index(jcodeConfig, "[providers.deepseek]"):]
|
|
if !strings.Contains(string(b), own0) || !strings.Contains(string(b), "# my own pick\n") {
|
|
t.Fatalf("the user's own profile or comment changed:\n%s", b)
|
|
}
|
|
|
|
// switching between magpie's models keeps what jcode was on before
|
|
if err := a.Pick("model", "magpie/kimi/k2-vision"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := f.Get(); got != "magpie/kimi/k2-vision" {
|
|
t.Fatalf("model %q", got)
|
|
}
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(path); string(got) != jcodeConfig {
|
|
t.Fatalf("Disconnect left:\n%s", got)
|
|
}
|
|
if a.Wired() {
|
|
t.Fatal("still wired")
|
|
}
|
|
}
|
|
|
|
// A model of jcode's own picked while it is on magpie takes magpie out: the
|
|
// profile goes, and jcode is on the user's own provider with that model.
|
|
func TestJcodeOwnModelTakesMagpieOut(t *testing.T) {
|
|
home, path := jcodeHome(t, jcodeConfig)
|
|
a := jcode(home)
|
|
if err := a.Pick("model", "magpie/kimi/k2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Pick("model", "deepseek-reasoner"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := strings.Replace(jcodeConfig, `default_model = "deepseek-chat"`+"\n\n", `default_model = "deepseek-reasoner"`+"\n\n", 1)
|
|
if got, _ := os.ReadFile(path); string(got) != want {
|
|
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
|
|
}
|
|
if a.Wired() {
|
|
t.Fatal("still wired")
|
|
}
|
|
}
|
|
|
|
// What the user sets in magpie's profile — a header, an effort on one of
|
|
// its models — is theirs, also after jcode saved the file itself (its
|
|
// models then are [[providers.magpie.models]] tables): a catalog sync
|
|
// rewrites the models and the endpoint, not those. A sync with nothing to
|
|
// change doesn't write.
|
|
func TestJcodeSyncKeepsTheirs(t *testing.T) {
|
|
home, path := jcodeHome(t, "")
|
|
a := jcode(home)
|
|
if err := a.Pick("model", "magpie/kimi/k2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gw := here(home)
|
|
// as jcode's Config::save writes it, with what the user changed in it
|
|
saved := `[provider]
|
|
default_provider = "magpie"
|
|
default_model = "kimi/k2"
|
|
|
|
[providers.magpie]
|
|
type = "openai-compatible"
|
|
base_url = "` + gw.v1() + `"
|
|
api_key = "` + agentKeyAt("jcode", gw.gw()) + `"
|
|
default_model = "kimi/k2"
|
|
requires_api_key = true
|
|
|
|
[[providers.magpie.models]]
|
|
id = "kimi/k2"
|
|
reasoning = true
|
|
reasoning_effort = "high"
|
|
context_window = 262144
|
|
input = ["text"]
|
|
|
|
[[providers.magpie.models]]
|
|
id = "kimi/k2-vision"
|
|
context_window = 128000
|
|
input = ["text", "image"]
|
|
|
|
[providers.magpie.headers]
|
|
X-Team = "mine"
|
|
`
|
|
writeFile(t, path, saved)
|
|
// a model magpie lists no more leaves jcode's list
|
|
if err := catalog.SaveLive("kimi", "https://api.moonshot.cn/v1", []catalog.Model{
|
|
{ID: "k2", Context: 262144, Efforts: []string{"low", "medium", "high"}},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "kimi", Name: "Kimi", Key: "k",
|
|
Chat: "https://api.moonshot.cn/v1", Models: []string{"k2"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := jcodeParse(t, path)
|
|
models := jcodeModelsOf(t, m)
|
|
if len(models) != 1 || models["kimi/k2"]["reasoning_effort"] != "high" {
|
|
t.Fatalf("models: %v", models)
|
|
}
|
|
if h := jcodeTable(m, "providers", "magpie", "headers"); h["X-Team"] != "mine" {
|
|
t.Fatalf("the user's header: %v", h)
|
|
}
|
|
if c := a.Check(); c != "" {
|
|
t.Fatalf("check: %s", c)
|
|
}
|
|
|
|
// nothing to change: the file isn't written
|
|
old := time.Now().Add(-time.Hour).Truncate(time.Second)
|
|
if err := os.Chtimes(path, old, old); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if st, _ := os.Stat(path); !st.ModTime().Equal(old) {
|
|
t.Fatalf("a sync with nothing to change wrote the file (%v)", st.ModTime())
|
|
}
|
|
|
|
// Disconnect takes magpie's profile out whole, its array tables and
|
|
// the user's header in it too, and leaves jcode with no pick, as before
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := os.ReadFile(path); strings.TrimSpace(string(got)) != "" {
|
|
t.Fatalf("Disconnect left:\n%s", got)
|
|
}
|
|
}
|
|
|
|
// jcode's folder is $JCODE_HOME when it is set; magpie's profile gone from
|
|
// the file while jcode still starts on it is said.
|
|
func TestJcodeHome(t *testing.T) {
|
|
home, _ := jcodeHome(t, "")
|
|
dir := filepath.Join(home, "elsewhere")
|
|
t.Setenv("JCODE_HOME", dir)
|
|
a := jcode(home)
|
|
if a.Path != filepath.Join(dir, "config.toml") || a.Detected() {
|
|
t.Fatalf("path %q detected %v", a.Path, a.Detected())
|
|
}
|
|
if err := a.Pick("model", "magpie/kimi/k2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !a.Wired() || !a.Detected() {
|
|
t.Fatalf("wired %v detected %v", a.Wired(), a.Detected())
|
|
}
|
|
writeFile(t, a.Path, "[provider]\ndefault_provider = \"magpie\"\ndefault_model = \"kimi/k2\"\n")
|
|
if c := a.Check(); !strings.Contains(c, "is gone") {
|
|
t.Fatalf("check: %q", c)
|
|
}
|
|
}
|