Files
magpie/internal/agent/avibe.go
T
yetone 9c39edb92c agent: Avibe, magpie as a model source through its Model Hub (alex_metacraft on X)
Avibe (avibe-bot/avibe, avibe-os on PyPI, the `vibe` CLI) runs its own
agent, Vibey, and Claude Code, Codex and OpenCode, all on models from its
Model Hub. Its sources live in model_hub.sources of config/config.json in
$AVIBE_HOME (else ~/.avibe, else a legacy ~/.vibe_remote), but a source
can't be written there by hand: its key is sealed in the hub's credential
store and Avibe puts it into its agents' route chains as it is made.

So the Avibe row adds "magpie" the way Avibe's Models page does, through
its internal API (POST /internal/model-hub on state/dispatch.sock, or on
Windows the loopback port + bearer of runtime/control-ipc/endpoint.json):
create_source with kind api_key, vendor custom, base_url the gateway's
/v1, key agentKey("avibe"), protocol openai_chat and save_unverified.
Avibe then lists the models from /v1/models and adds the source to
Vibey's, Claude's, Codex's and OpenCode's chains itself.

- openai_chat, not openai_responses: for a Responses source Avibe's
  CLIProxyAPI runs Codex's executor, which sends codex-tui headers the
  gateway would take for Codex.
- Off is delete_source with force, sent again with the would_remove_hops
  and would_interrupt Avibe answers, as its page confirms them. Sync is
  refresh_source the same way; on a moved gateway the source is made
  again (the key goes with the address; patch_source can't change it).
- The source id is stashed; a custom source at the gateway's /v1 counts
  as magpie's too and is stashed once seen.
- Every change needs Avibe running; otherwise the switch says to run
  `vibe start` (errAvibeDown). Nothing is written to config.json.
- A sync moves the source only from the gateway's own address on an
  older port (avibeMoved). One the user pointed at a NAS's or the LAN's
  magpie, a local proxy's route, or a WSL distro's way to Windows stays,
  as kept.away keeps every file-written agent's; a Rewire after a port
  move sets it again through the field's Set.
- Every test of the package but Avibe's own finds an Avibe running for
  each home (runningAvibe, served in-process), so the tests that connect
  every agent (gateway moved, LAN and proxy addresses kept, drift,
  ListOnly) take Avibe in too.
- The logo is a tile solid to its edges, so it joins app.css's tile rule
  (icon-tile.test.cjs went red without it).
- AVIBE_HOME joins agentenv and the ~-expanding path vars (Avibe expands
  it). Icon from Avibe's assets/logo.png (MIT), 64px.

Run:
- Real Avibe 3.2.5 (uv tool install avibe-os) in a sandbox HOME in WSL
  magpie-desktop, with a fake OpenAI server as the gateway: magpie's own
  code (a throwaway test, AVIBE_HOME + MAGPIE_ADDR) connected it, Avibe
  discovered both fake models with "Authorization: Bearer magpie-avibe"
  and put the source in vibey/claude/codex/opencode chains; Sync
  refreshed; a gateway move to :3592 made the source again there and
  stashed the new id; a second run found the source by address alone
  and still followed the move; off removed it (list_sources []).
  Earlier, by hand: Anthropic /v1/messages and /v1/responses through the
  hub reached the fake as chat completions.
- wsl-run, at this head rebased on origin/main 021a6e1fd (then 401187300, where the Avibe and all-agents tests, i18n-keys, icon-tile and the gui -run set were run again: ok on Linux and Windows): go test
  -tags nogui ./internal/agent ./internal/appdir ./internal/agentenv:
  appdir, agentenv ok; agent ok but TestGrokInstalled, red on WSL2 at
  main (#3548).
- win-run: the Avibe tests and the all-agents tests (drift, port move,
  LAN, proxy, ListOnly, pickers): ok. The whole internal/agent package
  on Windows (one commit earlier) has main's known Windows reds (#4115,
  #5068, #4846, #5130, #5344, ...); its two Avibe reds (drift, ListOnly)
  are what runningAvibe fixed.
- vet (on the Windows box): windows, GOOS=darwin, GOOS=linux nogui on
  agent/appdir/agentenv: ok.
  gofmt clean.
- GUI (chromium + webkit, win-run): i18n-keys, i18n-en-no-cjk,
  icon-one-tone, routing-client-icons, icon-tile: 63 of 63 ok; go test
  ./internal/gui -run 'Icon|Budget|CSS|I18n|Picker': ok.
- mote refused SSH all session, so everything ran on wsl-run/win-run.

Broken by hand:
- a sync moving any address that isn't the gateway's: the LAN- and
  proxy-kept tests red on Avibe's config.json.
- app.css without avibe in the tile rule: icon-tile.test.cjs red.
- protocol openai_responses: TestAvibeAddsMagpieAsAModelSource red.
- confirmed() not resending the echoed hops: the add, knows-its-source
  and follows-the-gateway tests red (source_in_route_chain).

Not tried: magpie on Windows wiring an Avibe that runs inside WSL; a
real Vibey turn against the real gateway (the fake stood in for it);
macOS with a real Avibe.
2026-11-10 19:36:59 +08:00

407 lines
13 KiB
Go

package agent
// Avibe (avibe-bot/avibe, MIT, the avibe-os package and its `vibe` CLI) is
// a local agent OS: its own agent, Vibey, and the Claude Code, Codex and
// OpenCode it runs all take their models from its Model Hub, a gateway of
// its own (CLIProxyAPI) in front of the model sources added on its Models
// page. Its home is $AVIBE_HOME, else ~/.avibe (~/.vibe_remote before 3.0:
// config/paths.py), and the sources are model_hub.sources in
// config/config.json there.
//
// magpie is one such source, "magpie": an API-key source of the custom
// vendor at the gateway's /v1, spoken to as chat completions with an agent
// key of Avibe's own. A hub source serves every backend
// (source_eligible_for_backend), the Model Hub translating Claude's and
// Codex's requests, and Avibe lists its models from the gateway's
// /v1/models itself, so the models picked for Avibe on the Agents page
// are what it offers.
//
// Chat completions rather than Responses: for a Responses source the Model
// Hub runs Codex's executor, which names itself codex-tui and would be
// taken for Codex itself (fromCodex); for chat it passes the backend's own
// headers on.
//
// A source can't be written into config.json by hand: each is checked
// whole, its key lives in the Model Hub's store under a credential_ref, and
// Avibe adds it to its agents' route chains as it is added. So magpie adds
// and removes it as Avibe's Models page does, through Avibe's internal API
// (core/handlers/model_hub/rpc.py), while Avibe runs:
//
// POST /internal/model-hub {"operation": "create_source"|"delete_source"|"refresh_source", "payload": {…}}
//
// on the socket state/dispatch.sock in its home (core/control_ipc.py), or
// on Windows at the loopback port runtime/control-ipc/endpoint.json gives
// with its bearer token. The source's id is stashed, so a renamed one is
// still magpie's.
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
)
// avibeStash is the stash key for the id of magpie's source in the Avibe
// at dir.
func avibeStash(dir string) string { return "avibe.source:" + dir }
// errAvibeDown is a change asked of an Avibe that isn't running.
var errAvibeDown = errors.New("Avibe adds and removes model sources only while it runs: start it (vibe start), then try again")
func avibe(home string) *Agent {
dir := avibeHome(home)
path := filepath.Join(dir, "config", "config.json")
wired := func() bool { _, ok := avibeOurs(dir, path); return ok }
return &Agent{
ID: "avibe", Name: "Avibe", Icon: "avibe", Aliases: []string{"vibe", "vibey", "avibe-os"},
Dir: dir, Path: path,
detect: func() bool { return isDir(dir) },
ListsModels: true,
ListOnly: true,
// a model picked or dropped for Avibe reaches its source when it
// lists the gateway's models again; Avibe does that itself only
// for a listing older than half an hour
Sync: func() error {
src, ok := avibeOurs(dir, path)
if !ok {
return nil
}
c := avibeLive(dir)
if c == nil {
return nil
}
if avibeMoved(src.BaseURL) {
return avibeWire(c, dir, src, true)
}
avibeKeep(dir, src.ID)
return c.confirmed("refresh_source", map[string]any{"source_id": src.ID})
},
Check: func() string {
src, ok := avibeOurs(dir, path)
if !ok {
return ""
}
return wiringOff("Avibe", path, func(string) (string, bool) { return strings.TrimSuffix(src.BaseURL, "/"), true }, "base_url", gatewayV1())
},
Fields: []Field{{
Key: "provider", Label: "provider",
Get: func() string {
if wired() {
return magpieID
}
return ""
},
Set: func(v string) error {
src, there := avibeOurs(dir, path)
if v == "" && !there {
return nil
}
if v != "" && there && strings.TrimSuffix(src.BaseURL, "/") == gatewayV1() {
avibeKeep(dir, src.ID)
return nil
}
c := avibeLive(dir)
if c == nil {
return errAvibeDown
}
if v == "" {
if err := c.confirmed("delete_source", map[string]any{"source_id": src.ID}); err != nil {
return err
}
forget(avibeStash(dir))
return nil
}
return avibeWire(c, dir, src, there)
},
Options: func(map[string]string) []Option {
return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "a model source on Avibe's Models page, for Vibey and the agents it runs"}}
},
}},
}
}
// avibeWire adds magpie's source to the Avibe c serves, in place of src
// when there is one: its key is sealed in the Model Hub and goes with the
// gateway's address, so a source on an old address is made again rather
// than patched.
func avibeWire(c *avibeClient, dir string, src avibeSource, there bool) error {
if there {
if err := c.confirmed("delete_source", map[string]any{"source_id": src.ID}); err != nil {
return err
}
}
id, err := c.create()
if err != nil {
return err
}
stash(map[string]string{avibeStash(dir): id})
return nil
}
// avibeStashed is the id of the source magpie added to the Avibe at dir.
func avibeStashed(dir string) string {
stashMu.Lock()
defer stashMu.Unlock()
return stashShared()[avibeStash(dir)]
}
// avibeKeep stashes id as magpie's source in the Avibe at dir, one found
// by its address (added by hand, or before the stash was lost), so it is
// still known once the gateway moves.
func avibeKeep(dir, id string) {
if avibeStashed(dir) != id {
stash(map[string]string{avibeStash(dir): id})
}
}
// avibeHome is Avibe's home: $AVIBE_HOME, else ~/.avibe, else the
// ~/.vibe_remote of an Avibe from before the move (paths.py's
// get_vibe_remote_dir).
func avibeHome(home string) string {
if d := strings.TrimSpace(appdir.Getenv("AVIBE_HOME")); d != "" {
if rest, ok := strings.CutPrefix(d, "~"); ok && (rest == "" || rest[0] == '/' || rest[0] == filepath.Separator) {
d = home + rest
}
if abs, err := filepath.Abs(d); err == nil {
d = abs
}
return d
}
d := filepath.Join(home, ".avibe")
if _, err := os.Lstat(d); err != nil {
if legacy := filepath.Join(home, ".vibe_remote"); isDir(legacy) {
return legacy
}
}
return d
}
// avibeSource is a model source as config.json keeps it.
type avibeSource struct {
ID string `json:"id"`
Vendor string `json:"vendor"`
DisplayName string `json:"display_name"`
BaseURL string `json:"base_url"`
}
// avibeOurs is magpie's source in the config.json at path: the one whose
// id magpie stashed when it added it, else one of the custom vendor at the
// gateway (added by hand on Avibe's Models page).
func avibeOurs(dir, path string) (avibeSource, bool) {
raw, ok := edit.GetJSON(path, "model_hub.sources")
if !ok {
return avibeSource{}, false
}
var srcs []avibeSource
if json.Unmarshal([]byte(raw), &srcs) != nil {
return avibeSource{}, false
}
if id := avibeStashed(dir); id != "" {
for _, s := range srcs {
if s.ID == id {
return s, true
}
}
}
for _, s := range srcs {
if s.ID != "" && s.Vendor == "custom" && strings.TrimSuffix(s.BaseURL, "/") == gatewayV1() {
return s, true
}
}
return avibeSource{}, false
}
// avibeProbe gives up quickly: an Avibe that doesn't answer at once isn't
// running.
const avibeProbe = 1500 * time.Millisecond
// avibeWait is how long a change may take: adding a source lists its
// models, and removing one rewrites every route chain it is in.
const avibeWait = 30 * time.Second
// avibeClient is a running Avibe's internal API.
type avibeClient struct {
http *http.Client
base string
token string
}
// avibeMoved says base, magpie's source's address, is the gateway's own
// on an older port, which a sync moves to where the gateway is now. One
// the user pointed at magpie on another machine (a NAS's, the LAN
// address), through a local proxy's route (a path before /v1), or the way
// a WSL distro reaches Windows stays where it is, as every other agent's
// sync keeps it (kept.away).
func avibeMoved(base string) bool {
base = strings.TrimSuffix(base, "/")
if base == gatewayV1() {
return false
}
root := strings.TrimSuffix(base, "/v1")
return root != base && !onAnotherMachine(root) && !loopbackWithPath(root) && !wslHost(root)
}
// avibeLive is avibeConnect, for the tests to stand in for.
var avibeLive = avibeConnect
// avibeConnect is the internal API of the Avibe at dir if it answers, nil
// if it isn't running: its socket, or on Windows its endpoint file,
// outlives a crash.
func avibeConnect(dir string) *avibeClient {
c := avibeAt(dir)
if c == nil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), avibeProbe)
defer cancel()
if _, err := c.callCtx(ctx, "list_sources", map[string]any{}); err != nil {
return nil
}
return c
}
// avibeAt is where the Avibe at dir listens, as its own CLI finds it
// (control_ipc.py's resolve_client_endpoint), or nil with nothing there.
func avibeAt(dir string) *avibeClient {
if runtime.GOOS == "windows" {
b, err := os.ReadFile(filepath.Join(dir, "runtime", "control-ipc", "endpoint.json"))
if err != nil {
return nil
}
var d struct {
Transport string `json:"transport"`
Host string `json:"host"`
Port int `json:"port"`
Token string `json:"bearer_token"`
}
if json.Unmarshal(b, &d) != nil || d.Transport != "tcp" || d.Host != "127.0.0.1" || d.Port <= 0 || d.Port > 65535 || d.Token == "" {
return nil
}
return &avibeClient{http: &http.Client{}, base: fmt.Sprintf("http://127.0.0.1:%d", d.Port), token: d.Token}
}
sock := filepath.Join(dir, "state", "dispatch.sock")
if fi, err := os.Stat(sock); err != nil || fi.Mode()&os.ModeSocket == 0 {
return nil
}
tr := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "unix", sock)
}}
return &avibeClient{http: &http.Client{Transport: tr}, base: "http://localhost"}
}
// avibeError is a refusal from Avibe's Model Hub.
type avibeError struct {
Code, Detail string
// what it would take away, for a change it asks to have confirmed
Hops, Interrupts json.RawMessage
}
func (e *avibeError) Error() string {
msg := "Avibe: " + e.Code
if e.Detail != "" && e.Detail != "modelHub.errors."+e.Code {
msg += " (" + e.Detail + ")"
}
return msg
}
func (c *avibeClient) call(op string, payload map[string]any) (json.RawMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), avibeWait)
defer cancel()
return c.callCtx(ctx, op, payload)
}
func (c *avibeClient) callCtx(ctx context.Context, op string, payload map[string]any) (json.RawMessage, error) {
b, err := json.Marshal(map[string]any{"operation": op, "payload": payload})
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST", c.base+"/internal/model-hub", bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if c.token != "" {
req.Header.Set("Authorization", "Bearer "+c.token)
}
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("Avibe: %w", err)
}
defer resp.Body.Close()
var out struct {
OK bool `json:"ok"`
Result json.RawMessage `json:"result"`
Error string `json:"error"`
Detail string `json:"detail"`
Hops json.RawMessage `json:"would_remove_hops"`
Interrupts json.RawMessage `json:"would_interrupt"`
}
if err := json.NewDecoder(io.LimitReader(resp.Body, 32<<20)).Decode(&out); err != nil {
return nil, fmt.Errorf("Avibe: %s %s: %w", resp.Status, op, err)
}
if !out.OK {
if out.Error == "" {
out.Error = resp.Status
}
return nil, &avibeError{Code: out.Error, Detail: out.Detail, Hops: out.Hops, Interrupts: out.Interrupts}
}
return out.Result, nil
}
// confirmed runs a change Avibe may ask to have confirmed first, as its
// Models page does when a source leaves routes behind: asked again with
// what it said it would take away. The user asked for it in magpie.
func (c *avibeClient) confirmed(op string, payload map[string]any) error {
payload["force"] = true
_, err := c.call(op, payload)
var e *avibeError
if !errors.As(err, &e) || (len(e.Hops) == 0 && len(e.Interrupts) == 0) {
return err
}
if len(e.Hops) > 0 {
payload["would_remove_hops"] = e.Hops
}
if len(e.Interrupts) > 0 {
payload["would_interrupt"] = e.Interrupts
}
_, err = c.call(op, payload)
return err
}
// create adds magpie's source and gives its id. It is saved without
// Avibe's protocol probe, which can't tell the gateway's protocols apart
// (it serves them all), and lists its models from the gateway at once.
func (c *avibeClient) create() (string, error) {
res, err := c.call("create_source", map[string]any{"source": map[string]any{
"kind": "api_key", "vendor": "custom", "display_name": magpieID,
"base_url": gatewayV1(), "key": agentKey("avibe"),
"protocol": "openai_chat", "supply_channel": "hub", "billing": "metered",
"save_unverified": true,
}})
if err != nil {
return "", err
}
var out struct {
Source struct {
ID string `json:"id"`
} `json:"source"`
}
if json.Unmarshal(res, &out) != nil || out.Source.ID == "" {
return "", errors.New("Avibe added no source")
}
return out.Source.ID, nil
}