Skip to content

Commit

Permalink
Merge pull request #66 from justyns/use-space-config
Browse files Browse the repository at this point in the history
Add support for config:loaded event
  • Loading branch information
justyns authored Sep 8, 2024
2 parents 7b843d9 + effca81 commit f24791f
Show file tree
Hide file tree
Showing 8 changed files with 6,224 additions and 159 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This page is a brief overview of each version.
- Use a separate queue for indexing embeddings and summaries, to prevent blocking the main SB indexing thread
- Refactor to use JSR for most Silverbullet imports, and lots of related changes
- Reduced bundle size
- Add support for [space-config](https://silverbullet.md/Space%20Config)

---
## 0.3.2
Expand Down
10 changes: 9 additions & 1 deletion sbai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ import {
* This should prevent us from having to reload or refresh when changing the settings.
* TODO: This gets triggered when other settings are changed too, but shouldn't make a difference
* when there are no changes to the objects we care about.
* TODO: Remove after space-config has been around for a while
*/
export async function reloadConfig(pageName: string) {
export async function reloadSettingsPage(pageName: string) {
if (pageName === "SETTINGS" || pageName === "SECRETS") {
await initializeOpenAI(true);
}
}

/**
* Similar to the above function, but meant for the config:loaded event.
*/
export async function reloadConfig() {
await initializeOpenAI(true);
}

/**
* Prompts the user to select a text/llm model from the configured models.
*/
Expand Down
6,349 changes: 6,198 additions & 151 deletions silverbullet-ai.plug.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions silverbullet-ai.plug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ functions:
queryAI:
path: sbai.ts:queryAI

reloadConfig:
path: sbai.ts:reloadConfig
reloadSettingsPageEvent:
path: sbai.ts:reloadSettingsPage
events:
- page:saved

reloadConfigEvent:
path: sbai.ts:reloadConfig
events:
- config:loaded

summarizeNote:
path: sbai.ts:openSummaryPanel
command:
Expand Down
2 changes: 1 addition & 1 deletion src/embeddings.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import "./mocks/syscalls.ts";
import { syscall } from "./mocks/syscalls.ts";
import { aiSettings, initializeOpenAI } from "./init.ts";
import {
canIndexPage,
indexEmbeddings,
shouldIndexEmbeddings,
shouldIndexSummaries,
} from "./embeddings.ts";
Expand Down
5 changes: 3 additions & 2 deletions src/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ export async function updateSearchPage() {
queryEmbedding = await generateEmbeddingsOnServer(phrase);
} catch (error) {
console.error("Error generating query vector embeddings", error);
loadingText +=
"\n\n> **error** ⚠️ Failed to generate query vector embeddings.\n";
// deno-fmt-ignore
loadingText += "\n\n> **error** ⚠️ Failed to generate query vector embeddings.\n";
loadingText += `> ${error}\n\n`;
await editor.setText(loadingText);
return;
Expand All @@ -674,6 +674,7 @@ export async function updateSearchPage() {
);
} catch (error) {
console.error("Error searching embeddings", error);
// deno-fmt-ignore
loadingText += "\n\n> **error** ⚠️ Failed to search through embeddings.\n";
loadingText += `> ${error}\n\n`;
await editor.setText(loadingText);
Expand Down
3 changes: 1 addition & 2 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readSecret } from "https://deno.land/x/[email protected]/plug-api/lib/secrets_page.ts";
import { readSetting } from "https://deno.land/x/[email protected]/plug-api/lib/settings_page.ts";
import { clientStore, system } from "@silverbulletmd/silverbullet/syscalls";
import { DallEProvider } from "./providers/dalle.ts";
import { GeminiEmbeddingProvider, GeminiProvider } from "./providers/gemini.ts";
Expand Down Expand Up @@ -347,7 +346,7 @@ async function loadAndMergeSettings() {
indexSummaryPrompt: "",
enhanceFrontMatterPrompt: "",
};
const newSettings = await readSetting("ai", {});
const newSettings = await system.getSpaceConfig("ai", {});
const newCombinedSettings = { ...defaultSettings, ...newSettings };
newCombinedSettings.chat = {
...defaultChatSettings,
Expand Down
3 changes: 3 additions & 0 deletions src/mocks/syscalls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse as parseYAML } from "https://deno.land/[email protected]/yaml/mod.ts";
import { parseMarkdown } from "$common/markdown_parser/parser.ts";
import { syscall } from "@silverbulletmd/silverbullet/syscalls";

let editorText = "Mock data";
(globalThis as any).editorText;
Expand Down Expand Up @@ -71,3 +72,5 @@ function invokeFunctionMock(args: readonly any[]) {
throw Error(`Missing invokeFunction mock for ${args[0]}`);
}
}

export { syscall };

0 comments on commit f24791f

Please sign in to comment.