Kraft102 commited on
Commit
13508db
·
1 Parent(s): 7dd66c0

fix: update backend URL to new HF space

Browse files
apps/backend/src/index.ts CHANGED
@@ -529,6 +529,11 @@ async function startServer() {
529
  app.use('/api/acquisition', acquisitionRoutes);
530
  console.log('🌾 Omni-Harvester API mounted at /api/acquisition');
531
 
 
 
 
 
 
532
  // Start KnowledgeCompiler auto-compilation (every 60 seconds)
533
  const { knowledgeCompiler } = await import('./services/Knowledge/index.js');
534
  knowledgeCompiler.startAutoCompilation(60000);
 
529
  app.use('/api/acquisition', acquisitionRoutes);
530
  console.log('🌾 Omni-Harvester API mounted at /api/acquisition');
531
 
532
+ // Email API - Cloud-compatible email fetching via IMAP
533
+ const emailRoutes = (await import('./routes/email.js')).default;
534
+ app.use('/api/email', emailRoutes);
535
+ console.log('📧 Email API mounted at /api/email');
536
+
537
  // Start KnowledgeCompiler auto-compilation (every 60 seconds)
538
  const { knowledgeCompiler } = await import('./services/Knowledge/index.js');
539
  knowledgeCompiler.startAutoCompilation(60000);
apps/backend/src/mcp/EventBus.ts CHANGED
@@ -72,7 +72,9 @@ export type EventType =
72
  | 'recovery:completed'
73
  | 'health:recorded'
74
  | 'source:health'
75
- | 'decision:made';
 
 
76
 
77
  export interface BaseEvent {
78
  type: EventType;
 
72
  | 'recovery:completed'
73
  | 'health:recorded'
74
  | 'source:health'
75
+ | 'decision:made'
76
+ // Email events
77
+ | 'email:refresh';
78
 
79
  export interface BaseEvent {
80
  type: EventType;
apps/backend/src/routes/email.ts ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Email API - Cloud-compatible email fetching
3
+ *
4
+ * Provides REST endpoints for email retrieval using:
5
+ * 1. IMAP with App Passwords (Outlook/Gmail)
6
+ * 2. JSON file fallback for cached emails
7
+ * 3. Mock data for demo purposes
8
+ */
9
+
10
+ import { Router, Request, Response } from 'express';
11
+ import { eventBus } from '../mcp/EventBus.js';
12
+ import { logger } from '../utils/logger.js';
13
+ import Imap from 'imap';
14
+ import { simpleParser } from 'mailparser';
15
+
16
+ const router = Router();
17
+ const log = logger.child({ module: 'email-api' });
18
+
19
+ // In-memory cache
20
+ let emailCache: any[] = [];
21
+ let lastFetchTime: Date | null = null;
22
+ const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
23
+
24
+ interface EmailMessage {
25
+ id: string;
26
+ subject: string;
27
+ sender: string;
28
+ content: string;
29
+ timestamp: string;
30
+ metadata: {
31
+ isRead: boolean;
32
+ importance: string;
33
+ provider: string;
34
+ };
35
+ }
36
+
37
+ /**
38
+ * Fetch emails via IMAP
39
+ */
40
+ async function fetchViaImap(config: {
41
+ host: string;
42
+ port: number;
43
+ user: string;
44
+ password: string;
45
+ tls: boolean;
46
+ provider: string;
47
+ }): Promise<EmailMessage[]> {
48
+ return new Promise((resolve, reject) => {
49
+ const emails: EmailMessage[] = [];
50
+
51
+ const imap = new Imap({
52
+ user: config.user,
53
+ password: config.password,
54
+ host: config.host,
55
+ port: config.port,
56
+ tls: config.tls,
57
+ tlsOptions: { rejectUnauthorized: false },
58
+ authTimeout: 10000,
59
+ connTimeout: 10000,
60
+ });
61
+
62
+ imap.once('ready', () => {
63
+ imap.openBox('INBOX', true, (err, box) => {
64
+ if (err) {
65
+ imap.end();
66
+ return reject(err);
67
+ }
68
+
69
+ // Fetch last 10 emails
70
+ const total = box.messages.total;
71
+ const start = Math.max(1, total - 9);
72
+ const range = `${start}:${total}`;
73
+
74
+ const fetch = imap.seq.fetch(range, {
75
+ bodies: '',
76
+ struct: true,
77
+ });
78
+
79
+ fetch.on('message', (msg, seqno) => {
80
+ msg.on('body', (stream) => {
81
+ simpleParser(stream as any, (parseErr, parsed) => {
82
+ if (parseErr) {
83
+ log.warn(`Failed to parse email ${seqno}:`, parseErr);
84
+ return;
85
+ }
86
+
87
+ emails.push({
88
+ id: `${config.provider}-${seqno}-${Date.now()}`,
89
+ subject: parsed.subject || '(No Subject)',
90
+ sender: parsed.from?.text || 'Unknown',
91
+ content: parsed.text?.substring(0, 500) || '',
92
+ timestamp: parsed.date?.toISOString() || new Date().toISOString(),
93
+ metadata: {
94
+ isRead: false, // IMAP would need flags check
95
+ importance: 'normal',
96
+ provider: config.provider,
97
+ },
98
+ });
99
+ });
100
+ });
101
+ });
102
+
103
+ fetch.once('error', (fetchErr) => {
104
+ log.error('IMAP fetch error:', fetchErr);
105
+ });
106
+
107
+ fetch.once('end', () => {
108
+ imap.end();
109
+ resolve(emails.reverse()); // Newest first
110
+ });
111
+ });
112
+ });
113
+
114
+ imap.once('error', (imapErr: Error) => {
115
+ log.error('IMAP connection error:', imapErr);
116
+ reject(imapErr);
117
+ });
118
+
119
+ imap.once('end', () => {
120
+ log.debug('IMAP connection ended');
121
+ });
122
+
123
+ imap.connect();
124
+ });
125
+ }
126
+
127
+ /**
128
+ * Try to fetch emails from all configured sources
129
+ */
130
+ async function fetchEmails(forceRefresh = false): Promise<{
131
+ emails: EmailMessage[];
132
+ source: string;
133
+ unreadCount: number;
134
+ }> {
135
+ // Check cache first
136
+ if (!forceRefresh && emailCache.length > 0 && lastFetchTime) {
137
+ const age = Date.now() - lastFetchTime.getTime();
138
+ if (age < CACHE_TTL_MS) {
139
+ log.debug('Returning cached emails');
140
+ return {
141
+ emails: emailCache,
142
+ source: 'cache',
143
+ unreadCount: emailCache.filter(e => !e.metadata?.isRead).length,
144
+ };
145
+ }
146
+ }
147
+
148
+ // Try Outlook IMAP
149
+ if (process.env.OUTLOOK_EMAIL && process.env.OUTLOOK_APP_PASSWORD) {
150
+ try {
151
+ log.info('Fetching emails via Outlook IMAP...');
152
+ const emails = await fetchViaImap({
153
+ host: 'outlook.office365.com',
154
+ port: 993,
155
+ user: process.env.OUTLOOK_EMAIL,
156
+ password: process.env.OUTLOOK_APP_PASSWORD,
157
+ tls: true,
158
+ provider: 'outlook',
159
+ });
160
+
161
+ emailCache = emails;
162
+ lastFetchTime = new Date();
163
+
164
+ return {
165
+ emails,
166
+ source: 'outlook',
167
+ unreadCount: emails.filter(e => !e.metadata?.isRead).length,
168
+ };
169
+ } catch (err) {
170
+ log.warn('Outlook IMAP failed:', err);
171
+ }
172
+ }
173
+
174
+ // Try Gmail IMAP
175
+ if (process.env.GMAIL_EMAIL && process.env.GMAIL_APP_PASSWORD) {
176
+ try {
177
+ log.info('Fetching emails via Gmail IMAP...');
178
+ const emails = await fetchViaImap({
179
+ host: 'imap.gmail.com',
180
+ port: 993,
181
+ user: process.env.GMAIL_EMAIL,
182
+ password: process.env.GMAIL_APP_PASSWORD,
183
+ tls: true,
184
+ provider: 'gmail',
185
+ });
186
+
187
+ emailCache = emails;
188
+ lastFetchTime = new Date();
189
+
190
+ return {
191
+ emails,
192
+ source: 'gmail',
193
+ unreadCount: emails.filter(e => !e.metadata?.isRead).length,
194
+ };
195
+ } catch (err) {
196
+ log.warn('Gmail IMAP failed:', err);
197
+ }
198
+ }
199
+
200
+ // Return mock data as fallback
201
+ log.info('Using mock email data');
202
+ const mockEmails: EmailMessage[] = [
203
+ {
204
+ id: 'mock-1',
205
+ subject: 'Quarterly Report Ready',
206
+ sender: 'analytics@company.dk',
207
+ content: 'The Q4 analytics report is ready for review...',
208
+ timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000).toISOString(),
209
+ metadata: { isRead: false, importance: 'high', provider: 'mock' },
210
+ },
211
+ {
212
+ id: 'mock-2',
213
+ subject: 'Meeting Reminder: Strategy Session',
214
+ sender: 'calendar@company.dk',
215
+ content: 'Reminder: Strategy session tomorrow at 10:00...',
216
+ timestamp: new Date(Date.now() - 5 * 60 * 60 * 1000).toISOString(),
217
+ metadata: { isRead: false, importance: 'normal', provider: 'mock' },
218
+ },
219
+ {
220
+ id: 'mock-3',
221
+ subject: 'Security Alert: New Login',
222
+ sender: 'security@company.dk',
223
+ content: 'A new login was detected from Copenhagen...',
224
+ timestamp: new Date(Date.now() - 12 * 60 * 60 * 1000).toISOString(),
225
+ metadata: { isRead: true, importance: 'high', provider: 'mock' },
226
+ },
227
+ {
228
+ id: 'mock-4',
229
+ subject: 'Weekly Newsletter',
230
+ sender: 'news@industry.com',
231
+ content: 'This week in technology: AI advances...',
232
+ timestamp: new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(),
233
+ metadata: { isRead: true, importance: 'normal', provider: 'mock' },
234
+ },
235
+ ];
236
+
237
+ emailCache = mockEmails;
238
+ lastFetchTime = new Date();
239
+
240
+ return {
241
+ emails: mockEmails,
242
+ source: 'mock',
243
+ unreadCount: mockEmails.filter(e => !e.metadata?.isRead).length,
244
+ };
245
+ }
246
+
247
+ /**
248
+ * GET /api/email/inbox
249
+ * Fetch emails from configured sources
250
+ */
251
+ router.get('/inbox', async (req: Request, res: Response) => {
252
+ try {
253
+ const forceRefresh = req.query.refresh === 'true';
254
+ const result = await fetchEmails(forceRefresh);
255
+
256
+ // Emit event for real-time updates
257
+ eventBus.emit('email:refresh', {
258
+ type: 'email:refresh',
259
+ timestamp: new Date().toISOString(),
260
+ source: 'email-api',
261
+ payload: { count: result.emails.length, source: result.source },
262
+ });
263
+
264
+ res.json({
265
+ success: true,
266
+ ...result,
267
+ lastFetch: lastFetchTime?.toISOString(),
268
+ });
269
+ } catch (error: any) {
270
+ log.error('Email inbox error:', error);
271
+ res.status(500).json({
272
+ success: false,
273
+ error: error.message || 'Failed to fetch emails',
274
+ emails: [],
275
+ unreadCount: 0,
276
+ source: 'error',
277
+ });
278
+ }
279
+ });
280
+
281
+ /**
282
+ * GET /api/email/status
283
+ * Get email service status
284
+ */
285
+ router.get('/status', (_req: Request, res: Response) => {
286
+ const hasOutlook = !!(process.env.OUTLOOK_EMAIL && process.env.OUTLOOK_APP_PASSWORD);
287
+ const hasGmail = !!(process.env.GMAIL_EMAIL && process.env.GMAIL_APP_PASSWORD);
288
+
289
+ res.json({
290
+ configured: hasOutlook || hasGmail,
291
+ providers: {
292
+ outlook: hasOutlook,
293
+ gmail: hasGmail,
294
+ },
295
+ cacheStatus: {
296
+ count: emailCache.length,
297
+ lastFetch: lastFetchTime?.toISOString() || null,
298
+ ttlMs: CACHE_TTL_MS,
299
+ },
300
+ });
301
+ });
302
+
303
+ /**
304
+ * POST /api/email/refresh
305
+ * Force refresh emails
306
+ */
307
+ router.post('/refresh', async (_req: Request, res: Response) => {
308
+ try {
309
+ const result = await fetchEmails(true);
310
+ res.json({
311
+ success: true,
312
+ message: 'Emails refreshed',
313
+ ...result,
314
+ });
315
+ } catch (error: any) {
316
+ log.error('Email refresh error:', error);
317
+ res.status(500).json({
318
+ success: false,
319
+ error: error.message || 'Failed to refresh emails',
320
+ });
321
+ }
322
+ });
323
+
324
+ export default router;