File size: 26,057 Bytes
a252004
ddb14f5
c7293c2
dd6bafd
447c15c
8b347b0
c7293c2
14352db
 
01da95c
447c15c
1a54e0b
c7293c2
5fd1071
e6e1d37
1a54e0b
25df053
c7293c2
9fa5636
 
55ba9e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9fa5636
 
f454973
e72db51
 
 
 
 
 
 
 
 
 
 
 
3f2fcc0
7279c20
 
c7293c2
e72db51
 
 
 
 
66e7c4d
 
 
 
e72db51
f454973
e72db51
535efdd
9fa5636
535efdd
9fa5636
 
 
d4ac652
 
 
 
 
 
 
 
 
 
 
 
 
9fa5636
d4ac652
 
 
 
 
 
 
 
9fa5636
d4ac652
 
 
9fa5636
e72db51
8b347b0
 
d4ac652
535efdd
 
 
8b347b0
 
 
 
 
 
 
 
 
 
d4ac652
e72db51
9fa5636
c7293c2
535efdd
 
 
9fa5636
535efdd
c7293c2
9fa5636
 
 
 
c7293c2
 
9fa5636
c7293c2
14352db
e72db51
0040505
cf59f92
11a0f16
 
 
 
 
 
 
 
 
c7293c2
 
 
 
 
 
 
 
 
 
 
 
 
 
538e3cc
 
c7293c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f454973
 
9acabb0
f454973
c7293c2
9acabb0
 
 
c7293c2
9acabb0
0a42fdb
c7293c2
 
f454973
9acabb0
c7293c2
f454973
9acabb0
c7293c2
 
 
 
 
 
 
 
 
 
 
 
 
 
9acabb0
c7293c2
9acabb0
 
 
 
 
 
 
c7293c2
 
 
 
 
fd61d49
3ad4750
c7293c2
 
3ad4750
c7293c2
 
 
 
3ad4750
c7293c2
 
 
e72db51
c7293c2
f454973
c7293c2
 
 
 
 
9acabb0
3ad4750
 
 
c7293c2
 
 
 
f454973
c7293c2
 
 
 
 
f454973
c7293c2
 
 
 
f454973
 
 
 
c7293c2
 
 
 
 
5fd1071
c7293c2
f454973
c7293c2
 
 
 
 
 
f454973
c7293c2
 
 
 
 
f454973
 
c7293c2
 
 
ddb14f5
 
 
5fd1071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7293c2
 
 
5fd1071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7293c2
 
f454973
c7293c2
 
01da95c
c7293c2
 
5fd1071
 
 
 
 
 
c7293c2
f454973
 
c7293c2
 
f454973
c7293c2
 
 
 
 
 
ddb14f5
 
 
 
 
 
f454973
ddb14f5
c7293c2
14352db
5fd1071
c7293c2
 
5fd1071
 
 
 
 
 
 
 
 
 
 
 
 
25df053
c7293c2
f454973
c7293c2
 
5fd1071
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7293c2
 
f454973
c7293c2
 
 
f454973
 
c7293c2
 
ddb14f5
c7293c2
ddb14f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7293c2
 
 
f454973
 
c7293c2
25df053
c7293c2
 
 
 
 
f454973
c7293c2
 
 
 
 
 
 
 
f454973
c7293c2
269cca5
 
 
 
 
 
 
 
 
 
25df053
c7293c2
 
 
 
f454973
c7293c2
 
 
 
 
ddb14f5
 
 
21a66b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddb14f5
c7293c2
 
 
21a66b7
c7293c2
 
 
25df053
c7293c2
 
538e3cc
 
 
c7293c2
 
 
f454973
c7293c2
f4dc293
c7293c2
 
f454973
c7293c2
3569e8a
f454973
25df053
f454973
25df053
 
c7293c2
 
 
 
 
 
 
31c2359
 
c7293c2
31c2359
25df053
c7293c2
 
25df053
3569e8a
f454973
c7293c2
f4dc293
25df053
 
c7293c2
f4dc293
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
"""
Flare – Chat Handler  (v1.7 · parameter parsing düzeltmesi)
==========================================
"""

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

from prompt_builder import build_intent_prompt, build_parameter_prompt, build_smart_parameter_question_prompt, extract_params_from_question
from utils import log
from api_executor import call_api as execute_api  
from validation_engine import validate
from session import session_store, Session
from llm_interface import LLMInterface, SparkLLM, GPT4oLLM

# ─────────────────────────  CONFIG ───────────────────────── #
# Lazy loading for config
_cfg = None

def get_config():
    """Get or reload config"""
    global _cfg
    if _cfg is None:
        from config_provider import ConfigProvider
        _cfg = ConfigProvider.get()
    return _cfg

def reload_config():
    """Force reload config"""
    global _cfg
    from config_provider import ConfigProvider
    ConfigProvider._instance = None
    _cfg = ConfigProvider.get()
    return _cfg

# Global LLM instance
llm_provider: Optional[LLMInterface] = 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
    
# ─────────────────────────  SPARK ───────────────────────── #
def initialize_llm(force_reload=False):
    """Initialize LLM provider based on work_mode"""
    global llm_provider
    
    # Get fresh config if forced or first time
    if force_reload:
        cfg = reload_config()
    else:
        cfg = get_config()
    
    work_mode = cfg.global_config.work_mode
    
    if cfg.global_config.is_gpt_mode():
        # GPT mode
        api_key = cfg.global_config.get_plain_token()
        if not api_key:
            raise ValueError("OpenAI API key not configured")
        
        model = cfg.global_config.get_gpt_model()
        llm_provider = GPT4oLLM(api_key, model)
        log(f"✅ Initialized {model} provider")
    else:
        # Spark mode
        spark_token = _get_spark_token()
        if not spark_token:
            raise ValueError("Spark token not configured")
            
        spark_endpoint = str(cfg.global_config.spark_endpoint)
        llm_provider = SparkLLM(spark_endpoint, spark_token)
        log("✅ Initialized Spark provider")

# ─────────────────────────  SPARK ───────────────────────── #
def _get_spark_token() -> Optional[str]:
    """Get Spark token based on work_mode"""
    cfg = get_config()
    work_mode = cfg.global_config.work_mode
    
    if work_mode in ("hfcloud", "cloud"):
        # Cloud mode - use HuggingFace Secrets
        token = os.getenv("SPARK_TOKEN")
        if not token:
            log("❌ SPARK_TOKEN not found in HuggingFace Secrets!")
        return token
    else:
        # On-premise mode - use .env file
        from dotenv import load_dotenv
        load_dotenv()
        return os.getenv("SPARK_TOKEN")

async def spark_generate(s: Session, prompt: str, user_msg: str) -> str:
    """Call LLM provider with proper error handling"""
    try:
        # Always reinitialize to get fresh config
        initialize_llm(force_reload=True)
        
        if not llm_provider:
            raise ValueError("Failed to initialize LLM provider")
        
        # Use the abstract interface
        raw = await llm_provider.generate(prompt, user_msg, s.chat_history)
        log(f"🪄 LLM raw response: {raw[:120]!r}")
        return raw
        
    except Exception as e:
        log(f"❌ LLM error: {e}")
        raise

# ─────────────────────────  FASTAPI ───────────────────────── #
router = APIRouter()

@router.get("/health")
def health_check():
    """Health check endpoint for monitoring"""
    return {
        "status": "ok",
        "sessions": len(session_store._sessions),
        "timestamp": datetime.now().isoformat()
    }

class StartRequest(BaseModel): 
    project_name: str

class ChatRequest(BaseModel):  
    user_input: str

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

@router.post("/start_session", response_model=ChatResponse)
async def start_session(req: StartRequest):
    """Create new session"""
    try:
        cfg = get_config()

        # 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")
        
        # Create session
        session = session_store.create_session(req.project_name)
        greeting = "Hoş geldiniz! Size nasıl yardımcı olabilirim?"
        session.add_turn("assistant", greeting)
        
        return ChatResponse(session_id=session.session_id, answer=greeting)
        
    except Exception as e:
        log(f"❌ Error creating session: {e}")
        raise HTTPException(500, str(e))

@router.post("/chat", response_model=ChatResponse)
async def chat(body: ChatRequest, x_session_id: str = Header(...)):
    """Process chat message"""
    try:
        # Get session
        session = session_store.get_session(x_session_id)
        if not session:
            raise HTTPException(404, "Session not found")
        
        user_input = body.user_input.strip()
        if not user_input:
            raise HTTPException(400, "Empty message")
            
        log(f"💬 User input: {user_input}")
        log(f"📊 Session state: {session.state}, last_intent: {session.last_intent}")
        log(f"📊 Session version: {session.version_number}")
        
        session.add_turn("user", user_input)
        
        # Get version config from session
        version = session.get_version_config()
        if not version:
            raise HTTPException(500, "Version configuration lost")

        # Handle based on state
        if session.state == "await_param":
            log(f"🔄 Handling parameter followup for missing: {session.awaiting_parameters}")
            answer = await _handle_parameter_followup(session, user_input)  # version parametresi kaldırıldı
        else:
            log("🆕 Handling new message")
            answer = await _handle_new_message(session, user_input)  # version parametresi kaldırıldı
        
        session.add_turn("assistant", answer)
        return ChatResponse(session_id=session.session_id, answer=answer)
        
    except HTTPException:
        raise
    except Exception as e:
        log(f"❌ Chat error: {e}")
        session.reset_flow()
        error_msg = "Bir hata oluştu. Lütfen tekrar deneyin."
        session.add_turn("assistant", error_msg)
        return ChatResponse(session_id=x_session_id, answer=error_msg)

# ─────────────────────────  MESSAGE HANDLERS ───────────────────────── #
async def _handle_new_message(session: Session, user_input: str) -> str:
    """Handle new message (not parameter followup)"""
    
    # Get version config from session
    version = session.get_version_config()
    if not version:
        log("❌ Version config not found")
        return "Bir hata oluştu. Lütfen tekrar deneyin."
    
    # Build intent detection prompt
    prompt = build_intent_prompt(
        version.general_prompt,
        session.chat_history,
        user_input,
        version.intents,
        session.project_name
    )
    
    # Get LLM response
    raw = await spark_generate(session, prompt, user_input)
    
    # Empty response fallback
    if not raw:
        log("⚠️ Empty response from LLM")
        return "Üzgünüm, mesajınızı anlayamadım. Lütfen tekrar dener misiniz?"
    
    # Check for intent
    if not raw.startswith("#DETECTED_INTENT"):
        # Small talk response
        log("💬 No intent detected, returning small talk")
        return _trim_response(raw)
    
    # Parse intent
    intent_name, tail = _safe_intent_parse(raw)
    
    # Validate intent against version's intents
    valid_intents = {intent.name for intent in version.intents}
    if intent_name not in valid_intents:
        log(f"⚠️ Invalid intent: {intent_name} (valid: {valid_intents})")
        return _trim_response(tail) if tail else "Size nasıl yardımcı olabilirim?"
    
    # Short message guard (less than 3 words usually means incomplete request)
    if len(user_input.split()) < 3 and intent_name != "flight-info":
        log(f"⚠️ Message too short ({len(user_input.split())} words) for intent {intent_name}")
        return _trim_response(tail) if tail else "Lütfen talebinizi biraz daha detaylandırır mısınız?"
    
    # Find intent config
    intent_config = next((i for i in version.intents if i.name == intent_name), None)
    if not intent_config:
        log(f"❌ Intent config not found for: {intent_name}")
        return "Üzgünüm, bu işlemi gerçekleştiremiyorum."
    
    # Set intent in session
    session.last_intent = intent_name
    log(f"✅ Intent set: {intent_name}")
    
    # Log intent parameters
    log(f"📋 Intent parameters: {[p.name for p in intent_config.parameters]}")
    
    # Extract parameters
    return await _extract_parameters(session, intent_config, user_input)

async def _handle_parameter_followup(session: Session, user_input: str, version) -> str:
    """Handle parameter collection followup with smart question generation"""
    if not session.last_intent:
        log("⚠️ No last intent in session")
        session.reset_flow()
        return "Üzgünüm, hangi işlem için bilgi istediğimi unuttum. Baştan başlayalım."
    
    # Get intent config
    intent_config = next((i for i in version.intents if i.name == session.last_intent), None)
    if not intent_config:
        log(f"❌ Intent config not found for: {session.last_intent}")
        session.reset_flow()
        return "Bir hata oluştu. Lütfen tekrar deneyin."
    
    # Try to extract missing parameters
    missing = session.awaiting_parameters
    log(f"🔍 Trying to extract missing params: {missing}")
    
    prompt = build_parameter_prompt(intent_config, missing, user_input, session.chat_history)
    raw = await spark_generate(session, prompt, user_input)
    
    # Try parsing with or without #PARAMETERS: prefix
    success = _process_parameters(session, intent_config, raw)
    
    # Hangi parametreler hala eksik?
    still_missing = _get_missing_parameters(session, intent_config)
    
    # Sorulan ama cevaplanmayan parametreleri belirle
    asked_but_not_answered = []
    for param in session.awaiting_parameters:
        if param in still_missing:
            asked_but_not_answered.append(param)
    
    # Cevaplanmayanları session'a kaydet
    if asked_but_not_answered:
        session.mark_parameters_unanswered(asked_but_not_answered)
        log(f"❓ Parameters not answered: {asked_but_not_answered}")
    
    # Cevaplananları işaretle
    for param in session.awaiting_parameters:
        if param not in still_missing:
            session.mark_parameter_answered(param)
            log(f"✅ Parameter answered: {param}")
    
    if still_missing:
        # Maksimum deneme kontrolü
        if session.missing_ask_count >= 3:
            session.reset_flow()
            return "Üzgünüm, istediğiniz bilgileri anlayamadım. Başka bir konuda yardımcı olabilir miyim?"
        
        session.missing_ask_count += 1
        
        # Akıllı soru üret
        question = await _generate_smart_parameter_question(
            session, intent_config, still_missing, version
        )
        
        # Sorulan parametreleri tahmin et ve kaydet
        params_in_question = extract_params_from_question(question, still_missing, intent_config)
        session.record_parameter_question(params_in_question)
        session.awaiting_parameters = params_in_question
        
        log(f"🤖 Smart question generated for params: {params_in_question}")
        return question
    
    # All parameters collected, call API
    log("✅ All parameters collected, calling API")
    session.state = "call_api"
    return await _execute_api_call(session, intent_config)

# ─────────────────────────  PARAMETER HANDLING ───────────────────────── #
async def _extract_parameters(session: Session, intent_config, user_input: str) -> str:
    """Extract parameters from user input with smart question generation"""
    
    # Yeni intent için parametre takibini sıfırla
    if session.parameter_ask_rounds == 0:
        session.reset_parameter_tracking()
    
    missing = _get_missing_parameters(session, intent_config)
    log(f"🔍 Missing parameters: {missing}")
    
    if not missing:
        # All parameters already available
        log("✅ All parameters already available")
        return await _execute_api_call(session, intent_config)
    
    # Build parameter extraction prompt
    prompt = build_parameter_prompt(intent_config, missing, user_input, session.chat_history)
    raw = await spark_generate(session, prompt, user_input)
    
    # Try processing with flexible parsing
    success = _process_parameters(session, intent_config, raw)
    
    if success:
        missing = _get_missing_parameters(session, intent_config)
        log(f"📊 After extraction, missing: {missing}")
    else:
        log("⚠️ Failed to extract parameters from response")
    
    if missing:
        # Still missing parameters - generate smart question
        session.state = "await_param"
        session.missing_ask_count = 0
        
        # Akıllı soru üret
        question = await _generate_smart_parameter_question(
            session, intent_config, missing, version
        )
        
        # Sorulan parametreleri tahmin et ve kaydet
        params_in_question = extract_params_from_question(question, missing, intent_config)
        session.record_parameter_question(params_in_question)
        session.awaiting_parameters = params_in_question
        
        log(f"🤖 Smart question generated for initial params: {params_in_question}")
        return question
    
    # All parameters collected
    log("✅ All parameters collected after extraction")
    return await _execute_api_call(session, intent_config)

async def _generate_smart_parameter_question(
    session: Session, 
    intent_config,
    missing_params: List[str],
    version
) -> str:
    """LLM kullanarak doğal parametre sorusu üret"""
    
    # Config'i al
    collection_config = cfg.global_config.parameter_collection_config
    
    # Öncelik sıralaması: önce cevaplanmayanlar
    prioritized_params = []
    
    # 1. Daha önce sorulup cevaplanmayanlar
    for param in session.unanswered_parameters:
        if param in missing_params:
            prioritized_params.append(param)
            log(f"🔝 Priority param (unanswered): {param}")
    
    # 2. Hiç sorulmamışlar
    for param in missing_params:
        if param not in prioritized_params:
            prioritized_params.append(param)
            log(f"➕ Additional param (not asked): {param}")
    
    # Maksimum parametre sayısını belirle
    max_params = min(
        len(prioritized_params),
        collection_config.max_params_per_question
    )
    params_to_ask = prioritized_params[:max_params]
    
    log(f"📋 Params to ask in this round: {params_to_ask}")
    
    # Proje dilini belirle
    project_language = "Turkish"  # Default
    if hasattr(version, 'project') and hasattr(version.project, 'default_language'):
        lang_map = {
            "tr": "Turkish", "en": "English", "de": "German",
            "fr": "French", "es": "Spanish"
        }
        project_language = lang_map.get(version.project.default_language, "Turkish")
    
    # Prompt oluştur
    prompt = build_smart_parameter_question_prompt(
        collection_config,
        intent_config,
        params_to_ask,  # Sadece bu turda sorulacak parametreler
        session,
        project_language
    )
    
    # LLM'den soru üret
    response = await spark_generate(session, prompt, "")
    
    # Güvenlik: Eğer LLM boş veya hatalı response verirse fallback
    if not response or len(response.strip()) < 10:
        log("⚠️ Empty or invalid response from LLM, using fallback")
        # En yüksek öncelikli parametre için fallback soru
        param = params_to_ask[0]
        param_config = next(p for p in intent_config.parameters if p.name == param)
        
        # Parametrenin kaç kez sorulduğuna göre farklı fallback
        ask_count = session.get_parameter_ask_count(param)
        if ask_count == 0:
            return f"{param_config.caption} bilgisini alabilir miyim?"
        elif ask_count == 1:
            return f"Lütfen {param_config.caption.lower()} bilgisini paylaşır mısınız?"
        else:
            return f"Devam edebilmem için {param_config.caption.lower()} bilgisine ihtiyacım var."
    
    # Response'u temizle
    clean_response = response.strip()
    
    # Eğer response yanlışlıkla başka şeyler içeriyorsa temizle
    if "#" in clean_response or "assistant:" in clean_response.lower():
        # İlk satırı al
        clean_response = clean_response.split('\n')[0].strip()
    
    log(f"💬 Generated smart question: {clean_response[:100]}...")
    return clean_response

def _get_missing_parameters(session: Session, intent_config) -> List[str]:
    """Get list of missing required parameters"""
    missing = [
        p.name for p in intent_config.parameters 
        if p.required and p.variable_name not in session.variables
    ]
    log(f"📊 Session variables: {list(session.variables.keys())}")
    return missing

def _process_parameters(session: Session, intent_config, raw: str) -> bool:
    """Process parameter extraction response with flexible parsing"""
    try:
        # Try to parse JSON, handling both with and without #PARAMETERS: prefix
        json_str = raw
        if raw.startswith("#PARAMETERS:"):
            json_str = raw[len("#PARAMETERS:"):]
            log(f"🔍 Found #PARAMETERS: prefix, removing it")
        
        # Clean up any trailing content after JSON
        # Find the closing brace for the JSON object
        brace_count = 0
        json_end = -1
        in_string = False
        escape_next = False
        
        for i, char in enumerate(json_str):
            if escape_next:
                escape_next = False
                continue
                
            if char == '\\':
                escape_next = True
                continue
                
            if char == '"' and not escape_next:
                in_string = not in_string
                continue
                
            if not in_string:
                if char == '{':
                    brace_count += 1
                elif char == '}':
                    brace_count -= 1
                    if brace_count == 0:
                        json_end = i + 1
                        break
        
        if json_end > 0:
            json_str = json_str[:json_end]
            log(f"🔍 Cleaned JSON string: {json_str[:200]}")
        
        data = json.loads(json_str)
        
        extracted = data.get("extracted", [])
        log(f"📦 Extracted data: {extracted}")
        
        any_valid = False
        
        for param_data in extracted:
            param_name = param_data.get("name")
            param_value = param_data.get("value")
            
            if not param_name or not param_value:
                log(f"⚠️ Invalid param data: {param_data}")
                continue
            
            # 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

            # Date tipi için özel kontrol
            if param_config.type == "date":
                try:
                    # ISO format kontrolü
                    from datetime import datetime
                    datetime.strptime(str(param_value), "%Y-%m-%d")
                except ValueError:
                    log(f"❌ Invalid date format for {param_name}: {param_value}")
                    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]}")
        
        # Fallback: Try to extract simple values from user input
        # This is especially useful for single parameter responses
        if session.state == "await_param" and len(session.awaiting_parameters) > 0:
            # Get the first missing parameter
            first_missing = session.awaiting_parameters[0]
            param_config = next(
                (p for p in intent_config.parameters if p.name == first_missing),
                None
            )
            
            if param_config and session.chat_history:
                # Get the last user input
                last_user_input = session.chat_history[-1].get("content", "").strip()
                
                # For simple inputs like city names, try direct assignment
                if param_config.type in ["str", "string"] and len(last_user_input.split()) <= 3:
                    if validate(last_user_input, param_config):
                        session.variables[param_config.variable_name] = last_user_input
                        log(f"✅ Fallback extraction: {first_missing}={last_user_input}")
                        return True
        
        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

        cfg = get_config()

        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 spark_generate(session, prompt, json.dumps(api_json))
            # Trim response to remove any trailing "assistant" artifacts
            trimmed_response = _trim_response(human_response)
            session.reset_flow()
            return trimmed_response if trimmed_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."