"""Non-tool helper for scanning slide text + notes for must-flag markers. ``find_text_markers_pure`` is called by: - the ``@function_tool find_text_markers`` in ``agents/tools.py`` - the runner's pre-scan safety-net loop in ``agents/runner.py`` """ from __future__ import annotations from dataclasses import dataclass from schemas.parsed import ParsedDeck _CONTEXT_WINDOW = 80 # chars either side of the match @dataclass class Hit: """One case-insensitive substring match of a marker term in a slide. Attributes ---------- term: The marker string that was searched for (original casing from the caller). offset: Character offset of the match within *source_text*. context_window: Up-to-160-char excerpt centred on the match (the match itself is included). source: ``"shape"`` when the hit came from a shape's text, ``"notes"`` for speaker notes. shape_id: Shape identifier (e.g. ``"s7.shape8"``); ``None`` for notes hits. """ term: str offset: int context_window: str source: str # "shape" | "notes" shape_id: str | None = None def find_text_markers_pure( deck: ParsedDeck, slide_n: int, markers: list[str], ) -> list[Hit]: """Case-insensitive substring scan of *slide_n* text + notes for *markers*. Parameters ---------- deck: Fully parsed deck (1-indexed slides). slide_n: 1-indexed slide number. markers: List of search terms (case-insensitive substring match). Returns ------- list[Hit] All hits found; order is shapes first (in declaration order), then notes. Multiple hits per marker per source are all returned. Raises ------ ValueError If *slide_n* is out of range (< 1 or > deck.slide_count). """ if slide_n < 1 or slide_n > deck.slide_count: raise ValueError( f"slide_n={slide_n} out of range; deck has {deck.slide_count} slide(s)" ) slide = deck.slides[slide_n - 1] hits: list[Hit] = [] # --- scan shapes --- for shape in slide.shapes: text = shape.text text_lower = text.lower() for marker in markers: needle = marker.lower() start = 0 while True: pos = text_lower.find(needle, start) if pos == -1: break ctx_start = max(0, pos - _CONTEXT_WINDOW) ctx_end = min(len(text), pos + len(needle) + _CONTEXT_WINDOW) hits.append( Hit( term=marker, offset=pos, context_window=text[ctx_start:ctx_end], source="shape", shape_id=shape.shape_id, ) ) start = pos + len(needle) # --- scan notes --- notes = slide.notes_text if notes: notes_lower = notes.lower() for marker in markers: needle = marker.lower() start = 0 while True: pos = notes_lower.find(needle, start) if pos == -1: break ctx_start = max(0, pos - _CONTEXT_WINDOW) ctx_end = min(len(notes), pos + len(needle) + _CONTEXT_WINDOW) hits.append( Hit( term=marker, offset=pos, context_window=notes[ctx_start:ctx_end], source="notes", shape_id=None, ) ) start = pos + len(needle) return hits