Serializable metadata describing where a function-tool-backed item came from.
| 277 | |
| 278 | @dataclass(frozen=True) |
| 279 | class ToolOrigin: |
| 280 | """Serializable metadata describing where a function-tool-backed item came from.""" |
| 281 | |
| 282 | type: ToolOriginType |
| 283 | mcp_server_name: str | None = None |
| 284 | agent_name: str | None = None |
| 285 | agent_tool_name: str | None = None |
| 286 | |
| 287 | def to_json_dict(self) -> dict[str, str]: |
| 288 | """Convert the metadata to a JSON-compatible dict.""" |
| 289 | result: dict[str, str] = {"type": self.type.value} |
| 290 | if self.mcp_server_name is not None: |
| 291 | result["mcp_server_name"] = self.mcp_server_name |
| 292 | if self.agent_name is not None: |
| 293 | result["agent_name"] = self.agent_name |
| 294 | if self.agent_tool_name is not None: |
| 295 | result["agent_tool_name"] = self.agent_tool_name |
| 296 | return result |
| 297 | |
| 298 | @classmethod |
| 299 | def from_json_dict(cls, data: Any) -> ToolOrigin | None: |
| 300 | """Deserialize tool origin metadata from JSON-compatible data.""" |
| 301 | if not isinstance(data, Mapping): |
| 302 | return None |
| 303 | |
| 304 | raw_type = data.get("type") |
| 305 | if not isinstance(raw_type, str): |
| 306 | return None |
| 307 | |
| 308 | try: |
| 309 | origin_type = ToolOriginType(raw_type) |
| 310 | except ValueError: |
| 311 | return None |
| 312 | |
| 313 | def _optional_string(key: str) -> str | None: |
| 314 | value = data.get(key) |
| 315 | return value if isinstance(value, str) else None |
| 316 | |
| 317 | return cls( |
| 318 | type=origin_type, |
| 319 | mcp_server_name=_optional_string("mcp_server_name"), |
| 320 | agent_name=_optional_string("agent_name"), |
| 321 | agent_tool_name=_optional_string("agent_tool_name"), |
| 322 | ) |
| 323 | |
| 324 | |
| 325 | ComputerLike = Computer | AsyncComputer |
no outgoing calls