-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcopilot.js
More file actions
334 lines (273 loc) · 10.8 KB
/
copilot.js
File metadata and controls
334 lines (273 loc) · 10.8 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const baseUrl = document.querySelector('meta[name="baseurl"]').content.replace(/\/+$/, '')
const language = document.querySelector('meta[name="language"]').content
const projectId = Number(document.querySelector('meta[name="project"]').content)
function truncate(string, maxLength = 32) {
return string.length > maxLength ? string.slice(0, maxLength) + '…' : string;
}
const getLangCode = async (args) => {
return language
}
const getProjectId = async (args) => {
return projectId
}
const getProject = async (args) => {
const url = `${baseUrl}/api/v1/chatbot/projects/${projectId}/`
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
const data = await response.json()
return data
}
const toggleCopilot = async (args) => {
window.toggleChainlitCopilot()
}
const handleTransfer = async (args) => {
const questions = document.querySelectorAll('.interview-question')
const inputs = Array.from(questions).reduce((inputs, question) => {
const widgets = question.querySelectorAll('.interview-widget')
return Array.from(widgets).reduce((inputs, widget) => {
const input = widget.querySelector('input[type="text"], textarea')
return input ? [...inputs, input] : inputs
}, inputs)
}, [])
const backdrop = document.createElement('div')
backdrop.id = 'chatbot-backdrop'
backdrop.innerHTML = '<div class="fade modal-backdrop in"></div>'
document.body.appendChild(backdrop)
const buttons = document.createElement('div')
buttons.id = 'chatbot-buttons'
buttons.style.position = 'absolute'
buttons.style.top = 0
buttons.style.right = 0
buttons.style.bottom = 0
buttons.style.left = 0
buttons.style.zIndex = 1050
buttons.addEventListener('click', () => {
document.getElementById('chatbot-backdrop').remove()
document.getElementById('chatbot-buttons').remove()
})
document.body.appendChild(buttons)
inputs.forEach(input => {
const rect = input.getBoundingClientRect();
const paddingTop = input.classList.contains('input-sm') ? 4 : 6
const paddingRight = 6
const buttonWrapper = document.createElement('div')
buttonWrapper.classList.add('text-right')
buttonWrapper.style.position = 'absolute'
buttonWrapper.style.zIndex = 1050
buttonWrapper.style.top = rect.top + paddingTop + window.scrollY + 'px'
buttonWrapper.style.left = rect.left - paddingRight + window.scrollX + 'px'
buttonWrapper.style.width = rect.width + 'px'
buttonWrapper.style.height = rect.height + 'px'
buttons.appendChild(buttonWrapper);
const replaceButton = document.createElement('button');
replaceButton.textContent = gettext('Replace')
replaceButton.classList.add('btn', 'btn-danger', 'btn-xs')
replaceButton.style.pointerEvents = 'auto' // allow the button to be clicked
replaceButton.addEventListener('click', () => setInput(input, args.content, false));
buttonWrapper.appendChild(replaceButton);
const appendButton = document.createElement('button');
appendButton.textContent = gettext('Append')
appendButton.classList.add('btn', 'btn-success', 'btn-xs')
appendButton.style.marginLeft = '6px'
appendButton.style.pointerEvents = 'auto' // allow the button to be clicked
appendButton.addEventListener('click', () => setInput(input, args.content, true));
buttonWrapper.appendChild(appendButton)
})
}
const setInput = async (input, content, append) => {
const lastValue = input.value
if (append) {
input.value = input.value + ' ' + content
} else {
input.value = content
}
// the following is needed for react to pick up the change
if (input._valueTracker) {
input._valueTracker.setValue(lastValue)
}
input.dispatchEvent(new Event('input', { bubbles: true }))
// document.getElementById('chatbot-backdrop').remove()
// document.getElementById('chatbot-buttons').remove()
}
const openContactModal = async (args) => {
const url = `${baseUrl}/api/v1/projects/projects/${projectId}/contact/`
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
const contactData = await response.json()
const contactModal = document.getElementById('chatbot-contact-modal')
const chatHistory = args?.history ? '\n\n' + gettext('Chat history') + ':' + '\n\n' + (
args.history.reduce((s,m) => s + `[${m.type}] ${m.content} \n`, '')
) : ''
const subjectInput = contactModal.querySelector('#chatbot-contact-subject')
const messageInput = contactModal.querySelector('#chatbot-contact-message')
subjectInput.value = contactData.subject
messageInput.value = contactData.message + chatHistory
const submitButton = contactModal.querySelector('#chatbot-contact-submit')
$(submitButton).click(async () => {
const payload = {
subject: subjectInput.value,
message: messageInput.value
}
await fetch(url, {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': Cookies.get('csrftoken')
}
})
$(contactModal).modal('hide')
})
$(contactModal).modal('show')
}
const handlers = {
getLangCode,
getProjectId,
getProject,
toggleCopilot,
handleTransfer,
openContactModal
}
const copilotEventHandler = async (event) => {
const { name, args, callback } = event.detail;
const handler = handlers[name];
const result = handler ? await handler(args) : {};
callback(result)
}
window.copilotEventHandler = copilotEventHandler
document.addEventListener("DOMContentLoaded", () => {
const copyToClipboardTooltip = gettext('Copy to clipboard')
const insertAnswerTooltip = gettext('Insert as answer')
const contactSupportTooltip = gettext('Contact support')
const createCopyIcon = () => {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg")
svg.setAttribute("xmlns", "http://www.w3.org/2000/svg")
svg.setAttribute("width", "24")
svg.setAttribute("height", "24")
svg.setAttribute("viewBox", "0 0 24 24")
svg.setAttribute("fill", "none")
svg.setAttribute("stroke", "currentColor")
svg.setAttribute("stroke-width", "2")
svg.setAttribute("stroke-linecap", "round")
svg.setAttribute("stroke-linejoin", "round")
svg.classList.add("lucide", "lucide-copy")
const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect")
rect.setAttribute("x", "9")
rect.setAttribute("y", "9")
rect.setAttribute("width", "13")
rect.setAttribute("height", "13")
rect.setAttribute("rx", "2")
rect.setAttribute("ry", "2")
const path = document.createElementNS("http://www.w3.org/2000/svg", "path")
path.setAttribute("d", "M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1")
svg.appendChild(rect)
svg.appendChild(path)
return svg
}
const getMessageText = (message) => {
const content = message.querySelector(".message-content, .message-text, .markdown") || message
return (content.textContent || "").trim()
}
const addCopyButton = (message) => {
if (message.querySelector("button svg.lucide-copy")) {
return
}
const existingButton = message.querySelector("button")
const buttonContainer = existingButton ? existingButton.parentElement : message
const copyButton = document.createElement("button")
copyButton.type = "button"
if (existingButton) {
copyButton.className = existingButton.className
}
copyButton.title = copyToClipboardTooltip
copyButton.setAttribute("aria-label", copyToClipboardTooltip)
copyButton.appendChild(createCopyIcon())
copyButton.addEventListener("click", (event) => {
event.stopPropagation()
const text = getMessageText(message)
if (!text) {
return
}
if (navigator.clipboard?.writeText) {
navigator.clipboard.writeText(text)
} else {
const textarea = document.createElement("textarea")
textarea.value = text
textarea.setAttribute("readonly", "")
textarea.style.position = "absolute"
textarea.style.left = "-9999px"
document.body.appendChild(textarea)
textarea.select()
document.execCommand("copy")
document.body.removeChild(textarea)
}
})
buttonContainer.appendChild(copyButton)
}
const observer = new MutationObserver((mutations, obs) => {
const copilot = document.getElementById("chainlit-copilot")
const shadow = copilot.shadowRoot
const modal = shadow.getElementById("new-chat-dialog")
const confirmButton = shadow.getElementById("confirm")
const copilotButton = shadow.getElementById("chainlit-copilot-button")
if (modal && confirmButton && !confirmButton.dataset.hasHandler) {
const handler = async (event) => {
event.stopPropagation()
window.sendChainlitMessage({
type: "system_message",
output: "",
metadata: {
"action": "reset_history",
"project": parseInt(projectId)
}
})
// remove this listener so we don’t fire again
confirmButton.removeEventListener("click", handler)
// trigger the original click (React handles it)
setTimeout(() => confirmButton.click(), 500)
// mark handler as attached to avoid duplicates
confirmButton.dataset.hasHandler = "true"
}
// attach the listener
confirmButton.addEventListener("click", handler)
}
// update copilot button
if (copilotButton) {
const copilotButtonDiv = copilotButton.querySelector("div")
if (!shadow.querySelector(".copilot-button-label")) {
const copilotButtonLabelTemplate = document.getElementById("copilot-button-label-template")
const copilotButtonLabel = document.createElement("span")
copilotButtonLabel.className = "copilot-button-label"
copilotButtonLabel.innerHTML = copilotButtonLabelTemplate.innerHTML
copilotButtonDiv.before(copilotButtonLabel)
copilotButton.classList.remove("w-16")
}
}
const actionButtons = shadow.querySelectorAll(".ai-message button")
actionButtons.forEach((button) => {
if (button.querySelector("svg.lucide-check") && button.title !== insertAnswerTooltip) {
button.title = insertAnswerTooltip
button.setAttribute("aria-label", insertAnswerTooltip)
}
if (button.querySelector("svg.lucide-x") && button.title !== contactSupportTooltip) {
button.title = contactSupportTooltip
button.setAttribute("aria-label", contactSupportTooltip)
}
if (button.querySelector("svg.lucide-copy") && button.title !== copyToClipboardTooltip) {
button.title = copyToClipboardTooltip
button.setAttribute("aria-label", copyToClipboardTooltip)
}
})
const aiMessages = shadow.querySelectorAll(".ai-message")
aiMessages.forEach(addCopyButton)
})
observer.observe(document.body, { childList: true, subtree: true })
});