docs: 更新并翻译 CLAUDE.md 为简体中文

- 同步版本号 2.15.3 -> 2.16.0
- 修正失效的构建命令:移除不存在的 compile/release profile,
  补充 JDK 驱动的模块选择(compile-8-to-16 / compile-17+)及
  release-on-8 / release-on-17 发布 profile 说明
- 新增 liteflow-react-agent(5 个子模块)与 liteflow-spring-boot4-starter
  的模块说明,并指向 docs/liteflow-react-agent-guide.md
- 将全文叙述性内容翻译为简体中文,代码/类名/方法名/配置项保持原样

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
everywhere.z
2026-05-31 21:43:18 +08:00
parent 49044d1b76
commit 53f7e20cf3

331
CLAUDE.md
View File

@@ -1,171 +1,186 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
本文件为 Claude Code (claude.ai/code) 在本仓库中工作时提供指引。
## Overview
## 概述
LiteFlow (v2.15.3) is a lightweight rules engine framework for complex component-based business orchestration. It uses a DSL to drive workflows with support for hot reload and 11 scripting languages. The project targets Java 8+ (up to JDK 25) and has 2000+ test cases.
LiteFlowv2.16.0)是一个轻量级规则引擎框架,用于复杂的组件化业务编排。它通过 DSL 驱动工作流,支持热重载和 11 种脚本语言。项目面向 Java 8+(最高支持到 JDK 25拥有 2000+ 测试用例。
**Official Documentation**: https://liteflow.cc/pages/5816c5/
**官方文档**https://liteflow.cc/pages/5816c5/
## Commands
## 命令
### Build
### 构建
```bash
# Build entire project (uses 'compile' profile by default, includes test modules)
# 构建整个项目(模块集由 JDK 版本自动选择 —— 见下方说明)
mvn clean package -DskipTests
# Build with tests
# 带测试构建
mvn clean package
# Build specific module
# 构建指定模块
mvn clean package -DskipTests -pl liteflow-core
# Build for release (production modules only, excludes tests)
mvn clean package -DskipTests -P release
# 发布生产模块(手动选择 profile不会自动激活
mvn clean package -DskipTests -P release-on-8 # JDK 8 发布产物
mvn clean package -DskipTests -P release-on-17 # react-agent + spring-boot4-starter
```
### Run Tests
**JDK 驱动的模块选择(重要):**`pom.xml` 中没有顶层的 `<modules>` 块 —— 构建的模块列表完全由按 JDK 激活的 profile 提供:
- `compile-8-to-16`(在 JDK `[1.8,17)` 上激活core、scripts、rules、spring-boot-starter、spring、solon、testcase-el、el-builder、benchmark。
- `compile-17+`(在 JDK `[17,)` 上激活):以上全部,**外加** `liteflow-react-agent``liteflow-spring-boot4-starter`
因此 `liteflow-react-agent``liteflow-spring-boot4-starter` 只有在 JDK 17+ 上构建时才会被纳入。这两个模块以 `maven.compiler.target=17` 编译,但根 pom 注释指出 agentscope-javareact-agent 背后的引擎)**运行时需要 Java 21+** —— 本仓库当前的开发 JDK 为 21。`release-on-8` / `release-on-17` 这两个 profile 没有 `<activation>`,必须用 `-P` 手动选择。
### 运行测试
```bash
# Run all tests
# 运行所有测试
mvn test
# Run tests for specific module (30+ test modules in liteflow-testcase-el/)
# 运行指定模块的测试(liteflow-testcase-el/ 下有 30+ 个测试模块)
mvn test -pl liteflow-testcase-el/liteflow-testcase-el-springboot
# Run single test class
# 运行单个测试类
mvn test -pl liteflow-core -Dtest=FlowExecutorTest
# Run specific test method
# 运行指定的测试方法
mvn test -pl liteflow-core -Dtest=FlowExecutorTest#testExecute
```
### Other Commands
### 其他命令
```bash
# Dependency tree
# 依赖树
mvn dependency:tree
# View module structure
# 查看模块结构
ls liteflow-*/pom.xml
```
## High-Level Architecture
## 高层架构
### Core Execution Model
### 核心执行模型
**FlowExecutor****Chain****Condition Tree****Node Components**
**FlowExecutor****Chain****Condition Tree(条件树)****Node Components(节点组件)**
1. **FlowExecutor**: Entry point for executing workflows (`execute2Resp(chainId, param)`)
2. **FlowBus**: Central metadata registry for all chains and nodes (thread-safe, supports hot reload)
3. **Chain**: Named workflow composed of an EL expression that compiles to a Condition tree
4. **Slot/DataBus**: Thread-safe context management using slot pooling (configurable `slotSize`)
5. **NodeComponent**: Base class for all business logic components
1. **FlowExecutor**:执行工作流的入口(`execute2Resp(chainId, param)`
2. **FlowBus**:所有 chain 和 node 的中央元数据注册中心(线程安全,支持热重载)
3. **Chain**:由 EL 表达式组成的命名工作流,编译为一棵 Condition
4. **Slot/DataBus**:基于 slot 池的线程安全上下文管理(`slotSize` 可配置)
5. **NodeComponent**:所有业务逻辑组件的基类
### Key Architectural Patterns
### 关键架构模式
#### 1. Two-Stage Parsing
Chains are built in two phases to handle circular dependencies:
- **Phase 1**: Register chain IDs (creates placeholder chains)
- **Phase 2**: Build complete condition trees with EL parsing
#### 1. 两阶段解析
为处理循环依赖chain 的构建分两个阶段进行:
- **阶段 1**:注册 chain ID创建占位 chain
- **阶段 2**:通过 EL 解析构建完整的条件树
#### 2. EL Expression Language
Uses QLExpress to parse declarative workflows. Example operators:
- **THEN(a, b, c)**: Sequential execution
- **WHEN(a, b, c)**: Parallel execution (async)
- **IF(condition, trueNode, falseNode)**: Conditional branching
- **SWITCH(selector).to(a, b, c)**: Multi-way branching
- **FOR(count).DO(loop)**: Fixed-count loop
- **WHILE(condition).DO(loop)**: Condition-based loop
- **ITERATOR(iterator).DO(loop)**: Iterator-based loop
- **RETRY(node).times(3).forException(Ex.class)**: Retry mechanism
- **CATCH(node).DO(handler)**: Exception handling
- **TIMEOUT(node).time(1000)**: Execution timeout (ms)
- **PRE(a, b)**: Pre-conditions (always run before chain, even on error)
- **FINALLY(a, b)**: Finally-conditions (always run after chain)
- **AND(a, b)**, **OR(a, b)**, **NOT(a)**: Boolean logic for IF conditions
- **node.tag("t")**, **.data("k","v")**, **.id("id")**: Per-node modifiers
#### 2. EL 表达式语言
使用 QLExpress 解析声明式工作流。示例算子:
- **THEN(a, b, c)**:顺序执行
- **WHEN(a, b, c)**:并行执行(异步)
- **IF(condition, trueNode, falseNode)**:条件分支
- **SWITCH(selector).to(a, b, c)**:多路分支
- **FOR(count).DO(loop)**:固定次数循环
- **WHILE(condition).DO(loop)**:条件循环
- **ITERATOR(iterator).DO(loop)**:迭代器循环
- **RETRY(node).times(3).forException(Ex.class)**:重试机制
- **CATCH(node).DO(handler)**:异常处理
- **TIMEOUT(node).time(1000)**:执行超时(毫秒)
- **PRE(a, b)**:前置条件(始终在 chain 之前执行,出错也会执行)
- **FINALLY(a, b)**finally 条件(始终在 chain 之后执行)
- **AND(a, b)****OR(a, b)**、**NOT(a)**:用于 IF 条件的布尔逻辑
- **node.tag("t")**、**.data("k","v")**、**.id("id")**:节点级修饰符
Rules are defined in XML/JSON/YML:
规则在 XML/JSON/YML 中定义:
```xml
<chain name="myChain">
THEN(a, WHEN(b, c).maxWaitSeconds(5), IF(e, f, g));
</chain>
```
#### 3. Component Types
All extend `NodeComponent` but have specialized behaviors:
- **NodeComponent**: Standard synchronous component (`process()` method)
- **NodeBooleanComponent**: Returns boolean for IF conditions (`processBoolean()`)
- **NodeSwitchComponent**: Returns string for SWITCH routing (`processSwitch()`)
- **NodeIteratorComponent**: Provides iteration logic for ITERATOR construct
- **NodeForComponent**: Controls FOR loop behavior
- **ScriptComponent**: Script-based components (Groovy, JS, Python, etc.)
#### 3. 组件类型
均继承 `NodeComponent`,但具有专门的行为:
- **NodeComponent**:标准同步组件(`process()` 方法)
- **NodeBooleanComponent**:为 IF 条件返回 boolean`processBoolean()`
- **NodeSwitchComponent**:为 SWITCH 路由返回 string`processSwitch()`
- **NodeIteratorComponent**:为 ITERATOR 结构提供迭代逻辑
- **NodeForComponent**:控制 FOR 循环行为
- **ScriptComponent**基于脚本的组件Groovy、JS、Python 等)
**Declarative Component Pattern**: Any Spring bean can become a component without extending base classes by using `@LiteflowCmpDefine` (class-level, specifies `NodeTypeEnum`) and `@LiteflowMethod` (method-level, maps to `LiteFlowMethodEnum`). This avoids class hierarchy constraints.
**声明式组件模式**:任何 Spring bean 都可以在不继承基类的情况下成为组件,方式是使用 `@LiteflowCmpDefine`(类级别,指定 `NodeTypeEnum`)和 `@LiteflowMethod`(方法级别,映射到 `LiteFlowMethodEnum`)。这样可以摆脱类继承层级的约束。
**Component Lifecycle Hooks** (override in NodeComponent subclasses or via `@LiteflowMethod`):
- `isAccess()` gate check before execution; return `false` to skip
- `beforeProcess()` / `afterProcess()` pre/post hooks per component
- `onSuccess()` / `onError()` outcome callbacks
- `isContinueOnError()` whether WHEN continues if this node fails
- `isEnd()` signals chain should stop after this node
- `rollback()` called in reverse order on failure
**组件生命周期钩子**(在 NodeComponent 子类中覆写,或通过 `@LiteflowMethod` 实现):
- `isAccess()` 执行前的准入检查;返回 `false` 则跳过
- `beforeProcess()` / `afterProcess()` 每个组件的前/后置钩子
- `onSuccess()` / `onError()` 结果回调
- `isContinueOnError()` 当该节点失败时 WHEN 是否继续
- `isEnd()` 标记该节点之后应停止 chain
- `rollback()` 失败时按逆序调用
**`@FallbackCmp`**: Annotate a fallback component that activates when the primary component is not found or throws.
**`@FallbackCmp`**:标注一个兜底组件,当主组件不存在或抛异常时启用。
#### 4. Slot-Based Context
Thread-safe execution context:
- `DataBus.offerSlot(chainId)` acquires a slot from pool
- Slot contains execution metadata, context beans, and step tracking
- `DataBus.releaseSlot(slotIndex)` returns slot to pool
- Pool size configurable via `slotSize` property
#### 4. 基于 Slot 的上下文
线程安全的执行上下文:
- `DataBus.offerSlot(chainId)` 从池中获取一个 slot
- Slot 包含执行元数据、上下文 bean 和步骤跟踪
- `DataBus.releaseSlot(slotIndex)` 将 slot 归还到池
- 池大小通过 `slotSize` 属性配置
#### 5. Parse Mode Strategies
Three modes (`ParseModeEnum`):
- **PARSE_ALL_ON_START**: Parse all chains at startup (default)
- **PARSE_ONE_ON_FIRST_EXEC**: Lazy parse each chain on first use (faster startup)
- **PARSE_ALL_ON_FIRST_EXEC**: Parse all chains on first any chain execution
#### 5. 解析模式策略
三种模式(`ParseModeEnum`
- **PARSE_ALL_ON_START**:启动时解析所有 chain默认
- **PARSE_ONE_ON_FIRST_EXEC**:每个 chain 在首次使用时惰性解析(启动更快)
- **PARSE_ALL_ON_FIRST_EXEC**:在任意 chain 首次执行时解析全部 chain
### Module Structure
### 模块结构
#### Core Modules
- **liteflow-core**: Core engine (FlowExecutor, FlowBus, DataBus, Slot, Condition system, component model)
- **liteflow-el-builder**: Programmatic chain builder API using QLExpress
#### 核心模块
- **liteflow-core**:核心引擎(FlowExecutorFlowBusDataBusSlotCondition 系统、组件模型)
- **liteflow-el-builder**:基于 QLExpress 的编程式 chain 构建器 API
#### Rule Source Plugins (6 implementations in `liteflow-rule-plugin/`)
- **liteflow-rule-zk**: ZooKeeper configuration source
- **liteflow-rule-sql**: SQL database configuration source
- **liteflow-rule-nacos**: Nacos configuration center
- **liteflow-rule-etcd**: etcd configuration source
- **liteflow-rule-apollo**: Apollo configuration center
- **liteflow-rule-redis**: Redis configuration source
#### 规则源插件(`liteflow-rule-plugin/` 下 6 种实现)
- **liteflow-rule-zk**ZooKeeper 配置源
- **liteflow-rule-sql**SQL 数据库配置源
- **liteflow-rule-nacos**Nacos 配置中心
- **liteflow-rule-etcd**etcd 配置源
- **liteflow-rule-apollo**Apollo 配置中心
- **liteflow-rule-redis**Redis 配置源
#### Script Plugins (11 languages in `liteflow-script-plugin/`)
- **liteflow-script-groovy**: Groovy scripting
- **liteflow-script-javascript**: Rhino JavaScript (JSR223)
- **liteflow-script-graaljs**: GraalVM JavaScript
- **liteflow-script-qlexpress**: Alibaba QLExpress
- **liteflow-script-python**: Jython (Python on JVM)
- **liteflow-script-lua**: LuaJ
- **liteflow-script-aviator**: Aviator expression language
- **liteflow-script-java**: Janino (Java compiler)
- **liteflow-script-javax**: JSR223 standard Java compiler
- **liteflow-script-javax-pro**: Liquor-based Java compiler (enhanced)
- **liteflow-script-kotlin**: Kotlin scripting
#### 脚本插件(`liteflow-script-plugin/` 下 11 种语言)
- **liteflow-script-groovy**Groovy 脚本
- **liteflow-script-javascript**Rhino JavaScriptJSR223
- **liteflow-script-graaljs**GraalVM JavaScript
- **liteflow-script-qlexpress**:阿里 QLExpress
- **liteflow-script-python**JythonJVM 上的 Python
- **liteflow-script-lua**LuaJ
- **liteflow-script-aviator**Aviator 表达式语言
- **liteflow-script-java**JaninoJava 编译器)
- **liteflow-script-javax**JSR223 标准 Java 编译器
- **liteflow-script-javax-pro**:基于 Liquor 的 Java 编译器(增强版)
- **liteflow-script-kotlin**Kotlin 脚本
#### Framework Integration
- **liteflow-spring**: Spring framework integration (component scanning, bean lifecycle, AOP)
- **liteflow-spring-boot-starter**: Spring Boot auto-configuration with `@ConfigurationProperties`
- **liteflow-solon-plugin**: Solon framework integration (lightweight alternative to Spring)
#### 框架集成
- **liteflow-spring**Spring 框架集成组件扫描、bean 生命周期、AOP
- **liteflow-spring-boot-starter**Spring Boot 2/3 自动配置,使用 `@ConfigurationProperties`
- **liteflow-spring-boot4-starter**Spring Boot 4 starter仅 JDK 17+ —— 在 `compile-17+` profile 下构建)
- **liteflow-solon-plugin**Solon 框架集成Spring 的轻量替代方案)
#### Testing Infrastructure
30+ test modules in `liteflow-testcase-el/` organized by:
1. **Framework**: springboot, springnative, solon, nospring
2. **Config Sources**: zk, nacos, etcd, apollo, redis, sql
3. **Scripts**: One module per language + multi-language scenarios
4. **Features**: builder, declare, routechain, monitoring, timeout, etc.
#### ReAct Agent`liteflow-react-agent/`,仅 JDK 17+
让一个 LLM ReAct agent由 agentscope-java 驱动)作为普通 LiteFlow 节点编排进 EL 链路。这是一个聚合模块,包含一个 core 模块 + 每个模型供应商各一个模块:
- **liteflow-react-agent-core**`ReActAgentComponent`(一个 `process()``final``NodeComponent`,通过 `model()``systemPrompt()``userPrompt()``tools()``handleReply()` 等受保护钩子定制行为)、`ModelSpec` 凭据/模型抽象、conversation/agentKey **会话模型**`AgentSessionManager`)、可插拔的 memory 持久化(通过 `AgentSessionFactory` SPI 支持 JVM/NONE/LOCAL_FILE/REDIS/MYSQL、桥接为 LiteFlow `FlowEvent` 的流式事件、workspace 文件工具,以及受管 shell 工具。
- **liteflow-react-agent-openai / -anthropic / -gemini / -dashscope**:各供应商的入口类(如 `OpenAI``DeepSeek``Kimi``Anthropic``Gemini``DashScope`),返回对应供应商的 `ModelSpec` 子类型。业务应用通常只依赖其中一个供应商模块(每个都会传递依赖 `-core`)。
Test pattern:
两层标识:`conversationId`(业务/对话维度,决定 workspace 子目录,整条 chain 内一致)和 `agentKey`(组件维度,默认取 `nodeId`,隔离各 agent 的 memory。**完整使用指南:`docs/liteflow-react-agent-guide.md`** —— 修改 agent 行为前请查阅它,不要在此处复制其配置表格。
#### 测试基础设施
`liteflow-testcase-el/` 下有 30+ 个测试模块,按以下维度组织:
1. **框架**springboot、springnative、solon、nospring
2. **配置源**zk、nacos、etcd、apollo、redis、sql
3. **脚本**:每种语言一个模块 + 多语言混合场景
4. **特性**builder、declare、routechain、monitoring、timeout 等
测试范式:
```java
@SpringBootTest
@TestPropertySource(value = "classpath:/application.properties")
@@ -180,58 +195,58 @@ public class MyTest {
}
```
### Important Design Patterns
### 重要设计模式
#### SPI Pattern for Extensibility
Used extensively for:
- `ContextAware`: Framework abstraction (Spring vs non-Spring)
- `PathContentParser`: Custom file path resolution
- `CmpAroundAspect`: Global component lifecycle hooks
- `DeclComponentParser`: Declarative component parsing
- Script executors for each language
#### 用于扩展的 SPI 模式
广泛用于:
- `ContextAware`:框架抽象(Spring vs Spring
- `PathContentParser`:自定义文件路径解析
- `CmpAroundAspect`:全局组件生命周期钩子
- `DeclComponentParser`:声明式组件解析
- 各语言的脚本执行器
#### Lifecycle Hooks
Multiple extension points:
- `PostProcessChainBuildLifeCycle`: Before/after chain building
- `PostProcessNodeBuildLifeCycle`: Before/after node building
- `PostProcessChainExecuteLifeCycle`: Before/after chain execution
- `PostProcessFlowExecuteLifeCycle`: Before/after flow execution
#### 生命周期钩子
多个扩展点:
- `PostProcessChainBuildLifeCycle`chain 构建前/后
- `PostProcessNodeBuildLifeCycle`node 构建前/后
- `PostProcessChainExecuteLifeCycle`chain 执行前/后
- `PostProcessFlowExecuteLifeCycle`flow 执行前/后
#### Rollback Mechanism
Components implement `rollback()` for automatic rollback on failure (executed in reverse order via `executeSteps` deque).
#### 回滚机制
组件实现 `rollback()` 以在失败时自动回滚(通过 `executeSteps` deque 按逆序执行)。
#### Hot Reload Support
- `MonitorFile` watches rule files for changes
- `reloadRule()` refreshes without restart
- Copy-on-write collections (unless `fastLoad=true`) prevent concurrent modification
#### 热重载支持
- `MonitorFile` 监听规则文件变化
- `reloadRule()` 无需重启即可刷新
- Copy-on-write 集合(除非 `fastLoad=true`)防止并发修改
#### Metadata Caching
- `FlowBus` uses CopyOnWriteHashMap (or regular HashMap with `fastLoad`)
- EL MD5 caching for expression reuse
- Chain caching configurable (`chainCacheEnabled`, `chainCacheCapacity`)
#### 元数据缓存
- `FlowBus` 使用 CopyOnWriteHashMap(或在 `fastLoad` 下使用普通 HashMap
- EL MD5 缓存以复用表达式
- chain 缓存可配置(`chainCacheEnabled``chainCacheCapacity`
### Critical Configuration (`LiteflowConfig`)
- **ruleSource**: Rule file locations (supports XML, JSON, YML)
- **parseMode**: Parsing strategy (affects startup performance)
- **slotSize**: Context slot pool size
- **enableMonitorFile**: Hot reload of rule files
- **supportMultipleType**: Mix XML/JSON/YML rules
- **whenMaxWaitTime**: Timeout for WHEN parallel execution
- **fastLoad**: Disable CopyOnWrite for faster startup
- **enableVirtualThread**: Use virtual threads (JDK 21+)
### 关键配置(`LiteflowConfig`
- **ruleSource**:规则文件位置(支持 XMLJSONYML
- **parseMode**:解析策略(影响启动性能)
- **slotSize**:上下文 slot 池大小
- **enableMonitorFile**:规则文件热重载
- **supportMultipleType**:混用 XML/JSON/YML 规则
- **whenMaxWaitTime**WHEN 并行执行的超时时间
- **fastLoad**:关闭 CopyOnWrite 以加快启动
- **enableVirtualThread**:使用虚拟线程(JDK 21+
### Key File Locations
- Core engine: `liteflow-core/src/main/java/com/yomahub/liteflow/flow/`
- FlowExecutor: `liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java`
- FlowBus: `liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java`
- Component base: `liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java`
- Condition system: `liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/`
- EL parser: `liteflow-core/src/main/java/com/yomahub/liteflow/parser/el/`
### 关键文件位置
- 核心引擎:`liteflow-core/src/main/java/com/yomahub/liteflow/flow/`
- FlowExecutor`liteflow-core/src/main/java/com/yomahub/liteflow/core/FlowExecutor.java`
- FlowBus`liteflow-core/src/main/java/com/yomahub/liteflow/flow/FlowBus.java`
- 组件基类:`liteflow-core/src/main/java/com/yomahub/liteflow/core/NodeComponent.java`
- Condition 系统:`liteflow-core/src/main/java/com/yomahub/liteflow/flow/element/condition/`
- EL 解析器:`liteflow-core/src/main/java/com/yomahub/liteflow/parser/el/`
### Important Conventions
- **Naming**: Components identified by `nodeId`, chains by `chainId`
- **Thread Safety**: Extensive ThreadLocal and concurrent collections
- **Fail-Fast**: Validation at parse time with detailed error messages
- **Fluent APIs**: Builder pattern for chain construction (EL builder)
- **Namespaces**: Chains can be organized into namespaces
- **Versioning**: Uses `${revision}` placeholder (currently 2.15.3) via flatten-maven-plugin
### 重要约定
- **命名**:组件以 `nodeId` 标识,chain `chainId` 标识
- **线程安全**:大量使用 ThreadLocal 和并发集合
- **快速失败**:解析期校验,并给出详细错误信息
- **流式 API**chain 构建采用 builder 模式(EL builder
- **命名空间**chain 可组织进命名空间
- **版本管理**:通过 flatten-maven-plugin 使用 `${revision}` 占位符(当前 2.16.0