refactor: split message tool schema + action handling

This commit is contained in:
Peter Steinberger
2026-01-17 03:58:47 +00:00
parent 97cfa0846c
commit dd68faef23
2 changed files with 491 additions and 351 deletions

View File

@@ -23,12 +23,24 @@ import { jsonResult, readNumberParam, readStringParam } from "./common.js";
const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES; const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES;
const MessageToolCommonSchema = { function buildRoutingSchema() {
return {
channel: Type.Optional(Type.String()), channel: Type.Optional(Type.String()),
to: Type.Optional(channelTargetSchema()), to: Type.Optional(channelTargetSchema()),
targets: Type.Optional(channelTargetsSchema()), targets: Type.Optional(channelTargetsSchema()),
accountId: Type.Optional(Type.String()),
dryRun: Type.Optional(Type.Boolean()),
};
}
function buildSendSchema(options: { includeButtons: boolean }) {
const props: Record<string, unknown> = {
message: Type.Optional(Type.String()), message: Type.Optional(Type.String()),
media: Type.Optional(Type.String()), media: Type.Optional(Type.String()),
replyTo: Type.Optional(Type.String()),
threadId: Type.Optional(Type.String()),
bestEffort: Type.Optional(Type.Boolean()),
gifPlayback: Type.Optional(Type.Boolean()),
buttons: Type.Optional( buttons: Type.Optional(
Type.Array( Type.Array(
Type.Array( Type.Array(
@@ -42,23 +54,41 @@ const MessageToolCommonSchema = {
}, },
), ),
), ),
};
if (!options.includeButtons) delete props.buttons;
return props;
}
function buildReactionSchema() {
return {
messageId: Type.Optional(Type.String()), messageId: Type.Optional(Type.String()),
replyTo: Type.Optional(Type.String()),
threadId: Type.Optional(Type.String()),
accountId: Type.Optional(Type.String()),
dryRun: Type.Optional(Type.Boolean()),
bestEffort: Type.Optional(Type.Boolean()),
gifPlayback: Type.Optional(Type.Boolean()),
emoji: Type.Optional(Type.String()), emoji: Type.Optional(Type.String()),
remove: Type.Optional(Type.Boolean()), remove: Type.Optional(Type.Boolean()),
};
}
function buildFetchSchema() {
return {
limit: Type.Optional(Type.Number()), limit: Type.Optional(Type.Number()),
before: Type.Optional(Type.String()), before: Type.Optional(Type.String()),
after: Type.Optional(Type.String()), after: Type.Optional(Type.String()),
around: Type.Optional(Type.String()), around: Type.Optional(Type.String()),
fromMe: Type.Optional(Type.Boolean()),
includeArchived: Type.Optional(Type.Boolean()),
};
}
function buildPollSchema() {
return {
pollQuestion: Type.Optional(Type.String()), pollQuestion: Type.Optional(Type.String()),
pollOption: Type.Optional(Type.Array(Type.String())), pollOption: Type.Optional(Type.Array(Type.String())),
pollDurationHours: Type.Optional(Type.Number()), pollDurationHours: Type.Optional(Type.Number()),
pollMulti: Type.Optional(Type.Boolean()), pollMulti: Type.Optional(Type.Boolean()),
};
}
function buildChannelTargetSchema() {
return {
channelId: Type.Optional(channelTargetSchema()), channelId: Type.Optional(channelTargetSchema()),
channelIds: Type.Optional(channelTargetsSchema()), channelIds: Type.Optional(channelTargetsSchema()),
guildId: Type.Optional(Type.String()), guildId: Type.Optional(Type.String()),
@@ -67,13 +97,29 @@ const MessageToolCommonSchema = {
authorIds: Type.Optional(Type.Array(Type.String())), authorIds: Type.Optional(Type.Array(Type.String())),
roleId: Type.Optional(Type.String()), roleId: Type.Optional(Type.String()),
roleIds: Type.Optional(Type.Array(Type.String())), roleIds: Type.Optional(Type.Array(Type.String())),
participant: Type.Optional(Type.String()),
};
}
function buildStickerSchema() {
return {
emojiName: Type.Optional(Type.String()), emojiName: Type.Optional(Type.String()),
stickerId: Type.Optional(Type.Array(Type.String())), stickerId: Type.Optional(Type.Array(Type.String())),
stickerName: Type.Optional(Type.String()), stickerName: Type.Optional(Type.String()),
stickerDesc: Type.Optional(Type.String()), stickerDesc: Type.Optional(Type.String()),
stickerTags: Type.Optional(Type.String()), stickerTags: Type.Optional(Type.String()),
};
}
function buildThreadSchema() {
return {
threadName: Type.Optional(Type.String()), threadName: Type.Optional(Type.String()),
autoArchiveMin: Type.Optional(Type.Number()), autoArchiveMin: Type.Optional(Type.Number()),
};
}
function buildEventSchema() {
return {
query: Type.Optional(Type.String()), query: Type.Optional(Type.String()),
eventName: Type.Optional(Type.String()), eventName: Type.Optional(Type.String()),
eventType: Type.Optional(Type.String()), eventType: Type.Optional(Type.String()),
@@ -83,14 +129,26 @@ const MessageToolCommonSchema = {
location: Type.Optional(Type.String()), location: Type.Optional(Type.String()),
durationMin: Type.Optional(Type.Number()), durationMin: Type.Optional(Type.Number()),
until: Type.Optional(Type.String()), until: Type.Optional(Type.String()),
};
}
function buildModerationSchema() {
return {
reason: Type.Optional(Type.String()), reason: Type.Optional(Type.String()),
deleteDays: Type.Optional(Type.Number()), deleteDays: Type.Optional(Type.Number()),
includeArchived: Type.Optional(Type.Boolean()), };
participant: Type.Optional(Type.String()), }
fromMe: Type.Optional(Type.Boolean()),
function buildGatewaySchema() {
return {
gatewayUrl: Type.Optional(Type.String()), gatewayUrl: Type.Optional(Type.String()),
gatewayToken: Type.Optional(Type.String()), gatewayToken: Type.Optional(Type.String()),
timeoutMs: Type.Optional(Type.Number()), timeoutMs: Type.Optional(Type.Number()),
};
}
function buildChannelManagementSchema() {
return {
name: Type.Optional(Type.String()), name: Type.Optional(Type.String()),
type: Type.Optional(Type.Number()), type: Type.Optional(Type.Number()),
parentId: Type.Optional(Type.String()), parentId: Type.Optional(Type.String()),
@@ -105,14 +163,30 @@ const MessageToolCommonSchema = {
}), }),
), ),
}; };
}
function buildMessageToolSchemaProps(options: { includeButtons: boolean }) {
return {
...buildRoutingSchema(),
...buildSendSchema(options),
...buildReactionSchema(),
...buildFetchSchema(),
...buildPollSchema(),
...buildChannelTargetSchema(),
...buildStickerSchema(),
...buildThreadSchema(),
...buildEventSchema(),
...buildModerationSchema(),
...buildGatewaySchema(),
...buildChannelManagementSchema(),
};
}
function buildMessageToolSchemaFromActions( function buildMessageToolSchemaFromActions(
actions: readonly string[], actions: readonly string[],
options: { includeButtons: boolean }, options: { includeButtons: boolean },
) { ) {
const props: Record<string, unknown> = { ...MessageToolCommonSchema }; const props = buildMessageToolSchemaProps(options);
if (!options.includeButtons) delete props.buttons;
return Type.Object({ return Type.Object({
action: stringEnum(actions), action: stringEnum(actions),
...props, ...props,

View File

@@ -150,6 +150,7 @@ function applyCrossContextMessageDecoration({
} }
return applied.message; return applied.message;
} }
function readBooleanParam(params: Record<string, unknown>, key: string): boolean | undefined { function readBooleanParam(params: Record<string, unknown>, key: string): boolean | undefined {
const raw = params[key]; const raw = params[key];
if (typeof raw === "boolean") return raw; if (typeof raw === "boolean") return raw;
@@ -227,16 +228,33 @@ async function resolveActionTarget(params: {
} }
} }
export async function runMessageAction( type ResolvedActionContext = {
input: RunMessageActionParams, cfg: ClawdbotConfig;
): Promise<MessageActionRunResult> { params: Record<string, unknown>;
const cfg = input.cfg; channel: ChannelId;
const params = { ...input.params }; accountId?: string | null;
parseButtonsParam(params); dryRun: boolean;
gateway?: MessageActionRunnerGateway;
input: RunMessageActionParams;
};
const action = input.action; function resolveGateway(input: RunMessageActionParams): MessageActionRunnerGateway | undefined {
if (action === "broadcast") { if (!input.gateway) return undefined;
const broadcastEnabled = cfg.tools?.message?.broadcast?.enabled !== false; return {
url: input.gateway.url,
token: input.gateway.token,
timeoutMs: input.gateway.timeoutMs,
clientName: input.gateway.clientName,
clientDisplayName: input.gateway.clientDisplayName,
mode: input.gateway.mode,
};
}
async function handleBroadcastAction(
input: RunMessageActionParams,
params: Record<string, unknown>,
): Promise<MessageActionRunResult> {
const broadcastEnabled = input.cfg.tools?.message?.broadcast?.enabled !== false;
if (!broadcastEnabled) { if (!broadcastEnabled) {
throw new Error("Broadcast is disabled. Set tools.message.broadcast.enabled to true."); throw new Error("Broadcast is disabled. Set tools.message.broadcast.enabled to true.");
} }
@@ -245,13 +263,13 @@ export async function runMessageAction(
throw new Error("Broadcast requires at least one target in --targets."); throw new Error("Broadcast requires at least one target in --targets.");
} }
const channelHint = readStringParam(params, "channel"); const channelHint = readStringParam(params, "channel");
const configured = await listConfiguredMessageChannels(cfg); const configured = await listConfiguredMessageChannels(input.cfg);
if (configured.length === 0) { if (configured.length === 0) {
throw new Error("Broadcast requires at least one configured channel."); throw new Error("Broadcast requires at least one configured channel.");
} }
const targetChannels = const targetChannels =
channelHint && channelHint.trim().toLowerCase() !== "all" channelHint && channelHint.trim().toLowerCase() !== "all"
? [await resolveChannel(cfg, { channel: channelHint })] ? [await resolveChannel(input.cfg, { channel: channelHint })]
: configured; : configured;
const results: Array<{ const results: Array<{
channel: ChannelId; channel: ChannelId;
@@ -264,7 +282,7 @@ export async function runMessageAction(
for (const target of rawTargets) { for (const target of rawTargets) {
try { try {
const resolved = await resolveMessagingTarget({ const resolved = await resolveMessagingTarget({
cfg, cfg: input.cfg,
channel: targetChannel, channel: targetChannel,
input: target, input: target,
}); });
@@ -304,38 +322,9 @@ export async function runMessageAction(
}; };
} }
const channel = await resolveChannel(cfg, params); async function handleSendAction(ctx: ResolvedActionContext): Promise<MessageActionRunResult> {
const accountId = readStringParam(params, "accountId") ?? input.defaultAccountId; const { cfg, params, channel, accountId, dryRun, gateway, input } = ctx;
const dryRun = Boolean(input.dryRun ?? readBooleanParam(params, "dryRun")); const action: ChannelMessageActionName = "send";
await resolveActionTarget({
cfg,
channel,
action,
args: params,
accountId,
});
enforceCrossContextPolicy({
channel,
action,
args: params,
toolContext: input.toolContext,
cfg,
});
const gateway = input.gateway
? {
url: input.gateway.url,
token: input.gateway.token,
timeoutMs: input.gateway.timeoutMs,
clientName: input.gateway.clientName,
clientDisplayName: input.gateway.clientDisplayName,
mode: input.gateway.mode,
}
: undefined;
if (action === "send") {
const to = readStringParam(params, "to", { required: true }); const to = readStringParam(params, "to", { required: true });
// Allow message to be omitted when sending media-only (e.g., voice notes) // Allow message to be omitted when sending media-only (e.g., voice notes)
const mediaHint = readStringParam(params, "media", { trim: false }); const mediaHint = readStringParam(params, "media", { trim: false });
@@ -433,7 +422,9 @@ export async function runMessageAction(
}; };
} }
if (action === "poll") { async function handlePollAction(ctx: ResolvedActionContext): Promise<MessageActionRunResult> {
const { cfg, params, channel, accountId, dryRun, gateway, input } = ctx;
const action: ChannelMessageActionName = "poll";
const to = readStringParam(params, "to", { required: true }); const to = readStringParam(params, "to", { required: true });
const question = readStringParam(params, "pollQuestion", { const question = readStringParam(params, "pollQuestion", {
required: true, required: true,
@@ -516,6 +507,12 @@ export async function runMessageAction(
}; };
} }
async function handlePluginAction(ctx: ResolvedActionContext): Promise<MessageActionRunResult> {
const { cfg, params, channel, accountId, dryRun, gateway, input } = ctx;
const action = input.action as Exclude<
ChannelMessageActionName,
"send" | "poll" | "broadcast"
>;
if (dryRun) { if (dryRun) {
return { return {
kind: "action", kind: "action",
@@ -550,3 +547,72 @@ export async function runMessageAction(
dryRun, dryRun,
}; };
} }
export async function runMessageAction(
input: RunMessageActionParams,
): Promise<MessageActionRunResult> {
const cfg = input.cfg;
const params = { ...input.params };
parseButtonsParam(params);
const action = input.action;
if (action === "broadcast") {
return handleBroadcastAction(input, params);
}
const channel = await resolveChannel(cfg, params);
const accountId = readStringParam(params, "accountId") ?? input.defaultAccountId;
const dryRun = Boolean(input.dryRun ?? readBooleanParam(params, "dryRun"));
await resolveActionTarget({
cfg,
channel,
action,
args: params,
accountId,
});
enforceCrossContextPolicy({
channel,
action,
args: params,
toolContext: input.toolContext,
cfg,
});
const gateway = resolveGateway(input);
if (action === "send") {
return handleSendAction({
cfg,
params,
channel,
accountId,
dryRun,
gateway,
input,
});
}
if (action === "poll") {
return handlePollAction({
cfg,
params,
channel,
accountId,
dryRun,
gateway,
input,
});
}
return handlePluginAction({
cfg,
params,
channel,
accountId,
dryRun,
gateway,
input,
});
}