Skip to content

Commit 4a5a4da

Browse files
authored
fix: fix ai extension assistant list fetch (#890)
* fix: fix ai extension assistant list fetch * refactor: update * refactor: update * refactor: update
1 parent efaaf73 commit 4a5a4da

File tree

12 files changed

+83
-55
lines changed

12 files changed

+83
-55
lines changed

src/commands/servers.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
MultiSourceQueryResponse,
1616
} from "@/types/commands";
1717
import { useAppStore } from "@/stores/appStore";
18-
import { useAuthStore } from "@/stores/authStore";
1918
import {
2019
getCurrentWindowService,
2120
handleLogout,
@@ -39,16 +38,9 @@ async function invokeWithErrorHandler<T>(
3938
command: string,
4039
args?: Record<string, any>
4140
): Promise<T> {
42-
const isCurrentLogin = useAuthStore.getState().isCurrentLogin;
43-
4441
const service = await getCurrentWindowService();
4542

46-
// Not logged in
47-
// console.log("isCurrentLogin", command, isCurrentLogin);
48-
if (
49-
!WHITELIST_SERVERS.includes(command) &&
50-
(!isCurrentLogin || !service?.profile)
51-
) {
43+
if (!WHITELIST_SERVERS.includes(command) && !service?.profile) {
5244
console.error("This command requires authentication");
5345
throw new Error("This command requires authentication");
5446
}

src/commands/windowService.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useConnectStore } from "@/stores/connectStore";
22
import { SETTINGS_WINDOW_LABEL } from "@/constants";
33
import platformAdapter from "@/utils/platformAdapter";
44
import { useAuthStore } from "@/stores/authStore";
5+
import { useExtensionsStore } from "@/stores/extensionsStore";
56

67
export async function getCurrentWindowService() {
78
const currentService = useConnectStore.getState().currentService;
@@ -13,23 +14,42 @@ export async function getCurrentWindowService() {
1314
: currentService;
1415
}
1516

16-
export async function setCurrentWindowService(
17-
service: any,
18-
isAll?: boolean
19-
) {
17+
export async function setCurrentWindowService(service: any, isAll?: boolean) {
2018
const { setCurrentService, setCloudSelectService } =
2119
useConnectStore.getState();
2220
// all refresh logout
2321
if (isAll) {
2422
setCloudSelectService(service);
25-
setCurrentService(service);
26-
return;
23+
return setCurrentService(service);
2724
}
2825
// current refresh
2926
const windowLabel = await platformAdapter.getCurrentWindowLabel();
30-
return windowLabel === SETTINGS_WINDOW_LABEL
31-
? setCloudSelectService(service)
32-
: setCurrentService(service);
27+
28+
if (windowLabel === SETTINGS_WINDOW_LABEL) {
29+
const { currentService } = useConnectStore.getState();
30+
const {
31+
aiOverviewServer,
32+
setAiOverviewServer,
33+
quickAiAccessServer,
34+
setQuickAiAccessServer,
35+
} = useExtensionsStore.getState();
36+
37+
if (currentService?.id === service.id) {
38+
setCurrentService(service);
39+
}
40+
41+
if (aiOverviewServer?.id === service.id) {
42+
setAiOverviewServer(service);
43+
}
44+
45+
if (quickAiAccessServer?.id === service.id) {
46+
setQuickAiAccessServer(service);
47+
}
48+
49+
return setCloudSelectService(service);
50+
}
51+
52+
return setCurrentService(service);
3353
}
3454

3555
export async function handleLogout(serverId?: string) {

src/components/Assistant/AssistantFetcher.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const AssistantFetcher = ({
2525
query?: string;
2626
}) => {
2727
try {
28-
if (unrequitable()) {
28+
if (await unrequitable()) {
2929
return {
3030
total: 0,
3131
list: [],

src/components/Assistant/Splash.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const Splash = ({ assistantIDs = [], startPage }: SplashProps) => {
5454

5555
let response: any;
5656
if (isTauri) {
57-
if (unrequitable()) {
57+
if (await unrequitable()) {
5858
return setVisibleStartPage(false);
5959
}
6060

@@ -72,6 +72,8 @@ const Splash = ({ assistantIDs = [], startPage }: SplashProps) => {
7272
setSettings(response);
7373
};
7474

75+
console.log("currentService", currentService);
76+
7577
useEffect(() => {
7678
getSettings();
7779
fetchData();

src/components/ChatMessage/MessageActions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const MessageActions = ({
6767
};
6868

6969
const handleSpeak = async () => {
70-
if (isDefaultServer()) {
70+
if (await isDefaultServer()) {
7171
return setSynthesizeItem({ id, content });
7272
}
7373

src/components/Search/InputBox.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
22
import { useTranslation } from "react-i18next";
3-
import { useKeyPress, useSize } from "ahooks";
3+
import { useAsyncEffect, useKeyPress, useSize } from "ahooks";
44
import clsx from "clsx";
55

66
import AutoResizeTextarea from "./AutoResizeTextarea";
@@ -210,8 +210,8 @@ export default function ChatInput({
210210
const extraIconRef = useRef<HTMLDivElement>(null);
211211
const extraIconSize = useSize(extraIconRef);
212212

213-
useEffect(() => {
214-
setVisibleAudioInput(isDefaultServer());
213+
useAsyncEffect(async () => {
214+
setVisibleAudioInput(await isDefaultServer());
215215
}, [currentService]);
216216

217217
const renderSearchIcon = () => (

src/components/Settings/Extensions/components/Details/SharedAi/index.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, useMemo, useState, useCallback } from "react";
1+
import { FC, useMemo, useState, useCallback, useEffect } from "react";
22
import { useTranslation } from "react-i18next";
33
import { isArray } from "lodash-es";
44
import { useAsyncEffect, useMount } from "ahooks";
@@ -37,6 +37,13 @@ const SharedAi: FC<SharedAiProps> = (props) => {
3737
const { t } = useTranslation();
3838
const [assistantSearchValue, setAssistantSearchValue] = useState("");
3939
const [isLoadingAssistants, setIsLoadingAssistants] = useState(false);
40+
const { setCloudSelectService } = useConnectStore();
41+
42+
useEffect(() => {
43+
if (!server) return;
44+
45+
setCloudSelectService(server);
46+
}, [server]);
4047

4148
const getEnabledServers = useCallback((servers: Server[]): Server[] => {
4249
if (!isArray(servers)) return [];
@@ -56,7 +63,9 @@ const SharedAi: FC<SharedAiProps> = (props) => {
5663
}
5764

5865
if (server) {
59-
const matchServer = enabledServers.find((item) => item.id === server.id);
66+
const matchServer = enabledServers.find(
67+
(item) => item.id === server.id
68+
);
6069
if (matchServer) {
6170
setServer(matchServer);
6271
return;
@@ -65,7 +74,7 @@ const SharedAi: FC<SharedAiProps> = (props) => {
6574

6675
setServer(enabledServers[0]);
6776
} catch (error) {
68-
console.error('Failed to load servers:', error);
77+
console.error("Failed to load servers:", error);
6978
addError(`Failed to load servers: ${String(error)}`);
7079
}
7180
});
@@ -86,7 +95,9 @@ const SharedAi: FC<SharedAiProps> = (props) => {
8695
query: assistantSearchValue,
8796
});
8897

89-
const assistants: Assistant[] = data.list.map((item: any) => item._source);
98+
const assistants: Assistant[] = data.list.map(
99+
(item: any) => item._source
100+
);
90101
setAssistantList(assistants);
91102

92103
if (assistants.length === 0) {
@@ -104,7 +115,7 @@ const SharedAi: FC<SharedAiProps> = (props) => {
104115

105116
setAssistant(assistants[0]);
106117
} catch (error) {
107-
console.error('Failed to fetch assistants:', error);
118+
console.error("Failed to fetch assistants:", error);
108119
addError(`Failed to fetch assistants: ${String(error)}`);
109120
setAssistantList([]);
110121
setAssistant(undefined);
@@ -181,7 +192,9 @@ const SharedAi: FC<SharedAiProps> = (props) => {
181192
searchable={searchable}
182193
onChange={onChange}
183194
onSearch={onSearch}
184-
placeholder={isLoadingAssistants && searchable ? "Loading..." : undefined}
195+
placeholder={
196+
isLoadingAssistants && searchable ? "Loading..." : undefined
197+
}
185198
/>
186199
</div>
187200
);

src/hooks/useChatActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ export function useChatActions(
471471
const getChatHistory = useCallback(async () => {
472472
let response: any;
473473
if (isTauri) {
474-
if (unrequitable()) {
474+
if (await unrequitable()) {
475475
return setChats([]);
476476
}
477477

src/hooks/useSyncStore.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const useSyncStore = () => {
117117
const setShowTooltip = useAppStore((state) => state.setShowTooltip);
118118
const setEndpoint = useAppStore((state) => state.setEndpoint);
119119
const setLanguage = useAppStore((state) => state.setLanguage);
120+
const { setCurrentService } = useConnectStore();
120121

121122
useEffect(() => {
122123
if (!resetFixedWindow) {
@@ -180,15 +181,20 @@ export const useSyncStore = () => {
180181
}),
181182

182183
platformAdapter.listenEvent("change-connect-store", ({ payload }) => {
183-
const { connectionTimeout, querySourceTimeout, allowSelfSignature } =
184-
payload;
184+
const {
185+
connectionTimeout,
186+
querySourceTimeout,
187+
allowSelfSignature,
188+
currentService,
189+
} = payload;
185190
if (isNumber(connectionTimeout)) {
186191
setConnectionTimeout(connectionTimeout);
187192
}
188193
if (isNumber(querySourceTimeout)) {
189194
setQueryTimeout(querySourceTimeout);
190195
}
191196
setAllowSelfSignature(allowSelfSignature);
197+
setCurrentService(currentService);
192198
}),
193199

194200
platformAdapter.listenEvent("change-appearance-store", ({ payload }) => {

src/pages/chat/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default function StandaloneChat({}: StandaloneChatProps) {
6666

6767
const getChatHistory = async () => {
6868
try {
69-
if (unrequitable()) {
69+
if (await unrequitable()) {
7070
return setChats([]);
7171
}
7272

@@ -271,7 +271,11 @@ export default function StandaloneChat({}: StandaloneChatProps) {
271271
const handleDelete = async (id: string) => {
272272
if (!currentService?.id) return;
273273

274-
await platformAdapter.commands("delete_session_chat", currentService.id, id);
274+
await platformAdapter.commands(
275+
"delete_session_chat",
276+
currentService.id,
277+
id
278+
);
275279
};
276280

277281
return (

0 commit comments

Comments
 (0)