feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制
This commit is contained in:
1 parent
ce26d6d0c4
commit
c4095b08da
175 files changed
+13064
-1814
No files matched your search
@@ -519,6 +519,25 @@ class DifyService:
|
||||
out["_workflow_error_type"] = error_type
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def _observe_workflow_event(event_observer, event_type: str, event_data: dict[str, Any], state: dict[str, Any]) -> None:
|
||||
"""向可选观察器发送脱敏 Workflow 生命周期字段;观察器错误不得中断Provider调用。"""
|
||||
if event_observer is None:
|
||||
return
|
||||
node_data = event_data.get('node_data') if isinstance(event_data.get('node_data'), dict) else {}
|
||||
try:
|
||||
event_observer({
|
||||
'event_type': str(event_type),
|
||||
'workflow_run_id': event_data.get('workflow_run_id') or state.get('workflow_run_id'),
|
||||
'node_execution_id': event_data.get('node_execution_id') or event_data.get('node_id'),
|
||||
'node_type': event_data.get('node_type') or node_data.get('type'),
|
||||
'node_title': event_data.get('title') or node_data.get('title'),
|
||||
'event_status': event_data.get('status'),
|
||||
})
|
||||
except Exception:
|
||||
if current_app:
|
||||
current_app.logger.exception('[workflow-streaming] event observer failed')
|
||||
|
||||
@staticmethod
|
||||
def _consume_workflow_stream(
|
||||
*,
|
||||
@@ -527,6 +546,7 @@ class DifyService:
|
||||
payload: dict[str, Any],
|
||||
timeout: tuple[int, int],
|
||||
state: dict[str, Any],
|
||||
event_observer=None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
执行一次 Workflow streaming POST 并消费 SSE 直至 workflow_finished。
|
||||
@@ -558,6 +578,8 @@ class DifyService:
|
||||
continue
|
||||
|
||||
event_type = evt.get("event", "")
|
||||
event_data = evt.get('data') if isinstance(evt.get('data'), dict) else {}
|
||||
DifyService._observe_workflow_event(event_observer, str(event_type), event_data, state)
|
||||
|
||||
if event_type == "workflow_started":
|
||||
run_id = (evt.get("data") or {}).get("workflow_run_id")
|
||||
@@ -596,6 +618,7 @@ class DifyService:
|
||||
inputs: dict[str, Any] | None = None,
|
||||
user: str = "system",
|
||||
timeout: tuple[int, int] = (5, 300),
|
||||
event_observer=None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
调用 Dify Workflow(streaming),并在断连/超时时优先 poll 补偿结果。
|
||||
@@ -639,6 +662,7 @@ class DifyService:
|
||||
payload=payload,
|
||||
timeout=timeout,
|
||||
state=stream_state,
|
||||
event_observer=event_observer,
|
||||
)
|
||||
workflow_run_id = stream_state.get("workflow_run_id")
|
||||
if workflow_run_id:
|
||||
@@ -647,6 +671,9 @@ class DifyService:
|
||||
# [STEP] 连接正常结束但未收到 workflow_finished 时,用 poll 补偿最终结果
|
||||
if not result and workflow_run_id:
|
||||
poll_used = True
|
||||
DifyService._observe_workflow_event(
|
||||
event_observer, 'poll_started', {'workflow_run_id': workflow_run_id}, stream_state,
|
||||
)
|
||||
if current_app:
|
||||
current_app.logger.warning(
|
||||
"[workflow-streaming] stream closed without finished event, polling run_id=%s",
|
||||
@@ -657,6 +684,10 @@ class DifyService:
|
||||
workflow_run_id=workflow_run_id,
|
||||
max_wait=max_wait,
|
||||
)
|
||||
DifyService._observe_workflow_event(
|
||||
event_observer, 'recovered' if result else 'stream_interrupted',
|
||||
{'workflow_run_id': workflow_run_id, 'status': result.get('status') if result else 'timeout'}, stream_state,
|
||||
)
|
||||
poll_error_type = None if result else "poll_timeout"
|
||||
return DifyService._attach_workflow_reliability_meta(
|
||||
result,
|
||||
@@ -713,6 +744,9 @@ class DifyService:
|
||||
|
||||
if workflow_run_id:
|
||||
poll_used = True
|
||||
DifyService._observe_workflow_event(
|
||||
event_observer, 'stream_interrupted', {'workflow_run_id': workflow_run_id}, stream_state,
|
||||
)
|
||||
if current_app:
|
||||
current_app.logger.warning(
|
||||
"[workflow-streaming] polling after disconnect run_id=%s error_type=%s",
|
||||
@@ -724,6 +758,10 @@ class DifyService:
|
||||
workflow_run_id=workflow_run_id,
|
||||
max_wait=max_wait,
|
||||
)
|
||||
DifyService._observe_workflow_event(
|
||||
event_observer, 'recovered' if result else 'stream_interrupted',
|
||||
{'workflow_run_id': workflow_run_id, 'status': result.get('status') if result else 'timeout'}, stream_state,
|
||||
)
|
||||
poll_error_type = None if result else "poll_timeout"
|
||||
return DifyService._attach_workflow_reliability_meta(
|
||||
result,
|
||||
@@ -735,6 +773,9 @@ class DifyService:
|
||||
|
||||
if retryable and attempt < max_attempts - 1:
|
||||
retry_count += 1
|
||||
DifyService._observe_workflow_event(
|
||||
event_observer, 'retry_started', {'workflow_run_id': workflow_run_id}, stream_state,
|
||||
)
|
||||
if current_app:
|
||||
current_app.logger.info(
|
||||
"[workflow-streaming] retrying attempt=%s error_type=%s",
|
||||
|
||||
Reference in new issue
Block a user