mirror of
https://github.com/kevinthedang/discord-ollama.git
synced 2025-12-12 11:56:06 -05:00
* add: validate thread creation in ollama channel * rm: channel_id variable * add: short notes for threads * update: openFile to openConfig for clarity * update: test ci runs on master * add: notes for work * add: basic chat storing via json * update: stores entire msgHist according to capacity * add: removes json file if thread is deleted * add: chats with independent histories * add: private vs public threads * update: validate threads made by ollama for chats * update: cleanup and version increment
128 lines
4.4 KiB
TypeScript
128 lines
4.4 KiB
TypeScript
import { ThreadChannel } from 'discord.js'
|
|
import { UserMessage } from './events.js'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
export interface Configuration {
|
|
readonly name: string
|
|
options: {
|
|
'message-stream'?: boolean,
|
|
'message-style'?: boolean,
|
|
'toggle-chat'?: boolean,
|
|
'modify-capacity'?: number
|
|
}
|
|
}
|
|
|
|
export interface Thread {
|
|
readonly id: string
|
|
readonly name: string
|
|
messages: UserMessage[]
|
|
}
|
|
|
|
/**
|
|
* Method to open a file in the working directory and modify/create it
|
|
*
|
|
* @param filename name of the file
|
|
* @param key key value to access
|
|
* @param value new value to assign
|
|
*/
|
|
export function openConfig(filename: string, key: string, value: any) {
|
|
// check if the file exists, if not then make the config file
|
|
if (fs.existsSync(filename)) {
|
|
fs.readFile(filename, 'utf8', (error, data) => {
|
|
if (error)
|
|
console.log(`[Error: openConfig] Incorrect file format`)
|
|
else {
|
|
const object = JSON.parse(data)
|
|
object['options'][key] = value
|
|
fs.writeFileSync(filename, JSON.stringify(object, null, 2))
|
|
}
|
|
})
|
|
} else { // work on dynamic file creation
|
|
const object: Configuration = JSON.parse('{ \"name\": \"Discord Ollama Confirgurations\" }')
|
|
|
|
// set standard information for config file and options
|
|
object['options'] = {
|
|
[key]: value
|
|
}
|
|
|
|
fs.writeFileSync(filename, JSON.stringify(object, null, 2))
|
|
console.log(`[Util: openConfig] Created '${filename}' in working directory`)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method to obtain the configurations of the message chat/thread
|
|
*
|
|
* @param filename name of the configuration file to get
|
|
* @param callback function to allow a promise from getting the config
|
|
*/
|
|
export async function getConfig(filename: string, callback: (config: Configuration | undefined) => void): Promise<void> {
|
|
// attempt to read the file and get the configuration
|
|
if (fs.existsSync(filename)) {
|
|
fs.readFile(filename, 'utf8', (error, data) => {
|
|
if (error) {
|
|
callback(undefined)
|
|
return // something went wrong... stop
|
|
}
|
|
callback(JSON.parse(data))
|
|
})
|
|
} else {
|
|
callback(undefined) // file not found
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method to open/create and modify a json file containing thread information
|
|
*
|
|
* @param filename name of the thread file
|
|
* @param thread the thread with all of the interactions
|
|
* @param message message contents and from who
|
|
*/
|
|
export function openThreadInfo(filename: string, thread: ThreadChannel, messages: UserMessage[] = []) {
|
|
// check if the file exists, if not then make the config file
|
|
const fullFileName = `data/${filename}`
|
|
if (fs.existsSync(fullFileName)) {
|
|
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
|
if (error)
|
|
console.log(`[Error: openConfig] Incorrect file format`)
|
|
else {
|
|
const object = JSON.parse(data)
|
|
object['messages'] = messages as []
|
|
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
|
}
|
|
})
|
|
} else { // file doesn't exist, create it
|
|
const object: Configuration = JSON.parse(`{ \"id\": \"${thread?.id}\", \"name\": \"${thread?.name}\", \"messages\": []}`)
|
|
|
|
const directory = path.dirname(fullFileName)
|
|
if (!fs.existsSync(directory))
|
|
fs.mkdirSync(directory, { recursive: true })
|
|
|
|
// only creating it, no need to add anything
|
|
fs.writeFileSync(fullFileName, JSON.stringify(object, null, 2))
|
|
console.log(`[Util: openThreadInfo] Created '${fullFileName}' in working directory`)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method to obtain the configurations of the message chat/thread
|
|
*
|
|
* @param filename name of the configuration file to get
|
|
* @param callback function to allow a promise from getting the config
|
|
*/
|
|
export async function getThread(filename: string, callback: (config: Thread | undefined) => void): Promise<void> {
|
|
// attempt to read the file and get the configuration
|
|
const fullFileName = `data/${filename}`
|
|
if (fs.existsSync(fullFileName)) {
|
|
fs.readFile(fullFileName, 'utf8', (error, data) => {
|
|
if (error) {
|
|
callback(undefined)
|
|
return // something went wrong... stop
|
|
}
|
|
callback(JSON.parse(data))
|
|
})
|
|
} else {
|
|
callback(undefined) // file not found
|
|
}
|
|
} |