Files
zclaw_openfang/desktop/src/lib/tauri-gateway.ts
iven 8bcabbfb43
Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
refactor: 代码质量清理 - 移除死代码和遗留别名
基于全面审计报告的 P0-P2 修复工作:

P0 (已完成):
- intelligence 模块: 精确注释 dead_code 标注原因(Tauri runtime 注册)
- compactor.rs: 实现 LLM 摘要生成(compact_with_llm)
- pipeline_commands.rs: 替换 println! 为 tracing 宏

P1 (已完成):
- 移除 8 个 gateway_* 向后兼容别名(OpenClaw 遗留)
- 前端 tauri-gateway.ts 改为调用 zclaw_* 命令
- 清理 generation.rs 6 个重复的实例方法(-217 行)
- A2A dead_code 注释更新

P2 (已完成):
- Predictor/Lead HAND.toml 设置 enabled=false
- Wasm/Native SkillMode 添加未实现说明
- browser/mod.rs 移除未使用的 re-export(消除 4 个警告)

文档更新:
- feature-checklist.md 从 v0.4.0 更新到 v0.6.0
- CLAUDE.md Hands 状态更新

验证: cargo check 零警告, 42 测试通过, 净减 371 行代码
2026-03-27 00:54:57 +08:00

219 lines
5.1 KiB
TypeScript

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<string, unknown>;
}
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<LocalGatewayStatus> {
if (!isTauriRuntime()) {
return buildFallbackStatus(false);
}
return invoke<LocalGatewayStatus>(command);
}
export function getUnsupportedLocalGatewayStatus(): LocalGatewayStatus {
return buildFallbackStatus(false);
}
export async function getLocalGatewayStatus(): Promise<LocalGatewayStatus> {
return callLocalGateway('zclaw_status');
}
export async function startLocalGateway(): Promise<LocalGatewayStatus> {
return callLocalGateway('zclaw_start');
}
export async function stopLocalGateway(): Promise<LocalGatewayStatus> {
return callLocalGateway('zclaw_stop');
}
export async function restartLocalGateway(): Promise<LocalGatewayStatus> {
return callLocalGateway('zclaw_restart');
}
export async function getLocalGatewayAuth(): Promise<LocalGatewayAuth> {
if (!isTauriRuntime()) {
return {
configPath: null,
gatewayToken: null,
};
}
return invoke<LocalGatewayAuth>('zclaw_local_auth');
}
export async function prepareLocalGatewayForTauri(): Promise<LocalGatewayPrepareResult> {
if (!isTauriRuntime()) {
return {
configPath: null,
originsUpdated: false,
gatewayRestarted: false,
};
}
return invoke<LocalGatewayPrepareResult>('zclaw_prepare_for_tauri');
}
export async function approveLocalGatewayDevicePairing(deviceId: string, publicKeyBase64: string, url?: string): Promise<LocalGatewayPairingApprovalResult> {
if (!isTauriRuntime()) {
return {
approved: false,
requestId: null,
deviceId: null,
};
}
return invoke<LocalGatewayPairingApprovalResult>('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<string, unknown>;
}
/**
* List ZCLAW processes
* @returns List of running ZCLAW processes with their status
*/
export async function getZclawProcessList(): Promise<ProcessListResponse> {
if (!isTauriRuntime()) {
return {
processes: [],
totalCount: 0,
runtimeSource: null,
};
}
return invoke<ProcessListResponse>('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<ProcessLogsResponse> {
if (!isTauriRuntime()) {
return {
pid: pid ?? null,
logs: '',
lines: 0,
runtimeSource: null,
};
}
return invoke<ProcessLogsResponse>('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<VersionResponse> {
if (!isTauriRuntime()) {
return {
version: 'unknown',
commit: null,
buildDate: null,
runtimeSource: null,
raw: {},
};
}
return invoke<VersionResponse>('zclaw_version');
}