Skip to content

Commit

Permalink
making moves monday
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpetro committed May 6, 2024
1 parent a07f1bd commit 774e4d3
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 99 deletions.
2 changes: 1 addition & 1 deletion build/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const swagger_jsdoc = require('swagger-jsdoc');
return;
}
files.forEach(file => {
if(path.extname(file) !== '.ejs') return;
if(!['.ejs', '.md'].includes(path.extname(file))) return;
const file_path = path.join(templates_dir, file);
const content = fs.readFileSync(file_path, 'utf8');
views[path.basename(file, path.extname(file))] = content;
Expand Down
18 changes: 9 additions & 9 deletions build/views.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/default_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function default_settings() {
// Smart Blocks Settings (chunking)
embed_input_min_chars: 50,
multi_heading_blocks: true,
// v2.2
enable_mobile: true,
// V1
api_key: "",
excluded_headings: "",
Expand Down
54 changes: 41 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const { SmartSearch } = require("./smart_search.js"); // rename to sc_search.js
const { SmartNotices } = require("./smart_notices.js"); // rename to sc_notices.js (extract smart_notices.js as standard structure first)
// v2.1
const { ScChatView } = require("./sc_chat_view.js");
const { ScSettingsTab } = require("./sc_settings.js");
const { ScSettings } = require("./sc_settings.js");
const { ScSettingsTab } = require("./sc_settings_tab.js");
const embed_models = require('smart-embed-model/models.json');
const { ScActionsUx } = require("./sc_actions_ux.js");
const { open_note } = require("./open_note.js");
Expand All @@ -31,8 +32,10 @@ class SmartConnectionsPlugin extends Plugin {
ScChatView,
}
}
get ScSettings() { return ScSettings };
async open_note(target_path, event=null) { await open_note(this, target_path, event); }
async load_settings() {
Object.assign(this, this.constructor.defaults);
Object.assign(this.settings, await this.loadData());
this.handle_deprecated_settings(); // HANDLE DEPRECATED SETTINGS
}
Expand All @@ -46,7 +49,6 @@ class SmartConnectionsPlugin extends Plugin {
}
async initialize() {
console.log("Loading Smart Connections v2...");
Object.assign(this, this.constructor.defaults);
await this.load_settings();
this.smart_connections_view = null;
this.add_commands(); // add commands
Expand Down Expand Up @@ -136,7 +138,7 @@ class SmartConnectionsPlugin extends Plugin {
await window.app.plugins.enablePlugin(id);
console.log("plugin restarted", id);
};
window.restart_plugin(this.manifest.id);
await window.restart_plugin(this.manifest.id);
}

add_commands() {
Expand Down Expand Up @@ -241,14 +243,19 @@ class SmartConnectionsPlugin extends Plugin {
get chat_view() { return ScChatView.get_view(this.app.workspace); }
// get folders, traverse non-hidden sub-folders
async get_folders(path = "/") {
const folders = (await this.app.vault.adapter.list(path)).folders;
let folder_list = [];
for (let i = 0; i < folders.length; i++) {
if (folders[i].startsWith(".")) continue;
folder_list.push(folders[i]);
folder_list = folder_list.concat(await this.get_folders(folders[i] + "/"));
try {
const folders = (await this.app.vault.adapter.list(path)).folders;
let folder_list = [];
for (let i = 0; i < folders.length; i++) {
if (folders[i].startsWith(".")) continue;
folder_list.push(folders[i]);
folder_list = folder_list.concat(await this.get_folders(folders[i] + "/"));
}
return folder_list;
} catch (error) {
console.warn("Error getting folders", error);
return [];
}
return folder_list;
}
// SUPPORTERS
async sync_notes() {
Expand Down Expand Up @@ -352,9 +359,30 @@ class SmartConnectionsPlugin extends Plugin {
// update chat history conversation folder
this.env.chats.folder = this.settings.smart_chat_folder;
}
// is smart view open
// is_smart_view_open() { return ScSmartView.is_open(this.app.workspace); }
// backwards compatibility

async update_early_access() {
// // if license key is not set, return
if(!this.settings.license_key) return this.show_notice("Supporter license key required for early access update");
const v2 = await this.obsidian.requestUrl({
url: "https://sync.smartconnections.app/download_v2",
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
license_key: this.settings.license_key,
})
});
if(v2.status !== 200) return console.error("Error downloading early access update", v2);
console.log(v2.json);
await this.app.vault.adapter.write(".obsidian/plugins/smart-connections/main.js", v2.json.main); // add new
await this.app.vault.adapter.write(".obsidian/plugins/smart-connections/manifest.json", v2.json.manifest); // add new
await this.app.vault.adapter.write(".obsidian/plugins/smart-connections/styles.css", v2.json.styles); // add new
await window.app.plugins.loadManifests();
await this.restart_plugin();
}

// BEGIN BACKWARD COMPATIBILITY (DEPRECATED: remove before 2.2 stable release)
async handle_deprecated_settings() {
// move api keys (api_key_PLATFORM) to PLATFORM.api_key
Object.entries(this.settings).forEach(([key, value]) => {
Expand Down
46 changes: 46 additions & 0 deletions src/sc_chats_ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,52 @@ class ScChatsUI extends SmartChatsUI {
if (!this.system_prompt_selector) this.system_prompt_selector = new ScSystemPromptSelectModal(this.env.plugin.app, this.env);
this.system_prompt_selector.open();
}
add_chat_input_listeners(){
// register default events in super
super.add_chat_input_listeners();
// register custom events
const chat_input = this.container.querySelector(".sc-chat-form");
this.brackets_ct = 0;
this.prevent_input = false;
chat_input.addEventListener("keyup", this.key_up_handler.bind(this));
}
key_up_handler(e){
const textarea = this.container.querySelector(".sc-chat-form textarea");
if(!["/", "@", "["].includes(e.key)) return;
const caret_pos = textarea.selectionStart;
// if key is open square bracket
if (e.key === "[") {
// if previous char is [
if (textarea.value[caret_pos - 2] === "[") {
// open file suggestion modal
this.open_file_suggestion_modal();
return;
}
} else {
this.brackets_ct = 0;
}
// if / is pressed
if (e.key === "/") {
// get caret position
// if this is first char or previous char is space
if (textarea.value.length === 1 || textarea.value[caret_pos - 2] === " ") {
// open folder suggestion modal
this.open_folder_suggestion_modal();
return;
}
}
// if @ is pressed
if (e.key === "@") {
// console.log("caret_pos", caret_pos);
// get caret position
// if this is first char or previous char is space
if (textarea.value.length === 1 || textarea.value[caret_pos - 2] === " ") {
// open system prompt suggestion modal
this.open_system_prompt_modal();
return;
}
}
}
}
exports.ScChatsUI = ScChatsUI;

Expand Down
21 changes: 3 additions & 18 deletions src/sc_settings.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
const { SmartSettings } = require("./smart_settings");
const smart_embed_models = require("smart-embed-model/models.json");
const { PluginSettingTab } = require("obsidian");
const { SmartChatSettings } = require("./smart_chat_settings");
const { SmartEmbedSettings } = require("./smart_embed_settings");

class ScSettingsTab extends PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
this.config = plugin.settings;
}
display() {
this.smart_settings = new ScSettings(this.plugin.env, this.containerEl);
return this.smart_settings.render();
}
}
exports.ScSettingsTab = ScSettingsTab;

// Smart Connections Specific Settings
class ScSettings extends SmartSettings {
constructor(env, container, template_name = "smart_settings") {
Expand Down Expand Up @@ -86,10 +72,9 @@ class ScSettings extends SmartSettings {
this.update("muted_notices", this.plugin.settings.muted_notices);
this.render(); // re-render settings
}

// DO: REMOVE FROM STABLE RELEASE
revert_to_v20() {
this.plugin.revert_to_v20();
// upgrade to early access
async upgrade_to_early_access() {
await this.plugin.update_early_access();
}
}
exports.ScSettings = ScSettings;
Expand Down
14 changes: 14 additions & 0 deletions src/sc_settings_tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { PluginSettingTab } = require("obsidian");

class ScSettingsTab extends PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
this.config = plugin.settings;
}
display() {
this.smart_settings = new this.plugin.ScSettings(this.plugin.env, this.containerEl);
return this.smart_settings.render();
}
}
exports.ScSettingsTab = ScSettingsTab;
8 changes: 3 additions & 5 deletions src/smart_chat_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ class SmartChatSettings extends SmartSettings {
async get_view_data() {
const view_data = {
settings: this.plugin.settings,
chat_platform: this.env.chat_model.platforms[this.plugin.settings.chat_model_platform_key],
chat_platform: this.env.chat_model?.platforms[this.plugin.settings.chat_model_platform_key],
chat_platforms: this.env.chat_model?.platforms ? Object.keys(this.env.chat_model.platforms).map(platform_key => ({ key: platform_key, ...(this.env.chat_model?.platforms[platform_key] || {}) })) : [],
};
view_data.platform_chat_models = await this.plugin.env.chat_model.get_models();
view_data.platform_chat_models = await this.plugin.env.chat_model?.get_models();
view_data.smart_chat_settings = this.ejs.render(this.template, view_data);
return view_data;
}
}
exports.SmartChatSettings = SmartChatSettings;


exports.SmartChatSettings = SmartChatSettings;
26 changes: 26 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ div[data-type="smart-connections-view"] > .view-content {
border-radius: 10px;
resize: none;
height: auto;
min-height: 4lh;
max-height: 100%;
border: 1px solid var(--blockquote-border-color);
}
Expand Down Expand Up @@ -316,3 +317,28 @@ div[data-type="smart-connections-view"] > .view-content {
}
}

.sc-supporters{
max-height: 37ch;
border: 1px solid var(--h1-color);
border-radius: 10px;
padding: 10px;
margin-left: -10px;
box-shadow: 0 1px 0 1px var(--shadow-300);
overflow: auto;
text-wrap: balance;

> p{
margin-top: 0;
margin-bottom: 5px;
}

> ul {
margin-top: 5px;
}

> * li {
margin-top: 0.37em;
}

}

2 changes: 1 addition & 1 deletion src/views/smart_chat_settings.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%- chat_platforms.map((platform, i) => `data-option-${i + 1}="${platform.key}|${platform.description}"`).join('\n') %>
data-callback="changed_smart_chat_model"
></div>
<% if(chat_platform.fetch_models) { %>
<% if(chat_platform?.fetch_models) { %>
<% if(settings[settings.chat_model_platform_key]?.api_key) { %>
<div class="setting-component"
data-name="Model Name"
Expand Down
Loading

0 comments on commit 774e4d3

Please sign in to comment.