From 481ae0c24785a1798a0445a2a982acce6db7e4f2 Mon Sep 17 00:00:00 2001
From: omagdy7 <omar.professional8777@gmail.com>
Date: Thu, 22 Feb 2024 19:12:22 +0200
Subject: Added the ability to Ask in context of the block only instead of the
 whole page

---
 src/ollama.tsx | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

(limited to 'src/ollama.tsx')

diff --git a/src/ollama.tsx b/src/ollama.tsx
index e59c633..c0c27e2 100644
--- a/src/ollama.tsx
+++ b/src/ollama.tsx
@@ -134,16 +134,22 @@ async function promptLLM(prompt: string) {
 }
 
 export async function defineWord(word: string) {
-  askAI(`What's the defintion of ${word}`, "")
+  askAI(`What's the defintion of ${word}?`, "")
 }
 
+type ContextType = 'block' | 'page'
 
-export async function askWithContext(prompt: string) {
+export async function askWithContext(prompt: string, contextType: ContextType) {
   try {
-    const currentBlocksTree = await logseq.Editor.getCurrentPageBlocksTree()
     let blocksContent = ""
-    for (const block of currentBlocksTree) {
-      blocksContent += await getTreeContent(block)
+    if (contextType === 'page') {
+      const currentBlocksTree = await logseq.Editor.getCurrentPageBlocksTree()
+      for (const block of currentBlocksTree) {
+        blocksContent += await getTreeContent(block)
+      }
+    } else {
+      const currentBlock = await logseq.Editor.getCurrentBlock()
+      blocksContent += await getTreeContent(currentBlock!)
     }
     askAI(prompt, `Context: ${blocksContent}`)
   } catch (e: any) {
@@ -152,7 +158,7 @@ export async function askWithContext(prompt: string) {
   }
 }
 
-export async function summarize() {
+export async function summarizePage() {
   await delay(300)
   try {
     const currentSelectedBlocks = await logseq.Editor.getCurrentPageBlocksTree()
@@ -247,7 +253,12 @@ export async function askAI(prompt: string, context: string) {
   await delay(300)
   try {
     const currentBlock = await logseq.Editor.getCurrentBlock()
-    const block = await logseq.Editor.insertBlock(currentBlock!.uuid, '⌛Generating....', { before: true })
+    let block = null;
+    if (currentBlock?.content.trim() === '') {
+      block = await logseq.Editor.insertBlock(currentBlock!.uuid, '⌛Generating....', { before: true })
+    } else {
+      block = await logseq.Editor.insertBlock(currentBlock!.uuid, '⌛Generating....', { before: false })
+    }
     let response = "";
     if (context == "") {
       response = await promptLLM(prompt)
-- 
cgit v1.2.3

