2026-01-11 18:27:51 +08:00
|
|
|
|
// main.js
|
2026-01-11 17:50:27 +08:00
|
|
|
|
import { createPanel } from './scripts/panel.js';
|
|
|
|
|
|
import { handleCommand, COMMANDS } from './scripts/commands.js';
|
|
|
|
|
|
import { initVoice } from './scripts/voice.js';
|
|
|
|
|
|
import { translateToCommand } from './scripts/ai.js';
|
2026-01-02 21:06:54 +08:00
|
|
|
|
|
2026-01-11 12:22:49 +08:00
|
|
|
|
const init = async () => {
|
|
|
|
|
|
const ui = createPanel();
|
2026-01-11 18:27:51 +08:00
|
|
|
|
let spaceTimer = null;
|
|
|
|
|
|
let isRecording = false;
|
2026-01-02 21:06:54 +08:00
|
|
|
|
|
2026-01-15 22:49:48 +08:00
|
|
|
|
// 统一定义 UI 更新引用,方便 handleCommand 调用
|
|
|
|
|
|
const uiRefs = {
|
|
|
|
|
|
updateStatus: (text) => {
|
|
|
|
|
|
const statusText = document.getElementById("statusText");
|
|
|
|
|
|
if (statusText) {
|
|
|
|
|
|
statusText.innerText = text;
|
|
|
|
|
|
statusText.style.color = "#409eff"; // 使用蓝色区分对话与就绪状态
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-11 12:22:49 +08:00
|
|
|
|
async function startProcess(text) {
|
2026-01-11 18:27:51 +08:00
|
|
|
|
if (!text) return;
|
2026-01-11 12:22:49 +08:00
|
|
|
|
ui.setLoading(true);
|
|
|
|
|
|
try {
|
2026-01-15 22:49:48 +08:00
|
|
|
|
// 无论是否本地匹配,统一走 translateToCommand 以获取对话回复
|
|
|
|
|
|
// 如果你希望本地极速响应,可以在此保留逻辑,但建议统一走 AI 获取 <communication>
|
|
|
|
|
|
const aiResult = await translateToCommand(text);
|
|
|
|
|
|
if (aiResult) {
|
|
|
|
|
|
handleCommand(aiResult, uiRefs);
|
2026-01-11 12:22:49 +08:00
|
|
|
|
}
|
2026-01-15 22:49:48 +08:00
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error("处理流程错误:", err);
|
|
|
|
|
|
uiRefs.updateStatus("处理指令时出错");
|
2026-01-11 12:22:49 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
ui.setLoading(false);
|
2026-01-02 21:06:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 18:27:51 +08:00
|
|
|
|
const voiceCtrl = initVoice(document.getElementById("automation-ai-panel"), (text) => {
|
|
|
|
|
|
ui.input.value = text;
|
|
|
|
|
|
startProcess(text);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-15 22:49:48 +08:00
|
|
|
|
// 按钮触发录音
|
2026-01-11 18:27:51 +08:00
|
|
|
|
ui.btn.onclick = () => {
|
|
|
|
|
|
if (!isRecording) {
|
|
|
|
|
|
voiceCtrl.start();
|
|
|
|
|
|
ui.setRecording(true);
|
|
|
|
|
|
isRecording = true;
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
if (isRecording) {
|
|
|
|
|
|
voiceCtrl.stop();
|
|
|
|
|
|
ui.setRecording(false);
|
|
|
|
|
|
isRecording = false;
|
|
|
|
|
|
}
|
2026-01-15 22:49:48 +08:00
|
|
|
|
}, 4000); // 按钮模式延长到 4s,确保说话完整
|
2026-01-11 18:27:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-15 22:49:48 +08:00
|
|
|
|
// 空格长按触发录音
|
2026-01-11 18:27:51 +08:00
|
|
|
|
window.addEventListener("keydown", (e) => {
|
|
|
|
|
|
if (e.code === "Space" && e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {
|
|
|
|
|
|
if (spaceTimer || isRecording) return;
|
|
|
|
|
|
|
|
|
|
|
|
spaceTimer = setTimeout(() => {
|
|
|
|
|
|
if (voiceCtrl.supportSpeech) {
|
|
|
|
|
|
voiceCtrl.start();
|
|
|
|
|
|
ui.setRecording(true);
|
|
|
|
|
|
isRecording = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}, 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
window.addEventListener("keyup", (e) => {
|
|
|
|
|
|
if (e.code === "Space") {
|
|
|
|
|
|
if (spaceTimer) {
|
|
|
|
|
|
clearTimeout(spaceTimer);
|
|
|
|
|
|
spaceTimer = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isRecording) {
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
voiceCtrl.stop();
|
|
|
|
|
|
ui.setRecording(false);
|
|
|
|
|
|
isRecording = false;
|
|
|
|
|
|
}, 200);
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-01-15 22:49:48 +08:00
|
|
|
|
// 输入框回车触发
|
2026-01-11 18:27:51 +08:00
|
|
|
|
ui.input.onkeydown = (e) => {
|
2026-01-11 12:22:49 +08:00
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
|
const val = ui.input.value;
|
|
|
|
|
|
ui.input.value = "";
|
|
|
|
|
|
startProcess(val);
|
|
|
|
|
|
}
|
2026-01-11 18:27:51 +08:00
|
|
|
|
};
|
2026-01-11 12:22:49 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-01-11 18:27:51 +08:00
|
|
|
|
if (document.readyState === "complete") init();
|
|
|
|
|
|
else window.addEventListener("load", init);
|