mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-27 00:17:29 +00:00
refactor: dedupe approval gateway resolver setup
This commit is contained in:
@@ -2,31 +2,21 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const gatewayRuntimeHoisted = vi.hoisted(() => ({
|
||||
requestSpy: vi.fn(),
|
||||
startSpy: vi.fn(),
|
||||
stopSpy: vi.fn(),
|
||||
stopAndWaitSpy: vi.fn(async () => undefined),
|
||||
createClientSpy: vi.fn(),
|
||||
withClientSpy: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/gateway-runtime", () => ({
|
||||
createOperatorApprovalsGatewayClient: gatewayRuntimeHoisted.createClientSpy,
|
||||
withOperatorApprovalsGatewayClient: gatewayRuntimeHoisted.withClientSpy,
|
||||
}));
|
||||
|
||||
describe("resolveTelegramExecApproval", () => {
|
||||
beforeEach(() => {
|
||||
gatewayRuntimeHoisted.requestSpy.mockReset();
|
||||
gatewayRuntimeHoisted.startSpy.mockReset();
|
||||
gatewayRuntimeHoisted.stopSpy.mockReset();
|
||||
gatewayRuntimeHoisted.stopAndWaitSpy.mockReset().mockResolvedValue(undefined);
|
||||
gatewayRuntimeHoisted.createClientSpy.mockReset().mockImplementation((opts) => ({
|
||||
start: () => {
|
||||
gatewayRuntimeHoisted.startSpy();
|
||||
opts.onHelloOk?.();
|
||||
},
|
||||
request: gatewayRuntimeHoisted.requestSpy,
|
||||
stop: gatewayRuntimeHoisted.stopSpy,
|
||||
stopAndWait: gatewayRuntimeHoisted.stopAndWaitSpy,
|
||||
}));
|
||||
gatewayRuntimeHoisted.withClientSpy.mockReset().mockImplementation(async (_params, run) => {
|
||||
await run({
|
||||
request: gatewayRuntimeHoisted.requestSpy,
|
||||
} as never);
|
||||
});
|
||||
});
|
||||
|
||||
it("routes plugin approval ids through plugin.approval.resolve", async () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import { isApprovalNotFoundError } from "openclaw/plugin-sdk/error-runtime";
|
||||
import { createOperatorApprovalsGatewayClient } from "openclaw/plugin-sdk/gateway-runtime";
|
||||
import { withOperatorApprovalsGatewayClient } from "openclaw/plugin-sdk/gateway-runtime";
|
||||
import type { ExecApprovalReplyDecision } from "openclaw/plugin-sdk/infra-runtime";
|
||||
|
||||
export type ResolveTelegramExecApprovalParams = {
|
||||
@@ -15,69 +15,33 @@ export type ResolveTelegramExecApprovalParams = {
|
||||
export async function resolveTelegramExecApproval(
|
||||
params: ResolveTelegramExecApprovalParams,
|
||||
): Promise<void> {
|
||||
let readySettled = false;
|
||||
let resolveReady!: () => void;
|
||||
let rejectReady!: (err: unknown) => void;
|
||||
const ready = new Promise<void>((resolve, reject) => {
|
||||
resolveReady = resolve;
|
||||
rejectReady = reject;
|
||||
});
|
||||
const markReady = () => {
|
||||
if (readySettled) {
|
||||
return;
|
||||
}
|
||||
readySettled = true;
|
||||
resolveReady();
|
||||
};
|
||||
const failReady = (err: unknown) => {
|
||||
if (readySettled) {
|
||||
return;
|
||||
}
|
||||
readySettled = true;
|
||||
rejectReady(err);
|
||||
};
|
||||
|
||||
const gatewayClient = await createOperatorApprovalsGatewayClient({
|
||||
config: params.cfg,
|
||||
gatewayUrl: params.gatewayUrl,
|
||||
clientDisplayName: `Telegram approval (${params.senderId?.trim() || "unknown"})`,
|
||||
onHelloOk: () => {
|
||||
markReady();
|
||||
await withOperatorApprovalsGatewayClient(
|
||||
{
|
||||
config: params.cfg,
|
||||
gatewayUrl: params.gatewayUrl,
|
||||
clientDisplayName: `Telegram approval (${params.senderId?.trim() || "unknown"})`,
|
||||
},
|
||||
onConnectError: (err) => {
|
||||
failReady(err);
|
||||
},
|
||||
onClose: (code, reason) => {
|
||||
// Once onHelloOk resolves `ready`, in-flight request failures must come from
|
||||
// gatewayClient.request() itself; failReady only covers the pre-ready phase.
|
||||
failReady(new Error(`gateway closed (${code}): ${reason}`));
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
gatewayClient.start();
|
||||
await ready;
|
||||
const requestApproval = async (method: "exec.approval.resolve" | "plugin.approval.resolve") => {
|
||||
await gatewayClient.request(method, {
|
||||
id: params.approvalId,
|
||||
decision: params.decision,
|
||||
});
|
||||
};
|
||||
if (params.approvalId.startsWith("plugin:")) {
|
||||
await requestApproval("plugin.approval.resolve");
|
||||
} else {
|
||||
try {
|
||||
await requestApproval("exec.approval.resolve");
|
||||
} catch (err) {
|
||||
if (!params.allowPluginFallback || !isApprovalNotFoundError(err)) {
|
||||
throw err;
|
||||
}
|
||||
async (gatewayClient) => {
|
||||
const requestApproval = async (
|
||||
method: "exec.approval.resolve" | "plugin.approval.resolve",
|
||||
) => {
|
||||
await gatewayClient.request(method, {
|
||||
id: params.approvalId,
|
||||
decision: params.decision,
|
||||
});
|
||||
};
|
||||
if (params.approvalId.startsWith("plugin:")) {
|
||||
await requestApproval("plugin.approval.resolve");
|
||||
} else {
|
||||
try {
|
||||
await requestApproval("exec.approval.resolve");
|
||||
} catch (err) {
|
||||
if (!params.allowPluginFallback || !isApprovalNotFoundError(err)) {
|
||||
throw err;
|
||||
}
|
||||
await requestApproval("plugin.approval.resolve");
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await gatewayClient.stopAndWait().catch(() => {
|
||||
gatewayClient.stop();
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user