Repository navigation
Expand file tree
/
Copy pathui.py
More file actions
802 lines (715 loc) Β· 34 KB
/
Copy pathui.py
File metadata and controls
802 lines (715 loc) Β· 34 KB
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
import base64
import streamlit as st
import model_registry
from contextlib import ExitStack
from dataclasses import replace
from datetime import datetime
from pathlib import Path
from scrape import scrape_multiple
from search import engines_unreachable, get_search_results_detailed
from llm_utils import (
BufferedStreamingHandler,
default_model,
get_model_choices,
get_model_display_names,
resolve_model_config,
)
from llm import get_llm, answer_followup, build_followup_context
# The investigation lives in pipeline.py, which the MCP server calls too. This
# page supplies the widgets, the spinners and the panels.
from pipeline import PipelineError, followup_model, run_investigation
from prompts import PRESETS, PRESET_PROMPTS
from store import load_investigations
from langchain_core.messages import HumanMessage, AIMessage
from config import DEPTH_LIMITS, RobinConfig
from health import check_llm_health, check_search_engines, check_tor_proxy
def _render_pipeline_error(stage: str, err: Exception) -> None:
message = str(err).strip() or err.__class__.__name__
lower_msg = message.lower()
status = getattr(err, "status_code", None)
hints = []
if any(t in lower_msg for t in ("context length", "context_length", "too many tokens",
"maximum context", "reduce the length", "token limit")):
hints = ["- The investigation exceeded the model's context window. Lower "
"**Content per Page** or **Max Pages to Scrape**, or pick a model with a larger window.",
"- On Ollama, raise `OLLAMA_NUM_CTX` (see TROUBLESHOOTING.md)."]
elif any(t in lower_msg for t in ("temperature", "top_p", "top_k", "reasoning_effort",
"thinking_budget", "unsupported parameter", "unknown parameter",
"unexpected keyword", "not supported", "deprecated parameter")):
hints = ["- The selected model rejected a request setting. Update Robin and its "
"provider dependencies, or choose another model.",
"- Current models can require their default sampling settings; changing the API key will not fix this error."]
elif status in (402, 429) or any(t in lower_msg for t in ("rate limit", "rate_limit", "429", "quota",
"resource has been exhausted", "resource_exhausted", "credits are depleted", "too many requests")):
hints = ["- The provider's rate limit or quota was reached. Check your billing and quota; "
"for temporary rate limits, wait and retry, or pick another model."]
elif status in (401, 403) or any(t in lower_msg for t in ("invalid api key", "invalid_api_key",
"authentication", "unauthorized", "permission denied", "api_key` is not set", "user not found")):
hints = ["- Confirm the selected provider's API key is set in your `.env` or shell, "
"is valid, and has access to this model.",
"- Remove any spaces around the key and restart Robin after updating it."]
elif "retired" in lower_msg or ((status == 404 or "model_not_found" in lower_msg)
and any(t in lower_msg for t in ("deprecated", "shut down", "shutdown"))):
hints = ["- The provider has retired this model. Select an active model from the sidebar.",
"- Restarting or refreshing cannot restore a retired model. Update Robin if it still appears in the picker."]
elif any(t in lower_msg for t in ("model_not_found", "model not found", "does not exist")):
hints = ["- Refresh the model list or restart Robin, then select a model your provider currently serves."]
elif status == 400:
hints = ["- The provider rejected the request. Check the error above for the setting "
"or input it rejected; update Robin and its provider dependencies if needed."]
else:
hints = ["- Check the provider error above. Retry or choose another model if the provider is unavailable."]
st.error(
"β Failed to {}.\n\nError: {}\n\n{}".format(
stage,
message,
"\n".join(hints),
)
)
st.stop()
def _render_no_results(headline: str, hints: list) -> None:
"""Stop the page and say plainly that nothing was found.
An empty result is a real answer, so no report is built from whatever
links happen to be left.
"""
st.warning("π {}\n\n{}".format(headline, "\n".join(hints)))
st.stop()
# Cached backend calls. They live here because `st.cache_data` is Streamlit's,
# and the pipeline takes them as injected callables so a repeated query reuses
# the cached search and scrape.
@st.cache_data(ttl=200, show_spinner=False)
def _cached_search(refined_query: str, threads: int):
return get_search_results_detailed(refined_query, max_workers=threads)
def cached_search_results(refined_query: str, threads: int):
"""The cached search, except an outage: a retry has to search again."""
outcome = _cached_search(refined_query, threads)
if engines_unreachable(outcome["stats"]):
_cached_search.clear(refined_query, threads)
return outcome
@st.cache_data(ttl=200, show_spinner=False)
def cached_scrape_multiple(filtered: list, threads: int, content_chars: int):
return scrape_multiple(filtered, max_workers=threads,
max_return_chars=content_chars)
st.set_page_config(
page_title="Robin: AI-Powered Dark Web OSINT Tool",
page_icon="π΅οΈββοΈ",
initial_sidebar_state="expanded",
)
st.markdown(
"""
<style>
.aStyle {
font-size: 18px;
font-weight: bold;
padding: 5px;
padding-left: 0px;
text-align: left;
}
.colHeight { max-height: 40vh; overflow-y: auto; text-align: center; }
.pTitle { font-weight: bold; color: #FF4B4B; margin-bottom: 0.5em; }
</style>""",
unsafe_allow_html=True,
)
st.sidebar.title("Robin")
st.sidebar.text("AI-Powered Dark Web OSINT Tool")
st.sidebar.markdown(
"""Made by [Apurv Singh Gautam](https://www.linkedin.com/in/apurvsinghgautam/)"""
)
st.sidebar.subheader("Settings")
def _env_is_set(value) -> bool:
return bool(value and str(value).strip() and "your_" not in str(value))
# Seed session state from .env on first run. This must happen before
# get_model_choices.
_env_cfg = RobinConfig.from_env()
if "custom_api_url" not in st.session_state:
st.session_state["custom_api_url"] = _env_cfg.custom_api_base_url or ""
if "custom_api_key" not in st.session_state:
st.session_state["custom_api_key"] = _env_cfg.custom_api_key or ""
if "custom_api_model" not in st.session_state:
st.session_state["custom_api_model"] = _env_cfg.custom_api_model or ""
# The config for this rerun is the environment, with whatever the user typed
# into the Custom API Provider box layered on top.
_robin_cfg = replace(
_env_cfg,
custom_api_base_url=st.session_state["custom_api_url"].strip() or None,
custom_api_key=st.session_state["custom_api_key"].strip() or None,
custom_api_model=st.session_state["custom_api_model"].strip() or None,
)
model_options = get_model_choices(_robin_cfg)
model_display_names = get_model_display_names(model_options, _robin_cfg)
# Preselect the newest inexpensive model. The rule lives in llm_utils because
# robin_investigate needs the same answer when nobody names a model.
default_model_index = (model_options.index(default_model(model_options))
if model_options else 0)
if not model_options:
# Distinguish "nothing configured" from "configured but unreachable": a
# good key with an unreachable provider is a different fix.
try:
_configured = model_registry.configured_providers(_robin_cfg)
except Exception:
_configured = []
# In the main area, not the sidebar: with the sidebar closed, the page would
# otherwise be blank.
if _configured:
st.error(
"β **Could not load models for: {}.**\n\n"
"Check the provider's availability and model access for this key, "
"then refresh the model list. "
"See TROUBLESHOOTING.md.".format(", ".join(_configured))
)
elif Path(__file__).with_name(".env").is_dir():
# Docker mounts a .env that does not exist yet as an empty folder.
st.error(
"β **`.env` is a folder, not a file, so no API keys were loaded.**\n\n"
"Docker creates an empty folder when the `.env` you mount does not "
"exist yet. Stop Robin, delete that folder, create a `.env` file with "
"your API key in it (see `.env.example`), and start Robin again.\n\n"
"See TROUBLESHOOTING.md."
)
else:
st.error(
"β **No LLM models available.**\n\n"
"No API keys or local providers are configured. "
"Set at least one in your `.env` file and restart Robin.\n\n"
"See TROUBLESHOOTING.md."
)
st.stop()
model = st.sidebar.selectbox(
"Select LLM Model",
model_options,
format_func=lambda m: model_display_names.get(m, m),
index=default_model_index,
key="model_select",
)
if any(model_display_names.get(name, "").startswith("[ollama]") for name in model_options):
st.sidebar.caption("Locally detected Ollama models are automatically added to this list.")
with st.sidebar.expander("π Custom API Provider"):
st.text_input(
"Base URL",
key="custom_api_url",
placeholder="https://api.groq.com/openai/v1",
help="Base URL for any OpenAI-compatible API (Groq, Mistral, LM Studio, etc.)",
)
st.text_input(
"API Key",
key="custom_api_key",
type="password",
help="API key for the custom provider (leave blank if not required)",
)
st.text_input(
"Model Name",
key="custom_api_model",
placeholder="llama-3.3-70b-versatile",
help="Model to use. Required if the provider doesn't expose /v1/models for auto-discovery.",
)
# Starting values come from ROBIN_DEFAULT_* when set, already clamped into these
# ranges by config.py; the ranges are the same ones the MCP server advertises.
threads = st.sidebar.slider(
"Scraping Threads", *DEPTH_LIMITS["threads"], _env_cfg.default_threads,
key="thread_slider",
)
max_results = st.sidebar.slider(
"Max Results to Filter", *DEPTH_LIMITS["max_results"], _env_cfg.default_max_results,
key="max_results_slider",
help="Cap the number of raw search results passed to the LLM filter step.",
)
max_scrape = st.sidebar.slider(
"Max Pages to Scrape", *DEPTH_LIMITS["max_scrape"], _env_cfg.default_max_scrape,
key="max_scrape_slider",
help="Cap the number of filtered results that get scraped for content.",
)
content_chars = st.sidebar.slider(
"Content per Page (characters)", *DEPTH_LIMITS["content_chars"],
_env_cfg.default_content_chars, step=1000,
key="content_chars_slider",
help="How much of each scraped page the model reads. Higher means richer "
"reports and more tokens per investigation. Raising this does not slow "
"down the Tor scrape.",
)
st.sidebar.caption(
"~{:,} characters (~{:,} tokens) sent to the model per investigation.".format(
content_chars * max_scrape, (content_chars * max_scrape) // 4
)
)
st.sidebar.divider()
st.sidebar.subheader("Provider Configuration")
_providers = [
("OpenAI", _robin_cfg.openai_api_key, True),
("Anthropic", _robin_cfg.anthropic_api_key, True),
("Google", _robin_cfg.google_api_key, True),
("Mistral", _robin_cfg.mistral_api_key, True),
("OpenRouter", _robin_cfg.openrouter_api_key, True),
("Ollama", _robin_cfg.ollama_base_url, False),
("llama.cpp", _robin_cfg.llama_cpp_base_url, False),
]
for name, value, is_cloud in _providers:
if _env_is_set(value):
st.sidebar.markdown(f" β
**{name}** β configured")
elif is_cloud:
st.sidebar.markdown(f" β οΈ **{name}** β API key not set")
else:
st.sidebar.markdown(f" π΅ **{name}** β not configured *(optional)*")
with st.sidebar.expander("βοΈ Prompt Settings"):
# Labels come from the preset catalog, so the sidebar and the MCP tools name
# the four domains identically.
preset_options = {label: key for key, (label, _) in PRESETS.items()}
preset_placeholders = {
"threat_intel": "e.g. Pay extra attention to cryptocurrency wallet addresses and exchange names.",
"ransomware_malware": "e.g. Highlight any references to double-extortion tactics or known ransomware-as-a-service affiliates.",
"personal_identity": "e.g. Flag any passport or government ID numbers and note which country they appear to be from.",
"corporate_espionage": "e.g. Prioritize any mentions of source code repositories, API keys, or internal Slack/email dumps.",
}
_preset_keys = list(preset_options.values())
selected_preset_label = st.selectbox(
"Research Domain",
list(preset_options.keys()),
index=(_preset_keys.index(_env_cfg.default_preset)
if _env_cfg.default_preset in _preset_keys else 0),
key="preset_select",
)
selected_preset = preset_options[selected_preset_label]
st.text_area(
"System Prompt",
value=PRESET_PROMPTS[selected_preset].strip(),
height=200,
disabled=True,
key="system_prompt_display",
)
custom_instructions = st.text_area(
"Custom Instructions (optional)",
placeholder=preset_placeholders[selected_preset],
height=100,
key="custom_instructions",
)
st.sidebar.divider()
st.sidebar.subheader("Health Checks")
if st.sidebar.button("π Check LLM Connection", use_container_width=True):
with st.sidebar:
with st.spinner(f"Testing {model}..."):
result = check_llm_health(model, _robin_cfg)
if result["status"] == "up":
st.sidebar.success(
f"β
**{result['provider']}** β Connected ({result['latency_ms']}ms)"
)
else:
st.sidebar.error(
f"β **{result['provider']}** β Failed\n\n{result['error']}"
)
if st.sidebar.button("π Check Search Engines", use_container_width=True):
with st.sidebar:
with st.spinner("Checking Tor proxy..."):
tor_result = check_tor_proxy()
if tor_result["status"] == "down":
st.sidebar.error(
f"β **Tor Proxy** β Not reachable\n\n{tor_result['error']}\n\n"
"Ensure Tor is running: `sudo systemctl start tor`"
)
elif tor_result["status"] != "up":
st.sidebar.warning(
f"β³ **Tor Proxy** β Still starting\n\n{tor_result['error']}\n\n"
"The engines cannot be reached until Tor has bootstrapped."
)
else:
st.sidebar.success(
f"β
**Tor Proxy** β Connected ({tor_result['latency_ms']}ms)"
)
with st.spinner("Pinging 16 search engines via Tor..."):
engine_results = check_search_engines()
up_count = sum(1 for r in engine_results if r["status"] == "up")
total = len(engine_results)
if up_count == total:
st.sidebar.success(f"β
**All {total} engines reachable**")
elif up_count > 0:
st.sidebar.warning(f"β οΈ **{up_count}/{total} engines reachable**")
else:
st.sidebar.error(f"β **0/{total} engines reachable**")
for r in engine_results:
if r["status"] == "up":
st.sidebar.markdown(
f" π’ **{r['name']}** β {r['latency_ms']}ms"
)
else:
st.sidebar.markdown(
f" π΄ **{r['name']}** β {r['error']}"
)
st.sidebar.divider()
st.sidebar.subheader("π Past Investigations")
saved_investigations = load_investigations()
if saved_investigations:
inv_labels = [
f"{inv['_filename'].replace('investigation_','').replace('.json','')} β {inv['query'][:40]}"
for inv in saved_investigations
]
selected_inv_label = st.sidebar.selectbox(
"Load investigation", ["(none)"] + inv_labels, key="inv_select"
)
if selected_inv_label != "(none)":
selected_inv_idx = inv_labels.index(selected_inv_label)
if st.sidebar.button("π Load", use_container_width=True, key="load_inv_btn"):
_saved = saved_investigations[selected_inv_idx]
# Some saved files carry only the display label, not the preset key,
# so the label is mapped back to its key for follow-ups.
_saved_preset = _saved.get("preset", "threat_intel")
if _saved.get("preset_key") in PRESETS:
_preset_key = _saved["preset_key"]
elif _saved_preset in preset_options:
_preset_key = preset_options[_saved_preset]
elif _saved_preset in preset_options.values():
_preset_key = _saved_preset
else:
_preset_key = "threat_intel"
# The same keys a fresh run stores below, with None where the file
# lacks a value: the follow-up chat reads content_chars and
# max_scrape from either shape.
st.session_state["active_investigation"] = {
"query": _saved.get("query", ""),
"refined": _saved.get("refined_query", ""),
"model": _saved.get("model", ""),
"preset": _preset_key,
"preset_label": _saved.get("preset", ""),
"sources": _saved.get("sources", []),
"scraped": None, # The raw scrape is not saved to disk.
"summary": _saved.get("summary", ""),
"results_count": _saved.get("results_count",
len(_saved.get("sources", []))),
"content_chars": _saved.get("content_chars"),
"max_scrape": _saved.get("max_scrape"),
"timestamp": _saved.get("timestamp", ""),
}
st.session_state["chat_history"] = []
# The saved record carries the pivots, so a reloaded investigation
# offers the same follow-up searches as the run that wrote it.
st.session_state["pivot_suggestions"] = list(_saved.get("pivots") or [])
st.rerun()
else:
st.sidebar.caption("No saved investigations yet.")
_, logo_col, _ = st.columns(3)
with logo_col:
st.image(".github/assets/robin_logo.png", width=200)
with st.form("search_form", clear_on_submit=True):
col_input, col_button = st.columns([10, 1])
query = col_input.text_input(
"Enter Dark Web Search Query",
placeholder="Enter Dark Web Search Query",
label_visibility="collapsed",
key="query_input",
)
run_button = col_button.form_submit_button("Run")
status_slot = st.empty()
_stat_cols = st.columns(3)
p1, p2, p3 = [col.empty() for col in _stat_cols]
notes_placeholder = st.empty()
sources_placeholder = st.empty()
findings_placeholder = st.empty()
def _render_investigation_body(inv):
"""Render Notes / Sources / Findings / Download for a stored investigation."""
with st.expander("π Notes", expanded=False):
st.markdown(f"**Refined Query:** `{inv.get('refined', '')}`")
st.markdown(
f"**Model:** `{inv.get('model', '')}` "
f"**Domain:** {inv.get('preset_label') or inv.get('preset', '')}"
)
_counts = f"**Sources:** {len(inv.get('sources', []))}"
if inv.get("scraped"):
_counts += f" **Scraped:** {len(inv['scraped'])}"
st.markdown(_counts)
sources = inv.get("sources", [])
with st.expander(f"π Sources ({len(sources)} results)", expanded=False):
for i, item in enumerate(sources, 1):
st.markdown(f"{i}. [{item.get('title', 'Untitled')}]({item.get('link', '')})")
st.subheader(":red[π Findings]", anchor=None, divider="gray")
summary = inv.get("summary", "") or ""
st.markdown(summary)
if summary:
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
b64 = base64.b64encode(summary.encode()).decode()
href = (
f'<div class="aStyle">π₯ <a href="data:file/markdown;base64,{b64}" '
f'download="summary_{now}.md">Download</a></div>'
)
st.markdown(href, unsafe_allow_html=True)
def _followup_history_messages(chat_history, max_turns=5):
"""Convert the last `max_turns` Q&A turns into LangChain messages for the model."""
recent = chat_history[-(max_turns * 2):] if chat_history else []
msgs = []
for turn in recent:
if turn.get("role") == "user":
msgs.append(HumanMessage(content=turn.get("content", "")))
else:
msgs.append(AIMessage(content=turn.get("content", "")))
return msgs
def _render_chat_panel(inv):
"""Suggested pivots, chat history, clear control, and the follow-up input."""
st.divider()
st.subheader(":red[π¬ Follow-up Chat]", anchor=None, divider="gray")
# One click on a suggested pivot runs it as a new investigation.
pivots = st.session_state.get("pivot_suggestions", [])
if pivots:
st.caption("Suggested pivots β click to run as a new investigation:")
pivot_cols = st.columns(len(pivots))
for i, (col, pq) in enumerate(zip(pivot_cols, pivots)):
if col.button(f"π {pq}", key=f"pivot_{i}", use_container_width=True):
st.session_state["pivot_query"] = pq
st.rerun()
for turn in st.session_state.get("chat_history", []):
with st.chat_message(turn.get("role", "assistant")):
st.markdown(turn.get("content", ""))
if st.session_state.get("chat_history"):
if st.button("π§Ή Clear chat", key="clear_chat"):
st.session_state["chat_history"] = []
st.rerun()
# A follow-up is answered from this investigation's context.
followup = st.chat_input("Ask a follow-up about this investigation")
if followup:
with st.chat_message("user"):
st.markdown(followup)
# Budget the follow-up against the same amount of evidence the summary
# saw. A fixed 12,000 would answer chat questions from a fraction of a
# high-budget investigation while the report used all of it.
_inv_budget = inv.get("content_chars") and inv.get("max_scrape")
context = build_followup_context(
inv.get("query", ""), inv.get("refined", ""),
inv.get("sources", []), inv.get("scraped"), inv.get("summary", ""),
char_budget=(inv["content_chars"] * inv["max_scrape"]) if _inv_budget else 12000,
)
history = _followup_history_messages(st.session_state.get("chat_history", []))
with st.chat_message("assistant"):
answer_slot = st.empty()
acc = {"text": ""}
def _emit(chunk: str):
acc["text"] += chunk
answer_slot.markdown(acc["text"])
try:
# The stored model is provenance. A report an agent saved
# stores "host", which is not a model this page can build, so
# the follow-up runs on the sidebar's model instead.
f_llm = get_llm(followup_model(
inv.get("model"), model,
lambda name: resolve_model_config(name, _robin_cfg) is not None,
), _robin_cfg)
f_llm.callbacks = [BufferedStreamingHandler(ui_callback=_emit)]
answer = answer_followup(
f_llm, followup, context, history=history,
preset=inv.get("preset", "threat_intel"),
)
# Reasoning models stream nothing, so fall back to the return value.
if not acc["text"].strip() and answer:
acc["text"] = answer
answer_slot.markdown(answer)
except Exception as e:
acc["text"] = f"β οΈ Failed to answer follow-up: {e}"
answer_slot.markdown(acc["text"])
st.session_state.setdefault("chat_history", [])
st.session_state["chat_history"].append({"role": "user", "content": followup})
st.session_state["chat_history"].append({"role": "assistant", "content": acc["text"]})
_STAGE_LABELS = {
"load_llm": "π Loading LLM...",
"refine": "π Refining query...",
"search": "π Searching dark web...",
"filter": "ποΈ Filtering results...",
"scrape": "π Scraping content...",
"summarize": "βοΈ Generating summary...",
"pivots": "π‘ Suggesting pivots...",
"save": "πΎ Saving investigation...",
}
class _StageIndicator:
"""One spinner at a time in the status slot, driven by the pipeline.
The investigation is one blocking call, so the stage callback enters and
closes the spinner's context managers by hand instead of a `with` block.
"""
def __init__(self, slot):
self._slot = slot
self._stack = None
def show(self, label: str) -> None:
self.close()
if not label:
return
stack = ExitStack()
stack.enter_context(self._slot.container())
stack.enter_context(st.spinner(label))
self._stack = stack
def close(self) -> None:
if self._stack is not None:
self._stack.close()
self._stack = None
def _render_stat(slot, title, value) -> None:
slot.container(border=True).markdown(
f"<div class='colHeight'><p class='pTitle'>{title}</p><p>{value}</p></div>",
unsafe_allow_html=True,
)
class _StatTiles:
"""The three counters: "β¦" while a tile's stage runs, its value once done."""
_ORDER = tuple(_STAGE_LABELS)
_TILES = (
("Refined Query", "refine", lambda inv: inv.refined),
("Search Results", "search", lambda inv: len(inv.results)),
("Filtered Results", "filter", lambda inv: len(inv.filtered)),
)
def __init__(self, slots):
self._slots = slots
self._reached = -1
def stage_started(self, stage: str, inv) -> None:
if stage in self._ORDER:
self._reached = self._ORDER.index(stage)
self._render(inv, running=True)
def finish(self, inv) -> None:
self._render(inv, running=False)
def fail(self) -> None:
"""Clear the tile whose stage failed, so no "β¦" is left behind."""
for slot, (_, stage, _) in zip(self._slots, self._TILES):
if self._ORDER.index(stage) == self._reached:
slot.empty()
def _render(self, inv, running: bool) -> None:
for slot, (title, stage, value) in zip(self._slots, self._TILES):
position = self._ORDER.index(stage)
if position < self._reached or (position == self._reached and not running):
_render_stat(slot, title, value(inv))
elif position == self._reached:
_render_stat(slot, title, "β¦")
# A run is triggered by a submitted query OR a one-click pivot from the chat panel.
_pivot_query = st.session_state.pop("pivot_query", None)
_active_query = _pivot_query or query
_do_run = bool(_active_query) and (run_button or _pivot_query is not None)
if _do_run:
query = _active_query
for k in ["active_investigation", "chat_history", "pivot_suggestions"]:
st.session_state.pop(k, None)
_indicator = _StageIndicator(status_slot)
_tiles = _StatTiles((p1, p2, p3))
_summary_view = {"slot": None, "text": ""}
def _on_stage(stage: str, inv) -> None:
# The findings panel opens as the summary starts.
if stage == "summarize" and _summary_view["slot"] is None:
with findings_placeholder.container():
st.subheader(":red[π Findings]", anchor=None, divider="gray")
_summary_view["slot"] = st.empty()
_tiles.stage_started(stage, inv)
_indicator.show(_STAGE_LABELS.get(stage, ""))
def _on_token(chunk: str) -> None:
_summary_view["text"] += chunk
if _summary_view["slot"] is not None:
_summary_view["slot"].markdown(_summary_view["text"])
try:
investigation = run_investigation(
_robin_cfg, query, model,
preset=selected_preset,
custom_instructions=custom_instructions,
max_results=max_results,
max_scrape=max_scrape,
content_chars=content_chars,
threads=threads,
on_stage=_on_stage,
on_token=_on_token,
search_fn=cached_search_results,
scrape_fn=cached_scrape_multiple,
preset_label=selected_preset_label,
)
except PipelineError as e:
_tiles.fail()
_render_pipeline_error(e.action, e.original)
finally:
_indicator.close()
_tiles.finish(investigation)
if investigation.status == "engines_unreachable":
_render_no_results(
"No search engine answered, so nothing was searched.",
[
"- This is not an empty result: the search never ran.",
"- Run **Check Search Engines** in the sidebar to see which are down.",
"- Confirm Tor is running and reachable on `socks5h://127.0.0.1:9050`.",
],
)
if investigation.status == "no_results":
_render_no_results(
"No dark web results came back for this query.",
[
"- Try broader or differently worded search terms.",
"- Run **Check Search Engines** in the sidebar; onion engines have irregular uptime.",
"- Confirm Tor is running and reachable on `socks5h://127.0.0.1:9050`.",
],
)
if investigation.status == "nothing_relevant":
_render_no_results(
"Found {} raw links, but none of them matched this query.".format(
len(investigation.results)
),
[
"- The engines that responded returned nothing relevant to these terms.",
"- Try broader terms, or a different phrasing of the same question.",
"- Robin stops here on purpose rather than summarizing unrelated pages.",
],
)
if investigation.status == "nothing_readable":
_render_no_results(
"Found {} relevant results, but none of the pages could be read "
"over Tor right now.".format(len(investigation.filtered)),
[
"- Onion services have irregular uptime; the same pages often answer a few minutes later.",
"- Retry this investigation in a few minutes.",
"- Run **Check Search Engines** in the sidebar to confirm Tor is healthy.",
"- Raise **Max Pages to Scrape** so more candidates are tried.",
],
)
with notes_placeholder.container():
with st.expander("π Notes", expanded=False):
st.markdown(f"**Refined Query:** `{investigation.refined}`")
st.markdown(f"**Model:** `{model}` **Domain:** {selected_preset_label}")
st.markdown(
f"**Results found:** {len(investigation.results)} "
f"**Filtered to:** {len(investigation.filtered)} "
f"**Scraped:** {len(investigation.scraped)}"
)
with sources_placeholder.container():
with st.expander(f"π Sources ({len(investigation.filtered)} results)", expanded=False):
for i, item in enumerate(investigation.filtered, 1):
title = item.get("title", "Untitled")
link = item.get("link", "")
st.markdown(f"{i}. [{title}]({link})")
with findings_placeholder.container():
st.subheader(":red[π Findings]", anchor=None, divider="gray")
st.markdown(investigation.summary)
now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
fname = f"summary_{now}.md"
b64 = base64.b64encode(investigation.summary.encode()).decode()
href = f'<div class="aStyle">π₯ <a href="data:file/markdown;base64,{b64}" download="{fname}">Download</a></div>'
st.markdown(href, unsafe_allow_html=True)
if investigation.saved_as:
status_slot.success(
f"βοΈ Pipeline completed successfully! Investigation saved as `{investigation.saved_as}`"
)
else:
# The report is on the page and downloadable; only the copy on disk is
# missing, and that is a mount permission problem with a known fix.
status_slot.warning(
"βοΈ Pipeline completed, but the investigation could not be saved to disk.\n\n"
'See "Saved investigations fail with permission denied on Linux" in '
"TROUBLESHOOTING.md."
)
# Persist as the active investigation so it survives chat reruns.
st.session_state["active_investigation"] = {
"query": query,
"refined": investigation.refined,
"model": model,
"preset": selected_preset,
"preset_label": selected_preset_label,
"sources": investigation.filtered,
"scraped": investigation.scraped,
"summary": investigation.summary,
"results_count": len(investigation.results),
"content_chars": content_chars,
"max_scrape": max_scrape,
# Both shapes carry the same keys. This one stays empty because the banner
# above the chat panel only dates an investigation loaded from disk.
"timestamp": "",
}
st.session_state["chat_history"] = []
st.session_state["pivot_suggestions"] = investigation.pivots
_render_chat_panel(st.session_state["active_investigation"])
# Returning visit (no run this pass, e.g. after a chat submit or a loaded
# investigation): render the active investigation and its chat panel.
elif st.session_state.get("active_investigation"):
_inv = st.session_state["active_investigation"]
_ts = _inv.get("timestamp")
st.info(f"π **{_inv.get('query', '')}**" + (f" β {_ts[:16]}" if _ts else ""))
_render_investigation_body(_inv)
_render_chat_panel(_inv)