File size: 15,207 Bytes
89111aa
51720d8
89111aa
 
 
 
 
 
 
 
 
51720d8
 
 
 
 
 
 
 
 
2205fd2
51720d8
89111aa
51720d8
 
 
c982278
89111aa
51720d8
 
c982278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51720d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c982278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51720d8
 
9c0bd3b
51720d8
 
 
 
 
 
 
 
 
 
 
 
9c0bd3b
 
 
 
 
 
 
 
 
89111aa
9c0bd3b
14a9b91
 
89111aa
9c0bd3b
 
89111aa
c982278
14a9b91
9c0bd3b
14a9b91
 
 
89111aa
 
 
14a9b91
89111aa
14a9b91
9c0bd3b
 
89111aa
9c0bd3b
89111aa
3dfd032
 
14a9b91
89111aa
9c0bd3b
89111aa
 
 
1022e34
14a9b91
1022e34
 
 
 
 
 
 
 
 
 
89111aa
 
 
 
1022e34
14a9b91
 
 
 
 
 
1022e34
14a9b91
89111aa
9c0bd3b
 
 
 
89111aa
 
 
 
9c0bd3b
 
14a9b91
9c0bd3b
 
14a9b91
2205fd2
1022e34
2205fd2
89111aa
14a9b91
 
1022e34
14a9b91
 
 
 
3dfd032
14a9b91
 
 
 
2205fd2
 
 
 
1022e34
 
 
 
2205fd2
1022e34
 
 
3dfd032
1022e34
 
3dfd032
 
1022e34
 
 
 
3dfd032
1022e34
 
 
3dfd032
1022e34
 
 
 
 
89111aa
 
 
 
1022e34
 
89111aa
 
 
 
 
 
 
 
 
1022e34
 
 
 
 
 
89111aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c0bd3b
c982278
89111aa
 
 
 
c982278
 
 
89111aa
c982278
 
 
 
 
89111aa
c982278
9c0bd3b
 
51720d8
 
 
 
 
 
 
 
9c0bd3b
51720d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""Mock store + find-and-point pipeline for local UI iteration (MOCK_MODELS=1).

Drop any PDF into MOCK_PDF_DIR (default app/data/mock_pdfs/) and the whole
find-and-point UX runs over real, rendered pages of that PDF — no GPU, no
model downloads, no HF library sync. The same MockStore instance backs both
approaches, so the manual dropdown, the page viewer, navigation, the three
router branches (go_to_section / point_here / search→classify→circle), the
circle overlay and the candidate-pages strip all exercise the real wiring;
only the router's tool choice, the page/bbox picks and the page classification
are faked (by simple keyword heuristics, so the branches are predictable in
tests).

Nothing here imports torch / spaces / the model modules (those load CUDA at
import), so this module is safe to load on a laptop.
"""

from __future__ import annotations

import glob
import hashlib
import json
import os
import re
import time

from core.constants import MOCK_PDF_DIR
from core.pdf import page_count, page_size, render_pages
from core.sections import match_section
from core.store import slugify

# Canned section titles spread evenly over each PDF's pages, realistic enough
# to exercise "go to <section>" fuzzy matching in local tests.
MOCK_SECTION_TITLES = [
    "General Information",
    "Engine Mechanical System",
    "Engine Electrical System",
    "Fuel System",
    "Cooling System",
    "Transmission System",
    "Brake System — Bleeding and Adjustment",
    "Steering System",
    "Suspension System",
    "Body Electrical System",
]

MOCK_ANSWER = (
    "🧪 **Mock mode** — canned answer for UI iteration, not a real model "
    "response.\n\n"
    "1. Loosen the three retaining bolts in a star pattern (reassembly torque: "
    "24 N·m / 18 ft-lb).\n"
    "2. Withdraw the assembly and inspect the seal lip for wear or scoring.\n"
    "3. Refit in reverse order, checking clearances against the spec table.\n\n"
    "The pages this answer *would* be grounded in are shown on the right.\n\n"
    "({label})"
)


class MockStore:
    """A folder of PDFs exposed through the slice of the DocStore API that
    app.py and MockAskPipeline use. doc_id is the slug of the file name."""

    def __init__(self, pdf_dir: str = MOCK_PDF_DIR):
        self.pdf_dir = os.path.abspath(pdf_dir)
        os.makedirs(self.pdf_dir, exist_ok=True)

    def _docs(self) -> dict[str, dict]:
        """Re-scanned on every call so PDFs dropped in while the app is running
        appear after a 🔄 Sync library click (which just rebuilds the dropdown)."""
        docs: dict[str, dict] = {}
        for path in sorted(glob.glob(os.path.join(self.pdf_dir, "*.pdf"))):
            name = os.path.splitext(os.path.basename(path))[0]
            docs[slugify(name)] = {"name": name, "path": path}
        return docs

    def list_docs(self) -> list[dict]:
        return [
            {
                "doc_id": doc_id,
                "name": info["name"],
                "pages": page_count(info["path"]),
                "size_mb": os.path.getsize(info["path"]) / 1e6,
            }
            for doc_id, info in self._docs().items()
        ]

    def exists(self, doc_id: str) -> bool:
        return doc_id in self._docs()

    def pdf_path(self, doc_id: str) -> str | None:
        info = self._docs().get(doc_id)
        return info["path"] if info else None

    def sections(self, doc_id: str) -> list[dict]:
        """Canned sections spread evenly over the PDF's real pages — same
        shape as core.sections.sections_from_chunks."""
        info = self._docs().get(doc_id)
        if not info:
            return []
        n = page_count(info["path"])
        k = min(len(MOCK_SECTION_TITLES), n)
        starts = [round(i * n / k) + 1 for i in range(k)]
        return [
            {
                "title": MOCK_SECTION_TITLES[i],
                "page_start": starts[i],
                "page_end": (starts[i + 1] - 1) if i + 1 < k else n,
            }
            for i in range(k)
        ]



class MockAskPipeline:
    """Stateless mock matching the real ask pipelines' contracts: picks a
    deterministic spread of real pages and returns a canned answer."""

    def run(self, store: MockStore, question: str, doc_ids: list[str] | None, top_k: int):
        """Return (answer markdown, gallery [(image, caption)], page_refs
        [(doc_id, page)]) — same shape as the real ask pipelines."""
        question = (question or "").strip()
        if not question:
            raise ValueError("Please enter a question.")
        # Real questions take seconds on the GPU; the mock is instant, so the
        # loading indicator never shows. MOCK_DELAY (seconds) fakes that latency
        # for local UI work — e.g. MOCK_DELAY=2.
        time.sleep(float(os.environ.get("MOCK_DELAY", "0")))
        doc_id, info = self._pick_doc(store, doc_ids)
        pages = self._pick_pages(question, info["pages"], int(top_k))
        images = render_pages(store.pdf_path(doc_id), pages)
        labels = [f"{info['name']} — p.{p}" for p in pages]
        answer = MOCK_ANSWER.format(label=labels[0])
        gallery = [(img, f"{label} (mock)") for label, img in zip(labels, images)]
        page_refs = [(doc_id, p) for p in pages]
        return answer, gallery, page_refs

    def run_find(
        self,
        visual_store: MockStore,
        parsed_store: MockStore,
        request: str,
        doc_ids: list[str] | None,
        top_k: int,
        sections: list[dict],
        viewer: dict | None = None,
        history: list | None = None,
    ):
        """Yield the same event sequence as pipelines/agent_ask.py, with a
        keyword-driven stand-in for the agent's tool choice. Both stores are the
        one MockStore; history is ignored. MOCK_DELAY (seconds) paces events."""
        request = (request or "").strip()
        if not request:
            raise ValueError("Tell me what to find.")
        doc_id, info = self._pick_doc(visual_store, doc_ids)
        return self._find_events(
            visual_store, request, doc_id, info, int(top_k), sections or [], viewer or {}
        )

    def _find_events(self, store, request, doc_id, info, top_k, sections, viewer):
        delay = float(os.environ.get("MOCK_DELAY", "0"))
        cur = max(1, int(viewer.get("page") or 1))
        shown = [int(p) for p in (viewer.get("pages") or []) if str(p).isdigit()][:2] or [cur]
        prompt = self._mock_prompt(request, sections, shown)
        yield {"type": "status", "text": "Thinking…"}
        time.sleep(delay)

        kind, a, b = self._mock_route(request, sections)

        if kind == "go_to_section":
            yield self._trace(0, {"tool": "go_to_section", "section": b}, prompt)
            yield {"type": "step", "tool": "go_to_section", "title": b, "page": a}
            yield {"type": "done", "kind": "navigate", "nav": "section",
                   "page": a, "title": b}
            return

        if kind == "go_to_page":
            page = max(1, min(int(a), info["pages"]))
            yield self._trace(0, {"tool": "go_to_page", "page": page}, prompt)
            yield {"type": "step", "tool": "go_to_page", "page": page}
            yield {"type": "done", "kind": "navigate", "nav": "page",
                   "page": page, "title": f"Page {page}"}
            return

        if kind == "point_here":
            target = a
            yield self._trace(0, {"tool": "circle", "target": target}, prompt)
            yield from self._circle(store, doc_id, cur, target, delay)
            return

        # search → show the best page → circle the target on it (mirrors the
        # agent's search-then-circle), unless a magic target hits the give-up path
        target, query = a, b
        yield self._trace(0, {"tool": "search", "query": query}, prompt)
        yield {"type": "step", "tool": "search", "query": query}
        pages = self._pick_pages(request, info["pages"], top_k)
        images = render_pages(store.pdf_path(doc_id), pages)
        yield {
            "type": "tool_result",
            "tool": "search_docs",
            "gallery": [
                (img, f"{info['name']} — p.{p} (mock)")
                for p, img in zip(pages, images)
            ],
            "page_refs": [(doc_id, p) for p in pages],
        }
        yield {"type": "status", "text": f"Reading {len(pages)} candidate pages…"}
        time.sleep(delay)

        if any(w in target.lower() for w in ("xyzzy", "flux capacitor", "nonexistent")):
            msg = f"Couldn't find “{target}” in this manual."
            yield self._trace(1, {"tool": "done", "message": msg}, prompt)
            yield {"type": "done", "kind": "reply", "message": msg}
            return
        best = pages[0]
        yield {"type": "found", "page": best}
        yield self._trace(1, {"tool": "circle", "target": target}, prompt)
        yield from self._circle(store, doc_id, best, target, delay)

    def _circle(self, store, doc_id, page, target, delay):
        """Emit the circle step + terminal point event for a target on a page."""
        yield {"type": "step", "tool": "circle", "target": target, "page": page}
        yield {"type": "status", "text": "Pinning it down…"}
        time.sleep(delay)
        box = self._mock_box(store, doc_id, page, target)
        yield {"type": "done", "kind": "point", "found": True,
               "target": target, "page": page, "bbox": box,
               "ground_raw": f"(mock) grounding for {target!r}{box}"}

    @staticmethod
    def _trace(step: int, tool: dict, prompt: str = "") -> dict:
        """A mock diagnostic 'trace' event mirroring agent_ask's: the prompt fed
        in, the raw model reply (here just the tool JSON) + the parsed tool, for
        the UI trace view."""
        return {"type": "trace", "step": step, "tool": tool,
                "raw": json.dumps(tool, separators=(",", ":")), "prompt": prompt}

    @staticmethod
    def _mock_prompt(request: str, sections: list[dict], shown: list[int]) -> str:
        """A representative stand-in for the rendered chat prompt, so the
        Diagnostics 'prompt' view is exercisable in MOCK_MODELS=1. Not the real
        template — just the same shape (system rules + the on-screen pages +
        TOC + request)."""
        toc = "\n".join(
            f"{i + 1}. {s['title']} (p.{s.get('page') or s.get('page_start')})"
            for i, s in enumerate(sections)
        ) or "(none)"
        where = " and ".join(f"p.{p}" for p in shown) or "(no page open)"
        return (
            "<|im_start|>system\n(mock) You FIND the right page and POINT at "
            "things — reply with ONE tool JSON, no prose.<|im_end|>\n"
            f"<|im_start|>user\nCURRENTLY ON SCREEN — {where} (mock text omitted)\n\n"
            f"TABLE OF CONTENTS:\n{toc}\n\n"
            f"The mechanic said: {request!r}\n"
            "Choose ONE tool and reply with ONLY its JSON object."
            "<|im_end|>\n<|im_start|>assistant\n"
        )

    @staticmethod
    def _mock_route(request: str, sections: list[dict]):
        """Fake the LLM router with keyword heuristics. Returns one of:
        ("go_to_section", page, title) / ("go_to_page", page, None) /
        ("point_here", target, None) / ("search", target, query)."""
        r = request.lower()
        nav_verb = any(
            v in r for v in ("go to", "take me", "open", "bring up", "pull up",
                             "navigate", "jump to")
        )
        is_circle = any(
            v in r for v in ("circle", "point", "highlight", "mark", "show me where")
        )
        here = any(v in r for v in ("here", "this page", "this", "current"))
        # A bare page number that the client's strict nav regex didn't catch
        # (extra words around it) → exercise the go_to_page tool, like the agent
        # reading a page number off an index.
        pm = re.search(r"\bp(?:age|g|\.)?\s*(\d+)\b", r)
        if pm and (nav_verb or "index" in r or "contents" in r):
            return "go_to_page", int(pm.group(1)), None
        secs = [{"title": o["title"], "page_start": o["page"]} for o in sections]
        best = match_section(request, secs) if secs else None

        if (nav_verb or "section" in r or "chapter" in r) and best and best["score"] >= 0.4:
            return "go_to_section", best["page"], best["title"]
        target = MockAskPipeline._clean_target(request)
        if is_circle and here:
            return "point_here", target, None
        if is_circle:
            return "search", target, request
        if best and best["score"] >= 0.6:
            return "go_to_section", best["page"], best["title"]
        return "search", target, request

    @staticmethod
    def _clean_target(request: str) -> str:
        r = re.sub(
            r"^(?:can you |please )?(?:circle|point (?:at|to)|highlight|mark|"
            r"show me where)\s+",
            "", request.strip(), flags=re.I,
        )
        r = re.sub(r"^(the |a |an )", "", r, flags=re.I)
        r = re.sub(r"\b(?:on |in )?(?:this|the current) page\b", "", r, flags=re.I)
        r = re.sub(r"\bhere\b", "", r, flags=re.I)
        r = re.sub(r"\s+(?:is|are)(?:\s+located)?$", "", r, flags=re.I)
        return " ".join(r.split()) or request

    @staticmethod
    def _mock_box(store: MockStore, doc_id: str, page: int, target: str):
        """A deterministic, target-dependent bbox in rendered-page pixels;
        None for ~1 in 8 targets so the "located but not pinpointed" path is
        exercised too."""
        path = store.pdf_path(doc_id)
        if not path:
            return None
        seed = int(hashlib.sha1(target.encode()).hexdigest(), 16)
        if seed % 8 == 7:
            return None
        w, h = page_size(path, page)
        x1 = w * (0.1 + (seed % 5) * 0.1)
        y1 = h * (0.15 + (seed // 5 % 5) * 0.12)
        return [round(x1), round(y1), round(x1 + w * 0.3), round(y1 + h * 0.18)]

    @staticmethod
    def _pick_doc(store: MockStore, doc_ids: list[str] | None):
        docs = {d["doc_id"]: d for d in store.list_docs()}
        if not docs:
            raise ValueError(
                f"No PDFs in the mock library yet — drop one into {store.pdf_dir}."
            )
        doc_id = (doc_ids or list(docs))[0]
        if doc_id not in docs:
            raise ValueError("That manual isn't in the mock library.")
        return doc_id, docs[doc_id]

    @staticmethod
    def _pick_pages(question: str, n_pages: int, top_k: int) -> list[int]:
        """A deterministic, question-dependent spread of 1-based pages, so
        different questions cite different pages (nice for clicking through the
        viewer) while the same question is stable across reloads."""
        k = max(1, min(top_k, n_pages))
        seed = int(hashlib.sha1(question.encode()).hexdigest(), 16)
        start = seed % n_pages  # 0-based anchor
        step = max(1, n_pages // k)
        out: list[int] = []
        for i in range(k):
            page = (start + i * step) % n_pages + 1  # back to 1-based
            if page not in out:
                out.append(page)
        return out