1.排程管理新增检核规则提示词维护,用于传入Dify中作为检核规则
2.聊天界面内容不可复制问题修复
This commit is contained in:
1 parent
452f6ebc6d
commit
ff2d02051f
17 files changed
+355
-20
No files matched your search
@@ -258,6 +258,7 @@ def _run_generic_check_job(task: ScheduleTask, run: ScheduleRun) -> ScheduleExec
|
||||
log_prefix=cfg.log_prefix,
|
||||
fetch_sql=cfg.fetch_sql,
|
||||
pre_dify_fn=pre_dify_fn,
|
||||
dify_system_prompt=cfg.dify_system_prompt,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.models import Agent, ScheduleRun, ScheduleTask
|
||||
from app.services.schedule.sql_config import get_schedule_sql_config_by_task_id
|
||||
|
||||
|
||||
def build_dify_execution_snapshot(task: ScheduleTask) -> dict[str, Any]:
|
||||
"""Capture Dify agent + system prompt used at execution time for audit."""
|
||||
captured_at = datetime.utcnow().isoformat() + "Z"
|
||||
if not task.dify_agent_id:
|
||||
return {
|
||||
"dify_enabled": False,
|
||||
"dify_agent_id": None,
|
||||
"dify_agent_name": None,
|
||||
"dify_app_id": None,
|
||||
"dify_system_prompt": "",
|
||||
"captured_at": captured_at,
|
||||
}
|
||||
|
||||
cfg = get_schedule_sql_config_by_task_id(task.id)
|
||||
prompt = str(cfg.dify_system_prompt if cfg else "").strip()
|
||||
agent = db.session.get(Agent, task.dify_agent_id)
|
||||
return {
|
||||
"dify_enabled": True,
|
||||
"dify_agent_id": task.dify_agent_id,
|
||||
"dify_agent_name": getattr(agent, "name", None) if agent else None,
|
||||
"dify_app_id": getattr(agent, "dify_app_id", None) if agent else None,
|
||||
"dify_system_prompt": prompt,
|
||||
"captured_at": captured_at,
|
||||
}
|
||||
|
||||
|
||||
def capture_dify_execution_snapshot(run: ScheduleRun, task: ScheduleTask) -> dict[str, Any]:
|
||||
snapshot = build_dify_execution_snapshot(task)
|
||||
run.dify_execution_snapshot = snapshot
|
||||
return snapshot
|
||||
@@ -54,6 +54,7 @@ def serialize_run(run: ScheduleRun) -> dict:
|
||||
'api_response_json': run.api_response_json,
|
||||
'response_summary_json': run.response_summary_json,
|
||||
'step_logs': run.step_logs,
|
||||
'dify_execution_snapshot': run.dify_execution_snapshot,
|
||||
'error_message': run.error_message,
|
||||
'excel_file_name': run.excel_file_name,
|
||||
'excel_file_size': run.excel_file_size,
|
||||
|
||||
@@ -14,6 +14,7 @@ from app.models import ScheduleRun, ScheduleTask, User
|
||||
from app.services.schedule import normalize_platform_api_path, resolve_schedule_executor, resolve_schedule_header
|
||||
from app.services.schedule.sql_config import get_schedule_sql_config_by_task_id
|
||||
from app.services.schedule.excel_service import export_schedule_result_excel
|
||||
from app.services.schedule.dify_snapshot import capture_dify_execution_snapshot
|
||||
|
||||
_worker_started = False
|
||||
_worker_lock = threading.Lock()
|
||||
@@ -248,6 +249,9 @@ def run_task_now(task: ScheduleTask, *, trigger_type: str, triggered_by_user_id:
|
||||
db.session.add(run)
|
||||
db.session.commit()
|
||||
|
||||
capture_dify_execution_snapshot(run, task)
|
||||
db.session.commit()
|
||||
|
||||
if task.cron_expr and trigger_type == "cron":
|
||||
try:
|
||||
iter = croniter(task.cron_expr, datetime.utcnow())
|
||||
|
||||
@@ -94,6 +94,7 @@ class ScheduleSqlConfig:
|
||||
data_source: DataSourceConfig = field(default_factory=DataSourceConfig)
|
||||
external_api: ExternalApiConfig = field(default_factory=ExternalApiConfig)
|
||||
data_source_type: str = "database"
|
||||
dify_system_prompt: str = ""
|
||||
|
||||
|
||||
def _build_fetch_sql(base: str, where_conds: str, order_by: str) -> str:
|
||||
@@ -153,6 +154,7 @@ def _config_from_model(cfg_model: Any) -> ScheduleSqlConfig:
|
||||
data_source=ds,
|
||||
external_api=ext,
|
||||
data_source_type=str(task.data_source_type if task else "database"),
|
||||
dify_system_prompt=str(cfg_model.dify_system_prompt or ""),
|
||||
)
|
||||
|
||||
|
||||
@@ -233,6 +235,8 @@ def upsert_schedule_task_config(task_id: int, data: dict[str, Any]) -> ScheduleS
|
||||
setattr(cfg_model, k, list(v) if isinstance(v, (list, tuple)) else [])
|
||||
elif k == "header_labels":
|
||||
setattr(cfg_model, k, dict(v) if isinstance(v, dict) else {})
|
||||
elif k == "dify_system_prompt":
|
||||
setattr(cfg_model, k, str(v) if v is not None else None)
|
||||
|
||||
db.session.commit()
|
||||
db.session.refresh(cfg_model)
|
||||
|
||||
@@ -117,6 +117,17 @@ def _truncate_str(s: str, max_len: int = _MAX_DIFY_RESPONSE_LEN) -> str:
|
||||
return s[:max_len] + f"...[truncated, total {len(s)} chars]"
|
||||
|
||||
|
||||
def _serialize_dify_inputs_for_log(inputs: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Log Dify request inputs; keep system_prompt intact for audit."""
|
||||
out: dict[str, Any] = {}
|
||||
for k, v in inputs.items():
|
||||
if k == "system_prompt":
|
||||
out[k] = str(v)
|
||||
else:
|
||||
out[k] = _truncate_str(str(v), 500)
|
||||
return out
|
||||
|
||||
|
||||
def _run_check_job_loop(
|
||||
*,
|
||||
conn_str: str,
|
||||
@@ -134,6 +145,7 @@ def _run_check_job_loop(
|
||||
fetch_sql: str = "",
|
||||
max_rows: int = 0,
|
||||
pre_dify_fn: PreDifyFunc | None = None,
|
||||
dify_system_prompt: str = "",
|
||||
) -> tuple[int, int, int, int, list[dict[str, Any]], list[str], list[dict[str, Any]]]:
|
||||
from app.services.schedule.scheduler_service import is_run_cancelled
|
||||
|
||||
@@ -218,6 +230,9 @@ def _run_check_job_loop(
|
||||
"record": record_text,
|
||||
"attachment_url": attachment_url,
|
||||
}
|
||||
prompt = str(dify_system_prompt or "").strip()
|
||||
if prompt:
|
||||
dify_inputs["system_prompt"] = prompt
|
||||
t_dify = _ts()
|
||||
try:
|
||||
dify_res = DifyService.run_workflow_streaming(
|
||||
@@ -228,7 +243,10 @@ def _run_check_job_loop(
|
||||
)
|
||||
except Exception as dify_exc:
|
||||
step_logs.append(_make_step("call_dify", "failed", t_dify, _ts(), row_id=row_id_value, detail={
|
||||
"request": {"inputs": {k: _truncate_str(str(v), 500) for k, v in dify_inputs.items()}, "agent_id": getattr(agent, "id", None)},
|
||||
"request": {
|
||||
"inputs": _serialize_dify_inputs_for_log(dify_inputs),
|
||||
"agent_id": getattr(agent, "id", None),
|
||||
},
|
||||
"error": str(dify_exc),
|
||||
}))
|
||||
raise
|
||||
@@ -243,7 +261,10 @@ def _run_check_job_loop(
|
||||
dify_message = f"Dify执行失败:{err_detail}"
|
||||
|
||||
step_logs.append(_make_step("call_dify", "failed", t_dify, _ts(), row_id=row_id_value, detail={
|
||||
"request": {"inputs": {k: _truncate_str(str(v), 500) for k, v in dify_inputs.items()}, "agent_id": getattr(agent, "id", None)},
|
||||
"request": {
|
||||
"inputs": _serialize_dify_inputs_for_log(dify_inputs),
|
||||
"agent_id": getattr(agent, "id", None),
|
||||
},
|
||||
"response": _truncate_str(str(dify_res)),
|
||||
"workflow_run_id": workflow_run_id,
|
||||
}))
|
||||
@@ -271,7 +292,10 @@ def _run_check_job_loop(
|
||||
dify_ok = True
|
||||
|
||||
step_logs.append(_make_step("call_dify", "success", t_dify, _ts(), row_id=row_id_value, detail={
|
||||
"request": {"inputs": {k: _truncate_str(str(v), 500) for k, v in dify_inputs.items()}, "agent_id": getattr(agent, "id", None)},
|
||||
"request": {
|
||||
"inputs": _serialize_dify_inputs_for_log(dify_inputs),
|
||||
"agent_id": getattr(agent, "id", None),
|
||||
},
|
||||
"response": {
|
||||
"status": dify_status,
|
||||
"dify_result": dify_result,
|
||||
|
||||
Reference in new issue
Block a user