Skip to content

Commit 9db6816

Browse files
committed
updates docs for events manager
1 parent 8380911 commit 9db6816

File tree

1 file changed

+38
-65
lines changed

1 file changed

+38
-65
lines changed

docs/open-source/events-manager.mdx

+38-65
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,60 @@ description: "How events are emitted and consumed."
55

66
## What is the Events Manager
77

8-
The Events Manager is a class designed to facilitate asynchronous handling of events in the application. It allows for non-blocking actions on events, such as processing transcripts, managing phone calls, and other tasks. The main components of the Events Manager are the `EventsManager` class and several `Event` subclasses representing various event types.
8+
The Events Manager consumes realtime events during conversations - it provides a framework to consume and take action on these events asychronously.
99

10-
## EventsManager Class
11-
12-
The `EventsManager` class is responsible for managing the event queue and handling events asynchronously. The class provides methods for publishing events, starting the event loop, handling events, and ending the event loop.
13-
14-
### Initialization
10+
## Current Event Types
1511

16-
```python
17-
def __init__(self, subscriptions: List[EventType] = []):
18-
self.queue = asyncio.Queue()
19-
self.subscriptions = set(subscriptions)
20-
self.active = False
21-
```
12+
The current event types include:
2213

23-
The `EventsManager` constructor accepts an optional list of `EventType` subscriptions. By default, it initializes an empty set of subscriptions, an asynchronous queue, and sets the `active` attribute to `False`.
14+
1. `TRANSCRIPT`: Indicates a partial transcript for the conversation has been received.
15+
2. `TRANSCRIPT_COMPLETE`: Indicates the transcript is complete (ie conversation has ended).
16+
3. `ACTION`: Indicates that a Vocode action has begun or completed.
17+
4. `PHONE_CALL_CONNECTED`: Indicates a phone call has been connected (only gets sent during `PhoneConversation`s)
18+
5. `PHONE_CALL_ENDED`: Indicates a phone call has ended.
2419

25-
### Publishing Events
20+
## Usage
2621

27-
```python
28-
def publish_event(self, event: Event):
29-
if event.type in self.subscriptions:
30-
self.queue.put_nowait(event)
31-
```
22+
Using the events manager to take particular action when events fire requires that you subclass `vocode.streaming.utils.EventsManager` and override the `handle_event` method.
3223

33-
The `publish_event` method takes an `Event` object as input and adds it to the queue if its type is in the set of subscribed event types.
24+
You can also configure which events your `EventsManager` is subscribed to by using the `subscriptions` property (see example).
3425

35-
### Starting the Event Loop
26+
### Example
3627

3728
```python
38-
async def start(self):
39-
self.active = True
40-
while self.active:
41-
try:
42-
event: Event = await self.queue.get()
43-
except asyncio.QueueEmpty:
44-
await asyncio.sleep(1)
45-
self.handle_event(event)
46-
```
29+
from vocode.streaming.models.events import Event, EventType
4730

48-
## Current Event Types
31+
from vocode.streaming.models.events import Event, EventType
32+
from vocode.streaming.models.transcript import TranscriptCompleteEvent
33+
from vocode.streaming.utils.events_manager import EventsManager
4934

50-
The current event types include:
5135

52-
1. `TRANSCRIPT`: Indicates a partial transcript for the conversation has been received.
53-
2. `TRANSCRIPT_COMPLETE`: Indicates the transcript is complete (ie conversation has ended).
54-
3. `PHONE_CALL_CONNECTED`: Indicates a phone call has been connected.
55-
4. `PHONE_CALL_ENDED`: Indicates a phone call has ended.
56-
5. `RECORDING`: (Vonage Only) Indicates a secure URL containing a recording of the call is available. Requires `recording=true` in `VonageConfig`.
36+
class CustomEventsManager(EventsManager):
37+
def __init__(self):
38+
super().__init__([EventType.TRANSCRIPT_COMPLETE])
5739

58-
## Example Usage
40+
async def handle_event(self, event: Event):
41+
if isinstance(event, TranscriptCompleteEvent):
42+
print("The call has finished, the transcript was", event.transcript.to_string())
43+
```
5944

60-
The following example demonstrates how the `EventsManager` class can be used to consume the `TRANSCRIPT_COMPLETE` event and save the transcript to a file using the `add_transcript` method:
45+
In this example, we create a custom `EventsManager` subclass is created with a subscription to the `TRANSCRIPT_COMPLETE` event and then print the transcript when we receive the event.
6146

62-
```python
63-
import logging
64-
from fastapi import FastAPI
65-
from vocode.streaming.models.events import Event, EventType, TranscriptCompleteEvent
66-
from vocode.streaming.utils import events_manager
67-
from call_transcript_utils import add_transcript
47+
To use `CustomEventsManager`, you can pass it into any Conversation, e.g.
6848

69-
app = FastAPI(docs_url=None)
49+
```
50+
...
51+
conversation = StreamingConversation(
52+
...,
53+
events_manager=CustomEventsManager()
54+
)
55+
```
7056

71-
logging.basicConfig()
72-
logger = logging.getLogger(__name__)
73-
logger.setLevel(logging.DEBUG)
57+
You can also pass it into a `TelephonyServer`, like:
7458

75-
class CustomEventsManager(events_manager.EventsManager):
76-
def __init__(self):
77-
super().__init__(subscriptions=[EventType.TRANSCRIPT_COMPLETE])
78-
79-
def handle_event(self, event: Event):
80-
if event.type == EventType.TRANSCRIPT_COMPLETE:
81-
transcript_complete_event = typing.cast(TranscriptCompleteEvent, event)
82-
add_transcript(
83-
transcript_complete_event.conversation_id,
84-
transcript_complete_event.transcript,
85-
)
86-
87-
events_manager_instance = CustomEventsManager()
88-
await events_manager_instance.start()
8959
```
90-
91-
In this example, a custom `EventsManager` subclass is created with a subscription to the `TRANSCRIPT_COMPLETE` event. The `handle_event` method is overridden to save the transcript to a file using the `add_transcript` method when the `TRANSCRIPT_COMPLETE` event is received.
60+
server = TelephonyServer(
61+
...,
62+
events_manager=CustomEventsManager()
63+
)
64+
```

0 commit comments

Comments
 (0)