File size: 13,051 Bytes
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3c28
 
6c4e218
 
 
93a3c28
 
 
 
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3c28
 
 
 
 
 
6c4e218
 
93a3c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c4e218
 
 
 
 
 
 
93a3c28
6c4e218
 
 
 
 
 
 
93a3c28
6c4e218
93a3c28
6c4e218
93a3c28
6c4e218
 
 
 
93a3c28
6c4e218
93a3c28
 
 
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3c28
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3c28
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3c28
6c4e218
 
 
 
 
 
 
93a3c28
6c4e218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a3c28
 
 
 
 
 
 
 
 
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
import os
import json
import time
import uuid
import threading
from typing import Any, Dict, List, Optional


class SchemaProfile:
    """
    Runtime schema profile representing the active database.
    Stores tables, columns, PK/FK metadata, row counts, sample rows, and
    a digest string suitable for injecting into LLM prompts.
    """

    SAMPLE_ROW_LIMIT = 3
    MAX_DIGEST_TABLES = 40
    MAX_DIGEST_COLUMNS = 25

    def __init__(self, name: str, db_url: Optional[str] = None, is_demo: bool = False,
                 notes: str = ""):
        self.name = name
        self.db_url = db_url
        self.is_demo = is_demo
        # Owner-supplied schema notes (domain hints): column semantics, business
        # rules, aliases. Injected verbatim into LLM prompts alongside the
        # introspected digest. Stays empty unless the database owner writes it.
        self.notes = (notes or "").strip()
        self.tables: List[str] = []
        self.columns_by_table: Dict[str, List[Dict[str, Any]]] = {}
        self.pk_by_table: Dict[str, List[str]] = {}
        self.fk_by_table: Dict[str, List[Dict[str, Any]]] = {}
        self.row_counts: Dict[str, int] = {}
        self.sample_rows: Dict[str, List[Dict[str, Any]]] = {}
        self.available_tables_set: set = set()

    @property
    def is_employee_db(self) -> bool:
        # Treat as employee demo if explicitly marked or name matches
        if self.is_demo:
            return True
        n = (self.name or "").lower()
        return "employee" in n or n == "demo_employee"

    # ------------------------------------------------------------------
    # Population
    # ------------------------------------------------------------------
    def populate_from_inspector(self, inspector: Any, engine: Optional[Any] = None) -> None:
        """Fill all metadata from a SQLAlchemy inspector (and optionally sample
        row counts / preview rows via a sync engine connection)."""
        try:
            tables = list(
                t for t in (inspector.get_table_names() + inspector.get_view_names())
                if not t.startswith("_")
            )
        except Exception as e:
            print(f"[SCHEMA-PROFILE] inspector table listing failed: {e}")
            tables = []

        self.tables = sorted(tables)
        self.available_tables_set = set(self.tables)
        self.columns_by_table = {}
        self.pk_by_table = {}
        self.fk_by_table = {}

        for t in self.tables:
            try:
                self.columns_by_table[t] = inspector.get_columns(t)
            except Exception:
                self.columns_by_table[t] = []
            try:
                pks = inspector.get_pk_constraint(t)
                self.pk_by_table[t] = (pks or {}).get("constrained_columns", []) or []
            except Exception:
                self.pk_by_table[t] = []
            try:
                self.fk_by_table[t] = inspector.get_foreign_keys(t) or []
            except Exception:
                self.fk_by_table[t] = []

        if engine is not None:
            self._collect_row_stats(engine)

    def _collect_row_stats(self, engine: Any) -> None:
        """Best-effort row counts + small sample rows using a sync engine.

        Dialect-aware so row previews also work on databases whose syntax for
        limiting differs (mssql uses TOP, oracle 12c+ uses FETCH FIRST, mysql
        prefers backtick-quoted identifiers, standard SQL uses LIMIT).
        """
        from sqlalchemy import text as sql_text

        # Identifier quoting + limit syntax per dialect (best-effort)
        dname = ""
        try:
            dname = (getattr(engine, "dialect", None).name or "").lower()
        except Exception:
            dname = ""

        def q(name: str) -> str:
            if dname == "mssql":
                return f"[{name}]"
            if dname in ("mysql", "mariadb"):
                return f"`{name}`"
            return f'"{name}"'

        def sample_candidates(table: str):
            limit = self.SAMPLE_ROW_LIMIT
            quoted = q(table)
            if dname == "mssql":
                return [f"SELECT TOP {limit} * FROM {quoted}"]
            if dname == "oracle":
                # 12c+ thin driver supports FETCH FIRST; legacy ROWNUM fallback
                return [
                    f"SELECT * FROM {quoted} FETCH FIRST {limit} ROWS ONLY",
                    f"SELECT * FROM {quoted} WHERE ROWNUM <= {limit}",
                ]
            # postgres / mysql / sqlite / everything else
            return [
                f"SELECT * FROM {quoted} LIMIT {limit}",
                f"SELECT * FROM {table} LIMIT {limit}",
            ]

        self.row_counts = {}
        self.sample_rows = {}
        try:
            with engine.connect() as conn:
                for t in self.tables:
                    # Row count (quoted identifier first, unquoted fallback)
                    try:
                        cnt = conn.execute(sql_text(f"SELECT COUNT(*) FROM {q(t)}")).scalar()
                        self.row_counts[t] = int(cnt) if cnt is not None else 0
                    except Exception:
                        try:
                            cnt = conn.execute(sql_text(f"SELECT COUNT(*) FROM {t}")).scalar()
                            self.row_counts[t] = int(cnt) if cnt is not None else 0
                        except Exception:
                            self.row_counts[t] = 0
                    # Sample rows (first successful candidate wins)
                    if self.row_counts.get(t, 0) > 0:
                        for sql_c in sample_candidates(t):
                            try:
                                result = conn.execute(sql_text(sql_c))
                                rows = result.mappings().all()
                                self.sample_rows[t] = [
                                    {k: _json_safe(v) for k, v in dict(r).items()} for r in rows
                                ]
                                break
                            except Exception:
                                continue
                        else:
                            self.sample_rows[t] = []
                    else:
                        self.sample_rows[t] = []
        except Exception as e:
            print(f"[SCHEMA-PROFILE] row stats collection failed: {e}")

    # ------------------------------------------------------------------
    # Digest for LLM prompts
    # ------------------------------------------------------------------
    def build_digest(self, max_tables: Optional[int] = None) -> str:
        max_tables = max_tables or self.MAX_DIGEST_TABLES
        lines: List[str] = []
        lines.append(f"Database schema name: {self.name}")
        lines.append(f"Number of tables/views: {len(self.tables)}")
        lines.append("Tables and columns:")
        for t in self.tables[:max_tables]:
            cols = self.columns_by_table.get(t, [])
            col_parts = []
            for c in cols[: self.MAX_DIGEST_COLUMNS]:
                ctype = str(c.get("type", ""))[:24]
                col_parts.append(f"{c.get('name')} {ctype}".strip())
            colstr = ", ".join(col_parts) if col_parts else "(no columns)"
            rc = self.row_counts.get(t)
            lines.append(f"  - {t} ({colstr})  [rows={rc if rc is not None else '?'}]")
            fks = self.fk_by_table.get(t, [])
            for fk in fks[:4]:
                ref = fk.get("referred_table")
                lcols = fk.get("constrained_columns") or []
                rcols = fk.get("referred_columns") or []
                if ref:
                    lines.append(f"      FK: {lcols} -> {ref}({rcols})")
            if len(cols) > self.MAX_DIGEST_COLUMNS:
                lines.append(f"      ... {len(cols) - self.MAX_DIGEST_COLUMNS} more columns")
        if len(self.tables) > max_tables:
            lines.append(f"  ... {len(self.tables) - max_tables} more tables")
        return "\n".join(lines)

    def summarize(self) -> Dict[str, Any]:
        """Compact JSON-friendly summary (for API responses / UI)."""
        return {
            "name": self.name,
            "db_url": self.db_url,
            "is_demo": self.is_demo,
            "is_employee_db": self.is_employee_db,
            "notes": self.notes,
            "tables": [
                {
                    "name": t,
                    "row_count": self.row_counts.get(t),
                    "columns": [c.get("name") for c in self.columns_by_table.get(t, [])],
                    "sample_rows": self.sample_rows.get(t, []),
                }
                for t in self.tables
            ],
        }


def _json_safe(v: Any) -> Any:
    try:
        json.dumps(v)
        return v
    except (TypeError, ValueError):
        return str(v)


class ProfileRegistry:
    """
    Registry of user-registered database profiles.

    Each registry entry stores lightweight metadata (id, name, db_url, kind).
    Full introspection happens lazily when a profile is switched to (the
    agent's initialize_database walks the schema at that moment).
    """

    def __init__(self, store_path: str):
        self.store_path = store_path
        self._lock = threading.Lock()
        self.profiles: Dict[str, Dict[str, Any]] = {}
        self.active_id: Optional[str] = None
        self.load()

    # ------------------------------------------------------------------
    def load(self) -> None:
        try:
            if os.path.exists(self.store_path):
                with open(self.store_path, "r", encoding="utf-8") as fh:
                    data = json.load(fh)
                self.profiles = data.get("profiles", {})
                self.active_id = data.get("active_id")
        except Exception as e:
            print(f"[PROFILE-REGISTRY] load failed: {e}")
            self.profiles = {}
            self.active_id = None

    def save(self) -> None:
        with self._lock:
            try:
                os.makedirs(os.path.dirname(self.store_path), exist_ok=True)
                with open(self.store_path, "w", encoding="utf-8") as fh:
                    json.dump(
                        {"profiles": self.profiles, "active_id": self.active_id},
                        fh,
                        indent=2,
                    )
            except Exception as e:
                print(f"[PROFILE-REGISTRY] save failed: {e}")

    # ------------------------------------------------------------------
    def list(self) -> List[Dict[str, Any]]:
        out = []
        for pid, p in self.profiles.items():
            out.append(
                {
                    "id": pid,
                    "name": p.get("name"),
                    "kind": p.get("kind", "user"),
                    "db_url": p.get("db_url"),
                    "created_at": p.get("created_at"),
                    "notes": p.get("notes", ""),
                    "active": pid == self.active_id,
                }
            )
        return sorted(out, key=lambda p: (not p["active"], p.get("created_at") or 0))

    def get(self, profile_id: str) -> Optional[Dict[str, Any]]:
        return self.profiles.get(profile_id)

    def active(self) -> Optional[Dict[str, Any]]:
        if self.active_id and self.active_id in self.profiles:
            return {"id": self.active_id, **self.profiles[self.active_id]}
        return None

    def add(
        self,
        name: str,
        db_url: str,
        kind: str = "user",
        activate: bool = True,
        notes: Optional[str] = None,
    ) -> Dict[str, Any]:
        pid = uuid.uuid4().hex[:12]
        entry = {
            "name": name,
            "db_url": db_url,
            "kind": kind,
            "created_at": time.time(),
            "notes": (notes or "").strip(),
        }
        with self._lock:
            self.profiles[pid] = entry
            if activate or self.active_id is None:
                self.active_id = pid
        self.save()
        return {"id": pid, **entry}

    def remove(self, profile_id: str) -> bool:
        with self._lock:
            if profile_id not in self.profiles:
                return False
            del self.profiles[profile_id]
            if self.active_id == profile_id:
                self.active_id = next(iter(self.profiles), None)
        self.save()
        return True

    def switch(self, profile_id: str) -> Optional[Dict[str, Any]]:
        with self._lock:
            if profile_id not in self.profiles:
                return None
            self.active_id = profile_id
        self.save()
        return {"id": profile_id, **self.profiles[profile_id]}

    def set_notes(self, profile_id: str, notes: str) -> bool:
        """Persist owner-supplied schema notes (domain hints) for a profile."""
        with self._lock:
            if profile_id not in self.profiles:
                return False
            self.profiles[profile_id]["notes"] = (notes or "").strip()
        self.save()
        return True