Skip to content

Commit

Permalink
fix: group !reset and better code & node-chatgpt-api 1.37.3 (#159)
Browse files Browse the repository at this point in the history
* fix: group !reset and better code

* chore: node-chatgpt-api 1.37.3
  • Loading branch information
Luisotee authored Aug 29, 2023
1 parent 3f6ec02 commit 90ffa71
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@ffmpeg/ffmpeg": "^0.11.6",
"@keyv/sqlite": "^3.6.5",
"@types/qrcode-terminal": "^0.12.0",
"@waylaidwanderer/chatgpt-api": "1.37.0",
"@waylaidwanderer/chatgpt-api": "1.37.3",
"common-tags": "^1.8.2",
"crypto": "^1.0.1",
"fluent-ffmpeg": "^2.1.2",
Expand Down
70 changes: 49 additions & 21 deletions src/handlers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,60 @@ export async function handleCommand(
break;
case "!reset":
if (chat.isGroup) {
const admins = (chat as GroupChat).participants.map((user) => {
if (user.isAdmin) return user.id._serialized;
});

if (admins.includes(message.author)) {
console.log("admins:", admins);
let onGoingConversation = await idsCache.get(chat.id._serialized);
const conversationData = JSON.parse(onGoingConversation);
let jailbreakId = conversationData.jailbreakConversationId;
console.log("jailbreakId:", jailbreakId);
await sydney.conversationsCache.delete(jailbreakId);
await idsCache.delete(chat.id._serialized);
await message.reply("Conversation history reset.");
const { participants, id } = chat as GroupChat;
const admins = participants.filter((user) => user.isAdmin);

const authorId = message.author;

if (
authorId &&
admins.some((admin) => admin.id._serialized === authorId)
) {
try {
const onGoingConversation = await idsCache.get(id._serialized);
const conversationData = JSON.parse(onGoingConversation);

if (conversationData && conversationData.jailbreakConversationId) {
const jailbreakId = conversationData.jailbreakConversationId;

await Promise.all([
sydney.conversationsCache.delete(jailbreakId),
idsCache.delete(id._serialized),
]);

await message.reply("Conversation history reset.");
} else {
await message.reply("Invalid conversation data.");
}
} catch (error) {
console.error("Cache operation or parsing failed:", error);
}
} else {
await message.reply("You are not allowed to perform this command.");
}
break;
} else {
let onGoingConversation = await idsCache.get(chat.id._serialized);
const conversationData = JSON.parse(onGoingConversation);
let jailbreakId = conversationData.jailbreakConversationId;
await idsCache.delete(chat.id._serialized);
await sydney.conversationsCache.delete(jailbreakId);
await message.reply("Conversation history reset.");
break;
try {
const onGoingConversation = await idsCache.get(chat.id._serialized);
const conversationData = JSON.parse(onGoingConversation);

if (conversationData && conversationData.jailbreakConversationId) {
const jailbreakId = conversationData.jailbreakConversationId;

await Promise.all([
sydney.conversationsCache.delete(jailbreakId),
idsCache.delete(chat.id._serialized),
]);

await message.reply("Conversation history reset.");
} else {
await message.reply("Invalid conversation data.");
}
} catch (error) {
console.error("Cache operation or parsing failed:", error);
}
}
break;

case "!pending":
const pendingPrompts = promptTracker.listPendingPrompts(chat);

Expand Down
56 changes: 26 additions & 30 deletions src/handlers/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,24 @@ async function upsertLastWAreplyId(chatId: string, lastWAreplyId: string) {

export async function handleGroupMessage(message: Message) {
const chat = await message.getChat();

//const mentions = await message.getMentions(); // Stoped working for some reason (I think it's because the number field is empty)
//const botMention = mentions.filter((mention) => mention.isMe).pop();
const quotedMessage = await message.getQuotedMessage();

let isInThread = false;
const OnGoingConversation = await sydney.conversationsCache.get(
chat.id._serialized
);
if (OnGoingConversation)
isInThread = quotedMessage
? quotedMessage.id._serialized === OnGoingConversation.lastWAreplyId
: false;

const mentionedIds = message.mentionedIds; // Temporary logic so that you can talk with Sydney with @Sydney in a group

const conversationDataJSON = await idsCache.get(chat.id._serialized);
if (conversationDataJSON) {
const conversationData = JSON.parse(conversationDataJSON);
const onGoingConversation = await sydney.conversationsCache.get(
conversationData.jailbreakConversationId
);

if (onGoingConversation)
isInThread = quotedMessage
? quotedMessage.id._serialized === onGoingConversation.lastWAreplyId
: false;
}

const mentionedIds = message.mentionedIds;
const toId = message.to;
let isMentionedInTo = false;
mentionedIds.forEach((mentionedId) => {
Expand All @@ -74,8 +77,6 @@ export async function handleGroupMessage(message: Message) {

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

//replaceMentions(message, mentions, botMention);

return true;
}

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

if (onGoingConversation) {
const conversationData = JSON.parse(onGoingConversation);
options.parentMessageId = conversationData.messageId;
options.jailbreakConversationId = conversationData.jailbreakConversationId;
console.log(
"options.jailbreakConversationId:",
options.jailbreakConversationId
);
const { messageId, jailbreakConversationId } =
JSON.parse(onGoingConversation);
options.parentMessageId = messageId;
options.jailbreakConversationId = jailbreakConversationId;
}

const response: SydneyResponse = await sydney.sendMessage(prompt, options);
await idsCache.set(
chatId,
JSON.stringify({
jailbreakConversationId: response.jailbreakConversationId,
messageId: response.messageId,
})
);

//console.dir(response, { depth: null });

const newConversationData = {
jailbreakConversationId: response.jailbreakConversationId,
messageId: response.messageId,
};
await idsCache.set(chatId, JSON.stringify(newConversationData));

return response;
}
3 changes: 2 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import scheduler from "node-schedule";
import { config } from "./config";
import WAWebJS, { Message } from "whatsapp-web.js";
import { boolean } from "zod";

interface IOptions {
toneStyle: (typeof config.VALID_TONES)[number];
systemMessage?: string;
jailbreakConversationId?: any; //FIX IT LATER
jailbreakConversationId?: string | boolean;
parentMessageId?: string;
context?: string;
onProgress?: (token: string) => void;
Expand Down
50 changes: 32 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@
dependencies:
"@types/node" "*"

"@waylaidwanderer/[email protected].0":
version "1.37.0"
resolved "https://registry.yarnpkg.com/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.37.0.tgz#dd9e6bcd1dd1da0c6f1811dddd6dbd86e911e2ef"
integrity sha512-fJfNvfZliuzRlTEY2kb3u6psfAaFI88YSU1RJ6J865BRk90Km3k62br/npCGLCtbREklTR18bMYhNXVM/BuFow==
"@waylaidwanderer/[email protected].3":
version "1.37.3"
resolved "https://registry.yarnpkg.com/@waylaidwanderer/chatgpt-api/-/chatgpt-api-1.37.3.tgz#674c41c18004ef1324730eff99240b7eeabda77a"
integrity sha512-ODonzVgIl9ZAliknnDr1r/yyHePlc9HIcUgLattuTfuNFYJ7IFcuVov3DWdQICa2jzxbtdp977biQA6SYEPM2A==
dependencies:
"@dqbd/tiktoken" "^1.0.2"
"@fastify/cors" "^8.2.0"
Expand All @@ -249,7 +249,7 @@
inquirer-autocomplete-prompt "^3.0.0"
keyv "^4.5.2"
keyv-file "^0.2.0"
ora "^6.1.2"
ora "^7.0.1"
undici "^5.20.0"
ws "^8.12.0"

Expand Down Expand Up @@ -663,7 +663,7 @@ chalk@^4.1.0:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^5.0.0, chalk@^5.2.0:
chalk@^5.0.0, chalk@^5.2.0, chalk@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.3.0.tgz#67c20a7ebef70e7f3970a01f90fa210cb6860385"
integrity sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==
Expand Down Expand Up @@ -727,7 +727,7 @@ cli-cursor@^4.0.0:
dependencies:
restore-cursor "^4.0.0"

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

emoji-regex@^10.2.1:
version "10.2.1"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-10.2.1.tgz#a41c330d957191efd3d9dfe6e1e8e1e9ab048b3f"
integrity sha512-97g6QgOk8zlDRdgq1WxwgTMgEWGVAQvB5Fdpgc1MkNy56la5SKP9GsMXKDOdqwn90/41a8yPwIGk1Y6WVbeMQA==

emoji-regex@^8.0.0:
version "8.0.0"
resolved "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz"
Expand Down Expand Up @@ -1530,7 +1535,7 @@ is-unicode-supported@^0.1.0:
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz#3f26c76a809593b52bfa2ecb5710ed2779b522a7"
integrity sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==

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

ora@^6.1.2:
version "6.3.1"
resolved "https://registry.yarnpkg.com/ora/-/ora-6.3.1.tgz#a4e9e5c2cf5ee73c259e8b410273e706a2ad3ed6"
integrity sha512-ERAyNnZOfqM+Ao3RAvIXkYh5joP220yf59gVe2X/cI6SiCxIdi4c9HZKZD8R6q/RDXEje1THBju6iExiSsgJaQ==
ora@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/ora/-/ora-7.0.1.tgz#cdd530ecd865fe39e451a0e7697865669cb11930"
integrity sha512-0TUxTiFJWv+JnjWm4o9yvuskpEJLXTcng8MJuKd+SzAzp2o+OP3HWqNhB4OdJRt1Vsd9/mR0oyaEYlOnL7XIRw==
dependencies:
chalk "^5.0.0"
chalk "^5.3.0"
cli-cursor "^4.0.0"
cli-spinners "^2.6.1"
cli-spinners "^2.9.0"
is-interactive "^2.0.0"
is-unicode-supported "^1.1.0"
is-unicode-supported "^1.3.0"
log-symbols "^5.1.0"
stdin-discarder "^0.1.0"
strip-ansi "^7.0.1"
wcwidth "^1.0.1"
string-width "^6.1.0"
strip-ansi "^7.1.0"

os-tmpdir@~1.0.2:
version "1.0.2"
Expand Down Expand Up @@ -2649,6 +2654,15 @@ string-width@^5.0.1, string-width@^5.1.2:
emoji-regex "^9.2.2"
strip-ansi "^7.0.1"

string-width@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-6.1.0.tgz#96488d6ed23f9ad5d82d13522af9e4c4c3fd7518"
integrity sha512-k01swCJAgQmuADB0YIc+7TuatfNvTBVOoaUWJjTB9R4VJzR5vNWzf5t42ESVZFPS8xTySF7CAdV4t/aaIm3UnQ==
dependencies:
eastasianwidth "^0.2.0"
emoji-regex "^10.2.1"
strip-ansi "^7.0.1"

string_decoder@^1.1.1, string_decoder@^1.3.0:
version "1.3.0"
resolved "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz"
Expand All @@ -2670,7 +2684,7 @@ strip-ansi@^6.0.0, strip-ansi@^6.0.1:
dependencies:
ansi-regex "^5.0.1"

strip-ansi@^7.0.1:
strip-ansi@^7.0.1, strip-ansi@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
integrity sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==
Expand Down

0 comments on commit 90ffa71

Please sign in to comment.