@jambonz/sdk 0.3.0 → 0.3.2
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.
- package/AGENTS.md +721 -0
- package/examples/agent/ws-app.ts +138 -0
- package/examples/assemblyai-autogenerate-prompt/app.js +92 -0
- package/examples/assemblyai-autogenerate-prompt/package.json +14 -0
- package/examples/bedrock-agent.ts +267 -0
- package/examples/call-recording/README.md +24 -0
- package/examples/call-recording/app.ts +70 -0
- package/examples/call-recording/ws-app.ts +47 -0
- package/examples/deepgram-voice-agent/ws-app.ts +118 -0
- package/examples/dial/app.ts +22 -0
- package/examples/echo/app.ts +65 -0
- package/examples/echo/ws-app.ts +67 -0
- package/examples/elevenlabs-voice-agent/ws-app.ts +81 -0
- package/examples/hello-world/README.md +15 -0
- package/examples/hello-world/app.js +20 -0
- package/examples/hello-world/app.ts +19 -0
- package/examples/hello-world/ws-app.ts +18 -0
- package/examples/ivr-menu/README.md +15 -0
- package/examples/ivr-menu/app.js +85 -0
- package/examples/ivr-menu/app.ts +70 -0
- package/examples/listen-record/app.ts +22 -0
- package/examples/llm-streaming/ws-app.ts +96 -0
- package/examples/openai-realtime/ws-app.ts +131 -0
- package/examples/queue-with-hold/README.md +19 -0
- package/examples/queue-with-hold/app.ts +68 -0
- package/examples/queue-with-hold/ws-app.ts +41 -0
- package/examples/realtime-translator/src/app.ts +21 -0
- package/examples/realtime-translator/src/routes/translator.ts +108 -0
- package/examples/realtime-translator/src/utils/config.ts +21 -0
- package/examples/realtime-translator/src/utils/translate.ts +23 -0
- package/examples/retell-hosted/src/app.ts +22 -0
- package/examples/retell-hosted/src/config.ts +53 -0
- package/examples/retell-hosted/src/retell.ts +166 -0
- package/examples/two-stage-dialer/app.ts +96 -0
- package/examples/ultravox-voice-agent/ws-app.ts +116 -0
- package/examples/voice-agent/README.md +19 -0
- package/examples/voice-agent/app.js +133 -0
- package/examples/voice-agent/app.ts +132 -0
- package/examples/voice-agent/ws-app.ts +137 -0
- package/package.json +9 -4
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
OPENAI_API_KEY: {
|
|
10
|
+
type: 'string',
|
|
11
|
+
description: 'OpenAI API key for LLM',
|
|
12
|
+
required: true,
|
|
13
|
+
obscure: true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const svc = makeService({ path: '/agent' });
|
|
19
|
+
|
|
20
|
+
const systemPrompt = `You are a helpful weather assistant.
|
|
21
|
+
You can look up current weather for any location using the get_weather tool.
|
|
22
|
+
When asked about weather, always use the tool to fetch real data — do not guess or make up temperatures.
|
|
23
|
+
Give concise, friendly responses in plain text without markdown, asterisks, or bullets.`;
|
|
24
|
+
|
|
25
|
+
const weatherTool = {
|
|
26
|
+
name: 'get_weather',
|
|
27
|
+
description: 'Get the current temperature and wind speed for a given location.',
|
|
28
|
+
parameters: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
location: {
|
|
32
|
+
type: 'string',
|
|
33
|
+
description: 'City name or location, e.g. "San Francisco" or "Paris"',
|
|
34
|
+
},
|
|
35
|
+
scale: {
|
|
36
|
+
type: 'string',
|
|
37
|
+
enum: ['celsius', 'fahrenheit'],
|
|
38
|
+
description: 'Temperature unit, defaults to celsius',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
required: ['location'],
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
svc.on('session:new', (session) => {
|
|
46
|
+
console.log(`Incoming call: ${session.callSid}`);
|
|
47
|
+
console.log(' from: %s to: %s', session.data.from, session.data.to);
|
|
48
|
+
console.log(' env_vars:', JSON.stringify(session.data.env_vars || {}));
|
|
49
|
+
|
|
50
|
+
const apiKey = session.data.env_vars?.OPENAI_API_KEY;
|
|
51
|
+
if (!apiKey) {
|
|
52
|
+
console.error('ERROR: OPENAI_API_KEY not found in application environment variables');
|
|
53
|
+
console.error(' Configure it in the jambonz portal under your application settings');
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
session
|
|
58
|
+
.on('/event', (evt: Record<string, any>) => {
|
|
59
|
+
console.log('agent event:', evt.type);
|
|
60
|
+
})
|
|
61
|
+
.on('/toolCall', async (evt: Record<string, any>) => {
|
|
62
|
+
const { tool_call_id, name, arguments: args } = evt;
|
|
63
|
+
console.log(`tool call: ${name}`, JSON.stringify(args));
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
if (name === 'get_weather') {
|
|
67
|
+
const { location, scale = 'celsius' } = args;
|
|
68
|
+
|
|
69
|
+
const geoRes = await fetch(
|
|
70
|
+
`https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(location)}&count=1&language=en&format=json`
|
|
71
|
+
);
|
|
72
|
+
const geoData = await geoRes.json() as any;
|
|
73
|
+
if (!geoData.results?.length) throw new Error('location_not_found');
|
|
74
|
+
|
|
75
|
+
const { latitude: lat, longitude: lng } = geoData.results[0];
|
|
76
|
+
const wxRes = await fetch(
|
|
77
|
+
`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}¤t=temperature_2m,wind_speed_10m&temperature_unit=${scale}`
|
|
78
|
+
);
|
|
79
|
+
const weather = await wxRes.json() as any;
|
|
80
|
+
const { temperature_2m, wind_speed_10m } = weather.current;
|
|
81
|
+
const unit = scale === 'fahrenheit' ? '°F' : '°C';
|
|
82
|
+
|
|
83
|
+
session.sendToolOutput(tool_call_id,
|
|
84
|
+
`The current temperature in ${location} is ${temperature_2m}${unit} with wind speed ${wind_speed_10m} km/h.`
|
|
85
|
+
);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
session.sendToolOutput(tool_call_id, `Unknown tool: ${name}`);
|
|
89
|
+
} catch (err: any) {
|
|
90
|
+
if (err.message === 'location_not_found') {
|
|
91
|
+
session.sendToolOutput(tool_call_id, `Sorry, I could not find weather data for "${args?.location}".`);
|
|
92
|
+
} else {
|
|
93
|
+
session.sendToolOutput(tool_call_id, `Error fetching weather: ${err.message}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
.on('/action', (evt: Record<string, any>) => {
|
|
98
|
+
console.log('agent ended:', evt.completion_reason);
|
|
99
|
+
session.reply();
|
|
100
|
+
})
|
|
101
|
+
.on('close', (code: number) => {
|
|
102
|
+
console.log(`Session ${session.callSid} closed: ${code}`);
|
|
103
|
+
})
|
|
104
|
+
.on('error', (err: Error) => {
|
|
105
|
+
console.error(`Session ${session.callSid} error:`, err);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
session
|
|
109
|
+
.agent({
|
|
110
|
+
stt: {
|
|
111
|
+
vendor: 'deepgram',
|
|
112
|
+
language: 'en-US',
|
|
113
|
+
},
|
|
114
|
+
llm: {
|
|
115
|
+
vendor: 'openai',
|
|
116
|
+
model: 'gpt-4o',
|
|
117
|
+
auth: { apiKey },
|
|
118
|
+
llmOptions: {
|
|
119
|
+
maxTokens: 1024,
|
|
120
|
+
systemPrompt,
|
|
121
|
+
tools: [weatherTool],
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
tts: {
|
|
125
|
+
vendor: 'deepgram',
|
|
126
|
+
voice: 'aura-2-andromeda-en',
|
|
127
|
+
},
|
|
128
|
+
vad: {
|
|
129
|
+
vendor: 'silero',
|
|
130
|
+
},
|
|
131
|
+
toolHook: '/toolCall',
|
|
132
|
+
eventHook: '/event',
|
|
133
|
+
actionHook: '/action',
|
|
134
|
+
})
|
|
135
|
+
.send();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
console.log('Agent listening on port 3000');
|
|
@@ -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');
|