diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-11-23 20:33:10 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-11-23 20:33:10 +0200 |
| commit | 21b59887998ed042aff2f6ccbbfbbd327f49eee0 (patch) | |
| tree | 51cd523197a14dd78be58ec27751d947566314ef | |
| parent | d27f32ae431cca78cda7b64150ae3c29008e9020 (diff) | |
| download | ollama-logseq-21b59887998ed042aff2f6ccbbfbbd327f49eee0.tar.xz ollama-logseq-21b59887998ed042aff2f6ccbbfbbd327f49eee0.zip | |
Applying DRY principle and removing duplicate code
| -rw-r--r-- | src/App.tsx | 2 | ||||
| -rw-r--r-- | src/components/OllamaCommandPallete.tsx | 4 | ||||
| -rw-r--r-- | src/ollama.tsx | 78 |
3 files changed, 31 insertions, 53 deletions
diff --git a/src/App.tsx b/src/App.tsx index c132365..83c2dc5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -43,7 +43,7 @@ function App() { logseq.Editor.registerSlashCommand("ollama", ollamaUI) logseq.Editor.registerBlockContextMenuItem("Create a flash card", convertToFlashCardFromEvent) logseq.Editor.registerBlockContextMenuItem("Summarize block", summarizeBlockFromEvent) - logseq.Editor.registerBlockContextMenuItem("Make task into subtasks", DivideTaskIntoSubTasksFromEvent) + logseq.Editor.registerBlockContextMenuItem("Divide into subtasks", DivideTaskIntoSubTasksFromEvent) logseq.App.registerCommandShortcut( { "binding": logseq.settings.shortcut }, ollamaUI diff --git a/src/components/OllamaCommandPallete.tsx b/src/components/OllamaCommandPallete.tsx index 10e3418..346fb1e 100644 --- a/src/components/OllamaCommandPallete.tsx +++ b/src/components/OllamaCommandPallete.tsx @@ -6,7 +6,7 @@ import { CommandItem, CommandList, } from "@/components/ui/command" -import { convertToFlashCard, convertToFlashCardCurrentBlock, DivideTaskIntoSubTasks, summarize, summarizeBlock } from "@/ollama"; +import { convertToFlashCard, convertToFlashCardCurrentBlock, DivideTaskIntoSubTasks, DivideTaskIntoSubTasksCurrentBlock, summarize, summarizeBlock } from "@/ollama"; import { PromptAI } from "./PromptAI"; export function OllamaCommandPallete({ options, theme }: { options: string[], theme: string }) { @@ -18,7 +18,7 @@ export function OllamaCommandPallete({ options, theme }: { options: string[], th switch (selection) { case "divide into subtasks": logseq.hideMainUI() - DivideTaskIntoSubTasks() + DivideTaskIntoSubTasksCurrentBlock() break; case "summarize": logseq.hideMainUI() diff --git a/src/ollama.tsx b/src/ollama.tsx index 4931fda..f5eaae7 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -109,7 +109,7 @@ export async function askWithContext(prompt: string) { for (const block of currentBlocksTree) { blocksContent += await getTreeContent(block) } - askAI(prompt, `With the Context of : ${blocksContent}`) + askAI(prompt, `Context: ${blocksContent}`) } catch (e: any) { logseq.App.showMsg(e.toString(), 'warning') console.error(e) @@ -179,23 +179,6 @@ export async function summarizeBlockFromEvent(b: IHookEvent) { } } -export async function convertToFlashCardFromEvent(b: IHookEvent) { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - if (!currentBlock) { - throw new Error("Block not found"); - } - await convertToFlashCard(currentBlock.uuid, currentBlock.content) -} - -export async function convertToFlashCardCurrentBlock() { - const currentBlock = await logseq.Editor.getCurrentBlock() - if (!currentBlock) { - throw new Error("Block not found"); - } - await convertToFlashCard(currentBlock.uuid, currentBlock.content) -} - - export async function convertToFlashCard(uuid: string, blockContent: string) { try { const questionBlock = await logseq.Editor.insertBlock(uuid, "Genearting question....", { before: false }) @@ -211,42 +194,28 @@ export async function convertToFlashCard(uuid: string, blockContent: string) { } } -export async function DivideTaskIntoSubTasksFromEvent(b: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - if (!currentBlock) { - throw new Error("Block not found"); - } - const block = await logseq.Editor.insertBlock(currentBlock.uuid, "Genearting todos....", { before: false }) - if (!block) { - throw new Error("Block not found"); - } - if (currentBlock) { - let i = 0; - const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `) - for (const todo of response.split("\n")) { - if (i == 0) { - await logseq.Editor.updateBlock(block.uuid, `TODO ${todo.slice(3)} `) - } else { - await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false }) - } - i++; - } - } - } catch (e: any) { - logseq.App.showMsg(e.toString(), 'warning') - console.error(e) - } +export async function convertToFlashCardFromEvent(b: IHookEvent) { + const currentBlock = await logseq.Editor.getBlock(b.uuid) + await convertToFlashCard(currentBlock!.uuid, currentBlock!.content) } -export async function DivideTaskIntoSubTasks() { +export async function convertToFlashCardCurrentBlock() { + const currentBlock = await logseq.Editor.getCurrentBlock() + await convertToFlashCard(currentBlock!.uuid, currentBlock!.content) +} + +export async function DivideTaskIntoSubTasks(uuid: string, content: string) { try { - const currentBlock = await logseq.Editor.getCurrentBlock() - if (currentBlock) { - const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `) - for (const todo of response.split("\n")) { - await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false }) + const block = await logseq.Editor.insertBlock(uuid, "Genearting todos....", { before: false }) + let i = 0; + const response = await promptLLM(`Divide this task into subtasks with numbers: ${content} `) + for (const todo of response.split("\n")) { + if (i == 0) { + await logseq.Editor.updateBlock(block!.uuid, `TODO ${todo.slice(3)} `) + } else { + await logseq.Editor.insertBlock(uuid, `TODO ${todo.slice(3)} `, { before: false }) } + i++; } } catch (e: any) { logseq.App.showMsg(e.toString(), 'warning') @@ -254,4 +223,13 @@ export async function DivideTaskIntoSubTasks() { } } +export async function DivideTaskIntoSubTasksFromEvent(b: IHookEvent) { + const currentBlock = await logseq.Editor.getBlock(b.uuid) + DivideTaskIntoSubTasks(currentBlock!.uuid, currentBlock!.content) +} + +export async function DivideTaskIntoSubTasksCurrentBlock() { + const currentBlock = await logseq.Editor.getCurrentBlock() + DivideTaskIntoSubTasks(currentBlock!.uuid, currentBlock!.content) +} |
