/**
* Inspiration Master - Frontend Logic
*/
// Global state for inspiration feature
let inspirationState = {
isLoggedIn: false,
llmConfig: {
baseUrl: "https://api.moonshot.cn/v1",
model: "kimi-k2-turbo-preview",
apiKey: ""
},
options: null,
selectedDataset: null
};
document.addEventListener('DOMContentLoaded', function() {
const inspirationBtn = document.getElementById('inspirationBtn');
if (inspirationBtn) {
inspirationBtn.addEventListener('click', openInspirationModal);
}
// Initialize event listeners for the modal
document.getElementById('inspire-region').addEventListener('change', updateUniversesAndDelays);
document.getElementById('inspire-search-dataset').addEventListener('click', searchDatasets);
document.getElementById('inspire-generate').addEventListener('click', generateAlphaTemplates);
document.getElementById('inspire-download').addEventListener('click', downloadInspirationResult);
document.getElementById('inspire-new-task').addEventListener('click', resetInspirationTask);
document.getElementById('inspire-close').addEventListener('click', closeInspirationModal);
const testBtn = document.getElementById('inspire-test-llm');
if (testBtn) {
testBtn.addEventListener('click', testLLMConnection);
}
// Initially disable generate button until tested
const genBtn = document.getElementById('inspire-generate');
if (genBtn) {
genBtn.disabled = true;
genBtn.title = "Please test LLM connection first";
}
// Initially disable new task button
const newTaskBtn = document.getElementById('inspire-new-task');
if (newTaskBtn) {
newTaskBtn.disabled = true;
newTaskBtn.style.opacity = '0.5';
newTaskBtn.style.cursor = 'not-allowed';
}
// Check login status periodically or on load to update button state
checkLoginAndUpdateButton();
});
function getHeaders() {
const headers = {'Content-Type': 'application/json'};
const sessionId = localStorage.getItem('brain_session_id');
if (sessionId) {
headers['Session-ID'] = sessionId;
}
return headers;
}
function checkLoginAndUpdateButton() {
fetch('/api/check_login', { headers: getHeaders() })
.then(response => response.json())
.then(data => {
const btn = document.getElementById('inspirationBtn');
if (data.logged_in) {
inspirationState.isLoggedIn = true;
if (btn) {
btn.style.opacity = '1';
btn.style.cursor = 'pointer';
// Add a visual indicator of logged-in state if desired, e.g., change icon color
}
} else {
inspirationState.isLoggedIn = false;
if (btn) {
// Keep it clickable but maybe visually distinct
btn.style.opacity = '1';
btn.style.cursor = 'pointer';
}
}
// Always ensure it's enabled
if (btn) btn.disabled = false;
})
.catch(err => console.error("Error checking login status:", err));
}
// Expose this function globally so other scripts (like brain.js) can call it after login
window.updateInspirationButtonState = checkLoginAndUpdateButton;
function openInspirationModal() {
if (!inspirationState.isLoggedIn) {
// Double check
fetch('/api/check_login', { headers: getHeaders() })
.then(response => response.json())
.then(data => {
if (data.logged_in) {
inspirationState.isLoggedIn = true;
document.getElementById('inspirationModal').style.display = 'block';
loadInspirationOptions();
} else {
// Trigger Brain Login Modal
if (typeof openBrainLoginModal === 'function') {
openBrainLoginModal();
} else {
alert("请先登录 BRAIN。");
}
}
});
return;
}
document.getElementById('inspirationModal').style.display = 'block';
loadInspirationOptions();
}
function closeInspirationModal() {
document.getElementById('inspirationModal').style.display = 'none';
}
function loadInspirationOptions() {
if (inspirationState.options) return; // Already loaded
fetch('/api/inspiration/options', { headers: getHeaders() })
.then(res => res.json())
.then(data => {
inspirationState.options = data;
populateRegionDropdown(data);
})
.catch(err => console.error("Failed to load options:", err));
}
function populateRegionDropdown(data) {
const regionSelect = document.getElementById('inspire-region');
regionSelect.innerHTML = '';
// Assuming data structure matches what we get from ace_lib
// Structure: { "EQUITY": { "USA": { ... }, "CHN": { ... } } }
// We'll focus on EQUITY for now or iterate all
let regions = new Set();
if (data.EQUITY) {
Object.keys(data.EQUITY).forEach(r => regions.add(r));
}
regions.forEach(r => {
const option = document.createElement('option');
option.value = r;
option.textContent = r;
regionSelect.appendChild(option);
});
}
function updateUniversesAndDelays() {
const region = document.getElementById('inspire-region').value;
const universeSelect = document.getElementById('inspire-universe');
const delaySelect = document.getElementById('inspire-delay');
universeSelect.innerHTML = '';
delaySelect.innerHTML = '';
if (!region || !inspirationState.options || !inspirationState.options.EQUITY || !inspirationState.options.EQUITY[region]) return;
const data = inspirationState.options.EQUITY[region];
data.universes.forEach(u => {
const option = document.createElement('option');
option.value = u;
option.textContent = u;
universeSelect.appendChild(option);
});
data.delays.forEach(d => {
const option = document.createElement('option');
option.value = d;
option.textContent = d;
delaySelect.appendChild(option);
});
}
function searchDatasets() {
const region = document.getElementById('inspire-region').value;
const delay = document.getElementById('inspire-delay').value;
const universe = document.getElementById('inspire-universe').value;
const search = document.getElementById('inspire-dataset-search').value;
if (!region || !delay || !universe) {
alert("请先选择区域、延迟和股票池。");
return;
}
const resultsDiv = document.getElementById('inspire-dataset-results');
resultsDiv.innerHTML = '正在加载数据集...';
fetch('/api/inspiration/datasets', {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ region, delay, universe, search })
})
.then(res => res.json())
.then(data => {
displayDatasetResults(data);
})
.catch(err => {
resultsDiv.innerHTML = '加载数据集出错: ' + err;
});
}
function displayDatasetResults(datasets) {
const resultsDiv = document.getElementById('inspire-dataset-results');
resultsDiv.innerHTML = '';
if (datasets.length === 0) {
resultsDiv.innerHTML = '未找到数据集。';
return;
}
const table = document.createElement('table');
table.className = 'dataset-table';
table.style.width = '100%'; // Ensure full width
table.style.borderCollapse = 'collapse';
table.innerHTML = `
ID
Name
Category
$1')
.replace(/\n/g, '