Support runtime backend switching via ctl

This commit is contained in:
Ollie Agent 2026-08-19 20:06:20 +02:00
parent 5a8f7e394e
commit 19971be343
2 changed files with 40 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import (
"os"
"runtime"
"slices"
"strings"
"sync"
"sync/atomic"
@ -298,6 +299,31 @@ func (ag *Agent) ModelName() string {
return ag.runtime.Backend.Model()
}
// SwitchBackend replaces the active backend by name. The new backend is fully
// constructed before the runtime is changed, so a failed switch leaves the
// current backend usable. Backend changes are only safe between turns.
func (ag *Agent) SwitchBackend(name string) error {
name = strings.TrimSpace(name)
if name == "" {
return fmt.Errorf("backend requires a name")
}
if ag.IsRunning() {
return fmt.Errorf("cannot switch backend while agent is running")
}
if ag.newBackend == nil {
return fmt.Errorf("backend switching unavailable")
}
be, err := ag.newBackend(name)
if err != nil {
return fmt.Errorf("backend %q: %w", name, err)
}
ag.runtime.Backend = be
ag.save()
ag.flush()
ag.notifyChange()
return nil
}
// State returns the agent's current execution state.
func (ag *Agent) State() string {
ag.stateMu.RLock()

View File

@ -1083,7 +1083,11 @@ func buildAgentChildren(a *agent.Agent, s *session.Session) []virtfs.FsNodeDecl
}
return nil, nil
}
return nil, fmt.Errorf("backend switching not supported via ctl; use cfg")
name := strings.Join(args, " ")
if err := a.SwitchBackend(name); err != nil {
return nil, err
}
return []byte(name + "\n"), nil
},
"name": func(args []string) ([]byte, error) {
if len(args) == 0 {