magpieMadeReasoning joined an item's summaries and its reasoning_text into the made summary. #2855's later rollout (lyufeng000, 10-31) has DeepSeek items whose content repeats their summary, so the made item would carry the words twice, and DeepSeek be handed them back twice (withReasoningText reads the summary). It now takes the text, else the summaries. It also ranged over item["summary"].([]any), a single-value assertion that panics on an item with no summary key; both reads now use the checked form. TestMadeReasoningSaysItsTextOnce. Reviewed and landed as one batch of 12 commits (gua's #4593, #3269, #3881, #2887, #3289, #2718, #2855, Edward4226's #2718 key-path sibling, and four review follow-ups). Run for the batch, whose last commit's tree is 5d58a75f3: - Full suite before the rebase (based307aa648, wsl-run magpie-desktop, go test -tags nogui -count=1 -timeout 45m ./...): 81 packages ok. Reds, all with issues and none in code this batch touches: internal/agent TestInstalledVersion, TestGrokInstalled, TestOmpDetectedWithPiDir (#4033, #3548, #3141); gateway TestUnroutedHeldReportSaved (#4300); gui TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestColdTurnsTaken (#4510); provider TestClaudeAccountServesModelsListedSinceItsFetch (#3174). Run alone at origin/main573b21f4b, TestGrokInstalled, TestOmpDetectedWithPiDir and TestSettingsSaveKeepsWhatItDoesNotSend are red there too. The other four pass there alone. - After the rebase onto origin/main573b21f4b, at this tree: internal/usage, update, gateway and provider ok. In internal/agent, the #4033/#3141 install-detection reds plus TestCopilotFolderFrom- AnIDEPluginIsNotTheCLI and TestZedAppPath. Those two are flaky the same way at origin/main (-count=10 alone: 4 and 5 of 10 red) and are already listed in #4033. The #3269 tests (TestCopilotLauncher*) pass 5 of 5. - Rebased onto origin/mainef63ef4a2. The #2855 commit met0b9727ec9(#2974) in responses.go: the streamed item's done now takes both, the upstream's own id (thinkUp) and the made-over item; and in gateway-routing.md main's paragraph is kept with #2855's clause. That matches gua's latest head (range-diff: context only). At tree d7c76c6fa: internal/gateway, provider, usage, redact and update ok; vet GOOS=linux -tags nogui ./internal/..., darwin and GOOS=windows for gateway and provider. - Rebased last onto origin/maind8f7d089f(5 commits: internal/agent and GUI files only). At tree0d4bfd8c8, the pushed one: internal/agent's TestCopilotLauncher*, TestClaude*, TestOwn* and TestVSCode* ok, -count=3; TestCopilotFolderFromAnIDEPluginIsNotTheCLI red once there, as it is at main (#4033). Vet of internal/agent: darwin, linux and windows. - gofmt -l clean. On mote: go vet darwin, GOOS=linux -tags nogui and GOOS=windows. - Broken by hand, each turning its guard red: the lift gate (TestReadingKeepsARefusalsRest, TestAutoVisionKeepsOffARefusedAccount); the named-time condition (TestNamedQuotaTimeReplacesAGuess); the filter-chain hop (WholeReply/glm-5.3-flash); Edward's aAt (TestKeyAllowanceElapsedResetStartsAgain); and the old magpieMadeReasoning, which panics in TestMadeReasoningSaysItsTextOnce/ no_summary. Every new test of gua's is red at based307aa648. Not tried: real OpenAI, DeepSeek, GLM or ChatGPT accounts; a real VS Code 1.144 install on Windows; a GUI run (no GUI files are touched).
207 lines
5.5 KiB
Go
207 lines
5.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
)
|
|
|
|
// OpenAI takes a reasoning item sent back to it only with its content
|
|
// empty ("Invalid 'input[n].content': array too long", #1411) and cannot
|
|
// open a seal that is not its own. A client stores what magpie hands it
|
|
// and replays it when the conversation goes back to OpenAI, on magpie or
|
|
// off it: an item another vendor wrote with its text in it would stick the
|
|
// conversation there (#2855, the reporter's rollout: 25 reasoning items a
|
|
// DeepSeek phase had left, each with its text in content parts and a
|
|
// 38-character encrypted_content of the vendor's own, against the
|
|
// thread's own GPT-phase items, which carry none). So an item that carries
|
|
// its text goes to the client as one magpie makes: the text a summary,
|
|
// nothing sealed.
|
|
|
|
// reasoningWithText is whether a Responses reasoning item carries its
|
|
// reasoning as content parts (reasoning_text), another vendor's shape:
|
|
// OpenAI's own carry summaries.
|
|
func reasoningWithText(item map[string]any) bool {
|
|
if t, _ := item["type"].(string); t != "reasoning" {
|
|
return false
|
|
}
|
|
parts, _ := item["content"].([]any)
|
|
return len(parts) > 0
|
|
}
|
|
|
|
// magpieMadeReasoning is the reasoning item magpie makes of one a
|
|
// Responses upstream wrote with its text in it, nil of one that carries no
|
|
// content, which goes as it came. Its text (else its summaries) becomes
|
|
// one summary of magpie's; its content and its seal go, the text kept, so
|
|
// an upstream that wants its reasoning back is sent it (withReasoningText).
|
|
// Its own id is kept, the events of a stream naming the item by it.
|
|
func magpieMadeReasoning(item map[string]any) map[string]any {
|
|
if !reasoningWithText(item) {
|
|
return nil
|
|
}
|
|
// the text, else the summaries: a vendor that writes both may write
|
|
// the same words twice (#2855's later rollout: each item's content
|
|
// the summary over again), and the text is what a vendor that wants
|
|
// its reasoning back reads
|
|
text := func(key, typ string) string {
|
|
var b strings.Builder
|
|
parts, _ := item[key].([]any)
|
|
for _, p := range parts {
|
|
pm, _ := p.(map[string]any)
|
|
if pm == nil {
|
|
continue
|
|
}
|
|
if t, _ := pm["type"].(string); typ != "" && t != typ {
|
|
continue
|
|
}
|
|
s, _ := pm["text"].(string)
|
|
b.WriteString(s)
|
|
}
|
|
return b.String()
|
|
}
|
|
said := text("content", "reasoning_text")
|
|
if said == "" {
|
|
said = text("summary", "")
|
|
}
|
|
id, _ := item["id"].(string)
|
|
if id == "" {
|
|
id = "rs_" + newID()
|
|
}
|
|
return map[string]any{"id": id, "type": "reasoning", "status": "completed",
|
|
"summary": []map[string]any{{"type": "summary_text", "text": said}}}
|
|
}
|
|
|
|
// magpieMadeReasoningRaw is the item's bytes, made over when the item
|
|
// carries its text; id, when not empty, is the id the item's events have
|
|
// already carried, which its done event must keep.
|
|
func magpieMadeReasoningRaw(raw json.RawMessage, id string) json.RawMessage {
|
|
var it map[string]any
|
|
if decodeJSON(raw, &it) != nil {
|
|
return raw
|
|
}
|
|
made := magpieMadeReasoning(it)
|
|
if made == nil {
|
|
return raw
|
|
}
|
|
if id != "" {
|
|
made["id"] = id
|
|
}
|
|
b, err := json.Marshal(made)
|
|
if err != nil {
|
|
return raw
|
|
}
|
|
return b
|
|
}
|
|
|
|
// reasoningTidy makes a Responses upstream's reasoning items that carry
|
|
// their text in it into the items magpie makes, in a reply relayed as it
|
|
// came, its events' sequence numbers and every other line kept as the
|
|
// upstream wrote them. A reply that isn't a stream is gone through whole.
|
|
type reasoningTidy struct {
|
|
sse bool
|
|
buf []byte
|
|
}
|
|
|
|
func (t *reasoningTidy) write(b []byte) []byte {
|
|
t.buf = append(t.buf, b...)
|
|
if !t.sse {
|
|
return nil
|
|
}
|
|
i := bytes.LastIndexByte(t.buf, '\n')
|
|
if i < 0 {
|
|
return nil
|
|
}
|
|
out := t.lines(t.buf[:i+1])
|
|
t.buf = append(t.buf[:0], t.buf[i+1:]...)
|
|
return out
|
|
}
|
|
|
|
func (t *reasoningTidy) flush() []byte {
|
|
b := t.buf
|
|
t.buf = nil
|
|
if !t.sse {
|
|
if out, ok := t.event(b); ok {
|
|
return out
|
|
}
|
|
return b
|
|
}
|
|
return t.lines(b)
|
|
}
|
|
|
|
func (t *reasoningTidy) lines(b []byte) []byte {
|
|
if !bytes.Contains(b, []byte(`"reasoning"`)) {
|
|
return append([]byte(nil), b...)
|
|
}
|
|
var out []byte
|
|
for len(b) > 0 {
|
|
line := b
|
|
if i := bytes.IndexByte(b, '\n'); i >= 0 {
|
|
line, b = b[:i+1], b[i+1:]
|
|
} else {
|
|
b = nil
|
|
}
|
|
body := bytes.TrimRight(line, "\r\n")
|
|
data, ok := bytes.CutPrefix(body, []byte("data:"))
|
|
if !ok || !bytes.Contains(data, []byte(`"reasoning"`)) {
|
|
out = append(out, line...)
|
|
continue
|
|
}
|
|
nb, ok := t.event(data)
|
|
if !ok {
|
|
out = append(out, line...)
|
|
continue
|
|
}
|
|
out = append(append(append(out, "data: "...), nb...), line[len(body):]...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// event is an SSE event's data, or a whole reply, with its reasoning items
|
|
// that carry their text made into magpie's, and whether one was.
|
|
func (t *reasoningTidy) event(data []byte) ([]byte, bool) {
|
|
dec := json.NewDecoder(bytes.NewReader(data))
|
|
dec.UseNumber()
|
|
var ev map[string]any
|
|
if dec.Decode(&ev) != nil || ev == nil {
|
|
return nil, false
|
|
}
|
|
changed := madeOver(ev["item"])
|
|
res, _ := ev["response"].(map[string]any)
|
|
if res == nil && ev["object"] == "response" {
|
|
res = ev
|
|
}
|
|
if res != nil {
|
|
out, _ := res["output"].([]any)
|
|
for _, it := range out {
|
|
if madeOver(it) {
|
|
changed = true
|
|
}
|
|
}
|
|
}
|
|
if !changed {
|
|
return nil, false
|
|
}
|
|
nb, err := marshalPlain(ev)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
return nb, true
|
|
}
|
|
|
|
// madeOver makes item, a reasoning item with its text in it, one of
|
|
// magpie's, in place, and reports whether it was.
|
|
func madeOver(item any) bool {
|
|
im, _ := item.(map[string]any)
|
|
made := magpieMadeReasoning(im)
|
|
if made == nil {
|
|
return false
|
|
}
|
|
for k := range im {
|
|
delete(im, k)
|
|
}
|
|
for k, v := range made {
|
|
im[k] = v
|
|
}
|
|
return true
|
|
}
|