openclaw/src/gateway/call.ts

115 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-12-09 14:41:41 +01:00
import { randomUUID } from "node:crypto";
import { loadConfig, resolveGatewayPort } from "../config/config.js";
2025-12-09 14:41:41 +01:00
import { GatewayClient } from "./client.js";
import { PROTOCOL_VERSION } from "./protocol/index.js";
2025-12-09 14:41:41 +01:00
export type CallGatewayOptions = {
url?: string;
token?: string;
password?: string;
2025-12-09 14:41:41 +01:00
method: string;
params?: unknown;
expectFinal?: boolean;
timeoutMs?: number;
clientName?: string;
clientVersion?: string;
platform?: string;
mode?: string;
instanceId?: string;
minProtocol?: number;
maxProtocol?: number;
};
export async function callGateway<T = unknown>(
opts: CallGatewayOptions,
): Promise<T> {
2025-12-09 14:41:41 +01:00
const timeoutMs = opts.timeoutMs ?? 10_000;
2026-01-01 20:10:50 +01:00
const config = loadConfig();
const isRemoteMode = config.gateway?.mode === "remote";
2026-01-02 17:44:25 +00:00
const remote = isRemoteMode ? config.gateway?.remote : undefined;
const authToken = config.gateway?.auth?.token;
const localPort = resolveGatewayPort(config);
2026-01-01 20:10:50 +01:00
const url =
(typeof opts.url === "string" && opts.url.trim().length > 0
? opts.url.trim()
: undefined) ||
(typeof remote?.url === "string" && remote.url.trim().length > 0
? remote.url.trim()
: undefined) ||
`ws://127.0.0.1:${localPort}`;
2026-01-01 20:10:50 +01:00
const token =
(typeof opts.token === "string" && opts.token.trim().length > 0
? opts.token.trim()
: undefined) ||
(isRemoteMode
? typeof remote?.token === "string" && remote.token.trim().length > 0
? remote.token.trim()
: undefined
: process.env.CLAWDIS_GATEWAY_TOKEN?.trim() ||
(typeof authToken === "string" && authToken.trim().length > 0
? authToken.trim()
: undefined));
const password =
(typeof opts.password === "string" && opts.password.trim().length > 0
? opts.password.trim()
: undefined) ||
2026-01-02 16:47:52 +01:00
process.env.CLAWDIS_GATEWAY_PASSWORD?.trim() ||
(typeof remote?.password === "string" && remote.password.trim().length > 0
? remote.password.trim()
: undefined);
2025-12-09 14:41:41 +01:00
return await new Promise<T>((resolve, reject) => {
let settled = false;
2025-12-29 20:45:50 +01:00
let ignoreClose = false;
2025-12-09 14:41:41 +01:00
const stop = (err?: Error, value?: T) => {
if (settled) return;
settled = true;
clearTimeout(timer);
if (err) reject(err);
else resolve(value as T);
};
const client = new GatewayClient({
2026-01-01 20:10:50 +01:00
url,
token,
password,
2025-12-09 14:41:41 +01:00
instanceId: opts.instanceId ?? randomUUID(),
clientName: opts.clientName ?? "cli",
clientVersion: opts.clientVersion ?? "dev",
platform: opts.platform,
mode: opts.mode ?? "cli",
minProtocol: opts.minProtocol ?? PROTOCOL_VERSION,
maxProtocol: opts.maxProtocol ?? PROTOCOL_VERSION,
2025-12-09 14:41:41 +01:00
onHelloOk: async () => {
try {
const result = await client.request<T>(opts.method, opts.params, {
expectFinal: opts.expectFinal,
});
2025-12-29 20:45:50 +01:00
ignoreClose = true;
2025-12-09 14:41:41 +01:00
stop(undefined, result);
2025-12-29 20:45:50 +01:00
client.stop();
2025-12-09 14:41:41 +01:00
} catch (err) {
2025-12-29 20:45:50 +01:00
ignoreClose = true;
2025-12-09 14:41:41 +01:00
client.stop();
stop(err as Error);
}
},
onClose: (code, reason) => {
2025-12-29 20:45:50 +01:00
if (settled || ignoreClose) return;
2025-12-09 14:41:41 +01:00
stop(new Error(`gateway closed (${code}): ${reason}`));
},
});
const timer = setTimeout(() => {
2025-12-29 20:45:50 +01:00
ignoreClose = true;
2025-12-09 14:41:41 +01:00
client.stop();
stop(new Error("gateway timeout"));
}, timeoutMs);
client.start();
});
}
export function randomIdempotencyKey() {
return randomUUID();
}