Skip to content

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

Merge remote-tracking branch 'origin/pr-1503' into codex/merge-pr-1503

Bingxi Zhao (Frank)7d330f043c78
diff --cc deeptutor/services/session/turns/executor.py
index 4548ea41c,df58b961f..9ff3d642f
--- a/deeptutor/services/session/turns/executor.py
+++ b/deeptutor/services/session/turns/executor.py
@@@ -345,58 -330,58 +346,109 @@@ class TurnExecutor
from deeptutor.utils.document_extractor import extract_documents_from_records
+ # Keep the built-in extractor as a graceful fallback, then replace
+ # every PDF's result with the configured parser output. Running the
+ # configured parser regardless of a native text layer is what lets
+ # OCR/layout engines see scanned pages and figures.
document_texts, attachment_records = extract_documents_from_records(attachment_records)
+ pdf_records = [
+ record
+ for record in attachment_records
+ if str(record.get("filename") or "").lower().endswith(".pdf")
+ ]
+ pdf_names = {
+ str(record.get("id") or ""): str(record.get("filename") or "PDF attachment")
+ for record in pdf_records
+ }
+ from deeptutor.services.session.attachment_parsing import parse_chat_pdf_attachments
+
+ loop = asyncio.get_running_loop()
+
+ def _attachment_progress(attachment_id: str, phase: str, message: str) -> None:
+ loop.call_soon_threadsafe(
+ lambda: asyncio.create_task(
+ self._publish_live_event(
+ execution,
+ StreamEvent(
+ type=StreamEventType.PROGRESS,
+ source="attachment_parsing",
+ stage=phase,
+ content=message,
+ metadata={
+ "attachment_id": attachment_id,
+ "filename": pdf_names.get(attachment_id, "PDF attachment"),
+ "phase": phase,
+ },
+ ),
+ )
+ )
+ )
+
+ for record in pdf_records:
+ _attachment_progress(
+ str(record.get("id") or ""),
+ "received",
+ "PDF uploaded to DeepTutor",
+ )
+
+ attachment_records, document_texts = await parse_chat_pdf_attachments(
+ attachment_records,
+ attachment_store=attachment_store,
+ session_id=session_id,
+ document_texts=document_texts,
+ on_progress=_attachment_progress,
+ )
+
+ # Immersive reading: the page the user is currently looking at
+ # rides along as image attachments, so a vision model sees what
+ # the question is about. A drawn page (vector diagram — common in
+ # slide-exported PDFs) leads with a full-page render, since its
+ # figures only exist as vectors and the embedded rasters alone
+ # would be meaningless fragments; the page's embedded figures
+ # follow. Total stays within READING_VIEWPORT_MAX_IMAGES (the
+ # render takes one slot). Keyed on the resolved workspace mode —
+ # the web composer sends capability "chat" with workspace_mode
+ # "immersive_reading", and the mode resolver falls back to the
+ # capability name for direct callers, so both shapes land here.
+ if workspace_mode == "immersive_reading":
+ attachment_records.extend(
+ _reading_viewport_image_attachments(
+ _reading_material_id(payload.get("reading_material_id")),
+ _reading_viewport(payload.get("reading_viewport")),
+ )
+ )
+
+ # Embedded images harvested out of office documents during
+ # extraction arrive with base64 and no URL — host them too so
+ # message previews survive the base64 pruning below and the
+ # reader pane can display them.
+ for record in attachment_records:
+ if record.get("url") or not record.get("base64"):
+ continue
+ try:
+ raw_bytes = _b64.b64decode(record["base64"], validate=False)
+ except Exception as exc:
+ logger.warning(
+ "skipping embedded-image upload for %r: invalid base64 (%s)",
+ record.get("filename"),
+ exc,
+ )
+ continue
+ try:
+ record["url"] = await attachment_store.put(
+ session_id=session_id,
+ attachment_id=record.get("id") or _uuid.uuid4().hex[:12],
+ filename=record.get("filename", "") or "image",
+ data=raw_bytes,
+ mime_type=record.get("mime_type", "") or "",
+ )
+ except Exception as exc:
+ logger.warning(
+ "attachment store rejected embedded image %r: %s",
+ record.get("filename"),
+ exc,
+ )
+
attachments = [
Attachment(
type=r.get("type", "file"),