Skip to content

Commit 36c8282

Browse files
shakkernerdsteipete
authored andcommitted
refactor: lazy load cli gateway helper runtimes
1 parent 58f1044 commit 36c8282

4 files changed

Lines changed: 65 additions & 26 deletions

File tree

src/cli/logs-cli.runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { buildGatewayConnectionDetails } from "../gateway/call.js";

src/cli/logs-cli.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { setTimeout as delay } from "node:timers/promises";
22
import type { Command } from "commander";
3-
import { buildGatewayConnectionDetails } from "../gateway/call.js";
43
import { parseLogLine } from "../logging/parse-log-line.js";
54
import { formatTimestamp, isValidTimeZone } from "../logging/timestamps.js";
65
import { formatDocsLink } from "../terminal/links.js";
@@ -10,6 +9,15 @@ import { colorize, isRich, theme } from "../terminal/theme.js";
109
import { formatCliCommand } from "./command-format.js";
1110
import { addGatewayClientOptions, callGatewayFromCli } from "./gateway-rpc.js";
1211

12+
type LogsCliRuntimeModule = typeof import("./logs-cli.runtime.js");
13+
14+
let logsCliRuntimePromise: Promise<LogsCliRuntimeModule> | undefined;
15+
16+
async function loadLogsCliRuntime(): Promise<LogsCliRuntimeModule> {
17+
logsCliRuntimePromise ??= import("./logs-cli.runtime.js");
18+
return logsCliRuntimePromise;
19+
}
20+
1321
type LogsTailPayload = {
1422
file?: string;
1523
cursor?: number;
@@ -149,19 +157,20 @@ function createLogWriters() {
149157
};
150158
}
151159

152-
function emitGatewayError(
160+
async function emitGatewayError(
153161
err: unknown,
154162
opts: LogsCliOptions,
155163
mode: "json" | "text",
156164
rich: boolean,
157165
emitJsonLine: (payload: Record<string, unknown>, toStdErr?: boolean) => boolean,
158166
errorLine: (text: string) => boolean,
159167
) {
160-
const details = buildGatewayConnectionDetails({ url: opts.url });
168+
const runtime = await loadLogsCliRuntime();
161169
const message = "Gateway not reachable. Is it running and accessible?";
162170
const hint = `Hint: run \`${formatCliCommand("openclaw doctor")}\`.`;
163171
const errorText = err instanceof Error ? err.message : String(err);
164172

173+
const details = runtime.buildGatewayConnectionDetails({ url: opts.url });
165174
if (mode === "json") {
166175
if (
167176
!emitJsonLine(
@@ -227,7 +236,14 @@ export function registerLogsCli(program: Command) {
227236
try {
228237
payload = await fetchLogs(opts, cursor, showProgress);
229238
} catch (err) {
230-
emitGatewayError(err, opts, jsonMode ? "json" : "text", rich, emitJsonLine, errorLine);
239+
await emitGatewayError(
240+
err,
241+
opts,
242+
jsonMode ? "json" : "text",
243+
rich,
244+
emitJsonLine,
245+
errorLine,
246+
);
231247
process.exit(1);
232248
return;
233249
}

src/cli/nodes-cli/rpc.runtime.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { callGateway } from "../../gateway/call.js";
2+
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../../utils/message-channel.js";
3+
import { withProgress } from "../progress.js";
4+
import type { NodesRpcOpts } from "./types.js";
5+
6+
export async function callGatewayCliRuntime(
7+
method: string,
8+
opts: NodesRpcOpts,
9+
params?: unknown,
10+
callOpts?: { transportTimeoutMs?: number },
11+
) {
12+
return await withProgress(
13+
{
14+
label: `Nodes ${method}`,
15+
indeterminate: true,
16+
enabled: opts.json !== true,
17+
},
18+
async () =>
19+
await callGateway({
20+
url: opts.url,
21+
token: opts.token,
22+
method,
23+
params,
24+
timeoutMs: callOpts?.transportTimeoutMs ?? Number(opts.timeout ?? 10_000),
25+
clientName: GATEWAY_CLIENT_NAMES.CLI,
26+
mode: GATEWAY_CLIENT_MODES.CLI,
27+
}),
28+
);
29+
}

src/cli/nodes-cli/rpc.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import { randomUUID } from "node:crypto";
12
import type { Command } from "commander";
2-
import { callGateway, randomIdempotencyKey } from "../../gateway/call.js";
33
import { resolveNodeFromNodeList } from "../../shared/node-resolve.js";
4-
import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../../utils/message-channel.js";
5-
import { withProgress } from "../progress.js";
64
import { parseNodeList, parsePairingList } from "./format.js";
75
import type { NodeListNode, NodesRpcOpts } from "./types.js";
86

7+
type NodesCliRpcRuntimeModule = typeof import("./rpc.runtime.js");
8+
9+
let nodesCliRpcRuntimePromise: Promise<NodesCliRpcRuntimeModule> | undefined;
10+
11+
async function loadNodesCliRpcRuntime(): Promise<NodesCliRpcRuntimeModule> {
12+
nodesCliRpcRuntimePromise ??= import("./rpc.runtime.js");
13+
return nodesCliRpcRuntimePromise;
14+
}
15+
916
export const nodesCallOpts = (cmd: Command, defaults?: { timeoutMs?: number }) =>
1017
cmd
1118
.option("--url <url>", "Gateway WebSocket URL (defaults to gateway.remote.url when configured)")
@@ -18,24 +25,10 @@ export const callGatewayCli = async (
1825
opts: NodesRpcOpts,
1926
params?: unknown,
2027
callOpts?: { transportTimeoutMs?: number },
21-
) =>
22-
withProgress(
23-
{
24-
label: `Nodes ${method}`,
25-
indeterminate: true,
26-
enabled: opts.json !== true,
27-
},
28-
async () =>
29-
await callGateway({
30-
url: opts.url,
31-
token: opts.token,
32-
method,
33-
params,
34-
timeoutMs: callOpts?.transportTimeoutMs ?? Number(opts.timeout ?? 10_000),
35-
clientName: GATEWAY_CLIENT_NAMES.CLI,
36-
mode: GATEWAY_CLIENT_MODES.CLI,
37-
}),
38-
);
28+
) => {
29+
const runtime = await loadNodesCliRpcRuntime();
30+
return await runtime.callGatewayCliRuntime(method, opts, params, callOpts);
31+
};
3932

4033
export function buildNodeInvokeParams(params: {
4134
nodeId: string;
@@ -48,7 +41,7 @@ export function buildNodeInvokeParams(params: {
4841
nodeId: params.nodeId,
4942
command: params.command,
5043
params: params.params,
51-
idempotencyKey: params.idempotencyKey ?? randomIdempotencyKey(),
44+
idempotencyKey: params.idempotencyKey ?? randomUUID(),
5245
};
5346
if (typeof params.timeoutMs === "number" && Number.isFinite(params.timeoutMs)) {
5447
invokeParams.timeoutMs = params.timeoutMs;

0 commit comments

Comments
 (0)