forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrations.ts
More file actions
138 lines (128 loc) · 4.64 KB
/
Copy pathintegrations.ts
File metadata and controls
138 lines (128 loc) · 4.64 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
import { ipcMain } from 'electron'
import { readStickyNotes } from '../integrations/stickyNotes'
import { connect, disconnect, isConnected, connectedEmail } from '../integrations/oauth'
import { fetchGmail, fetchCalendar } from '../integrations/google'
import {
xConnect,
xStatus,
xSync,
xDisconnect,
xRunStateSnapshot,
type XSession
} from '../integrations/xConnector'
import {
getSourceState,
markProcessed,
lastSyncAt,
clearSyncState
} from '../integrations/syncState'
import { filterNew } from '../integrations/syncStateLogic'
import {
gmailSessionConnect,
gmailSessionStatus,
gmailSessionVerify,
gmailSessionFetch,
gmailSessionDisconnect
} from '../integrations/gmailSession'
import type {
GoogleStatus,
GoogleSource,
FetchNewResult,
GmailItem,
CalendarItem,
GmailSessionStatus,
GmailSessionFetchResult
} from '../../shared/types'
// All integrations IPC lives here (3e Sticky Notes + 3d Gmail/Calendar) so
// concurrent chat/KG work doesn't conflict in index.ts.
function googleStatus(): GoogleStatus {
const connected = isConnected()
return {
connected,
email: connected ? connectedEmail() : undefined,
lastSyncAt: connected ? lastSyncAt() || undefined : undefined
}
}
export function registerIntegrationsHandlers(): void {
ipcMain.handle('integrations:stickyNotes:read', async () => readStickyNotes())
ipcMain.handle('integrations:google:connect', async (): Promise<GoogleStatus> => {
await connect()
return googleStatus()
})
ipcMain.handle('integrations:google:disconnect', async (): Promise<GoogleStatus> => {
disconnect()
clearSyncState()
return googleStatus()
})
ipcMain.handle('integrations:google:status', async (): Promise<GoogleStatus> => googleStatus())
ipcMain.handle(
'integrations:google:gmailFetchNew',
async (): Promise<FetchNewResult<GmailItem>> => {
if (!isConnected()) return { ok: false, items: [], error: 'not_connected' }
try {
const all = await fetchGmail()
return { ok: true, items: filterNew(all, getSourceState('gmail').processedIds) }
} catch (e) {
return { ok: false, items: [], error: (e as Error).message }
}
}
)
ipcMain.handle(
'integrations:google:calendarFetchNew',
async (): Promise<FetchNewResult<CalendarItem>> => {
if (!isConnected()) return { ok: false, items: [], error: 'not_connected' }
try {
const all = await fetchCalendar()
return { ok: true, items: filterNew(all, getSourceState('calendar').processedIds) }
} catch (e) {
return { ok: false, items: [], error: (e as Error).message }
}
}
)
ipcMain.handle(
'integrations:google:markProcessed',
async (_e, source: GoogleSource, ids: string[]): Promise<void> => {
markProcessed(source, ids)
}
)
// --- Gmail via an Omi-owned Electron session (Option B). The user signs into
// Google once inside our persistent-partition login window; we replay the same
// Gmail web endpoints macOS uses over that own-session cookie jar. No OAuth. ---
ipcMain.handle(
'integrations:gmailSession:connect',
async (_e, email?: string): Promise<GmailSessionStatus> => gmailSessionConnect(email)
)
ipcMain.handle(
'integrations:gmailSession:status',
async (): Promise<GmailSessionStatus> => gmailSessionStatus()
)
// A network probe (vs. the cheap cookie-presence status): the renderer calls this
// after a failed fetch so a stale-but-present session flips the row to reconnect.
ipcMain.handle(
'integrations:gmailSession:verify',
async (): Promise<GmailSessionStatus> => gmailSessionVerify()
)
ipcMain.handle(
'integrations:gmailSession:fetch',
async (_e, query?: string, maxResults?: number): Promise<GmailSessionFetchResult> =>
gmailSessionFetch(query, maxResults)
)
ipcMain.handle(
'integrations:gmailSession:disconnect',
async (): Promise<GmailSessionStatus> => gmailSessionDisconnect()
)
// --- X (Twitter) connector. The renderer relays { apiBase, token } per call; the
// connect run lives in main so it outlives the Connect panel and streams progress
// over integrations:x:progress. ---
ipcMain.handle('integrations:x:status', async (_e, session: XSession) => xStatus(session))
ipcMain.handle('integrations:x:connect', async (_e, session: XSession) => {
xConnect(session)
return xRunStateSnapshot()
})
ipcMain.handle('integrations:x:runState', async () => xRunStateSnapshot())
ipcMain.handle('integrations:x:sync', async (_e, session: XSession) => xSync(session))
ipcMain.handle('integrations:x:disconnect', async (_e, session: XSession) => {
await xDisconnect(session)
return { success: true }
})
}