@assistant-ui/react
Version:
React components for AI chat.
123 lines • 4.12 kB
JavaScript
// src/runtimes/local/LocalThreadListRuntimeCore.tsx
import { generateId } from "../../utils/idUtils.mjs";
var LocalThreadListRuntimeCore = class {
constructor(_threadFactory) {
this._threadFactory = _threadFactory;
const threadId = generateId();
this._threadData.set(threadId, {
data: { messages: [] },
metadata: { threadId },
isArchived: false
});
this._threads = [{ threadId }];
this._mainThread = this._threadFactory(threadId, { messages: [] });
}
_threadData = /* @__PURE__ */ new Map();
_threads = [];
_archivedThreads = [];
get threads() {
return this._threads;
}
get archivedThreads() {
return this._archivedThreads;
}
_mainThread;
get mainThread() {
return this._mainThread;
}
getThreadMetadataById(threadId) {
return this._threadData.get(threadId)?.metadata;
}
switchToThread(threadId) {
if (this._mainThread.threadId === threadId) return;
const data = this._threadData.get(threadId);
if (!data) throw new Error("Thread not found");
const thread = this._threadFactory(threadId, data.data);
this._performThreadSwitch(thread);
}
switchToNewThread() {
const threadId = generateId();
this._threadData.set(threadId, {
data: { messages: [] },
metadata: { threadId },
isArchived: false
});
this._threads = [{ threadId }];
const thread = this._threadFactory(threadId, { messages: [] });
this._performThreadSwitch(thread);
}
_performThreadSwitch(newThreadCore) {
const data = this._threadData.get(this._mainThread.threadId);
if (!data) throw new Error("Thread not found");
const exprt = this._mainThread.export();
data.data = exprt;
this._mainThread._notifyEventSubscribers("switched-away");
this._mainThread = newThreadCore;
newThreadCore._notifyEventSubscribers("switched-to");
this._notifySubscribers();
}
_performMoveOp(threadId, operation) {
const data = this._threadData.get(threadId);
if (!data) throw new Error("Thread not found");
if (operation === "archive" && data.isArchived) return;
if (operation === "unarchive" && !data.isArchived) return;
if (operation === "archive") {
data.isArchived = true;
this._archivedThreads = [...this._archivedThreads, data.metadata];
}
if (operation === "unarchive") {
data.isArchived = false;
this._threads = [...this._threads, data.metadata];
}
if (operation === "delete") {
this._threadData.delete(threadId);
}
if (operation === "archive" || operation === "delete" && data.isArchived) {
this._archivedThreads = this._archivedThreads.filter(
(t) => t.threadId !== threadId
);
}
if (operation === "unarchive" || operation === "delete" && !data.isArchived) {
this._threads = this._threads.filter((t) => t.threadId !== threadId);
}
this._notifySubscribers();
}
async rename(threadId, newTitle) {
const data = this._threadData.get(threadId);
if (!data) throw new Error("Thread not found");
data.metadata = {
...data.metadata,
title: newTitle
};
const threadList = data.isArchived ? this.archivedThreads : this.threads;
const idx = threadList.findIndex((t) => t.threadId === threadId);
const updatedThreadList = threadList.toSpliced(idx, 1, data.metadata);
if (data.isArchived) {
this._archivedThreads = updatedThreadList;
} else {
this._threads = updatedThreadList;
}
this._notifySubscribers();
}
async archive(threadId) {
this._performMoveOp(threadId, "archive");
}
async unarchive(threadId) {
this._performMoveOp(threadId, "unarchive");
}
async delete(threadId) {
this._performMoveOp(threadId, "delete");
}
_subscriptions = /* @__PURE__ */ new Set();
subscribe(callback) {
this._subscriptions.add(callback);
return () => this._subscriptions.delete(callback);
}
_notifySubscribers() {
for (const callback of this._subscriptions) callback();
}
};
export {
LocalThreadListRuntimeCore
};
//# sourceMappingURL=LocalThreadListRuntimeCore.mjs.map