44# Licensed under the MIT License. See License.txt in the project root for license information.
55# ======================================================================================================================
66import json
7+ import re
78from concurrent .futures import ThreadPoolExecutor , as_completed
89from dataclasses import dataclass
910from typing import Dict , List , Optional , Sequence
@@ -70,6 +71,11 @@ def _get_max_results_per_provider() -> int:
7071 pass
7172
7273
74+ @value ('internet-search.summary-length' )
75+ def _get_max_summary_length () -> int :
76+ pass
77+
78+
7379def _truncate (text : str , max_chars : int ) -> str :
7480 if len (text ) <= max_chars :
7581 return text
@@ -90,8 +96,6 @@ def _extract_summary(text: str, max_sentences: int = 4) -> str:
9096 if not text :
9197 return ""
9298
93- # 定义句子分隔符(支持中英文)
94- import re
9599 # 使用正则表达式匹配句子结束符号
96100 sentences = re .split (r'([。!?\.!?]+["\'»\)]?\s*)' , text )
97101
@@ -115,11 +119,12 @@ def _extract_summary(text: str, max_sentences: int = 4) -> str:
115119 summary = " " .join (combined_sentences [:max_sentences ])
116120
117121 # 确保摘要不会过长(最多150字符)
118- if len (summary ) > 150 :
119- summary = summary [:147 ].rstrip () + "..."
122+ if len (summary ) > _get_max_summary_length () :
123+ summary = summary [:( _get_max_summary_length () - 3 ) ].rstrip () + "..."
120124
121125 return summary
122126
127+
123128def _search_exa (query : str , api_key : str , max_results : int , max_snippet_chars : int ) -> List [SearchItem ]:
124129 """在 Exa 中搜索"""
125130 items : List [SearchItem ] = []
@@ -264,11 +269,11 @@ def _internet_search(
264269 except Exception as e :
265270 sys_plugin_logger .error (f'Unexpected error in { provider_name } search: { str (e )} ' )
266271 errors .append (provider_name )
267-
272+
268273 # 如果所有搜索都失败了,才抛出异常
269274 if not items and errors :
270275 raise FitException (
271- InternalErrorCode .CLIENT_ERROR ,
276+ InternalErrorCode .CLIENT_ERROR ,
272277 f'All search tools failed: { ", " .join (errors )} '
273278 )
274279
0 commit comments