-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarization.js
412 lines (368 loc) · 13 KB
/
summarization.js
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// summarization.js
const {
downloadFile,
summarizeDocument,
fetchThreadMessages,
fetchThreadMessagesAndFiles,
analyzeMessagesForContent,
summarizeDocumentFromFile,
summarizeUrlContent,
} = require('./utils');
const { summarizeTextContent } = require('./vertexAIClient');
const db = require('./firebaseClient');
const {
FREE_TIER_DAILY_LIMIT,
FREE_TIER_MAX_DAYS,
} = require('./config');
module.exports = function (app) {
// Handle messages where @briefops is mentioned in a thread
app.message(/<@\w+>/, async ({ message, say, client }) => {
try {
const botUserId = process.env.SLACK_BOT_USER_ID;
if (!message.text.includes(`<@${botUserId}>`)) {
return; // Ignore messages that don't mention the bot directly
}
// Fetch the thread context if this is in a thread
const channelId = message.channel;
const threadTs = message.thread_ts || message.ts;
const { messages, files } = await fetchThreadMessagesAndFiles(
app,
channelId,
threadTs
);
if (messages.length === 0 && files.length === 0) {
await say({
text: `:information_source: No messages or files found in the thread to summarize.`,
thread_ts: threadTs,
});
return;
}
// Summarize messages and files using Vertex AI
const summary = await summarizeMessagesAndFiles(messages, files);
// Post summary back to the thread
await say({
text: `*Summary for the thread:*\n${summary}`,
thread_ts: threadTs,
});
} catch (error) {
console.error('Error handling @briefops mention:', error);
await say({
text: error.message || 'An error occurred while processing your request.',
thread_ts: message.thread_ts || message.ts,
});
}
});
// Handle the /briefops command
app.command('/briefops', async ({ command, ack, respond, say }) => {
try {
// Acknowledge the command
await ack();
const isInThread = Boolean(command.thread_ts);
const threadTs = command.thread_ts || command.ts;
const channelId = command.channel_id;
// Parse the command text for flags
const args = command.text.trim().split(/\s+/);
let numDays = 7; // Default number of days
let isPublic = true;
const isYouTubeFlagPresent = args.includes('--youtube');
args.forEach((arg) => {
if (arg === '--private') {
isPublic = false;
} else if (/^\d+$/.test(arg)) {
numDays = parseInt(arg, 10);
if (numDays <= 0) numDays = 7; // Ensure positive integer
}
});
if (isInThread) {
// Summarization in a thread
const messages = await fetchThreadMessages(app, channelId, threadTs);
// Analyze messages for content (files, URLs, etc.)
const { file, youtubeUrl, url } = analyzeMessagesForContent(messages);
if (file) {
await say({
text: `Found a document "${file.name}". Summarizing...`,
thread_ts: threadTs,
});
try {
const summary = await summarizeDocumentFromFile(file);
await say({
text: `*Summary of the document "${file.name}":*\n${summary}`,
thread_ts: threadTs,
});
} catch (error) {
console.error('Error summarizing document:', error);
await say({
text: `:warning: Failed to summarize the document. ${error.message}`,
thread_ts: threadTs,
});
}
} else if (youtubeUrl) {
await say({
text: `Found a YouTube link: ${youtubeUrl}`,
thread_ts: threadTs,
});
if (isYouTubeFlagPresent) {
await say({
text: ':construction: Summarizing YouTube videos is under development.',
thread_ts: threadTs,
});
} else {
await say({
text: ':information_source: Use `--youtube` for YouTube transcript summaries.',
thread_ts: threadTs,
});
}
} else if (url) {
await say({
text: `Found a URL: ${url}. Summarizing...`,
thread_ts: threadTs,
});
try {
const summary = await summarizeUrlContent(url);
await say({
text: `*Summary of the content at ${url}:*\n${summary}`,
thread_ts: threadTs,
});
} catch (error) {
console.error('Error summarizing URL:', error);
await say({
text: `:warning: Failed to summarize the URL. ${error.message}`,
thread_ts: threadTs,
});
}
} else {
await say({
text: 'No documents or URLs found in this thread to summarize.',
thread_ts: threadTs,
});
}
} else {
// Summarization in a channel (fetch messages for a number of days)
let messages;
try {
messages = await fetchChannelMessages(app, channelId, numDays);
} catch (error) {
// Handle the 'not_in_channel' error specifically
if (error.message.includes('Please invite me by typing')) {
await respond({
response_type: 'ephemeral',
text: error.message,
});
return;
} else {
throw error; // Re-throw other errors
}
}
if (messages.length === 0) {
await respond({
response_type: isPublic ? 'in_channel' : 'ephemeral',
text: `:information_source: No messages found in the past ${numDays} day(s) to summarize.`,
});
return;
}
await respond({
response_type: isPublic ? 'in_channel' : 'ephemeral',
text: '_Generating the summary, please wait..._',
});
try {
const summary = await summarizeMessagesAndFiles(messages, []);
await respond({
response_type: isPublic ? 'in_channel' : 'ephemeral',
text: `*Here is the summary for the past ${numDays} day(s):*\n${summary}`,
});
} catch (error) {
console.error('Error summarizing messages:', error);
await respond({
response_type: 'ephemeral',
text: `:warning: Failed to summarize the messages. ${error.message}`,
});
}
}
} catch (error) {
console.error('Error handling /briefops command:', error);
await respond({
response_type: 'ephemeral',
text: error.message || 'An error occurred while processing your request.',
});
}
});
// Function to summarize messages and files
async function summarizeMessagesAndFiles(messages, files) {
let content = messages.join('\n');
// Add file content if available
for (const file of files) {
const fileSummary = await summarizeFile(file);
content += `\n\n${fileSummary}`;
}
try {
console.log('[INFO] Sending messages to Vertex AI for summarization...');
const summary = await summarizeTextContent(content);
return summary;
} catch (error) {
console.error('Error during summarization:', error);
return 'An error occurred while generating the summary.';
}
}
// Function to summarize an individual file
async function summarizeFile(file) {
try {
// Only process supported file types
const supportedTypes = ['application/pdf', 'text/csv'];
if (!supportedTypes.includes(file.mimetype)) {
return `:information_source: File type ${file.mimetype} is not supported for summarization.`;
}
// Download and summarize the file
const fileContent = await downloadFile(file.url_private, file.mimetype);
return await summarizeDocument(fileContent);
} catch (error) {
console.error('Error summarizing file:', error);
return 'An error occurred while summarizing the file.';
}
}
function getOldestTimestamp(numDays) {
const date = new Date();
date.setDate(date.getDate() - numDays);
return Math.floor(date.getTime() / 1000); // Convert to Unix timestamp
}
// Function to fetch messages from a channel
async function fetchChannelMessages(app, channelId, numDays) {
try {
const oldestTimestamp = getOldestTimestamp(numDays);
let messages = [];
let hasMore = true;
let cursor;
while (hasMore) {
const result = await app.client.conversations.history({
channel: channelId,
oldest: oldestTimestamp,
limit: 200, // Max limit per API call
cursor: cursor,
});
messages = messages.concat(result.messages || []);
hasMore = result.has_more;
cursor = result.response_metadata?.next_cursor;
}
// Filter messages to ensure they are within the desired time range
messages = messages.filter(
(msg) => parseFloat(msg.ts) >= oldestTimestamp
);
return messages.map((msg) => msg.text || '').reverse(); // Reverse to chronological order
} catch (error) {
console.error('Error fetching channel messages:', error);
// Check for the 'not_in_channel' error
if (error.data?.error === 'not_in_channel') {
throw new Error(
`It looks like I'm not a member of this channel. Please invite me by typing: \`/invite @briefops\`.`
);
}
throw new Error('Failed to fetch messages due to an unexpected error.');
}
}
// Function to fetch messages and files from a thread
async function fetchThreadMessagesAndFiles(app, channelId, threadTs) {
try {
let messages = [];
let files = [];
let hasMore = true;
let cursor;
while (hasMore) {
const result = await app.client.conversations.replies({
channel: channelId,
ts: threadTs,
limit: 200, // Max limit per API call
cursor: cursor,
});
result.messages.forEach((msg) => {
if (msg.text) messages.push(msg.text);
if (msg.files) {
files = files.concat(msg.files);
}
});
hasMore = result.has_more;
cursor = result.response_metadata?.next_cursor;
}
return { messages: messages.reverse(), files };
} catch (error) {
console.error('Error fetching thread messages and files:', error);
throw new Error('Failed to fetch messages and files due to an unexpected error.');
}
}
// Function to fetch messages from a thread
async function fetchThreadMessages(app, channelId, threadTs) {
try {
let messages = [];
let hasMore = true;
let cursor;
while (hasMore) {
const result = await app.client.conversations.replies({
channel: channelId,
ts: threadTs,
limit: 200,
cursor: cursor,
});
messages = messages.concat(result.messages || []);
hasMore = result.has_more;
cursor = result.response_metadata?.next_cursor;
}
return messages;
} catch (error) {
console.error('Error fetching thread messages:', error);
throw new Error('Failed to fetch thread messages due to an unexpected error.');
}
}
// Function to analyze messages for content
function analyzeMessagesForContent(messages) {
return utils.analyzeMessagesForContent(messages);
}
// Function to check usage limits
async function checkUsageLimits(userId, numDays) {
try {
const docRef = db.collection('usage').doc(userId);
const doc = await docRef.get();
const today = new Date().toISOString().slice(0, 10);
// Check for maximum days allowed
if (FREE_TIER_MAX_DAYS > 0 && numDays > FREE_TIER_MAX_DAYS) {
throw new Error(`Free users can summarize up to ${FREE_TIER_MAX_DAYS} days.`);
}
// Check for daily summary limit
if (FREE_TIER_DAILY_LIMIT > 0) {
if (!doc.exists) {
// No usage data, allow usage
return true;
} else {
const data = doc.data();
if (data.date === today && data.count >= FREE_TIER_DAILY_LIMIT) {
// Exceeded daily limit
return false;
} else {
// Allow usage
return true;
}
}
} else {
// No daily limit imposed
return true;
}
} catch (error) {
console.error('Error in checkUsageLimits:', error);
throw error; // Rethrow the error to be caught in the command handler
}
}
// Function to increment usage count
async function incrementUsageCount(userId) {
if (FREE_TIER_DAILY_LIMIT > 0) {
const docRef = db.collection('usage').doc(userId);
const today = new Date().toISOString().slice(0, 10);
await db.runTransaction(async (t) => {
const doc = await t.get(docRef);
if (!doc.exists || doc.data().date !== today) {
t.set(docRef, { date: today, count: 1 });
} else {
const newCount = doc.data().count + 1;
t.update(docRef, { count: newCount });
}
});
}
// If no daily limit, no need to increment usage count
}
};