Merge pull request #25 from matrixgpt/code-tidy

Add types to variable and tidy code
This commit is contained in:
bertybuttface 2023-01-03 02:56:40 +00:00 committed by GitHub
commit a5da083eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View File

@ -38,13 +38,13 @@ export const {
MATRIX_THREADS: {schema: z.boolean().default(true)}, MATRIX_THREADS: {schema: z.boolean().default(true)},
/** Matrix Bot Runtime Config */ /** 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: {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: {schema: z.boolean().default(false)},
MATRIX_DEFAULT_REQUIRE_MENTION_IN_DM: {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)}, MATRIX_DEFAULT_REQUIRE_MENTION_IN_REPLY: {schema: z.boolean().default(false)},
/** ChatGPT Settings */ /** ChatGPT Settings */
OPENAI_EMAIL: {schema: z.string().min(3)}, OPENAI_EMAIL: {schema: z.string().min(3)},
OPENAI_PASSWORD: {schema: z.string().min(1)}, OPENAI_PASSWORD: {schema: z.string().min(1)},
OPENAI_LOGIN_TYPE: {schema: z.enum(["google", "openai", "microsoft"]).default("google")}, 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)}
}); });

View File

@ -1,7 +1,7 @@
import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt"; import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt";
import { MatrixClient } from "matrix-bot-sdk"; import { MatrixClient } from "matrix-bot-sdk";
import { CHATGPT_TIMEOUT, MATRIX_BOT_USERNAME, MATRIX_DEFAULT_PREFIX_REPLY, MATRIX_DEFAULT_PREFIX} from "./env.js"; 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"; 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 rootEventId: string = (relatesTo !== undefined && relatesTo.event_id !== undefined) ? relatesTo.event_id : event.event_id;
const storedValue: string = await client.storageProvider.readValue('gpt-' + rootEventId) const storedValue: string = await client.storageProvider.readValue('gpt-' + rootEventId)
const storedConversation: StoredConversation = (storedValue !== undefined) ? JSON.parse(storedValue) : undefined; const storedConversation: StoredConversation = (storedValue !== undefined) ? JSON.parse(storedValue) : undefined;
const config = (storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}; const config: StoredConversationConfig = (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 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)); 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 (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 if (Date.now() - event.origin_server_ts > 10000) return; // Don't reply to old messages