Capacity Context Modify Command (#35)

* add: modify capacity command

* update: version increment
This commit is contained in:
Kevin Dang
2024-04-03 15:22:34 -07:00
committed by GitHub
parent 727731695e
commit 2bdc7b8583
8 changed files with 66 additions and 27 deletions

View File

@@ -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}

4
package-lock.json generated
View File

@@ -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",

View File

@@ -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",

33
src/commands/capacity.ts Normal file
View File

@@ -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
})
}
}

View File

@@ -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[]

View File

@@ -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,16 +29,36 @@ 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'])
response = await embedMessage(message, ollama, tokens, msgHist)
@@ -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

View File

@@ -17,7 +17,7 @@ export class Queue<T> implements IQueue<T> {
* 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<T> implements IQueue<T> {
getItems(): T[] {
return this.storage
}
/**
* Get capacity of the queue
* @returns capacity of queue
*/
getCapacity(): number {
return this.capacity
}
}

View File

@@ -5,7 +5,8 @@ export interface Configuration {
options: {
'message-stream'?: boolean,
'message-style'?: boolean,
'toggle-chat'?: boolean
'toggle-chat'?: boolean,
'history-capacity'?: number
}
}