Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat: stream emails to UI in real-time during sync

Adam Eivy66efa645f910
diff --git a/client/src/components/messages/InboxTab.jsx b/client/src/components/messages/InboxTab.jsx
index 01cd7dc7a7..7965de25b4 100644
--- a/client/src/components/messages/InboxTab.jsx
+++ b/client/src/components/messages/InboxTab.jsx
@@ -3,6 +3,7 @@ import { Mail, Search, RefreshCw, ChevronRight, Sparkles, Archive, Trash2, Reply
import { useNavigate } from 'react-router-dom';
import toast from 'react-hot-toast';
import * as api from '../../services/api';
+import socket from '../../services/socket';
import MessageDetail from './MessageDetail';
const ACTION_CONFIG = {
@@ -53,6 +54,34 @@ export default function InboxTab({ accounts }) {
fetchMessages();
}, [fetchMessages]);
+ // Stream messages into the list as they arrive during sync
+ useEffect(() => {
+ const onSyncMessage = ({ messages: incoming }) => {
+ if (!incoming?.length) return;
+ setMessages(prev => {
+ const byExtId = new Map(prev.map(m => [m.externalId, m]));
+ let changed = false;
+ for (const msg of incoming) {
+ if (msg.externalId && byExtId.has(msg.externalId)) {
+ const existing = byExtId.get(msg.externalId);
+ byExtId.set(msg.externalId, { ...existing, ...msg, id: existing.id });
+ changed = true;
+ } else {
+ byExtId.set(msg.externalId || msg.id, msg);
+ changed = true;
+ }
+ }
+ if (!changed) return prev;
+ return Array.from(byExtId.values()).sort((a, b) =>
+ new Date(b.date || 0) - new Date(a.date || 0)
+ );
+ });
+ setTotal(prev => prev + incoming.length);
+ };
+ socket.on('messages:sync:message', onSyncMessage);
+ return () => socket.off('messages:sync:message', onSyncMessage);
+ }, []);
+
const handleSync = async (mode) => {
const targets = selectedAccount
? accounts.filter(a => a.id === selectedAccount && a.enabled)
diff --git a/server/services/messagePlaywrightSync.js b/server/services/messagePlaywrightSync.js
index deb23fd01e..86ad32cccf 100644
--- a/server/services/messagePlaywrightSync.js
+++ b/server/services/messagePlaywrightSync.js
@@ -170,6 +170,34 @@ export async function syncPlaywright(account, cache, io, options = {}) {
const messages = [];
let detailsFetched = 0;
+ // Helper: build a message object from extracted data
+ const buildMessage = (msg, extId, overrides = {}) => ({
+ id: uuidv4(),
+ externalId: extId,
+ threadId: null,
+ from: { name: msg.from || '', email: msg.fromEmail || '' },
+ to: [], cc: [],
+ subject: msg.subject || '',
+ bodyText: msg.preview || '',
+ bodyFull: false,
+ date: msg.date || new Date().toISOString(),
+ isRead: !(msg.isUnread ?? false),
+ isUnread: msg.isUnread ?? false,
+ isPinned: msg.isPinned ?? false,
+ isFlagged: msg.isFlagged ?? false,
+ isReplied: msg.isReplied ?? false,
+ hasMeetingInvite: msg.hasMeetingInvite ?? false,
+ labels: [], source: account.type,
+ syncedAt: new Date().toISOString(),
+ ...overrides
+ });
+
+ // Helper: emit a batch of messages to the client in real-time
+ const emitMessages = (msgs) => {
+ if (!io || msgs.length === 0) return;
+ io.emit('messages:sync:message', { accountId: account.id, messages: msgs });
+ };
+
for (let i = 0; i < extracted.length; i++) {
const msg = extracted[i];
const extId = makeExternalId(msg.date || '', msg.from || '', msg.subject || '');
@@ -177,25 +205,9 @@ export async function syncPlaywright(account, cache, io, options = {}) {
// Skip detail fetch if we already have full body cached
if (existingMap.has(extId)) {
- messages.push({
- id: uuidv4(),
- externalId: extId,
- threadId: msg.threadKey || null,
- from: { name: msg.from || '', email: msg.fromEmail || '' },
- to: [], cc: [],
- subject: msg.subject || '',
- bodyText: msg.preview || '',
- bodyFull: false, // will keep existing cached full body
- date: msg.date || new Date().toISOString(),
- isRead: !(msg.isUnread ?? false),
- isUnread: msg.isUnread ?? false,
- isPinned: msg.isPinned ?? false,
- isFlagged: msg.isFlagged ?? false,
- isReplied: msg.isReplied ?? false,
- hasMeetingInvite: msg.hasMeetingInvite ?? false,
- labels: [], source: account.type,
- syncedAt: new Date().toISOString()
- });
+ const m = buildMessage(msg, extId, { threadId: msg.threadKey || null });
+ messages.push(m);
+ emitMessages([m]);
continue;
}
@@ -204,73 +216,27 @@ export async function syncPlaywright(account, cache, io, options = {}) {
const detail = await fetchOutlookConversationDetail(page, msg.subject, msg.from);
if (detail && detail.length > 0) {
detailsFetched++;
- // Thread key groups all messages in this conversation
const threadKey = `thread-${extId}`;
- for (const threadMsg of detail) {
- messages.push({
- id: uuidv4(),
- externalId: makeExternalId(threadMsg.date || msg.date || '', threadMsg.from || msg.from || '', msg.subject || ''),
- threadId: threadKey,
- from: { name: threadMsg.from || msg.from || '', email: threadMsg.fromEmail || msg.fromEmail || '' },
- to: threadMsg.to || [],
- cc: threadMsg.cc || [],
- subject: msg.subject || '',
- bodyText: threadMsg.body || msg.preview || '',
- bodyFull: true,
- date: threadMsg.date || msg.date || new Date().toISOString(),
- isRead: !(msg.isUnread ?? false),
- isUnread: msg.isUnread ?? false,
- isPinned: msg.isPinned ?? false,
- isFlagged: msg.isFlagged ?? false,
- isReplied: msg.isReplied ?? false,
- hasMeetingInvite: msg.hasMeetingInvite ?? false,
- labels: [], source: account.type,
- syncedAt: new Date().toISOString()
- });
- }
+ const batch = detail.map(threadMsg => buildMessage(msg, makeExternalId(threadMsg.date || msg.date || '', threadMsg.from || msg.from || '', msg.subject || ''), {
+ threadId: threadKey,
+ from: { name: threadMsg.from || msg.from || '', email: threadMsg.fromEmail || msg.fromEmail || '' },
+ to: threadMsg.to || [],
+ cc: threadMsg.cc || [],
+ bodyText: threadMsg.body || msg.preview || '',
+ bodyFull: true,
+ date: threadMsg.date || msg.date || new Date().toISOString()
+ }));
+ messages.push(...batch);
+ emitMessages(batch);
} else {
- // Fallback: use list preview if detail extraction failed
- messages.push({
- id: uuidv4(),
- externalId: extId,
- threadId: null,
- from: { name: msg.from || '', email: msg.fromEmail || '' },
- to: [], cc: [],
- subject: msg.subject || '',
- bodyText: msg.preview || '',
- bodyFull: false,
- date: msg.date || new Date().toISOString(),
- isRead: !(msg.isUnread ?? false),
- isUnread: msg.isUnread ?? false,
- isPinned: msg.isPinned ?? false,
- isFlagged: msg.isFlagged ?? false,
- isReplied: msg.isReplied ?? false,
- hasMeetingInvite: msg.hasMeetingInvite ?? false,
- labels: [], source: account.type,
- syncedAt: new Date().toISOString()
- });
+ const m = buildMessage(msg, extId);
+ messages.push(m);
+ emitMessages([m]);
}
} else {
- // Non-outlook: use list preview as before
- messages.push({
- id: uuidv4(),
- externalId: extId,
- threadId: null,
- from: { name: msg.from || '', email: msg.fromEmail || '' },
- to: [], cc: [],
- subject: msg.subject || '',
- bodyText: msg.preview || '',
- bodyFull: false,
- date: msg.date || new Date().toISOString(),
- isRead: !(msg.isUnread ?? false),
- isUnread: msg.isUnread ?? false,
- isPinned: msg.isPinned ?? false,
- isFlagged: msg.isFlagged ?? false,
- isReplied: msg.isReplied ?? false,
- hasMeetingInvite: msg.hasMeetingInvite ?? false,
- labels: [], source: account.type,
- syncedAt: new Date().toISOString()
- });
+ const m = buildMessage(msg, extId);
+ messages.push(m);
+ emitMessages([m]);
}
}