()
| 67 | } |
| 68 | |
| 69 | private async readMessages(): Promise<void> { |
| 70 | const rl = createInterface({ input: this.childStdout }) |
| 71 | try { |
| 72 | for await (const line of rl) { |
| 73 | if (line.trim()) { |
| 74 | try { |
| 75 | const message = JSON.parse(line) as SDKMessage | SDKControlResponse |
| 76 | if (message.type === 'control_response') { |
| 77 | const controlResponse = message as SDKControlResponse |
| 78 | const handler = this.pendingControlResponses.get(controlResponse.response.request_id) |
| 79 | if (handler) handler(controlResponse.response) |
| 80 | continue |
| 81 | } else if (message.type === 'control_request') { |
| 82 | await this.handleControlRequest(message as unknown as CanUseToolControlRequest) |
| 83 | continue |
| 84 | } else if (message.type === 'control_cancel_request') { |
| 85 | this.handleControlCancelRequest(message as unknown as ControlCancelRequest) |
| 86 | continue |
| 87 | } |
| 88 | this.inputStream.enqueue(message) |
| 89 | } catch (e) { |
| 90 | logger.debug(line) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | await this.processExitPromise |
| 95 | } catch (error) { |
| 96 | this.inputStream.error(error as Error) |
| 97 | } finally { |
| 98 | this.inputStream.done() |
| 99 | this.cleanupControllers() |
| 100 | rl.close() |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private async *readSdkMessages(): AsyncIterableIterator<SDKMessage> { |
| 105 | for await (const message of this.inputStream) { |
no test coverage detected