feat(schedule):cron支持图形,RoHS检核增加进度显示

This commit is contained in:
hugo committed 2026-06-15 13:42:15 +08:00
1 parent 6d0e78be81
commit f5aac9c893
46 files changed
+1987 -221

No files matched your search

+97 -29
View File
@@ -508,6 +508,7 @@ class DifyService:
conversation_id: str = None,
request_id: str | None = None,
session_id: str | None = None,
hxops_turn_id: str | None = None,
files: list[dict[str, Any]] | None = None,
inputs: dict[str, Any] | None = None,
):
@@ -571,14 +572,47 @@ class DifyService:
attribution_parts: List[str] = []
chat_persist_started = False
has_sent_answer = False
chat_log_id_persisted: int | None = None
run_id_persisted: int | None = None
def _resolve_persist_run_id() -> int | None:
try:
from app.services.rohs_check_tools.agent_resolver import is_rohs_check_chat_agent
from app.services.rohs_check_tools.chat_log_run_binding import resolve_run_id_for_chat_log
if not is_rohs_check_chat_agent(agent):
return None
return resolve_run_id_for_chat_log(
session_id=session_id,
query=query,
turn_id=hxops_turn_id,
)
except Exception:
return None
def persist_stream_chat(data: dict, event_name: str) -> None:
persist_stream_chat_async(data, event_name)
result = persist_stream_chat_async(data, event_name)
if isinstance(result, dict):
if result.get("chat_log_id") is not None:
data["chat_log_id"] = result["chat_log_id"]
if result.get("run_id") is not None:
data["run_id"] = result["run_id"]
def persist_stream_chat_async(data: dict, event_name: str) -> dict | None:
nonlocal chat_persist_started, chat_log_id_persisted, run_id_persisted
if chat_persist_started and event_name == "workflow_finished" and chat_log_id_persisted:
run_id = _resolve_persist_run_id()
if run_id and run_id != run_id_persisted:
from app.services.rohs_check_tools.chat_log_run_binding import update_chat_log_run_id
if update_chat_log_run_id(chat_log_id_persisted, run_id):
run_id_persisted = run_id
return {"chat_log_id": chat_log_id_persisted, "run_id": run_id_persisted}
def persist_stream_chat_async(data: dict, event_name: str) -> None:
nonlocal chat_persist_started
if chat_persist_started:
return
return {"chat_log_id": chat_log_id_persisted, "run_id": run_id_persisted} if chat_log_id_persisted else None
chat_persist_started = True
app_obj = current_app._get_current_object() if current_app else None
@@ -587,27 +621,33 @@ class DifyService:
attribution_snapshot = list(attribution_parts)
answer_snapshot = build_final_answer_text(persist_data, event_name)
latency_snapshot = int((time.time() - start_time) * 1000)
message_payload = persist_data.get('message_payload')
def _persist() -> None:
if not app_obj:
return
with app_obj.app_context():
DifyService._persist_stream_chat_record(
data=persist_data,
event_name=event_name,
request_id=request_id,
user_id=user_id,
agent_id=agent.id,
session_id=session_id,
normalized_conversation_id=normalized_conversation_id,
query=query,
answer_text=answer_snapshot,
latency=latency_snapshot,
retriever_resources=resources_snapshot,
attribution_parts=attribution_snapshot,
)
Thread(target=_persist, daemon=True, name=f"dify-chat-persist-{request_id or 'unknown'}").start()
if not app_obj:
return None
with app_obj.app_context():
result = DifyService._persist_stream_chat_record(
data=persist_data,
event_name=event_name,
request_id=request_id,
user_id=user_id,
agent=agent,
session_id=session_id,
hxops_turn_id=hxops_turn_id,
normalized_conversation_id=normalized_conversation_id,
query=query,
answer_text=answer_snapshot,
latency=latency_snapshot,
retriever_resources=resources_snapshot,
attribution_parts=attribution_snapshot,
message_payload=message_payload if isinstance(message_payload, dict) else None,
)
if isinstance(result, dict):
if result.get("chat_log_id"):
chat_log_id_persisted = int(result["chat_log_id"])
if result.get("run_id"):
run_id_persisted = int(result["run_id"])
return result
def build_final_answer_text(data: dict, event_name: str) -> str:
answer_text = ''.join(full_answer).strip()
@@ -790,6 +830,8 @@ class DifyService:
latency,
retriever_resources=None,
attribution_parts=None,
payload=None,
run_id: int | None = None,
) -> Optional[int]:
"""
把最终问答和元数据写入 ChatLog
@@ -803,6 +845,7 @@ class DifyService:
latency: End-to-end latency in milliseconds.
retriever_resources: Source retrieval metadata list.
attribution_parts: Attribution/annotation list.
run_id: RoHS 检核批次 ID(按轮问答绑定,可选)。
"""
try:
chat_log = ChatLog(
@@ -816,6 +859,8 @@ class DifyService:
latency=latency or 0,
retriever_resources=retriever_resources or [],
attribution_parts=attribution_parts or [],
payload=payload if isinstance(payload, dict) else None,
run_id=run_id if run_id and run_id > 0 else None,
)
db.session.add(chat_log)
db.session.commit()
@@ -835,17 +880,19 @@ class DifyService:
event_name: str,
request_id: str | None,
user_id: int,
agent_id: int,
agent,
session_id: str | None,
hxops_turn_id: str | None,
normalized_conversation_id: str | None,
query: str,
answer_text: str,
latency: int,
retriever_resources: List[Any],
attribution_parts: List[str],
) -> None:
message_payload: dict | None = None,
) -> dict | None:
if not answer_text and not str(query or '').strip():
return
return None
if current_app:
current_app.logger.info(
@@ -863,9 +910,23 @@ class DifyService:
usage = metadata.get('usage', {}) or {}
db_write_started = time.time()
run_id = None
try:
from app.services.rohs_check_tools.agent_resolver import is_rohs_check_chat_agent
from app.services.rohs_check_tools.chat_log_run_binding import resolve_run_id_for_chat_log
if is_rohs_check_chat_agent(agent):
run_id = resolve_run_id_for_chat_log(
session_id=session_id,
query=query,
turn_id=hxops_turn_id,
)
except Exception:
run_id = None
chat_log_id = DifyService._log_chat(
user_id=user_id,
agent_id=agent_id,
agent_id=agent.id,
session_id=session_id,
conversation_id=data.get('conversation_id') or normalized_conversation_id,
query=query,
@@ -874,17 +935,24 @@ class DifyService:
latency=latency,
retriever_resources=retriever_resources,
attribution_parts=attribution_parts,
payload=message_payload,
run_id=run_id,
)
if chat_log_id and current_app:
current_app.logger.info(
"[chat-perf] persisted async request_id=%s event=%s total_ms=%s db_write_ms=%s answer_len=%s conversation_id=%s",
"[chat-perf] persisted async request_id=%s event=%s total_ms=%s db_write_ms=%s answer_len=%s conversation_id=%s chat_log_id=%s run_id=%s",
request_id,
event_name,
latency,
int((time.time() - db_write_started) * 1000),
len(answer_text),
data.get('conversation_id') or normalized_conversation_id,
chat_log_id,
run_id,
)
if not chat_log_id:
return None
return {"chat_log_id": chat_log_id, "run_id": run_id}
@staticmethod
def _normalize_resources(resources):