Skip to content

Commit

Permalink
improved settings UX
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Joseph Petro committed Jul 11, 2024
1 parent ab255ed commit 38f7b8a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
2 changes: 1 addition & 1 deletion build/views.json

Large diffs are not rendered by default.

54 changes: 7 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ class SmartConnectionsPlugin extends Plugin {
get api() { return this._api; }
async load_settings() {
Object.assign(this, this.constructor.defaults); // set defaults
Object.assign(this.settings, await this.loadData()); // overwrites defaults with saved settings
const saved_settings = await this.loadData();
if(!saved_settings){
this.notices.show("fail-load-settings", "Failed to load settings. Restarting plugin...");
this.restart_plugin();
throw new Error("Failed to load settings. Restarting plugin...");
}
Object.assign(this.settings, saved_settings); // overwrites defaults with saved settings
this.handle_deprecated_settings(); // HANDLE DEPRECATED SETTINGS
}
async onload() { this.app.workspace.onLayoutReady(this.initialize.bind(this)); } // initialize when layout is ready
Expand Down Expand Up @@ -292,52 +298,6 @@ class SmartConnectionsPlugin extends Plugin {
}
}
// SUPPORTERS
async sync_notes() {
// if license key is not set, return
if(!this.settings.license_key){
new Notice("Smart Connections: Supporter license key is required to sync notes to the ChatGPT Plugin server.");
return;
}
console.log("syncing notes");
const files = this.brain.files;
const notes = await this.build_notes_object(files);
// POST notes object to server
const response = await requestUrl({
url: "https://sync.smartconnections.app/sync",
method: "POST",
headers: {
"Content-Type": "application/json",
},
contentType: "application/json",
body: JSON.stringify({
license_key: this.settings.license_key,
notes: notes
})
});
console.log(response);
}
async build_notes_object(files) {
let output = {};
for(let i = 0; i < files.length; i++) {
let file = files[i];
let parts = file.path.split("/");
let current = output;
for (let ii = 0; ii < parts.length; ii++) {
let part = parts[ii];
if (ii === parts.length - 1) {
// This is a file
current[part] = await this.app.vault.cachedRead(file);
} else {
// This is a directory
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
}
}
return output;
}
async render_code_block(contents, container, ctx) {
console.log(container);
return this.view.render_nearest((contents.trim().length? contents : ctx.sourcePath), container);
Expand Down
23 changes: 17 additions & 6 deletions src/sc_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,37 @@ const { SmartEmbedSettings } = require("./smart_embed_settings");
class ScSettings extends SmartSettings {
constructor(env, container, template_name = "smart_settings") {
super(env, container, template_name);
this.chat_settings = new SmartChatSettings(env, container, template_name);
this.embed_settings = new SmartEmbedSettings(env, container, template_name);
}
update_smart_chat_folder() { this.chat_settings.update_smart_chat_folder(); }
async changed_smart_chat_model(){
await this.chat_settings.changed_smart_chat_model(false);
this.render();
}
async render(){
await super.render();
const chat_settings_elm = this.container.querySelector(".smart-chat-settings");
if(chat_settings_elm){
this.chat_settings = new SmartChatSettings(this.env, chat_settings_elm, "smart_chat_settings");
await this.chat_settings.render();
}
const embed_settings_elm = this.container.querySelector(".smart-embed-settings");
if(embed_settings_elm){
this.embed_settings = new SmartEmbedSettings(this.env, embed_settings_elm, "smart_embed_settings");
await this.embed_settings.render();
}
}
async test_chat_api_key(){ await this.chat_settings.test_chat_api_key(); }
get self_ref_list() { return this.chat_settings.self_ref_list; }

async refresh_notes() {
this.env.smart_notes.import(this.env.files, { reset: true });
}
reload_env() { this.env.reload(); } // DEPRECATED
restart_plugin() { this.plugin.restart_plugin(); }
restart_plugin() {
this.plugin.notices.show('restarting_for_settings_to_take_effect', "Restarting for settings to take effect...", {timeout: 3000});
this.plugin.restart_plugin();
}
force_refresh() { this.env.force_refresh(); }
sync_for_chatgpt() { this.plugin.sync_notes(); }
update_smart_connections_folder() { this.plugin.update_smart_connections_folder(); }
refresh_smart_view() { this.embed_settings.refresh_smart_view(); }
async connect_to_smart_connect(){ await this.embed_settings.connect_to_smart_connect(); }
Expand Down Expand Up @@ -60,8 +73,6 @@ class ScSettings extends SmartSettings {
included_files: this.plugin.env.files.length,
total_files: this.plugin.env.all_files.length,
muted_notices: this.plugin.settings.muted_notices || false,
...((await this.chat_settings.get_view_data()) || {}),
...((await this.embed_settings.get_view_data()) || {}),
};
return view_data;
}
Expand Down
5 changes: 4 additions & 1 deletion src/smart_embed_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class SmartEmbedSettings extends SmartSettings {
}
}
reload_env() { this.env.reload(); } // DEPRECATED
restart_plugin() { this.plugin.restart_plugin(); }
restart_plugin() {
this.plugin.notices.show('restarting_for_settings_to_take_effect', "Restarting for settings to take effect...", {timeout: 3000});
this.plugin.restart_plugin();
}
get template() { return this.templates['smart_embed_settings']; }
async get_view_data() {
const view_data = {
Expand Down
8 changes: 4 additions & 4 deletions src/smart_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SmartSocket {
if(typeof this.is_server_running === 'function'){
const is_running = await this.is_server_running();
if(!is_running){
console.log("Smart Connect is not running, will try to connect again later");
// console.log("Smart Connect is not running, will try to connect again later");
this.connect(true);
return;
}
Expand All @@ -49,11 +49,11 @@ class SmartSocket {
can_attempt_connection(retry) {
retry = retry || this.retry;
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
console.log("WebSocket is already connected. Aborting new connection attempt.");
// console.log("WebSocket is already connected. Aborting new connection attempt.");
return false;
}
if (retry && this.ws_retries >= 10) {
console.error("Failed to reconnect after 10 attempts");
console.error("Failed to connect to Smart Connect after 10 attempts");
this.on_fail_to_reconnect();
return false;
}
Expand All @@ -69,7 +69,7 @@ class SmartSocket {
if (retry || this.retry) {
this.ws_retries += 1;
const backoff_time = Math.min(1000 * Math.pow(2, this.ws_retries), 60000);
console.log(`Attempting to reconnect in ${backoff_time / 1000} seconds...`);
// console.log(`Attempting to connect to Smart Connect in ${backoff_time / 1000} seconds...`);
return new Promise(resolve => setTimeout(resolve, backoff_time));
}
return Promise.resolve();
Expand Down
24 changes: 22 additions & 2 deletions src/views/smart_settings.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
<h1>Smart Connections</h1>
<% if(this.env.plugin.EARLY_ACCESS && !this.env.plugin.app.plugins.enabledPlugins["smart-templates"]){ %>
<div data-callout-metadata="" data-callout-fold="" data-callout="success" class="callout" style="mix-blend-mode: unset;">
<div class="callout-title">
<div class="callout-icon" dir="auto"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
class="svg-icon lucide-check">
<path d="M20 6 9 17l-5-5"></path>
</svg></div>
<div class="callout-title-inner"><p><strong>Smart Templates:</strong> Try out the new <a href="https://github.com/brianpetro/obsidian-smart-templates">Smart Templates</a>.</p>
<div class="setting-component"
data-name="Install Smart Templates"
data-description="Install the latest version of Smart Templates."
data-type="button"
data-callback="install_smart_templates_latest"
></div>
</div>
</div>
</div>
<% } %>
<% if(this.env.plugin.EARLY_ACCESS && this.env.plugin.obsidian.Platform.isMobile && !this.env.plugin.settings.enable_mobile) { %>
<div data-callout-metadata="" data-callout-fold="" data-callout="warning" class="callout">
<div class="callout-title">
Expand Down Expand Up @@ -124,12 +143,13 @@
<h1>Smart View & Embeddings</h1>
<p>Notes about embedding models:</p>
<ul>
<li>IMPORTANT: make sure local bge-micro-v2 embedding model works before trying other local models.</li>
<li>Local model compatibility depends on available CPU and RAM. Try reducing the max tokens (context) if a local model if failing.</li>
<li>API models are not dependent on local compute, but they require an API key and send your notes to third-party servers for processing.</li>
</ul>
<%- smart_embed_settings %>
<div class="smart-embed-settings"></div>
<h1>Smart Chat</h1>
<%- smart_chat_settings %>
<div class="smart-chat-settings"></div>
<h3>System Prompts</h3>
<div class="setting-component"
data-name="System Prompts Folder"
Expand Down

0 comments on commit 38f7b8a

Please sign in to comment.