Files
magpie/internal/agent/plugged_connect_facts_test.go
T
yetone 0b72ccbeff agentplug: connect's models carry /v1/models' native_endpoints and pricing
cleary5779 on Discord builds an agent as an agent plugin and wants, in
connect({ gateway, model, models }), the API each model passes straight
through on and what it costs. Each entry of models (and model) now also
has, by the names /v1/models uses:

  "native_endpoints": ["/v1/chat/completions"],
  "pricing": {"prompt": "0.0000003", "completion": "0.0000012",
              "input_cache_read": "0.00000003"}

Each is left out when not known: a group or a model translated anyway
has no native_endpoints, an unpriced model no pricing, never [] or 0.
The fields connect had (id, name, context, output, images, efforts) are
as they were.

No second lookup: nativeEndpoints moved from the gateway to
provider.NativeEndpoints, and openRouterPricing/perToken to
catalog.OpenRouterPricing/PerToken (internal/catalog/openrouter.go); the
gateway calls them as before. magpieModels already priced each model with
provider.EntryPricer, the price /v1/models gives (m.Price); it now also
keeps NativeEndpoints in catalog.Model.Endpoints (json:"-"), which
plugged's wire turns into the two keys.

Docs: reference.md's agent plugins section (what connect gets, with an
example), agent-wiring and gateway-routing subsystem pages, and the
site's Plugins page in en, zh and ja.

Run at this tree (on origin/main 66b6d6ade), load ~150:
- gofmt -l internal: nothing. go vet on the touched packages,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test ./internal/catalog ./internal/provider: ok (agentplug has no
  tests). go test ./internal/gateway -run 'Models|PerToken|OpenCode|
  Native|Cursor': ok.
- go test ./internal/agent -run 'Plugged|Pi|Price|Alma|ModelOrder|Image|
  Group|Codex': one red, TestCodexThreadsComeBackOffMagpie/disconnect
  (.cache/magpie/sessions.json left in its TempDir), open as #3422; it
  passes 5 of 5 alone and this change doesn't touch sessions.
- TestPluggedConnectSeesEndpointsAndPricing is red with plugged.go's two
  keys taken out (no native_endpoints, no pricing on model or models),
  green with them.
Not run: the full ./... suite (machine at load ~150 with several suites
queued); the whole agent and gateway packages. Not tried: a real agent
plugin reading the fields.
2026-10-25 03:39:50 +08:00

99 lines
3.2 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// An agent plugin's connect is told, of each model in models and of
// model, what /v1/models says of it by the same names: native_endpoints,
// the APIs a request goes on untranslated, and pricing, OpenRouter's USD
// a token (cleary5779 on Discord, a self-built agent picking the API that
// passes straight through and showing what a call costs). A model with no
// known price has no pricing at all, never a 0.
func TestPluggedConnectSeesEndpointsAndPricing(t *testing.T) {
home := pluggedHome(t, `
export const agent = { id: "facts", name: "Facts", config: "~/.facts.json", model: "model" };
export function connect({ gateway, model, models }) {
return { "seen": JSON.stringify({ model, models }) };
}
`)
if err := provider.SetModelPrice("deepseek/pro", &catalog.Price{Input: 0.3, Output: 1.2, CacheRead: 0.03}); err != nil {
t.Fatal(err)
}
a, err := Find("facts")
if err != nil {
t.Fatal(err)
}
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
b, err := os.ReadFile(filepath.Join(home, ".facts.json"))
if err != nil {
t.Fatal(err)
}
var file struct{ Seen string }
if err := json.Unmarshal(b, &file); err != nil {
t.Fatalf("%v:\n%s", err, b)
}
type entry struct {
ID string `json:"id"`
Native []string `json:"native_endpoints"`
Pricing map[string]string `json:"pricing"`
HasPrice bool `json:"-"`
HasNative bool `json:"-"`
}
var seen struct {
Model map[string]json.RawMessage `json:"model"`
Models []map[string]json.RawMessage `json:"models"`
}
if err := json.Unmarshal([]byte(file.Seen), &seen); err != nil {
t.Fatalf("%v:\n%s", err, file.Seen)
}
read := func(raw map[string]json.RawMessage) entry {
var e entry
b, _ := json.Marshal(raw)
json.Unmarshal(b, &e)
_, e.HasPrice = raw["pricing"]
_, e.HasNative = raw["native_endpoints"]
return e
}
byID := map[string]entry{}
for _, m := range seen.Models {
e := read(m)
byID[e.ID] = e
}
pro, flash, model := byID["deepseek/pro"], byID["deepseek/flash"], read(seen.Model)
want := map[string]string{"prompt": "0.0000003", "completion": "0.0000012", "input_cache_read": "0.00000003"}
for name, e := range map[string]entry{"models' pro": pro, "model": model} {
if len(e.Native) != 1 || e.Native[0] != "/v1/chat/completions" {
t.Errorf("%s native_endpoints = %v, want [/v1/chat/completions]:\n%s", name, e.Native, file.Seen)
}
if len(e.Pricing) != len(want) {
t.Errorf("%s pricing = %v, want %v", name, e.Pricing, want)
}
for k, v := range want {
if e.Pricing[k] != v {
t.Errorf("%s pricing[%s] = %q, want %q", name, k, e.Pricing[k], v)
}
}
}
if flash.ID == "" || !flash.HasNative {
t.Errorf("flash missing or without native_endpoints:\n%s", file.Seen)
}
if flash.HasPrice {
t.Errorf("unpriced flash has pricing %v, want none", flash.Pricing)
}
// what connect was told before stays as it was
for _, k := range []string{"id", "name", "context", "output", "images", "efforts"} {
if _, ok := seen.Model[k]; !ok {
t.Errorf("model lost %q:\n%s", k, file.Seen)
}
}
}