From 66139c8996a3dcb782284399d3a6885450f13aec Mon Sep 17 00:00:00 2001 From: Justyn Shull Date: Tue, 27 Feb 2024 01:41:59 -0600 Subject: [PATCH] Rebuild release bundle --- silverbullet-ai.plug.js | 1763 +-------------------------------------- 1 file changed, 19 insertions(+), 1744 deletions(-) diff --git a/silverbullet-ai.plug.js b/silverbullet-ai.plug.js index 111f596..b3b63d9 100644 --- a/silverbullet-ai.plug.js +++ b/silverbullet-ai.plug.js @@ -1,1748 +1,23 @@ -var __defProp = Object.defineProperty; -var __export = (target, all) => { - for (var name in all) - __defProp(target, name, { get: all[name], enumerable: true }); -}; - -// https://deno.land/x/silverbullet@0.7.1/plugos/worker_runtime.ts -var runningAsWebWorker = typeof window === "undefined" && // @ts-ignore: globalThis -typeof globalThis.WebSocketPair === "undefined"; -if (typeof Deno === "undefined") { - self.Deno = { - args: [], - // @ts-ignore: Deno hack - build: { - arch: "x86_64" - }, - env: { - // @ts-ignore: Deno hack - get() { - } - } - }; -} -var pendingRequests = /* @__PURE__ */ new Map(); -var syscallReqId = 0; -function workerPostMessage(msg) { - self.postMessage(msg); -} -if (runningAsWebWorker) { - globalThis.syscall = async (name, ...args) => { - return await new Promise((resolve, reject) => { - syscallReqId++; - pendingRequests.set(syscallReqId, { resolve, reject }); - workerPostMessage({ - type: "sys", - id: syscallReqId, - name, - args - }); - }); - }; -} -function setupMessageListener(functionMapping2, manifest2) { - if (!runningAsWebWorker) { - return; - } - self.addEventListener("message", (event) => { - (async () => { - const data = event.data; - switch (data.type) { - case "inv": - { - const fn = functionMapping2[data.name]; - if (!fn) { - throw new Error(`Function not loaded: ${data.name}`); - } - try { - const result = await Promise.resolve(fn(...data.args || [])); - workerPostMessage({ - type: "invr", - id: data.id, - result - }); - } catch (e) { - console.error( - "An exception was thrown as a result of invoking function", - data.name, - "error:", - e.message - ); - workerPostMessage({ - type: "invr", - id: data.id, - error: e.message - }); - } - } - break; - case "sysr": - { - const syscallId = data.id; - const lookup = pendingRequests.get(syscallId); - if (!lookup) { - throw Error("Invalid request id"); - } - pendingRequests.delete(syscallId); - if (data.error) { - lookup.reject(new Error(data.error)); - } else { - lookup.resolve(data.result); - } - } - break; - } - })().catch(console.error); - }); - workerPostMessage({ - type: "manifest", - manifest: manifest2 - }); -} -function base64Decode(s) { - const binString = atob(s); - const len = binString.length; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i++) { - bytes[i] = binString.charCodeAt(i); - } - return bytes; -} -function base64Encode(buffer) { - if (typeof buffer === "string") { - buffer = new TextEncoder().encode(buffer); - } - let binary = ""; - const len = buffer.byteLength; - for (let i = 0; i < len; i++) { - binary += String.fromCharCode(buffer[i]); - } - return btoa(binary); -} -async function sandboxFetch(reqInfo, options) { - if (typeof reqInfo !== "string") { - const body = new Uint8Array(await reqInfo.arrayBuffer()); - const encodedBody = body.length > 0 ? base64Encode(body) : void 0; - options = { - method: reqInfo.method, - headers: Object.fromEntries(reqInfo.headers.entries()), - base64Body: encodedBody - }; - reqInfo = reqInfo.url; - } - return syscall("sandboxFetch.fetch", reqInfo, options); -} -globalThis.nativeFetch = globalThis.fetch; -function monkeyPatchFetch() { - globalThis.fetch = async function(reqInfo, init) { - const encodedBody = init && init.body ? base64Encode( - new Uint8Array(await new Response(init.body).arrayBuffer()) - ) : void 0; - const r = await sandboxFetch( - reqInfo, - init && { - method: init.method, - headers: init.headers, - base64Body: encodedBody - } - ); - return new Response(r.base64Body ? base64Decode(r.base64Body) : null, { - status: r.status, - headers: r.headers - }); - }; -} -if (runningAsWebWorker) { - monkeyPatchFetch(); -} - -// ../../../../../../Users/justyns/dev/silverbullet/lib/tree.ts -function addParentPointers(tree) { - if (!tree.children) { - return; - } - for (const child of tree.children) { - if (child.parent) { - return; - } - child.parent = tree; - addParentPointers(child); - } -} -function collectNodesMatching(tree, matchFn) { - if (matchFn(tree)) { - return [tree]; - } - let results = []; - if (tree.children) { - for (const child of tree.children) { - results = [...results, ...collectNodesMatching(child, matchFn)]; - } - } - return results; -} -async function collectNodesMatchingAsync(tree, matchFn) { - if (await matchFn(tree)) { - return [tree]; - } - let results = []; - if (tree.children) { - for (const child of tree.children) { - results = [ - ...results, - ...await collectNodesMatchingAsync(child, matchFn) - ]; - } - } - return results; -} -async function replaceNodesMatchingAsync(tree, substituteFn) { - if (tree.children) { - const children = tree.children.slice(); - for (const child of children) { - const subst = await substituteFn(child); - if (subst !== void 0) { - const pos = tree.children.indexOf(child); - if (subst) { - tree.children.splice(pos, 1, subst); - } else { - tree.children.splice(pos, 1); - } - } else { - await replaceNodesMatchingAsync(child, substituteFn); - } - } - } -} -function findNodeOfType(tree, nodeType) { - return collectNodesMatching(tree, (n) => n.type === nodeType)[0]; -} -function traverseTree(tree, matchFn) { - collectNodesMatching(tree, matchFn); -} -async function traverseTreeAsync(tree, matchFn) { - await collectNodesMatchingAsync(tree, matchFn); -} -function renderToText(tree) { - if (!tree) { - return ""; - } - const pieces = []; - if (tree.text !== void 0) { - return tree.text; - } - for (const child of tree.children) { - pieces.push(renderToText(child)); - } - return pieces.join(""); -} - -// ../../../../../../Users/justyns/dev/silverbullet/lib/json.ts -function expandPropertyNames(a) { - if (!a) { - return a; - } - if (typeof a !== "object") { - return a; - } - if (Array.isArray(a)) { - return a.map(expandPropertyNames); - } - const expanded = {}; - for (const key of Object.keys(a)) { - const parts = key.split("."); - let target = expanded; - for (let i = 0; i < parts.length - 1; i++) { - const part = parts[i]; - if (!target[part]) { - target[part] = {}; - } - target = target[part]; - } - target[parts[parts.length - 1]] = expandPropertyNames(a[key]); - } - return expanded; -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/editor.ts -var editor_exports = {}; -__export(editor_exports, { - confirm: () => confirm, - dispatch: () => dispatch, - downloadFile: () => downloadFile, - filterBox: () => filterBox, - flashNotification: () => flashNotification, - fold: () => fold, - foldAll: () => foldAll, - getCurrentPage: () => getCurrentPage, - getCursor: () => getCursor, - getSelection: () => getSelection, - getText: () => getText, - getUiOption: () => getUiOption, - goHistory: () => goHistory, - hidePanel: () => hidePanel, - insertAtCursor: () => insertAtCursor, - insertAtPos: () => insertAtPos, - moveCursor: () => moveCursor, - navigate: () => navigate, - openCommandPalette: () => openCommandPalette, - openPageNavigator: () => openPageNavigator, - openSearchPanel: () => openSearchPanel, - openUrl: () => openUrl, - prompt: () => prompt, - reloadPage: () => reloadPage, - reloadSettingsAndCommands: () => reloadSettingsAndCommands, - reloadUI: () => reloadUI, - replaceRange: () => replaceRange, - save: () => save, - setPage: () => setPage, - setSelection: () => setSelection, - setText: () => setText, - setUiOption: () => setUiOption, - showPanel: () => showPanel, - toggleFold: () => toggleFold, - unfold: () => unfold, - unfoldAll: () => unfoldAll, - uploadFile: () => uploadFile, - vimEx: () => vimEx -}); - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscall.ts -if (typeof self === "undefined") { - self = { - syscall: () => { - throw new Error("Not implemented here"); - } - }; -} -var syscall2 = globalThis.syscall; - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/editor.ts -function getCurrentPage() { - return syscall2("editor.getCurrentPage"); -} -function setPage(newName) { - return syscall2("editor.setPage", newName); -} -function getText() { - return syscall2("editor.getText"); -} -function setText(newText) { - return syscall2("editor.setText", newText); -} -function getCursor() { - return syscall2("editor.getCursor"); -} -function getSelection() { - return syscall2("editor.getSelection"); -} -function setSelection(from, to) { - return syscall2("editor.setSelection", from, to); -} -function save() { - return syscall2("editor.save"); -} -function navigate(pageRef, replaceState = false, newWindow = false) { - return syscall2("editor.navigate", pageRef, replaceState, newWindow); -} -function openPageNavigator(mode = "page") { - return syscall2("editor.openPageNavigator", mode); -} -function openCommandPalette() { - return syscall2("editor.openCommandPalette"); -} -function reloadPage() { - return syscall2("editor.reloadPage"); -} -function reloadUI() { - return syscall2("editor.reloadUI"); -} -function reloadSettingsAndCommands() { - return syscall2("editor.reloadSettingsAndCommands"); -} -function openUrl(url, existingWindow = false) { - return syscall2("editor.openUrl", url, existingWindow); -} -function goHistory(delta) { - return syscall2("editor.goHistory", delta); -} -function downloadFile(filename, dataUrl) { - return syscall2("editor.downloadFile", filename, dataUrl); -} -function uploadFile(accept, capture) { - return syscall2("editor.uploadFile", accept, capture); -} -function flashNotification(message, type = "info") { - return syscall2("editor.flashNotification", message, type); -} -function filterBox(label, options, helpText = "", placeHolder = "") { - return syscall2("editor.filterBox", label, options, helpText, placeHolder); -} -function showPanel(id, mode, html, script = "") { - return syscall2("editor.showPanel", id, mode, html, script); -} -function hidePanel(id) { - return syscall2("editor.hidePanel", id); -} -function insertAtPos(text, pos) { - return syscall2("editor.insertAtPos", text, pos); -} -function replaceRange(from, to, text) { - return syscall2("editor.replaceRange", from, to, text); -} -function moveCursor(pos, center = false) { - return syscall2("editor.moveCursor", pos, center); -} -function insertAtCursor(text) { - return syscall2("editor.insertAtCursor", text); -} -function dispatch(change) { - return syscall2("editor.dispatch", change); -} -function prompt(message, defaultValue = "") { - return syscall2("editor.prompt", message, defaultValue); -} -function confirm(message) { - return syscall2("editor.confirm", message); -} -function getUiOption(key) { - return syscall2("editor.getUiOption", key); -} -function setUiOption(key, value) { - return syscall2("editor.setUiOption", key, value); -} -function vimEx(exCommand) { - return syscall2("editor.vimEx", exCommand); -} -function fold() { - return syscall2("editor.fold"); -} -function unfold() { - return syscall2("editor.unfold"); -} -function toggleFold() { - return syscall2("editor.toggleFold"); -} -function foldAll() { - return syscall2("editor.foldAll"); -} -function unfoldAll() { - return syscall2("editor.unfoldAll"); -} -function openSearchPanel() { - return syscall2("editor.openSearchPanel"); -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/markdown.ts -var markdown_exports = {}; -__export(markdown_exports, { - parseMarkdown: () => parseMarkdown -}); -function parseMarkdown(text) { - return syscall2("markdown.parseMarkdown", text); -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/space.ts -var space_exports = {}; -__export(space_exports, { - deleteAttachment: () => deleteAttachment, - deleteFile: () => deleteFile, - deletePage: () => deletePage, - getAttachmentMeta: () => getAttachmentMeta, - getFileMeta: () => getFileMeta, - getPageMeta: () => getPageMeta, - listAttachments: () => listAttachments, - listFiles: () => listFiles, - listPages: () => listPages, - listPlugs: () => listPlugs, - readAttachment: () => readAttachment, - readFile: () => readFile, - readPage: () => readPage, - writeAttachment: () => writeAttachment, - writeFile: () => writeFile, - writePage: () => writePage -}); -function listPages(unfiltered = false) { - return syscall2("space.listPages", unfiltered); -} -function getPageMeta(name) { - return syscall2("space.getPageMeta", name); -} -function readPage(name) { - return syscall2("space.readPage", name); -} -function writePage(name, text) { - return syscall2("space.writePage", name, text); -} -function deletePage(name) { - return syscall2("space.deletePage", name); -} -function listPlugs() { - return syscall2("space.listPlugs"); -} -function listAttachments() { - return syscall2("space.listAttachments"); -} -function getAttachmentMeta(name) { - return syscall2("space.getAttachmentMeta", name); -} -function readAttachment(name) { - return syscall2("space.readAttachment", name); -} -function writeAttachment(name, data) { - return syscall2("space.writeAttachment", name, data); -} -function deleteAttachment(name) { - return syscall2("space.deleteAttachment", name); -} -function listFiles() { - return syscall2("space.listFiles"); -} -function readFile(name) { - return syscall2("space.readFile", name); -} -function getFileMeta(name) { - return syscall2("space.getFileMeta", name); -} -function writeFile(name, data) { - return syscall2("space.writeFile", name, data); -} -function deleteFile(name) { - return syscall2("space.deleteFile", name); -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/system.ts -var system_exports = {}; -__export(system_exports, { - getEnv: () => getEnv, - getMode: () => getMode, - getVersion: () => getVersion, - invokeCommand: () => invokeCommand, - invokeFunction: () => invokeFunction, - listCommands: () => listCommands, - listSyscalls: () => listSyscalls, - reloadPlugs: () => reloadPlugs -}); -function invokeFunction(name, ...args) { - return syscall2("system.invokeFunction", name, ...args); -} -function invokeCommand(name, args) { - return syscall2("system.invokeCommand", name, args); -} -function listCommands() { - return syscall2("system.listCommands"); -} -function listSyscalls() { - return syscall2("system.listSyscalls"); -} -function reloadPlugs() { - syscall2("system.reloadPlugs"); -} -function getEnv() { - return syscall2("system.getEnv"); -} -function getMode() { - return syscall2("system.getMode"); -} -function getVersion() { - return syscall2("system.getVersion"); -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/template.ts -var template_exports = {}; -__export(template_exports, { - parseTemplate: () => parseTemplate, - renderTemplate: () => renderTemplate -}); -function renderTemplate(template, obj, globals = {}) { - return syscall2("template.renderTemplate", template, obj, globals); -} -function parseTemplate(template) { - return syscall2("template.parseTemplate", template); -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/syscalls/yaml.ts -var yaml_exports = {}; -__export(yaml_exports, { - parse: () => parse, - stringify: () => stringify -}); -function parse(text) { - return syscall2("yaml.parse", text); -} -function stringify(obj) { - return syscall2("yaml.stringify", obj); -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/lib/frontmatter.ts -async function extractFrontmatter(tree, options = {}) { - let data = { - tags: [] - }; - const tags = []; - addParentPointers(tree); - await replaceNodesMatchingAsync(tree, async (t) => { - if (t.type === "Paragraph" && t.parent?.type === "Document") { - let onlyTags = true; - const collectedTags = /* @__PURE__ */ new Set(); - for (const child of t.children) { - if (child.text) { - if (child.text.startsWith("\n") && child.text !== "\n") { - break; - } - if (child.text.trim()) { - onlyTags = false; - break; - } - } else if (child.type === "Hashtag") { - const tagname = child.children[0].text.substring(1); - collectedTags.add(tagname); - if (options.removeTags === true || options.removeTags?.includes(tagname)) { - child.children[0].text = ""; - } - } else if (child.type) { - onlyTags = false; - break; - } - } - if (onlyTags) { - tags.push(...collectedTags); - } - } - if (t.type === "FrontMatter") { - const yamlNode = t.children[1].children[0]; - const yamlText = renderToText(yamlNode); - try { - const parsedData = await yaml_exports.parse(yamlText); - const newData = { ...parsedData }; - data = { ...data, ...parsedData }; - if (!data.tags) { - data.tags = []; - } - if (typeof data.tags === "string") { - tags.push(...data.tags.split(/,\s*|\s+/)); - } - if (Array.isArray(data.tags)) { - tags.push(...data.tags); - } - if (options.removeKeys && options.removeKeys.length > 0) { - let removedOne = false; - for (const key of options.removeKeys) { - if (key in newData) { - delete newData[key]; - removedOne = true; - } - } - if (removedOne) { - yamlNode.text = await yaml_exports.stringify(newData); - } - } - if (Object.keys(newData).length === 0 || options.removeFrontmatterSection) { - return null; - } - } catch (e) { - console.warn("Could not parse frontmatter", e.message); - } - } - return void 0; - }); - data.tags = [.../* @__PURE__ */ new Set([...tags.map((t) => t.replace(/^#/, ""))])]; - data = expandPropertyNames(data); - return data; -} -async function prepareFrontmatterDispatch(tree, data) { - let dispatchData = null; - await traverseTreeAsync(tree, async (t) => { - if (t.type === "FrontMatter") { - const bodyNode = t.children[1].children[0]; - const yamlText = renderToText(bodyNode); - try { - let frontmatterText = ""; - if (typeof data === "string") { - frontmatterText = yamlText + data + "\n"; - } else { - const parsedYaml = await yaml_exports.parse(yamlText); - const newData = { ...parsedYaml, ...data }; - frontmatterText = await yaml_exports.stringify(newData); - } - dispatchData = { - changes: { - from: bodyNode.from, - to: bodyNode.to, - insert: frontmatterText - } - }; - } catch (e) { - console.error("Error parsing YAML", e); - } - return true; - } - return false; - }); - if (!dispatchData) { - let frontmatterText = ""; - if (typeof data === "string") { - frontmatterText = data + "\n"; - } else { - frontmatterText = await yaml_exports.stringify(data); - } - const fullFrontmatterText = "---\n" + frontmatterText + "---\n"; - dispatchData = { - changes: { - from: 0, - to: 0, - insert: fullFrontmatterText - } - }; - } - return dispatchData; -} - -// https://deno.land/std@0.216.0/encoding/_util.ts -var encoder = new TextEncoder(); - -// https://deno.land/std@0.216.0/encoding/base64.ts -function decodeBase64(b64) { - const binString = atob(b64); - const size = binString.length; - const bytes = new Uint8Array(size); - for (let i = 0; i < size; i++) { - bytes[i] = binString.charCodeAt(i); - } - return bytes; -} - -// ../../../../../../Users/justyns/dev/silverbullet-ai/src/editorUtils.ts -async function getSelectedText() { - const selectedRange = await editor_exports.getSelection(); - let selectedText = ""; - if (selectedRange.from === selectedRange.to) { - selectedText = ""; - } else { - const pageText = await editor_exports.getText(); - selectedText = pageText.slice(selectedRange.from, selectedRange.to); - } - return { - from: selectedRange.from, - to: selectedRange.to, - text: selectedText - }; -} -async function getSelectedTextOrNote() { - const selectedTextInfo = await getSelectedText(); - const pageText = await editor_exports.getText(); - if (selectedTextInfo.text === "") { - return { - from: 0, - to: pageText.length, - text: pageText, - isWholeNote: true - }; - } - const isWholeNote = selectedTextInfo.from === 0 && selectedTextInfo.to === pageText.length; - return { - ...selectedTextInfo, - isWholeNote - }; -} -async function getPageLength() { - const pageText = await editor_exports.getText(); - return pageText.length; -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/lib/yaml_page.ts -async function readCodeBlockPage(pageName, allowedLanguages) { - const text = await space_exports.readPage(pageName); - const tree = await markdown_exports.parseMarkdown(text); - let codeText; - traverseTree(tree, (t) => { - if (t.type !== "FencedCode") { - return false; - } - const codeInfoNode = findNodeOfType(t, "CodeInfo"); - if (allowedLanguages && !codeInfoNode) { - return false; - } - if (allowedLanguages && !allowedLanguages.includes(codeInfoNode.children[0].text)) { - return false; - } - const codeTextNode = findNodeOfType(t, "CodeText"); - if (!codeTextNode) { - return false; - } - codeText = codeTextNode.children[0].text; - return true; - }); - return codeText; -} -async function readYamlPage(pageName, allowedLanguages = ["yaml"]) { - const codeText = await readCodeBlockPage(pageName, allowedLanguages); - if (codeText === void 0) { - return void 0; - } - try { - return yaml_exports.parse(codeText); - } catch (e) { - console.error("YAML Page parser error", e); - throw new Error(`YAML Error: ${e.message}`); - } -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/lib/secrets_page.ts -async function readSecret(key) { - try { - const allSecrets = await readYamlPage("SECRETS", ["yaml", "secrets"]); - const val = allSecrets[key]; - if (val === void 0) { - throw new Error(`No such secret: ${key}`); - } - return val; - } catch (e) { - if (e.message === "Not found") { - throw new Error(`No such secret: ${key}`); - } - throw e; - } -} - -// https://deno.land/x/silverbullet@0.7.3/plug-api/lib/settings_page.ts -var SETTINGS_PAGE = "SETTINGS"; -async function readSetting(key, defaultValue) { - try { - const allSettings = await readYamlPage(SETTINGS_PAGE, ["yaml"]) || {}; - const val = allSettings[key]; - return val === void 0 ? defaultValue : val; - } catch (e) { - if (e.message === "Not found") { - return defaultValue; - } - throw e; - } -} - -// ../../../../../../Users/justyns/dev/silverbullet-ai/src/init.ts -var apiKey; -var aiSettings; -var chatSystemPrompt; -async function initializeOpenAI() { - const newApiKey = await readSecret("OPENAI_API_KEY"); - if (newApiKey !== apiKey) { - apiKey = newApiKey; - console.log("silverbullet-ai API key updated"); - } - if (!apiKey) { - const errorMessage = "OpenAI API key is missing. Please set it in the secrets page."; - await editor_exports.flashNotification(errorMessage, "error"); - throw new Error(errorMessage); - } - const defaultSettings = { - // TODO: These aren't used yet - // summarizePrompt: - // "Summarize this note. Use markdown for any formatting. The note name is ${noteName}", - // tagPrompt: - // 'You are an AI tagging assistant. Given the note titled "${noteName}" with the content below, please provide a short list of tags, separated by spaces. Only return tags and no other content. Tags must be one word only and lowercase.', - // imagePrompt: - // "Please rewrite the following prompt for better image generation:", - // temperature: 0.5, - // maxTokens: 1000, - defaultTextModel: "gpt-3.5-turbo", - openAIBaseUrl: "https://api.openai.com/v1", - dallEBaseUrl: "https://api.openai.com/v1", - requireAuth: true, - chat: {} - }; - const newSettings = await readSetting("ai", {}); - const newCombinedSettings = { ...defaultSettings, ...newSettings }; - if (JSON.stringify(aiSettings) !== JSON.stringify(newCombinedSettings)) { - console.log("aiSettings updating from", aiSettings); - aiSettings = newCombinedSettings; - console.log("aiSettings updated to", aiSettings); - } else { - console.log("aiSettings unchanged", aiSettings); - } - chatSystemPrompt = { - role: "system", - content: `This is an interactive chat session with a user in a markdown-based note-taking tool called SilverBullet.` - }; - if (aiSettings.chat.userInformation) { - chatSystemPrompt.content += ` -The user has provided the following information about their self: ${aiSettings.chat.userInformation}`; - } - if (aiSettings.chat.userInstructions) { - chatSystemPrompt.content += ` -The user has provided the following instructions for the chat, follow them as closely as possible: ${aiSettings.chat.userInstructions}`; - } -} - -// ../../../../../../Users/justyns/Library/Caches/deno/deno_esbuild/sse.js@2.2.0/node_modules/sse.js/lib/sse.js -var SSE = function(url, options) { - if (!(this instanceof SSE)) { - return new SSE(url, options); - } - this.INITIALIZING = -1; - this.CONNECTING = 0; - this.OPEN = 1; - this.CLOSED = 2; - this.url = url; - options = options || {}; - this.headers = options.headers || {}; - this.payload = options.payload !== void 0 ? options.payload : ""; - this.method = options.method || (this.payload && "POST" || "GET"); - this.withCredentials = !!options.withCredentials; - this.debug = !!options.debug; - this.FIELD_SEPARATOR = ":"; - this.listeners = {}; - this.xhr = null; - this.readyState = this.INITIALIZING; - this.progress = 0; - this.chunk = ""; - this.addEventListener = function(type, listener) { - if (this.listeners[type] === void 0) { - this.listeners[type] = []; - } - if (this.listeners[type].indexOf(listener) === -1) { - this.listeners[type].push(listener); - } - }; - this.removeEventListener = function(type, listener) { - if (this.listeners[type] === void 0) { - return; - } - var filtered = []; - this.listeners[type].forEach(function(element) { - if (element !== listener) { - filtered.push(element); - } - }); - if (filtered.length === 0) { - delete this.listeners[type]; - } else { - this.listeners[type] = filtered; - } - }; - this.dispatchEvent = function(e) { - if (!e) { - return true; - } - if (this.debug) { - console.debug(e); - } - e.source = this; - var onHandler = "on" + e.type; - if (this.hasOwnProperty(onHandler)) { - this[onHandler].call(this, e); - if (e.defaultPrevented) { - return false; - } - } - if (this.listeners[e.type]) { - return this.listeners[e.type].every(function(callback) { - callback(e); - return !e.defaultPrevented; - }); - } - return true; - }; - this._setReadyState = function(state) { - var event = new CustomEvent("readystatechange"); - event.readyState = state; - this.readyState = state; - this.dispatchEvent(event); - }; - this._onStreamFailure = function(e) { - var event = new CustomEvent("error"); - event.data = e.currentTarget.response; - this.dispatchEvent(event); - this.close(); - }; - this._onStreamAbort = function(e) { - this.dispatchEvent(new CustomEvent("abort")); - this.close(); - }; - this._onStreamProgress = function(e) { - if (!this.xhr) { - return; - } - if (this.xhr.status !== 200) { - this._onStreamFailure(e); - return; - } - if (this.readyState == this.CONNECTING) { - this.dispatchEvent(new CustomEvent("open")); - this._setReadyState(this.OPEN); - } - var data = this.xhr.responseText.substring(this.progress); - this.progress += data.length; - var parts = (this.chunk + data).split(/(\r\n\r\n|\r\r|\n\n)/g); - var lastPart = parts.pop(); - parts.forEach(function(part) { - if (part.trim().length > 0) { - this.dispatchEvent(this._parseEventChunk(part)); - } - }.bind(this)); - this.chunk = lastPart; - }; - this._onStreamLoaded = function(e) { - this._onStreamProgress(e); - this.dispatchEvent(this._parseEventChunk(this.chunk)); - this.chunk = ""; - }; - this._parseEventChunk = function(chunk) { - if (!chunk || chunk.length === 0) { - return null; - } - if (this.debug) { - console.debug(chunk); - } - var e = { "id": null, "retry": null, "data": null, "event": null }; - chunk.split(/\n|\r\n|\r/).forEach(function(line) { - var index = line.indexOf(this.FIELD_SEPARATOR); - var field, value; - if (index > 0) { - var skip = line[index + 1] === " " ? 2 : 1; - field = line.substring(0, index); - value = line.substring(index + skip); - } else if (index < 0) { - field = line; - value = ""; - } else { - return; - } - if (!(field in e)) { - return; - } - if (field === "data" && e[field] !== null) { - e["data"] += "\n" + value; - } else { - e[field] = value; - } - }.bind(this)); - var event = new CustomEvent(e.event || "message"); - event.data = e.data || ""; - event.id = e.id; - return event; - }; - this._checkStreamClosed = function() { - if (!this.xhr) { - return; - } - if (this.xhr.readyState === XMLHttpRequest.DONE) { - this._setReadyState(this.CLOSED); - } - }; - this.stream = function() { - if (this.xhr) { - return; - } - this._setReadyState(this.CONNECTING); - this.xhr = new XMLHttpRequest(); - this.xhr.addEventListener("progress", this._onStreamProgress.bind(this)); - this.xhr.addEventListener("load", this._onStreamLoaded.bind(this)); - this.xhr.addEventListener("readystatechange", this._checkStreamClosed.bind(this)); - this.xhr.addEventListener("error", this._onStreamFailure.bind(this)); - this.xhr.addEventListener("abort", this._onStreamAbort.bind(this)); - this.xhr.open(this.method, this.url); - for (var header in this.headers) { - this.xhr.setRequestHeader(header, this.headers[header]); - } - this.xhr.withCredentials = this.withCredentials; - this.xhr.send(this.payload); - }; - this.close = function() { - if (this.readyState === this.CLOSED) { - return; - } - this.xhr.abort(); - this.xhr = null; - this._setReadyState(this.CLOSED); - }; - if (options.start === void 0 || options.start) { - this.stream(); - } -}; -if (typeof exports !== "undefined") { - exports.SSE = SSE; -} - -// ../../../../../../Users/justyns/dev/silverbullet-ai/src/openai.ts -async function streamChatWithOpenAI({ - messages, - cursorStart = void 0, - cursorFollow = false, - scrollIntoView = true, - includeChatSystemPrompt = false -}) { - try { - if (!apiKey) - await initializeOpenAI(); - const sseUrl = `${aiSettings.openAIBaseUrl}/chat/completions`; - const payloadMessages = []; - if (includeChatSystemPrompt) { - payloadMessages.push(chatSystemPrompt); - } - if ("systemMessage" in messages && "userMessage" in messages) { - payloadMessages.push( - { role: "system", content: messages.systemMessage }, - { role: "user", content: messages.userMessage } - ); - } else { - payloadMessages.push(...messages); - } - const headers = { - "Content-Type": "application/json" - }; - if (aiSettings.requireAuth) { - headers["Authorization"] = `Bearer ${apiKey}`; - } - const sseOptions = { - method: "POST", - headers, - payload: JSON.stringify({ - model: aiSettings.defaultTextModel, - stream: true, - messages: payloadMessages - }), - withCredentials: false - }; - const source = new SSE(sseUrl, sseOptions); - let cursorPos; - if (!cursorStart) { - cursorPos = await getPageLength(); - } else { - cursorPos = cursorStart; - } - let loadingMsg = ` \u{1F914} Thinking \u2026\u2026 `; - await editor_exports.insertAtPos(loadingMsg, cursorPos); - let stillLoading = true; - const updateLoadingSpinner = async () => { - while (stillLoading) { - const replaceTo = cursorPos + loadingMsg.length; - currentStateIndex = (currentStateIndex + 1) % spinnerStates.length; - loadingMsg = ` \u{1F914} Thinking ${spinnerStates[currentStateIndex]} \u2026`; - await editor_exports.replaceRange(cursorPos, replaceTo, loadingMsg); - await new Promise((resolve) => setTimeout(resolve, 250)); - } - }; - source.addEventListener("message", function(e) { - try { - if (e.data == "[DONE]") { - source.close(); - stillLoading = false; - } else { - const data = JSON.parse(e.data); - const msg = data.choices[0]?.delta?.content || ""; - if (stillLoading) { - stillLoading = false; - editor_exports.replaceRange(cursorPos, cursorPos + loadingMsg.length, msg); - } else { - editor_exports.insertAtPos(msg, cursorPos); - } - cursorPos += msg.length; - } - if (cursorFollow) { - editor_exports.moveCursor(cursorPos, true); - } - if (scrollIntoView) { - } - } catch (error) { - console.error("Error processing message event:", error, e.data); - } - }); - source.addEventListener("end", function() { - source.close(); - }); - source.stream(); - } catch (error) { - console.error("Error streaming from OpenAI chat endpoint:", error); - await editor_exports.flashNotification( - "Error streaming from OpenAI chat endpoint.", - "error" - ); - throw error; - } -} -async function chatWithOpenAI(systemMessage, userMessages) { - try { - if (!apiKey) - await initializeOpenAI(); - if (!apiKey || !aiSettings || !aiSettings.openAIBaseUrl) { - await editor_exports.flashNotification( - "API key or AI settings are not properly configured.", - "error" - ); - throw new Error("API key or AI settings are not properly configured."); - } - const body = JSON.stringify({ - model: aiSettings.defaultTextModel, - messages: [ - { role: "system", content: systemMessage }, - ...userMessages - ] - }); - console.log("Sending body", body); - const headers = { - "Authorization": `Bearer ${apiKey}`, - "Content-Type": "application/json" - }; - console.log("Request headers:", headers); - const response = await nativeFetch( - aiSettings.openAIBaseUrl + "/chat/completions", - { - method: "POST", - headers, - body - } - ); - if (!response.ok) { - console.error("http response: ", response); - console.error("http response body: ", await response.json()); - throw new Error(`HTTP error, status: ${response.status}`); - } - const data = await response.json(); - if (!data || !data.choices || data.choices.length === 0) { - throw new Error("Invalid response from OpenAI."); - } - return data; - } catch (error) { - console.error("Error calling OpenAI chat endpoint:", error); - await editor_exports.flashNotification( - "Error calling OpenAI chat endpoint.", - "error" - ); - throw error; - } -} -async function generateImageWithDallE(prompt2, n, size = "1024x1024", quality = "hd") { - try { - if (!apiKey) - await initializeOpenAI(); - await editor_exports.flashNotification("Contacting DALL\xB7E, please wait..."); - const response = await nativeFetch( - aiSettings.dallEBaseUrl + "/images/generations", - { - method: "POST", - headers: { - "Authorization": `Bearer ${apiKey}`, - "Content-Type": "application/json" - }, - body: JSON.stringify({ - model: "dall-e-3", - prompt: prompt2, - quality, - n, - size, - response_format: "b64_json" - }) - } - ); - if (!response.ok) { - throw new Error(`HTTP error, status: ${response.status}`); - } - const data = await response.json(); - return data; - } catch (error) { - console.error("Error calling DALL\xB7E image generation endpoint:", error); - throw error; - } -} - -// ../../../../../../Users/justyns/dev/silverbullet-ai/src/utils.ts -function folderName(path) { - return path.split("/").slice(0, -1).join("/"); -} -async function convertPageToMessages() { - const pageText = await editor_exports.getText(); - const lines = pageText.split("\n"); - const messages = []; - let currentRole = "user"; - let contentBuffer = ""; - lines.forEach((line) => { - const match = line.match(/^\*\*(\w+)\*\*:/); - if (match) { - const newRole = match[1].toLowerCase(); - if (currentRole && currentRole !== newRole) { - messages.push( - { role: currentRole, content: contentBuffer.trim() } - ); - contentBuffer = ""; - } - currentRole = newRole; - contentBuffer += line.replace(/^\*\*(\w+)\*\*:/, "").trim() + "\n"; - } else if (currentRole) { - contentBuffer += line.trim() + "\n"; - } - }); - if (contentBuffer && currentRole) { - messages.push( - { role: currentRole, content: contentBuffer.trim() } - ); - } - return messages; -} - -// ../../../../../../Users/justyns/dev/silverbullet-ai/sbai.ts -async function reloadConfig(pageName) { - if (pageName === "SETTINGS" || pageName === "SECRETS") { - await initializeOpenAI(); - } -} -async function summarizeNote() { - const selectedTextInfo = await getSelectedTextOrNote(); - console.log("selectedTextInfo", selectedTextInfo); - if (selectedTextInfo.text.length > 0) { - const noteName = await editor_exports.getCurrentPage(); - const response = await chatWithOpenAI( - "You are an AI Note assistant here to help summarize the user's personal notes.", - [{ - role: "user", - content: `Please summarize this note using markdown for any formatting. Your summary will be appended to the end of this note, do not include any of the note contents yourself. Keep the summary brief. The note name is ${noteName}. - -${selectedTextInfo.text}` - }] - ); - console.log("OpenAI response:", response); - return { - summary: response.choices[0].message.content, - selectedTextInfo - }; - } - return { summary: "", selectedTextInfo: null }; -} -async function callOpenAIwithNote() { - const selectedTextInfo = await getSelectedTextOrNote(); - const userPrompt = await editor_exports.prompt( - "Please enter a prompt to send to the LLM. Selected text or the entire note will also be sent as context." - ); - const noteName = await editor_exports.getCurrentPage(); - const currentDate = /* @__PURE__ */ new Date(); - const dateString = currentDate.toISOString().split("T")[0]; - const dayString = currentDate.toLocaleDateString("en-US", { - weekday: "long" - }); - await streamChatWithOpenAI({ - messages: { - systemMessage: "You are an AI note assistant. Follow all user instructions and use the note context and note content to help follow those instructions. Use Markdown for any formatting.", - userMessage: `Note Context: Today is ${dayString}, ${dateString}. The current note name is "${noteName}". -User Prompt: ${userPrompt} +var ye=Object.defineProperty;var P=(e,t)=>{for(var r in t)ye(e,r,{get:t[r],enumerable:!0})};var L=typeof window>"u"&&typeof globalThis.WebSocketPair>"u";typeof Deno>"u"&&(self.Deno={args:[],build:{arch:"x86_64"},env:{get(){}}});var K=new Map,U=0;function E(e){self.postMessage(e)}L&&(globalThis.syscall=async(e,...t)=>await new Promise((r,n)=>{U++,K.set(U,{resolve:r,reject:n}),E({type:"sys",id:U,name:e,args:t})}));function W(e,t){L&&(self.addEventListener("message",r=>{(async()=>{let n=r.data;switch(n.type){case"inv":{let o=e[n.name];if(!o)throw new Error(`Function not loaded: ${n.name}`);try{let s=await Promise.resolve(o(...n.args||[]));E({type:"invr",id:n.id,result:s})}catch(s){console.error("An exception was thrown as a result of invoking function",n.name,"error:",s.message),E({type:"invr",id:n.id,error:s.message})}}break;case"sysr":{let o=n.id,s=K.get(o);if(!s)throw Error("Invalid request id");K.delete(o),n.error?s.reject(new Error(n.error)):s.resolve(n.result)}break}})().catch(console.error)}),E({type:"manifest",manifest:t}))}function xe(e){let t=atob(e),r=t.length,n=new Uint8Array(r);for(let o=0;o0?_(r):void 0;t={method:e.method,headers:Object.fromEntries(e.headers.entries()),base64Body:n},e=e.url}return syscall("sandboxFetch.fetch",e,t)}globalThis.nativeFetch=globalThis.fetch;function we(){globalThis.fetch=async function(e,t){let r=t&&t.body?_(new Uint8Array(await new Response(t.body).arrayBuffer())):void 0,n=await Pe(e,t&&{method:t.method,headers:t.headers,base64Body:r});return new Response(n.base64Body?xe(n.base64Body):null,{status:n.status,headers:n.headers})}}L&&we();function R(e){if(e.children)for(let t of e.children){if(t.parent)return;t.parent=e,R(t)}}function D(e,t){if(t(e))return[e];let r=[];if(e.children)for(let n of e.children)r=[...r,...D(n,t)];return r}async function G(e,t){if(await t(e))return[e];let r=[];if(e.children)for(let n of e.children)r=[...r,...await G(n,t)];return r}async function j(e,t){if(e.children){let r=e.children.slice();for(let n of r){let o=await t(n);if(o!==void 0){let s=e.children.indexOf(n);o?e.children.splice(s,1,o):e.children.splice(s,1)}else await j(n,t)}}}function B(e,t){return D(e,r=>r.type===t)[0]}function V(e,t){D(e,t)}async function Y(e,t){await G(e,t)}function w(e){if(!e)return"";let t=[];if(e.text!==void 0)return e.text;for(let r of e.children)t.push(w(r));return t.join("")}function I(e){if(!e||typeof e!="object")return e;if(Array.isArray(e))return e.map(I);let t={};for(let r of Object.keys(e)){let n=r.split("."),o=t;for(let s=0;sHe,dispatch:()=>Ye,downloadFile:()=>Re,filterBox:()=>Be,flashNotification:()=>je,fold:()=>Ze,foldAll:()=>rt,getCurrentPage:()=>Ae,getCursor:()=>Se,getSelection:()=>Ce,getText:()=>Te,getUiOption:()=>Qe,goHistory:()=>Le,hidePanel:()=>$e,insertAtCursor:()=>Ve,insertAtPos:()=>We,moveCursor:()=>Ge,navigate:()=>Oe,openCommandPalette:()=>Me,openPageNavigator:()=>Ne,openSearchPanel:()=>ot,openUrl:()=>Ke,prompt:()=>ze,reloadPage:()=>Fe,reloadSettingsAndCommands:()=>Ue,reloadUI:()=>ke,replaceRange:()=>_e,save:()=>Ie,setPage:()=>ve,setSelection:()=>Ee,setText:()=>be,setUiOption:()=>Je,showPanel:()=>qe,toggleFold:()=>tt,unfold:()=>et,unfoldAll:()=>nt,uploadFile:()=>De,vimEx:()=>Xe});typeof self>"u"&&(self={syscall:()=>{throw new Error("Not implemented here")}});var i=globalThis.syscall;function Ae(){return i("editor.getCurrentPage")}function ve(e){return i("editor.setPage",e)}function Te(){return i("editor.getText")}function be(e){return i("editor.setText",e)}function Se(){return i("editor.getCursor")}function Ce(){return i("editor.getSelection")}function Ee(e,t){return i("editor.setSelection",e,t)}function Ie(){return i("editor.save")}function Oe(e,t=!1,r=!1){return i("editor.navigate",e,t,r)}function Ne(e="page"){return i("editor.openPageNavigator",e)}function Me(){return i("editor.openCommandPalette")}function Fe(){return i("editor.reloadPage")}function ke(){return i("editor.reloadUI")}function Ue(){return i("editor.reloadSettingsAndCommands")}function Ke(e,t=!1){return i("editor.openUrl",e,t)}function Le(e){return i("editor.goHistory",e)}function Re(e,t){return i("editor.downloadFile",e,t)}function De(e,t){return i("editor.uploadFile",e,t)}function je(e,t="info"){return i("editor.flashNotification",e,t)}function Be(e,t,r="",n=""){return i("editor.filterBox",e,t,r,n)}function qe(e,t,r,n=""){return i("editor.showPanel",e,t,r,n)}function $e(e){return i("editor.hidePanel",e)}function We(e,t){return i("editor.insertAtPos",e,t)}function _e(e,t,r){return i("editor.replaceRange",e,t,r)}function Ge(e,t=!1){return i("editor.moveCursor",e,t)}function Ve(e){return i("editor.insertAtCursor",e)}function Ye(e){return i("editor.dispatch",e)}function ze(e,t=""){return i("editor.prompt",e,t)}function He(e){return i("editor.confirm",e)}function Qe(e){return i("editor.getUiOption",e)}function Je(e,t){return i("editor.setUiOption",e,t)}function Xe(e){return i("editor.vimEx",e)}function Ze(){return i("editor.fold")}function et(){return i("editor.unfold")}function tt(){return i("editor.toggleFold")}function rt(){return i("editor.foldAll")}function nt(){return i("editor.unfoldAll")}function ot(){return i("editor.openSearchPanel")}var h={};P(h,{parseMarkdown:()=>st});function st(e){return i("markdown.parseMarkdown",e)}var f={};P(f,{deleteAttachment:()=>ht,deleteFile:()=>At,deletePage:()=>mt,getAttachmentMeta:()=>dt,getFileMeta:()=>Pt,getPageMeta:()=>at,listAttachments:()=>ut,listFiles:()=>yt,listPages:()=>it,listPlugs:()=>pt,readAttachment:()=>ft,readFile:()=>xt,readPage:()=>ct,writeAttachment:()=>gt,writeFile:()=>wt,writePage:()=>lt});function it(e=!1){return i("space.listPages",e)}function at(e){return i("space.getPageMeta",e)}function ct(e){return i("space.readPage",e)}function lt(e,t){return i("space.writePage",e,t)}function mt(e){return i("space.deletePage",e)}function pt(){return i("space.listPlugs")}function ut(){return i("space.listAttachments")}function dt(e){return i("space.getAttachmentMeta",e)}function ft(e){return i("space.readAttachment",e)}function gt(e,t){return i("space.writeAttachment",e,t)}function ht(e){return i("space.deleteAttachment",e)}function yt(){return i("space.listFiles")}function xt(e){return i("space.readFile",e)}function Pt(e){return i("space.getFileMeta",e)}function wt(e,t){return i("space.writeFile",e,t)}function At(e){return i("space.deleteFile",e)}var O={};P(O,{getEnv:()=>Et,getMode:()=>It,getVersion:()=>Ot,invokeCommand:()=>Tt,invokeFunction:()=>vt,listCommands:()=>bt,listSyscalls:()=>St,reloadPlugs:()=>Ct});function vt(e,...t){return i("system.invokeFunction",e,...t)}function Tt(e,t){return i("system.invokeCommand",e,t)}function bt(){return i("system.listCommands")}function St(){return i("system.listSyscalls")}function Ct(){i("system.reloadPlugs")}function Et(){return i("system.getEnv")}function It(){return i("system.getMode")}function Ot(){return i("system.getVersion")}var T={};P(T,{parseTemplate:()=>Kt,renderTemplate:()=>Ut});function Ut(e,t,r={}){return i("template.renderTemplate",e,t,r)}function Kt(e){return i("template.parseTemplate",e)}var d={};P(d,{parse:()=>Bt,stringify:()=>qt});function Bt(e){return i("yaml.parse",e)}function qt(e){return i("yaml.stringify",e)}async function A(e,t={}){let r={tags:[]},n=[];return R(e),await j(e,async o=>{if(o.type==="Paragraph"&&o.parent?.type==="Document"){let s=!0,c=new Set;for(let l of o.children)if(l.text){if(l.text.startsWith(` +`)&&l.text!==` +`)break;if(l.text.trim()){s=!1;break}}else if(l.type==="Hashtag"){let m=l.children[0].text.substring(1);c.add(m),(t.removeTags===!0||t.removeTags?.includes(m))&&(l.children[0].text="")}else if(l.type){s=!1;break}s&&n.push(...c)}if(o.type==="FrontMatter"){let s=o.children[1].children[0],c=w(s);try{let l=await d.parse(c),m={...l};if(r={...r,...l},r.tags||(r.tags=[]),typeof r.tags=="string"&&n.push(...r.tags.split(/,\s*|\s+/)),Array.isArray(r.tags)&&n.push(...r.tags),t.removeKeys&&t.removeKeys.length>0){let p=!1;for(let g of t.removeKeys)g in m&&(delete m[g],p=!0);p&&(s.text=await d.stringify(m))}if(Object.keys(m).length===0||t.removeFrontmatterSection)return null}catch(l){console.warn("Could not parse frontmatter",l.message)}}}),r.tags=[...new Set([...n.map(o=>o.replace(/^#/,""))])],r=I(r),r}async function z(e,t){let r=null;if(await Y(e,async n=>{if(n.type==="FrontMatter"){let o=n.children[1].children[0],s=w(o);try{let c="";if(typeof t=="string")c=s+t+` +`;else{let m={...await d.parse(s),...t};c=await d.stringify(m)}r={changes:{from:o.from,to:o.to,insert:c}}}catch(c){console.error("Error parsing YAML",c)}return!0}return!1}),!r){let n="";typeof t=="string"?n=t+` +`:n=await d.stringify(t),r={changes:{from:0,to:0,insert:`--- +`+n+`--- +`}}}return r}var br=new TextEncoder;function H(e){let t=atob(e),r=t.length,n=new Uint8Array(r);for(let o=0;o{if(s.type!=="FencedCode")return!1;let c=B(s,"CodeInfo");if(t&&!c||t&&!t.includes(c.children[0].text))return!1;let l=B(s,"CodeText");return l?(o=l.children[0].text,!0):!1}),o}async function N(e,t=["yaml"]){let r=await Gt(e,t);if(r!==void 0)try{return d.parse(r)}catch(n){throw console.error("YAML Page parser error",n),new Error(`YAML Error: ${n.message}`)}}async function Q(e){try{let r=(await N("SECRETS",["yaml","secrets"]))[e];if(r===void 0)throw new Error(`No such secret: ${e}`);return r}catch(t){throw t.message==="Not found"?new Error(`No such secret: ${e}`):t}}var Vt="SETTINGS";async function J(e,t){try{let n=(await N(Vt,["yaml"])||{})[e];return n===void 0?t:n}catch(r){if(r.message==="Not found")return t;throw r}}var b=function(e,t){if(!(this instanceof b))return new b(e,t);this.INITIALIZING=-1,this.CONNECTING=0,this.OPEN=1,this.CLOSED=2,this.url=e,t=t||{},this.headers=t.headers||{},this.payload=t.payload!==void 0?t.payload:"",this.method=t.method||this.payload&&"POST"||"GET",this.withCredentials=!!t.withCredentials,this.debug=!!t.debug,this.FIELD_SEPARATOR=":",this.listeners={},this.xhr=null,this.readyState=this.INITIALIZING,this.progress=0,this.chunk="",this.addEventListener=function(r,n){this.listeners[r]===void 0&&(this.listeners[r]=[]),this.listeners[r].indexOf(n)===-1&&this.listeners[r].push(n)},this.removeEventListener=function(r,n){if(this.listeners[r]!==void 0){var o=[];this.listeners[r].forEach(function(s){s!==n&&o.push(s)}),o.length===0?delete this.listeners[r]:this.listeners[r]=o}},this.dispatchEvent=function(r){if(!r)return!0;this.debug&&console.debug(r),r.source=this;var n="on"+r.type;return this.hasOwnProperty(n)&&(this[n].call(this,r),r.defaultPrevented)?!1:this.listeners[r.type]?this.listeners[r.type].every(function(o){return o(r),!r.defaultPrevented}):!0},this._setReadyState=function(r){var n=new CustomEvent("readystatechange");n.readyState=r,this.readyState=r,this.dispatchEvent(n)},this._onStreamFailure=function(r){var n=new CustomEvent("error");n.data=r.currentTarget.response,this.dispatchEvent(n),this.close()},this._onStreamAbort=function(r){this.dispatchEvent(new CustomEvent("abort")),this.close()},this._onStreamProgress=function(r){if(this.xhr){if(this.xhr.status!==200){this._onStreamFailure(r);return}this.readyState==this.CONNECTING&&(this.dispatchEvent(new CustomEvent("open")),this._setReadyState(this.OPEN));var n=this.xhr.responseText.substring(this.progress);this.progress+=n.length;var o=(this.chunk+n).split(/(\r\n\r\n|\r\r|\n\n)/g),s=o.pop();o.forEach(function(c){c.trim().length>0&&this.dispatchEvent(this._parseEventChunk(c))}.bind(this)),this.chunk=s}},this._onStreamLoaded=function(r){this._onStreamProgress(r),this.dispatchEvent(this._parseEventChunk(this.chunk)),this.chunk=""},this._parseEventChunk=function(r){if(!r||r.length===0)return null;this.debug&&console.debug(r);var n={id:null,retry:null,data:null,event:null};r.split(/\n|\r\n|\r/).forEach(function(s){var c=s.indexOf(this.FIELD_SEPARATOR),l,m;if(c>0){var p=s[c+1]===" "?2:1;l=s.substring(0,c),m=s.substring(c+p)}else if(c<0)l=s,m="";else return;l in n&&(l==="data"&&n[l]!==null?n.data+=` +`+m:n[l]=m)}.bind(this));var o=new CustomEvent(n.event||"message");return o.data=n.data||"",o.id=n.id,o},this._checkStreamClosed=function(){this.xhr&&this.xhr.readyState===XMLHttpRequest.DONE&&this._setReadyState(this.CLOSED)},this.stream=function(){if(!this.xhr){this._setReadyState(this.CONNECTING),this.xhr=new XMLHttpRequest,this.xhr.addEventListener("progress",this._onStreamProgress.bind(this)),this.xhr.addEventListener("load",this._onStreamLoaded.bind(this)),this.xhr.addEventListener("readystatechange",this._checkStreamClosed.bind(this)),this.xhr.addEventListener("error",this._onStreamFailure.bind(this)),this.xhr.addEventListener("abort",this._onStreamAbort.bind(this)),this.xhr.open(this.method,this.url);for(var r in this.headers)this.xhr.setRequestHeader(r,this.headers[r]);this.xhr.withCredentials=this.withCredentials,this.xhr.send(this.payload)}},this.close=function(){this.readyState!==this.CLOSED&&(this.xhr.abort(),this.xhr=null,this._setReadyState(this.CLOSED))},(t.start===void 0||t.start)&&this.stream()};typeof exports<"u"&&(exports.SSE=b);var M=class{name;apiKey;baseUrl;modelName;constructor(t,r,n,o){this.name=t,this.apiKey=r,this.baseUrl=n,this.modelName=o}async streamChatIntoEditor(t,r){let{onDataReceived:n}=t,o="\u{1F914} Thinking \u2026 ",s=r??await v();await a.insertAtPos(o,s);let c=!0,l=m=>{c?(a.replaceRange(s,s+o.length,m),c=!1):a.insertAtPos(m,s),s+=m.length,n&&n(m)};await this.chatWithAI({...t,onDataReceived:l})}};var F=class extends M{name="OpenAI";requireAuth;constructor(t,r,n,o){super("OpenAI",t,n,r),this.requireAuth=o}async chatWithAI({messages:t,stream:r,onDataReceived:n}){return r?await this.streamChat({messages:t,onDataReceived:n}):await this.nonStreamingChat(t)}async streamChat(t){let{messages:r,onDataReceived:n}=t;try{let o=`${this.baseUrl}/chat/completions`,s={"Content-Type":"application/json"};this.requireAuth&&(s.Authorization=`Bearer ${this.apiKey}`);let c={method:"POST",headers:s,payload:JSON.stringify({model:this.modelName,stream:!0,messages:r}),withCredentials:!1},l=new b(o,c),m="";l.addEventListener("message",function(p){try{if(p.data=="[DONE]")return l.close(),m;{let $=JSON.parse(p.data).choices[0]?.delta?.content||"";m+=$,n&&n($)}}catch(g){console.error("Error processing message event:",g,p.data)}}),l.addEventListener("end",function(){return l.close(),m}),l.stream()}catch(o){throw console.error("Error streaming from OpenAI chat endpoint:",o),await a.flashNotification("Error streaming from OpenAI chat endpoint.","error"),o}return""}async nonStreamingChat(t){try{let r=JSON.stringify({model:this.modelName,messages:t}),n={Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json"},o=await nativeFetch(this.baseUrl+"/chat/completions",{method:"POST",headers:n,body:r});if(!o.ok)throw console.error("http response: ",o),console.error("http response body: ",await o.json()),new Error(`HTTP error, status: ${o.status}`);let s=await o.json();if(!s||!s.choices||s.choices.length===0)throw new Error("Invalid response from OpenAI.");return s}catch(r){throw console.error("Error calling OpenAI chat endpoint:",r),await a.flashNotification("Error calling OpenAI chat endpoint.","error"),r}}};async function X(e,t,r="1024x1024",n="hd"){try{x||await S(),await a.flashNotification("Contacting DALL\xB7E, please wait...");let o=await nativeFetch(u.dallEBaseUrl+"/images/generations",{method:"POST",headers:{Authorization:`Bearer ${x}`,"Content-Type":"application/json"},body:JSON.stringify({model:"dall-e-3",prompt:e,quality:n,n:t,size:r,response_format:"b64_json"})});if(!o.ok)throw new Error(`HTTP error, status: ${o.status}`);return await o.json()}catch(o){throw console.error("Error calling DALL\xB7E image generation endpoint:",o),o}}var x,u,C,y;async function Z(){(!x||!y||!u)&&await S()}async function S(){let e=await Q("OPENAI_API_KEY");if(e!==x&&(x=e,console.log("silverbullet-ai API key updated")),!x){let o="OpenAI API key is missing. Please set it in the secrets page.";throw await a.flashNotification(o,"error"),new Error(o)}let t={defaultTextModel:"gpt-3.5-turbo",openAIBaseUrl:"https://api.openai.com/v1",dallEBaseUrl:"https://api.openai.com/v1",requireAuth:!0,chat:{}},r=await J("ai",{}),n={...t,...r};JSON.stringify(u)!==JSON.stringify(n)?(console.log("aiSettings updating from",u),u=n,console.log("aiSettings updated to",u)):console.log("aiSettings unchanged",u),y=new F(x,u.defaultTextModel,u.openAIBaseUrl,u.requireAuth),C={role:"system",content:"This is an interactive chat session with a user in a markdown-based note-taking tool called SilverBullet."},u.chat.userInformation&&(C.content+=` +The user has provided the following information about their self: ${u.chat.userInformation}`),u.chat.userInstructions&&(C.content+=` +The user has provided the following instructions for the chat, follow them as closely as possible: ${u.chat.userInstructions}`)}function ee(e){return e.split("/").slice(0,-1).join("/")}async function te(){let t=(await a.getText()).split(` +`),r=[],n="user",o="";return t.forEach(s=>{let c=s.match(/^\*\*(\w+)\*\*:/);if(c){let l=c[1].toLowerCase();n&&n!==l&&(r.push({role:n,content:o.trim()}),o=""),n=l,o+=s.replace(/^\*\*(\w+)\*\*:/,"").trim()+` +`}else n&&(o+=s.trim()+` +`)}),o&&n&&r.push({role:n,content:o.trim()}),r}async function re(e){(e==="SETTINGS"||e==="SECRETS")&&await S()}async function ne(){let e=await q(),t=await a.prompt("Please enter a prompt to send to the LLM. Selected text or the entire note will also be sent as context."),r=await a.getCurrentPage(),n=new Date,o=n.toISOString().split("T")[0],s=n.toLocaleDateString("en-US",{weekday:"long"});await y.streamChatIntoEditor({messages:[{role:"system",content:"You are an AI note assistant. Follow all user instructions and use the note context and note content to help follow those instructions. Use Markdown for any formatting."},{role:"user",content:`Note Context: Today is ${s}, ${o}. The current note name is "${r}". +User Prompt: ${t} Note Content: -${selectedTextInfo.text}` - }, - cursorStart: selectedTextInfo.isWholeNote ? void 0 : selectedTextInfo.to - }); -} -async function openSummaryPanel() { - const { summary } = await summarizeNote(); - if (summary) { - await editor_exports.showPanel("rhs", 2, summary); - } else { - await editor_exports.flashNotification("No summary available."); - } -} -async function insertSummary() { - const { summary, selectedTextInfo } = await summarizeNote(); - if (summary && selectedTextInfo) { - await editor_exports.insertAtPos( - "\n\n" + summary, - selectedTextInfo.to - ); - } -} -async function tagNoteWithAI() { - const noteContent = await editor_exports.getText(); - const noteName = await editor_exports.getCurrentPage(); - const response = await chatWithOpenAI( - "You are an AI tagging assistant. Please provide a short list of tags, separated by spaces. Only return tags and no other content. Tags must be one word only and lowercase. Suggest tags sparringly, do not treat them as keywords.", - [{ - role: "user", - content: `Given the note titled "${noteName}" with the content below, please provide tags. - -${noteContent}` - }] - ); - const tags = response.choices[0].message.content.trim().replace(/,/g, "").split(/\s+/); - const tree = await markdown_exports.parseMarkdown(noteContent); - const frontMatter = await extractFrontmatter(tree); - const updatedTags = [.../* @__PURE__ */ new Set([...frontMatter.tags || [], ...tags])]; - frontMatter.tags = updatedTags; - console.log("Current frontmatter:", frontMatter); - const frontMatterChange = await prepareFrontmatterDispatch(tree, frontMatter); - console.log("updatedNoteContent", frontMatterChange); - await editor_exports.dispatch(frontMatterChange); - await editor_exports.flashNotification("Note tagged successfully."); -} -async function streamOpenAIWithSelectionAsPrompt() { - const selectedTextInfo = await getSelectedTextOrNote(); - await streamChatWithOpenAI({ - messages: { - systemMessage: "You are an AI note assistant in a markdown-based note tool.", - userMessage: selectedTextInfo.text - } - }); -} -async function streamChatOnPage() { - const messages = await convertPageToMessages(); - if (messages.length === 0) { - await editor_exports.flashNotification( - "Error: The page does not match the required format for a chat." - ); - return; - } - const currentPageLength = await getPageLength(); - await editor_exports.insertAtPos("\n\n**assistant**: ", currentPageLength); - const newPageLength = currentPageLength + "\n\n**assistant**: ".length; - await editor_exports.insertAtPos("\n\n**user**: ", newPageLength); - await editor_exports.moveCursor(newPageLength + "\n\n**user**: ".length); - await streamChatWithOpenAI({ - messages, - cursorStart: newPageLength, - scrollIntoView: true, - includeChatSystemPrompt: true - }); -} -async function promptAndGenerateImage() { - try { - const prompt2 = await editor_exports.prompt("Enter a prompt for DALL\xB7E:"); - if (!prompt2 || !prompt2.trim()) { - await editor_exports.flashNotification( - "No prompt entered. Operation cancelled.", - "error" - ); - return; - } - const imageData = await generateImageWithDallE(prompt2, 1); - if (imageData && imageData.data && imageData.data.length > 0) { - const base64Image = imageData.data[0].b64_json; - const revisedPrompt = imageData.data[0].revised_prompt; - const decodedImage = new Uint8Array(decodeBase64(base64Image)); - const finalFileName = `dall-e-${Date.now()}.png`; - let prefix = folderName(await editor_exports.getCurrentPage()) + "/"; - if (prefix === "/") { - prefix = ""; - } - await space_exports.writeAttachment(prefix + finalFileName, decodedImage); - const markdownImg = `![${finalFileName}](${finalFileName}) -*${revisedPrompt}*`; - await editor_exports.insertAtCursor(markdownImg); - await editor_exports.flashNotification( - "Image generated and inserted with caption successfully." - ); - } else { - await editor_exports.flashNotification("Failed to generate image.", "error"); - } - } catch (error) { - console.error("Error generating image with DALL\xB7E:", error); - await editor_exports.flashNotification("Error generating image.", "error"); - } -} -async function queryOpenAI(userPrompt, systemPrompt) { - try { - const messages = []; - messages.push({ role: "user", content: userPrompt }); - const defaultSystemPrompt = "You are an AI note assistant helping to render content for a note. Please follow user instructions and keep your response short and concise."; - const response = await chatWithOpenAI( - systemPrompt || defaultSystemPrompt, - messages - ); - return response.choices[0].message.content; - } catch (error) { - console.error("Error querying OpenAI:", error); - throw error; - } -} - -// ../../../../../../Users/justyns/dev/silverbullet/lib/limited_map.ts -var LimitedMap = class { - constructor(maxSize, initialJson = {}) { - this.maxSize = maxSize; - this.map = new Map(Object.entries(initialJson)); - } - map; - /** - * @param key - * @param value - * @param ttl time to live (in ms) - */ - set(key, value, ttl) { - const entry = { value, la: Date.now() }; - if (ttl) { - const existingEntry = this.map.get(key); - if (existingEntry?.expTimer) { - clearTimeout(existingEntry.expTimer); - } - entry.expTimer = setTimeout(() => { - this.map.delete(key); - }, ttl); - } - if (this.map.size >= this.maxSize) { - const oldestKey = this.getOldestKey(); - this.map.delete(oldestKey); - } - this.map.set(key, entry); - } - get(key) { - const entry = this.map.get(key); - if (entry) { - entry.la = Date.now(); - return entry.value; - } - return void 0; - } - remove(key) { - this.map.delete(key); - } - toJSON() { - return Object.fromEntries(this.map.entries()); - } - getOldestKey() { - let oldestKey; - let oldestTimestamp; - for (const [key, entry] of this.map.entries()) { - if (!oldestTimestamp || entry.la < oldestTimestamp) { - oldestKey = key; - oldestTimestamp = entry.la; - } - } - return oldestKey; - } -}; - -// ../../../../../../Users/justyns/dev/silverbullet/lib/memory_cache.ts -var cache = new LimitedMap(50); -async function ttlCache(key, fn, ttlSecs) { - if (!ttlSecs) { - return fn(key); - } - const serializedKey = JSON.stringify(key); - const cached = cache.get(serializedKey); - if (cached) { - return cached; - } - const result = await fn(key); - cache.set(serializedKey, result, ttlSecs * 1e3); - return result; -} - -// https://deno.land/x/silverbullet@0.7.3/plugs/index/plug_api.ts -function queryObjects(tag, query, ttlSecs) { - return ttlCache( - query, - () => system_exports.invokeFunction("index.queryObjects", tag, query), - ttlSecs - // no-op when undefined - ); -} +${e.text}`}],stream:!0},e.to)}async function oe(){let{summary:e}=await summarizeNote();e?await a.showPanel("rhs",2,e):await a.flashNotification("No summary available.")}async function se(){let e=await a.getText(),t=await a.getCurrentPage(),n=(await y.chatWithAI({messages:[{role:"system",content:"You are an AI tagging assistant. Please provide a short list of tags, separated by spaces. Only return tags and no other content. Tags must be one word only and lowercase. Suggest tags sparingly, do not treat them as keywords."},{role:"user",content:`Given the note titled "${t}" with the content below, please provide tags. -// https://deno.land/x/silverbullet@0.7.3/plugs/template/api.ts -async function renderTemplate2(templateText, data = {}, variables = {}) { - try { - const tree = await markdown_exports.parseMarkdown(templateText); - const frontmatter = await extractFrontmatter( - tree, - { - removeFrontmatterSection: true, - removeTags: ["template"] - } - ); - templateText = renderToText(tree).trimStart(); - let frontmatterText; - if (frontmatter.frontmatter) { - if (typeof frontmatter.frontmatter === "string") { - frontmatterText = frontmatter.frontmatter; - } else { - frontmatterText = await yaml_exports.stringify(frontmatter.frontmatter); - } - frontmatterText = await template_exports.renderTemplate( - frontmatterText, - data, - variables - ); - } - return { - frontmatter, - renderedFrontmatter: frontmatterText, - text: await template_exports.renderTemplate(templateText, data, variables) - }; - } catch (e) { - console.error("Error rendering template", e); - throw e; - } -} +${e}`}],stream:!1})).choices[0].message.content.trim().replace(/,/g,"").split(/\s+/),o=await h.parseMarkdown(e),s=await A(o),c=[...new Set([...s.tags||[],...n])];s.tags=c,console.log("Current frontmatter:",s);let l=await z(o,s);console.log("updatedNoteContent",l),await a.dispatch(l),await a.flashNotification("Note tagged successfully.")}async function ie(){let e=await q(),t=e.to;await y.streamChatIntoEditor({messages:[{role:"system",content:"You are an AI note assistant in a markdown-based note tool."},{role:"user",content:e.text}],stream:!0},t)}async function ae(){await Z();let e=await te();if(e.length===0){await a.flashNotification("Error: The page does not match the required format for a chat.");return}e.unshift(C);let t=await v();await a.insertAtPos(` -// ../../../../../../Users/justyns/dev/silverbullet-ai/src/prompts.ts -async function insertAiPromptFromTemplate(slashCompletion) { - let selectedTemplate; - if (!slashCompletion || !slashCompletion.templatePage) { - const aiPromptTemplates = await queryObjects("template", { - filter: ["attr", ["attr", "aiprompt"], "description"] - }); - selectedTemplate = await editor_exports.filterBox( - "Prompt Template", - aiPromptTemplates.map((templateObj) => { - const niceName = templateObj.ref.split("/").pop(); - return { - ...templateObj, - description: templateObj.aiprompt.description || templateObj.ref, - name: templateObj.aiprompt.displayName || niceName, - systemPrompt: templateObj.aiprompt.systemPrompt || "You are an AI note assistant. Please follow the prompt instructions.", - insertAt: templateObj.aiprompt.insertAt || "cursor" - // parseAs: templateObj.aiprompt.parseAs || "markdown", - }; - }), - `Select the template to use as the prompt. The prompt will be rendered and sent to the LLM model.` - ); - } else { - console.log("selectedTemplate from slash completion: ", slashCompletion); - const templatePage = await space_exports.readPage(slashCompletion.templatePage); - const tree = await markdown_exports.parseMarkdown(templatePage); - const { aiprompt } = await extractFrontmatter(tree); - console.log("templatePage from slash completion: ", templatePage); - selectedTemplate = { - ref: slashCompletion.templatePage, - systemPrompt: aiprompt.systemPrompt || "You are an AI note assistant. Please follow the prompt instructions.", - insertAt: aiprompt.insertAt || "cursor" - }; - } - if (!selectedTemplate) { - await editor_exports.flashNotification("No template selected"); - return; - } - console.log("User selected prompt template: ", selectedTemplate); - const validInsertAtOptions = [ - "cursor", - "page-start", - "page-end" - // "frontmatter", - // "modal", - ]; - if (!validInsertAtOptions.includes(selectedTemplate.insertAt)) { - console.error( - `Invalid insertAt value: ${selectedTemplate.insertAt}. It must be one of ${validInsertAtOptions.join(", ")}` - ); - await editor_exports.flashNotification( - `Invalid insertAt value: ${selectedTemplate.insertAt}. Please select a valid option.`, - "error" - ); - return; - } - const templateText = await space_exports.readPage(selectedTemplate.ref); - const currentPage = await editor_exports.getCurrentPage(); - const pageMeta = await space_exports.getPageMeta(currentPage); - let cursorPos; - switch (selectedTemplate.insertAt) { - case "page-start": - cursorPos = 0; - break; - case "page-end": - cursorPos = await getPageLength(); - break; - case "frontmatter": - await editor_exports.flashNotification( - `rendering in frontmatter not supported yet`, - "error" - ); - break; - case "modal": - break; - case "cursor": - default: - cursorPos = await editor_exports.getCursor(); - } - const renderedTemplate = await renderTemplate2(templateText, pageMeta, { - page: pageMeta - }); - await streamChatWithOpenAI({ - messages: { - systemMessage: selectedTemplate.systemPrompt, - userMessage: renderedTemplate.text - }, - cursorStart: cursorPos - }); -} +**assistant**: `,t),t+=17,await a.insertAtPos(` -// f0aea78a.js -var functionMapping = { - queryOpenAI, - reloadConfig, - summarizeNote: openSummaryPanel, - insertSummary, - callOpenAI: callOpenAIwithNote, - tagNoteWithAI, - promptAndGenerateImage, - streamOpenAIWithSelectionAsPrompt, - streamChatOnPage, - insertAiPromptFromTemplate -}; -var manifest = { - "name": "silverbullet-ai", - "requiredPermissions": [ - "fetch" - ], - "functions": { - "queryOpenAI": { - "path": "sbai.ts:queryOpenAI" - }, - "reloadConfig": { - "path": "sbai.ts:reloadConfig", - "events": [ - "page:saved" - ] - }, - "summarizeNote": { - "path": "sbai.ts:openSummaryPanel", - "command": { - "name": "AI: Summarize Note and open summary" - } - }, - "insertSummary": { - "path": "sbai.ts:insertSummary", - "command": { - "name": "AI: Insert Summary" - } - }, - "callOpenAI": { - "path": "sbai.ts:callOpenAIwithNote", - "command": { - "name": "AI: Call OpenAI with Note as context" - } - }, - "tagNoteWithAI": { - "path": "sbai.ts:tagNoteWithAI", - "command": { - "name": "AI: Generate tags for note" - } - }, - "promptAndGenerateImage": { - "path": "sbai.ts:promptAndGenerateImage", - "command": { - "name": "AI: Generate and insert image using DallE" - } - }, - "streamOpenAIWithSelectionAsPrompt": { - "path": "sbai.ts:streamOpenAIWithSelectionAsPrompt", - "command": { - "name": "AI: Stream response with selection or note as prompt" - } - }, - "streamChatOnPage": { - "path": "sbai.ts:streamChatOnPage", - "command": { - "name": "AI: Chat on current page", - "key": "Ctrl-Shift-Enter", - "mac": "Cmd-Shift-Enter" - } - }, - "insertAiPromptFromTemplate": { - "path": "src/prompts.ts:insertAiPromptFromTemplate", - "command": { - "name": "AI: Execute AI Prompt from Custom Template" - } - } - }, - "assets": {} -}; -var plug = { manifest, functionMapping }; -setupMessageListener(functionMapping, manifest); -export { - plug -}; -//# sourceMappingURL=silverbullet-ai.plug.js.map +**user**: `,t),await a.moveCursor(t+12);try{await y.streamChatIntoEditor({messages:e,stream:!0},t)}catch(r){console.error("Error streaming chat on page:",r),await a.flashNotification("Error streaming chat on page.","error")}}async function ce(){try{let e=await a.prompt("Enter a prompt for DALL\xB7E:");if(!e||!e.trim()){await a.flashNotification("No prompt entered. Operation cancelled.","error");return}let t=await X(e,1);if(t&&t.data&&t.data.length>0){let r=t.data[0].b64_json,n=t.data[0].revised_prompt,o=new Uint8Array(H(r)),s=`dall-e-${Date.now()}.png`,c=ee(await a.getCurrentPage())+"/";c==="/"&&(c=""),await f.writeAttachment(c+s,o);let l=`![${s}](${s}) +*${n}*`;await a.insertAtCursor(l),await a.flashNotification("Image generated and inserted with caption successfully.")}else await a.flashNotification("Failed to generate image.","error")}catch(e){console.error("Error generating image with DALL\xB7E:",e),await a.flashNotification("Error generating image.","error")}}async function le(e,t){try{let r=[];return r.push({role:"system",content:t||"You are an AI note assistant helping to render content for a note. Please follow user instructions and keep your response short and concise."}),r.push({role:"user",content:e}),(await y.chatWithAI({messages:r,stream:!1})).choices[0].message.content}catch(r){throw console.error("Error querying OpenAI:",r),r}}var k=class{constructor(t,r={}){this.maxSize=t;this.map=new Map(Object.entries(r))}map;set(t,r,n){let o={value:r,la:Date.now()};if(n){let s=this.map.get(t);s?.expTimer&&clearTimeout(s.expTimer),o.expTimer=setTimeout(()=>{this.map.delete(t)},n)}if(this.map.size>=this.maxSize){let s=this.getOldestKey();this.map.delete(s)}this.map.set(t,o)}get(t){let r=this.map.get(t);if(r)return r.la=Date.now(),r.value}remove(t){this.map.delete(t)}toJSON(){return Object.fromEntries(this.map.entries())}getOldestKey(){let t,r;for(let[n,o]of this.map.entries())(!r||o.laO.invokeFunction("index.queryObjects",e,t),r)}async function de(e,t={},r={}){try{let n=await h.parseMarkdown(e),o=await A(n,{removeFrontmatterSection:!0,removeTags:["template"]});e=w(n).trimStart();let s;return o.frontmatter&&(typeof o.frontmatter=="string"?s=o.frontmatter:s=await d.stringify(o.frontmatter),s=await T.renderTemplate(s,t,r)),{frontmatter:o,renderedFrontmatter:s,text:await T.renderTemplate(e,t,r)}}catch(n){throw console.error("Error rendering template",n),n}}async function fe(e){let t;if(!e||!e.templatePage){let m=await ue("template",{filter:["attr",["attr","aiprompt"],"description"]});t=await a.filterBox("Prompt Template",m.map(p=>{let g=p.ref.split("/").pop();return{...p,description:p.aiprompt.description||p.ref,name:p.aiprompt.displayName||g,systemPrompt:p.aiprompt.systemPrompt||"You are an AI note assistant. Please follow the prompt instructions.",insertAt:p.aiprompt.insertAt||"cursor"}}),"Select the template to use as the prompt. The prompt will be rendered and sent to the LLM model.")}else{console.log("selectedTemplate from slash completion: ",e);let m=await f.readPage(e.templatePage),p=await h.parseMarkdown(m),{aiprompt:g}=await A(p);console.log("templatePage from slash completion: ",m),t={ref:e.templatePage,systemPrompt:g.systemPrompt||"You are an AI note assistant. Please follow the prompt instructions.",insertAt:g.insertAt||"cursor"}}if(!t){await a.flashNotification("No template selected");return}console.log("User selected prompt template: ",t);let r=["cursor","page-start","page-end"];if(!r.includes(t.insertAt)){console.error(`Invalid insertAt value: ${t.insertAt}. It must be one of ${r.join(", ")}`),await a.flashNotification(`Invalid insertAt value: ${t.insertAt}. Please select a valid option.`,"error");return}let n=await f.readPage(t.ref),o=await a.getCurrentPage(),s=await f.getPageMeta(o),c;switch(t.insertAt){case"page-start":c=0;break;case"page-end":c=await v();break;case"frontmatter":await a.flashNotification("rendering in frontmatter not supported yet","error");break;case"modal":break;case"replace":break;case"cursor":default:c=await a.getCursor()}let l=await de(n,s,{page:s});await y.streamChatIntoEditor({messages:[{role:"system",content:t.systemPrompt},{role:"user",content:l.text}],stream:!0},c)}var ge={queryOpenAI:le,reloadConfig:re,summarizeNote:oe,callOpenAI:ne,tagNoteWithAI:se,promptAndGenerateImage:ce,streamOpenAIWithSelectionAsPrompt:ie,streamChatOnPage:ae,insertAiPromptFromTemplate:fe},he={name:"silverbullet-ai",requiredPermissions:["fetch"],functions:{queryOpenAI:{path:"sbai.ts:queryOpenAI"},reloadConfig:{path:"sbai.ts:reloadConfig",events:["page:saved"]},summarizeNote:{path:"sbai.ts:openSummaryPanel",command:{name:"AI: Summarize Note and open summary"}},callOpenAI:{path:"sbai.ts:callOpenAIwithNote",command:{name:"AI: Call OpenAI with Note as context"}},tagNoteWithAI:{path:"sbai.ts:tagNoteWithAI",command:{name:"AI: Generate tags for note"}},promptAndGenerateImage:{path:"sbai.ts:promptAndGenerateImage",command:{name:"AI: Generate and insert image using DallE"}},streamOpenAIWithSelectionAsPrompt:{path:"sbai.ts:streamOpenAIWithSelectionAsPrompt",command:{name:"AI: Stream response with selection or note as prompt"}},streamChatOnPage:{path:"sbai.ts:streamChatOnPage",command:{name:"AI: Chat on current page",key:"Ctrl-Shift-Enter",mac:"Cmd-Shift-Enter"}},insertAiPromptFromTemplate:{path:"src/prompts.ts:insertAiPromptFromTemplate",command:{name:"AI: Execute AI Prompt from Custom Template"}}},assets:{}},qn={manifest:he,functionMapping:ge};W(ge,he);export{qn as plug};