72 lines
3.6 KiB
TypeScript
72 lines
3.6 KiB
TypeScript
/**
|
|
* 用途:规范化智能体输出的 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, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
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>`;
|
|
}
|