49 lines
2.0 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.

// scripts/ai.js
import { COMMANDS } from './commands.js';
export async function translateToCommand(userInput) {
const availableKeys = COMMANDS.map(c => c.key).join(', ');
// 优化提示词:增加参数提取规则和别名映射
const systemPrompt = `你是一个工业自动化系统助手。
标准指令列表:[${availableKeys}]
任务规则:
1. 如果用户指令包含设备编号、序列号或验证码如“20260114”请按格式输出“标准指令 参数”。
例如“帮我添加一台编号为20260114的设备” -> 输出 “添加设备 20260114”。
2. 如果只是单纯跳转,只输出“标准指令”。
3. “主界面”、“主页”映射为“首页”。
4. 只输出结果,不要输出任何解释说明或标点。
5. 无法匹配请输出 UNKNOWN。`;
return new Promise((resolve) => {
const handler = (event) => {
const response = event.detail;
window.removeEventListener("AI_RESULT", handler);
if (response && response.success && response.data) {
try {
if (response.data.choices && response.data.choices.length > 0) {
const content = response.data.choices[0].message.content.trim();
console.log("📥 AI 映射结果:", content);
resolve(content === "UNKNOWN" ? null : content);
} else if (response.data.error || response.data.errors) {
console.error("AI服务报错:", response.data.error || response.data.errors);
resolve(null);
}
} catch (e) {
console.error("解析响应失败:", e);
resolve(null);
}
} else {
resolve(null);
}
};
window.addEventListener("AI_RESULT", handler);
window.dispatchEvent(new CustomEvent("DO_AI_REQUEST", {
detail: { userInput, systemPrompt }
}));
});
}