robert 2.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- robert/__init__.py +9 -0
- robert/__main__.py +20 -0
- robert/_version.py +3 -0
- robert/api.py +646 -0
- robert/aqme.py +280 -0
- robert/argument_parser.py +98 -0
- robert/checkml.py +433 -0
- robert/checkml_score.py +50 -0
- robert/curate.py +217 -0
- robert/evaluate_bot_retrieval.py +125 -0
- robert/evaluate_bot_support.py +102 -0
- robert/evaluate_workflow_summaries.py +87 -0
- robert/export_bot_docs_knowledge.py +1632 -0
- robert/generate.py +249 -0
- robert/generate_utils.py +311 -0
- robert/gui_easyrob/__init__.py +0 -0
- robert/gui_easyrob/assets/gif_easyrob.gif +0 -0
- robert/gui_easyrob/bot/__init__.py +47 -0
- robert/gui_easyrob/bot/answer_metadata.py +198 -0
- robert/gui_easyrob/bot/bot_context.py +679 -0
- robert/gui_easyrob/bot/bot_engine.py +1100 -0
- robert/gui_easyrob/bot/bot_panel.py +1231 -0
- robert/gui_easyrob/bot/bot_rag.py +1178 -0
- robert/gui_easyrob/bot/bot_window.py +39 -0
- robert/gui_easyrob/bot/bot_worker.py +149 -0
- robert/gui_easyrob/bot/credential_store.py +143 -0
- robert/gui_easyrob/bot/error_parser.py +105 -0
- robert/gui_easyrob/bot/evidence_policy.py +164 -0
- robert/gui_easyrob/bot/heuristics.py +859 -0
- robert/gui_easyrob/bot/icons.py +56 -0
- robert/gui_easyrob/bot/input_csv_audit.py +212 -0
- robert/gui_easyrob/bot/knowledge/aqme_context.json +5907 -0
- robert/gui_easyrob/bot/knowledge/robert_context.json +4817 -0
- robert/gui_easyrob/bot/knowledge/ui_context.json +8293 -0
- robert/gui_easyrob/bot/knowledge_schema.py +1252 -0
- robert/gui_easyrob/bot/knowledge_sources/aqme_user_guidance.json +442 -0
- robert/gui_easyrob/bot/knowledge_sources/coverage_manifest.json +95 -0
- robert/gui_easyrob/bot/knowledge_sources/robert_user_guidance.json +471 -0
- robert/gui_easyrob/bot/knowledge_sources/ui_user_guidance.json +1312 -0
- robert/gui_easyrob/bot/llm_providers.py +1573 -0
- robert/gui_easyrob/bot/online_docs.py +105 -0
- robert/gui_easyrob/bot/prompt_policy.py +575 -0
- robert/gui_easyrob/bot/question_intent.py +562 -0
- robert/gui_easyrob/bot/response_quality.py +104 -0
- robert/gui_easyrob/bot/result_insights.py +225 -0
- robert/gui_easyrob/bot/token_usage.py +75 -0
- robert/gui_easyrob/bot/workflow_results.py +1396 -0
- robert/gui_easyrob/checkml_launcher.py +15 -0
- robert/gui_easyrob/easyrob.py +29 -0
- robert/gui_easyrob/easyrob_launcher.py +113 -0
- robert/gui_easyrob/main/__init__.py +9 -0
- robert/gui_easyrob/main/window.py +5152 -0
- robert/gui_easyrob/result_navigation.py +14 -0
- robert/gui_easyrob/tabs/__init__.py +23 -0
- robert/gui_easyrob/tabs/advanced_options.py +370 -0
- robert/gui_easyrob/tabs/aqme.py +1234 -0
- robert/gui_easyrob/tabs/checkml.py +898 -0
- robert/gui_easyrob/tabs/images.py +427 -0
- robert/gui_easyrob/tabs/interactive_predictions.py +555 -0
- robert/gui_easyrob/tabs/molssi.py +447 -0
- robert/gui_easyrob/tabs/predictions.py +484 -0
- robert/gui_easyrob/tabs/result_catalog.py +170 -0
- robert/gui_easyrob/tabs/results.py +490 -0
- robert/gui_easyrob/tabs/results_workspace.py +120 -0
- robert/gui_easyrob/test_bot_input_csv_audit.py +152 -0
- robert/gui_easyrob/test_bot_question_routing.py +383 -0
- robert/gui_easyrob/test_bot_result_analysis.py +356 -0
- robert/gui_easyrob/test_bot_retrieval_evaluation.py +110 -0
- robert/gui_easyrob/test_bot_ui_integration.py +278 -0
- robert/gui_easyrob/test_checkml_app_mode.py +124 -0
- robert/gui_easyrob/test_checkml_completion.py +57 -0
- robert/gui_easyrob/test_checkml_defaults.py +37 -0
- robert/gui_easyrob/test_checkml_result_summary.py +113 -0
- robert/gui_easyrob/test_checkml_web_mode.py +57 -0
- robert/gui_easyrob/test_dialog_layout.py +33 -0
- robert/gui_easyrob/test_gui_bot_regressions.py +320 -0
- robert/gui_easyrob/test_molssi_auto.py +569 -0
- robert/gui_easyrob/test_tutorial_view.py +215 -0
- robert/gui_easyrob/tutorials/check_model.md +25 -0
- robert/gui_easyrob/tutorials/chemdraw.md +49 -0
- robert/gui_easyrob/tutorials/csv.md +53 -0
- robert/gui_easyrob/tutorials/descriptors.md +21 -0
- robert/gui_easyrob/tutorials/overview.md +65 -0
- robert/gui_easyrob/tutorials/predictions.md +25 -0
- robert/gui_easyrob/tutorials/robbot.md +29 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/check_model/check_model_7.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_10.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_11.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_12.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_13.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_7.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_8.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/chemdraw/chemdraw_9.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_1_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_7.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_8.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/csv/csv_9.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/descriptors/descriptors_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_7.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_1_8.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_5_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/overview/overview_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_1_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/predictions/predictions_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_1.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_2.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_3.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_4.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_5.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_6.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_7.png +0 -0
- robert/gui_easyrob/tutorials/tutorial_images/robbot/robbot_8.png +0 -0
- robert/gui_easyrob/utils/__init__.py +0 -0
- robert/gui_easyrob/utils/aqme_utils.py +238 -0
- robert/gui_easyrob/utils/molssi_utils.py +584 -0
- robert/gui_easyrob/utils/predictions_utils.py +985 -0
- robert/gui_easyrob/utils/utils_gui.py +738 -0
- robert/gui_easyrob/version.py +68 -0
- robert/gui_easyrob/web/app.css +484 -0
- robert/gui_easyrob/web/app.html +134 -0
- robert/gui_easyrob/web/app.js +764 -0
- robert/gui_easyrob/web/checkml_brand.png +0 -0
- robert/gui_easyrob/web/easyrob_background.gif +0 -0
- robert/gui_easyrob/web/easyrob_background.png +0 -0
- robert/gui_easyrob/web/images.css +89 -0
- robert/gui_easyrob/web/images.html +39 -0
- robert/gui_easyrob/web/images.js +101 -0
- robert/gui_easyrob/web/results.js +499 -0
- robert/gui_easyrob/web/robert_brand.png +0 -0
- robert/gui_easyrob/web_dialogs.py +64 -0
- robert/gui_easyrob/web_results.py +253 -0
- robert/gui_easyrob/web_shell.py +684 -0
- robert/gui_easyrob/workflow_progress.py +89 -0
- robert/gui_easyrob/workflow_run_lock.py +27 -0
- robert/icons/CheckML_icon.png +0 -0
- robert/icons/Robert_icon.png +0 -0
- robert/icons/Robert_logo.jpg +0 -0
- robert/icons/Robert_logo_transparent.png +0 -0
- robert/icons/documentation_icon.png +0 -0
- robert/icons/info_icon.png +0 -0
- robert/icons/pdf_icon.png +0 -0
- robert/icons/play_icon.png +0 -0
- robert/icons/stop_icon.png +0 -0
- robert/icons/youtube_icon.ico +0 -0
- robert/predict.py +193 -0
- robert/predict_utils.py +410 -0
- robert/report/CheckML_logo.jpg +0 -0
- robert/report/GENERATE.png +0 -0
- robert/report/Robert_icon.ico +0 -0
- robert/report/Robert_logo.jpg +0 -0
- robert/report/Robert_logo1.jpg +0 -0
- robert/report/abbrev.png +0 -0
- robert/report/adv_anal.png +0 -0
- robert/report/checkml_pass.svg +3 -0
- robert/report/checkml_unclear.svg +3 -0
- robert/report/checkml_warning.svg +3 -0
- robert/report/features.png +0 -0
- robert/report/outliers.png +0 -0
- robert/report/pred.png +0 -0
- robert/report/repro.png +0 -0
- robert/report/robust_x.png +0 -0
- robert/report/robust_y_high.png +0 -0
- robert/report/robust_y_low.png +0 -0
- robert/report/score.png +0 -0
- robert/report/score_0.jpg +0 -0
- robert/report/score_1.jpg +0 -0
- robert/report/score_10.jpg +0 -0
- robert/report/score_2.jpg +0 -0
- robert/report/score_3.jpg +0 -0
- robert/report/score_4.jpg +0 -0
- robert/report/score_5.jpg +0 -0
- robert/report/score_6.jpg +0 -0
- robert/report/score_7.jpg +0 -0
- robert/report/score_8.jpg +0 -0
- robert/report/score_9.jpg +0 -0
- robert/report/score_gray_0.jpg +0 -0
- robert/report/score_gray_1.jpg +0 -0
- robert/report/score_gray_10.jpg +0 -0
- robert/report/score_gray_2.jpg +0 -0
- robert/report/score_gray_3.jpg +0 -0
- robert/report/score_gray_4.jpg +0 -0
- robert/report/score_gray_5.jpg +0 -0
- robert/report/score_gray_6.jpg +0 -0
- robert/report/score_gray_7.jpg +0 -0
- robert/report/score_gray_8.jpg +0 -0
- robert/report/score_gray_9.jpg +0 -0
- robert/report/score_w_1_0.jpg +0 -0
- robert/report/score_w_1_1.jpg +0 -0
- robert/report/score_w_2_0.jpg +0 -0
- robert/report/score_w_2_1.jpg +0 -0
- robert/report/score_w_2_2.jpg +0 -0
- robert/report/score_w_2_gray_0.jpg +0 -0
- robert/report/score_w_2_gray_1.jpg +0 -0
- robert/report/score_w_2_gray_2.jpg +0 -0
- robert/report/score_w_3_0.jpg +0 -0
- robert/report/score_w_3_1.jpg +0 -0
- robert/report/score_w_3_2.jpg +0 -0
- robert/report/score_w_3_3.jpg +0 -0
- robert/report/score_w_4_0.jpg +0 -0
- robert/report/score_w_4_1.JPG +0 -0
- robert/report/score_w_4_2.jpg +0 -0
- robert/report/score_w_4_3.jpg +0 -0
- robert/report/score_w_4_4.jpg +0 -0
- robert/report/transpa.png +0 -0
- robert/report/y_distrib.png +0 -0
- robert/report.py +2880 -0
- robert/report_utils.py +2270 -0
- robert/robert.py +181 -0
- robert/utils.py +4763 -0
- robert/verify.py +606 -0
- robert-2.2.0.dist-info/METADATA +55 -0
- robert-2.2.0.dist-info/RECORD +261 -0
- robert-2.2.0.dist-info/WHEEL +5 -0
- robert-2.2.0.dist-info/entry_points.txt +3 -0
- robert-2.2.0.dist-info/licenses/LICENSE +21 -0
- robert-2.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1231 @@
|
|
|
1
|
+
"""Right-side conversational panel for robBOT."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from html import escape
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import re
|
|
8
|
+
from decimal import Decimal, ROUND_HALF_UP
|
|
9
|
+
|
|
10
|
+
from PySide6.QtCore import QEvent, Qt, Signal
|
|
11
|
+
from PySide6.QtGui import QColor, QMovie, QPalette
|
|
12
|
+
from PySide6.QtWidgets import (
|
|
13
|
+
QCheckBox,
|
|
14
|
+
QComboBox,
|
|
15
|
+
QDialog,
|
|
16
|
+
QFrame,
|
|
17
|
+
QHBoxLayout,
|
|
18
|
+
QLabel,
|
|
19
|
+
QLineEdit,
|
|
20
|
+
QMessageBox,
|
|
21
|
+
QPushButton,
|
|
22
|
+
QScrollArea,
|
|
23
|
+
QSizePolicy,
|
|
24
|
+
QTabWidget,
|
|
25
|
+
QTextBrowser,
|
|
26
|
+
QToolButton,
|
|
27
|
+
QVBoxLayout,
|
|
28
|
+
QWidget,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
from .llm_providers import provider_names
|
|
32
|
+
from .token_usage import MAX_USER_PROMPT_TOKENS, TokenUsage, estimate_token_count
|
|
33
|
+
|
|
34
|
+
__all__ = ["API_SETUP_GIF_PATH", "BotPanel", "SUGGESTED_QUESTIONS"]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
API_SETUP_GIF_PATH = Path(__file__).resolve().parents[1] / "assets" / "gif_easyrob.gif"
|
|
38
|
+
_SESSION_COST_NOTICE_USD = 0.05
|
|
39
|
+
_SESSION_COST_HIGH_USD = 0.25
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
SUGGESTED_QUESTIONS = (
|
|
43
|
+
"How does the GUI overview work?",
|
|
44
|
+
"How do I start from a CSV file?",
|
|
45
|
+
"How do I start from ChemDraw?",
|
|
46
|
+
"How do I make predictions for new molecules?",
|
|
47
|
+
"How do I generate descriptors without training a model?",
|
|
48
|
+
"How do I check my own machine learning model?",
|
|
49
|
+
"How do I use robBOT?",
|
|
50
|
+
)
|
|
51
|
+
SUGGESTED_TUTORIAL_IDS = (
|
|
52
|
+
"overview",
|
|
53
|
+
"csv",
|
|
54
|
+
"chemdraw",
|
|
55
|
+
"predictions",
|
|
56
|
+
"descriptors",
|
|
57
|
+
"check_model",
|
|
58
|
+
"robbot",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
from .answer_metadata import AnswerMetadata, EvidenceOrigin, SourceCitation
|
|
62
|
+
|
|
63
|
+
class _AnimatedGifLabel(QLabel):
|
|
64
|
+
"""Display native GIF frames with smooth, aspect-preserving scaling."""
|
|
65
|
+
|
|
66
|
+
def __init__(self, parent=None) -> None:
|
|
67
|
+
super().__init__(parent)
|
|
68
|
+
self._movie: QMovie | None = None
|
|
69
|
+
self.setAlignment(Qt.AlignCenter)
|
|
70
|
+
|
|
71
|
+
def set_movie(self, movie: QMovie) -> None:
|
|
72
|
+
movie.setParent(self)
|
|
73
|
+
self._movie = movie
|
|
74
|
+
movie.frameChanged.connect(self._update_frame)
|
|
75
|
+
self._update_frame()
|
|
76
|
+
|
|
77
|
+
def showEvent(self, event) -> None:
|
|
78
|
+
super().showEvent(event)
|
|
79
|
+
if self._movie is not None and self._movie.isValid():
|
|
80
|
+
if self._movie.state() != QMovie.Running:
|
|
81
|
+
self._movie.start()
|
|
82
|
+
|
|
83
|
+
def hideEvent(self, event) -> None:
|
|
84
|
+
if self._movie is not None and self._movie.state() == QMovie.Running:
|
|
85
|
+
self._movie.stop()
|
|
86
|
+
super().hideEvent(event)
|
|
87
|
+
|
|
88
|
+
def _update_frame(self, _frame_number: int = -1) -> None:
|
|
89
|
+
if self._movie is None:
|
|
90
|
+
return
|
|
91
|
+
frame = self._movie.currentPixmap()
|
|
92
|
+
if frame.isNull() or self.size().isEmpty():
|
|
93
|
+
return
|
|
94
|
+
self.setPixmap(frame.scaled(self.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
|
|
95
|
+
|
|
96
|
+
def resizeEvent(self, event) -> None:
|
|
97
|
+
super().resizeEvent(event)
|
|
98
|
+
self._update_frame()
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
_MARKDOWN_CODE_RE = re.compile(r"`([^`\n]+)`")
|
|
102
|
+
_MARKDOWN_BOLD_RE = re.compile(r"\*\*([^*\n]+)\*\*|__([^_\n]+)__")
|
|
103
|
+
_MARKDOWN_ITALIC_RE = re.compile(r"(?<!\*)\*([^*\n]+)\*(?!\*)|(?<!\w)_([^_\n]+)_(?!\w)")
|
|
104
|
+
_MARKDOWN_UNORDERED_ITEM_RE = re.compile(r"^\s*[-+*]\s+(.+)$")
|
|
105
|
+
_MARKDOWN_ORDERED_ITEM_RE = re.compile(r"^\s*\d+[.)]\s+(.+)$")
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _render_markdown(text: str) -> str:
|
|
109
|
+
"""Render a small safe Markdown subset for provider and user messages."""
|
|
110
|
+
normalized = str(text or "").replace("\r\n", "\n").replace("\r", "\n")
|
|
111
|
+
fenced_code: list[str] = []
|
|
112
|
+
def stash_fence(match: re.Match[str]) -> str:
|
|
113
|
+
fenced_code.append(match.group(1))
|
|
114
|
+
return f"\n@@EASYROB_FENCE_{len(fenced_code) - 1}@@\n"
|
|
115
|
+
normalized = re.sub(r"(?m)^```[^\n]*\n([\s\S]*?)(?:^```[ \t]*$|\Z)", stash_fence, normalized)
|
|
116
|
+
normalized = re.sub(r"(?m)^[ \t]*-[ \t]*\\[ \t]*(?:\n|$)", "", normalized)
|
|
117
|
+
normalized = re.sub(r"\\[ \t]*\n", "\n", normalized)
|
|
118
|
+
normalized = re.sub(r"\\(?=[*_-])", "", normalized)
|
|
119
|
+
normalized = re.sub(r"&#(?:x0*20|32);", " ", normalized, flags=re.IGNORECASE)
|
|
120
|
+
safe_lines = [escape(line, quote=False) for line in normalized.split("\n")]
|
|
121
|
+
|
|
122
|
+
code_fragments: list[str] = []
|
|
123
|
+
|
|
124
|
+
def stash_code(match: re.Match[str]) -> str:
|
|
125
|
+
token = f"@@EASYROB_CODE_{len(code_fragments)}@@"
|
|
126
|
+
code_fragments.append(match.group(1))
|
|
127
|
+
return token
|
|
128
|
+
|
|
129
|
+
def render_inline(value: str) -> str:
|
|
130
|
+
rendered = _MARKDOWN_CODE_RE.sub(stash_code, value)
|
|
131
|
+
rendered = _MARKDOWN_BOLD_RE.sub(
|
|
132
|
+
lambda match: f"<strong>{match.group(1) or match.group(2)}</strong>",
|
|
133
|
+
rendered,
|
|
134
|
+
)
|
|
135
|
+
rendered = _MARKDOWN_ITALIC_RE.sub(
|
|
136
|
+
lambda match: f"<em>{match.group(1) or match.group(2)}</em>",
|
|
137
|
+
rendered,
|
|
138
|
+
)
|
|
139
|
+
def render_link(match: re.Match[str]) -> str:
|
|
140
|
+
from html import unescape
|
|
141
|
+
citation = SourceCitation.create(unescape(match.group(1)), unescape(match.group(2)))
|
|
142
|
+
if citation is None:
|
|
143
|
+
return match.group(1)
|
|
144
|
+
return f'<a href="{escape(citation.url, quote=True)}">{match.group(1)}</a>'
|
|
145
|
+
rendered = re.sub(r"【(https?://[^\s】]+)】", r" [Source](\1)", rendered)
|
|
146
|
+
rendered = re.sub(r"\[([^\]\n]+)\]\(([^\s)]+)\)", render_link, rendered)
|
|
147
|
+
for index, fragment in enumerate(code_fragments):
|
|
148
|
+
rendered = rendered.replace(
|
|
149
|
+
f"@@EASYROB_CODE_{index}@@",
|
|
150
|
+
f"<code>{fragment}</code>",
|
|
151
|
+
)
|
|
152
|
+
return rendered
|
|
153
|
+
|
|
154
|
+
rendered_lines: list[str] = []
|
|
155
|
+
open_list: str | None = None
|
|
156
|
+
paragraph: list[str] = []
|
|
157
|
+
def flush_paragraph() -> None:
|
|
158
|
+
if paragraph:
|
|
159
|
+
rendered_lines.append('<p style="margin: 0 0 8px 0;">' + "<br>".join(paragraph) + "</p>")
|
|
160
|
+
paragraph.clear()
|
|
161
|
+
for line in safe_lines:
|
|
162
|
+
fence = re.fullmatch(r"@@EASYROB_FENCE_(\d+)@@", line)
|
|
163
|
+
if fence and int(fence.group(1)) < len(fenced_code):
|
|
164
|
+
flush_paragraph()
|
|
165
|
+
if open_list:
|
|
166
|
+
rendered_lines.append(f"</{open_list}>")
|
|
167
|
+
open_list = None
|
|
168
|
+
rendered_lines.append("<pre>" + escape(fenced_code[int(fence.group(1))], quote=False).rstrip("\n") + "</pre>")
|
|
169
|
+
continue
|
|
170
|
+
unordered = _MARKDOWN_UNORDERED_ITEM_RE.match(line)
|
|
171
|
+
ordered = _MARKDOWN_ORDERED_ITEM_RE.match(line)
|
|
172
|
+
if unordered or ordered:
|
|
173
|
+
flush_paragraph()
|
|
174
|
+
list_name = "ul" if unordered else "ol"
|
|
175
|
+
item_text = (unordered or ordered).group(1)
|
|
176
|
+
if open_list != list_name:
|
|
177
|
+
if open_list:
|
|
178
|
+
rendered_lines.append(f"</{open_list}>")
|
|
179
|
+
start_number = int(re.match(r"\s*(\d+)", line).group(1)) if ordered else 1
|
|
180
|
+
start_attribute = f' start="{start_number}"' if ordered and start_number != 1 else ""
|
|
181
|
+
rendered_lines.append(f"<{list_name}{start_attribute}>")
|
|
182
|
+
open_list = list_name
|
|
183
|
+
rendered_lines.append(f"<li>{render_inline(item_text)}</li>")
|
|
184
|
+
continue
|
|
185
|
+
if not line.strip():
|
|
186
|
+
flush_paragraph()
|
|
187
|
+
continue
|
|
188
|
+
if open_list:
|
|
189
|
+
rendered_lines.append(f"</{open_list}>")
|
|
190
|
+
open_list = None
|
|
191
|
+
heading = re.match(r"^#{1,6}\s+(.+)$", line)
|
|
192
|
+
if heading:
|
|
193
|
+
flush_paragraph()
|
|
194
|
+
rendered_lines.append('<p style="margin: 8px 0 4px 0;"><strong>' + render_inline(heading.group(1)) + "</strong></p>")
|
|
195
|
+
elif line:
|
|
196
|
+
paragraph.append(render_inline(line))
|
|
197
|
+
else:
|
|
198
|
+
flush_paragraph()
|
|
199
|
+
|
|
200
|
+
if open_list:
|
|
201
|
+
rendered_lines.append(f"</{open_list}>")
|
|
202
|
+
flush_paragraph()
|
|
203
|
+
return "".join(rendered_lines)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class BotPanel(QFrame):
|
|
207
|
+
send_requested = Signal(str, str)
|
|
208
|
+
workflow_summary_requested = Signal()
|
|
209
|
+
close_requested = Signal()
|
|
210
|
+
clear_requested = Signal()
|
|
211
|
+
api_save_requested = Signal()
|
|
212
|
+
api_delete_requested = Signal()
|
|
213
|
+
|
|
214
|
+
def __init__(self, parent=None) -> None:
|
|
215
|
+
super().__init__(parent)
|
|
216
|
+
self._applying_styles = False
|
|
217
|
+
self._busy = False
|
|
218
|
+
self._workflow_summary_available = False
|
|
219
|
+
self._selected_tutorial_id = ""
|
|
220
|
+
self._messages: list[str] = []
|
|
221
|
+
self._message_records: list[tuple[str, str]] = []
|
|
222
|
+
self._theme_colors: dict[str, str] = {}
|
|
223
|
+
self._session_usage = TokenUsage()
|
|
224
|
+
self.source_links: list[QLabel] = []
|
|
225
|
+
|
|
226
|
+
self.setObjectName("easyrobBotPanel")
|
|
227
|
+
self.setFrameShape(QFrame.NoFrame)
|
|
228
|
+
self.setMinimumWidth(520)
|
|
229
|
+
# Let the panel consume the available width when the floating window is
|
|
230
|
+
# maximized; the minimum keeps the normal compact layout usable.
|
|
231
|
+
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
|
232
|
+
|
|
233
|
+
self._build_ui()
|
|
234
|
+
self._apply_styles()
|
|
235
|
+
self._update_mode_ui(self.mode_combo.currentText())
|
|
236
|
+
self._update_send_enabled()
|
|
237
|
+
|
|
238
|
+
def _build_ui(self) -> None:
|
|
239
|
+
root_layout = QVBoxLayout(self)
|
|
240
|
+
root_layout.setContentsMargins(18, 18, 18, 18)
|
|
241
|
+
root_layout.setSpacing(12)
|
|
242
|
+
|
|
243
|
+
self.chrome = QFrame()
|
|
244
|
+
self.chrome.setObjectName("botChrome")
|
|
245
|
+
chrome_layout = QVBoxLayout(self.chrome)
|
|
246
|
+
chrome_layout.setContentsMargins(0, 0, 0, 0)
|
|
247
|
+
chrome_layout.setSpacing(0)
|
|
248
|
+
root_layout.addWidget(self.chrome)
|
|
249
|
+
|
|
250
|
+
header = QFrame()
|
|
251
|
+
header.setObjectName("botHeader")
|
|
252
|
+
header_layout = QHBoxLayout(header)
|
|
253
|
+
header_layout.setContentsMargins(16, 16, 16, 12)
|
|
254
|
+
header_layout.setSpacing(10)
|
|
255
|
+
|
|
256
|
+
identity_layout = QVBoxLayout()
|
|
257
|
+
identity_layout.setContentsMargins(0, 0, 0, 0)
|
|
258
|
+
identity_layout.setSpacing(2)
|
|
259
|
+
self.title_label = QLabel("robBOT")
|
|
260
|
+
self.title_label.setObjectName("botTitle")
|
|
261
|
+
subtitle_label = QLabel("Ask robBOT about workflows, results, and AQME/ROBERT documentation.")
|
|
262
|
+
subtitle_label.setObjectName("botSubtitle")
|
|
263
|
+
self.subtitle_label = subtitle_label
|
|
264
|
+
identity_layout.addWidget(self.title_label)
|
|
265
|
+
identity_layout.addWidget(subtitle_label)
|
|
266
|
+
header_layout.addLayout(identity_layout, 1)
|
|
267
|
+
|
|
268
|
+
self.clear_button = QPushButton("Clear")
|
|
269
|
+
self.close_button = QPushButton("Close")
|
|
270
|
+
self.clear_button.clicked.connect(self.clear_requested.emit)
|
|
271
|
+
self.close_button.clicked.connect(self.close_requested.emit)
|
|
272
|
+
header_layout.addWidget(self.clear_button)
|
|
273
|
+
header_layout.addWidget(self.close_button)
|
|
274
|
+
|
|
275
|
+
chrome_layout.addWidget(header)
|
|
276
|
+
|
|
277
|
+
self.content_tabs = QTabWidget()
|
|
278
|
+
self.content_tabs.setObjectName("botContentTabs")
|
|
279
|
+
self.chat_tab = QWidget()
|
|
280
|
+
self.chat_tab.setObjectName("botChatTab")
|
|
281
|
+
self.settings_tab = QWidget()
|
|
282
|
+
self.settings_tab.setObjectName("botSettingsTab")
|
|
283
|
+
self.content_tabs.addTab(self.chat_tab, "Chat")
|
|
284
|
+
self.content_tabs.addTab(self.settings_tab, "Settings")
|
|
285
|
+
|
|
286
|
+
self.settings_scroll = QScrollArea()
|
|
287
|
+
self.settings_scroll.setWidgetResizable(True)
|
|
288
|
+
self.settings_scroll.setFrameShape(QFrame.NoFrame)
|
|
289
|
+
self.settings_scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
290
|
+
self.settings_scroll.setObjectName("botSettingsScroll")
|
|
291
|
+
|
|
292
|
+
self.settings_body = QWidget()
|
|
293
|
+
settings_body_layout = QVBoxLayout(self.settings_body)
|
|
294
|
+
settings_body_layout.setContentsMargins(0, 4, 0, 0)
|
|
295
|
+
settings_body_layout.setSpacing(10)
|
|
296
|
+
|
|
297
|
+
self.settings_help_button = self._help_button(
|
|
298
|
+
"Settings help",
|
|
299
|
+
"Use Settings to choose which robBOT mode answers your questions.\n\n"
|
|
300
|
+
"Cloud AI is the default and uses an external provider with your API key.\n\n"
|
|
301
|
+
"Heuristic gives stable rule-based help from the GUI state, popups, logs, and packaged docs.",
|
|
302
|
+
)
|
|
303
|
+
settings_header = QWidget()
|
|
304
|
+
settings_header_layout = QHBoxLayout(settings_header)
|
|
305
|
+
settings_header_layout.setContentsMargins(0, 0, 0, 0)
|
|
306
|
+
settings_header_layout.setSpacing(6)
|
|
307
|
+
settings_header_label = QLabel("robBOT settings")
|
|
308
|
+
settings_header_label.setObjectName("botSectionLabel")
|
|
309
|
+
settings_header_layout.addWidget(settings_header_label)
|
|
310
|
+
settings_header_layout.addWidget(self.settings_help_button)
|
|
311
|
+
settings_header_layout.addStretch(1)
|
|
312
|
+
settings_body_layout.addWidget(settings_header)
|
|
313
|
+
|
|
314
|
+
self.mode_combo = QComboBox()
|
|
315
|
+
self.mode_combo.addItems(["Cloud AI", "Heuristic"])
|
|
316
|
+
self.mode_combo.setCurrentText("Cloud AI")
|
|
317
|
+
self.mode_combo.currentTextChanged.connect(self._update_mode_ui)
|
|
318
|
+
self.mode_help_button = self._help_button(
|
|
319
|
+
"Mode help",
|
|
320
|
+
"Cloud AI is the default and uses an external provider with your API key.\n\n"
|
|
321
|
+
"Heuristic is deterministic and uses only GUI state, popups, logs, and packaged knowledge.",
|
|
322
|
+
)
|
|
323
|
+
settings_body_layout.addWidget(self._labeled_row("Mode", self.mode_combo, self.mode_help_button))
|
|
324
|
+
|
|
325
|
+
self.llm_controls = QWidget()
|
|
326
|
+
llm_layout = QVBoxLayout(self.llm_controls)
|
|
327
|
+
llm_layout.setContentsMargins(0, 0, 0, 0)
|
|
328
|
+
llm_layout.setSpacing(10)
|
|
329
|
+
|
|
330
|
+
self.provider_combo = QComboBox()
|
|
331
|
+
self.provider_combo.addItems(provider_names())
|
|
332
|
+
groq_index = self.provider_combo.findText("Groq")
|
|
333
|
+
if groq_index >= 0:
|
|
334
|
+
self.provider_combo.setCurrentIndex(groq_index)
|
|
335
|
+
self.provider_combo.currentTextChanged.connect(
|
|
336
|
+
lambda _text: self._update_mode_indicator(self.mode_combo.currentText())
|
|
337
|
+
)
|
|
338
|
+
self.provider_help_button = self._help_button(
|
|
339
|
+
"Provider help",
|
|
340
|
+
"Groq is a fast cloud option for trying robBOT with an API key.\n\n"
|
|
341
|
+
"Other providers use their own accounts and billing rules.",
|
|
342
|
+
)
|
|
343
|
+
llm_layout.addWidget(self._labeled_row("Provider", self.provider_combo, self.provider_help_button))
|
|
344
|
+
|
|
345
|
+
self.web_search_checkbox = QCheckBox(
|
|
346
|
+
"Allow automatic web search when information is missing or current"
|
|
347
|
+
)
|
|
348
|
+
self.web_search_checkbox.setChecked(True)
|
|
349
|
+
self.web_search_checkbox.setToolTip(
|
|
350
|
+
"Supported cloud providers may perform at most one billed search per question."
|
|
351
|
+
)
|
|
352
|
+
llm_layout.addWidget(self.web_search_checkbox)
|
|
353
|
+
|
|
354
|
+
self.api_key_edit = QLineEdit()
|
|
355
|
+
self.api_key_edit.setEchoMode(QLineEdit.Password)
|
|
356
|
+
self.api_key_edit.setPlaceholderText("Enter API key for the selected provider")
|
|
357
|
+
self.api_key_help_button = self._help_button(
|
|
358
|
+
"API key help",
|
|
359
|
+
"API key means the secret token from the selected provider account.\n\n"
|
|
360
|
+
"easyROB uses it only to send this robBOT question to that provider.",
|
|
361
|
+
)
|
|
362
|
+
llm_layout.addWidget(self._labeled_row("API key", self.api_key_edit, self.api_key_help_button))
|
|
363
|
+
|
|
364
|
+
api_actions = QWidget()
|
|
365
|
+
api_actions_layout = QHBoxLayout(api_actions)
|
|
366
|
+
api_actions_layout.setContentsMargins(0, 0, 0, 0)
|
|
367
|
+
api_actions_layout.setSpacing(8)
|
|
368
|
+
self.save_api_button = QPushButton("Save API")
|
|
369
|
+
self.delete_api_button = QPushButton("Delete API")
|
|
370
|
+
self.save_api_button.clicked.connect(self.api_save_requested.emit)
|
|
371
|
+
self.delete_api_button.clicked.connect(self.api_delete_requested.emit)
|
|
372
|
+
api_actions_layout.addWidget(self.save_api_button)
|
|
373
|
+
api_actions_layout.addWidget(self.delete_api_button)
|
|
374
|
+
api_actions_layout.addStretch(1)
|
|
375
|
+
llm_layout.addWidget(api_actions)
|
|
376
|
+
|
|
377
|
+
self.api_setup_guide = QTextBrowser()
|
|
378
|
+
self.api_setup_guide.setObjectName("botApiGuide")
|
|
379
|
+
self.api_setup_guide.setOpenExternalLinks(True)
|
|
380
|
+
self.api_setup_guide.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
|
381
|
+
self.api_setup_guide.setMaximumHeight(132)
|
|
382
|
+
self.api_setup_guide.setHtml(
|
|
383
|
+
"<b>Groq quick setup</b><br>"
|
|
384
|
+
"1. Open <a href='https://console.groq.com/keys'>console.groq.com/keys</a>.<br>"
|
|
385
|
+
"2. Sign in or create a Groq account.<br>"
|
|
386
|
+
"3. Create an API key in <b>API Keys</b>.<br>"
|
|
387
|
+
"4. Paste it here, choose <b>Groq</b>, and ask again."
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
api_setup_media = QWidget()
|
|
391
|
+
self.api_setup_media = api_setup_media
|
|
392
|
+
api_setup_media_layout = QVBoxLayout(api_setup_media)
|
|
393
|
+
api_setup_media_layout.setContentsMargins(0, 0, 0, 0)
|
|
394
|
+
api_setup_media_layout.setSpacing(10)
|
|
395
|
+
self.api_setup_gif_label = _AnimatedGifLabel()
|
|
396
|
+
self.api_setup_gif_label.setObjectName("botApiSetupGif")
|
|
397
|
+
self.api_setup_gif_label.setMinimumHeight(180)
|
|
398
|
+
self.api_setup_gif_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
|
399
|
+
self.api_setup_movie = QMovie(str(API_SETUP_GIF_PATH))
|
|
400
|
+
if self.api_setup_movie.isValid():
|
|
401
|
+
self.api_setup_gif_label.set_movie(self.api_setup_movie)
|
|
402
|
+
else:
|
|
403
|
+
self.api_setup_gif_label.setText("Groq setup animation unavailable.\nUse the written steps.")
|
|
404
|
+
self.api_setup_gif_label.setWordWrap(True)
|
|
405
|
+
api_setup_media_layout.addWidget(self.api_setup_guide)
|
|
406
|
+
api_setup_media_layout.addWidget(self.api_setup_gif_label)
|
|
407
|
+
self.api_setup_open_button = QPushButton("Open visual guide")
|
|
408
|
+
self.api_setup_open_button.clicked.connect(self._show_api_setup_dialog)
|
|
409
|
+
api_setup_media_layout.addWidget(self.api_setup_open_button, alignment=Qt.AlignCenter)
|
|
410
|
+
llm_layout.addWidget(api_setup_media)
|
|
411
|
+
|
|
412
|
+
settings_body_layout.addWidget(self.llm_controls)
|
|
413
|
+
settings_body_layout.addStretch(1)
|
|
414
|
+
|
|
415
|
+
self.settings_scroll.setWidget(self.settings_body)
|
|
416
|
+
settings_tab_layout = QVBoxLayout(self.settings_tab)
|
|
417
|
+
settings_tab_layout.setContentsMargins(12, 12, 12, 12)
|
|
418
|
+
settings_tab_layout.setSpacing(10)
|
|
419
|
+
settings_tab_layout.addWidget(self.settings_scroll)
|
|
420
|
+
|
|
421
|
+
conversation_shell = QFrame()
|
|
422
|
+
conversation_shell.setObjectName("botConversationShell")
|
|
423
|
+
conversation_layout = QVBoxLayout(conversation_shell)
|
|
424
|
+
conversation_layout.setContentsMargins(12, 12, 12, 12)
|
|
425
|
+
conversation_layout.setSpacing(10)
|
|
426
|
+
|
|
427
|
+
conversation_label = QLabel("Conversation")
|
|
428
|
+
conversation_label.setObjectName("botSectionLabel")
|
|
429
|
+
conversation_layout.addWidget(conversation_label)
|
|
430
|
+
|
|
431
|
+
self.history_view = QTextBrowser()
|
|
432
|
+
self.history_view.setOpenExternalLinks(True)
|
|
433
|
+
self.history_view.setReadOnly(True)
|
|
434
|
+
self.history_view.setPlaceholderText("The bot conversation will appear here.")
|
|
435
|
+
self.history_view.setObjectName("botHistoryView")
|
|
436
|
+
self.history_view.setMinimumHeight(320)
|
|
437
|
+
conversation_layout.addWidget(self.history_view, stretch=1)
|
|
438
|
+
|
|
439
|
+
workflow_summary_shell = QFrame()
|
|
440
|
+
workflow_summary_shell.setObjectName("botComposerShell")
|
|
441
|
+
workflow_summary_layout = QHBoxLayout(workflow_summary_shell)
|
|
442
|
+
workflow_summary_layout.setContentsMargins(10, 8, 10, 8)
|
|
443
|
+
workflow_summary_layout.setSpacing(10)
|
|
444
|
+
self.workflow_summary_status = QLabel("No workflow results detected")
|
|
445
|
+
self.workflow_summary_status.setWordWrap(True)
|
|
446
|
+
self.workflow_summary_button = QPushButton("Summarize workflow")
|
|
447
|
+
self.workflow_summary_button.setEnabled(False)
|
|
448
|
+
self.workflow_summary_button.setToolTip(
|
|
449
|
+
"This action becomes available when easyROB detects an available workflow result."
|
|
450
|
+
)
|
|
451
|
+
self.workflow_summary_button.clicked.connect(self.workflow_summary_requested.emit)
|
|
452
|
+
workflow_summary_layout.addWidget(self.workflow_summary_status, stretch=1)
|
|
453
|
+
workflow_summary_layout.addWidget(self.workflow_summary_button)
|
|
454
|
+
conversation_layout.addWidget(workflow_summary_shell)
|
|
455
|
+
|
|
456
|
+
composer_shell = QFrame()
|
|
457
|
+
composer_shell.setObjectName("botComposerShell")
|
|
458
|
+
composer_layout = QHBoxLayout(composer_shell)
|
|
459
|
+
composer_layout.setContentsMargins(0, 0, 0, 0)
|
|
460
|
+
composer_layout.setSpacing(10)
|
|
461
|
+
self.input_edit = QLineEdit()
|
|
462
|
+
self.input_edit.setPlaceholderText("Ask about your workflow, results, or AQME/ROBERT")
|
|
463
|
+
self.input_edit.textChanged.connect(self._update_send_enabled)
|
|
464
|
+
self.input_edit.textEdited.connect(self._clear_selected_tutorial)
|
|
465
|
+
self.input_edit.returnPressed.connect(self._emit_send_requested)
|
|
466
|
+
self.send_button = QPushButton("Send")
|
|
467
|
+
self.send_button.clicked.connect(self._emit_send_requested)
|
|
468
|
+
composer_layout.addWidget(self.input_edit, stretch=1)
|
|
469
|
+
composer_layout.addWidget(self.send_button)
|
|
470
|
+
conversation_layout.addWidget(composer_shell)
|
|
471
|
+
|
|
472
|
+
self.prompt_usage_label = QLabel()
|
|
473
|
+
self.prompt_usage_label.setObjectName("botPromptUsageLabel")
|
|
474
|
+
conversation_layout.addWidget(self.prompt_usage_label)
|
|
475
|
+
self.request_stage_label = QLabel("")
|
|
476
|
+
self.request_stage_label.setObjectName("botPromptUsageLabel")
|
|
477
|
+
conversation_layout.addWidget(self.request_stage_label)
|
|
478
|
+
|
|
479
|
+
self.advanced_options_toggle = QToolButton(self.chat_tab)
|
|
480
|
+
self.advanced_options_toggle.setObjectName("botAdvancedOptionsToggle")
|
|
481
|
+
self.advanced_options_toggle.setText("Advanced options")
|
|
482
|
+
self.advanced_options_toggle.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
|
|
483
|
+
self.advanced_options_toggle.setArrowType(Qt.DownArrow)
|
|
484
|
+
self.advanced_options_toggle.setCheckable(True)
|
|
485
|
+
self.advanced_options_toggle.setChecked(True)
|
|
486
|
+
self.advanced_options_toggle.setCursor(Qt.PointingHandCursor)
|
|
487
|
+
conversation_layout.addWidget(self.advanced_options_toggle)
|
|
488
|
+
|
|
489
|
+
self.advanced_options_body = QWidget(self.chat_tab)
|
|
490
|
+
self.advanced_options_body.setObjectName("botAdvancedOptionsBody")
|
|
491
|
+
advanced_options_layout = QVBoxLayout(self.advanced_options_body)
|
|
492
|
+
advanced_options_layout.setContentsMargins(0, 0, 0, 0)
|
|
493
|
+
advanced_options_layout.setSpacing(10)
|
|
494
|
+
|
|
495
|
+
self.usage_shell = QFrame(self.advanced_options_body)
|
|
496
|
+
self.usage_shell.setObjectName("botUsageShell")
|
|
497
|
+
usage_layout = QVBoxLayout(self.usage_shell)
|
|
498
|
+
usage_layout.setContentsMargins(10, 8, 10, 8)
|
|
499
|
+
usage_layout.setSpacing(4)
|
|
500
|
+
usage_header = QHBoxLayout()
|
|
501
|
+
usage_header.setContentsMargins(0, 0, 0, 0)
|
|
502
|
+
usage_title = QLabel("Token usage")
|
|
503
|
+
usage_title.setObjectName("botSectionLabel")
|
|
504
|
+
self.reset_usage_button = QPushButton("Reset usage")
|
|
505
|
+
self.reset_usage_button.clicked.connect(self.reset_usage)
|
|
506
|
+
usage_header.addWidget(usage_title)
|
|
507
|
+
usage_header.addStretch(1)
|
|
508
|
+
usage_header.addWidget(self.reset_usage_button)
|
|
509
|
+
self.last_usage_label = QLabel("Last request: no usage yet")
|
|
510
|
+
self.session_usage_label = QLabel("Session: 0 tokens · $0.0000")
|
|
511
|
+
self.last_usage_label.setWordWrap(True)
|
|
512
|
+
self.session_usage_label.setWordWrap(True)
|
|
513
|
+
self.session_usage_warning = QLabel("")
|
|
514
|
+
self.session_usage_warning.setObjectName("botUsageWarning")
|
|
515
|
+
self.session_usage_warning.setWordWrap(True)
|
|
516
|
+
self.session_usage_warning.hide()
|
|
517
|
+
usage_layout.addLayout(usage_header)
|
|
518
|
+
usage_layout.addWidget(self.last_usage_label)
|
|
519
|
+
usage_layout.addWidget(self.session_usage_label)
|
|
520
|
+
usage_layout.addWidget(self.session_usage_warning)
|
|
521
|
+
self.sources_title_label = QLabel("Sources")
|
|
522
|
+
self.sources_title_label.setObjectName("botSectionLabel")
|
|
523
|
+
self.sources_title_label.hide()
|
|
524
|
+
self.sources_layout = QVBoxLayout()
|
|
525
|
+
self.sources_layout.setContentsMargins(0, 0, 0, 0)
|
|
526
|
+
self.sources_layout.setSpacing(2)
|
|
527
|
+
self.sources_scroll = QScrollArea()
|
|
528
|
+
self.sources_scroll.setWidgetResizable(True)
|
|
529
|
+
self.sources_scroll.setFrameShape(QFrame.NoFrame)
|
|
530
|
+
self.sources_scroll.setMaximumHeight(120)
|
|
531
|
+
source_container = QWidget()
|
|
532
|
+
source_container.setLayout(self.sources_layout)
|
|
533
|
+
self.sources_scroll.setWidget(source_container)
|
|
534
|
+
self.sources_scroll.hide()
|
|
535
|
+
usage_layout.addWidget(self.sources_title_label)
|
|
536
|
+
usage_layout.addWidget(self.sources_scroll)
|
|
537
|
+
advanced_options_layout.addWidget(self.usage_shell)
|
|
538
|
+
|
|
539
|
+
self.suggested_label = QLabel("Suggested questions", self.advanced_options_body)
|
|
540
|
+
self.suggested_label.setObjectName("botSectionLabel")
|
|
541
|
+
advanced_options_layout.addWidget(self.suggested_label)
|
|
542
|
+
|
|
543
|
+
self.suggested_combo = QComboBox(self.advanced_options_body)
|
|
544
|
+
self.suggested_combo.setObjectName("botSuggestedQuestionCombo")
|
|
545
|
+
self.suggested_combo.addItem("Choose a suggested question...")
|
|
546
|
+
for question, tutorial_id in zip(SUGGESTED_QUESTIONS, SUGGESTED_TUTORIAL_IDS):
|
|
547
|
+
self.suggested_combo.addItem(question, tutorial_id)
|
|
548
|
+
self.suggested_combo.currentIndexChanged.connect(self._apply_suggested_question_from_index)
|
|
549
|
+
advanced_options_layout.addWidget(self.suggested_combo)
|
|
550
|
+
conversation_layout.addWidget(self.advanced_options_body)
|
|
551
|
+
self.advanced_options_toggle.toggled.connect(self._set_advanced_options_visible)
|
|
552
|
+
self._set_advanced_options_visible(True)
|
|
553
|
+
|
|
554
|
+
chat_tab_layout = QVBoxLayout(self.chat_tab)
|
|
555
|
+
chat_tab_layout.setContentsMargins(12, 12, 12, 12)
|
|
556
|
+
chat_tab_layout.setSpacing(10)
|
|
557
|
+
chat_tab_layout.addWidget(conversation_shell, stretch=1)
|
|
558
|
+
|
|
559
|
+
chrome_layout.addWidget(self.content_tabs, stretch=1)
|
|
560
|
+
|
|
561
|
+
def _set_advanced_options_visible(self, expanded: bool) -> None:
|
|
562
|
+
"""Show or hide the optional questions and token accounting controls."""
|
|
563
|
+
expanded = bool(expanded)
|
|
564
|
+
self.advanced_options_body.setVisible(expanded)
|
|
565
|
+
self.advanced_options_toggle.setArrowType(Qt.DownArrow if expanded else Qt.RightArrow)
|
|
566
|
+
|
|
567
|
+
def _resize_api_setup_movie(self) -> None:
|
|
568
|
+
if not hasattr(self, "api_setup_movie") or not self.api_setup_movie.isValid():
|
|
569
|
+
return
|
|
570
|
+
available_width = self.api_setup_media.width()
|
|
571
|
+
if available_width <= 0:
|
|
572
|
+
return
|
|
573
|
+
width = min(600, available_width)
|
|
574
|
+
height = round(width * 9 / 16)
|
|
575
|
+
if self.api_setup_gif_label.height() != height:
|
|
576
|
+
self.api_setup_gif_label.setFixedHeight(height)
|
|
577
|
+
self.api_setup_gif_label.update()
|
|
578
|
+
|
|
579
|
+
def _show_api_setup_dialog(self) -> None:
|
|
580
|
+
if not self.api_setup_movie.isValid():
|
|
581
|
+
return
|
|
582
|
+
dialog = QDialog(self)
|
|
583
|
+
dialog.setWindowTitle("Groq quick setup")
|
|
584
|
+
dialog.resize(820, 520)
|
|
585
|
+
dialog_layout = QVBoxLayout(dialog)
|
|
586
|
+
dialog_layout.setContentsMargins(16, 16, 16, 16)
|
|
587
|
+
dialog_layout.setSpacing(10)
|
|
588
|
+
|
|
589
|
+
gif_label = _AnimatedGifLabel()
|
|
590
|
+
gif_label.setMinimumSize(720, 405)
|
|
591
|
+
gif_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
|
592
|
+
movie = QMovie(str(API_SETUP_GIF_PATH))
|
|
593
|
+
gif_label.set_movie(movie)
|
|
594
|
+
dialog_layout.addWidget(gif_label, stretch=1)
|
|
595
|
+
|
|
596
|
+
close_button = QPushButton("Close")
|
|
597
|
+
close_button.clicked.connect(dialog.accept)
|
|
598
|
+
dialog_layout.addWidget(close_button, alignment=Qt.AlignRight)
|
|
599
|
+
dialog._api_setup_movie = movie
|
|
600
|
+
movie.start()
|
|
601
|
+
dialog.exec()
|
|
602
|
+
|
|
603
|
+
def _apply_styles(self) -> None:
|
|
604
|
+
if self._applying_styles:
|
|
605
|
+
return
|
|
606
|
+
self._applying_styles = True
|
|
607
|
+
palette = self.palette()
|
|
608
|
+
is_dark = palette.color(QPalette.Window).lightness() < 128
|
|
609
|
+
|
|
610
|
+
def css(color: QColor) -> str:
|
|
611
|
+
return color.name(QColor.HexRgb)
|
|
612
|
+
|
|
613
|
+
window = palette.color(QPalette.Window)
|
|
614
|
+
base = palette.color(QPalette.Base)
|
|
615
|
+
text = palette.color(QPalette.Text)
|
|
616
|
+
button = palette.color(QPalette.Button)
|
|
617
|
+
button_text = palette.color(QPalette.ButtonText)
|
|
618
|
+
mid = palette.color(QPalette.Mid)
|
|
619
|
+
light = palette.color(QPalette.Light)
|
|
620
|
+
highlighted_text = palette.color(QPalette.HighlightedText)
|
|
621
|
+
robert_purple = QColor("#6A0DAD")
|
|
622
|
+
robert_purple_hover = QColor("#7B2CBF")
|
|
623
|
+
|
|
624
|
+
chrome_bg_start = base.lighter(108) if is_dark else base.lighter(102)
|
|
625
|
+
chrome_bg_end = window.lighter(112) if is_dark else window.darker(102)
|
|
626
|
+
header_start = robert_purple_hover
|
|
627
|
+
header_end = robert_purple
|
|
628
|
+
subtitle = highlighted_text if is_dark else light.lighter(105)
|
|
629
|
+
section = text.lighter(125) if is_dark else text.darker(115)
|
|
630
|
+
conversation_bg = base.lighter(112) if is_dark else base.lighter(102)
|
|
631
|
+
composer_bg = base.lighter(106) if is_dark else base.lighter(100)
|
|
632
|
+
suggestion_bg = button.lighter(112) if is_dark else button.lighter(104)
|
|
633
|
+
suggestion_hover = suggestion_bg.darker(108) if is_dark else suggestion_bg.darker(104)
|
|
634
|
+
disabled_text = mid.lighter(120) if is_dark else mid.darker(110)
|
|
635
|
+
user_bubble = conversation_bg.lighter(104) if is_dark else conversation_bg
|
|
636
|
+
user_border = mid.lighter(115) if is_dark else QColor("#DEE1E6")
|
|
637
|
+
user_role = text.lighter(145) if is_dark else text.darker(120)
|
|
638
|
+
bot_bubble = base.lighter(120) if is_dark else QColor("#F7F2FC")
|
|
639
|
+
bot_border = robert_purple.lighter(120) if is_dark else QColor("#DACBE8")
|
|
640
|
+
bot_role = robert_purple.lighter(155) if is_dark else robert_purple
|
|
641
|
+
|
|
642
|
+
self._theme_colors = {
|
|
643
|
+
"user_bubble": css(user_bubble),
|
|
644
|
+
"user_text": css(text),
|
|
645
|
+
"user_border": css(user_border),
|
|
646
|
+
"user_role": css(user_role),
|
|
647
|
+
"bot_bubble": css(bot_bubble),
|
|
648
|
+
"bot_text": css(text),
|
|
649
|
+
"bot_border": css(bot_border),
|
|
650
|
+
"bot_role": css(bot_role),
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
try:
|
|
654
|
+
self.setStyleSheet(
|
|
655
|
+
f"""
|
|
656
|
+
#easyrobBotPanel {{
|
|
657
|
+
background: {css(window)};
|
|
658
|
+
}}
|
|
659
|
+
#botChrome {{
|
|
660
|
+
background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
|
|
661
|
+
stop:0 {css(chrome_bg_start)}, stop:1 {css(chrome_bg_end)});
|
|
662
|
+
border: 1px solid {css(mid)};
|
|
663
|
+
border-radius: 20px;
|
|
664
|
+
}}
|
|
665
|
+
#botHeader {{
|
|
666
|
+
background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
|
|
667
|
+
stop:0 {css(header_start)}, stop:1 {css(header_end)});
|
|
668
|
+
border-top-left-radius: 20px;
|
|
669
|
+
border-top-right-radius: 20px;
|
|
670
|
+
}}
|
|
671
|
+
#botTitle {{
|
|
672
|
+
color: {css(highlighted_text)};
|
|
673
|
+
font-size: 18px;
|
|
674
|
+
font-weight: 700;
|
|
675
|
+
}}
|
|
676
|
+
#botSubtitle {{
|
|
677
|
+
color: {css(subtitle)};
|
|
678
|
+
font-size: 11px;
|
|
679
|
+
}}
|
|
680
|
+
#botSettingsTab, #botChatTab, #botConversationShell {{
|
|
681
|
+
background: transparent;
|
|
682
|
+
}}
|
|
683
|
+
#botContentTabs::pane {{
|
|
684
|
+
border: none;
|
|
685
|
+
background: transparent;
|
|
686
|
+
}}
|
|
687
|
+
#botContentTabs QTabBar::tab {{
|
|
688
|
+
background: {css(button)};
|
|
689
|
+
color: {css(button_text)};
|
|
690
|
+
border: 1px solid {css(mid)};
|
|
691
|
+
border-bottom: none;
|
|
692
|
+
border-top-left-radius: 8px;
|
|
693
|
+
border-top-right-radius: 8px;
|
|
694
|
+
padding: 8px 18px;
|
|
695
|
+
margin-right: 4px;
|
|
696
|
+
}}
|
|
697
|
+
#botContentTabs QTabBar::tab:selected {{
|
|
698
|
+
background: {css(base)};
|
|
699
|
+
color: {css(text)};
|
|
700
|
+
}}
|
|
701
|
+
#botSectionLabel {{
|
|
702
|
+
color: {css(section)};
|
|
703
|
+
font-size: 11px;
|
|
704
|
+
font-weight: 700;
|
|
705
|
+
letter-spacing: 0.08em;
|
|
706
|
+
text-transform: uppercase;
|
|
707
|
+
}}
|
|
708
|
+
#botHistoryView {{
|
|
709
|
+
background: {css(conversation_bg)};
|
|
710
|
+
color: {css(text)};
|
|
711
|
+
border: 1px solid {css(mid)};
|
|
712
|
+
border-radius: 16px;
|
|
713
|
+
padding: 6px;
|
|
714
|
+
}}
|
|
715
|
+
#botUsageShell {{
|
|
716
|
+
background: {css(base.lighter(108) if is_dark else base.lighter(102))};
|
|
717
|
+
color: {css(text)};
|
|
718
|
+
border: 1px solid {css(mid)};
|
|
719
|
+
border-radius: 14px;
|
|
720
|
+
}}
|
|
721
|
+
#botPromptUsageLabel {{
|
|
722
|
+
color: {css(disabled_text)};
|
|
723
|
+
font-size: 11px;
|
|
724
|
+
}}
|
|
725
|
+
#botPromptUsageLabel[warning="true"] {{
|
|
726
|
+
color: #B7791F;
|
|
727
|
+
font-weight: 600;
|
|
728
|
+
}}
|
|
729
|
+
#botPromptUsageLabel[limit_exceeded="true"] {{
|
|
730
|
+
color: #C53030;
|
|
731
|
+
font-weight: 700;
|
|
732
|
+
}}
|
|
733
|
+
#botUsageWarning {{
|
|
734
|
+
color: #B7791F;
|
|
735
|
+
font-weight: 600;
|
|
736
|
+
}}
|
|
737
|
+
#botComposerShell {{
|
|
738
|
+
background: {css(composer_bg)};
|
|
739
|
+
border: 1px solid {css(mid)};
|
|
740
|
+
border-radius: 16px;
|
|
741
|
+
padding: 8px;
|
|
742
|
+
}}
|
|
743
|
+
#botApiGuide {{
|
|
744
|
+
background: {css(base.lighter(108) if is_dark else base.lighter(102))};
|
|
745
|
+
color: {css(text)};
|
|
746
|
+
border: 1px solid {css(mid)};
|
|
747
|
+
border-radius: 12px;
|
|
748
|
+
padding: 8px;
|
|
749
|
+
}}
|
|
750
|
+
QLineEdit, QComboBox {{
|
|
751
|
+
background: {css(base)};
|
|
752
|
+
border: 1px solid {css(mid)};
|
|
753
|
+
border-radius: 10px;
|
|
754
|
+
padding: 8px 10px;
|
|
755
|
+
color: {css(text)};
|
|
756
|
+
}}
|
|
757
|
+
QPushButton {{
|
|
758
|
+
background: {css(button)};
|
|
759
|
+
border: 1px solid {css(mid)};
|
|
760
|
+
border-radius: 10px;
|
|
761
|
+
padding: 8px 12px;
|
|
762
|
+
color: {css(button_text)};
|
|
763
|
+
}}
|
|
764
|
+
QPushButton:hover {{
|
|
765
|
+
background: {css(button.darker(108) if is_dark else button.darker(104))};
|
|
766
|
+
}}
|
|
767
|
+
QPushButton:disabled {{
|
|
768
|
+
color: {css(disabled_text)};
|
|
769
|
+
background: {css(button)};
|
|
770
|
+
}}
|
|
771
|
+
#botSuggestionButton {{
|
|
772
|
+
text-align: left;
|
|
773
|
+
background: {css(suggestion_bg)};
|
|
774
|
+
border: 1px solid {css(mid)};
|
|
775
|
+
border-radius: 12px;
|
|
776
|
+
padding: 10px 12px;
|
|
777
|
+
}}
|
|
778
|
+
#botSuggestionButton:hover {{
|
|
779
|
+
background: {css(suggestion_hover)};
|
|
780
|
+
}}
|
|
781
|
+
QToolButton {{
|
|
782
|
+
background: transparent;
|
|
783
|
+
border: none;
|
|
784
|
+
color: {css(text)};
|
|
785
|
+
font-weight: 700;
|
|
786
|
+
padding: 4px 0;
|
|
787
|
+
}}
|
|
788
|
+
#botHelpButton {{
|
|
789
|
+
border: 1px solid {css(mid)};
|
|
790
|
+
border-radius: 9px;
|
|
791
|
+
min-width: 18px;
|
|
792
|
+
min-height: 18px;
|
|
793
|
+
max-width: 18px;
|
|
794
|
+
max-height: 18px;
|
|
795
|
+
padding: 0;
|
|
796
|
+
font-weight: 700;
|
|
797
|
+
}}
|
|
798
|
+
"""
|
|
799
|
+
)
|
|
800
|
+
self._rerender_history()
|
|
801
|
+
finally:
|
|
802
|
+
self._applying_styles = False
|
|
803
|
+
|
|
804
|
+
def _help_button(self, title: str, message: str) -> QToolButton:
|
|
805
|
+
button = QToolButton()
|
|
806
|
+
button.setObjectName("botHelpButton")
|
|
807
|
+
button.setText("?")
|
|
808
|
+
button.setCursor(Qt.PointingHandCursor)
|
|
809
|
+
button.setProperty("help_title", title)
|
|
810
|
+
button.setProperty("help_message", message)
|
|
811
|
+
button.clicked.connect(lambda checked=False, b=button: self._show_help_dialog(b))
|
|
812
|
+
return button
|
|
813
|
+
|
|
814
|
+
def _show_help_dialog(self, button: QToolButton) -> None:
|
|
815
|
+
title = str(button.property("help_title") or "Assistant help")
|
|
816
|
+
message = str(button.property("help_message") or "")
|
|
817
|
+
QMessageBox.information(self, title, message)
|
|
818
|
+
|
|
819
|
+
def _labeled_row(self, label_text: str, widget: QWidget, help_button: QWidget | None = None) -> QWidget:
|
|
820
|
+
wrapper = QWidget()
|
|
821
|
+
layout = QVBoxLayout(wrapper)
|
|
822
|
+
layout.setContentsMargins(0, 0, 0, 0)
|
|
823
|
+
layout.setSpacing(4)
|
|
824
|
+
label_row = QHBoxLayout()
|
|
825
|
+
label_row.setContentsMargins(0, 0, 0, 0)
|
|
826
|
+
label_row.setSpacing(6)
|
|
827
|
+
label = QLabel(label_text)
|
|
828
|
+
label.setObjectName("botSectionLabel")
|
|
829
|
+
label_row.addWidget(label)
|
|
830
|
+
if help_button is not None:
|
|
831
|
+
label_row.addWidget(help_button)
|
|
832
|
+
label_row.addStretch(1)
|
|
833
|
+
layout.addLayout(label_row)
|
|
834
|
+
layout.addWidget(widget)
|
|
835
|
+
return wrapper
|
|
836
|
+
|
|
837
|
+
def _apply_suggested_question(self, question: str) -> None:
|
|
838
|
+
self.input_edit.setText(question)
|
|
839
|
+
self.input_edit.setFocus(Qt.OtherFocusReason)
|
|
840
|
+
self.input_edit.selectAll()
|
|
841
|
+
|
|
842
|
+
def _apply_suggested_question_from_index(self, index: int) -> None:
|
|
843
|
+
if index <= 0:
|
|
844
|
+
return
|
|
845
|
+
self._selected_tutorial_id = str(self.suggested_combo.itemData(index) or "")
|
|
846
|
+
self._apply_suggested_question(self.suggested_combo.itemText(index))
|
|
847
|
+
self.suggested_combo.blockSignals(True)
|
|
848
|
+
self.suggested_combo.setCurrentIndex(0)
|
|
849
|
+
self.suggested_combo.blockSignals(False)
|
|
850
|
+
|
|
851
|
+
def _clear_selected_tutorial(self, _text: str = "") -> None:
|
|
852
|
+
self._selected_tutorial_id = ""
|
|
853
|
+
|
|
854
|
+
def _update_mode_indicator(self, mode: str) -> None:
|
|
855
|
+
normalized = str(mode).strip().lower()
|
|
856
|
+
if normalized == "cloud ai":
|
|
857
|
+
provider_label = str(self.provider_combo.currentText()).strip()
|
|
858
|
+
label = provider_label or "Cloud AI"
|
|
859
|
+
elif normalized == "heuristic":
|
|
860
|
+
label = "Heuristic"
|
|
861
|
+
else:
|
|
862
|
+
label = "Heuristic"
|
|
863
|
+
self.title_label.setText(f"robBOT ({label})")
|
|
864
|
+
if self._message_records:
|
|
865
|
+
self._rerender_history()
|
|
866
|
+
|
|
867
|
+
def _display_role_label(self, role: str) -> str:
|
|
868
|
+
normalized_role = str(role).strip().lower()
|
|
869
|
+
if normalized_role == "user":
|
|
870
|
+
return str(role).strip() or "User"
|
|
871
|
+
normalized_mode = str(self.mode_combo.currentText()).strip().lower()
|
|
872
|
+
if normalized_mode == "cloud ai":
|
|
873
|
+
active_label = str(self.provider_combo.currentText()).strip() or "Cloud AI"
|
|
874
|
+
elif normalized_mode == "heuristic":
|
|
875
|
+
active_label = "Heuristic"
|
|
876
|
+
else:
|
|
877
|
+
active_label = "Heuristic"
|
|
878
|
+
return f"robBOT ({active_label})"
|
|
879
|
+
|
|
880
|
+
def _update_mode_ui(self, mode: str) -> None:
|
|
881
|
+
normalized = str(mode).strip().lower()
|
|
882
|
+
is_cloud = normalized == "cloud ai"
|
|
883
|
+
self._update_mode_indicator(mode)
|
|
884
|
+
self.llm_controls.setHidden(not is_cloud)
|
|
885
|
+
self.provider_combo.setEnabled(not self._busy and is_cloud)
|
|
886
|
+
self.web_search_checkbox.setEnabled(not self._busy and is_cloud)
|
|
887
|
+
self.api_key_edit.setEnabled(not self._busy and is_cloud)
|
|
888
|
+
self.save_api_button.setEnabled(not self._busy and is_cloud)
|
|
889
|
+
self.delete_api_button.setEnabled(not self._busy and is_cloud and bool(self.delete_api_button.property("has_saved_api")))
|
|
890
|
+
self.api_setup_guide.setHidden(not is_cloud)
|
|
891
|
+
self.api_setup_gif_label.setHidden(not is_cloud)
|
|
892
|
+
self.api_setup_open_button.setHidden(not is_cloud)
|
|
893
|
+
self.api_setup_open_button.setEnabled(not self._busy and self.api_setup_movie.isValid())
|
|
894
|
+
if is_cloud and self.api_setup_movie.isValid() and self.api_setup_gif_label.isVisible():
|
|
895
|
+
if self.api_setup_movie.state() != QMovie.Running:
|
|
896
|
+
self.api_setup_movie.start()
|
|
897
|
+
elif self.api_setup_movie.state() == QMovie.Running:
|
|
898
|
+
self.api_setup_movie.stop()
|
|
899
|
+
|
|
900
|
+
def _update_send_enabled(self) -> None:
|
|
901
|
+
has_text = bool(self.input_edit.text().strip())
|
|
902
|
+
token_count = estimate_token_count(self.input_edit.text())
|
|
903
|
+
warning = token_count >= int(MAX_USER_PROMPT_TOKENS * 0.75)
|
|
904
|
+
limit_exceeded = token_count > MAX_USER_PROMPT_TOKENS
|
|
905
|
+
self.prompt_usage_label.setText(
|
|
906
|
+
f"Estimated question size: {token_count:,} / {MAX_USER_PROMPT_TOKENS:,} tokens"
|
|
907
|
+
)
|
|
908
|
+
self.prompt_usage_label.setProperty("warning", warning)
|
|
909
|
+
self.prompt_usage_label.setProperty("limit_exceeded", limit_exceeded)
|
|
910
|
+
self.prompt_usage_label.style().unpolish(self.prompt_usage_label)
|
|
911
|
+
self.prompt_usage_label.style().polish(self.prompt_usage_label)
|
|
912
|
+
self.send_button.setEnabled(has_text and not self._busy and not limit_exceeded)
|
|
913
|
+
self.input_edit.setEnabled(not self._busy)
|
|
914
|
+
|
|
915
|
+
def _emit_send_requested(self) -> None:
|
|
916
|
+
if self._busy:
|
|
917
|
+
return
|
|
918
|
+
question = self.input_edit.text().strip()
|
|
919
|
+
if not question:
|
|
920
|
+
return
|
|
921
|
+
tutorial_id = self._selected_tutorial_id
|
|
922
|
+
self._selected_tutorial_id = ""
|
|
923
|
+
self.send_requested.emit(question, tutorial_id)
|
|
924
|
+
|
|
925
|
+
def _message_html(self, role: str, text: str) -> str:
|
|
926
|
+
is_user = role.strip().lower() == "user"
|
|
927
|
+
bubble_color = self._theme_colors["user_bubble"] if is_user else self._theme_colors["bot_bubble"]
|
|
928
|
+
text_color = self._theme_colors["user_text"] if is_user else self._theme_colors["bot_text"]
|
|
929
|
+
border_color = self._theme_colors["user_border"] if is_user else self._theme_colors["bot_border"]
|
|
930
|
+
role_color = self._theme_colors["user_role"] if is_user else self._theme_colors["bot_role"]
|
|
931
|
+
safe_role = escape(self._display_role_label(role))
|
|
932
|
+
safe_text = _render_markdown(text)
|
|
933
|
+
# QTextBrowser supports table cells and padding, but not CSS bubble layouts.
|
|
934
|
+
return (
|
|
935
|
+
f"<table width='100%' cellspacing='0' cellpadding='12' "
|
|
936
|
+
f"style='margin-top: 10px; margin-bottom: 12px; border: 1px solid {border_color};'>"
|
|
937
|
+
f"<tr><td bgcolor='{bubble_color}' style='color: {text_color};'>"
|
|
938
|
+
f"<p style='font-size: 11px; font-weight: 700; color: {role_color}; margin: 0 0 8px 0;'>{safe_role}</p>"
|
|
939
|
+
f"<div style='font-size: 14px;'>{safe_text}</div>"
|
|
940
|
+
f"</td></tr></table><p style='margin: 0; font-size: 4px;'> </p>"
|
|
941
|
+
)
|
|
942
|
+
|
|
943
|
+
def append_message(self, role: str, text: str) -> None:
|
|
944
|
+
self._message_records.append((role, text))
|
|
945
|
+
self._messages.append(self._message_html(role, text))
|
|
946
|
+
self.history_view.setHtml("".join(self._messages))
|
|
947
|
+
scrollbar = self.history_view.verticalScrollBar()
|
|
948
|
+
scrollbar.setValue(scrollbar.maximum())
|
|
949
|
+
|
|
950
|
+
def clear_history(self) -> None:
|
|
951
|
+
self._messages.clear()
|
|
952
|
+
self._message_records.clear()
|
|
953
|
+
self.history_view.clear()
|
|
954
|
+
|
|
955
|
+
@staticmethod
|
|
956
|
+
def _format_cost(cost: float | None) -> str:
|
|
957
|
+
if cost is None:
|
|
958
|
+
return "cost unavailable"
|
|
959
|
+
rounded = Decimal(str(cost)).quantize(Decimal("0.0001"), rounding=ROUND_HALF_UP)
|
|
960
|
+
return f"~${rounded:.4f}"
|
|
961
|
+
|
|
962
|
+
def record_usage(self, usage: TokenUsage) -> TokenUsage:
|
|
963
|
+
if not isinstance(usage, TokenUsage):
|
|
964
|
+
usage = TokenUsage(
|
|
965
|
+
prompt_tokens=max(0, int(getattr(usage, "prompt_tokens", 0) or 0)),
|
|
966
|
+
completion_tokens=max(0, int(getattr(usage, "completion_tokens", 0) or 0)),
|
|
967
|
+
total_tokens=max(0, int(getattr(usage, "total_tokens", 0) or 0)),
|
|
968
|
+
cached_prompt_tokens=max(0, int(getattr(usage, "cached_prompt_tokens", 0) or 0)),
|
|
969
|
+
assessment_prompt_tokens=max(0, int(getattr(usage, "assessment_prompt_tokens", 0) or 0)),
|
|
970
|
+
assessment_completion_tokens=max(0, int(getattr(usage, "assessment_completion_tokens", 0) or 0)),
|
|
971
|
+
search_content_tokens=max(0, int(getattr(usage, "search_content_tokens", 0) or 0)),
|
|
972
|
+
search_requests=max(0, int(getattr(usage, "search_requests", 0) or 0)),
|
|
973
|
+
estimated_model_cost_usd=getattr(usage, "estimated_model_cost_usd", None),
|
|
974
|
+
estimated_search_cost_usd=getattr(usage, "estimated_search_cost_usd", None),
|
|
975
|
+
estimated_cost_usd=getattr(usage, "estimated_cost_usd", None),
|
|
976
|
+
is_estimated=getattr(usage, "is_estimated", True) is True,
|
|
977
|
+
provider=str(getattr(usage, "provider", "") or ""),
|
|
978
|
+
model=str(getattr(usage, "model", "") or ""),
|
|
979
|
+
)
|
|
980
|
+
self._session_usage = self._session_usage + usage
|
|
981
|
+
estimate_prefix = "Estimated " if usage.is_estimated else ""
|
|
982
|
+
cached = (
|
|
983
|
+
f", {usage.cached_prompt_tokens:,} cached"
|
|
984
|
+
if usage.cached_prompt_tokens else ""
|
|
985
|
+
)
|
|
986
|
+
source = f" ({usage.provider})" if usage.provider else ""
|
|
987
|
+
self.last_usage_label.setText(
|
|
988
|
+
f"{estimate_prefix}last request{source}: {usage.total_tokens:,} tokens "
|
|
989
|
+
f"({usage.prompt_tokens:,} input, {usage.completion_tokens:,} output{cached}) · "
|
|
990
|
+
f"{self._format_cost(usage.estimated_cost_usd)}"
|
|
991
|
+
)
|
|
992
|
+
session = self._session_usage
|
|
993
|
+
session_search_label = (
|
|
994
|
+
f"{session.search_requests} search"
|
|
995
|
+
if session.search_requests == 1
|
|
996
|
+
else f"{session.search_requests} searches"
|
|
997
|
+
)
|
|
998
|
+
cost_breakdown = ""
|
|
999
|
+
if (
|
|
1000
|
+
session.estimated_model_cost_usd is not None
|
|
1001
|
+
and session.estimated_search_cost_usd is not None
|
|
1002
|
+
):
|
|
1003
|
+
cost_breakdown = (
|
|
1004
|
+
f" (model {self._format_cost(session.estimated_model_cost_usd)} + "
|
|
1005
|
+
f"search {self._format_cost(session.estimated_search_cost_usd)})"
|
|
1006
|
+
)
|
|
1007
|
+
self.session_usage_label.setText(
|
|
1008
|
+
f"Session: {session.total_tokens:,} tokens "
|
|
1009
|
+
f"({session.prompt_tokens:,} input, {session.completion_tokens:,} output) | "
|
|
1010
|
+
f"{session_search_label} | total {self._format_cost(session.estimated_cost_usd)}"
|
|
1011
|
+
f"{cost_breakdown}"
|
|
1012
|
+
)
|
|
1013
|
+
if session.estimated_cost_usd is not None and session.estimated_cost_usd >= _SESSION_COST_HIGH_USD:
|
|
1014
|
+
self.session_usage_warning.setText(
|
|
1015
|
+
f"High usage: estimated session cost has reached {self._format_cost(session.estimated_cost_usd)}. "
|
|
1016
|
+
"Check your provider dashboard and project limits."
|
|
1017
|
+
)
|
|
1018
|
+
self.session_usage_warning.show()
|
|
1019
|
+
elif session.estimated_cost_usd is not None and session.estimated_cost_usd >= _SESSION_COST_NOTICE_USD:
|
|
1020
|
+
self.session_usage_warning.setText(
|
|
1021
|
+
f"Usage notice: estimated session cost has reached {self._format_cost(session.estimated_cost_usd)}."
|
|
1022
|
+
)
|
|
1023
|
+
self.session_usage_warning.show()
|
|
1024
|
+
elif session.estimated_cost_usd is not None:
|
|
1025
|
+
self.session_usage_warning.hide()
|
|
1026
|
+
elif session.total_tokens >= 50_000:
|
|
1027
|
+
milestone = (session.total_tokens // 50_000) * 50_000
|
|
1028
|
+
self.session_usage_warning.setText(
|
|
1029
|
+
f"High usage: this session has passed {milestone:,} tokens and reliable cost is unavailable. "
|
|
1030
|
+
"Check your provider dashboard and project limits."
|
|
1031
|
+
)
|
|
1032
|
+
self.session_usage_warning.show()
|
|
1033
|
+
elif session.total_tokens >= 10_000:
|
|
1034
|
+
self.session_usage_warning.setText(
|
|
1035
|
+
"Usage notice: this session has passed 10,000 tokens, but reliable cost is unavailable. "
|
|
1036
|
+
"You can reset this counter without deleting the chat."
|
|
1037
|
+
)
|
|
1038
|
+
self.session_usage_warning.show()
|
|
1039
|
+
else:
|
|
1040
|
+
self.session_usage_warning.hide()
|
|
1041
|
+
return usage
|
|
1042
|
+
|
|
1043
|
+
def record_answer_metadata(self, metadata: AnswerMetadata, usage: TokenUsage) -> None:
|
|
1044
|
+
"""Record usage and render normalized evidence details and safe links."""
|
|
1045
|
+
if isinstance(metadata, AnswerMetadata):
|
|
1046
|
+
safe_metadata = metadata
|
|
1047
|
+
else:
|
|
1048
|
+
raw_origin = getattr(metadata, "evidence_origin", "general_knowledge")
|
|
1049
|
+
origin_value = getattr(raw_origin, "value", raw_origin)
|
|
1050
|
+
try:
|
|
1051
|
+
normalized_origin = EvidenceOrigin(str(origin_value))
|
|
1052
|
+
except ValueError:
|
|
1053
|
+
normalized_origin = EvidenceOrigin.INSUFFICIENT
|
|
1054
|
+
normalized_sources = []
|
|
1055
|
+
raw_sources = getattr(metadata, "sources", ())
|
|
1056
|
+
for raw_source in raw_sources if type(raw_sources) in {list, tuple} else ():
|
|
1057
|
+
source = SourceCitation.create(
|
|
1058
|
+
getattr(raw_source, "title", ""),
|
|
1059
|
+
getattr(raw_source, "url", ""),
|
|
1060
|
+
start_index=getattr(raw_source, "start_index", None),
|
|
1061
|
+
end_index=getattr(raw_source, "end_index", None),
|
|
1062
|
+
)
|
|
1063
|
+
if source is not None:
|
|
1064
|
+
normalized_sources.append(source)
|
|
1065
|
+
safe_metadata = AnswerMetadata(
|
|
1066
|
+
evidence_origin=normalized_origin,
|
|
1067
|
+
sources=tuple(normalized_sources),
|
|
1068
|
+
search_error=str(getattr(metadata, "search_error", "") or "")[:240],
|
|
1069
|
+
)
|
|
1070
|
+
safe_usage = self.record_usage(usage)
|
|
1071
|
+
origin_labels = {
|
|
1072
|
+
EvidenceOrigin.LOCAL_SYSTEM: "Local system",
|
|
1073
|
+
EvidenceOrigin.LOCAL_DOCUMENTATION: "Local documentation",
|
|
1074
|
+
EvidenceOrigin.WEB_AND_DOCUMENTATION: "Web + documentation",
|
|
1075
|
+
EvidenceOrigin.GENERAL_KNOWLEDGE: "General knowledge",
|
|
1076
|
+
EvidenceOrigin.INSUFFICIENT: "Insufficient evidence",
|
|
1077
|
+
}
|
|
1078
|
+
origin = origin_labels.get(safe_metadata.evidence_origin, "Evidence unavailable")
|
|
1079
|
+
provider_model = " / ".join(
|
|
1080
|
+
value for value in (safe_usage.provider, safe_usage.model) if value
|
|
1081
|
+
) or "Offline"
|
|
1082
|
+
search_label = (
|
|
1083
|
+
f"{safe_usage.search_requests} search"
|
|
1084
|
+
if safe_usage.search_requests == 1
|
|
1085
|
+
else f"{safe_usage.search_requests} searches"
|
|
1086
|
+
)
|
|
1087
|
+
summary = (
|
|
1088
|
+
f"{origin} | {provider_model} | {safe_usage.total_tokens:,} tokens | "
|
|
1089
|
+
f"{search_label} | {self._format_cost(safe_usage.estimated_cost_usd)}"
|
|
1090
|
+
)
|
|
1091
|
+
details = [
|
|
1092
|
+
f"{safe_usage.prompt_tokens:,} input",
|
|
1093
|
+
f"{safe_usage.completion_tokens:,} output",
|
|
1094
|
+
]
|
|
1095
|
+
if safe_usage.cached_prompt_tokens:
|
|
1096
|
+
details.append(f"{safe_usage.cached_prompt_tokens:,} cached")
|
|
1097
|
+
assessment_tokens = (
|
|
1098
|
+
safe_usage.assessment_prompt_tokens + safe_usage.assessment_completion_tokens
|
|
1099
|
+
)
|
|
1100
|
+
if assessment_tokens:
|
|
1101
|
+
details.append(f"{assessment_tokens:,} assessment")
|
|
1102
|
+
if safe_usage.search_content_tokens:
|
|
1103
|
+
details.append(f"{safe_usage.search_content_tokens:,} search-content tokens")
|
|
1104
|
+
if safe_usage.estimated_model_cost_usd not in {None, 0.0}:
|
|
1105
|
+
details.append(f"model {self._format_cost(safe_usage.estimated_model_cost_usd)}")
|
|
1106
|
+
if safe_usage.estimated_search_cost_usd not in {None, 0.0}:
|
|
1107
|
+
details.append(f"search {self._format_cost(safe_usage.estimated_search_cost_usd)}")
|
|
1108
|
+
if safe_metadata.search_error:
|
|
1109
|
+
details.append(safe_metadata.search_error[:240])
|
|
1110
|
+
self.last_usage_label.setText(f"{summary}\n({', '.join(details)})")
|
|
1111
|
+
self._render_sources(safe_metadata.sources)
|
|
1112
|
+
|
|
1113
|
+
def _render_sources(self, sources: tuple[SourceCitation, ...]) -> None:
|
|
1114
|
+
for label in self.source_links:
|
|
1115
|
+
self.sources_layout.removeWidget(label)
|
|
1116
|
+
label.deleteLater()
|
|
1117
|
+
self.source_links.clear()
|
|
1118
|
+
seen: set[str] = set()
|
|
1119
|
+
for raw_source in sources:
|
|
1120
|
+
if not isinstance(raw_source, SourceCitation):
|
|
1121
|
+
continue
|
|
1122
|
+
source = SourceCitation.create(raw_source.title, raw_source.url)
|
|
1123
|
+
if source is None:
|
|
1124
|
+
continue
|
|
1125
|
+
key = source.url.casefold().rstrip("/")
|
|
1126
|
+
if key in seen:
|
|
1127
|
+
continue
|
|
1128
|
+
seen.add(key)
|
|
1129
|
+
label = QLabel(
|
|
1130
|
+
f'<a href="{escape(source.url, quote=True)}">'
|
|
1131
|
+
f'{escape(source.title[:90])}</a>'
|
|
1132
|
+
)
|
|
1133
|
+
label.setToolTip(source.url)
|
|
1134
|
+
label.setOpenExternalLinks(True)
|
|
1135
|
+
label.setTextInteractionFlags(Qt.TextBrowserInteraction)
|
|
1136
|
+
label.setWordWrap(True)
|
|
1137
|
+
self.sources_layout.addWidget(label)
|
|
1138
|
+
self.source_links.append(label)
|
|
1139
|
+
self.sources_title_label.setVisible(bool(self.source_links))
|
|
1140
|
+
self.sources_scroll.setFixedHeight(min(120, 40 * len(self.source_links)))
|
|
1141
|
+
self.sources_scroll.setVisible(bool(self.source_links))
|
|
1142
|
+
|
|
1143
|
+
def set_request_stage(self, stage: str) -> None:
|
|
1144
|
+
"""Show a bounded progress message for the active request."""
|
|
1145
|
+
messages = {
|
|
1146
|
+
"checking_documentation": "Checking documentation...",
|
|
1147
|
+
"searching_current_information": "Searching current information...",
|
|
1148
|
+
"generating_answer": "Generating answer...",
|
|
1149
|
+
}
|
|
1150
|
+
self.request_stage_label.setText(messages.get(str(stage or ""), ""))
|
|
1151
|
+
|
|
1152
|
+
def reset_usage(self) -> None:
|
|
1153
|
+
self._session_usage = TokenUsage()
|
|
1154
|
+
self.last_usage_label.setText("Last request: no usage yet")
|
|
1155
|
+
self.session_usage_label.setText("Session: 0 tokens · $0.0000")
|
|
1156
|
+
self.session_usage_warning.clear()
|
|
1157
|
+
self.session_usage_warning.hide()
|
|
1158
|
+
self._render_sources(())
|
|
1159
|
+
|
|
1160
|
+
def _rerender_history(self) -> None:
|
|
1161
|
+
if not self._message_records:
|
|
1162
|
+
return
|
|
1163
|
+
self._messages = [self._message_html(role, text) for role, text in self._message_records]
|
|
1164
|
+
self.history_view.setHtml("".join(self._messages))
|
|
1165
|
+
scrollbar = self.history_view.verticalScrollBar()
|
|
1166
|
+
scrollbar.setValue(scrollbar.maximum())
|
|
1167
|
+
|
|
1168
|
+
def changeEvent(self, event: QEvent) -> None:
|
|
1169
|
+
if event.type() in {QEvent.PaletteChange, QEvent.ApplicationPaletteChange}:
|
|
1170
|
+
self._apply_styles()
|
|
1171
|
+
super().changeEvent(event)
|
|
1172
|
+
|
|
1173
|
+
def resizeEvent(self, event) -> None:
|
|
1174
|
+
super().resizeEvent(event)
|
|
1175
|
+
self._resize_api_setup_movie()
|
|
1176
|
+
|
|
1177
|
+
def set_busy(self, is_busy: bool) -> None:
|
|
1178
|
+
self._busy = bool(is_busy)
|
|
1179
|
+
self.suggested_combo.setEnabled(not self._busy)
|
|
1180
|
+
self.mode_combo.setEnabled(not self._busy)
|
|
1181
|
+
self.content_tabs.setTabEnabled(1, not self._busy)
|
|
1182
|
+
self.clear_button.setEnabled(not self._busy)
|
|
1183
|
+
self.workflow_summary_button.setEnabled(
|
|
1184
|
+
self._workflow_summary_available and not self._busy
|
|
1185
|
+
)
|
|
1186
|
+
self._update_mode_ui(self.mode_combo.currentText())
|
|
1187
|
+
self._update_send_enabled()
|
|
1188
|
+
|
|
1189
|
+
def set_workflow_summary_available(
|
|
1190
|
+
self,
|
|
1191
|
+
available: bool,
|
|
1192
|
+
*,
|
|
1193
|
+
status: str = "",
|
|
1194
|
+
workflow_kind: str = "",
|
|
1195
|
+
) -> None:
|
|
1196
|
+
"""Update the summary action from the current result snapshot."""
|
|
1197
|
+
self._workflow_summary_available = bool(available)
|
|
1198
|
+
self.workflow_summary_button.setEnabled(self._workflow_summary_available and not self._busy)
|
|
1199
|
+
if not self._workflow_summary_available:
|
|
1200
|
+
self.workflow_summary_status.setText("No workflow results detected")
|
|
1201
|
+
self.workflow_summary_button.setText("Summarize workflow")
|
|
1202
|
+
self.workflow_summary_button.setToolTip(
|
|
1203
|
+
"This action becomes available when easyROB detects an available workflow result."
|
|
1204
|
+
)
|
|
1205
|
+
return
|
|
1206
|
+
status_label = {
|
|
1207
|
+
"completed": "completed",
|
|
1208
|
+
"completed_with_warnings": "completed with warnings",
|
|
1209
|
+
"failed": "failed",
|
|
1210
|
+
"partial": "partial results",
|
|
1211
|
+
"running": "running",
|
|
1212
|
+
}.get(str(status or "").strip().lower(), str(status or "result").replace("_", " "))
|
|
1213
|
+
kind_label = str(workflow_kind or "Workflow").strip()
|
|
1214
|
+
is_robert_report = "ROBERT" in kind_label.upper()
|
|
1215
|
+
self.workflow_summary_button.setText(
|
|
1216
|
+
"Summarize report" if is_robert_report else "Summarize workflow"
|
|
1217
|
+
)
|
|
1218
|
+
self.workflow_summary_status.setText(f"{kind_label}: {status_label}")
|
|
1219
|
+
self.workflow_summary_button.setToolTip(
|
|
1220
|
+
"Explain the ROBERT report from its structured DAT and CSV evidence without uploading the PDF."
|
|
1221
|
+
if is_robert_report else
|
|
1222
|
+
"Create a grounded summary from the detected workflow logs and CSV outputs."
|
|
1223
|
+
)
|
|
1224
|
+
|
|
1225
|
+
def set_saved_api_state(self, has_saved_api: bool) -> None:
|
|
1226
|
+
self.delete_api_button.setProperty("has_saved_api", bool(has_saved_api))
|
|
1227
|
+
self.delete_api_button.setEnabled(
|
|
1228
|
+
not self._busy
|
|
1229
|
+
and str(self.mode_combo.currentText()).strip().lower() == "cloud ai"
|
|
1230
|
+
and bool(has_saved_api)
|
|
1231
|
+
)
|