Skip to content

Commit 90ffa71

Browse files
authored
fix: group !reset and better code & node-chatgpt-api 1.37.3 (#159)
* fix: group !reset and better code * chore: node-chatgpt-api 1.37.3
1 parent 3f6ec02 commit 90ffa71

File tree

5 files changed

+110
-71
lines changed

5 files changed

+110
-71
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@ffmpeg/ffmpeg": "^0.11.6",
2626
"@keyv/sqlite": "^3.6.5",
2727
"@types/qrcode-terminal": "^0.12.0",
28-
"@waylaidwanderer/chatgpt-api": "1.37.0",
28+
"@waylaidwanderer/chatgpt-api": "1.37.3",
2929
"common-tags": "^1.8.2",
3030
"crypto": "^1.0.1",
3131
"fluent-ffmpeg": "^2.1.2",

src/handlers/command.ts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,60 @@ export async function handleCommand(
3535
break;
3636
case "!reset":
3737
if (chat.isGroup) {
38-
const admins = (chat as GroupChat).participants.map((user) => {
39-
if (user.isAdmin) return user.id._serialized;
40-
});
41-
42-
if (admins.includes(message.author)) {
43-
console.log("admins:", admins);
44-
let onGoingConversation = await idsCache.get(chat.id._serialized);
45-
const conversationData = JSON.parse(onGoingConversation);
46-
let jailbreakId = conversationData.jailbreakConversationId;
47-
console.log("jailbreakId:", jailbreakId);
48-
await sydney.conversationsCache.delete(jailbreakId);
49-
await idsCache.delete(chat.id._serialized);
50-
await message.reply("Conversation history reset.");
38+
const { participants, id } = chat as GroupChat;
39+
const admins = participants.filter((user) => user.isAdmin);
40+
41+
const authorId = message.author;
42+
43+
if (
44+
authorId &&
45+
admins.some((admin) => admin.id._serialized === authorId)
46+
) {
47+
try {
48+
const onGoingConversation = await idsCache.get(id._serialized);
49+
const conversationData = JSON.parse(onGoingConversation);
50+
51+
if (conversationData && conversationData.jailbreakConversationId) {
52+
const jailbreakId = conversationData.jailbreakConversationId;
53+
54+
await Promise.all([
55+
sydney.conversationsCache.delete(jailbreakId),
56+
idsCache.delete(id._serialized),
57+
]);
58+
59+
await message.reply("Conversation history reset.");
60+
} else {
61+
await message.reply("Invalid conversation data.");
62+
}
63+
} catch (error) {
64+
console.error("Cache operation or parsing failed:", error);
65+
}
5166
} else {
5267
await message.reply("You are not allowed to perform this command.");
5368
}
54-
break;
5569
} else {
56-
let onGoingConversation = await idsCache.get(chat.id._serialized);
57-
const conversationData = JSON.parse(onGoingConversation);
58-
let jailbreakId = conversationData.jailbreakConversationId;
59-
await idsCache.delete(chat.id._serialized);
60-
await sydney.conversationsCache.delete(jailbreakId);
61-
await message.reply("Conversation history reset.");
62-
break;
70+
try {
71+
const onGoingConversation = await idsCache.get(chat.id._serialized);
72+
const conversationData = JSON.parse(onGoingConversation);
73+
74+
if (conversationData && conversationData.jailbreakConversationId) {
75+
const jailbreakId = conversationData.jailbreakConversationId;
76+
77+
await Promise.all([
78+
sydney.conversationsCache.delete(jailbreakId),
79+
idsCache.delete(chat.id._serialized),
80+
]);
81+
82+
await message.reply("Conversation history reset.");
83+
} else {
84+
await message.reply("Invalid conversation data.");
85+
}
86+
} catch (error) {
87+
console.error("Cache operation or parsing failed:", error);
88+
}
6389
}
90+
break;
91+
6492
case "!pending":
6593
const pendingPrompts = promptTracker.listPendingPrompts(chat);
6694

src/handlers/message.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,24 @@ async function upsertLastWAreplyId(chatId: string, lastWAreplyId: string) {
4949

5050
export async function handleGroupMessage(message: Message) {
5151
const chat = await message.getChat();
52-
53-
//const mentions = await message.getMentions(); // Stoped working for some reason (I think it's because the number field is empty)
54-
//const botMention = mentions.filter((mention) => mention.isMe).pop();
5552
const quotedMessage = await message.getQuotedMessage();
5653

5754
let isInThread = false;
58-
const OnGoingConversation = await sydney.conversationsCache.get(
59-
chat.id._serialized
60-
);
61-
if (OnGoingConversation)
62-
isInThread = quotedMessage
63-
? quotedMessage.id._serialized === OnGoingConversation.lastWAreplyId
64-
: false;
65-
66-
const mentionedIds = message.mentionedIds; // Temporary logic so that you can talk with Sydney with @Sydney in a group
55+
56+
const conversationDataJSON = await idsCache.get(chat.id._serialized);
57+
if (conversationDataJSON) {
58+
const conversationData = JSON.parse(conversationDataJSON);
59+
const onGoingConversation = await sydney.conversationsCache.get(
60+
conversationData.jailbreakConversationId
61+
);
62+
63+
if (onGoingConversation)
64+
isInThread = quotedMessage
65+
? quotedMessage.id._serialized === onGoingConversation.lastWAreplyId
66+
: false;
67+
}
68+
69+
const mentionedIds = message.mentionedIds;
6770
const toId = message.to;
6871
let isMentionedInTo = false;
6972
mentionedIds.forEach((mentionedId) => {
@@ -74,8 +77,6 @@ export async function handleGroupMessage(message: Message) {
7477

7578
if (!isMentionedInTo && !isInThread) return false;
7679

77-
//replaceMentions(message, mentions, botMention);
78-
7980
return true;
8081
}
8182

@@ -234,24 +235,19 @@ async function askSydney(prompt: string, chatId: string, context: string) {
234235
const onGoingConversation = await idsCache.get(chatId);
235236

236237
if (onGoingConversation) {
237-
const conversationData = JSON.parse(onGoingConversation);
238-
options.parentMessageId = conversationData.messageId;
239-
options.jailbreakConversationId = conversationData.jailbreakConversationId;
240-
console.log(
241-
"options.jailbreakConversationId:",
242-
options.jailbreakConversationId
243-
);
238+
const { messageId, jailbreakConversationId } =
239+
JSON.parse(onGoingConversation);
240+
options.parentMessageId = messageId;
241+
options.jailbreakConversationId = jailbreakConversationId;
244242
}
245243

246244
const response: SydneyResponse = await sydney.sendMessage(prompt, options);
247-
await idsCache.set(
248-
chatId,
249-
JSON.stringify({
250-
jailbreakConversationId: response.jailbreakConversationId,
251-
messageId: response.messageId,
252-
})
253-
);
254-
255-
//console.dir(response, { depth: null });
245+
246+
const newConversationData = {
247+
jailbreakConversationId: response.jailbreakConversationId,
248+
messageId: response.messageId,
249+
};
250+
await idsCache.set(chatId, JSON.stringify(newConversationData));
251+
256252
return response;
257253
}

src/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import scheduler from "node-schedule";
22
import { config } from "./config";
33
import WAWebJS, { Message } from "whatsapp-web.js";
4+
import { boolean } from "zod";
45

56
interface IOptions {
67
toneStyle: (typeof config.VALID_TONES)[number];
78
systemMessage?: string;
8-
jailbreakConversationId?: any; //FIX IT LATER
9+
jailbreakConversationId?: string | boolean;
910
parentMessageId?: string;
1011
context?: string;
1112
onProgress?: (token: string) => void;

yarn.lock

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@
229229
dependencies:
230230
"@types/node" "*"
231231

232-
"@waylaidwanderer/[email protected].0":
233-
version "1.37.0"
234-
resolved "https://registry.yarnpkg.com/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.37.0.tgz#dd9e6bcd1dd1da0c6f1811dddd6dbd86e911e2ef"
235-
integrity sha512-fJfNvfZliuzRlTEY2kb3u6psfAaFI88YSU1RJ6J865BRk90Km3k62br/npCGLCtbREklTR18bMYhNXVM/BuFow==
232+
"@waylaidwanderer/[email protected].3":
233+
version "1.37.3"
234+
resolved "https://registry.yarnpkg.com/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.37.3.tgz#674c41c18004ef1324730eff99240b7eeabda77a"
235+
integrity sha512-ODonzVgIl9ZAliknnDr1r/yyHePlc9HIcUgLattuTfuNFYJ7IFcuVov3DWdQICa2jzxbtdp977biQA6SYEPM2A==
236236
dependencies:
237237
"@dqbd/tiktoken" "^1.0.2"
238238
"@fastify/cors" "^8.2.0"
@@ -249,7 +249,7 @@
249249
inquirer-autocomplete-prompt "^3.0.0"
250250
keyv "^4.5.2"
251251
keyv-file "^0.2.0"
252-
ora "^6.1.2"
252+
ora "^7.0.1"
253253
undici "^5.20.0"
254254
ws "^8.12.0"
255255

@@ -663,7 +663,7 @@ chalk@^4.1.0:
663663
ansi-styles "^4.1.0"
664664
supports-color "^7.1.0"
665665

666-
chalk@^5.0.0, chalk@^5.2.0:
666+
chalk@^5.0.0, chalk@^5.2.0, chalk@^5.3.0:
667667
version "5.3.0"
668668
resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.3.0.tgz#67c20a7ebef70e7f3970a01f90fa210cb6860385"
669669
integrity sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==
@@ -727,7 +727,7 @@ cli-cursor@^4.0.0:
727727
dependencies:
728728
restore-cursor "^4.0.0"
729729

730-
cli-spinners@^2.5.0, cli-spinners@^2.6.1:
730+
cli-spinners@^2.5.0, cli-spinners@^2.9.0:
731731
version "2.9.0"
732732
resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-2.9.0.tgz#5881d0ad96381e117bbe07ad91f2008fe6ffd8db"
733733
integrity sha512-4/aL9X3Wh0yiMQlE+eeRhWP6vclO3QRtw1JHKIT0FFUs5FjpFmESqtMvYZ0+lbzBw900b95mS0hohy+qn2VK/g==
@@ -937,6 +937,11 @@ eastasianwidth@^0.2.0:
937937
resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb"
938938
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==
939939

940+
emoji-regex@^10.2.1:
941+
version "10.2.1"
942+
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-10.2.1.tgz#a41c330d957191efd3d9dfe6e1e8e1e9ab048b3f"
943+
integrity sha512-97g6QgOk8zlDRdgq1WxwgTMgEWGVAQvB5Fdpgc1MkNy56la5SKP9GsMXKDOdqwn90/41a8yPwIGk1Y6WVbeMQA==
944+
940945
emoji-regex@^8.0.0:
941946
version "8.0.0"
942947
resolved "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz"
@@ -1530,7 +1535,7 @@ is-unicode-supported@^0.1.0:
15301535
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz#3f26c76a809593b52bfa2ecb5710ed2779b522a7"
15311536
integrity sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==
15321537

1533-
is-unicode-supported@^1.1.0, is-unicode-supported@^1.2.0:
1538+
is-unicode-supported@^1.1.0, is-unicode-supported@^1.2.0, is-unicode-supported@^1.3.0:
15341539
version "1.3.0"
15351540
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz#d824984b616c292a2e198207d4a609983842f714"
15361541
integrity sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ==
@@ -2080,20 +2085,20 @@ ora@^5.4.1:
20802085
strip-ansi "^6.0.0"
20812086
wcwidth "^1.0.1"
20822087

2083-
ora@^6.1.2:
2084-
version "6.3.1"
2085-
resolved "https://registry.yarnpkg.com/ora/-/ora-6.3.1.tgz#a4e9e5c2cf5ee73c259e8b410273e706a2ad3ed6"
2086-
integrity sha512-ERAyNnZOfqM+Ao3RAvIXkYh5joP220yf59gVe2X/cI6SiCxIdi4c9HZKZD8R6q/RDXEje1THBju6iExiSsgJaQ==
2088+
ora@^7.0.1:
2089+
version "7.0.1"
2090+
resolved "https://registry.yarnpkg.com/ora/-/ora-7.0.1.tgz#cdd530ecd865fe39e451a0e7697865669cb11930"
2091+
integrity sha512-0TUxTiFJWv+JnjWm4o9yvuskpEJLXTcng8MJuKd+SzAzp2o+OP3HWqNhB4OdJRt1Vsd9/mR0oyaEYlOnL7XIRw==
20872092
dependencies:
2088-
chalk "^5.0.0"
2093+
chalk "^5.3.0"
20892094
cli-cursor "^4.0.0"
2090-
cli-spinners "^2.6.1"
2095+
cli-spinners "^2.9.0"
20912096
is-interactive "^2.0.0"
2092-
is-unicode-supported "^1.1.0"
2097+
is-unicode-supported "^1.3.0"
20932098
log-symbols "^5.1.0"
20942099
stdin-discarder "^0.1.0"
2095-
strip-ansi "^7.0.1"
2096-
wcwidth "^1.0.1"
2100+
string-width "^6.1.0"
2101+
strip-ansi "^7.1.0"
20972102

20982103
os-tmpdir@~1.0.2:
20992104
version "1.0.2"
@@ -2649,6 +2654,15 @@ string-width@^5.0.1, string-width@^5.1.2:
26492654
emoji-regex "^9.2.2"
26502655
strip-ansi "^7.0.1"
26512656

2657+
string-width@^6.1.0:
2658+
version "6.1.0"
2659+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-6.1.0.tgz#96488d6ed23f9ad5d82d13522af9e4c4c3fd7518"
2660+
integrity sha512-k01swCJAgQmuADB0YIc+7TuatfNvTBVOoaUWJjTB9R4VJzR5vNWzf5t42ESVZFPS8xTySF7CAdV4t/aaIm3UnQ==
2661+
dependencies:
2662+
eastasianwidth "^0.2.0"
2663+
emoji-regex "^10.2.1"
2664+
strip-ansi "^7.0.1"
2665+
26522666
string_decoder@^1.1.1, string_decoder@^1.3.0:
26532667
version "1.3.0"
26542668
resolved "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz"
@@ -2670,7 +2684,7 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
26702684
dependencies:
26712685
ansi-regex "^5.0.1"
26722686

2673-
strip-ansi@^7.0.1:
2687+
strip-ansi@^7.0.1, strip-ansi@^7.1.0:
26742688
version "7.1.0"
26752689
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
26762690
integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==

0 commit comments

Comments
 (0)