From 12d95911092cb3fd495ed609e8570d8b82f99467 Mon Sep 17 00:00:00 2001 From: Danil Nikolaev Date: Wed, 28 Jan 2026 17:59:53 +0300 Subject: [PATCH] some improvements for prompt making --- src/commands/ollama-chat.ts | 9 ++++- src/common/environment.ts | 2 + src/db/message-dao.ts | 3 +- src/model/stored-message.ts | 4 +- src/util/utils.ts | 75 ++++++++++++++++--------------------- 5 files changed, 47 insertions(+), 46 deletions(-) diff --git a/src/commands/ollama-chat.ts b/src/commands/ollama-chat.ts index af70245..863d183 100644 --- a/src/commands/ollama-chat.ts +++ b/src/commands/ollama-chat.ts @@ -48,12 +48,19 @@ export class OllamaChat extends ChatCommand { const startTime = Date.now(); try { + const imagesCount = chatMessages.reduce((total, curr) => { + return total + (curr.images?.length ?? 0); + }, 0); + const uuid = crypto.randomUUID(); const cancelMarkup = {inline_keyboard: [[Cancel.withData(new OllamaCancel().data + " " + uuid).asButton()]]}; waitMessage = await bot.sendMessage({ chat_id: chatId, - text: Environment.waitText, + text: imagesCount ? + imagesCount > 1 ? Environment.analyzingPicturesText : Environment.analyzingPictureText + : Environment.waitText, + reply_parameters: { chat_id: chatId, message_id: msg.message_id diff --git a/src/common/environment.ts b/src/common/environment.ts index 58b9cbc..0be2790 100644 --- a/src/common/environment.ts +++ b/src/common/environment.ts @@ -36,6 +36,8 @@ export class Environment { static MISTRAL_MODEL: string; static waitText = "⏳ Дайте-ка подумать..."; + static analyzingPictureText = "🔍 Внимательно изучаю изображение..."; + static analyzingPicturesText = "🔍 Внимательно изучаю изображения..."; static ollamaCancelledText = "```Ollama\n❌ Отменено```"; static load() { diff --git a/src/db/message-dao.ts b/src/db/message-dao.ts index 458e2e4..2bf7768 100644 --- a/src/db/message-dao.ts +++ b/src/db/message-dao.ts @@ -89,6 +89,7 @@ export class MessageDao extends Dao { fromId: msg.fromId, text: msg.text, date: msg.date, + photoMaxSizeFilePath: msg.photoMaxSizeFilePath?.join(";"), }; }); } @@ -102,7 +103,7 @@ export class MessageDao extends Dao { fromId: m.fromId, text: m.text, date: m.date, - photoMaxSizeFilePath: m.photoMaxSizeFilePath + photoMaxSizeFilePath: m.photoMaxSizeFilePath?.split(";") }; }); } diff --git a/src/model/stored-message.ts b/src/model/stored-message.ts index 79bf9e1..6d06abf 100644 --- a/src/model/stored-message.ts +++ b/src/model/stored-message.ts @@ -1,9 +1,9 @@ export type StoredMessage = { chatId: number; id: number; - replyToMessageId?: number | null; + replyToMessageId?: number; fromId: number; text?: string; date: number; - photoMaxSizeFilePath?: string | null; + photoMaxSizeFilePath?: string[]; }; \ No newline at end of file diff --git a/src/util/utils.ts b/src/util/utils.ts index a62b93d..1454258 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -478,12 +478,12 @@ export function isStoredMessage(msg: Message | StoredMessage): msg is StoredMess return "id" in msg; } -export async function loadImageIfExists(msg: Message | StoredMessage): Promise { +export async function loadImagesIfExists(msg: Message | StoredMessage): Promise { if (isStoredMessage(msg)) { return msg.photoMaxSizeFilePath; } - let imageFilePath: string | null = null; + const imageFilePaths: string[] = []; const maxSize = await getPhotoMaxSize(msg.photo); if (maxSize) { @@ -492,7 +492,7 @@ export async function loadImageIfExists(msg: Message | StoredMessage): Promise(maxSize.url, {responseType: "arraybuffer"}); const src = Buffer.from(res.data); @@ -504,45 +504,49 @@ export async function loadImageIfExists(msg: Message | StoredMessage): Promise { + const parts: MessagePart[] = []; + + const pushPart = async (msg: Message | StoredMessage, textRequired: boolean = false) => { + const rawText = extractTextMessage(msg); + const cleanText = cutPrefix ? cutPrefixes(rawText) : rawText; + const images = await loadImagesIfExists(msg); + + if (!cleanText && textRequired) return; + if (!cleanText && !images?.length) return; + + const fromId = isStoredMessage(msg) ? msg.fromId : msg.from.id; + const firstName = isStoredMessage(msg) ? + (await UserStore.get(msg.fromId))?.firstName : msg.from.first_name; + + parts.push({ + bot: fromId === botUser.id, + content: cleanText ? cleanText : "", + name: firstName, + images: images ? images : [] + }); + }; + const chatId = triggerMsg.chat.id as number; - const parts: MessagePart[] = []; if (includeTrigger) { - const t = extractTextMessage(triggerMsg); - const text = cutPrefix ? cutPrefixes(t) : t; - const img = await loadImageIfExists(triggerMsg); - if (text) { - parts.push({ - bot: triggerMsg.from.id === botUser.id, - content: text, - name: triggerMsg.from.first_name, - images: img ? [img] : [] - }); - } + await pushPart(triggerMsg); } const first = triggerMsg.reply_to_message; if (!first) { return parts; } - - const ft = extractTextMessage(first); - const firstText = cutPrefix ? cutPrefixes(ft) : ft; - if (firstText || first.photo) { - const img = await loadImageIfExists(first); - parts.push({ - bot: first.from.id === botUser.id, - content: firstText, - name: first.from.first_name, - images: img ? [img] : [] - }); - } + await pushPart(first, false); let curId = first.message_id; @@ -552,20 +556,7 @@ export async function collectReplyChainText(triggerMsg: Message, limit: number = if (!parentId) break; const parent = await messageDao.getById({chatId: chatId, id: parentId}); - const pt = extractTextMessage(parent); - const parentText = cutPrefix ? cutPrefixes(pt) : pt; - - if (!parentText && !parent?.photoMaxSizeFilePath) break; - - const user = await UserStore.get(parent.fromId); - const img = await loadImageIfExists(parent); - - parts.push({ - bot: parent.fromId === botUser.id, - content: parentText, - name: user?.firstName, - images: img ? [img] : [] - }); + await pushPart(parent, false); curId = parentId; }