nodebb-plugin-openai 1.4.2 → 1.4.3
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/library.js +22 -17
- package/package.json +1 -1
package/library.js
CHANGED
|
@@ -125,9 +125,13 @@ plugin.actionMessagingSave = async function (hookData) {
|
|
|
125
125
|
return;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
// Check actual room membership directly against the database.
|
|
129
|
+
// messaging.isUserInRoom() runs filter:messaging.isUserInRoom, which
|
|
130
|
+
// moderation plugins use to grant virtual access to every room — that
|
|
131
|
+
// would make the bot react to every private chat on the forum.
|
|
128
132
|
const [roomData, inRoom] = await Promise.all([
|
|
129
133
|
messaging.getRoomData(roomId),
|
|
130
|
-
|
|
134
|
+
db.isSortedSetMember(`chat:room:${roomId}:uids`, chatgptUid),
|
|
131
135
|
]);
|
|
132
136
|
|
|
133
137
|
if (!roomData || !inRoom) {
|
|
@@ -145,24 +149,19 @@ plugin.actionMessagingSave = async function (hookData) {
|
|
|
145
149
|
}
|
|
146
150
|
let conversation = [{ role: 'user', content: message.content }];
|
|
147
151
|
if (isPrivate) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
// dont allow chats to get too long
|
|
151
|
-
if (mids.length > 20) {
|
|
152
|
-
await api.chats.post({ uid: chatgptUid, session: {} }, {
|
|
153
|
-
roomId,
|
|
154
|
-
message: 'Conversation too long, please start a new chat',
|
|
155
|
-
toMid: message.mid,
|
|
156
|
-
});
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
152
|
+
// keep context bounded to the 20 most recent messages
|
|
153
|
+
const mids = await getMessageIds(roomId, chatgptUid, 0, 19);
|
|
159
154
|
let messages = await messaging.getMessagesFields(mids, ['fromuid', 'content', 'system']);
|
|
160
|
-
messages = messages.filter(m => m && !m.system);
|
|
155
|
+
messages = messages.filter(m => m && !m.system && m.content);
|
|
161
156
|
conversation = messages.map(
|
|
162
|
-
msg => ({ role: msg.fromuid === chatgptUid ? 'assistant' : 'user', content: msg.content })
|
|
157
|
+
msg => ({ role: parseInt(msg.fromuid, 10) === parseInt(chatgptUid, 10) ? 'assistant' : 'user', content: msg.content })
|
|
163
158
|
);
|
|
164
159
|
}
|
|
165
160
|
|
|
161
|
+
if (!conversation.length) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
166
165
|
const response = await chatComplete(conversation);
|
|
167
166
|
|
|
168
167
|
if (response) {
|
|
@@ -227,17 +226,23 @@ async function checkGroupMembership(uid, settings, silent) {
|
|
|
227
226
|
return memberOfAny;
|
|
228
227
|
}
|
|
229
228
|
|
|
229
|
+
// Returns the newest messages (like core's getMessageIds), in chronological order
|
|
230
230
|
async function getMessageIds(roomId, uid, start, stop) {
|
|
231
231
|
const isPublic = await db.getObjectField(`chat:room:${roomId}`, 'public');
|
|
232
232
|
if (parseInt(isPublic, 10) === 1) {
|
|
233
|
-
|
|
233
|
+
const mids = await db.getSortedSetRevRange(
|
|
234
234
|
`chat:room:${roomId}:mids`, start, stop
|
|
235
235
|
);
|
|
236
|
+
return mids.reverse();
|
|
236
237
|
}
|
|
237
238
|
const userjoinTimestamp = await db.sortedSetScore(`chat:room:${roomId}:uids`, uid);
|
|
238
|
-
|
|
239
|
-
|
|
239
|
+
if (userjoinTimestamp === null) {
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
242
|
+
const mids = await db.getSortedSetRevRangeByScore(
|
|
243
|
+
`chat:room:${roomId}:mids`, start, stop === -1 ? -1 : stop - start + 1, '+inf', userjoinTimestamp
|
|
240
244
|
);
|
|
245
|
+
return mids.reverse();
|
|
241
246
|
}
|
|
242
247
|
|
|
243
248
|
async function chatComplete(messages) {
|