| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "strconv" |
| 10 | "sync" |
| 11 | ) |
| 12 | |
| 13 | // MaxAppServerFrame bounds one app-server line. |
| 14 | const MaxAppServerFrame = 1 << 20 |
| 15 | |
| 16 | // ErrAppServer is any protocol failure talking to codex app-server; the |
| 17 | // app-server's own error text is never surfaced. |
| 18 | var ErrAppServer = errors.New("codex app-server protocol failure") |
| 19 | |
| 20 | // Message is one app-server line that is not an answer to our call: a |
| 21 | // notification (ID empty) or a request the server makes of us (ID set), |
| 22 | // which must be answered with Answer or Refuse. |
| 23 | type Message struct { |
| 24 | ID json.RawMessage |
| 25 | Method string |
| 26 | Params json.RawMessage |
| 27 | } |
| 28 | |
| 29 | type reply struct { |
| 30 | result, err json.RawMessage |
| 31 | } |
| 32 | |
| 33 | // AppServer is a JSON-RPC 2.0 client over codex app-server's stdio, |
| 34 | // one JSON document per line. |
| 35 | type AppServer struct { |
| 36 | w io.Writer |
| 37 | wmu sync.Mutex |
| 38 | mu sync.Mutex |
| 39 | next int64 |
| 40 | pending map[string]chan reply |
| 41 | // Messages are notifications and server requests in order. It closes |
| 42 | // when the server's output ends. |
| 43 | Messages chan Message |
| 44 | done chan struct{} |
| 45 | err error |
| 46 | } |
| 47 | |
| 48 | // NewAppServer reads r until it ends; w is the server's stdin. |
| 49 | func NewAppServer(r io.Reader, w io.Writer) *AppServer { |
| 50 | a := &AppServer{w: w, pending: map[string]chan reply{}, Messages: make(chan Message, 256), done: make(chan struct{})} |
| 51 | go a.read(r) |
| 52 | return a |
| 53 | } |
| 54 | |
| 55 | func (a *AppServer) read(r io.Reader) { |
| 56 | defer close(a.done) |
| 57 | defer close(a.Messages) |
| 58 | scan := bufio.NewScanner(r) |
| 59 | scan.Buffer(make([]byte, 0, 64<<10), MaxAppServerFrame) |
| 60 | for scan.Scan() { |
| 61 | var m struct { |
| 62 | ID json.RawMessage `json:"id"` |
| 63 | Method string `json:"method"` |
| 64 | Params json.RawMessage `json:"params"` |
| 65 | Result json.RawMessage `json:"result"` |
| 66 | Error json.RawMessage `json:"error"` |
| 67 | } |
| 68 | if json.Unmarshal(scan.Bytes(), &m) != nil { |
| 69 | a.fail(ErrAppServer) |
| 70 | return |
| 71 | } |
| 72 | if m.Method == "" { |
| 73 | a.mu.Lock() |
| 74 | ch := a.pending[string(m.ID)] |
| 75 | delete(a.pending, string(m.ID)) |
| 76 | a.mu.Unlock() |
| 77 | if ch != nil { |
| 78 | ch <- reply{result: m.Result, err: m.Error} |
| 79 | } |
| 80 | continue |
| 81 | } |
| 82 | select { |
| 83 | case a.Messages <- Message{ID: m.ID, Method: m.Method, Params: m.Params}: |
| 84 | default: |
| 85 | // A reader that falls this far behind has lost the turn. |
| 86 | a.fail(ErrAppServer) |
| 87 | return |
| 88 | } |
| 89 | } |
| 90 | a.fail(io.EOF) |
| 91 | } |
| 92 | |
| 93 | func (a *AppServer) fail(err error) { |
| 94 | a.mu.Lock() |
| 95 | defer a.mu.Unlock() |
| 96 | if a.err == nil { |
| 97 | a.err = err |
| 98 | } |
| 99 | for id, ch := range a.pending { |
| 100 | close(ch) |
| 101 | delete(a.pending, id) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Done closes when the server's output has ended. |
| 106 | func (a *AppServer) Done() <-chan struct{} { return a.done } |
| 107 | |
| 108 | func (a *AppServer) write(v any) error { |
| 109 | raw, err := json.Marshal(v) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | a.wmu.Lock() |
| 114 | defer a.wmu.Unlock() |
| 115 | _, err = a.w.Write(append(raw, '\n')) |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | // Call sends a request and waits for its answer. A JSON-RPC error answer |
| 120 | // is returned as ErrAppServer with the error document for the caller to |
| 121 | // read codes from (never to show). |
| 122 | func (a *AppServer) Call(ctx context.Context, method string, params any) (json.RawMessage, json.RawMessage, error) { |
| 123 | a.mu.Lock() |
| 124 | if a.err != nil { |
| 125 | a.mu.Unlock() |
| 126 | return nil, nil, ErrAppServer |
| 127 | } |
| 128 | a.next++ |
| 129 | id := strconv.Quote("e" + strconv.FormatInt(a.next, 10)) |
| 130 | ch := make(chan reply, 1) |
| 131 | a.pending[id] = ch |
| 132 | a.mu.Unlock() |
| 133 | if err := a.write(map[string]any{"id": json.RawMessage(id), "method": method, "params": params}); err != nil { |
| 134 | return nil, nil, ErrAppServer |
| 135 | } |
| 136 | select { |
| 137 | case r, ok := <-ch: |
| 138 | if !ok { |
| 139 | return nil, nil, ErrAppServer |
| 140 | } |
| 141 | if len(r.err) > 0 && string(r.err) != "null" { |
| 142 | return nil, r.err, ErrAppServer |
| 143 | } |
| 144 | return r.result, nil, nil |
| 145 | case <-ctx.Done(): |
| 146 | a.mu.Lock() |
| 147 | delete(a.pending, id) |
| 148 | a.mu.Unlock() |
| 149 | return nil, nil, ctx.Err() |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Notify sends a notification. |
| 154 | func (a *AppServer) Notify(method string, params any) error { |
| 155 | return a.write(map[string]any{"method": method, "params": params}) |
| 156 | } |
| 157 | |
| 158 | // Answer answers a server request. |
| 159 | func (a *AppServer) Answer(id json.RawMessage, result any) error { |
| 160 | return a.write(map[string]any{"id": id, "result": result}) |
| 161 | } |
| 162 | |
| 163 | // Refuse answers a server request with an error: Equipo does not do it. |
| 164 | func (a *AppServer) Refuse(id json.RawMessage) error { |
| 165 | return a.write(map[string]any{"id": id, "error": map[string]any{"code": -32000, "message": "refused by policy"}}) |
| 166 | } |
| 167 | |