From d630ac90d1b88086e79ba595bedfe0abab66eb74 Mon Sep 17 00:00:00 2001 From: Flinn Xie Date: Wed, 6 May 2026 01:34:23 +0800 Subject: [PATCH 01/21] fix(cli): prevent TUI content duplication via transient Live and renderer routing Route progress output through the Live's render hook to fix cursor misalignment that caused content duplication. The root cause was that progress/reasoning output used a separate Console instance, bypassing Rich Live's process_renderables hook. Also fixes pre-existing issue where multiple headers printed per agent turn. Co-Authored-By: Claude Opus 4.7 --- nanobot/cli/commands.py | 46 ++++++++----- nanobot/cli/stream.py | 86 ++++++++++++++++-------- tests/cli/test_interactive_retry_wait.py | 2 +- 3 files changed, 88 insertions(+), 46 deletions(-) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 243280ed1..236d787ce 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -227,30 +227,37 @@ async def _print_interactive_response( await run_in_terminal(_write) -def _print_cli_progress_line(text: str, thinking: ThinkingSpinner | None) -> None: +def _print_cli_progress_line(text: str, thinking: ThinkingSpinner | None, renderer: StreamRenderer | None = None) -> None: """Print a CLI progress line, pausing the spinner if needed.""" if not text.strip(): return - with thinking.pause() if thinking else nullcontext(): - console.print(f" [dim]↳ {text}[/dim]") + target = renderer.console if renderer else console + pause = renderer.pause_spinner() if renderer else (thinking.pause() if thinking else nullcontext()) + with pause: + target.print(f" [dim]↳ {text}[/dim]") -async def _print_interactive_progress_line(text: str, renderer: StreamRenderer | None) -> None: - """Print an interactive progress line, pausing the renderer's spinner if needed.""" +async def _print_interactive_progress_line(text: str, thinking: ThinkingSpinner | None, renderer: StreamRenderer | None = None) -> None: + """Print an interactive progress line, pausing the spinner if needed.""" if not text.strip(): return - with renderer.pause() if renderer else nullcontext(): - await _print_interactive_line(text) + if renderer: + with renderer.pause_spinner(): + renderer.console.print(f" [dim]↳ {text}[/dim]") + else: + with thinking.pause() if thinking else nullcontext(): + await _print_interactive_line(text) async def _maybe_print_interactive_progress( msg: Any, - renderer: StreamRenderer | None, + thinking: ThinkingSpinner | None, channels_config: Any, + renderer: StreamRenderer | None = None, ) -> bool: metadata = msg.metadata or {} if metadata.get("_retry_wait"): - await _print_interactive_progress_line(msg.content, renderer) + await _print_interactive_progress_line(msg.content, thinking, renderer) return True if not metadata.get("_progress"): @@ -262,7 +269,7 @@ async def _maybe_print_interactive_progress( if channels_config and not is_tool_hint and not channels_config.send_progress: return True - await _print_interactive_progress_line(msg.content, renderer) + await _print_interactive_progress_line(msg.content, thinking, renderer) return True @@ -1121,13 +1128,15 @@ def agent( # Shared reference for progress callbacks _thinking: ThinkingSpinner | None = None - async def _cli_progress(content: str, *, tool_hint: bool = False, **_kwargs: Any) -> None: - ch = agent_loop.channels_config - if ch and tool_hint and not ch.send_tool_hints: - return - if ch and not tool_hint and not ch.send_progress: - return - _print_cli_progress_line(content, _thinking) + def _make_progress(renderer: StreamRenderer | None = None): + async def _cli_progress(content: str, *, tool_hint: bool = False, **_kwargs: Any) -> None: + ch = agent_loop.channels_config + if ch and tool_hint and not ch.send_tool_hints: + return + if ch and not tool_hint and not ch.send_progress: + return + _print_cli_progress_line(content, _thinking, renderer) + return _cli_progress if message: # Single message mode — direct call, no bus needed @@ -1135,7 +1144,7 @@ def agent( renderer = StreamRenderer(render_markdown=markdown) response = await agent_loop.process_direct( message, session_id, - on_progress=_cli_progress, + on_progress=_make_progress(renderer), on_stream=renderer.on_delta, on_stream_end=renderer.on_end, ) @@ -1206,6 +1215,7 @@ def agent( msg, renderer, agent_loop.channels_config, + renderer, ): continue diff --git a/nanobot/cli/stream.py b/nanobot/cli/stream.py index b0095f153..807c88fef 100644 --- a/nanobot/cli/stream.py +++ b/nanobot/cli/stream.py @@ -1,13 +1,15 @@ """Streaming renderer for CLI output. -Uses Rich Live with auto_refresh=False for stable, flicker-free -markdown rendering during streaming. Ellipsis mode handles overflow. +Uses Rich Live with ``transient=True`` for in-place markdown updates during +streaming. After the live display stops, a final clean render is printed +so the content persists on screen. ``transient=True`` ensures the live +area is erased before ``stop()`` returns, avoiding the duplication bug +that plagued earlier approaches. """ from __future__ import annotations import sys -import time from rich.console import Console from rich.live import Live @@ -67,27 +69,38 @@ class ThinkingSpinner: class StreamRenderer: - """Rich Live streaming with markdown. auto_refresh=False avoids render races. + """Streaming renderer with Rich Live for in-place updates. - Deltas arrive pre-filtered (no tags) from the agent loop. + During streaming: updates content in-place via Rich Live. + On end: stops Live (transient=True erases it), then prints final render. Flow per round: - spinner -> first visible delta -> header + Live renders -> - on_end -> Live stops (content stays on screen) + spinner -> first delta -> header + Live updates -> + on_end -> stop Live + final render """ def __init__(self, render_markdown: bool = True, show_spinner: bool = True): self._md = render_markdown self._show_spinner = show_spinner self._buf = "" - self._live: Live | None = None - self._t = 0.0 self.streamed = False + self._header_printed = False + self._console = _make_console() + self._live: Live | None = None self._spinner: ThinkingSpinner | None = None self._start_spinner() - def _render(self): - return Markdown(self._buf) if self._md and self._buf else Text(self._buf or "") + def _renderable(self): + """Create a renderable from the current buffer.""" + if self._md and self._buf: + return Markdown(self._buf) + return Text(self._buf or "") + + def _render_str(self) -> str: + """Render current buffer to a plain string via Rich.""" + with self._console.capture() as cap: + self._console.print(self._renderable()) + return cap.get() def _start_spinner(self) -> None: if self._show_spinner: @@ -99,36 +112,55 @@ class StreamRenderer: self._spinner.__exit__(None, None, None) self._spinner = None + @property + def console(self) -> Console: + """Expose the Live's console so external print functions can use it.""" + return self._console + + def pause_spinner(self): + """Context manager: temporarily stop spinner for clean output.""" + if self._spinner: + return self._spinner.pause() + from contextlib import nullcontext + return nullcontext() + async def on_delta(self, delta: str) -> None: self.streamed = True self._buf += delta - if self._live is None: - if not self._buf.strip(): - return - self._stop_spinner() - c = _make_console() - c.print() - c.print(f"[cyan]{__logo__} nanobot[/cyan]") - self._live = Live(self._render(), console=c, auto_refresh=False) + if not self._header_printed and self._buf.strip(): + self._console.print() + self._console.print(f"[cyan]{__logo__} nanobot[/cyan]") + self._header_printed = True + self._stop_spinner() + if not self._live: + self._live = Live( + self._renderable(), + console=self._console, + auto_refresh=False, + transient=True, + ) self._live.start() - now = time.monotonic() - if (now - self._t) > 0.15: - self._live.update(self._render()) - self._live.refresh() - self._t = now + else: + self._live.update(self._renderable()) + self._live.refresh() async def on_end(self, *, resuming: bool = False) -> None: if self._live: - self._live.update(self._render()) + # Double-refresh to sync _shape before stop() calls refresh(). + self._live.refresh() + self._live.update(self._renderable()) self._live.refresh() self._live.stop() self._live = None self._stop_spinner() + if self._header_printed and self._buf.strip(): + # Print final rendered content (persists after Live is gone). + out = sys.stdout + out.write(self._render_str()) + out.flush() if resuming: self._buf = "" self._start_spinner() - else: - _make_console().print() def stop_for_input(self) -> None: """Stop spinner before user input to avoid prompt_toolkit conflicts.""" diff --git a/tests/cli/test_interactive_retry_wait.py b/tests/cli/test_interactive_retry_wait.py index 5cc217c56..e58102dcd 100644 --- a/tests/cli/test_interactive_retry_wait.py +++ b/tests/cli/test_interactive_retry_wait.py @@ -17,7 +17,7 @@ async def test_interactive_retry_wait_is_rendered_as_progress_even_when_progress metadata={"_retry_wait": True}, ) - async def fake_print(text: str, active_thinking: object | None) -> None: + async def fake_print(text: str, active_thinking: object | None, renderer=None) -> None: calls.append((text, active_thinking)) with patch("nanobot.cli.commands._print_interactive_progress_line", side_effect=fake_print): From 3a27af0018b106f4b9212289c75da03d3e67da62 Mon Sep 17 00:00:00 2001 From: Flinn Xie Date: Wed, 6 May 2026 01:35:53 +0800 Subject: [PATCH 02/21] feat(cli): display model reasoning content during streaming MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add show_reasoning config (default: False) to display model thinking/reasoning content in the TUI during streaming. Reasoning is emitted via a new emit_reasoning hook on AgentHook, gated by the channels config. Display uses ✻ prefix with dim italic styling. Co-Authored-By: Claude Opus 4.7 --- nanobot/agent/hook.py | 6 +++ nanobot/agent/loop.py | 11 +++++ nanobot/agent/runner.py | 3 ++ nanobot/cli/commands.py | 27 +++++++++-- nanobot/cli/stream.py | 11 ++--- nanobot/config/schema.py | 1 + tests/agent/test_hook_composite.py | 23 ++++++++- tests/cli/test_cli_input.py | 54 ++++++++++++++++++++-- tests/cli/test_interactive_retry_wait.py | 59 ++++++++++++++++++++++++ 9 files changed, 182 insertions(+), 13 deletions(-) diff --git a/nanobot/agent/hook.py b/nanobot/agent/hook.py index d0106cfb6..5e4ea4d4d 100644 --- a/nanobot/agent/hook.py +++ b/nanobot/agent/hook.py @@ -48,6 +48,9 @@ class AgentHook: async def before_execute_tools(self, context: AgentHookContext) -> None: pass + async def emit_reasoning(self, reasoning_content: str | None) -> None: + pass + async def after_iteration(self, context: AgentHookContext) -> None: pass @@ -95,6 +98,9 @@ class CompositeHook(AgentHook): async def before_execute_tools(self, context: AgentHookContext) -> None: await self._for_each_hook_safe("before_execute_tools", context) + async def emit_reasoning(self, reasoning_content: str | None) -> None: + await self._for_each_hook_safe("emit_reasoning", reasoning_content) + async def after_iteration(self, context: AgentHookContext) -> None: await self._for_each_hook_safe("after_iteration", context) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 330c82357..e12bf53c9 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -155,6 +155,14 @@ class _LoopHook(AgentHook): session_key=self._session_key, ) + async def emit_reasoning(self, reasoning_content: str | None) -> None: + """Send reasoning/thinking content as progress before the main answer.""" + ch = self._loop.channels_config + if not ch or not ch.show_reasoning: + return + if self._on_progress and reasoning_content: + await self._on_progress(reasoning_content, reasoning=True) + async def after_iteration(self, context: AgentHookContext) -> None: if ( self._on_progress @@ -1114,10 +1122,13 @@ class AgentLoop: *, tool_hint: bool = False, tool_events: list[dict[str, Any]] | None = None, + reasoning: bool = False, ) -> None: meta = dict(msg.metadata or {}) meta["_progress"] = True meta["_tool_hint"] = tool_hint + if reasoning: + meta["_reasoning"] = True if tool_events: meta["_tool_events"] = tool_events await self.bus.publish_outbound( diff --git a/nanobot/agent/runner.py b/nanobot/agent/runner.py index 7fe92ad51..2ff2cf045 100644 --- a/nanobot/agent/runner.py +++ b/nanobot/agent/runner.py @@ -282,6 +282,9 @@ class AgentRunner: context.tool_calls = list(response.tool_calls) self._accumulate_usage(usage, raw_usage) + if response.reasoning_content: + await hook.emit_reasoning(response.reasoning_content) + if response.should_execute_tools: tool_calls = list(response.tool_calls) ask_index = next((i for i, tc in enumerate(tool_calls) if tc.name == "ask_user"), None) diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index 236d787ce..1c835962a 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -237,6 +237,16 @@ def _print_cli_progress_line(text: str, thinking: ThinkingSpinner | None, render target.print(f" [dim]↳ {text}[/dim]") +def _print_cli_reasoning(text: str, thinking: ThinkingSpinner | None, renderer: StreamRenderer | None = None) -> None: + """Print reasoning/thinking content in a distinct style.""" + if not text.strip(): + return + target = renderer.console if renderer else console + pause = renderer.pause_spinner() if renderer else (thinking.pause() if thinking else nullcontext()) + with pause: + target.print(f"[dim italic]✻ {text}[/dim italic]") + + async def _print_interactive_progress_line(text: str, thinking: ThinkingSpinner | None, renderer: StreamRenderer | None = None) -> None: """Print an interactive progress line, pausing the spinner if needed.""" if not text.strip(): @@ -264,12 +274,18 @@ async def _maybe_print_interactive_progress( return False is_tool_hint = metadata.get("_tool_hint", False) + is_reasoning = metadata.get("_reasoning", False) if channels_config and is_tool_hint and not channels_config.send_tool_hints: return True if channels_config and not is_tool_hint and not channels_config.send_progress: return True + if is_reasoning and channels_config and not channels_config.show_reasoning: + return True - await _print_interactive_progress_line(msg.content, thinking, renderer) + if is_reasoning: + _print_cli_reasoning(msg.content, thinking, renderer) + else: + await _print_interactive_progress_line(msg.content, thinking, renderer) return True @@ -1129,13 +1145,18 @@ def agent( _thinking: ThinkingSpinner | None = None def _make_progress(renderer: StreamRenderer | None = None): - async def _cli_progress(content: str, *, tool_hint: bool = False, **_kwargs: Any) -> None: + async def _cli_progress(content: str, *, tool_hint: bool = False, reasoning: bool = False, **_kwargs: Any) -> None: ch = agent_loop.channels_config if ch and tool_hint and not ch.send_tool_hints: return if ch and not tool_hint and not ch.send_progress: return - _print_cli_progress_line(content, _thinking, renderer) + if reasoning and ch and not ch.show_reasoning: + return + if reasoning: + _print_cli_reasoning(content, _thinking, renderer) + else: + _print_cli_progress_line(content, _thinking, renderer) return _cli_progress if message: diff --git a/nanobot/cli/stream.py b/nanobot/cli/stream.py index 807c88fef..ec7f0a96c 100644 --- a/nanobot/cli/stream.py +++ b/nanobot/cli/stream.py @@ -84,7 +84,6 @@ class StreamRenderer: self._show_spinner = show_spinner self._buf = "" self.streamed = False - self._header_printed = False self._console = _make_console() self._live: Live | None = None self._spinner: ThinkingSpinner | None = None @@ -127,12 +126,12 @@ class StreamRenderer: async def on_delta(self, delta: str) -> None: self.streamed = True self._buf += delta - if not self._header_printed and self._buf.strip(): + if self._live is None: + if not self._buf.strip(): + return + self._stop_spinner() self._console.print() self._console.print(f"[cyan]{__logo__} nanobot[/cyan]") - self._header_printed = True - self._stop_spinner() - if not self._live: self._live = Live( self._renderable(), console=self._console, @@ -153,7 +152,7 @@ class StreamRenderer: self._live.stop() self._live = None self._stop_spinner() - if self._header_printed and self._buf.strip(): + if self._buf.strip(): # Print final rendered content (persists after Live is gone). out = sys.stdout out.write(self._render_str()) diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index 47f2babcd..66a7a75aa 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -27,6 +27,7 @@ class ChannelsConfig(Base): send_progress: bool = True # stream agent's text progress to the channel send_tool_hints: bool = False # stream tool-call hints (e.g. read_file("…")) + show_reasoning: bool = False # show model reasoning/thinking content send_max_retries: int = Field(default=3, ge=0, le=10) # Max delivery attempts (initial send included) transcription_provider: str = "groq" # Voice transcription backend: "groq" or "openai" transcription_language: str | None = Field(default=None, pattern=r"^[a-z]{2,3}$") # Optional ISO-639-1 hint for audio transcription diff --git a/tests/agent/test_hook_composite.py b/tests/agent/test_hook_composite.py index 8971d48ec..9b6c2820d 100644 --- a/tests/agent/test_hook_composite.py +++ b/tests/agent/test_hook_composite.py @@ -13,6 +13,17 @@ def _ctx() -> AgentHookContext: return AgentHookContext(iteration=0, messages=[]) +# --------------------------------------------------------------------------- +# Base AgentHook emit_reasoning: no-op +# --------------------------------------------------------------------------- + + +@pytest.mark.asyncio +async def test_base_hook_emit_reasoning_is_noop(): + hook = AgentHook() + await hook.emit_reasoning("should not raise") + + # --------------------------------------------------------------------------- # Fan-out: every hook is called in order # --------------------------------------------------------------------------- @@ -45,6 +56,9 @@ async def test_composite_fans_out_all_async_methods(): async def before_iteration(self, context: AgentHookContext) -> None: events.append("before_iteration") + async def emit_reasoning(self, reasoning_content: str | None) -> None: + events.append(f"emit_reasoning:{reasoning_content}") + async def on_stream(self, context: AgentHookContext, delta: str) -> None: events.append(f"on_stream:{delta}") @@ -61,6 +75,7 @@ async def test_composite_fans_out_all_async_methods(): ctx = _ctx() await hook.before_iteration(ctx) + await hook.emit_reasoning("thinking...") await hook.on_stream(ctx, "hi") await hook.on_stream_end(ctx, resuming=True) await hook.before_execute_tools(ctx) @@ -68,6 +83,7 @@ async def test_composite_fans_out_all_async_methods(): assert events == [ "before_iteration", "before_iteration", + "emit_reasoning:thinking...", "emit_reasoning:thinking...", "on_stream:hi", "on_stream:hi", "on_stream_end:True", "on_stream_end:True", "before_execute_tools", "before_execute_tools", @@ -120,6 +136,8 @@ async def test_composite_error_isolation_all_async(): calls: list[str] = [] class Bad(AgentHook): + async def emit_reasoning(self, reasoning_content): + raise RuntimeError("err") async def on_stream_end(self, context, *, resuming): raise RuntimeError("err") async def before_execute_tools(self, context): @@ -128,6 +146,8 @@ async def test_composite_error_isolation_all_async(): raise RuntimeError("err") class Good(AgentHook): + async def emit_reasoning(self, reasoning_content): + calls.append("emit_reasoning") async def on_stream_end(self, context, *, resuming): calls.append("on_stream_end") async def before_execute_tools(self, context): @@ -137,10 +157,11 @@ async def test_composite_error_isolation_all_async(): hook = CompositeHook([Bad(), Good()]) ctx = _ctx() + await hook.emit_reasoning("test") await hook.on_stream_end(ctx, resuming=False) await hook.before_execute_tools(ctx) await hook.after_iteration(ctx) - assert calls == ["on_stream_end", "before_execute_tools", "after_iteration"] + assert calls == ["emit_reasoning", "on_stream_end", "before_execute_tools", "after_iteration"] # --------------------------------------------------------------------------- diff --git a/tests/cli/test_cli_input.py b/tests/cli/test_cli_input.py index e648e818c..69293f4b8 100644 --- a/tests/cli/test_cli_input.py +++ b/tests/cli/test_cli_input.py @@ -156,17 +156,65 @@ def test_stream_renderer_stop_for_input_stops_spinner(): # Create renderer with mocked console with patch.object(stream_mod, "_make_console", return_value=mock_console): renderer = stream_mod.StreamRenderer(show_spinner=True) - + # Verify spinner started spinner.start.assert_called_once() - + # Stop for input renderer.stop_for_input() - + # Verify spinner stopped spinner.stop.assert_called_once() +@pytest.mark.asyncio +async def test_on_end_writes_final_content_to_stdout_after_stopping_live(): + """on_end should stop Live (transient erases it) then print final content to stdout.""" + mock_live = MagicMock() + mock_console = MagicMock() + mock_console.capture.return_value.__enter__ = MagicMock( + return_value=MagicMock(get=lambda: "final output\n") + ) + mock_console.capture.return_value.__exit__ = MagicMock(return_value=False) + + with patch.object(stream_mod, "_make_console", return_value=mock_console): + renderer = stream_mod.StreamRenderer(show_spinner=False) + renderer._live = mock_live + renderer._buf = "final output" + + written: list[str] = [] + with patch("sys.stdout") as mock_stdout: + mock_stdout.write = lambda s: written.append(s) + mock_stdout.flush = MagicMock() + await renderer.on_end() + + mock_live.stop.assert_called_once() + assert renderer._live is None + assert written == ["final output\n"] + + +@pytest.mark.asyncio +async def test_on_end_resuming_clears_buffer_and_restarts_spinner(): + """on_end(resuming=True) should reset state for the next iteration.""" + spinner = MagicMock() + mock_console = MagicMock() + mock_console.status.return_value = spinner + mock_console.capture.return_value.__enter__ = MagicMock( + return_value=MagicMock(get=lambda: "") + ) + mock_console.capture.return_value.__exit__ = MagicMock(return_value=False) + + with patch.object(stream_mod, "_make_console", return_value=mock_console): + renderer = stream_mod.StreamRenderer(show_spinner=True) + renderer._buf = "some content" + + await renderer.on_end(resuming=True) + + assert renderer._buf == "" + # Spinner should have been restarted (start called twice: __init__ + resuming) + assert spinner.start.call_count == 2 + + def test_make_console_force_terminal_when_stdout_is_tty(): """Console should set force_terminal=True when stdout is a TTY (rich output).""" import sys diff --git a/tests/cli/test_interactive_retry_wait.py b/tests/cli/test_interactive_retry_wait.py index e58102dcd..e693b057c 100644 --- a/tests/cli/test_interactive_retry_wait.py +++ b/tests/cli/test_interactive_retry_wait.py @@ -29,3 +29,62 @@ async def test_interactive_retry_wait_is_rendered_as_progress_even_when_progress assert handled is True assert calls == [("Model request failed, retry in 2s (attempt 1).", thinking)] + + +@pytest.mark.asyncio +async def test_reasoning_displayed_when_show_reasoning_enabled(): + """Reasoning content should be displayed when show_reasoning is True.""" + calls: list[str] = [] + channels_config = SimpleNamespace( + send_progress=True, send_tool_hints=False, show_reasoning=True, + ) + msg = SimpleNamespace( + content="Let me think about this...", + metadata={"_progress": True, "_reasoning": True}, + ) + + with patch("nanobot.cli.commands._print_cli_reasoning", side_effect=lambda t, th, r=None: calls.append(t)): + handled = await commands._maybe_print_interactive_progress(msg, None, channels_config) + + assert handled is True + assert calls == ["Let me think about this..."] + + +@pytest.mark.asyncio +async def test_reasoning_hidden_when_show_reasoning_disabled(): + """Reasoning content should be suppressed when show_reasoning is False.""" + channels_config = SimpleNamespace( + send_progress=True, send_tool_hints=False, show_reasoning=False, + ) + msg = SimpleNamespace( + content="Let me think about this...", + metadata={"_progress": True, "_reasoning": True}, + ) + + with patch("nanobot.cli.commands._print_cli_reasoning") as mock_reasoning: + handled = await commands._maybe_print_interactive_progress(msg, None, channels_config) + + assert handled is True + mock_reasoning.assert_not_called() + + +@pytest.mark.asyncio +async def test_non_reasoning_progress_not_affected_by_show_reasoning(): + """Regular progress lines should display regardless of show_reasoning.""" + calls: list[str] = [] + channels_config = SimpleNamespace( + send_progress=True, send_tool_hints=False, show_reasoning=False, + ) + msg = SimpleNamespace( + content="working on it...", + metadata={"_progress": True}, + ) + + async def fake_print(text: str, thinking=None, renderer=None): + calls.append(text) + + with patch("nanobot.cli.commands._print_interactive_progress_line", side_effect=fake_print): + handled = await commands._maybe_print_interactive_progress(msg, None, channels_config) + + assert handled is True + assert calls == ["working on it..."] From 9e15925cf4d73767d5a3163116b5f7f8eeedee29 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Tue, 12 May 2026 18:36:03 +0800 Subject: [PATCH 03/21] refactor(agent): remove ask_user tool The ask_user tool used AskUserInterrupt(BaseException) for mid-turn blocking, creating heavy coupling across runner, loop, and session management. The model now asks questions naturally in response text, the turn ends normally, and the user's next message starts a new turn with session history providing continuity. Removed: - nanobot/agent/tools/ask.py (tool, interrupt, helpers) - tests/agent/test_ask_user.py - webui/src/components/thread/AskUserPrompt.tsx - AskUserInterrupt handling in runner.py - Dual-path message building in loop.py - Pending ask detection via history scanning - button_prompt/buttons emission in WebSocket channel - ask_user references in Slack channel docstrings Preserved (MessageTool uses these independently): - OutboundMessage.buttons field - Channel button rendering (Telegram, Slack, WebSocket) --- nanobot/agent/loop.py | 44 +--- nanobot/agent/runner.py | 35 +-- nanobot/agent/tools/ask.py | 136 ---------- nanobot/channels/slack.py | 6 +- nanobot/channels/websocket.py | 13 - nanobot/skills/update-setup/SKILL.md | 8 +- tests/agent/test_ask_user.py | 241 ------------------ tests/channels/test_slack_channel.py | 4 +- tests/channels/test_websocket_channel.py | 4 +- tests/tools/test_tool_loader.py | 2 +- webui/src/components/thread/AskUserPrompt.tsx | 108 -------- webui/src/components/thread/ThreadShell.tsx | 23 -- webui/src/hooks/useNanobotStream.ts | 3 +- webui/src/lib/types.ts | 5 - webui/src/tests/thread-shell.test.tsx | 42 --- webui/src/tests/useNanobotStream.test.tsx | 23 -- 16 files changed, 24 insertions(+), 673 deletions(-) delete mode 100644 nanobot/agent/tools/ask.py delete mode 100644 tests/agent/test_ask_user.py delete mode 100644 webui/src/components/thread/AskUserPrompt.tsx diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index c73013379..476a2caf2 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -22,12 +22,6 @@ from nanobot.agent.memory import Consolidator, Dream from nanobot.agent import model_presets as preset_helpers from nanobot.agent.runner import _MAX_INJECTIONS_PER_TURN, AgentRunner, AgentRunSpec from nanobot.agent.subagent import SubagentManager -from nanobot.agent.tools.ask import ( - ask_user_options_from_messages, - ask_user_outbound, - ask_user_tool_result_messages, - pending_ask_user_id, -) from nanobot.agent.tools.file_state import FileStateStore, bind_file_states, reset_file_states from nanobot.agent.tools.message import MessageTool from nanobot.agent.tools.registry import ToolRegistry @@ -693,7 +687,6 @@ class AgentLoop: self, msg: InboundMessage, session: Session, - pending_ask_id: str | None, ) -> bool: """Persist the triggering user message before the turn starts. @@ -701,7 +694,7 @@ class AgentLoop: """ media_paths = [p for p in (msg.media or []) if isinstance(p, str) and p] has_text = isinstance(msg.content, str) and msg.content.strip() - if not pending_ask_id and (has_text or media_paths): + if has_text or media_paths: extra: dict[str, Any] = {"media": list(media_paths)} if media_paths else {} text = msg.content if isinstance(msg.content, str) else "" session.add_message("user", text, **extra) @@ -715,21 +708,9 @@ class AgentLoop: msg: InboundMessage, session: Session, history: list[dict[str, Any]], - pending_ask_id: str | None, pending_summary: str | None, ) -> list[dict[str, Any]]: """Build the initial message list for the LLM turn.""" - if pending_ask_id: - system_prompt = self.context.build_system_prompt( - channel=msg.channel, - session_summary=pending_summary, - ) - return ask_user_tool_result_messages( - system_prompt, - history, - pending_ask_id, - image_generation_prompt(msg.content, msg.metadata), - ) return self.context.build_messages( history=history, current_message=image_generation_prompt(msg.content, msg.metadata), @@ -1237,12 +1218,7 @@ class AgentLoop: replay_max_messages=self._max_messages, ) ) - options = ask_user_options_from_messages(all_msgs) if stop_reason == "ask_user" else [] - content, buttons = ask_user_outbound( - final_content or "Background task completed.", - options, - channel, - ) + content = final_content or "Background task completed." outbound_metadata: dict[str, Any] = {} if channel == "slack" and key.startswith("slack:") and key.count(":") >= 2: outbound_metadata["slack"] = {"thread_ts": key.split(":", 2)[2]} @@ -1252,7 +1228,6 @@ class AgentLoop: channel=channel, chat_id=chat_id, content=content, - buttons=buttons, metadata=outbound_metadata, ) @@ -1365,21 +1340,15 @@ class AgentLoop: logger.info("Response to {}:{}: {}", msg.channel, msg.sender_id, preview) meta = dict(msg.metadata or {}) - content, buttons = ask_user_outbound( - final_content, - ask_user_options_from_messages(all_msgs) if stop_reason == "ask_user" else [], - msg.channel, - ) - if on_stream is not None and stop_reason not in {"ask_user", "error", "tool_error"}: + if on_stream is not None and stop_reason not in {"error", "tool_error"}: meta["_streamed"] = True return OutboundMessage( channel=msg.channel, chat_id=msg.chat_id, - content=content, + content=final_content, media=generated_media, metadata=meta, - buttons=buttons, ) async def _state_restore(self, ctx: TurnContext) -> TurnState: @@ -1446,12 +1415,11 @@ class AgentLoop: } ctx.history = ctx.session.get_history(**_hist_kwargs) - pending_ask_id = pending_ask_user_id(ctx.history) ctx.initial_messages = self._build_initial_messages( - ctx.msg, ctx.session, ctx.history, pending_ask_id, ctx.pending_summary + ctx.msg, ctx.session, ctx.history, ctx.pending_summary ) ctx.user_persisted_early = self._persist_user_message_early( - ctx.msg, ctx.session, pending_ask_id + ctx.msg, ctx.session ) if ctx.on_progress is None: diff --git a/nanobot/agent/runner.py b/nanobot/agent/runner.py index 7fe92ad51..9ea0d26de 100644 --- a/nanobot/agent/runner.py +++ b/nanobot/agent/runner.py @@ -13,7 +13,6 @@ from typing import Any from loguru import logger from nanobot.agent.hook import AgentHook, AgentHookContext -from nanobot.agent.tools.ask import AskUserInterrupt from nanobot.agent.tools.registry import ToolRegistry from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest from nanobot.utils.helpers import ( @@ -283,22 +282,18 @@ class AgentRunner: self._accumulate_usage(usage, raw_usage) if response.should_execute_tools: - tool_calls = list(response.tool_calls) - ask_index = next((i for i, tc in enumerate(tool_calls) if tc.name == "ask_user"), None) - if ask_index is not None: - tool_calls = tool_calls[: ask_index + 1] - context.tool_calls = list(tool_calls) + context.tool_calls = list(response.tool_calls) if hook.wants_streaming(): await hook.on_stream_end(context, resuming=True) assistant_message = build_assistant_message( response.content or "", - tool_calls=[tc.to_openai_tool_call() for tc in tool_calls], + tool_calls=[tc.to_openai_tool_call() for tc in response.tool_calls], reasoning_content=response.reasoning_content, thinking_blocks=response.thinking_blocks, ) messages.append(assistant_message) - tools_used.extend(tc.name for tc in tool_calls) + tools_used.extend(tc.name for tc in response.tool_calls) await self._emit_checkpoint( spec, { @@ -307,7 +302,7 @@ class AgentRunner: "model": spec.model, "assistant_message": assistant_message, "completed_tool_results": [], - "pending_tool_calls": [tc.to_openai_tool_call() for tc in tool_calls], + "pending_tool_calls": [tc.to_openai_tool_call() for tc in response.tool_calls], }, ) @@ -315,7 +310,7 @@ class AgentRunner: results, new_events, fatal_error = await self._execute_tools( spec, - tool_calls, + response.tool_calls, external_lookup_counts, workspace_violation_counts, ) @@ -323,9 +318,7 @@ class AgentRunner: context.tool_results = list(results) context.tool_events = list(new_events) completed_tool_results: list[dict[str, Any]] = [] - for tool_call, result in zip(tool_calls, results): - if isinstance(fatal_error, AskUserInterrupt) and tool_call.name == "ask_user": - continue + for tool_call, result in zip(response.tool_calls, results): tool_message = { "role": "tool", "tool_call_id": tool_call.id, @@ -340,15 +333,6 @@ class AgentRunner: messages.append(tool_message) completed_tool_results.append(tool_message) if fatal_error is not None: - if isinstance(fatal_error, AskUserInterrupt): - final_content = fatal_error.question - stop_reason = "ask_user" - context.final_content = final_content - context.stop_reason = stop_reason - if hook.wants_streaming(): - await hook.on_stream_end(context, resuming=False) - await hook.after_iteration(context) - break error = f"Error: {type(fatal_error).__name__}: {fatal_error}" final_content = error stop_reason = "tool_error" @@ -724,10 +708,6 @@ class AgentRunner: ) tool_results.append(result) batch_results.append(result) - if isinstance(result[2], AskUserInterrupt): - break - if any(isinstance(error, AskUserInterrupt) for _, _, error in batch_results): - break results: list[Any] = [] events: list[dict[str, str]] = [] @@ -799,9 +779,6 @@ class AgentRunner: "status": "error", "detail": str(exc), } - if isinstance(exc, AskUserInterrupt): - event["status"] = "waiting" - return "", event, exc payload = f"Error: {type(exc).__name__}: {exc}" handled = self._classify_violation( raw_text=str(exc), diff --git a/nanobot/agent/tools/ask.py b/nanobot/agent/tools/ask.py deleted file mode 100644 index db8c83a84..000000000 --- a/nanobot/agent/tools/ask.py +++ /dev/null @@ -1,136 +0,0 @@ -"""Tool for pausing a turn until the user answers.""" - -import json -from typing import Any - -from nanobot.agent.tools.base import Tool, tool_parameters -from nanobot.agent.tools.schema import ArraySchema, StringSchema, tool_parameters_schema - -STRUCTURED_BUTTON_CHANNELS = frozenset({"telegram", "websocket"}) - - -class AskUserInterrupt(BaseException): - """Internal signal: the runner should stop and wait for user input.""" - - def __init__(self, question: str, options: list[str] | None = None) -> None: - self.question = question - self.options = [str(option) for option in (options or []) if str(option)] - super().__init__(question) - - -@tool_parameters( - tool_parameters_schema( - question=StringSchema( - "The question to ask before continuing. Use this only when the task needs the user's answer." - ), - options=ArraySchema( - StringSchema("A possible answer label"), - description="Optional choices. The user may still reply with free text.", - ), - required=["question"], - ) -) -class AskUserTool(Tool): - """Ask the user a blocking question.""" - - @property - def name(self) -> str: - return "ask_user" - - @property - def description(self) -> str: - return ( - "Pause and ask the user a question when their answer is required to continue. " - "Use options for likely answers; the user's reply, typed or selected, is returned as the tool result. " - "For non-blocking notifications or buttons, use the message tool instead." - ) - - @property - def exclusive(self) -> bool: - return True - - async def execute(self, question: str, options: list[str] | None = None, **_: Any) -> Any: - raise AskUserInterrupt(question=question, options=options) - - -def _tool_call_name(tool_call: dict[str, Any]) -> str: - function = tool_call.get("function") - if isinstance(function, dict) and isinstance(function.get("name"), str): - return function["name"] - name = tool_call.get("name") - return name if isinstance(name, str) else "" - - -def _tool_call_arguments(tool_call: dict[str, Any]) -> dict[str, Any]: - function = tool_call.get("function") - raw = function.get("arguments") if isinstance(function, dict) else tool_call.get("arguments") - if isinstance(raw, dict): - return raw - if isinstance(raw, str): - try: - parsed = json.loads(raw) - except json.JSONDecodeError: - return {} - return parsed if isinstance(parsed, dict) else {} - return {} - - -def pending_ask_user_id(history: list[dict[str, Any]]) -> str | None: - pending: dict[str, str] = {} - for message in history: - if message.get("role") == "assistant": - for tool_call in message.get("tool_calls") or []: - if isinstance(tool_call, dict) and isinstance(tool_call.get("id"), str): - pending[tool_call["id"]] = _tool_call_name(tool_call) - elif message.get("role") == "tool": - tool_call_id = message.get("tool_call_id") - if isinstance(tool_call_id, str): - pending.pop(tool_call_id, None) - for tool_call_id, name in reversed(pending.items()): - if name == "ask_user": - return tool_call_id - return None - - -def ask_user_tool_result_messages( - system_prompt: str, - history: list[dict[str, Any]], - tool_call_id: str, - content: str, -) -> list[dict[str, Any]]: - return [ - {"role": "system", "content": system_prompt}, - *history, - { - "role": "tool", - "tool_call_id": tool_call_id, - "name": "ask_user", - "content": content, - }, - ] - - -def ask_user_options_from_messages(messages: list[dict[str, Any]]) -> list[str]: - for message in reversed(messages): - if message.get("role") != "assistant": - continue - for tool_call in reversed(message.get("tool_calls") or []): - if not isinstance(tool_call, dict) or _tool_call_name(tool_call) != "ask_user": - continue - options = _tool_call_arguments(tool_call).get("options") - if isinstance(options, list): - return [str(option) for option in options if isinstance(option, str)] - return [] - - -def ask_user_outbound( - content: str | None, - options: list[str], - channel: str, -) -> tuple[str | None, list[list[str]]]: - if not options: - return content, [] - if channel in STRUCTURED_BUTTON_CHANNELS: - return content, [options] - option_text = "\n".join(f"{index}. {option}" for index, option in enumerate(options, 1)) - return f"{content}\n\n{option_text}" if content else option_text, [] diff --git a/nanobot/channels/slack.py b/nanobot/channels/slack.py index dc8899861..be3172bff 100644 --- a/nanobot/channels/slack.py +++ b/nanobot/channels/slack.py @@ -471,7 +471,7 @@ class SlackChannel(BaseChannel): return preview.startswith(_HTML_DOWNLOAD_PREFIXES) async def _on_block_action(self, client: SocketModeClient, req: SocketModeRequest) -> None: - """Handle button clicks from ask_user blocks.""" + """Handle button clicks from inline action buttons.""" await client.send_socket_mode_response(SocketModeResponse(envelope_id=req.envelope_id)) payload = req.payload or {} actions = payload.get("actions") or [] @@ -568,7 +568,7 @@ class SlackChannel(BaseChannel): @staticmethod def _build_button_blocks(text: str, buttons: list[list[str]]) -> list[dict[str, Any]]: - """Build Slack Block Kit blocks with action buttons for ask_user choices.""" + """Build Slack Block Kit blocks with action buttons.""" blocks: list[dict[str, Any]] = [ {"type": "section", "text": {"type": "mrkdwn", "text": text[:3000]}}, ] @@ -579,7 +579,7 @@ class SlackChannel(BaseChannel): "type": "button", "text": {"type": "plain_text", "text": label[:75]}, "value": label[:75], - "action_id": f"ask_user_{label[:50]}", + "action_id": f"btn_{label[:50]}", }) if elements: blocks.append({"type": "actions", "elements": elements[:25]}) diff --git a/nanobot/channels/websocket.py b/nanobot/channels/websocket.py index 86a1e9654..76ca513d0 100644 --- a/nanobot/channels/websocket.py +++ b/nanobot/channels/websocket.py @@ -55,14 +55,6 @@ def _normalize_config_path(path: str) -> str: return _strip_trailing_slash(path) -def _append_buttons_as_text(text: str, buttons: list[list[str]]) -> str: - labels = [label for row in buttons for label in row if label] - if not labels: - return text - fallback = "\n".join(f"{index}. {label}" for index, label in enumerate(labels, 1)) - return f"{text}\n\n{fallback}" if text else fallback - - class WebSocketConfig(Base): """WebSocket server channel configuration. @@ -1468,16 +1460,11 @@ class WebSocketChannel(BaseChannel): await self.send_session_updated(msg.chat_id) return text = msg.content - if msg.buttons: - text = _append_buttons_as_text(text, msg.buttons) payload: dict[str, Any] = { "event": "message", "chat_id": msg.chat_id, "text": text, } - if msg.buttons: - payload["buttons"] = msg.buttons - payload["button_prompt"] = msg.content if msg.media: payload["media"] = msg.media urls: list[dict[str, str]] = [] diff --git a/nanobot/skills/update-setup/SKILL.md b/nanobot/skills/update-setup/SKILL.md index 7e9d5cc60..0838168f5 100644 --- a/nanobot/skills/update-setup/SKILL.md +++ b/nanobot/skills/update-setup/SKILL.md @@ -11,7 +11,7 @@ Generate a personalized upgrade skill for this workspace. Use `read_file` to check if `skills/update/SKILL.md` already exists in the workspace. -If it exists, use `ask_user` to ask: "An upgrade skill already exists. Reconfigure?" with options ["yes", "no"]. If no, stop here. +If it exists, ask the user: "An upgrade skill already exists. Reconfigure?" Wait for the user's reply. If no, stop here. ## Step 2: Current Version and Install Clues @@ -38,9 +38,9 @@ answer or confirmation, not from inference alone. If you cannot get a clear answer, stop and ask the user to rerun this setup when they know how nanobot was installed. -Use `ask_user` for the questions below, one question per call. If `ask_user` is -not available or cannot collect the answer, ask in normal chat and stop without -writing the skill. +Ask the user the questions below, one at a time, in your response text. Wait for +the user's reply before proceeding to the next question. If you cannot get a clear +answer, stop without writing the skill. **Question 1 — Install method:** diff --git a/tests/agent/test_ask_user.py b/tests/agent/test_ask_user.py deleted file mode 100644 index a192ee4a6..000000000 --- a/tests/agent/test_ask_user.py +++ /dev/null @@ -1,241 +0,0 @@ -import asyncio -from unittest.mock import MagicMock - -import pytest - -from nanobot.agent.loop import AgentLoop -from nanobot.agent.runner import AgentRunner, AgentRunSpec -from nanobot.agent.tools.ask import AskUserInterrupt, AskUserTool -from nanobot.agent.tools.base import Tool, tool_parameters -from nanobot.agent.tools.registry import ToolRegistry -from nanobot.agent.tools.schema import tool_parameters_schema -from nanobot.bus.events import InboundMessage -from nanobot.bus.queue import MessageBus -from nanobot.providers.base import GenerationSettings, LLMResponse, ToolCallRequest - - -def _make_provider(chat_with_retry): - async def chat_stream_with_retry(**kwargs): - kwargs.pop("on_content_delta", None) - return await chat_with_retry(**kwargs) - - provider = MagicMock() - provider.get_default_model.return_value = "test-model" - provider.generation = GenerationSettings() - provider.chat_with_retry = chat_with_retry - provider.chat_stream_with_retry = chat_stream_with_retry - return provider - - -def test_ask_user_tool_schema_and_interrupt(): - tool = AskUserTool() - schema = tool.to_schema()["function"] - - assert schema["name"] == "ask_user" - assert "question" in schema["parameters"]["required"] - assert schema["parameters"]["properties"]["options"]["type"] == "array" - - with pytest.raises(AskUserInterrupt) as exc: - asyncio.run(tool.execute("Continue?", options=["Yes", "No"])) - - assert exc.value.question == "Continue?" - assert exc.value.options == ["Yes", "No"] - - -@pytest.mark.asyncio -async def test_runner_pauses_on_ask_user_without_executing_later_tools(): - @tool_parameters(tool_parameters_schema(required=[])) - class LaterTool(Tool): - called = False - - @property - def name(self) -> str: - return "later" - - @property - def description(self) -> str: - return "Should not run after ask_user pauses the turn." - - async def execute(self, **kwargs): - self.called = True - return "later result" - - async def chat_with_retry(**kwargs): - return LLMResponse( - content="", - finish_reason="tool_calls", - tool_calls=[ - ToolCallRequest( - id="call_ask", - name="ask_user", - arguments={"question": "Install this package?", "options": ["Yes", "No"]}, - ), - ToolCallRequest(id="call_later", name="later", arguments={}), - ], - ) - - later = LaterTool() - tools = ToolRegistry() - tools.register(AskUserTool()) - tools.register(later) - - result = await AgentRunner(_make_provider(chat_with_retry)).run(AgentRunSpec( - initial_messages=[{"role": "user", "content": "continue"}], - tools=tools, - model="test-model", - max_iterations=3, - max_tool_result_chars=16_000, - concurrent_tools=True, - )) - - assert result.stop_reason == "ask_user" - assert result.final_content == "Install this package?" - assert "ask_user" in result.tools_used - assert later.called is False - assert result.messages[-1]["role"] == "assistant" - tool_calls = result.messages[-1]["tool_calls"] - assert [tool_call["function"]["name"] for tool_call in tool_calls] == ["ask_user"] - assert not any(message.get("name") == "ask_user" for message in result.messages) - - -@pytest.mark.asyncio -async def test_ask_user_text_fallback_resumes_with_next_message(tmp_path): - seen_messages: list[list[dict]] = [] - - async def chat_with_retry(**kwargs): - seen_messages.append(kwargs["messages"]) - if len(seen_messages) == 1: - return LLMResponse( - content="", - finish_reason="tool_calls", - tool_calls=[ - ToolCallRequest( - id="call_ask", - name="ask_user", - arguments={ - "question": "Install the optional package?", - "options": ["Install", "Skip"], - }, - ) - ], - ) - return LLMResponse(content="Skipped install.", usage={}) - - loop = AgentLoop( - bus=MessageBus(), - provider=_make_provider(chat_with_retry), - workspace=tmp_path, - model="test-model", - ) - - async def on_stream(delta: str) -> None: - pass - - async def on_stream_end(**kwargs) -> None: - pass - - first = await loop._process_message( - InboundMessage(channel="cli", sender_id="user", chat_id="direct", content="set it up"), - on_stream=on_stream, - on_stream_end=on_stream_end, - ) - - assert first is not None - assert first.content == "Install the optional package?\n\n1. Install\n2. Skip" - assert first.buttons == [] - assert "_streamed" not in first.metadata - - session = loop.sessions.get_or_create("cli:direct") - assert any(message.get("role") == "assistant" and message.get("tool_calls") for message in session.messages) - assert not any(message.get("role") == "tool" and message.get("name") == "ask_user" for message in session.messages) - - second = await loop._process_message( - InboundMessage(channel="cli", sender_id="user", chat_id="direct", content="Skip") - ) - - assert second is not None - assert second.content == "Skipped install." - assert any( - message.get("role") == "tool" - and message.get("name") == "ask_user" - and message.get("content") == "Skip" - for message in seen_messages[-1] - ) - assert not any( - message.get("role") == "user" and message.get("content") == "Skip" - for message in session.messages - ) - assert any( - message.get("role") == "tool" - and message.get("name") == "ask_user" - and message.get("content") == "Skip" - for message in session.messages - ) - - -@pytest.mark.asyncio -async def test_ask_user_keeps_buttons_for_telegram(tmp_path): - async def chat_with_retry(**kwargs): - return LLMResponse( - content="", - finish_reason="tool_calls", - tool_calls=[ - ToolCallRequest( - id="call_ask", - name="ask_user", - arguments={ - "question": "Install the optional package?", - "options": ["Install", "Skip"], - }, - ) - ], - ) - - loop = AgentLoop( - bus=MessageBus(), - provider=_make_provider(chat_with_retry), - workspace=tmp_path, - model="test-model", - ) - - response = await loop._process_message( - InboundMessage(channel="telegram", sender_id="user", chat_id="123", content="set it up") - ) - - assert response is not None - assert response.content == "Install the optional package?" - assert response.buttons == [["Install", "Skip"]] - - -@pytest.mark.asyncio -async def test_ask_user_keeps_buttons_for_websocket(tmp_path): - async def chat_with_retry(**kwargs): - return LLMResponse( - content="", - finish_reason="tool_calls", - tool_calls=[ - ToolCallRequest( - id="call_ask", - name="ask_user", - arguments={ - "question": "Install the optional package?", - "options": ["Install", "Skip"], - }, - ) - ], - ) - - loop = AgentLoop( - bus=MessageBus(), - provider=_make_provider(chat_with_retry), - workspace=tmp_path, - model="test-model", - ) - - response = await loop._process_message( - InboundMessage(channel="websocket", sender_id="user", chat_id="123", content="set it up") - ) - - assert response is not None - assert response.content == "Install the optional package?" - assert response.buttons == [["Install", "Skip"]] diff --git a/tests/channels/test_slack_channel.py b/tests/channels/test_slack_channel.py index 630685eed..d0f41766a 100644 --- a/tests/channels/test_slack_channel.py +++ b/tests/channels/test_slack_channel.py @@ -234,13 +234,13 @@ async def test_send_renders_buttons_on_last_message_chunk() -> None: "type": "button", "text": {"type": "plain_text", "text": "Yes"}, "value": "Yes", - "action_id": "ask_user_Yes", + "action_id": "btn_Yes", }, { "type": "button", "text": {"type": "plain_text", "text": "No"}, "value": "No", - "action_id": "ask_user_No", + "action_id": "btn_No", }, ], } diff --git a/tests/channels/test_websocket_channel.py b/tests/channels/test_websocket_channel.py index af144dbf7..92b61f7d6 100644 --- a/tests/channels/test_websocket_channel.py +++ b/tests/channels/test_websocket_channel.py @@ -224,11 +224,9 @@ async def test_send_delivers_json_message_with_media_and_reply() -> None: payload = json.loads(mock_ws.send.call_args[0][0]) assert payload["event"] == "message" assert payload["chat_id"] == "chat-1" - assert payload["text"] == "hello\n\n1. Yes\n2. No" - assert payload["button_prompt"] == "hello" + assert payload["text"] == "hello" assert payload["reply_to"] == "m1" assert payload["media"] == ["/tmp/a.png"] - assert payload["buttons"] == [["Yes", "No"]] @pytest.mark.asyncio diff --git a/tests/tools/test_tool_loader.py b/tests/tools/test_tool_loader.py index 60ad8057b..fa33b140b 100644 --- a/tests/tools/test_tool_loader.py +++ b/tests/tools/test_tool_loader.py @@ -405,7 +405,7 @@ def test_loader_registers_same_tools_as_old_hardcoded(): registered = loader.load(ctx, registry) expected = { - "ask_user", "read_file", "write_file", "edit_file", "list_dir", + "read_file", "write_file", "edit_file", "list_dir", "glob", "grep", "notebook_edit", "exec", "web_search", "web_fetch", "message", "spawn", "cron", } diff --git a/webui/src/components/thread/AskUserPrompt.tsx b/webui/src/components/thread/AskUserPrompt.tsx deleted file mode 100644 index 4de76307c..000000000 --- a/webui/src/components/thread/AskUserPrompt.tsx +++ /dev/null @@ -1,108 +0,0 @@ -import { useCallback, useEffect, useRef, useState } from "react"; -import { MessageSquareText } from "lucide-react"; - -import { Button } from "@/components/ui/button"; -import { cn } from "@/lib/utils"; - -interface AskUserPromptProps { - question: string; - buttons: string[][]; - onAnswer: (answer: string) => void; -} - -export function AskUserPrompt({ - question, - buttons, - onAnswer, -}: AskUserPromptProps) { - const [customOpen, setCustomOpen] = useState(false); - const [custom, setCustom] = useState(""); - const inputRef = useRef(null); - const options = buttons.flat().filter(Boolean); - - useEffect(() => { - if (customOpen) { - inputRef.current?.focus(); - } - }, [customOpen]); - - const submitCustom = useCallback(() => { - const answer = custom.trim(); - if (!answer) return; - onAnswer(answer); - setCustom(""); - setCustomOpen(false); - }, [custom, onAnswer]); - - if (options.length === 0) return null; - - return ( -
-
-
- -
-

- {question} -

-
- -
- {options.map((option) => ( - - ))} - -
- - {customOpen ? ( -
-