Action class for CopilotKit
| 21 | |
| 22 | |
| 23 | class Action: # pylint: disable=too-few-public-methods |
| 24 | """Action class for CopilotKit""" |
| 25 | |
| 26 | def __init__( |
| 27 | self, |
| 28 | *, |
| 29 | name: str, |
| 30 | handler: Callable, |
| 31 | description: Optional[str] = None, |
| 32 | parameters: Optional[List[Parameter]] = None, |
| 33 | ): |
| 34 | self.name = name |
| 35 | self.description = description |
| 36 | self.parameters = parameters |
| 37 | self.handler = handler |
| 38 | |
| 39 | if not re.match(r"^[a-zA-Z0-9_-]+$", name): |
| 40 | raise ValueError( |
| 41 | f"Invalid action name '{name}': " |
| 42 | + "must consist of alphanumeric characters, underscores, and hyphens only" |
| 43 | ) |
| 44 | |
| 45 | async def execute(self, *, arguments: dict) -> ActionResultDict: |
| 46 | """Execute the action""" |
| 47 | result = self.handler(**arguments) |
| 48 | |
| 49 | return {"result": await result if iscoroutinefunction(self.handler) else result} |
| 50 | |
| 51 | def dict_repr(self) -> ActionDict: |
| 52 | """Dict representation of the action""" |
| 53 | return { |
| 54 | "name": self.name, |
| 55 | "description": self.description or "", |
| 56 | "parameters": normalize_parameters(cast(Any, self.parameters)), |
| 57 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…