| 4539 | |
| 4540 | class SearchFeature extends BaseFeatureHandler { |
| 4541 | readonly name = "联网搜索"; |
| 4542 | readonly command = "search"; |
| 4543 | readonly description = "使用联网能力搜索并回答"; |
| 4544 | |
| 4545 | private aiService: AIService; |
| 4546 | private messageUtils: MessageUtils; |
| 4547 | |
| 4548 | constructor( |
| 4549 | aiService: AIService, |
| 4550 | configManagerPromise: Promise<ConfigManager>, |
| 4551 | httpClient: HttpClient, |
| 4552 | ) { |
| 4553 | super(configManagerPromise); |
| 4554 | this.aiService = aiService; |
| 4555 | this.messageUtils = new MessageUtils(configManagerPromise, httpClient); |
| 4556 | } |
| 4557 | |
| 4558 | async execute( |
| 4559 | msg: Api.Message, |
| 4560 | args: string[], |
| 4561 | _prefixes: string[], |
| 4562 | ): Promise<void> { |
| 4563 | const prefixes = getPrefixes(); |
| 4564 | const config = await this.getConfig(); |
| 4565 | |
| 4566 | const promptInput = args.slice(1).join(" ").trim(); |
| 4567 | |
| 4568 | const replyMsg = await safeGetReplyMessage(msg); |
| 4569 | requireUser(!!promptInput || !!replyMsg, "至少需要一条提示"); |
| 4570 | |
| 4571 | if ( |
| 4572 | !config.currentSearchTag || |
| 4573 | !config.currentSearchModel || |
| 4574 | !config.configs[config.currentSearchTag] |
| 4575 | ) { |
| 4576 | throw new UserError( |
| 4577 | `请先配置 API 并设置模型\n使用 ${prefixes[0]}ai config add <tag> <url> <key> [type] 和 ${prefixes[0]}ai model search <tag> <model-path>`, |
| 4578 | ); |
| 4579 | } |
| 4580 | |
| 4581 | await sendProcessing(msg, "search"); |
| 4582 | |
| 4583 | const replyToId = replyMsg?.id; |
| 4584 | |
| 4585 | let context = getMessageText(replyMsg); |
| 4586 | const imageParts = [ |
| 4587 | ...(await getMessageImageParts(replyMsg)), |
| 4588 | ...(await getMessageImageParts(msg)), |
| 4589 | ]; |
| 4590 | |
| 4591 | const normalizedPrompt = promptInput.trim(); |
| 4592 | const normalizedContext = (context || "").trim(); |
| 4593 | |
| 4594 | if ( |
| 4595 | normalizedPrompt && |
| 4596 | normalizedContext && |
| 4597 | normalizedPrompt === normalizedContext |
| 4598 | ) { |
nothing calls this directly
no outgoing calls
no test coverage detected