// 从配置文件导入设置 // 配置在 env.js 文件中定义 // DOM 元素 const taskInput = document.getElementById('task-input'); const levelSelect = document.getElementById('level-select'); const generateBtn = document.getElementById('generateBtn'); const loadingDiv = document.getElementById('loading'); const commandsContainer = document.getElementById('commands'); // 调用后端API async function callBackendAPI(taskDescription, difficultyLevel) { try { const token = localStorage.getItem('token'); const headers = { 'Content-Type': 'application/json' }; if (token) { headers['Authorization'] = `Bearer ${token}`; } const response = await fetch(`${window.API_CONFIG.baseUrl}${window.API_CONFIG.endpoints.linuxCommand}`, { method: 'POST', headers: headers, body: JSON.stringify({ task_description: taskDescription, difficulty_level: difficultyLevel }) }); if (!response.ok) { if (response.status === 402) { throw new Error('您的萌芽币余额不足,无法使用此功能'); } const errorData = await response.json(); throw new Error(errorData.error || `API请求失败: ${response.status} ${response.statusText}`); } const data = await response.json(); if (data.success) { return data.command_result; } else { throw new Error(data.error || 'API响应格式异常'); } } catch (error) { console.error('API调用错误:', error); throw error; } } // 解析AI响应 function parseAIResponse(response) { try { // 尝试直接解析JSON const parsed = JSON.parse(response); return parsed; } catch (error) { // 如果直接解析失败,尝试提取JSON部分 const jsonMatch = response.match(/\{[\s\S]*\}/); if (jsonMatch) { try { const parsed = JSON.parse(jsonMatch[0]); return parsed; } catch (e) { console.error('JSON解析失败:', e); } } // 如果JSON解析失败,返回空对象 console.error('无法解析AI响应:', response); return {}; } } // 获取安全等级颜色 function getSafetyLevelColor(safetyLevel) { const colors = { 'safe': '#34C759', 'caution': '#FF9500', 'dangerous': '#FF3B30' }; return colors[safetyLevel] || '#86868B'; } // 获取安全等级文本 function getSafetyLevelText(safetyLevel) { const texts = { 'safe': '安全', 'caution': '谨慎', 'dangerous': '危险' }; return texts[safetyLevel] || '未知'; } // 显示命令建议 function displayCommands(commandData) { commandsContainer.innerHTML = ''; if (!commandData || !commandData.commands || commandData.commands.length === 0) { commandsContainer.innerHTML = '
${command.example_output}${alt}`).join(' ')}