feat: forward ask_user_question from nested subagents to root session via pi-intercom#1
Open
avtc wants to merge 1 commit into
Open
Conversation
… via pi-intercom Enables subagents at any nesting depth to interactively ask the user questions by forwarding them through pi-intercom to the root session that has a TUI. - Add intercom-forwarding module with root-side handler (renders questions via AskUserQuestionComponent) and child-side forwarding (uses sendAndWait to relay questions up to root) - Extract renderQuestionsViaUI helper for shared use by tool handler and intercom handler - Register hooks lazily via intercom:ready event (no hard dependency) - Add message renderer for answered questions in root session history - Fall back to tool deregistration only when intercom is unavailable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds the ability for subagents at any nesting depth to interactively ask the user questions through the existing
ask_user_questionTUI — even though those subagent sessions have no UI of their own.Questions are forwarded over pi-intercom to the root session (which has a TUI), rendered as the familiar interactive component, and the user's answer is relayed back to the subagent.
Why
When a subagent calls
ask_user_questiontoday, it hits the!ctx.hasUIpath and the tool is deregistered — the subagent is told not to ask questions and must guess or skip. This is a poor experience when the agent genuinely needs clarification (e.g., ambiguous requirements, design choices, error recovery).Subagents are increasingly used for complex workflows where user input is valuable. Silently disabling the ability to ask defeats the purpose of interactive tooling.
How
Enabled by pi-intercom#21, which exposes
sendAndWaitandregisterHandlerthrough theintercom:readyevent — allowing extensions to build request/response patterns between sessions without a direct import dependency.The forwarding works in two parts:
Root session (has TUI): On
intercom:ready, registers a handler for the"ask_user_question"content type. When a subagent sends a question, the handler renders it viaAskUserQuestionComponentand returns the user's answer as JSON.Subagent session (no TUI): When
ask_user_questionis invoked and!ctx.hasUI, the tool checks for a subagent context (PI_SUBAGENT_ORCHESTRATOR_TARGET). If present, it callssendAndWaitto forward the questions to root and awaits the reply. The answer is returned to the LLM in the same format it would have received locally.Additional details:
intercom:readyevent. If pi-intercom is not installed, the extension works exactly as before (deregisters the tool in non-interactive sessions).renderQuestionsViaUIhelper from the inline closure so both the tool handler and the intercom handler share the same rendering logic."User cancelled") instead of hanging or crashing.Flow diagram