Files
DeepTutor/deeptutor/capabilities/setup/access.py
Bingxi Zhao (Frank) 7c6bcfaef7 release: v1.5.14
Immersive Reading, self-configuration, and a two-way IMA library.

- Immersive Reading: a new capability that opens a document beside the
  thread. New reading engine (PDF pages / slides / page-sized sections),
  five reading tools, a deterministic locate pre-pass that grounds the
  turn without a second LLM call, /api/v1/reading, and a reader pane with
  selection, highlights, notes, and export to an annotated PDF or Markdown.
- Setup capability: inspect the install, apply a setting, install a
  parsing engine, or request a credential from a chat turn. New settings
  spec table; a candidate value is probed before it is committed, and API
  keys never enter the model's context.
- Tencent IMA: pipeline split into transport/envelope/models/sources/
  notes/media/inventory, an additive capability with five tools, and
  connected libraries that can finally be enumerated (bounded listings
  are reported as a lower bound).
- Retrieval: supports_rag_retrieval() replaces is_connected_kb() for the
  "can I sweep this?" question, so linked / lightrag_server / ima sources
  stop being silently dropped from every book.
- Notebooks: their own console at /notebook, per-notebook locks and atomic
  writes, copy / move / export routes, damaged files surfaced instead of
  dropped, and renaming a record no longer clears its knowledge-base link.
- Settings persistence: one lock and one atomic replace shared by every
  writer of interface.json; the PUT endpoints stop writing the
  defaults-merged view back to disk.

Release notes: assets/releases/ver1-5-14.md
2026-08-19 17:34:52 +08:00

105 lines
3.9 KiB
Python

"""Who may let the setup agent change what.
Two rules, both inherited from where the settings files already live rather
than invented here:
*Personal* rows are the ones :class:`~deeptutor.services.path_service.PathService`
already resolves per user — a write lands in the caller's own
``settings/interface.json`` and is invisible to everyone else. Any signed-in
person may therefore change their own.
*Global* rows are the shared deployment settings under
``data/user/settings`` (``get_runtime_settings_dir`` does not go through the
per-user path service). Changing one changes it for every account on the
install, which is exactly the boundary
:func:`deeptutor.api.routers.settings._require_settings_admin` already guards —
so the capability requires the same administrator role rather than inventing a
softer rule for the chat surface than the settings page enforces.
A **partner is refused outright**, for either scope. A partner is a synthetic
user whose owner is a real account: its turns resolve ownership to that person
(:func:`~deeptutor.multi_user.paths.get_owner_path_service`), so honouring a
configuration request from an IM-facing companion would mean letting whoever is
chatting with it reconfigure the owner's DeepTutor — including, if the owner is
an administrator, the whole deployment.
"""
from __future__ import annotations
from dataclasses import dataclass
from deeptutor.services.config.settings_spec import Scope
@dataclass(frozen=True, slots=True)
class AccessDecision:
"""Whether a write may proceed, and what to tell the model when it may not.
``reason`` is written for the model to relay to the user, so it names the
human remedy ("ask an administrator") rather than the internal rule.
"""
allowed: bool
reason: str = ""
def _is_partner_turn() -> bool:
try:
from deeptutor.multi_user.context import get_current_user
from deeptutor.services.partners.scope import is_partner_user_id
return bool(is_partner_user_id(get_current_user().id))
except Exception: # noqa: BLE001 - no multi-user layer configured
return False
def _is_admin() -> bool:
"""Whether the caller may change deployment-wide settings.
A missing multi-user layer means a single-user install, whose only user *is*
the administrator — so ImportError grants. Any other failure is narrowed to
a refusal: if identity cannot be resolved, granting administrator rights is
the wrong way to be wrong, and the caller still keeps their personal
preferences either way.
"""
try:
from deeptutor.multi_user.context import get_current_user
except ImportError:
return True
try:
return bool(get_current_user().is_admin)
except Exception: # noqa: BLE001 - unresolvable identity: refuse, do not grant
return False
def can_write(scope: Scope) -> AccessDecision:
"""Decide whether the current turn may write a row of the given scope."""
if _is_partner_turn():
return AccessDecision(
allowed=False,
reason=(
"Configuration cannot be changed from a partner conversation. "
"The owner can make this change in DeepTutor directly."
),
)
if scope == "personal":
return AccessDecision(allowed=True)
if _is_admin():
return AccessDecision(allowed=True)
return AccessDecision(
allowed=False,
reason=(
"This is a deployment-wide setting that applies to every account, so only an "
"administrator can change it. Personal preferences (interface language, reply "
"language, theme) can still be changed here."
),
)
def writable_scopes() -> tuple[Scope, ...]:
"""Scopes the current turn may write — used to describe the surface up front."""
return tuple(scope for scope in ("personal", "global") if can_write(scope).allowed)
__all__ = ["AccessDecision", "can_write", "writable_scopes"]