-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsideband.ts
More file actions
56 lines (48 loc) · 1.94 KB
/
Copy pathsideband.ts
File metadata and controls
56 lines (48 loc) · 1.94 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
import WebSocket from "ws";
import { prisma } from "./db";
export async function initSideband(callId: string, interviewId: string) {
const url = "wss://api.openai.com/v1/realtime?call_id=" + callId;
const ws = new WebSocket(url, {
headers: {
Authorization: "Bearer " + process.env.OPENAI_KEY,
},
});
const interview = await prisma.interview.findFirst({
where: {
id: interviewId
}
})
ws.on("open", function open() {
console.log("Connected to server.");
// Send client events over the WebSocket once connected
ws.send(
JSON.stringify({
type: "session.update",
session: {
type: "realtime",
instructions: `You are supposed to interview this user on their computer science intellect. Ask around 2-3 questions based
on their experience. Please use english only during the interview.
Here is everything about the users github, will give you a rough idea about what the user does -
## Github metadata
${interview?.githubMetadata}
`,
},
})
);
});
ws.on("message", async function incoming(message) {
const parsedMessage = JSON.parse(message.toString());
if (parsedMessage.type == "response.done") {
let contents: {type: string, transcript: string}[] = [];
parsedMessage.response.output.map(x => contents = [...contents, ...x.content]);
const assistantMessage = contents.filter(x => x.type === "output_audio").map(x => x.transcript).join(" ");
await prisma.message.create({
data: {
interviewId,
type: "Assistant",
message: assistantMessage
}
})
}
});
}