File size: 10,397 Bytes
53ea588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD 2-Clause License

"""Action handler implementation for managing action state machines.

This module provides the core functionality for handling action state management through
an asynchronous state machine implementation. It defines the base ActionHandler class that
concrete action implementations can inherit from to manage their state lifecycle.
"""

from dataclasses import asdict, dataclass, field
from typing import Any

from pipecat.frames.frames import SystemFrame
from pipecat.processors.frame_processor import FrameProcessor
from transitions.extensions import MachineFactory

from nvidia_pipecat.frames.action import ActionFrame

async_machine_cls = MachineFactory.get_predefined(asyncio=True)


@dataclass
class InternalStateMachineTriggerFrame(SystemFrame):
    """Internal event frame for state machine transitions.

    Attributes:
        trigger (str): Name of the state machine trigger.
        action_name (str): Name of the action being triggered.
        action_id (str): Unique identifier for the action instance.
        data (dict[str, Any]): Additional trigger data.
    """

    trigger: str
    action_name: str = field(kw_only=True)
    action_id: str = field(kw_only=True)
    data: dict[str, Any] = field(kw_only=True, default_factory=dict)


@dataclass(kw_only=True)
class InternalStateMachineAbortFrame(InternalStateMachineTriggerFrame):
    """Internal event frame for aborting actions.

    Attributes:
        trigger (str): Always "abort" for this frame type.
        reason (str): Reason for the abort action.
    """

    trigger: str = "abort"
    reason: str = field(kw_only=True)


class ActionHandler:
    """Base class for action state machine implementations.

    Manages state transitions for actions through an async state machine. Concrete
    action implementations inherit from this class to define specific behavior.

    Attributes:
        parent_processor (FrameProcessor): The parent frame processor.
        machine (AsyncMachine): The state machine instance.
        action_state (dict[str, Any]): Current action state data.
        action_is_success (bool): Whether the action completed successfully.
        was_stopped (bool): Whether the action was explicitly stopped.
        action_failure_reason (str): Reason for action failure if unsuccessful.

    States:
        init: Initial state
        scheduled: Action is scheduled
        starting: Action is starting
        running: Action is running
        paused: Action is paused
        resuming: Action is resuming
        stopping: Action is stopping
        finished: Action is complete
    """

    states = ["init", "scheduled", "starting", "running", "paused", "resuming", "stopping", "finished"]

    def __init__(self, parent_processor: FrameProcessor) -> None:
        """Initialize the action handler.

        Args:
            parent_processor: The parent frame processor.
        """
        self.parent_processor: FrameProcessor = parent_processor
        transitions = [
            {
                "trigger": "start",
                "source": "init",
                "dest": "scheduled",
                "before": "update_action_state",
            },
            {
                "trigger": "modality_available",
                "source": "scheduled",
                "dest": "starting",
                "before": "update_action_state",
            },
            {
                "trigger": "abort",
                "source": "scheduled",
                "dest": "stopping",
                "before": ["update_action_state", "before_aborted"],
            },
            {
                "trigger": "stop",
                "source": "scheduled",
                "dest": "stopping",
                "before": ["update_action_state", "received_stop_frame"],
            },
            {
                "trigger": "pause",
                "source": "scheduled",
                "dest": "paused",
                "before": "update_action_state",
            },
            {
                "trigger": "started",
                "source": "starting",
                "dest": "running",
                "before": "update_action_state",
            },
            {
                "trigger": "stop",
                "source": "starting",
                "dest": "stopping",
                "before": [
                    "update_action_state",
                    "received_stop_frame",
                    "stopped_during_starting",
                ],
            },
            {
                "trigger": "abort",
                "source": "starting",
                "dest": "stopping",
                "before": ["update_action_state", "before_aborted"],
            },
            {
                "trigger": "abort",
                "source": "init",
                "dest": "stopping",
                "before": ["update_action_state", "before_aborted"],
            },
            {
                "trigger": "abort",
                "source": "running",
                "dest": "stopping",
                "before": ["update_action_state", "before_aborted"],
            },
            {
                "trigger": "abort",
                "source": "stopping",
                "dest": None,
                "before": [],
            },
            {
                "trigger": "change",
                "source": "running",
                "dest": "running",
                "before": ["update_action_state", "on_change"],
            },
            {
                "trigger": "pause",
                "source": "running",
                "dest": "paused",
                "before": "update_action_state",
            },
            {
                "trigger": "resume",
                "source": "paused",
                "dest": "resuming",
                "before": "update_action_state",
            },
            {
                "trigger": "stop",
                "source": "paused",
                "dest": "stopping",
                "before": [
                    "update_action_state",
                    "received_stop_frame",
                    "stopped_during_paused",
                ],
            },
            {
                "trigger": "resumed",
                "source": "resuming",
                "dest": "running",
                "before": "update_action_state",
            },
            {
                "trigger": "stop",
                "source": "running",
                "dest": "stopping",
                "before": [
                    "update_action_state",
                    "received_stop_frame",
                    "stopped_during_running",
                ],
            },
            {
                "trigger": "finished",
                "source": "stopping",
                "dest": "finished",
                "before": "update_action_state",
            },
            {"trigger": "started", "source": "stopping", "dest": None},
        ]
        self.machine = async_machine_cls(
            model=self,
            states=ActionHandler.states,
            initial="init",
            transitions=transitions,
        )
        self.action_state: dict[str, Any] = {}
        self.action_is_success: bool = False
        self.was_stopped = False
        self.action_failure_reason: str = ""

    @property
    def action_id(self) -> str:
        """Get the current action id."""
        return self.action_state.get("action_id", "")

    async def stopped_during_starting(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Handles stop event during starting state.

        Args:
            frame: The trigger or action frame that caused the stop.
        """
        if isinstance(frame, ActionFrame):
            self.action_is_success = False
            self.action_failure_reason = "Stopped during starting the action."

    async def stopped_during_running(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called when the action is stopped during the running state."""
        if isinstance(frame, ActionFrame):
            self.action_is_success = True
            self.action_failure_reason = ""

    async def stopped_during_paused(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called when the action is stopped during the paused state."""
        if isinstance(frame, ActionFrame):
            self.action_is_success = True
            self.action_failure_reason = ""

    async def received_stop_frame(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called when a stop frame is received."""
        self.was_stopped = True

    async def before_aborted(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called before the action is aborted."""
        assert isinstance(frame, InternalStateMachineAbortFrame)
        self.action_is_success = False
        self.action_failure_reason = frame.reason

    async def update_action_state(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Updates action state data during transitions.

        Args:
            frame: The frame containing updated state data.
        """
        if isinstance(frame, ActionFrame):
            assert "action_id" not in self.action_state or frame.action_id == self.action_state["action_id"]
            self.action_state.update(asdict(frame))

    async def on_enter_running(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called when the action enters the running state."""
        pass

    async def on_enter_finished(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called when the action enters the finished state."""
        pass

    async def on_enter_stopping(self, frame: InternalStateMachineTriggerFrame | ActionFrame) -> None:
        """Called when the action enters the stopping state."""
        pass

    async def on_enter_starting(self, frame: ActionFrame | InternalStateMachineTriggerFrame) -> None:
        """Called when the action enters the starting state."""
        pass

    async def clear_modality(self) -> None:
        """Clear the modality for the action."""
        pass