openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
117 lines (116 loc) • 5.48 kB
JavaScript
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-6kR-86XC.mjs";
import { w as parseStrictPositiveInteger } from "./number-coercion-Dq1l5muL.mjs";
import { a as writeRuntimeJson } from "./runtime-BC29JSZp.mjs";
import { t as formatCliCommand } from "./command-format-Cte1SWM4.mjs";
import { r as formatCliJsonFailure } from "./failure-output-Bu6CUgrO.mjs";
import { p as resolveAmbientOwnerAgentId } from "./agent-scope-config-BU4qc9bA.mjs";
import { r as getRuntimeConfig } from "./io.runtime-FunTy0y1.mjs";
import "./config-B-SAEPyK.mjs";
import { i as GATEWAY_CLIENT_NAMES, r as GATEWAY_CLIENT_MODES } from "./client-info-S_xwnr2o.mjs";
import { t as CHANNEL_MESSAGE_ACTION_NAMES } from "./message-action-names-BPz1joQd.mjs";
import { n as resolveMessageBroadcastAccountPlan, r as validateExplicitMessageAccountSelection } from "./message-account-selection-DnyFfrlo.mjs";
import { u as getScopedChannelsCommandSecretTargets } from "./command-secret-targets-DO7lceJ8.mjs";
import { t as resolveMessageSecretScope } from "./message-secret-scope-BLa31UDK.mjs";
import { n as resolveMessageActionMessageId, r as resolveMessageActionOutcome } from "./message-action-contracts-DM5uDaTN.mjs";
import { n as runMessageAction } from "./message-action-runner-CV6_dsD6.mjs";
import { r as withProgress } from "./progress-CqUCMTil.mjs";
import { t as createOutboundSendDepsFromCliSource } from "./outbound-send-mapping-B9jR7tfu.mjs";
import { t as resolveCommandConfigWithSecrets } from "./command-config-resolution-CKCyfKoR.mjs";
//#region src/commands/message.ts
/** CLI entrypoint for channel message actions. */
function buildMessageCliJson(result) {
const messageId = resolveMessageActionMessageId(result.payload);
const sendResult = result.kind === "send" ? result.sendResult : void 0;
const outcome = resolveMessageActionOutcome(result);
return {
...result.kind === "broadcast" ? { ok: outcome.ok } : !outcome.ok ? {
...formatCliJsonFailure(outcome.error),
...sendResult ? { deliveryStatus: sendResult.deliveryStatus } : {},
...outcome.sentBeforeError ? { sentBeforeError: true } : {}
} : {},
action: result.action,
channel: result.channel,
dryRun: result.dryRun,
handledBy: result.handledBy,
...messageId ? { messageId } : {},
payload: result.payload
};
}
/** Resolves config/secrets, runs a channel message action, then renders JSON or text. */
async function messageCommand(opts, deps, runtime) {
const loadedRaw = getRuntimeConfig();
const actionInput = (normalizeOptionalString(opts.action) ?? "") || "send";
const normalizedActionInput = normalizeLowercaseStringOrEmpty(actionInput);
const scope = resolveMessageSecretScope({
channel: opts.channel,
target: opts.target,
targets: opts.targets,
accountId: opts.accountId
});
const explicitAccountId = await validateExplicitMessageAccountSelection({
cfg: loadedRaw,
channel: scope.channel,
accountId: opts.accountId,
checkResolvedAccount: false
});
if (explicitAccountId) {
scope.accountId = explicitAccountId;
opts.accountId = explicitAccountId;
}
const broadcastAccountPlan = normalizedActionInput === "broadcast" && !scope.channel && explicitAccountId ? await resolveMessageBroadcastAccountPlan({
cfg: loadedRaw,
accountId: explicitAccountId
}) : void 0;
const scopedTargets = getScopedChannelsCommandSecretTargets({
config: loadedRaw,
channel: scope.channel,
...broadcastAccountPlan ? { channels: broadcastAccountPlan.secretChannels } : {},
accountId: scope.accountId
});
const { effectiveConfig: cfg } = await resolveCommandConfigWithSecrets({
config: loadedRaw,
commandName: "message",
targetIds: scopedTargets.targetIds,
...scopedTargets.allowedPaths ? { allowedPaths: scopedTargets.allowedPaths } : {},
runtime,
autoEnable: true
});
const agentId = resolveAmbientOwnerAgentId(cfg, void 0, {
surface: "message CLI",
hint: `Run ${formatCliCommand("openclaw config set agents.defaults.systemAgent.agentId <id>")} with a configured agent ID.`
});
const action = CHANNEL_MESSAGE_ACTION_NAMES.find((name) => normalizeLowercaseStringOrEmpty(name) === normalizedActionInput);
if (!action) throw new Error(`Unknown message action "${actionInput}". Use one of ${CHANNEL_MESSAGE_ACTION_NAMES.join(", ")}. Example: ${formatCliCommand("openclaw message send --channel <channel> --target <id> --text <message>")}.`);
const outboundDeps = createOutboundSendDepsFromCliSource(deps);
const run = async () => await runMessageAction({
cfg,
action,
params: opts,
deps: outboundDeps,
agentId,
senderIsOwner: opts.senderIsOwner !== false,
conversationReadOrigin: "direct-operator",
broadcastAccountPlan,
gateway: {
clientName: GATEWAY_CLIENT_NAMES.CLI,
mode: GATEWAY_CLIENT_MODES.CLI
}
});
const json = opts.json === true;
const dryRun = opts.dryRun === true;
const result = !json && !dryRun && (action === "send" || action === "poll") ? await withProgress({
label: action === "poll" ? "Sending poll..." : "Sending...",
indeterminate: true,
enabled: true
}, run) : await run();
if (json) {
writeRuntimeJson(runtime, buildMessageCliJson(result));
return result;
}
const { formatMessageCliText } = await import("./message-format-DVfNNR8z.mjs");
const displayLimit = parseStrictPositiveInteger(opts.limit);
for (const line of formatMessageCliText(result, { displayLimit })) runtime.log(line);
return result;
}
//#endregion
export { messageCommand };