fix(i18n): stop logging missing-key noise for optional tool descriptions

This commit is contained in:
matevip 2026-05-19 20:06:38 +08:00
parent d629c945ee
commit b2f9976f44
2 changed files with 28 additions and 3 deletions

View File

@ -45,6 +45,28 @@ public class I18nService {
}
}
/**
* Resolve a message key that is expected to be optionally absent e.g. an
* override that legitimately does not exist for every subject (a tool may
* ship its description in code rather than in the message bundle).
* <p>
* Unlike {@link #msg(String, Object...)}, a missing key returns {@code null}
* and is NOT logged: the caller treats absence as "use the built-in default",
* which is normal operation rather than a misconfiguration worth a log line.
*
* @param key 消息键
* @param args 占位符参数
* @return 解析后的消息文本找不到 key 时返回 {@code null}
*/
public String msgOptional(String key, Object... args) {
Locale locale = resolveLocale();
try {
return messageSource.getMessage(key, args, locale);
} catch (Exception e) {
return null;
}
}
/**
* Clear the cached Locale. Call after a language switch so that the next
* {@link #msg(String, Object...)} call re-reads {@code SystemSettingService.getLanguage()}.

View File

@ -152,9 +152,12 @@ public class ToolRegistry {
for (ToolCallback cb : cbs) {
String toolName = cb.getToolDefinition().name();
String descKey = "tool." + toolName + ".desc";
String localizedDesc = i18nService.msg(descKey);
// 如果 key 被解析不等于 key 本身使用本地化描述
if (!localizedDesc.equals(descKey)) {
// The i18n description is an optional override: tools without a
// bundle entry (e.g. wiki tools) keep the description declared on
// their @Tool annotation. Use msgOptional so an absent key is not
// logged as a "missing key" that is expected, not a fault.
String localizedDesc = i18nService.msgOptional(descKey);
if (localizedDesc != null) {
localizedCallbacks.add(new LocaleAwareToolCallback(cb, localizedDesc));
} else {
localizedCallbacks.add(cb);