diff options
Diffstat (limited to 'src/ollama.tsx')
| -rw-r--r-- | src/ollama.tsx | 251 |
1 files changed, 195 insertions, 56 deletions
diff --git a/src/ollama.tsx b/src/ollama.tsx index a08c6e3..f6e9b97 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -1,57 +1,130 @@ -import { SettingSchemaDesc } from "@logseq/libs/dist/LSPlugin.user"; +import { IHookEvent } from "@logseq/libs/dist/LSPlugin.user"; +import { BlockEntity, BlockUUIDTuple } from "@logseq/libs/dist/LSPlugin.user"; const delay = (t = 100) => new Promise(r => setTimeout(r, t)) -let settings: SettingSchemaDesc[] = [ - { - key: "host", - type: "string", - title: "Host", - description: "Set the host of your ollama model", - default: "localhost:11434" - }, - { - key: "model", - type: "string", - title: "LLM Model", - description: "Set your desired model to use ollama", - default: "mistral:instruct" - }, -] - -async function promptLLM(url: string, prompt: string, model: string) { - - - const response = await fetch('http://localhost:11434/api/generate', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - model: model, - prompt: prompt, - stream: false, - }), - }) - if (!response.ok) { - throw new Error('Network response was not ok'); +export async function ollamaUI() { + logseq.showMainUI() + setTimeout(() => { + const element = document.querySelector(".ai-input") as HTMLInputElement | null; + if (element) { + element.focus(); + } + }, 300) +} + +function isBlockEntity(b: BlockEntity | BlockUUIDTuple): b is BlockEntity { + return (b as BlockEntity).uuid !== undefined; +} + +async function getTreeContent(b: BlockEntity) { + let content = ""; + const trimmedBlockContent = b.content.trim(); + if (trimmedBlockContent.length > 0) { + content += trimmedBlockContent; } - const data = await response.json(); + if (!b.children) { + return content; + } + + for (const child of b.children) { + if (isBlockEntity(child)) { + content += await getTreeContent(child); + } else { + const childBlock = await logseq.Editor.getBlock(child[1], { + includeChildren: true, + }); + if (childBlock) { + content += await getTreeContent(childBlock); + } + } + } + return content; +} + +export async function getPageContentFromBlock(b: BlockEntity): Promise<string> { + let blockContents = []; + + const currentBlock = await logseq.Editor.getBlock(b); + if (!currentBlock) { + throw new Error("Block not found"); + } + + const page = await logseq.Editor.getPage(currentBlock.page.id); + if (!page) { + throw new Error("Page not found"); + } + + const pageBlocks = await logseq.Editor.getPageBlocksTree(page.name); + for (const pageBlock of pageBlocks) { + const blockContent = await getTreeContent(pageBlock); + if (blockContent.length > 0) { + blockContents.push(blockContent); + } + } + return blockContents.join(" "); +} - return data.response; +async function promptLLM(prompt: string) { + if (!logseq.settings) { + throw new Error("Couldn't find logseq settings"); + } + try { + const response = await fetch(`http://${logseq.settings.host}/api/generate`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + model: logseq.settings.model, + prompt: prompt, + stream: false, + }), + }) + if (!response.ok) { + console.log("Error: couldn't fulful request") + logseq.App.showMsg("Couldn't fulfuil request make sure you don't have a typo in the name of the model or the host url") + throw new Error('Network response was not ok'); + } + const data = await response.json(); + return data.response; + } catch (e: any) { + console.error("ERROR: ", e) + logseq.App.showMsg("Couldn't fulfuil request make sure you don't have a typo in the name of the model or the host url") + } } export async function defineWord(word: string) { - askAI(`Define this following ${word}`) + askAI(`What's the formal defintion of ${word}`) } -export async function summarize() { + +export async function askWithContext(prompt: string) { await delay(300) try { + const currentBlocksTree = await logseq.Editor.getCurrentPageBlocksTree() + const currentBlock = currentBlocksTree[0] + if (!currentBlock) { + throw new Error("Block not found"); + } + let blocksContent = "" + for (const block of currentBlocksTree) { + blocksContent += await getTreeContent(block) + } + askAI(`With the Context of : ${blocksContent}, ${prompt}`) + } catch (e: any) { + logseq.App.showMsg(e.toString(), 'warning') + console.error(e) + } +} + +export async function summarize() { + await delay(300) + try { const currentSelectedBlocks = await logseq.Editor.getCurrentPageBlocksTree() let blocksContent = "" if (currentSelectedBlocks) { @@ -60,15 +133,12 @@ export async function summarize() { blocksContent += block.content + "/n" } if (lastBlock) { - lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, '🚀 Summarizing....', { before: false }) + lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, '🚀 Summarizing....', { before: true }) } - - const summary = await promptLLM("localhost:11434", `Summarize the following ${blocksContent}`, "mistral:instruct") - + const summary = await promptLLM(`Summarize the following ${blocksContent}`) await logseq.Editor.updateBlock(lastBlock.uuid, `Summary: ${summary}`) } - - } catch (e) { + } catch (e: any) { logseq.App.showMsg(e.toString(), 'warning') console.error(e) } @@ -76,7 +146,6 @@ export async function summarize() { export async function askAI(prompt: string) { await delay(300) - try { const currentSelectedBlocks = await logseq.Editor.getCurrentPageBlocksTree() if (currentSelectedBlocks) { @@ -84,11 +153,87 @@ export async function askAI(prompt: string) { if (lastBlock) { lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, 'Generating....', { before: true }) } - const response = await promptLLM("localhost:11434", prompt, "mistral:instruct") + const response = await promptLLM(prompt) await logseq.Editor.updateBlock(lastBlock.uuid, response) } - } catch (e) { + } catch (e: any) { + logseq.App.showMsg(e.toString(), 'warning') + console.error(e) + } +} + + +export async function convertToFlashCardFromEvent(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, 'Generating....', { before: false }) + if (!block) { + throw new Error("Block not found"); + } + const response = await promptLLM(`Create a flashcard for:\n ${currentBlock.content}`) + await logseq.Editor.updateBlock(block.uuid, `${response} #card`) + } catch (e: any) { + logseq.App.showMsg(e.toString(), 'warning') + console.error(e) + } +} + + +export async function convertToFlashCard() { + try { + const currentBlock = await logseq.Editor.getCurrentBlock() + 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 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) } @@ -98,18 +243,12 @@ export async function DivideTaskIntoSubTasks() { try { const currentBlock = await logseq.Editor.getCurrentBlock() if (currentBlock) { - // const block = await logseq.Editor.insertBlock(currentBlock.uuid, 'Generating....', { before: false }) - logseq.App.showMsg(` - [:div.p-2 - [:h1 "currentBlock content"] - [:h2.text-xl "Divide this task into subtasks: ${currentBlock?.content}"]] - `) - const response = await promptLLM("localhost:11434", `Divide this task into subtasks with numbers: ${currentBlock.content}`, "mistral:instruct") + const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content}`) for (const todo of response.split("\n")) { - const block = await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)}`, { before: false }) + await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)}`, { before: false }) } } - } catch (e) { + } catch (e: any) { logseq.App.showMsg(e.toString(), 'warning') console.error(e) } |
