mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-09 15:26:03 +00:00
Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27bbb2297a | ||
|
|
0d235aaef8 | ||
|
|
dda8c637d8 | ||
|
|
e6d7aaa440 | ||
|
|
028458b33c | ||
|
|
9c7d8099cb | ||
|
|
5640dc332d | ||
|
|
40275c3ef1 | ||
|
|
ebe0b2f335 | ||
|
|
97603e8441 | ||
|
|
72cd444861 | ||
|
|
955b859f2c | ||
|
|
dea5cc9c0c | ||
|
|
d13ab1703e | ||
|
|
61ab6a009b | ||
|
|
a9ae36d362 | ||
|
|
f518395ce5 | ||
|
|
20b17fe378 | ||
|
|
572182180c | ||
|
|
de261099aa | ||
|
|
50f0122955 | ||
|
|
fe9eff923e | ||
|
|
dd36278032 | ||
|
|
a079821976 | ||
|
|
fa233e0a24 | ||
|
|
22306cb4ea | ||
|
|
f2d45a3668 | ||
|
|
db91ff12f7 | ||
|
|
eb841fb73e |
@@ -39,7 +39,7 @@
|
|||||||
|
|
||||||
- 💬 大模型对话、Agent:支持多种大模型,适配群聊和私聊;具有多轮对话、工具调用、多模态能力,并深度适配 [Dify](https://dify.ai)。目前支持 QQ、QQ频道、企业微信、个人微信、飞书、Discord、Telegram 等平台。
|
- 💬 大模型对话、Agent:支持多种大模型,适配群聊和私聊;具有多轮对话、工具调用、多模态能力,并深度适配 [Dify](https://dify.ai)。目前支持 QQ、QQ频道、企业微信、个人微信、飞书、Discord、Telegram 等平台。
|
||||||
- 🛠️ 高稳定性、功能完备:原生支持访问控制、限速、敏感词过滤等机制;配置简单,支持多种部署方式。
|
- 🛠️ 高稳定性、功能完备:原生支持访问控制、限速、敏感词过滤等机制;配置简单,支持多种部署方式。
|
||||||
- 🧩 插件扩展、活跃社区:支持事件驱动、组件扩展等插件机制;丰富生态,目前已有数十个[插件](https://docs.langbot.app/plugin/plugin-intro.html)
|
- 🧩 插件扩展、活跃社区:支持事件驱动、组件扩展等插件机制;适配 Anthropic [MCP 协议](https://modelcontextprotocol.io/);目前已有数十个[插件](https://docs.langbot.app/plugin/plugin-intro.html)
|
||||||
- 😻 [New] Web 管理面板:支持通过浏览器管理 LangBot 实例,具体支持功能,查看[文档](https://docs.langbot.app/webui/intro.html)
|
- 😻 [New] Web 管理面板:支持通过浏览器管理 LangBot 实例,具体支持功能,查看[文档](https://docs.langbot.app/webui/intro.html)
|
||||||
|
|
||||||
## 📦 开始使用
|
## 📦 开始使用
|
||||||
@@ -97,7 +97,7 @@
|
|||||||
|
|
||||||
🚧: 正在开发中
|
🚧: 正在开发中
|
||||||
|
|
||||||
### 大模型
|
### 大模型能力
|
||||||
|
|
||||||
| 模型 | 状态 | 备注 |
|
| 模型 | 状态 | 备注 |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
@@ -114,7 +114,7 @@
|
|||||||
| [SiliconFlow](https://siliconflow.cn/) | ✅ | 大模型聚合平台 |
|
| [SiliconFlow](https://siliconflow.cn/) | ✅ | 大模型聚合平台 |
|
||||||
| [阿里云百炼](https://bailian.console.aliyun.com/) | ✅ | 大模型聚合平台, LLMOps 平台 |
|
| [阿里云百炼](https://bailian.console.aliyun.com/) | ✅ | 大模型聚合平台, LLMOps 平台 |
|
||||||
| [火山方舟](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | 大模型聚合平台, LLMOps 平台 |
|
| [火山方舟](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | 大模型聚合平台, LLMOps 平台 |
|
||||||
|
| [MCP](https://modelcontextprotocol.io/) | ✅ | 支持通过 MCP 协议获取工具 |
|
||||||
### TTS
|
### TTS
|
||||||
|
|
||||||
| 平台/模型 | 备注 |
|
| 平台/模型 | 备注 |
|
||||||
|
|||||||
@@ -36,7 +36,7 @@
|
|||||||
|
|
||||||
- 💬 Chat with LLM / Agent: Supports multiple LLMs, adapt to group chats and private chats; Supports multi-round conversations, tool calls, and multi-modal capabilities. Deeply integrates with [Dify](https://dify.ai). Currently supports QQ, QQ Channel, WeCom, personal WeChat, Lark, DingTalk, Discord, Telegram, etc.
|
- 💬 Chat with LLM / Agent: Supports multiple LLMs, adapt to group chats and private chats; Supports multi-round conversations, tool calls, and multi-modal capabilities. Deeply integrates with [Dify](https://dify.ai). Currently supports QQ, QQ Channel, WeCom, personal WeChat, Lark, DingTalk, Discord, Telegram, etc.
|
||||||
- 🛠️ High Stability, Feature-rich: Native access control, rate limiting, sensitive word filtering, etc. mechanisms; Easy to use, supports multiple deployment methods.
|
- 🛠️ High Stability, Feature-rich: Native access control, rate limiting, sensitive word filtering, etc. mechanisms; Easy to use, supports multiple deployment methods.
|
||||||
- 🧩 Plugin Extension, Active Community: Support event-driven, component extension, etc. plugin mechanisms; Rich ecology, currently has dozens of [plugins](https://docs.langbot.app/plugin/plugin-intro.html)
|
- 🧩 Plugin Extension, Active Community: Support event-driven, component extension, etc. plugin mechanisms; Integrate Anthropic [MCP protocol](https://modelcontextprotocol.io/); Currently has dozens of [plugins](https://docs.langbot.app/plugin/plugin-intro.html)
|
||||||
- 😻 [New] Web UI: Support management LangBot instance through the browser, for details, see [documentation](https://docs.langbot.app/webui/intro.html)
|
- 😻 [New] Web UI: Support management LangBot instance through the browser, for details, see [documentation](https://docs.langbot.app/webui/intro.html)
|
||||||
|
|
||||||
## 📦 Getting Started
|
## 📦 Getting Started
|
||||||
@@ -111,6 +111,7 @@ Directly use the released version to run, see the [Manual Deployment](https://do
|
|||||||
| [SiliconFlow](https://siliconflow.cn/) | ✅ | LLM gateway(MaaS) |
|
| [SiliconFlow](https://siliconflow.cn/) | ✅ | LLM gateway(MaaS) |
|
||||||
| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLM gateway(MaaS), LLMOps platform |
|
| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLM gateway(MaaS), LLMOps platform |
|
||||||
| [Volc Engine Ark](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | LLM gateway(MaaS), LLMOps platform |
|
| [Volc Engine Ark](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | LLM gateway(MaaS), LLMOps platform |
|
||||||
|
| [MCP](https://modelcontextprotocol.io/) | ✅ | Support tool access through MCP protocol |
|
||||||
|
|
||||||
## 🤝 Community Contribution
|
## 🤝 Community Contribution
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
- 💬 LLM / エージェントとのチャット: 複数のLLMをサポートし、グループチャットとプライベートチャットに対応。マルチラウンドの会話、ツールの呼び出し、マルチモーダル機能をサポート。 [Dify](https://dify.ai) と深く統合。現在、QQ、QQ チャンネル、WeChat、個人 WeChat、Lark、DingTalk、Discord、Telegram など、複数のプラットフォームをサポートしています。
|
- 💬 LLM / エージェントとのチャット: 複数のLLMをサポートし、グループチャットとプライベートチャットに対応。マルチラウンドの会話、ツールの呼び出し、マルチモーダル機能をサポート。 [Dify](https://dify.ai) と深く統合。現在、QQ、QQ チャンネル、WeChat、個人 WeChat、Lark、DingTalk、Discord、Telegram など、複数のプラットフォームをサポートしています。
|
||||||
- 🛠️ 高い安定性、豊富な機能: ネイティブのアクセス制御、レート制限、敏感な単語のフィルタリングなどのメカニズムをサポート。使いやすく、複数のデプロイ方法をサポート。
|
- 🛠️ 高い安定性、豊富な機能: ネイティブのアクセス制御、レート制限、敏感な単語のフィルタリングなどのメカニズムをサポート。使いやすく、複数のデプロイ方法をサポート。
|
||||||
- 🧩 プラグイン拡張、活発なコミュニティ: イベント駆動、コンポーネント拡張などのプラグインメカニズムをサポート。豊富なエコシステム、現在数十の[プラグイン](https://docs.langbot.app/plugin/plugin-intro.html)が存在。
|
- 🧩 プラグイン拡張、活発なコミュニティ: イベント駆動、コンポーネント拡張などのプラグインメカニズムをサポート。適配 Anthropic [MCP プロトコル](https://modelcontextprotocol.io/);豊富なエコシステム、現在数十の[プラグイン](https://docs.langbot.app/plugin/plugin-intro.html)が存在。
|
||||||
- 😻 [新機能] Web UI: ブラウザを通じてLangBotインスタンスを管理することをサポート。詳細は[ドキュメント](https://docs.langbot.app/webui/intro.html)を参照。
|
- 😻 [新機能] Web UI: ブラウザを通じてLangBotインスタンスを管理することをサポート。詳細は[ドキュメント](https://docs.langbot.app/webui/intro.html)を参照。
|
||||||
|
|
||||||
## 📦 始め方
|
## 📦 始め方
|
||||||
@@ -110,6 +110,7 @@ LangBotはBTPanelにリストされています。BTPanelをインストール
|
|||||||
| [SiliconFlow](https://siliconflow.cn/) | ✅ | LLMゲートウェイ(MaaS) |
|
| [SiliconFlow](https://siliconflow.cn/) | ✅ | LLMゲートウェイ(MaaS) |
|
||||||
| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLMゲートウェイ(MaaS), LLMOpsプラットフォーム |
|
| [Aliyun Bailian](https://bailian.console.aliyun.com/) | ✅ | LLMゲートウェイ(MaaS), LLMOpsプラットフォーム |
|
||||||
| [Volc Engine Ark](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | LLMゲートウェイ(MaaS), LLMOpsプラットフォーム |
|
| [Volc Engine Ark](https://console.volcengine.com/ark/region:ark+cn-beijing/model?vendor=Bytedance&view=LIST_VIEW) | ✅ | LLMゲートウェイ(MaaS), LLMOpsプラットフォーム |
|
||||||
|
| [MCP](https://modelcontextprotocol.io/) | ✅ | MCPプロトコルをサポート |
|
||||||
|
|
||||||
## 🤝 コミュニティ貢献
|
## 🤝 コミュニティ貢献
|
||||||
|
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ class OAClient():
|
|||||||
|
|
||||||
class OAClientForLongerResponse():
|
class OAClientForLongerResponse():
|
||||||
|
|
||||||
def __init__(self,token:str,EncodingAESKey:str,AppID:str,Appsecret:str):
|
def __init__(self,token:str,EncodingAESKey:str,AppID:str,Appsecret:str,LoadingMessage:str):
|
||||||
self.token = token
|
self.token = token
|
||||||
self.aes = EncodingAESKey
|
self.aes = EncodingAESKey
|
||||||
self.appid = AppID
|
self.appid = AppID
|
||||||
@@ -189,6 +189,7 @@ class OAClientForLongerResponse():
|
|||||||
"example":[],
|
"example":[],
|
||||||
}
|
}
|
||||||
self.access_token_expiry_time = None
|
self.access_token_expiry_time = None
|
||||||
|
self.loading_message = LoadingMessage
|
||||||
|
|
||||||
async def handle_callback_request(self):
|
async def handle_callback_request(self):
|
||||||
try:
|
try:
|
||||||
@@ -246,7 +247,7 @@ class OAClientForLongerResponse():
|
|||||||
to_user=from_user,
|
to_user=from_user,
|
||||||
from_user=to_user,
|
from_user=to_user,
|
||||||
create_time=int(time.time()),
|
create_time=int(time.time()),
|
||||||
content="AI正在思考中,请发送任意内容获取回答。"
|
content=self.loading_message
|
||||||
)
|
)
|
||||||
|
|
||||||
if user_msg_queue.get(from_user) and user_msg_queue[from_user][0]["content"]:
|
if user_msg_queue.get(from_user) and user_msg_queue[from_user][0]["content"]:
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ class FuncOperator(operator.CommandOperator):
|
|||||||
|
|
||||||
all_functions = await self.ap.tool_mgr.get_all_functions(
|
all_functions = await self.ap.tool_mgr.get_all_functions(
|
||||||
plugin_enabled=True,
|
plugin_enabled=True,
|
||||||
plugin_status=plugin_context.RuntimeContainerStatus.INITIALIZED,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for func in all_functions:
|
for func in all_functions:
|
||||||
|
|||||||
@@ -204,6 +204,8 @@ class Application:
|
|||||||
case core_entities.LifecycleControlScope.PROVIDER.value:
|
case core_entities.LifecycleControlScope.PROVIDER.value:
|
||||||
self.logger.info("执行热重载 scope="+scope)
|
self.logger.info("执行热重载 scope="+scope)
|
||||||
|
|
||||||
|
await self.tool_mgr.shutdown()
|
||||||
|
|
||||||
latest_llm_model_config = await config.load_json_config("data/metadata/llm-models.json", "templates/metadata/llm-models.json")
|
latest_llm_model_config = await config.load_json_config("data/metadata/llm-models.json", "templates/metadata/llm-models.json")
|
||||||
self.llm_models_meta = latest_llm_model_config
|
self.llm_models_meta = latest_llm_model_config
|
||||||
llm_model_mgr_inst = llm_model_mgr.ModelManager(self)
|
llm_model_mgr_inst = llm_model_mgr.ModelManager(self)
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ required_deps = {
|
|||||||
"dingtalk_stream": "dingtalk_stream",
|
"dingtalk_stream": "dingtalk_stream",
|
||||||
"dashscope": "dashscope",
|
"dashscope": "dashscope",
|
||||||
"telegram": "python-telegram-bot",
|
"telegram": "python-telegram-bot",
|
||||||
|
"certifi": "certifi",
|
||||||
|
"mcp": "mcp",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
26
pkg/core/migrations/m036_wxoa_loading_message.py
Normal file
26
pkg/core/migrations/m036_wxoa_loading_message.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from .. import migration
|
||||||
|
|
||||||
|
|
||||||
|
@migration.migration_class("wxoa-loading-message", 36)
|
||||||
|
class WxoaLoadingMessageMigration(migration.Migration):
|
||||||
|
"""迁移"""
|
||||||
|
|
||||||
|
async def need_migrate(self) -> bool:
|
||||||
|
"""判断当前环境是否需要运行此迁移"""
|
||||||
|
|
||||||
|
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||||
|
if adapter['adapter'] == 'officialaccount':
|
||||||
|
if 'LoadingMessage' not in adapter:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def run(self):
|
||||||
|
"""执行迁移"""
|
||||||
|
for adapter in self.ap.platform_cfg.data['platform-adapters']:
|
||||||
|
if adapter['adapter'] == 'officialaccount':
|
||||||
|
if 'LoadingMessage' not in adapter:
|
||||||
|
adapter['LoadingMessage'] = 'AI正在思考中,请发送任意内容获取回复。'
|
||||||
|
|
||||||
|
await self.ap.platform_cfg.dump_config()
|
||||||
20
pkg/core/migrations/m037_mcp_config.py
Normal file
20
pkg/core/migrations/m037_mcp_config.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from .. import migration
|
||||||
|
|
||||||
|
|
||||||
|
@migration.migration_class("mcp-config", 37)
|
||||||
|
class MCPConfigMigration(migration.Migration):
|
||||||
|
"""迁移"""
|
||||||
|
|
||||||
|
async def need_migrate(self) -> bool:
|
||||||
|
"""判断当前环境是否需要运行此迁移"""
|
||||||
|
return 'mcp' not in self.ap.provider_cfg.data
|
||||||
|
|
||||||
|
async def run(self):
|
||||||
|
"""执行迁移"""
|
||||||
|
self.ap.provider_cfg.data['mcp'] = {
|
||||||
|
"servers": []
|
||||||
|
}
|
||||||
|
|
||||||
|
await self.ap.provider_cfg.dump_config()
|
||||||
@@ -11,7 +11,9 @@ from ..migrations import m015_gitee_ai_config, m016_dify_service_api, m017_dify_
|
|||||||
from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config, m023_siliconflow_config, m024_discord_config, m025_gewechat_config
|
from ..migrations import m020_wecom_config, m021_lark_config, m022_lmstudio_config, m023_siliconflow_config, m024_discord_config, m025_gewechat_config
|
||||||
from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config
|
from ..migrations import m026_qqofficial_config, m027_wx_official_account_config, m028_aliyun_requester_config
|
||||||
from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config
|
from ..migrations import m029_dashscope_app_api_config, m030_lark_config_cmpl, m031_dingtalk_config, m032_volcark_config
|
||||||
from ..migrations import m033_dify_thinking_config, m034_gewechat_file_url_config, m035_wxoa_mode
|
from ..migrations import m033_dify_thinking_config, m034_gewechat_file_url_config, m035_wxoa_mode, m036_wxoa_loading_message
|
||||||
|
from ..migrations import m037_mcp_config
|
||||||
|
|
||||||
|
|
||||||
@stage.stage_class("MigrationStage")
|
@stage.stage_class("MigrationStage")
|
||||||
class MigrationStage(stage.BootingStage):
|
class MigrationStage(stage.BootingStage):
|
||||||
|
|||||||
@@ -60,11 +60,14 @@ class PreProcessor(stage.PipelineStage):
|
|||||||
|
|
||||||
content_list = []
|
content_list = []
|
||||||
|
|
||||||
|
plain_text = ""
|
||||||
|
|
||||||
for me in query.message_chain:
|
for me in query.message_chain:
|
||||||
if isinstance(me, platform_message.Plain):
|
if isinstance(me, platform_message.Plain):
|
||||||
content_list.append(
|
content_list.append(
|
||||||
llm_entities.ContentElement.from_text(me.text)
|
llm_entities.ContentElement.from_text(me.text)
|
||||||
)
|
)
|
||||||
|
plain_text += me.text
|
||||||
elif isinstance(me, platform_message.Image):
|
elif isinstance(me, platform_message.Image):
|
||||||
if self.ap.provider_cfg.data['enable-vision'] and (self.ap.provider_cfg.data['runner'] != 'local-agent' or query.use_model.vision_supported):
|
if self.ap.provider_cfg.data['enable-vision'] and (self.ap.provider_cfg.data['runner'] != 'local-agent' or query.use_model.vision_supported):
|
||||||
if me.base64 is not None:
|
if me.base64 is not None:
|
||||||
@@ -72,6 +75,8 @@ class PreProcessor(stage.PipelineStage):
|
|||||||
llm_entities.ContentElement.from_image_base64(me.base64)
|
llm_entities.ContentElement.from_image_base64(me.base64)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
query.variables['user_message_text'] = plain_text
|
||||||
|
|
||||||
query.user_message = llm_entities.Message(
|
query.user_message = llm_entities.Message(
|
||||||
role='user',
|
role='user',
|
||||||
content=content_list
|
content=content_list
|
||||||
|
|||||||
@@ -170,7 +170,8 @@ class PlatformManager:
|
|||||||
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
|
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
|
||||||
|
|
||||||
tasks.append(exception_wrapper(adapter))
|
tasks.append(exception_wrapper(adapter))
|
||||||
|
|
||||||
|
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
self.ap.task_mgr.create_task(
|
self.ap.task_mgr.create_task(
|
||||||
task,
|
task,
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ from ..types import message as platform_message
|
|||||||
from ..types import events as platform_events
|
from ..types import events as platform_events
|
||||||
from ..types import entities as platform_entities
|
from ..types import entities as platform_entities
|
||||||
from ...utils import image
|
from ...utils import image
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
|
||||||
class GewechatMessageConverter(adapter.MessageConverter):
|
class GewechatMessageConverter(adapter.MessageConverter):
|
||||||
@@ -74,6 +75,7 @@ class GewechatMessageConverter(adapter.MessageConverter):
|
|||||||
if len(line_split) > 0 and regex.match(line_split[0]):
|
if len(line_split) > 0 and regex.match(line_split[0]):
|
||||||
message["Data"]["Content"]["string"] = "\n".join(line_split[1:])
|
message["Data"]["Content"]["string"] = "\n".join(line_split[1:])
|
||||||
|
|
||||||
|
|
||||||
# 正则表达式模式,匹配'@'后跟任意数量的非空白字符
|
# 正则表达式模式,匹配'@'后跟任意数量的非空白字符
|
||||||
pattern = r'@\S+'
|
pattern = r'@\S+'
|
||||||
at_string = f"@{bot_account_id}"
|
at_string = f"@{bot_account_id}"
|
||||||
@@ -82,8 +84,11 @@ class GewechatMessageConverter(adapter.MessageConverter):
|
|||||||
content_list.append(platform_message.At(target=bot_account_id))
|
content_list.append(platform_message.At(target=bot_account_id))
|
||||||
content_list.append(platform_message.Plain(message["Data"]["Content"]["string"].replace(at_string, '', 1)))
|
content_list.append(platform_message.Plain(message["Data"]["Content"]["string"].replace(at_string, '', 1)))
|
||||||
# 更优雅的替换改名后@机器人,仅仅限于单独AT的情况
|
# 更优雅的替换改名后@机器人,仅仅限于单独AT的情况
|
||||||
elif '在群聊中@了你' in message["Data"]["PushContent"]:
|
elif "PushContent" in message['Data'] and '在群聊中@了你' in message["Data"]["PushContent"]:
|
||||||
content_list.append(platform_message.At(target=bot_account_id))
|
if '@所有人' in message["Data"]["Content"]["string"]: # at全员时候传入atll不当作at自己
|
||||||
|
content_list.append(platform_message.AtAll())
|
||||||
|
else:
|
||||||
|
content_list.append(platform_message.At(target=bot_account_id))
|
||||||
content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"])))
|
content_list.append(platform_message.Plain(re.sub(pattern, '', message["Data"]["Content"]["string"])))
|
||||||
else:
|
else:
|
||||||
content_list = [platform_message.Plain(message["Data"]["Content"]["string"])]
|
content_list = [platform_message.Plain(message["Data"]["Content"]["string"])]
|
||||||
@@ -127,17 +132,78 @@ class GewechatMessageConverter(adapter.MessageConverter):
|
|||||||
# 支持微信聊天记录的消息类型,将 XML 内容转换为 MessageChain 传递
|
# 支持微信聊天记录的消息类型,将 XML 内容转换为 MessageChain 传递
|
||||||
try:
|
try:
|
||||||
content = message["Data"]["Content"]["string"]
|
content = message["Data"]["Content"]["string"]
|
||||||
|
# 有三种可能的消息结构weid开头,私聊直接<?xml>和直接<msg>
|
||||||
|
if content.startswith('wxid'):
|
||||||
|
xml_list = content.split('\n')[2:]
|
||||||
|
xml_data = '\n'.join(xml_list)
|
||||||
|
elif content.startswith('<?xml'):
|
||||||
|
xml_list = content.split('\n')[1:]
|
||||||
|
xml_data = '\n'.join(xml_list)
|
||||||
|
else:
|
||||||
|
xml_data = content
|
||||||
|
|
||||||
try:
|
content_data = ET.fromstring(xml_data)
|
||||||
content_bytes = content.encode('utf-8')
|
# print(xml_data)
|
||||||
decoded_content = base64.b64decode(content_bytes)
|
# 拿到细分消息类型,按照gewe接口中描述
|
||||||
|
'''
|
||||||
|
小程序:33/36
|
||||||
|
引用消息:57
|
||||||
|
转账消息:2000
|
||||||
|
红包消息:2001
|
||||||
|
视频号消息:51
|
||||||
|
'''
|
||||||
|
appmsg_data = content_data.find('.//appmsg')
|
||||||
|
data_type = appmsg_data.find('.//type').text
|
||||||
|
if data_type == '57':
|
||||||
|
user_data = appmsg_data.find('.//title').text # 拿到用户消息
|
||||||
|
quote_data = appmsg_data.find('.//refermsg').find('.//content').text # 引用原文
|
||||||
|
sender_id = appmsg_data.find('.//refermsg').find('.//chatusr').text # 引用用户id
|
||||||
|
from_name = message['Data']['FromUserName']['string']
|
||||||
|
message_list =[]
|
||||||
|
if message['Wxid'] == sender_id and from_name.endswith('@chatroom'): # 因为引用机制暂时无法响应用户,所以当引用用户是机器人是构建一个at激活机器人
|
||||||
|
message_list.append(platform_message.At(target=bot_account_id))
|
||||||
|
message_list.append(platform_message.Quote(
|
||||||
|
sender_id=sender_id,
|
||||||
|
origin=platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(quote_data)]
|
||||||
|
)))
|
||||||
|
message_list.append(platform_message.Plain(user_data))
|
||||||
|
return platform_message.MessageChain(message_list)
|
||||||
|
elif data_type == '51':
|
||||||
return platform_message.MessageChain(
|
return platform_message.MessageChain(
|
||||||
[platform_message.Unknown(content=decoded_content)]
|
[platform_message.Plain(text=f'[视频号消息]')]
|
||||||
)
|
)
|
||||||
except Exception as e:
|
# print(content_data)
|
||||||
|
elif data_type == '2000':
|
||||||
return platform_message.MessageChain(
|
return platform_message.MessageChain(
|
||||||
[platform_message.Plain(text=content)]
|
[platform_message.Plain(text=f'[转账消息]')]
|
||||||
)
|
)
|
||||||
|
elif data_type == '2001':
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(text=f'[红包消息]')]
|
||||||
|
)
|
||||||
|
elif data_type == '5':
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(text=f'[公众号消息]')]
|
||||||
|
)
|
||||||
|
elif data_type == '33' or data_type == '36':
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(text=f'[小程序消息]')]
|
||||||
|
)
|
||||||
|
# print(data_type.text)
|
||||||
|
else:
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
content_bytes = content.encode('utf-8')
|
||||||
|
decoded_content = base64.b64decode(content_bytes)
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Unknown(content=decoded_content)]
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return platform_message.MessageChain(
|
||||||
|
[platform_message.Plain(text=content)]
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error processing type 49 message: {str(e)}")
|
print(f"Error processing type 49 message: {str(e)}")
|
||||||
return platform_message.MessageChain(
|
return platform_message.MessageChain(
|
||||||
@@ -161,6 +227,14 @@ class GewechatEventConverter(adapter.EventConverter):
|
|||||||
event: dict,
|
event: dict,
|
||||||
bot_account_id: str
|
bot_account_id: str
|
||||||
) -> platform_events.MessageEvent:
|
) -> platform_events.MessageEvent:
|
||||||
|
# print(event)
|
||||||
|
# 排除自己发消息回调回答问题
|
||||||
|
if event['Wxid'] == event['Data']['FromUserName']['string']:
|
||||||
|
return None
|
||||||
|
# 排除公众号以及微信团队消息
|
||||||
|
if event['Data']['FromUserName']['string'].startswith('gh_')\
|
||||||
|
or event['Data']['FromUserName']['string'].startswith('weixin'):
|
||||||
|
return None
|
||||||
message_chain = await self.message_converter.target2yiri(copy.deepcopy(event), bot_account_id)
|
message_chain = await self.message_converter.target2yiri(copy.deepcopy(event), bot_account_id)
|
||||||
|
|
||||||
if not message_chain:
|
if not message_chain:
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ class OfficialAccountAdapter(adapter.MessagePlatformAdapter):
|
|||||||
EncodingAESKey=config['EncodingAESKey'],
|
EncodingAESKey=config['EncodingAESKey'],
|
||||||
Appsecret=config['AppSecret'],
|
Appsecret=config['AppSecret'],
|
||||||
AppID=config['AppID'],
|
AppID=config['AppID'],
|
||||||
|
LoadingMessage=config['LoadingMessage']
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise KeyError("请设置微信公众号通信模式")
|
raise KeyError("请设置微信公众号通信模式")
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import re
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import zipfile
|
import zipfile
|
||||||
|
import ssl
|
||||||
|
import certifi
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import aiofiles
|
import aiofiles
|
||||||
@@ -21,44 +23,39 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
|||||||
|
|
||||||
def get_github_plugin_repo_label(self, repo_url: str) -> list[str]:
|
def get_github_plugin_repo_label(self, repo_url: str) -> list[str]:
|
||||||
"""获取username, repo"""
|
"""获取username, repo"""
|
||||||
|
|
||||||
# 提取 username/repo , 正则表达式
|
|
||||||
repo = re.findall(
|
repo = re.findall(
|
||||||
r"(?:https?://github\.com/|git@github\.com:)([^/]+/[^/]+?)(?:\.git|/|$)",
|
r"(?:https?://github\.com/|git@github\.com:)([^/]+/[^/]+?)(?:\.git|/|$)",
|
||||||
repo_url,
|
repo_url,
|
||||||
)
|
)
|
||||||
|
if len(repo) > 0:
|
||||||
if len(repo) > 0: # github
|
|
||||||
return repo[0].split("/")
|
return repo[0].split("/")
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def download_plugin_source_code(self, repo_url: str, target_path: str, task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder()) -> str:
|
async def download_plugin_source_code(self, repo_url: str, target_path: str, task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder()) -> str:
|
||||||
"""下载插件源码(全异步)"""
|
"""下载插件源码(全异步)"""
|
||||||
|
|
||||||
# 提取 username/repo , 正则表达式
|
|
||||||
repo = self.get_github_plugin_repo_label(repo_url)
|
repo = self.get_github_plugin_repo_label(repo_url)
|
||||||
|
|
||||||
target_path += repo[1]
|
|
||||||
|
|
||||||
if repo is None:
|
if repo is None:
|
||||||
raise errors.PluginInstallerError('仅支持GitHub仓库地址')
|
raise errors.PluginInstallerError('仅支持GitHub仓库地址')
|
||||||
|
|
||||||
|
target_path += repo[1]
|
||||||
self.ap.logger.debug("正在下载源码...")
|
self.ap.logger.debug("正在下载源码...")
|
||||||
task_context.trace("下载源码...", "download-plugin-source-code")
|
task_context.trace("下载源码...", "download-plugin-source-code")
|
||||||
|
|
||||||
zipball_url = f"https://api.github.com/repos/{'/'.join(repo)}/zipball/HEAD"
|
zipball_url = f"https://api.github.com/repos/{'/'.join(repo)}/zipball/HEAD"
|
||||||
|
|
||||||
zip_resp: bytes = None
|
zip_resp: bytes = None
|
||||||
|
|
||||||
|
# 创建自定义SSL上下文,使用certifi提供的根证书
|
||||||
|
ssl_context = ssl.create_default_context(cafile=certifi.where())
|
||||||
|
|
||||||
async with aiohttp.ClientSession(trust_env=True) as session:
|
async with aiohttp.ClientSession(trust_env=True) as session:
|
||||||
async with session.get(
|
async with session.get(
|
||||||
url=zipball_url,
|
url=zipball_url,
|
||||||
timeout=aiohttp.ClientTimeout(total=300)
|
timeout=aiohttp.ClientTimeout(total=300),
|
||||||
|
ssl=ssl_context # 使用自定义SSL上下文来验证证书
|
||||||
) as resp:
|
) as resp:
|
||||||
if resp.status != 200:
|
if resp.status != 200:
|
||||||
raise errors.PluginInstallerError(f"下载源码失败: {resp.text}")
|
raise errors.PluginInstallerError(f"下载源码失败: {await resp.text()}")
|
||||||
|
|
||||||
zip_resp = await resp.read()
|
zip_resp = await resp.read()
|
||||||
|
|
||||||
if await aiofiles_os.path.exists("temp/" + target_path):
|
if await aiofiles_os.path.exists("temp/" + target_path):
|
||||||
@@ -80,15 +77,11 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
|||||||
await aiofiles_os.remove("temp/" + target_path + "/source.zip")
|
await aiofiles_os.remove("temp/" + target_path + "/source.zip")
|
||||||
|
|
||||||
import glob
|
import glob
|
||||||
|
|
||||||
unzip_dir = glob.glob("temp/" + target_path + "/*")[0]
|
unzip_dir = glob.glob("temp/" + target_path + "/*")[0]
|
||||||
|
|
||||||
await aioshutil.copytree(unzip_dir, target_path + "/")
|
await aioshutil.copytree(unzip_dir, target_path + "/")
|
||||||
|
|
||||||
await aioshutil.rmtree(unzip_dir)
|
await aioshutil.rmtree(unzip_dir)
|
||||||
|
|
||||||
self.ap.logger.debug("源码下载完成。")
|
self.ap.logger.debug("源码下载完成。")
|
||||||
|
|
||||||
return repo[1]
|
return repo[1]
|
||||||
|
|
||||||
async def install_requirements(self, path: str):
|
async def install_requirements(self, path: str):
|
||||||
@@ -100,20 +93,14 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
|||||||
plugin_source: str,
|
plugin_source: str,
|
||||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||||
):
|
):
|
||||||
"""安装插件
|
"""安装插件"""
|
||||||
"""
|
|
||||||
task_context.trace("下载插件源码...", "install-plugin")
|
task_context.trace("下载插件源码...", "install-plugin")
|
||||||
|
|
||||||
repo_label = await self.download_plugin_source_code(plugin_source, "plugins/", task_context)
|
repo_label = await self.download_plugin_source_code(plugin_source, "plugins/", task_context)
|
||||||
|
|
||||||
task_context.trace("安装插件依赖...", "install-plugin")
|
task_context.trace("安装插件依赖...", "install-plugin")
|
||||||
|
|
||||||
await self.install_requirements("plugins/" + repo_label)
|
await self.install_requirements("plugins/" + repo_label)
|
||||||
|
|
||||||
task_context.trace("完成.", "install-plugin")
|
task_context.trace("完成.", "install-plugin")
|
||||||
|
|
||||||
await self.ap.plugin_mgr.setting.record_installed_plugin_source(
|
await self.ap.plugin_mgr.setting.record_installed_plugin_source(
|
||||||
"plugins/"+repo_label+'/', plugin_source
|
"plugins/" + repo_label + '/', plugin_source
|
||||||
)
|
)
|
||||||
|
|
||||||
async def uninstall_plugin(
|
async def uninstall_plugin(
|
||||||
@@ -121,10 +108,8 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
|||||||
plugin_name: str,
|
plugin_name: str,
|
||||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||||
):
|
):
|
||||||
"""卸载插件
|
"""卸载插件"""
|
||||||
"""
|
|
||||||
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
|
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
|
||||||
|
|
||||||
if plugin_container is None:
|
if plugin_container is None:
|
||||||
raise errors.PluginInstallerError('插件不存在或未成功加载')
|
raise errors.PluginInstallerError('插件不存在或未成功加载')
|
||||||
else:
|
else:
|
||||||
@@ -135,24 +120,18 @@ class GitHubRepoInstaller(installer.PluginInstaller):
|
|||||||
async def update_plugin(
|
async def update_plugin(
|
||||||
self,
|
self,
|
||||||
plugin_name: str,
|
plugin_name: str,
|
||||||
plugin_source: str=None,
|
plugin_source: str = None,
|
||||||
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
task_context: taskmgr.TaskContext = taskmgr.TaskContext.placeholder(),
|
||||||
):
|
):
|
||||||
"""更新插件
|
"""更新插件"""
|
||||||
"""
|
|
||||||
task_context.trace("更新插件...", "update-plugin")
|
task_context.trace("更新插件...", "update-plugin")
|
||||||
|
|
||||||
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
|
plugin_container = self.ap.plugin_mgr.get_plugin_by_name(plugin_name)
|
||||||
|
|
||||||
if plugin_container is None:
|
if plugin_container is None:
|
||||||
raise errors.PluginInstallerError('插件不存在或未成功加载')
|
raise errors.PluginInstallerError('插件不存在或未成功加载')
|
||||||
else:
|
else:
|
||||||
if plugin_container.plugin_source:
|
if plugin_container.plugin_source:
|
||||||
plugin_source = plugin_container.plugin_source
|
plugin_source = plugin_container.plugin_source
|
||||||
|
|
||||||
task_context.trace("转交安装任务.", "update-plugin")
|
task_context.trace("转交安装任务.", "update-plugin")
|
||||||
|
|
||||||
await self.install_plugin(plugin_source, task_context)
|
await self.install_plugin(plugin_source, task_context)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise errors.PluginInstallerError('插件无源码信息,无法更新')
|
raise errors.PluginInstallerError('插件无源码信息,无法更新')
|
||||||
@@ -54,7 +54,6 @@ class SessionManager:
|
|||||||
use_model=await self.ap.model_mgr.get_model_by_name(self.ap.provider_cfg.data['model']),
|
use_model=await self.ap.model_mgr.get_model_by_name(self.ap.provider_cfg.data['model']),
|
||||||
use_funcs=await self.ap.tool_mgr.get_all_functions(
|
use_funcs=await self.ap.tool_mgr.get_all_functions(
|
||||||
plugin_enabled=True,
|
plugin_enabled=True,
|
||||||
plugin_status=plugin_context.RuntimeContainerStatus.INITIALIZED,
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
session.conversations.append(conversation)
|
session.conversations.append(conversation)
|
||||||
|
|||||||
54
pkg/provider/tools/loader.py
Normal file
54
pkg/provider/tools/loader.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import abc
|
||||||
|
import typing
|
||||||
|
|
||||||
|
from ...core import app, entities as core_entities
|
||||||
|
from . import entities as tools_entities
|
||||||
|
|
||||||
|
|
||||||
|
preregistered_loaders: list[typing.Type[ToolLoader]] = []
|
||||||
|
|
||||||
|
def loader_class(name: str):
|
||||||
|
"""注册一个工具加载器
|
||||||
|
"""
|
||||||
|
def decorator(cls: typing.Type[ToolLoader]) -> typing.Type[ToolLoader]:
|
||||||
|
cls.name = name
|
||||||
|
preregistered_loaders.append(cls)
|
||||||
|
return cls
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
class ToolLoader(abc.ABC):
|
||||||
|
"""工具加载器"""
|
||||||
|
|
||||||
|
name: str = None
|
||||||
|
|
||||||
|
ap: app.Application
|
||||||
|
|
||||||
|
def __init__(self, ap: app.Application):
|
||||||
|
self.ap = ap
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
async def get_tools(self, enabled: bool=True) -> list[tools_entities.LLMFunction]:
|
||||||
|
"""获取所有工具"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
async def has_tool(self, name: str) -> bool:
|
||||||
|
"""检查工具是否存在"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
async def invoke_tool(self, query: core_entities.Query, name: str, parameters: dict) -> typing.Any:
|
||||||
|
"""执行工具调用"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
async def shutdown(self):
|
||||||
|
"""关闭工具"""
|
||||||
|
pass
|
||||||
0
pkg/provider/tools/loaders/__init__.py
Normal file
0
pkg/provider/tools/loaders/__init__.py
Normal file
161
pkg/provider/tools/loaders/mcp.py
Normal file
161
pkg/provider/tools/loaders/mcp.py
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing
|
||||||
|
from contextlib import AsyncExitStack
|
||||||
|
|
||||||
|
from mcp import ClientSession, StdioServerParameters
|
||||||
|
from mcp.client.stdio import stdio_client
|
||||||
|
from mcp.client.sse import sse_client
|
||||||
|
|
||||||
|
from .. import loader, entities as tools_entities
|
||||||
|
from ....core import app, entities as core_entities
|
||||||
|
|
||||||
|
|
||||||
|
class RuntimeMCPSession:
|
||||||
|
"""运行时 MCP 会话"""
|
||||||
|
|
||||||
|
ap: app.Application
|
||||||
|
|
||||||
|
server_name: str
|
||||||
|
|
||||||
|
server_config: dict
|
||||||
|
|
||||||
|
session: ClientSession
|
||||||
|
|
||||||
|
exit_stack: AsyncExitStack
|
||||||
|
|
||||||
|
functions: list[tools_entities.LLMFunction] = []
|
||||||
|
|
||||||
|
def __init__(self, server_name: str, server_config: dict, ap: app.Application):
|
||||||
|
self.server_name = server_name
|
||||||
|
self.server_config = server_config
|
||||||
|
self.ap = ap
|
||||||
|
|
||||||
|
self.session = None
|
||||||
|
|
||||||
|
self.exit_stack = AsyncExitStack()
|
||||||
|
self.functions = []
|
||||||
|
|
||||||
|
async def _init_stdio_python_server(self):
|
||||||
|
server_params = StdioServerParameters(
|
||||||
|
command=self.server_config["command"],
|
||||||
|
args=self.server_config["args"],
|
||||||
|
env=self.server_config["env"],
|
||||||
|
)
|
||||||
|
|
||||||
|
stdio_transport = await self.exit_stack.enter_async_context(
|
||||||
|
stdio_client(server_params)
|
||||||
|
)
|
||||||
|
|
||||||
|
stdio, write = stdio_transport
|
||||||
|
|
||||||
|
self.session = await self.exit_stack.enter_async_context(
|
||||||
|
ClientSession(stdio, write)
|
||||||
|
)
|
||||||
|
|
||||||
|
await self.session.initialize()
|
||||||
|
|
||||||
|
async def _init_sse_server(self):
|
||||||
|
sse_transport = await self.exit_stack.enter_async_context(
|
||||||
|
sse_client(
|
||||||
|
self.server_config["url"],
|
||||||
|
headers=self.server_config.get("headers", {}),
|
||||||
|
timeout=self.server_config.get("timeout", 10),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
sseio, write = sse_transport
|
||||||
|
|
||||||
|
self.session = await self.exit_stack.enter_async_context(
|
||||||
|
ClientSession(sseio, write)
|
||||||
|
)
|
||||||
|
|
||||||
|
await self.session.initialize()
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
self.ap.logger.debug(f"初始化 MCP 会话: {self.server_name} {self.server_config}")
|
||||||
|
|
||||||
|
if self.server_config["mode"] == "stdio":
|
||||||
|
await self._init_stdio_python_server()
|
||||||
|
elif self.server_config["mode"] == "sse":
|
||||||
|
await self._init_sse_server()
|
||||||
|
else:
|
||||||
|
raise ValueError(f"无法识别 MCP 服务器类型: {self.server_name}: {self.server_config}")
|
||||||
|
|
||||||
|
tools = await self.session.list_tools()
|
||||||
|
|
||||||
|
self.ap.logger.debug(f"获取 MCP 工具: {tools}")
|
||||||
|
|
||||||
|
for tool in tools.tools:
|
||||||
|
|
||||||
|
async def func(query: core_entities.Query, **kwargs):
|
||||||
|
result = await self.session.call_tool(tool.name, kwargs)
|
||||||
|
if result.isError:
|
||||||
|
raise Exception(result.content[0].text)
|
||||||
|
return result.content[0].text
|
||||||
|
|
||||||
|
func.__name__ = tool.name
|
||||||
|
|
||||||
|
self.functions.append(tools_entities.LLMFunction(
|
||||||
|
name=tool.name,
|
||||||
|
human_desc=tool.description,
|
||||||
|
description=tool.description,
|
||||||
|
parameters=tool.inputSchema,
|
||||||
|
func=func,
|
||||||
|
))
|
||||||
|
|
||||||
|
async def shutdown(self):
|
||||||
|
"""关闭工具"""
|
||||||
|
await self.session._exit_stack.aclose()
|
||||||
|
|
||||||
|
@loader.loader_class("mcp")
|
||||||
|
class MCPLoader(loader.ToolLoader):
|
||||||
|
"""MCP 工具加载器。
|
||||||
|
|
||||||
|
在此加载器中管理所有与 MCP Server 的连接。
|
||||||
|
"""
|
||||||
|
|
||||||
|
sessions: dict[str, RuntimeMCPSession] = {}
|
||||||
|
|
||||||
|
_last_listed_functions: list[tools_entities.LLMFunction] = []
|
||||||
|
|
||||||
|
def __init__(self, ap: app.Application):
|
||||||
|
super().__init__(ap)
|
||||||
|
self.sessions = {}
|
||||||
|
self._last_listed_functions = []
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
|
||||||
|
for server_config in self.ap.provider_cfg.data.get("mcp", {}).get("servers", []):
|
||||||
|
if not server_config["enable"]:
|
||||||
|
continue
|
||||||
|
session = RuntimeMCPSession(server_config["name"], server_config, self.ap)
|
||||||
|
await session.initialize()
|
||||||
|
# self.ap.event_loop.create_task(session.initialize())
|
||||||
|
self.sessions[server_config["name"]] = session
|
||||||
|
|
||||||
|
async def get_tools(self, enabled: bool=True) -> list[tools_entities.LLMFunction]:
|
||||||
|
all_functions = []
|
||||||
|
|
||||||
|
for session in self.sessions.values():
|
||||||
|
all_functions.extend(session.functions)
|
||||||
|
|
||||||
|
self._last_listed_functions = all_functions
|
||||||
|
|
||||||
|
return all_functions
|
||||||
|
|
||||||
|
async def has_tool(self, name: str) -> bool:
|
||||||
|
return name in [f.name for f in self._last_listed_functions]
|
||||||
|
|
||||||
|
async def invoke_tool(self, query: core_entities.Query, name: str, parameters: dict) -> typing.Any:
|
||||||
|
for server_name, session in self.sessions.items():
|
||||||
|
for function in session.functions:
|
||||||
|
if function.name == name:
|
||||||
|
return await function.func(query, **parameters)
|
||||||
|
|
||||||
|
raise ValueError(f"未找到工具: {name}")
|
||||||
|
|
||||||
|
async def shutdown(self):
|
||||||
|
"""关闭工具"""
|
||||||
|
for session in self.sessions.values():
|
||||||
|
await session.shutdown()
|
||||||
92
pkg/provider/tools/loaders/plugin.py
Normal file
92
pkg/provider/tools/loaders/plugin.py
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import typing
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from .. import loader, entities as tools_entities
|
||||||
|
from ....core import app, entities as core_entities
|
||||||
|
from ....plugin import context as plugin_context
|
||||||
|
|
||||||
|
|
||||||
|
@loader.loader_class("plugin-tool-loader")
|
||||||
|
class PluginToolLoader(loader.ToolLoader):
|
||||||
|
"""插件工具加载器。
|
||||||
|
|
||||||
|
本加载器中不存储工具信息,仅负责从插件系统中获取工具信息。
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def get_tools(self, enabled: bool=True) -> list[tools_entities.LLMFunction]:
|
||||||
|
|
||||||
|
# 从插件系统获取工具(内容函数)
|
||||||
|
all_functions: list[tools_entities.LLMFunction] = []
|
||||||
|
|
||||||
|
for plugin in self.ap.plugin_mgr.plugins(
|
||||||
|
enabled=enabled, status=plugin_context.RuntimeContainerStatus.INITIALIZED
|
||||||
|
):
|
||||||
|
all_functions.extend(plugin.content_functions)
|
||||||
|
|
||||||
|
return all_functions
|
||||||
|
|
||||||
|
async def has_tool(self, name: str) -> bool:
|
||||||
|
"""检查工具是否存在"""
|
||||||
|
for plugin in self.ap.plugin_mgr.plugins(
|
||||||
|
enabled=True, status=plugin_context.RuntimeContainerStatus.INITIALIZED
|
||||||
|
):
|
||||||
|
for function in plugin.content_functions:
|
||||||
|
if function.name == name:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def _get_function_and_plugin(
|
||||||
|
self, name: str
|
||||||
|
) -> typing.Tuple[tools_entities.LLMFunction, plugin_context.BasePlugin]:
|
||||||
|
"""获取函数和插件实例"""
|
||||||
|
for plugin in self.ap.plugin_mgr.plugins(
|
||||||
|
enabled=True, status=plugin_context.RuntimeContainerStatus.INITIALIZED
|
||||||
|
):
|
||||||
|
for function in plugin.content_functions:
|
||||||
|
if function.name == name:
|
||||||
|
return function, plugin.plugin_inst
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
async def invoke_tool(self, query: core_entities.Query, name: str, parameters: dict) -> typing.Any:
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
function, plugin = await self._get_function_and_plugin(name)
|
||||||
|
if function is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
parameters = parameters.copy()
|
||||||
|
|
||||||
|
parameters = {"query": query, **parameters}
|
||||||
|
|
||||||
|
return await function.func(plugin, **parameters)
|
||||||
|
except Exception as e:
|
||||||
|
self.ap.logger.error(f"执行函数 {name} 时发生错误: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
return f"error occurred when executing function {name}: {e}"
|
||||||
|
finally:
|
||||||
|
plugin = None
|
||||||
|
|
||||||
|
for p in self.ap.plugin_mgr.plugins():
|
||||||
|
if function in p.content_functions:
|
||||||
|
plugin = p
|
||||||
|
break
|
||||||
|
|
||||||
|
if plugin is not None:
|
||||||
|
|
||||||
|
await self.ap.ctr_mgr.usage.post_function_record(
|
||||||
|
plugin={
|
||||||
|
"name": plugin.plugin_name,
|
||||||
|
"remote": plugin.plugin_source,
|
||||||
|
"version": plugin.plugin_version,
|
||||||
|
"author": plugin.plugin_author,
|
||||||
|
},
|
||||||
|
function_name=function.name,
|
||||||
|
function_description=function.description,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def shutdown(self):
|
||||||
|
"""关闭工具"""
|
||||||
|
pass
|
||||||
@@ -4,8 +4,9 @@ import typing
|
|||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from ...core import app, entities as core_entities
|
from ...core import app, entities as core_entities
|
||||||
from . import entities
|
from . import entities, loader as tools_loader
|
||||||
from ...plugin import context as plugin_context
|
from ...plugin import context as plugin_context
|
||||||
|
from .loaders import plugin, mcp
|
||||||
|
|
||||||
|
|
||||||
class ToolManager:
|
class ToolManager:
|
||||||
@@ -13,33 +14,26 @@ class ToolManager:
|
|||||||
|
|
||||||
ap: app.Application
|
ap: app.Application
|
||||||
|
|
||||||
|
loaders: list[tools_loader.ToolLoader]
|
||||||
|
|
||||||
def __init__(self, ap: app.Application):
|
def __init__(self, ap: app.Application):
|
||||||
self.ap = ap
|
self.ap = ap
|
||||||
self.all_functions = []
|
self.all_functions = []
|
||||||
|
self.loaders = []
|
||||||
|
|
||||||
async def initialize(self):
|
async def initialize(self):
|
||||||
pass
|
|
||||||
|
|
||||||
async def get_function_and_plugin(
|
for loader_cls in tools_loader.preregistered_loaders:
|
||||||
self, name: str
|
loader_inst = loader_cls(self.ap)
|
||||||
) -> typing.Tuple[entities.LLMFunction, plugin_context.BasePlugin]:
|
await loader_inst.initialize()
|
||||||
"""获取函数和插件实例"""
|
self.loaders.append(loader_inst)
|
||||||
for plugin in self.ap.plugin_mgr.plugins(
|
|
||||||
enabled=True, status=plugin_context.RuntimeContainerStatus.INITIALIZED
|
|
||||||
):
|
|
||||||
for function in plugin.content_functions:
|
|
||||||
if function.name == name:
|
|
||||||
return function, plugin.plugin_inst
|
|
||||||
return None, None
|
|
||||||
|
|
||||||
async def get_all_functions(self, plugin_enabled: bool=None, plugin_status: plugin_context.RuntimeContainerStatus=None) -> list[entities.LLMFunction]:
|
async def get_all_functions(self, plugin_enabled: bool=None) -> list[entities.LLMFunction]:
|
||||||
"""获取所有函数"""
|
"""获取所有函数"""
|
||||||
all_functions: list[entities.LLMFunction] = []
|
all_functions: list[entities.LLMFunction] = []
|
||||||
|
|
||||||
for plugin in self.ap.plugin_mgr.plugins(
|
for loader in self.loaders:
|
||||||
enabled=plugin_enabled, status=plugin_status
|
all_functions.extend(await loader.get_tools(plugin_enabled))
|
||||||
):
|
|
||||||
all_functions.extend(plugin.content_functions)
|
|
||||||
|
|
||||||
return all_functions
|
return all_functions
|
||||||
|
|
||||||
@@ -102,38 +96,13 @@ class ToolManager:
|
|||||||
) -> typing.Any:
|
) -> typing.Any:
|
||||||
"""执行函数调用"""
|
"""执行函数调用"""
|
||||||
|
|
||||||
try:
|
for loader in self.loaders:
|
||||||
|
if await loader.has_tool(name):
|
||||||
|
return await loader.invoke_tool(query, name, parameters)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"未找到工具: {name}")
|
||||||
|
|
||||||
function, plugin = await self.get_function_and_plugin(name)
|
async def shutdown(self):
|
||||||
if function is None:
|
"""关闭所有工具"""
|
||||||
return None
|
for loader in self.loaders:
|
||||||
|
await loader.shutdown()
|
||||||
parameters = parameters.copy()
|
|
||||||
|
|
||||||
parameters = {"query": query, **parameters}
|
|
||||||
|
|
||||||
return await function.func(plugin, **parameters)
|
|
||||||
except Exception as e:
|
|
||||||
self.ap.logger.error(f"执行函数 {name} 时发生错误: {e}")
|
|
||||||
traceback.print_exc()
|
|
||||||
return f"error occurred when executing function {name}: {e}"
|
|
||||||
finally:
|
|
||||||
plugin = None
|
|
||||||
|
|
||||||
for p in self.ap.plugin_mgr.plugins():
|
|
||||||
if function in p.content_functions:
|
|
||||||
plugin = p
|
|
||||||
break
|
|
||||||
|
|
||||||
if plugin is not None:
|
|
||||||
|
|
||||||
await self.ap.ctr_mgr.usage.post_function_record(
|
|
||||||
plugin={
|
|
||||||
"name": plugin.plugin_name,
|
|
||||||
"remote": plugin.plugin_source,
|
|
||||||
"version": plugin.plugin_version,
|
|
||||||
"author": plugin.plugin_author,
|
|
||||||
},
|
|
||||||
function_name=function.name,
|
|
||||||
function_description=function.description,
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
semantic_version = "v3.4.10.4"
|
semantic_version = "v3.4.11"
|
||||||
|
|
||||||
debug_mode = False
|
debug_mode = False
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,9 @@ import asyncio
|
|||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def get_gewechat_image_base64(
|
async def get_gewechat_image_base64(
|
||||||
gewechat_url: str,
|
gewechat_url: str,
|
||||||
gewechat_file_url: str,
|
gewechat_file_url: str,
|
||||||
@@ -67,6 +70,7 @@ async def get_gewechat_image_base64(
|
|||||||
}
|
}
|
||||||
) as response:
|
) as response:
|
||||||
if response.status != 200:
|
if response.status != 200:
|
||||||
|
# print(response)
|
||||||
raise Exception(f"获取gewechat图片下载失败: {await response.text()}")
|
raise Exception(f"获取gewechat图片下载失败: {await response.text()}")
|
||||||
|
|
||||||
resp_data = await response.json()
|
resp_data = await response.json()
|
||||||
@@ -108,6 +112,9 @@ async def get_gewechat_image_base64(
|
|||||||
raise Exception(f"获取图片失败: {str(e)}") from e
|
raise Exception(f"获取图片失败: {str(e)}") from e
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async def get_wecom_image_base64(pic_url: str) -> tuple[str, str]:
|
async def get_wecom_image_base64(pic_url: str) -> tuple[str, str]:
|
||||||
"""
|
"""
|
||||||
下载企业微信图片并转换为 base64
|
下载企业微信图片并转换为 base64
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ gewechat-client
|
|||||||
dingtalk_stream
|
dingtalk_stream
|
||||||
dashscope
|
dashscope
|
||||||
python-telegram-bot
|
python-telegram-bot
|
||||||
|
certifi
|
||||||
|
mcp
|
||||||
|
|
||||||
# indirect
|
# indirect
|
||||||
taskgroup==0.0.0a4
|
taskgroup==0.0.0a4
|
||||||
@@ -78,6 +78,7 @@
|
|||||||
"AppID":"",
|
"AppID":"",
|
||||||
"AppSecret":"",
|
"AppSecret":"",
|
||||||
"Mode":"drop",
|
"Mode":"drop",
|
||||||
|
"LoadingMessage":"AI正在思考中,请发送任意内容获取回复。",
|
||||||
"host": "0.0.0.0",
|
"host": "0.0.0.0",
|
||||||
"port": 2287
|
"port": 2287
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -138,5 +138,8 @@
|
|||||||
"date": "2023-08-10"
|
"date": "2023-08-10"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"mcp": {
|
||||||
|
"servers": []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -391,6 +391,11 @@
|
|||||||
"description": "对于超过15s的响应的处理模式",
|
"description": "对于超过15s的响应的处理模式",
|
||||||
"enum": ["drop", "passive"]
|
"enum": ["drop", "passive"]
|
||||||
},
|
},
|
||||||
|
"LoadingMessage": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "AI正在思考中,请发送任意内容获取回复。",
|
||||||
|
"description": "当使用被动模式时,显示给用户的提示信息"
|
||||||
|
},
|
||||||
"host": {
|
"host": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"default": "0.0.0.0",
|
"default": "0.0.0.0",
|
||||||
|
|||||||
@@ -520,6 +520,87 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"mcp": {
|
||||||
|
"type": "object",
|
||||||
|
"title": "MCP 配置",
|
||||||
|
"properties": {
|
||||||
|
"servers": {
|
||||||
|
"type": "array",
|
||||||
|
"title": "MCP 服务器配置",
|
||||||
|
"default": [],
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"title": "Stdio 模式服务器",
|
||||||
|
"properties": {
|
||||||
|
"mode": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "模式",
|
||||||
|
"const": "stdio"
|
||||||
|
},
|
||||||
|
"enable": {
|
||||||
|
"type": "boolean",
|
||||||
|
"title": "启用"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "名称"
|
||||||
|
},
|
||||||
|
"command": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "启动命令"
|
||||||
|
},
|
||||||
|
"args": {
|
||||||
|
"type": "array",
|
||||||
|
"title": "启动参数",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"default": []
|
||||||
|
},
|
||||||
|
"env": {
|
||||||
|
"type": "object",
|
||||||
|
"default": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"title": "SSE 模式服务器",
|
||||||
|
"properties": {
|
||||||
|
"mode": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "模式",
|
||||||
|
"const": "sse"
|
||||||
|
},
|
||||||
|
"enable": {
|
||||||
|
"type": "boolean",
|
||||||
|
"title": "启用"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "名称"
|
||||||
|
},
|
||||||
|
"url": {
|
||||||
|
"type": "string",
|
||||||
|
"title": "URL"
|
||||||
|
},
|
||||||
|
"headers": {
|
||||||
|
"type": "object",
|
||||||
|
"default": {}
|
||||||
|
},
|
||||||
|
"timeout": {
|
||||||
|
"type": "number",
|
||||||
|
"title": "请求超时时间",
|
||||||
|
"default": 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user