mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:11:25 +00:00
refactor(gateway): dedupe wizard and exec approval handler paths
This commit is contained in:
@@ -77,6 +77,15 @@ function redactExecApprovals(file: ExecApprovalsFile): ExecApprovalsFile {
|
||||
};
|
||||
}
|
||||
|
||||
function toExecApprovalsPayload(snapshot: ExecApprovalsSnapshot) {
|
||||
return {
|
||||
path: snapshot.path,
|
||||
exists: snapshot.exists,
|
||||
hash: snapshot.hash,
|
||||
file: redactExecApprovals(snapshot.file),
|
||||
};
|
||||
}
|
||||
|
||||
export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
"exec.approvals.get": ({ params, respond }) => {
|
||||
if (!assertValidParams(params, validateExecApprovalsGetParams, "exec.approvals.get", respond)) {
|
||||
@@ -84,16 +93,7 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
}
|
||||
ensureExecApprovals();
|
||||
const snapshot = readExecApprovalsSnapshot();
|
||||
respond(
|
||||
true,
|
||||
{
|
||||
path: snapshot.path,
|
||||
exists: snapshot.exists,
|
||||
hash: snapshot.hash,
|
||||
file: redactExecApprovals(snapshot.file),
|
||||
},
|
||||
undefined,
|
||||
);
|
||||
respond(true, toExecApprovalsPayload(snapshot), undefined);
|
||||
},
|
||||
"exec.approvals.set": ({ params, respond }) => {
|
||||
if (!assertValidParams(params, validateExecApprovalsSetParams, "exec.approvals.set", respond)) {
|
||||
@@ -117,16 +117,7 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
const next = mergeExecApprovalsSocketDefaults({ normalized, current: snapshot.file });
|
||||
saveExecApprovals(next);
|
||||
const nextSnapshot = readExecApprovalsSnapshot();
|
||||
respond(
|
||||
true,
|
||||
{
|
||||
path: nextSnapshot.path,
|
||||
exists: nextSnapshot.exists,
|
||||
hash: nextSnapshot.hash,
|
||||
file: redactExecApprovals(nextSnapshot.file),
|
||||
},
|
||||
undefined,
|
||||
);
|
||||
respond(true, toExecApprovalsPayload(nextSnapshot), undefined);
|
||||
},
|
||||
"exec.approvals.node.get": async ({ params, respond, context }) => {
|
||||
if (
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { GatewayRequestHandlers } from "./types.js";
|
||||
import type { GatewayRequestContext, GatewayRequestHandlers, RespondFn } from "./types.js";
|
||||
import { defaultRuntime } from "../../runtime.js";
|
||||
import { WizardSession } from "../../wizard/session.js";
|
||||
import {
|
||||
@@ -20,6 +20,19 @@ function readWizardStatus(session: WizardSession) {
|
||||
};
|
||||
}
|
||||
|
||||
function findWizardSessionOrRespond(params: {
|
||||
context: GatewayRequestContext;
|
||||
respond: RespondFn;
|
||||
sessionId: string;
|
||||
}): WizardSession | null {
|
||||
const session = params.context.wizardSessions.get(params.sessionId);
|
||||
if (!session) {
|
||||
params.respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
|
||||
return null;
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
export const wizardHandlers: GatewayRequestHandlers = {
|
||||
"wizard.start": async ({ params, respond, context }) => {
|
||||
if (!assertValidParams(params, validateWizardStartParams, "wizard.start", respond)) {
|
||||
@@ -50,9 +63,8 @@ export const wizardHandlers: GatewayRequestHandlers = {
|
||||
return;
|
||||
}
|
||||
const sessionId = params.sessionId;
|
||||
const session = context.wizardSessions.get(sessionId);
|
||||
const session = findWizardSessionOrRespond({ context, respond, sessionId });
|
||||
if (!session) {
|
||||
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
|
||||
return;
|
||||
}
|
||||
const answer = params.answer as { stepId?: string; value?: unknown } | undefined;
|
||||
@@ -79,9 +91,8 @@ export const wizardHandlers: GatewayRequestHandlers = {
|
||||
return;
|
||||
}
|
||||
const sessionId = params.sessionId;
|
||||
const session = context.wizardSessions.get(sessionId);
|
||||
const session = findWizardSessionOrRespond({ context, respond, sessionId });
|
||||
if (!session) {
|
||||
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
|
||||
return;
|
||||
}
|
||||
session.cancel();
|
||||
@@ -94,9 +105,8 @@ export const wizardHandlers: GatewayRequestHandlers = {
|
||||
return;
|
||||
}
|
||||
const sessionId = params.sessionId;
|
||||
const session = context.wizardSessions.get(sessionId);
|
||||
const session = findWizardSessionOrRespond({ context, respond, sessionId });
|
||||
if (!session) {
|
||||
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
|
||||
return;
|
||||
}
|
||||
const status = readWizardStatus(session);
|
||||
|
||||
Reference in New Issue
Block a user