Loading...
正在加载...
请稍候

JManus 分析

✨步子哥 (steper) 2025年11月08日 16:45
## 项目概览 - **项目定位**:JManus 是 Spring Boot 驱动的多智能体计划执行平台,目标是在企业环境中提供强确定性、可审计的 AI 工作流编排能力。前端采用 Vue3 + Ant Design Vue,提供图形化的计划编排、运行与监控界面。 - **总体形态**:单体式 Spring Boot 应用承载全部业务 API、计划执行内核、工具体系与持久化层,并通过静态资源方式托管 Vue 前端;同时兼容 Docker 与 Native Image(GraalVM)场景。 - **核心能力**:Plan-Act 模式的计划生成与调度、工具调用(数据库、浏览器、Bash、OCR 等)、执行轨迹记录、对话上下文记忆、MCP(Model Context Protocol)扩展、开放的 HTTP API 以及 Cron 定时。 - **运行依赖**:Java 17+、默认 H2 内嵌数据库(可切换 MySQL/PostgreSQL)、外部 LLM 服务(DashScope 等)、可选 Playwright/ChromeDriver。 ## 总体架构蓝图 - **界面层**:`ui-vue3` 子仓负责所有交互式 UI,打包后进入 `src/main/resources/static` 并由 Spring Boot 静态资源端点提供。 - **服务层**:Spring Boot 暴露 `/api/*` REST API;高层 API 聚焦计划执行(`ManusController`)、计划模板、配置与工具编排。 - **执行内核**:`planning`、`runtime`、`agent`、`tool`、`recorder` 等包构成执行链路,从计划解析、Agent 分发、工具调用到执行记录。 - **数据与配置层**:基于 Spring Data JPA 与 `Config`/`Recorder` 等实体包实现持久化,`config` 包提供动态配置、启动加载与属性映射。 - **外部集成层**:`llm` 负责与 LLM 服务交互,`mcp` 引入 MCP 服务与工具,`adapter` 提供 OpenAI 协议兼容层,`deploy` 目录封装容器化部署脚本。 - **事件与任务调度**:`event` 包提供领域事件总线,`cron` 包装 Spring Scheduling,实现计划的定时执行与监控。 ## 后端架构 ### 核心分层与入口 - **启动入口**:`OpenManusSpringBootApplication` 统一启用 Scheduling、JPA、Component 扫描,并提供 Playwright 初始化脚本开关。 ```36:44:src/main/java/com/alibaba/cloud/ai/manus/OpenManusSpringBootApplication.java else { SpringApplication.run(OpenManusSpringBootApplication.class, args); } ``` - **分层结构**: - `controller`:暴露 REST API,处理任务提交、计划模板、配置与工具操作。 - `service`:封装业务流程(计划生成、工具注册、执行协调、配置同步等)。 - `entity`/`vo`:对应数据库表与前端展示模型,支持计划树、执行记录、配置项等。 - `repository`:Spring Data JPA 仓储接口。 - `runtime` 与 `planning`:执行内核,负责解析计划、分配 Agent、调度工具与记录执行。 - `tool`:工具插件集合,采用 `ToolCallBiFunctionDef` 标准封装工具元数据、输入参数与执行逻辑。 ### 计划执行链路 - **计划装配与工具注册**:`PlanningFactory` 聚合浏览器、数据库、Cron 等工具的回调注册,并按 MCP 服务动态扩展工具列表。 ```207:306:src/main/java/com/alibaba/cloud/ai/manus/planning/PlanningFactory.java List functionCallbacks = mcpService.getFunctionCallbacks(planId); for (McpServiceEntity toolCallback : functionCallbacks) { String serviceGroup = toolCallback.getServiceGroup(); ToolCallback[] tCallbacks = toolCallback.getAsyncMcpToolCallbackProvider().getToolCallbacks(); for (ToolCallback tCallback : tCallbacks) { toolDefinitions.add(new McpTool(tCallback, serviceGroup, planId, innerStorageService, objectMapper)); } } // Create FunctionToolCallback for each tool for (ToolCallBiFunctionDef toolDefinition : toolDefinitions) { ``` - **执行协调**:`PlanningCoordinator` 将计划转换为 `ExecutionContext`,交由 `PlanExecutorFactory` 选择具体执行器,并在执行完成后进入 `PlanFinalizer`。 ```69:131:src/main/java/com/alibaba/cloud/ai/manus/runtime/service/PlanningCoordinator.java public CompletableFuture executeByPlan(PlanInterface plan, String rootPlanId, String parentPlanId, String currentPlanId, String toolcallId, boolean isVueRequest, String uploadKey, int planDepth) { try { // ... PlanExecutorInterface executor = planExecutorFactory.createExecutor(plan); CompletableFuture executionFuture = executor.executeAllStepsAsync(context); return executionFuture.thenCompose(result -> { try { PlanExecutionResult processedResult = planFinalizer.handlePostExecution(context, result); return CompletableFuture.completedFuture(processedResult); ``` - **执行器设计**:`AbstractPlanExecutor` 控制步骤循环、Agent 执行、上传文件同步、步骤结果记录与中断处理,通过 `LevelBasedExecutorPool` 实现分层线程池调度。 ```215:299:src/main/java/com/alibaba/cloud/ai/manus/runtime/executor/AbstractPlanExecutor.java public CompletableFuture executeAllStepsAsync(ExecutionContext context) { int planDepth = context.getPlanDepth(); ExecutorService executor = levelBasedExecutorPool.getExecutorForLevel(planDepth); return CompletableFuture.supplyAsync(() -> { PlanExecutionResult result = new PlanExecutionResult(); PlanInterface plan = context.getPlan(); plan.setCurrentPlanId(context.getCurrentPlanId()); plan.updateStepIndices(); try { syncUploadedFilesToPlan(context); List steps = plan.getAllSteps(); recorder.recordPlanExecutionStart(context.getCurrentPlanId(), context.getPlan().getTitle(), context.getUserRequest(), steps, context.getParentPlanId(), context.getRootPlanId(), ``` - **后置处理**:`PlanFinalizer` 根据上下文决定生成执行摘要、直接响应或中断提示,同时写入执行记录并交互 LLM 对话记忆。 ```180:221:src/main/java/com/alibaba/cloud/ai/manus/planning/service/PlanFinalizer.java public PlanExecutionResult handlePostExecution(ExecutionContext context, PlanExecutionResult result) { if (context == null || result == null) { return result; } try { if (isTaskInterrupted(context, result)) { handleInterruptedTask(context, result); return result; } if (context.isNeedSummary()) { generateSummary(context, result); return result; } else if (context.getPlan() != null && context.getPlan().isDirectResponse()) { generateDirectResponse(context, result); ``` ### 工具体系 - **工具抽象**:所有工具实现 `ToolCallBiFunctionDef` 接口,统一提供名称、描述、输入 Schema 与执行回调,可贴合 Model Function Calling。 - **内置工具族群**: - **浏览器自动化**:`BrowserUseTool` 借助 `ChromeDriverService` 与 Playwright,实现页面抓取与交互。 - **数据库工具**:`DatabaseReadTool`/`DatabaseWriteTool`/`DatabaseMetadataTool` 封装查询与元数据访问,通过 `DataSourceService` 管理连接。 - **文件与存储**:`TextFileService`、`UnifiedDirectoryManager`、OCR 转 Markdown(`MarkdownConverterTool`)等处理文件。 - **Cron 工具**:`CronTool` 将计划注册到 `CronService`,形成计划级定时任务。 - **并行执行**:`ParallelExecutionTool` 触发子计划并发。 - **扩展机制**: - `tool/howToCreateNewTool.md` 指导自定义工具实现。 - MCP 工具通过 `McpService` 动态加载远端能力。 ### 配置与启动 - `config` 包含: - **属性映射**:`ManusProperties`、`CoordinatorProperties`、`MemoryConfig` 等映射 `application.yml`。 - **动态配置管理**:`ConfigController` + `ConfigService` + `ConfigEntity` 支持 REST 修改配置。 - **启动监听**:`AppStartupListener`、`ConfigAppStartupListener` 导入默认配置、迁移数据库。 - **CORS/JSON/Playwright**:`CorsConfig`、`JacksonConfig`、`PlaywrightConfig`。 - 多环境配置:`src/main/resources` 下提供 `application-{profile}.yml`,`Makefile` 与 `deploy/Dockerfile` 支持本地与容器化构建。 ### 事件与调度 - `event` 包定义 `JmanusEvent`、`PlanExceptionEvent`、`JmanusListenerRegister`,在计划异常时被 `ManusController` 缓存并反馈给 API 调用方。 - `cron` 包含计划实体、枚举、调度器与 REST API,实现周期性执行与 Cron 工具的持久化。 - `runtime/service/TaskInterruptionManager` 与数据库表 `RootTaskManagerEntity` 协同,实现执行中断与状态查询。 ### 数据持久化与审计 - 计划执行轨迹存储在 `recorder` 包,`NewRepoPlanExecutionRecorder` 写入执行记录(Plan、Agent、Think-Act 层级)。 - `runtime/entity`/`recorder/entity` 结构化定义执行日志、工具调用、内嵌上下文,支持 UI 上的执行树展示。 - 配置信息由 `ConfigRepository`、工具目录由 `CoordinatorToolRepository` 管理;命名空间、用户等业务实体独立在 `namespace`、`user` 包内。 - 日志体系基于 `logback-spring.xml`,同时支持 `ManusProperties` 中的 debug 细节开关。 ## 前端架构 ### 技术栈与工程化 - **框架组合**:Vue 3(Composition API)+ TypeScript + Vite + Pinia + Vue Router + Ant Design Vue,辅以 `vue3-colorpicker`、`nprogress` 等增强组件。 - **构建与部署**:开发态 `pnpm run dev`,生产态 `pnpm run build` 并将 `ui-vue3/dist` 复制到后端 `static/ui`。`vite.config.ts` 配置静态资源路径与代理(可与后端同域)。 - **国际化与主题**:`base/i18n` 目录整合多语言文案,支持按路由/组件切换;Ant Design 的 `reset.css` 引导统一 UI 基线。 ### 状态管理与交互模式 - `stores` 下多个 Pinia Store 管理全局状态: - `task`:负责任务提交流程、运行状态、停止操作、表单预填。 - `sidebar`、`memory`、`namespace`:分别维护侧边栏折叠、对话记忆、命名空间上下文。 - Store 通过浏览器事件(`window.dispatchEvent`)、`localStorage`(记录引导完成)与 API 调用交织,保证任务跨界面同步。 ```28:192:ui-vue3/src/stores/task.ts export const useTaskStore = defineStore('task', () => { const currentTask = ref(null) const taskToInput = ref('') const hasVisitedHome = ref(false) const setTask = (prompt: string) => { if (!prompt.trim()) { return } const newTask = { prompt, timestamp: Date.now(), processed: false, } currentTask.value = newTask } // ... const stopCurrentTask = async () => { if (currentTask.value && currentTask.value.isRunning && currentTask.value.planId) { const { DirectApiService } = await import('@/api/direct-api-service') await DirectApiService.stopTask(currentTask.value.planId) currentTask.value.isRunning = false return true ``` ### API 协议与数据流 - API 层集中在 `src/api`,以 `fetch` 封装 REST 调用,约定 `/api/executor` 等后端路径,统一处理错误与 JSON 解析。 - `CommonApiService` 负责计划详情轮询、Form 表单提交、Prompt 列表获取,避免异常情况下抛出错误导致轮询中断。 ```22:74:ui-vue3/src/api/common-api-service.ts public static async getDetails(planId: string): Promise { try { const response = await fetch(`${this.BASE_URL}/details/${planId}`) if (response.status === 404) { return null } if (!response.ok) { const errorText = await response.text() throw new Error(`Failed to get detailed information: ${response.status} - ${errorText}`) } const rawText = await response.text() const data = JSON.parse(rawText) if (data && typeof data === 'object' && !data.currentPlanId) { data.currentPlanId = planId } return data } catch (error: unknown) { console.error('[CommonApiService] Failed to get plan details:', error) return null } ``` - 计划执行流程:前端提交任务 → 获取 `planId` → 使用 Pinia Store 记录状态 → 轮询 `/api/executor/details/{planId}` 获取执行树 → 若待用户输入,通过 `/submit-input/{planId}` 填写表单。 - 任务中断:`stopCurrentTask` 调用 `/api/executor/stopTask/{planId}`,并更新 Store 状态。 ## 服务交互与关键流程 ### 计划创建到执行 - **入口**:`ManusController.executeByToolNameAsync` 支持工具名称或模板 ID 调用,自动写入对话记忆、同步上传文件,并触发 `PlanningCoordinator`。 - **计划模板**:`PlanTemplateService` 读取最新版本 JSON,通过 `IPlanParameterMappingService` 进行参数替换(支持 `<>` 占位符)。 - **执行记录**:`PlanExecutionRecorder` 全程记录计划 → 步骤 → Think-Act → 工具调用,并在 API 层对接详情查询。 - **结果生成**:`PlanFinalizer` 决定是否基于 LLM 生成摘要或直接回应;对 Vue 请求默认生成摘要文本,满足 UI 展示。 ### 用户输入与中断 - **表单等待**:当工具请求用户输入时,`UserInputService` 在 `RootTaskManagerEntity` 中标记等待状态,`ManusController` 在详情 API 中合并 `UserInputWaitState` 返回 UI。 - **用户提交**:`/api/executor/submit-input/{planId}` 接收 JSON 表单,写回等待队列并唤醒执行器。 - **中断确认**:`TaskInterruptionManager` 通过数据库状态控制执行线程,在 `AbstractPlanExecutor` 的循环与 `PlanFinalizer` 后置处理中判定并返回中断消息。 ## 扩展性与定制化 ### 工具扩展 - 开发者可参考 `tool/howToCreateNewTool.md`,继承 `ToolCallBiFunctionDef` 并注册到 Spring 容器或 MCP 服务。 - `PlanningFactory` 在启动时根据 `agent.init` 配置决定是否注册全量工具,支持按环境裁剪。 ### MCP 与外部服务 - `mcp` 包对接外部 MCP 服务目录与工具注册,`McpService` 负责缓存、鉴权与动态生成 `McpTool`,让计划执行时透明调用远程能力。 - LLM 集成通过 `LlmService` 适配 Spring AI,支持多厂商模型、流式响应与对话记忆策略。 ### 动态 Agent 与子计划 - `agent` 包含 `DynamicAgent`、`ConfigurableDynaAgent` 与注解 `DynamicAgentDefinition`,可在运行时装配不同 Agent 流程。 - `subplan` 模块允许计划执行过程中动态生成子计划,通过 `SubplanToolService` 注册子计划工具,形成树状执行。 - `PlanExecutorFactory` 可按计划类型(Function 模式、动态工具模式等)选择不同执行器,实现差异化控制策略。 ## 部署与运维 - **构建链路**:`Makefile` 与 `tools/make` 模块封装 Maven、前端构建、Docker 镜像制作、CI 检查(codespell、markdownlint、yamllint)。 - **容器化**:`deploy/Dockerfile` 基于 JDK17,复制后端 Jar 与构建后的前端静态文件,`deploy/start.sh` 提供启动脚本。 - **配置管理**:通过 `/api/config` 对外提供动态配置接口;`ConfigAppStartupListener` 支持启动时加载默认配置模板。 - **日志与调试**:`logback-spring.xml` 控制日志级别;`ManusProperties.debugDetail` 可打开 LLM 调试信息;`PlanExecutionRecorder` 的持久化记录方便线下排错。 - **Playwright/Chrome 驱动**:`OpenManusSpringBootApplication` 启动参数 `playwright-init` 可预下载浏览器内核,避免首次运行延迟。 ## 附录:关键类索引 - **HTTP API**:`runtime/controller/ManusController`、`planning/controller`、`config/ConfigController`、`coordinator/controller/CoordinatorToolController`。 - **执行核心**:`planning/PlanningFactory`、`runtime/service/PlanningCoordinator`、`runtime/executor/*`、`agent/BaseAgent`。 - **工具体系**:`tool/*`、`tool/browse`、`tool/database`、`tool/convertToMarkdown`、`tool/mapreduce`。 - **持久化**:`recorder/service/PlanExecutionRecorder`、`runtime/entity`、`config/entity/ConfigEntity`、`coordinator/entity/po/CoordinatorToolEntity`。 - **前端入口**:`ui-vue3/src/main.ts`、`ui-vue3/src/router`、`ui-vue3/src/stores`、`ui-vue3/src/views`。 - **部署脚本**:`Makefile`、`deploy/Dockerfile`、`tools/make/*.mk`、`tools/scripts/*`。 通过上述架构设计,JManus 形成了“前端编排 → 后端计划执行内核 → 工具与外部服务 → 执行记录与可视化反馈”的闭环。开发者可在保持核心执行链路稳定的前提下,按需扩展工具能力、定制计划模板或替换 LLM 服务,以适配不同业务场景。

讨论回复

4 条回复
✨步子哥 (steper) #1
11-08 16:58
# JManus 架构深度分析:企业级多智能体协作系统的设计思想与实践 ## 引言 JManus 是阿里巴巴基于 Spring AI Alibaba 构建的企业级多智能体协作系统,代表了当前 AI 工程化领域的先进实践。作为 Manus 的 Java 实现版本,JManus 不仅继承了多智能体协作的核心思想,更在架构设计上体现了企业级应用所需的稳定性、可扩展性和可维护性。本文将从架构和设计思想的角度,对 JManus 进行系统性的深度分析。 ## 一、总体架构概览 ### 1.1 架构设计理念 JManus 的架构设计遵循以下核心原则: - **模块化设计**:通过清晰的分层和模块化,确保系统的可维护性和可扩展性 - **插件化扩展**:支持动态工具注册和 MCP 协议,实现功能的灵活扩展 - **企业级可靠**:提供完整的监控、记录、错误处理和容错机制 - **云原生架构**:支持容器化部署和弹性伸缩 - **多智能体协作**:实现复杂的智能体间协作和任务编排 ### 1.2 技术栈架构 ```mermaid graph TB subgraph "前端层" UI[Vue.js 前端界面] API[RESTful API] end subgraph "应用服务层" Controller[控制器层] Service[业务服务层] Agent[智能体层] Tool[工具层] end subgraph "数据持久层" JPA[JPA/Hibernate] DB[(H2/MySQL/PostgreSQL)] Cache[内存缓存] end subgraph "AI 能力层" LLM[大语言模型服务] MCP[MCP 协议集成] Memory[对话记忆管理] end subgraph "基础设施层" SpringBoot[Spring Boot 3.x] Reactor[WebFlux 响应式] Docker[容器化] end UI --> API API --> Controller Controller --> Service Service --> Agent Agent --> Tool Service --> JPA JPA --> DB Service --> Cache Agent --> LLM Tool --> MCP LLM --> Memory Controller --> SpringBoot SpringBoot --> Reactor SpringBoot --> Docker ``` ## 二、多智能体系统架构 ### 2.1 智能体层次结构 JManus 采用分层智能体架构,核心抽象为 [`BaseAgent`](src/main/java/com/alibaba/cloud/ai/manus/agent/BaseAgent.java:74): ```mermaid classDiagram class BaseAgent { -currentPlanId: String -rootPlanId: String -planDepth: int -maxSteps: int -currentStep: int -llmService: LlmService -planExecutionRecorder: PlanExecutionRecorder +run(): AgentExecResult +step(): AgentExecResult +getThinkMessage(): Message +getNextStepWithEnvMessage(): Message } class ReActAgent { +think(): boolean +act(): AgentExecResult } class ConfigurableDynaAgent { -configuration: Map +execute(): void } class ToolCallAgent { -toolCallbacks: List~ToolCallback~ +selectTools(): List~ToolCallback~ +executeTools(): ToolExecuteResult } BaseAgent <|-- ReActAgent ReActAgent <|-- ConfigurableDynaAgent ConfigurableDynaAgent <|-- ToolCallAgent ``` ### 2.2 ReAct 模式实现 JManus 实现了经典的 ReAct(Reasoning + Acting)模式,通过 [`ReActAgent`](src/main/java/com/alibaba/cloud/ai/manus/agent/ReActAgent.java:30) 提供思考-行动交替执行的框架: ```java @Override public AgentExecResult step() { try { boolean shouldAct = think(); // 思考阶段 if (!shouldAct) { return new AgentExecResult("Thinking complete - no action needed", AgentState.IN_PROGRESS); } return act(); // 行动阶段 } catch (TaskInterruptedException e) { return new AgentExecResult("Agent execution interrupted: " + e.getMessage(), AgentState.INTERRUPTED); } } ``` ### 2.3 智能体状态管理 智能体状态通过 [`AgentState`](src/main/java/com/alibaba/cloud/ai/manus/agent/BaseAgent.java:462) 枚举管理,支持以下状态: - **IDLE**:初始状态 - **RUNNING**:执行中 - **COMPLETED**:成功完成 - **INTERRUPTED**:被中断 - **ERROR**:执行错误 ## 三、工具系统架构 ### 3.1 工具抽象设计 工具系统基于 [`AbstractBaseTool`](src/main/java/com/alibaba/cloud/ai/manus/tool/AbstractBaseTool.java:28) 抽象类,提供统一的工具接口: ```mermaid classDiagram class AbstractBaseTool { -currentPlanId: String -rootPlanId: String +run(I input): ToolExecuteResult +apply(I input, ToolContext): ToolExecuteResult +isSelectable(): boolean +isReturnDirect(): boolean } class BrowserUseTool { -chromeDriverService: ChromeDriverService -innerStorageService: SmartContentSavingService +navigate(): void +click(): void +inputText(): void } class DatabaseReadTool { -dataSourceService: DataSourceService +executeQuery(): List~Map~ +getTableMetadata(): TableMeta } class Bash { -unifiedDirectoryManager: UnifiedDirectoryManager +executeCommand(): CommandResult } class TerminateTool { +terminate(): void } AbstractBaseTool <|-- BrowserUseTool AbstractBaseTool <|-- DatabaseReadTool AbstractBaseTool <|-- Bash AbstractBaseTool <|-- TerminateTool ``` ### 3.2 工具生命周期管理 工具系统实现了完整的生命周期管理,确保资源的正确分配和释放: 1. **初始化阶段**:工具在 [`PlanningFactory`](src/main/java/com/alibaba/cloud/ai/manus/planning/PlanningFactory.java:207) 中注册 2. **执行阶段**:工具在智能体执行过程中被调用 3. **清理阶段**:通过 [`clearUp()`](src/main/java/com/alibaba/cloud/ai/manus/agent/BaseAgent.java:105) 方法释放资源 ### 3.3 工具扩展机制 JManus 提供了多种工具扩展方式: - **内置工具**:浏览器操作、数据库访问、文件系统操作等 - **MCP 工具**:通过 Model Context Protocol 集成外部工具 - **动态工具**:支持运行时动态注册和卸载工具 ## 四、规划与执行框架 ### 4.1 计划执行模型 计划执行通过 [`AbstractPlanExecutor`](src/main/java/com/alibaba/cloud/ai/manus/runtime/executor/AbstractPlanExecutor.java:45) 实现,采用异步执行模式: ```mermaid sequenceDiagram participant Client participant Controller participant Executor participant Agent participant Tool Client->>Controller: 提交执行请求 Controller->>Executor: 创建执行上下文 Executor->>Executor: 异步执行计划 loop 每个步骤 Executor->>Agent: 创建智能体 Agent->>Agent: 执行思考 Agent->>Tool: 调用工具 Tool-->>Agent: 返回结果 Agent-->>Executor: 返回执行结果 end Executor-->>Client: 返回最终结果 ``` ### 4.2 层级执行池 为了支持复杂的嵌套计划执行,JManus 实现了 [`LevelBasedExecutorPool`](src/main/java/com/alibaba/cloud/ai/manus/runtime/executor/LevelBasedExecutorPool.java): - **深度分级**:根据计划的嵌套深度分配不同的执行线程池 - **资源隔离**:避免深层计划阻塞浅层计划的执行 - **性能优化**:提高整体执行效率和响应性 ### 4.3 执行状态管理 执行状态通过 [`ExecutionContext`](src/main/java/com/alibaba/cloud/ai/manus/runtime/entity/vo/ExecutionContext.java) 管理,包含: - 计划信息(PlanInterface) - 执行参数和环境数据 - 文件上传信息 - 父子计划关系 ## 五、MCP(Model Context Protocol)集成 ### 5.1 MCP 架构设计 JManus 原生支持 MCP 协议,通过 [`McpService`](src/main/java/com/alibaba/cloud/ai/manus/mcp/service/McpService.java:44) 提供服务: ```mermaid graph LR subgraph "JManus Core" MS[McpService] CM[CacheManager] CV[ConfigValidator] end subgraph "MCP Clients" MC1[MCP Client 1] MC2[MCP Client 2] MC3[MCP Client 3] end subgraph "External Services" ES1[外部服务A] ES2[外部服务B] ES3[外部服务C] end MS --> CM MS --> CV MS --> MC1 MS --> MC2 MS --> MC3 MC1 --> ES1 MC2 --> ES2 MC3 --> ES3 ``` ### 5.2 MCP 配置管理 MCP 配置通过 [`McpConfigEntity`](src/main/java/com/alibaba/cloud/ai/manus/mcp/model/po/McpConfigEntity.java) 持久化,支持: - **多种连接类型**:stdio、http、websocket - **动态配置**:运行时修改配置并热加载 - **状态管理**:启用/禁用服务状态控制 - **缓存机制**:提高服务访问性能 ### 5.3 MCP 工具集成 MCP 工具通过 [`McpTool`](src/main/java/com/alibaba/cloud/ai/manus/mcp/model/vo/McpTool.java) 包装器集成到 JManus 工具系统中,实现与内置工具的无缝协作。 ## 六、运行时与执行编排 ### 6.1 运行时架构 运行时系统通过 [`ManusController`](src/main/java/com/alibaba/cloud/ai/manus/runtime/controller/ManusController.java:77) 提供统一的执行入口: ```mermaid graph TB subgraph "API 层" GET[/executeByToolNameSync/GET] POST[/executeByToolNameSync/POST] POST[/executeByToolNameAsync/POST] end subgraph "业务逻辑层" PC[计划模板服务] PMS[参数映射服务] FUS[文件上传服务] TIS[任务中断服务] end subgraph "执行层" PCO[PlanningCoordinator] APE[AbstractPlanExecutor] BA[BaseAgent] end GET --> PC POST --> PC PC --> PMS PC --> FUS PC --> TIS PC --> PCO PCO --> APE APE --> BA ``` ### 6.2 异步执行模型 JManus 采用 CompletableFuture 实现异步执行,提供: - **非阻塞执行**:客户端提交后立即返回任务ID - **状态查询**:通过任务ID查询执行状态 - **结果获取**:支持轮询和回调两种方式获取结果 - **错误处理**:完整的异常捕获和错误报告机制 ### 6.3 任务中断机制 通过 [`TaskInterruptionManager`](src/main/java/com/alibaba/cloud/ai/manus/runtime/service/TaskInterruptionManager.java) 实现任务中断: - **数据库驱动**:使用数据库状态实现可靠的中断机制 - **多级检查**:在智能体、步骤、计划多个层级检查中断状态 - **优雅终止**:确保资源正确清理和状态一致性 ## 七、配置与属性管理 ### 7.1 动态配置系统 配置系统基于 [`ManusProperties`](src/main/java/com/alibaba/cloud/ai/manus/config/ManusProperties.java:28) 实现,支持运行时动态修改: ```java @ConfigProperty( group = "manus", subGroup = "agent", key = "maxSteps", path = "manus.maxSteps", description = "manus.agent.maxSteps.description", defaultValue = "200", inputType = ConfigInputType.NUMBER ) private volatile Integer maxSteps; ``` ### 7.2 配置分类管理 配置按功能模块分类管理: - **智能体配置**:最大步数、内存限制、并行工具调用等 - **浏览器配置**:无头模式、请求超时等 - **MCP 配置**:连接超时、重试次数、并发连接数等 - **文件系统配置**:外部访问权限、上传限制等 - **图像识别配置**:模型名称、DPI、重试次数等 ### 7.3 配置持久化 配置通过 [`ConfigService`](src/main/java/com/alibaba/cloud/ai/manus/config/ConfigService.java) 持久化到数据库,支持: - **多环境支持**:开发、测试、生产环境配置隔离 - **版本管理**:配置变更历史记录 - **热加载**:运行时配置修改立即生效 ## 八、记录与监控能力 ### 8.1 执行记录系统 执行记录通过 [`PlanExecutionRecorder`](src/main/java/com/alibaba/cloud/ai/manus/recorder/service/PlanExecutionRecorder.java:26) 接口实现,提供完整的执行轨迹: ```mermaid graph TD subgraph "记录层级" PE[计划执行记录
PlanExecutionRecord] AE[智能体执行记录
AgentExecutionRecord] TA[思考行动记录
ThinkActRecord] AT[工具调用记录
ActToolInfo] end PE -->|包含多个| AE AE -->|包含多个| TA TA -->|包含多个| AT ``` ### 8.2 多级记录机制 记录系统实现多级详细记录: 1. **计划级别**:记录计划的整体执行状态、开始结束时间、最终结果 2. **智能体级别**:记录每个智能体的执行过程、步数、状态变化 3. **思考-行动级别**:记录智能体的每次思考和行动过程 4. **工具调用级别**:记录每个工具调用的参数、结果、错误信息 ### 8.3 性能监控 集成 Micrometer 观测框架,提供: - **执行时间统计**:各层级执行时间监控 - **资源使用监控**:内存、CPU、数据库连接等资源使用 - **错误率统计**:各类型错误发生频率 - **吞吐量监控**:单位时间处理能力 ## 九、前端与 UI 架构 ### 9.1 前后端分离架构 前端采用 Vue.js 3 构建,通过 RESTful API 与后端交互: - **现代化 UI**:基于 Vue 3 的响应式界面 - **实时通信**:WebSocket 支持实时状态更新 - **文件上传**:支持大文件分片上传和进度显示 - **国际化支持**:中英文双语界面 ### 9.2 用户体验设计 前端设计注重用户体验: - **引导式配置**:新用户配置向导 - **实时反馈**:执行状态实时更新 - **错误友好**:清晰的错误提示和解决方案 - **响应式布局**:适配不同屏幕尺寸 ## 十、数据库与持久化层 ### 10.1 多数据库支持 JManus 支持多种数据库: - **H2**:默认嵌入式数据库,适合开发和测试 - **MySQL**:生产环境推荐,支持高并发 - **PostgreSQL**:企业级特性支持 ### 10.2 数据模型设计 核心实体包括: - **计划执行记录**:存储计划执行的完整轨迹 - **智能体配置**:动态智能体的配置信息 - **MCP 配置**:外部工具服务配置 - **对话记忆**:用户对话历史记录 - **系统配置**:运行时配置参数 ### 10.3 数据访问层 使用 Spring Data JPA 实现数据访问: - **Repository 模式**:统一的数据访问接口 - **查询优化**:关键查询添加索引优化 - **事务管理**:确保数据一致性 - **连接池管理**:HikariCP 提供高性能连接池 ## 十一、安全与访问控制 ### 11.1 文件系统安全 通过 [`ManusProperties.getAllowExternalAccess()`](src/main/java/com/alibaba/cloud/ai/manus/config/ManusProperties.java:324) 控制文件访问: - **沙箱机制**:默认限制在工作目录内操作 - **权限控制**:可配置是否允许外部文件访问 - **路径验证**:防止路径穿越攻击 ### 11.2 工具执行安全 工具执行采用多层安全机制: - **参数验证**:所有工具参数经过严格验证 - **超时控制**:防止工具执行无限等待 - **资源限制**:限制内存、CPU 使用 - **错误隔离**:工具错误不会影响系统稳定性 ### 11.3 网络安全 - **API 安全**:RESTful API 支持认证和授权 - **数据加密**:敏感数据加密存储 - **传输安全**:支持 HTTPS 加密传输 ## 十二、部署与容器化 ### 12.1 Docker 容器化 JManus 提供完整的 Docker 支持,通过 [`Dockerfile`](deploy/Dockerfile:15) 实现: ```dockerfile # 多阶段构建优化 FROM eclipse-temurin:17-jdk-noble # 系统依赖安装 RUN apt-get update && apt-get install -y \ ca-certificates curl gnupg \ xvfb x11vnc fluxbox \ fonts-liberation fonts-dejavu-core fonts-noto-cjk \ nodejs npm playwright # 应用部署 COPY target/jmanus.jar app.jar COPY deploy/start.sh /app/start.sh # 环境配置 ENV DISPLAY=:99 \ PLAYWRIGHT_BROWSERS_PATH=/root/.cache/ms-playwright \ JAVA_OPTS="-Xmx2g -Xms1g -XX:+UseG1GC -XX:+UseContainerSupport" EXPOSE 18080 ENTRYPOINT ["/app/start.sh"] ``` ### 12.2 云原生特性 - **健康检查**:提供 `/actuator/health` 健康检查端点 - **指标暴露**:通过 `/actuator/metrics` 暴露运行指标 - **配置外部化**:支持环境变量和配置中心 - **日志聚合**:结构化日志输出,便于日志收集 ### 12.3 弹性伸缩 - **水平扩展**:无状态设计支持多实例部署 - **负载均衡**:支持通过负载均衡器分发请求 - **资源监控**:集成 Prometheus 指标暴露 ## 十三、架构优势与创新点 ### 13.1 架构优势 1. **企业级可靠性**: - 完整的错误处理和容错机制 - 多级记录和监控系统 - 数据库驱动的任务状态管理 2. **高度可扩展性**: - 插件化工具系统 - MCP 协议支持 - 动态配置管理 3. **性能优化**: - 异步执行模型 - 层级执行池设计 - 智能缓存机制 4. **开发友好性**: - 清晰的架构分层 - 完善的文档和示例 - 便捷的工具开发框架 ### 13.2 技术创新点 1. **Func-Agent 模式**:提供极高执行确定性的功能智能体模式 2. **层级执行池**:根据计划深度分配不同执行资源 3. **数据库驱动中断**:可靠的任务中断机制 4. **多智能体协作**:复杂的智能体间协作和状态共享 5. **MCP 原生集成**:无缝集成外部工具和服务 ## 十四、应用场景与最佳实践 ### 14.1 典型应用场景 1. **数据处理自动化**: - 大规模数据清洗和转换 - 复杂的数据分析流程 - 报告生成和分发 2. **Web 自动化测试**: - 端到端测试流程 - 多步骤业务场景验证 - 可视化测试报告 3. **业务流程自动化**: - 复杂的业务流程编排 - 多系统数据同步 - 定时任务执行 4. **智能客服系统**: - 多轮对话管理 - 工具调用和查询 - 个性化响应生成 ### 14.2 最佳实践建议 1. **智能体设计**: - 保持智能体职责单一 - 合理设置最大执行步数 - 实现适当的错误处理 2. **工具开发**: - 遵循工具生命周期管理 - 提供清晰的工具描述 - 实现参数验证和错误处理 3. **性能优化**: - 合理使用异步执行 - 配置适当的执行池大小 - 启用必要的缓存机制 4. **监控运维**: - 配置完整的记录级别 - 设置合理的日志策略 - 建立性能监控告警 ## 十五、未来发展方向 ### 15.1 技术演进方向 1. **AI 能力增强**: - 支持更多大语言模型 - 集成多模态 AI 能力 - 实现更智能的任务规划 2. **分布式架构**: - 支持分布式智能体部署 - 实现跨节点的任务协调 - 提供高可用和容灾能力 3. **实时协作**: - 支持多人协作编辑 - 实现实时状态同步 - 提供协作权限管理 ### 15.2 生态建设 1. **工具生态**: - 建设工具市场 - 提供工具开发 SDK - 建立工具认证机制 2. **社区建设**: - 开源社区运营 - 开发者培训认证 - 最佳实践分享 3. **标准化推进**: - 参与行业标准制定 - 推动 MCP 协议发展 - 建立互操作性标准 ## 结语 JManus 作为企业级多智能体协作系统的优秀实践,其架构设计体现了现代 AI 工程化的先进理念。通过模块化的架构设计、插件化的扩展机制、企业级的可靠性保障,JManus 为复杂 AI 应用的开发和部署提供了完整的解决方案。 其创新的 Func-Agent 模式、层级执行池设计、数据库驱动中断机制等技术创新,不仅解决了多智能体协作中的关键技术挑战,更为整个行业的发展提供了宝贵的经验和参考。随着 AI 技术的不断发展和应用场景的不断扩展,JManus 必将在企业数字化转型的进程中发挥更加重要的作用。 通过深入分析 JManus 的架构设计,我们可以看到未来 AI 系统的发展方向:更加智能化、更加可靠、更加易用、更加开放。JManus 不仅是一个技术产品,更是 AI 工程化方法论的具体实践,为推动 AI 技术在企业级应用中的落地提供了重要的技术支撑和最佳实践指导。
✨步子哥 (steper) #2
11-08 17:13
# JManus UI-Vue3 前端架构深度分析:现代化企业级 Vue3 应用的设计思想与实践 ## 引言 JManus UI-Vue3 是 JManus 多智能体协作系统的前端实现,采用 Vue 3 + TypeScript 技术栈构建。作为一个企业级 AI 应用的前端,它不仅需要处理复杂的实时数据流和多步骤执行流程,还要提供优秀的用户体验和可维护的代码架构。本文将从架构和设计思想的角度,对 UI-Vue3 进行系统性的深度分析。 ## 一、总体架构概览 ### 1.1 技术栈架构 ```mermaid graph TB subgraph "核心框架" VUE[Vue 3.3+] TS[TypeScript] VITE[Vite 5+] end subgraph "状态管理" PINIA[Pinia] REACTIVE[Vue Reactive] end subgraph "UI 组件库" ANTD[Ant Design Vue] ICONIFY[Iconify] MONACO[Monaco Editor] end subgraph "工具库" AXIOS[Axios] DAYJS[Dayjs] LODASH[Lodash] MARKED[Marked] end subgraph "构建工具" ESLINT[ESLint 9+] PRETTIER[Prettier] VITEST[Vitest] CYPRESS[Cypress] end VUE --> PINIA VUE --> ANTD TS --> VITE PINIA --> REACTIVE ANTD --> ICONIFY AXIOS --> DAYJS VITE --> ESLINT ESLINT --> PRETTIER VITEST --> CYPRESS ``` ### 1.2 架构设计理念 UI-Vue3 的架构设计遵循以下核心原则: - **组件化架构**:高度模块化的组件设计,支持复用和独立测试 - **响应式状态管理**:基于 Pinia 和 Vue 3 Composition API 的状态管理 - **类型安全**:全面的 TypeScript 类型定义和接口设计 - **实时交互**:WebSocket 和长轮询结合的实时数据更新 - **国际化支持**:完整的 i18n 国际化架构 - **可访问性**:良好的用户体验和无障碍设计 ## 二、项目结构与模块划分 ### 2.1 目录结构分析 ``` ui-vue3/ ├── src/ │ ├── api/ # API 服务层 │ ├── base/ # 基础配置(i18n等) │ ├── components/ # 通用组件 │ │ ├── chat/ # 聊天组件 │ │ ├── editor/ # 编辑器组件 │ │ ├── file-browser/ # 文件浏览器 │ │ └── ... │ ├── composables/ # 组合式函数 │ ├── router/ # 路由配置 │ ├── stores/ # 状态管理 │ ├── types/ # TypeScript 类型定义 │ ├── utils/ # 工具函数 │ └── views/ # 页面视图 ├── public/ # 静态资源 ├── cypress/ # E2E 测试 └── ... ``` ### 2.2 模块化架构设计 ```mermaid graph LR subgraph "视图层 Views" HOME[HomeView] DIRECT[DirectView] CONFIG[ConfigView] end subgraph "组件层 Components" CHAT[ChatComponents] EDITOR[EditorComponents] FILE[FileComponents] end subgraph "状态层 Stores" TASK[TaskStore] MEMORY[MemoryStore] SIDEBAR[SidebarStore] end subgraph "API层" COMMON[CommonApiService] DIRECT_API[DirectApiService] PLAN_API[PlanActApiService] end subgraph "工具层 Utils" PLAN_MGR[PlanExecutionManager] REQUEST[RequestUtils] I18N[I18nUtils] end HOME --> CHAT DIRECT --> CHAT CONFIG --> EDITOR CHAT --> TASK CHAT --> MEMORY EDITOR --> SIDEBAR TASK --> DIRECT_API MEMORY --> COMMON SIDEBAR --> PLAN_API DIRECT_API --> PLAN_MGR PLAN_MGR --> REQUEST ``` ## 三、核心架构组件分析 ### 3.1 路由架构设计 路由系统采用 Vue Router 4 的 Hash 模式,通过 [`createWebHashHistory('/ui')`](ui-vue3/src/router/index.ts:21) 实现: ```typescript const router = createRouter({ history: createWebHashHistory('/ui'), routes, }) ``` 路由守卫实现了系统初始化检查: ```typescript router.beforeEach(async (to, _from, next) => { // 跳过初始化检查对于初始化页面本身 if (to.path === '/init') { next() return } try { // 从服务器检查初始化状态 const response = await fetch('/api/init/status') const result = await response.json() if (result.success && !result.initialized) { // 系统未初始化,重定向到初始化页面 localStorage.removeItem('hasInitialized') next('/init') return } } catch (error) { console.warn('Failed to check initialization status:', error) } next() }) ``` ### 3.2 状态管理架构 采用 Pinia 作为状态管理库,设计了多个专门的 Store: #### 3.2.1 任务状态管理 ([`TaskStore`](ui-vue3/src/stores/task.ts:28)) ```typescript export const useTaskStore = defineStore('task', () => { const currentTask = ref(null) const taskToInput = ref('') const hasVisitedHome = ref(false) // 任务状态管理方法 const setTask = (prompt: string) => { /* ... */ } const markTaskAsProcessed = () => { /* ... */ } const setTaskRunning = (planId: string) => { /* ... */ } const stopCurrentTask = async () => { /* ... */ } return { currentTask, taskToInput, hasVisitedHome, setTask, setTaskRunning, stopCurrentTask, // ... } }) ``` #### 3.2.2 侧边栏状态管理 ([`SidebarStore`](ui-vue3/src/stores/sidebar.ts:26)) 实现了复杂的模板管理功能: ```typescript export class SidebarStore { // 基础状态 isCollapsed = false currentTab: TabType = 'list' // 模板列表相关状态 currentPlanTemplateId: string | null = null planTemplateList: PlanTemplate[] = [] selectedTemplate: PlanTemplate | null = null // 配置相关状态 jsonContent = '' planType = 'dynamic_agent' generatorPrompt = '' executionParams = '' // 计算属性 get sortedTemplates(): PlanTemplate[] { /* ... */ } get groupedTemplates(): Map { /* ... */ } get canRollback(): boolean { /* ... */ } // 操作方法 async loadPlanTemplateList() { /* ... */ } async selectTemplate(template: PlanTemplate) { /* ... */ } async saveTemplate() { /* ... */ } } ``` #### 3.2.3 内存状态管理 ([`MemoryStore`](ui-vue3/src/stores/memory.ts:29)) ```typescript export class MemoryStore { isCollapsed = false selectMemoryId = '' loadMessages = () => {} intervalId: number | undefined = undefined toggleSidebar() { this.isCollapsed = !this.isCollapsed if (this.isCollapsed) { this.loadMessages() this.intervalId = window.setInterval(() => { this.loadMessages() }, 3000) } else { clearInterval(this.intervalId) } } } ``` ### 3.3 组合式函数架构 (Composables) Vue 3 Composition API 的最佳实践,实现了多个可复用的组合式函数: #### 3.3.1 请求处理组合式 ([`useRequest`](ui-vue3/src/composables/useRequest.ts:4)) ```typescript export function useRequest() { const loading = ref(false) const executeRequest = async ( requestFn: () => Promise>, successMessage?: string, errorMessage?: string ): Promise | null> => { try { loading.value = true const result = await requestFn() if (result.success && successMessage) { console.log(successMessage) } else if (!result.success && errorMessage) { console.error(errorMessage) } return result } catch (error) { console.error('Request execution failed:', error) return null } finally { loading.value = false } } return { loading, executeRequest, } } ``` #### 3.3.2 聊天消息组合式 ([`useChatMessages`](ui-vue3/src/components/chat/composables/useChatMessages.ts:117)) ```typescript export function useChatMessages() { // 状态 const messages = ref([]) const isLoading = ref(false) const streamingMessageId = ref(null) const activeMessageId = ref(null) // 计算属性 const lastMessage = computed(() => { return messages.value.length > 0 ? messages.value[messages.value.length - 1] : null }) const isStreaming = computed(() => { return streamingMessageId.value !== null }) // 方法 const addMessage = (type: 'user' | 'assistant', content: string, options?: Partial) => { const message: ChatMessage = { id: `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`, type, content, timestamp: new Date(), isStreaming: false, ...options, } messages.value.push(message) return message } const updateMessage = (id: string, updates: Partial) => { const index = messages.value.findIndex(m => m.id === id) if (index !== -1) { messages.value[index] = { ...messages.value[index], ...updates } } } return { messages: readonly(messages), isLoading, streamingMessageId: readonly(streamingMessageId), lastMessage, isStreaming, addMessage, updateMessage, // ... } } ``` ## 四、核心功能模块分析 ### 4.1 计划执行管理器 ([`PlanExecutionManager`](ui-vue3/src/utils/plan-execution-manager.ts:42)) 这是整个前端应用的核心组件,负责管理复杂的计划执行流程: ```typescript export class PlanExecutionManager { private static instance: PlanExecutionManager | null = null private readonly POLL_INTERVAL = 5000 // 响应式状态 private state = reactive({ activePlanId: null, lastSequenceSize: 0, isPolling: false, pollTimer: null, }) // 事件回调 private callbacks: EventCallbacks = {} // 缓存系统 private planExecutionCache = new Map() private uiStateCache = new Map() // 核心方法 public async handleUserMessageSendRequested(query: string): Promise public handlePlanExecutionRequested(planId: string, query?: string): void public initiatePlanExecutionSequence(query: string, planId: string): void private async pollPlanStatus(): Promise } ``` #### 4.1.1 执行流程管理 执行管理器实现了完整的执行生命周期管理: 1. **请求验证**:检查输入有效性和系统状态 2. **消息发送**:通过 API 发送用户消息到后端 3. **计划初始化**:获取计划ID并启动执行序列 4. **轮询监控**:定期轮询计划执行状态 5. **结果处理**:处理执行结果和错误状态 6. **资源清理**:执行完成后的资源清理 #### 4.1.2 缓存系统设计 实现了双层缓存机制: - **计划执行缓存**:存储计划执行记录,支持快速查询 - **UI状态缓存**:存储用户界面状态,支持状态恢复 ```typescript /** * 获取缓存的计划执行记录 */ getCachedPlanRecord(rootPlanId: string): PlanExecutionRecord | undefined { return this.planExecutionCache.get(rootPlanId) } /** * 设置缓存的计划执行记录 */ setCachedPlanRecord(rootPlanId: string, record: PlanExecutionRecord): void { this.planExecutionCache.set(rootPlanId, record) console.log(`[PlanExecutionManager] Cached plan execution record for rootPlanId: ${rootPlanId}`) } ``` ### 4.2 聊天界面架构 聊天界面采用组件化设计,核心组件包括: #### 4.2.1 聊天容器 ([`ChatContainer`](ui-vue3/src/components/chat/ChatContainer.vue:16)) ```vue ``` #### 4.2.2 消息组件架构 消息系统支持多种消息类型: - **用户消息**:用户输入的文本消息 - **助手消息**:AI 助手的响应消息 - **执行消息**:计划执行的状态消息 - **错误消息**:执行过程中的错误信息 ### 4.3 侧边栏架构设计 侧边栏实现了复杂的模板管理功能: #### 4.3.1 模板组织系统 支持多种组织方式: ```typescript // 组织方式:'by_time' | 'by_abc' | 'by_group_time' | 'by_group_abc' organizationMethod: 'by_time' | 'by_abc' | 'by_group_time' | 'by_group_abc' = 'by_time' get sortedTemplates(): PlanTemplate[] { const templates = [...this.planTemplateList] switch (this.organizationMethod) { case 'by_time': return templates.sort((a, b) => { const timeA = this.parseDateTime(a.updateTime ?? a.createTime) const timeB = this.parseDateTime(b.updateTime ?? b.createTime) return timeB.getTime() - timeA.getTime() }) case 'by_abc': return templates.sort((a, b) => { const titleA = (a.title ?? '').toLowerCase() const titleB = (b.title ?? '').toLowerCase() return titleA.localeCompare(titleB) }) case 'by_group_time': case 'by_group_abc': { // 分组逻辑处理 const groups = new Map() const ungrouped: PlanTemplate[] = [] templates.forEach(template => { const serviceGroup = this.templateServiceGroups.get(template.id) ?? '' if (!serviceGroup || serviceGroup === 'default' || serviceGroup === '') { ungrouped.push(template) } else { if (!groups.has(serviceGroup)) { groups.set(serviceGroup, []) } groups.get(serviceGroup)!.push(template) } }) // 返回排序后的结果 // ... } } } ``` #### 4.3.2 版本控制系统 实现了完整的版本管理功能: - **版本历史**:保存模板的多个版本 - **版本回滚**:支持回退到之前的版本 - **版本比较**:比较不同版本的差异 ## 五、国际化架构设计 ### 5.1 国际化系统架构 采用 Vue I18n 9 实现完整的国际化支持: ```typescript export const i18n = createI18n({ legacy: false, locale: localeConfig.locale, fallbackLocale: 'en', messages: { en: en, zh: zh, }, }) ``` ### 5.2 动态语言切换 实现了运行时语言切换功能: ```typescript export const changeLanguage = async (locale: string) => { localStorage.setItem(LOCAL_STORAGE_LOCALE, locale) i18n.global.locale.value = locale as 'zh' | 'en' localeConfig.locale = locale console.log(`Successfully switched frontend language to: ${locale}`) } /** * 初始化期间更改语言并重置所有智能体和提示 */ export const changeLanguageWithAgentReset = async (locale: string) => { // 首先更改前端语言 await changeLanguage(locale) try { // 重置提示为新语言 const promptResponse = await fetch(`/admin/prompts/switch-language?language=${locale}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, }) if (promptResponse.ok) { console.log(`Successfully reset prompts to language: ${locale}`) } // 用新语言初始化智能体 const agentResponse = await fetch('/api/agent-management/initialize', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ language: locale }), }) if (agentResponse.ok) { const result = await agentResponse.json() console.log(`Successfully initialized agents with language: ${locale}`, result) } } catch (error) { console.error('Error initializing agents and prompts during language change:', error) throw error } } ``` ## 六、API 架构设计 ### 6.1 API 服务层架构 采用面向对象的 API 服务设计,每个功能模块都有对应的 API 服务类: ```typescript // 通用 API 服务 export class CommonApiService { private static readonly BASE_URL = '/api/executor' // 获取详细执行记录 public static async getDetails(planId: string): Promise // 提交用户表单输入 public static async submitFormInput(planId: string, formData: Record): Promise> // 获取所有提示列表 static async getAllPrompts(): Promise } ``` ### 6.2 专门的 API 服务 为不同功能模块提供专门的 API 服务: - **DirectApiService**:直接执行模式的 API 服务 - **PlanActApiService**:计划模板相关的 API 服务 - **ToolApiService**:工具管理相关的 API 服务 - **McpApiService**:MCP 配置相关的 API 服务 - **ConfigApiService**:系统配置相关的 API 服务 ### 6.3 请求处理机制 实现了统一的请求处理机制: ```typescript export function useRequest() { const loading = ref(false) const executeRequest = async ( requestFn: () => Promise>, successMessage?: string, errorMessage?: string ): Promise | null> => { try { loading.value = true const result = await requestFn() // 统一的成功/错误处理 if (result.success && successMessage) { console.log(successMessage) } else if (!result.success && errorMessage) { console.error(errorMessage) } return result } catch (error) { console.error('Request execution failed:', error) return null } finally { loading.value = false } } return { loading, executeRequest } } ``` ## 七、UI/UX 设计架构 ### 7.1 设计语言系统 采用现代化的设计语言: - **深色主题**:主色调为深色系 (#0a0a0a) - **渐变效果**:使用蓝紫色渐变作为主色调 - **毛玻璃效果**:backdrop-filter 实现现代化视觉效果 - **动画过渡**:平滑的过渡动画提升用户体验 ### 7.2 响应式设计 实现了完整的响应式设计: ```css @media (max-width: 768px) { .chat-container { .messages { padding: 16px; } .scroll-to-bottom { bottom: 20px; right: 20px; width: 36px; height: 36px; svg { font-size: 18px; } } } } ``` ### 7.3 可访问性设计 - **键盘导航**:完整的键盘操作支持 - **屏幕阅读器**:语义化的 HTML 结构 - **高对比度**:确保文本可读性 - **焦点管理**:清晰的焦点指示器 ## 八、性能优化架构 ### 8.1 构建优化 Vite 配置优化: ```typescript export default defineConfig({ base: '/ui', build: { outDir: './ui', sourcemap: true, // 启用 source maps }, css: { devSourcemap: true, // 启用 CSS source maps }, server: { open: true, host: true, proxy: { '/api': { target: 'http://localhost:18080', changeOrigin: true, }, }, }, }) ``` ### 8.2 运行时优化 - **组件懒加载**:路由组件按需加载 - **状态缓存**:智能的状态缓存机制 - **虚拟滚动**:大量数据时的虚拟滚动 - **防抖节流**:用户输入的防抖处理 ### 8.3 内存管理 - **组件卸载清理**:及时清理定时器和事件监听 - **缓存大小控制**:限制缓存数据的大小 - **垃圾回收优化**:避免内存泄漏 ## 九、错误处理与监控架构 ### 9.1 错误处理机制 实现了多层次的错误处理: ```typescript try { const response = await fetch(`${this.BASE_URL}/details/${planId}`) if (response.status === 404) { return null } if (!response.ok) { const errorText = await response.text() throw new Error(`Failed to get detailed information: ${response.status} - ${errorText}`) } const rawText = await response.text() const data = JSON.parse(rawText) return data } catch (error: unknown) { console.error('[CommonApiService] Failed to get plan details:', error) return null } ``` ### 9.2 日志系统 实现了结构化的日志系统: - **错误日志**:记录所有错误信息 - **调试日志**:开发阶段的调试信息 - **性能日志**:关键操作的性能数据 - **用户行为日志**:用户操作轨迹记录 ### 9.3 监控指标 - **性能监控**:页面加载时间、API 响应时间 - **错误监控**:JavaScript 错误、API 错误 - **用户行为监控**:页面访问、功能使用频率 ## 十、测试架构设计 ### 10.1 单元测试 使用 Vitest 进行单元测试: ```json { "scripts": { "test:unit": "vitest", "test:e2e": "start-server-and-test preview http://localhost:4173 'cypress run --e2e'" } } ``` ### 10.2 E2E 测试 使用 Cypress 进行端到端测试: ```typescript // cypress.config.ts export default defineConfig({ e2e: { specPattern: 'cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}', baseUrl: 'http://localhost:4173' } }) ``` ### 10.3 代码质量工具 - **ESLint**:代码规范检查 - **Prettier**:代码格式化 - **TypeScript**:类型检查 - **Vue TSC**:Vue 模板类型检查 ## 十一、部署与构建架构 ### 11.1 构建配置 多环境构建支持: ```typescript // 支持多种构建模式 "scripts": { "dev": "vite", "build": "run-p type-check \"build-only {@}\" --", "preview": "vite preview", "serve": "vite preview" } ``` ### 11.2 部署架构 - **静态资源部署**:构建后的静态文件 - **CDN 支持**:支持 CDN 加速 - **环境变量**:多环境配置支持 - **健康检查**:部署后的健康状态检查 ### 11.3 容器化支持 Docker 容器化部署: ```dockerfile # 多阶段构建优化 FROM node:18-alpine as builder WORKDIR /app COPY package*.json ./ RUN npm ci --only=production FROM nginx:alpine COPY --from=builder /app/dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/nginx.conf ``` ## 十二、架构优势与创新点 ### 12.1 架构优势 1. **现代化技术栈**: - Vue 3 Composition API 的最佳实践 - TypeScript 的完整类型支持 - Vite 的高性能构建 2. **优秀的状态管理**: - Pinia 的响应式状态管理 - 模块化的 Store 设计 - 智能的缓存机制 3. **组件化设计**: - 高度可复用的组件 - 清晰的组件职责划分 - 组合式函数的最佳实践 4. **实时交互能力**: - WebSocket 和长轮询结合 - 流畅的实时数据更新 - 优雅的错误处理 5. **国际化支持**: - 完整的 i18n 架构 - 运行时语言切换 - 后端语言同步 ### 12.2 技术创新点 1. **执行管理器模式**: - 单例模式的管理器设计 - 事件驱动的架构 - 智能的轮询机制 2. **消息系统架构**: - 响应式的消息状态管理 - 流式消息处理 - 消息类型扩展性 3. **模板组织系统**: - 多种组织方式支持 - 分组和排序功能 - 版本控制机制 4. **缓存策略**: - 双层缓存设计 - 智能缓存清理 - 内存优化管理 5. **错误处理机制**: - 多层次的错误捕获 - 用户友好的错误提示 - 自动恢复机制 ## 十三、性能与用户体验优化 ### 13.1 性能优化策略 1. **组件渲染优化**: - 使用 `v-show` 替代 `v-if` 对于频繁切换的元素 - 合理使用 `computed` 和 `watch` - 避免不必要的组件重新渲染 2. **数据获取优化**: - API 请求的防抖处理 - 数据的本地缓存 - 分页和懒加载机制 3. **资源加载优化**: - 组件的异步加载 - 图片的懒加载 - 字体和图标的优化 ### 13.2 用户体验优化 1. **交互反馈**: - 加载状态的清晰指示 - 操作成功的即时反馈 - 错误信息的友好展示 2. **视觉设计**: - 现代化的 UI 设计 - 流畅的动画过渡 - 响应式布局适配 3. **可访问性**: - 键盘导航支持 - 屏幕阅读器兼容 - 高对比度模式 ## 十四、未来发展方向 ### 14.1 技术演进方向 1. **性能提升**: - 更智能的缓存策略 - 更高效的渲染机制 - 更好的内存管理 2. **功能扩展**: - 更多的交互模式 - 更丰富的可视化组件 - 更强大的编辑功能 3. **架构优化**: - 微前端架构支持 - 服务端渲染(SSR) - 渐进式Web应用(PWA) ### 14.2 生态建设 1. **组件库建设**: - 通用的组件库 - 主题定制系统 - 插件扩展机制 2. **开发工具**: - 可视化开发工具 - 自动化测试工具 - 性能分析工具 3. **社区建设**: - 开源社区运营 - 最佳实践分享 - 开发者培训 ## 结语 JManus UI-Vue3 作为现代化企业级 Vue3 应用的优秀实践,其架构设计体现了当前前端开发的最佳实践。通过组件化架构、响应式状态管理、实时交互能力、国际化支持等核心特性,为用户提供了优秀的使用体验。 其创新的执行管理器模式、智能的缓存策略、完善的错误处理机制等技术亮点,不仅解决了复杂 AI 应用的前端挑战,更为整个前端开发领域提供了宝贵的经验和参考。随着技术的不断发展和用户需求的不断变化,UI-Vue3 必将在企业级前端应用开发中发挥更加重要的作用。 通过深入分析 UI-Vue3 的架构设计,我们可以看到现代前端应用的发展方向:更加组件化、更加响应式、更加用户友好、更加可维护。UI-Vue3 不仅是一个技术产品,更是前端工程化方法论的具体实践,为推动前端技术在企业级应用中的落地提供了重要的技术支撑和最佳实践指导。
✨步子哥 (steper) #3
11-08 17:16
# JManus 项目编译与启动完整指南 ## 📋 前置要求 在开始之前,请确保您的系统满足以下要求: ### 1. 系统环境要求 - **操作系统**:Windows 10/11、macOS 10.15+ 或 Linux (Ubuntu 18.04+) - **内存**:至少 8GB RAM(推荐 16GB) - **磁盘空间**:至少 5GB 可用空间 ### 2. 软件依赖 #### Java 环境 - **Java JDK**:17 或更高版本 - **Maven**:3.8+(项目已包含 Maven Wrapper) #### Node.js 环境 - **Node.js**:18.x 或更高版本 - **pnpm**:包管理器(推荐) #### 可选工具 - **Git**:用于克隆代码仓库 - **Docker**:用于容器化部署(可选) ## 🚀 快速启动方法(推荐) ### 方法 1:使用预编译 JAR 文件(最简单) ```bash # 1. 下载最新版本的 JAR 文件 wget https://github.com/spring-ai-alibaba/JManus/releases/latest/download/jmanus.jar # 或者使用 curl curl -L -o jmanus.jar https://github.com/spring-ai-alibaba/JManus/releases/latest/download/jmanus.jar # 2. 直接运行 java -jar jmanus.jar # 3. 访问应用 # 打开浏览器访问 http://localhost:18080 ``` ### 方法 2:使用 Docker(推荐) ```bash # 1. 确保已安装 Docker docker --version # 2. 运行容器(自动下载镜像) docker run -d -p 18080:18080 --name jmanus springai/jmanus:latest # 3. 访问应用 # 打开浏览器访问 http://localhost:18080 ``` ## 🔧 从源码编译启动 ### 步骤 1:环境准备 #### 安装 Java 17+ ```bash # 检查 Java 版本 java -version # 如果未安装,请下载安装: # Windows: https://adoptium.net/ # macOS: brew install openjdk@17 # Linux: sudo apt install openjdk-17-jdk ``` #### 安装 Node.js 18+ ```bash # 检查 Node.js 版本 node --version # 如果未安装,请下载安装: # https://nodejs.org/ 下载 18.x 版本 # 安装 pnpm(推荐) npm install -g pnpm # 或者使用 npm npm install -g npm@latest ``` ### 步骤 2:获取源码 ```bash # 克隆代码仓库 git clone https://github.com/spring-ai-alibaba/JManus.git cd JManus # 或者下载 ZIP 包并解压 # wget https://github.com/spring-ai-alibaba/JManus/archive/refs/heads/main.zip # unzip main.zip # cd JManus-main ``` ### 步骤 3:编译后端(Java) #### 选项 A:使用 Maven Wrapper(推荐) ```bash # 进入项目目录 cd JManus # 编译项目(跳过测试以加快速度) ./mvnw clean package -DskipTests # 或者使用 Maven(如果已安装) mvn clean package -DskipTests ``` #### 选项 B:使用 Makefile ```bash # 编译 Java 后端 make build # 或者分别执行 make java-build ``` ### 步骤 4:编译前端(Vue3) #### 进入前端目录 ```bash cd ui-vue3 ``` #### 安装依赖 ```bash # 使用 pnpm(推荐) pnpm install # 或者使用 npm npm install # 或者使用 yarn yarn install ``` #### 编译前端 ```bash # 构建生产版本 pnpm run build # 或者使用 Makefile cd .. && make ui-build ``` ### 步骤 5:启动应用 #### 选项 A:直接启动 JAR 文件 ```bash # 进入项目根目录 cd .. # 运行编译好的 JAR 文件 java -jar target/jmanus.jar # 或者使用 Maven 运行 ./mvnw spring-boot:run ``` #### 选项 B:使用 Makefile 启动 ```bash # 启动后端服务 make run # 或者分别启动 make java-run ``` #### 选项 C:开发模式(前后端分离) **后端开发模式:** ```bash # 在项目根目录启动后端 ./mvnw spring-boot:run # 后端将在 http://localhost:18080 运行 ``` **前端开发模式:** ```bash # 在 ui-vue3 目录启动前端开发服务器 cd ui-vue3 pnpm run dev # 前端将在 http://localhost:5173 运行 # 会自动代理 API 请求到 http://localhost:18080 ``` ## 🛠️ 高级配置 ### 数据库配置(可选) 默认使用 H2 内存数据库,如需使用 MySQL/PostgreSQL: 1. **修改配置文件** `src/main/resources/application.yml`: ```yaml spring: profiles: active: mysql # 或 postgres ``` 2. **配置数据库连接** `src/main/resources/application-mysql.yml`: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/jmanus username: your_username password: your_password jpa: database-platform: org.hibernate.dialect.MySQLDialect ``` ### API 密钥配置 1. **启动后配置**:访问 http://localhost:18080 按向导配置 2. **手动配置**:修改配置文件或设置环境变量 ## 📊 验证启动 ### 1. 检查服务状态 ```bash # 检查端口是否监听 netstat -an | grep 18080 # 或者使用 curl curl http://localhost:18080/api/init/status ``` ### 2. 访问应用 - **Web 界面**:http://localhost:18080 - **API 文档**:http://localhost:18080/swagger-ui.html - **健康检查**:http://localhost:18080/actuator/health ### 3. 测试 API ```bash # 测试初始化状态 curl http://localhost:18080/api/init/status # 测试基本功能 curl -X POST http://localhost:18080/api/executor/executeByToolNameSync/test \ -H "Content-Type: application/json" \ -d '{"toolName":"test"}' ``` ## 🐛 常见问题解决 ### 问题 1:端口被占用 ```bash # 查找占用 18080 端口的进程 lsof -i :18080 # 或 netstat -ano | findstr 18080 # 终止进程(Linux/macOS) kill -9 # 或修改端口 java -jar jmanus.jar --server.port=8080 ``` ### 问题 2:内存不足 ```bash # 增加 JVM 内存 java -Xmx4g -Xms2g -jar jmanus.jar # 或者设置环境变量 export JAVA_OPTS="-Xmx4g -Xms2g" java -jar jmanus.jar ``` ### 问题 3:前端构建失败 ```bash # 清除缓存重新安装 cd ui-vue3 rm -rf node_modules pnpm-lock.yaml pnpm install # 检查 Node.js 版本 node --version # 需要 18+ ``` ### 问题 4:Maven 构建失败 ```bash # 清除 Maven 缓存 ./mvnw clean # 强制更新依赖 ./mvnw dependency:purge-local-repository # 跳过测试构建 ./mvnw package -DskipTests -Dmaven.test.skip=true ``` ## 🚀 生产环境部署 ### Docker 部署 ```bash # 构建 Docker 镜像 docker build -t jmanus:latest . # 运行容器 docker run -d \ --name jmanus \ -p 18080:18080 \ -e JAVA_OPTS="-Xmx2g -Xms1g" \ jmanus:latest ``` ### 系统服务(Linux) ```bash # 创建 systemd 服务文件 sudo nano /etc/systemd/system/jmanus.service # 添加以下内容 [Unit] Description=JManus AI System After=network.target [Service] Type=simple User=jmanus WorkingDirectory=/opt/jmanus ExecStart=/usr/bin/java -jar jmanus.jar Restart=always RestartSec=10 [Install] WantedBy=multi-user.target # 启动服务 sudo systemctl enable jmanus sudo systemctl start jmanus sudo systemctl status jmanus ``` ## 📋 开发环境设置 ### IDE 配置 1. **IntelliJ IDEA**(推荐) - 导入为 Maven 项目 - 配置 Java 17 SDK - 启用 Spring Boot 支持 2. **VS Code** - 安装 Java Extension Pack - 安装 Spring Boot Extension Pack - 配置调试环境 ### 调试模式 ```bash # 启用调试模式 java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar jmanus.jar # 前端开发模式(热重载) cd ui-vue3 pnpm run dev ``` ## 🔧 常用命令速查 ### 后端命令 ```bash # 编译 ./mvnw clean package # 运行测试 ./mvnw test # 代码格式化 ./mvnw spotless:apply # 启动应用 ./mvnw spring-boot:run ``` ### 前端命令 ```bash # 安装依赖 pnpm install # 开发模式 pnpm run dev # 构建生产版本 pnpm run build # 运行测试 pnpm run test:unit # 代码检查 pnpm run lint ``` ### Makefile 命令 ```bash # 查看所有可用命令 make help # 编译项目 make build # 运行测试 make test # 构建 UI make ui-build # 运行 UI 开发服务器 make ui-run # 完整构建(后端+前端) make build && make ui-build ``` ## 📚 下一步 启动成功后,您可以: 1. **访问 Web 界面**:http://localhost:18080 2. **配置 API 密钥**:按照向导配置您的 AI 模型 API 密钥 3. **查看文档**:访问 API 文档了解可用接口 4. **运行示例**:尝试内置的示例任务 5. **自定义配置**:根据需求调整系统配置
✨步子哥 (steper) #4
11-08 22:46
# JManus 存储层架构与设计思想深度解析 ## 引言 JManus 作为基于 Spring AI Alibaba 构建的 AI Agent 管理系统,其存储层设计体现了现代分布式系统中数据持久化的最佳实践。本文将从架构和设计思想的角度,深入剖析 JManus 存储层的设计理念、技术选型、核心组件以及实现细节,为读者呈现一个完整的企业级存储解决方案。 ## 一、整体架构概览 ### 1.1 架构设计理念 JManus 存储层采用**分层解耦、多数据库支持、领域驱动设计**三大核心设计理念: - **分层解耦**:通过 Repository 模式将数据访问逻辑与业务逻辑分离,实现关注点分离 - **多数据库支持**:支持 H2、MySQL、PostgreSQL 三种数据库,满足不同场景需求 - **领域驱动设计**:按照业务领域划分存储模块,每个领域拥有独立的实体和存储逻辑 ### 1.2 技术栈选型 ```mermaid graph TD A[Spring Boot 3.5.6] --> B[Spring Data JPA] B --> C[Hibernate] C --> D[H2/MySQL/PostgreSQL] E[Spring AI] --> F[Chat Memory] F --> G[Multi-database Support] H[Jackson] --> I[JSON Serialization] I --> J[Entity Conversion] K[HikariCP] --> L[Connection Pool] L --> M[Database Performance] ``` 核心依赖: - **Spring Data JPA 3.5.6**:提供声明式数据访问 - **Hibernate**:ORM 框架,支持自动 DDL 生成 - **HikariCP**:高性能数据库连接池 - **Jackson**:JSON 序列化与反序列化 - **Spring AI**:AI 对话内存管理 ## 二、多数据库支持架构 ### 2.1 数据库配置策略 JManus 采用**配置文件分离**的策略,为每种数据库提供独立的配置: #### H2 数据库配置(开发环境) ```yaml spring: datasource: url: jdbc:h2:file:./h2-data/openmanus_db;MODE=MYSQL;DATABASE_TO_LOWER=TRUE driver-class-name: org.h2.Driver username: sa password: $FSD#@!@#!#$!12341234 jpa: database-platform: org.hibernate.dialect.H2Dialect hibernate: ddl-auto: update ``` #### MySQL 配置(生产环境) ```yaml spring: datasource: url: jdbc:mysql://your-mysql-host:3306/openmanus_db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8 driver-class-name: com.mysql.cj.jdbc.Driver username: your_mysql_username password: your_mysql_password jpa: database-platform: org.hibernate.dialect.MySQLDialect hibernate: ddl-auto: update ``` #### PostgreSQL 配置(企业环境) ```yaml spring: datasource: url: jdbc:postgresql://localhost:5432/openmanus_db driver-class-name: org.postgresql.Driver username: postgres password: 123456 jpa: database-platform: org.hibernate.dialect.PostgreSQLDialect hibernate: ddl-auto: update ``` ### 2.2 连接池优化配置 JManus 使用 HikariCP 作为连接池,提供了精细化的配置: ```yaml spring: datasource: hikari: maximum-pool-size: 20 # 最大连接数 minimum-idle: 5 # 最小空闲连接 connection-timeout: 30000 # 连接超时时间 idle-timeout: 600000 # 空闲连接超时 max-lifetime: 1800000 # 连接最大生命周期 pool-name: Spring-AI-Alibaba-JManus-${spring.profiles.active}-Pool connection-test-query: SELECT 1 validation-timeout: 5000 leak-detection-threshold: 60000 ``` ### 2.3 数据库特定内存支持 JManus 为每种数据库实现了特定的聊天内存存储: #### 抽象基类设计 ```java public abstract class JdbcChatMemoryRepository implements ChatMemoryRepository { protected abstract String hasTableSql(String tableName); protected abstract String createTableSql(String tableName); protected abstract String getAddSql(); protected abstract String getGetSql(); } ``` #### H2 实现 ```java public class H2ChatMemoryRepository extends JdbcChatMemoryRepository { @Override protected String createTableSql(String tableName) { return String.format( "CREATE TABLE %s (id BIGINT AUTO_INCREMENT PRIMARY KEY, " + "conversation_id VARCHAR(256) NOT NULL, content LONGTEXT NOT NULL, " + "type VARCHAR(100) NOT NULL, timestamp TIMESTAMP NOT NULL, " + "CONSTRAINT chk_message_type CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL')))", tableName); } } ``` #### MySQL 实现 ```java public class MysqlChatMemoryRepository extends JdbcChatMemoryRepository { @Override protected String hasTableSql(String tableName) { return String.format( "SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = '%s'", tableName); } } ``` #### PostgreSQL 实现 ```java public class PostgresChatMemoryRepository extends JdbcChatMemoryRepository { @Override protected String createTableSql(String tableName) { return String.format( "CREATE TABLE %s (id BIGSERIAL PRIMARY KEY, " + "conversation_id VARCHAR(256) NOT NULL, content TEXT NOT NULL, " + "type VARCHAR(100) NOT NULL, timestamp TIMESTAMP NOT NULL, " + "CONSTRAINT chk_message_type CHECK (type IN ('USER', 'ASSISTANT', 'SYSTEM', 'TOOL')))", tableName); } } ``` ## 三、实体设计与 JPA 实现 ### 3.1 实体设计原则 JManus 的实体设计遵循以下原则: 1. **领域边界清晰**:每个业务领域拥有独立的实体 2. **关系映射合理**:使用适当的 JPA 关系注解 3. **索引优化**:为频繁查询的字段添加索引 4. **数据完整性**:使用约束保证数据质量 ### 3.2 核心实体架构 #### 计划执行记录实体 ```java @Entity @Table(name = "plan_execution_record") public class PlanExecutionRecordEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "current_plan_id", nullable = false, unique = true) private String currentPlanId; @Column(name = "root_plan_id") private String rootPlanId; @Column(name = "parent_plan_id") private String parentPlanId; @Column(name = "user_request", columnDefinition = "LONGTEXT") private String userRequest; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "plan_execution_id") private List agentExecutionSequence; } ``` #### Agent 执行记录实体 ```java @Entity @Table(name = "agent_execution_record", indexes = { @Index(columnList = "step_id") }) public class AgentExecutionRecordEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "step_id", unique = true) private String stepId; @Column(name = "agent_request", columnDefinition = "LONGTEXT") private String agentRequest; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "agent_execution_record_id") private List thinkActSteps; } ``` ### 3.3 复杂类型转换 JManus 使用 JPA 属性转换器处理复杂数据类型: ```java @Converter public class MapToStringConverter implements AttributeConverter, String> { private final ObjectMapper objectMapper; @Override public String convertToDatabaseColumn(Map attribute) { try { return objectMapper.writeValueAsString(attribute); } catch (Exception e) { throw new IllegalArgumentException("Error converting map to string", e); } } @Override public Map convertToEntityAttribute(String dbData) { if (dbData == null || dbData.isEmpty()) { return new HashMap<>(); } try { return objectMapper.readValue(dbData, new TypeReference<>() {}); } catch (Exception e) { throw new IllegalArgumentException("Error converting string to map", e); } } } ``` ## 四、执行记录与审计追踪 ### 4.1 分层执行记录架构 JManus 采用**三层执行记录架构**: ```mermaid graph TD A[PlanExecutionRecordEntity] --> B[AgentExecutionRecordEntity] B --> C[ThinkActRecordEntity] C --> D[ActToolInfoEntity] ``` #### 计划执行记录层 - 记录整个计划的执行生命周期 - 维护计划层次结构(根计划、父计划、子计划) - 存储用户请求和执行结果摘要 #### Agent 执行记录层 - 记录每个 Agent 的执行过程 - 维护执行状态和时间戳 - 关联具体的模型信息 #### 思考-行动记录层 - 记录 Agent 的思考和行动过程 - 存储工具调用信息和结果 - 支持并行工具执行记录 ### 4.2 执行记录服务实现 ```java @Service public class NewRepoPlanExecutionRecorder implements PlanExecutionRecorder { @Transactional public Long recordPlanExecutionStart(String currentPlanId, String title, String userRequest, List executionSteps, String parentPlanId, String rootPlanId, String toolcallId) { // 检查计划是否已存在 Optional existingPlanOpt = planExecutionRecordRepository.findByCurrentPlanId(currentPlanId); PlanExecutionRecordEntity planExecutionRecordEntity; if (existingPlanOpt.isPresent()) { // 更新现有计划 planExecutionRecordEntity = existingPlanOpt.get(); } else { // 创建新计划 planExecutionRecordEntity = new PlanExecutionRecordEntity(currentPlanId); } // 设置层次关系 if (rootPlanId != null && !rootPlanId.trim().isEmpty()) { createPlanRelationship(currentPlanId, parentPlanId, rootPlanId, toolcallId); } return planExecutionRecordRepository.save(planExecutionRecordEntity).getId(); } } ``` ### 4.3 事务管理策略 JManus 在关键业务操作中使用声明式事务管理: ```java @Transactional public void recordCompleteAgentExecution(ExecutionStep step) { // 查询 Agent 执行记录 Optional agentRecordOpt = agentExecutionRecordRepository.findByStepId(step.getStepId()); if (!agentRecordOpt.isPresent()) { throw new IllegalArgumentException("agent record is null"); } AgentExecutionRecordEntity agentRecord = agentRecordOpt.get(); // 更新执行状态 agentRecord.setStatus(ExecutionStatusEntity.FINISHED); agentRecord.setEndTime(LocalDateTime.now()); // 保存更新 agentExecutionRecordRepository.save(agentRecord); } ``` ## 五、MCP 配置存储架构 ### 5.1 MCP 配置模型设计 Model Context Protocol (MCP) 配置存储体现了 JManus 对 AI 工具生态的深度支持: ```java @Entity @Table(name = "mcp_config") public class McpConfigEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String mcpServerName; @Column(nullable = false) @Enumerated(EnumType.STRING) private McpConfigType connectionType; @Column(nullable = false, length = 4000) private String connectionConfig; @Column(nullable = false, columnDefinition = "VARCHAR(10) DEFAULT 'ENABLE'") @Enumerated(EnumType.STRING) private McpConfigStatus status = McpConfigStatus.ENABLE; } ``` ### 5.2 MCP 状态管理 JManus 实现了完整的 MCP 服务状态管理: ```java public enum McpConfigStatus { ENABLE, // 启用状态 DISABLE, // 禁用状态 ERROR, // 错误状态 CONNECTING // 连接中状态 } public enum McpConfigType { HTTP, // HTTP 连接 WEBSOCKET, // WebSocket 连接 STDIO // 标准输入输出 } ``` ### 5.3 MCP 配置验证与连接管理 ```java @Service public class McpService { public McpConfigVO validateAndConnect(McpServerRequestVO request) { // 配置验证 McpConfigEntity configEntity = mcpConfigRepository .findByMcpServerName(request.getServerName()); if (configEntity == null) { throw new McpConfigNotFoundException( "MCP config not found: " + request.getServerName()); } // 连接测试 McpConnection connection = mcpConnectionFactory .createConnection(configEntity); // 状态更新 configEntity.setStatus(McpConfigStatus.ENABLE); mcpConfigRepository.save(configEntity); return convertToVO(configEntity); } } ``` ## 六、计划模板与执行计划存储 ### 6.1 计划模板存储架构 计划模板存储支持版本管理和参数化配置: ```java @Entity @Table(name = "plan_template") public class PlanTemplate { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "plan_template_id", length = 50, unique = true, nullable = false) private String planTemplateId; @Column(name = "title", length = 255) private String title; @Column(name = "user_request", length = 4000) private String userRequest; @Column(name = "is_internal_toolcall", nullable = false) private boolean isInternalToolcall = false; } ``` ### 6.2 计划版本管理 ```java @Entity @Table(name = "plan_template_version") public class PlanTemplateVersion { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "plan_template_id", nullable = false) private PlanTemplate planTemplate; @Column(name = "version", nullable = false) private Integer version; @Column(name = "content", columnDefinition = "LONGTEXT") private String content; @Column(name = "is_current", nullable = false) private boolean isCurrent = false; } ``` ### 6.3 动态计划创建与存储 ```java @Service public class DynamicAgentPlanCreator { public PlanExecutionRecordEntity createDynamicPlan( String planTemplateId, Map parameters, String parentPlanId, String rootPlanId) { // 获取计划模板 PlanTemplate template = planTemplateRepository .findByPlanTemplateId(planTemplateId) .orElseThrow(() -> new PlanTemplateNotFoundException(planTemplateId)); // 参数映射 Map mappedParameters = planParameterMappingService .mapParameters(template, parameters); // 创建执行计划 PlanExecutionRecordEntity executionPlan = new PlanExecutionRecordEntity(); executionPlan.setCurrentPlanId(generatePlanId()); executionPlan.setTitle(template.getTitle()); executionPlan.setUserRequest(processTemplate(template.getUserRequest(), mappedParameters)); executionPlan.setParentPlanId(parentPlanId); executionPlan.setRootPlanId(rootPlanId); return planExecutionRecordRepository.save(executionPlan); } } ``` ## 七、用户管理与命名空间隔离 ### 7.1 用户实体设计 ```java @Entity @Table(name = "users") public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username", unique = true, nullable = false, length = 50) private String username; @Column(name = "email", unique = true, nullable = false, length = 100) private String email; @Column(name = "display_name", length = 100) private String displayName; @Column(name = "status", length = 20) private String status; @ElementCollection @CollectionTable(name = "user_preferences", joinColumns = @JoinColumn(name = "user_id")) @Column(name = "preference") private List preferences; } ``` ### 7.2 命名空间隔离机制 ```java @Entity @Table(name = "namespace") public class NamespaceEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name", nullable = false, unique = true, length = 100) private String name; @Column(name = "code", nullable = false, unique = true, length = 50) private String code; @Column(name = "description", length = 500) private String description; } ``` ### 7.3 数据初始化策略 JManus 使用 Spring Boot 的 `CommandLineRunner` 实现数据初始化: ```java @Component public class UserDataInitializer implements CommandLineRunner { @Override public void run(String... args) throws Exception { initializeDefaultUsers(); } private void initializeDefaultUsers() { if (!userRepository.existsByUsername("jmanus_user")) { UserEntity defaultUser = new UserEntity( "jmanus_user", "user@jmanus.ai", "JManus User"); defaultUser.setStatus("active"); defaultUser.setCreatedAt(LocalDateTime.now().minusDays(30)); defaultUser.setPreferences(Arrays.asList("dark_mode", "notifications_enabled")); userRepository.save(defaultUser); } } } ``` ## 八、内存管理与聊天历史存储 ### 8.1 聊天内存架构设计 JManus 实现了多数据库支持的聊天内存存储: ```mermaid graph TD A[ChatMemory] --> B[ChatMemoryRepository] B --> C[JdbcChatMemoryRepository] C --> D[H2ChatMemoryRepository] C --> E[MysqlChatMemoryRepository] C --> F[PostgresChatMemoryRepository] ``` ### 8.2 内存配置管理 ```java @Configuration public class MemoryConfig { @Bean public ChatMemoryRepository chatMemoryRepository(JdbcTemplate jdbcTemplate) { ChatMemoryRepository chatMemoryRepository = null; if (mysqlEnabled) { chatMemoryRepository = MysqlChatMemoryRepository .mysqlBuilder() .jdbcTemplate(jdbcTemplate) .build(); } else if (postgresEnabled) { chatMemoryRepository = PostgresChatMemoryRepository .postgresBuilder() .jdbcTemplate(jdbcTemplate) .build(); } else if (h2Enabled) { chatMemoryRepository = H2ChatMemoryRepository .h2Builder() .jdbcTemplate(jdbcTemplate) .build(); } return chatMemoryRepository; } } ``` ### 8.3 消息窗口管理 ```java @Bean public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) { return MessageWindowChatMemory.builder() .chatMemoryRepository(chatMemoryRepository) .build(); } ``` ## 九、文件上传与静态资源管理 ### 9.1 文件上传配置 ```yaml manus: file-upload: max-file-size: 1073741824 # 1GB max-files-per-upload: 10 upload-directory: uploaded_files validation-strategy: code # code 或 config ``` ### 9.2 文件验证策略 JManus 提供两种文件验证策略: 1. **代码验证策略**:通过代码逻辑验证文件类型 2. **配置验证策略**:通过配置文件定义允许的文件类型 ### 9.3 文件存储服务 ```java @Service public class FileUploadService { public FileUploadResult uploadFiles(MultipartFile[] files, String uploadId) { List uploadedFiles = new ArrayList<>(); for (MultipartFile file : files) { // 文件验证 if (!fileValidationService.validateFile(file)) { throw new FileValidationException("Invalid file type: " + file.getOriginalFilename()); } // 文件存储 String filePath = storeFile(file, uploadId); FileInfo fileInfo = createFileInfo(file, filePath); uploadedFiles.add(fileInfo); } return new FileUploadResult(uploadId, uploadedFiles); } } ``` ## 十、定时任务与调度存储 ### 10.1 Cron 任务实体设计 ```java @Entity @Table(name = "cron_tasks") public class CronEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "cron_name", nullable = false, length = 100) private String cronName; @Column(name = "cron_expression", nullable = false, length = 100) private String cronExpression; @Column(name = "plan_desc", columnDefinition = "TEXT") private String planDescription; @Enumerated(EnumType.STRING) @Column(name = "status", nullable = false) private TaskStatus status; } ``` ### 10.2 动态任务调度 ```java @Service public class DynamicCronTaskScheduler { public void scheduleCronTask(CronEntity cronTask) { // 解析 Cron 表达式 CronExpression cronExpression = CronExpression.parse(cronTask.getCronExpression()); // 创建调度任务 ScheduledFuture scheduledTask = taskScheduler.schedule( () -> executeCronTask(cronTask), cronExpression ); // 存储任务引用 scheduledTasks.put(cronTask.getId(), scheduledTask); } } ``` ## 十一、模型配置与动态模型管理 ### 11.1 动态模型实体设计 ```java @Entity @Table(name = "dynamic_models") public class DynamicModelEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "model_name", nullable = false, unique = true, length = 100) private String modelName; @Column(name = "base_url", nullable = false, length = 500) private String baseUrl; @Column(name = "api_key", length = 500) private String apiKey; @Convert(converter = MapToStringConverter.class) @Column(name = "headers", columnDefinition = "TEXT") private Map headers; @Column(name = "is_default") private Boolean isDefault = false; @Column(name = "temperature") private Double temperature = 0.7; @Column(name = "top_p") private Double topP = 1.0; } ``` ### 11.2 模型缓存策略 ```java @Service public class ModelServiceImpl implements ModelService { // 第三方 API 调用缓存,2秒过期 private final Map>> apiCache = new ConcurrentHashMap<>(); private static final long CACHE_EXPIRY_MS = 2000; // 2秒 public List getAvailableModels(String baseUrl, String apiKey) { String cacheKey = baseUrl + ":" + apiKey; // 检查缓存 CacheEntry> cachedEntry = apiCache.get(cacheKey); if (cachedEntry != null && !cachedEntry.isExpired()) { return cachedEntry.getData(); } // 调用 API List models = callThirdPartyApiInternal(baseUrl, apiKey); // 缓存结果 apiCache.put(cacheKey, new CacheEntry<>(models)); return models; } } ``` ### 11.3 模型验证与连接测试 ```java public ValidationResult validateConfig(String baseUrl, String apiKey) { try { // 1. 验证 Base URL 格式 if (!isValidBaseUrl(baseUrl)) { return ValidationResult.invalid("Base URL format is incorrect"); } // 2. 验证 API Key 格式 if (!isValidApiKey(apiKey)) { return ValidationResult.invalid("API Key format is incorrect"); } // 3. 调用第三方 API 验证 List models = callThirdPartyApiInternal(baseUrl, apiKey); return ValidationResult.valid(models); } catch (AuthenticationException e) { return ValidationResult.invalid("API Key is invalid or expired"); } catch (NetworkException e) { return ValidationResult.invalid("Network connection failed"); } } ``` ## 十二、数据初始化与迁移策略 ### 12.1 数据初始化架构 JManus 采用**模块化初始化**策略,每个业务领域拥有独立的数据初始化器: ```mermaid graph TD A[Application Startup] --> B[CommandLineRunner] B --> C[UserDataInitializer] B --> D[NamespaceDataInitialization] B --> E[ModelDataInitialization] B --> F[PlanTemplateInitializationService] C --> G[Default User Creation] D --> H[Default Namespace Creation] E --> I[Default Models Setup] F --> J[Plan Templates Setup] ``` ### 12.2 初始化实现模式 所有初始化器遵循统一的模式: ```java @Component public class DataInitializer implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(DataInitializer.class); @Override public void run(String... args) throws Exception { logger.info("Starting {} data initialization", getInitializerName()); try { if (shouldInitialize()) { initializeData(); logger.info("{} data initialization completed successfully", getInitializerName()); } else { logger.info("{} data already exists, skipping initialization", getInitializerName()); } } catch (Exception e) { logger.error("Error during {} data initialization: {}", getInitializerName(), e.getMessage(), e); // 初始化失败不应阻止应用启动 } } protected abstract String getInitializerName(); protected abstract boolean shouldInitialize(); protected abstract void initializeData(); } ``` ### 12.3 幂等性保证 所有初始化操作都具有幂等性: ```java private void initializeDefaultUsers() { // 检查是否已存在,避免重复创建 if (!userRepository.existsByUsername("jmanus_user")) { UserEntity defaultUser = new UserEntity( "jmanus_user", "user@jmanus.ai", "JManus User"); defaultUser.setStatus("active"); defaultUser.setCreatedAt(LocalDateTime.now()); userRepository.save(defaultUser); logger.info("Created default user: {}", defaultUser.getUsername()); } } ``` ## 十三、事务管理与数据一致性 ### 13.1 事务管理策略 JManus 采用**声明式事务管理**,在关键业务操作中使用 `@Transactional` 注解: #### 计划执行记录事务 ```java @Transactional public Long recordPlanExecutionStart(String currentPlanId, String title, String userRequest, List executionSteps, String parentPlanId, String rootPlanId, String toolcallId) { // 创建或更新计划执行记录 PlanExecutionRecordEntity planRecord = createOrUpdatePlan(currentPlanId, title, userRequest); // 创建 Agent 执行记录 List agentRecords = createAgentExecutionRecords(executionSteps); // 建立关联关系 planRecord.setAgentExecutionSequence(agentRecords); // 创建层次关系 if (rootPlanId != null) { createPlanRelationship(currentPlanId, parentPlanId, rootPlanId, toolcallId); } // 保存所有实体 return planExecutionRecordRepository.save(planRecord).getId(); } ``` #### Agent 执行完成事务 ```java @Transactional public void recordCompleteAgentExecution(ExecutionStep step) { // 查询 Agent 执行记录 AgentExecutionRecordEntity agentRecord = agentExecutionRecordRepository .findByStepId(step.getStepId()) .orElseThrow(() -> new IllegalArgumentException("Agent record not found")); // 更新执行状态 agentRecord.setStatus(convertAgentStateToExecutionStatus(step.getStatus())); agentRecord.setEndTime(LocalDateTime.now()); agentRecord.setResult(step.getResult()); // 保存更新 agentExecutionRecordRepository.save(agentRecord); } ``` ### 13.2 数据一致性保证 #### 乐观锁机制 对于可能并发更新的实体,使用版本字段实现乐观锁: ```java @Entity public class PlanExecutionRecordEntity { @Version private Long version; // 其他字段... } ``` #### 唯一约束保证 通过数据库唯一约束防止重复数据: ```java @Entity @Table(name = "agent_execution_record", uniqueConstraints = @UniqueConstraint(columnNames = "step_id")) public class AgentExecutionRecordEntity { @Column(name = "step_id", unique = true) private String stepId; } ``` #### 业务层一致性检查 在业务逻辑层进行一致性验证: ```java private void createPlanRelationship(String currentPlanId, String parentPlanId, String rootPlanId, String toolcallId) { // 验证必需参数 if (currentPlanId == null || currentPlanId.trim().isEmpty()) { throw new IllegalArgumentException("currentPlanId is required"); } if (rootPlanId == null || rootPlanId.trim().isEmpty()) { throw new IllegalArgumentException("rootPlanId is required"); } // 验证业务规则 if (parentPlanId != null && !parentPlanId.trim().isEmpty()) { if (toolcallId == null || toolcallId.trim().isEmpty()) { throw new IllegalArgumentException( "toolcallId is required when parentPlanId is provided"); } if (rootPlanId.equals(currentPlanId)) { throw new IllegalArgumentException( "rootPlanId cannot equal currentPlanId when parentPlanId is provided"); } } } ``` ## 十四、性能优化与索引策略 ### 14.1 数据库索引设计 JManus 针对高频查询场景设计了优化的索引策略: #### 执行记录索引 ```java @Entity @Table(name = "agent_execution_record", indexes = { @Index(columnList = "step_id") }) public class AgentExecutionRecordEntity { @Column(name = "step_id", unique = true) private String stepId; } ``` #### 计划执行记录复合索引 ```java @Entity @Table(name = "plan_execution_record", indexes = { @Index(columnList = "current_plan_id"), @Index(columnList = "root_plan_id"), @Index(columnList = "parent_plan_id") }) public class PlanExecutionRecordEntity { // 实体字段... } ``` #### MCP 配置索引 ```java @Entity @Table(name = "mcp_config", indexes = { @Index(columnList = "mcp_server_name") }) public class McpConfigEntity { @Column(nullable = false, unique = true) private String mcpServerName; } ``` ### 14.2 查询优化策略 #### 延迟加载策略 ```java @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumn(name = "plan_execution_id") private List agentExecutionSequence; ``` #### 批量操作优化 ```java @Override public void saveAll(String conversationId, List messages) { // 先删除旧数据 this.deleteByConversationId(conversationId); // 批量插入新数据 this.jdbcTemplate.batchUpdate(getAddSql(), new AddBatchPreparedStatement(conversationId, messages)); } ``` ### 14.3 缓存策略 #### API 调用缓存 ```java // 第三方 API 调用缓存,2秒过期 private final Map>> apiCache = new ConcurrentHashMap<>(); private static final long CACHE_EXPIRY_MS = 2000; // 2秒 public List getAvailableModels(String baseUrl, String apiKey) { String cacheKey = baseUrl + ":" + apiKey; // 检查缓存 CacheEntry> cachedEntry = apiCache.get(cacheKey); if (cachedEntry != null && !cachedEntry.isExpired()) { return cachedEntry.getData(); } // 调用 API 并缓存结果 List models = callThirdPartyApiInternal(baseUrl, apiKey); apiCache.put(cacheKey, new CacheEntry<>(models)); return models; } ``` #### 默认模型缓存 ```java @Service public class LlmService { private volatile ChatModel defaultChatModel; public void refreshDefaultModelCache() { // 刷新默认模型缓存 DynamicModelEntity defaultModel = dynamicModelRepository .findByIsDefaultTrue(); if (defaultModel != null) { this.defaultChatModel = createChatModel(defaultModel); } } } ``` ### 14.4 数据库连接池优化 #### HikariCP 配置优化 ```yaml spring: datasource: hikari: maximum-pool-size: 20 # 根据服务器配置调整 minimum-idle: 5 # 保持最小连接数 connection-timeout: 30000 # 连接超时时间 idle-timeout: 600000 # 空闲连接超时 max-lifetime: 1800000 # 连接最大生命周期 leak-detection-threshold: 60000 # 连接泄露检测 ``` #### 连接池监控 ```java @Component public class DatabaseConnectionPoolMonitor { @Autowired private DataSource dataSource; @Scheduled(fixedDelay = 60000) // 每分钟监控一次 public void monitorConnectionPool() { if (dataSource instanceof HikariDataSource) { HikariDataSource hikariDataSource = (HikariDataSource) dataSource; logger.info("Connection Pool Stats - Active: {}, Idle: {}, Total: {}, Waiting: {}", hikariDataSource.getHikariPoolMXBean().getActiveConnections(), hikariDataSource.getHikariPoolMXBean().getIdleConnections(), hikariDataSource.getHikariPoolMXBean().getTotalConnections(), hikariDataSource.getHikariPoolMXBean().getThreadsAwaitingConnection()); } } } ``` ## 十五、设计思想总结 ### 15.1 架构设计原则 JManus 存储层的设计体现了以下核心原则: #### 1. **领域驱动设计 (DDD)** - 按照业务领域划分存储模块 - 每个领域拥有独立的实体、仓库和服务 - 清晰的领域边界和职责分离 #### 2. **分层架构思想** - 表现层 → 业务层 → 数据访问层 → 数据存储层 - 每层职责单一,依赖关系清晰 - 通过接口实现层间解耦 #### 3. **配置驱动开发** - 通过配置控制不同数据库的支持 - 灵活的配置文件分离策略 - 环境特定的配置优化 #### 4. **企业级特性支持** - 完整的事务管理机制 - 数据一致性保证 - 性能优化和监控 - 安全性和权限控制 ### 15.2 技术创新点 #### 1. **多数据库统一抽象** 通过抽象基类和模板方法模式,实现了对 H2、MySQL、PostgreSQL 的统一支持,同时保留了各数据库的特性优化。 #### 2. **分层执行记录架构** 创新的三层执行记录架构(计划→Agent→思考行动),为 AI 执行过程提供了完整的审计追踪能力。 #### 3. **动态模型管理** 支持运行时动态配置和切换 AI 模型,通过缓存策略优化性能,为 AI 应用提供了灵活的模型管理能力。 #### 4. **MCP 生态集成** 深度集成 Model Context Protocol,为 AI 工具生态提供了标准化的配置和管理机制。 ### 15.3 最佳实践总结 #### 1. **数据访问最佳实践** - 使用 Spring Data JPA 简化数据访问 - 通过 Repository 模式实现数据访问抽象 - 合理使用索引优化查询性能 - 采用延迟加载避免 N+1 查询问题 #### 2. **事务管理最佳实践** - 在业务服务层使用声明式事务 - 合理控制事务边界和粒度 - 使用只读事务优化查询性能 - 实现幂等性保证重复操作安全 #### 3. **性能优化最佳实践** - 数据库连接池的合理配置 - 缓存策略的灵活运用 - 批量操作减少数据库交互 - 索引设计的针对性优化 #### 4. **可维护性最佳实践** - 统一的编码规范和命名约定 - 完善的日志记录和监控 - 模块化的代码组织结构 - 详尽的文档和注释 ## 结语 JManus 的存储层架构设计充分体现了现代分布式系统中数据持久化的先进理念和最佳实践。通过多数据库支持、领域驱动设计、分层架构、事务管理、性能优化等多个维度的深入设计,构建了一个**高可用、高性能、可扩展、易维护**的企业级存储解决方案。 该架构不仅满足了当前 AI Agent 管理系统的需求,也为未来的功能扩展和性能优化奠定了坚实的基础。其设计思想和实现模式对于构建类似的企业级应用具有重要的参考价值。 随着 AI 技术的不断发展,JManus 的存储架构也将持续演进,为 AI 应用提供更加强大和灵活的数据支撑能力。