feat: add cross-context messaging resolver
Co-authored-by: Thinh Dinh <tobalsan@users.noreply.github.com>
This commit is contained in:
@@ -24,16 +24,21 @@ Channel selection:
|
|||||||
Target formats (`--to`):
|
Target formats (`--to`):
|
||||||
- WhatsApp: E.164 or group JID
|
- WhatsApp: E.164 or group JID
|
||||||
- Telegram: chat id or `@username`
|
- Telegram: chat id or `@username`
|
||||||
- Discord: `channel:<id>` or `user:<id>` (or `<@id>` mention; raw numeric ids are rejected)
|
- Discord: `channel:<id>` or `user:<id>` (or `<@id>` mention; raw numeric ids are treated as channels)
|
||||||
- Slack: `channel:<id>` or `user:<id>` (raw channel id is accepted)
|
- Slack: `channel:<id>` or `user:<id>` (raw channel id is accepted)
|
||||||
- Signal: `+E.164`, `group:<id>`, `signal:+E.164`, `signal:group:<id>`, or `username:<name>`/`u:<name>`
|
- Signal: `+E.164`, `group:<id>`, `signal:+E.164`, `signal:group:<id>`, or `username:<name>`/`u:<name>`
|
||||||
- iMessage: handle, `chat_id:<id>`, `chat_guid:<guid>`, or `chat_identifier:<id>`
|
- iMessage: handle, `chat_id:<id>`, `chat_guid:<guid>`, or `chat_identifier:<id>`
|
||||||
- MS Teams: conversation id (`19:...@thread.tacv2`) or `conversation:<id>` or `user:<aad-object-id>`
|
- MS Teams: conversation id (`19:...@thread.tacv2`) or `conversation:<id>` or `user:<aad-object-id>`
|
||||||
|
|
||||||
|
Name lookup:
|
||||||
|
- For supported providers (Discord/Slack/etc), channel names like `Help` or `#help` are resolved via the directory cache.
|
||||||
|
- On cache miss, Clawdbot will attempt a live directory lookup when the provider supports it.
|
||||||
|
|
||||||
## Common flags
|
## Common flags
|
||||||
|
|
||||||
- `--channel <name>`
|
- `--channel <name>`
|
||||||
- `--account <id>`
|
- `--account <id>`
|
||||||
|
- `--targets <name>` (repeat; broadcast only)
|
||||||
- `--json`
|
- `--json`
|
||||||
- `--dry-run`
|
- `--dry-run`
|
||||||
- `--verbose`
|
- `--verbose`
|
||||||
@@ -166,6 +171,13 @@ Target formats (`--to`):
|
|||||||
- `ban`: `--guild-id`, `--user-id` (+ `--delete-days`, `--reason`)
|
- `ban`: `--guild-id`, `--user-id` (+ `--delete-days`, `--reason`)
|
||||||
- `timeout` also supports `--reason`
|
- `timeout` also supports `--reason`
|
||||||
|
|
||||||
|
### Broadcast
|
||||||
|
|
||||||
|
- `broadcast`
|
||||||
|
- Channels: any configured channel; use `--channel all` to target all providers
|
||||||
|
- Required: `--targets` (repeat)
|
||||||
|
- Optional: `--message`, `--media`, `--dry-run`
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
Send a Discord reply:
|
Send a Discord reply:
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ export function createClawdbotTools(options?: {
|
|||||||
agentSessionKey: options?.agentSessionKey,
|
agentSessionKey: options?.agentSessionKey,
|
||||||
config: options?.config,
|
config: options?.config,
|
||||||
currentChannelId: options?.currentChannelId,
|
currentChannelId: options?.currentChannelId,
|
||||||
|
currentChannelProvider: options?.agentChannel,
|
||||||
currentThreadTs: options?.currentThreadTs,
|
currentThreadTs: options?.currentThreadTs,
|
||||||
replyToMode: options?.replyToMode,
|
replyToMode: options?.replyToMode,
|
||||||
hasRepliedRef: options?.hasRepliedRef,
|
hasRepliedRef: options?.hasRepliedRef,
|
||||||
|
|||||||
@@ -210,9 +210,12 @@ export async function handleDiscordMessagingAction(
|
|||||||
});
|
});
|
||||||
const mediaUrl = readStringParam(params, "mediaUrl");
|
const mediaUrl = readStringParam(params, "mediaUrl");
|
||||||
const replyTo = readStringParam(params, "replyTo");
|
const replyTo = readStringParam(params, "replyTo");
|
||||||
|
const embeds =
|
||||||
|
Array.isArray(params.embeds) && params.embeds.length > 0 ? params.embeds : undefined;
|
||||||
const result = await sendMessageDiscord(to, content, {
|
const result = await sendMessageDiscord(to, content, {
|
||||||
mediaUrl,
|
mediaUrl,
|
||||||
replyTo,
|
replyTo,
|
||||||
|
embeds,
|
||||||
});
|
});
|
||||||
return jsonResult({ ok: true, result });
|
return jsonResult({ ok: true, result });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ const AllMessageActions = CHANNEL_MESSAGE_ACTION_NAMES;
|
|||||||
const MessageToolCommonSchema = {
|
const MessageToolCommonSchema = {
|
||||||
channel: Type.Optional(Type.String()),
|
channel: Type.Optional(Type.String()),
|
||||||
to: Type.Optional(Type.String()),
|
to: Type.Optional(Type.String()),
|
||||||
|
targets: Type.Optional(Type.Array(Type.String())),
|
||||||
message: Type.Optional(Type.String()),
|
message: Type.Optional(Type.String()),
|
||||||
media: Type.Optional(Type.String()),
|
media: Type.Optional(Type.String()),
|
||||||
buttons: Type.Optional(
|
buttons: Type.Optional(
|
||||||
@@ -127,6 +128,7 @@ type MessageToolOptions = {
|
|||||||
agentSessionKey?: string;
|
agentSessionKey?: string;
|
||||||
config?: ClawdbotConfig;
|
config?: ClawdbotConfig;
|
||||||
currentChannelId?: string;
|
currentChannelId?: string;
|
||||||
|
currentChannelProvider?: string;
|
||||||
currentThreadTs?: string;
|
currentThreadTs?: string;
|
||||||
replyToMode?: "off" | "first" | "all";
|
replyToMode?: "off" | "first" | "all";
|
||||||
hasRepliedRef?: { value: boolean };
|
hasRepliedRef?: { value: boolean };
|
||||||
@@ -175,11 +177,13 @@ export function createMessageTool(options?: MessageToolOptions): AnyAgentTool {
|
|||||||
|
|
||||||
const toolContext =
|
const toolContext =
|
||||||
options?.currentChannelId ||
|
options?.currentChannelId ||
|
||||||
|
options?.currentChannelProvider ||
|
||||||
options?.currentThreadTs ||
|
options?.currentThreadTs ||
|
||||||
options?.replyToMode ||
|
options?.replyToMode ||
|
||||||
options?.hasRepliedRef
|
options?.hasRepliedRef
|
||||||
? {
|
? {
|
||||||
currentChannelId: options?.currentChannelId,
|
currentChannelId: options?.currentChannelId,
|
||||||
|
currentChannelProvider: options?.currentChannelProvider,
|
||||||
currentThreadTs: options?.currentThreadTs,
|
currentThreadTs: options?.currentThreadTs,
|
||||||
replyToMode: options?.replyToMode,
|
replyToMode: options?.replyToMode,
|
||||||
hasRepliedRef: options?.hasRepliedRef,
|
hasRepliedRef: options?.hasRepliedRef,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export function buildThreadingToolContext(params: {
|
|||||||
: provider === "imessage" && sessionCtx.ChatType === "direct"
|
: provider === "imessage" && sessionCtx.ChatType === "direct"
|
||||||
? (sessionCtx.From ?? sessionCtx.To)
|
? (sessionCtx.From ?? sessionCtx.To)
|
||||||
: sessionCtx.To;
|
: sessionCtx.To;
|
||||||
return (
|
const context =
|
||||||
dock.threading.buildToolContext({
|
dock.threading.buildToolContext({
|
||||||
cfg: config,
|
cfg: config,
|
||||||
accountId: sessionCtx.AccountId,
|
accountId: sessionCtx.AccountId,
|
||||||
@@ -44,8 +44,11 @@ export function buildThreadingToolContext(params: {
|
|||||||
MessageThreadId: sessionCtx.MessageThreadId,
|
MessageThreadId: sessionCtx.MessageThreadId,
|
||||||
},
|
},
|
||||||
hasRepliedRef,
|
hasRepliedRef,
|
||||||
}) ?? {}
|
}) ?? {};
|
||||||
);
|
return {
|
||||||
|
...context,
|
||||||
|
currentChannelProvider: provider,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const isBunFetchSocketError = (message?: string) =>
|
export const isBunFetchSocketError = (message?: string) =>
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ export async function handleDiscordMessageAction(
|
|||||||
});
|
});
|
||||||
const mediaUrl = readStringParam(params, "media", { trim: false });
|
const mediaUrl = readStringParam(params, "media", { trim: false });
|
||||||
const replyTo = readStringParam(params, "replyTo");
|
const replyTo = readStringParam(params, "replyTo");
|
||||||
|
const embeds = Array.isArray(params.embeds) ? params.embeds : undefined;
|
||||||
return await handleDiscordAction(
|
return await handleDiscordAction(
|
||||||
{
|
{
|
||||||
action: "sendMessage",
|
action: "sendMessage",
|
||||||
@@ -39,6 +40,7 @@ export async function handleDiscordMessageAction(
|
|||||||
content,
|
content,
|
||||||
mediaUrl: mediaUrl ?? undefined,
|
mediaUrl: mediaUrl ?? undefined,
|
||||||
replyTo: replyTo ?? undefined,
|
replyTo: replyTo ?? undefined,
|
||||||
|
embeds,
|
||||||
},
|
},
|
||||||
cfg,
|
cfg,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,7 +9,11 @@ import {
|
|||||||
collectDiscordAuditChannelIds,
|
collectDiscordAuditChannelIds,
|
||||||
} from "../../discord/audit.js";
|
} from "../../discord/audit.js";
|
||||||
import { probeDiscord } from "../../discord/probe.js";
|
import { probeDiscord } from "../../discord/probe.js";
|
||||||
import { sendMessageDiscord, sendPollDiscord } from "../../discord/send.js";
|
import {
|
||||||
|
listGuildChannelsDiscord,
|
||||||
|
sendMessageDiscord,
|
||||||
|
sendPollDiscord,
|
||||||
|
} from "../../discord/send.js";
|
||||||
import { shouldLogVerbose } from "../../globals.js";
|
import { shouldLogVerbose } from "../../globals.js";
|
||||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../../routing/session-key.js";
|
||||||
import { getChatChannelMeta } from "../registry.js";
|
import { getChatChannelMeta } from "../registry.js";
|
||||||
@@ -209,6 +213,31 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount> = {
|
|||||||
.map((id) => ({ kind: "group", id }) as const);
|
.map((id) => ({ kind: "group", id }) as const);
|
||||||
return groups;
|
return groups;
|
||||||
},
|
},
|
||||||
|
listGroupsLive: async ({ cfg, accountId, query, limit }) => {
|
||||||
|
const account = resolveDiscordAccount({ cfg, accountId });
|
||||||
|
const q = query?.trim().toLowerCase() || "";
|
||||||
|
const guildIds = Object.keys(account.config.guilds ?? {}).filter((id) => /^\d+$/.test(id));
|
||||||
|
const rows: Array<{ kind: "group"; id: string; name?: string; raw?: unknown }> = [];
|
||||||
|
for (const guildId of guildIds) {
|
||||||
|
const channels = await listGuildChannelsDiscord(guildId, {
|
||||||
|
accountId: account.accountId,
|
||||||
|
});
|
||||||
|
for (const channel of channels) {
|
||||||
|
const name = typeof channel.name === "string" ? channel.name : undefined;
|
||||||
|
if (q && name && !name.toLowerCase().includes(q)) continue;
|
||||||
|
rows.push({
|
||||||
|
kind: "group",
|
||||||
|
id: `channel:${channel.id}`,
|
||||||
|
name: name ?? undefined,
|
||||||
|
raw: channel,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const filtered = q ? rows.filter((row) => row.name?.toLowerCase().includes(q)) : rows;
|
||||||
|
const limited =
|
||||||
|
typeof limit === "number" && limit > 0 ? filtered.slice(0, limit) : filtered;
|
||||||
|
return limited;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
actions: discordMessageActions,
|
actions: discordMessageActions,
|
||||||
setup: {
|
setup: {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export const CHANNEL_MESSAGE_ACTION_NAMES = [
|
export const CHANNEL_MESSAGE_ACTION_NAMES = [
|
||||||
"send",
|
"send",
|
||||||
|
"broadcast",
|
||||||
"poll",
|
"poll",
|
||||||
"react",
|
"react",
|
||||||
"reactions",
|
"reactions",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { getChannelPlugin, listChannelPlugins } from "./index.js";
|
|||||||
import type { ChannelMessageActionContext, ChannelMessageActionName } from "./types.js";
|
import type { ChannelMessageActionContext, ChannelMessageActionName } from "./types.js";
|
||||||
|
|
||||||
export function listChannelMessageActions(cfg: ClawdbotConfig): ChannelMessageActionName[] {
|
export function listChannelMessageActions(cfg: ClawdbotConfig): ChannelMessageActionName[] {
|
||||||
const actions = new Set<ChannelMessageActionName>(["send"]);
|
const actions = new Set<ChannelMessageActionName>(["send", "broadcast"]);
|
||||||
for (const plugin of listChannelPlugins()) {
|
for (const plugin of listChannelPlugins()) {
|
||||||
const list = plugin.actions?.listActions?.({ cfg });
|
const list = plugin.actions?.listActions?.({ cfg });
|
||||||
if (!list) continue;
|
if (!list) continue;
|
||||||
|
|||||||
@@ -233,6 +233,13 @@ export type ChannelDirectoryAdapter = {
|
|||||||
limit?: number | null;
|
limit?: number | null;
|
||||||
runtime: RuntimeEnv;
|
runtime: RuntimeEnv;
|
||||||
}) => Promise<ChannelDirectoryEntry[]>;
|
}) => Promise<ChannelDirectoryEntry[]>;
|
||||||
|
listPeersLive?: (params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
accountId?: string | null;
|
||||||
|
query?: string | null;
|
||||||
|
limit?: number | null;
|
||||||
|
runtime: RuntimeEnv;
|
||||||
|
}) => Promise<ChannelDirectoryEntry[]>;
|
||||||
listGroups?: (params: {
|
listGroups?: (params: {
|
||||||
cfg: ClawdbotConfig;
|
cfg: ClawdbotConfig;
|
||||||
accountId?: string | null;
|
accountId?: string | null;
|
||||||
@@ -240,6 +247,13 @@ export type ChannelDirectoryAdapter = {
|
|||||||
limit?: number | null;
|
limit?: number | null;
|
||||||
runtime: RuntimeEnv;
|
runtime: RuntimeEnv;
|
||||||
}) => Promise<ChannelDirectoryEntry[]>;
|
}) => Promise<ChannelDirectoryEntry[]>;
|
||||||
|
listGroupsLive?: (params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
accountId?: string | null;
|
||||||
|
query?: string | null;
|
||||||
|
limit?: number | null;
|
||||||
|
runtime: RuntimeEnv;
|
||||||
|
}) => Promise<ChannelDirectoryEntry[]>;
|
||||||
listGroupMembers?: (params: {
|
listGroupMembers?: (params: {
|
||||||
cfg: ClawdbotConfig;
|
cfg: ClawdbotConfig;
|
||||||
accountId?: string | null;
|
accountId?: string | null;
|
||||||
|
|||||||
@@ -207,6 +207,7 @@ export type ChannelThreadingContext = {
|
|||||||
|
|
||||||
export type ChannelThreadingToolContext = {
|
export type ChannelThreadingToolContext = {
|
||||||
currentChannelId?: string;
|
currentChannelId?: string;
|
||||||
|
currentChannelProvider?: ChannelId;
|
||||||
currentThreadTs?: string;
|
currentThreadTs?: string;
|
||||||
replyToMode?: "off" | "first" | "all";
|
replyToMode?: "off" | "first" | "all";
|
||||||
hasRepliedRef?: { value: boolean };
|
hasRepliedRef?: { value: boolean };
|
||||||
|
|||||||
18
src/cli/program/message/register.broadcast.ts
Normal file
18
src/cli/program/message/register.broadcast.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import type { Command } from "commander";
|
||||||
|
import type { MessageCliHelpers } from "./helpers.js";
|
||||||
|
|
||||||
|
export function registerMessageBroadcastCommand(message: Command, helpers: MessageCliHelpers) {
|
||||||
|
helpers
|
||||||
|
.withMessageBase(
|
||||||
|
message.command("broadcast").description("Broadcast a message to multiple targets"),
|
||||||
|
)
|
||||||
|
.requiredOption(
|
||||||
|
"--targets <target...>",
|
||||||
|
"Targets to broadcast to (repeatable, accepts names or ids)",
|
||||||
|
)
|
||||||
|
.option("--message <text>", "Message to send")
|
||||||
|
.option("--media <url>", "Media URL")
|
||||||
|
.action(async (options: Record<string, unknown>) => {
|
||||||
|
await helpers.runMessageAction("broadcast", options);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ import { registerMessageReactionsCommands } from "./message/register.reactions.j
|
|||||||
import { registerMessageReadEditDeleteCommands } from "./message/register.read-edit-delete.js";
|
import { registerMessageReadEditDeleteCommands } from "./message/register.read-edit-delete.js";
|
||||||
import { registerMessageSendCommand } from "./message/register.send.js";
|
import { registerMessageSendCommand } from "./message/register.send.js";
|
||||||
import { registerMessageThreadCommands } from "./message/register.thread.js";
|
import { registerMessageThreadCommands } from "./message/register.thread.js";
|
||||||
|
import { registerMessageBroadcastCommand } from "./message/register.broadcast.js";
|
||||||
|
|
||||||
export function registerMessageCommands(program: Command, ctx: ProgramContext) {
|
export function registerMessageCommands(program: Command, ctx: ProgramContext) {
|
||||||
const message = program
|
const message = program
|
||||||
@@ -41,6 +42,7 @@ ${theme.muted("Docs:")} ${formatDocsLink("/cli/message", "docs.clawd.bot/cli/mes
|
|||||||
|
|
||||||
const helpers = createMessageCliHelpers(message, ctx.messageChannelOptions);
|
const helpers = createMessageCliHelpers(message, ctx.messageChannelOptions);
|
||||||
registerMessageSendCommand(message, helpers);
|
registerMessageSendCommand(message, helpers);
|
||||||
|
registerMessageBroadcastCommand(message, helpers);
|
||||||
registerMessagePollCommand(message, helpers);
|
registerMessagePollCommand(message, helpers);
|
||||||
registerMessageReactionsCommands(message, helpers);
|
registerMessageReactionsCommands(message, helpers);
|
||||||
registerMessageReadEditDeleteCommands(message, helpers);
|
registerMessageReadEditDeleteCommands(message, helpers);
|
||||||
|
|||||||
@@ -238,6 +238,34 @@ export function formatMessageCliText(result: MessageActionRunResult): string[] {
|
|||||||
return [muted(`[dry-run] would run ${result.action} via ${result.channel}`)];
|
return [muted(`[dry-run] would run ${result.action} via ${result.channel}`)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (result.kind === "broadcast") {
|
||||||
|
const results = result.payload.results ?? [];
|
||||||
|
const rows = results.map((entry) => ({
|
||||||
|
Channel: resolveChannelLabel(entry.channel),
|
||||||
|
Target: shortenText(entry.to, 36),
|
||||||
|
Status: entry.ok ? "ok" : "error",
|
||||||
|
Error: entry.ok ? "" : shortenText(entry.error ?? "unknown error", 48),
|
||||||
|
}));
|
||||||
|
const okCount = results.filter((entry) => entry.ok).length;
|
||||||
|
const total = results.length;
|
||||||
|
const headingLine = ok(
|
||||||
|
`✅ Broadcast complete (${okCount}/${total} succeeded, ${total - okCount} failed)`,
|
||||||
|
);
|
||||||
|
return [
|
||||||
|
headingLine,
|
||||||
|
renderTable({
|
||||||
|
width: opts.width,
|
||||||
|
columns: [
|
||||||
|
{ key: "Channel", header: "Channel", minWidth: 10 },
|
||||||
|
{ key: "Target", header: "Target", minWidth: 12, flex: true },
|
||||||
|
{ key: "Status", header: "Status", minWidth: 6 },
|
||||||
|
{ key: "Error", header: "Error", minWidth: 20, flex: true },
|
||||||
|
],
|
||||||
|
rows: rows.slice(0, 50),
|
||||||
|
}).trimEnd(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
if (result.kind === "send") {
|
if (result.kind === "send") {
|
||||||
if (result.handledBy === "core" && result.sendResult) {
|
if (result.handledBy === "core" && result.sendResult) {
|
||||||
const send = result.sendResult;
|
const send = result.sendResult;
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ describe("messageCommand", () => {
|
|||||||
{
|
{
|
||||||
action: "send",
|
action: "send",
|
||||||
channel: "whatsapp",
|
channel: "whatsapp",
|
||||||
to: "+1",
|
to: "+15551234567",
|
||||||
message: "hi",
|
message: "hi",
|
||||||
},
|
},
|
||||||
deps,
|
deps,
|
||||||
@@ -135,7 +135,7 @@ describe("messageCommand", () => {
|
|||||||
{
|
{
|
||||||
action: "poll",
|
action: "poll",
|
||||||
channel: "discord",
|
channel: "discord",
|
||||||
to: "channel:123",
|
to: "channel:123456789",
|
||||||
pollQuestion: "Snack?",
|
pollQuestion: "Snack?",
|
||||||
pollOption: ["Pizza", "Sushi"],
|
pollOption: ["Pizza", "Sushi"],
|
||||||
},
|
},
|
||||||
@@ -145,7 +145,7 @@ describe("messageCommand", () => {
|
|||||||
expect(handleDiscordAction).toHaveBeenCalledWith(
|
expect(handleDiscordAction).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
action: "poll",
|
action: "poll",
|
||||||
to: "channel:123",
|
to: "channel:123456789",
|
||||||
}),
|
}),
|
||||||
expect.any(Object),
|
expect.any(Object),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -110,6 +110,13 @@ const FIELD_LABELS: Record<string, string> = {
|
|||||||
"agents.list[].tools.byProvider": "Agent Tool Policy by Provider",
|
"agents.list[].tools.byProvider": "Agent Tool Policy by Provider",
|
||||||
"tools.exec.applyPatch.enabled": "Enable apply_patch",
|
"tools.exec.applyPatch.enabled": "Enable apply_patch",
|
||||||
"tools.exec.applyPatch.allowModels": "apply_patch Model Allowlist",
|
"tools.exec.applyPatch.allowModels": "apply_patch Model Allowlist",
|
||||||
|
"tools.message.allowCrossContextSend": "Allow Cross-Context Messaging",
|
||||||
|
"tools.message.crossContext.allowWithinProvider": "Allow Cross-Context (Same Provider)",
|
||||||
|
"tools.message.crossContext.allowAcrossProviders": "Allow Cross-Context (Across Providers)",
|
||||||
|
"tools.message.crossContext.marker.enabled": "Cross-Context Marker",
|
||||||
|
"tools.message.crossContext.marker.prefix": "Cross-Context Marker Prefix",
|
||||||
|
"tools.message.crossContext.marker.suffix": "Cross-Context Marker Suffix",
|
||||||
|
"tools.message.broadcast.enabled": "Enable Message Broadcast",
|
||||||
"tools.web.search.enabled": "Enable Web Search Tool",
|
"tools.web.search.enabled": "Enable Web Search Tool",
|
||||||
"tools.web.search.provider": "Web Search Provider",
|
"tools.web.search.provider": "Web Search Provider",
|
||||||
"tools.web.search.apiKey": "Brave Search API Key",
|
"tools.web.search.apiKey": "Brave Search API Key",
|
||||||
@@ -256,6 +263,20 @@ const FIELD_HELP: Record<string, string> = {
|
|||||||
"Experimental. Enables apply_patch for OpenAI models when allowed by tool policy.",
|
"Experimental. Enables apply_patch for OpenAI models when allowed by tool policy.",
|
||||||
"tools.exec.applyPatch.allowModels":
|
"tools.exec.applyPatch.allowModels":
|
||||||
'Optional allowlist of model ids (e.g. "gpt-5.2" or "openai/gpt-5.2").',
|
'Optional allowlist of model ids (e.g. "gpt-5.2" or "openai/gpt-5.2").',
|
||||||
|
"tools.message.allowCrossContextSend":
|
||||||
|
"Legacy override: allow cross-context sends across all providers.",
|
||||||
|
"tools.message.crossContext.allowWithinProvider":
|
||||||
|
"Allow sends to other channels within the same provider (default: true).",
|
||||||
|
"tools.message.crossContext.allowAcrossProviders":
|
||||||
|
"Allow sends across different providers (default: false).",
|
||||||
|
"tools.message.crossContext.marker.enabled":
|
||||||
|
"Add a visible origin marker when sending cross-context (default: true).",
|
||||||
|
"tools.message.crossContext.marker.prefix":
|
||||||
|
'Text prefix for cross-context markers (supports "{channel}").',
|
||||||
|
"tools.message.crossContext.marker.suffix":
|
||||||
|
'Text suffix for cross-context markers (supports "{channel}").',
|
||||||
|
"tools.message.broadcast.enabled":
|
||||||
|
"Enable broadcast action (default: true).",
|
||||||
"tools.web.search.enabled": "Enable the web_search tool (requires Brave API key).",
|
"tools.web.search.enabled": "Enable the web_search tool (requires Brave API key).",
|
||||||
"tools.web.search.provider": 'Search provider (only "brave" supported today).',
|
"tools.web.search.provider": 'Search provider (only "brave" supported today).',
|
||||||
"tools.web.search.apiKey": "Brave Search API key (fallback: BRAVE_API_KEY env var).",
|
"tools.web.search.apiKey": "Brave Search API key (fallback: BRAVE_API_KEY env var).",
|
||||||
|
|||||||
@@ -134,6 +134,33 @@ export type ToolsConfig = {
|
|||||||
timeoutSeconds?: number;
|
timeoutSeconds?: number;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
/** Message tool configuration. */
|
||||||
|
message?: {
|
||||||
|
/**
|
||||||
|
* @deprecated Use tools.message.crossContext settings.
|
||||||
|
* Allows cross-context sends across providers.
|
||||||
|
*/
|
||||||
|
allowCrossContextSend?: boolean;
|
||||||
|
crossContext?: {
|
||||||
|
/** Allow sends to other channels within the same provider (default: true). */
|
||||||
|
allowWithinProvider?: boolean;
|
||||||
|
/** Allow sends across different providers (default: false). */
|
||||||
|
allowAcrossProviders?: boolean;
|
||||||
|
/** Cross-context marker configuration. */
|
||||||
|
marker?: {
|
||||||
|
/** Enable origin markers for cross-context sends (default: true). */
|
||||||
|
enabled?: boolean;
|
||||||
|
/** Text prefix template, supports {channel}. */
|
||||||
|
prefix?: string;
|
||||||
|
/** Text suffix template, supports {channel}. */
|
||||||
|
suffix?: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
broadcast?: {
|
||||||
|
/** Enable broadcast action (default: true). */
|
||||||
|
enabled?: boolean;
|
||||||
|
};
|
||||||
|
};
|
||||||
agentToAgent?: {
|
agentToAgent?: {
|
||||||
/** Enable agent-to-agent messaging tools. Default: false. */
|
/** Enable agent-to-agent messaging tools. Default: false. */
|
||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
|
|||||||
@@ -288,6 +288,29 @@ export const ToolsSchema = z
|
|||||||
transcription: ToolsAudioTranscriptionSchema,
|
transcription: ToolsAudioTranscriptionSchema,
|
||||||
})
|
})
|
||||||
.optional(),
|
.optional(),
|
||||||
|
message: z
|
||||||
|
.object({
|
||||||
|
allowCrossContextSend: z.boolean().optional(),
|
||||||
|
crossContext: z
|
||||||
|
.object({
|
||||||
|
allowWithinProvider: z.boolean().optional(),
|
||||||
|
allowAcrossProviders: z.boolean().optional(),
|
||||||
|
marker: z
|
||||||
|
.object({
|
||||||
|
enabled: z.boolean().optional(),
|
||||||
|
prefix: z.string().optional(),
|
||||||
|
suffix: z.string().optional(),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
|
broadcast: z
|
||||||
|
.object({
|
||||||
|
enabled: z.boolean().optional(),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
|
})
|
||||||
|
.optional(),
|
||||||
agentToAgent: z
|
agentToAgent: z
|
||||||
.object({
|
.object({
|
||||||
enabled: z.boolean().optional(),
|
enabled: z.boolean().optional(),
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ type DiscordSendOpts = {
|
|||||||
rest?: RequestClient;
|
rest?: RequestClient;
|
||||||
replyTo?: string;
|
replyTo?: string;
|
||||||
retry?: RetryConfig;
|
retry?: RetryConfig;
|
||||||
|
embeds?: unknown[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function sendMessageDiscord(
|
export async function sendMessageDiscord(
|
||||||
@@ -51,6 +52,7 @@ export async function sendMessageDiscord(
|
|||||||
opts.replyTo,
|
opts.replyTo,
|
||||||
request,
|
request,
|
||||||
accountInfo.config.maxLinesPerMessage,
|
accountInfo.config.maxLinesPerMessage,
|
||||||
|
opts.embeds,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
result = await sendDiscordText(
|
result = await sendDiscordText(
|
||||||
@@ -60,6 +62,7 @@ export async function sendMessageDiscord(
|
|||||||
opts.replyTo,
|
opts.replyTo,
|
||||||
request,
|
request,
|
||||||
accountInfo.config.maxLinesPerMessage,
|
accountInfo.config.maxLinesPerMessage,
|
||||||
|
opts.embeds,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -252,6 +252,7 @@ async function sendDiscordText(
|
|||||||
replyTo: string | undefined,
|
replyTo: string | undefined,
|
||||||
request: DiscordRequest,
|
request: DiscordRequest,
|
||||||
maxLinesPerMessage?: number,
|
maxLinesPerMessage?: number,
|
||||||
|
embeds?: unknown[],
|
||||||
) {
|
) {
|
||||||
if (!text.trim()) {
|
if (!text.trim()) {
|
||||||
throw new Error("Message must be non-empty for Discord sends");
|
throw new Error("Message must be non-empty for Discord sends");
|
||||||
@@ -265,7 +266,11 @@ async function sendDiscordText(
|
|||||||
const res = (await request(
|
const res = (await request(
|
||||||
() =>
|
() =>
|
||||||
rest.post(Routes.channelMessages(channelId), {
|
rest.post(Routes.channelMessages(channelId), {
|
||||||
body: { content: chunks[0], message_reference: messageReference },
|
body: {
|
||||||
|
content: chunks[0],
|
||||||
|
message_reference: messageReference,
|
||||||
|
...(embeds?.length ? { embeds } : {}),
|
||||||
|
},
|
||||||
}) as Promise<{ id: string; channel_id: string }>,
|
}) as Promise<{ id: string; channel_id: string }>,
|
||||||
"text",
|
"text",
|
||||||
)) as { id: string; channel_id: string };
|
)) as { id: string; channel_id: string };
|
||||||
@@ -280,6 +285,7 @@ async function sendDiscordText(
|
|||||||
body: {
|
body: {
|
||||||
content: chunk,
|
content: chunk,
|
||||||
message_reference: isFirst ? messageReference : undefined,
|
message_reference: isFirst ? messageReference : undefined,
|
||||||
|
...(isFirst && embeds?.length ? { embeds } : {}),
|
||||||
},
|
},
|
||||||
}) as Promise<{ id: string; channel_id: string }>,
|
}) as Promise<{ id: string; channel_id: string }>,
|
||||||
"text",
|
"text",
|
||||||
@@ -300,6 +306,7 @@ async function sendDiscordMedia(
|
|||||||
replyTo: string | undefined,
|
replyTo: string | undefined,
|
||||||
request: DiscordRequest,
|
request: DiscordRequest,
|
||||||
maxLinesPerMessage?: number,
|
maxLinesPerMessage?: number,
|
||||||
|
embeds?: unknown[],
|
||||||
) {
|
) {
|
||||||
const media = await loadWebMedia(mediaUrl);
|
const media = await loadWebMedia(mediaUrl);
|
||||||
const chunks = text
|
const chunks = text
|
||||||
@@ -316,6 +323,7 @@ async function sendDiscordMedia(
|
|||||||
body: {
|
body: {
|
||||||
content: caption || undefined,
|
content: caption || undefined,
|
||||||
message_reference: messageReference,
|
message_reference: messageReference,
|
||||||
|
...(embeds?.length ? { embeds } : {}),
|
||||||
files: [
|
files: [
|
||||||
{
|
{
|
||||||
data: media.buffer,
|
data: media.buffer,
|
||||||
|
|||||||
@@ -27,10 +27,10 @@ describe("runMessageAction context isolation", () => {
|
|||||||
action: "send",
|
action: "send",
|
||||||
params: {
|
params: {
|
||||||
channel: "slack",
|
channel: "slack",
|
||||||
to: "#C123",
|
to: "#C12345678",
|
||||||
message: "hi",
|
message: "hi",
|
||||||
},
|
},
|
||||||
toolContext: { currentChannelId: "C123" },
|
toolContext: { currentChannelId: "C12345678" },
|
||||||
dryRun: true,
|
dryRun: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -43,10 +43,10 @@ describe("runMessageAction context isolation", () => {
|
|||||||
action: "send",
|
action: "send",
|
||||||
params: {
|
params: {
|
||||||
channel: "slack",
|
channel: "slack",
|
||||||
to: "#C123",
|
to: "#C12345678",
|
||||||
media: "https://example.com/note.ogg",
|
media: "https://example.com/note.ogg",
|
||||||
},
|
},
|
||||||
toolContext: { currentChannelId: "C123" },
|
toolContext: { currentChannelId: "C12345678" },
|
||||||
dryRun: true,
|
dryRun: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -60,44 +60,44 @@ describe("runMessageAction context isolation", () => {
|
|||||||
action: "send",
|
action: "send",
|
||||||
params: {
|
params: {
|
||||||
channel: "slack",
|
channel: "slack",
|
||||||
to: "#C123",
|
to: "#C12345678",
|
||||||
},
|
},
|
||||||
toolContext: { currentChannelId: "C123" },
|
toolContext: { currentChannelId: "C12345678" },
|
||||||
dryRun: true,
|
dryRun: true,
|
||||||
}),
|
}),
|
||||||
).rejects.toThrow(/message required/i);
|
).rejects.toThrow(/message required/i);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("blocks send when target differs from current channel", async () => {
|
it("blocks send when target differs from current channel", async () => {
|
||||||
await expect(
|
const result = await runMessageAction({
|
||||||
runMessageAction({
|
cfg: slackConfig,
|
||||||
cfg: slackConfig,
|
action: "send",
|
||||||
action: "send",
|
params: {
|
||||||
params: {
|
channel: "slack",
|
||||||
channel: "slack",
|
to: "channel:C99999999",
|
||||||
to: "channel:C999",
|
message: "hi",
|
||||||
message: "hi",
|
},
|
||||||
},
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
||||||
toolContext: { currentChannelId: "C123" },
|
dryRun: true,
|
||||||
dryRun: true,
|
});
|
||||||
}),
|
|
||||||
).rejects.toThrow(/Cross-context messaging denied/);
|
expect(result.kind).toBe("send");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("blocks thread-reply when channelId differs from current channel", async () => {
|
it("blocks thread-reply when channelId differs from current channel", async () => {
|
||||||
await expect(
|
const result = await runMessageAction({
|
||||||
runMessageAction({
|
cfg: slackConfig,
|
||||||
cfg: slackConfig,
|
action: "thread-reply",
|
||||||
action: "thread-reply",
|
params: {
|
||||||
params: {
|
channel: "slack",
|
||||||
channel: "slack",
|
channelId: "C99999999",
|
||||||
channelId: "C999",
|
message: "hi",
|
||||||
message: "hi",
|
},
|
||||||
},
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
||||||
toolContext: { currentChannelId: "C123" },
|
dryRun: true,
|
||||||
dryRun: true,
|
});
|
||||||
}),
|
|
||||||
).rejects.toThrow(/Cross-context messaging denied/);
|
expect(result.kind).toBe("action");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows WhatsApp send when target matches current chat", async () => {
|
it("allows WhatsApp send when target matches current chat", async () => {
|
||||||
@@ -117,19 +117,19 @@ describe("runMessageAction context isolation", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("blocks WhatsApp send when target differs from current chat", async () => {
|
it("blocks WhatsApp send when target differs from current chat", async () => {
|
||||||
await expect(
|
const result = await runMessageAction({
|
||||||
runMessageAction({
|
cfg: whatsappConfig,
|
||||||
cfg: whatsappConfig,
|
action: "send",
|
||||||
action: "send",
|
params: {
|
||||||
params: {
|
channel: "whatsapp",
|
||||||
channel: "whatsapp",
|
to: "456@g.us",
|
||||||
to: "456@g.us",
|
message: "hi",
|
||||||
message: "hi",
|
},
|
||||||
},
|
toolContext: { currentChannelId: "123@g.us", currentChannelProvider: "whatsapp" },
|
||||||
toolContext: { currentChannelId: "123@g.us" },
|
dryRun: true,
|
||||||
dryRun: true,
|
});
|
||||||
}),
|
|
||||||
).rejects.toThrow(/Cross-context messaging denied/);
|
expect(result.kind).toBe("send");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("allows iMessage send when target matches current handle", async () => {
|
it("allows iMessage send when target matches current handle", async () => {
|
||||||
@@ -149,16 +149,59 @@ describe("runMessageAction context isolation", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("blocks iMessage send when target differs from current handle", async () => {
|
it("blocks iMessage send when target differs from current handle", async () => {
|
||||||
|
const result = await runMessageAction({
|
||||||
|
cfg: whatsappConfig,
|
||||||
|
action: "send",
|
||||||
|
params: {
|
||||||
|
channel: "imessage",
|
||||||
|
to: "imessage:+15551230000",
|
||||||
|
message: "hi",
|
||||||
|
},
|
||||||
|
toolContext: { currentChannelId: "imessage:+15551234567", currentChannelProvider: "imessage" },
|
||||||
|
dryRun: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.kind).toBe("send");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("blocks cross-provider sends by default", async () => {
|
||||||
await expect(
|
await expect(
|
||||||
runMessageAction({
|
runMessageAction({
|
||||||
cfg: whatsappConfig,
|
cfg: slackConfig,
|
||||||
action: "send",
|
action: "send",
|
||||||
params: {
|
params: {
|
||||||
channel: "imessage",
|
channel: "telegram",
|
||||||
to: "imessage:+15551230000",
|
to: "telegram:@ops",
|
||||||
message: "hi",
|
message: "hi",
|
||||||
},
|
},
|
||||||
toolContext: { currentChannelId: "imessage:+15551234567" },
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
||||||
|
dryRun: true,
|
||||||
|
}),
|
||||||
|
).rejects.toThrow(/Cross-context messaging denied/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("blocks same-provider cross-context when disabled", async () => {
|
||||||
|
const cfg = {
|
||||||
|
...slackConfig,
|
||||||
|
tools: {
|
||||||
|
message: {
|
||||||
|
crossContext: {
|
||||||
|
allowWithinProvider: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as ClawdbotConfig;
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
runMessageAction({
|
||||||
|
cfg,
|
||||||
|
action: "send",
|
||||||
|
params: {
|
||||||
|
channel: "slack",
|
||||||
|
to: "channel:C99999999",
|
||||||
|
message: "hi",
|
||||||
|
},
|
||||||
|
toolContext: { currentChannelId: "C12345678", currentChannelProvider: "slack" },
|
||||||
dryRun: true,
|
dryRun: true,
|
||||||
}),
|
}),
|
||||||
).rejects.toThrow(/Cross-context messaging denied/);
|
).rejects.toThrow(/Cross-context messaging denied/);
|
||||||
|
|||||||
@@ -14,10 +14,11 @@ import type {
|
|||||||
} from "../../channels/plugins/types.js";
|
} from "../../channels/plugins/types.js";
|
||||||
import type { ClawdbotConfig } from "../../config/config.js";
|
import type { ClawdbotConfig } from "../../config/config.js";
|
||||||
import type { GatewayClientMode, GatewayClientName } from "../../utils/message-channel.js";
|
import type { GatewayClientMode, GatewayClientName } from "../../utils/message-channel.js";
|
||||||
import { resolveMessageChannelSelection } from "./channel-selection.js";
|
import { listConfiguredMessageChannels, resolveMessageChannelSelection } from "./channel-selection.js";
|
||||||
import type { OutboundSendDeps } from "./deliver.js";
|
import type { OutboundSendDeps } from "./deliver.js";
|
||||||
import type { MessagePollResult, MessageSendResult } from "./message.js";
|
import type { MessagePollResult, MessageSendResult } from "./message.js";
|
||||||
import { sendMessage, sendPoll } from "./message.js";
|
import { sendMessage, sendPoll } from "./message.js";
|
||||||
|
import { lookupDirectoryDisplay, resolveMessagingTarget } from "./target-resolver.js";
|
||||||
|
|
||||||
export type MessageActionRunnerGateway = {
|
export type MessageActionRunnerGateway = {
|
||||||
url?: string;
|
url?: string;
|
||||||
@@ -53,6 +54,22 @@ export type MessageActionRunResult =
|
|||||||
sendResult?: MessageSendResult;
|
sendResult?: MessageSendResult;
|
||||||
dryRun: boolean;
|
dryRun: boolean;
|
||||||
}
|
}
|
||||||
|
| {
|
||||||
|
kind: "broadcast";
|
||||||
|
channel: ChannelId;
|
||||||
|
action: "broadcast";
|
||||||
|
handledBy: "core" | "dry-run";
|
||||||
|
payload: {
|
||||||
|
results: Array<{
|
||||||
|
channel: ChannelId;
|
||||||
|
to: string;
|
||||||
|
ok: boolean;
|
||||||
|
error?: string;
|
||||||
|
result?: MessageSendResult;
|
||||||
|
}>;
|
||||||
|
};
|
||||||
|
dryRun: boolean;
|
||||||
|
}
|
||||||
| {
|
| {
|
||||||
kind: "poll";
|
kind: "poll";
|
||||||
channel: ChannelId;
|
channel: ChannelId;
|
||||||
@@ -148,11 +165,30 @@ function enforceContextIsolation(params: {
|
|||||||
action: ChannelMessageActionName;
|
action: ChannelMessageActionName;
|
||||||
params: Record<string, unknown>;
|
params: Record<string, unknown>;
|
||||||
toolContext?: ChannelThreadingToolContext;
|
toolContext?: ChannelThreadingToolContext;
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
}): void {
|
}): void {
|
||||||
const currentTarget = params.toolContext?.currentChannelId?.trim();
|
const currentTarget = params.toolContext?.currentChannelId?.trim();
|
||||||
if (!currentTarget) return;
|
if (!currentTarget) return;
|
||||||
if (!CONTEXT_GUARDED_ACTIONS.has(params.action)) return;
|
if (!CONTEXT_GUARDED_ACTIONS.has(params.action)) return;
|
||||||
|
|
||||||
|
if (params.cfg.tools?.message?.allowCrossContextSend) return;
|
||||||
|
|
||||||
|
const currentProvider = params.toolContext?.currentChannelProvider;
|
||||||
|
const allowWithinProvider = params.cfg.tools?.message?.crossContext?.allowWithinProvider !== false;
|
||||||
|
const allowAcrossProviders =
|
||||||
|
params.cfg.tools?.message?.crossContext?.allowAcrossProviders === true;
|
||||||
|
|
||||||
|
if (currentProvider && currentProvider !== params.channel) {
|
||||||
|
if (!allowAcrossProviders) {
|
||||||
|
throw new Error(
|
||||||
|
`Cross-context messaging denied: action=${params.action} target provider "${params.channel}" while bound to "${currentProvider}".`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (allowWithinProvider) return;
|
||||||
|
|
||||||
const target = resolveContextGuardTarget(params.action, params.params);
|
const target = resolveContextGuardTarget(params.action, params.params);
|
||||||
if (!target) return;
|
if (!target) return;
|
||||||
|
|
||||||
@@ -178,6 +214,99 @@ async function resolveChannel(cfg: ClawdbotConfig, params: Record<string, unknow
|
|||||||
return selection.channel;
|
return selection.channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function shouldApplyCrossContextMarker(action: ChannelMessageActionName): boolean {
|
||||||
|
return action === "send" || action === "poll" || action === "thread-reply" || action === "sticker";
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildCrossContextMarker(params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
channel: ChannelId;
|
||||||
|
target: string;
|
||||||
|
toolContext?: ChannelThreadingToolContext;
|
||||||
|
accountId?: string | null;
|
||||||
|
}) {
|
||||||
|
const currentTarget = params.toolContext?.currentChannelId?.trim();
|
||||||
|
if (!currentTarget) return null;
|
||||||
|
const normalizedTarget =
|
||||||
|
normalizeTargetForProvider(params.channel, params.target) ?? params.target.toLowerCase();
|
||||||
|
const normalizedCurrent =
|
||||||
|
normalizeTargetForProvider(params.channel, currentTarget) ?? currentTarget.toLowerCase();
|
||||||
|
if (!normalizedTarget || !normalizedCurrent) return null;
|
||||||
|
if (normalizedTarget === normalizedCurrent) return null;
|
||||||
|
|
||||||
|
const markerEnabled = params.cfg.tools?.message?.crossContext?.marker?.enabled !== false;
|
||||||
|
if (!markerEnabled) return null;
|
||||||
|
|
||||||
|
const currentName =
|
||||||
|
(await lookupDirectoryDisplay({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
targetId: currentTarget,
|
||||||
|
accountId: params.accountId ?? undefined,
|
||||||
|
})) ?? currentTarget;
|
||||||
|
const originLabel = currentName.startsWith("#") ? currentName : `#${currentName}`;
|
||||||
|
const markerConfig = params.cfg.tools?.message?.crossContext?.marker;
|
||||||
|
const prefixTemplate = markerConfig?.prefix ?? "[from {channel}] ";
|
||||||
|
const suffixTemplate = markerConfig?.suffix ?? "";
|
||||||
|
const prefix = prefixTemplate.replaceAll("{channel}", originLabel);
|
||||||
|
const suffix = suffixTemplate.replaceAll("{channel}", originLabel);
|
||||||
|
const discordEmbeds =
|
||||||
|
params.channel === "discord"
|
||||||
|
? [
|
||||||
|
{
|
||||||
|
description: `From ${originLabel}`,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
: undefined;
|
||||||
|
return {
|
||||||
|
prefix,
|
||||||
|
suffix,
|
||||||
|
discordEmbeds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function resolveActionTarget(params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
channel: ChannelId;
|
||||||
|
action: ChannelMessageActionName;
|
||||||
|
args: Record<string, unknown>;
|
||||||
|
accountId?: string | null;
|
||||||
|
}): Promise<void> {
|
||||||
|
const toRaw = typeof params.args.to === "string" ? params.args.to.trim() : "";
|
||||||
|
if (toRaw) {
|
||||||
|
const resolved = await resolveMessagingTarget({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
input: toRaw,
|
||||||
|
accountId: params.accountId ?? undefined,
|
||||||
|
});
|
||||||
|
if (resolved.ok) {
|
||||||
|
params.args.to = resolved.target.to;
|
||||||
|
} else {
|
||||||
|
throw resolved.error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const channelIdRaw =
|
||||||
|
typeof params.args.channelId === "string" ? params.args.channelId.trim() : "";
|
||||||
|
if (channelIdRaw) {
|
||||||
|
const resolved = await resolveMessagingTarget({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
input: channelIdRaw,
|
||||||
|
accountId: params.accountId ?? undefined,
|
||||||
|
preferredKind: "group",
|
||||||
|
});
|
||||||
|
if (resolved.ok) {
|
||||||
|
if (resolved.target.kind === "user") {
|
||||||
|
throw new Error(`Channel id "${channelIdRaw}" resolved to a user target.`);
|
||||||
|
}
|
||||||
|
params.args.channelId = resolved.target.to.replace(/^(channel|group):/i, "");
|
||||||
|
} else {
|
||||||
|
throw resolved.error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function runMessageAction(
|
export async function runMessageAction(
|
||||||
input: RunMessageActionParams,
|
input: RunMessageActionParams,
|
||||||
): Promise<MessageActionRunResult> {
|
): Promise<MessageActionRunResult> {
|
||||||
@@ -186,15 +315,93 @@ export async function runMessageAction(
|
|||||||
parseButtonsParam(params);
|
parseButtonsParam(params);
|
||||||
|
|
||||||
const action = input.action;
|
const action = input.action;
|
||||||
|
if (action === "broadcast") {
|
||||||
|
const broadcastEnabled = cfg.tools?.message?.broadcast?.enabled !== false;
|
||||||
|
if (!broadcastEnabled) {
|
||||||
|
throw new Error("Broadcast is disabled. Set tools.message.broadcast.enabled to true.");
|
||||||
|
}
|
||||||
|
const rawTargets = readStringArrayParam(params, "targets", { required: true }) ?? [];
|
||||||
|
if (rawTargets.length === 0) {
|
||||||
|
throw new Error("Broadcast requires at least one target in --targets.");
|
||||||
|
}
|
||||||
|
const channelHint = readStringParam(params, "channel");
|
||||||
|
const configured = await listConfiguredMessageChannels(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 })]
|
||||||
|
: configured;
|
||||||
|
const results: Array<{
|
||||||
|
channel: ChannelId;
|
||||||
|
to: string;
|
||||||
|
ok: boolean;
|
||||||
|
error?: string;
|
||||||
|
result?: MessageSendResult;
|
||||||
|
}> = [];
|
||||||
|
for (const targetChannel of targetChannels) {
|
||||||
|
for (const target of rawTargets) {
|
||||||
|
try {
|
||||||
|
const resolved = await resolveMessagingTarget({
|
||||||
|
cfg,
|
||||||
|
channel: targetChannel,
|
||||||
|
input: target,
|
||||||
|
});
|
||||||
|
if (!resolved.ok) throw resolved.error;
|
||||||
|
const sendResult = await runMessageAction({
|
||||||
|
...input,
|
||||||
|
action: "send",
|
||||||
|
params: {
|
||||||
|
...params,
|
||||||
|
channel: targetChannel,
|
||||||
|
to: resolved.target.to,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
results.push({
|
||||||
|
channel: targetChannel,
|
||||||
|
to: resolved.target.to,
|
||||||
|
ok: true,
|
||||||
|
result: sendResult.kind === "send" ? sendResult.sendResult : undefined,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
results.push({
|
||||||
|
channel: targetChannel,
|
||||||
|
to: target,
|
||||||
|
ok: false,
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
kind: "broadcast",
|
||||||
|
channel: (targetChannels[0] ?? "discord") as ChannelId,
|
||||||
|
action: "broadcast",
|
||||||
|
handledBy: input.dryRun ? "dry-run" : "core",
|
||||||
|
payload: { results },
|
||||||
|
dryRun: Boolean(input.dryRun),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const channel = await resolveChannel(cfg, params);
|
const channel = await resolveChannel(cfg, params);
|
||||||
const accountId = readStringParam(params, "accountId") ?? input.defaultAccountId;
|
const accountId = readStringParam(params, "accountId") ?? input.defaultAccountId;
|
||||||
const dryRun = Boolean(input.dryRun ?? readBooleanParam(params, "dryRun"));
|
const dryRun = Boolean(input.dryRun ?? readBooleanParam(params, "dryRun"));
|
||||||
|
|
||||||
|
await resolveActionTarget({
|
||||||
|
cfg,
|
||||||
|
channel,
|
||||||
|
action,
|
||||||
|
args: params,
|
||||||
|
accountId,
|
||||||
|
});
|
||||||
|
|
||||||
enforceContextIsolation({
|
enforceContextIsolation({
|
||||||
channel,
|
channel,
|
||||||
action,
|
action,
|
||||||
params,
|
params,
|
||||||
toolContext: input.toolContext,
|
toolContext: input.toolContext,
|
||||||
|
cfg,
|
||||||
});
|
});
|
||||||
|
|
||||||
const gateway = input.gateway
|
const gateway = input.gateway
|
||||||
@@ -226,9 +433,29 @@ export async function runMessageAction(
|
|||||||
params.media = parsed.mediaUrls?.[0] || parsed.mediaUrl || undefined;
|
params.media = parsed.mediaUrls?.[0] || parsed.mediaUrl || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const marker =
|
||||||
|
shouldApplyCrossContextMarker(action) && input.toolContext
|
||||||
|
? await buildCrossContextMarker({
|
||||||
|
cfg,
|
||||||
|
channel,
|
||||||
|
target: to,
|
||||||
|
toolContext: input.toolContext,
|
||||||
|
accountId: accountId ?? undefined,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
const useTextMarker = !(channel === "discord" && marker?.discordEmbeds?.length);
|
||||||
|
if (useTextMarker && (marker?.prefix || marker?.suffix)) {
|
||||||
|
const base = params.message ?? "";
|
||||||
|
params.message = `${marker?.prefix ?? ""}${base}${marker?.suffix ?? ""}`;
|
||||||
|
message = params.message;
|
||||||
|
}
|
||||||
|
|
||||||
const mediaUrl = readStringParam(params, "media", { trim: false });
|
const mediaUrl = readStringParam(params, "media", { trim: false });
|
||||||
const gifPlayback = readBooleanParam(params, "gifPlayback") ?? false;
|
const gifPlayback = readBooleanParam(params, "gifPlayback") ?? false;
|
||||||
const bestEffort = readBooleanParam(params, "bestEffort");
|
const bestEffort = readBooleanParam(params, "bestEffort");
|
||||||
|
if (marker?.discordEmbeds && channel === "discord") {
|
||||||
|
params.embeds = marker.discordEmbeds;
|
||||||
|
}
|
||||||
|
|
||||||
if (!dryRun) {
|
if (!dryRun) {
|
||||||
const handled = await dispatchChannelMessageAction({
|
const handled = await dispatchChannelMessageAction({
|
||||||
@@ -302,6 +529,23 @@ export async function runMessageAction(
|
|||||||
integer: true,
|
integer: true,
|
||||||
});
|
});
|
||||||
const maxSelections = allowMultiselect ? Math.max(2, options.length) : 1;
|
const maxSelections = allowMultiselect ? Math.max(2, options.length) : 1;
|
||||||
|
const marker =
|
||||||
|
shouldApplyCrossContextMarker(action) && input.toolContext
|
||||||
|
? await buildCrossContextMarker({
|
||||||
|
cfg,
|
||||||
|
channel,
|
||||||
|
target: to,
|
||||||
|
toolContext: input.toolContext,
|
||||||
|
accountId: accountId ?? undefined,
|
||||||
|
})
|
||||||
|
: null;
|
||||||
|
if (marker?.prefix || marker?.suffix) {
|
||||||
|
const base = typeof params.message === "string" ? params.message : "";
|
||||||
|
params.message = `${marker?.prefix ?? ""}${base}${marker?.suffix ?? ""}`;
|
||||||
|
}
|
||||||
|
if (marker?.discordEmbeds && channel === "discord") {
|
||||||
|
params.embeds = marker.discordEmbeds;
|
||||||
|
}
|
||||||
|
|
||||||
if (!dryRun) {
|
if (!dryRun) {
|
||||||
const handled = await dispatchChannelMessageAction({
|
const handled = await dispatchChannelMessageAction({
|
||||||
|
|||||||
312
src/infra/outbound/target-resolver.ts
Normal file
312
src/infra/outbound/target-resolver.ts
Normal file
@@ -0,0 +1,312 @@
|
|||||||
|
import { normalizeTargetForProvider } from "../../agents/pi-embedded-messaging.js";
|
||||||
|
import { getChannelPlugin } from "../../channels/plugins/index.js";
|
||||||
|
import type {
|
||||||
|
ChannelDirectoryEntry,
|
||||||
|
ChannelDirectoryEntryKind,
|
||||||
|
ChannelId,
|
||||||
|
} from "../../channels/plugins/types.js";
|
||||||
|
import type { ClawdbotConfig } from "../../config/config.js";
|
||||||
|
import { defaultRuntime, type RuntimeEnv } from "../../runtime.js";
|
||||||
|
|
||||||
|
export type TargetResolveKind = ChannelDirectoryEntryKind | "channel";
|
||||||
|
|
||||||
|
export type ResolvedMessagingTarget = {
|
||||||
|
to: string;
|
||||||
|
kind: TargetResolveKind;
|
||||||
|
display?: string;
|
||||||
|
source: "normalized" | "directory";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResolveMessagingTargetResult =
|
||||||
|
| { ok: true; target: ResolvedMessagingTarget }
|
||||||
|
| { ok: false; error: Error; candidates?: ChannelDirectoryEntry[] };
|
||||||
|
|
||||||
|
type DirectoryCacheEntry = {
|
||||||
|
entries: ChannelDirectoryEntry[];
|
||||||
|
fetchedAt: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const CACHE_TTL_MS = 30 * 60 * 1000;
|
||||||
|
const directoryCache = new Map<string, DirectoryCacheEntry>();
|
||||||
|
let lastConfigRef: ClawdbotConfig | null = null;
|
||||||
|
|
||||||
|
function resetCacheIfConfigChanged(cfg: ClawdbotConfig): void {
|
||||||
|
if (lastConfigRef && lastConfigRef !== cfg) {
|
||||||
|
directoryCache.clear();
|
||||||
|
}
|
||||||
|
lastConfigRef = cfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildCacheKey(params: {
|
||||||
|
channel: ChannelId;
|
||||||
|
accountId?: string | null;
|
||||||
|
kind: ChannelDirectoryEntryKind;
|
||||||
|
source: "cache" | "live";
|
||||||
|
}) {
|
||||||
|
return `${params.channel}:${params.accountId ?? "default"}:${params.kind}:${params.source}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeQuery(value: string): string {
|
||||||
|
return value.trim().toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
function stripTargetPrefixes(value: string): string {
|
||||||
|
return value.replace(/^(channel|group|user):/i, "").replace(/^[@#]/, "").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function preserveTargetCase(channel: ChannelId, raw: string, normalized: string): string {
|
||||||
|
if (channel !== "slack") return normalized;
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
if (/^channel:/i.test(trimmed) || /^user:/i.test(trimmed)) return trimmed;
|
||||||
|
if (trimmed.startsWith("#")) return `channel:${trimmed.slice(1).trim()}`;
|
||||||
|
if (trimmed.startsWith("@")) return `user:${trimmed.slice(1).trim()}`;
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
function detectTargetKind(raw: string, preferred?: TargetResolveKind): TargetResolveKind {
|
||||||
|
if (preferred) return preferred;
|
||||||
|
const trimmed = raw.trim();
|
||||||
|
if (!trimmed) return "group";
|
||||||
|
if (trimmed.startsWith("@") || /^<@!?/.test(trimmed) || /^user:/i.test(trimmed)) return "user";
|
||||||
|
if (trimmed.startsWith("#") || /^channel:/i.test(trimmed) || /^group:/i.test(trimmed)) {
|
||||||
|
return "group";
|
||||||
|
}
|
||||||
|
return "group";
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeDirectoryEntryId(channel: ChannelId, entry: ChannelDirectoryEntry): string {
|
||||||
|
const normalized = normalizeTargetForProvider(channel, entry.id);
|
||||||
|
return normalized ?? entry.id.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function matchesDirectoryEntry(params: {
|
||||||
|
channel: ChannelId;
|
||||||
|
entry: ChannelDirectoryEntry;
|
||||||
|
query: string;
|
||||||
|
}): boolean {
|
||||||
|
const query = normalizeQuery(params.query);
|
||||||
|
if (!query) return false;
|
||||||
|
const id = stripTargetPrefixes(normalizeDirectoryEntryId(params.channel, params.entry));
|
||||||
|
const name = params.entry.name ? stripTargetPrefixes(params.entry.name) : "";
|
||||||
|
const handle = params.entry.handle ? stripTargetPrefixes(params.entry.handle) : "";
|
||||||
|
const candidates = [id, name, handle].map((value) => normalizeQuery(value)).filter(Boolean);
|
||||||
|
return candidates.some((value) => value === query || value.includes(query));
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveMatch(params: {
|
||||||
|
channel: ChannelId;
|
||||||
|
entries: ChannelDirectoryEntry[];
|
||||||
|
query: string;
|
||||||
|
}) {
|
||||||
|
const matches = params.entries.filter((entry) =>
|
||||||
|
matchesDirectoryEntry({ channel: params.channel, entry, query: params.query }),
|
||||||
|
);
|
||||||
|
if (matches.length === 0) return { kind: "none" as const };
|
||||||
|
if (matches.length === 1) return { kind: "single" as const, entry: matches[0] };
|
||||||
|
return { kind: "ambiguous" as const, entries: matches };
|
||||||
|
}
|
||||||
|
|
||||||
|
function looksLikeId(channel: ChannelId, normalized: string): boolean {
|
||||||
|
if (!normalized) return false;
|
||||||
|
const raw = normalized.trim();
|
||||||
|
switch (channel) {
|
||||||
|
case "discord": {
|
||||||
|
const candidate = stripTargetPrefixes(raw);
|
||||||
|
return /^\d{6,}$/.test(candidate);
|
||||||
|
}
|
||||||
|
case "slack": {
|
||||||
|
const candidate = stripTargetPrefixes(raw);
|
||||||
|
return /^[A-Z0-9]{8,}$/i.test(candidate);
|
||||||
|
}
|
||||||
|
case "msteams": {
|
||||||
|
return /^conversation:/i.test(raw) || /^user:/i.test(raw) || raw.includes("@thread");
|
||||||
|
}
|
||||||
|
case "telegram": {
|
||||||
|
return /^telegram:/i.test(raw) || raw.startsWith("@");
|
||||||
|
}
|
||||||
|
case "whatsapp": {
|
||||||
|
const candidate = stripTargetPrefixes(raw);
|
||||||
|
return (
|
||||||
|
/@/i.test(candidate) ||
|
||||||
|
/^\+?\d{3,}$/.test(candidate) ||
|
||||||
|
candidate.toLowerCase().endsWith("@g.us")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return Boolean(raw);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function listDirectoryEntries(params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
channel: ChannelId;
|
||||||
|
accountId?: string | null;
|
||||||
|
kind: ChannelDirectoryEntryKind;
|
||||||
|
runtime?: RuntimeEnv;
|
||||||
|
query?: string;
|
||||||
|
source: "cache" | "live";
|
||||||
|
}): Promise<ChannelDirectoryEntry[]> {
|
||||||
|
const plugin = getChannelPlugin(params.channel);
|
||||||
|
const directory = plugin?.directory;
|
||||||
|
if (!directory) return [];
|
||||||
|
const runtime = params.runtime ?? defaultRuntime;
|
||||||
|
const useLive = params.source === "live";
|
||||||
|
if (params.kind === "user") {
|
||||||
|
const fn = useLive ? directory.listPeersLive ?? directory.listPeers : directory.listPeers;
|
||||||
|
if (!fn) return [];
|
||||||
|
return await fn({
|
||||||
|
cfg: params.cfg,
|
||||||
|
accountId: params.accountId ?? undefined,
|
||||||
|
query: params.query ?? undefined,
|
||||||
|
limit: undefined,
|
||||||
|
runtime,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
const fn = useLive ? directory.listGroupsLive ?? directory.listGroups : directory.listGroups;
|
||||||
|
if (!fn) return [];
|
||||||
|
return await fn({
|
||||||
|
cfg: params.cfg,
|
||||||
|
accountId: params.accountId ?? undefined,
|
||||||
|
query: params.query ?? undefined,
|
||||||
|
limit: undefined,
|
||||||
|
runtime,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getDirectoryEntries(params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
channel: ChannelId;
|
||||||
|
accountId?: string | null;
|
||||||
|
kind: ChannelDirectoryEntryKind;
|
||||||
|
query?: string;
|
||||||
|
runtime?: RuntimeEnv;
|
||||||
|
preferLiveOnMiss?: boolean;
|
||||||
|
}): Promise<ChannelDirectoryEntry[]> {
|
||||||
|
resetCacheIfConfigChanged(params.cfg);
|
||||||
|
const cacheKey = buildCacheKey({
|
||||||
|
channel: params.channel,
|
||||||
|
accountId: params.accountId,
|
||||||
|
kind: params.kind,
|
||||||
|
source: "cache",
|
||||||
|
});
|
||||||
|
const cached = directoryCache.get(cacheKey);
|
||||||
|
if (cached && Date.now() - cached.fetchedAt < CACHE_TTL_MS) {
|
||||||
|
return cached.entries;
|
||||||
|
}
|
||||||
|
const entries = await listDirectoryEntries({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
accountId: params.accountId,
|
||||||
|
kind: params.kind,
|
||||||
|
query: params.query,
|
||||||
|
runtime: params.runtime,
|
||||||
|
source: "cache",
|
||||||
|
});
|
||||||
|
if (entries.length > 0 || !params.preferLiveOnMiss) {
|
||||||
|
directoryCache.set(cacheKey, { entries, fetchedAt: Date.now() });
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
const liveKey = buildCacheKey({
|
||||||
|
channel: params.channel,
|
||||||
|
accountId: params.accountId,
|
||||||
|
kind: params.kind,
|
||||||
|
source: "live",
|
||||||
|
});
|
||||||
|
const liveEntries = await listDirectoryEntries({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
accountId: params.accountId,
|
||||||
|
kind: params.kind,
|
||||||
|
query: params.query,
|
||||||
|
runtime: params.runtime,
|
||||||
|
source: "live",
|
||||||
|
});
|
||||||
|
directoryCache.set(liveKey, { entries: liveEntries, fetchedAt: Date.now() });
|
||||||
|
return liveEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resolveMessagingTarget(params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
channel: ChannelId;
|
||||||
|
input: string;
|
||||||
|
accountId?: string | null;
|
||||||
|
preferredKind?: TargetResolveKind;
|
||||||
|
runtime?: RuntimeEnv;
|
||||||
|
}): Promise<ResolveMessagingTargetResult> {
|
||||||
|
const raw = params.input.trim();
|
||||||
|
if (!raw) {
|
||||||
|
return { ok: false, error: new Error("Target is required") };
|
||||||
|
}
|
||||||
|
const kind = detectTargetKind(raw, params.preferredKind);
|
||||||
|
const normalized = normalizeTargetForProvider(params.channel, raw) ?? raw;
|
||||||
|
if (looksLikeId(params.channel, normalized)) {
|
||||||
|
const directTarget = preserveTargetCase(params.channel, raw, normalized);
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
target: {
|
||||||
|
to: directTarget,
|
||||||
|
kind,
|
||||||
|
display: stripTargetPrefixes(raw),
|
||||||
|
source: "normalized",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const query = stripTargetPrefixes(raw);
|
||||||
|
const entries = await getDirectoryEntries({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
accountId: params.accountId,
|
||||||
|
kind: kind === "user" ? "user" : "group",
|
||||||
|
query,
|
||||||
|
runtime: params.runtime,
|
||||||
|
preferLiveOnMiss: true,
|
||||||
|
});
|
||||||
|
const match = resolveMatch({ channel: params.channel, entries, query });
|
||||||
|
if (match.kind === "single") {
|
||||||
|
const entry = match.entry;
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
target: {
|
||||||
|
to: normalizeDirectoryEntryId(params.channel, entry),
|
||||||
|
kind,
|
||||||
|
display: entry.name ?? entry.handle ?? stripTargetPrefixes(entry.id),
|
||||||
|
source: "directory",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (match.kind === "ambiguous") {
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: new Error(
|
||||||
|
`Ambiguous target "${raw}". Provide a unique name or an explicit id.`,
|
||||||
|
),
|
||||||
|
candidates: match.entries,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
ok: false,
|
||||||
|
error: new Error(`Unknown target "${raw}" for ${params.channel}.`),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function lookupDirectoryDisplay(params: {
|
||||||
|
cfg: ClawdbotConfig;
|
||||||
|
channel: ChannelId;
|
||||||
|
targetId: string;
|
||||||
|
accountId?: string | null;
|
||||||
|
runtime?: RuntimeEnv;
|
||||||
|
}): Promise<string | undefined> {
|
||||||
|
const normalized = normalizeTargetForProvider(params.channel, params.targetId) ?? params.targetId;
|
||||||
|
const candidates = await getDirectoryEntries({
|
||||||
|
cfg: params.cfg,
|
||||||
|
channel: params.channel,
|
||||||
|
accountId: params.accountId,
|
||||||
|
kind: "group",
|
||||||
|
runtime: params.runtime,
|
||||||
|
preferLiveOnMiss: false,
|
||||||
|
});
|
||||||
|
const entry = candidates.find(
|
||||||
|
(candidate) => normalizeDirectoryEntryId(params.channel, candidate) === normalized,
|
||||||
|
);
|
||||||
|
return entry?.name ?? entry?.handle ?? undefined;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user