Files
cc-switch/src/lib/api/settings.ts
T
Dex Miller 96a0a37331 feat(terminal): add directory picker before launching Claude terminal (#1752)
* Add directory picker before launching Claude terminal

* fix(terminal): preserve cwd path and strip Windows verbatim prefix

- Stop trimming non-empty paths so directories with leading/trailing
  spaces on Unix are handled correctly
- Strip \\?\ extended-length prefix from canonicalized paths on Windows
  to prevent batch script cd failures

* fix(terminal): restore UNC paths when stripping Windows verbatim prefix

Handle \\?\UNC\server\share form separately from regular \\?\ prefix,
converting it back to \\server\share so network/WSL directory paths
remain valid in batch cd commands.

* fix(terminal): use pushd for UNC paths in Windows batch launcher

`cmd.exe` cannot set a UNC path (e.g. `\\wsl$\...`) as the current
directory via `cd /d`; it errors with "CMD does not support UNC paths
as current directories". Switch to `pushd` which temporarily maps the
UNC share to a drive letter.

Rename `build_windows_cd_command` → `build_windows_cwd_command` to
reflect the broader semantics. Extract `build_windows_cwd_command_str`
and `is_windows_unc_path` helpers for testability, and add unit tests
covering drive paths, UNC paths, and batch metacharacter escaping.

Also fix minor style issues: sort mod declarations alphabetically,
add missing EOF newline in lightweight.rs, add explicit type annotation
in streaming_responses test, and reformat tray menu builder chain.
2026-03-31 15:10:08 +08:00

265 lines
7.0 KiB
TypeScript

import { invoke } from "@tauri-apps/api/core";
import type { Settings, WebDavSyncSettings, RemoteSnapshotInfo } from "@/types";
import type { AppId } from "./types";
export interface ConfigTransferResult {
success: boolean;
message: string;
filePath?: string;
backupId?: string;
}
export interface WebDavTestResult {
success: boolean;
message?: string;
}
export interface WebDavSyncResult {
status: string;
}
export const settingsApi = {
async get(): Promise<Settings> {
return await invoke("get_settings");
},
async save(settings: Settings): Promise<boolean> {
return await invoke("save_settings", { settings });
},
async restart(): Promise<boolean> {
return await invoke("restart_app");
},
async checkUpdates(): Promise<void> {
await invoke("check_for_updates");
},
async isPortable(): Promise<boolean> {
return await invoke("is_portable_mode");
},
async getConfigDir(appId: AppId): Promise<string> {
return await invoke("get_config_dir", { app: appId });
},
async openConfigFolder(appId: AppId): Promise<void> {
await invoke("open_config_folder", { app: appId });
},
async pickDirectory(defaultPath?: string): Promise<string | null> {
return await invoke("pick_directory", { defaultPath });
},
async selectConfigDirectory(defaultPath?: string): Promise<string | null> {
return await invoke("pick_directory", { defaultPath });
},
async getClaudeCodeConfigPath(): Promise<string> {
return await invoke("get_claude_code_config_path");
},
async getAppConfigPath(): Promise<string> {
return await invoke("get_app_config_path");
},
async openAppConfigFolder(): Promise<void> {
await invoke("open_app_config_folder");
},
async getAppConfigDirOverride(): Promise<string | null> {
return await invoke("get_app_config_dir_override");
},
async setAppConfigDirOverride(path: string | null): Promise<boolean> {
return await invoke("set_app_config_dir_override", { path });
},
async applyClaudePluginConfig(options: {
official: boolean;
}): Promise<boolean> {
const { official } = options;
return await invoke("apply_claude_plugin_config", { official });
},
async applyClaudeOnboardingSkip(): Promise<boolean> {
return await invoke("apply_claude_onboarding_skip");
},
async clearClaudeOnboardingSkip(): Promise<boolean> {
return await invoke("clear_claude_onboarding_skip");
},
async saveFileDialog(defaultName: string): Promise<string | null> {
return await invoke("save_file_dialog", { defaultName });
},
async openFileDialog(): Promise<string | null> {
return await invoke("open_file_dialog");
},
async exportConfigToFile(filePath: string): Promise<ConfigTransferResult> {
return await invoke("export_config_to_file", { filePath });
},
async importConfigFromFile(filePath: string): Promise<ConfigTransferResult> {
return await invoke("import_config_from_file", { filePath });
},
// ─── WebDAV sync ──────────────────────────────────────────
async webdavTestConnection(
settings: WebDavSyncSettings,
preserveEmptyPassword = true,
): Promise<WebDavTestResult> {
return await invoke("webdav_test_connection", {
settings,
preserveEmptyPassword,
});
},
async webdavSyncUpload(): Promise<WebDavSyncResult> {
return await invoke("webdav_sync_upload");
},
async webdavSyncDownload(): Promise<WebDavSyncResult> {
return await invoke("webdav_sync_download");
},
async webdavSyncSaveSettings(
settings: WebDavSyncSettings,
passwordTouched = false,
): Promise<{ success: boolean }> {
return await invoke("webdav_sync_save_settings", {
settings,
passwordTouched,
});
},
async webdavSyncFetchRemoteInfo(): Promise<
RemoteSnapshotInfo | { empty: true }
> {
return await invoke("webdav_sync_fetch_remote_info");
},
async syncCurrentProvidersLive(): Promise<void> {
const result = (await invoke("sync_current_providers_live")) as {
success?: boolean;
message?: string;
};
if (!result?.success) {
throw new Error(result?.message || "Sync current providers failed");
}
},
async openExternal(url: string): Promise<void> {
try {
const u = new URL(url);
const scheme = u.protocol.replace(":", "").toLowerCase();
if (scheme !== "http" && scheme !== "https") {
throw new Error("Unsupported URL scheme");
}
} catch {
throw new Error("Invalid URL");
}
await invoke("open_external", { url });
},
async setAutoLaunch(enabled: boolean): Promise<boolean> {
return await invoke("set_auto_launch", { enabled });
},
async getAutoLaunchStatus(): Promise<boolean> {
return await invoke("get_auto_launch_status");
},
async getToolVersions(
tools?: string[],
wslShellByTool?: Record<
string,
{ wslShell?: string | null; wslShellFlag?: string | null }
>,
): Promise<
Array<{
name: string;
version: string | null;
latest_version: string | null;
error: string | null;
env_type: "windows" | "wsl" | "macos" | "linux" | "unknown";
wsl_distro: string | null;
}>
> {
return await invoke("get_tool_versions", { tools, wslShellByTool });
},
async getRectifierConfig(): Promise<RectifierConfig> {
return await invoke("get_rectifier_config");
},
async setRectifierConfig(config: RectifierConfig): Promise<boolean> {
return await invoke("set_rectifier_config", { config });
},
async getOptimizerConfig(): Promise<OptimizerConfig> {
return await invoke("get_optimizer_config");
},
async setOptimizerConfig(config: OptimizerConfig): Promise<boolean> {
return await invoke("set_optimizer_config", { config });
},
async getLogConfig(): Promise<LogConfig> {
return await invoke("get_log_config");
},
async setLogConfig(config: LogConfig): Promise<boolean> {
return await invoke("set_log_config", { config });
},
};
export interface RectifierConfig {
enabled: boolean;
requestThinkingSignature: boolean;
requestThinkingBudget: boolean;
}
export interface OptimizerConfig {
enabled: boolean;
thinkingOptimizer: boolean;
cacheInjection: boolean;
cacheTtl: string;
}
export interface LogConfig {
enabled: boolean;
level: "error" | "warn" | "info" | "debug" | "trace";
}
export interface BackupEntry {
filename: string;
sizeBytes: number;
createdAt: string;
}
export const backupsApi = {
async createDbBackup(): Promise<string> {
return await invoke("create_db_backup");
},
async listDbBackups(): Promise<BackupEntry[]> {
return await invoke("list_db_backups");
},
async restoreDbBackup(filename: string): Promise<string> {
return await invoke("restore_db_backup", { filename });
},
async renameDbBackup(oldFilename: string, newName: string): Promise<string> {
return await invoke("rename_db_backup", { oldFilename, newName });
},
async deleteDbBackup(filename: string): Promise<void> {
await invoke("delete_db_backup", { filename });
},
};