Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
Merge origin/main into feat/show-reasoning
Xubin Ren01fa362c032f
diff --cc nanobot/agent/loop.pyindex 028d9ddd9,476a2caf2..c7091a5f6--- a/nanobot/agent/loop.py+++ b/nanobot/agent/loop.py@@@ -523,6 -637,90 +651,93 @@@ class AgentLoopreturn format_tool_hints(tool_calls, max_length=self.tool_hint_max_length)+ async def _build_bus_progress_callback(+ self, msg: InboundMessage+ ) -> Callable[..., Awaitable[None]]:+ """Build a progress callback that publishes to the message bus."""++ async def _bus_progress(+ content: str,+ *,+ 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(+ OutboundMessage(+ channel=msg.channel,+ chat_id=msg.chat_id,+ content=content,+ metadata=meta,+ )+ )++ return _bus_progress++ async def _build_retry_wait_callback(+ self, msg: InboundMessage+ ) -> Callable[[str], Awaitable[None]]:+ """Build a retry-wait callback that publishes to the message bus."""++ async def _on_retry_wait(content: str) -> None:+ meta = dict(msg.metadata or {})+ meta["_retry_wait"] = True+ await self.bus.publish_outbound(+ OutboundMessage(+ channel=msg.channel,+ chat_id=msg.chat_id,+ content=content,+ metadata=meta,+ )+ )++ return _on_retry_wait++ def _persist_user_message_early(+ self,+ msg: InboundMessage,+ session: Session,+ ) -> bool:+ """Persist the triggering user message before the turn starts.++ Returns True if the message was persisted.+ """+ 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 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)+ self._mark_pending_user_turn(session)+ self.sessions.save(session)+ return True+ return False++ def _build_initial_messages(+ self,+ msg: InboundMessage,+ session: Session,+ history: list[dict[str, Any]],+ pending_summary: str | None,+ ) -> list[dict[str, Any]]:+ """Build the initial message list for the LLM turn."""+ return self.context.build_messages(+ history=history,+ current_message=image_generation_prompt(msg.content, msg.metadata),+ media=msg.media if msg.media else None,+ channel=msg.channel,+ chat_id=self._runtime_chat_id(msg),+ sender_id=msg.sender_id,+ session_summary=pending_summary,+ )+async def _dispatch_command_inline(self,msg: InboundMessage,diff --cc nanobot/agent/runner.pyindex 2713359be,9ea0d26de..6b8e5383c--- a/nanobot/agent/runner.py+++ b/nanobot/agent/runner.py@@@ -284,22 -281,8 +283,18 @@@ class AgentRunnercontext.tool_calls = list(response.tool_calls)self._accumulate_usage(usage, raw_usage)+ reasoning_text, cleaned_content = extract_reasoning(+ response.reasoning_content,+ response.thinking_blocks,+ response.content,+ )+ response.content = cleaned_content+ if reasoning_text and not context.streamed_reasoning:+ await hook.emit_reasoning(reasoning_text)+ context.streamed_reasoning = True+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)diff --cc nanobot/cli/commands.pyindex 467683ed9,0d71d91db..dd23cb620--- a/nanobot/cli/commands.py+++ b/nanobot/cli/commands.py@@@ -1162,10 -1075,14 +1105,14 @@@ def agentif message:# Single message mode β direct call, no bus neededasync def run_once():- renderer = StreamRenderer(render_markdown=markdown)+ renderer = StreamRenderer(+ render_markdown=markdown,+ bot_name=config.agents.defaults.bot_name,+ bot_icon=config.agents.defaults.bot_icon,+ )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,)diff --cc nanobot/cli/stream.pyindex ec7f0a96c,c6b5a87ad..64cb4ed78--- a/nanobot/cli/stream.py+++ b/nanobot/cli/stream.py@@@ -69,23 -65,30 +67,31 @@@ class ThinkingSpinnerclass 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 <think> 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):+ def __init__(+ self,+ render_markdown: bool = True,+ show_spinner: bool = True,+ bot_name: str = "nanobot",+ bot_icon: str = "π",+ ):self._md = render_markdownself._show_spinner = show_spinner+ self._bot_name = bot_name+ self._bot_icon = bot_iconself._buf = ""- self._live: Live | None = None- self._t = 0.0self.streamed = False+ self._console = _make_console()+ self._live: Live | None = Noneself._spinner: ThinkingSpinner | None = Noneself._start_spinner()@@@ -130,18 -112,17 +136,19 @@@if not self._buf.strip():returnself._stop_spinner()- c = _make_console()- c.print()+ self._console.print()- self._console.print(f"[cyan]{__logo__} nanobot[/cyan]")+ header = f"{self._bot_icon} {self._bot_name}" if self._bot_icon else self._bot_name- c.print(f"[cyan]{header}[/cyan]")- self._live = Live(self._render(), console=c, auto_refresh=False)++ self._console.print(f"[cyan]{header}[/cyan]")+ 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:diff --cc tests/agent/test_runner_reasoning.pyindex 000000000,000000000..512f3d2e9new file mode 100644--- /dev/null+++ b/tests/agent/test_runner_reasoning.py@@@ -1,0 -1,0 +1,279 @@@++"""Tests for AgentRunner reasoning extraction and emission.++++Covers the three sources of model reasoning (dedicated ``reasoning_content``,++Anthropic ``thinking_blocks``, inline ``<think>``/``<thought>`` tags) plus++the streaming interaction: reasoning and answer streams are independent++channels, gated by ``context.streamed_reasoning`` rather than++``context.streamed_content``.++"""++++from __future__ import annotations++++from unittest.mock import AsyncMock, MagicMock++++import pytest++++from nanobot.agent.hook import AgentHook++from nanobot.config.schema import AgentDefaults++from nanobot.providers.base import LLMResponse, ToolCallRequest++++_MAX_TOOL_RESULT_CHARS = AgentDefaults().max_tool_result_chars++++++class _RecordingHook(AgentHook):++ def __init__(self) -> None:++ super().__init__()++ self.emitted: list[str] = []++++ async def emit_reasoning(self, reasoning_content: str | None) -> None:++ if reasoning_content:++ self.emitted.append(reasoning_content)++++++async def test_runner_preserves_reasoning_fields_in_assistant_history():++ """Reasoning fields ride along on the persisted assistant message so++ follow-up provider calls retain the model's prior thinking context."""++ from nanobot.agent.runner import AgentRunSpec, AgentRunner++++ provider = MagicMock()++ captured_second_call: list[dict] = []++ call_count = {"n": 0}++++ async def chat_with_retry(*, messages, **kwargs):++ call_count["n"] += 1++ if call_count["n"] == 1:++ return LLMResponse(++ content="thinking",++ tool_calls=[ToolCallRequest(id="call_1", name="list_dir", arguments={"path": "."})],++ reasoning_content="hidden reasoning",++ thinking_blocks=[{"type": "thinking", "thinking": "step"}],++ usage={"prompt_tokens": 5, "completion_tokens": 3},++ )++ captured_second_call[:] = messages++ return LLMResponse(content="done", tool_calls=[], usage={})++++ provider.chat_with_retry = chat_with_retry++ tools = MagicMock()++ tools.get_definitions.return_value = []++ tools.execute = AsyncMock(return_value="tool result")++++ runner = AgentRunner(provider)++ result = await runner.run(AgentRunSpec(++ initial_messages=[++ {"role": "system", "content": "system"},++ {"role": "user", "content": "do task"},++ ],++ tools=tools,++ model="test-model",++ max_iterations=3,++ max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,++ ))++++ assert result.final_content == "done"++ assistant_messages = [++ msg for msg in captured_second_call++ if msg.get("role") == "assistant" and msg.get("tool_calls")++ ]++ assert len(assistant_messages) == 1++ assert assistant_messages[0]["reasoning_content"] == "hidden reasoning"++ assert assistant_messages[0]["thinking_blocks"] == [{"type": "thinking", "thinking": "step"}]++++++async def test_runner_emits_anthropic_thinking_blocks():++ from nanobot.agent.runner import AgentRunSpec, AgentRunner++++ provider = MagicMock()++++ async def chat_with_retry(**kwargs):++ return LLMResponse(++ content="The answer is 42.",++ thinking_blocks=[++ {"type": "thinking", "thinking": "Let me analyze this step by step.", "signature": "sig1"},++ {"type": "thinking", "thinking": "After careful consideration.", "signature": "sig2"},++ ],++ tool_calls=[],++ usage={"prompt_tokens": 5, "completion_tokens": 3},++ )++++ provider.chat_with_retry = chat_with_retry++ tools = MagicMock()++ tools.get_definitions.return_value = []++++ hook = _RecordingHook()++ runner = AgentRunner(provider)++ result = await runner.run(AgentRunSpec(++ initial_messages=[{"role": "user", "content": "question"}],++ tools=tools,++ model="test-model",++ max_iterations=3,++ max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,++ hook=hook,++ ))++++ assert result.final_content == "The answer is 42."++ assert len(hook.emitted) == 1++ assert "Let me analyze this" in hook.emitted[0]++ assert "After careful consideration" in hook.emitted[0]++++++async def test_runner_emits_inline_think_content_as_reasoning():++ """Models embedding reasoning in <think>...</think> blocks should have++ that content extracted and emitted, and stripped from the answer."""++ from nanobot.agent.runner import AgentRunSpec, AgentRunner++++ provider = MagicMock()++++ async def chat_with_retry(**kwargs):++ return LLMResponse(++ content="<think>Let me think about this...\nThe answer is 42.</think>The answer is 42.",++ tool_calls=[],++ usage={"prompt_tokens": 5, "completion_tokens": 3},++ )++++ provider.chat_with_retry = chat_with_retry++ tools = MagicMock()++ tools.get_definitions.return_value = []++++ hook = _RecordingHook()++ runner = AgentRunner(provider)++ result = await runner.run(AgentRunSpec(++ initial_messages=[{"role": "user", "content": "what is the answer?"}],++ tools=tools,++ model="test-model",++ max_iterations=3,++ max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,++ hook=hook,++ ))++++ assert result.final_content == "The answer is 42."++ assert len(hook.emitted) == 1++ assert "Let me think about this" in hook.emitted[0]++++++async def test_runner_prefers_reasoning_content_over_inline_think():++ """Fallback priority: dedicated reasoning_content wins; inline <think>++ is still scrubbed from the answer content."""++ from nanobot.agent.runner import AgentRunSpec, AgentRunner++++ provider = MagicMock()++++ async def chat_with_retry(**kwargs):++ return LLMResponse(++ content="<think>inline thinking</think>The answer.",++ reasoning_content="dedicated reasoning field",++ tool_calls=[],++ usage={"prompt_tokens": 5, "completion_tokens": 3},++ )++++ provider.chat_with_retry = chat_with_retry++ tools = MagicMock()++ tools.get_definitions.return_value = []++++ hook = _RecordingHook()++ runner = AgentRunner(provider)++ result = await runner.run(AgentRunSpec(++ initial_messages=[{"role": "user", "content": "question"}],++ tools=tools,++ model="test-model",++ max_iterations=3,++ max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,++ hook=hook,++ ))++++ assert result.final_content == "The answer."++ assert hook.emitted == ["dedicated reasoning field"]++++++async def test_runner_emits_reasoning_content_even_when_answer_was_streamed():++ """`reasoning_content` arrives only on the final response; streaming the++ answer must not suppress it (the answer stream and the reasoning channel++ are independent β only the reasoning-already-emitted bit matters)."""++ from nanobot.agent.runner import AgentRunSpec, AgentRunner++++ provider = MagicMock()++ provider.supports_progress_deltas = True++++ async def chat_stream_with_retry(*, on_content_delta=None, **kwargs):++ if on_content_delta:++ await on_content_delta("The ")++ await on_content_delta("answer.")++ return LLMResponse(++ content="The answer.",++ reasoning_content="step-by-step deduction",++ tool_calls=[],++ usage={"prompt_tokens": 5, "completion_tokens": 3},++ )++++ provider.chat_stream_with_retry = chat_stream_with_retry++ tools = MagicMock()++ tools.get_definitions.return_value = []++++ progress_calls: list[str] = []++++ async def _progress(content: str, **_kwargs):++ progress_calls.append(content)++++ hook = _RecordingHook()++ runner = AgentRunner(provider)++ result = await runner.run(AgentRunSpec(++ initial_messages=[{"role": "user", "content": "question"}],++ tools=tools,++ model="test-model",++ max_iterations=3,++ max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,++ hook=hook,++ stream_progress_deltas=True,++ progress_callback=_progress,++ ))++++ assert result.final_content == "The answer."++ assert progress_calls, "answer should have streamed via progress callback"++ assert hook.emitted == ["step-by-step deduction"]++++++async def test_runner_does_not_double_emit_when_inline_think_already_streamed():++ """Inline `<think>` blocks streamed incrementally during the answer++ stream must not be re-emitted from the final response."""++ from nanobot.agent.runner import AgentRunSpec, AgentRunner++++ provider = MagicMock()++ provider.supports_progress_deltas = True++++ async def chat_stream_with_retry(*, on_content_delta=None, **kwargs):++ if on_content_delta:++ await on_content_delta("<think>working...</think>")++ await on_content_delta("The answer.")++ return LLMResponse(++ content="<think>working...</think>The answer.",++ tool_calls=[],++ usage={"prompt_tokens": 5, "completion_tokens": 3},++ )++++ provider.chat_stream_with_retry = chat_stream_with_retry++ tools = MagicMock()++ tools.get_definitions.return_value = []++++ async def _progress(content: str, **_kwargs):++ pass++++ hook = _RecordingHook()++ runner = AgentRunner(provider)++ result = await runner.run(AgentRunSpec(++ initial_messages=[{"role": "user", "content": "question"}],++ tools=tools,++ model="test-model",++ max_iterations=3,++ max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,++ hook=hook,++ stream_progress_deltas=True,++ progress_callback=_progress,++ ))++++ assert result.final_content == "The answer."++ assert hook.emitted == ["working..."]