Skip to content

Your lightweight open-source personal agent.

merge: resolve conflict with main, keep extracted _run_agent_loop with temperature

Re-bin155fc48b29cd
diff --cc nanobot/agent/loop.py
index 80abae112,22e3315c6..091d2059b
--- a/nanobot/agent/loop.py
+++ b/nanobot/agent/loop.py
@@@ -113,76 -112,6 +115,77 @@@ class AgentLoop
if self.cron_service:
self.tools.register(CronTool(self.cron_service))
+ def _set_tool_context(self, channel: str, chat_id: str) -> None:
+ """Update context for all tools that need routing info."""
+ if message_tool := self.tools.get("message"):
+ if isinstance(message_tool, MessageTool):
+ message_tool.set_context(channel, chat_id)
+
+ if spawn_tool := self.tools.get("spawn"):
+ if isinstance(spawn_tool, SpawnTool):
+ spawn_tool.set_context(channel, chat_id)
+
+ if cron_tool := self.tools.get("cron"):
+ if isinstance(cron_tool, CronTool):
+ cron_tool.set_context(channel, chat_id)
+
+ async def _run_agent_loop(self, initial_messages: list[dict]) -> tuple[str | None, list[str]]:
+ """
+ Run the agent iteration loop.
+
+ Args:
+ initial_messages: Starting messages for the LLM conversation.
+
+ Returns:
+ Tuple of (final_content, list_of_tools_used).
+ """
+ messages = initial_messages
+ iteration = 0
+ final_content = None
+ tools_used: list[str] = []
+
+ while iteration < self.max_iterations:
+ iteration += 1
+
+ response = await self.provider.chat(
+ messages=messages,
+ tools=self.tools.get_definitions(),
- model=self.model
++ model=self.model,
++ temperature=self.temperature,
+ )
+
+ if response.has_tool_calls:
+ tool_call_dicts = [
+ {
+ "id": tc.id,
+ "type": "function",
+ "function": {
+ "name": tc.name,
+ "arguments": json.dumps(tc.arguments)
+ }
+ }
+ for tc in response.tool_calls
+ ]
+ messages = self.context.add_assistant_message(
+ messages, response.content, tool_call_dicts,
+ reasoning_content=response.reasoning_content,
+ )
+
+ for tool_call in response.tool_calls:
+ tools_used.append(tool_call.name)
+ args_str = json.dumps(tool_call.arguments, ensure_ascii=False)
+ logger.info(f"Tool call: {tool_call.name}({args_str[:200]})")
+ result = await self.tools.execute(tool_call.name, tool_call.arguments)
+ messages = self.context.add_tool_result(
+ messages, tool_call.id, tool_call.name, result
+ )
+ messages.append({"role": "user", "content": "Reflect on the results and decide next steps."})
+ else:
+ final_content = response.content
+ break
+
+ return final_content, tools_used
+
async def run(self) -> None:
"""Run the agent loop, processing messages from the bus."""
self._running = True