Compare commits
21
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab04d68a22 | ||
|
|
c05de8054a | ||
|
|
0efc4bbf0e | ||
|
|
fe99bc8727 | ||
|
|
2f3cd100f8 | ||
|
|
4eb0ae660e | ||
|
|
45cb5cbb6c | ||
|
|
64900a2504 | ||
|
|
b2e2791ff6 | ||
|
|
84939eeccc | ||
|
|
fc036d89e3 | ||
|
|
2ac43a97c1 | ||
|
|
d656c5c834 | ||
|
|
971d39203b | ||
|
|
5fb0f3a82d | ||
|
|
aecdb15ed3 | ||
|
|
482fa41d2d | ||
|
|
257cc91a3c | ||
|
|
39df2c6ed1 | ||
|
|
7bd2b95a57 | ||
|
|
5d23a415e1 |
Binary file not shown.
@@ -74,14 +74,6 @@ class CoreToolRuntime:
|
||||
đều phải tra tên, tra trên danh sách sẽ chậm dần theo số tool.
|
||||
"""
|
||||
self._output_dir = Path(output_dir)
|
||||
# Every sandboxed tool (run_command included) gets this as its cwd —
|
||||
# it must exist BEFORE the first tool call, same as the older
|
||||
# run_cowork() (core/chat_agent.py) already does at its output_dir.
|
||||
# Without this, a per-turn ".turns/<id>" folder that was never created
|
||||
# makes run_command's subprocess.Popen(cwd=...) fail immediately with
|
||||
# WinError 267 ("directory name is invalid") before the command even
|
||||
# starts — no network, no output, just an opaque OS error.
|
||||
self._output_dir.mkdir(parents=True, exist_ok=True)
|
||||
self._title = title
|
||||
self._extra_tools = list(extra_tools or ())
|
||||
self._extra_names = {getattr(t, "name", "") for t in self._extra_tools}
|
||||
|
||||
@@ -100,15 +100,11 @@ DEFAULT_CONFIG: Dict[str, Any] = {
|
||||
"resource_limit_cpu_percent": 80, # 0 = unlimited; caps a run_command/install_package process TREE's total CPU%
|
||||
"resource_limit_memory_mb": 2048, # 0 = unlimited; caps total RSS memory (MB)
|
||||
"resource_limit_disk_mb": 512, # 0 = unlimited; caps total disk read+write (MB)
|
||||
# Cut the agent off the network: proxy env pointed at a black hole for
|
||||
# agent-run shell commands, PLUS a flat refusal from every tool tagged
|
||||
# ToolCapability.NETWORK (fetch_url, jira_*, install_package) — those
|
||||
# reach the net in-process, where the proxy trick has nothing to act on.
|
||||
"block_network": True,
|
||||
"block_network": True, # strip proxy env / point at a black-hole address for agent-run commands
|
||||
# Allow the agent's fetch_url tool to read web pages / online documents /
|
||||
# SharePoint-OneDrive share links. Its own toggle — reading a URL for info
|
||||
# is safe and useful, so this defaults ON — but block_network outranks it:
|
||||
# with the network blocked the tool is refused either way.
|
||||
# SharePoint-OneDrive share links. SEPARATE from block_network (that only
|
||||
# sandboxes agent-run shell commands) — reading a URL for info is safe and
|
||||
# useful, so this defaults ON. Toggle in Settings → Security.
|
||||
"allow_url_fetch": True,
|
||||
"sandbox_pw": "", # set through COWORK_SANDBOX_PASSWORD
|
||||
"rulebase_path": "", # custom RULEBASE.md — attached to every agent execution
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
"""Mirror a OneDrive/SharePoint folder to/from a local directory (DF-007).
|
||||
|
||||
This is deliberately NOT a general sync engine: every existing tool
|
||||
(``run_command``, ``read_file``, ``write_file``...) operates on a real local
|
||||
``Path`` (``Project.output_dir`` — see ``core/projects.py::Project.workspace_dir``),
|
||||
and that contract does not change here. A cloud-backed project's
|
||||
``output_dir`` still points at a real local folder; this module only knows how
|
||||
to pull that folder's content down from Graph once, and push it back up once,
|
||||
both on explicit user action (a button click) — there is no background
|
||||
watcher, no continuous sync, no delete propagation, and no conflict
|
||||
resolution beyond "whichever side ran last wins" for a given file. See the
|
||||
DF-007 plan for why: OneDrive/SharePoint sync-client detection is unreliable,
|
||||
so a local mirror + manual sync is the only predictable option that does not
|
||||
touch the sandboxed command/file tools.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Dict, List
|
||||
|
||||
from . import ms365_graph as graph
|
||||
|
||||
|
||||
@dataclass
|
||||
class SyncReport:
|
||||
"""Kết quả một lượt tải xuống/đẩy lên — hiển thị cho người dùng sau khi chạy."""
|
||||
transferred: int = 0
|
||||
skipped_too_large: List[str] = field(default_factory=list)
|
||||
errors: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
def _list_children(token: str, cloud_source: Dict[str, str], remote_path: str) -> List[dict]:
|
||||
provider = cloud_source.get("provider")
|
||||
if provider == "sharepoint":
|
||||
return graph.list_sharepoint_files(token, cloud_source["site_id"], remote_path)
|
||||
return graph.list_onedrive_files(token, remote_path)
|
||||
|
||||
|
||||
def _download_file(token: str, cloud_source: Dict[str, str], remote_path: str) -> bytes:
|
||||
if cloud_source.get("provider") == "sharepoint":
|
||||
return graph.download_sharepoint_file_bytes(token, cloud_source["site_id"], remote_path)
|
||||
return graph.download_onedrive_file_bytes(token, remote_path)
|
||||
|
||||
|
||||
def _upload_file(token: str, cloud_source: Dict[str, str], remote_path: str, data: bytes) -> None:
|
||||
if cloud_source.get("provider") == "sharepoint":
|
||||
graph.upload_sharepoint_file_bytes(token, cloud_source["site_id"], remote_path, data)
|
||||
else:
|
||||
graph.upload_onedrive_file_bytes(token, remote_path, data)
|
||||
|
||||
|
||||
def download_folder(token: str, cloud_source: Dict[str, str], local_dir: Path) -> SyncReport:
|
||||
"""Tải toàn bộ cây thư mục ``cloud_source['remote_path']`` xuống ``local_dir``,
|
||||
giữ nguyên cấu trúc thư mục con. Ghi đè file local nếu đã tồn tại (một
|
||||
chiều: cloud thắng). Không xoá file local nào không còn ở phía cloud."""
|
||||
report = SyncReport()
|
||||
root_remote = cloud_source.get("remote_path", "")
|
||||
local_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _walk(remote_path: str, local_sub: Path) -> None:
|
||||
try:
|
||||
children = _list_children(token, cloud_source, remote_path)
|
||||
except graph.Ms365GraphError as exc:
|
||||
report.errors.append(f"{remote_path or '/'}: {exc}")
|
||||
return
|
||||
for item in children:
|
||||
name = item.get("name", "")
|
||||
if not name:
|
||||
continue
|
||||
child_remote = f"{remote_path}/{name}" if remote_path else name
|
||||
child_local = local_sub / name
|
||||
if "folder" in item:
|
||||
child_local.mkdir(parents=True, exist_ok=True)
|
||||
_walk(child_remote, child_local)
|
||||
else:
|
||||
try:
|
||||
data = _download_file(token, cloud_source, child_remote)
|
||||
child_local.write_bytes(data)
|
||||
report.transferred += 1
|
||||
except graph.Ms365GraphError as exc:
|
||||
report.errors.append(f"{child_remote}: {exc}")
|
||||
|
||||
_walk(root_remote, local_dir)
|
||||
return report
|
||||
|
||||
|
||||
def upload_folder(token: str, cloud_source: Dict[str, str], local_dir: Path) -> SyncReport:
|
||||
"""Đẩy mọi file dưới ``local_dir`` lên đúng đường dẫn tương ứng phía cloud
|
||||
(tạo mới hoặc ghi đè). Một chiều: local thắng cho từng file được duyệt qua.
|
||||
Không xoá file cloud nào đã bị xoá ở local, không phát hiện xung đột."""
|
||||
report = SyncReport()
|
||||
root_remote = cloud_source.get("remote_path", "")
|
||||
local_dir = Path(local_dir)
|
||||
for dirpath, _dirnames, filenames in os.walk(local_dir):
|
||||
rel_dir = Path(dirpath).relative_to(local_dir)
|
||||
for fname in filenames:
|
||||
local_file = Path(dirpath) / fname
|
||||
rel_parts = [] if str(rel_dir) == "." else list(rel_dir.parts)
|
||||
rel_parts.append(fname)
|
||||
child_remote = "/".join(([root_remote] if root_remote else []) + rel_parts)
|
||||
try:
|
||||
data = local_file.read_bytes()
|
||||
_upload_file(token, cloud_source, child_remote, data)
|
||||
report.transferred += 1
|
||||
except graph.Ms365GraphError as exc:
|
||||
if "too large" in str(exc):
|
||||
report.skipped_too_large.append(child_remote)
|
||||
else:
|
||||
report.errors.append(f"{child_remote}: {exc}")
|
||||
except OSError as exc:
|
||||
report.errors.append(f"{child_remote}: {exc}")
|
||||
return report
|
||||
@@ -196,40 +196,6 @@ def write_onedrive_file(token: str, path: str, content: str) -> dict:
|
||||
return resp.json()
|
||||
|
||||
|
||||
# Graph's "simple upload" (a single PUT to .../content) is documented to only
|
||||
# support items up to 4 MiB; anything larger needs a chunked "upload session"
|
||||
# (createUploadSession + PUT-per-range), which this module does not implement
|
||||
# (see DF-007 cloud workspace picker — v1 explicitly skips large files rather
|
||||
# than silently truncating or corrupting them).
|
||||
MAX_SIMPLE_UPLOAD_BYTES = 4 * 1024 * 1024
|
||||
|
||||
|
||||
def _check_upload_size(data: bytes) -> None:
|
||||
if len(data) > MAX_SIMPLE_UPLOAD_BYTES:
|
||||
raise Ms365GraphError(
|
||||
f"File too large for simple upload ({len(data)} bytes > "
|
||||
f"{MAX_SIMPLE_UPLOAD_BYTES} bytes) — chunked upload sessions are not "
|
||||
"implemented yet."
|
||||
)
|
||||
|
||||
|
||||
def download_onedrive_file_bytes(token: str, path: str) -> bytes:
|
||||
"""Đọc RAW BYTES một tệp OneDrive (không ép UTF-8/không cắt) — dùng cho
|
||||
mirror thư mục cloud xuống local, khác với :func:`read_onedrive_file` vốn
|
||||
chỉ dành cho việc đọc nội dung văn bản vào ngữ cảnh chat."""
|
||||
resp = _request("GET", f"/me/drive/root:/{_path_segment(path)}:/content", token)
|
||||
return resp.content
|
||||
|
||||
|
||||
def upload_onedrive_file_bytes(token: str, path: str, data: bytes) -> dict:
|
||||
"""Ghi RAW BYTES vào một tệp OneDrive (tạo mới hoặc ghi đè). Xem
|
||||
:data:`MAX_SIMPLE_UPLOAD_BYTES`."""
|
||||
_check_upload_size(data)
|
||||
resp = _request("PUT", f"/me/drive/root:/{_path_segment(path)}:/content", token,
|
||||
data=data, headers={"Content-Type": "application/octet-stream"})
|
||||
return resp.json()
|
||||
|
||||
|
||||
def _encode_share_url(url: str) -> str:
|
||||
"""Encode a OneDrive/SharePoint sharing URL into Graph's ``u!<base64url>``
|
||||
share-id form (see Microsoft's 'Get access to shared items' docs)."""
|
||||
@@ -263,24 +229,6 @@ def list_sharepoint_files(token: str, site_id: str, path: str = "") -> List[dict
|
||||
return resp.json().get("value", [])
|
||||
|
||||
|
||||
def download_sharepoint_file_bytes(token: str, site_id: str, path: str) -> bytes:
|
||||
"""Đọc RAW BYTES một tệp trong thư viện tài liệu SharePoint — xem
|
||||
:func:`download_onedrive_file_bytes`."""
|
||||
resp = _request(
|
||||
"GET", f"/sites/{quote(site_id)}/drive/root:/{_path_segment(path)}:/content", token)
|
||||
return resp.content
|
||||
|
||||
|
||||
def upload_sharepoint_file_bytes(token: str, site_id: str, path: str, data: bytes) -> dict:
|
||||
"""Ghi RAW BYTES vào một tệp trong thư viện tài liệu SharePoint. Xem
|
||||
:data:`MAX_SIMPLE_UPLOAD_BYTES`."""
|
||||
_check_upload_size(data)
|
||||
resp = _request(
|
||||
"PUT", f"/sites/{quote(site_id)}/drive/root:/{_path_segment(path)}:/content", token,
|
||||
data=data, headers={"Content-Type": "application/octet-stream"})
|
||||
return resp.json()
|
||||
|
||||
|
||||
# ---- Teams meeting transcripts ------------------------------------------
|
||||
def find_online_meeting(token: str, join_url: str) -> List[dict]:
|
||||
"""Tìm cuộc họp online theo link tham gia."""
|
||||
|
||||
@@ -65,13 +65,6 @@ class Project:
|
||||
# auto_run: None → follow the global agent_security.cowork_confirm_commands;
|
||||
# True → auto-approve commands (no confirm); False → always confirm.
|
||||
auto_run: Optional[bool] = None
|
||||
# {} = an ordinary local/managed workspace. Non-empty when ``output_dir``
|
||||
# is a LOCAL MIRROR of a OneDrive/SharePoint folder (see
|
||||
# core/cloud_workspace_sync.py) — {"provider": "onedrive"|"sharepoint",
|
||||
# "site_id": "", "site_name": "", "remote_path": ""}. ``output_dir`` itself
|
||||
# always stays a real local path; nothing that reads ``workspace_dir()``
|
||||
# needs to change because of this field.
|
||||
cloud_source: Dict[str, str] = field(default_factory=dict)
|
||||
|
||||
def workspace_dir(self, base: Path = None) -> Path:
|
||||
"""The project's sandbox root. Every chat of the project writes inside
|
||||
|
||||
@@ -48,7 +48,6 @@ from . import skills_dialog as _skills_dialog
|
||||
from . import libreoffice_view as _libreoffice_view
|
||||
from . import agents_admin_tab as _agents_admin_tab
|
||||
from . import monitoring_overview as _monitoring_overview
|
||||
from . import cloud_workspace as _cloud_workspace
|
||||
from . import dialog_buttons as _dialog_buttons
|
||||
from . import connectors as _connectors
|
||||
|
||||
@@ -65,7 +64,6 @@ STRINGS: Dict[str, Dict[str, str]] = {
|
||||
**_libreoffice_view.STRINGS,
|
||||
**_agents_admin_tab.STRINGS,
|
||||
**_monitoring_overview.STRINGS,
|
||||
**_cloud_workspace.STRINGS,
|
||||
**_dialog_buttons.STRINGS,
|
||||
**_connectors.STRINGS,
|
||||
}
|
||||
|
||||
@@ -232,14 +232,6 @@ STRINGS: Dict[str, Dict[str, str]] = {
|
||||
"ja": "行をフィルター(質問を入力しても可)…",
|
||||
"vi": "Lọc dòng (hoặc gõ câu hỏi rồi bấm )…"},
|
||||
"monitoring.ai_filter_btn": {"en": "AI", "ja": "AI", "vi": "AI"},
|
||||
"monitoring.page_size_label": {
|
||||
"en": "Rows/page:", "ja": "1ページの行数:", "vi": "Số dòng/trang:"},
|
||||
"monitoring.page_indicator": {
|
||||
"en": "Page {page}/{total}", "ja": "{page}/{total} ページ", "vi": "Trang {page}/{total}"},
|
||||
"monitoring.page_prev": {
|
||||
"en": "Previous page", "ja": "前のページ", "vi": "Trang trước"},
|
||||
"monitoring.page_next": {
|
||||
"en": "Next page", "ja": "次のページ", "vi": "Trang sau"},
|
||||
"monitoring.pricing_title": {
|
||||
"en": "Model pricing (USD / 1M tokens)", "ja": "モデル価格表 (USD / 100万トークン)",
|
||||
"vi": "Bảng giá model (USD / 1 triệu token)"},
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
"""DF-007 — Microsoft 365 sign-in dialog + cloud (OneDrive/SharePoint)
|
||||
folder picker. Deliberately its own module rather than reusing the
|
||||
similarly-named orphaned keys under ``settings.ms365_*`` in ``cowork_tab.py``/
|
||||
``settings_dialog.py`` — those are leftovers from a MS365 sign-in UI that was
|
||||
removed (see ``ui/settings_dialog.py`` module docstring) and the two files
|
||||
disagree with each other on wording for several duplicate keys, so reusing
|
||||
them risked resurrecting an inconsistency rather than a clean, tested string
|
||||
set."""
|
||||
from __future__ import annotations
|
||||
|
||||
STRINGS = {
|
||||
# ---- ui/ms365_signin_dialog.py ----
|
||||
"ms365_signin.title": {
|
||||
"en": "Sign in to Microsoft 365", "ja": "Microsoft 365 にサインイン",
|
||||
"vi": "Đăng nhập Microsoft 365",
|
||||
},
|
||||
"ms365_signin.already": {
|
||||
"en": "Signed in as {who}.", "ja": "{who} としてサインイン済みです。",
|
||||
"vi": "Đã đăng nhập với {who}.",
|
||||
},
|
||||
"ms365_signin.intro": {
|
||||
"en": "Sign in with your Microsoft work/school (or personal) account to "
|
||||
"browse OneDrive/SharePoint folders.",
|
||||
"ja": "OneDrive/SharePoint のフォルダーを参照するには、Microsoft の職場/学校\n"
|
||||
"(または個人) アカウントでサインインしてください。",
|
||||
"vi": "Đăng nhập bằng tài khoản Microsoft (công ty/trường học hoặc cá nhân) "
|
||||
"để duyệt thư mục OneDrive/SharePoint.",
|
||||
},
|
||||
"ms365_signin.button": {
|
||||
"en": "Sign in", "ja": "サインイン", "vi": "Đăng nhập",
|
||||
},
|
||||
"ms365_signin.signing_in": {
|
||||
"en": "Signing in…", "ja": "サインイン中…", "vi": "Đang đăng nhập…",
|
||||
},
|
||||
"ms365_signin.code_hint": {
|
||||
"en": "Open {url} and enter this code:", "ja": "{url} を開いてこのコードを入力してください:",
|
||||
"vi": "Mở {url} và nhập mã sau:",
|
||||
},
|
||||
"ms365_signin.open_link": {
|
||||
"en": "Open link", "ja": "リンクを開く", "vi": "Mở link",
|
||||
},
|
||||
"ms365_signin.failed": {
|
||||
"en": "Sign-in failed: {err}", "ja": "サインインに失敗しました: {err}",
|
||||
"vi": "Đăng nhập thất bại: {err}",
|
||||
},
|
||||
"ms365_signin.cancel": {
|
||||
"en": "Cancel", "ja": "キャンセル", "vi": "Hủy",
|
||||
},
|
||||
# ---- ui/cloud_folder_picker_dialog.py ----
|
||||
"cloud_picker.title": {
|
||||
"en": "Choose a OneDrive/SharePoint folder", "ja": "OneDrive/SharePoint フォルダーを選択",
|
||||
"vi": "Chọn thư mục OneDrive/SharePoint",
|
||||
},
|
||||
"cloud_picker.source_onedrive": {
|
||||
"en": "My OneDrive", "ja": "自分の OneDrive", "vi": "OneDrive của tôi",
|
||||
},
|
||||
"cloud_picker.source_sharepoint": {
|
||||
"en": "SharePoint site", "ja": "SharePoint サイト", "vi": "Site SharePoint",
|
||||
},
|
||||
"cloud_picker.search_sites_placeholder": {
|
||||
"en": "Search SharePoint sites…", "ja": "SharePoint サイトを検索…",
|
||||
"vi": "Tìm site SharePoint…",
|
||||
},
|
||||
"cloud_picker.search_btn": {
|
||||
"en": "Search", "ja": "検索", "vi": "Tìm",
|
||||
},
|
||||
"cloud_picker.up": {
|
||||
"en": ".. (up)", "ja": ".. (上へ)", "vi": ".. (lùi lại)",
|
||||
},
|
||||
"cloud_picker.choose_here": {
|
||||
"en": "Choose this folder", "ja": "このフォルダーを選択", "vi": "Chọn thư mục này",
|
||||
},
|
||||
"cloud_picker.cancel": {
|
||||
"en": "Cancel", "ja": "キャンセル", "vi": "Hủy",
|
||||
},
|
||||
"cloud_picker.load_failed": {
|
||||
"en": "Could not load this folder: {err}", "ja": "フォルダーを読み込めませんでした: {err}",
|
||||
"vi": "Không tải được thư mục này: {err}",
|
||||
},
|
||||
"cloud_picker.no_sites": {
|
||||
"en": "No matching SharePoint sites.", "ja": "一致する SharePoint サイトがありません。",
|
||||
"vi": "Không tìm thấy site SharePoint phù hợp.",
|
||||
},
|
||||
# ---- ui/workspace_tab.py additions ----
|
||||
"workspace.cloud_pick": {
|
||||
"en": "Choose from OneDrive/SharePoint…", "ja": "OneDrive/SharePoint から選択…",
|
||||
"vi": "Chọn từ OneDrive/SharePoint…",
|
||||
},
|
||||
"workspace.cloud_sync": {
|
||||
"en": "Sync with cloud", "ja": "クラウドと同期", "vi": "Đồng bộ với cloud",
|
||||
},
|
||||
"workspace.cloud_badge_onedrive": {
|
||||
"en": "☁ Local mirror of OneDrive: {path}", "ja": "☁ OneDrive のローカルミラー: {path}",
|
||||
"vi": "☁ Bản sao cục bộ của OneDrive: {path}",
|
||||
},
|
||||
"workspace.cloud_badge_sharepoint": {
|
||||
"en": "☁ Local mirror of SharePoint ({site}): {path}",
|
||||
"ja": "☁ SharePoint ({site}) のローカルミラー: {path}",
|
||||
"vi": "☁ Bản sao cục bộ của SharePoint ({site}): {path}",
|
||||
},
|
||||
"workspace.cloud_sync_result": {
|
||||
"en": "Sync done — {up} uploaded, {down} downloaded.",
|
||||
"ja": "同期完了 — アップロード {up} 件、ダウンロード {down} 件。",
|
||||
"vi": "Đồng bộ xong — {up} tệp đẩy lên, {down} tệp tải về.",
|
||||
},
|
||||
"workspace.cloud_sync_errors": {
|
||||
"en": "{n} item(s) had errors — see details below.",
|
||||
"ja": "{n} 件のエラーがありました — 詳細は下記のとおりです。",
|
||||
"vi": "{n} mục bị lỗi — chi tiết bên dưới.",
|
||||
},
|
||||
"workspace.cloud_sync_skipped": {
|
||||
"en": "{n} file(s) skipped (over 4 MB, not supported yet).",
|
||||
"ja": "{n} 件のファイルはスキップされました (4 MB 超、未対応)。",
|
||||
"vi": "{n} tệp bị bỏ qua (quá 4 MB, chưa hỗ trợ).",
|
||||
},
|
||||
}
|
||||
@@ -344,6 +344,12 @@ STRINGS: Dict[str, Dict[str, str]] = {
|
||||
"en": "Let the control agent review a command with AI before it runs.",
|
||||
"ja": "実行前に制御エージェントがAIでコマンドを確認します。",
|
||||
"vi": "Cho control-agent dùng AI xét lệnh trước khi chạy."},
|
||||
"settings.sandbox_pw_unset_title": {
|
||||
"en": "Sandbox Security", "ja": "サンドボックスセキュリティ", "vi": "Bảo mật Sandbox"},
|
||||
"settings.sandbox_pw_unset_body": {
|
||||
"en": "No sandbox password is set yet, so these settings stay locked. Set COWORK_SANDBOX_PASSWORD, or ask your administrator.",
|
||||
"ja": "サンドボックスのパスワードが未設定のため、この設定はロックされたままです。COWORK_SANDBOX_PASSWORD を設定するか、管理者にお問い合わせください。",
|
||||
"vi": "Chưa đặt mật khẩu sandbox nên nhóm thiết lập này vẫn khóa. Hãy đặt COWORK_SANDBOX_PASSWORD, hoặc liên hệ quản trị viên."},
|
||||
"settings.sandbox_confirm_commands": {
|
||||
"en": "Confirm before Cowork runs a command",
|
||||
"ja": "Cowork がコマンドを実行する前に確認する",
|
||||
|
||||
@@ -177,9 +177,6 @@ STRINGS: Dict[str, Dict[str, str]] = {
|
||||
"vi": "Project cho đoạn chat mới"},
|
||||
"app.nav.no_project": {
|
||||
"en": "No project yet", "ja": "プロジェクトなし", "vi": "Chưa có project"},
|
||||
# KHAC no_project: đã có project, chỉ là người dùng chưa chọn cái nào.
|
||||
"app.nav.pick_project": {
|
||||
"en": "Select a project…", "ja": "プロジェクトを選択…", "vi": "Chọn project…"},
|
||||
"app.nav.recents": {"en": "RECENTS", "ja": "最近", "vi": "GẦN ĐÂY"},
|
||||
"app.nav.all_projects": {
|
||||
"en": "All projects…", "ja": "すべてのプロジェクト…", "vi": "Tất cả project…"},
|
||||
|
||||
@@ -45,6 +45,28 @@ STRINGS: Dict[str, Dict[str, str]] = {
|
||||
"ファイアウォールではありません。上のコマンドホワイトリストと併用してください。",
|
||||
"vi": "Kiểm soát ở tầng chính sách (trỏ biến môi trường proxy vào hố đen) — không phải "
|
||||
"firewall tầng kernel. Kết hợp với whitelist lệnh ở trên để phòng thủ nhiều lớp."},
|
||||
"settings.sandbox_pw_label": {
|
||||
"en": "Sandbox Security Password", "ja": "サンドボックスセキュリティのパスワード",
|
||||
"vi": "Mật khẩu Bảo mật Sandbox"},
|
||||
"settings.sandbox_pw_placeholder": {
|
||||
"en": "Enter password to edit sandbox settings",
|
||||
"ja": "サンドボックス設定を変更するにはパスワードを入力してください",
|
||||
"vi": "Nhập mật khẩu để sửa thiết lập sandbox"},
|
||||
"settings.sandbox_unlock_btn": {"en": "Unlock", "ja": "ロック解除", "vi": "Mở khoá"},
|
||||
"settings.sandbox_locked": {
|
||||
"en": "Locked (changes disabled)", "ja": "ロック中(変更できません)",
|
||||
"vi": "Đang khoá (không sửa được)"},
|
||||
"settings.sandbox_unlocked": {
|
||||
"en": "Unlocked", "ja": "ロック解除済み", "vi": "Đã mở khoá"},
|
||||
"settings.sandbox_unlocked_body": {
|
||||
"en": "Sandbox settings unlocked.", "ja": "サンドボックス設定のロックを解除しました。",
|
||||
"vi": "Đã mở khoá thiết lập sandbox."},
|
||||
"settings.sandbox_pw_wrong_title": {
|
||||
"en": "Wrong Password", "ja": "パスワードが違います", "vi": "Sai mật khẩu"},
|
||||
"settings.sandbox_pw_wrong_body": {
|
||||
"en": "Password incorrect. Sandbox settings remain locked.",
|
||||
"ja": "パスワードが正しくありません。サンドボックス設定はロックされたままです。",
|
||||
"vi": "Mật khẩu không đúng. Thiết lập sandbox vẫn bị khoá."},
|
||||
"settings.sandbox_unlimited": {"en": "Unlimited", "ja": "無制限", "vi": "Không giới hạn"},
|
||||
"settings.sandbox_cpu_label": {"en": "CPU limit", "ja": "CPU 制限", "vi": "Giới hạn CPU"},
|
||||
"settings.sandbox_memory_label": {"en": "Memory limit", "ja": "メモリ制限", "vi": "Giới hạn bộ nhớ"},
|
||||
|
||||
@@ -62,9 +62,7 @@ def run_command(ctx: ToolContext, args: Dict[str, Any],
|
||||
"""
|
||||
from cowork_local.core.deps import network_blocked_env, run_cancellable, sandbox_env
|
||||
from cowork_local.core.sandbox_manager import ExecutionConfig, SandboxManager
|
||||
from cowork_local.security.command_risk_classifier import (
|
||||
classify_command, command_bypasses_network_proxy,
|
||||
)
|
||||
from cowork_local.security.command_risk_classifier import classify_command
|
||||
|
||||
command = str(args.get("command", "")).strip()
|
||||
if not command:
|
||||
@@ -76,21 +74,6 @@ def run_command(ctx: ToolContext, args: Dict[str, Any],
|
||||
denial = "Command blocked by security policy: " + "; ".join(risk.reasons)
|
||||
return {"ok": False, "output": denial}
|
||||
|
||||
# Every sandbox backend's network block is a proxy-env-var trick (see
|
||||
# core/deps.py::network_blocked_env) — it does nothing against a tool
|
||||
# that reaches the network without an HTTP proxy (ping/ICMP, nslookup/
|
||||
# direct DNS, ssh/ftp/raw TCP...). Deny those BY NAME here instead, so
|
||||
# "Chặn mạng cho lệnh do agent chạy" actually blocks them too.
|
||||
if ctx.block_network:
|
||||
bypass_tool = command_bypasses_network_proxy(command)
|
||||
if bypass_tool:
|
||||
return {"ok": False, "output": (
|
||||
f"Command blocked: '{bypass_tool}' can reach the network without going through "
|
||||
"an HTTP proxy, so the sandbox's network block (which only filters proxy-aware "
|
||||
"traffic) cannot stop it by itself — blocked by name instead while "
|
||||
"'Chặn mạng cho lệnh do agent chạy' is on."
|
||||
)}
|
||||
|
||||
# Route through SandboxManager for risk-based isolation
|
||||
mgr = SandboxManager(ExecutionConfig(
|
||||
enabled=True,
|
||||
@@ -128,15 +111,6 @@ def install_package(ctx: ToolContext, args: Dict[str, Any],
|
||||
package = str(args.get("package", "")).strip()
|
||||
if not package:
|
||||
return {"ok": False, "output": "No package specified."}
|
||||
# ``pip install`` bắt buộc phải ra internet, mà ``deps.pip_install`` chạy
|
||||
# subprocess với ``os.environ`` nguyên vẹn — biến proxy hố đen của
|
||||
# ``network_blocked_env`` không chạm tới nó. Từ chối thẳng ở đây (giống cách
|
||||
# run_command chặn theo tên các công cụ không đi qua proxy) thay vì để pip
|
||||
# thử 600 giây rồi báo một lỗi proxy khó hiểu.
|
||||
if ctx.block_network:
|
||||
return {"ok": False, "output": (
|
||||
"install_package: network access is blocked by the Sandbox Security Layer "
|
||||
"(\"Block network for agent-run commands\" is on in Settings).")}
|
||||
python = _sandbox_python(ctx, cancel, on_output)
|
||||
ok, detail = pip_install(package, cancel=cancel, on_output=on_output, python=python)
|
||||
head = f"Installed {package}." if ok else f"Could not install {package}."
|
||||
|
||||
@@ -6,26 +6,11 @@ tag added in R05-T01/domain/tools/tool_registry.py describes.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Any, Dict
|
||||
|
||||
from .tool_context import ToolContext
|
||||
|
||||
|
||||
def _network_refusal(ctx: ToolContext, tool: str) -> Optional[Dict[str, Any]]:
|
||||
"""Lời từ chối khi Sandbox Security Layer đang chặn mạng; None nếu được đi.
|
||||
|
||||
``block_network`` trước đây chỉ được đọc ở ``command_tools.py`` (lệnh shell),
|
||||
nên ba tool mang ``ToolCapability.NETWORK`` ở file này vẫn ra internet bình
|
||||
thường trong khi Monitoring báo "Mạng: Bị chặn". Kiểm ở đây, TRƯỚC mọi lời
|
||||
gọi mạng, để công tắc chặn đúng thứ nó nói là chặn.
|
||||
"""
|
||||
if not ctx.block_network:
|
||||
return None
|
||||
return {"ok": False, "output": (
|
||||
f"{tool}: network access is blocked by the Sandbox Security Layer "
|
||||
"(\"Block network for agent-run commands\" is on in Settings).")}
|
||||
|
||||
|
||||
def fetch_url(ctx: ToolContext, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Fetch a URL's text content (web page / online document / SharePoint-
|
||||
OneDrive share link) via link_fetch — the same parser task-link attachments
|
||||
@@ -35,9 +20,6 @@ def fetch_url(ctx: ToolContext, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return {"ok": False, "output": "fetch_url: 'url' is required."}
|
||||
if not url.lower().startswith(("http://", "https://")):
|
||||
return {"ok": False, "output": f"fetch_url: not an http(s) URL: {url}"}
|
||||
blocked = _network_refusal(ctx, "fetch_url")
|
||||
if blocked is not None:
|
||||
return blocked
|
||||
if not ctx.allow_url_fetch:
|
||||
return {"ok": False,
|
||||
"output": ("fetch_url: URL fetching is turned off in Settings → Security "
|
||||
@@ -55,9 +37,6 @@ def fetch_url(ctx: ToolContext, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
|
||||
def jira_search(ctx: ToolContext, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Tìm issue trên Jira bằng JQL."""
|
||||
blocked = _network_refusal(ctx, "jira_search")
|
||||
if blocked is not None:
|
||||
return blocked
|
||||
from cowork_local.core import jira_tool
|
||||
|
||||
out = jira_tool.search(ctx.jira, str(args.get("jql", "")),
|
||||
@@ -68,9 +47,6 @@ def jira_search(ctx: ToolContext, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
|
||||
def jira_get_issue(ctx: ToolContext, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Lấy chi tiết một issue Jira theo mã."""
|
||||
blocked = _network_refusal(ctx, "jira_get_issue")
|
||||
if blocked is not None:
|
||||
return blocked
|
||||
from cowork_local.core import jira_tool
|
||||
|
||||
out = jira_tool.get_issue(ctx.jira, str(args.get("key", "")))
|
||||
|
||||
@@ -37,16 +37,12 @@ class ToolContext:
|
||||
# None (default) = no limits, matching pre-existing behavior.
|
||||
resource_limits: Optional[Dict[str, float]] = None
|
||||
# Sandbox Security Layer — Settings' "Block network for agent commands"
|
||||
# — the proxy-env block for shell commands (deps.py::network_blocked_env)
|
||||
# AND a flat refusal from every NETWORK-capability tool, which reaches the
|
||||
# net in-process where proxy env vars mean nothing. False (default) =
|
||||
# (policy-level, see deps.py::network_blocked_env). False (default) =
|
||||
# unrestricted, matching pre-existing behavior.
|
||||
block_network: bool = False
|
||||
# Whether the fetch_url tool may read URLs. Its own toggle, but NOT a way
|
||||
# around block_network: with the network blocked every NETWORK-capability
|
||||
# tool is refused first (fetch_tools.py::_network_refusal), so this flag only
|
||||
# decides anything while the network is open. Defaults True; set from
|
||||
# agent_security.allow_url_fetch.
|
||||
# Whether the fetch_url tool may read URLs — SEPARATE from block_network
|
||||
# (reading a web page/share link for info is safe; running networked shell
|
||||
# commands is the risk). Defaults True; set from agent_security.allow_url_fetch.
|
||||
allow_url_fetch: bool = True
|
||||
# Jira read connector config (base_url/email/api_token) — None disables the
|
||||
# jira_* tools' ability to connect. Populated from config.data["jira"].
|
||||
|
||||
@@ -200,14 +200,6 @@ class _NodeItem(QGraphicsObject):
|
||||
e.accept()
|
||||
return
|
||||
super().mousePressEvent(e)
|
||||
if self.isSelected():
|
||||
# itemChange() only emits node_selected when the SELECTION STATE
|
||||
# actually flips (ItemSelectedHasChanged) — clicking a node that
|
||||
# was already selected (e.g. left selected when a run started)
|
||||
# never re-fires it, so the property panel silently kept showing
|
||||
# stale data and looked "locked" while the node ran. Emit
|
||||
# explicitly on every click so the panel always reloads.
|
||||
self.canvas.node_selected.emit(self.node.id)
|
||||
|
||||
def mouseMoveEvent(self, e):
|
||||
"""Rê chuột trong lúc kéo nối: vẽ lại đường nét đứt theo con trỏ."""
|
||||
|
||||
@@ -273,15 +273,6 @@ class Co4ECanvas(_CanvasInteractionMixin, QGraphicsView):
|
||||
item.status = status
|
||||
item.update()
|
||||
|
||||
def node_status(self, node_id: str) -> str:
|
||||
"""Trạng thái chạy hiện tại của một node — "idle" nếu không tìm thấy.
|
||||
|
||||
Dùng để quyết định có khóa bảng thuộc tính bên phải hay không khi
|
||||
người dùng chọn node (xem ``StepConfigPanel.set_locked``).
|
||||
"""
|
||||
item = self._nodes.get(node_id)
|
||||
return item.status if item is not None else "idle"
|
||||
|
||||
def reset_statuses(self) -> None:
|
||||
"""Đưa mọi node về trạng thái chờ — gọi trước mỗi lần chạy lại luồng."""
|
||||
for it in self._nodes.values():
|
||||
|
||||
@@ -101,11 +101,6 @@ class Co4EFlowTabsMixin:
|
||||
self.center_stack.setCurrentIndex(1)
|
||||
self._sync_runs_toggle(False)
|
||||
self._apply_workflow(self._flows[flow_idx])
|
||||
# _apply_workflow() rebuilds the canvas from wf.nodes/edges, which
|
||||
# resets every node's live status to "idle" — without this, coming
|
||||
# back to a flow that's still running (e.g. from the Runs page)
|
||||
# shows every node as idle even though it's actually mid-run.
|
||||
self._reflect_active_run(self._flows[flow_idx].id)
|
||||
def _sync_runs_toggle(self, on: bool) -> None:
|
||||
"""Keep the Runs toggle showing which page is up, however it got there
|
||||
(a double-click in the runs table also switches pages)."""
|
||||
|
||||
@@ -17,7 +17,6 @@ from PySide6.QtWidgets import QMenu, QMessageBox, QTableWidget, QTableWidgetItem
|
||||
from ...core import co4e
|
||||
from ...i18n import tr
|
||||
from ...theme import current_palette
|
||||
from .co4e_workflow_crud import _LOCKED_NODE_STATUSES
|
||||
|
||||
|
||||
class Co4ERunsMixin:
|
||||
@@ -156,14 +155,7 @@ class Co4ERunsMixin:
|
||||
t = ev.get("type")
|
||||
if t == "node_status":
|
||||
if shown:
|
||||
nid = ev.get("node_id")
|
||||
self.canvas.update_node_status(nid, ev.get("status"))
|
||||
# If the panel is showing THIS node right now (e.g. it was
|
||||
# idle and the user had it open when the run started), keep
|
||||
# the lock in sync instead of waiting for the next click.
|
||||
if nid == getattr(self.config, "_node_id", None):
|
||||
self.config.set_locked(
|
||||
self.canvas.node_status(nid) in _LOCKED_NODE_STATUSES)
|
||||
self.canvas.update_node_status(ev.get("node_id"), ev.get("status"))
|
||||
elif t == "node_output":
|
||||
if run_wf is not None:
|
||||
self._outputs_for(run_wf)[ev["node_id"]] = ev.get("output", "")
|
||||
|
||||
@@ -10,14 +10,11 @@ from typing import List, Optional
|
||||
from PySide6.QtCore import QSize, Qt
|
||||
from PySide6.QtWidgets import QMenu
|
||||
from ...core import co4e
|
||||
from ...core.co4e import STEP_RUNNING
|
||||
from ...i18n import tr
|
||||
from ...ui.dialog_buttons import ask_text
|
||||
from ...ui.icons import icon
|
||||
from ...presentation.co4e.co4e_chat_view import _skill_names
|
||||
|
||||
_LOCKED_NODE_STATUSES = (STEP_RUNNING,)
|
||||
|
||||
|
||||
class Co4EWorkflowCrudMixin:
|
||||
"""Phần tạo/mở/lưu/xoá luồng của Co4E Studio.
|
||||
@@ -167,15 +164,10 @@ class Co4EWorkflowCrudMixin:
|
||||
self.canvas.add_palette_step(co4e.Step(label="New Step"),
|
||||
self.canvas.mapToScene(self.canvas.rect().center()))
|
||||
def _on_node_selected(self, node_id: str) -> None:
|
||||
"""Chọn một node thì nạp bước đó vào bảng thuộc tính, tự mở bảng nếu đang gập.
|
||||
|
||||
Chỉ khoá ô nhập liệu khi bước ĐANG chạy (DF-002) — chạy xong rồi thì
|
||||
vẫn sửa lại được bình thường.
|
||||
"""
|
||||
"""Chọn một node thì nạp bước đó vào bảng thuộc tính, tự mở bảng nếu đang gập."""
|
||||
for n in self.canvas.nodes():
|
||||
if n.id == node_id:
|
||||
self.config.load_step(node_id, n.data, _skill_names())
|
||||
self.config.set_locked(self.canvas.node_status(node_id) in _LOCKED_NODE_STATUSES)
|
||||
if self._config_collapsed:
|
||||
self._toggle_config()
|
||||
return
|
||||
|
||||
@@ -38,7 +38,7 @@ from PySide6.QtWidgets import (
|
||||
)
|
||||
|
||||
from ...config import PROVIDER_LABELS
|
||||
from ...core.co4e import PERMISSION_PRESETS, STEP_DONE, STEP_RUNNING, Step
|
||||
from ...core.co4e import PERMISSION_PRESETS, Step
|
||||
from ...i18n import bind_items, bind_placeholder, bind_text, bind_tip, tr
|
||||
from ...ui.icons import icon, icon_picker_combo
|
||||
from .node_property_actions_mixin import _StepConfigActionsMixin
|
||||
@@ -65,8 +65,6 @@ class StepConfigPanel(_StepConfigActionsMixin, QScrollArea):
|
||||
self._step: Optional[Step] = None
|
||||
self._node_id = ""
|
||||
self._loading = False
|
||||
self._ctx_available = ctx is not None
|
||||
self._locked = False
|
||||
self.setWidgetResizable(True)
|
||||
host = QWidget()
|
||||
self.setWidget(host)
|
||||
@@ -291,27 +289,6 @@ class StepConfigPanel(_StepConfigActionsMixin, QScrollArea):
|
||||
self.sub_list.addItem(sub.agent)
|
||||
self._loading = False
|
||||
|
||||
def set_locked(self, locked: bool) -> None:
|
||||
"""Khoá/mở khoá các trường chỉnh sửa theo trạng thái chạy của bước.
|
||||
|
||||
Chỉ khoá khi bước ĐANG chạy — tránh sửa nhầm cấu hình trong lúc chưa
|
||||
biết kết quả (DF-002: trước đây còn khoá cả bước đã chạy xong, khiến
|
||||
không sửa lại được sau khi run xong). Nút Chạy/Chạy từ đây/Xoá bước
|
||||
vẫn hoạt động bình thường khi khoá — chỉ ô nhập liệu bị khoá, không
|
||||
phải cả panel.
|
||||
"""
|
||||
self._locked = locked
|
||||
editable = not locked
|
||||
for w in (self.label_edit, self.role_edit, self.icon_edit,
|
||||
self.instructions_edit, self.context_edit,
|
||||
self.model_combo, self.perm_combo, self.verify_chk,
|
||||
self.rounds_spin, self.skills_list,
|
||||
self.attach_add_btn, self.attach_del_btn,
|
||||
self.sub_add_btn, self.sub_del_btn, self.sub_list):
|
||||
w.setEnabled(editable)
|
||||
self.gen_btn.setEnabled(editable and self._ctx_available)
|
||||
self.load_models_btn.setEnabled(editable and self._ctx_available)
|
||||
|
||||
def clear_step(self) -> None:
|
||||
"""Xoá bảng khi không có bước nào được chọn."""
|
||||
self._step = None
|
||||
|
||||
@@ -21,9 +21,9 @@ from __future__ import annotations
|
||||
from typing import List, Optional
|
||||
|
||||
from PySide6.QtWidgets import (
|
||||
QComboBox, QHBoxLayout, QLabel, QPlainTextEdit, QPushButton, QVBoxLayout, QWidget,
|
||||
QComboBox, QHBoxLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget,
|
||||
)
|
||||
from PySide6.QtCore import Qt, Signal
|
||||
from PySide6.QtCore import Signal
|
||||
|
||||
from cowork_local.i18n import on_language_changed, tr
|
||||
from cowork_local.presentation.folder.ai_edit_model_resolver import AiEditModelResolver
|
||||
@@ -32,45 +32,6 @@ from cowork_local.theme import current_palette
|
||||
from cowork_local.ui.chat_view import ChatView
|
||||
|
||||
|
||||
class _AutoExpandInput(QPlainTextEdit):
|
||||
"""Instruction box: grows with content (1..~6 lines, then scrolls), Enter
|
||||
submits, Shift+Enter inserts a newline — same convention as the Cowork
|
||||
composer (``presentation/chat/chat_input_box.py::_Input``), minus its
|
||||
``/skill``/``/agent`` popups and drag-drop attachment handling, which
|
||||
don't apply to a single AI-edit instruction. DF-008: a fixed-height
|
||||
single-line ``QLineEdit`` read as cramped for a full instruction; this
|
||||
replaces it instead of just nudging the height up further."""
|
||||
|
||||
submit = Signal()
|
||||
|
||||
MIN_HEIGHT = 36 # matches the old QLineEdit's bumped-up height
|
||||
MAX_HEIGHT = 140 # ~6 lines, then it scrolls instead of growing further
|
||||
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setTabChangesFocus(True) # Tab moves focus, doesn't insert a tab
|
||||
self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
|
||||
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
||||
self.textChanged.connect(self._adjust_height)
|
||||
self._adjust_height()
|
||||
|
||||
def _adjust_height(self) -> None:
|
||||
# QPlainTextEdit reports the document height in LINES, not pixels —
|
||||
# convert via line spacing (same approach as chat_input_box.py).
|
||||
lines = self.document().size().height() or 1
|
||||
line_px = self.fontMetrics().lineSpacing()
|
||||
h = int(lines * line_px + 2 * self.frameWidth() + 12)
|
||||
h = max(self.MIN_HEIGHT, min(self.MAX_HEIGHT, h))
|
||||
if h != self.height():
|
||||
self.setFixedHeight(h)
|
||||
|
||||
def keyPressEvent(self, e) -> None: # noqa: N802
|
||||
if e.key() in (Qt.Key_Return, Qt.Key_Enter) and not (e.modifiers() & Qt.ShiftModifier):
|
||||
self.submit.emit()
|
||||
return
|
||||
super().keyPressEvent(e)
|
||||
|
||||
|
||||
class AiFileEditorDialog(QWidget):
|
||||
"""Collapsible panel: a Cowork-style inline chat timeline, this panel's
|
||||
OWN model picker + routing toggle, an instruction box, and an Apply/
|
||||
@@ -133,9 +94,9 @@ class AiFileEditorDialog(QWidget):
|
||||
ctx, self.ai_model_combo, self.ai_chat.add_status, self._confirm_routing_switch)
|
||||
|
||||
row = QHBoxLayout()
|
||||
self.ai_input = _AutoExpandInput()
|
||||
self.ai_input = QLineEdit()
|
||||
self.ai_input.setPlaceholderText(tr("folder.ai_placeholder"))
|
||||
self.ai_input.submit.connect(self._ai_send)
|
||||
self.ai_input.returnPressed.connect(self._ai_send)
|
||||
row.addWidget(self.ai_input, 1)
|
||||
self.ai_send_btn = QPushButton(tr("folder.ai_send"))
|
||||
self.ai_send_btn.setObjectName("primary")
|
||||
@@ -209,7 +170,7 @@ class AiFileEditorDialog(QWidget):
|
||||
if not self.preview.root:
|
||||
self.ai_chat.add_error(tr("folder.ai_no_file"))
|
||||
return
|
||||
instruction = self.ai_input.toPlainText().strip()
|
||||
instruction = self.ai_input.text().strip()
|
||||
if not instruction:
|
||||
return
|
||||
self.ai_input.clear()
|
||||
|
||||
@@ -219,12 +219,6 @@ class StructureGraphView(QWidget):
|
||||
"""Dựng sẵn khung đồ thị trước khi người dùng bấm vào, để lần mở đầu không giật."""
|
||||
self.renderer.prewarm()
|
||||
|
||||
def refresh_project_list(self) -> None:
|
||||
"""Project khác vừa được tạo/sửa/xoá/đổi tên — làm mới danh sách trong
|
||||
bộ chọn project của renderer (bộ chọn KHÔNG tự nạp lại khi project
|
||||
thay đổi ở màn khác, chỉ khi ``set_project`` được gọi)."""
|
||||
self.renderer._refresh_project_combo()
|
||||
|
||||
def hideEvent(self, e): # noqa: N802
|
||||
# Leaving the GraphRAG tab → drop the temporary extracted info.
|
||||
"""Rời màn GraphRAG thì xoá phần trích xuất tạm của khung hỏi-đáp."""
|
||||
|
||||
@@ -10,7 +10,6 @@ the ``status_message`` signal, ``select_subtab(index)``, ``nav_subtabs()``,
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, timedelta
|
||||
from typing import List
|
||||
|
||||
from PySide6.QtCore import QTimer, Signal
|
||||
@@ -26,25 +25,11 @@ from .tabs.overview_tab import OverviewTab
|
||||
from .tabs.security_events_tab import SecurityEventsTab
|
||||
|
||||
_REFRESH_MS = 3000
|
||||
# Comfortably larger than any realistic audit-log size for the WINDOW of
|
||||
# events _load_events() now actually reads (see _LOG_WINDOW_DAYS below) — this
|
||||
# is MonitoringQueryService's query-side page size, kept unbounded so it
|
||||
# always returns every matching event within the window; the user-facing
|
||||
# "Số dòng/trang" control (DF-006 — see shared/event_table.py::set_page_size,
|
||||
# shared/filter_scaffold.py::build_filter_scaffold's with_page_size) trims
|
||||
# that down for DISPLAY, client-side, per event tab.
|
||||
# Comfortably larger than any realistic audit-log size — the event tables
|
||||
# have never had pagination controls, so every tab still shows "all matching
|
||||
# events" exactly like before; MonitoringQueryService's pagination support
|
||||
# is exercised for real here, just not surfaced as UI (yet).
|
||||
_UNBOUNDED_PAGE_SIZE = 100_000
|
||||
# _load_events() re-reads the audit log from disk every _REFRESH_MS (3s) via
|
||||
# _auto_refresh(), and audit_log.load_events()/load_shared_audit_events() are
|
||||
# day-sharded JSONL — unbounded start/end means EVERY day file ever written
|
||||
# gets re-read and re-parsed on EVERY tick, which is what actually made
|
||||
# Monitoring "gây nặng khi log lớn" (see DF-006): the slowness was never in
|
||||
# rendering (EventTable paginates client-side, 5-100 rows/page — see
|
||||
# shared/event_table.py::_DEFAULT_PAGE_SIZE), it was this repeated full-history
|
||||
# read.
|
||||
# 30 days is a live-monitoring window, not a hard retention limit — nothing
|
||||
# is deleted, older days are simply not re-read on every 3s tick.
|
||||
_LOG_WINDOW_DAYS = 30
|
||||
|
||||
|
||||
class MonitoringTab(QWidget):
|
||||
@@ -274,20 +259,14 @@ class MonitoringTab(QWidget):
|
||||
|
||||
Có cấu hình thư mục chia sẻ VÀ đọc ra được dữ liệu thì dùng nó, để cả đội
|
||||
nhìn chung một bức tranh; rỗng thì rơi về nhật ký của máy này.
|
||||
|
||||
Chỉ đọc ``_LOG_WINDOW_DAYS`` ngày gần nhất — cả hai nguồn đều lưu theo
|
||||
file JSONL từng ngày, nên bounding ở đây tránh việc đọc lại TOÀN BỘ
|
||||
lịch sử mỗi 3 giây (xem ``_auto_refresh``), là nguyên nhân thật của
|
||||
DF-006 (gây nặng khi log lớn).
|
||||
"""
|
||||
start = date.today() - timedelta(days=_LOG_WINDOW_DAYS)
|
||||
shared_dir = self.ctx.config.shared_dir
|
||||
if shared_dir:
|
||||
from ...core import telemetry_shared
|
||||
shared_events = telemetry_shared.load_shared_audit_events(shared_dir, start=start)
|
||||
shared_events = telemetry_shared.load_shared_audit_events(shared_dir)
|
||||
if shared_events:
|
||||
return shared_events
|
||||
return audit_log.load_events(start=start)
|
||||
return audit_log.load_events()
|
||||
|
||||
def _apply_events_to_event_tabs(self, events: List[dict]) -> None:
|
||||
"""Filters the ALREADY-LOADED event list (see ``_load_events`` — one
|
||||
|
||||
@@ -6,7 +6,7 @@ from __future__ import annotations
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from PySide6.QtCore import QEvent, QObject, QRect, QSize, Qt, Signal
|
||||
from PySide6.QtCore import QEvent, QObject, QRect, QSize, Qt
|
||||
from PySide6.QtGui import QBrush, QColor
|
||||
from PySide6.QtWidgets import QHeaderView, QTableWidget, QTableWidgetItem, QWidget
|
||||
|
||||
@@ -17,8 +17,7 @@ from ....ui.icons import DOT_GREEN, DOT_RED, icon
|
||||
from .badges import action_label
|
||||
from .formatters import agent_avatar_icon, fmt_event_time
|
||||
|
||||
_DEFAULT_PAGE_SIZE = 20
|
||||
PAGE_SIZE_OPTIONS = (5, 10, 20, 50, 100)
|
||||
_MAX_ROWS = 300
|
||||
|
||||
|
||||
class _TimeItem(QTableWidgetItem):
|
||||
@@ -63,12 +62,6 @@ class EventTable(QTableWidget):
|
||||
"secret_in_output": "warning",
|
||||
}
|
||||
|
||||
# Emitted whenever the rendered page changes (new data, page-size change,
|
||||
# or prev/next navigation) — args are (current_page, page_count), both
|
||||
# 1-based-friendly in that current_page is 0-indexed but page_count is a
|
||||
# plain count. filter_scaffold.py's pager label/buttons listen to this.
|
||||
page_changed = Signal(int, int)
|
||||
|
||||
def __init__(self, show_result: bool = True):
|
||||
# Security Events drops the result column entirely (see _ACTION_TINTS).
|
||||
"""Bảng sự kiện dùng chung của các tab Giám sát.
|
||||
@@ -77,10 +70,6 @@ class EventTable(QTableWidget):
|
||||
là thất bại nên cột ấy chỉ tốn chỗ.
|
||||
"""
|
||||
self._show_result = show_result
|
||||
self._page_size = _DEFAULT_PAGE_SIZE
|
||||
self._current_page = 0
|
||||
self._last_events: List[dict] = []
|
||||
self._sorted_events: List[dict] = []
|
||||
super().__init__(0, 7 if show_result else 6)
|
||||
self.setEditTriggers(QTableWidget.NoEditTriggers)
|
||||
self.setSelectionBehavior(QTableWidget.SelectRows)
|
||||
@@ -109,60 +98,13 @@ class EventTable(QTableWidget):
|
||||
cols += [tr("monitoring.col_detail_block") if not self._show_result else tr("monitoring.col_detail")]
|
||||
self.setHorizontalHeaderLabels(cols)
|
||||
|
||||
def page_size(self) -> int:
|
||||
"""Số dòng đang hiển thị mỗi trang."""
|
||||
return self._page_size
|
||||
|
||||
def page_count(self) -> int:
|
||||
"""Tổng số trang với dữ liệu và số dòng/trang hiện tại (tối thiểu 1)."""
|
||||
if not self._sorted_events:
|
||||
return 1
|
||||
return -(-len(self._sorted_events) // self._page_size) # ceil div
|
||||
|
||||
def current_page(self) -> int:
|
||||
"""Trang đang hiển thị, đánh số từ 0."""
|
||||
return self._current_page
|
||||
|
||||
def go_to_page(self, page: int) -> None:
|
||||
"""Nhảy tới một trang cụ thể (đánh số từ 0), tự kẹp trong khoảng hợp lệ."""
|
||||
self._current_page = page
|
||||
self._render_current_page()
|
||||
|
||||
def next_page(self) -> None:
|
||||
"""Sang trang kế — không làm gì nếu đã ở trang cuối."""
|
||||
self.go_to_page(self._current_page + 1)
|
||||
|
||||
def prev_page(self) -> None:
|
||||
"""Về trang trước — không làm gì nếu đã ở trang đầu."""
|
||||
self.go_to_page(self._current_page - 1)
|
||||
|
||||
def set_page_size(self, n: int) -> None:
|
||||
"""Đổi số dòng hiển thị mỗi trang, quay về trang đầu, rồi vẽ lại với dữ
|
||||
liệu đã có sẵn (không cần refresh lại từ nguồn)."""
|
||||
self._page_size = n
|
||||
self._current_page = 0
|
||||
self._render_current_page()
|
||||
|
||||
def set_events(self, events: List[dict]) -> None:
|
||||
"""Đổ danh sách sự kiện vào bảng: mới nhất lên đầu, chia trang theo
|
||||
``self._page_size`` — xem qua trang khác bằng ``next_page``/``prev_page``
|
||||
(nút tiến/lùi ở filter_scaffold.py), không còn bị cắt bỏ vĩnh viễn như
|
||||
trước (DF-006)."""
|
||||
self._last_events = events
|
||||
self._sorted_events = sorted(events, key=lambda e: e.get("ts", ""), reverse=True)
|
||||
self._current_page = 0
|
||||
self._render_current_page()
|
||||
|
||||
def _render_current_page(self) -> None:
|
||||
"""Vẽ đúng một trang (theo ``self._current_page``/``self._page_size``)
|
||||
từ ``self._sorted_events`` đã sắp sẵn.
|
||||
"""Đổ danh sách sự kiện vào bảng: mới nhất lên đầu, cắt ở ``_MAX_ROWS``.
|
||||
|
||||
Tắt sắp xếp trong lúc đổ dữ liệu — để bật, Qt sắp lại sau mỗi dòng và việc
|
||||
nạp chậm đi theo bậc hai.
|
||||
"""
|
||||
self._current_page = max(0, min(self._current_page, self.page_count() - 1))
|
||||
start = self._current_page * self._page_size
|
||||
events = self._sorted_events[start:start + self._page_size]
|
||||
events = sorted(events, key=lambda e: e.get("ts", ""), reverse=True)[:_MAX_ROWS]
|
||||
self.setSortingEnabled(False)
|
||||
self.setRowCount(len(events))
|
||||
for row, ev in enumerate(events):
|
||||
@@ -207,7 +149,6 @@ class EventTable(QTableWidget):
|
||||
self.setItem(row, col, item)
|
||||
self.setSortingEnabled(True)
|
||||
self.apply_filter(getattr(self, "_filter_needle", ""))
|
||||
self.page_changed.emit(self._current_page, self.page_count())
|
||||
|
||||
def apply_filter(self, needle: str) -> None:
|
||||
"""Ẩn/hiện dòng theo từ khoá tìm kiếm (không phân biệt hoa thường)."""
|
||||
|
||||
@@ -16,13 +16,13 @@ from typing import Callable, Dict, Optional
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtGui import QKeySequence, QShortcut
|
||||
from PySide6.QtWidgets import (
|
||||
QApplication, QComboBox, QHBoxLayout, QLabel, QLineEdit, QPushButton,
|
||||
QSplitter, QTableWidget, QVBoxLayout, QWidget,
|
||||
QApplication, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSplitter,
|
||||
QTableWidget, QVBoxLayout, QWidget,
|
||||
)
|
||||
|
||||
from ....i18n import bind_tip, tr
|
||||
from ....ui.icons import icon
|
||||
from .event_table import PAGE_SIZE_OPTIONS, ClickOutsideCloser, EventTable
|
||||
from .event_table import ClickOutsideCloser, EventTable
|
||||
from .event_detail_panel import EventDetailPanel
|
||||
|
||||
|
||||
@@ -47,12 +47,11 @@ def _sync_event_detail(table: EventTable, panel: EventDetailPanel) -> None:
|
||||
def build_filter_scaffold(
|
||||
page: QWidget, table: QTableWidget, *, on_refresh: Callable[[], None],
|
||||
title_key: Optional[str] = None, with_search: bool = True,
|
||||
with_detail: bool = False, with_page_size: bool = False,
|
||||
with_detail: bool = False,
|
||||
on_ai_filter: Optional[Callable[[QLineEdit, QPushButton], None]] = None,
|
||||
) -> Dict[str, object]:
|
||||
"""Dựng khung chung cho một tab sự kiện: tiêu đề, nút làm mới, ô tìm kiếm,
|
||||
nút lọc bằng AI, control "Số dòng/trang" (nếu ``with_page_size``) và panel
|
||||
chi tiết.
|
||||
nút lọc bằng AI và panel chi tiết.
|
||||
|
||||
Bốn tab sự kiện của màn Giám sát chỉ khác nhau ở nguồn dữ liệu, nên phần vỏ
|
||||
này được dựng một lần và dùng chung.
|
||||
@@ -92,55 +91,6 @@ def build_filter_scaffold(
|
||||
ai_btn.clicked.connect(lambda: on_ai_filter(search, ai_btn))
|
||||
row.addWidget(search, 1)
|
||||
row.addWidget(ai_btn)
|
||||
if with_page_size and isinstance(table, EventTable):
|
||||
# DF-006: the item-per-page count was never surfaced anywhere in
|
||||
# the UI (design called for it) — EventTable already trims to a
|
||||
# page size internally, this just makes that number visible AND
|
||||
# user-choosable instead of a fixed constant.
|
||||
page_size_lbl = QLabel(tr("monitoring.page_size_label"))
|
||||
page_size_combo = QComboBox()
|
||||
for n in PAGE_SIZE_OPTIONS:
|
||||
page_size_combo.addItem(str(n), n)
|
||||
current = table.page_size()
|
||||
page_size_combo.setCurrentIndex(
|
||||
PAGE_SIZE_OPTIONS.index(current) if current in PAGE_SIZE_OPTIONS else 0)
|
||||
page_size_combo.currentIndexChanged.connect(
|
||||
lambda i: table.set_page_size(page_size_combo.itemData(i)))
|
||||
row.addWidget(page_size_lbl)
|
||||
row.addWidget(page_size_combo)
|
||||
|
||||
# DF-006 follow-up: trimming to a page size alone silently dropped
|
||||
# every row past it with no way back to see them — prev/next
|
||||
# buttons plus a "trang X/Y" indicator make the rest reachable.
|
||||
page_prev_btn = QPushButton()
|
||||
page_prev_btn.setIcon(icon("chevron-left"))
|
||||
page_prev_btn.setCursor(Qt.PointingHandCursor)
|
||||
bind_tip(page_prev_btn, "monitoring.page_prev")
|
||||
page_next_btn = QPushButton()
|
||||
page_next_btn.setIcon(icon("chevron-right"))
|
||||
page_next_btn.setCursor(Qt.PointingHandCursor)
|
||||
bind_tip(page_next_btn, "monitoring.page_next")
|
||||
page_indicator_lbl = QLabel()
|
||||
|
||||
def _refresh_pager(cur: int = None, total: int = None) -> None:
|
||||
if cur is None or total is None:
|
||||
cur, total = table.current_page(), table.page_count()
|
||||
page_indicator_lbl.setText(tr("monitoring.page_indicator", page=cur + 1, total=total))
|
||||
page_prev_btn.setEnabled(cur > 0)
|
||||
page_next_btn.setEnabled(cur < total - 1)
|
||||
|
||||
page_prev_btn.clicked.connect(table.prev_page)
|
||||
page_next_btn.clicked.connect(table.next_page)
|
||||
table.page_changed.connect(_refresh_pager)
|
||||
_refresh_pager()
|
||||
|
||||
row.addWidget(page_prev_btn)
|
||||
row.addWidget(page_indicator_lbl)
|
||||
row.addWidget(page_next_btn)
|
||||
parts.update(
|
||||
page_size_label=page_size_lbl, page_size_combo=page_size_combo,
|
||||
page_prev_btn=page_prev_btn, page_next_btn=page_next_btn,
|
||||
page_indicator_label=page_indicator_lbl, page_pager_refresh=_refresh_pager)
|
||||
lay.addLayout(row)
|
||||
parts.update(filter_edit=search, ai_filter_btn=ai_btn)
|
||||
|
||||
|
||||
@@ -25,16 +25,13 @@ class ActionLogsTab(QWidget):
|
||||
parts = build_filter_scaffold(
|
||||
self, self.table, on_refresh=on_refresh_all,
|
||||
title_key="monitoring.action_logs_title",
|
||||
with_search=True, with_detail=True, with_page_size=True,
|
||||
on_ai_filter=self._start_ai_filter)
|
||||
with_search=True, with_detail=True, on_ai_filter=self._start_ai_filter)
|
||||
self.title_lbl = parts["title_lbl"]
|
||||
self.title_key = parts["title_key"]
|
||||
self.title_refresh_btn = parts["title_refresh_btn"]
|
||||
self.filter_edit = parts["filter_edit"]
|
||||
self.ai_filter_btn = parts["ai_filter_btn"]
|
||||
self.detail_panel = parts["detail_panel"]
|
||||
self.page_size_label = parts["page_size_label"]
|
||||
self.page_pager_refresh = parts["page_pager_refresh"]
|
||||
|
||||
def set_events(self, events: List[dict]) -> None:
|
||||
"""Đổ danh sách sự kiện vào bảng."""
|
||||
@@ -47,8 +44,6 @@ class ActionLogsTab(QWidget):
|
||||
self.detail_panel.retranslate()
|
||||
self.title_lbl.setText(tr(self.title_key))
|
||||
self.title_refresh_btn.setText(tr("monitoring.refresh"))
|
||||
self.page_size_label.setText(tr("monitoring.page_size_label"))
|
||||
self.page_pager_refresh()
|
||||
|
||||
def _start_ai_filter(self, search: QLineEdit, ai_btn: QPushButton) -> None:
|
||||
"""Nhờ AI dịch câu tìm kiếm tự nhiên thành từ khoá lọc."""
|
||||
|
||||
@@ -25,16 +25,13 @@ class McpTab(QWidget):
|
||||
parts = build_filter_scaffold(
|
||||
self, self.table, on_refresh=on_refresh_all,
|
||||
title_key="monitoring.mcp_history_title",
|
||||
with_search=True, with_detail=True, with_page_size=True,
|
||||
on_ai_filter=self._start_ai_filter)
|
||||
with_search=True, with_detail=True, on_ai_filter=self._start_ai_filter)
|
||||
self.title_lbl = parts["title_lbl"]
|
||||
self.title_key = parts["title_key"]
|
||||
self.title_refresh_btn = parts["title_refresh_btn"]
|
||||
self.filter_edit = parts["filter_edit"]
|
||||
self.ai_filter_btn = parts["ai_filter_btn"]
|
||||
self.detail_panel = parts["detail_panel"]
|
||||
self.page_size_label = parts["page_size_label"]
|
||||
self.page_pager_refresh = parts["page_pager_refresh"]
|
||||
|
||||
def set_events(self, events: List[dict]) -> None:
|
||||
"""Đổ danh sách sự kiện vào bảng."""
|
||||
@@ -47,8 +44,6 @@ class McpTab(QWidget):
|
||||
self.detail_panel.retranslate()
|
||||
self.title_lbl.setText(tr(self.title_key))
|
||||
self.title_refresh_btn.setText(tr("monitoring.refresh"))
|
||||
self.page_size_label.setText(tr("monitoring.page_size_label"))
|
||||
self.page_pager_refresh()
|
||||
|
||||
def _start_ai_filter(self, search: QLineEdit, ai_btn: QPushButton) -> None:
|
||||
"""Nhờ AI dịch câu tìm kiếm tự nhiên thành từ khoá lọc."""
|
||||
|
||||
@@ -31,16 +31,13 @@ class SecurityEventsTab(QWidget):
|
||||
parts = build_filter_scaffold(
|
||||
self, self.table, on_refresh=on_refresh_all,
|
||||
title_key="monitoring.security_events_title",
|
||||
with_search=True, with_detail=True, with_page_size=True,
|
||||
on_ai_filter=self._start_ai_filter)
|
||||
with_search=True, with_detail=True, on_ai_filter=self._start_ai_filter)
|
||||
self.title_lbl = parts["title_lbl"]
|
||||
self.title_key = parts["title_key"]
|
||||
self.title_refresh_btn = parts["title_refresh_btn"]
|
||||
self.filter_edit = parts["filter_edit"]
|
||||
self.ai_filter_btn = parts["ai_filter_btn"]
|
||||
self.detail_panel = parts["detail_panel"]
|
||||
self.page_size_label = parts["page_size_label"]
|
||||
self.page_pager_refresh = parts["page_pager_refresh"]
|
||||
|
||||
def set_events(self, events: List[dict]) -> None:
|
||||
"""Đổ danh sách sự kiện vào bảng."""
|
||||
@@ -53,8 +50,6 @@ class SecurityEventsTab(QWidget):
|
||||
self.detail_panel.retranslate()
|
||||
self.title_lbl.setText(tr(self.title_key))
|
||||
self.title_refresh_btn.setText(tr("monitoring.refresh"))
|
||||
self.page_size_label.setText(tr("monitoring.page_size_label"))
|
||||
self.page_pager_refresh()
|
||||
|
||||
def _start_ai_filter(self, search: QLineEdit, ai_btn: QPushButton) -> None:
|
||||
"""Nhờ AI dịch câu tìm kiếm tự nhiên thành từ khoá lọc."""
|
||||
|
||||
@@ -218,11 +218,6 @@ class MainWindow(NavRailMixin, RailProjectMixin, TopBarMixin,
|
||||
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
|
||||
|
||||
@@ -81,7 +81,7 @@ class NavRailMixin:
|
||||
# 16px icon up with the nav items' icons below (1px list frame + item
|
||||
# padding) — same indent level, same icon size as e.g. Dashboard.
|
||||
toggle_row = QHBoxLayout()
|
||||
toggle_row.setContentsMargins(10, 8, 10, 8)
|
||||
toggle_row.setContentsMargins(0, 8, 10, 8)
|
||||
toggle_row.addWidget(self._nav_toggle_btn, 0, Qt.AlignLeft)
|
||||
toggle_row.addStretch(1)
|
||||
nvl.addLayout(toggle_row)
|
||||
@@ -111,7 +111,7 @@ class NavRailMixin:
|
||||
self.nav_project_btn.menu().aboutToShow.connect(self._fill_rail_project_menu)
|
||||
self.nav_project_btn.setVisible(False)
|
||||
head = QVBoxLayout()
|
||||
head.setContentsMargins(10, 0, 10, 6)
|
||||
head.setContentsMargins(6, 0, 6, 6)
|
||||
head.setSpacing(6)
|
||||
head.addWidget(self.nav_project)
|
||||
head.addWidget(self.nav_project_btn)
|
||||
@@ -136,7 +136,7 @@ class NavRailMixin:
|
||||
self._nav_scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
|
||||
scroll_body = QWidget()
|
||||
sv = QVBoxLayout(scroll_body)
|
||||
sv.setContentsMargins(6, 0, 6, 0)
|
||||
sv.setContentsMargins(0, 0, 0, 0)
|
||||
sv.setSpacing(0)
|
||||
sv.addWidget(self.nav, 0)
|
||||
# RECENTS — the threads of the project named in the picker above, right
|
||||
@@ -197,9 +197,9 @@ class NavRailMixin:
|
||||
def _nav_rows(self):
|
||||
"""(tree, page, sub, label, icon, enabled) for every row, rail order.
|
||||
|
||||
Workspace contributes all five of its sub-views; ``_rebuild_nav`` bỏ
|
||||
những hàng mà cổng project đang đóng (Cowork, GraphRAG) thay vì hiện
|
||||
chúng ở dạng mờ.
|
||||
Workspace contributes all five of its sub-views — including the two the
|
||||
project gate currently disables — so the rail never changes shape while
|
||||
the user is looking at it.
|
||||
"""
|
||||
rows = [(self.nav, self._ROW_WORKSPACE, sub, label, ic, on)
|
||||
for label, sub, ic, on in self.workspace.nav_entries()]
|
||||
@@ -238,13 +238,15 @@ class NavRailMixin:
|
||||
tree.clear()
|
||||
tree.blockSignals(blocked)
|
||||
for tree, page, sub, label, icon_name, enabled in spec:
|
||||
if not enabled:
|
||||
# Cổng project đóng → bỏ hẳn hàng, không hiện dạng mờ nữa.
|
||||
continue
|
||||
it = QTreeWidgetItem([""] if self._nav_collapsed else [label])
|
||||
it.setIcon(0, _icon(icon_name))
|
||||
it.setData(0, Qt.UserRole, {"page": page, "sub": sub})
|
||||
if self._nav_collapsed:
|
||||
if not enabled:
|
||||
# Same gate as before, shown instead of hidden: the row stays
|
||||
# in place, greyed, and says why it cannot be opened.
|
||||
it.setDisabled(True)
|
||||
it.setToolTip(0, tr("app.nav.needs_project"))
|
||||
elif self._nav_collapsed:
|
||||
it.setToolTip(0, label)
|
||||
blocked = tree.blockSignals(True)
|
||||
tree.addTopLevelItem(it)
|
||||
|
||||
@@ -13,7 +13,7 @@ from PySide6.QtWidgets import QStyledItemDelegate
|
||||
# ---- kích thước ---------------------------------------------------------
|
||||
_NAV_EXPANDED_WIDTH = 232
|
||||
_NAV_COLLAPSED_WIDTH = 54
|
||||
_NAV_ROW_INSET = 8
|
||||
_NAV_ROW_INSET = 4
|
||||
_NAV_ROW_GAP = 6
|
||||
# Khe TRÊN nút Cài đặt, tính bằng khoảng trống thật trong layout của rail.
|
||||
# Không đặt bằng ``margin`` trong QSS: margin của stylesheet được vẽ BÊN TRONG
|
||||
|
||||
@@ -42,12 +42,6 @@ class RailProjectMixin:
|
||||
# No project yet: say so, and say what to do about it, instead of
|
||||
# leaving an empty box and a button that silently does nothing.
|
||||
self.nav_project.addItem(tr("app.nav.no_project"), "")
|
||||
elif not current:
|
||||
# Có project nhưng CHƯA chọn cái nào (mở app lên, hoặc vừa xoá
|
||||
# project đang mở). Không có mục này thì combo rơi về mục 0 và
|
||||
# chỉ bừa vào project đầu danh sách, trong khi cổng
|
||||
# Cowork/GraphRAG vẫn đóng — hai chỗ nói hai đằng.
|
||||
self.nav_project.insertItem(0, tr("app.nav.pick_project"), "")
|
||||
idx = self.nav_project.findData(current)
|
||||
if idx >= 0:
|
||||
self.nav_project.setCurrentIndex(idx)
|
||||
|
||||
@@ -106,4 +106,4 @@ class SessionEventsMixin:
|
||||
"""Project được tạo/sửa/xoá: gom nhóm lại cột lịch sử và cập nhật nhãn thư mục."""
|
||||
self.sidebar.refresh() # History regroups by project
|
||||
self.cowork._apply_output_folder_label() # project may have been renamed
|
||||
self.structure.refresh_project_list() # GraphRAG's project lock list follows too
|
||||
self.structure._refresh_project_combo() # GraphRAG's project lock list follows too
|
||||
|
||||
@@ -19,14 +19,6 @@ set "APPHOME=%LOCALAPPDATA%\CoworkLocal"
|
||||
set "VENV=%APPHOME%\venv"
|
||||
set "LAUNCHER=%APPHOME%\launcher"
|
||||
|
||||
rem An cua so console NGAY TU DAU, ke ca trong luc kiem tra ben duoi — khong
|
||||
rem chi truoc luc chay app. Moi cho bao loi (echo + pause) ben duoi tu hien
|
||||
rem lai cua so truoc khi in, de thong bao van doc duoc.
|
||||
set "CONSOLE_VIS=%REPO%\scripts\console_visibility.ps1"
|
||||
if exist "%CONSOLE_VIS%" (
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File "%CONSOLE_VIS%" -Mode 0 >nul 2>&1
|
||||
)
|
||||
|
||||
rem --------------------------------------------------------------------------
|
||||
rem 1. Chon trinh thong dich
|
||||
rem
|
||||
@@ -46,7 +38,6 @@ if exist "%VENV%\Scripts\python.exe" (
|
||||
)
|
||||
|
||||
if not defined RUNPY (
|
||||
if exist "%CONSOLE_VIS%" powershell -NoProfile -ExecutionPolicy Bypass -File "%CONSOLE_VIS%" -Mode 5 >nul 2>&1
|
||||
echo.
|
||||
echo [LỖI] Không tìm thấy Python. Chạy install.bat trước đã.
|
||||
echo.
|
||||
@@ -60,7 +51,6 @@ rem biet la phai chay install.bat.
|
||||
if not exist "%VENV%\Scripts\python.exe" (
|
||||
!RUNPY! -c "import PySide6" >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
if exist "%CONSOLE_VIS%" powershell -NoProfile -ExecutionPolicy Bypass -File "%CONSOLE_VIS%" -Mode 5 >nul 2>&1
|
||||
echo.
|
||||
echo [LỖI] Thư viện chưa được cài. Chạy install.bat trước đã.
|
||||
echo.
|
||||
@@ -104,7 +94,6 @@ if /I "%REPO_NAME%"=="cowork_local" (
|
||||
if exist "!PKGPATH!\cowork_local" rmdir "!PKGPATH!\cowork_local" >nul 2>&1
|
||||
mklink /J "!PKGPATH!\cowork_local" "%REPO%" >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
if exist "%CONSOLE_VIS%" powershell -NoProfile -ExecutionPolicy Bypass -File "%CONSOLE_VIS%" -Mode 5 >nul 2>&1
|
||||
echo.
|
||||
echo [LOI] Khong tao duoc lien ket thu muc:
|
||||
echo "!PKGPATH!\cowork_local" -> "%REPO%"
|
||||
@@ -123,7 +112,6 @@ rem Chot lai: goi phai THAT SU nhin thay duoc qua duong dan vua dung. Khong co
|
||||
rem buoc nay thi mot junction hong chi hien ra duoi dang loi Python kho hieu
|
||||
rem ("'cowork_local' is a package and cannot be directly executed").
|
||||
if not exist "!PKGPATH!\cowork_local\__main__.py" (
|
||||
if exist "%CONSOLE_VIS%" powershell -NoProfile -ExecutionPolicy Bypass -File "%CONSOLE_VIS%" -Mode 5 >nul 2>&1
|
||||
echo.
|
||||
echo [LOI] Khong tim thay cowork_local\__main__.py qua duong dan:
|
||||
echo "!PKGPATH!"
|
||||
@@ -150,20 +138,10 @@ if defined PYTHONPATH (
|
||||
set "PYTHONIOENCODING=utf-8"
|
||||
cd /d "%REPO%"
|
||||
|
||||
rem --------------------------------------------------------------------------
|
||||
rem 4. Chay app
|
||||
rem
|
||||
rem App la GUI (Qt), khong can console — cua so console da bi an tu dau file
|
||||
rem roi (xem khoi CONSOLE_VIS phia tren), chi hien lai NEU app thoat loi, de
|
||||
rem thong bao loi ben duoi van doc duoc.
|
||||
rem --------------------------------------------------------------------------
|
||||
!RUNPY! -m cowork_local %*
|
||||
set "RC=%ERRORLEVEL%"
|
||||
|
||||
if not "%RC%"=="0" (
|
||||
if exist "%CONSOLE_VIS%" (
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File "%CONSOLE_VIS%" -Mode 5 >nul 2>&1
|
||||
)
|
||||
echo.
|
||||
echo Ứng dụng thoát với mã lỗi %RC%. Xem thông báo ở trên.
|
||||
echo.
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Ẩn/hiện cửa sổ console hiện tại — dùng bởi run.bat để không hiện cửa sổ
|
||||
cmd đen suốt phiên chạy app (app là GUI Qt, không cần console), nhưng vẫn
|
||||
hiện lại được nếu app thoát lỗi để người dùng đọc thông báo.
|
||||
|
||||
.PARAMETER Mode
|
||||
0 = ẩn (SW_HIDE), 5 = hiện lại (SW_SHOW).
|
||||
#>
|
||||
param(
|
||||
[int]$Mode = 0
|
||||
)
|
||||
|
||||
Add-Type -Name Win32 -Namespace CoworkLocalNative -MemberDefinition @"
|
||||
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||
[DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow();
|
||||
"@
|
||||
|
||||
$hwnd = [CoworkLocalNative.Win32]::GetConsoleWindow()
|
||||
if ($hwnd -ne [IntPtr]::Zero) {
|
||||
[CoworkLocalNative.Win32]::ShowWindow($hwnd, $Mode) | Out-Null
|
||||
}
|
||||
@@ -73,38 +73,6 @@ _MODERATE_PATTERNS = [
|
||||
r'\b(test|pytest|jest|mocha)\b',
|
||||
]
|
||||
|
||||
# Tools that reach the network over ICMP/raw sockets/direct DNS instead of an
|
||||
# HTTP(S) connection — none of them read HTTP_PROXY/HTTPS_PROXY, so
|
||||
# core/deps.py::network_blocked_env()'s proxy-env-var block (the only network
|
||||
# control this sandbox actually enforces) has no effect on them at all. Used
|
||||
# by command_bypasses_network_proxy() to deny these BY NAME when the user has
|
||||
# "Chặn mạng cho lệnh do agent chạy" on, since the proxy trick alone silently
|
||||
# lets them through (see DF-005 in Defect Management).
|
||||
_NETWORK_PROXY_BYPASS_PATTERNS = [
|
||||
r'\bping\b', r'\btracert\b', r'\btraceroute\b', r'\bnslookup\b', r'\bdig\b',
|
||||
r'\btelnet\b', r'\bftp\b', r'\bsftp\b', r'\bscp\b', r'\bssh\b',
|
||||
r'\bnc\b', r'\bncat\b', r'\bnetcat\b', r'\barp\b',
|
||||
r'\btest-netconnection\b', r'\btest-connection\b', r'\bresolve-dnsname\b',
|
||||
]
|
||||
|
||||
|
||||
def command_bypasses_network_proxy(command: str) -> Optional[str]:
|
||||
"""Tên công cụ mạng đầu tiên khớp trong ``command`` mà không tôn trọng
|
||||
HTTP_PROXY/HTTPS_PROXY — None nếu không có công cụ nào như vậy.
|
||||
|
||||
``network_blocked_env()`` chỉ set biến proxy, nên chỉ chặn được các công
|
||||
cụ có ĐỌC biến đó (curl/pip/requests...). ``ping`` (ICMP), ``nslookup``
|
||||
(DNS trực tiếp), ``ssh``/``ftp`` (TCP thô)... đều đi qua giao thức khác,
|
||||
biến proxy không có tác dụng gì với chúng — phải chặn riêng theo tên lệnh
|
||||
khi ``block_network`` đang bật.
|
||||
"""
|
||||
cmd_lower = command.lower()
|
||||
for pattern in _NETWORK_PROXY_BYPASS_PATTERNS:
|
||||
m = re.search(pattern, cmd_lower, re.IGNORECASE)
|
||||
if m:
|
||||
return m.group()
|
||||
return None
|
||||
|
||||
|
||||
def classify_command(command: str, is_cowork_mode: bool = True) -> RiskResult:
|
||||
"""Chấm điểm rủi ro một lệnh shell.
|
||||
|
||||
@@ -55,25 +55,6 @@ def test_structure_graph_view_builds(ctx):
|
||||
assert view.qa is not None
|
||||
|
||||
|
||||
def test_refresh_project_list_forwards_to_renderer(ctx):
|
||||
"""Crash bug: ``presentation/shell/session_events.py::_on_projects_changed``
|
||||
called ``self.structure._refresh_project_combo()`` — a method that only
|
||||
ever existed on ``GraphRenderer``, not on ``StructureGraphView`` itself —
|
||||
so creating/renaming/deleting a project (anywhere in the app) raised
|
||||
``AttributeError`` and crashed. ``refresh_project_list()`` is the public
|
||||
forwarding method callers must use instead (matching ``schedule_rescan``/
|
||||
``set_project``/``prewarm``'s existing forwarding pattern)."""
|
||||
from cowork_local.presentation.graph.structure_graph_view import StructureGraphView
|
||||
|
||||
view = StructureGraphView(ctx)
|
||||
assert not hasattr(view, "_refresh_project_combo")
|
||||
|
||||
combo = view.renderer.project_combo
|
||||
before = combo.count()
|
||||
view.refresh_project_list() # must not raise
|
||||
assert combo.count() == before # re-populated from the same project list, same size
|
||||
|
||||
|
||||
def test_render_populates_the_scene_and_emits_graph_rendered(ctx, tmp_path):
|
||||
from cowork_local.presentation.graph.graph_renderer import GraphRenderer
|
||||
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
"""DF-008 — presentation/folder/ai_file_editor_dialog.py::_AutoExpandInput.
|
||||
|
||||
The AI-edit instruction box was a fixed-height single-line QLineEdit (read as
|
||||
cramped); it is now a QPlainTextEdit that grows with content, submits on
|
||||
Enter, and inserts a newline on Shift+Enter — same convention as the Cowork
|
||||
composer's input (presentation/chat/chat_input_box.py::_Input)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import pytest
|
||||
|
||||
QApplication = pytest.importorskip("PySide6.QtWidgets").QApplication
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtGui import QKeyEvent
|
||||
from PySide6.QtCore import QEvent
|
||||
|
||||
from cowork_local.presentation.folder.ai_file_editor_dialog import _AutoExpandInput
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qapp():
|
||||
app = QApplication.instance() or QApplication([])
|
||||
yield app
|
||||
|
||||
|
||||
def _press_enter(widget, shift: bool = False) -> None:
|
||||
mods = Qt.ShiftModifier if shift else Qt.NoModifier
|
||||
event = QKeyEvent(QEvent.KeyPress, Qt.Key_Return, mods)
|
||||
widget.keyPressEvent(event)
|
||||
|
||||
|
||||
def test_starts_at_min_height(qapp) -> None:
|
||||
box = _AutoExpandInput()
|
||||
assert box.height() == _AutoExpandInput.MIN_HEIGHT
|
||||
|
||||
|
||||
def test_grows_with_multiline_content(qapp) -> None:
|
||||
box = _AutoExpandInput()
|
||||
start_height = box.height()
|
||||
box.setPlainText("\n".join(f"line {i}" for i in range(10)))
|
||||
assert box.height() > start_height
|
||||
assert box.height() <= _AutoExpandInput.MAX_HEIGHT
|
||||
|
||||
|
||||
def test_enter_emits_submit_and_does_not_insert_newline(qapp) -> None:
|
||||
box = _AutoExpandInput()
|
||||
box.setPlainText("hello")
|
||||
received = []
|
||||
box.submit.connect(lambda: received.append(True))
|
||||
_press_enter(box)
|
||||
assert received == [True]
|
||||
assert box.toPlainText() == "hello" # Enter did not add a newline
|
||||
|
||||
|
||||
def test_shift_enter_inserts_newline_without_submitting(qapp) -> None:
|
||||
box = _AutoExpandInput()
|
||||
box.setPlainText("hello")
|
||||
cursor = box.textCursor()
|
||||
cursor.movePosition(cursor.MoveOperation.End)
|
||||
box.setTextCursor(cursor)
|
||||
received = []
|
||||
box.submit.connect(lambda: received.append(True))
|
||||
_press_enter(box, shift=True)
|
||||
assert received == []
|
||||
assert box.toPlainText() == "hello\n"
|
||||
@@ -1,132 +0,0 @@
|
||||
"""DF-007 — core/cloud_workspace_sync.py: mirror a cloud folder to/from a
|
||||
local directory. All Graph calls are faked (monkeypatch on the ``graph``
|
||||
module the sync module imports) — no network."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from cowork_local.core import cloud_workspace_sync as sync
|
||||
from cowork_local.core import ms365_graph as graph
|
||||
|
||||
|
||||
def _fake_tree():
|
||||
"""root/
|
||||
a.txt
|
||||
sub/
|
||||
b.txt
|
||||
"""
|
||||
files = {"a.txt": b"hello", "sub/b.txt": b"world"}
|
||||
listing = {
|
||||
"": [{"name": "a.txt"}, {"name": "sub", "folder": {}}],
|
||||
"sub": [{"name": "b.txt"}],
|
||||
}
|
||||
return files, listing
|
||||
|
||||
|
||||
def test_download_folder_mirrors_tree(tmp_path: Path, monkeypatch) -> None:
|
||||
files, listing = _fake_tree()
|
||||
|
||||
def fake_list_onedrive_files(token, path=""):
|
||||
return listing.get(path, [])
|
||||
|
||||
def fake_download_bytes(token, path):
|
||||
return files[path]
|
||||
|
||||
monkeypatch.setattr(graph, "list_onedrive_files", fake_list_onedrive_files)
|
||||
monkeypatch.setattr(graph, "download_onedrive_file_bytes", fake_download_bytes)
|
||||
|
||||
local_dir = tmp_path / "mirror"
|
||||
report = sync.download_folder("tok", {"provider": "onedrive", "remote_path": ""}, local_dir)
|
||||
|
||||
assert report.transferred == 2
|
||||
assert report.errors == []
|
||||
assert (local_dir / "a.txt").read_bytes() == b"hello"
|
||||
assert (local_dir / "sub" / "b.txt").read_bytes() == b"world"
|
||||
|
||||
|
||||
def test_download_folder_collects_errors_without_raising(tmp_path: Path, monkeypatch) -> None:
|
||||
def fake_list_onedrive_files(token, path=""):
|
||||
raise graph.Ms365GraphError("boom")
|
||||
|
||||
monkeypatch.setattr(graph, "list_onedrive_files", fake_list_onedrive_files)
|
||||
|
||||
local_dir = tmp_path / "mirror"
|
||||
report = sync.download_folder("tok", {"provider": "onedrive", "remote_path": ""}, local_dir)
|
||||
|
||||
assert report.transferred == 0
|
||||
assert len(report.errors) == 1
|
||||
assert "boom" in report.errors[0]
|
||||
|
||||
|
||||
def test_upload_folder_pushes_every_file(tmp_path: Path, monkeypatch) -> None:
|
||||
local_dir = tmp_path / "mirror"
|
||||
(local_dir / "sub").mkdir(parents=True)
|
||||
(local_dir / "a.txt").write_bytes(b"hello")
|
||||
(local_dir / "sub" / "b.txt").write_bytes(b"world")
|
||||
|
||||
uploaded = {}
|
||||
|
||||
def fake_upload_bytes(token, path, data):
|
||||
uploaded[path] = data
|
||||
return {}
|
||||
|
||||
monkeypatch.setattr(graph, "upload_onedrive_file_bytes", fake_upload_bytes)
|
||||
|
||||
report = sync.upload_folder("tok", {"provider": "onedrive", "remote_path": "work"}, local_dir)
|
||||
|
||||
assert report.transferred == 2
|
||||
assert uploaded == {"work/a.txt": b"hello", "work/sub/b.txt": b"world"}
|
||||
|
||||
|
||||
def test_upload_folder_reports_files_over_the_simple_upload_limit(tmp_path: Path, monkeypatch) -> None:
|
||||
local_dir = tmp_path / "mirror"
|
||||
local_dir.mkdir()
|
||||
(local_dir / "big.bin").write_bytes(b"x")
|
||||
|
||||
def fake_upload_bytes(token, path, data):
|
||||
raise graph.Ms365GraphError("File too large for simple upload (huge > 4 bytes)")
|
||||
|
||||
monkeypatch.setattr(graph, "upload_onedrive_file_bytes", fake_upload_bytes)
|
||||
|
||||
report = sync.upload_folder("tok", {"provider": "onedrive", "remote_path": ""}, local_dir)
|
||||
|
||||
assert report.transferred == 0
|
||||
assert report.skipped_too_large == ["big.bin"]
|
||||
assert report.errors == []
|
||||
|
||||
|
||||
def test_upload_size_guard_rejects_before_any_request(monkeypatch) -> None:
|
||||
huge = b"x" * (graph.MAX_SIMPLE_UPLOAD_BYTES + 1)
|
||||
|
||||
def fail_if_called(*a, **k): # pragma: no cover - must not be reached
|
||||
raise AssertionError("_request should not be called for an oversized upload")
|
||||
|
||||
monkeypatch.setattr(graph, "_request", fail_if_called)
|
||||
|
||||
with pytest.raises(graph.Ms365GraphError, match="too large"):
|
||||
graph.upload_onedrive_file_bytes("tok", "a.bin", huge)
|
||||
|
||||
|
||||
def test_sharepoint_provider_uses_site_scoped_calls(tmp_path: Path, monkeypatch) -> None:
|
||||
seen = {}
|
||||
|
||||
def fake_list_sharepoint_files(token, site_id, path=""):
|
||||
seen["list_site_id"] = site_id
|
||||
return [{"name": "a.txt"}] if path == "" else []
|
||||
|
||||
def fake_download_sharepoint_bytes(token, site_id, path):
|
||||
seen["download_site_id"] = site_id
|
||||
return b"hi"
|
||||
|
||||
monkeypatch.setattr(graph, "list_sharepoint_files", fake_list_sharepoint_files)
|
||||
monkeypatch.setattr(graph, "download_sharepoint_file_bytes", fake_download_sharepoint_bytes)
|
||||
|
||||
local_dir = tmp_path / "mirror"
|
||||
cloud_source = {"provider": "sharepoint", "site_id": "site-123", "remote_path": ""}
|
||||
report = sync.download_folder("tok", cloud_source, local_dir)
|
||||
|
||||
assert report.transferred == 1
|
||||
assert seen["list_site_id"] == "site-123"
|
||||
assert seen["download_site_id"] == "site-123"
|
||||
@@ -1,55 +0,0 @@
|
||||
"""DF-002 (phần b) — node đang chạy HOẶC đã chạy xong không cho edit thông
|
||||
tin trong Node. Trước khi sửa, ``_LOCKED_NODE_STATUSES`` khoá cả STEP_RUNNING
|
||||
lẫn STEP_DONE, nên một bước đã chạy xong không bao giờ sửa lại được nữa.
|
||||
|
||||
Fix: chỉ khoá khi bước ĐANG chạy (STEP_RUNNING) — chạy xong rồi thì mở khoá
|
||||
trở lại. Test này chốt cả nguồn sự thật (tuple
|
||||
``co4e_workflow_crud._LOCKED_NODE_STATUSES``) lẫn hành vi ở widget
|
||||
(``StepConfigPanel.set_locked``), để không bị hồi quy về hành vi cũ.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import pytest
|
||||
|
||||
QApplication = pytest.importorskip("PySide6.QtWidgets").QApplication
|
||||
|
||||
from cowork_local.core.co4e import (
|
||||
STEP_DONE, STEP_ERROR, STEP_IDLE, STEP_PLANNED, STEP_RUNNING,
|
||||
)
|
||||
from cowork_local.presentation.co4e.co4e_workflow_crud import _LOCKED_NODE_STATUSES
|
||||
from cowork_local.presentation.co4e.node_property_panel import StepConfigPanel
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qapp():
|
||||
app = QApplication.instance() or QApplication([])
|
||||
yield app
|
||||
|
||||
|
||||
def test_only_running_status_is_locked() -> None:
|
||||
"""Một bước đã chạy xong (STEP_DONE) phải sửa lại được — chỉ bước đang
|
||||
thực sự chạy (STEP_RUNNING) mới bị khoá."""
|
||||
assert _LOCKED_NODE_STATUSES == (STEP_RUNNING,)
|
||||
assert STEP_DONE not in _LOCKED_NODE_STATUSES
|
||||
assert STEP_IDLE not in _LOCKED_NODE_STATUSES
|
||||
assert STEP_ERROR not in _LOCKED_NODE_STATUSES
|
||||
assert STEP_PLANNED not in _LOCKED_NODE_STATUSES
|
||||
|
||||
|
||||
def test_set_locked_disables_then_reenables_edit_fields(qapp) -> None:
|
||||
panel = StepConfigPanel()
|
||||
panel.setEnabled(True) # panel starts disabled until a step is loaded
|
||||
|
||||
panel.set_locked(True)
|
||||
assert not panel.label_edit.isEnabled()
|
||||
assert not panel.instructions_edit.isEnabled()
|
||||
assert not panel.model_combo.isEnabled()
|
||||
|
||||
panel.set_locked(False)
|
||||
assert panel.label_edit.isEnabled()
|
||||
assert panel.instructions_edit.isEnabled()
|
||||
assert panel.model_combo.isEnabled()
|
||||
@@ -1,140 +0,0 @@
|
||||
"""DF-006 — the "Số dòng/trang" (rows per page) control plus real prev/next
|
||||
pagination: EventTable's page-size/page-index state
|
||||
(presentation/monitoring/shared/event_table.py) and its QComboBox + pager
|
||||
button wiring in build_filter_scaffold (.../shared/filter_scaffold.py).
|
||||
|
||||
Options are 5/10/20/50/100 with a next/prev pager, per the QA follow-up on
|
||||
DF-006 — the earlier fix only trimmed to a page size (dropping every row past
|
||||
it with no way back); this exercises the real paging end to end."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import pytest
|
||||
|
||||
QApplication = pytest.importorskip("PySide6.QtWidgets").QApplication
|
||||
QWidget = pytest.importorskip("PySide6.QtWidgets").QWidget
|
||||
|
||||
from cowork_local.presentation.monitoring.shared.event_table import (
|
||||
PAGE_SIZE_OPTIONS, EventTable,
|
||||
)
|
||||
from cowork_local.presentation.monitoring.shared.filter_scaffold import build_filter_scaffold
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qapp():
|
||||
app = QApplication.instance() or QApplication([])
|
||||
yield app
|
||||
|
||||
|
||||
def _events(n: int):
|
||||
return [{"ts": f"2026-09-0{i % 9 + 1}T00:00:0{i % 9}", "kind": "tool_call",
|
||||
"name": f"e{i}", "ok": True, "detail": ""} for i in range(n)]
|
||||
|
||||
|
||||
def test_page_size_options_are_5_10_20_50_100() -> None:
|
||||
assert PAGE_SIZE_OPTIONS == (5, 10, 20, 50, 100)
|
||||
|
||||
|
||||
def test_default_page_size(qapp) -> None:
|
||||
table = EventTable()
|
||||
assert table.page_size() == 20
|
||||
table.set_events(_events(45))
|
||||
assert table.rowCount() == 20
|
||||
assert table.page_count() == 3
|
||||
assert table.current_page() == 0
|
||||
|
||||
|
||||
def test_set_page_size_resets_to_first_page(qapp) -> None:
|
||||
table = EventTable()
|
||||
table.set_events(_events(45))
|
||||
table.next_page()
|
||||
assert table.current_page() == 1
|
||||
table.set_page_size(50)
|
||||
assert table.page_size() == 50
|
||||
assert table.current_page() == 0
|
||||
assert table.rowCount() == 45 # only 45 events total, fits in one page of 50
|
||||
|
||||
|
||||
def test_next_prev_page_navigate_without_dropping_rows(qapp) -> None:
|
||||
table = EventTable()
|
||||
table.set_events(_events(45))
|
||||
table.set_page_size(20)
|
||||
assert table.rowCount() == 20
|
||||
|
||||
table.next_page()
|
||||
assert table.current_page() == 1
|
||||
assert table.rowCount() == 20
|
||||
|
||||
table.next_page()
|
||||
assert table.current_page() == 2
|
||||
assert table.rowCount() == 5 # last page: remainder
|
||||
|
||||
table.next_page() # already on last page — stays put
|
||||
assert table.current_page() == 2
|
||||
|
||||
table.prev_page()
|
||||
assert table.current_page() == 1
|
||||
table.prev_page()
|
||||
table.prev_page() # already on first page — stays put
|
||||
assert table.current_page() == 0
|
||||
|
||||
|
||||
def test_page_changed_signal_reports_current_and_total(qapp) -> None:
|
||||
table = EventTable()
|
||||
seen = []
|
||||
table.page_changed.connect(lambda cur, total: seen.append((cur, total)))
|
||||
table.set_events(_events(45))
|
||||
table.set_page_size(20)
|
||||
table.next_page()
|
||||
assert seen[-1] == (1, 3)
|
||||
|
||||
|
||||
def test_page_size_combo_is_only_added_when_requested(qapp) -> None:
|
||||
page = QWidget()
|
||||
table = EventTable()
|
||||
parts = build_filter_scaffold(page, table, on_refresh=lambda: None, with_page_size=False)
|
||||
assert "page_size_combo" not in parts
|
||||
assert "page_prev_btn" not in parts
|
||||
|
||||
|
||||
def test_page_size_combo_changes_the_table(qapp) -> None:
|
||||
page = QWidget()
|
||||
table = EventTable()
|
||||
table.set_events(_events(45))
|
||||
parts = build_filter_scaffold(page, table, on_refresh=lambda: None, with_page_size=True)
|
||||
combo = parts["page_size_combo"]
|
||||
assert combo.count() == len(PAGE_SIZE_OPTIONS)
|
||||
assert combo.currentData() == 20 # matches EventTable's current page_size
|
||||
|
||||
idx = PAGE_SIZE_OPTIONS.index(10)
|
||||
combo.setCurrentIndex(idx)
|
||||
|
||||
assert table.page_size() == 10
|
||||
assert table.rowCount() == 10
|
||||
|
||||
|
||||
def test_pager_buttons_disable_at_bounds_and_indicator_updates(qapp) -> None:
|
||||
page = QWidget()
|
||||
table = EventTable()
|
||||
table.set_events(_events(45))
|
||||
parts = build_filter_scaffold(page, table, on_refresh=lambda: None, with_page_size=True)
|
||||
table.set_page_size(20)
|
||||
prev_btn, next_btn = parts["page_prev_btn"], parts["page_next_btn"]
|
||||
indicator = parts["page_indicator_label"]
|
||||
|
||||
assert not prev_btn.isEnabled()
|
||||
assert next_btn.isEnabled()
|
||||
assert indicator.text() == "Trang 1/3"
|
||||
|
||||
next_btn.click()
|
||||
assert prev_btn.isEnabled()
|
||||
assert next_btn.isEnabled()
|
||||
assert indicator.text() == "Trang 2/3"
|
||||
|
||||
next_btn.click()
|
||||
assert prev_btn.isEnabled()
|
||||
assert not next_btn.isEnabled()
|
||||
assert indicator.text() == "Trang 3/3"
|
||||
@@ -1,77 +0,0 @@
|
||||
"""DF-007 — construction smoke tests for the two new MS365 cloud dialogs.
|
||||
Not a full characterization suite (see tests/test_monitoring_tab_container.py
|
||||
for the convention this follows) — just proves each dialog builds against a
|
||||
real AppConfig/AppContext without touching the network (Graph calls faked via
|
||||
monkeypatch)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import os
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
import pytest
|
||||
|
||||
QApplication = pytest.importorskip("PySide6.QtWidgets").QApplication
|
||||
QDialog = pytest.importorskip("PySide6.QtWidgets").QDialog
|
||||
|
||||
from cowork_local.config import AppConfig, DEFAULT_CONFIG
|
||||
from cowork_local.core import ms365_auth
|
||||
from cowork_local.core import ms365_graph as graph
|
||||
from cowork_local.ui.cloud_folder_picker_dialog import CloudFolderPickerDialog
|
||||
from cowork_local.ui.ms365_signin_dialog import Ms365SignInDialog, ensure_signed_in
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qapp():
|
||||
app = QApplication.instance() or QApplication([])
|
||||
yield app
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def config(tmp_path):
|
||||
return AppConfig(data=copy.deepcopy(DEFAULT_CONFIG), path=tmp_path / "config.json")
|
||||
|
||||
|
||||
def test_signin_dialog_constructs(qapp, config) -> None:
|
||||
dialog = Ms365SignInDialog(config)
|
||||
assert dialog.windowTitle()
|
||||
|
||||
|
||||
def test_ensure_signed_in_short_circuits_when_already_signed_in(qapp, config, monkeypatch) -> None:
|
||||
monkeypatch.setattr(ms365_auth, "is_signed_in", lambda cfg: True)
|
||||
assert ensure_signed_in(None, config) is True
|
||||
|
||||
|
||||
def test_cloud_folder_picker_constructs_and_lists_onedrive_root(qapp, config, monkeypatch) -> None:
|
||||
monkeypatch.setattr(ms365_auth, "get_access_token", lambda tenant_id, client_id: "fake-token")
|
||||
monkeypatch.setattr(graph, "list_onedrive_files", lambda token, path="": [
|
||||
{"name": "Documents", "folder": {}},
|
||||
{"name": "readme.txt"},
|
||||
])
|
||||
|
||||
dialog = CloudFolderPickerDialog(config)
|
||||
|
||||
assert dialog._tree.topLevelItemCount() == 2
|
||||
source = dialog.cloud_source()
|
||||
assert source == {"provider": "onedrive", "site_id": "", "site_name": "", "remote_path": ""}
|
||||
|
||||
|
||||
def test_cloud_folder_picker_navigates_into_a_folder(qapp, config, monkeypatch) -> None:
|
||||
monkeypatch.setattr(ms365_auth, "get_access_token", lambda tenant_id, client_id: "fake-token")
|
||||
|
||||
def fake_list(token, path=""):
|
||||
if path == "":
|
||||
return [{"name": "Documents", "folder": {}}]
|
||||
if path == "Documents":
|
||||
return [{"name": "report.docx"}]
|
||||
return []
|
||||
|
||||
monkeypatch.setattr(graph, "list_onedrive_files", fake_list)
|
||||
|
||||
dialog = CloudFolderPickerDialog(config)
|
||||
folder_item = dialog._tree.topLevelItem(0)
|
||||
dialog._on_item_activated(folder_item, 0)
|
||||
|
||||
assert dialog._current_remote_path() == "Documents"
|
||||
assert dialog.cloud_source()["remote_path"] == "Documents"
|
||||
@@ -1,125 +0,0 @@
|
||||
"""Công tắc "Chặn mạng cho lệnh do agent chạy" phải chặn MỌI đường ra mạng của
|
||||
agent, không riêng ``run_command``.
|
||||
|
||||
Trước đây ``block_network`` chỉ được đọc ở đúng một chỗ —
|
||||
``infrastructure/filesystem/command_tools.py`` trong ``run_command`` — nên bốn
|
||||
tool mang ``ToolCapability.NETWORK`` (``fetch_url``, ``jira_search``,
|
||||
``jira_get_issue``, ``install_package``) vẫn ra internet bình thường trong khi
|
||||
màn Monitoring báo "Mạng: Bị chặn" và docstring của ``fetch_tools`` tự nhận là
|
||||
*"Honors the Sandbox Security Layer's Block network policy"*. Người dùng bật
|
||||
công tắc rồi thấy agent vẫn search web được — đúng triệu chứng được báo.
|
||||
|
||||
Hai nhóm bài:
|
||||
|
||||
* **hành vi** — bật thì mọi tool NETWORK từ chối TRƯỚC khi chạm mạng, tắt thì
|
||||
đường cũ giữ nguyên (chặn một chiều là hỏng tính năng);
|
||||
* **guardrail** — thêm tool mạng mới mà quên chặn thì bài ở đây đỏ ngay.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
|
||||
from cowork_local.core.tools import ToolContext
|
||||
from cowork_local.domain.tools import BUILT_IN_CAPABILITIES, ToolCapability
|
||||
from cowork_local.infrastructure.filesystem import command_tools, fetch_tools
|
||||
|
||||
# tên tool -> (handler, args hợp lệ tối thiểu). Args phải hợp lệ, nếu không bài
|
||||
# test sẽ đỏ vì lỗi thiếu tham số chứ không vì cổng chặn mạng.
|
||||
_TOOL_MANG: Dict[str, tuple] = {
|
||||
"fetch_url": (fetch_tools.fetch_url, {"url": "https://example.com/"}),
|
||||
"jira_search": (fetch_tools.jira_search, {"jql": "project = ABC"}),
|
||||
"jira_get_issue": (fetch_tools.jira_get_issue, {"key": "ABC-1"}),
|
||||
"install_package": (command_tools.install_package, {"package": "requests"}),
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cam_ra_mang(monkeypatch):
|
||||
"""Mọi đường ra mạng thật đều nổ.
|
||||
|
||||
Vừa giữ cho bộ test không chạm internet, vừa làm lộ tool nào lọt qua cổng
|
||||
chặn: nó sẽ đỏ ngay tại lời gọi mạng thay vì im lặng đi ra ngoài.
|
||||
"""
|
||||
def no_ra_mang(*args, **kwargs):
|
||||
raise AssertionError("tool đã chạm mạng dù 'Chặn mạng' đang bật")
|
||||
|
||||
from cowork_local.core import deps, jira_tool, link_fetch
|
||||
|
||||
monkeypatch.setattr(link_fetch, "fetch_link_preview", no_ra_mang)
|
||||
monkeypatch.setattr(jira_tool, "search", no_ra_mang)
|
||||
monkeypatch.setattr(jira_tool, "get_issue", no_ra_mang)
|
||||
monkeypatch.setattr(jira_tool, "get_issue_by_url", no_ra_mang)
|
||||
monkeypatch.setattr(deps, "pip_install", no_ra_mang)
|
||||
|
||||
|
||||
# ---- hành vi: bật công tắc thì mọi tool mạng đều bị chặn -----------------
|
||||
|
||||
@pytest.mark.parametrize("ten", sorted(_TOOL_MANG))
|
||||
def test_bat_chan_mang_thi_tool_tu_choi_truoc_khi_cham_mang(tmp_path, cam_ra_mang, ten):
|
||||
"""Đây là chính triệu chứng người dùng báo: bật rồi mà vẫn ra được web."""
|
||||
handler, args = _TOOL_MANG[ten]
|
||||
ctx = ToolContext(tmp_path, block_network=True)
|
||||
|
||||
ket_qua = handler(ctx, args)
|
||||
|
||||
assert ket_qua["ok"] is False, f"{ten} vẫn chạy khi đang chặn mạng"
|
||||
assert "Sandbox Security Layer" in ket_qua["output"], ket_qua["output"]
|
||||
|
||||
|
||||
def test_allow_url_fetch_khong_lach_duoc_chan_mang(tmp_path, cam_ra_mang):
|
||||
"""Hai công tắc vẫn độc lập, nhưng "Chặn mạng" là cái mạnh hơn: bật nó thì
|
||||
"Cho phép agent lấy dữ liệu từ URL" không mở lại đường được."""
|
||||
ctx = ToolContext(tmp_path, block_network=True, allow_url_fetch=True)
|
||||
|
||||
ket_qua = fetch_tools.fetch_url(ctx, {"url": "https://example.com/"})
|
||||
|
||||
assert ket_qua["ok"] is False
|
||||
|
||||
|
||||
# ---- hành vi: tắt công tắc thì đường cũ giữ nguyên -----------------------
|
||||
|
||||
def test_tat_chan_mang_thi_fetch_url_van_doc_duoc(tmp_path, monkeypatch):
|
||||
"""Chặn một chiều là hỏng tính năng — cổng phải mở lại được."""
|
||||
from cowork_local.core import link_fetch
|
||||
|
||||
monkeypatch.setattr(link_fetch, "fetch_link_preview",
|
||||
lambda url: f"nội dung của {url}")
|
||||
ctx = ToolContext(tmp_path, block_network=False)
|
||||
|
||||
ket_qua = fetch_tools.fetch_url(ctx, {"url": "https://example.com/"})
|
||||
|
||||
assert ket_qua["ok"] is True
|
||||
assert "example.com" in ket_qua["output"]
|
||||
|
||||
|
||||
def test_tat_chan_mang_thi_install_package_van_chay(tmp_path, monkeypatch):
|
||||
from cowork_local.core import deps
|
||||
|
||||
da_goi = []
|
||||
|
||||
def gia_lap_pip(package, **kwargs):
|
||||
da_goi.append(package)
|
||||
return True, "ok"
|
||||
|
||||
monkeypatch.setattr(deps, "pip_install", gia_lap_pip)
|
||||
ctx = ToolContext(tmp_path, block_network=False)
|
||||
|
||||
ket_qua = command_tools.install_package(ctx, {"package": "requests"})
|
||||
|
||||
assert da_goi == ["requests"]
|
||||
assert ket_qua["ok"] is True
|
||||
|
||||
|
||||
# ---- guardrail: danh sách tool mạng không được lệch ----------------------
|
||||
|
||||
def test_moi_tool_mang_deu_co_bai_o_day():
|
||||
"""``BUILT_IN_CAPABILITIES`` là nơi duy nhất khai báo tool nào chạm mạng.
|
||||
Thêm một tool NETWORK mới mà quên chặn thì bài này đỏ ngay."""
|
||||
tag_mang = {ten for ten, cap in BUILT_IN_CAPABILITIES.items()
|
||||
if cap & ToolCapability.NETWORK}
|
||||
|
||||
assert tag_mang == set(_TOOL_MANG), (
|
||||
"danh sách tool mạng đã đổi — chặn tool mới ở cổng block_network "
|
||||
"rồi bổ sung vào _TOOL_MANG")
|
||||
@@ -1,141 +0,0 @@
|
||||
"""Cổng project: Cowork và GraphRAG chỉ hiện khi đã chọn một project cụ thể.
|
||||
|
||||
Cổng có hai mặt và trước đây chỉ mặt thứ nhất làm đúng:
|
||||
|
||||
* **Sub-tab trong màn Workspace** — ``_update_tab_visibility`` vốn đã ẩn/hiện
|
||||
đúng. Chỗ hỏng nằm ở ``refresh()``: nó mặc định ``row_to_select = 0`` nên lúc
|
||||
mở app (chưa ai bấm gì) danh sách tự chọn hộ project đầu tiên, mở cổng cho một
|
||||
project người dùng chưa hề chọn.
|
||||
* **Hàng trên menu trái** — ``NavRailMixin._rebuild_nav`` từng dựng hàng ở dạng
|
||||
mờ kèm tooltip thay vì bỏ đi ("shown instead of hidden"), nên người dùng vẫn
|
||||
thấy Cowork/GraphRAG trên menu dù cổng đang đóng.
|
||||
|
||||
Các bài dưới đây chốt cả hai mặt, ở cả ba trạng thái: chưa chọn → ẩn, chọn rồi →
|
||||
hiện, bỏ chọn → ẩn lại.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("PySide6", reason="cần PySide6 để dựng cửa sổ thật")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def win(qapp, tmp_path):
|
||||
"""MainWindow thật — cần cả cửa sổ vì phải kiểm cả menu trái.
|
||||
|
||||
Đọc project từ ``~/.cowork_local`` như bản cài thật (``core/projects.py``
|
||||
gắn ``PROJECTS_DIR`` vào đó) nên các bài này KHÔNG tạo/xoá project nào.
|
||||
Bài nào cần cổng MỞ thì gọi thẳng ``_update_tab_visibility(True)`` thay vì
|
||||
tạo project trên đĩa của người chạy test.
|
||||
"""
|
||||
from cowork_local.presentation.shell.bootstrap import build_config, build_context
|
||||
from cowork_local.presentation.shell.main_window import MainWindow
|
||||
|
||||
config_path = tmp_path / "config.json"
|
||||
build_config(config_path)
|
||||
window = MainWindow(build_context(config_path))
|
||||
yield window
|
||||
window.close()
|
||||
|
||||
|
||||
def _cong(ws):
|
||||
"""Hai sub-tab nằm sau cổng project, bỏ qua bản dựng không có chúng."""
|
||||
return [(ten, idx) for ten, idx in
|
||||
(("Cowork", ws._cowork_tab_idx), ("GraphRAG", ws._graphrag_tab_idx))
|
||||
if idx >= 0]
|
||||
|
||||
|
||||
def _hang_menu(win):
|
||||
"""Nhãn của mọi hàng đang có trên cột menu trái."""
|
||||
return [win.nav.topLevelItem(i).text(0) for i in range(win.nav.topLevelItemCount())]
|
||||
|
||||
|
||||
# ---- mặt 1: không tự chọn hộ project ------------------------------------
|
||||
|
||||
def test_mo_app_len_chua_chon_thi_khong_tu_chon_ho(win):
|
||||
"""Đây là nguyên nhân gốc: ``refresh()`` từng mặc định chọn dòng 0."""
|
||||
ws = win.workspace
|
||||
|
||||
assert ws._current_id == ""
|
||||
assert ws.project_list.currentRow() == -1
|
||||
|
||||
|
||||
def test_chua_chon_project_thi_hai_sub_tab_deu_an(win):
|
||||
ws = win.workspace
|
||||
|
||||
for ten, idx in _cong(ws):
|
||||
assert ws.tabs.isTabVisible(idx) is False, f"{ten} hiện khi chưa chọn project"
|
||||
assert ws.subtab_available(idx) is False, f"{ten} vẫn mở cổng"
|
||||
|
||||
|
||||
def test_chua_chon_project_thi_dung_o_tab_project(win):
|
||||
"""Ẩn hai tab kia mà lại đứng ở một tab đã ẩn thì màn hình trống trơn."""
|
||||
ws = win.workspace
|
||||
|
||||
assert ws.current_subtab() == ws._project_tab_idx
|
||||
|
||||
|
||||
def test_refresh_giu_nguyen_project_dang_chon(win):
|
||||
"""Sửa cổng không được làm mất lựa chọn hiện có: ``keep`` vẫn phải thắng."""
|
||||
ws = win.workspace
|
||||
if ws.project_list.count() == 0:
|
||||
pytest.skip("máy chạy test chưa có project nào để chọn")
|
||||
|
||||
ws.project_list.setCurrentRow(0)
|
||||
dang_chon = ws._current_id
|
||||
|
||||
ws.refresh()
|
||||
|
||||
assert ws._current_id == dang_chon
|
||||
assert ws.project_list.currentRow() >= 0
|
||||
|
||||
|
||||
# ---- mặt 2: menu trái bỏ hẳn hàng, không hiện dạng mờ -------------------
|
||||
|
||||
def test_chua_chon_project_thi_menu_trai_khong_co_hai_hang(win):
|
||||
"""Đây là thứ người dùng nhìn thấy — trước đây hai hàng vẫn nằm đó, chỉ mờ."""
|
||||
nhan = _hang_menu(win)
|
||||
|
||||
assert "Cowork" not in nhan, f"Cowork vẫn trên menu: {nhan}"
|
||||
assert "GraphRAG" not in nhan, f"GraphRAG vẫn trên menu: {nhan}"
|
||||
|
||||
|
||||
def test_mo_cong_thi_hai_hang_quay_lai_menu_trai(win):
|
||||
"""Bỏ hàng phải đảo ngược được, nếu không thì chọn project xong vẫn kẹt."""
|
||||
ws = win.workspace
|
||||
ws._current_id = "gia-lap"
|
||||
ws._update_tab_visibility(True)
|
||||
|
||||
nhan = _hang_menu(win)
|
||||
assert "Cowork" in nhan, f"Cowork không quay lại: {nhan}"
|
||||
assert "GraphRAG" in nhan, f"GraphRAG không quay lại: {nhan}"
|
||||
|
||||
|
||||
def test_mo_cong_thi_hai_sub_tab_cung_hien_lai(win):
|
||||
ws = win.workspace
|
||||
ws._current_id = "gia-lap"
|
||||
ws._update_tab_visibility(True)
|
||||
|
||||
for ten, idx in _cong(ws):
|
||||
assert ws.tabs.isTabVisible(idx) is True, f"{ten} vẫn ẩn khi cổng đã mở"
|
||||
|
||||
|
||||
def test_dong_cong_lai_thi_hai_hang_bien_mat(win):
|
||||
"""Cổng phải đóng lại được, không chỉ mở một chiều."""
|
||||
ws = win.workspace
|
||||
ws._current_id = "gia-lap"
|
||||
ws._update_tab_visibility(True)
|
||||
ws._current_id = ""
|
||||
ws._update_tab_visibility(False)
|
||||
|
||||
nhan = _hang_menu(win)
|
||||
assert "Cowork" not in nhan and "GraphRAG" not in nhan, nhan
|
||||
|
||||
|
||||
def test_cac_hang_khac_khong_bi_anh_huong(win):
|
||||
"""Chỉ hai hàng sau cổng bị bỏ — phần còn lại của menu giữ nguyên."""
|
||||
nhan = _hang_menu(win)
|
||||
|
||||
for bat_buoc in ("Project", "Co4E"):
|
||||
assert bat_buoc in nhan, f"{bat_buoc} biến mất khỏi menu: {nhan}"
|
||||
@@ -1,21 +1,23 @@
|
||||
"""Sandbox Security Layer: bốn công tắc luôn sửa được, không còn khoá mật khẩu.
|
||||
"""Sandbox Security unlock — chốt các đường KHÔNG được mở khoá (SEC-20260907-01).
|
||||
|
||||
Trước đây nhóm này bị khoá: bốn công tắc dựng ra ở trạng thái ``setEnabled(False)``
|
||||
và chỉ mở khi nhập đúng mật khẩu qua ``_sandbox_unlock()``. Bộ bài cũ ở file này
|
||||
(SEC-20260907-01) chốt các đường KHÔNG được mở khoá — chúng mất đối tượng kiểm khi
|
||||
tính năng khoá bị bỏ theo yêu cầu, nên được thay bằng các bài dưới đây.
|
||||
``DEFAULT_CONFIG`` ship ``agent_security.sandbox_pw = ""`` kể từ commit
|
||||
``3827552 fix(security): remove shared unlock defaults``, và cấu hình đưa tới
|
||||
dialog LUÔN được deep-merge với defaults đó
|
||||
(``infrastructure/config/json_config_repository.py``). Nghĩa là trên mọi bản cài
|
||||
không đặt ``COWORK_SANDBOX_PASSWORD``, mật khẩu đã lưu là chuỗi rỗng — và phép so
|
||||
sánh ``pw == self._sandbox_pw`` nhận luôn ô nhập trống.
|
||||
|
||||
Docstring của ``_sandbox_unlock()`` cũ đã tự nói rõ nó là gì: *"khoá phía giao diện
|
||||
để chặn bấm nhầm vào một mục nhạy cảm, KHÔNG phải cơ chế bảo mật thật"*. Rào thật
|
||||
nằm ở tầng sandbox lúc chạy lệnh, không ở hộp thoại Cài đặt.
|
||||
Ba nhóm bài ở đây:
|
||||
|
||||
Hai nhóm bài:
|
||||
|
||||
* **hành vi mới** — mở hộp thoại là bật/tắt được ngay, không qua bước nào;
|
||||
* **guardrail** — quét mã nguồn để lần sau không ai lặng lẽ khoá lại.
|
||||
* **đường tấn công** — chốt đúng lỗ trên;
|
||||
* **đường đi đúng** — bản vá không được phá, kể cả với mật khẩu có dấu;
|
||||
* **chặn cả lớp lỗi** — commit ``3827552`` sửa ``config.py`` nhưng bỏ sót bản sao
|
||||
thứ hai của literal trong ``ui/settings_dialog.py``. Bài cuối quét chéo mọi thư
|
||||
mục nguồn để lần sau không sót kiểu đó nữa.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
@@ -25,76 +27,209 @@ import pytest
|
||||
from .test_settings_dialog_dac_ta import _Ctx
|
||||
|
||||
|
||||
def _dialog():
|
||||
"""SettingsDialog dựng đúng như bản cài thật."""
|
||||
@pytest.fixture
|
||||
def shown(monkeypatch):
|
||||
"""Ghi lại mọi QMessageBox thay vì bật modal thật (modal sẽ treo test).
|
||||
|
||||
Trả về list các ``(loại, tiêu_đề, nội_dung)`` — cần thiết để phân biệt
|
||||
"chưa cấu hình mật khẩu" với "sai mật khẩu"; nếu chỉ nuốt hộp thoại đi thì
|
||||
hai nhánh gộp lại làm một mà test vẫn xanh.
|
||||
"""
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
calls: list[tuple[str, str, str]] = []
|
||||
|
||||
def _record(kind):
|
||||
def _fn(_parent, title, text, *a, **k):
|
||||
calls.append((kind, title, text))
|
||||
return staticmethod(_fn)
|
||||
|
||||
monkeypatch.setattr(QMessageBox, "warning", _record("warning"))
|
||||
monkeypatch.setattr(QMessageBox, "information", _record("information"))
|
||||
return calls
|
||||
|
||||
|
||||
def _dialog(stored_pw: str):
|
||||
"""SettingsDialog với ``sandbox_pw`` đúng như bản cài thật: key CÓ mặt."""
|
||||
from cowork_local.ui.settings_dialog import SettingsDialog
|
||||
return SettingsDialog(_Ctx())
|
||||
ctx = _Ctx()
|
||||
ctx.config.data["agent_security"]["sandbox_pw"] = stored_pw
|
||||
return SettingsDialog(ctx)
|
||||
|
||||
|
||||
_CONG_TAC = ("sandbox_confirm", "sandbox_block_network", "sec_enabled", "ai_check")
|
||||
# ---- đường tấn công ------------------------------------------------------
|
||||
|
||||
def test_o_trong_khong_mo_duoc_khoa(qapp, shown):
|
||||
"""Chưa đặt mật khẩu (sandbox_pw == "") thì ô nhập trống KHÔNG được mở khoá."""
|
||||
dlg = _dialog("")
|
||||
dlg.sandbox_pw_edit.setText("")
|
||||
|
||||
dlg._sandbox_unlock()
|
||||
|
||||
assert dlg._sandbox_unlocked is False
|
||||
dlg.deleteLater()
|
||||
|
||||
|
||||
# ---- hành vi mới: sửa được ngay, không cần mật khẩu ----------------------
|
||||
def test_go_bua_khi_chua_dat_mat_khau_cung_khong_mo_duoc(qapp, shown):
|
||||
"""Mật khẩu lưu rỗng thì KHÔNG chuỗi nào mở được, kể cả chuỗi khác rỗng."""
|
||||
dlg = _dialog("")
|
||||
dlg.sandbox_pw_edit.setText("bat ky")
|
||||
|
||||
@pytest.mark.parametrize("ten", _CONG_TAC)
|
||||
def test_cong_tac_sua_duoc_ngay_khi_mo_hop_thoai(qapp, ten):
|
||||
"""Đây là chính yêu cầu: không còn bước nhập mật khẩu nào chắn ở giữa."""
|
||||
dlg = _dialog()
|
||||
dlg._sandbox_unlock()
|
||||
|
||||
assert getattr(dlg, ten).isEnabled() is True, f"{ten} vẫn bị khoá"
|
||||
assert dlg._sandbox_unlocked is False
|
||||
dlg.deleteLater()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("ten", _CONG_TAC)
|
||||
def test_bat_tat_duoc_va_luu_dung_gia_tri(qapp, ten):
|
||||
"""Bật/tắt phải ăn vào widget — khoá cũ chặn đúng ở bước này."""
|
||||
dlg = _dialog()
|
||||
w = getattr(dlg, ten)
|
||||
def test_mat_khau_sai_khong_mo_duoc(qapp, shown):
|
||||
"""Đã đặt mật khẩu thì gõ sai vẫn khoá."""
|
||||
dlg = _dialog("K7MNP2QRSTVW")
|
||||
dlg.sandbox_pw_edit.setText("K7MNP2QRSTVX")
|
||||
|
||||
truoc = w.isChecked()
|
||||
w.setChecked(not truoc)
|
||||
assert w.isChecked() is (not truoc)
|
||||
w.setChecked(truoc)
|
||||
assert w.isChecked() is truoc
|
||||
dlg._sandbox_unlock()
|
||||
|
||||
assert dlg._sandbox_unlocked is False
|
||||
dlg.deleteLater()
|
||||
|
||||
|
||||
def test_khong_con_widget_mat_khau_nao(qapp):
|
||||
"""Ô nhập, nút Mở khoá và nhãn "Đang khoá" phải biến mất khỏi hộp thoại."""
|
||||
dlg = _dialog()
|
||||
# ---- thông báo phải phân biệt được hai tình huống -------------------------
|
||||
|
||||
for ten in ("sandbox_pw_edit", "sandbox_unlock_btn", "sandbox_locked_status",
|
||||
"sandbox_pw_label"):
|
||||
assert not hasattr(dlg, ten), f"{ten} vẫn còn trên hộp thoại"
|
||||
def test_chua_cau_hinh_bao_khac_voi_sai_mat_khau(qapp, shown):
|
||||
"""Hai nhánh phải nói hai chuyện khác nhau.
|
||||
|
||||
Người chưa từng đặt mật khẩu mà nhận "Password incorrect" sẽ gõ lại mãi một
|
||||
thứ không tồn tại. Không có bài này thì gộp hai nhánh về một thông báo chung
|
||||
vẫn xanh hết.
|
||||
"""
|
||||
from cowork_local.i18n import tr
|
||||
|
||||
dlg = _dialog("")
|
||||
dlg.sandbox_pw_edit.setText("")
|
||||
dlg._sandbox_unlock()
|
||||
chua_cau_hinh = list(shown)
|
||||
dlg.deleteLater()
|
||||
|
||||
shown.clear()
|
||||
dlg2 = _dialog("K7MNP2QRSTVW")
|
||||
dlg2.sandbox_pw_edit.setText("sai roi")
|
||||
dlg2._sandbox_unlock()
|
||||
sai_mat_khau = list(shown)
|
||||
dlg2.deleteLater()
|
||||
|
||||
assert len(chua_cau_hinh) == 1, "phải hiện đúng một thông báo"
|
||||
assert len(sai_mat_khau) == 1
|
||||
assert chua_cau_hinh[0][2] == tr("settings.sandbox_pw_unset_body")
|
||||
assert chua_cau_hinh[0][2] != sai_mat_khau[0][2], (
|
||||
"chưa cấu hình mật khẩu và sai mật khẩu phải là hai thông báo khác nhau")
|
||||
|
||||
|
||||
def test_khong_con_duong_mo_khoa_trong_ma(qapp):
|
||||
"""Hàm mở khoá và cờ trạng thái khoá không còn tồn tại."""
|
||||
import cowork_local.ui.settings_dialog as mod
|
||||
# ---- đường đi đúng vẫn phải chạy ----------------------------------------
|
||||
|
||||
dlg = _dialog()
|
||||
assert not hasattr(dlg, "_sandbox_unlock")
|
||||
assert not hasattr(dlg, "_sandbox_unlocked")
|
||||
assert not hasattr(dlg, "_sandbox_widgets")
|
||||
assert not hasattr(mod, "_sandbox_password_matches")
|
||||
def test_mat_khau_dung_van_mo_duoc(qapp, shown):
|
||||
"""Bản vá không được phá đường đi hợp lệ."""
|
||||
dlg = _dialog("K7MNP2QRSTVW")
|
||||
dlg.sandbox_pw_edit.setText("K7MNP2QRSTVW")
|
||||
|
||||
dlg._sandbox_unlock()
|
||||
|
||||
assert dlg._sandbox_unlocked is True
|
||||
dlg.deleteLater()
|
||||
|
||||
|
||||
# ---- guardrail: không ai khoá lại mà không sửa bài test này --------------
|
||||
@pytest.mark.parametrize("pw", ["mật khẩu", "パスワード", "sénhà-2026"])
|
||||
def test_mat_khau_co_dau_khong_lam_crash(qapp, shown, pw):
|
||||
"""``secrets.compare_digest`` ném TypeError nếu str có ký tự ngoài ASCII.
|
||||
|
||||
def test_ma_nguon_khong_con_khoa_nhom_sandbox():
|
||||
"""Chặn cả lớp lỗi: lần sau ai thêm lại ``setEnabled(False)`` cho nhóm này
|
||||
thì bài này đỏ ngay, không đợi có người mở app mới thấy."""
|
||||
src = (Path(__file__).resolve().parents[2]
|
||||
/ "ui" / "settings_dialog.py").read_text(encoding="utf-8")
|
||||
code = "\n".join(l for l in src.splitlines() if not l.strip().startswith("#"))
|
||||
App mặc định tiếng Việt và phục vụ khách Nhật, nên chữ có dấu trong ô mật
|
||||
khẩu là input bình thường. Phải so sánh trên bytes.
|
||||
"""
|
||||
dlg = _dialog(pw)
|
||||
dlg.sandbox_pw_edit.setText(pw)
|
||||
|
||||
for dau_hieu in ("_sandbox_unlock", "_sandbox_widgets", "_sandbox_unlocked",
|
||||
"sandbox_pw"):
|
||||
assert dau_hieu not in code, f"khoá sandbox đã quay lại: {dau_hieu}"
|
||||
dlg._sandbox_unlock() # không được ném TypeError
|
||||
|
||||
assert dlg._sandbox_unlocked is True
|
||||
dlg.deleteLater()
|
||||
|
||||
|
||||
def test_phep_quet_thuc_su_doc_duoc_file():
|
||||
"""Lưới an toàn: đổi tên file làm bài trên quét rỗng mà vẫn xanh."""
|
||||
src = (Path(__file__).resolve().parents[2]
|
||||
/ "ui" / "settings_dialog.py").read_text(encoding="utf-8")
|
||||
def test_mat_khau_co_dau_sai_thi_van_khoa(qapp, shown):
|
||||
"""Chữ có dấu không được biến thành đường mở khoá dễ dãi."""
|
||||
dlg = _dialog("mật khẩu")
|
||||
dlg.sandbox_pw_edit.setText("mat khau")
|
||||
|
||||
assert "class SettingsDialog" in src
|
||||
assert len(src) > 2000, f"chỉ đọc được {len(src)} ký tự — đường dẫn đã hỏng"
|
||||
dlg._sandbox_unlock()
|
||||
|
||||
assert dlg._sandbox_unlocked is False
|
||||
dlg.deleteLater()
|
||||
|
||||
|
||||
# ---- hàm so khớp, gọi thẳng ----------------------------------------------
|
||||
|
||||
@pytest.mark.parametrize("entered,stored,expected", [
|
||||
("", "", False), # cả hai rỗng
|
||||
("", "K7MNP2QRSTVW", False), # ô nhập rỗng
|
||||
("K7MNP2QRSTVW", "", False), # chưa đặt mật khẩu — nhánh phòng thủ
|
||||
("K7MNP2QRSTVW", "K7MNP2QRSTVW", True),
|
||||
("mật khẩu", "mật khẩu", True), # ngoài ASCII
|
||||
("mật khẩu", "mat khau", False),
|
||||
])
|
||||
def test_ham_so_khop(entered, stored, expected):
|
||||
"""Gọi thẳng ``_sandbox_password_matches`` — phủ cả nhánh mà call site đã
|
||||
chặn trước bằng return sớm."""
|
||||
from cowork_local.ui.settings_dialog import _sandbox_password_matches
|
||||
assert _sandbox_password_matches(entered, stored) is expected
|
||||
|
||||
|
||||
# ---- chặn cả lớp lỗi -----------------------------------------------------
|
||||
|
||||
#: ``.get("<khoá kiểu credential>", "<literal khác rỗng>")`` — mặc định trông có
|
||||
#: vẻ an toàn nhưng thực ra là credential nằm trong mã nguồn. Nó cũng là code
|
||||
#: chết: cấu hình đã deep-merge với DEFAULT_CONFIG nên key luôn tồn tại.
|
||||
#:
|
||||
#: Cố ý KHÔNG bắt ``key`` và ``code`` trần: ``it.get("key", "?")`` của Jira
|
||||
#: (``core/jira_tool.py``) là mã issue, không phải credential. Danh sách dưới đây
|
||||
#: chỉ gồm tên đã mang nghĩa bí mật.
|
||||
_CREDENTIAL_FALLBACK = re.compile(
|
||||
r'\.get\(\s*["\'][a-z_]*'
|
||||
r'(?:pw|passwd|password|secret|token|api_key|unlock_code|access_code)'
|
||||
r'[a-z_]*["\']\s*,\s*["\'][^"\']+["\']'
|
||||
)
|
||||
|
||||
#: Quét CHÉO mọi thư mục nguồn, không chỉ tầng giao diện. Sai sót gốc của commit
|
||||
#: ``3827552`` là sửa ``config.py`` mà quên bản sao trong ``ui/`` — tức là lỗi đi
|
||||
#: xuyên thư mục, nên phép quét cũng phải đi xuyên thư mục.
|
||||
_SCANNED = (
|
||||
"ui", "presentation", "core", "infrastructure", "application", "domain",
|
||||
"mcp_servers", "providers", "security", "theme", "config.py", "state.py",
|
||||
)
|
||||
|
||||
|
||||
def test_khong_con_fallback_credential_trong_ma_nguon():
|
||||
"""Không file nguồn nào được đặt credential làm giá trị mặc định của ``.get()``."""
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
offenders = []
|
||||
for name in _SCANNED:
|
||||
target = root / name
|
||||
if target.is_file():
|
||||
files = [target]
|
||||
elif target.is_dir():
|
||||
files = [p for p in target.rglob("*.py") if "__pycache__" not in p.parts]
|
||||
else: # thư mục bị đổi tên/xoá
|
||||
continue
|
||||
for path in files:
|
||||
for lineno, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
|
||||
if _CREDENTIAL_FALLBACK.search(line):
|
||||
offenders.append(
|
||||
f"{path.relative_to(root).as_posix()}:{lineno}: {line.strip()}")
|
||||
|
||||
assert not offenders, "credential nằm trong mã nguồn:\n " + "\n ".join(offenders)
|
||||
|
||||
|
||||
def test_phep_quet_thuc_su_nhin_thay_file():
|
||||
"""Lưới an toàn cho bài trên: đổi tên thư mục làm nó quét rỗng mà vẫn xanh."""
|
||||
root = Path(__file__).resolve().parents[2]
|
||||
seen = sum(
|
||||
1 for name in _SCANNED
|
||||
for _ in ([root / name] if (root / name).is_file()
|
||||
else (root / name).rglob("*.py") if (root / name).is_dir() else [])
|
||||
)
|
||||
assert seen > 200, f"chỉ quét được {seen} file — phạm vi quét đã hỏng"
|
||||
|
||||
@@ -128,24 +128,19 @@ def test_bam_project_tren_thanh_menu_an_ngay_lan_dau(window):
|
||||
|
||||
|
||||
def test_khi_cong_project_MO_thi_ha_canh_o_cowork_va_vet_sang_theo(window):
|
||||
"""Nhánh của người dùng ĐÃ chọn một project — nhánh mà bug được báo.
|
||||
"""Nhánh của người dùng ĐÃ có project — nhánh mà bug được báo.
|
||||
|
||||
Trước đây bài này mở cổng bằng cửa sau ``setTabVisible(True)`` vì môi trường
|
||||
test không có project nào (``core/projects.py`` ghi vào ``~/.cowork_local``
|
||||
thật, nên test không tạo project). Cửa sau đó hết tác dụng từ khi cổng được
|
||||
điều khiển bằng ``_current_id``: ``refresh()``/``goto_all_projects()`` đóng
|
||||
lại ngay. Giờ mở cổng bằng đúng đường thật — chọn một project — và bỏ qua
|
||||
bài này trên máy chưa có project nào.
|
||||
Môi trường test không có project nào (cố ý: ``core/projects.py`` ghi vào
|
||||
``~/.cowork_local`` thật). Mở cổng bằng tay để đi đúng nhánh đó mà không
|
||||
phải tạo project trên đĩa.
|
||||
"""
|
||||
from PySide6.QtCore import Qt
|
||||
|
||||
ws = window.workspace
|
||||
if ws._cowork_tab_idx < 0:
|
||||
pytest.skip("bản dựng này không có sub-tab Cowork")
|
||||
if ws.project_list.count() == 0:
|
||||
pytest.skip("máy chạy test chưa có project nào — cổng không mở được")
|
||||
|
||||
ws.project_list.setCurrentRow(0)
|
||||
ws.tabs.setTabVisible(ws._cowork_tab_idx, True)
|
||||
try:
|
||||
window.goto_all_projects()
|
||||
|
||||
@@ -159,5 +154,5 @@ def test_khi_cong_project_MO_thi_ha_canh_o_cowork_va_vet_sang_theo(window):
|
||||
f"nội dung ở Cowork ({ws._cowork_tab_idx}) "
|
||||
f"nhưng thanh menu sáng ở {data.get('sub')}")
|
||||
finally:
|
||||
ws.project_list.setCurrentRow(-1) # đóng cổng lại đúng đường thật
|
||||
ws.tabs.setTabVisible(ws._cowork_tab_idx, False)
|
||||
window.goto_all_projects()
|
||||
|
||||
+1
-1
@@ -49,7 +49,7 @@ QWidget#contentArea { background: $bg; }
|
||||
carries a 2px accent marker, so which section you are in survives even at a
|
||||
glance or for anyone who cannot separate the two greys. */
|
||||
QWidget#navWrap QTreeWidget::item, QWidget#navWrap QListWidget::item {
|
||||
padding: 6px 10px; border-radius: ${radius}px;
|
||||
padding: 6px 4px; border-radius: ${radius}px;
|
||||
}
|
||||
QWidget#navWrap QTreeWidget::item:hover, QWidget#navWrap QListWidget::item:hover {
|
||||
background: $nav_hover;
|
||||
|
||||
@@ -26,7 +26,7 @@ from capture_screens import _apply_theme, _isolate_home, _load_fonts # noqa: E4
|
||||
SETTINGS_FIELDS = [
|
||||
"language_combo", "theme_combo", "tray_chk", "notify_chk",
|
||||
"provider_combo", "prov_base", "prov_key", "prov_model",
|
||||
"sandbox_confirm",
|
||||
"sandbox_pw_edit", "sandbox_unlock_btn", "sandbox_confirm",
|
||||
"sandbox_block_network", "sec_enabled", "ai_check",
|
||||
]
|
||||
TASK_FIELDS = [
|
||||
|
||||
+18
-25
@@ -74,30 +74,22 @@ def main() -> int:
|
||||
|
||||
main_rows, bottom_rows = rows(win.nav), rows(win.nav_bottom)
|
||||
n_total = len(main_rows) + len(bottom_rows)
|
||||
# Workspace gop cac sub-view DANG MO CONG + Schedule, roi Dashboard +
|
||||
# Monitoring. Cowork/GraphRAG chi co mat khi da chon mot project, nen so
|
||||
# dong doi theo cong thay vi co dinh 6.
|
||||
mo_cong = sum(1 for _l, _i, _ic, on in win.workspace.nav_entries() if on)
|
||||
cho_chinh = mo_cong + 1
|
||||
if len(main_rows) != cho_chinh:
|
||||
fails.append(f"thanh chinh co {len(main_rows)} dong, cho {cho_chinh}")
|
||||
# Five Workspace sub-views + Schedule, then Dashboard + Monitoring.
|
||||
if len(main_rows) != 6:
|
||||
fails.append(f"thanh chinh co {len(main_rows)} dong, cho 6")
|
||||
if len(bottom_rows) != 2:
|
||||
fails.append(f"nhom day co {len(bottom_rows)} dong, cho 2")
|
||||
if any(sub is not None for _l, _p, sub, _o in bottom_rows):
|
||||
fails.append("nhom day khong duoc mang sub-tab")
|
||||
|
||||
# Hang bi cong project dong thi BO HAN khoi menu; hang dang mo phai co mat.
|
||||
# The two gated rows must be PRESENT (that is the point) — greyed is fine.
|
||||
labels = [r[0] for r in main_rows]
|
||||
ws_mo = [lab for lab, _i, _ic, on in win.workspace.nav_entries() if on]
|
||||
ws_dong = [lab for lab, _i, _ic, on in win.workspace.nav_entries() if not on]
|
||||
for lab in ws_mo:
|
||||
ws_labels = [lab for lab, _i, _ic, _on in win.workspace.nav_entries()]
|
||||
for lab in ws_labels:
|
||||
if lab not in labels:
|
||||
fails.append(f"mat dong Workspace dang mo cong: {lab}")
|
||||
for lab in ws_dong:
|
||||
if lab in labels:
|
||||
fails.append(f"dong Workspace dang dong cong van tren menu: {lab}")
|
||||
print(f"man Workspace dang mo cong tren menu: {all(l in labels for l in ws_mo)}"
|
||||
f" ({', '.join(ws_mo) or 'khong co'})")
|
||||
fails.append(f"mat dong Workspace: {lab}")
|
||||
print(f"du 5 man Workspace tren thanh menu: {all(l in labels for l in ws_labels)}"
|
||||
f" ({', '.join(ws_labels)})")
|
||||
|
||||
# Highlight must follow the content for every row, both ways round.
|
||||
# Re-fetch items by index every time: navigating can rebuild the rail, which
|
||||
@@ -153,18 +145,19 @@ def main() -> int:
|
||||
if ws_strip:
|
||||
fails.append("dai tab Workspace hien lai — trung voi thanh menu")
|
||||
|
||||
# Yeu cau: chua chon project thi Cowork/GraphRAG khong duoc hien tren menu.
|
||||
# The whole point of the change: with no project selected the two gated rows
|
||||
# must stay in place, greyed — not vanish and resize the menu.
|
||||
win.workspace._update_tab_visibility(False)
|
||||
app.processEvents()
|
||||
gated = rows(win.nav)
|
||||
nhan_gated = [lab for lab, _p, _s, _on in gated]
|
||||
off = [lab for lab, _p, _s, on in gated if not on]
|
||||
print()
|
||||
print(f"chua chon project : con {len(gated)} dong ({', '.join(nhan_gated)})")
|
||||
for lab in ("Cowork", "GraphRAG"):
|
||||
if lab in nhan_gated:
|
||||
fails.append(f"chua chon project ma {lab} van tren menu")
|
||||
if any(not on for _l, _p, _s, on in gated):
|
||||
fails.append("con dong bi mo tren menu — dang le phai bo han")
|
||||
print(f"chua chon project : van du {len(gated)} dong, mo: {off or 'khong'}")
|
||||
if len(gated) != len(main_rows):
|
||||
fails.append(f"chua chon project thi thanh menu con {len(gated)} dong "
|
||||
f"(truoc {len(main_rows)}) — item van bien mat")
|
||||
if len(off) != 2:
|
||||
fails.append(f"cho 2 dong bi mo (Cowork, GraphRAG), thay {len(off)}")
|
||||
|
||||
# --- rail header: project picker + new chat (Phase A) ------------------
|
||||
print()
|
||||
|
||||
@@ -1,212 +0,0 @@
|
||||
"""DF-007 — browse OneDrive/SharePoint via Microsoft Graph and pick a folder
|
||||
to use as (a local mirror of) a project's working directory. See
|
||||
``core/cloud_workspace_sync.py`` for the mirror/sync side and
|
||||
``ui/ms365_signin_dialog.py`` for the sign-in gate this calls first.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import (
|
||||
QDialog, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton,
|
||||
QRadioButton, QTreeWidget, QTreeWidgetItem, QVBoxLayout,
|
||||
)
|
||||
|
||||
from ..core import ms365_auth
|
||||
from ..core import ms365_graph as graph
|
||||
from ..i18n import tr
|
||||
from .icons import icon
|
||||
from .ms365_signin_dialog import ensure_signed_in
|
||||
|
||||
_ITEM_KIND = Qt.UserRole
|
||||
_ITEM_NAME = Qt.UserRole + 1
|
||||
|
||||
|
||||
class CloudFolderPickerDialog(QDialog):
|
||||
"""Chọn "OneDrive của tôi" hoặc tìm 1 site SharePoint, rồi duyệt thư mục
|
||||
con của nó; "Chọn thư mục này" trả về thư mục ĐANG HIỂN THỊ (không phải
|
||||
dòng đang bôi đen — giống hành vi ``QFileDialog`` khi đang ở trong 1
|
||||
thư mục)."""
|
||||
|
||||
def __init__(self, config, parent=None):
|
||||
super().__init__(parent)
|
||||
self._config = config
|
||||
ms365 = (config.ms365 if config is not None else {}) or {}
|
||||
self._token = ms365_auth.get_access_token(
|
||||
ms365.get("tenant_id", ""), ms365.get("client_id", ""))
|
||||
self._site_id = ""
|
||||
self._site_name = ""
|
||||
self._path_parts: list[str] = [] # relative path segments from the drive root
|
||||
|
||||
self.setWindowTitle(tr("cloud_picker.title"))
|
||||
self.setModal(True)
|
||||
self.resize(520, 480)
|
||||
layout = QVBoxLayout(self)
|
||||
|
||||
source_row = QHBoxLayout()
|
||||
self._onedrive_radio = QRadioButton(tr("cloud_picker.source_onedrive"))
|
||||
self._onedrive_radio.setChecked(True)
|
||||
self._onedrive_radio.toggled.connect(self._on_source_toggled)
|
||||
self._sharepoint_radio = QRadioButton(tr("cloud_picker.source_sharepoint"))
|
||||
source_row.addWidget(self._onedrive_radio)
|
||||
source_row.addWidget(self._sharepoint_radio)
|
||||
source_row.addStretch(1)
|
||||
layout.addLayout(source_row)
|
||||
|
||||
search_row = QHBoxLayout()
|
||||
self._site_search = QLineEdit()
|
||||
self._site_search.setPlaceholderText(tr("cloud_picker.search_sites_placeholder"))
|
||||
self._site_search.setEnabled(False)
|
||||
self._site_search.returnPressed.connect(self._search_sites)
|
||||
self._search_btn = QPushButton(tr("cloud_picker.search_btn"))
|
||||
self._search_btn.setEnabled(False)
|
||||
self._search_btn.clicked.connect(self._search_sites)
|
||||
search_row.addWidget(self._site_search, 1)
|
||||
search_row.addWidget(self._search_btn)
|
||||
layout.addLayout(search_row)
|
||||
|
||||
self._path_lbl = QLabel()
|
||||
self._path_lbl.setWordWrap(True)
|
||||
layout.addWidget(self._path_lbl)
|
||||
|
||||
self._tree = QTreeWidget()
|
||||
self._tree.setHeaderHidden(True)
|
||||
self._tree.itemDoubleClicked.connect(self._on_item_activated)
|
||||
layout.addWidget(self._tree, 1)
|
||||
|
||||
btn_row = QHBoxLayout()
|
||||
self._choose_btn = QPushButton(tr("cloud_picker.choose_here"))
|
||||
self._choose_btn.setObjectName("primary")
|
||||
self._choose_btn.setIcon(icon("cloud"))
|
||||
self._choose_btn.clicked.connect(self.accept)
|
||||
self._cancel_btn = QPushButton(tr("cloud_picker.cancel"))
|
||||
self._cancel_btn.clicked.connect(self.reject)
|
||||
btn_row.addStretch(1)
|
||||
btn_row.addWidget(self._cancel_btn)
|
||||
btn_row.addWidget(self._choose_btn)
|
||||
layout.addLayout(btn_row)
|
||||
|
||||
self._refresh_path_label()
|
||||
self._reload()
|
||||
|
||||
# ---- source switching -------------------------------------------------
|
||||
def _on_source_toggled(self, _checked: bool) -> None:
|
||||
is_sharepoint = self._sharepoint_radio.isChecked()
|
||||
self._site_search.setEnabled(is_sharepoint)
|
||||
self._search_btn.setEnabled(is_sharepoint)
|
||||
self._choose_btn.setEnabled(not is_sharepoint or bool(self._site_id))
|
||||
if not is_sharepoint:
|
||||
self._site_id = ""
|
||||
self._site_name = ""
|
||||
self._path_parts = []
|
||||
self._refresh_path_label()
|
||||
self._reload()
|
||||
|
||||
def _search_sites(self) -> None:
|
||||
query = self._site_search.text().strip()
|
||||
if not query:
|
||||
return
|
||||
self._tree.clear()
|
||||
try:
|
||||
sites = graph.list_sharepoint_sites(self._token, query)
|
||||
except graph.Ms365GraphError as exc:
|
||||
QMessageBox.warning(self, tr("cloud_picker.title"),
|
||||
tr("cloud_picker.load_failed", err=str(exc)))
|
||||
return
|
||||
if not sites:
|
||||
item = QTreeWidgetItem([tr("cloud_picker.no_sites")])
|
||||
item.setData(0, _ITEM_KIND, "empty")
|
||||
self._tree.addTopLevelItem(item)
|
||||
return
|
||||
for site in sites:
|
||||
item = QTreeWidgetItem([site.get("displayName") or site.get("name") or site.get("id", "")])
|
||||
item.setIcon(0, icon("globe"))
|
||||
item.setData(0, _ITEM_KIND, "site")
|
||||
item.setData(0, _ITEM_NAME, site.get("id", ""))
|
||||
item.setData(0, Qt.UserRole + 2, site.get("displayName") or site.get("name") or "")
|
||||
self._tree.addTopLevelItem(item)
|
||||
self._choose_btn.setEnabled(False) # must pick a site before choosing a folder
|
||||
|
||||
def _on_item_activated(self, item: QTreeWidgetItem, _col: int) -> None:
|
||||
kind = item.data(0, _ITEM_KIND)
|
||||
if kind == "site":
|
||||
self._site_id = item.data(0, _ITEM_NAME)
|
||||
self._site_name = item.data(0, Qt.UserRole + 2)
|
||||
self._path_parts = []
|
||||
self._choose_btn.setEnabled(True)
|
||||
self._refresh_path_label()
|
||||
self._reload()
|
||||
elif kind == "up":
|
||||
self._path_parts.pop()
|
||||
self._refresh_path_label()
|
||||
self._reload()
|
||||
elif kind == "folder":
|
||||
self._path_parts.append(item.data(0, _ITEM_NAME))
|
||||
self._refresh_path_label()
|
||||
self._reload()
|
||||
# kind == "file": not navigable, double-click does nothing
|
||||
|
||||
# ---- listing ------------------------------------------------------------
|
||||
def _current_remote_path(self) -> str:
|
||||
return "/".join(self._path_parts)
|
||||
|
||||
def _refresh_path_label(self) -> None:
|
||||
if self._sharepoint_radio.isChecked():
|
||||
root = self._site_name or tr("cloud_picker.source_sharepoint")
|
||||
else:
|
||||
root = tr("cloud_picker.source_onedrive")
|
||||
parts = "/".join([root] + self._path_parts)
|
||||
self._path_lbl.setText(parts)
|
||||
|
||||
def _reload(self) -> None:
|
||||
self._tree.clear()
|
||||
if self._sharepoint_radio.isChecked() and not self._site_id:
|
||||
return # waiting on a site search/selection
|
||||
if self._path_parts:
|
||||
up = QTreeWidgetItem([tr("cloud_picker.up")])
|
||||
up.setData(0, _ITEM_KIND, "up")
|
||||
self._tree.addTopLevelItem(up)
|
||||
try:
|
||||
if self._sharepoint_radio.isChecked():
|
||||
children = graph.list_sharepoint_files(
|
||||
self._token, self._site_id, self._current_remote_path())
|
||||
else:
|
||||
children = graph.list_onedrive_files(self._token, self._current_remote_path())
|
||||
except graph.Ms365GraphError as exc:
|
||||
QMessageBox.warning(self, tr("cloud_picker.title"),
|
||||
tr("cloud_picker.load_failed", err=str(exc)))
|
||||
return
|
||||
for entry in children:
|
||||
name = entry.get("name", "")
|
||||
if not name:
|
||||
continue
|
||||
is_folder = "folder" in entry
|
||||
item = QTreeWidgetItem([name])
|
||||
item.setIcon(0, icon("folder" if is_folder else "file"))
|
||||
item.setData(0, _ITEM_KIND, "folder" if is_folder else "file")
|
||||
item.setData(0, _ITEM_NAME, name)
|
||||
self._tree.addTopLevelItem(item)
|
||||
|
||||
# ---- result ---------------------------------------------------------
|
||||
def cloud_source(self) -> dict:
|
||||
"""Chỉ gọi sau khi ``exec()`` trả về ``QDialog.Accepted``."""
|
||||
if self._sharepoint_radio.isChecked():
|
||||
return {
|
||||
"provider": "sharepoint", "site_id": self._site_id,
|
||||
"site_name": self._site_name, "remote_path": self._current_remote_path(),
|
||||
}
|
||||
return {"provider": "onedrive", "site_id": "", "site_name": "",
|
||||
"remote_path": self._current_remote_path()}
|
||||
|
||||
|
||||
def pick_cloud_folder(parent, config) -> Optional[dict]:
|
||||
"""Đảm bảo đã đăng nhập MS365 rồi mở dialog duyệt; trả về ``cloud_source``
|
||||
dict nếu người dùng xác nhận một thư mục, ``None`` nếu hủy hoặc chưa đăng
|
||||
nhập."""
|
||||
if not ensure_signed_in(parent, config):
|
||||
return None
|
||||
dialog = CloudFolderPickerDialog(config, parent)
|
||||
if dialog.exec() == QDialog.Accepted:
|
||||
return dialog.cloud_source()
|
||||
return None
|
||||
@@ -96,20 +96,11 @@ class FileEditDialog(QDialog):
|
||||
|
||||
# ---- AI edit row ---------------------------------------------------
|
||||
ai_row = QHBoxLayout()
|
||||
ai_row.setContentsMargins(0, 6, 0, 2)
|
||||
self.instruction_edit = QLineEdit()
|
||||
self.instruction_edit.setPlaceholderText(tr("fileedit.instruction_placeholder"))
|
||||
self.instruction_edit.setMinimumHeight(38)
|
||||
self.instruction_edit.setStyleSheet(
|
||||
"QLineEdit { padding: 8px 12px; font-size: 13px; border-radius: 6px; }"
|
||||
)
|
||||
self.instruction_edit.returnPressed.connect(self._ai_edit)
|
||||
self.ai_btn = QPushButton(tr("fileedit.ai_btn"))
|
||||
self.ai_btn.setIcon(icon("sparkle"))
|
||||
self.ai_btn.setMinimumHeight(38)
|
||||
self.ai_btn.setStyleSheet(
|
||||
"QPushButton { padding: 0 16px; font-size: 13px; border-radius: 6px; }"
|
||||
)
|
||||
self.ai_btn.clicked.connect(self._ai_edit)
|
||||
ai_row.addWidget(self.instruction_edit, 1)
|
||||
ai_row.addWidget(self.ai_btn)
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
"""Microsoft 365 sign-in dialog (DF-007) — thin UI over the working device-code
|
||||
flow in ``core/ms365_auth.py``. There was an older MS365 sign-in UI in this
|
||||
app; it was removed as dead code (no entry point — see
|
||||
``ui/settings_dialog.py`` module docstring) before this feature existed, so
|
||||
this is a fresh, small dialog rather than a resurrection of that one.
|
||||
|
||||
Usage: ``if ensure_signed_in(parent, ctx.config): ...`` — returns ``True``
|
||||
immediately (no dialog shown) when already signed in.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import webbrowser
|
||||
|
||||
from PySide6.QtCore import QThread, Signal
|
||||
from PySide6.QtWidgets import (
|
||||
QDialog, QHBoxLayout, QLabel, QMessageBox, QPushButton, QVBoxLayout,
|
||||
)
|
||||
|
||||
from ..core import ms365_auth
|
||||
from ..i18n import tr
|
||||
|
||||
|
||||
class _SignInWorker(QThread):
|
||||
"""Chạy ``sign_in_device_code()`` (blocking, poll tới khi xong/hết hạn) ở
|
||||
luồng nền — xem ``core/worker.py::AgentWorker`` cho cùng idiom (bắt hết
|
||||
exception, phát signal thay vì để lỗi giết luồng âm thầm)."""
|
||||
code_ready = Signal(dict)
|
||||
finished_ok = Signal(dict)
|
||||
failed = Signal(str)
|
||||
|
||||
def __init__(self, config, parent=None):
|
||||
super().__init__(parent)
|
||||
self._config = config
|
||||
|
||||
def run(self) -> None: # noqa: D401
|
||||
try:
|
||||
result = ms365_auth.sign_in(lambda flow: self.code_ready.emit(flow), self._config)
|
||||
self.finished_ok.emit(result or {})
|
||||
except Exception as exc: # noqa: BLE001 - surfaced to the UI, never crashes the thread
|
||||
self.failed.emit(str(exc))
|
||||
|
||||
|
||||
class Ms365SignInDialog(QDialog):
|
||||
"""Modal: hiện user_code + verification_uri, tự mở trình duyệt, đóng lại
|
||||
khi đăng nhập xong (hoặc người dùng bấm Hủy)."""
|
||||
|
||||
def __init__(self, config, parent=None):
|
||||
super().__init__(parent)
|
||||
self._config = config
|
||||
self._worker: _SignInWorker | None = None
|
||||
self.setWindowTitle(tr("ms365_signin.title"))
|
||||
self.setModal(True)
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
self._intro_lbl = QLabel(tr("ms365_signin.intro"))
|
||||
self._intro_lbl.setWordWrap(True)
|
||||
layout.addWidget(self._intro_lbl)
|
||||
|
||||
self._code_lbl = QLabel()
|
||||
self._code_lbl.setWordWrap(True)
|
||||
self._code_lbl.hide()
|
||||
layout.addWidget(self._code_lbl)
|
||||
|
||||
self._open_link_btn = QPushButton(tr("ms365_signin.open_link"))
|
||||
self._open_link_btn.hide()
|
||||
self._open_link_btn.clicked.connect(self._open_link)
|
||||
layout.addWidget(self._open_link_btn)
|
||||
|
||||
self._error_lbl = QLabel()
|
||||
self._error_lbl.setWordWrap(True)
|
||||
self._error_lbl.setStyleSheet("color: #c0392b;")
|
||||
self._error_lbl.hide()
|
||||
layout.addWidget(self._error_lbl)
|
||||
|
||||
btn_row = QHBoxLayout()
|
||||
self._signin_btn = QPushButton(tr("ms365_signin.button"))
|
||||
self._signin_btn.setObjectName("primary")
|
||||
self._signin_btn.clicked.connect(self._start_sign_in)
|
||||
self._cancel_btn = QPushButton(tr("ms365_signin.cancel"))
|
||||
self._cancel_btn.clicked.connect(self.reject)
|
||||
btn_row.addStretch(1)
|
||||
btn_row.addWidget(self._cancel_btn)
|
||||
btn_row.addWidget(self._signin_btn)
|
||||
layout.addLayout(btn_row)
|
||||
|
||||
self._verification_uri = ""
|
||||
|
||||
def _open_link(self) -> None:
|
||||
if self._verification_uri:
|
||||
webbrowser.open(self._verification_uri)
|
||||
|
||||
def _start_sign_in(self) -> None:
|
||||
self._signin_btn.setEnabled(False)
|
||||
self._signin_btn.setText(tr("ms365_signin.signing_in"))
|
||||
self._error_lbl.hide()
|
||||
self._worker = _SignInWorker(self._config, self)
|
||||
self._worker.code_ready.connect(self._on_code_ready)
|
||||
self._worker.finished_ok.connect(self._on_finished_ok)
|
||||
self._worker.failed.connect(self._on_failed)
|
||||
self._worker.start()
|
||||
|
||||
def _on_code_ready(self, flow: dict) -> None:
|
||||
self._verification_uri = flow.get("verification_uri_complete") or flow.get(
|
||||
"verification_uri", "")
|
||||
self._code_lbl.setText(
|
||||
tr("ms365_signin.code_hint", url=flow.get("verification_uri", "")) +
|
||||
f"\n\n{flow.get('user_code', '')}")
|
||||
self._code_lbl.show()
|
||||
self._open_link_btn.show()
|
||||
if self._verification_uri:
|
||||
webbrowser.open(self._verification_uri)
|
||||
|
||||
def _on_finished_ok(self, _result: dict) -> None:
|
||||
self.accept()
|
||||
|
||||
def _on_failed(self, err: str) -> None:
|
||||
self._signin_btn.setEnabled(True)
|
||||
self._signin_btn.setText(tr("ms365_signin.button"))
|
||||
self._error_lbl.setText(tr("ms365_signin.failed", err=err))
|
||||
self._error_lbl.show()
|
||||
|
||||
def reject(self) -> None:
|
||||
# NOTE: MSAL's acquire_token_by_device_flow() has no cancellation hook,
|
||||
# so a worker already polling keeps polling in the background until it
|
||||
# times out on its own (a few minutes) — closing this dialog just stops
|
||||
# the UI from waiting on it. Its late signals are harmless no-ops
|
||||
# against an already-closed (but not destroyed) dialog.
|
||||
super().reject()
|
||||
|
||||
|
||||
def ensure_signed_in(parent, config) -> bool:
|
||||
"""True nếu đã (hoặc vừa) đăng nhập MS365; False nếu người dùng hủy hoặc
|
||||
đăng nhập thất bại và đóng dialog."""
|
||||
if ms365_auth.is_signed_in(config):
|
||||
return True
|
||||
dialog = Ms365SignInDialog(config, parent)
|
||||
return dialog.exec() == QDialog.Accepted
|
||||
+83
-6
@@ -13,17 +13,20 @@ chưa từng được gán nên gọi vào là AttributeError.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtGui import QGuiApplication
|
||||
from PySide6.QtWidgets import (
|
||||
QCheckBox, QComboBox, QDialog, QDialogButtonBox, QFileDialog, QFormLayout,
|
||||
QGroupBox, QHBoxLayout, QListWidget, QListWidgetItem,
|
||||
QScrollArea, QSpinBox,
|
||||
QGroupBox, QHBoxLayout, QLabel, QLineEdit, QListWidget, QListWidgetItem,
|
||||
QMessageBox, QPushButton, QScrollArea, QSpinBox,
|
||||
QTreeWidgetItem, QVBoxLayout, QWidget,
|
||||
)
|
||||
|
||||
from ..i18n import tr
|
||||
from .dialog_buttons import dialog_buttons
|
||||
from .icons import IconLabel
|
||||
from .widgets import ToggleSwitch
|
||||
|
||||
|
||||
@@ -34,6 +37,25 @@ from ..presentation.settings.routing_settings_widget import RoutingSettingsWidge
|
||||
from ..presentation.settings.about_widget import AboutSettingsWidget
|
||||
|
||||
|
||||
def _sandbox_password_matches(entered: str, stored: str) -> bool:
|
||||
"""Whether ``entered`` unlocks the Sandbox Security group.
|
||||
|
||||
An empty ``stored`` must never match. ``DEFAULT_CONFIG`` ships
|
||||
``agent_security.sandbox_pw = ""`` and the config handed to this dialog is
|
||||
always deep-merged with those defaults, so a plain ``entered == stored``
|
||||
accepts an empty field on every install that never set a password. The MS365
|
||||
unlock guards the same way — see ``json_config_repository.unlock_ms365``.
|
||||
|
||||
Both sides are compared as UTF-8 bytes, not as ``str``:
|
||||
``compare_digest`` raises ``TypeError`` on ``str`` holding anything outside
|
||||
ASCII, and this app defaults to Vietnamese and ships to Japanese customers,
|
||||
so an accented password is ordinary input rather than an edge case.
|
||||
"""
|
||||
if not entered or not stored:
|
||||
return False
|
||||
return secrets.compare_digest(entered.encode("utf-8"), stored.encode("utf-8"))
|
||||
|
||||
|
||||
class SettingsDialog(QDialog):
|
||||
"""Hộp thoại Cài đặt: cột mục lục bên trái, các trang bên phải
|
||||
(Nhà cung cấp · Connectors · Định tuyến · Tham số · Chung).
|
||||
@@ -86,10 +108,29 @@ class SettingsDialog(QDialog):
|
||||
self.sandbox_group = QGroupBox(tr("settings.group.sandbox"))
|
||||
sbl = QVBoxLayout(self.sandbox_group)
|
||||
|
||||
# Nhóm này KHÔNG còn khoá bằng mật khẩu: bốn công tắc dưới đây bật/tắt
|
||||
# tự do. Khoá cũ chỉ là rào chống bấm nhầm ở phía giao diện, không phải
|
||||
# cơ chế bảo mật thật (rào thật nằm ở sandbox lúc chạy lệnh), nên bỏ đi
|
||||
# theo yêu cầu thay vì giữ một bước nhập mật khẩu không bảo vệ được gì.
|
||||
# --- Password protection for Sandbox Security (at top) ---
|
||||
self.sandbox_pw_label = IconLabel("lock", tr("settings.sandbox_pw_label"))
|
||||
sbl.addWidget(self.sandbox_pw_label)
|
||||
|
||||
pw_row = QHBoxLayout()
|
||||
self.sandbox_pw_edit = QLineEdit("")
|
||||
self.sandbox_pw_edit.setPlaceholderText(tr("settings.sandbox_pw_placeholder"))
|
||||
self.sandbox_pw_edit.setEchoMode(QLineEdit.Password)
|
||||
pw_row.addWidget(self.sandbox_pw_edit, 1)
|
||||
self.sandbox_unlock_btn = QPushButton(tr("settings.sandbox_unlock_btn"))
|
||||
self.sandbox_unlock_btn.clicked.connect(self._sandbox_unlock)
|
||||
pw_row.addWidget(self.sandbox_unlock_btn)
|
||||
self.sandbox_locked_status = IconLabel("lock", tr("settings.sandbox_locked"), color="#c00")
|
||||
self.sandbox_locked_status.text_label().setStyleSheet("color: #c00; font-weight: bold;")
|
||||
pw_row.addWidget(self.sandbox_locked_status)
|
||||
sbl.addLayout(pw_row)
|
||||
self._sandbox_unlocked = False # Start LOCKED — must enter password first
|
||||
self._sandbox_pw = sec.get("sandbox_pw", "")
|
||||
|
||||
# Separator line between pw section and sandbox settings
|
||||
pw_sep = QLabel("────────────────")
|
||||
sbl.addWidget(pw_sep)
|
||||
|
||||
self.sandbox_confirm = ToggleSwitch(tr("settings.sandbox_confirm_commands"))
|
||||
self.sandbox_confirm.setChecked(bool(sec.get("cowork_confirm_commands", False)))
|
||||
self.sandbox_confirm.setToolTip(tr("settings.sandbox_confirm_commands_tooltip"))
|
||||
@@ -119,6 +160,14 @@ class SettingsDialog(QDialog):
|
||||
# Resource limits (CPU/Memory/Disk I/O) moved to the Parameter group
|
||||
# below — see _param_section("settings.group.sandbox_limits").
|
||||
|
||||
# Collect all sandbox-editable widgets and lock them until unlocked
|
||||
self._sandbox_widgets = [
|
||||
self.sandbox_confirm, self.sandbox_block_network,
|
||||
self.ai_check, self.sec_enabled,
|
||||
]
|
||||
for _w in self._sandbox_widgets:
|
||||
_w.setEnabled(False)
|
||||
|
||||
root.addWidget(self.sandbox_group)
|
||||
|
||||
# Connectors (MCP / REST API) are managed entirely in Monitoring → Tools
|
||||
@@ -250,6 +299,34 @@ class SettingsDialog(QDialog):
|
||||
|
||||
|
||||
|
||||
def _sandbox_unlock(self) -> None:
|
||||
"""Mở khoá nhóm cài đặt sandbox bằng mật khẩu.
|
||||
|
||||
Đây là khoá phía giao diện để chặn bấm nhầm vào một mục nhạy cảm, KHÔNG
|
||||
phải cơ chế bảo mật thật.
|
||||
"""
|
||||
pw = self.sandbox_pw_edit.text()
|
||||
if not self._sandbox_pw:
|
||||
# No password configured. Refusing with "wrong password" would be a
|
||||
# dead end — the user would keep retrying a password that cannot
|
||||
# exist — so name the actual state instead.
|
||||
QMessageBox.warning(self, tr("settings.sandbox_pw_unset_title"),
|
||||
tr("settings.sandbox_pw_unset_body"))
|
||||
return
|
||||
if _sandbox_password_matches(pw, self._sandbox_pw):
|
||||
self._sandbox_unlocked = True
|
||||
self.sandbox_locked_status.setText(tr("settings.sandbox_unlocked"))
|
||||
self.sandbox_locked_status.set_icon("unlock", "#090")
|
||||
self.sandbox_locked_status.text_label().setStyleSheet("color: #090; font-weight: bold;")
|
||||
# Enable all sandbox widgets
|
||||
for w in self._sandbox_widgets:
|
||||
w.setEnabled(True)
|
||||
QMessageBox.information(self, tr("settings.group.sandbox"),
|
||||
tr("settings.sandbox_unlocked_body"))
|
||||
else:
|
||||
QMessageBox.warning(self, tr("settings.sandbox_pw_wrong_title"),
|
||||
tr("settings.sandbox_pw_wrong_body"))
|
||||
|
||||
def _save(self) -> None:
|
||||
"""Gom cấu hình từ mọi trang con rồi ghi xuống đĩa."""
|
||||
data = self.ctx.config.data
|
||||
|
||||
+7
-139
@@ -63,9 +63,10 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
"""(label, index, icon_name, enabled) for EVERY sub-tab, hidden ones
|
||||
included.
|
||||
|
||||
Cột ``enabled`` là trạng thái cổng project; ``NavRailMixin._rebuild_nav``
|
||||
bỏ hẳn những hàng đang đóng (Cowork, GraphRAG) khỏi menu trái cho tới khi
|
||||
người dùng chọn một project. See nav_subtabs() for the visible-only view.
|
||||
The rail lists all five all the time and greys out the ones the project
|
||||
gate is currently closing (Cowork, GraphRAG) instead of removing them —
|
||||
same gate, shown rather than hidden, so the menu stops changing shape
|
||||
under the user's hand. See nav_subtabs() for the visible-only view.
|
||||
"""
|
||||
icons = {self._project_tab_idx: "folder", self._cowork_tab_idx: "chat",
|
||||
self._co4e_tab_idx: "flow", self._folder_tab_idx: "folder",
|
||||
@@ -77,8 +78,8 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
def subtab_available(self, index: int) -> bool:
|
||||
"""False while the project gate is holding this sub-tab shut.
|
||||
|
||||
Rail bỏ hẳn những hàng đó khỏi menu, nhưng đó chỉ chắn được đường vào
|
||||
qua rail. Hàm này để mọi đường vào khác hỏi cùng một trạng thái.
|
||||
The rail greys those rows out, but that only guards the rail. This lets
|
||||
every other route ask the same question of the same state.
|
||||
"""
|
||||
return bool(0 <= index < self.tabs.count() and self.tabs.isTabVisible(index))
|
||||
|
||||
@@ -290,28 +291,6 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
folder_row.addWidget(self._open_btn)
|
||||
rl.addLayout(folder_row)
|
||||
|
||||
# DF-007 — an ALTERNATIVE way to set output_dir: browse OneDrive/
|
||||
# SharePoint via Graph API and download a local mirror instead of
|
||||
# picking an already-local folder. output_dir still always ends up a
|
||||
# real local path (see Project.cloud_source) — nothing downstream
|
||||
# (run_command/read_file/...) needs to know the difference.
|
||||
cloud_row = QHBoxLayout()
|
||||
self._cloud_pick_btn = QPushButton()
|
||||
self._cloud_pick_btn.setIcon(icon("cloud"))
|
||||
self._cloud_pick_btn.clicked.connect(self._pick_cloud_folder)
|
||||
self._cloud_sync_btn = QPushButton()
|
||||
self._cloud_sync_btn.setIcon(icon("refresh"))
|
||||
self._cloud_sync_btn.clicked.connect(self._sync_cloud_folder)
|
||||
self._cloud_sync_btn.hide()
|
||||
cloud_row.addWidget(self._cloud_pick_btn)
|
||||
cloud_row.addWidget(self._cloud_sync_btn)
|
||||
cloud_row.addStretch(1)
|
||||
rl.addLayout(cloud_row)
|
||||
self._cloud_badge_lbl = QLabel()
|
||||
self._cloud_badge_lbl.setWordWrap(True)
|
||||
self._cloud_badge_lbl.hide()
|
||||
rl.addWidget(self._cloud_badge_lbl)
|
||||
|
||||
rl.addStretch(1) # the drawing floats Lưu project at the bottom
|
||||
save_row = QHBoxLayout()
|
||||
self._save_btn = QPushButton()
|
||||
@@ -497,9 +476,6 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
self._browse_btn.setText(tr("workspace.browse"))
|
||||
self._browse_btn.setToolTip(tr("workspace.browse_tooltip"))
|
||||
self._open_btn.setText(tr("workspace.open_folder"))
|
||||
self._cloud_pick_btn.setText(tr("workspace.cloud_pick"))
|
||||
self._cloud_sync_btn.setText(tr("workspace.cloud_sync"))
|
||||
self._refresh_cloud_badge()
|
||||
self._save_btn.setText(tr("workspace.save"))
|
||||
self.retranslate_project_rows()
|
||||
self._proj_collapse_btn.setToolTip(tr("workspace.collapse_projects_tooltip"))
|
||||
@@ -609,12 +585,7 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
counts = self._project_counts()
|
||||
self.project_list.blockSignals(True)
|
||||
self.project_list.clear()
|
||||
# -1 chứ không phải 0: chưa chọn gì thì KHÔNG tự chọn hộ project đầu
|
||||
# danh sách. Chọn hộ là mở luôn cổng Cowork/GraphRAG (xem
|
||||
# _update_tab_visibility) cho một project người dùng chưa hề bấm vào —
|
||||
# lúc mở app, và cả sau khi xoá project đang mở. Có ``keep`` khớp thì
|
||||
# vẫn giữ đúng dòng cũ như trước.
|
||||
row_to_select = -1
|
||||
row_to_select = 0
|
||||
for i, p in enumerate(list_projects()):
|
||||
chats, tasks = counts.get(p.project_id, (0, 0))
|
||||
# No text on the item: the row widget paints the name, and setting
|
||||
@@ -704,7 +675,6 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
self.instr_edit.clear()
|
||||
self.folder_lbl.setText("")
|
||||
self._del_btn.setEnabled(False)
|
||||
self._refresh_cloud_badge(project)
|
||||
# Show Cowork/GraphRAG ONLY when a project is actually selected.
|
||||
self._update_tab_visibility(project is not None)
|
||||
# Bind the embedded Cowork/GraphRAG/History to this project's sandbox.
|
||||
@@ -916,105 +886,3 @@ class WorkspaceTab(ProjectEditingMixin, QWidget):
|
||||
wd = project.workspace_dir()
|
||||
wd.mkdir(parents=True, exist_ok=True)
|
||||
open_folder(str(wd))
|
||||
|
||||
def _cloud_token(self):
|
||||
"""Lấy access token MS365 hiện tại theo cấu hình — raise
|
||||
``Ms365AuthError`` nếu chưa đăng nhập/hết hạn (gọi picker trước nên
|
||||
thường đã có sẵn phiên đăng nhập)."""
|
||||
from ..core.ms365_auth import get_access_token
|
||||
|
||||
ms365 = (self.ctx.config.ms365 or {})
|
||||
return get_access_token(ms365.get("tenant_id", ""), ms365.get("client_id", ""))
|
||||
|
||||
def _pick_cloud_folder(self) -> None:
|
||||
"""DF-007 — duyệt OneDrive/SharePoint qua Graph API, tải một bản
|
||||
mirror cục bộ xuống rồi dùng bản mirror đó làm output_dir của
|
||||
project. Xem core/cloud_workspace_sync.py cho giới hạn (một chiều,
|
||||
thủ công, không đồng bộ liên tục, không xử lý xung đột)."""
|
||||
from ..core.cloud_workspace_sync import download_folder
|
||||
from ..core.ms365_auth import Ms365AuthError
|
||||
from ..core.projects import WORKSPACES_DIR, load_project, save_project
|
||||
from .cloud_folder_picker_dialog import pick_cloud_folder
|
||||
|
||||
pid = self._current_id
|
||||
project = load_project(pid) if pid else None
|
||||
if project is None:
|
||||
return
|
||||
cloud_source = pick_cloud_folder(self, self.ctx.config)
|
||||
if not cloud_source:
|
||||
return
|
||||
local_dir = WORKSPACES_DIR / project.project_id / "_cloud_mirror"
|
||||
self._cloud_pick_btn.setEnabled(False)
|
||||
try:
|
||||
token = self._cloud_token()
|
||||
report = download_folder(token, cloud_source, local_dir)
|
||||
except Ms365AuthError as exc:
|
||||
QMessageBox.warning(self, tr("workspace.cloud_pick"), str(exc))
|
||||
return
|
||||
finally:
|
||||
self._cloud_pick_btn.setEnabled(True)
|
||||
project.output_dir = str(local_dir)
|
||||
project.cloud_source = cloud_source
|
||||
save_project(project)
|
||||
self.folder_lbl.setText(str(local_dir))
|
||||
self._refresh_cloud_badge(project)
|
||||
self.projects_changed.emit()
|
||||
if report.errors:
|
||||
QMessageBox.warning(self, tr("workspace.cloud_pick"),
|
||||
tr("workspace.cloud_sync_errors", n=len(report.errors)))
|
||||
self.status_message.emit(tr("workspace.saved", name=project.name))
|
||||
|
||||
def _sync_cloud_folder(self) -> None:
|
||||
"""DF-007 — đẩy thay đổi cục bộ lên cloud rồi tải lại (một chiều mỗi
|
||||
bước, thủ công, chạy khi bấm nút). Không xoá file 2 phía, không phát
|
||||
hiện xung đột — xem core/cloud_workspace_sync.py."""
|
||||
from ..core.cloud_workspace_sync import download_folder, upload_folder
|
||||
from ..core.ms365_auth import Ms365AuthError
|
||||
from ..core.projects import load_project
|
||||
|
||||
pid = self._current_id
|
||||
project = load_project(pid) if pid else None
|
||||
if project is None or not project.cloud_source:
|
||||
return
|
||||
self._cloud_sync_btn.setEnabled(False)
|
||||
try:
|
||||
token = self._cloud_token()
|
||||
up_report = upload_folder(token, project.cloud_source, project.workspace_dir())
|
||||
down_report = download_folder(token, project.cloud_source, project.workspace_dir())
|
||||
except Ms365AuthError as exc:
|
||||
QMessageBox.warning(self, tr("workspace.cloud_sync"), str(exc))
|
||||
return
|
||||
finally:
|
||||
self._cloud_sync_btn.setEnabled(True)
|
||||
lines = [tr("workspace.cloud_sync_result",
|
||||
up=up_report.transferred, down=down_report.transferred)]
|
||||
n_errors = len(up_report.errors) + len(down_report.errors)
|
||||
if n_errors:
|
||||
lines.append(tr("workspace.cloud_sync_errors", n=n_errors))
|
||||
n_skipped = len(up_report.skipped_too_large)
|
||||
if n_skipped:
|
||||
lines.append(tr("workspace.cloud_sync_skipped", n=n_skipped))
|
||||
QMessageBox.information(self, tr("workspace.cloud_sync"), "\n".join(lines))
|
||||
|
||||
def _refresh_cloud_badge(self, project=None) -> None:
|
||||
"""Hiện/ẩn badge ☁ + nút Đồng bộ theo project đang mở có phải một
|
||||
mirror cloud hay không (``project.cloud_source``)."""
|
||||
if project is None:
|
||||
from ..core.projects import load_project
|
||||
|
||||
project = load_project(self._current_id) if self._current_id else None
|
||||
cloud_source = project.cloud_source if project is not None else None
|
||||
if not cloud_source:
|
||||
self._cloud_badge_lbl.hide()
|
||||
self._cloud_sync_btn.hide()
|
||||
return
|
||||
if cloud_source.get("provider") == "sharepoint":
|
||||
text = tr("workspace.cloud_badge_sharepoint",
|
||||
site=cloud_source.get("site_name", ""),
|
||||
path=cloud_source.get("remote_path", "") or "/")
|
||||
else:
|
||||
text = tr("workspace.cloud_badge_onedrive",
|
||||
path=cloud_source.get("remote_path", "") or "/")
|
||||
self._cloud_badge_lbl.setText(text)
|
||||
self._cloud_badge_lbl.show()
|
||||
self._cloud_sync_btn.show()
|
||||
|
||||
Reference in New Issue
Block a user