mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 17:01:07 +08:00
Partners stop being a deployment-wide admin resource and become per-user: three nested rights (manage / use / neither) replace the blanket admin gate on the router, each member can build and own their own, and every person's conversations with a partner stay private to their account. Grants keep working and now offer only admin-managed partners. Channel runtime state moves from one directory per channel to one per partner — copied, not moved, and only when its owner is unambiguous — so two partners can finally run the same channel. Around that: a knowledge base can track GitHub repositories and re-sync their Markdown (#903); Antigravity CLI joins My Agents now that Gemini CLI is retired (#828); personal WeChat signs in from a QR code drawn in the browser instead of the server log (#951); a chat account can be linked to a DeepTutor account with /link; containers re-apply the extras they declare instead of losing them at compose down (#762); and `deeptutor doctor` answers whether a workspace is ready to run (#959). Reliability, largely from community PRs: embeddings survive a rate-limited provider through key rotation, a retry budget and a global throttle (#962); MinerU parsing and code execution work on Windows (#844, #964); indexing fails bounded instead of hanging (#946); MinerU page-layout blocks stop being indexed as content (#949); voice uploads carry a MIME the transcription API accepts (#945); and reasoning effort can be overridden per conversation (#641). Release plumbing: notes in assets/releases/ver1-5-17.md, v1.5.13 archived, __version__ 1.5.17, README Releases row and all eleven translations synced. Two defects found by the release gate and fixed here: - Six partner routes — the four channel-onboarding endpoints and both WeChat QR endpoints — kept the _auth-only mount they had inherited from the blanket admin gate, so any signed-in account could start a QR login for someone else's partner and write a bot token into its channel config. All six now declare _MANAGEABLE, and a sweep test asserts no {partner_id} route is left ungated. - A configured api_key became `str | list[str]` when key rotation landed, but ten call sites pass it somewhere that takes one key; a list would have rendered into an Authorization header verbatim. keypool.primary_api_key reduces at each boundary. Gates: pre-commit (all 15 hooks), ruff check + format, pytest 5276 passed, web 605 node tests, docs build.
247 lines
8.8 KiB
Python
247 lines
8.8 KiB
Python
"""Public, stable views of pending mastery questions.
|
||
|
||
The persisted :class:`~deeptutor.learning.models.PendingQuestion` contains the
|
||
server-only expected answer. This module projects it into the smaller contract
|
||
that is safe to give to the tutor model and interactive clients. It also owns
|
||
the pure multiple-choice translations shared by registration, presentation,
|
||
and grading, so all three boundaries use the same immutable label/body map.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from collections.abc import Iterable
|
||
from dataclasses import dataclass
|
||
import re
|
||
from typing import TYPE_CHECKING, Any
|
||
|
||
if TYPE_CHECKING:
|
||
from deeptutor.learning.models import PendingQuestion
|
||
|
||
|
||
OPTION_PREFIX_RE = re.compile(r"^\s*([A-Z])\s*[.::、))-]\s*(.+)$", re.IGNORECASE | re.DOTALL)
|
||
|
||
|
||
def positional_label(index: int) -> str:
|
||
"""The label an option carries by its position: A, B, C, … then 27, 28, …"""
|
||
return chr(ord("A") + index) if index < 26 else str(index + 1)
|
||
|
||
|
||
def _option_texts(options: list[str]) -> list[str]:
|
||
return [text for text in (str(raw or "").strip() for raw in options) if text]
|
||
|
||
|
||
def _split_labelled(texts: list[str]) -> tuple[list[str], list[str]] | None:
|
||
"""``(labels, bodies)`` when every option carries a single-letter prefix."""
|
||
labels: list[str] = []
|
||
bodies: list[str] = []
|
||
for text in texts:
|
||
if len(text) == 1 and text.isalnum():
|
||
# Legacy rows persisted the bare labels themselves.
|
||
labels.append(text.upper())
|
||
bodies.append(text)
|
||
continue
|
||
match = OPTION_PREFIX_RE.match(text)
|
||
if match is None:
|
||
return None
|
||
labels.append(match.group(1).upper())
|
||
bodies.append(match.group(2).strip())
|
||
return labels, bodies
|
||
|
||
|
||
def option_label_intent(options: list[str]) -> list[str] | None:
|
||
"""The A/B/C labels the caller *meant* to give, or ``None`` if unlabelled.
|
||
|
||
"Meant to" is decided by two things together: every option carries a
|
||
single-letter prefix, and the first one is ``A``. A leading letter alone is
|
||
not evidence — ``"x - 1 = 0"`` matches the prefix pattern and would
|
||
otherwise register as option ``X`` with the body ``"1 = 0"``, which is how
|
||
a maths question ended up with mislabelled and (once two options collided
|
||
on one letter) duplicated choices.
|
||
|
||
The returned labels may still be malformed — repeated or skipping a letter.
|
||
:func:`parse_options` reads those positionally so nothing is lost, and
|
||
registration rejects them so the model fixes the question instead.
|
||
"""
|
||
split = _split_labelled(_option_texts(options))
|
||
if split is None:
|
||
return None
|
||
labels, _ = split
|
||
return labels if labels and labels[0] == "A" else None
|
||
|
||
|
||
def canonical_labels(count: int) -> set[str]:
|
||
"""The label set a well-formed *count*-option question must carry."""
|
||
return {positional_label(index) for index in range(count)}
|
||
|
||
|
||
def parse_options(options: list[str]) -> dict[str, str]:
|
||
"""Map persisted option strings to their stable ``{label: body}`` form."""
|
||
texts = _option_texts(options)
|
||
split = _split_labelled(texts)
|
||
if split is not None:
|
||
labels, bodies = split
|
||
if labels[:1] == ["A"] and set(labels) == canonical_labels(len(labels)):
|
||
return dict(zip(labels, bodies, strict=True))
|
||
return {positional_label(index): text for index, text in enumerate(texts)}
|
||
|
||
|
||
def has_option_bodies(options: dict[str, str]) -> bool:
|
||
"""Whether a choice map holds real answer text, not only A/B/C labels."""
|
||
return len(options) >= 2 and all(
|
||
value.strip() and value.strip().upper() != key.upper() for key, value in options.items()
|
||
)
|
||
|
||
|
||
def format_options(options: dict[str, str]) -> list[str]:
|
||
"""Render a choice map as canonical, persistable ``"label: body"`` strings."""
|
||
return [f"{label}: {body}" for label, body in options.items()]
|
||
|
||
|
||
def resolve_answer(answer: str, options: dict[str, str]) -> str:
|
||
"""Resolve a label, labelled option, or unique body to its stable label."""
|
||
candidate = str(answer or "").strip()
|
||
if not candidate:
|
||
return ""
|
||
|
||
key = candidate.upper()
|
||
if key in options:
|
||
return key
|
||
|
||
prefix_match = OPTION_PREFIX_RE.match(candidate)
|
||
if prefix_match and prefix_match.group(1).upper() in options:
|
||
return prefix_match.group(1).upper()
|
||
|
||
needle = candidate.casefold()
|
||
exact = [label for label, text in options.items() if text.casefold() == needle]
|
||
if len(exact) == 1:
|
||
return exact[0]
|
||
contained = [label for label, text in options.items() if needle in text.casefold()]
|
||
return contained[0] if len(contained) == 1 else ""
|
||
|
||
|
||
def _squeezed(value: str) -> str:
|
||
"""Case-folded with all whitespace removed, for comparing formulas."""
|
||
return "".join(str(value or "").split()).casefold()
|
||
|
||
|
||
def _mentioned_labels(text: str, labels: Iterable[str]) -> list[str]:
|
||
"""Labels named in *text* as standalone tokens, not inside another word.
|
||
|
||
``\\b`` is useless here: Chinese is word-character too, so ``"选C"`` has no
|
||
word boundary before the ``C``. The guard is therefore "not glued to
|
||
another latin letter or digit", which accepts ``选C`` / ``答案是 C`` /
|
||
``C。`` and rejects the ``C`` inside ``ABC``.
|
||
"""
|
||
return [
|
||
label
|
||
for label in labels
|
||
if re.search(rf"(?<![0-9A-Za-z]){re.escape(label)}(?![0-9A-Za-z])", text, re.IGNORECASE)
|
||
]
|
||
|
||
|
||
def resolve_choice_submission(answer: str, options: dict[str, str]) -> str:
|
||
"""Resolve a learner submission to the option label it picked, or ``""``.
|
||
|
||
A learner typing into the composer instead of tapping the card writes
|
||
``"选C"``, ``"答案是 C"`` or the option body itself — none of which the
|
||
label-only comparison could read, so a correct answer was graded wrong.
|
||
Every form that identifies exactly ONE option is accepted; anything
|
||
ambiguous (two labels named, a body fragment matching several) resolves to
|
||
nothing, which the caller must treat as "unreadable", never as wrong.
|
||
|
||
Registration stays separately forgiving of a model-supplied body fragment
|
||
through :func:`resolve_answer`.
|
||
"""
|
||
candidate = str(answer or "").strip()
|
||
if not candidate:
|
||
return ""
|
||
key = candidate.upper()
|
||
if key in options:
|
||
return key
|
||
prefix_match = OPTION_PREFIX_RE.match(candidate)
|
||
if prefix_match and prefix_match.group(1).upper() in options:
|
||
return prefix_match.group(1).upper()
|
||
needle = _squeezed(candidate)
|
||
exact = [label for label, body in options.items() if _squeezed(body) == needle]
|
||
if len(exact) == 1:
|
||
return exact[0]
|
||
mentioned = _mentioned_labels(candidate, options)
|
||
return mentioned[0] if len(mentioned) == 1 else ""
|
||
|
||
|
||
@dataclass(frozen=True, slots=True)
|
||
class PublicPendingOption:
|
||
"""One learner-visible option; ``id`` and ``label`` are intentionally stable."""
|
||
|
||
id: str
|
||
label: str
|
||
body: str
|
||
|
||
def to_dict(self) -> dict[str, str]:
|
||
return {"id": self.id, "label": self.label, "body": self.body}
|
||
|
||
def to_ask_user_dict(self) -> dict[str, str]:
|
||
return {"label": self.label, "description": self.body}
|
||
|
||
|
||
@dataclass(frozen=True, slots=True)
|
||
class PublicPendingQuestion:
|
||
"""Learner-visible pending state, deliberately excluding the answer key."""
|
||
|
||
question_id: str
|
||
prompt: str
|
||
question_type: str
|
||
options: tuple[PublicPendingOption, ...] = ()
|
||
|
||
def to_dict(self) -> dict[str, Any]:
|
||
return {
|
||
"question_id": self.question_id,
|
||
"prompt": self.prompt,
|
||
"question_type": self.question_type,
|
||
"options": [option.to_dict() for option in self.options],
|
||
}
|
||
|
||
def to_ask_user_dict(self) -> dict[str, Any]:
|
||
return {
|
||
"id": self.question_id,
|
||
"prompt": self.prompt,
|
||
"options": [option.to_ask_user_dict() for option in self.options],
|
||
"multi_select": False,
|
||
"allow_free_text": True,
|
||
}
|
||
|
||
|
||
def public_pending_question(pending: PendingQuestion) -> PublicPendingQuestion:
|
||
"""Project persisted pending state without exposing ``expected_answer``."""
|
||
choice_map = parse_options(list(pending.options or []))
|
||
options = (
|
||
tuple(
|
||
PublicPendingOption(id=label, label=label, body=body)
|
||
for label, body in choice_map.items()
|
||
)
|
||
if pending.question_type == "choice"
|
||
else ()
|
||
)
|
||
return PublicPendingQuestion(
|
||
question_id=pending.question_id,
|
||
prompt=pending.prompt,
|
||
question_type=pending.question_type,
|
||
options=options,
|
||
)
|
||
|
||
|
||
__all__ = [
|
||
"OPTION_PREFIX_RE",
|
||
"canonical_labels",
|
||
"PublicPendingOption",
|
||
"PublicPendingQuestion",
|
||
"format_options",
|
||
"has_option_bodies",
|
||
"option_label_intent",
|
||
"parse_options",
|
||
"positional_label",
|
||
"public_pending_question",
|
||
"resolve_answer",
|
||
"resolve_choice_submission",
|
||
]
|