Batch of fixes for defects tracked in "Task Tracking Template.xlsx" (sheet Defect Management), verified against the sheet's Root Cause/Cach xu ly columns before this commit: - DF-002: Co4E node status not reflected after tab switch + missing edit-lock on running/done nodes (node_property_panel.py, co4e_runs.py, co4e_workflow_crud.py, co4e_canvas_widget.py, co4e_flow_tabs.py, canvas_items.py) - DF-003: hide the run.bat console window unless the app exits with an error (run.bat, scripts/console_visibility.ps1 - new) - DF-004: floating Help Assistant icon covering the Send button after a window resize (presentation/shell/main_window.py) - DF-005: "block network" toggle didn't stop ICMP/raw-socket tools like ping (infrastructure/filesystem/command_tools.py, security/command_risk_classifier.py) - DF-006: Monitoring "gay nang khi log lon" - root cause was re-reading the ENTIRE audit log history every 3s tick, not missing pagination; bounded to a 30-day window (presentation/monitoring/monitoring_tab.py) AND added the "So dong/trang" page-size control the ticket also asked for (presentation/monitoring/shared/event_table.py, shared/filter_scaffold.py, tabs/action_logs_tab.py, tabs/mcp_tab.py, tabs/security_events_tab.py, i18n/agents_admin_tab.py) - DF-007: support choosing a OneDrive/SharePoint folder as a project's working directory via Microsoft Graph, downloaded as a local mirror with manual sync (core/projects.py, core/ms365_graph.py, core/cloud_workspace_sync.py - new, ui/ms365_signin_dialog.py - new, ui/cloud_folder_picker_dialog.py - new, i18n/cloud_workspace.py - new, ui/workspace_tab.py) - DF-008: AI-edit instruction box was a fixed-height single-line QLineEdit; replaced with an auto-expanding, Enter-to-send/Shift+Enter-newline input (presentation/folder/ai_file_editor_dialog.py) - DF-011: run_command failed with WinError 267 for a project whose per-turn output directory had never been created (application/conversations/core_runtime_adapter.py) DF-009 (AI-edit Apply/Discard buttons easy to miss) and DF-010 (AI reply language - dev-confirmed not a bug) are intentionally NOT part of this commit: DF-009 has no code fix yet (still "Assigned" in the sheet, only a UX recommendation was recorded), DF-010 was rejected as expected behavior. Tests: tests/test_cloud_workspace_sync.py, tests/test_ms365_cloud_dialogs.py, tests/test_ai_file_editor_input.py, tests/test_monitoring_page_size.py (all new, all passing). Full suite: 896 passed, 13 known-and-documented failures unrelated to this change (an existing core/audit_log.py bug, this checkout not being a git repo before now, and a repo/subprocess folder-naming mismatch affecting ~66 characterization tests) - see the sheet's DF-006 Evidence column for details.
392 lines
19 KiB
Python
392 lines
19 KiB
Python
"""Cửa sổ chính — R08-T10.
|
||
|
||
Bóc nguyên khối ra khỏi ``app.py``. ``app.py`` giờ chỉ còn điểm vào của chương
|
||
trình: dựng QApplication, gọi Composition Root, mở cửa sổ.
|
||
|
||
Vì sao tách: ``app.py`` là nơi mọi thứ đổ về — nó vừa là điểm vào, vừa giữ cửa
|
||
sổ, vừa giữ thanh điều hướng, vừa giữ thanh trên cùng. Ai sửa bất cứ mảng nào
|
||
cũng phải mở đúng một file 1.293 dòng, và ba người sửa ba mảng khác nhau thì
|
||
đụng nhau ở cùng một chỗ.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
"""
|
||
pages (Dashboard / Schedule / Workspace / Cowork / Structure) and top bar."""
|
||
|
||
import sys
|
||
|
||
from PySide6.QtCore import Qt, QTimer
|
||
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QStackedWidget, QVBoxLayout, QWidget
|
||
|
||
from ... import DISPLAY_NAME, __version__
|
||
from ...i18n import on_language_changed, tr
|
||
from .branding import app_icon
|
||
from .lifecycle_coordinator import LifecycleCoordinator
|
||
from .page_registry import PageRegistryMixin
|
||
from .rail_project import RailProjectMixin
|
||
from .session_events import SessionEventsMixin
|
||
from .toast import Toast
|
||
from .top_bar import TopBarMixin
|
||
from .nav_rail import NavRailMixin
|
||
from .rail_metrics import _NAV_MIN_WIDTH
|
||
from .tray_manager import TrayManager
|
||
from ...state import AppContext
|
||
from ...core.task_scheduler import TaskScheduler
|
||
from ...ui.cowork_tab import CoworkTab
|
||
from ...ui.sidebar import HistorySidebar
|
||
from ..graph.structure_graph_view import StructureGraphView
|
||
from ...ui.workspace_tab import WorkspaceTab
|
||
|
||
|
||
|
||
|
||
# Nav rail (sidebar navigation) widths — expanded shows icon+label, collapsed
|
||
# shows icon-only (still fully clickable, just narrower).
|
||
# The splitter between rail and content draws a drag handle. It only means
|
||
# something if the rail can actually take a width from it, so the expanded rail
|
||
# is a range rather than one number; long project and thread names in RECENTS
|
||
# are the reason someone would widen it.
|
||
#
|
||
# The ceiling is a SHARE of the window, not a pixel count: 360px is a quarter
|
||
# of a 1440 screen and more than a quarter of a 1280 one, where it left the
|
||
# seven Kanban lanes 920px of the 1067 they need. A share behaves the same on
|
||
# every monitor.
|
||
# Where a rail row starts, and how much air sits between its icon and its
|
||
# label. The tree rows get these from the style; anything laid out by hand
|
||
# beside them has to use the same two numbers or it will not line up.
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
class MainWindow(NavRailMixin, RailProjectMixin, TopBarMixin,
|
||
PageRegistryMixin, SessionEventsMixin, QMainWindow):
|
||
#: Biểu tượng khay, hoặc None nếu máy không có khay. Vẫn giữ tên cũ vì
|
||
#: còn vài chỗ đọc thẳng self.tray; bản thân việc dựng/ẩn/thông báo đã
|
||
#: chuyển sang self._tray (TrayManager).
|
||
"""Cửa sổ chính: thanh menu trái, thanh trên, bốn trang nội dung và khay hệ thống.
|
||
|
||
Ghép từ nhiều mixin (thanh menu, bộ chọn project, thanh trên, sổ đăng ký
|
||
trang, sự kiện phiên) để mỗi phần nằm trong một file dưới trần 400 dòng.
|
||
"""
|
||
tray = property(lambda self: self._tray.icon)
|
||
|
||
# Nav rows (Dashboard/Schedule/Monitoring are lazy; Workspace is the eager home page).
|
||
_ROW_DASHBOARD, _ROW_SCHEDULE, _ROW_WORKSPACE, _ROW_MONITORING = 0, 1, 2, 3
|
||
|
||
def __init__(self, ctx: AppContext, user_name: str = ""):
|
||
"""Dựng cửa sổ chính: thanh điều hướng, các trang và biểu tượng khay."""
|
||
super().__init__()
|
||
self.ctx = ctx
|
||
self._user_name = user_name
|
||
self._really_quit = False
|
||
self._life = LifecycleCoordinator(self)
|
||
# Khay hệ thống: presentation/shell/tray_manager.py (R08-T10).
|
||
self._tray = TrayManager(self, icon=app_icon, tooltip=DISPLAY_NAME, tr=tr)
|
||
self._nav_collapsed = False # icon-only nav rail toggle (Task: collapsible nav)
|
||
self._history_collapsed = False # remembers History's own collapse-to-strip state
|
||
self.setWindowTitle(f"{DISPLAY_NAME} v{__version__}")
|
||
self.setWindowIcon(app_icon())
|
||
# Fit to the available screen so the window never opens larger than the
|
||
# monitor (auto-fit). Keep a modest minimum that still fits small laptops.
|
||
self._fit_to_screen(1180, 760)
|
||
|
||
self.sidebar = HistorySidebar(ctx)
|
||
# Task scheduler ENGINE runs in the background whether or not its Kanban
|
||
# UI (built lazily) is on screen — scheduled tasks must fire regardless.
|
||
self.task_scheduler = TaskScheduler(ctx, parent=self)
|
||
# Desktop notification when a scheduled task finishes; also refresh
|
||
# History — a cowork/code task run saves itself as a new session there.
|
||
self.task_scheduler.task_finished.connect(self._on_scheduled_task_done)
|
||
# NOTE: task_started fires BEFORE the worker thread even begins, so its
|
||
# session doesn't exist on disk yet — refreshing History here would
|
||
# find nothing. history_ready fires once the session is actually
|
||
# saved (right as the run starts, then again after each turn), which
|
||
# is what really makes a Running task's session show up live.
|
||
self.task_scheduler.history_ready.connect(lambda _tid: self._refresh_history())
|
||
|
||
# Cowork chat + GraphRAG view are embedded as sub-tabs INSIDE the
|
||
# Workspace screen (per selected project). GraphRAG's heavy
|
||
# QtWebEngine is still built lazily on first display
|
||
# (StructureGraphView._ensure_web).
|
||
self.cowork = CoworkTab(ctx)
|
||
self.structure = StructureGraphView(ctx)
|
||
self.structure.status_message.connect(self.statusBar().showMessage)
|
||
self.cowork.output_changed.connect(self.structure.schedule_rescan)
|
||
self.cowork.status_message.connect(self.statusBar().showMessage)
|
||
# Refresh History (list + running markers + current highlight) whenever a
|
||
# conversation is created/updated or a turn finishes.
|
||
self.cowork.turn_finished.connect(lambda *_: self._refresh_history())
|
||
self.cowork.history_changed.connect(self._refresh_history)
|
||
self.cowork.turn_finished.connect(
|
||
lambda result: self._notify_task(self.cowork, "cowork", result))
|
||
|
||
# Workspace screen — the app HOME: project management + the per-project
|
||
# Cowork / GraphRAG sub-tabs and History.
|
||
self.workspace = WorkspaceTab(ctx, cowork=self.cowork, structure=self.structure,
|
||
sidebar=self.sidebar)
|
||
self.workspace.status_message.connect(self.statusBar().showMessage)
|
||
self.workspace.projects_changed.connect(self._on_projects_changed)
|
||
self.workspace.open_chat.connect(lambda *_: self._refresh_history())
|
||
self.workspace.new_chat.connect(lambda *_: self._refresh_history())
|
||
|
||
# Dashboard + Schedule pages are built lazily on first visit (lazy page
|
||
# creation — keeps startup light); None until then.
|
||
self.dashboard = None
|
||
self.schedule = None
|
||
self.monitoring = None
|
||
|
||
# --- right side: top bar + pages (nav rail drives the stack) ---
|
||
right = QWidget()
|
||
right.setObjectName("contentArea")
|
||
rlay = QVBoxLayout(right)
|
||
rlay.setContentsMargins(10, 10, 10, 10)
|
||
rlay.setSpacing(10)
|
||
rlay.addWidget(self._build_topbar())
|
||
|
||
self.pages = QStackedWidget()
|
||
# (i18n key, icon, builder-or-None, eager-widget-or-None) — page index == list index
|
||
self._nav_defs = [
|
||
("app.tab.dashboard", "dashboard", self._build_dashboard, None),
|
||
("app.tab.schedule", "schedule", self._build_schedule, None),
|
||
("app.tab.workspace", "workspaces", None, self.workspace),
|
||
("app.tab.monitoring", "monitoring", self._build_monitoring, None),
|
||
]
|
||
self._page_widgets = [] # page index → widget (placeholder until lazily built)
|
||
self._built = []
|
||
for _key, _icon_name, _builder, widget in self._nav_defs:
|
||
page = widget if widget is not None else QWidget()
|
||
self.pages.addWidget(page)
|
||
self._page_widgets.append(page)
|
||
self._built.append(widget is not None)
|
||
|
||
self._build_nav_rail(right, rlay)
|
||
# Landing stays Workspace ▸ Project, exactly as before. Go through _goto
|
||
# so the page is actually shown — selecting the row alone only moves the
|
||
# highlight (its signals are blocked to avoid rebuild loops).
|
||
self._goto(self._ROW_WORKSPACE, self.workspace.current_subtab())
|
||
self.toast = Toast(self) # top-left "task done" popup
|
||
# Floating in-app Help assistant — a robot icon pinned bottom-right on
|
||
# every screen; expands into a small help-only chat (see
|
||
# ui/help_agent_widget.py). Managed in Monitoring → Agents Admin.
|
||
from ...ui.help_agent_widget import HelpAgentWidget
|
||
self.help_agent = HelpAgentWidget(ctx, self, user_name=self._user_name)
|
||
self.help_agent.status_message.connect(self.statusBar().showMessage)
|
||
|
||
self.statusBar().showMessage(tr("app.status.ready"))
|
||
# Author credit, pinned to the bottom-right corner. A permanent status-bar
|
||
# widget sits at the right end and is never cleared by showMessage (which
|
||
# writes on the left).
|
||
self._credit = QLabel(tr("app.credit"))
|
||
self._credit.setObjectName("faint")
|
||
self._credit.setStyleSheet("padding: 0 10px;")
|
||
self.statusBar().addPermanentWidget(self._credit)
|
||
self._restore_sessions()
|
||
self._tray.setup()
|
||
# Start the task scheduler last, once the whole window exists — it
|
||
# catches up any overdue tasks right away (first tick runs inline).
|
||
self.task_scheduler.start()
|
||
# Auto Model Routing: periodic reassess + pending-switch expiry. Runs
|
||
# background probes only when genuinely due (never a burst at launch).
|
||
try:
|
||
from ...core.routing.scheduler import RoutingScheduler
|
||
self.routing_scheduler = RoutingScheduler(self.ctx, self.ctx.routing(), parent=self)
|
||
self.routing_scheduler.start()
|
||
except Exception: # noqa: BLE001 — routing must never block app startup
|
||
self.routing_scheduler = None
|
||
on_language_changed(self._retranslate)
|
||
|
||
def resizeEvent(self, event): # noqa: N802 - Qt override
|
||
"""Đổi kích thước cửa sổ thì tính lại trần bề rộng thanh menu.
|
||
|
||
Trần là một tỉ lệ của cửa sổ nên phải tính lại ở đây; tính một lần lúc dựng
|
||
sẽ đọc phải kích thước của cửa sổ chưa được cấp phát.
|
||
"""
|
||
super().resizeEvent(event)
|
||
# The rail's ceiling is a share of the window, so it moves with the
|
||
# window. Computed once at construction it was read off a not-yet-sized
|
||
# window and stuck at 162px on every monitor.
|
||
if getattr(self, "_nav_wrap", None) is not None and not self._nav_collapsed:
|
||
self._set_nav_width_range(_NAV_MIN_WIDTH, self._nav_max_width())
|
||
# Keep the floating Help assistant pinned to the bottom-right corner.
|
||
if getattr(self, "help_agent", None) is not None:
|
||
# The Cowork composer can wrap an extra control row as the window
|
||
# narrows/widens, which changes how much bottom guard the dock
|
||
# needs — recompute it on every resize, not just reposition with
|
||
# whatever guard height was last measured at tab-entry time.
|
||
self._update_dock_guard()
|
||
self.help_agent.reposition()
|
||
|
||
def showEvent(self, event): # noqa: N802 - Qt override
|
||
"""Lần hiện đầu tiên: ghim lại trợ lý nổi cho đúng vị trí thật."""
|
||
super().showEvent(event)
|
||
if getattr(self, "help_agent", None) is not None:
|
||
self._update_dock_guard()
|
||
self.help_agent.reposition()
|
||
self.help_agent.raise_()
|
||
# Build GraphRAG's browser view and first graph once the window is up
|
||
# and idle, so clicking GraphRAG does not sit on an empty view while
|
||
# both happen. 3s is after the first paint and any startup refresh.
|
||
if not getattr(self, "_graph_prewarmed", False):
|
||
self._graph_prewarmed = True
|
||
QTimer.singleShot(3000, self._prewarm_graph)
|
||
|
||
def _prewarm_graph(self) -> None:
|
||
"""Dựng sẵn khung đồ thị GraphRAG lúc máy rảnh, để lần bấm đầu không giật."""
|
||
view = getattr(self, "structure", None)
|
||
if view is None or not hasattr(view, "prewarm"):
|
||
return
|
||
try:
|
||
view.prewarm()
|
||
except Exception: # noqa: BLE001 — a warm-up must never break the app
|
||
pass
|
||
|
||
# ---- i18n ----------------------------------------------------------
|
||
def _retranslate(self) -> None:
|
||
"""Re-apply the current language to this window's own static chrome
|
||
(tabs are the only long-lived text here; the tabs/dialogs retranslate
|
||
themselves)."""
|
||
self._apply_nav_labels()
|
||
self._nav_toggle_btn.setText("" if self._nav_collapsed else tr("app.nav.menu_label"))
|
||
self._nav_toggle_btn.setToolTip(
|
||
tr("app.nav.expand_tooltip") if self._nav_collapsed else tr("app.nav.collapse_tooltip"))
|
||
self._credit.setText(tr("app.credit"))
|
||
if hasattr(self, "provider_lbl"):
|
||
self.provider_lbl.setText(tr("app.provider"))
|
||
if hasattr(self, "settings_btn"):
|
||
self.settings_btn.setText(tr("app.settings"))
|
||
if hasattr(self, "theme_btn"):
|
||
self.theme_btn.setToolTip(tr("settings.theme"))
|
||
for value, act in self._theme_actions.items():
|
||
act.setText(tr(f"settings.theme_{value}"))
|
||
if hasattr(self, "logo_lbl"):
|
||
self.logo_lbl.setText(tr("app.logo"))
|
||
if getattr(self, "help_agent", None) is not None:
|
||
self.help_agent.retranslate()
|
||
self._tray.retranslate()
|
||
|
||
# ---- system tray (run in background when the window is closed) ---
|
||
|
||
|
||
# ---- lazy page building -------------------------------------------
|
||
|
||
|
||
|
||
# Monitoring KEEPS its own tab strip: its eight sub-views live in the
|
||
# page, not in the rail. Workspace is the one that hides its strip,
|
||
# because the rail lists its sub-views directly.
|
||
|
||
|
||
# ---- flat nav rail -------------------------------------------------
|
||
|
||
|
||
|
||
|
||
|
||
def _show_window(self) -> None:
|
||
"""Đưa cửa sổ trở lại từ khay hệ thống và kéo lên trước."""
|
||
self.showNormal()
|
||
self.raise_()
|
||
self.activateWindow()
|
||
|
||
def _quit_app(self) -> None:
|
||
"""Thoát hẳn ứng dụng (khác với đóng cửa sổ về khay)."""
|
||
self._really_quit = True
|
||
self.close()
|
||
|
||
|
||
# ---- top bar -----------------------------------------------------
|
||
|
||
|
||
_BRAND_LOGO_NAMES = ("fpt_logo.png", "fpt-logo.png", "logo_fpt.png", "fpt_logo.jpg")
|
||
_BRAND_LOGO_HEIGHT = 22
|
||
|
||
|
||
_THEME_ICONS = {"system": "monitor", "dark": "moon", "light": "sun"}
|
||
|
||
|
||
|
||
# ---- handlers ----------------------------------------------------
|
||
|
||
|
||
|
||
|
||
def _update_dock_guard(self) -> None:
|
||
"""Keep the floating assistant clear of a screen's own bottom bar.
|
||
|
||
Only Cowork has one (the composer). Everywhere else the dock sits in
|
||
the corner as before.
|
||
"""
|
||
dock = getattr(self, "help_agent", None)
|
||
if dock is None:
|
||
return
|
||
guard = 0
|
||
on_cowork = (self.pages.currentIndex() == self._ROW_WORKSPACE
|
||
and self.workspace.current_subtab() == self.workspace._cowork_tab_idx)
|
||
if on_cowork:
|
||
comp = getattr(self.cowork, "composer", None)
|
||
if comp is not None and not comp.isHidden():
|
||
# Measured from the composer's TOP edge in window coordinates:
|
||
# its own height misses the extra row of controls laid out under
|
||
# it, which left the dot still overlapping by ~25px.
|
||
origin = comp.mapTo(self, comp.rect().topLeft())
|
||
# ...but only lift the dot if the composer is actually beneath
|
||
# it. The composer stops at the chat column's right edge, well
|
||
# short of the dot, so lifting it there raised the dot 156px for
|
||
# nothing — on Cowork alone it sat off the corner every other
|
||
# screen keeps it in.
|
||
dock_left = dock.x() - self.mapToGlobal(self.rect().topLeft()).x()
|
||
dock_right = dock_left + dock.width()
|
||
if dock_right > origin.x() and dock_left < origin.x() + comp.width():
|
||
guard = max(0, self.height() - origin.y() + 8)
|
||
dock.set_bottom_guard(guard)
|
||
|
||
|
||
|
||
# ---- sizing ------------------------------------------------------
|
||
# Share of the available screen the window takes when it has room to. Fixed
|
||
# pixels do not travel: 1180×760 fills a laptop and looks lost on a 4K
|
||
# panel. `want_*` stays the floor so a small screen behaves as before.
|
||
# Canh cửa sổ và tắt sạch: presentation/shell/lifecycle_coordinator.py
|
||
def _fit_to_screen(self, want_w: int, want_h: int) -> None:
|
||
"""Canh cửa sổ vừa màn hình hiện tại."""
|
||
self._life.fit_to_screen(want_w, want_h)
|
||
|
||
def _on_screen_maybe_changed(self) -> None:
|
||
"""Cửa sổ có thể đã sang màn hình khác: ghim lại trợ lý và tính lại bố cục."""
|
||
if not self._life.screen_maybe_changed():
|
||
return
|
||
if getattr(self, "help_agent", None) is not None:
|
||
self._update_dock_guard()
|
||
self.help_agent.reposition()
|
||
|
||
|
||
def moveEvent(self, event): # noqa: N802 - Qt override
|
||
"""Kéo cửa sổ sang màn hình khác thì vùng làm việc và tỉ lệ hiển thị có thể
|
||
khác — trợ lý nổi ghim lại, các pane quyết định lại xem còn vừa không.
|
||
"""
|
||
super().moveEvent(event)
|
||
# Dragged to another monitor: its work area (and scaling) may differ, so
|
||
# the floating assistant re-pins and the panes re-decide if they fit.
|
||
self._on_screen_maybe_changed()
|
||
|
||
|
||
# ---- lifecycle ---------------------------------------------------
|
||
def closeEvent(self, event) -> None: # noqa: N802
|
||
"""Đóng cửa sổ: thu về khay nếu cấu hình cho chạy nền, còn không thì thoát hẳn.
|
||
|
||
Chạy nền tiếp thì task theo lịch vẫn chạy và vẫn tự lưu.
|
||
"""
|
||
if self._life.should_keep_running():
|
||
# Chạy nền tiếp: task vẫn chạy và vẫn tự lưu.
|
||
event.ignore()
|
||
self.hide()
|
||
self._tray.show_message(DISPLAY_NAME, tr("app.tray.running_body"), msec=4000)
|
||
return
|
||
# Real quit: stop every running turn (a tab may have several), then close.
|
||
self._life.shutdown()
|
||
self._tray.hide()
|
||
super().closeEvent(event)
|