diff --git a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentFactory.java b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentFactory.java index d443416a1..a885873b9 100644 --- a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentFactory.java +++ b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentFactory.java @@ -64,9 +64,6 @@ public class AIComponentFactory { * @return NodeComponent实例,如果不是AI组件则返回null */ public NodeComponent createAIComponent(Class interfaceClass, String beanName) { - LOG.info("Attempting to create AI component for interface: {}, beanName: {}", - interfaceClass.getName(), beanName); - // 检查是否是AI组件 AIComponent aiComponent = interfaceClass.getAnnotation(AIComponent.class); if (Objects.isNull(aiComponent)) { diff --git a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentPostProcessor.java b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentPostProcessor.java index a95e57ecc..5f2294a09 100644 --- a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentPostProcessor.java +++ b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/AIComponentPostProcessor.java @@ -1,14 +1,14 @@ package com.yomahub.liteflow.ai.proxy; +import cn.hutool.core.util.StrUtil; import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.log.LFLog; import com.yomahub.liteflow.log.LFLoggerManager; +import com.yomahub.liteflow.process.holder.SpringNodeIdHolder; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.Ordered; -import java.util.Objects; - /** * AI组件后置处理器 * @@ -38,16 +38,12 @@ public class AIComponentPostProcessor implements BeanPostProcessor, Ordered { // 使用工厂创建AI组件 NodeComponent aiComponent = aiComponentFactory.createAIComponent(clazz, beanName); - if (Objects.nonNull(aiComponent)) { - LOG.info("Successfully created AI component for interface: {}, replacing bean: {}", - clazz.getName(), beanName); - return aiComponent; - } else { - LOG.warn("Failed to create AI component for interface: {}, returning original bean", - clazz.getName()); - return bean; - } + LOG.info("AI proxy component[{}] has been created for interface: {}", beanName, clazz.getName()); + String nodeId = StrUtil.isNotBlank(aiComponent.getNodeId()) ? aiComponent.getNodeId() : SpringNodeIdHolder.getRealBeanName(clazz, beanName); + SpringNodeIdHolder.add(nodeId); + + return aiComponent; } catch (Exception e) { LOG.error("Error creating AI component for interface: {}, beanName: {}", clazz.getName(), beanName, e); diff --git a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/handler/AbstractAIComponentHandler.java b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/handler/AbstractAIComponentHandler.java index 55bf7b32f..c2c7129c7 100644 --- a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/handler/AbstractAIComponentHandler.java +++ b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/proxy/handler/AbstractAIComponentHandler.java @@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil; import com.yomahub.liteflow.ai.annotation.AIComponent; import com.yomahub.liteflow.ai.enums.AITypeEnum; import com.yomahub.liteflow.ai.proxy.wrap.AIProxyWrapBean; +import com.yomahub.liteflow.ai.util.SetUtil; import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.exception.ProxyException; import com.yomahub.liteflow.log.LFLog; @@ -136,8 +137,8 @@ public abstract class AbstractAIComponentHandler { .getLoaded() .newInstance(); - nodeComponent.setNodeId(wrapBean.getNodeId()); - nodeComponent.setName(wrapBean.getNodeName()); + SetUtil.setIfPresent(nodeComponent::setNodeId, wrapBean.getNodeId()); + SetUtil.setIfPresent(nodeComponent::setName, wrapBean.getNodeName()); LOG.info("Created AI component: {}, beanName: {}, type: {}", nodeComponent.getNodeId(), wrapBean.getBeanName(), aiType); diff --git a/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/util/SetUtil.java b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/util/SetUtil.java new file mode 100644 index 000000000..4d3d7d090 --- /dev/null +++ b/liteflow-ai/liteflow-ai-core/src/main/java/com/yomahub/liteflow/ai/util/SetUtil.java @@ -0,0 +1,56 @@ +package com.yomahub.liteflow.ai.util; + +import java.lang.reflect.Array; +import java.util.Collection; +import java.util.Map; +import java.util.Objects; +import java.util.function.Consumer; + +/** + * TODO + * + * @author 苍镜月 + * @since TODO + */ + +public class SetUtil { + + /** + * 如果给定的值不为 null 或 "空" 或 默认值,则调用消费者。 + * + * @param consumer 要执行的操作。 + * @param value 要检查的值。 + * @param 值的类型。 + */ + public static void setIfPresent(Consumer consumer, T value) { + if (!isNullOrEmptyOrDefault(value)) { + consumer.accept(value); + } + } + + /** + * 检查给定的对象是否为 null 或 "空" 或 默认值 + * 如果一个对象是 String、Collection、Map 或数组,且其元素个数或长度为零, + * 那么它被认为是"空"的。 + * 如果一个对象是 TriState 类型,并且其值为 UNSET,那么认为它是默认值 + * + * @param value 要检查的对象。 + * @return 如果对象为 null 或 “空” 或 默认值,则返回 true,否则返回 false。 + */ + private static boolean isNullOrEmptyOrDefault(Object value) { + if (Objects.isNull(value)) { + return true; + } else if (value instanceof Collection) { + return ((Collection) value).isEmpty(); + } else if (value instanceof Map) { + return ((Map) value).isEmpty(); + } else if (value instanceof String) { + return ((String) value).trim().isEmpty(); + } else if (value.getClass().isArray()) { + return Array.getLength(value) == 0; + } else if (value instanceof TriState) { + return value == TriState.UNSET; + } + return false; + } +}