Skip to content

Commit

Permalink
perf: 修改callback_query 错误处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 13, 2024
1 parent 4d75571 commit fe9ef45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
14 changes: 10 additions & 4 deletions src/telegram/callback_query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ const QUERY_HANDLERS = [

export async function handleCallbackQuery(callbackQuery: Telegram.CallbackQuery, context: WorkerContext): Promise<Response | null> {
const sender = MessageSender.fromCallbackQuery(context.SHARE_CONTEXT.botToken, callbackQuery);
const answerCallbackQuery = (msg: string): Promise<Response> => {
return sender.api.answerCallbackQuery({
callback_query_id: callbackQuery.id,
text: msg,
});
};
try {
if (!callbackQuery.message) {
return sender.sendPlainText('no message');
return null;
}
const chatId = callbackQuery.message.chat.id;
const speakerId = callbackQuery.from?.id || chatId;
Expand All @@ -27,10 +33,10 @@ export async function handleCallbackQuery(callbackQuery: Telegram.CallbackQuery,
// 获取身份并判断
const chatRole = await loadChatRoleWithContext(chatId, speakerId, context);
if (chatRole === null) {
return sender.sendPlainText('ERROR: Get chat role failed');
return answerCallbackQuery('ERROR: Get chat role failed');
}
if (!roleList.includes(chatRole)) {
return sender.sendPlainText(`ERROR: Permission denied, need ${roleList.join(' or ')}`);
return answerCallbackQuery(`ERROR: Permission denied, need ${roleList.join(' or ')}`);
}
}
}
Expand All @@ -41,7 +47,7 @@ export async function handleCallbackQuery(callbackQuery: Telegram.CallbackQuery,
}
}
} catch (e) {
return sender.sendPlainText(`ERROR: ${(e as Error).message}`);
return answerCallbackQuery(`ERROR: ${(e as Error).message}`);
}
return null;
}
12 changes: 6 additions & 6 deletions src/telegram/callback_query/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AgentListCallbackQueryHandler implements CallbackQueryHandler {

handle = async (query: Telegram.CallbackQuery, data: string, context: WorkerContext): Promise<Response> => {
if (!query.message) {
return new Response('no message');
throw new Error('no message');
}
const names = CHAT_AGENTS.filter(agent => agent.enable(ENV.USER_CONFIG)).map(agent => agent.name);
const sender = MessageSender.fromCallbackQuery(context.SHARE_CONTEXT.botToken, query);
Expand Down Expand Up @@ -53,7 +53,7 @@ export class ModelListCallbackQueryHandler implements CallbackQueryHandler {

async handle(query: Telegram.CallbackQuery, data: string, context: WorkerContext): Promise<Response> {
if (!query.message) {
return new Response('no message');
throw new Error('no message');
}
const sender = MessageSender.fromCallbackQuery(context.SHARE_CONTEXT.botToken, query);
const [agent, page] = JSON.parse(data.substring(this.prefix.length));
Expand All @@ -63,7 +63,7 @@ export class ModelListCallbackQueryHandler implements CallbackQueryHandler {
};
const chatAgent = loadChatLLM(conf);
if (!chatAgent) {
return sender.sendPlainText(`ERROR: agent not found: ${agent}`);
throw new Error(`agent not found: ${agent}`);
}
const models = await chatAgent.modelList(conf);
const keyboard: Telegram.InlineKeyboardButton[][] = [];
Expand Down Expand Up @@ -130,7 +130,7 @@ export class ModelChangeCallbackQueryHandler implements CallbackQueryHandler {

async handle(query: Telegram.CallbackQuery, data: string, context: WorkerContext): Promise<Response> {
if (!query.message) {
return new Response('no message');
throw new Error('no message');
}
const sender = MessageSender.fromCallbackQuery(context.SHARE_CONTEXT.botToken, query);
const [agent, model] = JSON.parse(data.substring(this.prefix.length));
Expand All @@ -140,10 +140,10 @@ export class ModelChangeCallbackQueryHandler implements CallbackQueryHandler {
};
const chatAgent = loadChatLLM(conf);
if (!agent) {
return sender.sendPlainText(`ERROR: agent not found: ${model}`);
throw new Error(`agent not found: ${agent}`);
}
if (!chatAgent?.modelKey) {
return sender.sendPlainText(`ERROR: agent not support model change: ${agent}`);
throw new Error(`modelKey not found: ${agent}`);
}
await setUserConfig({
AI_PROVIDER: agent,
Expand Down

0 comments on commit fe9ef45

Please sign in to comment.