Compare commits

..
2 Commits
+64 -24
View File
@@ -30,20 +30,23 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
const uuid = () => ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16));
const KEYS = {
const SESSION_STORAGE_KEYS = {
SESSION_ID: 'aRah9OhHeijee6sho3baequu9phoovah',
SESSION_TOKEN: 'ThiegiecohViuZ1Iecio7gahphiechub',
AGENT_TOKEN: 'baf-agent-token',
CURRENT_TASK: 'Reebo1eitahh2aotumai5jae1neetoh3',
CURRENT_TASK_DATA: 'cheishailoh5keePoo6oe2Quie1gaxah',
CURRENT_STEP: 'eDusaidu2hooweiMoonahng3fua7aoso',
}
if (!sessionStorage.getItem(KEYS.SESSION_ID)) {
sessionStorage.setItem(KEYS.SESSION_ID, uuid());
if (!sessionStorage.getItem(SESSION_STORAGE_KEYS.SESSION_ID)) {
sessionStorage.setItem(SESSION_STORAGE_KEYS.SESSION_ID, uuid());
}
// ====== CONFIGURATION ======
const CONFIG = {
HEARTBEAT_INTERVAL: 10000,
HEARTBEAT_INTERVAL: 10000, // Send heartbeat every 10 seconds
DASHBOARD_UPDATE_INTERVAL: 500, // Update dashboard overlay every 0.5 seconds
TASK_INTERVAL: 1000, // Check for new task every 1 second
SERVERS: {
local: { label: '🏠 Local', url: 'http://localhost:3000' },
prod: { label: '🌐 Prod', url: 'https://baf.thuanle.me' },
@@ -52,10 +55,23 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
const AppSession = {
SESSION_ID: sessionStorage.getItem(KEYS.SESSION_ID),
SESSION_ID: sessionStorage.getItem(SESSION_STORAGE_KEYS.SESSION_ID),
getSessionToken: () => sessionStorage.getItem(KEYS.SESSION_TOKEN),
setSessionToken: token => sessionStorage.setItem(KEYS.SESSION_TOKEN, token),
getSessionToken: () => sessionStorage.getItem(SESSION_STORAGE_KEYS.SESSION_TOKEN),
setSessionToken: token => sessionStorage.setItem(SESSION_STORAGE_KEYS.SESSION_TOKEN, token),
// Task state - Trạng thái task hiện tại (lưu trong sessionStorage)
getCurrentTask: () => sessionStorage.getItem(SESSION_STORAGE_KEYS.CURRENT_TASK),
setCurrentTask: task => sessionStorage.setItem(SESSION_STORAGE_KEYS.CURRENT_TASK, task),
getCurrentTaskData: () => {
const data = sessionStorage.getItem(SESSION_STORAGE_KEYS.CURRENT_TASK_DATA);
return data ? JSON.parse(data) : null;
},
setCurrentTaskData: data => sessionStorage.setItem(SESSION_STORAGE_KEYS.CURRENT_TASK_DATA, JSON.stringify(data)),
getCurrentStep: () => sessionStorage.getItem(SESSION_STORAGE_KEYS.CURRENT_STEP),
setCurrentStep: step => sessionStorage.setItem(SESSION_STORAGE_KEYS.CURRENT_STEP, step),
}
// ====== APP ENUMS ======
@@ -82,10 +98,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
current_page: null, // Trang hiện tại (detected từ URL)
is_logged_in: false, // Trạng thái đăng nhập Binance
// UI state - Trạng thái UI
is_loading: false, // Đang loading
error_message: null, // Thông báo lỗi
// Session flags - Flags cho session hiện tại (reset khi reload)
menuCreated: false, // Menu đã được tạo
popupInitialized: false, // Popup đã được khởi tạo
@@ -112,7 +124,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
current_page: currentPage,
});
TL.log('APP-STATE', 'AppState initialized');
TL.debug('APP-STATE', 'AppState initialized');
}
/**
@@ -138,7 +150,6 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
getCurrentPage() { return this.data.current_page; }
getIsLoggedIn() { return this.data.is_logged_in; }
getIsLoading() { return this.data.is_loading; }
getMenuCreated() { return this.data.menuCreated; }
getPopupInitialized() { return this.data.popupInitialized; }
getAppInitialized() { return this.data.appInitialized; }
@@ -258,10 +269,10 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
* TL - Common utilities cho logging, notifications, và DOM helpers
*/
const TL = {
debug: (tag, msg, ...args) => AppSettings.getDebug() && GM_log(`[TL] [${tag}]\n${msg}`, ...args),
log: (tag, msg, ...args) => GM_log(`[TL] [${tag}]\n${msg}`, ...args),
warn: (tag, msg, ...args) => GM_log(`[TL] [WARN] [${tag}] ⚠️\n${msg}`, ...args),
error: (tag, msg, ...args) => GM_log(`[TL] [ERROR] [${tag}] ❌\n${msg}`, ...args),
debug: (tag, msg, ...args) => AppSettings.getDebug() && GM_log(`[TL] [DEBUG] [${new Date().toLocaleString()}] [${tag}] \n${msg}`, ...args),
log: (tag, msg, ...args) => GM_log(`[TL] [LOG] [${new Date().toLocaleString()}] [${tag}] \n${msg}`, ...args),
warn: (tag, msg, ...args) => GM_log(`[TL] [WARN] [${new Date().toLocaleString()}] [${tag}] ⚠️\n${msg}`, ...args),
error: (tag, msg, ...args) => GM_log(`[TL] [ERROR] [${new Date().toLocaleString()}] [${tag}] ❌\n${msg}`, ...args),
noti: (title, text, timeout = 2500) => {
if (typeof GM_notification === 'function') {
@@ -875,7 +886,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
// Setup interval services
AppServices.initInterval();
TL.log('APP-SERVICES', 'AppServices initialized');
TL.debug('APP-SERVICES', 'AppServices initialized');
},
initObservers: () => {
@@ -900,7 +911,7 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
registerSession: async () => {
const msg = await BAF.register({ session_id: AppSession.SESSION_ID });
AppSession.setSessionToken(msg.data.token);
TL.log('APP-SERVICES', `Session token: ${AppSession.getSessionToken()}`);
TL.debug('APP-SERVICES', `Session token: ${AppSession.getSessionToken()}`);
},
// Setup interval services
@@ -925,9 +936,9 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
logged_in: AppState.getIsLoggedIn(),
current_page: AppState.getCurrentPage(),
bot_status: AppSettings.getBotStatus(),
current_task: null, // TODO: Add to AppState if needed
current_task_data: null, // TODO: Add to AppState if needed
current_step: null, // TODO: Add to AppState if needed
current_task: AppSession.getCurrentTask(),
current_task_data: AppSession.getCurrentTaskData(),
current_step: AppSession.getCurrentStep(),
};
// TL.debug(`HEARTBEAT`, `${JSON.stringify(status, null, 2)}`);
@@ -945,6 +956,34 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
TL.error('HEARTBEAT', e.message);
return null;
}
},
getTask: async () => {
const data = {
logged_in: AppState.getIsLoggedIn(),
current_page: AppState.getCurrentPage(),
bot_status: AppSettings.getBotStatus(),
current_task: AppSession.getCurrentTask(),
current_task_data: AppSession.getCurrentTaskData(),
current_step: AppSession.getCurrentStep(),
};
const task = await BAF.getTask(data);
return task;
},
handleTask: (task) => {
TL.debug('APP-SERVICES', `Task: ${JSON.stringify(task, null, 2)}`);
},
start: async () => {
while (true) {
const task = await AppServices.getTask();
if (task.ok) {
AppServices.handleTask(task.data);
}
await TL.sleep(CONFIG.TASK_INTERVAL);
}
}
};
@@ -956,4 +995,5 @@ GM_log('[TL] 🏁 Welcome to Binance Alpha Farm Agent.');
}
TL.log('MAIN', 'App started');
await AppServices.start();
})();