6.9 KiB
6.9 KiB
Node 后端知识图谱
日期:2026-04-08
1. 当前定位
当前运行时后端以 server-node/ 为唯一有效服务端实现。
当前职责:
- 承接运行时鉴权
- 承接运行时持久化
- 承接运行时 AI 接口
- 承接编辑器写盘与资产生成工具接口
- 为 Vite 前端提供开发期代理目标
当前不再使用:
- 已删除的旧 Vite 本地 API 插件链路
scripts/dev-server/*.ts
2. 技术栈
- HTTP 框架:
Express - 语言与构建:
TypeScript+tsx+esbuild - 数据库:
PostgreSQL - JWT:
jose - 密码哈希:
@node-rs/argon2 - 日志:
pino+pino-http+pino-roll
3. 运行入口
推荐命令:
npm run dev
相关脚本:
- 根目录联调:
npm run dev/npm run dev:node - 仅前端开发:
npm run dev:web - 单独启动后端开发模式:
npm run server-node:dev - 构建后端:
npm run server-node:build - 运行后端测试:
npm run server-node:test
默认监听:
- 前端:
3000 - Node 后端:
8081
4. 目录与主入口
服务端主入口:
server-node/src/server.tsserver-node/src/app.ts
路由入口:
server-node/src/routes/authRoutes.tsserver-node/src/routes/runtimeRoutes.tsserver-node/src/modules/editor/editorRoutes.tsserver-node/src/modules/assets/characterAssetRoutes.tsserver-node/src/modules/assets/qwenSpriteRoutes.ts
基础设施:
server-node/src/config.tsserver-node/src/logging.tsserver-node/src/db.tsserver-node/src/context.ts
数据访问:
server-node/src/repositories/userRepository.tsserver-node/src/repositories/runtimeRepository.ts
鉴权相关:
server-node/src/auth/authService.tsserver-node/src/auth/token.tsserver-node/src/auth/password.tsserver-node/src/middleware/auth.ts
5. 鉴权模型
当前采用:
- 前端本地保存
JWT + 自动生成的用户名密码 - 请求头使用
Authorization: Bearer <token> - 后端 middleware 统一解析出
UserID - handler 不直接解析 token
当前账号策略:
- 默认自动匿名账号启动
- 本地无 JWT 时,前端会自动生成随机用户名密码并调用
POST /api/auth/entry - 本地 JWT 失效但仍保留随机凭据时,前端自动重新调用
auth/entry恢复同一账号
JWT 现状:
- 当前为永久签发
- claim 仍保留:
sub、iat、iss、ver logout通过递增token_version立即失效旧 token
6. 数据存储
当前数据库:
- 当前运行时持久化已切换到
PostgreSQL - 连接信息由后端
DATABASE_URL环境变量注入 - 不再以本地
SQLite文件作为正式运行时数据库 - 后端测试默认使用
pg-mem作为内存级 PostgreSQL 兼容实现 - 基础表初始化通过
schema_migrations基线管理 - 可通过
npm run server-node:db:migrate主动校验并补齐数据库基线
当前核心表:
userssave_snapshotsruntime_settingscustom_world_profiles
当前隔离原则:
- 所有运行时数据按用户隔离
7. 已承接接口
鉴权:
POST /api/auth/entryGET /api/auth/mePOST /api/auth/logout
运行时持久化:
GET /api/runtime/save/snapshotPUT /api/runtime/save/snapshotDELETE /api/runtime/save/snapshotGET /api/runtime/settingsPUT /api/runtime/settingsGET /api/runtime/custom-world-libraryPUT /api/runtime/custom-world-library/:profileIdDELETE /api/runtime/custom-world-library/:profileId
运行时 AI:
POST /api/llm/chat/completionsPOST /api/custom-world/scene-imagePOST /api/runtime/story/initialPOST /api/runtime/story/continuePOST /api/runtime/custom-world/agent/sessionsGET /api/runtime/custom-world/agent/sessions/:sessionIdPOST /api/runtime/custom-world/agent/sessions/:sessionId/messagesPOST /api/runtime/custom-world/agent/sessions/:sessionId/actionsGET /api/runtime/custom-world/worksPOST /api/runtime/chat/character/suggestionsPOST /api/runtime/chat/character/summaryPOST /api/runtime/chat/character/reply/streamPOST /api/runtime/chat/npc/dialogue/streamPOST /api/runtime/chat/npc/recruit/streamPOST /api/runtime/items/runtime-intentPOST /api/runtime/quests/generate
补充说明(2026-04-19):
POST /api/custom-world/scene-image现在支持前端仅提交profile + landmark + userPrompt上下文,由后端统一补齐场景图 prompt 与默认 negative prompt。- runtime story option 的
interaction元数据现在由后端随 option 一并返回,前端不再本地按functionId重建 NPC / treasure 交互语义。
编辑器工具:
GET /api/editor/catalog/itemsGET /api/editor/json/:resourceIdPOST /api/editor/json/:resourceId
资产工具:
POST /api/assets/character-visual/generatePOST /api/assets/character-visual/publishGET /api/assets/character-visual/jobs/:taskIdPOST /api/assets/character-animation/generatePOST /api/assets/character-animation/publishGET /api/assets/character-animation/jobs/:taskIdPOST /api/assets/character-animation/import-videoGET /api/assets/character-animation/templates
编辑器与资产接口门禁:
EDITOR_API_ENABLED控制/api/editor/*ASSETS_API_ENABLED控制/api/assets/*- 非生产环境默认开启,生产环境默认关闭
8. Story 与 Custom World 现状
Story:
- Node 后端直接复用前端成熟 prompt 与归一化逻辑
- 服务端走
src/services/ai.ts中的严格版 story 生成链
Custom World:
- Node 后端当前已自持
server-node/src/modules/custom-world/**运行时模块 - 已承接
creator intent归一化、anchorPack / lockState推导、framework normalize、runtime profile compile customWorldOrchestrator与customWorldAgentFoundationDraftService已不再运行时 import 前端src/services/customWorld*.ts与src/types.jsserver-node/src/prompts/customWorldPrompts.ts已承接 foundation draft 与 scene image 使用的 custom world prompt source- 上述 prompt 迁移只改变源码归属位置,没有改动提示词正文
- 当前保留
session + answers + SSE progress/result/error协议 - 前端已支持接收真实阶段进度对象
9. 前端接入点
鉴权与请求:
src/services/apiClient.tssrc/services/authService.tssrc/components/auth/AuthGate.tsx
运行时服务层:
src/services/storageService.tssrc/services/aiService.ts
编辑器与资产工具层:
src/editor/shared/editorApiClient.tssrc/components/preset-editor/characterAssetStudioPersistence.ts
10. 当前 Vite 角色
Vite 当前只负责代理,不再提供本地 API 插件。
当前代理目标:
/api/auth/api/runtime/api/editor/api/assets/api/llm/api/custom-world/scene-image/api/ws
全部转发到 Node 后端。
scripts/dev-server/ 目录现仅保留 README 作为迁移说明,旧本地 API 实现代码已于 2026-04-19 删除。