Files
cobble/src/harness/tools/bot/react-to-message-tool.ts
T
tbenandClaude Opus 5.5 d1b5b6d68a Add replies and emoji reactions to chat messages
Every message has a row under it to reply or react. Replying quotes the
message above the composer, and the sent message stores
reply_to_message_id (a plain id, not a foreign key, so deleting the
quoted message only drops the quote). send-message accepts a quote from
any session of the same conversation, so a rollover or a thread that
moved on still sends; the conversation drops a picked quote when its
session changes. The bot reads the quote as "[Replying to ...]" before
the user's turn, file-only replies included, and relayed messages carry
it too.

Reactions are a fixed set (BotMessageReactions) in
bot_message_reactions, one per message, emoji and user or bot. Users
toggle their own through api/admin/bots/react-message, and every bot
gets react_to_message for the session it answers (the user's latest
message by default). Both push the fresh list as server:bot-reaction,
with mine worked out for the session's user.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 21:56:31 +01:00

89 lines
3.1 KiB
TypeScript

import { z } from "zod";
import pushToSessionUser from "@/src/bots-queue/push-to-session-user";
import {
BotMessageReactions,
type BotMessageReactionEmoji,
} from "@/src/dict/bot-dict";
import addBotMessageReaction from "@/src/functions/backend/db/bots/add-bot-message-reaction";
import grabLatestUserMessage from "@/src/functions/backend/db/bots/grab-latest-user-message";
import grabSessionMessage from "@/src/functions/backend/db/bots/grab-session-message";
import type { HarnessTool } from "../../types";
type Params = {
bot_id: number | string;
/**
* The user whose conversation it is.
*/
user_id: number | string;
/**
* The session being answered: the reaction goes to a message in it,
* and the user's open conversation sees it live.
*/
session_id: number | string;
};
type Args = {
emoji: BotMessageReactionEmoji;
message_id?: number;
};
const ReactionEmojis = BotMessageReactions.map((reaction) => reaction.value) as [
BotMessageReactionEmoji,
...BotMessageReactionEmoji[],
];
const EmojiList = BotMessageReactions.map(
(reaction) => `${reaction.value} (${reaction.title.toLowerCase()})`,
).join(", ");
/**
* Lets a bot react to a message in the conversation it's answering. With
* no message id it reacts to the user's latest message, the one a reply is
* usually about. The reaction shows under the message and reaches the
* user's open conversation at once.
*/
export default function reactToMessageTool({ bot_id, user_id, session_id }: Params): HarnessTool<Args> {
return {
name: "react_to_message",
description: `React to a message in this conversation with an emoji (${EmojiList}). Use it when a reaction says it better than words, or when the user asks you to react. It doesn't replace answering. Without message_id it reacts to the user's latest message.`,
schema: z.object({
emoji: z.enum(ReactionEmojis).describe(`One of: ${EmojiList}`),
message_id: z
.number()
.int()
.positive()
.optional()
.describe(`The message to react to; defaults to the user's latest message`),
}),
run: async ({ args }) => {
const message = args.message_id
? await grabSessionMessage({
session_id,
message_id: args.message_id,
})
: await grabLatestUserMessage({ session_id });
if (!message?.id) {
throw new Error(`No message to react to in this conversation.`);
}
const reactions = await addBotMessageReaction({
message_id: Number(message.id),
bot_id,
user_id,
emoji: args.emoji,
});
await pushToSessionUser({
session_id,
data: {
event: "server:bot-reaction",
data: { message_id: Number(message.id), reactions },
},
});
return `Reacted with ${args.emoji}.`;
},
};
}