Murciee (#3883, OpenCode 2.1.4 as OpenChamber bundles it, Windows): a model added in magpie never reached OpenCode 2, though magpie said it was exposed. ~/.config/opencode/opencode.json held magpie's provider twice, provider.magpie with 43 models and providers.magpie with 38, and opencode models listed the 38. OpenCode 2 keeps a provider under providers, in words of its own, and still reads OpenCode 1's provider: a provider named under both is the one under providers, whole, and the other only a WARN in its log, "retained native value over legacy value" (packages/core/src/config/normalize.ts, mergeMaps, at v2.1.4). OpenChamber's provider form saves a provider under providers and takes it out of provider (packages/web/server/lib/opencode/config-v2.js, writeSectionEntry). Once it had saved magpie's, magpie's next sync put provider.magpie back, as a model of magpie's was picked, and from then on brought only that one up to date. Where an OpenCode config keeps magpie's provider under providers, each sync and model pick now write it there too, and the wiring check reads it there (opencode_providers.go). It is written as OpenCode 2 reads magpie's provider.magpie (packages/core/src/v1/config/migrate.ts, migrateStandardProvider and migrateModel): npm as package "aisdk:<npm>", options as settings, a model's modalities as capabilities beside tools, its variants a list of {id, settings, body} with the options OpenCode 2 reads itself in settings and the rest in body (provider-options.ts, COMPATIBLE_KEYS: a fast variant's service_tier), and reasoning, which it doesn't read, left out. As under provider (theirsKept, openCodeKept), magpie's models are written whole, a key of the user's in the block stays, and so does a variant the user put on a model of magpie's. A variant is magpie's when it says what magpie's of its name says, split as OpenCode 2 splits it or all in settings, as OpenChamber carries one over from provider (toModelVariants). A config without providers.magpie doesn't get one: provider.magpie is still what magpie writes, and OpenCode 1 and 2 both read it. Taking magpie out of OpenCode or OpenChamber takes out both blocks, as OpenChamber's own delete does. agent-wiring.md says so beside #2379's variants. Old data: a config already in the reporter's state is brought up to date by the next sync, which SyncCatalog runs over every agent at each start and update of magpie. A providers.magpie an older magpie left behind when taking magpie out (it took out provider.magpie alone) is kept up to date from now on, and goes the next time magpie is taken out. Not changed: - Air: its magpie-opencode.json is magpie's own and holds provider.magpie alone. With OpenChamber's providers.magpie in the global config and nothing else of magpie's there, OpenCode 2.1.4 handed Air's file as OPENCODE_CONFIG still listed the model only Air's file had. - MiMo Code and DevEco Code go through the same code. Their configs hold no providers block unless something of theirs writes one; not checked against their source. - A provider of the user's own at magpie's gateway kept under providers isn't read by openCodeSpelled and ownGatewayProvider, which read provider alone: #3915, filed with this. - What else OpenChamber's form puts on a model of magpie's under providers (a modelID, a cost) goes with the rest of the model at the next sync, as a key on one under provider does today. What was run (tree 61b3fff7, on origin/maine056779a, go1.26.8, linux/amd64, Linux 5.15), outside graftcp, umask 077: - TestOpenCodeNativeIsOpenCode2s, new: openCodeNative of a block with levels, fast variants, an image model and one without levels is what OpenCode 2.1.4's opencode debug config printed under providers.magpie for that block in an opencode.json (its apiKey, shown as ***, put back). - TestOpenCodeProvidersKeptCurrent and TestOpenChamberProvidersKeptCurrent, new: a config as OpenChamber's form leaves it, provider.magpie gone and providers.magpie from before think/new (a stale baseURL and key, a header and a timeout of the user's, a modelID, magpie's variants of levels deep no longer has, one of them in OpenChamber's form, one the user wrote over, one of their own, and a model the catalog dropped). A sync, and a model picked, bring it to the models under provider with the user's keys and two variants kept; a second sync writes nothing; stepping off magpie takes out both. A config without providers gets none. TestOpenCodeCheckReadsProviders, new: a providers.magpie at another address is off until a sync. - The 21 tests named TestOpenCode*, TestOpenChamber* and TestDevEco*: ok. -race -count=5 over the four new ones: ok. - Broken by hand, one at a time, each red where it says: no sync under providers in OpenCode's Sync, in a pick, in OpenChamber's Sync; no delete there for OpenCode or for OpenChamber; the check reading provider alone; no theirsKept; the user's variants dropped; no variant taken for magpie's; only OpenCode 2's split taken for magpie's (OpenChamber's high-fast stayed); providers written into a config without it; a write on every sync; every option in settings; no capabilities. The sync, pick, delete and check breaks at once, which is whate056779adoes: three of the four new tests red. - The reporter's case with the real binaries, in a sandbox HOME: magpie ate056779awired OpenCode to think/deep; OpenChamber's own toProviderEntity (openchamber main 6b48aecb, its zod import stubbed so the module loads) turned that provider.magpie into the providers.magpie its form saves, and provider.magpie was taken out, as its form does; think/new was added, and magpie ate056779asynced. OpenCode 2.1.4's opencode models (npm @opencode/cli-linux-x64, integrity checked) listed magpie/think/deep alone, and its log had two "retained native value over legacy value" lines. The same file synced by this commit: magpie/think/deep and magpie/think/new, no such line, and a second sync left the file as it was, mtime too. - go test -tags nogui -count=1 -timeout 45m ./... (log kept), Bun 1.4.2: 69 packages ok and 7 red, none in what this changes; in internal/agent every test this adds or touches passes. The reds: agent TestFolderVarsAreListed (#3884, main's CI red), TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141: alone at -count=40, TestZedAppPath is red 37 and 35 times ate056779a, 35 and 38 here), TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout (#3641); davsync TestSync (#3420: alone at -count=20, red 3 times ate056779aand 2 here); edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077, #3640); gateway TestPluginToldWhereTheAgentWorks (#3675); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649); provider TestCodexAppServersOnHome (#3906, main's CI red), then "close of synctest channel from outside bubble" from TestUnreadLoginsWriteErrorsKeepCredentials (#3632, main's CI red). - gofmt -l clean. go vet -tags nogui and go build -tags nogui for linux, windows and darwin, and go vet and go build for windows without the tag: exit 0 each. go vet and go build without the tag on linux: exit 1 (this box has no GTK or WebKitGTK dev packages). Not tried: Windows, the reporter's system, and OpenChamber's app itself: its converter alone was run. Ported onto main by yetone. Since this was written,cdd470ce9(#4575) made the models of magpie's provider under provider an ordered list (orderedJSON) instead of a map, so the block under providers came out with no models at all (TestOpenCodeProvidersKeptCurrent and TestOpenChamberProvidersKeptCurrent red on main with the patch as it was), and #4575 added a model's cost. openCodeNative now reads that list and writes the models under providers in the same order, and a cost in OpenCode 2's words: a list of tiers, cache prices under cache (migrateModel's costs in packages/core/src/v1/config/migrate.ts). OpenCode's source read at v2.1.4 (68bc26b7) and v2.2.5: in both, normalize.ts merges provider into providers (mergeMaps), and a name under both is the one under providers, taken whole, so the fix holds on the current 2.2.x too. TestOpenCodeNativeKeepsOrderAndCost is new, and providersCurrent checks the order. Run for the port (mote-run, macOS, go1.26.8): go test -tags nogui -count=1 -run 'OpenCode|OpenChamber|MiMo|DevEco|Spelled|Wiring' ./internal/agent: ok. Batch run, at tree 104d75f0a (the twelve coffemon ports together, on origin/main7d022bdb6), mote, macOS, go1.26.8: - go test -tags nogui -count=1 -timeout 45m on the packages the batch changes (agent, autostart, daemon, gateway, library, proc, provider, sessions, testenv, tui, gui): ok each. - GUI, node --test with Playwright 1.62.0, Chromium and WebKit: library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass. - gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same batch on origin/main00098f12bone rebase before. - The whole go test ./... at cdd7c8e46: 86 packages ok, one red, internal/cli TestEveryRouteHasACommandOrIsNamed (GET /api/context/reply has no command), red on main too (#5026). - Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch onf4c664a09): proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp) pass. - Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv, tui ok; library and agent red in the same tests as on origin/mainf4c664a09(#4115, #1827, #4786; #5068, #5069, #5070 filed now). - Rebased onto origin/mainfb1f4856a(three commits, no file in common with the batch) and run again at the pushed tree: gofmt -l nothing, go build ./... and go vet ./... ok; go test -tags nogui sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and library-* 1044 of 1044 pass. - Not run before the push: the whole GUI suite (a run at cdd7c8e46 was cut off at 1170 of ~1174 tests when mote rebooted; it runs again after the tag and any red is filed). Co-authored-by: yetone <9rqwv2ak@gmail.com>
330 lines
13 KiB
Go
330 lines
13 KiB
Go
package agent
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"maps"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// magpie's provider under providers says what OpenCode 2 reads magpie's
|
|
// provider under provider as: want is what OpenCode 2.1.4's opencode debug
|
|
// config printed under providers.magpie for this block in an opencode.json
|
|
// (its apiKey, shown there as ***, put back), levels, fast variants, an
|
|
// image model and one without levels.
|
|
func TestOpenCodeNativeIsOpenCode2s(t *testing.T) {
|
|
block := map[string]any{"npm": "@ai-sdk/openai-compatible", "name": "magpie",
|
|
"options": map[string]any{"baseURL": "http://127.0.0.1:3425/v1", "apiKey": "mk-test"},
|
|
"models": orderedJSON{
|
|
{"think/deep", map[string]any{"name": "Deep", "modalities": map[string]any{"input": []string{"text", "image"}, "output": []string{"text"}},
|
|
"limit": map[string]any{"context": 1000000, "output": 64000}, "reasoning": true, "variants": openCodeVariants([]string{"none", "high", "max"}, true)}},
|
|
{"think/plain", map[string]any{"name": "Plain", "variants": openCodeVariants(nil, false)}},
|
|
{"gpt/fast", map[string]any{"name": "Fast", "variants": openCodeVariants(nil, true)}},
|
|
}}
|
|
const want = `{"name":"magpie","package":"aisdk:@ai-sdk/openai-compatible","settings":{"apiKey":"mk-test","baseURL":"http://127.0.0.1:3425/v1"},"models":{` +
|
|
`"gpt/fast":{"name":"Fast","variants":[{"id":"fast","settings":{"extraBody":{"service_tier":"priority"}},"body":{"service_tier":"priority"}}]},` +
|
|
`"think/deep":{"name":"Deep","capabilities":{"tools":true,"input":["text","image"],"output":["text"]},"variants":[` +
|
|
`{"id":"none","settings":{"reasoningEffort":"none"}},{"id":"high","settings":{"reasoningEffort":"high"}},{"id":"max","settings":{"reasoningEffort":"max"}},` +
|
|
`{"id":"none-fast","settings":{"extraBody":{"service_tier":"priority"},"reasoningEffort":"none"},"body":{"service_tier":"priority"}},` +
|
|
`{"id":"high-fast","settings":{"extraBody":{"service_tier":"priority"},"reasoningEffort":"high"},"body":{"service_tier":"priority"}},` +
|
|
`{"id":"max-fast","settings":{"extraBody":{"service_tier":"priority"},"reasoningEffort":"max"},"body":{"service_tier":"priority"}}],` +
|
|
`"limit":{"context":1000000,"output":64000}},` +
|
|
`"think/plain":{"name":"Plain","variants":[]}}}`
|
|
if got := openCodeNative(block); !sameJSON(want, got) {
|
|
b, _ := json.Marshal(got)
|
|
t.Fatalf("got %s\nwant %s", b, want)
|
|
}
|
|
}
|
|
|
|
// thinkCatalog gives the sandbox a provider whose models are think/deep,
|
|
// with levels and images, and think/new, with neither.
|
|
func thinkCatalog(t *testing.T) {
|
|
t.Helper()
|
|
if err := provider.Save(provider.Provider{ID: "think", Name: "Think", Chat: "https://example.test/v1", Key: "key",
|
|
Models: []string{"deep", "new"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("think", "https://example.test/v1", []catalog.Model{
|
|
{ID: "deep", Efforts: []string{"none", "high", "max"}, Images: true},
|
|
{ID: "new"},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// openChamberSaved does to the OpenCode config at path what OpenChamber's
|
|
// form does to a provider it saves (config-v2.js, writeSectionEntry): the
|
|
// one under provider goes, and the block under providers is the form's,
|
|
// from before think/new: a stale baseURL and key, a header and a setting
|
|
// of the user's own, magpie's variants (some of levels deep no longer has,
|
|
// one in OpenChamber's split), one the user wrote over and one of their
|
|
// own, a modelID, as #3883's block has, and a model the catalog no longer
|
|
// has.
|
|
func openChamberSaved(t *testing.T, path string) {
|
|
t.Helper()
|
|
const stale = `{"name":"magpie","package":"aisdk:@ai-sdk/openai-compatible",
|
|
"settings":{"baseURL":"http://127.0.0.1:1/v1","apiKey":"old","timeout":600000},
|
|
"headers":{"X-Mine":"1"},
|
|
"models":{
|
|
"think/deep":{"modelID":"think/deep","name":"Deep (old)","variants":[
|
|
{"id":"none","settings":{"reasoningEffort":"none"}},
|
|
{"id":"medium","settings":{"reasoningEffort":"medium"}},
|
|
{"id":"high","settings":{"reasoningEffort":"high","temperature":0.2}},
|
|
{"id":"high-fast","settings":{"reasoningEffort":"high","service_tier":"priority","extraBody":{"service_tier":"priority"}}},
|
|
{"id":"mine","settings":{"reasoningEffort":"low"},"body":{"top_k":5}}]},
|
|
"think/old":{"name":"Old","variants":[]}}}`
|
|
if err := edit.DelJSON(path, "provider."+magpieID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := edit.SetJSON(path, edit.KV{Path: openCodeNativeKey, Value: json.RawMessage(stale)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// providersCurrent checks that magpie's provider under providers in the
|
|
// OpenCode config at path says what the one under provider says, with what
|
|
// openChamberSaved left of the user's kept.
|
|
func providersCurrent(t *testing.T, path string) {
|
|
t.Helper()
|
|
raw, ok := edit.GetJSON(path, openCodeNativeKey)
|
|
if !ok {
|
|
t.Fatalf("no %s", openCodeNativeKey)
|
|
}
|
|
var got struct {
|
|
Package string `json:"package"`
|
|
Settings map[string]any `json:"settings"`
|
|
Headers map[string]string `json:"headers"`
|
|
Models map[string]struct {
|
|
Name string `json:"name"`
|
|
Capabilities map[string]any `json:"capabilities"`
|
|
Variants []json.RawMessage `json:"variants"`
|
|
} `json:"models"`
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var v1 struct {
|
|
Models map[string]json.RawMessage `json:"models"`
|
|
}
|
|
if raw1, _ := edit.GetJSON(path, "provider."+magpieID); json.Unmarshal([]byte(raw1), &v1) != nil {
|
|
t.Fatalf("provider: %s", raw1)
|
|
}
|
|
// think/old, gone from the catalog, goes; think/new is there
|
|
ids, want := slices.Sorted(maps.Keys(got.Models)), slices.Sorted(maps.Keys(v1.Models))
|
|
if !slices.Contains(ids, "think/new") || !reflect.DeepEqual(ids, want) {
|
|
t.Fatalf("models %v, provider's %v: %s", ids, want, raw)
|
|
}
|
|
// in the order magpie lists them under provider (#4575)
|
|
if order, v1order := jsonKeyOrder(t, raw, "models"), jsonKeyOrder(t, raw1Of(path), "models"); !slices.Equal(order, v1order) {
|
|
t.Fatalf("models in the order %v, provider's %v", order, v1order)
|
|
}
|
|
base, _ := edit.GetJSON(path, "provider."+magpieID+".options.baseURL")
|
|
key, _ := edit.GetJSON(path, "provider."+magpieID+".options.apiKey")
|
|
if base == "" || got.Settings["baseURL"] != base || got.Settings["apiKey"] != key || got.Package != "aisdk:@ai-sdk/openai-compatible" {
|
|
t.Fatalf("settings %v, package %q; provider's %s %s", got.Settings, got.Package, base, key)
|
|
}
|
|
if got.Settings["timeout"] != float64(600000) || got.Headers["X-Mine"] != "1" {
|
|
t.Fatalf("the user's setting or header is gone: %s", raw)
|
|
}
|
|
deep := got.Models["think/deep"]
|
|
if name, _ := edit.GetJSON(path, "provider."+magpieID+".models.think/deep.name"); deep.Name != name {
|
|
t.Fatalf("deep's name %q, provider's %q", deep.Name, name)
|
|
}
|
|
if in, _ := deep.Capabilities["input"].([]any); !slices.Contains(in, any("image")) {
|
|
t.Fatalf("deep's capabilities %v", deep.Capabilities)
|
|
}
|
|
var vids []string
|
|
for _, v := range deep.Variants {
|
|
id, _ := openCodeOwnVariant(v)
|
|
vids = append(vids, id)
|
|
}
|
|
if !reflect.DeepEqual(vids, []string{"none", "high", "max", "mine"}) {
|
|
t.Fatalf("deep's variants %v: %s", vids, raw)
|
|
}
|
|
if !sameJSON(string(deep.Variants[1]), map[string]any{"id": "high", "settings": map[string]any{"reasoningEffort": "high", "temperature": 0.2}}) {
|
|
t.Fatalf("the user's high is gone: %s", deep.Variants[1])
|
|
}
|
|
if !sameJSON(string(deep.Variants[3]), map[string]any{"id": "mine", "settings": map[string]any{"reasoningEffort": "low"}, "body": map[string]any{"top_k": 5}}) {
|
|
t.Fatalf("the user's own variant is gone: %s", deep.Variants[3])
|
|
}
|
|
}
|
|
|
|
// #3883: once OpenChamber's form had saved magpie's provider under
|
|
// providers, which OpenCode 2 reads instead of provider, magpie's sync put
|
|
// its provider back under provider and OpenCode 2 never saw think/new.
|
|
// The sync brings the block under providers up to magpie's models too;
|
|
// a config without one doesn't get one.
|
|
func TestOpenCodeProvidersKeptCurrent(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
thinkCatalog(t)
|
|
oc := opencode(home, filepath.Join(home, ".config"))
|
|
if err := oc.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := oc.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := edit.GetJSON(oc.Path, "providers"); ok {
|
|
t.Fatal("providers written into a config that had none")
|
|
}
|
|
openChamberSaved(t, oc.Path)
|
|
if err := oc.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providersCurrent(t, oc.Path)
|
|
// and the next sync has nothing to write
|
|
var wrote []string
|
|
if err := edit.Filtered(func(p string, b []byte) []byte { wrote = append(wrote, p); return b }, oc.Sync); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(wrote) > 0 {
|
|
t.Fatalf("a second sync wrote %v", wrote)
|
|
}
|
|
// a model picked again writes it there as well
|
|
openChamberSaved(t, oc.Path)
|
|
if err := oc.Apply("model", "magpie/think/new"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providersCurrent(t, oc.Path)
|
|
// stepped off magpie, its provider goes under both
|
|
if err := oc.Apply("model", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, k := range []string{"provider." + magpieID, openCodeNativeKey} {
|
|
if v, ok := edit.GetJSON(oc.Path, k); ok {
|
|
t.Fatalf("%s left: %s", k, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// OpenChamber, magpie's agent for the OpenCode it runs, writes magpie's
|
|
// provider into the same config: its sync, model and step off reach the
|
|
// block under providers as well.
|
|
func TestOpenChamberProvidersKeptCurrent(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
thinkCatalog(t)
|
|
writeFile(t, filepath.Join(home, ".config", "openchamber", "settings.json"), `{"defaultModel":"anthropic/claude-sonnet-5"}`)
|
|
ch := openChamber(home, filepath.Join(home, ".config"))
|
|
if err := ch.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := opencode(home, filepath.Join(home, ".config")).Path
|
|
openChamberSaved(t, path)
|
|
if err := ch.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providersCurrent(t, path)
|
|
openChamberSaved(t, path)
|
|
if err := ch.Apply("model", "magpie/think/new"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providersCurrent(t, path)
|
|
if err := ch.Apply("model", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, k := range []string{"provider." + magpieID, openCodeNativeKey} {
|
|
if v, ok := edit.GetJSON(path, k); ok {
|
|
t.Fatalf("%s left: %s", k, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// What says whether OpenCode reaches magpie reads the block under
|
|
// providers too, which OpenCode 2 sends by: one at another address is off
|
|
// until a sync brings it back.
|
|
func TestOpenCodeCheckReadsProviders(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
thinkCatalog(t)
|
|
oc := opencode(home, filepath.Join(home, ".config"))
|
|
if err := oc.Apply("model", "magpie/think/deep"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if off := oc.Check(); off != "" {
|
|
t.Fatalf("off on magpie's own: %s", off)
|
|
}
|
|
if err := edit.SetJSON(oc.Path, edit.KV{Path: openCodeNativeKey, Value: map[string]any{
|
|
"settings": map[string]any{"baseURL": "http://127.0.0.1:1/v1", "apiKey": "old"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if off := oc.Check(); !strings.Contains(off, "baseURL") || !strings.Contains(off, "127.0.0.1:1/v1") {
|
|
t.Fatalf("a block under providers at another address: %q", off)
|
|
}
|
|
if err := oc.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if off := oc.Check(); off != "" {
|
|
t.Fatalf("after a sync: %s", off)
|
|
}
|
|
}
|
|
|
|
func raw1Of(path string) string {
|
|
raw, _ := edit.GetJSON(path, "provider."+magpieID)
|
|
return raw
|
|
}
|
|
|
|
// jsonKeyOrder is the order of the keys of the object under key in the
|
|
// JSON object raw.
|
|
func jsonKeyOrder(t *testing.T, raw, key string) []string {
|
|
t.Helper()
|
|
var top map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(raw), &top); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d := json.NewDecoder(bytes.NewReader(top[key]))
|
|
if tok, err := d.Token(); err != nil || tok != json.Delim('{') {
|
|
t.Fatalf("%s is no object: %s", key, top[key])
|
|
}
|
|
var keys []string
|
|
for d.More() {
|
|
tok, err := d.Token()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys = append(keys, tok.(string))
|
|
var skip json.RawMessage
|
|
if err := d.Decode(&skip); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// magpie's models go under providers in the order magpie lists them, as
|
|
// under provider (#4575), and a model's cost in OpenCode 2's words: a list
|
|
// of tiers, its cache prices under cache (migrateModel's costs at v2.2.5).
|
|
func TestOpenCodeNativeKeepsOrderAndCost(t *testing.T) {
|
|
block := map[string]any{"name": "magpie", "npm": "@ai-sdk/openai-compatible", "models": orderedJSON{
|
|
{"zeta/z", map[string]any{"name": "Z", "cost": map[string]any{"input": 1.0, "output": 2.0, "cache_read": 0.1, "cache_write": 1.25}}},
|
|
{"alpha/a", map[string]any{"name": "A"}},
|
|
}}
|
|
b, err := json.Marshal(openCodeNative(block))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if order := jsonKeyOrder(t, string(b), "models"); !reflect.DeepEqual(order, []string{"zeta/z", "alpha/a"}) {
|
|
t.Fatalf("order %v: %s", order, b)
|
|
}
|
|
var got struct {
|
|
Models map[string]struct {
|
|
Cost json.RawMessage `json:"cost"`
|
|
} `json:"models"`
|
|
}
|
|
if err := json.Unmarshal(b, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := `[{"cache":{"read":0.1,"write":1.25},"input":1,"output":2}]`; string(got.Models["zeta/z"].Cost) != want {
|
|
t.Fatalf("cost %s, want %s", got.Models["zeta/z"].Cost, want)
|
|
}
|
|
if got.Models["alpha/a"].Cost != nil {
|
|
t.Fatalf("a model with no price got a cost: %s", got.Models["alpha/a"].Cost)
|
|
}
|
|
}
|