mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 11:56:06 -05:00
Compare commits
12 Commits
v0.8.3
...
f632ae559f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f632ae559f | ||
|
|
e85aff01a7 | ||
|
|
186b428454 | ||
|
|
4236582cf4 | ||
|
|
e07e8fbf89 | ||
|
|
6d0a537540 | ||
|
|
0ddd59aea1 | ||
|
|
a5faca87aa | ||
|
|
4c96b3863a | ||
|
|
40783818b9 | ||
|
|
ed0d8600df | ||
|
|
03939ef562 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -33,6 +33,7 @@ jobs:
|
||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||
echo REDIS_IP = ${{ secrets.REDIS_IP }} >> .env
|
||||
echo REDIS_PORT = ${{ secrets.REDIS_PORT }} >> .env
|
||||
|
||||
@@ -61,6 +62,7 @@ jobs:
|
||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||
echo REDIS_IP = ${{ secrets.REDIS_IP }} >> .env
|
||||
echo REDIS_PORT = ${{ secrets.REDIS_PORT }} >> .env
|
||||
|
||||
|
||||
3
.github/workflows/coverage.yml
vendored
3
.github/workflows/coverage.yml
vendored
@@ -30,12 +30,13 @@ jobs:
|
||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||
echo REDIS_IP = ${{ secrets.REDIS_IP }} >> .env
|
||||
echo REDIS_PORT = ${{ secrets.REDIS_PORT }} >> .env
|
||||
|
||||
- name: Collect Code Coverage
|
||||
run: |
|
||||
LINE_PCT=$(npm run test:coverage | tail -2 | head -1 | awk '{print $3}')
|
||||
LINE_PCT=$(npm run coverage | tail -2 | head -1 | awk '{print $3}')
|
||||
echo "COVERAGE=$LINE_PCT" >> $GITHUB_ENV
|
||||
|
||||
- name: Upload Code Coverage
|
||||
|
||||
133
.github/workflows/deploy.yml
vendored
Normal file
133
.github/workflows/deploy.yml
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
name: Deploy
|
||||
run-name: Deploy Application Latest
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
Deploy-Application:
|
||||
runs-on: self-hosted
|
||||
environment: deploy
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- name: Checkout Repo
|
||||
uses: actions/checkout@v4
|
||||
|
||||
# Generate Secret File for Compose case
|
||||
- name: Create Environment Variables
|
||||
run: |
|
||||
touch .env
|
||||
echo CLIENT_TOKEN = ${{ secrets.CLIENT }} >> .env
|
||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||
echo DISCORD_IP = ${{ secrets.DISCORD_IP }} >> .env
|
||||
echo SUBNET_ADDRESS = ${{ secrets.SUBNET_ADDRESS }} >> .env
|
||||
echo REDIS_IP = ${{ secrets.REDIS_IP }} >> .env
|
||||
echo REDIS_PORT = ${{ secrets.REDIS_PORT }} >> .env
|
||||
|
||||
- name: Check if directory exists and delete it
|
||||
run: |
|
||||
if [ -d "${{ secrets.PATH }}" ]; then
|
||||
echo "Directory exists, deleting old version..."
|
||||
rm -rf ${{ secrets.PATH }}
|
||||
else
|
||||
echo "Directory does not exist."
|
||||
fi
|
||||
|
||||
- name: Clone Repo onto Server
|
||||
run: |
|
||||
git clone https://github.com/kevinthedang/discord-ollama.git ${{ secrets.PATH }}
|
||||
cd ${{ secrets.PATH }}
|
||||
|
||||
- name: Install nvm and Node.js lts/jod
|
||||
run: |
|
||||
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
|
||||
export NVM_DIR="$HOME/.nvm"
|
||||
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
||||
echo "NVM installed successfully."
|
||||
nvm install lts/jod
|
||||
nvm alias default lts/jod
|
||||
node -v
|
||||
npm -v
|
||||
|
||||
- name: Build Application
|
||||
run: |
|
||||
export NVM_DIR="$HOME/.nvm"
|
||||
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
||||
npm install
|
||||
|
||||
IMAGE="kevinthedang/discord-ollama"
|
||||
REDIS="redis"
|
||||
OLLAMA="ollama/ollama"
|
||||
|
||||
if docker images | grep -q $IMAGE; then
|
||||
IMAGE_ID=$(docker images -q $IMAGE)
|
||||
CONTAINER_IDS=$(docker ps -q --filter "ancestor=$IMAGE_ID")
|
||||
|
||||
if [ ! -z "$CONTAINER_IDS" ]; then
|
||||
# Stop and remove the running containers
|
||||
docker stop $CONTAINER_IDS
|
||||
echo "Stopped and removed the containers using the image $IMAGE"
|
||||
fi
|
||||
docker rmi $IMAGE_ID
|
||||
echo "Old $IMAGE Image Removed"
|
||||
fi
|
||||
|
||||
if docker images | grep -q $REDIS; then
|
||||
IMAGE_ID=$(docker images -q $REDIS)
|
||||
CONTAINER_IDS=$(docker ps -q --filter "ancestor=$IMAGE_ID")
|
||||
|
||||
if [ ! -z "$CONTAINER_IDS" ]; then
|
||||
# Stop and remove the running containers
|
||||
docker stop $CONTAINER_IDS
|
||||
echo "Stopped and removed the containers using the image $REDIS"
|
||||
fi
|
||||
docker rmi $IMAGE_ID
|
||||
echo "Old $REDIS Image Removed"
|
||||
fi
|
||||
|
||||
if docker images | grep -q $OLLAMA; then
|
||||
IMAGE_ID=$(docker images -q $OLLAMA)
|
||||
CONTAINER_IDS=$(docker ps -q --filter "ancestor=$IMAGE_ID")
|
||||
|
||||
if [ ! -z "$CONTAINER_IDS" ]; then
|
||||
# Stop and remove the running containers
|
||||
docker stop $CONTAINER_IDS
|
||||
echo "Stopped and removed the containers using the image $OLLAMA"
|
||||
fi
|
||||
docker rmi $IMAGE_ID
|
||||
echo "Old $OLLAMA Image Removed"
|
||||
fi
|
||||
|
||||
docker network prune -f
|
||||
docker system prune -a -f
|
||||
|
||||
npm run docker:build-latest
|
||||
|
||||
- name: Start Application
|
||||
run: |
|
||||
docker network create --subnet=${{ secrets.SUBNET_ADDRESS }}/16 ollama-net || true
|
||||
docker run --rm -d \
|
||||
-v ollama:/root/.ollama \
|
||||
-p ${{ secrets.OLLAMA_PORT }}:${{ secrets.OLLAMA_PORT }} \
|
||||
--name ollama \
|
||||
--network ollama-net \
|
||||
--ip ${{ secrets.OLLAMA_IP }} \
|
||||
ollama/ollama:latest
|
||||
|
||||
docker run --rm -d \
|
||||
-v redis:/root/.redis \
|
||||
-p ${{ secrets.REDIS_PORT }}:${{ secrets.REDIS_PORT }} \
|
||||
--name redis \
|
||||
--network ollama-net \
|
||||
--ip ${{ secrets.REDIS_IP }} \
|
||||
redis:latest
|
||||
|
||||
docker run --rm -d \
|
||||
-v discord:/src/app \
|
||||
--name discord \
|
||||
--network ollama-net \
|
||||
--ip ${{ secrets.DISCORD_IP }} \
|
||||
kevinthedang/discord-ollama
|
||||
50
.github/workflows/release.yml
vendored
50
.github/workflows/release.yml
vendored
@@ -1,50 +0,0 @@
|
||||
name: Deploy
|
||||
run-name: Release Docker Image
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
|
||||
jobs:
|
||||
Release-Docker-Image:
|
||||
runs-on: ubuntu-latest
|
||||
environment: release
|
||||
timeout-minutes: 3
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Node Environment lts/jod
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: lts/jod
|
||||
cache: "npm"
|
||||
|
||||
- name: Create Environment Variables
|
||||
run: |
|
||||
touch .env
|
||||
echo CLIENT_TOKEN = NOT_REAL_TOKEN >> .env
|
||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||
echo REDIS_IP = ${{ secrets.REDIS_IP }} >> .env
|
||||
echo REDIS_PORT = ${{ secrets.REDIS_PORT }} >> .env
|
||||
|
||||
- name: Get Version from package.json
|
||||
run: echo "VERSION=$(jq -r '.version' package.json)" >> $GITHUB_ENV
|
||||
|
||||
- name: Build Image
|
||||
run: |
|
||||
npm run docker:build
|
||||
|
||||
- name: Build Image as Latest
|
||||
run: |
|
||||
npm run docker:build-latest
|
||||
|
||||
- name: Log into Docker
|
||||
run: |
|
||||
docker login --username ${{ vars.DOCKER_USER }} --password ${{ secrets.DOCKER_PASS }}
|
||||
|
||||
- name: Release Docker Image
|
||||
run: |
|
||||
docker push ${{ vars.DOCKER_USER }}/discord-ollama:${{ env.VERSION }}
|
||||
docker push ${{ vars.DOCKER_USER }}/discord-ollama:latest
|
||||
3
.github/workflows/test.yml
vendored
3
.github/workflows/test.yml
vendored
@@ -41,9 +41,10 @@ jobs:
|
||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
||||
echo REDIS_IP = ${{ secrets.REDIS_IP }} >> .env
|
||||
echo REDIS_PORT = ${{ secrets.REDIS_PORT }} >> .env
|
||||
|
||||
- name: Test Application
|
||||
run: |
|
||||
npm run test:run
|
||||
npm run tests
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<p><a href="#"></a><a href="https://creativecommons.org/licenses/by/4.0/"><img alt="License" src="https://img.shields.io/badge/License-CC_BY_4.0-darkgreen.svg" /></a>
|
||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/releases/latest"><img alt="Release" src="https://img.shields.io/github/v/release/kevinthedang/discord-ollama?logo=github" /></a>
|
||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/build.yml"><img alt="Build Status" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/build.yml/badge.svg" /></a>
|
||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/release.yml"><img alt="Release Status" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/release.yml/badge.svg" /></a>
|
||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/deploy.yml"><img alt="Deploy Status" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/deploy.yml/badge.svg" /></a>
|
||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml"><img alt="Testing Status" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml/badge.svg" /></a>
|
||||
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/coverage.yml"><img alt="Code Coverage" src="https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/kevinthedang/bc7b5dcfa16561ab02bb3df67a99b22d/raw/coverage.json"></a>
|
||||
</div>
|
||||
|
||||
@@ -7,11 +7,12 @@ services:
|
||||
build: ./ # find docker file in designated path
|
||||
container_name: discord
|
||||
restart: always # rebuild container always
|
||||
image: kevinthedang/discord-ollama:0.8.3
|
||||
image: kevinthedang/discord-ollama:0.8.5
|
||||
environment:
|
||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||
OLLAMA_IP: ${OLLAMA_IP}
|
||||
OLLAMA_PORT: ${OLLAMA_PORT}
|
||||
MODEL: ${MODEL}
|
||||
REDIS_IP: ${REDIS_IP}
|
||||
REDIS_PORT: ${REDIS_PORT}
|
||||
networks:
|
||||
|
||||
1277
package-lock.json
generated
1277
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "discord-ollama",
|
||||
"version": "0.8.3",
|
||||
"version": "0.8.5",
|
||||
"description": "Ollama Integration into discord",
|
||||
"main": "build/index.js",
|
||||
"exports": "./build/index.js",
|
||||
"scripts": {
|
||||
"test:run": "vitest run",
|
||||
"test:coverage": "vitest run --coverage",
|
||||
"tests": "vitest run",
|
||||
"coverage": "vitest run --coverage",
|
||||
"watch": "tsx watch src",
|
||||
"build": "tsc",
|
||||
"prod": "node .",
|
||||
@@ -27,18 +27,18 @@
|
||||
"author": "Kevin Dang",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"discord.js": "^14.17.3",
|
||||
"dotenv": "^16.4.7",
|
||||
"ollama": "^0.5.11",
|
||||
"discord.js": "^14.20.0",
|
||||
"dotenv": "^16.5.0",
|
||||
"ollama": "^0.5.15",
|
||||
"redis": "^4.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.2",
|
||||
"@vitest/coverage-v8": "^2.1.8",
|
||||
"@types/node": "^22.13.14",
|
||||
"@vitest/coverage-v8": "^3.0.9",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.7.2",
|
||||
"vitest": "^2.1.4"
|
||||
"tsx": "^4.19.3",
|
||||
"typescript": "^5.8.2",
|
||||
"vitest": "^3.0.4"
|
||||
},
|
||||
"type": "module",
|
||||
"engines": {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Client, GatewayIntentBits } from 'discord.js'
|
||||
import { Ollama } from 'ollama'
|
||||
import { createClient } from 'redis'
|
||||
import { Queue } from './queues/queue.js'
|
||||
import { Queue } from './components/index.js'
|
||||
import { UserMessage, registerEvents } from './utils/index.js'
|
||||
import Events from './events/index.js'
|
||||
import Keys from './keys.js'
|
||||
@@ -34,10 +34,14 @@ registerEvents(client, Events, messageHistory, ollama, Keys.defaultModel)
|
||||
|
||||
// Try to connect to redis
|
||||
await redis.connect()
|
||||
.then(() => console.log('[Redis] Connected'))
|
||||
.catch((error) => {
|
||||
console.error('[Redis] Connection Error', error)
|
||||
process.exit(1)
|
||||
.then(response => {
|
||||
console.log('[Redis] Successfully Connected')
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('[Redis] Connection Error. See error below:\n', error)
|
||||
console.warn('[Redis] Failed to connect to Redis Database, using local system')
|
||||
// TODO: create boolean flag that will probably be used in messageCreate.ts if redis database is down
|
||||
// When implementing this boolean flag, move connection to database BEFORE the registerEvents method
|
||||
})
|
||||
|
||||
// Try to log in the client
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Client, CommandInteraction, ApplicationCommandOptionType, MessageFlags } from 'discord.js'
|
||||
import { Client, ChatInputCommandInteraction, ApplicationCommandOptionType, MessageFlags } from 'discord.js'
|
||||
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||
|
||||
export const Capacity: SlashCommand = {
|
||||
@@ -16,14 +16,14 @@ export const Capacity: SlashCommand = {
|
||||
],
|
||||
|
||||
// Query for message information and set the style
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
|
||||
// fetch channel and message
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
if (!channel || !UserCommand.includes(channel.type)) return
|
||||
|
||||
// set state of bot chat features
|
||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName,
|
||||
interaction.options.get('context-capacity')?.value
|
||||
interaction.options.getNumber('context-capacity')
|
||||
)
|
||||
|
||||
interaction.reply({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction, MessageFlags } from 'discord.js'
|
||||
import { ApplicationCommandOptionType, ChatInputCommandInteraction, Client, CommandInteraction, MessageFlags } from 'discord.js'
|
||||
import { UserCommand, SlashCommand } from '../utils/index.js'
|
||||
import { ollama } from '../client.js'
|
||||
import { ModelResponse } from 'ollama'
|
||||
@@ -18,10 +18,11 @@ export const DeleteModel: SlashCommand = {
|
||||
],
|
||||
|
||||
// Delete Model locally stored
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
|
||||
// defer reply to avoid timeout
|
||||
await interaction.deferReply()
|
||||
const modelInput: string = interaction.options.get('model-name')!!.value as string
|
||||
const modelInput: string = interaction.options.getString('model-name') as string
|
||||
let ollamaOffline: boolean = false
|
||||
|
||||
// fetch channel and message
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
@@ -37,9 +38,22 @@ export const DeleteModel: SlashCommand = {
|
||||
}
|
||||
|
||||
// check if model exists
|
||||
const modelExists: boolean = await ollama.list()
|
||||
const modelExists = await ollama.list()
|
||||
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
||||
.catch(error => {
|
||||
ollamaOffline = true
|
||||
console.error(`[Command: delete-model] Failed to connect with Ollama service. Error: ${error.message}`)
|
||||
})
|
||||
|
||||
// Validate for any issue or if service is running
|
||||
if (ollamaOffline) {
|
||||
interaction.editReply({
|
||||
content: `The Ollama service is not running. Please turn on/download the [service](https://ollama.com/).`
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// call ollama to delete model
|
||||
if (modelExists) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Client, CommandInteraction, ApplicationCommandOptionType, MessageFlags } from 'discord.js'
|
||||
import { Client, ChatInputCommandInteraction, ApplicationCommandOptionType, MessageFlags } from 'discord.js'
|
||||
import { AdminCommand, openConfig, SlashCommand } from '../utils/index.js'
|
||||
|
||||
export const Disable: SlashCommand = {
|
||||
@@ -16,7 +16,7 @@ export const Disable: SlashCommand = {
|
||||
],
|
||||
|
||||
// Query for message information and set the style
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
|
||||
// fetch channel and message
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
if (!channel || !AdminCommand.includes(channel.type)) return
|
||||
@@ -32,11 +32,11 @@ export const Disable: SlashCommand = {
|
||||
|
||||
// set state of bot chat features
|
||||
openConfig(`${interaction.guildId}-config.json`, interaction.commandName,
|
||||
interaction.options.get('enabled')?.value
|
||||
interaction.options.getBoolean('enabled')
|
||||
)
|
||||
|
||||
interaction.reply({
|
||||
content: `${client.user?.username} is now **${interaction.options.get('enabled')?.value ? "enabled" : "disabled"}**.`,
|
||||
content: `${client.user?.username} is now **${interaction.options.getBoolean('enabled') ? "enabled" : "disabled"}**.`,
|
||||
flags: MessageFlags.Ephemeral
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction, MessageFlags } from 'discord.js'
|
||||
import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction, MessageFlags } from 'discord.js'
|
||||
import { openConfig, SlashCommand, UserCommand } from '../utils/index.js'
|
||||
|
||||
export const MessageStream: SlashCommand = {
|
||||
@@ -16,18 +16,18 @@ export const MessageStream: SlashCommand = {
|
||||
],
|
||||
|
||||
// change preferences based on command
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
|
||||
// verify channel
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
if (!channel || !UserCommand.includes(channel.type)) return
|
||||
|
||||
// save value to json and write to it
|
||||
openConfig(`${interaction.user.username}-config.json`, interaction.commandName,
|
||||
interaction.options.get('stream')?.value
|
||||
interaction.options.getBoolean('stream')
|
||||
)
|
||||
|
||||
interaction.reply({
|
||||
content: `Message streaming is now set to: \`${interaction.options.get('stream')?.value}\``,
|
||||
content: `Message streaming is now set to: \`${interaction.options.getBoolean('stream')}\``,
|
||||
flags: MessageFlags.Ephemeral
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction, MessageFlags } from "discord.js"
|
||||
import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction, MessageFlags } from "discord.js"
|
||||
import { ollama } from "../client.js"
|
||||
import { ModelResponse } from "ollama"
|
||||
import { UserCommand, SlashCommand } from "../utils/index.js"
|
||||
@@ -18,10 +18,11 @@ export const PullModel: SlashCommand = {
|
||||
],
|
||||
|
||||
// Pull for model from Ollama library
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
|
||||
// defer reply to avoid timeout
|
||||
await interaction.deferReply()
|
||||
const modelInput: string = interaction.options.get('model-to-pull')!!.value as string
|
||||
const modelInput: string = interaction.options.getString('model-to-pull') as string
|
||||
let ollamaOffline: boolean = false
|
||||
|
||||
// fetch channel and message
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
@@ -36,9 +37,22 @@ export const PullModel: SlashCommand = {
|
||||
return
|
||||
}
|
||||
|
||||
// check if model was already pulled
|
||||
const modelExists: boolean = await ollama.list()
|
||||
// check if model was already pulled, if the ollama service isn't running throw error
|
||||
const modelExists = await ollama.list()
|
||||
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
|
||||
.catch(error => {
|
||||
ollamaOffline = true
|
||||
console.error(`[Command: pull-model] Failed to connect with Ollama service. Error: ${error.message}`)
|
||||
})
|
||||
|
||||
// Validate for any issue or if service is running
|
||||
if (ollamaOffline) {
|
||||
interaction.editReply({
|
||||
content: `The Ollama service is not running. Please turn on/download the [service](https://ollama.com/).`
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
// call ollama to pull desired model
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ApplicationCommandOptionType, Client, CommandInteraction } from "discord.js"
|
||||
import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction } from "discord.js"
|
||||
import { ollama } from "../client.js"
|
||||
import { ModelResponse } from "ollama"
|
||||
import { openConfig, UserCommand, SlashCommand } from "../utils/index.js"
|
||||
@@ -18,10 +18,10 @@ export const SwitchModel: SlashCommand = {
|
||||
],
|
||||
|
||||
// Switch user preferred model if available in local library
|
||||
run: async (client: Client, interaction: CommandInteraction) => {
|
||||
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
|
||||
await interaction.deferReply()
|
||||
|
||||
const modelInput: string = interaction.options.get('model-to-use')!!.value as string
|
||||
const modelInput: string = interaction.options.getString('model-to-use') as string
|
||||
|
||||
// fetch channel and message
|
||||
const channel = await client.channels.fetch(interaction.channelId)
|
||||
@@ -45,6 +45,9 @@ export const SwitchModel: SlashCommand = {
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(`[Command: switch-model] Failed to connect with Ollama service. Error: ${error.message}`)
|
||||
})
|
||||
// todo: problem can be here if async messes up
|
||||
if (switchSuccess) {
|
||||
// set model now that it exists
|
||||
@@ -56,10 +59,13 @@ export const SwitchModel: SlashCommand = {
|
||||
interaction.editReply({
|
||||
content: `Could not find **${modelInput}** in local model library.\n\nPlease contact an server admin for access to this model.`
|
||||
})
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
// could not resolve user model switch
|
||||
if (error.message.includes("fetch failed") as string)
|
||||
error.message = "The Ollama service is not running. Please turn on/download the [service](https://ollama.com/)."
|
||||
|
||||
interaction.editReply({
|
||||
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to request an server admin run \`/pull-model ${modelInput}\` and try again.`
|
||||
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error.message}`
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
46
src/components/binder.ts
Normal file
46
src/components/binder.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
/**
|
||||
* @class Logger
|
||||
* @description A class to handle logging messages
|
||||
* @method log
|
||||
*/
|
||||
export class Logger {
|
||||
private logPrefix: string = ''
|
||||
private type: string = 'log'
|
||||
|
||||
private constructPrefix(component?: string, method?: string): string {
|
||||
let prefix = this.type.toUpperCase()
|
||||
|
||||
if (component) {
|
||||
prefix += ` [${component}`
|
||||
if (method) prefix += `: ${method}`
|
||||
prefix += ']'
|
||||
}
|
||||
|
||||
return prefix
|
||||
}
|
||||
|
||||
public bind(component?: string, method?: string): CallableFunction {
|
||||
let tempPrefix = this.constructPrefix(component, method)
|
||||
|
||||
if (tempPrefix !== this.logPrefix) this.logPrefix = tempPrefix
|
||||
|
||||
switch (this.type) {
|
||||
case 'warn':
|
||||
return console.warn.bind(console, this.logPrefix)
|
||||
case 'error':
|
||||
return console.error.bind(console, this.logPrefix)
|
||||
case 'log':
|
||||
default:
|
||||
return console.log.bind(console, this.logPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
public log(type: string, message: unknown, component?: string, method?: string): void {
|
||||
if (type && type !== this.type) this.type = type
|
||||
|
||||
let log = this.bind(component, method)
|
||||
|
||||
log(message)
|
||||
}
|
||||
}
|
||||
2
src/components/index.ts
Normal file
2
src/components/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './queue.js'
|
||||
export * from './binder.js'
|
||||
@@ -17,7 +17,7 @@ export class Queue<T> implements IQueue<T> {
|
||||
* Set up Queue
|
||||
* @param capacity max length of queue
|
||||
*/
|
||||
constructor(public capacity: number = 5) {}
|
||||
constructor(public capacity: number = 5) { }
|
||||
|
||||
/**
|
||||
* Put item in front of queue
|
||||
@@ -6,15 +6,14 @@ import commands from '../commands/index.js'
|
||||
* @param interaction the interaction received from the server
|
||||
*/
|
||||
export default event(Events.InteractionCreate, async ({ log, client }, interaction) => {
|
||||
if (!interaction.isCommand() || !interaction.isChatInputCommand()) return
|
||||
if (!interaction.isCommand() || !interaction.isChatInputCommand()) return
|
||||
|
||||
log(`Interaction called \'${interaction.commandName}\' from ${interaction.user.tag}.`)
|
||||
log(`Interaction called \'${interaction.commandName}\' from ${interaction.user.tag}.`)
|
||||
|
||||
// ensure command exists, otherwise kill event
|
||||
const command = commands.find(command => command.name === interaction.commandName)
|
||||
if (!command) return
|
||||
// ensure command exists, otherwise kill event
|
||||
const command = commands.find(command => command.name === interaction.commandName)
|
||||
if (!command) return
|
||||
|
||||
// the command exists, execute it
|
||||
command.run(client, interaction)
|
||||
}
|
||||
)
|
||||
// the command exists, execute it
|
||||
command.run(client, interaction)
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { TextChannel } from 'discord.js'
|
||||
import { event, Events, normalMessage, UserMessage, clean } from '../utils/index.js'
|
||||
import {
|
||||
event, Events, normalMessage, UserMessage, clean,
|
||||
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
|
||||
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
|
||||
} from '../utils/index.js'
|
||||
@@ -16,129 +16,128 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
let cleanedMessage = clean(message.content, clientId)
|
||||
log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
||||
|
||||
// Do not respond if bot talks in the chat
|
||||
if (message.author.username === message.client.user.username) return
|
||||
// Do not respond if bot talks in the chat
|
||||
if (message.author.username === message.client.user.username) return
|
||||
|
||||
// Only respond if message mentions the bot
|
||||
if (!message.mentions.has(clientId)) return
|
||||
// Only respond if message mentions the bot
|
||||
if (!message.mentions.has(clientId)) return
|
||||
|
||||
// default stream to false
|
||||
let shouldStream = false
|
||||
// default stream to false
|
||||
let shouldStream = false
|
||||
|
||||
// Params for Preferences Fetching
|
||||
const maxRetries = 3
|
||||
const delay = 1000 // in millisecons
|
||||
// Params for Preferences Fetching
|
||||
const maxRetries = 3
|
||||
const delay = 1000 // in millisecons
|
||||
|
||||
try {
|
||||
// Retrieve Server/Guild Preferences
|
||||
let attempt = 0
|
||||
while (attempt < maxRetries) {
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
||||
// check if config.json exists
|
||||
if (config === undefined) {
|
||||
// Allowing chat options to be available
|
||||
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
||||
reject(new Error('Failed to locate or create Server Preferences\n\nPlease try chatting again...'))
|
||||
}
|
||||
|
||||
// check if chat is disabled
|
||||
else if (!config.options['toggle-chat'])
|
||||
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
||||
else
|
||||
resolve(config)
|
||||
})
|
||||
})
|
||||
break // successful
|
||||
} catch (error) {
|
||||
++attempt
|
||||
if (attempt < maxRetries) {
|
||||
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`)
|
||||
await new Promise(ret => setTimeout(ret, delay))
|
||||
} else
|
||||
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
|
||||
}
|
||||
}
|
||||
|
||||
// Reset attempts for User preferences
|
||||
attempt = 0
|
||||
let userConfig: UserConfig | undefined
|
||||
|
||||
while (attempt < maxRetries) {
|
||||
try {
|
||||
// Retrieve User Preferences
|
||||
userConfig = await new Promise((resolve, reject) => {
|
||||
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
||||
if (config === undefined) {
|
||||
openConfig(`${message.author.username}-config.json`, 'message-style', false)
|
||||
openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel)
|
||||
reject(new Error('No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.'))
|
||||
return
|
||||
}
|
||||
|
||||
// check if there is a set capacity in config
|
||||
else 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
|
||||
|
||||
if (typeof config.options['switch-model'] !== 'string')
|
||||
reject(new Error(`No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.`))
|
||||
try {
|
||||
// Retrieve Server/Guild Preferences
|
||||
let attempt = 0
|
||||
while (attempt < maxRetries) {
|
||||
try {
|
||||
await new Promise((resolve, reject) => {
|
||||
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
||||
// check if config.json exists
|
||||
if (config === undefined) {
|
||||
// Allowing chat options to be available
|
||||
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
||||
reject(new Error('Failed to locate or create Server Preferences\n\nPlease try chatting again...'))
|
||||
}
|
||||
|
||||
// check if chat is disabled
|
||||
else if (!config.options['toggle-chat'])
|
||||
reject(new Error('Admin(s) have disabled chat features.\n\n Please contact your server\'s admin(s).'))
|
||||
else
|
||||
resolve(config)
|
||||
})
|
||||
})
|
||||
break // successful
|
||||
} catch (error) {
|
||||
++attempt
|
||||
if (attempt < maxRetries) {
|
||||
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`)
|
||||
await new Promise(ret => setTimeout(ret, delay))
|
||||
} else
|
||||
throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
|
||||
}
|
||||
})
|
||||
break // successful
|
||||
} catch (error) {
|
||||
++attempt
|
||||
if (attempt < maxRetries) {
|
||||
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`)
|
||||
await new Promise(ret => setTimeout(ret, delay))
|
||||
} else
|
||||
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
|
||||
}
|
||||
}
|
||||
|
||||
// need new check for "open/active" threads/channels here!
|
||||
let chatMessages: UserMessage[] = await new Promise((resolve) => {
|
||||
// set new queue to modify
|
||||
// Reset attempts for User preferences
|
||||
attempt = 0
|
||||
let userConfig: UserConfig | undefined
|
||||
|
||||
while (attempt < maxRetries) {
|
||||
try {
|
||||
// Retrieve User Preferences
|
||||
userConfig = await new Promise((resolve, reject) => {
|
||||
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
||||
if (config === undefined) {
|
||||
openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel)
|
||||
reject(new Error(`No User Preferences is set up.\n\nCreating new preferences file for ${message.author.username}\nPlease try chatting again.`))
|
||||
return
|
||||
}
|
||||
|
||||
// check if there is a set capacity in config
|
||||
else 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
|
||||
|
||||
if (typeof config.options['switch-model'] !== 'string')
|
||||
reject(new Error(`No Model was set. Please set a model by running \`/switch-model <model of choice>\`.\n\nIf you do not have any models. Run \`/pull-model <model name>\`.`))
|
||||
|
||||
resolve(config)
|
||||
})
|
||||
})
|
||||
break // successful
|
||||
} catch (error) {
|
||||
++attempt
|
||||
if (attempt < maxRetries) {
|
||||
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`)
|
||||
await new Promise(ret => setTimeout(ret, delay))
|
||||
} else
|
||||
throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
|
||||
}
|
||||
}
|
||||
|
||||
// need new check for "open/active" threads/channels here!
|
||||
let chatMessages: UserMessage[] = await new Promise((resolve) => {
|
||||
// set new queue to modify
|
||||
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
|
||||
if (channelInfo?.messages)
|
||||
resolve(channelInfo.messages)
|
||||
else {
|
||||
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
|
||||
resolve([])
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
if (chatMessages.length === 0) {
|
||||
chatMessages = await new Promise((resolve, reject) => {
|
||||
openChannelInfo(message.channelId,
|
||||
message.channel as TextChannel,
|
||||
message.author.tag
|
||||
)
|
||||
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
|
||||
if (channelInfo?.messages)
|
||||
resolve(channelInfo.messages)
|
||||
else {
|
||||
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
|
||||
resolve([])
|
||||
reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (chatMessages.length === 0) {
|
||||
chatMessages = await new Promise((resolve, reject) => {
|
||||
openChannelInfo(message.channelId,
|
||||
message.channel as TextChannel,
|
||||
message.author.tag
|
||||
)
|
||||
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
|
||||
if (channelInfo?.messages)
|
||||
resolve(channelInfo.messages)
|
||||
else {
|
||||
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`)
|
||||
reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
if (!userConfig)
|
||||
throw new Error(`Failed to initialize User Preference for **${message.author.username}**.\n\nIt's likely you do not have a model set. Please use the \`switch-model\` command to do that.`)
|
||||
if (!userConfig)
|
||||
throw new Error(`Failed to initialize User Preference for **${message.author.username}**.\n\nIt's likely you do not have a model set. Please use the \`switch-model\` command to do that.`)
|
||||
|
||||
// get message attachment if exists
|
||||
const attachment = message.attachments.first()
|
||||
@@ -148,47 +147,46 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client
|
||||
cleanedMessage += await getTextFileAttachmentData(attachment)
|
||||
else if (attachment)
|
||||
messageAttachment = await getAttachmentData(attachment)
|
||||
|
||||
|
||||
const model: string = userConfig.options['switch-model']
|
||||
|
||||
// set up new queue
|
||||
msgHist.setQueue(chatMessages)
|
||||
// set up new queue
|
||||
msgHist.setQueue(chatMessages)
|
||||
|
||||
// check if we can push, if not, remove oldest
|
||||
while (msgHist.size() >= msgHist.capacity) msgHist.dequeue()
|
||||
// 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: cleanedMessage,
|
||||
images: messageAttachment || []
|
||||
})
|
||||
// push user response before ollama query
|
||||
msgHist.enqueue({
|
||||
role: 'user',
|
||||
content: cleanedMessage,
|
||||
images: messageAttachment || []
|
||||
})
|
||||
|
||||
// response string for ollama to put its response
|
||||
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream)
|
||||
// response string for ollama to put its response
|
||||
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream)
|
||||
|
||||
// If something bad happened, remove user query and stop
|
||||
if (response == undefined) { msgHist.pop(); return }
|
||||
// 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()
|
||||
// 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,
|
||||
images: messageAttachment || []
|
||||
})
|
||||
// successful query, save it in context history
|
||||
msgHist.enqueue({
|
||||
role: 'assistant',
|
||||
content: response,
|
||||
images: messageAttachment || []
|
||||
})
|
||||
|
||||
// only update the json on success
|
||||
openChannelInfo(message.channelId,
|
||||
message.channel as TextChannel,
|
||||
message.author.tag,
|
||||
msgHist.getItems()
|
||||
)
|
||||
} catch (error: any) {
|
||||
msgHist.pop() // remove message because of failure
|
||||
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
||||
}
|
||||
// only update the json on success
|
||||
openChannelInfo(message.channelId,
|
||||
message.channel as TextChannel,
|
||||
message.author.tag,
|
||||
msgHist.getItems()
|
||||
)
|
||||
} catch (error: any) {
|
||||
msgHist.pop() // remove message because of failure
|
||||
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
||||
}
|
||||
)
|
||||
})
|
||||
@@ -4,15 +4,14 @@ import commands from '../commands/index.js'
|
||||
|
||||
// Log when the bot successfully logs in and export it
|
||||
export default event(Events.ClientReady, ({ log }, client) => {
|
||||
// Register the commands associated with the bot upon loggin in
|
||||
registerCommands(client, commands)
|
||||
// Register the commands associated with the bot upon loggin in
|
||||
registerCommands(client, commands)
|
||||
|
||||
// set status of the bot
|
||||
client.user.setActivity({
|
||||
name: 'Powered by Ollama',
|
||||
type: ActivityType.Custom
|
||||
})
|
||||
// set status of the bot
|
||||
client.user.setActivity({
|
||||
name: 'Powered by Ollama',
|
||||
type: ActivityType.Custom
|
||||
})
|
||||
|
||||
log(`Logged in as ${client.user.username}.`)
|
||||
}
|
||||
)
|
||||
log(`Logged in as ${client.user.username}.`)
|
||||
})
|
||||
@@ -6,36 +6,35 @@ import fs from 'fs'
|
||||
* Event to remove the associated .json file for a thread once deleted
|
||||
*/
|
||||
export default event(Events.ThreadDelete, async ({ log }, thread: ThreadChannel) => {
|
||||
// iterate through every guild member in the thread and delete their history, except the bot
|
||||
try {
|
||||
log(`Number of User Guild Members in Thread being deleted: ${thread.memberCount!! - 1}`)
|
||||
const dirPath = 'data/'
|
||||
// iterate through every guild member in the thread and delete their history, except the bot
|
||||
try {
|
||||
log(`Number of User Guild Members in Thread being deleted: ${thread.memberCount!! - 1}`)
|
||||
const dirPath = 'data/'
|
||||
|
||||
// read all files in data/
|
||||
fs.readdir(dirPath, (error, files) => {
|
||||
if (error) {
|
||||
log(`Error reading directory ${dirPath}`, error)
|
||||
return
|
||||
}
|
||||
// read all files in data/
|
||||
fs.readdir(dirPath, (error, files) => {
|
||||
if (error) {
|
||||
log(`Error reading directory ${dirPath}`, error)
|
||||
return
|
||||
}
|
||||
|
||||
// filter files by thread id being deleted
|
||||
const filesToDiscard = files.filter(
|
||||
file => file.startsWith(`${thread.id}-`) &&
|
||||
file.endsWith('.json'))
|
||||
// filter files by thread id being deleted
|
||||
const filesToDiscard = files.filter(
|
||||
file => file.startsWith(`${thread.id}-`) &&
|
||||
file.endsWith('.json'))
|
||||
|
||||
// remove files by unlinking
|
||||
filesToDiscard.forEach(file => {
|
||||
const filePath = dirPath + file
|
||||
fs.unlink(filePath, error => {
|
||||
if (error)
|
||||
log(`Error deleting file ${filePath}`, error)
|
||||
else
|
||||
log(`Successfully deleted ${filePath} thread information`)
|
||||
})
|
||||
// remove files by unlinking
|
||||
filesToDiscard.forEach(file => {
|
||||
const filePath = dirPath + file
|
||||
fs.unlink(filePath, error => {
|
||||
if (error)
|
||||
log(`Error deleting file ${filePath}`, error)
|
||||
else
|
||||
log(`Successfully deleted ${filePath} thread information`)
|
||||
})
|
||||
})
|
||||
} catch (error) {
|
||||
log(`Issue deleting user history files from ${thread.id}`)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
log(`Issue deleting user history files from ${thread.id}`)
|
||||
}
|
||||
)
|
||||
})
|
||||
@@ -1,4 +1,4 @@
|
||||
import { CommandInteraction, ChatInputApplicationCommandData, Client, ApplicationCommandOption } from 'discord.js'
|
||||
import { ChatInputCommandInteraction, ChatInputApplicationCommandData, Client, ApplicationCommandOption } from 'discord.js'
|
||||
|
||||
/**
|
||||
* interface for how slash commands should be run
|
||||
@@ -6,7 +6,7 @@ import { CommandInteraction, ChatInputApplicationCommandData, Client, Applicatio
|
||||
export interface SlashCommand extends ChatInputApplicationCommandData {
|
||||
run: (
|
||||
client: Client,
|
||||
interaction: CommandInteraction,
|
||||
interaction: ChatInputCommandInteraction,
|
||||
options?: ApplicationCommandOption[]
|
||||
) => void
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { ClientEvents, Awaitable, Client } from 'discord.js'
|
||||
import { Ollama } from 'ollama'
|
||||
import { Queue } from '../queues/queue.js'
|
||||
import { Queue } from '../components/index.js'
|
||||
|
||||
// Export events through here to reduce amount of imports
|
||||
export { Events } from 'discord.js'
|
||||
|
||||
@@ -64,7 +64,7 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
|
||||
* @param user the user's name
|
||||
* @param messages their messages
|
||||
*/
|
||||
export async function openChannelInfo(filename: string, channel: TextChannel | ThreadChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
||||
export async function openChannelInfo(this: any, filename: string, channel: TextChannel | ThreadChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
||||
const fullFileName = `data/${filename}-${user}.json`
|
||||
if (fs.existsSync(fullFileName)) {
|
||||
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
||||
@@ -95,7 +95,7 @@ export async function openChannelInfo(filename: string, channel: TextChannel | T
|
||||
|
||||
// only creating it, no need to add anything
|
||||
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
||||
console.log(`[Util: openChannelInfo] Created '${fullFileName}' in working directory`)
|
||||
console.log(`[Util: ${this.name}] Created '${fullFileName}' in working directory`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import path from 'path'
|
||||
* @param value new value to assign
|
||||
*/
|
||||
// add type of change (server, user)
|
||||
export function openConfig(filename: string, key: string, value: any) {
|
||||
export function openConfig(this: any, filename: string, key: string, value: any) {
|
||||
const fullFileName = `data/${filename}`
|
||||
|
||||
// check if the file exists, if not then make the config file
|
||||
@@ -41,7 +41,7 @@ export function openConfig(filename: string, key: string, value: any) {
|
||||
fs.mkdirSync(directory, { recursive: true })
|
||||
|
||||
fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2))
|
||||
console.log(`[Util: openConfig] Created '${filename}' in working directory`)
|
||||
console.log(`[Util: ${this.name}] Created '${filename}' in working directory`)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { ChatResponse } from "ollama"
|
||||
import { ChatResponse, AbortableAsyncIterator } from "ollama"
|
||||
import { ChatParams } from "../index.js"
|
||||
import { AbortableAsyncIterator } from "ollama/src/utils.js"
|
||||
|
||||
/**
|
||||
* Method to query the Ollama client for async generation
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { Message, SendableChannels } from 'discord.js'
|
||||
import { ChatResponse, Ollama } from 'ollama'
|
||||
import { ChatResponse, Ollama, AbortableAsyncIterator } from 'ollama'
|
||||
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
||||
import { Queue } from '../queues/queue.js'
|
||||
import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
||||
import { Queue } from '../components/index.js'
|
||||
|
||||
/**
|
||||
* Method to send replies as normal text on discord like any other user
|
||||
@@ -11,6 +10,7 @@ import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
||||
* @param msgHist message history between user and model
|
||||
*/
|
||||
export async function normalMessage(
|
||||
this: any,
|
||||
message: Message,
|
||||
ollama: Ollama,
|
||||
model: string,
|
||||
@@ -73,7 +73,7 @@ export async function normalMessage(
|
||||
sentMessage.edit(result)
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`[Util: messageNormal] Error creating message: ${error.message}`)
|
||||
console.log(`[Util: ${this.name}] Error creating message: ${error.message}`)
|
||||
if (error.message.includes('try pulling it first'))
|
||||
sentMessage.edit(`**Response generation failed.**\n\nReason: You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`)
|
||||
else
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { Queue } from '../src/queues/queue.js'
|
||||
import { Queue } from '../src/components/index.js'
|
||||
|
||||
/**
|
||||
* Queue test suite, tests the Queue class
|
||||
|
||||
Reference in New Issue
Block a user