Files
copilotkit__copilotkit/sdk-python/copilotkit/agent.py
T

67 lines
1.6 KiB
Python
Raw Normal View History

"""Agents"""
import re
from typing import Optional, List, TypedDict
from abc import ABC, abstractmethod
from .types import Message
from .action import ActionDict
2025-02-04 17:32:42 +01:00
from .types import MetaEvent
class AgentDict(TypedDict):
"""Agent dictionary"""
name: str
description: Optional[str]
class Agent(ABC):
"""Agent class for CopilotKit"""
def __init__(
self,
*,
name: str,
description: Optional[str] = None,
):
self.name = name
self.description = description
if not re.match(r"^[a-zA-Z0-9_-]+$", name):
raise ValueError(
f"Invalid agent name '{name}': "
+ "must consist of alphanumeric characters, underscores, and hyphens only"
)
@abstractmethod
def execute( # pylint: disable=too-many-arguments
self,
*,
state: dict,
config: Optional[dict] = None,
messages: List[Message],
thread_id: str,
actions: Optional[List[ActionDict]] = None,
2025-02-04 17:32:42 +01:00
meta_events: Optional[List[MetaEvent]] = None,
**kwargs,
):
"""Execute the agent"""
2025-01-20 16:53:51 +01:00
@abstractmethod
2025-02-12 16:30:23 +01:00
async def get_state(
2025-01-20 16:53:51 +01:00
self,
*,
thread_id: str,
):
2025-02-12 16:30:23 +01:00
"""Default get_state implementation"""
return {
"threadId": thread_id or "",
"threadExists": False,
"state": {},
"messages": [],
2025-02-12 16:30:23 +01:00
}
2025-01-20 16:53:51 +01:00
def dict_repr(self) -> AgentDict:
"""Dict representation of the action"""
return {"name": self.name, "description": self.description or ""}