@jambonz/sdk 0.2.0 → 0.3.1

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.
Files changed (74) hide show
  1. package/AGENTS.md +715 -0
  2. package/dist/client/index.cjs +3 -3
  3. package/dist/client/index.cjs.map +1 -1
  4. package/dist/client/index.d.cts +4 -4
  5. package/dist/client/index.d.ts +4 -4
  6. package/dist/client/index.js +3 -3
  7. package/dist/client/index.js.map +1 -1
  8. package/dist/index.cjs +14 -14
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +4 -4
  11. package/dist/index.d.ts +4 -4
  12. package/dist/index.js +14 -14
  13. package/dist/index.js.map +1 -1
  14. package/dist/{rest-B_GQITxM.d.cts → rest-BvOtq44O.d.cts} +3 -3
  15. package/dist/{rest-DgctQy2b.d.ts → rest-DAAEUWsa.d.ts} +3 -3
  16. package/dist/{session-DVGHbtan.d.cts → session-DKaSRkIo.d.cts} +19 -19
  17. package/dist/{session-DVGHbtan.d.ts → session-DKaSRkIo.d.ts} +19 -19
  18. package/dist/types/index.d.cts +3 -3
  19. package/dist/types/index.d.ts +3 -3
  20. package/dist/{verb-builder-B7RIVi0t.d.ts → verb-builder-CFPD9yWI.d.cts} +3 -3
  21. package/dist/{verb-builder-B6zPuGd1.d.cts → verb-builder-D5XRbTtu.d.ts} +3 -3
  22. package/dist/{verbs-B18Kazd9.d.cts → verbs-DKy6nGIH.d.cts} +6 -6
  23. package/dist/{verbs-B18Kazd9.d.ts → verbs-DKy6nGIH.d.ts} +6 -6
  24. package/dist/webhook/index.cjs +3 -3
  25. package/dist/webhook/index.cjs.map +1 -1
  26. package/dist/webhook/index.d.cts +2 -2
  27. package/dist/webhook/index.d.ts +2 -2
  28. package/dist/webhook/index.js +3 -3
  29. package/dist/webhook/index.js.map +1 -1
  30. package/dist/websocket/index.cjs +11 -11
  31. package/dist/websocket/index.cjs.map +1 -1
  32. package/dist/websocket/index.d.cts +7 -7
  33. package/dist/websocket/index.d.ts +7 -7
  34. package/dist/websocket/index.js +11 -11
  35. package/dist/websocket/index.js.map +1 -1
  36. package/examples/agent/ws-app.ts +138 -0
  37. package/examples/assemblyai-autogenerate-prompt/app.js +92 -0
  38. package/examples/assemblyai-autogenerate-prompt/package.json +14 -0
  39. package/examples/bedrock-agent.ts +267 -0
  40. package/examples/call-recording/README.md +24 -0
  41. package/examples/call-recording/app.ts +70 -0
  42. package/examples/call-recording/ws-app.ts +47 -0
  43. package/examples/deepgram-voice-agent/ws-app.ts +118 -0
  44. package/examples/dial/app.ts +22 -0
  45. package/examples/echo/app.ts +65 -0
  46. package/examples/echo/ws-app.ts +67 -0
  47. package/examples/elevenlabs-voice-agent/ws-app.ts +81 -0
  48. package/examples/hello-world/README.md +15 -0
  49. package/examples/hello-world/app.js +20 -0
  50. package/examples/hello-world/app.ts +19 -0
  51. package/examples/hello-world/ws-app.ts +18 -0
  52. package/examples/ivr-menu/README.md +15 -0
  53. package/examples/ivr-menu/app.js +85 -0
  54. package/examples/ivr-menu/app.ts +70 -0
  55. package/examples/listen-record/app.ts +22 -0
  56. package/examples/llm-streaming/ws-app.ts +96 -0
  57. package/examples/openai-realtime/ws-app.ts +131 -0
  58. package/examples/queue-with-hold/README.md +19 -0
  59. package/examples/queue-with-hold/app.ts +68 -0
  60. package/examples/queue-with-hold/ws-app.ts +41 -0
  61. package/examples/realtime-translator/src/app.ts +21 -0
  62. package/examples/realtime-translator/src/routes/translator.ts +108 -0
  63. package/examples/realtime-translator/src/utils/config.ts +21 -0
  64. package/examples/realtime-translator/src/utils/translate.ts +23 -0
  65. package/examples/retell-hosted/src/app.ts +22 -0
  66. package/examples/retell-hosted/src/config.ts +53 -0
  67. package/examples/retell-hosted/src/retell.ts +166 -0
  68. package/examples/two-stage-dialer/app.ts +96 -0
  69. package/examples/ultravox-voice-agent/ws-app.ts +116 -0
  70. package/examples/voice-agent/README.md +19 -0
  71. package/examples/voice-agent/app.js +133 -0
  72. package/examples/voice-agent/app.ts +132 -0
  73. package/examples/voice-agent/ws-app.ts +137 -0
  74. package/package.json +9 -4
@@ -0,0 +1,92 @@
1
+ const http = require('http');
2
+ const {createEndpoint} = require('@jambonz/sdk/websocket');
3
+
4
+ const server = http.createServer();
5
+ const makeService = createEndpoint({server, port: 3000});
6
+ const svc = makeService({path: '/'});
7
+
8
+ const recognizer = {
9
+ vendor: 'assemblyai',
10
+ language: 'en',
11
+ autogeneratePrompt: true,
12
+ assemblyAiOptions: {
13
+ serviceVersion: 'v3',
14
+ speechModel: 'u3-rt-pro'
15
+ }
16
+ };
17
+
18
+ const questions = [
19
+ 'What medication are you currently taking? For example, metformin, lisinopril, or atorvastatin.',
20
+ 'What is your street address including city and zip code?',
21
+ 'Can you spell your last name for me please?',
22
+ 'What is your date of birth? Please say the month, day, and year.',
23
+ 'What is your email address?',
24
+ 'What is the make, model, and year of your vehicle?',
25
+ 'Can you read me the six-digit confirmation code from your email? It should be a mix of letters and numbers.',
26
+ ];
27
+
28
+ svc.on('session:new', (session) => {
29
+ session.locals = {questionIndex: 0};
30
+
31
+ session
32
+ .on('close', (code, reason) => {
33
+ console.log(`Session ${session.callSid} closed: ${code}`);
34
+ })
35
+ .on('error', (err) => {
36
+ console.error(`Session ${session.callSid} error:`, err);
37
+ })
38
+ .on('/gather-result', (evt) => {
39
+ if (evt.reason === 'speechDetected' && evt.speech?.alternatives?.length) {
40
+ const transcript = evt.speech.alternatives[0].transcript;
41
+ const confidence = evt.speech.alternatives[0].confidence;
42
+ const question = questions[session.locals.questionIndex];
43
+ console.log(`Q: ${question}`);
44
+ console.log(`A: ${transcript} (confidence: ${confidence})`);
45
+ console.log('---');
46
+
47
+ session.locals.questionIndex++;
48
+
49
+ if (session.locals.questionIndex < questions.length) {
50
+ session
51
+ .say({text: `Got it. You said: ${transcript}.`})
52
+ .gather({
53
+ input: ['speech'],
54
+ actionHook: '/gather-result',
55
+ timeout: 15,
56
+ recognizer,
57
+ say: {text: `Next question. ${questions[session.locals.questionIndex]}`}
58
+ })
59
+ .reply();
60
+ } else {
61
+ session
62
+ .say({text: `Got it. You said: ${transcript}. That was the last question. Thanks for helping us test. Goodbye!`})
63
+ .hangup()
64
+ .reply();
65
+ }
66
+ } else {
67
+ console.log(`Gather completed with reason: ${evt.reason}`);
68
+ session
69
+ .gather({
70
+ input: ['speech'],
71
+ actionHook: '/gather-result',
72
+ timeout: 15,
73
+ recognizer,
74
+ say: {text: `I didn't catch that. Let me repeat the question. ${questions[session.locals.questionIndex]}`}
75
+ })
76
+ .reply();
77
+ }
78
+ });
79
+
80
+ session
81
+ .say({text: 'Welcome! This app tests Assembly A I auto-generate prompt. I\'m going to ask you a series of questions. The recognizer should be biased toward the type of answer each question expects.'})
82
+ .gather({
83
+ input: ['speech'],
84
+ actionHook: '/gather-result',
85
+ timeout: 15,
86
+ recognizer,
87
+ say: {text: `Here is the first question. ${questions[0]}`}
88
+ })
89
+ .send();
90
+ });
91
+
92
+ console.log('AssemblyAI autogeneratePrompt test app listening on port 3000');
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "assemblyai-test",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "app.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC",
11
+ "dependencies": {
12
+ "@jambonz/sdk": "^0.1.8"
13
+ }
14
+ }
@@ -0,0 +1,267 @@
1
+ import * as http from 'node:http';
2
+ import pino from 'pino';
3
+ import { createEndpoint, Session } from '@jambonz/sdk/websocket';
4
+
5
+ const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
6
+
7
+ const envVars = {
8
+ LLM_VENDOR: {
9
+ type: 'string' as const,
10
+ description: 'LLM vendor (openai, bedrock, anthropic, google)',
11
+ default: 'openai',
12
+ },
13
+ LLM_MODEL: {
14
+ type: 'string' as const,
15
+ description: 'LLM model to use',
16
+ default: 'gpt-4.1-mini',
17
+ },
18
+ CARTESIA_VOICE: {
19
+ type: 'string' as const,
20
+ description: 'Cartesia voice ID',
21
+ default: '9626c31c-bec5-4cca-baa8-f8ba9e84c8bc',
22
+ },
23
+ ELEVENLABS_VOICE: {
24
+ type: 'string' as const,
25
+ description: 'ElevenLabs voice id',
26
+ default: 'hpp4J3VqNfWAUOO0d1Us',
27
+ },
28
+ SYSTEM_PROMPT: {
29
+ type: 'string' as const,
30
+ description: 'System prompt for the voice agent',
31
+ uiHint: 'textarea' as const,
32
+ default: [
33
+ 'You are a helpful voice AI assistant.',
34
+ 'The user is interacting with you via voice,',
35
+ 'even if you perceive the conversation as text.',
36
+ 'You eagerly assist users with their questions',
37
+ 'by providing information from your extensive knowledge.',
38
+ 'Your responses are concise, to the point,',
39
+ 'and use natural spoken English with proper punctuation.',
40
+ 'Never use markdown, bullet points, numbered lists,',
41
+ 'emojis, asterisks, or any special formatting.',
42
+ 'You are curious, friendly, and have a sense of humor.',
43
+ 'When the conversation begins,',
44
+ 'greet the user in a helpful and friendly manner.',
45
+ ].join(' '),
46
+ },
47
+ };
48
+
49
+ interface SttConfig {
50
+ vendor: string;
51
+ language?: string;
52
+ deepgramOptions?: Record<string, unknown>;
53
+ assemblyAiOptions?: Record<string, unknown>;
54
+ speechmaticsOptions?: Record<string, unknown>;
55
+ }
56
+
57
+ interface TtsConfig {
58
+ vendor: string;
59
+ voiceEnvVar: string;
60
+ options?: Record<string, unknown>;
61
+ }
62
+
63
+ interface AgentOptions {
64
+ stt: SttConfig;
65
+ tts: TtsConfig;
66
+ turnDetection: 'krisp' | 'stt';
67
+ noiseIsolation?: 'krisp' | 'rnnoise';
68
+ }
69
+
70
+ function handleSession(session: Session, opts: AgentOptions) {
71
+ const log = logger.child({ call_sid: session.callSid });
72
+ const llmVendor = session.data.env_vars?.LLM_VENDOR || 'openai';
73
+ const model = session.data.env_vars?.LLM_MODEL || 'gpt-4.1-mini';
74
+ const voice = session.data.env_vars?.[opts.tts.voiceEnvVar]
75
+ || envVars[opts.tts.voiceEnvVar as keyof typeof envVars]?.default;
76
+ const systemPrompt = session.data.env_vars?.SYSTEM_PROMPT || envVars.SYSTEM_PROMPT.default;
77
+
78
+ /* Demo: update_tools mid-conversation to add web search capability.
79
+ After the user's second question (turn_end #2), inject a web_search tool.
80
+ The agent starts without web search, so early questions get stale answers.
81
+ Once the tool is added, it can search the web via Tavily. */
82
+ let turnCount = 0;
83
+ let toolsInjected = false;
84
+
85
+ session.on('/agent-event', (evt: Record<string, unknown>) => {
86
+ log.info({payload: evt}, `agent event: ${evt.type}`);
87
+
88
+ if (evt.type === 'turn_end') {
89
+ turnCount++;
90
+ if (turnCount === 2 && !toolsInjected) {
91
+ toolsInjected = true;
92
+ log.info('injecting web_search tool');
93
+ session.updateAgent({
94
+ type: 'update_tools',
95
+ tools: [
96
+ {
97
+ type: 'function',
98
+ function: {
99
+ name: 'web_search',
100
+ description: 'Search the web for current information. Use this when the user asks about recent events, product releases, or anything that may be newer than your training data.',
101
+ parameters: {
102
+ type: 'object',
103
+ properties: {
104
+ query: {
105
+ type: 'string',
106
+ description: 'The search query',
107
+ },
108
+ },
109
+ required: ['query'],
110
+ },
111
+ },
112
+ },
113
+ ],
114
+ });
115
+ session.updateAgent({
116
+ type: 'inject_context',
117
+ messages: [
118
+ {
119
+ role: 'system',
120
+ content: 'You now have access to a web_search tool. When the user asks about current products, recent releases, or anything that might be newer than your training data, use the web_search tool to get up-to-date information.',
121
+ },
122
+ ],
123
+ });
124
+ }
125
+ }
126
+ });
127
+
128
+ session.on('/tool-call', async (evt: Record<string, unknown>) => {
129
+ log.info({payload: evt}, 'tool call received');
130
+ if (evt.name === 'web_search') {
131
+ const args = evt.arguments as Record<string, string>;
132
+ const query = args?.query || '';
133
+ log.info({query}, 'searching Tavily');
134
+ try {
135
+ const res = await fetch('https://api.tavily.com/search', {
136
+ method: 'POST',
137
+ headers: {'Content-Type': 'application/json'},
138
+ body: JSON.stringify({
139
+ api_key: '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',
140
+ query,
141
+ max_results: 3,
142
+ search_depth: 'basic',
143
+ }),
144
+ });
145
+ const data = await res.json() as {results?: Array<{title: string; content: string; url: string}>};
146
+ const results = (data.results || []).map(
147
+ (r: {title: string; content: string; url: string}) => `${r.title}: ${r.content}`
148
+ ).join('\n\n');
149
+ log.info({results}, 'Tavily results');
150
+ session.sendToolOutput(evt.tool_call_id as string, results || 'No results found.');
151
+ } catch (err) {
152
+ log.error({err}, 'Tavily search failed');
153
+ session.sendToolOutput(evt.tool_call_id as string, 'Web search failed. Please try again.');
154
+ }
155
+ }
156
+ });
157
+
158
+ session.on('/agent-complete', (evt: Record<string, unknown>) => {
159
+ log.info({payload: evt}, 'agent completed');
160
+ session.hangup().reply();
161
+ });
162
+
163
+ session
164
+ .agent({
165
+ stt: opts.stt,
166
+ tts: {
167
+ vendor: opts.tts.vendor,
168
+ voice,
169
+ ...opts.tts.options && { options: opts.tts.options },
170
+ },
171
+ llm: {
172
+ vendor: llmVendor,
173
+ model,
174
+ llmOptions: {
175
+ messages: [
176
+ { role: 'system', content: systemPrompt },
177
+ ],
178
+ },
179
+ },
180
+ turnDetection: opts.turnDetection,
181
+ ...opts.noiseIsolation && { noiseIsolation: opts.noiseIsolation },
182
+ earlyGeneration: true,
183
+ bargeIn: {
184
+ enable: true,
185
+ },
186
+ eventHook: '/agent-event',
187
+ toolHook: '/tool-call',
188
+ actionHook: '/agent-complete',
189
+ })
190
+ .send();
191
+ }
192
+
193
+ const port = parseInt(process.env.PORT || '3000', 10);
194
+ const server = http.createServer();
195
+ const makeService = createEndpoint({ server, port, envVars });
196
+
197
+ /* Deepgram nova-3 + Krisp turn detection */
198
+ const svc = makeService({ path: '/' });
199
+ svc.on('session:new', (session) => {
200
+ handleSession(session, {
201
+ stt: {
202
+ vendor: 'deepgram',
203
+ language: 'multi',
204
+ deepgramOptions: { model: 'nova-3-general' },
205
+ },
206
+ tts: { vendor: 'cartesia', voiceEnvVar: 'CARTESIA_VOICE' },
207
+ turnDetection: 'krisp',
208
+ });
209
+ });
210
+
211
+ /* Deepgram Flux + native turn detection */
212
+ const fluxSvc = makeService({ path: '/flux' });
213
+ fluxSvc.on('session:new', (session) => {
214
+ handleSession(session, {
215
+ stt: { vendor: 'deepgramflux' },
216
+ tts: { vendor: 'cartesia', voiceEnvVar: 'CARTESIA_VOICE' },
217
+ turnDetection: 'stt',
218
+ });
219
+ });
220
+
221
+ /* AssemblyAI u3-rt-pro + native turn detection */
222
+ const aaiSvc = makeService({ path: '/aai' });
223
+ aaiSvc.on('session:new', (session) => {
224
+ handleSession(session, {
225
+ stt: {
226
+ vendor: 'assemblyai',
227
+ assemblyAiOptions: {
228
+ languageDetection: true,
229
+ },
230
+ },
231
+ tts: { vendor: 'cartesia', voiceEnvVar: 'CARTESIA_VOICE' },
232
+ turnDetection: 'stt',
233
+ });
234
+ });
235
+
236
+ /* Deepgram nova-3 + Krisp turn detection + ElevenLabs TTS */
237
+ const elSvc = makeService({ path: '/elevenlabs' });
238
+ elSvc.on('session:new', (session) => {
239
+ handleSession(session, {
240
+ stt: {
241
+ vendor: 'deepgram',
242
+ language: 'multi',
243
+ deepgramOptions: { model: 'nova-3-general' },
244
+ },
245
+ tts: {
246
+ vendor: 'elevenlabs',
247
+ voiceEnvVar: 'ELEVENLABS_VOICE',
248
+ options: { model_id: 'eleven_flash_v2_5' },
249
+ },
250
+ turnDetection: 'krisp',
251
+ });
252
+ });
253
+
254
+ /* Speechmatics preview + native turn detection */
255
+ const smSvc = makeService({ path: '/speechmatics' });
256
+ smSvc.on('session:new', (session) => {
257
+ handleSession(session, {
258
+ stt: {
259
+ vendor: 'speechmaticspreview',
260
+ language: 'en',
261
+ },
262
+ tts: { vendor: 'cartesia', voiceEnvVar: 'CARTESIA_VOICE' },
263
+ turnDetection: 'stt',
264
+ });
265
+ });
266
+
267
+ logger.info({ port }, 'jambonz voice agent listening');
@@ -0,0 +1,24 @@
1
+ # Call Recording
2
+
3
+ Demonstrates mid-call recording control. An incoming call is bridged to an agent, and recording can be started/stopped dynamically during the call.
4
+
5
+ ## What It Demonstrates
6
+ - The `dial` verb with `anchorMedia: true` (required for mid-call recording)
7
+ - **Webhook version**: Using the REST API client (`JambonzClient`) to whisper and mute active calls from an external endpoint
8
+ - **WebSocket version**: Using `injectRecord()` to start/stop SIPREC recording mid-call
9
+ - Asynchronous call control (modifying a call that's already in progress)
10
+
11
+ ## Prerequisites
12
+ - A jambonz account with API credentials (for the webhook version's REST client)
13
+ - A SIPREC server URL (for the WebSocket version's recording destination)
14
+
15
+ ## Environment Variables
16
+ - `JAMBONZ_BASE_URL` — jambonz API URL (default: `https://api.jambonz.us`)
17
+ - `JAMBONZ_ACCOUNT_SID` — your account SID
18
+ - `JAMBONZ_API_KEY` — your API key
19
+ - `AGENT_NUMBER` — phone number to bridge the caller to
20
+ - `SIPREC_URL` — SIP URI of the recording server (WebSocket version)
21
+
22
+ ## Files
23
+ - `app.ts` — Webhook version with REST API control (TypeScript using `@jambonz/sdk`)
24
+ - `ws-app.ts` — WebSocket version with inject commands (TypeScript using `@jambonz/sdk`)
@@ -0,0 +1,70 @@
1
+ import express from 'express';
2
+ import { WebhookResponse } from '@jambonz/sdk/webhook';
3
+ import { JambonzClient } from '@jambonz/sdk/client';
4
+
5
+ const app = express();
6
+ app.use(express.json());
7
+
8
+ // REST client for mid-call control
9
+ const client = new JambonzClient({
10
+ baseUrl: process.env.JAMBONZ_BASE_URL || 'https://api.jambonz.us',
11
+ accountSid: process.env.JAMBONZ_ACCOUNT_SID || '',
12
+ apiKey: process.env.JAMBONZ_API_KEY || '',
13
+ });
14
+
15
+ // Incoming call — bridge to an agent with media anchored for recording
16
+ app.post('/incoming', (_req, res) => {
17
+ const jambonz = new WebhookResponse();
18
+ jambonz
19
+ .say({ text: 'This call may be recorded for quality assurance.' })
20
+ .dial({
21
+ target: [{ type: 'phone', number: process.env.AGENT_NUMBER || '+15085551212' }],
22
+ answerOnBridge: true,
23
+ anchorMedia: true, // required for mid-call recording
24
+ timeout: 30,
25
+ actionHook: '/call-ended',
26
+ })
27
+ .say({ text: 'The agent is unavailable. Goodbye.' })
28
+ .hangup();
29
+
30
+ res.json(jambonz);
31
+ });
32
+
33
+ // Called when the bridged call ends
34
+ app.post('/call-ended', (req, res) => {
35
+ console.log(`Call ended: ${JSON.stringify(req.body)}`);
36
+ const jambonz = new WebhookResponse();
37
+ jambonz.hangup();
38
+ res.json(jambonz);
39
+ });
40
+
41
+ // External API endpoint — start recording on an active call
42
+ // Call this from your admin UI or monitoring system
43
+ app.post('/api/start-recording', async (req, res) => {
44
+ const { callSid } = req.body;
45
+ try {
46
+ // Whisper a notification to the caller
47
+ await client.calls.whisper(callSid, {
48
+ verb: 'say',
49
+ text: 'Recording has started.',
50
+ });
51
+ res.json({ status: 'recording started' });
52
+ } catch (err) {
53
+ console.error('Failed to start recording:', err);
54
+ res.status(500).json({ error: (err as Error).message });
55
+ }
56
+ });
57
+
58
+ // External API endpoint — mute/unmute an active call
59
+ app.post('/api/mute', async (req, res) => {
60
+ const { callSid, status } = req.body;
61
+ try {
62
+ await client.calls.mute(callSid, status);
63
+ res.json({ status: `call ${status}d` });
64
+ } catch (err) {
65
+ console.error('Failed to mute:', err);
66
+ res.status(500).json({ error: (err as Error).message });
67
+ }
68
+ });
69
+
70
+ app.listen(3000, () => console.log('Recording app listening on port 3000'));
@@ -0,0 +1,47 @@
1
+ import http from 'http';
2
+ import { createEndpoint } from '@jambonz/sdk/websocket';
3
+
4
+ const server = http.createServer();
5
+ const makeService = createEndpoint({ server, port: 3000 });
6
+
7
+ const svc = makeService({ path: '/' });
8
+
9
+ svc.on('session:new', (session) => {
10
+ console.log(`Call ${session.callSid} connected`);
11
+
12
+ // Bridge to an agent with media anchored for recording
13
+ session
14
+ .say({ text: 'This call may be recorded for quality assurance.' })
15
+ .dial({
16
+ target: [{ type: 'phone', number: process.env.AGENT_NUMBER || '+15085551212' }],
17
+ answerOnBridge: true,
18
+ anchorMedia: true,
19
+ timeout: 30,
20
+ })
21
+ .send();
22
+
23
+ // Start recording after a short delay (e.g., triggered by external event)
24
+ // In practice, you'd trigger this from a supervisor UI or event handler
25
+ setTimeout(() => {
26
+ console.log(`Starting recording on ${session.callSid}`);
27
+ session.injectRecord('startCallRecording', {
28
+ siprecServerURL: process.env.SIPREC_URL || 'sip:recorder@example.com',
29
+ });
30
+ }, 5000);
31
+
32
+ // Stop recording after 60 seconds (example — in practice, triggered externally)
33
+ setTimeout(() => {
34
+ console.log(`Stopping recording on ${session.callSid}`);
35
+ session.injectRecord('stopCallRecording');
36
+ }, 65000);
37
+
38
+ session.on('verb:status', (data) => {
39
+ console.log(`Verb status: ${JSON.stringify(data)}`);
40
+ });
41
+
42
+ session.on('call:status', (data) => {
43
+ console.log(`Call status: ${JSON.stringify(data)}`);
44
+ });
45
+ });
46
+
47
+ console.log('Recording ws app listening on port 3000');
@@ -0,0 +1,118 @@
1
+ import http from 'http';
2
+ import { createEndpoint } from '@jambonz/sdk/websocket';
3
+
4
+ const server = http.createServer();
5
+ const makeService = createEndpoint({
6
+ server,
7
+ port: 3000,
8
+ envVars: {
9
+ DEEPGRAM_API_KEY: {
10
+ type: 'string',
11
+ description: 'Deepgram API key for Voice Agent',
12
+ required: true,
13
+ obscure: true,
14
+ },
15
+ },
16
+ });
17
+
18
+ const svc = makeService({ path: '/deepgram-s2s' });
19
+
20
+ svc.on('session:new', (session) => {
21
+ console.log(`Incoming call: ${session.callSid}`);
22
+ console.log(' from: %s to: %s', session.data.from, session.data.to);
23
+ console.log(' env_vars:', JSON.stringify(session.data.env_vars || {}));
24
+
25
+ const apiKey = session.data.env_vars?.DEEPGRAM_API_KEY;
26
+ if (!apiKey) {
27
+ console.error('ERROR: DEEPGRAM_API_KEY not found in application environment variables');
28
+ return;
29
+ }
30
+
31
+ session
32
+ .on('/event', (_evt: Record<string, any>) => {
33
+ // Deepgram voice agent events
34
+ })
35
+ .on('/toolCall', async (evt: Record<string, any>) => {
36
+ const { name, args, tool_call_id } = evt;
37
+
38
+ if (name === 'get_weather') {
39
+ try {
40
+ const geoRes = await fetch(
41
+ `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(args.location)}&count=1&language=en&format=json`
42
+ );
43
+ const geoData = await geoRes.json() as any;
44
+ if (!geoData.results?.length) throw new Error('location_not_found');
45
+
46
+ const { latitude: lat, longitude: lng } = geoData.results[0];
47
+ const wxRes = await fetch(
48
+ `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}&current=temperature_2m,wind_speed_10m&temperature_unit=${args.scale}`
49
+ );
50
+ const weather = await wxRes.json();
51
+
52
+ session.sendToolOutput(tool_call_id, {
53
+ type: 'FunctionCallResponse',
54
+ id: tool_call_id,
55
+ name,
56
+ content: JSON.stringify(weather),
57
+ });
58
+ } catch (err) {
59
+ session.sendToolOutput(tool_call_id, { error: String(err) });
60
+ }
61
+ }
62
+ })
63
+ .on('/final', (evt: Record<string, any>) => {
64
+ if (['server failure', 'server error'].includes(evt.completion_reason)) {
65
+ session.say({ text: 'Sorry, there was an error processing your request.' }).hangup();
66
+ }
67
+ session.reply();
68
+ })
69
+ .on('close', (code: number) => {
70
+ console.log(`Session ${session.callSid} closed: ${code}`);
71
+ })
72
+ .on('error', (err: Error) => {
73
+ console.error(`Session ${session.callSid} error:`, err);
74
+ });
75
+
76
+ session
77
+ .answer()
78
+ .pause({ length: 1 })
79
+ .say({ text: 'Hello, how can I help you today?' })
80
+ .deepgram_s2s({
81
+ model: 'voice-agent',
82
+ auth: { apiKey },
83
+ actionHook: '/final',
84
+ eventHook: '/event',
85
+ toolHook: '/toolCall',
86
+ events: ['all'],
87
+ llmOptions: {
88
+ Settings: {
89
+ type: 'Settings',
90
+ agent: {
91
+ listen: { provider: { type: 'deepgram', model: 'nova-3' } },
92
+ think: {
93
+ provider: { type: 'open_ai', model: 'gpt-4o-mini' },
94
+ prompt: 'Please help the user with their request.',
95
+ functions: [
96
+ {
97
+ name: 'get_weather',
98
+ description: 'Get the weather at a given location',
99
+ parameters: {
100
+ type: 'object',
101
+ properties: {
102
+ location: { type: 'string', description: 'Location to get the weather from' },
103
+ scale: { type: 'string', enum: ['fahrenheit', 'celsius'] },
104
+ },
105
+ required: ['location', 'scale'],
106
+ },
107
+ },
108
+ ],
109
+ },
110
+ },
111
+ },
112
+ },
113
+ })
114
+ .hangup()
115
+ .send();
116
+ });
117
+
118
+ console.log('Deepgram voice agent listening on port 3000');
@@ -0,0 +1,22 @@
1
+ import express from 'express';
2
+ import { WebhookResponse } from '@jambonz/sdk/webhook';
3
+
4
+ const app = express();
5
+ app.use(express.json());
6
+
7
+ app.post('/dial', (_req, res) => {
8
+ const jambonz = new WebhookResponse();
9
+ jambonz.dial({
10
+ answerOnBridge: true,
11
+ target: [
12
+ {
13
+ type: 'phone',
14
+ number: process.env.OUTDIAL_NUMBER || '13034997111',
15
+ },
16
+ ],
17
+ });
18
+
19
+ res.json(jambonz);
20
+ });
21
+
22
+ app.listen(3000, () => console.log('Dial webhook app listening on port 3000'));