File size: 8,882 Bytes
a252004
a63397b
 
01da95c
a63397b
dd6bafd
447c15c
a63397b
14352db
 
 
01da95c
447c15c
a63397b
a252004
14352db
 
01da95c
a63397b
01da95c
a63397b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14352db
 
01da95c
cf59f92
14352db
01da95c
c4a5a79
01da95c
14352db
 
 
 
a63397b
c4a5a79
 
14352db
 
 
 
01da95c
cf59f92
01da95c
 
 
 
 
 
 
cf59f92
01da95c
 
14352db
01da95c
14352db
01da95c
cf59f92
 
 
14352db
01da95c
c4a5a79
01da95c
0040505
14352db
c4a5a79
14352db
 
 
 
 
0040505
 
14352db
0040505
447c15c
14352db
 
 
01da95c
 
 
c4a5a79
14352db
 
 
 
 
 
 
c4a5a79
0040505
01da95c
 
 
 
 
0040505
14352db
 
a63397b
14352db
c4a5a79
14352db
 
 
a63397b
14352db
cf59f92
 
 
 
 
 
 
 
 
 
 
 
 
14352db
c4a5a79
14352db
 
 
 
 
 
 
01da95c
cf59f92
01da95c
c4a5a79
cf59f92
 
14352db
0040505
01da95c
 
14352db
c4a5a79
 
14352db
c4a5a79
cf59f92
 
 
c4a5a79
 
01da95c
14352db
c4a5a79
cf59f92
 
c4a5a79
01da95c
c4a5a79
 
 
 
 
 
a63397b
 
cf59f92
c4a5a79
cf59f92
c4a5a79
01da95c
c4a5a79
14352db
c4a5a79
cf59f92
 
c4a5a79
01da95c
c4a5a79
 
14352db
cf59f92
14352db
cf59f92
14352db
0040505
14352db
cf59f92
 
c4a5a79
14352db
c4a5a79
 
14352db
c4a5a79
 
 
14352db
c4a5a79
14352db
0040505
14352db
cf59f92
c4a5a79
14352db
 
c4a5a79
cf59f92
14352db
 
cf59f92
 
14352db
 
c4a5a79
14352db
 
01da95c
14352db
 
01da95c
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
"""
Flare – Chat Handler  (Spark /generate + safe-intent + config-validate)
======================================================================
• X-Session-ID header
• Config JSONC parse hatası -> log + graceful exit
"""

import re, json, uuid, sys, httpx, commentjson
from datetime import datetime
from typing import Dict, List, Optional

from fastapi import APIRouter, HTTPException, Header
from pydantic import BaseModel
from commentjson import JSONLibraryException

from prompt_builder import build_intent_prompt, build_parameter_prompt, log

# --------------------------------------------------------------------------- #
# CONFIG LOADING + VALIDATION
# --------------------------------------------------------------------------- #
def load_config(path: str = "service_config.jsonc") -> dict:
    try:
        with open(path, encoding="utf-8") as f:
            cfg = commentjson.load(f)
        log("✅ service_config.jsonc parsed successfully.")
        return cfg
    except JSONLibraryException as e:
        log("❌ CONFIG PARSE ERROR:")
        log(str(e))
        sys.exit(1)
    except FileNotFoundError:
        log(f"❌ Config file '{path}' not found.")
        sys.exit(1)

CFG = load_config()

PROJECTS = {p["name"]: p for p in CFG["projects"]}
APIS     = {a["name"]: a for a in CFG["apis"]}
SPARK_URL = CFG["config"]["spark_endpoint"].rstrip("/") + "/generate"
ALLOWED_INTENTS = {"flight-booking", "flight-info", "booking-cancel"}

# --------------------------------------------------------------------------- #
# SESSION
# --------------------------------------------------------------------------- #
class Session:
    def __init__(self, project_name: str):
        self.id = str(uuid.uuid4())
        self.project = PROJECTS[project_name]
        self.history: List[Dict[str, str]] = []
        self.variables: Dict[str, str] = {}
        self.awaiting: Optional[Dict] = None
        log(f"🆕 Session {self.id} for {project_name}")

SESSIONS: Dict[str, Session] = {}

# --------------------------------------------------------------------------- #
# SPARK CLIENT
# --------------------------------------------------------------------------- #
async def spark_generate(session: Session,
                         system_prompt: str,
                         user_input: str) -> str:
    payload = {
        "project_name": session.project["name"],
        "user_input": user_input,
        "context": session.history[-10:],   # last 10 turns
        "system_prompt": system_prompt
    }
    async with httpx.AsyncClient(timeout=60) as c:
        r = await c.post(SPARK_URL, json=payload)
        r.raise_for_status()
        data = r.json()
        return (data.get("assistant") or
                data.get("model_answer") or
                data.get("text", "")).strip()

# --------------------------------------------------------------------------- #
# FASTAPI ROUTER
# --------------------------------------------------------------------------- #
router = APIRouter()

@router.get("/")
def health():
    return {"status": "ok"}

class StartSessionRequest(BaseModel):
    project_name: str

class ChatBody(BaseModel):
    user_input: str

class ChatResponse(BaseModel):
    session_id: str
    answer: str

# --------------------------------------------------------------------------- #
# ENDPOINTS
# --------------------------------------------------------------------------- #
@router.post("/start_session", response_model=ChatResponse)
async def start_session(req: StartSessionRequest):
    if req.project_name not in PROJECTS:
        raise HTTPException(404, "Unknown project")
    s = Session(req.project_name)
    SESSIONS[s.id] = s
    return ChatResponse(session_id=s.id, answer="Nasıl yardımcı olabilirim?")

@router.post("/chat", response_model=ChatResponse)
async def chat(body: ChatBody,
               x_session_id: str = Header(...)):
    if x_session_id not in SESSIONS:
        raise HTTPException(404, "Invalid session")

    s = SESSIONS[x_session_id]
    user_msg = body.user_input.strip()
    s.history.append({"role": "user", "content": user_msg})

    # ---------------- Follow-up?
    if s.awaiting:
        answer = await _followup(s, user_msg)
        s.history.append({"role": "assistant", "content": answer})
        return ChatResponse(session_id=s.id, answer=answer)

    # ---------------- Intent detect
    gen_prompt = s.project["versions"][0]["general_prompt"]
    intent_raw = await spark_generate(s, gen_prompt, user_msg)

    if not intent_raw.startswith("#DETECTED_INTENT:"):
        s.history.append({"role": "assistant", "content": intent_raw})
        return ChatResponse(session_id=s.id, answer=intent_raw)

    intent_name = intent_raw.split(":", 1)[1].strip()
    if intent_name not in ALLOWED_INTENTS:
        clean = intent_raw.split("#DETECTED_INTENT")[0].split("\nassistant")[0].strip()
        s.history.append({"role": "assistant", "content": clean})
        return ChatResponse(session_id=s.id, answer=clean)

    intent_cfg = _find_intent(s.project, intent_name)
    if not intent_cfg:
        err = "Üzgünüm, anlayamadım."
        s.history.append({"role": "assistant", "content": err})
        return ChatResponse(session_id=s.id, answer=err)

    answer = await _handle_intent(s, intent_cfg, user_msg)
    s.history.append({"role": "assistant", "content": answer})
    return ChatResponse(session_id=s.id, answer=answer)

# --------------------------------------------------------------------------- #
# HELPER FUNCS
# --------------------------------------------------------------------------- #
def _find_intent(project, name_):
    return next((i for i in project["versions"][0]["intents"]
                 if i["name"] == name_), None)

def _missing(s, intent_cfg):
    return [p["name"] for p in intent_cfg["parameters"]
            if p["variable_name"] not in s.variables]

async def _handle_intent(s, intent_cfg, user_msg):
    missing = _missing(s, intent_cfg)
    if missing:
        p_prompt = build_parameter_prompt(intent_cfg, missing, user_msg, s.history)
        p_raw    = await spark_generate(s, p_prompt, user_msg)
        if p_raw.startswith("#PARAMETERS:"):
            if bad := _process_params(s, intent_cfg, p_raw):
                return bad
            missing = _missing(s, intent_cfg)

    if missing:
        s.awaiting = {"intent": intent_cfg, "missing": missing}
        cap = next(p for p in intent_cfg["parameters"]
                   if p["name"] == missing[0])["caption"]
        return f"{cap} nedir?"

    s.awaiting = None
    return await _call_api(s, intent_cfg)

async def _followup(s, user_msg):
    intent_cfg = s.awaiting["intent"]
    missing    = s.awaiting["missing"]
    p_prompt = build_parameter_prompt(intent_cfg, missing, user_msg, s.history)
    p_raw    = await spark_generate(s, p_prompt, user_msg)
    if not p_raw.startswith("#PARAMETERS:"):
        return "Üzgünüm, anlayamadım."
    if bad := _process_params(s, intent_cfg, p_raw):
        return bad

    missing = _missing(s, intent_cfg)
    if missing:
        s.awaiting["missing"] = missing
        cap = next(p for p in intent_cfg["parameters"]
                   if p["name"] == missing[0])["caption"]
        return f"{cap} nedir?"

    s.awaiting = None
    return await _call_api(s, intent_cfg)

def _process_params(s, intent_cfg, p_raw):
    try:
        data = json.loads(p_raw[len("#PARAMETERS:"):])
    except json.JSONDecodeError:
        return "Parametreleri çözemedim."
    for pair in data.get("extracted", []):
        p_cfg = next(p for p in intent_cfg["parameters"]
                     if p["name"] == pair["name"])
        if not _valid(p_cfg, pair["value"]):
            return p_cfg.get("invalid_prompt", "Geçersiz değer.")
        s.variables[p_cfg["variable_name"]] = pair["value"]
    return None

def _valid(p_cfg, val):
    rx = p_cfg.get("validation_regex")
    return re.match(rx, val) is not None if rx else True

async def _call_api(s, intent_cfg):
    api = APIS[intent_cfg["action"]]
    token = "testtoken"
    headers = {k: v.replace("{{token}}", token) for k, v in api["headers"].items()}

    body = json.loads(json.dumps(api["body_template"]))
    for k, v in body.items():
        if isinstance(v, str) and v.startswith("{{") and v.endswith("}}"):
            body[k] = s.variables.get(v[2:-2], "")

    try:
        async with httpx.AsyncClient(timeout=api["timeout_seconds"]) as c:
            r = await c.request(api["method"], api["url"],
                                headers=headers, json=body)
            r.raise_for_status()
            api_json = r.json()
    except Exception:
        return intent_cfg["fallback_error_prompt"]

    summary_prompt = api["response_prompt"].replace(
        "{{api_response}}", json.dumps(api_json, ensure_ascii=False)
    )
    return await spark_generate(s, summary_prompt, "")