2025-10-25 16:48:43 +08:00
|
|
|
import { http, HttpResponse } from "msw";
|
2025-10-30 14:59:15 +08:00
|
|
|
import type { AppId } from "@/lib/api/types";
|
2025-10-26 13:52:42 +08:00
|
|
|
import type { McpServer, Provider, Settings } from "@/types";
|
2025-10-25 16:48:43 +08:00
|
|
|
import {
|
|
|
|
|
addProvider,
|
|
|
|
|
deleteProvider,
|
|
|
|
|
getCurrentProviderId,
|
|
|
|
|
getProviders,
|
|
|
|
|
listProviders,
|
|
|
|
|
resetProviderState,
|
|
|
|
|
setCurrentProviderId,
|
|
|
|
|
updateProvider,
|
|
|
|
|
updateSortOrder,
|
2025-10-25 19:59:31 +08:00
|
|
|
getSettings,
|
|
|
|
|
setSettings,
|
|
|
|
|
getAppConfigDirOverride,
|
|
|
|
|
setAppConfigDirOverrideState,
|
2025-10-26 13:52:42 +08:00
|
|
|
getMcpConfig,
|
|
|
|
|
setMcpServerEnabled,
|
|
|
|
|
upsertMcpServer,
|
|
|
|
|
deleteMcpServer,
|
2025-10-25 16:48:43 +08:00
|
|
|
} from "./state";
|
|
|
|
|
|
|
|
|
|
const TAURI_ENDPOINT = "http://tauri.local";
|
|
|
|
|
|
|
|
|
|
const withJson = async <T>(request: Request): Promise<T> => {
|
|
|
|
|
try {
|
|
|
|
|
const body = await request.text();
|
|
|
|
|
if (!body) return {} as T;
|
|
|
|
|
return JSON.parse(body) as T;
|
|
|
|
|
} catch {
|
|
|
|
|
return {} as T;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const success = <T>(payload: T) => HttpResponse.json(payload as any);
|
|
|
|
|
|
|
|
|
|
export const handlers = [
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/get_providers`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { app } = await withJson<{ app: AppId }>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
return success(getProviders(app));
|
2025-10-25 16:48:43 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/get_current_provider`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { app } = await withJson<{ app: AppId }>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
return success(getCurrentProviderId(app));
|
2025-10-25 16:48:43 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/update_providers_sort_order`, async ({ request }) => {
|
2025-10-30 11:35:14 +08:00
|
|
|
const { updates = [], app } = await withJson<{
|
2025-10-25 16:48:43 +08:00
|
|
|
updates: { id: string; sortIndex: number }[];
|
2025-10-30 14:59:15 +08:00
|
|
|
app: AppId;
|
2025-10-25 16:48:43 +08:00
|
|
|
}>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
updateSortOrder(app, updates);
|
2025-10-25 16:48:43 +08:00
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/update_tray_menu`, () => success(true)),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/switch_provider`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { id, app } = await withJson<{ id: string; app: AppId }>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
const providers = listProviders(app);
|
2025-10-25 16:48:43 +08:00
|
|
|
if (!providers[id]) {
|
|
|
|
|
return HttpResponse.json(false, { status: 404 });
|
|
|
|
|
}
|
2025-10-30 11:35:14 +08:00
|
|
|
setCurrentProviderId(app, id);
|
2025-10-25 16:48:43 +08:00
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/add_provider`, async ({ request }) => {
|
2025-10-30 11:35:14 +08:00
|
|
|
const { provider, app } = await withJson<{
|
2025-10-25 16:48:43 +08:00
|
|
|
provider: Provider & { id?: string };
|
2025-10-30 14:59:15 +08:00
|
|
|
app: AppId;
|
2025-10-25 16:48:43 +08:00
|
|
|
}>(request);
|
|
|
|
|
|
|
|
|
|
const newId = provider.id ?? `mock-${Date.now()}`;
|
2025-10-30 11:35:14 +08:00
|
|
|
addProvider(app, { ...provider, id: newId });
|
2025-10-25 16:48:43 +08:00
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/update_provider`, async ({ request }) => {
|
2025-10-30 11:35:14 +08:00
|
|
|
const { provider, app } = await withJson<{
|
2025-10-25 16:48:43 +08:00
|
|
|
provider: Provider;
|
2025-10-30 14:59:15 +08:00
|
|
|
app: AppId;
|
2025-10-25 16:48:43 +08:00
|
|
|
}>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
updateProvider(app, provider);
|
2025-10-25 16:48:43 +08:00
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/delete_provider`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { id, app } = await withJson<{ id: string; app: AppId }>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
deleteProvider(app, id);
|
2025-10-25 16:48:43 +08:00
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/import_default_config`, async () => {
|
|
|
|
|
resetProviderState();
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/open_external`, () => success(true)),
|
|
|
|
|
|
2025-10-26 13:52:42 +08:00
|
|
|
// MCP APIs
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/get_mcp_config`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { app } = await withJson<{ app: AppId }>(request);
|
2025-10-26 13:52:42 +08:00
|
|
|
return success(getMcpConfig(app));
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/import_mcp_from_claude`, () => success(1)),
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/import_mcp_from_codex`, () => success(1)),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/set_mcp_enabled`, async ({ request }) => {
|
|
|
|
|
const { app, id, enabled } = await withJson<{
|
2025-10-30 14:59:15 +08:00
|
|
|
app: AppId;
|
2025-10-26 13:52:42 +08:00
|
|
|
id: string;
|
|
|
|
|
enabled: boolean;
|
|
|
|
|
}>(request);
|
|
|
|
|
setMcpServerEnabled(app, id, enabled);
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/upsert_mcp_server_in_config`, async ({ request }) => {
|
|
|
|
|
const { app, id, spec } = await withJson<{
|
2025-10-30 14:59:15 +08:00
|
|
|
app: AppId;
|
2025-10-26 13:52:42 +08:00
|
|
|
id: string;
|
|
|
|
|
spec: McpServer;
|
|
|
|
|
}>(request);
|
|
|
|
|
upsertMcpServer(app, id, spec);
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/delete_mcp_server_in_config`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { app, id } = await withJson<{ app: AppId; id: string }>(request);
|
2025-10-26 13:52:42 +08:00
|
|
|
deleteMcpServer(app, id);
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
2025-10-25 16:48:43 +08:00
|
|
|
http.post(`${TAURI_ENDPOINT}/restart_app`, () => success(true)),
|
2025-10-25 19:59:31 +08:00
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/get_settings`, () => success(getSettings())),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/save_settings`, async ({ request }) => {
|
|
|
|
|
const { settings } = await withJson<{ settings: Settings }>(request);
|
|
|
|
|
setSettings(settings);
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/set_app_config_dir_override`, async ({ request }) => {
|
|
|
|
|
const { path } = await withJson<{ path: string | null }>(request);
|
|
|
|
|
setAppConfigDirOverrideState(path ?? null);
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/get_app_config_dir_override`, () =>
|
|
|
|
|
success(getAppConfigDirOverride()),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/apply_claude_plugin_config`, async ({ request }) => {
|
|
|
|
|
const { official } = await withJson<{ official: boolean }>(request);
|
|
|
|
|
setSettings({ enableClaudePluginIntegration: !official });
|
|
|
|
|
return success(true);
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/get_config_dir`, async ({ request }) => {
|
2025-10-30 14:59:15 +08:00
|
|
|
const { app } = await withJson<{ app: AppId }>(request);
|
2025-10-30 11:35:14 +08:00
|
|
|
return success(app === "claude" ? "/default/claude" : "/default/codex");
|
2025-10-25 19:59:31 +08:00
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/is_portable_mode`, () => success(false)),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/select_config_directory`, async ({ request }) => {
|
|
|
|
|
const { default_path } = await withJson<{ default_path?: string }>(request);
|
2025-10-25 20:43:47 +08:00
|
|
|
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/pick_directory`, async ({ request }) => {
|
|
|
|
|
const { default_path } = await withJson<{ default_path?: string }>(request);
|
2025-10-25 19:59:31 +08:00
|
|
|
return success(default_path ? `${default_path}/picked` : "/mock/selected-dir");
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/open_file_dialog`, () =>
|
|
|
|
|
success("/mock/import-settings.json"),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/import_config_from_file`, async ({ request }) => {
|
|
|
|
|
const { filePath } = await withJson<{ filePath: string }>(request);
|
|
|
|
|
if (!filePath) {
|
|
|
|
|
return success({ success: false, message: "Missing file" });
|
|
|
|
|
}
|
|
|
|
|
setSettings({ language: "en" });
|
|
|
|
|
return success({ success: true, backupId: "backup-123" });
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/export_config_to_file`, async ({ request }) => {
|
|
|
|
|
const { filePath } = await withJson<{ filePath: string }>(request);
|
|
|
|
|
if (!filePath) {
|
|
|
|
|
return success({ success: false, message: "Invalid destination" });
|
|
|
|
|
}
|
|
|
|
|
return success({ success: true, filePath });
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/save_file_dialog`, () =>
|
|
|
|
|
success("/mock/export-settings.json"),
|
|
|
|
|
),
|
2025-10-30 10:07:30 +08:00
|
|
|
|
|
|
|
|
// Sync current providers live (no-op success)
|
|
|
|
|
http.post(`${TAURI_ENDPOINT}/sync_current_providers_live`, () =>
|
|
|
|
|
success({ success: true }),
|
|
|
|
|
),
|
2025-10-25 16:48:43 +08:00
|
|
|
];
|