Repository navigation
Expand file tree
/
Copy pathtest.mjs
More file actions
85 lines (67 loc) · 1.8 KB
/
Copy pathtest.mjs
File metadata and controls
85 lines (67 loc) · 1.8 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
81
82
83
84
85
import { BreakoutRoom } from "breakout-room";
import OpenAI from "openai";
const openai = new OpenAI();
const invite = process.argv[2];
if (!invite) {
console.error("Please provide an invite code to join the room.");
process.exit(1);
}
let messageCount = 0;
let isGenerating = false;
let peerKey = "";
async function playGame() {
const room = new BreakoutRoom({ invite });
await room.ready();
const transcript = [];
room.on("peerEntered", (key) => {
peerKey = key;
console.log("peer entered the room", key);
});
room.on("peerLeft", (key) => {
console.log("peer left the room", key);
shutdown();
});
room.on("message", async (message) => {
if (isGenerating) {
console.log("Ignoring message while generating response...");
return;
}
if (message.who !== peerKey) {
console.log("Ignoring message not from peer...");
return;
}
transcript.push({
role: "user",
content: message.data,
});
console.log("Generating question or guess...");
isGenerating = true;
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: transcript,
});
isGenerating = false;
await room.message(response.choices[0].message.content);
transcript.push({
role: "assistant",
content: response.choices[0].message.content,
});
messageCount++;
if (messageCount >= 30) {
console.log("You have run out of questions. Exiting...");
await shutdown();
}
});
async function exitRoom() {
await room.exit();
}
async function shutdown() {
const transcript = await room.getTranscript();
console.log("transcript:", transcript);
exitRoom();
process.exit(0);
}
process.on("SIGINT", exitRoom);
process.on("SIGTERM", exitRoom);
}
playGame();