Skip to content

Commit

Permalink
Merge pull request #474 from subin-chella/feat---441--Writing-assista…
Browse files Browse the repository at this point in the history
…nt-checkbox-for-feeding-current-editor-contents-to-llm

Feat   441  writing assistant checkbox for feeding current editor contents to llm
  • Loading branch information
weilirs authored Nov 10, 2024
2 parents ba9011f + 6e0744a commit 0df494c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 14 deletions.
9 changes: 9 additions & 0 deletions electron/main/electron-store/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ export const registerStoreHandlers = (store: Store<StoreSchema>, windowsManager:
chatHistoriesMap[vaultDir] = filteredChatHistories
store.set(StoreKeys.Chats, chatHistoriesMap)
})

ipcMain.handle('set-auto-context', (event, autoContext: boolean) => {
store.set(StoreKeys.AutoContext, autoContext)
event.sender.send('auto-context-changed', autoContext)
})

ipcMain.handle('get-auto-context', () => {
return store.get(StoreKeys.AutoContext, true) // Default to true
})
}

export function getDefaultEmbeddingModelConfig(store: Store<StoreSchema>): EmbeddingModelConfig {
Expand Down
2 changes: 2 additions & 0 deletions electron/main/electron-store/storeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface StoreSchema {
spellCheck: string
EditorFlexCenter: boolean
showDocumentStats: boolean
autoContext: boolean
}

export enum StoreKeys {
Expand All @@ -82,4 +83,5 @@ export enum StoreKeys {
SpellCheck = 'spellCheck',
EditorFlexCenter = 'editorFlexCenter',
showDocumentStats = 'showDocumentStats',
AutoContext = 'autoContext',
}
2 changes: 2 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const electronStore = {
setEditorFlexCenter: createIPCHandler<(editorFlexCenter: boolean) => Promise<void>>('set-editor-flex-center'),
getAgentConfigs: createIPCHandler<() => Promise<AgentConfig[]>>('get-agent-configs'),
setAgentConfig: createIPCHandler<(agentConfig: AgentConfig) => Promise<void>>('set-agent-config'),
setAutoContext: createIPCHandler<(value: boolean) => Promise<void>>('set-auto-context'),
getAutoContext: createIPCHandler<() => Promise<boolean>>('get-auto-context'),
}

const fileSystem = {
Expand Down
84 changes: 70 additions & 14 deletions src/components/WritingAssistant/WritingAssistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const WritingAssistant: React.FC = () => {
// const [prompts, setPrompts] = useState<{ option?: string; customPromptInput?: string }[]>([])

const { editor, highlightData } = useFileContext()
const [autoContext, setAutoContext] = useState<boolean>(true)

useOutsideClick(markdownContainerRef, () => {
setMessages([])
Expand Down Expand Up @@ -138,23 +139,48 @@ const WritingAssistant: React.FC = () => {
}, [editor])

useEffect(() => {
if (editor && isSpaceTrigger && spacePosition !== null) {
const checkSpacePresence = () => {
const currentContent = editor.state.doc.textBetween(spacePosition, spacePosition + 1)
if (currentContent !== ' ') {
setIsOptionsVisible(false)
setIsSpaceTrigger(false)
setSpacePosition(null)
if (!editor || !isSpaceTrigger || spacePosition === null) {
return undefined
}

const resetSpaceTrigger = () => {
setIsOptionsVisible(false)
setIsSpaceTrigger(false)
setSpacePosition(null)
}

const checkSpacePresence = () => {
try {
if (!editor.state?.doc) {
resetSpaceTrigger()
return
}
}

editor.on('update', checkSpacePresence)
const { from } = editor.state.selection

return () => {
editor.off('update', checkSpacePresence)
if (from !== spacePosition) {
resetSpaceTrigger()
return
}

const $pos = editor.state.doc.resolve(from)
if (!$pos?.parent?.textContent?.startsWith(' ')) {
resetSpaceTrigger()
}
} catch (error) {
resetSpaceTrigger()
}
}
return () => {}

const handler = () => {
requestAnimationFrame(checkSpacePresence)
}

editor.on('update', handler)

return () => {
editor.off('update', handler)
}
}, [editor, isSpaceTrigger, spacePosition])

useEffect(() => {
Expand All @@ -180,6 +206,14 @@ const WritingAssistant: React.FC = () => {
}
}, [editor, cursorPosition])

useEffect(() => {
const loadAutoContext = async () => {
const savedAutoContext = await window.electronStore.getAutoContext()
setAutoContext(savedAutoContext)
}
loadAutoContext()
}, [])

const copyToClipboard = () => {
const assistantMessage = messages[currentConversationIndex + 1]
if (!assistantMessage || assistantMessage.role !== 'assistant') return
Expand Down Expand Up @@ -213,9 +247,12 @@ const WritingAssistant: React.FC = () => {
if (!assistantMessage || assistantMessage.role !== 'assistant' || !editor) return

const replacementText = assistantMessage.visibleContent || assistantMessage.content

if (replacementText) {
editor.chain().focus().deleteSelection().insertContent(replacementText).run()
if (highlightData.text) {
editor.chain().focus().deleteSelection().insertContent(replacementText).run()
} else if (autoContext) {
editor.chain().focus().selectAll().deleteSelection().insertContent(replacementText).run()
}
}

setMessages([])
Expand Down Expand Up @@ -260,6 +297,9 @@ const WritingAssistant: React.FC = () => {

const handleOption = async (option: string, customPromptInput?: string) => {
let selectedText = highlightData.text
if (autoContext && !selectedText && editor) {
selectedText = editor.state.doc.textBetween(0, editor.state.doc.content.size)
}
if (lastAssistantMessage) {
selectedText =
typeof lastAssistantMessage.content === 'string'
Expand All @@ -272,6 +312,10 @@ const WritingAssistant: React.FC = () => {
setIsOptionsVisible(false)
await getLLMResponse(prompt)
}
const handleAutoContextChange = async (value: boolean) => {
setAutoContext(value)
await window.electronStore.setAutoContext(value)
}

if (!isSpaceTrigger && !highlightData.position) return null
if (isSpaceTrigger && isOptionsVisible && !getLastMessage(messages, 'assistant'))
Expand Down Expand Up @@ -324,6 +368,18 @@ const WritingAssistant: React.FC = () => {
List key Takeaways
</Button>
</div>
<div className="mt-2 flex items-center">
<label htmlFor="autoContextCheckbox" className="flex items-center">

Check failure on line 372 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Insert `····`

Check failure on line 372 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Insert `····`

Check failure on line 372 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Insert `····`

Check failure on line 372 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Insert `····`
<input

Check failure on line 373 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Insert `····`

Check failure on line 373 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Insert `····`

Check failure on line 373 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Insert `····`

Check failure on line 373 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Insert `····`
type="checkbox"

Check failure on line 374 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Replace `··········` with `··············`

Check failure on line 374 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Replace `··········` with `··············`

Check failure on line 374 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Replace `··········` with `··············`

Check failure on line 374 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Replace `··········` with `··············`
id="autoContextCheckbox"

Check failure on line 375 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Insert `····`

Check failure on line 375 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Insert `····`

Check failure on line 375 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Insert `····`

Check failure on line 375 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Insert `····`
checked={autoContext}

Check failure on line 376 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Replace `··········` with `··············`

Check failure on line 376 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Replace `··········` with `··············`

Check failure on line 376 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Replace `··········` with `··············`

Check failure on line 376 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Replace `··········` with `··············`
onChange={(e) => handleAutoContextChange(e.target.checked)}

Check failure on line 377 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Replace `··········` with `··············`

Check failure on line 377 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Replace `··········` with `··············`

Check failure on line 377 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Replace `··········` with `··············`

Check failure on line 377 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Replace `··········` with `··············`
className="size-4 rounded border-gray-600 bg-gray-700 text-indigo-500 focus:ring-1 focus:ring-indigo-500/30"

Check failure on line 378 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Insert `····`

Check failure on line 378 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Insert `····`

Check failure on line 378 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Insert `····`

Check failure on line 378 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Insert `····`
/>

Check failure on line 379 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Insert `····`

Check failure on line 379 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Insert `····`

Check failure on line 379 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Insert `····`

Check failure on line 379 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Insert `····`
<span className="ml-2 select-none text-xs text-gray-400">Use File Content (If no text selected)</span>

Check failure on line 380 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Replace `········` with `············`

Check failure on line 380 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Replace `········` with `············`

Check failure on line 380 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Replace `········` with `············`

Check failure on line 380 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Replace `········` with `············`
</label>

Check failure on line 381 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (ubuntu-latest, x64)

Insert `····`

Check failure on line 381 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-latest)

Insert `····`

Check failure on line 381 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (macos-13)

Insert `····`

Check failure on line 381 in src/components/WritingAssistant/WritingAssistant.tsx

View workflow job for this annotation

GitHub Actions / build_and_package (windows-latest)

Insert `····`
</div>
</div>
)
return (
Expand Down

0 comments on commit 0df494c

Please sign in to comment.