1- // memory-api.js
2- // This file creates a bridge between the Memory API used by chat-part3.js / ui.js
3- // and the underlying Storage-based memory methods.
4-
51document . addEventListener ( "DOMContentLoaded" , ( ) => {
62 window . Memory = {
7- /**
8- * Get the full list of memories stored in localStorage.
9- * @returns {string[] } An array of memory strings.
10- */
113 getMemories : function ( ) {
124 if ( ! window . Storage || typeof Storage . getMemories !== 'function' ) {
135 console . warn ( "Storage API is missing or incomplete. Returning empty memory array." ) ;
@@ -16,11 +8,6 @@ document.addEventListener("DOMContentLoaded", () => {
168 return Storage . getMemories ( ) || [ ] ;
179 } ,
1810
19- /**
20- * Add a new memory entry to localStorage, if it’s not empty/duplicate.
21- * @param {string } text - The memory text to store.
22- * @returns {boolean } True if successfully added; false otherwise.
23- */
2411 addMemoryEntry : function ( text ) {
2512 if ( ! text || typeof text !== 'string' || text . trim ( ) === '' ) {
2613 console . warn ( "Attempted to add an empty or invalid memory entry." ) ;
@@ -49,11 +36,6 @@ document.addEventListener("DOMContentLoaded", () => {
4936 }
5037 } ,
5138
52- /**
53- * Remove a specific memory entry by its array index.
54- * @param {number } index - The memory array index to remove.
55- * @returns {boolean } True if removed; false otherwise.
56- */
5739 removeMemoryEntry : function ( index ) {
5840 const memories = this . getMemories ( ) ;
5941 if ( index < 0 || index >= memories . length ) {
@@ -75,10 +57,6 @@ document.addEventListener("DOMContentLoaded", () => {
7557 }
7658 } ,
7759
78- /**
79- * Clear all memory entries from localStorage.
80- * @returns {boolean } True if cleared; false otherwise.
81- */
8260 clearAllMemories : function ( ) {
8361 if ( ! window . Storage || typeof Storage . clearAllMemories !== 'function' ) {
8462 console . error ( "Storage API not available for clearAllMemories." ) ;
@@ -94,12 +72,6 @@ document.addEventListener("DOMContentLoaded", () => {
9472 }
9573 } ,
9674
97- /**
98- * Replace the memory at a given index with new text.
99- * @param {number } index - The memory array index to update.
100- * @param {string } newText - The new text to store at that index.
101- * @returns {boolean } True if updated successfully; false otherwise.
102- */
10375 updateMemoryEntry : function ( index , newText ) {
10476 const memories = this . getMemories ( ) ;
10577 if ( index < 0 || index >= memories . length ) {
@@ -111,12 +83,9 @@ document.addEventListener("DOMContentLoaded", () => {
11183 return false ;
11284 }
11385
114- // We don't strictly check duplicates for edits, so user can overwrite with whatever they want.
115- // We'll just do it:
11686 const updatedText = newText . trim ( ) ;
11787
11888 try {
119- // Manually update the local array and then store it.
12089 memories [ index ] = updatedText ;
12190 localStorage . setItem ( "pollinations_memory" , JSON . stringify ( memories ) ) ;
12291 console . log ( `Memory at index ${ index } updated to: ${ updatedText } ` ) ;
@@ -127,28 +96,16 @@ document.addEventListener("DOMContentLoaded", () => {
12796 }
12897 } ,
12998
130- /**
131- * Update an existing memory that matches `pattern`, or create a new memory if none was found.
132- * @param {string } pattern - The text pattern to search for in existing memories.
133- * @param {string } newText - The new memory text to insert or update.
134- * @returns {boolean } True if updated or added successfully; false otherwise.
135- */
13699 updateOrAddMemory : function ( pattern , newText ) {
137100 const memories = this . getMemories ( ) ;
138101 const index = memories . findIndex ( mem => mem . includes ( pattern ) ) ;
139102
140- // If it exists, remove it first, then add the new text
141103 if ( index !== - 1 ) {
142104 this . removeMemoryEntry ( index ) ;
143105 }
144106 return this . addMemoryEntry ( newText ) ;
145107 } ,
146108
147- /**
148- * Example helper: store user’s preference for voice (spoken) or silent AI.
149- * @param {boolean } enabled - Whether the user wants voice speaking enabled.
150- * @returns {boolean } True if updated or added successfully; false otherwise.
151- */
152109 setVoicePreference : function ( enabled ) {
153110 const text = `Voice Preference: User prefers AI responses to be ${ enabled ? 'spoken aloud' : 'not spoken' } .` ;
154111 return this . updateOrAddMemory ( "Voice Preference:" , text ) ;
0 commit comments