-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.ts
More file actions
356 lines (308 loc) · 12.8 KB
/
background.ts
File metadata and controls
356 lines (308 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
import { Storage } from "@plasmohq/storage"
const storage = new Storage()
const ACTIVE_MEETING_SESSION_KEY = "activeMeetingSession_v1"
console.log("Vexa-Hubspot background script loaded.")
// Clear any stale active session on startup or install
const clearActiveSession = async () => {
await storage.remove(ACTIVE_MEETING_SESSION_KEY)
console.log("Cleared any stale active meeting session.")
}
chrome.runtime.onStartup.addListener(clearActiveSession)
chrome.runtime.onInstalled.addListener(clearActiveSession)
// Listen for messages from content scripts or popup
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("Background script received message:", message)
if (message.type === "MEETING_STARTED") {
handleMeetingStarted(message.meetingId, sendResponse)
return true
} else if (message.type === "LOG_TO_HUBSPOT") {
console.log(`[BACKGROUND] Received LOG_TO_HUBSPOT. Message MeetingId: '${message.meetingId}', Type: ${typeof message.meetingId}`);
handleLogToHubspotInBackground(message.meetingId, sendResponse)
return true
} else if (message.type === "GET_ACTIVE_MEETING_SESSION") {
storage.get(ACTIVE_MEETING_SESSION_KEY).then(sessionData => {
console.log("[BACKGROUND] GET_ACTIVE_MEETING_SESSION: Retrieved from storage:", sessionData);
sendResponse(sessionData);
});
return true
} else if (message.type === "UPDATE_ASSOCIATED_CONTACTS") {
handleUpdateAssociatedContacts(message.meetingId, message.contactIds, sendResponse);
return true
} else if (message.type === "MEETING_ENDED") {
handleMeetingEndedLogic(message.meetingId, sendResponse)
return true
}
return false
})
async function handleMeetingStarted(meetingId: string, sendResponse: (response: any) => void) {
try {
console.log("Handling meeting started for ID:", meetingId)
const apiKey = await storage.get("vexaApiKey")
if (!apiKey) {
const errorMsg = "No Vexa API key found. Please configure it in the extension popup."
console.error("Vexa-Hubspot:", errorMsg)
sendResponse({ success: false, error: errorMsg })
return
}
// Store new active meeting session, overwriting any previous one
const newSession = {
id: meetingId,
startTime: Date.now(),
associatedContactIds: []
}
await storage.set(ACTIVE_MEETING_SESSION_KEY, newSession)
console.log("New active meeting session stored:", newSession)
console.log("Vexa API key found, deploying bot...")
const botResponse = await deployVexaBot(meetingId, apiKey)
if (botResponse.success) {
console.log("Vexa bot successfully deployed:", botResponse.data)
sendResponse({ success: true, message: "Bot deployed successfully", data: botResponse.data, meetingSession: newSession })
} else {
console.error("Failed to deploy Vexa bot:", botResponse.error)
sendResponse({ success: false, error: botResponse.error })
}
} catch (error) {
console.error("Error in handleMeetingStarted:", error)
sendResponse({ success: false, error: error.message })
}
}
async function handleUpdateAssociatedContacts(
meetingId: string,
contactIds: string[],
sendResponse: (response: any) => void
) {
try {
const currentSession = await storage.get(ACTIVE_MEETING_SESSION_KEY) as any
if (currentSession && currentSession.id === meetingId) {
currentSession.associatedContactIds = contactIds
await storage.set(ACTIVE_MEETING_SESSION_KEY, currentSession)
console.log(`Updated associated contacts for meeting ${meetingId}:`, contactIds)
sendResponse({ success: true, updatedSession: currentSession })
} else {
const errorMsg = `No active session found for meeting ID ${meetingId} or ID mismatch.`
console.warn(errorMsg, "Current session:", currentSession)
sendResponse({ success: false, error: errorMsg })
}
} catch (error) {
console.error("Error updating associated contacts:", error)
sendResponse({ success: false, error: error.message })
}
}
async function handleLogToHubspotInBackground(
meetingId: string,
sendResponse: (response: any) => void
) {
try {
const currentSession = await storage.get(ACTIVE_MEETING_SESSION_KEY) as any;
console.log(`[BACKGROUND] handleLogToHubspotInBackground: Incoming meetingId: '${meetingId}', Type: ${typeof meetingId}`);
console.log("[BACKGROUND] handleLogToHubspotInBackground: Fetched currentSession from storage:", currentSession);
if (!currentSession || currentSession.id !== meetingId) {
const errorMsg = `No active session found for meeting ID ${meetingId} to log, or ID mismatch. Session ID: ${currentSession?.id}`;
console.error("Vexa-Hubspot:", errorMsg, "Current session from storage:", currentSession, "Popup meetingId:", meetingId);
sendResponse({ success: false, error: errorMsg });
return
}
const contactIds = currentSession.associatedContactIds
if (!contactIds || contactIds.length === 0) {
const errorMsg = `No contacts associated with meeting ID ${meetingId}. Please select contacts in the popup.`
console.error("Vexa-Hubspot:", errorMsg)
sendResponse({ success: false, error: errorMsg })
return
}
console.log(`Logging call to HubSpot for meeting ${meetingId}, contacts: ${contactIds.join(", ")}`)
const hubspotToken = await storage.get("hubspotAccessToken")
const vexaApiKey = await storage.get("vexaApiKey")
if (!hubspotToken || !vexaApiKey) {
const errorMsg = "HubSpot Token or Vexa API Key not found. Please configure them in the extension popup."
console.error("Vexa-Hubspot:", errorMsg)
sendResponse({ success: false, error: errorMsg })
return
}
// --- Fetch Transcript from Vexa ---
console.log(`Fetching transcript for meeting ID: ${meetingId}`);
const transcriptResponse = await retrieveTranscript(meetingId, vexaApiKey);
let transcriptText: string;
if (transcriptResponse.success && transcriptResponse.data) {
const vexaData = transcriptResponse.data;
// Try to extract transcript text in a few common ways, Vexa API structure dependent
if (typeof vexaData.transcript === 'string') {
transcriptText = vexaData.transcript;
} else if (vexaData.transcript && typeof vexaData.transcript.text_formatted === 'string') {
transcriptText = vexaData.transcript.text_formatted;
} else if (typeof vexaData.text_formatted === 'string') {
transcriptText = vexaData.text_formatted;
} else if (typeof vexaData.text === 'string') {
transcriptText = vexaData.text;
} else if (Array.isArray(vexaData.segments)) {
transcriptText = vexaData.segments
.map(segment => `${segment.speaker_label || segment.speaker || 'Unknown Speaker'}: ${segment.text || segment.line || ''}`)
.join('\n');
} else {
// Fallback to stringifying the whole data object if no specific text field is found
transcriptText = JSON.stringify(vexaData, null, 2);
}
console.log("Using fetched Vexa transcript.");
} else {
const errorMsg = `Failed to retrieve Vexa transcript: ${transcriptResponse.error || 'Unknown error'}. Call cannot be logged with transcript.`;
console.error("Vexa-Hubspot:", errorMsg);
sendResponse({ success: false, error: errorMsg });
return;
}
const logResult = await logCallToHubspot(hubspotToken, contactIds, transcriptText, meetingId)
if (logResult.success) {
console.log("Call logged to HubSpot successfully:", logResult.data)
sendResponse({ success: true, data: logResult.data })
} else {
console.error("Failed to log call to HubSpot:", logResult.error)
sendResponse({ success: false, error: logResult.error })
}
} catch (error) {
console.error("Error in handleLogToHubspotInBackground:", error)
sendResponse({ success: false, error: error.message })
}
}
async function handleMeetingEndedLogic(meetingId: string, sendResponse: (response: any) => void) {
console.log(`Logic for meeting ended: ${meetingId}. Clearing active session.`);
// TODO: Implement actual transcript fetching and potential auto-logging here
// For now, just clear the active session
await storage.remove(ACTIVE_MEETING_SESSION_KEY)
sendResponse({ success: true, message: `Session for meeting ${meetingId} cleared.` })
}
async function deployVexaBot(meetingId: string, apiKey: string) {
try {
console.log("Calling Vexa API to deploy bot for meeting:", meetingId)
const response = await fetch("https://gateway.dev.vexa.ai/bots", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey
},
body: JSON.stringify({
native_meeting_id: meetingId,
platform: "google_meet"
})
})
if (!response.ok) {
const errorText = await response.text()
console.error("Vexa API error response:", response.status, errorText)
return {
success: false,
error: `Vexa API error: ${response.status} - ${errorText}`
}
}
const data = await response.json()
console.log("Vexa API success response:", data)
return {
success: true,
data: data
}
} catch (error) {
console.error("Network error calling Vexa API:", error)
return {
success: false,
error: `Network error: ${error.message}`
}
}
}
async function retrieveTranscript(meetingId: string, apiKey: string) {
try {
console.log("Calling Vexa API to retrieve transcript for meeting:", meetingId)
const response = await fetch(`https://gateway.dev.vexa.ai/transcripts/google_meet/${meetingId}`, {
method: "GET",
headers: {
"X-API-Key": apiKey
}
})
if (!response.ok) {
const errorText = await response.text()
console.error("Vexa transcript API error response:", response.status, errorText)
return {
success: false,
error: `Vexa transcript API error: ${response.status} - ${errorText}`
}
}
const data = await response.json()
console.log("Vexa transcript API success response:", data)
return {
success: true,
data: data
}
} catch (error) {
console.error("Network error calling Vexa transcript API:", error)
return {
success: false,
error: `Network error: ${error.message}`
}
}
}
async function logCallToHubspot(
token: string,
contactIds: string[],
transcriptText: string,
meetingId: string // For call title or other metadata
) {
const isLegacyKey = !token.startsWith('pat-')
const engagementUrl = isLegacyKey
? `https://api.hubapi.com/engagements/v1/engagements?hapikey=${token}`
: `https://api.hubapi.com/engagements/v1/engagements`
const headers = isLegacyKey
? { "Content-Type": "application/json" }
: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
}
const callBody = `Meeting Transcript (${meetingId})\n\n${transcriptText}`
const now = Date.now()
const engagementData = {
engagement: {
active: true,
// ownerId: 1, // Optional: associate with a specific HubSpot user ID
type: "CALL",
timestamp: now, // Timestamp of when the call occurred (or was logged)
},
associations: {
contactIds: contactIds, // Array of contact IDs
companyIds: [],
dealIds: [],
// ownerIds: [],
// ticketIds: []
},
metadata: {
body: callBody, // The actual transcript
// title: `Google Meet Call - ${meetingId}`, // Optional title for the engagement in HubSpot UI
// toNumber: "", // If applicable
// fromNumber: "", // If applicable
// status: "COMPLETED", // Call status
// durationMilliseconds: 360000 // Example: 1 hour
}
}
try {
console.log("Logging call to HubSpot with data:", JSON.stringify(engagementData, null, 2))
const response = await fetch(engagementUrl, {
method: "POST",
headers: headers,
body: JSON.stringify(engagementData)
})
if (!response.ok) {
const errorText = await response.text()
console.error("HubSpot log call API error response:", response.status, errorText)
return {
success: false,
error: `HubSpot API error: ${response.status} - ${errorText}`
}
}
const data = await response.json()
console.log("HubSpot log call API success response:", data)
return { success: true, data: data }
} catch (error) {
console.error("Network error logging call to HubSpot:", error)
return { success: false, error: `Network error: ${error.message}` }
}
}
// Optional: Listen for extension startup
chrome.runtime.onStartup.addListener(() => {
console.log("Vexa-Hubspot extension started.")
})
chrome.runtime.onInstalled.addListener((details) => {
console.log("Vexa-Hubspot extension installed/updated:", details.reason)
})