80 lines
1.7 KiB
Python
80 lines
1.7 KiB
Python
"""任务进度写入 Markdown 文档,便于理解和审计。"""
|
||
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
|
||
from backend.config import TASKS_DIR
|
||
|
||
|
||
def task_doc_dir(task_id: str) -> Path:
|
||
d = TASKS_DIR / task_id
|
||
d.mkdir(parents=True, exist_ok=True)
|
||
return d
|
||
|
||
|
||
def progress_doc_path(task_id: str) -> Path:
|
||
return task_doc_dir(task_id) / "progress.md"
|
||
|
||
|
||
def init_progress_doc(task_id: str, knowledge_name: str, goal: str) -> None:
|
||
path = progress_doc_path(task_id)
|
||
if path.exists():
|
||
return
|
||
path.write_text(
|
||
f"""# 任务进度:{task_id}
|
||
|
||
- **业务**:{knowledge_name}
|
||
- **目标**:{goal}
|
||
- **创建时间**:{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
||
|
||
---
|
||
|
||
## 进度时间线
|
||
|
||
""",
|
||
encoding="utf-8",
|
||
)
|
||
|
||
|
||
def append_progress(
|
||
task_id: str,
|
||
step: str,
|
||
status: str,
|
||
detail: str = "",
|
||
*,
|
||
level: str = "info",
|
||
) -> None:
|
||
"""追加一条进度记录到 progress.md"""
|
||
path = progress_doc_path(task_id)
|
||
if not path.exists():
|
||
init_progress_doc(task_id, "未知", "")
|
||
|
||
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||
icon = {"info": "ℹ️", "success": "✅", "warning": "⚠️", "error": "❌", "pause": "⏸️"}.get(level, "•")
|
||
|
||
block = f"""### {icon} [{ts}] {step}
|
||
|
||
- **状态**:{status}
|
||
"""
|
||
if detail:
|
||
block += f"- **详情**:{detail}\n"
|
||
block += "\n"
|
||
|
||
with path.open("a", encoding="utf-8") as f:
|
||
f.write(block)
|
||
|
||
|
||
def write_summary(task_id: str, summary: str) -> None:
|
||
path = progress_doc_path(task_id)
|
||
section = f"""
|
||
---
|
||
|
||
## 总结
|
||
|
||
{summary}
|
||
|
||
*更新时间:{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}*
|
||
"""
|
||
with path.open("a", encoding="utf-8") as f:
|
||
f.write(section)
|