livekit-plugins-hedra 1.1.0__tar.gz

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.

Potentially problematic release.


This version of livekit-plugins-hedra might be problematic. Click here for more details.

@@ -0,0 +1,172 @@
1
+ **/.vscode
2
+ **/.DS_Store
3
+
4
+ # Byte-compiled / optimized / DLL files
5
+ __pycache__/
6
+ *.py[cod]
7
+ *$py.class
8
+
9
+ # C extensions
10
+ *.so
11
+
12
+ # Distribution / packaging
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ share/python-wheels/
27
+ *.egg-info/
28
+ .installed.cfg
29
+ *.egg
30
+ MANIFEST
31
+
32
+ # PyInstaller
33
+ # Usually these files are written by a python script from a template
34
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
35
+ *.manifest
36
+ *.spec
37
+
38
+ # Installer logs
39
+ pip-log.txt
40
+ pip-delete-this-directory.txt
41
+
42
+ # Unit test / coverage reports
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+ .coverage
47
+ .coverage.*
48
+ .cache
49
+ nosetests.xml
50
+ coverage.xml
51
+ *.cover
52
+ *.py,cover
53
+ .hypothesis/
54
+ .pytest_cache/
55
+ cover/
56
+
57
+ # Translations
58
+ *.mo
59
+ *.pot
60
+
61
+ # Django stuff:
62
+ *.log
63
+ local_settings.py
64
+ db.sqlite3
65
+ db.sqlite3-journal
66
+
67
+ # Flask stuff:
68
+ instance/
69
+ .webassets-cache
70
+
71
+ # Scrapy stuff:
72
+ .scrapy
73
+
74
+ # Sphinx documentation
75
+ docs/_build/
76
+
77
+ # PyBuilder
78
+ .pybuilder/
79
+ target/
80
+
81
+ # Jupyter Notebook
82
+ .ipynb_checkpoints
83
+
84
+ # IPython
85
+ profile_default/
86
+ ipython_config.py
87
+
88
+ # pyenv
89
+ # For a library or package, you might want to ignore these files since the code is
90
+ # intended to run in multiple environments; otherwise, check them in:
91
+ # .python-version
92
+
93
+ # pipenv
94
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
96
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
97
+ # install all needed dependencies.
98
+ #Pipfile.lock
99
+
100
+ # poetry
101
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
102
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
103
+ # commonly ignored for libraries.
104
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
105
+ #poetry.lock
106
+
107
+ # pdm
108
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
109
+ #pdm.lock
110
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
111
+ # in version control.
112
+ # https://pdm.fming.dev/#use-with-ide
113
+ .pdm.toml
114
+
115
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
116
+ __pypackages__/
117
+
118
+ # Celery stuff
119
+ celerybeat-schedule
120
+ celerybeat.pid
121
+
122
+ # SageMath parsed files
123
+ *.sage.py
124
+
125
+ # Environments
126
+ .env
127
+ .venv
128
+ env/
129
+ venv/
130
+ ENV/
131
+ env.bak/
132
+ venv.bak/
133
+
134
+ # Spyder project settings
135
+ .spyderproject
136
+ .spyproject
137
+
138
+ # Rope project settings
139
+ .ropeproject
140
+
141
+ # mkdocs documentation
142
+ /site
143
+
144
+ # mypy
145
+ .mypy_cache/
146
+ .dmypy.json
147
+ dmypy.json
148
+
149
+ # trunk
150
+ .trunk/
151
+
152
+ # Pyre type checker
153
+ .pyre/
154
+
155
+ # pytype static type analyzer
156
+ .pytype/
157
+
158
+ # Cython debug symbols
159
+ cython_debug/
160
+
161
+ # PyCharm
162
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
163
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
164
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
165
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
166
+ .idea/
167
+
168
+ node_modules
169
+
170
+ credentials.json
171
+ pyrightconfig.json
172
+ docs/
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.4
2
+ Name: livekit-plugins-hedra
3
+ Version: 1.1.0
4
+ Summary: Agent Framework plugin for Hedra Avatar
5
+ Project-URL: Documentation, https://docs.livekit.io
6
+ Project-URL: Website, https://livekit.io/
7
+ Project-URL: Source, https://github.com/livekit/agents
8
+ Author-email: LiveKit <support@livekit.io>
9
+ License-Expression: Apache-2.0
10
+ Keywords: audio,livekit,realtime,video,webrtc
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: Apache Software License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Topic :: Multimedia :: Sound/Audio
18
+ Classifier: Topic :: Multimedia :: Video
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Requires-Python: >=3.9.0
21
+ Requires-Dist: livekit-agents>=1.1.0
22
+ Description-Content-Type: text/markdown
23
+
24
+ # LiveKit Plugins for Hedra Avatar
@@ -0,0 +1 @@
1
+ # LiveKit Plugins for Hedra Avatar
@@ -0,0 +1,35 @@
1
+ # Copyright 2023 LiveKit, 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
+
16
+ from .avatar import AvatarSession, HedraException
17
+ from .version import __version__
18
+
19
+ __all__ = [
20
+ "HedraException",
21
+ "AvatarSession",
22
+ "__version__",
23
+ ]
24
+
25
+ from livekit.agents import Plugin
26
+
27
+ from .log import logger
28
+
29
+
30
+ class HedraPlugin(Plugin):
31
+ def __init__(self) -> None:
32
+ super().__init__(__name__, __version__, __package__, logger)
33
+
34
+
35
+ Plugin.register_plugin(HedraPlugin())
@@ -0,0 +1,165 @@
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import io
5
+ import os
6
+
7
+ import aiohttp
8
+ from PIL.Image import Image
9
+
10
+ from livekit import api, rtc
11
+ from livekit.agents import (
12
+ DEFAULT_API_CONNECT_OPTIONS,
13
+ NOT_GIVEN,
14
+ AgentSession,
15
+ APIConnectionError,
16
+ APIConnectOptions,
17
+ APIStatusError,
18
+ NotGivenOr,
19
+ get_job_context,
20
+ utils,
21
+ )
22
+ from livekit.agents.voice.avatar import DataStreamAudioOutput
23
+ from livekit.agents.voice.room_io import ATTRIBUTE_PUBLISH_ON_BEHALF
24
+
25
+ from .log import logger
26
+
27
+ DEFAULT_API_URL = "https://api.hedra.com/public/livekit/v1/session"
28
+ _AVATAR_AGENT_IDENTITY = "hedra-avatar-agent"
29
+ _AVATAR_AGENT_NAME = "hedra-avatar-agent"
30
+
31
+
32
+ class HedraException(Exception):
33
+ """Exception for Hedra errors"""
34
+
35
+
36
+ class AvatarSession:
37
+ """A Hedra avatar session"""
38
+
39
+ def __init__(
40
+ self,
41
+ *,
42
+ avatar_id: NotGivenOr[str | None] = NOT_GIVEN,
43
+ avatar_image: NotGivenOr[Image] = NOT_GIVEN,
44
+ api_url: NotGivenOr[str] = NOT_GIVEN,
45
+ api_key: NotGivenOr[str] = NOT_GIVEN,
46
+ avatar_participant_identity: NotGivenOr[str] = NOT_GIVEN,
47
+ avatar_participant_name: NotGivenOr[str] = NOT_GIVEN,
48
+ conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS,
49
+ ) -> None:
50
+ self._avatar_id = avatar_id
51
+ self._avatar_image = avatar_image
52
+ if not self._avatar_id and not self._avatar_image:
53
+ raise HedraException("avatar_id or avatar_image must be provided")
54
+
55
+ self._api_url = api_url or os.getenv("HEDRA_API_URL", DEFAULT_API_URL)
56
+ self._api_key = api_key or os.getenv("HEDRA_API_KEY")
57
+ if self._api_key is None:
58
+ raise HedraException(
59
+ "The api_key must be set either by passing api_key to the client or "
60
+ "by setting the HEDRA_API_KEY environment variable"
61
+ )
62
+
63
+ self._avatar_participant_identity = avatar_participant_identity or _AVATAR_AGENT_IDENTITY
64
+ self._avatar_participant_name = avatar_participant_name or _AVATAR_AGENT_NAME
65
+ self._http_session: aiohttp.ClientSession | None = None
66
+ self._conn_options = conn_options
67
+
68
+ def _ensure_http_session(self) -> aiohttp.ClientSession:
69
+ if self._http_session is None:
70
+ self._http_session = utils.http_context.http_session()
71
+
72
+ return self._http_session
73
+
74
+ async def start(
75
+ self,
76
+ agent_session: AgentSession,
77
+ room: rtc.Room,
78
+ *,
79
+ livekit_url: NotGivenOr[str] = NOT_GIVEN,
80
+ livekit_api_key: NotGivenOr[str] = NOT_GIVEN,
81
+ livekit_api_secret: NotGivenOr[str] = NOT_GIVEN,
82
+ ) -> None:
83
+ livekit_url = livekit_url or (os.getenv("LIVEKIT_URL") or NOT_GIVEN)
84
+ livekit_api_key = livekit_api_key or (os.getenv("LIVEKIT_API_KEY") or NOT_GIVEN)
85
+ livekit_api_secret = livekit_api_secret or (os.getenv("LIVEKIT_API_SECRET") or NOT_GIVEN)
86
+ if not livekit_url or not livekit_api_key or not livekit_api_secret:
87
+ raise HedraException(
88
+ "livekit_url, livekit_api_key, and livekit_api_secret must be set "
89
+ "by arguments or environment variables"
90
+ )
91
+
92
+ try:
93
+ job_ctx = get_job_context()
94
+ decoded = job_ctx.decode_token()
95
+ local_participant_identity = decoded["sub"]
96
+ except (RuntimeError, KeyError):
97
+ if not room.isconnected():
98
+ raise HedraException(
99
+ "local participant identity not found in token, and room is not connected"
100
+ ) from None
101
+ local_participant_identity = room.local_participant.identity
102
+
103
+ livekit_token = (
104
+ api.AccessToken(api_key=livekit_api_key, api_secret=livekit_api_secret)
105
+ .with_kind("agent")
106
+ .with_identity(self._avatar_participant_identity)
107
+ .with_name(self._avatar_participant_name)
108
+ .with_grants(api.VideoGrants(room_join=True, room=room.name))
109
+ # allow the avatar agent to publish audio and video on behalf of your local agent
110
+ .with_attributes({ATTRIBUTE_PUBLISH_ON_BEHALF: local_participant_identity})
111
+ .to_jwt()
112
+ )
113
+
114
+ logger.debug("starting avatar session")
115
+ await self._start_agent(livekit_url, livekit_token)
116
+
117
+ agent_session.output.audio = DataStreamAudioOutput(
118
+ room=room,
119
+ destination_identity=self._avatar_participant_identity,
120
+ )
121
+
122
+ async def _start_agent(self, livekit_url: str, livekit_token: str) -> None:
123
+ assert self._api_key is not None
124
+ assert isinstance(self._api_url, str)
125
+
126
+ data = aiohttp.FormData({"livekit_url": livekit_url, "livekit_token": livekit_token})
127
+
128
+ if self._avatar_id:
129
+ data.add_field("avatar_id", self._avatar_id)
130
+
131
+ if self._avatar_image:
132
+ img_byte_arr = io.BytesIO()
133
+ self._avatar_image.save(img_byte_arr, format="JPEG", quality=95)
134
+ img_byte_arr.seek(0)
135
+ data.add_field(
136
+ "avatar_image", img_byte_arr, filename="avatar.jpg", content_type="image/jpeg"
137
+ )
138
+
139
+ for i in range(self._conn_options.max_retry):
140
+ try:
141
+ async with self._ensure_http_session().post(
142
+ self._api_url,
143
+ headers={
144
+ "x-api-key": self._api_key,
145
+ },
146
+ data=data,
147
+ timeout=aiohttp.ClientTimeout(sock_connect=self._conn_options.timeout),
148
+ ) as response:
149
+ if not response.ok:
150
+ text = await response.text()
151
+ raise APIStatusError(
152
+ "Server returned an error", status_code=response.status, body=text
153
+ )
154
+ return
155
+
156
+ except Exception as e:
157
+ if isinstance(e, APIConnectionError):
158
+ logger.warning("failed to call hedra avatar api", extra={"error": str(e)})
159
+ else:
160
+ logger.exception("failed to call hedra avatar api")
161
+
162
+ if i < self._conn_options.max_retry - 1:
163
+ await asyncio.sleep(self._conn_options.retry_interval)
164
+
165
+ raise APIConnectionError("Failed to start Hedra Avatar Session after all retries")
@@ -0,0 +1,3 @@
1
+ import logging
2
+
3
+ logger = logging.getLogger("livekit.plugins.hedra")
@@ -0,0 +1,15 @@
1
+ # Copyright 2025 LiveKit, 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
+ __version__ = "1.1.0"
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "livekit-plugins-hedra"
7
+ dynamic = ["version"]
8
+ description = "Agent Framework plugin for Hedra Avatar"
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ requires-python = ">=3.9.0"
12
+ authors = [{ name = "LiveKit", email = "support@livekit.io" }]
13
+ keywords = ["webrtc", "realtime", "audio", "video", "livekit"]
14
+ classifiers = [
15
+ "Intended Audience :: Developers",
16
+ "License :: OSI Approved :: Apache Software License",
17
+ "Topic :: Multimedia :: Sound/Audio",
18
+ "Topic :: Multimedia :: Video",
19
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3 :: Only",
24
+ ]
25
+ dependencies = ["livekit-agents>=1.1.0"]
26
+
27
+ [project.urls]
28
+ Documentation = "https://docs.livekit.io"
29
+ Website = "https://livekit.io/"
30
+ Source = "https://github.com/livekit/agents"
31
+
32
+ [tool.hatch.version]
33
+ path = "livekit/plugins/hedra/version.py"
34
+
35
+ [tool.hatch.build.targets.wheel]
36
+ packages = ["livekit"]
37
+
38
+ [tool.hatch.build.targets.sdist]
39
+ include = ["/livekit"]