Files
magpie/internal/gateway/responses.go
T
yetone a97643bcc8 gateway: a translated or relayed stream keeps its client alive while its provider is (#436, congee949: Chat → Responses dropped the upstream's ": keepalive" and Codex hit "idle timeout waiting for SSE" on a live connection) — a reply magpie translates was read event by event and written only when an event had something for the client, so a provider's SSE keepalives, and events with nothing to translate, reached no one, and a reply that thought quietly for five minutes ended in Codex's idle timeout and a retry. Now, whenever the provider is heard from and the client has heard nothing for a second, magpie sends the client a keepalive in its own protocol: response.in_progress again on Responses (Codex's timeout counts events, never a comment — a sandboxed codex 0.159.2 on a fake provider sending only comments timed out, one sending response.in_progress didn't), ping on Anthropic (as Anthropic does; Claude Code's watchdog counts it), an SSE comment on Chat Completions, nothing on Gemini (Google's SDK can't read past one). A provider that sends nothing at all is not covered for, so a dead connection still ends at the client's timeout. A reply relayed from the Claude subscription's bridge, Kiro, Cursor, Devin, search, Qoder, Zed or Command Code is kept alive every 15s while it goes quiet, for up to 5 minutes since its last event. Native passthrough is unchanged, and prompts and headers are unchanged. The routing trace no longer drops a request still going for the 60 finished after it: the oldest finished ones go first, unless more than 120 are going at once. TestTranslatedStreamKeepsClientAlive (the issue's repro: 20ms keepalives, 150ms idle timeout; Chat → Responses, Chat → Anthropic, Responses → Chat), TestRelayKeepsClientAlive and TestTraceKeepsInflight fail without the change; TestTranslatedSilenceStillTimesOut keeps a silent provider uncovered. In a sandbox CODEX_HOME, codex exec through the gateway on a provider sending 6s of keepalives with stream_idle_timeout_ms 2500 answers, and without the change fails with idle timeout waiting for SSE. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.
2026-10-01 22:36:36 +08:00

905 lines
31 KiB
Go

package gateway
import (
"encoding/json"
"fmt"
"slices"
"strings"
"time"
"github.com/yetone/magpie/internal/provider"
)
// ---- OpenAI Responses -------------------------------------------------------
type rItem struct {
Type string `json:"type,omitempty"`
Role string `json:"role,omitempty"`
Content json.RawMessage `json:"content,omitempty"`
// function_call / function_call_output
ID string `json:"id,omitempty"`
CallID string `json:"call_id,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Arguments rArgs `json:"arguments,omitempty"`
Output json.RawMessage `json:"output,omitempty"`
Status string `json:"status,omitempty"`
// tool_search_output: the tools Codex's search found, which the model
// may call from then on
Tools []rTool `json:"tools,omitempty"`
// reasoning
Summary []rText `json:"summary,omitempty"`
EncryptedContent string `json:"encrypted_content,omitempty"`
// web_search_call
Action *struct {
Query string `json:"query"`
Sources []struct {
URL string `json:"url"`
} `json:"sources"`
} `json:"action,omitempty"`
}
// rArgs is a call's arguments: a JSON string on a function_call, an object
// on Codex's tool_search_call.
type rArgs string
func (a *rArgs) UnmarshalJSON(b []byte) error {
var s string
if json.Unmarshal(b, &s) == nil {
*a = rArgs(s)
} else if string(b) != "null" {
*a = rArgs(b)
}
return nil
}
type rText struct {
Type string `json:"type"`
Text string `json:"text"`
}
type rTool struct {
Type string `json:"type"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters json.RawMessage `json:"parameters,omitempty"`
Strict *bool `json:"strict,omitempty"`
Tools []rTool `json:"tools,omitempty"` // a namespace's
Execution string `json:"execution,omitempty"`
}
// toolSearch is Codex's tool search. With it Codex names its MCP tools (the
// ChatGPT apps' among them) and its sub-agent tools only in the search's
// description, and hands the model the ones it searched for in a
// tool_search_output, instead of every schema on every request (#258).
// Codex runs the search itself ("execution": "client"): a model magpie
// translates for is offered it as the function it is, and its call goes
// back to Codex as the tool_search_call Codex runs.
const toolSearch = "tool_search"
// searchFound is what a model reads of a tool search's result: the tools it
// may call now, by the names it is offered them under.
func searchFound(tools []rTool) string {
var names []string
for _, t := range tools {
switch t.Type {
case "function":
names = append(names, t.Name)
case "namespace":
for _, nt := range t.Tools {
if nt.Type == "function" {
names = append(names, flatName(t.Name, nt.Name))
}
}
}
}
if len(names) == 0 {
return "No tools matched the search."
}
return "These tools are now available to call: " + strings.Join(names, ", ")
}
// flatName is the name a namespaced tool is offered to a model under,
// namespace__name, the same a Grok subscription is offered it under.
func flatName(namespace, name string) string {
return provider.FlatName(namespace, name)
}
type rRequest struct {
Model string `json:"model"`
Instructions string `json:"instructions,omitempty"`
Input json.RawMessage `json:"input"`
Tools []rTool `json:"tools,omitempty"`
ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
ServiceTier string `json:"service_tier,omitempty"`
PromptCacheKey string `json:"prompt_cache_key,omitempty"`
Include []string `json:"include,omitempty"`
ClientMetadata json.RawMessage `json:"client_metadata,omitempty"`
Text json.RawMessage `json:"text,omitempty"`
Reasoning *struct {
Effort string `json:"effort,omitempty"`
Summary string `json:"summary,omitempty"`
} `json:"reasoning,omitempty"`
}
// sentRaw is a raw field the client sent, unless it sent null.
func sentRaw(v json.RawMessage) json.RawMessage {
if t := strings.TrimSpace(string(v)); t == "" || t == "null" {
return nil
}
return v
}
func parseResponses(body []byte) (*Request, error) {
// Responses Lite's tools, sent as the first input item (#350)
body = liftAdditionalTools(body)
var q rRequest
if err := json.Unmarshal(body, &q); err != nil {
return nil, fmt.Errorf("invalid request: %v", err)
}
r := &Request{Model: q.Model, System: q.Instructions, MaxTokens: q.MaxOutputTokens, Temp: q.Temperature,
TopP: q.TopP, Stream: q.Stream, Parallel: q.ParallelToolCalls, Fast: q.ServiceTier == "priority", CacheKey: q.PromptCacheKey, Include: q.Include,
ClientMetadata: sentRaw(q.ClientMetadata), Text: sentRaw(q.Text)}
if q.Reasoning != nil {
r.Effort = effortOf(q.Reasoning.Effort)
r.Thinking = true
r.ThinkOff = strings.EqualFold(strings.TrimSpace(q.Reasoning.Effort), "none")
}
var found []rTool // what Codex's tool searches found
var s string
if json.Unmarshal(q.Input, &s) == nil {
r.Messages = append(r.Messages, Message{Role: "user", Parts: []Part{{Kind: Text, Text: s}}})
} else {
var items []rItem
if err := json.Unmarshal(q.Input, &items); err != nil {
return nil, fmt.Errorf("invalid input: %v", err)
}
for _, it := range items {
switch {
case it.Type == "message" || (it.Type == "" && it.Role != ""):
role := "user"
if it.Role == "assistant" {
role = "assistant"
}
parts := responsesParts(it.Content)
if it.Role == "system" || it.Role == "developer" {
if t := text(parts); t != "" {
if r.System != "" {
r.System += "\n\n"
}
r.System += t
}
continue
}
r.Messages = append(r.Messages, Message{Role: role, Parts: parts})
case it.Type == "agent_message":
// MultiAgentV2 hands a subagent its task, and agents their
// messages to each other, as this item: what it says is the
// user's turn for the agent that receives it.
if parts := responsesParts(it.Content); len(parts) > 0 {
r.Messages = append(r.Messages, Message{Role: "user", Parts: parts})
}
case it.Type == "function_call":
name := it.Name
if it.Namespace != "" {
name = flatName(it.Namespace, it.Name)
}
r.Messages = append(r.Messages, Message{Role: "assistant", Parts: []Part{{Kind: ToolCall, ID: it.CallID, Name: name, Args: parseArgs(string(it.Arguments))}}})
case it.Type == "tool_search_call":
r.Messages = append(r.Messages, Message{Role: "assistant", Parts: []Part{{Kind: ToolCall, ID: it.CallID, Name: toolSearch, Args: parseArgs(string(it.Arguments))}}})
case it.Type == "tool_search_output":
found = append(found, it.Tools...)
r.Messages = append(r.Messages, Message{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: it.CallID, Text: searchFound(it.Tools)}}})
case it.Type == "function_call_output":
out, images := toolOutput(it.Output)
r.Messages = append(r.Messages, Message{Role: "user", Parts: []Part{{Kind: ToolResult, CallID: it.CallID, Text: out, Images: images}}})
case it.Type == "reasoning":
// the reasoning itself when the item carries it, else its
// summary (all magpie gives a client of a translated reply)
var b strings.Builder
var content []rText
_ = json.Unmarshal(it.Content, &content)
for _, c := range content {
if c.Type == "reasoning_text" {
b.WriteString(c.Text)
}
}
if b.Len() == 0 {
for _, s := range it.Summary {
b.WriteString(s.Text)
}
}
if b.Len() > 0 {
r.Messages = append(r.Messages, Message{Role: "assistant", Parts: []Part{{Kind: Thinking, Text: b.String()}}})
}
}
}
}
r.Messages = mergeTurns(r.Messages)
sent := len(q.Tools)
offer := func(i int, t Tool, ns nsTool) {
// a tool searched for twice, or sent as well, is offered once
if i >= sent && slices.ContainsFunc(r.Tools, func(o Tool) bool { return o.Name == t.Name }) {
return
}
if ns != (nsTool{}) {
if r.Namespaced == nil {
r.Namespaced = map[string]nsTool{}
}
r.Namespaced[t.Name] = ns
}
r.Tools = append(r.Tools, t)
}
// the tools Codex's searches found are offered after those it sent, as
// Codex does not send them again
for i, t := range append(q.Tools, found...) {
if strings.HasPrefix(t.Type, "web_search") {
r.WebSearch = true
}
switch t.Type {
case "function":
offer(i, Tool{Name: t.Name, Description: t.Description, Schema: t.Parameters, Strict: t.Strict != nil && *t.Strict}, nsTool{})
case "namespace":
// offered flat, as few models know namespaces; a call is given
// its namespace back on the way out
for _, nt := range t.Tools {
if nt.Type != "function" {
continue
}
flat := flatName(t.Name, nt.Name)
offer(i, Tool{Name: flat, Description: nt.Description, Schema: nt.Parameters, Strict: nt.Strict != nil && *nt.Strict}, nsTool{Namespace: t.Name, Name: nt.Name})
}
case toolSearch:
if t.Execution == "client" {
offer(i, Tool{Name: toolSearch, Description: t.Description, Schema: t.Parameters}, nsTool{Search: true})
}
}
}
var tc string
if json.Unmarshal(q.ToolChoice, &tc) == nil {
r.ToolChoice = tc
} else {
var o struct {
Type string `json:"type"`
Name string `json:"name"`
Mode string `json:"mode"`
Tools []struct {
Type string `json:"type"`
Name string `json:"name"`
Namespace string `json:"namespace"`
} `json:"tools"`
}
if json.Unmarshal(q.ToolChoice, &o) == nil {
switch o.Type {
case "allowed_tools":
if o.Mode != "auto" && o.Mode != "required" {
return nil, fmt.Errorf("invalid allowed_tools mode %q", o.Mode)
}
r.ToolChoice = o.Mode
allowed := make(map[string]bool, len(o.Tools))
webSearch := false
for _, tool := range o.Tools {
switch {
case tool.Type == "function" && tool.Name != "":
if tool.Namespace != "" {
allowed[flatName(tool.Namespace, tool.Name)] = true
} else {
allowed[tool.Name] = true
}
case strings.HasPrefix(tool.Type, "web_search"):
webSearch = true
}
}
r.WebSearch = r.WebSearch && webSearch
var tools []Tool
for _, tool := range r.Tools {
if allowed[tool.Name] {
tools = append(tools, tool)
}
}
r.Tools = tools
for name := range r.Namespaced {
if !allowed[name] {
delete(r.Namespaced, name)
}
}
case "function":
if o.Name != "" {
r.ToolChoice = "name:" + o.Name
}
}
}
}
return r, nil
}
// mergeTurns joins consecutive messages of the same role, since the
// Responses API splits an assistant turn into one item per part.
func mergeTurns(msgs []Message) []Message {
var out []Message
for _, m := range msgs {
if n := len(out); n > 0 && out[n-1].Role == m.Role {
out[n-1].Parts = append(out[n-1].Parts, m.Parts...)
continue
}
out = append(out, m)
}
return out
}
func responsesParts(raw json.RawMessage) []Part {
var s string
if json.Unmarshal(raw, &s) == nil {
if s == "" {
return nil
}
return []Part{{Kind: Text, Text: s}}
}
var items []struct {
Type string `json:"type"`
Text string `json:"text"`
ImageURL string `json:"image_url"`
}
json.Unmarshal(raw, &items)
var out []Part
for _, it := range items {
switch it.Type {
case "input_text", "output_text", "text":
out = append(out, Part{Kind: Text, Text: it.Text})
case "input_image":
if it.ImageURL != "" {
out = append(out, imagePart(it.ImageURL))
}
}
}
return out
}
// buildResponses renders a request for a Responses upstream.
func buildResponses(r *Request, model, host string, rejectTemp bool) []byte {
// A turn's reasoning goes back as a reasoning item, as a model that
// thinks between tool calls wants it (DeepSeek: "The reasoning_text in
// the thinking mode must be passed back", #388). Only a DeepSeek model
// gets it: OpenAI's and those in front of it read only their own
// sealed reasoning, and may refuse an item without it.
replay := strings.Contains(strings.ToLower(model), "deepseek") &&
!slices.Contains([]string{"chatgpt.com", "api.openai.com", "api.x.ai", "api.githubcopilot.com"}, host) &&
!strings.HasSuffix(host, ".openai.azure.com")
var input []map[string]any
for _, m := range r.Messages {
var content []map[string]any
flushMsg := func() {
if len(content) == 0 {
return
}
input = append(input, map[string]any{"type": "message", "role": m.Role, "content": content})
content = nil
}
for _, p := range m.Parts {
switch p.Kind {
case Text:
t := "input_text"
if m.Role == "assistant" {
t = "output_text"
}
content = append(content, map[string]any{"type": t, "text": p.Text})
case File:
t := "input_text"
if m.Role == "assistant" {
t = "output_text"
}
content = append(content, map[string]any{"type": t, "text": attachmentText(p)})
case Image:
if m.Role != "assistant" {
content = append(content, map[string]any{"type": "input_image", "image_url": dataURL(p)})
}
case Thinking:
if replay && p.Text != "" {
flushMsg()
input = append(input, map[string]any{"type": "reasoning", "summary": []any{},
"content": []map[string]any{{"type": "reasoning_text", "text": p.Text}}})
}
case ToolCall:
flushMsg()
id := p.ID
if id == "" {
id = "call_" + newID()
}
input = append(input, map[string]any{"type": "function_call", "call_id": id, "name": p.Name, "arguments": argsString(p)})
case ToolResult:
flushMsg()
var output any = p.Text
if len(p.Images) > 0 {
// an output can be a list of text and images
var items []map[string]any
if strings.TrimSpace(p.Text) != "" {
items = append(items, map[string]any{"type": "input_text", "text": p.Text})
}
for _, im := range p.Images {
items = append(items, map[string]any{"type": "input_image", "image_url": dataURL(im)})
}
output = items
}
input = append(input, map[string]any{"type": "function_call_output", "call_id": p.CallID, "output": output})
}
}
flushMsg()
}
if input == nil {
input = []map[string]any{}
}
out := map[string]any{"model": model, "input": input, "stream": r.Stream, "store": false}
if len(r.ClientMetadata) > 0 {
out["client_metadata"] = r.ClientMetadata
}
if len(r.Text) > 0 {
out["text"] = r.Text
}
if r.CacheKey != "" {
out["prompt_cache_key"] = r.CacheKey
}
if r.System != "" {
out["instructions"] = r.System
}
if r.MaxTokens > 0 {
out["max_output_tokens"] = r.MaxTokens
}
if !rejectTemp {
if r.Temp != nil {
out["temperature"] = *r.Temp
}
if r.TopP != nil {
out["top_p"] = *r.TopP
}
}
// Fast goes to OpenAI's own backends; another's may not know the tier
if r.Fast && (host == "chatgpt.com" || host == "api.openai.com") {
out["service_tier"] = "priority"
}
if r.Effort != "" {
out["reasoning"] = map[string]any{"effort": r.Effort, "summary": "auto"}
} else if r.Thinking {
out["reasoning"] = map[string]any{"summary": "auto"}
}
// the client's include goes on as the request would have without
// magpie. Sealed reasoning is asked for only with reasoning, as Codex
// asks for it: OpenAI refuses it of a model that doesn't reason. What
// comes back sealed goes no further than magpie, and the input's sealed
// reasoning isn't sent on this way, so no account or vendor is handed
// another's to refuse (withoutRefused, sealedKinds, on a relay).
var include []string
for _, v := range r.Include {
if v == "" || slices.Contains(include, v) || (v == "reasoning.encrypted_content" && out["reasoning"] == nil) {
continue
}
include = append(include, v)
}
if len(r.Tools) > 0 || r.WebSearch {
var tools []map[string]any
for _, t := range r.Tools {
// strict is said, as Codex says it: left out, the ChatGPT
// backend holds the schema to strict mode's rules and refuses a
// pattern with a lookaround (MiniMax Code's path, #383)
tool := map[string]any{"type": "function", "name": t.Name, "description": t.Description, "strict": t.Strict}
if len(t.Schema) > 0 {
tool["parameters"] = t.Schema
}
tools = append(tools, tool)
}
if r.WebSearch {
tools = append(tools, map[string]any{"type": "web_search"})
// the pages it found, which a client of another protocol is
// told of (OpenAI's option; xAI's API isn't known to take it)
if host != "api.x.ai" && !slices.Contains(include, "web_search_call.action.sources") {
include = append(include, "web_search_call.action.sources")
}
}
out["tools"] = tools
switch {
case r.ToolChoice == "auto" || r.ToolChoice == "none" || r.ToolChoice == "required":
out["tool_choice"] = r.ToolChoice
case strings.HasPrefix(r.ToolChoice, "name:"):
out["tool_choice"] = map[string]any{"type": "function", "name": strings.TrimPrefix(r.ToolChoice, "name:")}
}
if r.Parallel != nil {
out["parallel_tool_calls"] = *r.Parallel
}
}
if len(include) > 0 {
out["include"] = include
}
b, _ := json.Marshal(out)
return b
}
type rUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
TotalTokens int `json:"total_tokens"`
InputTokensDetails struct {
CachedTokens int `json:"cached_tokens"`
} `json:"input_tokens_details"`
OutputTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens"`
} `json:"output_tokens_details"`
}
func (u rUsage) usage() Usage {
return Usage{Input: u.InputTokens - u.InputTokensDetails.CachedTokens, Output: u.OutputTokens,
CacheRead: u.InputTokensDetails.CachedTokens, Reasoning: u.OutputTokensDetails.ReasoningTokens}
}
func (u Usage) responses() map[string]any {
in := u.prompt()
return map[string]any{"input_tokens": in, "output_tokens": u.Output, "total_tokens": in + u.Output,
"input_tokens_details": map[string]any{"cached_tokens": u.CacheRead},
"output_tokens_details": map[string]any{"reasoning_tokens": u.Reasoning}}
}
// responsesDecoder turns a Responses stream into events.
type responsesDecoder struct {
started bool
argsSeen bool // arguments of the open function call arrived as deltas
called bool // a function call was streamed
}
func (d *responsesDecoder) decode(data string, emit func(Event)) error {
var ev struct {
Type string `json:"type"`
Delta string `json:"delta"`
Item rItem `json:"item"`
Response struct {
ID string `json:"id"`
Model string `json:"model"`
Status string `json:"status"`
Usage rUsage `json:"usage"`
IncompleteDetails *struct {
Reason string `json:"reason"`
} `json:"incomplete_details"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
Output []rItem `json:"output"`
} `json:"response"`
Error *struct {
Message string `json:"message"`
} `json:"error"`
Message string `json:"message"`
}
if err := json.Unmarshal([]byte(data), &ev); err != nil {
return nil
}
switch ev.Type {
case "response.created":
if !d.started {
d.started = true
emit(Event{Kind: KStart, MsgID: ev.Response.ID, Model: ev.Response.Model})
}
case "response.output_item.added":
if ev.Item.Type == "function_call" {
d.argsSeen, d.called = false, true
emit(Event{Kind: KToolStart, ID: ev.Item.CallID, Name: ev.Item.Name})
}
case "response.output_text.delta":
emit(Event{Kind: KText, Text: ev.Delta})
case "response.reasoning_summary_text.delta", "response.reasoning_text.delta":
emit(Event{Kind: KThink, Text: ev.Delta})
case "response.function_call_arguments.delta":
d.argsSeen = true
emit(Event{Kind: KToolArgs, Text: ev.Delta})
case "response.output_item.done":
if ev.Item.Type == "function_call" && !d.argsSeen && ev.Item.Arguments != "" {
emit(Event{Kind: KToolArgs, Text: string(ev.Item.Arguments)})
}
if a := ev.Item.Action; ev.Item.Type == "web_search_call" && a != nil && a.Query != "" {
var hits []Hit
for _, src := range a.Sources {
if src.URL != "" {
hits = append(hits, Hit{Title: src.URL, URL: src.URL})
}
}
emit(Event{Kind: KSearch, Text: a.Query, Hits: hits})
}
case "response.completed", "response.incomplete", "response.failed":
if ev.Response.Error != nil {
emit(Event{Kind: KError, Text: ev.Response.Error.Message, Code: refusedCode(data)})
return nil
}
stop := "stop"
if ev.Response.Status == "incomplete" {
stop = "length"
if ev.Response.IncompleteDetails != nil && ev.Response.IncompleteDetails.Reason == "content_filter" {
stop = "filter"
}
} else if d.called {
// the ChatGPT backend's completed response lists no output
stop = "tool"
} else {
for _, it := range ev.Response.Output {
if it.Type == "function_call" {
stop = "tool"
}
}
}
emit(Event{Kind: KStop, Stop: stop})
emit(Event{Kind: KUsage, Usage: ev.Response.Usage.usage()})
case "error":
msg := ev.Message
if ev.Error != nil {
msg = ev.Error.Message
}
emit(Event{Kind: KError, Text: msg, Code: refusedCode(data)})
}
return nil
}
// responsesEncoder writes events as a Responses stream. Codex reads the
// full item from output_item.done and usage from response.completed.
type responsesEncoder struct {
w *sseWriter
model string
id string
created int64
seq int
started bool
item int // index of the open output item
open Kind // kind of the open item
itemID string // id of the open item
text strings.Builder // text of the open message / reasoning
output []map[string]any
col collector
named map[string]nsTool // the request's namespaced tools
}
// callTo names the tool a function_call item is to as the client knows it:
// a namespaced tool by its name and namespace, not the flat name the model
// used.
func callTo(item map[string]any, name string, named map[string]nsTool) map[string]any {
if q := named[name]; q.Search {
// Codex runs its tool search itself, from the item it knows it by
args, _ := item["arguments"].(string)
delete(item, "name")
item["type"], item["execution"], item["arguments"] = "tool_search_call", "client", parseArgs(args)
} else if q, ok := named[name]; ok {
item["name"], item["namespace"] = q.Name, q.Namespace
// Nothing magpie serves seals arguments. Codex reads a namespaced call
// without this list as sealed: spawn_agent's message in MultiAgentV2
// would reach the subagent as ciphertext, which is really plain text.
item["encrypted_function_args"] = []any{}
} else {
item["name"] = name
}
return item
}
// itemPrefix is the prefix of a call item's id, by its type: OpenAI turns
// away a tool_search_call whose id isn't a tsc_ one ("Invalid
// 'input[98].id': 'fc_…'. Expected an ID that begins with 'tsc'"), and
// Codex hands the item back to it when the conversation goes there.
func itemPrefix(item map[string]any) string {
if item["type"] == "tool_search_call" {
return "tsc_"
}
return "fc_"
}
func (e *responsesEncoder) send(typ string, fields map[string]any) {
fields["type"] = typ
fields["sequence_number"] = e.seq
e.seq++
e.w.event(typ, fields)
}
func (e *responsesEncoder) response(status string, extra map[string]any) map[string]any {
// a list, never null, before any item: Muse Code refuses a stream whose
// response.created has output null (its nil slice in an any isn't nil)
output := e.output
if output == nil {
output = []map[string]any{}
}
out := map[string]any{"id": e.id, "object": "response", "created_at": e.created, "status": status,
"model": e.model, "output": output, "parallel_tool_calls": true, "tool_choice": "auto", "tools": []any{}}
for k, v := range extra {
out[k] = v
}
return out
}
func (e *responsesEncoder) start(ev Event) {
if e.started {
return
}
e.started, e.item = true, -1
e.id, e.created = ev.MsgID, time.Now().Unix()
if e.id == "" {
e.id = newID()
}
if !strings.HasPrefix(e.id, "resp_") {
e.id = "resp_" + e.id
}
if ev.Model != "" {
e.model = ev.Model
}
e.send("response.created", map[string]any{"response": e.response("in_progress", nil)})
e.send("response.in_progress", map[string]any{"response": e.response("in_progress", nil)})
}
func (e *responsesEncoder) closeItem() {
if e.open == "" {
return
}
var item map[string]any
switch e.open {
case Text:
t := e.text.String()
e.send("response.output_text.done", map[string]any{"item_id": e.itemID, "output_index": e.item, "content_index": 0, "text": t, "logprobs": []any{}})
part := map[string]any{"type": "output_text", "text": t, "annotations": []any{}, "logprobs": []any{}}
e.send("response.content_part.done", map[string]any{"item_id": e.itemID, "output_index": e.item, "content_index": 0, "part": part})
item = map[string]any{"id": e.itemID, "type": "message", "role": "assistant", "status": "completed", "content": []map[string]any{part}}
case Thinking:
t := e.text.String()
e.send("response.reasoning_summary_text.done", map[string]any{"item_id": e.itemID, "output_index": e.item, "summary_index": 0, "text": t})
part := map[string]any{"type": "summary_text", "text": t}
e.send("response.reasoning_summary_part.done", map[string]any{"item_id": e.itemID, "output_index": e.item, "summary_index": 0, "part": part})
item = map[string]any{"id": e.itemID, "type": "reasoning", "status": "completed", "summary": []map[string]any{part}}
case ToolCall:
args := strings.TrimSpace(e.text.String())
if args == "" {
args = "{}"
}
p := e.col.last(ToolCall)
if !e.named[p.Name].Search {
e.send("response.function_call_arguments.done", callTo(map[string]any{"item_id": e.itemID, "output_index": e.item, "call_id": p.ID, "arguments": args}, p.Name, e.named))
}
item = callTo(map[string]any{"id": e.itemID, "type": "function_call", "status": "completed", "call_id": p.ID, "arguments": args}, p.Name, e.named)
}
e.send("response.output_item.done", map[string]any{"output_index": e.item, "item": item})
e.output = append(e.output, item)
e.open, e.text = "", strings.Builder{}
}
func (e *responsesEncoder) openItem(k Kind, prefix string, item map[string]any) {
e.closeItem()
e.item++
e.open, e.itemID = k, prefix+newID()
item["id"] = e.itemID
item["status"] = "in_progress"
e.send("response.output_item.added", map[string]any{"output_index": e.item, "item": item})
}
func (e *responsesEncoder) event(ev Event) {
if ev.Kind != KStart && !e.started {
e.start(Event{})
}
switch ev.Kind {
case KStart:
e.start(ev)
case KText:
if ev.Text == "" {
return
}
if e.open != Text {
e.openItem(Text, "msg_", map[string]any{"type": "message", "role": "assistant", "content": []any{}})
e.send("response.content_part.added", map[string]any{"item_id": e.itemID, "output_index": e.item, "content_index": 0,
"part": map[string]any{"type": "output_text", "text": "", "annotations": []any{}, "logprobs": []any{}}})
}
e.text.WriteString(ev.Text)
e.send("response.output_text.delta", map[string]any{"item_id": e.itemID, "output_index": e.item, "content_index": 0, "delta": ev.Text, "logprobs": []any{}})
case KThink:
if ev.Text == "" {
return
}
if e.open != Thinking {
e.openItem(Thinking, "rs_", map[string]any{"type": "reasoning", "summary": []any{}})
e.send("response.reasoning_summary_part.added", map[string]any{"item_id": e.itemID, "output_index": e.item, "summary_index": 0,
"part": map[string]any{"type": "summary_text", "text": ""}})
}
e.text.WriteString(ev.Text)
e.send("response.reasoning_summary_text.delta", map[string]any{"item_id": e.itemID, "output_index": e.item, "summary_index": 0, "delta": ev.Text})
case KToolStart:
if ev.ID == "" {
ev.ID = "call_" + newID()
}
// Open (and so close the previous item) before recording this call:
// closeItem reads the call ID from e.col.last(ToolCall).
item := callTo(map[string]any{"type": "function_call", "call_id": ev.ID, "arguments": ""}, ev.Name, e.named)
e.openItem(ToolCall, itemPrefix(item), item)
e.col.add(ev)
return
case KToolArgs:
if e.open == ToolCall && ev.Text != "" {
e.text.WriteString(ev.Text)
e.send("response.function_call_arguments.delta", map[string]any{"item_id": e.itemID, "output_index": e.item, "delta": ev.Text})
}
case KError:
e.closeItem()
code := "server_error"
if ev.Code != "" {
code = ev.Code
}
e.send("response.failed", map[string]any{"response": e.response("failed", map[string]any{"error": map[string]any{"code": code, "message": ev.Text}})})
}
e.col.add(ev)
}
// keepalive is response.in_progress again: Codex's idle timeout counts
// events only, an SSE comment never reaching it, and skips this one.
func (e *responsesEncoder) keepalive() {
if !e.started {
e.start(Event{}) // response.created and response.in_progress
return
}
e.send("response.in_progress", map[string]any{"response": e.response("in_progress", nil)})
}
func (e *responsesEncoder) finish() {
if !e.started {
e.start(Event{})
}
e.closeItem()
res := e.col.finish()
status, typ := "completed", "response.completed"
extra := map[string]any{"usage": res.Usage.responses(), "incomplete_details": nil}
if res.Stop == "length" || res.Stop == "filter" {
status, typ = "incomplete", "response.incomplete"
reason := "max_output_tokens"
if res.Stop == "filter" {
reason = "content_filter"
}
extra["incomplete_details"] = map[string]any{"reason": reason}
}
e.send(typ, map[string]any{"response": e.response(status, extra)})
}
// renderResponses is the non-streaming reply.
func renderResponses(res Result, model string, named map[string]nsTool) []byte {
output := []map[string]any{}
for _, p := range res.Parts {
switch p.Kind {
case Text:
output = append(output, map[string]any{"id": "msg_" + newID(), "type": "message", "role": "assistant", "status": "completed",
"content": []map[string]any{{"type": "output_text", "text": p.Text, "annotations": []any{}}}})
case Thinking:
output = append(output, map[string]any{"id": "rs_" + newID(), "type": "reasoning", "status": "completed",
"summary": []map[string]any{{"type": "summary_text", "text": p.Text}}})
case ToolCall:
id := p.ID
if id == "" {
id = "call_" + newID()
}
item := callTo(map[string]any{"type": "function_call", "status": "completed",
"call_id": id, "arguments": argsString(p)}, p.Name, named)
item["id"] = itemPrefix(item) + newID()
output = append(output, item)
}
}
id := res.ID
if id == "" {
id = newID()
}
if !strings.HasPrefix(id, "resp_") {
id = "resp_" + id
}
if res.Model != "" {
model = res.Model
}
status := "completed"
var incomplete any
switch res.Stop {
case "length":
status, incomplete = "incomplete", map[string]any{"reason": "max_output_tokens"}
case "filter":
// as the stream says it (#248)
status, incomplete = "incomplete", map[string]any{"reason": "content_filter"}
}
b, _ := json.Marshal(map[string]any{"id": id, "object": "response", "created_at": time.Now().Unix(), "status": status,
"model": model, "output": output, "usage": res.Usage.responses(), "incomplete_details": incomplete,
"parallel_tool_calls": true, "tool_choice": "auto", "tools": []any{}, "error": nil})
return b
}