49 lines
1.9 KiB
JavaScript
49 lines
1.9 KiB
JavaScript
// 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. 识别用户意图并按以下 XML 格式输出:
|
|
- 基础格式:<cmd>标准指令</cmd>
|
|
- 带参数格式(如设备号、序列号):<cmd>标准指令</cmd><arg>参数内容</arg>
|
|
2. 示例:
|
|
- “帮我添加一台编号为20260114的设备” -> 输出 “<cmd>添加设备</cmd><arg>20260114</arg>”
|
|
- “打开监控中心” -> 输出 “<cmd>监控中心</cmd>”
|
|
3. “主界面”、“主页”映射为“首页”。
|
|
4. 只输出 XML 结果,不要输出任何解释说明或标点。
|
|
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 {
|
|
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 }
|
|
}));
|
|
});
|
|
} |