feat: expose acceptance Link AI aggregate data

This commit is contained in:
2026-08-20 17:07:55 +08:00
parent fd83c2d1b2
commit 6bc0f16006
3 changed files with 76 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ from crawler_client import (
collect_keyword_sources,
fetch_content_detail,
get_wen_resources,
match_feishu_notes,
query_collection,
schedule_xapi_screenshots,
)
@@ -965,3 +966,61 @@ def list_acceptance_notes(public_id: str) -> dict[str, Any]:
item["parsed_data"] = json.loads(item.pop("raw_json")) if item.get("raw_json") else None
items.append(item)
return {"items": items, "total": len(items)}
def get_acceptance_link_ai_data(public_id: str) -> dict[str, Any]:
"""返回 Link AI 所需的笔记、AI 采集结果和逐关键词比对结果。"""
with connection() as conn:
task = conn.execute(
"SELECT * FROM acceptance_tasks WHERE public_task_id=?", (public_id,),
).fetchone()
if not task:
raise TaskError("ACCEPTANCE_TASK_NOT_FOUND", "验收任务不存在", 404)
task_data = _task_dict(conn, task)
rows = conn.execute(
"""SELECT a.keyword,a.credential_id,a.attempt_number,a.crawler_task_id,
a.result_json,a.completed_at
FROM acceptance_cookie_attempts a
WHERE a.task_id=? AND a.status='completed' AND a.result_json IS NOT NULL
ORDER BY a.keyword,a.id""",
(task["id"],),
).fetchall()
ai_results = []
for row in rows:
try:
result = json.loads(row["result_json"])
except (TypeError, json.JSONDecodeError):
continue
ai_results.append({
"keyword": row["keyword"],
"credential_id": row["credential_id"],
"attempt_number": row["attempt_number"],
"crawler_task_id": row["crawler_task_id"],
"completed_at": row["completed_at"],
"result": result,
})
comparisons = []
for keyword in task_data["keywords"]:
try:
comparisons.append({
"keyword": keyword,
"status": "completed",
"data": match_feishu_notes(keyword),
})
except Exception as exc:
comparisons.append({
"keyword": keyword,
"status": "failed",
"error": str(exc),
"data": None,
})
return {
"task": task_data,
"notes": list_acceptance_notes(public_id),
"ai_results": {"items": ai_results, "total": len(ai_results)},
"note_matches": {"items": comparisons, "total": len(comparisons)},
"data_scope": "仅包含当前已完成并已入库的数据",
}