feat(file-check): 增加文件检核 Excel 下载链接解析与展示功能
This commit is contained in:
1 parent
5e7e5a8717
commit
f21216f81e
21 files changed
+885
-125
No files matched your search
@@ -134,6 +134,98 @@ class DifyService:
|
||||
return "当前会话上下文过长,模型无法继续回答。请新建对话后重新查询。"
|
||||
return f"{resolved_agent_name}暂时无法完成查询,请稍后重试。"
|
||||
|
||||
@staticmethod
|
||||
def _merge_stream_answer_text(
|
||||
streamed_text: str,
|
||||
terminal_answer: str | None,
|
||||
*,
|
||||
replace_answer: bool = False,
|
||||
) -> str:
|
||||
"""
|
||||
合并流式增量文本与终态 answer,避免 Dify 结束事件重复携带整段正文导致落库翻倍。
|
||||
核心逻辑:与前端 useChat.ts 终态事件去重策略保持一致。
|
||||
|
||||
Args:
|
||||
streamed_text: message 事件累积的增量文本。
|
||||
terminal_answer: message_end / workflow_finished 携带的 answer。
|
||||
replace_answer: 为 True 时仅使用终态 answer。
|
||||
|
||||
Returns:
|
||||
去重后的最终正文。
|
||||
"""
|
||||
current_content = str(streamed_text or '')
|
||||
terminal = str(terminal_answer or '')
|
||||
|
||||
if replace_answer and terminal.strip():
|
||||
return terminal.strip()
|
||||
if not terminal:
|
||||
return current_content.strip()
|
||||
if not current_content.strip():
|
||||
return terminal.strip()
|
||||
|
||||
current_stripped = current_content.strip()
|
||||
terminal_stripped = terminal.strip()
|
||||
if (
|
||||
terminal_stripped
|
||||
and current_stripped
|
||||
and len(terminal_stripped) >= len(current_stripped)
|
||||
and current_stripped in terminal_stripped
|
||||
):
|
||||
return terminal_stripped
|
||||
if terminal_stripped and current_stripped and terminal_stripped in current_stripped:
|
||||
return current_stripped
|
||||
return (current_content + terminal).strip()
|
||||
|
||||
@staticmethod
|
||||
def _build_final_answer_text(
|
||||
*,
|
||||
streamed_parts: list[str],
|
||||
data: dict,
|
||||
event_name: str,
|
||||
) -> str:
|
||||
"""
|
||||
根据流式片段与终态事件组装最终落库 answer。
|
||||
核心逻辑:先走 _merge_stream_answer_text 去重;workflow_finished 无 answer 时回退 outputs。
|
||||
"""
|
||||
streamed_text = ''.join(streamed_parts)
|
||||
terminal = str(data.get('answer') or '') if data.get('answer') else ''
|
||||
answer_text = DifyService._merge_stream_answer_text(
|
||||
streamed_text,
|
||||
terminal or None,
|
||||
replace_answer=bool(data.get('replace_answer')),
|
||||
)
|
||||
|
||||
if not answer_text and event_name == 'workflow_finished':
|
||||
wf_data = data.get('data') or {}
|
||||
outputs = wf_data.get('outputs') if isinstance(wf_data, dict) else None
|
||||
if isinstance(outputs, dict):
|
||||
for key in ('answer', 'final_answer', 'output', 'text', 'result', 'message'):
|
||||
value = outputs.get(key)
|
||||
if isinstance(value, str) and value.strip():
|
||||
answer_text = value.strip()
|
||||
break
|
||||
if not answer_text:
|
||||
for value in outputs.values():
|
||||
if isinstance(value, str) and value.strip():
|
||||
answer_text = value.strip()
|
||||
break
|
||||
return answer_text
|
||||
|
||||
@staticmethod
|
||||
def _dedupe_exact_doubled_text(text: str) -> str:
|
||||
"""
|
||||
修复历史落库时将同一段正文原样拼接两次的记录。
|
||||
核心逻辑:仅当整段文本可被均分为两段完全相同子串时才裁剪。
|
||||
"""
|
||||
normalized = str(text or '')
|
||||
stripped = normalized.strip()
|
||||
if len(stripped) < 2:
|
||||
return normalized
|
||||
half = len(stripped) // 2
|
||||
if len(stripped) % 2 == 0 and stripped[:half] == stripped[half:]:
|
||||
return stripped[:half]
|
||||
return normalized
|
||||
|
||||
@staticmethod
|
||||
def _normalize_parameters_payload(data: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
if not isinstance(data, dict):
|
||||
@@ -896,25 +988,11 @@ class DifyService:
|
||||
return result
|
||||
|
||||
def build_final_answer_text(data: dict, event_name: str) -> str:
|
||||
answer_text = ''.join(full_answer).strip()
|
||||
if data.get('answer'):
|
||||
answer_text = (answer_text + str(data.get('answer') or '')).strip()
|
||||
|
||||
if not answer_text and event_name == 'workflow_finished':
|
||||
wf_data = data.get('data') or {}
|
||||
outputs = wf_data.get('outputs') if isinstance(wf_data, dict) else None
|
||||
if isinstance(outputs, dict):
|
||||
for key in ('answer', 'final_answer', 'output', 'text', 'result', 'message'):
|
||||
value = outputs.get(key)
|
||||
if isinstance(value, str) and value.strip():
|
||||
answer_text = value.strip()
|
||||
break
|
||||
if not answer_text:
|
||||
for value in outputs.values():
|
||||
if isinstance(value, str) and value.strip():
|
||||
answer_text = value.strip()
|
||||
break
|
||||
return answer_text
|
||||
return DifyService._build_final_answer_text(
|
||||
streamed_parts=full_answer,
|
||||
data=data,
|
||||
event_name=event_name,
|
||||
)
|
||||
|
||||
|
||||
max_retries = 3
|
||||
|
||||
Reference in new issue
Block a user