UNPKG

@assistant-ui/react

Version:

React components for AI chat.

148 lines (147 loc) 5.26 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/runtimes/local/LocalThreadListRuntimeCore.tsx var LocalThreadListRuntimeCore_exports = {}; __export(LocalThreadListRuntimeCore_exports, { LocalThreadListRuntimeCore: () => LocalThreadListRuntimeCore }); module.exports = __toCommonJS(LocalThreadListRuntimeCore_exports); var import_idUtils = require("../../utils/idUtils.cjs"); var LocalThreadListRuntimeCore = class { constructor(_threadFactory) { this._threadFactory = _threadFactory; const threadId = (0, import_idUtils.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 = (0, import_idUtils.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(); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { LocalThreadListRuntimeCore }); //# sourceMappingURL=LocalThreadListRuntimeCore.js.map