* Inject a control_response message to resolve a pending permission request. * Used by the bridge to feed permission responses from the remote client into the * SDK permission flow. * * Also sends a control_cancel_request to the SDK consumer so its canUseTool * callback is aborted via
(response: SDKControlResponse)
| 282 | * callback is aborted via the signal — otherwise the callback hangs. |
| 283 | */ |
| 284 | injectControlResponse(response: SDKControlResponse): void { |
| 285 | const requestId = response.response?.request_id |
| 286 | if (!requestId) return |
| 287 | const request = this.pendingRequests.get(requestId) |
| 288 | if (!request) return |
| 289 | this.trackResolvedToolUseId(request.request) |
| 290 | this.pendingRequests.delete(requestId) |
| 291 | // Cancel the SDK consumer's canUseTool callback — the bridge won. |
| 292 | void this.write({ |
| 293 | type: 'control_cancel_request', |
| 294 | request_id: requestId, |
| 295 | }) |
| 296 | if (response.response.subtype === 'error') { |
| 297 | request.reject(new Error(response.response.error)) |
| 298 | } else { |
| 299 | const result = response.response.response |
| 300 | if (request.schema) { |
| 301 | try { |
| 302 | request.resolve(request.schema.parse(result)) |
| 303 | } catch (error) { |
| 304 | request.reject(error) |
| 305 | } |
| 306 | } else { |
| 307 | request.resolve({}) |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Register a callback invoked whenever a can_use_tool control_request |
no test coverage detected