-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 1.63 KB
/
index.js
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
const fetch = require("cross-fetch");
const readline = require("readline");
let sessionID = null;
const startConversation = () => {
fetch("https://engine.memori.ai/memori/v2/session", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
memoriId: "1afe57c6-1b69-4a61-96ea-52bf7b8d158e",
}),
})
.then((response) => response.json())
.then((result) => {
if (!result?.sessionID) {
console.error("Received no session ID");
return;
}
sessionID = result?.sessionID;
let emission = result?.currentState?.emission;
if (emission) console.log(`Nunzio: ${emission}`);
handleConversation();
})
.catch((error) => console.log("error", error));
};
const handleConversation = () => {
const commandLineIO = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
commandLineIO.question("You: ", (question) => {
if (!sessionID) {
console.log("No session ID");
return;
}
fetch(`https://engine.memori.ai/memori/v2/TextEnteredEvent/${sessionID}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text: question,
}),
})
.then((response) => response.json())
.then((result) => {
let emission = result?.currentState?.emission;
if (emission) console.log(`Nunzio: ${emission}`);
})
.catch((error) => console.log("error", error))
.finally(() => {
commandLineIO.close();
handleConversation();
});
});
};
startConversation();