Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add conversation reset command #212

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const ErrorCode2Message: Record<string, string> = {
"OpenAI 服务器繁忙,请稍后再试| The OpenAI server is busy, please try again later",
unknown: "未知错误,请看日志 | Error unknown, please see the log",
};
const Commands = ["/reset", "/help"] as const;
export class ChatGPTPoole {
chatGPTPools: Array<IChatGPTItem> | [] = [];
conversationsPool: Map<string, IConversationItem> = new Map();
Expand Down Expand Up @@ -129,6 +130,9 @@ export class ChatGPTPoole {
);
}
}
resetConversation(talkid: string) {
this.conversationsPool.delete(talkid);
}
async startPools() {
const sessionAccounts = config.chatGPTAccountPool.filter(
isAccountWithSessionToken
Expand Down Expand Up @@ -160,6 +164,17 @@ export class ChatGPTPoole {
}
console.log(`ChatGPTPools: ${this.chatGPTPools.length}`);
}
async command(cmd: typeof Commands[number], talkid: string): Promise<string> {
console.log(`command: ${cmd} talkid: ${talkid}`);
if (cmd == "/reset") {
this.resetConversation(talkid);
return "♻️ 已重置对话 | Conversation reset";
}
if (cmd == "/help") {
return `🧾 支持的命令|Support command:${Commands.join(",")}`;
}
return "❓ 未知命令|Unknow Command";
}
// Randome get chatgpt item form pool
get chatGPTAPI(): IChatGPTItem {
return this.chatGPTPools[
Expand All @@ -185,6 +200,13 @@ export class ChatGPTPoole {
}
// send message with talkid
async sendMessage(message: string, talkid: string): Promise<string> {
if (
Commands.some((cmd) => {
return message.startsWith(cmd);
})
) {
return this.command(message as typeof Commands[number], talkid);
}
const conversationItem = this.getConversation(talkid);
const { conversation, account } = conversationItem;
try {
Expand Down