File size: 12,662 Bytes
399944f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import os
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Any, List, Optional, Sequence, cast
from typing_extensions import LiteralString
from dotenv import load_dotenv
from rich.progress import track

from neo4j import GraphDatabase, Driver, Query
from neo4j.exceptions import Neo4jError

from kbdebugger.types import GraphRelation, EdgeProperties
from .utils import rows_to_graph_relations
from .types import BatchUpsertSummary
from .aura_api import ensure_aura_running_from_env 

import rich
from rich.console import Console
from rich.panel import Panel

# Load env vars once here
load_dotenv(override=True)

@dataclass
class GraphStore:
    """
    Central access point to the knowledge graph.

    - Handles connecting to Neo4j
    - Exposes:
        - `query(...)` for arbitrary Cypher
        - `upsert_relation(...)` for writing extracted relations
    """
    # inner: Neo4jGraph
    driver: Driver

    # ---------- construction / connection ----------
    @classmethod
    def connect(
        cls,
        *,
        uri: Optional[str] = None,
        username: Optional[str] = None,
        password: Optional[str] = None,
        auto_env: bool = True,
        verbose: bool = True,
    ) -> "GraphStore":
        """
        Build a GraphStore from environment variables (or explicit args).

        Env vars:
            - NEO4J_URI
            - NEO4J_USERNAME (default "neo4j")
            - NEO4J_PASSWORD (default "")
        """
        if auto_env:
            load_dotenv(override=True)

        # ✅ Preflight: make sure Aura is running before Neo4j driver even tries DNS
        ensure_aura_running_from_env(verbose=verbose)

        neo4j_uri = uri or os.getenv("NEO4J_URI")
        neo4j_user = username or os.getenv("NEO4J_USERNAME", "neo4j")
        neo4j_pass = password or os.getenv("NEO4J_PASSWORD", "")

        if not neo4j_uri:
            raise RuntimeError("NEO4J_URI is not set (pass uri=... or set env var).")

        # inner = Neo4jGraph(url=neo4j_uri, username=neo4j_user, password=neo4j_pass)
        driver = GraphDatabase.driver(neo4j_uri, auth=(neo4j_user, neo4j_pass))

        if verbose:
            rich.print(
                f"[kbdebugger] Connected to Neo4j at {neo4j_uri!r} "
                f"as user {neo4j_user!r}"
            )

        # return cls(inner=inner)
        return cls(driver=driver)


    def close(self) -> None:
        self.driver.close()


    # ---------- basic query API ----------
    def query(
        self,
        cypher: str,
        params: dict[str, Any] | None = None,
    ) -> list[dict[str, Any]]:
        """
        Run a Cypher query with consistent error handling.

        This is the *only* low-level escape hatch other code should use.
        """
        # print("GraphStore backend:", type(self.driver), type(self))
        try:
            with self.driver.session() as session:
                cypher_as_literal_str = cast(LiteralString, cypher)
                # result = session.run(Query(cypher), params or {})
                result = session.run(cypher_as_literal_str, params or {})
                return [record.data() for record in result]
        except Neo4jError as e:
            message = "Neo4j query failed!\n" \
                f"Error: {e.__class__.__name__}: {e}\n" \
                f"Query:\n{cypher}\n" \
                f"Params:\n{params}"
            
            rich.print(
                f"[bold red]{message}[/bold red]\n"
            )
            raise RuntimeError(message) from e
        except Exception as e:
            raise RuntimeError(
                "Unexpected error during Neo4j query:\n"
                f"{e}\nQuery:\n{cypher}\nParams:\n{params}"
            ) from e


    def query_relations(
        self,
        cypher: str,
        params: dict[str, Any] | None = None,
        *,
        source_key: str = "source",
        target_key: str = "target",
        predicate_key: str = "predicate",
        props_key: str = "props",
    ) -> list[GraphRelation]:
        """
        Run a Cypher query that returns (source, target, predicate, props) columns
        and coerce it to List[GraphRelation].
        """
        rows = self.query(cypher, params=params or {})
        return rows_to_graph_relations(
            rows,
            source_key=source_key,
            target_key=target_key,
            predicate_key=predicate_key,
            props_key=props_key,
        )


    # ---------- high-level write API ----------
    def upsert_relation(self, relation: GraphRelation) -> list[dict[str, Any]]:
        """
        Insert or update a single GraphRelation into Neo4j.

        - Nodes are always `(:Node {label: ...})`
        - Relationships are always `[:REL {label: ..., ...}]`
        - Dedupe is based on (`source_label`, `target_label`, `rel.label`, `edge.properties['source']`)
            - i.e., we assume that the same relation from the same source text is the same fact.

        Example relation:
        ```
            relation = {
                'source': {
                    'label': 'Monitoring'
                },
                'target': {
                    'label': 'KI system'
                },
                'edge': {
                    'label': 'is performed during operation of',
                    'properties': {
                        'sentence': 'Monitoring is performed during operation of KI system',
                        'source': '20241015_MISSION_KI_Glossar_v1.0 en.pdf',
                        'page_number': 1,
                        'start_index': 0
                    }
                },
            }
        ```
        """

        src_label = relation["source"]["label"]
        tgt_label = relation["target"]["label"]
        rel_label = relation["edge"]["label"]
        props_all = relation["edge"]["properties"]

        # Key used to *match* existing relationships (dedupe policy)
        rel_key_props: EdgeProperties = {
            "label": rel_label,
            "source": props_all.get("source", ""),
            # Add more fields here if we want stricter deduplication
        }
        # ⚠️ remember: the rel_key_props are the props of the relationship used to find existing rels!
        # so choose them carefully to avoid over- or under-merging.

        # Why this is safer
        # - We won't create a new rel just because page_number or some metadata changed.
        # - We still persist all the rich properties on first create, and can update on matches.

        now_iso = datetime.now(timezone.utc).isoformat()

        # Properties set only when a relationship is first created
        on_create_props: EdgeProperties = {
            **props_all,
            "created_at": now_iso,
        }

        # Properties updated whenever we re-encounter the same relationship
        on_match_props = {
            "last_updated_at": now_iso,
        }

        """
        apoc.merge.relationship is a Neo4j APOC procedure used to dynamically merge or create a relationship between two nodes, which either exists or is created based on the provided parameters. 
        It handles dynamic relationship types and properties for creation or matching, and its signature is apoc.merge.relationship(
            startNode, 
            relType,        # e.g., "REL"
            relKeyProps,    # properties used to identify existing relationships
            onCREATEProps,  # properties set only when a new relationship is created
            endNode, 
            onMatchProps    # properties updated when a relationship already exists
        ). 
        This is useful for building Cypher queries where the relationship details are provided as parameters, for example, when unwinding a list of rows.
        """
        
        # About {} vs {{}}
        # - In a plain triple-quoted Python string (""" ... """), {} is just literal braces — fine.
        # - In an f-string (f""" ... """), {} is interpolation. To emit literal braces, we must escape as {{}}.
        # So:
        # - If we keep it as a plain string: """ ... {} ... """ is OK.
        # - If we switch to f""" ... """: change to {{}}.

        cypher = """
        MERGE (s:Node {label: $source_label})
        ON CREATE SET s.created_at = datetime()
        SET s.last_updated_at = datetime(),
            s.created_at = coalesce(s.created_at, datetime())

        MERGE (t:Node {label: $target_label})
        ON CREATE SET t.created_at = datetime()
        SET t.last_updated_at = datetime(),
            t.created_at = coalesce(t.created_at, datetime())

        WITH s, t
        CALL apoc.merge.relationship(
            s,
            $rel_type,
            $rel_key_props,
            $on_create,
            t,
            $on_match
        ) YIELD rel

        // ensure temporal props exist on the rel too
        SET rel.created_at    = coalesce(rel.created_at, datetime()),
            rel.last_updated_at = datetime()

        RETURN s, t, rel
        """

        return self.query(
            cypher,
            params={
                "source_label": src_label,
                "target_label": tgt_label,
                "rel_type": "REL", # always 'REL' as the relationship type; actual label is in properties
                "rel_key_props": rel_key_props,
                "on_create": on_create_props, # set only on create of the relationship
                "on_match": on_match_props, # set only on match (update) of the relationship
            },
        )


    def upsert_relations(
            self, 
            relations: Sequence[GraphRelation],
            *,
            pretty_print: bool = True,
    ) -> BatchUpsertSummary:
        """
        Upsert multiple GraphRelation objects into Neo4j.

        This is a convenience wrapper around `upsert_relation()` that:
        - preserves the dedupe semantics of the single-upsert operation
        - continues on individual failures (best-effort write)
        - returns a typed summary for logging and monitoring

        Parameters
        ----------
        relations:
            Relations to be inserted/merged into the KG.

        Returns
        -------
        BatchUpsertSummary
            Counts and error strings describing any failures.

        Notes
        -----
        - This method performs no client-side deduplication.
          Deduplication is handled inside `upsert_relation()` via the APOC merge policy.
        - If you later want "fail-fast" semantics, add a flag like `stop_on_error`.
        """
        if not relations:
            # Nothing to upsert is not an error here; extraction may legitimately produce nothing.
            return BatchUpsertSummary(
                attempted=0,
                succeeded=0,
                failed=0,
                errors=[],
            )

        attempted = len(relations)
        succeeded = 0
        errors: List[str] = []

        for i, rel in track(
            enumerate(relations, start=1), 
            description="➕🛸 Upserting triplets (relations) into Knowledge Graph",
            total=len(relations)):
            try:
                self.upsert_relation(rel)
                succeeded += 1
            except Exception as e:  # pylint: disable=broad-exception-caught
                src = rel.get("source", {}).get("label", "?")
                tgt = rel.get("target", {}).get("label", "?")
                pred = rel.get("edge", {}).get("label", "?")
                errors.append(f"[{i}/{attempted}] {src} - {pred} -> {tgt}: {e}")

        failed = attempted - succeeded
        
        summary = BatchUpsertSummary(
            attempted=attempted,
            succeeded=succeeded,
            failed=failed,
            errors=errors,
        )

        if pretty_print:
            console = Console()

            body_lines = [
                f"[bold]Attempted:[/bold] {summary.attempted}",
                f"[bold green]Succeeded:[/bold green] {summary.succeeded}",
                f"[bold red]Failed:[/bold red] {summary.failed}",
            ]

            if summary.failed > 0:
                body_lines.append("\n[bold red]Errors:[/bold red]")
                for err in summary.errors:
                    body_lines.append(f"  • {err}")

            console.print(
                Panel(
                    "\n".join(body_lines),
                    title="[bold cyan]🧠📊 Knowledge Graph Upsert Summary[/bold cyan]",
                    border_style="cyan",
                    padding=(1, 2),
                )
            )


        return summary