Участники чата, промпты в файлах, личности только из кода
- Бот запоминает участников (имя, фамилия, ник, активность) и ID автора каждого сообщения - Упоминания в вопросе (@ник и по имени-ссылке) передаются модели с данными участника - Новые инструменты агента: list_chat_members, read_member_messages (поиск по нику/имени) - Бот обращается к людям по имени - Промпты вынесены в prompts/system.md и prompts/personas/<id>.md - Личности больше не редактируются из чата: скрипт seed синхронизирует их с БД при старте - Оставлена одна личность boltun (Dirty D) - Миграция старых баз: колонка messages.user_id Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,20 +1,43 @@
|
||||
import { Composer } from "grammy";
|
||||
import type { Message } from "grammy/types";
|
||||
import { mentionedUsers, toMemberProfile } from "../../libs/telegram-format.js";
|
||||
import type { ChatHistoryService } from "../../services/chat-history-service.js";
|
||||
import type { ChatMemberService } from "../../services/chat-member-service.js";
|
||||
import { createMessageRecorder } from "../reply.js";
|
||||
|
||||
/** Запоминает сообщения всех участников — из них бот читает контекст по просьбе. */
|
||||
export const createHistoryHandlers = (chatHistoryService: ChatHistoryService) => {
|
||||
interface HistoryHandlersDeps {
|
||||
chatHistoryService: ChatHistoryService;
|
||||
chatMemberService: ChatMemberService;
|
||||
}
|
||||
|
||||
/** Запоминает сообщения и участников чата — из них бот берёт контекст по просьбе. */
|
||||
export const createHistoryHandlers = ({ chatHistoryService, chatMemberService }: HistoryHandlersDeps) => {
|
||||
const composer = new Composer();
|
||||
|
||||
const recordMessage = createMessageRecorder(chatHistoryService);
|
||||
|
||||
/** Имена людей, упомянутых по имени-ссылке, — чтобы потом узнавать их и без ника. */
|
||||
const rememberMentioned = (msg: Message) =>
|
||||
mentionedUsers(msg)
|
||||
.filter((user) => !user.is_bot)
|
||||
.forEach((user) => chatMemberService.remember(msg.chat.id, toMemberProfile(user)));
|
||||
|
||||
composer.on("message", async (ctx, next) => {
|
||||
recordMessage(ctx.msg);
|
||||
rememberMentioned(ctx.msg);
|
||||
|
||||
if (ctx.from && !ctx.from.is_bot) chatMemberService.recordActivity(ctx.chat.id, toMemberProfile(ctx.from), ctx.msg.date);
|
||||
|
||||
await next();
|
||||
});
|
||||
|
||||
// Правка только обновляет историю: команды из отредактированных сообщений не выполняются повторно.
|
||||
composer.on("edited_message", (ctx) => recordMessage(ctx.msg));
|
||||
composer.on("edited_message", (ctx) => {
|
||||
recordMessage(ctx.msg);
|
||||
rememberMentioned(ctx.msg);
|
||||
|
||||
if (ctx.from && !ctx.from.is_bot) chatMemberService.remember(ctx.chat.id, toMemberProfile(ctx.from));
|
||||
});
|
||||
|
||||
return composer;
|
||||
};
|
||||
|
||||
@@ -8,13 +8,6 @@ const CALLBACK_PREFIX = "persona:";
|
||||
|
||||
const CALLBACK_PATTERN = new RegExp(`^${CALLBACK_PREFIX}(.+)$`);
|
||||
|
||||
const ADD_USAGE =
|
||||
"Формат: /persona_add <id> <описание характера>\n" +
|
||||
"id — латиница, цифры, _ или - (до 32 символов).\n" +
|
||||
"Пример: /persona_add gopnik Ты гопник с района, говоришь с понятиями, но помогаешь.";
|
||||
|
||||
const DEL_USAGE = "Формат: /persona_del <id>. Список: /personas";
|
||||
|
||||
const preview = (prompt: string): string =>
|
||||
prompt.length > PROMPT_PREVIEW_LENGTH ? `${prompt.slice(0, PROMPT_PREVIEW_LENGTH)}…` : prompt;
|
||||
|
||||
@@ -27,9 +20,8 @@ const buildKeyboard = (personas: Persona[], activeId: string): InlineKeyboard =>
|
||||
const buildListText = (personas: Persona[], activeId: string): string => {
|
||||
const lines = personas.map((p) => {
|
||||
const mark = p.id === activeId ? "✅" : "•";
|
||||
const kind = p.builtin ? "" : " (пользовательская)";
|
||||
|
||||
return `${mark} ${p.name} [${p.id}]${kind}\n ${preview(p.prompt)}`;
|
||||
return `${mark} ${p.name} [${p.id}]\n ${preview(p.prompt)}`;
|
||||
});
|
||||
|
||||
return `Личности бота:\n\n${lines.join("\n\n")}`;
|
||||
@@ -83,41 +75,5 @@ export const createPersonaHandlers = (personaService: PersonaService) => {
|
||||
await ctx.editMessageText(text, { reply_markup }).catch(() => {});
|
||||
});
|
||||
|
||||
composer.command("persona_add", async (ctx) => {
|
||||
const args = ctx.match.trim();
|
||||
const [rawId = ""] = args.split(/\s+/);
|
||||
|
||||
const result = personaService.saveCustom(rawId, args.slice(rawId.length));
|
||||
|
||||
const errors = {
|
||||
invalid: ADD_USAGE,
|
||||
builtin: `«${result.id}» — встроенная личность, её нельзя изменить. Выбери другой id.`,
|
||||
};
|
||||
|
||||
await ctx.reply(
|
||||
result.ok
|
||||
? `${result.updated ? "Обновил" : "Добавил"} личность «${result.id}». Включить: /persona ${result.id}`
|
||||
: errors[result.reason],
|
||||
);
|
||||
});
|
||||
|
||||
composer.command("persona_del", async (ctx) => {
|
||||
const id = ctx.match.trim();
|
||||
|
||||
if (!id) {
|
||||
await ctx.reply(DEL_USAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
const result = personaService.removeCustom(id);
|
||||
|
||||
const errors = {
|
||||
not_found: `Личность «${result.id}» не найдена. Список: /personas`,
|
||||
builtin: "Встроенные личности удалить нельзя.",
|
||||
};
|
||||
|
||||
await ctx.reply(result.ok ? `Удалил личность «${result.id}».` : errors[result.reason]);
|
||||
});
|
||||
|
||||
return composer;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
import { Composer, type Context } from "grammy";
|
||||
import { displayName, mentions, removeMention, toChatMessage } from "../../libs/telegram-format.js";
|
||||
import {
|
||||
displayName,
|
||||
mentionedUsernames,
|
||||
mentionedUsers,
|
||||
mentions,
|
||||
removeMention,
|
||||
toChatMessage,
|
||||
toMemberProfile,
|
||||
} from "../../libs/telegram-format.js";
|
||||
import type { AssistantService } from "../../services/assistant/assistant-service.js";
|
||||
import type { ChatHistoryService } from "../../services/chat-history-service.js";
|
||||
import type { ChatMemberService } from "../../services/chat-member-service.js";
|
||||
import type { PersonaService } from "../../services/persona-service.js";
|
||||
import { createReplier, withTyping } from "../reply.js";
|
||||
|
||||
@@ -9,6 +18,7 @@ interface QuestionHandlersDeps {
|
||||
assistantService: AssistantService;
|
||||
personaService: PersonaService;
|
||||
chatHistoryService: ChatHistoryService;
|
||||
chatMemberService: ChatMemberService;
|
||||
}
|
||||
|
||||
const chatTitle = (ctx: Context): string => {
|
||||
@@ -18,7 +28,12 @@ const chatTitle = (ctx: Context): string => {
|
||||
};
|
||||
|
||||
/** Вопросы боту: /ask, упоминание, ответ на сообщение бота, любое сообщение в личке. Только за фильтром админа. */
|
||||
export const createQuestionHandlers = ({ assistantService, personaService, chatHistoryService }: QuestionHandlersDeps) => {
|
||||
export const createQuestionHandlers = ({
|
||||
assistantService,
|
||||
personaService,
|
||||
chatHistoryService,
|
||||
chatMemberService,
|
||||
}: QuestionHandlersDeps) => {
|
||||
const composer = new Composer();
|
||||
|
||||
const reply = createReplier(chatHistoryService);
|
||||
@@ -31,6 +46,16 @@ export const createQuestionHandlers = ({ assistantService, personaService, chatH
|
||||
return chatHistoryService.find(original.chat.id, original.message_id) ?? toChatMessage(original);
|
||||
};
|
||||
|
||||
/** Кого упомянули в вопросе (кроме самого бота) — подсказываем модели, кто эти люди. */
|
||||
const resolveMentions = (ctx: Context) => {
|
||||
const msg = ctx.msg!;
|
||||
|
||||
const usernames = mentionedUsernames(msg).filter((u) => u.toLowerCase() !== ctx.me.username.toLowerCase());
|
||||
const profiles = mentionedUsers(msg).filter((user) => !user.is_bot).map(toMemberProfile);
|
||||
|
||||
return chatMemberService.resolveMentions(ctx.chat!.id, { usernames, profiles });
|
||||
};
|
||||
|
||||
const answerQuestion = async (ctx: Context, text: string) => {
|
||||
if (!text) {
|
||||
await reply(ctx, "Задай вопрос: /ask <вопрос>");
|
||||
@@ -39,12 +64,16 @@ export const createQuestionHandlers = ({ assistantService, personaService, chatH
|
||||
|
||||
const chatId = ctx.chat!.id;
|
||||
|
||||
const { members, unknownUsernames } = resolveMentions(ctx);
|
||||
|
||||
const question = {
|
||||
chatId,
|
||||
chatTitle: chatTitle(ctx),
|
||||
askerName: displayName(ctx.from!),
|
||||
text,
|
||||
repliedTo: repliedToMessage(ctx),
|
||||
mentionedMembers: members,
|
||||
unknownUsernames,
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user