diff --git a/src/App.tsx b/src/App.tsx index 6ab84a8..b6b6918 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,9 +4,7 @@ import { convertToFlashCardFromEvent, DivideTaskIntoSubTasksFromEvent, ollamaUI, - summarizeBlockFromEvent, - promptFromBlockEvent, - expandBlockEvent + promptFromBlockEventClosure } from "./ollama"; import { useAppVisible } from "./utils"; @@ -46,16 +44,33 @@ function App() { if (!logseq.settings) { return } + +logseq.Editor.getPageBlocksTree("ollama-logseq-config").then((blocks) => { + blocks!.forEach((block) => { + logseq.Editor.getBlockProperty(block.uuid, "ollama-context-menu-title").then((title) => { + logseq.Editor.getBlockProperty(block.uuid, "ollama-prompt-prefix").then((prompt_prefix) => { + logseq.Editor.registerBlockContextMenuItem(title, promptFromBlockEventClosure(prompt_prefix)) + }) + }).catch((reason) => { + }) + }) +}).catch((reason) => { + console.log("Can not find the configuration page named 'ollama-logseq-config'") + console.log(reason) +}) + + logseq.Editor.registerSlashCommand("ollama", ollamaUI) logseq.Editor.registerBlockContextMenuItem("Ollama: Create a flash card", convertToFlashCardFromEvent) - logseq.Editor.registerBlockContextMenuItem("Ollama: Summarize block", summarizeBlockFromEvent) logseq.Editor.registerBlockContextMenuItem("Ollama: Divide into subtasks", DivideTaskIntoSubTasksFromEvent) - logseq.Editor.registerBlockContextMenuItem("Ollama: Prompt from Block", promptFromBlockEvent) - logseq.Editor.registerBlockContextMenuItem("Ollama: Expand Block", expandBlockEvent) + logseq.Editor.registerBlockContextMenuItem("Ollama: Prompt from Block", promptFromBlockEventClosure()) + logseq.Editor.registerBlockContextMenuItem("Ollama: Summarize block", promptFromBlockEventClosure("Summarize: ")) + logseq.Editor.registerBlockContextMenuItem("Ollama: Expand Block", promptFromBlockEventClosure("Expand: ")) + logseq.App.registerCommandShortcut( { "binding": logseq.settings.shortcut }, ollamaUI - ); + ); }, []) if (visible) { diff --git a/src/ollama.tsx b/src/ollama.tsx index ca493ac..5ba47ab 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -97,8 +97,7 @@ async function ollamaGenerate(prompt: string, parameters?: OllamaGenerateParamet throw new Error("Error in Ollama request: " + response.statusText) } const data = await response.json() - return data.response - + return data } catch (e: any) { console.error("ERROR: ", e) logseq.App.showMsg("Coudln't fulfull request make sure that ollama service is running and make sure there is no typo in host or model name") @@ -199,30 +198,48 @@ async function getOllamaParametersFromBlockProperties(b: BlockEntity) { return ollamaParameters } -export async function promptFromBlockEvent(b: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - const answerBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, '🦙Generating ...', { before: false }) - const params = await getOllamaParametersFromBlockProperties(currentBlock!) - const prompt = currentBlock!.content.replace(/^.*::.*$/gm, '') // nasty hack to remove properties from block content - const response = await ollamaGenerate(prompt, params); +async function getOllamaParametersFromBlockAndParentProperties(b: BlockEntity) { + let p_params: OllamaGenerateParameters = {} + if (b.parent) { + let parentBlock = await logseq.Editor.getBlock(b.parent.id) + if (parentBlock) + p_params = await getOllamaParametersFromBlockProperties(parentBlock) + } + const b_params = await getOllamaParametersFromBlockProperties(b) + return {...p_params, ...b_params} +} - await logseq.Editor.updateBlock(answerBlock!.uuid, `${response}`) - } catch (e: any) { - logseq.UI.showMsg(e.toString(), 'warning') - console.error(e) +async function promptFromBlock(block: BlockEntity, prefix?: string) { + const answerBlock = await logseq.Editor.insertBlock(block!.uuid, '🦙Generating ...', { before: false }) + const params = await getOllamaParametersFromBlockAndParentProperties(block!) + console.log("ollama params", params) + + let prompt = block!.content.replace(/^.*::.*$/gm, '') // hack to remove properties from block content + if (prefix) { + prompt = prefix + " " + prompt + } + console.log("prompt", prompt) + + const result = await ollamaGenerate(prompt, params); + + console.log("ollama response", result) + + if (params.usecontext) { //FIXME: work out the best way to story context + await logseq.Editor.upsertBlockProperty(block!.uuid, 'ollama-generate-context', result.context) } + + await logseq.Editor.updateBlock(answerBlock!.uuid, `${result.response}`) } -export async function expandBlockEvent(b: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - const answerBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, '⌛Generating ...', { before: false }) - const response = await promptLLM(`Expand: ${currentBlock!.content}`); - await logseq.Editor.updateBlock(answerBlock!.uuid, `${response}`) - } catch (e: any) { - logseq.UI.showMsg(e.toString(), 'warning') - console.error(e) +export function promptFromBlockEventClosure(prefix?: string) { + return async (event: IHookEvent) => { + try { + const currentBlock = await logseq.Editor.getBlock(event.uuid) + await promptFromBlock(currentBlock!, prefix) + } catch (e: any) { + logseq.UI.showMsg(e.toString(), 'warning') + console.error(e) + } } } @@ -244,18 +261,6 @@ export async function askAI(prompt: string, context: string) { } } -export async function summarizeBlockFromEvent(b: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - let summaryBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, `⌛Summarizing Block...`, { before: true }) - const summary = await promptLLM(`Summarize the following ${currentBlock!.content}`); - await logseq.Editor.updateBlock(summaryBlock!.uuid, `Summary: ${summary}`) - } catch (e: any) { - logseq.App.showMsg(e.toString(), 'warning') - console.error(e) - } -} - export async function convertToFlashCard(uuid: string, blockContent: string) { try { const questionBlock = await logseq.Editor.insertBlock(uuid, "⌛Genearting question....", { before: false })