feat(plugin): 集成 MCP 协议支持并优化代码风格
- 新增 fastmcp 依赖,支持通过 Streamable HTTP 连接外部工具服务器 - 在 component_registry 与 tool_api 中实现 MCP 工具加载、注册及调用链路 - 补充 README 中的 MCP 特性说明 - 统一修复多处 import 顺序、空行、引号及类型注解,提升代码整洁度 - 在 pyproject.toml 中忽略 PERF203 规则,允许循环内异常处理 - 优化语音缓存与本地 ASR 调用逻辑,减少冗余代码
This commit is contained in:
@@ -303,7 +303,7 @@ class Prompt:
|
||||
|
||||
@staticmethod
|
||||
def _process_escaped_braces(template) -> str:
|
||||
"""预处理模板,将 `\{` 和 `\}` 替换为临时标记."""
|
||||
r"""预处理模板,将 `\{` 和 `\}` 替换为临时标记."""
|
||||
if isinstance(template, list):
|
||||
template = "\n".join(str(item) for item in template)
|
||||
elif not isinstance(template, str):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import asyncio
|
||||
import re
|
||||
from typing import Type
|
||||
|
||||
from src.chat.utils.prompt_params import PromptParameters
|
||||
from src.common.logger import get_logger
|
||||
@@ -21,7 +20,7 @@ class PromptComponentManager:
|
||||
3. 提供一个接口,以便在构建核心Prompt时,能够获取并执行所有相关的组件。
|
||||
"""
|
||||
|
||||
def _get_rules_for(self, target_prompt_name: str) -> list[tuple[InjectionRule, Type[BasePrompt]]]:
|
||||
def _get_rules_for(self, target_prompt_name: str) -> list[tuple[InjectionRule, type[BasePrompt]]]:
|
||||
"""
|
||||
获取指定目标Prompt的所有注入规则及其关联的组件类。
|
||||
|
||||
|
||||
@@ -6,15 +6,14 @@
|
||||
避免不必要的自我语音识别。
|
||||
"""
|
||||
import hashlib
|
||||
from typing import Dict
|
||||
|
||||
# 一个简单的内存缓存,用于将机器人自己发送的语音消息映射到其原始文本。
|
||||
# 键是语音base64内容的SHA256哈希值。
|
||||
_self_voice_cache: Dict[str, str] = {}
|
||||
_self_voice_cache: dict[str, str] = {}
|
||||
|
||||
def get_voice_key(base64_content: str) -> str:
|
||||
"""为语音内容生成一个一致的键。"""
|
||||
return hashlib.sha256(base64_content.encode('utf-8')).hexdigest()
|
||||
return hashlib.sha256(base64_content.encode("utf-8")).hexdigest()
|
||||
|
||||
def register_self_voice(base64_content: str, text: str):
|
||||
"""
|
||||
@@ -39,4 +38,4 @@ def consume_self_voice_text(base64_content: str) -> str | None:
|
||||
str | None: 如果找到,则返回原始文本,否则返回None。
|
||||
"""
|
||||
key = get_voice_key(base64_content)
|
||||
return _self_voice_cache.pop(key, None)
|
||||
return _self_voice_cache.pop(key, None)
|
||||
|
||||
@@ -19,10 +19,11 @@ async def get_voice_text(voice_base64: str) -> str:
|
||||
|
||||
# 如果选择本地识别
|
||||
if asr_provider == "local":
|
||||
from src.plugin_system.apis import tool_api
|
||||
import tempfile
|
||||
import base64
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from src.plugin_system.apis import tool_api
|
||||
|
||||
local_asr_tool = tool_api.get_tool_instance("local_asr")
|
||||
if not local_asr_tool:
|
||||
@@ -39,8 +40,8 @@ async def get_voice_text(voice_base64: str) -> str:
|
||||
text = await local_asr_tool.execute(function_args={"audio_path": audio_path})
|
||||
if "失败" in text or "出错" in text or "错误" in text:
|
||||
logger.warning(f"本地语音识别失败: {text}")
|
||||
return f"[语音(本地识别失败)]"
|
||||
|
||||
return "[语音(本地识别失败)]"
|
||||
|
||||
logger.info(f"本地语音识别成功: {text}")
|
||||
return f"[语音] {text}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user