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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
083214f
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
# ============================================================================
# research_agent.py - BACKEND
# ============================================================================

from typing import TypedDict, List, Literal, Annotated
import os
from dotenv import load_dotenv

from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode
from langchain.tools import tool
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from openai import OpenAI
from tavily import TavilyClient

# ============================================================================
# CONFIGURATION & INITIALIZATION
# ============================================================================

# Load environment variables from .env file
load_dotenv()

# Initialize API clients with environment variables as fallback
# These will be overridden by user-provided keys in process_message
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")

# Initialize API clients with environment variables
# These will be re-initialized with user-provided keys when needed
openai_client = None
llm = None
light_llm = None

def initialize_clients(openai_key: str, tavily_key: str):
    """
    Initialize API clients with provided keys.
    This function is called from process_message with user-provided API keys.
    """
    global openai_client, llm, light_llm, tool_node, llm_with_tools
    
    if not openai_key or not tavily_key:
        raise ValueError("Both OpenAI and Tavily API keys must be provided.")
    
    openai_client = OpenAI(api_key=openai_key)
    llm = ChatOpenAI(model="gpt-5.1", api_key=openai_key, temperature=0)  # Main LLM for research and synthesis
    light_llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_key, temperature=0)  # Lighter LLM for clarification
    
    # Initialize tool node and bind tools to LLM
    tool_node = ToolNode(tools)
    llm_with_tools = llm.bind_tools([rag_search, web_search], tool_choice="auto")
    
    # Create and compile the research graph now that tool_node is ready
    create_research_graph()


# Set maximum number of research iterations to prevent infinite loops
# This is a default value and will be overridden by user input
DEFAULT_MAX_ITERATIONS = 3

# Global variable to store max iterations for research
MAX_ITERATIONS = DEFAULT_MAX_ITERATIONS

# Global variable to store tavily API key for use in web_search tool
_tavily_api_key = None

# ============================================================================
# VECTOR STORE SETUP
# ============================================================================

# Connect to pre-existing vector store containing embedded PDF documents
VECTOR_STORE_NAME = "yt-store"
store_id = None

def get_vector_store_id():
    """
    Retrieve the vector store ID. This needs to be called after clients are initialized.
    """
    global store_id
    
    if not openai_client:
        raise ValueError("OpenAI client not initialized. Call initialize_clients first.")
    
    # Search for the vector store by name in the OpenAI account
    for s in openai_client.vector_stores.list().data:
        if s.name == VECTOR_STORE_NAME:
            store_id = s.id
            break
    
    # Raise error if vector store is not found
    if not store_id:
        raise ValueError("❌ Vector store not found!")
    
    print(f"β™» Using vector store ID: {store_id}")
    return store_id

# ============================================================================
# TOOL DEFINITIONS
# ============================================================================

@tool
def rag_search(query: str) -> str:
    """
    Search INTERNAL knowledge base containing curated documents,
    architecture guides, tutorials, and domain expertise.

    Use for conceptual understanding, internal best practices,
    and previously authored materials.
    """
    print(f"πŸ” RAG Search called with query: {query}")
    
    # Query the vector store with semantic search
    res = openai_client.vector_stores.search(
        vector_store_id=store_id,
        query=query,
        max_num_results=3,  # Return top 3 most relevant chunks
        ranking_options={"score_threshold": 0.5, "ranker": "auto"}  # Filter by relevance score
    )
    
    # Handle case where no results are found
    if not res.data:
        return "No relevant information found in internal documents."
    
    # Extract text content from search results
    out = []
    for item in res.data:
        for block in item.content or []:
            if block.type == "text" and block.text:
                out.append(block.text)
    
    # Combine all text chunks with separator
    result = "\n\n".join(out) if out else "No relevant text extracted from internal documents."
    print(f"βœ… RAG Search returned {len(out)} chunks")
    return result

@tool
def web_search(query: str) -> str:
    """
    Search the public internet for recent developments,
    benchmarks, and up-to-date information.
    """
    global _tavily_api_key
    
    print(f"🌐 Web Search called with query: {query}")
    try:
        # Use Tavily API for web search
        tavily_client = TavilyClient(api_key=_tavily_api_key)
        response = tavily_client.search(query, max_results=3)
        
        # Format search results into readable text
        results = []
        for result in response.get('results', []):
            results.append(f"Title: {result.get('title', 'N/A')}\nContent: {result.get('content', 'N/A')}\nURL: {result.get('url', 'N/A')}")
        
        final_result = "\n\n---\n\n".join(results) if results else "No web search results found."
        print(f"βœ… Web Search returned {len(results)} results")
        return final_result
    except Exception as e:
        print(f"❌ Web search error: {str(e)}")
        return f"Web search error: {str(e)}"

# ============================================================================
# TOOL NODE SETUP
# ============================================================================

# Combine tools into a list for the agent
tools = [rag_search, web_search]

# These will be initialized when initialize_clients() is called
tool_node = None
llm_with_tools = None

# ============================================================================
# STATE DEFINITION
# ============================================================================

# Define the state structure that will be passed between nodes
class AgentState(TypedDict):
    messages: Annotated[List, add_messages]  # Conversation history with automatic message aggregation
    research_steps: List[str]  # Track the research plan steps
    final_answer: str  # Store the synthesized final answer
    iterations: int  # Count how many research iterations have occurred

# ============================================================================
# CLARIFICATION NODE
# ============================================================================

# System prompt for the clarification phase
CLARIFICATION_PROMPT = """
You are a research assistant helping refine a user's research request.

Your goal is NOT to answer yet.

Instead:

1. Acknowledge the topic positively
2. Ask smart follow-up questions to narrow scope
3. Offer categories to choose from
4. Ask about:
   - target audience
   - depth level
   - specific domains
   - time range
   - technical vs business focus

Example style:

"Great topic! Before I begin deep research, could you help me refine the scope?"

Then ask 3-5 concise questions.

DO NOT start researching yet.
"""

def clarification_node(state: AgentState):
    """
    Node that asks clarifying questions to refine the research scope.
    This runs BEFORE research begins.
    """
    print("\nπŸ’¬ Clarification phase...\n")

    messages = state["messages"]

    # If we already asked clarification, just wait for user input
    if state.get("phase") == "awaiting_user":
        return state

    # Use lighter LLM to generate clarification questions
    response = light_llm.invoke([
        SystemMessage(content=CLARIFICATION_PROMPT),
        messages[-1]
    ])

    return {
        "messages": [response]
    }

# ============================================================================
# PLANNER NODE
# ============================================================================

# System prompt for research planning
PLANNER_PROMPT = """
You are a deep research planning assistant.

Your job is to create a DUAL-SOURCE research plan.

For each major aspect of the user question:

Generate:

- ONE query specifically designed for INTERNAL knowledge (rag_search)
- ONE complementary query for EXTERNAL knowledge (web_search)

Return the plan in this EXACT format:

TOPIC 1:
RAG_QUERY: <query text optimized for internal documents>
WEB_QUERY: <query text optimized for current internet info>

TOPIC 2:
RAG_QUERY: ...
WEB_QUERY: ...

TOPIC 3:
RAG_QUERY: ...
WEB_QUERY: ...

At least 3 topics must be generated.

The goal is to ensure BOTH tools are used for comprehensive coverage.
"""

def planner_node(state: AgentState):
    """
    Node that creates a structured research plan.
    Generates paired RAG and Web queries for each research topic.
    """
    print("\n🧠 Planning research approach...\n")

    # Extract the user's refined question
    user_question = state["messages"][-1].content

    messages = [
        SystemMessage(content=PLANNER_PROMPT),
        HumanMessage(content=user_question)
    ]

    # Generate the research plan
    plan = llm.invoke(messages)

    return {
        "messages": [plan],
        "research_steps": [plan.content]  # Store plan for tracking
    } 

# ============================================================================
# RESEARCH AGENT NODE
# ============================================================================

# System prompt for the research agent
RESEARCH_PROMPT = """
You are a balanced deep research assistant.

You MUST use BOTH tools for every major topic:

- rag_search β†’ to gather internal curated knowledge
- web_search β†’ to gather current external information

Do NOT rely on only one tool.

Process:

1. Look at the research plan
2. For each topic:
   - First call rag_search with the RAG_QUERY
   - Then call web_search with the WEB_QUERY

You may call tools multiple times in one response.

Continue until all planned topics have been covered.

Only stop when:
- every RAG query has been executed
- every WEB query has been executed
"""

def research_agent_node(state: AgentState):
    """
    Node that executes the research plan by calling tools.
    This is the core agentic loop that gathers information.
    """
    print("\nπŸ”Ž Research agent analyzing next step...\n")

    messages = state["messages"]

    # Add system prompt if not already present
    if not any(isinstance(msg, SystemMessage) for msg in messages):
        messages = [SystemMessage(content=RESEARCH_PROMPT)] + messages

    # LLM decides which tools to call based on the plan
    response = llm_with_tools.invoke(messages)

    return {
        "messages": [response],
        "iterations": state.get("iterations", 0) + 1  # Increment iteration counter
    }

# ============================================================================
# SYNTHESIZER NODE
# ============================================================================

# System prompt for final synthesis
SYNTHESIS_PROMPT = """
You are a research report writer.

Using ALL the previous research results:

- Internal RAG findings
- Web findings
- Multiple iterations of searches

Create a detailed, well-structured final answer.

Format:

OVERVIEW
-------

INTERNAL KNOWLEDGE FINDINGS
--------------------------

WEB RESEARCH FINDINGS
---------------------

FINAL SYNTHESIS
--------------

CONCLUSION
---------
"""

def synthesizer_node(state: AgentState):
    """
    Node that synthesizes all research findings into a structured report.
    This is the final step that produces the deliverable.
    """
    print("\n✍️  Synthesizing final answer...\n")

    # Clean messages to remove tool calls and tool responses
    # (only keep actual content for cleaner synthesis)
    clean_messages = []

    for msg in state["messages"]:

        # Skip any message containing tool calls
        if hasattr(msg, "tool_calls") and msg.tool_calls:
            continue

        # Skip any raw tool response messages
        if msg.type == "tool":
            continue

        clean_messages.append(msg)

    # Add synthesis prompt and generate final report
    messages = clean_messages + [
        SystemMessage(content=SYNTHESIS_PROMPT)
    ]

    final = llm.invoke(messages)

    return {
        "messages": [final],
        "final_answer": final.content  # Store the final answer
    }

# ============================================================================
# ROUTING LOGIC
# ============================================================================

def research_router(state: AgentState) -> Literal["tools", "synthesize"]:
    """
    Router that decides whether to continue research or move to synthesis.
    
    Decision logic:
    1. Ensure both RAG and Web tools have been used at least once
    2. Stop if max iterations reached (safety limit)
    3. Continue if agent is still calling tools
    4. Otherwise, move to synthesis
    """
    last_message = state["messages"][-1]

    current_iterations = state.get("iterations", 0)

    print(f"πŸ”„ Iteration count: {current_iterations}")
    
    # Check conversation history to see which tools were used
    history = str(state["messages"])
    
    used_rag = "rag_search" in history
    used_web = "web_search" in history
    
    # Force at least one call to each tool for comprehensive research
    if not (used_rag and used_web):
        print("πŸ” Ensuring both RAG and Web sources are used...")
        return "tools"

    # HARD STOP: Prevent infinite loops by limiting iterations
    if current_iterations >= MAX_ITERATIONS:
        print("πŸ›‘ Max iterations reached – forcing synthesis")
        return "synthesize"

    # If agent is still requesting tool calls, continue research
    if hasattr(last_message, "tool_calls") and last_message.tool_calls:
        print("πŸ” Continuing research loop...")
        return "tools"

    # Otherwise, research is complete
    print("βœ… Research sufficient. Moving to synthesis.")
    return "synthesize"

# ============================================================================
# GRAPH CONSTRUCTION
# ============================================================================

# ---------- CLARIFICATION GRAPH ----------
# Simple linear graph: just asks clarifying questions
clarify_graph = StateGraph(AgentState)

# Add nodes
clarify_graph.add_node("clarify", clarification_node)

# Add edges: START β†’ clarify β†’ END
clarify_graph.add_edge(START, "clarify")
clarify_graph.add_edge("clarify", END)

# Compile the graph into an executable app
clarify_app = clarify_graph.compile()


# ---------- RESEARCH WORKFLOW GRAPH ----------
# This will be compiled on first use after clients are initialized
research_app = None

def create_research_graph():
    """
    Create and compile the research workflow graph.
    Must be called after initialize_clients() so that tool_node is ready.
    """
    global research_app
    
    if tool_node is None:
        raise ValueError("Tool node not initialized. Call initialize_clients first.")
    
    research_graph = StateGraph(AgentState)

    # Add nodes
    research_graph.add_node("planner", planner_node)
    research_graph.add_node("researcher", research_agent_node)
    research_graph.add_node("tools", tool_node)
    research_graph.add_node("synthesizer", synthesizer_node)

    # Add edges
    # Flow: START β†’ planner β†’ researcher β†’ (conditional loop) β†’ synthesizer β†’ END
    research_graph.add_edge(START, "planner")
    research_graph.add_edge("planner", "researcher")

    # Conditional edge: researcher decides to continue or finish
    research_graph.add_conditional_edges(
        "researcher",
        research_router,  # Router function decides next step
        {
            "tools": "tools",  # Continue research
            "synthesize": "synthesizer"  # Finish and synthesize
        }
    )

    # After tools execute, loop back to researcher
    research_graph.add_edge("tools", "researcher")
    # After synthesis, end the workflow
    research_graph.add_edge("synthesizer", END)

    # Compile the research graph
    research_app = research_graph.compile()
    return research_app

# ============================================================================
# BACKEND API FUNCTIONS
# ============================================================================

def create_initial_state():
    """
    Initialize the session state for a new conversation.
    Tracks: messages, iterations, answers, and current stage.
    """
    return {
        "messages": [],
        "iterations": 0,
        "final_answer": "",
        "research_steps": [],
        "stage": "awaiting_topic"  # UI flow stage
    }


def process_message(user_input, history, state, openai_api_key, tavily_api_key, max_iterations=2):
    """
    Main message processing function that handles the multi-turn conversation.
    
    Flow:
    1. User enters topic β†’ Clarification questions
    2. User answers β†’ Deep research begins
    3. Research completes β†’ Final report shown
    
    Args:
        max_iterations: Number of research loops to perform (2 means one loop of the research node)
    """
    global _tavily_api_key, MAX_ITERATIONS
    
    # Set the global MAX_ITERATIONS with user-provided value
    MAX_ITERATIONS = max_iterations
    
    # Initialize clients with provided API keys
    try:
        initialize_clients(openai_api_key, tavily_api_key)
        _tavily_api_key = tavily_api_key
        # Retrieve vector store ID with initialized client
        get_vector_store_id()
    except ValueError as e:
        error_msg = f"❌ Configuration Error: {str(e)}"
        if history is None:
            history = []
        history.append({"role": "assistant", "content": error_msg})
        return "", history, state
    
    # Initialize state if first message
    if state is None:
        state = create_initial_state()

    if history is None:
        history = []

    stage = state.get("stage", "awaiting_topic")

    # ---- FIRST TURN: Initial topic entry ----
    if stage == "awaiting_topic":

        # Store user's initial question
        state["messages"] = [HumanMessage(content=user_input)]

        # Run clarification graph to ask follow-up questions
        state = clarify_app.invoke(state)

        clarification = state["messages"][-1].content

        # Update chat history
        history.append({"role": "user", "content": user_input})
        history.append({"role": "assistant", "content": clarification})

        # Move to next stage
        state["stage"] = "awaiting_clarification"

        return "", history, state


    # ---- SECOND TURN: User provides clarification ----
    elif stage == "awaiting_clarification":

        # Add user's clarification to conversation
        state["messages"].append(HumanMessage(content=user_input))

        # Clean artifacts from previous interactions
        clean_messages = []
        for msg in state["messages"]:
            if hasattr(msg, "tool_calls") and msg.tool_calls:
                continue
            if msg.type == "tool":
                continue
            clean_messages.append(msg)

        # Reset state for fresh research
        state["messages"] = clean_messages
        state["iterations"] = 0

        # Update UI
        history.append({"role": "user", "content": user_input})
        history.append({"role": "assistant", "content": "πŸ” Starting deep research... please wait..."})

        # Run the full research workflow graph
        final_state = research_app.invoke(state)

        # Extract final answer
        final_answer = final_state["messages"][-1].content

        # Show result in chat
        history.append({"role": "assistant", "content": final_answer})

        # Mark research as completed
        final_state["stage"] = "completed"

        return "", history, final_state


    # ---- AFTER COMPLETION: No further research ----
    else:
        history.append({"role": "assistant", "content": "Research already completed. Refresh page to start a new topic."})
        return "", history, state