Skip to content

Commit 4431f8f

Browse files
authored
feat: add endpoint setting (#96)
* feat: add endpoint setting * feat: add endpoint change * feat: add baseURL config * feat: add port 9000
1 parent cc20245 commit 4431f8f

File tree

13 files changed

+158
-18
lines changed

13 files changed

+158
-18
lines changed

src/api/request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { fetch as tauriFetchModule } from "@tauri-apps/plugin-http";
33

44
import { clientEnv } from "@/utils/env";
55

6-
const baseURL = `${clientEnv.COCO_SERVER_URL}`
7-
86
// Use a conditional fetch depending on whether it's in a Tauri environment or web
97
let customFetch: typeof window.fetch | typeof tauriFetchModule = window.fetch;
108

@@ -19,6 +17,7 @@ interface FetchRequestConfig {
1917
body?: any;
2018
timeout?: number;
2119
parseAs?: "json" | "text" | "binary";
20+
baseURL?: string;
2221
}
2322

2423
interface FetchResponse<T = any> {
@@ -43,6 +42,7 @@ export const tauriFetch = async <T = any>({
4342
body,
4443
timeout = 30,
4544
parseAs = "json",
45+
baseURL = clientEnv.COCO_SERVER_URL
4646
}: FetchRequestConfig): Promise<FetchResponse<T>> => {
4747
try {
4848
url = baseURL + url;

src/api/tauriFetchClient.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { fetch } from "@tauri-apps/plugin-http";
22

33
import { clientEnv } from "@/utils/env";
44

5-
const baseURL = `${clientEnv.COCO_SERVER_URL}`
6-
75
interface FetchRequestConfig {
86
url: string;
97
method?: "GET" | "POST" | "PUT" | "DELETE";
108
headers?: Record<string, string>;
119
body?: any;
1210
timeout?: number;
1311
parseAs?: "json" | "text" | "binary";
12+
baseURL?: string;
1413
}
1514

1615
interface FetchResponse<T = any> {
@@ -33,7 +32,10 @@ export const tauriFetch = async <T = any>({
3332
body,
3433
timeout = 30,
3534
parseAs = "json",
35+
baseURL = clientEnv.COCO_SERVER_URL
3636
}: FetchRequestConfig): Promise<FetchResponse<T>> => {
37+
console.log(11111111, baseURL)
38+
3739
try {
3840
url = baseURL + url;
3941
if (method !== "GET") {

src/components/AppAI/Search.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import DropdownList from "./DropdownList";
77
import Footer from "./Footer";
88
import { tauriFetch } from "@/api/tauriFetchClient";
99
import noDataImg from "@/assets/coconut-tree.png";
10+
import { useAppStore } from '@/stores/appStore';
1011

1112
interface SearchProps {
1213
changeInput: (val: string) => void;
@@ -15,6 +16,8 @@ interface SearchProps {
1516
}
1617

1718
function Search({ isChatMode, input }: SearchProps) {
19+
const appStore = useAppStore();
20+
1821
const [suggests, setSuggests] = useState<any[]>([]);
1922
const [isSearchComplete, setIsSearchComplete] = useState(false);
2023
const [selectedItem, setSelectedItem] = useState<any>();
@@ -64,6 +67,7 @@ function Search({ isChatMode, input }: SearchProps) {
6467
const response = await tauriFetch({
6568
url: `/query/_search?query=${input}`,
6669
method: "GET",
70+
baseURL: appStore.endpoint_http,
6771
});
6872
console.log("_suggest", input, response);
6973
const data = response.data?.hits?.hits || [];

src/components/ChatAI/Chat.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useWebSocket } from "../../hooks/useWebSocket";
1515
import { useChatStore } from "../../stores/chatStore";
1616
import { useWindows } from "../../hooks/useWindows";
1717
import { clientEnv } from "@/utils/env";
18+
import { useAppStore } from '@/stores/appStore';
1819

1920
interface ChatAIProps {
2021
inputValue: string;
@@ -38,6 +39,8 @@ const ChatAI = forwardRef<ChatAIRef, ChatAIProps>(
3839
reconnect: reconnect
3940
}));
4041

42+
const appStore = useAppStore();
43+
4144
const { createWin } = useWindows();
4245

4346
const { curChatEnd, setCurChatEnd, setConnected } = useChatStore();
@@ -56,9 +59,9 @@ const ChatAI = forwardRef<ChatAIRef, ChatAIProps>(
5659
const curIdRef = useRef(curId);
5760
curIdRef.current = curId;
5861

59-
console.log("chat useWebSocket")
62+
console.log("chat useWebSocket", clientEnv.COCO_WEBSOCKET_URL)
6063
const { messages, setMessages, connected, reconnect } = useWebSocket(
61-
`${clientEnv.COCO_WEBSOCKET_URL}`,
64+
`${appStore.endpoint_websocket || clientEnv.COCO_WEBSOCKET_URL}`,
6265
(msg) => {
6366
console.log("msg", msg);
6467
if (msg.includes("websocket-session-id")) {
@@ -138,6 +141,7 @@ const ChatAI = forwardRef<ChatAIRef, ChatAIProps>(
138141
const response = await tauriFetch({
139142
url: "/chat/_new",
140143
method: "POST",
144+
baseURL: appStore.endpoint_http,
141145
});
142146
console.log("_new", response);
143147
const newChat: Chat = response.data;
@@ -169,6 +173,7 @@ const ChatAI = forwardRef<ChatAIRef, ChatAIProps>(
169173
"WEBSOCKET-SESSION-ID": websocketId,
170174
},
171175
body: JSON.stringify({ message: content }),
176+
baseURL: appStore.endpoint_http,
172177
});
173178
console.log("_send", response, websocketId);
174179
setCurId(response.data[0]?._id);
@@ -191,6 +196,7 @@ const ChatAI = forwardRef<ChatAIRef, ChatAIProps>(
191196
const response = await tauriFetch({
192197
url: `/chat/${activeChat._id}/_close`,
193198
method: "POST",
199+
baseURL: appStore.endpoint_http,
194200
});
195201
console.log("_close", response);
196202
} catch (error) {
@@ -206,6 +212,7 @@ const ChatAI = forwardRef<ChatAIRef, ChatAIProps>(
206212
const response = await tauriFetch({
207213
url: `/chat/${activeChat._id}/_cancel`,
208214
method: "POST",
215+
baseURL: appStore.endpoint_http,
209216
});
210217

211218
console.log("_cancel", response);

src/components/ChatAI/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import { tauriFetch } from "../../api/tauriFetchClient";
1010
import { useWebSocket } from "../../hooks/useWebSocket";
1111
import { useWindows } from "../../hooks/useWindows";
1212
import { clientEnv } from "@/utils/env";
13+
import { useAppStore } from '@/stores/appStore';
1314

1415
interface ChatAIProps {}
1516

1617
export default function ChatAI({}: ChatAIProps) {
1718
const { closeWin } = useWindows();
1819

20+
const appStore = useAppStore();
21+
1922
const [chats, setChats] = useState<Chat[]>([]);
2023
const [activeChat, setActiveChat] = useState<Chat>();
2124
const [isSidebarOpen, setIsSidebarOpen] = useState(true);
@@ -36,7 +39,7 @@ export default function ChatAI({}: ChatAIProps) {
3639

3740
console.log("index useWebSocket")
3841
const { messages, setMessages } = useWebSocket(
39-
`${clientEnv.COCO_WEBSOCKET_URL}`,
42+
`${appStore.endpoint_websocket || clientEnv.COCO_WEBSOCKET_URL}`,
4043
(msg) => {
4144
if (msg.includes("websocket-session-id")) {
4245
const array = msg.split(" ");
@@ -104,6 +107,7 @@ export default function ChatAI({}: ChatAIProps) {
104107
const response = await tauriFetch({
105108
url: "/chat/_history",
106109
method: "GET",
110+
baseURL: appStore.endpoint_http,
107111
});
108112
console.log("_history", response);
109113
const hits = response.data?.hits?.hits || [];
@@ -134,6 +138,7 @@ export default function ChatAI({}: ChatAIProps) {
134138
const response = await tauriFetch({
135139
url: "/chat/_new",
136140
method: "POST",
141+
baseURL: appStore.endpoint_http,
137142
});
138143
console.log("_new", response);
139144
const newChat: Chat = response.data;
@@ -166,6 +171,7 @@ export default function ChatAI({}: ChatAIProps) {
166171
"WEBSOCKET-SESSION-ID": websocketId,
167172
},
168173
body: JSON.stringify({ message: content }),
174+
baseURL: appStore.endpoint_http,
169175
});
170176
console.log("_send", response, websocketId);
171177
setCurId(response.data[0]?._id);
@@ -187,6 +193,7 @@ export default function ChatAI({}: ChatAIProps) {
187193
const response = await tauriFetch({
188194
url: `/chat/${chat._id}/_history`,
189195
method: "GET",
196+
baseURL: appStore.endpoint_http,
190197
});
191198
console.log("id_history", response);
192199
const hits = response.data?.hits?.hits || [];
@@ -206,6 +213,7 @@ export default function ChatAI({}: ChatAIProps) {
206213
const response = await tauriFetch({
207214
url: `/chat/${activeChat._id}/_close`,
208215
method: "POST",
216+
baseURL: appStore.endpoint_http,
209217
});
210218
console.log("_close", response);
211219
} catch (error) {
@@ -219,6 +227,7 @@ export default function ChatAI({}: ChatAIProps) {
219227
const response = await tauriFetch({
220228
url: `/chat/${chat._id}/_open`,
221229
method: "POST",
230+
baseURL: appStore.endpoint_http,
222231
});
223232
console.log("_open", response);
224233
chatHistory(response.data);
@@ -233,6 +242,7 @@ export default function ChatAI({}: ChatAIProps) {
233242
const response = await tauriFetch({
234243
url: `/chat/${activeChat._id}/_cancel`,
235244
method: "POST",
245+
baseURL: appStore.endpoint_http,
236246
});
237247
console.log("_cancel", response);
238248
} catch (error) {

src/components/SearchChat/Search.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import DropdownList from "./DropdownList";
55
import { Footer } from "./Footer";
66
import { SearchResults } from "./SearchResults";
77
import { tauriFetch } from "../../api/tauriFetchClient";
8-
8+
import { useAppStore } from '@/stores/appStore';
99
interface SearchProps {
1010
changeInput: (val: string) => void;
1111
isTransitioned: boolean;
@@ -14,6 +14,13 @@ interface SearchProps {
1414
}
1515

1616
function Search({ isTransitioned, isChatMode, input }: SearchProps) {
17+
const initializeListeners = useAppStore(state => state.initializeListeners);
18+
useEffect(() => {
19+
initializeListeners();
20+
}, []);
21+
22+
const appStore = useAppStore();
23+
1724
const [suggests, setSuggests] = useState<any[]>([]);
1825
const [isSearchComplete, setIsSearchComplete] = useState(false);
1926
const [selectedItem, setSelectedItem] = useState<any>();
@@ -53,6 +60,7 @@ function Search({ isTransitioned, isChatMode, input }: SearchProps) {
5360
const response = await tauriFetch({
5461
url: `/query/_search?query=${input}`,
5562
method: "GET",
63+
baseURL: appStore.endpoint_http,
5664
});
5765
console.log("_suggest", input, response);
5866
const data = response.data?.hits?.hits || [];

src/components/Settings/Account.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import logoImg from "@/assets/32x32.png";
1313
import callbackTemplate from "@/components/Auth/callback.template";
1414
import { clientEnv } from "@/utils/env";
1515

16-
1716
export default function Account() {
1817
const app_uid = useAppStore((state) => state.app_uid);
1918
const setAppUid = useAppStore((state) => state.setAppUid);
19+
const endpoint_http = useAppStore((state) => state.endpoint_http);
2020

2121
const { auth, setAuth } = useAuthStore();
2222

@@ -94,7 +94,7 @@ export default function Account() {
9494
});
9595

9696
await shell.open(
97-
`${clientEnv.COCO_SERVER_URL}/api/desktop/session/request?port=${port}`
97+
`${endpoint_http || clientEnv.COCO_SERVER_URL}/api/desktop/session/request?port=${port}`
9898
);
9999

100100
const url = await new Promise<URL>((r) => {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { useEffect } from "react";
2+
import { Globe } from "lucide-react";
3+
4+
import SettingsItem from "./SettingsItem";
5+
import { useAppStore } from "@/stores/appStore";
6+
import { AppEndpoint } from "@/utils/tauri";
7+
8+
const ENDPOINTS = [
9+
{ value: "coco.infini.cloud", label: "coco.infini.cloud" },
10+
{ value: "localhost:2900", label: "localhost:2900" },
11+
{ value: "localhost:9000", label: "localhost:9000" },
12+
];
13+
14+
export default function AdvancedSettings() {
15+
const endpoint = useAppStore(state => state.endpoint);
16+
const setEndpoint = useAppStore(state => state.setEndpoint);
17+
18+
useEffect(() => {}, [endpoint]);
19+
20+
const onChangeEndpoint = async (newEndpoint: AppEndpoint) => {
21+
await setEndpoint(newEndpoint);
22+
};
23+
24+
return (
25+
<div className="space-y-8">
26+
<div>
27+
<h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-4">
28+
Advanced Settings
29+
</h2>
30+
<div className="space-y-6">
31+
<SettingsItem
32+
icon={Globe}
33+
title="API Endpoint"
34+
description="Domain name for interface and websocket"
35+
>
36+
<div className={`p-4 rounded-lg`}>
37+
<select
38+
value={endpoint}
39+
onChange={(e) =>
40+
onChangeEndpoint(e.target.value as AppEndpoint)
41+
}
42+
className={`w-full px-3 py-2 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent bg-white border-gray-300 text-gray-900 dark:bg-gray-800 dark:border-gray-600 dark:text-white`}
43+
>
44+
{ENDPOINTS.map(({ value, label }) => (
45+
<option key={value} value={value}>
46+
{label}
47+
</option>
48+
))}
49+
</select>
50+
</div>
51+
</SettingsItem>
52+
</div>
53+
</div>
54+
</div>
55+
);
56+
}

src/components/Settings/index2.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useSearchParams } from "react-router-dom";
55

66
import SettingsPanel from "./SettingsPanel";
77
import GeneralSettings from "./GeneralSettings";
8+
import AdvancedSettings from "./AdvancedSettings";
89
import AboutView from "./AboutView";
910
import Account from "./Account";
1011
import CocoCloud from "@/components/Auth/CocoCloud"
@@ -83,9 +84,7 @@ function SettingsPage() {
8384
</TabPanel>
8485
<TabPanel>
8586
<SettingsPanel title="">
86-
<div className="text-gray-600 dark:text-gray-400">
87-
Advanced settings content
88-
</div>
87+
<AdvancedSettings />
8988
</SettingsPanel>
9089
</TabPanel>
9190
<TabPanel>

src/pages/app/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { useState, useRef } from "react";
1+
import { useState, useRef, useEffect } from "react";
22
import { isTauri } from "@tauri-apps/api/core";
33

44
import InputBox from "@/components/AppAI/InputBox";
55
import Search from "@/components/AppAI/Search";
66
import ChatAI, { ChatAIRef } from "@/components/ChatAI/Chat";
7+
import { useAppStore } from '@/stores/appStore';
78

89
export default function DesktopApp() {
10+
const initializeListeners = useAppStore(state => state.initializeListeners);
11+
useEffect(() => {
12+
initializeListeners();
13+
}, []);
14+
915
const chatAIRef = useRef<ChatAIRef>(null);
1016

1117
const [isChatMode, setIsChatMode] = useState(false);

src/pages/#/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ import logoImg from "@/assets/images/coco-logo.png";
88
import AppleImg from "@/assets/images/apple.png";
99
import GithubImg from "@/assets/images/github.png";
1010
import GoogleImg from "@/assets/images/google.png";
11+
import { useAppStore } from "@/stores/appStore";
1112

1213
export default function LoginPage() {
14+
const initializeListeners = useAppStore((state) => state.initializeListeners);
15+
useEffect(() => {
16+
initializeListeners();
17+
}, []);
18+
1319
const handleGoogleSignIn = (response: any) => {
1420
console.log("Google Login Success:", response);
1521
// response.credential

0 commit comments

Comments
 (0)