Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
openspline/examples/node/elevenlabs_agents.mjs at main · codehornets/openspline · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
codehornets
openspline
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
openspline
/
examples
/
node
/
elevenlabs_agents.mjs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
53 lines (51 loc) · 2.67 KB
main
Breadcrumbs
openspline
/
examples
/
node
/
elevenlabs_agents.mjs
Copy path
Top
File metadata and controls
Code
Blame
53 lines (51 loc) · 2.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Play one response. Use the web demo for microphone conversations.
import
WebSocket
from
'ws'
;
import
{
Openspline
}
from
'@openspline/node'
;
import
{
ElevenLabsAgents
,
eventQueue
}
from
'@openspline/node/integrations'
;
const
agentId
=
process
.
env
.
ELEVENLABS_AGENT_ID
;
if
(
!
agentId
)
throw
new
Error
(
'Set ELEVENLABS_AGENT_ID.'
)
;
const
query
=
new
URLSearchParams
(
{
agent_id
:
agentId
}
)
;
let
url
=
`wss://api.elevenlabs.io/v1/convai/conversation?
${
query
}
`
;
if
(
process
.
env
.
ELEVENLABS_API_KEY
)
{
const
response
=
await
fetch
(
`https://api.elevenlabs.io/v1/convai/conversation/get-signed-url?
${
query
}
`
,
{
headers
:
{
'xi-api-key'
:
process
.
env
.
ELEVENLABS_API_KEY
}
,
signal
:
AbortSignal
.
timeout
(
20000
)
,
}
)
;
if
(
!
response
.
ok
)
throw
new
Error
(
`ElevenLabs authentication failed (HTTP
${
response
.
status
}
).`
)
;
url
=
(
await
response
.
json
(
)
)
.
signed_url
;
}
const
avatar
=
await
new
Openspline
(
)
.
avatar
(
process
.
argv
[
2
]
)
;
let
socket
,
timer
;
try
{
console
.
log
(
avatar
.
viewerUrl
)
;
await
avatar
.
waitForViewer
(
)
;
const
adapter
=
new
ElevenLabsAgents
(
avatar
)
;
let
complete
,
fail
,
heardAudio
=
false
;
const
finished
=
new
Promise
(
(
resolve
,
reject
)
=>
{
complete
=
resolve
;
fail
=
reject
;
}
)
;
socket
=
new
WebSocket
(
url
)
;
const
send
=
event
=>
socket
.
send
(
JSON
.
stringify
(
event
)
)
;
const
queue
=
eventQueue
(
async
event
=>
{
await
adapter
.
handle
(
event
)
;
if
(
event
.
type
===
'ping'
)
send
(
{
type
:
'pong'
,
event_id
:
event
.
ping_event
.
event_id
}
)
;
else
if
(
event
.
type
===
'conversation_initiation_metadata'
)
send
(
{
type
:
'user_message'
,
text
:
'Introduce yourself briefly.'
}
)
;
else
if
(
event
.
type
===
'client_error'
)
throw
new
Error
(
'ElevenLabs rejected the conversation. Check your agent configuration.'
)
;
else
if
(
event
.
type
===
'client_tool_call'
)
throw
new
Error
(
'Add your client tool handler before using this agent.'
)
;
else
if
(
event
.
type
===
'audio'
)
{
heardAudio
=
true
;
if
(
event
.
audio_event
.
is_final
)
complete
(
)
;
}
else
if
(
event
.
type
===
'agent_response_complete'
&&
heardAudio
)
complete
(
)
;
}
)
;
socket
.
on
(
'open'
,
(
)
=>
send
(
{
type
:
'conversation_initiation_client_data'
}
)
)
;
socket
.
on
(
'message'
,
raw
=>
{
try
{
queue
.
push
(
JSON
.
parse
(
raw
.
toString
(
)
)
)
.
catch
(
fail
)
;
}
catch
(
error
)
{
fail
(
error
)
;
}
}
)
;
socket
.
on
(
'error'
,
(
)
=>
fail
(
new
Error
(
'ElevenLabs connection failed. Check agent access and credentials.'
)
)
)
;
socket
.
on
(
'close'
,
(
)
=>
fail
(
new
Error
(
'ElevenLabs closed before completing a response.'
)
)
)
;
timer
=
setTimeout
(
(
)
=>
fail
(
new
Error
(
'Timed out waiting for ElevenLabs audio.'
)
)
,
60000
)
;
try
{
await
finished
;
}
finally
{
socket
.
close
(
)
;
await
queue
.
drain
(
)
;
}
await
avatar
.
endTurn
(
)
;
}
finally
{
clearTimeout
(
timer
)
;
socket
?.
close
(
)
;
await
avatar
.
close
(
)
;
}
You can’t perform that action at this time.