Compare commits

...
123 Commits
Author SHA1 Message Date
欣南科技andGitHub 497def8055 Merge pull request #569 from xinnan-tech/add-tag
update:0.1.16版本
2025-03-28 18:48:05 +08:00
hrz 62be26ff47 update:0.1.16版本
- 增加mcp服务
- 支持Dify、Coze时使用独立的意图识别
2025-03-28 18:47:20 +08:00
hrzandGitHub 6a81d80778 Merge pull request #492 from xinnan-tech/mcp-client
增加mcp的tool功能,可自行配置mcp服务,实现命令控制
2025-03-28 18:42:24 +08:00
hrzandGitHub 60c38a8dea Merge pull request #562 from mkanako/fix_handle_device
fix:修复handle_device.py 参数类型定义错误导致DoubaoLLM调用报 400 错误
2025-03-28 17:51:04 +08:00
欣南科技andGitHub 4e0934debf Merge pull request #567 from xinnan-tech/fix_intent_llm
fix:Dify、Coze可使用独立意图识别
2025-03-28 17:49:27 +08:00
hrz 17126303fa fix:已经使用了intent_llm,不应该再回到functioncall的chat中 2025-03-28 17:17:14 +08:00
CGDandGitHub 98fa95190c Merge pull request #563 from xinnan-tech/web_UserManagement_optimize
优化了“用户管理”,分页还未调整
2025-03-28 15:42:03 +08:00
Ran_Chen 3a2261107c 优化了“用户管理”,分页还未调整 2025-03-28 15:24:53 +08:00
kanakoandGitHub b1b64abed9 fix:修复handle_device.py 参数类型定义错误导致DoubaoLLM调用报 400 错误 2025-03-28 14:20:11 +08:00
hrz 05356b6652 update:简化intent_llm代码 2025-03-28 14:13:54 +08:00
CGDandGitHub cbd0c6615d Merge pull request #558 from xinnan-tech/web-HeaderBar-optimize
优化“导航栏”响应调整
2025-03-28 11:35:52 +08:00
Sakura-RanChenandGitHub 90e389efa2 Merge pull request #560 from xinnan-tech/web-api-AgentTemplate-get
完成“获取智能体模板”
2025-03-28 11:34:36 +08:00
CGD 00a0b26797 完成“获取智能体模板” 2025-03-28 11:20:08 +08:00
Ran_Chen 69b5ffe3bf 优化“导航栏”响应调整 2025-03-28 11:08:12 +08:00
玄凤科技 31539294eb 调整mcp等待时间为15秒 2025-03-28 09:52:27 +08:00
玄凤科技 d1a2307134 Merge branch 'mcp-client' of https://github.com/xinnan-tech/xiaozhi-esp32-server into mcp-client
# Conflicts:
#	main/xiaozhi-server/core/connection.py
#	main/xiaozhi-server/requirements.txt
2025-03-28 09:11:48 +08:00
hrzandGitHub 97b777f1b2 Merge pull request #550 from Ike-li/fix_Dockerfile-server
修复 Dockerfile-server 注释
2025-03-27 22:30:26 +08:00
Sakura-RanChenandGitHub a63137abc9 Merge pull request #551 from xinnan-tech/web-API-device
Web api device
2025-03-27 17:59:54 +08:00
CGD 3ca06f8d50 完成”绑定设备“功能 2025-03-27 17:54:05 +08:00
Ike-li e5b0e202be 修复 Dockerfile-server 注释 2025-03-27 15:39:11 +08:00
CGDandGitHub c66385d1f7 Merge pull request #547 from xinnan-tech/web-AddModel-optimize
优化“添加模型”页面布局
2025-03-27 11:57:01 +08:00
Ran_Chen b0304fd875 优化“添加模型”页面布局 2025-03-27 11:54:05 +08:00
CGD 5bfc3efc9c 完成“解绑设备” 2025-03-27 11:24:49 +08:00
hrzandGitHub 62845cfcf1 Merge pull request #515 from koalalgx/main
意图识别intent_llm单独配置独立的LLM,意图识别intent_llm增加天气和新闻查询function
2025-03-27 10:43:49 +08:00
Sakura-RanChenandGitHub 5c536e72df Merge pull request #546 from xinnan-tech/web-ModelConfig-modify
优化了”模型配置“页面
2025-03-27 09:13:41 +08:00
欣南科技andGitHub 7cfece1592 Merge pull request #544 from xinnan-tech/hot-fix
fix:函数调用时有时参数为空的bug
2025-03-27 01:08:50 +08:00
hrzandGitHub f393b2c9ba Merge pull request #542 from flashily/manger-api-flashily
修复Long id写入string的bug
2025-03-27 01:07:02 +08:00
hrzandGitHub 164f2e5472 Merge branch 'main' into manger-api-flashily 2025-03-27 01:06:42 +08:00
hrz 1434ecbc2e fix:函数调用时有时参数为空的bug 2025-03-27 01:03:38 +08:00
flashily c2e443d4ac 修复Long id写入string的bug 2025-03-27 00:26:37 +08:00
hrzandGitHub c079125458 Merge pull request #536 from xinnan-tech/manage-server-aliTTSToken
增加token过期刷新,修复过期bug
2025-03-27 00:21:01 +08:00
欣南科技andGitHub 4c1d7b34b9 Merge pull request #541 from xinnan-tech/hot-fix
merge:manag-api
2025-03-27 00:11:37 +08:00
hrzandGitHub 53772b3fe3 Merge pull request #540 from xinnan-tech/manager-api-agent-jsonL
agent api
2025-03-27 00:06:03 +08:00
hrz 28d3c40835 fixed:modelConfigEntity.setId是String类型 2025-03-27 00:02:26 +08:00
hrzandGitHub a01faecd0c Delete main/manager-api/.vscode/settings.json 2025-03-26 23:59:48 +08:00
hrzandGitHub a1b071e38e Merge pull request #518 from xinnan-tech/manager-api-admin
管理员功能重置密码,删除用户接口完成,分页和查询设备需等设备管理完成后调用实现
2025-03-26 23:12:56 +08:00
hrzandGitHub 571622a0f4 Merge pull request #483 from xinnan-tech/manager-api-model
manager-api 模块新增 /api/v1/models/ 接口
2025-03-26 23:10:36 +08:00
CGD 4ab84d7209 优化了”模型配置“页面 2025-03-26 21:43:07 +08:00
hrzandGitHub 30a938e348 update:更换iot演示demo图 (#538) 2025-03-26 18:31:23 +08:00
Sakura-RanChenandGitHub f6f4384e42 Merge pull request #537 from xinnan-tech/web-vue-modelconfig
优化了”模型配置“页面
2025-03-26 17:15:15 +08:00
CGD 911b11211e 优化了”模型配置“页面 2025-03-26 17:10:06 +08:00
Ken 8bb596c74f 增加token过期刷新,修复 2025-03-26 16:51:26 +08:00
CGDandGitHub d0fdeb33e0 Merge pull request #533 from xinnan-tech/web-model-AddModelDialog
完成添加模型组件
2025-03-26 16:43:19 +08:00
Ran_Chen ae7f41032e 完成添加模型组件 2025-03-26 15:53:47 +08:00
剑雨 f30cb6badb 完善删除,删除用户时,删除其他关联数据(等待其他功能方法调用)
--SysUserServiceImpl.java
2025-03-26 14:49:22 +08:00
欣南科技andGitHub 97a9c1e08a Merge pull request #532 from xinnan-tech/test-server-pr
merge:农历插件优化、异步消费线程不销毁的BUG
2025-03-26 13:14:43 +08:00
hrzandGitHub 1e699c027d Merge pull request #504 from suofang/fix_not_clear_threads
fix:解决客户端websocket连接断开后异步消费线程不销毁的BUG
2025-03-26 11:40:06 +08:00
hrzandGitHub 9bd731729d Merge pull request #528 from journey-ad/refactor-plugins
优化获取日期和音量亮度控制插件
2025-03-26 11:39:40 +08:00
hrzandGitHub dc17eccf76 add:在日志里添加版本信息 (#529) 2025-03-26 10:15:00 +08:00
Jad 3649a33afb feat(plugin_func): 重构音量控制插件,支持亮度调整
支持立创实战派等设备亮度调整
2025-03-26 04:24:47 +08:00
Jad d76bcc1366 feat(plugin_func): 拆分get_time和get_lunar插件 2025-03-26 01:32:43 +08:00
欣南科技andGitHub 2987f1c680 Merge pull request #519 from xinnan-tech/manage-server-edgetts-stream
edge-tts 流式写
2025-03-26 00:40:15 +08:00
hrz ab93ee0406 update:调整加载顺序 2025-03-26 00:39:34 +08:00
hrzandGitHub 69dd933a7d Merge pull request #526 from xinnan-tech/fix-wakeup_words
Fix wakeup words
2025-03-25 23:48:53 +08:00
欣南科技andGitHub 5440de0453 Merge pull request #525 from xinnan-tech/flashily-web
Flashily web
2025-03-25 22:44:42 +08:00
hrzandGitHub f2e130c70e Merge pull request #517 from flashily/flashily-web
与mock连接
2025-03-25 22:41:38 +08:00
Sakura-RanChenandGitHub 4c845c26f7 Merge pull request #521 from xinnan-tech/web-device-bind
Web device bind
2025-03-25 19:52:23 +08:00
CGD a43acca375 优化了user.js文件 2025-03-25 18:59:05 +08:00
Ken 635520aa85 edge-tts 流式写入,避免一次性加载完整语音到内存,无需等待完整音频生成再保存,音频数据块(chunk)生成后立即写入文件,相比等待完整音频生成后再保存,能更早得到部分结果 2025-03-25 18:05:25 +08:00
剑雨 fce9759e93 修复bug
--AdminController.java 重置密码添加返回新密码
--AdminPageUserDTO.java 取消参数验证
--SysUserServiceImpl.java 修改分页查询条件
2025-03-25 18:00:50 +08:00
flashily 18243cd4b0 与mock连接 2025-03-25 16:52:40 +08:00
剑雨 75a6e51e68 管理员控制层,添加接口,删除用户,重置密码,查找所有设备(未完成)
--AdminController.java
2025-03-25 16:50:47 +08:00
剑雨 312edd4b15 用户业务层为添加新方法,重置密码,直接修改密码
--SysUserService.java 定义2个方法
--SysUserServiceImpl.java 实现重置密码,直接修改密码
2025-03-25 16:39:38 +08:00
CGD be3e67b2c1 完成“已绑设备”功能 2025-03-25 16:23:28 +08:00
剑雨 a5765f81fe 控制层添加了新接口
--AdminController.java 添加管理员分页接口
2025-03-25 15:22:56 +08:00
剑雨 4bee92abb0 用户业务层添加了新功能:分页查找
--AdminPageUserDTO.java 管理员分页用户参数DTO
--AdminPageUserVO.java 管理员分页用户展示VO
--SysUserService.java 添加分页方法定义
--SysUserServiceImpl.java 实现了分页方法
2025-03-25 15:04:48 +08:00
CGDandGitHub d9dcb90745 Merge pull request #512 from xinnan-tech/web-change-password
完成修改密码
2025-03-25 14:33:03 +08:00
koalalgxandGitHub fbd25353d5 Update get_weather.py 2025-03-25 14:29:28 +08:00
koalalgxandGitHub 44c3e73577 Update intentHandler.py 2025-03-25 14:28:59 +08:00
koalalgxandGitHub 9cbad1a448 Update get_news.py 2025-03-25 14:28:32 +08:00
koalalgxandGitHub 3a3ddbc2bc Update connection.py 2025-03-25 14:27:48 +08:00
koalalgxandGitHub 63a3c33df9 Update base.py 2025-03-25 14:27:10 +08:00
koalalgxandGitHub 32d7400f7d Update intent_llm.py 2025-03-25 14:26:46 +08:00
koalalgxandGitHub 38191f976b Create xinference.py 2025-03-25 14:25:31 +08:00
CGDandGitHub c380e0e110 Merge branch 'main' into web-change-password 2025-03-25 14:24:58 +08:00
koalalgxandGitHub 2199b8eca7 Update config.yaml 2025-03-25 14:24:05 +08:00
koalalgxandGitHub 88455604a2 Update config.yaml 2025-03-25 14:22:34 +08:00
Sakura-RanChenandGitHub 63eb69091f Merge pull request #505 from xinnan-tech/web-agent-put
Web agent put
2025-03-25 14:19:33 +08:00
欣南科技andGitHub fc668810bd Merge pull request #510 from xinnan-tech/web-flashily
Web flashily
2025-03-25 11:32:57 +08:00
hrzandGitHub 2773e3a48a Merge pull request #506 from flashily/web-flashily
优化界面与细节
2025-03-25 11:28:31 +08:00
flashily 18a03f6ccc 优化界面与细节 2025-03-25 03:15:04 +08:00
CGD e9348f5512 优化了”配置智能体“ 2025-03-25 00:01:13 +08:00
suofang ce65226ea4 fix:解决客户端websocket连接断开后异步消费线程不销毁的BUG 2025-03-24 23:53:56 +08:00
CGD a3b3079b63 完成“配置智能体“功能 2025-03-24 22:41:32 +08:00
hrzandGitHub 22de4bb13b update:修复音乐播放卡顿和断连的bug
修复音乐播放卡顿和断连的bug
2025-03-24 21:55:02 +08:00
zhishengandGitHub eef7bb0cad Delete main/manager-api/src/main/resources/db/changelog/data.sql~ 2025-03-24 20:13:14 +08:00
pengzhisheng 8f1fbbdeef 去除多余文件,优化接口权限,优化 model 实体类主键生成策略 2025-03-24 20:08:33 +08:00
pengzhisheng f3562e091f 去除多余文件,优化接口权限,优化 model 实体类主键生成策略 2025-03-24 20:03:56 +08:00
Ran_Chen 2001258ef4 完成修改密码 2025-03-24 18:08:05 +08:00
CGD 52a66ea304 完成获取智能体配置 2025-03-24 17:27:08 +08:00
欣南科技andGitHub 21ddb1541f update:优化天气、农历插件
update:优化天气、农历插件
2025-03-24 17:16:34 +08:00
hrzandGitHub a3ec6d79b0 feat: 优化查询天气插件
feat: 优化查询天气插件
2025-03-24 16:32:11 +08:00
hrzandGitHub bb8c315ffc Merge pull request #495 from journey-ad/improve-func-time
feat: 支持查询农历日期和黄历信息
2025-03-24 16:30:55 +08:00
hrzandGitHub f2ae398d3f Merge pull request #493 from xinnan-tech/web-agent-configuration
完成删除智能体
2025-03-24 15:31:01 +08:00
Jad 02e33f68b4 feat: 支持查询农历日期和黄历信息 2025-03-24 13:12:46 +08:00
玄凤科技 8c4c9d88da 增加网站浏览mcp 2025-03-24 10:28:48 +08:00
李健豪 49cb98a606 修复音乐播放卡顿和断连的bug 2025-03-24 09:28:01 +08:00
玄凤科技 32ab716f0d Merge branch 'main' into mcp-client
# Conflicts:
#	main/xiaozhi-server/core/connection.py
#	main/xiaozhi-server/requirements.txt
2025-03-24 09:26:14 +08:00
LFNL-scholarandGitHub fe24529fce Merge branch 'xinnan-tech:main' into main 2025-03-24 09:24:30 +08:00
CGD b59c097bca 完成删除智能体 2025-03-24 09:16:48 +08:00
玄凤科技 6d6414eb62 Merge branch 'main' of https://github.com/xinnan-tech/xiaozhi-esp32-server 2025-03-24 08:44:29 +08:00
Jad 5b1c20d625 feat: 优化查询天气插件
7天预报包含每日天气
2025-03-24 02:09:01 +08:00
hrzandGitHub af6c8ab935 update:优化唤醒词缓存文件
* update:增加私有唤醒答复文件

* update:优化唤醒词缓存文件
2025-03-23 22:59:09 +08:00
hrzandGitHub 4acb5f924a update:增加私有唤醒答复文件 (#488) 2025-03-23 22:40:31 +08:00
b1b99f1066 update:忽略提示词缓存文件
* update:开启唤醒词加速

* update:忽略提示词缓存文件

---------

Co-authored-by: 欣南科技 <huangrongzhuang@xin-nan.com>
2025-03-23 22:12:11 +08:00
hrzandGitHub 004ee6f143 update:开启唤醒词加速 (#486) 2025-03-23 22:02:13 +08:00
欣南科技andGitHub 19455f89d3 update:优化唤醒词答复
update:优化唤醒词答复
2025-03-23 17:49:04 +08:00
hrz 18d98e3bd0 update:优化对唤醒词的识别 2025-03-23 17:47:39 +08:00
pengzhisheng af038b7fc8 manager-api 模块新增 /api/v1/models/ 接口:
1、新增模型配置对应接口;
2、优化 mapper 目录文件结构;
3、优化启动时自动执行数据库初始化脚本,新增默认账号 admin
2025-03-23 17:32:59 +08:00
hrz 4a9297dc65 update:优化提示音配置,这只成可配置项 2025-03-23 17:20:36 +08:00
HuangandGitHub 49fefb41a6 update:优化唤醒词响应时间,修复初始化函数异步bug (#482) 2025-03-23 16:20:20 +08:00
HuangandGitHub 2fa64dfdd0 Merge pull request #476 from journey-ad/optional-greeting
优化打招呼和进入聆听模式的反馈
2025-03-23 12:09:55 +08:00
Jad ed91e5d607 feat: 开场是否回复唤醒词支持配置 2025-03-23 01:26:31 +08:00
Jad 0bf0b0d381 feat: TTS播放结束提示音 2025-03-23 01:26:29 +08:00
玄凤科技 dc4968f961 添加命令行服务 2025-03-21 17:31:29 +08:00
玄凤科技 7ead5537f6 提交requirements.txt中mcp版本依赖 2025-03-21 17:15:54 +08:00
李健豪 220af8068e 修复音乐播放断开bug
- 修复音乐播放速度太快导致设备内存溢出断开连接的问题
2025-03-21 14:59:01 +08:00
jsonL 1a1d6c0835 agent api 2025-03-21 13:00:17 +08:00
玄凤科技 3ea95ffa48 mcp模板文件 2025-03-20 18:20:50 +08:00
玄凤科技 898fd36eca 增加mcp调用超时检测逻辑 2025-03-20 18:20:09 +08:00
玄凤科技 2b81ebca8e 调通mcp tool 2025-03-20 11:52:37 +08:00
玄凤科技 de9809ca5a 修复时间插件导致chatglm卡住问题 2025-03-20 09:06:36 +08:00
玄凤科技 8504c181c0 mcp初步调试 2025-03-20 08:59:45 +08:00
83 changed files with 4219 additions and 727 deletions
+2
View File
@@ -145,6 +145,7 @@ tmp
.history .history
.DS_Store .DS_Store
main/xiaozhi-server/data main/xiaozhi-server/data
main/xiaozhi-server/config/assets/wakeup_words.*
main/manager-web/node_modules main/manager-web/node_modules
.config.yaml .config.yaml
.secrets.yaml .secrets.yaml
@@ -154,3 +155,4 @@ main/manager-web/node_modules
# model files # model files
main/xiaozhi-server/models/SenseVoiceSmall/model.pt main/xiaozhi-server/models/SenseVoiceSmall/model.pt
main/xiaozhi-server/models/sherpa-onnx* main/xiaozhi-server/models/sherpa-onnx*
my_wakeup_words.mp3
+3 -3
View File
@@ -5,15 +5,15 @@ WORKDIR /app
COPY main/xiaozhi-server/requirements.txt . COPY main/xiaozhi-server/requirements.txt .
# 优化apt安装 # 安装Python依赖
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# 第阶段:生产镜像 # 第阶段:生产镜像
FROM python:3.10-slim FROM python:3.10-slim
WORKDIR /opt/xiaozhi-esp32-server WORKDIR /opt/xiaozhi-esp32-server
# 优化apt安装 # 安装系统依赖
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y --no-install-recommends libopus0 ffmpeg && \ apt-get install -y --no-install-recommends libopus0 ffmpeg && \
apt-get clean && \ apt-get clean && \
Binary file not shown.

Before

Width:  |  Height:  |  Size: 279 KiB

After

Width:  |  Height:  |  Size: 298 KiB

@@ -0,0 +1,84 @@
package xiaozhi.modules.admin.contrloler;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import xiaozhi.common.constant.Constant;
import xiaozhi.common.page.PageData;
import xiaozhi.common.utils.Result;
import xiaozhi.common.validator.ValidatorUtils;
import xiaozhi.modules.sys.dto.AdminPageUserDTO;
import xiaozhi.modules.sys.service.SysUserService;
import xiaozhi.modules.sys.vo.AdminPageUserVO;
import java.util.Map;
/**
* 管理员控制层
*
* @author zjy
* @since 2025-3-25
*/
@AllArgsConstructor
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员管理")
public class AdminController {
private final SysUserService sysUserService;
@GetMapping("/users")
@Operation(summary = "分页查找用户")
@RequiresPermissions("sys:role:superAdmin")
@Parameters({
@Parameter(name = "mobile", description = "用户手机号码", required = true),
@Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true),
@Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
})
public Result<PageData<AdminPageUserVO>> pageUser(
@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
AdminPageUserDTO dto = new AdminPageUserDTO();
dto.setMobile((String) params.get("mobile"));
dto.setLimit((String) params.get(Constant.LIMIT));
dto.setPage((String) params.get("pages"));
ValidatorUtils.validateEntity(dto);
PageData<AdminPageUserVO> page = sysUserService.page(dto);
return new Result<PageData<AdminPageUserVO>>().ok(page);
}
@PutMapping("/users/{id}")
@Operation(summary = "重置密码")
@RequiresPermissions("sys:role:superAdmin")
public Result<String> update(
@PathVariable Long id) {
String password = sysUserService.resetPassword(id);
return new Result<String>().ok(password);
}
@DeleteMapping("/users/{id}")
@Operation(summary = "用户删除")
@RequiresPermissions("sys:role:superAdmin")
public Result<Void> delete(@PathVariable Long id) {
sysUserService.delete(new Long[]{id});
return new Result<>();
}
@GetMapping("/device/all")
@Operation(summary = "分页查找设备")
@RequiresPermissions("sys:role:superAdmin")
@Parameters({
@Parameter(name = "keywords", description = "用户手机号码", required = true),
@Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true),
@Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
})
public Result<Void> pageDevice(
@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
//TODO 等设备功能模块写好
return new Result<Void>().error(600,"等设备功能模块写好");
}
}
@@ -0,0 +1,149 @@
package xiaozhi.modules.agent.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import xiaozhi.common.constant.Constant;
import xiaozhi.common.page.PageData;
import xiaozhi.common.user.UserDetail;
import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.common.utils.Result;
import xiaozhi.modules.agent.dto.AgentCreateDTO;
import xiaozhi.modules.agent.dto.AgentUpdateDTO;
import xiaozhi.modules.agent.entity.AgentEntity;
import xiaozhi.modules.agent.service.AgentService;
import xiaozhi.modules.security.user.SecurityUser;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Tag(name = "智能体管理")
@AllArgsConstructor
@RestController
@RequestMapping("/agent")
public class AgentController {
private final AgentService agentService;
@GetMapping("/list")
@Operation(summary = "获取用户智能体列表")
@RequiresPermissions("sys:role:normal")
public Result<List<AgentEntity>> getUserAgents() {
UserDetail user = SecurityUser.getUser();
List<AgentEntity> agents = agentService.getUserAgents(user.getId());
return new Result<List<AgentEntity>>().ok(agents);
}
@GetMapping("/all")
@Operation(summary = "智能体列表(管理员)")
@RequiresPermissions("sys:role:superAdmin")
@Parameters({
@Parameter(name = Constant.PAGE, description = "当前页码,从1开始", required = true),
@Parameter(name = Constant.LIMIT, description = "每页显示记录数", required = true),
})
public Result<PageData<AgentEntity>> adminAgentList(
@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
PageData<AgentEntity> page = agentService.adminAgentList(params);
return new Result<PageData<AgentEntity>>().ok(page);
}
@GetMapping("/{id}")
@Operation(summary = "获取智能体详情")
@RequiresPermissions("sys:role:normal")
public Result<AgentEntity> getAgentById(@PathVariable("id") String id) {
AgentEntity agent = agentService.getAgentById(id);
return new Result<AgentEntity>().ok(agent);
}
@PostMapping
@Operation(summary = "创建智能体")
@RequiresPermissions("sys:role:normal")
public Result<Void> save(@RequestBody @Valid AgentCreateDTO dto) {
AgentEntity entity = ConvertUtils.sourceToTarget(dto, AgentEntity.class);
// 设置用户ID和创建者信息
UserDetail user = SecurityUser.getUser();
entity.setUserId(user.getId());
entity.setCreator(user.getId());
entity.setCreatedAt(new Date());
// ID、智能体编码和排序会在Service层自动生成
agentService.insert(entity);
return new Result<>();
}
@PutMapping
@Operation(summary = "更新智能体")
@RequiresPermissions("sys:role:normal")
public Result<Void> update(@RequestBody @Valid AgentUpdateDTO dto) {
// 先查询现有实体
AgentEntity existingEntity = agentService.getAgentById(dto.getId());
if (existingEntity == null) {
return new Result<Void>().error("智能体不存在");
}
// 只更新提供的非空字段
if (dto.getAgentName() != null) {
existingEntity.setAgentName(dto.getAgentName());
}
if (dto.getAgentCode() != null) {
existingEntity.setAgentCode(dto.getAgentCode());
}
if (dto.getAsrModelId() != null) {
existingEntity.setAsrModelId(dto.getAsrModelId());
}
if (dto.getVadModelId() != null) {
existingEntity.setVadModelId(dto.getVadModelId());
}
if (dto.getLlmModelId() != null) {
existingEntity.setLlmModelId(dto.getLlmModelId());
}
if (dto.getTtsModelId() != null) {
existingEntity.setTtsModelId(dto.getTtsModelId());
}
if (dto.getTtsVoiceId() != null) {
existingEntity.setTtsVoiceId(dto.getTtsVoiceId());
}
if (dto.getMemModelId() != null) {
existingEntity.setMemModelId(dto.getMemModelId());
}
if (dto.getIntentModelId() != null) {
existingEntity.setIntentModelId(dto.getIntentModelId());
}
if (dto.getSystemPrompt() != null) {
existingEntity.setSystemPrompt(dto.getSystemPrompt());
}
if (dto.getLangCode() != null) {
existingEntity.setLangCode(dto.getLangCode());
}
if (dto.getLanguage() != null) {
existingEntity.setLanguage(dto.getLanguage());
}
if (dto.getSort() != null) {
existingEntity.setSort(dto.getSort());
}
// 设置更新者信息
UserDetail user = SecurityUser.getUser();
existingEntity.setUpdater(user.getId());
existingEntity.setUpdatedAt(new Date());
agentService.updateById(existingEntity);
return new Result<>();
}
@DeleteMapping("/{id}")
@Operation(summary = "删除智能体")
@RequiresPermissions("sys:role:normal")
public Result<Void> delete(@PathVariable String id) {
agentService.deleteById(id);
return new Result<>();
}
}
@@ -0,0 +1,10 @@
package xiaozhi.modules.agent.dao;
import org.apache.ibatis.annotations.Mapper;
import xiaozhi.common.dao.BaseDao;
import xiaozhi.modules.agent.entity.AgentEntity;
@Mapper
public interface AgentDao extends BaseDao<AgentEntity> {
}
@@ -0,0 +1,51 @@
package xiaozhi.modules.agent.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.io.Serializable;
/**
* 智能体创建DTO
* 专用于新增智能体,不包含id、agentCode和sort字段,这些字段由系统自动生成/设置默认值
*/
@Data
@Schema(description = "智能体创建对象")
public class AgentCreateDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "智能体名称", example = "客服助手")
@NotBlank(message = "智能体名称不能为空")
private String agentName;
@Schema(description = "语音识别模型标识", example = "asr_model_01")
private String asrModelId;
@Schema(description = "语音活动检测标识", example = "vad_model_01")
private String vadModelId;
@Schema(description = "大语言模型标识", example = "llm_model_01")
private String llmModelId;
@Schema(description = "语音合成模型标识", example = "tts_model_01")
private String ttsModelId;
@Schema(description = "音色标识", example = "voice_01")
private String ttsVoiceId;
@Schema(description = "记忆模型标识", example = "mem_model_01")
private String memModelId;
@Schema(description = "意图模型标识", example = "intent_model_01")
private String intentModelId;
@Schema(description = "角色设定参数", example = "你是一个专业的客服助手,负责回答用户问题")
private String systemPrompt;
@Schema(description = "语言编码", example = "zh_CN")
private String langCode;
@Schema(description = "交互语种", example = "中文")
private String language;
}
@@ -0,0 +1,61 @@
package xiaozhi.modules.agent.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.io.Serializable;
/**
* 智能体更新DTO
* 专用于更新智能体,id字段是必需的,用于标识要更新的智能体
* 其他字段均为非必填,只更新提供的字段
*/
@Data
@Schema(description = "智能体更新对象")
public class AgentUpdateDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "智能体唯一标识", example = "a1b2c3d4e5f6", required = true)
@NotBlank(message = "智能体ID不能为空")
private String id;
@Schema(description = "智能体编码", example = "AGT_1234567890", required = false)
private String agentCode;
@Schema(description = "智能体名称", example = "客服助手", required = false)
private String agentName;
@Schema(description = "语音识别模型标识", example = "asr_model_02", required = false)
private String asrModelId;
@Schema(description = "语音活动检测标识", example = "vad_model_02", required = false)
private String vadModelId;
@Schema(description = "大语言模型标识", example = "llm_model_02", required = false)
private String llmModelId;
@Schema(description = "语音合成模型标识", example = "tts_model_02", required = false)
private String ttsModelId;
@Schema(description = "音色标识", example = "voice_02", required = false)
private String ttsVoiceId;
@Schema(description = "记忆模型标识", example = "mem_model_02", required = false)
private String memModelId;
@Schema(description = "意图模型标识", example = "intent_model_02", required = false)
private String intentModelId;
@Schema(description = "角色设定参数", example = "你是一个专业的客服助手,负责回答用户问题并提供帮助", required = false)
private String systemPrompt;
@Schema(description = "语言编码", example = "zh_CN", required = false)
private String langCode;
@Schema(description = "交互语种", example = "中文", required = false)
private String language;
@Schema(description = "排序", example = "1", required = false)
private Integer sort;
}
@@ -0,0 +1,69 @@
package xiaozhi.modules.agent.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
@Data
@TableName("ai_agent")
@Schema(description = "智能体信息")
public class AgentEntity {
@Schema(description = "智能体唯一标识")
private String id;
@Schema(description = "所属用户ID")
private Long userId;
@Schema(description = "智能体编码")
private String agentCode;
@Schema(description = "智能体名称")
private String agentName;
@Schema(description = "语音识别模型标识")
private String asrModelId;
@Schema(description = "语音活动检测标识")
private String vadModelId;
@Schema(description = "大语言模型标识")
private String llmModelId;
@Schema(description = "语音合成模型标识")
private String ttsModelId;
@Schema(description = "音色标识")
private String ttsVoiceId;
@Schema(description = "记忆模型标识")
private String memModelId;
@Schema(description = "意图模型标识")
private String intentModelId;
@Schema(description = "角色设定参数")
private String systemPrompt;
@Schema(description = "语言编码")
private String langCode;
@Schema(description = "交互语种")
private String language;
@Schema(description = "排序")
private Integer sort;
@Schema(description = "创建者")
private Long creator;
@Schema(description = "创建时间")
private Date createdAt;
@Schema(description = "更新者")
private Long updater;
@Schema(description = "更新时间")
private Date updatedAt;
}
@@ -0,0 +1,25 @@
package xiaozhi.modules.agent.service;
import xiaozhi.common.page.PageData;
import xiaozhi.common.service.BaseService;
import xiaozhi.modules.agent.entity.AgentEntity;
import java.util.List;
import java.util.Map;
public interface AgentService extends BaseService<AgentEntity> {
/**
* 根据用户ID获取智能体列表
*/
List<AgentEntity> getUserAgents(Long userId);
/**
* 管理员获取所有智能体列表(分页)
*/
PageData<AgentEntity> adminAgentList(Map<String, Object> params);
/**
* 获取智能体详情
*/
AgentEntity getAgentById(String id);
}
@@ -0,0 +1,64 @@
package xiaozhi.modules.agent.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import xiaozhi.common.page.PageData;
import xiaozhi.common.service.impl.BaseServiceImpl;
import xiaozhi.modules.agent.dao.AgentDao;
import xiaozhi.modules.agent.entity.AgentEntity;
import xiaozhi.modules.agent.service.AgentService;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Service
public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> implements AgentService {
private final AgentDao agentDao;
public AgentServiceImpl(AgentDao agentDao) {
this.agentDao = agentDao;
}
@Override
public List<AgentEntity> getUserAgents(Long userId) {
QueryWrapper<AgentEntity> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", userId);
return agentDao.selectList(wrapper);
}
@Override
public PageData<AgentEntity> adminAgentList(Map<String, Object> params) {
IPage<AgentEntity> page = agentDao.selectPage(
getPage(params, "sort", true),
new QueryWrapper<>()
);
return new PageData<>(page.getRecords(), page.getTotal());
}
@Override
public AgentEntity getAgentById(String id) {
return agentDao.selectById(id);
}
@Override
public boolean insert(AgentEntity entity) {
// 如果ID为空,自动生成一个UUID作为ID
if (entity.getId() == null || entity.getId().trim().isEmpty()) {
entity.setId(UUID.randomUUID().toString().replace("-", ""));
}
// 如果智能体编码为空,自动生成一个带前缀的编码
if (entity.getAgentCode() == null || entity.getAgentCode().trim().isEmpty()) {
entity.setAgentCode("AGT_" + System.currentTimeMillis());
}
// 如果排序字段为空,设置默认值0
if (entity.getSort() == null) {
entity.setSort(0);
}
return super.insert(entity);
}
}
@@ -0,0 +1,2 @@
package xiaozhi.modules.model.controller;public class ModelConfigController {
}
@@ -0,0 +1,106 @@
package xiaozhi.modules.model.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import xiaozhi.common.page.PageData;
import xiaozhi.common.utils.Result;
import xiaozhi.modules.model.dto.ModelConfigBodyDTO;
import xiaozhi.modules.model.dto.ModelConfigDTO;
import xiaozhi.modules.model.dto.ModelProviderDTO;
import xiaozhi.modules.model.service.ModelConfigService;
import xiaozhi.modules.model.service.ModelProviderService;
import java.util.List;
@AllArgsConstructor
@RestController
@RequestMapping("/models")
@Tag(name = "模型配置")
public class ModelController {
private final ModelProviderService modelProviderService;
private final ModelConfigService modelConfigService;
@GetMapping("/models/names")
@Operation(summary = "获取所有模型名称")
@RequiresPermissions("sys:role:superAdmin")
public Result<List<String>> getModelNames(@RequestParam String modelType,
@RequestParam(required = false) String modelName) {
List<String> modelNameList = modelConfigService.getModelCodeList(modelType, modelName);
return new Result<List<String>>().ok(modelNameList);
}
@GetMapping("/{modelType}/provideTypes")
@Operation(summary = "获取模型供应器列表")
@RequiresPermissions("sys:role:superAdmin")
public Result<List<ModelProviderDTO>> getModelProviderList(@PathVariable String modelType) {
List<ModelProviderDTO> modelProviderDTOS = modelProviderService.getListByModelType(modelType);
return new Result<List<ModelProviderDTO>>().ok(modelProviderDTOS);
}
@GetMapping("/{modelType}/{provideCode}/fields")
@Operation(summary = "获取模型供应器字段")
@RequiresPermissions("sys:role:superAdmin")
public Result<List<String>> getModelProviderFields(@PathVariable String modelType, @PathVariable String provideCode) {
List<String> fieldList = modelProviderService.getFieldList(modelType, provideCode);
return new Result<List<String>>().ok(fieldList);
}
@GetMapping("/models/list")
@Operation(summary = "获取模型配置列表")
@RequiresPermissions("sys:role:superAdmin")
public Result<PageData<ModelConfigDTO>> getModelConfigList(@RequestParam String modelType,
@RequestParam(required = false) String modelName,
@RequestParam(required = false, defaultValue = "0") Integer page,
@RequestParam(required = false,defaultValue = "10") Integer limit) {
PageData<ModelConfigDTO> pageList = modelConfigService.getPageList(modelType, modelName, page, limit);
return new Result<PageData<ModelConfigDTO>>().ok(pageList);
}
@PostMapping("/models/{modelType}/{provideCode}")
@Operation(summary = "新增模型配置")
@RequiresPermissions("sys:role:superAdmin")
public Result<ModelConfigDTO> addModelConfig(@PathVariable String modelType,
@PathVariable String provideCode,
@RequestBody ModelConfigBodyDTO modelConfigBodyDTO) {
ModelConfigDTO modelConfigDTO = modelConfigService.add(modelType, provideCode, modelConfigBodyDTO);
return new Result<ModelConfigDTO>().ok(modelConfigDTO);
}
@PutMapping("/models/{modelType}/{provideCode}/{id}")
@Operation(summary = "编辑模型配置")
@RequiresPermissions("sys:role:superAdmin")
public Result<ModelConfigDTO> editModelConfig(@PathVariable String modelType,
@PathVariable String provideCode,
@PathVariable String id,
@RequestBody ModelConfigBodyDTO modelConfigBodyDTO) {
ModelConfigDTO modelConfigDTO = modelConfigService.edit(modelType, provideCode, id, modelConfigBodyDTO);
return new Result<ModelConfigDTO>().ok(modelConfigDTO);
}
@DeleteMapping("/models/{modelType}/{provideCode}/{id}")
@Operation(summary = "删除模型配置")
@RequiresPermissions("sys:role:superAdmin")
public Result<Void> deleteModelConfig(@PathVariable String modelType, @PathVariable String provideCode, @PathVariable String id) {
modelConfigService.delete(modelType, provideCode, id);
return new Result<>();
}
@GetMapping("/models/{modelName}/voices")
@Operation(summary = "获取模型音色")
@RequiresPermissions("sys:role:normal")
public Result<List<String>> getVoiceList(@PathVariable String modelName,
@RequestParam(required = false) String voiceName) {
List<String> voiceList = modelConfigService.getVoiceList(modelName, voiceName);
return new Result<List<String>>().ok(voiceList);
}
}
@@ -0,0 +1,17 @@
package xiaozhi.modules.model.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import xiaozhi.common.dao.BaseDao;
import xiaozhi.modules.model.entity.ModelConfigEntity;
import java.util.List;
@Mapper
public interface ModelConfigDao extends BaseDao<ModelConfigEntity> {
/**
* get model_code list
*/
List<String> getModelCodeList(@Param("modelType") String modelType, @Param("modelName") String modelName);
}
@@ -0,0 +1,14 @@
package xiaozhi.modules.model.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import xiaozhi.common.dao.BaseDao;
import xiaozhi.modules.model.entity.ModelProviderEntity;
import java.util.List;
@Mapper
public interface ModelProviderDao extends BaseDao<ModelProviderEntity> {
List<String> getFieldList(@Param("modelType") String modelType, @Param("provideCode") String provideCode);
}
@@ -0,0 +1,41 @@
package xiaozhi.modules.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
@Data
@Schema(description = "模型供应器/商")
public class ModelConfigBodyDTO {
@Serial
private static final long serialVersionUID = 1L;
// @Schema(description = "模型类型(Memory/ASR/VAD/LLM/TTS)")
// private String modelType;
//
@Schema(description = "模型编码(如AliLLM、DoubaoTTS)")
private String modelCode;
@Schema(description = "模型名称")
private String modelName;
@Schema(description = "是否默认配置(0否 1是)")
private Integer isDefault;
@Schema(description = "是否启用")
private Integer isEnabled;
@Schema(description = "模型配置(JSON格式)")
private String configJson;
@Schema(description = "官方文档链接")
private String docLink;
@Schema(description = "备注")
private String remark;
@Schema(description = "排序")
private Integer sort;
}
@@ -0,0 +1,45 @@
package xiaozhi.modules.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
@Schema(description = "模型供应器/商")
public class ModelConfigDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "主键")
private Long id;
@Schema(description = "模型类型(Memory/ASR/VAD/LLM/TTS)")
private String modelType;
@Schema(description = "模型编码(如AliLLM、DoubaoTTS)")
private String modelCode;
@Schema(description = "模型名称")
private String modelName;
@Schema(description = "是否默认配置(0否 1是)")
private Integer isDefault;
@Schema(description = "是否启用")
private Integer isEnabled;
@Schema(description = "模型配置(JSON格式)")
private String configJson;
@Schema(description = "官方文档链接")
private String docLink;
@Schema(description = "备注")
private String remark;
@Schema(description = "排序")
private Integer sort;
}
@@ -0,0 +1,45 @@
package xiaozhi.modules.model.dto;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
@Schema(description = "模型供应器/商")
public class ModelProviderDTO implements Serializable {
//
// @Schema(description = "主键")
// private Long id;
@Schema(description = "模型类型(Memory/ASR/VAD/LLM/TTS)")
private String modelType;
@Schema(description = "供应器类型")
private String providerCode;
@Schema(description = "供应器名称")
private String name;
@Schema(description = "供应器字段列表(JSON格式)")
@TableField(typeHandler = JacksonTypeHandler.class)
private String fields;
@Schema(description = "排序")
private Integer sort;
@Schema(description = "创建者")
private Long creator;
@Schema(description = "创建时间")
private Date createDate;
@Schema(description = "更新者")
private Long updater;
@Schema(description = "更新时间")
private Date updateDate;
}
@@ -0,0 +1,62 @@
package xiaozhi.modules.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
@Data
@TableName("ai_model_config")
@Schema(description = "模型配置表")
public class ModelConfigEntity {
@TableId(type = IdType.ASSIGN_UUID)
@Schema(description = "主键")
private String id;
@Schema(description = "模型类型(Memory/ASR/VAD/LLM/TTS)")
private String modelType;
@Schema(description = "模型编码(如AliLLM、DoubaoTTS)")
private String modelCode;
@Schema(description = "模型名称")
private String modelName;
@Schema(description = "是否默认配置(0否 1是)")
private Integer isDefault;
@Schema(description = "是否启用")
private Integer isEnabled;
@TableField(typeHandler = JacksonTypeHandler.class)
@Schema(description = "模型配置(JSON格式)")
private String configJson;
@Schema(description = "官方文档链接")
private String docLink;
@Schema(description = "备注")
private String remark;
@Schema(description = "排序")
private Integer sort;
@Schema(description = "创建者")
private Long creator;
@Schema(description = "创建时间")
private Date createDate;
@Schema(description = "更新者")
private Long updater;
@Schema(description = "更新时间")
private Date updateDate;
}
@@ -0,0 +1,49 @@
package xiaozhi.modules.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
@Data
@TableName("ai_model_provider")
@Schema(description = "模型供应器表")
public class ModelProviderEntity {
@TableId(type = IdType.ASSIGN_UUID)
@Schema(description = "主键")
private String id;
@Schema(description = "模型类型(Memory/ASR/VAD/LLM/TTS)")
private String modelType;
@Schema(description = "供应器类型,如 openai、")
private String providerCode;
@Schema(description = "供应器名称")
private String name;
@Schema(description = "供应器字段列表(JSON格式)")
@TableField(typeHandler = JacksonTypeHandler.class)
private String fields;
@Schema(description = "排序")
private Integer sort;
@Schema(description = "创建者")
private Long creator;
@Schema(description = "创建时间")
private Date createDate;
@Schema(description = "更新者")
private Long updater;
@Schema(description = "更新时间")
private Date updateDate;
}
@@ -0,0 +1,22 @@
package xiaozhi.modules.model.service;
import xiaozhi.common.page.PageData;
import xiaozhi.modules.model.dto.ModelConfigBodyDTO;
import xiaozhi.modules.model.dto.ModelConfigDTO;
import java.util.List;
public interface ModelConfigService {
List<String> getModelCodeList(String modelType, String modelName);
PageData<ModelConfigDTO> getPageList(String modelType, String modelName, Integer page, Integer limit);
ModelConfigDTO add(String modelType, String provideCode, ModelConfigBodyDTO modelConfigBodyDTO);
ModelConfigDTO edit(String modelType, String provideCode, String id, ModelConfigBodyDTO modelConfigBodyDTO);
void delete(String modelType, String provideCode, String id);
List<String> getVoiceList(String modelName, String voiceName);
}
@@ -0,0 +1,23 @@
package xiaozhi.modules.model.service;
import xiaozhi.modules.model.dto.ModelProviderDTO;
import xiaozhi.modules.model.entity.ModelProviderEntity;
import java.util.List;
public interface ModelProviderService {
// List<String> getModelNames(String modelType, String modelName);
List<ModelProviderDTO> getListByModelType(String modelType);
ModelProviderDTO add(ModelProviderEntity modelProviderEntity);
ModelProviderDTO edit(ModelProviderEntity modelProviderEntity);
void delete();
List<ModelProviderDTO> getList(String modelType, String provideCode);
List<String> getFieldList(String modelType, String provideCode);
}
@@ -0,0 +1,124 @@
package xiaozhi.modules.model.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import xiaozhi.common.constant.Constant;
import xiaozhi.common.exception.RenException;
import xiaozhi.common.page.PageData;
import xiaozhi.common.service.impl.BaseServiceImpl;
import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.modules.model.dao.ModelConfigDao;
import xiaozhi.modules.model.dto.ModelConfigBodyDTO;
import xiaozhi.modules.model.dto.ModelConfigDTO;
import xiaozhi.modules.model.dto.ModelProviderDTO;
import xiaozhi.modules.model.entity.ModelConfigEntity;
import xiaozhi.modules.model.service.ModelConfigService;
import xiaozhi.modules.model.service.ModelProviderService;
import xiaozhi.modules.timbre.service.TimbreService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@AllArgsConstructor
public class ModelConfigServiceImpl extends BaseServiceImpl<ModelConfigDao, ModelConfigEntity> implements ModelConfigService {
private final ModelConfigDao modelConfigDao;
private final ModelProviderService modelProviderService;
private final TimbreService timbreService;
private static final Logger logger = LoggerFactory.getLogger(ModelConfigServiceImpl.class);
@Override
public List<String> getModelCodeList(String modelType, String modelName) {
return modelConfigDao.getModelCodeList(modelType, modelName);
}
@Override
public PageData<ModelConfigDTO> getPageList(String modelType, String modelName, Integer page, Integer limit) {
Map<String, Object> params = new HashMap<String, Object>();
params.put(Constant.PAGE, page);
params.put(Constant.LIMIT, limit);
IPage<ModelConfigEntity> modelConfigEntityIPage = modelConfigDao.selectPage(
getPage(params, "sort", true),
new QueryWrapper<ModelConfigEntity>()
.eq("model_type", modelType)
.like(StringUtils.isNotBlank(modelName), "model_name", modelName)
);
return getPageData(modelConfigEntityIPage, ModelConfigDTO.class);
}
@Override
public ModelConfigDTO add(String modelType, String provideCode, ModelConfigBodyDTO modelConfigBodyDTO) {
// 先验证有没有供应器
if (StringUtils.isBlank(modelType) || StringUtils.isBlank(provideCode)) {
throw new RenException("modelType和provideCode不能为空");
}
List<ModelProviderDTO> providerList = modelProviderService.getList(modelType, provideCode);
if (CollectionUtil.isEmpty(providerList)) {
throw new RenException("供应器不存在");
}
// 再保存供应器提供的模型
ModelConfigEntity modelConfigEntity = ConvertUtils.sourceToTarget(modelConfigBodyDTO, ModelConfigEntity.class);
modelConfigEntity.setModelType(modelType);
modelConfigDao.insert(modelConfigEntity);
return ConvertUtils.sourceToTarget(modelConfigEntity, ModelConfigDTO.class);
}
@Override
public ModelConfigDTO edit(String modelType, String provideCode, String id, ModelConfigBodyDTO modelConfigBodyDTO) {
// 先验证有没有供应器
if (StringUtils.isBlank(modelType) || StringUtils.isBlank(provideCode)) {
throw new RenException("modelType和provideCode不能为空");
}
List<ModelProviderDTO> providerList = modelProviderService.getList(modelType, provideCode);
if (CollectionUtil.isEmpty(providerList)) {
throw new RenException("供应器不存在");
}
// 再更新供应器提供的模型
ModelConfigEntity modelConfigEntity = ConvertUtils.sourceToTarget(modelConfigBodyDTO, ModelConfigEntity.class);
modelConfigEntity.setId(id);
modelConfigEntity.setModelType(modelType);
modelConfigDao.updateById(modelConfigEntity);
return ConvertUtils.sourceToTarget(modelConfigEntity, ModelConfigDTO.class);
}
@Override
public void delete(String modelType, String provideCode, String id) {
// 先验证有没有供应器
if (StringUtils.isBlank(modelType) || StringUtils.isBlank(provideCode)) {
throw new RenException("modelType和provideCode不能为空");
}
List<ModelProviderDTO> providerList = modelProviderService.getList(modelType, provideCode);
if (CollectionUtil.isEmpty(providerList)) {
throw new RenException("供应器不存在");
}
modelConfigDao.deleteById(Long.getLong(id));
}
@Override
public List<String> getVoiceList(String modelName, String voiceName) {
QueryWrapper<ModelConfigEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("model_name", StringUtils.isBlank(modelName) ? "" : modelName);
queryWrapper.eq("model_type", "TTS");
List<ModelConfigEntity> modelConfigEntities = modelConfigDao.selectList(queryWrapper);
if (CollectionUtil.isEmpty(modelConfigEntities)) {
logger.warn("没有找到模型配置信息");
return null;
}
ModelConfigEntity modelConfigEntity = modelConfigEntities.get(0);
String id = modelConfigEntity.getId();
return timbreService.getVoiceNames(id, voiceName);
}
}
@@ -0,0 +1,59 @@
package xiaozhi.modules.model.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import xiaozhi.common.service.impl.BaseServiceImpl;
import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.modules.model.dao.ModelProviderDao;
import xiaozhi.modules.model.dto.ModelProviderDTO;
import xiaozhi.modules.model.entity.ModelProviderEntity;
import xiaozhi.modules.model.service.ModelProviderService;
import java.util.List;
@Service
@AllArgsConstructor
public class ModelProviderServiceImpl extends BaseServiceImpl<ModelProviderDao, ModelProviderEntity> implements ModelProviderService {
private final ModelProviderDao modelProviderDao;
@Override
public List<ModelProviderDTO> getListByModelType(String modelType) {
QueryWrapper<ModelProviderEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("model_type", StringUtils.isBlank(modelType) ? "" : modelType);
List<ModelProviderEntity> providerEntities = modelProviderDao.selectList(queryWrapper);
return ConvertUtils.sourceToTarget(providerEntities, ModelProviderDTO.class);
}
@Override
public ModelProviderDTO add(ModelProviderEntity modelProviderEntity) {
return null;
}
@Override
public ModelProviderDTO edit(ModelProviderEntity modelProviderEntity) {
return null;
}
@Override
public void delete() {
}
@Override
public List<ModelProviderDTO> getList(String modelType, String provideCode) {
QueryWrapper<ModelProviderEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("model_type", StringUtils.isBlank(modelType) ? "" : modelType);
queryWrapper.eq("provide_code", StringUtils.isBlank(provideCode) ? "" : provideCode);
List<ModelProviderEntity> providerEntities = modelProviderDao.selectList(queryWrapper);
return ConvertUtils.sourceToTarget(providerEntities, ModelProviderDTO.class);
}
@Override
public List<String> getFieldList(String modelType, String provideCode) {
return modelProviderDao.getFieldList(modelType, provideCode);
}
}
@@ -8,6 +8,8 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter; import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import xiaozhi.common.constant.Constant; import xiaozhi.common.constant.Constant;
import xiaozhi.common.exception.ErrorCode; import xiaozhi.common.exception.ErrorCode;
@@ -24,12 +26,15 @@ import java.io.IOException;
*/ */
public class Oauth2Filter extends AuthenticatingFilter { public class Oauth2Filter extends AuthenticatingFilter {
private static final Logger logger = LoggerFactory.getLogger(Oauth2Filter.class);
@Override @Override
protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception { protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
//获取请求token //获取请求token
String token = getRequestToken((HttpServletRequest) request); String token = getRequestToken((HttpServletRequest) request);
if (StringUtils.isBlank(token)) { if (StringUtils.isBlank(token)) {
logger.warn("createToken:token is empty");
return null; return null;
} }
@@ -49,7 +54,15 @@ public class Oauth2Filter extends AuthenticatingFilter {
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//获取请求token,如果token不存在,直接返回401 //获取请求token,如果token不存在,直接返回401
String token = getRequestToken((HttpServletRequest) request); String token = getRequestToken((HttpServletRequest) request);
// TODO 调试接口,临时取消登录限制,需要 token 参数的除外
if (true) {
return true;
}
if (StringUtils.isBlank(token)) { if (StringUtils.isBlank(token)) {
logger.warn("onAccessDenied:token is empty");
HttpServletResponse httpResponse = (HttpServletResponse) response; HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setContentType("application/json;charset=utf-8"); httpResponse.setContentType("application/json;charset=utf-8");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
@@ -73,13 +86,14 @@ public class Oauth2Filter extends AuthenticatingFilter {
httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin()); httpResponse.setHeader("Access-Control-Allow-Origin", HttpContextUtils.getOrigin());
try { try {
//处理登录失败的异常 //处理登录失败的异常
logger.error("onLoginFailure:登录失败!", e);
Throwable throwable = e.getCause() == null ? e : e.getCause(); Throwable throwable = e.getCause() == null ? e : e.getCause();
Result r = new Result().error(ErrorCode.UNAUTHORIZED, throwable.getMessage()); Result r = new Result().error(ErrorCode.UNAUTHORIZED, throwable.getMessage());
String json = JsonUtils.toJsonString(r); String json = JsonUtils.toJsonString(r);
httpResponse.getWriter().print(json); httpResponse.getWriter().print(json);
} catch (IOException e1) { } catch (IOException e1) {
logger.error("onLoginFailure:登录失败! msg:{}", e1.getMessage(), e1);
} }
return false; return false;
@@ -4,8 +4,11 @@ import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.wf.captcha.SpecCaptcha; import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha; import com.wf.captcha.base.Captcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xiaozhi.common.redis.RedisKeys; import xiaozhi.common.redis.RedisKeys;
import xiaozhi.common.redis.RedisUtils; import xiaozhi.common.redis.RedisUtils;
import xiaozhi.modules.security.oauth2.Oauth2Realm;
import xiaozhi.modules.security.service.CaptchaService; import xiaozhi.modules.security.service.CaptchaService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@@ -29,6 +32,7 @@ public class CaptchaServiceImpl implements CaptchaService {
* Local Cache 5分钟过期 * Local Cache 5分钟过期
*/ */
Cache<String, String> localCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(5, TimeUnit.MINUTES).build(); Cache<String, String> localCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterAccess(5, TimeUnit.MINUTES).build();
private static final Logger logger = LoggerFactory.getLogger(Oauth2Realm.class);
@Override @Override
public void create(HttpServletResponse response, String uuid) throws IOException { public void create(HttpServletResponse response, String uuid) throws IOException {
@@ -0,0 +1,25 @@
package xiaozhi.modules.sys.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
/**
* 管理员分页用户的参数DTO
* @author zjy
* @since 2025-3-21
*/
@Data
@Schema(description = "音色分页参数")
public class AdminPageUserDTO {
@Schema(description = "手机号码")
private String mobile;
@Schema(description = "页数")
private String page;
@Schema(description = "显示列数")
private String limit;
}
@@ -1,9 +1,12 @@
package xiaozhi.modules.sys.service; package xiaozhi.modules.sys.service;
import xiaozhi.common.page.PageData;
import xiaozhi.common.service.BaseService; import xiaozhi.common.service.BaseService;
import xiaozhi.modules.sys.dto.AdminPageUserDTO;
import xiaozhi.modules.sys.dto.PasswordDTO; import xiaozhi.modules.sys.dto.PasswordDTO;
import xiaozhi.modules.sys.dto.SysUserDTO; import xiaozhi.modules.sys.dto.SysUserDTO;
import xiaozhi.modules.sys.entity.SysUserEntity; import xiaozhi.modules.sys.entity.SysUserEntity;
import xiaozhi.modules.sys.vo.AdminPageUserVO;
/** /**
@@ -19,5 +22,30 @@ public interface SysUserService extends BaseService<SysUserEntity> {
void delete(Long[] ids); void delete(Long[] ids);
/**
* 验证是否允许修改密码更改
* @param userId 用户id
* @param passwordDTO 验证密码的参数
*/
void changePassword(Long userId, PasswordDTO passwordDTO); void changePassword(Long userId, PasswordDTO passwordDTO);
/**
* 直接修改密码,不需要验证
* @param userId 用户id
* @param password 密码
*/
void changePasswordDirectly(Long userId, String password);
/**
* 重置密码
* @param userId 用户id
* @return 随机生成符合规范的密码
*/
String resetPassword(Long userId);
/**
* 管理员分页用户信息
* @param dto 分页查找参数
* @return 用户列表分页数据
*/
PageData<AdminPageUserVO> page(AdminPageUserDTO dto);
} }
@@ -1,23 +1,28 @@
package xiaozhi.modules.sys.service.impl; package xiaozhi.modules.sys.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import xiaozhi.common.constant.Constant;
import xiaozhi.common.exception.ErrorCode; import xiaozhi.common.exception.ErrorCode;
import xiaozhi.common.exception.RenException; import xiaozhi.common.exception.RenException;
import xiaozhi.common.page.PageData;
import xiaozhi.common.service.impl.BaseServiceImpl; import xiaozhi.common.service.impl.BaseServiceImpl;
import xiaozhi.common.utils.ConvertUtils; import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.modules.security.password.PasswordUtils; import xiaozhi.modules.security.password.PasswordUtils;
import xiaozhi.modules.sys.dao.SysUserDao; import xiaozhi.modules.sys.dao.SysUserDao;
import xiaozhi.modules.sys.dto.AdminPageUserDTO;
import xiaozhi.modules.sys.dto.PasswordDTO; import xiaozhi.modules.sys.dto.PasswordDTO;
import xiaozhi.modules.sys.dto.SysUserDTO; import xiaozhi.modules.sys.dto.SysUserDTO;
import xiaozhi.modules.sys.entity.SysUserEntity; import xiaozhi.modules.sys.entity.SysUserEntity;
import xiaozhi.modules.sys.enums.SuperAdminEnum; import xiaozhi.modules.sys.enums.SuperAdminEnum;
import xiaozhi.modules.sys.service.SysUserService; import xiaozhi.modules.sys.service.SysUserService;
import xiaozhi.modules.sys.vo.AdminPageUserVO;
import java.util.Arrays; import java.util.*;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -80,9 +85,11 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
public void delete(Long[] ids) { public void delete(Long[] ids) {
//删除用户 //删除用户
baseDao.deleteBatchIds(Arrays.asList(ids)); baseDao.deleteBatchIds(Arrays.asList(ids));
//TODO 除了要删除用户还要删除用户关联的设备,对话,智能体。等此3个功能完善在添加
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void changePassword(Long userId, PasswordDTO passwordDTO) { public void changePassword(Long userId, PasswordDTO passwordDTO) {
SysUserEntity sysUserEntity = sysUserDao.selectById(userId); SysUserEntity sysUserEntity = sysUserDao.selectById(userId);
@@ -107,11 +114,50 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
updateById(sysUserEntity); updateById(sysUserEntity);
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void changePasswordDirectly(Long userId, String password) {
SysUserEntity sysUserEntity = new SysUserEntity();
sysUserEntity.setId(userId);
sysUserEntity.setPassword(PasswordUtils.encode(password));
updateById(sysUserEntity);
}
@Override
@Transactional(rollbackFor = Exception.class)
public String resetPassword(Long userId) {
String password = generatePassword();
changePasswordDirectly(userId,password);
return password;
}
private Long getUserCount() { private Long getUserCount() {
QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
return baseDao.selectCount(queryWrapper); return baseDao.selectCount(queryWrapper);
} }
@Override
public PageData<AdminPageUserVO> page(AdminPageUserDTO dto) {
Map<String, Object> params = new HashMap<String, Object>();
params.put(Constant.PAGE, dto.getPage());
params.put(Constant.LIMIT,dto.getLimit());
IPage<SysUserEntity> page = baseDao.selectPage(
getPage(params, "id", true),
//定义查询条件
new QueryWrapper<SysUserEntity>()
//必须按照手机号码查找
.eq(StringUtils.isNotBlank(dto.getMobile()),"username",dto.getMobile()));
List<AdminPageUserVO> list = page.getRecords().stream().map(user -> {
AdminPageUserVO adminPageUserVO = new AdminPageUserVO();
adminPageUserVO.setUserid(user.getId().toString());
adminPageUserVO.setMobile(user.getUsername());
//TODO 2. 等设备功能写好,获取对应数据
adminPageUserVO.setDeviceCount("0");
return adminPageUserVO;
}).toList();
return new PageData<>(list, page.getTotal());
}
private boolean isStrongPassword(String password) { private boolean isStrongPassword(String password) {
// 弱密码的正则表达式 // 弱密码的正则表达式
@@ -120,4 +166,19 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
Matcher matcher = pattern.matcher(password); Matcher matcher = pattern.matcher(password);
return matcher.matches(); return matcher.matches();
} }
private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
private static final Random random = new Random();
/**
* 生成随机密码
* @return 随机生成的密码
*/
private String generatePassword(){
StringBuilder password = new StringBuilder();
for (int i = 0; i < 10; i++) {
int randomIndex = random.nextInt(CHARACTERS.length());
password.append(CHARACTERS.charAt(randomIndex));
}
return password.toString();
}
} }
@@ -0,0 +1,22 @@
package xiaozhi.modules.sys.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 管理员分页展示用户的VO
* @ zjy
* @since 2025-3-25
*/
@Data
public class AdminPageUserVO {
@Schema(description = "设备数量")
private String deviceCount;
@Schema(description = "手机号码")
private String mobile;
@Schema(description = "用户id")
private String userid;
}
@@ -7,6 +7,8 @@ import xiaozhi.modules.timbre.dto.TimbrePageDTO;
import xiaozhi.modules.timbre.entity.TimbreEntity; import xiaozhi.modules.timbre.entity.TimbreEntity;
import xiaozhi.modules.timbre.vo.TimbreDetailsVO; import xiaozhi.modules.timbre.vo.TimbreDetailsVO;
import java.util.List;
/** /**
* 音色的业务层的定义 * 音色的业务层的定义
@@ -46,4 +48,6 @@ public interface TimbreService extends BaseService<TimbreEntity> {
* @param ids 需要被删除的音色id列表 * @param ids 需要被删除的音色id列表
*/ */
void delete(Long[] ids); void delete(Long[] ids);
List<String> getVoiceNames(String ttsModelId, String voiceName);
} }
@@ -1,7 +1,9 @@
package xiaozhi.modules.timbre.service.impl; package xiaozhi.modules.timbre.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -12,12 +14,13 @@ import xiaozhi.common.utils.ConvertUtils;
import xiaozhi.modules.timbre.dao.TimbreDao; import xiaozhi.modules.timbre.dao.TimbreDao;
import xiaozhi.modules.timbre.dto.TimbreDataDTO; import xiaozhi.modules.timbre.dto.TimbreDataDTO;
import xiaozhi.modules.timbre.dto.TimbrePageDTO; import xiaozhi.modules.timbre.dto.TimbrePageDTO;
import xiaozhi.modules.timbre.service.TimbreService;
import xiaozhi.modules.timbre.entity.TimbreEntity; import xiaozhi.modules.timbre.entity.TimbreEntity;
import xiaozhi.modules.timbre.service.TimbreService;
import xiaozhi.modules.timbre.vo.TimbreDetailsVO; import xiaozhi.modules.timbre.vo.TimbreDetailsVO;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@@ -25,9 +28,11 @@ import java.util.Map;
* @author zjy * @author zjy
* @since 2025-3-21 * @since 2025-3-21
*/ */
@AllArgsConstructor
@Service @Service
public class TimbreServiceImpl extends BaseServiceImpl<TimbreDao, TimbreEntity> implements TimbreService { public class TimbreServiceImpl extends BaseServiceImpl<TimbreDao, TimbreEntity> implements TimbreService {
private final TimbreDao timbreDao;
@Override @Override
public PageData<TimbreDetailsVO> page(TimbrePageDTO dto) { public PageData<TimbreDetailsVO> page(TimbrePageDTO dto) {
@@ -76,6 +81,21 @@ public class TimbreServiceImpl extends BaseServiceImpl<TimbreDao, TimbreEntity>
baseDao.deleteBatchIds(Arrays.asList(ids)); baseDao.deleteBatchIds(Arrays.asList(ids));
} }
@Override
public List<String> getVoiceNames(String ttsModelId, String voiceName) {
QueryWrapper<TimbreEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("ttsModelId", StringUtils.isBlank(ttsModelId) ? "" : ttsModelId);
if (StringUtils.isNotBlank(voiceName)) {
queryWrapper.like("name", voiceName);
}
List<TimbreEntity> timbreEntities = timbreDao.selectList(queryWrapper);
if (CollectionUtil.isEmpty(timbreEntities)) {
return null;
}
return timbreEntities.stream().map(TimbreEntity::getName).toList();
}
/** /**
* 处理是不是tts模型的id * 处理是不是tts模型的id
*/ */
@@ -29,7 +29,7 @@ spring:
database: 0 database: 0
host: 127.0.0.1 host: 127.0.0.1
port: 6379 port: 6379
password: # 密码(默认为空) password: # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒) timeout: 6000ms # 连接超时时长(毫秒)
lettuce: lettuce:
pool: pool:
@@ -39,7 +39,6 @@ spring:
min-idle: 5 # 连接池中的最小空闲连接 min-idle: 5 # 连接池中的最小空闲连接
main: main:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
knife4j: knife4j:
enable: true enable: true
basic: basic:
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xiaozhi.modules.model.dao.ModelConfigDao">
<!-- 获取模型供应器字段 -->
<select id="getModelCodeList" resultType="String">
select model_name from ai_model_config where model_type = #{modelType}
<if test="modelName != null">and model_name = #{modelName}</if>
</select>
</mapper>
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xiaozhi.modules.model.dao.ModelProviderDao">
<!-- 获取模型供应器字段 -->
<select id="getFieldList" resultType="string">
select fields from ai_model_provider where model_type = #{modelType} and provider_code = #{providerCode};
</select>
</mapper>
+1 -1
View File
@@ -1,6 +1,6 @@
// 引入各个模块的请求 // 引入各个模块的请求
import user from './module/user.js' import user from './module/user.js'
import admin from './module/admin.js'
/** /**
* 接口地址 * 接口地址
* 当前8002端口接口还没开发完成,暂时用 apifoxmock 接口代替 * 当前8002端口接口还没开发完成,暂时用 apifoxmock 接口代替
+20
View File
@@ -0,0 +1,20 @@
import RequestService from '../httpRequest'
import {getServiceUrl} from '../api'
export default {
// 用户列表
getUserList(callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/admin/users`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime()
callback(res)
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.getList()
})
}).send()
},
}
+143 -101
View File
@@ -5,7 +5,8 @@ import {getServiceUrl} from '../api'
export default { export default {
// 登录 // 登录
login(loginForm, callback) { login(loginForm, callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/login`) RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/login`)
.method('POST') .method('POST')
.data(loginForm) .data(loginForm)
.success((res) => { .success((res) => {
@@ -18,51 +19,6 @@ export default {
}) })
}).send() }).send()
}, },
// 获取设备信息
getHomeList(callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/device/bind`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime()
callback(res)
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.getUserInfo()
})
}).send()
},
// 解绑设备
unbindDevice(device_id, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/device/unbind/${device_id}`)
.method('PUT')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.unbindDevice(device_id, callback);
});
}).send()
},
// 绑定设备
bindDevice(deviceCode, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/device/bind/${deviceCode}`)
.method('POST')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('绑定设备失败:', err);
RequestService.reAjaxFun(() => {
this.bindDevice(deviceCode, callback);
});
}).send();
},
// 获取验证码 // 获取验证码
getCaptcha(uuid, callback) { getCaptcha(uuid, callback) {
@@ -71,9 +27,9 @@ export default {
.method('GET') .method('GET')
.type('blob') .type('blob')
.header({ .header({
'Content-Type': 'image/gif', 'Content-Type': 'image/gif',
'Pragma': 'No-cache', 'Pragma': 'No-cache',
'Cache-Control': 'no-cache' 'Cache-Control': 'no-cache'
}) })
.success((res) => { .success((res) => {
RequestService.clearRequestTime(); RequestService.clearRequestTime();
@@ -85,7 +41,9 @@ export default {
}, },
// 注册账号 // 注册账号
register(registerForm, callback) { register(registerForm, callback) {
RequestService.sendRequest().url(`${getServiceUrl()}/api/v1/user/register`).method('POST') RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/register`)
.method('POST')
.data(registerForm) .data(registerForm)
.success((res) => { .success((res) => {
RequestService.clearRequestTime() RequestService.clearRequestTime()
@@ -112,53 +70,6 @@ export default {
}); });
}).send(); }).send();
}, },
// 获取设备配置
getDeviceConfig(device_id, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/configDevice/${device_id}`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('获取配置失败:', err);
RequestService.reAjaxFun(() => {
this.getDeviceConfig(device_id, callback);
});
}).send();
},
// 获取所有模型名称
getModelNames(callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/models/names`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.getModelNames(callback);
});
}).send();
},
// 获取模型音色
getModelVoices(modelName, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/models/${modelName}/voices`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.getModelVoices(modelName, callback);
});
}).send();
},
// 获取智能体列表 // 获取智能体列表
getAgentList(callback) { getAgentList(callback) {
RequestService.sendRequest() RequestService.sendRequest()
@@ -173,8 +84,8 @@ export default {
this.getAgentList(callback); this.getAgentList(callback);
}); });
}).send(); }).send();
}, },
// 用户信息获取
getUserInfo(callback) { getUserInfo(callback) {
RequestService.sendRequest() RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/info`) .url(`${getServiceUrl()}/api/v1/user/info`)
@@ -195,7 +106,7 @@ export default {
RequestService.sendRequest() RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent`) .url(`${getServiceUrl()}/api/v1/user/agent`)
.method('POST') .method('POST')
.data({ name: agentName }) .data({name: agentName})
.success((res) => { .success((res) => {
RequestService.clearRequestTime(); RequestService.clearRequestTime();
callback(res); callback(res);
@@ -206,5 +117,136 @@ export default {
}); });
}).send(); }).send();
}, },
// 删除智能体
deleteAgent(agentId, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/${agentId}`)
.method('DELETE')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.deleteAgent(agentId, callback);
});
}).send();
},
// 修改用户密码
changePassword(oldPassword, newPassword, successCallback, errorCallback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/change-password`)
.method('PUT')
.data({
old_password: oldPassword,
new_password: newPassword,
})
.success((res) => {
RequestService.clearRequestTime();
successCallback(res);
})
.fail((error) => {
RequestService.reAjaxFun(() => {
this.changePassword(oldPassword, newPassword, successCallback, errorCallback);
});
})
.send();
},
// 获取智能体配置
getDeviceConfig(deviceId, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/${deviceId}`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('获取配置失败:', err);
RequestService.reAjaxFun(() => {
this.getDeviceConfig(deviceId, callback);
});
}).send();
},
// 配置智能体
updateAgentConfig(agentId, configData, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/${agentId}`)
.method('PUT')
.data(configData)
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail(() => {
RequestService.reAjaxFun(() => {
this.updateAgentConfig(agentId, configData, callback);
});
}).send();
},
// 已绑设备
getAgentBindDevices(agentId, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/device/bind/${agentId}`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('获取设备列表失败:', err);
RequestService.reAjaxFun(() => {
this.getAgentBindDevices(agentId, callback);
});
}).send();
},
// 解绑设备
unbindDevice(device_id, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/device/unbind/${device_id}`)
.method('PUT')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('解绑设备失败:', err);
RequestService.reAjaxFun(() => {
this.unbindDevice(device_id, callback);
});
}).send();
},
// 绑定设备
bindDevice(agentId, code, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/device/bind/${agentId}`)
.method('POST')
.data({ code })
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('绑定设备失败:', err);
RequestService.reAjaxFun(() => {
this.bindDevice(agentId, code, callback);
});
}).send();
},
// 新增方法:获取智能体模板
getAgentTemplate(templateName, callback) {
RequestService.sendRequest()
.url(`${getServiceUrl()}/api/v1/user/agent/templateId`)
.method('GET')
.success((res) => {
RequestService.clearRequestTime();
callback(res);
})
.fail((err) => {
console.error('获取模板失败:', err);
RequestService.reAjaxFun(() => {
this.getAgentTemplate(templateName, callback);
});
}).send();
},
} }
Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

@@ -33,16 +33,46 @@
export default { export default {
name: 'AddDeviceDialog', name: 'AddDeviceDialog',
props: { props: {
visible: { type: Boolean, required: true } visible: { type: Boolean, required: true },
agentId: { type: String, required: true }
}, },
data() { data() {
return { deviceCode: "" } return {
deviceCode: "",
loading: false,
}
}, },
methods: { methods: {
confirm() { confirm() {
this.$emit('update:visible', false) if (!/^\d{6}$/.test(this.deviceCode)) {
this.$emit('added', this.deviceCode) this.$message.error('请输入6位数字验证码');
this.deviceCode = "" return;
}
this.loading = true;
import('@/apis/module/user').then(({ default: userApi }) => {
userApi.bindDevice(
this.agentId,
this.deviceCode, ({data}) => {
this.loading = false;
if (data.code === 0) {
this.$emit('refresh');
this.$message.success('设备绑定成功');
this.closeDialog();
} else {
this.$message.error(data.msg || '绑定失败');
}
}
);
}).catch((err) => {
this.loading = false;
console.error('API模块加载失败:', err);
this.$message.error('绑定服务不可用');
});
},
closeDialog() {
this.$emit('update:visible', false);
this.deviceCode = '';
}, },
cancel() { cancel() {
this.$emit('update:visible', false) this.$emit('update:visible', false)
@@ -0,0 +1,387 @@
<template>
<el-dialog
:visible.sync="visible"
width="975px"
center
custom-class="custom-dialog"
:show-close="false"
class="center-dialog"
>
<div style="margin: 0 18px; text-align: left; padding: 10px; border-radius: 10px;">
<div style="font-size: 30px; color: #3d4566; margin-top: -10px; margin-bottom: 10px; text-align: center;">
添加模型
</div>
<!-- 关闭按钮 -->
<button class="custom-close-btn" @click="handleClose">
×
</button>
<!-- 模型信息部分 -->
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
<div style="font-size: 20px; font-weight: bold; color: #3d4566;">模型信息</div>
<div style="display: flex; align-items: center; gap: 20px;">
<div style="display: flex; align-items: center;">
<span style="margin-right: 8px;">是否启用</span>
<el-switch v-model="formData.isEnabled" class="custom-switch"></el-switch>
</div>
<div style="display: flex; align-items: center;">
<span style="margin-right: 8px;">设为默认</span>
<el-switch v-model="formData.isDefault" class="custom-switch"></el-switch>
</div>
</div>
</div>
<div style="height: 2px; background: #e9e9e9; margin-bottom: 22px;"></div>
<el-form :model="formData" label-width="100px" label-position="left" class="custom-form">
<!-- 第一行模型名称和模型编码 -->
<div style="display: flex; gap: 20px; margin-bottom: 0;">
<el-form-item label="模型名称" prop="modelName" style="flex: 1;">
<el-input
v-model="formData.modelName"
placeholder="请输入模型名称"
class="custom-input-bg"
></el-input>
</el-form-item>
<el-form-item label="模型编码" prop="modelCode" style="flex: 1;">
<el-input
v-model="formData.modelCode"
placeholder="请输入模型编码"
class="custom-input-bg"
></el-input>
</el-form-item>
</div>
<!-- 第二行供应器和排序号 -->
<div style="display: flex; gap: 20px; margin-bottom: 0;">
<el-form-item label="供应器" prop="supplier" style="flex: 1;">
<el-select
v-model="formData.supplier"
placeholder="请选择"
class="custom-select custom-input-bg"
style="width: 100%;"
>
<el-option label="硅基流动" value="硅基流动"></el-option>
<el-option label="智脑科技" value="智脑科技"></el-option>
<el-option label="云智科技" value="云智科技"></el-option>
<el-option label="其他" value="其他"></el-option>
</el-select>
</el-form-item>
<el-form-item label="排序号" prop="sortOrder" style="flex: 1;">
<el-input
v-model="formData.sort"
placeholder="请输入排序号"
class="custom-input-bg"
></el-input>
</el-form-item>
</div>
<!-- 文档地址 -->
<el-form-item label="文档地址" prop="docLink" style="margin-bottom: 27px;">
<el-input
v-model="formData.docLink"
placeholder="请输入文档地址"
class="custom-input-bg"
></el-input>
</el-form-item>
<!-- 备注 -->
<el-form-item label="备注" prop="remark" class="prop-remark">
<el-input
v-model="formData.remark"
type="textarea"
:rows="3"
placeholder="请输入模型备注"
class="custom-input-bg"
></el-input>
</el-form-item>
</el-form>
<!-- 调用信息部分 -->
<div style="font-size: 20px; font-weight: bold; color: #3d4566; margin-bottom: 15px;">调用信息</div>
<div style="height: 2px; background: #e9e9e9; margin-bottom: 15px;"></div>
<el-form :model="formData" label-width="100px" label-position="left" class="custom-form">
<!-- 第一行模型名称和接口地址 -->
<div style="display: flex; gap: 10px; margin-bottom: 15px;">
<el-form-item label="模型名称" prop="param1" style="flex: 0.5; margin-bottom: 0;">
<el-input
v-model="formData.configJson.param1"
placeholder="请输入model_name"
class="custom-input-bg"
></el-input>
</el-form-item>
<el-form-item label="接口地址" prop="param2" style="flex: 1; margin-bottom: 0;">
<el-input
v-model="formData.configJson.param2"
placeholder="请输入base_url"
class="custom-input-bg"
></el-input>
</el-form-item>
</div>
<!-- 秘钥信息 -->
<el-form-item label="秘钥信息" prop="apiKey">
<el-input
v-model="formData.configJson.apiKey"
placeholder="请输入api_key"
show-password
class="custom-input-bg"
></el-input>
</el-form-item>
</el-form>
</div>
<!-- 保存按钮 -->
<div style="display: flex;justify-content: center;">
<el-button
type="primary"
@click="confirm"
class="save-btn"
>
保存
</el-button>
</div>
</el-dialog>
</template>
<script>
export default {
name: 'AddModelDialog',
props: {
visible: {type: Boolean, required: true},
modelType: {type: String, required: true}
},
data() {
return {
formData: {
modelName: '',
modelCode: '',
supplier: '',
sort: 1,
docLink: '',
remark: '',
isEnabled: true,
isDefault: true,
configJson: {
param1: '',
param2: '',
apiKey: ''
}
}
}
},
methods: {
confirm() {
if (!this.formData.modelName || !this.formData.modelCode || !this.formData.supplier ||
!this.formData.configJson.param1 || !this.formData.configJson.param2 || !this.formData.configJson.apiKey) {
this.$message.error('请填写所有必填字段');
return;
}
this.$emit('confirm', {
...this.formData,
provideType: this.formData.supplier
});
this.$emit('update:visible', false);
this.resetForm();
},
resetForm() {
this.formData = {
modelName: '',
modelCode: '',
supplier: '',
sort: 1,
docLink: '',
remark: '',
isEnabled: true,
isDefault: true,
configJson: {
param1: '',
param2: '',
apiKey: ''
}
};
},
handleClose() {
this.resetForm();
this.$emit('update:visible', false);
}
}
}
</script>
<style>
.custom-dialog {
position: relative;
border-radius: 20px;
overflow: hidden;
background: white;
padding-bottom: 17px;
}
.custom-dialog .el-dialog__header {
padding: 0;
border-bottom: none;
}
.center-dialog {
display: flex;
align-items: center;
justify-content: center;
}
.center-dialog .el-dialog {
margin: 4% 0 auto !important;
display: flex;
flex-direction: column;
}
.custom-close-btn {
position: absolute;
top: 20px;
right: 20px;
width: 35px;
height: 35px;
border-radius: 50%;
border: 2px solid #cfcfcf;
background: none;
font-size: 30px;
font-weight: lighter;
color: #cfcfcf;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
z-index: 1;
padding: 0;
outline: none;
}
.custom-close-btn:hover {
color: #409EFF;
border-color: #409EFF;
}
.custom-select .el-input__suffix {
background: #e6e8ea;
right: 6px;
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
top: 9px;
}
.custom-select .el-input__suffix-inner {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
}
.custom-select .el-icon-arrow-up:before {
content: "";
display: inline-block;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 7px solid #c0c4cc;
position: relative;
top: -2px;
transform: rotate(180deg);
}
.custom-form .el-form-item {
margin-bottom: 20px; /* 统一设置所有表单项的间距 */
}
.custom-form .el-form-item__label {
color: #3d4566;
font-weight: normal;
text-align: right;
padding-right: 20px;
}
.custom-form .el-form-item.prop-remark .el-form-item__label {
margin-top: -4px;
}
/* 修改placeholder颜色 */
.custom-input-bg .el-input__inner::-webkit-input-placeholder,
.custom-input-bg .el-textarea__inner::-webkit-input-placeholder {
color: #9c9f9e;
}
/* 输入框背景色 */
.custom-input-bg .el-input__inner,
.custom-input-bg .el-textarea__inner {
background-color: #f6f8fc;
}
.save-btn {
background: #e6f0fd;
color: #237ff4;
border: 1px solid #b3d1ff;
width: 150px;
height: 40px;
font-size: 16px;
transition: all 0.3s ease;
}
.save-btn:hover {
background: linear-gradient(to right, #237ff4, #9c40d5);
color: white;
border: none;
}
/* 修改开关样式 */
.custom-switch .el-switch__core {
border-radius: 20px;
height: 23px;
background-color: #c0ccda;
width: 35px;
padding: 0 20px; /* 调整左右内边距 */
}
.custom-switch .el-switch__core:after {
width: 15px;
height: 15px;
background-color: white;
top: 3px;
left: 4px;
transition: all .3s;
}
.custom-switch.is-checked .el-switch__core {
border-color: #b5bcf0;
background-color: #cfd7fa;
padding: 0 20px; /* 确保启用状态也有相同的间隔 */
}
.custom-switch.is-checked .el-switch__core:after {
left: 100%;
margin-left: -18px;
background-color: #1b47ee;
}
/* 调整flex布局的gap */
[style*="display: flex"] {
gap: 20px; /* 扩大flex项间距 */
}
/* 调整输入框高度 */
.custom-input-bg .el-input__inner {
height: 32px; /* 固定输入框高度 */
}
</style>
@@ -0,0 +1,141 @@
<template>
<el-dialog :visible.sync="visible" width="400px" center>
<div
style="margin: 0 10px 10px;display: flex;align-items: center;gap: 10px;font-weight: 700;font-size: 20px;text-align: left;color: #3d4566;">
<div
style="width: 40px;height: 40px;border-radius: 50%;background: #5778ff;display: flex;align-items: center;justify-content: center;">
<img src="@/assets/login/shield.png" alt="" style="width: 19px;height: 23px; filter: brightness(0) invert(1);" />
</div>
修改密码
</div>
<div style="height: 1px;background: #e8f0ff;"/>
<div style="margin: 22px 15px;">
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;">
<div style="color: red;display: inline-block;">*</div>
旧密码
</div>
<div class="input-46" style="margin-top: 12px;">
<el-input placeholder="请输入旧密码" v-model="oldPassword" type="password" show-password/>
</div>
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
<div style="color: red;display: inline-block;">*</div>
新密码
</div>
<div class="input-46" style="margin-top: 12px;">
<el-input placeholder="请输入新密码" v-model="newPassword" type="password" show-password/>
</div>
<div style="font-weight: 400;font-size: 14px;text-align: left;color: #3d4566;margin-top: 12px;">
<div style="color: red;display: inline-block;">*</div>
确认新密码
</div>
<div class="input-46" style="margin-top: 12px;">
<el-input placeholder="请再次输入新密码" v-model="confirmNewPassword" type="password" show-password/>
</div>
</div>
<div style="display: flex;margin: 15px 15px;gap: 7px;">
<div class="dialog-btn" @click="confirm">
确定
</div>
<div class="dialog-btn"
style="background: #e6ebff;border: 1px solid #adbdff;color: #5778ff;"
@click="cancel">
取消
</div>
</div>
</el-dialog>
</template>
<script>
import userApi from '@/apis/module/user';
export default {
name: 'ChangePasswordDialog',
props: {
visible: {type: Boolean, required: true}
},
data() {
return {
oldPassword: "",
newPassword: "",
confirmNewPassword: ""
}
},
methods: {
confirm() {
if (!this.oldPassword.trim() || !this.newPassword.trim() || !this.confirmNewPassword.trim()) {
this.$message.error('请填写所有字段');
return;
}
if (this.newPassword !== this.confirmNewPassword) {
this.$message.error('两次输入的新密码不一致');
return;
}
if (this.newPassword === this.oldPassword) {
this.$message.error('新密码不能与旧密码相同');
return;
}
// 直接调用修改密码接口
userApi.changePassword(this.oldPassword, this.newPassword, (res) => {
if (res.code === 0) {
this.$message.error(res.msg || '密码修改失败');
} else {
this.$message.success('密码修改成功');
this.$emit('confirm', res);
this.$emit('update:visible', false);
this.resetForm();
}
}, (err) => {
this.$message.error(err.msg || '密码修改失败');
});
},
cancel() {
this.$emit('update:visible', false);
this.resetForm();
},
resetForm() {
this.oldPassword = "";
this.newPassword = "";
this.confirmNewPassword = "";
}
}
}
</script>
<style scoped>
.input-46 {
border: 1px solid #e4e6ef;
background: #f6f8fb;
border-radius: 15px;
}
.dialog-btn {
cursor: pointer;
flex: 1;
border-radius: 23px;
background: #5778ff;
height: 40px;
font-weight: 500;
font-size: 12px;
color: #fff;
line-height: 40px;
text-align: center;
}
::v-deep .el-dialog {
border-radius: 15px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
::v-deep .el-dialog__headerbtn {
display: none;
}
::v-deep .el-dialog__body {
padding: 4px 6px;
}
::v-deep .el-dialog__header {
padding: 10px;
}
</style>
+14 -3
View File
@@ -6,7 +6,7 @@
</div> </div>
<div> <div>
<img src="@/assets/home/delete.png" alt="" <img src="@/assets/home/delete.png" alt=""
style="width: 18px;height: 18px;margin-right: 10px;" /> style="width: 18px;height: 18px;margin-right: 10px;" @click.stop="handleDelete" />
<img src="@/assets/home/info.png" alt="" style="width: 18px;height: 18px;" /> <img src="@/assets/home/info.png" alt="" style="width: 18px;height: 18px;" />
</div> </div>
</div> </div>
@@ -17,7 +17,7 @@
音色模型{{ device.ttsVoiceName }} 音色模型{{ device.ttsVoiceName }}
</div> </div>
<div style="display: flex;gap: 10px;align-items: center;"> <div style="display: flex;gap: 10px;align-items: center;">
<div class="settings-btn" @click="$emit('configure')"> <div class="settings-btn" @click="handleConfigure">
配置角色 配置角色
</div> </div>
<div class="settings-btn"> <div class="settings-btn">
@@ -26,7 +26,7 @@
<div class="settings-btn"> <div class="settings-btn">
历史对话 历史对话
</div> </div>
<div class="settings-btn" @click="$emit('deviceManage')"> <div class="settings-btn" @click="handleDeviceManage">
设备管理 设备管理
</div> </div>
</div> </div>
@@ -44,6 +44,17 @@ export default {
}, },
data() { data() {
return { switchValue: false } return { switchValue: false }
},
methods: {
handleDelete() {
this.$emit('delete', this.device.agentId)
},
handleConfigure() {
this.$router.push({ path: '/role-config', query: { agentId: this.device.agentId } });
},
handleDeviceManage() {
this.$router.push({ path: '/device-management', query: { agentId: this.device.agentId } });
}
} }
} }
</script> </script>
+182 -72
View File
@@ -1,49 +1,68 @@
<template> <template>
<el-header class="header"> <el-header class="header">
<div style="display: flex;justify-content: space-between;margin-top: 6px; "> <div class="header-container">
<div style="display: flex;align-items: center;gap: 10px;"> <!-- 左侧元素 -->
<img alt="" src="@/assets/xiaozhi-logo.png" style="width: 42px;height: 42px;"/> <div class="header-left">
<img alt="" src="@/assets/xiaozhi-ai.png" style="width: 58px;height: 12px;"/> <img alt="" src="@/assets/xiaozhi-logo.png" class="logo-img"/>
<div class="equipment-management" @click="goHome"> <img alt="" src="@/assets/xiaozhi-ai.png" class="brand-img"/>
<img alt="" src="@/assets/home/equipment.png" style="width: 12px;height: 10px;"/> </div>
<!-- 中间导航菜单 -->
<div class="header-center">
<div class="equipment-management" :class="{ 'active-tab': $route.path === '/home' }" @click="goHome">
<img alt="" src="@/assets/header/roboot.png" :style="{ filter: $route.path === '/home' ? 'brightness(0) invert(1)' : 'None' }"/>
智能体管理 智能体管理
</div> </div>
<div class="equipment-management2" :class="{ 'active-tab': $route.path === '/user-management' }" @click="goUserManagement"> <div class="equipment-management" :class="{ 'active-tab': $route.path === '/user-management' }" @click="goUserManagement">
<img alt="" src="@/assets/header/user_management.png" :style="{ filter: $route.path === '/user-management' ? 'brightness(0) invert(1)' : 'None' }"/>
用户管理 用户管理
</div> </div>
<div class="equipment-management2" :class="{ 'active-tab': $route.path === '/model-config' }" @click="goModelConfig"> <div class="equipment-management" :class="{ 'active-tab': $route.path === '/model-config' }" @click="goModelConfig">
<img alt="" src="@/assets/header/model_config.png" :style="{ filter: $route.path === '/model-config' ? 'brightness(0) invert(1)' : 'None' }"/>
模型配置 模型配置
</div> </div>
</div> </div>
<div style="display: flex;align-items: center;gap: 7px; margin-top: 2px;">
<div class="serach-box"> <!-- 右侧元素 -->
<el-input v-model="serach" placeholder="输入名称搜索.." style="border: none; background: transparent;" <div class="header-right">
@keyup.enter.native="handleSearch"/> <div class="search-container">
<img alt="" src="@/assets/home/search.png" <el-input
style="width: 14px;height: 14px;margin-right: 11px;cursor: pointer;" @click="handleSearch"/> v-model="serach"
placeholder="输入名称搜索.."
class="custom-search-input"
@keyup.enter.native="handleSearch"
>
<i slot="suffix" class="el-icon-search search-icon" @click="handleSearch"></i>
</el-input>
</div> </div>
<img alt="" src="@/assets/home/avatar.png" style="width: 21px;height: 21px;"/> <img alt="" src="@/assets/home/avatar.png" class="avatar-img"/>
<el-dropdown trigger="click"> <el-dropdown trigger="click" class="user-dropdown">
<span class="el-dropdown-link"> <span class="el-dropdown-link">
{{ userInfo.username || '加载中...' }}<i class="el-icon-arrow-down el-icon--right"></i> {{ userInfo.mobile || '加载中...' }}<i class="el-icon-arrow-down el-icon--right"></i>
</span> </span>
<el-dropdown-menu slot="dropdown"> <el-dropdown-menu slot="dropdown">
<el-dropdown-item icon="el-icon-plus" @click.native="">个人中心</el-dropdown-item> <el-dropdown-item icon="el-icon-plus" @click.native="">个人中心</el-dropdown-item>
<el-dropdown-item icon="el-icon-circle-plus" @click.native="">修改密码</el-dropdown-item> <el-dropdown-item icon="el-icon-circle-plus" @click.native="showChangePasswordDialog">修改密码</el-dropdown-item>
<el-dropdown-item icon="el-icon-circle-plus-outline" @click.native="">退出登录</el-dropdown-item> <el-dropdown-item icon="el-icon-circle-plus-outline" @click.native="">退出登录</el-dropdown-item>
</el-dropdown-menu> </el-dropdown-menu>
</el-dropdown> </el-dropdown>
</div> </div>
</div> </div>
<!-- 修改密码弹窗 -->
<ChangePasswordDialog :visible.sync="isChangePasswordDialogVisible"/>
</el-header> </el-header>
</template> </template>
<script> <script>
import userApi from '@/apis/module/user' import userApi from '@/apis/module/user';
import ChangePasswordDialog from './ChangePasswordDialog.vue'; // 引入修改密码弹窗组件
export default { export default {
name: 'HeaderBar', name: 'HeaderBar',
components: {
ChangePasswordDialog
},
props: ['devices'], // 接收父组件设备列表 props: ['devices'], // 接收父组件设备列表
data() { data() {
return { return {
@@ -51,7 +70,8 @@ export default {
userInfo: { userInfo: {
username: '', username: '',
mobile: '' mobile: ''
} },
isChangePasswordDialogVisible: false // 控制修改密码弹窗的显示
} }
}, },
mounted() { mounted() {
@@ -93,38 +113,72 @@ export default {
} }
this.$emit('search-result', filteredDevices); this.$emit('search-result', filteredDevices);
},
// 显示修改密码弹窗
showChangePasswordDialog() {
this.isChangePasswordDialogVisible = true;
} }
} }
} }
</script> </script>
<style scoped> <style scoped>
.equipment-management, .header {
.equipment-management2 { background: #f6fcfe66;
cursor: pointer; border: 1px solid #fff;
height: 53px !important;
min-width: 900px; /* 设置最小宽度防止过度压缩 */
overflow: hidden;
}
.header-container {
display: flex;
justify-content: space-between;
align-items: center;
height: 100%;
padding: 0 10px;
}
.header-left {
display: flex;
align-items: center;
gap: 10px;
min-width: 120px;
}
.logo-img {
width: 42px;
height: 42px;
}
.brand-img {
width: 58px;
height: 12px;
}
.header-center {
display: flex;
align-items: center;
gap: 25px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.header-right {
display: flex;
align-items: center;
gap: 7px;
min-width: 300px;
justify-content: flex-end;
} }
.equipment-management { .equipment-management {
width: 82px; width: 82px;
height: 24px; height: 24px;
border-radius: 12px; border-radius: 12px;
background: #5778ff; background: #deeafe;
display: flex;
align-items: center;
justify-content: center;
gap: 7px;
font-weight: 500;
color: #fff;
font-size: 10px;
}
.equipment-management2 {
width: 82px;
height: 24px;
border-radius: 12px;
background: #fff;
display: flex; display: flex;
justify-content: center; justify-content: center;
font-size: 10px; font-size: 10px;
@@ -134,48 +188,104 @@ export default {
margin-left: 1px; margin-left: 1px;
align-items: center; align-items: center;
transition: all 0.3s ease; transition: all 0.3s ease;
cursor: pointer;
flex-shrink: 0; /* 防止导航按钮被压缩 */
} }
.equipment-management2.active-tab { .equipment-management.active-tab {
background: #5778ff !important; background: #5778ff !important;
color: #fff !important; color: #fff !important;
} }
.header { .equipment-management img {
background: #f6fcfe66; width: 15px;
border: 1px solid #fff; height: 13px;
height: 53px !important;
} }
.serach-box { .search-container {
display: flex; margin-right: 15px;
width: 220px; min-width: 150px;
flex-grow: 1;
max-width: 220px;
}
.custom-search-input >>> .el-input__inner {
height: 30px; height: 30px;
border-radius: 15px; border-radius: 15px;
background-color: #f6fcfe66; background-color: #e2e5f8;
border: 1px solid #e4e6ef; border: 1px solid #e4e6ef;
align-items: center; padding-left: 15px;
padding: 0 7px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-right: 15px;
}
.serach-box /deep/ .el-input__inner {
border-radius: 15px;
height: 100%;
width: 100%;
border: 0;
background: transparent;
padding-left: 12px;
}
.user-info {
font-weight: 600;
font-size: 12px; font-size: 12px;
letter-spacing: -0.02px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
text-align: left; width: 100%;
color: #3d4566;
} }
</style> .search-icon {
cursor: pointer;
color: #909399;
margin-right: 8px;
font-size: 14px;
line-height: 30px;
}
.avatar-img {
width: 21px;
height: 21px;
flex-shrink: 0;
}
.user-dropdown {
flex-shrink: 0;
}
/* 响应式调整 */
@media (max-width: 1200px) {
.header-center {
gap: 14px;
}
.equipment-management {
width: 70px;
font-size: 9px;
}
}
@media (max-width: 1024px) {
.search-container {
margin-right: 10px;
max-width: 150px;
}
.header-right {
gap: 5px;
}
}
@media (max-width: 900px) {
.header-left {
margin-right: auto;
}
.search-container {
max-width: 150px;
}
}
@media (max-width: 768px) {
.search-container {
max-width: 145px;
}
.custom-search-input >>> .el-input__inner {
padding-left: 10px;
font-size: 11px;
}
}
@media (max-width: 600px) {
.search-container {
max-width: 120px;
min-width: 100px;
}
}
</style>
+92 -46
View File
@@ -1,20 +1,20 @@
<template> <template>
<div class="welcome"> <div class="welcome">
<HeaderBar /> <HeaderBar />
<el-main style="padding: 20px; display: flex; flex-direction: column;"> <el-main style="padding: 20px; display: flex; flex-direction: column;">
<div class="table-container"> <div class="table-container">
<h3 class="device-list-title">设备列表</h3> <h3 class="device-list-title">设备列表</h3>
<el-button type="primary" class="add-device-btn" @click="handleAddDevice"> <el-button type="primary" class="add-device-btn" @click="handleAddDevice">
+ 添加设备 + 添加设备
</el-button> </el-button>
<el-table :data="deviceList" style="width: 100%; margin-top: 20px" border stripe> <el-table :data="paginatedDeviceList" style="width: 100%; margin-top: 20px" border stripe>
<el-table-column label="设备型号" prop="model" flex></el-table-column> <el-table-column label="设备型号" prop="model" flex></el-table-column>
<el-table-column label="固件版本" prop="firmwareVersion" width="120"></el-table-column> <el-table-column label="固件版本" prop="firmwareVersion" width="120"></el-table-column>
<el-table-column label="Mac地址" prop="macAddress"></el-table-column> <el-table-column label="Mac地址" prop="macAddress"></el-table-column>
<el-table-column label="绑定时间" prop="bindTime" width="200"></el-table-column> <el-table-column label="绑定时间" prop="bindTime" width="200"></el-table-column>
<el-table-column label="最近对话" prop="lastConversation" width="140"></el-table-column> <el-table-column label="最近对话" prop="lastConversation" width="140"></el-table-column>
<el-table-column label="备注" width="180"> <el-table-column label="备注" width="180">
<template slot-scope="scope"> <template slot-scope="scope">
<el-input v-if="scope.row.isEdit" v-model="scope.row.remark" size="mini" @blur="stopEditRemark(scope.$index)"></el-input> <el-input v-if="scope.row.isEdit" v-model="scope.row.remark" size="mini" @blur="stopEditRemark(scope.$index)"></el-input>
<span v-else> <span v-else>
<i v-if="!scope.row.remark" class="el-icon-edit" @click="startEditRemark(scope.$index, scope.row)"></i> <i v-if="!scope.row.remark" class="el-icon-edit" @click="startEditRemark(scope.$index, scope.row)"></i>
@@ -31,17 +31,27 @@
</el-table-column> </el-table-column>
<el-table-column label="操作" width="80"> <el-table-column label="操作" width="80">
<template slot-scope="scope"> <template slot-scope="scope">
<el-button size="mini" type="text" @click="handleUnbind(scope.row)" style="color: #ff4949"> <el-button size="mini" type="text" @click="handleUnbind(scope.row.device_id)" style="color: #ff4949">
解绑 解绑
</el-button> </el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
<el-pagination
class="pagination"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[5, 10, 20, 50]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="deviceList.length"
></el-pagination>
</div> </div>
<div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;"> <div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;">
©2025 xiaozhi-esp32-server ©2025 xiaozhi-esp32-server
</div> </div>
<AddDeviceDialog :visible.sync="addDeviceDialogVisible" @added="handleDeviceAdded" /> <AddDeviceDialog :visible.sync="addDeviceDialogVisible" :agent-id="currentAgentId" @refresh="fetchBindDevices(currentAgentId)" />
</el-main> </el-main>
</div> </div>
</template> </template>
@@ -49,49 +59,37 @@
<script> <script>
import HeaderBar from "@/components/HeaderBar.vue"; import HeaderBar from "@/components/HeaderBar.vue";
import AddDeviceDialog from "@/components/AddDeviceDialog.vue"; import AddDeviceDialog from "@/components/AddDeviceDialog.vue";
export default { export default {
components: {HeaderBar, AddDeviceDialog }, components: {HeaderBar, AddDeviceDialog },
data() { data() {
return { return {
addDeviceDialogVisible: false, addDeviceDialogVisible: false,
deviceList: [ currentAgentId: this.$route.query.agentId || '',
{ currentPage: 1,
model: 'xingzhi-cube-0.96oled-wifi', pageSize: 5,
firmwareVersion: '1.4.6', deviceList: [],
macAddress: 'fc:01:2c:c5:d5:7c', loading: false,
bindTime: '2025-03-10 18:16:21', userApi: null,
lastConversation: '6 天前',
remark: '',
isEdit: false,
otaSwitch: false
},
{
model: 'xingzhi-board-1.3tft-ble',
firmwareVersion: '2.1.0',
macAddress: 'ac:12:3d:e7:f8:9a',
bindTime: '2025-03-12 09:30:15',
lastConversation: '4 天前',
remark: '测试设备',
isEdit: false,
otaSwitch: true
},
{
model: 'xingzhi-kit-0.91oled-4g',
firmwareVersion: '1.8.3',
macAddress: 'bc:45:6f:1e:2d:3c',
bindTime: '2025-03-15 14:22:08',
lastConversation: '2 天前',
remark: '生产环境设备',
isEdit: false,
otaSwitch: false
},
]
}; };
}, },
computed: {
paginatedDeviceList() {
const start = (this.currentPage - 1) * this.pageSize;
const end = start + this.pageSize;
return this.deviceList.slice(start, end);
}
},
mounted() {
const agentId = this.$route.query.agentId;
import('@/apis/module/user').then(({ default: userApi }) => {
this.userApi = userApi;
if (agentId) {
this.fetchBindDevices(agentId);
}
});
},
methods: { methods: {
handleAddDevice() { handleAddDevice() {
// 添加设备逻辑
this.addDeviceDialogVisible = true; this.addDeviceDialogVisible = true;
}, },
startEditRemark(index, row) { startEditRemark(index, row) {
@@ -100,13 +98,57 @@ export default {
stopEditRemark(index) { stopEditRemark(index) {
this.deviceList[index].isEdit = false; this.deviceList[index].isEdit = false;
}, },
handleUnbind(device) { handleUnbind(device_id) {
// 解绑逻辑 if (!this.userApi) {
console.log('解绑设备', device); this.$message.error('功能模块加载失败');
return;
}
this.$confirm('确认要解绑该设备吗?', '警告', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.userApi.unbindDevice(device_id, ({ data }) => {
if (data.code === 0) {
this.$message.success('设备解绑成功');
this.fetchBindDevices(this.$route.query.agentId);
} else {
this.$message.error(data.msg || '设备解绑失败');
}
});
});
}, },
handleDeviceAdded(deviceCode) { handleSizeChange(val) {
console.log('添加的智慧体名称:', deviceCode); this.pageSize = val;
}, },
handleCurrentChange(val) {
this.currentPage = val;
},
fetchBindDevices(agentId) {
this.loading = true;
import('@/apis/module/user').then(({ default: userApi }) => {
userApi.getAgentBindDevices(agentId, ({ data }) => {
this.loading = false;
if (data.code === 0) {
this.deviceList = data.data[0].list.map(device => ({
device_id: device.id,
model: device.device_type,
firmwareVersion: device.app_version,
macAddress: device.mac_address,
lastConversation: device.recent_chat_time,
remark: '',
isEdit: false,
otaSwitch: device.ota_upgrade === 1
}));
} else {
this.$message.error(data.msg || '获取设备列表失败');
}
});
}).catch(error => {
console.error('模块加载失败:', error);
this.$message.error('功能模块加载失败');
});
},
} }
}; };
</script> </script>
@@ -166,4 +208,8 @@ export default {
vertical-align: middle; vertical-align: middle;
} }
</style> .pagination {
margin-top: 20px;
text-align: right;
}
</style>
+302 -100
View File
@@ -1,90 +1,133 @@
<template> <template>
<div class="welcome"> <div class="welcome">
<HeaderBar /> <HeaderBar />
<div class="model-tabs-container">
<el-tabs v-model="activeTab" class="model-tabs" > <div class="operation-bar">
<el-tab-pane label="语音活动检测模型(VAD)" name="vad"></el-tab-pane>
<el-tab-pane label="语音识别(ASR)" name="asr"></el-tab-pane> <div class="right-operations">
<el-tab-pane label="大语言模型(LLM)" name="llm"></el-tab-pane> <el-button v-if="activeTab === 'tts'" type="primary" plain size="small" @click="ttsDialogVisible = true">
<el-tab-pane label="意图识别模型(Intent)" name="intent"></el-tab-pane> 语音设置
<el-tab-pane label="语音合成模型(TTS)" name="tts"></el-tab-pane> </el-button>
<el-tab-pane label="记忆模型(Memory)" name="memory"></el-tab-pane> <el-button type="primary" plain size="small" @click="handleImport">
</el-tabs> 导入配置
<div class="import-export-btn"> </el-button>
<el-button v-if="activeTab === 'tts'" type="primary" @click="ttsDialogVisible = true" style="margin-right: 10px;">模型设置</el-button> <el-button type="success" plain size="small" @click="handleExport">
<el-button size="small" @click="handleImportExport">导入导出配置</el-button> 导出配置
</el-button>
</div>
</div> </div>
<!-- 主体内容 -->
<div class="main-wrapper">
<div class="content-panel">
<!-- 左侧导航 -->
<el-menu :default-active="activeTab" class="nav-panel" @select="handleMenuSelect"
style="background-size: cover; background-position: center;">
<el-menu-item index="vad">
<span class="menu-text">语言活动检测</span>
</el-menu-item>
<el-menu-item index="asr">
<span class="menu-text">语音识别</span>
</el-menu-item>
<el-menu-item index="llm">
<span class="menu-text">大语言模型</span>
</el-menu-item>
<el-menu-item index="intent">
<span class="menu-text">意图识别</span>
</el-menu-item>
<el-menu-item index="tts">
<span class="menu-text">语音合成</span>
</el-menu-item>
<el-menu-item index="memory">
<span class="menu-text">记忆</span>
</el-menu-item>
</el-menu>
<!-- 右侧内容 -->
<div class="content-area">
<div class="title-bar">
<div class="title-wrapper">
<h2 class="model-title">大语言模型LLM</h2>
<el-button type="primary" size="small" @click="addModel" class="add-btn">
添加
</el-button>
</div>
<div class="action-group">
<div class="search-group">
<el-input placeholder="请输入模型名称查询" v-model="search" size="small" class="search-input" clearable/>
<el-button type="primary" size="small" class="search-btn" @click="handleSearch">
查询
</el-button>
</div>
</div>
</div>
<el-table :header-cell-style="{background: 'transparent'}" :data="modelList" border class="data-table" header-row-class-name="table-header" >
<el-table-column type="selection" width="55" align="center"></el-table-column>
<el-table-column label="模型名称" prop="candidateName" align="center"></el-table-column>
<el-table-column label="模型编码" prop="code" align="center"></el-table-column>
<el-table-column label="提供商" prop="supplier" align="center"></el-table-column>
<el-table-column label="是否启用" align="center" width="120">
<template slot-scope="scope">
<el-switch v-model="scope.row.isApplied" class="custom-switch" :active-color="null" :inactive-color="null"/>
</template>
</el-table-column>
<el-table-column label="操作" align="center" width="180">
<template slot-scope="scope">
<el-button type="text" size="mini" @click="editModel(scope.row)" class="edit-btn">
修改
</el-button>
<el-button type="text" size="mini" @click="deleteModel(scope.row)" class="delete-btn">
删除
</el-button>
</template>
</el-table-column>
</el-table>
<div class="table-footer">
<div class="batch-actions">
<el-button size="mini" @click="selectAll">全选</el-button>
<el-button size="mini" type="danger" icon="el-icon-delete" @click="batchDelete">
删除
</el-button>
</div>
<div class="pagination-container">
<el-pagination @current-change="handleCurrentChange" background :current-page="currentPage" :page-size="pageSize" layout="prev, pager, next" :total="total"/>
</div>
</div>
</div>
</div>
<ModelEditDialog :visible.sync="editDialogVisible" :modelData="editModelData" @save="handleModelSave"/>
<TtsModel :visible.sync="ttsDialogVisible" />
<AddModelDialog :visible.sync="addDialogVisible" @confirm="handleAddConfirm"/>
</div> </div>
<el-main style="padding: 20px; display: flex; flex-direction: column;"> <div class="copyright">
<el-card class="model-card" shadow="always">
<div class="model-search-operate" style="display: flex; align-items: center; margin-bottom: 20px;">
<el-input placeholder="请输入模型名称查询" v-model="search" style="width: 300px; margin-right: 10px" />
<el-button @click="handleSearch">查询</el-button>
<el-button type="primary" @click="addModel">增加模型</el-button>
<el-button type="danger" @click="batchDelete">批量删除</el-button>
</div>
<el-table :data="modelList" style="width: 100%;" border stripe>
<el-table-column label="模型编码" prop="code"></el-table-column>
<el-table-column label="模型名称" prop="candidateName"></el-table-column>
<el-table-column label="是否应用">
<template slot-scope="scope">
<el-tag :type="scope.row.isApplied ? 'success' : 'danger'">
{{ scope.row.isApplied ? '是' : '否' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="供应商名称" prop="supplier"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button size="mini" @click="editModel(scope.row)" style="margin-right: 10px;">修改</el-button>
<el-button size="mini" type="danger" @click="deleteModel(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-card>
<div class="pagination-container">
<el-pagination
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[5, 10, 15]"
:page-size="pageSize"
layout="prev, pager, next"
:total="total"
/>
</div>
<div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;">
©2025 xiaozhi-esp32-server ©2025 xiaozhi-esp32-server
</div> </div>
<ModelEditDialog :visible.sync="editDialogVisible" :modelData="editModelData" @save="handleModelSave"/> </div>
<TtsModel :visible.sync="ttsDialogVisible" />
</el-main>
</div>
</template> </template>
<script> <script>
import HeaderBar from "@/components/HeaderBar.vue"; import HeaderBar from "@/components/HeaderBar.vue";
import ModelEditDialog from "@/components/ModelEditDialog.vue"; import ModelEditDialog from "@/components/ModelEditDialog.vue";
import TtsModel from "@/components/TtsModel.vue"; import TtsModel from "@/components/TtsModel.vue";
import AddModelDialog from "@/components/AddModelDialog.vue";
export default { export default {
components: { HeaderBar, ModelEditDialog, TtsModel }, components: { HeaderBar, ModelEditDialog, TtsModel, AddModelDialog },
data() { data() {
return { return {
activeTab: 'vad', addDialogVisible: false,
activeTab: 'llm',
search: '', search: '',
editDialogVisible: false, editDialogVisible: false,
editModelData: {}, editModelData: {},
ttsDialogVisible: false, ttsDialogVisible: false,
modelList: [ modelList: [
{ code: 'AILLM', candidateName: '阿里百炼', isApplied: true, supplier: 'openai' }, { code: 'DeepSeek', candidateName: '深度求索', isApplied: true, supplier: '硅基流动' },
{ code: 'DoubaoLLM', candidateName: '豆包大模型', isApplied: true, supplier: 'openai' }, { code: 'SmartAssist', candidateName: '智能助手', isApplied: false, supplier: '智脑科技' },
{ code: 'DeepSeekLLM', candidateName: '深度求索', isApplied: true, supplier: 'openai' }, { code: 'CogEngine', candidateName: '认知引擎', isApplied: true, supplier: '云智科技' },
{ code: 'DifyLLM', candidateName: 'DifChat', isApplied: true, supplier: 'dify' },
], ],
currentPage: 1, currentPage: 1,
pageSize: 4, pageSize: 4,
@@ -92,19 +135,18 @@ export default {
}; };
}, },
methods: { methods: {
// 查询 handleMenuSelect(index) {
this.activeTab = index;
},
handleSearch() { handleSearch() {
console.log('查询:', this.search); console.log('查询:', this.search);
}, },
// 批量删除
batchDelete() { batchDelete() {
console.log('批量删除'); console.log('批量删除');
}, },
// 增加
addModel() { addModel() {
console.log('增加模型'); this.addDialogVisible = true;
}, },
// 修改
editModel(model) { editModel(model) {
this.editModelData = { this.editModelData = {
code: model.code, code: model.code,
@@ -113,7 +155,6 @@ export default {
}; };
this.editDialogVisible = true; this.editDialogVisible = true;
}, },
// 删除
deleteModel(model) { deleteModel(model) {
console.log('删除:', model); console.log('删除:', model);
}, },
@@ -121,79 +162,240 @@ export default {
this.currentPage = page; this.currentPage = page;
console.log('当前页码:', page); console.log('当前页码:', page);
}, },
// 导入导出 handleImport() {
handleImportExport() { console.log('导入配置');
console.log('导入导出'); },
handleExport() {
console.log('导出配置');
}, },
handleModelSave(formData) { handleModelSave(formData) {
// 处理保存
console.log('保存的模型数据:', formData); console.log('保存的模型数据:', formData);
}, },
} selectAll() {
console.log('全选');
},
handleAddConfirm(newModel) {
console.log('新增模型数据:', newModel);
}
},
}; };
</script> </script>
<style scoped> <style scoped>
::v-deep .el-table tr{
background: transparent;
}
.welcome { .welcome {
min-width: 900px; min-width: 900px;
min-height: 506px; min-height: 506px;
height: 100vh; height: 100vh;
display: flex; display: flex;
position: relative;
flex-direction: column; flex-direction: column;
background-image: url("@/assets/home/background.png");
background-size: cover; background-size: cover;
background-position: center; background: linear-gradient(to bottom right, #dce8ff, #e4eeff, #e6cbfd) center;
-webkit-background-size: cover; -webkit-background-size: cover;
-o-background-size: cover; -o-background-size: cover;
} }
.model-search-operate { .main-wrapper {
margin-bottom: 20px; margin: 5px 60px;
background-image: url("@/assets/home/background.png");
border-radius: 15px;
min-height: 600px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
position: relative;
}
.operation-bar {
display: flex; display: flex;
justify-content: flex-end;
align-items: center; align-items: center;
padding: 16px 24px;
border-bottom: 1px solid #ebeef5;
} }
.model-search-operate > * { .content-panel {
margin-right: 10px; flex: 1;
display: flex;
overflow: hidden;
height: 100%;
border-radius: 15px;
background: transparent;
} }
.el-table__header th { .nav-panel {
background-color: #f5f7fa; width: 18%;
min-width: 200px;
height: 100%;
border-right: 1px solid #ebeef5;
background-size: cover;
background: url("../assets/model/model.png") no-repeat center;
padding: 16px 0;
flex-shrink: 0;
display: flex;
flex-direction: column;
}
.nav-panel .el-menu-item {
height: 48px;
line-height: 40px;
border-radius: 4px;
transition: all 0.3s;
display: flex !important;
justify-content: flex-end;
padding-right: 12px !important;
width: fit-content;
margin: 8px 12px 8px auto;
min-width: unset;
}
.nav-panel .el-menu-item.is-active {
background: #ecf5ff;
color: #409EFF;
border-right: 3px solid #409EFF;
}
.menu-text {
font-size: 14px;
color: #606266; color: #606266;
text-align: right;
width: 100%;
padding-right: 8px;
} }
.model-card {
background: #fff; .content-area {
border-radius: 12px; flex: 1;
padding: 20px; padding: 24px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); height: 100%;
min-width: 600px;
overflow-x: auto;
} }
.model-tabs-container { .title-bar {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
margin-top: 25px; margin-bottom: 24px;
flex-wrap: nowrap;
} }
.model-tabs { .model-title {
flex-grow: 0; font-size: 18px;
margin-right: 20px; color: #303133;
margin: 0;
} }
.import-export-btn { .action-group {
margin-right: 40px; display: flex;
margin-bottom: 20px; align-items: center;
gap: 16px;
}
.search-group {
display: flex;
gap: 8px;
}
.search-input {
width: 240px;
}
.search-btn {
background: linear-gradient(135deg, #6B8CFF, #A966FF);
border: none;
color: white;
}
.data-table {
border-radius: 6px;
overflow: hidden;
background-color: transparent !important;
}
.data-table /deep/ .el-table__row {
background-color: transparent !important;
}
.table-header th {
background-color: transparent !important;
color: #606266;
font-weight: 600;
}
.table-footer {
margin-top: 24px;
display: flex;
justify-content: space-between;
align-items: center;
padding: 16px 0;
width: 100%;
}
.batch-actions {
display: flex;
gap: 8px;
}
.copyright {
text-align: center;
color: #979db1;
font-size: 12px;
font-weight: 400;
margin-top: auto;
padding: 30px 0 20px;
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 100%;
}
.edit-btn {
color: #409EFF !important;
}
.delete-btn {
color: #F56C6C !important;
}
.add-btn {
background: #87CEFA;
border: none;
color: white;
padding: 8px 16px;
}
.title-wrapper {
display: flex;
align-items: center;
gap: 8px;
}
.batch-actions .el-button:first-child {
background: linear-gradient(135deg, #409EFF, #6B8CFF);
border: none;
color: white;
}
.batch-actions .el-button:first-child:hover {
background: linear-gradient(135deg, #3A8EE6, #5A7CFF);
}
.el-table th /deep/ .el-table__cell {
overflow: hidden;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
background-color: transparent !important;
} }
.pagination-container { .pagination-container {
margin-top: 20px;
display: flex; display: flex;
justify-content: flex-end; justify-content: flex-end;
} }
::v-deep .el-tabs {
margin-left: 70px;
}
</style> </style>
+163 -37
View File
@@ -1,45 +1,64 @@
<template> <template>
<div class="welcome"> <div class="welcome">
<HeaderBar /> <HeaderBar />
<el-main style="padding: 20px; display: flex; flex-direction: column;"> <el-main class="main" style="padding: 20px; display: flex; flex-direction: column;">
<el-card class="user-card" shadow="always" > <div class="top-area">
<div class="user-search-operate" style="display: flex; align-items: center; margin-bottom: 20px;"> <div class="page-title">用户管理</div>
<el-input placeholder="请输入手机号码查询" v-model="searchPhone" style="width: 300px; margin-right: 10px" /> <div class="page-search">
<el-button @click="handleSearch">查询</el-button> <el-input placeholder="请输入手机号码查询" v-model="searchPhone" class="search-input" />
<el-button type="danger" @click="batchDelete">批量删除</el-button> <el-button class="btn-search" @click="handleSearch">搜索</el-button>
<el-button type="danger" @click="batchDisable">批量禁用</el-button> <!-- <el-button type="danger" @click="batchDelete">批量删除</el-button>
<el-button type="danger" @click="batchDisable">批量禁用</el-button> -->
</div>
</div> </div>
<el-table :data="userList" style="width: 100%;" border stripe> <el-card class="user-card" shadow="never">
<el-table-column label="用户Id" prop="userId"></el-table-column> <!-- <div class="user-search-operate" style="display: flex; align-items: center; margin-bottom: 20px;">
<el-table-column label="手机号码" prop="phone"></el-table-column> <el-input placeholder="请输入手机号码查询" v-model="searchPhone" style="width: 300px; margin-right: 10px" />
<el-table-column label="Status" prop="status"></el-table-column> <el-button @click="handleSearch">查询</el-button>
<el-table-column label="设备数量" prop="deviceCount"></el-table-column> <el-button type="danger" @click="batchDelete">批量删除</el-button>
<el-table-column label="操作"> <el-button type="danger" @click="batchDisable">批量禁用</el-button>
<template slot-scope="scope"> </div> -->
<el-button size="mini" @click="resetPassword(scope.row)">重置密码</el-button> <el-table :data="userList" class="transparent-table" :header-cell-class-name="headerCellClassName">
<el-button size="mini" <el-table-column label="选择" type="selection" width="55"></el-table-column>
<el-table-column label="用户Id" prop="user_id"></el-table-column>
<el-table-column label="手机号码" prop="mobile"></el-table-column>
<el-table-column label="设备数量" prop="device_count"></el-table-column>
<el-table-column label="状态" prop="status"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button size="mini" type="text" @click="resetPassword(scope.row)" style="color: #989fdd">重置密码</el-button>
<el-button size="mini" type="text"
v-if="scope.row.status === '正常'" v-if="scope.row.status === '正常'"
@click="disableUser(scope.row)">禁用</el-button> @click="disableUser(scope.row)">禁用</el-button>
<el-button size="mini" <el-button size="mini" type="text"
v-if="scope.row.status === '禁用'" v-if="scope.row.status === '禁用'"
@click="restoreUser(scope.row)">恢复</el-button> @click="restoreUser(scope.row)">恢复</el-button>
<el-button size="mini" @click="deleteUser(scope.row)" style="color: #ff4949">删除用户</el-button> <el-button size="mini" type="text" @click="deleteUser(scope.row)" style="color: #989fdd">删除用户</el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
</el-card>
<div class="pagination-container"> <div class="table_bottom">
<el-pagination <div class="ctrl_btn">
@current-change="handleCurrentChange" <el-button size="mini" type="primary" style="width: 72px; background: #5f70f3">全选</el-button>
:current-page="currentPage" <el-button size="mini" type="success" icon="el-icon-circle-check" style="background: #5bc98c">启用</el-button>
:page-sizes="[5, 10, 15]" <el-button size="mini" type="warning" style="color: black; background: #f6d075"><i class="el-icon-remove-outline rotated-icon"></i>禁用</el-button>
:page-size="pageSize" <el-button size="mini" type="danger" icon="el-icon-delete" style="background: #fd5b63">删除</el-button>
layout="prev, pager, next" </div>
:total="total" <div class="pagination-container">
/> <el-pagination
</div> background
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[5, 10, 15]"
:page-size="pageSize"
layout="prev, pager, next"
:total="total"
/>
</div>
</div>
</el-card>
<div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;"> <div style="font-size: 12px; font-weight: 400; margin-top: auto; padding-top: 30px; color: #979db1;">
©2025 xiaozhi-esp32-server ©2025 xiaozhi-esp32-server
@@ -50,6 +69,9 @@
<script> <script>
import HeaderBar from "@/components/HeaderBar.vue"; import HeaderBar from "@/components/HeaderBar.vue";
import Api from '@/apis/api';
import adminApi from '@/apis/module/admin';
export default { export default {
components: { HeaderBar }, components: { HeaderBar },
@@ -67,6 +89,13 @@ export default {
total: 20 total: 20
}; };
}, },
created() {
adminApi.getUserList(({data}) => {
//mock偶尔会返回-1导致出错,又会返回两个list,所以这里只取第一个
this.userList = data.data[0].list;
console.log('用户列表:', this.userList);
})
},
methods: { methods: {
handleSearch() { handleSearch() {
// 模拟搜索逻辑 // 模拟搜索逻辑
@@ -96,11 +125,45 @@ export default {
this.currentPage = page; this.currentPage = page;
console.log('当前页码:', page); console.log('当前页码:', page);
}, },
headerCellClassName({ column, columnIndex }) {
if (columnIndex === 0) {
return 'custom-selection-header'
}
return ''
},
} }
}; };
</script> </script>
<style scoped> <style lang="scss" scoped>
$table-bg-color: #ecf1fd;
.main {
padding: 20px; display: flex; flex-direction: column;
background: linear-gradient(to bottom right, #dce8ff, #e4eeff, #e6cbfd);
}
.top-area {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
.page-title {
font-size: 20px;
font-weight: 600;
color: #303133;
}
.page-search {
display: flex;
align-items: center;
.btn-search {
margin-left: 10px;
background: linear-gradient(to right, #5778ff, #c793f3);
width: 100px;
color: #fff;
}
}
}
.welcome { .welcome {
min-width: 900px; min-width: 900px;
min-height: 506px; min-height: 506px;
@@ -130,16 +193,79 @@ export default {
} }
.user-card { .user-card {
background: #fff; background: $table-bg-color;
border-radius: 12px; border-radius: 12px;
padding: 20px; padding: 20px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); //box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.table_bottom {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 20px;
.ctrl_btn {
display: flex;
align-items: center;
}
} }
.pagination-container { .pagination-container {
margin-top: 20px;
display: flex; display: flex;
justify-content: flex-end; justify-content: flex-end;
} }
.rotated-icon {
display: inline-block;
transform: rotate(45deg);
margin-right: 4px;
color: black;
}
:deep(.el-table) {
background: $table-bg-color;
&.transparent-table {
.el-table__header th {
background: $table-bg-color !important;
color: black;
}
.el-table__body tr {
background-color: $table-bg-color;
}
}
}
.search-input {
width: 300px;
margin-right: 10px;
:deep(.el-input__inner) {
background-color: transparent;
&:focus {
border-color: #409eff; // 保持聚焦状态下的边框颜色
}
//文字颜色
&::placeholder {
color: #606266;
opacity: 0.7;
}
}
}
:deep(.custom-selection-header) {
.el-checkbox {
display: none !important;
}
&::after {
content: '选择';
display: inline-block;
color: black;
font-weight: bold;
padding-bottom: 18px;
}
}
</style> </style>
+29 -4
View File
@@ -31,7 +31,11 @@
</div> </div>
</div> </div>
<div style="display: flex;flex-wrap: wrap;margin-top: 20px;gap: 20px;justify-content: flex-start;box-sizing: border-box;"> <div style="display: flex;flex-wrap: wrap;margin-top: 20px;gap: 20px;justify-content: flex-start;box-sizing: border-box;">
<DeviceItem v-for="(item,index) in devices" :key="index" :device="item" @configure="goToRoleConfig" @deviceManage="handleDeviceManage" /> <DeviceItem v-for="(item,index) in devices" :key="index" :device="item"
@configure="goToRoleConfig"
@deviceManage="handleDeviceManage"
@delete="handleDeleteAgent"
/>
</div> </div>
</div> </div>
<div style="font-size: 12px;font-weight: 400;margin-top: auto;padding-top: 30px;color: #979db1;"> <div style="font-size: 12px;font-weight: 400;margin-top: auto;padding-top: 30px;color: #979db1;">
@@ -83,16 +87,37 @@ export default {
fetchAgentList() { fetchAgentList() {
import('@/apis/module/user').then(({ default: userApi }) => { import('@/apis/module/user').then(({ default: userApi }) => {
userApi.getAgentList(({data}) => { userApi.getAgentList(({data}) => {
this.originalDevices = data.data; this.originalDevices = data.data.map(item => ({
this.devices = data.data; ...item,
agentId: item.id // 字段映射
}));
this.devices = this.originalDevices;
}); });
}); });
}, },
// 搜索更新智能体列表 // 搜索更新智能体列表
handleSearchResult(filteredList) { handleSearchResult(filteredList) {
this.devices = filteredList; // 更新设备列表 this.devices = filteredList; // 更新设备列表
},
// 删除智能体
handleDeleteAgent(agentId) {
this.$confirm('确定要删除该智能体吗', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
import('@/apis/module/user').then(({ default: userApi }) => {
userApi.deleteAgent(agentId, (res) => {
if (res.data.code === 0) {
this.$message.success('删除成功');
this.fetchAgentList(); // 刷新列表
} else {
this.$message.error(res.data.msg || '删除失败');
}
});
});
}).catch(() => {});
} }
} }
} }
</script> </script>
+146 -44
View File
@@ -1,6 +1,5 @@
<template> <template>
<div class="welcome"> <div class="welcome">
<!-- 公共头部 -->
<HeaderBar/> <HeaderBar/>
<el-main style="padding: 16px;display: flex;flex-direction: column;"> <el-main style="padding: 16px;display: flex;flex-direction: column;">
<div style="border-radius: 16px;background: #fafcfe; border: 1px solid #e8f0ff;"> <div style="border-radius: 16px;background: #fafcfe; border: 1px solid #e8f0ff;">
@@ -10,19 +9,19 @@
style="width: 37px;height: 37px;background: #5778ff;border-radius: 50%;display: flex;align-items: center;justify-content: center;"> style="width: 37px;height: 37px;background: #5778ff;border-radius: 50%;display: flex;align-items: center;justify-content: center;">
<img src="@/assets/home/setting-user.png" alt="" style="width: 19px;height: 19px;"/> <img src="@/assets/home/setting-user.png" alt="" style="width: 19px;height: 19px;"/>
</div> </div>
{{ deviceMac }} {{ form.agentName }}
</div> </div>
<div style="height: 1px;background: #e8f0ff;"/> <div style="height: 1px;background: #e8f0ff;"/>
<el-form ref="form" :model="form" label-width="72px"> <el-form ref="form" :model="form" label-width="72px">
<div style="padding: 16px 24px;max-width: 792px;"> <div style="padding: 16px 24px;max-width: 792px;">
<el-form-item label="助手昵称:"> <el-form-item label="助手昵称:">
<div class="input-46" style="width: 100%; max-width: 412px;"> <div class="input-46" style="width: 100%; max-width: 412px;">
<el-input v-model="form.name"/> <el-input v-model="form.agentName"/>
</div> </div>
</el-form-item> </el-form-item>
<el-form-item label="角色模版:"> <el-form-item label="角色模版:">
<div style="display: flex;gap: 8px;"> <div style="display: flex;gap: 8px;">
<div v-for="template in templates" :key="template" class="template-item" @click="selectTemplate(template)"> <div v-for="template in templates" :key="template" class="template-item" :class="{ 'template-loading': loadingTemplate }" @click="selectTemplate(template)">
{{ template }} {{ template }}
</div> </div>
</div> </div>
@@ -30,7 +29,7 @@
<el-form-item label="角色音色:"> <el-form-item label="角色音色:">
<div style="display: flex;gap: 8px;align-items: center;"> <div style="display: flex;gap: 8px;align-items: center;">
<div class="input-46" style="flex:1.4;"> <div class="input-46" style="flex:1.4;">
<el-select v-model="form.timbre" placeholder="请选择" style="width: 100%;"> <el-select v-model="form.ttsVoiceId" placeholder="请选择" style="width: 100%;">
<el-option v-for="item in options" :key="item.value" :label="item.label" <el-option v-for="item in options" :key="item.value" :label="item.label"
:value="item.value"> :value="item.value">
</el-option> </el-option>
@@ -45,14 +44,14 @@
<el-form-item label="角色介绍:"> <el-form-item label="角色介绍:">
<div class="textarea-box"> <div class="textarea-box">
<el-input type="textarea" rows="5" resize="none" placeholder="请输入内容" <el-input type="textarea" rows="5" resize="none" placeholder="请输入内容"
v-model="form.introduction" maxlength="2000" show-word-limit/> v-model="form.systemPrompt" maxlength="2000" show-word-limit/>
</div> </div>
</el-form-item> </el-form-item>
<el-form-item label="记忆体:"> <el-form-item label="记忆体:">
<div class="textarea-box"> <div class="textarea-box">
<el-input type="textarea" rows="5" resize="none" placeholder="请输入内容" <el-input type="textarea" rows="5" resize="none" placeholder="请输入内容"
v-model="form.prompt" maxlength="1000"/> v-model="form.langCode" maxlength="1000"/>
<div class="prompt-bottom"> <div class="prompt-bottom" @click="clearMemory">
<div style="display: flex;gap: 8px;align-items: center;"> <div style="display: flex;gap: 8px;align-items: center;">
<div style="color: #979db1;font-size: 11px;">当前记忆每次对话后重新生成</div> <div style="color: #979db1;font-size: 11px;">当前记忆每次对话后重新生成</div>
<div class="clear-btn"> <div class="clear-btn">
@@ -60,7 +59,7 @@
清除 清除
</div> </div>
</div> </div>
<div style="color: #979db1;font-size:11px;">{{ form.prompt.length }}/1000</div> <div style="color: #979db1;font-size:11px;">{{ form.langCode.length }}/1000</div>
</div> </div>
</div> </div>
</el-form-item> </el-form-item>
@@ -104,41 +103,65 @@ export default {
components: {HeaderBar}, components: {HeaderBar},
data() { data() {
return { return {
deviceMac: 'CC:ba:97:11:a6:ac',
form: { form: {
name: "", agentCode: "",
timbre: "", agentName: "",
introduction: "", ttsVoiceId: "",
prompt: "", systemPrompt: "",
langCode: "",
language: "",
sort: "",
model: { model: {
tts: "", ttsModelId: "",
vad: "", vadModelId: "",
asr: "", asrModelId: "",
llm: "", llmModelId: "",
memory:"", memModelId: "",
intent:"" intentModelId: "",
} }
}, },
options: [ options: [
{ value: '选项1', label: '黄金糕' }, {value: '选项1', label: '黄金糕'},
{ value: '选项2', label: '双皮奶' } {value: '选项2', label: '双皮奶'}
], ],
models: [ models: [
{ label: '大语言模型(LLM)', key: 'llm' }, {label: '大语言模型(LLM)', key: 'llmModelId'},
{ label: '语音识别(ASR)', key: 'asr' }, {label: '语音识别(ASR)', key: 'asrModelId'},
{ label: '语音活动检测模型(VAD)', key: 'vad' }, {label: '语音活动检测模型(VAD)', key: 'vadModelId'},
{ label: '语音合成模型(TTS)', key: 'tts' }, {label: '语音合成模型(TTS)', key: 'ttsModelId'},
{ label: '意图识别模型(Intent)', key: 'intent' }, {label: '意图识别模型(Intent)', key: 'intentModelId'},
{ label: '记忆模型(Memory)', key: 'memory' } {label: '记忆模型(Memory)', key: 'memModelId'}
], ],
templates: ['台湾女友', '土豆子', '英语老师', '好奇小男孩', '汪汪队队长'] templates: ['台湾女友', '土豆子', '英语老师', '好奇小男孩', '汪汪队队长'],
loadingTemplate: false
} }
}, },
methods: { methods: {
saveConfig() { saveConfig() {
// 此处写保存配置逻辑 const configData = {
this.$message.success('配置已保存') agentCode: this.form.agentCode,
agentName: this.form.agentName,
asrModelId: this.form.model.asrModelId,
vadModelId: this.form.model.vadModelId,
llmModelId: this.form.model.llmModelId,
ttsModelId: this.form.model.ttsModelId,
ttsVoiceId: this.form.ttsVoiceId,
memModelId: this.form.model.memModelId,
intentModelId: this.form.model.intentModelId,
systemPrompt: this.form.systemPrompt,
langCode: this.form.langCode,
language: this.form.language,
sort: this.form.sort
};
import('@/apis/module/user').then(({default: userApi}) => {
userApi.updateAgentConfig(this.$route.query.agentId, configData, ({data}) => {
if (data.code === 0) {
this.$message.success('配置保存成功');
} else {
this.$message.error(data.msg || '配置保存失败');
}
});
});
}, },
resetConfig() { resetConfig() {
this.$confirm('确定要重置配置吗?', '提示', { this.$confirm('确定要重置配置吗?', '提示', {
@@ -148,20 +171,99 @@ export default {
}).then(() => { }).then(() => {
// 重置表单 // 重置表单
this.form = { this.form = {
name: "", agentCode: "",
timbre: "", agentName: "",
introduction: "", ttsVoiceId: "",
prompt: "", systemPrompt: "",
model: "" langCode: "",
language: "",
sort: "",
model: {
ttsModelId: "",
vadModelId: "",
asrModelId: "",
llmModelId: "",
memModelId: "",
intentModelId: "",
}
} }
this.$message.success('配置已重置') this.$message.success('配置已重置')
}).catch(() => { }).catch(() => {
}) })
}, },
// 处理选择模板的逻辑 selectTemplate(templateName) {
selectTemplate(template) { if (this.loadingTemplate) return;
this.form.name = template;
this.$message.success(`已选择模板:${template}`); this.loadingTemplate = true;
import('@/apis/module/user').then(({default: userApi}) => {
userApi.getAgentTemplate(
{templateName},
(response) => {
this.loadingTemplate = false;
if (response.data.code === 0 && response.data.data.length > 0) {
this.applyTemplateData(response.data.data[0]);
this.$message.success(`${templateName}」模板已应用`);
} else {
this.$message.warning(`未找到「${templateName}」模板`);
}
}
);
}).catch((error) => {
this.loadingTemplate = false;
this.$message.error('模板加载失败');
console.error('接口异常:', error);
});
},
applyTemplateData(templateData) {
this.form = {
...this.form,
agentName: templateData.agentName || this.form.agentName,
ttsVoiceId: templateData.ttsVoiceId || this.form.ttsVoiceId,
systemPrompt: templateData.systemPrompt || this.form.systemPrompt,
langCode: templateData.langCode || this.form.langCode,
model: {
ttsModelId: templateData.ttsModelId || this.form.model.ttsModelId,
vadModelId: templateData.vadModelId || this.form.model.vadModelId,
asrModelId: templateData.asrModelId || this.form.model.asrModelId,
llmModelId: templateData.llmModelId || this.form.model.llmModelId,
memModelId: templateData.memModelId || this.form.model.memModelId,
intentModelId: templateData.intentModelId || this.form.model.intentModelId
}
};
},
fetchAgentConfig(agentId) {
import('@/apis/module/user').then(({default: userApi}) => {
userApi.getDeviceConfig(agentId, ({data}) => {
if (data.code === 0) {
this.form = {
...this.form,
...data.data,
model: {
ttsModelId: data.data.ttsModelId,
vadModelId: data.data.vadModelId,
asrModelId: data.data.asrModelId,
llmModelId: data.data.llmModelId,
memModelId: data.data.memModelId,
intentModelId: data.data.intentModelId
}
};
} else {
this.$message.error(data.msg || '获取配置失败');
}
});
});
},
// 清空记忆体内容
clearMemory() {
this.form.langCode = "";
this.$message.success("记忆体已清空");
},
},
mounted() {
const agentId = this.$route.query.agentId;
console.log('agentId2222',agentId);
if (agentId) {
this.fetchAgentConfig(agentId);
} }
} }
} }
+44 -6
View File
@@ -24,9 +24,9 @@ server:
# - "24:0A:C4:1D:3B:F0" # MAC地址列表 # - "24:0A:C4:1D:3B:F0" # MAC地址列表
log: log:
# 设置控制台输出的日志格式,时间、日志级别、标签、消息 # 设置控制台输出的日志格式,时间、日志级别、标签、消息
log_format: "<green>{time:YY-MM-DD HH:mm:ss}</green>[<light-blue>{extra[tag]}</light-blue>] - <level>{level}</level> - <light-green>{message}</light-green>" log_format: "<green>{time:YYMMDD HH:mm:ss}</green>[{version}_{selected_module}][<light-blue>{extra[tag]}</light-blue>]-<level>{level}</level>-<light-green>{message}</light-green>"
# 设置日志文件输出的格式,时间、日志级别、标签、消息 # 设置日志文件输出的格式,时间、日志级别、标签、消息
log_format_simple: "{time:YYYY-MM-DD HH:mm:ss} - {name} - {level} - {extra[tag]} - {message}" log_format_file: "{time:YYYY-MM-DD HH:mm:ss} - {version}_{selected_module} - {name} - {level} - {extra[tag]} - {message}"
# 设置日志等级:INFO、DEBUG # 设置日志等级:INFO、DEBUG
log_level: INFO log_level: INFO
# 设置日志路径 # 设置日志路径
@@ -57,6 +57,14 @@ delete_audio: true
close_connection_no_voice_time: 120 close_connection_no_voice_time: 120
# TTS请求超时时间(秒) # TTS请求超时时间(秒)
tts_timeout: 10 tts_timeout: 10
# 开启唤醒词加速
enable_wakeup_words_response_cache: true
# 开场是否回复唤醒词
enable_greeting: true
# 说完话是否开启提示音
enable_stop_tts_notify: false
# 说完话是否开启提示音,音效地址
stop_tts_notify_voice: "config/assets/tts_notify.mp3"
CMD_exit: CMD_exit:
- "退出" - "退出"
@@ -74,9 +82,10 @@ selected_module:
TTS: EdgeTTS TTS: EdgeTTS
# 记忆模块,默认不开启记忆;如果想使用超长记忆,推荐使用mem0ai;如果注重隐私,请使用本地的mem_local_short # 记忆模块,默认不开启记忆;如果想使用超长记忆,推荐使用mem0ai;如果注重隐私,请使用本地的mem_local_short
Memory: nomem Memory: nomem
# 意图识别模块,默认使用function_call。开启后,可以播放音乐、控制音量、识别退出指令 # 意图识别模块开启后,可以播放音乐、控制音量、识别退出指令
# 意图识别使用intent_llm,优点:通用性强,缺点:增加串行前置意图识别模块,会增加处理时间 # 不想开通意图识别,就设置成:nointent
# 意图识别使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快 # 意图识别使用intent_llm,如果你的LLM是DifyLLM或CozeLLM,建议使用这个。优点:通用性强,缺点:增加串行前置意图识别模块,会增加处理时间,这个意图识别暂时不支持控制音量大小等iot操作
# 意图识别可使用function_call,缺点:需要所选择的LLM支持function_call,优点:按需调用工具、速度快,理论上能全部操作所有iot指令
# 默认免费的ChatGLMLLM就已经支持function_call,但是如果像追求稳定建议把LLM设置成:DoubaoLLM,使用的具体model_name是:doubao-pro-32k-functioncall-241028 # 默认免费的ChatGLMLLM就已经支持function_call,但是如果像追求稳定建议把LLM设置成:DoubaoLLM,使用的具体model_name是:doubao-pro-32k-functioncall-241028
Intent: function_call Intent: function_call
@@ -89,9 +98,13 @@ Intent:
intent_llm: intent_llm:
# 不需要动type # 不需要动type
type: intent_llm type: intent_llm
# 配备意图识别独立的思考模型
# 如果这里不填,则会默认使用selected_module.LLM的模型作为意图识别的思考模型
# 如果你的selected_module.LLM选择了DifyLLM或CozeLLM,这里最好使用独立的LLM作为意图识别,例如使用免费的ChatGLMLLM
llm: ChatGLMLLM
function_call: function_call:
# 不需要动type # 不需要动type
type: nointent type: function_call
# plugins_func/functions下的模块,可以通过配置,选择加载哪个模块,加载后对话支持相应的function调用 # plugins_func/functions下的模块,可以通过配置,选择加载哪个模块,加载后对话支持相应的function调用
# 系统默认已经记载“handle_exit_intent(退出识别)”、“play_music(音乐播放)”插件,请勿重复加载 # 系统默认已经记载“handle_exit_intent(退出识别)”、“play_music(音乐播放)”插件,请勿重复加载
# 下面是加载查天气、角色切换、加载查新闻的插件示例 # 下面是加载查天气、角色切换、加载查新闻的插件示例
@@ -270,6 +283,18 @@ LLM:
variables: variables:
k: "v" k: "v"
k2: "v2" k2: "v2"
XinferenceLLM:
# 定义LLM API类型
type: xinference
# Xinference服务地址和模型名称
model_name: qwen2.5:72b-AWQ # 使用的模型名称,需要预先在Xinference启动对应模型
base_url: http://localhost:9997 # Xinference服务地址
XinferenceSmallLLM:
# 定义轻量级LLM API类型,用于意图识别
type: xinference
# Xinference服务地址和模型名称
model_name: qwen2.5:3b-AWQ # 使用的小模型名称,用于意图识别
base_url: http://localhost:9997 # Xinference服务地址
TTS: TTS:
# 当前支持的type为edge、doubao,可自行适配 # 当前支持的type为edge、doubao,可自行适配
EdgeTTS: EdgeTTS:
@@ -514,3 +539,16 @@ module_test:
- "你好,请介绍一下你自己" - "你好,请介绍一下你自己"
- "What's the weather like today?" - "What's the weather like today?"
- "请用100字概括量子计算的基本原理和应用前景" - "请用100字概括量子计算的基本原理和应用前景"
# 唤醒词,用于识别唤醒词还是讲话内容
wakeup_words:
- "你好小智"
- "你好小志"
- "小爱同学"
- "你好小鑫"
- "你好小新"
- "小美同学"
- "小龙小龙"
- "喵喵同学"
- "小滨小滨"
- "小冰小冰"
Binary file not shown.
Binary file not shown.
+15 -3
View File
@@ -3,12 +3,24 @@ import sys
from loguru import logger from loguru import logger
from config.settings import load_config from config.settings import load_config
SERVER_VERSION = "0.1.16"
def setup_logging(): def setup_logging():
"""从配置文件中读取日志配置,并设置日志输出格式和级别""" """从配置文件中读取日志配置,并设置日志输出格式和级别"""
config = load_config() config = load_config()
log_config = config["log"] log_config = config["log"]
log_format = log_config.get("log_format", "<green>{time:YY-MM-DD HH:mm:ss}</green>[<light-blue>{extra[tag]}</light-blue>] - <level>{level}</level> - <light-green>{message}</light-green>") log_format = log_config.get("log_format", "<green>{time:YYMMDD HH:mm:ss}</green>[{version}_{selected_module}][<light-blue>{extra[tag]}</light-blue>]-<level>{level}</level>-<light-green>{message}</light-green>")
log_format_simple = log_config.get("log_format_file", "{time:YYYY-MM-DD HH:mm:ss} - {name} - {level} - {extra[tag]} - {message}") log_format_file = log_config.get("log_format_file", "{time:YYYY-MM-DD HH:mm:ss} - {version_{selected_module}} - {name} - {level} - {extra[tag]} - {message}")
selected_module = config.get("selected_module")
selected_module_str = ''.join([value[0] + value[1] for key, value in selected_module.items()])
log_format = log_format.replace("{version}", SERVER_VERSION)
log_format = log_format.replace("{selected_module}", selected_module_str)
log_format_file = log_format_file.replace("{version}", SERVER_VERSION)
log_format_file = log_format_file.replace("{selected_module}", selected_module_str)
log_level = log_config.get("log_level", "INFO") log_level = log_config.get("log_level", "INFO")
log_dir = log_config.get("log_dir", "tmp") log_dir = log_config.get("log_dir", "tmp")
log_file = log_config.get("log_file", "server.log") log_file = log_config.get("log_file", "server.log")
@@ -24,6 +36,6 @@ def setup_logging():
logger.add(sys.stdout, format=log_format, level=log_level) logger.add(sys.stdout, format=log_format, level=log_level)
# 输出到文件 # 输出到文件
logger.add(os.path.join(log_dir, log_file), format=log_format_simple, level=log_level) logger.add(os.path.join(log_dir, log_file), format=log_format_file, level=log_level)
return logger return logger
+155 -37
View File
@@ -9,7 +9,7 @@ import traceback
import threading import threading
import websockets import websockets
from typing import Dict, Any from typing import Dict, Any
import plugins_func.loadplugins from plugins_func.loadplugins import auto_import_modules
from config.logger import setup_logging from config.logger import setup_logging
from core.utils.dialogue import Message, Dialogue from core.utils.dialogue import Message, Dialogue
from core.handle.textHandle import handleTextMessage from core.handle.textHandle import handleTextMessage
@@ -18,13 +18,16 @@ from concurrent.futures import ThreadPoolExecutor, TimeoutError
from core.handle.sendAudioHandle import sendAudioMessage from core.handle.sendAudioHandle import sendAudioMessage
from core.handle.receiveAudioHandle import handleAudioMessage from core.handle.receiveAudioHandle import handleAudioMessage
from core.handle.functionHandler import FunctionHandler from core.handle.functionHandler import FunctionHandler
from plugins_func.register import Action from plugins_func.register import Action, ActionResponse
from config.private_config import PrivateConfig from config.private_config import PrivateConfig
from core.auth import AuthMiddleware, AuthenticationError from core.auth import AuthMiddleware, AuthenticationError
from core.utils.auth_code_gen import AuthCodeGenerator from core.utils.auth_code_gen import AuthCodeGenerator
from core.mcp.manager import MCPManager
TAG = __name__ TAG = __name__
auto_import_modules('plugins_func.functions')
class TTSException(RuntimeError): class TTSException(RuntimeError):
pass pass
@@ -98,6 +101,8 @@ class ConnectionHandler:
self.use_function_call_mode = False self.use_function_call_mode = False
if self.config["selected_module"]["Intent"] == 'function_call': if self.config["selected_module"]["Intent"] == 'function_call':
self.use_function_call_mode = True self.use_function_call_mode = True
self.mcp_manager = MCPManager(self)
async def handle_connection(self, ws): async def handle_connection(self, ws):
try: try:
@@ -109,11 +114,15 @@ class ConnectionHandler:
# 进行认证 # 进行认证
await self.auth.authenticate(self.headers) await self.auth.authenticate(self.headers)
device_id = self.headers.get("device-id", None) device_id = self.headers.get("device-id", None)
self.memory.init_memory(device_id, self.llm)
self.intent.set_llm(self.llm)
# 认证通过,继续处理
self.websocket = ws
self.session_id = str(uuid.uuid4())
self.welcome_msg = self.config["xiaozhi"]
self.welcome_msg["session_id"] = self.session_id
await self.websocket.send(json.dumps(self.welcome_msg))
# Load private configuration if device_id is provided # Load private configuration if device_id is provided
bUsePrivateConfig = self.config.get("use_private_config", False) bUsePrivateConfig = self.config.get("use_private_config", False)
self.logger.bind(tag=TAG).info(f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}") self.logger.bind(tag=TAG).info(f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}")
@@ -141,43 +150,32 @@ class ConnectionHandler:
self.private_config = None self.private_config = None
raise raise
# 认证通过,继续处理
self.websocket = ws
self.session_id = str(uuid.uuid4())
self.welcome_msg = self.config["xiaozhi"]
self.welcome_msg["session_id"] = self.session_id
await self.websocket.send(json.dumps(self.welcome_msg))
# 异步初始化 # 异步初始化
await self.loop.run_in_executor(None, self._initialize_components) self.executor.submit(self._initialize_components)
# tts 消化线程 # tts 消化线程
tts_priority = threading.Thread(target=self._tts_priority_thread, daemon=True) self.tts_priority_thread = threading.Thread(target=self._tts_priority_thread, daemon=True)
tts_priority.start() self.tts_priority_thread.start()
# 音频播放 消化线程 # 音频播放 消化线程
audio_play_priority = threading.Thread(target=self._audio_play_priority_thread, daemon=True) self.audio_play_priority_thread = threading.Thread(target=self._audio_play_priority_thread, daemon=True)
audio_play_priority.start() self.audio_play_priority_thread.start()
try: try:
async for message in self.websocket: async for message in self.websocket:
await self._route_message(message) await self._route_message(message)
except websockets.exceptions.ConnectionClosed: except websockets.exceptions.ConnectionClosed:
self.logger.bind(tag=TAG).info("客户端断开连接") self.logger.bind(tag=TAG).info("客户端断开连接")
await self.close()
except AuthenticationError as e: except AuthenticationError as e:
self.logger.bind(tag=TAG).error(f"Authentication failed: {str(e)}") self.logger.bind(tag=TAG).error(f"Authentication failed: {str(e)}")
await ws.close()
return return
except Exception as e: except Exception as e:
stack_trace = traceback.format_exc() stack_trace = traceback.format_exc()
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}") self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}-{stack_trace}")
await ws.close()
return return
finally: finally:
await self.memory.save_memory(self.dialogue.dialogue) await self.memory.save_memory(self.dialogue.dialogue)
await self.close(ws)
async def _route_message(self, message): async def _route_message(self, message):
"""消息路由""" """消息路由"""
@@ -187,17 +185,54 @@ class ConnectionHandler:
await handleAudioMessage(self, message) await handleAudioMessage(self, message)
def _initialize_components(self): def _initialize_components(self):
"""加载提示词"""
self.prompt = self.config["prompt"] self.prompt = self.config["prompt"]
if self.private_config: if self.private_config:
self.prompt = self.private_config.private_config.get("prompt", self.prompt) self.prompt = self.private_config.private_config.get("prompt", self.prompt)
self.client_ip_info = get_ip_info(self.client_ip)
self.logger.bind(tag=TAG).info(f"Client ip info: {self.client_ip_info}")
self.prompt = self.prompt + f"\n我在:{self.client_ip_info}"
self.dialogue.put(Message(role="system", content=self.prompt)) self.dialogue.put(Message(role="system", content=self.prompt))
"""加载插件"""
self.func_handler = FunctionHandler(self) self.func_handler = FunctionHandler(self)
"""加载记忆"""
device_id = self.headers.get("device-id", None)
self.memory.init_memory(device_id, self.llm)
"""为意图识别设置LLM,优先使用专用LLM"""
# 检查是否配置了专用的意图识别LLM
intent_llm_name = self.config["Intent"]["intent_llm"]["llm"]
# 记录开始初始化意图识别LLM的时间
intent_llm_init_start = time.time()
if not self.use_function_call_mode and intent_llm_name and intent_llm_name in self.config["LLM"]:
# 如果配置了专用LLM,则创建独立的LLM实例
from core.utils import llm as llm_utils
intent_llm_config = self.config["LLM"][intent_llm_name]
intent_llm_type = intent_llm_config.get("type", intent_llm_name)
intent_llm = llm_utils.create_instance(intent_llm_type, intent_llm_config)
self.logger.bind(tag=TAG).info(f"为意图识别创建了专用LLM: {intent_llm_name}, 类型: {intent_llm_type}")
self.intent.set_llm(intent_llm)
else:
# 否则使用主LLM
self.intent.set_llm(self.llm)
self.logger.bind(tag=TAG).info("意图识别使用主LLM")
# 记录意图识别LLM初始化耗时
intent_llm_init_time = time.time() - intent_llm_init_start
self.logger.bind(tag=TAG).info(f"意图识别LLM初始化完成,耗时: {intent_llm_init_time:.4f}")
"""加载位置信息"""
self.client_ip_info = get_ip_info(self.client_ip)
if self.client_ip_info is not None and "city" in self.client_ip_info:
self.logger.bind(tag=TAG).info(f"Client ip info: {self.client_ip_info}")
self.prompt = self.prompt + f"\nuser location:{self.client_ip_info}"
self.dialogue.update_system_message(self.prompt)
"""加载MCP工具"""
asyncio.run_coroutine_threadsafe(self.mcp_manager.initialize_servers(), self.loop)
def change_system_prompt(self, prompt): def change_system_prompt(self, prompt):
self.prompt = prompt self.prompt = prompt
# 找到原来的role==system,替换原来的系统提示 # 找到原来的role==system,替换原来的系统提示
@@ -316,8 +351,9 @@ class ConnectionHandler:
self.dialogue.put(Message(role="user", content=query)) self.dialogue.put(Message(role="user", content=query))
# Define intent functions # Define intent functions
functions = self.func_handler.get_functions() functions = None
if hasattr(self, 'func_handler'):
functions = self.func_handler.get_functions()
response_message = [] response_message = []
processed_chars = 0 # 跟踪已处理的字符位置 processed_chars = 0 # 跟踪已处理的字符位置
@@ -351,6 +387,9 @@ class ConnectionHandler:
content_arguments = "" content_arguments = ""
for response in llm_responses: for response in llm_responses:
content, tools_call = response content, tools_call = response
if "content" in response:
content = response["content"]
tools_call = None
if content is not None and len(content) > 0: if content is not None and len(content) > 0:
if len(response_message) <= 0 and (content == "```" or "<tool_call>" in content): if len(response_message) <= 0 and (content == "```" or "<tool_call>" in content):
tool_call_flag = True tool_call_flag = True
@@ -429,8 +468,14 @@ class ConnectionHandler:
"id": function_id, "id": function_id,
"arguments": function_arguments "arguments": function_arguments
} }
result = self.func_handler.handle_llm_function_call(self, function_call_data)
self._handle_function_result(result, function_call_data, text_index + 1) # 处理MCP工具调用
if self.mcp_manager.is_mcp_tool(function_name):
result = self._handle_mcp_tool_call(function_call_data)
else:
# 处理系统函数
result = self.func_handler.handle_llm_function_call(self, function_call_data)
self._handle_function_result(result, function_call_data, text_index+1)
# 处理最后剩余的文本 # 处理最后剩余的文本
full_text = "".join(response_message) full_text = "".join(response_message)
@@ -452,6 +497,42 @@ class ConnectionHandler:
return True return True
def _handle_mcp_tool_call(self, function_call_data):
function_arguments = function_call_data["arguments"]
function_name = function_call_data["name"]
try:
args_dict = function_arguments
if isinstance(function_arguments, str):
try:
args_dict = json.loads(function_arguments)
except json.JSONDecodeError:
self.logger.bind(tag=TAG).error(f"无法解析 function_arguments: {function_arguments}")
return ActionResponse(action=Action.REQLLM, result="参数解析失败", response="")
tool_result = asyncio.run_coroutine_threadsafe(self.mcp_manager.execute_tool(
function_name,
args_dict
), self.loop).result()
# meta=None content=[TextContent(type='text', text='北京当前天气:\n温度: 21°C\n天气: 晴\n湿度: 6%\n风向: 西北 风\n风力等级: 5级', annotations=None)] isError=False
content_text = ""
if tool_result is not None and tool_result.content is not None:
for content in tool_result.content:
content_type = content.type
if content_type == "text":
content_text = content.text
elif content_type == "image":
pass
if len(content_text) > 0:
return ActionResponse(action=Action.REQLLM, result=content_text, response="")
except Exception as e:
self.logger.bind(tag=TAG).error(f"MCP工具调用错误: {e}")
return ActionResponse(action=Action.REQLLM, result="工具调用出错", response="")
return ActionResponse(action=Action.REQLLM, result="工具调用出错", response="")
def _handle_function_result(self, result, function_call_data, text_index): def _handle_function_result(self, result, function_call_data, text_index):
if result.action == Action.RESPONSE: # 直接回复前端 if result.action == Action.RESPONSE: # 直接回复前端
text = result.response text = result.response
@@ -492,7 +573,12 @@ class ConnectionHandler:
while not self.stop_event.is_set(): while not self.stop_event.is_set():
text = None text = None
try: try:
future = self.tts_queue.get() try:
future = self.tts_queue.get(timeout=1)
except queue.Empty:
if self.stop_event.is_set():
break
continue
if future is None: if future is None:
continue continue
text = None text = None
@@ -533,7 +619,12 @@ class ConnectionHandler:
while not self.stop_event.is_set(): while not self.stop_event.is_set():
text = None text = None
try: try:
opus_datas, text, text_index = self.audio_play_queue.get() try:
opus_datas, text, text_index = self.audio_play_queue.get(timeout=1)
except queue.Empty:
if self.stop_event.is_set():
break
continue
future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index), future = asyncio.run_coroutine_threadsafe(sendAudioMessage(self, opus_datas, text, text_index),
self.loop) self.loop)
future.result() future.result()
@@ -563,16 +654,43 @@ class ConnectionHandler:
self.tts_first_text_index = text_index self.tts_first_text_index = text_index
self.tts_last_text_index = text_index self.tts_last_text_index = text_index
async def close(self): async def close(self, ws=None):
"""资源清理方法""" """资源清理方法"""
# 清理MCP资源
await self.mcp_manager.cleanup_all()
# 清理其他资源 # 触发停止事件并清理资源
self.stop_event.set() if self.stop_event:
self.executor.shutdown(wait=False) self.stop_event.set()
if self.websocket:
# 立即关闭线程池
if self.executor:
self.executor.shutdown(wait=False, cancel_futures=True)
self.executor = None
# 清空任务队列
self._clear_queues()
if ws:
await ws.close()
elif self.websocket:
await self.websocket.close() await self.websocket.close()
self.logger.bind(tag=TAG).info("连接资源已释放") self.logger.bind(tag=TAG).info("连接资源已释放")
def _clear_queues(self):
# 清空所有任务队列
for q in [self.tts_queue, self.audio_play_queue]:
if not q:
continue
while not q.empty():
try:
q.get_nowait()
except queue.Empty:
continue
q.queue.clear()
# 添加毒丸信号到队列,确保线程退出
# q.queue.put(None)
def reset_vad_states(self): def reset_vad_states(self):
self.client_audio_buffer = bytes() self.client_audio_buffer = bytes()
self.client_have_voice = False self.client_have_voice = False
@@ -50,7 +50,8 @@ class FunctionHandler:
self.function_registry.register_function("handle_exit_intent") self.function_registry.register_function("handle_exit_intent")
self.function_registry.register_function("plugin_loader") self.function_registry.register_function("plugin_loader")
self.function_registry.register_function("get_time") self.function_registry.register_function("get_time")
self.function_registry.register_function("raise_and_lower_the_volume") self.function_registry.register_function("get_lunar")
self.function_registry.register_function("handle_device")
def register_config_functions(self): def register_config_functions(self):
"""注册配置中的函数,可以不同客户端使用不同的配置""" """注册配置中的函数,可以不同客户端使用不同的配置"""
@@ -1,8 +1,74 @@
import json import json
from config.logger import setup_logging from config.logger import setup_logging
from core.handle.sendAudioHandle import send_stt_message
from core.utils.util import remove_punctuation_and_length
import shutil
import asyncio
import os
import random
import time
logger = setup_logging() logger = setup_logging()
WAKEUP_CONFIG = {"dir": "config/assets/", "file_name": "wakeup_words", "create_time": time.time(), "refresh_time": 10,
"words": ["很高兴见到你", "你好啊", "我们又见面了", "最近可好?", "很高兴再次和你谈话", "在干嘛"]}
async def handleHelloMessage(conn): async def handleHelloMessage(conn):
await conn.websocket.send(json.dumps(conn.welcome_msg)) await conn.websocket.send(json.dumps(conn.welcome_msg))
async def checkWakeupWords(conn, text):
enable_wakeup_words_response_cache = conn.config["enable_wakeup_words_response_cache"]
"""是否开启唤醒词加速"""
if not enable_wakeup_words_response_cache:
return False
"""检查是否是唤醒词"""
_, text = remove_punctuation_and_length(text)
if text in conn.config.get("wakeup_words"):
await send_stt_message(conn, text)
conn.tts_first_text_index = 0
conn.tts_last_text_index = 0
conn.llm_finish_task = True
file = getWakeupWordFile(WAKEUP_CONFIG["file_name"])
if file is None:
asyncio.create_task(wakeupWordsResponse(conn))
return False
opus_packets, duration = conn.tts.audio_to_opus_data(file)
conn.audio_play_queue.put((opus_packets, text, 0))
if time.time() - WAKEUP_CONFIG["create_time"] > WAKEUP_CONFIG["refresh_time"]:
asyncio.create_task(wakeupWordsResponse(conn))
return True
return False
def getWakeupWordFile(file_name):
for file in os.listdir(WAKEUP_CONFIG["dir"]):
if file.startswith("my_" + file_name):
"""避免缓存文件是一个空文件"""
if os.stat(f"config/assets/{file}").st_size > (5 * 1024):
return f"config/assets/{file}"
"""查找config/assets/目录下名称为wakeup_words的文件"""
for file in os.listdir(WAKEUP_CONFIG["dir"]):
if file.startswith(file_name):
return f"config/assets/{file}"
return None
async def wakeupWordsResponse(conn):
"""唤醒词响应"""
wakeup_word = random.choice(WAKEUP_CONFIG["words"])
result = conn.llm.response_no_stream(conn.config["prompt"], wakeup_word)
tts_file = await asyncio.to_thread(conn.tts.to_tts, result)
if tts_file is not None and os.path.exists(tts_file):
file_type = os.path.splitext(tts_file)[1]
if file_type:
file_type = file_type.lstrip('.')
old_file = getWakeupWordFile("my_" + WAKEUP_CONFIG["file_name"])
if old_file is not None:
os.remove(old_file)
"""将文件挪到"wakeup_words.mp3"""
shutil.move(tts_file, WAKEUP_CONFIG["dir"] + "my_" + WAKEUP_CONFIG["file_name"] + "." + file_type)
WAKEUP_CONFIG["create_time"] = time.time()
@@ -2,7 +2,10 @@ from config.logger import setup_logging
import json import json
import uuid import uuid
from core.handle.sendAudioHandle import send_stt_message from core.handle.sendAudioHandle import send_stt_message
from core.handle.helloHandle import checkWakeupWords
from core.utils.util import remove_punctuation_and_length from core.utils.util import remove_punctuation_and_length
from core.utils.dialogue import Message
from loguru import logger
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
@@ -12,15 +15,19 @@ async def handle_user_intent(conn, text):
# 检查是否有明确的退出命令 # 检查是否有明确的退出命令
if await check_direct_exit(conn, text): if await check_direct_exit(conn, text):
return True return True
# 检查是否是唤醒词
if await checkWakeupWords(conn, text):
return True
if conn.use_function_call_mode: if conn.use_function_call_mode:
# 使用支持function calling的聊天方法,不再进行意图分析 # 使用支持function calling的聊天方法,不再进行意图分析
return False return False
# 使用LLM进行意图分析 # 使用LLM进行意图分析
intent = await analyze_intent_with_llm(conn, text) intent_result = await analyze_intent_with_llm(conn, text)
if not intent: if not intent_result:
return False return False
# 处理各种意图 # 处理各种意图
return await process_intent_result(conn, intent, text) return await process_intent_result(conn, intent_result, text)
async def check_direct_exit(conn, text): async def check_direct_exit(conn, text):
@@ -45,52 +52,65 @@ async def analyze_intent_with_llm(conn, text):
dialogue = conn.dialogue dialogue = conn.dialogue
try: try:
intent_result = await conn.intent.detect_intent(conn, dialogue.dialogue, text) intent_result = await conn.intent.detect_intent(conn, dialogue.dialogue, text)
# 尝试解析JSON结果 return intent_result
try:
intent_data = json.loads(intent_result)
if "intent" in intent_data:
return intent_data["intent"]
except json.JSONDecodeError:
# 如果不是JSON格式,尝试直接获取意图文本
return intent_result.strip()
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"意图识别失败: {str(e)}") logger.bind(tag=TAG).error(f"意图识别失败: {str(e)}")
return None return None
async def process_intent_result(conn, intent, original_text): async def process_intent_result(conn, intent_result, original_text):
"""处理意图识别结果""" """处理意图识别结果"""
# 处理退出意图 try:
if "结束聊天" in intent: # 尝试将结果解析为JSON
logger.bind(tag=TAG).info(f"识别到退出意图: {intent}") intent_data = json.loads(intent_result)
# 如果是明确的离别意图,发送告别语并关闭连接
await send_stt_message(conn, original_text)
conn.executor.submit(conn.chat_and_close, original_text)
return True
# 处理播放音乐意图 # 检查是否有function_call
if "播放音乐" in intent: if "function_call" in intent_data:
logger.bind(tag=TAG).info(f"识别到音乐播放意图: {intent}") # 直接从意图识别获取了function_call
# 调用play_music函数来播放音乐 logger.bind(tag=TAG).info(f"检测到function_call格式的意图结果: {intent_data['function_call']['name']}")
song_name = extract_text_in_brackets(intent) function_name = intent_data["function_call"]["name"]
function_id = str(uuid.uuid4().hex) if function_name == "continue_chat":
function_name = "play_music" return False
function_arguments = '{ "song_name": "' + song_name + '" }' function_args = None
if "arguments" in intent_data["function_call"]:
function_args = intent_data["function_call"]["arguments"]
# 确保参数是字符串格式的JSON
if isinstance(function_args, dict):
function_args = json.dumps(function_args)
function_call_data = { function_call_data = {
"name": function_name, "name": function_name,
"id": function_id, "id": str(uuid.uuid4().hex),
"arguments": function_arguments "arguments": function_args
} }
conn.func_handler.handle_llm_function_call(conn, function_call_data)
return True
# 其他意图处理可以在这里扩展 await send_stt_message(conn, original_text)
# 默认返回False,表示继续常规聊天流程 # 使用executor执行函数调用和结果处理
return False def process_function_call():
conn.dialogue.put(Message(role="user", content=original_text))
result = conn.func_handler.handle_llm_function_call(conn, function_call_data)
if result and function_name != 'play_music':
# 获取当前最新的文本索引
text = result.response
if text is None:
text = result.result
if text is not None:
text_index = conn.tts_last_text_index + 1 if hasattr(conn, 'tts_last_text_index') else 0
conn.recode_first_last_text(text, text_index)
future = conn.executor.submit(conn.speak_and_play, text, text_index)
conn.llm_finish_task = True
conn.tts_queue.put(future)
conn.dialogue.put(Message(role="assistant", content=text))
# 将函数执行放在线程池中
conn.executor.submit(process_function_call)
return True
return False
except json.JSONDecodeError as e:
logger.bind(tag=TAG).error(f"处理意图结果时出错: {e}")
return False
def extract_text_in_brackets(s): def extract_text_in_brackets(s):
@@ -106,4 +126,4 @@ def extract_text_in_brackets(s):
if left_bracket_index != -1 and right_bracket_index != -1 and left_bracket_index < right_bracket_index: if left_bracket_index != -1 and right_bracket_index != -1 and left_bracket_index < right_bracket_index:
return s[left_bracket_index + 1:right_bracket_index] return s[left_bracket_index + 1:right_bracket_index]
else: else:
return "" return ""
@@ -7,40 +7,15 @@ from core.utils.util import remove_punctuation_and_length, get_string_no_punctua
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
async def sendAudioMessage(conn, audios, text, text_index=0): async def sendAudioMessage(conn, audios, text, text_index=0):
# 发送句子开始消息 # 发送句子开始消息
if text_index == conn.tts_first_text_index: if text_index == conn.tts_first_text_index:
logger.bind(tag=TAG).info(f"发送第一段语音: {text}") logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
await send_tts_message(conn, "sentence_start", text) await send_tts_message(conn, "sentence_start", text)
# 流控参数优化 # 播放音频
original_frame_duration = 60 # 原始帧时长(毫秒) await sendAudio(conn, audios)
adjusted_frame_duration = int(original_frame_duration * 0.8) # 缩短20%
total_frames = len(audios) # 获取总帧数
compensation = total_frames * (original_frame_duration - adjusted_frame_duration) / 1000 # 补偿时间(秒)
start_time = time.perf_counter()
play_position = 0 # 已播放时长(毫秒)
for opus_packet in audios:
if conn.client_abort:
return
# 计算带加速因子的预期时间
expected_time = start_time + (play_position / 1000)
current_time = time.perf_counter()
# 流控等待(使用加速后的帧时长)
delay = expected_time - current_time
if delay > 0:
await asyncio.sleep(delay)
await conn.websocket.send(opus_packet)
play_position += adjusted_frame_duration # 使用调整后的帧时长
# 补偿因加速损失的时长
if compensation > 0:
await asyncio.sleep(compensation)
await send_tts_message(conn, "sentence_end", text) await send_tts_message(conn, "sentence_end", text)
@@ -50,6 +25,38 @@ async def sendAudioMessage(conn, audios, text, text_index=0):
if conn.close_after_chat: if conn.close_after_chat:
await conn.close() await conn.close()
# 播放音频
async def sendAudio(conn, audios):
# 流控参数优化
frame_duration = 60 # 帧时长(毫秒),匹配 Opus 编码
start_time = time.perf_counter()
play_position = 0
# 预缓冲:发送前 3 帧
pre_buffer = min(3, len(audios))
for i in range(pre_buffer):
await conn.websocket.send(audios[i])
conn.logger.bind(tag=TAG).debug(f"预缓冲帧 {i}, 时间: {(time.perf_counter() - start_time) * 1000:.2f}ms")
# 正常播放剩余帧
for opus_packet in audios[pre_buffer:]:
if conn.client_abort:
return
# 计算预期发送时间
expected_time = start_time + (play_position / 1000)
current_time = time.perf_counter()
delay = expected_time - current_time
if delay > 0:
await asyncio.sleep(delay)
await conn.websocket.send(opus_packet)
conn.logger.bind(tag=TAG).debug(f"发送帧,位置: {play_position}ms, 实际间隔: {(time.perf_counter() - current_time) * 1000:.2f}ms")
play_position += frame_duration
async def send_tts_message(conn, state, text=None): async def send_tts_message(conn, state, text=None):
"""发送 TTS 状态消息""" """发送 TTS 状态消息"""
message = { message = {
@@ -60,10 +67,20 @@ async def send_tts_message(conn, state, text=None):
if text is not None: if text is not None:
message["text"] = text message["text"] = text
await conn.websocket.send(json.dumps(message)) # TTS播放结束
if state == "stop": if state == "stop":
# 播放提示音
tts_notify = conn.config.get("enable_stop_tts_notify", False)
if tts_notify:
stop_tts_notify_voice = conn.config.get("stop_tts_notify_voice", "config/assets/tts_notify.mp3")
audios, duration = conn.tts.audio_to_opus_data(stop_tts_notify_voice)
await sendAudio(conn, audios)
# 清除服务端讲话状态
conn.clearSpeakStatus() conn.clearSpeakStatus()
# 发送消息到客户端
await conn.websocket.send(json.dumps(message))
async def send_stt_message(conn, text): async def send_stt_message(conn, text):
"""发送 STT 状态消息""" """发送 STT 状态消息"""
+17 -1
View File
@@ -2,7 +2,9 @@ from config.logger import setup_logging
import json import json
from core.handle.abortHandle import handleAbortMessage from core.handle.abortHandle import handleAbortMessage
from core.handle.helloHandle import handleHelloMessage from core.handle.helloHandle import handleHelloMessage
from core.utils.util import remove_punctuation_and_length
from core.handle.receiveAudioHandle import startToChat, handleAudioMessage from core.handle.receiveAudioHandle import startToChat, handleAudioMessage
from core.handle.sendAudioHandle import send_stt_message, send_tts_message
from core.handle.iotHandle import handleIotDescriptors, handleIotStatus from core.handle.iotHandle import handleIotDescriptors, handleIotStatus
TAG = __name__ TAG = __name__
@@ -38,7 +40,21 @@ async def handleTextMessage(conn, message):
conn.client_have_voice = False conn.client_have_voice = False
conn.asr_audio.clear() conn.asr_audio.clear()
if "text" in msg_json: if "text" in msg_json:
await startToChat(conn, msg_json["text"]) text = msg_json["text"]
_, text = remove_punctuation_and_length(text)
# 识别是否是唤醒词
is_wakeup_words = text in conn.config.get("wakeup_words")
# 是否开启唤醒词回复
enable_greeting = conn.config.get("enable_greeting", True)
if is_wakeup_words and not enable_greeting:
# 如果是唤醒词,且关闭了唤醒词回复,就不用回答
await send_stt_message(conn, text)
await send_tts_message(conn, "stop", None)
else:
# 否则需要LLM对文字内容进行答复
await startToChat(conn, text)
elif msg_json["type"] == "iot": elif msg_json["type"] == "iot":
if "descriptors" in msg_json: if "descriptors" in msg_json:
await handleIotDescriptors(conn, msg_json["descriptors"]) await handleIotDescriptors(conn, msg_json["descriptors"])
+86
View File
@@ -0,0 +1,86 @@
from datetime import timedelta
from typing import Optional
from contextlib import AsyncExitStack
import os, shutil
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from config.logger import setup_logging
TAG = __name__
class MCPClient:
def __init__(self, config):
# Initialize session and client objects
self.session: Optional[ClientSession] = None
self.exit_stack = AsyncExitStack()
self.logger = setup_logging()
self.config = config
self.tolls = []
async def initialize(self):
args = self.config.get("args", [])
command = (
shutil.which("npx")
if self.config["command"] == "npx"
else self.config["command"]
)
env={**os.environ}
if self.config.get("env"):
env.update(self.config["env"])
server_params = StdioServerParameters(
command=command,
args=args,
env=env
)
stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
self.stdio, self.write = stdio_transport
time_out_delta = timedelta(seconds=15)
self.session = await self.exit_stack.enter_async_context(ClientSession(read_stream=self.stdio, write_stream=self.write, read_timeout_seconds=time_out_delta))
await self.session.initialize()
# List available tools
response = await self.session.list_tools()
tools = response.tools
self.tools = tools
self.logger.bind(tag=TAG).info(f"Connected to server with tools:{[tool.name for tool in tools]}")
def has_tool(self, tool_name):
return any(tool.name == tool_name for tool in self.tools)
def get_available_tools(self):
available_tools = [{"type": "function", "function":{
"name": tool.name,
"description": tool.description,
"parameters": tool.inputSchema
} } for tool in self.tools]
return available_tools
async def call_tool(self, tool_name: str, tool_args: dict):
self.logger.bind(tag=TAG).info(f"MCPClient Calling tool {tool_name} with args: {tool_args}")
try:
response = await self.session.call_tool(tool_name, tool_args)
except Exception as e:
self.logger.bind(tag=TAG).error(f"Error calling tool {tool_name}: {e}")
from types import SimpleNamespace
error_content = SimpleNamespace(
type='text',
text=f"Error calling tool {tool_name}: {e}"
)
error_response = SimpleNamespace(
content=[error_content],
isError=True
)
return error_response
self.logger.bind(tag=TAG).info(f"MCPClient Response from tool {tool_name}: {response}")
return response
async def cleanup(self):
"""Clean up resources"""
await self.exit_stack.aclose()
+110
View File
@@ -0,0 +1,110 @@
"""MCP服务管理器"""
import os, json
from typing import Dict, Any, List
from .MCPClient import MCPClient
from config.logger import setup_logging
from core.utils.util import get_project_dir
from plugins_func.register import register_function, ActionResponse, Action, ToolType
TAG = __name__
class MCPManager:
"""管理多个MCP服务的集中管理器"""
def __init__(self,conn) -> None:
"""
初始化MCP管理器
"""
self.conn = conn
self.logger = setup_logging()
self.config_path = get_project_dir() + 'data/.mcp_server_settings.json'
if os.path.exists(self.config_path) == False:
self.config_path = ""
self.logger.bind(tag=TAG).warning(f"请检查mcp服务配置文件:data/.mcp_server_settings.json")
self.client: Dict[str, MCPClient] = {}
self.tools = []
def load_config(self) -> Dict[str, Any]:
"""加载MCP服务配置
Returns:
Dict[str, Any]: 服务配置字典
"""
if len(self.config_path) == 0:
return {}
try:
with open(self.config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
return config.get('mcpServers', {})
except Exception as e:
self.logger.bind(tag=TAG).error(f"Error loading MCP config from {self.config_path}: {e}")
return {}
async def initialize_servers(self) -> None:
"""初始化所有MCP服务"""
config = self.load_config()
for name, srv_config in config.items():
if not srv_config.get("command"):
self.logger.bind(tag=TAG).warning(f"Skipping server {name}: command not specified")
continue
try:
client = MCPClient(srv_config)
await client.initialize()
self.client[name] = client
self.logger.bind(tag=TAG).info(f"Initialized MCP client: {name}")
client_tools = client.get_available_tools()
self.tools.extend(client_tools)
for tool in client_tools:
func_name = "mcp_"+tool["function"]["name"]
register_function(func_name, tool, ToolType.MCP_CLIENT)(self.execute_tool)
self.conn.func_handler.function_registry.register_function(func_name)
except Exception as e:
self.logger.bind(tag=TAG).error(f"Failed to initialize MCP server {name}: {e}")
self.conn.func_handler.upload_functions_desc()
def get_all_tools(self) -> List[Dict[str, Any]]:
"""获取所有服务的工具function定义
Returns:
List[Dict[str, Any]]: 所有工具的function定义列表
"""
return self.tools
def is_mcp_tool(self, tool_name: str) -> bool:
"""检查是否是MCP工具
Args:
tool_name: 工具名称
Returns:
bool: 是否是MCP工具
"""
for tool in self.tools:
if tool.get("function") != None and tool["function"].get("name") == tool_name:
return True
return False
async def execute_tool(self, tool_name: str, arguments: Dict[str, Any]) -> Any:
"""执行工具调用
Args:
tool_name: 工具名称
arguments: 工具参数
Returns:
Any: 工具执行结果
Raises:
ValueError: 工具未找到时抛出
"""
self.logger.bind(tag=TAG).info(f"Executing tool {tool_name} with arguments: {arguments}")
for client in self.client.values():
if client.has_tool(tool_name):
return await client.call_tool(tool_name, arguments)
raise ValueError(f"Tool {tool_name} not found in any MCP server")
async def cleanup_all(self) -> None:
for name, client in self.client.items():
try:
await client.cleanup()
self.logger.bind(tag=TAG).info(f"Cleaned up MCP client: {name}")
except Exception as e:
self.logger.bind(tag=TAG).error(f"Error cleaning up MCP client {name}: {e}")
self.client.clear()
@@ -10,14 +10,21 @@ class IntentProviderBase(ABC):
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
self.intent_options = config.get("intent_options", { self.intent_options = config.get("intent_options", {
"handle_exit_intent": "结束聊天, 用户发来如再见之类的表示结束的话, 不想再进行对话的时候",
"play_music": "播放音乐, 用户希望你可以播放音乐, 只用于播放音乐的意图",
"get_weather": "查询天气, 用户希望查询某个地点的天气情况",
"get_news": "查询新闻, 用户希望查询最新新闻或特定类型的新闻",
"get_lunar": "用于获取今天的阴历/农历和黄历信息",
"get_time": "获取今天日期或者当前时间信息",
"continue_chat": "继续聊天", "continue_chat": "继续聊天",
"end_chat": "结束聊天",
"play_music": "播放音乐"
}) })
def set_llm(self, llm): def set_llm(self, llm):
self.llm = llm self.llm = llm
logger.bind(tag=TAG).debug("Set LLM for intent provider") # 获取模型名称和类型信息
model_name = getattr(llm, 'model_name', str(llm.__class__.__name__))
# 记录更详细的日志
logger.bind(tag=TAG).info(f"意图识别设置LLM: {model_name}")
@abstractmethod @abstractmethod
async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str: async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
@@ -30,5 +37,6 @@ class IntentProviderBase(ABC):
- "继续聊天" - "继续聊天"
- "结束聊天" - "结束聊天"
- "播放音乐 歌名""随机播放音乐" - "播放音乐 歌名""随机播放音乐"
- "查询天气 地点名""查询天气 [当前位置]"
""" """
pass pass
@@ -0,0 +1,20 @@
from ..base import IntentProviderBase
from typing import List, Dict
from config.logger import setup_logging
TAG = __name__
logger = setup_logging()
class IntentProvider(IntentProviderBase):
async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
"""
默认的意图识别实现,始终返回继续聊天
Args:
dialogue_history: 对话历史记录列表
text: 本次对话记录
Returns:
固定返回"继续聊天"
"""
logger.bind(tag=TAG).debug("Using functionCallProvider, always returning continue chat")
return self.intent_options["continue_chat"]
@@ -3,6 +3,9 @@ from ..base import IntentProviderBase
from plugins_func.functions.play_music import initialize_music_handler from plugins_func.functions.play_music import initialize_music_handler
from config.logger import setup_logging from config.logger import setup_logging
import re import re
import json
import hashlib
import time
TAG = __name__ TAG = __name__
logger = setup_logging() logger = setup_logging()
@@ -13,6 +16,10 @@ class IntentProvider(IntentProviderBase):
super().__init__(config) super().__init__(config)
self.llm = None self.llm = None
self.promot = self.get_intent_system_prompt() self.promot = self.get_intent_system_prompt()
# 添加缓存管理
self.intent_cache = {} # 缓存意图识别结果
self.cache_expiry = 600 # 缓存有效期10分钟
self.cache_max_size = 100 # 最多缓存100个意图
def get_intent_system_prompt(self) -> str: def get_intent_system_prompt(self) -> str:
""" """
@@ -22,62 +29,119 @@ class IntentProvider(IntentProviderBase):
""" """
intent_list = [] intent_list = []
"""
"continue_chat": "1.继续聊天, 除了播放音乐和结束聊天的时候的选项, 比如日常的聊天和问候, 对话等",
"end_chat": "2.结束聊天, 用户发来如再见之类的表示结束的话, 不想再进行对话的时候",
"play_music": "3.播放音乐, 用户希望你可以播放音乐, 只用于播放音乐的意图"
"""
for key, value in self.intent_options.items():
if key == "play_music":
intent_list.append("3.播放音乐, 用户希望你可以播放音乐, 只用于播放音乐的意图")
elif key == "end_chat":
intent_list.append("2.结束聊天, 用户发来如再见之类的表示结束的话, 不想再进行对话的时候")
elif key == "continue_chat":
intent_list.append("1.继续聊天, 除了播放音乐和结束聊天的时候的选项, 比如日常的聊天和问候, 对话等")
else:
intent_list.append(value)
# "如果是唱歌、听歌、播放音乐,请指定歌名,格式为'播放音乐 [识别出的歌名]'。\n"
# "如果听不出具体歌名,可以返回'随机播放音乐'。\n"
# "只需要返回意图结果的json,不要解释。"
# "返回格式如下:\n"
prompt = ( prompt = (
"你是一个意图识别助手。你需要根据和用户的对话记录,重点分析用户的最后一句话,判断用户意图属于以下哪一类(使用<start>和<end>标志)\n" "你是一个意图识别助手。分析用户的最后一句话,判断用户意图属于以下哪一类:\n"
"<start>" "<start>"
f"{', '.join(intent_list)}" f"{', '.join(intent_list)}"
"<end>\n" "<end>\n"
"你需要按照以下的步骤处理用户的对话" "处理步骤:"
"1. 思考出对话的意图是哪一类的" "1. 思考意图类型,生成function_call格式"
"2. 属于1和2的意图, 直接返回,返回格式如下:\n" "\n\n"
"{intent: '用户意图'}\n" "返回格式示例:\n"
"3. 属于3的意图,则继续分析用户希望播放音乐\n" "1. 播放音乐意图: {\"function_call\": {\"name\": \"play_music\", \"arguments\": {\"song_name\": \"音乐名称\"}}}\n"
"4. 如果无法识别出具体歌名,可以返回'随机播放音乐'\n" "2. 查询天气意图: {\"function_call\": {\"name\": \"get_weather\", \"arguments\": {\"location\": \"地点名称\", \"lang\": \"zh_CN\"}}}\n"
"{intent: '播放音乐 [获取的音乐名字]'}\n" "3. 查询新闻意图: {\"function_call\": {\"name\": \"get_news\", \"arguments\": {\"category\": \"新闻类别\", \"detail\": false, \"lang\": \"zh_CN\"}}}\n"
"下面是几个处理的示例(思考的内容不返回, 只返回json部分, 无额外的内容)\n" "4. 结束对话意图: {\"function_call\": {\"name\": \"handle_exit_intent\", \"arguments\": {\"say_goodbye\": \"goodbye\"}}}\n"
"```" "5. 获取当天日期时间: {\"function_call\": {\"name\": \"get_time\"}}\n"
"6. 获取当前黄历意图: {\"function_call\": {\"name\": \"get_lunar\"}}\n"
"7. 继续聊天意图: {\"function_call\": {\"name\": \"continue_chat\"}}\n"
"\n"
"注意:\n"
"- 播放音乐:无歌名时,song_name设为\"random\"\n"
"- 查询天气:无地点时,location设为null\n"
"- 查询新闻:无类别时,category设为null;查询详情时,detail设为true\n"
"- 如果没有明显的意图,应按照继续聊天意图处理\n"
"- 只返回纯JSON,不要任何其他内容\n"
"\n"
"示例分析:\n"
"```\n"
"用户: 你好小智\n"
"返回: {\"function_call\": {\"name\": \"continue_chat\"}}\n"
"```\n"
"```\n"
"用户: 你今天怎么样?\n" "用户: 你今天怎么样?\n"
"思考(不返回): 用户发来的数据是一个问候语,属于继续聊天的意图, 是种类1, 种类1的需求是直接返回\n" "返回: {\"function_call\": {\"name\": \"continue_chat\"}}\n"
"返回结果: {intent: '继续聊天'}\n" "```\n"
"```" "```\n"
"用户: 我今天有点累了, 我们明天再聊吧\n" "用户: 现在是几号了?现在几点了?\n"
"思考(不返回): 用户表达了今天不想继续对话,属于结束聊天的意图, 是种类2, 种类2的需求是直接返回\n" "返回: {\"function_call\": {\"name\": \"get_time\"}}\n"
"返回结果: {intent: '结束聊天'}\n" "```\n"
"```" "```\n"
"用户: 今天有点累了, 我们明天再聊吧\n" "用户: 今天农历是多少?\n"
"思考(不返回): 用户表达了今天不想继续对话,属于结束聊天的意图, 是种类2, 种类2的需求是直接返回\n" "返回: {\"function_call\": {\"name\": \"get_lunar\"}}\n"
"返回结果: {intent: '结束聊天'}\n" "```\n"
"```" "```\n"
"用户: 你可以播放一首中秋月给我听吗\n" "用户: 我们明天再聊吧\n"
"思考(不返回): 用户表达了想听音乐的续签,属于播放音乐的意图, 是种类3, 种类3的需求需要继续判断播放的音乐, 这里用户希望的歌曲名明确给出是中秋月\n" "返回: {\"function_call\": {\"name\": \"handle_exit_intent\"}}\n"
"返回结果: {intent: '播放音乐 [中秋月]'}\n" "```\n"
"```" "```\n"
"你现在可以使用的音乐的名称如下(使用<start>和<end>标志):\n" "用户: 播放中秋月\n"
"返回: {\"function_call\": {\"name\": \"play_music\", \"arguments\": {\"song_name\": \"中秋月\"}}}\n"
"```\n"
"```\n"
"用户: 北京天气怎么样\n"
"返回: {\"function_call\": {\"name\": \"get_weather\", \"arguments\": {\"location\": \"北京\", \"lang\": \"zh_CN\"}}}\n"
"```\n"
"```\n"
"用户: 今天天气怎么样\n"
"返回: {\"function_call\": {\"name\": \"get_weather\", \"arguments\": {\"location\": null, \"lang\": \"zh_CN\"}}}\n"
"```\n"
"```\n"
"用户: 播报财经新闻\n"
"返回: {\"function_call\": {\"name\": \"get_news\", \"arguments\": {\"category\": \"财经\", \"detail\": false, \"lang\": \"zh_CN\"}}}\n"
"```\n"
"```\n"
"用户: 有什么最新新闻\n"
"返回: {\"function_call\": {\"name\": \"get_news\", \"arguments\": {\"category\": null, \"detail\": false, \"lang\": \"zh_CN\"}}}\n"
"```\n"
"```\n"
"用户: 详细介绍一下这条新闻\n"
"返回: {\"function_call\": {\"name\": \"get_news\", \"arguments\": {\"detail\": true, \"lang\": \"zh_CN\"}}}\n"
"```\n"
"可用的音乐名称:\n"
) )
return prompt return prompt
def clean_cache(self):
"""清理过期缓存"""
now = time.time()
# 找出过期键
expired_keys = [k for k, v in self.intent_cache.items() if now - v['timestamp'] > self.cache_expiry]
for key in expired_keys:
del self.intent_cache[key]
# 如果缓存太大,移除最旧的条目
if len(self.intent_cache) > self.cache_max_size:
# 按时间戳排序并保留最新的条目
sorted_items = sorted(self.intent_cache.items(), key=lambda x: x[1]['timestamp'])
for key, _ in sorted_items[:len(sorted_items) - self.cache_max_size]:
del self.intent_cache[key]
async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str: async def detect_intent(self, conn, dialogue_history: List[Dict], text: str) -> str:
if not self.llm: if not self.llm:
raise ValueError("LLM provider not set") raise ValueError("LLM provider not set")
# 记录整体开始时间
total_start_time = time.time()
# 打印使用的模型信息
model_info = getattr(self.llm, 'model_name', str(self.llm.__class__.__name__))
logger.bind(tag=TAG).info(f"使用意图识别模型: {model_info}")
# 计算缓存键
cache_key = hashlib.md5(text.encode()).hexdigest()
# 检查缓存
if cache_key in self.intent_cache:
cache_entry = self.intent_cache[cache_key]
# 检查缓存是否过期
if time.time() - cache_entry['timestamp'] <= self.cache_expiry:
cache_time = time.time() - total_start_time
logger.bind(tag=TAG).info(f"使用缓存的意图: {cache_key} -> {cache_entry['intent']}, 耗时: {cache_time:.4f}")
return cache_entry['intent']
# 清理缓存
self.clean_cache()
# 构建用户最后一句话的提示 # 构建用户最后一句话的提示
msgStr = "" msgStr = ""
@@ -94,18 +158,78 @@ class IntentProvider(IntentProviderBase):
music_file_names = music_config["music_file_names"] music_file_names = music_config["music_file_names"]
prompt_music = f"{self.promot}\n<start>{music_file_names}\n<end>" prompt_music = f"{self.promot}\n<start>{music_file_names}\n<end>"
logger.bind(tag=TAG).debug(f"User prompt: {prompt_music}") logger.bind(tag=TAG).debug(f"User prompt: {prompt_music}")
# 记录预处理完成时间
preprocess_time = time.time() - total_start_time
logger.bind(tag=TAG).debug(f"意图识别预处理耗时: {preprocess_time:.4f}")
# 使用LLM进行意图识别 # 使用LLM进行意图识别
llm_start_time = time.time()
logger.bind(tag=TAG).info(f"开始LLM意图识别调用, 模型: {model_info}")
intent = self.llm.response_no_stream( intent = self.llm.response_no_stream(
system_prompt=prompt_music, system_prompt=prompt_music,
user_prompt=user_prompt user_prompt=user_prompt
) )
# 使用正则表达式提取大括号中的内容
# 使用正则表达式提取 {} 中的内容 # 记录LLM调用完成时间
match = re.search(r'\{.*?\}', intent) llm_time = time.time() - llm_start_time
logger.bind(tag=TAG).info(f"LLM意图识别完成, 模型: {model_info}, 调用耗时: {llm_time:.4f}")
# 记录后处理开始时间
postprocess_start_time = time.time()
# 清理和解析响应
intent = intent.strip()
# 尝试提取JSON部分
match = re.search(r'\{.*\}', intent, re.DOTALL)
if match: if match:
result = match.group(0) intent = match.group(0)
intent = result
else: # 记录总处理时间
intent = "{intent: '继续聊天'}" total_time = time.time() - total_start_time
logger.bind(tag=TAG).info(f"Detected intent: {intent}") logger.bind(tag=TAG).info(f"【意图识别性能】模型: {model_info}, 总耗时: {total_time:.4f}秒, LLM调用: {llm_time:.4f}秒, 查询: '{text[:20]}...'")
return intent.strip()
# 尝试解析为JSON
try:
intent_data = json.loads(intent)
# 如果包含function_call,则格式化为适合处理的格式
if "function_call" in intent_data:
function_data = intent_data["function_call"]
function_name = function_data.get("name")
function_args = function_data.get("arguments", {})
# 记录识别到的function call
logger.bind(tag=TAG).info(f"识别到function call: {function_name}, 参数: {function_args}")
# 添加到缓存
self.intent_cache[cache_key] = {
'intent': intent,
'timestamp': time.time()
}
# 后处理时间
postprocess_time = time.time() - postprocess_start_time
logger.bind(tag=TAG).debug(f"意图后处理耗时: {postprocess_time:.4f}")
# 确保返回完全序列化的JSON字符串
return intent
else:
# 添加到缓存
self.intent_cache[cache_key] = {
'intent': intent,
'timestamp': time.time()
}
# 后处理时间
postprocess_time = time.time() - postprocess_start_time
logger.bind(tag=TAG).debug(f"意图后处理耗时: {postprocess_time:.4f}")
# 返回普通意图
return intent
except json.JSONDecodeError:
# 后处理时间
postprocess_time = time.time() - postprocess_start_time
logger.bind(tag=TAG).error(f"无法解析意图JSON: {intent}, 后处理耗时: {postprocess_time:.4f}")
# 如果解析失败,默认返回继续聊天意图
return "{\"intent\": \"继续聊天\"}"
@@ -0,0 +1,85 @@
from config.logger import setup_logging
from openai import OpenAI
import json
from core.providers.llm.base import LLMProviderBase
TAG = __name__
logger = setup_logging()
class LLMProvider(LLMProviderBase):
def __init__(self, config):
self.model_name = config.get("model_name")
self.base_url = config.get("base_url", "http://localhost:9997")
# Initialize OpenAI client with Xinference base URL
# 如果没有v1,增加v1
if not self.base_url.endswith("/v1"):
self.base_url = f"{self.base_url}/v1"
logger.bind(tag=TAG).info(f"Initializing Xinference LLM provider with model: {self.model_name}, base_url: {self.base_url}")
try:
self.client = OpenAI(
base_url=self.base_url,
api_key="xinference" # Xinference has a similar setup to Ollama where it doesn't need an actual key
)
logger.bind(tag=TAG).info("Xinference client initialized successfully")
except Exception as e:
logger.bind(tag=TAG).error(f"Error initializing Xinference client: {e}")
raise
def response(self, session_id, dialogue):
try:
logger.bind(tag=TAG).debug(f"Sending request to Xinference with model: {self.model_name}, dialogue length: {len(dialogue)}")
responses = self.client.chat.completions.create(
model=self.model_name,
messages=dialogue,
stream=True
)
is_active=True
for chunk in responses:
try:
delta = chunk.choices[0].delta if getattr(chunk, 'choices', None) else None
content = delta.content if hasattr(delta, 'content') else ''
if content:
if '<think>' in content:
is_active = False
content = content.split('<think>')[0]
if '</think>' in content:
is_active = True
content = content.split('</think>')[-1]
if is_active:
yield content
except Exception as e:
logger.bind(tag=TAG).error(f"Error processing chunk: {e}")
except Exception as e:
logger.bind(tag=TAG).error(f"Error in Xinference response generation: {e}")
yield "【Xinference服务响应异常】"
def response_with_functions(self, session_id, dialogue, functions=None):
try:
logger.bind(tag=TAG).debug(f"Sending function call request to Xinference with model: {self.model_name}, dialogue length: {len(dialogue)}")
if functions:
logger.bind(tag=TAG).debug(f"Function calls enabled with: {[f.get('function', {}).get('name') for f in functions]}")
stream = self.client.chat.completions.create(
model=self.model_name,
messages=dialogue,
stream=True,
tools=functions,
)
for chunk in stream:
delta = chunk.choices[0].delta
content = delta.content
tool_calls = delta.tool_calls
if content:
yield content, tool_calls
elif tool_calls:
yield None, tool_calls
except Exception as e:
logger.bind(tag=TAG).error(f"Error in Xinference function call: {e}")
yield {"type": "content", "content": f"【Xinference服务响应异常: {str(e)}"}
@@ -39,22 +39,22 @@ class AccessToken:
'Version': '2019-02-28'} 'Version': '2019-02-28'}
# 构造规范化的请求字符串 # 构造规范化的请求字符串
query_string = AccessToken._encode_dict(parameters) query_string = AccessToken._encode_dict(parameters)
print('规范化的请求字符串: %s' % query_string) # print('规范化的请求字符串: %s' % query_string)
# 构造待签名字符串 # 构造待签名字符串
string_to_sign = 'GET' + '&' + AccessToken._encode_text('/') + '&' + AccessToken._encode_text(query_string) string_to_sign = 'GET' + '&' + AccessToken._encode_text('/') + '&' + AccessToken._encode_text(query_string)
print('待签名的字符串: %s' % string_to_sign) # print('待签名的字符串: %s' % string_to_sign)
# 计算签名 # 计算签名
secreted_string = hmac.new(bytes(access_key_secret + '&', encoding='utf-8'), secreted_string = hmac.new(bytes(access_key_secret + '&', encoding='utf-8'),
bytes(string_to_sign, encoding='utf-8'), bytes(string_to_sign, encoding='utf-8'),
hashlib.sha1).digest() hashlib.sha1).digest()
signature = base64.b64encode(secreted_string) signature = base64.b64encode(secreted_string)
print('签名: %s' % signature) # print('签名: %s' % signature)
# 进行URL编码 # 进行URL编码
signature = AccessToken._encode_text(signature) signature = AccessToken._encode_text(signature)
print('URL编码后的签名: %s' % signature) # print('URL编码后的签名: %s' % signature)
# 调用服务 # 调用服务
full_url = 'http://nls-meta.cn-shanghai.aliyuncs.com/?Signature=%s&%s' % (signature, query_string) full_url = 'http://nls-meta.cn-shanghai.aliyuncs.com/?Signature=%s&%s' % (signature, query_string)
print('url: %s' % full_url) # print('url: %s' % full_url)
# 提交HTTP GET请求 # 提交HTTP GET请求
response = requests.get(full_url) response = requests.get(full_url)
if response.ok: if response.ok:
@@ -64,7 +64,7 @@ class AccessToken:
token = root_obj[key]['Id'] token = root_obj[key]['Id']
expire_time = root_obj[key]['ExpireTime'] expire_time = root_obj[key]['ExpireTime']
return token, expire_time return token, expire_time
print(response.text) # print(response.text)
return None, None return None, None
@@ -75,37 +75,80 @@ class TTSProvider(TTSProviderBase):
super().__init__(config, delete_audio_file) super().__init__(config, delete_audio_file)
# 新增空值判断逻辑 # 新增空值判断逻辑
access_key_id = config.get("access_key_id") self.access_key_id = config.get("access_key_id")
access_key_secret = config.get("access_key_secret") self.access_key_secret = config.get("access_key_secret")
if access_key_id and access_key_secret:
# 使用密钥对生成临时token
token, expire_time = AccessToken.create_token(access_key_id, access_key_secret)
else:
# 直接使用预生成的长期token
token = config.get("token")
expire_time = None
print('token: %s, expire time(s): %s' % (token, expire_time))
self.appkey = config.get("appkey") self.appkey = config.get("appkey")
self.token = token
self.format = config.get("format", "wav") self.format = config.get("format", "wav")
self.sample_rate = config.get("sample_rate", 16000) self.sample_rate = config.get("sample_rate", 16000)
self.voice = config.get("voice", "xiaoyun") self.voice = config.get("voice", "xiaoyun")
self.volume = config.get("volume", 50) self.volume = config.get("volume", 50)
self.speech_rate = config.get("speech_rate", 0) self.speech_rate = config.get("speech_rate", 0)
self.pitch_rate = config.get("pitch_rate", 0) self.pitch_rate = config.get("pitch_rate", 0)
self.host = config.get("host", "nls-gateway-cn-shanghai.aliyuncs.com") self.host = config.get("host", "nls-gateway-cn-shanghai.aliyuncs.com")
self.api_url = f"https://{self.host}/stream/v1/tts" self.api_url = f"https://{self.host}/stream/v1/tts"
self.header = { self.header = {
"Content-Type": "application/json" "Content-Type": "application/json"
} }
if self.access_key_id and self.access_key_secret:
# 使用密钥对生成临时token
self._refresh_token()
else:
# 直接使用预生成的长期token
self.token = config.get("token")
self.expire_time = None
def _refresh_token(self):
"""刷新Token并记录过期时间"""
if self.access_key_id and self.access_key_secret:
self.token, expire_time_str = AccessToken.create_token(
self.access_key_id,
self.access_key_secret
)
if not expire_time_str:
raise ValueError("无法获取有效的Token过期时间")
try:
#统一转换为字符串处理
expire_str = str(expire_time_str).strip()
if expire_str.isdigit():
expire_time = datetime.fromtimestamp(int(expire_str))
else:
expire_time = datetime.strptime(
expire_str,
"%Y-%m-%dT%H:%M:%SZ"
)
self.expire_time = expire_time.timestamp() - 60
except Exception as e:
raise ValueError(f"无效的过期时间格式: {expire_str}") from e
else:
self.expire_time = None
if not self.token:
raise ValueError("无法获取有效的访问Token")
def _is_token_expired(self):
"""检查Token是否过期"""
if not self.expire_time:
return False # 长期Token不过期
# 新增调试日志
# current_time = time.time()
# remaining = self.expire_time - current_time
# print(f"Token过期检查: 当前时间 {datetime.fromtimestamp(current_time)} | "
# f"过期时间 {datetime.fromtimestamp(self.expire_time)} | "
# f"剩余 {remaining:.2f}秒")
return time.time() > self.expire_time
def generate_filename(self, extension=".wav"): def generate_filename(self, extension=".wav"):
return os.path.join(self.output_file, f"tts-{__name__}{datetime.now().date()}@{uuid.uuid4().hex}{extension}") return os.path.join(self.output_file, f"tts-{__name__}{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
async def text_to_speak(self, text, output_file): async def text_to_speak(self, text, output_file):
if self._is_token_expired():
logger.warning("Token已过期,正在自动刷新...")
self._refresh_token()
request_json = { request_json = {
"appkey": self.appkey, "appkey": self.appkey,
"token": self.token, "token": self.token,
@@ -118,9 +161,12 @@ class TTSProvider(TTSProviderBase):
"pitch_rate": self.pitch_rate "pitch_rate": self.pitch_rate
} }
print(self.api_url, json.dumps(request_json, ensure_ascii=False)) # print(self.api_url, json.dumps(request_json, ensure_ascii=False))
try: try:
resp = requests.post(self.api_url, json.dumps(request_json), headers=self.header) resp = requests.post(self.api_url, json.dumps(request_json), headers=self.header)
if resp.status_code == 401: # Token过期特殊处理
self._refresh_token()
resp = requests.post(self.api_url, json.dumps(request_json), headers=self.header)
# 检查返回请求数据的mime类型是否是audio/***,是则保存到指定路径下;返回的是binary格式的 # 检查返回请求数据的mime类型是否是audio/***,是则保存到指定路径下;返回的是binary格式的
if resp.headers['Content-Type'].startswith('audio/'): if resp.headers['Content-Type'].startswith('audio/'):
with open(output_file, 'wb') as f: with open(output_file, 'wb') as f:
@@ -36,7 +36,7 @@ class TTSProviderBase(ABC):
return tmp_file return tmp_file
except Exception as e: except Exception as e:
logger.bind(tag=TAG).info(f"Failed to generate TTS file: {e}") logger.bind(tag=TAG).error(f"Failed to generate TTS file: {e}")
return None return None
@abstractmethod @abstractmethod
+11 -2
View File
@@ -14,5 +14,14 @@ class TTSProvider(TTSProviderBase):
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}") return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
async def text_to_speak(self, text, output_file): async def text_to_speak(self, text, output_file):
communicate = edge_tts.Communicate(text, voice=self.voice) # Use your preferred voice communicate = edge_tts.Communicate(text, voice=self.voice)
await communicate.save(output_file) # 确保目录存在并创建空文件
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'wb') as f:
pass
# 流式写入音频数据
with open(output_file, 'ab') as f: # 改为追加模式避免覆盖
async for chunk in communicate.stream():
if chunk["type"] == "audio": # 只处理音频数据块
f.write(chunk["data"])
+1 -3
View File
@@ -73,9 +73,7 @@ def get_ip_info(ip_addr):
resp = requests.get(url).json() resp = requests.get(url).json()
ip_info = { ip_info = {
"city": resp.get("cityName"), "city": resp.get("cityName")
"region": resp.get("regionName"),
"country": resp.get("countryName")
} }
return ip_info return ip_info
except Exception as e: except Exception as e:
@@ -63,8 +63,6 @@ class WebSocketServer:
server_config = self.config["server"] server_config = self.config["server"]
host = server_config["ip"] host = server_config["ip"]
port = server_config["port"] port = server_config["port"]
selected_module = self.config.get("selected_module")
self.logger.bind(tag=TAG).info(f"selected_module values: {', '.join(selected_module.values())}")
self.logger.bind(tag=TAG).info("Server is running at ws://{}:{}", get_local_ip(), port) self.logger.bind(tag=TAG).info("Server is running at ws://{}:{}", get_local_ip(), port)
self.logger.bind(tag=TAG).info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======") self.logger.bind(tag=TAG).info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======")
@@ -0,0 +1,31 @@
{
"des": [
"在data目录下创建.mcp_server_settings.json文件,可以选择下面的MCP服务,也可以自行添加新的MCP服务。",
"后面不断测试补充好用的mcp服务,欢迎大家一起补充。",
"记得删除注释行,des属性仅为说明,不会被解析。",
"des和link属性,仅为说明安装方式,方便大家查看原始链接,不是必须项。"
],
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/username/Desktop",
"/path/to/other/allowed/dir"
],
"link":"https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem"
},
"playwright": {
"command": "npx",
"args": ["-y", "@executeautomation/playwright-mcp-server"],
"des" : "run 'npx playwright install' first",
"link": "https://github.com/executeautomation/mcp-playwright"
},
"windows-cli": {
"command": "npx",
"args": ["-y", "@simonb97/server-win-cli"],
"link": "https://github.com/SimonB97/win-cli-mcp-server"
}
}
}
@@ -45,10 +45,10 @@ def fetch_news_from_rss(rss_url):
try: try:
response = requests.get(rss_url) response = requests.get(rss_url)
response.raise_for_status() response.raise_for_status()
# 解析XML # 解析XML
root = ET.fromstring(response.content) root = ET.fromstring(response.content)
# 查找所有item元素(新闻条目) # 查找所有item元素(新闻条目)
news_items = [] news_items = []
for item in root.findall('.//item'): for item in root.findall('.//item'):
@@ -56,14 +56,14 @@ def fetch_news_from_rss(rss_url):
link = item.find('link').text if item.find('link') is not None else "#" link = item.find('link').text if item.find('link') is not None else "#"
description = item.find('description').text if item.find('description') is not None else "无描述" description = item.find('description').text if item.find('description') is not None else "无描述"
pubDate = item.find('pubDate').text if item.find('pubDate') is not None else "未知时间" pubDate = item.find('pubDate').text if item.find('pubDate') is not None else "未知时间"
news_items.append({ news_items.append({
'title': title, 'title': title,
'link': link, 'link': link,
'description': description, 'description': description,
'pubDate': pubDate 'pubDate': pubDate
}) })
return news_items return news_items
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"获取RSS新闻失败: {e}") logger.bind(tag=TAG).error(f"获取RSS新闻失败: {e}")
@@ -75,9 +75,9 @@ def fetch_news_detail(url):
try: try:
response = requests.get(url) response = requests.get(url)
response.raise_for_status() response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser') soup = BeautifulSoup(response.content, 'html.parser')
# 尝试提取正文内容 (这里的选择器需要根据实际网站结构调整) # 尝试提取正文内容 (这里的选择器需要根据实际网站结构调整)
content_div = soup.select_one('.content_desc, .content, article, .article-content') content_div = soup.select_one('.content_desc, .content, article, .article-content')
if content_div: if content_div:
@@ -98,7 +98,7 @@ def map_category(category_text):
"""将用户输入的中文类别映射到配置文件中的类别键""" """将用户输入的中文类别映射到配置文件中的类别键"""
if not category_text: if not category_text:
return None return None
# 类别映射字典,目前支持社会、国际、财经新闻,如需更多类型,参见配置文件 # 类别映射字典,目前支持社会、国际、财经新闻,如需更多类型,参见配置文件
category_map = { category_map = {
# 社会新闻 # 社会新闻
@@ -113,10 +113,10 @@ def map_category(category_text):
"金融": "finance", "金融": "finance",
"经济": "finance" "经济": "finance"
} }
# 转换为小写并去除空格 # 转换为小写并去除空格
normalized_category = category_text.lower().strip() normalized_category = category_text.lower().strip()
# 返回映射结果,如果没有匹配项则返回原始输入 # 返回映射结果,如果没有匹配项则返回原始输入
return category_map.get(normalized_category, category_text) return category_map.get(normalized_category, category_text)
@@ -129,21 +129,22 @@ def get_news(conn, category: str = None, detail: bool = False, lang: str = "zh_C
if detail: if detail:
if not hasattr(conn, 'last_news_link') or not conn.last_news_link or 'link' not in conn.last_news_link: if not hasattr(conn, 'last_news_link') or not conn.last_news_link or 'link' not in conn.last_news_link:
return ActionResponse(Action.REQLLM, "抱歉,没有找到最近查询的新闻,请先获取一条新闻。", None) return ActionResponse(Action.REQLLM, "抱歉,没有找到最近查询的新闻,请先获取一条新闻。", None)
link = conn.last_news_link.get('link') link = conn.last_news_link.get('link')
title = conn.last_news_link.get('title', '未知标题') title = conn.last_news_link.get('title', '未知标题')
if link == '#': if link == '#':
return ActionResponse(Action.REQLLM, "抱歉,该新闻没有可用的链接获取详细内容。", None) return ActionResponse(Action.REQLLM, "抱歉,该新闻没有可用的链接获取详细内容。", None)
logger.bind(tag=TAG).debug(f"获取新闻详情: {title}, URL={link}") logger.bind(tag=TAG).debug(f"获取新闻详情: {title}, URL={link}")
# 获取新闻详情 # 获取新闻详情
detail_content = fetch_news_detail(link) detail_content = fetch_news_detail(link)
if not detail_content or detail_content == "无法获取详细内容": if not detail_content or detail_content == "无法获取详细内容":
return ActionResponse(Action.REQLLM, f"抱歉,无法获取《{title}》的详细内容,可能是链接已失效或网站结构发生变化。", None) return ActionResponse(Action.REQLLM,
f"抱歉,无法获取《{title}》的详细内容,可能是链接已失效或网站结构发生变化。", None)
# 构建详情报告 # 构建详情报告
detail_report = ( detail_report = (
f"根据下列数据,用{lang}回应用户的新闻详情查询请求:\n\n" f"根据下列数据,用{lang}回应用户的新闻详情查询请求:\n\n"
@@ -152,33 +153,33 @@ def get_news(conn, category: str = None, detail: bool = False, lang: str = "zh_C
f"(请对上述新闻内容进行总结,提取关键信息,以自然、流畅的方式向用户播报," f"(请对上述新闻内容进行总结,提取关键信息,以自然、流畅的方式向用户播报,"
f"不要提及这是总结,就像是在讲述一个完整的新闻故事)" f"不要提及这是总结,就像是在讲述一个完整的新闻故事)"
) )
return ActionResponse(Action.REQLLM, detail_report, None) return ActionResponse(Action.REQLLM, detail_report, None)
# 否则,获取新闻列表并随机选择一条 # 否则,获取新闻列表并随机选择一条
# 从配置中获取RSS URL # 从配置中获取RSS URL
rss_config = conn.config["plugins"]["get_news"] rss_config = conn.config["plugins"]["get_news"]
default_rss_url = rss_config.get("default_rss_url", "https://www.chinanews.com.cn/rss/society.xml") default_rss_url = rss_config.get("default_rss_url", "https://www.chinanews.com.cn/rss/society.xml")
# 将用户输入的类别映射到配置中的类别键 # 将用户输入的类别映射到配置中的类别键
mapped_category = map_category(category) mapped_category = map_category(category)
# 如果提供了类别,尝试从配置中获取对应的URL # 如果提供了类别,尝试从配置中获取对应的URL
rss_url = default_rss_url rss_url = default_rss_url
if mapped_category and mapped_category in rss_config.get("category_urls", {}): if mapped_category and mapped_category in rss_config.get("category_urls", {}):
rss_url = rss_config["category_urls"][mapped_category] rss_url = rss_config["category_urls"][mapped_category]
logger.bind(tag=TAG).info(f"获取新闻: 原始类别={category}, 映射类别={mapped_category}, URL={rss_url}") logger.bind(tag=TAG).info(f"获取新闻: 原始类别={category}, 映射类别={mapped_category}, URL={rss_url}")
# 获取新闻列表 # 获取新闻列表
news_items = fetch_news_from_rss(rss_url) news_items = fetch_news_from_rss(rss_url)
if not news_items: if not news_items:
return ActionResponse(Action.REQLLM, "抱歉,未能获取到新闻信息,请稍后再试。", None) return ActionResponse(Action.REQLLM, "抱歉,未能获取到新闻信息,请稍后再试。", None)
# 随机选择一条新闻 # 随机选择一条新闻
selected_news = random.choice(news_items) selected_news = random.choice(news_items)
# 保存当前新闻链接到连接对象,以便后续查询详情 # 保存当前新闻链接到连接对象,以便后续查询详情
if not hasattr(conn, 'last_news_link'): if not hasattr(conn, 'last_news_link'):
conn.last_news_link = {} conn.last_news_link = {}
@@ -186,7 +187,7 @@ def get_news(conn, category: str = None, detail: bool = False, lang: str = "zh_C
'link': selected_news.get('link', '#'), 'link': selected_news.get('link', '#'),
'title': selected_news.get('title', '未知标题') 'title': selected_news.get('title', '未知标题')
} }
# 构建新闻报告 # 构建新闻报告
news_report = ( news_report = (
f"根据下列数据,用{lang}回应用户的新闻查询请求:\n\n" f"根据下列数据,用{lang}回应用户的新闻查询请求:\n\n"
@@ -197,9 +198,9 @@ def get_news(conn, category: str = None, detail: bool = False, lang: str = "zh_C
f"直接读出新闻即可,不需要额外多余的内容。" f"直接读出新闻即可,不需要额外多余的内容。"
f"如果用户询问更多详情,告知用户可以说'请详细介绍这条新闻'获取更多内容)" f"如果用户询问更多详情,告知用户可以说'请详细介绍这条新闻'获取更多内容)"
) )
return ActionResponse(Action.REQLLM, news_report, None) return ActionResponse(Action.REQLLM, news_report, None)
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"获取新闻出错: {e}") logger.bind(tag=TAG).error(f"获取新闻出错: {e}")
return ActionResponse(Action.REQLLM, "抱歉,获取新闻时发生错误,请稍后再试。", None) return ActionResponse(Action.REQLLM, "抱歉,获取新闻时发生错误,请稍后再试。", None)
@@ -1,20 +1,19 @@
from datetime import datetime from datetime import datetime
import cnlunar
from plugins_func.register import register_function, ToolType, ActionResponse, Action from plugins_func.register import register_function, ToolType, ActionResponse, Action
get_time_function_desc = { get_time_function_desc = {
"type": "function", "type": "function",
"function": { "function": {
"name": "get_time", "name": "get_time",
"description": "获取当前时间、日期、星期几", "description": "获取今天日期或者当前时间信息",
'parameters': {'type': 'object', 'properties': {}, 'required': []} 'parameters': {'type': 'object', 'properties': {}, 'required': []}
} }
} }
@register_function('get_time', get_time_function_desc, ToolType.WAIT) @register_function('get_time', get_time_function_desc, ToolType.WAIT)
def get_time(): def get_time():
""" """
获取当前时间、日期、星期几 获取当前的日期时间信息
""" """
now = datetime.now() now = datetime.now()
current_time = now.strftime("%H:%M:%S") current_time = now.strftime("%H:%M:%S")
@@ -22,4 +21,69 @@ def get_time():
current_weekday = now.strftime("%A") current_weekday = now.strftime("%A")
response_text = f"当前日期: {current_date},当前时间: {current_time},星期: {current_weekday}" response_text = f"当前日期: {current_date},当前时间: {current_time},星期: {current_weekday}"
return ActionResponse(Action.REQLLM, response_text, None)
get_lunar_function_desc = {
"type": "function",
"function": {
"name": "get_lunar",
"description": (
"用于获取今天的阴历/农历和黄历信息。"
"用户可以指定查询内容,如:阴历日期、天干地支、节气、生肖、星座、八字、宜忌等。"
"如果没有指定查询内容,则默认查询干支年和农历日期。"
),
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "要查询的内容,例如阴历日期、天干地支、节日、节气、生肖、星座、八字、宜忌等"
}
},
"required": []
}
}
}
@register_function('get_lunar', get_lunar_function_desc, ToolType.WAIT)
def get_lunar(query=None):
"""
用于获取当前的阴历/农历,和天干地支、节气、生肖、星座、八字、宜忌等黄历信息
"""
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%Y-%m-%d")
current_weekday = now.strftime("%A")
# 如果 query 为 None,则使用默认文本
if query is None:
query = "默认查询干支年和农历日期"
response_text = f"根据以下信息回应用户的查询请求,并提供与{query}相关的信息:\n"
lunar = cnlunar.Lunar(now, godType='8char')
response_text += (
f"当前公历日期: {current_date},当前时间: {current_time},星期: {current_weekday}\n"
"农历信息:\n"
"%s%s%s\n" % (lunar.lunarYearCn, lunar.lunarMonthCn[:-1], lunar.lunarDayCn) +
"干支: %s%s%s\n" % (lunar.year8Char, lunar.month8Char, lunar.day8Char) +
"生肖: 属%s\n" % (lunar.chineseYearZodiac) +
"八字: %s\n" % (' '.join([lunar.year8Char, lunar.month8Char, lunar.day8Char, lunar.twohour8Char])) +
"今日节日: %s\n" % (",".join(filter(None, (lunar.get_legalHolidays(), lunar.get_otherHolidays(), lunar.get_otherLunarHolidays())))) +
"今日节气: %s\n" % (lunar.todaySolarTerms) +
"下一节气: %s %s%s%s\n" % (lunar.nextSolarTerm, lunar.nextSolarTermYear, lunar.nextSolarTermDate[0], lunar.nextSolarTermDate[1]) +
"今年节气表: %s\n" % (', '.join([f"{term}({date[0]}{date[1]}日)" for term, date in lunar.thisYearSolarTermsDic.items()])) +
"生肖冲煞: %s\n" % (lunar.chineseZodiacClash) +
"星座: %s\n" % (lunar.starZodiac) +
"纳音: %s\n" % lunar.get_nayin() +
"彭祖百忌: %s\n" % (lunar.get_pengTaboo(delimit=", ")) +
"值日: %s执位\n" % lunar.get_today12DayOfficer()[0] +
"值神: %s(%s)\n" % (lunar.get_today12DayOfficer()[1], lunar.get_today12DayOfficer()[2]) +
"廿八宿: %s\n" % lunar.get_the28Stars() +
"吉神方位: %s\n" % ' '.join(lunar.get_luckyGodsDirection()) +
"今日胎神: %s\n" % lunar.get_fetalGod() +
"宜: %s\n" % ''.join(lunar.goodThing[:10]) +
"忌: %s\n" % ''.join(lunar.badThing[:10]) +
"(默认返回干支年和农历日期;仅在要求查询宜忌信息时才返回本日宜忌)"
)
return ActionResponse(Action.REQLLM, response_text, None) return ActionResponse(Action.REQLLM, response_text, None)
@@ -39,6 +39,23 @@ HEADERS = {
) )
} }
# 天气代码 https://dev.qweather.com/docs/resource/icons/#weather-icons
WEATHER_CODE_MAP = {
"100": "", "101": "多云", "102": "少云", "103": "晴间多云", "104": "",
"150": "", "151": "多云", "152": "少云", "153": "晴间多云",
"300": "阵雨", "301": "强阵雨", "302": "雷阵雨", "303": "强雷阵雨", "304": "雷阵雨伴有冰雹",
"305": "小雨", "306": "中雨", "307": "大雨", "308": "极端降雨", "309": "毛毛雨/细雨",
"310": "暴雨", "311": "大暴雨", "312": "特大暴雨", "313": "冻雨", "314": "小到中雨",
"315": "中到大雨", "316": "大到暴雨", "317": "暴雨到大暴雨", "318": "大暴雨到特大暴雨",
"350": "阵雨", "351": "强阵雨", "399": "",
"400": "小雪", "401": "中雪", "402": "大雪", "403": "暴雪", "404": "雨夹雪",
"405": "雨雪天气", "406": "阵雨夹雪", "407": "阵雪", "408": "小到中雪", "409": "中到大雪", "410": "大到暴雪",
"456": "阵雨夹雪", "457": "阵雪", "499": "",
"500": "薄雾", "501": "", "502": "", "503": "扬沙", "504": "浮尘",
"507": "沙尘暴", "508": "强沙尘暴",
"509": "浓雾", "510": "强浓雾", "511": "中度霾", "512": "重度霾", "513": "严重霾", "514": "大雾", "515": "特强浓雾",
"900": "", "901": "", "999": "未知"
}
def fetch_city_info(location, api_key): def fetch_city_info(location, api_key):
url = f"https://geoapi.qweather.com/v2/city/lookup?key={api_key}&location={location}&lang=zh" url = f"https://geoapi.qweather.com/v2/city/lookup?key={api_key}&location={location}&lang=zh"
@@ -67,9 +84,11 @@ def parse_weather_info(soup):
temps_list = [] temps_list = []
for row in soup.select(".city-forecast-tabs__row")[:7]: # 取前7天的数据 for row in soup.select(".city-forecast-tabs__row")[:7]: # 取前7天的数据
date = row.select_one(".date-bg .date").get_text(strip=True) date = row.select_one(".date-bg .date").get_text(strip=True)
weather_code = row.select_one(".date-bg .icon")["src"].split("/")[-1].split(".")[0]
weather = WEATHER_CODE_MAP.get(weather_code, "未知")
temps = [span.get_text(strip=True) for span in row.select(".tmp-cont .temp")] temps = [span.get_text(strip=True) for span in row.select(".tmp-cont .temp")]
high_temp, low_temp = (temps[0], temps[-1]) if len(temps) >= 2 else (None, None) high_temp, low_temp = (temps[0], temps[-1]) if len(temps) >= 2 else (None, None)
temps_list.append((date, high_temp, low_temp)) temps_list.append((date, weather, high_temp, low_temp))
return city_name, current_abstract, current_basic, temps_list return city_name, current_abstract, current_basic, temps_list
@@ -91,14 +110,14 @@ def get_weather(conn, location: str = None, lang: str = "zh_CN"):
city_name, current_abstract, current_basic, temps_list = parse_weather_info(soup) city_name, current_abstract, current_basic, temps_list = parse_weather_info(soup)
weather_report = f"根据下列数据,用{lang}回应用户的查询天气请求:\n{city_name}未来7天天气:\n" weather_report = f"根据下列数据,用{lang}回应用户的查询天气请求:\n{city_name}未来7天天气:\n"
for i, (date, high, low) in enumerate(temps_list): for i, (date, weather, high, low) in enumerate(temps_list):
if high and low: if high and low:
weather_report += f"{date}: {low}{high}\n" weather_report += f"{date}: {low}{high}, {weather}\n"
weather_report += ( weather_report += (
f"当前天气: {current_abstract}\n" f"当前天气: {current_abstract}\n"
f"当前天气参数: {current_basic}\n" f"当前天气参数: {current_basic}\n"
f"(确保只报告指定单日的气温范围,除非用户明确要求想要了解多日天气,如果未指定,默认报告今天的温度范围" f"(确保只报告指定单日的天气情况,除非未来会出现异常天气;或者用户明确要求想要了解多日天气,如果未指定,默认报告今天的天气"
"参数为0的值不需要报告给用户,每次都报告体感温度,根据语境选择合适的参数内容告知用户,并对参数给出相应评价)" "参数为0的值不需要报告给用户,每次都报告体感温度,根据语境选择合适的参数内容告知用户,并对参数给出相应评价)"
) )
return ActionResponse(Action.REQLLM, weather_report, None) return ActionResponse(Action.REQLLM, weather_report, None)
@@ -0,0 +1,105 @@
from config.logger import setup_logging
from plugins_func.register import register_function, ToolType, ActionResponse, Action
from core.handle.iotHandle import get_iot_status, send_iot_conn
import asyncio
TAG = __name__
logger = setup_logging()
async def _get_device_status(conn, device_name, device_type, property_name):
"""获取设备状态"""
status = await get_iot_status(conn, device_type, property_name)
if status is None:
raise Exception(f"你的设备不支持{device_name}控制")
return status
async def _set_device_property(conn, device_name, device_type, method_name, property_name, new_value=None, action=None, step=10):
"""设置设备属性"""
current_value = await _get_device_status(conn, device_name, device_type, property_name)
if action == 'raise':
current_value += step
elif action == 'lower':
current_value -= step
elif action == 'set':
if new_value is None:
raise Exception(f"缺少{property_name}参数")
current_value = new_value
# 限制属性范围在0到100之间
current_value = max(0, min(100, current_value))
await send_iot_conn(conn, device_type, method_name, {property_name: current_value})
return current_value
def _handle_device_action(conn, func, success_message, error_message, *args, **kwargs):
"""处理设备操作的通用函数"""
future = asyncio.run_coroutine_threadsafe(func(conn, *args, **kwargs), conn.loop)
try:
result = future.result()
logger.bind(tag=TAG).info(f"{success_message}: {result}")
response = f"{success_message}{result}"
return ActionResponse(action=Action.RESPONSE, result=result, response=response)
except Exception as e:
logger.bind(tag=TAG).error(f"{error_message}: {e}")
response = f"{error_message}: {e}"
return ActionResponse(action=Action.RESPONSE, result=None, response=response)
# 设备控制
handle_device_function_desc = {
"type": "function",
"function": {
"name": "handle_device",
"description": (
"用户想要获取或者设置设备的音量/亮度大小,或者用户觉得声音/亮度过高或过低,或者用户想提高或降低音量/亮度。"
"比如用户说现在亮度多少,参数为:device_type:Backlight,action:get。"
"比如用户说设置音量为50,参数为:device_type:Speaker,action:set,value:50。"
"比如用户说亮度太高了,参数为:device_type:Backlight,action:lower。"
"比如用户说调大音量,参数为:device_type:Speaker,action:raise。"
),
"parameters": {
"type": "object",
"properties": {
"device_type": {
"type": "string",
"description": "设备类型,可选值:Speaker(音量),Backlight(亮度)"
},
"action": {
"type": "string",
"description": "动作名称,可选值:get(获取),set(设置),raise(提高),lower(降低)"
},
"value": {
"type": "number",
"description": "值大小,可选值:0-100之间的整数"
}
},
"required": ["device_type", "action"]
}
}
}
@register_function('handle_device', handle_device_function_desc, ToolType.IOT_CTL)
def handle_device(conn, device_type: str, action: str, value: int = None):
if device_type == "Speaker":
method_name, property_name, device_name = "SetVolume", "volume", "音量"
elif device_type == "Backlight":
method_name, property_name, device_name = "SetBrightness", "brightness", "亮度"
else:
raise Exception(f"未识别的设备类型: {device_type}")
if action not in ["get", "set", "raise", "lower"]:
raise Exception(f"未识别的动作名称: {action}")
if action == "get":
# get
return _handle_device_action(
conn, _get_device_status, f"当前{device_name}", f"获取{device_name}失败",
device_name=device_name, device_type=device_type, property_name=property_name,
)
else:
# set, raise, lower
return _handle_device_action(
conn, _set_device_property, f"{device_name}已调整到", f"{device_name}调整失败",
device_name=device_name, device_type=device_type, method_name=method_name,
property_name=property_name, new_value=value, action=action
)
@@ -1,64 +0,0 @@
from config.logger import setup_logging
from plugins_func.register import register_function, ToolType, ActionResponse, Action
from core.handle.iotHandle import get_iot_status, send_iot_conn
import asyncio
TAG = __name__
logger = setup_logging()
raise_and_lower_the_volume_function_desc = {
"type": "function",
"function": {
"name": "raise_and_lower_the_volume",
"description": "用户觉得声音过高或过低,或者用户想提高或降低音量。比如用户说太大声了,参数为:lower,比如用户说提高音量,参数为:raise",
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"description": "动作名称,要么是raise,要么是lower"
}
},
"required": ["action"]
}
}
}
@register_function('raise_and_lower_the_volume', raise_and_lower_the_volume_function_desc, ToolType.IOT_CTL)
def raise_and_lower_the_volume(conn, action: str):
"""
获取当前设备音量
"""
future = asyncio.run_coroutine_threadsafe(
_raise_and_lower_the_volume(conn, action),
conn.loop
)
try:
new_volume = future.result() # 同步等待异步操作完成
logger.bind(tag=TAG).info(f"音量操作完成: {new_volume}")
response = f"音量已调整到{new_volume}"
except Exception as e:
logger.bind(tag=TAG).error(f"音量操作失败: {e}")
response = f"音量调整失败: {e}"
return ActionResponse(action=Action.RESPONSE, result="指令已接收", response=response)
async def _raise_and_lower_the_volume(conn, action):
volume = await get_iot_status(conn, "Speaker", "volume")
if volume is None:
raise Exception("你的设备不支持音量控制")
if action == 'raise':
volume += 10
elif action == 'lower':
volume -= 10
# 限制音量范围在0到100之间
if volume < 0:
volume = 0
elif volume > 100:
volume = 100
await send_iot_conn(conn, "Speaker", "SetVolume", {"volume": volume})
return volume
@@ -22,6 +22,4 @@ def auto_import_modules(package_name):
# 导入模块 # 导入模块
full_module_name = f"{package_name}.{module_name}" full_module_name = f"{package_name}.{module_name}"
importlib.import_module(full_module_name) importlib.import_module(full_module_name)
#logger.bind(tag=TAG).info(f"模块 '{full_module_name}' 已加载") #logger.bind(tag=TAG).info(f"模块 '{full_module_name}' 已加载")
auto_import_modules('plugins_func.functions')
@@ -12,6 +12,7 @@ class ToolType(Enum):
CHANGE_SYS_PROMPT = (3, "修改系统提示词,切换角色性格或职责") CHANGE_SYS_PROMPT = (3, "修改系统提示词,切换角色性格或职责")
SYSTEM_CTL = (4, "系统控制,影响正常的对话流程,如退出、播放音乐等,需要传递conn参数") SYSTEM_CTL = (4, "系统控制,影响正常的对话流程,如退出、播放音乐等,需要传递conn参数")
IOT_CTL = (5, "IOT设备控制,需要传递conn参数") IOT_CTL = (5, "IOT设备控制,需要传递conn参数")
MCP_CLIENT = (6, "MCP客户端")
def __init__(self, code, message): def __init__(self, code, message):
self.code = code self.code = code
+3
View File
@@ -22,3 +22,6 @@ mem0ai==0.1.62
bs4==0.0.2 bs4==0.0.2
modelscope==1.23.2 modelscope==1.23.2
sherpa_onnx==1.11.0 sherpa_onnx==1.11.0
mcp==1.4.1
cnlunar==0.2.0