/clear из лички: выбор чата кнопками с подтверждением
- /clear в личке с ботом показывает чаты с контекстом диалога (название и число реплик), дальше подтверждение - /clear <ID чата> в личке — сразу подтверждение для этого чата; в группе /clear работает как раньше - Общий хелпер chatTitle для /archive и /clear Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,7 @@ import type { Persona } from "../../types.js";
|
||||
import type { ChatHistoryService } from "../chat-history-service.js";
|
||||
import type { ChatMemberService } from "../chat-member-service.js";
|
||||
import { stripPastReasoning } from "./history-compat.js";
|
||||
import { chatThreadId } from "./memory.js";
|
||||
import { chatIdOfThread, chatThreadId } from "./memory.js";
|
||||
import { buildInstructions, buildUserText, type Question } from "./prompts.js";
|
||||
import { createListChatMembersTool } from "./tools/list-chat-members-tool.js";
|
||||
import { createReadChatMessagesTool } from "./tools/read-chat-messages-tool.js";
|
||||
@@ -117,8 +117,23 @@ export const createAssistantService = ({
|
||||
/** Начать диалог с чистого листа: удаляем тред чата из Memory. Архив чата не трогаем. */
|
||||
const clearDialog = (chatId: number): Promise<void> => memory.deleteThread(chatThreadId(chatId));
|
||||
|
||||
/** Чаты, где есть контекст диалога, и сколько в нём реплик — для выбора в личке с ботом. */
|
||||
const dialogChats = async (): Promise<{ chatId: number; messages: number }[]> => {
|
||||
const { threads } = await memory.listThreads({ perPage: false });
|
||||
|
||||
const chatIds = threads.flatMap((thread) => {
|
||||
const chatId = chatIdOfThread(thread.id);
|
||||
|
||||
return chatId === undefined ? [] : [chatId];
|
||||
});
|
||||
|
||||
const chats = await Promise.all(chatIds.map(async (chatId) => ({ chatId, messages: await dialogSize(chatId) })));
|
||||
|
||||
return chats.filter((chat) => chat.messages > 0).sort((a, b) => b.messages - a.messages);
|
||||
};
|
||||
|
||||
/** Боту стоит скачивать фото, только если модель умеет их смотреть. */
|
||||
return { answer, dialogSize, clearDialog, canSeeImages: imageInput };
|
||||
return { answer, dialogSize, clearDialog, dialogChats, canSeeImages: imageInput };
|
||||
};
|
||||
|
||||
export type AssistantService = ReturnType<typeof createAssistantService>;
|
||||
|
||||
@@ -28,3 +28,10 @@ export const createAssistantMemory = ({ path, maxTokens }: AssistantMemoryOption
|
||||
|
||||
/** Один тред на чат: вопросы разных админов и ответы бота — общий диалог (шаблон Mastra для групповых чатов). */
|
||||
export const chatThreadId = (chatId: number): string => `chat:${chatId}`;
|
||||
|
||||
/** Обратно из ID треда в ID чата; undefined — если тред создан не ботом. */
|
||||
export const chatIdOfThread = (threadId: string): number | undefined => {
|
||||
const match = threadId.match(/^chat:(-?\d+)$/);
|
||||
|
||||
return match ? Number(match[1]) : undefined;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user