forked from mihranmiroyan/edison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedstem_frontend.js
103 lines (93 loc) · 3.54 KB
/
edstem_frontend.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
99
100
101
102
103
// Utility functions
const processHistory = (promptComment) => {
const conversationHistory = [];
const addComment = (comment) => {
conversationHistory.push({
user_role: comment.user.role,
document: comment.xml,
text: comment.plaintext
});
};
addComment(promptComment.thread);
promptComment.context.slice(0, -1).forEach(comment => {
if (!comment.is_private) {
addComment(comment);
}
});
return conversationHistory;
};
const getIDFromComment = (comment, isPublic = false) => {
const comments = comment.context;
const commentCount = isPublic ? 3 : 1;
if (comments.length === commentCount) {
return `thread_${comment.thread.url.split('/').pop()}`;
} else {
const studentComment = comments[comments.length - (isPublic ? 4 : 2)];
return `comment_${studentComment.url.split('=').pop()}`;
}
};
const EDISON_ENDPOINT = "EDISON ENDPOINT";
const AUTH_TOKEN = "EDISON AUTH TOKEN";
const COURSE = "YOUR COURSE";
const sendEdisonRequest = async (url, body) => {
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Authorization": AUTH_TOKEN,
"Content-Type": "application/json",
},
body: JSON.stringify(body)
});
return response;
} catch (error) {
console.error("Error sending request to Edison:", error);
}
};
// EdBot post comment handler
edBot.postComment(async (comment, actions) => {
if (comment.plaintext.includes('prompt edison')) {
const requestBody = {
prod: "true",
log_local: "false",
log_blob: "true",
post_comment: "true",
thread_id: comment.thread.url.split('/').pop(),
comment_id: comment.url.split('=').pop(),
experiment_name: "",
course: COURSE,
thread_title: comment.thread.title,
category: comment.thread.category,
subcategory: comment.thread.subcategory,
subsubcategory: comment.thread.subsubcategory,
conversation_history: processHistory(comment),
question_id: getIDFromComment(comment)
};
await sendEdisonRequest(EDISON_ENDPOINT, requestBody);
} else if (comment.plaintext.includes('public edison')) {
const comments = comment.context;
const requestBody = {
log_blob: "true",
course: COURSE,
question_id: getIDFromComment(comment, true),
text: comments[comments.length - 2].xml,
curr_comment_id: comment.url.split('=').pop(),
parent_comment_id: comments[comments.length - 2].url.split('=').pop(),
};
await sendEdisonRequest(`${EDISON_ENDPOINT}/public`, requestBody);
}
});
// EdBot pre-comment handler
edBot.preComment((comment, actions) => {
if (comment.user.role !== 'student' && comment.plaintext.startsWith('edison')) {
actions.comment(comment.plaintext.substring(6), { markdown: true });
actions.drop();
} else if (comment.user.role !== 'student' && comment.plaintext.startsWith('publicedison')) {
if (comment.plaintext.startsWith('publicedisonanswer')) {
actions.answer(comment.plaintext.substring(18), { markdown: true, private: false });
} else if (comment.plaintext.startsWith('publicedisoncomment')) {
actions.comment(comment.plaintext.substring(19), { markdown: true, private: false });
}
actions.drop();
}
});