File size: 26,511 Bytes
717293c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py
import os
import uuid
from datetime import datetime
from typing import Annotated, TypedDict, List, Any, Optional, Dict
 
from dotenv import load_dotenv
from pydantic import BaseModel, Field

import fitz  # PyMuPDF
import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.schema import SystemMessage, HumanMessage, AIMessage
from langchain.memory import ConversationSummaryMemory
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.checkpoint.memory import MemorySaver

# ---------------------------
# Load environment variables
# ---------------------------
load_dotenv()

# ---------------------------
# State Definition
# ---------------------------
class State(TypedDict):
    messages: Annotated[list[Any], add_messages]
    job_desc: str                      # keep the original job description
    user_tone: str                     # user-specified tone
    selected_tone: str                 # optional tone selection
    phases: list                        # <-- replace `job_data` with the phases output
    proposal_text: str                  # generated proposal text
    evaluation_scores: dict             # scoring for AI detection etc.
    user_portfolio_text: str            # user’s portfolio content
    user_feedback: dict                 # feedback if any
    session_summary: str                # summary of the chat/session
    success_status: str                 # success/failure flag
    instructions: str                   # instruction for proposal edits
    count_loop: int                     # for retry or loop logic

# ---------------------------
# Phase Models
# ---------------------------
class PhaseStep(BaseModel):
    step_number: int = Field(
        description="The sequential order of this step within the phase, starting from 1"
    )
    title: str = Field(
        description="A short, descriptive title summarizing the step's action, e.g., 'Analyze Job Description'"
    )
    description: str = Field(
        description="A detailed explanation of what needs to be done in this step"
    )
    tools_required: List[str] = Field(
        default=[],
        description="A list of tools, software, or platforms required to complete this step"
    )
    output_example: Optional[str] = Field(
        default=None,
        description="An example of the expected output or deliverable for this step, if applicable"
    )
    notes: Optional[str] = Field(
        default=None,
        description="Additional notes, tips, or context to ensure the step is carried out correctly"
    )

class Phase(BaseModel):
    phase_name: str = Field(
        description="The name of the phase, grouping related steps, e.g., 'Preparation', 'Execution', 'Delivery'"
    )
    tasks: List[PhaseStep] = Field(
        description="A list of PhaseStep objects detailing each actionable task in this phase"
    )

class Phases(BaseModel):
    phases: List[Phase] = Field(
        description="A complete list of all phases for the job, each containing structured step-by-step tasks"
    )



# ---------------------------
# Load environment variables and initialize LLM
# ---------------------------


load_dotenv()

# Get OpenAI API key
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
    raise ValueError("OPENAI_API_KEY not found in environment variables")
print("✅ API Key loaded successfully.")

# Initialize LLM agent
agent = ChatOpenAI(
    model="gpt-4o-mini",
    api_key=api_key,
    temperature=0.0,
    streaming=True
)

# Structured output wrapper for Phases
node1_agent_llm_with_output = agent.with_structured_output(Phases)

# ---------------------------
# Phase Maker Function
# ---------------------------
def phase_maker(state: State) -> Dict[str, Any]:
    """Generates structured phases from job description and portfolio text."""
    print("---- Entered PhaseMaker ----")
    job_description = state["job_desc"]
    portfolio_text = state.get("user_portfolio_text", "")

    print(f"[PhaseMaker] Received job_desc: {job_description[:100]}...")
    print(f"[PhaseMaker] Received portfolio_text: {portfolio_text[:100]}...")

    node_1_user_prompt = f"""
You are a step-by-step implementation assistant. Your task is to convert the following job description and past work history into a fully structured plan that can be used to generate a proposal. 

Output MUST follow the Pydantic model `Phases`:
- Phases → Phase → PhaseStep
- Every step must have: step_number, title, description, tools_required, output_example, notes
- Every variable must be fully populated with realistic, specific information. Do NOT leave placeholders or generic statements.
- Include all relevant tools, software, and platforms needed for each step.
- Include expected deliverables/output in each step if applicable.
- Provide actionable instructions that someone could follow to complete each step.
- Organize steps logically into phases like Preparation, Execution, Delivery.

Requirements:
- Ensure clarity, specificity, and actionable content.
- Avoid generic or filler text.
- Reflect relevant skills and tools used in your past projects if applicable.
- Strictly make these 3 phases only: Preparation, Execution, Delivery.

Example expected output:
{{
  "phases": [
    {{
      "phase_name": "Preparation",
      "tasks": [
        {{
          "step_number": 1,
          "title": "Analyze Job Description",
          "description": "Carefully read the job posting to identify core tasks and client expectations...",
          "tools_required": ["Notion", "Google Docs"],
          "output_example": "List of required skills and deliverables",
          "notes": "Focus on mandatory skills and desired experience"
        }}
      ]
    }}
  ]
}}

Generate the full Phases JSON now.
Job Description:
{job_description}

Past Work History:
{portfolio_text}
"""

    if not state.get("phases"):
        print("[PhaseMaker] No existing phases found → generating new ones...")
        messages = [HumanMessage(content=node_1_user_prompt)]
        response = node1_agent_llm_with_output.invoke(messages)
        print("[PhaseMaker] Generated Phases response successfully ✅")
        return {"phases": response}

    print("[PhaseMaker] Existing phases found → skipping generation.")
    return {"phases": state.get("phases")}



# ---------------------------
# Node 2 Prompt Template
# ---------------------------


node_2_prompt = """
You are a professional proposal writing assistant. Your task is to generate a personalized proposal using a predefined template. Follow these rules strictly:

1. Use the `phases` JSON provided as structured output from the previous step.
2. Replace all placeholders in the template with the actual, specific content from `phases` and the job description.
3. Do NOT invent new content beyond what is in `phases`, `job_description`, or `user_portfolio_text`.
4. Maintain a natural, human-like writing style to minimize AI detection.
5. Output the proposal as a single coherent text, ready to send to the client.
6. If `instructions` are provided by the user, apply them carefully without regenerating phases.

When filling the placeholders, extract details directly from phases.
"""

# ---------------------------
# Proposal Output & LLM Wrapper
# ---------------------------
from pydantic import BaseModel, Field
from typing import Dict, Any, List
from langchain_openai import ChatOpenAI
from langchain.schema import HumanMessage, AIMessage, SystemMessage

class ProposalOutput(BaseModel):
    """The full, ready-to-send proposal generated for the client."""
    proposal: str = Field(
        description="The full, ready-to-send proposal generated for the client, written in natural human-like style."
    )

# Wrap LLM output with structured ProposalOutput
agent_llm_with_output = agent.with_structured_output(ProposalOutput)

# ---------------------------
# Template Selection Function
# ---------------------------
def get_template_by_tone(tone: str) -> str:
    """Return proposal template text based on user-selected tone."""
    print(f"[Template] Getting template for tone: {tone}")
    templates = {
        "Professional & Confident": "[HOOK] Hi {client_name}, I had a look at your project on {final_phase_main_output} ...",
        "Friendly & Conversational": "[HOOK] Hi {client_name}, I just checked out your project on {project_type} ...",
        "Creative & Narrative-Driven": "[HOOK] Hi {client_name}, I came across your project on {project_type} ...",
        "Analytical & Problem-Solver": "[HOOK] from {preparation_phase_decription_short} to the '{final_phase_delieverable}' ...",
        "Persuasive & Client-Focused": "[HOOK] Picture this: your {project_type} not only looks amazing, but actually works hard ..."
    }
    return templates.get(tone, "Tone not found. Please choose a valid tone.")

# ---------------------------
# Worker Node Function
# ---------------------------
def worker(state: State) -> Dict[str, Any]:
    """Generate a client-ready proposal based on job description, phases, and user portfolio."""
    print("---- Entered Worker ----")
    
    job_message = state["job_desc"]
    tone = state.get("user_tone", "")
    portfolio_text = state.get("user_portfolio_text", "")
    instructions = state.get("instructions", "")
    template_text = get_template_by_tone(tone)
    phases = state.get("phases", [])

    print(f"[Worker] Job description received: {job_message[:100]}...")
    print(f"[Worker] Tone: {tone}")
    print(f"[Worker] Instructions: {instructions[:100]}...")
    print(f"[Worker] Portfolio text: {portfolio_text[:100]}...")
    print(f"[Worker] Phases present? {'Yes' if phases else 'No'}")

    node_2_user_prompt = f"""
- job_description: {job_message}

- phases: structured step-by-step implementation
{phases}

- user_portfolio_text:
{portfolio_text}

- instructions: optional user edits for the proposal
{instructions}

Template: {template_text}
"""

    # Handle feedback
    if state.get("feedback_on_work"):
        print("[Worker] Feedback found → appending to system message.")
        feedback_text = f"""
Previous submission was rejected.
Here is the feedback you received:
{state['feedback_on_work']}

Use this feedback to revise and improve the proposal to meet the success criteria.
"""
    else:
        feedback_text = ""
        print("[Worker] No feedback found → proceeding normally.")

    # Build messages
    found_system_message = False
    messages = state.get("messages", [])
    for message in messages:
        if isinstance(message, SystemMessage):
            message.content = node_2_user_prompt + feedback_text
            found_system_message = True

    if not found_system_message:
        print("[Worker] No existing SystemMessage → creating a new one.")
        messages = [SystemMessage(content=node_2_prompt + feedback_text)] + messages
    else:
        print("[Worker] Existing SystemMessage found → updated with new prompt.")

    msg_list = [HumanMessage(content=node_2_user_prompt)] + messages
    print(f"[Worker] Built msg_list with {len(msg_list)} messages")

    # Call LLM
    response = agent_llm_with_output.invoke(msg_list)
    print("[Worker] Received response from agent LLM ✅")

    new_state = {
        "proposal_text": response.proposal,
        "messages": [AIMessage(content=f"proposal written {response.proposal[:100]}...")]
    }

    print("[Worker] Returning new state with proposal_text and updated messages.")
    return new_state

# ---------------------------
# Utility Function: Format Conversation
# ---------------------------
def format_conversation(messages: List[Any]) -> str:
    """Format the conversation history for display or logging."""
    conversation = "Conversation history:\n\n"
    for message in messages:
        if isinstance(message, HumanMessage):
            conversation += f"User: {message.content}\n"
        elif isinstance(message, AIMessage):
            conversation += f"Assistant: {message.content}\n"
    return conversation


# ---------------------------
# Evaluator Output & LLM Wrapper
# ---------------------------
from pydantic import BaseModel, Field
from typing import Dict, Any
from langchain.schema import SystemMessage, HumanMessage
from langgraph.graph import StateGraph

class EvaluatorOutput(BaseModel):
    """Structured evaluation output for proposal quality."""
    user_feedback: str = Field(
        description="Detailed feedback on the assistant's proposal, including strengths and areas to improve"
    )
    evaluation_scores: str = Field(
        description="Scoring or boolean evaluation for each success criterion"
    )
    success_status: str = Field(
        description="Overall status of evaluation: 'passed', 'failed', or 'needs_input'"
    )

# System prompt for the evaluator
evaluator_system_prompt = """You are an evaluator for a professional proposal-writing AI assistant.
You will be given:
1. The job description (client’s posting).
2. The template and tone that were used to generate the proposal.
3. The structured phases the assistant followed.
4. The assistant’s final proposal.

Your job is to check whether the proposal aligns with both:
- The client’s job description.
- The intended template/phases.

Check the proposal for:
- Relevance — Does it clearly address the specific job description and client needs, not just generic text?
- Profile Use — Does it correctly use the freelancer’s profile and portfolio as its own?
- Tone — Does the proposal’s tone match the selected template?
- Structure — Is the structure aligned with the phases and template (clear, professional flow)?
- Portfolio — Are relevant portfolio links included if provided?
- Authenticity — Does it avoid robotic or filler text, and sound human?

Respond with:
1. A list of feedback points for improvement.
2. A score (0–10) for each: relevance, tone, structure.
3. Overall status: passed, failed, or needs_input.

If status is failed, explicitly explain what must be changed in the proposal to pass.
"""

# ---------------------------
# Evaluator Function
# ---------------------------
def evaluator(state: State) -> State:
    """Evaluate the generated proposal for quality, alignment with job description, and template adherence."""
    print("---- Entered Evaluator ----")

    last_response = state.get("proposal_text", "")
    user_message = f"""Conversation history:
{format_conversation(state.get('messages', []))}

# Job description:
{state.get('job_desc', '')} 

# Template used:
{state.get('selected_template', '')}

# Structured phases followed:
{state.get('phases', '')}

Last proposal:
{last_response}
"""

    if state.get("user_feedback"):
        user_message += f"\nPrevious feedback: {state['user_feedback']}\n"
        print("[Evaluator] Found user feedback → appended to prompt.")
    else:
        print("[Evaluator] No previous user feedback.")

    # Build evaluator messages
    evaluator_messages = [
        SystemMessage(content=evaluator_system_prompt),
        HumanMessage(content=user_message)
    ]
    print(f"[Evaluator] Built evaluator_messages with {len(evaluator_messages)} messages")

    # LLM call with structured output
    evaluator_llm_with_output = agent.with_structured_output(EvaluatorOutput)
    eval_result = evaluator_llm_with_output.invoke(evaluator_messages)
    print("[Evaluator] Got evaluation result from LLM ✅")
    print(f"[Evaluator] success_status: {eval_result.success_status}")
    print(f"[Evaluator] evaluation_scores: {eval_result.evaluation_scores}")
    print(f"[Evaluator] user_feedback: {eval_result.user_feedback[:100]}...")

    # Increment loop counter
    state["count_loop"] = state.get("count_loop", 0) + 1
    print(f"[Evaluator] Evaluation loop count → {state['count_loop']}")

    # Stop after 2 attempts (3rd means needs input)
    success_status = eval_result.success_status
    if state["count_loop"] >= 2:
        print(f"[Evaluator] Maximum limit reached ({state['count_loop']} attempts). Forcing 'needs_input'")
        success_status = "needs_input"

    new_state = {
        "user_feedback": eval_result.user_feedback,
        "evaluation_scores": eval_result.evaluation_scores,
        "success_status": success_status,
        "proposal_text": last_response,
        "count_loop": state["count_loop"]
    }

    print("[Evaluator] Returning new state with evaluation results.")
    return new_state

# ---------------------------
# Route Based on Evaluation
# ---------------------------
def route_based_on_evaluation(state: dict) -> str:
    """Determine the next step in the workflow based on evaluation status."""
    if state.get("success_status") in ["passed", "needs_input"]:
        return "END"
    return "worker"



# ---------------------------
# State Graph Initialization
# ---------------------------


graph_builder = StateGraph(State)



# ---------------------------
# Graph Nodes & Flow Setup
# ---------------------------
from langgraph.checkpoint.memory import MemorySaver
from IPython.display import Image, display

# Add nodes
graph_builder.add_node("phase_maker", phase_maker)
graph_builder.add_node("worker", worker)
graph_builder.add_node("evaluator", evaluator)

# Add edges
graph_builder.add_edge("phase_maker", "worker")       # phases go into worker
graph_builder.add_edge("worker", "evaluator")         # proposal goes to evaluator
graph_builder.add_conditional_edges(
    "evaluator",
    route_based_on_evaluation,
    {"worker": "worker", "END": END}                  # loop back or end
)

# Entry point
graph_builder.add_edge(START, "phase_maker")

# Compile the graph with memory
memory = MemorySaver()
compiled_graph = graph_builder.compile(checkpointer=memory)

# # Optional: Display graph as PNG in Jupyter
# display(Image(compiled_graph.get_graph().draw_mermaid_png()))

# ---------------------------
# Thread & Reset Utilities
# ---------------------------
import uuid

def make_thread_id() -> str:
    """Generate a unique thread ID for new conversation sessions."""
    return str(uuid.uuid4())

def reset(thread: str):
    """Reset conversation memory for a given thread and generate a new thread ID."""
    print(f"[Reset] Current thread id: {thread}")
    try:
        memory.delete(thread)  # Clear old conversation memory
    except Exception as e:
        print(f"[Warning] Could not delete memory for thread {thread}: {e}")

    new_thread = make_thread_id()

    # Return initial state for Gradio interface (matching output order & type)
    return (
        "",          # instructions (Textbox)
        "",          # job_desc (Textbox)
        [],          # conversation (Chatbot messages)
        None,        # pdf_uploader (File)
        "Professional & Confident",    # tone_selector (Dropdown default value)
        new_thread   # thread (State)
    )


# ---------------------------
# supabase_module.py
# ---------------------------

import os
from datetime import datetime
from dotenv import load_dotenv
from supabase import create_client
import fitz  # PyMuPDF
from typing import List, Optional

# Load env variables
load_dotenv()

SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
supabase = create_client(SUPABASE_URL, SUPABASE_KEY)

MAX_FILES = 2

# ---------------------------
# DB Functions
# ---------------------------
def add_file_to_db(file_name: str, file_path: str):
    """Extract text from PDF and insert/update in Supabase."""
    text = ""
    with fitz.open(file_path) as doc:
        for page in doc:
            text += page.get_text()

    supabase.table("uploaded_files").upsert({
        "file_name": file_name,
        "file_text": text,
        "upload_date": datetime.now().isoformat()
    }, on_conflict="file_name").execute()

    enforce_max_files()


def enforce_max_files():
    """Ensure only MAX_FILES are stored in DB, delete oldest if needed."""
    try:
        data = supabase.table("uploaded_files").select("id, file_name, upload_date").execute()
        files = data.data
        if len(files) > MAX_FILES:
            for file in sorted(files, key=lambda x: x["upload_date"])[:len(files)-MAX_FILES]:
                supabase.table("uploaded_files").delete().eq("id", file["id"]).execute()
    except Exception as e:
        print("[Supabase] Error enforcing max files:", e)


def get_files_from_db() -> List[dict]:
    """Return all uploaded files, latest first."""
    try:
        data = supabase.table("uploaded_files").select("file_name, file_text, upload_date").execute()
        files = data.data if data.data else []
        files.sort(key=lambda x: x["upload_date"], reverse=True)
        return files
    except Exception as e:
        print("[Supabase] Error fetching files:", e)
        return []


def extract_pdf_data() -> str:
    """Concatenate text from all uploaded PDFs."""
    text = ""
    for file in get_files_from_db():
        text += file['file_text']
    print(f"[PDF Extraction] Preview: {text[:50]}...")
    return text


# ---------------------------
# Gradio Functions
# ---------------------------
def upload_files(files) -> List[str]:
    """Handle uploaded files from Gradio and store in Supabase."""
    if not files:
        return refresh_file_list()

    for f in files:
        file_name = os.path.basename(f.name)
        add_file_to_db(file_name, f.name)
    return refresh_file_list()


def refresh_file_list() -> List[str]:
    """Return file list for display in UI."""
    files_data = get_files_from_db()
    return [f"{f['file_name']} (Uploaded: {f['upload_date']})" for f in files_data]


async def generate_proposal(job_description: str, instructions: str, tone: str, history: list, thread: str):
    """Main function to generate proposal using compiled graph and PDF data."""
    print(f"[Generate Proposal] thread_id={thread}, tone={tone}")
    user_message = {"role": "user", "content": instructions}
    portfolio_text = extract_pdf_data()

    state = {
        "proposal_text": "",
        "user_tone": tone,
        "instructions": instructions,
        "messages": user_message,
        "selected_tone": tone,
        "user_portfolio_text": portfolio_text,
        "job_desc": job_description,
        "count_loop": 0
    }
    config = {"configurable": {"thread_id": thread}}

    result = await compiled_graph.ainvoke(state, config=config)

    proposal_text = result["proposal_text"]
    new_history = history + [user_message, {"role": "assistant", "content": proposal_text}]
    return new_history, result


def get_summary_from_memory(saved_state: dict) -> str:
    """Generate conversation summary using MemorySaver and ConversationSummaryMemory."""
    if not saved_state or "messages" not in saved_state:
        return ""

    from langchain.memory import ConversationSummaryMemory
    llm = ChatOpenAI(model="gpt-4o-mini")
    summary_memory = ConversationSummaryMemory(llm=llm)

    user_msg = None
    for msg in saved_state.get("messages", []):
        if isinstance(msg, HumanMessage):
            user_msg = msg.content
        elif isinstance(msg, AIMessage) and user_msg is not None:
            summary_memory.save_context({"input": user_msg}, {"output": msg.content})
            user_msg = None

    summary_final = summary_memory.load_memory_variables({})["history"]
    print(f"[Summary] {summary_final[:100]}...")
    return summary_final

# ---------------------------
# Session Finalization
# ---------------------------

def finish_session(thread_id: str, saved_state: dict):
    """
    Finalize a session by generating a conversation summary
    and saving it to Supabase.
    """
    print(f"[Session] Finishing session for thread_id={thread_id}")

    # Generate summary
    summary = get_summary_from_memory(saved_state)
    print(f"[Session] Generated summary preview: {summary[:100]}...")

    # Save summary to Supabase
    try:
        supabase.table("summaries").insert({
            "session_id": thread_id,
            "summary": summary,
            "created_at": datetime.now().isoformat()
        }).execute()
        print(f"✅ Conversation summarized and saved for thread {thread_id}.")
    except Exception as e:
        print(f"[Session] Error saving summary to Supabase: {e}")


#----------------------
#-------GRADIO APP-----
#-----------------------


import gradio as gr

# -------- Gradio UI --------
with gr.Blocks(css="""
#left-panel {width: 200px;}
#conversation-box {height: 400px; overflow-y: auto;}
""") as demo:
    thread = gr.State(make_thread_id())
    saved_state=gr.State()
    with gr.Row():
        # Left side panel
        with gr.Column(elem_id="left-panel"):
            gr.Markdown("### 📂 Session")
            refresh_btn = gr.Button("🔄 New Session")
            finish_btn = gr.Button("🏁 Summary")
            
        
        # Center conversation + inputs
        with gr.Column(scale=3):
            conversation = gr.Chatbot(label="AI Proposal Conversation", elem_id="conversation-box" ,type="messages")
            job_desc = gr.Textbox(label="Job Description", placeholder="Paste the job description here", lines=4)
            instructions = gr.Textbox(label="Instructions", placeholder="Add any special instructions here", lines=3)
            submit_btn = gr.Button("Generate Proposal")
        
        # Right controls
        with gr.Column(scale=1):
            tone_selector = gr.Dropdown(["Professional & Confident","Friendly & Conversational","Persuasive & Client-Focused","Creative & Narrative-Driven","Analytical & Problem-Solver"],value="Professional & Confident",label="Tone")
            uploader = gr.File(label="Upload Files", file_types=[".pdf"], file_count="multiple")
            file_list = gr.List(label="Files in Database", value=refresh_file_list())
    # -------- Event Bindings --------
   
    uploader.change(upload_files, inputs=uploader, outputs=file_list)
    submit_btn.click(
        fn=generate_proposal,
        inputs=[job_desc, instructions, tone_selector, conversation, thread],
        outputs=[conversation,saved_state]
    )
    refresh_btn.click(
    fn=reset,
    inputs=[thread],
    outputs=[instructions, job_desc, conversation, uploader, tone_selector, thread]
    )
    finish_btn.click(
    fn=finish_session,
    inputs=[thread,saved_state],
    outputs=[]
)


if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=int(os.environ.get("PORT", 7860))
    )