File size: 21,463 Bytes
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c69841
 
63bcd5a
 
46d9d7a
63bcd5a
 
 
 
 
 
 
809b701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63bcd5a
60cb4b3
 
 
63bcd5a
 
 
8463318
809b701
 
 
8463318
809b701
 
 
 
8463318
809b701
 
 
 
8463318
 
809b701
8463318
809b701
 
8463318
 
 
 
809b701
8463318
809b701
63bcd5a
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
4552666
63bcd5a
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809b701
 
 
63bcd5a
 
 
 
 
4552666
63bcd5a
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
809b701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
4552666
63bcd5a
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
809b701
 
 
 
 
 
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
4552666
63bcd5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809b701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63bcd5a
60cb4b3
63bcd5a
 
 
 
 
 
 
 
 
60cb4b3
 
 
 
 
 
 
 
 
cff9d3b
 
809b701
60cb4b3
 
 
 
 
809b701
cff9d3b
 
 
 
 
 
 
 
 
 
60cb4b3
 
cff9d3b
 
 
 
60cb4b3
 
cff9d3b
60cb4b3
 
 
 
809b701
60cb4b3
 
 
 
 
 
cff9d3b
 
809b701
 
60cb4b3
63df0e6
60cb4b3
 
 
 
 
 
 
 
 
 
 
 
 
809b701
60cb4b3
809b701
60cb4b3
 
 
 
 
63bcd5a
60cb4b3
 
 
 
63bcd5a
 
60cb4b3
63bcd5a
60cb4b3
 
 
4552666
60cb4b3
 
 
 
 
 
63bcd5a
60cb4b3
 
809b701
60cb4b3
 
 
63bcd5a
60cb4b3
63bcd5a
 
60cb4b3
63bcd5a
 
 
 
 
60cb4b3
63bcd5a
60cb4b3
 
 
 
 
63bcd5a
60cb4b3
 
 
 
 
 
 
63bcd5a
60cb4b3
4552666
60cb4b3
 
 
63bcd5a
60cb4b3
b09149c
 
 
 
 
60cb4b3
63bcd5a
60cb4b3
 
 
4552666
60cb4b3
 
63bcd5a
 
60cb4b3
 
 
63bcd5a
60cb4b3
63bcd5a
cff9d3b
 
 
60cb4b3
 
 
cff9d3b
 
 
 
60cb4b3
809b701
60cb4b3
 
 
63bcd5a
60cb4b3
63bcd5a
 
809b701
63bcd5a
 
809b701
 
63bcd5a
 
809b701
63bcd5a
809b701
 
63bcd5a
809b701
 
63bcd5a
 
 
 
 
 
 
809b701
 
 
60cb4b3
b09149c
63bcd5a
60cb4b3
63bcd5a
60cb4b3
b09149c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60cb4b3
 
 
 
63bcd5a
60cb4b3
b09149c
 
 
 
 
 
 
 
 
 
 
60cb4b3
 
 
 
4552666
cff9d3b
 
 
 
 
 
 
 
 
 
 
63bcd5a
60cb4b3
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
from src.recommendation_engine.memory_store import (
    get_user_memory,
    save_user_memory,
    default_state
)

from src.recommendation_engine.llm_router import analyze_user_input

from src.recommendation_engine.command_handler import (
    is_command,
    handle_command
)

from src.recommendation_engine.idea_generator import generate_ideas
from src.recommendation_engine.feature_generator import generate_features

from src.recommendation_engine.llm_client import generate_text, generate_list
from src.recommendation_engine.prompt_builder import build_chat_prompt, build_niche_domains_prompt
from src.recommendation_engine.response_formatter import format_response
from src.recommendation_engine.state_manager import update_state
from src.recommendation_engine.context_builder import extract_domain, DOMAIN_KEYWORDS

from src.recommendation_engine.full_project_generator import (
    generate_full_project
)

import re

# ─────────────────────────────────────────────
#  Project Idea Validator + Categorizer
# ─────────────────────────────────────────────
def validate_and_categorize_project(title: str, abstract: str = "") -> dict:
    """
    Uses Gemini to:
    1. Verify whether the title is a valid graduation project idea.
    2. Assign it to the best-matching domain from the known list.

    Returns:
        {
            "is_valid": bool,
            "domain": str | None,
            "reason": str
        }
    """
    known_domains = [d for d in DOMAIN_KEYWORDS.keys() if d != "Others"]
    domain_list_str = "\n".join(f"- {d}" for d in known_domains)

    prompt = f"""
You are an expert academic advisor evaluating graduation project ideas.

Project Title: "{title}"
{"Abstract: " + abstract[:400] if abstract else ""}

Task 1 – Validity Check:
Is this a valid, feasible graduation project idea for a university student?
- It must be a technical or academic topic (not a random phrase, celebrity name, or nonsense)
- It should be specific enough to build something real
Answer: YES or NO

Task 2 – Domain Classification:
If valid, which ONE of the following domains best fits this project?
{domain_list_str}

Return your answer in this EXACT format (two lines only):
VALID: YES
DOMAIN: <domain name from the list above>

If invalid:
VALID: NO
DOMAIN: None
REASON: <one sentence why>
"""
    try:
        raw = generate_text(prompt, task="intent").strip()
        lines = {line.split(":", 1)[0].strip().upper(): line.split(":", 1)[1].strip()
                 for line in raw.splitlines() if ":" in line}

        is_valid = lines.get("VALID", "NO").upper() == "YES"
        domain = lines.get("DOMAIN", "").strip()
        reason = lines.get("REASON", "")

        if domain == "None" or domain not in known_domains:
            domain = None
        return {"is_valid": is_valid, "domain": domain, "reason": reason}

    except Exception:
        return {"is_valid": True, "domain": None, "reason": ""}


def extract_number(text: str, default=5):
    cleaned = str(text).strip()
    if cleaned in ["1", "2"]:
        return default
    nums = re.findall(r"\d+", text)
    return min(int(nums[0]), 20) if nums else default

def validate_and_format_domain(domain: str) -> str:
    # 1. Quick local validation for standard domains
    extracted = extract_domain(domain)
    if extracted and extracted.lower() != "others":
        return extracted

    # 2. Fall back to LLM validation
    prompt = f"""
Determine if the following domain/field is a valid academic, engineering, scientific, or technology domain suitable for a university graduation project (e.g., Computer Science, Engineering, Medicine, Business, Agriculture, Biology, etc.).
Also, correct any typos and format it cleanly (e.g., Title Case).

Domain to evaluate: "{domain}"

Rules:
- If it is a valid field of study, technology, or academic discipline (e.g., "artificial intelligence", "robotics", "bioinformatics", "educational games"), return ONLY the corrected and formatted domain name (e.g., "Artificial Intelligence").
- If it is unrelated to academic/technology graduation projects, or contains names of celebrities, sports teams, food, pop culture, or random questions (e.g., "messi", "fc barcelona", "pizza", "what is this"), return exactly "INVALID".

Return ONLY the formatted domain name or "INVALID". Do not include any other text.
"""
    try:
        res = generate_text(prompt, task="intent").strip()
        if not res or res.upper() == "INVALID":
            return ""
        return res.strip('"').strip("'")
    except Exception:
        return ""

def is_weak_project_title(title: str) -> bool:

    if not title:
        return True

    title = title.strip()

    words = title.split()

    
    if len(words) < 4:
        return True

    weak_words = {
        "system",
        "platform",
        "app",
        "website",
        "application",
        "project",
        "ai",
        "smart",
        "tool"
    }

    meaningful = [
        w.lower()
        for w in words
        if w.lower() not in weak_words
    ]

    return len(words) < 3

def is_generic_project_reference(text: str) -> bool:

    text = text.strip().lower()

    generic_titles = {
        "my project",
        "this project",
        "the project",
        "my system",
        "this system",
        "my app",
        "my application",
        "my idea",
        "project",
        "system",
        "app",
        "idea"
    }

    return text in generic_titles

def looks_like_real_project_title(title: str) -> bool:

    if not title:
        return False

    title = title.strip()

    words = title.split()

    
    if len(words) < 2:
        return False

    
    unique_ratio = len(set(words)) / len(words)

    if unique_ratio < 0.5:
        return False

    
    nonsense_patterns = [
        "asd",
        "qwe",
        "zxc",
        "testtest",
        "aaaa",
        "xxxxx"
    ]

    lowered = title.lower()

    question_starts = (
        "how ",
        "what ",
        "why ",
        "when ",
        "where ",
        "can ",
        "could ",
        "should ",
        "is ",
        "are ",
        "do ",
        "does "
    )

    for qs in question_starts:
        if lowered.startswith(qs):
            return False

    for p in nonsense_patterns:
        if p in lowered:
            return False

    
    keywords = {

    
    "management",
    "analysis",
    "detection",
    "tracking",
    "recognition",
    "monitoring",
    "security",
    "attendance",
    "automation",
    "prediction",
    "dashboard",
    "diagnosis",
    "learning",
    "recommendation",
    "classification",
    "authentication",
    "optimization",

    
    "healthcare",
    "fintech",
    "education",
    "library",
    "hospital",
    "school",
    "medical",
    "industrial",
    "agriculture",
    "transport",

    
    "ai",
    "iot",
    "blockchain",
    "cloud",
    "robotics",
    "vision",
    "embedded",

    
    "system",
    "platform",
    "application",
    "app",
    "website",
    "portal",
    "tool",
    "game",
    "generator",
    "engine",
    "software",
    "database",
    "model",
    "chatbot",
    "chat",
    "assistant",
    "network",
    "api",
    "mobile",
    "web",
    "smart"
}

    if not any(
        k in lowered
        for k in keywords
    ):
        return False

    return True

FOLLOWUP_WORDS = [
    "another",
    "more",
    "again",
    "other ideas",
    "more ideas",
    "more features",
    "another features"
]

def finalize_response(
    user_input,
    response,
    history,
    state,
    user_id
):

    history.append({
        "role": "user",
        "content": user_input
    })

    history.append({
        "role": "assistant",
        "content": response
    })

    history = history[-20:]

    save_user_memory(user_id, {
        "history": history,
        "state": state
    })

    return response

def is_gibberish_text(text: str) -> bool:

    text = text.strip().lower()

    
    if text in {"1", "2", "3"}:
        return False

    
    if len(text) < 3:

        
        allowed_short = {
            "hi",
            "hey",
            "hello",
            "ai",
            "ml",
            "ui",
            "ux",
            "vr",
            "ar",
            "iot",
            "no",
            "la",
            "n",
            "y",
            "ok"
        }

        if text in allowed_short:
            return False

        return True

    gibberish_patterns = [
        "asd",
        "qwe",
        "zxc",
        "aaa",
        "bbb",
        "ccc",
        "xxx",
        "testtest"
    ]

    for p in gibberish_patterns:
        if p in text:
            return True

    words = text.split()

    
    if len(words) >= 3:

        unique_ratio = len(set(words)) / len(words)

        if unique_ratio < 0.5:
            return True

    return False

def is_project_related(text: str) -> bool:

    text = text.lower().strip()

    keywords = [

        
        "project",
        "system",
        "platform",
        "application",
        "app",
        "website",
        "dashboard",
        "management",

        
        "ai",
        "ml",
        "machine learning",
        "deep learning",
        "computer vision",
        "blockchain",
        "iot",
        "web",
        "mobile",
        "cloud",
        "security",
        "database",
        "api",

        
        "generate",
        "feature",
        "features",
        "idea",
        "ideas",
        "improve",
        "description",
        "technologies",
        "architecture",

        
        "healthcare",
        "education",
        "fintech",
        "smart",
        "attendance",
        "monitoring",
        "tracking",
        "analysis",
        "recognition"
    ]

    return any(
        keyword in text
        for keyword in keywords
    )

def is_general_question_or_unrelated_chat(text: str) -> bool:
    lowered = text.strip().lower()
    
    # Ends with question mark
    if lowered.endswith("?"):
        return True
        
    # Starts with common question words
    question_starts = (
        "how ", "what ", "why ", "when ", "where ", "can ", "could ", "should ", 
        "is ", "are ", "do ", "does ", "explain ", "tell me ", "show me ", "describe "
    )
    if lowered.startswith(question_starts):
        return True
        
    # Contains common question phrases
    question_phrases = (
        "what is", "what's", "tell me about", "can you", "could you", "how to", "how do"
    )
    if any(phrase in lowered for phrase in question_phrases):
        return True
        
    return False


def chatbot(user_id: str, user_input: str):
    text = user_input.lower().strip()

    if is_command(user_input):
        return handle_command(user_input)

    memory = get_user_memory(user_id)
    history = memory.get("history", [])
    state = memory.get("state") or default_state()

    # The Orchestrator handles all context and validation
    from src.recommendation_engine.llm_router import analyze_user_input
    analysis = analyze_user_input(user_input, state)
    
    action = analysis.get("action", "reply_directly")
    reply_text = analysis.get("reply_text")
    domain = analysis.get("domain")
    project_title = analysis.get("project_title")
    number = analysis.get("number")
    abstract = analysis.get("abstract")
    description = analysis.get("description")

    if action == "reply_directly":
        if project_title and not state.get("project_title"):
            state["project_title"] = project_title
        if domain and not state.get("domain"):
            state["domain"] = domain
            
        custom_saved = False
        if abstract:
            state["abstract"] = abstract
            state["custom_abstract"] = True
            custom_saved = True
        if description:
            state["description"] = description
            state["custom_description"] = True
            custom_saved = True
            
        save_user_memory(user_id, {"history": history, "state": state})
        
        final_reply = reply_text or "I didn't quite catch that. Can you clarify?"
        if custom_saved:
            final_reply = "βœ… I have saved your custom project details!\n\n" + final_reply
            
        return finalize_response(
            user_input,
            final_reply,
            history,
            state,
            user_id
        )

    elif action == "trigger_idea_generation":
        if domain:
            domain_lower = domain.lower()
            if domain_lower in ["other", "others", "general", "any"]:
                state["domain"] = "general"
                state["waiting_for_domain"] = False
            elif domain_lower in ["domain", "domains", "list", "options", "help"]:
                state["domain"] = None
            else:
                state["domain"] = domain
                state["waiting_for_domain"] = False
        elif not any(w in user_input.lower() for w in FOLLOWUP_WORDS):
            state["domain"] = None
            
        if not state.get("domain"):
             state["waiting_for_domain"] = True
             save_user_memory(user_id, {"history": history, "state": state})
             domain_list = "\n".join(f"- {d}" for d in DOMAIN_KEYWORDS.keys() if d != "Others")
             response = (
                f"Which domain is your project in? πŸ“š\n\n"
                f"{domain_list}\n\n"
                f"πŸ’‘ Just type one of the domains above (e.g. **AI** or **Healthcare**)\n"
                f"If your domain isn't listed, type **Others** to see more options."
             )
             return finalize_response(user_input, response, history, state, user_id)

        top_k = number or extract_number(user_input, 5)

        all_past_ideas = state.get("all_generated_ideas", [])
        if state.get("ideas"):
            for i in state["ideas"]:
                if i not in all_past_ideas:
                    all_past_ideas.append(i)

        result = generate_ideas(
            domain=state.get("domain"),
            top_k=top_k,
            previous_generated_ideas=all_past_ideas
        )

        ideas = result.get("final_ideas", [])

        state["all_generated_ideas"] = all_past_ideas + ideas
        state["ideas"] = ideas
        state["last_action"] = "idea"
        
        state["project_title"] = ""
        state["features"] = []
        state["all_generated_features"] = []
        state["description"] = ""
        state["abstract"] = ""
        state["technologies"] = []

        response = format_response("idea", "", state)
        return finalize_response(user_input, response, history, state, user_id)

    elif action == "trigger_feature_generation":
        if project_title:
            state["project_title"] = project_title

        if not state.get("project_title"):
            return finalize_response(
                user_input,
                "I need a project title to generate features! πŸ“\nJust type your project title.",
                history,
                state,
                user_id
            )

        top_k = number or extract_number(user_input, 5)

        all_past_features = state.get("all_generated_features", [])
        if state.get("features"):
            for f in state["features"]:
                if f not in all_past_features:
                    all_past_features.append(f)

        result = generate_features(
            title=state.get("project_title"),
            description=state.get("description", ""),
            features=[],
            previous_generated_features=all_past_features,
            top_k=top_k
        )

        new_features = result.get("recommended_features", [])
        
        state["all_generated_features"] = all_past_features + new_features
        state["features"] = new_features
        state["last_action"] = "feature"

        response = format_response("feature", "", state)
        
        if state.get("custom_abstract") or state.get("custom_description"):
            state["waiting_for_abstract_update"] = True
            response += "\n\n✨ **Would you like me to seamlessly weave these new features into your custom abstract and description? (Yes/No)**"
            
        return finalize_response(user_input, response, history, state, user_id)

    elif action == "trigger_full_project_generation":
        if project_title:
            state["project_title"] = project_title
            
        if not state.get("features"):
            feature_result = generate_features(
                title=state.get("project_title"),
                description=state.get("description", ""),
                features=[],
                previous_generated_features=[],
                top_k=8
            )
            state["features"] = feature_result.get("recommended_features", [])

        custom_desc = state.get("custom_description", False)
        custom_abs = state.get("custom_abstract", False)

        result = generate_full_project(
            title=state.get("project_title"),
            features=state.get("features", []),
            description=state.get("description", "") if custom_desc else "",
            abstract=state.get("abstract", "") if custom_abs else "",
            custom_description=custom_desc,
            custom_abstract=custom_abs
        )

        state = update_state(state, result, mode="merge")
        if state.get("domain"):
            state["category"] = state.get("domain")

        response = f"""
πŸ“¦ Full Project Generated

πŸ“Œ Project Title:
{state.get("project_title")}

πŸ“‚ Category:
{state.get("category")}

πŸ›  Technologies:
{", ".join(state.get("technologies", []))}

πŸ“„ Abstract:
{state.get("abstract")}

πŸ“„ Detailed Description:
{state.get("description")}

❗ Problem Statement:
{state.get("problem_statement")}

πŸ’‘ Proposed Solution:
{state.get("proposed_solution")}

🎯 Objectives:
{chr(10).join("- " + x for x in state.get("objectives", []))}

━━━━━━━━━━━━━━━━━━━━━━
πŸ‘‰ What's next? You can say "improve features", or tell me to "replace abstract with..." your own custom text!
"""
        return finalize_response(user_input, response, history, state, user_id)
        
    elif action == "confirmation_yes":
        if state.get("waiting_for_abstract_update"):
            from src.recommendation_engine.full_project_generator import rewrite_custom_sections
            state["waiting_for_abstract_update"] = False
            
            rewritten = rewrite_custom_sections(
                features=state.get("features", []),
                abstract=state.get("abstract", "") if state.get("custom_abstract") else "",
                description=state.get("description", "") if state.get("custom_description") else ""
            )
            
            if state.get("custom_abstract") and rewritten.get("abstract"):
                state["abstract"] = rewritten["abstract"]
            if state.get("custom_description") and rewritten.get("description"):
                state["description"] = rewritten["description"]
                
            save_user_memory(user_id, {"history": history, "state": state})
            return finalize_response(
                user_input, 
                "βœ… **Done!** I've upgraded your custom abstract and description with the new features while keeping your original style intact.\n\nType **'2'** to generate and view your newly upgraded full project!", 
                history, 
                state, 
                user_id
            )
            
        state["waiting_for_project_idea_confirm"] = False
        state["waiting_for_title_confirmation"] = False
        save_user_memory(user_id, {"history": history, "state": state})
        return finalize_response(user_input, "Great! Confirmed. Let's move on.", history, state, user_id)
        
    elif action == "confirmation_no":
        if state.get("waiting_for_abstract_update"):
            state["waiting_for_abstract_update"] = False
            save_user_memory(user_id, {"history": history, "state": state})
            return finalize_response(
                user_input, 
                "πŸ‘ **Got it!** I will leave your custom abstract and description exactly as you wrote them.\n\nType **'2'** whenever you're ready to view the full project.", 
                history, 
                state, 
                user_id
            )
            
        state["waiting_for_project_idea_confirm"] = False
        state["waiting_for_title_confirmation"] = False
        save_user_memory(user_id, {"history": history, "state": state})
        return finalize_response(user_input, "No problem! Let's try something else.", history, state, user_id)
        
    elif action == "clear_session":
        state = default_state()
        save_user_memory(user_id, {"history": history, "state": state})
        return finalize_response(
            user_input,
            "βœ… Session cleared! We are starting fresh. How can I help you today?",
            history,
            state,
            user_id
        )

    else:
        return finalize_response(user_input, "I am not sure how to handle that.", history, state, user_id)