package codexhost import ( "encoding/json" "errors" "fmt" "log" "sort" ) func (h *bridge) fromClient(client *downstream, msg message) error { var method string _ = json.Unmarshal(msg["method"], &method) id := msg["id"] if len(id) > 256 { return errors.New("Codex RPC id exceeds bridge limit") } if method == "initialize" && len(id) > 0 { if client.ready { return errors.New("duplicate initialize") } response := message{"id": id} for key, value := range h.initialize { if key != "id" { response[key] = value } } if err := h.write(client.conn, response); err != nil { return err } client.ready = true keys := make([]string, 0, len(h.pending)) for key := range h.pending { keys = append(keys, key) } sort.Strings(keys) for _, key := range keys { if err := h.writeRaw(client.conn, h.pending[key].data); err != nil { return err } } return nil } if !client.ready { return errors.New("initialize required") } if method == "initialized" { return nil } // the host initialized upstream once if method != "" && len(id) > 0 { if len(h.calls) >= maxCalls { return errors.New("too many outstanding Codex calls") } h.next++ hostID := json.RawMessage(fmt.Sprintf(`"host-%d"`, h.next)) h.calls[string(hostID)] = route{client, id} msg["id"] = hostID if err := h.write(h.upstream, msg); err != nil { h.cancel() return err } return nil } if method == "" && len(id) > 0 { key := string(id) item, ok := h.pending[key] if !ok { return nil } // stale/duplicate callback response is never replayed if err := h.write(h.upstream, msg); err != nil { h.cancel() return err } delete(h.pending, key) h.pendingBytes -= len(item.data) return nil } if err := h.write(h.upstream, msg); err != nil { h.cancel() return err } return nil } func (h *bridge) readUpstream() { defer h.cancel() for { _, data, err := h.upstream.Read(h.ctx) if err != nil { if h.ctx.Err() == nil { log.Print("Codex App Server connection ended; connection-bound interactions cannot be recovered by a new upstream connection") } return } var msg message if json.Unmarshal(data, &msg) != nil { return } h.mu.Lock() ok := h.fromUpstream(msg, data) h.mu.Unlock() if !ok { log.Print("Codex bridge received invalid or over-budget state; closing rather than auto-answering interactions") return } } } func (h *bridge) fromUpstream(msg message, data []byte) bool { id := msg["id"] if len(id) > 256 { return false } var method string _ = json.Unmarshal(msg["method"], &method) if string(id) == `"host-initialize"` && method == "" { if h.initialize != nil { return false } h.initialize = msg close(h.initialized) return true } if len(id) > 0 && method != "" { key := string(id) old, exists := h.pending[key] if (!exists && len(h.pending) >= maxPending) || h.pendingBytes-len(old.data)+len(data) > maxPendingBytes { return false } var params struct { Thread string `json:"threadId"` } _ = json.Unmarshal(msg["params"], ¶ms) h.pending[key] = pending{append([]byte(nil), data...), params.Thread} h.pendingBytes += len(data) - len(old.data) } else if len(id) > 0 { target, ok := h.calls[string(id)] delete(h.calls, string(id)) if ok && h.active == target.client && target.client.ready { msg["id"] = target.id h.send(target.client, msg) } return true } else if method == "serverRequest/resolved" { var p struct { Thread string `json:"threadId"` ID json.RawMessage `json:"requestId"` } if json.Unmarshal(msg["params"], &p) == nil { if old, ok := h.pending[string(p.ID)]; ok && old.thread == p.Thread { delete(h.pending, string(p.ID)) h.pendingBytes -= len(old.data) } } } if h.active != nil && h.active.ready { h.send(h.active, msg) } return true }