-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathultravox-utils.js
68 lines (53 loc) · 1.83 KB
/
ultravox-utils.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'dotenv/config';
import https from 'node:https';
// Configuration
const ULTRAVOX_API_KEY = process.env.ULTRAVOX_API_KEY;
const ULTRAVOX_API_URL = 'https://api.ultravox.ai/api';
// Create Ultravox call and get join URL
export async function createUltravoxCall(callConfig) {
const request = https.request(`${ULTRAVOX_API_URL}/calls`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': ULTRAVOX_API_KEY
}
});
return new Promise((resolve, reject) => {
let data = '';
request.on('response', (response) => {
response.on('data', chunk => data += chunk);
response.on('end', () => resolve(JSON.parse(data)));
});
request.on('error', reject);
request.write(JSON.stringify(callConfig));
request.end();
});
}
export async function getCallTranscript(callId) {
let allMessages = [];
let nextCursor = null;
try {
// Keep fetching until we have all messages
do {
const url = `${ULTRAVOX_API_URL}/calls/${callId}/messages${nextCursor ? `?cursor=${nextCursor}` : ''}`;
const response = await fetch(url, {
headers: {
'X-API-Key': ULTRAVOX_API_KEY,
'Accept': 'application/json'
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Add the current page of results to our collection
allMessages = allMessages.concat(data.results);
// Update the cursor for the next iteration
nextCursor = data.next ? new URL(data.next).searchParams.get('cursor') : null;
} while (nextCursor);
return allMessages;
} catch (error) {
console.error('Error fetching Ultravox messages:', error.message);
throw error;
}
}