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]>
This commit is contained in:
@@ -74,7 +74,7 @@ export default async function grabBotChatTools({
|
||||
}: Params): Promise<HarnessTool[]> {
|
||||
const bot_id = bot.id || 0;
|
||||
const tools = [
|
||||
...grabBotTools({ user_id, bot_id, workspace_id, computers }),
|
||||
...grabBotTools({ user_id, bot_id, workspace_id, computers, session_id }),
|
||||
startTaskTool({
|
||||
user_id,
|
||||
bot_id,
|
||||
|
||||
@@ -7,6 +7,7 @@ import setWorkspaceComputerTool from "./set-workspace-computer-tool";
|
||||
import updateScheduleTool from "./update-schedule-tool";
|
||||
import updateInstructionsTool from "./update-instructions-tool";
|
||||
import updateMemoryTool from "./update-memory-tool";
|
||||
import reactToMessageTool from "./react-to-message-tool";
|
||||
|
||||
type Params = {
|
||||
user_id: number | string;
|
||||
@@ -20,6 +21,10 @@ type Params = {
|
||||
* The bot's computers where it runs, the current one first.
|
||||
*/
|
||||
computers?: BUN_SQLITE_COBBLE_COMPUTERS[];
|
||||
/**
|
||||
* The session being answered, so the bot can react to its messages.
|
||||
*/
|
||||
session_id?: number | string;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -32,6 +37,7 @@ export default function grabBotTools({
|
||||
bot_id,
|
||||
workspace_id,
|
||||
computers = [],
|
||||
session_id,
|
||||
}: Params): HarnessTool[] {
|
||||
return [
|
||||
updateMemoryTool({ user_id, bot_id, workspace_id }),
|
||||
@@ -40,6 +46,7 @@ export default function grabBotTools({
|
||||
listSchedulesTool({ user_id, bot_id }),
|
||||
updateScheduleTool({ user_id, bot_id }),
|
||||
deleteScheduleTool({ user_id, bot_id }),
|
||||
...(session_id ? [reactToMessageTool({ bot_id, user_id, session_id })] : []),
|
||||
...(workspace_id && computers.length > 1
|
||||
? [setWorkspaceComputerTool({ user_id, workspace_id, computers })]
|
||||
: []),
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
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}.`;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user