Spaces:
Sleeping
Sleeping
| from typing import Dict, Any, List, Optional, Callable, Awaitable, Protocol, runtime_checkable | |
| from pydantic import BaseModel | |
| from abc import ABC, abstractmethod | |
| from datetime import datetime | |
| class ToolContext(BaseModel): | |
| session_id: str | |
| message_id: str | |
| tool_call_id: Optional[str] = None | |
| agent: str = "default" | |
| class ToolResult(BaseModel): | |
| title: str | |
| output: str | |
| metadata: Dict[str, Any] = {} | |
| truncated: bool = False | |
| original_length: int = 0 | |
| class Tool(Protocol): | |
| def id(self) -> str: ... | |
| def description(self) -> str: ... | |
| def parameters(self) -> Dict[str, Any]: ... | |
| async def execute(self, args: Dict[str, Any], ctx: ToolContext) -> ToolResult: ... | |
| class BaseTool(ABC): | |
| MAX_OUTPUT_LENGTH = 50000 | |
| def __init__(self): | |
| self.status: str = "pending" | |
| self.time_start: Optional[datetime] = None | |
| self.time_end: Optional[datetime] = None | |
| def id(self) -> str: | |
| pass | |
| def description(self) -> str: | |
| pass | |
| def parameters(self) -> Dict[str, Any]: | |
| pass | |
| async def execute(self, args: Dict[str, Any], ctx: ToolContext) -> ToolResult: | |
| pass | |
| def get_schema(self) -> Dict[str, Any]: | |
| return { | |
| "name": self.id, | |
| "description": self.description, | |
| "parameters": self.parameters | |
| } | |
| def truncate_output(self, output: str) -> str: | |
| """์ถ๋ ฅ์ด MAX_OUTPUT_LENGTH๋ฅผ ์ด๊ณผํ๋ฉด ์๋ฅด๊ณ ๋ฉ์์ง ์ถ๊ฐ""" | |
| if len(output) <= self.MAX_OUTPUT_LENGTH: | |
| return output | |
| truncated = output[:self.MAX_OUTPUT_LENGTH] | |
| truncated += "\n\n[Output truncated...]" | |
| return truncated | |
| def update_status(self, status: str) -> None: | |
| """๋๊ตฌ ์ํ ์ ๋ฐ์ดํธ (pending, running, completed, error)""" | |
| self.status = status | |
| if status == "running" and self.time_start is None: | |
| self.time_start = datetime.now() | |
| elif status in ("completed", "error") and self.time_end is None: | |
| self.time_end = datetime.now() | |
| from .registry import get_registry | |
| def register_tool(tool: BaseTool) -> None: | |
| """๋๊ตฌ ๋ฑ๋ก (ํธํ์ฑ ํจ์ - ToolRegistry ์ฌ์ฉ)""" | |
| get_registry().register(tool) | |
| def get_tool(tool_id: str) -> Optional[BaseTool]: | |
| """๋๊ตฌ ์กฐํ (ํธํ์ฑ ํจ์ - ToolRegistry ์ฌ์ฉ)""" | |
| return get_registry().get(tool_id) | |
| def list_tools() -> List[BaseTool]: | |
| """๋๊ตฌ ๋ชฉ๋ก (ํธํ์ฑ ํจ์ - ToolRegistry ์ฌ์ฉ)""" | |
| return get_registry().list() | |
| def get_tools_schema() -> List[Dict[str, Any]]: | |
| """๋๊ตฌ ์คํค๋ง ๋ชฉ๋ก (ํธํ์ฑ ํจ์ - ToolRegistry ์ฌ์ฉ)""" | |
| return get_registry().get_schema() | |