mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-24 07:01:49 +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 {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
formatValidationErrors,
|
||||
validateConfigApplyParams,
|
||||
validateConfigGetParams,
|
||||
validateConfigPatchParams,
|
||||
validateConfigSchemaParams,
|
||||
validateConfigSetParams,
|
||||
} from "../protocol/index.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;
|
||||
}
|
||||
import { resolveBaseHashParam } from "./base-hash.js";
|
||||
import { assertValidParams } from "./validation.js";
|
||||
|
||||
function requireConfigBaseHash(
|
||||
params: unknown,
|
||||
@@ -68,7 +60,7 @@ function requireConfigBaseHash(
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const baseHash = resolveBaseHash(params);
|
||||
const baseHash = resolveBaseHashParam(params);
|
||||
if (!baseHash) {
|
||||
respond(
|
||||
false,
|
||||
@@ -258,15 +250,7 @@ function loadSchemaWithPlugins(): ConfigSchemaResponse {
|
||||
|
||||
export const configHandlers: GatewayRequestHandlers = {
|
||||
"config.get": async ({ params, respond }) => {
|
||||
if (!validateConfigGetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.get params: ${formatValidationErrors(validateConfigGetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigGetParams, "config.get", respond)) {
|
||||
return;
|
||||
}
|
||||
const snapshot = await readConfigFileSnapshot();
|
||||
@@ -274,29 +258,13 @@ export const configHandlers: GatewayRequestHandlers = {
|
||||
respond(true, redactConfigSnapshot(snapshot, schema.uiHints), undefined);
|
||||
},
|
||||
"config.schema": ({ params, respond }) => {
|
||||
if (!validateConfigSchemaParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.schema params: ${formatValidationErrors(validateConfigSchemaParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigSchemaParams, "config.schema", respond)) {
|
||||
return;
|
||||
}
|
||||
respond(true, loadSchemaWithPlugins(), undefined);
|
||||
},
|
||||
"config.set": async ({ params, respond }) => {
|
||||
if (!validateConfigSetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.set params: ${formatValidationErrors(validateConfigSetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigSetParams, "config.set", respond)) {
|
||||
return;
|
||||
}
|
||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||
@@ -319,15 +287,7 @@ export const configHandlers: GatewayRequestHandlers = {
|
||||
);
|
||||
},
|
||||
"config.patch": async ({ params, respond }) => {
|
||||
if (!validateConfigPatchParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.patch params: ${formatValidationErrors(validateConfigPatchParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigPatchParams, "config.patch", respond)) {
|
||||
return;
|
||||
}
|
||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||
@@ -433,15 +393,7 @@ export const configHandlers: GatewayRequestHandlers = {
|
||||
);
|
||||
},
|
||||
"config.apply": async ({ params, respond }) => {
|
||||
if (!validateConfigApplyParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid config.apply params: ${formatValidationErrors(validateConfigApplyParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateConfigApplyParams, "config.apply", respond)) {
|
||||
return;
|
||||
}
|
||||
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
|
||||
|
||||
@@ -11,22 +11,14 @@ import {
|
||||
import {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
formatValidationErrors,
|
||||
validateExecApprovalsGetParams,
|
||||
validateExecApprovalsNodeGetParams,
|
||||
validateExecApprovalsNodeSetParams,
|
||||
validateExecApprovalsSetParams,
|
||||
} from "../protocol/index.js";
|
||||
import { resolveBaseHashParam } from "./base-hash.js";
|
||||
import { respondUnavailableOnThrow, safeParseJson } from "./nodes.helpers.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;
|
||||
}
|
||||
import { assertValidParams } from "./validation.js";
|
||||
|
||||
function requireApprovalsBaseHash(
|
||||
params: unknown,
|
||||
@@ -47,7 +39,7 @@ function requireApprovalsBaseHash(
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const baseHash = resolveBaseHash(params);
|
||||
const baseHash = resolveBaseHashParam(params);
|
||||
if (!baseHash) {
|
||||
respond(
|
||||
false,
|
||||
@@ -83,15 +75,7 @@ function redactExecApprovals(file: ExecApprovalsFile): ExecApprovalsFile {
|
||||
|
||||
export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
"exec.approvals.get": ({ params, respond }) => {
|
||||
if (!validateExecApprovalsGetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid exec.approvals.get params: ${formatValidationErrors(validateExecApprovalsGetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateExecApprovalsGetParams, "exec.approvals.get", respond)) {
|
||||
return;
|
||||
}
|
||||
ensureExecApprovals();
|
||||
@@ -108,15 +92,7 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
);
|
||||
},
|
||||
"exec.approvals.set": ({ params, respond }) => {
|
||||
if (!validateExecApprovalsSetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid exec.approvals.set params: ${formatValidationErrors(validateExecApprovalsSetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateExecApprovalsSetParams, "exec.approvals.set", respond)) {
|
||||
return;
|
||||
}
|
||||
ensureExecApprovals();
|
||||
@@ -149,15 +125,14 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
);
|
||||
},
|
||||
"exec.approvals.node.get": async ({ params, respond, context }) => {
|
||||
if (!validateExecApprovalsNodeGetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid exec.approvals.node.get params: ${formatValidationErrors(validateExecApprovalsNodeGetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (
|
||||
!assertValidParams(
|
||||
params,
|
||||
validateExecApprovalsNodeGetParams,
|
||||
"exec.approvals.node.get",
|
||||
respond,
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const { nodeId } = params as { nodeId: string };
|
||||
@@ -187,15 +162,14 @@ export const execApprovalsHandlers: GatewayRequestHandlers = {
|
||||
});
|
||||
},
|
||||
"exec.approvals.node.set": async ({ params, respond, context }) => {
|
||||
if (!validateExecApprovalsNodeSetParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid exec.approvals.node.set params: ${formatValidationErrors(validateExecApprovalsNodeSetParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (
|
||||
!assertValidParams(
|
||||
params,
|
||||
validateExecApprovalsNodeSetParams,
|
||||
"exec.approvals.node.set",
|
||||
respond,
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const { nodeId, file, baseHash } = params as {
|
||||
|
||||
@@ -9,24 +9,12 @@ import {
|
||||
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
|
||||
import { normalizeUpdateChannel } from "../../infra/update-channels.js";
|
||||
import { runGatewayUpdate } from "../../infra/update-runner.js";
|
||||
import {
|
||||
ErrorCodes,
|
||||
errorShape,
|
||||
formatValidationErrors,
|
||||
validateUpdateRunParams,
|
||||
} from "../protocol/index.js";
|
||||
import { validateUpdateRunParams } from "../protocol/index.js";
|
||||
import { assertValidParams } from "./validation.js";
|
||||
|
||||
export const updateHandlers: GatewayRequestHandlers = {
|
||||
"update.run": async ({ params, respond }) => {
|
||||
if (!validateUpdateRunParams(params)) {
|
||||
respond(
|
||||
false,
|
||||
undefined,
|
||||
errorShape(
|
||||
ErrorCodes.INVALID_REQUEST,
|
||||
`invalid update.run params: ${formatValidationErrors(validateUpdateRunParams.errors)}`,
|
||||
),
|
||||
);
|
||||
if (!assertValidParams(params, validateUpdateRunParams, "update.run", respond)) {
|
||||
return;
|
||||
}
|
||||
const sessionKey =
|
||||
|
||||
Reference in New Issue
Block a user