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,96 +23,170 @@ import { jsonResult, readNumberParam, readStringParam } from "./common.js";
const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES; const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES;
const MessageToolCommonSchema = { function buildRoutingSchema() {
channel: Type.Optional(Type.String()), return {
to: Type.Optional(channelTargetSchema()), channel: Type.Optional(Type.String()),
targets: Type.Optional(channelTargetsSchema()), to: Type.Optional(channelTargetSchema()),
message: Type.Optional(Type.String()), targets: Type.Optional(channelTargetsSchema()),
media: Type.Optional(Type.String()), accountId: Type.Optional(Type.String()),
buttons: Type.Optional( dryRun: Type.Optional(Type.Boolean()),
Type.Array( };
}
function buildSendSchema(options: { includeButtons: boolean }) {
const props: Record<string, unknown> = {
message: 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(
Type.Array( Type.Array(
Type.Object({ Type.Array(
text: Type.String(), Type.Object({
callback_data: Type.String(), text: Type.String(),
}), callback_data: Type.String(),
}),
),
{
description: "Telegram inline keyboard buttons (array of button rows)",
},
), ),
{
description: "Telegram inline keyboard buttons (array of button rows)",
},
), ),
), };
messageId: Type.Optional(Type.String()), if (!options.includeButtons) delete props.buttons;
replyTo: Type.Optional(Type.String()), return props;
threadId: Type.Optional(Type.String()), }
accountId: Type.Optional(Type.String()),
dryRun: Type.Optional(Type.Boolean()), function buildReactionSchema() {
bestEffort: Type.Optional(Type.Boolean()), return {
gifPlayback: Type.Optional(Type.Boolean()), messageId: Type.Optional(Type.String()),
emoji: Type.Optional(Type.String()), emoji: Type.Optional(Type.String()),
remove: Type.Optional(Type.Boolean()), remove: Type.Optional(Type.Boolean()),
limit: Type.Optional(Type.Number()), };
before: Type.Optional(Type.String()), }
after: Type.Optional(Type.String()),
around: Type.Optional(Type.String()), function buildFetchSchema() {
pollQuestion: Type.Optional(Type.String()), return {
pollOption: Type.Optional(Type.Array(Type.String())), limit: Type.Optional(Type.Number()),
pollDurationHours: Type.Optional(Type.Number()), before: Type.Optional(Type.String()),
pollMulti: Type.Optional(Type.Boolean()), after: Type.Optional(Type.String()),
channelId: Type.Optional(channelTargetSchema()), around: Type.Optional(Type.String()),
channelIds: Type.Optional(channelTargetsSchema()), fromMe: Type.Optional(Type.Boolean()),
guildId: Type.Optional(Type.String()), includeArchived: Type.Optional(Type.Boolean()),
userId: Type.Optional(Type.String()), };
authorId: Type.Optional(Type.String()), }
authorIds: Type.Optional(Type.Array(Type.String())),
roleId: Type.Optional(Type.String()), function buildPollSchema() {
roleIds: Type.Optional(Type.Array(Type.String())), return {
emojiName: Type.Optional(Type.String()), pollQuestion: Type.Optional(Type.String()),
stickerId: Type.Optional(Type.Array(Type.String())), pollOption: Type.Optional(Type.Array(Type.String())),
stickerName: Type.Optional(Type.String()), pollDurationHours: Type.Optional(Type.Number()),
stickerDesc: Type.Optional(Type.String()), pollMulti: Type.Optional(Type.Boolean()),
stickerTags: Type.Optional(Type.String()), };
threadName: Type.Optional(Type.String()), }
autoArchiveMin: Type.Optional(Type.Number()),
query: Type.Optional(Type.String()), function buildChannelTargetSchema() {
eventName: Type.Optional(Type.String()), return {
eventType: Type.Optional(Type.String()), channelId: Type.Optional(channelTargetSchema()),
startTime: Type.Optional(Type.String()), channelIds: Type.Optional(channelTargetsSchema()),
endTime: Type.Optional(Type.String()), guildId: Type.Optional(Type.String()),
desc: Type.Optional(Type.String()), userId: Type.Optional(Type.String()),
location: Type.Optional(Type.String()), authorId: Type.Optional(Type.String()),
durationMin: Type.Optional(Type.Number()), authorIds: Type.Optional(Type.Array(Type.String())),
until: Type.Optional(Type.String()), roleId: Type.Optional(Type.String()),
reason: Type.Optional(Type.String()), roleIds: Type.Optional(Type.Array(Type.String())),
deleteDays: Type.Optional(Type.Number()), participant: Type.Optional(Type.String()),
includeArchived: Type.Optional(Type.Boolean()), };
participant: Type.Optional(Type.String()), }
fromMe: Type.Optional(Type.Boolean()),
gatewayUrl: Type.Optional(Type.String()), function buildStickerSchema() {
gatewayToken: Type.Optional(Type.String()), return {
timeoutMs: Type.Optional(Type.Number()), emojiName: Type.Optional(Type.String()),
name: Type.Optional(Type.String()), stickerId: Type.Optional(Type.Array(Type.String())),
type: Type.Optional(Type.Number()), stickerName: Type.Optional(Type.String()),
parentId: Type.Optional(Type.String()), stickerDesc: Type.Optional(Type.String()),
topic: Type.Optional(Type.String()), stickerTags: Type.Optional(Type.String()),
position: Type.Optional(Type.Number()), };
nsfw: Type.Optional(Type.Boolean()), }
rateLimitPerUser: Type.Optional(Type.Number()),
categoryId: Type.Optional(Type.String()), function buildThreadSchema() {
clearParent: Type.Optional( return {
Type.Boolean({ threadName: Type.Optional(Type.String()),
description: "Clear the parent/category when supported by the provider.", autoArchiveMin: Type.Optional(Type.Number()),
}), };
), }
};
function buildEventSchema() {
return {
query: Type.Optional(Type.String()),
eventName: Type.Optional(Type.String()),
eventType: Type.Optional(Type.String()),
startTime: Type.Optional(Type.String()),
endTime: Type.Optional(Type.String()),
desc: Type.Optional(Type.String()),
location: Type.Optional(Type.String()),
durationMin: Type.Optional(Type.Number()),
until: Type.Optional(Type.String()),
};
}
function buildModerationSchema() {
return {
reason: Type.Optional(Type.String()),
deleteDays: Type.Optional(Type.Number()),
};
}
function buildGatewaySchema() {
return {
gatewayUrl: Type.Optional(Type.String()),
gatewayToken: Type.Optional(Type.String()),
timeoutMs: Type.Optional(Type.Number()),
};
}
function buildChannelManagementSchema() {
return {
name: Type.Optional(Type.String()),
type: Type.Optional(Type.Number()),
parentId: Type.Optional(Type.String()),
topic: Type.Optional(Type.String()),
position: Type.Optional(Type.Number()),
nsfw: Type.Optional(Type.Boolean()),
rateLimitPerUser: Type.Optional(Type.Number()),
categoryId: Type.Optional(Type.String()),
clearParent: Type.Optional(
Type.Boolean({
description: "Clear the parent/category when supported by the provider.",
}),
),
};
}
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,295 +228,291 @@ 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 {
if (!broadcastEnabled) { url: input.gateway.url,
throw new Error("Broadcast is disabled. Set tools.message.broadcast.enabled to true."); token: input.gateway.token,
} timeoutMs: input.gateway.timeoutMs,
const rawTargets = readStringArrayParam(params, "targets", { required: true }) ?? []; clientName: input.gateway.clientName,
if (rawTargets.length === 0) { clientDisplayName: input.gateway.clientDisplayName,
throw new Error("Broadcast requires at least one target in --targets."); mode: input.gateway.mode,
} };
const channelHint = readStringParam(params, "channel"); }
const configured = await listConfiguredMessageChannels(cfg);
if (configured.length === 0) { async function handleBroadcastAction(
throw new Error("Broadcast requires at least one configured channel."); input: RunMessageActionParams,
} params: Record<string, unknown>,
const targetChannels = ): Promise<MessageActionRunResult> {
channelHint && channelHint.trim().toLowerCase() !== "all" const broadcastEnabled = input.cfg.tools?.message?.broadcast?.enabled !== false;
? [await resolveChannel(cfg, { channel: channelHint })] if (!broadcastEnabled) {
: configured; throw new Error("Broadcast is disabled. Set tools.message.broadcast.enabled to true.");
const results: Array<{ }
channel: ChannelId; const rawTargets = readStringArrayParam(params, "targets", { required: true }) ?? [];
to: string; if (rawTargets.length === 0) {
ok: boolean; throw new Error("Broadcast requires at least one target in --targets.");
error?: string; }
result?: MessageSendResult; const channelHint = readStringParam(params, "channel");
}> = []; const configured = await listConfiguredMessageChannels(input.cfg);
for (const targetChannel of targetChannels) { if (configured.length === 0) {
for (const target of rawTargets) { throw new Error("Broadcast requires at least one configured channel.");
try { }
const resolved = await resolveMessagingTarget({ const targetChannels =
cfg, channelHint && channelHint.trim().toLowerCase() !== "all"
channel: targetChannel, ? [await resolveChannel(input.cfg, { channel: channelHint })]
input: target, : configured;
}); const results: Array<{
if (!resolved.ok) throw resolved.error; channel: ChannelId;
const sendResult = await runMessageAction({ to: string;
...input, ok: boolean;
action: "send", error?: string;
params: { result?: MessageSendResult;
...params, }> = [];
channel: targetChannel, for (const targetChannel of targetChannels) {
to: resolved.target.to, for (const target of rawTargets) {
}, try {
}); const resolved = await resolveMessagingTarget({
results.push({ cfg: input.cfg,
channel: targetChannel,
input: target,
});
if (!resolved.ok) throw resolved.error;
const sendResult = await runMessageAction({
...input,
action: "send",
params: {
...params,
channel: targetChannel, channel: targetChannel,
to: resolved.target.to, to: resolved.target.to,
ok: true, },
result: sendResult.kind === "send" ? sendResult.sendResult : undefined, });
}); results.push({
} catch (err) { channel: targetChannel,
results.push({ to: resolved.target.to,
channel: targetChannel, ok: true,
to: target, result: sendResult.kind === "send" ? sendResult.sendResult : undefined,
ok: false, });
error: err instanceof Error ? err.message : String(err), } catch (err) {
}); results.push({
} channel: targetChannel,
to: target,
ok: false,
error: err instanceof Error ? err.message : String(err),
});
} }
} }
return { }
kind: "broadcast", return {
channel: (targetChannels[0] ?? "discord") as ChannelId, kind: "broadcast",
action: "broadcast", channel: (targetChannels[0] ?? "discord") as ChannelId,
handledBy: input.dryRun ? "dry-run" : "core", action: "broadcast",
payload: { results }, handledBy: input.dryRun ? "dry-run" : "core",
dryRun: Boolean(input.dryRun), payload: { results },
}; dryRun: Boolean(input.dryRun),
};
}
async function handleSendAction(ctx: ResolvedActionContext): Promise<MessageActionRunResult> {
const { cfg, params, channel, accountId, dryRun, gateway, input } = ctx;
const action: ChannelMessageActionName = "send";
const to = readStringParam(params, "to", { required: true });
// Allow message to be omitted when sending media-only (e.g., voice notes)
const mediaHint = readStringParam(params, "media", { trim: false });
let message =
readStringParam(params, "message", {
required: !mediaHint, // Only require message if no media hint
allowEmpty: true,
}) ?? "";
const parsed = parseReplyDirectives(message);
message = parsed.text;
params.message = message;
if (!params.replyTo && parsed.replyToId) params.replyTo = parsed.replyToId;
if (!params.media) {
params.media = parsed.mediaUrls?.[0] || parsed.mediaUrl || undefined;
} }
const channel = await resolveChannel(cfg, params); const decoration =
const accountId = readStringParam(params, "accountId") ?? input.defaultAccountId; shouldApplyCrossContextMarker(action) && input.toolContext
const dryRun = Boolean(input.dryRun ?? readBooleanParam(params, "dryRun")); ? await buildCrossContextDecoration({
cfg,
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 });
// Allow message to be omitted when sending media-only (e.g., voice notes)
const mediaHint = readStringParam(params, "media", { trim: false });
let message =
readStringParam(params, "message", {
required: !mediaHint, // Only require message if no media hint
allowEmpty: true,
}) ?? "";
const parsed = parseReplyDirectives(message);
message = parsed.text;
params.message = message;
if (!params.replyTo && parsed.replyToId) params.replyTo = parsed.replyToId;
if (!params.media) {
params.media = parsed.mediaUrls?.[0] || parsed.mediaUrl || undefined;
}
const decoration =
shouldApplyCrossContextMarker(action) && input.toolContext
? await buildCrossContextDecoration({
cfg,
channel,
target: to,
toolContext: input.toolContext,
accountId: accountId ?? undefined,
})
: null;
if (decoration) {
message = applyCrossContextMessageDecoration({
params,
message,
decoration,
preferEmbeds: true,
});
}
const mediaUrl = readStringParam(params, "media", { trim: false });
const gifPlayback = readBooleanParam(params, "gifPlayback") ?? false;
const bestEffort = readBooleanParam(params, "bestEffort");
if (!dryRun) {
const handled = await dispatchChannelMessageAction({
channel,
action,
cfg,
params,
accountId: accountId ?? undefined,
gateway,
toolContext: input.toolContext,
dryRun,
});
if (handled) {
return {
kind: "send",
channel, channel,
action, target: to,
to, toolContext: input.toolContext,
handledBy: "plugin", accountId: accountId ?? undefined,
payload: extractToolPayload(handled), })
toolResult: handled, : null;
dryRun, if (decoration) {
}; message = applyCrossContextMessageDecoration({
} params,
} message,
decoration,
preferEmbeds: true,
});
}
const result: MessageSendResult = await sendMessage({ const mediaUrl = readStringParam(params, "media", { trim: false });
const gifPlayback = readBooleanParam(params, "gifPlayback") ?? false;
const bestEffort = readBooleanParam(params, "bestEffort");
if (!dryRun) {
const handled = await dispatchChannelMessageAction({
channel,
action,
cfg, cfg,
to, params,
content: message,
mediaUrl: mediaUrl || undefined,
channel: channel || undefined,
accountId: accountId ?? undefined, accountId: accountId ?? undefined,
gifPlayback,
dryRun,
bestEffort: bestEffort ?? undefined,
deps: input.deps,
gateway, gateway,
mirror: toolContext: input.toolContext,
input.sessionKey && !dryRun
? {
sessionKey: input.sessionKey,
agentId: input.agentId,
}
: undefined,
});
return {
kind: "send",
channel,
action,
to,
handledBy: "core",
payload: result,
sendResult: result,
dryRun, dryRun,
};
}
if (action === "poll") {
const to = readStringParam(params, "to", { required: true });
const question = readStringParam(params, "pollQuestion", {
required: true,
}); });
const options = readStringArrayParam(params, "pollOption", { required: true }) ?? []; if (handled) {
if (options.length < 2) { return {
throw new Error("pollOption requires at least two values"); kind: "send",
}
const allowMultiselect = readBooleanParam(params, "pollMulti") ?? false;
const durationHours = readNumberParam(params, "pollDurationHours", {
integer: true,
});
const maxSelections = allowMultiselect ? Math.max(2, options.length) : 1;
const decoration =
shouldApplyCrossContextMarker(action) && input.toolContext
? await buildCrossContextDecoration({
cfg,
channel,
target: to,
toolContext: input.toolContext,
accountId: accountId ?? undefined,
})
: null;
if (decoration) {
const base = typeof params.message === "string" ? params.message : "";
applyCrossContextMessageDecoration({
params,
message: base,
decoration,
preferEmbeds: true,
});
}
if (!dryRun) {
const handled = await dispatchChannelMessageAction({
channel, channel,
action, action,
cfg, to,
params, handledBy: "plugin",
accountId: accountId ?? undefined, payload: extractToolPayload(handled),
gateway, toolResult: handled,
toolContext: input.toolContext,
dryRun, dryRun,
}); };
if (handled) {
return {
kind: "poll",
channel,
action,
to,
handledBy: "plugin",
payload: extractToolPayload(handled),
toolResult: handled,
dryRun,
};
}
} }
const result: MessagePollResult = await sendPoll({
cfg,
to,
question,
options,
maxSelections,
durationHours: durationHours ?? undefined,
channel,
dryRun,
gateway,
});
return {
kind: "poll",
channel,
action,
to,
handledBy: "core",
payload: result,
pollResult: result,
dryRun,
};
} }
const result: MessageSendResult = await sendMessage({
cfg,
to,
content: message,
mediaUrl: mediaUrl || undefined,
channel: channel || undefined,
accountId: accountId ?? undefined,
gifPlayback,
dryRun,
bestEffort: bestEffort ?? undefined,
deps: input.deps,
gateway,
mirror:
input.sessionKey && !dryRun
? {
sessionKey: input.sessionKey,
agentId: input.agentId,
}
: undefined,
});
return {
kind: "send",
channel,
action,
to,
handledBy: "core",
payload: result,
sendResult: result,
dryRun,
};
}
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 question = readStringParam(params, "pollQuestion", {
required: true,
});
const options = readStringArrayParam(params, "pollOption", { required: true }) ?? [];
if (options.length < 2) {
throw new Error("pollOption requires at least two values");
}
const allowMultiselect = readBooleanParam(params, "pollMulti") ?? false;
const durationHours = readNumberParam(params, "pollDurationHours", {
integer: true,
});
const maxSelections = allowMultiselect ? Math.max(2, options.length) : 1;
const decoration =
shouldApplyCrossContextMarker(action) && input.toolContext
? await buildCrossContextDecoration({
cfg,
channel,
target: to,
toolContext: input.toolContext,
accountId: accountId ?? undefined,
})
: null;
if (decoration) {
const base = typeof params.message === "string" ? params.message : "";
applyCrossContextMessageDecoration({
params,
message: base,
decoration,
preferEmbeds: true,
});
}
if (!dryRun) {
const handled = await dispatchChannelMessageAction({
channel,
action,
cfg,
params,
accountId: accountId ?? undefined,
gateway,
toolContext: input.toolContext,
dryRun,
});
if (handled) {
return {
kind: "poll",
channel,
action,
to,
handledBy: "plugin",
payload: extractToolPayload(handled),
toolResult: handled,
dryRun,
};
}
}
const result: MessagePollResult = await sendPoll({
cfg,
to,
question,
options,
maxSelections,
durationHours: durationHours ?? undefined,
channel,
dryRun,
gateway,
});
return {
kind: "poll",
channel,
action,
to,
handledBy: "core",
payload: result,
pollResult: result,
dryRun,
};
}
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,
});
}