diff --git a/src/env.ts b/src/env.ts index f9049ec..b634009 100644 --- a/src/env.ts +++ b/src/env.ts @@ -38,13 +38,13 @@ export const { MATRIX_THREADS: {schema: z.boolean().default(true)}, /** Matrix Bot Runtime Config */ MATRIX_DEFAULT_PREFIX: {schema: z.string().default(""), description: "Set this to empty string if you don't want to use it. Trailing space matters."}, - MATRIX_DEFAULT_PREFIX_REPLY: {schema: z.boolean().default(false)}, + MATRIX_DEFAULT_PREFIX_REPLY: {schema: z.boolean().default(false)}, MATRIX_DEFAULT_REQUIRE_MENTION: {schema: z.boolean().default(false)}, MATRIX_DEFAULT_REQUIRE_MENTION_IN_DM: {schema: z.boolean().default(false)}, MATRIX_DEFAULT_REQUIRE_MENTION_IN_REPLY: {schema: z.boolean().default(false)}, /** ChatGPT Settings */ - OPENAI_EMAIL: {schema: z.string().min(3)}, - OPENAI_PASSWORD: {schema: z.string().min(1)}, + OPENAI_EMAIL: {schema: z.string().min(3)}, + OPENAI_PASSWORD: {schema: z.string().min(1)}, OPENAI_LOGIN_TYPE: {schema: z.enum(["google", "openai", "microsoft"]).default("google")}, - CHATGPT_TIMEOUT: {schema: z.number().default(2 * 60 * 1000)} + CHATGPT_TIMEOUT: {schema: z.number().default(2 * 60 * 1000)} }); diff --git a/src/handlers.ts b/src/handlers.ts index b3470ea..fd33887 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -1,7 +1,7 @@ import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt"; import { MatrixClient } from "matrix-bot-sdk"; import { CHATGPT_TIMEOUT, MATRIX_BOT_USERNAME, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX} from "./env.js"; -import { RelatesTo, StoredConversation } from "./interfaces.js"; +import { RelatesTo, StoredConversation, StoredConversationConfig } from "./interfaces.js"; import { isEventAMessage, sendError, sendThreadReply } from "./utils.js"; /** @@ -17,8 +17,8 @@ export async function handleRoomEvent(client: MatrixClient, chatGPT: ChatGPTAPIB const rootEventId: string = (relatesTo !== undefined && relatesTo.event_id !== undefined) ? relatesTo.event_id : event.event_id; const storedValue: string = await client.storageProvider.readValue('gpt-' + rootEventId) const storedConversation: StoredConversation = (storedValue !== undefined) ? JSON.parse(storedValue) : undefined; - const config = (storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}; - const MATRIX_PREFIX_REPLY = (config.MATRIX_PREFIX_REPLY === undefined) ? MATRIX_DEFAULT_PREFIX_REPLY : config.MATRIX_PREFIX_REPLY + const config: StoredConversationConfig = (storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}; + const MATRIX_PREFIX_REPLY:boolean = (config.MATRIX_PREFIX_REPLY === undefined) ? MATRIX_DEFAULT_PREFIX_REPLY : config.MATRIX_PREFIX_REPLY const shouldBePrefixed: boolean = ((MATRIX_DEFAULT_PREFIX) && (relatesTo === undefined)) || (MATRIX_PREFIX_REPLY && (relatesTo !== undefined)); if (event.sender === MATRIX_BOT_USERNAME) return; // Don't reply to ourself if (Date.now() - event.origin_server_ts > 10000) return; // Don't reply to old messages