1
0
Fork 0
taleweave-ai/taleweave/models/event.py

172 lines
3.8 KiB
Python
Raw Normal View History

2024-05-09 02:11:16 +00:00
from json import loads
from typing import Any, Callable, Dict, List, Literal, Union
from pydantic import Field
2024-05-09 02:11:16 +00:00
from .base import BaseModel, dataclass, uuid
from .entity import Character, Item, Room, WorldEntity
2024-05-09 02:11:16 +00:00
@dataclass
class GenerateEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A new entity has been generated.
"""
name: str
entity: WorldEntity | None = None
id: str = Field(default_factory=uuid)
type: Literal["generate"] = "generate"
2024-05-09 02:11:16 +00:00
@staticmethod
def from_name(name: str) -> "GenerateEvent":
return GenerateEvent(name=name)
@staticmethod
def from_entity(entity: WorldEntity) -> "GenerateEvent":
return GenerateEvent(name=entity.name, entity=entity)
@dataclass
class ActionEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A character has taken an action.
2024-05-09 02:11:16 +00:00
"""
action: str
parameters: Dict[str, bool | float | int | str]
2024-05-09 02:11:16 +00:00
room: Room
character: Character
2024-05-09 02:11:16 +00:00
item: Item | None = None
id: str = Field(default_factory=uuid)
type: Literal["action"] = "action"
2024-05-09 02:11:16 +00:00
@staticmethod
def from_json(json: str, room: Room, character: Character) -> "ActionEvent":
2024-05-09 02:11:16 +00:00
openai_json = loads(json)
return ActionEvent(
action=openai_json["function"],
parameters=openai_json["parameters"],
room=room,
character=character,
2024-05-09 02:11:16 +00:00
item=None,
)
@dataclass
class PromptEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A prompt for a character to take an action.
2024-05-09 02:11:16 +00:00
"""
actions: List[Dict[str, Any]]
2024-05-09 02:11:16 +00:00
prompt: str
room: Room
character: Character
id: str = Field(default_factory=uuid)
type: Literal["prompt"] = "prompt"
2024-05-09 02:11:16 +00:00
@dataclass
class ReplyEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A character has replied with text.
2024-05-09 02:11:16 +00:00
"""
room: Room
speaker: Character
audience: Character | Room
text: str
id: str = Field(default_factory=uuid)
type: Literal["reply"] = "reply"
2024-05-09 02:11:16 +00:00
@dataclass
class ResultEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A result of an action.
"""
result: str
room: Room
character: Character
id: str = Field(default_factory=uuid)
type: Literal["result"] = "result"
2024-05-09 02:11:16 +00:00
@dataclass
class StatusEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A status broadcast event with text.
"""
text: str
room: Room | None = None
character: Character | None = None
id: str = Field(default_factory=uuid)
type: Literal["status"] = "status"
2024-05-09 02:11:16 +00:00
@dataclass
class SnapshotEvent(BaseModel):
"""
A snapshot of the world state.
This one is slightly unusual, because the world has already been dumped to a JSON-compatible dictionary.
That is especially important for the memory, which is a dictionary of character names to lists of messages.
"""
world: Dict[str, Any]
memory: Dict[str, List[Any]]
2024-05-27 12:54:36 +00:00
turn: int
id: str = Field(default_factory=uuid)
type: Literal["snapshot"] = "snapshot"
2024-05-09 02:11:16 +00:00
@dataclass
class PlayerEvent(BaseModel):
2024-05-09 02:11:16 +00:00
"""
A player joining or leaving the game.
"""
status: Literal["join", "leave"]
character: str
client: str
id: str = Field(default_factory=uuid)
type: Literal["player"] = "player"
2024-05-09 02:11:16 +00:00
@dataclass
class PlayerListEvent(BaseModel):
"""
A list of players in the game and the characters they are playing.
"""
players: Dict[str, str]
id: str = Field(default_factory=uuid)
type: Literal["players"] = "players"
@dataclass
class RenderEvent(BaseModel):
"""
Images have been rendered.
"""
paths: List[str]
2024-05-19 20:51:58 +00:00
prompt: str
source: Union["GameEvent", WorldEntity]
2024-05-19 20:51:58 +00:00
title: str
id: str = Field(default_factory=uuid)
type: Literal["render"] = "render"
2024-05-09 02:11:16 +00:00
# event types
WorldEvent = ActionEvent | PromptEvent | ReplyEvent | ResultEvent | StatusEvent
PlayerEventType = PlayerEvent | PlayerListEvent
GameEvent = GenerateEvent | PlayerEventType | RenderEvent | WorldEvent
2024-05-09 02:11:16 +00:00
# callback types
EventCallback = Callable[[GameEvent], None]