This repository was archived by the owner on Oct 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcli.ts
More file actions
76 lines (66 loc) · 1.59 KB
/
cli.ts
File metadata and controls
76 lines (66 loc) · 1.59 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
import { createClient } from "@rivetkit/worker/client";
import type { Registry } from "../workers/registry";
import prompts from "prompts";
async function main() {
const { username, room } = await initPrompt();
// Create type-aware client
const client = createClient<Registry>("http://localhost:6420");
// connect to chat room - now accessed via property
// can still pass parameters like room
const chatRoom = client.chatRoom
.getOrCreate(room, {
params: { room },
})
.connect();
// fetch history
const history = await chatRoom.getHistory();
console.log(
`History:\n${history.map((m) => `[${m.username}] ${m.message}`).join("\n")}`,
);
// listen for new messages
//
// `needsNewLine` is a hack to work around console.log clobbering prompts
let needsNewLine = false;
chatRoom.on("newMessage", (username: string, message: string) => {
if (needsNewLine) {
needsNewLine = false;
console.log();
}
console.log(`[${username}] ${message}`);
});
// loop to send messages
while (true) {
needsNewLine = true;
const message = await textPrompt("Message");
if (!message) break;
needsNewLine = false;
await chatRoom.sendMessage(username, message);
}
await chatRoom.dispose();
}
async function initPrompt(): Promise<{
room: string;
username: string;
}> {
return await prompts([
{
type: "text",
name: "username",
message: "Username",
},
{
type: "text",
name: "room",
message: "Room",
},
]);
}
async function textPrompt(message: string): Promise<string> {
const { x } = await prompts({
type: "text",
name: "x",
message,
});
return x;
}
main();