fix: derive prefixes from routed identity (#578) (thanks @p6l-richard)
This commit is contained in:
@@ -1962,7 +1962,28 @@ describe("web auto-reply", () => {
|
||||
|
||||
it("uses identity.name for messagePrefix when set", async () => {
|
||||
setLoadConfigMock(() => ({
|
||||
identity: { name: "Richbot", emoji: "🦁" },
|
||||
agents: {
|
||||
list: [
|
||||
{
|
||||
id: "main",
|
||||
default: true,
|
||||
identity: { name: "Mainbot", emoji: "🦞", theme: "space lobster" },
|
||||
},
|
||||
{
|
||||
id: "rich",
|
||||
identity: { name: "Richbot", emoji: "🦁", theme: "lion bot" },
|
||||
},
|
||||
],
|
||||
},
|
||||
bindings: [
|
||||
{
|
||||
agentId: "rich",
|
||||
match: {
|
||||
provider: "whatsapp",
|
||||
peer: { kind: "dm", id: "+1555" },
|
||||
},
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
let capturedOnMessage:
|
||||
@@ -2003,8 +2024,28 @@ describe("web auto-reply", () => {
|
||||
|
||||
it("uses identity.name for responsePrefix when set", async () => {
|
||||
setLoadConfigMock(() => ({
|
||||
identity: { name: "Richbot", emoji: "🦁" },
|
||||
whatsapp: { allowFrom: ["*"] },
|
||||
agents: {
|
||||
list: [
|
||||
{
|
||||
id: "main",
|
||||
default: true,
|
||||
identity: { name: "Mainbot", emoji: "🦞", theme: "space lobster" },
|
||||
},
|
||||
{
|
||||
id: "rich",
|
||||
identity: { name: "Richbot", emoji: "🦁", theme: "lion bot" },
|
||||
},
|
||||
],
|
||||
},
|
||||
bindings: [
|
||||
{
|
||||
agentId: "rich",
|
||||
match: {
|
||||
provider: "whatsapp",
|
||||
peer: { kind: "dm", id: "+1555" },
|
||||
},
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
let capturedOnMessage:
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
resolveIdentityNamePrefix,
|
||||
resolveResponsePrefix,
|
||||
} from "../agents/identity.js";
|
||||
import {
|
||||
chunkMarkdownText,
|
||||
resolveTextChunkLimit,
|
||||
@@ -1032,13 +1036,14 @@ export async function monitorWebProvider(
|
||||
return `[Replying to ${sender}${idPart}]\n${msg.replyToBody}\n[/Replying]`;
|
||||
};
|
||||
|
||||
const buildLine = (msg: WebInboundMsg) => {
|
||||
// Build message prefix: explicit config > identity name > default "clawdbot"
|
||||
const buildLine = (msg: WebInboundMsg, agentId: string) => {
|
||||
// Build message prefix: explicit config > identity name > default based on allowFrom
|
||||
let messagePrefix = cfg.messages?.messagePrefix;
|
||||
if (messagePrefix === undefined) {
|
||||
const hasAllowFrom = (cfg.whatsapp?.allowFrom?.length ?? 0) > 0;
|
||||
const identityName = cfg.identity?.name?.trim() || "clawdbot";
|
||||
messagePrefix = hasAllowFrom ? "" : `[${identityName}]`;
|
||||
messagePrefix = hasAllowFrom
|
||||
? ""
|
||||
: (resolveIdentityNamePrefix(cfg, agentId) ?? "[clawdbot]");
|
||||
}
|
||||
const prefixStr = messagePrefix ? `${messagePrefix} ` : "";
|
||||
const senderLabel =
|
||||
@@ -1070,7 +1075,7 @@ export async function monitorWebProvider(
|
||||
status.lastEventAt = status.lastMessageAt;
|
||||
emitStatus();
|
||||
const conversationId = msg.conversationId ?? msg.from;
|
||||
let combinedBody = buildLine(msg);
|
||||
let combinedBody = buildLine(msg, route.agentId);
|
||||
let shouldClearGroupHistory = false;
|
||||
|
||||
if (msg.chatType === "group") {
|
||||
@@ -1088,7 +1093,10 @@ export async function monitorWebProvider(
|
||||
}),
|
||||
)
|
||||
.join("\\n");
|
||||
combinedBody = `[Chat messages since your last reply - for context]\\n${historyText}\\n\\n[Current message - respond to this]\\n${buildLine(msg)}`;
|
||||
combinedBody = `[Chat messages since your last reply - for context]\\n${historyText}\\n\\n[Current message - respond to this]\\n${buildLine(
|
||||
msg,
|
||||
route.agentId,
|
||||
)}`;
|
||||
}
|
||||
// Always surface who sent the triggering message so the agent can address them.
|
||||
const senderLabel =
|
||||
@@ -1170,12 +1178,7 @@ export async function monitorWebProvider(
|
||||
const textLimit = resolveTextChunkLimit(cfg, "whatsapp");
|
||||
let didLogHeartbeatStrip = false;
|
||||
let didSendReply = false;
|
||||
// Derive responsePrefix from identity.name if not explicitly set
|
||||
const responsePrefix =
|
||||
cfg.messages?.responsePrefix ??
|
||||
(cfg.identity?.name?.trim()
|
||||
? `[${cfg.identity.name.trim()}]`
|
||||
: undefined);
|
||||
const responsePrefix = resolveResponsePrefix(cfg, route.agentId);
|
||||
const { dispatcher, replyOptions, markDispatchIdle } =
|
||||
createReplyDispatcherWithTyping({
|
||||
responsePrefix,
|
||||
|
||||
Reference in New Issue
Block a user