| 1 | # HTTP Wire Protocol: `cn remote` � `cn serve` |
| 2 | |
| 3 | This document describes the HTTP protocol used for communication between the `cn remote` client and `cn serve` server |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | The protocol uses a polling-based REST API where: |
| 8 | |
| 9 | - The server (`cn serve`) runs an Express HTTP server on port 3000 |
| 10 | - The client (`cn remote`) polls the server every 500ms for state updates |
| 11 | - All communication uses JSON payloads |
| 12 | |
| 13 | ## Endpoints |
| 14 | |
| 15 | ### `GET /state` |
| 16 | |
| 17 | Returns the current chat state including message history and processing status. |
| 18 | |
| 19 | **Response:** |
| 20 | |
| 21 | ```json |
| 22 | { |
| 23 | "chatHistory": [ |
| 24 | { |
| 25 | "role": "user" | "assistant" | "system", |
| 26 | "content": "string", |
| 27 | "isStreaming": boolean, |
| 28 | "messageType": "tool-start" | "tool-result" | "tool-error" | "system", |
| 29 | "toolName": "string", |
| 30 | "toolResult": "string" |
| 31 | } |
| 32 | ], |
| 33 | "isProcessing": boolean, |
| 34 | "messageQueueLength": number |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | ### `POST /message` |
| 39 | |
| 40 | Sends a user message to the server. Messages are queued and processed sequentially. |
| 41 | |
| 42 | **Request Body:** |
| 43 | |
| 44 | ```json |
| 45 | { |
| 46 | "message": "string" |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | **Response:** |
| 51 | |
| 52 | ```json |
| 53 | { |
| 54 | "queued": true, |
| 55 | "position": number, |
| 56 | "willInterrupt": boolean |
| 57 | } |
| 58 | ``` |
| 59 | |
| 60 | **Special Cases:** |
| 61 | |
| 62 | - Empty message (`""`) interrupts current processing |
| 63 | - Message `/exit` initiates server shutdown |
| 64 | |
| 65 | ### `GET /diff` |
| 66 | |
| 67 | Returns the git diff between the current branch and main branch. |
| 68 | |
| 69 | **Response (Success):** |
| 70 | |
| 71 | ```json |
| 72 | { |
| 73 | "diff": "string" |
| 74 | } |
| 75 | ``` |
| 76 | |
| 77 | **Response (Error):** |
| 78 | |
| 79 | - 404: Not in a git repository |
| 80 | - 500: Git command failed |
| 81 | |
| 82 | ### `POST /exit` |
| 83 | |
| 84 | Gracefully shuts down the server. |
| 85 | |
| 86 | **Response:** |
| 87 | |
| 88 | ```json |
| 89 | { |
| 90 | "message": "Server shutting down", |
| 91 | "success": true |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | ## Message Types |
| 96 | |
| 97 | Messages in the chat history can have different types: |
| 98 | |
| 99 | - **Regular messages**: Standard user/assistant messages |
| 100 | - **Tool messages**: Messages with `messageType` set to: |
| 101 | - `tool-start`: Tool execution started |
| 102 | - `tool-result`: Tool execution completed |
| 103 | - `tool-error`: Tool execution failed |
| 104 | - `system`: System messages |
| 105 | |
| 106 | ## Protocol Flow |
| 107 | |
| 108 | 1. **Client starts**: Begins polling `GET /state` every 500ms |
| 109 | 2. **User sends message**: Client posts to `POST /message` |
| 110 | 3. **Server queues message**: Returns queue position |
| 111 | 4. **Server processes**: Updates state with streaming responses |
| 112 | 5. **Client displays**: Shows updates from state polling |
| 113 | 6. **Interruption**: Client sends empty message to interrupt |
| 114 | 7. **Exit**: Client sends `/exit` or `POST /exit` to shutdown |
| 115 | |
| 116 | ## Implementation Files |
| 117 | |
| 118 | ### Server Implementation |
| 119 | |
| 120 | - **Main server**: `src/commands/serve.ts:51-363` |
| 121 | - Express server setup |
| 122 | - Endpoint handlers |
| 123 | - State management |
| 124 | - Message processing |
| 125 | |
| 126 | ### Client Implementation |
| 127 | |
| 128 | - **Remote chat hook**: `src/ui/hooks/useChat.ts:170-310` |
| 129 | - State polling logic |
| 130 | - Message sending |
| 131 | - Interrupt handling |
| 132 | |
| 133 | ### Type Definitions |
| 134 | |
| 135 | - **Display message types**: `src/ui/types.ts:1-16` |
| 136 | - **Server state interface**: `src/commands/serve.ts:20-33` |
| 137 | |
| 138 | ### Testing |
| 139 | |
| 140 | - **Mock server**: `src/ui/__tests__/mockRemoteServer.ts` |
| 141 | - Complete mock implementation for testing |
| 142 | - Simulates streaming and message processing |
| 143 | |
| 144 | ## Features |
| 145 | |
| 146 | ### Message Queueing |
| 147 | |
| 148 | - Messages are queued and processed one at a time |
| 149 | - Queue position returned on message submission |
| 150 | - Supports interruption of current processing |
| 151 | |
| 152 | ### Streaming Support |
| 153 | |
| 154 | - `isStreaming` flag indicates ongoing response |
| 155 | - Character-by-character updates for real-time display |
| 156 | |
| 157 | ### Auto-shutdown |
| 158 | |
| 159 | - Server shuts down after timeout (default: 300 seconds) |
| 160 | - Configurable via `--timeout` flag |
| 161 | |
| 162 | ### Error Handling |
| 163 | |
| 164 | - HTTP status codes for error conditions |
| 165 | - Graceful error messages in responses |
| 166 | |
| 167 | ## Security Considerations |
| 168 | |
| 169 | - Server binds to `127.0.0.1` (localhost only) |
| 170 | - No authentication implemented (local use only) |
| 171 | - File system access through tool execution |