-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbackground.js
98 lines (93 loc) · 3.35 KB
/
background.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
console.log("Background!!");
let chats = {}; // { meetId: [ { ticId, role, content } ] }
let chatGptResponses = {}; // { meetId: { generatedResponse, startChat, endChat } }
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
// if request is of type CURRENT_CHAT then concat on the ticId
if (request.type === "CURRENT_CHAT") {
console.log("CURRENT_CHAT", request.data.chat)
const meetId = request.data.meetId;
const passedChat = request.data.chat;
if (!chats[meetId]) {
chats[meetId] = [];
}
const chat = chats[meetId];
passedChat.forEach((passedChatInstance) => {
const chatIndex = chat.findIndex(
(chatInstance) => chatInstance.ticId === passedChatInstance.ticId
);
if (chatIndex > -1) {
chat[chatIndex].content = passedChatInstance.content;
} else {
chat.push(passedChatInstance);
}
});
chrome.runtime.sendMessage({ type: "FULL_CURRENT_CHATS", data: { chats } });
}
if (request.type === "GET_RESPONSE") {
const { meetId, startChat, endChat } = request.data;
// get the slice of chat from startChat to endChat and make a deep copy of it
const chat = chats[meetId];
const chatSlice = JSON.parse(
JSON.stringify(chat.slice(startChat, endChat + 1))
);
// preprocess the chatSlice so that any recurring roles are merged into one
const preprocessedChatSlice = [];
let i = 0;
let currentChatInstance = null;
while (i < chatSlice.length) {
currentChatInstance = chatSlice[i];
const nextChatInstance = chatSlice[i + 1];
if (
nextChatInstance &&
currentChatInstance.role === nextChatInstance.role
) {
currentChatInstance.content += " " + nextChatInstance.content;
chatSlice.splice(i + 1, 1);
} else {
delete currentChatInstance.ticId;
preprocessedChatSlice.push(currentChatInstance);
i++;
}
}
console.log("preprocessedChatSlice", preprocessedChatSlice)
// send the preprocessedChatSlice to the backend https://635f-68-234-232-23.ngrok.io/infer_response and pass the preprocessedChatSlice as the json body
const response = await fetch(
`https://tech-int-cheat-backend.herokuapp.com/infer_response`,
{
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(preprocessedChatSlice),
}
);
const responseJson = await response.json();
console.log("responseJson", responseJson)
const generatedResponse = responseJson.response;
// if response fails then send a message for FAILED_RESPONSE
if (!generatedResponse) {
chrome.runtime.sendMessage({
type: "FAILED_RESPONSE"
});
return;
} else {
// send the generatedResponse back to the popup
chatGptResponses[meetId] = { generatedResponse, startChat, endChat };
chrome.runtime.sendMessage({
type: "CHATGPT_RESPONSE",
data: chatGptResponses[meetId],
});
}
}
// if request is of type GET_RESPONSE then send the response back to the popup
if (request.type === "GET_CHATGPT_RESPONSE") {
const { meetId } = request.data;
if (chatGptResponses[meetId]) {
chrome.runtime.sendMessage({
type: "CHATGPT_RESPONSE",
data: chatGptResponses[meetId],
});
}
}
});