| 1 | package codex |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 9 | ) |
| 10 | |
| 11 | const MaxFrameBytes = 1024 * 1024 |
| 12 | const errProtocol provider.Error = "codex_protocol_rejected" |
| 13 | |
| 14 | type envelope struct { |
| 15 | ID json.RawMessage `json:"id"` |
| 16 | Method string `json:"method"` |
| 17 | Params json.RawMessage `json:"params"` |
| 18 | Result json.RawMessage `json:"result"` |
| 19 | Error json.RawMessage `json:"error"` |
| 20 | } |
| 21 | |
| 22 | func parse(raw []byte) (envelope, error) { |
| 23 | var v envelope |
| 24 | if len(raw) == 0 || len(raw) > MaxFrameBytes || !uniqueJSON(raw) || json.Unmarshal(raw, &v) != nil { |
| 25 | return v, errProtocol |
| 26 | } |
| 27 | if len(v.ID) > 0 { |
| 28 | var s string |
| 29 | var n int64 |
| 30 | if json.Unmarshal(v.ID, &s) != nil && json.Unmarshal(v.ID, &n) != nil { |
| 31 | return v, errProtocol |
| 32 | } |
| 33 | if bytes.Equal(v.ID, []byte("null")) { |
| 34 | return v, errProtocol |
| 35 | } |
| 36 | } |
| 37 | if v.Method != "" { |
| 38 | if len(v.Result) > 0 || len(v.Error) > 0 || len(v.Params) == 0 { |
| 39 | return v, errProtocol |
| 40 | } |
| 41 | } else if len(v.ID) == 0 || (len(v.Result) == 0) == (len(v.Error) == 0) { |
| 42 | return v, errProtocol |
| 43 | } |
| 44 | return v, nil |
| 45 | } |
| 46 | |
| 47 | // Decode retains the complete frame, including provider IDs and usage totals. |
| 48 | func Decode(raw []byte) ([]provider.Event, error) { |
| 49 | v, e := parse(raw) |
| 50 | if e != nil { |
| 51 | return nil, e |
| 52 | } |
| 53 | if v.Method == "deprecationNotice" { |
| 54 | if !pinnedDeprecation(v) { |
| 55 | return nil, errProtocol |
| 56 | } |
| 57 | return nil, nil |
| 58 | } |
| 59 | if v.Method == "account/rateLimits/updated" { |
| 60 | var params map[string]json.RawMessage |
| 61 | var limits map[string]json.RawMessage |
| 62 | if len(v.ID) != 0 || json.Unmarshal(v.Params, ¶ms) != nil || len(params) != 1 || json.Unmarshal(params["rateLimits"], &limits) != nil || limits == nil { |
| 63 | return nil, errProtocol |
| 64 | } |
| 65 | // Global subscription telemetry has no thread or turn to journal as an event. |
| 66 | // The durable capture still retains the frame; account/updated stays fenced. |
| 67 | return nil, nil |
| 68 | } |
| 69 | if v.Method == "" || disabledRemoteControl(v) { |
| 70 | return nil, nil |
| 71 | } |
| 72 | var p struct { |
| 73 | ConversationID string `json:"conversationId"` |
| 74 | ThreadID string `json:"threadId"` |
| 75 | TurnID string `json:"turnId"` |
| 76 | Thread struct{ ID string } |
| 77 | Turn struct{ ID, Status string } |
| 78 | Item struct { |
| 79 | Type, ID, Delivery string |
| 80 | Questions json.RawMessage |
| 81 | } |
| 82 | } |
| 83 | if json.Unmarshal(v.Params, &p) != nil { |
| 84 | return nil, errProtocol |
| 85 | } |
| 86 | if p.ThreadID == "" { |
| 87 | p.ThreadID = p.Thread.ID |
| 88 | } |
| 89 | if (v.Method == "execCommandApproval" || v.Method == "applyPatchApproval") && p.ThreadID == "" { |
| 90 | p.ThreadID = p.ConversationID |
| 91 | } |
| 92 | if p.TurnID == "" { |
| 93 | p.TurnID = p.Turn.ID |
| 94 | } |
| 95 | |
| 96 | switch v.Method { |
| 97 | case "turn/started", "turn/completed", "thread/tokenUsage/updated", "item/started", "item/completed", "item/agentMessage/delta", "item/commandExecution/outputDelta": |
| 98 | if p.ThreadID == "" || p.TurnID == "" { |
| 99 | return nil, errProtocol |
| 100 | } |
| 101 | } |
| 102 | kind := v.Method |
| 103 | switch v.Method { |
| 104 | case "thread/started": |
| 105 | kind = "session.init" |
| 106 | case "turn/started": |
| 107 | kind = "turn.started" |
| 108 | case "turn/completed": |
| 109 | if p.Turn.ID == "" || p.ThreadID == "" { |
| 110 | return nil, errProtocol |
| 111 | } |
| 112 | switch p.Turn.Status { |
| 113 | case "completed": |
| 114 | kind = "turn.completed" |
| 115 | case "failed", "interrupted": |
| 116 | kind = "turn.failed" |
| 117 | default: |
| 118 | return nil, errProtocol |
| 119 | } |
| 120 | case "thread/tokenUsage/updated": |
| 121 | kind = "usage" |
| 122 | case "item/agentMessage/delta": |
| 123 | kind = "message.delta" |
| 124 | case "item/commandExecution/outputDelta": |
| 125 | kind = "tool.output" |
| 126 | case "item/started", "item/completed": |
| 127 | if p.Item.Type == "commandExecution" || p.Item.Type == "dynamicToolCall" || p.Item.Type == "mcpToolCall" || p.Item.Type == "fileChange" { |
| 128 | if v.Method == "item/started" { |
| 129 | kind = "tool.started" |
| 130 | } else { |
| 131 | kind = "tool.completed" |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | if len(v.ID) > 0 { |
| 136 | kind = "request" |
| 137 | } |
| 138 | request := "" |
| 139 | if len(v.ID) > 0 { |
| 140 | request = string(v.ID) |
| 141 | } // JSON preserves string versus numeric server IDs. |
| 142 | if v.Method == "item/completed" && p.Item.Type == "agentMessage" && p.Item.Delivery == "async" && len(p.Item.Questions) > 0 && string(p.Item.Questions) != "null" { |
| 143 | if p.Item.ID == "" { |
| 144 | return nil, errProtocol |
| 145 | } |
| 146 | kind = "request" |
| 147 | request = "async:" + p.Item.ID |
| 148 | } |
| 149 | return []provider.Event{{SessionID: p.ThreadID, TurnID: p.TurnID, RequestID: request, Kind: kind, Payload: bytes.Clone(raw)}}, nil |
| 150 | } |
| 151 | func uniqueJSON(raw []byte) bool { |
| 152 | d := json.NewDecoder(bytes.NewReader(raw)) |
| 153 | d.UseNumber() |
| 154 | var value func() bool |
| 155 | value = func() bool { |
| 156 | token, e := d.Token() |
| 157 | if e != nil { |
| 158 | return false |
| 159 | } |
| 160 | delim, ok := token.(json.Delim) |
| 161 | if !ok { |
| 162 | return true |
| 163 | } |
| 164 | switch delim { |
| 165 | case '{': |
| 166 | seen := map[string]bool{} |
| 167 | for d.More() { |
| 168 | k, e := d.Token() |
| 169 | if e != nil { |
| 170 | return false |
| 171 | } |
| 172 | s, ok := k.(string) |
| 173 | if !ok || seen[s] { |
| 174 | return false |
| 175 | } |
| 176 | seen[s] = true |
| 177 | if !value() { |
| 178 | return false |
| 179 | } |
| 180 | } |
| 181 | end, e := d.Token() |
| 182 | return e == nil && end == json.Delim('}') |
| 183 | case '[': |
| 184 | for d.More() { |
| 185 | if !value() { |
| 186 | return false |
| 187 | } |
| 188 | } |
| 189 | end, e := d.Token() |
| 190 | return e == nil && end == json.Delim(']') |
| 191 | } |
| 192 | return false |
| 193 | } |
| 194 | if !value() { |
| 195 | return false |
| 196 | } |
| 197 | _, e := d.Token() |
| 198 | return e == io.EOF |
| 199 | } |
| 200 | |
| 201 | func disabledRemoteControl(v envelope) bool { |
| 202 | var p struct{ Status string } |
| 203 | return v.Method == "remoteControl/status/changed" && len(v.ID) == 0 && json.Unmarshal(v.Params, &p) == nil && p.Status == "disabled" |
| 204 | } |
| 205 | |