Hồi quy đã vá
-------------
F-12 Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
`chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
`composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
gắn 1 tệp, khớp bản trước refactor.
F-01 Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
`folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
`ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
tiên hỏng thì đổi provider chính là lúc phải thử lại.
F-07 Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
`check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.
F-11 Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
và không bao giờ chạy.
Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:
graph_renderer.py -> graph_scene_builder.py + graph_export.py
co4e_workflow_service.py -> co4e_run_history.py
json_config_repository.py -> config_sections.py
agents_admin_tab.py -> shared/agent_kind_visuals.py
File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.
Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.
Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.
859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
228 lines
9.5 KiB
Python
228 lines
9.5 KiB
Python
"""Schedule Task — multi-format importer.
|
|
|
|
Import tasks from several input file formats, all producing the same task dicts
|
|
the Excel importer returns (previewed, then saved on confirm):
|
|
|
|
* .xlsx / .xls → the fill-in template (delegates to ``task_excel``)
|
|
* .csv → same columns as the template (header row, order-flexible)
|
|
* .json → a list of task objects (or ``{"tasks": [...]}``) — the most
|
|
expressive form: supports Co4E-flow tasks via ``flow_id``.
|
|
|
|
Every format runs through the SAME field mapping / enum fallbacks as the Excel
|
|
importer (``task_excel.task_from_cells`` / ``resolve_depends``), so behaviour is
|
|
consistent across formats.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any, Dict, List
|
|
|
|
from . import task_excel
|
|
from .tasks import PRIORITIES, REPEAT_TYPES, TASK_TYPES, new_task, parse_run_at
|
|
|
|
SUPPORTED_EXTS = (".xlsx", ".xls", ".csv", ".json")
|
|
IMPORT_FILTER = "Tasks (*.xlsx *.xls *.csv *.json)"
|
|
|
|
_HEADER_ALIASES = {h.split(" (")[0].strip().lower(): i for i, h in enumerate(task_excel.HEADERS)}
|
|
|
|
|
|
def import_tasks(path: str | Path) -> List[Dict[str, Any]]:
|
|
"""Dispatch by file extension. Raises ``ValueError`` with a human message on
|
|
an unusable / unsupported file. Imported tasks are auto-chained to run in the
|
|
file's top→bottom order (unless the file already defines dependencies)."""
|
|
p = Path(path)
|
|
ext = p.suffix.lower()
|
|
if ext in (".xlsx", ".xls"):
|
|
tasks = task_excel.import_tasks(p)
|
|
elif ext == ".csv":
|
|
tasks = _import_csv(p)
|
|
elif ext == ".json":
|
|
tasks = _import_json(p)
|
|
else:
|
|
raise ValueError(f"Unsupported file type '{ext}'. Use one of: {', '.join(SUPPORTED_EXTS)}.")
|
|
return auto_chain_in_order(tasks)
|
|
|
|
|
|
def auto_chain_in_order(tasks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
"""Chain imported tasks so they run one after another in the file's row order
|
|
(top → bottom): each task triggers the next on success, and the FIRST task is
|
|
scheduled to start immediately. Skipped when the file already defines its own
|
|
dependencies/chains (those are respected instead)."""
|
|
if len(tasks) < 1:
|
|
return tasks
|
|
already = any((t.get("dependency", {}).get("depends_on")
|
|
or t.get("dependency", {}).get("next_task_id")) for t in tasks)
|
|
if already:
|
|
return tasks
|
|
from datetime import datetime
|
|
|
|
from .tasks import format_run_at
|
|
for i in range(len(tasks) - 1):
|
|
dep = tasks[i].setdefault("dependency", {})
|
|
dep["next_task_id"] = tasks[i + 1]["task_id"]
|
|
dep["run_next_mode"] = "run_after_success"
|
|
first = tasks[0]
|
|
if not first.get("schedule", {}).get("enabled"):
|
|
first.setdefault("schedule", {})["enabled"] = True
|
|
first["schedule"]["run_at"] = format_run_at(datetime.now())
|
|
first["status"] = "scheduled"
|
|
return tasks
|
|
|
|
|
|
# ---- CSV -----------------------------------------------------------------
|
|
def _import_csv(path: Path) -> List[Dict[str, Any]]:
|
|
"""Đọc danh sách task từ file CSV (chấp nhận BOM của Excel)."""
|
|
try:
|
|
text = path.read_text(encoding="utf-8-sig")
|
|
except OSError as exc:
|
|
raise ValueError(f"Cannot read CSV file: {exc}") from exc
|
|
reader = list(csv.reader(text.splitlines()))
|
|
if not reader:
|
|
raise ValueError("The CSV file is empty.")
|
|
header = [str(c or "").strip().lower() for c in reader[0]]
|
|
# A header row lets columns be in any order; without one, assume template order.
|
|
has_header = any(h in _HEADER_ALIASES for h in header)
|
|
col_map = None
|
|
if has_header:
|
|
col_map = {i: _HEADER_ALIASES[h] for i, h in enumerate(header) if h in _HEADER_ALIASES}
|
|
data_rows = reader[1:] if has_header else reader
|
|
|
|
tasks: List[Dict[str, Any]] = []
|
|
depends_raw: List[List[str]] = []
|
|
for row in data_rows:
|
|
if col_map is not None:
|
|
cells = [None] * len(task_excel.HEADERS)
|
|
for src_i, dst_i in col_map.items():
|
|
if src_i < len(row):
|
|
cells[dst_i] = row[src_i]
|
|
else:
|
|
cells = list(row)
|
|
t, depends = task_excel.task_from_cells(cells)
|
|
if t is None:
|
|
continue
|
|
tasks.append(t)
|
|
depends_raw.append(depends)
|
|
if not tasks:
|
|
raise ValueError("No task rows found in the CSV file.")
|
|
task_excel.resolve_depends(tasks, depends_raw)
|
|
return tasks
|
|
|
|
|
|
# ---- JSON ----------------------------------------------------------------
|
|
def _import_json(path: Path) -> List[Dict[str, Any]]:
|
|
"""Đọc danh sách task từ file JSON."""
|
|
try:
|
|
data = json.loads(path.read_text(encoding="utf-8"))
|
|
except (OSError, json.JSONDecodeError) as exc:
|
|
raise ValueError(f"Cannot read JSON file: {exc}") from exc
|
|
if isinstance(data, dict) and isinstance(data.get("tasks"), list):
|
|
data = data["tasks"]
|
|
if not isinstance(data, list):
|
|
raise ValueError("JSON must be a list of task objects (or {\"tasks\": [...]}).")
|
|
|
|
tasks: List[Dict[str, Any]] = []
|
|
depends_raw: List[List[str]] = []
|
|
for obj in data:
|
|
if not isinstance(obj, dict):
|
|
continue
|
|
t, depends = _task_from_mapping(obj)
|
|
if t is None:
|
|
continue
|
|
tasks.append(t)
|
|
depends_raw.append(depends)
|
|
if not tasks:
|
|
raise ValueError("No task objects found in the JSON file.")
|
|
# depends_on may reference titles OR ids — resolve titles here, keep ids.
|
|
task_excel.resolve_depends(tasks, depends_raw)
|
|
return tasks
|
|
|
|
|
|
def _pick(d: Dict[str, Any], *keys, default=""):
|
|
"""Lấy giá trị đầu tiên khác rỗng trong các khoá được nêu.
|
|
|
|
File nhập từ nhiều nguồn đặt tên cột khác nhau (``title``/``name``/``Tiêu đề``),
|
|
nên phải thử lần lượt.
|
|
"""
|
|
for k in keys:
|
|
if k in d and d[k] not in (None, ""):
|
|
return d[k]
|
|
return default
|
|
|
|
|
|
def _clamp(value, allowed, default):
|
|
"""Ép một giá trị về tập hợp lệ; ngoài tập thì lấy mặc định."""
|
|
v = str(value or "").strip().lower()
|
|
return v if v in allowed else default
|
|
|
|
|
|
def _task_from_mapping(d: Dict[str, Any]):
|
|
"""Map a JSON object to a task dict + its depends-on titles. Recognises the
|
|
template field names plus friendly aliases, and — uniquely for JSON — a
|
|
``flow_id`` that turns the task into a Co4E-flow run."""
|
|
title = str(_pick(d, "title", "name")).strip()
|
|
if not title:
|
|
return None, None
|
|
t = new_task(title)
|
|
t["description"] = str(_pick(d, "description", "desc")).strip()
|
|
flow_id = str(_pick(d, "flow_id", "co4e_flow", "flow")).strip()
|
|
if flow_id:
|
|
t["task_type"] = "flow"
|
|
t["flow"]["flow_id"] = flow_id
|
|
else:
|
|
t["task_type"] = _clamp(_pick(d, "task_type", "type", default="cowork"),
|
|
TASK_TYPES, "cowork")
|
|
t["priority"] = _clamp(_pick(d, "priority", default="medium"), PRIORITIES, "medium")
|
|
t["script_command"] = str(_pick(d, "script_command", "command")).strip()
|
|
t["provider"] = str(_pick(d, "provider")).strip()
|
|
t["model"] = str(_pick(d, "model")).strip()
|
|
t["skill_slug"] = str(_pick(d, "skill_slug", "skill")).strip()
|
|
sched = d.get("schedule") if isinstance(d.get("schedule"), dict) else d
|
|
enabled = _truthy(_pick(sched, "schedule_enabled", "enabled", default=False))
|
|
run_at = str(_pick(sched, "run_at")).strip()[:16]
|
|
t["schedule"]["run_at"] = run_at if parse_run_at(run_at) else None
|
|
t["schedule"]["repeat_type"] = _clamp(_pick(sched, "repeat", "repeat_type", default="none"),
|
|
REPEAT_TYPES, "none")
|
|
t["schedule"]["cron_expression"] = str(_pick(sched, "cron_expression", "cron")).strip() or None
|
|
if enabled and (t["schedule"]["run_at"] or t["schedule"]["repeat_type"] == "cron"):
|
|
t["schedule"]["enabled"] = True
|
|
t["status"] = "scheduled"
|
|
if _truthy(_pick(d, "use_previous_output", default=False)):
|
|
t["input"]["mode"] = "previous_task_output"
|
|
manual_text = str(_pick(d, "manual_text", "input_text")).strip()
|
|
if manual_text:
|
|
t["input"]["mode"] = "manual"
|
|
t["input"]["manual_text"] = manual_text
|
|
files = _pick(d, "file_paths", "files", default=[])
|
|
if isinstance(files, list):
|
|
t["input"]["file_paths"] = [str(x) for x in files if x]
|
|
links = _pick(d, "links", "urls", default=[])
|
|
if isinstance(links, list):
|
|
t["input"]["links"] = [str(x) for x in links if x]
|
|
t["execution"]["requires_approval"] = _truthy(_pick(d, "requires_approval", default=False))
|
|
channel = str(_pick(d, "notify_channel", "reminder")).strip().lower()
|
|
t["execution"]["notify_channel"] = channel if channel in ("teams", "outlook") else "none"
|
|
t["execution"]["notify_email"] = str(_pick(d, "notify_email", "reminder_email")).strip()
|
|
if t["execution"]["notify_channel"] != "none":
|
|
t["execution"]["notify_on_complete"] = True
|
|
t["execution"]["notify_on_error"] = True
|
|
depends = _pick(d, "depends_on", "depends", default=[])
|
|
if isinstance(depends, str):
|
|
depends = [s.strip() for s in depends.split(";") if s.strip()]
|
|
elif isinstance(depends, list):
|
|
depends = [str(x).strip() for x in depends if str(x).strip()]
|
|
else:
|
|
depends = []
|
|
return t, depends
|
|
|
|
|
|
_TRUE = {"yes", "y", "true", "1", "x", "có", "co"}
|
|
|
|
|
|
def _truthy(value) -> bool:
|
|
"""Đọc giá trị đúng/sai từ nhiều kiểu ghi khác nhau (bool, 'yes', '1', 'có'…)."""
|
|
if isinstance(value, bool):
|
|
return value
|
|
return str(value or "").strip().lower() in _TRUE
|