From d7a14fd4eefb8c1098a222dcba5aab8756ca44fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Thu, 19 Oct 2023 17:27:27 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20modify=20matrix=20message=20content?= =?UTF-8?q?=20to=20format=20mentions=20and=20newlines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nio_llm/client.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/nio_llm/client.py b/src/nio_llm/client.py index 5542860..f7e318d 100644 --- a/src/nio_llm/client.py +++ b/src/nio_llm/client.py @@ -1,5 +1,6 @@ import asyncio import logging +import re import time from collections import deque @@ -190,6 +191,18 @@ class LLMClient(AsyncClient): output = response["choices"][0]["message"]["content"] # type: ignore output = output.strip().removeprefix(f"{self.uid}:").strip() + # replace newlines with
+ formatted_output = output.replace("\n", "
") + + # detect mentions and replace them with html mentions + formatted_output = re.sub( + r"@[^:]+:[^ :]+", + lambda match: f'', + formatted_output, + ) + + logger.debug(f"Formatted response: {formatted_output}") + # send the response await self.room_send( room_id=self.room, @@ -197,6 +210,8 @@ class LLMClient(AsyncClient): content={ "msgtype": "m.text", "body": output, + "format": "org.matrix.custom.html", + "formatted_body": formatted_output, }, ) logger.debug(f"Sent response: {output}")