refactor: vá 4 hồi quy, tách 4 file chạm trần LOC, docstring lên 100%
Hồi quy đã vá
-------------
F-12 Kéo–thả hoặc dán tệp vào ô chat ném NameError. R08 tách `_Input` sang
`chat_input_box.py` nhưng để `_paths_from_mime()` ở lại
`composer_widget.py`, nên hai hàm sự kiện Qt gọi một cái tên không tồn
tại. Bốn hàm dùng chung chuyển sang `composer_mime.py` — module thứ ba
là chỗ duy nhất không lặp lại được lỗi này. Đo lại: cả thả lẫn dán đều
gắn 1 tệp, khớp bản trước refactor.
F-01 Đổi provider thì bộ chọn model AI-Edit không làm gì. Hook cũ kiểm
`folder.ai_model_combo`, thuộc tính R08-T12 đã dời sang
`ai_panel.resolver`. Làm mới vô điều kiện, đúng như tab cũ: lần lấy đầu
tiên hỏng thì đổi provider chính là lúc phải thử lại.
F-07 Hàng chọn kỳ của Dashboard bị đẩy xuống dưới các thẻ số liệu. Hàng này
lọc CẢ BA thẻ con chứ không riêng biểu đồ, nên để nó nằm dưới là bắt
người dùng đọc con số trước khi thấy con số đó tính cho kỳ nào. Kèm
theo: `TokenUsageCardWidget` bị bỏ sót `setContentsMargins(0,0,0,0)`
mà hai thẻ con còn lại đã có, đẩy cả hàng thẻ lệch 9px.
`check_layout_geometry` nay khớp TỪNG BYTE với bản trước refactor.
F-11 Hai lớp khai trùng tên phương thức; Python giữ bản sau nên bản đầu là
mã chết. `co4e_tab.py::showEvent` bản đầu gọi `_narrow_guard.attach()`
và không bao giờ chạy.
Tách file (F-09)
----------------
Bốn file chạm trần 400 dòng, mỗi lần cắt ra một trách nhiệm thật:
graph_renderer.py -> graph_scene_builder.py + graph_export.py
co4e_workflow_service.py -> co4e_run_history.py
json_config_repository.py -> config_sections.py
agents_admin_tab.py -> shared/agent_kind_visuals.py
File cuối còn xoá 3 bản sao của hàm đã có trong `shared/formatters.py`,
giống hệt đến từng dòng — nay định dạng thời gian và avatar không lệch nhau
giữa các bảng Giám sát nữa.
Docstring
---------
41,6% -> 100% (3.478/3.478 định nghĩa production), kể cả module dormant và
phương thức dunder. Toàn bộ phần bổ sung viết bằng tiếng Việt; comment tiếng
Anh có sẵn giữ nguyên — dịch ngược là một đợt riêng.
Seam chưa nối dây (F-05)
------------------------
9 seam mang nhãn `SEAM · dựng <ngày>` kèm hai câu: được nối khi nào, và để
dormant thì hỏng gì. Ngày lấy từ lịch sử git, không phải hạn tự đặt. Gate O
đọc nhãn đó và nhắc khi quá 30 ngày.
859 test xanh · 4/4 cổng CASAN · 19/24 checker khớp từng byte bản cũ.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -54,6 +54,9 @@ RUN_MODES = ("auto", "plan", "manual")
|
||||
|
||||
|
||||
def slugify(value: str) -> str:
|
||||
"""Chuyển một chuỗi thành slug an toàn cho tên file: chỉ chữ/số/gạch, gộp gạch
|
||||
liên tiếp. Rỗng thì trả về 'step' để không bao giờ sinh ra tên file trống.
|
||||
"""
|
||||
s = "".join(c if (c.isalnum() or c in "-_") else "-" for c in (value or "").strip().lower())
|
||||
return "-".join(filter(None, s.split("-"))) or "step"
|
||||
|
||||
@@ -88,11 +91,13 @@ class Step:
|
||||
|
||||
@property
|
||||
def is_parallel(self) -> bool:
|
||||
"""Bước này có chạy nhiều sub-agent song song hay không."""
|
||||
return self.variant == "parallel"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
"""Một node trên khung vẽ: id, toạ độ, và bước (:class:`Step`) mà nó đại diện."""
|
||||
id: str
|
||||
x: float = 0.0
|
||||
y: float = 0.0
|
||||
@@ -101,6 +106,7 @@ class Node:
|
||||
|
||||
@dataclass
|
||||
class Edge:
|
||||
"""Một cạnh nối hai node, quy định thứ tự chạy giữa chúng."""
|
||||
id: str
|
||||
source: str
|
||||
target: str
|
||||
@@ -108,6 +114,7 @@ class Edge:
|
||||
|
||||
@dataclass
|
||||
class Workflow:
|
||||
"""Một luồng Co4E: danh sách node, cạnh, và cờ đánh dấu đây có phải mẫu không."""
|
||||
id: str
|
||||
name: str = "Untitled flow"
|
||||
is_template: bool = False
|
||||
@@ -132,6 +139,10 @@ class CustomAgent:
|
||||
|
||||
# ---- (de)serialization ---------------------------------------------------
|
||||
def step_from_dict(d: dict) -> Step:
|
||||
"""Dựng :class:`Step` từ dict đọc trên đĩa.
|
||||
|
||||
Lọc bỏ khoá lạ để file luồng của phiên bản mới hơn không làm vỡ bản cũ.
|
||||
"""
|
||||
d = dict(d or {})
|
||||
subs = d.pop("sub_agents", None) or []
|
||||
known = Step().__dict__.keys()
|
||||
@@ -145,11 +156,13 @@ def step_from_dict(d: dict) -> Step:
|
||||
|
||||
|
||||
def node_from_dict(d: dict) -> Node:
|
||||
"""Dựng :class:`Node` từ dict đọc trên đĩa."""
|
||||
return Node(id=str(d.get("id", "")), x=float(d.get("x", 0) or 0),
|
||||
y=float(d.get("y", 0) or 0), data=step_from_dict(d.get("data", {})))
|
||||
|
||||
|
||||
def workflow_from_dict(d: dict) -> Workflow:
|
||||
"""Dựng :class:`Workflow` từ dict đọc trên đĩa."""
|
||||
return Workflow(
|
||||
id=str(d.get("id", "")),
|
||||
name=d.get("name", "Untitled flow"),
|
||||
@@ -161,6 +174,7 @@ def workflow_from_dict(d: dict) -> Workflow:
|
||||
|
||||
|
||||
def workflow_to_dict(wf: Workflow) -> dict:
|
||||
"""Chuyển một luồng thành dict để ghi JSON."""
|
||||
return {
|
||||
"id": wf.id, "name": wf.name, "is_template": wf.is_template,
|
||||
"nodes": [{"id": n.id, "x": n.x, "y": n.y, "data": _step_dict(n.data)} for n in wf.nodes],
|
||||
@@ -169,16 +183,19 @@ def workflow_to_dict(wf: Workflow) -> dict:
|
||||
|
||||
|
||||
def _step_dict(step: Step) -> dict:
|
||||
"""Chuyển một bước thành dict; ``asdict`` đã tự chuyển ``sub_agents`` thành list dict."""
|
||||
d = asdict(step)
|
||||
# asdict already turns sub_agents into list[dict]
|
||||
return d
|
||||
|
||||
|
||||
def agent_to_dict(a: CustomAgent) -> dict:
|
||||
"""Chuyển một agent tự tạo thành dict để ghi JSON."""
|
||||
return asdict(a)
|
||||
|
||||
|
||||
def agent_from_dict(d: dict) -> CustomAgent:
|
||||
"""Dựng :class:`CustomAgent` từ dict, lọc bỏ khoá lạ."""
|
||||
known = CustomAgent(id="").__dict__.keys()
|
||||
d = {k: v for k, v in (d or {}).items() if k in known}
|
||||
d.setdefault("id", "")
|
||||
@@ -193,32 +210,43 @@ _counter = {"n": 0}
|
||||
|
||||
|
||||
def _mint_id(prefix: str) -> str:
|
||||
"""Sinh id tăng dần dạng ``<prefix>_000001``."""
|
||||
_counter["n"] += 1
|
||||
return f"{prefix}_{_counter['n']:06d}"
|
||||
|
||||
|
||||
def new_node_id() -> str:
|
||||
"""Id mới cho một node."""
|
||||
return _mint_id("node")
|
||||
|
||||
|
||||
def new_edge_id(source: str, target: str) -> str:
|
||||
"""Id cạnh suy ra TỪ cặp nguồn/đích.
|
||||
|
||||
Cố ý không ngẫu nhiên: nhờ vậy nối lại đúng cặp node đó luôn cho ra cùng
|
||||
một id, và không thể sinh ra hai cạnh trùng nhau.
|
||||
"""
|
||||
return f"e_{source}__{target}"
|
||||
|
||||
|
||||
def new_workflow(name: str = "Untitled flow") -> Workflow:
|
||||
"""Tạo một luồng rỗng với id mới."""
|
||||
return Workflow(id=_mint_id("wf"), name=name)
|
||||
|
||||
|
||||
def new_custom_agent(name: str = "") -> CustomAgent:
|
||||
"""Tạo một agent tự tạo rỗng với id mới."""
|
||||
return CustomAgent(id=_mint_id("agent"), name=name)
|
||||
|
||||
|
||||
# ---- workflow store ------------------------------------------------------
|
||||
def workflows_dir() -> Path:
|
||||
"""Thư mục chứa file luồng."""
|
||||
return WORKFLOWS_DIR
|
||||
|
||||
|
||||
def list_workflows(directory: Optional[Path] = None) -> List[Workflow]:
|
||||
"""Liệt kê mọi luồng đã lưu; thư mục chưa có thì trả list rỗng."""
|
||||
directory = directory or WORKFLOWS_DIR
|
||||
if not directory.exists():
|
||||
return []
|
||||
@@ -232,6 +260,7 @@ def list_workflows(directory: Optional[Path] = None) -> List[Workflow]:
|
||||
|
||||
|
||||
def save_workflow(wf: Workflow, directory: Optional[Path] = None) -> Path:
|
||||
"""Ghi một luồng ra ``<id>.json``, tự tạo thư mục nếu chưa có."""
|
||||
directory = directory or WORKFLOWS_DIR
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
path = directory / f"{wf.id}.json"
|
||||
@@ -242,6 +271,7 @@ def save_workflow(wf: Workflow, directory: Optional[Path] = None) -> Path:
|
||||
|
||||
|
||||
def get_workflow(wf_id: str, directory: Optional[Path] = None) -> Optional[Workflow]:
|
||||
"""Đọc một luồng theo id; ``None`` nếu không có."""
|
||||
directory = directory or WORKFLOWS_DIR
|
||||
path = directory / f"{wf_id}.json"
|
||||
if not path.exists():
|
||||
@@ -280,6 +310,7 @@ def tr_copy_suffix() -> str:
|
||||
|
||||
|
||||
def delete_workflow(wf_id: str, directory: Optional[Path] = None) -> None:
|
||||
"""Xoá file luồng theo id; không có thì bỏ qua."""
|
||||
directory = directory or WORKFLOWS_DIR
|
||||
path = directory / f"{wf_id}.json"
|
||||
if path.exists():
|
||||
@@ -291,10 +322,12 @@ def delete_workflow(wf_id: str, directory: Optional[Path] = None) -> None:
|
||||
|
||||
# ---- custom-agent store --------------------------------------------------
|
||||
def agents_dir() -> Path:
|
||||
"""Thư mục chứa file agent tự tạo."""
|
||||
return AGENTS_DIR
|
||||
|
||||
|
||||
def list_custom_agents(directory: Optional[Path] = None) -> List[CustomAgent]:
|
||||
"""Liệt kê mọi agent tự tạo; thư mục chưa có thì trả list rỗng."""
|
||||
directory = directory or AGENTS_DIR
|
||||
if not directory.exists():
|
||||
return []
|
||||
@@ -308,6 +341,7 @@ def list_custom_agents(directory: Optional[Path] = None) -> List[CustomAgent]:
|
||||
|
||||
|
||||
def save_custom_agent(agent: CustomAgent, directory: Optional[Path] = None) -> Path:
|
||||
"""Ghi một agent tự tạo ra ``<id>.json``."""
|
||||
directory = directory or AGENTS_DIR
|
||||
directory.mkdir(parents=True, exist_ok=True)
|
||||
path = directory / f"{agent.id}.json"
|
||||
@@ -316,6 +350,7 @@ def save_custom_agent(agent: CustomAgent, directory: Optional[Path] = None) -> P
|
||||
|
||||
|
||||
def delete_custom_agent(agent_id: str, directory: Optional[Path] = None) -> None:
|
||||
"""Xoá file agent tự tạo theo id; không có thì bỏ qua."""
|
||||
directory = directory or AGENTS_DIR
|
||||
path = directory / f"{agent_id}.json"
|
||||
if path.exists():
|
||||
@@ -340,6 +375,11 @@ def compute_waves(nodes: List[Node], edges: List[Edge]) -> Dict[str, int]:
|
||||
limit = len(nodes) + 1
|
||||
|
||||
def depth(nid: str, seen: frozenset) -> int:
|
||||
"""Độ sâu của một node = lớp chạy của nó.
|
||||
|
||||
Có nhớ kết quả và chặn theo ``limit``: đồ thị có vòng sẽ khiến đệ quy chạy
|
||||
mãi, nên gặp node đã thấy trong nhánh hiện tại thì dừng.
|
||||
"""
|
||||
if nid in wave:
|
||||
return wave[nid]
|
||||
if nid in seen or len(seen) > limit:
|
||||
@@ -360,12 +400,14 @@ def connected_component_count(nodes: List[Node], edges: List[Edge]) -> int:
|
||||
parent = {n.id: n.id for n in nodes}
|
||||
|
||||
def find(x):
|
||||
"""Tìm gốc của một phần tử, kèm nén đường đi (union-find)."""
|
||||
while parent[x] != x:
|
||||
parent[x] = parent[parent[x]]
|
||||
x = parent[x]
|
||||
return x
|
||||
|
||||
def union(a, b):
|
||||
"""Gộp hai tập hợp lại làm một (union-find)."""
|
||||
ra, rb = find(a), find(b)
|
||||
if ra != rb:
|
||||
parent[ra] = rb
|
||||
@@ -379,6 +421,7 @@ def connected_component_count(nodes: List[Node], edges: List[Edge]) -> int:
|
||||
# ---- run-stage compilation ----------------------------------------------
|
||||
@dataclass
|
||||
class RunStage:
|
||||
"""Một chặng chạy: ứng với một node, hoặc một nhánh song song / bước gộp của nó."""
|
||||
id: str # node id, or "<node>__p<i>" / "<node>__pjoin"
|
||||
node_id: str # which canvas node this stage maps back onto
|
||||
wave: int
|
||||
@@ -395,6 +438,7 @@ PLAN_MODE_PREAMBLE = (
|
||||
|
||||
|
||||
def build_skills_block(skills: List[str], skill_map: Dict[str, str]) -> str:
|
||||
"""Ghép nội dung các skill được chọn thành một khối chèn vào prompt."""
|
||||
parts = []
|
||||
for name in skills or []:
|
||||
content = (skill_map.get(name) or "").strip()
|
||||
@@ -407,6 +451,9 @@ def build_skills_block(skills: List[str], skill_map: Dict[str, str]) -> str:
|
||||
|
||||
|
||||
def _shared_prompt_parts(step: Step, skill_map: Dict[str, str], extra_context: str) -> str:
|
||||
"""Phần prompt dùng chung cho cả ba loại chặng: chỉ dẫn của bước, khối skill,
|
||||
và ngữ cảnh thêm từ các bước trước.
|
||||
"""
|
||||
parts = []
|
||||
if step.instructions.strip():
|
||||
parts.append(step.instructions.strip())
|
||||
@@ -423,6 +470,7 @@ def _shared_prompt_parts(step: Step, skill_map: Dict[str, str], extra_context: s
|
||||
|
||||
|
||||
def build_step_prompt(step: Step, skill_map: Dict[str, str], extra_context: str = "") -> str:
|
||||
"""Prompt cho một bước chạy tuần tự bình thường."""
|
||||
head = f'You are the {step.role} agent for the workflow step "{step.label}".'
|
||||
body = _shared_prompt_parts(step, skill_map, extra_context)
|
||||
return f"{head}\n{body}".strip()
|
||||
@@ -430,6 +478,11 @@ def build_step_prompt(step: Step, skill_map: Dict[str, str], extra_context: str
|
||||
|
||||
def build_subagent_prompt(step: Step, sub: SubAgent, peers: List[str],
|
||||
skill_map: Dict[str, str], extra_context: str = "") -> str:
|
||||
"""Prompt cho một sub-agent chạy song song.
|
||||
|
||||
Nói rõ nó đang chạy CÙNG LÚC với những ai và phải ở trong phạm vi của mình —
|
||||
không có câu đó, các sub-agent hay làm chồng việc của nhau.
|
||||
"""
|
||||
peer_txt = ", ".join(p for p in peers if p) or "peers"
|
||||
head = (f'You are the "{sub.agent}" agent working concurrently (in parallel with '
|
||||
f'{peer_txt}) on the workflow step "{step.label}". Stay within your own scope.')
|
||||
@@ -443,6 +496,7 @@ def build_subagent_prompt(step: Step, sub: SubAgent, peers: List[str],
|
||||
|
||||
|
||||
def build_join_prompt(step: Step, skill_map: Dict[str, str], extra_context: str = "") -> str:
|
||||
"""Prompt cho bước gộp: hợp nhất đầu ra của các sub-agent thành một kết quả."""
|
||||
head = (f'You are the coordinator for the parallel step "{step.label}". Consolidate the '
|
||||
f"outputs of the sub-agents (provided above as prior outputs) into one coherent result.")
|
||||
body = _shared_prompt_parts(step, skill_map, extra_context)
|
||||
@@ -461,6 +515,9 @@ def compile_run_stages(nodes: List[Node], edges: List[Edge],
|
||||
stages: List[RunStage] = []
|
||||
|
||||
def finalize(prompt: str, preset: str) -> tuple:
|
||||
"""Chốt prompt của một chặng: áp phạm vi theo preset, và thêm lời mở đầu chế
|
||||
độ lập kế hoạch nếu đang chạy ở chế độ đó.
|
||||
"""
|
||||
scope = PRESET_SCOPES.get(preset)
|
||||
if plan_mode:
|
||||
prompt = PLAN_MODE_PREAMBLE + prompt
|
||||
|
||||
Reference in New Issue
Block a user