mirror of
https://github.com/ValueCell-ai/ClawX.git
synced 2026-08-14 00:48:10 +00:00
68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import type { GatewayManager } from '../gateway/manager';
|
|
import type { CompleteHostServiceRegistry } from '../main/ipc/host-contract';
|
|
import { PORTS } from '../utils/config';
|
|
import { approvePendingLocalDeviceRequests } from '../utils/control-ui-device-pairing';
|
|
import { logger } from '../utils/logger';
|
|
import { buildOpenClawControlUiUrl } from '../utils/openclaw-control-ui';
|
|
import { getSetting } from '../utils/store';
|
|
import { isRecord } from './payload-utils';
|
|
|
|
type HealthPayload = {
|
|
probe?: unknown;
|
|
};
|
|
|
|
type RpcPayload = {
|
|
method?: unknown;
|
|
params?: unknown;
|
|
timeoutMs?: unknown;
|
|
};
|
|
|
|
function parseTimeoutMs(timeoutMs: unknown): number | undefined {
|
|
if (timeoutMs === undefined) return undefined;
|
|
if (typeof timeoutMs !== 'number' || !Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
throw new Error('Invalid gateway RPC timeout');
|
|
}
|
|
return timeoutMs;
|
|
}
|
|
|
|
export function createGatewayApi(gatewayManager: GatewayManager): CompleteHostServiceRegistry['gateway'] {
|
|
return {
|
|
status: () => gatewayManager.getStatus(),
|
|
start: async () => {
|
|
await gatewayManager.start();
|
|
return { success: true };
|
|
},
|
|
stop: async () => {
|
|
await gatewayManager.stop();
|
|
return { success: true };
|
|
},
|
|
restart: async () => {
|
|
await gatewayManager.restart();
|
|
return { success: true };
|
|
},
|
|
health: async (payload) => {
|
|
const body = isRecord(payload) ? payload as HealthPayload : {};
|
|
return gatewayManager.checkHealth({ probe: body.probe === true });
|
|
},
|
|
controlUi: async () => {
|
|
const status = gatewayManager.getStatus();
|
|
const token = await getSetting('gatewayToken');
|
|
const port = status.port || PORTS.OPENCLAW_GATEWAY;
|
|
const url = buildOpenClawControlUiUrl(port, token);
|
|
void approvePendingLocalDeviceRequests(gatewayManager).catch((error) => {
|
|
logger.debug(`[gateway] Control UI device auto-approve skipped: ${String(error)}`);
|
|
});
|
|
return { success: true, url, token, port };
|
|
},
|
|
rpc: async (payload) => {
|
|
const body = isRecord(payload) ? payload as RpcPayload : {};
|
|
const method = typeof body.method === 'string' ? body.method.trim() : '';
|
|
if (!method) {
|
|
throw new Error('Invalid gateway RPC method');
|
|
}
|
|
const timeoutMs = parseTimeoutMs(body.timeoutMs);
|
|
return gatewayManager.rpc(method, body.params, timeoutMs);
|
|
},
|
|
};
|
|
}
|