Files
magpie/internal/agent/codebuddy.go
T
yetone 20582866a5 agent, gui: an agent's advice after a change reads in the window's language (#1508, NovaTrailX)
A Chinese window said "ZCode 已接入 magpie. ZCode reads its providers at
start-up — restart ZCode …": the notice an agent gives after a change
(state.notice) was English text written in place in each agent's file, and
i18n.js had no entry for any of them.

What changed:
- internal/agent/notice.go: a notice is a template registered with
  newNotice ("Restart {agent} to …"), filled with say(kv...); Notices()
  lists every one. All the agents' notices (claude, codex daemon, zcode,
  agy, cursor-local, wsl, AstrBot's added today, …) are now such templates, the issue's list
  being a sample of them.
- i18n.js: every template has a zh, zh-TW, ja and de translation, and
  NOTICES lists them. tNotice() reads a notice of several joined: longest
  template first, a {slot} never runs over a sentence's end, and a notice
  with any part that has no template (a plugin's own) reads as given
  rather than half in each language. withNotice() joins the status line
  and the notice with 。 in zh/ja.
- app.js: the six places that showed state.notice use withNotice.

Tests:
- TestEveryNoticeIsTranslated: every Notices() template is in NOTICES and
  in each language table, with the same slots. Fails with the zh ZCode
  line deleted from i18n.js (tried).
- TestNoticesAreTemplates: an AST scan for English written in place as a
  Notice. Fails with zcode.go's literal put back (tried).
- tests/agent-notice-lang.test.cjs: every template, filled, alone and
  joined, plus a plugin's notice, and connecting ZCode as the reporter
  did; en, zh, zh-TW, ja, de at 1100 and 420px, Chromium and WebKit:
  22/22. Its zh subtests fail without the zh line.

Run: go test -tags nogui ./internal/agent ./internal/gui; gofmt -l;
GOOS=linux and GOOS=windows go vet -tags nogui ./internal/agent
./internal/gui; the full GUI suite (node --test, both engines, -concurrency 6,
machine load ~140): 5130/5195, 64 failing in 22 files. Rerun alone, 16
files pass; mobile-web, old-webkit, panel-usage and routing-context/
group/keys-fold fail the same on the base commit, b6a263b9.

go test -tags nogui -timeout 40m ./... on the rebased tree: every package
ok but internal/gateway, whose TestQueuedStreamKeptAliveForTheMinute hit
its 1.5s read deadline at a machine load of ~130; it passes -count=3 run
alone, and this change touches nothing it runs.
2026-10-11 02:34:30 +08:00

105 lines
3.6 KiB
Go

package agent
// CodeBuddy Code (Tencent's CLI, npm @tencent-ai/codebuddy-code: codebuddy,
// cbc) is one package for the international edition (codebuddy.ai) and the
// China one (codebuddy.cn, CODEBUDDY_INTERNET_ENVIRONMENT=internal). Both
// take models of one's own from ~/.codebuddy/models.json ($CODEBUDDY_CONFIG_DIR
// if set), in WorkBuddy's format (workbuddy.go):
//
// {"models":[{"id":"deepseek/pro","name":…,"vendor":"magpie",
// "apiKey":"magpie-codebuddy","url":"http://127.0.0.1:3425/v1/chat/completions",
// "maxInputTokens":…,"maxOutputTokens":…,"supportsToolCall":true,…}],
// "availableModels":[…]}
//
// and pick up a change to it without a restart. Such a model needs no
// CodeBuddy sign-in: the CLI asks url for it under Bearer apiKey. The model
// a session starts on is settings.json's "model" (an id from that list, or
// one of CodeBuddy's own), read when a session starts. Its requests say
// "CLI/<ver> CodeBuddy/<ver>", so they are told apart by their key.
import (
"encoding/json"
"path/filepath"
"slices"
"strings"
"github.com/yetone/magpie/internal/edit"
)
func codebuddy(home string) *Agent { return codebuddyIn(here(home)) }
// codebuddyIn is CodeBuddy Code at a place: this machine's home, or a WSL
// distro's (see wsl.go), where CODEBUDDY_CONFIG_DIR isn't read and its
// models name the gateway as the distro reaches it, with the key it takes
// from there.
func codebuddyIn(at place) *Agent {
dir := at.getenv("CODEBUDDY_CONFIG_DIR")
if dir == "" {
dir = filepath.Join(at.home, ".codebuddy")
}
path := filepath.Join(dir, "models.json")
write := func(on bool) error { return buddyWriteAt(path, "codebuddy", on, at) }
settings := filepath.Join(dir, "settings.json")
model := func() string { v, _ := edit.GetJSON(settings, "model"); return v }
// ours is whether id is one of magpie's models in models.json
ours := func(id string) bool {
d, _ := workbuddyRead(path)
return slices.ContainsFunc(d.models, func(raw json.RawMessage) bool { e, ok := workbuddyMine(raw); return ok && e.ID == id })
}
return &Agent{
ID: "codebuddy", Name: "CodeBuddy Code", Icon: "codebuddy-color", Aliases: []string{"codebuddy-code", "cbc"},
UA: []string{"codebuddy"}, Spelled: prefixed,
Bin: "codebuddy", Dir: dir, Path: path,
Sync: func() error {
if !workbuddyWired(path) {
return nil
}
return write(true)
},
Notice: func() string {
if Running(`(^|/)(codebuddy|cbc|codebuddy-code)( |$)`) {
return noticeCodebuddy.String()
}
return ""
},
Fields: []Field{{
Key: "model", Label: "model",
Get: func() string {
m := model()
if m != "" && ours(m) {
return magpieID + "/" + m
}
return m
},
Set: func(v string) error {
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if err := write(true); err != nil {
return err
}
return edit.SetJSON(settings, edit.KV{Path: "model", Value: ref})
}
// CodeBuddy's own: magpie's models come out of its list
if err := write(false); err != nil {
return err
}
if v == "" {
return edit.DelJSON(settings, "model")
}
return edit.SetJSON(settings, edit.KV{Path: "model", Value: v})
},
Options: func(cur map[string]string) []Option {
var out []Option
if v := cur["model"]; v != "" && !usesMagpie(v) {
out = append(out, Option{Value: v, Icon: modelIcon("", v)})
}
return append(out, viaMagpie("codebuddy", magpieID+"/")...)
},
}},
}
}
// what codebuddy says after a change (notice.go)
var (
noticeCodebuddy = newNotice("CodeBuddy Code reads its model when a session starts — restart open codebuddy sessions, or run /clear in them, to use this.")
)