Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ccd1a012e | ||
|
|
68a5e097fe | ||
|
|
624ff2e5c8 | ||
|
|
9f61f6bc6c | ||
|
|
5d02800c3f | ||
|
|
5061dab335 | ||
|
|
947ff89958 | ||
|
|
6a9ee2d6d0 | ||
|
|
e3b0c9abe4 | ||
|
|
36a0cd309b | ||
|
|
b49b464afb | ||
|
|
2caf54346a | ||
|
|
6e6467c2a5 | ||
|
|
b463b0a8cb |
@@ -1,12 +1,6 @@
|
|||||||
# Discord token for the bot
|
# Discord token for the bot
|
||||||
CLIENT_TOKEN = BOT_TOKEN
|
CLIENT_TOKEN = BOT_TOKEN
|
||||||
|
|
||||||
# model for the bot to query from (i.e. llama2 [llama2:13b], mistral, codellama, etc... )
|
|
||||||
MODEL = MODEL_NAME
|
|
||||||
|
|
||||||
# discord bot user id for mentions
|
|
||||||
CLIENT_UID = BOT_USER_ID
|
|
||||||
|
|
||||||
# ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local
|
# ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local
|
||||||
OLLAMA_IP = IP_ADDRESS
|
OLLAMA_IP = IP_ADDRESS
|
||||||
OLLAMA_PORT = PORT
|
OLLAMA_PORT = PORT
|
||||||
|
|||||||
26
.github/CONTRIBUTING.md
vendored
26
.github/CONTRIBUTING.md
vendored
@@ -1,31 +1,39 @@
|
|||||||
<!--
|
<!--
|
||||||
Author: Kevin Dang
|
Author: Kevin Dang
|
||||||
Date: 1-30-2024
|
Date: 1-30-2024
|
||||||
|
Changes:
|
||||||
|
10-01-2024 - Jonathan Smoley
|
||||||
-->
|
-->
|
||||||
|
|
||||||
|
## Naming Conventions
|
||||||
|
* Branches
|
||||||
|
* prefix your branch name with the type of contribution:
|
||||||
|
* features: `'feature/**'`
|
||||||
|
* releases: `'releases/**'`
|
||||||
|
* bugs: `'bug/**'`
|
||||||
|
* docs: `'docs/**'`
|
||||||
|
|
||||||
## Run the Bot
|
## Run the Bot
|
||||||
* Refer to all sections below before running the bot.
|
* Refer to all sections below before running the bot.
|
||||||
* You should now have `Ollama`, `NodeJS`, ran `npm install`.
|
* You should now have `Ollama`, `NodeJS`, ran `npm install`.
|
||||||
* You will also need a discord bot to run. Refer to the [developer portal](https://discord.com/developers/) to learn how to set one up and invite it to your server. If that does not help then look up a YouTube video like this [one](https://www.youtube.com/watch?v=KZ3tIGHU314&ab_channel=UnderCtrl).
|
* You will also need a discord bot to run. Refer to the [developer portal](https://discord.com/developers/) to learn how to set one up and invite it to your server. If that does not help then look up a YouTube video like this [one](https://www.youtube.com/watch?v=KZ3tIGHU314&ab_channel=UnderCtrl).
|
||||||
* Now run `npm run start` to run the client and ollama at the same time (this must be one in wsl or a Linux distro)
|
* Now run `npm run client` to run the client (this must be done in wsl or a Linux distro)
|
||||||
|
|
||||||
|
|
||||||
## Set up (Development-side)
|
## Set up (Development-side)
|
||||||
* Pull the repository using `https://github.com/kevinthedang/discord-ollama.git`.
|
* Pull the repository using `https://github.com/kevinthedang/discord-ollama.git`.
|
||||||
* Refer to `Ollama Setup` in the readme to set up Ollama.
|
* Refer to `Environment Setup` in the readme to set up Ollama.
|
||||||
* This must be set up in a Linux environment or wsl2.
|
* This must be set up in a Linux environment or wsl2.
|
||||||
* Install NodeJS `v18.18.2`
|
* Install NodeJS `v18.18.2`
|
||||||
* You can check out `Resources` and `To Run` in the readme for a bit of help.
|
* You can check out `Resources` in the readme for a bit of help.
|
||||||
* You can also reference [NodeJS Setup](#nodejs-setup)
|
* You can also reference [NodeJS Setup](#nodejs-setup)
|
||||||
* When you have the project pulled from github, open up a terminal and run `npm i` or `npm install` to get all of the packages for the project.
|
* When you have the project pulled from github, open up a terminal and run `npm i` or `npm install` to get all of the packages for the project.
|
||||||
* In some kind of terminal (`git bash` is good) to run the client. You can run Ollama but opening up wsl2 and typing `ollama serve`.
|
* In some kind of terminal (`git bash` is good) to run the client. You can run Ollama but opening up wsl2 and typing `ollama serve`.
|
||||||
* Refer to `Ollama Setup` if there are any issues.
|
* Refer to `Ollama Setup` if there are any issues.
|
||||||
|
|
||||||
## Environment
|
## Environment
|
||||||
* You will need two environment files:
|
* You will need an environment file:
|
||||||
* `.env`: for running the bot
|
* `.env`: for running the bot
|
||||||
* Please refer to `.env.sample` for all environment variables to include
|
* Please refer to `.env.sample` for all environment variables to include
|
||||||
* `.env.dev.local`: also runs the bot, but with development variables
|
|
||||||
* Currently there are no differences between the two, but when needed, you may add environment variables as needed.
|
|
||||||
|
|
||||||
## NodeJS Setup
|
## NodeJS Setup
|
||||||
* Install [nvm](https://github.com/nvm-sh/nvm?tab=readme-ov-file#installing-and-updating) using `curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash`
|
* Install [nvm](https://github.com/nvm-sh/nvm?tab=readme-ov-file#installing-and-updating) using `curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash`
|
||||||
|
|||||||
6
.github/workflows/build.yml
vendored
6
.github/workflows/build.yml
vendored
@@ -31,9 +31,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
echo GUILD_ID = ${{ secrets.GUILD_ID }} >> .env
|
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
|
||||||
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
|
||||||
@@ -60,9 +57,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
echo GUILD_ID = ${{ secrets.GUILD_ID }} >> .env
|
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
|
||||||
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
|
||||||
|
|||||||
49
.github/workflows/coverage.yml
vendored
Normal file
49
.github/workflows/coverage.yml
vendored
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
name: Coverage
|
||||||
|
run-name: Code Coverage
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Discord-Node-Coverage:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
environment: coverage
|
||||||
|
timeout-minutes: 2
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Node Environment lts/hydrogen
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: lts/hydrogen
|
||||||
|
cache: "npm"
|
||||||
|
|
||||||
|
- name: Install Project Dependencies
|
||||||
|
run: |
|
||||||
|
npm install
|
||||||
|
|
||||||
|
- name: Create Environment Variables
|
||||||
|
run: |
|
||||||
|
touch .env
|
||||||
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
|
||||||
|
- name: Collect Code Coverage
|
||||||
|
run: |
|
||||||
|
LINE_PCT=$(npm run test:coverage | tail -2 | head -1 | awk '{print $3}')
|
||||||
|
echo "COVERAGE=$LINE_PCT" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Upload Code Coverage
|
||||||
|
uses: schneegans/dynamic-badges-action@v1.7.0
|
||||||
|
with:
|
||||||
|
auth: ${{ secrets.GIST_SECRET }}
|
||||||
|
gistID: ${{ vars.GIST_ID }}
|
||||||
|
filename: coverage.json
|
||||||
|
label: Coverage
|
||||||
|
message: ${{ env.COVERAGE }}
|
||||||
|
valColorRange: ${{ env.COVERAGE }}
|
||||||
|
maxColorRange: 100
|
||||||
|
minColorRange: 0
|
||||||
7
.github/workflows/release.yml
vendored
7
.github/workflows/release.yml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: release
|
name: Deploy
|
||||||
run-name: Release Docker Image
|
run-name: Release Docker Image
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@@ -6,7 +6,7 @@ on:
|
|||||||
- 'v*'
|
- 'v*'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
Deploy-Image:
|
Release-Docker-Image:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
environment: release
|
environment: release
|
||||||
timeout-minutes: 3
|
timeout-minutes: 3
|
||||||
@@ -24,9 +24,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = NOT_REAL_TOKEN >> .env
|
echo CLIENT_TOKEN = NOT_REAL_TOKEN >> .env
|
||||||
echo GUILD_ID = 123456 >> .env
|
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
|
||||||
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
|
||||||
|
|||||||
3
.github/workflows/test.yml
vendored
3
.github/workflows/test.yml
vendored
@@ -39,9 +39,6 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
touch .env
|
touch .env
|
||||||
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
|
||||||
echo GUILD_ID = ${{ secrets.GUILD_ID }} >> .env
|
|
||||||
echo MODEL = ${{ secrets.MODEL }} >> .env
|
|
||||||
echo CLIENT_UID = ${{ secrets.CLIENT_UID }} >> .env
|
|
||||||
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env
|
||||||
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
|
||||||
|
|
||||||
|
|||||||
24
README.md
24
README.md
@@ -5,7 +5,9 @@
|
|||||||
<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>
|
<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/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/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/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/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>
|
</div>
|
||||||
|
|
||||||
## About/Goals
|
## About/Goals
|
||||||
@@ -23,36 +25,34 @@ The project aims to:
|
|||||||
* [x] User vs. Server Preferences
|
* [x] User vs. Server Preferences
|
||||||
* [ ] Redis Caching
|
* [ ] Redis Caching
|
||||||
* [x] Administrator Role Compatible
|
* [x] Administrator Role Compatible
|
||||||
* [x] Multi-User Chat Generation (Multiple users chatting at the same time) - This was built into from Ollama `v0.2.1+`
|
* [x] Multi-User Chat Generation (Multiple users chatting at the same time) - This was built in from Ollama `v0.2.1+`
|
||||||
* [ ] Automatic and Manual model pulling through the Discord client
|
* [x] Automatic and Manual model pulling through the Discord client
|
||||||
* [ ] Allow others to create their own models personalized for their own servers!
|
* [ ] Allow others to create their own models personalized for their own servers!
|
||||||
* [ ] Documentation on creating your own LLM
|
* [ ] Documentation on creating your own LLM
|
||||||
* [ ] Documentation on web scrapping and cleaning
|
* [ ] Documentation on web scrapping and cleaning
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
These are guides to the feature set included and the events triggered in this app.
|
||||||
|
* [User Slash Commands](./docs/commands-guide.md)
|
||||||
|
* [Client Events](./docs/events-guide.md)
|
||||||
|
|
||||||
## Environment Setup
|
## Environment Setup
|
||||||
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
* Clone this repo using `git clone https://github.com/kevinthedang/discord-ollama.git` or just use [GitHub Desktop](https://desktop.github.com/) to clone the repo.
|
||||||
* You will need a `.env` file in the root of the project directory with the bot's token. There is a `.env.sample` is provided for you as a reference for what environment variables.
|
* You will need a `.env` file in the root of the project directory with the bot's token. There is a `.env.sample` is provided for you as a reference for what environment variables.
|
||||||
* For example, `CLIENT_TOKEN = [Bot Token]`
|
* For example, `CLIENT_TOKEN = [Bot Token]`
|
||||||
* Please refer to the docs for bot setup.
|
* Please refer to the docs for bot setup.
|
||||||
|
* [Creating a Discord App](./docs/setup-discord-app.md)
|
||||||
* [Local Machine Setup](./docs/setup-local.md)
|
* [Local Machine Setup](./docs/setup-local.md)
|
||||||
* [Docker Setup for Servers and Local Machines](./docs/setup-docker.md)
|
* [Docker Setup for Servers and Local Machines](./docs/setup-docker.md)
|
||||||
* Nvidia is recommended for now, but support for other GPUs should be development.
|
* Nvidia is recommended for now, but support for other GPUs should be development.
|
||||||
* Local use is not recommended.
|
* Local use is not recommended.
|
||||||
* [Creating a Discord App](./docs/setup-discord-app.md)
|
|
||||||
|
|
||||||
## Resources
|
## Resources
|
||||||
* [NodeJS](https://nodejs.org/en)
|
* [NodeJS](https://nodejs.org/en)
|
||||||
* This project runs on `lts\hydrogen`.
|
* This project runs on `lts\hydrogen`.
|
||||||
* To run dev in `ts-node`/`nodemon`, using `v18.18.2` is recommended.
|
|
||||||
* To run dev with `tsx`, you can use `v20.10.0` or earlier.
|
|
||||||
* This project supports any NodeJS version above `16.x.x` to only allow ESModules.
|
* This project supports any NodeJS version above `16.x.x` to only allow ESModules.
|
||||||
* [Ollama](https://ollama.com/)
|
* [Ollama](https://ollama.com/)
|
||||||
* [Ollama Docker Image](https://hub.docker.com/r/ollama/ollama)
|
* [Ollama Docker Image](https://hub.docker.com/r/ollama/ollama)
|
||||||
|
|
||||||
> [!CAUTION]
|
|
||||||
> `v18.X.X` or `lts/hydrogen` will not run properly for `npm run dev-mon`. It is recommended to just use `npm run dev-tsx` for development. The nodemon version will likely be removed in a future update.
|
|
||||||
|
|
||||||
* [Discord Developer Portal](https://discord.com/developers/docs/intro)
|
|
||||||
* [Discord.js Docs](https://discord.js.org/docs/packages/discord.js/main)
|
* [Discord.js Docs](https://discord.js.org/docs/packages/discord.js/main)
|
||||||
* [Setting up Docker (Ubuntu 20.04)](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
|
* [Setting up Docker (Ubuntu 20.04)](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
|
||||||
* [Setting up Nvidia Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)
|
* [Setting up Nvidia Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html)
|
||||||
@@ -61,4 +61,4 @@ The project aims to:
|
|||||||
* [Kevin Dang](https://github.com/kevinthedang)
|
* [Kevin Dang](https://github.com/kevinthedang)
|
||||||
* [Jonathan Smoley](https://github.com/JT2M0L3Y)
|
* [Jonathan Smoley](https://github.com/JT2M0L3Y)
|
||||||
|
|
||||||
[discord-ollama](https://github.com/kevinthedang/discord-ollama) © 2023 by [Kevin Dang](https://github.com/kevinthedang) is licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
|
[discord-ollama](https://github.com/kevinthedang/discord-ollama) © 2023 by [Kevin Dang](https://github.com/kevinthedang) is licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
# creates the docker compose
|
# creates the docker compose
|
||||||
version: '3.7'
|
|
||||||
|
|
||||||
# build individual services
|
# build individual services
|
||||||
services:
|
services:
|
||||||
@@ -8,12 +7,9 @@ services:
|
|||||||
build: ./ # find docker file in designated path
|
build: ./ # find docker file in designated path
|
||||||
container_name: discord
|
container_name: discord
|
||||||
restart: always # rebuild container always
|
restart: always # rebuild container always
|
||||||
image: kevinthedang/discord-ollama:0.5.9
|
image: kevinthedang/discord-ollama:0.7.1
|
||||||
environment:
|
environment:
|
||||||
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
CLIENT_TOKEN: ${CLIENT_TOKEN}
|
||||||
GUILD_ID: ${GUILD_ID}
|
|
||||||
MODEL: ${MODEL}
|
|
||||||
CLIENT_UID: ${CLIENT_UID}
|
|
||||||
OLLAMA_IP: ${OLLAMA_IP}
|
OLLAMA_IP: ${OLLAMA_IP}
|
||||||
OLLAMA_PORT: ${OLLAMA_PORT}
|
OLLAMA_PORT: ${OLLAMA_PORT}
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
105
docs/commands-guide.md
Normal file
105
docs/commands-guide.md
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
## Commands Guide
|
||||||
|
This is a guide to all of the slash commands for the app.
|
||||||
|
|
||||||
|
* Action Commands are commands that do not affect a user's `preference file`.
|
||||||
|
* Guild Commands can also be considered action commands.
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> Administrator commands are only usable by actual administrators on the Discord server.
|
||||||
|
|
||||||
|
### Guild Commands (Administrator)
|
||||||
|
1. Disable (or Toggle Chat)
|
||||||
|
This command will `enable` or `disable` whether or not the app will respond to users.
|
||||||
|
|
||||||
|
```
|
||||||
|
/toggle-chat enabled true
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Shutoff
|
||||||
|
This command will shutoff the app so no users can converse with it.
|
||||||
|
The app must be manually restarted upon being shutoff.
|
||||||
|
|
||||||
|
Below shuts off the app by putting `true` in the `are-your-sure` field.
|
||||||
|
|
||||||
|
```
|
||||||
|
/shutoff are-you-sure true
|
||||||
|
```
|
||||||
|
|
||||||
|
### Action Commands
|
||||||
|
1. Clear Channel (Message) History
|
||||||
|
This command will clear the history of the current channel for the user that calls it.
|
||||||
|
Running the command in any channel will clear the message history.
|
||||||
|
|
||||||
|
```
|
||||||
|
/clear-user-channel-history
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Pull Model
|
||||||
|
This command will pull a model that exists on the [Ollama Model Library](https://ollama.com/library). If it does not exist there, it will throw a hissy fit.
|
||||||
|
|
||||||
|
Below trys to pull the `codellama` model.
|
||||||
|
|
||||||
|
```
|
||||||
|
/pull-model model-to-pull codellama
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Thread Create
|
||||||
|
This command creates a public thread to talk with the app instead of using a `GuildText` channel.
|
||||||
|
|
||||||
|
```
|
||||||
|
/thread
|
||||||
|
```
|
||||||
|
|
||||||
|
4. (Private) Thread Create
|
||||||
|
This command creates a private thread to talk with the bot privately.
|
||||||
|
Invite others to the channel and they will be able to talk to the app as well.
|
||||||
|
|
||||||
|
```
|
||||||
|
/private-thread
|
||||||
|
```
|
||||||
|
|
||||||
|
### User Preference Commands
|
||||||
|
1. Capacity
|
||||||
|
This command changes how much context it will keep in conversations with the app.
|
||||||
|
This is applied for all of existing chats when interacting with the app.
|
||||||
|
|
||||||
|
Below sets the message history capacity to at most 5 messages at once.
|
||||||
|
|
||||||
|
```
|
||||||
|
/modify-capacity context-capacity 5
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Message Stream
|
||||||
|
This command will toggle whether or not the app will "stream" a response.
|
||||||
|
(think of how ChatGPT and other interfaces do this)
|
||||||
|
|
||||||
|
Below sets the `stream` to true to make the app respond in increments.
|
||||||
|
|
||||||
|
```
|
||||||
|
/message-stream stream true
|
||||||
|
```
|
||||||
|
> [!NOTE]
|
||||||
|
> This is a very slow progress on Discord because "spamming" changes within 5 seconds is not allowed.
|
||||||
|
|
||||||
|
3. Message Style
|
||||||
|
This command allows a user to select whether to embed the app's response.
|
||||||
|
|
||||||
|
```
|
||||||
|
/message-style embed true
|
||||||
|
```
|
||||||
|
|
||||||
|
This allows the app to respond as a user would normally respond.
|
||||||
|
|
||||||
|
```
|
||||||
|
/message-style embed false
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Switch Model
|
||||||
|
This command will switch the user-preferred model so long as it exists in within the local ollama service or from the [Ollama Model Library](https://ollama.com/library).
|
||||||
|
If it cannot be found locally, it will attempt to find it in the model library.
|
||||||
|
|
||||||
|
Below we are trying to switch to a specific model size.
|
||||||
|
|
||||||
|
```
|
||||||
|
/switch-model model-to-use llama3.2:1.3b
|
||||||
|
```
|
||||||
27
docs/events-guide.md
Normal file
27
docs/events-guide.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
## Events Guide
|
||||||
|
This is a guide to all of the client events for the app.
|
||||||
|
|
||||||
|
> [!NOTE] Each of these is logged to the console for a developer to track.
|
||||||
|
|
||||||
|
1. ClientReady
|
||||||
|
This event signifies that the Discord app is online.
|
||||||
|
Here the app's activity is set and its commands are registered.
|
||||||
|
|
||||||
|
2. InteractionCreate
|
||||||
|
This event signifies that a user interacted from Discord in some way.
|
||||||
|
Here commands are selected from a knowledge bank and executed if found.
|
||||||
|
|
||||||
|
> [!NOTE] Possible interactions include commands, buttons, menus, etc.
|
||||||
|
|
||||||
|
3. MessageCreate
|
||||||
|
This event signifies that a message was sent.
|
||||||
|
Here user questions and comments for the LLM are processed.
|
||||||
|
1. check message is from a user and mentions the app
|
||||||
|
2. check for interaction preferences
|
||||||
|
3. add the message to a queue
|
||||||
|
4. check the response for success
|
||||||
|
5. send a response back to the user.
|
||||||
|
|
||||||
|
4. ThreadDelete
|
||||||
|
This event signifies that a Discord Thread was deleted.
|
||||||
|
Here any preferences set for interaction within the thread are cleared away.
|
||||||
@@ -18,10 +18,6 @@
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
* You will also need your App's **Client ID**, navigate to **OAuth2** and copy your id.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
* That should be all of the environment variables needed from Discord, now we need this app on your server.
|
* That should be all of the environment variables needed from Discord, now we need this app on your server.
|
||||||
* Navigate to **Installation** and Copy the provided **Install Link** to allow your App to your server.
|
* Navigate to **Installation** and Copy the provided **Install Link** to allow your App to your server.
|
||||||
* You should set the **Guild Install** permissions as you like, for this purpose we will allow admin priviledges for now. Ensure the **bot** scope is added to do this.
|
* You should set the **Guild Install** permissions as you like, for this purpose we will allow admin priviledges for now. Ensure the **bot** scope is added to do this.
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
* Follow this guide to setup [Docker](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
|
* Follow this guide to setup [Docker](https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)
|
||||||
* If on Windows, download [Docker Desktop](https://docs.docker.com/desktop/install/windows-install/) to get the docker engine.
|
* If on Windows, download [Docker Desktop](https://docs.docker.com/desktop/install/windows-install/) to get the docker engine.
|
||||||
* Please also install [Docker Compose](https://docs.docker.com/compose/install/linux/) for easy running. If not, there are [scripts](#manual-run-with-docker) to set everything up.
|
* Please also install [Docker Compose](https://docs.docker.com/compose/install/linux/) for easy running. If not, there are [scripts](#manual-run-with-docker) to set everything up.
|
||||||
* **IMPORTANT NOTE**: Currently, it seems like wsl does not like Nvidia Container Toolkit. It will work initially then reset it for some odd reason. For now, it is advised to use an actually Linux machine to run using Docker. If you do not care about utilizing your GPU or don't even have a Nvidia GPU then disregard this.
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> Currently, it seems like wsl does not like Nvidia Container Toolkit. It will work initially then reset it for some odd reason. For now, it is advised to use an actually Linux machine to run using Docker. If you do not care about utilizing your GPU or don't even have a Nvidia GPU then disregard this.
|
||||||
|
|
||||||
## Nvidia Container Toolkit Setup
|
## Nvidia Container Toolkit Setup
|
||||||
### Installation with Apt
|
### Installation with Apt
|
||||||
@@ -48,15 +50,13 @@ sudo systemctl restart docker
|
|||||||
* `SUBNET_ADDRESS = 172.18.0.0`
|
* `SUBNET_ADDRESS = 172.18.0.0`
|
||||||
* Don't understand any of this? watch a Networking video to understand subnetting.
|
* Don't understand any of this? watch a Networking video to understand subnetting.
|
||||||
* You also need all environment variables shown in [`.env.sample`](../.env.sample)
|
* You also need all environment variables shown in [`.env.sample`](../.env.sample)
|
||||||
* You will need a model in the container for this to work properly, on Docker Desktop go to the `Containers` tab, select the `ollama` container, and select `Exec` to run as root on your container. Now, run `ollama pull [model name]` to get your model.
|
|
||||||
* For Linux Servers, you need another shell to pull the model, or if you run `docker compose build && docker compose up -d`, then it will run in the background to keep your shell. Run `docker exec -it ollama bash` to get into the container and run the samme pull command above.
|
|
||||||
* Otherwise, there is no need to install any npm packages for this, you just need to run `npm run start` to pull the containers and spin them up.
|
* Otherwise, there is no need to install any npm packages for this, you just need to run `npm run start` to pull the containers and spin them up.
|
||||||
* For cleaning up on Linux (or Windows), run the following commands:
|
* For cleaning up on Linux (or Windows), run the following commands:
|
||||||
* `docker compose stop`
|
* `docker compose stop`
|
||||||
* `docker compose rm`
|
* `docker compose rm`
|
||||||
* `docker ps` to check if containers have been removed.
|
* `docker ps` to check if containers have been removed.
|
||||||
* This may not work if the nvidia installation was done incorrectly. If this is the case, please utilize the [Manual "Clean-up"](#manual-run-with-docker) shown below.
|
* This may not work if the nvidia installation was done incorrectly. If this is the case, please utilize the [Manual "Clean-up"](#manual-run-with-docker) shown below.
|
||||||
* You can also use `npm run clean` to clean up the containers and remove the network to address a possible `Address already in use` problem.
|
* You can also use `npm run clean` to clean up the containers and remove the network to address a possible `Address already in use` problem. This script does not have to end without error to work.
|
||||||
|
|
||||||
## Manual Run (with Docker)
|
## Manual Run (with Docker)
|
||||||
* Run the following commands:
|
* Run the following commands:
|
||||||
|
|||||||
@@ -11,14 +11,17 @@
|
|||||||
* You can now interact with the model you just ran (it might take a second to startup).
|
* You can now interact with the model you just ran (it might take a second to startup).
|
||||||
* Response time varies with processing power!
|
* Response time varies with processing power!
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> You can now pull models directly from the Discord client using `/pull-model <model-name>` or `/switch-model <model-name>`. They must exist from your local model library or from the [Ollama Model Library](https://ollama.com/library)
|
||||||
|
|
||||||
## To Run Locally (without Docker)
|
## To Run Locally (without Docker)
|
||||||
* Run `npm install` to install the npm packages.
|
* Run `npm install` to install the npm packages.
|
||||||
* Ensure that your [.env](../.env.sample) file's `OLLAMA_IP` is `127.0.0.1` to work properly.
|
* Ensure that your [.env](../.env.sample) file's `OLLAMA_IP` is `127.0.0.1` to work properly.
|
||||||
* You only need your `CLIENT_TOKEN`, `GUILD_ID`, `MODEL`, `CLIENT_UID`, `OLLAMA_IP`, `OLLAMA_PORT`.
|
* You only need your `CLIENT_TOKEN`, `OLLAMA_IP`, `OLLAMA_PORT`.
|
||||||
* The ollama ip and port should just use it's defaults by nature. If not, utilize `OLLAMA_IP = 127.0.0.1` and `OLLAMA_PORT = 11434`.
|
* The ollama ip and port should just use it's defaults by nature. If not, utilize `OLLAMA_IP = 127.0.0.1` and `OLLAMA_PORT = 11434`.
|
||||||
* Now, you can run the bot by running `npm run client` which will build and run the decompiled typescript and run the setup for ollama.
|
* Now, you can run the bot by running `npm run client` which will build and run the decompiled typescript and run the setup for ollama.
|
||||||
* **IMPORTANT**: This must be ran in the wsl/Linux instance to work properly! Using Command Prompt/Powershell/Git Bash/etc. will not work on Windows (at least in my experience).
|
* **IMPORTANT**: This must be ran in the wsl/Linux instance to work properly! Using Command Prompt/Powershell/Git Bash/etc. will not work on Windows (at least in my experience).
|
||||||
* Refer to the [resources](../README.md#resources) on what node version to use.
|
* Refer to the [resources](../README.md#resources) on what node version to use.
|
||||||
* If you are using wsl, open up a separate terminal/shell to startup the ollama service. Again, if you are running an older ollama, you must run `ollama serve` in that shell.
|
* If you are using wsl, open up a separate terminal/shell to startup the ollama service. Again, if you are running an older ollama, you must run `ollama serve` in that shell.
|
||||||
* If you are on an actual Linux machine/VM there is no need for another terminal (unless you have an older ollama version).
|
* If you are on an actual Linux machine/VM there is no need for another terminal (unless you have an older ollama version).
|
||||||
* If you do not have a model, you will need to run `ollama pull [model name]` in a separate terminal to get it.
|
* If you do not have a model, you **can optionally** run `ollama pull [model name]` in wsl prior to application start. You are not required as it can be pulled from the Discord client.
|
||||||
12
nodemon.json
12
nodemon.json
@@ -1,12 +0,0 @@
|
|||||||
{
|
|
||||||
"restartable": "rs",
|
|
||||||
"ignore": ["node_modules/"],
|
|
||||||
"watch": ["src/"],
|
|
||||||
"execMap": {
|
|
||||||
"ts": "ts-node --esm"
|
|
||||||
},
|
|
||||||
"env": {
|
|
||||||
"NODE_ENV": "development"
|
|
||||||
},
|
|
||||||
"ext": "js,json,ts"
|
|
||||||
}
|
|
||||||
2217
package-lock.json
generated
2217
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@@ -1,44 +1,42 @@
|
|||||||
{
|
{
|
||||||
"name": "discord-ollama",
|
"name": "discord-ollama",
|
||||||
"version": "0.5.9",
|
"version": "0.7.1",
|
||||||
"description": "Ollama Integration into discord",
|
"description": "Ollama Integration into discord",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"exports": "./build/index.js",
|
"exports": "./build/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev-tsx": "tsx watch src/index.ts",
|
|
||||||
"dev-mon": "nodemon --config nodemon.json src/index.ts",
|
|
||||||
"build": "tsc",
|
|
||||||
"test:run": "vitest run",
|
"test:run": "vitest run",
|
||||||
"test:coverage": "vitest run --coverage",
|
"test:coverage": "vitest run --coverage",
|
||||||
|
"watch": "tsx watch src",
|
||||||
|
"build": "tsc",
|
||||||
"prod": "node .",
|
"prod": "node .",
|
||||||
"client": "npm run build && npm run prod",
|
"client": "npm run build && npm run prod",
|
||||||
"clean": "docker compose down && docker rmi $(docker images | grep $(node -p \"require('./package.json').version\") | tr -s ' ' | cut -d ' ' -f 3) && docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
"clean": "docker compose down && docker rmi $(docker images | grep kevinthedang | tr -s ' ' | cut -d ' ' -f 3) && docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
||||||
"start": "docker compose build --no-cache && docker compose up -d",
|
"start": "docker compose build --no-cache && docker compose up -d",
|
||||||
"docker:start": "npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama",
|
"docker:clean": "docker rm -f discord && docker rm -f ollama && docker network prune -f && docker rmi $(docker images | grep kevinthedang | tr -s ' ' | cut -d ' ' -f 3) && docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
||||||
"docker:start-cpu": "npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama-cpu",
|
|
||||||
"docker:clean": "docker rm -f discord && docker rm -f ollama && docker rmi $(docker images --filter \"dangling=true\" -q --no-trunc)",
|
|
||||||
"docker:network": "docker network create --subnet=172.18.0.0/16 ollama-net",
|
"docker:network": "docker network create --subnet=172.18.0.0/16 ollama-net",
|
||||||
"docker:build": "docker build --no-cache -t kevinthedang/discord-ollama:$(node -p \"require('./package.json').version\") .",
|
"docker:build": "docker build --no-cache -t kevinthedang/discord-ollama:$(node -p \"require('./package.json').version\") .",
|
||||||
"docker:build-latest": "docker build --no-cache -t kevinthedang/discord-ollama:latest .",
|
"docker:build-latest": "docker build --no-cache -t kevinthedang/discord-ollama:latest .",
|
||||||
"docker:client": "docker run -d -v discord:/src/app --name discord --network ollama-net --ip 172.18.0.3 kevinthedang/discord-ollama:$(node -p \"require('./package.json').version\")",
|
"docker:client": "docker run -d -v discord:/src/app --name discord --network ollama-net --ip 172.18.0.3 kevinthedang/discord-ollama:$(node -p \"require('./package.json').version\")",
|
||||||
"docker:ollama": "docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest",
|
"docker:ollama": "docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest",
|
||||||
"docker:ollama-cpu": "docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest"
|
"docker:ollama-cpu": "docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama --network ollama-net --ip 172.18.0.2 ollama/ollama:latest",
|
||||||
|
"docker:start": "docker network prune -f && npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama",
|
||||||
|
"docker:start-cpu": "docker network prune -f && npm run docker:network && npm run docker:build && npm run docker:client && npm run docker:ollama-cpu"
|
||||||
},
|
},
|
||||||
"author": "Kevin Dang",
|
"author": "Kevin Dang",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"discord.js": "^14.15.3",
|
"discord.js": "^14.16.3",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"ollama": "^0.5.6"
|
"ollama": "^0.5.9"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^20.14.12",
|
"@types/node": "^22.9.0",
|
||||||
"@vitest/coverage-v8": "^2.0.4",
|
"@vitest/coverage-v8": "^2.1.4",
|
||||||
"nodemon": "^3.1.4",
|
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"tsx": "^4.16.2",
|
"tsx": "^4.19.2",
|
||||||
"typescript": "^5.5.4",
|
"typescript": "^5.6.3",
|
||||||
"vitest": "^2.0.4"
|
"vitest": "^2.1.4"
|
||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
import { Client, GatewayIntentBits } from 'discord.js'
|
import { Client, GatewayIntentBits } from 'discord.js'
|
||||||
import { UserMessage, registerEvents } from './utils/events.js'
|
|
||||||
import Events from './events/index.js'
|
|
||||||
import { Ollama } from 'ollama'
|
import { Ollama } from 'ollama'
|
||||||
import { Queue } from './queues/queue.js'
|
import { Queue } from './queues/queue.js'
|
||||||
|
import { UserMessage, registerEvents } from './utils/index.js'
|
||||||
// Import keys/tokens
|
import Events from './events/index.js'
|
||||||
import Keys from './keys.js'
|
import Keys from './keys.js'
|
||||||
|
|
||||||
|
|
||||||
@@ -19,21 +17,15 @@ const client = new Client({
|
|||||||
});
|
});
|
||||||
|
|
||||||
// initialize connection to ollama container
|
// initialize connection to ollama container
|
||||||
const ollama = new Ollama({
|
export const ollama = new Ollama({
|
||||||
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
host: `http://${Keys.ipAddress}:${Keys.portAddress}`,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Create Queue managed by Events
|
// Create Queue managed by Events
|
||||||
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
const messageHistory: Queue<UserMessage> = new Queue<UserMessage>
|
||||||
|
|
||||||
/**
|
// register all events
|
||||||
* register events for bot to listen to in discord
|
registerEvents(client, Events, messageHistory, ollama)
|
||||||
* @param messageHistory message history for the llm
|
|
||||||
* @param Events events to register
|
|
||||||
* @param client the bot reference
|
|
||||||
* @param Keys tokens from .env files
|
|
||||||
*/
|
|
||||||
registerEvents(client, Events, messageHistory, Keys, ollama)
|
|
||||||
|
|
||||||
// Try to log in the client
|
// Try to log in the client
|
||||||
await client.login(Keys.clientToken)
|
await client.login(Keys.clientToken)
|
||||||
@@ -45,5 +37,6 @@ await client.login(Keys.clientToken)
|
|||||||
// queue up bots name
|
// queue up bots name
|
||||||
messageHistory.enqueue({
|
messageHistory.enqueue({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: `My name is ${client.user?.username}`
|
content: `My name is ${client.user?.username}`,
|
||||||
|
images: []
|
||||||
})
|
})
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||||
import { openConfig } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const Capacity: SlashCommand = {
|
export const Capacity: SlashCommand = {
|
||||||
name: 'modify-capacity',
|
name: 'modify-capacity',
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js'
|
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
|
||||||
import { openConfig } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const ChannelToggle: SlashCommand = {
|
|
||||||
name: 'channel-toggle',
|
|
||||||
description: 'toggles channel or thread usage.',
|
|
||||||
|
|
||||||
// set user option for toggling
|
|
||||||
options: [
|
|
||||||
{
|
|
||||||
name: 'toggle-channel',
|
|
||||||
description: 'toggle channel usage, otherwise threads',
|
|
||||||
type: ApplicationCommandOptionType.Boolean,
|
|
||||||
required: true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
|
|
||||||
// Query for chatting preference
|
|
||||||
run: async (client: Client, interaction: CommandInteraction) => {
|
|
||||||
// fetch channel location
|
|
||||||
const channel = await client.channels.fetch(interaction.channelId)
|
|
||||||
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
|
||||||
|
|
||||||
// set state of bot channel preferences
|
|
||||||
openConfig(`${interaction.guildId}-config.json`, interaction.commandName, interaction.options.get('toggle-channel')?.value)
|
|
||||||
|
|
||||||
interaction.reply({
|
|
||||||
content: `Channel Preferences have for Regular Channels set to \`${interaction.options.get('toggle-channel')?.value}\``,
|
|
||||||
ephemeral: true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { clearChannelInfo, SlashCommand } from '../utils/index.js'
|
||||||
import { clearChannelInfo } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const ClearUserChannelHistory: SlashCommand = {
|
export const ClearUserChannelHistory: SlashCommand = {
|
||||||
name: 'clear-user-channel-history',
|
name: 'clear-user-channel-history',
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||||
import { openConfig } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const Disable: SlashCommand = {
|
export const Disable: SlashCommand = {
|
||||||
name: 'toggle-chat',
|
name: 'toggle-chat',
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ import { Disable } from './disable.js'
|
|||||||
import { Shutoff } from './shutoff.js'
|
import { Shutoff } from './shutoff.js'
|
||||||
import { Capacity } from './capacity.js'
|
import { Capacity } from './capacity.js'
|
||||||
import { PrivateThreadCreate } from './threadPrivateCreate.js'
|
import { PrivateThreadCreate } from './threadPrivateCreate.js'
|
||||||
import { ChannelToggle } from './channelToggle.js'
|
|
||||||
import { ClearUserChannelHistory } from './cleanUserChannelHistory.js'
|
import { ClearUserChannelHistory } from './cleanUserChannelHistory.js'
|
||||||
|
import { PullModel } from './pullModel.js'
|
||||||
|
import { SwitchModel } from './switchModel.js'
|
||||||
|
|
||||||
export default [
|
export default [
|
||||||
ThreadCreate,
|
ThreadCreate,
|
||||||
@@ -17,6 +18,7 @@ export default [
|
|||||||
Disable,
|
Disable,
|
||||||
Shutoff,
|
Shutoff,
|
||||||
Capacity,
|
Capacity,
|
||||||
ChannelToggle,
|
ClearUserChannelHistory,
|
||||||
ClearUserChannelHistory
|
PullModel,
|
||||||
|
SwitchModel
|
||||||
] as SlashCommand[]
|
] as SlashCommand[]
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js'
|
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||||
import { openConfig } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const MessageStream: SlashCommand = {
|
export const MessageStream: SlashCommand = {
|
||||||
name: 'message-stream',
|
name: 'message-stream',
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, ApplicationCommandOptionType } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { openConfig, SlashCommand } from '../utils/index.js'
|
||||||
import { openConfig } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const MessageStyle: SlashCommand = {
|
export const MessageStyle: SlashCommand = {
|
||||||
name: 'message-style',
|
name: 'message-style',
|
||||||
|
|||||||
47
src/commands/pullModel.ts
Normal file
47
src/commands/pullModel.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from "discord.js";
|
||||||
|
import { SlashCommand } from "../utils/commands.js";
|
||||||
|
import { ollama } from "../client.js";
|
||||||
|
|
||||||
|
export const PullModel: SlashCommand = {
|
||||||
|
name: 'pull-model',
|
||||||
|
description: 'pulls a model from the ollama model library',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'model-to-pull',
|
||||||
|
description: 'the name of the model to pull',
|
||||||
|
type: ApplicationCommandOptionType.String,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
// Pull for model from Ollama library
|
||||||
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
|
// defer reply to avoid timeout
|
||||||
|
await interaction.deferReply()
|
||||||
|
const modelInput: string = interaction.options.get('model-to-pull')!!.value as string
|
||||||
|
|
||||||
|
// fetch channel and message
|
||||||
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
|
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
// call ollama to pull desired model
|
||||||
|
await ollama.pull({
|
||||||
|
model: modelInput
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
// could not resolve pull or model unfound
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Could not pull/locate the **${modelInput}** model within the [Ollama Model Library](https://ollama.com/library).\n\nPlease check the model library and try again.`
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// successful pull
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Successfully added **${modelInput}** into your local model library.`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
75
src/commands/switchModel.ts
Normal file
75
src/commands/switchModel.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { ApplicationCommandOptionType, ChannelType, Client, CommandInteraction } from "discord.js";
|
||||||
|
import { SlashCommand } from "../utils/commands.js";
|
||||||
|
import { ollama } from "../client.js";
|
||||||
|
import { ModelResponse } from "ollama";
|
||||||
|
import { openConfig } from "../utils/index.js";
|
||||||
|
|
||||||
|
export const SwitchModel: SlashCommand = {
|
||||||
|
name: 'switch-model',
|
||||||
|
description: 'switches current model to preferred model to use.',
|
||||||
|
|
||||||
|
// set available user options to pass to the command
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
name: 'model-to-use',
|
||||||
|
description: 'the name of the model to use',
|
||||||
|
type: ApplicationCommandOptionType.String,
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
// Switch user preferred model if available in local library
|
||||||
|
run: async (client: Client, interaction: CommandInteraction) => {
|
||||||
|
await interaction.deferReply()
|
||||||
|
|
||||||
|
const modelInput: string = interaction.options.get('model-to-use')!!.value as string
|
||||||
|
|
||||||
|
// fetch channel and message
|
||||||
|
const channel = await client.channels.fetch(interaction.channelId)
|
||||||
|
if (!channel || channel.type !== (ChannelType.PrivateThread && ChannelType.PublicThread && ChannelType.GuildText)) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Phase 1: Set the model
|
||||||
|
let switchSuccess = false
|
||||||
|
await ollama.list()
|
||||||
|
.then(response => {
|
||||||
|
for (const model in response.models) {
|
||||||
|
const currentModel: ModelResponse = response.models[model]
|
||||||
|
if (currentModel.name.startsWith(modelInput)) {
|
||||||
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, modelInput)
|
||||||
|
|
||||||
|
// successful switch
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Successfully switched to **${modelInput}** as the preferred model for ${interaction.user.username}.`
|
||||||
|
})
|
||||||
|
switchSuccess = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if (switchSuccess) return
|
||||||
|
|
||||||
|
// Phase 2: Try to get it regardless
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Could not find **${modelInput}** in local model library, trying to pull it now...\n\nThis could take a few moments... Please be patient!`
|
||||||
|
})
|
||||||
|
|
||||||
|
await ollama.pull({
|
||||||
|
model: modelInput
|
||||||
|
})
|
||||||
|
|
||||||
|
// set model now that it exists
|
||||||
|
openConfig(`${interaction.user.username}-config.json`, interaction.commandName, modelInput)
|
||||||
|
|
||||||
|
// We got the model!
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Successfully added and set **${modelInput}** as your preferred model.`
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
// could not resolve user model switch
|
||||||
|
interaction.editReply({
|
||||||
|
content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to run \`/pull-model ${modelInput}\` and try again.`
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, TextChannel, ThreadChannel } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { openChannelInfo, SlashCommand } from '../utils/index.js'
|
||||||
import { openThreadInfo } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const ThreadCreate: SlashCommand = {
|
export const ThreadCreate: SlashCommand = {
|
||||||
name: 'thread',
|
name: 'thread',
|
||||||
@@ -19,11 +18,12 @@ export const ThreadCreate: SlashCommand = {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Send a message in the thread
|
// Send a message in the thread
|
||||||
thread.send(`Hello ${interaction.user} and others! \n\nIt's nice to meet you. Please talk to me by typing **@${client.user?.username}** with your prompt.\n\nIf I do not respond, ensure \`channel-toggle\` is set to \`false\``)
|
thread.send(`Hello ${interaction.user} and others! \n\nIt's nice to meet you. Please talk to me by typing **@${client.user?.username}** with your prompt.`)
|
||||||
|
|
||||||
// handle storing this chat channel
|
// handle storing this chat channel
|
||||||
// store: thread.id, thread.name
|
openChannelInfo(thread.id,
|
||||||
openThreadInfo(`${thread.id}.json`, thread)
|
thread as ThreadChannel,
|
||||||
|
interaction.user.tag)
|
||||||
|
|
||||||
// user only reply
|
// user only reply
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import { ChannelType, Client, CommandInteraction, TextChannel } from 'discord.js'
|
import { ChannelType, Client, CommandInteraction, TextChannel, ThreadChannel } from 'discord.js'
|
||||||
import { SlashCommand } from '../utils/commands.js'
|
import { openChannelInfo, SlashCommand } from '../utils/index.js'
|
||||||
import { openThreadInfo } from '../utils/index.js'
|
|
||||||
|
|
||||||
export const PrivateThreadCreate: SlashCommand = {
|
export const PrivateThreadCreate: SlashCommand = {
|
||||||
name: 'private-thread',
|
name: 'private-thread',
|
||||||
@@ -23,7 +22,10 @@ export const PrivateThreadCreate: SlashCommand = {
|
|||||||
|
|
||||||
// handle storing this chat channel
|
// handle storing this chat channel
|
||||||
// store: thread.id, thread.name
|
// store: thread.id, thread.name
|
||||||
openThreadInfo(`${thread.id}.json`, thread)
|
openChannelInfo(thread.id,
|
||||||
|
thread as ThreadChannel,
|
||||||
|
interaction.user.tag
|
||||||
|
)
|
||||||
|
|
||||||
// user only reply
|
// user only reply
|
||||||
return interaction.reply({
|
return interaction.reply({
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { embedMessage, event, Events, normalMessage, UserMessage } from '../utils/index.js'
|
import { TextChannel } from 'discord.js'
|
||||||
import { getChannelInfo, getServerConfig, getThread, getUserConfig, openChannelInfo, openConfig, openThreadInfo, ServerConfig, UserConfig } from '../utils/index.js'
|
import { embedMessage, event, Events, normalMessage, UserMessage, clean } from '../utils/index.js'
|
||||||
import { clean } from '../utils/mentionClean.js'
|
import { getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, openConfig, UserConfig, getAttachmentData } from '../utils/index.js'
|
||||||
import { TextChannel, ThreadChannel } from 'discord.js'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Max Message length for free users is 2000 characters (bot or not).
|
* Max Message length for free users is 2000 characters (bot or not).
|
||||||
@@ -9,29 +8,28 @@ import { TextChannel, ThreadChannel } from 'discord.js'
|
|||||||
*
|
*
|
||||||
* @param message the message received from the channel
|
* @param message the message received from the channel
|
||||||
*/
|
*/
|
||||||
export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama, client }, message) => {
|
export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client }, message) => {
|
||||||
log(`Message \"${clean(message.content)}\" from ${message.author.tag} in channel/thread ${message.channelId}.`)
|
const clientId = client.user!!.id
|
||||||
|
const 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
|
// Do not respond if bot talks in the chat
|
||||||
if (message.author.username === message.client.user.username) return
|
if (message.author.username === message.client.user.username) return
|
||||||
|
|
||||||
// Only respond if message mentions the bot
|
// Only respond if message mentions the bot
|
||||||
if (!message.mentions.has(tokens.clientUid)) return
|
if (!message.mentions.has(clientId)) return
|
||||||
|
|
||||||
// default stream to false
|
// default stream to false
|
||||||
let shouldStream = false
|
let shouldStream = false
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Retrieve Server/Guild Preferences
|
// Retrieve Server/Guild Preferences
|
||||||
const serverConfig: ServerConfig = await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
getServerConfig(`${message.guildId}-config.json`, (config) => {
|
||||||
// check if config.json exists
|
// check if config.json exists
|
||||||
if (config === undefined) {
|
if (config === undefined) {
|
||||||
// Allowing chat options to be available
|
// Allowing chat options to be available
|
||||||
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
openConfig(`${message.guildId}-config.json`, 'toggle-chat', true)
|
||||||
|
|
||||||
// default to channel scope chats
|
|
||||||
openConfig(`${message.guildId}-config.json`, 'channel-toggle', true)
|
|
||||||
reject(new Error('No Server Preferences is set up.\n\nCreating default server preferences file...\nPlease try chatting again.'))
|
reject(new Error('No Server Preferences is set up.\n\nCreating default server preferences file...\nPlease try chatting again.'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -42,14 +40,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ensure channel json exists, if not create it
|
|
||||||
if (config.options['channel-toggle']) {
|
|
||||||
openChannelInfo(message.channelId,
|
|
||||||
message.channel as TextChannel,
|
|
||||||
message.author.username
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(config)
|
resolve(config)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -59,7 +49,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
getUserConfig(`${message.author.username}-config.json`, (config) => {
|
||||||
if (config === undefined) {
|
if (config === undefined) {
|
||||||
openConfig(`${message.author.username}-config.json`, 'message-style', false)
|
openConfig(`${message.author.username}-config.json`, 'message-style', false)
|
||||||
reject(new Error('No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular messages.\nPlease try chatting again.'))
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,36 +65,51 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
|
|
||||||
// set stream state
|
// set stream state
|
||||||
shouldStream = config.options['message-stream'] as boolean || false
|
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)
|
resolve(config)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// need new check for "open/active" threads/channels here!
|
// need new check for "open/active" threads/channels here!
|
||||||
const chatMessages: UserMessage[] = await new Promise((resolve) => {
|
let chatMessages: UserMessage[] = await new Promise((resolve) => {
|
||||||
// set new queue to modify
|
// set new queue to modify
|
||||||
if (serverConfig.options['channel-toggle']) {
|
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) => {
|
getChannelInfo(`${message.channelId}-${message.author.username}.json`, (channelInfo) => {
|
||||||
if (channelInfo?.messages)
|
if (channelInfo?.messages)
|
||||||
resolve(channelInfo.messages)
|
resolve(channelInfo.messages)
|
||||||
else
|
else {
|
||||||
log(`Channel ${message.channel}-${message.author.username} does not exist.`)
|
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.`))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
} else {
|
})
|
||||||
getThread(`${message.channelId}.json`, (threadInfo) => {
|
}
|
||||||
if (threadInfo?.messages)
|
|
||||||
resolve(threadInfo.messages)
|
|
||||||
else
|
|
||||||
log(`Thread ${message.channelId} does not exist.`)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// response string for ollama to put its response
|
// response string for ollama to put its response
|
||||||
let response: string
|
let response: string
|
||||||
|
|
||||||
|
// get message attachment if exists
|
||||||
|
const messageAttachment: string[] = await getAttachmentData(message.attachments.first())
|
||||||
|
const model: string = userConfig.options['switch-model']
|
||||||
|
|
||||||
// set up new queue
|
// set up new queue
|
||||||
msgHist.setQueue(chatMessages)
|
msgHist.setQueue(chatMessages)
|
||||||
|
|
||||||
@@ -114,14 +119,15 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
// push user response before ollama query
|
// push user response before ollama query
|
||||||
msgHist.enqueue({
|
msgHist.enqueue({
|
||||||
role: 'user',
|
role: 'user',
|
||||||
content: clean(message.content)
|
content: cleanedMessage,
|
||||||
|
images: messageAttachment || []
|
||||||
})
|
})
|
||||||
|
|
||||||
// undefined or false, use normal, otherwise use embed
|
// undefined or false, use normal, otherwise use embed
|
||||||
if (userConfig.options['message-style'])
|
if (userConfig.options['message-style'])
|
||||||
response = await embedMessage(message, ollama, tokens, msgHist, shouldStream)
|
response = await embedMessage(message, ollama, model, msgHist, shouldStream)
|
||||||
else
|
else
|
||||||
response = await normalMessage(message, ollama, tokens, msgHist, shouldStream)
|
response = await normalMessage(message, ollama, model, msgHist, shouldStream)
|
||||||
|
|
||||||
// If something bad happened, remove user query and stop
|
// If something bad happened, remove user query and stop
|
||||||
if (response == undefined) { msgHist.pop(); return }
|
if (response == undefined) { msgHist.pop(); return }
|
||||||
@@ -132,22 +138,16 @@ export default event(Events.MessageCreate, async ({ log, msgHist, tokens, ollama
|
|||||||
// successful query, save it in context history
|
// successful query, save it in context history
|
||||||
msgHist.enqueue({
|
msgHist.enqueue({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: response
|
content: response,
|
||||||
|
images: messageAttachment || []
|
||||||
})
|
})
|
||||||
|
|
||||||
// only update the json on success
|
// only update the json on success
|
||||||
if (serverConfig.options['channel-toggle']) {
|
openChannelInfo(message.channelId,
|
||||||
openChannelInfo(message.channelId,
|
message.channel as TextChannel,
|
||||||
message.channel as TextChannel,
|
message.author.tag,
|
||||||
message.author.tag,
|
msgHist.getItems()
|
||||||
msgHist.getItems()
|
)
|
||||||
)
|
|
||||||
} else {
|
|
||||||
openThreadInfo(`${message.channelId}.json`,
|
|
||||||
client.channels.fetch(message.channelId) as unknown as ThreadChannel,
|
|
||||||
msgHist.getItems()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
msgHist.pop() // remove message because of failure
|
msgHist.pop() // remove message because of failure
|
||||||
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
message.reply(`**Error Occurred:**\n\n**Reason:** *${error.message}*`)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { event, Events, registerCommands } from '../utils/index.js'
|
|
||||||
import { ActivityType } from 'discord.js'
|
import { ActivityType } from 'discord.js'
|
||||||
|
import { event, Events, registerCommands } from '../utils/index.js'
|
||||||
import commands from '../commands/index.js'
|
import commands from '../commands/index.js'
|
||||||
|
|
||||||
// Log when the bot successfully logs in and export it
|
// Log when the bot successfully logs in and export it
|
||||||
|
|||||||
@@ -5,16 +5,36 @@ import fs from 'fs'
|
|||||||
/**
|
/**
|
||||||
* Event to remove the associated .json file for a thread once deleted
|
* Event to remove the associated .json file for a thread once deleted
|
||||||
*/
|
*/
|
||||||
export default event(Events.ThreadDelete, ({ log }, thread: ThreadChannel) => {
|
export default event(Events.ThreadDelete, async ({ log }, thread: ThreadChannel) => {
|
||||||
const filePath = `data/${thread.id}.json`
|
// iterate through every guild member in the thread and delete their history, except the bot
|
||||||
if (fs.existsSync(filePath)) {
|
try {
|
||||||
fs.unlink(filePath, (error) => {
|
log(`Number of User Guild Members in Thread being deleted: ${thread.memberCount!! - 1}`)
|
||||||
if (error)
|
const dirPath = 'data/'
|
||||||
log(`Error deleting file ${filePath}`, error)
|
|
||||||
else
|
// read all files in data/
|
||||||
log(`Successfully deleted ${filePath} thread info`)
|
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'))
|
||||||
|
|
||||||
|
// 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`)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
} else {
|
} catch (error) {
|
||||||
log(`File ${filePath} does not exist.`)
|
log(`Issue deleting user history files from ${thread.id}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
import { getEnvVar } from './utils/env.js'
|
import { getEnvVar } from './utils/index.js'
|
||||||
|
|
||||||
export const Keys = {
|
export const Keys = {
|
||||||
clientToken: getEnvVar('CLIENT_TOKEN'),
|
clientToken: getEnvVar('CLIENT_TOKEN'),
|
||||||
model: getEnvVar('MODEL'),
|
ipAddress: getEnvVar('OLLAMA_IP', '127.0.0.1'), // default ollama ip if none
|
||||||
clientUid: getEnvVar('CLIENT_UID'),
|
portAddress: getEnvVar('OLLAMA_PORT', '11434'), // default ollama port if none
|
||||||
ipAddress: getEnvVar('OLLAMA_IP'),
|
|
||||||
portAddress: getEnvVar('OLLAMA_PORT'),
|
|
||||||
} as const // readonly keys
|
} as const // readonly keys
|
||||||
|
|
||||||
export default Keys
|
export default Keys
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import { UserMessage } from './events.js'
|
import { UserMessage } from './index.js'
|
||||||
|
|
||||||
export interface UserConfiguration {
|
export interface UserConfiguration {
|
||||||
'message-stream'?: boolean,
|
'message-stream'?: boolean,
|
||||||
'message-style'?: boolean,
|
'message-style'?: boolean,
|
||||||
'modify-capacity': number
|
'modify-capacity': number,
|
||||||
|
'switch-model': string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ServerConfiguration {
|
export interface ServerConfiguration {
|
||||||
'toggle-chat'?: boolean,
|
'toggle-chat'?: boolean,
|
||||||
'channel-toggle'?: boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,12 +35,6 @@ export interface ServerConfig {
|
|||||||
options: ServerConfiguration
|
options: ServerConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Thread {
|
|
||||||
readonly id: string
|
|
||||||
readonly name: string
|
|
||||||
messages: UserMessage[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Channel {
|
export interface Channel {
|
||||||
readonly id: string
|
readonly id: string
|
||||||
readonly name: string
|
readonly name: string
|
||||||
@@ -54,5 +48,5 @@ export interface Channel {
|
|||||||
* @returns true if command is from Server Config, false otherwise
|
* @returns true if command is from Server Config, false otherwise
|
||||||
*/
|
*/
|
||||||
export function isServerConfigurationKey(key: string): key is keyof ServerConfiguration {
|
export function isServerConfigurationKey(key: string): key is keyof ServerConfiguration {
|
||||||
return ['toggle-chat', 'channel-toggle'].includes(key);
|
return ['toggle-chat'].includes(key);
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,35 @@
|
|||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import { config } from 'dotenv'
|
import { config } from 'dotenv'
|
||||||
|
|
||||||
// Find config - ONLY WORKS WITH NODEMON
|
|
||||||
const envFile = process.env.NODE_ENV === 'development' ? '.env.dev.local' : '.env'
|
|
||||||
|
|
||||||
// resolve config file
|
// resolve config file
|
||||||
const envFilePath = resolve(process.cwd(), envFile)
|
const envFilePath = resolve(process.cwd(), '.env')
|
||||||
|
const ipValidate: RegExp = /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/
|
||||||
|
|
||||||
// set current environment variable file
|
// set current environment variable file
|
||||||
config({ path: envFilePath })
|
config({ path: envFilePath })
|
||||||
|
|
||||||
// Getter for environment variables
|
/**
|
||||||
|
* Method to validate if environment variables found in file utils/env.ts
|
||||||
|
*
|
||||||
|
* @param name Name of the environment variable in .env
|
||||||
|
* @param fallback fallback value to set if environment variable is not set (used manually in src/keys.ts)
|
||||||
|
* @returns environment variable value
|
||||||
|
*/
|
||||||
export function getEnvVar(name: string, fallback?: string): string {
|
export function getEnvVar(name: string, fallback?: string): string {
|
||||||
const value = process.env[name] ?? fallback
|
const value = process.env[name] ?? fallback
|
||||||
if (value == undefined)
|
if (!value)
|
||||||
throw new Error(`Environment variable ${name} is not set.`)
|
throw new Error(`Environment variable ${name} is not set.`)
|
||||||
|
|
||||||
|
// validate User-Generated Discord Application Tokens
|
||||||
|
if (name === "CLIENT_TOKEN")
|
||||||
|
if (value.length < 72) throw new Error(`The "CLIENT_TOKEN" provided is not of at least length 72.
|
||||||
|
This is probably an invalid token unless Discord updated their token policy. Please provide a valid token.`)
|
||||||
|
|
||||||
|
// validate IPv4 address found in environment variables
|
||||||
|
if (name.endsWith("_IP") || name.endsWith("_ADDRESS"))
|
||||||
|
if (!ipValidate.test(value))
|
||||||
|
throw new Error(`Environment variable ${name} does not follow IPv4 formatting.`)
|
||||||
|
|
||||||
// return env variable
|
// return env variable
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { ClientEvents, Awaitable, Client, User } from 'discord.js'
|
import type { ClientEvents, Awaitable, Client } from 'discord.js'
|
||||||
import { Ollama } from 'ollama'
|
import { Ollama } from 'ollama'
|
||||||
import { Queue } from '../queues/queue.js'
|
import { Queue } from '../queues/queue.js'
|
||||||
|
|
||||||
@@ -8,17 +8,6 @@ export { Events } from 'discord.js'
|
|||||||
export type LogMethod = (...args: unknown[]) => void
|
export type LogMethod = (...args: unknown[]) => void
|
||||||
export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object
|
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 = {
|
|
||||||
model: string,
|
|
||||||
clientUid: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameters to run the chat query
|
* Parameters to run the chat query
|
||||||
* @param model the model to run
|
* @param model the model to run
|
||||||
@@ -38,7 +27,8 @@ export type ChatParams = {
|
|||||||
*/
|
*/
|
||||||
export type UserMessage = {
|
export type UserMessage = {
|
||||||
role: string,
|
role: string,
|
||||||
content: string
|
content: string,
|
||||||
|
images: string[] // May or may not have images in message
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event properties
|
// Event properties
|
||||||
@@ -46,7 +36,6 @@ export interface EventProps {
|
|||||||
client: Client
|
client: Client
|
||||||
log: LogMethod
|
log: LogMethod
|
||||||
msgHist: Queue<UserMessage>
|
msgHist: Queue<UserMessage>
|
||||||
tokens: Tokens,
|
|
||||||
ollama: Ollama
|
ollama: Ollama
|
||||||
}
|
}
|
||||||
export type EventCallback<T extends EventKeys> = (
|
export type EventCallback<T extends EventKeys> = (
|
||||||
@@ -69,14 +58,12 @@ export function event<T extends EventKeys>(key: T, callback: EventCallback<T>):
|
|||||||
* @param client initialized bot client
|
* @param client initialized bot client
|
||||||
* @param events all the exported events from the index.ts in the events dir
|
* @param events all the exported events from the index.ts in the events dir
|
||||||
* @param msgHist The message history of the bot
|
* @param msgHist The message history of the bot
|
||||||
* @param tokens the passed in environment tokens for the service
|
|
||||||
* @param ollama the initialized ollama instance
|
* @param ollama the initialized ollama instance
|
||||||
*/
|
*/
|
||||||
export function registerEvents(
|
export function registerEvents(
|
||||||
client: Client,
|
client: Client,
|
||||||
events: Event[],
|
events: Event[],
|
||||||
msgHist: Queue<UserMessage>,
|
msgHist: Queue<UserMessage>,
|
||||||
tokens: Tokens,
|
|
||||||
ollama: Ollama
|
ollama: Ollama
|
||||||
): void {
|
): void {
|
||||||
for (const { key, callback } of events) {
|
for (const { key, callback } of events) {
|
||||||
@@ -86,7 +73,7 @@ export function registerEvents(
|
|||||||
|
|
||||||
// Handle Errors, call callback, log errors as needed
|
// Handle Errors, call callback, log errors as needed
|
||||||
try {
|
try {
|
||||||
callback({ client, log, msgHist, tokens, ollama }, ...args)
|
callback({ client, log, msgHist, ollama }, ...args)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log('[Uncaught Error]', error)
|
log('[Uncaught Error]', error)
|
||||||
}
|
}
|
||||||
|
|||||||
57
src/utils/handlers/bufferHandler.ts
Normal file
57
src/utils/handlers/bufferHandler.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import { Attachment } from "discord.js"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to convert a Discord attachment url to an array buffer
|
||||||
|
*
|
||||||
|
* @param url Discord Attachment Url
|
||||||
|
* @returns array buffer from Attachment Url
|
||||||
|
*/
|
||||||
|
async function getAttachmentBuffer(url: string): Promise<ArrayBuffer> {
|
||||||
|
// Get the data from the image
|
||||||
|
const response = await fetch(url)
|
||||||
|
|
||||||
|
// Validate the image came in fine
|
||||||
|
if (!response.ok)
|
||||||
|
throw new Error('Failed to fetch the attachment.')
|
||||||
|
|
||||||
|
// Return image as Buffer
|
||||||
|
return await response.arrayBuffer()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to convert an array buffer to a Base64 String
|
||||||
|
*
|
||||||
|
* @param buffer Array Buffer from attachment
|
||||||
|
* @returns converted Base64 string
|
||||||
|
*/
|
||||||
|
function arrayBufferToBase64(buffer: ArrayBuffer): string {
|
||||||
|
// Converting to Uint8Array
|
||||||
|
const uint8Array = new Uint8Array(buffer)
|
||||||
|
let binary = ''
|
||||||
|
const len = uint8Array.byteLength;
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
binary += String.fromCharCode(uint8Array[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return as Base64
|
||||||
|
return Buffer.from(binary, 'binary').toString('base64')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to retrieve the Base64 Array of provided Message Attachment
|
||||||
|
*
|
||||||
|
* @param attachment Message Attachment from Discord
|
||||||
|
* @returns Base64 string array
|
||||||
|
*/
|
||||||
|
export async function getAttachmentData(attachment: Attachment | undefined): Promise<string[]> {
|
||||||
|
const url: string = attachment !== undefined ? attachment.url : "Missing Url"
|
||||||
|
|
||||||
|
// case of no attachment
|
||||||
|
if (url === "Missing Url")
|
||||||
|
return []
|
||||||
|
|
||||||
|
// Convert data to base64
|
||||||
|
const buffer = await getAttachmentBuffer(url)
|
||||||
|
const base64String = arrayBufferToBase64(buffer)
|
||||||
|
return [base64String]
|
||||||
|
}
|
||||||
@@ -1,63 +1,8 @@
|
|||||||
import { TextChannel, ThreadChannel } from 'discord.js'
|
import { TextChannel, ThreadChannel } from 'discord.js'
|
||||||
import { Configuration, Thread, Channel, UserMessage } from '../index.js'
|
import { Configuration, Channel, UserMessage } from '../index.js'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
|
||||||
/**
|
|
||||||
* Method to open/create and modify a json file containing thread information
|
|
||||||
*
|
|
||||||
* @param filename name of the thread file
|
|
||||||
* @param thread the thread with all of the interactions
|
|
||||||
* @param message message contents and from who
|
|
||||||
*/
|
|
||||||
export function openThreadInfo(filename: string, thread: ThreadChannel, messages: UserMessage[] = []) {
|
|
||||||
// check if the file exists, if not then make the config file
|
|
||||||
const fullFileName = `data/${filename}`
|
|
||||||
if (fs.existsSync(fullFileName)) {
|
|
||||||
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
|
||||||
if (error)
|
|
||||||
console.log(`[Error: openThreadInfo] Incorrect file format`)
|
|
||||||
else {
|
|
||||||
const object = JSON.parse(data)
|
|
||||||
object['messages'] = messages as []
|
|
||||||
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else { // file doesn't exist, create it
|
|
||||||
const object: Configuration = JSON.parse(`{ \"id\": \"${thread?.id}\", \"name\": \"${thread?.name}\", \"messages\": []}`)
|
|
||||||
|
|
||||||
const directory = path.dirname(fullFileName)
|
|
||||||
if (!fs.existsSync(directory))
|
|
||||||
fs.mkdirSync(directory, { recursive: true })
|
|
||||||
|
|
||||||
// only creating it, no need to add anything
|
|
||||||
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
|
||||||
console.log(`[Util: openThreadInfo] Created '${fullFileName}' in working directory`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Method to obtain the configurations of the message chat/thread
|
|
||||||
*
|
|
||||||
* @param filename name of the configuration file to get
|
|
||||||
* @param callback function to allow a promise from getting the config
|
|
||||||
*/
|
|
||||||
export async function getThread(filename: string, callback: (config: Thread | undefined) => void): Promise<void> {
|
|
||||||
// attempt to read the file and get the configuration
|
|
||||||
const fullFileName = `data/${filename}`
|
|
||||||
if (fs.existsSync(fullFileName)) {
|
|
||||||
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
|
||||||
if (error) {
|
|
||||||
callback(undefined)
|
|
||||||
return // something went wrong... stop
|
|
||||||
}
|
|
||||||
callback(JSON.parse(data))
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
callback(undefined) // file not found
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to check if a thread history file exists
|
* Method to check if a thread history file exists
|
||||||
*
|
*
|
||||||
@@ -65,16 +10,15 @@ export async function getThread(filename: string, callback: (config: Thread | un
|
|||||||
* @returns true if channel does not exist, false otherwise
|
* @returns true if channel does not exist, false otherwise
|
||||||
*/
|
*/
|
||||||
async function checkChannelInfoExists(channel: TextChannel, user: string) {
|
async function checkChannelInfoExists(channel: TextChannel, user: string) {
|
||||||
// thread exist handler
|
const doesExists: boolean = await new Promise((resolve) => {
|
||||||
const isThread: boolean = await new Promise((resolve) => {
|
getChannelInfo(`${channel.id}-${user}.json`, (channelInfo) => {
|
||||||
getThread(`${channel.id}-${user}.json`, (channelInfo) => {
|
|
||||||
if (channelInfo?.messages)
|
if (channelInfo?.messages)
|
||||||
resolve(true)
|
resolve(true)
|
||||||
else
|
else
|
||||||
resolve(false)
|
resolve(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
return isThread
|
return doesExists
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,19 +65,7 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
|
|||||||
* @param user the user's name
|
* @param user the user's name
|
||||||
* @param messages their messages
|
* @param messages their messages
|
||||||
*/
|
*/
|
||||||
export async function openChannelInfo(filename: string, channel: TextChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
export async function openChannelInfo(filename: string, channel: TextChannel | ThreadChannel, user: string, messages: UserMessage[] = []): Promise<void> {
|
||||||
const isThread: boolean = await new Promise((resolve) => {
|
|
||||||
getThread(`${channel.id}.json`, (threadInfo) => {
|
|
||||||
if (threadInfo?.messages)
|
|
||||||
resolve(true)
|
|
||||||
else
|
|
||||||
resolve(false)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// this is a thread channel, do not duplicate files
|
|
||||||
if (isThread) return
|
|
||||||
|
|
||||||
const fullFileName = `data/${filename}-${user}.json`
|
const fullFileName = `data/${filename}-${user}.json`
|
||||||
if (fs.existsSync(fullFileName)) {
|
if (fs.existsSync(fullFileName)) {
|
||||||
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ export * from './messageEmbed.js'
|
|||||||
export * from './messageNormal.js'
|
export * from './messageNormal.js'
|
||||||
export * from './commands.js'
|
export * from './commands.js'
|
||||||
export * from './configInterfaces.js'
|
export * from './configInterfaces.js'
|
||||||
|
export * from './mentionClean.js'
|
||||||
|
|
||||||
// handler imports
|
// handler imports
|
||||||
export * from './handlers/chatHistoryHandler.js'
|
export * from './handlers/chatHistoryHandler.js'
|
||||||
export * from './handlers/configHandler.js'
|
export * from './handlers/configHandler.js'
|
||||||
export * from './handlers/streamHandler.js'
|
export * from './handlers/streamHandler.js'
|
||||||
|
export * from './handlers/bufferHandler.js'
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ import Keys from "../keys.js"
|
|||||||
* - replace function works well for this
|
* - replace function works well for this
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message
|
||||||
* @returns
|
* @returns message without client id
|
||||||
*/
|
*/
|
||||||
export function clean(message: string): string {
|
export function clean(message: string, clientId: string): string {
|
||||||
const cleanedMessage: string = message.replace(`<@${Keys.clientUid}>`, '').trim()
|
const cleanedMessage: string = message.replace(`<@${clientId}>`, '').trim()
|
||||||
return cleanedMessage
|
return cleanedMessage
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { EmbedBuilder, Message } from 'discord.js'
|
import { EmbedBuilder, Message, SendableChannels } from 'discord.js'
|
||||||
import { ChatResponse, Ollama } from 'ollama'
|
import { ChatResponse, Ollama } from 'ollama'
|
||||||
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
||||||
import { Queue } from '../queues/queue.js'
|
import { Queue } from '../queues/queue.js'
|
||||||
@@ -7,15 +7,13 @@ import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
|||||||
/**
|
/**
|
||||||
* Method to send replies as normal text on discord like any other user
|
* Method to send replies as normal text on discord like any other user
|
||||||
* @param message message sent by the user
|
* @param message message sent by the user
|
||||||
* @param tokens tokens to run query
|
* @param model name of model to run query
|
||||||
* @param msgHist message history between user and model
|
* @param msgHist message history between user and model
|
||||||
*/
|
*/
|
||||||
export async function embedMessage(
|
export async function embedMessage(
|
||||||
message: Message,
|
message: Message,
|
||||||
ollama: Ollama,
|
ollama: Ollama,
|
||||||
tokens: {
|
model: string,
|
||||||
model: string
|
|
||||||
},
|
|
||||||
msgHist: Queue<UserMessage>,
|
msgHist: Queue<UserMessage>,
|
||||||
stream: boolean
|
stream: boolean
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
@@ -30,11 +28,12 @@ export async function embedMessage(
|
|||||||
.setColor('#00FF00')
|
.setColor('#00FF00')
|
||||||
|
|
||||||
// send the message
|
// send the message
|
||||||
const sentMessage = await message.channel.send({ embeds: [botMessage] })
|
const channel = message.channel as SendableChannels
|
||||||
|
const sentMessage = await channel.send({ embeds: [botMessage] })
|
||||||
|
|
||||||
// create params
|
// create params
|
||||||
const params: ChatParams = {
|
const params: ChatParams = {
|
||||||
model: tokens.model,
|
model: model,
|
||||||
ollama: ollama,
|
ollama: ollama,
|
||||||
msgHist: msgHist.getItems()
|
msgHist: msgHist.getItems()
|
||||||
}
|
}
|
||||||
@@ -50,12 +49,12 @@ export async function embedMessage(
|
|||||||
// exceeds handled length
|
// exceeds handled length
|
||||||
if (result.length > 5000) {
|
if (result.length > 5000) {
|
||||||
const errorEmbed = new EmbedBuilder()
|
const errorEmbed = new EmbedBuilder()
|
||||||
.setTitle(`Responding to ${message.author.tag}`)
|
.setTitle(`Responding to ${message.author.tag}`)
|
||||||
.setDescription(`Response length ${result.length} has exceeded Discord maximum.\n\nLong Stream messages not supported.`)
|
.setDescription(`Response length ${result.length} has exceeded Discord maximum.\n\nLong Stream messages not supported.`)
|
||||||
.setColor('#00FF00')
|
.setColor('#00FF00')
|
||||||
|
|
||||||
// send error
|
// send error
|
||||||
message.channel.send({ embeds: [errorEmbed] })
|
channel.send({ embeds: [errorEmbed] })
|
||||||
break // cancel loop and stop
|
break // cancel loop and stop
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +91,7 @@ export async function embedMessage(
|
|||||||
.setDescription(result.slice(0, 5000) || 'No Content to Provide...')
|
.setDescription(result.slice(0, 5000) || 'No Content to Provide...')
|
||||||
.setColor('#00FF00')
|
.setColor('#00FF00')
|
||||||
|
|
||||||
message.channel.send({ embeds: [whileEmbed] })
|
channel.send({ embeds: [whileEmbed] })
|
||||||
result = result.slice(5000)
|
result = result.slice(5000)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +101,7 @@ export async function embedMessage(
|
|||||||
.setColor('#00FF00')
|
.setColor('#00FF00')
|
||||||
|
|
||||||
// rest of the response
|
// rest of the response
|
||||||
message.channel.send({ embeds: [lastEmbed] })
|
channel.send({ embeds: [lastEmbed] })
|
||||||
} else {
|
} else {
|
||||||
// only need to create 1 embed, handles 6000 characters
|
// only need to create 1 embed, handles 6000 characters
|
||||||
const newEmbed = new EmbedBuilder()
|
const newEmbed = new EmbedBuilder()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Message } from 'discord.js'
|
import { Message, SendableChannels } from 'discord.js'
|
||||||
import { ChatResponse, Ollama } from 'ollama'
|
import { ChatResponse, Ollama } from 'ollama'
|
||||||
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
import { ChatParams, UserMessage, streamResponse, blockResponse } from './index.js'
|
||||||
import { Queue } from '../queues/queue.js'
|
import { Queue } from '../queues/queue.js'
|
||||||
@@ -7,26 +7,25 @@ import { AbortableAsyncIterator } from 'ollama/src/utils.js'
|
|||||||
/**
|
/**
|
||||||
* Method to send replies as normal text on discord like any other user
|
* Method to send replies as normal text on discord like any other user
|
||||||
* @param message message sent by the user
|
* @param message message sent by the user
|
||||||
* @param tokens tokens to run query
|
* @param model name of model to run query
|
||||||
* @param msgHist message history between user and model
|
* @param msgHist message history between user and model
|
||||||
*/
|
*/
|
||||||
export async function normalMessage(
|
export async function normalMessage(
|
||||||
message: Message,
|
message: Message,
|
||||||
ollama: Ollama,
|
ollama: Ollama,
|
||||||
tokens: {
|
model: string,
|
||||||
model: string
|
|
||||||
},
|
|
||||||
msgHist: Queue<UserMessage>,
|
msgHist: Queue<UserMessage>,
|
||||||
stream: boolean
|
stream: boolean
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
// bot's respnse
|
// bot's respnse
|
||||||
let response: ChatResponse | AbortableAsyncIterator<ChatResponse>
|
let response: ChatResponse | AbortableAsyncIterator<ChatResponse>
|
||||||
let result: string = ''
|
let result: string = ''
|
||||||
|
const channel = message.channel as SendableChannels
|
||||||
|
|
||||||
await message.channel.send('Generating Response . . .').then(async sentMessage => {
|
await channel.send('Generating Response . . .').then(async sentMessage => {
|
||||||
try {
|
try {
|
||||||
const params: ChatParams = {
|
const params: ChatParams = {
|
||||||
model: tokens.model,
|
model: model,
|
||||||
ollama: ollama,
|
ollama: ollama,
|
||||||
msgHist: msgHist.getItems()
|
msgHist: msgHist.getItems()
|
||||||
}
|
}
|
||||||
@@ -41,7 +40,7 @@ export async function normalMessage(
|
|||||||
result = portion.message.content
|
result = portion.message.content
|
||||||
|
|
||||||
// new message block, wait for it to send and assign new block to respond.
|
// new message block, wait for it to send and assign new block to respond.
|
||||||
await message.channel.send("Creating new stream block...").then(sentMessage => { messageBlock = sentMessage })
|
await channel.send("Creating new stream block...").then(sentMessage => { messageBlock = sentMessage })
|
||||||
} else {
|
} else {
|
||||||
result += portion.message.content
|
result += portion.message.content
|
||||||
|
|
||||||
@@ -63,12 +62,12 @@ export async function normalMessage(
|
|||||||
|
|
||||||
// handle for rest of message that is >2000
|
// handle for rest of message that is >2000
|
||||||
while (result.length > 2000) {
|
while (result.length > 2000) {
|
||||||
message.channel.send(result.slice(0, 2000))
|
channel.send(result.slice(0, 2000))
|
||||||
result = result.slice(2000)
|
result = result.slice(2000)
|
||||||
}
|
}
|
||||||
|
|
||||||
// last part of message
|
// last part of message
|
||||||
message.channel.send(result)
|
channel.send(result)
|
||||||
} else // edit the 'generic' response to new message since <2000
|
} else // edit the 'generic' response to new message since <2000
|
||||||
sentMessage.edit(result)
|
sentMessage.edit(result)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
// expect takes a value from an expression
|
// expect takes a value from an expression
|
||||||
// it marks a test case
|
// it marks a test case
|
||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import commands from '../src/commands'
|
import commands from '../src/commands/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Commands test suite, tests the commands object
|
* Commands test suite, tests the commands object
|
||||||
@@ -12,7 +12,7 @@ import commands from '../src/commands'
|
|||||||
* @param name name of the test suite
|
* @param name name of the test suite
|
||||||
* @param fn function holding tests to run
|
* @param fn function holding tests to run
|
||||||
*/
|
*/
|
||||||
describe('#commands', () => {
|
describe('Commands Existence', () => {
|
||||||
// test definition of commands object
|
// test definition of commands object
|
||||||
it('references defined object', () => {
|
it('references defined object', () => {
|
||||||
// toBe compares the value to the expected value
|
// toBe compares the value to the expected value
|
||||||
@@ -22,6 +22,51 @@ describe('#commands', () => {
|
|||||||
// test specific commands in the object
|
// test specific commands in the object
|
||||||
it('references specific commands', () => {
|
it('references specific commands', () => {
|
||||||
const commandsString = commands.map(e => e.name).join(', ')
|
const commandsString = commands.map(e => e.name).join(', ')
|
||||||
expect(commandsString).toBe('thread, private-thread, message-style, message-stream, toggle-chat, shutoff, modify-capacity, channel-toggle, clear-user-channel-history')
|
expect(commandsString).toBe('thread, private-thread, message-style, message-stream, toggle-chat, shutoff, modify-capacity, clear-user-channel-history, pull-model, switch-model')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User Commands Test suite for testing out commands
|
||||||
|
* that would be run by users when using the application.
|
||||||
|
*/
|
||||||
|
describe('User Command Tests', () => {
|
||||||
|
// test capacity command
|
||||||
|
it('run modify-capacity command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run clear-user-channel-history command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run message-stream command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run message-style command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run thread command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run private-thread command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Admin Commands Test suite for running administrative
|
||||||
|
* commands with the application.
|
||||||
|
*/
|
||||||
|
describe('Admin Command Tests', () => {
|
||||||
|
it('run shutoff command', () => {
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('run toggle-chat command', () => {
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it, vi } from 'vitest'
|
||||||
import events from '../src/events'
|
import events from '../src/events/index.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mocking ollama found in client.ts because pullModel.ts
|
||||||
|
* relies on the existence on ollama. To prevent the mock,
|
||||||
|
* we will have to pass through ollama to the commands somehow.
|
||||||
|
*/
|
||||||
|
vi.mock('../src/client.js', () => ({
|
||||||
|
ollama: {
|
||||||
|
pull: vi.fn() // Mock the pull method found with ollama
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Events test suite, tests the events object
|
* Events test suite, tests the events object
|
||||||
* Each event is to be tested elsewhere, this file
|
* Each event is to be tested elsewhere, this file
|
||||||
* is to ensure that the events object is defined.
|
* is to ensure that the events object is defined.
|
||||||
*
|
|
||||||
* @param name name of the test suite
|
|
||||||
* @param fn function holding tests to run
|
|
||||||
*/
|
*/
|
||||||
describe('#events', () => {
|
describe('Events Existence', () => {
|
||||||
// test definition of events object
|
// test definition of events object
|
||||||
it('references defined object', () => {
|
it('references defined object', () => {
|
||||||
expect(typeof events).toBe('object')
|
expect(typeof events).toBe('object')
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { getEnvVar } from '../src/utils'
|
import { getEnvVar } from '../src/utils/index.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getEnvVar test suite, tests the getEnvVar function
|
* getEnvVar test suite, tests the getEnvVar function
|
||||||
@@ -7,7 +7,7 @@ import { getEnvVar } from '../src/utils'
|
|||||||
* @param name name of the test suite
|
* @param name name of the test suite
|
||||||
* @param fn function holding tests to run
|
* @param fn function holding tests to run
|
||||||
*/
|
*/
|
||||||
describe('#getEnvVar', () => {
|
describe('Environment Setup', () => {
|
||||||
// dummy set of keys
|
// dummy set of keys
|
||||||
const keys = {
|
const keys = {
|
||||||
clientToken: 'CLIENT_TOKEN',
|
clientToken: 'CLIENT_TOKEN',
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { clean } from '../src/utils/mentionClean'
|
import { clean } from '../src/utils/index.js'
|
||||||
import { getEnvVar } from '../src/utils'
|
|
||||||
|
// Sample UID for testing
|
||||||
|
const sampleId = '123456789'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MentionClean test suite, tests the clean function
|
* MentionClean test suite, tests the clean function
|
||||||
@@ -8,10 +10,10 @@ import { getEnvVar } from '../src/utils'
|
|||||||
* @param name name of the test suite
|
* @param name name of the test suite
|
||||||
* @param fn function holding tests to run
|
* @param fn function holding tests to run
|
||||||
*/
|
*/
|
||||||
describe('#clean', () => {
|
describe('Mentions Cleaned', () => {
|
||||||
// test for id removal from message
|
// test for id removal from message
|
||||||
it('removes the mention from a message', () => {
|
it('removes the mention from a message', () => {
|
||||||
const message = `<@${getEnvVar('CLIENT_UID')}> Hello, World!`
|
const message = `<@${sampleId}> Hello, World!`
|
||||||
expect(clean(message)).toBe('Hello, World!')
|
expect(clean(message, sampleId)).toBe('Hello, World!')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import { Queue } from '../src/queues/queue'
|
import { Queue } from '../src/queues/queue.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queue test suite, tests the Queue class
|
* Queue test suite, tests the Queue class
|
||||||
@@ -7,7 +7,7 @@ import { Queue } from '../src/queues/queue'
|
|||||||
* @param name name of the test suite
|
* @param name name of the test suite
|
||||||
* @param fn function holding tests to run
|
* @param fn function holding tests to run
|
||||||
*/
|
*/
|
||||||
describe('#queue', () => {
|
describe('Queue Structure', () => {
|
||||||
let queue= new Queue<string>()
|
let queue= new Queue<string>()
|
||||||
|
|
||||||
// test for queue creation
|
// test for queue creation
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import { defineConfig, configDefaults } from 'vitest/config'
|
|||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
test: {
|
test: {
|
||||||
globals: true, // <-- reduces test file imports
|
globals: true, // <-- reduces test file imports
|
||||||
|
reporters: ['verbose'], // <-- verbose output
|
||||||
coverage: {
|
coverage: {
|
||||||
exclude: [...configDefaults.exclude, 'build/*'], // <-- exclude JS build
|
exclude: [...configDefaults.exclude, 'build/*', 'tests/*'], // <-- exclude JS build
|
||||||
reporter: ['text', 'html'] // <-- reports in text, html
|
reporter: ['text-summary'] // <-- report in text-summary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user