Files
magpie/internal/agent/aside.go
T
Junjie Zhou 4a97acf12a refactor(agent): separate Aside provider connection from model settings (#827)
* refactor(agent): connect Aside as a native provider with verified settings

* fix(agent): offer explicit offline Aside model and disconnect actions

Offer Save for next start after an initial runtime read failure, using a
structured API error instead of saved runtime state. Keep refusals and
failed readbacks on the live error path.

Add a separate offline disconnect confirmation that restores the checked
file plan, clears restore records and removes the provider last. Bind the
confirmation to its preview revision and roll back ordinary write failures.

Cover offline API and browser flows, cancellation, stale plans, missing
CLI, restore metadata and native provider spelling. Keep state file-only.
2026-10-05 14:37:44 +08:00

89 lines
2.6 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)
}
return a
}
var asideRoles = []string{"fast", "standard", "deep", "visual"}
const asideImageKey = "imageGenerationModel"
const asideAccount = 0
func asideAccountID() string { return "u0" }
func asideDir(at place) string { return filepath.Join(at.home, ".aside", "u", "0") }
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
}