mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 21:44:32 +00:00
refactor(gateway): reuse shared validators + baseHash
This commit is contained in:
8
src/gateway/server-methods/base-hash.ts
Normal file
8
src/gateway/server-methods/base-hash.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
export function resolveBaseHashParam(params: unknown): string | null {
|
||||||
|
const raw = (params as { baseHash?: unknown })?.baseHash;
|
||||||
|
if (typeof raw !== "string") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
return trimmed ? trimmed : null;
|
||||||
|
}
|
||||||
@@ -31,22 +31,14 @@ import { loadOpenClawPlugins } from "../../plugins/loader.js";
|
|||||||
import {
|
import {
|
||||||
ErrorCodes,
|
ErrorCodes,
|
||||||
errorShape,
|
errorShape,
|
||||||
formatValidationErrors,
|
|
||||||
validateConfigApplyParams,
|
validateConfigApplyParams,
|
||||||
validateConfigGetParams,
|
validateConfigGetParams,
|
||||||
validateConfigPatchParams,
|
validateConfigPatchParams,
|
||||||
validateConfigSchemaParams,
|
validateConfigSchemaParams,
|
||||||
validateConfigSetParams,
|
validateConfigSetParams,
|
||||||
} from "../protocol/index.js";
|
} from "../protocol/index.js";
|
||||||
|
import { resolveBaseHashParam } from "./base-hash.js";
|
||||||
function resolveBaseHash(params: unknown): string | null {
|
import { assertValidParams } from "./validation.js";
|
||||||
const raw = (params as { baseHash?: unknown })?.baseHash;
|
|
||||||
if (typeof raw !== "string") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const trimmed = raw.trim();
|
|
||||||
return trimmed ? trimmed : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function requireConfigBaseHash(
|
function requireConfigBaseHash(
|
||||||
params: unknown,
|
params: unknown,
|
||||||
@@ -68,7 +60,7 @@ function requireConfigBaseHash(
|
|||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const baseHash = resolveBaseHash(params);
|
const baseHash = resolveBaseHashParam(params);
|
||||||
if (!baseHash) {
|
if (!baseHash) {
|
||||||
respond(
|
respond(
|
||||||
false,
|
false,
|
||||||
@@ -258,15 +250,7 @@ function loadSchemaWithPlugins(): ConfigSchemaResponse {
|
|||||||
|
|
||||||
export const configHandlers: GatewayRequestHandlers = {
|
export const configHandlers: GatewayRequestHandlers = {
|
||||||
"config.get": async ({ params, respond }) => {
|
"config.get": async ({ params, respond }) => {
|
||||||
if (!validateConfigGetParams(params)) {
|
if (!assertValidParams(params, validateConfigGetParams, "config.get", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid config.get params: ${formatValidationErrors(validateConfigGetParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const snapshot = await readConfigFileSnapshot();
|
const snapshot = await readConfigFileSnapshot();
|
||||||
@@ -274,29 +258,13 @@ export const configHandlers: GatewayRequestHandlers = {
|
|||||||
respond(true, redactConfigSnapshot(snapshot, schema.uiHints), undefined);
|
respond(true, redactConfigSnapshot(snapshot, schema.uiHints), undefined);
|
||||||
},
|
},
|
||||||
"config.schema": ({ params, respond }) => {
|
"config.schema": ({ params, respond }) => {
|
||||||
if (!validateConfigSchemaParams(params)) {
|
if (!assertValidParams(params, validateConfigSchemaParams, "config.schema", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid config.schema params: ${formatValidationErrors(validateConfigSchemaParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
respond(true, loadSchemaWithPlugins(), undefined);
|
respond(true, loadSchemaWithPlugins(), undefined);
|
||||||
},
|
},
|
||||||
"config.set": async ({ params, respond }) => {
|
"config.set": async ({ params, respond }) => {
|
||||||
if (!validateConfigSetParams(params)) {
|
if (!assertValidParams(params, validateConfigSetParams, "config.set", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid config.set params: ${formatValidationErrors(validateConfigSetParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||||
@@ -319,15 +287,7 @@ export const configHandlers: GatewayRequestHandlers = {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
"config.patch": async ({ params, respond }) => {
|
"config.patch": async ({ params, respond }) => {
|
||||||
if (!validateConfigPatchParams(params)) {
|
if (!assertValidParams(params, validateConfigPatchParams, "config.patch", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid config.patch params: ${formatValidationErrors(validateConfigPatchParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||||
@@ -433,15 +393,7 @@ export const configHandlers: GatewayRequestHandlers = {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
"config.apply": async ({ params, respond }) => {
|
"config.apply": async ({ params, respond }) => {
|
||||||
if (!validateConfigApplyParams(params)) {
|
if (!assertValidParams(params, validateConfigApplyParams, "config.apply", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid config.apply params: ${formatValidationErrors(validateConfigApplyParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||||
|
|||||||
@@ -11,22 +11,14 @@ import {
|
|||||||
import {
|
import {
|
||||||
ErrorCodes,
|
ErrorCodes,
|
||||||
errorShape,
|
errorShape,
|
||||||
formatValidationErrors,
|
|
||||||
validateExecApprovalsGetParams,
|
validateExecApprovalsGetParams,
|
||||||
validateExecApprovalsNodeGetParams,
|
validateExecApprovalsNodeGetParams,
|
||||||
validateExecApprovalsNodeSetParams,
|
validateExecApprovalsNodeSetParams,
|
||||||
validateExecApprovalsSetParams,
|
validateExecApprovalsSetParams,
|
||||||
} from "../protocol/index.js";
|
} from "../protocol/index.js";
|
||||||
|
import { resolveBaseHashParam } from "./base-hash.js";
|
||||||
import { respondUnavailableOnThrow, safeParseJson } from "./nodes.helpers.js";
|
import { respondUnavailableOnThrow, safeParseJson } from "./nodes.helpers.js";
|
||||||
|
import { assertValidParams } from "./validation.js";
|
||||||
function resolveBaseHash(params: unknown): string | null {
|
|
||||||
const raw = (params as { baseHash?: unknown })?.baseHash;
|
|
||||||
if (typeof raw !== "string") {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const trimmed = raw.trim();
|
|
||||||
return trimmed ? trimmed : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function requireApprovalsBaseHash(
|
function requireApprovalsBaseHash(
|
||||||
params: unknown,
|
params: unknown,
|
||||||
@@ -47,7 +39,7 @@ function requireApprovalsBaseHash(
|
|||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const baseHash = resolveBaseHash(params);
|
const baseHash = resolveBaseHashParam(params);
|
||||||
if (!baseHash) {
|
if (!baseHash) {
|
||||||
respond(
|
respond(
|
||||||
false,
|
false,
|
||||||
@@ -83,15 +75,7 @@ function redactExecApprovals(file: ExecApprovalsFile): ExecApprovalsFile {
|
|||||||
|
|
||||||
export const execApprovalsHandlers: GatewayRequestHandlers = {
|
export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||||
"exec.approvals.get": ({ params, respond }) => {
|
"exec.approvals.get": ({ params, respond }) => {
|
||||||
if (!validateExecApprovalsGetParams(params)) {
|
if (!assertValidParams(params, validateExecApprovalsGetParams, "exec.approvals.get", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid exec.approvals.get params: ${formatValidationErrors(validateExecApprovalsGetParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ensureExecApprovals();
|
ensureExecApprovals();
|
||||||
@@ -108,15 +92,7 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
"exec.approvals.set": ({ params, respond }) => {
|
"exec.approvals.set": ({ params, respond }) => {
|
||||||
if (!validateExecApprovalsSetParams(params)) {
|
if (!assertValidParams(params, validateExecApprovalsSetParams, "exec.approvals.set", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid exec.approvals.set params: ${formatValidationErrors(validateExecApprovalsSetParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ensureExecApprovals();
|
ensureExecApprovals();
|
||||||
@@ -149,15 +125,14 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
"exec.approvals.node.get": async ({ params, respond, context }) => {
|
"exec.approvals.node.get": async ({ params, respond, context }) => {
|
||||||
if (!validateExecApprovalsNodeGetParams(params)) {
|
if (
|
||||||
respond(
|
!assertValidParams(
|
||||||
false,
|
params,
|
||||||
undefined,
|
validateExecApprovalsNodeGetParams,
|
||||||
errorShape(
|
"exec.approvals.node.get",
|
||||||
ErrorCodes.INVALID_REQUEST,
|
respond,
|
||||||
`invalid exec.approvals.node.get params: ${formatValidationErrors(validateExecApprovalsNodeGetParams.errors)}`,
|
)
|
||||||
),
|
) {
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { nodeId } = params as { nodeId: string };
|
const { nodeId } = params as { nodeId: string };
|
||||||
@@ -187,15 +162,14 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
"exec.approvals.node.set": async ({ params, respond, context }) => {
|
"exec.approvals.node.set": async ({ params, respond, context }) => {
|
||||||
if (!validateExecApprovalsNodeSetParams(params)) {
|
if (
|
||||||
respond(
|
!assertValidParams(
|
||||||
false,
|
params,
|
||||||
undefined,
|
validateExecApprovalsNodeSetParams,
|
||||||
errorShape(
|
"exec.approvals.node.set",
|
||||||
ErrorCodes.INVALID_REQUEST,
|
respond,
|
||||||
`invalid exec.approvals.node.set params: ${formatValidationErrors(validateExecApprovalsNodeSetParams.errors)}`,
|
)
|
||||||
),
|
) {
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { nodeId, file, baseHash } = params as {
|
const { nodeId, file, baseHash } = params as {
|
||||||
|
|||||||
@@ -9,24 +9,12 @@ import {
|
|||||||
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
|
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
|
||||||
import { normalizeUpdateChannel } from "../../infra/update-channels.js";
|
import { normalizeUpdateChannel } from "../../infra/update-channels.js";
|
||||||
import { runGatewayUpdate } from "../../infra/update-runner.js";
|
import { runGatewayUpdate } from "../../infra/update-runner.js";
|
||||||
import {
|
import { validateUpdateRunParams } from "../protocol/index.js";
|
||||||
ErrorCodes,
|
import { assertValidParams } from "./validation.js";
|
||||||
errorShape,
|
|
||||||
formatValidationErrors,
|
|
||||||
validateUpdateRunParams,
|
|
||||||
} from "../protocol/index.js";
|
|
||||||
|
|
||||||
export const updateHandlers: GatewayRequestHandlers = {
|
export const updateHandlers: GatewayRequestHandlers = {
|
||||||
"update.run": async ({ params, respond }) => {
|
"update.run": async ({ params, respond }) => {
|
||||||
if (!validateUpdateRunParams(params)) {
|
if (!assertValidParams(params, validateUpdateRunParams, "update.run", respond)) {
|
||||||
respond(
|
|
||||||
false,
|
|
||||||
undefined,
|
|
||||||
errorShape(
|
|
||||||
ErrorCodes.INVALID_REQUEST,
|
|
||||||
`invalid update.run params: ${formatValidationErrors(validateUpdateRunParams.errors)}`,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const sessionKey =
|
const sessionKey =
|
||||||
|
|||||||
Reference in New Issue
Block a user