livekit-plugins-hamming 1.5.1__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,55 @@
1
+ # Copyright 2026 Hamming, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Hamming plugin for LiveKit Agents.
16
+
17
+ Exports final post-call monitoring artifacts to Hamming.
18
+ """
19
+
20
+ from livekit.agents import Plugin
21
+
22
+ from ._setup import (
23
+ DoctorReport,
24
+ attach_session,
25
+ configure_hamming,
26
+ doctor,
27
+ doctor_json,
28
+ )
29
+ from .log import logger
30
+ from .version import __version__
31
+
32
+ __all__ = [
33
+ "configure_hamming",
34
+ "doctor",
35
+ "doctor_json",
36
+ "DoctorReport",
37
+ "attach_session",
38
+ "__version__",
39
+ ]
40
+
41
+
42
+ class HammingPlugin(Plugin):
43
+ def __init__(self) -> None:
44
+ super().__init__(__name__, __version__, __package__, logger)
45
+
46
+
47
+ Plugin.register_plugin(HammingPlugin())
48
+
49
+ # Cleanup docs of unexported modules
50
+ _module = dir()
51
+ NOT_IN_ALL = [m for m in _module if m not in __all__]
52
+
53
+ __pdoc__: dict[str, bool] = {}
54
+ for n in NOT_IN_ALL:
55
+ __pdoc__[n] = False
@@ -0,0 +1,302 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from collections.abc import Callable, Mapping
5
+ from dataclasses import dataclass
6
+ from typing import Any, Protocol
7
+
8
+ from livekit.agents.voice.events import CloseEvent
9
+
10
+ from .log import logger
11
+
12
+ _CALL_ID_METADATA_KEY = "call_id"
13
+ CALL_ID_STRATEGY_ROOM_NAME = "room_name"
14
+ CALL_ID_STRATEGY_PARTICIPANT_IDENTITY = "participant_identity"
15
+ CALL_ID_STRATEGY_PARTICIPANT_METADATA = "participant_metadata"
16
+ CALL_ID_STRATEGY_CUSTOM = "custom"
17
+
18
+
19
+ class SessionReportLike(Protocol):
20
+ @property
21
+ def room(self) -> str: ...
22
+
23
+ @property
24
+ def started_at(self) -> float | None: ...
25
+
26
+ @property
27
+ def timestamp(self) -> float: ...
28
+
29
+ @property
30
+ def events(self) -> list[Any]: ...
31
+
32
+ @property
33
+ def audio_recording_path(self) -> Any | None: ...
34
+
35
+ def to_dict(self) -> dict[str, Any]: ...
36
+
37
+
38
+ @dataclass(frozen=True)
39
+ class CallIdResolutionContext:
40
+ room_name: str
41
+ participant_identity: str | None
42
+ participant_metadata_raw: str | None
43
+ external_agent_id: str
44
+
45
+
46
+ CallIdResolver = Callable[[CallIdResolutionContext], str | None]
47
+
48
+
49
+ @dataclass(frozen=True)
50
+ class PayloadBuildConfig:
51
+ external_agent_id: str
52
+ plugin_api_version: str
53
+ plugin_version: str
54
+ payload_schema_version: str
55
+ call_id_strategy: str = CALL_ID_STRATEGY_ROOM_NAME
56
+ call_id_metadata_key: str = _CALL_ID_METADATA_KEY
57
+ resolve_call_id: CallIdResolver | None = None
58
+ capture_manifest: Mapping[str, Any] | None = None
59
+
60
+
61
+ def build_livekit_monitoring_envelope(
62
+ *,
63
+ config: PayloadBuildConfig,
64
+ report: SessionReportLike,
65
+ participant_identity: str | None,
66
+ participant_metadata_raw: str | None,
67
+ recording_context: Mapping[str, Any] | None,
68
+ close_event: CloseEvent | None,
69
+ ) -> dict[str, Any]:
70
+ call_id = _resolve_call_id(
71
+ room_name=report.room,
72
+ participant_identity=participant_identity,
73
+ participant_metadata_raw=participant_metadata_raw,
74
+ external_agent_id=config.external_agent_id,
75
+ strategy=config.call_id_strategy,
76
+ metadata_key=config.call_id_metadata_key,
77
+ resolve_call_id=config.resolve_call_id,
78
+ )
79
+ test_case_run_id = _resolve_test_case_run_id(
80
+ participant_metadata_raw=participant_metadata_raw,
81
+ recording_context=recording_context,
82
+ )
83
+
84
+ payload = {
85
+ "call_id": call_id,
86
+ "call_type": "web",
87
+ "livekit_room_name": report.room,
88
+ "start_timestamp": int((report.started_at or report.timestamp) * 1000),
89
+ "end_timestamp": int(report.timestamp * 1000),
90
+ "status": _resolve_status(close_event),
91
+ "livekit_capture": _build_livekit_capture(
92
+ report=report,
93
+ participant_identity=participant_identity,
94
+ participant_metadata_raw=participant_metadata_raw,
95
+ close_event=close_event,
96
+ ),
97
+ }
98
+ if test_case_run_id:
99
+ payload["test_case_run_id"] = test_case_run_id
100
+
101
+ metadata: dict[str, Any] = {
102
+ "integration": "livekit-plugin-hamming",
103
+ "mode": "call_review",
104
+ "call_id_strategy": config.call_id_strategy,
105
+ }
106
+ if config.capture_manifest:
107
+ metadata["capture_manifest"] = dict(config.capture_manifest)
108
+
109
+ return {
110
+ "provider": "custom",
111
+ "external_agent_id": config.external_agent_id,
112
+ "payload_schema_version": config.payload_schema_version,
113
+ "plugin_api_version": config.plugin_api_version,
114
+ "plugin_version": config.plugin_version,
115
+ "payload": payload,
116
+ "metadata": metadata,
117
+ }
118
+
119
+
120
+ def _build_livekit_capture(
121
+ *,
122
+ report: SessionReportLike,
123
+ participant_identity: str | None,
124
+ participant_metadata_raw: str | None,
125
+ close_event: CloseEvent | None,
126
+ ) -> dict[str, Any]:
127
+ capture = report.to_dict()
128
+ capture["started_at"] = report.started_at
129
+ capture["timestamp"] = report.timestamp
130
+ capture["participant_identity"] = participant_identity
131
+ if participant_metadata_raw:
132
+ capture["participant_metadata"] = participant_metadata_raw
133
+ capture["close_reason"] = _serialize_close_reason(close_event)
134
+ capture["events"] = _serialize_events(report.events)
135
+ return capture
136
+
137
+
138
+ def _serialize_events(events: list[Any]) -> list[dict[str, Any]]:
139
+ events_out: list[dict[str, Any]] = []
140
+
141
+ for event in events:
142
+ try:
143
+ raw = event.model_dump(mode="json", exclude_none=False)
144
+ except Exception:
145
+ logger.debug(
146
+ "failed to serialize session event for livekit capture",
147
+ extra={"event_type": type(event).__name__},
148
+ exc_info=True,
149
+ )
150
+ continue
151
+
152
+ if isinstance(raw, dict) and isinstance(raw.get("type"), str):
153
+ events_out.append(raw)
154
+
155
+ return events_out
156
+
157
+
158
+ def _resolve_call_id(
159
+ *,
160
+ room_name: str,
161
+ participant_identity: str | None,
162
+ participant_metadata_raw: str | None,
163
+ external_agent_id: str,
164
+ strategy: str = CALL_ID_STRATEGY_ROOM_NAME,
165
+ metadata_key: str = _CALL_ID_METADATA_KEY,
166
+ resolve_call_id: CallIdResolver | None = None,
167
+ ) -> str:
168
+ fallback = room_name
169
+
170
+ if strategy == CALL_ID_STRATEGY_ROOM_NAME:
171
+ return fallback
172
+
173
+ if strategy == CALL_ID_STRATEGY_PARTICIPANT_IDENTITY:
174
+ return _resolved_string_or_fallback(participant_identity, fallback)
175
+
176
+ if strategy == CALL_ID_STRATEGY_PARTICIPANT_METADATA:
177
+ metadata = _parse_metadata(participant_metadata_raw)
178
+ return _resolved_string_or_fallback(metadata.get(metadata_key), fallback)
179
+
180
+ if strategy == CALL_ID_STRATEGY_CUSTOM and resolve_call_id is not None:
181
+ return _resolve_custom_call_id(
182
+ room_name=room_name,
183
+ participant_identity=participant_identity,
184
+ participant_metadata_raw=participant_metadata_raw,
185
+ external_agent_id=external_agent_id,
186
+ resolve_call_id=resolve_call_id,
187
+ fallback=fallback,
188
+ )
189
+
190
+ return fallback
191
+
192
+
193
+ def _resolve_custom_call_id(
194
+ *,
195
+ room_name: str,
196
+ participant_identity: str | None,
197
+ participant_metadata_raw: str | None,
198
+ external_agent_id: str,
199
+ resolve_call_id: CallIdResolver,
200
+ fallback: str,
201
+ ) -> str:
202
+ try:
203
+ resolved_call_id = resolve_call_id(
204
+ CallIdResolutionContext(
205
+ room_name=room_name,
206
+ participant_identity=participant_identity,
207
+ participant_metadata_raw=participant_metadata_raw,
208
+ external_agent_id=external_agent_id,
209
+ )
210
+ )
211
+ except Exception:
212
+ logger.warning(
213
+ "custom call_id resolver failed; falling back to room name",
214
+ extra={"room_name": room_name},
215
+ exc_info=True,
216
+ )
217
+ return fallback
218
+
219
+ return _resolved_string_or_fallback(resolved_call_id, fallback)
220
+
221
+
222
+ def _resolved_string_or_fallback(value: object, fallback: str) -> str:
223
+ if value is None:
224
+ return fallback
225
+
226
+ candidate = str(value).strip()
227
+ return candidate or fallback
228
+
229
+
230
+ def _parse_metadata(metadata_raw: str | None) -> dict[str, Any]:
231
+ if not metadata_raw:
232
+ return {}
233
+
234
+ try:
235
+ parsed = json.loads(metadata_raw)
236
+ except json.JSONDecodeError:
237
+ return {}
238
+
239
+ if isinstance(parsed, dict):
240
+ return parsed
241
+
242
+ return {}
243
+
244
+
245
+ def _resolve_test_case_run_id(
246
+ *,
247
+ participant_metadata_raw: str | None,
248
+ recording_context: Mapping[str, Any] | None,
249
+ ) -> str | None:
250
+ metadata = _parse_metadata(participant_metadata_raw)
251
+ for key in (
252
+ "test_case_run_id",
253
+ "testCaseRunId",
254
+ "conversation_id",
255
+ "conversationId",
256
+ ):
257
+ value = metadata.get(key)
258
+ if value is None:
259
+ continue
260
+ candidate = str(value).strip()
261
+ if candidate:
262
+ return candidate
263
+
264
+ if not recording_context:
265
+ return None
266
+
267
+ for key in (
268
+ "customer_conversation_id",
269
+ "test_case_run_id",
270
+ "testCaseRunId",
271
+ "conversation_id",
272
+ "conversationId",
273
+ ):
274
+ value = recording_context.get(key)
275
+ if value is None:
276
+ continue
277
+ candidate = str(value).strip()
278
+ if candidate:
279
+ return candidate
280
+
281
+ return None
282
+
283
+
284
+ def _resolve_status(close_event: CloseEvent | None) -> str:
285
+ return "error" if _serialize_close_reason(close_event) == "error" else "ended"
286
+
287
+
288
+ def _serialize_close_reason(close_event: CloseEvent | None) -> str | None:
289
+ if close_event is None:
290
+ return None
291
+
292
+ reason = close_event.reason
293
+ if reason is None:
294
+ return None
295
+ if isinstance(reason, str):
296
+ return reason
297
+
298
+ enum_value = getattr(reason, "value", None)
299
+ if isinstance(enum_value, str):
300
+ return enum_value
301
+
302
+ return str(reason)