(
msg: Api.Message,
question: string,
trigger?: Api.Message,
token?: AbortToken,
)
| 4419 | |
| 4420 | async handleQuestion( |
| 4421 | msg: Api.Message, |
| 4422 | question: string, |
| 4423 | trigger?: Api.Message, |
| 4424 | token?: AbortToken, |
| 4425 | ): Promise<void> { |
| 4426 | const config = await this.getConfig(); |
| 4427 | |
| 4428 | if ( |
| 4429 | !config.currentChatTag || |
| 4430 | !config.currentChatModel || |
| 4431 | !config.configs[config.currentChatTag] |
| 4432 | ) { |
| 4433 | const prefixes = getPrefixes(); |
| 4434 | throw new UserError( |
| 4435 | `请先配置 API 并设置模型\n使用 ${prefixes[0]}ai config add <tag> <url> <key> [type] 和 ${prefixes[0]}ai model chat <tag> <model-path>`, |
| 4436 | ); |
| 4437 | } |
| 4438 | |
| 4439 | token?.throwIfAborted(); |
| 4440 | |
| 4441 | await sendProcessing(msg, "chat"); |
| 4442 | |
| 4443 | const replyMsg = await safeGetReplyMessage(msg); |
| 4444 | let context = getMessageText(replyMsg); |
| 4445 | const replyToId = replyMsg?.id; |
| 4446 | const imageParts = [ |
| 4447 | ...(await getMessageImageParts(replyMsg)), |
| 4448 | ...(await getMessageImageParts(msg)), |
| 4449 | ]; |
| 4450 | |
| 4451 | const normalizedQuestion = question.trim(); |
| 4452 | const normalizedContext = context.trim(); |
| 4453 | if ( |
| 4454 | normalizedQuestion && |
| 4455 | normalizedContext && |
| 4456 | normalizedQuestion === normalizedContext |
| 4457 | ) { |
| 4458 | context = ""; |
| 4459 | } |
| 4460 | |
| 4461 | const userText = context |
| 4462 | ? `上下文:\n${context}\n\n问题:\n${question}` |
| 4463 | : question; |
| 4464 | |
| 4465 | const response = await this.aiService.callAI(userText, imageParts, token); |
| 4466 | const answer = response.text || "AI 回复为空"; |
| 4467 | |
| 4468 | const collapseSafe = config.collapse; |
| 4469 | const htmlAnswer = TelegramFormatter.markdownToHtml(answer, { |
| 4470 | collapseSafe, |
| 4471 | }); |
| 4472 | const safeQuestion = htmlEscape(question); |
| 4473 | const formattedAnswer = `Q:\n${safeQuestion}\n\nA:\n${htmlAnswer}`; |
| 4474 | |
| 4475 | token?.throwIfAborted(); |
| 4476 | |
| 4477 | if (config.telegraph.enabled && formattedAnswer.length > 4050) { |
| 4478 | await this.handleLongContentWithTelegraph( |
no test coverage detected