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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
import json
import os
from datetime import datetime, timezone
from pathlib import Path
from typing import Annotated, Any, TypedDict

import faiss
import numpy as np
import streamlit as st
from duckduckgo_search import DDGS
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_groq import ChatGroq
from langgraph.graph import END, StateGraph
from langgraph.graph.message import add_messages
from sentence_transformers import SentenceTransformer

# --- Settings ---
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
GROQ_MODEL = os.getenv("GROQ_MODEL", "openai/gpt-oss-120b")
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
MAX_RESEARCH_ITERATIONS = 2

MEMORY_DIR = Path(__file__).parent / "data" / "memory"
MEMORY_DIR.mkdir(parents=True, exist_ok=True)
FAISS_INDEX_PATH = MEMORY_DIR / "research_memory.index"
FAISS_META_PATH = MEMORY_DIR / "research_memory_meta.json"

AGENT_COLORS = {
    "Researcher": "#4A90D9",
    "Critic": "#E67E22",
    "Synthesizer": "#9B59B6",
    "Writer": "#27AE60",
}


# --- Memory ---
class ResearchMemory:
    def __init__(self) -> None:
        self._encoder = SentenceTransformer(EMBEDDING_MODEL)
        self._dim = self._encoder.get_sentence_embedding_dimension()
        self._index: faiss.IndexFlatL2 | None = None
        self._metadata: list[dict[str, Any]] = []
        self._load()

    def _load(self) -> None:
        if FAISS_INDEX_PATH.exists() and FAISS_META_PATH.exists():
            self._index = faiss.read_index(str(FAISS_INDEX_PATH))
            self._metadata = json.loads(FAISS_META_PATH.read_text(encoding="utf-8"))
        else:
            self._index = faiss.IndexFlatL2(self._dim)
            self._metadata = []

    def _save(self) -> None:
        if self._index is not None:
            faiss.write_index(self._index, str(FAISS_INDEX_PATH))
        FAISS_META_PATH.write_text(
            json.dumps(self._metadata, indent=2, ensure_ascii=False),
            encoding="utf-8",
        )

    def _embed(self, text: str) -> np.ndarray:
        vec = self._encoder.encode([text], normalize_embeddings=True)
        return np.array(vec, dtype=np.float32)

    def store(self, topic: str, report: str) -> None:
        entry = {
            "topic": topic,
            "report": report,
            "timestamp": datetime.now(timezone.utc).isoformat(),
        }
        vector = self._embed(f"{topic}\n{report[:2000]}")
        self._index.add(vector)
        self._metadata.append(entry)
        self._save()

    def recall(self, query: str, k: int = 3) -> list[dict[str, Any]]:
        if not self._metadata or self._index is None or self._index.ntotal == 0:
            return []
        vector = self._embed(query)
        distances, indices = self._index.search(vector, min(k, self._index.ntotal))
        results = []
        for dist, idx in zip(distances[0], indices[0]):
            if idx < 0 or idx >= len(self._metadata):
                continue
            entry = dict(self._metadata[idx])
            entry["score"] = float(dist)
            results.append(entry)
        return results

    def list_sessions(self) -> list[dict[str, Any]]:
        return [
            {
                "topic": m["topic"],
                "timestamp": m["timestamp"],
                "preview": m["report"][:200] + "..." if len(m["report"]) > 200 else m["report"],
            }
            for m in reversed(self._metadata)
        ]

    def clear(self) -> None:
        self._index = faiss.IndexFlatL2(self._dim)
        self._metadata = []
        self._save()


# --- Search ---
def web_search(query: str, max_results: int = 5) -> list[dict[str, str]]:
    try:
        with DDGS() as ddgs:
            results = list(ddgs.text(query, max_results=max_results))
        return [
            {"title": r.get("title", ""), "snippet": r.get("body", ""), "url": r.get("href", "")}
            for r in results
        ]
    except Exception:
        return []


def format_search_results(results: list[dict[str, str]]) -> str:
    if not results:
        return "No web results found. Use your training knowledge and note assumptions."
    lines = []
    for i, r in enumerate(results, 1):
        lines.append(f"[{i}] {r['title']}\n    URL: {r['url']}\n    {r['snippet']}")
    return "\n\n".join(lines)


# --- LangGraph state & agents ---
class ResearchState(TypedDict):
    topic: str
    memory_context: str
    research_notes: str
    sources: list[str]
    critique: str
    critique_approved: bool
    synthesis: str
    report: str
    iteration: int
    messages: Annotated[list, add_messages]


def _llm() -> ChatGroq:
    return ChatGroq(groq_api_key=GROQ_API_KEY, model_name=GROQ_MODEL, temperature=0.3)


def _invoke(system: str, user: str) -> str:
    response = _llm().invoke([SystemMessage(content=system), HumanMessage(content=user)])
    return response.content.strip()


def researcher_node(state: ResearchState) -> dict:
    topic = state["topic"]
    search_results = web_search(f"{topic} research overview latest developments", max_results=6)
    sources = [r["url"] for r in search_results if r.get("url")]

    user_parts = [f"Topic: {topic}"]
    if state.get("memory_context"):
        user_parts.append(f"Relevant past research from memory:\n{state['memory_context']}")
    if state.get("research_notes") and state.get("critique"):
        user_parts.append(
            f"Previous research (needs improvement):\n{state['research_notes']}\n\n"
            f"Critic feedback to address:\n{state['critique']}"
        )
    user_parts.append(f"Web search results:\n{format_search_results(search_results)}")

    notes = _invoke(
        "You are a Research Agent. Gather comprehensive, factual research on the given topic. "
        "Structure your notes with: Key Concepts, Current State, Important Developments, "
        "Open Questions, and Data Points. Be thorough and cite sources by URL when available.",
        "\n\n".join(user_parts),
    )
    return {
        "research_notes": notes,
        "sources": sources,
        "messages": [AIMessage(content=f"[Researcher] Completed research on: {topic}")],
    }


def critic_node(state: ResearchState) -> dict:
    iteration = state.get("iteration", 0)
    critique = _invoke(
        "You are a Critic Agent. Rigorously evaluate research quality. "
        "Check for: factual gaps, weak reasoning, missing perspectives, outdated info, "
        "and lack of evidence. End your response with exactly one line: "
        "VERDICT: APPROVED or VERDICT: REVISE",
        f"Topic: {state['topic']}\n\nResearch notes to evaluate:\n{state['research_notes']}",
    )
    approved = "VERDICT: APPROVED" in critique.upper()
    force_approve = iteration >= MAX_RESEARCH_ITERATIONS - 1
    return {
        "critique": critique,
        "critique_approved": approved or force_approve,
        "iteration": iteration + 1,
        "messages": [
            AIMessage(
                content=f"[Critic] {'Approved' if (approved or force_approve) else 'Requested revision'}"
            )
        ],
    }


def synthesizer_node(state: ResearchState) -> dict:
    user_parts = [
        f"Topic: {state['topic']}",
        f"Research notes:\n{state['research_notes']}",
        f"Critic evaluation:\n{state['critique']}",
    ]
    if state.get("memory_context"):
        user_parts.append(f"Historical context from memory:\n{state['memory_context']}")

    synthesis = _invoke(
        "You are a Synthesizer Agent. Merge research findings and critique into a unified "
        "analytical framework. Identify themes, connections, contradictions, and insights. "
        "Produce a structured synthesis ready for report writing.",
        "\n\n".join(user_parts),
    )
    return {
        "synthesis": synthesis,
        "messages": [AIMessage(content="[Synthesizer] Unified analytical framework ready")],
    }


def writer_node(state: ResearchState) -> dict:
    sources = state.get("sources", [])
    source_block = "\n".join(f"- {url}" for url in sources) if sources else "No external URLs captured."
    report = _invoke(
        "You are a Writer Agent. Produce a polished, publication-quality deep-research report. "
        "Use markdown formatting with: Executive Summary, Introduction, Key Findings, "
        "Deep Analysis, Implications, Limitations, and Conclusion. "
        "Write in clear, authoritative prose. Include a Sources section at the end.",
        f"Topic: {state['topic']}\n\nSynthesis to expand into full report:\n{state['synthesis']}\n\n"
        f"Available sources:\n{source_block}",
    )
    return {
        "report": report,
        "messages": [AIMessage(content="[Writer] Deep-research report completed")],
    }


def _route_after_critic(state: ResearchState) -> str:
    return "synthesizer" if state.get("critique_approved", False) else "researcher"


def build_research_graph():
    graph = StateGraph(ResearchState)
    graph.add_node("researcher", researcher_node)
    graph.add_node("critic", critic_node)
    graph.add_node("synthesizer", synthesizer_node)
    graph.add_node("writer", writer_node)
    graph.set_entry_point("researcher")
    graph.add_edge("researcher", "critic")
    graph.add_conditional_edges(
        "critic",
        _route_after_critic,
        {"researcher": "researcher", "synthesizer": "synthesizer"},
    )
    graph.add_edge("synthesizer", "writer")
    graph.add_edge("writer", END)
    return graph.compile()


# --- Streamlit UI ---
st.set_page_config(page_title="Deep Research Agents", page_icon="πŸ”¬", layout="wide")


def init_session():
    if "memory" not in st.session_state:
        st.session_state.memory = ResearchMemory()
    if "graph" not in st.session_state:
        st.session_state.graph = build_research_graph()
    if "running" not in st.session_state:
        st.session_state.running = False


def render_agent_status(agent: str, status: str):
    color = AGENT_COLORS.get(agent, "#888")
    st.markdown(
        f'<div style="padding:8px 12px;margin:4px 0;border-left:4px solid {color};'
        f'background:#1a1a2e;border-radius:4px;">'
        f'<strong style="color:{color}">{agent}</strong> β€” {status}</div>',
        unsafe_allow_html=True,
    )


def run_research(topic: str, use_memory: bool):
    memory: ResearchMemory = st.session_state.memory
    graph = st.session_state.graph

    memory_context = ""
    if use_memory:
        recalled = memory.recall(topic, k=3)
        if recalled:
            memory_context = "\n\n---\n\n".join(
                f"Past topic: {r['topic']} (relevance score: {r['score']:.3f})\n{r['report'][:1500]}"
                for r in recalled
            )

    progress = st.progress(0, text="Initializing agents...")
    status_box = st.empty()
    stages = [
        ("Researcher", "Gathering information..."),
        ("Critic", "Evaluating research quality..."),
        ("Synthesizer", "Building analytical framework..."),
        ("Writer", "Composing final report..."),
    ]

    initial_state = {
        "topic": topic,
        "memory_context": memory_context,
        "research_notes": "",
        "sources": [],
        "critique": "",
        "critique_approved": False,
        "synthesis": "",
        "report": "",
        "iteration": 0,
        "messages": [],
    }

    final_state = None
    stage_idx = 0

    for event in graph.stream(initial_state, stream_mode="updates"):
        for node_name, node_output in event.items():
            stage_idx = {"researcher": 0, "critic": 1, "synthesizer": 2, "writer": 3}.get(node_name, stage_idx)
            progress.progress(min((stage_idx + 1) / len(stages), 1.0), text=stages[stage_idx][1])
            with status_box.container():
                for i, (agent, desc) in enumerate(stages):
                    if i <= stage_idx:
                        render_agent_status(agent, "βœ“ " + desc if i < stage_idx else desc)
            if final_state is None:
                final_state = {**initial_state, **node_output}
            else:
                final_state.update(node_output)

    progress.progress(1.0, text="Research complete!")
    return final_state


def main():
    init_session()
    memory: ResearchMemory = st.session_state.memory

    st.title("πŸ”¬ Deep Research Agents")
    st.caption(
        "Multi-agent research pipeline β€” **Researcher** β†’ **Critic** β†’ **Synthesizer** β†’ **Writer** "
        "powered by Groq + LangGraph + FAISS"
    )

    with st.sidebar:
        st.header("Settings")
        use_memory = st.toggle("Use cross-session memory", value=True)
        st.divider()
        st.subheader("Past Sessions")
        sessions = memory.list_sessions()
        if sessions:
            for s in sessions[:10]:
                st.markdown(f"**{s['topic']}**")
                st.caption(s["timestamp"][:19])
                st.text(s["preview"][:120])
                st.divider()
        else:
            st.info("No past sessions yet.")
        if st.button("Clear memory", type="secondary"):
            memory.clear()
            st.rerun()

    if not GROQ_API_KEY:
        st.error(
            "Set your `GROQ_API_KEY` environment variable. "
            "Get a free key at [console.groq.com](https://console.groq.com)."
        )
        st.stop()

    col1, col2 = st.columns([3, 1])
    with col1:
        topic = st.text_input(
            "Research topic",
            placeholder="e.g. The impact of quantum computing on cryptography",
        )
    with col2:
        st.write("")
        st.write("")
        submit = st.button("Start Research", type="primary", disabled=st.session_state.running)

    if submit and topic.strip():
        st.session_state.running = True
        with st.spinner("Agents collaborating..."):
            result = run_research(topic.strip(), use_memory)

        if result and result.get("report"):
            st.success("Report generated successfully!")
            tabs = st.tabs(["πŸ“„ Report", "πŸ” Research Notes", "βš–οΈ Critique", "🧩 Synthesis"])
            with tabs[0]:
                st.markdown(result["report"])
            with tabs[1]:
                st.markdown(result.get("research_notes", ""))
            with tabs[2]:
                st.markdown(result.get("critique", ""))
            with tabs[3]:
                st.markdown(result.get("synthesis", ""))
            if use_memory:
                memory.store(topic.strip(), result["report"])
                st.toast("Saved to cross-session memory", icon="πŸ’Ύ")
            st.download_button(
                "Download Report",
                data=result["report"],
                file_name=f"research_{topic[:40].replace(' ', '_')}.md",
                mime="text/markdown",
            )
        else:
            st.error("Research pipeline did not produce a report. Check your API key and try again.")
        st.session_state.running = False

    with st.expander("How it works"):
        st.markdown(
            """

            | Agent | Role |

            |-------|------|

            | **Researcher** | Web search + knowledge synthesis into structured notes |

            | **Critic** | Quality gate β€” gaps, bias, evidence checks; loops back if needed |

            | **Synthesizer** | Merges research + critique into unified analytical framework |

            | **Writer** | Produces publication-quality markdown report |

            """
        )


if __name__ == "__main__":
    main()