diff --git a/src/interface/web/app/chat/page.tsx b/src/interface/web/app/chat/page.tsx index c3d5ff37..c0472b23 100644 --- a/src/interface/web/app/chat/page.tsx +++ b/src/interface/web/app/chat/page.tsx @@ -19,7 +19,11 @@ import { StreamMessage, } from "../components/chatMessage/chatMessage"; import { useIPLocationData, useIsMobileWidth, welcomeConsole } from "../common/utils"; -import { ChatInputArea, ChatOptions } from "../components/chatInputArea/chatInputArea"; +import { + AttachedFileText, + ChatInputArea, + ChatOptions, +} from "../components/chatInputArea/chatInputArea"; import { useAuthenticatedData } from "../common/auth"; import { AgentData } from "../agents/page"; @@ -30,7 +34,7 @@ interface ChatBodyDataProps { setQueryToProcess: (query: string) => void; streamedMessages: StreamMessage[]; setStreamedMessages: (messages: StreamMessage[]) => void; - setUploadedFiles: (files: string[]) => void; + setUploadedFiles: (files: AttachedFileText[] | undefined) => void; isMobileWidth?: boolean; isLoggedIn: boolean; setImages: (images: string[]) => void; @@ -77,6 +81,20 @@ function ChatBodyData(props: ChatBodyDataProps) { setIsInResearchMode(true); } } + + const storedUploadedFiles = localStorage.getItem("uploadedFiles"); + const parsedFiles = storedUploadedFiles ? JSON.parse(storedUploadedFiles) : []; + + const uploadedFiles: AttachedFileText[] = []; + for (const file of parsedFiles) { + uploadedFiles.push({ + name: file.name, + file_type: file.file_type, + content: file.content, + size: file.size, + }); + } + props.setUploadedFiles(uploadedFiles); }, [setQueryToProcess, props.setImages]); useEffect(() => { @@ -100,6 +118,7 @@ function ChatBodyData(props: ChatBodyDataProps) { ) { setProcessingMessage(false); setImages([]); // Reset images after processing + props.setUploadedFiles(undefined); // Reset uploaded files after processing } else { setMessage(""); } @@ -153,7 +172,7 @@ export default function Chat() { const [messages, setMessages] = useState([]); const [queryToProcess, setQueryToProcess] = useState(""); const [processQuerySignal, setProcessQuerySignal] = useState(false); - const [uploadedFiles, setUploadedFiles] = useState([]); + const [uploadedFiles, setUploadedFiles] = useState(undefined); const [images, setImages] = useState([]); const locationData = useIPLocationData() || { @@ -192,6 +211,7 @@ export default function Chat() { timestamp: new Date().toISOString(), rawQuery: queryToProcess || "", images: images, + attachedFiles: uploadedFiles, }; setMessages((prevMessages) => [...prevMessages, newStreamMessage]); setProcessQuerySignal(true); @@ -273,6 +293,7 @@ export default function Chat() { timezone: locationData.timezone, }), ...(images.length > 0 && { images: images }), + ...(uploadedFiles && { files: uploadedFiles }), }; const response = await fetch(chatAPI, { @@ -325,7 +346,7 @@ export default function Chat() {
diff --git a/src/interface/web/app/common/chatFunctions.ts b/src/interface/web/app/common/chatFunctions.ts index a42dde40..3aff7596 100644 --- a/src/interface/web/app/common/chatFunctions.ts +++ b/src/interface/web/app/common/chatFunctions.ts @@ -267,6 +267,58 @@ export async function createNewConversation(slug: string) { } } +export async function packageFilesForUpload(files: FileList): Promise { + const formData = new FormData(); + + const fileReadPromises = Array.from(files).map((file) => { + return new Promise((resolve, reject) => { + let reader = new FileReader(); + reader.onload = function (event) { + if (event.target === null) { + reject(); + return; + } + + let fileContents = event.target.result; + let fileType = file.type; + let fileName = file.name; + if (fileType === "") { + let fileExtension = fileName.split(".").pop(); + if (fileExtension === "org") { + fileType = "text/org"; + } else if (fileExtension === "md") { + fileType = "text/markdown"; + } else if (fileExtension === "txt") { + fileType = "text/plain"; + } else if (fileExtension === "html") { + fileType = "text/html"; + } else if (fileExtension === "pdf") { + fileType = "application/pdf"; + } else { + // Skip this file if its type is not supported + resolve(); + return; + } + } + + if (fileContents === null) { + reject(); + return; + } + + let fileObj = new Blob([fileContents], { type: fileType }); + formData.append("files", fileObj, file.name); + resolve(); + }; + reader.onerror = reject; + reader.readAsArrayBuffer(file); + }); + }); + + await Promise.all(fileReadPromises); + return formData; +} + export function uploadDataForIndexing( files: FileList, setWarning: (warning: string) => void, diff --git a/src/interface/web/app/common/utils.ts b/src/interface/web/app/common/utils.ts index 6c10ba8a..efa2c1d3 100644 --- a/src/interface/web/app/common/utils.ts +++ b/src/interface/web/app/common/utils.ts @@ -71,6 +71,16 @@ export function useIsMobileWidth() { return isMobileWidth; } +export const convertBytesToText = (fileSize: number) => { + if (fileSize < 1024) { + return `${fileSize} B`; + } else if (fileSize < 1024 * 1024) { + return `${(fileSize / 1024).toFixed(2)} KB`; + } else { + return `${(fileSize / (1024 * 1024)).toFixed(2)} MB`; + } +}; + export function useDebounce(value: T, delay: number): T { const [debouncedValue, setDebouncedValue] = useState(value); diff --git a/src/interface/web/app/components/chatInputArea/chatInputArea.tsx b/src/interface/web/app/components/chatInputArea/chatInputArea.tsx index 9f8f8c18..e2e3fd07 100644 --- a/src/interface/web/app/components/chatInputArea/chatInputArea.tsx +++ b/src/interface/web/app/components/chatInputArea/chatInputArea.tsx @@ -40,19 +40,27 @@ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/comp import { convertColorToTextClass, convertToBGClass } from "@/app/common/colorUtils"; import LoginPrompt from "../loginPrompt/loginPrompt"; -import { uploadDataForIndexing } from "../../common/chatFunctions"; import { InlineLoading } from "../loading/loading"; -import { getIconForSlashCommand } from "@/app/common/iconUtils"; +import { getIconForSlashCommand, getIconFromFileType } from "@/app/common/iconUtils"; +import { packageFilesForUpload } from "@/app/common/chatFunctions"; +import { convertBytesToText } from "@/app/common/utils"; export interface ChatOptions { [key: string]: string; } +export interface AttachedFileText { + name: string; + content: string; + file_type: string; + size: number; +} + interface ChatInputProps { sendMessage: (message: string) => void; sendImage: (image: string) => void; sendDisabled: boolean; - setUploadedFiles?: (files: string[]) => void; + setUploadedFiles: (files: AttachedFileText[]) => void; conversationId?: string | null; chatOptionsData?: ChatOptions | null; isMobileWidth?: boolean; @@ -75,6 +83,9 @@ export const ChatInputArea = forwardRef((pr const [imagePaths, setImagePaths] = useState([]); const [imageData, setImageData] = useState([]); + const [attachedFiles, setAttachedFiles] = useState(null); + const [convertedAttachedFiles, setConvertedAttachedFiles] = useState([]); + const [recording, setRecording] = useState(false); const [mediaRecorder, setMediaRecorder] = useState(null); @@ -154,6 +165,8 @@ export const ChatInputArea = forwardRef((pr } props.sendMessage(messageToSend); + setAttachedFiles(null); + setConvertedAttachedFiles([]); setMessage(""); } @@ -203,22 +216,57 @@ export const ChatInputArea = forwardRef((pr setImagePaths((prevPaths) => [...prevPaths, ...newImagePaths]); // Set focus to the input for user message after uploading files chatInputRef?.current?.focus(); - return; } - uploadDataForIndexing( - files, - setWarning, - setUploading, - setError, - props.setUploadedFiles, - props.conversationId, + // Process all non-image files + const nonImageFiles = Array.from(files).filter( + (file) => !image_endings.includes(file.name.split(".").pop() || ""), ); + // Concatenate attachedFiles and files + const newFiles = nonImageFiles + ? Array.from(nonImageFiles).concat(Array.from(attachedFiles || [])) + : Array.from(attachedFiles || []); + + const dataTransfer = new DataTransfer(); + newFiles.forEach((file) => dataTransfer.items.add(file)); + setAttachedFiles(dataTransfer.files); + + // Extract text from files + extractTextFromFiles(dataTransfer.files).then((data) => { + props.setUploadedFiles(data); + setConvertedAttachedFiles(data); + }); + + const totalSize = Array.from(files).reduce((acc, file) => acc + file.size, 0); + const totalSizeInMB = totalSize / (1024 * 1024); + // Set focus to the input for user message after uploading files chatInputRef?.current?.focus(); } + async function extractTextFromFiles(files: FileList): Promise { + const formData = await packageFilesForUpload(files); + setUploading(true); + + try { + const response = await fetch("/api/content/convert", { + method: "POST", + body: formData, + }); + setUploading(false); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + return await response.json(); + } catch (error) { + console.error("Error converting files:", error); + return []; + } + } + // Assuming this function is added within the same context as the provided excerpt async function startRecordingAndTranscribe() { try { @@ -445,6 +493,73 @@ export const ChatInputArea = forwardRef((pr )}
+
+ {imageUploaded && + imagePaths.map((path, index) => ( +
+ {`img-${index}`} + +
+ ))} + {convertedAttachedFiles && + Array.from(convertedAttachedFiles).map((file, index) => ( +
+
+
+ + {file.name} + + + {getIconFromFileType(file.file_type)} + + {convertBytesToText(file.size)} + + +
+
+ +
+ ))} +
((pr onChange={handleFileChange} style={{ display: "none" }} /> +
-
- {imageUploaded && - imagePaths.map((path, index) => ( -
- {`img-${index}`} - -
- ))} -