45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { callGateway } from "../../gateway/call.js";
|
|
|
|
export const DEFAULT_GATEWAY_URL = "ws://127.0.0.1:18789";
|
|
|
|
export type GatewayCallOptions = {
|
|
gatewayUrl?: string;
|
|
gatewayToken?: string;
|
|
timeoutMs?: number;
|
|
};
|
|
|
|
export function resolveGatewayOptions(opts?: GatewayCallOptions) {
|
|
const url =
|
|
typeof opts?.gatewayUrl === "string" && opts.gatewayUrl.trim()
|
|
? opts.gatewayUrl.trim()
|
|
: DEFAULT_GATEWAY_URL;
|
|
const token =
|
|
typeof opts?.gatewayToken === "string" && opts.gatewayToken.trim()
|
|
? opts.gatewayToken.trim()
|
|
: undefined;
|
|
const timeoutMs =
|
|
typeof opts?.timeoutMs === "number" && Number.isFinite(opts.timeoutMs)
|
|
? Math.max(1, Math.floor(opts.timeoutMs))
|
|
: 10_000;
|
|
return { url, token, timeoutMs };
|
|
}
|
|
|
|
export async function callGatewayTool<T = unknown>(
|
|
method: string,
|
|
opts: GatewayCallOptions,
|
|
params?: unknown,
|
|
extra?: { expectFinal?: boolean },
|
|
) {
|
|
const gateway = resolveGatewayOptions(opts);
|
|
return await callGateway<T>({
|
|
url: gateway.url,
|
|
token: gateway.token,
|
|
method,
|
|
params,
|
|
timeoutMs: gateway.timeoutMs,
|
|
expectFinal: extra?.expectFinal,
|
|
clientName: "agent",
|
|
mode: "agent",
|
|
});
|
|
}
|