improvements

This commit is contained in:
2026-01-22 13:50:36 +03:00
parent a7efc1a778
commit e833c16878
3 changed files with 50 additions and 32 deletions
+4
View File
@@ -27,6 +27,8 @@ export class OllamaCancel extends CallbackCommand {
const aborted = abortOllamaRequest(uuid); const aborted = abortOllamaRequest(uuid);
console.log(`aborted request ${uuid}:`, aborted); console.log(`aborted request ${uuid}:`, aborted);
} else {
console.log(`no request with uuid "${uuid}" found`);
} }
let msg: StoredMessage | null = null; let msg: StoredMessage | null = null;
@@ -36,6 +38,8 @@ export class OllamaCancel extends CallbackCommand {
logError(e); logError(e);
} }
console.log(`Message for ${chatId}-${messageId}:`, msg);
let content: string | null = null; let content: string | null = null;
if (msg?.text?.trim()?.length > 0) { if (msg?.text?.trim()?.length > 0) {
+43 -28
View File
@@ -3,7 +3,6 @@ import {Message} from "typescript-telegram-bot-api";
import {abortOllamaRequest, bot, getOllamaRequest, ollama, ollamaRequests} from "../index"; import {abortOllamaRequest, bot, getOllamaRequest, ollama, ollamaRequests} from "../index";
import { import {
collectReplyChainText, collectReplyChainText,
editMessageText,
escapeMarkdownV2Text, escapeMarkdownV2Text,
extractText, extractText,
logError, logError,
@@ -50,7 +49,6 @@ export class OllamaChat extends ChatCommand {
const startTime = Date.now(); const startTime = Date.now();
try { try {
let isOver: boolean = false;
const uuid = crypto.randomUUID(); const uuid = crypto.randomUUID();
const cancelMarkup = {inline_keyboard: [[Cancel.withData(new OllamaCancel().data + " " + uuid).asButton()]]}; const cancelMarkup = {inline_keyboard: [[Cancel.withData(new OllamaCancel().data + " " + uuid).asButton()]]};
@@ -60,8 +58,7 @@ export class OllamaChat extends ChatCommand {
reply_parameters: { reply_parameters: {
chat_id: chatId, chat_id: chatId,
message_id: msg.message_id message_id: msg.message_id
}, }
reply_markup: cancelMarkup
}); });
const stream = await ollama.chat({ const stream = await ollama.chat({
@@ -72,7 +69,24 @@ export class OllamaChat extends ChatCommand {
messages: chatMessages, messages: chatMessages,
}); });
ollamaRequests.push({uuid: uuid, stream: stream, done: false, fromId: msg.from.id, chatId: msg.chat.id}); const newRequest = {
uuid: uuid,
stream: stream,
done: false,
fromId: msg.from.id,
chatId: msg.chat.id,
};
console.log("Pushing new request", newRequest);
ollamaRequests.push(newRequest);
await bot.editMessageReplyMarkup(
{
chat_id: chatId,
message_id: waitMessage.message_id,
reply_markup: cancelMarkup
}
).catch(logError);
let currentText = ""; let currentText = "";
let shouldBreak = false; let shouldBreak = false;
@@ -82,14 +96,18 @@ export class OllamaChat extends ChatCommand {
intervalMs: 4500, intervalMs: 4500,
getText: () => currentText, getText: () => currentText,
editFn: async (text) => { editFn: async (text) => {
if (getOllamaRequest(uuid)?.done) return;
try { try {
await editMessageText( await bot.editMessageText({
chatId, chat_id: chatId,
waitMessage.message_id, message_id: waitMessage.message_id,
escapeMarkdownV2Text(text), text: escapeMarkdownV2Text(text),
"Markdown", parse_mode: "Markdown",
isOver ? {inline_keyboard: []} : cancelMarkup reply_markup: cancelMarkup
); }).catch(logError);
console.log("editMessageText", text);
waitMessage.reply_to_message = msg; waitMessage.reply_to_message = msg;
waitMessage.text = text; waitMessage.text = text;
@@ -97,8 +115,6 @@ export class OllamaChat extends ChatCommand {
} catch (e) { } catch (e) {
logError(e); logError(e);
} }
},
onStop: async () => {
} }
}); });
await editor.tick(); await editor.tick();
@@ -111,13 +127,12 @@ export class OllamaChat extends ChatCommand {
if (content === "<think>" || chunk.message.thinking) { if (content === "<think>" || chunk.message.thinking) {
if (!isThinking) { if (!isThinking) {
await editMessageText( await bot.editMessageText({
chatId, chat_id: chatId,
waitMessage.message_id, message_id: waitMessage.message_id,
"🤔 Размышляю...", text: "🤔 Размышляю...",
"Markdown", parse_mode: "Markdown",
isOver ? {inline_keyboard: []} : cancelMarkup }).catch(logError);
).catch(logError);
} }
isThinking = true; isThinking = true;
@@ -145,8 +160,6 @@ export class OllamaChat extends ChatCommand {
} }
if (shouldBreak || chunk.done) { if (shouldBreak || chunk.done) {
isOver = true;
console.log("messageText", currentText); console.log("messageText", currentText);
console.log("length", currentText.length); console.log("length", currentText.length);
@@ -156,13 +169,13 @@ export class OllamaChat extends ChatCommand {
console.log("ended", true); console.log("ended", true);
} }
console.log(`aborted request ${uuid}:`, abortOllamaRequest(uuid));
const diff = Math.abs(Date.now() - startTime) / 1000; const diff = Math.abs(Date.now() - startTime) / 1000;
await editor.tick(); await editor.tick();
await editor.stop(); await editor.stop();
console.log(`aborted request ${uuid}:`, abortOllamaRequest(uuid));
waitMessage.reply_to_message = msg; waitMessage.reply_to_message = msg;
waitMessage.text = currentText; waitMessage.text = currentText;
await MessageStore.put(waitMessage); await MessageStore.put(waitMessage);
@@ -171,9 +184,11 @@ export class OllamaChat extends ChatCommand {
} }
} }
} finally { } finally {
console.log(`aborted request ${uuid}:`, abortOllamaRequest(uuid)); await bot.editMessageReplyMarkup({
await editor.tick(); chat_id: chatId,
await editor.stop(); message_id: waitMessage.message_id,
reply_markup: {inline_keyboard: []}
}).catch(logError);
} }
} catch (error) { } catch (error) {
if (error.message.toLowerCase().includes("aborted")) return; if (error.message.toLowerCase().includes("aborted")) return;
+3 -4
View File
@@ -4,7 +4,7 @@ import {CallbackCommand} from "../base/callback-command";
import {CallbackQuery, InlineKeyboardMarkup, Message, ParseMode, PhotoSize, User} from "typescript-telegram-bot-api"; import {CallbackQuery, InlineKeyboardMarkup, Message, ParseMode, PhotoSize, User} from "typescript-telegram-bot-api";
import {Environment} from "../common/environment"; import {Environment} from "../common/environment";
import {TelegramError} from "typescript-telegram-bot-api/dist/errors"; import {TelegramError} from "typescript-telegram-bot-api/dist/errors";
import {bot, botUser, getOllamaRequest, messageDao, setSystemInfo} from "../index"; import {bot, botUser, messageDao, setSystemInfo} from "../index";
import os from "os"; import os from "os";
import axios from "axios"; import axios from "axios";
import {MessagePart} from "../common/message-part"; import {MessagePart} from "../common/message-part";
@@ -750,7 +750,7 @@ export function startIntervalEditor(params: {
intervalMs: number; intervalMs: number;
getText: () => string; getText: () => string;
editFn: (text: string) => Promise<void>; editFn: (text: string) => Promise<void>;
onStop: () => Promise<void>; onStop?: () => Promise<void>;
}) { }) {
let lastSent = ""; let lastSent = "";
let stopped = false; let stopped = false;
@@ -778,8 +778,7 @@ export function startIntervalEditor(params: {
stop: async () => { stop: async () => {
stopped = true; stopped = true;
clearInterval(timer); clearInterval(timer);
await tick(); await params.onStop?.();
await params.onStop();
}, },
}; };
} }