xiaoxiaofei on Discord: "magpie 默认导入的是 message 格式的,要可以自定义请求格式".
magpie wrote one LiveAgent provider, "magpie", type claude_code with
api anthropic-messages, for every model. K-brain asks all of a
provider's models on the provider's one API (routing/client.go builds
the client per provider; PiModel.api is only validated), and LiveAgent's
settings lock an Anthropic provider's API: only an OpenAI provider
(type codex) has a request format to pick (ProviderModalView.tsx,
resolveProviderApi; K-brain settings_provider.go accepts
openai-completions and openai-responses). So the format couldn't be
changed, and a GPT served on Responses was translated from Messages on
every turn.
Now, as dsh's routes do (dshKinds), each model goes on the provider of
the API it is served on natively (catalog.Model.APIs):
- magpie: type codex, Chat Completions, baseUrl the gateway's /v1, for
every model with no Responses or Messages API of its own (groups, Chat
providers' models);
- magpie-responses: type codex, Responses, baseUrl /v1;
- magpie-anthropic: type claude_code, Messages, baseUrl the root (K-brain
appends /v1; for OpenAI it appends only /chat/completions or
/responses, hence /v1 there).
A codex provider is written with api and requestFormat, as LiveAgent's
settings write one; when the user switches it in LiveAgent, a sync keeps
their format (one LiveAgent doesn't offer goes back to magpie's). A
provider left with no model is taken out. A config.json from before (all
on one claude_code "magpie") is moved on the next sync: each model brings
its record (the user's display name) and whether it was turned off, and
a default/compact/task pick of it follows it to its new provider.
Disconnect takes all three out. Docs: reference.md, agent-wiring.md.
Run (mote, through mote-run):
- go test -tags nogui ./internal/agent -run '^TestLiveAgent|Key': all pass,
new TestLiveAgentRequestFormat and TestLiveAgentFormatMove included.
- go test -tags nogui -timeout 45m ./internal/agent: ok (320s).
- GOOS=linux and GOOS=windows go vet ./internal/agent: clean; gofmt -l clean.
- K-brain 507f560 (the version LiveAgent af89ea7 pins), with Go 1.27.0
on mote: config.LoadFile on the config.json magpie writes in
TestLiveAgentRequestFormat loads it, and routing.ClientForProvider
builds *ai.OpenAI at http://127.0.0.1:3425/v1 for magpie,
*ai.Responses at /v1 for magpie-responses and *ai.Anthropic at the
root for magpie-anthropic (a throwaway test, not committed).
Broken by hand (a copy), each turned a test red:
- the user's format not kept -> TestLiveAgentRequestFormat;
- no carried record from another provider of magpie's -> FormatMove;
- picks not following their model -> FormatMove;
- Responses models left on magpie -> both;
- an emptied provider not taken out -> FormatMove;
- turned-off not carried to the new provider -> FormatMove.
Full suite at c24915f04 (the change before this one), go test -timeout
45m ./... on mote: 86 packages ok, internal/gui red on
TestMainWindowCorners/macOS_26_SDK (#3949) and
TestPanelHiddenByWailsMarksPageOutOfSight (#5322), both filed, both
macOS 27 on mote.
Not tried: the LiveAgent app itself (its UI showing the three providers
and switching a format, then a real turn through the gateway); LiveAgent
on Windows or Linux; a real config.json of a user's.
726 lines
25 KiB
Go
726 lines
25 KiB
Go
package agent
|
|
|
|
// LiveAgent (Stack-Cairn/LiveAgent, a Tauri app; xiaoxiaofei. on Discord)
|
|
// is a desktop agent whose v2 runs its model calls through K-brain, a Go
|
|
// backend it starts as `k-brain backend -data-dir <root>`. K-brain keeps
|
|
// every provider in one config.json in that root (LiveAgent's
|
|
// kbrain_backend.rs ensure_started, data_home.rs; K-brain v0.107.12's
|
|
// internal/config): $LIVEAGENT_HOME, else $K_BRAIN_HOME, else the folder
|
|
// ~/.liveagent/data-dir.json points to ({"path": <absolute folder>}, set
|
|
// when the user moves LiveAgent's data in its settings), else ~/.liveagent,
|
|
// on every system. The file is JSONC, Pi's provider shape:
|
|
//
|
|
// // k-brain configuration — JSONC: comments and trailing commas are allowed.
|
|
// {"defaultModel":"…", "defaultProvider":"…", …,
|
|
// "providers":{…,
|
|
// "magpie":{"name":"magpie","type":"codex",
|
|
// "baseUrl":"http://127.0.0.1:3425/v1","api":"openai-completions",
|
|
// "apiKey":"magpie-liveagent","activeModels":null,
|
|
// "modelOrder":["<model>",…],"requestFormat":"openai-completions",
|
|
// "models":[{"id":"<model>","displayName":"…","input":["text","image"],
|
|
// "contextWindow":200000,"maxTokens":64000},…]},
|
|
// "magpie-responses":{…"type":"codex","api":"openai-responses"…},
|
|
// "magpie-anthropic":{…"type":"claude_code",
|
|
// "baseUrl":"http://127.0.0.1:3425","api":"anthropic-messages"…}}}
|
|
//
|
|
// magpie's models go on three providers by the API each is served on
|
|
// natively (liveAgentKinds). type codex with a requestFormat is what
|
|
// LiveAgent's settings write for an OpenAI-compatible provider, whose
|
|
// request format they let the user switch; claude_code with
|
|
// anthropic-messages an Anthropic-compatible one (providerSettings.ts).
|
|
// K-brain appends /v1 to an Anthropic baseUrl that doesn't end in a
|
|
// version and posts to /messages with X-Api-Key, so that one is the
|
|
// gateway's root; an OpenAI one gets only /chat/completions or /responses
|
|
// appended, so it is the gateway's /v1. activeModels null is every model
|
|
// active (Provider.ModelActive). K-brain decodes the file with unknown
|
|
// fields refused, so magpie writes only fields it knows. A file K-brain
|
|
// wouldn't read as providers (a top-level models, or one provider written
|
|
// bare at the top) is left alone.
|
|
//
|
|
// The backend makes the file as {} when there is none, reads it once, when
|
|
// it starts (storage.OpenLocal), and writes the whole of it back from what
|
|
// it holds whenever a setting changes (Config.SaveFile), so a
|
|
// change magpie makes while it runs is lost: magpie refuses one then,
|
|
// where it can tell (Running can't on Windows, where the notice says so).
|
|
// The model is picked in LiveAgent's model picker, so the field is whether
|
|
// magpie's models are in it (ListsModels). The user's name for each of
|
|
// magpie's providers, the request format they picked, the models they
|
|
// turned off, their order of them and the fields K-brain adds (metadata)
|
|
// stay through a sync, and a sync with nothing to change doesn't write.
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/jsonc"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// liveAgentApp is LiveAgent's macOS bundle (tauri.conf.json's identifier).
|
|
var liveAgentApp = desktopApp{id: "com.xiaofei.liveagent", names: []string{"LiveAgent.app"}}
|
|
|
|
// liveAgentProc is LiveAgent's processes: the app (CFBundleExecutable
|
|
// liveagent, the Linux and Windows binary's name too) and the k-brain
|
|
// backend it starts, which holds the settings. The program itself, the
|
|
// first word of its command line, or an executable in the .app wherever
|
|
// the bundle is.
|
|
var liveAgentProc = []string{`^([^ ]*/)?(liveagent|k-brain)( |$)`, `LiveAgent\.app/Contents/MacOS/(liveagent|k-brain)( |$)`}
|
|
|
|
var noticeLiveAgent = newNotice("LiveAgent keeps its settings in memory while it runs and writes them all back when one changes — quit LiveAgent before changing this, then open it and pick a magpie model in its model picker.")
|
|
|
|
// errLiveAgentRunning is a change refused while LiveAgent runs.
|
|
var errLiveAgentRunning = errors.New("LiveAgent writes its settings back over what magpie writes now: quit LiveAgent, then try again")
|
|
|
|
// liveAgentDemo is the provider K-brain's CLI writes in a config it makes
|
|
// (config.Default), with no endpoint, and its models.
|
|
const liveAgentDemo = "demo"
|
|
|
|
const liveAgentDemoModel = "model1"
|
|
|
|
func liveAgent(home string) *Agent {
|
|
dir := liveAgentDir(home)
|
|
path := filepath.Join(dir, "config.json")
|
|
write := func(on bool) error {
|
|
if !on {
|
|
return liveAgentUnwire(path)
|
|
}
|
|
return liveAgentWire(path, liveAgentFor(gateway.URL()))
|
|
}
|
|
return atomic(&Agent{
|
|
ID: "liveagent", Name: "LiveAgent", Icon: "liveagent-color", Aliases: []string{"live-agent", "k-brain", "kbrain"},
|
|
Dir: dir, Path: path,
|
|
detect: func() bool {
|
|
return isDir(dir) || isDir(filepath.Join(home, ".liveagent")) || macBundle(liveAgentApp) || liveAgentWindowsApp()
|
|
},
|
|
Notice: func() string {
|
|
if liveAgentWired(path) && Running(liveAgentProc...) {
|
|
return noticeLiveAgent.String()
|
|
}
|
|
return ""
|
|
},
|
|
// a sync while it runs is written as well: its next save puts back
|
|
// what it holds, and the next sync magpie's models
|
|
Sync: func() error {
|
|
if !liveAgentWired(path) {
|
|
return nil
|
|
}
|
|
return write(true)
|
|
},
|
|
ListsModels: true,
|
|
Fields: []Field{{
|
|
Key: "provider", Label: "provider",
|
|
Get: func() string {
|
|
if liveAgentWired(path) {
|
|
return magpieID
|
|
}
|
|
return ""
|
|
},
|
|
Set: func(v string) error {
|
|
if liveAgentRunning() {
|
|
return errLiveAgentRunning
|
|
}
|
|
return write(v != "")
|
|
},
|
|
Options: func(map[string]string) []Option {
|
|
return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "every magpie model in LiveAgent's model picker"}}
|
|
},
|
|
}},
|
|
}, path)
|
|
}
|
|
|
|
// liveAgentRunning says LiveAgent is open where magpie can tell; on
|
|
// Windows, where Running can't, no, and the notice asks to quit it first.
|
|
var liveAgentRunning = func() bool {
|
|
return runtime.GOOS != "windows" && Running(liveAgentProc...)
|
|
}
|
|
|
|
// liveAgentDir is the folder of K-brain's config.json as LiveAgent hands it
|
|
// over: $LIVEAGENT_HOME, else $K_BRAIN_HOME (each only an absolute path,
|
|
// here), else the folder ~/.liveagent/data-dir.json points to, else
|
|
// ~/.liveagent.
|
|
func liveAgentDir(home string) string {
|
|
for _, v := range []string{"LIVEAGENT_HOME", "K_BRAIN_HOME"} {
|
|
if d := strings.TrimSpace(appdir.Getenv(v)); d != "" && filepath.IsAbs(d) {
|
|
return d
|
|
}
|
|
}
|
|
def := filepath.Join(home, ".liveagent")
|
|
if b, err := os.ReadFile(filepath.Join(def, "data-dir.json")); err == nil {
|
|
var p struct {
|
|
Path string `json:"path"`
|
|
}
|
|
if json.Unmarshal(b, &p) == nil && filepath.IsAbs(p.Path) && filepath.Clean(p.Path) != filepath.Clean(def) {
|
|
return p.Path
|
|
}
|
|
}
|
|
return def
|
|
}
|
|
|
|
// liveAgentWindowsApp says LiveAgent's installer has put it where it
|
|
// installs it: per user under %LOCALAPPDATA%\LiveAgent, or for everyone
|
|
// under %ProgramFiles%\LiveAgent.
|
|
func liveAgentWindowsApp() bool {
|
|
if runtime.GOOS != "windows" {
|
|
return false
|
|
}
|
|
for _, v := range []string{"LOCALAPPDATA", "ProgramW6432", "ProgramFiles"} {
|
|
if d := appdir.Getenv(v); d != "" && isFile(filepath.Join(d, "LiveAgent", "liveagent.exe")) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// liveAgentModel is one of magpie's models as K-brain writes one (PiModel).
|
|
type liveAgentModel struct {
|
|
ID string `json:"id"`
|
|
DisplayName string `json:"displayName,omitempty"`
|
|
Input []string `json:"input,omitempty"`
|
|
ContextWindow int `json:"contextWindow,omitempty"`
|
|
MaxTokens int `json:"maxTokens,omitempty"`
|
|
}
|
|
|
|
// liveAgentEntry is one of magpie's providers, in the order K-brain
|
|
// writes one.
|
|
type liveAgentEntry struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
BaseURL string `json:"baseUrl"`
|
|
API string `json:"api"`
|
|
APIKey string `json:"apiKey"`
|
|
ActiveModels []string `json:"activeModels"`
|
|
ModelOrder []string `json:"modelOrder,omitempty"`
|
|
RequestFormat string `json:"requestFormat,omitempty"`
|
|
Models []liveAgentModel `json:"models"`
|
|
}
|
|
|
|
// liveAgentKind is one of magpie's providers in config.json: its key, the
|
|
// API it speaks, LiveAgent's type for it and its name.
|
|
type liveAgentKind struct {
|
|
key, api, typ, name string
|
|
}
|
|
|
|
// liveAgentKinds are magpie's providers, one per API a model is best asked
|
|
// on, as dsh's routes are (dshKinds). K-brain asks a provider's every model
|
|
// on the provider's one API, so with Anthropic Messages for all, a GPT
|
|
// served on Responses was translated from Messages each turn, and the
|
|
// format couldn't be changed: LiveAgent's settings lock an Anthropic
|
|
// provider's API and offer a request format only for an OpenAI one
|
|
// (ProviderModalView.tsx; Discord, xiaoxiaofei: magpie 默认导入的是 message
|
|
// 格式的,要可以自定义请求格式). A model goes on the provider of the API its
|
|
// provider serves it on natively (catalog.Model.APIs), every other one — a
|
|
// group, a Chat provider's — on "magpie", on Chat Completions. The two
|
|
// OpenAI ones are type codex with a requestFormat, which LiveAgent's
|
|
// settings let the user switch between Chat Completions and Responses, and
|
|
// a sync keeps the one they picked.
|
|
var liveAgentKinds = []liveAgentKind{
|
|
{"magpie", "openai-completions", "codex", "magpie"},
|
|
{"magpie-responses", "openai-responses", "codex", "magpie · Responses"},
|
|
{"magpie-anthropic", "anthropic-messages", "claude_code", "magpie · Anthropic"},
|
|
}
|
|
|
|
// liveAgentOpenAI are the request formats LiveAgent offers an OpenAI
|
|
// provider (CODEX_REQUEST_FORMAT_LABELS; K-brain's settings_provider.go).
|
|
var liveAgentOpenAI = []string{"openai-completions", "openai-responses"}
|
|
|
|
// isLiveAgentKey says a provider key in config.json is one of magpie's.
|
|
func isLiveAgentKey(key string) bool {
|
|
return slices.ContainsFunc(liveAgentKinds, func(k liveAgentKind) bool { return k.key == key })
|
|
}
|
|
|
|
// liveAgentKindOf is the provider of magpie's a model goes on.
|
|
func liveAgentKindOf(m catalog.Model) liveAgentKind {
|
|
switch {
|
|
case slices.Contains(m.APIs, string(provider.Responses)):
|
|
return liveAgentKinds[1]
|
|
case slices.Contains(m.APIs, string(provider.Anthropic)):
|
|
return liveAgentKinds[2]
|
|
}
|
|
return liveAgentKinds[0]
|
|
}
|
|
|
|
// liveAgentBaseURL is the gateway as K-brain asks it on api: Messages at
|
|
// the root (K-brain appends /v1), OpenAI's under /v1 (it appends only
|
|
// /chat/completions or /responses).
|
|
func liveAgentBaseURL(api, gw string) string {
|
|
if api == "anthropic-messages" {
|
|
return gw
|
|
}
|
|
return gw + "/v1"
|
|
}
|
|
|
|
// liveAgentWant is what magpie writes for a gateway: the key that names
|
|
// LiveAgent, each provider of magpie's that has a model, every model in
|
|
// magpie's order, and the provider each one is on.
|
|
type liveAgentWant struct {
|
|
Key string
|
|
Entries map[string]liveAgentEntry
|
|
Order []string
|
|
On map[string]string
|
|
}
|
|
|
|
// liveAgentFor is magpie's providers for a gateway at gw, each with the
|
|
// models picked for it that go on it, each once, sorted by id as K-brain
|
|
// writes them, and in magpie's order as modelOrder. With no model at all
|
|
// it is "magpie" alone, empty.
|
|
func liveAgentFor(gw string) liveAgentWant {
|
|
w := liveAgentWant{Key: agentKeyAt("liveagent", gw), Entries: map[string]liveAgentEntry{}, On: map[string]string{}}
|
|
entry := func(k liveAgentKind) liveAgentEntry {
|
|
e, ok := w.Entries[k.key]
|
|
if !ok {
|
|
e = liveAgentEntry{Name: k.name, Type: k.typ, BaseURL: liveAgentBaseURL(k.api, gw), API: k.api,
|
|
APIKey: w.Key, Models: []liveAgentModel{}}
|
|
if k.typ == "codex" {
|
|
e.RequestFormat = k.api
|
|
}
|
|
}
|
|
return e
|
|
}
|
|
for _, m := range sharedModels("liveagent") {
|
|
if strings.TrimSpace(m.ID) == "" || w.On[m.ID] != "" {
|
|
continue
|
|
}
|
|
k := liveAgentKindOf(m)
|
|
e := entry(k)
|
|
lm := liveAgentModel{ID: m.ID, ContextWindow: max(m.Context, 0), MaxTokens: max(m.Output, 0)}
|
|
if m.Name != "" && m.Name != m.ID {
|
|
lm.DisplayName = m.Name
|
|
}
|
|
if m.Images {
|
|
lm.Input = []string{"text", "image"}
|
|
}
|
|
e.Models = append(e.Models, lm)
|
|
e.ModelOrder = append(e.ModelOrder, m.ID)
|
|
w.Entries[k.key] = e
|
|
w.On[m.ID] = k.key
|
|
w.Order = append(w.Order, m.ID)
|
|
}
|
|
if len(w.Entries) == 0 {
|
|
w.Entries[liveAgentKinds[0].key] = entry(liveAgentKinds[0])
|
|
}
|
|
for k, e := range w.Entries {
|
|
sort.SliceStable(e.Models, func(i, j int) bool { return e.Models[i].ID < e.Models[j].ID })
|
|
w.Entries[k] = e
|
|
}
|
|
return w
|
|
}
|
|
|
|
// liveAgentRead is config.json without its comments and trailing commas,
|
|
// the object it holds, and an error for one K-brain wouldn't read as its
|
|
// providers: not an object, providers not an object, a top-level models,
|
|
// or one provider written bare. A file that isn't there, or holds nothing,
|
|
// is nil.
|
|
func liveAgentRead(path string) ([]byte, map[string]json.RawMessage, error) {
|
|
b, err := edit.Read(path)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if len(bytes.TrimSpace(b)) == 0 {
|
|
return nil, nil, nil
|
|
}
|
|
b = jsonc.ToJSON(bytes.TrimPrefix(b, []byte("\xef\xbb\xbf")))
|
|
var root map[string]json.RawMessage
|
|
if !json.Valid(b) || json.Unmarshal(b, &root) != nil || root == nil {
|
|
return nil, nil, fmt.Errorf("%s isn't a JSON object K-brain reads, so magpie leaves it alone", path)
|
|
}
|
|
if _, ok := root["models"]; ok {
|
|
return nil, nil, fmt.Errorf("%s has a top-level models, which K-brain refuses, so magpie leaves it alone", path)
|
|
}
|
|
p, ok := root["providers"]
|
|
if !ok {
|
|
for _, k := range []string{"name", "api", "baseUrl", "apiKey"} {
|
|
if _, bare := root[k]; bare {
|
|
return nil, nil, fmt.Errorf("%s is one provider written bare, so magpie leaves it alone", path)
|
|
}
|
|
}
|
|
} else if r := gjson.ParseBytes(p); r.Type != gjson.Null && !r.IsObject() {
|
|
return nil, nil, fmt.Errorf("%s: providers is not an object, so magpie leaves it alone", path)
|
|
}
|
|
return b, root, nil
|
|
}
|
|
|
|
// liveAgentWired says config.json has a provider of magpie's.
|
|
func liveAgentWired(path string) bool {
|
|
b, _, err := liveAgentRead(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, k := range liveAgentKinds {
|
|
if gjson.GetBytes(b, "providers."+k.key).IsObject() {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// liveAgentPicks are config.json's picks of a model, each with the key of
|
|
// its provider.
|
|
var liveAgentPicks = [][2]string{{"defaultModel", "defaultProvider"}, {"compactModel", "compactProvider"}, {"taskModel", "taskProvider"}}
|
|
|
|
// liveAgentWire puts magpie's providers into config.json, the file made
|
|
// when there is none, and takes out one left with no model. One there
|
|
// already keeps its name, the fields K-brain adds, the request format the
|
|
// user picked, each model's other fields, the models the user turned off
|
|
// and their order — a model that moved from another provider of magpie's
|
|
// brings its own; a model new to magpie's list is turned on and goes last.
|
|
// A pick of magpie's model follows it to its provider. A default model
|
|
// still K-brain's own (model1 of its empty demo provider, which has no
|
|
// endpoint to call) becomes magpie's first.
|
|
func liveAgentWire(path string, want liveAgentWant) error {
|
|
b, _, err := liveAgentRead(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
olds := map[string]gjson.Result{}
|
|
for _, k := range liveAgentKinds {
|
|
if o := gjson.GetBytes(b, "providers."+k.key); o.IsObject() {
|
|
olds[k.key] = o
|
|
}
|
|
}
|
|
prior := liveAgentPriorOf(olds)
|
|
var set []edit.KV
|
|
var del []string
|
|
for _, k := range liveAgentKinds {
|
|
w, ok := want.Entries[k.key]
|
|
old, had := olds[k.key]
|
|
if !ok {
|
|
if had {
|
|
del = append(del, "providers."+k.key)
|
|
}
|
|
continue
|
|
}
|
|
var o []byte
|
|
if had {
|
|
o = []byte(old.Raw)
|
|
}
|
|
entry, err := liveAgentMerge(o, w, prior)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if had && jsonEqual([]byte(old.Raw), entry) {
|
|
continue
|
|
}
|
|
set = append(set, edit.KV{Path: "providers." + k.key, Value: json.RawMessage(entry)})
|
|
}
|
|
for _, pick := range liveAgentPicks {
|
|
p := gjson.GetBytes(b, pick[1]).String()
|
|
if on := want.On[gjson.GetBytes(b, pick[0]).String()]; isLiveAgentKey(p) && on != "" && on != p {
|
|
set = append(set, edit.KV{Path: pick[1], Value: on})
|
|
}
|
|
}
|
|
if len(want.Order) > 0 {
|
|
first, on := want.Order[0], want.On[want.Order[0]]
|
|
if liveAgentDefaultUnset(b, "defaultModel", "defaultProvider") {
|
|
set = append(set, edit.KV{Path: "defaultModel", Value: first}, edit.KV{Path: "defaultProvider", Value: on})
|
|
}
|
|
if b != nil && liveAgentDefaultUnset(b, "compactModel", "compactProvider") && gjson.GetBytes(b, "compactModel").Exists() {
|
|
set = append(set, edit.KV{Path: "compactModel", Value: first}, edit.KV{Path: "compactProvider", Value: on})
|
|
}
|
|
}
|
|
if len(set) == 0 && len(del) == 0 {
|
|
return nil
|
|
}
|
|
if len(del) == 0 {
|
|
return edit.SetJSON(path, set...)
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out, err := edit.PatchJSON(raw, set, del)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return edit.WriteAtomic(path, out)
|
|
}
|
|
|
|
// liveAgentPrior is what config.json's providers of magpie's hold of each
|
|
// model: its record, and whether it is turned off in its provider.
|
|
type liveAgentPrior struct {
|
|
rec map[string][]byte
|
|
off map[string]bool
|
|
}
|
|
|
|
func liveAgentPriorOf(olds map[string]gjson.Result) liveAgentPrior {
|
|
p := liveAgentPrior{rec: map[string][]byte{}, off: map[string]bool{}}
|
|
for _, k := range liveAgentKinds {
|
|
old, ok := olds[k.key]
|
|
if !ok {
|
|
continue
|
|
}
|
|
a := old.Get("activeModels")
|
|
for _, m := range old.Get("models").Array() {
|
|
id := m.Get("id").String()
|
|
if id == "" {
|
|
continue
|
|
}
|
|
if _, seen := p.rec[id]; !seen {
|
|
p.rec[id] = []byte(m.Raw)
|
|
}
|
|
if a.IsArray() && !slices.ContainsFunc(a.Array(), func(x gjson.Result) bool { return x.String() == id }) {
|
|
p.off[id] = true
|
|
}
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
// liveAgentMerge is magpie's entry want laid over the one config.json has
|
|
// under its key, old (nil for none), with prior the models of magpie's
|
|
// other providers.
|
|
func liveAgentMerge(old []byte, want liveAgentEntry, prior liveAgentPrior) ([]byte, error) {
|
|
fresh := old == nil
|
|
if fresh {
|
|
var err error
|
|
if old, err = json.Marshal(want); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
own := map[string][]byte{}
|
|
for _, m := range gjson.GetBytes(old, "models").Array() {
|
|
if id := m.Get("id").String(); id != "" && !fresh {
|
|
own[id] = []byte(m.Raw)
|
|
}
|
|
}
|
|
ids := map[string]bool{}
|
|
var models []json.RawMessage
|
|
for _, m := range want.Models {
|
|
ids[m.ID] = true
|
|
w, err := json.Marshal(m)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
o, had := own[m.ID]
|
|
if !had {
|
|
o, had = prior.rec[m.ID]
|
|
}
|
|
if !had {
|
|
models = append(models, w)
|
|
continue
|
|
}
|
|
var set []jsonKV
|
|
gjson.ParseBytes(w).ForEach(func(k, v gjson.Result) bool {
|
|
// a name the user gave it in LiveAgent stays
|
|
if k.String() == "displayName" && gjson.GetBytes(o, "displayName").Exists() {
|
|
return true
|
|
}
|
|
set = append(set, jsonKV{k.String(), []byte(v.Raw)})
|
|
return true
|
|
})
|
|
models = append(models, jsonSetKeys(o, set))
|
|
}
|
|
if models == nil {
|
|
models = []json.RawMessage{}
|
|
}
|
|
// an OpenAI provider keeps the request format the user picked
|
|
api := want.API
|
|
if want.Type == "codex" && !fresh && gjson.GetBytes(old, "type").String() == "codex" {
|
|
cur := gjson.GetBytes(old, "requestFormat").String()
|
|
if cur == "" {
|
|
cur = gjson.GetBytes(old, "api").String()
|
|
}
|
|
if slices.Contains(liveAgentOpenAI, cur) {
|
|
api = cur
|
|
}
|
|
}
|
|
set := []jsonKV{
|
|
{"type", rawJSON(want.Type)}, {"baseUrl", rawJSON(want.BaseURL)}, {"api", rawJSON(api)},
|
|
{"apiKey", rawJSON(want.APIKey)},
|
|
}
|
|
if want.Type == "codex" {
|
|
set = append(set, jsonKV{"requestFormat", rawJSON(api)})
|
|
}
|
|
set = append(set, jsonKV{"models", joinRaw(models)})
|
|
// the models the user turned off stay off, a new one is on
|
|
if a := gjson.GetBytes(old, "activeModels"); a.IsArray() || slices.ContainsFunc(want.Models, func(m liveAgentModel) bool { return prior.off[m.ID] }) {
|
|
var active []string
|
|
for _, id := range a.Array() {
|
|
if ids[id.String()] && !prior.off[id.String()] && !slices.Contains(active, id.String()) {
|
|
active = append(active, id.String())
|
|
}
|
|
}
|
|
for _, m := range want.Models {
|
|
if !prior.off[m.ID] && !slices.Contains(active, m.ID) {
|
|
active = append(active, m.ID)
|
|
}
|
|
}
|
|
if active == nil {
|
|
active = []string{}
|
|
}
|
|
set = append(set, jsonKV{"activeModels", rawJSON(active)})
|
|
} else {
|
|
set = append(set, jsonKV{"activeModels", []byte("null")})
|
|
}
|
|
// the user's order stays, a new model goes last
|
|
order := want.ModelOrder
|
|
if o := gjson.GetBytes(old, "modelOrder"); o.IsArray() && !fresh {
|
|
var kept []string
|
|
for _, id := range o.Array() {
|
|
if ids[id.String()] && !slices.Contains(kept, id.String()) {
|
|
kept = append(kept, id.String())
|
|
}
|
|
}
|
|
for _, id := range want.ModelOrder {
|
|
if !slices.Contains(kept, id) {
|
|
kept = append(kept, id)
|
|
}
|
|
}
|
|
order = kept
|
|
}
|
|
if len(order) > 0 {
|
|
set = append(set, jsonKV{"modelOrder", rawJSON(order)})
|
|
}
|
|
if !gjson.GetBytes(old, "name").Exists() {
|
|
set = append(set, jsonKV{"name", rawJSON(want.Name)})
|
|
}
|
|
return jsonSetKeys(old, set), nil
|
|
}
|
|
|
|
// liveAgentDefaultUnset says the pick at the keys model and prov is none,
|
|
// or still K-brain's own: model1 of its demo provider, which has no
|
|
// endpoint, with no other provider listing a model1.
|
|
func liveAgentDefaultUnset(b []byte, model, prov string) bool {
|
|
if b == nil {
|
|
return true
|
|
}
|
|
m, p := gjson.GetBytes(b, model).String(), gjson.GetBytes(b, prov).String()
|
|
if p != "" && p != liveAgentDemo {
|
|
return false
|
|
}
|
|
if m == "" {
|
|
return p == ""
|
|
}
|
|
if m != liveAgentDemoModel {
|
|
return false
|
|
}
|
|
ok := true
|
|
gjson.GetBytes(b, "providers").ForEach(func(k, v gjson.Result) bool {
|
|
if k.String() == liveAgentDemo {
|
|
if strings.TrimSpace(v.Get("baseUrl").String()) != "" {
|
|
ok = false
|
|
}
|
|
return true
|
|
}
|
|
for _, x := range v.Get("models").Array() {
|
|
if x.Get("id").String() == liveAgentDemoModel {
|
|
ok = false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
return ok
|
|
}
|
|
|
|
// liveAgentUnwire takes magpie's providers out of config.json and every
|
|
// pick of it back to K-brain's default; the file goes when only what
|
|
// magpie made is left. A file left with no provider gets K-brain's own
|
|
// demo one back, since K-brain reads one with none as written over and
|
|
// restores its config.json.bak, which can still hold magpie's.
|
|
func liveAgentUnwire(path string) error {
|
|
b, root, err := liveAgentRead(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var magpieOnly = map[string]bool{}
|
|
var del []string
|
|
for _, k := range liveAgentKinds {
|
|
key := "providers." + k.key
|
|
if !gjson.GetBytes(b, key).Exists() {
|
|
continue
|
|
}
|
|
del = append(del, key)
|
|
for _, m := range gjson.GetBytes(b, key+".models").Array() {
|
|
magpieOnly[m.Get("id").String()] = true
|
|
}
|
|
}
|
|
if del == nil {
|
|
return nil
|
|
}
|
|
left := 0
|
|
// K-brain's demo provider still there, with its model1
|
|
demo := gjson.GetBytes(b, "providers."+liveAgentDemo+".models.#(id=="+liveAgentDemoModel+")").Exists()
|
|
gjson.GetBytes(b, "providers").ForEach(func(k, v gjson.Result) bool {
|
|
if isLiveAgentKey(k.String()) {
|
|
return true
|
|
}
|
|
left++
|
|
for _, m := range v.Get("models").Array() {
|
|
delete(magpieOnly, m.Get("id").String())
|
|
}
|
|
return true
|
|
})
|
|
// the file magpie made: its provider and its picks
|
|
rest := 0
|
|
for k := range root {
|
|
switch k {
|
|
case "providers", "defaultModel", "defaultProvider":
|
|
default:
|
|
rest++
|
|
}
|
|
}
|
|
if left == 0 && rest == 0 {
|
|
return os.Remove(path)
|
|
}
|
|
var set []edit.KV
|
|
for _, pick := range liveAgentPicks {
|
|
m, p := gjson.GetBytes(b, pick[0]).String(), gjson.GetBytes(b, pick[1]).String()
|
|
if !isLiveAgentKey(p) && (p != "" || !magpieOnly[m]) {
|
|
continue
|
|
}
|
|
if p != "" {
|
|
del = append(del, pick[1])
|
|
}
|
|
// K-brain's own default and compact model are its demo's model1
|
|
switch {
|
|
case pick[0] != "taskModel" && (left == 0 || demo):
|
|
set = append(set, edit.KV{Path: pick[0], Value: liveAgentDemoModel})
|
|
case pick[0] == "defaultModel":
|
|
// K-brain picks the first model of its providers
|
|
set = append(set, edit.KV{Path: pick[0], Value: ""})
|
|
default:
|
|
del = append(del, pick[0])
|
|
}
|
|
}
|
|
if left == 0 {
|
|
set = append(set, edit.KV{Path: "providers." + liveAgentDemo, Value: json.RawMessage(liveAgentDemoProvider)})
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out, err := edit.PatchJSON(raw, set, del)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return edit.WriteAtomic(path, out)
|
|
}
|
|
|
|
// liveAgentDemoProvider is K-brain's demo provider as its Save writes
|
|
// config.Default's.
|
|
const liveAgentDemoProvider = `{"name":"demo","baseUrl":"","api":"openai-completions","apiKey":"","activeModels":null,"models":[{"id":"model1","contextWindow":128000,"maxTokens":8192},{"id":"model2","contextWindow":128000,"maxTokens":8192}]}`
|
|
|
|
func rawJSON(v any) []byte {
|
|
b, _ := json.Marshal(v)
|
|
return b
|
|
}
|
|
|
|
// jsonEqual says two JSON texts hold the same value.
|
|
func jsonEqual(a, b []byte) bool {
|
|
var y any
|
|
return json.Unmarshal(b, &y) == nil && sameJSON(string(a), y)
|
|
}
|