@edryslabs/edrys-dev 0.3.3 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -14
- package/dist/chat.js +0 -27
- package/dist/index.js +30 -7
- package/dist/init.js +12 -3
- package/dist/mcp.js +17 -10
- package/dist/server.js +23 -8
- package/dist/tutor.js +131 -16
- package/package.json +2 -4
- package/views/reload.js +9 -0
- package/views/panel.js +0 -657
package/views/panel.js
DELETED
|
@@ -1,657 +0,0 @@
|
|
|
1
|
-
// The agent chat over the class tab, injected by edrys-dev into edrys-Lite's
|
|
2
|
-
// page. It lives in a shadow root on <html>: edrys-Lite mounts its app into
|
|
3
|
-
// <body> again on every route, which would wipe anything put there.
|
|
4
|
-
;(function () {
|
|
5
|
-
// only the tab opened with ?dev (DevBridge keeps that in sessionStorage);
|
|
6
|
-
// student and station tabs only load again when a local module changed
|
|
7
|
-
if (location.search !== '?dev' && !sessionStorage.getItem('edrys-dev')) {
|
|
8
|
-
if (/^\?\/(classroom|station)\//.test(location.search)) {
|
|
9
|
-
new EventSource('/edrys-dev/reload').addEventListener('reload-module', () => location.reload())
|
|
10
|
-
}
|
|
11
|
-
return
|
|
12
|
-
}
|
|
13
|
-
// the chat only with an agent to answer it (edrys-dev --mcp), the students
|
|
14
|
-
// view only with a tutor (--tutor)
|
|
15
|
-
const script = document.currentScript
|
|
16
|
-
const hasChat = !!script && script.hasAttribute('data-chat')
|
|
17
|
-
const hasTutor = !!script && script.hasAttribute('data-tutor')
|
|
18
|
-
if (!hasChat && !hasTutor) return
|
|
19
|
-
|
|
20
|
-
const OPEN_KEY = 'edrys-dev:chat-open'
|
|
21
|
-
const VIEW_KEY = 'edrys-dev:chat-view'
|
|
22
|
-
|
|
23
|
-
const css = `
|
|
24
|
-
:host { all: initial; }
|
|
25
|
-
* { box-sizing: border-box; font-family: system-ui, sans-serif; }
|
|
26
|
-
.wrap {
|
|
27
|
-
--bg: #fff; --fg: #1d1d1f; --muted: #6b6b70; --line: #d9d9de;
|
|
28
|
-
--ai: #f2f2f5; --me: #1f5fbf; --me-fg: #fff; --accent: #1f5fbf;
|
|
29
|
-
color: var(--fg);
|
|
30
|
-
}
|
|
31
|
-
@media (prefers-color-scheme: dark) {
|
|
32
|
-
.wrap {
|
|
33
|
-
--bg: #1e1e21; --fg: #ececf0; --muted: #a0a0a8; --line: #3a3a40;
|
|
34
|
-
--ai: #2a2a2f; --me: #3d7be0; --me-fg: #fff; --accent: #6ea2ff;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
.toggle {
|
|
38
|
-
position: fixed; right: 16px; bottom: 16px; z-index: 2147483000;
|
|
39
|
-
width: 48px; height: 48px; border-radius: 50%; border: 1px solid var(--line);
|
|
40
|
-
background: var(--bg); color: var(--fg); font-size: 22px; cursor: pointer;
|
|
41
|
-
box-shadow: 0 2px 8px rgba(0,0,0,.25);
|
|
42
|
-
}
|
|
43
|
-
.toggle .dot {
|
|
44
|
-
position: absolute; top: 4px; right: 4px; width: 10px; height: 10px;
|
|
45
|
-
border-radius: 50%; background: #e0452b; display: none;
|
|
46
|
-
}
|
|
47
|
-
.toggle.unread .dot { display: block; }
|
|
48
|
-
.panel {
|
|
49
|
-
position: fixed; right: 16px; bottom: 76px; z-index: 2147483000;
|
|
50
|
-
width: min(380px, calc(100vw - 32px)); height: min(520px, calc(100vh - 100px));
|
|
51
|
-
display: none; flex-direction: column; background: var(--bg);
|
|
52
|
-
border: 1px solid var(--line); border-radius: 12px; overflow: hidden;
|
|
53
|
-
box-shadow: 0 4px 20px rgba(0,0,0,.3);
|
|
54
|
-
}
|
|
55
|
-
.panel.open { display: flex; }
|
|
56
|
-
header {
|
|
57
|
-
padding: 8px 10px 8px 14px; border-bottom: 1px solid var(--line);
|
|
58
|
-
font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px;
|
|
59
|
-
}
|
|
60
|
-
header small { font-weight: 400; color: var(--muted); white-space: nowrap; }
|
|
61
|
-
.tabs { display: flex; gap: 2px; }
|
|
62
|
-
.tabs button {
|
|
63
|
-
font: inherit; font-size: 13px; padding: 3px 8px; border-radius: 6px; cursor: pointer;
|
|
64
|
-
border: 1px solid transparent; background: transparent; color: var(--muted);
|
|
65
|
-
}
|
|
66
|
-
.tabs button[aria-selected="true"] { border-color: var(--line); color: var(--fg); font-weight: 600; }
|
|
67
|
-
.tabs .count { display: inline-block; min-width: 16px; padding: 0 4px; margin-left: 2px; border-radius: 8px;
|
|
68
|
-
background: #e0452b; color: #fff; font-size: 11px; font-weight: 600; }
|
|
69
|
-
.tabs .count:empty { display: none; }
|
|
70
|
-
.spacer { flex: 1; }
|
|
71
|
-
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
|
72
|
-
.view[hidden] { display: none; }
|
|
73
|
-
.share { padding: 8px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
|
|
74
|
-
.share[hidden] { display: none; }
|
|
75
|
-
.share .row { display: flex; gap: 6px; align-items: center; }
|
|
76
|
-
.share .row[hidden] { display: none; }
|
|
77
|
-
.share .row span { font-weight: 600; white-space: nowrap; }
|
|
78
|
-
.share input {
|
|
79
|
-
flex: 1; min-width: 0; font: 12px ui-monospace, monospace; padding: 4px 6px; border-radius: 6px;
|
|
80
|
-
border: 1px solid var(--line); background: var(--bg); color: var(--fg);
|
|
81
|
-
}
|
|
82
|
-
.share button {
|
|
83
|
-
font-size: 12px; padding: 4px 10px; border-radius: 6px; cursor: pointer;
|
|
84
|
-
border: 1px solid var(--accent); background: transparent; color: var(--accent);
|
|
85
|
-
}
|
|
86
|
-
.share .note { margin-top: 2px; color: var(--muted); font-size: 12px; }
|
|
87
|
-
.share .note:empty { display: none; }
|
|
88
|
-
.switch { padding: 8px 12px; border-bottom: 1px solid var(--line); font-size: 13px; }
|
|
89
|
-
.switch label { display: flex; gap: 6px; align-items: center; font-weight: 600; }
|
|
90
|
-
.switch .note { margin-top: 2px; color: var(--muted); font-size: 12px; }
|
|
91
|
-
.threads { flex: 1; overflow-y: auto; padding: 8px 12px; display: flex; flex-direction: column; gap: 8px; }
|
|
92
|
-
.threads .empty { color: var(--muted); font-size: 13px; }
|
|
93
|
-
.thread { border: 1px solid var(--line); border-radius: 8px; }
|
|
94
|
-
.thread summary { padding: 6px 10px; cursor: pointer; font-size: 13px; font-weight: 600; }
|
|
95
|
-
.thread summary small { font-weight: 400; color: var(--muted); }
|
|
96
|
-
.thread .entries { padding: 0 8px 8px; display: flex; flex-direction: column; gap: 6px; }
|
|
97
|
-
.msg.student { background: var(--me); color: var(--me-fg); align-self: flex-end; white-space: pre-wrap; }
|
|
98
|
-
.msg.tutor { background: var(--ai); align-self: flex-start; }
|
|
99
|
-
.msg .meta { display: block; font-size: 11px; opacity: .75; margin-bottom: 2px; white-space: normal; }
|
|
100
|
-
.msg.failed { align-self: stretch; font-size: 12px; border: 1px solid #e0452b; background: rgba(224,69,43,.1); }
|
|
101
|
-
header select {
|
|
102
|
-
flex: 1; min-width: 0; font-size: 13px; padding: 3px 4px; border-radius: 6px;
|
|
103
|
-
border: 1px solid var(--line); background: var(--bg); color: var(--fg);
|
|
104
|
-
}
|
|
105
|
-
.log { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
|
|
106
|
-
.msg { max-width: 88%; padding: 8px 11px; border-radius: 10px; font-size: 14px; line-height: 1.4;
|
|
107
|
-
overflow-wrap: anywhere; }
|
|
108
|
-
.msg.ai { background: var(--ai); align-self: flex-start; }
|
|
109
|
-
.msg.user, .msg.answer { background: var(--me); color: var(--me-fg); align-self: flex-end; white-space: pre-wrap; }
|
|
110
|
-
.msg p { margin: 0 0 6px; } .msg p:last-child { margin-bottom: 0; }
|
|
111
|
-
.msg pre { overflow-x: auto; background: rgba(127,127,127,.15); padding: 6px; border-radius: 6px; }
|
|
112
|
-
.msg code { font-family: ui-monospace, monospace; font-size: 13px; }
|
|
113
|
-
.msg a { color: var(--accent); }
|
|
114
|
-
.msg form { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
|
|
115
|
-
fieldset { border: 0; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
|
|
116
|
-
legend { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
|
|
117
|
-
label.option { font-size: 14px; display: flex; gap: 6px; align-items: center; width: 100%; }
|
|
118
|
-
button.choice, button.submit {
|
|
119
|
-
font-size: 13px; padding: 5px 10px; border-radius: 8px; cursor: pointer;
|
|
120
|
-
border: 1px solid var(--accent); background: transparent; color: var(--accent);
|
|
121
|
-
}
|
|
122
|
-
.msg form[aria-disabled="true"] { opacity: .5; pointer-events: none; }
|
|
123
|
-
.problem { margin: 0 12px 8px; padding: 8px 10px; border-radius: 8px; font-size: 13px; line-height: 1.4;
|
|
124
|
-
border: 1px solid #e0452b; background: rgba(224,69,43,.1); }
|
|
125
|
-
.problem[hidden] { display: none; }
|
|
126
|
-
.problem summary { cursor: pointer; color: var(--muted); font-size: 12px; margin-top: 4px; }
|
|
127
|
-
.problem pre { margin: 4px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; font: 12px ui-monospace, monospace; }
|
|
128
|
-
.status { padding: 0 12px 6px; font-size: 12px; color: var(--muted); min-height: 1em; }
|
|
129
|
-
.hint { padding: 0 12px 6px; font-size: 12px; color: var(--muted); font-style: italic; }
|
|
130
|
-
.hint:empty { display: none; }
|
|
131
|
-
.reason { margin-top: 4px; font-size: 12px; color: var(--muted); }
|
|
132
|
-
.skipped { align-self: center; font-size: 12px; color: var(--muted); }
|
|
133
|
-
.compose { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
|
|
134
|
-
textarea {
|
|
135
|
-
flex: 1; resize: none; height: 40px; padding: 8px; font-size: 14px; border-radius: 8px;
|
|
136
|
-
border: 1px solid var(--line); background: var(--bg); color: var(--fg);
|
|
137
|
-
}
|
|
138
|
-
.compose button {
|
|
139
|
-
padding: 0 14px; border-radius: 8px; border: 0; background: var(--accent); color: #fff; cursor: pointer;
|
|
140
|
-
}
|
|
141
|
-
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
|
|
142
|
-
`
|
|
143
|
-
|
|
144
|
-
const host = document.createElement('edrys-dev-chat')
|
|
145
|
-
const root = host.attachShadow({ mode: 'open' })
|
|
146
|
-
root.innerHTML = `
|
|
147
|
-
<style>${css}</style>
|
|
148
|
-
<div class="wrap">
|
|
149
|
-
<button class="toggle" type="button" aria-expanded="false" aria-controls="panel" aria-label="edrys-dev">
|
|
150
|
-
🤖<span class="dot"></span>
|
|
151
|
-
</button>
|
|
152
|
-
<section class="panel" id="panel" aria-label="edrys-dev">
|
|
153
|
-
<header>
|
|
154
|
-
<div class="tabs" role="tablist">
|
|
155
|
-
<button type="button" role="tab" data-view="agent" aria-controls="agent">Agent</button>
|
|
156
|
-
<button type="button" role="tab" data-view="students" aria-controls="students">Students<span class="count"></span></button>
|
|
157
|
-
</div>
|
|
158
|
-
<label for="sessions" hidden>Chats</label>
|
|
159
|
-
<select id="sessions"></select>
|
|
160
|
-
<span class="spacer"></span>
|
|
161
|
-
<small title="edrys-dev: not the class's chat">only you see this</small>
|
|
162
|
-
</header>
|
|
163
|
-
<div class="share" hidden>
|
|
164
|
-
<div class="row" hidden>
|
|
165
|
-
<span>Link for students</span>
|
|
166
|
-
<input readonly aria-label="Link for your students">
|
|
167
|
-
<button type="button">Copy</button>
|
|
168
|
-
</div>
|
|
169
|
-
<div class="note" aria-live="polite"></div>
|
|
170
|
-
</div>
|
|
171
|
-
<div class="view" id="agent" role="tabpanel">
|
|
172
|
-
<div class="log" role="log" aria-live="polite"></div>
|
|
173
|
-
<div class="problem" role="alert" hidden></div>
|
|
174
|
-
<div class="status" aria-live="polite"></div>
|
|
175
|
-
<div class="hint" aria-live="polite"></div>
|
|
176
|
-
<form class="compose">
|
|
177
|
-
<label for="text" hidden>Message to the agent</label>
|
|
178
|
-
<textarea id="text" placeholder="Message to the agent (Enter sends)"></textarea>
|
|
179
|
-
<button type="submit">Send</button>
|
|
180
|
-
</form>
|
|
181
|
-
</div>
|
|
182
|
-
<div class="view" id="students" role="tabpanel" hidden>
|
|
183
|
-
<div class="switch">
|
|
184
|
-
<label><input type="checkbox" id="tutor-on"> Tutor answers students</label>
|
|
185
|
-
<div class="note" aria-live="polite"></div>
|
|
186
|
-
</div>
|
|
187
|
-
<div class="threads" role="log" aria-live="polite"></div>
|
|
188
|
-
</div>
|
|
189
|
-
</section>
|
|
190
|
-
</div>`
|
|
191
|
-
document.documentElement.appendChild(host)
|
|
192
|
-
|
|
193
|
-
const toggle = root.querySelector('.toggle')
|
|
194
|
-
const panel = root.querySelector('.panel')
|
|
195
|
-
const log = root.querySelector('.log')
|
|
196
|
-
const statusLine = root.querySelector('.status')
|
|
197
|
-
const hintLine = root.querySelector('.hint')
|
|
198
|
-
const problemBox = root.querySelector('.problem')
|
|
199
|
-
const compose = root.querySelector('.compose')
|
|
200
|
-
const textarea = root.querySelector('textarea')
|
|
201
|
-
const menu = root.querySelector('#sessions')
|
|
202
|
-
const tabs = [...root.querySelectorAll('.tabs button')]
|
|
203
|
-
const count = root.querySelector('.tabs .count')
|
|
204
|
-
const tutorBox = root.querySelector('#tutor-on')
|
|
205
|
-
const tutorNote = root.querySelector('.switch .note')
|
|
206
|
-
const threadList = root.querySelector('.threads')
|
|
207
|
-
|
|
208
|
-
// the views there are; the tabs only when there are two
|
|
209
|
-
let view = null
|
|
210
|
-
function showView(name) {
|
|
211
|
-
view = name
|
|
212
|
-
for (const tab of tabs) tab.setAttribute('aria-selected', String(tab.dataset.view === name))
|
|
213
|
-
root.querySelector('#agent').hidden = name !== 'agent'
|
|
214
|
-
root.querySelector('#students').hidden = name !== 'students'
|
|
215
|
-
menu.hidden = name !== 'agent'
|
|
216
|
-
// the menu fills the header; without it, the note goes right
|
|
217
|
-
root.querySelector('.spacer').hidden = name === 'agent'
|
|
218
|
-
if (name === 'students') count.textContent = ''
|
|
219
|
-
try {
|
|
220
|
-
sessionStorage.setItem(VIEW_KEY, name)
|
|
221
|
-
} catch {}
|
|
222
|
-
}
|
|
223
|
-
for (const tab of tabs) {
|
|
224
|
-
tab.hidden = !(hasChat && hasTutor) || (tab.dataset.view === 'agent' ? !hasChat : !hasTutor)
|
|
225
|
-
tab.addEventListener('click', () => showView(tab.dataset.view))
|
|
226
|
-
}
|
|
227
|
-
if (!(hasChat && hasTutor)) {
|
|
228
|
-
// one view: its name as the title
|
|
229
|
-
const title = root.querySelector('.tabs')
|
|
230
|
-
title.textContent = hasChat ? 'Agent' : 'Students'
|
|
231
|
-
}
|
|
232
|
-
let savedView = null
|
|
233
|
-
try {
|
|
234
|
-
savedView = sessionStorage.getItem(VIEW_KEY)
|
|
235
|
-
} catch {}
|
|
236
|
-
showView(!hasChat ? 'students' : !hasTutor ? 'agent' : savedView === 'students' ? 'students' : 'agent')
|
|
237
|
-
|
|
238
|
-
let open = null // the ask id of the form the user can still answer
|
|
239
|
-
let current = null // the chat shown, {id, title}
|
|
240
|
-
let sessions = [] // the saved chats, the one changed last first
|
|
241
|
-
|
|
242
|
-
const NEW = ''
|
|
243
|
-
// + New chat, then the chats; the current one is new until it has a file
|
|
244
|
-
function showMenu() {
|
|
245
|
-
const option = (value, text) => {
|
|
246
|
-
const o = document.createElement('option')
|
|
247
|
-
o.value = value
|
|
248
|
-
o.textContent = text
|
|
249
|
-
return o
|
|
250
|
-
}
|
|
251
|
-
const options = [option(NEW, '+ New chat')]
|
|
252
|
-
if (current && !sessions.some((s) => s.id === current.id)) options.push(option(current.id, 'New chat'))
|
|
253
|
-
for (const s of sessions) options.push(option(s.id, s.title))
|
|
254
|
-
menu.replaceChildren(...options)
|
|
255
|
-
if (current) menu.value = current.id
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
menu.addEventListener('change', async () => {
|
|
259
|
-
const response = await post({ session: menu.value === NEW ? null : menu.value })
|
|
260
|
-
// the server sends the chat it shows now; otherwise back to the one before
|
|
261
|
-
if (!response?.ok) {
|
|
262
|
-
showMenu()
|
|
263
|
-
notSent()
|
|
264
|
-
} else if (menu.value === NEW) showMenu()
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
function setOpen(isOpen) {
|
|
268
|
-
panel.classList.toggle('open', isOpen)
|
|
269
|
-
toggle.setAttribute('aria-expanded', String(isOpen))
|
|
270
|
-
if (isOpen) toggle.classList.remove('unread')
|
|
271
|
-
if (isOpen && view === 'students') count.textContent = ''
|
|
272
|
-
try {
|
|
273
|
-
sessionStorage.setItem(OPEN_KEY, isOpen ? '1' : '')
|
|
274
|
-
} catch {}
|
|
275
|
-
}
|
|
276
|
-
toggle.addEventListener('click', () => setOpen(!panel.classList.contains('open')))
|
|
277
|
-
setOpen(sessionStorage.getItem(OPEN_KEY) === '1')
|
|
278
|
-
|
|
279
|
-
// the response, or null if edrys-dev could not be reached
|
|
280
|
-
function post(body) {
|
|
281
|
-
return fetch('/edrys-dev/chat', {
|
|
282
|
-
method: 'POST',
|
|
283
|
-
headers: { 'Content-Type': 'application/json' },
|
|
284
|
-
body: JSON.stringify(body),
|
|
285
|
-
}).catch(() => null)
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// shown in the status line until the agent sets the next one
|
|
289
|
-
function notSent() {
|
|
290
|
-
statusLine.textContent = 'Not sent: is edrys-dev still running? Try again.'
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
compose.addEventListener('submit', async (event) => {
|
|
294
|
-
event.preventDefault()
|
|
295
|
-
const text = textarea.value.trim()
|
|
296
|
-
if (!text) return
|
|
297
|
-
textarea.value = ''
|
|
298
|
-
const response = await post({ text })
|
|
299
|
-
if (response?.ok) return
|
|
300
|
-
// back into the box, unless the user typed something new meanwhile
|
|
301
|
-
if (!textarea.value) textarea.value = text
|
|
302
|
-
notSent()
|
|
303
|
-
})
|
|
304
|
-
textarea.addEventListener('keydown', (event) => {
|
|
305
|
-
if (event.key === 'Enter' && !event.shiftKey) {
|
|
306
|
-
event.preventDefault()
|
|
307
|
-
compose.requestSubmit()
|
|
308
|
-
}
|
|
309
|
-
})
|
|
310
|
-
|
|
311
|
-
// the agent writes Markdown; nothing it relays may run in this page
|
|
312
|
-
function render(markdown) {
|
|
313
|
-
const html = window.DOMPurify.sanitize(window.marked.parse(markdown))
|
|
314
|
-
const div = document.createElement('div')
|
|
315
|
-
div.innerHTML = html
|
|
316
|
-
div.querySelectorAll('a').forEach((a) => {
|
|
317
|
-
a.target = '_blank'
|
|
318
|
-
a.rel = 'noopener noreferrer'
|
|
319
|
-
})
|
|
320
|
-
return div
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
function formFor(entry) {
|
|
324
|
-
const form = document.createElement('form')
|
|
325
|
-
form.dataset.ask = entry.ask
|
|
326
|
-
const values = {}
|
|
327
|
-
const single = entry.inputs.length === 1 && entry.inputs[0].type === 'buttons'
|
|
328
|
-
|
|
329
|
-
for (const input of entry.inputs) {
|
|
330
|
-
const set = document.createElement('fieldset')
|
|
331
|
-
if (input.label) {
|
|
332
|
-
const legend = document.createElement('legend')
|
|
333
|
-
legend.textContent = input.label
|
|
334
|
-
set.appendChild(legend)
|
|
335
|
-
}
|
|
336
|
-
for (const option of input.options) {
|
|
337
|
-
if (input.type === 'buttons') {
|
|
338
|
-
const button = document.createElement('button')
|
|
339
|
-
button.type = single ? 'submit' : 'button'
|
|
340
|
-
button.className = 'choice'
|
|
341
|
-
button.textContent = option
|
|
342
|
-
button.addEventListener('click', () => {
|
|
343
|
-
values[input.id] = option
|
|
344
|
-
set.querySelectorAll('button').forEach((b) => b.setAttribute('aria-pressed', String(b === button)))
|
|
345
|
-
})
|
|
346
|
-
set.appendChild(button)
|
|
347
|
-
} else {
|
|
348
|
-
const label = document.createElement('label')
|
|
349
|
-
label.className = 'option'
|
|
350
|
-
const box = document.createElement('input')
|
|
351
|
-
box.type = input.type
|
|
352
|
-
box.name = entry.ask + '-' + input.id
|
|
353
|
-
box.value = option
|
|
354
|
-
label.append(box, option)
|
|
355
|
-
set.appendChild(label)
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
form.appendChild(set)
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
if (!single) {
|
|
362
|
-
const submit = document.createElement('button')
|
|
363
|
-
submit.type = 'submit'
|
|
364
|
-
submit.className = 'submit'
|
|
365
|
-
submit.textContent = 'Send'
|
|
366
|
-
form.appendChild(submit)
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
form.addEventListener('submit', async (event) => {
|
|
370
|
-
event.preventDefault()
|
|
371
|
-
for (const input of entry.inputs) {
|
|
372
|
-
if (input.type === 'buttons') continue
|
|
373
|
-
const checked = [...form.querySelectorAll(`input[name="${entry.ask}-${input.id}"]:checked`)].map((b) => b.value)
|
|
374
|
-
values[input.id] = input.type === 'radio' ? checked[0] ?? '' : checked
|
|
375
|
-
}
|
|
376
|
-
const response = await post({ ask: entry.ask, values })
|
|
377
|
-
// closed in the meantime; otherwise the form stays, to send again
|
|
378
|
-
if (response?.status === 409) disable(entry.ask)
|
|
379
|
-
else if (!response?.ok) notSent()
|
|
380
|
-
})
|
|
381
|
-
|
|
382
|
-
if (entry.ask !== open) form.setAttribute('aria-disabled', 'true')
|
|
383
|
-
return form
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function disable(ask, reason) {
|
|
387
|
-
root.querySelectorAll(`form[data-ask="${ask}"]`).forEach((form) => {
|
|
388
|
-
form.setAttribute('aria-disabled', 'true')
|
|
389
|
-
// why the agent closed it, shown only live
|
|
390
|
-
if (reason && !form.nextElementSibling?.classList.contains('reason')) {
|
|
391
|
-
const note = document.createElement('div')
|
|
392
|
-
note.className = 'reason'
|
|
393
|
-
note.textContent = reason
|
|
394
|
-
form.after(note)
|
|
395
|
-
}
|
|
396
|
-
})
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// the lab file does not load: until it does, in plain words, edrys-Lite's message folded
|
|
400
|
-
function showProblem(problem) {
|
|
401
|
-
problemBox.hidden = !problem
|
|
402
|
-
if (!problem) return problemBox.replaceChildren()
|
|
403
|
-
const text = document.createElement('div')
|
|
404
|
-
text.textContent = problem.text
|
|
405
|
-
const details = document.createElement('details')
|
|
406
|
-
const summary = document.createElement('summary')
|
|
407
|
-
summary.textContent = 'Details'
|
|
408
|
-
const pre = document.createElement('pre')
|
|
409
|
-
pre.textContent = problem.detail
|
|
410
|
-
details.append(summary, pre)
|
|
411
|
-
problemBox.replaceChildren(text, details)
|
|
412
|
-
if (!panel.classList.contains('open')) toggle.classList.add('unread')
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
function show(entry) {
|
|
416
|
-
const div = document.createElement('div')
|
|
417
|
-
div.className = 'msg ' + entry.type
|
|
418
|
-
if (entry.type === 'ai') {
|
|
419
|
-
div.appendChild(render(entry.markdown))
|
|
420
|
-
if (entry.ask) div.appendChild(formFor(entry))
|
|
421
|
-
} else if (entry.type === 'user') {
|
|
422
|
-
div.textContent = entry.text
|
|
423
|
-
} else if (entry.type === 'answer') {
|
|
424
|
-
disable(entry.ask)
|
|
425
|
-
div.textContent = Object.values(entry.values).flat().join(', ')
|
|
426
|
-
}
|
|
427
|
-
log.appendChild(div)
|
|
428
|
-
log.scrollTop = log.scrollHeight
|
|
429
|
-
if (!panel.classList.contains('open') && entry.type === 'ai') toggle.classList.add('unread')
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
function connect() {
|
|
433
|
-
const events = new EventSource('/edrys-dev/chat/events')
|
|
434
|
-
events.onmessage = (event) => {
|
|
435
|
-
const message = JSON.parse(event.data)
|
|
436
|
-
switch (message.type) {
|
|
437
|
-
case 'history':
|
|
438
|
-
log.replaceChildren()
|
|
439
|
-
open = message.open
|
|
440
|
-
current = message.session
|
|
441
|
-
sessions = message.sessions
|
|
442
|
-
showMenu()
|
|
443
|
-
if (message.skipped) {
|
|
444
|
-
const note = document.createElement('div')
|
|
445
|
-
note.className = 'skipped'
|
|
446
|
-
note.textContent = `${message.skipped} earlier messages not shown`
|
|
447
|
-
log.appendChild(note)
|
|
448
|
-
}
|
|
449
|
-
message.entries.forEach(show)
|
|
450
|
-
statusLine.textContent = message.status
|
|
451
|
-
hintLine.textContent = message.hint || ''
|
|
452
|
-
toggle.classList.remove('unread')
|
|
453
|
-
showProblem(message.lab)
|
|
454
|
-
break
|
|
455
|
-
case 'lab':
|
|
456
|
-
showProblem(message.problem)
|
|
457
|
-
break
|
|
458
|
-
case 'entry':
|
|
459
|
-
if (message.entry.type === 'ai' && message.entry.ask) open = message.entry.ask
|
|
460
|
-
if (message.entry.type === 'answer' && open === message.entry.ask) open = null
|
|
461
|
-
show(message.entry)
|
|
462
|
-
break
|
|
463
|
-
case 'status':
|
|
464
|
-
statusLine.textContent = message.text
|
|
465
|
-
break
|
|
466
|
-
case 'sessions':
|
|
467
|
-
sessions = message.sessions
|
|
468
|
-
showMenu()
|
|
469
|
-
break
|
|
470
|
-
case 'hint':
|
|
471
|
-
hintLine.textContent = message.text
|
|
472
|
-
break
|
|
473
|
-
case 'close':
|
|
474
|
-
if (open === message.ask) open = null
|
|
475
|
-
disable(message.ask, message.reason)
|
|
476
|
-
break
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// --- the students' tutor threads ---
|
|
482
|
-
|
|
483
|
-
const threads = new Map() // thread id → {box, entries, when}
|
|
484
|
-
const time = (t) => new Date(t).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
|
|
485
|
-
|
|
486
|
-
function emptyNote() {
|
|
487
|
-
if (threads.size) return threadList.querySelector('.empty')?.remove()
|
|
488
|
-
const note = document.createElement('div')
|
|
489
|
-
note.className = 'empty'
|
|
490
|
-
note.textContent = 'No questions yet. Students ask with the 🤖 button in the class.'
|
|
491
|
-
threadList.replaceChildren(note)
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
// the thread's box, moved to the top: the one changed last first
|
|
495
|
-
function threadFor(id, name) {
|
|
496
|
-
let thread = threads.get(id)
|
|
497
|
-
if (!thread) {
|
|
498
|
-
const box = document.createElement('details')
|
|
499
|
-
box.className = 'thread'
|
|
500
|
-
box.open = true
|
|
501
|
-
const summary = document.createElement('summary')
|
|
502
|
-
const entries = document.createElement('div')
|
|
503
|
-
entries.className = 'entries'
|
|
504
|
-
box.append(summary, entries)
|
|
505
|
-
thread = { box, summary, entries }
|
|
506
|
-
threads.set(id, thread)
|
|
507
|
-
emptyNote()
|
|
508
|
-
}
|
|
509
|
-
// the name is the student's choice: text only
|
|
510
|
-
const when = document.createElement('small')
|
|
511
|
-
when.textContent = thread.last ? ' · ' + time(thread.last) : ''
|
|
512
|
-
thread.summary.replaceChildren(name, when)
|
|
513
|
-
threadList.prepend(thread.box)
|
|
514
|
-
return thread
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function showTutorEntry(thread, entry) {
|
|
518
|
-
const div = document.createElement('div')
|
|
519
|
-
const meta = document.createElement('span')
|
|
520
|
-
meta.className = 'meta'
|
|
521
|
-
meta.textContent = time(entry.t) + (entry.room ? ' · ' + entry.room : '')
|
|
522
|
-
if (entry.from === 'student') {
|
|
523
|
-
div.className = 'msg student'
|
|
524
|
-
div.append(meta, entry.text)
|
|
525
|
-
} else {
|
|
526
|
-
div.className = 'msg tutor'
|
|
527
|
-
div.append(meta, render(entry.text))
|
|
528
|
-
}
|
|
529
|
-
thread.entries.appendChild(div)
|
|
530
|
-
thread.last = entry.t
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
function addTutorEntry(id, name, entry) {
|
|
534
|
-
const thread = threadFor(id, name)
|
|
535
|
-
showTutorEntry(thread, entry)
|
|
536
|
-
threadFor(id, name)
|
|
537
|
-
threadList.scrollTop = 0
|
|
538
|
-
if (entry.from !== 'student') return
|
|
539
|
-
if (view !== 'students' || !panel.classList.contains('open')) {
|
|
540
|
-
count.textContent = String(Number(count.textContent || 0) + 1)
|
|
541
|
-
if (!panel.classList.contains('open')) toggle.classList.add('unread')
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
function showSwitch(on) {
|
|
546
|
-
tutorBox.checked = on
|
|
547
|
-
tutorNote.textContent = on
|
|
548
|
-
? 'Students see the 🤖 button while this tab is open.'
|
|
549
|
-
: 'Off: students do not see the button, and questions get "The tutor is off".'
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
tutorBox.addEventListener('change', async () => {
|
|
553
|
-
const on = tutorBox.checked
|
|
554
|
-
const response = await fetch('/edrys-dev/tutor', {
|
|
555
|
-
method: 'POST',
|
|
556
|
-
headers: { 'Content-Type': 'application/json' },
|
|
557
|
-
body: JSON.stringify({ on }),
|
|
558
|
-
}).catch(() => null)
|
|
559
|
-
if (response?.ok) return
|
|
560
|
-
showSwitch(!on)
|
|
561
|
-
tutorNote.textContent = 'Not switched: is edrys-dev still running? Try again.'
|
|
562
|
-
})
|
|
563
|
-
|
|
564
|
-
function connectTutor() {
|
|
565
|
-
const events = new EventSource('/edrys-dev/tutor/events')
|
|
566
|
-
events.onmessage = (event) => {
|
|
567
|
-
const message = JSON.parse(event.data)
|
|
568
|
-
switch (message.type) {
|
|
569
|
-
case 'threads':
|
|
570
|
-
threads.clear()
|
|
571
|
-
threadList.replaceChildren()
|
|
572
|
-
showSwitch(message.on)
|
|
573
|
-
// oldest first, so the newest ends up on top
|
|
574
|
-
for (const saved of [...message.threads].reverse()) {
|
|
575
|
-
const thread = threadFor(saved.id, saved.name)
|
|
576
|
-
if (saved.skipped) {
|
|
577
|
-
const note = document.createElement('div')
|
|
578
|
-
note.className = 'skipped'
|
|
579
|
-
note.textContent = `${saved.skipped} earlier messages not shown`
|
|
580
|
-
thread.entries.appendChild(note)
|
|
581
|
-
}
|
|
582
|
-
saved.entries.forEach((entry) => showTutorEntry(thread, entry))
|
|
583
|
-
threadFor(saved.id, saved.name)
|
|
584
|
-
}
|
|
585
|
-
emptyNote()
|
|
586
|
-
break
|
|
587
|
-
case 'entry':
|
|
588
|
-
addTutorEntry(message.id, message.name, message.entry)
|
|
589
|
-
break
|
|
590
|
-
case 'failed': {
|
|
591
|
-
// the reason is for the teacher; the student was told it failed
|
|
592
|
-
const thread = threadFor(message.id, message.name)
|
|
593
|
-
const div = document.createElement('div')
|
|
594
|
-
div.className = 'msg failed'
|
|
595
|
-
div.textContent = 'No answer: ' + message.error
|
|
596
|
-
thread.entries.appendChild(div)
|
|
597
|
-
break
|
|
598
|
-
}
|
|
599
|
-
case 'on':
|
|
600
|
-
showSwitch(message.on)
|
|
601
|
-
break
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
// --- the link for students: the class's share link would only work on this computer ---
|
|
607
|
-
const shareBox = root.querySelector('.share')
|
|
608
|
-
const shareRow = shareBox.querySelector('.row')
|
|
609
|
-
const shareInput = shareBox.querySelector('input')
|
|
610
|
-
const shareButton = shareBox.querySelector('button')
|
|
611
|
-
const shareNote = shareBox.querySelector('.note')
|
|
612
|
-
|
|
613
|
-
function showLink({ reachable, link, local_urls }) {
|
|
614
|
-
shareBox.hidden = false
|
|
615
|
-
shareRow.hidden = !link
|
|
616
|
-
shareInput.value = link || ''
|
|
617
|
-
const modules = local_urls.length
|
|
618
|
-
shareNote.textContent = !reachable
|
|
619
|
-
? 'Students on other computers cannot join: edrys-dev runs with --local, which reaches this computer only.'
|
|
620
|
-
: !link
|
|
621
|
-
? 'The link for students appears once the class is open.'
|
|
622
|
-
: modules
|
|
623
|
-
? `${modules === 1 ? 'A module loads' : `${modules} modules load`} from this computer (localhost) and will not load for students: publish ${modules === 1 ? 'it' : 'them'} first.`
|
|
624
|
-
: ''
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
shareInput.addEventListener('focus', () => shareInput.select())
|
|
628
|
-
shareButton.addEventListener('click', async () => {
|
|
629
|
-
let copied = false
|
|
630
|
-
try {
|
|
631
|
-
await navigator.clipboard.writeText(shareInput.value)
|
|
632
|
-
copied = true
|
|
633
|
-
} catch {
|
|
634
|
-
shareInput.select()
|
|
635
|
-
try {
|
|
636
|
-
copied = document.execCommand('copy')
|
|
637
|
-
} catch {}
|
|
638
|
-
}
|
|
639
|
-
shareButton.textContent = copied ? 'Copied' : 'Select and copy it'
|
|
640
|
-
setTimeout(() => (shareButton.textContent = 'Copy'), 2000)
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
new EventSource('/edrys-dev/link/events').onmessage = (event) => showLink(JSON.parse(event.data))
|
|
644
|
-
|
|
645
|
-
// the renderer first, then the messages that need it
|
|
646
|
-
let loaded = 0
|
|
647
|
-
for (const name of ['marked', 'purify']) {
|
|
648
|
-
const script = document.createElement('script')
|
|
649
|
-
script.src = `/edrys-dev/${name}.js`
|
|
650
|
-
script.onload = () => {
|
|
651
|
-
if (++loaded < 2) return
|
|
652
|
-
if (hasChat) connect()
|
|
653
|
-
if (hasTutor) connectTutor()
|
|
654
|
-
}
|
|
655
|
-
document.head.appendChild(script)
|
|
656
|
-
}
|
|
657
|
-
})()
|