2025-04-02 14:03:40 +08:00
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
2025-03-25 20:57:46 +08:00
|
|
|
|
2025-04-02 14:03:40 +08:00
|
|
|
import {
|
|
|
|
|
ServerTokenResponse,
|
|
|
|
|
Server,
|
|
|
|
|
Connector,
|
|
|
|
|
DataSource,
|
|
|
|
|
GetResponse,
|
2025-04-02 14:45:39 +08:00
|
|
|
UploadAttachmentPayload,
|
|
|
|
|
UploadAttachmentResponse,
|
|
|
|
|
GetAttachmentPayload,
|
|
|
|
|
GetAttachmentResponse,
|
|
|
|
|
DeleteAttachmentPayload,
|
|
|
|
|
TranscriptionPayload,
|
|
|
|
|
TranscriptionResponse,
|
2025-04-18 10:36:00 +08:00
|
|
|
MultiSourceQueryResponse,
|
2025-04-02 14:03:40 +08:00
|
|
|
} from "@/types/commands";
|
2025-04-22 18:32:20 +08:00
|
|
|
import { useAppStore } from "@/stores/appStore";
|
2025-05-22 10:59:46 +08:00
|
|
|
import { useAuthStore } from "@/stores/authStore";
|
|
|
|
|
|
|
|
|
|
// Endpoints that don't require authentication
|
|
|
|
|
const WHITELIST_SERVERS = [
|
|
|
|
|
"list_coco_servers",
|
|
|
|
|
"add_coco_server",
|
|
|
|
|
"enable_server",
|
|
|
|
|
"disable_server",
|
|
|
|
|
"remove_coco_server",
|
|
|
|
|
"logout_coco_server",
|
|
|
|
|
"refresh_coco_server_info",
|
|
|
|
|
"handle_sso_callback",
|
|
|
|
|
"query_coco_fusion",
|
|
|
|
|
];
|
2025-04-18 10:36:00 +08:00
|
|
|
|
|
|
|
|
async function invokeWithErrorHandler<T>(
|
|
|
|
|
command: string,
|
|
|
|
|
args?: Record<string, any>
|
|
|
|
|
): Promise<T> {
|
2025-05-22 10:59:46 +08:00
|
|
|
const isCurrentLogin = useAuthStore.getState().isCurrentLogin;
|
|
|
|
|
if (!WHITELIST_SERVERS.includes(command) && !isCurrentLogin) {
|
|
|
|
|
console.error("This command requires authentication");
|
|
|
|
|
throw new Error("This command requires authentication");
|
|
|
|
|
}
|
|
|
|
|
//
|
2025-04-18 10:36:00 +08:00
|
|
|
const addError = useAppStore.getState().addError;
|
|
|
|
|
try {
|
|
|
|
|
const result = await invoke<T>(command, args);
|
2025-04-18 14:39:27 +08:00
|
|
|
// console.log(command, result);
|
2025-04-18 10:36:00 +08:00
|
|
|
|
2025-04-22 18:32:20 +08:00
|
|
|
if (result && typeof result === "object" && "failed" in result) {
|
2025-04-18 10:36:00 +08:00
|
|
|
const failedResult = result as any;
|
2025-05-15 08:54:42 +08:00
|
|
|
if (failedResult.failed?.length > 0 && failedResult?.hits?.length == 0) {
|
2025-04-18 10:36:00 +08:00
|
|
|
failedResult.failed.forEach((error: any) => {
|
2025-05-15 08:54:42 +08:00
|
|
|
addError(error.error, 'error');
|
|
|
|
|
// console.error(error.error);
|
2025-04-18 10:36:00 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-22 18:32:20 +08:00
|
|
|
if (typeof result === "string") {
|
2025-04-18 10:36:00 +08:00
|
|
|
const res = JSON.parse(result);
|
2025-04-22 18:32:20 +08:00
|
|
|
if (typeof res === "string") {
|
2025-04-18 10:36:00 +08:00
|
|
|
throw new Error(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} catch (error: any) {
|
2025-04-22 18:32:20 +08:00
|
|
|
const errorMessage = error || "Command execution failed";
|
2025-04-23 00:12:22 +08:00
|
|
|
addError(command + ":" + errorMessage, "error");
|
2025-04-18 10:36:00 +08:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-25 20:57:46 +08:00
|
|
|
|
|
|
|
|
export function get_server_token(id: string): Promise<ServerTokenResponse> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`get_server_token`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function list_coco_servers(): Promise<Server[]> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`list_coco_servers`);
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function add_coco_server(endpoint: string): Promise<Server> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`add_coco_server`, { endpoint });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function enable_server(id: string): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`enable_server`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function disable_server(id: string): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`disable_server`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function remove_coco_server(id: string): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`remove_coco_server`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function logout_coco_server(id: string): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`logout_coco_server`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function refresh_coco_server_info(id: string): Promise<Server> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`refresh_coco_server_info`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function handle_sso_callback({
|
|
|
|
|
serverId,
|
|
|
|
|
requestId,
|
|
|
|
|
code,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
requestId: string;
|
|
|
|
|
code: string;
|
|
|
|
|
}): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`handle_sso_callback`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
|
|
|
|
requestId,
|
|
|
|
|
code,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function get_connectors_by_server(id: string): Promise<Connector[]> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`get_connectors_by_server`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-04-24 19:00:16 +08:00
|
|
|
export function datasource_search(id: string): Promise<DataSource[]> {
|
|
|
|
|
return invokeWithErrorHandler(`datasource_search`, { id });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function mcp_server_search(id: string): Promise<DataSource[]> {
|
|
|
|
|
return invokeWithErrorHandler(`mcp_server_search`, { id });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-30 19:33:49 +08:00
|
|
|
export function connect_to_server(id: string, clientId: string): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`connect_to_server`, { id, clientId });
|
2025-03-30 19:33:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function disconnect(clientId: string): Promise<void> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`disconnect`, { clientId });
|
2025-03-25 20:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function chat_history({
|
|
|
|
|
serverId,
|
|
|
|
|
from = 0,
|
|
|
|
|
size = 20,
|
2025-04-02 14:03:40 +08:00
|
|
|
query = "",
|
2025-03-25 20:57:46 +08:00
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
from?: number;
|
|
|
|
|
size?: number;
|
2025-04-02 14:03:40 +08:00
|
|
|
query?: string;
|
2025-03-25 20:57:46 +08:00
|
|
|
}): Promise<string> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`chat_history`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
|
|
|
|
from,
|
|
|
|
|
size,
|
2025-04-02 14:03:40 +08:00
|
|
|
query,
|
2025-03-25 20:57:46 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function session_chat_history({
|
|
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
from = 0,
|
|
|
|
|
size = 20,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
sessionId: string;
|
|
|
|
|
from?: number;
|
|
|
|
|
size?: number;
|
|
|
|
|
}): Promise<string> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`session_chat_history`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
from,
|
|
|
|
|
size,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function close_session_chat({
|
|
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
sessionId: string;
|
|
|
|
|
}): Promise<string> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`close_session_chat`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function open_session_chat({
|
|
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
sessionId: string;
|
|
|
|
|
}): Promise<string> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`open_session_chat`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function cancel_session_chat({
|
|
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
sessionId: string;
|
|
|
|
|
}): Promise<string> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`cancel_session_chat`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function new_chat({
|
|
|
|
|
serverId,
|
2025-03-30 19:33:49 +08:00
|
|
|
websocketId,
|
2025-03-25 20:57:46 +08:00
|
|
|
message,
|
|
|
|
|
queryParams,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
2025-03-30 19:33:49 +08:00
|
|
|
websocketId?: string;
|
2025-03-25 20:57:46 +08:00
|
|
|
message: string;
|
|
|
|
|
queryParams?: Record<string, any>;
|
|
|
|
|
}): Promise<GetResponse> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`new_chat`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
2025-03-30 19:33:49 +08:00
|
|
|
websocketId,
|
2025-03-25 20:57:46 +08:00
|
|
|
message,
|
|
|
|
|
queryParams,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function send_message({
|
|
|
|
|
serverId,
|
2025-03-30 19:33:49 +08:00
|
|
|
websocketId,
|
2025-03-25 20:57:46 +08:00
|
|
|
sessionId,
|
|
|
|
|
message,
|
|
|
|
|
queryParams,
|
|
|
|
|
}: {
|
|
|
|
|
serverId: string;
|
2025-03-30 19:33:49 +08:00
|
|
|
websocketId?: string;
|
2025-03-25 20:57:46 +08:00
|
|
|
sessionId: string;
|
|
|
|
|
message: string;
|
|
|
|
|
queryParams?: Record<string, any>;
|
|
|
|
|
}): Promise<string> {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler(`send_message`, {
|
2025-03-25 20:57:46 +08:00
|
|
|
serverId,
|
2025-03-30 19:33:49 +08:00
|
|
|
websocketId,
|
2025-03-25 20:57:46 +08:00
|
|
|
sessionId,
|
|
|
|
|
message,
|
|
|
|
|
queryParams,
|
|
|
|
|
});
|
2025-04-02 14:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const delete_session_chat = (serverId: string, sessionId: string) => {
|
2025-04-22 18:32:20 +08:00
|
|
|
return invokeWithErrorHandler<boolean>(`delete_session_chat`, {
|
|
|
|
|
serverId,
|
|
|
|
|
sessionId,
|
|
|
|
|
});
|
2025-04-02 14:03:40 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const update_session_chat = (payload: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
sessionId: string;
|
|
|
|
|
title?: string;
|
|
|
|
|
context?: {
|
|
|
|
|
attachments?: string[];
|
|
|
|
|
};
|
|
|
|
|
}): Promise<boolean> => {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler<boolean>("update_session_chat", payload);
|
2025-04-02 14:03:40 +08:00
|
|
|
};
|
2025-04-02 14:45:39 +08:00
|
|
|
|
2025-04-20 21:27:25 +08:00
|
|
|
export const assistant_search = (payload: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
}): Promise<boolean> => {
|
|
|
|
|
return invokeWithErrorHandler<boolean>("assistant_search", payload);
|
|
|
|
|
};
|
|
|
|
|
|
2025-05-27 16:29:43 +08:00
|
|
|
export const assistant_get = (payload: {
|
|
|
|
|
serverId: string;
|
|
|
|
|
assistantId: string;
|
|
|
|
|
}): Promise<boolean> => {
|
|
|
|
|
return invokeWithErrorHandler<boolean>("assistant_get", payload);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const assistant_get_multi = (payload: {
|
|
|
|
|
assistantId: string;
|
|
|
|
|
}): Promise<boolean> => {
|
|
|
|
|
return invokeWithErrorHandler<boolean>("assistant_get_multi", payload);
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-02 14:45:39 +08:00
|
|
|
export const upload_attachment = async (payload: UploadAttachmentPayload) => {
|
2025-04-22 18:32:20 +08:00
|
|
|
const response = await invokeWithErrorHandler<UploadAttachmentResponse>(
|
|
|
|
|
"upload_attachment",
|
|
|
|
|
{
|
|
|
|
|
...payload,
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-04-02 14:45:39 +08:00
|
|
|
|
|
|
|
|
if (response?.acknowledged) {
|
|
|
|
|
return response.attachments;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const get_attachment = (payload: GetAttachmentPayload) => {
|
2025-04-22 18:32:20 +08:00
|
|
|
return invokeWithErrorHandler<GetAttachmentResponse>("get_attachment", {
|
|
|
|
|
...payload,
|
|
|
|
|
});
|
2025-04-02 14:45:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const delete_attachment = (payload: DeleteAttachmentPayload) => {
|
2025-04-18 10:36:00 +08:00
|
|
|
return invokeWithErrorHandler<boolean>("delete_attachment", { ...payload });
|
2025-04-02 14:45:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const transcription = (payload: TranscriptionPayload) => {
|
2025-04-22 18:32:20 +08:00
|
|
|
return invokeWithErrorHandler<TranscriptionResponse>("transcription", {
|
|
|
|
|
...payload,
|
|
|
|
|
});
|
2025-04-18 10:36:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const query_coco_fusion = (payload: {
|
|
|
|
|
from: number;
|
|
|
|
|
size: number;
|
2025-04-23 18:49:42 +08:00
|
|
|
queryStrings: Record<string, string>;
|
|
|
|
|
queryTimeout: number;
|
2025-04-18 10:36:00 +08:00
|
|
|
}) => {
|
2025-04-22 18:32:20 +08:00
|
|
|
return invokeWithErrorHandler<MultiSourceQueryResponse>("query_coco_fusion", {
|
|
|
|
|
...payload,
|
|
|
|
|
});
|
2025-04-02 14:45:39 +08:00
|
|
|
};
|