Files
magpie/internal/agent/t3code_test.go
T
yetone ff36a2994f agents: T3 Code gets magpie as a provider of its own, every magpie model in it (KevinXC on Discord: T3 Code's Settings → Providers lists Codex, Claude, Cursor… each with "Add custom model"; magpie's models should be there without adding them one by one) — T3 Code (pingdotgg/t3code) keeps its settings in ~/.t3/userdata/settings.json ($T3CODE_HOME/userdata), sparse JSON its server watches and reloads on an outside edit, and besides its built-in providers takes provider instances of one's own: {"providerInstances":{"<id>":{"driver","displayName","enabled","environment":[{name,value,sensitive}],"config":{"customModels":[{slug,name}]}}}}. A claudeAgent instance runs Claude Code (the Agent SDK, ~/.claude settings read as Claude Code reads them) with its environment added, and sends a custom model's slug as Claude Code's model. The new t3code agent (t3code.go) adds the instance "magpie": driver claudeAgent, displayName magpie, ANTHROPIC_BASE_URL the gateway and ANTHROPIC_AUTH_TOKEN the token Claude Code is routed with (so its requests are Claude Code's to the gateway, haiku tier and all), not sensitive so T3 keeps them in the file rather than its secret store, and every magpie model shown to T3 Code as a custom model, a 1M one marked [1m] as for Claude Code (claude1MFor). The binary path and Claude home the user gave T3's own Claude (its instance, else the legacy providers.claudeAgent) are carried over. It is magpie's alone: the user's instances, their custom models and T3's other keys stay, off takes only it out, and the catalog sync rewrites it only where it is. T3's Codex lists Codex's model/list, which is magpie's catalog once Codex is routed through magpie, so it needs nothing; T3's own Claude asks for Claude's models by their full ids, which the gateway already sends to Claude Code's tiers (claudeTierStandIn). Found by ~/.t3 or, on a Mac, T3 Code (Alpha).app / T3 Code.app; T3CODE_HOME is in agentenv.Vars. Its icon is T3's mark (assets/prod/logo.svg, MIT), its letters alone so it draws as a mask; the option's note has its Chinese. Checked against T3 Code's own schema (effect 4.0.0-rc.115, packages/contracts at 54084ae1): the file magpie writes decodes as ServerSettings, magpie's config as ClaudeSettings, and encodes back to the same instance. Not run against a live T3 Code. TestT3CodeInstance (the user's instance, legacy Claude binary path and T3's key kept through on, sync and off; magpie's instance decoded strictly; found only with ~/.t3; among All()), TestT3CodeHomeAndInstance ($T3CODE_HOME, a file made, the Claude home carried from the user's claudeAgent instance) and TestStateListsT3Code are new. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 00:15:53 +08:00

233 lines
7.2 KiB
Go

package agent
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"regexp"
"slices"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// t3User is ~/.t3/userdata/settings.json as T3 Code writes it sparse: its
// Claude given a binary path (the legacy key), a Codex account of the
// user's own as an instance with a custom model, and a key of T3's.
const t3User = `{
"providers": {
"claudeAgent": {"binaryPath": "/opt/claude/bin/claude"}
},
"providerInstances": {
"codex_work": {
"driver": "codex",
"displayName": "Codex (work)",
"config": {"homePath": "~/.codex-work", "customModels": [{"slug": "gpt-6-astra", "name": "gpt-6-astra"}]}
}
},
"textGenerationModelSelection": {"instanceId": "codex", "model": "gpt-6-luna"}
}
`
// t3Envelope is T3 Code's ProviderInstanceConfig, with the claudeAgent
// config magpie writes; nothing else taken.
type t3Envelope struct {
Driver string `json:"driver"`
DisplayName string `json:"displayName"`
Enabled *bool `json:"enabled"`
Environment []struct {
Name string `json:"name"`
Value string `json:"value"`
Sensitive *bool `json:"sensitive"`
} `json:"environment"`
Config struct {
BinaryPath string `json:"binaryPath"`
HomePath string `json:"homePath"`
CustomModels []struct {
Slug string `json:"slug"`
Name string `json:"name"`
} `json:"customModels"`
} `json:"config"`
}
// t3Format decodes magpie's instance strictly as T3 Code's schema has it:
// an instance id of its slug pattern, a claudeAgent envelope, environment
// variable names of its pattern, custom models with a slug.
func t3Format(t *testing.T, raw []byte) t3Envelope {
t.Helper()
var e t3Envelope
d := json.NewDecoder(bytes.NewReader(raw))
d.DisallowUnknownFields()
if err := d.Decode(&e); err != nil {
t.Fatalf("not T3 Code's instance: %v\n%s", err, raw)
}
if !regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]*$`).MatchString(magpieID) {
t.Errorf("instance id %q", magpieID)
}
if e.Driver != "claudeAgent" || e.DisplayName == "" || e.Enabled == nil || !*e.Enabled {
t.Errorf("envelope: %s", raw)
}
for _, v := range e.Environment {
if !regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`).MatchString(v.Name) || v.Sensitive == nil || *v.Sensitive {
t.Errorf("environment: %s", raw)
}
}
for _, m := range e.Config.CustomModels {
if m.Slug == "" || m.Name == "" {
t.Errorf("custom model: %s", raw)
}
}
return e
}
// T3 Code (KevinXC on Discord) gets magpie as a provider of its own: a
// claudeAgent instance on the gateway with every magpie model as a custom
// model, the user's instances, their Claude's binary path and T3's keys
// kept; off takes only magpie's out, and the catalog sync leaves it off.
func TestT3CodeInstance(t *testing.T) {
home := syncHome(t)
t.Setenv("T3CODE_HOME", "")
for _, p := range []provider.Provider{
{ID: "resp", Name: "Resp", Key: "k", Responses: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.5"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
base := filepath.Join(home, ".t3")
path := filepath.Join(base, "userdata", "settings.json")
a := t3code(home)
if a.Path != path {
t.Fatalf("path %s", a.Path)
}
if a.Detected() && !t3App(home) {
t.Fatal("found with no ~/.t3")
}
if !slices.ContainsFunc(All(), func(x *Agent) bool { return x.ID == "t3code" && x.Name == "T3 Code" }) {
t.Fatal("T3 Code is not among the agents")
}
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(t3User), 0o600)
if !a.Detected() {
t.Fatal("not found with ~/.t3")
}
f := a.Field("provider")
if f == nil || f.Get() != "" {
t.Fatalf("provider field: %+v", f)
}
if err := f.Set(magpieID); err != nil {
t.Fatal(err)
}
if f.Get() != magpieID {
t.Fatalf("get: %q", f.Get())
}
var file, user map[string]json.RawMessage
if err := json.Unmarshal([]byte(readFile(path)), &file); err != nil {
t.Fatalf("%v\n%s", err, readFile(path))
}
json.Unmarshal([]byte(t3User), &user)
for _, k := range []string{"providers", "textGenerationModelSelection"} {
if !sameJSON(string(file[k]), user[k]) {
t.Errorf("T3's %s changed: %s", k, file[k])
}
}
var insts, userInsts map[string]json.RawMessage
json.Unmarshal(file["providerInstances"], &insts)
json.Unmarshal(user["providerInstances"], &userInsts)
if !sameJSON(string(insts["codex_work"]), userInsts["codex_work"]) {
t.Errorf("the user's instance changed: %s", insts["codex_work"])
}
e := t3Format(t, insts[magpieID])
env := map[string]string{}
for _, v := range e.Environment {
env[v.Name] = v.Value
}
if env["ANTHROPIC_BASE_URL"] != gateway.URL() || env["ANTHROPIC_AUTH_TOKEN"] != gateway.Token {
t.Errorf("environment: %v", env)
}
if e.Config.BinaryPath != "/opt/claude/bin/claude" || e.Config.HomePath != "" {
t.Errorf("T3's Claude's binary path not carried over: %+v", e.Config)
}
slugs := []string{}
for _, m := range e.Config.CustomModels {
slugs = append(slugs, m.Slug)
}
for _, id := range []string{"relay/glm-4.6", "resp/gpt-5.5"} {
if !slices.Contains(slugs, id) {
t.Errorf("%s not listed: %v", id, slugs)
}
}
// the catalog sync keeps it, and the user's
if err := a.Sync(); err != nil {
t.Fatal(err)
}
json.Unmarshal([]byte(readFile(path)), &file)
insts = nil
json.Unmarshal(file["providerInstances"], &insts)
t3Format(t, insts[magpieID])
if _, ok := insts["codex_work"]; !ok {
t.Fatal("the user's instance is gone after a sync")
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if f.Get() != "" {
t.Fatalf("get after off: %q", f.Get())
}
file, insts = nil, nil
json.Unmarshal([]byte(readFile(path)), &file)
json.Unmarshal(file["providerInstances"], &insts)
if _, ok := insts[magpieID]; ok || !sameJSON(string(insts["codex_work"]), userInsts["codex_work"]) ||
!sameJSON(string(file["providers"]), user["providers"]) {
t.Errorf("after off: %s", readFile(path))
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if f.Get() != "" {
t.Fatalf("sync put it back: %s", readFile(path))
}
}
// T3 Code's Claude already an instance (T3 moves it there once edited in
// its settings) has its Claude home carried over from there; $T3CODE_HOME
// moves the file, and a settings.json not there yet is made.
func TestT3CodeHomeAndInstance(t *testing.T) {
home := syncHome(t)
base := filepath.Join(home, "elsewhere")
t.Setenv("T3CODE_HOME", base)
a := t3code(home)
path := filepath.Join(base, "userdata", "settings.json")
if a.Path != path {
t.Fatalf("path %s, want %s", a.Path, path)
}
f := a.Field("provider")
if err := f.Set(magpieID); err != nil {
t.Fatal(err)
}
var file map[string]map[string]json.RawMessage
if err := json.Unmarshal([]byte(readFile(path)), &file); err != nil {
t.Fatalf("%v\n%s", err, readFile(path))
}
t3Format(t, file["providerInstances"][magpieID])
os.WriteFile(path, []byte(`{"providerInstances":{"claudeAgent":{"driver":"claudeAgent","config":{"homePath":"~/.claude-t3"}}}}`), 0o600)
if err := f.Set(magpieID); err != nil {
t.Fatal(err)
}
json.Unmarshal([]byte(readFile(path)), &file)
if e := t3Format(t, file["providerInstances"][magpieID]); e.Config.HomePath != "~/.claude-t3" || e.Config.BinaryPath != "" {
t.Errorf("config: %+v", e.Config)
}
if _, ok := file["providerInstances"]["claudeAgent"]; !ok {
t.Error("the user's Claude instance is gone")
}
}