refactor(tools): use structured tool error results

This commit is contained in:
chengyongru authored and Xubin Ren committed 2026-07-01 13:03:47 +08:00
1 parent 8d2c31eb6a
commit 8493560976
20 files changed
+294 -188

No files matched your search

+20 -1
View File
@@ -128,6 +128,21 @@ class Schema(ABC):
return Schema.validate_json_schema_value(value, self.to_json_schema(), path)
class ToolResult(str):
"""String-compatible tool output with structured status."""
is_error: bool
def __new__(cls, content: str, *, is_error: bool = False) -> ToolResult:
obj = str.__new__(cls, content)
obj.is_error = is_error
return obj
@classmethod
def error(cls, content: str) -> ToolResult:
return cls(content, is_error=True)
class Tool(ABC):
"""Agent capability: read files, run commands, etc."""
@@ -193,9 +208,13 @@ class Tool(ABC):
@abstractmethod
async def execute(self, **kwargs: Any) -> Any:
"""Run the tool; returns a string or list of content blocks."""
"""Run the tool; return content, or ``ToolResult.error(...)`` for failures."""
...
@staticmethod
def error(content: str) -> ToolResult:
return ToolResult.error(content)
def _cast_object(self, obj: Any, schema: dict[str, Any]) -> dict[str, Any]:
if not isinstance(obj, dict):
return obj