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