|
| 1 | +package cn.fxbin.bubble.ai.lightrag.autoconfigure; |
| 2 | + |
| 3 | +import cn.fxbin.bubble.core.support.YamlConfigFactory; |
| 4 | +import cn.hutool.core.date.SystemClock; |
| 5 | +import com.dtflys.forest.http.ForestRequest; |
| 6 | +import com.dtflys.forest.http.ForestResponse; |
| 7 | +import com.dtflys.forest.interceptor.ForestInterceptor; |
| 8 | +import com.dtflys.forest.springboot.annotation.ForestScan; |
| 9 | +import jakarta.annotation.PostConstruct; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 13 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 14 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 15 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 16 | +import org.springframework.context.annotation.Bean; |
| 17 | +import org.springframework.context.annotation.Configuration; |
| 18 | +import org.springframework.context.annotation.PropertySource; |
| 19 | +import org.springframework.util.StringUtils; |
| 20 | + |
| 21 | +import java.time.Duration; |
| 22 | + |
| 23 | +/** |
| 24 | + * LightRAG 自动配置类 |
| 25 | + * |
| 26 | + * <p>该类负责自动配置 LightRAG 相关的 Bean,包括 Forest HTTP 客户端的配置、 |
| 27 | + * 认证拦截器、超时设置、重试策略等。当满足特定条件时,会自动创建和配置相关组件。</p> |
| 28 | + * |
| 29 | + * <h3>自动配置条件:</h3> |
| 30 | + * <ul> |
| 31 | + * <li>类路径中存在 Forest 相关类</li> |
| 32 | + * <li>配置属性 dm.ai.lightrag.enabled=true(默认为 true)</li> |
| 33 | + * <li>配置了有效的 base-url</li> |
| 34 | + * </ul> |
| 35 | + * |
| 36 | + * <h3>主要功能:</h3> |
| 37 | + * <ul> |
| 38 | + * <li>配置 Forest HTTP 客户端的全局设置</li> |
| 39 | + * <li>创建认证拦截器,支持API Key认证</li> |
| 40 | + * <li>配置超时和重试参数</li> |
| 41 | + * <li>提供 LightRAG 客户端 Bean 的自动装配</li> |
| 42 | + * </ul> |
| 43 | + * |
| 44 | + * @author fxbin |
| 45 | + * @version v1.0 |
| 46 | + * @since 2025-08-21 15:22:03 |
| 47 | + */ |
| 48 | +@Slf4j |
| 49 | +@Configuration( |
| 50 | + proxyBeanMethods = false |
| 51 | +) |
| 52 | +@PropertySource(value = "classpath:lightrag-forest.yaml", factory = YamlConfigFactory.class) |
| 53 | +@RequiredArgsConstructor |
| 54 | +@ForestScan(basePackages = "cn.fxbin.bubble.ai.lightrag.client") |
| 55 | +@ConditionalOnClass(com.dtflys.forest.Forest.class) |
| 56 | +@EnableConfigurationProperties(LightRagProperties.class) |
| 57 | +@ConditionalOnProperty(prefix = "bubble.ai.lightrag", name = "enabled", havingValue = "true", matchIfMissing = true) |
| 58 | +public class LightRagAutoConfiguration { |
| 59 | + |
| 60 | + private final LightRagProperties properties; |
| 61 | + |
| 62 | + /** |
| 63 | + * 配置 Forest 全局属性 |
| 64 | + * |
| 65 | + * <p>通过 Spring Boot 配置属性的方式配置 Forest 全局参数。 |
| 66 | + * 使用 @ForestScan 注解后,Forest 会自动读取这些配置。</p> |
| 67 | + */ |
| 68 | + @PostConstruct |
| 69 | + public void configureForest() { |
| 70 | + log.info("正在配置 LightRAG Forest 客户端,基础 URL: {}", properties.getBaseUrl()); |
| 71 | + log.info("LightRAG Forest 客户端配置完成,使用 @ForestScan 自动扫描客户端接口"); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 创建认证拦截器 |
| 76 | + * |
| 77 | + * <p>创建API Key认证拦截器,支持通过API Key进行认证。</p> |
| 78 | + * |
| 79 | + * @return 认证拦截器 |
| 80 | + */ |
| 81 | + @Bean |
| 82 | + @ConditionalOnMissingBean(name = "lightRagAuthInterceptor") |
| 83 | + public ForestInterceptor lightRagAuthInterceptor() { |
| 84 | + return new LightRagAuthInterceptor(properties); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 创建日志拦截器 |
| 89 | + * |
| 90 | + * <p>用于记录 HTTP 请求和响应的详细信息,便于调试和监控。</p> |
| 91 | + * |
| 92 | + * @return 日志拦截器 |
| 93 | + */ |
| 94 | + @Bean |
| 95 | + @ConditionalOnMissingBean(name = "lightRagLoggingInterceptor") |
| 96 | + public ForestInterceptor lightRagLoggingInterceptor() { |
| 97 | + return new LightRagLoggingInterceptor(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * LightRAG 认证拦截器 |
| 102 | + * |
| 103 | + * <p>实现API Key认证的 HTTP 请求拦截和认证信息添加。</p> |
| 104 | + */ |
| 105 | + @RequiredArgsConstructor |
| 106 | + public static class LightRagAuthInterceptor implements ForestInterceptor { |
| 107 | + |
| 108 | + private final LightRagProperties properties; |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean beforeExecute(ForestRequest request) { |
| 112 | + String apiKey = properties.getApiKey(); |
| 113 | + if (StringUtils.hasText(apiKey)) { |
| 114 | + // Add API key as query parameter |
| 115 | + request.addQuery("api_key_header_value", apiKey); |
| 116 | + } |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * LightRAG 日志拦截器 |
| 123 | + * |
| 124 | + * <p>记录 HTTP 请求和响应的详细信息,包括请求 URL、方法、参数、响应状态码、耗时等。</p> |
| 125 | + */ |
| 126 | + public static class LightRagLoggingInterceptor implements ForestInterceptor { |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean beforeExecute(ForestRequest request) { |
| 130 | + if (log.isDebugEnabled()) { |
| 131 | + log.debug("LightRAG 请求开始: {} {}", request.getType().getName(), request.getUrl()); |
| 132 | + if (request.getQuery() != null && !request.getQuery().isEmpty()) { |
| 133 | + log.debug("请求参数: {}", request.getQuery()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // 记录请求开始时间 |
| 138 | + request.addAttachment("startTime", SystemClock.now()); |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void afterExecute(ForestRequest request, ForestResponse response) { |
| 144 | + if (log.isDebugEnabled()) { |
| 145 | + log.debug("LightRAG 请求结束: {} {} - 状态码: {}", |
| 146 | + request.getType().getName(), request.getUrl(), response.getStatusCode()); |
| 147 | + if (response.getContent() != null) { |
| 148 | + log.debug("响应内容: {}", response.getContent()); |
| 149 | + } |
| 150 | + } |
| 151 | + log.debug("请求耗时: {}ms", SystemClock.now() - (Long) request.getAttachment("startTime")); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * 内部配置类 |
| 157 | + * |
| 158 | + * <p>包含需要在特定条件下才创建的 Bean 配置。</p> |
| 159 | + */ |
| 160 | + @Configuration |
| 161 | + @ConditionalOnProperty(prefix = "dm.ai.lightrag", name = "base-url") |
| 162 | + static class LightRagClientConfiguration { |
| 163 | + |
| 164 | + /** |
| 165 | + * 验证配置的有效性 |
| 166 | + * |
| 167 | + * <p>在应用启动时验证 LightRAG 配置是否正确,如果配置无效则记录警告信息。</p> |
| 168 | + * |
| 169 | + * @param properties LightRAG 配置属性 |
| 170 | + */ |
| 171 | + @Bean |
| 172 | + @ConditionalOnMissingBean |
| 173 | + public LightRagConfigurationValidator lightRagConfigurationValidator(LightRagProperties properties) { |
| 174 | + return new LightRagConfigurationValidator(properties); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * LightRAG 配置验证器 |
| 180 | + * |
| 181 | + * <p>用于验证 LightRAG 配置的有效性,确保必要的配置项都已正确设置。</p> |
| 182 | + */ |
| 183 | + @RequiredArgsConstructor |
| 184 | + public static class LightRagConfigurationValidator { |
| 185 | + |
| 186 | + private final LightRagProperties properties; |
| 187 | + |
| 188 | + @PostConstruct |
| 189 | + public void validate() { |
| 190 | + log.info("开始验证 LightRAG 配置..."); |
| 191 | + |
| 192 | + // 验证基础 URL |
| 193 | + if (!StringUtils.hasText(properties.getBaseUrl())) { |
| 194 | + log.warn("LightRAG 基础 URL 未配置,服务可能无法正常工作"); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + String apiKey = properties.getApiKey(); |
| 199 | + if (!StringUtils.hasText(apiKey)) { |
| 200 | + log.warn("LightRAG 配置了 API Key 认证但未提供 apiKey"); |
| 201 | + } |
| 202 | + |
| 203 | + int maxRetries = properties.getMaxRetries(); |
| 204 | + if (maxRetries < 0) { |
| 205 | + log.warn("LightRAG 配置了无效的重试次数,将使用默认值 3"); |
| 206 | + } |
| 207 | + if (maxRetries > 5) { |
| 208 | + log.warn("LightRAG 配置了超过 5 次的重试次数,这可能会导致性能问题"); |
| 209 | + } |
| 210 | + |
| 211 | + Duration timeout = properties.getTimeout(); |
| 212 | + if (timeout.isNegative()) { |
| 213 | + log.warn("LightRAG 配置了无效的超时时间,将使用默认值 10 秒"); |
| 214 | + } |
| 215 | + |
| 216 | + log.info("LightRAG 配置验证完成"); |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments