-
Notifications
You must be signed in to change notification settings - Fork 0
/
workertest.js
43 lines (38 loc) · 1.47 KB
/
workertest.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
import fetch from 'node-fetch';
import readline from 'readline';
const context = 'its a dummy context';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Function to send user input and context to the worker and receive the response
async function sendMessageToWorker(message) {
try {
const response = await fetch('https://worker-aged-night-839d.i-f9f.workers.dev', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message, context })
});
const data = await response.json();
return data.message; // Assuming the worker responds with JSON containing a 'message' field
} catch (error) {
console.error('Error sending message to worker:', error);
return "Failed to communicate with the worker.";
}
}
// Modified function to ask for user input and handle the worker's response
async function askForMessagePrompt() {
rl.question('Prompt (or type "exit"): ', async (msg) => {
if (msg.toLowerCase() === "exit") {
console.log("Exiting...");
rl.close();
} else {
console.log(`Sending message to the worker...`);
const response = await sendMessageToWorker(msg);
console.log('Response from worker:', response);
askForMessagePrompt(); // Ask for the next message
}
});
}
// Start the interaction
askForMessagePrompt();