From f62afac343c97796f1913bb977cb3ccb750b4692 Mon Sep 17 00:00:00 2001 From: weilirs Date: Mon, 4 Nov 2024 18:05:26 +0800 Subject: [PATCH 1/2] feat: @ syntax draft --- electron/main/filesystem/filesystem.ts | 17 +++ electron/main/filesystem/ipcHandlers.ts | 5 + electron/preload/index.ts | 1 + src/components/Chat/ChatInput.tsx | 151 +++++++++++++++++------ src/components/Chat/FileAutocomplete.tsx | 40 ++++++ src/components/Chat/StartChat.tsx | 101 +++++++++++++-- src/components/Chat/index.tsx | 24 +++- 7 files changed, 290 insertions(+), 49 deletions(-) create mode 100644 src/components/Chat/FileAutocomplete.tsx diff --git a/electron/main/filesystem/filesystem.ts b/electron/main/filesystem/filesystem.ts index fc64a5ab..a6b3ae2b 100644 --- a/electron/main/filesystem/filesystem.ts +++ b/electron/main/filesystem/filesystem.ts @@ -5,9 +5,11 @@ import * as path from 'path' import chokidar from 'chokidar' import { BrowserWindow } from 'electron' +import Store from 'electron-store' import { FileInfo, FileInfoTree } from './types' import addExtensionToFilenameIfNoExtensionPresent from '../path/path' import { isFileNodeDirectory } from '../../../shared/utils' +import { StoreSchema, StoreKeys } from '../electron-store/storeConfig' export const markdownExtensions = ['.md', '.markdown', '.mdown', '.mkdn', '.mkd'] @@ -217,3 +219,18 @@ export function splitDirectoryPathIntoBaseAndRepo(fullPath: string) { return { localModelPath, repoName } } + +export const searchFiles = async (store: Store, searchTerm: string): Promise => { + const vaultDirectory = store.get(StoreKeys.DirectoryFromPreviousSession) + if (!vaultDirectory || typeof vaultDirectory !== 'string') { + throw new Error('No valid vault directory found') + } + + const allFiles = GetFilesInfoList(vaultDirectory) + + const searchTermLower = searchTerm.toLowerCase() + + const matchingFiles = allFiles.filter((file) => file.name.toLowerCase().startsWith(searchTermLower)) + + return matchingFiles.map((file) => file.path) +} diff --git a/electron/main/filesystem/ipcHandlers.ts b/electron/main/filesystem/ipcHandlers.ts index 8d4454d4..8ecea1b6 100644 --- a/electron/main/filesystem/ipcHandlers.ts +++ b/electron/main/filesystem/ipcHandlers.ts @@ -15,6 +15,7 @@ import { GetFilesInfoListForListOfPaths, startWatchingDirectory, updateFileListForRenderer, + searchFiles, } from './filesystem' import { FileInfoTree, WriteFileProps, RenameFileProps, FileInfoWithContent } from './types' @@ -193,6 +194,10 @@ const registerFileHandlers = (store: Store, _windowsManager: Window } return [] }) + + ipcMain.handle('search-files', async (_event, searchTerm: string) => { + return searchFiles(store, searchTerm) + }) } export default registerFileHandlers diff --git a/electron/preload/index.ts b/electron/preload/index.ts index 383eb77c..59a985f1 100644 --- a/electron/preload/index.ts +++ b/electron/preload/index.ts @@ -93,6 +93,7 @@ const fileSystem = { moveFileOrDir: createIPCHandler<(sourcePath: string, destinationPath: string) => Promise>('move-file-or-dir'), getAllFilenamesInDirectory: createIPCHandler<(dirName: string) => Promise>('get-files-in-directory'), getFiles: createIPCHandler<(filePaths: string[]) => Promise>('get-files'), + searchFiles: createIPCHandler<(searchTerm: string) => Promise>('search-files'), } const path = { diff --git a/src/components/Chat/ChatInput.tsx b/src/components/Chat/ChatInput.tsx index 7bb9b5f2..2959a2bf 100644 --- a/src/components/Chat/ChatInput.tsx +++ b/src/components/Chat/ChatInput.tsx @@ -1,7 +1,7 @@ -import React from 'react' - +import React, { useState, useRef } from 'react' import { PiPaperPlaneRight } from 'react-icons/pi' import { LoadingState } from '../../lib/llm/types' +import FileAutocomplete from './FileAutocomplete' interface ChatInputProps { userTextFieldInput: string @@ -15,46 +15,121 @@ const ChatInput: React.FC = ({ setUserTextFieldInput, handleSubmitNewMessage, loadingState, -}) => ( -
-
-
-
-