-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
309 lines (264 loc) · 12.7 KB
/
Copy pathserver.js
File metadata and controls
309 lines (264 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// WheelHouse Server with CBOE Proxy and Settings API
// Serves static files AND proxies CBOE requests to avoid CORS
// Load environment variables FIRST
require('dotenv').config();
const express = require('express');
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const { execSync, spawn } = require('child_process');
const { Server: SocketIOServer } = require('socket.io');
// Initialize secure storage if running in Electron mode
const secureStore = require('./src/secureStore');
if (process.env.WHEELHOUSE_ENCRYPTION_KEY) {
secureStore.initialize(process.env.WHEELHOUSE_ENCRYPTION_KEY);
// Migrate any secrets from .env to secure storage (one-time)
secureStore.migrateFromEnv();
}
// Import route modules
const settingsRoutes = require('./src/routes/settingsRoutes');
const schwabRoutes = require('./src/routes/schwabRoutes');
const { schwabApiCall } = schwabRoutes; // For internal option chain calls
const wisdomRoutes = require('./src/routes/wisdomRoutes');
const cboeRoutes = require('./src/routes/cboeRoutes');
const updateRoutes = require('./src/routes/updateRoutes');
const aiRoutes = require('./src/routes/aiRoutes');
const scannerRoutes = require('./src/routes/scannerRoutes');
const streamingRoutes = require('./src/routes/streamingRoutes');
const summaryRoutes = require('./src/routes/summaryRoutes');
const coachingRoutes = require('./src/routes/coachingRoutes');
const autonomousRoutes = require('./src/routes/autonomousRoutes');
// ============================================================================
// UTILITY MODULES (extracted for modularity)
// ============================================================================
const { formatExpiryForCBOE, parseExpiryDate, calculateDTE } = require('./src/utils/dateHelpers');
const { getLocalVersion, getChangelog, detectGPU, MODEL_VRAM_REQUIREMENTS, MIME_TYPES, compareVersions } = require('./src/utils/serverHelpers');
// ============================================================================
// SERVICE MODULES (extracted for modularity)
// ============================================================================
const CacheService = require('./src/services/CacheService');
const DiscoveryService = require('./src/services/DiscoveryService');
const AIService = require('./src/services/AIService');
const WisdomService = require('./src/services/WisdomService');
const promptBuilders = require('./src/services/promptBuilders');
const DataService = require('./src/services/DataService');
const TechnicalService = require('./src/services/TechnicalService');
const CoachingService = require('./src/services/CoachingService');
const AutonomousTraderService = require('./src/services/AutonomousTraderService');
const TraderDatabase = require('./src/services/TraderDatabase');
// Destructure AI functions for backward compatibility (used throughout server.js)
const { callAI, callGrok, callOllama, callMoE } = AIService;
// Destructure Wisdom functions for backward compatibility
const { loadWisdom, saveWisdom, generateEmbedding, cosineSimilarity, searchWisdom, regenerateAllEmbeddings } = WisdomService;
// Destructure prompt builders for backward compatibility
const {
buildDeepDivePrompt,
buildCheckupPrompt,
buildTradeParsePrompt,
buildDiscordTradeAnalysisPrompt,
buildCritiquePrompt,
buildTradePrompt,
buildIdeaPrompt,
buildStrategyAdvisorPrompt
} = promptBuilders;
// Destructure data functions for backward compatibility
const {
fetchJson,
fetchJsonHttp,
fetchText,
fetchTickerIVData,
estimateIVRank,
fetchOptionPremium,
fetchOptionPremiumSchwab,
fetchOptionPremiumCBOE,
fetchDeepDiveData
} = DataService;
// ============================================================================
// CENTRALIZED MARKET DATA SERVICE
// All features should use this for consistent, reliable pricing
// ============================================================================
const MarketDataService = require('./src/services/MarketDataService');
MarketDataService.initialize(schwabApiCall);
const PORT = process.env.PORT || 8888;
const app = express();
// ============================================================================
// DATA CACHE - Re-exported from CacheService for backward compatibility
// ============================================================================
const { tickerDataCache, optionPremiumCache, CACHE_TTL, getCacheKey, logCache } = CacheService;
// Parse JSON request bodies
app.use(express.json({ limit: '50mb' })); // Large limit for image uploads
// CORS middleware
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
return res.sendStatus(204);
}
next();
});
// Health check endpoint (for live indicator)
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
uptime: process.uptime(),
timestamp: Date.now()
});
});
// Mount settings API routes
app.use('/api/settings', settingsRoutes);
// Mount Schwab API routes
app.use('/api/schwab', schwabRoutes);
// Mount wisdom API routes (Vector RAG for trading rules)
app.use('/api/wisdom', wisdomRoutes);
// Initialize and mount CBOE/Yahoo routes
cboeRoutes.init({
fetchJson,
fetchTickerIVData
});
app.use('/api', cboeRoutes);
// Initialize and mount Update routes
updateRoutes.init({
fetchJson,
fetchText,
getLocalVersion,
compareVersions,
rootDir: __dirname
});
app.use('/api', updateRoutes);
// Initialize and mount AI routes
aiRoutes.init({
AIService,
DataService,
DiscoveryService,
promptBuilders,
MarketDataService,
TechnicalService,
CoachingService,
formatExpiryForCBOE,
detectGPU,
fetchJsonHttp,
MODEL_VRAM_REQUIREMENTS
});
app.use('/api/ai', aiRoutes);
// Mount Scanner routes (wheel candidate scanner)
scannerRoutes.init({ DataService });
app.use('/api/scanner', scannerRoutes);
// Initialize and mount Summary routes (weekly summaries)
summaryRoutes.init({ AIService, promptBuilders });
app.use('/api/summary', summaryRoutes);
// Initialize and mount Coaching routes (trading coach)
coachingRoutes.init({ CoachingService });
app.use('/api/coaching', coachingRoutes);
// Initialize and mount Autonomous Trader routes
autonomousRoutes.init({ AutonomousTrader: AutonomousTraderService, TraderDB: TraderDatabase });
app.use('/api/autonomous', autonomousRoutes);
// Main request handler (converted to Express middleware)
// Now only handles static file serving - all API routes moved to route modules
const mainHandler = async (req, res, next) => {
try {
const url = new URL(req.url, `http://localhost:${PORT}`);
// Skip all API routes - they're handled by Express routers
if (url.pathname.startsWith('/api/')) {
return next();
}
// Static file serving
let filePath = url.pathname;
if (filePath === '/') filePath = '/index.html';
const fullPath = path.join(__dirname, filePath);
const ext = path.extname(fullPath);
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
try {
const content = fs.readFileSync(fullPath);
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);
} catch (e) {
if (e.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File not found');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Server error');
}
}
} catch (err) {
// Top-level error handler for mainHandler
console.error('[SERVER] Unhandled error in mainHandler:', err.message);
if (!res.headersSent) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Internal server error' }));
}
}
};
// ═══════════════════════════════════════════════════════════════════════════
// ALL API ROUTES NOW IN ROUTE MODULES:
// - /api/cboe/*, /api/iv/*, /api/yahoo/* → src/routes/cboeRoutes.js
// - /api/update/*, /api/version, /api/restart → src/routes/updateRoutes.js
// - /api/ai/* (17 endpoints) → src/routes/aiRoutes.js
// ═══════════════════════════════════════════════════════════════════════════
// ═══════════════════════════════════════════════════════════════════════════
// BUILD STRATEGY ADVISOR PROMPT - Moved to src/services/promptBuilders.js
// ═══════════════════════════════════════════════════════════════════════════
// ═══════════════════════════════════════════════════════════════════════════
// PROMPT BUILDERS (continued) - Moved to src/services/promptBuilders.js
// Functions: buildIdeaPrompt, buildStrategyAdvisorPrompt
// ═══════════════════════════════════════════════════════════════════════════
// ============================================================================
// AI CALLING FUNCTIONS - Now imported from AIService.js
// callAI, callGrok, callOllama, callMoE are destructured at top of file
// ============================================================================
// Use main handler as Express middleware
app.use(mainHandler);
// Create HTTP server with Express app
// Global Express error handler (catches route errors)
app.use((err, req, res, next) => {
console.error('[EXPRESS ERROR]', err.message);
if (!res.headersSent) {
res.status(500).json({ error: 'Internal server error', details: err.message });
}
});
const server = http.createServer(app);
// Create Socket.IO server for real-time streaming
const io = new SocketIOServer(server, {
cors: {
origin: '*',
methods: ['GET', 'POST']
}
});
// Initialize streaming routes with Socket.IO
streamingRoutes.init({ app, socketIO: io });
// Initialize Autonomous Trader (after Socket.IO is ready)
AutonomousTraderService.init({
AIService,
MarketDataService,
DataService,
DiscoveryService,
promptBuilders,
socketIO: io
});
// Handle uncaught exceptions to prevent server crash
process.on('uncaughtException', (err) => {
console.error('[FATAL] Uncaught exception:', err.message);
console.error(err.stack);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('[FATAL] Unhandled rejection:', reason);
});
server.listen(PORT, () => {
const version = getLocalVersion();
console.log(`
╔════════════════════════════════════════════════════╗
║ 🏠 WheelHouse Server v${version.padEnd(6)} ║
╠════════════════════════════════════════════════════╣
║ URL: http://localhost:${PORT} ║
║ ║
║ Features: ║
║ • Static file serving ║
║ • CBOE options proxy at /api/cboe/{TICKER}.json ║
║ • Settings API at /api/settings ║
║ • Update check at /api/update/check ║
║ • Real-time streaming at /api/streaming/* ║
╚════════════════════════════════════════════════════╝
`);
});