Commit 5daacfc6 authored by 王舵's avatar 王舵

feature: 调整如下

- 增加启动 profile 区分环境
- fix: AgentToolManager中如果Tool 类是代理类则不能被 spring ai 正确识别,所以将传入 spring ai 中的Tool 类都降级为原始类
- 调整了data.sql, 默认启动不执行 scheam.sql
parent 53570949
...@@ -208,4 +208,5 @@ Thumbs.db ...@@ -208,4 +208,5 @@ Thumbs.db
.Trashes .Trashes
ehthumbs.db ehthumbs.db
Icon? Icon?
*.icon? *.icon?
\ No newline at end of file backend/src/main/resources/application-dev.yml
\ No newline at end of file
...@@ -309,7 +309,49 @@ ...@@ -309,7 +309,49 @@
</dependencies> </dependencies>
<profiles>
<!-- 开发环境(默认激活) -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- 默认激活dev -->
</activation>
<properties>
<!-- 定义环境标识,对应配置文件后缀 -->
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
<build> <build>
<resources>
<resource>
<!-- 配置文件所在目录 -->
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 关键:开启资源过滤 -->
<includes>
<include>**/*</include> <!-- 包含所有配置文件 -->
</includes>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -322,6 +364,13 @@ ...@@ -322,6 +364,13 @@
</exclude> </exclude>
</excludes> </excludes>
</configuration> </configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin> </plugin>
<!-- Maven Compiler Plugin --> <!-- Maven Compiler Plugin -->
......
...@@ -587,7 +587,8 @@ public class AgentChatService { ...@@ -587,7 +587,8 @@ public class AgentChatService {
// 这里只需要等待足够的时间让异步的onComplete回调执行完成 // 这里只需要等待足够的时间让异步的onComplete回调执行完成
try { try {
// 通过轮询检查是否已完成,最多等待5秒 // 通过轮询检查是否已完成,最多等待5秒
long maxWaitTime = 5000; // long maxWaitTime = 5000;
long maxWaitTime = 60000;
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
while (!isCompleted.get() && (System.currentTimeMillis() - startTime) < maxWaitTime) { while (!isCompleted.get() && (System.currentTimeMillis() - startTime) < maxWaitTime) {
Thread.sleep(100); // 每100ms检查一次 Thread.sleep(100); // 每100ms检查一次
......
package pangea.hiagent.core; package pangea.hiagent.core;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -19,100 +22,103 @@ import java.util.stream.Collectors; ...@@ -19,100 +22,103 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Service @Service
public class AgentToolManager { public class AgentToolManager {
@Autowired @Autowired
private pangea.hiagent.service.ToolService toolService; private pangea.hiagent.service.ToolService toolService;
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
/** /**
* 获取Agent可用的工具列表 * 获取Agent可用的工具列表
*
* @param agent Agent对象 * @param agent Agent对象
* @return 工具列表 * @return 工具列表
*/ */
public List<Tool> getAvailableTools(Agent agent) { public List<Tool> getAvailableTools(Agent agent) {
try { try {
log.info("获取Agent可用工具列表,Agent ID: {}, 名称: {}", agent.getId(), agent.getName()); log.info("获取Agent可用工具列表,Agent ID: {}, 名称: {}", agent.getId(), agent.getName());
// 获取Agent所有者的所有活跃工具 // 获取Agent所有者的所有活跃工具
List<Tool> allTools = toolService.getUserToolsByStatus(agent.getOwner(), "active"); List<Tool> allTools = toolService.getUserToolsByStatus(agent.getOwner(), "active");
log.info("用户所有活跃工具数量: {}", allTools != null ? allTools.size() : 0); log.info("用户所有活跃工具数量: {}", allTools != null ? allTools.size() : 0);
if (allTools == null || allTools.isEmpty()) { if (allTools == null || allTools.isEmpty()) {
log.warn("Agent: {} 没有配置可用的工具", agent.getId()); log.warn("Agent: {} 没有配置可用的工具", agent.getId());
return List.of(); return List.of();
} }
// 如果Agent配置了特定的工具列表,则只返回配置的工具 // 如果Agent配置了特定的工具列表,则只返回配置的工具
List<String> toolNames = agent.getToolNames(); List<String> toolNames = agent.getToolNames();
log.info("Agent配置的工具名称列表: {}", toolNames); log.info("Agent配置的工具名称列表: {}", toolNames);
if (toolNames != null && !toolNames.isEmpty()) { if (toolNames != null && !toolNames.isEmpty()) {
// 根据工具名称筛选工具 // 根据工具名称筛选工具
List<Tool> filteredTools = filterToolsByName(allTools, toolNames); List<Tool> filteredTools = filterToolsByName(allTools, toolNames);
log.info("筛选后的工具数量: {}", filteredTools.size()); log.info("筛选后的工具数量: {}", filteredTools.size());
return filteredTools; return filteredTools;
} }
return allTools; return allTools;
} catch (Exception e) { } catch (Exception e) {
log.error("获取Agent可用工具时发生错误", e); log.error("获取Agent可用工具时发生错误", e);
return List.of(); return List.of();
} }
} }
/** /**
* 根据工具名称筛选工具 * 根据工具名称筛选工具
* @param allTools 所有工具 *
* @param allTools 所有工具
* @param toolNames 工具名称列表 * @param toolNames 工具名称列表
* @return 筛选后的工具列表 * @return 筛选后的工具列表
*/ */
public List<Tool> filterToolsByName(List<Tool> allTools, List<String> toolNames) { public List<Tool> filterToolsByName(List<Tool> allTools, List<String> toolNames) {
return allTools.stream() return allTools.stream()
.filter(tool -> toolNames.contains(tool.getName())) .filter(tool -> toolNames.contains(tool.getName()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
/** /**
* 根据工具名称集合筛选工具实例(用于ReActService) * 根据工具名称集合筛选工具实例(用于ReActService)
* @param allTools 所有工具实例 *
* @param allTools 所有工具实例
* @param toolNames 工具名称集合 * @param toolNames 工具名称集合
* @return 筛选后的工具实例列表 * @return 筛选后的工具实例列表
*/ */
public List<Object> filterToolsByInstances(List<Object> allTools, Set<String> toolNames) { public List<Object> filterToolsByInstances(List<Object> allTools, Set<String> toolNames) {
log.debug("开始筛选工具实例,工具名称集合: {}", toolNames); log.debug("开始筛选工具实例,工具名称集合: {}", toolNames);
if (toolNames == null || toolNames.isEmpty()) { if (toolNames == null || toolNames.isEmpty()) {
log.debug("工具名称集合为空,返回所有工具实例"); log.debug("工具名称集合为空,返回所有工具实例");
return allTools; return allTools;
} }
List<Object> filteredTools = allTools.stream() List<Object> filteredTools = allTools.stream()
.filter(tool -> { .filter(tool -> {
// 获取工具类名(不含包名) // 获取工具类名(不含包名)
String className = tool.getClass().getSimpleName(); String className = tool.getClass().getSimpleName();
log.debug("检查工具类: {}", className); log.debug("检查工具类: {}", className);
// 检查类名是否匹配 // 检查类名是否匹配
boolean isMatch = toolNames.contains(className) || boolean isMatch = toolNames.contains(className) ||
toolNames.stream().anyMatch(name -> toolNames.stream().anyMatch(name -> className.toLowerCase().contains(name.toLowerCase()));
className.toLowerCase().contains(name.toLowerCase()));
if (isMatch) {
if (isMatch) { log.debug("工具 {} 匹配成功", className);
log.debug("工具 {} 匹配成功", className); }
}
return isMatch;
return isMatch; })
}) .collect(Collectors.toList());
.collect(Collectors.toList());
log.debug("筛选完成,返回 {} 个工具实例", filteredTools.size()); log.debug("筛选完成,返回 {} 个工具实例", filteredTools.size());
return filteredTools; return filteredTools;
} }
/** /**
* 构建工具描述文本 * 构建工具描述文本
*
* @param tools 工具列表 * @param tools 工具列表
* @return 工具描述文本 * @return 工具描述文本
*/ */
...@@ -120,7 +126,7 @@ public class AgentToolManager { ...@@ -120,7 +126,7 @@ public class AgentToolManager {
if (tools.isEmpty()) { if (tools.isEmpty()) {
return "(暂无可用工具)"; return "(暂无可用工具)";
} }
StringBuilder description = new StringBuilder(); StringBuilder description = new StringBuilder();
for (int i = 0; i < tools.size(); i++) { for (int i = 0; i < tools.size(); i++) {
Tool tool = tools.get(i); Tool tool = tools.get(i);
...@@ -134,47 +140,58 @@ public class AgentToolManager { ...@@ -134,47 +140,58 @@ public class AgentToolManager {
} }
description.append("\n"); description.append("\n");
} }
return description.toString(); return description.toString();
} }
/** /**
* 检查字符串是否有值 * 检查字符串是否有值
*
* @param value 字符串值 * @param value 字符串值
* @return 是否有值 * @return 是否有值
*/ */
private boolean hasValue(String value) { private boolean hasValue(String value) {
return value != null && !value.isEmpty(); return value != null && !value.isEmpty();
} }
/** /**
* 根据Agent获取可用的工具实例 * 根据Agent获取可用的工具实例
*
* @param agent Agent对象 * @param agent Agent对象
* @return 工具实例列表 * @return 工具实例列表
*/ */
public List<Object> getAvailableToolInstances(Agent agent) { public List<Object> getAvailableToolInstances(Agent agent) {
// 获取Agent可用的工具定义 // 获取Agent可用的工具定义
List<Tool> availableTools = getAvailableTools(agent); List<Tool> availableTools = getAvailableTools(agent);
// 获取所有Spring管理的bean名称 // 获取所有Spring管理的bean名称
String[] beanNames = applicationContext.getBeanDefinitionNames(); String[] beanNames = applicationContext.getBeanDefinitionNames();
// 根据工具名称筛选对应的工具实例 // 根据工具名称筛选对应的工具实例
List<Object> toolInstances = new ArrayList<>(); List<Object> toolInstances = new ArrayList<>();
Set<String> availableToolNames = availableTools.stream() Set<String> availableToolNames = availableTools.stream()
.map(Tool::getName) .map(Tool::getName)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
for (String beanName : beanNames) { for (String beanName : beanNames) {
Object bean = applicationContext.getBean(beanName); Object bean = applicationContext.getBean(beanName);
String simpleClassName = bean.getClass().getSimpleName(); String simpleClassName = bean.getClass().getSimpleName();
// 判断是否是代理类,如果是代理类,获取目标类
if (AopUtils.isAopProxy(bean)) {
log.debug("beanName: {} 是代理类,尝试获取目标类", beanName);
try {
bean = ((Advised) bean).getTargetSource().getTarget();
simpleClassName = bean.getClass().getSimpleName();
} catch (Exception e) {
e.printStackTrace();
}
}
// 检查bean的类名是否与可用工具名称匹配 // 检查bean的类名是否与可用工具名称匹配
if (availableToolNames.contains(simpleClassName)) { if (availableToolNames.contains(simpleClassName)) {
toolInstances.add(bean); toolInstances.add(bean);
} }
} }
log.info("智能体{}获取到的工具实例数量: {}", agent.getName(), toolInstances.size()); log.info("智能体{}获取到的工具实例数量: {}", agent.getName(), toolInstances.size());
return toolInstances; return toolInstances;
......
...@@ -22,7 +22,8 @@ spring: ...@@ -22,7 +22,8 @@ spring:
sql: sql:
init: init:
schema-locations: classpath:schema.sql schema-locations: classpath:schema.sql
mode: always mode: never
# mode: always
# JPA/Hibernate配置 # JPA/Hibernate配置
jpa: jpa:
......
This diff is collapsed.
#!/bin/bash
###
# @Date: 2025-12-19 08:44:46
# @LastEditors: wangduo3 wangduo3@hisense.com
# @LastEditTime: 2025-12-19 14:39:27
# @FilePath: /pangea-agent/run-backend-with-env.sh
###
# 提取参数
ENV_PROFILE=$1
# 拼接启动命令(注意双引号保留参数格式,避免空格/特殊字符问题)
RUN_CMD="mvn spring-boot:run -P${ENV_PROFILE} -Dspring-boot.run.arguments=--spring.profiles.active=${ENV_PROFILE}"
cd backend
# 打印并执行命令
echo "执行启动命令:$RUN_CMD"
eval $RUN_CMD
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment