1 directories, 28 files

tinai

Home / testing / ai / tinai
import Api from './api.js';

/**
 * Class Storage
 * Manages the application's local storage persistence.
 * Handles configuration settings, the global conversation index,
 * and individual conversation history data.
 */
class Storage {
	constructor() {
		this.api = new Api();
		this.user_id_prefix = '';
		this.KEY_LOCATION_ID = 'LOCATION_ID';
	}

	async init() {
		this.init_location_id();
		const user_id = await this.api.get_user_id();
		if (user_id) {
			this.user_id_prefix = user_id + '_';
		}
		this.KEY_APP_CONFIG = this.user_id_prefix + 'APP_CONFIG';
		this.KEY_CONFIG_THEME = 'THEME';
		this.KEY_CONFIG_SELECTED_CONVERSATION_GUID = 'SELECTED_CONVERSATION_GUID';
		this.KEY_CONFIG_EXPERIMENTAL_FEATURES = 'EXPERIMENTAL_FEATURES';
		this.KEY_CONFIG_DEFAULTS = this.user_id_prefix + 'APP_CONFIG_DEFAULTS';
		this.KEY_CONFIG_DEFAULT_VERBOSITY = 'VERBOSITY';
		this.KEY_CONFIG_DEFAULT_MODEL = 'MODEL';
		this.KEY_CONFIG_SHOW_SUGGESTED_QUERIES = 'SHOW_SUGGESTED_QUERIES';
		this.KEY_CONFIG_SHOW_RELATED_QUERIES = 'SHOW_RELATED_QUERIES';
		this.KEY_CONFIG_ENFORCE_TOPICS = 'ENFORCE_TOPICS';
		this.KEY_CONFIG_AUTO_RUN_PROPOSED_QUERIES = 'AUTO_RUN_PROPOSED_QUERIES';
		this.KEY_APP_INDEX = this.user_id_prefix + 'APP_INDEX';
		this.KEY_INDEX_DATE_CREATED = 'DATE_CREATED';
		this.KEY_INDEX_DATE_UPDATED = 'DATE_UPDATED';
		this.KEY_INDEX_SYNC = 'SYNC';
		this.KEY_INDEX_GUID = 'GUID';
		this.KEY_SHOW_ARCHIVES = "SHOW_ARCHIVES";
		this.KEY_APP_CONVERSATION_PREFIX = this.user_id_prefix + 'CONVERSATION_';
		this.KEY_CONVERSATION_TITLE = 'TITLE';
		this.KEY_CONVERSATION_SUMMARY = 'SUMMARY';
		this.KEY_CONVERSATION_TIMESTAMP = "TIMESTAMP";
		this.KEY_CONVERSATION_HISTORY = "HISTORY";
		this.KEY_CONVERSATION_ARCHIVED = "ARCHIVED";
		this.KEY_CONVERSATION_VERBOSITY = "VERBOSITY";
		this.KEY_CONVERSATION_MODEL = "MODEL";
		this.KEY_CONVERSATION_TYPE = "TYPE";
		this.KEY_CONVERSATION_SHOW_SUGGESTED_QUERIES = 'SHOW_SUGGESTED_QUERIES';
		this.KEY_CONVERSATION_SHOW_RELATED_QUERIES = 'SHOW_RELATED_QUERIES';
		this.KEY_CONVERSATION_ENFORCE_TOPICS = 'ENFORCE_TOPICS';
		this.KEY_CONVERSATION_AUTO_RUN_PROPOSED_QUERIES = 'AUTO_RUN_PROPOSED_QUERIES';
		this.KEY_CONVERSATION_TOPICS = 'TOPICS';
		this.KEY_CONVERSATION_CONSIDERATIONS = 'CONSIDERATIONS';
		this.CONVERSATION_TYPE_CHAT = "CHAT";
		this.CONVERSATION_TYPE_NOTEBOOK = "NOTEBOOK";
	}

	init_location_id() {
		let location_id = localStorage.getItem(this.KEY_LOCATION_ID);
		if (!location_id) {
			if (typeof crypto !== 'undefined' && crypto.randomUUID) {
				location_id = crypto.randomUUID();
			} else {
				let ts = new Date().getTime();
				location_id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
					var r = (ts + Math.random() * 16) % 16 | 0;
					ts = Math.floor(ts / 16);
					return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
				});
			}
			localStorage.setItem(this.KEY_LOCATION_ID, location_id);
		}
	}

	get_location_id() {
		return localStorage.getItem(this.KEY_LOCATION_ID);
	}

	//region App Defaults

	/**
	 * Updates a specific key in the application defaults object and dispatches a storage event.
	 * @param {string} key The key for the item in the APP_CONFIG_DEFAULTS object to be updated.
	 * @param {*} value The new value to be assigned to the key.
	 */
	update_app_defaults(key, value) {
		const defaults = this.get_app_defaults();
		defaults[key] = value;
		localStorage.setItem(this.KEY_CONFIG_DEFAULTS, JSON.stringify(defaults));
		const event = new StorageEvent('storage', {
			key: this.KEY_CONFIG_DEFAULTS
		});
		window.dispatchEvent(event);
	}

	/**
	 * Retrieves the current application defaults object from localStorage.
	 * @returns {Object} The parsed defaults object.
	 */
	get_app_defaults() {
		const defaults = JSON.parse(localStorage.getItem(this.KEY_CONFIG_DEFAULTS) || '{}');
		if (defaults[this.KEY_CONFIG_SHOW_SUGGESTED_QUERIES] === undefined) {
			defaults[this.KEY_CONFIG_SHOW_SUGGESTED_QUERIES] = false;
		}
		if (defaults[this.KEY_CONFIG_SHOW_RELATED_QUERIES] === undefined) {
			defaults[this.KEY_CONFIG_SHOW_RELATED_QUERIES] = false;
		}
		if (defaults[this.KEY_CONFIG_ENFORCE_TOPICS] === undefined) {
			defaults[this.KEY_CONFIG_ENFORCE_TOPICS] = false;
		}
		if (defaults[this.KEY_CONFIG_AUTO_RUN_PROPOSED_QUERIES] === undefined) {
			defaults[this.KEY_CONFIG_AUTO_RUN_PROPOSED_QUERIES] = false;
		}
		return defaults;
	}

	//endregion

	//region App Configuration

	/**
	 * Updates a specific key in the application configuration object and dispatches a storage event.
	 * @param {string} key The key for the item in the APP_CONFIG object to be updated.
	 * @param {*} value The new value to be assigned to the key.
	 */
	update_app_config(key, value) {
		const config = this.get_app_config();
		config[key] = value;
		localStorage.setItem(this.KEY_APP_CONFIG, JSON.stringify(config));
		const event = new StorageEvent('storage', {
			key: this.KEY_APP_CONFIG
		});
		window.dispatchEvent(event);
	}

	/**
	 * Retrieves the current application configuration object from localStorage.
	 * @returns {Object} The parsed configuration object.
	 */
	get_app_config() {
		return JSON.parse(localStorage.getItem(this.KEY_APP_CONFIG) || '{}');
	}

	//endregion

	//region App Index Management

	/**
	 * Updates or creates metadata for a conversation in the global application index.
	 * @param {string} guid The unique identifier of the conversation. If empty, a new GUID is generated.
	 * @param {boolean} sync Whether the conversation should be marked for synchronization.
	 * @param {string} type The type of conversation, defaults to CHAT.
	 * @returns {string} The GUID of the updated or newly created conversation.
	 */
	update_app_index(guid, sync, type = this.CONVERSATION_TYPE_CHAT) {
		const index = this.get_app_index();
		const now = new Date().getTime();
		let found = false;

		guid = guid.trim();

		if (guid !== '') {
			for (let i = 0; i < index.length; i++) {
				if (index[i][this.KEY_INDEX_GUID] === guid) {
					index[i][this.KEY_INDEX_DATE_UPDATED] = now;
					index[i][this.KEY_INDEX_SYNC] = sync;
					found = true;
					break;
				}
			}
		} else {
			if (typeof crypto !== 'undefined' && crypto.randomUUID) {
				guid = crypto.randomUUID();
			} else {
				let ts = new Date().getTime();
				while (index.some(item => item[this.KEY_INDEX_GUID] === ts.toString())) {
					ts++;
				}
				guid = ts.toString();
			}
		}

		if (!found) {
			const item = {};
			item[this.KEY_INDEX_GUID] = guid;
			item[this.KEY_INDEX_DATE_CREATED] = now;
			item[this.KEY_INDEX_DATE_UPDATED] = now;
			item[this.KEY_INDEX_SYNC] = sync;
			index.push(item);
			
			const conversation = this.get_conversation(guid);
			conversation[this.KEY_CONVERSATION_TYPE] = type;
			if (type === this.CONVERSATION_TYPE_NOTEBOOK) {
				conversation[this.KEY_CONVERSATION_TITLE] = 'New Notebook';
			}
			this.save_conversation(guid, conversation);
		}

		localStorage.setItem(this.KEY_APP_INDEX, JSON.stringify(index));
		const event = new StorageEvent('storage', {
			key: this.KEY_APP_INDEX
		});
		window.dispatchEvent(event);
		return guid;
	}

	/**
	 * Deletes a conversation by its GUID from the application index and localStorage.
	 * @param {string} guid The unique identifier of the conversation to delete.
	 */
	index_delete(guid) {
		const config = this.get_app_config();
		if (config[this.KEY_CONFIG_SELECTED_CONVERSATION_GUID] === guid) {
			this.update_app_config(this.KEY_CONFIG_SELECTED_CONVERSATION_GUID, '');
		}
		let index = this.get_app_index();
		index = index.filter(item => item[this.KEY_INDEX_GUID] !== guid);
		localStorage.setItem(this.KEY_APP_INDEX, JSON.stringify(index));
		localStorage.removeItem(this.KEY_APP_CONVERSATION_PREFIX + guid);
		const event = new StorageEvent('storage', {
			key: this.KEY_APP_INDEX
		});
		window.dispatchEvent(event);
	}

	/**
	 * Retrieves the global list of all conversation metadata from localStorage.
	 * @returns {Array} An array of conversation index objects.
	 */
	get_app_index() {
		return JSON.parse(localStorage.getItem(this.KEY_APP_INDEX) || '[]');
	}

	//endregion

	//region Conversation Data Management

	/**
	 * Retrieves the full conversation object for the given GUID from localStorage.
	 * @param {string} guid The unique identifier of the conversation.
	 * @returns {Object} The conversation object or an empty object if not found.
	 */
	get_conversation(guid) {
		if (!guid) return {};
		const conversation = JSON.parse(localStorage.getItem(this.KEY_APP_CONVERSATION_PREFIX + guid) || '{}');
		if (conversation[this.KEY_CONVERSATION_SHOW_SUGGESTED_QUERIES] === undefined) {
			conversation[this.KEY_CONVERSATION_SHOW_SUGGESTED_QUERIES] = false;
		}
		if (conversation[this.KEY_CONVERSATION_SHOW_RELATED_QUERIES] === undefined) {
			conversation[this.KEY_CONVERSATION_SHOW_RELATED_QUERIES] = false;
		}
		if (conversation[this.KEY_CONVERSATION_ENFORCE_TOPICS] === undefined) {
			conversation[this.KEY_CONVERSATION_ENFORCE_TOPICS] = false;
		}
		if (conversation[this.KEY_CONVERSATION_AUTO_RUN_PROPOSED_QUERIES] === undefined) {
			conversation[this.KEY_CONVERSATION_AUTO_RUN_PROPOSED_QUERIES] = false;
		}
		if (conversation[this.KEY_CONVERSATION_TOPICS] === undefined) {
			conversation[this.KEY_CONVERSATION_TOPICS] = [];
		}
		if (conversation[this.KEY_CONVERSATION_CONSIDERATIONS] === undefined) {
			conversation[this.KEY_CONVERSATION_CONSIDERATIONS] = [];
		}
		return conversation;
	}

	/**
	 * Saves the full conversation object for the given GUID to localStorage.
	 * @param {string} guid The unique identifier of the conversation.
	 * @param {Object} conversation The conversation object to save.
	 */
	save_conversation(guid, conversation) {
		if (!guid) return;
		localStorage.setItem(this.KEY_APP_CONVERSATION_PREFIX + guid, JSON.stringify(conversation));
		window.dispatchEvent(new StorageEvent('storage', {
			key: this.KEY_APP_CONVERSATION_PREFIX + guid
		}));
	}

	/**
	 * Updates a specific field within a conversation object and dispatches a storage event.
	 * @param {string} guid The unique identifier of the conversation.
	 * @param {string} key The key of the field to update within the conversation object.
	 * @param {*} value The new value for the specified field.
	 */
	update_conversation_field(guid, key, value) {
		if (!guid) return;
		const conversation = this.get_conversation(guid);
		conversation[key] = value;
		this.save_conversation(guid, conversation);
	}

	/**
	 * Adds a topic to the conversation's topics array.
	 * @param {string} guid The unique identifier of the conversation.
	 * @param {string} topic The topic to add.
	 */
	addTopicToConversation(guid, topic) {
		if (!guid || !topic) return;
		const conversation = this.get_conversation(guid);
		const topics = conversation[this.KEY_CONVERSATION_TOPICS] || [];
		if (!topics.includes(topic)) {
			topics.push(topic);
			this.update_conversation_field(guid, this.KEY_CONVERSATION_TOPICS, topics);
		}
	}

	/**
	 * Deletes a topic from the conversation's topics array.
	 * @param {string} guid The unique identifier of the conversation.
	 * @param {string} topic The topic to delete.
	 */
	deleteTopicFromConversation(guid, topic) {
		if (!guid || !topic) return;
		const conversation = this.get_conversation(guid);
		let topics = conversation[this.KEY_CONVERSATION_TOPICS] || [];
		const updatedTopics = topics.filter(t => t !== topic);
		if (updatedTopics.length !== topics.length) { // Only update if a topic was actually removed
			this.update_conversation_field(guid, this.KEY_CONVERSATION_TOPICS, updatedTopics);
		}
	}

	addConsiderationToConversation(guid, consideration) {
		if (!guid || !consideration) return;
		const conversation = this.get_conversation(guid);
		const considerations = conversation[this.KEY_CONVERSATION_CONSIDERATIONS] || [];
		if (!considerations.includes(consideration)) {
			considerations.push(consideration);
			this.update_conversation_field(guid, this.KEY_CONVERSATION_CONSIDERATIONS, considerations);
		}
	}

	deleteConsiderationFromConversation(guid, consideration) {
		if (!guid || !consideration) return;
		const conversation = this.get_conversation(guid);
		let considerations = conversation[this.KEY_CONVERSATION_CONSIDERATIONS] || [];
		const updatedConsiderations = considerations.filter(c => c !== consideration);
		if (updatedConsiderations.length !== considerations.length) {
			this.update_conversation_field(guid, this.KEY_CONVERSATION_CONSIDERATIONS, updatedConsiderations);
		}
	}

	/**
	 * Retrieves the full conversation object for the currently selected GUID from localStorage.
	 * @returns {Object|null} The conversation object or null if none selected.
	 */
	get_selected_conversation() {
		const config = this.get_app_config();
		const guid = config[this.KEY_CONFIG_SELECTED_CONVERSATION_GUID];
		if (!guid) return null;
		return this.get_conversation(guid);
	}

	//endregion
}

export default Storage;
🌐
storage.js ×
Type: Web, text/plain
12.57 Kilobytes
Last Modified 2026-05-04 01:36:05
⬇ Download File