-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
65 lines (57 loc) · 2.02 KB
/
Copy pathroute.ts
File metadata and controls
65 lines (57 loc) · 2.02 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
import { NextResponse } from 'next/server';
import { MossVoiceServer } from '@moss-tools/voice-server';
type ConnectionDetails = {
serverUrl: string;
roomName: string;
participantName: string;
participantToken: string;
};
// Cache the initialization promise to prevent race conditions with concurrent requests
let voiceServerPromise: Promise<MossVoiceServer> | null = null;
async function getVoiceServer(): Promise<MossVoiceServer> {
if (!voiceServerPromise) {
voiceServerPromise = MossVoiceServer.create({
projectId: process.env.MOSS_PROJECT_ID!,
projectKey: process.env.MOSS_PROJECT_KEY!,
voiceAgentId: process.env.MOSS_VOICE_AGENT_ID!,
}).then((instance) => {
console.log('✅ MossVoiceServer instance created and cached');
return instance;
});
}
return voiceServerPromise;
}
export async function POST(req: Request) {
try {
const voiceServer = await getVoiceServer();
// Parse agent configuration from request body
const body = await req.json();
const agentName: string = body?.room_config?.agents?.[0]?.agent_name;
console.log(`Received request for agent: ${agentName}`);
// Generate participant token
const participantName = 'user';
const participantIdentity = `voice_assistant_user_${Math.floor(Math.random() * 10_000)}`;
const roomName = `voice_assistant_room_${Math.floor(Math.random() * 10_000)}`;
const participantToken = await voiceServer.createParticipantToken(
{ identity: participantIdentity, name: participantName },
roomName,
agentName
);
// Return connection details
const data: ConnectionDetails = {
serverUrl: voiceServer.getServerUrl(),
roomName,
participantToken: participantToken,
participantName,
};
const headers = new Headers({
'Cache-Control': 'no-store',
});
return NextResponse.json(data, { headers });
} catch (error) {
if (error instanceof Error) {
console.error(error);
return new NextResponse(error.message, { status: 500 });
}
}
}