improve replies, formatting and system prompt handling

This commit is contained in:
2026-04-29 19:13:28 +03:00
parent 27ca75ed7c
commit d41a4ed3ea
8 changed files with 258 additions and 17 deletions
+4 -1
View File
@@ -44,7 +44,10 @@ export class GeminiChat extends ChatCommand {
}; };
}); });
chatMessages.reverse(); chatMessages.reverse();
chatMessages.unshift({role: "system", content: Environment.SYSTEM_PROMPT});
if (Environment.SYSTEM_PROMPT) {
chatMessages.unshift({role: "system", content: Environment.SYSTEM_PROMPT});
}
let chatContent = ""; let chatContent = "";
for (const part of chatMessages) { for (const part of chatMessages) {
+5 -2
View File
@@ -58,7 +58,10 @@ export class MistralChat extends ChatCommand {
}; };
}); });
chatMessages.reverse(); chatMessages.reverse();
chatMessages.unshift({role: "system", content: [{type: "text", text: Environment.SYSTEM_PROMPT}]});
if (Environment.SYSTEM_PROMPT) {
chatMessages.unshift({role: "system", content: [{type: "text", text: Environment.SYSTEM_PROMPT}]});
}
let waitMessage: Message; let waitMessage: Message;
@@ -116,7 +119,7 @@ export class MistralChat extends ChatCommand {
chat_id: chatId, chat_id: chatId,
message_id: waitMessage.message_id, message_id: waitMessage.message_id,
text: escapeMarkdownV2Text(text), text: escapeMarkdownV2Text(text),
parse_mode: "Markdown" parse_mode: "MarkdownV2"
} }
).catch(logError); ).catch(logError);
+4 -1
View File
@@ -44,7 +44,10 @@ export class OllamaChat extends ChatCommand {
}; };
}); });
chatMessages.reverse(); chatMessages.reverse();
chatMessages.unshift({role: "system", content: Environment.SYSTEM_PROMPT, images: []});
if (Environment.SYSTEM_PROMPT) {
chatMessages.unshift({role: "system", content: Environment.SYSTEM_PROMPT, images: []});
}
let waitMessage: Message; let waitMessage: Message;
+8 -5
View File
@@ -58,11 +58,14 @@ export class OpenAIChat extends ChatCommand {
}; };
}); });
chatMessages.reverse(); chatMessages.reverse();
chatMessages.unshift({
role: "system", if (Environment.SYSTEM_PROMPT) {
content: [{type: "input_text", text: Environment.SYSTEM_PROMPT}], chatMessages.unshift({
type: "message" role: "system",
}); content: [{type: "input_text", text: Environment.SYSTEM_PROMPT}],
type: "message"
});
}
let waitMessage: Message; let waitMessage: Message;
+4 -2
View File
@@ -112,8 +112,6 @@ export class Environment {
Environment.IMAGE_HANDLE_FALLBACK_POLICY = ImageHandleFallbackPolicy.NOTIFY_USER; Environment.IMAGE_HANDLE_FALLBACK_POLICY = ImageHandleFallbackPolicy.NOTIFY_USER;
} }
Environment.SYSTEM_PROMPT = process.env.SYSTEM_PROMPT?.trim();
Environment.OLLAMA_ADDRESS = process.env.OLLAMA_ADDRESS; Environment.OLLAMA_ADDRESS = process.env.OLLAMA_ADDRESS;
Environment.OLLAMA_MODEL = process.env.OLLAMA_MODEL || "gemma3:4b"; Environment.OLLAMA_MODEL = process.env.OLLAMA_MODEL || "gemma3:4b";
Environment.OLLAMA_IMAGE_MODEL = process.env.OLLAMA_IMAGE_MODEL || Environment.OLLAMA_MODEL; Environment.OLLAMA_IMAGE_MODEL = process.env.OLLAMA_IMAGE_MODEL || Environment.OLLAMA_MODEL;
@@ -133,6 +131,10 @@ export class Environment {
Environment.OPENAI_IMAGE_MODEL = process.env.OPENAI_IMAGE_MODEL || "gpt-image-1-mini"; Environment.OPENAI_IMAGE_MODEL = process.env.OPENAI_IMAGE_MODEL || "gpt-image-1-mini";
} }
static setSystemPrompt(prompt: string) {
this.SYSTEM_PROMPT = prompt;
}
static setAdmins(admins: Set<number>) { static setAdmins(admins: Set<number>) {
this.ADMIN_IDS = admins; this.ADMIN_IDS = admins;
} }
+14
View File
@@ -2,6 +2,7 @@ import * as fs from "fs";
import {Environment} from "../common/environment"; import {Environment} from "../common/environment";
import {logError} from "../util/utils"; import {logError} from "../util/utils";
import {Answers} from "../model/answers"; import {Answers} from "../model/answers";
import path from "node:path";
type DataJsonFile = { type DataJsonFile = {
admins: number[] admins: number[]
@@ -27,6 +28,19 @@ export async function readData(): Promise<void> {
} }
} }
export async function readPrompts(): Promise<void> {
try {
const prompt = fs.readFileSync(path.join(Environment.DATA_PATH, "system_prompt.txt")).toString().trim();
if (prompt.length) {
Environment.setSystemPrompt(prompt);
}
} catch (e) {
logError(e);
}
return Promise.resolve();
}
export async function saveData(): Promise<void> { export async function saveData(): Promise<void> {
const adminIds: number[] = []; const adminIds: number[] = [];
Environment.ADMIN_IDS.forEach(id => adminIds.push(id)); Environment.ADMIN_IDS.forEach(id => adminIds.push(id));
+5 -1
View File
@@ -20,7 +20,7 @@ import {Ping} from "./commands/ping";
import {RandomString} from "./commands/random-string"; import {RandomString} from "./commands/random-string";
import {SystemInfo} from "./commands/system-info"; import {SystemInfo} from "./commands/system-info";
import {Test} from "./commands/test"; import {Test} from "./commands/test";
import {readData, retrieveAnswers} from "./db/database"; import {readData, readPrompts, retrieveAnswers} from "./db/database";
import {Uptime} from "./commands/uptime"; import {Uptime} from "./commands/uptime";
import {WhatBetter} from "./commands/what-better"; import {WhatBetter} from "./commands/what-better";
import {When} from "./commands/when"; import {When} from "./commands/when";
@@ -252,6 +252,10 @@ async function shutdown(signal: NodeJS.Signals) {
async function main() { async function main() {
const start = Date.now(); const start = Date.now();
await readPrompts();
console.log(Environment.SYSTEM_PROMPT);
console.log( console.log(
`TEST_ENVIRONMENT: ${Environment.TEST_ENVIRONMENT}\n` + `TEST_ENVIRONMENT: ${Environment.TEST_ENVIRONMENT}\n` +
`DATA_PATH: ${Environment.DATA_PATH}\n` + `DATA_PATH: ${Environment.DATA_PATH}\n` +
+214 -5
View File
@@ -452,12 +452,221 @@ export const delay = (ms: number, signal?: AbortSignal): Promise<void> =>
} }
}); });
export function escapeMarkdownV2Text(s: string) { const MARKDOWN_V2_RESERVED_RE = /([\\_*\[\]()~`>#+\-=|{}.!])/g;
s = s.replace(/^\*{3,}\s*$/gm, "— — —");
s = s.replace(/^\*\s+(?=\S)/gm, "• ");
s = s.replace(/\*\*(.+?)\*\*/g, "*$1*");
return s; function escapePlainMarkdownV2(s: string): string {
return s.replace(MARKDOWN_V2_RESERVED_RE, "\\$1");
}
function escapeCodeMarkdownV2(s: string): string {
return s.replace(/[\\`]/g, "\\$&");
}
function escapeLinkUrlMarkdownV2(s: string): string {
return s.replace(/[\\)]/g, "\\$&");
}
function escapeMarkdownV2PreservingAllowedFormatting(s: string): string {
let result = "";
let i = 0;
while (i < s.length) {
// links: [text](url)
if (s[i] === "[") {
const linkMatch = s.slice(i).match(/^\[([^\]\n]+)]\(([^)\n]+)\)/);
if (linkMatch) {
const [, text, url] = linkMatch;
result += `[${escapePlainMarkdownV2(text)}](${escapeLinkUrlMarkdownV2(url)})`;
i += linkMatch[0].length;
continue;
}
}
// monospace: `text`
if (s[i] === "`") {
const end = s.indexOf("`", i + 1);
if (end !== -1) {
const content = s.slice(i + 1, end);
result += "`" + escapeCodeMarkdownV2(content) + "`";
i = end + 1;
continue;
}
}
// spoiler: ||text||
if (s.startsWith("||", i)) {
const end = s.indexOf("||", i + 2);
if (end !== -1) {
const content = s.slice(i + 2, end);
result += "||" + escapeMarkdownV2PreservingAllowedFormatting(content) + "||";
i = end + 2;
continue;
}
}
// underline: __text__
if (s.startsWith("__", i)) {
const end = s.indexOf("__", i + 2);
if (end !== -1) {
const content = s.slice(i + 2, end);
result += "__" + escapeMarkdownV2PreservingAllowedFormatting(content) + "__";
i = end + 2;
continue;
}
}
// bold: *text*
if (s[i] === "*") {
const end = s.indexOf("*", i + 1);
if (end !== -1) {
const content = s.slice(i + 1, end);
result += "*" + escapeMarkdownV2PreservingAllowedFormatting(content) + "*";
i = end + 1;
continue;
}
}
// italic: _text_
if (s[i] === "_") {
const end = s.indexOf("_", i + 1);
if (end !== -1) {
const content = s.slice(i + 1, end);
result += "_" + escapeMarkdownV2PreservingAllowedFormatting(content) + "_";
i = end + 1;
continue;
}
}
// strikethrough: ~text~
if (s[i] === "~") {
const end = s.indexOf("~", i + 1);
if (end !== -1) {
const content = s.slice(i + 1, end);
result += "~" + escapeMarkdownV2PreservingAllowedFormatting(content) + "~";
i = end + 1;
continue;
}
}
result += escapePlainMarkdownV2(s[i]);
i++;
}
return result;
}
function unescapeAccidentalMarkdownV2(s: string): string {
return s.replace(/\\([_*\[\]()~`>#+\-=|{}.!\\])/g, "$1");
}
function escapeTelegramQuoteLine(line: string): string {
const content = line.replace(/^>\s*/, "");
if (!content.trim()) {
return ">";
}
return ">" + escapeMarkdownV2PreservingAllowedFormatting(content);
}
function normalizeTelegramQuoteLines(s: string): string {
return s
.split("\n")
.map(line => {
if (!line.startsWith(">")) return line;
return line.replace(/^>\s+/, ">");
})
.join("\n");
}
function looksLikeMarkdownTableRow(line: string): boolean {
const trimmed = line.trim();
if (trimmed.startsWith("||") && trimmed.endsWith("||")) {
return false;
}
const pipeCount = (trimmed.match(/\|/g) ?? []).length;
if (pipeCount < 2) {
return false;
}
return trimmed.startsWith("|") || trimmed.endsWith("|") || pipeCount >= 2;
}
function isMarkdownTableSeparator(line: string): boolean {
return /^\s*\|?\s*:?-{3,}:?\s*(\|\s*:?-{3,}:?\s*)+\|?\s*$/.test(line);
}
function normalizeMarkdownTables(s: string): string {
return s
.split("\n")
.filter(line => !isMarkdownTableSeparator(line))
.map(line => {
if (!looksLikeMarkdownTableRow(line)) {
return line;
}
return line
.replace(/^\s*\|/, "")
.replace(/\|\s*$/, "")
.split("|")
.map(cell => cell.trim())
.filter(Boolean)
.join(" — ");
})
.join("\n");
}
export function escapeMarkdownV2Text(s: string): string {
s = unescapeAccidentalMarkdownV2(s);
s = normalizeTelegramQuoteLines(s);
s = s.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
s = s.replace(/^\s*[-*_]{3,}\s*$/gm, "— — —");
s = s.replace(/^\s*[-*+]\s+(?=\S)/gm, "• ");
s = s.replace(/\*\*(.+?)\*\*/gs, "*$1*");
s = s.replace(/~~(.+?)~~/gs, "~$1~");
s = s.replace(/^#{1,6}\s+/gm, "");
s = s.replace(/```[a-zA-Z0-9_-]*\n?([\s\S]*?)```/g, (_, code) => {
return code.trim();
});
s = s.replace(/!\[([^\]]*)]\(([^)]+)\)/g, (_, alt, url) => {
return alt ? `${alt}: ${url}` : url;
});
s = normalizeMarkdownTables(s);
s = s
.split("\n")
.map(line => {
if (line.startsWith(">")) {
return escapeTelegramQuoteLine(line);
}
if (line === ">") {
return ">";
}
return escapeMarkdownV2PreservingAllowedFormatting(line);
})
.join("\n");
s = s.replace(/\n{3,}/g, "\n\n");
return s.trim();
} }
export async function getFileUrl(fileId: string): Promise<string> { export async function getFileUrl(fileId: string): Promise<string> {