Compare commits

...
5 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
24 changed files with 1416 additions and 1626 deletions
+18
View File
@@ -23,6 +23,24 @@ These are guides to the features and capabilities of this app.
* [User Slash Commands](./docs/commands-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
* 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.
+1226 -1547
View File
File diff suppressed because it is too large Load Diff
+7 -2
View File
@@ -27,18 +27,23 @@
"dependencies": {
"discord.js": "^14.20.0",
"dotenv": "^16.5.0",
"ollama": "^0.5.15"
"ollama": "^0.5.15",
"pino": "^10.4.0",
"pino-caller": "^4.0.0"
},
"devDependencies": {
"@types/node": "^22.13.14",
"ts-node": "^10.9.2",
"tsx": "^4.19.3",
"typescript": "^5.8.2",
"vitest": "^3.0.4"
"vitest": "^5.0.1"
},
"type": "module",
"engines": {
"npm": ">=10.9.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 { Ollama } from 'ollama'
import { Queue } from './queues/queue.js'
import { Queue, createLogger } from './components/index.js'
import { UserMessage, registerEvents } from './utils/index.js'
import Events from './events/index.js'
import Keys from './keys.js'
const logger = createLogger({ component: 'client' })
// initialize the client with the following permissions when logging in
const client = new Client({
intents: [
@@ -32,7 +34,7 @@ registerEvents(client, Events, messageHistory, channelMessageHistory, ollama, Ke
// Try to log in the client
await client.login(Keys.clientToken)
.catch((error) => {
console.error('[Login Error]', error)
logger.fatal({ err: error }, 'Client login failed')
process.exit(1)
})
+3 -3
View File
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, ChatInputCommandInteraction, Client, Comm
import { UserCommand, SlashCommand } from '../utils/index.js'
import { ollama } from '../client.js'
import { ModelResponse } from 'ollama'
import type { Logger as PinoLogger } from 'pino'
export const DeleteModel: SlashCommand = {
name: 'delete-model',
@@ -18,7 +19,7 @@ export const DeleteModel: SlashCommand = {
],
// Delete Model locally stored
run: async (client: Client, interaction: ChatInputCommandInteraction) => {
run: async (client: Client, interaction: ChatInputCommandInteraction, logger: PinoLogger) => {
// defer reply to avoid timeout
await interaction.deferReply()
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)))
.catch(error => {
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
@@ -53,7 +54,6 @@ export const DeleteModel: SlashCommand = {
return
}
try {
// call ollama to delete model
if (modelExists) {
+3 -2
View File
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction, Mess
import { ollama } from "../client.js"
import { ModelResponse } from "ollama"
import { UserCommand, SlashCommand } from "../utils/index.js"
import type { Logger as PinoLogger } from 'pino'
export const PullModel: SlashCommand = {
name: 'pull-model',
@@ -18,7 +19,7 @@ export const PullModel: SlashCommand = {
],
// 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
await interaction.deferReply()
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)))
.catch(error => {
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
+4 -3
View File
@@ -1,18 +1,19 @@
import { Client, CommandInteraction, MessageFlags } from 'discord.js'
import { AdminCommand, SlashCommand } from '../utils/index.js'
import type { Logger as PinoLogger } from 'pino'
export const Shutoff: SlashCommand = {
name: 'shutoff',
description: 'shutdown the bot. You will need to manually bring it online again. Administrator Only.',
// 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
const channel = await client.channels.fetch(interaction.channelId)
if (!channel || !AdminCommand.includes(channel.type)) return
// 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
if (!interaction.memberPermissions?.has('Administrator')) {
@@ -29,7 +30,7 @@ export const Shutoff: SlashCommand = {
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
client.destroy()
+6 -5
View File
@@ -1,5 +1,6 @@
import { Client, CommandInteraction, Message, MessageFlags, SendableChannels } from "discord.js";
import { accessChannelContext, normalMessage, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js";
import { Client, CommandInteraction, MessageFlags } from "discord.js";
import { accessChannelContext, SlashCommand, summarizeContextHistory, UserCommand, UserMessage } from "../utils/index.js";
import type { Logger as PinoLogger } from 'pino'
import { ollama } from "../client.js"
export const Summary: SlashCommand = {
@@ -7,7 +8,7 @@ export const Summary: SlashCommand = {
description: 'provides a summary of the chat history.',
// Generate Summary from additional context
run: async (client: Client, interaction: CommandInteraction) => {
run: async (client: Client, interaction: CommandInteraction, logger: PinoLogger) => {
// fetch channel
const channel = await client.channels.fetch(interaction.channelId)
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.
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 })
}
+3 -2
View File
@@ -2,6 +2,7 @@ import { ApplicationCommandOptionType, Client, ChatInputCommandInteraction } fro
import { ollama } from "../client.js"
import { ModelResponse } from "ollama"
import { openConfig, UserCommand, SlashCommand } from "../utils/index.js"
import type { Logger as PinoLogger } from 'pino'
export const SwitchModel: SlashCommand = {
name: 'switch-model',
@@ -18,7 +19,7 @@ export const SwitchModel: SlashCommand = {
],
// 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()
const modelInput: string = interaction.options.getString('model-to-use') as string
@@ -46,7 +47,7 @@ export const SwitchModel: SlashCommand = {
}
})
.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
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
* @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
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
const command = commands.find(command => command.name === interaction.commandName)
if (!command) return
// 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 { event, Events, normalMessage, UserMessage, clean, addToChannelContext } from '../utils/index.js'
import {
event, Events, normalMessage, UserMessage, clean, addToChannelContext,
getChannelInfo, getServerConfig, getUserConfig, openChannelInfo,
openConfig, UserConfig, getAttachmentData, getTextFileAttachmentData
} from '../utils/index.js'
@@ -11,10 +11,10 @@ import {
*
* @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
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
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)
resolve(channelInfo.messages)
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([])
}
})
@@ -40,7 +40,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
if (channelInfo?.messages)
resolve(channelInfo.messages)
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()
// push user response to channel history
console.log
channelHistory.enqueue({
role: 'user',
content: cleanedMessage,
@@ -109,7 +108,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
} catch (error) {
++attempt
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))
} else
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
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)
log(`Capacity matches config as ${msgHist.capacity}, no changes made.`)
logger.info({ capacity: msgHist.capacity }, 'Configured capacity matches current capacity')
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']
}
@@ -154,7 +153,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
} catch (error) {
++attempt
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))
} else
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)
resolve(channelInfo.messages)
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([])
}
})
@@ -184,7 +183,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
if (channelInfo?.messages)
resolve(channelInfo.messages)
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.`))
}
})
@@ -210,7 +209,7 @@ export default event(Events.MessageCreate, async ({ log, msgHist, channelHistory
})
// 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 (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'
// 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
registerCommands(client, commands)
@@ -13,5 +13,5 @@ export default event(Events.ClientReady, ({ log }, client) => {
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
*/
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
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/'
// read all files in data/
fs.readdir(dirPath, (error, files) => {
if (error) {
log(`Error reading directory ${dirPath}`, error)
logger.error({ err: error, directory: dirPath }, 'Failed to read history directory')
return
}
@@ -28,13 +28,13 @@ export default event(Events.ThreadDelete, async ({ log }, thread: ThreadChannel)
const filePath = dirPath + file
fs.unlink(filePath, error => {
if (error)
log(`Error deleting file ${filePath}`, error)
logger.error({ err: error, file: filePath }, 'Failed to delete thread history file')
else
log(`Successfully deleted ${filePath} thread information`)
logger.info({ file: filePath }, 'Deleted thread history file')
})
})
})
} catch (error) {
log(`Issue deleting user history files from ${thread.id}`)
logger.error({ err: error, threadId: thread.id }, 'Failed to delete user history files')
}
})
+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
@@ -7,7 +11,7 @@ export interface SlashCommand extends ChatInputApplicationCommandData {
run: (
client: Client,
interaction: ChatInputCommandInteraction,
options?: ApplicationCommandOption[]
logger: PinoLogger
) => void
}
@@ -27,10 +31,15 @@ export function registerCommands(client: Client, commands: SlashCommand[]): void
client.application.commands.fetch().then((fetchedCommands) => {
for (const command of fetchedCommands.values()) {
if (!commandsToRegister.includes(command.name)) {
command.delete().catch(console.error)
console.log(`[Command: ${command.name}] Removed from Discord`)
command.delete().then(() => {
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
@@ -41,7 +50,12 @@ export function registerCommands(client: Client, commands: SlashCommand[]): void
client.application.commands
.create(command)
.then((c) => {
console.log(`[Command: ${c.name}] Registered on Discord`)
c.options?.forEach((o) => console.log(` - ${o.name}`))
logger.info({
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')
})
}
+8 -7
View File
@@ -1,11 +1,12 @@
import type { ClientEvents, Awaitable, Client } from 'discord.js'
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 } from 'discord.js'
export type LogMethod = (...args: unknown[]) => void
export type LogMethod = PinoLogger
export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents object
/**
@@ -35,7 +36,7 @@ export type UserMessage = {
// Event properties
export interface EventProps {
client: Client,
log: LogMethod,
logger: LogMethod,
msgHist: Queue<UserMessage>,
channelHistory: Queue<UserMessage>,
ollama: Ollama,
@@ -85,14 +86,14 @@ export function registerEvents(
): void {
for (const { key, callback } of events) {
client.on(key, (...args) => {
// Create a new log method for this event
const log = console.log.bind(console, `[Event: ${key}]`)
const logger = createLogger({ component: 'event', event: key })
// Handle Errors, call callback, log errors as needed
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) {
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 fs from 'fs'
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
@@ -40,7 +43,7 @@ export async function clearChannelInfo(filename: string, channel: TextChannel, u
const cleanedHistory: boolean = await new Promise((resolve) => {
fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error)
console.log(`[Error: openChannelInfo] Incorrect file format`)
logger.error({ err: error, file: fullFileName }, 'Failed to read channel history')
else {
const object = JSON.parse(data)
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
}
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`
if (fs.existsSync(fullFileName)) {
fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error)
console.log(`[Error: addToChannelContext] Incorrect file format`)
logger.error({ err: error, file: fullFileName }, 'Failed to read channel context')
else {
const object = JSON.parse(data)
if (object['messages'].length === 0)
@@ -85,7 +88,7 @@ export async function addToChannelContext(filename: string, channel : TextChanne
fs.mkdirSync(directory, { recursive: true })
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)) {
fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error)
console.log(`[Error: openChannelInfo] Incorrect file format`)
logger.error({ err: error, file: fullFileName }, 'Failed to read channel history')
else {
const object = JSON.parse(data)
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
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 fs from 'fs'
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
@@ -17,7 +20,7 @@ export function openConfig(filename: string, key: string, value: any) {
if (fs.existsSync(fullFileName)) {
fs.readFile(fullFileName, 'utf8', (error, data) => {
if (error)
console.log(`[Error: openConfig] Incorrect file format`)
logger.error({ err: error, file: fullFileName }, 'Failed to read configuration file')
else {
const object = JSON.parse(data)
object['options'][key] = value
@@ -41,7 +44,7 @@ export function openConfig(filename: string, key: string, value: any) {
fs.mkdirSync(directory, { recursive: true })
fs.writeFileSync(`data/${filename}`, JSON.stringify(object, null, 2))
console.log(`[Util: openConfig] Created '${filename}' in working directory`)
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 { AbortableAsyncIterator } from "ollama/src/utils.js"
/**
* Method to query the Ollama client for async generation
+12 -11
View File
@@ -1,8 +1,8 @@
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 { Queue } from '../queues/queue.js'
import { AbortableAsyncIterator } from 'ollama/src/utils.js'
import { Queue } from '../components/index.js'
/**
* Method to send replies as normal text on discord like any other user
@@ -11,6 +11,7 @@ import { AbortableAsyncIterator } from 'ollama/src/utils.js'
* @param msgHist message history between user and model
*/
export async function normalMessage(
logger: PinoLogger,
message: Message,
ollama: Ollama,
model: string,
@@ -49,7 +50,7 @@ export async function normalMessage(
if (result.length > 5)
messageBlock.edit(result)
}
console.log(result)
logger.debug({ response: result }, 'Received response chunk')
}
}
else {
@@ -77,12 +78,11 @@ export async function normalMessage(
sentMessage.edit(result)
}
} catch (error: any) {
console.log(`[Util: messageNormal] Error creating message: ${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}`)
logger.error({ err: error, model }, 'Failed to create message response')
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}`)
}
})
@@ -91,6 +91,7 @@ export async function normalMessage(
}
export async function summarizeContextHistory(
logger: PinoLogger,
ollama: Ollama,
model: string,
msgHist: UserMessage[],
@@ -110,7 +111,7 @@ export async function summarizeContextHistory(
return result
} 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'))
error.message = 'Missing ollama service on machine'
else if (error.message.includes('try pulling it first'))
+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 { Queue } from '../src/queues/queue.js'
import { Queue } from '../src/components/index.js'
/**
* Queue test suite, tests the Queue class