mirror of
https://github.com/matrixgpt/matrix-chatgpt-bot.git
synced 2024-09-30 13:05:50 +00:00
Cache details for mentions.
This commit is contained in:
parent
2f6c0379ab
commit
19a8dfb0e4
@ -37,7 +37,7 @@ If you want to get an access token without exposing your password to this bot yo
|
|||||||
OPENAI_EMAIL=
|
OPENAI_EMAIL=
|
||||||
OPENAI_PASSWORD=
|
OPENAI_PASSWORD=
|
||||||
# What type of Login it is, possibility's are google, openai, microsoft
|
# What type of Login it is, possibility's are google, openai, microsoft
|
||||||
OPENAI_LOGIN_TYPE="google"
|
OPENAI_LOGIN_TYPE=google
|
||||||
|
|
||||||
# Matrix Static Settings (required, see notes)
|
# Matrix Static Settings (required, see notes)
|
||||||
# Defaults to "https://matrix.org"
|
# Defaults to "https://matrix.org"
|
||||||
|
@ -1,64 +1,88 @@
|
|||||||
import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt";
|
import { ChatGPTAPIBrowser, ChatResponse } from "chatgpt";
|
||||||
import { MatrixClient } from "matrix-bot-sdk";
|
import { LogService, MatrixClient, UserID } 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, StoredConversationConfig } from "./interfaces.js";
|
import { RelatesTo, StoredConversation, StoredConversationConfig } from "./interfaces.js";
|
||||||
import { isEventAMessage, sendError, sendThreadReply } from "./utils.js";
|
import { sendError, sendThreadReply } from "./utils.js";
|
||||||
|
|
||||||
/**
|
export default class CommandHandler {
|
||||||
* Run when *any* room event is received. The bot only sends a message if needed.
|
|
||||||
* @param client
|
// Variables so we can cache the bot's display name and ID for command matching later.
|
||||||
|
private displayName: string;
|
||||||
|
private userId: string;
|
||||||
|
private localpart: string;
|
||||||
|
|
||||||
|
constructor(private client: MatrixClient, private chatGPT:ChatGPTAPIBrowser) {}
|
||||||
|
|
||||||
|
public async start() {
|
||||||
|
await this.prepareProfile(); // Populate the variables above (async)
|
||||||
|
this.client.on("room.message", this.onMessage.bind(this)); // Set up the event handler
|
||||||
|
}
|
||||||
|
|
||||||
|
private async prepareProfile() {
|
||||||
|
this.userId = await this.client.getUserId();
|
||||||
|
this.localpart = new UserID(this.userId).localpart;
|
||||||
|
try {
|
||||||
|
const profile = await this.client.getUserProfile(this.userId);
|
||||||
|
if (profile && profile['displayname']) this.displayName = profile['displayname'];
|
||||||
|
} catch (e) {
|
||||||
|
// Non-fatal error - we'll just log it and move on.
|
||||||
|
LogService.warn("CommandHandler", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run when *any* room message is received. The bot only sends a message if needed.
|
||||||
* @returns Room event handler, which itself returnings nothing
|
* @returns Room event handler, which itself returnings nothing
|
||||||
*/
|
*/
|
||||||
export async function handleRoomEvent(client: MatrixClient, chatGPT: ChatGPTAPIBrowser): Promise<(roomId: string, event: any) => Promise<void>> {
|
private async onMessage(roomId: string, event: any) {
|
||||||
return async (roomId: string, event: any) => {
|
|
||||||
if (isEventAMessage(event)) {
|
|
||||||
try {
|
try {
|
||||||
if (event.sender === MATRIX_BOT_USERNAME) return; // Don't reply to ourself
|
if (event.sender === MATRIX_BOT_USERNAME) return; // Ignore 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; // Ignore old messages
|
||||||
const relatesTo: RelatesTo | undefined = event.content["m.relates_to"];
|
const relatesTo: RelatesTo | undefined = event.content["m.relates_to"];
|
||||||
if ((relatesTo !== undefined) && (relatesTo["rel_type"] === "m.replace")) return; // Don't reply to edits
|
if ((relatesTo !== undefined) && (relatesTo["rel_type"] === "m.replace")) return; // Ignore edits
|
||||||
|
|
||||||
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 this.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: 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_PREFIX) && (relatesTo === undefined)) || (MATRIX_PREFIX_REPLY && (relatesTo !== undefined));
|
const shouldBePrefixed: boolean = ((MATRIX_PREFIX) && (relatesTo === undefined)) || (MATRIX_PREFIX_REPLY && (relatesTo !== undefined));
|
||||||
if (shouldBePrefixed && !event.content.body.startsWith(MATRIX_PREFIX)) return; // Don't reply without prefix if prefixed
|
const prefixes = [MATRIX_PREFIX, `${this.localpart}:`, `${this.displayName}:`, `${this.userId}:`];
|
||||||
|
const prefixUsed = prefixes.find(p => event.content.body.startsWith(p));
|
||||||
|
if (shouldBePrefixed && !prefixUsed) return; // Ignore without prefix if prefixed
|
||||||
|
await Promise.all([this.client.sendReadReceipt(roomId, event.event_id), this.client.setTyping(roomId, true, 10000)]);
|
||||||
|
|
||||||
await Promise.all([client.sendReadReceipt(roomId, event.event_id), client.setTyping(roomId, true, 10000)]);
|
const trimLength: number = shouldBePrefixed ? prefixUsed.length : 0
|
||||||
|
|
||||||
const trimLength: number = shouldBePrefixed ? MATRIX_DEFAULT_PREFIX.length : 0
|
|
||||||
const question: string = event.content.body.slice(trimLength).trimStart();
|
const question: string = event.content.body.slice(trimLength).trimStart();
|
||||||
|
|
||||||
if ((question === undefined) || !question) {
|
if ((question === undefined) || !question) {
|
||||||
await sendError(client, "Error with question: " + question, roomId, event.event_id);
|
await sendError(this.client, "Error with question: " + question, roomId, event.event_id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result: ChatResponse
|
let result: ChatResponse
|
||||||
if (storedConversation !== undefined) {
|
if (storedConversation !== undefined) {
|
||||||
result = await chatGPT.sendMessage(question, {
|
result = await this.chatGPT.sendMessage(question, {
|
||||||
timeoutMs: CHATGPT_TIMEOUT,
|
timeoutMs: CHATGPT_TIMEOUT,
|
||||||
conversationId: storedConversation.conversationId,
|
conversationId: storedConversation.conversationId,
|
||||||
parentMessageId: storedConversation.messageId
|
parentMessageId: storedConversation.messageId
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
result = await chatGPT.sendMessage(question, {timeoutMs: CHATGPT_TIMEOUT});
|
result = await this.chatGPT.sendMessage(question, {timeoutMs: CHATGPT_TIMEOUT});
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all([client.setTyping(roomId, false, 500), sendThreadReply(client, `${result.response}`, roomId, rootEventId)]);
|
await Promise.all([this.client.setTyping(roomId, false, 500), sendThreadReply(this.client, `${result.response}`, roomId, rootEventId)]);
|
||||||
|
|
||||||
await client.storageProvider.storeValue('gpt-' + rootEventId, JSON.stringify({
|
await this.client.storageProvider.storeValue('gpt-' + rootEventId, JSON.stringify({
|
||||||
conversationId: result.conversationId,
|
conversationId: result.conversationId,
|
||||||
messageId: result.messageId,
|
messageId: result.messageId,
|
||||||
config: ((storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}),
|
config: ((storedConversation !== undefined && storedConversation.config !== undefined) ? storedConversation.config : {}),
|
||||||
}));
|
}));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
await sendError(client, "Bot error, terminating.", roomId, event.event_id);
|
await sendError(this.client, "Bot error, terminating.", roomId, event.event_id);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
import { DATA_PATH, OPENAI_EMAIL, OPENAI_PASSWORD, OPENAI_LOGIN_TYPE, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION } from './env.js'
|
import { DATA_PATH, OPENAI_EMAIL, OPENAI_PASSWORD, OPENAI_LOGIN_TYPE, MATRIX_HOMESERVER_URL, MATRIX_ACCESS_TOKEN, MATRIX_AUTOJOIN, MATRIX_BOT_PASSWORD, MATRIX_BOT_USERNAME, MATRIX_ENCRYPTION } from './env.js'
|
||||||
import { parseMatrixUsernamePretty } from './utils.js';
|
import { parseMatrixUsernamePretty } from './utils.js';
|
||||||
import { handleRoomEvent } from './handlers.js';
|
import CommandHandler from "./handlers.js"
|
||||||
import { ChatGPTAPIBrowser } from 'chatgpt'
|
import { ChatGPTAPIBrowser } from 'chatgpt'
|
||||||
|
|
||||||
LogService.setLogger(new RichConsoleLogger());
|
LogService.setLogger(new RichConsoleLogger());
|
||||||
@ -68,7 +68,9 @@ async function main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("room.event", await handleRoomEvent(client, chatGPT));
|
// Prepare the command handler
|
||||||
|
const commands = new CommandHandler(client, chatGPT);
|
||||||
|
await commands.start();
|
||||||
|
|
||||||
LogService.info("index", "Starting bot...");
|
LogService.info("index", "Starting bot...");
|
||||||
await client.start()
|
await client.start()
|
||||||
|
@ -9,10 +9,6 @@ export function parseMatrixUsernamePretty(matrix_username: string): string {
|
|||||||
return withoutUrl.split('@')[1]
|
return withoutUrl.split('@')[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isEventAMessage(event: any): event is MessageEvent {
|
|
||||||
return event.type === 'm.room.message'
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function sendError(client: MatrixClient, text: string, roomId: string, eventId: string): Promise<void> {
|
export async function sendError(client: MatrixClient, text: string, roomId: string, eventId: string): Promise<void> {
|
||||||
Promise.all([client.setTyping(roomId, false, 500), client.sendText(roomId, text), client.sendReadReceipt(roomId, eventId)]);
|
Promise.all([client.setTyping(roomId, false, 500), client.sendText(roomId, text), client.sendReadReceipt(roomId, eventId)]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user