Skip to content

Commit

Permalink
fix & chore: fix telegraph article model read problem, fix reply obje…
Browse files Browse the repository at this point in the history
…ct error; add websocket check
  • Loading branch information
adolphnov committed Oct 14, 2024
1 parent fce547e commit ee8e86b
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 182 deletions.
24 changes: 13 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
chatgpt-telegram-workers:
container_name: chatgpt-telegram-workers
build: .
image: chatgpt-telegram-workers:latest
image: adolphnov/chatgpt-telegram-workers:latest
ports:
- "8787:8787"
volumes:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"deploy:vercel": "vercel deploy --prod",
"deploy:plugin": "BUILD_MODE=plugins-page vite build && wrangler pages deploy plugins --project-name=interpolate-test --branch=main",
"start:dist": "node dist/index.js",
"start:local": "CONFIG_PATH=./src/adapter/local/config.json TOML_PATH=./wrangler.toml tsx src/adapter/local/index.ts",
"start:local": "CONFIG_PATH=./config.json TOML_PATH=./wrangler.toml tsx src/adapter/local/index.ts",
"start:debug": "wrangler dev --local",
"prepare:vercel": "tsx ./scripts/plugins/vercel/setenv.ts",
"wrangler": "wrangler"
Expand Down
10 changes: 0 additions & 10 deletions src/adapter/local/config.json

This file was deleted.

6 changes: 3 additions & 3 deletions src/agent/wsrequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import WebSocket from 'ws';
import type { CompletionData } from './types';
import { AsyncIter } from './readable';
import { iterStream } from './request';
Expand All @@ -16,7 +15,7 @@ const perplexityExtractor = {
},
finalAdd: (data: any): string => {
if (data.web_results && data.web_results.length > 0) {
return `${data.web_results.map((r: Record<string, string>, i: number) => `${i + 1}.[${r.name}](${r.url})`).join('\n')}`;
return `${data.web_results.map((r: Record<string, string>, i: number) => `${i + 1}. [${r.name}](${r.url})`).join('\n')}`;
}
return '';
},
Expand Down Expand Up @@ -50,7 +49,8 @@ function perplexityFormatter(message: any[]): { done: boolean; content: any } {
}
}

export function WssRequest(url: string, protocols: string | string[] | null, options: Record<string, any>, messages: string[], handlers: Record<string, any>): Promise<any> {
export async function WssRequest(url: string, protocols: string | string[] | null, options: Record<string, any>, messages: string[], handlers: Record<string, any>): Promise<any> {
const { WebSocket } = await import('ws');
let { extractor, formatter, onStream } = handlers;
return new Promise((resolve) => {
const ws = protocols ? new WebSocket(url, protocols, options) : new WebSocket(url, options);
Expand Down
5 changes: 1 addition & 4 deletions src/extra/log/logDecortor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ export function getLog(context: AgentUserConfig, returnModel: boolean = false):
if (!logObj)
return '';
if (returnModel) {
return {
chat: logObj.chat.model.at(-1) || 'UNKNOWN',
tool: logObj.tool.model || 'UNKNOWN',
};
return logObj.chat.model?.at(-1) || logObj.tool.model || 'UNKNOWN';
}

// console.log('logObj:\n', JSON.stringify(logObj, null, 2));
Expand Down
1 change: 1 addition & 0 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function bindWebHookAction(request: RouterRequest): Promise<Response> {
for (const token of ENV.TELEGRAM_AVAILABLE_TOKENS) {
const api = createTelegramBotAPI(token);
const url = `https://${domain}/telegram/${token.trim()}/${hookMode}`;
console.log('webhook url: ', url);
const id = token.split(':')[0];
result[id] = {};
result[id].webhook = await api.setWebhook({ url }).then(res => res.json()).catch(e => errorToString(e));
Expand Down
5 changes: 4 additions & 1 deletion src/telegram/command/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type * as Telegram from 'telegram-bot-api-types';
import type { WorkerContext } from '../../config/context';
import type { UnionData } from '../utils/utils';
import { UUIDv4, isTelegramChatTypeGroup } from '../utils/utils';
import { UUIDv4, isCfWorker, isTelegramChatTypeGroup } from '../utils/utils';
import type { HistoryItem, HistoryModifierResult } from '../../agent/types';
import { OnStreamHander, chatWithLLM, sendImages } from '../handler/chat';
import { customInfo, loadChatLLM, loadImageGen } from '../../agent';
Expand Down Expand Up @@ -539,6 +539,9 @@ export class PerplexityCommandHandler implements CommandHandler {
needAuth = COMMAND_AUTH_CHECKER.shareModeGroup;
scopes: ScopeType[] = ['all_private_chats', 'all_chat_administrators'];
handle = async (message: Telegram.Message, subcommand: string, context: WorkerContext, sender: MessageSender): Promise<Response> => {
if (isCfWorker) {
return sender.sendPlainText('Due to the limitation of browser, Perplexity is not supported in worker / browser');
}
if (!ENV.PPLX_COOKIE) {
return sender.sendPlainText('Perplexity cookie is not set');
}
Expand Down
149 changes: 1 addition & 148 deletions src/telegram/handler/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,83 +165,6 @@ export class ChatHandler implements MessageHandler<WorkerContext> {
}
}

// class StreamHandler {
// private sender: MessageSender;
// private context?: WorkerContext;
// private _nextEnableTime: number | null = null;

// constructor(sender: MessageSender, context?: WorkerContext) {
// this.sender = sender;
// this.context = context;
// }

// get nextEnableTime(): number | null {
// return this._nextEnableTime;
// }

// set nextEnableTime(value: number | null) {
// this._nextEnableTime = value;
// }

// public async onStream(text: string, isEnd: boolean = false): Promise<void> {
// try {
// if (isEnd && this.context && ENV.TELEGRAPH_NUM_LIMIT > 0
// && text.length > ENV.TELEGRAPH_NUM_LIMIT
// && ['group', 'supergroup'].includes(this.sender.context.chatType)) {
// await this.sendTelegraph(this.context.MIDDEL_CONTEXT.originalMessage.text || 'Redo', text);
// return;
// }

// if (this.nextEnableTime && this.nextEnableTime > Date.now()) {
// return;
// }

// const data = this.context ? `${getLog(this.context.USER_CONFIG)}\n${text}` : text;
// const resp = await this.sender.sendRichText(
// data,
// ENV.DEFAULT_PARSE_MODE as Telegram.ParseMode,
// 'chat',
// );

// if (resp.status === 429) {
// const retryAfter = Number.parseInt(resp.headers.get('Retry-After') || '', 10);
// if (retryAfter) {
// this.nextEnableTime = Date.now() + retryAfter * 1000;
// }
// return;
// }

// this.nextEnableTime = null;
// if (resp.ok) {
// const respJson = await resp.json() as Telegram.ResponseWithMessage;
// this.sender.update({
// message_id: respJson.result.message_id,
// });
// }
// } catch (e) {
// console.error(e);
// }
// }

// private async sendTelegraph(question: string, text: string): Promise<void> {
// const prefix = `#Question\n\`\`\`\n${question.length > 400 ? `${question.slice(0, 200)}...${question.slice(-200)}` : question}\n\`\`\`\n---`;
// const botName = this.context!.SHARE_CONTEXT.botName;

// const telegraphPrefix = `${prefix}\n#Answer\n🤖 **${this.context?.USER_CONFIG.MODEL}**\n`;
// const debugInfo = `debug info:${getLog(this.context!.USER_CONFIG)}`;
// const telegraphSuffix = `\n---\n\`\`\`\n${debugInfo}\n\`\`\``;
// const telegraphSender = new TelegraphSender(
// this.sender.context,
// botName,
// this.context!.SHARE_CONTEXT.telegraphAccessTokenKey!,
// );
// await telegraphSender.send('Daily Q&A', telegraphPrefix + text + telegraphSuffix);
// const url = `https://telegra.ph/${telegraphSender.teleph_path}`;
// const msg = `回答已经转换成完整文章~\n[🔗点击进行查看](${url})`;
// await this.sender.sendRichText(msg);
// }
// }

export function OnStreamHander(sender: MessageSender, context?: WorkerContext): ChatStreamTextHandler {
let nextEnableTime: number | null = null;
async function onStream(text: string, isEnd: boolean = false): Promise<any> {
Expand All @@ -255,7 +178,7 @@ export function OnStreamHander(sender: MessageSender, context?: WorkerContext):
const prefix = `#Question\n\`\`\`\n${question.length > 400 ? `${question.slice(0, 200)}...${question.slice(-200)}` : question}\n\`\`\`\n---`;
const botName = context.SHARE_CONTEXT.botName;

const telegraph_prefix = `${prefix}\n#Answer\n🤖 **${getLog(context.USER_CONFIG, true).chat}**\n`;
const telegraph_prefix = `${prefix}\n#Answer\n🤖 **${getLog(context.USER_CONFIG, true)}**\n`;
const debug_info = `debug info:\n${getLog(context.USER_CONFIG) as string}`;
const telegraph_suffix = `\n---\n\`\`\`\n${debug_info}\n\`\`\``;
const telegraphSender = new TelegraphSender(sender.context, botName, context.SHARE_CONTEXT.telegraphAccessTokenKey!);
Expand Down Expand Up @@ -369,10 +292,6 @@ async function workflow(
if (i < flows.length - 1 && ['image', 'text'].includes(result?.type)) {
injectHistory(context, result, flows[i + 1].type);
}
// if (result.type === 'audio') {
// params.audio = [result.raw as unknown as Blob];
// params.message = result.text!;
// }

MiddleResult.push(result);
clearMessageContext(context);
Expand Down Expand Up @@ -425,72 +344,6 @@ async function handleAudioToText(
return result;
}

// async function workflow(context: WorkerContext, flows: Record<string, any>[], message: Telegram.Message, params: LLMChatRequestParams): Promise<Response | void> {
// const MiddleResult = context.MIDDEL_CONTEXT.middleResult;
// let eMsg;
// let result: UnionData | Response;

// for (let i = 0; i < flows.length; i++) {
// eMsg = (i === 0) ? context.MIDDEL_CONTEXT.originalMessage : MiddleResult[i - 1];
// if (params.extra_params) {
// delete params.extra_params;
// }
// if (Object.keys(flows[i]).filter(k => k !== 'type').length > 0) {
// params.extra_params = { ...flows[i] };
// delete params.extra_params?.type;
// }
// switch (`${eMsg?.type || 'text'}:${flows[i]?.type || 'text'}`) {
// case 'text:text':
// case 'image:text':
// result = await chatWithLLM(message, params, context, null);
// break;
// case 'text:image':
// {
// const sender = MessageSender.from(context.SHARE_CONTEXT.botToken, message);
// const agent = loadImageGen(context.USER_CONFIG);
// if (!agent) {
// return sender.sendPlainText('ERROR: Image generator not found');
// }
// sendAction(context.SHARE_CONTEXT.botToken, message.chat.id);
// const msg = await sender.sendPlainText('Please wait a moment...').then(r => r.json());
// result = await agent.request(eMsg.text!, context.USER_CONFIG);
// await sendImages(result as ImageResult, ENV.SEND_IMAGE_FILE, sender, context.USER_CONFIG);
// const api = createTelegramBotAPI(context.SHARE_CONTEXT.botToken);
// api.deleteMessage({ chat_id: sender.context.chat_id, message_id: msg.result.message_id }).catch(console.error);
// break;
// }
// case 'audio:text':
// {
// const agent = loadAudioLLM(context.USER_CONFIG);
// const sender = MessageSender.from(context.SHARE_CONTEXT.botToken, message);
// if (!agent) {
// return sender.sendPlainText('ERROR: Audio agent not found');
// }
// result = await agent.request(params.audio![0], context.USER_CONFIG, params.message);
// context.MIDDEL_CONTEXT.history.push({ role: 'user', content: result.text || '' });
// await sender.sendRichText(`${getLog(context.USER_CONFIG)}\n> \`${result.text}\``, 'MarkdownV2', 'chat');
// break;
// }
// case 'image:image':
// default:
// throw new Error('Unsupported type');
// }
// if (result instanceof Response) {
// return result;
// }
// if (i < flows.length - 1 && ['image', 'text'].includes(result.type)) {
// // Blob类型暂不支持
// injectHistory(context, result, flows[i + 1].type);
// }
// // if (result.type === 'audio') {
// // params.audio = [result.raw as unknown as Blob];
// // params.message = result.text!;
// // }
// MiddleResult.push(result);
// clearMessageContext(context);
// }
// }

export async function sendImages(img: ImageResult, SEND_IMAGE_FILE: boolean, sender: MessageSender, config: AgentUserConfig) {
const caption = img.text ? `${getLog(config)}\n> \`${img.text}\`` : getLog(config);
if (img.url && img.url.length > 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/telegram/handler/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class GroupMention implements MessageHandler {
if (!isMention) {
throw new Error('Not mention');
}

// 开启引用消息,并且不是回复bot,则将引用消息和当前消息合并
if (ENV.EXTRA_MESSAGE_CONTEXT && !replyMe && message.reply_to_message?.text) {
if (message.text || message.caption) {
message.text = `${message.reply_to_message.text}\n${message.text || message.caption}`;
Expand Down
2 changes: 1 addition & 1 deletion src/telegram/utils/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MessageContext implements Record<string, any> {
if (message.chat.type === 'group' || message.chat.type === 'supergroup') {
// 是否回复被回复的消息
if (message?.reply_to_message && ENV.EXTRA_MESSAGE_CONTEXT
&& ENV.ENABLE_REPLY_TO_MENTION) {
&& ENV.ENABLE_REPLY_TO_MENTION && !message.reply_to_message.from?.is_bot) {
this.reply_to_message_id = message.reply_to_message.message_id;
} else {
this.reply_to_message_id = message.message_id;
Expand Down
4 changes: 3 additions & 1 deletion src/telegram/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ export function UUIDv4() {
});
}

export const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
export const isCfWorker = typeof globalThis !== 'undefined'
&& typeof (globalThis as any).ServiceWorkerGlobalScope !== 'undefined'
&& globalThis instanceof ((globalThis as any).ServiceWorkerGlobalScope);

0 comments on commit ee8e86b

Please sign in to comment.