2026-01-15 22:49:48 +08:00

105 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// main.js
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';
const init = async () => {
const ui = createPanel();
let spaceTimer = null;
let isRecording = false;
// 统一定义 UI 更新引用,方便 handleCommand 调用
const uiRefs = {
updateStatus: (text) => {
const statusText = document.getElementById("statusText");
if (statusText) {
statusText.innerText = text;
statusText.style.color = "#409eff"; // 使用蓝色区分对话与就绪状态
}
}
};
async function startProcess(text) {
if (!text) return;
ui.setLoading(true);
try {
// 无论是否本地匹配,统一走 translateToCommand 以获取对话回复
// 如果你希望本地极速响应,可以在此保留逻辑,但建议统一走 AI 获取 <communication>
const aiResult = await translateToCommand(text);
if (aiResult) {
handleCommand(aiResult, uiRefs);
}
} catch (err) {
console.error("处理流程错误:", err);
uiRefs.updateStatus("处理指令时出错");
} finally {
ui.setLoading(false);
}
}
const voiceCtrl = initVoice(document.getElementById("automation-ai-panel"), (text) => {
ui.input.value = text;
startProcess(text);
});
// 按钮触发录音
ui.btn.onclick = () => {
if (!isRecording) {
voiceCtrl.start();
ui.setRecording(true);
isRecording = true;
setTimeout(() => {
if (isRecording) {
voiceCtrl.stop();
ui.setRecording(false);
isRecording = false;
}
}, 4000); // 按钮模式延长到 4s确保说话完整
}
};
// 空格长按触发录音
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();
}
}
});
// 输入框回车触发
ui.input.onkeydown = (e) => {
if (e.key === "Enter") {
const val = ui.input.value;
ui.input.value = "";
startProcess(val);
}
};
};
if (document.readyState === "complete") init();
else window.addEventListener("load", init);