pipecat-facemode 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ """FaceMode integration for Pipecat."""
2
+
3
+ from .exceptions import (
4
+ FaceModeAPIError,
5
+ FaceModeConfigurationError,
6
+ FaceModeError,
7
+ FaceModeNotReadyError,
8
+ FaceModeProtocolError,
9
+ )
10
+ from .models import IngestionDetails, LiveKitRoom, SessionDetails, SessionRequest
11
+ from .service import FaceModeProcessor, FaceModeService, FaceModeVideoService
12
+ from .version import __version__
13
+
14
+ __all__ = [
15
+ "FaceModeAPIError",
16
+ "FaceModeConfigurationError",
17
+ "FaceModeError",
18
+ "FaceModeNotReadyError",
19
+ "FaceModeProcessor",
20
+ "FaceModeProtocolError",
21
+ "FaceModeService",
22
+ "FaceModeVideoService",
23
+ "IngestionDetails",
24
+ "LiveKitRoom",
25
+ "SessionDetails",
26
+ "SessionRequest",
27
+ "__version__",
28
+ ]
@@ -0,0 +1,21 @@
1
+ """Exceptions raised by the Pipecat FaceMode integration."""
2
+
3
+
4
+ class FaceModeError(RuntimeError):
5
+ """Base error for FaceMode integration failures."""
6
+
7
+
8
+ class FaceModeConfigurationError(FaceModeError):
9
+ """Raised when required integration configuration is missing or invalid."""
10
+
11
+
12
+ class FaceModeAPIError(FaceModeError):
13
+ """Raised when the FaceMode REST API rejects or cannot fulfill a request."""
14
+
15
+
16
+ class FaceModeProtocolError(FaceModeError):
17
+ """Raised when the FaceMode WebSocket protocol fails."""
18
+
19
+
20
+ class FaceModeNotReadyError(FaceModeError):
21
+ """Raised when the LiveKit avatar media stream is not ready."""
@@ -0,0 +1,238 @@
1
+ """Typed FaceMode session and LiveKit room models."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Mapping
6
+ from dataclasses import dataclass, field
7
+ from typing import Any, Literal
8
+
9
+
10
+ @dataclass(frozen=True, slots=True)
11
+ class LiveKitRoom:
12
+ """Credentials for a customer-owned LiveKit room."""
13
+
14
+ url: str
15
+ token: str = field(repr=False)
16
+ name: str = ""
17
+ type: Literal["livekit"] = field(default="livekit", kw_only=True)
18
+
19
+ def __post_init__(self) -> None:
20
+ if self.type != "livekit":
21
+ raise ValueError("room.type must be 'livekit'")
22
+ if not self.url:
23
+ raise ValueError("room.url is required")
24
+ if not self.token:
25
+ raise ValueError("room.token is required")
26
+
27
+ def to_payload(self) -> dict[str, str]:
28
+ """Return the API representation without exposing credentials in reprs."""
29
+ payload = {"type": self.type, "url": self.url, "token": self.token}
30
+ if self.name:
31
+ payload["name"] = self.name
32
+ return payload
33
+
34
+ @classmethod
35
+ def from_value(cls, value: "LiveKitRoom | Mapping[str, Any]") -> "LiveKitRoom":
36
+ """Normalize a typed room or mapping supplied by an integration caller."""
37
+ if isinstance(value, cls):
38
+ return value
39
+ if not isinstance(value, Mapping):
40
+ raise ValueError("room must be an object")
41
+ room_type = value.get("type")
42
+ if room_type != "livekit":
43
+ raise ValueError("room.type must be 'livekit'")
44
+ url = value.get("url")
45
+ token = value.get("token")
46
+ name = value.get("name", "")
47
+ if not isinstance(url, str) or not url:
48
+ raise ValueError("room.url is required")
49
+ if not isinstance(token, str) or not token:
50
+ raise ValueError("room.token is required")
51
+ if not isinstance(name, str):
52
+ raise ValueError("room.name must be a string")
53
+ return cls(url=url, token=token, name=name)
54
+
55
+
56
+ @dataclass(frozen=True, slots=True)
57
+ class SessionRequest:
58
+ """Request body used to create a FaceMode session."""
59
+
60
+ avatar_id: str
61
+ room: LiveKitRoom
62
+ room_name: str = ""
63
+ input_provider: str | None = None
64
+
65
+ def to_payload(self) -> dict[str, Any]:
66
+ """Return the room-object API wire representation."""
67
+ payload: dict[str, Any] = {
68
+ "avatarId": self.avatar_id,
69
+ "room": self.room.to_payload(),
70
+ }
71
+ if self.input_provider:
72
+ payload["inputProvider"] = self.input_provider
73
+ if self.room_name:
74
+ payload["livekit_room_id"] = self.room_name
75
+ return payload
76
+
77
+
78
+ @dataclass(frozen=True, slots=True)
79
+ class IngestionDetails:
80
+ """The current state of the FaceMode canonical ingestion WebSocket."""
81
+
82
+ ready: bool
83
+ url: str = ""
84
+ ws_token: str = field(default="", repr=False)
85
+ authority: str | None = None
86
+ headers: Mapping[str, str] = field(default_factory=dict, repr=False)
87
+
88
+
89
+ @dataclass(frozen=True, slots=True)
90
+ class SessionDetails:
91
+ """Session and ingestion assignment returned by the FaceMode API."""
92
+
93
+ session_id: str
94
+ room_name: str
95
+ room: LiveKitRoom
96
+ ingestion: IngestionDetails = field(repr=False)
97
+ worker_status: str | None = None
98
+ avatar_participant_identity: str | None = None
99
+ input_provider: str | None = None
100
+
101
+ @classmethod
102
+ def from_api(
103
+ cls,
104
+ payload: Mapping[str, Any],
105
+ *,
106
+ fallback: "SessionDetails | None" = None,
107
+ ) -> "SessionDetails":
108
+ """Parse current and legacy response envelopes, including pending ingestion."""
109
+ root: Mapping[str, Any] = payload
110
+ nested = payload.get("data")
111
+ if isinstance(nested, Mapping):
112
+ root = nested
113
+
114
+ session_value = root.get("session")
115
+ session = session_value if isinstance(session_value, Mapping) else {}
116
+ ingestion_value = root.get("ingestion") or session.get("ingestion")
117
+ ingestion = ingestion_value if isinstance(ingestion_value, Mapping) else {}
118
+
119
+ session_id = _first_text(
120
+ session.get("id"),
121
+ session.get("sessionId"),
122
+ root.get("sessionId"),
123
+ root.get("id"),
124
+ root.get("jobId"),
125
+ fallback.session_id if fallback else None,
126
+ )
127
+ if not session_id:
128
+ raise ValueError("FaceMode session response is missing a session ID")
129
+
130
+ ingestion_url = _first_text(
131
+ ingestion.get("url"),
132
+ ingestion.get("websocketUrl"),
133
+ root.get("websocketUrl"),
134
+ root.get("websocket_url"),
135
+ root.get("url"),
136
+ fallback.ingestion.url if fallback else None,
137
+ )
138
+ ws_token = _first_text(
139
+ ingestion.get("wsToken"),
140
+ ingestion.get("ws_token"),
141
+ ingestion.get("token"),
142
+ root.get("wsToken"),
143
+ root.get("ws_token"),
144
+ root.get("ingestionToken"),
145
+ root.get("ingestion_token"),
146
+ fallback.ingestion.ws_token if fallback else None,
147
+ )
148
+ ready = (
149
+ ingestion.get("ready") is True
150
+ or root.get("ready") is True
151
+ or (
152
+ "ready" not in ingestion
153
+ and "ready" not in root
154
+ and bool(ingestion_url and ws_token)
155
+ )
156
+ )
157
+ if ready and (not ingestion_url or not ws_token):
158
+ raise ValueError("FaceMode reported ready ingestion without WebSocket credentials")
159
+
160
+ return cls(
161
+ session_id=session_id,
162
+ room_name=_first_text(
163
+ root.get("roomName"),
164
+ session.get("roomName"),
165
+ fallback.room_name if fallback else "",
166
+ ),
167
+ room=_parse_room(root, session, fallback.room if fallback else None),
168
+ ingestion=IngestionDetails(
169
+ ready=ready,
170
+ url=ingestion_url,
171
+ ws_token=ws_token,
172
+ authority=_first_optional_text(
173
+ ingestion.get("authority"),
174
+ fallback.ingestion.authority if fallback else None,
175
+ ),
176
+ headers=(
177
+ _parse_headers(ingestion.get("headers"))
178
+ or (dict(fallback.ingestion.headers) if fallback else {})
179
+ ),
180
+ ),
181
+ worker_status=_first_optional_text(
182
+ root.get("workerStatus"),
183
+ root.get("worker_status"),
184
+ session.get("workerStatus"),
185
+ session.get("worker_status"),
186
+ fallback.worker_status if fallback else None,
187
+ ),
188
+ avatar_participant_identity=_first_optional_text(
189
+ root.get("avatarParticipantIdentity"),
190
+ root.get("avatar_participant_identity"),
191
+ session.get("avatarParticipantIdentity"),
192
+ session.get("avatar_participant_identity"),
193
+ fallback.avatar_participant_identity if fallback else None,
194
+ ),
195
+ input_provider=_first_optional_text(
196
+ root.get("inputProvider"),
197
+ root.get("input_provider"),
198
+ session.get("inputProvider"),
199
+ session.get("input_provider"),
200
+ fallback.input_provider if fallback else None,
201
+ ),
202
+ )
203
+
204
+
205
+ def _parse_room(
206
+ root: Mapping[str, Any],
207
+ session: Mapping[str, Any],
208
+ fallback: LiveKitRoom | None,
209
+ ) -> LiveKitRoom:
210
+ room_value = root.get("room") or session.get("room")
211
+ if isinstance(room_value, Mapping):
212
+ return LiveKitRoom.from_value(room_value)
213
+ if fallback is not None:
214
+ return fallback
215
+ raise ValueError("FaceMode session response is missing a LiveKit room")
216
+
217
+
218
+ def _parse_headers(value: Any) -> Mapping[str, str]:
219
+ if not isinstance(value, Mapping):
220
+ return {}
221
+ return {
222
+ str(name): header_value
223
+ for name, header_value in value.items()
224
+ if str(name) and isinstance(header_value, str)
225
+ }
226
+
227
+
228
+ def _first_text(*values: Any) -> str:
229
+ """Return the first non-empty value as text."""
230
+ for value in values:
231
+ if value is not None and str(value):
232
+ return str(value)
233
+ return ""
234
+
235
+
236
+ def _first_optional_text(*values: Any) -> str | None:
237
+ value = _first_text(*values)
238
+ return value or None