From a37e22c2276140003877b756d348dc5f6665bb74 Mon Sep 17 00:00:00 2001 From: BiggerRain <15911122312@163.COM> Date: Tue, 17 Jun 2025 15:38:39 +0800 Subject: [PATCH] fix: quick ai state synchronous (#693) * fix: quick ai state synchronous * docs: update notes --- docs/content.en/docs/release-notes/_index.md | 3 ++ src/components/Search/AskAi.tsx | 29 ++++----------- src/components/Search/AssistantManager.tsx | 38 ++++++++------------ src/components/Settings/Extensions/index.tsx | 2 +- 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/docs/content.en/docs/release-notes/_index.md b/docs/content.en/docs/release-notes/_index.md index b73462f5..a9c123af 100644 --- a/docs/content.en/docs/release-notes/_index.md +++ b/docs/content.en/docs/release-notes/_index.md @@ -15,7 +15,10 @@ Information about release notes of Coco Server is provided here. ### 🐛 Bug fix +- fix: quick ai state synchronous #693 + ### ✈️ Improvements + - refactor: use author/ext_id as extension unique identifier #643 - refactor: refactoring search api #679 - chore: continue to chat page display #690 diff --git a/src/components/Search/AskAi.tsx b/src/components/Search/AskAi.tsx index 11f1c3f1..2edbf437 100644 --- a/src/components/Search/AskAi.tsx +++ b/src/components/Search/AskAi.tsx @@ -7,6 +7,7 @@ import { } from "ahooks"; import { useEffect, useRef, useState } from "react"; import { noop } from "lodash-es"; +import { nanoid } from "nanoid"; import { ChatMessage } from "../ChatMessage"; import { useSearchStore } from "@/stores/searchStore"; @@ -15,7 +16,6 @@ import useMessageChunkData from "@/hooks/useMessageChunkData"; import { useAppStore } from "@/stores/appStore"; import { useExtensionsStore } from "@/stores/extensionsStore"; import { useShortcutsStore } from "@/stores/shortcutsStore"; -import { nanoid } from "nanoid"; interface State { serverId?: string; @@ -24,12 +24,9 @@ interface State { } const AskAi = () => { - const askAiMessage = useSearchStore((state) => state.askAiMessage); + const { askAiMessage, setGoAskAi, setSelectedAssistant, setAskAiSessionId, selectedAssistant, setAskAiServerId, setAskAiAssistantId } = useSearchStore(); + const addError = useAppStore((state) => state.addError); - const setGoAskAi = useSearchStore((state) => state.setGoAskAi); - const setSelectedAssistant = useSearchStore((state) => { - return state.setSelectedAssistant; - }); const { data: { @@ -62,23 +59,11 @@ const AskAi = () => { const unlisten = useRef<() => void>(noop); const sessionIdRef = useRef(""); - const setAskAiSessionId = useSearchStore((state) => state.setAskAiSessionId); - const quickAiAccessServer = useExtensionsStore((state) => { - return state.quickAiAccessServer; - }); - const quickAiAccessAssistant = useExtensionsStore((state) => { - return state.quickAiAccessAssistant; - }); - const selectedAssistant = useSearchStore((state) => { - return state.selectedAssistant; - }); - const setAskAiServerId = useSearchStore((state) => { - return state.setAskAiServerId; - }); + + const { quickAiAccessServer, quickAiAccessAssistant } = useExtensionsStore(); + const state = useReactive({}); - const setAskAiAssistantId = useSearchStore((state) => { - return state.setAskAiAssistantId; - }); + const modifierKey = useShortcutsStore((state) => state.modifierKey); useEffect(() => { diff --git a/src/components/Search/AssistantManager.tsx b/src/components/Search/AssistantManager.tsx index 628504ff..e279b363 100644 --- a/src/components/Search/AssistantManager.tsx +++ b/src/components/Search/AssistantManager.tsx @@ -26,9 +26,7 @@ export function useAssistantManager({ const { goAskAi, setGoAskAi, setAskAiMessage, selectedAssistant } = useSearchStore(); - const quickAiAccessAssistant = useExtensionsStore( - (state) => state.quickAiAccessAssistant - ); + const { quickAiAccessAssistant, disabledExtensions } = useExtensionsStore(); const askAIRef = useRef(null); @@ -41,6 +39,8 @@ export function useAssistantManager({ const assistant_get = useCallback(async () => { if (!askAI?.id) return; + if (disabledExtensions.includes("QuickAIAccess")) return; + if (isTauri) { if (!askAI?.querySource?.id) return; const res = await platformAdapter.commands("assistant_get", { @@ -56,17 +56,17 @@ export function useAssistantManager({ } setAssistantDetail(res); } - }, [askAI]); + }, [askAI?.id, askAI?.querySource?.id, disabledExtensions]); - const handleAskAi = () => { + const handleAskAi = useCallback(() => { if (!isTauri) return; - askAIRef.current = cloneDeep(askAI); + if (disabledExtensions.includes("QuickAIAccess")) return; + askAIRef.current = cloneDeep(askAI); if (!askAIRef.current) return; let value = inputValue.trim(); - if (isEmpty(value)) return; if (!goAskAi && selectedAssistant) { @@ -76,41 +76,31 @@ export function useAssistantManager({ changeInput(""); setAskAiMessage(value); setGoAskAi(true); - }; + }, [disabledExtensions, askAI, inputValue, goAskAi, selectedAssistant]); - const handleKeyDownAutoResizeTextarea = ( + const handleKeyDownAutoResizeTextarea = useCallback(( e: React.KeyboardEvent ) => { - const { key, shiftKey } = e; - - const { value } = e.currentTarget; + const { key, shiftKey, currentTarget } = e; + const { value } = currentTarget; if (key === "Backspace" && value === "") { return setGoAskAi(false); } - if (key === "Tab" && isTauri) { + if (key === "Tab" && !isChatMode && isTauri) { e.preventDefault(); - if (isChatMode) { - return; - } - assistant_get(); - return handleAskAi(); } if (key === "Enter" && !shiftKey && !isChatMode && isTauri) { e.preventDefault(); - if (goAskAi) { - return handleAskAi(); - } - - handleSubmit(); + goAskAi ? handleAskAi() : handleSubmit(); } - }; + }, [isChatMode, goAskAi, assistant_get, handleAskAi, handleSubmit]); return { askAI, diff --git a/src/components/Settings/Extensions/index.tsx b/src/components/Settings/Extensions/index.tsx index f786f766..629cef8b 100644 --- a/src/components/Settings/Extensions/index.tsx +++ b/src/components/Settings/Extensions/index.tsx @@ -2,11 +2,11 @@ import { createContext, useEffect } from "react"; import { useMount, useReactive } from "ahooks"; import { useTranslation } from "react-i18next"; import type { LiteralUnion } from "type-fest"; +import { cloneDeep, sortBy } from "lodash-es"; import platformAdapter from "@/utils/platformAdapter"; import Content from "./components/Content"; import Details from "./components/Details"; -import { cloneDeep, sortBy } from "lodash-es"; import { useExtensionsStore } from "@/stores/extensionsStore"; export type ExtensionId = LiteralUnion<