* Make an RPC call to the Gateway * Uses OpenClaw protocol format: { type: "req", id: "...", method: "...", params: {...} }
(method: string, params?: unknown, timeoutMs = 30000)
| 865 | * Uses OpenClaw protocol format: { type: "req", id: "...", method: "...", params: {...} } |
| 866 | */ |
| 867 | async rpc<T>(method: string, params?: unknown, timeoutMs = 30000): Promise<T> { |
| 868 | const startedAt = Date.now(); |
| 869 | return await new Promise<T>((resolve, reject) => { |
| 870 | if (!this.ws || this.ws.readyState !== WebSocket.OPEN) { |
| 871 | reject(new Error('Gateway not connected')); |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | const id = crypto.randomUUID(); |
| 876 | |
| 877 | // Set timeout for request |
| 878 | const timeout = setTimeout(() => { |
| 879 | rejectPendingGatewayRequest(this.pendingRequests, id, new Error(`RPC timeout: ${method}`)); |
| 880 | }, timeoutMs); |
| 881 | |
| 882 | // Store pending request |
| 883 | this.pendingRequests.set(id, { |
| 884 | resolve: resolve as (value: unknown) => void, |
| 885 | reject, |
| 886 | timeout, |
| 887 | }); |
| 888 | |
| 889 | // Send request using OpenClaw protocol format |
| 890 | const request = { |
| 891 | type: 'req', |
| 892 | id, |
| 893 | method, |
| 894 | params, |
| 895 | }; |
| 896 | |
| 897 | try { |
| 898 | if (isGatewayWsTraceEnabled()) { |
| 899 | logger.debug('[gateway-ws-trace] send', { |
| 900 | summary: summarizeGatewayFrameForTrace(request), |
| 901 | frame: redactGatewayFrameForTrace(request), |
| 902 | }); |
| 903 | } |
| 904 | this.ws.send(JSON.stringify(request)); |
| 905 | } catch (error) { |
| 906 | rejectPendingGatewayRequest(this.pendingRequests, id, new Error(`Failed to send RPC request: ${error}`)); |
| 907 | } |
| 908 | }).then((result) => { |
| 909 | this.recordRpcSuccess(); |
| 910 | if (isCoreRpcMethod(method)) { |
| 911 | this.capabilityMonitor.recordCoreProbe({ |
| 912 | ok: true, |
| 913 | checkedAt: Date.now(), |
| 914 | durationMs: Date.now() - startedAt, |
| 915 | }); |
| 916 | } |
| 917 | const capability = classifyCapabilityMethod(method); |
| 918 | if (capability) { |
| 919 | this.capabilityMonitor.recordCapabilitySuccess( |
| 920 | capability, |
| 921 | result as GatewayRuntimePayload, |
| 922 | Date.now() - startedAt, |
| 923 | ); |
| 924 | } |
no test coverage detected