mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
Merge branch 'xinnan-tech:main' into main
This commit is contained in:
commit
ad13f8c02c
7 changed files with 415 additions and 3 deletions
|
|
@ -0,0 +1,78 @@
|
|||
package xiaozhi.common.utils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
public class AESUtils {
|
||||
|
||||
private static final String ALGORITHM = "AES";
|
||||
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
|
||||
|
||||
/**
|
||||
* AES加密
|
||||
*
|
||||
* @param key 密钥(16位、24位或32位)
|
||||
* @param plainText 待加密字符串
|
||||
* @return 加密后的Base64字符串
|
||||
*/
|
||||
public static String encrypt(String key, String plainText) {
|
||||
try {
|
||||
// 确保密钥长度为16、24或32位
|
||||
byte[] keyBytes = padKey(key.getBytes(StandardCharsets.UTF_8));
|
||||
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
|
||||
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
|
||||
return Base64.getEncoder().encodeToString(encryptedBytes);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("AES加密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
*
|
||||
* @param key 密钥(16位、24位或32位)
|
||||
* @param encryptedText 待解密的Base64字符串
|
||||
* @return 解密后的字符串
|
||||
*/
|
||||
public static String decrypt(String key, String encryptedText) {
|
||||
try {
|
||||
// 确保密钥长度为16、24或32位
|
||||
byte[] keyBytes = padKey(key.getBytes(StandardCharsets.UTF_8));
|
||||
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
|
||||
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||
|
||||
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
|
||||
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
|
||||
return new String(decryptedBytes, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("AES解密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 填充密钥到指定长度(16、24或32位)
|
||||
*
|
||||
* @param keyBytes 原始密钥字节数组
|
||||
* @return 填充后的密钥字节数组
|
||||
*/
|
||||
private static byte[] padKey(byte[] keyBytes) {
|
||||
int keyLength = keyBytes.length;
|
||||
if (keyLength == 16 || keyLength == 24 || keyLength == 32) {
|
||||
return keyBytes;
|
||||
}
|
||||
|
||||
// 如果密钥长度不足,用0填充;如果超过,截取前32位
|
||||
byte[] paddedKey = new byte[32];
|
||||
System.arraycopy(keyBytes, 0, paddedKey, 0, Math.min(keyLength, 32));
|
||||
return paddedKey;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
package xiaozhi.common.utils;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AESUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testEncryptAndDecrypt() {
|
||||
String key = "xiaozhi1234567890";
|
||||
String plainText = "Hello, 小智!";
|
||||
|
||||
System.out.println("原始文本: " + plainText);
|
||||
System.out.println("密钥: " + key);
|
||||
|
||||
// 加密
|
||||
String encrypted = AESUtils.encrypt(key, plainText);
|
||||
System.out.println("加密结果: " + encrypted);
|
||||
|
||||
// 解密
|
||||
String decrypted = AESUtils.decrypt(key, encrypted);
|
||||
System.out.println("解密结果: " + decrypted);
|
||||
|
||||
// 验证
|
||||
assertEquals(plainText, decrypted, "加解密结果应该一致");
|
||||
System.out.println("加解密一致性: " + plainText.equals(decrypted));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentKeyLengths() {
|
||||
String[] keys = {
|
||||
"1234567890123456", // 16位
|
||||
"123456789012345678901234", // 24位
|
||||
"12345678901234567890123456789012", // 32位
|
||||
"short", // 短密钥
|
||||
"verylongkeythatwillbetruncatedto32bytes" // 长密钥
|
||||
};
|
||||
|
||||
String plainText = "测试文本";
|
||||
|
||||
for (String key : keys) {
|
||||
String encrypted = AESUtils.encrypt(key, plainText);
|
||||
String decrypted = AESUtils.decrypt(key, encrypted);
|
||||
assertEquals(plainText, decrypted, "密钥长度: " + key.length());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecialCharacters() {
|
||||
String key = "xiaozhi1234567890";
|
||||
String[] testTexts = {
|
||||
"Hello World",
|
||||
"你好世界",
|
||||
"Hello, 小智!",
|
||||
"特殊字符: !@#$%^&*()",
|
||||
"数字123和中文混合",
|
||||
"Emoji: 😀🎉🚀",
|
||||
"空字符串测试",
|
||||
""
|
||||
};
|
||||
|
||||
for (String text : testTexts) {
|
||||
String encrypted = AESUtils.encrypt(key, text);
|
||||
String decrypted = AESUtils.decrypt(key, encrypted);
|
||||
assertEquals(text, decrypted, "测试文本: " + text);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCrossLanguageCompatibility() {
|
||||
// 这些是Python版本生成的加密结果,用于测试跨语言兼容性
|
||||
String key = "xiaozhi1234567890";
|
||||
String plainText = "Hello, 小智!";
|
||||
|
||||
// Python版本生成的加密结果(需要运行Python测试后获取)
|
||||
// String pythonEncrypted = "从Python测试中获取的加密结果";
|
||||
// String decrypted = AESUtils.decrypt(key, pythonEncrypted);
|
||||
// assertEquals(plainText, decrypted, "Java应该能解密Python加密的结果");
|
||||
|
||||
// 生成Java加密结果供Python测试
|
||||
String javaEncrypted = AESUtils.encrypt(key, plainText);
|
||||
System.out.println("Java加密结果供Python测试: " + javaEncrypted);
|
||||
}
|
||||
}
|
||||
|
|
@ -99,6 +99,45 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<!-- MCP区域 -->
|
||||
<div class="mcp-access-point">
|
||||
<div class="mcp-header">
|
||||
<h3 class="bold-title">MCP接入点</h3>
|
||||
</div>
|
||||
<div class="mcp-container">
|
||||
<!-- 左侧区域 -->
|
||||
<div class="mcp-left">
|
||||
<div class="url-header">
|
||||
<h4>接入点地址</h4>
|
||||
<div class="address-desc">
|
||||
<span>以下是智能体的MCP接入点地址。</span>
|
||||
<a href="#" class="doc-link">查看接入点使用文档</a>
|
||||
</div>
|
||||
</div>
|
||||
<el-input v-model="mcpUrl" readonly class="url-input">
|
||||
<template #suffix>
|
||||
<el-button @click="copyUrl" class="inner-copy-btn" icon="el-icon-document-copy">
|
||||
复制
|
||||
</el-button>
|
||||
</template>
|
||||
</el-input>
|
||||
</div>
|
||||
|
||||
<!-- 右侧区域 -->
|
||||
<div class="mcp-right">
|
||||
<h4>接入点状态</h4>
|
||||
<div class="status-container">
|
||||
<span class="status-indicator" :class="mcpStatus"></span>
|
||||
<span class="status-text">{{ mcpStatus === 'connected' ? '已连接' : '未连接' }}</span>
|
||||
<button class="refresh-btn" @click="refreshStatus">
|
||||
<span class="refresh-icon">↻</span>
|
||||
<span>刷新</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="drawer-footer">
|
||||
<el-button @click="closeDialog">取消</el-button>
|
||||
<el-button type="primary" @click="saveSelection">保存配置</el-button>
|
||||
|
|
@ -134,6 +173,9 @@ export default {
|
|||
// 添加一个标志位来跟踪是否已经保存
|
||||
hasSaved: false,
|
||||
loading: false,
|
||||
|
||||
mcpUrl: "https://api.example.com/mcp/v1/endpoint",
|
||||
mcpStatus: "disconnected",
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
|
|
@ -184,6 +226,37 @@ export default {
|
|||
}
|
||||
},
|
||||
methods: {
|
||||
copyUrl() {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.value = this.mcpUrl;
|
||||
textarea.style.position = 'fixed'; // 防止页面滚动
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
|
||||
try {
|
||||
const successful = document.execCommand('copy');
|
||||
if (successful) {
|
||||
this.$message.success('已复制到剪贴板');
|
||||
} else {
|
||||
this.$message.error('复制失败,请手动复制');
|
||||
}
|
||||
} catch (err) {
|
||||
this.$message.error('复制失败,请手动复制');
|
||||
console.error('复制失败:', err);
|
||||
} finally {
|
||||
document.body.removeChild(textarea);
|
||||
}
|
||||
},
|
||||
|
||||
refreshStatus() {
|
||||
// 模拟状态刷新
|
||||
this.mcpStatus = "loading";
|
||||
setTimeout(() => {
|
||||
// 随机切换状态用于演示
|
||||
this.mcpStatus = Math.random() > 0.5 ? "connected" : "disconnected";
|
||||
}, 800);
|
||||
},
|
||||
|
||||
flushArray(key) {
|
||||
const text = this.textCache[key] || '';
|
||||
const arr = text
|
||||
|
|
@ -298,7 +371,7 @@ export default {
|
|||
display: grid;
|
||||
grid-template-columns: max-content max-content 1fr;
|
||||
gap: 12px;
|
||||
height: calc(70vh - 60px);
|
||||
height: calc(58vh);
|
||||
}
|
||||
|
||||
.custom-header {
|
||||
|
|
@ -514,4 +587,161 @@ export default {
|
|||
::v-deep .el-checkbox__label {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.mcp-access-point {
|
||||
border-top: 1px solid #EBEEF5;
|
||||
padding: 20px 24px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.mcp-header {
|
||||
.bold-title {
|
||||
font-size: 18px;
|
||||
font-weight: bold;
|
||||
margin: 5px 0 30px 0;
|
||||
}
|
||||
}
|
||||
|
||||
.mcp-container {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 30px;
|
||||
}
|
||||
|
||||
.mcp-left, .mcp-right {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.url-header {
|
||||
margin-bottom: 8px;
|
||||
color: black;
|
||||
h4 {
|
||||
margin: 0 0 15px 0;
|
||||
font-size: 16px;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
.address-desc {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
font-size: 14px;
|
||||
margin-bottom: 12px;
|
||||
|
||||
.doc-link {
|
||||
color: #1677ff;
|
||||
text-decoration: none;
|
||||
margin-left: 4px;
|
||||
|
||||
&:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.url-input {
|
||||
border-radius: 4px 0 0 4px;
|
||||
font-size: 14px;
|
||||
height: 36px;
|
||||
box-sizing: border-box;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
::v-deep .el-input__inner{
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.url-input {
|
||||
|
||||
::v-deep .el-input__suffix {
|
||||
right: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding-right: 15px;
|
||||
|
||||
.inner-copy-btn {
|
||||
pointer-events: auto;
|
||||
border: none;
|
||||
background: white;
|
||||
color: black;
|
||||
padding: 6px;
|
||||
margin-left: 4px;
|
||||
|
||||
&:hover {
|
||||
background: #1677ff;
|
||||
color: white;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.mcp-right {
|
||||
h4 {
|
||||
margin: 0 0 10px 0;
|
||||
font-size: 16px;
|
||||
font-weight: normal;
|
||||
color: black;
|
||||
}
|
||||
}
|
||||
|
||||
.status-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
.status-indicator {
|
||||
display: inline-block;
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
margin-right: 8px;
|
||||
|
||||
&.disconnected {
|
||||
background-color: #909399; /* 灰色 - 未连接 */
|
||||
}
|
||||
|
||||
&.connected {
|
||||
background-color: #67C23A; /* 绿色 - 已连接 */
|
||||
}
|
||||
|
||||
&.loading {
|
||||
background-color: #E6A23C; /* 橙色 - 加载中 */
|
||||
animation: pulse 1.5s infinite;
|
||||
}
|
||||
}
|
||||
|
||||
.status-text {
|
||||
font-size: 14px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.refresh-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 2px 10px;
|
||||
background: white;
|
||||
color: black;
|
||||
border: 1px solid #DCDFE6;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: all 0.3s;
|
||||
|
||||
&:hover {
|
||||
background: #1677ff;
|
||||
color: white;
|
||||
border-color: #1677ff;
|
||||
}
|
||||
|
||||
.refresh-icon {
|
||||
margin-right: 6px;
|
||||
font-size: 14px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0% { opacity: 1; }
|
||||
50% { opacity: 0.4; }
|
||||
100% { opacity: 1; }
|
||||
}
|
||||
</style>
|
||||
|
|
@ -61,6 +61,16 @@ class FunctionHandler:
|
|||
return self.function_registry.get_function(name)
|
||||
|
||||
def handle_llm_function_call(self, conn, function_call_data):
|
||||
# 多函数调用处理
|
||||
if "function_calls" in function_call_data:
|
||||
responses = []
|
||||
for call in function_call_data["function_calls"]:
|
||||
func = self.get_function(call["name"])
|
||||
if func:
|
||||
# 执行函数并收集响应
|
||||
response = func(conn, **call.get("arguments", {}))
|
||||
responses.append(response)
|
||||
return self._combine_responses(responses) # 合并响应
|
||||
try:
|
||||
function_name = function_call_data["name"]
|
||||
funcItem = self.get_function(function_name)
|
||||
|
|
|
|||
|
|
@ -82,7 +82,11 @@ def create_iot_function(device_name, method_name, method_info):
|
|||
response = response.replace("{value}", str(param_value))
|
||||
break
|
||||
|
||||
return ActionResponse(Action.RESPONSE, result, response)
|
||||
return ActionResponse(
|
||||
Action.REQLLM,
|
||||
result=f"{device_name}操作执行成功,请继续处理剩余指令",
|
||||
response=response_success # 保留成功提示
|
||||
)
|
||||
except Exception as e:
|
||||
conn.logger.bind(tag=TAG).error(
|
||||
f"执行{device_name}的{method_name}操作失败: {e}"
|
||||
|
|
|
|||
|
|
@ -95,6 +95,10 @@ class IntentProvider(IntentProviderBase):
|
|||
"1. 只返回JSON格式,不要包含任何其他文字\n"
|
||||
'2. 如果没有找到匹配的函数,返回{"function_call": {"name": "continue_chat"}}\n'
|
||||
"3. 确保返回的JSON格式正确,包含所有必要的字段\n"
|
||||
"特殊说明:\n"
|
||||
"- 当用户单次输入包含多个指令时(如'打开灯并且调高音量')\n"
|
||||
"- 请返回多个function_call组成的JSON数组\n"
|
||||
"- 示例:{'function_calls': [{name:'light_on'}, {name:'volume_up'}]}"
|
||||
)
|
||||
return prompt
|
||||
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ services:
|
|||
volumes:
|
||||
# 配置文件目录
|
||||
- ./uploadfile:/uploadfile
|
||||
|
||||
# 数据库模块
|
||||
xiaozhi-esp32-server-db:
|
||||
image: mysql:latest
|
||||
container_name: xiaozhi-esp32-server-db
|
||||
|
|
@ -73,6 +73,7 @@ services:
|
|||
- MYSQL_ROOT_PASSWORD=123456
|
||||
- MYSQL_DATABASE=xiaozhi_esp32_server
|
||||
- MYSQL_INITDB_ARGS="--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"
|
||||
# redis模块
|
||||
xiaozhi-esp32-server-redis:
|
||||
image: redis
|
||||
expose:
|
||||
|
|
|
|||
Loading…
Reference in a new issue