diff --git a/docker-compose.yml b/docker-compose.yml index 2b0f93b..0df8cdc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: build: ./ # find docker file in designated path container_name: discord restart: always # rebuild container always - image: discord/bot:0.3.5 + image: discord/bot:0.3.6 environment: CLIENT_TOKEN: ${CLIENT_TOKEN} GUILD_ID: ${GUILD_ID} diff --git a/package-lock.json b/package-lock.json index d82aa88..9640abf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "discord-ollama", - "version": "0.3.5", + "version": "0.3.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "discord-ollama", - "version": "0.3.5", + "version": "0.3.6", "license": "ISC", "dependencies": { "axios": "^1.6.2", diff --git a/package.json b/package.json index f620d57..8881638 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-ollama", - "version": "0.3.5", + "version": "0.3.6", "description": "Ollama Integration into discord", "main": "build/index.js", "exports": "./build/index.js", diff --git a/src/commands/capacity.ts b/src/commands/capacity.ts new file mode 100644 index 0000000..ba79c6f --- /dev/null +++ b/src/commands/capacity.ts @@ -0,0 +1,33 @@ +import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js' +import { SlashCommand } from '../utils/commands.js' +import { openFile } from '../utils/jsonHandler.js' + +export const Capacity: SlashCommand = { + name: 'modify-capacity', + description: 'number of messages bot will hold for context.', + + // set available user options to pass to the command + options: [ + { + name: 'context-capacity', + description: 'a number to set capacity', + type: ApplicationCommandOptionType.Number, + required: true + } + ], + + // Query for message information and set the style + run: async (client: Client, interaction: CommandInteraction) => { + // fetch channel and message + const channel = await client.channels.fetch(interaction.channelId) + if (!channel || channel.type !== ChannelType.GuildText) return + + // set state of bot chat features + openFile('config.json', interaction.commandName, interaction.options.get('context-capacity')?.value) + + interaction.reply({ + content: `Message History Capacity has been set to \`${interaction.options.get('context-capacity')?.value}\``, + ephemeral: true + }) + } +} \ No newline at end of file diff --git a/src/commands/index.ts b/src/commands/index.ts index e9d8490..55a2990 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -4,11 +4,13 @@ import { MessageStyle } from './messageStyle.js' import { MessageStream } from './messageStream.js' import { Disable } from './disable.js' import { Shutoff } from './shutoff.js' +import { Capacity } from './capacity.js' export default [ ThreadCreate, MessageStyle, MessageStream, Disable, - Shutoff + Shutoff, + Capacity ] as SlashCommand[] \ No newline at end of file diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index aeb0c8e..744cc78 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -18,15 +18,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama // Only respond if message mentions the bot if (!message.mentions.has(tokens.clientUid)) return - // check if we can push, if not, remove oldest - if (msgHist.size() === msgHist.getCapacity()) msgHist.dequeue() - - // push user response - msgHist.enqueue({ - role: 'user', - content: message.content - }) - // Try to query and send embed try { const config: Configuration = await new Promise((resolve, reject) => { @@ -38,15 +29,35 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama } // check if chat is disabled - if(!config.options['toggle-chat']) { + 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['history-capacity'] !== 'number') + log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`) + else if (config.options['history-capacity'] === msgHist.capacity) + log(`Capacity matches config as ${msgHist.capacity}, no changes made.`) + else { + log(`New Capacity found. Setting Context Capacity to ${config.options['history-capacity']}.`) + msgHist.capacity = config.options['history-capacity'] + } + resolve(config) }) }) let response: ChatResponse + + // check if we can push, if not, remove oldest + if (msgHist.size() === msgHist.capacity) msgHist.dequeue() + + // push user response before ollama query + msgHist.enqueue({ + role: 'user', + content: message.content + }) // undefined or false, use normal, otherwise use embed if (config.options['message-style']) @@ -58,9 +69,9 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama if (response == undefined) { msgHist.pop(); return } // if queue is full, remove the oldest message - if (msgHist.size() === msgHist.getCapacity()) msgHist.dequeue() + if (msgHist.size() === msgHist.capacity) msgHist.dequeue() - // successful query, save it as history + // successful query, save it in context history msgHist.enqueue({ role: 'assistant', content: response.message.content diff --git a/src/queues/queue.ts b/src/queues/queue.ts index c634b98..72b90bd 100644 --- a/src/queues/queue.ts +++ b/src/queues/queue.ts @@ -17,7 +17,7 @@ export class Queue implements IQueue { * Set up Queue * @param capacity max length of queue */ - constructor(private capacity: number = 5) {} + constructor(public capacity: number = 5) {} /** * Put item in front of queue @@ -59,12 +59,4 @@ export class Queue implements IQueue { getItems(): T[] { return this.storage } - - /** - * Get capacity of the queue - * @returns capacity of queue - */ - getCapacity(): number { - return this.capacity - } } \ No newline at end of file diff --git a/src/utils/jsonHandler.ts b/src/utils/jsonHandler.ts index 4bb257b..d133f9c 100644 --- a/src/utils/jsonHandler.ts +++ b/src/utils/jsonHandler.ts @@ -5,7 +5,8 @@ export interface Configuration { options: { 'message-stream'?: boolean, 'message-style'?: boolean, - 'toggle-chat'?: boolean + 'toggle-chat'?: boolean, + 'history-capacity'?: number } }