forked from tinyhumansai/openhuman-skills
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.ts
More file actions
379 lines (316 loc) · 12.8 KB
/
Copy pathsync.ts
File metadata and controls
379 lines (316 loc) · 12.8 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Gmail email sync: initial + incremental sync with 30-day window.
// Fetches messages via Gmail API and upserts into local SQLite database.
// Skips emails already in the local DB to avoid redundant API calls.
import { syncIntegrationMetadata } from '../../shared/integration-metadata';
import { gmailFetch, isGmailConnected } from './api';
import { loadGmailProfile } from './api/helpers';
import {
emailExists,
getEmailCount,
getEmails,
getUnsubmittedEmails,
markEmailsSubmitted,
markSensitiveAsSubmitted,
upsertEmail,
} from './db/helpers';
import { getGmailSkillState, publishSkillState } from './state';
import type { GmailMessage } from './types';
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
/** Number of days to look back for emails. */
const SYNC_WINDOW_DAYS = 30;
/** Max emails to fetch per API page. */
const PAGE_SIZE = 20;
/** Max pages to fetch per sync (20 emails/page × 10 pages = 200 emails). */
const MAX_PAGES = 10;
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/** Progress callback: receives a human-readable message and a 0-100 percentage. */
type SyncProgressCallback = (message: string, progress: number) => void;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Emit sync progress to the frontend via state. */
function emitSyncProgress(message: string, progress: number): void {
const s = getGmailSkillState();
s.syncStatus.syncProgress = progress;
s.syncStatus.syncProgressMessage = message;
state.setPartial({ syncProgress: progress, syncProgressMessage: message });
}
function syncGmailMetadataToBackend(): void {
const s = getGmailSkillState();
if (!s.profile) return;
const metadata = {
email_address: s.profile.emailAddress,
messages_total: s.profile.messagesTotal,
threads_total: s.profile.threadsTotal,
history_id: s.profile.historyId,
};
syncIntegrationMetadata({
title: 'Gmail profile sync',
content: JSON.stringify(metadata),
sourceType: 'email',
metadata,
});
}
/** Format a timestamp (ms) or days-ago offset as YYYY/MM/DD for Gmail query syntax. */
function gmailDateStr(msOrDaysAgo: number, isDaysAgo = false): string {
const ms = isDaysAgo ? Date.now() - msOrDaysAgo * 24 * 60 * 60 * 1000 : msOrDaysAgo;
const d = new Date(ms);
return `${d.getFullYear()}/${String(d.getMonth() + 1).padStart(2, '0')}/${String(d.getDate()).padStart(2, '0')}`;
}
/**
* Fetch a page of message IDs from the Gmail API.
* Returns the message references and optional next page token.
*/
async function fetchMessagePage(
query: string,
pageToken?: string
): Promise<{ messages: Array<{ id: string; threadId: string }>; nextPageToken?: string }> {
const params = [`maxResults=${PAGE_SIZE}`, `q=${encodeURIComponent(query)}`];
if (pageToken) params.push(`pageToken=${encodeURIComponent(pageToken)}`);
const response = await gmailFetch<{
messages?: Array<{ id: string; threadId: string }>;
nextPageToken?: string;
}>(`/users/me/messages?${params.join('&')}`);
if (!response.success || !response.data?.messages) {
if (response.error) console.error(`[gmail-sync] List error: ${response.error.message}`);
return { messages: [] };
}
return { messages: response.data.messages, nextPageToken: response.data.nextPageToken };
}
/**
* Fetch full message details and upsert into DB.
* Uses emailExists (SELECT 1) instead of fetching the full row for the skip check.
* Returns true if a new email was synced, false if skipped (already exists).
*/
async function syncMessage(msgId: string): Promise<boolean> {
if (emailExists(msgId)) return false;
const msgResponse = await gmailFetch(`/users/me/messages/${msgId}`);
if (msgResponse.success && msgResponse.data) {
const s = getGmailSkillState();
upsertEmail(msgResponse.data as GmailMessage, !s.config.showSensitiveMessages);
s.syncStatus.totalEmails++;
publishSkillState();
return true;
}
return false;
}
/**
* Shared pagination loop used by both initial and incremental sync.
* Fetches pages of message IDs then syncs each message individually.
* Returns { newEmails, skipped }.
*/
async function runSyncPages(
query: string,
maxPages: number,
log?: SyncProgressCallback
): Promise<{ newEmails: number; skipped: number }> {
let pageToken: string | undefined;
let newEmails = 0;
let skipped = 0;
let page = 0;
do {
page++;
log?.(`Fetching page ${page}...`, Math.min(5 + page * 8, 80));
const result = await fetchMessagePage(query, pageToken);
if (result.messages.length === 0) break;
pageToken = result.nextPageToken;
// Sync messages in parallel (5 concurrent) to avoid sequential proxy round-trips
const CONCURRENCY = 5;
for (let i = 0; i < result.messages.length; i += CONCURRENCY) {
const batch = result.messages.slice(i, i + CONCURRENCY);
const results = await Promise.all(batch.map(msgRef => syncMessage(msgRef.id)));
for (const wasNew of results) {
if (wasNew) newEmails++;
else skipped++;
}
}
log?.(`Page ${page}: ${newEmails} new, ${skipped} skipped`, Math.min(10 + page * 10, 90));
} while (pageToken && page < maxPages);
return { newEmails, skipped };
}
// ---------------------------------------------------------------------------
// Initial Sync
// ---------------------------------------------------------------------------
/**
* Perform initial sync: loads all emails from the last 30 days.
* Paginates through results and skips emails already in the local database.
* Called on first connect or when initial sync hasn't been completed.
*/
export async function performInitialSync(onProgress?: SyncProgressCallback): Promise<void> {
const s = getGmailSkillState();
if (!isGmailConnected()) {
console.log('[gmail-sync] No credential, skipping initial sync');
return;
}
if (s.syncStatus.syncInProgress) {
console.log('[gmail-sync] Sync already in progress, skipping');
return;
}
const log = (msg: string, pct: number) => {
console.log(`[gmail-sync] [${pct}%] ${msg}`);
emitSyncProgress(msg, pct);
onProgress?.(msg, pct);
};
s.syncStatus.syncInProgress = true;
s.syncStatus.newEmailsCount = 0;
s.syncStatus.totalEmails = getEmailCount();
publishSkillState();
try {
const afterDate = gmailDateStr(SYNC_WINDOW_DAYS, true);
log(`Starting initial sync (emails after ${afterDate})...`, 0);
const { newEmails, skipped } = await runSyncPages(`after:${afterDate}`, MAX_PAGES, log);
const now = Date.now();
state.set('initialSyncCompleted', true);
state.set('lastSyncTime', now);
s.syncStatus.lastSyncTime = now;
s.syncStatus.newEmailsCount = newEmails;
s.syncStatus.nextSyncTime = now + s.config.syncIntervalMinutes * 60 * 1000;
log(`Initial sync complete: ${newEmails} new emails, ${skipped} skipped`, 100);
// Ingest newly synced emails into knowledge graph
ingestNewEmails();
if (newEmails > 0 && s.config.notifyOnNewEmails) {
platform.notify('Gmail Sync Complete', `Synchronized ${newEmails} new emails`);
}
} catch (error) {
console.error(`[gmail-sync] Initial sync failed: ${error}`);
s.lastApiError = error instanceof Error ? error.message : String(error);
emitSyncProgress(`Sync failed: ${s.lastApiError}`, 0);
} finally {
s.syncStatus.syncInProgress = false;
s.syncStatus.syncProgress = 0;
s.syncStatus.syncProgressMessage = '';
publishSkillState();
const emails = getEmails();
state.setPartial({ emails });
}
}
// ---------------------------------------------------------------------------
// Incremental Sync
// ---------------------------------------------------------------------------
/**
* Incremental sync: fetches only emails newer than the last sync time,
* within the 30-day window. Falls back to initial sync if not yet completed.
*/
export async function onSync(): Promise<void> {
const s = getGmailSkillState();
if (!isGmailConnected() || s.syncStatus.syncInProgress) return;
try {
loadGmailProfile();
syncGmailMetadataToBackend();
} catch (error) {
console.warn(`[gmail] Profile fetch failed, continuing sync: ${error}`);
}
publishSkillState();
if (!isSyncCompleted()) {
return performInitialSync();
}
s.syncStatus.syncInProgress = true;
s.syncStatus.newEmailsCount = 0;
s.syncStatus.totalEmails = getEmailCount();
emitSyncProgress('Starting incremental sync...', 0);
try {
const lastSyncTime = getLastSyncTime();
const thirtyDaysAgoMs = Date.now() - SYNC_WINDOW_DAYS * 24 * 60 * 60 * 1000;
const effectiveMs = lastSyncTime ? Math.max(lastSyncTime, thirtyDaysAgoMs) : thirtyDaysAgoMs;
const query = `after:${gmailDateStr(effectiveMs)}`;
const { newEmails, skipped } = await runSyncPages(query, MAX_PAGES);
const now = Date.now();
state.set('lastSyncTime', now);
s.syncStatus.lastSyncTime = now;
s.syncStatus.newEmailsCount = newEmails;
s.syncStatus.nextSyncTime = now + s.config.syncIntervalMinutes * 60 * 1000;
emitSyncProgress(`Sync complete: ${newEmails} new, ${skipped} skipped`, 100);
console.log(`[gmail-sync] Incremental sync done: ${newEmails} new, ${skipped} skipped`);
// Ingest newly synced emails into knowledge graph
ingestNewEmails();
if (newEmails > 0 && s.config.notifyOnNewEmails) {
platform.notify('New Gmail Emails', `${newEmails} new emails synced`);
}
} catch (error) {
console.error(`[gmail-sync] Incremental sync failed: ${error}`);
s.lastApiError = error instanceof Error ? error.message : String(error);
emitSyncProgress(`Sync failed: ${s.lastApiError}`, 0);
} finally {
s.syncStatus.syncInProgress = false;
s.syncStatus.syncProgress = 0;
s.syncStatus.syncProgressMessage = '';
publishSkillState();
syncGmailMetadataToBackend();
const emails = getEmails();
state.setPartial({ emails });
}
}
// ---------------------------------------------------------------------------
// Ingest synced emails into knowledge graph via memory.insert()
// ---------------------------------------------------------------------------
/** Max emails to pull from DB per ingestion round. */
const INGEST_QUERY_LIMIT = 500;
/**
* Ingest un-submitted emails into the knowledge graph.
* Each email is sent via memory.insert() which routes through the Rust
* ingestion pipeline (upsert → GLiNER entity/relation extraction → graph).
* Sensitive emails are marked as submitted without being ingested.
*/
function ingestNewEmails(): void {
// Mark sensitive emails as submitted so they never enter the ingestion queue
markSensitiveAsSubmitted();
const emails = getUnsubmittedEmails(INGEST_QUERY_LIMIT);
if (emails.length === 0) return;
const submittedIds: string[] = [];
let ingested = 0;
for (const email of emails) {
const content = email.body_text || email.snippet || '';
if (content.length === 0) {
submittedIds.push(email.id);
continue;
}
try {
memory.insert({
title: email.subject || `Email ${email.id}`,
content,
sourceType: 'email',
documentId: `gmail-email-${email.id}`,
metadata: {
source: 'gmail',
type: 'email',
emailId: email.id,
threadId: email.thread_id,
senderEmail: email.sender_email,
senderName: email.sender_name,
recipientEmails: email.recipient_emails,
isRead: email.is_read === 1,
isImportant: email.is_important === 1,
isStarred: email.is_starred === 1,
hasAttachments: email.has_attachments === 1,
labels: email.labels,
},
createdAt: email.date ? email.date / 1000 : undefined,
updatedAt: email.updated_at ? email.updated_at / 1000 : undefined,
});
submittedIds.push(email.id);
ingested++;
} catch (e) {
console.error(`[gmail] Failed to ingest email ${email.id}: ${e}`);
}
}
if (submittedIds.length > 0) markEmailsSubmitted(submittedIds);
if (ingested > 0) {
console.log(`[gmail] Ingested ${ingested} email(s) into knowledge graph`);
}
}
// ---------------------------------------------------------------------------
// Sync state helpers
// ---------------------------------------------------------------------------
/** Check if initial sync has been completed. */
export function isSyncCompleted(): boolean {
return state.get('initialSyncCompleted') === true;
}
/** Get last sync timestamp (ms since epoch), or null if never synced. */
export function getLastSyncTime(): number | null {
const value = state.get('lastSyncTime');
return typeof value === 'number' ? value : null;
}