以下是 `leaf_playground.core.scene_agent` 模块的部分源码：

```python
# leaf_playground.core.scene_agent 模块

# 这里省略导入语句

class _ActionHandler:
    def __init__(self, action_fn: callable, action_name: str, exec_timeout: int, executable: asyncio.Event):
        self.action_fn = action_fn
        self.action_name = action_name
        self.exec_timeout = exec_timeout

        self.executable = executable
        # this will force the same action of one instance be called once in every moment
        # thus calling the same action of one instance multiple times concurrently
        # it will still run sequentially
        self._lock = asyncio.Lock()
        self._task = None
        self._clock = None
        self._executed_seconds = 0.0

    async def _record_executed_seconds(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    def _reset_clock(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    def _reset(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    async def execute(self, *args, **kwargs):
        ...  # 这里省略具体代码实现，可忽略此方法

    @property
    def task(self):
        return self._task


class _HumanActionHandler(_ActionHandler):
    def __init__(
        self,
        human_agent: "SceneHumanAgent",
        action_fn: callable,
        action_name: str,
        exec_timeout: int,
        executable: asyncio.Event,
    ):
        super().__init__(action_fn, action_name, exec_timeout, executable)
        self.human_agent = human_agent

    async def execute(self, *args, **kwargs):
        ...  # 这里省略具体代码实现，可忽略此方法


class SceneAgentMetadata(BaseModel):
    ...  # 这里省略具体代码实现，可忽略


class SceneAgentMetaClass(ABCMeta):
    def __new__(
        cls,
        name,
        bases,
        attrs,
        *,
        role_definition: RoleDefinition = None,
        cls_description: str = None,
        action_exec_timeout: int = None,
    ):
        attrs["role_definition"] = Immutable(role_definition or getattr(bases[0], "role_definition", None))
        attrs["cls_description"] = Immutable(cls_description)
        if action_exec_timeout is None:
            action_exec_timeout = getattr(bases[0], "action_exec_timeout", 30)
        attrs["action_exec_timeout"] = Immutable(action_exec_timeout)
        attrs["obj_for_import"] = Immutable(DynamicObject(obj=name, module=_getframe(1).f_globals["__name__"]))

        new_cls = super().__new__(cls, name, bases, attrs)

        # 这里省略部分具体实现代码

        return new_cls

    def __init__(
        cls,
        name,
        bases,
        attrs,
        *,
        role_definition: RoleDefinition = None,
        cls_description: str = None,
        action_exec_timeout: int = 30,
    ):
        super().__init__(name, bases, attrs)


class SceneAgentConfig(_Config):
    profile: Profile = Field(default=...)
    chart_major_color: Optional[str] = Field(default=None, pattern=r"^#[0-9a-fA-F]{6}$")


class SceneAgent(_Configurable, ABC, metaclass=SceneAgentMetaClass):
    config_cls = SceneAgentConfig
    config: config_cls

    # class attributes initialized in metaclass
    role_definition: RoleDefinition
    cls_description: str
    action_exec_timeout: int
    obj_for_import: DynamicObject

    def __init__(self, config: config_cls):
        super().__init__(config=config)

        self._profile = self.config.profile
        self._role = self.role_definition.role_instance
        self._profile.role = self._role
        self._env_vars: Dict[str, EnvironmentVariable] = None

        self._not_paused = asyncio.Event()
        self._not_paused.set()
        self._action2handler = {}
        if ABC not in self.__class__.__bases__:
            for action in self.role_definition.actions:
                action_name = action.name
                action_handler = _ActionHandler(
                    getattr(self, action_name), action_name, self.action_exec_timeout, self._not_paused
                )
                setattr(self, action_name, action_handler.execute)
                self._action2handler[action_name] = action_handler

    def bind_env_vars(self, env_vars: Dict[str, EnvironmentVariable]):
        self._env_vars = env_vars

    def pause(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    def resume(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    @property
    def env_vars(self) -> Dict[str, EnvironmentVariable]:
        return self._env_vars

    @property
    def profile(self):
        return self._profile

    @property
    def role(self):
        return self._role

    @property
    def id(self):
        return self.profile.id

    @property
    def name(self):
        return self.profile.name

    @property
    def role_name(self):
        return self.role.name

    @property
    def env_var(self):
        return self._env_vars

    @classmethod
    def from_config(cls, config: config_cls) -> "SceneAgent":
        ...  # 这里省略具体代码实现，可忽略此方法

    @classmethod
    def get_metadata(cls):
        ...  # 这里省略具体代码实现，可忽略此方法


class SceneDynamicAgentConfig(SceneAgentConfig):
    def model_post_init(self, __context) -> None:
        pass


class SceneDynamicAgent(SceneAgent, ABC):
    config_cls = SceneDynamicAgentConfig
    config: config_cls

    def __init__(self, config: config_cls):
        super().__init__(config=config)

        self.connected = False

    @abstractmethod
    def connect(self, *args, **kwargs):
        pass

    @abstractmethod
    def disconnect(self, *args, **kwargs):
        pass


class SceneAIAgentConfig(SceneDynamicAgentConfig):
    ai_backend_config: AIBackendConfig = Field(default=...)
    ai_backend_obj: DynamicObject = Field(default=..., exclude=True)

    def model_post_init(self, __context) -> None:
        ...  # 这里省略具体代码实现，可忽略此方法

    def create_backend_instance(self) -> AIBackend:
        ...  # 这里省略具体代码实现，可忽略此方法

    @staticmethod
    def valid(ai_backend_config: AIBackendConfig, ai_backend_obj: DynamicObject):
        ...  # 这里省略具体代码实现，可忽略此方法


class SceneAIAgent(SceneDynamicAgent, ABC):
    config_cls = SceneAIAgentConfig
    config: config_cls

    def __init__(self, config: config_cls):
        super().__init__(config=config)

        self.backend = self.config.create_backend_instance()
        self.connected = True

    def connect(self):
        pass

    def disconnect(self):
        pass


class HumanConnection:
    def __init__(self, agent: "SceneHumanAgent", socket: WebSocket):
        self.agent = agent
        self.socket = socket

        self.events = asyncio.Queue()
        self.state = WebSocketState.CONNECTING

        self.cancel_event = asyncio.Event()

    async def connect(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    def disconnect(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    def notify_human_to_input(self):
        self.events.put_nowait(SocketEvent(event="wait_human_input"))

    def notify_human_to_not_input(self):
        self.events.put_nowait(SocketEvent(event="disable_human_input"))

    def notify_to_cancel(self):
        self.cancel_event.set()

    async def _keep_alive(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    async def _send_socket_event(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    async def _receive_human_input(self):
        ...  # 这里省略具体代码实现，可忽略此方法

    async def run(self):
        ...  # 这里省略具体代码实现，可忽略此方法


class SceneHumanAgentConfig(SceneDynamicAgentConfig):
    pass


class SceneHumanAgent(SceneDynamicAgent, ABC):
    config_cls = SceneHumanAgentConfig
    config: config_cls

    def __init__(self, config: config_cls):
        super().__init__(config=config)

        self.connection: HumanConnection = None
        self.human_input = None
        self.wait_human_input = False

        if ABC not in self.__class__.__bases__:
            for action in self.role_definition.actions:
                action_name = action.name
                action_handler = _HumanActionHandler(
                    self,
                    self._action2handler[action_name].action_fn,
                    action_name,
                    self.action_exec_timeout,
                    self._not_paused,
                )
                setattr(self, action_name, action_handler.execute)
                self._action2handler[action_name] = action_handler

    def connect(self, connection: HumanConnection):
        self.connection = connection
        self.connected = True

    def disconnect(self):
        self.connection = None
        self.connected = False

    async def wait_human_text_input(self) -> Optional[str]:
        if not self.connected:
            return None
        self.wait_human_input = True
        self.connection.notify_human_to_input()
        while not self.human_input:
            await asyncio.sleep(0.1)
        self.wait_human_input = False
        if self.connected:
            self.connection.notify_human_to_not_input()
        human_input = self.human_input
        self.human_input = None
        return human_input

    async def wait_human_image_input(self, *args, **kwargs):
        raise NotImplementedError()  # TODO: impl

    @classmethod
    def get_metadata(cls):
        metadata = super().get_metadata()
        metadata.is_human = True
        return metadata


class SceneStaticAgentConfig(SceneAgentConfig):
    def model_post_init(self, __context: Any) -> None:
        ...  # 这里省略具体代码实现，可忽略此方法

    @classmethod
    def create_config_model(cls, role_definition: RoleDefinition) -> "SceneStaticAgentConfig":
        ...  # 这里省略具体代码实现，可忽略此方法


class SceneStaticAgent(SceneAgent, ABC):
    config_cls = SceneStaticAgentConfig
    config: config_cls

    def __init__(self, config: config_cls):
        super().__init__(config=config)
```