diff --git a/README.md b/README.md index d805fa4..f161ff7 100644 --- a/README.md +++ b/README.md @@ -51,10 +51,14 @@ there are some commands that are hidden: > /unlock - Unlock bot functionalities with a password > /get - Retrieve config.json or cookies.json, respectively > /update - Update config.json or cookies.json, respectively +> /history_update - Force chat history update > /reset - Reload bot files > /cancel - Cancel current update action > ``` +> Be careful with /history_update. History will be recreated, removing ownership +> and moving the ownsership to admin1 (if multiple admins are present, to the first one) + In order to use inline queries, you need to enable them in [@BotFather](https://t.me/BotFather). For ease of use, use the placeholder ``` diff --git a/src/backend.py b/src/backend.py index 25c7b37..b964b19 100644 --- a/src/backend.py +++ b/src/backend.py @@ -84,31 +84,46 @@ async def run(self) -> None: ) ut.CONV["all"][self.cid][cur_conv][1] = self.text start = time.time() - async for final, resp in ut.CONV["all"][self.cid][cur_conv][ - 0 - ].ask_stream( - prompt=self.text, - conversation_style=getattr(ConversationStyle, db.style(self.cid)), - ): - current = time.time() - if current - start > 0.4 and not final: - text = SRCH_RESP.sub("", resp) - resp = GEN_RESP.sub("\\1", text) - resp = REF_INLINE_ST.sub("", resp) - resp = REF_ST.sub("", resp) - resp = resp.strip() - if resp: - text = ( - f"You: {html.escape(self.text)}\n\n" - f"Bing: {html.escape(resp)}" - ) - if not self.inline: - await ut.edit(self.edit, text) - else: - await ut.edit_inline(self.update, self.context, text) - start = current - if final: - self._response = resp + try: + async for final, resp in ut.CONV["all"][self.cid][cur_conv][ + 0 + ].ask_stream( + prompt=self.text, + conversation_style=getattr( + ConversationStyle, db.style(self.cid) + ), + ): + current = time.time() + if current - start > 0.4 and not final: + text = SRCH_RESP.sub("", resp) + resp = GEN_RESP.sub("\\1", text) + resp = REF_INLINE_ST.sub("", resp) + resp = REF_ST.sub("", resp) + resp = resp.strip() + if resp: + text = ( + f"You: {html.escape(self.text)}\n\n" + f"Bing: {html.escape(resp)}" + ) + if not self.inline: + await ut.edit(self.edit, text) + else: + await ut.edit_inline( + self.update, self.context, text + ) + start = current + if final: + self._response = resp + except KeyError: + # Conversation was removed from Bing but we still have a reference + await ut.CONV["all"][self.cid][cur_conv][0].close() + del ut.CONV["all"][self.cid][cur_conv] + ut.CONV["current"][self.cid] = "" + await self.edit.delete() + await ut.is_active_conversation(self.update, new=True) + query = BingAI(self.update, self.context) + await query.run() + return if not self.inline: ut.RUN[self.cid][cur_conv].remove(turn) ut.delete_job(self.context, job_name) diff --git a/src/utils.py b/src/utils.py index 2b5fa4c..533f3aa 100644 --- a/src/utils.py +++ b/src/utils.py @@ -135,6 +135,9 @@ async def retrieve_history() -> None: data = json.loads(await resp.text()) if data["chats"]: client_id = data["clientId"] + CONV["all"].clear() + CONV["current"].clear() + RUN.clear() for chat in data["chats"]: name = chat["chatName"] conversation_id = chat["conversationId"]