feat: Add ask_user_question tool for interactive TUI #161
+2,573
−0
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.
Summary
Introduces a new
ask_user_questiontool that allows agents to ask users interactive multiple-choice questions through a split-panel terminal TUI. This feature is similar to Claude Code's AskUserQuestion tool, enabling agents to gather user input, preferences, and confirmations during task execution.ask.mp4
Changes Made
New Tool Package:
code_puppy/tools/ask_user_question/__init__.py- Package exports with public API (ask_user_question,register_ask_user_question, and Pydantic models)constants.py- Configuration constants including limits, timeouts, UI settings, and Unicode symbolsmodels.py- Comprehensive Pydantic models with validation:QuestionOption- Individual selectable option with label and descriptionQuestion- Question with header, options (2-6), and multi-select supportAskUserQuestionInput- Input schema supporting 1-10 questionsQuestionAnswer- Answer container with selected options and "Other" textAskUserQuestionOutput- Output with answers, cancelled/timed_out/error stateshandler.py- Main entry point with:registration.py- Tool registration with comprehensive docstring and usage examplesterminal_ui.py- Core UI state management (QuestionUIStateclass) with:tui_loop.py- Async TUI event loop with prompt_toolkit integrationrenderers.py- Panel rendering (left question tabs, right question details)theme.py- Themeable color schemes with code-puppy config integrationdemo_tui.py- Standalone demo script for testing the TUIIntegration Points
code_puppy/tools/__init__.pyfrom code_puppy.tools.ask_user_question import register_ask_user_question"ask_user_question": register_ask_user_questioncode_puppy/agents/agent_code_puppy.py"ask_user_question"toget_available_tools()listTechnical Details
Architecture
Design Decisions
Split-panel TUI: Left panel shows question headers (tabs), right panel shows current question with options. Similar to
/colorscommand UX.Pydantic validation: All input is validated with sanitization (ANSI stripping, whitespace trimming) and constraints (max lengths, option counts).
Async-first with sync wrapper: Core TUI runs async with
prompt_toolkit. Theask_user_question()function wraps withasyncio.run()for sync callers, with helpful error if called from async context.Inactivity timeout: Default 5-minute timeout with warning at 60 seconds remaining. Uses
time.monotonic()to avoid clock drift issues.CI environment detection: Automatically returns error in CI environments (GitHub Actions, GitLab CI, Jenkins, etc.) rather than hanging.
Alternate screen buffer: Uses terminal escape sequences to preserve existing terminal content during interaction.
"Other" option: Automatically adds "Other" option for custom user input, with dedicated text entry mode.
Theming integration: Colors can be customized via code-puppy's config system.
Keyboard Navigation
Validation Rules
Files Modified
code_puppy/tools/__init__.pycode_puppy/agents/agent_code_puppy.pycode_puppy/tools/ask_user_question/__init__.pycode_puppy/tools/ask_user_question/constants.pycode_puppy/tools/ask_user_question/demo_tui.pycode_puppy/tools/ask_user_question/handler.pycode_puppy/tools/ask_user_question/models.pycode_puppy/tools/ask_user_question/registration.pycode_puppy/tools/ask_user_question/renderers.pycode_puppy/tools/ask_user_question/terminal_ui.pycode_puppy/tools/ask_user_question/theme.pycode_puppy/tools/ask_user_question/tui_loop.pytests/tools/test_ask_user_question/__init__.pytests/tools/test_ask_user_question/test_handler.pytests/tools/test_ask_user_question/test_models.pyTotal: 15 files, +2,573 lines
Testing
Unit Tests Added
test_models.py(444 lines)test_handler.py(201 lines)Manual Testing
# Run the demo TUI python -m code_puppy.tools.ask_user_question.demo_tuiBreaking Changes
None. This is a purely additive feature.
Additional Notes
Usage Example
Minimalism Principles (from docstring)
Dependencies
No new dependencies added. Uses existing:
pydantic- Input/output validationprompt_toolkit- Terminal TUIrich- Panel rendering