Skip to content

fix(mermaid): render flowchart sources missing the graph TD declaration - #39

Merged
taekchef merged 3 commits into
omdsh-dev:mainfrom
passionjj:fix/mermaid-undeclared-graph-kind
Aug 21, 2026
Merged

fix(mermaid): render flowchart sources missing the graph TD declaration#39
taekchef merged 3 commits into
omdsh-dev:mainfrom
passionjj:fix/mermaid-undeclared-graph-kind

Conversation

@passionjj

Copy link
Copy Markdown
Contributor

Problem

Model-generated mermaid (e.g. from chat assistants) frequently omits the mandatory diagram-type declaration line - A[高帧率摄像头 120-240fps] --> B[...] with no leading graph TD. The kind whitelist in renderMermaid rejects such sources before rendering ever runs, so the mermaid node falls back to the raw-source error display even though the body is a perfectly valid flowchart.

Fix

  • src/client/mermaid-safe.ts - new pure function ensureFlowchartKind(code): when the first token is not an already-declared graph/flowchart kind but the body contains flowchart edges (-->, ==>, -.->; negative lookahead keeps sequence -->> messages from matching), prepend graph TD. Everything else - declared kinds, sequence/gantt/pie bodies, plain text - passes through untouched: the diagram type is never guessed.
  • src/client/mermaid-core.ts - renderMermaid wires it into the kind gate: undeclared flowchart bodies now render instead of throwing straight to the fallback; genuinely unclassifiable input still fails as before.
  • src/client/mermaid-lazy.ts - re-export so the shared client bundle carries it.
  • tests - 8 new cases in genui-mermaid-safe.spec.ts (21 -> 29, all green).
  • lib artifacts - minimal rebuild (lib/assets/mermaid.js, lib/client.js, two .d.ts) so the published bundle carries the fix.

Verification

  • jsdom end-to-end: an undeclared flowchart source renders to SVG; declared graphs unchanged; sequence-diagram bodies and plain text are still rejected (no type guessing).
  • node --check passes on both bundles.
  • Full suite: 296 passed / 104 skipped, no new failures (the 14 pre-existing failures are Windows-local environment items - install-script chmod / skill-md yaml version - reproduced identically on the pristine baseline).

Notes

  • Changelog entry added under [Unreleased].

passionjj and others added 3 commits August 20, 2026 16:19
Model-generated mermaid (chat assistants) frequently omits the mandatory
diagram-type declaration line - 'A[高帧率摄像头 120-240fps] --> B[...]' with
no leading 'graph TD' - so the kind whitelist rejects the source before
renderMermaid runs and the mermaid node falls back to the raw-source error
display.

Add ensureFlowchartKind (mermaid-safe.ts, pure): when the first token is not
an already-declared graph/flowchart kind but the body contains flowchart
edges ('-->', '==>', '-.->'; negative lookahead keeps sequence '-->>'
messages from matching), prepend 'graph TD'. renderMermaid (mermaid-core.ts)
wires it into the kind gate; declared kinds, sequence/gantt/pie bodies and
plain text pass through untouched - the diagram type is never guessed.

Adds 8 tests (21 -> 29 in genui-mermaid-safe) and rebuilds the lib artifacts
(lib/assets/mermaid.js, lib/client.js, lib/types) with a minimal diff.
The previously committed bundles were built against a drifted local
toolchain, so CI's 'Verify lib matches src' gate failed. Rebuilt with
'pnpm install --frozen-lockfile' + 'pnpm run build' so the committed
artifacts are byte-identical to CI's rebuild.
@taekchef

Copy link
Copy Markdown
Collaborator

@passionjj 感谢这个修复 🙏 方向和实现都很好:ensureFlowchartKind 只在「未声明类型 + 正文含流程图边」时补 graph TD,负向前瞻排除时序图 -->>,不做任何类型猜测——这正是模型输出降级的第一大来源,8 个新用例也覆盖得很全。我在本地完整跑了全量测试:310 passed / 104 skipped、0 失败。

CI 红的唯一原因是 Verify lib matches src 硬门:PR 里提交的 lib 产物是用与本仓 lockfile 不一致的工具链构建的,和 CI 重建结果有压缩变量名级别的漂移。由于 #40 已合入 main、lib/client.js 也有冲突,我顺手代劳了两件机械操作(已直接推到你的分支,commit 93c719a):

  1. git merge origin/main(唯一冲突是构建产物 lib/client.js);
  2. pnpm install --frozen-lockfile + pnpm run build 重建并提交了 lib 产物,保证与 CI 重建逐字节一致。

CI 转绿后我就合并,感谢贡献 ✨

@taekchef
taekchef merged commit da6b6f9 into omdsh-dev:main Aug 21, 2026
2 checks passed
taekchef added a commit to jordanking211/dsh-awesome-genui that referenced this pull request Aug 22, 2026
…o feat/echarts-support; lib to be rebuilt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants