Files
clawdbot/src/cli/program/message/helpers.ts
2026-01-17 04:15:46 +00:00

67 lines
2.2 KiB
TypeScript

import type { Command } from "commander";
import { messageCommand } from "../../../commands/message.js";
import { danger, setVerbose } from "../../../globals.js";
import { CHANNEL_TARGET_DESCRIPTION } from "../../../infra/outbound/channel-target.js";
import { defaultRuntime } from "../../../runtime.js";
import { createDefaultDeps } from "../../deps.js";
export type MessageCliHelpers = {
withMessageBase: (command: Command) => Command;
withMessageTarget: (command: Command) => Command;
withRequiredMessageTarget: (command: Command) => Command;
runMessageAction: (action: string, opts: Record<string, unknown>) => Promise<void>;
};
export function createMessageCliHelpers(
message: Command,
messageChannelOptions: string,
): MessageCliHelpers {
const withMessageBase = (command: Command) =>
command
.option("--channel <channel>", `Channel: ${messageChannelOptions}`)
.option("--account <id>", "Channel account id (accountId)")
.option("--json", "Output result as JSON", false)
.option("--dry-run", "Print payload and skip sending", false)
.option("--verbose", "Verbose logging", false);
const withMessageTarget = (command: Command) =>
command.option("-t, --target <dest>", CHANNEL_TARGET_DESCRIPTION);
const withRequiredMessageTarget = (command: Command) =>
command.requiredOption("-t, --target <dest>", CHANNEL_TARGET_DESCRIPTION);
const runMessageAction = async (action: string, opts: Record<string, unknown>) => {
setVerbose(Boolean(opts.verbose));
const deps = createDefaultDeps();
try {
await messageCommand(
{
...(() => {
const { account, ...rest } = opts;
return {
...rest,
accountId: typeof account === "string" ? account : undefined,
};
})(),
action,
},
deps,
defaultRuntime,
);
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
};
// `message` is only used for `message.help({ error: true })`, keep the
// command-specific helpers grouped here.
void message;
return {
withMessageBase,
withMessageTarget,
withRequiredMessageTarget,
runMessageAction,
};
}