feat: add response timeout

* also switch to env vars
This commit is contained in:
Udit Karode
2023-04-09 19:22:30 +05:30
parent bb4800505b
commit 02d7adbdca
7 changed files with 108 additions and 45 deletions

View File

@ -1,14 +1,12 @@
import { done, firstPos, queue } from "./queue.js";
import { transformBingResponse } from "./transformers.js";
import { BingChat, ChatMessage } from "bing-chat";
import { ChatMessage } from "bing-chat";
import { Context } from "telegraf";
import { bold, fmt } from "telegraf/format";
import { Update } from "typegram";
import { BING_COOKIE } from "../variables.js";
export const bingChat = new BingChat({
cookie: BING_COOKIE.replaceAll('"', "").trim(),
});
import { bingChat } from "./config.js";
import pTimeout from "p-timeout";
import { Message } from "telegraf/types";
const chats: Record<
number,
@ -16,10 +14,21 @@ const chats: Record<
> = {};
export const variants = ["creative", "balanced", "precise"];
const defaultTimeoutMs = 50 * 1000;
const defaultVariant = "Balanced";
export async function ai(ctx: Context<Update>, prompt: string) {
export async function ai(
ctx: Context<Update>,
prompt: string,
): Promise<void | Message> {
try {
if (!prompt) {
return await ctx.reply(
"No prompt provided! Please read /help for more information.",
);
}
const chatId = ctx.chat!.id;
chats[chatId] ||= { index: 1 };
@ -33,19 +42,33 @@ export async function ai(ctx: Context<Update>, prompt: string) {
fmt`${bold`[${chats[chatId].index++}]`} Running prompt...`,
);
const bingRes = await bingChat.sendMessage(
prompt,
Object.assign({}, chats[chatId].res, {
variant: chats[chatId].variant ?? defaultVariant,
}),
);
let bingRes: ChatMessage;
try {
bingRes = await pTimeout(
bingChat.sendMessage(
prompt,
Object.assign({}, chats[chatId].res, {
variant: chats[chatId].variant ?? defaultVariant,
}),
),
{
milliseconds: defaultTimeoutMs,
message: `No response from Bing, waited ${
defaultTimeoutMs / 1000
} seconds`,
},
);
} catch (e) {
return await ctx.reply((e as Error).message || "Something went wrong");
}
chats[chatId].res = bingRes;
const tgRes = (() => {
if (bingRes.text === prompt) {
// Bing Chat often replies with the exact prompt
// in case it's unable to continue the conversation.
return "Something went wrong. Starting a new chat with /newchat is recommended.";
return "Bing replied with the exact text as your prompt. This usually happens when the AI is unable to continue the conversation. Starting a new chat with /newchat is recommended.";
}
if (!bingRes.text) {

18
src/config.ts Normal file
View File

@ -0,0 +1,18 @@
import { BingChat } from "bing-chat";
import dotenv from "dotenv-safe";
import { Context, Telegraf } from "telegraf";
import { Update } from "typegram";
dotenv.config();
export const bingChat = new BingChat({ cookie: process.env.BING_COOKIE! });
export const bot: Telegraf<Context<Update>> = new Telegraf(
process.env.TG_TOKEN!,
);
const allowedChatsConfig = process.env.ALLOWED_CHATS!;
export const allowedChats =
allowedChatsConfig.toLowerCase() === "all"
? undefined
: allowedChatsConfig.split(",").map(Number).filter(Boolean);

View File

@ -1,21 +1,19 @@
import { bold, code, fmt, underline } from "telegraf/format";
import { ALLOWED_CHAT_IDS, TG_TOKEN } from "../variables.js";
import { ai, getVariant, newChat, setVariant, variants } from "./ai.js";
import { checkOrigin } from "./check-origin.js";
import { message } from "telegraf/filters";
import { useNewReplies } from "telegraf/future";
import { Telegraf } from "telegraf";
function args(cmd: string) {
return cmd.split(" ").splice(1).join(" ");
}
import { allowedChats, bot } from "./config.js";
async function main() {
const bot = new Telegraf(TG_TOKEN.trim());
bot.use(useNewReplies());
if (typeof ALLOWED_CHAT_IDS != "string") {
bot.use(checkOrigin(ALLOWED_CHAT_IDS));
if (allowedChats) {
console.log("Usage allowed in chats: ");
console.log(allowedChats);
bot.use(checkOrigin(allowedChats));
} else {
console.log("Usage allowed in all chats");
}
bot.command("ai", async ctx => {
@ -81,6 +79,10 @@ The variant command accepts the name of any of these 3 variants in a case-insens
);
});
bot.catch(err => {
console.log(err);
});
bot.launch();
console.log("Bot running!");
console.log("Use ^C to stop");
@ -90,4 +92,8 @@ The variant command accepts the name of any of these 3 variants in a case-insens
process.once("SIGTERM", () => bot.stop("SIGTERM"));
}
function args(cmd: string) {
return cmd.split(" ").splice(1).join(" ");
}
main();