增加AI文本回复

This commit is contained in:
Cx330 committed 2026-01-15 22:49:48 +08:00
1 parent eaf75ac17f
commit c43b5e5ac1
3 files changed
+64 -63

No files matched your search

+7 -9
View File
@@ -8,15 +8,13 @@ export async function translateToCommand(userInput) {
标准指令列表:[${availableKeys}]
任务规则:
1. 识别用户意图并按以下 XML 格式输出:
- 基础格式:<cmd>标准指令</cmd>
- 带参数格式(如设备号、序列号):<cmd>标准指令</cmd><arg>参数内容</arg>
2. 示例:
- “帮我添加一台编号为20260114的设备” -> 输出 “<cmd>添加设备</cmd><arg>20260114</arg>”
- “打开监控中心” -> 输出 “<cmd>监控中心</cmd>”
3. “主界面”、“主页”映射为“首页”。
4. 只输出 XML 结果,不要输出任何解释说明或标点。
5. 无法匹配请输出 UNKNOWN。`;
1. **必须包含对话**:无论是否执行指令,你都必须在 <communication>XXXX</communication> 标签中放入你对用户说的话。
2. **意图识别**:如果用户意图匹配指令列表,输出 <cmd>标准指令</cmd>。如有参数(如设备号),输出 <arg>参数</arg>。
3. **示例**:
- 用户:“打开监控中心” -> “<communication>好的,正在为您进入监控中心。</communication><cmd>监控中心</cmd>”
- 用户:“你是谁” -> “<communication>我是您的自动化 AI 助手,可以帮您操作平台。</communication>”
- 用户:“添加设备 888” -> “<communication>没问题,正在为您添加编号为 888 的设备。</communication><cmd>添加设备</cmd><arg>888</arg>”
4. 只输出 XML 结果,不要输出任何解释说明,保持简洁。`;
return new Promise((resolve) => {
const handler = (event) => {
+34 -43
View File
@@ -1,3 +1,4 @@
// scripts/commands.js
export const COMMANDS = [
{ key: "首页", menu: "首页", route: "/首页" },
{ key: "添加设备", menu: "添加设备", route: "/添加设备/添加设备" },
@@ -53,28 +54,16 @@ function waitForElement(selector, callback, timeout = 5000) {
}, 200);
}
/**
* 自动化填写和点击逻辑
*/
function autoFillAndSubmit(value) {
// 1. 等待输入框出现 (针对 Element Plus 使用 .el-input__inner)
// 提示:如果有多个框,可改用 'input[placeholder*="上云码"]' 增加精度
waitForElement('.el-input__inner', (input) => {
console.log("🔍 已定位输入框,开始填充:", value);
// 填写内容
input.value = value;
// 关键:手动触发事件以同步 Vue 的双向绑定 (v-model)
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
// 2. 自动点击“确认”按钮 (通常类名为 el-button--primary)
const submitBtn = document.querySelector('.el-button--primary');
if (submitBtn) {
setTimeout(() => {
submitBtn.click();
console.log("✅ 自动化流程已触发提交");
}, 600);
}
});
@@ -88,48 +77,50 @@ export function expandParentMenu(span) {
}
}
export function handleCommand(aiResult) {
/**
* 处理 AI 返回的混合指令 (对话 + 操作)
*/
export function handleCommand(aiResult, uiRefs) {
if (!aiResult || aiResult === "UNKNOWN") return;
console.log("🚀 收到结构化指令:", aiResult);
// 使用正则解析 XML 标签内容
// 1. 解析对话内容并反馈
const commMatch = aiResult.match(/<communication>([\s\S]*?)<\/communication>/);
if (commMatch && commMatch[1]) {
const speechText = commMatch[1].trim();
// 更新 UI 状态
if (uiRefs && uiRefs.updateStatus) {
uiRefs.updateStatus(speechText);
}
// 语音播报
const utterance = new SpeechSynthesisUtterance(speechText);
utterance.lang = "zh-CN";
window.speechSynthesis.speak(utterance);
}
// 2. 解析指令逻辑
const cmdMatch = aiResult.match(/<cmd>([\s\S]*?)<\/cmd>/);
const argMatch = aiResult.match(/<arg>([\s\S]*?)<\/arg>/);
const key = cmdMatch ? cmdMatch[1].trim() : null;
const arg = argMatch ? argMatch[1].trim() : null;
if (!key) {
console.warn("未能解析出有效的 <cmd> 标签");
return;
}
if (key) {
const command = COMMANDS.find(c => c.key === key) ||
COMMANDS.find(c => key.includes(c.key));
// 在命令库中寻找匹配项
const command = COMMANDS.find(c => c.key === key) ||
COMMANDS.find(c => key.includes(c.key));
if (command) {
const allSpans = Array.from(document.querySelectorAll("span"));
let span = allSpans.find(el => el.innerText.trim() === command.menu);
if (command) {
// 第一步:查找菜单并跳转
const allSpans = Array.from(document.querySelectorAll("span"));
let span = allSpans.find(el => el.innerText.trim() === command.menu);
if (span) {
expandParentMenu(span);
span.click();
window.location.hash = command.route;
// 第二步:如果有参数(如设备号),启动后续填充流程
if (arg) {
console.log(`⏳ 检测到参数,准备自动填充: ${arg}`);
autoFillAndSubmit(arg);
if (span) {
expandParentMenu(span);
span.click();
window.location.hash = command.route;
if (arg) autoFillAndSubmit(arg);
} else {
window.location.hash = command.route;
if (arg) autoFillAndSubmit(arg);
}
} else {
// 容错:如果没找到菜单元素,依然尝试直接跳转路由
console.log("未发现菜单 DOM,尝试直接通过路由跳转");
window.location.hash = command.route;
if (arg) autoFillAndSubmit(arg);
}
} else {
console.warn("未匹配到标准库指令:", key);
}
}