* Parse elicitation-specific fields from a HookOutsideReplResult. * Mirrors the relevant branches of processHookJSONOutput for Elicitation * and ElicitationResult hook events.
( result: HookOutsideReplResult, expectedEventName: 'Elicitation' | 'ElicitationResult', )
| 4388 | * and ElicitationResult hook events. |
| 4389 | */ |
| 4390 | function parseElicitationHookOutput( |
| 4391 | result: HookOutsideReplResult, |
| 4392 | expectedEventName: 'Elicitation' | 'ElicitationResult', |
| 4393 | ): { |
| 4394 | response?: ElicitationResponse |
| 4395 | blockingError?: HookBlockingError |
| 4396 | } { |
| 4397 | // Exit code 2 = blocking (same as executeHooks path) |
| 4398 | if (result.blocked && !result.succeeded) { |
| 4399 | return { |
| 4400 | blockingError: { |
| 4401 | blockingError: result.output || `Elicitation blocked by hook`, |
| 4402 | command: result.command, |
| 4403 | }, |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | if (!result.output.trim()) { |
| 4408 | return {} |
| 4409 | } |
| 4410 | |
| 4411 | // Try to parse JSON output for structured elicitation response |
| 4412 | const trimmed = result.output.trim() |
| 4413 | if (!trimmed.startsWith('{')) { |
| 4414 | return {} |
| 4415 | } |
| 4416 | |
| 4417 | try { |
| 4418 | const parsed = hookJSONOutputSchema().parse(JSON.parse(trimmed)) |
| 4419 | if (isAsyncHookJSONOutput(parsed)) { |
| 4420 | return {} |
| 4421 | } |
| 4422 | if (!isSyncHookJSONOutput(parsed)) { |
| 4423 | return {} |
| 4424 | } |
| 4425 | |
| 4426 | // Check for top-level decision: 'block' (exit code 0 + JSON block) |
| 4427 | if (parsed.decision === 'block' || result.blocked) { |
| 4428 | return { |
| 4429 | blockingError: { |
| 4430 | blockingError: parsed.reason || 'Elicitation blocked by hook', |
| 4431 | command: result.command, |
| 4432 | }, |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | const specific = parsed.hookSpecificOutput |
| 4437 | if (!specific || specific.hookEventName !== expectedEventName) { |
| 4438 | return {} |
| 4439 | } |
| 4440 | |
| 4441 | if (!specific.action) { |
| 4442 | return {} |
| 4443 | } |
| 4444 | |
| 4445 | const response: ElicitationResponse = { |
| 4446 | action: specific.action, |
| 4447 | content: specific.content as ElicitationResponse['content'] | undefined, |
no test coverage detected