Compare commits

..
10 Commits
Author SHA1 Message Date
JT2M0L3Y 288c0eb08f pass root logger, don't init new ones 2026-10-21 19:31:02 -07:00
JT2M0L3Y 217291e531 implement pino poc 2026-10-21 19:09:28 -07:00
JT2M0L3Y 99db6d8163 Update: fix imports based on last pkg fix 2026-09-23 10:57:14 -07:00
JT2M0L3Y 3a770be5de Added: defined objects directory 2026-09-23 10:24:07 -07:00
JT2M0L3Y f8d8fe765f Update: utility method logs use method name 2026-09-23 10:24:05 -07:00
Kevin Dang 2c25f7f500 Upgrade Node to Krypton (#200)
* node 24 and vulnerabilities

* ready is clientReady

* better about section
2026-09-23 09:52:28 -07:00
Eric Curtin 0beaed85aa Document llmman and rename OLLAMA_IP/PORT to LLM_ENDPOINT/PORT (#198)
llmman (https://github.com/llmmanorg/llmman) serves the Ollama API on
port 17434 and works with the bot as-is, so document it in setup-local
and link it from the README. Since the backend is no longer Ollama-only,
rename the env vars to LLM_ENDPOINT / LLM_PORT as requested in review.
OLLAMA_IP / OLLAMA_PORT are kept as deprecated aliases in keys.ts and
docker-compose.yml so existing deployments keep working.
2026-09-10 17:42:42 -07:00
Kevin Dang 69637722db Separate Test File for Fork PRs (#199) 2026-09-05 07:31:59 -07:00
Jonathan Smoley 3d6d651e18 drop refs to coverage (#197) 2026-07-17 11:30:46 -07:00
Jonathan Smoley 7583d6d179 Fix: Code Coverage Badge Failed (#196)
* switch coverage to codecov

* reset node, increment ver

* remove auto-pass job var

* remove coverage 'env' note

* set badge with new action

* add fqvn for action

* fix step name

* make badge commit explicit
2026-07-16 17:44:11 -07:00
42 changed files with 1730 additions and 2028 deletions
+6 -4
View File
@@ -4,11 +4,13 @@ CLIENT_TOKEN = BOT_TOKEN
# Default model for new users # Default model for new users
MODEL = DEFAULT_MODEL MODEL = DEFAULT_MODEL
# ip/port address of docker container, I use 172.18.0.3 for docker, 127.0.0.1 for local # ip/port address of the LLM server (ollama, llmman, ...), I use 172.18.0.3 for docker, 127.0.0.1 for local
OLLAMA_IP = IP_ADDRESS # ollama listens on 11434, llmman (https://github.com/llmmanorg/llmman) on 17434
OLLAMA_PORT = PORT # OLLAMA_IP / OLLAMA_PORT are still accepted as deprecated aliases
LLM_ENDPOINT = IP_ADDRESS
LLM_PORT = PORT
# ip address for discord bot container, I use 172.18.0.2, use different IP than ollama_ip # ip address for discord bot container, I use 172.18.0.2, use different IP than llm_endpoint
DISCORD_IP = IP_ADDRESS DISCORD_IP = IP_ADDRESS
# subnet address, ex. 172.18.0.0 as we use /16. # subnet address, ex. 172.18.0.0 as we use /16.
+13 -11
View File
@@ -1,5 +1,6 @@
name: Builds name: Builds
run-name: Validate Node and Docker Builds run-name: Validate Node and Docker Builds
on: on:
push: push:
branches: branches:
@@ -9,15 +10,16 @@ jobs:
Discord-Node-Build: # test if the node install and run Discord-Node-Build: # test if the node install and run
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 2 timeout-minutes: 2
steps: steps:
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Node Environment lts/jod - name: Set up Node Environment
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: lts/jod node-version: lts/krypton
cache: "npm" cache: npm
- name: Install Project Dependencies - name: Install Project Dependencies
run: | run: |
@@ -31,8 +33,8 @@ jobs:
run: | run: |
touch .env touch .env
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
echo MODEL = ${{ secrets.MODEL }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env
# set -e ensures if nohup fails, this section fails # set -e ensures if nohup fails, this section fails
@@ -43,23 +45,23 @@ jobs:
Discord-Ollama-Container-Build: # test docker build and run Discord-Ollama-Container-Build: # test docker build and run
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 2 timeout-minutes: 3
steps: steps:
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Node Environment lts/jod - name: Set up Node Environment
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: lts/jod node-version: lts/krypton
cache: "npm" cache: npm
- name: Create Environment Variables - name: Create Environment Variables
run: | run: |
touch .env touch .env
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
echo MODEL = ${{ secrets.MODEL }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env
- name: Setup Docker Network and Images - name: Setup Docker Network and Images
-50
View File
@@ -1,50 +0,0 @@
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/jod
uses: actions/setup-node@v4
with:
node-version: lts/jod
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
echo MODEL = ${{ secrets.MODEL }} >> .env
- name: Collect Code Coverage
run: |
LINE_PCT=$(npm run 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
+5 -7
View File
@@ -1,9 +1,6 @@
name: Deploy name: Deploy
run-name: Deploy Application Latest run-name: Deploy Application Latest
# on:
# push:
# tags:
# - 'v*'
on: workflow_dispatch on: workflow_dispatch
jobs: jobs:
@@ -11,6 +8,7 @@ jobs:
runs-on: self-hosted runs-on: self-hosted
environment: deploy environment: deploy
timeout-minutes: 5 timeout-minutes: 5
steps: steps:
- name: Checkout Repo - name: Checkout Repo
uses: actions/checkout@v4 uses: actions/checkout@v4
@@ -20,8 +18,8 @@ jobs:
run: | run: |
touch .env touch .env
echo CLIENT_TOKEN = ${{ secrets.CLIENT }} >> .env echo CLIENT_TOKEN = ${{ secrets.CLIENT }} >> .env
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
echo MODEL = ${{ secrets.MODEL }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env
echo DISCORD_IP = ${{ secrets.DISCORD_IP }} >> .env echo DISCORD_IP = ${{ secrets.DISCORD_IP }} >> .env
echo SUBNET_ADDRESS = ${{ secrets.SUBNET_ADDRESS }} >> .env echo SUBNET_ADDRESS = ${{ secrets.SUBNET_ADDRESS }} >> .env
@@ -40,7 +38,7 @@ jobs:
git clone https://github.com/kevinthedang/discord-ollama.git ${{ secrets.PATH }} git clone https://github.com/kevinthedang/discord-ollama.git ${{ secrets.PATH }}
cd ${{ secrets.PATH }} cd ${{ secrets.PATH }}
- name: Install nvm and Node.js lts/jod - name: Install nvm and Node.js
run: | run: |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
export NVM_DIR="$HOME/.nvm" export NVM_DIR="$HOME/.nvm"
+47
View File
@@ -0,0 +1,47 @@
name: Tests (Fork PRs)
run-name: Unit Tests
on:
pull_request:
branches:
- master
paths:
- '*'
- 'package*.json'
- 'src/**'
- 'tests/**'
- '!docs/**'
- '!imgs/**'
- '!.github/**'
- '.github/workflows/**'
- '!.gitignore'
- '!LICENSE'
- '!README'
jobs:
Discord-Node-Test-Fork:
if: ${{ github.event.pull_request.head.repo.fork == true }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 'lts/jod'
cache: npm
- name: Install Project Dependencies
run: |
npm install
- name: Create Dummy Env
run: |
echo CLIENT_TOKEN=test-token >> .env
echo LLM_ENDPOINT=127.0.0.1 >> .env
echo LLM_PORT=11434 >> .env
echo MODEL=dummy-model >> .env
- name: Test Application
run: |
npm run tests
+7 -4
View File
@@ -1,5 +1,6 @@
name: Tests name: Tests
run-name: Unit Tests run-name: Unit Tests
on: on:
pull_request: pull_request:
branches: branches:
@@ -19,17 +20,19 @@ on:
jobs: jobs:
Discord-Node-Test: Discord-Node-Test:
if: ${{ github.event.pull_request.head.repo.fork == false }}
runs-on: ubuntu-latest runs-on: ubuntu-latest
timeout-minutes: 2 timeout-minutes: 2
steps: steps:
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Set up Node Environment lts/jod - name: Set up Node Environment
uses: actions/setup-node@v4 uses: actions/setup-node@v4
with: with:
node-version: lts/jod node-version: lts/jod
cache: "npm" cache: npm
- name: Install Project Dependencies - name: Install Project Dependencies
run: | run: |
@@ -39,8 +42,8 @@ jobs:
run: | run: |
touch .env touch .env
echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env echo CLIENT_TOKEN = ${{ secrets.BOT_TOKEN }} >> .env
echo OLLAMA_IP = ${{ secrets.OLLAMA_IP }} >> .env echo LLM_ENDPOINT = ${{ secrets.OLLAMA_IP }} >> .env
echo OLLAMA_PORT = ${{ secrets.OLLAMA_PORT }} >> .env echo LLM_PORT = ${{ secrets.OLLAMA_PORT }} >> .env
echo MODEL = ${{ secrets.MODEL }} >> .env echo MODEL = ${{ secrets.MODEL }} >> .env
- name: Test Application - name: Test Application
-10
View File
@@ -37,16 +37,6 @@ pids
*.seed *.seed
*.pid.lock *.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt .grunt
+1 -1
View File
@@ -12,7 +12,7 @@
# Technical/Business Code Ownership # Technical/Business Code Ownership
/src/ @kevinthedang @JT2M0L3Y /src/ @kevinthedang @JT2M0L3Y
/tests/ @JT2M0L3Y /tests/ @JT2M0L3Y
/.github/ @kevinthedang /.github/ @kevinthedang @JT2M0L3Y
# Docker Ownership # Docker Ownership
Dockerfile @kevinthedang Dockerfile @kevinthedang
+2 -2
View File
@@ -1,5 +1,5 @@
# use node LTS image for version 22 # use node LTS image for version 24
FROM node:jod-alpine FROM node:krypton-alpine
# set working directory inside container # set working directory inside container
WORKDIR /app WORKDIR /app
+28 -31
View File
@@ -5,51 +5,47 @@
<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="Builds" 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="Builds" 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/deploy.yml"><img alt="Deploy Status" src="https://github.com/kevinthedang/discord-ollama/actions/workflows/deploy.yml/badge.svg" /></a> -->
<a href="#"></a><a href="https://github.com/kevinthedang/discord-ollama/actions/workflows/test.yml"><img alt="Tests" 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="Tests" 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
Ollama is an AI model management tool that allows users to install and use custom large language models locally. Discord Ollama is a Discord bot that lets users interact with locally hosted
The project aims to: Ollama language models directly from Discord.
* [x] Create a Discord bot that will utilize Ollama and chat to chat with users!
* [x] User and Server Preferences
* [x] Message Persistance
* [x] Containerization with Docker
* [x] Slash Commands Compatible
* [x] Summary Command
* [ ] Model Info Command
* [ ] List Models Command
* [x] Pull Model Command
* [x] Switch Model Command
* [x] Delete Model Command
* [x] Create Thread Command
* [x] Create Private Thread Command
* [x] Message Stream Command
* [x] Change Message History Size Command
* [x] Clear Channel History Command (User Only)
* [x] Administrator Role Compatible
* [x] Generated Token Length Handling for >2000
* [x] Token Length Handling of any message size
* [x] Multi-User Chat Generation - This was built in from Ollama `v0.2.1+`
* [ ] Ollama Tool Support Implementation
* [ ] Enhanced Channel Context Awareness
* [ ] Improved User Replied Triggers
Further, Ollama provides the functionality to utilize custom models or provide context for the top-layer of any model available through the Ollama model library. The bot supports model management, streamed responses, conversation history,
* [Customize a model](https://github.com/ollama/ollama#customize-a-model) private threads, and configurable server controls. Because Ollama runs locally,
* [Modelfile Docs](https://github.com/ollama/ollama/blob/main/docs/modelfile.md) you retain control over the models and data used by the bot.
See the [setup guides](./docs/setup-local.md) to get started.
## Documentation ## Documentation
These are guides to the features and capabilities of this app. These are guides to the features and capabilities of this app.
* [User Slash Commands](./docs/commands-guide.md) * [User Slash Commands](./docs/commands-guide.md)
* [Client Events](./docs/events-guide.md) * [Client Events](./docs/events-guide.md)
## Logging
The application uses Pino child loggers with structured component metadata.
Outside production, `pino-caller` adds the source file and line to each log
entry. Set `LOG_LEVEL` to configure the minimum level (defaults to `info`).
```ts
import { createLogger } from './components/index.js'
const logger = createLogger({ component: 'request', requestId })
logger.info('Request started')
```
Log entries are JSON and include the structured fields provided to the log
method, along with `component` and (outside production) `caller`. The root Pino
instance is shared; create one child when entering a context, then pass that
same child logger through the context's work. Create another child only when
you need to add more scoped bindings; child creation does not mutate its parent.
## 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]`
* The LLM server is configured with `LLM_ENDPOINT` / `LLM_PORT` (`OLLAMA_IP` / `OLLAMA_PORT` still work as deprecated aliases).
* 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) * [Creating a Discord App](./docs/setup-discord-app.md)
* [Local Machine Setup](./docs/setup-local.md) * [Local Machine Setup](./docs/setup-local.md)
@@ -63,6 +59,7 @@ These are guides to the features and capabilities of this app.
* This project requires the use of npm version `10.9.0` or above. * This project requires the use of npm version `10.9.0` or above.
* [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)
* [llmman](https://github.com/llmmanorg/llmman) also serves the Ollama API (on port `17434`) and can be used in its place, see [Local Machine Setup](./docs/setup-local.md#using-with-llmman-alternative-to-ollama).
* [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)
+5 -5
View File
@@ -7,11 +7,11 @@ 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.9.0 image: kevinthedang/discord-ollama:0.9.2
environment: environment:
CLIENT_TOKEN: ${CLIENT_TOKEN} CLIENT_TOKEN: ${CLIENT_TOKEN}
OLLAMA_IP: ${OLLAMA_IP} LLM_ENDPOINT: ${LLM_ENDPOINT:-${OLLAMA_IP}} # OLLAMA_IP is a deprecated alias
OLLAMA_PORT: ${OLLAMA_PORT} LLM_PORT: ${LLM_PORT:-${OLLAMA_PORT}} # OLLAMA_PORT is a deprecated alias
MODEL: ${MODEL} MODEL: ${MODEL}
networks: networks:
ollama-net: ollama-net:
@@ -26,14 +26,14 @@ services:
restart: always restart: always
networks: networks:
ollama-net: ollama-net:
ipv4_address: ${OLLAMA_IP} ipv4_address: ${LLM_ENDPOINT:-${OLLAMA_IP}}
runtime: nvidia # use Nvidia Container Toolkit for GPU support runtime: nvidia # use Nvidia Container Toolkit for GPU support
devices: devices:
- /dev/nvidia0 - /dev/nvidia0
volumes: volumes:
- ollama:/root/.ollama - ollama:/root/.ollama
ports: ports:
- ${OLLAMA_PORT}:${OLLAMA_PORT} - ${LLM_PORT:-${OLLAMA_PORT}}:${LLM_PORT:-${OLLAMA_PORT}}
# create a network that supports giving addresses withing a specific subnet # create a network that supports giving addresses withing a specific subnet
networks: networks:
+3 -3
View File
@@ -43,10 +43,10 @@ sudo systemctl restart docker
* [GitHub repository](https://github.com/NVIDIA/nvidia-container-toolkit?tab=readme-ov-file) for Nvidia Container Toolkit * [GitHub repository](https://github.com/NVIDIA/nvidia-container-toolkit?tab=readme-ov-file) for Nvidia Container Toolkit
## To Run (with Docker and Docker Compose) ## To Run (with Docker and Docker Compose)
* With the inclusion of subnets in the `docker-compose.yml`, you will need to set the `SUBNET_ADDRESS`, `OLLAMA_IP`, `OLLAMA_PORT`, and `DISCORD_IP`. Here are some default values if you don't care: * With the inclusion of subnets in the `docker-compose.yml`, you will need to set the `SUBNET_ADDRESS`, `LLM_ENDPOINT`, `LLM_PORT`, and `DISCORD_IP`. Here are some default values if you don't care:
* `SUBNET_ADDRESS = 172.18.0.0` * `SUBNET_ADDRESS = 172.18.0.0`
* `OLLAMA_IP = 172.18.0.2` * `LLM_ENDPOINT = 172.18.0.2`
* `OLLAMA_PORT = 11434` * `LLM_PORT = 11434`
* `DISCORD_IP = 172.18.0.3` * `DISCORD_IP = 172.18.0.3`
* 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)
+10 -3
View File
@@ -14,11 +14,18 @@
> [!NOTE] > [!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) > 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)
## Using with llmman (alternative to Ollama)
* [llmman](https://github.com/llmmanorg/llmman) is a local model runner that serves the Ollama API on port `17434`. The bot talks to it exactly as it would to Ollama, only the port differs.
* Install it with `curl -fsSL https://raw.githubusercontent.com/llmmanorg/llmman/main/install.sh | sh` (Linux/macOS) or `irm https://raw.githubusercontent.com/llmmanorg/llmman/main/install.ps1 | iex` (Windows).
* Start the server with `llmman serve` and pull a model with `llmman pull [model name]`, for example `llmman pull gemma4` or `llmman pull hf.co/unsloth/Qwen3.5-0.8B-GGUF`.
* Point the bot at it by setting `LLM_ENDPOINT = 127.0.0.1` and `LLM_PORT = 17434` in your `.env`, then set `MODEL` to a model you pulled. `/pull-model`, `/switch-model` and `/delete-model` work the same way.
## 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 `LLM_ENDPOINT` is `127.0.0.1` to work properly.
* You only need your `CLIENT_TOKEN`, `OLLAMA_IP`, `OLLAMA_PORT`. * You only need your `CLIENT_TOKEN`, `LLM_ENDPOINT`, `LLM_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 LLM server ip and port should just use it's defaults by nature. If not, utilize `LLM_ENDPOINT = 127.0.0.1` and `LLM_PORT = 11434`.
* The older `OLLAMA_IP` / `OLLAMA_PORT` names are still accepted as deprecated aliases.
* 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.
+1396 -1800
View File
File diff suppressed because it is too large Load Diff
+9 -6
View File
@@ -1,12 +1,11 @@
{ {
"name": "discord-ollama", "name": "discord-ollama",
"version": "0.9.0", "version": "0.9.2",
"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": {
"tests": "vitest run", "tests": "vitest run",
"coverage": "vitest run --coverage",
"watch": "tsx watch src", "watch": "tsx watch src",
"build": "tsc", "build": "tsc",
"prod": "node .", "prod": "node .",
@@ -28,19 +27,23 @@
"dependencies": { "dependencies": {
"discord.js": "^14.20.0", "discord.js": "^14.20.0",
"dotenv": "^16.5.0", "dotenv": "^16.5.0",
"ollama": "^0.5.15" "ollama": "^0.5.15",
"pino": "^10.4.0",
"pino-caller": "^4.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^22.13.14", "@types/node": "^22.13.14",
"@vitest/coverage-v8": "^3.0.9",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"tsx": "^4.19.3", "tsx": "^4.19.3",
"typescript": "^5.8.2", "typescript": "^5.8.2",
"vitest": "^3.0.4" "vitest": "^5.0.1"
}, },
"type": "module", "type": "module",
"engines": { "engines": {
"npm": ">=10.9.0", "npm": ">=10.9.0",
"node": ">=22.12.0" "node": ">=24.0.0"
},
"allowScripts": {
"esbuild@0.25.12": true
} }
} }
+4 -2
View File
@@ -1,10 +1,12 @@
import { Client, GatewayIntentBits } from 'discord.js' import { Client, GatewayIntentBits } from 'discord.js'
import { Ollama } from 'ollama' import { Ollama } from 'ollama'
import { Queue } from './queues/queue.js' import { Queue, createLogger } from './components/index.js'
import { UserMessage, registerEvents } from './utils/index.js' import { UserMessage, registerEvents } from './utils/index.js'
import Events from './events/index.js' import Events from './events/index.js'
import Keys from './keys.js' import Keys from './keys.js'
const logger = createLogger({ component: 'client' })
// initialize the client with the following permissions when logging in // initialize the client with the following permissions when logging in
const client = new Client({ const client = new Client({
intents: [ intents: [
@@ -32,7 +34,7 @@ registerEvents(client, Events, messageHistory, channelMessageHistory, ollama, Ke
// Try to log in the client // Try to log in the client
await client.login(Keys.clientToken) await client.login(Keys.clientToken)
.catch((error) => { .catch((error) => {
console.error('[Login Error]', error) logger.fatal({ err: error }, 'Client login failed')
process.exit(1) process.exit(1)
}) })
+3 -3
View File
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, ChatInputCommandInteraction, Client, Comm
import { UserCommand, SlashCommand } from '../utils/index.js' import { UserCommand, SlashCommand } from '../utils/index.js'
import { ollama } from '../client.js' import { ollama } from '../client.js'
import { ModelResponse } from 'ollama' import { ModelResponse } from 'ollama'
import type { Logger as PinoLogger } from 'pino'
export const DeleteModel: SlashCommand = { export const DeleteModel: SlashCommand = {
name: 'delete-model', name: 'delete-model',
@@ -18,7 +19,7 @@ export const DeleteModel: SlashCommand = {
], ],
// Delete Model locally stored // Delete Model locally stored
run: async (client: Client, interaction: ChatInputCommandInteraction) => { run: async (client: Client, interaction: ChatInputCommandInteraction, logger: PinoLogger) => {
// defer reply to avoid timeout // defer reply to avoid timeout
await interaction.deferReply() await interaction.deferReply()
const modelInput: string = interaction.options.getString('model-name') as string const modelInput: string = interaction.options.getString('model-name') as string
@@ -42,7 +43,7 @@ export const DeleteModel: SlashCommand = {
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput))) .then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
.catch(error => { .catch(error => {
ollamaOffline = true ollamaOffline = true
console.error(`[Command: delete-model] Failed to connect with Ollama service. Error: ${error.message}`) logger.error({ err: error }, 'Failed to connect to Ollama service')
}) })
// Validate for any issue or if service is running // Validate for any issue or if service is running
@@ -53,7 +54,6 @@ export const DeleteModel: SlashCommand = {
return return
} }
try { try {
// call ollama to delete model // call ollama to delete model
if (modelExists) { if (modelExists) {
+3 -2
View File
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction, Mess
import { ollama } from "../client.js" import { ollama } from "../client.js"
import { ModelResponse } from "ollama" import { ModelResponse } from "ollama"
import { UserCommand, SlashCommand } from "../utils/index.js" import { UserCommand, SlashCommand } from "../utils/index.js"
import type { Logger as PinoLogger } from 'pino'
export const PullModel: SlashCommand = { export const PullModel: SlashCommand = {
name: 'pull-model', name: 'pull-model',
@@ -18,7 +19,7 @@ export const PullModel: SlashCommand = {
], ],
// Pull for model from Ollama library // Pull for model from Ollama library
run: async (client: Client, interaction: ChatInputCommandInteraction) => { run: async (client: Client, interaction: ChatInputCommandInteraction, logger: PinoLogger) => {
// defer reply to avoid timeout // defer reply to avoid timeout
await interaction.deferReply() await interaction.deferReply()
const modelInput: string = interaction.options.getString('model-to-pull') as string const modelInput: string = interaction.options.getString('model-to-pull') as string
@@ -42,7 +43,7 @@ export const PullModel: SlashCommand = {
.then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput))) .then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput)))
.catch(error => { .catch(error => {
ollamaOffline = true ollamaOffline = true
console.error(`[Command: pull-model] Failed to connect with Ollama service. Error: ${error.message}`) logger.error({ err: error }, 'Failed to connect to Ollama service')
}) })
// Validate for any issue or if service is running // Validate for any issue or if service is running
+4 -3
View File
@@ -1,18 +1,19 @@
import { Client, CommandInteraction, MessageFlags } from 'discord.js' import { Client, CommandInteraction, MessageFlags } from 'discord.js'
import { AdminCommand, SlashCommand } from '../utils/index.js' import { AdminCommand, SlashCommand } from '../utils/index.js'
import type { Logger as PinoLogger } from 'pino'
export const Shutoff: SlashCommand = { export const Shutoff: SlashCommand = {
name: 'shutoff', name: 'shutoff',
description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.', description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
// Query for message information and set the style // Query for message information and set the style
run: async (client: Client, interaction: CommandInteraction) => { run: async (client: Client, interaction: CommandInteraction, logger: PinoLogger) => {
// fetch channel and message // fetch channel and message
const channel = await client.channels.fetch(interaction.channelId) const channel = await client.channels.fetch(interaction.channelId)
if (!channel || !AdminCommand.includes(channel.type)) return if (!channel || !AdminCommand.includes(channel.type)) return
// log this, this will probably be improtant for logging who did this // log this, this will probably be improtant for logging who did this
console.log(`[Command: shutoff] User ${interaction.user.tag} attempting to shutdown ${client.user!!.tag}`) logger.info({ actor: interaction.user.tag, bot: client.user!!.tag }, 'Shutdown requested')
// check if admin or false on shutdown // check if admin or false on shutdown
if (!interaction.memberPermissions?.has('Administrator')) { if (!interaction.memberPermissions?.has('Administrator')) {
@@ -29,7 +30,7 @@ export const Shutoff: SlashCommand = {
flags: MessageFlags.Ephemeral flags: MessageFlags.Ephemeral
}) })
console.log(`[Command: shutoff] ${client.user?.tag} is shutting down.`) logger.info({ bot: client.user?.tag }, 'Bot is shutting down')
// clean up client instance and stop // clean up client instance and stop
client.destroy() client.destroy()
+6 -5
View File
@@ -1,5 +1,6 @@
import { Client, CommandInteraction, Message, MessageFlags, SendableChannels } from "discord.js"; import { Client, CommandInteraction, MessageFlags } from "discord.js";
import { accessChannelContext, normalMessage, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js"; import { accessChannelContext, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js";
import type { Logger as PinoLogger } from 'pino'
import { ollama } from "../client.js" import { ollama } from "../client.js"
export const Summary: SlashCommand = { export const Summary: SlashCommand = {
@@ -7,7 +8,7 @@ export const Summary: SlashCommand = {
description: 'provides a summary of the chat history.', description: 'provides a summary of the chat history.',
// Generate Summary from additional context // Generate Summary from additional context
run: async (client: Client, interaction: CommandInteraction) => { run: async (client: Client, interaction: CommandInteraction, logger: PinoLogger) => {
// fetch channel // fetch channel
const channel = await client.channels.fetch(interaction.channelId) const channel = await client.channels.fetch(interaction.channelId)
if (!channel || !UserCommand.includes(channel.type)) return if (!channel || !UserCommand.includes(channel.type)) return
@@ -41,9 +42,9 @@ export const Summary: SlashCommand = {
}) })
// todo: instead of a default of codellama, we can user default model somehow. Look into later. // todo: instead of a default of codellama, we can user default model somehow. Look into later.
const response: string = await summarizeContextHistory(ollama, "codellama", channelContext) const response: string = await summarizeContextHistory(logger, ollama, "codellama", channelContext)
console.log(response) logger.info({ response }, 'Generated channel summary')
interaction.editReply({ content: response }) interaction.editReply({ content: response })
} }
+3 -2
View File
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction } fro
import { ollama } from "../client.js" import { ollama } from "../client.js"
import { ModelResponse } from "ollama" import { ModelResponse } from "ollama"
import { openConfig, UserCommand, SlashCommand } from "../utils/index.js" import { openConfig, UserCommand, SlashCommand } from "../utils/index.js"
import type { Logger as PinoLogger } from 'pino'
export const SwitchModel: SlashCommand = { export const SwitchModel: SlashCommand = {
name: 'switch-model', name: 'switch-model',
@@ -18,7 +19,7 @@ export const SwitchModel: SlashCommand = {
], ],
// Switch user preferred model if available in local library // Switch user preferred model if available in local library
run: async (client: Client, interaction: ChatInputCommandInteraction) => { run: async (client: Client, interaction: ChatInputCommandInteraction, logger: PinoLogger) => {
await interaction.deferReply() await interaction.deferReply()
const modelInput: string = interaction.options.getString('model-to-use') as string const modelInput: string = interaction.options.getString('model-to-use') as string
@@ -46,7 +47,7 @@ export const SwitchModel: SlashCommand = {
} }
}) })
.catch(error => { .catch(error => {
console.error(`[Command: switch-model] Failed to connect with Ollama service. Error: ${error.message}`) logger.error({ err: error }, 'Failed to connect to Ollama service')
}) })
// todo: problem can be here if async messes up // todo: problem can be here if async messes up
if (switchSuccess) { if (switchSuccess) {
+2
View File
@@ -0,0 +1,2 @@
export * from './queue.js'
export * from './logger.js'
+15
View File
@@ -0,0 +1,15 @@
import 'dotenv/config'
import pino, { type Logger as PinoLogger } from 'pino'
import { pinoCaller } from 'pino-caller'
export const rootLogger = pino({
level: process.env.LOG_LEVEL ?? 'info'
})
export function createLogger(bindings: pino.Bindings, parent: PinoLogger = rootLogger): PinoLogger {
const childLogger = parent.child(bindings)
return process.env.NODE_ENV === 'production'
? childLogger
: pinoCaller(childLogger, { relativeTo: process.cwd() })
}
+3 -3
View File
@@ -5,15 +5,15 @@ import commands from '../commands/index.js'
* Interaction creation listener for the client * Interaction creation listener for the client
* @param interaction the interaction received from the server * @param interaction the interaction received from the server
*/ */
export default event(Events.InteractionCreate, async ({ log, client }, interaction) => { export default event(Events.InteractionCreate, async ({ logger, client }, interaction) => {
if (!interaction.isCommand() || !interaction.isChatInputCommand()) return if (!interaction.isCommand() || !interaction.isChatInputCommand()) return
log(`Interaction called \'${interaction.commandName}\' from ${interaction.user.tag}.`) logger.info({ command: interaction.commandName, user: interaction.user.tag }, 'Interaction received')
// ensure command exists, otherwise kill event // ensure command exists, otherwise kill event
const command = commands.find(command => command.name === interaction.commandName) const command = commands.find(command => command.name === interaction.commandName)
if (!command) return if (!command) return
// the command exists, execute it // the command exists, execute it
command.run(client, interaction) command.run(client, interaction, logger)
}) })
+13 -14
View File
@@ -1,6 +1,6 @@
import { TextChannel } from 'discord.js' import { TextChannel } from 'discord.js'
import { event, Events, normalMessage, UserMessage, clean, addToChannelContext } from '../utils/index.js'
import { import {
event, Events, normalMessage, UserMessage, clean, addToChannelContext,
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo, getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
} from '../utils/index.js' } from '../utils/index.js'
@@ -11,10 +11,10 @@ import {
* *
* @param message the message received from the channel * @param message the message received from the channel
*/ */
export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory, ollama, client, defaultModel }, message) => { export default event(Events.MessageCreate, async ({ logger, msgHist, channelHistory, ollama, client, defaultModel }, message) => {
const clientId = client.user!!.id const clientId = client.user!!.id
let cleanedMessage = clean(message.content, clientId) let cleanedMessage = clean(message.content, clientId)
log(`Message \"${cleanedMessage}\" from ${message.author.tag} in channel/thread ${message.channelId}.`) logger.info({ author: message.author.tag, channelId: message.channelId, message: cleanedMessage }, 'Received message')
// 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
@@ -25,7 +25,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
if (channelInfo?.messages) if (channelInfo?.messages)
resolve(channelInfo.messages) resolve(channelInfo.messages)
else { else {
log(`Channel/Thread ${message.channel}-context does not exist. File will be created shortly...`) logger.info({ channelId: message.channelId }, 'Channel context does not exist; creating it')
resolve([]) resolve([])
} }
}) })
@@ -40,7 +40,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
if (channelInfo?.messages) if (channelInfo?.messages)
resolve(channelInfo.messages) resolve(channelInfo.messages)
else { else {
log(`Channel/Thread ${message.channel}-context does not exist. File will be created shortly...`) logger.info({ channelId: message.channelId }, 'Channel context does not exist; creating it')
} }
}) })
}) })
@@ -61,7 +61,6 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
while (channelHistory.size() >= channelHistory.capacity) channelHistory.dequeue() while (channelHistory.size() >= channelHistory.capacity) channelHistory.dequeue()
// push user response to channel history // push user response to channel history
console.log
channelHistory.enqueue({ channelHistory.enqueue({
role: 'user', role: 'user',
content: cleanedMessage, content: cleanedMessage,
@@ -109,7 +108,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
} catch (error) { } catch (error) {
++attempt ++attempt
if (attempt < maxRetries) { if (attempt < maxRetries) {
log(`Attempt ${attempt} failed for Server Preferences. Retrying in ${delay}ms...`) logger.warn({ attempt, delay }, 'Failed to retrieve server preferences; retrying')
await new Promise(ret => setTimeout(ret, delay)) await new Promise(ret => setTimeout(ret, delay))
} else } else
throw new Error(`Could not retrieve Server Preferences, please try chatting again...`) throw new Error(`Could not retrieve Server Preferences, please try chatting again...`)
@@ -133,11 +132,11 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
// check if there is a set capacity in config // check if there is a set capacity in config
else if (typeof config.options['modify-capacity'] !== 'number') else if (typeof config.options['modify-capacity'] !== 'number')
log(`Capacity is undefined, using default capacity of ${msgHist.capacity}.`) logger.info({ capacity: msgHist.capacity }, 'Capacity is undefined; using default')
else if (config.options['modify-capacity'] === msgHist.capacity) else if (config.options['modify-capacity'] === msgHist.capacity)
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`) logger.info({ capacity: msgHist.capacity }, 'Configured capacity matches current capacity')
else { else {
log(`New Capacity found. Setting Context Capacity to ${config.options['modify-capacity']}.`) logger.info({ capacity: config.options['modify-capacity'] }, 'Updating context capacity')
msgHist.capacity = config.options['modify-capacity'] msgHist.capacity = config.options['modify-capacity']
} }
@@ -154,7 +153,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
} catch (error) { } catch (error) {
++attempt ++attempt
if (attempt < maxRetries) { if (attempt < maxRetries) {
log(`Attempt ${attempt} failed for User Preferences. Retrying in ${delay}ms...`) logger.warn({ attempt, delay }, 'Failed to retrieve user preferences; retrying')
await new Promise(ret => setTimeout(ret, delay)) await new Promise(ret => setTimeout(ret, delay))
} else } else
throw new Error(`Could not retrieve User Preferences, please try chatting again...`) throw new Error(`Could not retrieve User Preferences, please try chatting again...`)
@@ -168,7 +167,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
if (channelInfo?.messages) if (channelInfo?.messages)
resolve(channelInfo.messages) resolve(channelInfo.messages)
else { else {
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`) logger.info({ channelId: message.channelId, username: message.author.username }, 'User channel history does not exist; creating it')
resolve([]) resolve([])
} }
}) })
@@ -184,7 +183,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
if (channelInfo?.messages) if (channelInfo?.messages)
resolve(channelInfo.messages) resolve(channelInfo.messages)
else { else {
log(`Channel/Thread ${message.channel}-${message.author.username} does not exist. File will be created shortly...`) logger.info({ channelId: message.channelId, username: message.author.username }, 'User channel history does not exist; creating it')
reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`)) reject(new Error(`Failed to find ${message.author.username}'s history. Try chatting again.`))
} }
}) })
@@ -210,7 +209,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
}) })
// response string for ollama to put its response // response string for ollama to put its response
const response: string = await normalMessage(message, ollama, model, msgHist, shouldStream) const response: string = await normalMessage(logger, 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 }
+2 -2
View File
@@ -3,7 +3,7 @@ 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
export default event(Events.ClientReady, ({ log }, client) => { export default event(Events.ClientReady, ({ logger }, client) => {
// Register the commands associated with the bot upon loggin in // Register the commands associated with the bot upon loggin in
registerCommands(client, commands) registerCommands(client, commands)
@@ -13,5 +13,5 @@ export default event(Events.ClientReady, ({ log }, client) => {
type: ActivityType.Custom type: ActivityType.Custom
}) })
log(`Logged in as ${client.user.username}.`) logger.info({ username: client.user.username }, 'Client ready')
}) })
+6 -6
View File
@@ -5,16 +5,16 @@ 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, async ({ log }, thread: ThreadChannel) => { export default event(Events.ThreadDelete, async ({ logger }, thread: ThreadChannel) => {
// iterate through every guild member in the thread and delete their history, except the bot // iterate through every guild member in the thread and delete their history, except the bot
try { try {
log(`Number of User Guild Members in Thread being deleted: ${thread.memberCount!! - 1}`) logger.info({ memberCount: thread.memberCount!! - 1 }, 'Deleting thread history')
const dirPath = 'data/' const dirPath = 'data/'
// read all files in data/ // read all files in data/
fs.readdir(dirPath, (error, files) => { fs.readdir(dirPath, (error, files) => {
if (error) { if (error) {
log(`Error reading directory ${dirPath}`, error) logger.error({ err: error, directory: dirPath }, 'Failed to read history directory')
return return
} }
@@ -28,13 +28,13 @@ export default event(Events.ThreadDelete, async ({ log }, thread: ThreadChannel)
const filePath = dirPath + file const filePath = dirPath + file
fs.unlink(filePath, error => { fs.unlink(filePath, error => {
if (error) if (error)
log(`Error deleting file ${filePath}`, error) logger.error({ err: error, file: filePath }, 'Failed to delete thread history file')
else else
log(`Successfully deleted ${filePath} thread information`) logger.info({ file: filePath }, 'Deleted thread history file')
}) })
}) })
}) })
} catch (error) { } catch (error) {
log(`Issue deleting user history files from ${thread.id}`) logger.error({ err: error, threadId: thread.id }, 'Failed to delete user history files')
} }
}) })
+2 -2
View File
@@ -2,8 +2,8 @@ import { getEnvVar } from './utils/index.js'
export const Keys = { export const Keys = {
clientToken: getEnvVar('CLIENT_TOKEN'), clientToken: getEnvVar('CLIENT_TOKEN'),
ipAddress: getEnvVar('OLLAMA_IP', '127.0.0.1'), // default ollama ip if none ipAddress: getEnvVar('LLM_ENDPOINT', process.env.OLLAMA_IP ?? '127.0.0.1'), // OLLAMA_IP is a deprecated alias
portAddress: getEnvVar('OLLAMA_PORT', '11434'), // default ollama port if none portAddress: getEnvVar('LLM_PORT', process.env.OLLAMA_PORT ?? '11434'), // OLLAMA_PORT is a deprecated alias
defaultModel: getEnvVar('MODEL', 'llama3.2') defaultModel: getEnvVar('MODEL', 'llama3.2')
} as const // readonly keys } as const // readonly keys
+20 -6
View File
@@ -1,4 +1,8 @@
import { ChatInputCommandInteraction, ChatInputApplicationCommandData, Client, ApplicationCommandOption } from 'discord.js' import { ChatInputCommandInteraction, ChatInputApplicationCommandData, Client } from 'discord.js'
import { createLogger } from '../components/index.js'
import type { Logger as PinoLogger } from 'pino'
const logger = createLogger({ component: 'commands' })
/** /**
* interface for how slash commands should be run * interface for how slash commands should be run
@@ -7,7 +11,7 @@ export interface SlashCommand extends ChatInputApplicationCommandData {
run: ( run: (
client: Client, client: Client,
interaction: ChatInputCommandInteraction, interaction: ChatInputCommandInteraction,
options?: ApplicationCommandOption[] logger: PinoLogger
) => void ) => void
} }
@@ -27,10 +31,15 @@ export function registerCommands(client: Client, commands: SlashCommand[]): void
client.application.commands.fetch().then((fetchedCommands) => { client.application.commands.fetch().then((fetchedCommands) => {
for (const command of fetchedCommands.values()) { for (const command of fetchedCommands.values()) {
if (!commandsToRegister.includes(command.name)) { if (!commandsToRegister.includes(command.name)) {
command.delete().catch(console.error) command.delete().then(() => {
console.log(`[Command: ${command.name}] Removed from Discord`) logger.info({ command: command.name }, 'Removed command from Discord')
}).catch(error => {
logger.error({ err: error, command: command.name }, 'Failed to remove command from Discord')
})
} }
} }
}).catch(error => {
logger.error({ err: error }, 'Failed to fetch registered commands')
}) })
// clear the cache of the commands // clear the cache of the commands
@@ -41,7 +50,12 @@ export function registerCommands(client: Client, commands: SlashCommand[]): void
client.application.commands client.application.commands
.create(command) .create(command)
.then((c) => { .then((c) => {
console.log(`[Command: ${c.name}] Registered on Discord`) logger.info({
c.options?.forEach((o) => console.log(` - ${o.name}`)) command: c.name,
options: c.options?.map(option => option.name) ?? []
}, 'Registered command on Discord')
})
.catch(error => {
logger.error({ err: error, command: command.name }, 'Failed to register command on Discord')
}) })
} }
+1 -1
View File
@@ -26,7 +26,7 @@ export function getEnvVar(name: string, fallback?: string): string {
This is probably an invalid token unless Discord updated their token policy. Please provide a valid token.`) This is probably an invalid token unless Discord updated their token policy. Please provide a valid token.`)
// validate IPv4 address found in environment variables // validate IPv4 address found in environment variables
if ((name.endsWith("_IP") || name.endsWith("_ADDRESS")) && !ipValidate.test(value)) if ((name.endsWith("_IP") || name.endsWith("_ADDRESS") || name.endsWith("_ENDPOINT")) && !ipValidate.test(value))
throw new Error(`Environment variable ${name} does not follow IPv4 formatting.`) throw new Error(`Environment variable ${name} does not follow IPv4 formatting.`)
// return env variable // return env variable
+8 -7
View File
@@ -1,11 +1,12 @@
import type { ClientEvents, Awaitable, Client } 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, createLogger } from '../components/index.js'
import type { Logger as PinoLogger } from 'pino'
// Export events through here to reduce amount of imports // Export events through here to reduce amount of imports
export { Events } from 'discord.js' export { Events } from 'discord.js'
export type LogMethod = (...args: unknown[]) => void export type LogMethod = PinoLogger
export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object
/** /**
@@ -35,7 +36,7 @@ export type UserMessage = {
// Event properties // Event properties
export interface EventProps { export interface EventProps {
client: Client, client: Client,
log: LogMethod, logger: LogMethod,
msgHist: Queue<UserMessage>, msgHist: Queue<UserMessage>,
channelHistory: Queue<UserMessage>, channelHistory: Queue<UserMessage>,
ollama: Ollama, ollama: Ollama,
@@ -85,14 +86,14 @@ export function registerEvents(
): void { ): void {
for (const { key, callback } of events) { for (const { key, callback } of events) {
client.on(key, (...args) => { client.on(key, (...args) => {
// Create a new log method for this event const logger = createLogger({ component: 'event', event: key })
const log = console.log.bind(console, `[Event: ${key}]`)
// Handle Errors, call callback, log errors as needed // Handle Errors, call callback, log errors as needed
try { try {
callback({ client, log, msgHist, channelHistory, ollama, defaultModel }, ...args) void Promise.resolve(callback({ client, logger, msgHist, channelHistory, ollama, defaultModel }, ...args))
.catch(error => logger.error({ err: error }, 'Uncaught event error'))
} catch (error) { } catch (error) {
log('[Uncaught Error]', error) logger.error({ err: error }, 'Uncaught event error')
} }
}) })
} }
+9 -6
View File
@@ -2,6 +2,9 @@ import { TextChannel, ThreadChannel } from 'discord.js'
import { Configuration, 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'
import { createLogger } from '../../components/index.js'
const logger = createLogger({ component: 'chat-history' })
/** /**
* Method to check if a thread history file exists * Method to check if a thread history file exists
@@ -40,7 +43,7 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
const cleanedHistory: boolean = await new Promise((resolve) => { const cleanedHistory: boolean = await new Promise((resolve) => {
fs.readFile(fullFileName, 'utf8', (error, data) => { fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error) if (error)
console.log(`[Error: openChannelInfo] Incorrect file format`) logger.error({ err: error, file: fullFileName }, 'Failed to read channel history')
else { else {
const object = JSON.parse(data) const object = JSON.parse(data)
if (object['messages'].length === 0) // already empty, let user know if (object['messages'].length === 0) // already empty, let user know
@@ -56,12 +59,12 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
return cleanedHistory return cleanedHistory
} }
export async function addToChannelContext(filename: string, channel : TextChannel | ThreadChannel, messages: UserMessage[] = []): Promise<void> { export async function addToChannelContext(filename: string, channel: TextChannel | ThreadChannel, messages: UserMessage[] = []): Promise<void> {
const fullFileName = `data/${filename}-context.json` const fullFileName = `data/${filename}-context.json`
if (fs.existsSync(fullFileName)) { if (fs.existsSync(fullFileName)) {
fs.readFile(fullFileName, 'utf8', (error, data) => { fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error) if (error)
console.log(`[Error: addToChannelContext] Incorrect file format`) logger.error({ err: error, file: fullFileName }, 'Failed to read channel context')
else { else {
const object = JSON.parse(data) const object = JSON.parse(data)
if (object['messages'].length === 0) if (object['messages'].length === 0)
@@ -85,7 +88,7 @@ export async function addToChannelContext(filename: string, channel : TextChanne
fs.mkdirSync(directory, { recursive: true }) fs.mkdirSync(directory, { recursive: true })
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2)) fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
console.log(`[Util: addToChannelContext] Created '${fullFileName}' in working directory`) logger.info({ file: fullFileName }, 'Created channel context')
} }
} }
@@ -117,7 +120,7 @@ export async function openChannelInfo(filename: string, channel: TextChannel | T
if (fs.existsSync(fullFileName)) { if (fs.existsSync(fullFileName)) {
fs.readFile(fullFileName, 'utf8', (error, data) => { fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error) if (error)
console.log(`[Error: openChannelInfo] Incorrect file format`) logger.error({ err: error, file: fullFileName }, 'Failed to read channel history')
else { else {
const object = JSON.parse(data) const object = JSON.parse(data)
if (object['messages'].length === 0) if (object['messages'].length === 0)
@@ -143,7 +146,7 @@ export async function openChannelInfo(filename: string, channel: TextChannel | T
// only creating it, no need to add anything // only creating it, no need to add anything
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2)) fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
console.log(`[Util: openChannelInfo] Created '${fullFileName}' in working directory`) logger.info({ file: fullFileName }, 'Created channel history')
} }
} }
+5 -2
View File
@@ -1,6 +1,9 @@
import { Configuration, ServerConfig, UserConfig, isServerConfigurationKey } from '../index.js' import { Configuration, ServerConfig, UserConfig, isServerConfigurationKey } from '../index.js'
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { createLogger } from '../../components/index.js'
const logger = createLogger({ component: 'config' })
/** /**
* Method to open a file in the working directory and modify/create it * Method to open a file in the working directory and modify/create it
@@ -17,7 +20,7 @@ export function openConfig(filename: string, key: string, value: any) {
if (fs.existsSync(fullFileName)) { if (fs.existsSync(fullFileName)) {
fs.readFile(fullFileName, 'utf8', (error, data) => { fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error) if (error)
console.log(`[Error: openConfig] Incorrect file format`) logger.error({ err: error, file: fullFileName }, 'Failed to read configuration file')
else { else {
const object = JSON.parse(data) const object = JSON.parse(data)
object['options'][key] = value object['options'][key] = value
@@ -41,7 +44,7 @@ export function openConfig(filename: string, key: string, value: any) {
fs.mkdirSync(directory, { recursive: true }) fs.mkdirSync(directory, { recursive: true })
fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2)) fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2))
console.log(`[Util: openConfig] Created '${filename}' in working directory`) logger.info({ file: fullFileName }, 'Created configuration file')
} }
} }
+1 -2
View File
@@ -1,6 +1,5 @@
import { ChatResponse } from "ollama" import { ChatResponse, AbortableAsyncIterator } from "ollama"
import { ChatParams } from "../index.js" import { ChatParams } from "../index.js"
import { AbortableAsyncIterator } from "ollama/src/utils.js"
/** /**
* Method to query the Ollama client for async generation * Method to query the Ollama client for async generation
+11 -10
View File
@@ -1,8 +1,8 @@
import { Message, SendableChannels } from 'discord.js' import { Message, SendableChannels } from 'discord.js'
import { ChatResponse, Ollama } from 'ollama' import { ChatResponse, Ollama, AbortableAsyncIterator } from 'ollama'
import type { Logger as PinoLogger } from 'pino'
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 '../components/index.js'
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
@@ -11,6 +11,7 @@ import { AbortableAsyncIterator } from 'ollama/src/utils.js'
* @param msgHist message history between user and model * @param msgHist message history between user and model
*/ */
export async function normalMessage( export async function normalMessage(
logger: PinoLogger,
message: Message, message: Message,
ollama: Ollama, ollama: Ollama,
model: string, model: string,
@@ -49,7 +50,7 @@ export async function normalMessage(
if (result.length > 5) if (result.length > 5)
messageBlock.edit(result) messageBlock.edit(result)
} }
console.log(result) logger.debug({ response: result }, 'Received response chunk')
} }
} }
else { else {
@@ -77,11 +78,10 @@ export async function normalMessage(
sentMessage.edit(result) sentMessage.edit(result)
} }
} catch (error: any) { } catch (error: any) {
console.log(`[Util: messageNormal] Error creating message: ${error.message}`) logger.error({ err: error, model }, 'Failed to create message response')
if (error.message.includes('fetch failed')) if (error.message.includes('try pulling it first'))
error.message = 'Missing ollama service on machine' sentMessage.edit(`**Response generation failed.**\n\nReason: You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`)
else if (error.message.includes('try pulling it first')) else
error.message = `You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`
sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`) sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`)
} }
}) })
@@ -91,6 +91,7 @@ export async function normalMessage(
} }
export async function summarizeContextHistory( export async function summarizeContextHistory(
logger: PinoLogger,
ollama: Ollama, ollama: Ollama,
model: string, model: string,
msgHist: UserMessage[], msgHist: UserMessage[],
@@ -110,7 +111,7 @@ export async function summarizeContextHistory(
return result return result
} catch (error: any) { } catch (error: any) {
console.log(`[Util: messageNormal] Error creating message: ${error.message}`) logger.error({ err: error, model }, 'Failed to summarize channel history')
if (error.message.includes('fetch failed')) if (error.message.includes('fetch failed'))
error.message = 'Missing ollama service on machine' error.message = 'Missing ollama service on machine'
else if (error.message.includes('try pulling it first')) else if (error.message.includes('try pulling it first'))
+1 -1
View File
@@ -26,6 +26,6 @@ describe('Events Existence', () => {
// test specific events in the object // test specific events in the object
it('references specific events', () => { it('references specific events', () => {
const eventsString = events.map(e => e.key.toString()).join(', ') const eventsString = events.map(e => e.key.toString()).join(', ')
expect(eventsString).toBe('ready, messageCreate, interactionCreate, threadDelete') expect(eventsString).toBe('clientReady, messageCreate, interactionCreate, threadDelete')
}) })
}) })
+24
View File
@@ -47,4 +47,28 @@ describe('Environment Setup', () => {
it('throws an error if key is not found', () => { it('throws an error if key is not found', () => {
expect(() => getEnvVar('NON_EXISTENT_KEY')).toThrowError() expect(() => getEnvVar('NON_EXISTENT_KEY')).toThrowError()
}) })
// test that *_ENDPOINT keys are validated as IPv4 addresses like *_IP keys
it('validates *_ENDPOINT keys as IPv4', () => {
expect(getEnvVar('UNSET_TEST_ENDPOINT', '127.0.0.1')).toBe('127.0.0.1')
expect(() => getEnvVar('UNSET_TEST_ENDPOINT', 'not-an-ip')).toThrowError()
})
// test the deprecated alias lookup used in src/keys.ts
it('falls back to the deprecated OLLAMA_IP / OLLAMA_PORT aliases', () => {
const saved = { LLM_ENDPOINT: process.env.LLM_ENDPOINT, LLM_PORT: process.env.LLM_PORT }
delete process.env.LLM_ENDPOINT
delete process.env.LLM_PORT
process.env.OLLAMA_IP = '10.0.0.5'
process.env.OLLAMA_PORT = '17434'
try {
expect(getEnvVar('LLM_ENDPOINT', process.env.OLLAMA_IP ?? '127.0.0.1')).toBe('10.0.0.5')
expect(getEnvVar('LLM_PORT', process.env.OLLAMA_PORT ?? '11434')).toBe('17434')
} finally {
delete process.env.OLLAMA_IP
delete process.env.OLLAMA_PORT
if (saved.LLM_ENDPOINT !== undefined) process.env.LLM_ENDPOINT = saved.LLM_ENDPOINT
if (saved.LLM_PORT !== undefined) process.env.LLM_PORT = saved.LLM_PORT
}
})
}) })
+45
View File
@@ -0,0 +1,45 @@
import { Writable } from 'node:stream'
import pino from 'pino'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createLogger } from '../src/components/logger.js'
describe('Pino logger', () => {
let output = ''
const destination = new Writable({
write(chunk, _encoding, callback) {
output += chunk.toString()
callback()
}
})
const parentLogger = pino({ level: 'trace' }, destination)
beforeEach(() => {
vi.stubEnv('NODE_ENV', 'test')
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('creates a child logger with component and caller metadata', () => {
output = ''
const logger = createLogger({ component: 'test' }, parentLogger)
logger.info('bound message')
const entry = JSON.parse(output)
expect(entry.msg).toBe('bound message')
expect(entry.component).toBe('test')
expect(entry.caller).toMatch(/logger\.test\.ts:\d+:\d+/)
})
it('supports native Pino levels and structured log fields', () => {
output = ''
const logger = createLogger({ component: 'test' }, parentLogger)
logger.warn({ answer: 42 }, 'warning')
const entry = JSON.parse(output)
expect(entry.level).toBe(40)
expect(entry.answer).toBe(42)
expect(entry.msg).toBe('warning')
})
})
+1 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { Queue } from '../src/queues/queue.js' import { Queue } from '../src/components/index.js'
/** /**
* Queue test suite, tests the Queue class * Queue test suite, tests the Queue class
+1 -2
View File
@@ -18,9 +18,8 @@
// Decompile .ts to .js into a folder named build // Decompile .ts to .js into a folder named build
"outDir": "build", "outDir": "build",
"rootDir": "src", "rootDir": "src",
"baseUrl": ".",
"paths": { "paths": {
"*": ["node_modules/"] "*": ["./node_modules/"]
} }
}, },
// environment for env vars // environment for env vars
+2 -6
View File
@@ -1,13 +1,9 @@
import { defineConfig, configDefaults } from 'vitest/config' import { defineConfig } from 'vitest/config'
// config for vitest // config for vitest
export default defineConfig({ export default defineConfig({
test: { test: {
globals: true, // <-- reduces test file imports globals: true, // <-- reduces test file imports
reporters: ['verbose'], // <-- verbose output reporters: ['verbose'] // <-- verbose output
coverage: {
exclude: [...configDefaults.exclude, 'build/*', 'tests/*'], // <-- exclude JS build
reporter: ['text-summary'] // <-- report in text-summary
}
} }
}) })