From 50b9cb4d0657bc1e6f53af60db0889848f2dba1e Mon Sep 17 00:00:00 2001 From: WFH Brian Date: Tue, 16 Apr 2024 15:39:39 -0400 Subject: [PATCH] js docs --- docs/AnthropicAdapter.html | 4 +- docs/CohereAdapter.html | 4 +- docs/Collection.html | 4 +- docs/CollectionItem.html | 4 +- docs/GeminiAdapter.html | 4 +- docs/ObsMultiAJSON.html | 812 ++++++++++++++++++ docs/ObsidianAJSON.html | 631 ++++++++++++++ docs/SmartChat.html | 4 +- docs/SmartChatModel.html | 4 +- docs/SmartEmbed.html | 4 +- docs/SmartMarkdown.html | 4 +- docs/SmartSocket.html | 4 +- docs/global.html | 4 +- docs/index.html | 4 +- ...mart-chat-model_adapters_anthropic.js.html | 4 +- docs/smart-chat-model_adapters_cohere.js.html | 4 +- ...rt-chat-model_adapters_cohere.test.js.html | 4 +- docs/smart-chat-model_adapters_gemini.js.html | 4 +- ...at-model_dist_smart_chat_model_web.js.html | 4 +- .../smart-chat-model_smart_chat_model.js.html | 4 +- docs/smart-chat-model_streamer.js.html | 4 +- ...hat-model_utils_is_valid_tool_call.js.html | 4 +- docs/smart-chats_smart_chat.js.html | 4 +- docs/smart-chats_smart_chat_md.js.html | 4 +- docs/smart-chunks_SmartMarkdown.js.html | 4 +- docs/smart-collections_Collection.js.html | 4 +- docs/smart-collections_CollectionItem.js.html | 4 +- docs/smart-collections_ObsMultiAJSON.js.html | 163 ++++ docs/smart-collections_ObsidianAJSON.js.html | 145 ++++ docs/smart-embed_SmartEmbed.js.html | 4 +- docs/smart-embed_smart_socket.js.html | 4 +- 31 files changed, 1805 insertions(+), 54 deletions(-) create mode 100644 docs/ObsMultiAJSON.html create mode 100644 docs/ObsidianAJSON.html create mode 100644 docs/smart-collections_ObsMultiAJSON.js.html create mode 100644 docs/smart-collections_ObsidianAJSON.js.html diff --git a/docs/AnthropicAdapter.html b/docs/AnthropicAdapter.html index fa461387..091635bb 100644 --- a/docs/AnthropicAdapter.html +++ b/docs/AnthropicAdapter.html @@ -1447,13 +1447,13 @@
Returns:

diff --git a/docs/CohereAdapter.html b/docs/CohereAdapter.html index 54aa1c1b..bbf224b0 100644 --- a/docs/CohereAdapter.html +++ b/docs/CohereAdapter.html @@ -797,13 +797,13 @@
Returns:

diff --git a/docs/Collection.html b/docs/Collection.html index e45de253..5c2949f4 100644 --- a/docs/Collection.html +++ b/docs/Collection.html @@ -3092,13 +3092,13 @@
Returns:

diff --git a/docs/CollectionItem.html b/docs/CollectionItem.html index 9de6ebb3..dcc5b958 100644 --- a/docs/CollectionItem.html +++ b/docs/CollectionItem.html @@ -1849,13 +1849,13 @@
Returns:

diff --git a/docs/GeminiAdapter.html b/docs/GeminiAdapter.html index 36eee496..bc157060 100644 --- a/docs/GeminiAdapter.html +++ b/docs/GeminiAdapter.html @@ -1790,13 +1790,13 @@
Returns:

diff --git a/docs/ObsMultiAJSON.html b/docs/ObsMultiAJSON.html new file mode 100644 index 00000000..eeda3c43 --- /dev/null +++ b/docs/ObsMultiAJSON.html @@ -0,0 +1,812 @@ + + + + + JSDoc: Class: ObsMultiAJSON + + + + + + + + + + +
+ +

Class: ObsMultiAJSON

+ + + + + + +
+ +
+ +

ObsMultiAJSON(collection)

+ +

Class representing a specialized form of LongTermMemory that handles multiple .ajson files. +It extends the LongTermMemory class to manage collections of items stored in .ajson format.

+ + +
+ +
+
+ + + + +

Constructor

+ + + +

new ObsMultiAJSON(collection)

+ + + + + + +
+

Creates an instance of ObsMultiAJSON.

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
collection + + +Object + + + +

The collection of items to be managed.

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +

Members

+ + + +

data_path

+ + + + +
+

Gets the data path for storing .ajson files, appending '/multi' to the base path.

+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + +

Methods

+ + + + + + + +

(async) _save(forceopt)

+ + + + + + +
+

Asynchronously saves modified collection items to their respective .ajson files.

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDefaultDescription
force + + +boolean + + + + + + <optional>
+ + + + + +
+ + false + +

Forces the save operation even if it's currently flagged as saving.

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

(async) load()

+ + + + + + +
+

Asynchronously loads collection items from .ajson files within the specified data path. +It ensures that only .ajson files are processed and handles JSON parsing and item instantiation.

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

save()

+ + + + + + +
+

Schedules a save operation to prevent multiple saves happening at the same time.

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

(async) validate_save(new_file_path, old_file_path) → {Promise.<boolean>}

+ + + + + + +
+

Validates the save operation by comparing the file sizes of the new and old files.

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
new_file_path + + +string + + + +

Path to the new file.

old_file_path + + +string + + + +

Path to the old file.

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + +
Returns:
+ + +
+
    +
  • True if the new file size is at least 50% of the old file size, otherwise false.
  • +
+
+ + + +
+
+ Type +
+
+ +Promise.<boolean> + + +
+
+ + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/docs/ObsidianAJSON.html b/docs/ObsidianAJSON.html new file mode 100644 index 00000000..ba343c7d --- /dev/null +++ b/docs/ObsidianAJSON.html @@ -0,0 +1,631 @@ + + + + + JSDoc: Class: ObsidianAJSON + + + + + + + + + + +
+ +

Class: ObsidianAJSON

+ + + + + + +
+ +
+ +

ObsidianAJSON(collection)

+ +

Class representing a specialized JSON storage system for Obsidian, extending the LongTermMemory class. +This class handles the loading and saving of collections in a JSON format with specific handling +for memory optimization and error management.

+ + +
+ +
+
+ + + + +

Constructor

+ + + +

new ObsidianAJSON(collection)

+ + + + + + +
+

Creates an instance of ObsidianAJSON.

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
collection + + +Object + + + +

The collection of items to be managed.

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +

Members

+ + + +

file_name

+ + + + +
+

Getter for the file name with a custom extension.

+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + +

Methods

+ + + + + + + +

(async) _save(forceopt)

+ + + + + + +
+

Internal method to save the collection to a file. +It checks for conditions to avoid unnecessary saves and handles errors.

+
+ + + + + + + + + +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeAttributesDefaultDescription
force + + +boolean + + + + + + <optional>
+ + + + + +
+ + false + +

Forces the save operation regardless of the usual checks.

+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

(async) load()

+ + + + + + +
+

Asynchronously loads the collection from a JSON file. +Parses the JSON file, instantiates items based on their class names, and handles errors such as missing files.

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +

save()

+ + + + + + +
+

Saves the collection to a file with a delay to prevent multiple saves at once. +This method sets up a timeout to delay the actual save operation.

+
+ + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/docs/SmartChat.html b/docs/SmartChat.html index 189a45c6..afeb1c82 100644 --- a/docs/SmartChat.html +++ b/docs/SmartChat.html @@ -253,13 +253,13 @@
Parameters:

diff --git a/docs/SmartChatModel.html b/docs/SmartChatModel.html index 7bab6d4f..42eb9225 100644 --- a/docs/SmartChatModel.html +++ b/docs/SmartChatModel.html @@ -1416,13 +1416,13 @@
Returns:

diff --git a/docs/SmartEmbed.html b/docs/SmartEmbed.html index 8921ac4c..89cd7e75 100644 --- a/docs/SmartEmbed.html +++ b/docs/SmartEmbed.html @@ -1234,13 +1234,13 @@
Returns:

diff --git a/docs/SmartMarkdown.html b/docs/SmartMarkdown.html index f91408ea..4f2a1680 100644 --- a/docs/SmartMarkdown.html +++ b/docs/SmartMarkdown.html @@ -2138,13 +2138,13 @@
Returns:

diff --git a/docs/SmartSocket.html b/docs/SmartSocket.html index e1cd2b46..24c8b062 100644 --- a/docs/SmartSocket.html +++ b/docs/SmartSocket.html @@ -1579,13 +1579,13 @@

unload
- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/global.html b/docs/global.html index ea431f92..3d1aed6d 100644 --- a/docs/global.html +++ b/docs/global.html @@ -1496,13 +1496,13 @@
Returns:

- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/index.html b/docs/index.html index 993cd742..5a8453e0 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,13 +50,13 @@


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_adapters_anthropic.js.html b/docs/smart-chat-model_adapters_anthropic.js.html index a3df570f..dd23ba4f 100644 --- a/docs/smart-chat-model_adapters_anthropic.js.html +++ b/docs/smart-chat-model_adapters_anthropic.js.html @@ -147,13 +147,13 @@

Source: smart-chat-model/adapters/anthropic.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_adapters_cohere.js.html b/docs/smart-chat-model_adapters_cohere.js.html index 00385620..c8003dfc 100644 --- a/docs/smart-chat-model_adapters_cohere.js.html +++ b/docs/smart-chat-model_adapters_cohere.js.html @@ -125,13 +125,13 @@

Source: smart-chat-model/adapters/cohere.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_adapters_cohere.test.js.html b/docs/smart-chat-model_adapters_cohere.test.js.html index 804213c2..7b558bbf 100644 --- a/docs/smart-chat-model_adapters_cohere.test.js.html +++ b/docs/smart-chat-model_adapters_cohere.test.js.html @@ -145,13 +145,13 @@

Source: smart-chat-model/adapters/cohere.test.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_adapters_gemini.js.html b/docs/smart-chat-model_adapters_gemini.js.html index 71821df2..9bfe9cb5 100644 --- a/docs/smart-chat-model_adapters_gemini.js.html +++ b/docs/smart-chat-model_adapters_gemini.js.html @@ -218,13 +218,13 @@

Source: smart-chat-model/adapters/gemini.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_dist_smart_chat_model_web.js.html b/docs/smart-chat-model_dist_smart_chat_model_web.js.html index 1e01fac8..973437b2 100644 --- a/docs/smart-chat-model_dist_smart_chat_model_web.js.html +++ b/docs/smart-chat-model_dist_smart_chat_model_web.js.html @@ -985,13 +985,13 @@

Source: smart-chat-model/dist/smart_chat_model_web.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_smart_chat_model.js.html b/docs/smart-chat-model_smart_chat_model.js.html index 910fe661..ae6413e0 100644 --- a/docs/smart-chat-model_smart_chat_model.js.html +++ b/docs/smart-chat-model_smart_chat_model.js.html @@ -381,13 +381,13 @@

Source: smart-chat-model/smart_chat_model.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_streamer.js.html b/docs/smart-chat-model_streamer.js.html index 0d42312f..21ee9f47 100644 --- a/docs/smart-chat-model_streamer.js.html +++ b/docs/smart-chat-model_streamer.js.html @@ -217,13 +217,13 @@

Source: smart-chat-model/streamer.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chat-model_utils_is_valid_tool_call.js.html b/docs/smart-chat-model_utils_is_valid_tool_call.js.html index 0de09603..fe620ccd 100644 --- a/docs/smart-chat-model_utils_is_valid_tool_call.js.html +++ b/docs/smart-chat-model_utils_is_valid_tool_call.js.html @@ -112,13 +112,13 @@

Source: smart-chat-model/utils/is_valid_tool_call.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chats_smart_chat.js.html b/docs/smart-chats_smart_chat.js.html index 235b8fe3..2d07c0ff 100644 --- a/docs/smart-chats_smart_chat.js.html +++ b/docs/smart-chats_smart_chat.js.html @@ -126,13 +126,13 @@

Source: smart-chats/smart_chat.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chats_smart_chat_md.js.html b/docs/smart-chats_smart_chat_md.js.html index 39f6351e..433ec5c4 100644 --- a/docs/smart-chats_smart_chat_md.js.html +++ b/docs/smart-chats_smart_chat_md.js.html @@ -196,13 +196,13 @@

Source: smart-chats/smart_chat_md.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-chunks_SmartMarkdown.js.html b/docs/smart-chunks_SmartMarkdown.js.html index 3386a081..e7720064 100644 --- a/docs/smart-chunks_SmartMarkdown.js.html +++ b/docs/smart-chunks_SmartMarkdown.js.html @@ -310,13 +310,13 @@

Source: smart-chunks/SmartMarkdown.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-collections_Collection.js.html b/docs/smart-collections_Collection.js.html index 7d17db53..16e63604 100644 --- a/docs/smart-collections_Collection.js.html +++ b/docs/smart-collections_Collection.js.html @@ -290,13 +290,13 @@

Source: smart-collections/Collection.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-collections_CollectionItem.js.html b/docs/smart-collections_CollectionItem.js.html index d5a41401..a432fa7c 100644 --- a/docs/smart-collections_CollectionItem.js.html +++ b/docs/smart-collections_CollectionItem.js.html @@ -217,13 +217,13 @@

Source: smart-collections/CollectionItem.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-collections_ObsMultiAJSON.js.html b/docs/smart-collections_ObsMultiAJSON.js.html new file mode 100644 index 00000000..ed4c4186 --- /dev/null +++ b/docs/smart-collections_ObsMultiAJSON.js.html @@ -0,0 +1,163 @@ + + + + + JSDoc: Source: smart-collections/ObsMultiAJSON.js + + + + + + + + + + +
+ +

Source: smart-collections/ObsMultiAJSON.js

+ + + + + + +
+
+
const { LongTermMemory } = require('./long_term_memory');
+
+/**
+ * Class representing a specialized form of LongTermMemory that handles multiple .ajson files.
+ * It extends the LongTermMemory class to manage collections of items stored in .ajson format.
+ */
+class ObsMultiAJSON extends LongTermMemory {
+  /**
+   * Creates an instance of ObsMultiAJSON.
+   * @param {Object} collection - The collection of items to be managed.
+   */
+  constructor(collection) {
+    super(collection);
+    this.adapter = this.env.main.app.vault.adapter;
+  }
+
+  /**
+   * Asynchronously loads collection items from .ajson files within the specified data path.
+   * It ensures that only .ajson files are processed and handles JSON parsing and item instantiation.
+   */
+  async load() {
+    console.log("Loading collection items");
+    if(!(await this.adapter.exists(this.data_path))) await this.adapter.mkdir(this.data_path);
+    const files = (await this.adapter.list(this.data_path)).files; // List all files in the directory
+    for (const file_path of files) {
+      try {
+        if (file_path.endsWith('.ajson')) { // Ensure it's an .ajson file
+          const content = await this.adapter.read(file_path);
+          // if last char is , remove it before wrapping in {} and parse
+          const data = JSON.parse(`{${content.endsWith(',') ? content.slice(0, -1) : content}}`);
+          let pruned = '';
+          Object.entries(data).forEach(([key, value]) => {
+            const entity = new (this.env.item_types[value.class_name])(this.env, value);
+            this.env[entity.collection_name].items[key] = entity;
+            pruned += entity.ajson + ',\n';
+          });
+          // parsed data removes any duplicate keys (pruned)
+          await this.adapter.write(file_path, pruned.trim());
+        }
+      } catch (err) {
+        console.log("Error loading file: " + file_path);
+        console.log(err.stack); // stack trace
+      }
+    }
+    console.log("Loaded collection items");
+  }
+
+  /**
+   * Schedules a save operation to prevent multiple saves happening at the same time.
+   */
+  save() {
+    if(this.save_timeout) clearTimeout(this.save_timeout);
+    this.save_timeout = setTimeout(() => { this._save(); }, 10000);
+  }
+
+  /**
+   * Asynchronously saves modified collection items to their respective .ajson files.
+   * @param {boolean} [force=false] - Forces the save operation even if it's currently flagged as saving.
+   */
+  async _save(force = false) {
+    let saved_ct = 0;
+    if(this._saving) return console.log("Already saving");
+    this._saving = true; // prevent multiple saves at once
+    setTimeout(() => { this._saving = false; }, 10000); // set _saving to false after 10 seconds
+    const start = Date.now();
+    console.log("Saving collection items");
+    // ensure data_path exists
+    if(!(await this.adapter.exists(this.data_path))) await this.adapter.mkdir(this.data_path);
+    const items = Object.values(this.items).filter(i => i.vec && i.changed);
+    if(items.length === 0) {
+      this._saving = false;
+      console.log("Nothing to save");
+      return;
+    }
+    try {
+      for (const item of items) {
+        const item_file_path = `${this.data_path}/${item.multi_ajson_file_name}.ajson`; // Use item.file_name for file naming
+        await this.adapter.append(item_file_path, '\n' + item.ajson + ',');
+        saved_ct++;
+      }
+      const end = Date.now(); // log time
+      const time = end - start;
+      console.log(`Saved ${saved_ct} collection items in ${time}ms`);
+    } catch (err) {
+      console.error("Error saving collection items");
+      console.error(err.stack);
+    }
+    this._saving = false;
+  }
+
+  /**
+   * Validates the save operation by comparing the file sizes of the new and old files.
+   * @param {string} new_file_path - Path to the new file.
+   * @param {string} old_file_path - Path to the old file.
+   * @returns {Promise<boolean>} - True if the new file size is at least 50% of the old file size, otherwise false.
+   */
+  async validate_save(new_file_path, old_file_path) {
+    const new_file_size = (await this.adapter.stat(new_file_path))?.size;
+    const old_file_size = (await this.adapter.stat(old_file_path))?.size;
+    if(!old_file_size) return true;
+    console.log("New file size: " + new_file_size + " bytes");
+    console.log("Old file size: " + old_file_size + " bytes");
+    return new_file_size > (old_file_size * 0.5);
+  }
+
+  /**
+   * Gets the data path for storing .ajson files, appending '/multi' to the base path.
+   * @returns {string} The data path for .ajson files.
+   */
+  get data_path() { return super.data_path + '/multi'; }
+}
+
+exports.ObsMultiAJSON = ObsMultiAJSON;
+
+
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time) +
+ + + + + diff --git a/docs/smart-collections_ObsidianAJSON.js.html b/docs/smart-collections_ObsidianAJSON.js.html new file mode 100644 index 00000000..1e667762 --- /dev/null +++ b/docs/smart-collections_ObsidianAJSON.js.html @@ -0,0 +1,145 @@ + + + + + JSDoc: Source: smart-collections/ObsidianAJSON.js + + + + + + + + + + +
+ +

Source: smart-collections/ObsidianAJSON.js

+ + + + + + +
+
+
const { LongTermMemory } = require('./long_term_memory');
+/**
+ * Class representing a specialized JSON storage system for Obsidian, extending the LongTermMemory class.
+ * This class handles the loading and saving of collections in a JSON format with specific handling
+ * for memory optimization and error management.
+ */
+class ObsidianAJSON extends LongTermMemory {
+  /**
+   * Creates an instance of ObsidianAJSON.
+   * @param {Object} collection - The collection of items to be managed.
+   */
+  constructor(collection) {
+    super(collection);
+    this.adapter = this.brain.main.app.vault.adapter;
+  }
+
+  /**
+   * Asynchronously loads the collection from a JSON file.
+   * Parses the JSON file, instantiates items based on their class names, and handles errors such as missing files.
+   */
+  async load() {
+    console.log("Loading: " + this.file_path);
+    try {
+      // replaced reviver b/c it was using too much memory
+      Object.entries(JSON.parse(`{${await this.adapter.read(this.file_path)}}`)).forEach(([key, value]) => {
+        this.collection.items[key] = new (this.brain.item_types[value.class_name])(this.brain, value);
+        // this.collection.keys.push(key); // replaced by getter
+      });
+      console.log("Loaded: " + this.file_name);
+    } catch (err) {
+      console.log("Error loading: " + this.file_path);
+      console.log(err.stack); // stack trace
+      // Create folder and file if they don't exist
+      if (err.code === 'ENOENT') {
+        this.items = {};
+        // this.keys = []; // replaced by getter
+        try {
+          await this.adapter.mkdir(this.data_path);
+          await this.adapter.write(this.file_path, "");
+        } catch (creationErr) {
+          console.log("Failed to create folder or file: ", creationErr);
+        }
+      }
+    }
+  }
+
+  /**
+   * Saves the collection to a file with a delay to prevent multiple saves at once.
+   * This method sets up a timeout to delay the actual save operation.
+   */
+  save() {
+    if(this.save_timeout) clearTimeout(this.save_timeout);
+    this.save_timeout = setTimeout(() => { this._save(); }, 10000);
+  }
+
+  /**
+   * Internal method to save the collection to a file.
+   * It checks for conditions to avoid unnecessary saves and handles errors.
+   * @param {boolean} [force=false] - Forces the save operation regardless of the usual checks.
+   */
+  async _save(force=false) {
+    if(this.save_timeout) clearTimeout(this.save_timeout);
+    this.save_timeout = null;
+    if(this._saving) return console.log("Already saving: " + this.file_name);
+    this._saving = true; // prevent multiple saves at once
+    setTimeout(() => { this._saving = false; }, 10000); // set _saving to false after 10 seconds
+    const start = Date.now();
+    console.log("Saving: " + this.file_name);
+    try {
+      // const file_content = JSON.stringify(this.items, this.replacer.bind(this), 2); // pretty print
+      const file_content = JSON.stringify(this.items, this.replacer.bind(this)); // minified
+      const new_size = file_content.length;
+      if(!force && (new_size < 100)) return console.log("File content empty, not saving"); // if file content empty, do not save
+      const old_size = (await this.adapter.stat(this.file_path))?.size || 0;
+      if(!force && (new_size < (0.8 * old_size))) return console.log("File content smaller than 80% of original, not saving " + this.file_name ); // if file content smaller than 80% of original, do not save
+      // replaced slice with substring and removed comma (prefix appends with comma instead of suffix to prevent removal at load)
+      await this.adapter.write( this.file_path, file_content.substring(1, file_content.length - 1));
+      const end = Date.now(); // log time
+      const time = end - start;
+      console.log("Saved " + this.file_name + " in " + time + "ms");
+    } catch (err) {
+      console.error("Error saving: " + this.file_name);
+      console.error(err.stack);
+    }
+    this._saving = false;
+  }
+
+  /**
+   * Getter for the file name with a custom extension.
+   * @returns {string} The file name appended with '.ajson'.
+   */
+  get file_name() { return super.file_name + '.ajson'; }
+}
+
+exports.ObsidianAJSON = ObsidianAJSON;
+
+
+ + + + +
+ + + +
+ +
+ Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time) +
+ + + + + diff --git a/docs/smart-embed_SmartEmbed.js.html b/docs/smart-embed_SmartEmbed.js.html index e14442b2..46ffd5ef 100644 --- a/docs/smart-embed_SmartEmbed.js.html +++ b/docs/smart-embed_SmartEmbed.js.html @@ -122,13 +122,13 @@

Source: smart-embed/SmartEmbed.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)
diff --git a/docs/smart-embed_smart_socket.js.html b/docs/smart-embed_smart_socket.js.html index fae4176f..5b988abd 100644 --- a/docs/smart-embed_smart_socket.js.html +++ b/docs/smart-embed_smart_socket.js.html @@ -231,13 +231,13 @@

Source: smart-embed/smart_socket.js


- Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:30:53 GMT-0400 (Eastern Daylight Time) + Documentation generated by JSDoc 4.0.2 on Tue Apr 16 2024 15:39:32 GMT-0400 (Eastern Daylight Time)