nodebb-plugin-openai 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/summary.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const db = require.main.require('./src/database');
3
4
  const batch = require.main.require('./src/batch');
4
5
  const topics = require.main.require('./src/topics');
5
6
  const posts = require.main.require('./src/posts');
@@ -37,6 +38,9 @@ function chunkPosts(posts, maxTokensPerChunk = 3000) {
37
38
 
38
39
  async function summarizeChunk(chunk, openai, settings) {
39
40
  const threadText = formatPosts(chunk);
41
+ if (!threadText) {
42
+ return '';
43
+ }
40
44
  const response = await openai.chat.completions.create({
41
45
  model: settings.model || 'gpt-3.5-turbo',
42
46
  messages: [
@@ -63,8 +67,8 @@ exports.summarizeTopic = async function(tid, openai, settings) {
63
67
  const chunkSummaries = [];
64
68
 
65
69
  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);
68
72
  const missingUids = [];
69
73
  postData.forEach((p) => {
70
74
  if (!userMap.hasOwnProperty(p.uid)) {
@@ -93,6 +97,9 @@ exports.summarizeTopic = async function(tid, openai, settings) {
93
97
  }
94
98
  // Final summary from all summaries
95
99
  const finalInput = chunkSummaries.join("\n\n");
100
+ if (!finalInput) {
101
+ return '';
102
+ }
96
103
  const finalResponse = await openai.chat.completions.create({
97
104
  model: settings.model || 'gpt-3.5-turbo',
98
105
  messages: [
@@ -109,4 +116,8 @@ exports.summarizeTopic = async function(tid, openai, settings) {
109
116
  });
110
117
 
111
118
  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']);
112
123
  };
package/library.js CHANGED
@@ -10,7 +10,6 @@ const controllers = require('./lib/controllers');
10
10
  const routeHelpers = require.main.require('./src/routes/helpers');
11
11
  const socketHelpers = require.main.require('./src/socket.io/helpers');
12
12
  const topics = require.main.require('./src/topics');
13
- const posts = require.main.require('./src/posts');
14
13
  const user = require.main.require('./src/user');
15
14
  const messaging = require.main.require('./src/messaging');
16
15
  const api = require.main.require('./src/api');
@@ -277,6 +276,38 @@ plugin.filterTopicThreadTools = async (hookData) => {
277
276
  return hookData;
278
277
  };
279
278
 
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
+
280
311
  socketPlugins.openai = {};
281
312
 
282
313
  socketPlugins.openai.summarizeTopic = async function (socket, data) {
@@ -289,5 +320,11 @@ socketPlugins.openai.summarizeTopic = async function (socket, data) {
289
320
  return;
290
321
  }
291
322
 
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;
293
330
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-openai",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "A NodeBB OpenAI plugin",
5
5
  "main": "library.js",
6
6
  "repository": {
package/plugin.json CHANGED
@@ -7,7 +7,15 @@
7
7
  { "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
8
8
  { "hook": "action:mentions.notify", "method": "actionMentionsNotify" },
9
9
  { "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" }
11
19
  ],
12
20
  "scripts": [
13
21
  "./public/lib/main.js"