File size: 3,739 Bytes
fdfe81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""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