Merge pull request #30 from matrixgpt/configurable-prefix

Allow changing the MATRIX_DEFAULT_PREFIX
This commit is contained in:
bertybuttface 2023-01-06 16:23:09 +00:00 committed by GitHub
commit bcaf9850f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -17,12 +17,16 @@ 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: StoredConversationConfig = (storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}; const config: StoredConversationConfig = (storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {};
const MATRIX_PREFIX: string = (config.MATRIX_PREFIX === undefined) ? MATRIX_DEFAULT_PREFIX : config.MATRIX_PREFIX
const MATRIX_PREFIX_REPLY:boolean = (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_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
if (shouldBePrefixed && !event.content.body.startsWith(MATRIX_DEFAULT_PREFIX)) return; // Don't reply without prefix if prefixed if (shouldBePrefixed && !event.content.body.startsWith(MATRIX_PREFIX)) return; // Don't reply without prefix if prefixed
await Promise.all([client.sendReadReceipt(roomId, event.event_id), client.setTyping(roomId, true, 10000)]); await Promise.all([client.sendReadReceipt(roomId, event.event_id), client.setTyping(roomId, true, 10000)]);

View File

@ -69,6 +69,7 @@ export type MatrixInviteEvent = CommonMatrixEventFields & {
export type MembershipType = 'leave' | 'invite' | 'join' export type MembershipType = 'leave' | 'invite' | 'join'
export type StoredConversationConfig = { export type StoredConversationConfig = {
MATRIX_PREFIX?: string;
MATRIX_PREFIX_REPLY?: boolean; MATRIX_PREFIX_REPLY?: boolean;
} }