Skip to content

Commit

Permalink
Added the ability to Ask in context of the block only instead of the …
Browse files Browse the repository at this point in the history
…whole page
  • Loading branch information
omagdy7 committed Feb 22, 2024
1 parent 4f19710 commit 481ae0c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { useAppVisible } from "./utils";

const options = [
'Ask ai',
'Ask with context',
'Ask with page context',
'Ask with block context',
'Define',
'Divide into subtasks',
'Summarize',
'Summarize Page',
'Summarize Block',
'Convert to flash card',
];
Expand Down
14 changes: 8 additions & 6 deletions src/components/OllamaCommandPallete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CommandItem,
CommandList,
} from "@/components/ui/command"
import { convertToFlashCardCurrentBlock, DivideTaskIntoSubTasksCurrentBlock, summarize, summarizeBlock } from "@/ollama";
import { convertToFlashCardCurrentBlock, DivideTaskIntoSubTasksCurrentBlock, summarizePage, summarizeBlock } from "@/ollama";
import { PromptAI } from "./PromptAI";

export function OllamaCommandPallete({ options, theme }: { options: string[], theme: string }) {
Expand All @@ -20,9 +20,9 @@ export function OllamaCommandPallete({ options, theme }: { options: string[], th
logseq.hideMainUI()
DivideTaskIntoSubTasksCurrentBlock()
break;
case "summarize":
case "summarize page":
logseq.hideMainUI()
summarize()
summarizePage()
break;
case "summarize block":
logseq.hideMainUI()
Expand Down Expand Up @@ -51,12 +51,14 @@ export function OllamaCommandPallete({ options, theme }: { options: string[], th
};
}, []);

if (isEnterPressed && (selection !== 'ask ai' && selection !== 'define' && selection !== 'ask with context')) {
return null
const validSelections = ['ask with page context', 'ask with block context', 'ask ai', 'define', 'ask with context'];

if (isEnterPressed && !validSelections.includes(selection)) {
return null;
}

return (
selection === 'ask with context' || selection === 'ask ai' || selection === 'define' ? (<PromptAI theme={theme} type={selection} />) : (
validSelections.includes(selection) ? (<PromptAI theme={theme} type={selection} />) : (
<Command className={(theme === 'dark' ? "dark dark:bg-gray-900" : "bg-gray-200") + " rounded-lg border shadow-md w-1/2"}>
<CommandInput className="ai-input" placeholder="Type a command or search..." />
<CommandList>
Expand Down
8 changes: 5 additions & 3 deletions src/components/PromptAI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Input } from '@/components/ui/input';

export const PromptAI = ({ type, theme }: { type: string, theme: string }) => {

const placeholder = type === 'ask ai' ? "Prompt..." : "Define..."
const placeholder = type.startsWith('ask') ? "Prompt..." : "Define..."
const [inputValue, setInputValue] = useState('');
const [hitEnter, setHitEnter] = useState(false)

Expand All @@ -15,8 +15,10 @@ export const PromptAI = ({ type, theme }: { type: string, theme: string }) => {
askAI(inputValue, "")
} else if (type === 'define') {
defineWord(inputValue)
} else if (type === 'ask with context') {
askWithContext(inputValue)
} else if (type === 'ask with page context') {
askWithContext(inputValue, 'page')
} else if (type === 'ask with block context') {
askWithContext(inputValue, 'block')
}
}
}, [hitEnter])
Expand Down
62 changes: 44 additions & 18 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -1,76 +1,102 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
:root {
--background: 0 0% 100%;
--foreground: 222.2 84% 4.9%;

--card: 0 0% 100%;
--card-foreground: 222.2 84% 4.9%;

--popover: 0 0% 100%;
--popover-foreground: 222.2 84% 4.9%;

--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%;

--secondary: 210 40% 96.1%;
--secondary-foreground: 222.2 47.4% 11.2%;

--muted: 210 40% 96.1%;
--muted-foreground: 215.4 16.3% 46.9%;

--accent: 210 40% 96.1%;
--accent-foreground: 222.2 47.4% 11.2%;

--destructive: 0 84.2% 60.2%;
--destructive-foreground: 210 40% 98%;

--border: 214.3 31.8% 91.4%;
--input: 214.3 31.8% 91.4%;
--ring: 222.2 84% 4.9%;

--radius: 0.5rem;
}

.dark {
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;

--card: 222.2 84% 4.9%;
--card-foreground: 210 40% 98%;

--popover: 222.2 84% 4.9%;
--popover-foreground: 210 40% 98%;

--primary: 210 40% 98%;
--primary-foreground: 222.2 47.4% 11.2%;

--secondary: 217.2 32.6% 17.5%;
--secondary-foreground: 210 40% 98%;

--muted: 217.2 32.6% 17.5%;
--muted-foreground: 215 20.2% 65.1%;

--accent: 217.2 32.6% 17.5%;
--accent-foreground: 210 40% 98%;

--destructive: 0 62.8% 30.6%;
--destructive-foreground: 210 40% 98%;

--border: 217.2 32.6% 17.5%;
--input: 217.2 32.6% 17.5%;
--ring: 212.7 26.8% 83.9%;
}
}

@layer base {
* {
@apply border-border;
}

/* body { */
/* @apply bg-background text-foreground; */
/* } */
}


/* Custom scrollbar styles */
/* Works in Firefox, Chrome, Edge, Safari */

/* Track */
::-webkit-scrollbar {
width: 10px;
border-radius: 5px;
/* Adjust to make scrollbar rounded */
background-color: #000000;
/* Gray background color */
}

/* Handle */
::-webkit-scrollbar-thumb {
background: #808080;
border-radius: 5px;
/* Adjust to make scrollbar thumb rounded */
}

/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
background: #555;
}
25 changes: 18 additions & 7 deletions src/ollama.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 481ae0c

Please sign in to comment.