2026-01-30 03:15:10 +01:00
|
|
|
import type { OpenClawConfig, HumanDelayConfig, IdentityConfig } from "../config/config.js";
|
2026-01-09 12:42:50 +00:00
|
|
|
import { resolveAgentConfig } from "./agent-scope.js";
|
|
|
|
|
|
|
|
|
|
const DEFAULT_ACK_REACTION = "👀";
|
|
|
|
|
|
|
|
|
|
export function resolveAgentIdentity(
|
2026-01-30 03:15:10 +01:00
|
|
|
cfg: OpenClawConfig,
|
2026-01-09 12:42:50 +00:00
|
|
|
agentId: string,
|
|
|
|
|
): IdentityConfig | undefined {
|
|
|
|
|
return resolveAgentConfig(cfg, agentId)?.identity;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-15 11:29:51 -06:00
|
|
|
export function resolveAckReaction(
|
|
|
|
|
cfg: OpenClawConfig,
|
|
|
|
|
agentId: string,
|
|
|
|
|
opts?: { channel?: string; accountId?: string },
|
|
|
|
|
): string {
|
|
|
|
|
// L1: Channel account level
|
|
|
|
|
if (opts?.channel && opts?.accountId) {
|
|
|
|
|
const channelCfg = getChannelConfig(cfg, opts.channel);
|
|
|
|
|
const accounts = channelCfg?.accounts as Record<string, Record<string, unknown>> | undefined;
|
|
|
|
|
const accountReaction = accounts?.[opts.accountId]?.ackReaction as string | undefined;
|
|
|
|
|
if (accountReaction !== undefined) {
|
|
|
|
|
return accountReaction.trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// L2: Channel level
|
|
|
|
|
if (opts?.channel) {
|
|
|
|
|
const channelCfg = getChannelConfig(cfg, opts.channel);
|
|
|
|
|
const channelReaction = channelCfg?.ackReaction as string | undefined;
|
|
|
|
|
if (channelReaction !== undefined) {
|
|
|
|
|
return channelReaction.trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// L3: Global messages level
|
2026-01-09 12:42:50 +00:00
|
|
|
const configured = cfg.messages?.ackReaction;
|
2026-01-31 16:19:20 +09:00
|
|
|
if (configured !== undefined) {
|
|
|
|
|
return configured.trim();
|
|
|
|
|
}
|
2026-02-15 11:29:51 -06:00
|
|
|
|
|
|
|
|
// L4: Agent identity emoji fallback
|
2026-01-09 12:42:50 +00:00
|
|
|
const emoji = resolveAgentIdentity(cfg, agentId)?.emoji?.trim();
|
|
|
|
|
return emoji || DEFAULT_ACK_REACTION;
|
|
|
|
|
}
|
2026-01-09 16:39:32 +01:00
|
|
|
|
2026-01-30 03:15:10 +01:00
|
|
|
export function resolveIdentityNamePrefix(
|
|
|
|
|
cfg: OpenClawConfig,
|
|
|
|
|
agentId: string,
|
|
|
|
|
): string | undefined {
|
2026-01-09 16:39:32 +01:00
|
|
|
const name = resolveAgentIdentity(cfg, agentId)?.name?.trim();
|
2026-01-31 16:19:20 +09:00
|
|
|
if (!name) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2026-01-09 16:39:32 +01:00
|
|
|
return `[${name}]`;
|
|
|
|
|
}
|
|
|
|
|
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
/** Returns just the identity name (without brackets) for template context. */
|
2026-01-30 03:15:10 +01:00
|
|
|
export function resolveIdentityName(cfg: OpenClawConfig, agentId: string): string | undefined {
|
feat: add dynamic template variables to messages.responsePrefix (#923)
Adds support for template variables in `messages.responsePrefix` that
resolve dynamically at runtime with the actual model used (including
after fallback).
Supported variables (case-insensitive):
- {model} - short model name (e.g., "claude-opus-4-5", "gpt-4o")
- {modelFull} - full model identifier (e.g., "anthropic/claude-opus-4-5")
- {provider} - provider name (e.g., "anthropic", "openai")
- {thinkingLevel} or {think} - thinking level ("high", "low", "off")
- {identity.name} or {identityName} - agent identity name
Example: "[{model} | think:{thinkingLevel}]" → "[claude-opus-4-5 | think:high]"
Variables show the actual model used after fallback, not the intended
model. Unresolved variables remain as literal text.
Implementation:
- New module: src/auto-reply/reply/response-prefix-template.ts
- Template interpolation in normalize-reply.ts via context provider
- onModelSelected callback in agent-runner-execution.ts
- Updated all 6 provider message handlers (web, signal, discord,
telegram, slack, imessage)
- 27 unit tests covering all variables and edge cases
- Documentation in docs/gateway/configuration.md and JSDoc
Fixes #923
2026-01-14 23:05:08 -05:00
|
|
|
return resolveAgentIdentity(cfg, agentId)?.name?.trim() || undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 16:52:25 +01:00
|
|
|
export function resolveMessagePrefix(
|
2026-01-30 03:15:10 +01:00
|
|
|
cfg: OpenClawConfig,
|
2026-01-09 16:52:25 +01:00
|
|
|
agentId: string,
|
2026-01-09 19:28:59 +00:00
|
|
|
opts?: { configured?: string; hasAllowFrom?: boolean; fallback?: string },
|
2026-01-09 16:52:25 +01:00
|
|
|
): string {
|
2026-01-09 19:28:59 +00:00
|
|
|
const configured = opts?.configured ?? cfg.messages?.messagePrefix;
|
2026-01-31 16:19:20 +09:00
|
|
|
if (configured !== undefined) {
|
|
|
|
|
return configured;
|
|
|
|
|
}
|
2026-01-09 16:52:25 +01:00
|
|
|
|
|
|
|
|
const hasAllowFrom = opts?.hasAllowFrom === true;
|
2026-01-31 16:19:20 +09:00
|
|
|
if (hasAllowFrom) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2026-01-09 16:52:25 +01:00
|
|
|
|
2026-01-30 03:15:10 +01:00
|
|
|
return resolveIdentityNamePrefix(cfg, agentId) ?? opts?.fallback ?? "[openclaw]";
|
2026-01-09 16:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
feat: per-channel responsePrefix override (#9001)
* 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>
2026-02-05 05:16:34 +08:00
|
|
|
/** Helper to extract a channel config value by dynamic key. */
|
|
|
|
|
function getChannelConfig(
|
|
|
|
|
cfg: OpenClawConfig,
|
|
|
|
|
channel: string,
|
|
|
|
|
): Record<string, unknown> | undefined {
|
|
|
|
|
const channels = cfg.channels as Record<string, unknown> | undefined;
|
|
|
|
|
const value = channels?.[channel];
|
|
|
|
|
return typeof value === "object" && value !== null
|
|
|
|
|
? (value as Record<string, unknown>)
|
|
|
|
|
: undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resolveResponsePrefix(
|
|
|
|
|
cfg: OpenClawConfig,
|
|
|
|
|
agentId: string,
|
|
|
|
|
opts?: { channel?: string; accountId?: string },
|
|
|
|
|
): string | undefined {
|
|
|
|
|
// L1: Channel account level
|
|
|
|
|
if (opts?.channel && opts?.accountId) {
|
|
|
|
|
const channelCfg = getChannelConfig(cfg, opts.channel);
|
|
|
|
|
const accounts = channelCfg?.accounts as Record<string, Record<string, unknown>> | undefined;
|
|
|
|
|
const accountPrefix = accounts?.[opts.accountId]?.responsePrefix as string | undefined;
|
|
|
|
|
if (accountPrefix !== undefined) {
|
|
|
|
|
if (accountPrefix === "auto") {
|
|
|
|
|
return resolveIdentityNamePrefix(cfg, agentId);
|
|
|
|
|
}
|
|
|
|
|
return accountPrefix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// L2: Channel level
|
|
|
|
|
if (opts?.channel) {
|
|
|
|
|
const channelCfg = getChannelConfig(cfg, opts.channel);
|
|
|
|
|
const channelPrefix = channelCfg?.responsePrefix as string | undefined;
|
|
|
|
|
if (channelPrefix !== undefined) {
|
|
|
|
|
if (channelPrefix === "auto") {
|
|
|
|
|
return resolveIdentityNamePrefix(cfg, agentId);
|
|
|
|
|
}
|
|
|
|
|
return channelPrefix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// L4: Global level
|
2026-01-09 16:39:32 +01:00
|
|
|
const configured = cfg.messages?.responsePrefix;
|
2026-01-09 19:28:59 +00:00
|
|
|
if (configured !== undefined) {
|
|
|
|
|
if (configured === "auto") {
|
|
|
|
|
return resolveIdentityNamePrefix(cfg, agentId);
|
|
|
|
|
}
|
|
|
|
|
return configured;
|
|
|
|
|
}
|
2026-01-09 19:18:30 +00:00
|
|
|
return undefined;
|
2026-01-09 16:39:32 +01:00
|
|
|
}
|
2026-01-09 16:52:25 +01:00
|
|
|
|
|
|
|
|
export function resolveEffectiveMessagesConfig(
|
2026-01-30 03:15:10 +01:00
|
|
|
cfg: OpenClawConfig,
|
2026-01-09 16:52:25 +01:00
|
|
|
agentId: string,
|
feat: per-channel responsePrefix override (#9001)
* 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>
2026-02-05 05:16:34 +08:00
|
|
|
opts?: {
|
|
|
|
|
hasAllowFrom?: boolean;
|
|
|
|
|
fallbackMessagePrefix?: string;
|
|
|
|
|
channel?: string;
|
|
|
|
|
accountId?: string;
|
|
|
|
|
},
|
2026-01-09 16:52:25 +01:00
|
|
|
): { messagePrefix: string; responsePrefix?: string } {
|
|
|
|
|
return {
|
|
|
|
|
messagePrefix: resolveMessagePrefix(cfg, agentId, {
|
|
|
|
|
hasAllowFrom: opts?.hasAllowFrom,
|
|
|
|
|
fallback: opts?.fallbackMessagePrefix,
|
|
|
|
|
}),
|
feat: per-channel responsePrefix override (#9001)
* 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>
2026-02-05 05:16:34 +08:00
|
|
|
responsePrefix: resolveResponsePrefix(cfg, agentId, {
|
|
|
|
|
channel: opts?.channel,
|
|
|
|
|
accountId: opts?.accountId,
|
|
|
|
|
}),
|
2026-01-09 16:52:25 +01:00
|
|
|
};
|
|
|
|
|
}
|
2026-01-07 22:56:46 -05:00
|
|
|
|
|
|
|
|
export function resolveHumanDelayConfig(
|
2026-01-30 03:15:10 +01:00
|
|
|
cfg: OpenClawConfig,
|
2026-01-07 22:56:46 -05:00
|
|
|
agentId: string,
|
|
|
|
|
): HumanDelayConfig | undefined {
|
|
|
|
|
const defaults = cfg.agents?.defaults?.humanDelay;
|
|
|
|
|
const overrides = resolveAgentConfig(cfg, agentId)?.humanDelay;
|
2026-01-31 16:19:20 +09:00
|
|
|
if (!defaults && !overrides) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2026-01-07 22:56:46 -05:00
|
|
|
return {
|
|
|
|
|
mode: overrides?.mode ?? defaults?.mode,
|
|
|
|
|
minMs: overrides?.minMs ?? defaults?.minMs,
|
|
|
|
|
maxMs: overrides?.maxMs ?? defaults?.maxMs,
|
|
|
|
|
};
|
|
|
|
|
}
|