Files
magpie/internal/catalog/openrouter.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

46 lines
1.5 KiB
Go

package catalog
import (
"strconv"
"strings"
)
// OpenRouterPricing is a price as OpenRouter's model list writes it
// (gateway /v1/models' and an agent plugin's connect's "pricing"), the
// shape agents and SDKs built on that list read: USD a token, as decimal
// strings. prompt and completion are always there, a price set at 0
// included, since the price is known; a cache read or write only when it
// has a price, as a part a list leaves at 0 is one it doesn't give. The
// base price only: a model billed more above a prompt size (Tiers) tells
// what it costs below it, and the 1-hour cache write has no field there.
func OpenRouterPricing(pr Price) map[string]string {
out := map[string]string{"prompt": PerToken(pr.Input), "completion": PerToken(pr.Output)}
if pr.CacheRead > 0 {
out["input_cache_read"] = PerToken(pr.CacheRead)
}
if pr.CacheWrite > 0 {
out["input_cache_write"] = PerToken(pr.CacheWrite)
}
return out
}
// PerToken is a price of USD a million tokens as USD a token, written as
// a decimal: the decimal point moved, not the float divided, so $0.3 is
// "0.0000003" and not 2.9999999999999997e-07.
func PerToken(perMillion float64) string {
s := strconv.FormatFloat(max(perMillion, 0), 'f', -1, 64)
whole, frac, _ := strings.Cut(s, ".")
if len(whole) < 7 {
whole = strings.Repeat("0", 7-len(whole)) + whole
}
cut := len(whole) - 6
whole, frac = strings.TrimLeft(whole[:cut], "0"), strings.TrimRight(whole[cut:]+frac, "0")
if whole == "" {
whole = "0"
}
if frac == "" {
return whole
}
return whole + "." + frac
}