* add: validate thread creation in ollama channel * rm: channel_id variable * add: short notes for threads * update: openFile to openConfig for clarity * update: test ci runs on master * add: notes for work * add: basic chat storing via json * update: stores entire msgHist according to capacity * add: removes json file if thread is deleted * add: chats with independent histories * add: private vs public threads * update: validate threads made by ollama for chats * update: cleanup and version increment
108 lines
4.4 KiB
TypeScript
108 lines
4.4 KiB
TypeScript
import { embedMessage, event, Events, normalMessage, UserMessage } from '../utils/index.js'
|
|
import { Configuration, getConfig, getThread, openConfig, openThreadInfo } from '../utils/jsonHandler.js'
|
|
import { clean } from '../utils/mentionClean.js'
|
|
import { ThreadChannel } from 'discord.js'
|
|
|
|
/**
|
|
* Max Message length for free users is 2000 characters (bot or not).
|
|
* @param message the message received from the channel
|
|
*/
|
|
export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama, client }, message) => {
|
|
log(`Message \"${clean(message.content)}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
|
|
|
// need new check for "open/active" threads here!
|
|
const threadMessages: UserMessage[] = await new Promise((resolve) => {
|
|
// set new queue to modify
|
|
getThread(`${message.channelId}.json`, (threadInfo) => {
|
|
if (threadInfo?.messages)
|
|
resolve(threadInfo.messages)
|
|
else
|
|
log(`Channel/Thread ${message.channelId} does not exist.`)
|
|
})
|
|
})
|
|
|
|
// Do not respond if bot talks in the chat
|
|
if (message.author.tag === message.client.user.tag) return
|
|
|
|
// Only respond if message mentions the bot
|
|
if (!message.mentions.has(tokens.clientUid)) return
|
|
|
|
let shouldStream = false
|
|
|
|
// Try to query and send embed
|
|
try {
|
|
const config: Configuration = await new Promise((resolve, reject) => {
|
|
getConfig('config.json', (config) => {
|
|
// check if config.json exists
|
|
if (config === undefined) {
|
|
reject(new Error('No Configuration is set up.\n\nCreating \`config.json\` with \`message-style\` set as \`false\` for regular messages.\nPlease try chatting again.'))
|
|
return
|
|
}
|
|
|
|
// check if chat is disabled
|
|
if (!config.options['toggle-chat']) {
|
|
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
|
return
|
|
}
|
|
|
|
// check if there is a set capacity in config
|
|
if (typeof config.options['modify-capacity'] !== 'number')
|
|
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`)
|
|
else if (config.options['modify-capacity'] === msgHist.capacity)
|
|
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
|
|
else {
|
|
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`)
|
|
msgHist.capacity = config.options['modify-capacity']
|
|
}
|
|
|
|
// set stream state
|
|
shouldStream = config.options['message-stream'] as boolean || false
|
|
|
|
resolve(config)
|
|
})
|
|
})
|
|
|
|
// response string for ollama to put its response
|
|
let response: string
|
|
|
|
// set up new queue
|
|
msgHist.setQueue(threadMessages)
|
|
|
|
// check if we can push, if not, remove oldest
|
|
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
|
|
|
|
// push user response before ollama query
|
|
msgHist.enqueue({
|
|
role: 'user',
|
|
content: clean(message.content)
|
|
})
|
|
|
|
// undefined or false, use normal, otherwise use embed
|
|
if (config.options['message-style'])
|
|
response = await embedMessage(message, ollama, tokens, msgHist, shouldStream)
|
|
else
|
|
response = await normalMessage(message, ollama, tokens, msgHist, shouldStream)
|
|
|
|
// If something bad happened, remove user query and stop
|
|
if (response == undefined) { msgHist.pop(); return }
|
|
|
|
// if queue is full, remove the oldest message
|
|
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
|
|
|
|
// successful query, save it in context history
|
|
msgHist.enqueue({
|
|
role: 'assistant',
|
|
content: response
|
|
})
|
|
|
|
// only update the json on success
|
|
openThreadInfo(`${message.channelId}.json`,
|
|
client.channels.fetch(message.channelId) as unknown as ThreadChannel,
|
|
msgHist.getItems()
|
|
)
|
|
} catch (error: any) {
|
|
msgHist.pop() // remove message because of failure
|
|
openConfig('config.json', 'message-style', false)
|
|
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
|
}
|
|
}) |