Public/Private Chat Threads (#62)

* 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
This commit is contained in:
Kevin Dang
2024-06-10 19:47:08 -07:00
committed by GitHub
parent 9f77c5287f
commit 1973b1d3ae
22 changed files with 199 additions and 49 deletions

View File

@@ -15,7 +15,6 @@ export type EventKeys = keyof ClientEvents // only wants keys of ClientEvents ob
* @param clientUid the discord id for the bot
*/
export type Tokens = {
channel: string,
model: string,
clientUid: string
}

View File

@@ -1,4 +1,7 @@
import { ThreadChannel } from 'discord.js'
import { UserMessage } from './events.js'
import fs from 'fs'
import path from 'path'
export interface Configuration {
readonly name: string
@@ -10,6 +13,12 @@ export interface Configuration {
}
}
export interface Thread {
readonly id: string
readonly name: string
messages: UserMessage[]
}
/**
* Method to open a file in the working directory and modify/create it
*
@@ -17,19 +26,19 @@ export interface Configuration {
* @param key key value to access
* @param value new value to assign
*/
export function openFile(filename: string, key: string, value: any) {
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: openFile] Incorrect file format`)
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 {
} else { // work on dynamic file creation
const object: Configuration = JSON.parse('{ \"name\": \"Discord Ollama Confirgurations\" }')
// set standard information for config file and options
@@ -38,10 +47,16 @@ export function openFile(filename: string, key: string, value: any) {
}
fs.writeFileSync(filename, JSON.stringify(object, null, 2))
console.log(`[Util: openFile] Created 'config.json' in working directory`)
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)) {
@@ -55,4 +70,59 @@ export async function getConfig(filename: string, callback: (config: Configurati
} 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
}
}

View File

@@ -13,7 +13,6 @@ export async function embedMessage(
message: Message,
ollama: Ollama,
tokens: {
channel: string,
model: string
},
msgHist: Queue<UserMessage>,

View File

@@ -13,7 +13,6 @@ export async function normalMessage(
message: Message,
ollama: Ollama,
tokens: {
channel: string,
model: string
},
msgHist: Queue<UserMessage>,