xiaoxiaofei. asked for LiveAgent (Stack-Cairn/LiveAgent, v2). LiveAgent 2 is a Tauri app that runs its models through K-brain, a Go backend it starts as `k-brain backend -data-dir <root>`; every provider lives in <root>/config.json (JSONC, Pi's provider shape). Root: $LIVEAGENT_HOME, else $K_BRAIN_HOME, else the folder ~/.liveagent/data-dir.json points to, else ~/.liveagent, on every OS (LiveAgent's data_home.rs and kbrain_backend.rs, K-brain's internal/config and internal/storage). Connect adds providers.magpie: type claude_code + api anthropic-messages (what LiveAgent's own settings write for an Anthropic-compatible provider), baseUrl the gateway root (K-brain appends /v1 and posts /messages with X-Api-Key), the key naming LiveAgent, activeModels null, modelOrder in magpie's order, models sorted by id with display name, window, output limit and input [text, image]. K-brain decodes with unknown fields refused, so only its fields are written. A default or compact model still K-brain's endpoint-less demo model1 becomes magpie's first; the user's own pick stays. Through a sync the user's name, the models they turned off, their order, K-brain's metadata and a model's own displayName stay; a new model is on and last; nothing to change writes nothing. A file K-brain wouldn't read as providers (not an object, providers not an object, top-level models, a bare provider) is left alone. Disconnect takes the entry out, puts picks back to K-brain's default, gives a file left with no provider K-brain's demo back (its CLI restores config.json.bak, which could hold magpie's, from a file with none), and removes a file holding only what magpie made. LiveAgent's backend holds the settings and writes them whole on any change, so a change is refused while liveagent or k-brain runs (macOS/Linux; the notice says to quit it first, in every language). Logo: the app's own 64px icon (src-tauri/icons/64x64.png, MIT) in an SVG. Run on mote at this tree: - gofmt -l clean; go vet ./internal/agent ./internal/agentenv for darwin, linux (nogui) and windows - go test -tags nogui ./internal/agent -run 'LiveAgent|GatewayKey|ListsModels' ok; ./internal/agentenv ok; ./internal/gui: only TestCoreScriptBudget red, which is red on its base too (app.js untouched here; #5139) - GUI icon-tile, i18n-keys, agent-notice-lang, gui-zh-tw, gui-ja, gui-de: Chromium 27/27, WebKit 27/27 Broken by hand on mote, each red: the user's activeModels not kept (TestLiveAgentProvider "activeModels after sync [], want [...]"); the demo provider not put back (TestLiveAgentMakesItsFile "emptied file"); the demo default not replaced (TestLiveAgentProvider, TestLiveAgentMakesItsFile). Real use on mote (sandbox HOME and LIVEAGENT_HOME, this magpie on 127.0.0.1:3562, a fake Anthropic upstream on 3561, LiveAgent 2.0.5's bundled k-brain from the release dmg): - k-brain backend's first start wrote config.json as {}; magpie liveagent magpie then wrote providers.magpie and the default pick - `k-brain run "Reply with exactly: pong"` (K-brain's CLI, same config and routing code) answered "pong"; the upstream saw POST /v1/messages from magpie - the backend LiveAgent starts listed magpie's model in /v1/models with it as the default, and its /v1/text/generate answered "pong" through magpie - with k-brain running, connect/disconnect were refused with the notice; after it quit, disconnect removed the file magpie made Not tried: LiveAgent's window itself (its model picker was not opened; the backend's API it reads was); LiveAgent on Linux or Windows; a real vendor reply (fake upstream).
263 lines
9.0 KiB
Go
263 lines
9.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/url"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
// An agent in a WSL distro under NAT reaches the gateway at Windows'
|
|
// address, where gateway.Token is turned away (whqtian on Discord: OpenCode
|
|
// in WSL was written "apiKey": "magpie" and refused). Every agent's config
|
|
// carries the LAN sharing key there while sharing is on, and gateway.Token on
|
|
// loopback, or while nothing is shared.
|
|
func TestGatewayKeyBeyondLoopback(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
wsl := "http://172.23.80.1:3425"
|
|
local := "http://127.0.0.1:3425"
|
|
|
|
keys := func(gw string) map[string]string {
|
|
oc := magpieProviderJSONAt("opencode", "opencode", gw).(map[string]any)["options"].(map[string]any)
|
|
cr := magpieProviderJSONAt("crush", "crush", gw).(map[string]any)
|
|
pi := magpieProviderJSONAt("pi", "pi", gw).(map[string]any)
|
|
out := map[string]string{
|
|
"opencode": oc["apiKey"].(string),
|
|
"crush": cr["api_key"].(string),
|
|
"pi": pi["apiKey"].(string),
|
|
"hermes": hermesProviderAt(gw).APIKey,
|
|
}
|
|
for _, e := range droidEntriesAt(gw) {
|
|
out["droid"] = e.APIKey
|
|
}
|
|
return out
|
|
}
|
|
want := func(gw, key string) {
|
|
t.Helper()
|
|
for agent, k := range keys(gw) {
|
|
if k != key {
|
|
t.Errorf("%s at %s: key %q, want %q", agent, gw, k, key)
|
|
}
|
|
}
|
|
}
|
|
|
|
want(wsl, gateway.Token) // nothing shared: nothing else to give
|
|
if err := access.ConfigureLAN(true, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lan := access.LANSecret()
|
|
if !strings.HasPrefix(lan, access.Prefix) {
|
|
t.Fatalf("LAN key %q", lan)
|
|
}
|
|
want(wsl, lan)
|
|
want(local, gateway.Token)
|
|
want("http://localhost:3425", gateway.Token)
|
|
if k := qoderProviderAt("qoder", "", wsl+"/v1")["apiKey"]; k != lan {
|
|
t.Errorf("Qoder in WSL: key %q, want the LAN key", k)
|
|
}
|
|
if k := qoderProviderAt("qoder", "", local+"/v1")["apiKey"]; k != gateway.TokenFor("qoder") {
|
|
t.Errorf("Qoder on loopback: key %q", k)
|
|
}
|
|
if !ourKey(lan) || !ourKey(gateway.Token) || ourKey("sk-someone-else") || ourKey("") {
|
|
t.Error("ourKey tells magpie's keys from others wrongly")
|
|
}
|
|
if !qoderKeyed(lan, "qoder") {
|
|
t.Error("a Qoder given the LAN key isn't seen as on magpie")
|
|
}
|
|
|
|
if err := access.ConfigureLAN(false, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want(wsl, gateway.Token)
|
|
}
|
|
|
|
// T3 Code's magpie instance is Claude Code routed with the gateway's
|
|
// address and the token Claude Code is sent with: beyond loopback that is
|
|
// the LAN sharing key, as at.gwKey() routes Claude Code itself.
|
|
// gateway.Token is turned away there, so T3's every call was answered 401
|
|
// (#1647, ggbdpq).
|
|
func TestT3CodeKeyBeyondLoopback(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
path := filepath.Join(home, ".t3", "userdata", "settings.json")
|
|
wsl := "http://172.23.80.1:3425"
|
|
|
|
env := func(gw string) map[string]string {
|
|
t.Helper()
|
|
b, err := json.Marshal(t3InstanceJSON(path, gw))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e := t3Format(t, b)
|
|
out := map[string]string{}
|
|
for _, v := range e.Environment {
|
|
out[v.Name] = v.Value
|
|
}
|
|
return out
|
|
}
|
|
|
|
if err := access.ConfigureLAN(true, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { access.ConfigureLAN(false, false) })
|
|
lan := access.LANSecret()
|
|
if !strings.HasPrefix(lan, access.Prefix) {
|
|
t.Fatalf("LAN key %q", lan)
|
|
}
|
|
if e := env(wsl); e["ANTHROPIC_AUTH_TOKEN"] != lan {
|
|
t.Errorf("T3 Code at %s: token %q, want the LAN sharing key", wsl, e["ANTHROPIC_AUTH_TOKEN"])
|
|
}
|
|
if e := env("http://127.0.0.1:3425"); e["ANTHROPIC_AUTH_TOKEN"] != gateway.Token {
|
|
t.Errorf("T3 Code on loopback: token %q, want %q", e["ANTHROPIC_AUTH_TOKEN"], gateway.Token)
|
|
}
|
|
if err := access.ConfigureLAN(false, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if e := env(wsl); e["ANTHROPIC_AUTH_TOKEN"] != gateway.Token {
|
|
t.Errorf("T3 Code at %s with nothing shared: token %q, want %q", wsl, e["ANTHROPIC_AUTH_TOKEN"], gateway.Token)
|
|
}
|
|
}
|
|
|
|
// Air's and OpenChamber's OpenCode provider block carries the key for the
|
|
// gateway's address, and each row's Check compares against it: beyond
|
|
// loopback, the LAN sharing key. Against gateway.Token there, the rows said
|
|
// "magpie's gateway won't take its requests" of the config Sync had just
|
|
// written and the gateway does take (#1647, ggbdpq).
|
|
func TestAirAndOpenChamberChecksTakeTheKeyForTheGateway(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
t.Setenv("MAGPIE_ADDR", "172.23.80.1:3425")
|
|
if err := access.ConfigureLAN(true, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { access.ConfigureLAN(false, false) })
|
|
lan := access.LANSecret()
|
|
if !strings.HasPrefix(lan, access.Prefix) {
|
|
t.Fatalf("LAN key %q", lan)
|
|
}
|
|
if gateway.URL() != "http://172.23.80.1:3425" {
|
|
t.Fatalf("gateway URL %q", gateway.URL())
|
|
}
|
|
|
|
airDir := airDir(home, filepath.Join(home, ".config"))
|
|
was := airOpenCode
|
|
airOpenCode = func(string) string { return "/opt/bin/opencode" }
|
|
t.Cleanup(func() { airOpenCode = was })
|
|
a := air(home, filepath.Join(home, ".config"))
|
|
if err := a.Apply("model", "magpie/fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg := filepath.Join(airDir, "magpie-opencode.json")
|
|
if v, _ := edit.GetJSON(cfg, "provider.magpie.options.apiKey"); v != lan {
|
|
t.Fatalf("Air's config has key %q, want the LAN sharing key", v)
|
|
}
|
|
if d := a.Check(); d != "" {
|
|
t.Errorf("Air's check: %s", d)
|
|
}
|
|
|
|
t.Setenv("OPENCODE_CONFIG_DIR", filepath.Join(home, "oc"))
|
|
dir := filepath.Join(home, ".config", "openchamber")
|
|
writeFile(t, filepath.Join(dir, "preferences.json"), `{"version":1,"fields":{"defaultModel":{"value":"anthropic/claude-sonnet-5","updatedAt":7}}}`)
|
|
writeFile(t, filepath.Join(dir, "settings.json"), `{"defaultModel":"anthropic/claude-sonnet-5"}`)
|
|
ch := openChamber(home, filepath.Join(home, ".config"))
|
|
if err := ch.Apply("model", "magpie/fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ocCfg := filepath.Join(home, "oc", "opencode.json")
|
|
if v, _ := edit.GetJSON(ocCfg, "provider.magpie.options.apiKey"); v != lan {
|
|
t.Fatalf("OpenChamber's OpenCode config has key %q, want the LAN sharing key", v)
|
|
}
|
|
if d := ch.Check(); d != "" {
|
|
t.Errorf("OpenChamber's check: %s", d)
|
|
}
|
|
}
|
|
|
|
// Every agent given the gateway's own address, gateway.URL(), with the key
|
|
// that names it (gateway.TokenFor) or gateway.Token, is given the LAN
|
|
// sharing key instead while MAGPIE_ADDR pins that address beyond loopback
|
|
// and sharing is on: the gateway takes neither from there (#1647's class).
|
|
func TestPinnedGatewayKeyForEveryWriter(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
t.Setenv("MAGPIE_ADDR", "172.23.80.1:3425")
|
|
gw := gateway.URL()
|
|
if gw != "http://172.23.80.1:3425" {
|
|
t.Fatalf("gateway URL %q", gw)
|
|
}
|
|
keys := func() map[string]string {
|
|
var cindy struct {
|
|
Auth struct {
|
|
APIKey string `json:"apiKey"`
|
|
} `json:"auth"`
|
|
}
|
|
data := CindyLink(gw)[strings.Index(CindyLink(gw), "data=")+len("data="):]
|
|
b, err := base64.RawURLEncoding.DecodeString(data)
|
|
if err != nil || json.Unmarshal(b, &cindy) != nil {
|
|
t.Fatalf("Cindy's link: %v %s", err, b)
|
|
}
|
|
jb := filepath.Join(home, "jb", "copilot.json")
|
|
if err := copilotJBWrite(jb); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
jbKey, _ := edit.GetJSON(jb, copilotJBKey)
|
|
_, zc := zcodeAddress(gw, gateway.Token)
|
|
agy := ""
|
|
if m := regexp.MustCompile(`GEMINI_API_KEY="?([^" ;]+)`).FindStringSubmatch(AgyLaunch("m")); m != nil {
|
|
agy = m[1]
|
|
}
|
|
return map[string]string{
|
|
"agy": agy,
|
|
"aionui": aionuiKey(AionUiLink(gw)),
|
|
"alma": almaKey(),
|
|
"cindy": cindy.Auth.APIKey,
|
|
"copilotjb": jbKey,
|
|
"cursor": cursorLocalEnv(gw)[cursorLocalVars[1]],
|
|
"hanako": hanakoProvider().APIKey,
|
|
"liveagent": liveAgentEntryFor(gw).APIKey,
|
|
"newmax": newMaxEntryFor(gw).APIKey,
|
|
"openclaw": openClawProviderAt(gw)["apiKey"],
|
|
"openpencil": openPencilEntry(gw).APIKey,
|
|
"pencil": pencilProviderJSON(gw)["apiKey"].(string),
|
|
"vscode": vscodeStable.token(),
|
|
"vscodium": vscodiumKind.token(),
|
|
"workbuddy": buddyModel("codebuddy", "m", "m", 0, 0, false, nil)["apiKey"].(string),
|
|
"zcode": zc,
|
|
}
|
|
}
|
|
plain := keys()
|
|
if err := access.ConfigureLAN(true, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { access.ConfigureLAN(false, false) })
|
|
lan := access.LANSecret()
|
|
if !strings.HasPrefix(lan, access.Prefix) {
|
|
t.Fatalf("LAN key %q", lan)
|
|
}
|
|
for agent, k := range keys() {
|
|
if k != lan {
|
|
t.Errorf("%s at %s: key %q, want the LAN sharing key", agent, gw, k)
|
|
}
|
|
if plain[agent] == lan || !strings.HasPrefix(plain[agent], gateway.Token) {
|
|
t.Errorf("%s with nothing shared: key %q, want magpie's own", agent, plain[agent])
|
|
}
|
|
}
|
|
}
|
|
|
|
// aionuiKey is the api_key AionUi's link hands it.
|
|
func aionuiKey(link string) string {
|
|
v, _ := url.ParseQuery(strings.TrimPrefix(link, AionUiScheme))
|
|
return v.Get("api_key")
|
|
}
|