Skip to content

Commit 85cbaf0

Browse files
committed
refactor: cache summary in topic hash
remove summary on new reply, edit, post purge/restore/delete/move and change owner
1 parent 0cdd7ac commit 85cbaf0

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

lib/summary.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const db = require.main.require('./src/database');
34
const batch = require.main.require('./src/batch');
45
const topics = require.main.require('./src/topics');
56
const posts = require.main.require('./src/posts');
@@ -37,6 +38,9 @@ function chunkPosts(posts, maxTokensPerChunk = 3000) {
3738

3839
async function summarizeChunk(chunk, openai, settings) {
3940
const threadText = formatPosts(chunk);
41+
if (!threadText) {
42+
return '';
43+
}
4044
const response = await openai.chat.completions.create({
4145
model: settings.model || 'gpt-3.5-turbo',
4246
messages: [
@@ -63,8 +67,8 @@ exports.summarizeTopic = async function(tid, openai, settings) {
6367
const chunkSummaries = [];
6468

6569
await batch.processArray(allPids, async function (pids) {
66-
const postData = await posts.getPostsFields(pids, ['uid', 'content']);
67-
70+
let postData = await posts.getPostsFields(pids, ['uid', 'content', 'deleted']);
71+
postData = postData.filter((p) => !p.deleted);
6872
const missingUids = [];
6973
postData.forEach((p) => {
7074
if (!userMap.hasOwnProperty(p.uid)) {
@@ -93,6 +97,9 @@ exports.summarizeTopic = async function(tid, openai, settings) {
9397
}
9498
// Final summary from all summaries
9599
const finalInput = chunkSummaries.join("\n\n");
100+
if (!finalInput) {
101+
return '';
102+
}
96103
const finalResponse = await openai.chat.completions.create({
97104
model: settings.model || 'gpt-3.5-turbo',
98105
messages: [
@@ -109,4 +116,8 @@ exports.summarizeTopic = async function(tid, openai, settings) {
109116
});
110117

111118
return finalResponse.choices[0].message.content.trim();
119+
};
120+
121+
exports.clearTopicSummary = async function(tids) {
122+
await db.deleteObjectFields(tids.map((tid) => `topic:${tid}`), ['openai:summary']);
112123
};

library.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const controllers = require('./lib/controllers');
1010
const routeHelpers = require.main.require('./src/routes/helpers');
1111
const socketHelpers = require.main.require('./src/socket.io/helpers');
1212
const topics = require.main.require('./src/topics');
13-
const posts = require.main.require('./src/posts');
1413
const user = require.main.require('./src/user');
1514
const messaging = require.main.require('./src/messaging');
1615
const api = require.main.require('./src/api');
@@ -277,6 +276,38 @@ plugin.filterTopicThreadTools = async (hookData) => {
277276
return hookData;
278277
};
279278

279+
plugin.actionTopicReply = async (hookData) => {
280+
await summary.clearTopicSummary([hookData.post.tid]);
281+
};
282+
283+
plugin.actionPostEdit = async (hookData) => {
284+
await summary.clearTopicSummary([hookData.post.tid]);
285+
};
286+
287+
plugin.actionPostsPurge = async (hookData) => {
288+
const { posts } = hookData;
289+
const uniqTids = [...new Set(posts.map(p => p.tid))];
290+
await summary.clearTopicSummary(uniqTids);
291+
};
292+
293+
plugin.actionPostRestore = async (hookData) => {
294+
await summary.clearTopicSummary([hookData.post.tid]);
295+
};
296+
297+
plugin.actionPostDelete = async (hookData) => {
298+
await summary.clearTopicSummary([hookData.post.tid]);
299+
};
300+
301+
plugin.actionPostMove = async (hookData) => {
302+
await summary.clearTopicSummary([hookData.post.tid, hookData.tid]);
303+
};
304+
305+
plugin.actionPostChangeOwner = async (hookData) => {
306+
const { posts } = hookData;
307+
const uniqTids = [...new Set(posts.map(p => p.tid))];
308+
await summary.clearTopicSummary(uniqTids);
309+
};
310+
280311
socketPlugins.openai = {};
281312

282313
socketPlugins.openai.summarizeTopic = async function (socket, data) {
@@ -289,5 +320,11 @@ socketPlugins.openai.summarizeTopic = async function (socket, data) {
289320
return;
290321
}
291322

292-
return summary.summarizeTopic(tid, openai, settings);
323+
let openaiSummary = await topics.getTopicField(tid, 'openai:summary');
324+
if (openaiSummary) {
325+
return openaiSummary;
326+
}
327+
openaiSummary = await summary.summarizeTopic(tid, openai, settings);
328+
await topics.setTopicField(tid, 'openai:summary', openaiSummary);
329+
return openaiSummary;
293330
};

plugin.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
{ "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
88
{ "hook": "action:mentions.notify", "method": "actionMentionsNotify" },
99
{ "hook": "action:messaging.save", "method": "actionMessagingSave" },
10-
{ "hook": "filter:topic.thread_tools", "method": "filterTopicThreadTools" }
10+
{ "hook": "filter:topic.thread_tools", "method": "filterTopicThreadTools" },
11+
12+
{ "hook": "action:topic.reply", "method": "actionTopicReply" },
13+
{ "hook": "action:post.edit", "method": "actionPostEdit" },
14+
{ "hook": "action:posts.purge", "method": "actionPostsPurge" },
15+
{ "hook": "action:post.restore", "method": "actionPostRestore" },
16+
{ "hook": "action:post.delete", "method": "actionPostDelete" },
17+
{ "hook": "action:post.move", "method": "actionPostMove" },
18+
{ "hook": "action:post.changeOwner", "method": "actionPostChangeOwner" }
1119
],
1220
"scripts": [
1321
"./public/lib/main.js"

0 commit comments

Comments
 (0)