Skip to content

Commit

Permalink
ok.. we can load our app without the persistent database now :=)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeus committed Dec 9, 2023
1 parent 2fd2ccc commit f9520ed
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xyntopia",
"version": "0.0.2",
"version": "0.1.0",
"description": "Modular Frontend for Xyntopia Applications and Company Page",
"productName": "Xyntopia",
"author": "thomas meschede / yeus <thomas.meschede@web.de>",
Expand Down
13 changes: 11 additions & 2 deletions src/boot/taskyon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { boot } from 'quasar/wrappers';
import { reactive } from 'vue';
import { TaskManager } from 'src/modules/taskManager';
import { LLMTask } from 'src/modules/types';
import { createTaskyonDatabase } from 'src/modules/rxdb';
import { createTaskyonDatabase, TaskyonDatabase } from 'src/modules/rxdb';
import { run } from 'src/modules/taskWorker';
import { useTaskyonStore } from 'src/stores/taskyonState';

Expand All @@ -15,9 +15,17 @@ export async function getTaskManager() {
// we are creating a reactive map for our memory-based task databae
// this ensures, that we receive upates for our task in our UI.
const TaskList = reactive<Map<string, LLMTask>>(new Map());
const taskyonDBInstance = await createTaskyonDatabase('taskyondb');
console.log('initializing taskyondb');
let taskyonDBInstance: TaskyonDatabase | undefined = undefined;
try {
taskyonDBInstance = await createTaskyonDatabase('taskyondb');
} catch (err) {
console.log('could not initialize taskyonDB', err);
}
console.log('initializing task manager');
taskManagerInstance = new TaskManager(TaskList, taskyonDBInstance);
}
console.log('finished initialization');
return taskManagerInstance;
}

Expand All @@ -29,5 +37,6 @@ export default boot(async (/* { app, router, ... } */) => {

// keys are reactive here, so in theory, when they change, taskyon should automatically
// pick up on this...
console.log('starting taskyon worker');
void run(state.chatState, taskManagerRef, state.keys);
});
2 changes: 1 addition & 1 deletion src/modules/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ export async function getOpenAIAssistantResponse(
}

// Update file mapping database
await taskManager.getFileDB().bulkUpsert(currentTaskListfileMappings);
await taskManager.bulkUpsertFiles(currentTaskListfileMappings)

// Finally, Convert task list to OpenAI thread messages
const openAIConversationThread = convertTasksToOpenAIThread(
Expand Down
14 changes: 10 additions & 4 deletions src/modules/rxdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import type { RxStorageMemory } from 'rxdb/plugins/storage-memory';
import { addRxPlugin } from 'rxdb';
import { RxDBJsonDumpPlugin } from 'rxdb/plugins/json-dump';
import { LLMTask } from './types';
// TOOD: remove at some point in the future...
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';
addRxPlugin(RxDBDevModePlugin);
addRxPlugin(RxDBJsonDumpPlugin);

const llmTaskSchemaLiteral = {
title: 'LLMTask schema',
version: 0,
version: 0.1,
type: 'object',
primaryKey: 'id',
properties: {
id: {
type: 'string',
primary: true,
maxLength: 128, // <- the primary key must have set maxLength
},
role: {
type: 'string',
Expand Down Expand Up @@ -91,11 +95,11 @@ export const llmTaskSchema: RxJsonSchema<LLMTaskDocType> = llmTaskSchemaLiteral;

const fileMappingSchemaLiteral = {
title: 'FileMapping schema',
version: 0,
version: 0.1,
type: 'object',
primaryKey: 'uuid',
properties: {
uuid: { type: 'string' },
uuid: { type: 'string', maxLength: 128 },
opfs: { type: 'string' },
openAIFileId: { type: 'string' },
fileData: { type: 'string' },
Expand Down Expand Up @@ -156,7 +160,9 @@ export function transformLLMTaskToDocType(llmTask: LLMTask): LLMTaskDocType {
const nonReactiveLLMTask = JSON.parse(JSON.stringify(llmTask)) as LLMTask;
return {
...nonReactiveLLMTask,
configuration: llmTask.configuration ? JSON.stringify(llmTask.configuration) : undefined,
configuration: llmTask.configuration
? JSON.stringify(llmTask.configuration)
: undefined,
debugging: llmTask.debugging
? JSON.stringify(llmTask.debugging)
: undefined,
Expand Down
70 changes: 48 additions & 22 deletions src/modules/taskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,16 @@ export async function addFile(
fileMapping.openAIFileId = openAIFileId;
}

const fileMappingDoc = await taskManager.getFileDB().insert(fileMapping);
return fileMappingDoc.uuid;
await taskManager.addFile(fileMapping);
return fileMapping.uuid;
}

export async function getFileMappingByUuid(
uuid: string,
taskManager: TaskManager
): Promise<FileMappingDocType | null> {
// Find the document with the matching UUID
const fileMappingDoc = await taskManager.getFileDB().findOne(uuid).exec();
const fileMappingDoc = await taskManager.getFileMappingByUuid(uuid);

// Check if the document exists
if (!fileMappingDoc) {
Expand All @@ -178,7 +178,9 @@ export async function getFile(uuid: string, taskManager: TaskManager) {
export function findAllFilesInTasks(taskList: LLMTask[]): string[] {
const fileSet = new Set<string>();
taskList.forEach((task) => {
(task.configuration?.uploadedFiles || []).forEach((file) => fileSet.add(file));
(task.configuration?.uploadedFiles || []).forEach((file) =>
fileSet.add(file)
);
});
return Array.from(fileSet);
}
Expand Down Expand Up @@ -231,7 +233,7 @@ export class TaskManager {
// uses RxDB as a DB backend..
// Usage example:
// const taskManager = new TaskManager(initialTasks, taskyonDBInstance);
private taskyonDB: TaskyonDatabase;
private taskyonDB: TaskyonDatabase | undefined;
private tasks: Map<string, LLMTask>;
// we need these locks in order to sync our databases..
private taskLocks: Map<string, Lock> = new Map();
Expand All @@ -240,7 +242,7 @@ export class TaskManager {
(task?: LLMTask, taskNum?: number) => void
> = [];

constructor(tasks: TaskManager['tasks'], taskyonDB: TaskyonDatabase) {
constructor(tasks: TaskManager['tasks'], taskyonDB?: TaskyonDatabase) {
this.tasks = tasks;
this.taskyonDB = taskyonDB;
void this.initializeTasksFromDB();
Expand All @@ -262,23 +264,21 @@ export class TaskManager {
}

async initializeTasksFromDB() {
const tasksFromDb = await this.taskyonDB.llmtasks.find().exec();
tasksFromDb.forEach((taskDoc) => {
const task = transformDocToLLMTask(taskDoc);
this.tasks.set(task.id, task);
});
console.log('all tasks loaded from DB!');
this.notifySubscribers(undefined, true);
}

getFileDB() {
return this.taskyonDB.filemappings;
if (this.taskyonDB) {
const tasksFromDb = await this.taskyonDB.llmtasks.find().exec();
tasksFromDb.forEach((taskDoc) => {
const task = transformDocToLLMTask(taskDoc);
this.tasks.set(task.id, task);
});
console.log('all tasks loaded from DB!');
this.notifySubscribers(undefined, true);
}
}

private async unblockedGetTask(taskId: string): Promise<LLMTask | undefined> {
// Check if the task exists in the local record
let task = this.tasks.get(taskId);
if (!task) {
if (!task && this.taskyonDB) {
// If not, load from the database
const taskFromDb = await this.taskyonDB.llmtasks.findOne(taskId).exec();
if (taskFromDb) {
Expand Down Expand Up @@ -328,22 +328,48 @@ export class TaskManager {
private async saveTask(taskId: string): Promise<void> {
const task = this.tasks.get(taskId);
console.log('save task: ', task);
if (task) {
if (task && this.taskyonDB) {
const newDBTask = transformLLMTaskToDocType(task);
await this.taskyonDB.llmtasks.upsert(newDBTask);
}
}

async addFile(fileMapping: FileMappingDocType) {
if (this.taskyonDB) {
const fileMappingDoc = await this.taskyonDB.filemappings.insert(
fileMapping
);
return fileMappingDoc?.uuid;
}
}

async bulkUpsertFiles(filemappings: FileMappingDocType[]) {
if (this.taskyonDB) {
await this.taskyonDB.filemappings.bulkUpsert(filemappings);
}
}

async getFileMappingByUuid(uuid: string) {
if (this.taskyonDB) {
const fileMappingDoc = await this.taskyonDB.filemappings
.findOne(uuid)
.exec();
return fileMappingDoc;
}
}

async deleteTask(taskId: string): Promise<void> {
const unlock = await this.lockTask(taskId);
console.log('deleting task:', taskId);
// Delete from local record
this.tasks.delete(taskId);

// Delete from the database
const taskDoc = await this.taskyonDB.llmtasks.findOne(taskId).exec();
if (taskDoc) {
await taskDoc.remove();
if (this.taskyonDB) {
const taskDoc = await this.taskyonDB.llmtasks.findOne(taskId).exec();
if (taskDoc) {
await taskDoc.remove();
}
}
console.log('done deleting task:', taskId);
this.notifySubscribers(taskId, true);
Expand Down

0 comments on commit f9520ed

Please sign in to comment.