idTidy and phaseTidy each copied every event of a relayed Responses stream twice, once out of their buffer and again onto an output grown event by event, and dropped their buffer's bytes at each write so the next write took new ones. eventData copied each event's data even when it is one line, as nearly every upstream's is, and phaseTidy decoded a reply's whole last event into a map to find no message without a phase. - tidyEvents goes through a write's whole events into one output sized for them; an event that goes as it came is copied once, and the part of an event still coming moves to the buffer's front. - eventData gives a one-line event's data as ev's own bytes; its callers only read it. - phaseTidy.ended reads the output in place (anyUnphased, which agrees with unphased item by item) and decodes only when a message needs a phase. Measured with BenchmarkZZCodexStream (a 680 KB Codex /v1/responses stream through serve, kept out of the tree): 14.16 -> 12.53 MB/op, ~53,450 -> ~49,700 allocs/op. Run at this head: - go test ./internal/gateway -timeout 45m: ok (439s, at 848b3bf7e rebased on4b9694c34) - New tests: TestAnyUnphasedAgreesWithUnphased (null, number and missing phase and role, a non-list output), TestEventDataJoinsAsSSE, TestTidiesPassAStreamSplitAnywhere (1, 7, 64 bytes and whole, the caller's buffer cleared after each write). Broken by hand: moving nothing to the buffer's front turned the split test red, and reading a null role as an assistant's turned the agreement test red; restored, green. - gofmt -l clean; go vet ./internal/gateway on darwin, and GOOS=linux and GOOS=windows with -tags nogui: ok - Real APIKEY.FAN account (deepseek-flash over its /v1/responses, so both tidies run; sandbox HOME with only that provider copied): five Codex-shaped streams, 5 of 5 answered "ok" at1859da57band at this head. - Those five real replies, the same with every phase taken out, and a real OpenAI billing_not_active reply (error then response.failed), replayed to both builds in 7-byte, 4 KB and whole chunks: the client's bytes are identical between the builds in every case but the error reply at 7 bytes, where both builds cut the stream at a varying point after the error event (old: 3633 and 4757 bytes; new: 3500, 3857 and 4757), as before this change. Not tried: a real Copilot account (the case idTidy is for); the OpenAI key's account is not active.
312 lines
8.3 KiB
Go
312 lines
8.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// An assistant message in a Responses reply says its phase, as OpenAI's
|
|
// own models' do: "commentary" for what the model said on the way (before
|
|
// a call), "final_answer" for the answer that ends the turn. Codex folds a
|
|
// finished turn's commentary and calls under "Worked for …" and leaves the
|
|
// final answer out, and reads a message with no phase as an answer, so a
|
|
// model magpie translates had every progress line left unfolded (#2437).
|
|
//
|
|
// A message's phase is known only from what comes after it: a call, a
|
|
// search or another message makes it commentary; the reply ending with
|
|
// nothing but reasoning after it makes it the final answer. A reply cut
|
|
// short (incomplete, failed, cut mid-stream) guesses no final answer, and
|
|
// a phase the upstream wrote is kept.
|
|
|
|
const (
|
|
phaseCommentary = "commentary"
|
|
phaseFinal = "final_answer"
|
|
)
|
|
|
|
// heldMost is how many events may wait behind a message whose phase isn't
|
|
// known yet; past it they go on, and the message without a phase.
|
|
const heldMost = 512
|
|
|
|
// unphased reports whether an output item is an assistant message with no
|
|
// phase of its own.
|
|
func unphased(it map[string]any) bool {
|
|
if it == nil || it["type"] != "message" {
|
|
return false
|
|
}
|
|
if r, ok := it["role"]; ok && r != "assistant" {
|
|
return false
|
|
}
|
|
p, _ := it["phase"].(string)
|
|
return p == ""
|
|
}
|
|
|
|
// anyUnphased reports whether a reply's output has an item unphased
|
|
// would give a phase, read in place: a reply whose messages all have one
|
|
// (OpenAI's own) goes as it came without being decoded whole.
|
|
func anyUnphased(output gjson.Result) bool {
|
|
if !output.IsArray() {
|
|
return false
|
|
}
|
|
found := false
|
|
output.ForEach(func(_, it gjson.Result) bool {
|
|
typ, role, phase := it.Get("type"), it.Get("role"), it.Get("phase")
|
|
if typ.Type != gjson.String || typ.Str != "message" {
|
|
return true
|
|
}
|
|
if role.Exists() && (role.Type != gjson.String || role.Str != "assistant") {
|
|
return true
|
|
}
|
|
found = phase.Type != gjson.String || phase.Str == ""
|
|
return !found
|
|
})
|
|
return found
|
|
}
|
|
|
|
// phaseOutput gives each assistant message in a reply's output its phase,
|
|
// and reports whether it gave any. done is whether the reply ended
|
|
// completed: only then is the last message the final answer.
|
|
func phaseOutput(output []any, done bool) bool {
|
|
last := -1 // the last item that isn't reasoning
|
|
for i, it := range output {
|
|
if m, _ := it.(map[string]any); m != nil && m["type"] != "reasoning" {
|
|
last = i
|
|
}
|
|
}
|
|
changed := false
|
|
for i, it := range output {
|
|
m, _ := it.(map[string]any)
|
|
if !unphased(m) {
|
|
continue
|
|
}
|
|
switch {
|
|
case i < last:
|
|
m["phase"] = phaseCommentary
|
|
case i == last && done:
|
|
m["phase"] = phaseFinal
|
|
default:
|
|
continue
|
|
}
|
|
changed = true
|
|
}
|
|
return changed
|
|
}
|
|
|
|
// phaseTidy gives, in a relayed Responses reply, its assistant messages a
|
|
// phase where the upstream wrote none. In a stream a message's
|
|
// output_item.done waits, with what comes after it, until the next item
|
|
// that isn't reasoning (commentary) or the reply's end (final answer); a
|
|
// reply that isn't a stream is held whole and gone through at the end.
|
|
type phaseTidy struct {
|
|
sse bool
|
|
buf []byte // a stream's part of an event, or a whole reply
|
|
held [][]byte // events waiting on the first's message's phase
|
|
}
|
|
|
|
func (t *phaseTidy) write(b []byte) []byte {
|
|
t.buf = append(t.buf, b...)
|
|
if !t.sse {
|
|
return nil
|
|
}
|
|
out, rest := tidyEvents(t.buf, t.event)
|
|
t.buf = rest
|
|
return out
|
|
}
|
|
|
|
// tidyEvents is each whole event in buf through event, and buf's part of
|
|
// an event that hasn't ended, moved to buf's front so the next write
|
|
// appends into the same bytes. What event gives may be its ev, a slice of
|
|
// buf, so it is copied out before buf is written again.
|
|
func tidyEvents(buf []byte, event func(ev []byte) []byte) (out, rest []byte) {
|
|
at := 0
|
|
for {
|
|
n := eventEnd(buf[at:])
|
|
if n < 0 {
|
|
break
|
|
}
|
|
if out == nil {
|
|
out = make([]byte, 0, len(buf))
|
|
}
|
|
out = append(out, event(buf[at:at+n])...)
|
|
at += n
|
|
}
|
|
return out, append(buf[:0], buf[at:]...)
|
|
}
|
|
|
|
func (t *phaseTidy) flush() []byte {
|
|
b := t.buf
|
|
t.buf = nil
|
|
if !t.sse {
|
|
if out, ok := phaseWhole(b); ok {
|
|
return out
|
|
}
|
|
return b
|
|
}
|
|
// a stream that ended with a message still waiting: nothing says it
|
|
// ended the turn, so it goes as it came
|
|
out := t.release("")
|
|
return append(out, b...)
|
|
}
|
|
|
|
// eventData is an event's data, joined as SSE joins it. An event of one
|
|
// data line, as nearly every upstream's is, gets ev's own bytes, so what
|
|
// it gives is only read.
|
|
func eventData(ev []byte) []byte {
|
|
var data, first []byte
|
|
n := 0
|
|
for rest := ev; len(rest) > 0; {
|
|
var line []byte
|
|
line, rest, _ = bytes.Cut(rest, []byte("\n"))
|
|
d, ok := bytes.CutPrefix(bytes.TrimRight(line, "\r"), []byte("data:"))
|
|
if !ok {
|
|
continue
|
|
}
|
|
d = bytes.TrimPrefix(d, []byte(" "))
|
|
switch n {
|
|
case 0:
|
|
first = d
|
|
case 1:
|
|
data = append(append(append([]byte(nil), first...), '\n'), d...)
|
|
default:
|
|
data = append(append(data, '\n'), d...)
|
|
}
|
|
n++
|
|
}
|
|
if n == 1 {
|
|
return first
|
|
}
|
|
return data
|
|
}
|
|
|
|
// withData is ev with its data lines put back as data.
|
|
func withData(ev, data []byte) []byte {
|
|
var out []byte
|
|
put := false
|
|
for _, line := range bytes.SplitAfter(ev, []byte("\n")) {
|
|
body := bytes.TrimRight(line, "\r\n")
|
|
if bytes.HasPrefix(body, []byte("data:")) {
|
|
if !put {
|
|
out = append(append(append(out, "data: "...), data...), line[len(body):]...)
|
|
put = true
|
|
}
|
|
continue
|
|
}
|
|
out = append(out, line...)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (t *phaseTidy) event(ev []byte) []byte {
|
|
data := eventData(ev)
|
|
typ := gjson.GetBytes(data, "type").String()
|
|
if t.held != nil {
|
|
switch typ {
|
|
case "response.output_item.added":
|
|
if gjson.GetBytes(data, "item.type").String() == "reasoning" {
|
|
return t.hold(ev)
|
|
}
|
|
return append(t.release(phaseCommentary), t.event(ev)...)
|
|
case "response.completed":
|
|
return append(t.release(phaseFinal), t.ended(ev, data, true)...)
|
|
case "response.incomplete", "response.failed", "error":
|
|
return append(t.release(""), t.ended(ev, data, false)...)
|
|
}
|
|
return t.hold(ev)
|
|
}
|
|
switch typ {
|
|
case "response.output_item.done":
|
|
it := gjson.GetBytes(data, "item")
|
|
if it.Get("type").String() == "message" && it.Get("role").String() != "user" && it.Get("phase").String() == "" {
|
|
t.held = [][]byte{append([]byte(nil), ev...)}
|
|
return nil
|
|
}
|
|
case "response.completed":
|
|
return t.ended(ev, data, true)
|
|
case "response.incomplete":
|
|
return t.ended(ev, data, false)
|
|
}
|
|
return ev
|
|
}
|
|
|
|
func (t *phaseTidy) hold(ev []byte) []byte {
|
|
t.held = append(t.held, append([]byte(nil), ev...))
|
|
if len(t.held) > heldMost {
|
|
return t.release("")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// release is the waiting events, the message among them given phase (or
|
|
// left as it came, for "").
|
|
func (t *phaseTidy) release(phase string) []byte {
|
|
held := t.held
|
|
t.held = nil
|
|
if len(held) == 0 {
|
|
return nil
|
|
}
|
|
if phase != "" {
|
|
if nb, ok := phasedEvent(eventData(held[0]), phase); ok {
|
|
held[0] = withData(held[0], nb)
|
|
}
|
|
}
|
|
return bytes.Join(held, nil)
|
|
}
|
|
|
|
// ended is the reply's last event, its output's messages given their phase
|
|
// as the stream gave them.
|
|
func (t *phaseTidy) ended(ev, data []byte, done bool) []byte {
|
|
if !anyUnphased(gjson.GetBytes(data, "response.output")) {
|
|
return ev
|
|
}
|
|
var e map[string]any
|
|
if decodeNumbers(data, &e) != nil {
|
|
return ev
|
|
}
|
|
res, _ := e["response"].(map[string]any)
|
|
out, _ := res["output"].([]any)
|
|
if !phaseOutput(out, done) {
|
|
return ev
|
|
}
|
|
nb, err := marshalPlain(e)
|
|
if err != nil {
|
|
return ev
|
|
}
|
|
return withData(ev, nb)
|
|
}
|
|
|
|
// phasedEvent is an output_item.done event with its message given phase.
|
|
func phasedEvent(data []byte, phase string) ([]byte, bool) {
|
|
var e map[string]any
|
|
if decodeNumbers(data, &e) != nil {
|
|
return nil, false
|
|
}
|
|
it, _ := e["item"].(map[string]any)
|
|
if !unphased(it) {
|
|
return nil, false
|
|
}
|
|
it["phase"] = phase
|
|
nb, err := marshalPlain(e)
|
|
return nb, err == nil
|
|
}
|
|
|
|
// phaseWhole is a whole Responses reply with its messages' phases.
|
|
func phaseWhole(b []byte) ([]byte, bool) {
|
|
var res map[string]any
|
|
if decodeNumbers(b, &res) != nil || res == nil || res["object"] != "response" {
|
|
return nil, false
|
|
}
|
|
out, _ := res["output"].([]any)
|
|
if !phaseOutput(out, res["status"] == "completed") {
|
|
return nil, false
|
|
}
|
|
nb, err := marshalPlain(res)
|
|
return nb, err == nil
|
|
}
|
|
|
|
func decodeNumbers(b []byte, v any) error {
|
|
dec := json.NewDecoder(bytes.NewReader(b))
|
|
dec.UseNumber()
|
|
return dec.Decode(v)
|
|
}
|