From e07e8fbf89379b2d2fcceb1445a60cb79d264c1a Mon Sep 17 00:00:00 2001 From: Kevin Dang <77701718+kevinthedang@users.noreply.github.com> Date: Wed, 18 Jun 2025 07:54:53 -0700 Subject: [PATCH] Fix Missing Redis Connections and Error Messages (#182) * Update: simplify npm command to run tests * Fix: redis workaround for local non docker * Update: error message and config creation * Fix: Better Messages for Ollama service being offline * Update: version increment * Fix: verion typo * Update: Use built-in catch method for logging * Update: same catch method for redis --- .github/workflows/coverage.yml | 2 +- .github/workflows/test.yml | 2 +- docker-compose.yml | 2 +- package-lock.json | 4 ++-- package.json | 6 +++--- src/client.ts | 12 ++++++++---- src/commands/deleteModel.ts | 14 +++++++++++++- src/commands/pullModel.ts | 16 ++++++++++++++-- src/commands/switchModel.ts | 10 ++++++++-- src/events/messageCreate.ts | 3 +-- src/utils/messageNormal.ts | 9 +++++---- 11 files changed, 57 insertions(+), 23 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index a1b74f9..d67e161 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -36,7 +36,7 @@ jobs: - name: Collect Code Coverage run: | - LINE_PCT=$(npm run test:coverage | tail -2 | head -1 | awk '{print $3}') + LINE_PCT=$(npm run coverage | tail -2 | head -1 | awk '{print $3}') echo "COVERAGE=$LINE_PCT" >> $GITHUB_ENV - name: Upload Code Coverage diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6be8b0e..cb063bb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -47,4 +47,4 @@ jobs: - name: Test Application run: | - npm run test:run + npm run tests diff --git a/docker-compose.yml b/docker-compose.yml index 442fc5a..c05070d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,7 @@ services: build: ./ # find docker file in designated path container_name: discord restart: always # rebuild container always - image: kevinthedang/discord-ollama:0.8.4 + image: kevinthedang/discord-ollama:0.8.5 environment: CLIENT_TOKEN: ${CLIENT_TOKEN} OLLAMA_IP: ${OLLAMA_IP} diff --git a/package-lock.json b/package-lock.json index a6378d8..0135ccb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "discord-ollama", - "version": "0.8.4", + "version": "0.8.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "discord-ollama", - "version": "0.8.4", + "version": "0.8.5", "license": "ISC", "dependencies": { "discord.js": "^14.18.0", diff --git a/package.json b/package.json index a1f1e42..bad0836 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "discord-ollama", - "version": "0.8.4", + "version": "0.8.5", "description": "Ollama Integration into discord", "main": "build/index.js", "exports": "./build/index.js", "scripts": { - "test:run": "vitest run", - "test:coverage": "vitest run --coverage", + "tests": "vitest run", + "coverage": "vitest run --coverage", "watch": "tsx watch src", "build": "tsc", "prod": "node .", diff --git a/src/client.ts b/src/client.ts index ba905c6..7fa4034 100644 --- a/src/client.ts +++ b/src/client.ts @@ -34,10 +34,14 @@ registerEvents(client, Events, messageHistory, ollama, Keys.defaultModel) // Try to connect to redis await redis.connect() - .then(() => console.log('[Redis] Connected')) - .catch((error) => { - console.error('[Redis] Connection Error', error) - process.exit(1) + .then(response => { + console.log('[Redis] Successfully Connected') + }) + .catch(error => { + console.error('[Redis] Connection Error. See error below:\n', error) + console.warn('[Redis] Failed to connect to Redis Database, using local system') + // TODO: create boolean flag that will probably be used in messageCreate.ts if redis database is down + // When implementing this boolean flag, move connection to database BEFORE the registerEvents method }) // Try to log in the client diff --git a/src/commands/deleteModel.ts b/src/commands/deleteModel.ts index e07193e..d1517db 100644 --- a/src/commands/deleteModel.ts +++ b/src/commands/deleteModel.ts @@ -37,9 +37,21 @@ export const DeleteModel: SlashCommand = { } // check if model exists - const modelExists: boolean = await ollama.list() + const modelExists = await ollama.list() .then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput))) + .catch(error => { + console.error(`[Command: delete-model] Failed to connect with Ollama service. Error: ${error.message}`) + }) + // Validate for any issue or if service is running + if (!modelExists) { + interaction.editReply({ + content: `The Ollama service is not running. Please turn on/download the [service](https://ollama.com/).` + }) + return + } + + try { // call ollama to delete model if (modelExists) { diff --git a/src/commands/pullModel.ts b/src/commands/pullModel.ts index 7fba93d..2582d3f 100644 --- a/src/commands/pullModel.ts +++ b/src/commands/pullModel.ts @@ -36,9 +36,21 @@ export const PullModel: SlashCommand = { return } - // check if model was already pulled - const modelExists: boolean = await ollama.list() + // check if model was already pulled, if the ollama service isn't running throw error + const modelExists = await ollama.list() .then(response => response.models.some((model: ModelResponse) => model.name.startsWith(modelInput))) + .catch(error => { + console.error(`[Command: pull-model] Failed to connect with Ollama service. Error: ${error.message}`) + }) + + // Validate for any issue or if service is running + if (!modelExists) { + interaction.editReply({ + content: `The Ollama service is not running. Please turn on/download the [service](https://ollama.com/).` + }) + return + } + try { // call ollama to pull desired model diff --git a/src/commands/switchModel.ts b/src/commands/switchModel.ts index c81c1e9..ef74b85 100644 --- a/src/commands/switchModel.ts +++ b/src/commands/switchModel.ts @@ -45,6 +45,9 @@ export const SwitchModel: SlashCommand = { } } }) + .catch(error => { + console.error(`[Command: switch-model] Failed to connect with Ollama service. Error: ${error.message}`) + }) // todo: problem can be here if async messes up if (switchSuccess) { // set model now that it exists @@ -56,10 +59,13 @@ export const SwitchModel: SlashCommand = { interaction.editReply({ content: `Could not find **${modelInput}** in local model library.\n\nPlease contact an server admin for access to this model.` }) - } catch (error) { + } catch (error: any) { // could not resolve user model switch + if (error.message.includes("fetch failed") as string) + error.message = "The Ollama service is not running. Please turn on/download the [service](https://ollama.com/)." + interaction.editReply({ - content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error}\n\nPossible solution is to request an server admin run \`/pull-model ${modelInput}\` and try again.` + content: `Unable to switch user preferred model to **${modelInput}**.\n\n${error.message}` }) return } diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 5ec6fa0..587dc48 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -71,9 +71,8 @@ export default event(Events.MessageCreate, async ({ log, msgHist, ollama, client userConfig = await new Promise((resolve, reject) => { getUserConfig(`${message.author.username}-config.json`, (config) => { if (config === undefined) { - openConfig(`${message.author.username}-config.json`, 'message-style', false) openConfig(`${message.author.username}-config.json`, 'switch-model', defaultModel) - reject(new Error('No User Preferences is set up.\n\nCreating preferences file with \`message-style\` set as \`false\` for regular message style.\nPlease try chatting again.')) + reject(new Error(`No User Preferences is set up.\n\nCreating new preferences file for ${message.author.username}\nPlease try chatting again.`)) return } diff --git a/src/utils/messageNormal.ts b/src/utils/messageNormal.ts index a438e41..969f388 100644 --- a/src/utils/messageNormal.ts +++ b/src/utils/messageNormal.ts @@ -74,10 +74,11 @@ export async function normalMessage( } } catch (error: any) { console.log(`[Util: messageNormal] Error creating message: ${error.message}`) - if (error.message.includes('try pulling it first')) - sentMessage.edit(`**Response generation failed.**\n\nReason: You do not have the ${model} downloaded. Ask an admin to pull it using the \`pull-model\` command.`) - else - sentMessage.edit(`**Response generation failed.**\n\nReason: ${error.message}`) + if (error.message.includes('fetch failed')) + error.message = 'Missing ollama service on machine' + else if (error.message.includes('try pulling it first')) + 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}`) } })