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 
 
 
 
 
 
 
 
 
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from pathlib import Path
import re

# Clean BOM
for path in Path("app").rglob("*.py"):
    text = path.read_text(encoding="utf-8-sig")
    text = text.replace("\ufeff", "")
    path.write_text(text, encoding="utf-8")

Path("app/product").mkdir(parents=True, exist_ok=True)

# -----------------------------------------------------
# 1. Create source_viewer.py
# -----------------------------------------------------

Path("app/product/source_viewer.py").write_text(r"""
import json
import csv
from pathlib import Path
from typing import Dict, Any, List, Optional

from fastapi import HTTPException
from fastapi.responses import HTMLResponse

from app.core.config import settings


def safe_str(value) -> str:
    if value is None:
        return ""
    return str(value)


def html_escape(value: str) -> str:
    return (
        safe_str(value)
        .replace("&", "&")
        .replace("<", "&lt;")
        .replace(">", "&gt;")
        .replace('"', "&quot;")
    )


def normalize(value) -> str:
    return safe_str(value).strip().lower()


def get_processed_document_dir(document_id: str) -> Path:
    return Path(settings.PROCESSED_DIR) / document_id


def load_json_file(path: Path):
    try:
        return json.loads(path.read_text(encoding="utf-8"))
    except Exception:
        try:
            return json.loads(path.read_text(encoding="utf-8-sig"))
        except Exception:
            return None


def load_jsonl_file(path: Path) -> List[Dict[str, Any]]:
    rows = []

    try:
        lines = path.read_text(encoding="utf-8").splitlines()
    except Exception:
        try:
            lines = path.read_text(encoding="utf-8-sig").splitlines()
        except Exception:
            return rows

    for line in lines:
        line = line.strip()
        if not line:
            continue

        try:
            item = json.loads(line)
            if isinstance(item, dict):
                rows.append(item)
        except Exception:
            pass

    return rows


def load_csv_file(path: Path) -> List[Dict[str, Any]]:
    rows = []

    for enc in ["utf-8", "utf-8-sig"]:
        try:
            with path.open("r", encoding=enc, newline="") as f:
                reader = csv.DictReader(f)
                for row in reader:
                    rows.append(dict(row))
            return rows
        except Exception:
            rows = []

    return rows


def flatten_json_records(data) -> List[Dict[str, Any]]:
    records = []

    if isinstance(data, dict):
        for key in ["chunks", "results", "pages", "items", "documents", "data"]:
            if isinstance(data.get(key), list):
                for item in data[key]:
                    if isinstance(item, dict):
                        records.append(item)

        if not records:
            records.append(data)

    elif isinstance(data, list):
        for item in data:
            if isinstance(item, dict):
                records.append(item)

    return records


def collect_candidate_records(document_id: str) -> List[Dict[str, Any]]:
    doc_dir = get_processed_document_dir(document_id)
    processed_dir = Path(settings.PROCESSED_DIR)

    roots = []

    if doc_dir.exists():
        roots.append(doc_dir)

    if processed_dir.exists():
        roots.append(processed_dir)

    records = []
    seen_files = set()

    for root in roots:
        for path in root.rglob("*"):
            if not path.is_file():
                continue

            if path in seen_files:
                continue

            seen_files.add(path)

            suffix = path.suffix.lower()
            file_records = []

            if suffix == ".json":
                file_records = flatten_json_records(load_json_file(path))
            elif suffix == ".jsonl":
                file_records = load_jsonl_file(path)
            elif suffix == ".csv":
                file_records = load_csv_file(path)

            for record in file_records:
                enriched = dict(record)
                enriched["_source_file_path"] = str(path)
                records.append(enriched)

    return records


def value_from(record: Dict[str, Any], keys: List[str], default: str = "") -> str:
    for key in keys:
        if key in record and record[key] not in [None, ""]:
            return safe_str(record[key])

    metadata = record.get("metadata")

    if isinstance(metadata, dict):
        for key in keys:
            if key in metadata and metadata[key] not in [None, ""]:
                return safe_str(metadata[key])

    return default


def record_text(record: Dict[str, Any]) -> str:
    return value_from(
        record,
        [
            "text",
            "content",
            "chunk_text",
            "page_text",
            "cleaned_text",
            "raw_text",
            "body",
            "preview",
            "text_preview",
            "chunk_preview"
        ],
        ""
    )


def record_match_score(
    record: Dict[str, Any],
    source_id: str,
    page: Optional[str] = None,
    chunk_id: Optional[str] = None
) -> int:
    score = 0

    source_id_norm = normalize(source_id)
    page_norm = normalize(page)
    chunk_id_norm = normalize(chunk_id)

    candidate_source_values = [
        value_from(record, ["source_id", "citation_id", "id", "source"]),
        value_from(record, ["chunk_id", "chunk", "chunk_index", "chunk_number"]),
        value_from(record, ["page_id", "page_source_id"])
    ]

    candidate_page_values = [
        value_from(record, ["page", "page_number", "page_no", "page_index"])
    ]

    candidate_chunk_values = [
        value_from(record, ["chunk_id", "chunk", "chunk_index", "chunk_number", "id"])
    ]

    if source_id_norm:
        for value in candidate_source_values:
            value_norm = normalize(value)

            if value_norm == source_id_norm:
                score += 10
            elif source_id_norm in value_norm or value_norm in source_id_norm:
                score += 3

    if page_norm:
        for value in candidate_page_values:
            if normalize(value) == page_norm:
                score += 5

    if chunk_id_norm:
        for value in candidate_chunk_values:
            if normalize(value) == chunk_id_norm:
                score += 8

    if record_text(record):
        score += 1

    return score


def find_best_source_record(
    document_id: str,
    source_id: str,
    page: Optional[str] = None,
    chunk_id: Optional[str] = None
) -> Dict[str, Any]:
    records = collect_candidate_records(document_id)

    if not records:
        raise HTTPException(
            status_code=404,
            detail="No processed records found. Upload/index the document first."
        )

    scored = []

    for record in records:
        score = record_match_score(
            record=record,
            source_id=source_id,
            page=page,
            chunk_id=chunk_id
        )
        scored.append((score, record))

    scored.sort(key=lambda item: item[0], reverse=True)

    best_score, best_record = scored[0]

    if best_score <= 0:
        raise HTTPException(
            status_code=404,
            detail="Source record not found."
        )

    return best_record


def get_source_details(
    document_id: str,
    source_id: str,
    page: Optional[str] = None,
    chunk_id: Optional[str] = None
) -> Dict[str, Any]:
    record = find_best_source_record(
        document_id=document_id,
        source_id=source_id,
        page=page,
        chunk_id=chunk_id
    )

    document_name = value_from(
        record,
        ["document_name", "source_file_name", "file_name", "filename", "document_title"],
        "Selected document"
    )

    page_number = value_from(
        record,
        ["page", "page_number", "page_no", "page_index"],
        page or "Not available"
    )

    resolved_chunk_id = value_from(
        record,
        ["chunk_id", "chunk", "chunk_index", "chunk_number", "id"],
        chunk_id or source_id
    )

    text = record_text(record)

    return {
        "document_id": document_id,
        "source_id": source_id,
        "document_name": document_name,
        "page": page_number,
        "chunk_id": resolved_chunk_id,
        "text": text,
        "text_preview": text[:1200],
        "metadata": record,
        "source_file_path": record.get("_source_file_path")
    }


def get_source_html(
    document_id: str,
    source_id: str,
    page: Optional[str] = None,
    chunk_id: Optional[str] = None
) -> HTMLResponse:
    details = get_source_details(
        document_id=document_id,
        source_id=source_id,
        page=page,
        chunk_id=chunk_id
    )

    document_name = html_escape(details.get("document_name", "Selected document"))
    page_value = html_escape(details.get("page", "Not available"))
    chunk_value = html_escape(details.get("chunk_id", source_id))
    text_value = html_escape(details.get("text", "Source text not available."))
    metadata_value = html_escape(json.dumps(details.get("metadata", {}), indent=2, ensure_ascii=False))

    html = f'''
<!DOCTYPE html>
<html>
<head>
    <title>Source {html_escape(source_id)} - GraphResearcher</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <style>
        body {{
            font-family: Inter, Arial, sans-serif;
            background: #f8fafc;
            color: #0f172a;
            margin: 0;
            padding: 32px;
        }}

        .container {{
            max-width: 980px;
            margin: 0 auto;
        }}

        .card {{
            background: white;
            border: 1px solid #e5e7eb;
            border-radius: 18px;
            padding: 22px;
            margin-bottom: 18px;
            box-shadow: 0 1px 4px rgba(0,0,0,0.04);
        }}

        .pill {{
            display: inline-block;
            background: #eef2ff;
            color: #3730a3;
            padding: 6px 10px;
            border-radius: 999px;
            font-size: 13px;
            margin: 4px 5px 4px 0;
        }}

        pre {{
            white-space: pre-wrap;
            word-break: break-word;
            background: #0f172a;
            color: #e5e7eb;
            padding: 16px;
            border-radius: 14px;
            line-height: 1.55;
        }}

        .source-text {{
            white-space: pre-wrap;
            line-height: 1.75;
            font-size: 16px;
        }}

        a {{
            color: #2563eb;
            font-weight: 800;
            text-decoration: none;
        }}
    </style>
</head>

<body>
    <div class="container">
        <p><a href="/app">← Back to app</a></p>

        <div class="card">
            <h1>Source {html_escape(source_id)}</h1>
            <span class="pill">Document: {document_name}</span>
            <span class="pill">Page: {page_value}</span>
            <span class="pill">Chunk: {chunk_value}</span>
        </div>

        <div class="card">
            <h2>Evidence Text</h2>
            <div class="source-text">{text_value or "Source text not available."}</div>
        </div>

        <div class="card">
            <h2>Raw Metadata</h2>
            <pre>{metadata_value}</pre>
        </div>
    </div>
</body>
</html>
'''

    return HTMLResponse(content=html)
""", encoding="utf-8")


# -----------------------------------------------------
# 2. Patch main.py
# -----------------------------------------------------

main_path = Path("app/main.py")
main_text = main_path.read_text(encoding="utf-8-sig")
main_text = main_text.replace("\ufeff", "")

if "from app.product.source_viewer import" not in main_text:
    main_text = (
        "from app.product.source_viewer import get_source_details, get_source_html\n"
        + main_text
    )

if "# Source viewer endpoints" not in main_text:
    main_text += '''

# Source viewer endpoints

@app.get("/documents/{document_id}/sources/{source_id}")
def document_source_details(
    document_id: str,
    source_id: str,
    page: str = "",
    chunk_id: str = ""
):
    return get_source_details(
        document_id=document_id,
        source_id=source_id,
        page=page,
        chunk_id=chunk_id
    )


@app.get("/documents/{document_id}/sources/{source_id}/view", response_class=HTMLResponse)
def document_source_view(
    document_id: str,
    source_id: str,
    page: str = "",
    chunk_id: str = ""
):
    return get_source_html(
        document_id=document_id,
        source_id=source_id,
        page=page,
        chunk_id=chunk_id
    )
'''

main_path.write_text(main_text, encoding="utf-8")


# -----------------------------------------------------
# 3. Patch app UI button text only
# -----------------------------------------------------

hf_path = Path("app/deployment/hf_status.py")
ui_text = hf_path.read_text(encoding="utf-8-sig")
ui_text = ui_text.replace("\ufeff", "")

ui_text = ui_text.replace("View source details", "Open source details")

hf_path.write_text(ui_text, encoding="utf-8")

print("Fixed Phase 28 source viewer patch complete.")