From 22baebaf8c49f81bb22c39a7753ac6695d515166 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Wed, 21 Feb 2024 04:19:12 +0700 Subject: [PATCH 1/9] [Cherry Pick] Fix [Utils] Regex trimTopic - [+] fix(utils.ts): update regular expressions in trimTopic function to handle asterisks --- app/utils.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index 33b8eccd2..b502eab36 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -9,8 +9,9 @@ export function trimTopic(topic: string) { // This will remove the specified punctuation from the end of the string // and also trim quotes from both the start and end if they exist. return topic - .replace(/^["“”]+|["“”]+$/g, "") - .replace(/[,。!?”“"、,.!?]*$/, ""); + // fix for gemini + .replace(/^["“”*]+|["“”*]+$/g, "") + .replace(/[,。!?”“"、,.!?*]*$/, ""); } export async function copyToClipboard(text: string) { From aacd26c7db7d87953d14f3c0cf841cf422a2e027 Mon Sep 17 00:00:00 2001 From: Fred Date: Mon, 26 Feb 2024 18:14:10 +0800 Subject: [PATCH 2/9] feat: bump version --- src-tauri/tauri.conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index d88194020..405d267ff 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -9,7 +9,7 @@ }, "package": { "productName": "NextChat", - "version": "2.10.3" + "version": "2.11.2" }, "tauri": { "allowlist": { From 410a22dc634816b13848977d037506fbe2ad4957 Mon Sep 17 00:00:00 2001 From: Fred Date: Tue, 27 Feb 2024 11:43:40 +0800 Subject: [PATCH 3/9] feat: auto detach scrolling --- app/components/chat.tsx | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 369d84c33..ba1e94b9c 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -6,6 +6,7 @@ import React, { useMemo, useCallback, Fragment, + RefObject, } from "react"; import SendWhiteIcon from "../icons/send-white.svg"; @@ -382,11 +383,13 @@ function ChatAction(props: { ); } -function useScrollToBottom() { +function useScrollToBottom( + scrollRef: RefObject, + detach: boolean = false, +) { // for auto-scroll - const scrollRef = useRef(null); - const [autoScroll, setAutoScroll] = useState(true); + const [autoScroll, setAutoScroll] = useState(true); function scrollDomToBottom() { const dom = scrollRef.current; if (dom) { @@ -399,7 +402,7 @@ function useScrollToBottom() { // auto scroll useEffect(() => { - if (autoScroll) { + if (autoScroll && !detach) { scrollDomToBottom(); } }); @@ -658,7 +661,17 @@ function _Chat() { const [userInput, setUserInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const { submitKey, shouldSubmit } = useSubmitHandler(); - const { scrollRef, setAutoScroll, scrollDomToBottom } = useScrollToBottom(); + const scrollRef = useRef(null); + const isScrolledToBottom = scrollRef?.current + ? Math.abs( + scrollRef.current.scrollHeight - + (scrollRef.current.scrollTop + scrollRef.current.clientHeight), + ) <= 1 + : false; + const { setAutoScroll, scrollDomToBottom } = useScrollToBottom( + scrollRef, + isScrolledToBottom, + ); const [hitBottom, setHitBottom] = useState(true); const isMobileScreen = useMobileScreen(); const navigate = useNavigate(); @@ -1003,7 +1016,6 @@ function _Chat() { setHitBottom(isHitBottom); setAutoScroll(isHitBottom); }; - function scrollToBottom() { setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE); scrollDomToBottom(); @@ -1151,6 +1163,13 @@ function _Chat() { )}
+

+ {JSON.stringify({ + scrollHeight: scrollRef?.current?.scrollHeight, + scrollTop: scrollRef?.current?.scrollTop, + clientHeight: scrollRef?.current?.clientHeight, + })} +

setIsEditingMessage(true)} From e3b3ae97bc3edb660104ca8f414ecf021837e3a5 Mon Sep 17 00:00:00 2001 From: Fred Date: Tue, 27 Feb 2024 11:49:44 +0800 Subject: [PATCH 4/9] chore: clear scroll info --- app/components/chat.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index ba1e94b9c..9144f9a5f 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1163,13 +1163,6 @@ function _Chat() { )}
-

- {JSON.stringify({ - scrollHeight: scrollRef?.current?.scrollHeight, - scrollTop: scrollRef?.current?.scrollTop, - clientHeight: scrollRef?.current?.clientHeight, - })} -

setIsEditingMessage(true)} From 08fa22749aea8f497811f684bd9c7ef68d698666 Mon Sep 17 00:00:00 2001 From: fred-bf <157469842+fred-bf@users.noreply.github.com> Date: Tue, 27 Feb 2024 17:28:01 +0800 Subject: [PATCH 5/9] fix: add max_tokens when using vision model (#4157) --- app/client/platforms/openai.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index 919716bfb..437aff582 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -110,6 +110,16 @@ export class ChatGPTApi implements LLMApi { // Please do not ask me why not send max_tokens, no reason, this param is just shit, I dont want to explain anymore. }; + // add max_tokens to vision model + if (visionModel) { + Object.defineProperty(requestPayload, "max_tokens", { + enumerable: true, + configurable: true, + writable: true, + value: Math.max(modelConfig.max_tokens, 4096), + }); + } + console.log("[Request] openai payload: ", requestPayload); const shouldStream = !!options.config.stream; From 43e5dc22920c60bf87fc1b78bf95c441356bb1d8 Mon Sep 17 00:00:00 2001 From: Fred Date: Wed, 28 Feb 2024 11:33:43 +0800 Subject: [PATCH 6/9] fix: fix the method to detect vision model --- app/client/platforms/openai.ts | 2 +- app/utils.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index 437aff582..629158843 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -116,7 +116,7 @@ export class ChatGPTApi implements LLMApi { enumerable: true, configurable: true, writable: true, - value: Math.max(modelConfig.max_tokens, 4096), + value: modelConfig.max_tokens, }); } diff --git a/app/utils.ts b/app/utils.ts index 33b8eccd2..0436a128a 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -292,8 +292,8 @@ export function getMessageImages(message: RequestMessage): string[] { export function isVisionModel(model: string) { return ( - model.startsWith("gpt-4-vision") || - model.startsWith("gemini-pro-vision") || - !DEFAULT_MODELS.find((m) => m.name == model) + // model.startsWith("gpt-4-vision") || + // model.startsWith("gemini-pro-vision") || + model.includes("vision") ); } From bd19e97cf84755e4ac20c731bae292c2a09b76b7 Mon Sep 17 00:00:00 2001 From: Snow Kawashiro Date: Wed, 28 Feb 2024 20:05:13 +0800 Subject: [PATCH 7/9] add_image_pasting --- app/components/chat.tsx | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 9144f9a5f..22acb8e4f 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1100,6 +1100,45 @@ function _Chat() { }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + + const handlePaste = useCallback( + async (event: React.ClipboardEvent) => { + const items = (event.clipboardData || window.clipboardData).items; + for (const item of items) { + if (item.kind === "file" && item.type.startsWith("image/")) { + event.preventDefault(); + const file = item.getAsFile(); + if (file) { + const images: string[] = []; + images.push(...attachImages); + images.push( + ...(await new Promise((res, rej) => { + setUploading(true); + const imagesData: string[] = []; + compressImage(file, 256 * 1024) + .then((dataUrl) => { + imagesData.push(dataUrl); + setUploading(false); + res(imagesData); + }) + .catch((e) => { + setUploading(false); + rej(e); + }); + })), + ); + const imagesLength = images.length; + + if (imagesLength > 3) { + images.splice(3, imagesLength - 3); + } + setAttachImages(images); + } + } + } + }, + [attachImages], + ); async function uploadImage() { const images: string[] = []; @@ -1449,6 +1488,7 @@ function _Chat() { onKeyDown={onInputKeyDown} onFocus={scrollToBottom} onClick={scrollToBottom} + onPaste={handlePaste} rows={inputRows} autoFocus={autoFocus} style={{ From e7051353eb8aff0e89a5e0a5da13cfcc5bcb4b6f Mon Sep 17 00:00:00 2001 From: Snow Kawashiro Date: Wed, 28 Feb 2024 20:38:00 +0800 Subject: [PATCH 8/9] vision_model_only --- app/components/chat.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 22acb8e4f..d730a4a10 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1102,6 +1102,8 @@ function _Chat() { }, []); const handlePaste = useCallback( + const currentModel = chatStore.currentSession().mask.modelConfig.model; + if(!isVisionModel(currentModel)){return;} async (event: React.ClipboardEvent) => { const items = (event.clipboardData || window.clipboardData).items; for (const item of items) { @@ -1137,7 +1139,7 @@ function _Chat() { } } }, - [attachImages], + [attachImages, chatStore], ); async function uploadImage() { From 9775660da7a7fd6b9edc616c42def0dc69b534d4 Mon Sep 17 00:00:00 2001 From: Snow Kawashiro Date: Wed, 28 Feb 2024 20:45:42 +0800 Subject: [PATCH 9/9] Update chat.tsx --- app/components/chat.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index d730a4a10..bcd0e605d 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -1102,9 +1102,9 @@ function _Chat() { }, []); const handlePaste = useCallback( - const currentModel = chatStore.currentSession().mask.modelConfig.model; - if(!isVisionModel(currentModel)){return;} async (event: React.ClipboardEvent) => { + const currentModel = chatStore.currentSession().mask.modelConfig.model; + if(!isVisionModel(currentModel)){return;} const items = (event.clipboardData || window.clipboardData).items; for (const item of items) { if (item.kind === "file" && item.type.startsWith("image/")) {