LotusDecoder on X runs two magpies, each the other's provider. Codex says store false, so the leg between them carried the whole conversation every turn. Now passthrough asks a remote magpie to keep the Responses turn (X-Magpie-Keep-Turn: magpie/<version>). A magpie that does answers X-Magpie-Kept-Turn. This side records per route and scope a digest of each input item as sent, the reply's output types and ids, and the other magpie's id (remoteTurns: 16 per route and scope, 32 MB, prevTTL). The next request that continues exactly that input and reply goes as the items after them with previous_response_id. Anything else goes whole: an edited history, another answer, a magpie that never said it keeps (an older one). A refused delta is sent again whole at once, and the route goes whole for 30 minutes. The receiving magpie honours the header only from a magpie's hop (keepTurnAsks): its value starts magpie/ and the User-Agent is magpie's, or Codex's, which forwardOnce passes on for Codex turns. It keeps the turn under the caller's prevScope and the usual caps. An id such a caller names that isn't held gets previous_response_not_found before any vendor is asked. withCaller takes the header off on arrival, and X-Magpie- is never a passable request header, so no vendor gets it. Run on mote (mote-run) at base93d5c76c0with this tree: - gofmt -l internal/gateway internal/provider: clean - GOOS=linux go vet -tags nogui ./internal/gateway: ok - GOOS=windows go vet ./internal/gateway: ok - go test -tags nogui -count=1 -timeout 45m ./internal/gateway: ok (1123.7s) - The five new tests, plus TestRemoteMagpie*, TestPreviousResponse*, TestUnknownPreviousResponse and TestPrevStore: 54 passed. - Again rebased on538026e62, the same suite: one red, TestUnroutedEmptyEncodedStreamIsBounded/gzip ("TempDir RemoveAll cleanup: ... magpie/held: directory not empty"). It is #4775's test: the held report is written from the timer after the 408, so the test can end mid-write. Not this change; fixed in its own commit. - At the pushed base50aaa5726: gofmt clean, Linux and Windows vet ok, the related tests above (57 passes) ok. Broken by hand, each red, then restored: - delta never sent: SentTheNewInput, LostTurnSentWhole and the EditedHistory control go red. - the header honoured from any client: KeepTurnOnlyFromAMagpie goes red (curl told kept). - refusal not remembered: LostTurnSentWhole goes red (turn 3 is tried as a delta). - deltas without the Kept-Turn reply: OlderRemoteMagpieSentWhole goes red. - keptTurnLost check removed: KeepTurnOnlyFromAMagpie goes red (the lost id is relayed). - output marks not compared: EditedHistory "other answer" goes red. - the withCaller strip and the X-Magpie- never-pass prefix both removed: KeepTurnOnlyFromAMagpie goes red (vendor sent the header). Either one alone holds it, so removing only the strip stays green. The new tests don't compile on the old code, since they name the new header constants and remoteTurns. Found on the way: a Codex turn reaches the remote magpie with Codex's User-Agent, not magpie's, so callerFrom's existing X-Magpie-Agent/Via reading never applies to Codex hops. Unchanged here; keepTurnAsks accepts that shape. Not tried: - two real magpies over a real link, and a WebSocket-fed Codex (only HTTP turns in tests); - an SSE reply over the hop (tee parses SSE through prevReply, which prev_response tests cover, but no remote-delta test streams); - other platforms' test runs (Linux and Windows were vetted only). Known gaps: - the reply is matched by type and ids, so a client that edits the last answer's text (no ids) gets a delta over the kept one; - tools and instructions are still sent every turn; - the remote's kept turns are in memory only; - a client that copies a hop's headers gets its store-false turns kept under its own scope and the caps.
441 lines
14 KiB
Go
441 lines
14 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"container/list"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A Responses client may send only a turn's new input and name the
|
|
// response it follows (previous_response_id), as OpenAI's own API lets it
|
|
// (kevinzhow on X: every request carried the whole conversation, images
|
|
// and all). magpie keeps each response it gave such a client, its input
|
|
// and its output, under the response's id, and a request that names one
|
|
// is given the conversation whole before anything else reads it: a
|
|
// translation, a hook, the masking. A provider that keeps responses itself
|
|
// (prevKeeps), and answered the one named, is sent the new input alone
|
|
// after its own id, so what it holds isn't sent again.
|
|
//
|
|
// What is kept is the client's own form, before masking: the conversation
|
|
// it is given back into goes through the masking as every request does.
|
|
// It is kept in memory alone, bounded (prevCap) and for prevTTL, under the
|
|
// caller's gateway key (prevScope): one key never reads another's.
|
|
// A client that says store false (Codex over HTTP, whose requests never
|
|
// name a previous response: codex-rs/codex-api/src/common.rs keeps
|
|
// previous_response_id to its websocket's response.create) has nothing
|
|
// kept, but for another magpie asking its turns kept (remote_delta.go).
|
|
|
|
const (
|
|
prevTTL = 6 * time.Hour
|
|
// prevCap bounds the bytes kept, counted as each turn adds them: a
|
|
// turn's history is the earlier turn's items, shared, not copied
|
|
prevCap = 256 << 20
|
|
// prevReplyMax is the most of one reply read for its output
|
|
prevReplyMax = 64 << 20
|
|
)
|
|
|
|
type prevKey struct{ scope, id string }
|
|
|
|
// prevTurn is one response kept: the conversation up to and with it.
|
|
type prevTurn struct {
|
|
key prevKey
|
|
items []json.RawMessage // input, then the response's output
|
|
own int // bytes this turn added to what it follows
|
|
at time.Time
|
|
// the provider that keeps it too, if one does: the id it gave it
|
|
// and which provider and key (prevRoute)
|
|
upstreamID, route string
|
|
}
|
|
|
|
type prevStore struct {
|
|
mu sync.Mutex
|
|
m map[prevKey]*list.Element
|
|
order list.List // front the newest
|
|
size int
|
|
cap int
|
|
ttl time.Duration
|
|
now func() time.Time
|
|
}
|
|
|
|
var prevResponses = newPrevStore(prevCap, prevTTL)
|
|
|
|
func newPrevStore(cap int, ttl time.Duration) *prevStore {
|
|
return &prevStore{m: map[prevKey]*list.Element{}, cap: cap, ttl: ttl, now: time.Now}
|
|
}
|
|
|
|
func (s *prevStore) get(k prevKey) (*prevTurn, bool) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
e, ok := s.m[k]
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
t := e.Value.(*prevTurn)
|
|
if s.now().Sub(t.at) > s.ttl {
|
|
s.drop(e)
|
|
return nil, false
|
|
}
|
|
s.order.MoveToFront(e)
|
|
return t, true
|
|
}
|
|
|
|
func (s *prevStore) put(t *prevTurn) {
|
|
if t.own > s.cap/2 {
|
|
return // one turn would push out nearly everything
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
if e, ok := s.m[t.key]; ok {
|
|
s.drop(e)
|
|
}
|
|
t.at = s.now()
|
|
s.m[t.key] = s.order.PushFront(t)
|
|
s.size += t.own
|
|
for s.size > s.cap {
|
|
s.drop(s.order.Back())
|
|
}
|
|
// and what has outlived its time, from the oldest
|
|
for e := s.order.Back(); e != nil && s.now().Sub(e.Value.(*prevTurn).at) > s.ttl; e = s.order.Back() {
|
|
s.drop(e)
|
|
}
|
|
}
|
|
|
|
func (s *prevStore) drop(e *list.Element) {
|
|
t := e.Value.(*prevTurn)
|
|
s.order.Remove(e)
|
|
delete(s.m, t.key)
|
|
s.size -= t.own
|
|
}
|
|
|
|
// prevScope is whose responses a request may name: its gateway key, the
|
|
// computer a remote magpie passed it on from, and for a request with no
|
|
// key from another computer, that computer's address.
|
|
func prevScope(r *http.Request) string {
|
|
keyID := access.Caller(r.Context()).KeyID
|
|
s := "key:" + keyID
|
|
if via := callerOf(r).via; via != "" {
|
|
s += "\x00via:" + via
|
|
}
|
|
if keyID == "" && !local(r) {
|
|
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
host = r.RemoteAddr
|
|
}
|
|
s += "\x00ip:" + host
|
|
}
|
|
return s
|
|
}
|
|
|
|
// prevRoute is the provider and API key a response came from: one the
|
|
// provider keeps is named to it again only with the same key.
|
|
func prevRoute(p provider.Provider) string {
|
|
sum := sha256.Sum256([]byte(p.Key))
|
|
return p.ID + "\x00" + hex.EncodeToString(sum[:8])
|
|
}
|
|
|
|
// prevKeeps is whether p keeps the responses it gives and takes
|
|
// previous_response_id: OpenAI's API and Azure OpenAI, and Ark's own
|
|
// /api/v3 (tried with a real key on 10-29: a stored response's word came
|
|
// back after its id, and an unknown id is a 400 PreviousResponseNotFound).
|
|
// A subscription's or a plugin's account is never asked so.
|
|
var prevKeptBy = prevKeeps
|
|
|
|
func prevKeeps(p provider.Provider) bool {
|
|
if p.Account != nil || p.PluginProvider() != "" {
|
|
return false
|
|
}
|
|
host := p.Host()
|
|
if host == "api.openai.com" || p.IsAzure() {
|
|
return true
|
|
}
|
|
return host == "ark.cn-beijing.volces.com" && strings.Contains(p.Base(provider.Responses), "/api/v3")
|
|
}
|
|
|
|
// prevAsk is what serveAgent made of a request's previous_response_id,
|
|
// for the tries of it (attempt, passthrough).
|
|
type prevAsk struct {
|
|
id string // as the client named it
|
|
known bool // the conversation was given back whole
|
|
// the items of the whole conversation, and how many at its end are
|
|
// the client's own new input
|
|
total, fresh int
|
|
upstreamID string // the id the provider that answered it keeps it under
|
|
route string
|
|
// which try answered: its route if it was relayed to a provider
|
|
// that keeps what it is sent, empty otherwise
|
|
mu sync.Mutex
|
|
answered string
|
|
}
|
|
|
|
type prevAskKey struct{}
|
|
|
|
func prevAskOf(ctx context.Context) *prevAsk {
|
|
a, _ := ctx.Value(prevAskKey{}).(*prevAsk)
|
|
return a
|
|
}
|
|
|
|
// prevLost is the id a request named that magpie doesn't hold, which a
|
|
// try that doesn't relay it to a provider as written can't honour.
|
|
func prevLost(r *http.Request) string {
|
|
if a := prevAskOf(r.Context()); a != nil && a.id != "" && !a.known {
|
|
return a.id
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// writePrevNotFound is OpenAI's own answer to an id it doesn't hold.
|
|
func writePrevNotFound(w http.ResponseWriter, id string) (int, string) {
|
|
msg := "Previous response with id '" + id + "' not found."
|
|
return writeErrorSaid(w, provider.Responses, 400, msg, "previous_response_not_found", "previous_response_id"), msg
|
|
}
|
|
|
|
// noteAnswered is called by each try: the last is the one that answered.
|
|
func noteAnswered(r *http.Request, route string) {
|
|
if a := prevAskOf(r.Context()); a != nil {
|
|
a.mu.Lock()
|
|
a.answered = route
|
|
a.mu.Unlock()
|
|
}
|
|
}
|
|
|
|
// previousResponse gives a Responses request that names a response magpie
|
|
// holds the conversation whole, and keeps the reply for the next turn.
|
|
// Its done is deferred by the caller.
|
|
func previousResponse(w http.ResponseWriter, r *http.Request, from provider.Protocol, body []byte) (http.ResponseWriter, *http.Request, []byte, func()) {
|
|
nothing := func() {}
|
|
if from != provider.Responses {
|
|
return w, r, body, nothing
|
|
}
|
|
var q map[string]json.RawMessage
|
|
if decodeJSONView(body, &q) != nil {
|
|
return w, r, body, nothing
|
|
}
|
|
scope := prevScope(r)
|
|
ask := &prevAsk{}
|
|
var prior []json.RawMessage
|
|
if raw := q["previous_response_id"]; len(raw) > 0 && !bytes.Equal(raw, []byte("null")) {
|
|
var id string
|
|
if json.Unmarshal(raw, &id) == nil && id != "" {
|
|
ask.id = id
|
|
if t, ok := prevResponses.get(prevKey{scope, id}); ok {
|
|
prior = t.items
|
|
ask.known, ask.upstreamID, ask.route = true, t.upstreamID, t.route
|
|
}
|
|
}
|
|
}
|
|
input, fresh := prevInput(q["input"])
|
|
if fresh == nil {
|
|
// an input magpie can't read as items: left as it came
|
|
return w, r, body, nothing
|
|
}
|
|
if ask.known {
|
|
input = append(prior[:len(prior):len(prior)], fresh...)
|
|
if b, err := json.Marshal(input); err == nil {
|
|
q["input"] = b
|
|
delete(q, "previous_response_id")
|
|
if nb, err := json.Marshal(q); err == nil {
|
|
body = nb
|
|
}
|
|
}
|
|
ask.total, ask.fresh = len(input), len(fresh)
|
|
}
|
|
if ask.id != "" {
|
|
r = r.WithContext(context.WithValue(r.Context(), prevAskKey{}, ask))
|
|
}
|
|
var store bool
|
|
if json.Unmarshal(q["store"], &store) == nil && !store && !keepsTurnFor(r) {
|
|
return w, r, body, nothing // the client asked that nothing be kept
|
|
}
|
|
if keepsTurnFor(r) {
|
|
// another magpie asked it kept, store false or not, and is told
|
|
// it is (remote_delta.go)
|
|
w.Header().Set(KeptTurnHeader, "1")
|
|
}
|
|
// what this turn adds: its input, copied out of the body. A view held
|
|
// the whole body, tools and instructions with it, on every turn kept,
|
|
// while own counted only the items, so the cap bounded far less than
|
|
// was held.
|
|
kept := ownItems(fresh)
|
|
if ask.known {
|
|
kept = append(prior[:len(prior):len(prior)], kept...)
|
|
}
|
|
own := 0
|
|
for _, it := range fresh {
|
|
own += len(it)
|
|
}
|
|
if ask.id == "" {
|
|
// the answer note is wanted for a request naming none too
|
|
r = r.WithContext(context.WithValue(r.Context(), prevAskKey{}, ask))
|
|
}
|
|
sink := &conversationWriter{captureResponseWriter: &captureResponseWriter{
|
|
ResponseWriter: w, full: &spool{limit: prevReplyMax, pattern: "magpie-prevresp-*"},
|
|
}}
|
|
return sink, r, body, func() {
|
|
defer sink.full.discard()
|
|
if sink.status != 0 && sink.status != http.StatusOK || sink.full.cut() {
|
|
return
|
|
}
|
|
reply, err := sink.full.read()
|
|
if err != nil || len(reply) == 0 {
|
|
return
|
|
}
|
|
id, output, ok := prevReply(sink.Header().Get("Content-Type"), reply)
|
|
if !ok {
|
|
return
|
|
}
|
|
for _, it := range output {
|
|
own += len(it)
|
|
}
|
|
t := &prevTurn{key: prevKey{scope, id}, items: append(kept, output...), own: own}
|
|
ask.mu.Lock()
|
|
if ask.answered != "" {
|
|
t.upstreamID, t.route = id, ask.answered
|
|
}
|
|
ask.mu.Unlock()
|
|
prevResponses.put(t)
|
|
}
|
|
}
|
|
|
|
// prevInput is a Responses input as items: a string is one user message.
|
|
// fresh is nil for an input that is neither. The items share the body's
|
|
// bytes, which nothing writes once read; what is kept past the request
|
|
// copies them (ownItems).
|
|
func prevInput(raw json.RawMessage) (all, fresh []json.RawMessage) {
|
|
raw = bytes.TrimSpace(raw)
|
|
if len(raw) == 0 {
|
|
return nil, nil
|
|
}
|
|
if raw[0] == '"' {
|
|
var s string
|
|
if json.Unmarshal(raw, &s) != nil {
|
|
return nil, nil
|
|
}
|
|
m, _ := json.Marshal(map[string]any{"type": "message", "role": "user", "content": s})
|
|
return []json.RawMessage{m}, []json.RawMessage{m}
|
|
}
|
|
items, ok := inputList(raw)
|
|
if !ok || raw[0] != '[' {
|
|
return nil, nil
|
|
}
|
|
if items == nil {
|
|
items = []json.RawMessage{}
|
|
}
|
|
return items, items
|
|
}
|
|
|
|
// prevReply is a Responses reply's id and output items, a stream's or a
|
|
// whole body's; ok false for one that didn't complete.
|
|
func prevReply(ctype string, reply []byte) (id string, output []json.RawMessage, ok bool) {
|
|
var resp json.RawMessage
|
|
if strings.Contains(ctype, "event-stream") || bytes.HasPrefix(reply, []byte("event:")) || bytes.HasPrefix(reply, []byte("data:")) {
|
|
items := map[int]json.RawMessage{}
|
|
readSSE(bytes.NewReader(reply), func(event, data string) error {
|
|
var e struct {
|
|
Type string `json:"type"`
|
|
Response json.RawMessage `json:"response"`
|
|
Item json.RawMessage `json:"item"`
|
|
OutputIndex int `json:"output_index"`
|
|
}
|
|
if json.Unmarshal([]byte(data), &e) != nil {
|
|
return nil
|
|
}
|
|
if e.Type == "" {
|
|
e.Type = event
|
|
}
|
|
switch e.Type {
|
|
case "response.output_item.done":
|
|
if len(e.Item) > 0 {
|
|
items[e.OutputIndex] = e.Item
|
|
}
|
|
case "response.completed", "response.incomplete":
|
|
resp = e.Response
|
|
}
|
|
return nil
|
|
})
|
|
if len(resp) == 0 {
|
|
return "", nil, false
|
|
}
|
|
resp = withStreamedOutput(resp, items)
|
|
} else {
|
|
resp = reply
|
|
}
|
|
var r struct {
|
|
ID string `json:"id"`
|
|
Object string `json:"object"`
|
|
Status string `json:"status"`
|
|
Output []json.RawMessage `json:"output"`
|
|
}
|
|
if json.Unmarshal(resp, &r) != nil || r.ID == "" || r.Status == "failed" || r.Status == "cancelled" {
|
|
return "", nil, false
|
|
}
|
|
return r.ID, r.Output, true
|
|
}
|
|
|
|
// prevDelta is body, a Responses request magpie gave its conversation
|
|
// whole, with the new input alone after the id p keeps the previous
|
|
// response under, when p is the provider and key that answered it and
|
|
// keeps what it is sent. The tries' changes to the input are kept: the
|
|
// new items are the body's own last ones, and only when the body still
|
|
// has as many items as magpie gave it.
|
|
func prevDelta(r *http.Request, p provider.Provider, body []byte) ([]byte, bool) {
|
|
a := prevAskOf(r.Context())
|
|
if a == nil || !a.known || a.upstreamID == "" || a.route != prevRoute(p) || !prevKeptBy(p) {
|
|
return body, false
|
|
}
|
|
var q map[string]json.RawMessage
|
|
if decodeJSONView(body, &q) != nil {
|
|
return body, false
|
|
}
|
|
var store bool
|
|
if json.Unmarshal(q["store"], &store) == nil && !store {
|
|
return body, false
|
|
}
|
|
items, ok := inputList(q["input"])
|
|
if !ok || len(items) != a.total || a.fresh > len(items) {
|
|
return body, false
|
|
}
|
|
in, err := json.Marshal(items[len(items)-a.fresh:])
|
|
if err != nil {
|
|
return body, false
|
|
}
|
|
id, _ := json.Marshal(a.upstreamID)
|
|
out := make(map[string]json.RawMessage, len(q)+1)
|
|
for k, v := range q {
|
|
out[k] = v
|
|
}
|
|
out["input"], out["previous_response_id"] = in, id
|
|
nb, err := json.Marshal(out)
|
|
if err != nil {
|
|
return body, false
|
|
}
|
|
return nb, true
|
|
}
|
|
|
|
// prevRefused is an upstream's answer that it doesn't hold the previous
|
|
// response, or takes no such field: the conversation is sent whole.
|
|
func prevRefused(status int, b []byte) bool {
|
|
if !badRequest(status) && status != http.StatusNotFound {
|
|
return false
|
|
}
|
|
s := strings.ToLower(string(b))
|
|
return strings.Contains(s, "previous_response") || strings.Contains(s, "previousresponse")
|
|
}
|
|
|
|
// storeOff is whether a Responses body says store false.
|
|
func storeOff(body []byte) bool {
|
|
v := gjson.GetBytes(body, "store")
|
|
return v.Exists() && v.Type == gjson.False
|
|
}
|