Files
magpie/internal/tui/keep.go
T
yetone 32c830fc16 tui: speaks the language picked in Settings, Simplified Chinese first
zaidu259434 on X asked why magpie's CLI/TUI pages don't support Chinese.
The GUI had en/zh/zh-TW/ja/de; the TUI had English only.

- internal/uilang: the language pick the tray already made (the
  setting, else the system's: zh-Hant regions to zh-TW, other zh to zh,
  ja, de) moved out of internal/gui so the TUI asks the same function.
  The tray calls it unchanged; its tests moved with it.
- internal/tui/i18n.go: tr/trf look a string up by its English text in
  a zh table (464 entries), falling back to the English. zh-TW reads
  Simplified for now; ja and de stay English. Terms come from the GUI's
  i18n.js where the same word is there (供应商, 路由, 用量, 会话, 资源库,
  分组, 方案 …).
- Every user-visible string in the TUI goes through tr/trf; widths that
  counted runes (Library's name column, sessions' columns) use
  lipgloss.Width, so 共享指令 is no longer cut to 共….
- Names stay names: agent and model ids, flags, commands, and the field
  labels magpie/ultracode/harness/opus/sonnet/haiku/fable.

TestEveryWordHasZh reads the TUI's source (every tr/trf literal, every
hints label, the tables shown through tr, agent field labels) and fails
on a word with no zh, on a zh format that takes the English one's
arguments badly, and on a zh entry no longer shown.

Not changed: the CLI's own output is still English (#6649).

Run:
- mica (macOS): go vet ./internal/uilang ./internal/gui ./internal/tui;
  go test -count=1 -timeout 45m ./... was still running when this
  was pushed (pushed on the coordinator's ask); its result goes on #6649.
- wsl-run (Linux): go vet + go test ./internal/tui ./internal/uilang ok;
  GOOS=windows go vet ./internal/uilang ./internal/gui ./internal/tui ok;
  GOOS=darwin CGO_ENABLED=0 go vet ./internal/uilang ./internal/tui ok.
- Real TUI (go build -tags nogui) in WSL magpie-desktop, sandbox HOME
  with settings.json {"lang":"zh"}, tmux 150x40: every page (Agents,
  Providers, Routing, Usage, Sessions, Library), the add-provider
  picker, sessions stats, the gateway line and footers read in Chinese.
  The Library row showed 共… before the lipgloss.Width fix, 共享指令 after.
- Broken by hand: an added tr("zzz untranslated") and a zh format with
  its %s dropped each turned TestEveryWordHasZh red.

Not tried: zh-TW, ja and de wording (they fall back as above); the TUI
on Windows' console.
2026-11-14 19:09:43 +08:00

143 lines
4.8 KiB
Go

package tui
import (
"os"
"path/filepath"
"runtime"
"strings"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/autostart"
"github.com/yetone/magpie/internal/daemon"
"github.com/yetone/magpie/internal/proc"
)
// On Linux with no desktop session (a WSL distro, a server over SSH)
// nothing opens magpie at login, so once magpie tui quit, the agents it had
// connected asked a gateway that was gone: Codex in WSL "连接会失败,没自动
// 启动magpie的网关" (zaidu259434 on X, with magpie installed in WSL and
// pointed at Windows' magpie as a remote one). As it quits having served
// the gateway with an agent connected, the terminal hands the gateway on:
// to magpie's service (magpie daemon), installed now where systemd runs it
// and the user never removed it, which starts it with the system or the
// user's login from then on; else to a `magpie serve` of its own, in the
// background until the system stops.
// keeper is what keepGateway asks and does; a var for the tests.
type keeper struct {
goos string
autostartOn func() bool
status func() (daemon.Status, error)
removed func() bool
start func() error
install func() error
linger func() string
background func() (pid int, log string, err error)
}
var keep = keeper{
goos: runtime.GOOS,
autostartOn: func() bool { return autostart.Enabled() != autostart.Off },
status: daemon.Get,
removed: daemon.Removed,
start: daemon.Start,
install: func() error { return daemon.Install(daemon.Options{}) },
linger: daemon.Linger,
background: serveInBackground,
}
// keepGateway hands the gateway on as the terminal quits having served it
// (see above), and is what to say of it.
func keepGateway(connected bool) string {
k := keep
if k.goos != "linux" || !connected {
return gatewayGoneNote()
}
if k.autostartOn() {
return atLoginNote()
}
st, err := k.status()
switch {
case err == nil && st.Installed:
if err := k.start(); err == nil {
return tr("magpie: the gateway goes on in magpie's service (magpie daemon status)")
}
case err == nil && !k.removed():
if err := k.install(); err == nil {
msg := tr("magpie: the gateway goes on in magpie's service, which now starts with this system or your login, so agents connected to magpie reach it with magpie tui closed; magpie daemon uninstall removes it")
if l := k.linger(); l != "" {
msg += " (" + l + ")"
}
return msg
}
}
pid, log, err := k.background()
if err != nil {
return gatewayGoneNote() + " " + trf("(couldn't start magpie serve in the background: %s)", err.Error())
}
msg := trf("magpie: the gateway goes on in the background (magpie serve, pid %d, log %s) until this system stops", pid, log)
if wslHere() {
msg += tr("; for it to start with WSL, turn systemd on with [boot] systemd=true in /etc/wsl.conf and run magpie daemon install")
} else {
msg += tr("; magpie daemon install starts it with the system")
}
return msg
}
// atLoginNote is said where magpie opens at login, so the terminal left
// the gateway to the app. On Linux that can be a package's own autostart
// entry on a box no desktop logs into (NixOS-WSL, a server over SSH),
// which nothing ever starts (#3756): the note says the switch that has the
// terminal hand the gateway on instead.
func atLoginNote() string {
return gatewayGoneNote() + tr("; magpie opens at login and serves it then. Where no desktop logs in here, `magpie autostart off` has magpie tui hand it on as it quits")
}
// BackgroundLog is the log of a magpie serve that magpie tui left
// running as it quit (serveInBackground).
func BackgroundLog() string { return filepath.Join(appdir.Cache(), "serve.log") }
// serveInBackground starts `magpie serve` on its own, outliving this
// process and its terminal, its output in a log in magpie's cache.
func serveInBackground() (int, string, error) {
exe, err := os.Executable()
if err != nil {
return 0, "", err
}
log := BackgroundLog()
if err := os.MkdirAll(filepath.Dir(log), 0o700); err != nil {
return 0, "", err
}
f, err := os.OpenFile(log, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
if err != nil {
return 0, "", err
}
defer f.Close()
cmd := proc.Command(exe, "serve")
cmd.Stdout, cmd.Stderr = f, f
detach(cmd)
// reaped if it ends while this process runs: a released one stayed
// <defunct> until it quit
if err := proc.StartReaped(cmd); err != nil {
return 0, "", err
}
return cmd.Process.Pid, log, nil
}
// wslHere: this Linux is a WSL distro.
func wslHere() bool {
b, _ := os.ReadFile("/proc/version")
return strings.Contains(strings.ToLower(string(b)), "microsoft")
}
// connected says one of agents is connected to magpie.
func connected(agents []*agent.Agent) bool {
for _, a := range agents {
if a.Routed != nil && a.Routed() {
return true
}
}
return false
}