@plteam/chat-ui
Version:
CUI Kit is a free and open-source library for creating AI assistant chat interfaces, built with React, Material UI, and TypeScript
62 lines (61 loc) • 2.27 kB
JavaScript
import { ObservableReactValue } from '../utils/observers';
import { ThreadModel } from './ThreadModel';
import { ThreadListCache } from './ThreadListCache';
import { EventsEmitter } from './EventsEmitter';
export class Threads {
list = new ObservableReactValue([]);
currentThread = new ObservableReactValue(undefined);
listGroups = new ThreadListCache();
menuDrawerOpen = new ObservableReactValue(false);
deleteItem = new ObservableReactValue(undefined);
renameItem = new ObservableReactValue(undefined);
emitter = new EventsEmitter();
constructor(adapter, threads, onUserMessageSent) {
const threadConstructor = ((t) => new ThreadModel(adapter.transformThread(t), onUserMessageSent));
this.list.value = threads.map(threadConstructor);
}
get = (id) => {
return this.list.value.find(d => d.id === id);
};
delete = (id) => {
const thread = this.get(id);
if (thread) {
this.list.value = this.list.value.filter(d => d.id !== id);
}
};
rename = (id, title) => {
const thread = this.get(id);
if (thread) {
thread.title = title;
}
};
/**
* Populates a Thread instance from provided data parameters, or fetches the existing instance
* if it already exists in the list.
*
* @param params - The arguments required to create a Thread instance, including data and a stream function.
* @returns The existing or newly created Thread instance.
*/
/*fromData = (...params: ConstructorParameters<typeof ThreadModel<DM, DD>>) => {
const [data, streamFn] = params;
let thread = this.get(data.id);
if (!thread) {
thread = new ThreadModel(data, streamFn);
this.list.value = [...this.list.value, thread];
}
return thread;
}*/
createFromData = (...params) => {
const [data, streamFn] = params;
return new ThreadModel(data, streamFn);
};
setMenuDrawerOpen = (v) => {
this.menuDrawerOpen.value = v;
};
setDeleteItem = (v) => {
this.deleteItem.value = v;
};
setRenameItem = (v) => {
this.renameItem.value = v;
};
}