mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
Merge pull request #14 from Craftech360-projects/admin_panel_agents
User agents are displaying in admin panel
This commit is contained in:
commit
14088736b2
10 changed files with 155 additions and 8 deletions
|
|
@ -67,11 +67,21 @@ public class AgentController {
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
|
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
@Operation(summary = "Get user agents list")
|
@Operation(summary = "Get agents list (admin gets all agents, user gets own agents)")
|
||||||
@RequiresPermissions("sys:role:normal")
|
@RequiresPermissions("sys:role:normal")
|
||||||
public Result<List<AgentDTO>> getUserAgents() {
|
public Result<List<AgentDTO>> getAgentsList() {
|
||||||
UserDetail user = SecurityUser.getUser();
|
UserDetail user = SecurityUser.getUser();
|
||||||
List<AgentDTO> agents = agentService.getUserAgents(user.getId());
|
List<AgentDTO> agents;
|
||||||
|
|
||||||
|
// Check if user is super admin
|
||||||
|
if (user.getSuperAdmin() != null && user.getSuperAdmin() == 1) {
|
||||||
|
// Admin sees all agents from all users with owner information
|
||||||
|
agents = agentService.getAllAgentsForAdmin();
|
||||||
|
} else {
|
||||||
|
// Regular user sees only their own agents
|
||||||
|
agents = agentService.getUserAgents(user.getId());
|
||||||
|
}
|
||||||
|
|
||||||
return new Result<List<AgentDTO>>().ok(agents);
|
return new Result<List<AgentDTO>>().ok(agents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
package xiaozhi.modules.agent.dao;
|
package xiaozhi.modules.agent.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
import xiaozhi.common.dao.BaseDao;
|
import xiaozhi.common.dao.BaseDao;
|
||||||
import xiaozhi.modules.agent.entity.AgentEntity;
|
import xiaozhi.modules.agent.entity.AgentEntity;
|
||||||
|
|
@ -30,6 +32,24 @@ public interface AgentDao extends BaseDao<AgentEntity> {
|
||||||
" ORDER BY d.id DESC LIMIT 1")
|
" ORDER BY d.id DESC LIMIT 1")
|
||||||
AgentEntity getDefaultAgentByMacAddress(@Param("macAddress") String macAddress);
|
AgentEntity getDefaultAgentByMacAddress(@Param("macAddress") String macAddress);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有智能体及其所有者信息(管理员专用)
|
||||||
|
*
|
||||||
|
* @return 所有智能体列表及用户信息
|
||||||
|
*/
|
||||||
|
@Select("SELECT " +
|
||||||
|
"a.id, a.agent_name, a.system_prompt, a.tts_model_id, " +
|
||||||
|
"a.llm_model_id, a.vllm_model_id, a.mem_model_id, a.tts_voice_id, " +
|
||||||
|
"a.created_at, a.updated_at, a.user_id, " +
|
||||||
|
"u.username as owner_username, " +
|
||||||
|
"GROUP_CONCAT(d.mac_address SEPARATOR ',') as device_mac_addresses " +
|
||||||
|
"FROM ai_agent a " +
|
||||||
|
"LEFT JOIN sys_user u ON a.user_id = u.id " +
|
||||||
|
"LEFT JOIN ai_device d ON a.id = d.agent_id " +
|
||||||
|
"GROUP BY a.id " +
|
||||||
|
"ORDER BY a.created_at DESC")
|
||||||
|
List<Map<String, Object>> getAllAgentsWithOwnerInfo();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据id查询agent信息,包括插件信息
|
* 根据id查询agent信息,包括插件信息
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -45,4 +45,14 @@ public class AgentDTO {
|
||||||
|
|
||||||
@Schema(description = "设备数量", example = "10")
|
@Schema(description = "设备数量", example = "10")
|
||||||
private Integer deviceCount;
|
private Integer deviceCount;
|
||||||
|
|
||||||
|
@Schema(description = "设备MAC地址列表", example = "AA:BB:CC:DD:EE:FF,11:22:33:44:55:66")
|
||||||
|
private String deviceMacAddresses;
|
||||||
|
|
||||||
|
// 管理员专用字段 - 智能体所有者信息
|
||||||
|
@Schema(description = "所有者用户名", example = "john_doe")
|
||||||
|
private String ownerUsername;
|
||||||
|
|
||||||
|
@Schema(description = "创建时间", example = "2024-03-20 10:00:00")
|
||||||
|
private Date createDate;
|
||||||
}
|
}
|
||||||
|
|
@ -43,6 +43,21 @@ public interface AgentService extends BaseService<AgentEntity> {
|
||||||
*/
|
*/
|
||||||
boolean insert(AgentEntity entity);
|
boolean insert(AgentEntity entity);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户的智能体列表
|
||||||
|
*
|
||||||
|
* @param userId 用户ID
|
||||||
|
* @return 智能体列表
|
||||||
|
*/
|
||||||
|
List<AgentDTO> getUserAgents(Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有智能体列表(管理员专用,包含用户信息)
|
||||||
|
*
|
||||||
|
* @return 所有智能体列表
|
||||||
|
*/
|
||||||
|
List<AgentDTO> getAllAgentsForAdmin();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据用户ID删除智能体
|
* 根据用户ID删除智能体
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -150,6 +150,59 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AgentDTO> getAllAgentsForAdmin() {
|
||||||
|
List<Map<String, Object>> agentMaps = agentDao.getAllAgentsWithOwnerInfo();
|
||||||
|
return agentMaps.stream().map(agentMap -> {
|
||||||
|
AgentDTO dto = new AgentDTO();
|
||||||
|
|
||||||
|
// 基础智能体信息
|
||||||
|
String agentId = (String) agentMap.get("id");
|
||||||
|
dto.setId(agentId);
|
||||||
|
dto.setAgentName((String) agentMap.get("agent_name"));
|
||||||
|
dto.setSystemPrompt((String) agentMap.get("system_prompt"));
|
||||||
|
|
||||||
|
// Handle LocalDateTime to Date conversion for createDate
|
||||||
|
Object createdAt = agentMap.get("created_at");
|
||||||
|
if (createdAt instanceof java.time.LocalDateTime) {
|
||||||
|
dto.setCreateDate(java.sql.Timestamp.valueOf((java.time.LocalDateTime) createdAt));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取模型名称 - 同用户方法
|
||||||
|
String ttsModelId = (String) agentMap.get("tts_model_id");
|
||||||
|
String llmModelId = (String) agentMap.get("llm_model_id");
|
||||||
|
String vllmModelId = (String) agentMap.get("vllm_model_id");
|
||||||
|
String memModelId = (String) agentMap.get("mem_model_id");
|
||||||
|
String ttsVoiceId = (String) agentMap.get("tts_voice_id");
|
||||||
|
|
||||||
|
dto.setTtsModelName(modelConfigService.getModelNameById(ttsModelId));
|
||||||
|
dto.setLlmModelName(modelConfigService.getModelNameById(llmModelId));
|
||||||
|
dto.setVllmModelName(modelConfigService.getModelNameById(vllmModelId));
|
||||||
|
dto.setMemModelId(memModelId);
|
||||||
|
dto.setTtsVoiceName(timbreModelService.getTimbreNameById(ttsVoiceId));
|
||||||
|
|
||||||
|
// 获取智能体最近的最后连接时长 - 同用户方法
|
||||||
|
dto.setLastConnectedAt(deviceService.getLatestLastConnectionTime(agentId));
|
||||||
|
|
||||||
|
// 获取设备MAC地址列表 - 管理员专用
|
||||||
|
String macAddresses = (String) agentMap.get("device_mac_addresses");
|
||||||
|
dto.setDeviceMacAddresses(macAddresses);
|
||||||
|
|
||||||
|
// 计算设备数量(从MAC地址列表或使用原方法)
|
||||||
|
if (macAddresses != null && !macAddresses.isEmpty()) {
|
||||||
|
dto.setDeviceCount(macAddresses.split(",").length);
|
||||||
|
} else {
|
||||||
|
// 使用原来的方法获取设备数量
|
||||||
|
dto.setDeviceCount(getDeviceCountByAgentId(agentId));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 管理员专用字段 - 用户信息
|
||||||
|
dto.setOwnerUsername((String) agentMap.get("owner_username"));
|
||||||
|
|
||||||
|
return dto;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Integer getDeviceCountByAgentId(String agentId) {
|
public Integer getDeviceCountByAgentId(String agentId) {
|
||||||
if (StringUtils.isBlank(agentId)) {
|
if (StringUtils.isBlank(agentId)) {
|
||||||
|
|
|
||||||
|
|
@ -71,7 +71,17 @@ public class DeviceController {
|
||||||
@RequiresPermissions("sys:role:normal")
|
@RequiresPermissions("sys:role:normal")
|
||||||
public Result<List<DeviceEntity>> getUserDevices(@PathVariable String agentId) {
|
public Result<List<DeviceEntity>> getUserDevices(@PathVariable String agentId) {
|
||||||
UserDetail user = SecurityUser.getUser();
|
UserDetail user = SecurityUser.getUser();
|
||||||
List<DeviceEntity> devices = deviceService.getUserDevices(user.getId(), agentId);
|
List<DeviceEntity> devices;
|
||||||
|
|
||||||
|
// Check if user is super admin
|
||||||
|
if (user.getSuperAdmin() != null && user.getSuperAdmin() == 1) {
|
||||||
|
// Admin can see all devices for any agent
|
||||||
|
devices = deviceService.getDevicesByAgentId(agentId);
|
||||||
|
} else {
|
||||||
|
// Regular user can only see their own devices
|
||||||
|
devices = deviceService.getUserDevices(user.getId(), agentId);
|
||||||
|
}
|
||||||
|
|
||||||
return new Result<List<DeviceEntity>>().ok(devices);
|
return new Result<List<DeviceEntity>>().ok(devices);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,11 @@ public interface DeviceService extends BaseService<DeviceEntity> {
|
||||||
*/
|
*/
|
||||||
List<DeviceEntity> getUserDevices(Long userId, String agentId);
|
List<DeviceEntity> getUserDevices(Long userId, String agentId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定智能体的所有设备列表(管理员专用)
|
||||||
|
*/
|
||||||
|
List<DeviceEntity> getDevicesByAgentId(String agentId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解绑设备
|
* 解绑设备
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -211,6 +211,13 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
||||||
return baseDao.selectList(wrapper);
|
return baseDao.selectList(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<DeviceEntity> getDevicesByAgentId(String agentId) {
|
||||||
|
QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
|
||||||
|
wrapper.eq("agent_id", agentId);
|
||||||
|
return baseDao.selectList(wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unbindDevice(Long userId, String deviceId) {
|
public void unbindDevice(Long userId, String deviceId) {
|
||||||
UpdateWrapper<DeviceEntity> wrapper = new UpdateWrapper<>();
|
UpdateWrapper<DeviceEntity> wrapper = new UpdateWrapper<>();
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ spring:
|
||||||
# Profile will be set by SPRING_PROFILES_ACTIVE environment variable
|
# Profile will be set by SPRING_PROFILES_ACTIVE environment variable
|
||||||
# Default to 'dev' if not specified
|
# Default to 'dev' if not specified
|
||||||
profiles:
|
profiles:
|
||||||
default: prod
|
default: dev
|
||||||
messages:
|
messages:
|
||||||
encoding: UTF-8
|
encoding: UTF-8
|
||||||
basename: i18n/messages
|
basename: i18n/messages
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="device-item">
|
<div class="device-item">
|
||||||
<div style="display: flex;justify-content: space-between;">
|
<div style="display: flex;justify-content: space-between;">
|
||||||
<div style="font-weight: 700;font-size: 18px;text-align: left;color: #3d4566;">
|
<div>
|
||||||
{{ device.agentName }}
|
<div style="font-weight: 700;font-size: 18px;text-align: left;color: #3d4566;">
|
||||||
|
{{ device.agentName }}
|
||||||
|
</div>
|
||||||
|
<!-- Admin-only: Show owner username -->
|
||||||
|
<div v-if="isAdmin && device.ownerUsername" class="owner-info">
|
||||||
|
Owner: {{ device.ownerUsername }}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<img src="@/assets/home/delete.png" alt="" class="action-icon delete-icon"
|
<img src="@/assets/home/delete.png" alt="" class="action-icon delete-icon"
|
||||||
|
|
@ -53,6 +59,9 @@ export default {
|
||||||
return { switchValue: false }
|
return { switchValue: false }
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
isAdmin() {
|
||||||
|
return this.$store.getters.getIsSuperAdmin;
|
||||||
|
},
|
||||||
formattedLastConnectedTime() {
|
formattedLastConnectedTime() {
|
||||||
if (!this.device.lastConnectedAt) return 'No conversations yet';
|
if (!this.device.lastConnectedAt) return 'No conversations yet';
|
||||||
|
|
||||||
|
|
@ -112,6 +121,14 @@ export default {
|
||||||
text-align: left;
|
text-align: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.owner-info {
|
||||||
|
font-weight: 400;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #5778ff;
|
||||||
|
text-align: left;
|
||||||
|
margin-top: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
.settings-btn {
|
.settings-btn {
|
||||||
font-weight: 500;
|
font-weight: 500;
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue