Files
magpie/internal/agent/aside.go
T
yetone cdfdb62c69 agent: a model field's list is joined with the catalog once while neither changed
Each agent's model field returned append(its own models,
viaMagpieShared(...)...), a copy of the whole catalog's options for
each field of each agent on every poll: Picks was 94.7 MB of 496 MB
over forty polls of the Agents page with real accounts. withVia keeps
the joined list per agent and prefix while the own models read the same
(sameOption, field by field) and the catalog's list is the very one
kept (same array), and joins it again when either changed. The list is
read only and clipped, as viaMagpieShared's is; every caller of a
field's Options reads it, Picks clones it before it drops anything.
The 52 `return append(own, viaMagpieShared(...)...)` sites call it.
omp's and Reasonix's planner edit the joined list after the append, and
Grok's writes Same into its copy, so they keep their own copies.

TestWithViaKeepsTheJoinedList: the same own models give the same array,
own models changed in a value or only an icon are joined anew, a
provider saved since is listed, and nothing joined is the catalog's own
list. TestSameOptionReadsEveryField sets each field of Option alone and
fails when sameOption doesn't see it. Broken by hand: never answering
from the kept list ("the same own models joined the catalog again"),
and FastFor left out of sameOption ("sameOption doesn't read
Option.FastFor").

Run on mote: both tests alone 3 of 3; internal/agent, internal/tui and
internal/cli ok; internal/gui red only on TestSettingsSaveKeepsWhatItDoesNotSend
(#4514) and TestMainWindowCorners (#3949); gofmt, vet and GOOS=windows
and linux vet clean. Real accounts in a sandbox HOME, forty polls:
Picks 94.7 MB -> 51.2 MB, state 190.8 MB -> 154.0 MB, state and drift
replies byte for byte the same as before. Footprint peak 136 MB.
2026-11-01 07:54:54 +08:00

100 lines
3.0 KiB
Go

package agent
import (
"bytes"
"context"
"fmt"
"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: at.join(".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 withVia(asideOwnOptions(c.models, cur[key]), "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
}