diff --git a/.gitignore b/.gitignore index 2f1ff63..e4f0ec2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ build/ dist/ app/ tmp/ +data/ # dotenv environment variable files .env diff --git a/package.json b/package.json index ff2fa7f..414e076 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "prod": "node .", "client": "npm i && npm run build && npm run prod", "clean": "docker rmi $(docker images -a -q) -f && docker images -a", - "start": "echo \"y\" | docker-compose rm && docker-compose build && docker-compose up" + "start": "echo \"y\" | docker-compose rm && docker-compose build --no-cache && docker-compose up" }, "author": "Kevin Dang", "license": "ISC", diff --git a/src/client.ts b/src/client.ts index 56dd83d..c4587e8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,5 +1,5 @@ import { Client, GatewayIntentBits } from 'discord.js' -import { registerEvents } from './utils/events.js' +import { UserMessage, registerEvents } from './utils/events.js' import Events from './events/index.js' import { Ollama } from 'ollama' @@ -22,7 +22,8 @@ const ollama = new Ollama({ host: `http://${Keys.ipAddress}:${Keys.portAddress}`, }) -const messageHistory = [ +// Create Queue managed by Events +const messageHistory: [UserMessage] = [ { role: 'system', content: 'Your name is Ollama GU' diff --git a/src/utils/events.ts b/src/utils/events.ts index fbc9434..50228de 100644 --- a/src/utils/events.ts +++ b/src/utils/events.ts @@ -6,12 +6,29 @@ export { Events } from 'discord.js' export type LogMethod = (...args: unknown[]) => void export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object + +/** + * Tokens to run the bot as intended + * @param channel the channel where the bot will respond to queries + * @param model chosen model for the ollama to utilize + * @param clientUid the discord id for the bot + */ export type Tokens = { channel: string, model: string, clientUid: string } +/** + * Format for the messages to be stored when communicating when the bot + * @param role either assistant, user, or system + * @param content string of the message the user or assistant provided + */ +export type UserMessage = { + role: string, + content: string +} + // Event properties export interface EventProps { client: Client @@ -35,10 +52,18 @@ export function event(key: T, callback: EventCallback): return { key, callback } } +/** + * Method to register events to the bot per file in the events directory + * @param client initialized bot client + * @param events all the exported events from the index.ts in the events dir + * @param msgHist The message history of the bot + * @param tokens the passed in environment tokens for the service + * @param ollama the initialized ollama instance + */ export function registerEvents( client: Client, events: Event[], - msgHist: { role: string, content: string }[], + msgHist: UserMessage[], tokens: Tokens, ollama: Ollama ): void { diff --git a/src/utils/messageEmbed.ts b/src/utils/messageEmbed.ts index 1f05771..ea14eda 100644 --- a/src/utils/messageEmbed.ts +++ b/src/utils/messageEmbed.ts @@ -1,5 +1,6 @@ import { EmbedBuilder, Message } from 'discord.js' import { ChatResponse, Ollama } from 'ollama' +import { UserMessage } from './events.js' /** * Method to send replies as normal text on discord like any other user @@ -14,10 +15,7 @@ export async function embedMessage( channel: string, model: string }, - msgHist: { - role: string, - content: string - }[] + msgHist: UserMessage[] ) { // bot response let response: ChatResponse @@ -48,13 +46,13 @@ export async function embedMessage( // dummy message to let user know that query is underway const newEmbed = new EmbedBuilder() .setTitle(`Responding to ${message.author.tag}`) - .setDescription(response.message.content || 'No Content to Provided...') + .setDescription(response.message.content || 'No Content to Provide...') .setColor('#00FF00') // edit the message sentMessage.edit({ embeds: [newEmbed] }) } catch(error: any) { - console.log(`[Event: messageEmbed] Error creating message: ${error.message}`); + console.log(`[Event: messageEmbed] Error creating message: ${error.message}`) const errorEmbed = new EmbedBuilder() .setTitle(`Responding to ${message.author.tag}`) .setDescription(`Issue creating response: ${error.message}`) diff --git a/src/utils/messageNormal.ts b/src/utils/messageNormal.ts index 1884645..5286248 100644 --- a/src/utils/messageNormal.ts +++ b/src/utils/messageNormal.ts @@ -1,5 +1,6 @@ import { Message } from 'discord.js' import ollama, { ChatResponse } from 'ollama' +import { UserMessage } from './events.js' /** * Method to send replies as normal text on discord like any other user @@ -13,10 +14,7 @@ export function normalMessage( channel: string, model: string }, - msgHist: { - role: string, - content: string - }[] + msgHist: UserMessage[] ) { // bot's respnse let response: ChatResponse diff --git a/src/utils/streamParse.ts b/src/utils/streamParse.ts index c9ea87c..0490543 100644 --- a/src/utils/streamParse.ts +++ b/src/utils/streamParse.ts @@ -2,6 +2,8 @@ import { AxiosResponse } from 'axios' /** * When running a /api/chat stream, the output needs to be parsed into an array of objects + * This method is used for development purposes and testing + * * @param stream Axios response to from Ollama */ export function parseStream(stream: AxiosResponse) {