Files
magpie/internal/gateway/middleware.go
T
yetone 3e5bc3d8fe gateway, usage: conversation text is kept only under a session the client named, and a client's "request-…" session counts like any other (#1355)
What changed: with recording on, a request that names no session used to
get a made-up "request-…" id and its text saved under it. Nothing listed
that id and GatewaySessionByID turned it away, so the text could never be
opened. magpie now makes up no id: such a request isn't recorded, and the
X-Magpie-Session reply header only echoes the client's own session.

With no made-up ids left, the "request-" prefix checks in appendUsage,
OTel, Usage's sessions, Routing's vias and the gateway Sessions projection
only dropped sessions clients named themselves (appendUsage wiped them from
the ledger). They are gone. The tests that held the old rule now say the
new one, and gateway-sessions.md says it.

Run: TestGatewayConversationNeedsTheClientsSession and
TestGatewayConversationClientRequestPrefixedSession fail on the PR's code
(a made-up id is returned; the ledger keeps 0 of 2 calls) and pass here;
putting appendUsage's prefix check back turns the second red again.
gofmt -l clean; go vet; GOOS=linux and GOOS=windows go vet -tags nogui on
gateway, gui, sessions, settings, usage; go test -race -timeout 900s on
those five: ok (gateway's TestCORSKeyThroughTheServer rerun after its fix).
GUI: gateway-conversations, gateway-mode and all sessions-* tests, 216
cases in Chromium and WebKit: pass.
2026-10-10 04:24:18 +08:00

34 lines
1.1 KiB
Go

package gateway
import (
"net/http"
"github.com/yetone/magpie/internal/middleware"
"github.com/yetone/magpie/internal/provider"
)
// serveAgent is serve for a request an agent sent: the gateway middleware
// plugins (internal/middleware) see its body first, and its reply on the
// way back, in the agent's own API. magpie's own requests through serve
// (titles, the router's classifier, search and vision stand-ins) go
// without them, as their replies are magpie's to read.
func (s *Server) serveAgent(w http.ResponseWriter, r *http.Request, from provider.Protocol, body []byte) {
withGatewaySession(w, r)
w, recorded := recordConversation(w, r, from, body)
defer recorded()
run := middleware.Begin(middleware.Info{Protocol: string(from), Model: modelOf(body), Stream: streamOf(body), Path: r.URL.Path, Agent: agentOf(r)})
if run == nil {
s.serve(w, r, from, body)
return
}
defer run.End()
body, turned := run.Request(body)
if turned != nil {
writeError(w, from, turned.Status, turned.Message)
return
}
w, done := run.Wrap(w)
defer done()
s.serve(w, r, from, body)
}