| 211 | } else { |
| 212 | // Handle stdio connection |
| 213 | const responseHandler = (data: Buffer) => { |
| 214 | try { |
| 215 | const response = JSON.parse(data.toString().trim()); |
| 216 | if (response.id === message.id) { |
| 217 | clearTimeout(timeout); |
| 218 | connection.stdout?.off('data', responseHandler); |
| 219 | |
| 220 | if (response.error) { |
| 221 | reject(new Error(response.error.message)); |
| 222 | } else { |
| 223 | resolve(response.result); |
| 224 | } |
| 225 | } |
| 226 | } catch (error) { |
| 227 | // Ignore JSON parse errors for partial messages |
| 228 | } |
| 229 | }; |
| 230 | connection.stdout?.on('data', responseHandler); |
| 231 | connection.stdin?.write(JSON.stringify(message) + '\n'); |
| 232 | } |