For an OpenCode 2 magpie wrote OpenCode 1's opencode.json: its provider
under `provider`, the small model as `small_model`, and `reasoning: true`
on models that reason. OpenCode 2 converts the first two in memory, but
it doesn't read `reasoning` and logged a WARN for each such model on
every load ("configuration normalization diagnostic ... omitted
unsupported legacy setting", reproduced with a real OpenCode 2.2.6).
Now the OpenCode that reads the file decides its words (openCodeTwo in
opencode_v2.go: the opencode on PATH, or a WSL distro's probed version,
now asked for opencode too). For 2.x magpie writes `providers.magpie` in
OpenCode 2's shape (openCodeNative), `agents.title.model`, and no
`reasoning`. A file written for OpenCode 1 moves over on the next sync:
openCodeFromV1 ports migrateStandardProvider (v2.2.6) so the user's keys
in magpie's provider (headers, timeout, setCacheKey, their own variants,
limit.output) go along; an empty `provider` is dropped; a small_model
that isn't magpie's stays. OpenCode 1, or an unknown version, gets the
same output as before. OpenChamber writes the same file and takes the
same decision, so the two syncs don't undo each other. mimocode, DevEco
Code and Air keep OpenCode 1's words.
Run:
- gofmt; go vet ./internal/agent for darwin, linux (-tags nogui) and
windows (mote): clean.
- go test -count=1 ./internal/agent on mote: ok, at the rebased head.
- win-run go test -run 'OpenCode|OpenChamber|WSL' ./internal/agent: ok.
- Real binaries on mote, sandbox HOMEs, `opencode debug config
--print-logs`: the file magpie now writes for OpenCode 2 loads in
OpenCode 2.2.6 with no WARN/ERROR, and resolves (model, agents,
providers) exactly as 2.2.6 resolves the OpenCode 1 file it replaces;
the OpenCode 1 file loads in 1.19.5 with no WARN, small_model and
reasoning intact. The old file in 2.2.6 still shows the reasoning WARN.
Broken by hand:
- openCodeTwo always false: TestOpenCode2WritesItsOwnWords,
TestOpenCode2MovesAnOpenCode1File, TestOpenChamberAndOpenCode2AgreeOnWords
and TestOpenCodeTwo red.
- moveProvider not carrying the v1 block over: the move test red on the
user's timeout.
- moveSmall off: the move test red on agents.title.model.
Not tried: a real OpenCode 1 desktop app reading a file written for an
OpenCode 2 CLI on PATH (it would see no magpie provider; 1.19.5 logs
`providers` as not representable); Air's bundled OpenCode version; a real
WSL distro with OpenCode 2.
185 lines
5.3 KiB
Go
185 lines
5.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// A reply limit typed by hand into an agent's file stays (sayagoodbyeplz
|
|
// on Discord: WorkBuddy's deepseek-v4.1-flash replies 128000 tokens, not
|
|
// the 384000 models.dev says, and Pi's maxTokens set to 128000 was written
|
|
// back to 384000 on the next sync). magpie writes its models whole, so a
|
|
// value in the file is told apart by what magpie has wanted there: the
|
|
// stash keeps, per file and model, the reply limits magpie handed it
|
|
// (outputsWantedKey), and one in the file that is none of them is the
|
|
// user's. It stays until the user sets the model's Max output in magpie
|
|
// (the editor's Model settings, or `magpie model output`), which is their
|
|
// word on it there and is written over it, as is magpie's when no record
|
|
// says what magpie wanted (a model new to the file, a stash that can't be
|
|
// read: unknown is magpie's, as before).
|
|
//
|
|
// The record is of every value wanted, not only the last written: an
|
|
// agent's block is also built to be compared (Drift) and never written,
|
|
// and an old value of magpie's left in the file is still magpie's.
|
|
|
|
// outputsWantedKey is where the stash keeps the reply limits magpie has
|
|
// wanted in the file at path, as JSON: {model: [n, …]}, newest last.
|
|
func outputsWantedKey(path string) string { return "output:" + path + ":wanted" }
|
|
|
|
// outputsRemembered is how many values a model's record keeps.
|
|
const outputsRemembered = 8
|
|
|
|
// outputsKept is want, the reply limit magpie hands each model of the file
|
|
// at path, with have's, the file's own now, kept where it is the user's.
|
|
// It records want's values as wanted.
|
|
func outputsKept(path string, want, have map[string]int) map[string]int {
|
|
key := outputsWantedKey(path)
|
|
rec := map[string][]int{}
|
|
if v := stashShared()[key]; v != "" && json.Unmarshal([]byte(v), &rec) != nil {
|
|
rec = map[string][]int{}
|
|
}
|
|
out := make(map[string]int, len(want))
|
|
next := make(map[string][]int, len(want))
|
|
var s *settings.Settings
|
|
for id, w := range want {
|
|
out[id] = w
|
|
was, known := rec[id]
|
|
if f := have[id]; known && f > 0 && f != w && !slices.Contains(was, f) {
|
|
if s == nil {
|
|
l := settings.Load()
|
|
s = &l
|
|
}
|
|
if !outputSetIn(*s, id) {
|
|
out[id] = f
|
|
}
|
|
}
|
|
if w <= 0 || slices.Contains(was, w) {
|
|
next[id] = was
|
|
continue
|
|
}
|
|
was = append(slices.Clone(was), w)
|
|
if len(was) > outputsRemembered {
|
|
was = was[len(was)-outputsRemembered:]
|
|
}
|
|
next[id] = was
|
|
}
|
|
for id, v := range next {
|
|
if len(v) == 0 {
|
|
delete(next, id)
|
|
}
|
|
}
|
|
if !maps.EqualFunc(rec, next, slices.Equal) {
|
|
b, _ := json.Marshal(next)
|
|
if len(next) == 0 {
|
|
b = nil
|
|
}
|
|
stash(map[string]string{key: string(b)})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// outputSetIn says the user set a Max output in magpie for the model an
|
|
// agent lists as id, "provider/model": its own or its provider's.
|
|
func outputSetIn(s settings.Settings, id string) bool {
|
|
prov, _, ok := strings.Cut(id, "/")
|
|
return ok && (s.ModelOutputs[id] > 0 || s.ModelOutputs[prov+"/*"] > 0)
|
|
}
|
|
|
|
// numberOf is a JSON number as an int, 0 for anything else.
|
|
func numberOf(v any) int {
|
|
switch n := v.(type) {
|
|
case int:
|
|
return n
|
|
case float64:
|
|
return int(n)
|
|
case json.Number:
|
|
i, _ := n.Int64()
|
|
return int(i)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// piOutputsKept keeps a maxTokens the user typed into magpie's models in
|
|
// Pi's models.json at path (outputsKept); v is the block magpie writes.
|
|
func piOutputsKept(path string, v any) {
|
|
block, _ := v.(map[string]any)
|
|
ms, _ := block["models"].([]map[string]any)
|
|
want := map[string]int{}
|
|
for _, e := range ms {
|
|
if id, _ := e["id"].(string); id != "" {
|
|
if n := numberOf(e["maxTokens"]); n > 0 {
|
|
want[id] = n
|
|
}
|
|
}
|
|
}
|
|
if len(want) == 0 {
|
|
return
|
|
}
|
|
have := map[string]int{}
|
|
if raw, ok := edit.GetJSON(path, "providers."+magpieID+".models"); ok {
|
|
var cur []struct {
|
|
ID string `json:"id"`
|
|
MaxTokens int `json:"maxTokens"`
|
|
}
|
|
if json.Unmarshal([]byte(raw), &cur) == nil {
|
|
for _, c := range cur {
|
|
have[c.ID] = c.MaxTokens
|
|
}
|
|
}
|
|
}
|
|
kept := outputsKept(path, want, have)
|
|
for _, e := range ms {
|
|
if id, _ := e["id"].(string); kept[id] > 0 {
|
|
e["maxTokens"] = kept[id]
|
|
}
|
|
}
|
|
}
|
|
|
|
// openCodeOutputsKept keeps a limit.output the user typed into magpie's
|
|
// models in an OpenCode config at path (outputsKept); ms are the models
|
|
// magpie writes.
|
|
func openCodeOutputsKept(path string, ms orderedJSON) {
|
|
want := map[string]int{}
|
|
for _, p := range ms {
|
|
e, _ := p.v.(map[string]any)
|
|
l, _ := e["limit"].(map[string]any)
|
|
if n := numberOf(l["output"]); n > 0 {
|
|
want[p.k] = n
|
|
}
|
|
}
|
|
if len(want) == 0 {
|
|
return
|
|
}
|
|
have := map[string]int{}
|
|
// under providers where the file keeps it there alone: OpenCode 2's,
|
|
// whose limit is OpenCode 1's
|
|
raw, ok := edit.GetJSON(path, "provider."+magpieID+".models")
|
|
if !ok {
|
|
raw, ok = edit.GetJSON(path, openCodeNativeKey+".models")
|
|
}
|
|
if ok {
|
|
var cur map[string]struct {
|
|
Limit struct {
|
|
Output int `json:"output"`
|
|
} `json:"limit"`
|
|
}
|
|
if json.Unmarshal([]byte(raw), &cur) == nil {
|
|
for id, c := range cur {
|
|
have[id] = c.Limit.Output
|
|
}
|
|
}
|
|
}
|
|
kept := outputsKept(path, want, have)
|
|
for _, p := range ms {
|
|
e, _ := p.v.(map[string]any)
|
|
if l, _ := e["limit"].(map[string]any); l != nil && kept[p.k] > 0 {
|
|
l["output"] = kept[p.k]
|
|
}
|
|
}
|
|
}
|