zzzkl3994 on Discord: a video made through the magpie-image MCP was in Usage → Requests but never on Routing → Requests. The MCP's requests do go through the gateway, but `images`, `videosCreate` and `retrieve` (embeddings, rerank, audio transcriptions) wrote their usage row and began no route, so the routing trace never had them. Only the chat path, the Codex backend, titles and System One began routes. Each now begins one route (media_route.go, sideRoute) with the caller's agent and session, the model as asked, the provider and the routing group. Each key, account or group member asked is a try, put in Order so the page finds its seat. The route ends with the status, error and tokens, and the usage row names it in RouteID. Route.Usage is for display and is not a second ledger row, so nothing is counted twice. An image's accounts are tried in drawOnAccounts, which gets the route through the request's context. A video's polls and its download are the same generation; they add no route, as they add no usage row. A video turned away before any vendor was asked ends its route as failed. Route.Kind stays empty. Run: - New tests (media_route_test.go): TestImageIsOnRouting, TestFailedImageIsOnRouting, TestVideoIsOnRoutingOnce (create, three polls and the download give one route and one usage row), TestRefusedVideoIsOnRouting and TestEmbeddingAndRerankAreOnRouting. All are red at5d24bc6ccwithout the fix ("Routing lists 0 routes") and green with it (wsl-run). Their neighbours pass too: TestVideoIsStartedPolledAndFetched, TestImageGroupMovesOnToTheNextMember, TestCallerUsageVideos and TestEmbeddingsGoToTheModelsProvider. - Broken by hand. With RouteID dropped from the image's usage row, both image tests go red ("isn't the one the usage row names (0)"). With sideRoute.end always writing status 200, TestRefusedVideoIsOnRouting goes red. - Full suite at5d24bc6ccplus this change (wsl-run, `go test -tags nogui ./... -timeout 45m -count=1`): 77 packages ok. internal/agent has 4 reds (TestInstalledVersion, TestGrokInstalled, TestCopilotFolderFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir), all already filed for the WSL box at main in #4033. - GOOS=windows and GOOS=linux `go vet ./internal/gateway` (mote): ok. - Rebased on4d9058a98, then on95b40fb2f(redact and library only). internal/gateway and internal/imagemcp at the4d9058a98head (mote-run) are still running when this is pushed. Not tried: - A real generate_video through the MCP in the GUI. The Routing page's GUI suite wasn't run, as no GUI file changed; the route has the shape the page already reads (Order, Tries, Kind "").
146 lines
4.6 KiB
Go
146 lines
4.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// sideRoute is the Routing page's route of a request the chat path doesn't
|
|
// carry: an image, a video's start, an embedding, a rerank or a
|
|
// transcription. Each wrote its usage row but began no route, so Usage
|
|
// listed it and Routing never did (zzzkl3994 on Discord: a video the
|
|
// magpie-image MCP made). One route stands for one request, its tries the
|
|
// accounts, keys or group members asked in turn; a video's polls and its
|
|
// download are the same generation and add none, as they add no usage row.
|
|
// A nil sideRoute does nothing, so a caller with none goes on as before.
|
|
type sideRoute struct {
|
|
s *Server
|
|
tr *Route
|
|
start time.Time
|
|
}
|
|
|
|
// beginSide begins r's side route: asked is the model as the caller named
|
|
// it, prov the provider it resolved to first, group the routing group it
|
|
// named (nil for none).
|
|
func (s *Server) beginSide(r *http.Request, start time.Time, asked, prov string, group *GroupRef) *sideRoute {
|
|
who := callerOf(r)
|
|
tr := s.trace.beginFor(r, Route{Time: start, Agent: who.agent, Session: sessionOf(r.Header), Model: asked, Provider: prov,
|
|
Group: group, Order: []Weighed{}})
|
|
return &sideRoute{s: s, tr: tr, start: start}
|
|
}
|
|
|
|
// id is the route's id, which its usage row names (RouteID); 0 for none.
|
|
func (sr *sideRoute) id() int64 {
|
|
if sr == nil {
|
|
return 0
|
|
}
|
|
return sr.tr.ID
|
|
}
|
|
|
|
// masked puts what masking swapped in the request on the route.
|
|
func (sr *sideRoute) masked(m *Masked) {
|
|
if sr == nil || m == nil {
|
|
return
|
|
}
|
|
sr.s.trace.update(sr.tr, func(t *Route) { t.Masked = m })
|
|
}
|
|
|
|
// try begins a try on c, put in the route's order too when it isn't yet,
|
|
// as the page finds a try's seat there; the func it answers ends it.
|
|
func (sr *sideRoute) try(c candidate) func(status int, err error) {
|
|
return sr.tryAt(c, time.Now())
|
|
}
|
|
|
|
// tryAt is try for one begun at at, told once it is known who had it
|
|
// (a video's start picks its key inside startVideo).
|
|
func (sr *sideRoute) tryAt(c candidate, at time.Time) func(status int, err error) {
|
|
if sr == nil {
|
|
return func(int, error) {}
|
|
}
|
|
if c.rest == "" {
|
|
c.rest = candidateOf(c.p, c.model).rest
|
|
}
|
|
sr.s.trace.update(sr.tr, func(t *Route) {
|
|
t.Provider = c.p.ID
|
|
if !slices.ContainsFunc(t.Order, func(w Weighed) bool { return w.ID == c.rest && w.Model == c.model }) {
|
|
t.Order = append(t.Order, weighed(c, c.p, weighing{}, false, provider.Chat))
|
|
}
|
|
t.Tries = append(t.Tries, Try{ID: c.rest, Model: c.model, Start: at})
|
|
})
|
|
i := -1
|
|
sr.s.trace.update(sr.tr, func(t *Route) { i = len(t.Tries) - 1 })
|
|
return func(status int, err error) {
|
|
ms := time.Since(at).Milliseconds()
|
|
sr.s.trace.update(sr.tr, func(t *Route) {
|
|
if i < 0 || i >= len(t.Tries) {
|
|
return
|
|
}
|
|
try := &t.Tries[i]
|
|
try.Done, try.Status, try.Millis = true, status, ms
|
|
if err != nil {
|
|
try.Error = err.Error()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// end ends the route as the request ended: p and model the ones that had
|
|
// it last, u what they counted. A route ends once; a second end is a no-op.
|
|
func (sr *sideRoute) end(p provider.Provider, model string, status int, err error, u Usage) {
|
|
if sr == nil {
|
|
return
|
|
}
|
|
ms := time.Since(sr.start).Milliseconds()
|
|
sr.s.trace.update(sr.tr, func(t *Route) {
|
|
if t.Done {
|
|
return
|
|
}
|
|
if p.ID != "" {
|
|
t.Provider = p.ID
|
|
}
|
|
t.Done, t.Status, t.Millis, t.Tokens, t.Output = true, status, ms, u.Input+u.Output, u.Output
|
|
if err != nil {
|
|
t.Error = err.Error()
|
|
}
|
|
t.Usage = routeUsage(t.Provider, model, u)
|
|
})
|
|
}
|
|
|
|
// candidateOf is the candidate p, the key or account a request went out
|
|
// on, stands for, named as perKeyBarred names it in rest: the account
|
|
// signed in to by the provider's id, another by its user after an "@",
|
|
// one of several keys by its key after a "#", else the provider alone.
|
|
func candidateOf(p provider.Provider, model string) candidate {
|
|
c := candidate{p: p, model: model, rest: p.ID}
|
|
switch {
|
|
case p.Account != nil:
|
|
if own, err := provider.Find(p.ID); err == nil && own.Account != nil && !strings.EqualFold(own.Account.User, p.Account.User) {
|
|
c.rest = p.ID + "@" + p.Account.User
|
|
}
|
|
case p.Key != "" && len(p.KeysOn()) > 1:
|
|
c.rest = p.ID + "#" + provider.KeyID(p.Key)
|
|
}
|
|
return c
|
|
}
|
|
|
|
type sideRouteKey struct{}
|
|
|
|
// withSide carries sr to the code that asks each account (drawOnAccounts).
|
|
func withSide(ctx context.Context, sr *sideRoute) context.Context {
|
|
if sr == nil {
|
|
return ctx
|
|
}
|
|
return context.WithValue(ctx, sideRouteKey{}, sr)
|
|
}
|
|
|
|
// sideOf is the side route ctx carries, nil for none.
|
|
func sideOf(ctx context.Context) *sideRoute {
|
|
sr, _ := ctx.Value(sideRouteKey{}).(*sideRoute)
|
|
return sr
|
|
}
|