Web 前端架构
Web 前端架构
相关源文件
本章引用的主要源码文件:
.github/actions/setup-web/action.ymlapi/Dockerfilepackage.jsonpackages/dify-ui/README.mdpackages/dify-ui/src/dialog/index.tsxpackages/dify-ui/src/toast/__tests__/index.spec.tsxpackages/dify-ui/vite.config.tspackages/dify-ui/vitest.setup.tspnpm-lock.yamlpnpm-workspace.yamlsdks/nodejs-client/tsconfig.jsonsdks/nodejs-client/vite.config.tsvite.config.tsweb/.env.exampleweb/Dockerfileweb/app/components/base/chat/chat/answer/workflow-process.tsxweb/app/components/base/form/index.stories.tsxweb/app/components/header/account-setting/members-page/edit-workspace-modal/__tests__/index.spec.tsxweb/app/components/workflow/__tests__/workflow-edge-events.spec.tsxweb/app/components/workflow/collaboration/core/__tests__/collaboration-manager.logs-and-events.spec.tsweb/app/components/workflow/collaboration/core/__tests__/collaboration-manager.socket-and-subscriptions.spec.tsweb/app/components/workflow/collaboration/core/collaboration-manager.tsweb/app/components/workflow/collaboration/types/collaboration.tsweb/app/components/workflow/header/__tests__/header-in-restoring.spec.tsxweb/app/components/workflow/header/__tests__/header-layouts.spec.tsxweb/app/components/workflow/header/header-in-restoring.tsxweb/app/components/workflow/hooks/index.tsweb/app/components/workflow/hooks/use-nodes-interactions.tsweb/app/components/workflow/hooks/use-nodes-sync-draft.tsweb/app/components/workflow/hooks/use-workflow-interactions.tsweb/app/components/workflow/hooks/use-workflow.tsweb/app/components/workflow/index.tsxweb/app/components/workflow/nodes/_base/components/__tests__/node-handle.spec.tsxweb/app/components/workflow/nodes/_base/components/next-step/add.tsxweb/app/components/workflow/nodes/_base/components/next-step/index.tsxweb/app/components/workflow/nodes/_base/components/node-handle.tsxweb/app/components/workflow/nodes/_base/components/output-vars.tsxweb/app/components/workflow/nodes/_base/node.tsxweb/app/components/workflow/nodes/iteration/use-interactions.tsweb/app/components/workflow/nodes/loop/use-interactions.tsweb/app/components/workflow/operator/add-block.tsxweb/app/layout.tsxweb/config/index.tsweb/contract/router.tsweb/docker/entrypoint.shweb/eslint.config.mjsweb/eslint.constants.mjsweb/knip.config.tsweb/types/feature.tsweb/vite.config.ts
目的与范围
本文档涵盖了 Dify 前端应用的高层架构。前端基于 Next.js、TypeScript 和 React 构建,提供了用于提示工程、工作流编排和多模态聊天交互的复杂界面。它采用了基于 pnpm 和 Vite/Vitest 的现代构建系统进行测试,并利用共享组件库 dify-ui 实现一致的设计模式。
构建系统与配置
Dify 前端使用 Next.js 作为主要框架,在 pnpm workspace 中进行管理 pnpm-workspace.yaml:23-27。虽然 Next.js 负责生产环境的服务端渲染和路由,但项目集成了 Vinext 和 Vite+ 来增强开发体验,提供更快的 HMR(热模块替换)和更灵活的插件系统 web/vite.config.ts:4-6。
图表:前端构建与工具链
来源: pnpm-workspace.yaml:1-27, web/vite.config.ts:16-94, pnpm-lock.yaml:7-249
配置通过 ESLint 严格实施,使用 @antfu/eslint-config 作为基础,并添加了针对 Dify 特定规则的自定义插件,例如 i18n 键值校验和 Tailwind 类排序 web/eslint.config.mjs:21-166。
详情请参见 前端构建系统与配置。
组件与 UI 架构
Dify 的 UI 以集中式组件策略为核心。仓库中包含一个专用包 @langgenius/dify-ui,它提供了标准化的基础组件,如 ToastHost、TooltipProvider 和 AlertDialog web/app/layout.tsx:2-3, web/app/components/workflow/index.tsx:17-25。
应用外壳与上下文
根布局初始化了关键提供者,包括用于状态管理的 JotaiProvider、用于深色模式的 ThemeProvider,以及用于服务端状态管理的 TanstackQueryInitializer web/app/layout.tsx:61-82。环境变量通过 web/config/index.ts 和 web/env.ts 从后端映射,以确保一致的 API 前缀和功能开关 web/config/index.ts:18-33。
图表:前端运行时架构
来源: web/app/layout.tsx:61-85, web/app/components/workflow/index.tsx:43-53, pnpm-lock.yaml:183-248
主题与样式
样式由 Tailwind CSS 驱动,使用共享配置,并通过 @egoist/tailwindcss-icons 包含自定义图标 pnpm-lock.yaml:27-29。深色模式通过 next-themes 管理,并在文档级别应用 web/app/layout.tsx:5-63。
核心前端功能区域
前端分为多个专门的功能区域,每个区域都有复杂的状态和 UI 需求。
聊天与消息交互
该区域处理对话界面,包括流式消息响应、通过 js-audio-recorder 实现的音频播放(TTS),以及用户反馈机制 pnpm-lock.yaml:184-191。它使用专门的组件来渲染 Markdown 和 LaTeX web/app/layout.tsx:19, web/config/index.ts:188。 详情请参见 聊天界面与消息交互。
工作流编排 UI
工作流 UI 是一个基于 ReactFlow 的高度交互的图形编辑器 web/app/components/workflow/index.tsx:43-53。它管理复杂的节点交互,包括拖放、连接逻辑以及迭代/循环嵌套 web/app/components/workflow/hooks/use-nodes-interactions.ts:146-222。 详情请参见 工作流节点 UI 组件。
国际化(i18n)
Dify 通过 i18next 和 react-i18next 支持多语言界面 pnpm-lock.yaml:179-211。ESLint 规则严格强制 JSON 资源文件中的 i18n 键值一致性和占位符匹配 web/eslint.config.mjs:144-154。
测试基础设施
前端质量通过 Vitest 和 React Testing Library 来维护。环境使用 happy-dom 在 Node.js 中实现快速的、类似浏览器的测试 web/vite.config.ts:83-86。
| 工具 | 用途 |
|---|---|
| Vitest | 单元测试和集成测试运行器 web/vite.config.ts:83 |
| happy-dom | 用于测试的轻量级 DOM 实现 web/vite.config.ts:85 |
| Storybook | 可视化组件开发和文档 web/vite.config.ts:18-19 |
| Playwright | 端到端测试(位于 /e2e 目录) pnpm-workspace.yaml:25 |
来源: web/vite.config.ts:83-94, pnpm-lock.yaml:95-109, pnpm-workspace.yaml:23-27
子页面
- 前端构建系统与配置 — Next.js 配置、pnpm 包管理、Vite/Vitest 设置、环境变量映射以及
dify-ui共享组件库。 - 聊天界面与消息交互 — 操作组件、反馈系统、音频播放(TTS)和消息操作按钮。
- 工作流节点 UI 组件 — 工作流节点的面板组件、
useConfig钩子、列表操作 UI、HTTP 请求 UI、变量引用选择器和提示编辑器。