File size: 1,739 Bytes
0cac9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pure data models exchanged across the core engine boundary.

These carry no rich/duckdb objects so both the CLI adapter and a future web
backend can serialize and render them however they like.

Author: mohamedgamal04
"""

from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path


@dataclass(frozen=True)
class EngineConfig:
    """Everything the engine needs to talk to a provider and find workbooks."""

    base_url: str
    api_key: str
    model: str
    provider_name: str
    system_prompt: str
    excel_dir: Path | None = None
    excel_files_count: int = 1


@dataclass
class StatementResult:
    """Outcome of a single SQL statement: preview rows or an error."""

    sql: str
    kind: str  # select / insert / update / delete / unknown
    prepared_sql: str | None = None
    columns: list[str] = field(default_factory=list)
    rows: list[dict] = field(default_factory=list)
    row_count: int = 0
    truncated: bool = False
    error: str | None = None


@dataclass
class WritebackTarget:
    """A single sheet a DML statement wants to persist back to disk."""

    file_path: Path
    sheet_name: str
    table_name: str
    affected_rows: int
    preview_columns: list[str] = field(default_factory=list)
    preview_rows: list[dict] = field(default_factory=list)


@dataclass
class EngineResult:
    """Full result of one engine run, ready to render or return as JSON."""

    prompt: str
    raw_llm_output: str = ""
    explanation: str = ""
    statements: list[StatementResult] = field(default_factory=list)
    executed: bool = False
    wrote_back: bool = False
    writeback_targets: list[WritebackTarget] = field(default_factory=list)
    error: str | None = None