fix(platform): 汇总知识库问答与图表展示修复

This commit is contained in:
HTEWJ\26070045 authored and sophie committed 2026-08-07 10:07:00 +08:00
1 parent f615e56bb9
commit 87ea4ed8a9
41 files changed
+4569 -770

No files matched your search

@@ -0,0 +1,28 @@
/**
* 用途:验证 Mermaid 兼容规范化,防止中文分组标题再次导致流程图解析失败。
* 依赖:vitest、mermaidChart。
* 逻辑:覆盖无 ID 中文 subgraph 的标准化以及原本标准语法的不变性。
*/
import { describe, expect, it } from 'vitest';
import { buildMermaidFlowFallbackHtml, normalizeMermaidChart } from './mermaidChart';
describe('normalizeMermaidChart', () => {
it('为中文 subgraph 标题补齐可解析的 ASCII ID', () => {
// 断言:Dify 常见的中文分组写法被转为 Mermaid 标准分组声明。
expect(normalizeMermaidChart('graph TD\nsubgraph 项目启动阶段\nA[接单]\nend'))
.toContain('subgraph group_1["项目启动阶段"]');
});
it('保留已有 ID 的标准 subgraph', () => {
// 断言:已规范的 Mermaid 不被重复改写。
expect(normalizeMermaidChart('subgraph phase_start["项目启动阶段"]'))
.toBe('subgraph phase_start["项目启动阶段"]');
});
it('将无法解析的主流程降级为可视化卡片', () => {
// 断言:即使 Mermaid 语法异常,用户仍能看到节点和顺序,而不是红色源码。
const html = buildMermaidFlowFallbackHtml('A[接单] --> B[项目立项]\nB --> C[项目启动阶段]');
expect(html).toContain('接单');
expect(html).toContain('项目启动阶段');
});
});
@@ -0,0 +1,71 @@
/**
* 用途:规范化智能体输出的 Mermaid 图表文本,兼容中文分组标题等常见非标准写法。
* 依赖:由 ChatMessageBubble 在调用 mermaid.render 前使用。
* 逻辑:保留合法语句不变;仅为没有 ID 的 subgraph 补充稳定 ASCII ID 与带引号标题,
* 避免 Mermaid 把中文标题解析为图语法 token 而导致整张流程图无法显示。
*/
/**
* 用途:将常见的中文 Mermaid 分组语法转换为 Mermaid 可稳定解析的标准形式。
* AI 触发时机:智能体返回 `subgraph 项目启动阶段` 等没有 ASCII 节点 ID 的分组标题时调用。
* 核心逻辑:逐行识别 subgraph;已经包含 ID、方括号或引号的标准写法不变;其余标题生成唯一 ID。
* 参数与输出约束:输入必须为 Mermaid 文本;输出只调整 subgraph 声明,不修改节点、连线及 end 语句。
*/
export function normalizeMermaidChart(source: string): string {
let subgraphIndex = 0;
return String(source || '')
.replace(/^\s*graph\s+/im, (matched) => matched.replace(/graph/i, 'flowchart'))
.split('\n')
.map((line) => {
const match = line.match(/^(\s*)subgraph\s+(.+?)\s*$/i);
if (!match) return line;
const indent = match[1] || '';
const rawTitle = (match[2] || '').trim();
if (!rawTitle || /[\[\]"']/.test(rawTitle) || /^[A-Za-z_][A-Za-z0-9_-]*\s/.test(rawTitle)) return line;
subgraphIndex += 1;
return `${indent}subgraph group_${subgraphIndex}["${rawTitle.replace(/"/g, '\\"')}"]`;
})
.join('\n')
// Mermaid 对含括号、标点或中文的节点标签需要显式引号;Dify 常会遗漏它。
.replace(/([A-Za-z][A-Za-z0-9_-]*)\[([^\]"\n]+)\]/g, (_matched, nodeId: string, label: string) => (
`${nodeId}["${label.trim().replace(/"/g, '\\"')}"]`
));
}
/**
* 用途:在 Mermaid 仍无法解析时,将简单流程图转换为可读的流程卡片。
* AI 触发时机:图表库抛出语法异常且原文中存在 `A[节点] --> B[节点]` 这类链路时调用。
* 核心逻辑:提取节点顺序和标签,输出已转义的 HTML 卡片;忽略不影响主流程的 subgraph 细节。
* 参数与输出约束:只用于展示兜底,不执行任何模型输出中的 HTML 或脚本。
*/
export function buildMermaidFlowFallbackHtml(source: string): string | null {
const labels = new Map<string, string>();
const order: string[] = [];
const text = String(source || '');
const nodePattern = /([A-Za-z][A-Za-z0-9_-]*)\s*\[\s*"?([^\]\n"]+)"?\s*\]/g;
const edgePattern = /([A-Za-z][A-Za-z0-9_-]*)(?:\s*\[[^\]\n]*\])?\s*--+>\s*([A-Za-z][A-Za-z0-9_-]*)(?:\s*\[[^\]\n]*\])?/g;
const escapeHtml = (value: string) => String(value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
for (const match of text.matchAll(nodePattern)) {
const nodeId = match[1];
const label = match[2];
if (nodeId && label) labels.set(nodeId, label.trim());
}
for (const match of text.matchAll(edgePattern)) {
const fromId = match[1];
const toId = match[2];
if (!fromId || !toId) continue;
if (!order.includes(fromId)) order.push(fromId);
if (!order.includes(toId)) order.push(toId);
}
if (order.length < 2) return null;
const cards = order.map((id, index) => {
const card = `<div class="chat-flow-fallback-card">${escapeHtml(labels.get(id) || id)}</div>`;
return index < order.length - 1 ? `${card}<div class="chat-flow-fallback-arrow" aria-hidden="true">↓</div>` : card;
});
return `<div class="chat-flow-fallback" role="img" aria-label="流程图">${cards.join('')}</div>`;
}