update error logging
cutting all kind of prefixes for prompt making
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
import {CallbackQuery, InlineKeyboardButton} from "typescript-telegram-bot-api";
|
import {CallbackQuery, InlineKeyboardButton} from "typescript-telegram-bot-api";
|
||||||
import {Requirements} from "./requirements";
|
import {Requirements} from "./requirements";
|
||||||
import {bot} from "../index";
|
import {bot} from "../index";
|
||||||
|
import {logError} from "../util/utils";
|
||||||
|
|
||||||
export abstract class CallbackCommand {
|
export abstract class CallbackCommand {
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ export abstract class CallbackCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async answerCallbackQuery(query: CallbackQuery): Promise<void> {
|
async answerCallbackQuery(query: CallbackQuery): Promise<void> {
|
||||||
bot.answerCallbackQuery(this.getOptions(query)).catch(console.error);
|
bot.answerCallbackQuery(this.getOptions(query)).catch(logError);
|
||||||
}
|
}
|
||||||
|
|
||||||
asButton(): InlineKeyboardButton {
|
asButton(): InlineKeyboardButton {
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import {Requirements} from "../base/requirements";
|
|||||||
import {Requirement} from "../base/requirement";
|
import {Requirement} from "../base/requirement";
|
||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment";
|
||||||
|
|
||||||
const cancelledText = "```Ollama\n❌ Отменено```";
|
|
||||||
|
|
||||||
export class OllamaCancel extends CallbackCommand {
|
export class OllamaCancel extends CallbackCommand {
|
||||||
|
|
||||||
data = "/cancel_ollama";
|
data = "/cancel_ollama";
|
||||||
@@ -48,17 +46,27 @@ export class OllamaCancel extends CallbackCommand {
|
|||||||
|
|
||||||
if (msg?.text?.trim()?.length > 0) {
|
if (msg?.text?.trim()?.length > 0) {
|
||||||
content = msg?.text.trim();
|
content = msg?.text.trim();
|
||||||
if (content.length + cancelledText.length > 4096) {
|
if (content.length + Environment.ollamaCancelledText.length > 4096) {
|
||||||
content = content.substring(0, 4096 - cancelledText.length - 2) + "\n";
|
content = content.substring(0, 4096 - Environment.ollamaCancelledText.length - 2) + "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await bot.editMessageText({
|
const newText = `${content ? content : ""}${Environment.ollamaCancelledText}`;
|
||||||
chat_id: chatId,
|
|
||||||
message_id: messageId,
|
try {
|
||||||
text: `${content ? content : ""}${cancelledText}`,
|
await bot.editMessageText({
|
||||||
parse_mode: "Markdown",
|
chat_id: chatId,
|
||||||
reply_markup: {inline_keyboard: []},
|
message_id: messageId,
|
||||||
}).catch(logError);
|
text: newText,
|
||||||
|
parse_mode: "Markdown",
|
||||||
|
reply_markup: {inline_keyboard: []},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (msg) {
|
||||||
|
await MessageStore.put(msg);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
logError(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -29,7 +29,7 @@ export class Ae extends ChatCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.error(`${text}
|
logError(`${text}
|
||||||
* Stacktrace: ${e.stack}`);
|
* Stacktrace: ${e.stack}`);
|
||||||
|
|
||||||
await oldSendMessage(msg, text).catch(logError);
|
await oldSendMessage(msg, text).catch(logError);
|
||||||
|
|||||||
+10
-11
@@ -1,19 +1,18 @@
|
|||||||
import {ChatCommand} from "../base/chat-command";
|
import {ChatCommand} from "../base/chat-command";
|
||||||
import {Message} from "typescript-telegram-bot-api";
|
import {Message} from "typescript-telegram-bot-api";
|
||||||
import {
|
|
||||||
collectReplyChainText,
|
|
||||||
escapeMarkdownV2Text,
|
|
||||||
extractText,
|
|
||||||
logError,
|
|
||||||
oldReplyToMessage,
|
|
||||||
startIntervalEditor
|
|
||||||
} from "../util/utils";
|
|
||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment";
|
||||||
import {bot, googleAi} from "../index";
|
import {bot, googleAi} from "../index";
|
||||||
import {MessageStore} from "../common/message-store";
|
import {MessageStore} from "../common/message-store";
|
||||||
import {Requirements} from "../base/requirements";
|
import {Requirements} from "../base/requirements";
|
||||||
import {Requirement} from "../base/requirement";
|
import {Requirement} from "../base/requirement";
|
||||||
import {ApiError} from "@google/genai";
|
import {ApiError} from "@google/genai";
|
||||||
|
import {
|
||||||
|
collectReplyChainText,
|
||||||
|
escapeMarkdownV2Text,
|
||||||
|
logError,
|
||||||
|
oldReplyToMessage,
|
||||||
|
startIntervalEditor
|
||||||
|
} from "../util/utils";
|
||||||
|
|
||||||
export class GeminiChat extends ChatCommand {
|
export class GeminiChat extends ChatCommand {
|
||||||
command = "gemini";
|
command = "gemini";
|
||||||
@@ -34,13 +33,13 @@ export class GeminiChat extends ChatCommand {
|
|||||||
|
|
||||||
const chatId = msg.chat.id;
|
const chatId = msg.chat.id;
|
||||||
|
|
||||||
const messageParts = await collectReplyChainText(msg, "/gemini");
|
const messageParts = await collectReplyChainText(msg);
|
||||||
console.log("MESSAGE PARTS", messageParts);
|
console.log("MESSAGE PARTS", messageParts);
|
||||||
|
|
||||||
const chatMessages = messageParts.map(part => {
|
const chatMessages = messageParts.map(part => {
|
||||||
return {
|
return {
|
||||||
role: part.bot ? "assistant" : "user",
|
role: part.bot ? "assistant" : "user",
|
||||||
content: (Environment.USE_NAMES_IN_PROMPT && !part.bot ? `MESSAGE FROM USER "${part.name}":\n` : "") + extractText(part.content, "/gemini")
|
content: (Environment.USE_NAMES_IN_PROMPT && !part.bot ? `MESSAGE FROM USER "${part.name}":\n` : "") + part.content
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
chatMessages.reverse();
|
chatMessages.reverse();
|
||||||
@@ -134,7 +133,7 @@ export class GeminiChat extends ChatCommand {
|
|||||||
await oldReplyToMessage(waitMessage, `⏱️ ${diff}s`);
|
await oldReplyToMessage(waitMessage, `⏱️ ${diff}s`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
logError(error);
|
||||||
|
|
||||||
if (error instanceof ApiError) {
|
if (error instanceof ApiError) {
|
||||||
if (error.status === 429) {
|
if (error.status === 429) {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export class GeminiListModels extends ChatCommand {
|
|||||||
parse_mode: "HTML"
|
parse_mode: "HTML"
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
await replyToMessage({message: msg, text: "Не получилось загрузить список моделей"}).catch(logError);
|
await replyToMessage({message: msg, text: "Не получилось загрузить список моделей"}).catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {Message} from "typescript-telegram-bot-api";
|
|||||||
import {
|
import {
|
||||||
collectReplyChainText,
|
collectReplyChainText,
|
||||||
escapeMarkdownV2Text,
|
escapeMarkdownV2Text,
|
||||||
extractText,
|
|
||||||
logError,
|
logError,
|
||||||
oldReplyToMessage,
|
oldReplyToMessage,
|
||||||
startIntervalEditor
|
startIntervalEditor
|
||||||
@@ -34,14 +33,14 @@ export class MistralChat extends ChatCommand {
|
|||||||
|
|
||||||
const chatId = msg.chat.id;
|
const chatId = msg.chat.id;
|
||||||
|
|
||||||
const messageParts = await collectReplyChainText(msg, "/mistral");
|
const messageParts = await collectReplyChainText(msg);
|
||||||
console.log("MESSAGE PARTS", messageParts);
|
console.log("MESSAGE PARTS", messageParts);
|
||||||
|
|
||||||
const chatMessages = messageParts.map(part => {
|
const chatMessages = messageParts.map(part => {
|
||||||
const content = [];
|
const content = [];
|
||||||
content.push({
|
content.push({
|
||||||
type: "text",
|
type: "text",
|
||||||
text: (Environment.USE_NAMES_IN_PROMPT && !part.bot ? `MESSAGE FROM USER "${part.name}":\n` : "") + extractText(part.content, Environment.BOT_PREFIX),
|
text: (Environment.USE_NAMES_IN_PROMPT && !part.bot ? `MESSAGE FROM USER "${part.name}":\n` : "") + part.content,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (part.images && part.images.length > 0) {
|
if (part.images && part.images.length > 0) {
|
||||||
@@ -143,7 +142,7 @@ export class MistralChat extends ChatCommand {
|
|||||||
await oldReplyToMessage(waitMessage, `⏱️ ${diff}s`);
|
await oldReplyToMessage(waitMessage, `⏱️ ${diff}s`);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
logError(error);
|
||||||
await oldReplyToMessage(waitMessage, `Произошла ошибка!\n${error.toString()}`).catch(logError);
|
await oldReplyToMessage(waitMessage, `Произошла ошибка!\n${error.toString()}`).catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export class MistralListModels extends ChatCommand {
|
|||||||
parse_mode: "HTML"
|
parse_mode: "HTML"
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
await oldReplyToMessage(msg, "Не получилось загрузить список моделей").catch(logError);
|
await oldReplyToMessage(msg, "Не получилось загрузить список моделей").catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import {abortOllamaRequest, bot, getOllamaRequest, ollama, ollamaRequests} from
|
|||||||
import {
|
import {
|
||||||
collectReplyChainText,
|
collectReplyChainText,
|
||||||
escapeMarkdownV2Text,
|
escapeMarkdownV2Text,
|
||||||
extractText,
|
|
||||||
logError,
|
logError,
|
||||||
oldReplyToMessage,
|
oldReplyToMessage,
|
||||||
startIntervalEditor
|
startIntervalEditor
|
||||||
@@ -37,7 +36,7 @@ export class OllamaChat extends ChatCommand {
|
|||||||
const chatMessages = messageParts.map(part => {
|
const chatMessages = messageParts.map(part => {
|
||||||
return {
|
return {
|
||||||
role: part.bot ? "assistant" : "user",
|
role: part.bot ? "assistant" : "user",
|
||||||
content: (Environment.USE_NAMES_IN_PROMPT && !part.bot ? `MESSAGE FROM USER "${part.name}":\n` : "") + extractText(part.content, Environment.BOT_PREFIX),
|
content: (Environment.USE_NAMES_IN_PROMPT && !part.bot ? `MESSAGE FROM USER "${part.name}":\n` : "") + part.content,
|
||||||
images: part.images
|
images: part.images
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -198,7 +197,7 @@ export class OllamaChat extends ChatCommand {
|
|||||||
reply_markup: {inline_keyboard: []}
|
reply_markup: {inline_keyboard: []}
|
||||||
}).catch(logError);
|
}).catch(logError);
|
||||||
|
|
||||||
console.error(error);
|
logError(error);
|
||||||
await oldReplyToMessage(waitMessage, `Произошла ошибка!\n${error.toString()}`).catch(logError);
|
await oldReplyToMessage(waitMessage, `Произошла ошибка!\n${error.toString()}`).catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export class OllamaListModels extends ChatCommand {
|
|||||||
parse_mode: "HTML"
|
parse_mode: "HTML"
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
await oldReplyToMessage(msg, "Не получилось загрузить список моделей").catch(logError);
|
await oldReplyToMessage(msg, "Не получилось загрузить список моделей").catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ export class OllamaPrompt extends ChatCommand {
|
|||||||
reply_markup: {inline_keyboard: []}
|
reply_markup: {inline_keyboard: []}
|
||||||
}).catch(logError);
|
}).catch(logError);
|
||||||
|
|
||||||
console.error(error);
|
logError(error);
|
||||||
await oldReplyToMessage(waitMessage, `Произошла ошибка!\n${error.toString()}`).catch(logError);
|
await oldReplyToMessage(waitMessage, `Произошла ошибка!\n${error.toString()}`).catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {Requirement} from "../base/requirement";
|
|||||||
import {Message} from "typescript-telegram-bot-api";
|
import {Message} from "typescript-telegram-bot-api";
|
||||||
import {bot, ollama} from "../index";
|
import {bot, ollama} from "../index";
|
||||||
import {WebSearchResponse} from "../model/web-search-response";
|
import {WebSearchResponse} from "../model/web-search-response";
|
||||||
import {editMessageText} from "../util/utils";
|
import {editMessageText, logError} from "../util/utils";
|
||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment";
|
||||||
|
|
||||||
export class OllamaSearch extends ChatCommand {
|
export class OllamaSearch extends ChatCommand {
|
||||||
@@ -42,7 +42,7 @@ export class OllamaSearch extends ChatCommand {
|
|||||||
|
|
||||||
await editMessageText(chatId, wait.message_id, message);
|
await editMessageText(chatId, wait.message_id, message);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
logError(error);
|
||||||
}
|
}
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ try {
|
|||||||
GlobalFonts.registerFromPath("./assets/JetBrainsMono-Italic.ttf", "JetBrainsMonoItalic");
|
GlobalFonts.registerFromPath("./assets/JetBrainsMono-Italic.ttf", "JetBrainsMonoItalic");
|
||||||
GlobalFonts.registerFromPath("./assets/JetBrainsMono-Regular.ttf", "JetBrainsMonoRegular");
|
GlobalFonts.registerFromPath("./assets/JetBrainsMono-Regular.ttf", "JetBrainsMonoRegular");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Quote extends ChatCommand {
|
export class Quote extends ChatCommand {
|
||||||
@@ -75,7 +75,7 @@ export class Quote extends ChatCommand {
|
|||||||
},
|
},
|
||||||
}).catch(logError);
|
}).catch(logError);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
await oldSendMessage(msg, "Не смог собрать цитату 😢").catch(logError);
|
await oldSendMessage(msg, "Не смог собрать цитату 😢").catch(logError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ export class Environment {
|
|||||||
static MISTRAL_MODEL: string;
|
static MISTRAL_MODEL: string;
|
||||||
|
|
||||||
static waitText = "⏳ Дайте-ка подумать...";
|
static waitText = "⏳ Дайте-ка подумать...";
|
||||||
|
static ollamaCancelledText = "```Ollama\n❌ Отменено```";
|
||||||
|
|
||||||
static load() {
|
static load() {
|
||||||
Environment.BOT_TOKEN = process.env.BOT_TOKEN;
|
Environment.BOT_TOKEN = process.env.BOT_TOKEN;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import {StoredMessage} from "../model/stored-message";
|
import {StoredMessage} from "../model/stored-message";
|
||||||
import {Message} from "typescript-telegram-bot-api";
|
import {Message} from "typescript-telegram-bot-api";
|
||||||
import {extractTextMessage} from "../util/utils";
|
import {extractTextMessage, isStoredMessage} from "../util/utils";
|
||||||
import {Environment} from "./environment";
|
|
||||||
import {messageDao} from "../index";
|
import {messageDao} from "../index";
|
||||||
|
|
||||||
export class MessageStore {
|
export class MessageStore {
|
||||||
@@ -15,19 +14,18 @@ export class MessageStore {
|
|||||||
return this.map;
|
return this.map;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async put(m: Message, prefix: string = Environment.BOT_PREFIX) {
|
static async put(m: Message | StoredMessage) {
|
||||||
const msg: StoredMessage = {
|
const msg: StoredMessage = isStoredMessage(m) ? m : {
|
||||||
chatId: m.chat.id,
|
chatId: m.chat.id,
|
||||||
id: m.message_id,
|
id: m.message_id,
|
||||||
replyToMessageId: m.reply_to_message?.message_id ?? null,
|
replyToMessageId: m.reply_to_message?.message_id ?? null,
|
||||||
fromId: m.from.id,
|
fromId: m.from.id,
|
||||||
text: extractTextMessage(m, prefix),
|
text: extractTextMessage(m),
|
||||||
date: m.date ?? 0,
|
date: m.date ?? 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.map.set(this.key(m.chat.id, m.message_id), msg);
|
this.map.set(this.key(msg.chatId, msg.id), msg);
|
||||||
|
await messageDao.insert(messageDao.mapStoredTo([msg]));
|
||||||
await messageDao.insert(messageDao.mapTo([m]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async get(chatId: number, messageId: number): Promise<StoredMessage | null> {
|
static async get(chatId: number, messageId: number): Promise<StoredMessage | null> {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import {drizzle, LibSQLDatabase} from "drizzle-orm/libsql";
|
import {drizzle, LibSQLDatabase} from "drizzle-orm/libsql";
|
||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment";
|
||||||
|
import {logError} from "../util/utils";
|
||||||
|
|
||||||
export class DatabaseManager {
|
export class DatabaseManager {
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ export class DatabaseManager {
|
|||||||
try {
|
try {
|
||||||
DatabaseManager.db = drizzle(Environment.DB_PATH);
|
DatabaseManager.db = drizzle(Environment.DB_PATH);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+3
-2
@@ -1,5 +1,6 @@
|
|||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import {Environment} from "../common/environment";
|
import {Environment} from "../common/environment";
|
||||||
|
import {logError} from "../util/utils";
|
||||||
|
|
||||||
|
|
||||||
export let muted: Set<number> = new Set<number>();
|
export let muted: Set<number> = new Set<number>();
|
||||||
@@ -57,7 +58,7 @@ export async function readData(): Promise<void> {
|
|||||||
|
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
return Promise.reject(e);
|
return Promise.reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,7 +92,7 @@ export async function retrieveAnswers(): Promise<void> {
|
|||||||
json.day.forEach(e => dayAnswers.push(e));
|
json.day.forEach(e => dayAnswers.push(e));
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
return Promise.reject(e);
|
return Promise.reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,8 @@ import {DatabaseManager} from "./database-manager";
|
|||||||
import {StoredMessage} from "../model/stored-message";
|
import {StoredMessage} from "../model/stored-message";
|
||||||
import {and, eq} from "drizzle-orm";
|
import {and, eq} from "drizzle-orm";
|
||||||
import {inArray} from "drizzle-orm/sql/expressions/conditions";
|
import {inArray} from "drizzle-orm/sql/expressions/conditions";
|
||||||
import {Message} from "typescript-telegram-bot-api";
|
|
||||||
import {Dao} from "../base/dao";
|
import {Dao} from "../base/dao";
|
||||||
import {buildExcludedSet, extractTextMessage} from "../util/utils";
|
import {buildExcludedSet} from "../util/utils";
|
||||||
import {Environment} from "../common/environment";
|
|
||||||
|
|
||||||
export class MessageDao extends Dao<StoredMessage> {
|
export class MessageDao extends Dao<StoredMessage> {
|
||||||
|
|
||||||
@@ -82,14 +80,14 @@ export class MessageDao extends Dao<StoredMessage> {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
mapTo(messages: Message[]): MessageInsert[] {
|
mapStoredTo(messages: StoredMessage[]): MessageInsert[] {
|
||||||
return messages.map(msg => {
|
return messages.map(msg => {
|
||||||
return {
|
return {
|
||||||
chatId: msg.chat.id,
|
chatId: msg.chatId,
|
||||||
id: msg.message_id,
|
id: msg.id,
|
||||||
replyToMessageId: msg.reply_to_message?.message_id,
|
replyToMessageId: msg.replyToMessageId,
|
||||||
fromId: msg.from.id,
|
fromId: msg.fromId,
|
||||||
text: extractTextMessage(msg, Environment.BOT_PREFIX),
|
text: msg.text,
|
||||||
date: msg.date,
|
date: msg.date,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
+5
-5
@@ -71,7 +71,7 @@ import {GeminiGetModel} from "./commands/gemini-get-model";
|
|||||||
import {GeminiSetModel} from "./commands/gemini-set-model";
|
import {GeminiSetModel} from "./commands/gemini-set-model";
|
||||||
import {Debug} from "./commands/debug";
|
import {Debug} from "./commands/debug";
|
||||||
|
|
||||||
process.setUncaughtExceptionCaptureCallback(console.error);
|
process.setUncaughtExceptionCaptureCallback(logError);
|
||||||
|
|
||||||
Environment.load();
|
Environment.load();
|
||||||
DatabaseManager.init();
|
DatabaseManager.init();
|
||||||
@@ -112,7 +112,7 @@ export function abortOllamaRequest(uuid: string): boolean {
|
|||||||
updateOllamaRequest(uuid, {...request, done: true});
|
updateOllamaRequest(uuid, {...request, done: true});
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ async function main() {
|
|||||||
|
|
||||||
console.log("Bot started!");
|
console.log("Bot started!");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
logError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,7 +336,7 @@ bot.on("inline_query", async (query) => {
|
|||||||
results: queryResults,
|
results: queryResults,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await bot.answerInlineQuery({
|
await bot.answerInlineQuery({
|
||||||
@@ -351,4 +351,4 @@ bot.on("callback_query", async (query) => {
|
|||||||
await findAndExecuteCallbackCommand(callbackCommands, query);
|
await findAndExecuteCallbackCommand(callbackCommands, query);
|
||||||
});
|
});
|
||||||
|
|
||||||
main().catch(console.error);
|
main().catch(logError);
|
||||||
+38
-36
@@ -40,7 +40,7 @@ export const ignoreIfMarkupFailed = (e: Error | TelegramError) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const logError = (e: Error | TelegramError) => {
|
export const logError = (e: Error | TelegramError | string) => {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ export async function editMessageText(chatId: number, messageId: number, message
|
|||||||
}).catch(ignoreIfMarkupFailed);
|
}).catch(ignoreIfMarkupFailed);
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
|
|
||||||
if (e instanceof TelegramError && e.response.description.includes("Too Many Requests")) {
|
if (e instanceof TelegramError && e.response.description.includes("Too Many Requests")) {
|
||||||
const delay = Number(e.message.split("retry after ")[1]) || 30;
|
const delay = Number(e.message.split("retry after ")[1]) || 30;
|
||||||
@@ -301,7 +301,7 @@ export async function oldReplyToMessage(message: Message, text: string, parseMod
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function sendErrorPlaceholder(message: Message): Promise<Message> {
|
export async function sendErrorPlaceholder(message: Message): Promise<Message> {
|
||||||
return await sendMessage({message: message, text: "Произошла ошибка ⚠️"}).catch(console.error) as Message;
|
return await sendMessage({message: message, text: "Произошла ошибка ⚠️"}).catch(logError) as Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initSystemSpecs(): Promise<void> {
|
export async function initSystemSpecs(): Promise<void> {
|
||||||
@@ -446,34 +446,32 @@ export async function getUserAvatar(userId: number): Promise<Buffer | null> {
|
|||||||
return Buffer.from(res.data);
|
return Buffer.from(res.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractTextMessage(msg: Message, prefix: string = ""): string | null {
|
export function extractTextMessage(msg: Message | StoredMessage | string): string | null {
|
||||||
let text = (msg?.text ?? msg?.caption ?? "").trim();
|
const text = (typeof msg === "string" ? msg : isStoredMessage(msg) ? msg.text : msg?.text ?? msg?.caption ?? "").trim();
|
||||||
if (text.toLowerCase().startsWith(prefix.toLowerCase())) {
|
|
||||||
text = text.substring(prefix.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
text = text.trim();
|
|
||||||
if (text.length === 0) return null;
|
if (text.length === 0) return null;
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractTextStored(msg: StoredMessage, prefix: string): string {
|
export function cutPrefixes(msg: Message | StoredMessage | string): string {
|
||||||
let text = (msg?.text ?? "").trim();
|
const prefixes = [
|
||||||
if (text.toLowerCase().startsWith(prefix.toLowerCase())) {
|
Environment.BOT_PREFIX,
|
||||||
text = text.substring(prefix.length).trim();
|
`/gemini@${botUser.username}`,
|
||||||
|
"/gemini",
|
||||||
|
`/mistral@${botUser.username}`,
|
||||||
|
"/mistral"
|
||||||
|
];
|
||||||
|
|
||||||
|
const text = extractTextMessage(msg);
|
||||||
|
let newText = text;
|
||||||
|
|
||||||
|
for (const prefix of prefixes) {
|
||||||
|
if (text.toLowerCase().startsWith(prefix.toLowerCase())) {
|
||||||
|
newText = newText.substring(prefix.length).trim();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return newText;
|
||||||
}
|
|
||||||
|
|
||||||
export function extractText(text: string, prefix: string): string {
|
|
||||||
if (!text) return "";
|
|
||||||
if (text.toLowerCase().startsWith(prefix.toLowerCase())) {
|
|
||||||
text = text.substring(prefix.length).trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isStoredMessage(msg: Message | StoredMessage): msg is StoredMessage {
|
export function isStoredMessage(msg: Message | StoredMessage): msg is StoredMessage {
|
||||||
@@ -502,7 +500,7 @@ export async function loadImageIfExists(msg: Message | StoredMessage): Promise<s
|
|||||||
try {
|
try {
|
||||||
fs.writeFileSync(imageFilePath, src);
|
fs.writeFileSync(imageFilePath, src);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
logError(e);
|
||||||
imageFilePath = null;
|
imageFilePath = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -511,18 +509,18 @@ export async function loadImageIfExists(msg: Message | StoredMessage): Promise<s
|
|||||||
return imageFilePath;
|
return imageFilePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function collectReplyChainText(triggerMsg: Message, prefix: string = Environment.BOT_PREFIX, limit: number = 40, includeTrigger = true): Promise<MessagePart[]> {
|
export async function collectReplyChainText(triggerMsg: Message, limit: number = 40, includeTrigger = true, cutPrefix: boolean = true): Promise<MessagePart[]> {
|
||||||
const chatId = triggerMsg.chat.id as number;
|
const chatId = triggerMsg.chat.id as number;
|
||||||
|
|
||||||
const parts: MessagePart[] = [];
|
const parts: MessagePart[] = [];
|
||||||
if (includeTrigger) {
|
if (includeTrigger) {
|
||||||
const t = extractTextMessage(triggerMsg, prefix);
|
const t = extractTextMessage(triggerMsg);
|
||||||
const img = (await loadImageIfExists(triggerMsg)) /*|| triggerMsg.reply_to_message ?
|
const text = cutPrefix ? cutPrefixes(t) : t;
|
||||||
(await loadImageIfExists(triggerMsg.reply_to_message)) : null*/;
|
const img = await loadImageIfExists(triggerMsg);
|
||||||
if (t) {
|
if (text) {
|
||||||
parts.push({
|
parts.push({
|
||||||
bot: triggerMsg.from.id === botUser.id,
|
bot: triggerMsg.from.id === botUser.id,
|
||||||
content: t,
|
content: text,
|
||||||
name: triggerMsg.from.first_name,
|
name: triggerMsg.from.first_name,
|
||||||
images: img ? [img] : []
|
images: img ? [img] : []
|
||||||
});
|
});
|
||||||
@@ -534,7 +532,8 @@ export async function collectReplyChainText(triggerMsg: Message, prefix: string
|
|||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
const firstText = extractTextMessage(first, prefix);
|
const ft = extractTextMessage(first);
|
||||||
|
const firstText = cutPrefix ? cutPrefixes(ft) : ft;
|
||||||
if (firstText || first.photo) {
|
if (firstText || first.photo) {
|
||||||
const img = await loadImageIfExists(first);
|
const img = await loadImageIfExists(first);
|
||||||
parts.push({
|
parts.push({
|
||||||
@@ -553,14 +552,17 @@ export async function collectReplyChainText(triggerMsg: Message, prefix: string
|
|||||||
if (!parentId) break;
|
if (!parentId) break;
|
||||||
|
|
||||||
const parent = await messageDao.getById({chatId: chatId, id: parentId});
|
const parent = await messageDao.getById({chatId: chatId, id: parentId});
|
||||||
if (!parent?.text && !parent?.photoMaxSizeFilePath) break;
|
const pt = extractTextMessage(parent);
|
||||||
|
const parentText = cutPrefix ? cutPrefixes(pt) : pt;
|
||||||
|
|
||||||
|
if (!parentText && !parent?.photoMaxSizeFilePath) break;
|
||||||
|
|
||||||
const user = await UserStore.get(parent.fromId);
|
const user = await UserStore.get(parent.fromId);
|
||||||
const img = await loadImageIfExists(parent);
|
const img = await loadImageIfExists(parent);
|
||||||
|
|
||||||
parts.push({
|
parts.push({
|
||||||
bot: parent.fromId === botUser.id,
|
bot: parent.fromId === botUser.id,
|
||||||
content: extractTextStored(parent, prefix),
|
content: parentText,
|
||||||
name: user?.firstName,
|
name: user?.firstName,
|
||||||
images: img ? [img] : []
|
images: img ? [img] : []
|
||||||
});
|
});
|
||||||
@@ -829,7 +831,7 @@ export function startIntervalEditor(params: {
|
|||||||
lastSent = next;
|
lastSent = next;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if ((e?.description ?? e?.message ?? "").includes("message is not modified")) return;
|
if ((e?.description ?? e?.message ?? "").includes("message is not modified")) return;
|
||||||
console.error("edit failed:", e);
|
logError("edit failed: " + e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user