import { done, firstPos, queue } from "./queue.js"; import { transformBingResponse } from "./transformers.js"; import { BingChat, 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(), }); const chats: Record< number, { index: number; res?: ChatMessage; variant?: string } > = {}; export const variants = ["creative", "balanced", "precise"]; const defaultVariant = "Balanced"; export async function ai(ctx: Context, prompt: string) { try { const chatId = ctx.chat!.id; chats[chatId] ||= { index: 1 }; const { pos, turn } = queue(); if (pos > firstPos) { await ctx.reply(fmt`Queued at position: ${bold`${pos}`}`); await turn; } const { message_id } = await ctx.reply( fmt`${bold`[${chats[chatId].index++}]`} Running prompt...`, ); const bingRes = await bingChat.sendMessage( prompt, Object.assign({}, chats[chatId].res, { variant: chats[chatId].variant ?? defaultVariant, }), ); 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."; } if (!bingRes.text) { return "Received an empty response. Make sure the bot is set up properly and that you haven't crossed the daily message limit."; } return transformBingResponse(bingRes); })(); await ctx.telegram.editMessageText(chatId, message_id, undefined, tgRes, { disable_web_page_preview: true, }); } catch (e) { console.log(e); await ctx.reply("Something went wrong!"); } finally { done(); } } export function newChat(chatId: number) { delete chats[chatId].res; chats[chatId].index = 1; } export function getVariant(chatId: number) { return chats[chatId]?.variant ?? defaultVariant; } export function setVariant(chatId: number, variant: string) { chats[chatId] ||= { index: 1 }; variant = variant.toLowerCase(); chats[chatId].variant = variant.charAt(0).toUpperCase() + variant.slice(1); }