Files
magpie/internal/agent/aside.go
T
yetone d06df18255 agent: magpie configures the Aside account Aside has active, or the one picked on its row (#1499, fjrtdk)
Aside keeps one folder per account (~/.aside/u/<id>, each with its own
settings.json, models.json, credentials.json and state.db), and which one is
in use is currentAccountId in ~/.aside/accounts.json, which the app and
`aside account use <id>` both set. magpie only ever wrote u/0 and asked the
runtime about --account u0, so with u1 active Connect changed an account
the reporter wasn't using.

- magpie now follows currentAccountId. An accounts.json that is missing,
  doesn't parse, names no active account, or names one with no folder is
  "unknown" and keeps u0, as before; it is never written.
- The Aside row gets an account field (under Details, shown only with more
  than one account) listing each account with its e-mail or "Local
  Account" and which one is active in Aside; `magpie aside account u1` does
  the same from the CLI. The pick is kept in magpie's own
  aside-account.json; Default removes it and follows Aside again. A pick
  whose folder is gone is passed over.
- What magpie remembers to put back on Disconnect is per account
  (aside-state.json for u0, as before, aside-state-uN.json for others), so
  switching accounts never restores one account's model into another. The
  legacy stash still migrates into u0's only.
- All runtime reads and writes go to `aside repl --account uN` for the
  account in use.
- GUI strings in en, zh, zh-TW, ja, de.

Run: go test -tags nogui ./... in a sandbox HOME; new
aside_account_test.go uses the reporter's accounts.json bytes
(TestAsideFollowsTheActiveAccount, TestAsideUnreadableAccountsKeepU0,
TestAsidePickedAccountOverridesActive, TestAsideRestorePointsArePerAccount,
TestAsideKeepsModelsPermissions); with asideChoice forced to 0 the follow,
pick and restore tests fail, and with one shared record the restore test
fails. GUI: aside-account.test.cjs (chromium and webkit, 980 and 360px,
en/zh/zh-TW/ja/de) plus aside-native, i18n-keys, agent-field-names,
agent-layout and agent-models-picker, 45 passing. gofmt -l; GOOS=linux and
GOOS=windows go vet -tags nogui ./internal/agent ./internal/gui.
2026-10-10 23:35:05 +08:00

101 lines
3.0 KiB
Go

package agent
import (
"bytes"
"context"
"fmt"
"path/filepath"
"time"
"github.com/yetone/magpie/internal/proc"
)
func aside(home string) *Agent { return asideIn(here(home)) }
func asideIn(at place) *Agent {
c := newAsideConnection(at)
a := &Agent{ID: "aside", Name: "Aside", Icon: "aside", Bin: "aside", Path: c.path, Spelled: prefixed, Dir: filepath.Join(at.home, ".aside"), Sync: c.sync}
a.Native = &NativeConnection{Read: c.state, Connect: c.connect, Apply: c.apply, Stage: c.stage, Disconnect: c.plan, Execute: c.execute, ExecuteOffline: c.executeOffline}
a.Check = func() string {
status, detail := c.provider()
if status == "invalid" {
return detail
}
return ""
}
a.Joined = func() bool { status, _ := c.provider(); return status == "connected" }
for _, key := range append([]string{"model", "effort", "image"}, asideRoles...) {
label := key
if key == "effort" {
label = "thinking"
}
f := Field{Key: key, Label: label, Quiet: key != "model" && key != "effort", Get: func() string { return c.value(key) }, Set: func(v string) error { return c.apply(key, v) }}
if f.Quiet && key != "image" {
f.Follows = "model"
}
f.Options = func(cur map[string]string) []Option {
if key == "effort" {
if offered := piOffered("aside", cur["model"]); offered != nil {
return static(offered...)
}
return static(piLevels...)
}
if key == "image" {
opts := asideImageOptions()
if v := cur[key]; v != "" {
found := false
for _, o := range opts {
if o.Value == v {
found = true
}
}
if !found {
opts = append([]Option{{Value: v, Note: "current image selection"}}, opts...)
}
}
return opts
}
return append(asideOwnOptions(c.models, cur[key]), viaMagpie("aside", "magpie/")...)
}
a.Fields = append(a.Fields, f)
}
// which of Aside's accounts magpie configures: empty follows the one
// Aside has active (#1499)
a.Fields = append(a.Fields, Field{Key: "account", Label: "account", Quiet: true,
Get: func() string {
if id, ok := asidePicked(); ok {
return asideAccountName(id)
}
return ""
},
Set: c.pickAccount,
Options: func(cur map[string]string) []Option { return asideAccountOptions(at, cur["account"]) },
})
return a
}
var asideRoles = []string{"fast", "standard", "deep", "visual"}
const asideImageKey = "imageGenerationModel"
// asideDir is the folder of the Aside account magpie configures
// (asideChoice).
func asideDir(at place) string { return asideAccountDir(at, asideChoice(at)) }
var asideTimeout = 10 * time.Second
const asideOK = "MAGPIE_ASIDE_OK"
var asideSet = func(account, expr string) error {
ctx, cancel := context.WithTimeout(context.Background(), asideTimeout)
defer cancel()
out, err := proc.CommandContext(ctx, "aside", "repl", "--account", account, "--host", "local", expr).Output()
if err != nil {
return fmt.Errorf("Aside did not accept the settings change: %w", err)
}
if !bytes.Contains(out, []byte(asideOK)) {
return fmt.Errorf("Aside did not confirm the settings change")
}
return nil
}