Skip to content

Commit a37e22c

Browse files
authored
fix: quick ai state synchronous (#693)
* fix: quick ai state synchronous * docs: update notes
1 parent d75ab10 commit a37e22c

4 files changed

Lines changed: 25 additions & 47 deletions

File tree

docs/content.en/docs/release-notes/_index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ Information about release notes of Coco Server is provided here.
1515

1616
### 🐛 Bug fix
1717

18+
- fix: quick ai state synchronous #693
19+
1820
### ✈️ Improvements
21+
1922
- refactor: use author/ext_id as extension unique identifier #643
2023
- refactor: refactoring search api #679
2124
- chore: continue to chat page display #690

src/components/Search/AskAi.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "ahooks";
88
import { useEffect, useRef, useState } from "react";
99
import { noop } from "lodash-es";
10+
import { nanoid } from "nanoid";
1011

1112
import { ChatMessage } from "../ChatMessage";
1213
import { useSearchStore } from "@/stores/searchStore";
@@ -15,7 +16,6 @@ import useMessageChunkData from "@/hooks/useMessageChunkData";
1516
import { useAppStore } from "@/stores/appStore";
1617
import { useExtensionsStore } from "@/stores/extensionsStore";
1718
import { useShortcutsStore } from "@/stores/shortcutsStore";
18-
import { nanoid } from "nanoid";
1919

2020
interface State {
2121
serverId?: string;
@@ -24,12 +24,9 @@ interface State {
2424
}
2525

2626
const AskAi = () => {
27-
const askAiMessage = useSearchStore((state) => state.askAiMessage);
27+
const { askAiMessage, setGoAskAi, setSelectedAssistant, setAskAiSessionId, selectedAssistant, setAskAiServerId, setAskAiAssistantId } = useSearchStore();
28+
2829
const addError = useAppStore((state) => state.addError);
29-
const setGoAskAi = useSearchStore((state) => state.setGoAskAi);
30-
const setSelectedAssistant = useSearchStore((state) => {
31-
return state.setSelectedAssistant;
32-
});
3330

3431
const {
3532
data: {
@@ -62,23 +59,11 @@ const AskAi = () => {
6259

6360
const unlisten = useRef<() => void>(noop);
6461
const sessionIdRef = useRef<string>("");
65-
const setAskAiSessionId = useSearchStore((state) => state.setAskAiSessionId);
66-
const quickAiAccessServer = useExtensionsStore((state) => {
67-
return state.quickAiAccessServer;
68-
});
69-
const quickAiAccessAssistant = useExtensionsStore((state) => {
70-
return state.quickAiAccessAssistant;
71-
});
72-
const selectedAssistant = useSearchStore((state) => {
73-
return state.selectedAssistant;
74-
});
75-
const setAskAiServerId = useSearchStore((state) => {
76-
return state.setAskAiServerId;
77-
});
62+
63+
const { quickAiAccessServer, quickAiAccessAssistant } = useExtensionsStore();
64+
7865
const state = useReactive<State>({});
79-
const setAskAiAssistantId = useSearchStore((state) => {
80-
return state.setAskAiAssistantId;
81-
});
66+
8267
const modifierKey = useShortcutsStore((state) => state.modifierKey);
8368

8469
useEffect(() => {

src/components/Search/AssistantManager.tsx

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export function useAssistantManager({
2626
const { goAskAi, setGoAskAi, setAskAiMessage, selectedAssistant } =
2727
useSearchStore();
2828

29-
const quickAiAccessAssistant = useExtensionsStore(
30-
(state) => state.quickAiAccessAssistant
31-
);
29+
const { quickAiAccessAssistant, disabledExtensions } = useExtensionsStore();
3230

3331
const askAIRef = useRef<Assistant | null>(null);
3432

@@ -41,6 +39,8 @@ export function useAssistantManager({
4139

4240
const assistant_get = useCallback(async () => {
4341
if (!askAI?.id) return;
42+
if (disabledExtensions.includes("QuickAIAccess")) return;
43+
4444
if (isTauri) {
4545
if (!askAI?.querySource?.id) return;
4646
const res = await platformAdapter.commands("assistant_get", {
@@ -56,17 +56,17 @@ export function useAssistantManager({
5656
}
5757
setAssistantDetail(res);
5858
}
59-
}, [askAI]);
59+
}, [askAI?.id, askAI?.querySource?.id, disabledExtensions]);
6060

61-
const handleAskAi = () => {
61+
const handleAskAi = useCallback(() => {
6262
if (!isTauri) return;
6363

64-
askAIRef.current = cloneDeep(askAI);
64+
if (disabledExtensions.includes("QuickAIAccess")) return;
6565

66+
askAIRef.current = cloneDeep(askAI);
6667
if (!askAIRef.current) return;
6768

6869
let value = inputValue.trim();
69-
7070
if (isEmpty(value)) return;
7171

7272
if (!goAskAi && selectedAssistant) {
@@ -76,41 +76,31 @@ export function useAssistantManager({
7676
changeInput("");
7777
setAskAiMessage(value);
7878
setGoAskAi(true);
79-
};
79+
}, [disabledExtensions, askAI, inputValue, goAskAi, selectedAssistant]);
8080

81-
const handleKeyDownAutoResizeTextarea = (
81+
const handleKeyDownAutoResizeTextarea = useCallback((
8282
e: React.KeyboardEvent<HTMLTextAreaElement>
8383
) => {
84-
const { key, shiftKey } = e;
85-
86-
const { value } = e.currentTarget;
84+
const { key, shiftKey, currentTarget } = e;
85+
const { value } = currentTarget;
8786

8887
if (key === "Backspace" && value === "") {
8988
return setGoAskAi(false);
9089
}
9190

92-
if (key === "Tab" && isTauri) {
91+
if (key === "Tab" && !isChatMode && isTauri) {
9392
e.preventDefault();
9493

95-
if (isChatMode) {
96-
return;
97-
}
98-
9994
assistant_get();
100-
10195
return handleAskAi();
10296
}
10397

10498
if (key === "Enter" && !shiftKey && !isChatMode && isTauri) {
10599
e.preventDefault();
106100

107-
if (goAskAi) {
108-
return handleAskAi();
109-
}
110-
111-
handleSubmit();
101+
goAskAi ? handleAskAi() : handleSubmit();
112102
}
113-
};
103+
}, [isChatMode, goAskAi, assistant_get, handleAskAi, handleSubmit]);
114104

115105
return {
116106
askAI,

src/components/Settings/Extensions/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { createContext, useEffect } from "react";
22
import { useMount, useReactive } from "ahooks";
33
import { useTranslation } from "react-i18next";
44
import type { LiteralUnion } from "type-fest";
5+
import { cloneDeep, sortBy } from "lodash-es";
56

67
import platformAdapter from "@/utils/platformAdapter";
78
import Content from "./components/Content";
89
import Details from "./components/Details";
9-
import { cloneDeep, sortBy } from "lodash-es";
1010
import { useExtensionsStore } from "@/stores/extensionsStore";
1111

1212
export type ExtensionId = LiteralUnion<

0 commit comments

Comments
 (0)