-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
gptr-webhook.js
81 lines (66 loc) · 1.92 KB
/
gptr-webhook.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// gptr-webhook.js
const WebSocket = require('ws');
let socket = null;
let responseCallback = null;
async function initializeWebSocket() {
if (!socket) {
const host = 'localhost:8000';
const ws_uri = `ws://${host}/ws`;
socket = new WebSocket(ws_uri);
socket.onopen = () => {
console.log('WebSocket connection established');
};
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('WebSocket data received:', data);
if (data.content === 'dev_team_result'
&& data.output.rubber_ducker_thoughts != undefined
&& data.output.tech_lead_review != undefined) {
if (responseCallback) {
responseCallback(data.output);
responseCallback = null; // Clear callback after use
}
} else {
console.log('Received data:', data);
}
};
socket.onclose = () => {
console.log('WebSocket connection closed');
socket = null;
};
socket.onerror = (error) => {
console.error('WebSocket error:', error);
};
}
}
async function sendWebhookMessage(message) {
return new Promise((resolve, reject) => {
if (!socket || socket.readyState !== WebSocket.OPEN) {
initializeWebSocket();
}
const data = {
task: message,
report_type: 'dev_team',
report_source: 'web',
tone: 'Objective',
headers: {},
repo_name: 'elishakay/gpt-researcher'
};
const payload = "start " + JSON.stringify(data);
responseCallback = (response) => {
resolve(response); // Resolve the promise with the WebSocket response
};
if (socket.readyState === WebSocket.OPEN) {
socket.send(payload);
console.log('Message sent:', payload);
} else {
socket.onopen = () => {
socket.send(payload);
console.log('Message sent after connection:', payload);
};
}
});
}
module.exports = {
sendWebhookMessage
};