Files
cowork-local/ui/routing_toggle.py
T
anhtnm1andClaude Opus 5 e29a0ccdbd refactor: vá 4 hồi quy, tách 4 file chạm trần LOC, docstring lên 100%
Hồi quy đã vá
-------------
F-12  Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
      `chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
      `composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
      tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
      là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
      gắn 1 tệp, khớp bản trước refactor.

F-01  Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
      `folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
      `ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
      tiên hỏng thì đổi provider chính là lúc phải thử lại.

F-07  Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
      lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
      người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
      theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
      mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
      `check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.

F-11  Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
      mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
      và không bao giờ chạy.

Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:

    graph_renderer.py         -> graph_scene_builder.py + graph_export.py
    co4e_workflow_service.py  -> co4e_run_history.py
    json_config_repository.py -> config_sections.py
    agents_admin_tab.py       -> shared/agent_kind_visuals.py

File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.

Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.

Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.

859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-30 10:41:45 +09:00

233 lines
8.8 KiB
Python

"""Off/Auto/Manual/Fallback routing toggle + Auto-run toggle + confirm dialog.
Dropped into every chat surface's composer (Cowork / Co4E / AI-Edit). By
default a :class:`RoutingToggle` reads/writes the **per-workspace** mode via
``AppContext.project_routing_mode`` / ``set_project_routing_mode`` (so each
workspace keeps its own mode), but the storage is fully injectable through
``get_mode``/``set_mode`` callables — all the real decision logic lives in
``application/model_routing`` (which the surfaces call through
``RoutingApplicationService``). Call :meth:`refresh` when the active workspace
changes so the control shows that workspace's mode.
"""
from __future__ import annotations
from typing import Any, Callable, Optional
from PySide6.QtCore import Qt, QTimer, Signal
from PySide6.QtWidgets import (
QCheckBox,
QComboBox,
QHBoxLayout,
QLabel,
QMessageBox,
QWidget,
)
from ..i18n import tr
class RoutingToggle(QWidget):
"""A small ``Routing: [Off ▾]`` control bound to one chat surface.
Storage is injectable so the same widget can back a per-workspace mode, a
global mode, or anything else:
* ``get_mode()`` returns the current mode string to display.
* ``set_mode(mode)`` persists a newly-chosen mode.
When omitted, both default to the ACTIVE workspace's per-surface mode
(``AppContext.project_routing_mode`` / ``set_project_routing_mode``).
Emits :attr:`mode_changed`; call :meth:`refresh` after the workspace switches.
"""
mode_changed = Signal(str) # "off" | "auto" | "manual" | "fallback"
def __init__(
self,
ctx: Any,
surface: str,
parent: Optional[QWidget] = None,
*,
get_mode: Optional[Callable[[], str]] = None,
set_mode: Optional[Callable[[str], None]] = None,
) -> None:
"""Công tắc chế độ định tuyến cho một bề mặt chat.
``get_mode``/``set_mode`` tiêm được để dùng lại công tắc này ở chỗ đọc/ghi
chế độ theo cách khác, mà không phải chép lại cả widget.
"""
super().__init__(parent)
self.ctx = ctx
self.surface = surface
# Default to per-workspace storage (each workspace keeps its own mode).
self._get_mode = get_mode or (lambda: ctx.project_routing_mode(surface))
self._set_mode = set_mode or (lambda m: ctx.set_project_routing_mode(surface, m))
lay = QHBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
lay.setSpacing(4)
self._label = QLabel(tr("routing.toggle_label"))
self._label.setObjectName("hint")
self._combo = QComboBox()
self._combo.setToolTip(tr("routing.toggle_tooltip"))
# (data value, i18n key) — data is the persisted mode string. Order is
# least-to-most autonomous, with Fallback (R03-T03) last because it is
# the "only when something breaks" mode rather than a stronger Auto.
self._modes = [
("off", "routing.mode_off"),
("auto", "routing.mode_auto"),
("manual", "routing.mode_manual"),
("fallback", "routing.mode_fallback"),
]
for value, key in self._modes:
self._combo.addItem(tr(key), value)
self.refresh() # reflect the current (per-workspace) mode
self._combo.currentIndexChanged.connect(self._on_changed)
lay.addWidget(self._label)
lay.addWidget(self._combo)
def current_mode(self) -> str:
"""Chế độ định tuyến đang chọn; 'off' nếu chưa đặt."""
return self._combo.currentData() or "off"
def refresh(self) -> None:
"""Re-read the backing mode (e.g. after switching workspace) and show it
without emitting a spurious change."""
try:
mode = self._get_mode() or "off"
except Exception: # noqa: BLE001
mode = "off"
idx = self._combo.findData(mode)
if idx < 0:
idx = 0
self._combo.blockSignals(True)
self._combo.setCurrentIndex(idx)
self._combo.blockSignals(False)
def retranslate(self) -> None:
"""Re-apply labels after a language change."""
self._label.setText(tr("routing.toggle_label"))
self._combo.setToolTip(tr("routing.toggle_tooltip"))
for i, (value, key) in enumerate(self._modes):
self._combo.setItemText(i, tr(key))
def _on_changed(self, _idx: int) -> None:
"""Lưu chế độ vừa chọn. Nuốt lỗi có chủ ý: một cú đổi công tắc không được
phép làm vỡ giao diện.
"""
mode = self.current_mode()
try:
self._set_mode(mode)
except Exception: # noqa: BLE001 — never let a toggle change crash the UI
pass
self.mode_changed.emit(mode)
class AutoRunToggle(QWidget):
"""A checkbox that auto-approves commands for the ACTIVE workspace.
Checked → commands run without a confirm dialog (auto-approve) in this
workspace; unchecked → the Approve/Reject dialog is shown. Backed by the
per-workspace ``auto_run`` override (``AppContext.set_project_auto_run``),
falling back to the global ``cowork_confirm_commands`` when unset.
"""
toggled_auto = Signal(bool)
def __init__(self, ctx: Any, parent: Optional[QWidget] = None) -> None:
"""Ô tick tự chạy: đổi model xong có tự tiếp tục lượt hay dừng lại hỏi."""
super().__init__(parent)
self.ctx = ctx
lay = QHBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
lay.setSpacing(4)
self._chk = QCheckBox(tr("routing.autorun_label"))
self._chk.setToolTip(tr("routing.autorun_tooltip"))
self.refresh()
self._chk.toggled.connect(self._on_toggled)
lay.addWidget(self._chk)
def refresh(self) -> None:
"""Đọc lại trạng thái tự chạy của project đang mở lên ô đánh dấu."""
try:
auto = bool(self.ctx.project_auto_run())
except Exception: # noqa: BLE001
auto = False
self._chk.blockSignals(True)
self._chk.setChecked(auto)
self._chk.blockSignals(False)
def retranslate(self) -> None:
"""Áp lại chữ theo ngôn ngữ đang chọn."""
self._chk.setText(tr("routing.autorun_label"))
self._chk.setToolTip(tr("routing.autorun_tooltip"))
def _on_toggled(self, checked: bool) -> None:
"""Ghi trạng thái tự chạy vào project đang mở."""
try:
self.ctx.set_project_auto_run(bool(checked))
except Exception: # noqa: BLE001
pass
self.toggled_auto.emit(bool(checked))
def confirm_switch(parent: QWidget, decision: Any, timeout_sec: float) -> bool:
"""Modal Manual-mode confirm: ask before switching, auto-keep on timeout.
Returns True if the user approved the switch; False if they declined or the
``timeout_sec`` window elapsed (→ keep the current model, per spec). The
"Keep current" button shows a live countdown so the timeout is visible.
"""
from ..core.routing.models import split_key
from_id = split_key(decision.from_model)[1] if decision.from_model else "—"
to_id = split_key(decision.to_model)[1] if decision.to_model else "—"
box = QMessageBox(parent)
box.setIcon(QMessageBox.Question)
box.setWindowTitle(tr("routing.confirm_title"))
box.setText(tr(
"routing.confirm_body",
task=decision.task_type or "?",
from_model=from_id,
to_model=to_id,
gain=f"{decision.score_gain:.2f}",
reason=decision.reason,
))
yes_btn = box.addButton(tr("routing.confirm_yes"), QMessageBox.AcceptRole)
no_btn = box.addButton(tr("routing.confirm_no"), QMessageBox.RejectRole)
box.setDefaultButton(no_btn)
# Countdown that auto-declines (keep current) when the window elapses.
remaining = {"secs": int(max(1, round(timeout_sec)))}
timer = QTimer(box)
timer.setInterval(1000)
def _tick() -> None:
"""Đếm lùi mỗi giây; hết giờ thì tự đóng hộp thoại theo hướng GIỮ model hiện tại.
Hết giờ mà tự đổi model là quyết định thay người dùng — mặc định an toàn
phải là không đổi gì.
"""
remaining["secs"] -= 1
if remaining["secs"] <= 0:
timer.stop()
box.done(QMessageBox.RejectRole) # timeout → keep current
else:
no_btn.setText(tr("routing.confirm_countdown", secs=remaining["secs"]))
no_btn.setText(tr("routing.confirm_countdown", secs=remaining["secs"]))
timer.timeout.connect(_tick)
if timeout_sec > 0:
timer.start()
box.exec()
timer.stop()
return box.clickedButton() is yes_btn
__all__ = ["RoutingToggle", "AutoRunToggle", "confirm_switch"]