mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-16 11:37:47 +01:00
refactor: refactor invoke related code (#309)
* refactor: refactor invoke related code * refactor: refactor invoke related code * docs: update release notes
This commit is contained in:
@@ -19,6 +19,8 @@ Information about release notes of Coco Server is provided here.
|
||||
|
||||
### Improvements
|
||||
|
||||
- refactor: refactor invoke related code #309
|
||||
|
||||
## 0.2.1 (2025-03-14)
|
||||
|
||||
### Features
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { fetch } from "@tauri-apps/plugin-http";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
import { clientEnv } from "@/utils/env";
|
||||
import { useLogStore } from "@/stores/logStore";
|
||||
|
||||
import { get_server_token } from "@/commands";
|
||||
interface FetchRequestConfig {
|
||||
url: string;
|
||||
method?: "GET" | "POST" | "PUT" | "DELETE";
|
||||
@@ -63,8 +62,8 @@ export const tauriFetch = async <T = any>({
|
||||
}
|
||||
|
||||
const server_id = connectStore.state?.currentService?.id || "default_coco_server"
|
||||
const res: any = await invoke("get_server_token", {id: server_id});
|
||||
|
||||
const res: any = await get_server_token(server_id);
|
||||
|
||||
headers["X-API-TOKEN"] = headers["X-API-TOKEN"] || res?.access_token || undefined;
|
||||
|
||||
// debug API
|
||||
|
||||
2
src/commands/index.ts
Normal file
2
src/commands/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './servers';
|
||||
export * from './system';
|
||||
172
src/commands/servers.ts
Normal file
172
src/commands/servers.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
||||
import { ServerTokenResponse, Server, Connector, DataSource, GetResponse } from "@/types/commands"
|
||||
|
||||
export function get_server_token(id: string): Promise<ServerTokenResponse> {
|
||||
return invoke(`get_server_token`, { id });
|
||||
}
|
||||
|
||||
export function list_coco_servers(): Promise<Server[]> {
|
||||
return invoke(`list_coco_servers`);
|
||||
}
|
||||
|
||||
export function add_coco_server(endpoint: string): Promise<Server> {
|
||||
return invoke(`add_coco_server`, { endpoint });
|
||||
}
|
||||
|
||||
export function enable_server(id: string): Promise<void> {
|
||||
return invoke(`enable_server`, { id });
|
||||
}
|
||||
|
||||
export function disable_server(id: string): Promise<void> {
|
||||
return invoke(`disable_server`, { id });
|
||||
}
|
||||
|
||||
export function remove_coco_server(id: string): Promise<void> {
|
||||
return invoke(`remove_coco_server`, { id });
|
||||
}
|
||||
|
||||
export function logout_coco_server(id: string): Promise<void> {
|
||||
return invoke(`logout_coco_server`, { id });
|
||||
}
|
||||
|
||||
export function refresh_coco_server_info(id: string): Promise<Server> {
|
||||
return invoke(`refresh_coco_server_info`, { id });
|
||||
}
|
||||
|
||||
export function handle_sso_callback({
|
||||
serverId,
|
||||
requestId,
|
||||
code,
|
||||
}: {
|
||||
serverId: string;
|
||||
requestId: string;
|
||||
code: string;
|
||||
}): Promise<void> {
|
||||
return invoke(`handle_sso_callback`, {
|
||||
serverId,
|
||||
requestId,
|
||||
code,
|
||||
});
|
||||
}
|
||||
|
||||
export function get_connectors_by_server(id: string): Promise<Connector[]> {
|
||||
return invoke(`get_connectors_by_server`, { id });
|
||||
}
|
||||
|
||||
export function get_datasources_by_server(id: string): Promise<DataSource[]> {
|
||||
return invoke(`get_datasources_by_server`, { id });
|
||||
}
|
||||
|
||||
export function connect_to_server(id: string): Promise<void> {
|
||||
return invoke(`connect_to_server`, { id });
|
||||
}
|
||||
|
||||
export function chat_history({
|
||||
serverId,
|
||||
from = 0,
|
||||
size = 20,
|
||||
}: {
|
||||
serverId: string;
|
||||
from?: number;
|
||||
size?: number;
|
||||
}): Promise<string> {
|
||||
return invoke(`chat_history`, {
|
||||
serverId,
|
||||
from,
|
||||
size,
|
||||
});
|
||||
}
|
||||
|
||||
export function session_chat_history({
|
||||
serverId,
|
||||
sessionId,
|
||||
from = 0,
|
||||
size = 20,
|
||||
}: {
|
||||
serverId: string;
|
||||
sessionId: string;
|
||||
from?: number;
|
||||
size?: number;
|
||||
}): Promise<string> {
|
||||
return invoke(`session_chat_history`, {
|
||||
serverId,
|
||||
sessionId,
|
||||
from,
|
||||
size,
|
||||
});
|
||||
}
|
||||
|
||||
export function close_session_chat({
|
||||
serverId,
|
||||
sessionId,
|
||||
}: {
|
||||
serverId: string;
|
||||
sessionId: string;
|
||||
}): Promise<string> {
|
||||
return invoke(`close_session_chat`, {
|
||||
serverId,
|
||||
sessionId,
|
||||
});
|
||||
}
|
||||
|
||||
export function open_session_chat({
|
||||
serverId,
|
||||
sessionId,
|
||||
}: {
|
||||
serverId: string;
|
||||
sessionId: string;
|
||||
}): Promise<string> {
|
||||
return invoke(`open_session_chat`, {
|
||||
serverId,
|
||||
sessionId,
|
||||
});
|
||||
}
|
||||
|
||||
export function cancel_session_chat({
|
||||
serverId,
|
||||
sessionId,
|
||||
}: {
|
||||
serverId: string;
|
||||
sessionId: string;
|
||||
}): Promise<string> {
|
||||
return invoke(`cancel_session_chat`, {
|
||||
serverId,
|
||||
sessionId,
|
||||
});
|
||||
}
|
||||
|
||||
export function new_chat({
|
||||
serverId,
|
||||
message,
|
||||
queryParams,
|
||||
}: {
|
||||
serverId: string;
|
||||
message: string;
|
||||
queryParams?: Record<string, any>;
|
||||
}): Promise<GetResponse> {
|
||||
return invoke(`new_chat`, {
|
||||
serverId,
|
||||
message,
|
||||
queryParams,
|
||||
});
|
||||
}
|
||||
|
||||
export function send_message({
|
||||
serverId,
|
||||
sessionId,
|
||||
message,
|
||||
queryParams,
|
||||
}: {
|
||||
serverId: string;
|
||||
sessionId: string;
|
||||
message: string;
|
||||
queryParams?: Record<string, any>;
|
||||
}): Promise<string> {
|
||||
return invoke(`send_message`, {
|
||||
serverId,
|
||||
sessionId,
|
||||
message,
|
||||
queryParams,
|
||||
});
|
||||
}
|
||||
29
src/commands/system.ts
Normal file
29
src/commands/system.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
||||
export function change_autostart(open: boolean): Promise<void> {
|
||||
return invoke('change_autostart', { open });
|
||||
}
|
||||
|
||||
export function get_current_shortcut(): Promise<string> {
|
||||
return invoke('get_current_shortcut');
|
||||
}
|
||||
|
||||
export function change_shortcut(key: string): Promise<void> {
|
||||
return invoke('change_shortcut', { key });
|
||||
}
|
||||
|
||||
export function unregister_shortcut(): Promise<void> {
|
||||
return invoke('unregister_shortcut');
|
||||
}
|
||||
|
||||
export function hide_coco(): Promise<void> {
|
||||
return invoke('hide_coco');
|
||||
}
|
||||
|
||||
export function show_coco(): Promise<void> {
|
||||
return invoke('show_coco');
|
||||
}
|
||||
|
||||
export function show_settings(): Promise<void> {
|
||||
return invoke('show_settings');
|
||||
}
|
||||
@@ -39,9 +39,3 @@ export interface IChunkData {
|
||||
message_chunk: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface DataSource {
|
||||
id: string;
|
||||
name: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
getCurrent as getCurrentDeepLinkUrls,
|
||||
onOpenUrl,
|
||||
} from "@tauri-apps/plugin-deep-link";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import clsx from "clsx";
|
||||
import { emit } from "@tauri-apps/api/event";
|
||||
@@ -29,6 +28,16 @@ import { useConnectStore } from "@/stores/connectStore";
|
||||
import bannerImg from "@/assets/images/coco-cloud-banner.jpeg";
|
||||
import SettingsToggle from "@/components/Settings/SettingsToggle";
|
||||
import Tooltip from "@/components/Common/Tooltip";
|
||||
import {
|
||||
list_coco_servers,
|
||||
add_coco_server,
|
||||
enable_server,
|
||||
disable_server,
|
||||
logout_coco_server,
|
||||
remove_coco_server,
|
||||
refresh_coco_server_info,
|
||||
handle_sso_callback,
|
||||
} from "@/commands";
|
||||
|
||||
export default function Cloud() {
|
||||
const { t } = useTranslation();
|
||||
@@ -66,7 +75,7 @@ export default function Cloud() {
|
||||
}, [JSON.stringify(currentService)]);
|
||||
|
||||
const fetchServers = async (resetSelection: boolean) => {
|
||||
invoke("list_coco_servers")
|
||||
list_coco_servers()
|
||||
.then((res: any) => {
|
||||
if (error) {
|
||||
res = (res || []).map((item: any) => {
|
||||
@@ -98,7 +107,7 @@ export default function Cloud() {
|
||||
});
|
||||
};
|
||||
|
||||
const add_coco_server = (endpointLink: string) => {
|
||||
const addServer = (endpointLink: string) => {
|
||||
if (!endpointLink) {
|
||||
throw new Error("Endpoint is required");
|
||||
}
|
||||
@@ -111,7 +120,7 @@ export default function Cloud() {
|
||||
|
||||
setRefreshLoading(true);
|
||||
|
||||
return invoke("add_coco_server", { endpoint: endpointLink })
|
||||
return add_coco_server(endpointLink)
|
||||
.then((res: any) => {
|
||||
// console.log("add_coco_server", res);
|
||||
fetchServers(false)
|
||||
@@ -126,7 +135,6 @@ export default function Cloud() {
|
||||
});
|
||||
})
|
||||
.catch((err: any) => {
|
||||
// Handle the invoke error
|
||||
console.error("add coco server failed:", err);
|
||||
setError(err);
|
||||
throw err; // Propagate error back up
|
||||
@@ -138,14 +146,14 @@ export default function Cloud() {
|
||||
|
||||
const handleOAuthCallback = useCallback(
|
||||
async (code: string | null, serverId: string | null) => {
|
||||
if (!code) {
|
||||
if (!code || !serverId) {
|
||||
setError("No authorization code received");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
console.log("Handling OAuth callback:", { code, serverId });
|
||||
await invoke("handle_sso_callback", {
|
||||
await handle_sso_callback({
|
||||
serverId: serverId, // Make sure 'server_id' is the correct argument
|
||||
requestId: ssoRequestID, // Make sure 'request_id' is the correct argument
|
||||
code: code,
|
||||
@@ -257,7 +265,7 @@ export default function Cloud() {
|
||||
|
||||
const refreshClick = (id: string) => {
|
||||
setRefreshLoading(true);
|
||||
invoke("refresh_coco_server_info", { id })
|
||||
refresh_coco_server_info(id)
|
||||
.then((res: any) => {
|
||||
console.log("refresh_coco_server_info", id, res);
|
||||
fetchServers(false).then((r) => {
|
||||
@@ -283,7 +291,7 @@ export default function Cloud() {
|
||||
function onLogout(id: string) {
|
||||
console.log("onLogout", id);
|
||||
setRefreshLoading(true);
|
||||
invoke("logout_coco_server", { id })
|
||||
logout_coco_server(id)
|
||||
.then((res: any) => {
|
||||
console.log("logout_coco_server", id, JSON.stringify(res));
|
||||
refreshClick(id);
|
||||
@@ -298,8 +306,8 @@ export default function Cloud() {
|
||||
});
|
||||
}
|
||||
|
||||
const remove_coco_server = (id: string) => {
|
||||
invoke("remove_coco_server", { id })
|
||||
const removeServer = (id: string) => {
|
||||
remove_coco_server(id)
|
||||
.then((res: any) => {
|
||||
console.log("remove_coco_server", id, JSON.stringify(res));
|
||||
fetchServers(true).then((r) => {
|
||||
@@ -316,9 +324,11 @@ export default function Cloud() {
|
||||
const enable_coco_server = useCallback(
|
||||
async (enabled: boolean) => {
|
||||
try {
|
||||
const command = enabled ? "enable_server" : "disable_server";
|
||||
|
||||
await invoke(command, { id: currentService?.id });
|
||||
if (enabled) {
|
||||
await enable_server(currentService?.id);
|
||||
} else {
|
||||
await disable_server(currentService?.id);
|
||||
}
|
||||
|
||||
setCurrentService({ ...currentService, enabled });
|
||||
|
||||
@@ -391,7 +401,7 @@ export default function Cloud() {
|
||||
{!currentService?.builtin && (
|
||||
<button
|
||||
className="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300 rounded-[6px] bg-white dark:bg-gray-800 border border-[rgba(228,229,239,1)] dark:border-gray-700"
|
||||
onClick={() => remove_coco_server(currentService?.id)}
|
||||
onClick={() => removeServer(currentService?.id)}
|
||||
>
|
||||
<Trash2 className="w-3.5 h-3.5 text-[#ff4747]" />
|
||||
</button>
|
||||
@@ -490,7 +500,7 @@ export default function Cloud() {
|
||||
) : null}
|
||||
</div>
|
||||
) : (
|
||||
<Connect setIsConnect={setIsConnect} onAddServer={add_coco_server} />
|
||||
<Connect setIsConnect={setIsConnect} onAddServer={addServer} />
|
||||
)}
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useEffect, useState } from "react";
|
||||
import { RefreshCcw } from "lucide-react";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
import { DataSourceItem } from "./DataSourceItem";
|
||||
import { useConnectStore } from "@/stores/connectStore";
|
||||
import { useAppStore } from "@/stores/appStore";
|
||||
import {
|
||||
get_connectors_by_server,
|
||||
get_datasources_by_server,
|
||||
} from "@/commands";
|
||||
|
||||
export function DataSourcesList({ server }: { server: string }) {
|
||||
const { t } = useTranslation();
|
||||
@@ -17,7 +20,7 @@ export function DataSourcesList({ server }: { server: string }) {
|
||||
|
||||
function initServerAppData({ server }: { server: string }) {
|
||||
//fetch datasource data
|
||||
invoke("get_connectors_by_server", { id: server })
|
||||
get_connectors_by_server(server)
|
||||
.then((res: any) => {
|
||||
// console.log("get_connectors_by_server", res);
|
||||
setConnectorData(res, server);
|
||||
@@ -29,7 +32,7 @@ export function DataSourcesList({ server }: { server: string }) {
|
||||
.finally(() => {});
|
||||
|
||||
//fetch datasource data
|
||||
invoke("get_datasources_by_server", { id: server })
|
||||
get_datasources_by_server(server)
|
||||
.then((res: any) => {
|
||||
// console.log("get_datasources_by_server", res);
|
||||
setDatasourceData(res, server);
|
||||
|
||||
@@ -11,8 +11,9 @@ import { useAppStore } from "@/stores/appStore";
|
||||
import { useSearchStore } from "@/stores/searchStore";
|
||||
import { metaOrCtrlKey } from "@/utils/keyboardUtils";
|
||||
import SearchPopover from "./SearchPopover";
|
||||
import { DataSource } from "@/components/Assistant/types";
|
||||
import AudioRecording from "../AudioRecording";
|
||||
import { hide_coco } from "@/commands";
|
||||
import { DataSource } from "@/types/commands";
|
||||
|
||||
interface ChatInputProps {
|
||||
onSend: (message: string) => void;
|
||||
@@ -30,7 +31,6 @@ interface ChatInputProps {
|
||||
isChatPage?: boolean;
|
||||
getDataSourcesByServer: (serverId: string) => Promise<DataSource[]>;
|
||||
setupWindowFocusListener: (callback: () => void) => Promise<() => void>;
|
||||
hideCoco: () => Promise<any>;
|
||||
checkScreenPermission: () => Promise<boolean>;
|
||||
requestScreenPermission: () => void;
|
||||
getScreenMonitors: () => Promise<any[]>;
|
||||
@@ -60,7 +60,6 @@ export default function ChatInput({
|
||||
isChatPage = false,
|
||||
getDataSourcesByServer,
|
||||
setupWindowFocusListener,
|
||||
hideCoco,
|
||||
}: // checkScreenPermission,
|
||||
// requestScreenPermission,
|
||||
// getScreenMonitors,
|
||||
@@ -124,7 +123,7 @@ ChatInputProps) {
|
||||
if (inputValue) {
|
||||
changeInput("");
|
||||
} else if (!isPinned) {
|
||||
hideCoco();
|
||||
hide_coco();
|
||||
}
|
||||
}, [inputValue, isPinned]);
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import { useTranslation } from "react-i18next";
|
||||
import TypeIcon from "@/components/Common/Icons/TypeIcon";
|
||||
import { useConnectStore } from "@/stores/connectStore";
|
||||
import { useSearchStore } from "@/stores/searchStore";
|
||||
import { DataSource } from "@/components/Assistant/types"
|
||||
import { DataSource } from "@/types/commands";
|
||||
|
||||
interface SearchPopoverProps {
|
||||
isSearchActive: boolean;
|
||||
@@ -42,7 +42,9 @@ export default function SearchPopover({
|
||||
|
||||
const getDataSourceList = useCallback(async () => {
|
||||
try {
|
||||
const res: DataSource[] = await getDataSourcesByServer(currentService?.id);
|
||||
const res: DataSource[] = await getDataSourcesByServer(
|
||||
currentService?.id
|
||||
);
|
||||
const data = [
|
||||
{
|
||||
id: "all",
|
||||
@@ -180,7 +182,7 @@ export default function SearchPopover({
|
||||
<TypeIcon item={item} className="size-[16px]" />
|
||||
)}
|
||||
|
||||
<span>{isAll ? t(name) : name}</span>
|
||||
<span>{isAll && name ? t(name) : name}</span>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-center items-center size-[24px]">
|
||||
|
||||
@@ -11,10 +11,9 @@ import {
|
||||
Globe,
|
||||
} from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { isTauri, invoke } from "@tauri-apps/api/core";
|
||||
import { isTauri } from "@tauri-apps/api/core";
|
||||
import {
|
||||
isEnabled,
|
||||
// enable, disable
|
||||
} from "@tauri-apps/plugin-autostart";
|
||||
import { emit } from "@tauri-apps/api/event";
|
||||
import { useCreation } from "ahooks";
|
||||
@@ -27,6 +26,7 @@ import { useShortcutEditor } from "@/hooks/useShortcutEditor";
|
||||
import { useAppStore } from "@/stores/appStore";
|
||||
import { AppTheme } from "@/utils/tauri";
|
||||
import { useThemeStore } from "@/stores/themeStore";
|
||||
import { change_autostart, get_current_shortcut, change_shortcut, unregister_shortcut } from "@/commands"
|
||||
|
||||
export function ThemeOption({
|
||||
icon: Icon,
|
||||
@@ -90,8 +90,7 @@ export default function GeneralSettings() {
|
||||
const enableAutoStart = async () => {
|
||||
if (isTauri()) {
|
||||
try {
|
||||
// await enable();
|
||||
invoke("change_autostart", { open: true });
|
||||
await change_autostart(true);
|
||||
} catch (error) {
|
||||
console.error("Failed to enable autostart:", error);
|
||||
}
|
||||
@@ -102,8 +101,7 @@ export default function GeneralSettings() {
|
||||
const disableAutoStart = async () => {
|
||||
if (isTauri()) {
|
||||
try {
|
||||
// await disable();
|
||||
invoke("change_autostart", { open: false });
|
||||
await change_autostart(false);
|
||||
} catch (error) {
|
||||
console.error("Failed to disable autostart:", error);
|
||||
}
|
||||
@@ -123,7 +121,7 @@ export default function GeneralSettings() {
|
||||
|
||||
async function getCurrentShortcut() {
|
||||
try {
|
||||
const res: any = await invoke("get_current_shortcut");
|
||||
const res: any = await get_current_shortcut();
|
||||
// console.log("get_current_shortcut: ", res);
|
||||
setShortcut(res?.split("+"));
|
||||
} catch (err) {
|
||||
@@ -143,7 +141,7 @@ export default function GeneralSettings() {
|
||||
setShortcut(key);
|
||||
//
|
||||
if (key.length === 0) return;
|
||||
invoke("change_shortcut", { key: key?.join("+") }).catch((err) => {
|
||||
change_shortcut(key?.join("+")).catch((err) => {
|
||||
console.error("Failed to save hotkey:", err);
|
||||
});
|
||||
};
|
||||
@@ -154,7 +152,7 @@ export default function GeneralSettings() {
|
||||
const onEditShortcut = async () => {
|
||||
startEditing();
|
||||
//
|
||||
invoke("unregister_shortcut").catch((err) => {
|
||||
unregister_shortcut().catch((err) => {
|
||||
console.error("Failed to save hotkey:", err);
|
||||
});
|
||||
};
|
||||
@@ -162,7 +160,7 @@ export default function GeneralSettings() {
|
||||
const onCancelShortcut = async () => {
|
||||
cancelEditing();
|
||||
//
|
||||
invoke("change_shortcut", { key: shortcut?.join("+") }).catch((err) => {
|
||||
change_shortcut(shortcut?.join("+")).catch((err) => {
|
||||
console.error("Failed to save hotkey:", err);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { useCallback } from "react";
|
||||
import { invoke, isTauri } from "@tauri-apps/api/core";
|
||||
import { isTauri } from "@tauri-apps/api/core";
|
||||
|
||||
import type { Chat } from "@/components/Assistant/types";
|
||||
import { close_session_chat, cancel_session_chat, session_chat_history, new_chat, send_message, open_session_chat, chat_history } from "@/commands"
|
||||
|
||||
export function useChatActions(
|
||||
currentServiceId: string | undefined,
|
||||
@@ -18,9 +19,9 @@ export function useChatActions(
|
||||
changeInput?: (val: string) => void,
|
||||
) {
|
||||
const chatClose = useCallback(async (activeChat?: Chat) => {
|
||||
if (!activeChat?._id) return;
|
||||
if (!activeChat?._id || !currentServiceId) return;
|
||||
try {
|
||||
let response: any = await invoke("close_session_chat", {
|
||||
let response: any = await close_session_chat({
|
||||
serverId: currentServiceId,
|
||||
sessionId: activeChat?._id,
|
||||
});
|
||||
@@ -33,9 +34,9 @@ export function useChatActions(
|
||||
|
||||
const cancelChat = useCallback(async (activeChat?: Chat) => {
|
||||
setCurChatEnd(true);
|
||||
if (!activeChat?._id) return;
|
||||
if (!activeChat?._id || !currentServiceId) return;
|
||||
try {
|
||||
let response: any = await invoke("cancel_session_chat", {
|
||||
let response: any = await cancel_session_chat({
|
||||
serverId: currentServiceId,
|
||||
sessionId: activeChat?._id,
|
||||
});
|
||||
@@ -50,8 +51,9 @@ export function useChatActions(
|
||||
chat: Chat,
|
||||
callback?: (chat: Chat) => void
|
||||
) => {
|
||||
if (!chat?._id || !currentServiceId) return;
|
||||
try {
|
||||
let response: any = await invoke("session_chat_history", {
|
||||
let response: any = await session_chat_history({
|
||||
serverId: currentServiceId,
|
||||
sessionId: chat?._id,
|
||||
from: 0,
|
||||
@@ -78,9 +80,10 @@ export function useChatActions(
|
||||
chatClose(activeChat);
|
||||
clearAllChunkData();
|
||||
setQuestion(value);
|
||||
if (!currentServiceId) return;
|
||||
try {
|
||||
console.log("sourceDataIds", sourceDataIds);
|
||||
let response: any = await invoke("new_chat", {
|
||||
let response: any = await new_chat({
|
||||
serverId: currentServiceId,
|
||||
message: value,
|
||||
queryParams: {
|
||||
@@ -114,10 +117,10 @@ export function useChatActions(
|
||||
|
||||
const sendMessage = useCallback(
|
||||
async (content: string, newChat: Chat) => {
|
||||
if (!newChat?._id || !content) return;
|
||||
if (!newChat?._id || !currentServiceId || !content) return;
|
||||
clearAllChunkData();
|
||||
try {
|
||||
let response: any = await invoke("send_message", {
|
||||
let response: any = await send_message({
|
||||
serverId: currentServiceId,
|
||||
sessionId: newChat?._id,
|
||||
queryParams: {
|
||||
@@ -161,8 +164,9 @@ export function useChatActions(
|
||||
);
|
||||
|
||||
const openSessionChat = useCallback(async (chat: Chat) => {
|
||||
if (!chat?._id || !currentServiceId) return;
|
||||
try {
|
||||
let response: any = await invoke("open_session_chat", {
|
||||
let response: any = await open_session_chat({
|
||||
serverId: currentServiceId,
|
||||
sessionId: chat?._id,
|
||||
});
|
||||
@@ -178,7 +182,7 @@ export function useChatActions(
|
||||
const getChatHistory = useCallback(async () => {
|
||||
if (!currentServiceId) return [];
|
||||
try {
|
||||
let response: any = await invoke("chat_history", {
|
||||
let response: any = await chat_history({
|
||||
serverId: currentServiceId,
|
||||
from: 0,
|
||||
size: 20,
|
||||
|
||||
@@ -1,36 +1,38 @@
|
||||
import {useEffect} from "react";
|
||||
import {invoke, isTauri} from "@tauri-apps/api/core";
|
||||
import {listen} from "@tauri-apps/api/event";
|
||||
import { useEffect } from "react";
|
||||
import { isTauri } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
|
||||
import { hide_coco } from "@/commands"
|
||||
|
||||
const useEscape = () => {
|
||||
const handleEscape = async (event: KeyboardEvent) => {
|
||||
if (event.key === "Escape") {
|
||||
console.log("Escape key pressed.");
|
||||
const handleEscape = async (event: KeyboardEvent) => {
|
||||
if (event.key === "Escape") {
|
||||
console.log("Escape key pressed.");
|
||||
|
||||
event.preventDefault();
|
||||
event.preventDefault();
|
||||
|
||||
// Hide the Tauri app window when 'Esc' is pressed
|
||||
await invoke("hide_coco");
|
||||
// Hide the Tauri app window when 'Esc' is pressed
|
||||
await hide_coco()
|
||||
|
||||
console.log("App window hidden successfully.");
|
||||
}
|
||||
console.log("App window hidden successfully.");
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!isTauri()) return;
|
||||
|
||||
const unlisten = listen("tauri://focus", () => {
|
||||
// Add event listener for keydown
|
||||
window.addEventListener("keydown", handleEscape);
|
||||
});
|
||||
|
||||
// Cleanup event listener on component unmount
|
||||
return () => {
|
||||
unlisten.then((unlistenFn) => unlistenFn());
|
||||
|
||||
window.removeEventListener("keydown", handleEscape);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!isTauri()) return;
|
||||
|
||||
const unlisten = listen("tauri://focus", () => {
|
||||
// Add event listener for keydown
|
||||
window.addEventListener("keydown", handleEscape);
|
||||
});
|
||||
|
||||
// Cleanup event listener on component unmount
|
||||
return () => {
|
||||
unlisten.then((unlistenFn) => unlistenFn());
|
||||
|
||||
window.removeEventListener("keydown", handleEscape);
|
||||
};
|
||||
}, []);
|
||||
}, []);
|
||||
};
|
||||
|
||||
export default useEscape;
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { TrayIcon, type TrayIconOptions } from "@tauri-apps/api/tray";
|
||||
import { Menu, MenuItem, PredefinedMenuItem } from "@tauri-apps/api/menu";
|
||||
import { isMac } from "@/utils/platform";
|
||||
import { resolveResource } from "@tauri-apps/api/path";
|
||||
import { useUpdateEffect } from "ahooks";
|
||||
import { exit } from "@tauri-apps/plugin-process";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
import { isMac } from "@/utils/platform";
|
||||
import { useAppStore } from "@/stores/appStore";
|
||||
import { show_coco, show_settings } from "@/commands";
|
||||
|
||||
const TRAY_ID = "COCO_TRAY";
|
||||
|
||||
@@ -50,7 +51,7 @@ export const useTray = () => {
|
||||
text: t("tray.showCoco"),
|
||||
accelerator: showCocoShortcuts.join("+"),
|
||||
action: () => {
|
||||
invoke("show_coco");
|
||||
show_coco()
|
||||
},
|
||||
}),
|
||||
PredefinedMenuItem.new({ item: "Separator" }),
|
||||
@@ -58,7 +59,7 @@ export const useTray = () => {
|
||||
text: t("tray.settings"),
|
||||
// accelerator: "CommandOrControl+,",
|
||||
action: () => {
|
||||
invoke("show_settings");
|
||||
show_settings()
|
||||
},
|
||||
}),
|
||||
PredefinedMenuItem.new({ item: "Separator" }),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
|
||||
import { IServer } from "@/stores/appStore";
|
||||
import { connect_to_server } from "@/commands"
|
||||
|
||||
interface WebSocketProps {
|
||||
connected: boolean;
|
||||
@@ -24,7 +24,7 @@ export default function useWebSocket({
|
||||
if (!targetServer?.id) return;
|
||||
try {
|
||||
console.log("reconnect", targetServer.id);
|
||||
await invoke("connect_to_server", { id: targetServer.id });
|
||||
await connect_to_server(targetServer.id);
|
||||
setConnected(true);
|
||||
} catch (error) {
|
||||
setConnected(false);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useRef, useEffect, useCallback } from "react";
|
||||
import { invoke, convertFileSrc } from "@tauri-apps/api/core";
|
||||
import { convertFileSrc } from "@tauri-apps/api/core";
|
||||
import { listen } from "@tauri-apps/api/event";
|
||||
import {
|
||||
checkScreenRecordingPermission,
|
||||
@@ -19,7 +19,14 @@ import { Sidebar } from "@/components/Assistant/Sidebar";
|
||||
import type { Chat } from "@/components/Assistant/types";
|
||||
import { useConnectStore } from "@/stores/connectStore";
|
||||
import InputBox from "@/components/Search/InputBox";
|
||||
import { DataSource } from "@/components/Assistant/types";
|
||||
import {
|
||||
chat_history,
|
||||
session_chat_history,
|
||||
close_session_chat,
|
||||
open_session_chat,
|
||||
get_datasources_by_server,
|
||||
} from "@/commands";
|
||||
import { DataSource } from "@/types/commands"
|
||||
|
||||
interface ChatProps {}
|
||||
|
||||
@@ -38,7 +45,7 @@ export default function Chat({}: ChatProps) {
|
||||
const [isSearchActive, setIsSearchActive] = useState(false);
|
||||
const [isDeepThinkActive, setIsDeepThinkActive] = useState(false);
|
||||
|
||||
const isChatPage = true
|
||||
const isChatPage = true;
|
||||
|
||||
useEffect(() => {
|
||||
getChatHistory();
|
||||
@@ -46,7 +53,7 @@ export default function Chat({}: ChatProps) {
|
||||
|
||||
const getChatHistory = async () => {
|
||||
try {
|
||||
let response: any = await invoke("chat_history", {
|
||||
let response: any = await chat_history({
|
||||
serverId: currentService?.id,
|
||||
from: 0,
|
||||
size: 20,
|
||||
@@ -84,7 +91,7 @@ export default function Chat({}: ChatProps) {
|
||||
|
||||
const chatHistory = async (chat: Chat) => {
|
||||
try {
|
||||
let response: any = await invoke("session_chat_history", {
|
||||
let response: any = await session_chat_history({
|
||||
serverId: currentService?.id,
|
||||
sessionId: chat?._id,
|
||||
from: 0,
|
||||
@@ -106,7 +113,7 @@ export default function Chat({}: ChatProps) {
|
||||
const chatClose = async () => {
|
||||
if (!activeChat?._id) return;
|
||||
try {
|
||||
let response: any = await invoke("close_session_chat", {
|
||||
let response: any = await close_session_chat({
|
||||
serverId: currentService?.id,
|
||||
sessionId: activeChat?._id,
|
||||
});
|
||||
@@ -120,7 +127,7 @@ export default function Chat({}: ChatProps) {
|
||||
const onSelectChat = async (chat: any) => {
|
||||
chatClose();
|
||||
try {
|
||||
let response: any = await invoke("open_session_chat", {
|
||||
let response: any = await open_session_chat({
|
||||
serverId: currentService?.id,
|
||||
sessionId: chat?._id,
|
||||
});
|
||||
@@ -145,19 +152,13 @@ export default function Chat({}: ChatProps) {
|
||||
chatAIRef.current?.reconnect();
|
||||
};
|
||||
|
||||
const hideCoco = useCallback(() => {
|
||||
return invoke("hide_coco");
|
||||
}, []);
|
||||
|
||||
const getFileUrl = useCallback((path: string) => {
|
||||
return convertFileSrc(path);
|
||||
}, []);
|
||||
|
||||
const getDataSourcesByServer = useCallback(
|
||||
async (serverId: string): Promise<DataSource[]> => {
|
||||
return invoke("get_datasources_by_server", {
|
||||
id: serverId,
|
||||
});
|
||||
return get_datasources_by_server(serverId);
|
||||
},
|
||||
[]
|
||||
);
|
||||
@@ -244,7 +245,9 @@ export default function Chat({}: ChatProps) {
|
||||
/>
|
||||
|
||||
{/* Input area */}
|
||||
<div className={`border-t p-4 pb-0 border-gray-200 dark:border-gray-800`}>
|
||||
<div
|
||||
className={`border-t p-4 pb-0 border-gray-200 dark:border-gray-800`}
|
||||
>
|
||||
<InputBox
|
||||
isChatMode={true}
|
||||
inputValue={input}
|
||||
@@ -260,7 +263,6 @@ export default function Chat({}: ChatProps) {
|
||||
isChatPage={isChatPage}
|
||||
getDataSourcesByServer={getDataSourcesByServer}
|
||||
setupWindowFocusListener={setupWindowFocusListener}
|
||||
hideCoco={hideCoco}
|
||||
checkScreenPermission={checkScreenPermission}
|
||||
requestScreenPermission={requestScreenPermission}
|
||||
getScreenMonitors={getScreenMonitors}
|
||||
|
||||
@@ -19,8 +19,9 @@ import { useWindowEvents } from "@/hooks/useWindowEvents";
|
||||
import { useAppStore } from "@/stores/appStore";
|
||||
import { useAuthStore } from "@/stores/authStore";
|
||||
import platformAdapter from "@/utils/platformAdapter";
|
||||
import { DataSource } from "@/components/Assistant/types";
|
||||
import { useStartupStore } from "@/stores/startupStore";
|
||||
import { DataSource } from "@/types/commands"
|
||||
|
||||
interface SearchChatProps {
|
||||
querySearch: (input: string) => Promise<any>;
|
||||
queryDocuments: (
|
||||
@@ -275,7 +276,6 @@ function SearchChat({ querySearch, queryDocuments }: SearchChatProps) {
|
||||
setIsDeepThinkActive={toggleDeepThinkActive}
|
||||
getDataSourcesByServer={getDataSourcesByServer}
|
||||
setupWindowFocusListener={setupWindowFocusListener}
|
||||
hideCoco={hideCoco}
|
||||
checkScreenPermission={checkScreenPermission}
|
||||
requestScreenPermission={requestScreenPermission}
|
||||
getScreenMonitors={getScreenMonitors}
|
||||
|
||||
114
src/types/commands.ts
Normal file
114
src/types/commands.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
export interface ServerTokenResponse {
|
||||
access_token?: string;
|
||||
}
|
||||
|
||||
interface Provider {
|
||||
name: string;
|
||||
icon: string;
|
||||
website: string;
|
||||
eula: string;
|
||||
privacy_policy: string;
|
||||
banner: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
interface Version {
|
||||
number: string;
|
||||
}
|
||||
|
||||
interface Sso {
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface AuthProvider {
|
||||
sso: Sso;
|
||||
}
|
||||
|
||||
interface MinimalClientVersion {
|
||||
number: string;
|
||||
}
|
||||
|
||||
type Status = "green" | "yellow" | "red";
|
||||
|
||||
interface Health {
|
||||
services?: Record<string, Status>;
|
||||
status: Status;
|
||||
}
|
||||
|
||||
interface Preferences {
|
||||
theme: string;
|
||||
language: string;
|
||||
}
|
||||
|
||||
interface UserProfile {
|
||||
name: string;
|
||||
email: string;
|
||||
avatar: string;
|
||||
preferences: Preferences;
|
||||
}
|
||||
|
||||
export interface Server {
|
||||
id: string;
|
||||
builtin: boolean;
|
||||
name: string;
|
||||
endpoint: string;
|
||||
provider: Provider;
|
||||
version: Version;
|
||||
minimal_client_version?: MinimalClientVersion;
|
||||
updated: string;
|
||||
enabled: boolean;
|
||||
public: boolean;
|
||||
available: boolean;
|
||||
health?: Health;
|
||||
profile?: UserProfile;
|
||||
auth_provider: AuthProvider;
|
||||
priority: number;
|
||||
}
|
||||
|
||||
interface ConnectorAssets {
|
||||
icons?: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface Connector {
|
||||
id: string;
|
||||
created?: string;
|
||||
updated?: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
category?: string;
|
||||
icon?: string;
|
||||
tags?: string[];
|
||||
url?: string;
|
||||
assets?: ConnectorAssets;
|
||||
}
|
||||
|
||||
interface ConnectorConfig {
|
||||
id?: string;
|
||||
config?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface DataSource {
|
||||
id: string;
|
||||
icon?: string;
|
||||
created?: string;
|
||||
updated?: string;
|
||||
type?: string;
|
||||
name?: string;
|
||||
connector?: ConnectorConfig;
|
||||
connector_info?: Connector;
|
||||
}
|
||||
|
||||
interface Source {
|
||||
id: string;
|
||||
created: string;
|
||||
updated: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export interface GetResponse {
|
||||
_id: string;
|
||||
_source: Source;
|
||||
result: string;
|
||||
payload?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { invoke, isTauri } from "@tauri-apps/api/core";
|
||||
import { isTauri } from "@tauri-apps/api/core";
|
||||
import { open } from "@tauri-apps/plugin-shell";
|
||||
|
||||
import { hide_coco } from "@/commands"
|
||||
|
||||
// 1
|
||||
export async function copyToClipboard(text: string) {
|
||||
try {
|
||||
@@ -66,7 +68,7 @@ export const OpenURLWithBrowser = async (url: string) => {
|
||||
if (isTauri()) {
|
||||
try {
|
||||
await open(url);
|
||||
await invoke("hide_coco");
|
||||
await hide_coco();
|
||||
console.log("URL opened in default browser");
|
||||
} catch (error) {
|
||||
console.error("Failed to open URL:", error);
|
||||
|
||||
Reference in New Issue
Block a user