Skip to content

Your lightweight open-source personal agent.

Merge remote-tracking branch 'origin/main' into pr-2722

Xubin Ren77a88446fbd7
diff --cc nanobot/agent/tools/registry.py
index 8c0c05f3c,725706dce..99d3ec63a
--- a/nanobot/agent/tools/registry.py
+++ b/nanobot/agent/tools/registry.py
@@@ -31,59 -31,45 +31,72 @@@ class ToolRegistry
"""Check if a tool is registered."""
return name in self._tools
+ @staticmethod
+ def _schema_name(schema: dict[str, Any]) -> str:
+ """Extract a normalized tool name from either OpenAI or flat schemas."""
+ fn = schema.get("function")
+ if isinstance(fn, dict):
+ name = fn.get("name")
+ if isinstance(name, str):
+ return name
+ name = schema.get("name")
+ return name if isinstance(name, str) else ""
+
def get_definitions(self) -> list[dict[str, Any]]:
- """Get all tool definitions in OpenAI format."""
- return [tool.to_schema() for tool in self._tools.values()]
+ """Get tool definitions with stable ordering for cache-friendly prompts.
+
+ Built-in tools are sorted first as a stable prefix, then MCP tools are
+ sorted and appended.
+ """
+ definitions = [tool.to_schema() for tool in self._tools.values()]
+ builtins: list[dict[str, Any]] = []
+ mcp_tools: list[dict[str, Any]] = []
+ for schema in definitions:
+ name = self._schema_name(schema)
+ if name.startswith("mcp_"):
+ mcp_tools.append(schema)
+ else:
+ builtins.append(schema)
+
+ builtins.sort(key=self._schema_name)
+ mcp_tools.sort(key=self._schema_name)
+ return builtins + mcp_tools
- async def execute(self, name: str, params: dict[str, Any]) -> Any:
- """Execute a tool by name with given parameters."""
- hint = "\n\n[Analyze the error above and try a different approach.]"
-
+ def prepare_call(
+ self,
+ name: str,
+ params: dict[str, Any],
+ ) -> tuple[Tool | None, dict[str, Any], str | None]:
+ """Resolve, cast, and validate one tool call."""
tool = self._tools.get(name)
if not tool:
- return f"Error: Tool '{name}' not found. Available: {', '.join(self.tool_names)}"
+ return None, params, (
+ f"Error: Tool '{name}' not found. Available: {', '.join(self.tool_names)}"
+ )
+
+ cast_params = tool.cast_params(params)
+ errors = tool.validate_params(cast_params)
+ if errors:
+ return tool, cast_params, (
+ f"Error: Invalid parameters for tool '{name}': " + "; ".join(errors)
+ )
+ return tool, cast_params, None
- try:
- # Attempt to cast parameters to match schema types
- params = tool.cast_params(params)
+ async def execute(self, name: str, params: dict[str, Any]) -> Any:
+ """Execute a tool by name with given parameters."""
+ _HINT = "\n\n[Analyze the error above and try a different approach.]"
+ tool, params, error = self.prepare_call(name, params)
+ if error:
+ return error + _HINT
- # Validate parameters
- errors = tool.validate_params(params)
- if errors:
- return f"Error: Invalid parameters for tool '{name}': " + "; ".join(errors) + hint
+ try:
+ assert tool is not None # guarded by prepare_call()
result = await tool.execute(**params)
if isinstance(result, str) and result.startswith("Error"):
- return result + hint
+ return result + _HINT
return result
except Exception as e:
- return f"Error executing {name}: {str(e)}" + hint
+ return f"Error executing {name}: {str(e)}" + _HINT
@property
def tool_names(self) -> list[str]:
diff --cc nanobot/providers/base.py
index 8eb67d6b0,5644d194f..118eb80ca
--- a/nanobot/providers/base.py
+++ b/nanobot/providers/base.py
@@@ -46,9 -51,10 +51,10 @@@ class LLMResponse
tool_calls: list[ToolCallRequest] = field(default_factory=list)
finish_reason: str = "stop"
usage: dict[str, int] = field(default_factory=dict)
- reasoning_content: str | None = None # Kimi, DeepSeek-R1 etc.
+ retry_after: float | None = None # Provider supplied retry wait in seconds.
+ reasoning_content: str | None = None # Kimi, DeepSeek-R1, MiMo etc.
thinking_blocks: list[dict] | None = None # Anthropic extended thinking
-
+
@property
def has_tool_calls(self) -> bool:
"""Check if response contains tool calls."""