* feat: per-channel responsePrefix override
Add responsePrefix field to all channel config types and Zod schemas,
enabling per-channel and per-account outbound response prefix overrides.
Resolution cascade (most specific wins):
L1: channels.<ch>.accounts.<id>.responsePrefix
L2: channels.<ch>.responsePrefix
L3: (reserved for channels.defaults)
L4: messages.responsePrefix (existing global)
Semantics:
- undefined -> inherit from parent level
- empty string -> explicitly no prefix (stops cascade)
- "auto" -> derive [identity.name] from routed agent
Changes:
- Core logic: resolveResponsePrefix() in identity.ts accepts
optional channel/accountId and walks the cascade
- resolveEffectiveMessagesConfig() passes channel context through
- Types: responsePrefix added to WhatsApp, Telegram, Discord, Slack,
Signal, iMessage, Google Chat, MS Teams, Feishu, BlueBubbles configs
- Zod schemas: responsePrefix added for config validation
- All channel handlers wired: telegram, discord, slack, signal,
imessage, line, heartbeat runner, route-reply, native commands
- 23 new tests covering backward compat, channel/account levels,
full cascade, auto keyword, empty string stops, unknown fallthrough
Fully backward compatible - no existing config is affected.
Fixes #8857
* fix: address CI lint + review feedback
- Replace Record<string, any> with proper typed helpers (no-explicit-any)
- Add curly braces to single-line if returns (eslint curly)
- Fix JSDoc: 'Per-channel' → 'channel/account' on shared config types
- Extract getChannelConfig() helper for type-safe dynamic key access
* fix: finish responsePrefix overrides (#9001) (thanks @mudrii)
* fix: normalize prefix wiring and types (#9001) (thanks @mudrii)
---------
Co-authored-by: Gustavo Madeira Santana <gumadeiras@gmail.com>
163 lines
5.5 KiB
TypeScript
163 lines
5.5 KiB
TypeScript
/**
|
|
* Provider-agnostic reply router.
|
|
*
|
|
* Routes replies to the originating channel based on OriginatingChannel/OriginatingTo
|
|
* instead of using the session's lastChannel. This ensures replies go back to the
|
|
* provider where the message originated, even when the main session is shared
|
|
* across multiple providers.
|
|
*/
|
|
|
|
import type { OpenClawConfig } from "../../config/config.js";
|
|
import type { OriginatingChannelType } from "../templating.js";
|
|
import type { ReplyPayload } from "../types.js";
|
|
import { resolveSessionAgentId } from "../../agents/agent-scope.js";
|
|
import { resolveEffectiveMessagesConfig } from "../../agents/identity.js";
|
|
import { normalizeChannelId } from "../../channels/plugins/index.js";
|
|
import { INTERNAL_MESSAGE_CHANNEL, normalizeMessageChannel } from "../../utils/message-channel.js";
|
|
import { normalizeReplyPayload } from "./normalize-reply.js";
|
|
|
|
export type RouteReplyParams = {
|
|
/** The reply payload to send. */
|
|
payload: ReplyPayload;
|
|
/** The originating channel type (telegram, slack, etc). */
|
|
channel: OriginatingChannelType;
|
|
/** The destination chat/channel/user ID. */
|
|
to: string;
|
|
/** Session key for deriving agent identity defaults (multi-agent). */
|
|
sessionKey?: string;
|
|
/** Provider account id (multi-account). */
|
|
accountId?: string;
|
|
/** Thread id for replies (Telegram topic id or Matrix thread event id). */
|
|
threadId?: string | number;
|
|
/** Config for provider-specific settings. */
|
|
cfg: OpenClawConfig;
|
|
/** Optional abort signal for cooperative cancellation. */
|
|
abortSignal?: AbortSignal;
|
|
/** Mirror reply into session transcript (default: true when sessionKey is set). */
|
|
mirror?: boolean;
|
|
};
|
|
|
|
export type RouteReplyResult = {
|
|
/** Whether the reply was sent successfully. */
|
|
ok: boolean;
|
|
/** Optional message ID from the provider. */
|
|
messageId?: string;
|
|
/** Error message if the send failed. */
|
|
error?: string;
|
|
};
|
|
|
|
/**
|
|
* Routes a reply payload to the specified channel.
|
|
*
|
|
* This function provides a unified interface for sending messages to any
|
|
* supported provider. It's used by the followup queue to route replies
|
|
* back to the originating channel when OriginatingChannel/OriginatingTo
|
|
* are set.
|
|
*/
|
|
export async function routeReply(params: RouteReplyParams): Promise<RouteReplyResult> {
|
|
const { payload, channel, to, accountId, threadId, cfg, abortSignal } = params;
|
|
const normalizedChannel = normalizeMessageChannel(channel);
|
|
|
|
// Debug: `pnpm test src/auto-reply/reply/route-reply.test.ts`
|
|
const responsePrefix = params.sessionKey
|
|
? resolveEffectiveMessagesConfig(
|
|
cfg,
|
|
resolveSessionAgentId({
|
|
sessionKey: params.sessionKey,
|
|
config: cfg,
|
|
}),
|
|
{ channel: normalizedChannel, accountId },
|
|
).responsePrefix
|
|
: cfg.messages?.responsePrefix === "auto"
|
|
? undefined
|
|
: cfg.messages?.responsePrefix;
|
|
const normalized = normalizeReplyPayload(payload, {
|
|
responsePrefix,
|
|
});
|
|
if (!normalized) {
|
|
return { ok: true };
|
|
}
|
|
|
|
let text = normalized.text ?? "";
|
|
let mediaUrls = (normalized.mediaUrls?.filter(Boolean) ?? []).length
|
|
? (normalized.mediaUrls?.filter(Boolean) as string[])
|
|
: normalized.mediaUrl
|
|
? [normalized.mediaUrl]
|
|
: [];
|
|
const replyToId = normalized.replyToId;
|
|
|
|
// Skip empty replies.
|
|
if (!text.trim() && mediaUrls.length === 0) {
|
|
return { ok: true };
|
|
}
|
|
|
|
if (channel === INTERNAL_MESSAGE_CHANNEL) {
|
|
return {
|
|
ok: false,
|
|
error: "Webchat routing not supported for queued replies",
|
|
};
|
|
}
|
|
|
|
const channelId = normalizeChannelId(channel) ?? null;
|
|
if (!channelId) {
|
|
return { ok: false, error: `Unknown channel: ${String(channel)}` };
|
|
}
|
|
if (abortSignal?.aborted) {
|
|
return { ok: false, error: "Reply routing aborted" };
|
|
}
|
|
|
|
const resolvedReplyToId =
|
|
replyToId ??
|
|
(channelId === "slack" && threadId != null && threadId !== "" ? String(threadId) : undefined);
|
|
const resolvedThreadId = channelId === "slack" ? null : (threadId ?? null);
|
|
|
|
try {
|
|
// Provider docking: this is an execution boundary (we're about to send).
|
|
// Keep the module cheap to import by loading outbound plumbing lazily.
|
|
const { deliverOutboundPayloads } = await import("../../infra/outbound/deliver.js");
|
|
const results = await deliverOutboundPayloads({
|
|
cfg,
|
|
channel: channelId,
|
|
to,
|
|
accountId: accountId ?? undefined,
|
|
payloads: [normalized],
|
|
replyToId: resolvedReplyToId ?? null,
|
|
threadId: resolvedThreadId,
|
|
abortSignal,
|
|
mirror:
|
|
params.mirror !== false && params.sessionKey
|
|
? {
|
|
sessionKey: params.sessionKey,
|
|
agentId: resolveSessionAgentId({ sessionKey: params.sessionKey, config: cfg }),
|
|
text,
|
|
mediaUrls,
|
|
}
|
|
: undefined,
|
|
});
|
|
|
|
const last = results.at(-1);
|
|
return { ok: true, messageId: last?.messageId };
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
return {
|
|
ok: false,
|
|
error: `Failed to route reply to ${channel}: ${message}`,
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if a channel type is routable via routeReply.
|
|
*
|
|
* Some channels (webchat) require special handling and cannot be routed through
|
|
* this generic interface.
|
|
*/
|
|
export function isRoutableChannel(
|
|
channel: OriginatingChannelType | undefined,
|
|
): channel is Exclude<OriginatingChannelType, typeof INTERNAL_MESSAGE_CHANNEL> {
|
|
if (!channel || channel === INTERNAL_MESSAGE_CHANNEL) {
|
|
return false;
|
|
}
|
|
return normalizeChannelId(channel) !== null;
|
|
}
|