Add basic support for optional threads.

This commit is contained in:
bertybuttface 2023-01-27 17:45:41 +00:00
parent e1d7b99706
commit 177fe3c60e
2 changed files with 10 additions and 5 deletions

View File

@ -1,8 +1,8 @@
import { ChatGPTAPIBrowser } from "chatgpt";
import { LogService, MatrixClient, UserID } from "matrix-bot-sdk";
import { CHATGPT_CONTEXT, CHATGPT_TIMEOUT, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX, MATRIX_BLACKLIST, MATRIX_WHITELIST, MATRIX_RICH_TEXT, MATRIX_PREFIX_DM } from "./env.js";
import { CHATGPT_CONTEXT, CHATGPT_TIMEOUT, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX, MATRIX_BLACKLIST, MATRIX_WHITELIST, MATRIX_RICH_TEXT, MATRIX_PREFIX_DM, MATRIX_THREADS } from "./env.js";
import { RelatesTo, MessageEvent, StoredConversation, StoredConversationConfig } from "./interfaces.js";
import { sendChatGPTMessage, sendError, sendThreadReply } from "./utils.js";
import { sendChatGPTMessage, sendError, sendReply } from "./utils.js";
export default class CommandHandler {
@ -124,7 +124,7 @@ export default class CommandHandler {
const result = await sendChatGPTMessage(this.chatGPT, await bodyWithoutPrefix, storedConversation);
await Promise.all([
this.client.setTyping(roomId, false, 500),
sendThreadReply(this.client, roomId, this.getRootEventId(event), `${result.response}`, MATRIX_RICH_TEXT)
sendReply(this.client, roomId, this.getRootEventId(event), `${result.response}`, MATRIX_THREADS, MATRIX_RICH_TEXT)
]);
const storedConfig = ((storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {})

View File

@ -28,13 +28,17 @@ export async function sendError(client: MatrixClient, text: string, roomId: stri
* @param {string} roomId the room ID the event being replied to resides in
* @param {string} rootEventId the root event of the thread
* @param {string} text the plain text to reply with
* @param {boolean} thread reply as a thread
* @param {boolean} rich should the plain text be rendered to html using markdown?
*/
export async function sendThreadReply(client: MatrixClient, roomId: string, rootEventId: string, text: string, rich:boolean = false): Promise<void> {
export async function sendReply(client: MatrixClient, roomId: string, rootEventId: string, text: string, thread: boolean = false, rich:boolean = false): Promise<void> {
const contentCommon = {
body: text,
msgtype: "m.text",
}
const contentThreadOnly = {
"m.relates_to": {
event_id: rootEventId,
is_falling_back: true,
@ -67,8 +71,9 @@ export async function sendThreadReply(client: MatrixClient, roomId: string, root
}
const content = rich ? { ...contentCommon, ...contentRichOnly } : { ...contentCommon, ...contentTextOnly };
const finalContent = thread ? { ...content, ...contentThreadOnly } : content
await client.sendEvent(roomId, "m.room.message", content);
await client.sendEvent(roomId, "m.room.message", finalContent);
}
export async function sendChatGPTMessage(chatGPT: ChatGPTAPIBrowser, question: string, storedConversation: StoredConversation) {