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
.Trashes
ehthumbs.db
Icon?
*.icon?
\ No newline at end of file
*.icon?
backend/src/main/resources/application-dev.yml
\ No newline at end of file
......@@ -309,7 +309,49 @@
</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>
<resources>
<resource>
<!-- 配置文件所在目录 -->
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 关键:开启资源过滤 -->
<includes>
<include>**/*</include> <!-- 包含所有配置文件 -->
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
......@@ -322,6 +364,13 @@
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Maven Compiler Plugin -->
......
......@@ -587,7 +587,8 @@ public class AgentChatService {
// 这里只需要等待足够的时间让异步的onComplete回调执行完成
try {
// 通过轮询检查是否已完成,最多等待5秒
long maxWaitTime = 5000;
// long maxWaitTime = 5000;
long maxWaitTime = 60000;
long startTime = System.currentTimeMillis();
while (!isCompleted.get() && (System.currentTimeMillis() - startTime) < maxWaitTime) {
Thread.sleep(100); // 每100ms检查一次
......
package pangea.hiagent.core;
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.context.ApplicationContext;
import org.springframework.stereotype.Service;
......@@ -19,100 +22,103 @@ import java.util.stream.Collectors;
@Slf4j
@Service
public class AgentToolManager {
@Autowired
private pangea.hiagent.service.ToolService toolService;
@Autowired
private ApplicationContext applicationContext;
/**
* 获取Agent可用的工具列表
*
* @param agent Agent对象
* @return 工具列表
*/
public List<Tool> getAvailableTools(Agent agent) {
try {
log.info("获取Agent可用工具列表,Agent ID: {}, 名称: {}", agent.getId(), agent.getName());
// 获取Agent所有者的所有活跃工具
List<Tool> allTools = toolService.getUserToolsByStatus(agent.getOwner(), "active");
log.info("用户所有活跃工具数量: {}", allTools != null ? allTools.size() : 0);
if (allTools == null || allTools.isEmpty()) {
log.warn("Agent: {} 没有配置可用的工具", agent.getId());
return List.of();
}
// 如果Agent配置了特定的工具列表,则只返回配置的工具
List<String> toolNames = agent.getToolNames();
log.info("Agent配置的工具名称列表: {}", toolNames);
if (toolNames != null && !toolNames.isEmpty()) {
// 根据工具名称筛选工具
List<Tool> filteredTools = filterToolsByName(allTools, toolNames);
log.info("筛选后的工具数量: {}", filteredTools.size());
return filteredTools;
}
return allTools;
} catch (Exception e) {
log.error("获取Agent可用工具时发生错误", e);
return List.of();
}
}
/**
* 根据工具名称筛选工具
* @param allTools 所有工具
*
* @param allTools 所有工具
* @param toolNames 工具名称列表
* @return 筛选后的工具列表
*/
public List<Tool> filterToolsByName(List<Tool> allTools, List<String> toolNames) {
return allTools.stream()
.filter(tool -> toolNames.contains(tool.getName()))
.collect(Collectors.toList());
.filter(tool -> toolNames.contains(tool.getName()))
.collect(Collectors.toList());
}
/**
* 根据工具名称集合筛选工具实例(用于ReActService)
* @param allTools 所有工具实例
*
* @param allTools 所有工具实例
* @param toolNames 工具名称集合
* @return 筛选后的工具实例列表
*/
public List<Object> filterToolsByInstances(List<Object> allTools, Set<String> toolNames) {
log.debug("开始筛选工具实例,工具名称集合: {}", toolNames);
if (toolNames == null || toolNames.isEmpty()) {
log.debug("工具名称集合为空,返回所有工具实例");
return allTools;
}
List<Object> filteredTools = allTools.stream()
.filter(tool -> {
// 获取工具类名(不含包名)
String className = tool.getClass().getSimpleName();
log.debug("检查工具类: {}", className);
// 检查类名是否匹配
boolean isMatch = toolNames.contains(className) ||
toolNames.stream().anyMatch(name ->
className.toLowerCase().contains(name.toLowerCase()));
if (isMatch) {
log.debug("工具 {} 匹配成功", className);
}
return isMatch;
})
.collect(Collectors.toList());
.filter(tool -> {
// 获取工具类名(不含包名)
String className = tool.getClass().getSimpleName();
log.debug("检查工具类: {}", className);
// 检查类名是否匹配
boolean isMatch = toolNames.contains(className) ||
toolNames.stream().anyMatch(name -> className.toLowerCase().contains(name.toLowerCase()));
if (isMatch) {
log.debug("工具 {} 匹配成功", className);
}
return isMatch;
})
.collect(Collectors.toList());
log.debug("筛选完成,返回 {} 个工具实例", filteredTools.size());
return filteredTools;
}
/**
* 构建工具描述文本
*
* @param tools 工具列表
* @return 工具描述文本
*/
......@@ -120,7 +126,7 @@ public class AgentToolManager {
if (tools.isEmpty()) {
return "(暂无可用工具)";
}
StringBuilder description = new StringBuilder();
for (int i = 0; i < tools.size(); i++) {
Tool tool = tools.get(i);
......@@ -134,47 +140,58 @@ public class AgentToolManager {
}
description.append("\n");
}
return description.toString();
}
/**
* 检查字符串是否有值
*
* @param value 字符串值
* @return 是否有值
*/
private boolean hasValue(String value) {
return value != null && !value.isEmpty();
}
/**
* 根据Agent获取可用的工具实例
*
* @param agent Agent对象
* @return 工具实例列表
*/
public List<Object> getAvailableToolInstances(Agent agent) {
// 获取Agent可用的工具定义
List<Tool> availableTools = getAvailableTools(agent);
// 获取所有Spring管理的bean名称
String[] beanNames = applicationContext.getBeanDefinitionNames();
// 根据工具名称筛选对应的工具实例
List<Object> toolInstances = new ArrayList<>();
Set<String> availableToolNames = availableTools.stream()
.map(Tool::getName)
.collect(Collectors.toSet());
.map(Tool::getName)
.collect(Collectors.toSet());
for (String beanName : beanNames) {
Object bean = applicationContext.getBean(beanName);
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的类名是否与可用工具名称匹配
if (availableToolNames.contains(simpleClassName)) {
toolInstances.add(bean);
}
}
log.info("智能体{}获取到的工具实例数量: {}", agent.getName(), toolInstances.size());
return toolInstances;
......
......@@ -22,7 +22,8 @@ spring:
sql:
init:
schema-locations: classpath:schema.sql
mode: always
mode: never
# mode: always
# JPA/Hibernate配置
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