[PR #5958] feat(new tool):Adding a new tool to opencode -> askquestion tool #11670

Closed
opened 2026-02-16 18:16:35 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/5958

State: closed
Merged: No


This PR introduces the askquestion tool, a new interactive primitive that allows the AI to pause execution and solicit structured feedback from the user via a TUI wizard very similarly to claude code's tool. It also resolves a critical race condition in the tool execution lifecycle that previously caused UI updates to be missed.

The "AskQuestion" Tool

The askquestion tool bridges the gap between the AI's autonomous execution and user intent. Instead of guessing preferences, the agent can now halts its process to ask specific configuration questions (e.g., "Which database do you prefer?", "Select features to enable").

How It Works (User Flow)

  1. Invocation The Agent calls the askquestion tool with a schema of 1-6 questions (Single Select, Multi-Select, or Confirm).
  2. Suspension The Tool registers a pending request and enters a waiting state. Execution is effectively paused, awaiting a Promise resolution.
  3. Intervention The TUI detects the waiting metadata and renders a modal overlay (Wizard) on top of the chat.
  4. Interaction The user navigates options using the keyboard (Space to toggle, Enter to confirm).
  5. Resumption Upon submission, the Promise resolves, the tool returns the structured answers to the Agent, and the AI resumes execution with the new context.

Robust State Synchronization (The Fix)

A core issue addressed in this PR was a race condition where the tool would attempt to emit metadata updates before the system had fully registered the tool execution part.

  • The execute() method ran faster than the event propagation for the tool call, causing the "Waiting for user..." signal to be lost.
  • Implemented a polling retry mechanism in src/session/prompt.ts. The system now waits (exponential backoff up to 1s) for the specific Tool Part to transition to the running state before attempting to attach metadata. This ensures the TUI guarantees valid state reads.

2. The Tool Definition (src/tool/askquestion.ts)

  • Uses Zod to strictly enforce a limit of 1-6 questions to prevent context window bloat.
  • Formats the output as a human-readable markdown string for the LLM to easily ingest the user's choices.

3. TUI & UX Enhancements

  • Reactivity The TUI (src/cli/cmd/tui/) now contains a reactive memo pendingAskQuestionFromSync that listens specifically for tool parts in the waiting state.

  • Visual Polish

    • Added distinct visual indicators for selection types: ○/● (Radio) vs [ ]/[✓] (Checkbox).
    • Implemented "Auto-Advance" logic for single-select questions to reduce keystrokes.

    Resolves #3844

Currently this tool is CLI ONLY

  • CLI TUI sends x-opencode-client: tui header
  • Web/Desktop apps don't send this header (defaults to "web")
  • Server passes client type through prompt system
  • askquestion tool is only provided to TUI clients
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/5958 **State:** closed **Merged:** No --- This PR introduces the askquestion tool, a new interactive primitive that allows the AI to pause execution and solicit structured feedback from the user via a TUI wizard very similarly to claude code's tool. It also resolves a critical race condition in the tool execution lifecycle that previously caused UI updates to be missed. ## The "AskQuestion" Tool The askquestion tool bridges the gap between the AI's autonomous execution and user intent. Instead of guessing preferences, the agent can now halts its process to ask specific configuration questions (e.g., "Which database do you prefer?", "Select features to enable"). ### How It Works (User Flow) 1. Invocation The Agent calls the `askquestion` tool with a schema of 1-6 questions (Single Select, Multi-Select, or Confirm). 2. Suspension The Tool registers a pending request and enters a `waiting` state. Execution is effectively paused, awaiting a Promise resolution. 3. Intervention The TUI detects the `waiting` metadata and renders a modal overlay (Wizard) on top of the chat. 4. Interaction The user navigates options using the keyboard (`Space` to toggle, `Enter` to confirm). 5. Resumption Upon submission, the Promise resolves, the tool returns the structured answers to the Agent, and the AI resumes execution with the new context. ### Robust State Synchronization (The Fix) A core issue addressed in this PR was a race condition where the tool would attempt to emit metadata updates before the system had fully registered the tool execution part. * The `execute()` method ran faster than the event propagation for the tool call, causing the "Waiting for user..." signal to be lost. * Implemented a polling retry mechanism in `src/session/prompt.ts`. The system now waits (exponential backoff up to 1s) for the specific Tool Part to transition to the `running` state before attempting to attach metadata. This ensures the TUI guarantees valid state reads. ### 2. The Tool Definition (`src/tool/askquestion.ts`) * Uses Zod to strictly enforce a limit of 1-6 questions to prevent context window bloat. * Formats the output as a human-readable markdown string for the LLM to easily ingest the user's choices. ### 3. TUI & UX Enhancements * Reactivity The TUI (`src/cli/cmd/tui/`) now contains a reactive memo `pendingAskQuestionFromSync` that listens specifically for tool parts in the `waiting` state. * Visual Polish * Added distinct visual indicators for selection types: `○/●` (Radio) vs `[ ]/[✓]` (Checkbox). * Implemented "Auto-Advance" logic for single-select questions to reduce keystrokes. Resolves #3844 ## Currently this tool is CLI ONLY - CLI TUI sends x-opencode-client: tui header - Web/Desktop apps don't send this header (defaults to "web") - Server passes client type through prompt system - askquestion tool is only provided to TUI clients
yindo added the pull-request label 2026-02-16 18:16:35 -05:00
yindo closed this issue 2026-02-16 18:16:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#11670