Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(co-writer): preserve model selection HTTP errors

dajiaohuang4650bf5394d8
diff --git a/deeptutor/api/routers/co_writer.py b/deeptutor/api/routers/co_writer.py
index 60136e5d6..6d8a06b66 100644
--- a/deeptutor/api/routers/co_writer.py
+++ b/deeptutor/api/routers/co_writer.py
@@ -541,6 +541,8 @@ async def edit_text(request: EditRequest):
return result
+ except HTTPException:
+ raise
except Exception as e:
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@@ -592,6 +594,8 @@ async def auto_mark_text(request: AutoMarkRequest):
print_stats()
return result
+ except HTTPException:
+ raise
except Exception as e:
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
diff --git a/tests/api/test_co_writer.py b/tests/api/test_co_writer.py
index b435a907b..748fa8d45 100644
--- a/tests/api/test_co_writer.py
+++ b/tests/api/test_co_writer.py
@@ -168,6 +168,32 @@ def test_llm_selection_defaults_to_admin_active_model(monkeypatch):
assert co_writer_router._validated_llm_selection(None) is None
[email protected]("endpoint", ["edit", "automark"])
[email protected]("status", [403, 422])
+def test_edit_actions_preserve_model_selection_errors(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, endpoint: str, status: int
+) -> None:
+ """Expected model-selection failures keep their HTTP status and detail."""
+ monkeypatch.setattr(
+ "deeptutor.multi_user.context.get_current_user",
+ lambda: SimpleNamespace(is_admin=False),
+ )
+ monkeypatch.setattr(
+ "deeptutor.multi_user.model_access.has_capability_access", lambda _capability: False
+ )
+ client, _storage = _client(tmp_path, monkeypatch)
+ payload = {"text": "A passage", "instruction": "Rewrite"}
+ if status == 422:
+ payload["llm_selection"] = {"profile_id": "profile", "model_id": ""}
+ response = client.post(f"/documents/actions/{endpoint}", json=payload)
+ assert response.status_code == status
+ assert response.json()["detail"] == (
+ "No LLM model is assigned to your account. Please contact an administrator."
+ if status == 403
+ else "Invalid LLM selection: profile_id and model_id are required."
+ )
+
+
def test_llm_selection_uses_first_granted_model_for_ordinary_user(monkeypatch):
monkeypatch.setattr(
"deeptutor.multi_user.context.get_current_user",