jodit-pro
Version:
PRO Version of Jodit Editor
2 lines (1 loc) • 3.62 kB
JavaScript
var v=Object.defineProperty;var a=(n,t)=>v(n,"name",{value:t,configurable:!0});import{AsyncStorage as w}from"jodit/esm/core/storage/async-storage.js";const l=class l{constructor(t,e){this.storage=t,this.maxConversations=e}close(){return this.storage.close()}getConversationKey(t){return`conv:${t}`}async getAllKeys(){const e=await this.storage.get("__conversation_index__");return e&&Array.isArray(e)?e:[]}async updateIndex(t){await this.storage.set("__conversation_index__",t)}async addToIndex(t){const e=await this.getAllKeys();e.includes(t)||(e.push(t),await this.updateIndex(e))}async removeFromIndex(t){const s=(await this.getAllKeys()).filter(r=>r!==t);await this.updateIndex(s)}async list(t){var e,s;try{const r=await this.getAllKeys(),i=[],c=t?.toLowerCase().trim();for(const d of r){const o=await this.get(d);if(o){if(c){const g=(e=o.title)===null||e===void 0?void 0:e.toLowerCase().includes(c),u=!g&&o.messages.some(y=>y.content.toLowerCase().includes(c));if(!g&&!u)continue}i.push({id:o.id,title:(s=o.title)!==null&&s!==void 0?s:"",created:o.created,updated:o.updated,messageCount:o.messages.length})}}return i.sort((d,o)=>o.updated-d.updated),i}catch(r){return console.error("Failed to list conversations:",r),[]}}async get(t){try{const e=this.getConversationKey(t);return await this.storage.get(e)||null}catch(e){return console.error(`Failed to get conversation ${t}:`,e),null}}async save(t){try{if(t.messages.length===0)throw new Error("Conversation must have at least one message.");const e=await this.getAllKeys();e.length>=this.maxConversations&&!e.includes(t.id)&&await this.evictOldest();const s=this.getConversationKey(t.id);await this.storage.set(s,t),await this.addToIndex(t.id)}catch(e){if(e?.name==="QuotaExceededError"||e?.code===22||e?.code===1014){await this.evictOldest();try{const s=this.getConversationKey(t.id);await this.storage.set(s,t),await this.addToIndex(t.id)}catch{throw new Error("Storage quota exceeded. Please delete some conversations.")}}else throw new Error(`Failed to save conversation: ${e?.message||"Unknown error"}`)}}async delete(t){try{const e=this.getConversationKey(t);await this.storage.delete(e),await this.removeFromIndex(t)}catch(e){console.error(`Failed to delete conversation ${t}:`,e)}}async clear(){try{const t=await this.getAllKeys();for(const e of t){const s=this.getConversationKey(e);await this.storage.delete(s)}await this.updateIndex([])}catch(t){console.error("Failed to clear conversations:",t)}}async evictOldest(){try{const t=await this.list();if(t.length===0)return;t.sort((s,r)=>s.updated-r.updated);const e=t[0];await this.delete(e.id)}catch(t){console.error("Failed to evict oldest conversation:",t)}}async getGlobalSettings(){try{return await this.storage.get("global-settings")||null}catch{return null}}async saveGlobalSettings(t){try{const s={...await this.getGlobalSettings(),...t};await this.storage.set("global-settings",s)}catch(e){throw console.error("Failed to save global settings:",e),new Error("Failed to save global settings")}}};a(l,"WebStorageConversationStorage");let h=l;function p(n,t){return w.makeStorage(n,t)}a(p,"createStorageProvider");function f(){return`conv_${Date.now()}_${Math.random().toString(36).substring(2,11)}`}a(f,"generateConversationId");function _(){return`msg_${Date.now()}_${Math.random().toString(36).substring(2,11)}`}a(_,"generateMessageId");function K(){return`tc_${Date.now()}_${Math.random().toString(36).substring(2,11)}`}a(K,"generateToolCallId");export{h as WebStorageConversationStorage,p as createStorageProvider,f as generateConversationId,_ as generateMessageId,K as generateToolCallId};