File size: 16,998 Bytes
a252004
4966ebb
c7293c2
dd6bafd
447c15c
4966ebb
14352db
4966ebb
01da95c
447c15c
1a54e0b
c7293c2
4966ebb
e6e1d37
1a54e0b
4966ebb
25df053
c7293c2
341a67b
4966ebb
 
341a67b
4966ebb
 
 
f454973
e72db51
 
 
 
 
 
 
 
 
 
 
 
3f2fcc0
7279c20
 
c7293c2
e72db51
 
 
 
 
66e7c4d
 
 
 
e72db51
f454973
e72db51
341a67b
 
 
4966ebb
9fa5636
 
b47d24c
4966ebb
b47d24c
 
 
 
 
d4ac652
4966ebb
b47d24c
 
4966ebb
 
 
 
 
c7293c2
9acabb0
4966ebb
c7293c2
4966ebb
 
 
 
 
 
 
c7293c2
4966ebb
 
c7293c2
4966ebb
 
 
 
 
 
341a67b
4966ebb
 
5fd1071
4966ebb
 
 
 
 
 
c7293c2
4966ebb
 
 
c7293c2
4966ebb
 
 
 
 
ddb14f5
4966ebb
 
 
c7293c2
4966ebb
 
f454973
c7293c2
4966ebb
c7293c2
 
 
 
 
4966ebb
c7293c2
f454973
c7293c2
25df053
c7293c2
 
 
 
f454973
c7293c2
 
 
 
 
ddb14f5
 
 
 
c7293c2
 
 
341a67b
c7293c2
 
 
25df053
c7293c2
 
 
 
 
f454973
c7293c2
f4dc293
c7293c2
 
f454973
c7293c2
3569e8a
f454973
25df053
f454973
25df053
 
c7293c2
 
 
 
 
 
4966ebb
c7293c2
341a67b
25df053
c7293c2
 
25df053
3569e8a
f454973
c7293c2
f4dc293
25df053
 
c7293c2
341a67b
 
4966ebb
 
 
 
ec93388
 
 
ae9f986
 
 
 
ec93388
4966ebb
ec93388
 
 
4966ebb
ec93388
 
4966ebb
ec93388
4966ebb
ec93388
 
 
 
 
 
 
 
 
 
 
 
 
 
4966ebb
ec93388
 
 
 
4966ebb
ec93388
4966ebb
ec93388
4966ebb
 
 
 
 
ec93388
4966ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341a67b
 
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
"""
Flare – Chat Handler (Refactored with LLM Factory)
==========================================
"""

import re, json, sys, httpx, os
from datetime import datetime
from typing import Dict, List, Optional, Any
from fastapi import APIRouter, HTTPException, Header
from pydantic import BaseModel
import requests

from prompt_builder import build_intent_prompt, build_parameter_prompt
from utils import log
from api_executor import call_api as execute_api  
from config_provider import ConfigProvider
from validation_engine import validate
from session import session_store, Session

# Initialize router
router = APIRouter()

# ─────────────────────────  GLOBAL VARS ───────────────────────── #
cfg = ConfigProvider.get()
llm_provider = None

# ─────────────────────────  HELPERS ───────────────────────── #
def _trim_response(raw: str) -> str:
    """
    Remove everything after the first logical assistant block or intent tag.
    Also strips trailing 'assistant' artifacts and prompt injections.
    """
    # Stop at our own rules if model leaked them
    for stop in ["#DETECTED_INTENT", "⚠️", "\nassistant", "assistant\n", "assistant"]:
        idx = raw.find(stop)
        if idx != -1:
            raw = raw[:idx]
    # Normalise selamlama
    raw = re.sub(r"Hoş[\s-]?geldin(iz)?", "Hoş geldiniz", raw, flags=re.IGNORECASE)
    return raw.strip()

def _safe_intent_parse(raw: str) -> tuple[str, str]:
    """Extract intent name and extra tail."""
    m = re.search(r"#DETECTED_INTENT:\s*([A-Za-z0-9_-]+)", raw)
    if not m:
        return "", raw
    name = m.group(1)
    # Remove 'assistant' suffix if exists
    if name.endswith("assistant"):
        name = name[:-9]  # Remove last 9 chars ("assistant")
        log(f"πŸ”§ Removed 'assistant' suffix from intent name")
    tail = raw[m.end():]
    log(f"🎯 Parsed intent: {name}")
    return name, tail

# ─────────────────────────  LLM SETUP ───────────────────────── #
def setup_llm_provider():
    """Initialize LLM provider using factory pattern"""
    global llm_provider
    
    try:
        from llm_factory import LLMFactory
        llm_provider = LLMFactory.create_provider()
        log("βœ… LLM provider initialized successfully")
    except Exception as e:
        log(f"❌ Failed to initialize LLM provider: {e}")
        raise

# ─────────────────────────  LLM GENERATION ───────────────────────── #
async def llm_generate(s: Session, prompt: str, user_msg: str) -> str:
    """Call LLM provider with proper error handling"""
    global llm_provider
    
    if llm_provider is None:
        setup_llm_provider()
    
    try:
        # Get version config from session
        version = s.get_version_config()
        if not version:
            # Fallback: get from project config
            project = next((p for p in cfg.projects if p.name == s.project_name), None)
            if not project:
                raise ValueError(f"Project not found: {s.project_name}")
            version = next((v for v in project.versions if v.published), None)
            if not version:
                raise ValueError("No published version found")
        
        log(f"πŸš€ Calling LLM for session {s.session_id[:8]}...")
        log(f"πŸ“‹ Prompt preview (first 200 chars): {prompt[:200]}...")
        
        # Call the configured LLM provider
        raw = await llm_provider.generate(
            user_input=user_msg,
            system_prompt=prompt,
            context=s.chat_history[-10:] if s.chat_history else []
        )
        
        log(f"πŸͺ„ LLM raw response: {raw[:100]}...")
        return raw
        
    except requests.exceptions.Timeout:
        log(f"⏱️ LLM timeout for session {s.session_id[:8]}")
        raise HTTPException(status_code=504, detail="LLM request timed out")
    except Exception as e:
        log(f"❌ LLM error: {e}")
        raise HTTPException(status_code=500, detail=f"LLM error: {str(e)}")

# ─────────────────────────  PARAMETER EXTRACTION ───────────────────────── #
def _extract_parameters_from_response(raw: str, session: Session, intent_config) -> bool:
    """Extract parameters from the LLM response"""
    try:
        # Look for JSON block in response
        json_match = re.search(r'```json\s*(.*?)\s*```', raw, re.DOTALL)
        if not json_match:
            # Try to find JSON without code block
            json_match = re.search(r'\{[^}]+\}', raw)
        
        if not json_match:
            log("❌ No JSON found in response")
            return False
        
        json_str = json_match.group(1) if '```' in raw else json_match.group(0)
        params = json.loads(json_str)
        
        any_valid = False
        for param_name, param_value in params.items():
            # Find parameter config
            param_config = next(
                (p for p in intent_config.parameters if p.name == param_name),
                None
            )
            
            if not param_config:
                log(f"⚠️ Parameter config not found for: {param_name}")
                continue
            
            # Validate parameter
            if validate(str(param_value), param_config):
                session.variables[param_config.variable_name] = str(param_value)
                any_valid = True
                log(f"βœ… Extracted {param_name}={param_value} β†’ {param_config.variable_name}")
            else:
                log(f"❌ Invalid {param_name}={param_value}")
        
        return any_valid
        
    except json.JSONDecodeError as e:
        log(f"❌ JSON parsing error: {e}")
        log(f"❌ Failed to parse: {raw[:200]}")
        return False
    except Exception as e:
        log(f"❌ Parameter processing error: {e}")
        return False

# ─────────────────────────  API EXECUTION ───────────────────────── #
async def _execute_api_call(session: Session, intent_config) -> str:
    """Execute API call and return humanized response"""
    try:
        session.state = "call_api"
        api_name = intent_config.action
        api_config = cfg.get_api(api_name)
        
        if not api_config:
            log(f"❌ API config not found: {api_name}")
            session.reset_flow()
            return intent_config.fallback_error_prompt or "İşlem başarısız oldu."
        
        log(f"πŸ“‘ Calling API: {api_name}")
        log(f"πŸ“¦ API variables: {session.variables}")
        
        # Execute API call with session
        response = execute_api(api_config, session)
        api_json = response.json()
        log(f"βœ… API response: {api_json}")
        
        # Humanize response
        session.state = "humanize"
        if api_config.response_prompt:
            prompt = api_config.response_prompt.replace(
                "{{api_response}}", 
                json.dumps(api_json, ensure_ascii=False)
            )
            human_response = await llm_generate(session, prompt, json.dumps(api_json))
            session.reset_flow()
            return human_response if human_response else f"İşlem sonucu: {api_json}"
        else:
            session.reset_flow()
            return f"İşlem tamamlandı: {api_json}"
            
    except requests.exceptions.Timeout:
        log(f"⏱️ API timeout: {api_name}")
        session.reset_flow()
        return intent_config.fallback_timeout_prompt or "İşlem zaman aşımına uğradı."
    except Exception as e:
        log(f"❌ API call error: {e}")
        session.reset_flow()
        return intent_config.fallback_error_prompt or "İşlem sırasında bir hata oluştu."

# ─────────────────────────  REQUEST MODELS ───────────────────────── #
class ChatRequest(BaseModel):
    message: str

class StartRequest(BaseModel): 
    project_name: str
    version_number: Optional[int] = None  # Opsiyonel, belirtilmezse published olan en bΓΌyΓΌk version no'yu kullan

class ChatResponse(BaseModel):  
    session_id: str
    answer: str
    
# ─────────────────────────  API ENDPOINTS ───────────────────────── #
@router.post("/start_session", response_model=ChatResponse)
async def start_session(req: StartRequest):
    """Create new session"""
    try:
        # Validate project exists
        project = next((p for p in cfg.projects if p.name == req.project_name and p.enabled), None)
        if not project:
            raise HTTPException(404, f"Project '{req.project_name}' not found or disabled")
        
        # Find version
        if req.version_number:
            # Specific version requested
            version = next((v for v in project.versions if v.id == req.version_number), None)
            if not version:
                raise HTTPException(404, f"Version {req.version_number} not found for project '{req.project_name}'")
        else:
            # Find published version with highest version number
            published_versions = [v for v in project.versions if v.published]
            if not published_versions:
                raise HTTPException(404, f"No published version for project '{req.project_name}'")
            
            # Sort by version number (id) and get the highest
            version = max(published_versions, key=lambda v: v.id)
        
        # Create session with version config
        session = session_store.create_session(req.project_name, version)
        greeting = "Hoş geldiniz! Size nasıl yardımcı olabilirim?"
        session.add_turn("assistant", greeting)
        
        log(f"βœ… Session created for project '{req.project_name}' version {version.id} (highest published)")
        
        return ChatResponse(session_id=session.session_id, answer=greeting)
        
    except HTTPException:
        raise
    except Exception as e:
        log(f"❌ Session creation error: {e}")
        raise HTTPException(500, f"Session creation failed: {str(e)}")

@router.post("/chat")
async def chat(req: ChatRequest, x_session_id: str = Header(...)):
    """Process chat message"""
    try:
        # Get session
        session = session_store.get(x_session_id)
        if not session:
            raise HTTPException(status_code=404, detail="Session not found or expired")
        
        # Add user message to history
        session.add_message("user", req.message)
        log(f"πŸ’¬ User [{session.session_id[:8]}...]: {req.message}")
        
        # Get project and version config
        project = next((p for p in cfg.projects if p.name == session.project_name), None)
        if not project:
            raise HTTPException(status_code=404, detail=f"Project '{session.project_name}' not found")
        
        version = session.get_version_config()
        if not version:
            raise HTTPException(status_code=400, detail="Version config not found in session")
        
        # Process based on current state
        if session.state == "idle":
            # Build intent detection prompt
            prompt = build_intent_prompt(version, session.chat_history, project.default_locale)
            raw = await llm_generate(session, prompt, req.message)
            
            # Check for intent
            intent_name, tail = _safe_intent_parse(raw)
            
            if intent_name:
                # Find intent config
                intent_config = next((i for i in version.intents if i.name == intent_name), None)
                
                if intent_config:
                    session.current_intent = intent_name
                    session.intent_config = intent_config
                    session.state = "collect_params"
                    log(f"🎯 Intent detected: {intent_name}")
                    
                    # Check if parameters were already extracted
                    if tail and _extract_parameters_from_response(tail, session, intent_config):
                        log("πŸ“¦ Some parameters extracted from initial response")
                    
                    # Check what parameters are missing
                    missing_params = [
                        p for p in intent_config.parameters
                        if p.required and p.variable_name not in session.variables
                    ]
                    
                    if not missing_params:
                        # All required parameters collected, execute API
                        response = await _execute_api_call(session, intent_config)
                        session.add_message("assistant", response)
                        return {"response": response, "intent": intent_name, "state": "completed"}
                    else:
                        # Need to collect more parameters
                        param_prompt = build_parameter_prompt(
                            intent_config, 
                            session.variables, 
                            session.chat_history,
                            project.default_locale
                        )
                        param_question = await llm_generate(session, param_prompt, req.message)
                        clean_question = _trim_response(param_question)
                        session.add_message("assistant", clean_question)
                        return {"response": clean_question, "intent": intent_name, "state": "collecting_params"}
                else:
                    log(f"⚠️ Unknown intent: {intent_name}")
            
            # No intent detected, return general response
            clean_response = _trim_response(raw)
            session.add_message("assistant", clean_response)
            return {"response": clean_response, "state": "idle"}
            
        elif session.state == "collect_params":
            # Continue parameter collection
            intent_config = session.intent_config
            
            # Try to extract parameters from user message
            param_prompt = f"""
            Extract parameters from user message: "{req.message}"
            
            Expected parameters:
            {json.dumps([{
                'name': p.name,
                'type': p.type,
                'required': p.required,
                'extraction_prompt': p.extraction_prompt
            } for p in intent_config.parameters if p.variable_name not in session.variables], ensure_ascii=False)}
            
            Return as JSON object with parameter names as keys.
            """
            
            raw = await llm_generate(session, param_prompt, req.message)
            _extract_parameters_from_response(raw, session, intent_config)
            
            # Check what parameters are still missing
            missing_params = [
                p for p in intent_config.parameters
                if p.required and p.variable_name not in session.variables
            ]
            
            if not missing_params:
                # All parameters collected, execute API
                response = await _execute_api_call(session, intent_config)
                session.add_message("assistant", response)
                return {"response": response, "intent": session.current_intent, "state": "completed"}
            else:
                # Still need more parameters
                param_prompt = build_parameter_prompt(
                    intent_config, 
                    session.variables, 
                    session.chat_history,
                    project.default_locale
                )
                param_question = await llm_generate(session, param_prompt, req.message)
                clean_question = _trim_response(param_question)
                session.add_message("assistant", clean_question)
                return {"response": clean_question, "intent": session.current_intent, "state": "collecting_params"}
        
        else:
            # Unknown state, reset
            session.reset_flow()
            return {"response": "Bir hata oluştu, lütfen tekrar deneyin.", "state": "error"}
            
    except HTTPException:
        raise
    except Exception as e:
        log(f"❌ Chat error: {e}")
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))

# Initialize LLM on module load
setup_llm_provider()