-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorti-setup.js
More file actions
80 lines (70 loc) · 2.47 KB
/
corti-setup.js
File metadata and controls
80 lines (70 loc) · 2.47 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
import { CortiClient } from '@corti/sdk';
// Load configuration from environment variables
const ENVIRONMENT_ID = process.env.ENVIRONMENT_ID;
const TENANT_NAME = process.env.TENANT_NAME;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const cortiClient = new CortiClient({
environment: ENVIRONMENT_ID,
tenantName: TENANT_NAME,
auth: {
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
},
});
// Factory function to create/reuse Corti transcribe socket
const socketFactory = () => {
let socket;
const getSocket = async () => {
if (socket) {
return socket;
}
socket = cortiClient.transcribe.connect();
return socket;
};
getSocket.reset = () => {
socket = null;
};
return getSocket;
};
// Alternative approach: If you only need WebSocket proxying and don't use other Corti API calls,
// you can use CortiAuth with getToken() method and create WebSockets natively. This is a lighter
// solution that avoids initializing the full CortiClient.
//
// Note: When using native WebSocket, you must use native event handlers (socket.on('message', ...),
// socket.on('error', ...), etc.) instead of the SDK's event handlers.
//
// import { CortiAuth, CortiEnvironment } from '@corti/sdk';
// import WebSocket from 'ws';
//
// const auth = new CortiAuth({
// environment: ENVIRONMENT_ID,
// tenantName: TENANT_NAME,
// auth: { clientId: CLIENT_ID, clientSecret: CLIENT_SECRET }
// });
//
// const socketFactory = () => {
// let socket;
//
// return async () => {
// if (socket && socket.readyState === WebSocket.OPEN) {
// return socket;
// }
//
// // Get scoped token for transcribe
// const tokenResponse = await auth.getToken({ scopes: ['transcribe'] });
//
// // Get WebSocket URL for the environment
// // Option 1: Use CortiEnvironment enum (for 'eu' or 'us')
// const env = ENVIRONMENT_ID === 'us' ? CortiEnvironment.Us : CortiEnvironment.Eu;
// // Option 2: Construct manually for other environments
// // const wssBase = `wss://api.${ENVIRONMENT_ID}.corti.app/audio-bridge/v2`;
// const wsUrl = `${env.wss}/transcribe?tenant-name=${TENANT_NAME}&token=${tokenResponse.data.accessToken}`;
//
// // Create native WebSocket connection
// socket = new WebSocket(wsUrl);
//
// return socket;
// };
// };
export { cortiClient, socketFactory };