import { invoke } from '@tauri-apps/api/core'; export interface LocalGatewayStatus { supported: boolean; cliAvailable: boolean; runtimeSource: string | null; runtimePath: string | null; serviceLabel: string | null; serviceLoaded: boolean; serviceStatus: string | null; configOk: boolean; port: number | null; portStatus: string | null; probeUrl: string | null; listenerPids: number[]; error: string | null; raw: Record; } export interface LocalGatewayAuth { configPath: string | null; gatewayToken: string | null; } export interface LocalGatewayPrepareResult { configPath: string | null; originsUpdated: boolean; gatewayRestarted: boolean; } export interface LocalGatewayPairingApprovalResult { approved: boolean; requestId: string | null; deviceId: string | null; } function buildFallbackStatus(supported: boolean, error: string | null = null): LocalGatewayStatus { return { supported, cliAvailable: false, runtimeSource: null, runtimePath: null, serviceLabel: null, serviceLoaded: false, serviceStatus: null, configOk: false, port: null, portStatus: null, probeUrl: null, listenerPids: [], error, raw: {}, }; } export function isTauriRuntime(): boolean { return typeof window !== 'undefined' && '__TAURI_INTERNALS__' in window; } async function callLocalGateway(command: string): Promise { if (!isTauriRuntime()) { return buildFallbackStatus(false); } return invoke(command); } export function getUnsupportedLocalGatewayStatus(): LocalGatewayStatus { return buildFallbackStatus(false); } export async function getLocalGatewayStatus(): Promise { return callLocalGateway('zclaw_status'); } export async function startLocalGateway(): Promise { return callLocalGateway('zclaw_start'); } export async function stopLocalGateway(): Promise { return callLocalGateway('zclaw_stop'); } export async function restartLocalGateway(): Promise { return callLocalGateway('zclaw_restart'); } export async function getLocalGatewayAuth(): Promise { if (!isTauriRuntime()) { return { configPath: null, gatewayToken: null, }; } return invoke('zclaw_local_auth'); } export async function prepareLocalGatewayForTauri(): Promise { if (!isTauriRuntime()) { return { configPath: null, originsUpdated: false, gatewayRestarted: false, }; } return invoke('zclaw_prepare_for_tauri'); } export async function approveLocalGatewayDevicePairing(deviceId: string, publicKeyBase64: string, url?: string): Promise { if (!isTauriRuntime()) { return { approved: false, requestId: null, deviceId: null, }; } return invoke('zclaw_approve_device_pairing', { deviceId, publicKeyBase64, url, }); } // ============================================================================ // Process Monitoring Types and Functions // ============================================================================ export interface ProcessInfo { pid: number; name: string; status: string; cpuPercent: number | null; memoryMb: number | null; uptimeSeconds: number | null; } export interface ProcessListResponse { processes: ProcessInfo[]; totalCount: number; runtimeSource: string | null; } export interface ProcessLogsResponse { pid: number | null; logs: string; lines: number; runtimeSource: string | null; } export interface VersionResponse { version: string; commit: string | null; buildDate: string | null; runtimeSource: string | null; raw: Record; } /** * List ZCLAW processes * @returns List of running ZCLAW processes with their status */ export async function getZclawProcessList(): Promise { if (!isTauriRuntime()) { return { processes: [], totalCount: 0, runtimeSource: null, }; } return invoke('zclaw_process_list'); } /** * Get ZCLAW process logs * @param pid - Optional process ID to get logs for. If not specified, gets main process logs. * @param lines - Number of log lines to retrieve (default: 100) * @returns Process logs */ export async function getZclawProcessLogs( pid?: number, lines?: number ): Promise { if (!isTauriRuntime()) { return { pid: pid ?? null, logs: '', lines: 0, runtimeSource: null, }; } return invoke('zclaw_process_logs', { pid, lines, }); } /** * Get ZCLAW version information * @returns Version information including version string, commit hash, and build date */ export async function getZclawVersion(): Promise { if (!isTauriRuntime()) { return { version: 'unknown', commit: null, buildDate: null, runtimeSource: null, raw: {}, }; } return invoke('zclaw_version'); }