Files
magpie/internal/gateway/prompt_reply.go
T
yetone 0872bc2a0e context: a request's text and reply from the request archive (wangbinyq)
wangbinyq on Discord: with the request archive on, clicking a block of
the context window card still said "只保留最近 10 个请求的原文", since the
card read only the last 10 bodies magpie keeps in memory.

What changed:
- /api/context/text tries memory first, then the request archive's copy
  when the route names one (Route.Archive, the route found in the trace
  or in the history file of the card's ?day=, HistoryRoute looks a day
  either side). gateway.ArchivedPromptText parses it as PromptText does
  and masks it through maskedCopy, as the body in memory is; it is
  marked from "archive" and the card says so.
- New /api/context/reply: the reply to the request, from the Recent
  calls (Server.ReplyBody) or the archive's Response. gateway.ReplyPieces
  reads it into thinking/assistant/call pieces (conversationOutput, split
  out of conversationTurn) after redact.ScrubJSON, and masks every piece
  through maskedCopy. The card's text view has a Reply button beside
  Close that shows it and goes back to the part.
- What is shown is what the archive stores, masked as the memory copy
  would be, nothing more. A body or reply that can't be masked shows
  nothing (404 "unread"); one the archive cut short isn't parsed ("cut");
  one it names and doesn't have says so ("missing"), and one with no
  archive copy keeps the old "only the last 10" line ("off"). A failed
  bucket read is an error, not "gone".
- The archive read is capped at archiveReadMost and the last copy read is
  kept 5 minutes, so the items opened one by one read it once.
- ctxtext.js takes the route (id and time) instead of the id; context.js
  passes it (its one-line change paid for by a comment cut, core budget
  not raised). New strings in zh, zh-TW, ja and de.
- docs/subsystems/gateway-routing.md (Prompt) and gui-shell.md.

Run on f3335dd1b (main has since d73f2254d, textcalls_anthropic.go and
its test only, none of these files), on mote (macOS 27):
- go test -run 'TestArchivedPromptText|TestReplyPieces|TestPromptText|
  TestConversation|TestContext|TestCoreScriptBudget' ./internal/gateway
  ./internal/gui: ok.
- Whole ./internal/gui and ./internal/gateway (before the last rebase,
  same change): ok except TestMainWindowCorners/macOS_26_SDK (#3949) and
  TestCodexPoolSaysTheClientsVersion (#4807, fixed on main by 17f2387e1
  since).
- GOOS=linux go vet -tags nogui and GOOS=windows go vet of both: ok;
  gofmt -l clean.
- GUI, both engines, every language, 1100 and 420 wide: every file that
  reaches the context card (context-text, context-map, context-window,
  context-map-live, context-scroll-end, context-scripts, context-tune,
  ctx-route, compact-one-value, routing-ctx-*, routing-context-fold,
  routing-live-patch, routing-return-cost) plus i18n-keys: 514 of 518.
  The 4 reds are context-map's webkit 420px "two tiles overlap", red the
  same on origin/main f3335dd1b run alone (5 of 10 webkit there), #3488.
- The whole GUI suite is running on the Linux box (wsl-run) as this is
  pushed; mote had another session's full suite on it.

Broken by hand (each went red):
- maskReplyPieces skipped: TestReplyPiecesAreMasked (phone unmasked).
- ArchivedPromptText's masking skipped: TestArchivedPromptTextIsMasked
  and TestContextTextFromTheArchive (phone in the 200 body).
- ctxtext.js's "missing" reason dropped: context-text.test.cjs, 10 of 10
  chromium.

Not tried: a real archived request in the running app, the bucket
(WebDAV) archive with a real server, and a reply cut short by the
archive's size in a real call.
2026-11-03 13:34:35 +08:00

101 lines
3.0 KiB
Go

package gateway
// The reply beside the prompt's text (wangbinyq on Discord): the context
// window card's text view shows what the model answered too, read from the
// reply as the agent got it, from the Recent calls in memory or from the
// request archive's copy. It is masked as the prompt's text is, so the view
// shows no value the prompt's own text would have masked.
import (
"context"
"encoding/json"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/redact"
)
// PromptReply is a request's reply, piece by piece: the answer's text, its
// thinking and its tool calls.
type PromptReply struct {
ID int64 `json:"id"`
Pieces []PromptPiece `json:"pieces"`
// Cut: only the reply's start was kept, or it ended before it was whole
Cut bool `json:"cut,omitempty"`
// From: "memory" (the Recent calls) or "archive"
From string `json:"from"`
}
// ReplyBody is the reply to request route id as the Recent calls keep it,
// with the protocol it was written in; false once the call has left them.
func (s *Server) ReplyBody(id int64) (provider.Protocol, []byte, bool, bool) {
c, ok := s.RouteCall(id)
if !ok || c.ResponseBody == "" {
return "", nil, false, false
}
return c.From, []byte(c.ResponseBody), c.ResponseTruncated, true
}
// ReplyPieces reads a reply body (JSON or a stream) written in proto into
// pieces, masked as a prompt's text is; false when nothing in it can be
// read, or masking failed.
func ReplyPieces(proto provider.Protocol, body []byte, cut bool) ([]PromptPiece, bool, bool) {
parts, outCut := conversationOutput(proto, redact.ScrubJSON(body))
var pieces []PromptPiece
for _, p := range parts {
pc := PromptPiece{Role: "assistant", Text: p.Text}
switch p.Kind {
case "thinking":
pc.Role = "thinking"
case "tool_use":
pc.Role, pc.Name = "call", p.Name
}
pieces = append(pieces, pc)
}
if len(pieces) == 0 {
return nil, false, false
}
if !maskReplyPieces(pieces) {
return nil, false, false
}
for i := range pieces {
if pieces[i].Role == "call" {
pieces[i] = rawPiece("call", pieces[i].Name, []byte(pieces[i].Text)).read()
}
}
return pieces, cut || outCut, true
}
// maskReplyPieces masks the pieces' text in place, through the masker a
// request's body goes through: put as a conversation's assistant turns, so
// each text is masked as message text is.
func maskReplyPieces(pieces []PromptPiece) bool {
type msg struct {
Role string `json:"role"`
Content string `json:"content"`
}
in := struct {
Messages []msg `json:"messages"`
}{}
for _, pc := range pieces {
in.Messages = append(in.Messages, msg{Role: "assistant", Content: pc.Text})
}
body, err := json.Marshal(in)
if err != nil {
return false
}
masked := maskedCopy(context.Background(), body)
if masked == nil {
return false
}
var out struct {
Messages []msg `json:"messages"`
}
if json.Unmarshal(masked, &out) != nil || len(out.Messages) != len(pieces) {
return false
}
for i := range pieces {
pieces[i].Text = out.Messages[i].Content
}
return true
}