Skip to content

Commit

Permalink
relative path in chat
Browse files Browse the repository at this point in the history
  • Loading branch information
weilirs committed Nov 20, 2024
1 parent a559036 commit b13d21e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
8 changes: 8 additions & 0 deletions electron/main/filesystem/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,11 @@ export const searchFiles = async (store: Store<StoreSchema>, searchTerm: string)

return matchingFiles
}

export const getVaultPath = (store: Store<StoreSchema>): string => {
const vaultDirectory = store.get(StoreKeys.DirectoryFromPreviousSession)
if (!vaultDirectory || typeof vaultDirectory !== 'string') {
throw new Error('No valid vault directory found')
}
return vaultDirectory
}
5 changes: 5 additions & 0 deletions electron/main/filesystem/ipcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isHidden,
GetFilesInfoListForListOfPaths,
searchFiles,
getVaultPath,
} from './filesystem'
import { FileInfoTree, WriteFileProps, RenameFileProps, FileInfoWithContent } from './types'

Expand Down Expand Up @@ -159,6 +160,10 @@ const registerFileHandlers = (store: Store<StoreSchema>, _windowsManager: Window
ipcMain.handle('search-files', async (_event, searchTerm: string) => {
return searchFiles(store, searchTerm)
})

ipcMain.handle('get-vault-path', async () => {
return getVaultPath(store)
})
}

export default registerFileHandlers
6 changes: 5 additions & 1 deletion electron/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ const fileSystem = {
deleteFile: createIPCHandler<(filePath: string) => Promise<void>>('delete-file'),
getAllFilenamesInDirectory: createIPCHandler<(dirName: string) => Promise<string[]>>('get-files-in-directory'),
getFiles: createIPCHandler<(filePaths: string[]) => Promise<FileInfoWithContent[]>>('get-files'),
searchFiles: createIPCHandler<(searchTerm: string) => Promise<string[]>>('search-files'),
searchFiles:
createIPCHandler<(searchTerm: string) => Promise<Array<{ absolutePath: string; relativePath: string }>>>(
'search-files',
),
getVaultPath: createIPCHandler<() => Promise<string>>('get-vault-path'),
}

const path = {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Chat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ const ChatInput: React.FC<ChatInputProps> = ({
e.target.style.height = `${Math.min(e.target.scrollHeight, 160)}px`
}

const handleFileSelect = (filePath: string) => {
const handleFileSelect = (filePath: { absolutePath: string; relativePath: string }) => {
const lastAtIndex = userTextFieldInput.lastIndexOf('@')
const newValue = `${userTextFieldInput.slice(0, lastAtIndex)}@${filePath} ${userTextFieldInput.slice(
const newValue = `${userTextFieldInput.slice(0, lastAtIndex)}@${filePath.relativePath} ${userTextFieldInput.slice(
lastAtIndex + searchTerm.length + 1,
)}`

Expand Down
24 changes: 13 additions & 11 deletions src/components/Chat/FileAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react'
interface FileAutocompleteProps {
searchTerm: string
position: { top: number; left: number }
onSelect: (filePath: string) => void
onSelect: (filePath: { absolutePath: string; relativePath: string }) => void
visible: boolean
}

Expand All @@ -14,7 +14,7 @@ const FileAutocomplete: React.FC<FileAutocompleteProps> = ({ searchTerm, positio
const searchFiles = async () => {
if (searchTerm && visible) {
const results = await window.fileSystem.searchFiles(searchTerm)
setFiles(results.map((path) => ({ absolutePath: path, relativePath: path })))
setFiles(results)
}
}
searchFiles()
Expand Down Expand Up @@ -43,15 +43,17 @@ const FileAutocomplete: React.FC<FileAutocompleteProps> = ({ searchTerm, positio
left: position.left,
}}
>
{files.map((file) => (
<div
key={file.absolutePath}
className="cursor-pointer px-4 py-2 hover:bg-neutral-700"
onClick={() => onSelect(file.absolutePath)}
>
{formatFilePath(file.relativePath)}
</div>
))}
{files.map((file) => {
return (
<div
key={file.absolutePath}
className="cursor-pointer px-4 py-2 hover:bg-neutral-700"
onClick={() => onSelect({ absolutePath: file.absolutePath, relativePath: file.relativePath })}
>
{formatFilePath(file.relativePath)}
</div>
)
})}
</div>
)
}
Expand Down
16 changes: 12 additions & 4 deletions src/components/Chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ import { useChatContext } from '@/contexts/ChatContext'
import resolveLLMClient from '@/lib/llm/client'
import { appendToolCallsAndAutoExecuteTools, convertToolConfigToZodSchema } from '../../lib/llm/tools/utils'

const extractFileReferences = (message: string): string[] => {
const extractFileReferences = async (message: string): Promise<string[]> => {
const regex = /@([^@]+?\.md)/g
const matches = message.match(regex)
// console.log('matches', matches)
return matches ? matches.map((match) => match.slice(1)) : []

if (!matches) return []

// Convert relative paths to absolute paths
const vaultPath = await window.fileSystem.getVaultPath()
return matches.map((match) => {
const relativePath = match.slice(1) // Remove @ symbol

return `${vaultPath}/${relativePath}`
})
}

const ChatComponent: React.FC = () => {
Expand Down Expand Up @@ -47,7 +55,7 @@ const ChatComponent: React.FC = () => {
}

// Extract file references from the message
const fileRefs = extractFileReferences(userTextFieldInput || '')
const fileRefs = await extractFileReferences(userTextFieldInput || '')
// console.log('fileRefs', fileRefs)

// Create or update chat with file context
Expand Down

0 comments on commit b13d21e

Please sign in to comment.