| 217 | } |
| 218 | |
| 219 | private async *read() { |
| 220 | let content = '' |
| 221 | |
| 222 | // Called once before for-await (an empty this.input otherwise skips the |
| 223 | // loop body entirely), then again per block. prependedLines re-check is |
| 224 | // inside the while so a prepend pushed between two messages in the SAME |
| 225 | // block still lands first. |
| 226 | const splitAndProcess = async function* (this: StructuredIO) { |
| 227 | for (;;) { |
| 228 | if (this.prependedLines.length > 0) { |
| 229 | content = this.prependedLines.join('') + content |
| 230 | this.prependedLines = [] |
| 231 | } |
| 232 | const newline = content.indexOf('\n') |
| 233 | if (newline === -1) break |
| 234 | const line = content.slice(0, newline) |
| 235 | content = content.slice(newline + 1) |
| 236 | const message = await this.processLine(line) |
| 237 | if (message) { |
| 238 | logForDiagnosticsNoPII('info', 'cli_stdin_message_parsed', { |
| 239 | type: message.type, |
| 240 | }) |
| 241 | yield message |
| 242 | } |
| 243 | } |
| 244 | }.bind(this) |
| 245 | |
| 246 | yield* splitAndProcess() |
| 247 | |
| 248 | for await (const block of this.input) { |
| 249 | content += block |
| 250 | yield* splitAndProcess() |
| 251 | } |
| 252 | if (content) { |
| 253 | const message = await this.processLine(content) |
| 254 | if (message) { |
| 255 | yield message |
| 256 | } |
| 257 | } |
| 258 | this.inputClosed = true |
| 259 | for (const request of this.pendingRequests.values()) { |
| 260 | // Reject all pending requests if the input stream |
| 261 | request.reject( |
| 262 | new Error('Tool permission stream closed before response received'), |
| 263 | ) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | getPendingPermissionRequests() { |
| 268 | return Array.from(this.pendingRequests.values()) |