From 83f948239cf7dea4ddc62ed02cbb742e2c6ec9f7 Mon Sep 17 00:00:00 2001 From: dakai Date: Sat, 5 Oct 2024 19:05:07 +0800 Subject: [PATCH] style: fix formatting issues in chat.tsx --- app/components/chat.tsx | 2 +- app/store/chat.ts | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 416c76e1c..eee63faab 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1038,7 +1038,7 @@ function _Chat() { } setIsLoading(true); chatStore - .onUserInput(userInput, attachImages) + .onUserInput(userInput, attachImages, attachFiles) .then(() => setIsLoading(false)); setAttachImages([]); chatStore.setLastInput(userInput); diff --git a/app/store/chat.ts b/app/store/chat.ts index 86de99836..4e6f09304 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -153,6 +153,21 @@ function fillTemplateWith(input: string, modelConfig: ModelConfig) { return output; } +const readFileContent = async (url: string): Promise => { + try { + const response = await fetch(url); + if (!response.ok) { + throw new Error( + `Failed to fetch content from ${url}: ${response.statusText}`, + ); + } + return await response.text(); + } catch (error) { + console.error("Error reading file content:", error); + return ""; + } +}; + const DEFAULT_CHAT_STATE = { sessions: [createEmptySession()], currentSessionIndex: 0, @@ -326,10 +341,22 @@ export const useChatStore = createPersistStore( get().summarizeSession(); }, - async onUserInput(content: string, attachImages?: string[]) { + async onUserInput( + content: string, + attachImages?: string[], + attachFiles?: string[], + ) { const session = get().currentSession(); const modelConfig = session.mask.modelConfig; + //read file content from the url + if (attachFiles && attachFiles.length > 0) { + content += " file content: \n"; + for (let i = 0; i < attachFiles.length; i++) { + content += await readFileContent(attachFiles[i]); + } + } + const userContent = fillTemplateWith(content, modelConfig); console.log("[User Input] after template: ", userContent);