Files
magpie/internal/provider/remote_magpie.go
T
yetone ccffdadb8a gateway: /v1/audio/transcriptions passes speech-to-text through to the provider (#2053, niyoh120)
niyoh120 asked for an OpenAI-style speech-to-text endpoint so a client's
voice input can go through magpie like its chat does. magpie had no
/v1/audio route, so the request 404ed.

What changed:
- POST /v1/audio/transcriptions and /v1/audio/translations (and the
  /audio/... spellings) route through the same core as embeddings and
  rerank. embeddings.go's routing is pulled out into serveRetrieval, so
  disabled models, key access, groups, pause rules, account holds,
  failover, redaction and usage records are shared rather than copied.
- transcribe.go reads the multipart form only to find `model` (as
  provider/model) and `prompt`. The form goes to the provider byte for
  byte; it is rebuilt part by part, under the same boundary, only when
  the model name is rewritten or the prompt was masked. The audio part
  is always copied as sent.
- Only providers with an OpenAI-style base are tried; an Anthropic-only
  provider gets a 400 saying so. A body that isn't multipart, a form with
  no model, or one that doesn't parse gets a 400 and nothing is sent.
- The vendor's answer type passes through (response_format text/srt/vtt
  stays text/plain). Usage records output tokens from usage.output_tokens.
- The 404 message, the info api list, remote magpie's suffix list, the
  CORS and LAN tests, docs/reference.md, gateway-routing.md and the
  remote docs (en, zh, ja) name the new paths.

What was run, at 9c36c3a0 (this change rebased on 10f38770):
- gofmt -l internal/gateway internal/provider: empty
- go vet (darwin); GOOS=linux and GOOS=windows go vet -tags nogui on
  ./internal/gateway ./internal/provider: clean
- go test -tags nogui -timeout 45m ./internal/gateway ./internal/provider:
  ok (1367s, 339s); also green on two earlier bases (b4fe6663, 06fbfd95)
- Rebased again on a0ebe003 (main had moved 5 commits, none in these
  files; routing.go's Factory rest is the nearest): gofmt, vet on darwin,
  linux (nogui) and windows (nogui) clean, and
  go test -tags nogui -run 'Transcri|Embed|Rerank|CORS|LAN|Lan|Factory|
  Retriev|NotFound|Info' ./internal/gateway ok. The full packages were not
  rerun on this last base.

Break-checks, each turned red and restored:
- routes removed (the old code): all 5 new tests fail
- model never rewritten: TestTranscriptionGoesToTheModelsProvider and
  TestTranscriptionPromptRedacted fail
- answer always labelled JSON: TestTranscriptAsTextKeepsItsType fails
- rebuild truncates the audio part: Goes + PromptRedacted fail
- prompt not masked: TestTranscriptionPromptRedacted fails
- output tokens ignored: TestTranscriptionGoesToTheModelsProvider fails

Real use: through magpie's real handler against aihubmix
(https://aihubmix.com/v1) with a `say`-made m4a: whisper-1 json 200
{"text":"Hello from the Magpie Gateway."}, response_format=text 200,
srt 200 text/plain with SRT cues, translations 200.

Not tried: OpenAI direct (the key answers 429 "account is not active"),
Groq, a local vLLM/whisper server, and any real client's voice input.
2026-10-14 09:19:16 +08:00

150 lines
5.4 KiB
Go

package provider
import (
"maps"
"strings"
"github.com/yetone/magpie/internal/catalog"
)
// A remote magpie is another computer's magpie gateway, shared on its
// network (Settings → Share on local network), as a provider: the
// computers' agents stay wired by their own magpie, while the providers,
// routing groups and usage are the one magpie's they all share. Its
// address serves every API magpie does, so the provider speaks them all
// — Chat Completions and Responses at <address>/v1, Anthropic's Messages
// at the address — and a request goes on in the API the agent spoke. Its
// model list tells, of each model, the APIs its own provider serves it on
// (native_endpoints), and a request for one goes on one of those: turned
// into another API once, here, when it must be, never on both computers.
// Its list also has the image models the other magpie draws with, which
// it gives only to a magpie that asks for them (DrawersHeader), each
// marked "kind": "image": an older magpie would have taken one whose id
// doesn't say it draws for a model to chat with. They go on to the other
// magpie's images API, which asks their vendor as it would its own.
// Each model's label is the other magpie's name for it, with its provider
// there after it ("Claude Sonnet 5 · RelayA"), as its own agents see it:
// two of its providers' models of one name are told apart here too.
// Provider in model names Off here names each by its name there alone
// (display_name, catalog.Model's Plain), whatever that magpie's own
// setting put after it (ARNO on Discord).
// Its video models come the same way (VideomakersHeader), marked "kind":
// "video", and go on to its videos API (#545).
// Its list also tells what each model costs there (PricesHeader): the
// price the other magpie counts its usage at — what its user set for the
// model by hand, else its provider's list price, at that provider's price
// rate — so its calls cost the same here (Sorghum on Discord). It is this
// provider's list price for the model: a price set here for it still comes
// first, as one set for any provider's list price does. A magpie tells it
// only to its own computer and to one sharing it with a gateway key, and
// one from before it tells nothing, which leaves the models priced as
// they were: by models.dev.
// Its decision models are requested with DecidersHeader, marked
// "kind": "decision", and asked at its System One API, including models
// whose names aren't Jev's.
// DrawersHeader asks a magpie's model list for its image models as well.
const DrawersHeader = "X-Magpie-Drawers"
// VideomakersHeader asks a magpie's model list for its video models as
// well: a magpie that doesn't send it would take grok-imagine-video, whose
// id says "imagine", for an image model.
const VideomakersHeader = "X-Magpie-Videomakers"
// PricesHeader asks a magpie's model list for what each model costs
// there ("magpie_price"): a magpie that doesn't send it is told nothing
// more than before.
const PricesHeader = "X-Magpie-Prices"
// DecidersHeader asks a magpie's model list for its decision models too.
const DecidersHeader = "X-Magpie-Deciders"
// RemoteMagpiePreset is the preset's id.
const RemoteMagpiePreset = "remote-magpie"
// IsRemoteMagpie reports whether the provider is another magpie.
func (p Provider) IsRemoteMagpie() bool { return p.Preset == RemoteMagpiePreset }
// remoteMagpieEndpoints puts every API on the address given, however it
// was typed: the bare host and port (plain http, which magpie serves),
// …/v1, or the Anthropic root.
func (p *Provider) remoteMagpieEndpoints() {
if !p.IsRemoteMagpie() {
return
}
root := ""
for _, u := range []string{p.Chat, p.Responses, p.Anthropic, p.Decide} {
if u = strings.TrimRight(strings.TrimSpace(u), "/"); u != "" {
root = u
break
}
}
if root == "" {
return
}
if !strings.Contains(root, "://") {
root = "http://" + root
}
for _, suf := range []string{
"/v1/messages/count_tokens", "/v1/messages",
"/v1/chat/completions", "/v1/responses", "/v1/systemone",
"/v1/embeddings", "/v1/rerank",
"/v1/audio/transcriptions", "/v1/audio/translations",
"/v1/images/generations", "/v1/images/edits",
"/v1/videos", "/v1",
} {
if b, ok := strings.CutSuffix(root, suf); ok {
root = b
break
}
}
p.Chat, p.Responses, p.Anthropic = root+"/v1", root+"/v1", root
p.Decide = p.Chat
}
// listHeaders are the headers p's model list is asked with: a remote
// magpie is asked for its image, video and decision models too, and its prices.
func (p Provider) listHeaders() map[string]string {
if !p.IsRemoteMagpie() {
return p.Headers
}
h := maps.Clone(p.Headers)
if h == nil {
h = map[string]string{}
}
h[DrawersHeader] = "1"
h[VideomakersHeader] = "1"
h[PricesHeader] = "1"
h[DecidersHeader] = "1"
return h
}
// remotePrice is what a remote magpie's list said the model costs there,
// if it said.
func (p Provider) remotePrice(model string) (catalog.Price, bool) {
if !p.IsRemoteMagpie() {
return catalog.Price{}, false
}
ms, _, ok := p.live()
if !ok {
return catalog.Price{}, false
}
for _, m := range ms {
if m.ID == model && m.Price != nil {
return *m.Price, true
}
}
return catalog.Price{}, false
}
// RemotePriced reports whether the model's list price is the one a remote
// magpie's list gave, for the editor to say whose it is.
func (p Provider) RemotePriced(model string) bool {
_, ok := p.remotePrice(model)
return ok
}