saa-livekit-client 0.3.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.
- saa_livekit_client-0.3.0/LICENSE +201 -0
- saa_livekit_client-0.3.0/PKG-INFO +175 -0
- saa_livekit_client-0.3.0/README.md +157 -0
- saa_livekit_client-0.3.0/__init__.py +52 -0
- saa_livekit_client-0.3.0/_wire.py +84 -0
- saa_livekit_client-0.3.0/api.py +168 -0
- saa_livekit_client-0.3.0/engine.py +469 -0
- saa_livekit_client-0.3.0/factory.py +140 -0
- saa_livekit_client-0.3.0/pyproject.toml +31 -0
- saa_livekit_client-0.3.0/saa_livekit_client.egg-info/PKG-INFO +175 -0
- saa_livekit_client-0.3.0/saa_livekit_client.egg-info/SOURCES.txt +22 -0
- saa_livekit_client-0.3.0/saa_livekit_client.egg-info/dependency_links.txt +1 -0
- saa_livekit_client-0.3.0/saa_livekit_client.egg-info/requires.txt +4 -0
- saa_livekit_client-0.3.0/saa_livekit_client.egg-info/top_level.txt +1 -0
- saa_livekit_client-0.3.0/setup.cfg +4 -0
- saa_livekit_client-0.3.0/tokens.py +60 -0
- saa_livekit_client-0.3.0/types.py +130 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of tracking or improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Attention Labs Incorporated
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: saa-livekit-client
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: LiveKit client for the saa hosted bridge — consume attention/interrupt/interjection events inside your voice agent.
|
|
5
|
+
Author: Attention Labs
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://attentionlabs.ai
|
|
8
|
+
Project-URL: Documentation, https://attentionlabs.ai/docs/livekit
|
|
9
|
+
Keywords: livekit,voice-agent,attention,vad,turn-detection
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: livekit>=1.0
|
|
14
|
+
Requires-Dist: livekit-api>=1.0
|
|
15
|
+
Requires-Dist: livekit-agents>=1.0
|
|
16
|
+
Requires-Dist: httpx>=0.25
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# saa-livekit-client
|
|
20
|
+
|
|
21
|
+
LiveKit client for the [saa](https://attentionlabs.ai).
|
|
22
|
+
|
|
23
|
+
Adds attention-aware gating, barge-in, and proactive interjection to any LiveKit voice agent.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install saa-livekit-client
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quickstart: existing voice agent
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import asyncio
|
|
35
|
+
import os
|
|
36
|
+
|
|
37
|
+
from livekit.agents import Agent, AgentServer, AgentSession, JobContext, cli
|
|
38
|
+
from livekit.plugins import openai
|
|
39
|
+
from saa_livekit_client import (
|
|
40
|
+
AttentionEngine, attention_agent_token, start_attention_session,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class MyAssistant(Agent):
|
|
45
|
+
def __init__(self):
|
|
46
|
+
super().__init__(instructions="You are a helpful assistant")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
server = AgentServer()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@server.rtc_session()
|
|
53
|
+
async def entrypoint(ctx: JobContext):
|
|
54
|
+
await ctx.connect()
|
|
55
|
+
user = await ctx.wait_for_participant()
|
|
56
|
+
|
|
57
|
+
# summon the saa hosted agent into the room
|
|
58
|
+
saa = await start_attention_session(
|
|
59
|
+
api_key=os.environ["SAA_API_KEY"],
|
|
60
|
+
livekit_url=os.environ["LIVEKIT_URL"],
|
|
61
|
+
agent_token=attention_agent_token(
|
|
62
|
+
api_key=os.environ["LIVEKIT_API_KEY"],
|
|
63
|
+
api_secret=os.environ["LIVEKIT_API_SECRET"],
|
|
64
|
+
room_name=ctx.room.name,
|
|
65
|
+
),
|
|
66
|
+
room_name=ctx.room.name,
|
|
67
|
+
participant_identity=user.identity,
|
|
68
|
+
)
|
|
69
|
+
ctx.add_shutdown_callback(saa.stop)
|
|
70
|
+
|
|
71
|
+
# stand up the voice agent, then wire saa on top of the running session
|
|
72
|
+
# (session.input / session.interrupt() are only valid once started)
|
|
73
|
+
voice = AgentSession(llm=openai.realtime.RealtimeModel(voice="alloy"))
|
|
74
|
+
await voice.start(agent=MyAssistant(), room=ctx.room)
|
|
75
|
+
|
|
76
|
+
engine = AttentionEngine(ctx.room, agent_identity=saa.agent_identity)
|
|
77
|
+
ctx.add_shutdown_callback(engine.stop)
|
|
78
|
+
|
|
79
|
+
@engine.on_prediction
|
|
80
|
+
def _(p):
|
|
81
|
+
# gate the mic, only class 2 (talking-to-device) reaches the model
|
|
82
|
+
voice.input.set_audio_enabled(p.aligned_class == 2)
|
|
83
|
+
|
|
84
|
+
@engine.on_interrupt
|
|
85
|
+
def _(ev):
|
|
86
|
+
voice.interrupt()
|
|
87
|
+
|
|
88
|
+
@engine.on_interjection
|
|
89
|
+
async def _(ev):
|
|
90
|
+
await voice.generate_reply(instructions="Briefly offer to help")
|
|
91
|
+
|
|
92
|
+
# tell saa when the agent is speaking, arms interrupt, suppresses interjection
|
|
93
|
+
@voice.on("agent_state_changed")
|
|
94
|
+
def _(ev):
|
|
95
|
+
if ev.new_state == "speaking":
|
|
96
|
+
asyncio.create_task(engine.responding_start())
|
|
97
|
+
elif ev.old_state == "speaking":
|
|
98
|
+
asyncio.create_task(engine.responding_stop())
|
|
99
|
+
|
|
100
|
+
await engine.start()
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
if __name__ == "__main__":
|
|
104
|
+
cli.run_app(server)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
That's the whole integration. Works with any LiveKit pipeline, including `RealtimeModel`
|
|
108
|
+
speech-to-speech. Runnable variants are in the [`examples/livekit/`](https://github.com/attenlabs/saa-sdk/tree/main/examples/livekit)
|
|
109
|
+
samples. (`WorkerOptions(entrypoint_fnc=...)` also works on 1.5.x, the older idiom.)
|
|
110
|
+
|
|
111
|
+
## Greenfield, `build_attention_entrypoint`
|
|
112
|
+
|
|
113
|
+
For new voice agents that don't have an existing pipeline:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from livekit.agents import AgentServer, JobContext, cli
|
|
117
|
+
from saa_livekit_client import build_attention_entrypoint, TurnReadyEvent
|
|
118
|
+
|
|
119
|
+
async def handle_turn(event: TurnReadyEvent, ctx: JobContext):
|
|
120
|
+
# event.audio_pcm16 = int16 mono 16 kHz; event.frames = list[TurnFrame]
|
|
121
|
+
response_pcm = await my_llm.respond(event.audio_pcm16, frames=event.frames)
|
|
122
|
+
await publish_response_audio(ctx.room, response_pcm)
|
|
123
|
+
|
|
124
|
+
entrypoint = build_attention_entrypoint(on_turn=handle_turn)
|
|
125
|
+
|
|
126
|
+
server = AgentServer()
|
|
127
|
+
server.rtc_session()(entrypoint)
|
|
128
|
+
# or the older idiom: cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
|
|
129
|
+
|
|
130
|
+
if __name__ == "__main__":
|
|
131
|
+
cli.run_app(server)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Environment: `SAA_API_KEY`, `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET`, `LIVEKIT_URL`.
|
|
135
|
+
|
|
136
|
+
## Event types
|
|
137
|
+
|
|
138
|
+
| Event | Fires | Payload |
|
|
139
|
+
|---|---|---|
|
|
140
|
+
| `PredictionEvent` | every 250 ms | `raw_class`, `aligned_class` (0/1/2), `confidence`, `source`, `num_faces`, `responding` |
|
|
141
|
+
| `VADEvent` | every 250 ms | `is_speech`, `probability` |
|
|
142
|
+
| warmup | model warmed up, predictions begin | — |
|
|
143
|
+
| listening_start / listening_cancelled | state edges | — |
|
|
144
|
+
| `TurnReadyEvent` | end of user turn | `audio_pcm16`, `duration`, `frames`, `context` |
|
|
145
|
+
| `InterruptEvent` | user barges in during AI playback | `confidence` |
|
|
146
|
+
| `InterjectionEvent` | humans went quiet after side-chat | `reason`, `audio_pcm16`, `duration` |
|
|
147
|
+
| `ErrorEvent` | out-of-band errors | `code`, `message` |
|
|
148
|
+
|
|
149
|
+
Classes: `0`=silent, `1`=human-to-human, `2`=human-to-device. `responding` is `True` while the AI is mid-playback.
|
|
150
|
+
|
|
151
|
+
Each is delivered through an `@engine.on_*` callback: `on_prediction`, `on_vad`, `on_warmup`, `on_listening_start`, `on_listening_cancelled`, `on_turn_ready`, `on_interrupt`, `on_interjection`, `on_error`.
|
|
152
|
+
|
|
153
|
+
## Upstream actions
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
await attention.mute() # stop feeding mic to processor
|
|
157
|
+
await attention.unmute()
|
|
158
|
+
await attention.responding_start() # AI is now speaking
|
|
159
|
+
await attention.responding_stop()
|
|
160
|
+
await attention.set_threshold(0.65) # model class-2 confidence threshold
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
These are routed only to the SAA agent (`destination_identities=[...]`)
|
|
164
|
+
so they never leak to other room participants.
|
|
165
|
+
|
|
166
|
+
## Requirements
|
|
167
|
+
|
|
168
|
+
- Python 3.10+
|
|
169
|
+
- LiveKit URL must be publicly reachable from our cloud (no private VPC)
|
|
170
|
+
- Audio + video tracks must both be available (the model is multimodal)
|
|
171
|
+
- Customer voice agent and hosted attention agent share the same LiveKit room
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
Apache-2.0. © Attention Labs.
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# saa-livekit-client
|
|
2
|
+
|
|
3
|
+
LiveKit client for the [saa](https://attentionlabs.ai).
|
|
4
|
+
|
|
5
|
+
Adds attention-aware gating, barge-in, and proactive interjection to any LiveKit voice agent.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install saa-livekit-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quickstart: existing voice agent
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
import asyncio
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
from livekit.agents import Agent, AgentServer, AgentSession, JobContext, cli
|
|
20
|
+
from livekit.plugins import openai
|
|
21
|
+
from saa_livekit_client import (
|
|
22
|
+
AttentionEngine, attention_agent_token, start_attention_session,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class MyAssistant(Agent):
|
|
27
|
+
def __init__(self):
|
|
28
|
+
super().__init__(instructions="You are a helpful assistant")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
server = AgentServer()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@server.rtc_session()
|
|
35
|
+
async def entrypoint(ctx: JobContext):
|
|
36
|
+
await ctx.connect()
|
|
37
|
+
user = await ctx.wait_for_participant()
|
|
38
|
+
|
|
39
|
+
# summon the saa hosted agent into the room
|
|
40
|
+
saa = await start_attention_session(
|
|
41
|
+
api_key=os.environ["SAA_API_KEY"],
|
|
42
|
+
livekit_url=os.environ["LIVEKIT_URL"],
|
|
43
|
+
agent_token=attention_agent_token(
|
|
44
|
+
api_key=os.environ["LIVEKIT_API_KEY"],
|
|
45
|
+
api_secret=os.environ["LIVEKIT_API_SECRET"],
|
|
46
|
+
room_name=ctx.room.name,
|
|
47
|
+
),
|
|
48
|
+
room_name=ctx.room.name,
|
|
49
|
+
participant_identity=user.identity,
|
|
50
|
+
)
|
|
51
|
+
ctx.add_shutdown_callback(saa.stop)
|
|
52
|
+
|
|
53
|
+
# stand up the voice agent, then wire saa on top of the running session
|
|
54
|
+
# (session.input / session.interrupt() are only valid once started)
|
|
55
|
+
voice = AgentSession(llm=openai.realtime.RealtimeModel(voice="alloy"))
|
|
56
|
+
await voice.start(agent=MyAssistant(), room=ctx.room)
|
|
57
|
+
|
|
58
|
+
engine = AttentionEngine(ctx.room, agent_identity=saa.agent_identity)
|
|
59
|
+
ctx.add_shutdown_callback(engine.stop)
|
|
60
|
+
|
|
61
|
+
@engine.on_prediction
|
|
62
|
+
def _(p):
|
|
63
|
+
# gate the mic, only class 2 (talking-to-device) reaches the model
|
|
64
|
+
voice.input.set_audio_enabled(p.aligned_class == 2)
|
|
65
|
+
|
|
66
|
+
@engine.on_interrupt
|
|
67
|
+
def _(ev):
|
|
68
|
+
voice.interrupt()
|
|
69
|
+
|
|
70
|
+
@engine.on_interjection
|
|
71
|
+
async def _(ev):
|
|
72
|
+
await voice.generate_reply(instructions="Briefly offer to help")
|
|
73
|
+
|
|
74
|
+
# tell saa when the agent is speaking, arms interrupt, suppresses interjection
|
|
75
|
+
@voice.on("agent_state_changed")
|
|
76
|
+
def _(ev):
|
|
77
|
+
if ev.new_state == "speaking":
|
|
78
|
+
asyncio.create_task(engine.responding_start())
|
|
79
|
+
elif ev.old_state == "speaking":
|
|
80
|
+
asyncio.create_task(engine.responding_stop())
|
|
81
|
+
|
|
82
|
+
await engine.start()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
cli.run_app(server)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
That's the whole integration. Works with any LiveKit pipeline, including `RealtimeModel`
|
|
90
|
+
speech-to-speech. Runnable variants are in the [`examples/livekit/`](https://github.com/attenlabs/saa-sdk/tree/main/examples/livekit)
|
|
91
|
+
samples. (`WorkerOptions(entrypoint_fnc=...)` also works on 1.5.x, the older idiom.)
|
|
92
|
+
|
|
93
|
+
## Greenfield, `build_attention_entrypoint`
|
|
94
|
+
|
|
95
|
+
For new voice agents that don't have an existing pipeline:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from livekit.agents import AgentServer, JobContext, cli
|
|
99
|
+
from saa_livekit_client import build_attention_entrypoint, TurnReadyEvent
|
|
100
|
+
|
|
101
|
+
async def handle_turn(event: TurnReadyEvent, ctx: JobContext):
|
|
102
|
+
# event.audio_pcm16 = int16 mono 16 kHz; event.frames = list[TurnFrame]
|
|
103
|
+
response_pcm = await my_llm.respond(event.audio_pcm16, frames=event.frames)
|
|
104
|
+
await publish_response_audio(ctx.room, response_pcm)
|
|
105
|
+
|
|
106
|
+
entrypoint = build_attention_entrypoint(on_turn=handle_turn)
|
|
107
|
+
|
|
108
|
+
server = AgentServer()
|
|
109
|
+
server.rtc_session()(entrypoint)
|
|
110
|
+
# or the older idiom: cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
|
|
111
|
+
|
|
112
|
+
if __name__ == "__main__":
|
|
113
|
+
cli.run_app(server)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Environment: `SAA_API_KEY`, `LIVEKIT_API_KEY`, `LIVEKIT_API_SECRET`, `LIVEKIT_URL`.
|
|
117
|
+
|
|
118
|
+
## Event types
|
|
119
|
+
|
|
120
|
+
| Event | Fires | Payload |
|
|
121
|
+
|---|---|---|
|
|
122
|
+
| `PredictionEvent` | every 250 ms | `raw_class`, `aligned_class` (0/1/2), `confidence`, `source`, `num_faces`, `responding` |
|
|
123
|
+
| `VADEvent` | every 250 ms | `is_speech`, `probability` |
|
|
124
|
+
| warmup | model warmed up, predictions begin | — |
|
|
125
|
+
| listening_start / listening_cancelled | state edges | — |
|
|
126
|
+
| `TurnReadyEvent` | end of user turn | `audio_pcm16`, `duration`, `frames`, `context` |
|
|
127
|
+
| `InterruptEvent` | user barges in during AI playback | `confidence` |
|
|
128
|
+
| `InterjectionEvent` | humans went quiet after side-chat | `reason`, `audio_pcm16`, `duration` |
|
|
129
|
+
| `ErrorEvent` | out-of-band errors | `code`, `message` |
|
|
130
|
+
|
|
131
|
+
Classes: `0`=silent, `1`=human-to-human, `2`=human-to-device. `responding` is `True` while the AI is mid-playback.
|
|
132
|
+
|
|
133
|
+
Each is delivered through an `@engine.on_*` callback: `on_prediction`, `on_vad`, `on_warmup`, `on_listening_start`, `on_listening_cancelled`, `on_turn_ready`, `on_interrupt`, `on_interjection`, `on_error`.
|
|
134
|
+
|
|
135
|
+
## Upstream actions
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
await attention.mute() # stop feeding mic to processor
|
|
139
|
+
await attention.unmute()
|
|
140
|
+
await attention.responding_start() # AI is now speaking
|
|
141
|
+
await attention.responding_stop()
|
|
142
|
+
await attention.set_threshold(0.65) # model class-2 confidence threshold
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
These are routed only to the SAA agent (`destination_identities=[...]`)
|
|
146
|
+
so they never leak to other room participants.
|
|
147
|
+
|
|
148
|
+
## Requirements
|
|
149
|
+
|
|
150
|
+
- Python 3.10+
|
|
151
|
+
- LiveKit URL must be publicly reachable from our cloud (no private VPC)
|
|
152
|
+
- Audio + video tracks must both be available (the model is multimodal)
|
|
153
|
+
- Customer voice agent and hosted attention agent share the same LiveKit room
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
Apache-2.0. © Attention Labs.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""saa-livekit-client — consume saa events inside a LiveKit voice agent.
|
|
2
|
+
|
|
3
|
+
Public exports:
|
|
4
|
+
|
|
5
|
+
AttentionEngine - listens to the hidden hosted agent's data
|
|
6
|
+
channel events and fires typed callbacks.
|
|
7
|
+
start_attention_session - summons the hosted agent into your room.
|
|
8
|
+
attention_agent_token - issues the hidden-participant token the
|
|
9
|
+
hosted agent uses to connect.
|
|
10
|
+
build_attention_entrypoint - factory that composes the above into a
|
|
11
|
+
ready-to-go entrypoint(ctx) function.
|
|
12
|
+
|
|
13
|
+
Event types:
|
|
14
|
+
PredictionEvent, VADEvent, TurnReadyEvent, TurnFrame,
|
|
15
|
+
InterruptEvent, InterjectionEvent, ErrorEvent
|
|
16
|
+
"""
|
|
17
|
+
from .api import (
|
|
18
|
+
AttentionAPIError,
|
|
19
|
+
SessionHandle,
|
|
20
|
+
start_attention_session,
|
|
21
|
+
)
|
|
22
|
+
from .engine import AttentionEngine, DATA_TOPIC
|
|
23
|
+
from .factory import build_attention_entrypoint
|
|
24
|
+
from .tokens import DEFAULT_AGENT_IDENTITY, attention_agent_token
|
|
25
|
+
from .types import (
|
|
26
|
+
ErrorEvent,
|
|
27
|
+
InterjectionEvent,
|
|
28
|
+
InterruptEvent,
|
|
29
|
+
PredictionEvent,
|
|
30
|
+
TurnFrame,
|
|
31
|
+
TurnReadyEvent,
|
|
32
|
+
VADEvent,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
__version__ = "0.3.0"
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
# Engine
|
|
40
|
+
"AttentionEngine", "DATA_TOPIC",
|
|
41
|
+
# REST client
|
|
42
|
+
"start_attention_session", "SessionHandle", "AttentionAPIError",
|
|
43
|
+
# Tokens
|
|
44
|
+
"attention_agent_token", "DEFAULT_AGENT_IDENTITY",
|
|
45
|
+
# Factory
|
|
46
|
+
"build_attention_entrypoint",
|
|
47
|
+
# Event types
|
|
48
|
+
"PredictionEvent", "VADEvent", "TurnReadyEvent", "TurnFrame",
|
|
49
|
+
"InterruptEvent", "InterjectionEvent", "ErrorEvent",
|
|
50
|
+
# Version
|
|
51
|
+
"__version__",
|
|
52
|
+
]
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Binary turn payload codec (parser side).
|
|
2
|
+
|
|
3
|
+
Wire format (`application/x-saa-turn`), little-endian throughout:
|
|
4
|
+
|
|
5
|
+
[4-byte uint32: pcm_byte_len]
|
|
6
|
+
[pcm_byte_len bytes: int16 LE PCM @ 16 kHz mono]
|
|
7
|
+
[for each frame:
|
|
8
|
+
[4-byte float32: ts_offset_s]
|
|
9
|
+
[4-byte uint32: jpeg_byte_len]
|
|
10
|
+
[jpeg_byte_len bytes: raw JPEG]
|
|
11
|
+
]
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import struct
|
|
16
|
+
from dataclasses import dataclass
|
|
17
|
+
|
|
18
|
+
from .types import TurnFrame
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
_HEADER = struct.Struct("<I") # pcm_byte_len
|
|
22
|
+
_FRAME_HEADER = struct.Struct("<fI") # ts_offset_s, jpeg_byte_len
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TurnPayloadError(ValueError):
|
|
26
|
+
"""Raised when the binary buffer does not match the wire format."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True)
|
|
30
|
+
class ParsedTurnPayload:
|
|
31
|
+
pcm16: bytes
|
|
32
|
+
frames: list[TurnFrame]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def parse_turn_payload(buf: bytes) -> ParsedTurnPayload:
|
|
36
|
+
"""Decode a turn payload received via LiveKit byte stream.
|
|
37
|
+
|
|
38
|
+
Raises TurnPayloadError on truncation or invalid length fields.
|
|
39
|
+
"""
|
|
40
|
+
if len(buf) < _HEADER.size:
|
|
41
|
+
raise TurnPayloadError(
|
|
42
|
+
f"buffer too short for header: {len(buf)} < {_HEADER.size}"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
(pcm_len,) = _HEADER.unpack_from(buf, 0)
|
|
46
|
+
pcm_start = _HEADER.size
|
|
47
|
+
pcm_end = pcm_start + pcm_len
|
|
48
|
+
if pcm_end > len(buf):
|
|
49
|
+
raise TurnPayloadError(
|
|
50
|
+
f"pcm length {pcm_len} exceeds buffer ({len(buf) - pcm_start} remaining)"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
pcm16 = bytes(buf[pcm_start:pcm_end])
|
|
54
|
+
|
|
55
|
+
frames: list[TurnFrame] = []
|
|
56
|
+
cursor = pcm_end
|
|
57
|
+
while cursor < len(buf):
|
|
58
|
+
if cursor + _FRAME_HEADER.size > len(buf):
|
|
59
|
+
raise TurnPayloadError(
|
|
60
|
+
f"truncated frame header at offset {cursor} "
|
|
61
|
+
f"({len(buf) - cursor} bytes remaining, need {_FRAME_HEADER.size})"
|
|
62
|
+
)
|
|
63
|
+
ts_offset_s, jpeg_len = _FRAME_HEADER.unpack_from(buf, cursor)
|
|
64
|
+
cursor += _FRAME_HEADER.size
|
|
65
|
+
if cursor + jpeg_len > len(buf):
|
|
66
|
+
raise TurnPayloadError(
|
|
67
|
+
f"truncated frame JPEG at offset {cursor} "
|
|
68
|
+
f"(need {jpeg_len}, have {len(buf) - cursor})"
|
|
69
|
+
)
|
|
70
|
+
frames.append(TurnFrame(
|
|
71
|
+
ts_offset_s=float(ts_offset_s),
|
|
72
|
+
jpeg_bytes=bytes(buf[cursor:cursor + jpeg_len]),
|
|
73
|
+
))
|
|
74
|
+
cursor += jpeg_len
|
|
75
|
+
|
|
76
|
+
return ParsedTurnPayload(pcm16=pcm16, frames=frames)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def encode_turn_payload(pcm16: bytes, frames: list[TurnFrame]) -> bytes:
|
|
80
|
+
parts: list[bytes] = [_HEADER.pack(len(pcm16)), pcm16]
|
|
81
|
+
for f in frames:
|
|
82
|
+
parts.append(_FRAME_HEADER.pack(f.ts_offset_s, len(f.jpeg_bytes)))
|
|
83
|
+
parts.append(f.jpeg_bytes)
|
|
84
|
+
return b"".join(parts)
|