File size: 26,498 Bytes
fd475e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f5bc4
 
 
 
 
fd475e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f5bc4
 
fd475e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f5bc4
 
 
fd475e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
"""AI help (PRD 4.15): students never call the AI. A student's photographed problem, or a check of a handed-in answer, is a request in the queue
of the class's teacher, who sends it or declines it. The AI runs in a small background pool, so a send returns at once and a batch does not hold
the page. Budgets are taken in one step before the call and given back when it fails or could not read the picture."""
from __future__ import annotations

import logging
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from typing import Literal

from fastapi import APIRouter, Depends, HTTPException, Request
from pydantic import BaseModel, Field
from starlette.concurrency import run_in_threadpool

from . import ai, clock, pictures, reads
from .access import allowed, learner, need
from .assignments import _my_assignment, quiz_view
from .pictures import PictureError
from .realtests import _my_class
from .written import picture_error

log = logging.getLogger("mathly2.ai_help")
router = APIRouter(prefix="/api")

MAX_WAITING = 5            # a student's requests waiting at once
MAX_PHOTOS_A_DAY = 20
MAX_LOOK_AGAIN = 5         # a student's open requests to look again
TEACHER_ALLOWANCE = 3      # a teacher's own AI requests a day
OPEN = ("waiting", "processing")
POOL = ThreadPoolExecutor(max_workers=3, thread_name_prefix="ai")


def _is_staff(user: dict) -> bool:
    return allowed(user["roles"], "class.teach")


def _developer(user: dict) -> bool:
    return allowed(user["roles"], "*")


# ---- what a request looks like ----

def _skill(request: Request, skill_id: str | None) -> dict | None:
    skill = request.app.state.bank.skill(skill_id) if skill_id else None
    return {"id": skill["id"], "name": skill["name"]} if skill else None


def view(request: Request, row: dict, staff: bool = False, emails: dict[str, str] | None = None) -> dict:
    """A request as its student sees it, or (`staff`) as the teacher who sends it and a developer see it, with the failure's cause."""
    store, here = request.app.state.store, reads.of(request)
    klass = here.get_class(row["class_id"]) if row["class_id"] else None
    out = {"id": row["id"], "kind": row["kind"], "state": row["state"], "created_at": row["created_at"].isoformat(), "teacher_note": row["teacher_note"],
           "class": {"id": klass["id"], "name": klass["name"]} if klass else None, "skill": _skill(request, row["skill_id"]), "result": None}
    if staff:
        out["student"] = (emails or {}).get(row["user_id"], "")
    if row["kind"] == "photo":
        picture = store.get_picture(row["picture_id"]) if row["picture_id"] else None
        out["picture"] = {"id": picture["id"]} if picture and not picture["deleted_at"] else None
    else:
        assignment = here.get_assignment(row["assignment_id"])
        out["assignment"] = {"id": row["assignment_id"], "title": assignment["title"] if assignment else ""}
    result = row["result"]
    if row["state"] == "done" and result:
        if row["kind"] == "photo":
            out["result"] = {**result, "skill": _skill(request, result.get("skill"))}
        else:
            keep = ("score", "verdict", "feedback", "next_step") + (("transcription", "solution", "readable", "confident") if staff else ())
            out["result"] = {k: result[k] for k in keep if k in result}
    if row["state"] == "failed":
        failure = row["failure"] or {}
        if staff:
            label, todo = ai.FAILURES.get(failure.get("reason"), ai.FAILURES["provider_error"])
            out["failure"] = {"reason": failure.get("reason"), "label": label, "todo": todo, "detail": failure.get("detail", "")}
        else:
            out["failure"] = {"message": ai.STUDENT_MESSAGE}
    return out


def snapshot_of(request: Request, assignment: dict) -> dict:
    """The question as it is now, kept with a check: later changes to the bank or the assignment do not change an old check. No name, no email."""
    snap = {"title": assignment["title"], "note": assignment["note"], "kind": assignment["kind"]}
    if assignment["kind"] == "quiz":
        snap["quiz"] = quiz_view(request.app.state.bank, assignment["quiz_lesson"])
    return snap


# ---- the budgets and the run ----

def _budget(request: Request, row: dict, sender: dict) -> dict | None:
    """What to take for sending this request: the class's daily checks for a student's request, the teacher's own allowance for their own,
    nothing for a developer's own. Returns {"scope", "cap"}."""
    if row["user_id"] == sender["id"]:
        return None if _developer(sender) else {"scope": f"teacher:{sender['email']}", "cap": TEACHER_ALLOWANCE}
    klass = reads.of(request).get_class(row["class_id"])
    return {"scope": f"class:{row['class_id']}", "cap": klass["ai_daily_checks"] if klass else 0}


def send(request: Request, sender: dict, row: dict) -> dict:
    """Take the budget, mark the request processing and hand it to the pool. Raises an HTTP error that says why it could not."""
    store, app = request.app.state.store, request.app
    if row["state"] not in ("waiting", "failed"):
        raise HTTPException(409, "ื”ื‘ืงืฉื” ื›ื‘ืจ ื˜ื•ืคืœื”")
    own = row["user_id"] == sender["id"]
    if not own:
        klass = reads.of(request).get_class(row["class_id"])
        if not klass or not klass["ai_enabled"]:
            raise HTTPException(409, "ื”ึพAI ื›ื‘ื•ื™ ืœื›ื™ืชื” ื”ื–ื•")
    take = _budget(request, row, sender)
    today = clock.school_day()
    if take and not store.take_ai_allowance(take["scope"], today, 1, take["cap"]):
        raise HTTPException(429, "ื ื’ืžืจื” ื”ืžื›ืกื” ื”ื™ื•ืžื™ืช ืฉืœ ื‘ื“ื™ืงื•ืช ื”ึพAI" if take["scope"].startswith("class:") else f"ื ื’ืžืจื• {TEACHER_ALLOWANCE} ื‘ืงืฉื•ืช ื”ึพAI ื”ืื™ืฉื™ื•ืช ืœื”ื™ื•ื")
    started = store.start_ai_request(row["id"], sender["email"], clock.now())
    if started is None:
        if take:
            store.give_back_ai_allowance(take["scope"], today, 1)
        raise HTTPException(409, "ื”ื‘ืงืฉื” ื›ื‘ืจ ื˜ื•ืคืœื”")
    if take:
        started = store.save_ai_request(row["id"], budget={"scope": take["scope"], "day": today.isoformat(), "n": 1})
    POOL.submit(run, app, row["id"])
    return started


def _give_back(store, row: dict) -> None:
    b = row.get("budget")
    if b:
        store.give_back_ai_allowance(b["scope"], datetime.fromisoformat(b["day"]).date(), b["n"])
        store.save_ai_request(row["id"], budget=None)


def _delete_photo(app, row: dict) -> None:
    pic = app.state.store.get_picture(row["picture_id"]) if row.get("picture_id") else None
    if pic and not pic["deleted_at"]:
        try:
            pictures.remove_picture(app.state.store, app.state.pictures, pic, clock.now())
        except PictureError as exc:
            log.warning("could not delete a help photo (%s)", exc.code)


def run(app, request_id: str) -> None:
    """The AI call for one processing request, in the pool. It always leaves the request done or failed."""
    store, bank = app.state.store, app.state.bank
    row = store.get_ai_request(request_id)
    if row is None or row["state"] != "processing":
        return
    try:
        if row["kind"] == "photo":
            pic = store.get_picture(row["picture_id"])
            if pic is None or pic["deleted_at"]:
                raise ai.AIFailure("bad_request", "the photo is gone")
            subject = bank.subject_of_skill(row["skill_id"]) if row["skill_id"] else (row["snapshot"] or {}).get("subject")
            subject = subject if any(s["id"] == subject for s in bank.subjects) else "math"
            skills = [{"id": s["id"], "name": s["name"]} for s in bank.skills if bank.subject_of_skill(s["id"]) == subject]
            name = next(s["name"] for s in bank.subjects if s["id"] == subject)
            result = app.state.ai.solve(app.state.pictures.get(pic["path"]), "image/jpeg", skills, *([] if subject == "math" else [name]))
            unreadable = not result.get("readable")
        else:
            images = _check_images(app, row)
            result = app.state.ai.check(images, row["snapshot"] or {})
            unreadable = result.get("verdict") in ("unreadable", "needs_review")
        usage = result.pop("usage", None)
        store.save_ai_request(request_id, state="done", result=result, done_at=clock.now())
        if unreadable:
            _give_back(store, row)                              # a picture the AI could not read does not use the budget
        if row["kind"] == "photo":
            _delete_photo(app, row)                             # the photo is not kept
        _record(app, row, usage)
    except ai.AIFailure as exc:
        _fail(store, row, exc.reason, exc.detail)
    except Exception as exc:                                    # never leave a request processing
        log.exception("AI run failed")
        _fail(store, row, "provider_error", type(exc).__name__)


def _fail(store, row: dict, reason: str, detail: str) -> None:
    store.save_ai_request(row["id"], state="failed", failure={"reason": reason, "detail": detail[:300]}, done_at=clock.now())
    _give_back(store, row)


def _record(app, row: dict, usage: dict | None) -> None:
    if not usage:
        return
    try:
        app.state.store.add_ai_usage(ai.usage_row(usage, "photo" if row["kind"] == "photo" else "check", row["id"], row["user_id"]))
    except Exception as exc:                                    # bookkeeping never fails a request
        log.warning("AI usage not saved: %s", type(exc).__name__)


def _check_images(app, row: dict) -> list[tuple[str, bytes]]:
    store, backend = app.state.store, app.state.pictures
    assignment, sub = store.get_assignment(row["assignment_id"]), store.get_submission(row["assignment_id"], row["user_id"])
    if assignment is None or sub is None:
        raise ai.AIFailure("bad_request", "the answer is gone")
    images: list[tuple[str, bytes]] = []
    ids = [("ื“ืคื™ ื”ืžื˜ืœื”", p) for p in assignment["pages"]] + [("ืชืฉื•ื‘ืช ื”ืชืœืžื™ื“", p) for p in sub["pictures"]]
    for label, pid in ids:
        pic = store.get_picture(pid)
        if pic is None or pic["deleted_at"]:
            if label == "ืชืฉื•ื‘ืช ื”ืชืœืžื™ื“":
                raise ai.AIFailure("bad_request", "the answer pictures were deleted")
            continue
        images.append((label, backend.get(pic["path"])))
    return images


def requeue_after_restart(store) -> int:
    try:
        return store.requeue_processing_ai_requests()
    except Exception as exc:                                    # the AI tables may not be migrated yet
        log.warning("could not requeue AI requests: %s", type(exc).__name__)
        return 0


# ---- the student ----

def _student_classes(request: Request, user: dict) -> list[dict]:
    return reads.of(request).classes_of_student(user["email"])


def _choose_class(request: Request, user: dict, wanted: int | None) -> dict:
    """The class whose teacher gets the request: the one asked for, else the nearest coming test, else the one used last, else the first.
    Only a class with the AI on can be chosen."""
    store, today = request.app.state.store, clock.school_day()
    on = [c for c in _student_classes(request, user) if c["ai_enabled"]]
    if not on:
        raise HTTPException(409, "ื”ึพAI ืœื ืคื•ืขืœ ื‘ื›ื™ืชื” ืฉืœืš")
    if wanted is not None:
        found = next((c for c in on if c["id"] == wanted), None)
        if found is None:
            raise HTTPException(409, "ื”ึพAI ื›ื‘ื•ื™ ื‘ื›ื™ืชื” ื”ื–ื•")
        return found
    soonest = {}
    for c in on:
        days = [(t["date"] - today).days for t in store.tests_of_classes([c["id"]]) if t["date"] >= today]
        if days:
            soonest[c["id"]] = min(days)
    if soonest:
        return next(c for c in on if c["id"] == min(soonest, key=soonest.get))
    last = store.ai_requests(user_id=user["id"])
    used = [r["class_id"] for r in reversed(last) if r["class_id"] in {c["id"] for c in on}]
    return next(c for c in on if c["id"] == used[0]) if used else on[0]


def _check_student_limits(request: Request, user: dict, photo: bool) -> None:
    mine = request.app.state.store.ai_requests(user_id=user["id"])
    if sum(1 for r in mine if r["state"] == "waiting" and r["kind"] != "again") >= MAX_WAITING:
        raise HTTPException(429, f"ื™ืฉ ื›ื‘ืจ {MAX_WAITING} ื‘ืงืฉื•ืช ืฉืžื—ื›ื•ืช ืœืžื•ืจื”. ืืคืฉืจ ืœื‘ื˜ืœ ืื—ืช ืื• ืœื—ื›ื•ืช.")
    today = clock.school_day()
    if photo and sum(1 for r in mine if r["kind"] == "photo" and clock.school_day(r["created_at"]) == today) >= MAX_PHOTOS_A_DAY:
        raise HTTPException(429, f"ืืคืฉืจ ืœืฉืœื•ื— ืขื“ {MAX_PHOTOS_A_DAY} ืชืžื•ื ื•ืช ื‘ื™ื•ื")


@router.post("/ai/photo", status_code=201)
async def photo(request: Request, class_id: int | None = None, skill_id: str | None = None, subject: str | None = None,
                user: dict = Depends(learner)) -> dict:
    """A photographed problem (raw image bytes; the Content-Type says which kind). A student's goes to the queue of the class's teacher; a teacher's or
    developer's own is sent at once."""
    staff = _is_staff(user)
    if skill_id and request.app.state.bank.skill(skill_id) is None:
        raise HTTPException(422, "ืžื™ื•ืžื ื•ืช ืœื ืžื•ื›ืจืช")
    declared = request.headers.get("content-length")
    if declared and declared.isdigit() and int(declared) > pictures.MAX_IMAGE + 1024:
        raise picture_error(PictureError(413, "image_too_large"))
    data = await request.body()
    content_type = request.headers.get("content-type", "")

    def work() -> dict:
        store = request.app.state.store
        klass = None
        if not staff:
            klass = _choose_class(request, user, class_id)
            _check_student_limits(request, user, True)
        jpeg = pictures.clean_image(data, content_type)
        pic = pictures.store_picture(store, request.app.state.pictures, user["id"], "help", jpeg)
        known = subject if subject and any(s["id"] == subject for s in request.app.state.bank.subjects) else None
        row = store.create_ai_request("photo", user["id"], klass["id"] if klass else None, picture_id=pic["id"], skill_id=skill_id,
                                      snapshot={"subject": known} if known and not skill_id else None, created_at=clock.now())
        if staff:
            try:
                row = send(request, user, row)                       # their own request goes straight out
            except HTTPException:
                store.save_ai_request(row["id"], state="cancelled")
                _delete_photo(request.app, row)
                raise
        return view(request, row)
    try:
        return await run_in_threadpool(work)
    except PictureError as exc:
        raise picture_error(exc) from exc


@router.post("/assignments/{assignment_id}/ai-check", status_code=201)
def ask_for_check(assignment_id: int, request: Request, user: dict = Depends(learner)) -> dict:
    """The student asks for an AI check of their handed-in, not yet checked answer. It waits for the teacher."""
    store, here = request.app.state.store, reads.of(request)
    assignment = _my_assignment(request, user, assignment_id)
    klass = here.get_class(assignment["class_id"])
    if not klass or not klass["ai_enabled"]:
        raise HTTPException(409, "ื”ึพAI ื›ื‘ื•ื™ ื‘ื›ื™ืชื” ื”ื–ื•")
    sub = store.get_submission(assignment_id, user["id"])
    if assignment["kind"] == "exercise_test" or sub is None or sub["state"] != "handed_in":
        raise HTTPException(409, "ืืคืฉืจ ืœื‘ืงืฉ ื‘ื“ื™ืงื” ืจืง ืœื”ื’ืฉื” ืฉืขื•ื“ ืœื ื ื‘ื“ืงื”")
    if store.ai_requests(user_id=user["id"], assignment_id=assignment_id, kinds=["check"], states=OPEN):
        raise HTTPException(409, "ื›ื‘ืจ ื‘ื™ืงืฉืชื ื‘ื“ื™ืงื” ืœื”ื’ืฉื” ื”ื–ื•")
    _check_student_limits(request, user, False)
    row = store.create_ai_request("check", user["id"], klass["id"], assignment_id=assignment_id, snapshot=snapshot_of(request, assignment), created_at=clock.now())
    return view(request, row)


@router.post("/assignments/{assignment_id}/look-again", status_code=201)
def ask_to_look_again(assignment_id: int, request: Request, user: dict = Depends(learner)) -> dict:
    """The student disagrees with a check or a grade and asks the teacher to look again: one open request per answer, five per student."""
    store = request.app.state.store
    assignment = _my_assignment(request, user, assignment_id)
    sub = store.get_submission(assignment_id, user["id"])
    done_check = store.ai_requests(user_id=user["id"], assignment_id=assignment_id, kinds=["check"], states=["done"])
    if sub is None or not (sub["state"] in ("graded", "returned") or done_check):
        raise HTTPException(409, "ืืคืฉืจ ืœื‘ืงืฉ ื‘ื“ื™ืงื” ื—ื•ื–ืจืช ืจืง ืื—ืจื™ ื‘ื“ื™ืงื”")
    mine = store.ai_requests(user_id=user["id"], kinds=["again"], states=["waiting"])
    if any(r["assignment_id"] == assignment_id for r in mine):
        raise HTTPException(409, "ื›ื‘ืจ ื‘ื™ืงืฉืชื ืžื”ืžื•ืจื” ืœื‘ื“ื•ืง ืฉื•ื‘")
    if len(mine) >= MAX_LOOK_AGAIN:
        raise HTTPException(429, f"ื™ืฉ ื›ื‘ืจ {MAX_LOOK_AGAIN} ื‘ืงืฉื•ืช ืœื‘ื“ื™ืงื” ื—ื•ื–ืจืช ืคืชื•ื—ื•ืช")
    row = store.create_ai_request("again", user["id"], assignment["class_id"], assignment_id=assignment_id, created_at=clock.now())
    return view(request, row)


def close_look_again(store, assignment_id: int, user_id: str) -> None:
    """Saving a review closes the student's open requests to look again."""
    for r in store.ai_requests(user_id=user_id, assignment_id=assignment_id, kinds=["again"], states=["waiting"]):
        store.save_ai_request(r["id"], state="closed", done_at=clock.now())


@router.get("/ai/status")
def ai_status(request: Request, user: dict = Depends(learner)) -> dict:
    """Whether to show photo help: a student's classes have the AI on (a teacher or developer can always use it, with their allowance), and the
    student's latest requests with their state."""
    on = _is_staff(user) or any(c["ai_enabled"] for c in _student_classes(request, user))
    rows = request.app.state.store.ai_requests(user_id=user["id"], kinds=["photo"]) if on else []
    return {"on": on, "requests": [view(request, r) for r in reversed(rows) if r["state"] != "cancelled"][:5]}


@router.get("/ai/requests")
def my_requests(request: Request, user: dict = Depends(learner)) -> list[dict]:
    rows = request.app.state.store.ai_requests(user_id=user["id"])
    return [view(request, r, staff=_is_staff(user)) for r in reversed(rows)][:30]


def _readable(request: Request, user: dict, row: dict) -> bool:
    if row["user_id"] == user["id"]:
        return True
    if _developer(user):
        return True
    klass = request.app.state.store.get_class(row["class_id"]) if row["class_id"] else None
    return bool(klass and (klass["teacher_email"] or "").lower() == user["email"] and _is_staff(user))


def _mine_or_mine_to_handle(request: Request, user: dict, request_id: str) -> dict:
    row = request.app.state.store.get_ai_request(request_id)
    if row is None or not _readable(request, user, row):
        raise HTTPException(404, "ื”ื‘ืงืฉื” ืœื ื ืžืฆืื”")
    return row


@router.get("/ai/requests/{request_id}")
def one_request(request_id: str, request: Request, user: dict = Depends(learner)) -> dict:
    row = _mine_or_mine_to_handle(request, user, request_id)
    emails = {m["user_id"]: m["email"] for m in request.app.state.store.members(row["class_id"]) if m["user_id"]} if row["class_id"] else {}
    return view(request, row, staff=row["user_id"] != user["id"] or _is_staff(user), emails=emails)


@router.post("/ai/requests/{request_id}/cancel")
def cancel(request_id: str, request: Request, user: dict = Depends(learner)) -> dict:
    store = request.app.state.store
    row = store.get_ai_request(request_id)
    if row is None or row["user_id"] != user["id"]:
        raise HTTPException(404, "ื”ื‘ืงืฉื” ืœื ื ืžืฆืื”")
    if row["state"] != "waiting":
        raise HTTPException(409, "ืืคืฉืจ ืœื‘ื˜ืœ ืจืง ื‘ืงืฉื” ืฉืขื“ื™ื™ืŸ ืžื—ื›ื”")
    saved = store.save_ai_request(request_id, state="cancelled", done_at=clock.now())
    _delete_photo(request.app, row)
    return view(request, saved)


# ---- the teacher ----

def _teachable(request: Request, user: dict) -> list[dict]:
    store = request.app.state.store
    return store.classes() if _developer(user) else store.classes_taught(user["email"])


def _handle(request: Request, user: dict, request_id: str) -> dict:
    """A request the teacher may send or decline: one of their classes' students' (a developer: any)."""
    row = request.app.state.store.get_ai_request(request_id)
    if row is None or row["kind"] == "again" or row["class_id"] is None:
        raise HTTPException(404, "ื”ื‘ืงืฉื” ืœื ื ืžืฆืื”")
    _my_class(request, user, row["class_id"])
    return row


@router.get("/ai/queue")
def queue(request: Request, user: dict = Depends(need("class.teach"))) -> dict:
    """The AI requests of the classes the teacher teaches (a developer: every class): waiting, being processed, failed and today's done ones."""
    store, today = request.app.state.store, clock.school_day()
    classes = _teachable(request, user)
    ids = [c["id"] for c in classes]
    rows = store.ai_requests(class_ids=ids, kinds=["photo", "check"], states=["waiting", "processing", "failed", "done"]) if ids else []
    emails = {m["user_id"]: m["email"] for c in classes for m in reads.of(request).members(c["id"]) if m["user_id"]}
    rows = [r for r in rows if r["state"] != "done" or (r["done_at"] and clock.school_day(r["done_at"]) == today)]
    items = [view(request, r, staff=True, emails=emails) for r in rows]
    own = None if _developer(user) else {"allowance": TEACHER_ALLOWANCE, "used": store.ai_allowance_used(f"teacher:{user['email']}", today)}
    return {"waiting": sum(1 for r in rows if r["state"] == "waiting"), "items": items, "own": own,
            "classes": [{"id": c["id"], "name": c["name"], "ai_enabled": c["ai_enabled"], "daily": c["ai_daily_checks"],
                         "used": store.ai_allowance_used(f"class:{c['id']}", today)} for c in classes]}


@router.post("/ai/requests/{request_id}/send")
def send_request(request_id: str, request: Request, user: dict = Depends(need("class.teach"))) -> dict:
    row = _handle(request, user, request_id)
    return view(request, send(request, user, row), staff=True)


class Decline(BaseModel):
    note: str = Field(default="", max_length=500)


@router.post("/ai/requests/{request_id}/decline")
def decline(request_id: str, body: Decline, request: Request, user: dict = Depends(need("class.teach"))) -> dict:
    row = _handle(request, user, request_id)
    if row["state"] not in ("waiting", "failed"):
        raise HTTPException(409, "ื”ื‘ืงืฉื” ื›ื‘ืจ ื˜ื•ืคืœื”")
    saved = request.app.state.store.save_ai_request(request_id, state="declined", teacher_note=body.note.strip(), done_at=clock.now())
    _delete_photo(request.app, row)
    return view(request, saved, staff=True)


class Batch(BaseModel):
    scope: Literal["photos", "assignment"]
    class_id: int | None = None
    assignment_id: int | None = None


@router.post("/ai/batch")
def batch(body: Batch, request: Request, user: dict = Depends(need("class.teach"))) -> dict:
    """Send every waiting request of one scope, oldest first (the single help photos of the teacher's classes, or one assignment's checks). When the
    budget is short it sends what is left and the rest stays waiting."""
    store = request.app.state.store
    if body.scope == "assignment":
        if body.assignment_id is None:
            raise HTTPException(422, "ื—ืกืจื” ื”ืžืฉื™ืžื”")
        assignment = store.get_assignment(body.assignment_id)
        if assignment is None:
            raise HTTPException(404, "ื”ืžืฉื™ืžื” ืœื ื ืžืฆืื”")
        _my_class(request, user, assignment["class_id"])
        rows = store.ai_requests(assignment_id=body.assignment_id, kinds=["check"], states=["waiting"])
    else:
        ids = [c["id"] for c in _teachable(request, user) if body.class_id in (None, c["id"])]
        rows = store.ai_requests(class_ids=ids, kinds=["photo"], states=["waiting"]) if ids else []
    sent, short = 0, False
    for row in rows:
        try:
            send(request, user, row)
            sent += 1
        except HTTPException as exc:
            if exc.status_code == 429:
                short = True
                break
            if exc.status_code == 409:                               # the class's AI was switched off, or it was handled already: leave it
                continue
    return {"sent": sent, "left": len(rows) - sent, "short": short,
            "message": ("ื”ืžื›ืกื” ื”ื™ื•ืžื™ืช ื ื’ืžืจื”. ืžื” ืฉื ืฉืืจ ื™ืžืฉื™ืš ืœื—ื›ื•ืช ืœืžื—ืจ." if short else "")}


@router.post("/submissions/{submission_id}/ai-check", status_code=201)
def teacher_asks_for_check(submission_id: int, request: Request, user: dict = Depends(need("class.teach"))) -> dict:
    """The teacher asks for an AI check of a handed-in answer and it is sent at once (it counts against the class's budget)."""
    store = request.app.state.store
    sub = store.get_submission_by_id(submission_id)
    assignment = store.get_assignment(sub["assignment_id"]) if sub else None
    if sub is None or assignment is None or assignment["removed_at"]:
        raise HTTPException(404, "ื”ื”ื’ืฉื” ืœื ื ืžืฆืื”")
    _my_class(request, user, assignment["class_id"])
    if store.ai_requests(user_id=sub["user_id"], assignment_id=assignment["id"], kinds=["check"], states=OPEN):
        raise HTTPException(409, "ื›ื‘ืจ ื‘ื™ืงืฉื• ื‘ื“ื™ืงื” ืœื”ื’ืฉื” ื”ื–ื•")
    row = store.create_ai_request("check", sub["user_id"], assignment["class_id"], assignment_id=assignment["id"],
                                  snapshot=snapshot_of(request, assignment), created_at=clock.now())
    try:
        row = send(request, user, row)
    except HTTPException:
        store.save_ai_request(row["id"], state="cancelled")
        raise
    return view(request, row, staff=True)


def latest_check(store, assignment_id: int, user_id: str) -> dict | None:
    """The newest AI check that finished for an answer (the teacher's review is kept next to it and never replaces it)."""
    done = store.ai_requests(user_id=user_id, assignment_id=assignment_id, kinds=["check"], states=["done"])
    return done[-1] if done else None