@edryslabs/edrys-dev 0.3.4 → 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 +24 -12
- package/dist/chat.js +0 -27
- package/dist/index.js +4 -3
- package/dist/init.js +3 -3
- package/dist/mcp.js +1 -1
- package/dist/server.js +12 -5
- package/dist/tutor.js +12 -0
- package/package.json +2 -4
- package/views/reload.js +9 -0
- package/views/panel.js +0 -661
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ Stop it with `Ctrl+C`.
|
|
|
52
52
|
|
|
53
53
|
- **The `/?dev` tab is yours.** It is the teacher's view of the class. Keep it open: it is the tab that picks up your changes to `lab.yml`.
|
|
54
54
|
- **Try the lab as a student or station.** Open the share link in a *different* browser or a private window. In the same browser you would be the teacher again.
|
|
55
|
-
- **Let students in from their own computers.** Give them the link after `students join at:`. It opens the same class on https://edrys-labs.github.io, so students need nothing but a browser, while your `/?dev` tab stays on your computer and keeps following `lab.yml`. Keep that tab open while students join: they get the class from the browsers that have it open. With an agent or the tutor, the
|
|
55
|
+
- **Let students in from their own computers.** Give them the link after `students join at:`. It opens the same class on https://edrys-labs.github.io, so students need nothing but a browser, while your `/?dev` tab stays on your computer and keeps following `lab.yml`. Keep that tab open while students join: they get the class from the browsers that have it open. With an agent or the tutor, the agent chat in your `/?dev` tab shows this link at the top, with a Copy button. The class's Settings (Members, Communication) give the same link, and below it the address of your tab, which works only on your computer. Three things to know:
|
|
56
56
|
- Both sides must connect the same way. Without `--local` they do. With `--local` the class runs through edrys-dev on your computer, which other computers cannot reach, so edrys-dev prints no link for students.
|
|
57
57
|
- If you picked a WebSocket server in the class's Settings, the link carries it (`#comm=…`); don't cut that part off.
|
|
58
58
|
- Modules from `http://localhost:…` (see below) do not load on the students' computers. edrys-dev warns you about them.
|
|
@@ -138,7 +138,7 @@ By hand:
|
|
|
138
138
|
|
|
139
139
|
### Talking to the agent from the class
|
|
140
140
|
|
|
141
|
-
The class tab has
|
|
141
|
+
The class tab has an agent chat button (sparkles icon) in its top bar, next to the class chat button. It opens the chat on the right side of the class, like the class chat; drag its left edge to make it wider or narrower. On a phone it fills the screen. What you write there goes straight to the agent. Only you see this chat; it is not the class chat. The agent can answer with text and also with buttons or checkboxes for you to pick from.
|
|
142
142
|
|
|
143
143
|
When the class cannot take your lab file, for example after a typing mistake, a red note in the chat says so in plain words, with edrys-Lite's message under "Details". It goes away once the file loads again.
|
|
144
144
|
|
|
@@ -156,7 +156,7 @@ For safety, the chat only accepts messages from the class page on your own compu
|
|
|
156
156
|
|---|---|
|
|
157
157
|
| `get_state` | the lab file, class address, share links (also the one for students), rooms, users and modules |
|
|
158
158
|
| `goto` | move your class tab to a room, or open a student or station tab |
|
|
159
|
-
| `chat`, `read_chat` | write to you in the
|
|
159
|
+
| `chat`, `read_chat` | write to you in the agent chat, read your messages |
|
|
160
160
|
| `read_messages` | messages between modules, and errors from your own modules |
|
|
161
161
|
| `read_module_state` | what the modules share in a room |
|
|
162
162
|
| `read_student_chats` | what students asked the tutor and what it answered (with `--tutor`) |
|
|
@@ -165,17 +165,24 @@ For safety, the chat only accepts messages from the class page on your own compu
|
|
|
165
165
|
|
|
166
166
|
## A tutor for your students
|
|
167
167
|
|
|
168
|
-
Start edrys-dev with `--tutor`, and your students get a
|
|
168
|
+
Start edrys-dev with `--tutor`, and your students get a tutor button (robot icon) in the class's top bar. What they ask there is answered by an AI on your computer: [Claude Code](https://claude.com/claude-code) must be installed and signed in (`claude` in a terminal). With `--tutor-agent codex`, [Codex](https://developers.openai.com/codex) answers instead (`codex login` first). Students need nothing but the class link.
|
|
169
169
|
|
|
170
170
|
- **It knows the lab.** The tutor reads `lab.yml` with every question, so it always knows the version the class shows. It gives hints and explanations instead of whole solutions.
|
|
171
171
|
- **Notes only for the tutor.** Put a file `tutor.md` next to `lab.yml`, for example "Task 2 is about Ohm's law, don't give the number". Students don't receive this file. A student who tries hard may still get the tutor to repeat it, so keep secrets out of it.
|
|
172
172
|
- **It sees the student's room.** With each question it gets what the modules of the student's room share, such as the code in an editor, read through your `/?dev` tab. It cannot change it or run it. It does not see terminal output or the screen, so it asks the student to paste those.
|
|
173
173
|
- **It cannot do anything but answer.** It runs as a separate `claude` without tools, MCP servers or your settings. A student cannot reach your files, your shell or the agent that edits your lab. Codex has no switch for "no tools", so edrys-dev turns off each of its tools and your config, runs it read-only, and drops any answer for which it still used a tool. This was checked with Codex 0.165. If a newer version no longer knows one of these settings, the tutor gives no answers, and the terminal says why.
|
|
174
174
|
- **Your `/?dev` tab passes the messages on.** Keep it open. The button shows only while it is, and only on an edrys-Lite new enough to have it, on your computer and on the students' site alike.
|
|
175
|
-
- **You watch and switch it in the
|
|
175
|
+
- **You watch and switch it in the agent chat.** The agent chat button (sparkles icon) in the top bar of your `/?dev` tab has a Students view: every question and answer as it happens, and a switch that turns the tutor off and on without restarting. While it is off, students don't see their button.
|
|
176
176
|
- **Each student has their own conversation**, which continues with the next question. Chats are saved in `.edrys-dev/tutor/`, and the agent keeps its own copy of each conversation (Claude Code and Codex in their usual session folders). Your agent can read them with `read_student_chats` and tell you where students get stuck.
|
|
177
177
|
- **Limits.** At most two answers are written at once; a question that finds no free turn within 30 seconds gets "the tutor is busy". Each student can have one open question and up to 20 questions an hour, of up to 2000 characters each. The whole class gets up to 200 questions an hour, not counting those turned away as busy (`--tutor-per-hour` changes it), so a student cannot get past the limits with new names and use up your account. When the class reaches it, the terminal and the Students view say so.
|
|
178
|
-
- **Cost.** Each question uses
|
|
178
|
+
- **Cost.** Each question uses the account `claude` or `codex` is signed in to. Answering students with a personal plan (Claude Pro/Max, ChatGPT) may break its terms, which don't let you make your account available to others. For a class, use an API key: start edrys-dev with it, and only the tutor uses it, not your own agent.
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
ANTHROPIC_API_KEY=sk-ant-... npx edrys-dev lab.yml --tutor
|
|
182
|
+
CODEX_API_KEY=sk-... npx edrys-dev lab.yml --tutor --tutor-agent codex
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
The terminal says what pays when edrys-dev starts, e.g. `tutor: on (claude, answers paid with API key from ANTHROPIC_API_KEY)`. With a key, claude runs in its minimal mode (`--bare`), which takes only the key and never your login. Codex reads `CODEX_API_KEY`, not `OPENAI_API_KEY`. `--tutor-model` picks a cheaper model, e.g. `--tutor-model haiku`.
|
|
179
186
|
- **Privacy.** Students are told that an AI on your computer answers and that you can read the chat. Questions and answers are encrypted between the student's browser and your tab, on every connection. Other members of the class can't read them, and neither can a WebSocket server that relays the class.
|
|
180
187
|
|
|
181
188
|
## Options
|
|
@@ -228,22 +235,27 @@ flowchart LR
|
|
|
228
235
|
mods[modules/] -- watch --> msrv["module server :3211<br/>(module pages + error script)"]
|
|
229
236
|
subgraph srv ["edrys-dev :3210"]
|
|
230
237
|
direction TB
|
|
231
|
-
static["edrys-Lite dist +
|
|
232
|
-
api["/edrys-dev/lab · events · reload · bridge · module-error"]
|
|
238
|
+
static["edrys-Lite dist + reload.js"]
|
|
239
|
+
api["/edrys-dev/lab · events · reload · bridge · module-error<br/>chat · tutor · link"]
|
|
233
240
|
end
|
|
234
|
-
srv -- "reload-lab, reload-module (SSE events)" --> tab["class tab /?dev"]
|
|
235
|
-
tab -- "GET lab, republish signed" --> room((class))
|
|
236
|
-
tab -- "state, messages, replies (POST bridge)" --> srv
|
|
241
|
+
srv -- "reload-lab, reload-module, tutor answers (SSE events)" --> tab["class tab /?dev"]
|
|
242
|
+
tab -- "GET lab, republish signed, tutor answers" --> room((class))
|
|
243
|
+
tab -- "state, messages, replies, tutor questions (POST bridge)" --> srv
|
|
237
244
|
msrv -- module frames --> tab
|
|
238
245
|
msrv -. "errors (POST module-error)" .-> srv
|
|
239
246
|
srv -- "reload (SSE)" --> peers
|
|
240
247
|
room -- "default transport, or --local on :3210" --> peers[student / station tabs]
|
|
248
|
+
peers -- "tutor questions" --> room
|
|
249
|
+
room -- "tutor questions" --> tab
|
|
241
250
|
agent[AI agent] <-- "MCP over stdio (--mcp)" --> srv
|
|
251
|
+
srv <-- "question in, answer out (--tutor)" --> tutor["tutor: claude or codex<br/>without tools"]
|
|
242
252
|
```
|
|
243
253
|
|
|
244
254
|
- `index.ts` reads the options and starts the server, and with `--mcp` the MCP server (`mcp.ts`).
|
|
245
255
|
- `server.ts` serves edrys-Lite, watches the lab (`watch.ts`) and talks to the class tab: events go out over SSE, the tab's reports come back over `POST /edrys-dev/bridge`.
|
|
246
|
-
- In the class tab, edrys-Lite's own `DevBridge` (`src/ts/DevBridge.ts` in edrys-Lite, active with `?dev`) loads and republishes the lab and reports the class. edrys-dev
|
|
256
|
+
- In the class tab, edrys-Lite's own `DevBridge` (`src/ts/DevBridge.ts` in edrys-Lite, active with `?dev`) loads and republishes the lab and reports the class. `GET /edrys-dev/lab` also tells it which views of the agent chat there are (`chat: {agent, students}`); edrys-Lite then shows its `AgentChat.vue` drawer, which talks to `chat.ts` (Agent view, `/edrys-dev/chat`) and `tutor.ts` (Students view, `/edrys-dev/tutor`) on the server, and to `/edrys-dev/link/events` for the students' link.
|
|
257
|
+
- edrys-dev adds `views/reload.js` to the page: student and station tabs load again when a local module changed.
|
|
258
|
+
- `tutor.ts` answers students with `--tutor`: the class tab passes their questions on over the bridge, `tutor.ts` runs `claude` or `codex` without tools for each one, and the answer goes back to the tab as an event, which sends it to the student through the class.
|
|
247
259
|
- `modules.ts` serves local modules on the next port and adds the error reporter to their pages.
|
|
248
260
|
- `sync.ts` is the Yjs sync for `--local`, on edrys-dev's own port.
|
|
249
261
|
|
package/dist/chat.js
CHANGED
|
@@ -3,8 +3,6 @@ import fsp from 'node:fs/promises';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import crypto from 'node:crypto';
|
|
5
5
|
import { EventEmitter } from 'node:events';
|
|
6
|
-
import { createRequire } from 'node:module';
|
|
7
|
-
import { fileURLToPath } from 'node:url';
|
|
8
6
|
import express from 'express';
|
|
9
7
|
import { sameOrigin } from './origin.js';
|
|
10
8
|
// what a saved chat keeps, oldest first out
|
|
@@ -444,28 +442,3 @@ export class Chat extends EventEmitter {
|
|
|
444
442
|
}
|
|
445
443
|
}
|
|
446
444
|
}
|
|
447
|
-
const require = createRequire(import.meta.url);
|
|
448
|
-
const views = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'views');
|
|
449
|
-
// browser builds that set window.marked and window.DOMPurify
|
|
450
|
-
const PANEL_FILES = {
|
|
451
|
-
'panel.js': () => path.join(views, 'panel.js'),
|
|
452
|
-
'marked.js': () => path.join(path.dirname(require.resolve('marked/package.json')), 'lib', 'marked.umd.js'),
|
|
453
|
-
'purify.js': () => require.resolve('dompurify/purify.min.js'),
|
|
454
|
-
};
|
|
455
|
-
/** Serves panel.js and the libraries its chat uses. */
|
|
456
|
-
export function panelFiles(app) {
|
|
457
|
-
for (const [file, source] of Object.entries(PANEL_FILES)) {
|
|
458
|
-
// dotfiles: sendFile checks the whole absolute path, and installs often
|
|
459
|
-
// live under one (~/.nvm, ~/.npm/_npx); the path is ours, not the request's
|
|
460
|
-
app.get('/edrys-dev/' + file, (req, res) => res.sendFile(source(), { dotfiles: 'allow' }));
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* The script tag that brings panel.js into the edrys-Lite page: it reloads
|
|
465
|
-
* student and station tabs when a local module changed, and in the class tab
|
|
466
|
-
* shows the chat with `chat` (an agent is connected) and the students' tutor
|
|
467
|
-
* threads with `tutor`.
|
|
468
|
-
*/
|
|
469
|
-
export function panelScript(chat, tutor = false) {
|
|
470
|
-
return `<script src="/edrys-dev/panel.js" defer${chat ? ' data-chat' : ''}${tutor ? ' data-tutor' : ''}></script>`;
|
|
471
|
-
}
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { startDevServer } from './server.js';
|
|
|
7
7
|
import { startMcp, studentLink, localUrls } from './mcp.js';
|
|
8
8
|
import { reachable } from './origin.js';
|
|
9
9
|
import { readSaved } from './watch.js';
|
|
10
|
-
import { CLASS_PER_HOUR, TUTOR_AGENTS } from './tutor.js';
|
|
10
|
+
import { CLASS_PER_HOUR, TUTOR_AGENTS, tutorAuth } from './tutor.js';
|
|
11
11
|
const HELP = `edrys-dev: develop an edrys-Lite lab locally
|
|
12
12
|
|
|
13
13
|
Usage: edrys-dev [lab.yml] [options]
|
|
@@ -167,8 +167,9 @@ server.bus.on('report', async (report) => {
|
|
|
167
167
|
const host = ['127.0.0.1', '0.0.0.0', '::'].includes(values.host) ? 'localhost' : values.host;
|
|
168
168
|
const url = `http://${host}:${server.port}/?dev`;
|
|
169
169
|
say(`edrys-dev: ${lab}\n ${url}`);
|
|
170
|
-
if (server.tutor)
|
|
171
|
-
say(`tutor: on (${tutorAgent}), notes for it in ${path.join(path.dirname(lab), 'tutor.md')}`);
|
|
170
|
+
if (server.tutor) {
|
|
171
|
+
say(`tutor: on (${tutorAgent}, answers paid with ${tutorAuth(tutorAgent)}), notes for it in ${path.join(path.dirname(lab), 'tutor.md')}`);
|
|
172
|
+
}
|
|
172
173
|
if (server.modules)
|
|
173
174
|
say(`modules: ${server.modules.dir}\n ${server.modules.url}<name>/...`);
|
|
174
175
|
if (reachable(values.host)) {
|
package/dist/init.js
CHANGED
|
@@ -21,7 +21,7 @@ modules:
|
|
|
21
21
|
content: |
|
|
22
22
|
# Welcome
|
|
23
23
|
|
|
24
|
-
Open the
|
|
24
|
+
Open the agent chat (sparkles button in the top bar) and tell the agent what this lab should do.
|
|
25
25
|
width: full
|
|
26
26
|
height: tall
|
|
27
27
|
`;
|
|
@@ -37,7 +37,7 @@ When a session starts:
|
|
|
37
37
|
|
|
38
38
|
The user may not be a programmer. Answer in plain words, show no YAML or code in the chat unless they ask, and offer choices as chat forms (buttons).
|
|
39
39
|
|
|
40
|
-
When they want to share the class or ask how students join, give them \`get_state.student_link\` (the class's own share link works only on this computer). It is also shown at the top of the
|
|
40
|
+
When they want to share the class or ask how students join, give them \`get_state.student_link\` (the class's own share link works only on this computer). It is also shown at the top of the agent chat panel.
|
|
41
41
|
|
|
42
42
|
If the edrys-lab and edrys-module skills are installed, use them to write the lab and its modules.
|
|
43
43
|
`;
|
|
@@ -118,7 +118,7 @@ Your lab is ready in ${dir}
|
|
|
118
118
|
|
|
119
119
|
Next: open this folder in your AI agent (Claude Code, or VS Code with Copilot)
|
|
120
120
|
and write "start". The agent starts edrys-dev and opens the class in your
|
|
121
|
-
browser; from then on you can talk to it in the
|
|
121
|
+
browser; from then on you can talk to it in the agent chat there (sparkles button in the top bar).`);
|
|
122
122
|
if (extra.includes('--tutor')) {
|
|
123
123
|
say(`
|
|
124
124
|
Your students get a tutor chat in the class, answered by the AI on this
|
package/dist/mcp.js
CHANGED
|
@@ -94,7 +94,7 @@ const TOOLS = [
|
|
|
94
94
|
},
|
|
95
95
|
{
|
|
96
96
|
name: 'chat',
|
|
97
|
-
description: 'Post a message into the chat panel over the class tab; only the user sees it, not the class. markdown is Markdown (
|
|
97
|
+
description: 'Post a message into the chat panel over the class tab; only the user sees it, not the class. markdown is Markdown (HTML in it shows as text, links open in a new tab). status alone updates the progress line below the messages; with markdown it stays after the message; every message without it clears it. reply_to: the entry of the chat or answer event this answers; the result then has newer_user_messages if the user wrote or answered since: answer those too. With inputs the message carries a form and the call BLOCKS until the user answers: {ask, entry, values: {<id>: option | [options]}}, or {ask, entry, dismissed: true, text} if the user typed instead (the form stays open, a later answer arrives as an answer event), {ask, superseded: true} if a newer form replaced it, {ask, disconnected: true} if the panel closed. While a Monitor watches get_state.events, or with wait: false, it returns at once with {sent, ask} and the answer arrives as an answer event. Only the newest form stays open; close: true closes it (alone or with a message), reason says why under it. session: the id of the chat you answer in; if the user opened another one meanwhile, nothing is sent. {ask, switched: true}: the user opened another chat while the form waited. A form waits at most timeout_s (default 3600), then returns {ask, timeout: true}; it stays open and a later answer arrives as an answer event. buttons: one click answers (a single buttons input submits at once); radio: one option; checkbox: any number.',
|
|
98
98
|
inputSchema: {
|
|
99
99
|
type: 'object',
|
|
100
100
|
properties: {
|
package/dist/server.js
CHANGED
|
@@ -4,12 +4,13 @@ import fs from 'node:fs/promises';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { EventEmitter } from 'node:events';
|
|
6
6
|
import { createRequire } from 'node:module';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
7
8
|
import express from 'express';
|
|
8
9
|
import { unpack } from 'msgpackr';
|
|
9
10
|
import { readSaved, watchFile, watchModules } from './watch.js';
|
|
10
11
|
import { moduleApp } from './modules.js';
|
|
11
12
|
import { hostGuard, hostNames, sameOrigin } from './origin.js';
|
|
12
|
-
import { Chat, explain
|
|
13
|
+
import { Chat, explain } from './chat.js';
|
|
13
14
|
import { STATE_MS, Tutor, threadId } from './tutor.js';
|
|
14
15
|
import { localUrls, studentLink } from './mcp.js';
|
|
15
16
|
// entries the log keeps, oldest first out
|
|
@@ -43,6 +44,8 @@ const SETTLE_MS = 2000;
|
|
|
43
44
|
// what the chat panel shows of the students' tutor threads
|
|
44
45
|
const TUTOR_THREADS = 50;
|
|
45
46
|
const TUTOR_ENTRIES = 50;
|
|
47
|
+
// reloads student and station tabs of this machine when a local module changed
|
|
48
|
+
const RELOAD_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'views', 'reload.js');
|
|
46
49
|
/** The built edrys-Lite this package serves. */
|
|
47
50
|
export function edrysDist() {
|
|
48
51
|
const require = createRequire(import.meta.url);
|
|
@@ -85,6 +88,8 @@ export async function startDevServer(options) {
|
|
|
85
88
|
text: await readSaved(options.lab),
|
|
86
89
|
// the class tab announces it to the students
|
|
87
90
|
tutor: !!tutor?.enabled,
|
|
91
|
+
// the views of edrys-Lite's agent chat in the class tab
|
|
92
|
+
chat: { agent: !!options.agent, students: !!tutor },
|
|
88
93
|
// the class tab's own links for students go there; null: they cannot (--local)
|
|
89
94
|
studentSite: options.studentSite ?? null,
|
|
90
95
|
// Without it the class keeps its own (or edrys-Lite's default) transport.
|
|
@@ -258,9 +263,10 @@ export async function startDevServer(options) {
|
|
|
258
263
|
// without an agent, nobody would answer
|
|
259
264
|
if (options.agent)
|
|
260
265
|
chat.routes(app);
|
|
261
|
-
|
|
262
|
-
//
|
|
263
|
-
|
|
266
|
+
// dotfiles: sendFile checks the whole absolute path, and installs often
|
|
267
|
+
// live under one (~/.nvm, ~/.npm/_npx); the path is ours, not the request's
|
|
268
|
+
app.get('/edrys-dev/reload.js', (req, res) => res.sendFile(RELOAD_SCRIPT, { dotfiles: 'allow' }));
|
|
269
|
+
// the link for students in the agent chat: the teacher may see neither the
|
|
264
270
|
// terminal nor the agent. Again when the class opens and when the lab changes.
|
|
265
271
|
const linkWatchers = new Set();
|
|
266
272
|
const linkState = async () => ({
|
|
@@ -285,9 +291,10 @@ export async function startDevServer(options) {
|
|
|
285
291
|
linkWatchers.add(res);
|
|
286
292
|
req.on('close', () => linkWatchers.delete(res));
|
|
287
293
|
});
|
|
294
|
+
// edrys-Lite's page, with reload.js for the student and station tabs
|
|
288
295
|
app.get(['/', '/index.html'], async (req, res) => {
|
|
289
296
|
const page = await fs.readFile(path.join(edrysDist(), 'index.html'), 'utf8');
|
|
290
|
-
res.type('html').send(page +
|
|
297
|
+
res.type('html').send(page + '<script src="/edrys-dev/reload.js" defer></script>');
|
|
291
298
|
});
|
|
292
299
|
app.use(express.static(edrysDist()));
|
|
293
300
|
const server = http.createServer(app);
|
package/dist/tutor.js
CHANGED
|
@@ -36,6 +36,15 @@ const CODEX_CONFIG = [
|
|
|
36
36
|
'include_collaboration_mode_instructions=false',
|
|
37
37
|
'include_apps_instructions=false',
|
|
38
38
|
];
|
|
39
|
+
/**
|
|
40
|
+
* What the tutor's answers are paid with: an API key in the environment, or
|
|
41
|
+
* the account the agent CLI is signed in to.
|
|
42
|
+
*/
|
|
43
|
+
export function tutorAuth(agent = 'claude', env = process.env) {
|
|
44
|
+
if (agent === 'codex')
|
|
45
|
+
return env.CODEX_API_KEY ? 'API key from CODEX_API_KEY' : 'the account codex is signed in to';
|
|
46
|
+
return env.ANTHROPIC_API_KEY ? 'API key from ANTHROPIC_API_KEY' : 'the account claude is signed in to';
|
|
47
|
+
}
|
|
39
48
|
// what a codex --json run may report; anything else is a tool it used
|
|
40
49
|
const CODEX_ITEMS = ['agent_message', 'reasoning', 'error'];
|
|
41
50
|
const RULES = `You are the tutor of an edrys-Lite class: students ask you while they work through the lab below. Help each student learn: explain, give hints, ask back what they tried, point to the part of the lab that helps. Do not hand out whole solutions to tasks of the lab, unless the teacher's notes allow it. Answer short and friendly, in the language of the student, in Markdown.
|
|
@@ -342,6 +351,9 @@ export class Tutor extends EventEmitter {
|
|
|
342
351
|
const id = session ?? crypto.randomUUID();
|
|
343
352
|
const { code, out, err } = await this.spawn([
|
|
344
353
|
'-p',
|
|
354
|
+
// signed in, claude -p answers with that account and passes over ANTHROPIC_API_KEY;
|
|
355
|
+
// --bare takes strictly the key (checked with Claude Code 2.1.316)
|
|
356
|
+
...(process.env.ANTHROPIC_API_KEY ? ['--bare'] : []),
|
|
345
357
|
'--tools', '',
|
|
346
358
|
'--strict-mcp-config',
|
|
347
359
|
'--setting-sources', '',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edryslabs/edrys-dev",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Develop edrys-Lite labs locally: serves edrys-Lite, syncs on its own port and republishes the class whenever the lab file changes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -36,11 +36,9 @@
|
|
|
36
36
|
},
|
|
37
37
|
"homepage": "https://github.com/edrys-labs/edrys-devserver#readme",
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@edryslabs/edrys-lite": "3.2.0-dev.
|
|
39
|
+
"@edryslabs/edrys-lite": "3.2.0-dev.5",
|
|
40
40
|
"chokidar": "^4.0.3",
|
|
41
|
-
"dompurify": "^3.4.17",
|
|
42
41
|
"express": "^5.1.0",
|
|
43
|
-
"marked": "^18.1.1",
|
|
44
42
|
"msgpackr": "^2.1.2",
|
|
45
43
|
"open": "^10.1.0"
|
|
46
44
|
},
|
package/views/reload.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Injected by edrys-dev into edrys-Lite's page: student and station tabs load
|
|
2
|
+
// again when a local module changed. The tab opened with ?dev (DevBridge keeps
|
|
3
|
+
// that in sessionStorage) reloads only the changed modules itself, and shows
|
|
4
|
+
// the agent chat (AgentChat.vue in edrys-Lite).
|
|
5
|
+
;(function () {
|
|
6
|
+
if (location.search === '?dev' || sessionStorage.getItem('edrys-dev')) return
|
|
7
|
+
if (!/^\?\/(classroom|station)\//.test(location.search)) return
|
|
8
|
+
new EventSource('/edrys-dev/reload').addEventListener('reload-module', () => location.reload())
|
|
9
|
+
})()
|
package/views/panel.js
DELETED
|
@@ -1,661 +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
|
-
case 'limit':
|
|
603
|
-
tutorNote.textContent = `The class reached its limit of ${message.perHour} questions this hour: more get no answer for now. Start edrys-dev with --tutor-per-hour to change it.`
|
|
604
|
-
if (!panel.classList.contains('open')) toggle.classList.add('unread')
|
|
605
|
-
break
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
// --- the link for students: the class's share link would only work on this computer ---
|
|
611
|
-
const shareBox = root.querySelector('.share')
|
|
612
|
-
const shareRow = shareBox.querySelector('.row')
|
|
613
|
-
const shareInput = shareBox.querySelector('input')
|
|
614
|
-
const shareButton = shareBox.querySelector('button')
|
|
615
|
-
const shareNote = shareBox.querySelector('.note')
|
|
616
|
-
|
|
617
|
-
function showLink({ reachable, link, local_urls }) {
|
|
618
|
-
shareBox.hidden = false
|
|
619
|
-
shareRow.hidden = !link
|
|
620
|
-
shareInput.value = link || ''
|
|
621
|
-
const modules = local_urls.length
|
|
622
|
-
shareNote.textContent = !reachable
|
|
623
|
-
? 'Students on other computers cannot join: edrys-dev runs with --local, which reaches this computer only.'
|
|
624
|
-
: !link
|
|
625
|
-
? 'The link for students appears once the class is open.'
|
|
626
|
-
: modules
|
|
627
|
-
? `${modules === 1 ? 'A module loads' : `${modules} modules load`} from this computer (localhost) and will not load for students: publish ${modules === 1 ? 'it' : 'them'} first.`
|
|
628
|
-
: ''
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
shareInput.addEventListener('focus', () => shareInput.select())
|
|
632
|
-
shareButton.addEventListener('click', async () => {
|
|
633
|
-
let copied = false
|
|
634
|
-
try {
|
|
635
|
-
await navigator.clipboard.writeText(shareInput.value)
|
|
636
|
-
copied = true
|
|
637
|
-
} catch {
|
|
638
|
-
shareInput.select()
|
|
639
|
-
try {
|
|
640
|
-
copied = document.execCommand('copy')
|
|
641
|
-
} catch {}
|
|
642
|
-
}
|
|
643
|
-
shareButton.textContent = copied ? 'Copied' : 'Select and copy it'
|
|
644
|
-
setTimeout(() => (shareButton.textContent = 'Copy'), 2000)
|
|
645
|
-
})
|
|
646
|
-
|
|
647
|
-
new EventSource('/edrys-dev/link/events').onmessage = (event) => showLink(JSON.parse(event.data))
|
|
648
|
-
|
|
649
|
-
// the renderer first, then the messages that need it
|
|
650
|
-
let loaded = 0
|
|
651
|
-
for (const name of ['marked', 'purify']) {
|
|
652
|
-
const script = document.createElement('script')
|
|
653
|
-
script.src = `/edrys-dev/${name}.js`
|
|
654
|
-
script.onload = () => {
|
|
655
|
-
if (++loaded < 2) return
|
|
656
|
-
if (hasChat) connect()
|
|
657
|
-
if (hasTutor) connectTutor()
|
|
658
|
-
}
|
|
659
|
-
document.head.appendChild(script)
|
|
660
|
-
}
|
|
661
|
-
})()
|