File size: 37,249 Bytes
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c9c028
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
167611e
 
a35224c
 
 
 
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9124e72
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9124e72
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c9c028
 
e1f6f2b
 
 
 
 
 
3c9c028
 
 
e1f6f2b
3c9c028
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390c120
 
 
e1f6f2b
 
 
 
 
 
390c120
e1f6f2b
390c120
 
 
3559e28
 
 
 
 
390c120
 
 
 
3559e28
390c120
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e479746
 
 
 
 
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9124e72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a86b4
9124e72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9124e72
e1f6f2b
 
 
 
 
 
6cecbb6
e1f6f2b
 
 
6cecbb6
 
 
e1f6f2b
 
 
 
 
 
 
f1196cf
e1f6f2b
f1196cf
 
 
 
 
 
 
6cecbb6
 
 
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cecbb6
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e479746
 
3559e28
e479746
 
 
 
e1f6f2b
3559e28
 
 
 
 
 
 
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a35224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cecbb6
e1f6f2b
 
 
390c120
167611e
9124e72
50a86b4
390c120
 
167611e
3559e28
e1f6f2b
167611e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a86b4
e1f6f2b
 
167611e
 
 
 
 
 
 
 
 
 
390c120
 
9124e72
 
e1f6f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
9124e72
 
50a86b4
 
 
 
 
 
 
390c120
 
 
 
167611e
 
 
 
 
 
e1f6f2b
 
50a86b4
 
 
 
 
 
 
 
 
 
 
 
 
 
e1f6f2b
 
 
50a86b4
 
 
 
 
 
 
 
 
 
e479746
 
 
 
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
"""
GraphoLab core — Agentic Document Analysis.

Provides:
  - create_forensic_agent()   build a LangChain AgentExecutor with all GraphoLab tools
  - agent_stream()            streaming generator for Gradio / FastAPI
  - SUGGESTED_PROMPTS         pre-formatted prompt templates for the UI
  - AGENT_TOOLS_NAMES         list of tool names for status endpoint

The agent uses qwen3 (via Ollama) by default — qwen3 has excellent tool-calling
support. The system prompt instructs the model to respond always in Italian and
to use file paths injected into the user message as [file: /path/to/file].
"""

from __future__ import annotations

import shutil
import tempfile
import threading
from pathlib import Path
from typing import Generator, Any, Optional

import numpy as np
from PIL import Image

# ──────────────────────────────────────────────────────────────────────────────
# Configuration
# ──────────────────────────────────────────────────────────────────────────────

AGENT_MODEL = "qwen3:4b"

_ROOT = Path(__file__).parent.parent
SIGNET_WEIGHTS = _ROOT / "models" / "signet.pth"
WRITER_SAMPLES_DIR = _ROOT / "data" / "samples"

FORENSIC_SYSTEM_PROMPT = (
    "Sei un consulente forense specializzato in grafologia e analisi documentale.\n"
    "Hai accesso a strumenti specializzati per analizzare documenti, scritture a mano e firme.\n"
    "Rispondi SEMPRE in italiano, in modo professionale e dettagliato.\n"
    "Usa gli strumenti nella sequenza più logica per rispondere alla richiesta dell'utente.\n"
    "Quando l'utente allega dei file, i loro percorsi ti vengono forniti nel messaggio "
    "nel formato [file: /percorso/al/file]. Usali come argomenti degli strumenti.\n"
    "Se la richiesta riguarda sia la trascrizione che altre analisi (NER, date, ecc.), "
    "trascrivi prima il testo e poi usa il testo risultante come input per gli altri strumenti.\n"
    "Quando uno strumento restituisce una tabella Markdown (righe con | ... |), "
    "includila SEMPRE integralmente nella risposta senza riscriverla come testo.\n"
    "Al termine di ogni risposta, fornisci un breve riepilogo delle analisi effettuate.\n"
    "IMPORTANTE: quando l'utente chiede la conformità ENFSI, la verifica ENFSI BPM, "
    "o usa il prompt 'Conformità ENFSI', devi SEMPRE chiamare il tool "
    "`verifica_conformita_enfsi` passando il percorso del PDF allegato. "
    "Non rispondere mai a queste richieste senza aver prima chiamato il tool."
)

SUGGESTED_PROMPTS = [
    {"label": "Trascrivi testo",
     "text": "Trascrivi il testo manoscritto nel documento allegato"},
    {"label": "NER",
     "text": "Estrai tutte le entità nominate (persone, luoghi, organizzazioni) dal testo del documento allegato"},
    {"label": "Rileva firma",
     "text": "Rileva ed estrai le firme presenti nel documento allegato"},
    {"label": "Verifica firma",
     "text": "Verifica se la firma nel documento è autentica rispetto alla firma di riferimento allegata"},
    {"label": "Grafologia",
     "text": "Analizza la scrittura nel documento e fornisci un profilo grafologico dettagliato"},
    {"label": "Chi ha scritto?",
     "text": "Chi ha scritto questo documento? Confronta la scrittura con i campioni nel database"},
    {"label": "Layout documento",
     "text": "Analizza il layout del documento: identifica tabelle, figure e sezioni di testo"},
    {"label": "Datazione",
     "text": "Trascrivi il documento allegato e cerca le date citate nel testo"},
    {"label": "Pipeline completa",
     "text": "Esegui un'analisi forense completa del documento allegato: trascrizione, entità, firma, grafologia e datazione"},
    {"label": "Analisi tabella",
     "text": "Estrai e struttura i dati dalla tabella presente nel documento allegato"},
    {"label": "Analisi figura",
     "text": "Descrivi e interpreta la figura o il grafico presente nel documento allegato"},
    {"label": "Analisi testamento",
     "text": (
         "Analizza questo testamento: estrai il testo, identifica le persone nominate, "
         "rileva la firma e confrontala con la firma di riferimento allegata"
     )},
    {"label": "Conformità ENFSI",
     "text": "Verifica la conformità di questa perizia agli standard ENFSI BPM"},
]

AGENT_TOOLS_NAMES = [
    "trascrivi_documento",
    "estrai_entita",
    "rileva_firma",
    "verifica_firma",
    "analisi_grafologica",
    "identifica_scrittore",
    "data_documento",
    "analisi_layout",
    "estrai_testo_layout",
    "analizza_tabella",
    "analizza_figura",
    "consulta_knowledge_base",
    "verifica_conformita_enfsi",
]

# ──────────────────────────────────────────────────────────────────────────────
# Image loading utility
# ──────────────────────────────────────────────────────────────────────────────

def _load_image(path: str) -> np.ndarray:
    """Load an image file to an RGB numpy array."""
    img = Image.open(path).convert("RGB")
    return np.array(img)


# ──────────────────────────────────────────────────────────────────────────────
# LangChain Tools
# ──────────────────────────────────────────────────────────────────────────────

from langchain_core.tools import tool  # noqa: E402


@tool
def trascrivi_documento(image_path: str) -> str:
    """Trascrive il testo manoscritto presente in un documento immagine.

    Usa questo strumento quando l'utente vuole leggere o estrarre testo scritto a mano.
    Il modello OCR usato è quello selezionato dall'utente nella sidebar
    (easyocr / vlm / paddleocr / trocr). Di default usa EasyOCR (CPU, veloce).
    Restituisce il testo trascritto che può essere passato ad altri strumenti.

    Args:
        image_path: Percorso assoluto del file immagine del documento.
    """
    try:
        from core.ocr import htr_transcribe
        img = _load_image(image_path)
        result = htr_transcribe(img)
        return f"Testo trascritto:\n\n{result}"
    except Exception as e:
        return f"Errore nella trascrizione: {e}"


@tool
def estrai_entita(testo: str) -> str:
    """Estrae le entità nominate (persone, luoghi, organizzazioni) da un testo.

    Usa questo strumento dopo aver trascritto un documento, o quando hai già il testo.
    Restituisce un elenco strutturato delle entità trovate.

    Args:
        testo: Il testo da cui estrarre le entità.
    """
    try:
        from core.ner import ner_extract
        _entities, report_md = ner_extract(testo)
        if not _entities:
            return "Nessuna entità nominata trovata nel testo."
        return f"Entità trovate:\n\n{report_md}"
    except Exception as e:
        return f"Errore nell'estrazione delle entità: {e}"


@tool
def rileva_firma(image_path: str) -> str:
    """Rileva le firme presenti in un documento immagine tramite object detection.

    Usa questo strumento per trovare e salvare la firma estratta da un documento.
    Salva il ritaglio della firma come file PNG nella cartella temporanea e restituisce
    il percorso reale del file salvato, che l'utente può usare direttamente.
    Se vuoi poi verificare l'autenticità di una firma, usa `verifica_firma`.

    Args:
        image_path: Percorso assoluto del file immagine del documento.
    """
    try:
        from core.signature import detect_and_crop
        img = _load_image(image_path)
        _annotated, crop, summary = detect_and_crop(img, conf_threshold=0.3)

        if crop is not None:
            import uuid as _uuid
            _img_dir = _ROOT / "data" / "uploads" / "agent" / "images"
            _img_dir.mkdir(parents=True, exist_ok=True)
            img_name = f"firma_{_uuid.uuid4().hex[:12]}.png"
            crop_path = _img_dir / img_name
            Image.fromarray(crop.astype("uint8")).save(str(crop_path))
            return (
                f"Risultato rilevamento firme:\n\n{summary}\n\n"
                f"Firma estratta salvata in: `{crop_path}`\n\n"
                f"![Firma estratta](/api/agent/images/{img_name})"
            )
        return f"Risultato rilevamento firme:\n\n{summary}"
    except Exception as e:
        return f"Errore nel rilevamento firma: {e}"


@tool
def verifica_firma(riferimento_path: str, query_path: str) -> str:
    """Verifica l'autenticità di una firma confrontandola con una firma di riferimento.

    Usa questo strumento quando hai due immagini di firme da confrontare:
    una firma autentica nota (riferimento) e una firma da verificare (query).

    Args:
        riferimento_path: Percorso dell'immagine della firma autentica di riferimento.
        query_path: Percorso dell'immagine della firma da verificare.
    """
    try:
        from core.signature import sig_verify
        ref_img = _load_image(riferimento_path)
        query_img = _load_image(query_path)
        report, _chart = sig_verify(ref_img, None, query_img, SIGNET_WEIGHTS)
        return f"Risultato verifica firma:\n\n{report}"
    except Exception as e:
        return f"Errore nella verifica della firma: {e}"


@tool
def analisi_grafologica(image_path: str) -> str:
    """Analizza le caratteristiche grafologiche di una scrittura a mano.

    Estrae metriche quantitative: inclinazione, pressione, dimensioni lettere,
    spaziatura, densità del tratto, componenti connesse.

    Args:
        image_path: Percorso assoluto del file immagine con la scrittura a mano.
    """
    try:
        from core.graphology import grapho_analyse
        img = _load_image(image_path)
        report_md, _annotated = grapho_analyse(img)
        return f"Analisi grafologica:\n\n{report_md}"
    except Exception as e:
        return f"Errore nell'analisi grafologica: {e}"


@tool
def identifica_scrittore(image_path: str) -> str:
    """Identifica chi ha scritto un documento confrontando con i campioni nel database.

    Usa questo strumento per abbinare una scrittura a uno scrittore noto nel sistema.
    Restituisce i candidati ordinati per probabilità.

    Args:
        image_path: Percorso assoluto del file immagine con la scrittura a mano.
    """
    try:
        from core.writer import writer_identify
        img = _load_image(image_path)
        report, _chart = writer_identify(img, WRITER_SAMPLES_DIR)
        return f"Identificazione scrittore:\n\n{report}"
    except Exception as e:
        return f"Errore nell'identificazione dello scrittore: {e}"


@tool
def data_documento(testo: str) -> str:
    """Estrae e normalizza le date presenti nel testo di un documento.

    Usa questo strumento dopo aver trascritto un documento per trovare le date citate.
    Supporta formati italiani e internazionali.

    Args:
        testo: Il testo del documento da cui estrarre le date.
    """
    try:
        from core.dating import extract_dates
        dates = extract_dates(testo)
        if not dates:
            return "Nessuna data trovata nel testo."
        lines = [f"- {raw}{dt.strftime('%d/%m/%Y')}" for raw, dt in dates]
        return "Date trovate nel documento:\n\n" + "\n".join(lines)
    except Exception as e:
        return f"Errore nell'estrazione delle date: {e}"


@tool
def analisi_layout(image_path: str) -> str:
    """Analizza il layout di un documento identificando regioni strutturate.

    Rileva automaticamente: testo, tabelle, figure, titoli e altri elementi.
    Usa questo strumento per capire la struttura visiva di un documento.

    Args:
        image_path: Percorso assoluto del file immagine del documento.
    """
    try:
        from core.document_layout import detect_layout
        result = detect_layout(image_path)
        if "error" in result:
            err = result['error']
            # Return a plain technical error; do NOT speculate on the cause
            return (
                f"Errore tecnico nel modello di layout detection: {err}\n"
                "Nota: questo è un errore del backend PaddlePaddle, non dipende dal tipo di file."
            )
        regions = result.get("regions", [])
        if not regions:
            return "Nessuna regione strutturata rilevata nel documento."
        lines = [
            f"- **{r['label']}** (confidenza: {r.get('score', 0):.0%})"
            for r in regions
        ]
        return f"Layout del documento — {len(regions)} regioni rilevate:\n\n" + "\n".join(lines)
    except Exception as e:
        return f"Errore nell'analisi del layout: {e}"


@tool
def estrai_testo_layout(image_path: str) -> str:
    """Estrae il testo da un documento stampato usando OCR con ordinamento di lettura.

    A differenza di `trascrivi_documento` (ottimizzato per manoscritti), questo
    strumento è pensato per documenti stampati o misti, e mantiene l'ordine di lettura.

    Args:
        image_path: Percorso assoluto del file immagine del documento.
    """
    try:
        from core.document_layout import extract_ordered_text
        text = extract_ordered_text(image_path)
        if not text.strip():
            return "Nessun testo estratto dal documento."
        return f"Testo estratto (ordinato per posizione di lettura):\n\n{text}"
    except Exception as e:
        return f"Errore nell'estrazione del testo con layout: {e}"


@tool
def analizza_tabella(image_path: str, region_index: int = 0) -> str:
    """Estrae e struttura i dati da una tabella presente in un documento.

    Rileva automaticamente la tabella nel documento, la ritaglia e usa un modello
    visuale (qwen3-vl) per estrarne il contenuto in formato Markdown strutturato.
    Ispirato all'approccio del corso DeepLearning.AI Document AI (L6).

    Usa questo strumento quando il documento contiene tabelle di dati da analizzare
    (es. tabelle comparative, dati anagrafici, elenchi strutturati).

    Args:
        image_path:   Percorso assoluto del file immagine del documento.
        region_index: Indice della tabella da analizzare (0 = prima tabella, default).
    """
    try:
        from core.document_layout import analyse_table_region
        result = analyse_table_region(image_path, region_index=region_index, model=AGENT_MODEL)
        if "error" in result:
            return f"Analisi tabella: {result['error']}"
        region = result["region"]
        return (
            f"Tabella #{region_index + 1} estratta "
            f"(posizione: {region['bbox']}, confidenza: {region.get('score', 0):.0%}):\n\n"
            f"{result['markdown']}"
        )
    except Exception as e:
        return f"Errore nell'analisi della tabella: {e}"


@tool
def analizza_figura(image_path: str, region_index: int = 0) -> str:
    """Analizza e descrive una figura o grafico presente in un documento.

    Rileva automaticamente la figura, la ritaglia e usa un modello visuale
    (qwen3-vl) per descriverne il contenuto: tipo di grafico, valori, trend,
    legenda. Ispirato all'approccio del corso DeepLearning.AI Document AI (L6).

    Usa questo strumento quando il documento contiene grafici, diagrammi, immagini
    o figure che necessitano di interpretazione visiva.

    Args:
        image_path:   Percorso assoluto del file immagine del documento.
        region_index: Indice della figura da analizzare (0 = prima figura, default).
    """
    try:
        from core.document_layout import analyse_figure_region
        result = analyse_figure_region(image_path, region_index=region_index, model=AGENT_MODEL)
        if "error" in result:
            return f"Analisi figura: {result['error']}"
        region = result["region"]
        return (
            f"Figura #{region_index + 1} analizzata "
            f"(posizione: {region['bbox']}, confidenza: {region.get('score', 0):.0%}):\n\n"
            f"{result['description']}"
        )
    except Exception as e:
        return f"Errore nell'analisi della figura: {e}"


@tool
def consulta_knowledge_base(domanda: str) -> str:
    """Consulta la knowledge base forense per rispondere a domande teoriche.

    Usa questo strumento per domande su tecniche forensi, standard ENFSI,
    metodi grafologici, interpretazione di parametri, ecc.
    NON usare questo strumento per analizzare file — usa gli altri strumenti per quello.

    Args:
        domanda: La domanda da porre alla knowledge base forense.
    """
    try:
        from core.rag import rag_retrieve, stream_ollama
        results, err = rag_retrieve(domanda)
        if err:
            return f"Errore nella knowledge base: {err}"
        if not results:
            return "Nessun risultato trovato nella knowledge base."
        context = "\n\n".join(chunk["text"] for _score, chunk in results[:3])
        prompt = (
            "Rispondi in italiano alla seguente domanda forense basandoti sul contesto.\n\n"
            f"Contesto:\n{context}\n\n"
            f"Domanda: {domanda}\n\nRisposta:"
        )
        response = "".join(stream_ollama(prompt))
        return response.strip() or "Nessuna risposta generata."
    except Exception as e:
        return f"Errore nella consultazione della knowledge base: {e}"


@tool
def verifica_conformita_enfsi(pdf_path: str) -> str:
    """Verifica la conformità di una perizia grafologica agli standard ENFSI BPM.

    Analizza il documento PDF e valuta i 20 requisiti del Best Practice Manual
    ENFSI-BPM-FHX-01 Ed.03 sez. 13.2, producendo un rapporto dettagliato con
    verdetti (CONFORME / PARZIALE / MANCANTE) per ciascun requisito.
    Usa questo strumento quando l'utente vuole verificare se una perizia rispetta
    gli standard ENFSI, o quando chiede 'conformità ENFSI' / 'check ENFSI'.

    Args:
        pdf_path: Percorso assoluto del file PDF della perizia da analizzare.
    """
    import json as _json
    import re as _re

    try:
        from core.compliance import (
            compliance_check_stream,
            extract_perizia_text,
            _parse_verdicts,
            _ENFSI_CHECKLIST,
        )

        perizia_text = extract_perizia_text(Path(pdf_path))
        if not perizia_text.strip():
            return "❌ Impossibile estrarre il testo dal PDF. Verifica che il file non sia corrotto o protetto da password."

        # Accumulate the full streamed output (generator yields accumulated text)
        full_text = ""
        for chunk in compliance_check_stream(perizia_text):
            full_text = chunk

        if not full_text:
            return "❌ Nessun risultato prodotto dall'analisi di conformità."

        # Build structured data for frontend rendering
        verdicts = _parse_verdicts(full_text)
        blocks = []
        for i, (name, _) in enumerate(_ENFSI_CHECKLIST):
            req_num = i + 1
            bp = _re.compile(
                rf"REQ[-\s]?{req_num:02d}[^\n]*\n([\s\S]+?)(?=REQ[-\s]?\d{{1,2}}\.|\n---|\Z)",
                _re.IGNORECASE,
            )
            m = bp.search(full_text)
            block_text = m.group(1) if m else ""
            motiv_m = _re.search(
                r"Motivazione:\s*([\s\S]+?)(?=\s*💡|(?:\n\s*){3}|$)", block_text
            )
            sugg_m = _re.search(r"💡\s*Suggerimento:\s*([\s\S]+?)$", block_text.strip())
            suggerimento = sugg_m.group(1).strip() if sugg_m else None
            if suggerimento and _re.match(r"^nessuno\.?$", suggerimento, _re.IGNORECASE):
                suggerimento = None
            blocks.append({
                "num": req_num,
                "name": name,
                "verdict": verdicts.get(req_num),
                "motivazione": motiv_m.group(1).strip() if motiv_m else "",
                "suggerimento": suggerimento,
            })

        conformi = sum(1 for b in blocks if b["verdict"] == "✅")
        parziali  = sum(1 for b in blocks if b["verdict"] == "⚠️")
        mancanti  = sum(1 for b in blocks if b["verdict"] == "❌")
        jm = _re.search(r"\*\*Giudizio complessivo:\s*([^*]+)\*\*", full_text)
        structured = {
            "filename": Path(pdf_path).name,
            "blocks": blocks,
            "conformi": conformi,
            "parziali": parziali,
            "mancanti": mancanti,
            "judgment": jm.group(1).strip() if jm else "",
        }
        marker = f"\n<!-- COMPLIANCE_REPORT: {_json.dumps(structured, ensure_ascii=False)} -->"
        return full_text + marker

    except Exception as e:
        return f"Errore nella verifica di conformità ENFSI: {e}"


# ──────────────────────────────────────────────────────────────────────────────
# Tool list
# ──────────────────────────────────────────────────────────────────────────────

_ALL_TOOLS = [
    trascrivi_documento,
    estrai_entita,
    rileva_firma,
    verifica_firma,
    analisi_grafologica,
    identifica_scrittore,
    data_documento,
    analisi_layout,
    estrai_testo_layout,
    analizza_tabella,
    analizza_figura,
    consulta_knowledge_base,
    verifica_conformita_enfsi,
]

# ──────────────────────────────────────────────────────────────────────────────
# Agent factory  (LangGraph — replaces deprecated AgentExecutor)
# ──────────────────────────────────────────────────────────────────────────────

def create_forensic_agent(model: str = AGENT_MODEL, project_context: str | None = None) -> Any:
    """Create a LangGraph react agent with all GraphoLab forensic tools.

    Args:
        model:           Ollama model name (default: qwen3:8b).
        project_context: Optional context string injected after the base system
                         prompt (e.g. list of project documents and past chats).

    Returns:
        Compiled LangGraph graph (CompiledGraph).
    """
    import inspect
    from langgraph.prebuilt import create_react_agent
    from langchain_core.messages import SystemMessage
    from core.providers import is_openai_model

    if is_openai_model(model):
        from langchain_openai import ChatOpenAI
        from core.providers import _read_openai_key
        llm = ChatOpenAI(model=model, temperature=0, openai_api_key=_read_openai_key())
    else:
        from langchain_ollama import ChatOllama
        llm = ChatOllama(model=model, temperature=0)
    prompt_text = FORENSIC_SYSTEM_PROMPT
    if project_context:
        prompt_text = prompt_text + "\n\n" + project_context
    system_msg = SystemMessage(content=prompt_text)

    # LangGraph ≥0.2.57 uses `prompt`; older versions use `state_modifier`
    sig = inspect.signature(create_react_agent)
    if "prompt" in sig.parameters:
        return create_react_agent(llm, _ALL_TOOLS, prompt=system_msg)
    else:
        return create_react_agent(llm, _ALL_TOOLS, state_modifier=system_msg)


# ──────────────────────────────────────────────────────────────────────────────
# Streaming helper
# ──────────────────────────────────────────────────────────────────────────────

def get_active_model() -> str:
    """Return the currently active Ollama model.

    Reads _rag_model from core.rag at call time so it always reflects the
    model selected by the user from the sidebar (which calls set_rag_model).
    Falls back to AGENT_MODEL if core.rag is not yet initialised.
    """
    try:
        from core.rag import _rag_model
        return _rag_model or AGENT_MODEL
    except Exception:
        return AGENT_MODEL


def agent_stream(
    message: str,
    file_paths: list[str],
    history: list[dict],
    model: str | None = None,
    stop_event: Optional[threading.Event] = None,
    project_context: str | None = None,
) -> Generator[str, None, None]:
    """Run the forensic agent and stream accumulated response text.

    Each yield replaces the previous one (accumulative SSE pattern, same as RAG chat).

    Args:
        message:    User's text message.
        file_paths: Absolute paths of uploaded files (injected into the message).
        history:    List of {"role": "user"|"assistant", "content": str}.
        model:      Ollama model name. If None, uses the globally active model
                    (set via the sidebar / set_rag_model).

    Yields:
        Accumulated response string (each yield is a complete update, not a delta).
    """
    if model is None:
        model = get_active_model()
    from langchain_core.messages import HumanMessage, AIMessage

    # Copy uploaded files into a unique per-session subfolder so concurrent
    # requests never overwrite each other's files (e.g. /tmp/gl/<uuid>/f0.jpg).
    # Keep the original filename so the LLM knows what each file is.
    import uuid as _uuid
    _session_id = _uuid.uuid4().hex[:12]
    _gl_tmp = Path(tempfile.gettempdir()) / "gl" / _session_id
    _gl_tmp.mkdir(parents=True, exist_ok=True)
    short_paths: list[str] = []
    for p in file_paths:
        orig_name = Path(p).name
        dest = _gl_tmp / orig_name
        # Avoid collisions if two files share the same name
        if dest.exists():
            stem = Path(orig_name).stem
            ext = Path(orig_name).suffix
            dest = _gl_tmp / f"{stem}_{_uuid.uuid4().hex[:6]}{ext}"
        shutil.copy2(p, dest)
        short_paths.append(str(dest))

    # Inject file paths into the user message so the agent can use them as tool args
    if short_paths:
        paths_str = "\n".join(f"[file: {p}]" for p in short_paths)
        full_message = f"{message}\n\nFile allegati:\n{paths_str}"
    else:
        full_message = message

    # Build messages list: history + current user message
    messages: list = []
    for msg in history:
        role = msg.get("role", "")
        content = msg.get("content", "")
        if role == "user":
            messages.append(HumanMessage(content=content))
        elif role == "assistant":
            messages.append(AIMessage(content=content))
    messages.append(HumanMessage(content=full_message))

    # ── Fast path: compliance request with a PDF ─────────────────────────────
    # qwen3 is unreliable at tool-calling when the result is very long.
    # Detect compliance requests and call the tool directly, bypassing the LLM
    # tool-call decision entirely.
    _COMPLIANCE_KEYWORDS = ("conformità enfsi", "conformita enfsi", "verifica enfsi",
                            "enfsi bpm", "check enfsi")
    _is_compliance = any(kw in message.lower() for kw in _COMPLIANCE_KEYWORDS)
    _pdf_paths = [p for p in short_paths if p.lower().endswith(".pdf")]
    if _is_compliance and _pdf_paths:
        yield "🔧 *`verifica_conformita_enfsi(…)`…*"
        result = verifica_conformita_enfsi.invoke({"pdf_path": _pdf_paths[0]})
        _prefix = "<!-- COMPLIANCE_REPORT: "
        _suffix = " -->"
        _ps = result.find(_prefix)
        if _ps != -1:
            _pe = result.rfind(_suffix)
            if _pe > _ps:
                _marker = "\n" + result[_ps:_pe + len(_suffix)]
                _tool_log = "\n\n🔧 *`verifica_conformita_enfsi({...})`* ✅"
                details = (
                    "\n\n<details><summary>__TOOL_LOG__</summary>\n"
                    + _tool_log
                    + "\n</details>"
                )
                yield _marker + details
                if _gl_tmp.exists():
                    shutil.rmtree(_gl_tmp, ignore_errors=True)
                return
        # If marker not found (error), fall through to normal agent
        yield result
        if _gl_tmp.exists():
            shutil.rmtree(_gl_tmp, ignore_errors=True)
        return
    # ─────────────────────────────────────────────────────────────────────────

    agent = create_forensic_agent(model, project_context=project_context)

    accumulated = ""
    tool_log: list[str] = []
    image_blocks: list[str] = []  # image markdown extracted from tool results
    table_blocks: list[str] = []  # markdown tables extracted from tool results
    compliance_marker: str = ""   # COMPLIANCE_REPORT marker extracted from tool results
    compliance_emitted: bool = False  # True once the compliance final response is yielded

    import re as _re
    import logging as _logging
    _img_md_re = _re.compile(r'!\[.*?\]\(/api/agent/images/[^\)]+\)')

    def _extract_tables(text: str) -> list[str]:
        """Extract markdown tables from tool result text.

        Handles both properly-newlined tables and collapsed single-line tables
        (LangGraph sometimes strips newlines from tool result strings).
        """
        tables: list[str] = []

        # ── Case 1: multi-line table (normal case) ────────────────────────────
        current: list[str] = []
        for line in text.splitlines():
            stripped = line.strip()
            if stripped.startswith("|") and stripped.endswith("|"):
                current.append(stripped)
            else:
                if len(current) >= 3:
                    tables.append("\n".join(current))
                current = []
        if len(current) >= 3:
            tables.append("\n".join(current))

        if tables:
            return tables

        # ── Case 2: collapsed single-line table ───────────────────────────────
        # Detect a line with many pipes and a separator chunk like |---|
        for line in text.splitlines():
            stripped = line.strip()
            if stripped.count("|") >= 6 and "|--" in stripped:
                # Split on " | " boundary keeping the outer pipes
                # e.g. "| A | B | C | |---|---|---| | x | y | z |"
                # Reconstruct by splitting at separator pattern
                parts = _re.split(r'(?=\|[-:| ]+\|)', stripped)
                rows: list[str] = []
                for part in parts:
                    part = part.strip()
                    if part.startswith("|") and part.endswith("|"):
                        rows.append(part)
                if len(rows) >= 3:
                    tables.append("\n".join(rows))
                    break

        return tables

    try:
        for chunk in agent.stream(
            {"messages": messages},
            stream_mode="updates",
        ):
            if stop_event is not None and stop_event.is_set():
                break

            # ── Agent node: either a tool-call decision or the final answer ──
            if "agent" in chunk:
                for msg in chunk["agent"]["messages"]:
                    tool_calls = getattr(msg, "tool_calls", [])
                    content = getattr(msg, "content", "") or ""
                    if tool_calls:
                        for tc in tool_calls:
                            tool_name = tc.get("name", "tool")
                            tool_args = str(tc.get("args", {}))[:80]
                            entry = f"\n\n🔧 *`{tool_name}({tool_args})`…*"
                            tool_log.append(entry)
                        yield accumulated + "".join(tool_log)
                    elif content and not compliance_emitted:
                        # Final answer from the agent
                        accumulated = content
                        # If we extracted clean tables from tool results, strip
                        # any mangled pipe-rows the LLM may have written inline
                        # (qwen3 collapses table newlines into a single line)
                        if table_blocks:
                            clean_lines = [
                                ln for ln in accumulated.splitlines()
                                if ln.count("|") < 4  # keep narrative, drop pipe-heavy lines
                            ]
                            accumulated = "\n".join(clean_lines).strip()
                            accumulated += "\n\n" + "\n\n".join(table_blocks)
                        if image_blocks:
                            accumulated += "\n\n" + "\n\n".join(image_blocks)
                        if compliance_marker:
                            accumulated += compliance_marker
                        if tool_log:
                            details = (
                                "\n\n<details><summary>__TOOL_LOG__</summary>\n"
                                + "".join(tool_log)
                                + "\n</details>"
                            )
                            yield accumulated + details
                        else:
                            yield accumulated

            # ── Tools node: results returned by tool calls ──
            elif "tools" in chunk:
                for msg in chunk["tools"]["messages"]:
                    content = getattr(msg, "content", "") or ""
                    # Extract compliance report marker (verifica_conformita_enfsi tool)
                    if not compliance_marker:
                        _prefix = "<!-- COMPLIANCE_REPORT: "
                        _suffix = " -->"
                        _ps = content.find(_prefix)
                        if _ps != -1:
                            _pe = content.rfind(_suffix)
                            if _pe > _ps:
                                compliance_marker = "\n" + content[_ps:_pe + len(_suffix)]
                    # Extract image markdown from tool result before truncating
                    for img_md in _img_md_re.findall(content):
                        if img_md not in image_blocks:
                            image_blocks.append(img_md)
                    # Extract markdown tables from tool result so they are
                    # appended verbatim to the final response (LLM tends to
                    # collapse table rows onto a single line when rewriting)
                    for tbl in _extract_tables(content):
                        if tbl not in table_blocks:
                            table_blocks.append(tbl)
                    if tool_log:
                        tool_log[-1] = tool_log[-1].rstrip("…*") + " ✅*"
                    # If compliance marker found, emit final response immediately
                    # without waiting for the LLM to synthesise the tool output
                    if compliance_marker and not compliance_emitted:
                        compliance_emitted = True
                        details = (
                            "\n\n<details><summary>__TOOL_LOG__</summary>\n"
                            + "".join(tool_log)
                            + "\n</details>"
                        )
                        yield accumulated + compliance_marker + details
                    else:
                        short = content[:120] + ("…" if len(content) > 120 else "")
                        tool_log.append(f"\n> *{short}*")
                        yield accumulated + "".join(tool_log)

    except Exception as e:
        yield f"{accumulated}\n\n❌ Errore dell'agente: {e}"
    else:
        # Fallback: if compliance marker was never emitted (shouldn't happen
        # with the tools-node immediate emit, but just in case)
        if compliance_marker and not compliance_emitted:
            details = (
                "\n\n<details><summary>__TOOL_LOG__</summary>\n"
                + "".join(tool_log)
                + "\n</details>"
            ) if tool_log else ""
            yield accumulated + compliance_marker + details
    finally:
        # Clean up the per-session temp directory after streaming completes
        if _gl_tmp.exists():
            shutil.rmtree(_gl_tmp, ignore_errors=True)