Skip to content

Commit 2dd8e31

Browse files
authored
fix: resolved navigation error on continue chat action (#558)
* fix: resolved navigation error on continue chat action * docs: update changelog
1 parent 6aeecfe commit 2dd8e31

6 files changed

Lines changed: 79 additions & 30 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Information about release notes of Coco Server is provided here.
4646
- fix: the scroll button is not displayed by default #552
4747
- fix: suggestion list position #553
4848
- fix: independent chat window has no data #554
49+
- fix: resolved navigation error on continue chat action #558
4950

5051
### ✈️ Improvements
5152

src/components/Assistant/Chat.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,25 @@ const ChatAI = memo(
9696
const setAskAiSessionId = useSearchStore(
9797
(state) => state.setAskAiSessionId
9898
);
99+
const askAiServerId = useSearchStore((state) => {
100+
return state.askAiServerId;
101+
});
99102

100103
useEffect(() => {
101104
activeChatProp && setActiveChat(activeChatProp);
102105
}, [activeChatProp]);
103106

104107
useEffect(() => {
105-
console.log("chats", chats);
106-
console.log("askAiSessionId", askAiSessionId);
107-
if (chats.length === 0 || !askAiSessionId) return;
108+
if (askAiServerId || !askAiSessionId || chats.length === 0) return;
108109

109110
const matched = chats.find((item) => item._id === askAiSessionId);
110111

111-
console.log("matched", matched);
112-
113112
if (matched) {
114113
onSelectChat(matched);
115114

116115
setAskAiSessionId(void 0);
117116
}
118-
}, [chats, chats]);
117+
}, [chats, askAiSessionId, askAiServerId]);
119118

120119
const [Question, setQuestion] = useState<string>("");
121120

src/components/Assistant/ServerList.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@ import { useConnectStore } from "@/stores/connectStore";
1616
import { Server as IServer } from "@/types/server";
1717
import StatusIndicator from "@/components/Cloud/StatusIndicator";
1818
import { useAuthStore } from "@/stores/authStore";
19+
import { useSearchStore } from "@/stores/searchStore";
1920

2021
interface ServerListProps {
2122
reconnect: (server?: IServer) => void;
2223
clearChat: () => void;
2324
}
2425

25-
export function ServerList({
26-
reconnect,
27-
clearChat,
28-
}: ServerListProps) {
26+
export function ServerList({ reconnect, clearChat }: ServerListProps) {
2927
const { t } = useTranslation();
3028

31-
const isCurrentLogin = useAuthStore(state => state.isCurrentLogin);
32-
const setIsCurrentLogin = useAuthStore(state => state.setIsCurrentLogin);
29+
const isCurrentLogin = useAuthStore((state) => state.isCurrentLogin);
30+
const setIsCurrentLogin = useAuthStore((state) => state.setIsCurrentLogin);
3331
const serviceList = useShortcutsStore((state) => state.serviceList);
3432
const setEndpoint = useAppStore((state) => state.setEndpoint);
3533
const setCurrentService = useConnectStore((state) => state.setCurrentService);
@@ -40,7 +38,12 @@ export function ServerList({
4038

4139
const [serverList, setServerList] = useState<IServer[]>([]);
4240
const [isRefreshing, setIsRefreshing] = useState(false);
43-
41+
const askAiServerId = useSearchStore((state) => {
42+
return state.askAiServerId;
43+
});
44+
const setAskAiServerId = useSearchStore((state) => {
45+
return state.setAskAiServerId;
46+
});
4447
const serverListButtonRef = useRef<HTMLButtonElement>(null);
4548

4649
const fetchServers = useCallback(
@@ -73,6 +76,19 @@ export function ServerList({
7376
[currentService?.id]
7477
);
7578

79+
useEffect(() => {
80+
if (!askAiServerId || serverList.length === 0) return;
81+
82+
const matched = serverList.find((server) => {
83+
return server.id === askAiServerId;
84+
});
85+
86+
if (!matched) return;
87+
88+
switchServer(matched);
89+
setAskAiServerId(void 0);
90+
}, [serverList, askAiServerId]);
91+
7692
useEffect(() => {
7793
if (!isTauri) return;
7894

@@ -109,7 +125,7 @@ export function ServerList({
109125
setCurrentService(server);
110126
setEndpoint(server.endpoint);
111127
setMessages(""); // Clear previous messages
112-
clearChat();
128+
clearChat();
113129
//
114130
if (!server.public && !server.profile) {
115131
setIsCurrentLogin(false);
@@ -217,7 +233,7 @@ export function ServerList({
217233
{server.name}
218234
</div>
219235
<div className="text-xs text-gray-500 dark:text-gray-400 truncate max-w-[200px]">
220-
AI Assistant: { server.stats?.assistant_count || 1}
236+
AI Assistant: {server.stats?.assistant_count || 1}
221237
</div>
222238
</div>
223239
</div>

src/components/Search/AskAi.tsx

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { useAsyncEffect, useKeyPress, useMount, useUnmount } from "ahooks";
1+
import {
2+
useAsyncEffect,
3+
useKeyPress,
4+
useMount,
5+
useReactive,
6+
useUnmount,
7+
} from "ahooks";
28
import { useEffect, useRef, useState } from "react";
39

410
import { ChatMessage } from "../ChatMessage";
511
import { ASK_AI_CLIENT_ID, COPY_BUTTON_ID } from "@/constants";
6-
import { useConnectStore } from "@/stores/connectStore";
712
import { useSearchStore } from "@/stores/searchStore";
813
import platformAdapter from "@/utils/platformAdapter";
914
import useMessageChunkData from "@/hooks/useMessageChunkData";
@@ -12,9 +17,13 @@ import { isMac } from "@/utils/platform";
1217
import { noop } from "lodash-es";
1318
import { useExtensionsStore } from "@/stores/extensionsStore";
1419

20+
interface State {
21+
serverId?: string;
22+
assistantId?: string;
23+
}
24+
1525
const AskAi = () => {
1626
const askAiMessage = useSearchStore((state) => state.askAiMessage);
17-
const currentService = useConnectStore((state) => state.currentService);
1827
const addError = useAppStore((state) => state.addError);
1928
const setGoAskAi = useSearchStore((state) => state.setGoAskAi);
2029
const setSelectedAssistant = useSearchStore((state) => {
@@ -62,12 +71,27 @@ const AskAi = () => {
6271
const selectedAssistant = useSearchStore((state) => {
6372
return state.selectedAssistant;
6473
});
65-
const assistantRef = useRef<any>(null);
74+
const setAskAiServerId = useSearchStore((state) => {
75+
return state.setAskAiServerId;
76+
});
77+
const state = useReactive<State>({});
6678

6779
useEffect(() => {
68-
if (!quickAiAccessAssistant) return;
80+
if (state.serverId) return;
81+
82+
const server = selectedAssistant
83+
? selectedAssistant.querySource
84+
: quickAiAccessServer;
6985

70-
assistantRef.current = selectedAssistant ?? quickAiAccessAssistant;
86+
state.serverId = server?.id;
87+
}, [selectedAssistant, quickAiAccessAssistant]);
88+
89+
useEffect(() => {
90+
if (state.assistantId) return;
91+
92+
const assistant = selectedAssistant ?? quickAiAccessAssistant;
93+
94+
state.assistantId = assistant?.id;
7195
}, [selectedAssistant, quickAiAccessAssistant]);
7296

7397
useMount(async () => {
@@ -134,37 +158,43 @@ const AskAi = () => {
134158
});
135159

136160
useAsyncEffect(async () => {
137-
if (!askAiMessage || !currentService?.id || !assistantRef.current) return;
161+
if (!askAiMessage || !state.serverId || !state.assistantId) return;
138162

139163
clearAllChunkData();
140164

165+
const { serverId, assistantId } = state;
166+
167+
console.log("serverId", serverId);
168+
console.log("assistantId", assistantId);
169+
141170
try {
142171
await platformAdapter.invokeBackend("ask_ai", {
143172
message: askAiMessage,
144-
serverId: selectedAssistant
145-
? selectedAssistant.querySource.id
146-
: quickAiAccessServer.id,
147-
assistantId: assistantRef.current.id,
173+
serverId,
174+
assistantId,
148175
clientId: ASK_AI_CLIENT_ID,
149176
});
150177
} catch (error) {
151178
addError(String(error));
152179
}
153-
}, [askAiMessage, assistantRef]);
180+
}, [askAiMessage, state.serverId, state.assistantId]);
154181

155182
useKeyPress("enter", async (event) => {
156183
const { metaKey, ctrlKey } = event;
157184

158185
if (isTyping) return;
159186

187+
const { serverId } = state;
188+
160189
if ((isMac && metaKey) || (!isMac && ctrlKey)) {
161190
await platformAdapter.commands("open_session_chat", {
162-
serverId: currentService?.id,
191+
serverId,
163192
sessionId: sessionIdRef.current,
164193
});
165194

166195
platformAdapter.emitEvent("toggle-to-chat-mode");
167196

197+
setAskAiServerId(serverId);
168198
return setAskAiSessionId(sessionIdRef.current);
169199
}
170200

src/hooks/useSyncStore.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ export const useSyncStore = () => {
176176
platformAdapter.listenEvent("change-extensions-store", ({ payload }) => {
177177
const { quickAiAccessServer, quickAiAccessAssistant } = payload;
178178

179-
console.log("quickAiAccessAssistant", quickAiAccessAssistant);
180-
181179
setQuickAiAccessServer(quickAiAccessServer);
182180
setQuickAiAccessAssistant(quickAiAccessAssistant);
183181
}),

src/stores/searchStore.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export type ISearchStore = {
2222
setAskAiSessionId: (askAiSessionId?: string) => void;
2323
selectedAssistant?: any;
2424
setSelectedAssistant: (selectedAssistant?: any) => void;
25+
askAiServerId?: string;
26+
setAskAiServerId: (askAiServerId?: string) => void;
2527
};
2628

2729
export const useSearchStore = create<ISearchStore>()(
@@ -54,6 +56,9 @@ export const useSearchStore = create<ISearchStore>()(
5456
setSelectedAssistant: (selectedAssistant) => {
5557
return set({ selectedAssistant });
5658
},
59+
setAskAiServerId: (askAiServerId) => {
60+
return set({ askAiServerId });
61+
},
5762
}),
5863
{
5964
name: "search-store",

0 commit comments

Comments
 (0)