refactor: split message tool schema + action handling
This commit is contained in:
@@ -23,12 +23,24 @@ import { jsonResult, readNumberParam, readStringParam } from "./common.js";
|
||||
|
||||
const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES;
|
||||
|
||||
const MessageToolCommonSchema = {
|
||||
function buildRoutingSchema() {
|
||||
return {
|
||||
channel: Type.Optional(Type.String()),
|
||||
to: Type.Optional(channelTargetSchema()),
|
||||
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()),
|
||||
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(
|
||||
@@ -42,23 +54,41 @@ const MessageToolCommonSchema = {
|
||||
},
|
||||
),
|
||||
),
|
||||
};
|
||||
if (!options.includeButtons) delete props.buttons;
|
||||
return props;
|
||||
}
|
||||
|
||||
function buildReactionSchema() {
|
||||
return {
|
||||
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()),
|
||||
remove: Type.Optional(Type.Boolean()),
|
||||
};
|
||||
}
|
||||
|
||||
function buildFetchSchema() {
|
||||
return {
|
||||
limit: Type.Optional(Type.Number()),
|
||||
before: Type.Optional(Type.String()),
|
||||
after: 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()),
|
||||
pollOption: Type.Optional(Type.Array(Type.String())),
|
||||
pollDurationHours: Type.Optional(Type.Number()),
|
||||
pollMulti: Type.Optional(Type.Boolean()),
|
||||
};
|
||||
}
|
||||
|
||||
function buildChannelTargetSchema() {
|
||||
return {
|
||||
channelId: Type.Optional(channelTargetSchema()),
|
||||
channelIds: Type.Optional(channelTargetsSchema()),
|
||||
guildId: Type.Optional(Type.String()),
|
||||
@@ -67,13 +97,29 @@ const MessageToolCommonSchema = {
|
||||
authorIds: Type.Optional(Type.Array(Type.String())),
|
||||
roleId: Type.Optional(Type.String()),
|
||||
roleIds: Type.Optional(Type.Array(Type.String())),
|
||||
participant: Type.Optional(Type.String()),
|
||||
};
|
||||
}
|
||||
|
||||
function buildStickerSchema() {
|
||||
return {
|
||||
emojiName: Type.Optional(Type.String()),
|
||||
stickerId: Type.Optional(Type.Array(Type.String())),
|
||||
stickerName: Type.Optional(Type.String()),
|
||||
stickerDesc: Type.Optional(Type.String()),
|
||||
stickerTags: Type.Optional(Type.String()),
|
||||
};
|
||||
}
|
||||
|
||||
function buildThreadSchema() {
|
||||
return {
|
||||
threadName: Type.Optional(Type.String()),
|
||||
autoArchiveMin: Type.Optional(Type.Number()),
|
||||
};
|
||||
}
|
||||
|
||||
function buildEventSchema() {
|
||||
return {
|
||||
query: Type.Optional(Type.String()),
|
||||
eventName: Type.Optional(Type.String()),
|
||||
eventType: Type.Optional(Type.String()),
|
||||
@@ -83,14 +129,26 @@ const MessageToolCommonSchema = {
|
||||
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()),
|
||||
includeArchived: Type.Optional(Type.Boolean()),
|
||||
participant: Type.Optional(Type.String()),
|
||||
fromMe: Type.Optional(Type.Boolean()),
|
||||
};
|
||||
}
|
||||
|
||||
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()),
|
||||
@@ -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(
|
||||
actions: readonly string[],
|
||||
options: { includeButtons: boolean },
|
||||
) {
|
||||
const props: Record<string, unknown> = { ...MessageToolCommonSchema };
|
||||
if (!options.includeButtons) delete props.buttons;
|
||||
|
||||
const props = buildMessageToolSchemaProps(options);
|
||||
return Type.Object({
|
||||
action: stringEnum(actions),
|
||||
...props,
|
||||
|
||||
@@ -150,6 +150,7 @@ function applyCrossContextMessageDecoration({
|
||||
}
|
||||
return applied.message;
|
||||
}
|
||||
|
||||
function readBooleanParam(params: Record<string, unknown>, key: string): boolean | undefined {
|
||||
const raw = params[key];
|
||||
if (typeof raw === "boolean") return raw;
|
||||
@@ -227,16 +228,33 @@ async function resolveActionTarget(params: {
|
||||
}
|
||||
}
|
||||
|
||||
export async function runMessageAction(
|
||||
input: RunMessageActionParams,
|
||||
): Promise<MessageActionRunResult> {
|
||||
const cfg = input.cfg;
|
||||
const params = { ...input.params };
|
||||
parseButtonsParam(params);
|
||||
type ResolvedActionContext = {
|
||||
cfg: ClawdbotConfig;
|
||||
params: Record<string, unknown>;
|
||||
channel: ChannelId;
|
||||
accountId?: string | null;
|
||||
dryRun: boolean;
|
||||
gateway?: MessageActionRunnerGateway;
|
||||
input: RunMessageActionParams;
|
||||
};
|
||||
|
||||
const action = input.action;
|
||||
if (action === "broadcast") {
|
||||
const broadcastEnabled = cfg.tools?.message?.broadcast?.enabled !== false;
|
||||
function resolveGateway(input: RunMessageActionParams): MessageActionRunnerGateway | undefined {
|
||||
if (!input.gateway) return undefined;
|
||||
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) {
|
||||
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.");
|
||||
}
|
||||
const channelHint = readStringParam(params, "channel");
|
||||
const configured = await listConfiguredMessageChannels(cfg);
|
||||
const configured = await listConfiguredMessageChannels(input.cfg);
|
||||
if (configured.length === 0) {
|
||||
throw new Error("Broadcast requires at least one configured channel.");
|
||||
}
|
||||
const targetChannels =
|
||||
channelHint && channelHint.trim().toLowerCase() !== "all"
|
||||
? [await resolveChannel(cfg, { channel: channelHint })]
|
||||
? [await resolveChannel(input.cfg, { channel: channelHint })]
|
||||
: configured;
|
||||
const results: Array<{
|
||||
channel: ChannelId;
|
||||
@@ -264,7 +282,7 @@ export async function runMessageAction(
|
||||
for (const target of rawTargets) {
|
||||
try {
|
||||
const resolved = await resolveMessagingTarget({
|
||||
cfg,
|
||||
cfg: input.cfg,
|
||||
channel: targetChannel,
|
||||
input: target,
|
||||
});
|
||||
@@ -304,38 +322,9 @@ export async function runMessageAction(
|
||||
};
|
||||
}
|
||||
|
||||
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 = 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") {
|
||||
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 });
|
||||
@@ -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 question = readStringParam(params, "pollQuestion", {
|
||||
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) {
|
||||
return {
|
||||
kind: "action",
|
||||
@@ -550,3 +547,72 @@ export async function runMessageAction(
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user