@@ -1,14 +1,26 @@
|
||||
# Changelog
|
||||
|
||||
## [0.9.1] - 2025-7-25
|
||||
## [0.9.1] - 2025-7-26
|
||||
|
||||
### 主要修复和优化
|
||||
|
||||
- 优化回复意愿
|
||||
- 优化专注模式回复频率
|
||||
- 优化关键词提取
|
||||
- 修复部分模型产生的400问题
|
||||
|
||||
### 细节优化
|
||||
|
||||
- 修复reply导致的planner异常空跳
|
||||
- 修复表达方式迁移空目录问题
|
||||
- 修复reply_to空字段问题
|
||||
- 无可用动作导致的空plan问题
|
||||
- 修复人格未压缩导致产生句号分割
|
||||
- 将metioned bot 和 at应用到focus prompt中
|
||||
- 更好的兴趣度计算
|
||||
- 修复部分模型由于enable_thinking导致的400问题
|
||||
- 优化关键词提取
|
||||
- 移除dependency_manager
|
||||
|
||||
|
||||
## [0.9.0] - 2025-7-24
|
||||
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
6. 增加了插件和组件管理的API。
|
||||
7. `BaseCommand`的`execute`方法现在返回一个三元组,包含是否执行成功、可选的回复消息和是否拦截消息。
|
||||
- 这意味着你终于可以动态控制是否继续后续消息的处理了。
|
||||
8. 移除了dependency_manager,但是依然保留了`python_dependencies`属性,等待后续重构。
|
||||
- 一并移除了文档有关manager的内容。
|
||||
|
||||
# 插件系统修改
|
||||
1. 现在所有的匹配模式不再是关键字了,而是枚举类。**(可能有遗漏)**
|
||||
|
||||
@@ -22,7 +22,7 @@ class ExampleAction(BaseAction):
|
||||
action_name = "example_action" # 动作的唯一标识符
|
||||
action_description = "这是一个示例动作" # 动作描述
|
||||
activation_type = ActionActivationType.ALWAYS # 这里以 ALWAYS 为例
|
||||
mode_enable = ChatMode.ALL # 这里以 ALL 为例
|
||||
mode_enable = ChatMode.ALL # 一般取ALL,表示在所有聊天模式下都可用
|
||||
associated_types = ["text", "emoji", ...] # 关联类型
|
||||
parallel_action = False # 是否允许与其他Action并行执行
|
||||
action_parameters = {"param1": "参数1的说明", "param2": "参数2的说明", ...}
|
||||
@@ -60,7 +60,7 @@ class ExampleAction(BaseAction):
|
||||
**请知悉,对于不同的处理器,其支持的消息类型可能会有所不同。在开发时请注意。**
|
||||
|
||||
#### action_parameters: 该Action的参数说明。
|
||||
这是一个字典,键为参数名,值为参数说明。这个字段可以帮助LLM理解如何使用这个Action,并由LLM返回对应的参数,最后传递到 Action 的 action_data 属性中。其格式与你定义的格式完全相同 **(除非LLM哈气了,返回了错误的内容)**。
|
||||
这是一个字典,键为参数名,值为参数说明。这个字段可以帮助LLM理解如何使用这个Action,并由LLM返回对应的参数,最后传递到 Action 的 **`action_data`** 属性中。其格式与你定义的格式完全相同 **(除非LLM哈气了,返回了错误的内容)**。
|
||||
|
||||
---
|
||||
|
||||
@@ -180,6 +180,8 @@ class GreetingAction(BaseAction):
|
||||
return True, "发送了问候"
|
||||
```
|
||||
|
||||
一个完整的使用`ActionActivationType.KEYWORD`的例子请参考`plugins/hello_world_plugin`中的`ByeAction`。
|
||||
|
||||
#### 第二层:使用决策
|
||||
|
||||
**在Action被激活后,使用条件决定麦麦什么时候会"选择"使用这个Action**。
|
||||
|
||||
@@ -5,147 +5,126 @@
|
||||
## 导入方式
|
||||
|
||||
```python
|
||||
from src.plugin_system.apis import chat_api
|
||||
from src.plugin_system import chat_api
|
||||
# 或者
|
||||
from src.plugin_system.apis.chat_api import ChatManager as chat
|
||||
from src.plugin_system.apis import chat_api
|
||||
```
|
||||
|
||||
一种**Deprecated**方式:
|
||||
```python
|
||||
from src.plugin_system.apis.chat_api import ChatManager
|
||||
```
|
||||
|
||||
## 主要功能
|
||||
|
||||
### 1. 获取聊天流
|
||||
### 1. 获取所有的聊天流
|
||||
|
||||
#### `get_all_streams(platform: str = "qq") -> List[ChatStream]`
|
||||
获取所有聊天流
|
||||
```python
|
||||
def get_all_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
```
|
||||
|
||||
**参数:**
|
||||
- `platform`:平台筛选,默认为"qq"
|
||||
**Args**:
|
||||
- `platform`:平台筛选,默认为"qq",可以使用`SpecialTypes`枚举类中的`SpecialTypes.ALL_PLATFORMS`来获取所有平台的聊天流。
|
||||
|
||||
**返回:**
|
||||
**Returns**:
|
||||
- `List[ChatStream]`:聊天流列表
|
||||
|
||||
**示例:**
|
||||
### 2. 获取群聊聊天流
|
||||
|
||||
```python
|
||||
streams = chat_api.get_all_streams()
|
||||
for stream in streams:
|
||||
print(f"聊天流ID: {stream.stream_id}")
|
||||
def get_group_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
```
|
||||
|
||||
#### `get_group_streams(platform: str = "qq") -> List[ChatStream]`
|
||||
获取所有群聊聊天流
|
||||
**Args**:
|
||||
- `platform`:平台筛选,默认为"qq",可以使用`SpecialTypes`枚举类中的`SpecialTypes.ALL_PLATFORMS`来获取所有平台的群聊流。
|
||||
|
||||
**参数:**
|
||||
- `platform`:平台筛选,默认为"qq"
|
||||
|
||||
**返回:**
|
||||
**Returns**:
|
||||
- `List[ChatStream]`:群聊聊天流列表
|
||||
|
||||
#### `get_private_streams(platform: str = "qq") -> List[ChatStream]`
|
||||
获取所有私聊聊天流
|
||||
### 3. 获取私聊聊天流
|
||||
|
||||
**参数:**
|
||||
- `platform`:平台筛选,默认为"qq"
|
||||
```python
|
||||
def get_private_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
```
|
||||
|
||||
**返回:**
|
||||
**Args**:
|
||||
- `platform`:平台筛选,默认为"qq",可以使用`SpecialTypes`枚举类中的`SpecialTypes.ALL_PLATFORMS`来获取所有平台的私聊流。
|
||||
|
||||
**Returns**:
|
||||
- `List[ChatStream]`:私聊聊天流列表
|
||||
|
||||
### 2. 查找特定聊天流
|
||||
### 4. 根据群ID获取聊天流
|
||||
|
||||
#### `get_stream_by_group_id(group_id: str, platform: str = "qq") -> Optional[ChatStream]`
|
||||
根据群ID获取聊天流
|
||||
```python
|
||||
def get_stream_by_group_id(group_id: str, platform: Optional[str] | SpecialTypes = "qq") -> Optional[ChatStream]:
|
||||
```
|
||||
|
||||
**参数:**
|
||||
**Args**:
|
||||
- `group_id`:群聊ID
|
||||
- `platform`:平台,默认为"qq"
|
||||
- `platform`:平台筛选,默认为"qq",可以使用`SpecialTypes`枚举类中的`SpecialTypes.ALL_PLATFORMS`来获取所有平台的群聊流。
|
||||
|
||||
**返回:**
|
||||
**Returns**:
|
||||
- `Optional[ChatStream]`:聊天流对象,如果未找到返回None
|
||||
|
||||
**示例:**
|
||||
### 5. 根据用户ID获取私聊流
|
||||
|
||||
```python
|
||||
chat_stream = chat_api.get_stream_by_group_id("123456789")
|
||||
if chat_stream:
|
||||
print(f"找到群聊: {chat_stream.group_info.group_name}")
|
||||
def get_stream_by_user_id(user_id: str, platform: Optional[str] | SpecialTypes = "qq") -> Optional[ChatStream]:
|
||||
```
|
||||
|
||||
#### `get_stream_by_user_id(user_id: str, platform: str = "qq") -> Optional[ChatStream]`
|
||||
根据用户ID获取私聊流
|
||||
|
||||
**参数:**
|
||||
**Args**:
|
||||
- `user_id`:用户ID
|
||||
- `platform`:平台,默认为"qq"
|
||||
- `platform`:平台筛选,默认为"qq",可以使用`SpecialTypes`枚举类中的`SpecialTypes.ALL_PLATFORMS`来获取所有平台的私聊流。
|
||||
|
||||
**返回:**
|
||||
**Returns**:
|
||||
- `Optional[ChatStream]`:聊天流对象,如果未找到返回None
|
||||
|
||||
### 3. 聊天流信息查询
|
||||
### 6. 获取聊天流类型
|
||||
|
||||
#### `get_stream_type(chat_stream: ChatStream) -> str`
|
||||
获取聊天流类型
|
||||
```python
|
||||
def get_stream_type(chat_stream: ChatStream) -> str:
|
||||
```
|
||||
|
||||
**参数:**
|
||||
**Args**:
|
||||
- `chat_stream`:聊天流对象
|
||||
|
||||
**返回:**
|
||||
- `str`:聊天类型 ("group", "private", "unknown")
|
||||
**Returns**:
|
||||
- `str`:聊天流类型,可能的值包括`private`(私聊流),`group`(群聊流)以及`unknown`(未知类型)。
|
||||
|
||||
#### `get_stream_info(chat_stream: ChatStream) -> Dict[str, Any]`
|
||||
获取聊天流详细信息
|
||||
### 7. 获取聊天流信息
|
||||
|
||||
**参数:**
|
||||
```python
|
||||
def get_stream_info(chat_stream: ChatStream) -> Dict[str, Any]:
|
||||
```
|
||||
|
||||
**Args**:
|
||||
- `chat_stream`:聊天流对象
|
||||
|
||||
**返回:**
|
||||
- `Dict[str, Any]`:聊天流信息字典,包含stream_id、platform、type等信息
|
||||
**Returns**:
|
||||
- `Dict[str, Any]`:聊天流的详细信息,包括但不限于:
|
||||
- `stream_id`:聊天流ID
|
||||
- `platform`:平台名称
|
||||
- `type`:聊天流类型
|
||||
- `group_id`:群聊ID
|
||||
- `group_name`:群聊名称
|
||||
- `user_id`:用户ID
|
||||
- `user_name`:用户名称
|
||||
|
||||
### 8. 获取聊天流统计摘要
|
||||
|
||||
**示例:**
|
||||
```python
|
||||
info = chat_api.get_stream_info(chat_stream)
|
||||
print(f"聊天类型: {info['type']}")
|
||||
print(f"平台: {info['platform']}")
|
||||
if info['type'] == 'group':
|
||||
print(f"群ID: {info['group_id']}")
|
||||
print(f"群名: {info['group_name']}")
|
||||
def get_streams_summary() -> Dict[str, int]:
|
||||
```
|
||||
|
||||
#### `get_streams_summary() -> Dict[str, int]`
|
||||
获取聊天流统计信息
|
||||
**Returns**:
|
||||
- `Dict[str, int]`:聊天流统计信息摘要,包含以下键:
|
||||
- `total_streams`:总聊天流数量
|
||||
- `group_streams`:群聊流数量
|
||||
- `private_streams`:私聊流数量
|
||||
- `qq_streams`:QQ平台流数量
|
||||
|
||||
**返回:**
|
||||
- `Dict[str, int]`:包含各平台群聊和私聊数量的统计字典
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 基础用法
|
||||
```python
|
||||
from src.plugin_system.apis import chat_api
|
||||
|
||||
# 获取所有群聊
|
||||
group_streams = chat_api.get_group_streams()
|
||||
print(f"共有 {len(group_streams)} 个群聊")
|
||||
|
||||
# 查找特定群聊
|
||||
target_group = chat_api.get_stream_by_group_id("123456789")
|
||||
if target_group:
|
||||
group_info = chat_api.get_stream_info(target_group)
|
||||
print(f"群名: {group_info['group_name']}")
|
||||
```
|
||||
|
||||
### 遍历所有聊天流
|
||||
```python
|
||||
# 获取所有聊天流并分类处理
|
||||
all_streams = chat_api.get_all_streams()
|
||||
|
||||
for stream in all_streams:
|
||||
stream_type = chat_api.get_stream_type(stream)
|
||||
if stream_type == "group":
|
||||
print(f"群聊: {stream.group_info.group_name}")
|
||||
elif stream_type == "private":
|
||||
print(f"私聊: {stream.user_info.user_nickname}")
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 所有函数都有错误处理,失败时会记录日志
|
||||
2. 查询函数返回None或空列表时表示未找到结果
|
||||
3. `platform`参数通常为"qq",也可能支持其他平台
|
||||
4. `ChatStream`对象包含了聊天的完整信息,包括用户信息、群信息等
|
||||
1. 大部分函数在参数不合法时候会抛出异常,请确保你的程序进行了捕获。
|
||||
2. `ChatStream`对象包含了聊天的完整信息,包括用户信息、群信息等。
|
||||
@@ -6,178 +6,47 @@
|
||||
|
||||
```python
|
||||
from src.plugin_system.apis import config_api
|
||||
# 或者
|
||||
from src.plugin_system import config_api
|
||||
```
|
||||
|
||||
## 主要功能
|
||||
|
||||
### 1. 配置访问
|
||||
### 1. 访问全局配置
|
||||
|
||||
#### `get_global_config(key: str, default: Any = None) -> Any`
|
||||
安全地从全局配置中获取一个值
|
||||
|
||||
**参数:**
|
||||
- `key`:配置键名,支持嵌套访问如 "section.subsection.key"
|
||||
- `default`:如果配置不存在时返回的默认值
|
||||
|
||||
**返回:**
|
||||
- `Any`:配置值或默认值
|
||||
|
||||
**示例:**
|
||||
```python
|
||||
# 获取机器人昵称
|
||||
def get_global_config(key: str, default: Any = None) -> Any:
|
||||
```
|
||||
|
||||
**Args**:
|
||||
- `key`: 命名空间式配置键名,使用嵌套访问,如 "section.subsection.key",大小写敏感
|
||||
- `default`: 如果配置不存在时返回的默认值
|
||||
|
||||
**Returns**:
|
||||
- `Any`: 配置值或默认值
|
||||
|
||||
#### 示例:
|
||||
获取机器人昵称
|
||||
```python
|
||||
bot_name = config_api.get_global_config("bot.nickname", "MaiBot")
|
||||
|
||||
# 获取嵌套配置
|
||||
llm_model = config_api.get_global_config("model.default.model_name", "gpt-3.5-turbo")
|
||||
|
||||
# 获取不存在的配置
|
||||
unknown_config = config_api.get_global_config("unknown.config", "默认值")
|
||||
```
|
||||
|
||||
#### `get_plugin_config(plugin_config: dict, key: str, default: Any = None) -> Any`
|
||||
从插件配置中获取值,支持嵌套键访问
|
||||
### 2. 获取插件配置
|
||||
|
||||
**参数:**
|
||||
- `plugin_config`:插件配置字典
|
||||
- `key`:配置键名,支持嵌套访问如 "section.subsection.key"
|
||||
- `default`:如果配置不存在时返回的默认值
|
||||
|
||||
**返回:**
|
||||
- `Any`:配置值或默认值
|
||||
|
||||
**示例:**
|
||||
```python
|
||||
# 在插件中使用
|
||||
class MyPlugin(BasePlugin):
|
||||
async def handle_action(self, action_data, chat_stream):
|
||||
# 获取插件配置
|
||||
api_key = config_api.get_plugin_config(self.config, "api.key", "")
|
||||
timeout = config_api.get_plugin_config(self.config, "timeout", 30)
|
||||
|
||||
if not api_key:
|
||||
logger.warning("API密钥未配置")
|
||||
return False
|
||||
def get_plugin_config(plugin_config: dict, key: str, default: Any = None) -> Any:
|
||||
```
|
||||
**Args**:
|
||||
- `plugin_config`: 插件配置字典
|
||||
- `key`: 配置键名,支持嵌套访问如 "section.subsection.key",大小写敏感
|
||||
- `default`: 如果配置不存在时返回的默认值
|
||||
|
||||
### 2. 用户信息API
|
||||
|
||||
#### `get_user_id_by_person_name(person_name: str) -> tuple[str, str]`
|
||||
根据用户名获取用户ID
|
||||
|
||||
**参数:**
|
||||
- `person_name`:用户名
|
||||
|
||||
**返回:**
|
||||
- `tuple[str, str]`:(平台, 用户ID)
|
||||
|
||||
**示例:**
|
||||
```python
|
||||
platform, user_id = await config_api.get_user_id_by_person_name("张三")
|
||||
if platform and user_id:
|
||||
print(f"用户张三在{platform}平台的ID是{user_id}")
|
||||
```
|
||||
|
||||
#### `get_person_info(person_id: str, key: str, default: Any = None) -> Any`
|
||||
获取用户信息
|
||||
|
||||
**参数:**
|
||||
- `person_id`:用户ID
|
||||
- `key`:信息键名
|
||||
- `default`:默认值
|
||||
|
||||
**返回:**
|
||||
- `Any`:用户信息值或默认值
|
||||
|
||||
**示例:**
|
||||
```python
|
||||
# 获取用户昵称
|
||||
nickname = await config_api.get_person_info(person_id, "nickname", "未知用户")
|
||||
|
||||
# 获取用户印象
|
||||
impression = await config_api.get_person_info(person_id, "impression", "")
|
||||
```
|
||||
|
||||
## 使用示例
|
||||
|
||||
### 配置驱动的插件开发
|
||||
```python
|
||||
from src.plugin_system.apis import config_api
|
||||
from src.plugin_system.base import BasePlugin
|
||||
|
||||
class WeatherPlugin(BasePlugin):
|
||||
async def handle_action(self, action_data, chat_stream):
|
||||
# 从全局配置获取API配置
|
||||
api_endpoint = config_api.get_global_config("weather.api_endpoint", "")
|
||||
default_city = config_api.get_global_config("weather.default_city", "北京")
|
||||
|
||||
# 从插件配置获取特定设置
|
||||
api_key = config_api.get_plugin_config(self.config, "api_key", "")
|
||||
timeout = config_api.get_plugin_config(self.config, "timeout", 10)
|
||||
|
||||
if not api_key:
|
||||
return {"success": False, "message": "Weather API密钥未配置"}
|
||||
|
||||
# 使用配置进行天气查询...
|
||||
return {"success": True, "message": f"{default_city}今天天气晴朗"}
|
||||
```
|
||||
|
||||
### 用户信息查询
|
||||
```python
|
||||
async def get_user_by_name(user_name: str):
|
||||
"""根据用户名获取完整的用户信息"""
|
||||
|
||||
# 获取用户的平台和ID
|
||||
platform, user_id = await config_api.get_user_id_by_person_name(user_name)
|
||||
|
||||
if not platform or not user_id:
|
||||
return None
|
||||
|
||||
# 构建person_id
|
||||
from src.person_info.person_info import PersonInfoManager
|
||||
person_id = PersonInfoManager.get_person_id(platform, user_id)
|
||||
|
||||
# 获取用户详细信息
|
||||
nickname = await config_api.get_person_info(person_id, "nickname", user_name)
|
||||
impression = await config_api.get_person_info(person_id, "impression", "")
|
||||
|
||||
return {
|
||||
"platform": platform,
|
||||
"user_id": user_id,
|
||||
"nickname": nickname,
|
||||
"impression": impression
|
||||
}
|
||||
```
|
||||
|
||||
## 配置键名说明
|
||||
|
||||
### 常用全局配置键
|
||||
- `bot.nickname`:机器人昵称
|
||||
- `bot.qq_account`:机器人QQ号
|
||||
- `model.default`:默认LLM模型配置
|
||||
- `database.path`:数据库路径
|
||||
|
||||
### 嵌套配置访问
|
||||
配置支持点号分隔的嵌套访问:
|
||||
```python
|
||||
# config.toml 中的配置:
|
||||
# [bot]
|
||||
# nickname = "MaiBot"
|
||||
# qq_account = "123456"
|
||||
#
|
||||
# [model.default]
|
||||
# model_name = "gpt-3.5-turbo"
|
||||
# temperature = 0.7
|
||||
|
||||
# API调用:
|
||||
bot_name = config_api.get_global_config("bot.nickname")
|
||||
model_name = config_api.get_global_config("model.default.model_name")
|
||||
temperature = config_api.get_global_config("model.default.temperature")
|
||||
```
|
||||
**Returns**:
|
||||
- `Any`: 配置值或默认值
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. **只读访问**:配置API只提供读取功能,插件不能修改全局配置
|
||||
2. **异步函数**:用户信息相关的函数是异步的,需要使用`await`
|
||||
3. **错误处理**:所有函数都有错误处理,失败时会记录日志并返回默认值
|
||||
4. **安全性**:插件通过此API访问配置是安全和隔离的
|
||||
5. **性能**:频繁访问的配置建议在插件初始化时获取并缓存
|
||||
2. **错误处理**:所有函数都有错误处理,失败时会记录日志并返回默认值
|
||||
3. **安全性**:插件通过此API访问配置是安全和隔离的
|
||||
4. **性能**:频繁访问的配置建议在插件初始化时获取并缓存
|
||||
@@ -6,34 +6,6 @@
|
||||
>
|
||||
> 系统会根据你在代码中定义的 `config_schema` 自动生成配置文件。手动创建配置文件会破坏自动化流程,导致配置不一致、缺失注释和文档等问题。
|
||||
|
||||
## 📖 目录
|
||||
|
||||
1. [配置架构变更说明](#配置架构变更说明)
|
||||
2. [配置版本管理](#配置版本管理)
|
||||
3. [配置定义:Schema驱动的配置系统](#配置定义schema驱动的配置系统)
|
||||
4. [配置访问:在Action和Command中使用配置](#配置访问在action和command中使用配置)
|
||||
5. [完整示例:从定义到使用](#完整示例从定义到使用)
|
||||
6. [最佳实践与注意事项](#最佳实践与注意事项)
|
||||
|
||||
---
|
||||
|
||||
## 配置架构变更说明
|
||||
|
||||
- **`_manifest.json`** - 负责插件的**元数据信息**(静态)
|
||||
- 插件名称、版本、描述
|
||||
- 作者信息、许可证
|
||||
- 仓库链接、关键词、分类
|
||||
- 组件列表、兼容性信息
|
||||
|
||||
- **`config.toml`** - 负责插件的**运行时配置**(动态)
|
||||
- `enabled` - 是否启用插件
|
||||
- 功能参数配置
|
||||
- 组件启用开关
|
||||
- 用户可调整的行为参数
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 配置版本管理
|
||||
|
||||
### 🎯 版本管理概述
|
||||
@@ -103,7 +75,7 @@ config_schema = {
|
||||
2. **迁移配置值** - 将旧配置文件中的值迁移到新结构中
|
||||
3. **处理新增字段** - 新增的配置项使用默认值
|
||||
4. **更新版本号** - `config_version` 字段自动更新为最新版本
|
||||
5. **保存配置文件** - 迁移后的配置直接覆盖原文件(不保留备份)
|
||||
5. **保存配置文件** - 迁移后的配置直接覆盖原文件**(不保留备份)**
|
||||
|
||||
### 🔧 实际使用示例
|
||||
|
||||
@@ -174,28 +146,13 @@ min_duration = 120
|
||||
- 跳过版本检查和迁移
|
||||
- 直接加载现有配置
|
||||
- 新增的配置项在代码中使用默认值访问
|
||||
|
||||
### 📝 配置迁移日志
|
||||
|
||||
系统会详细记录配置迁移过程:
|
||||
|
||||
```log
|
||||
[MutePlugin] 检测到配置版本需要更新: 当前=v1.0.0, 期望=v1.1.0
|
||||
[MutePlugin] 生成新配置结构...
|
||||
[MutePlugin] 迁移配置值: plugin.enabled = true
|
||||
[MutePlugin] 更新配置版本: plugin.config_version = 1.1.0 (旧值: 1.0.0)
|
||||
[MutePlugin] 迁移配置值: mute.min_duration = 120
|
||||
[MutePlugin] 迁移配置值: mute.max_duration = 3600
|
||||
[MutePlugin] 新增节: permissions
|
||||
[MutePlugin] 配置文件已从 v1.0.0 更新到 v1.1.0
|
||||
```
|
||||
- 系统会详细记录配置迁移过程。
|
||||
|
||||
### ⚠️ 重要注意事项
|
||||
|
||||
#### 1. 版本号管理
|
||||
- 当你修改 `config_schema` 时,**必须同步更新** `config_version`
|
||||
- 建议使用语义化版本号 (例如:`1.0.0`, `1.1.0`, `2.0.0`)
|
||||
- 配置结构的重大变更应该增加主版本号
|
||||
- 请使用语义化版本号 (例如:`1.0.0`, `1.1.0`, `2.0.0`)
|
||||
|
||||
#### 2. 迁移策略
|
||||
- **保留原值优先**: 迁移时优先保留用户的原有配置值
|
||||
@@ -207,45 +164,7 @@ min_duration = 120
|
||||
- **不保留备份**: 迁移后直接覆盖原配置文件,不保留备份
|
||||
- **失败安全**: 如果迁移过程中出现错误,会回退到原配置
|
||||
|
||||
---
|
||||
|
||||
## 配置定义:Schema驱动的配置系统
|
||||
|
||||
### 核心理念:Schema驱动的配置
|
||||
|
||||
在新版插件系统中,我们引入了一套 **配置Schema(模式)驱动** 的机制。**你不需要也不应该手动创建和维护 `config.toml` 文件**,而是通过在插件代码中 **声明配置的结构**,系统将为你完成剩下的工作。
|
||||
|
||||
> **⚠️ 绝对不要手动创建 config.toml 文件!**
|
||||
>
|
||||
> - ❌ **错误做法**:手动在插件目录下创建 `config.toml` 文件
|
||||
> - ✅ **正确做法**:在插件代码中定义 `config_schema`,让系统自动生成配置文件
|
||||
|
||||
**核心优势:**
|
||||
|
||||
- **自动化 (Automation)**: 如果配置文件不存在,系统会根据你的声明 **自动生成** 一份包含默认值和详细注释的 `config.toml` 文件。
|
||||
- **规范化 (Standardization)**: 所有插件的配置都遵循统一的结构,提升了可维护性。
|
||||
- **自带文档 (Self-documenting)**: 配置文件中的每一项都包含详细的注释、类型说明、可选值和示例,极大地降低了用户的使用门槛。
|
||||
- **健壮性 (Robustness)**: 在代码中直接定义配置的类型和默认值,减少了因配置错误导致的运行时问题。
|
||||
- **易于管理 (Easy Management)**: 生成的配置文件可以方便地加入 `.gitignore`,避免将个人配置(如API Key)提交到版本库。
|
||||
|
||||
### 配置生成工作流程
|
||||
|
||||
```mermaid
|
||||
graph TD
|
||||
A[编写插件代码] --> B[定义 config_schema]
|
||||
B --> C[首次加载插件]
|
||||
C --> D{config.toml 是否存在?}
|
||||
D -->|不存在| E[系统自动生成 config.toml]
|
||||
D -->|存在| F[加载现有配置文件]
|
||||
E --> G[配置完成,插件可用]
|
||||
F --> G
|
||||
|
||||
style E fill:#90EE90
|
||||
style B fill:#87CEEB
|
||||
style G fill:#DDA0DD
|
||||
```
|
||||
|
||||
### 如何定义配置
|
||||
## 配置定义
|
||||
|
||||
配置的定义在你的插件主类(继承自 `BasePlugin`)中完成,主要通过两个类属性:
|
||||
|
||||
@@ -257,6 +176,7 @@ graph TD
|
||||
每个配置项都通过一个 `ConfigField` 对象来定义。
|
||||
|
||||
```python
|
||||
from dataclasses import dataclass
|
||||
from src.plugin_system.base.config_types import ConfigField
|
||||
|
||||
@dataclass
|
||||
@@ -270,28 +190,21 @@ class ConfigField:
|
||||
choices: Optional[List[Any]] = None # 可选值列表 (可选)
|
||||
```
|
||||
|
||||
### 配置定义示例
|
||||
### 配置示例
|
||||
|
||||
让我们以一个功能丰富的 `MutePlugin` 为例,看看如何定义它的配置。
|
||||
|
||||
```python
|
||||
# src/plugins/built_in/mute_plugin/plugin.py
|
||||
|
||||
from src.plugin_system import BasePlugin, register_plugin
|
||||
from src.plugin_system.base.config_types import ConfigField
|
||||
from src.plugin_system import BasePlugin, register_plugin, ConfigField
|
||||
from typing import List, Tuple, Type
|
||||
|
||||
@register_plugin
|
||||
class MutePlugin(BasePlugin):
|
||||
"""禁言插件"""
|
||||
|
||||
# 插件基本信息
|
||||
plugin_name = "mute_plugin"
|
||||
plugin_description = "群聊禁言管理插件,提供智能禁言功能"
|
||||
plugin_version = "2.0.0"
|
||||
plugin_author = "MaiBot开发团队"
|
||||
enable_plugin = True
|
||||
config_file_name = "config.toml"
|
||||
# 这里是插件基本信息,略去
|
||||
|
||||
# 步骤1: 定义配置节的描述
|
||||
config_section_descriptions = {
|
||||
@@ -339,22 +252,9 @@ class MutePlugin(BasePlugin):
|
||||
}
|
||||
}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
# 在这里可以通过 self.get_config() 来获取配置值
|
||||
enable_smart_mute = self.get_config("components.enable_smart_mute", True)
|
||||
enable_mute_command = self.get_config("components.enable_mute_command", False)
|
||||
|
||||
components = []
|
||||
if enable_smart_mute:
|
||||
components.append((SmartMuteAction.get_action_info(), SmartMuteAction))
|
||||
if enable_mute_command:
|
||||
components.append((MuteCommand.get_command_info(), MuteCommand))
|
||||
|
||||
return components
|
||||
# 这里是插件方法,略去
|
||||
```
|
||||
|
||||
### 自动生成的配置文件
|
||||
|
||||
当 `mute_plugin` 首次加载且其目录中不存在 `config.toml` 时,系统会自动创建以下文件:
|
||||
|
||||
```toml
|
||||
@@ -413,317 +313,24 @@ prefix = "[MutePlugin]"
|
||||
|
||||
---
|
||||
|
||||
## 配置访问:在Action和Command中使用配置
|
||||
## 配置访问
|
||||
|
||||
### 问题描述
|
||||
如果你想要在你的组件中访问配置,可以通过组件内置的 `get_config()` 方法访问配置。
|
||||
|
||||
在插件开发中,你可能遇到这样的问题:
|
||||
- 想要在Action或Command中访问插件配置
|
||||
|
||||
### ✅ 解决方案
|
||||
|
||||
**直接使用 `self.get_config()` 方法!**
|
||||
|
||||
系统已经自动为你处理了配置传递,你只需要通过组件内置的 `get_config` 方法访问配置即可。
|
||||
|
||||
### 📖 快速示例
|
||||
|
||||
#### 在Action中访问配置
|
||||
其参数为一个命名空间化的字符串。以上面的 `MutePlugin` 为例,你可以这样访问配置:
|
||||
|
||||
```python
|
||||
from src.plugin_system import BaseAction
|
||||
|
||||
class MyAction(BaseAction):
|
||||
async def execute(self):
|
||||
# 方法1: 获取配置值(带默认值)
|
||||
api_key = self.get_config("api.key", "default_key")
|
||||
timeout = self.get_config("api.timeout", 30)
|
||||
|
||||
# 方法2: 支持嵌套键访问
|
||||
log_level = self.get_config("advanced.logging.level", "INFO")
|
||||
|
||||
# 方法3: 直接访问顶层配置
|
||||
enable_feature = self.get_config("features.enable_smart", False)
|
||||
|
||||
# 使用配置值
|
||||
if enable_feature:
|
||||
await self.send_text(f"API密钥: {api_key}")
|
||||
|
||||
return True, "配置访问成功"
|
||||
enable_smart_mute = self.get_config("components.enable_smart_mute", True)
|
||||
```
|
||||
|
||||
#### 在Command中访问配置
|
||||
|
||||
```python
|
||||
from src.plugin_system import BaseCommand
|
||||
|
||||
class MyCommand(BaseCommand):
|
||||
async def execute(self):
|
||||
# 使用方式与Action完全相同
|
||||
welcome_msg = self.get_config("messages.welcome", "欢迎!")
|
||||
max_results = self.get_config("search.max_results", 10)
|
||||
|
||||
# 根据配置执行不同逻辑
|
||||
if self.get_config("features.debug_mode", False):
|
||||
await self.send_text(f"调试模式已启用,最大结果数: {max_results}")
|
||||
|
||||
await self.send_text(welcome_msg)
|
||||
return True, "命令执行完成"
|
||||
```
|
||||
|
||||
### 🔧 API方法详解
|
||||
|
||||
#### 1. `get_config(key, default=None)`
|
||||
|
||||
获取配置值,支持嵌套键访问:
|
||||
|
||||
```python
|
||||
# 简单键
|
||||
value = self.get_config("timeout", 30)
|
||||
|
||||
# 嵌套键(用点号分隔)
|
||||
value = self.get_config("database.connection.host", "localhost")
|
||||
value = self.get_config("features.ai.model", "gpt-3.5-turbo")
|
||||
```
|
||||
|
||||
#### 2. 类型安全的配置访问
|
||||
|
||||
```python
|
||||
# 确保正确的类型
|
||||
max_retries = self.get_config("api.max_retries", 3)
|
||||
if not isinstance(max_retries, int):
|
||||
max_retries = 3 # 使用安全的默认值
|
||||
|
||||
# 布尔值配置
|
||||
debug_mode = self.get_config("features.debug_mode", False)
|
||||
if debug_mode:
|
||||
# 调试功能逻辑
|
||||
pass
|
||||
```
|
||||
|
||||
#### 3. 配置驱动的组件行为
|
||||
|
||||
```python
|
||||
class ConfigDrivenAction(BaseAction):
|
||||
async def execute(self):
|
||||
# 根据配置决定激活行为
|
||||
activation_config = {
|
||||
"use_keywords": self.get_config("activation.use_keywords", True),
|
||||
"use_llm": self.get_config("activation.use_llm", False),
|
||||
"keywords": self.get_config("activation.keywords", []),
|
||||
}
|
||||
|
||||
# 根据配置调整功能
|
||||
features = {
|
||||
"enable_emoji": self.get_config("features.enable_emoji", True),
|
||||
"enable_llm_reply": self.get_config("features.enable_llm_reply", False),
|
||||
"max_length": self.get_config("output.max_length", 200),
|
||||
}
|
||||
|
||||
# 使用配置执行逻辑
|
||||
if features["enable_llm_reply"]:
|
||||
# 使用LLM生成回复
|
||||
pass
|
||||
else:
|
||||
# 使用模板回复
|
||||
pass
|
||||
|
||||
return True, "配置驱动执行完成"
|
||||
```
|
||||
|
||||
### 🔄 配置传递机制
|
||||
|
||||
系统自动处理配置传递,无需手动操作:
|
||||
|
||||
1. **插件初始化** → `BasePlugin`加载`config.toml`到`self.config`
|
||||
2. **组件注册** → 系统记录插件配置
|
||||
3. **组件实例化** → 自动传递`plugin_config`参数给Action/Command
|
||||
4. **配置访问** → 组件通过`self.get_config()`直接访问配置
|
||||
|
||||
---
|
||||
|
||||
## 完整示例:从定义到使用
|
||||
|
||||
### 插件定义
|
||||
|
||||
```python
|
||||
from src.plugin_system.base.config_types import ConfigField
|
||||
|
||||
@register_plugin
|
||||
class GreetingPlugin(BasePlugin):
|
||||
"""问候插件完整示例"""
|
||||
|
||||
plugin_name = "greeting_plugin"
|
||||
plugin_description = "智能问候插件,展示配置定义和访问的完整流程"
|
||||
plugin_version = "1.0.0"
|
||||
config_file_name = "config.toml"
|
||||
|
||||
# 配置节描述
|
||||
config_section_descriptions = {
|
||||
"plugin": "插件启用配置",
|
||||
"greeting": "问候功能配置",
|
||||
"features": "功能开关配置",
|
||||
"messages": "消息模板配置"
|
||||
}
|
||||
|
||||
# 配置Schema定义
|
||||
config_schema = {
|
||||
"plugin": {
|
||||
"enabled": ConfigField(type=bool, default=True, description="是否启用插件")
|
||||
},
|
||||
"greeting": {
|
||||
"template": ConfigField(
|
||||
type=str,
|
||||
default="你好,{username}!欢迎使用问候插件!",
|
||||
description="问候消息模板"
|
||||
),
|
||||
"enable_emoji": ConfigField(type=bool, default=True, description="是否启用表情符号"),
|
||||
"enable_llm": ConfigField(type=bool, default=False, description="是否使用LLM生成个性化问候")
|
||||
},
|
||||
"features": {
|
||||
"smart_detection": ConfigField(type=bool, default=True, description="是否启用智能检测"),
|
||||
"random_greeting": ConfigField(type=bool, default=False, description="是否使用随机问候语"),
|
||||
"max_greetings_per_hour": ConfigField(type=int, default=5, description="每小时最大问候次数")
|
||||
},
|
||||
"messages": {
|
||||
"custom_greetings": ConfigField(
|
||||
type=list,
|
||||
default=["你好!", "嗨!", "欢迎!"],
|
||||
description="自定义问候语列表"
|
||||
),
|
||||
"error_message": ConfigField(
|
||||
type=str,
|
||||
default="问候功能暂时不可用",
|
||||
description="错误时显示的消息"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
"""根据配置动态注册组件"""
|
||||
components = []
|
||||
|
||||
# 根据配置决定是否注册组件
|
||||
if self.get_config("plugin.enabled", True):
|
||||
components.append((SmartGreetingAction.get_action_info(), SmartGreetingAction))
|
||||
components.append((GreetingCommand.get_command_info(), GreetingCommand))
|
||||
|
||||
return components
|
||||
```
|
||||
|
||||
### Action组件使用配置
|
||||
|
||||
```python
|
||||
class SmartGreetingAction(BaseAction):
|
||||
"""智能问候Action - 展示配置访问"""
|
||||
|
||||
focus_activation_type = ActionActivationType.KEYWORD
|
||||
normal_activation_type = ActionActivationType.KEYWORD
|
||||
activation_keywords = ["你好", "hello", "hi"]
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
"""执行智能问候,大量使用配置"""
|
||||
try:
|
||||
# 检查插件是否启用
|
||||
if not self.get_config("plugin.enabled", True):
|
||||
return False, "插件已禁用"
|
||||
|
||||
# 获取问候配置
|
||||
template = self.get_config("greeting.template", "你好,{username}!")
|
||||
enable_emoji = self.get_config("greeting.enable_emoji", True)
|
||||
enable_llm = self.get_config("greeting.enable_llm", False)
|
||||
|
||||
# 获取功能配置
|
||||
smart_detection = self.get_config("features.smart_detection", True)
|
||||
random_greeting = self.get_config("features.random_greeting", False)
|
||||
max_per_hour = self.get_config("features.max_greetings_per_hour", 5)
|
||||
|
||||
# 获取消息配置
|
||||
custom_greetings = self.get_config("messages.custom_greetings", [])
|
||||
error_message = self.get_config("messages.error_message", "问候功能不可用")
|
||||
|
||||
# 根据配置执行不同逻辑
|
||||
username = self.action_data.get("username", "用户")
|
||||
|
||||
if random_greeting and custom_greetings:
|
||||
# 使用随机自定义问候语
|
||||
import random
|
||||
greeting_msg = random.choice(custom_greetings)
|
||||
elif enable_llm:
|
||||
# 使用LLM生成个性化问候
|
||||
greeting_msg = await self._generate_llm_greeting(username)
|
||||
else:
|
||||
# 使用模板问候
|
||||
greeting_msg = template.format(username=username)
|
||||
|
||||
# 发送问候消息
|
||||
await self.send_text(greeting_msg)
|
||||
|
||||
# 根据配置发送表情
|
||||
if enable_emoji:
|
||||
await self.send_emoji("😊")
|
||||
|
||||
return True, f"向{username}发送了问候"
|
||||
|
||||
except Exception as e:
|
||||
# 使用配置的错误消息
|
||||
await self.send_text(self.get_config("messages.error_message", "出错了"))
|
||||
return False, f"问候失败: {str(e)}"
|
||||
|
||||
async def _generate_llm_greeting(self, username: str) -> str:
|
||||
"""根据配置使用LLM生成问候语"""
|
||||
# 这里可以进一步使用配置来定制LLM行为
|
||||
llm_style = self.get_config("greeting.llm_style", "friendly")
|
||||
# ... LLM调用逻辑
|
||||
return f"你好 {username}!很高兴见到你!"
|
||||
```
|
||||
|
||||
### Command组件使用配置
|
||||
|
||||
```python
|
||||
class GreetingCommand(BaseCommand):
|
||||
"""问候命令 - 展示配置访问"""
|
||||
|
||||
command_pattern = r"^/greet(?:\s+(?P<username>\w+))?$"
|
||||
command_help = "发送问候消息"
|
||||
command_examples = ["/greet", "/greet Alice"]
|
||||
|
||||
async def execute(self) -> Tuple[bool, Optional[str]]:
|
||||
"""执行问候命令"""
|
||||
# 检查功能是否启用
|
||||
if not self.get_config("plugin.enabled", True):
|
||||
await self.send_text("问候功能已禁用")
|
||||
return False, "功能禁用"
|
||||
|
||||
# 获取用户名
|
||||
username = self.matched_groups.get("username", "用户")
|
||||
|
||||
# 根据配置选择问候方式
|
||||
if self.get_config("features.random_greeting", False):
|
||||
custom_greetings = self.get_config("messages.custom_greetings", ["你好!"])
|
||||
import random
|
||||
greeting = random.choice(custom_greetings)
|
||||
else:
|
||||
template = self.get_config("greeting.template", "你好,{username}!")
|
||||
greeting = template.format(username=username)
|
||||
|
||||
# 发送问候
|
||||
await self.send_text(greeting)
|
||||
|
||||
# 根据配置发送表情
|
||||
if self.get_config("greeting.enable_emoji", True):
|
||||
await self.send_text("😊")
|
||||
|
||||
return True, "问候发送成功"
|
||||
```
|
||||
如果尝试访问了一个不存在的配置项,系统会自动返回默认值(你传递的)或者 `None`。
|
||||
|
||||
---
|
||||
|
||||
## 最佳实践与注意事项
|
||||
|
||||
### 配置定义最佳实践
|
||||
|
||||
> **🚨 核心原则:永远不要手动创建 config.toml 文件!**
|
||||
**🚨 核心原则:永远不要手动创建 config.toml 文件!**
|
||||
|
||||
1. **🔥 绝不手动创建配置文件**: **任何时候都不要手动创建 `config.toml` 文件**!必须通过在 `plugin.py` 中定义 `config_schema` 让系统自动生成。
|
||||
- ❌ **禁止**:`touch config.toml`、手动编写配置文件
|
||||
@@ -737,76 +344,4 @@ class GreetingCommand(BaseCommand):
|
||||
|
||||
5. **gitignore**: 将 `plugins/*/config.toml` 或 `src/plugins/built_in/*/config.toml` 加入 `.gitignore`,以避免提交个人敏感信息。
|
||||
|
||||
6. **配置文件只供修改**: 自动生成的 `config.toml` 文件只应该被用户**修改**,而不是从零创建。
|
||||
|
||||
### 配置访问最佳实践
|
||||
|
||||
#### 1. 总是提供默认值
|
||||
|
||||
```python
|
||||
# ✅ 好的做法
|
||||
timeout = self.get_config("api.timeout", 30)
|
||||
|
||||
# ❌ 避免这样做
|
||||
timeout = self.get_config("api.timeout") # 可能返回None
|
||||
```
|
||||
|
||||
#### 2. 验证配置类型
|
||||
|
||||
```python
|
||||
# 获取配置后验证类型
|
||||
max_items = self.get_config("list.max_items", 10)
|
||||
if not isinstance(max_items, int) or max_items <= 0:
|
||||
max_items = 10 # 使用安全的默认值
|
||||
```
|
||||
|
||||
#### 3. 缓存复杂配置解析
|
||||
|
||||
```python
|
||||
class MyAction(BaseAction):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# 在初始化时解析复杂配置,避免重复解析
|
||||
self._api_config = self._parse_api_config()
|
||||
|
||||
def _parse_api_config(self):
|
||||
return {
|
||||
'key': self.get_config("api.key", ""),
|
||||
'timeout': self.get_config("api.timeout", 30),
|
||||
'retries': self.get_config("api.max_retries", 3)
|
||||
}
|
||||
```
|
||||
|
||||
#### 4. 配置驱动的组件注册
|
||||
|
||||
```python
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
"""根据配置动态注册组件"""
|
||||
components = []
|
||||
|
||||
# 从配置获取组件启用状态
|
||||
enable_action = self.get_config("components.enable_action", True)
|
||||
enable_command = self.get_config("components.enable_command", True)
|
||||
|
||||
if enable_action:
|
||||
components.append((MyAction.get_action_info(), MyAction))
|
||||
if enable_command:
|
||||
components.append((MyCommand.get_command_info(), MyCommand))
|
||||
|
||||
return components
|
||||
```
|
||||
|
||||
### 🎉 总结
|
||||
|
||||
现在你掌握了插件配置的完整流程:
|
||||
|
||||
1. **定义配置**: 在插件中使用 `config_schema` 定义配置结构
|
||||
2. **访问配置**: 在组件中使用 `self.get_config("key", default_value)` 访问配置
|
||||
3. **自动生成**: 系统自动生成带注释的配置文件
|
||||
4. **动态行为**: 根据配置动态调整插件行为
|
||||
|
||||
> **🚨 最后强调:任何时候都不要手动创建 config.toml 文件!**
|
||||
>
|
||||
> 让系统根据你的 `config_schema` 自动生成配置文件,这是插件系统的核心设计原则。
|
||||
|
||||
不需要继承`BasePlugin`,不需要复杂的配置传递,不需要手动创建配置文件,组件内置的`get_config`方法和自动化的配置生成机制已经为你准备好了一切!
|
||||
6. **配置文件只供修改**: 自动生成的 `config.toml` 文件只应该被用户**修改**,而不是从零创建。
|
||||
@@ -1,93 +1,6 @@
|
||||
# 📦 插件依赖管理系统
|
||||
|
||||
> 🎯 **简介**:MaiBot插件系统提供了强大的Python包依赖管理功能,让插件开发更加便捷和可靠。
|
||||
|
||||
## ✨ 功能概述
|
||||
|
||||
### 🎯 核心能力
|
||||
- **声明式依赖**:插件可以明确声明需要的Python包
|
||||
- **智能检查**:自动检查依赖包的安装状态
|
||||
- **版本控制**:精确的版本要求管理
|
||||
- **可选依赖**:区分必需依赖和可选依赖
|
||||
- **自动安装**:可选的自动安装功能
|
||||
- **批量管理**:生成统一的requirements文件
|
||||
- **安全控制**:防止意外安装和版本冲突
|
||||
|
||||
### 🔄 工作流程
|
||||
1. **声明依赖** → 在插件中声明所需的Python包
|
||||
2. **加载检查** → 插件加载时自动检查依赖状态
|
||||
3. **状态报告** → 详细报告缺失或版本不匹配的依赖
|
||||
4. **智能安装** → 可选择自动安装或手动安装
|
||||
5. **运行时处理** → 插件运行时优雅处理依赖缺失
|
||||
|
||||
## 🚀 快速开始
|
||||
|
||||
### 步骤1:声明依赖
|
||||
|
||||
在你的插件类中添加`python_dependencies`字段:
|
||||
|
||||
```python
|
||||
from src.plugin_system import BasePlugin, PythonDependency, register_plugin
|
||||
|
||||
@register_plugin
|
||||
class MyPlugin(BasePlugin):
|
||||
name = "my_plugin"
|
||||
|
||||
# 声明Python包依赖
|
||||
python_dependencies = [
|
||||
PythonDependency(
|
||||
package_name="requests",
|
||||
version=">=2.25.0",
|
||||
description="HTTP请求库,用于网络通信"
|
||||
),
|
||||
PythonDependency(
|
||||
package_name="numpy",
|
||||
version=">=1.20.0",
|
||||
optional=True,
|
||||
description="数值计算库(可选功能)"
|
||||
),
|
||||
]
|
||||
|
||||
def get_plugin_components(self):
|
||||
# 返回插件组件
|
||||
return []
|
||||
```
|
||||
|
||||
### 步骤2:处理依赖
|
||||
|
||||
在组件代码中优雅处理依赖缺失:
|
||||
|
||||
```python
|
||||
class MyAction(BaseAction):
|
||||
async def execute(self, action_input, context=None):
|
||||
try:
|
||||
import requests
|
||||
# 使用requests进行网络请求
|
||||
response = requests.get("https://api.example.com")
|
||||
return {"status": "success", "data": response.json()}
|
||||
except ImportError:
|
||||
return {
|
||||
"status": "error",
|
||||
"message": "功能不可用:缺少requests库",
|
||||
"hint": "请运行: pip install requests>=2.25.0"
|
||||
}
|
||||
```
|
||||
|
||||
### 步骤3:检查和管理
|
||||
|
||||
使用依赖管理API:
|
||||
|
||||
```python
|
||||
from src.plugin_system import plugin_manager
|
||||
|
||||
# 检查所有插件的依赖状态
|
||||
result = plugin_manager.check_all_dependencies()
|
||||
print(f"检查了 {result['total_plugins_checked']} 个插件")
|
||||
print(f"缺少必需依赖的插件: {result['plugins_with_missing_required']} 个")
|
||||
|
||||
# 生成requirements文件
|
||||
plugin_manager.generate_plugin_requirements("plugin_requirements.txt")
|
||||
```
|
||||
现在的Python依赖包管理依然存在问题,请保留你的`python_dependencies`属性,等待后续重构。
|
||||
|
||||
## 📚 详细教程
|
||||
|
||||
@@ -97,11 +10,11 @@ plugin_manager.generate_plugin_requirements("plugin_requirements.txt")
|
||||
|
||||
```python
|
||||
PythonDependency(
|
||||
package_name="requests", # 导入时的包名
|
||||
version=">=2.25.0", # 版本要求
|
||||
optional=False, # 是否为可选依赖
|
||||
description="HTTP请求库", # 依赖描述
|
||||
install_name="" # pip安装时的包名(可选)
|
||||
package_name="PIL", # 导入时的包名
|
||||
version=">=11.2.0", # 版本要求
|
||||
optional=False, # 是否为可选依赖
|
||||
description="图像处理库", # 依赖描述
|
||||
install_name="pillow" # pip安装时的包名(可选)
|
||||
)
|
||||
```
|
||||
|
||||
@@ -110,10 +23,10 @@ PythonDependency(
|
||||
| 参数 | 类型 | 必需 | 说明 |
|
||||
|------|------|------|------|
|
||||
| `package_name` | str | ✅ | Python导入时使用的包名(如`requests`) |
|
||||
| `version` | str | ❌ | 版本要求,支持pip格式(如`>=1.0.0`, `==2.1.3`) |
|
||||
| `version` | str | ❌ | 版本要求,使用pip格式(如`>=1.0.0`, `==2.1.3`) |
|
||||
| `optional` | bool | ❌ | 是否为可选依赖,默认`False` |
|
||||
| `description` | str | ❌ | 依赖的用途描述 |
|
||||
| `install_name` | str | ❌ | pip安装时的包名,默认与`package_name`相同 |
|
||||
| `install_name` | str | ❌ | pip安装时的包名,默认与`package_name`相同,用于处理安装名称和导入名称不一致的情况 |
|
||||
|
||||
#### 版本格式示例
|
||||
|
||||
@@ -125,201 +38,3 @@ PythonDependency("pillow", "==8.3.2") # 精确版本
|
||||
PythonDependency("scipy", ">=1.7.0,!=1.8.0") # 排除特定版本
|
||||
```
|
||||
|
||||
#### 特殊情况处理
|
||||
|
||||
**导入名与安装名不同的包:**
|
||||
|
||||
```python
|
||||
PythonDependency(
|
||||
package_name="PIL", # import PIL
|
||||
install_name="Pillow", # pip install Pillow
|
||||
version=">=8.0.0"
|
||||
)
|
||||
```
|
||||
|
||||
**可选依赖示例:**
|
||||
|
||||
```python
|
||||
python_dependencies = [
|
||||
# 必需依赖 - 核心功能
|
||||
PythonDependency(
|
||||
package_name="requests",
|
||||
version=">=2.25.0",
|
||||
description="HTTP库,插件核心功能必需"
|
||||
),
|
||||
|
||||
# 可选依赖 - 增强功能
|
||||
PythonDependency(
|
||||
package_name="numpy",
|
||||
version=">=1.20.0",
|
||||
optional=True,
|
||||
description="数值计算库,用于高级数学运算"
|
||||
),
|
||||
PythonDependency(
|
||||
package_name="matplotlib",
|
||||
version=">=3.0.0",
|
||||
optional=True,
|
||||
description="绘图库,用于数据可视化功能"
|
||||
),
|
||||
]
|
||||
```
|
||||
|
||||
### 依赖检查机制
|
||||
|
||||
系统在以下时机会自动检查依赖:
|
||||
|
||||
1. **插件加载时**:检查插件声明的所有依赖
|
||||
2. **手动调用时**:通过API主动检查
|
||||
3. **运行时检查**:在组件执行时动态检查
|
||||
|
||||
#### 检查结果状态
|
||||
|
||||
| 状态 | 描述 | 处理建议 |
|
||||
|------|------|----------|
|
||||
| `no_dependencies` | 插件未声明任何依赖 | 无需处理 |
|
||||
| `ok` | 所有依赖都已满足 | 正常使用 |
|
||||
| `missing_optional` | 缺少可选依赖 | 部分功能不可用,考虑安装 |
|
||||
| `missing_required` | 缺少必需依赖 | 插件功能受限,需要安装 |
|
||||
|
||||
## 🎯 最佳实践
|
||||
|
||||
### 1. 依赖声明原则
|
||||
|
||||
#### ✅ 推荐做法
|
||||
|
||||
```python
|
||||
python_dependencies = [
|
||||
# 明确的版本要求
|
||||
PythonDependency(
|
||||
package_name="requests",
|
||||
version=">=2.25.0,<3.0.0", # 主版本兼容
|
||||
description="HTTP请求库,用于API调用"
|
||||
),
|
||||
|
||||
# 合理的可选依赖
|
||||
PythonDependency(
|
||||
package_name="numpy",
|
||||
version=">=1.20.0",
|
||||
optional=True,
|
||||
description="数值计算库,用于数据处理功能"
|
||||
),
|
||||
]
|
||||
```
|
||||
|
||||
#### ❌ 避免的做法
|
||||
|
||||
```python
|
||||
python_dependencies = [
|
||||
# 过于宽泛的版本要求
|
||||
PythonDependency("requests"), # 没有版本限制
|
||||
|
||||
# 过于严格的版本要求
|
||||
PythonDependency("numpy", "==1.21.0"), # 精确版本过于严格
|
||||
|
||||
# 缺少描述
|
||||
PythonDependency("matplotlib", ">=3.0.0"), # 没有说明用途
|
||||
]
|
||||
```
|
||||
|
||||
### 2. 错误处理模式
|
||||
|
||||
#### 优雅降级模式
|
||||
|
||||
```python
|
||||
class SmartAction(BaseAction):
|
||||
async def execute(self, action_input, context=None):
|
||||
# 检查可选依赖
|
||||
try:
|
||||
import numpy as np
|
||||
# 使用numpy的高级功能
|
||||
return await self._advanced_processing(action_input, np)
|
||||
except ImportError:
|
||||
# 降级到基础功能
|
||||
return await self._basic_processing(action_input)
|
||||
|
||||
async def _advanced_processing(self, input_data, np):
|
||||
"""使用numpy的高级处理"""
|
||||
result = np.array(input_data).mean()
|
||||
return {"result": result, "method": "advanced"}
|
||||
|
||||
async def _basic_processing(self, input_data):
|
||||
"""基础处理(不依赖外部库)"""
|
||||
result = sum(input_data) / len(input_data)
|
||||
return {"result": result, "method": "basic"}
|
||||
```
|
||||
|
||||
## 🔧 使用API
|
||||
|
||||
### 检查依赖状态
|
||||
|
||||
```python
|
||||
from src.plugin_system import plugin_manager
|
||||
|
||||
# 检查所有插件依赖(仅检查,不安装)
|
||||
result = plugin_manager.check_all_dependencies(auto_install=False)
|
||||
|
||||
# 检查并自动安装缺失的必需依赖
|
||||
result = plugin_manager.check_all_dependencies(auto_install=True)
|
||||
```
|
||||
|
||||
### 生成requirements文件
|
||||
|
||||
```python
|
||||
# 生成包含所有插件依赖的requirements文件
|
||||
plugin_manager.generate_plugin_requirements("plugin_requirements.txt")
|
||||
```
|
||||
|
||||
### 获取依赖状态报告
|
||||
|
||||
```python
|
||||
# 获取详细的依赖检查报告
|
||||
result = plugin_manager.check_all_dependencies()
|
||||
for plugin_name, status in result['plugin_status'].items():
|
||||
print(f"插件 {plugin_name}: {status['status']}")
|
||||
if status['missing']:
|
||||
print(f" 缺失必需依赖: {status['missing']}")
|
||||
if status['optional_missing']:
|
||||
print(f" 缺失可选依赖: {status['optional_missing']}")
|
||||
```
|
||||
|
||||
## 🛡️ 安全考虑
|
||||
|
||||
### 1. 自动安装控制
|
||||
- 🛡️ **默认手动**: 自动安装默认关闭,需要明确启用
|
||||
- 🔍 **依赖审查**: 安装前会显示将要安装的包列表
|
||||
- ⏱️ **超时控制**: 安装操作有超时限制(5分钟)
|
||||
|
||||
### 2. 权限管理
|
||||
- 📁 **环境隔离**: 推荐在虚拟环境中使用
|
||||
- 🔒 **版本锁定**: 支持精确的版本控制
|
||||
- 📝 **安装日志**: 记录所有安装操作
|
||||
|
||||
## 📊 故障排除
|
||||
|
||||
### 常见问题
|
||||
|
||||
1. **依赖检查失败**
|
||||
```python
|
||||
# 手动检查包是否可导入
|
||||
try:
|
||||
import package_name
|
||||
print("包可用")
|
||||
except ImportError:
|
||||
print("包不可用,需要安装")
|
||||
```
|
||||
|
||||
2. **版本冲突**
|
||||
```python
|
||||
# 检查已安装的包版本
|
||||
import package_name
|
||||
print(f"当前版本: {package_name.__version__}")
|
||||
```
|
||||
|
||||
3. **安装失败**
|
||||
```python
|
||||
# 查看安装日志
|
||||
from src.plugin_system import dependency_manager
|
||||
result = dependency_manager.get_install_summary()
|
||||
print("安装日志:", result['install_log'])
|
||||
print("失败详情:", result['failed_installs'])
|
||||
```
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 18 KiB |
@@ -4,15 +4,34 @@
|
||||
|
||||
## 新手入门
|
||||
|
||||
- [📖 快速开始指南](quick-start.md) - 5分钟创建你的第一个插件
|
||||
- [📖 快速开始指南](quick-start.md) - 快速创建你的第一个插件
|
||||
|
||||
## 组件功能详解
|
||||
|
||||
- [🧱 Action组件详解](action-components.md) - 掌握最核心的Action组件
|
||||
- [💻 Command组件详解](command-components.md) - 学习直接响应命令的组件
|
||||
- [⚙️ 配置管理指南](configuration-guide.md) - 学会使用自动生成的插件配置文件
|
||||
- [⚙️ 配置文件系统指南](configuration-guide.md) - 学会使用自动生成的插件配置文件
|
||||
- [📄 Manifest系统指南](manifest-guide.md) - 了解插件元数据管理和配置架构
|
||||
|
||||
Command vs Action 选择指南
|
||||
|
||||
1. 使用Command的场景
|
||||
|
||||
- ✅ 用户需要明确调用特定功能
|
||||
- ✅ 需要精确的参数控制
|
||||
- ✅ 管理和配置操作
|
||||
- ✅ 查询和信息显示
|
||||
- ✅ 系统维护命令
|
||||
|
||||
2. 使用Action的场景
|
||||
|
||||
- ✅ 增强麦麦的智能行为
|
||||
- ✅ 根据上下文自动触发
|
||||
- ✅ 情绪和表情表达
|
||||
- ✅ 智能建议和帮助
|
||||
- ✅ 随机化的互动
|
||||
|
||||
|
||||
## API浏览
|
||||
|
||||
### 消息发送与处理API
|
||||
@@ -53,3 +72,9 @@
|
||||
2. 查看相关示例代码
|
||||
3. 参考其他类似插件
|
||||
4. 提交文档仓库issue
|
||||
|
||||
## 一个方便的小设计
|
||||
|
||||
我们在`__init__.py`中定义了一个`__all__`变量,包含了所有需要导出的类和函数。
|
||||
这样在其他地方导入时,可以直接使用 `from src.plugin_system import *` 来导入所有插件相关的类和函数。
|
||||
或者你可以直接使用 `from src.plugin_system import BasePlugin, register_plugin, ComponentInfo` 之类的方式来导入你需要的部分。
|
||||
@@ -1,20 +1,20 @@
|
||||
# 🚀 快速开始指南
|
||||
|
||||
本指南将带你用5分钟时间,从零开始创建一个功能完整的MaiCore插件。
|
||||
本指南将带你从零开始创建一个功能完整的MaiCore插件。
|
||||
|
||||
## 📖 概述
|
||||
|
||||
这个指南将带你快速创建你的第一个MaiCore插件。我们将创建一个简单的问候插件,展示插件系统的基本概念。无需阅读其他文档,跟着本指南就能完成!
|
||||
这个指南将带你快速创建你的第一个MaiCore插件。我们将创建一个简单的问候插件,展示插件系统的基本概念。
|
||||
|
||||
## 🎯 学习目标
|
||||
以下代码都在我们的`plugins/hello_world_plugin/`目录下。
|
||||
|
||||
- 理解插件的基本结构
|
||||
- 从最简单的插件开始,循序渐进
|
||||
- 学会创建Action组件(智能动作)
|
||||
- 学会创建Command组件(命令响应)
|
||||
- 掌握配置Schema定义和配置文件自动生成(可选)
|
||||
### 一个方便的小设计
|
||||
|
||||
## 📂 准备工作
|
||||
在开发中,我们在`__init__.py`中定义了一个`__all__`变量,包含了所有需要导出的类和函数。
|
||||
这样在其他地方导入时,可以直接使用 `from src.plugin_system import *` 来导入所有插件相关的类和函数。
|
||||
或者你可以直接使用 `from src.plugin_system import BasePlugin, register_plugin, ComponentInfo` 之类的方式来导入你需要的部分。
|
||||
|
||||
### 📂 准备工作
|
||||
|
||||
确保你已经:
|
||||
|
||||
@@ -26,16 +26,29 @@
|
||||
|
||||
### 1. 创建插件目录
|
||||
|
||||
在项目根目录的 `plugins/` 文件夹下创建你的插件目录,目录名与插件名保持一致:
|
||||
在项目根目录的 `plugins/` 文件夹下创建你的插件目录
|
||||
|
||||
可以用以下命令快速创建:
|
||||
这里我们创建一个名为 `hello_world_plugin` 的目录
|
||||
|
||||
```bash
|
||||
mkdir plugins/hello_world_plugin
|
||||
cd plugins/hello_world_plugin
|
||||
### 2. 创建`_manifest.json`文件
|
||||
|
||||
在插件目录下面创建一个 `_manifest.json` 文件,内容如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"manifest_version": 1,
|
||||
"name": "Hello World 插件",
|
||||
"version": "1.0.0",
|
||||
"description": "一个简单的 Hello World 插件",
|
||||
"author": {
|
||||
"name": "你的名字"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 创建最简单的插件
|
||||
有关 `_manifest.json` 的详细说明,请参考 [Manifest文件指南](./manifest-guide.md)。
|
||||
|
||||
### 3. 创建最简单的插件
|
||||
|
||||
让我们从最基础的开始!创建 `plugin.py` 文件:
|
||||
|
||||
@@ -43,34 +56,33 @@ cd plugins/hello_world_plugin
|
||||
from typing import List, Tuple, Type
|
||||
from src.plugin_system import BasePlugin, register_plugin, ComponentInfo
|
||||
|
||||
# ===== 插件注册 =====
|
||||
|
||||
@register_plugin
|
||||
@register_plugin # 注册插件
|
||||
class HelloWorldPlugin(BasePlugin):
|
||||
"""Hello World插件 - 你的第一个MaiCore插件"""
|
||||
|
||||
# 插件基本信息(必须填写)
|
||||
# 以下是插件基本信息和方法(必须填写)
|
||||
plugin_name = "hello_world_plugin"
|
||||
plugin_description = "我的第一个MaiCore插件"
|
||||
plugin_version = "1.0.0"
|
||||
plugin_author = "你的名字"
|
||||
enable_plugin = True # 启用插件
|
||||
dependencies = [] # 插件依赖列表(目前为空)
|
||||
python_dependencies = [] # Python依赖列表(目前为空)
|
||||
config_file_name = "config.toml" # 配置文件名
|
||||
config_schema = {} # 配置文件模式(目前为空)
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]: # 获取插件组件
|
||||
"""返回插件包含的组件列表(目前是空的)"""
|
||||
return []
|
||||
```
|
||||
|
||||
🎉 **恭喜!你刚刚创建了一个最简单但完整的MaiCore插件!**
|
||||
🎉 恭喜!你刚刚创建了一个最简单但完整的MaiCore插件!
|
||||
|
||||
**解释一下这些代码:**
|
||||
|
||||
- 首先,我们在plugin.py中定义了一个HelloWorldPulgin插件类,继承自 `BasePlugin` ,提供基本功能。
|
||||
- 首先,我们在`plugin.py`中定义了一个HelloWorldPlugin插件类,继承自 `BasePlugin` ,提供基本功能。
|
||||
- 通过给类加上,`@register_plugin` 装饰器,我们告诉系统"这是一个插件"
|
||||
- `plugin_name` 等是插件的基本信息,必须填写,**此部分必须与目录名称相同,否则插件无法使用**
|
||||
- `get_plugin_components()` 返回插件的功能组件,现在我们没有定义任何action(动作)或者command(指令),是空的
|
||||
- `plugin_name` 等是插件的基本信息,必须填写
|
||||
- `get_plugin_components()` 返回插件的功能组件,现在我们没有定义任何 Action, Command 或者 EventHandler,所以返回空列表。
|
||||
|
||||
### 3. 测试基础插件
|
||||
### 4. 测试基础插件
|
||||
|
||||
现在就可以测试这个插件了!启动MaiCore:
|
||||
|
||||
@@ -80,7 +92,7 @@ class HelloWorldPlugin(BasePlugin):
|
||||
|
||||

|
||||
|
||||
### 4. 添加第一个功能:问候Action
|
||||
### 5. 添加第一个功能:问候Action
|
||||
|
||||
现在我们要给插件加入一个有用的功能,我们从最好玩的Action做起
|
||||
|
||||
@@ -107,40 +119,34 @@ class HelloAction(BaseAction):
|
||||
# === 基本信息(必须填写)===
|
||||
action_name = "hello_greeting"
|
||||
action_description = "向用户发送问候消息"
|
||||
activation_type = ActionActivationType.ALWAYS # 始终激活
|
||||
|
||||
# === 功能描述(必须填写)===
|
||||
action_parameters = {
|
||||
"greeting_message": "要发送的问候消息"
|
||||
}
|
||||
action_require = [
|
||||
"需要发送友好问候时使用",
|
||||
"当有人向你问好时使用",
|
||||
"当你遇见没有见过的人时使用"
|
||||
]
|
||||
action_parameters = {"greeting_message": "要发送的问候消息"}
|
||||
action_require = ["需要发送友好问候时使用", "当有人向你问好时使用", "当你遇见没有见过的人时使用"]
|
||||
associated_types = ["text"]
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
"""执行问候动作 - 这是核心功能"""
|
||||
# 发送问候消息
|
||||
greeting_message = self.action_data.get("greeting_message","")
|
||||
|
||||
message = "嗨!很开心见到你!😊" + greeting_message
|
||||
greeting_message = self.action_data.get("greeting_message", "")
|
||||
base_message = self.get_config("greeting.message", "嗨!很开心见到你!😊")
|
||||
message = base_message + greeting_message
|
||||
await self.send_text(message)
|
||||
|
||||
return True, "发送了问候消息"
|
||||
|
||||
# ===== 插件注册 =====
|
||||
|
||||
@register_plugin
|
||||
class HelloWorldPlugin(BasePlugin):
|
||||
"""Hello World插件 - 你的第一个MaiCore插件"""
|
||||
|
||||
# 插件基本信息
|
||||
plugin_name = "hello_world_plugin"
|
||||
plugin_description = "我的第一个MaiCore插件,包含问候功能"
|
||||
plugin_version = "1.0.0"
|
||||
plugin_author = "你的名字"
|
||||
enable_plugin = True
|
||||
dependencies = []
|
||||
python_dependencies = []
|
||||
config_file_name = "config.toml"
|
||||
config_schema = {}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
"""返回插件包含的组件列表"""
|
||||
@@ -150,13 +156,17 @@ class HelloWorldPlugin(BasePlugin):
|
||||
]
|
||||
```
|
||||
|
||||
**新增内容解释:**
|
||||
**解释一下这些代码:**
|
||||
|
||||
- `HelloAction` 是一个Action组件,MaiCore可能会选择使用它
|
||||
- `HelloAction` 是我们定义的问候动作类,继承自 `BaseAction`,并实现了核心功能。
|
||||
- 在 `HelloWorldPlugin` 中,我们通过 `get_plugin_components()` 方法,通过调用`get_action_info()`这个内置方法将 `HelloAction` 注册为插件的一个组件。
|
||||
- 这样一来,当插件被加载时,问候动作也会被一并加载,并可以在MaiCore中使用。
|
||||
- `execute()` 函数是Action的核心,定义了当Action被MaiCore选择后,具体要做什么
|
||||
- `self.send_text()` 是发送文本消息的便捷方法
|
||||
|
||||
### 5. 测试问候功能
|
||||
Action 组件中有关`activation_type`、`action_parameters`、`action_require`、`associated_types` 等的详细说明请参考 [Action组件指南](./action-components.md)。
|
||||
|
||||
### 6. 测试问候Action
|
||||
|
||||
重启MaiCore,然后在聊天中发送任意消息,比如:
|
||||
|
||||
@@ -174,96 +184,17 @@ MaiCore可能会选择使用你的问候Action,发送回复:
|
||||
|
||||
> **💡 小提示**:MaiCore会智能地决定什么时候使用它。如果没有立即看到效果,多试几次不同的消息。
|
||||
|
||||
🎉 **太棒了!你的插件已经有实际功能了!**
|
||||
🎉 太棒了!你的插件已经有实际功能了!
|
||||
|
||||
### 5.5. 了解激活系统(重要概念)
|
||||
|
||||
Action固然好用简单,但是现在有个问题,当用户加载了非常多的插件,添加了很多自定义Action,LLM需要选择的Action也会变多
|
||||
|
||||
而不断增多的Action会加大LLM的消耗和负担,降低Action使用的精准度。而且我们并不需要LLM在所有时候都考虑所有Action
|
||||
|
||||
例如,当群友只是在进行正常的聊天,就没有必要每次都考虑是否要选择“禁言”动作,这不仅影响决策速度,还会增加消耗。
|
||||
|
||||
那有什么办法,能够让Action有选择的加入MaiCore的决策池呢?
|
||||
|
||||
**什么是激活系统?**
|
||||
激活系统决定了什么时候你的Action会被MaiCore"考虑"使用:
|
||||
|
||||
- **`ActionActivationType.ALWAYS`** - 总是可用(默认值)
|
||||
- **`ActionActivationType.KEYWORD`** - 只有消息包含特定关键词时才可用
|
||||
- **`ActionActivationType.PROBABILITY`** - 根据概率随机可用
|
||||
- **`ActionActivationType.NEVER`** - 永不可用(用于调试)
|
||||
|
||||
> **💡 使用提示**:
|
||||
>
|
||||
> - 推荐使用枚举类型(如 `ActionActivationType.ALWAYS`),有代码提示和类型检查
|
||||
> - 也可以直接使用字符串(如 `"always"`),系统都支持
|
||||
|
||||
### 5.6. 进阶:尝试关键词激活(可选)
|
||||
|
||||
现在让我们尝试一个更精确的激活方式!添加一个只在用户说特定关键词时才激活的Action:
|
||||
|
||||
```python
|
||||
# 在HelloAction后面添加这个新Action
|
||||
class ByeAction(BaseAction):
|
||||
"""告别Action - 只在用户说再见时激活"""
|
||||
|
||||
action_name = "bye_greeting"
|
||||
action_description = "向用户发送告别消息"
|
||||
|
||||
# 使用关键词激活
|
||||
focus_activation_type = ActionActivationType.KEYWORD
|
||||
normal_activation_type = ActionActivationType.KEYWORD
|
||||
|
||||
# 关键词设置
|
||||
activation_keywords = ["再见", "bye", "88", "拜拜"]
|
||||
keyword_case_sensitive = False
|
||||
|
||||
action_parameters = {"bye_message": "要发送的告别消息"}
|
||||
action_require = [
|
||||
"用户要告别时使用",
|
||||
"当有人要离开时使用",
|
||||
"当有人和你说再见时使用",
|
||||
]
|
||||
associated_types = ["text"]
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
bye_message = self.action_data.get("bye_message","")
|
||||
|
||||
message = "再见!期待下次聊天!👋" + bye_message
|
||||
await self.send_text(message)
|
||||
return True, "发送了告别消息"
|
||||
```
|
||||
|
||||
然后在插件注册中添加这个Action:
|
||||
|
||||
```python
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
return [
|
||||
(HelloAction.get_action_info(), HelloAction),
|
||||
(ByeAction.get_action_info(), ByeAction), # 添加告别Action
|
||||
]
|
||||
```
|
||||
|
||||
现在测试:发送"再见",应该会触发告别Action!
|
||||
|
||||
**关键词激活的特点:**
|
||||
|
||||
- 更精确:只在包含特定关键词时才会被考虑
|
||||
- 更可预测:用户知道说什么会触发什么功能
|
||||
- 更适合:特定场景或命令式的功能
|
||||
|
||||
### 6. 添加第二个功能:时间查询Command
|
||||
### 7. 添加第二个功能:时间查询Command
|
||||
|
||||
现在让我们添加一个Command组件。Command和Action不同,它是直接响应用户命令的:
|
||||
|
||||
Command是最简单,最直接的相应,不由LLM判断选择使用
|
||||
Command是最简单,最直接的响应,不由LLM判断选择使用
|
||||
|
||||
```python
|
||||
# 在现有代码基础上,添加Command组件
|
||||
|
||||
# ===== Command组件 =====
|
||||
|
||||
import datetime
|
||||
from src.plugin_system import BaseCommand
|
||||
#导入Command基类
|
||||
|
||||
@@ -275,53 +206,49 @@ class TimeCommand(BaseCommand):
|
||||
|
||||
# === 命令设置(必须填写)===
|
||||
command_pattern = r"^/time$" # 精确匹配 "/time" 命令
|
||||
command_help = "查询当前时间"
|
||||
command_examples = ["/time"]
|
||||
intercept_message = True # 拦截消息,不让其他组件处理
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
async def execute(self) -> Tuple[bool, Optional[str], bool]:
|
||||
"""执行时间查询"""
|
||||
import datetime
|
||||
|
||||
# 获取当前时间
|
||||
time_format = self.get_config("time.format", "%Y-%m-%d %H:%M:%S")
|
||||
time_format: str = "%Y-%m-%d %H:%M:%S"
|
||||
now = datetime.datetime.now()
|
||||
time_str = now.strftime(time_format)
|
||||
|
||||
|
||||
# 发送时间信息
|
||||
message = f"⏰ 当前时间:{time_str}"
|
||||
await self.send_text(message)
|
||||
|
||||
return True, f"显示了当前时间: {time_str}"
|
||||
|
||||
# ===== 插件注册 =====
|
||||
return True, f"显示了当前时间: {time_str}", True
|
||||
|
||||
@register_plugin
|
||||
class HelloWorldPlugin(BasePlugin):
|
||||
"""Hello World插件 - 你的第一个MaiCore插件"""
|
||||
|
||||
# 插件基本信息
|
||||
plugin_name = "hello_world_plugin"
|
||||
plugin_description = "我的第一个MaiCore插件,包含问候和时间查询功能"
|
||||
plugin_version = "1.0.0"
|
||||
plugin_author = "你的名字"
|
||||
enable_plugin = True
|
||||
dependencies = []
|
||||
python_dependencies = []
|
||||
config_file_name = "config.toml"
|
||||
config_schema = {}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
return [
|
||||
(HelloAction.get_action_info(), HelloAction),
|
||||
(ByeAction.get_action_info(), ByeAction),
|
||||
(TimeCommand.get_command_info(), TimeCommand),
|
||||
]
|
||||
```
|
||||
|
||||
同样的,我们通过 `get_plugin_components()` 方法,通过调用`get_action_info()`这个内置方法将 `TimeCommand` 注册为插件的一个组件。
|
||||
|
||||
**Command组件解释:**
|
||||
|
||||
- Command是直接响应用户命令的组件
|
||||
- `command_pattern` 使用正则表达式匹配用户输入
|
||||
- `^/time$` 表示精确匹配 "/time"
|
||||
- `intercept_message = True` 表示处理完命令后不再让其他组件处理
|
||||
|
||||
### 7. 测试时间查询功能
|
||||
有关 Command 组件的更多信息,请参考 [Command组件指南](./command-components.md)。
|
||||
|
||||
### 8. 测试时间查询Command
|
||||
|
||||
重启MaiCore,发送命令:
|
||||
|
||||
@@ -332,106 +259,147 @@ class HelloWorldPlugin(BasePlugin):
|
||||
你应该会收到回复:
|
||||
|
||||
```
|
||||
⏰ 当前时间:2024-01-01 12:30:45
|
||||
⏰ 当前时间:2024-01-01 12:00:00
|
||||
```
|
||||
|
||||
🎉 **太棒了!现在你的插件有3个功能了!**
|
||||
🎉 太棒了!现在你已经了解了基本的 Action 和 Command 组件的使用方法。你可以根据自己的需求,继续扩展插件的功能,添加更多的 Action 和 Command 组件,让你的插件更加丰富和强大!
|
||||
|
||||
### 8. 添加配置文件(可选进阶)
|
||||
---
|
||||
|
||||
如果你想让插件更加灵活,可以添加配置支持。
|
||||
## 进阶教程
|
||||
|
||||
如果你想让插件更加灵活和强大,可以参考接下来的进阶教程。
|
||||
|
||||
### 1. 添加配置文件
|
||||
|
||||
想要为插件添加配置文件吗?让我们一起来配置`config_schema`属性!
|
||||
|
||||
> **🚨 重要:不要手动创建config.toml文件!**
|
||||
>
|
||||
> 我们需要在插件代码中定义配置Schema,让系统自动生成配置文件。
|
||||
|
||||
#### 📄 配置架构说明
|
||||
|
||||
在新的插件系统中,我们采用了**职责分离**的设计:
|
||||
|
||||
- **`_manifest.json`** - 插件元数据(名称、版本、描述、作者等)
|
||||
- **`config.toml`** - 运行时配置(启用状态、功能参数等)
|
||||
|
||||
这样避免了信息重复,提高了维护性。
|
||||
|
||||
首先,在插件类中定义配置Schema:
|
||||
|
||||
```python
|
||||
from src.plugin_system.base.config_types import ConfigField
|
||||
from src.plugin_system import ConfigField
|
||||
|
||||
@register_plugin
|
||||
class HelloWorldPlugin(BasePlugin):
|
||||
"""Hello World插件 - 你的第一个MaiCore插件"""
|
||||
|
||||
plugin_name = "hello_world_plugin"
|
||||
plugin_description = "我的第一个MaiCore插件,包含问候和时间查询功能"
|
||||
plugin_version = "1.0.0"
|
||||
plugin_author = "你的名字"
|
||||
enable_plugin = True
|
||||
config_file_name = "config.toml" # 配置文件名
|
||||
|
||||
# 配置节描述
|
||||
config_section_descriptions = {
|
||||
"plugin": "插件启用配置",
|
||||
"greeting": "问候功能配置",
|
||||
"time": "时间查询配置"
|
||||
}
|
||||
# 插件基本信息
|
||||
plugin_name: str = "hello_world_plugin" # 内部标识符
|
||||
enable_plugin: bool = True
|
||||
dependencies: List[str] = [] # 插件依赖列表
|
||||
python_dependencies: List[str] = [] # Python包依赖列表
|
||||
config_file_name: str = "config.toml" # 配置文件名
|
||||
|
||||
# 配置Schema定义
|
||||
config_schema = {
|
||||
config_schema: dict = {
|
||||
"plugin": {
|
||||
"enabled": ConfigField(type=bool, default=True, description="是否启用插件")
|
||||
"name": ConfigField(type=str, default="hello_world_plugin", description="插件名称"),
|
||||
"version": ConfigField(type=str, default="1.0.0", description="插件版本"),
|
||||
"enabled": ConfigField(type=bool, default=False, description="是否启用插件"),
|
||||
},
|
||||
"greeting": {
|
||||
"message": ConfigField(
|
||||
type=str,
|
||||
default="嗨!很开心见到你!😊",
|
||||
description="默认问候消息"
|
||||
),
|
||||
"enable_emoji": ConfigField(type=bool, default=True, description="是否启用表情符号")
|
||||
"message": ConfigField(type=str, default="嗨!很开心见到你!😊", description="默认问候消息"),
|
||||
"enable_emoji": ConfigField(type=bool, default=True, description="是否启用表情符号"),
|
||||
},
|
||||
"time": {
|
||||
"format": ConfigField(
|
||||
type=str,
|
||||
default="%Y-%m-%d %H:%M:%S",
|
||||
description="时间显示格式"
|
||||
)
|
||||
}
|
||||
"time": {"format": ConfigField(type=str, default="%Y-%m-%d %H:%M:%S", description="时间显示格式")},
|
||||
}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
return [
|
||||
(HelloAction.get_action_info(), HelloAction),
|
||||
(ByeAction.get_action_info(), ByeAction),
|
||||
(TimeCommand.get_command_info(), TimeCommand),
|
||||
]
|
||||
```
|
||||
|
||||
然后修改Action和Command代码,让它们读取配置:
|
||||
这会生成一个如下的 `config.toml` 文件:
|
||||
|
||||
```toml
|
||||
# hello_world_plugin - 自动生成的配置文件
|
||||
# 我的第一个MaiCore插件,包含问候功能和时间查询等基础示例
|
||||
|
||||
# 插件基本信息
|
||||
[plugin]
|
||||
|
||||
# 插件名称
|
||||
name = "hello_world_plugin"
|
||||
|
||||
# 插件版本
|
||||
version = "1.0.0"
|
||||
|
||||
# 是否启用插件
|
||||
enabled = false
|
||||
|
||||
|
||||
# 问候功能配置
|
||||
[greeting]
|
||||
|
||||
# 默认问候消息
|
||||
message = "嗨!很开心见到你!😊"
|
||||
|
||||
# 是否启用表情符号
|
||||
enable_emoji = true
|
||||
|
||||
|
||||
# 时间查询配置
|
||||
[time]
|
||||
|
||||
# 时间显示格式
|
||||
format = "%Y-%m-%d %H:%M:%S"
|
||||
```
|
||||
|
||||
然后修改Action和Command代码,通过 `get_config()` 方法让它们读取配置(配置的键是命名空间式的):
|
||||
|
||||
```python
|
||||
# 在HelloAction的execute方法中:
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
# 从配置文件读取问候消息
|
||||
greeting_message = self.action_data.get("greeting_message", "")
|
||||
base_message = self.get_config("greeting.message", "嗨!很开心见到你!😊")
|
||||
|
||||
message = base_message + greeting_message
|
||||
await self.send_text(message)
|
||||
return True, "发送了问候消息"
|
||||
class HelloAction(BaseAction):
|
||||
"""问候Action - 简单的问候动作"""
|
||||
|
||||
# 在TimeCommand的execute方法中:
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
import datetime
|
||||
|
||||
# 从配置文件读取时间格式
|
||||
time_format = self.get_config("time.format", "%Y-%m-%d %H:%M:%S")
|
||||
now = datetime.datetime.now()
|
||||
time_str = now.strftime(time_format)
|
||||
|
||||
message = f"⏰ 当前时间:{time_str}"
|
||||
await self.send_text(message)
|
||||
return True, f"显示了当前时间: {time_str}"
|
||||
# === 基本信息(必须填写)===
|
||||
action_name = "hello_greeting"
|
||||
action_description = "向用户发送问候消息"
|
||||
activation_type = ActionActivationType.ALWAYS # 始终激活
|
||||
|
||||
# === 功能描述(必须填写)===
|
||||
action_parameters = {"greeting_message": "要发送的问候消息"}
|
||||
action_require = ["需要发送友好问候时使用", "当有人向你问好时使用", "当你遇见没有见过的人时使用"]
|
||||
associated_types = ["text"]
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
"""执行问候动作 - 这是核心功能"""
|
||||
# 发送问候消息
|
||||
greeting_message = self.action_data.get("greeting_message", "")
|
||||
base_message = self.get_config("greeting.message", "嗨!很开心见到你!😊")
|
||||
message = base_message + greeting_message
|
||||
await self.send_text(message)
|
||||
|
||||
return True, "发送了问候消息"
|
||||
|
||||
class TimeCommand(BaseCommand):
|
||||
"""时间查询Command - 响应/time命令"""
|
||||
|
||||
command_name = "time"
|
||||
command_description = "查询当前时间"
|
||||
|
||||
# === 命令设置(必须填写)===
|
||||
command_pattern = r"^/time$" # 精确匹配 "/time" 命令
|
||||
|
||||
async def execute(self) -> Tuple[bool, str, bool]:
|
||||
"""执行时间查询"""
|
||||
import datetime
|
||||
|
||||
# 获取当前时间
|
||||
time_format: str = self.get_config("time.format", "%Y-%m-%d %H:%M:%S") # type: ignore
|
||||
now = datetime.datetime.now()
|
||||
time_str = now.strftime(time_format)
|
||||
|
||||
# 发送时间信息
|
||||
message = f"⏰ 当前时间:{time_str}"
|
||||
await self.send_text(message)
|
||||
|
||||
return True, f"显示了当前时间: {time_str}", True
|
||||
```
|
||||
|
||||
**配置系统工作流程:**
|
||||
@@ -441,47 +409,20 @@ async def execute(self) -> Tuple[bool, str]:
|
||||
3. **用户修改**: 用户可以修改生成的配置文件
|
||||
4. **代码读取**: 使用 `self.get_config()` 读取配置值
|
||||
|
||||
**配置功能解释:**
|
||||
**绝对不要手动创建 `config.toml` 文件!**
|
||||
|
||||
- `self.get_config()` 可以读取配置文件中的值
|
||||
- 第一个参数是配置路径(用点分隔),第二个参数是默认值
|
||||
- 配置文件会包含详细的注释和说明,用户可以轻松理解和修改
|
||||
- **绝不要手动创建配置文件**,让系统自动生成
|
||||
更详细的配置系统介绍请参考 [配置指南](./configuration-guide.md)。
|
||||
|
||||
### 9. 创建说明文档(可选)
|
||||
### 2. 创建说明文档
|
||||
|
||||
创建 `README.md` 文件来说明你的插件:
|
||||
你可以创建一个 `README.md` 文件,描述插件的功能和使用方法。
|
||||
|
||||
```markdown
|
||||
# Hello World 插件
|
||||
### 3. 发布到插件市场
|
||||
|
||||
## 概述
|
||||
我的第一个MaiCore插件,包含问候和时间查询功能。
|
||||
如果你想让更多人使用你的插件,可以将它发布到MaiCore的插件市场。
|
||||
|
||||
## 功能
|
||||
- **问候功能**: 当用户说"你好"、"hello"、"hi"时自动回复
|
||||
- **时间查询**: 发送 `/time` 命令查询当前时间
|
||||
这部分请参考 [plugin-repo](https://github.com/Maim-with-u/plugin-repo) 的文档。
|
||||
|
||||
## 使用方法
|
||||
### 问候功能
|
||||
发送包含以下关键词的消息:
|
||||
- "你好"
|
||||
- "hello"
|
||||
- "hi"
|
||||
---
|
||||
|
||||
### 时间查询
|
||||
发送命令:`/time`
|
||||
|
||||
## 配置文件
|
||||
插件会自动生成 `config.toml` 配置文件,用户可以修改:
|
||||
- 问候消息内容
|
||||
- 时间显示格式
|
||||
- 插件启用状态
|
||||
|
||||
注意:配置文件是自动生成的,不要手动创建!
|
||||
```
|
||||
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
🎉 恭喜你!你已经成功的创建了自己的插件了!
|
||||
|
||||
@@ -1425,3 +1425,4 @@ def main():
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import asyncio
|
||||
import time
|
||||
import traceback
|
||||
import random
|
||||
from typing import List, Optional, Dict, Any
|
||||
from typing import List, Optional, Dict, Any, Tuple
|
||||
from rich.traceback import install
|
||||
|
||||
from src.config.config import global_config
|
||||
@@ -18,11 +18,12 @@ from src.chat.chat_loop.hfc_utils import CycleDetail
|
||||
from src.person_info.relationship_builder_manager import relationship_builder_manager
|
||||
from src.person_info.person_info import get_person_info_manager
|
||||
from src.plugin_system.base.component_types import ActionInfo, ChatMode
|
||||
from src.plugin_system.apis import generator_api, send_api, message_api
|
||||
from src.plugin_system.apis import generator_api, send_api, message_api, database_api
|
||||
from src.chat.willing.willing_manager import get_willing_manager
|
||||
from src.mais4u.mai_think import mai_thinking_manager
|
||||
from maim_message.message_base import GroupInfo
|
||||
from src.mais4u.constant_s4u import ENABLE_S4U
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
from src.chat.chat_loop.hfc_utils import send_typing, stop_typing
|
||||
|
||||
ERROR_LOOP_INFO = {
|
||||
"loop_plan_info": {
|
||||
@@ -88,11 +89,6 @@ class HeartFChatting:
|
||||
|
||||
self.loop_mode = ChatMode.NORMAL # 初始循环模式为普通模式
|
||||
|
||||
# 新增:消息计数器和疲惫阈值
|
||||
self._message_count = 0 # 发送的消息计数
|
||||
self._message_threshold = max(10, int(30 * global_config.chat.focus_value))
|
||||
self._fatigue_triggered = False # 是否已触发疲惫退出
|
||||
|
||||
self.action_manager = ActionManager()
|
||||
self.action_planner = ActionPlanner(chat_id=self.stream_id, action_manager=self.action_manager)
|
||||
self.action_modifier = ActionModifier(action_manager=self.action_manager, chat_id=self.stream_id)
|
||||
@@ -112,7 +108,6 @@ class HeartFChatting:
|
||||
|
||||
self.last_read_time = time.time() - 1
|
||||
|
||||
self.willing_amplifier = 1
|
||||
self.willing_manager = get_willing_manager()
|
||||
|
||||
logger.info(f"{self.log_prefix} HeartFChatting 初始化完成")
|
||||
@@ -182,6 +177,9 @@ class HeartFChatting:
|
||||
if self.loop_mode == ChatMode.NORMAL:
|
||||
self.energy_value -= 0.3
|
||||
self.energy_value = max(self.energy_value, 0.3)
|
||||
if self.loop_mode == ChatMode.FOCUS:
|
||||
self.energy_value -= 0.6
|
||||
self.energy_value = max(self.energy_value, 0.3)
|
||||
|
||||
def print_cycle_info(self, cycle_timers):
|
||||
# 记录循环信息和计时器结果
|
||||
@@ -200,9 +198,9 @@ class HeartFChatting:
|
||||
async def _loopbody(self):
|
||||
if self.loop_mode == ChatMode.FOCUS:
|
||||
if await self._observe():
|
||||
self.energy_value -= 1 * global_config.chat.focus_value
|
||||
self.energy_value -= 1 / global_config.chat.focus_value
|
||||
else:
|
||||
self.energy_value -= 3 * global_config.chat.focus_value
|
||||
self.energy_value -= 3 / global_config.chat.focus_value
|
||||
if self.energy_value <= 1:
|
||||
self.energy_value = 1
|
||||
self.loop_mode = ChatMode.NORMAL
|
||||
@@ -218,15 +216,17 @@ class HeartFChatting:
|
||||
limit_mode="earliest",
|
||||
filter_bot=True,
|
||||
)
|
||||
if global_config.chat.focus_value != 0:
|
||||
if len(new_messages_data) > 3 / pow(global_config.chat.focus_value, 0.5):
|
||||
self.loop_mode = ChatMode.FOCUS
|
||||
self.energy_value = (
|
||||
10 + (len(new_messages_data) / (3 / pow(global_config.chat.focus_value, 0.5))) * 10
|
||||
)
|
||||
return True
|
||||
|
||||
if len(new_messages_data) > 3 * global_config.chat.focus_value:
|
||||
self.loop_mode = ChatMode.FOCUS
|
||||
self.energy_value = 10 + (len(new_messages_data) / (3 * global_config.chat.focus_value)) * 10
|
||||
return True
|
||||
|
||||
if self.energy_value >= 30 * global_config.chat.focus_value:
|
||||
self.loop_mode = ChatMode.FOCUS
|
||||
return True
|
||||
if self.energy_value >= 30:
|
||||
self.loop_mode = ChatMode.FOCUS
|
||||
return True
|
||||
|
||||
if new_messages_data:
|
||||
earliest_messages_data = new_messages_data[0]
|
||||
@@ -235,10 +235,10 @@ class HeartFChatting:
|
||||
if_think = await self.normal_response(earliest_messages_data)
|
||||
if if_think:
|
||||
factor = max(global_config.chat.focus_value, 0.1)
|
||||
self.energy_value *= 1.1 / factor
|
||||
self.energy_value *= 1.1 * factor
|
||||
logger.info(f"{self.log_prefix} 进行了思考,能量值按倍数增加,当前能量值:{self.energy_value:.1f}")
|
||||
else:
|
||||
self.energy_value += 0.1 / global_config.chat.focus_value
|
||||
self.energy_value += 0.1 * global_config.chat.focus_value
|
||||
logger.debug(f"{self.log_prefix} 没有进行思考,能量值线性增加,当前能量值:{self.energy_value:.1f}")
|
||||
|
||||
logger.debug(f"{self.log_prefix} 当前能量值:{self.energy_value:.1f}")
|
||||
@@ -257,44 +257,69 @@ class HeartFChatting:
|
||||
person_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
return f"{person_name}:{message_data.get('processed_plain_text')}"
|
||||
|
||||
async def send_typing(self):
|
||||
group_info = GroupInfo(platform="amaidesu_default", group_id="114514", group_name="内心")
|
||||
async def _send_and_store_reply(
|
||||
self,
|
||||
response_set,
|
||||
reply_to_str,
|
||||
loop_start_time,
|
||||
action_message,
|
||||
cycle_timers: Dict[str, float],
|
||||
thinking_id,
|
||||
plan_result,
|
||||
) -> Tuple[Dict[str, Any], str, Dict[str, float]]:
|
||||
with Timer("回复发送", cycle_timers):
|
||||
reply_text = await self._send_response(response_set, reply_to_str, loop_start_time, action_message)
|
||||
|
||||
chat = await get_chat_manager().get_or_create_stream(
|
||||
platform="amaidesu_default",
|
||||
user_info=None,
|
||||
group_info=group_info,
|
||||
# 存储reply action信息
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_id = person_info_manager.get_person_id(
|
||||
action_message.get("chat_info_platform", ""),
|
||||
action_message.get("user_id", ""),
|
||||
)
|
||||
person_name = await person_info_manager.get_value(person_id, "person_name")
|
||||
action_prompt_display = f"你对{person_name}进行了回复:{reply_text}"
|
||||
|
||||
await database_api.store_action_info(
|
||||
chat_stream=self.chat_stream,
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=action_prompt_display,
|
||||
action_done=True,
|
||||
thinking_id=thinking_id,
|
||||
action_data={"reply_text": reply_text, "reply_to": reply_to_str},
|
||||
action_name="reply",
|
||||
)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="typing", stream_id=chat.stream_id, storage_message=False
|
||||
)
|
||||
# 构建循环信息
|
||||
loop_info: Dict[str, Any] = {
|
||||
"loop_plan_info": {
|
||||
"action_result": plan_result.get("action_result", {}),
|
||||
},
|
||||
"loop_action_info": {
|
||||
"action_taken": True,
|
||||
"reply_text": reply_text,
|
||||
"command": "",
|
||||
"taken_time": time.time(),
|
||||
},
|
||||
}
|
||||
|
||||
async def stop_typing(self):
|
||||
group_info = GroupInfo(platform="amaidesu_default", group_id="114514", group_name="内心")
|
||||
|
||||
chat = await get_chat_manager().get_or_create_stream(
|
||||
platform="amaidesu_default",
|
||||
user_info=None,
|
||||
group_info=group_info,
|
||||
)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="stop_typing", stream_id=chat.stream_id, storage_message=False
|
||||
)
|
||||
return loop_info, reply_text, cycle_timers
|
||||
|
||||
async def _observe(self, message_data: Optional[Dict[str, Any]] = None):
|
||||
# sourcery skip: hoist-statement-from-if, merge-comparisons, reintroduce-else
|
||||
if not message_data:
|
||||
message_data = {}
|
||||
action_type = "no_action"
|
||||
reply_text = "" # 初始化reply_text变量,避免UnboundLocalError
|
||||
gen_task = None # 初始化gen_task变量,避免UnboundLocalError
|
||||
reply_to_str = "" # 初始化reply_to_str变量
|
||||
|
||||
# 创建新的循环信息
|
||||
cycle_timers, thinking_id = self.start_cycle()
|
||||
|
||||
logger.info(f"{self.log_prefix} 开始第{self._cycle_counter}次思考[模式:{self.loop_mode}]")
|
||||
|
||||
|
||||
if ENABLE_S4U:
|
||||
await self.send_typing()
|
||||
await send_typing()
|
||||
|
||||
async with global_prompt_manager.async_message_scope(self.chat_stream.context.get_template_name()):
|
||||
loop_start_time = time.time()
|
||||
@@ -310,95 +335,254 @@ class HeartFChatting:
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 动作修改失败: {e}")
|
||||
|
||||
# 如果normal,开始一个回复生成进程,先准备好回复(其实是和planer同时进行的)
|
||||
# 检查是否在normal模式下没有可用动作(除了reply相关动作)
|
||||
skip_planner = False
|
||||
if self.loop_mode == ChatMode.NORMAL:
|
||||
reply_to_str = await self.build_reply_to_str(message_data)
|
||||
gen_task = asyncio.create_task(self._generate_response(message_data, available_actions, reply_to_str))
|
||||
# 过滤掉reply相关的动作,检查是否还有其他动作
|
||||
non_reply_actions = {
|
||||
k: v for k, v in available_actions.items() if k not in ["reply", "no_reply", "no_action"]
|
||||
}
|
||||
|
||||
with Timer("规划器", cycle_timers):
|
||||
plan_result, target_message = await self.action_planner.plan(mode=self.loop_mode)
|
||||
if not non_reply_actions:
|
||||
skip_planner = True
|
||||
logger.info(f"{self.log_prefix} Normal模式下没有可用动作,直接回复")
|
||||
|
||||
action_result: dict = plan_result.get("action_result", {}) # type: ignore
|
||||
action_type, action_data, reasoning, is_parallel = (
|
||||
action_result.get("action_type", "error"),
|
||||
action_result.get("action_data", {}),
|
||||
action_result.get("reasoning", "未提供理由"),
|
||||
action_result.get("is_parallel", True),
|
||||
)
|
||||
# 直接设置为reply动作
|
||||
action_type = "reply"
|
||||
reasoning = ""
|
||||
action_data = {"loop_start_time": loop_start_time}
|
||||
is_parallel = False
|
||||
|
||||
action_data["loop_start_time"] = loop_start_time
|
||||
# 构建plan_result用于后续处理
|
||||
plan_result = {
|
||||
"action_result": {
|
||||
"action_type": action_type,
|
||||
"action_data": action_data,
|
||||
"reasoning": reasoning,
|
||||
"timestamp": time.time(),
|
||||
"is_parallel": is_parallel,
|
||||
},
|
||||
"action_prompt": "",
|
||||
}
|
||||
target_message = message_data
|
||||
|
||||
if self.loop_mode == ChatMode.NORMAL:
|
||||
if action_type == "no_action":
|
||||
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复")
|
||||
elif is_parallel:
|
||||
logger.info(
|
||||
f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作"
|
||||
# 如果normal模式且不跳过规划器,开始一个回复生成进程,先准备好回复(其实是和planer同时进行的)
|
||||
if not skip_planner:
|
||||
reply_to_str = await self.build_reply_to_str(message_data)
|
||||
gen_task = asyncio.create_task(
|
||||
self._generate_response(
|
||||
message_data=message_data,
|
||||
available_actions=available_actions,
|
||||
reply_to=reply_to_str,
|
||||
request_type="chat.replyer.normal",
|
||||
)
|
||||
)
|
||||
else:
|
||||
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定执行{action_type}动作")
|
||||
|
||||
if action_type == "no_action":
|
||||
if not skip_planner:
|
||||
with Timer("规划器", cycle_timers):
|
||||
plan_result, target_message = await self.action_planner.plan(mode=self.loop_mode)
|
||||
|
||||
action_result: Dict[str, Any] = plan_result.get("action_result", {}) # type: ignore
|
||||
action_type, action_data, reasoning, is_parallel = (
|
||||
action_result.get("action_type", "error"),
|
||||
action_result.get("action_data", {}),
|
||||
action_result.get("reasoning", "未提供理由"),
|
||||
action_result.get("is_parallel", True),
|
||||
)
|
||||
|
||||
action_data["loop_start_time"] = loop_start_time
|
||||
|
||||
if action_type == "reply":
|
||||
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复")
|
||||
elif is_parallel:
|
||||
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作")
|
||||
else:
|
||||
# 只有在gen_task存在时才进行相关操作
|
||||
if gen_task:
|
||||
if not gen_task.done():
|
||||
gen_task.cancel()
|
||||
logger.debug(f"{self.log_prefix} 已取消预生成的回复任务")
|
||||
logger.info(
|
||||
f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复,但选择执行{action_type},不发表回复"
|
||||
)
|
||||
elif generation_result := gen_task.result():
|
||||
content = " ".join([item[1] for item in generation_result if item[0] == "text"])
|
||||
logger.debug(f"{self.log_prefix} 预生成的回复任务已完成")
|
||||
logger.info(
|
||||
f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复:{content},但选择执行{action_type},不发表回复"
|
||||
)
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 预生成的回复任务未生成有效内容")
|
||||
|
||||
action_message: Dict[str, Any] = message_data or target_message # type: ignore
|
||||
if action_type == "reply":
|
||||
# 等待回复生成完毕
|
||||
gather_timeout = global_config.chat.thinking_timeout
|
||||
try:
|
||||
response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout)
|
||||
except asyncio.TimeoutError:
|
||||
response_set = None
|
||||
if self.loop_mode == ChatMode.NORMAL:
|
||||
# 只有在gen_task存在时才等待
|
||||
if not gen_task:
|
||||
reply_to_str = await self.build_reply_to_str(message_data)
|
||||
gen_task = asyncio.create_task(
|
||||
self._generate_response(
|
||||
message_data=message_data,
|
||||
available_actions=available_actions,
|
||||
reply_to=reply_to_str,
|
||||
request_type="chat.replyer.normal",
|
||||
)
|
||||
)
|
||||
|
||||
if response_set:
|
||||
content = " ".join([item[1] for item in response_set if item[0] == "text"])
|
||||
gather_timeout = global_config.chat.thinking_timeout
|
||||
try:
|
||||
response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout)
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"{self.log_prefix} 回复生成超时>{global_config.chat.thinking_timeout}s,已跳过")
|
||||
response_set = None
|
||||
|
||||
# 模型炸了,没有回复内容生成
|
||||
if not response_set:
|
||||
logger.warning(f"{self.log_prefix}模型未生成回复内容")
|
||||
return False
|
||||
elif action_type not in ["no_action"] and not is_parallel:
|
||||
logger.info(
|
||||
f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复:{content},但选择执行{action_type},不发表回复"
|
||||
)
|
||||
return False
|
||||
# 模型炸了或超时,没有回复内容生成
|
||||
if not response_set:
|
||||
logger.warning(f"{self.log_prefix}模型未生成回复内容")
|
||||
return False
|
||||
else:
|
||||
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复 (focus模式)")
|
||||
|
||||
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定的回复内容: {content}")
|
||||
# 构建reply_to字符串
|
||||
reply_to_str = await self.build_reply_to_str(action_message)
|
||||
|
||||
# 发送回复 (不再需要传入 chat)
|
||||
reply_text = await self._send_response(response_set, reply_to_str, loop_start_time,message_data)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if ENABLE_S4U:
|
||||
await self.stop_typing()
|
||||
await mai_thinking_manager.get_mai_think(self.stream_id).do_think_after_response(reply_text)
|
||||
# 生成回复
|
||||
with Timer("回复生成", cycle_timers):
|
||||
response_set = await self._generate_response(
|
||||
message_data=action_message,
|
||||
available_actions=available_actions,
|
||||
reply_to=reply_to_str,
|
||||
request_type="chat.replyer.focus",
|
||||
)
|
||||
|
||||
if not response_set:
|
||||
logger.warning(f"{self.log_prefix}模型未生成回复内容")
|
||||
return False
|
||||
|
||||
loop_info, reply_text, cycle_timers = await self._send_and_store_reply(
|
||||
response_set, reply_to_str, loop_start_time, action_message, cycle_timers, thinking_id, plan_result
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
else:
|
||||
action_message: Dict[str, Any] = message_data or target_message # type: ignore
|
||||
# 并行执行:同时进行回复发送和动作执行
|
||||
# 先置空防止未定义错误
|
||||
background_reply_task = None
|
||||
background_action_task = None
|
||||
# 如果是并行执行且在normal模式下,需要等待预生成的回复任务完成并发送回复
|
||||
if self.loop_mode == ChatMode.NORMAL and is_parallel and gen_task:
|
||||
|
||||
# 动作执行计时
|
||||
with Timer("动作执行", cycle_timers):
|
||||
success, reply_text, command = await self._handle_action(
|
||||
action_type, reasoning, action_data, cycle_timers, thinking_id, action_message
|
||||
async def handle_reply_task() -> Tuple[Optional[Dict[str, Any]], str, Dict[str, float]]:
|
||||
# 等待预生成的回复任务完成
|
||||
gather_timeout = global_config.chat.thinking_timeout
|
||||
try:
|
||||
response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout)
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(
|
||||
f"{self.log_prefix} 并行执行:回复生成超时>{global_config.chat.thinking_timeout}s,已跳过"
|
||||
)
|
||||
return None, "", {}
|
||||
except asyncio.CancelledError:
|
||||
logger.debug(f"{self.log_prefix} 并行执行:回复生成任务已被取消")
|
||||
return None, "", {}
|
||||
|
||||
if not response_set:
|
||||
logger.warning(f"{self.log_prefix} 模型超时或生成回复内容为空")
|
||||
return None, "", {}
|
||||
|
||||
reply_to_str = await self.build_reply_to_str(action_message)
|
||||
loop_info, reply_text, cycle_timers_reply = await self._send_and_store_reply(
|
||||
response_set,
|
||||
reply_to_str,
|
||||
loop_start_time,
|
||||
action_message,
|
||||
cycle_timers,
|
||||
thinking_id,
|
||||
plan_result,
|
||||
)
|
||||
return loop_info, reply_text, cycle_timers_reply
|
||||
|
||||
# 执行回复任务并赋值到变量
|
||||
background_reply_task = asyncio.create_task(handle_reply_task())
|
||||
|
||||
# 动作执行任务
|
||||
async def handle_action_task():
|
||||
with Timer("动作执行", cycle_timers):
|
||||
success, reply_text, command = await self._handle_action(
|
||||
action_type, reasoning, action_data, cycle_timers, thinking_id, action_message
|
||||
)
|
||||
return success, reply_text, command
|
||||
|
||||
# 执行动作任务并赋值到变量
|
||||
background_action_task = asyncio.create_task(handle_action_task())
|
||||
|
||||
reply_loop_info = None
|
||||
reply_text_from_reply = ""
|
||||
action_success = False
|
||||
action_reply_text = ""
|
||||
action_command = ""
|
||||
|
||||
# 并行执行所有任务
|
||||
if background_reply_task:
|
||||
results = await asyncio.gather(
|
||||
background_reply_task, background_action_task, return_exceptions=True
|
||||
)
|
||||
# 处理回复任务结果
|
||||
reply_result = results[0]
|
||||
if isinstance(reply_result, BaseException):
|
||||
logger.error(f"{self.log_prefix} 回复任务执行异常: {reply_result}")
|
||||
elif reply_result and reply_result[0] is not None:
|
||||
reply_loop_info, reply_text_from_reply, _ = reply_result
|
||||
|
||||
loop_info = {
|
||||
"loop_plan_info": {
|
||||
"action_result": plan_result.get("action_result", {}),
|
||||
},
|
||||
"loop_action_info": {
|
||||
"action_taken": success,
|
||||
"reply_text": reply_text,
|
||||
"command": command,
|
||||
"taken_time": time.time(),
|
||||
},
|
||||
}
|
||||
# 处理动作任务结果
|
||||
action_task_result = results[1]
|
||||
if isinstance(action_task_result, BaseException):
|
||||
logger.error(f"{self.log_prefix} 动作任务执行异常: {action_task_result}")
|
||||
else:
|
||||
action_success, action_reply_text, action_command = action_task_result
|
||||
else:
|
||||
results = await asyncio.gather(background_action_task, return_exceptions=True)
|
||||
# 只有动作任务
|
||||
action_task_result = results[0]
|
||||
if isinstance(action_task_result, BaseException):
|
||||
logger.error(f"{self.log_prefix} 动作任务执行异常: {action_task_result}")
|
||||
else:
|
||||
action_success, action_reply_text, action_command = action_task_result
|
||||
|
||||
if loop_info["loop_action_info"]["command"] == "stop_focus_chat":
|
||||
logger.info(f"{self.log_prefix} 麦麦决定停止专注聊天")
|
||||
return False
|
||||
# 停止该聊天模式的循环
|
||||
# 构建最终的循环信息
|
||||
if reply_loop_info:
|
||||
# 如果有回复信息,使用回复的loop_info作为基础
|
||||
loop_info = reply_loop_info
|
||||
# 更新动作执行信息
|
||||
loop_info["loop_action_info"].update(
|
||||
{
|
||||
"action_taken": action_success,
|
||||
"command": action_command,
|
||||
"taken_time": time.time(),
|
||||
}
|
||||
)
|
||||
reply_text = reply_text_from_reply
|
||||
else:
|
||||
# 没有回复信息,构建纯动作的loop_info
|
||||
loop_info = {
|
||||
"loop_plan_info": {
|
||||
"action_result": plan_result.get("action_result", {}),
|
||||
},
|
||||
"loop_action_info": {
|
||||
"action_taken": action_success,
|
||||
"reply_text": action_reply_text,
|
||||
"command": action_command,
|
||||
"taken_time": time.time(),
|
||||
},
|
||||
}
|
||||
reply_text = action_reply_text
|
||||
|
||||
if ENABLE_S4U:
|
||||
await stop_typing()
|
||||
await mai_thinking_manager.get_mai_think(self.stream_id).do_think_after_response(reply_text)
|
||||
|
||||
self.end_cycle(loop_info, cycle_timers)
|
||||
self.print_cycle_info(cycle_timers)
|
||||
@@ -406,8 +590,16 @@ class HeartFChatting:
|
||||
if self.loop_mode == ChatMode.NORMAL:
|
||||
await self.willing_manager.after_generate_reply_handle(message_data.get("message_id", ""))
|
||||
|
||||
# 管理no_reply计数器:当执行了非no_reply动作时,重置计数器
|
||||
if action_type != "no_reply" and action_type != "no_action":
|
||||
# 导入NoReplyAction并重置计数器
|
||||
NoReplyAction.reset_consecutive_count()
|
||||
logger.info(f"{self.log_prefix} 执行了{action_type}动作,重置no_reply计数器")
|
||||
return True
|
||||
elif action_type == "no_action":
|
||||
# 当执行回复动作时,也重置no_reply计数器s
|
||||
NoReplyAction.reset_consecutive_count()
|
||||
logger.info(f"{self.log_prefix} 执行了回复动作,重置no_reply计数器")
|
||||
|
||||
return True
|
||||
|
||||
@@ -435,7 +627,7 @@ class HeartFChatting:
|
||||
action: str,
|
||||
reasoning: str,
|
||||
action_data: dict,
|
||||
cycle_timers: dict,
|
||||
cycle_timers: Dict[str, float],
|
||||
thinking_id: str,
|
||||
action_message: dict,
|
||||
) -> tuple[bool, str, str]:
|
||||
@@ -501,7 +693,7 @@ class HeartFChatting:
|
||||
在"兴趣"模式下,判断是否回复并生成内容。
|
||||
"""
|
||||
|
||||
interested_rate = (message_data.get("interest_value") or 0.0) * self.willing_amplifier
|
||||
interested_rate = (message_data.get("interest_value") or 0.0) * global_config.chat.willing_amplifier
|
||||
|
||||
self.willing_manager.setup(message_data, self.chat_stream)
|
||||
|
||||
@@ -515,8 +707,8 @@ class HeartFChatting:
|
||||
reply_probability += additional_config["maimcore_reply_probability_gain"]
|
||||
reply_probability = min(max(reply_probability, 0), 1) # 确保概率在 0-1 之间
|
||||
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.stream_id)
|
||||
reply_probability = talk_frequency * reply_probability
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.stream_id)
|
||||
reply_probability = talk_frequency * reply_probability
|
||||
|
||||
# 处理表情包
|
||||
if message_data.get("is_emoji") or message_data.get("is_picid"):
|
||||
@@ -544,7 +736,11 @@ class HeartFChatting:
|
||||
return False
|
||||
|
||||
async def _generate_response(
|
||||
self, message_data: dict, available_actions: Optional[Dict[str, ActionInfo]], reply_to: str
|
||||
self,
|
||||
message_data: dict,
|
||||
available_actions: Optional[Dict[str, ActionInfo]],
|
||||
reply_to: str,
|
||||
request_type: str = "chat.replyer.normal",
|
||||
) -> Optional[list]:
|
||||
"""生成普通回复"""
|
||||
try:
|
||||
@@ -552,8 +748,8 @@ class HeartFChatting:
|
||||
chat_stream=self.chat_stream,
|
||||
reply_to=reply_to,
|
||||
available_actions=available_actions,
|
||||
enable_tool=global_config.tool.enable_in_normal_chat,
|
||||
request_type="chat.replyer.normal",
|
||||
enable_tool=global_config.tool.enable_tool,
|
||||
request_type=request_type,
|
||||
)
|
||||
|
||||
if not success or not reply_set:
|
||||
@@ -566,7 +762,7 @@ class HeartFChatting:
|
||||
logger.error(f"{self.log_prefix}回复生成出现错误:{str(e)} {traceback.format_exc()}")
|
||||
return None
|
||||
|
||||
async def _send_response(self, reply_set, reply_to, thinking_start_time, message_data):
|
||||
async def _send_response(self, reply_set, reply_to, thinking_start_time, message_data) -> str:
|
||||
current_time = time.time()
|
||||
new_message_count = message_api.count_new_messages(
|
||||
chat_id=self.chat_stream.stream_id, start_time=thinking_start_time, end_time=current_time
|
||||
@@ -578,13 +774,9 @@ class HeartFChatting:
|
||||
need_reply = new_message_count >= random.randint(2, 4)
|
||||
|
||||
if need_reply:
|
||||
logger.info(
|
||||
f"{self.log_prefix} 从思考到回复,共有{new_message_count}条新消息,使用引用回复"
|
||||
)
|
||||
logger.info(f"{self.log_prefix} 从思考到回复,共有{new_message_count}条新消息,使用引用回复")
|
||||
else:
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 从思考到回复,共有{new_message_count}条新消息,不使用引用回复"
|
||||
)
|
||||
logger.info(f"{self.log_prefix} 从思考到回复,共有{new_message_count}条新消息,不使用引用回复")
|
||||
|
||||
reply_text = ""
|
||||
first_replied = False
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import time
|
||||
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
from src.config.config import global_config
|
||||
from src.common.message_repository import count_messages
|
||||
from src.common.logger import get_logger
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.plugin_system.apis import send_api
|
||||
from maim_message.message_base import GroupInfo
|
||||
|
||||
from src.common.message_repository import count_messages
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -106,3 +109,30 @@ def get_recent_message_stats(minutes: float = 30, chat_id: Optional[str] = None)
|
||||
bot_reply_count = count_messages(bot_filter)
|
||||
|
||||
return {"bot_reply_count": bot_reply_count, "total_message_count": total_message_count}
|
||||
|
||||
|
||||
async def send_typing():
|
||||
group_info = GroupInfo(platform="amaidesu_default", group_id="114514", group_name="内心")
|
||||
|
||||
chat = await get_chat_manager().get_or_create_stream(
|
||||
platform="amaidesu_default",
|
||||
user_info=None,
|
||||
group_info=group_info,
|
||||
)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="typing", stream_id=chat.stream_id, storage_message=False
|
||||
)
|
||||
|
||||
async def stop_typing():
|
||||
group_info = GroupInfo(platform="amaidesu_default", group_id="114514", group_name="内心")
|
||||
|
||||
chat = await get_chat_manager().get_or_create_stream(
|
||||
platform="amaidesu_default",
|
||||
user_info=None,
|
||||
group_info=group_info,
|
||||
)
|
||||
|
||||
await send_api.custom_to_stream(
|
||||
message_type="state", content="stop_typing", stream_id=chat.stream_id, storage_message=False
|
||||
)
|
||||
@@ -525,9 +525,9 @@ class EmojiManager:
|
||||
如果文件已被删除,则执行对象的删除方法并从列表中移除
|
||||
"""
|
||||
try:
|
||||
if not self.emoji_objects:
|
||||
logger.warning("[检查] emoji_objects为空,跳过完整性检查")
|
||||
return
|
||||
# if not self.emoji_objects:
|
||||
# logger.warning("[检查] emoji_objects为空,跳过完整性检查")
|
||||
# return
|
||||
|
||||
total_count = len(self.emoji_objects)
|
||||
self.emoji_num = total_count
|
||||
@@ -707,6 +707,38 @@ class EmojiManager:
|
||||
return emoji
|
||||
return None # 如果循环结束还没找到,则返回 None
|
||||
|
||||
async def get_emoji_description_by_hash(self, emoji_hash: str) -> Optional[str]:
|
||||
"""根据哈希值获取已注册表情包的描述
|
||||
|
||||
Args:
|
||||
emoji_hash: 表情包的哈希值
|
||||
|
||||
Returns:
|
||||
Optional[str]: 表情包描述,如果未找到则返回None
|
||||
"""
|
||||
try:
|
||||
# 先从内存中查找
|
||||
emoji = await self.get_emoji_from_manager(emoji_hash)
|
||||
if emoji and emoji.description:
|
||||
logger.info(f"[缓存命中] 从内存获取表情包描述: {emoji.description[:50]}...")
|
||||
return emoji.description
|
||||
|
||||
# 如果内存中没有,从数据库查找
|
||||
self._ensure_db()
|
||||
try:
|
||||
emoji_record = Emoji.get_or_none(Emoji.emoji_hash == emoji_hash)
|
||||
if emoji_record and emoji_record.description:
|
||||
logger.info(f"[缓存命中] 从数据库获取表情包描述: {emoji_record.description[:50]}...")
|
||||
return emoji_record.description
|
||||
except Exception as e:
|
||||
logger.error(f"从数据库查询表情包描述时出错: {e}")
|
||||
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取表情包描述失败 (Hash: {emoji_hash}): {str(e)}")
|
||||
return None
|
||||
|
||||
async def delete_emoji(self, emoji_hash: str) -> bool:
|
||||
"""根据哈希值删除表情包
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ def init_prompt() -> None:
|
||||
当"想说明某个具体的事实观点,但懒得明说,或者不便明说,或表达一种默契",使用"懂的都懂"
|
||||
当"当涉及游戏相关时,表示意外的夸赞,略带戏谑意味"时,使用"这么强!"
|
||||
|
||||
注意不要总结你自己(SELF)的发言
|
||||
请注意:不要总结你自己(SELF)的发言
|
||||
现在请你概括
|
||||
"""
|
||||
Prompt(learn_style_prompt, "learn_style_prompt")
|
||||
@@ -330,48 +330,8 @@ class ExpressionLearner:
|
||||
"""
|
||||
current_time = time.time()
|
||||
|
||||
# 全局衰减所有已存储的表达方式
|
||||
for type in ["style", "grammar"]:
|
||||
base_dir = os.path.join("data", "expression", f"learnt_{type}")
|
||||
if not os.path.exists(base_dir):
|
||||
logger.debug(f"目录不存在,跳过衰减: {base_dir}")
|
||||
continue
|
||||
|
||||
try:
|
||||
chat_ids = os.listdir(base_dir)
|
||||
logger.debug(f"在 {base_dir} 中找到 {len(chat_ids)} 个聊天ID目录进行衰减")
|
||||
except Exception as e:
|
||||
logger.error(f"读取目录失败 {base_dir}: {e}")
|
||||
continue
|
||||
|
||||
for chat_id in chat_ids:
|
||||
file_path = os.path.join(base_dir, chat_id, "expressions.json")
|
||||
if not os.path.exists(file_path):
|
||||
continue
|
||||
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
expressions = json.load(f)
|
||||
|
||||
if not isinstance(expressions, list):
|
||||
logger.warning(f"表达方式文件格式错误,跳过衰减: {file_path}")
|
||||
continue
|
||||
|
||||
# 应用全局衰减
|
||||
decayed_expressions = self.apply_decay_to_expressions(expressions, current_time)
|
||||
|
||||
# 保存衰减后的结果
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(decayed_expressions, f, ensure_ascii=False, indent=2)
|
||||
|
||||
logger.debug(f"已对 {file_path} 应用衰减,剩余 {len(decayed_expressions)} 个表达方式")
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"JSON解析失败,跳过衰减 {file_path}: {e}")
|
||||
except PermissionError as e:
|
||||
logger.error(f"权限不足,无法更新 {file_path}: {e}")
|
||||
except Exception as e:
|
||||
logger.error(f"全局衰减{type}表达方式失败 {file_path}: {e}")
|
||||
continue
|
||||
# 全局衰减所有已存储的表达方式(直接操作数据库)
|
||||
self._apply_global_decay_to_database(current_time)
|
||||
|
||||
learnt_style: Optional[List[Tuple[str, str, str]]] = []
|
||||
learnt_grammar: Optional[List[Tuple[str, str, str]]] = []
|
||||
@@ -388,6 +348,42 @@ class ExpressionLearner:
|
||||
|
||||
return learnt_style, learnt_grammar
|
||||
|
||||
def _apply_global_decay_to_database(self, current_time: float) -> None:
|
||||
"""
|
||||
对数据库中的所有表达方式应用全局衰减
|
||||
"""
|
||||
try:
|
||||
# 获取所有表达方式
|
||||
all_expressions = Expression.select()
|
||||
|
||||
updated_count = 0
|
||||
deleted_count = 0
|
||||
|
||||
for expr in all_expressions:
|
||||
# 计算时间差
|
||||
last_active = expr.last_active_time
|
||||
time_diff_days = (current_time - last_active) / (24 * 3600) # 转换为天
|
||||
|
||||
# 计算衰减值
|
||||
decay_value = self.calculate_decay_factor(time_diff_days)
|
||||
new_count = max(0.01, expr.count - decay_value)
|
||||
|
||||
if new_count <= 0.01:
|
||||
# 如果count太小,删除这个表达方式
|
||||
expr.delete_instance()
|
||||
deleted_count += 1
|
||||
else:
|
||||
# 更新count
|
||||
expr.count = new_count
|
||||
expr.save()
|
||||
updated_count += 1
|
||||
|
||||
if updated_count > 0 or deleted_count > 0:
|
||||
logger.info(f"全局衰减完成:更新了 {updated_count} 个表达方式,删除了 {deleted_count} 个表达方式")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"数据库全局衰减失败: {e}")
|
||||
|
||||
def calculate_decay_factor(self, time_diff_days: float) -> float:
|
||||
"""
|
||||
计算衰减值
|
||||
@@ -410,30 +406,6 @@ class ExpressionLearner:
|
||||
|
||||
return min(0.01, decay)
|
||||
|
||||
def apply_decay_to_expressions(
|
||||
self, expressions: List[Dict[str, Any]], current_time: float
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
对表达式列表应用衰减
|
||||
返回衰减后的表达式列表,移除count小于0的项
|
||||
"""
|
||||
result = []
|
||||
for expr in expressions:
|
||||
# 确保last_active_time存在,如果不存在则使用current_time
|
||||
if "last_active_time" not in expr:
|
||||
expr["last_active_time"] = current_time
|
||||
|
||||
last_active = expr["last_active_time"]
|
||||
time_diff_days = (current_time - last_active) / (24 * 3600) # 转换为天
|
||||
|
||||
decay_value = self.calculate_decay_factor(time_diff_days)
|
||||
expr["count"] = max(0.01, expr.get("count", 1) - decay_value)
|
||||
|
||||
if expr["count"] > 0:
|
||||
result.append(expr)
|
||||
|
||||
return result
|
||||
|
||||
async def learn_and_store(self, type: str, num: int = 10) -> List[Tuple[str, str, str]]:
|
||||
# sourcery skip: use-join
|
||||
"""
|
||||
|
||||
@@ -2,7 +2,7 @@ import json
|
||||
import time
|
||||
import random
|
||||
|
||||
from typing import List, Dict, Tuple, Optional
|
||||
from typing import List, Dict, Tuple, Optional, Any
|
||||
from json_repair import repair_json
|
||||
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
@@ -117,36 +117,42 @@ class ExpressionSelector:
|
||||
|
||||
def get_random_expressions(
|
||||
self, chat_id: str, total_num: int, style_percentage: float, grammar_percentage: float
|
||||
) -> Tuple[List[Dict[str, str]], List[Dict[str, str]]]:
|
||||
) -> Tuple[List[Dict[str, Any]], List[Dict[str, Any]]]:
|
||||
# 支持多chat_id合并抽选
|
||||
related_chat_ids = self.get_related_chat_ids(chat_id)
|
||||
style_exprs = []
|
||||
grammar_exprs = []
|
||||
for cid in related_chat_ids:
|
||||
style_query = Expression.select().where((Expression.chat_id == cid) & (Expression.type == "style"))
|
||||
grammar_query = Expression.select().where((Expression.chat_id == cid) & (Expression.type == "grammar"))
|
||||
style_exprs.extend([
|
||||
{
|
||||
"situation": expr.situation,
|
||||
"style": expr.style,
|
||||
"count": expr.count,
|
||||
"last_active_time": expr.last_active_time,
|
||||
"source_id": cid,
|
||||
"type": "style",
|
||||
"create_date": expr.create_date if expr.create_date is not None else expr.last_active_time,
|
||||
} for expr in style_query
|
||||
])
|
||||
grammar_exprs.extend([
|
||||
{
|
||||
"situation": expr.situation,
|
||||
"style": expr.style,
|
||||
"count": expr.count,
|
||||
"last_active_time": expr.last_active_time,
|
||||
"source_id": cid,
|
||||
"type": "grammar",
|
||||
"create_date": expr.create_date if expr.create_date is not None else expr.last_active_time,
|
||||
} for expr in grammar_query
|
||||
])
|
||||
|
||||
# 优化:一次性查询所有相关chat_id的表达方式
|
||||
style_query = Expression.select().where(
|
||||
(Expression.chat_id.in_(related_chat_ids)) & (Expression.type == "style")
|
||||
)
|
||||
grammar_query = Expression.select().where(
|
||||
(Expression.chat_id.in_(related_chat_ids)) & (Expression.type == "grammar")
|
||||
)
|
||||
|
||||
style_exprs = [
|
||||
{
|
||||
"situation": expr.situation,
|
||||
"style": expr.style,
|
||||
"count": expr.count,
|
||||
"last_active_time": expr.last_active_time,
|
||||
"source_id": expr.chat_id,
|
||||
"type": "style",
|
||||
"create_date": expr.create_date if expr.create_date is not None else expr.last_active_time,
|
||||
} for expr in style_query
|
||||
]
|
||||
|
||||
grammar_exprs = [
|
||||
{
|
||||
"situation": expr.situation,
|
||||
"style": expr.style,
|
||||
"count": expr.count,
|
||||
"last_active_time": expr.last_active_time,
|
||||
"source_id": expr.chat_id,
|
||||
"type": "grammar",
|
||||
"create_date": expr.create_date if expr.create_date is not None else expr.last_active_time,
|
||||
} for expr in grammar_query
|
||||
]
|
||||
|
||||
style_num = int(total_num * style_percentage)
|
||||
grammar_num = int(total_num * grammar_percentage)
|
||||
# 按权重抽样(使用count作为权重)
|
||||
@@ -162,7 +168,7 @@ class ExpressionSelector:
|
||||
selected_grammar = []
|
||||
return selected_style, selected_grammar
|
||||
|
||||
def update_expressions_count_batch(self, expressions_to_update: List[Dict[str, str]], increment: float = 0.1):
|
||||
def update_expressions_count_batch(self, expressions_to_update: List[Dict[str, Any]], increment: float = 0.1):
|
||||
"""对一批表达方式更新count值,按chat_id+type分组后一次性写入数据库"""
|
||||
if not expressions_to_update:
|
||||
return
|
||||
@@ -203,7 +209,7 @@ class ExpressionSelector:
|
||||
max_num: int = 10,
|
||||
min_num: int = 5,
|
||||
target_message: Optional[str] = None,
|
||||
) -> List[Dict[str, str]]:
|
||||
) -> List[Dict[str, Any]]:
|
||||
# sourcery skip: inline-variable, list-comprehension
|
||||
"""使用LLM选择适合的表达方式"""
|
||||
|
||||
@@ -273,6 +279,7 @@ class ExpressionSelector:
|
||||
|
||||
if not isinstance(result, dict) or "selected_situations" not in result:
|
||||
logger.error("LLM返回格式错误")
|
||||
logger.info(f"LLM返回结果: \n{content}")
|
||||
return []
|
||||
|
||||
selected_indices = result["selected_situations"]
|
||||
|
||||
@@ -12,7 +12,7 @@ from src.chat.message_receive.storage import MessageStorage
|
||||
from src.chat.heart_flow.heartflow import heartflow
|
||||
from src.chat.utils.utils import is_mentioned_bot_in_message
|
||||
from src.chat.utils.timer_calculator import Timer
|
||||
from src.chat.utils.chat_message_builder import replace_user_references_in_content
|
||||
from src.chat.utils.chat_message_builder import replace_user_references_sync
|
||||
from src.common.logger import get_logger
|
||||
from src.person_info.relationship_manager import get_relationship_manager
|
||||
from src.mood.mood_manager import mood_manager
|
||||
@@ -151,10 +151,9 @@ class HeartFCMessageReceiver:
|
||||
processed_plain_text = re.sub(picid_pattern, "[图片]", message.processed_plain_text)
|
||||
|
||||
# 应用用户引用格式替换,将回复<aaa:bbb>和@<aaa:bbb>格式转换为可读格式
|
||||
processed_plain_text = replace_user_references_in_content(
|
||||
processed_plain_text,
|
||||
message.message_info.platform,
|
||||
is_async=False,
|
||||
processed_plain_text = replace_user_references_sync(
|
||||
processed_plain_text,
|
||||
message.message_info.platform, # type: ignore
|
||||
replace_bot_name=True
|
||||
)
|
||||
|
||||
|
||||
@@ -224,13 +224,14 @@ class Hippocampus:
|
||||
return hash((source, target))
|
||||
|
||||
@staticmethod
|
||||
def find_topic_llm(text:str, topic_num:int|list[int]):
|
||||
def find_topic_llm(text: str, topic_num: int | list[int]):
|
||||
# sourcery skip: inline-immediately-returned-variable
|
||||
topic_num_str = ""
|
||||
if isinstance(topic_num, list):
|
||||
topic_num_str = f"{topic_num[0]}-{topic_num[1]}"
|
||||
else:
|
||||
topic_num_str = topic_num
|
||||
|
||||
|
||||
prompt = (
|
||||
f"这是一段文字:\n{text}\n\n请你从这段话中总结出最多{topic_num_str}个关键的概念,可以是名词,动词,或者特定人物,帮我列出来,"
|
||||
f"将主题用逗号隔开,并加上<>,例如<主题1>,<主题2>......尽可能精简。只需要列举最多{topic_num}个话题就好,不要有序号,不要告诉我其他内容。"
|
||||
@@ -304,10 +305,10 @@ class Hippocampus:
|
||||
# 按相似度降序排序
|
||||
memories.sort(key=lambda x: x[2], reverse=True)
|
||||
return memories
|
||||
|
||||
|
||||
async def get_keywords_from_text(self, text: str) -> list:
|
||||
"""从文本中提取关键词。
|
||||
|
||||
|
||||
Args:
|
||||
text (str): 输入文本
|
||||
fast_retrieval (bool, optional): 是否使用快速检索。默认为False。
|
||||
@@ -319,25 +320,25 @@ class Hippocampus:
|
||||
|
||||
# 使用LLM提取关键词 - 根据详细文本长度分布优化topic_num计算
|
||||
text_length = len(text)
|
||||
topic_num:str|list[int] = None
|
||||
topic_num: int | list[int] = 0
|
||||
if text_length <= 5:
|
||||
words = jieba.cut(text)
|
||||
keywords = [word for word in words if len(word) > 1]
|
||||
keywords = list(set(keywords))[:3] # 限制最多3个关键词
|
||||
logger.info(f"提取关键词: {keywords}")
|
||||
if keywords:
|
||||
logger.info(f"提取关键词: {keywords}")
|
||||
return keywords
|
||||
elif text_length <= 10:
|
||||
topic_num = [1,3] # 6-10字符: 1个关键词 (27.18%的文本)
|
||||
topic_num = [1, 3] # 6-10字符: 1个关键词 (27.18%的文本)
|
||||
elif text_length <= 20:
|
||||
topic_num = [2,4] # 11-20字符: 2个关键词 (22.76%的文本)
|
||||
topic_num = [2, 4] # 11-20字符: 2个关键词 (22.76%的文本)
|
||||
elif text_length <= 30:
|
||||
topic_num = [3,5] # 21-30字符: 3个关键词 (10.33%的文本)
|
||||
topic_num = [3, 5] # 21-30字符: 3个关键词 (10.33%的文本)
|
||||
elif text_length <= 50:
|
||||
topic_num = [4,5] # 31-50字符: 4个关键词 (9.79%的文本)
|
||||
topic_num = [4, 5] # 31-50字符: 4个关键词 (9.79%的文本)
|
||||
else:
|
||||
topic_num = 5 # 51+字符: 5个关键词 (其余长文本)
|
||||
|
||||
|
||||
|
||||
topics_response, (reasoning_content, model_name) = await self.model_summary.generate_response_async(
|
||||
self.find_topic_llm(text, topic_num)
|
||||
)
|
||||
@@ -353,7 +354,8 @@ class Hippocampus:
|
||||
if keyword.strip()
|
||||
]
|
||||
|
||||
logger.info(f"提取关键词: {keywords}")
|
||||
if keywords:
|
||||
logger.info(f"提取关键词: {keywords}")
|
||||
|
||||
return keywords
|
||||
|
||||
@@ -1310,6 +1312,7 @@ class ParahippocampalGyrus:
|
||||
return compressed_memory, similar_topics_dict
|
||||
|
||||
async def operation_build_memory(self):
|
||||
# sourcery skip: merge-list-appends-into-extend
|
||||
logger.info("------------------------------------开始构建记忆--------------------------------------")
|
||||
start_time = time.time()
|
||||
memory_samples = self.hippocampus.entorhinal_cortex.get_memory_sample()
|
||||
|
||||
@@ -3,7 +3,7 @@ from src.plugin_system.base.base_action import BaseAction
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.core.component_registry import component_registry
|
||||
from src.plugin_system.base.component_types import ComponentType, ActionActivationType, ChatMode, ActionInfo
|
||||
from src.plugin_system.base.component_types import ComponentType, ActionInfo
|
||||
|
||||
logger = get_logger("action_manager")
|
||||
|
||||
@@ -15,11 +15,6 @@ class ActionManager:
|
||||
现在统一使用新插件系统,简化了原有的新旧兼容逻辑。
|
||||
"""
|
||||
|
||||
# 类常量
|
||||
DEFAULT_RANDOM_PROBABILITY = 0.3
|
||||
DEFAULT_MODE = ChatMode.ALL
|
||||
DEFAULT_ACTIVATION_TYPE = ActionActivationType.ALWAYS
|
||||
|
||||
def __init__(self):
|
||||
"""初始化动作管理器"""
|
||||
|
||||
|
||||
@@ -174,7 +174,7 @@ class ActionModifier:
|
||||
continue # 总是激活,无需处理
|
||||
|
||||
elif activation_type == ActionActivationType.RANDOM:
|
||||
probability = action_info.random_activation_probability or ActionManager.DEFAULT_RANDOM_PROBABILITY
|
||||
probability = action_info.random_activation_probability
|
||||
if random.random() >= probability:
|
||||
reason = f"RANDOM类型未触发(概率{probability})"
|
||||
deactivated_actions.append((action_name, reason))
|
||||
|
||||
@@ -33,10 +33,11 @@ def init_prompt():
|
||||
{time_block}
|
||||
{identity_block}
|
||||
你现在需要根据聊天内容,选择的合适的action来参与聊天。
|
||||
{chat_context_description},以下是具体的聊天内容:
|
||||
{chat_context_description},以下是具体的聊天内容
|
||||
{chat_content_block}
|
||||
|
||||
|
||||
|
||||
{moderation_prompt}
|
||||
|
||||
现在请你根据{by_what}选择合适的action和触发action的消息:
|
||||
@@ -45,7 +46,7 @@ def init_prompt():
|
||||
{no_action_block}
|
||||
{action_options_text}
|
||||
|
||||
你必须从上面列出的可用action中选择一个,并说明触发action的消息id和原因。
|
||||
你必须从上面列出的可用action中选择一个,并说明触发action的消息id(不是消息原文)和选择该action的原因。
|
||||
|
||||
请根据动作示例,以严格的 JSON 格式输出,且仅包含 JSON 内容:
|
||||
""",
|
||||
@@ -128,20 +129,6 @@ class ActionPlanner:
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix}使用中的动作 {action_name} 未在已注册动作中找到")
|
||||
|
||||
# 如果没有可用动作或只有no_reply动作,直接返回no_reply
|
||||
# 因为现在reply是永远激活,所以不需要空跳判定
|
||||
# if not current_available_actions:
|
||||
# action = "no_reply" if mode == ChatMode.FOCUS else "no_action"
|
||||
# reasoning = "没有可用的动作"
|
||||
# logger.info(f"{self.log_prefix}{reasoning}")
|
||||
# return {
|
||||
# "action_result": {
|
||||
# "action_type": action,
|
||||
# "action_data": action_data,
|
||||
# "reasoning": reasoning,
|
||||
# },
|
||||
# }, None
|
||||
|
||||
# --- 构建提示词 (调用修改后的 PromptBuilder 方法) ---
|
||||
prompt, message_id_list = await self.build_planner_prompt(
|
||||
is_group_chat=is_group_chat, # <-- Pass HFC state
|
||||
@@ -224,7 +211,7 @@ class ActionPlanner:
|
||||
reasoning = f"Planner 内部处理错误: {outer_e}"
|
||||
|
||||
is_parallel = False
|
||||
if action in current_available_actions:
|
||||
if mode == ChatMode.NORMAL and action in current_available_actions:
|
||||
is_parallel = current_available_actions[action].parallel_action
|
||||
|
||||
action_result = {
|
||||
@@ -268,7 +255,7 @@ class ActionPlanner:
|
||||
|
||||
actions_before_now = get_actions_by_timestamp_with_chat(
|
||||
chat_id=self.chat_id,
|
||||
timestamp_start=time.time()-3600,
|
||||
timestamp_start=time.time() - 3600,
|
||||
timestamp_end=time.time(),
|
||||
limit=5,
|
||||
)
|
||||
@@ -276,7 +263,7 @@ class ActionPlanner:
|
||||
actions_before_now_block = build_readable_actions(
|
||||
actions=actions_before_now,
|
||||
)
|
||||
|
||||
|
||||
actions_before_now_block = f"你刚刚选择并执行过的action是:\n{actions_before_now_block}"
|
||||
|
||||
self.last_obs_time_mark = time.time()
|
||||
@@ -288,7 +275,6 @@ class ActionPlanner:
|
||||
if global_config.chat.at_bot_inevitable_reply:
|
||||
mentioned_bonus = "\n- 有人提到你,或者at你"
|
||||
|
||||
|
||||
by_what = "聊天内容"
|
||||
target_prompt = '\n "target_message_id":"触发action的消息id"'
|
||||
no_action_block = f"""重要说明:
|
||||
@@ -311,7 +297,7 @@ class ActionPlanner:
|
||||
by_what = "聊天内容和用户的最新消息"
|
||||
target_prompt = ""
|
||||
no_action_block = """重要说明:
|
||||
- 'no_action' 表示只进行普通聊天回复,不执行任何额外动作
|
||||
- 'reply' 表示只进行普通聊天回复,不执行任何额外动作
|
||||
- 其他action表示在普通回复的基础上,执行相应的额外动作"""
|
||||
|
||||
chat_context_description = "你现在正在一个群聊中"
|
||||
|
||||
@@ -17,7 +17,11 @@ from src.chat.message_receive.uni_message_sender import HeartFCSender
|
||||
from src.chat.utils.timer_calculator import Timer # <--- Import Timer
|
||||
from src.chat.utils.utils import get_chat_type_and_target_info
|
||||
from src.chat.utils.prompt_builder import Prompt, global_prompt_manager
|
||||
from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat, replace_user_references_in_content
|
||||
from src.chat.utils.chat_message_builder import (
|
||||
build_readable_messages,
|
||||
get_raw_msg_before_timestamp_with_chat,
|
||||
replace_user_references_sync,
|
||||
)
|
||||
from src.chat.express.expression_selector import expression_selector
|
||||
from src.chat.knowledge.knowledge_lib import qa_manager
|
||||
from src.chat.memory_system.memory_activator import MemoryActivator
|
||||
@@ -30,38 +34,13 @@ from src.plugin_system.base.component_types import ActionInfo
|
||||
|
||||
logger = get_logger("replyer")
|
||||
|
||||
|
||||
def init_prompt():
|
||||
Prompt("你正在qq群里聊天,下面是群里在聊的内容:", "chat_target_group1")
|
||||
Prompt("你正在和{sender_name}聊天,这是你们之前聊的内容:", "chat_target_private1")
|
||||
Prompt("在群里聊天", "chat_target_group2")
|
||||
Prompt("和{sender_name}聊天", "chat_target_private2")
|
||||
Prompt("\n你有以下这些**知识**:\n{prompt_info}\n请你**记住上面的知识**,之后可能会用到。\n", "knowledge_prompt")
|
||||
|
||||
Prompt(
|
||||
"""
|
||||
{expression_habits_block}
|
||||
{tool_info_block}
|
||||
{knowledge_prompt}
|
||||
{memory_block}
|
||||
{relation_info_block}
|
||||
{extra_info_block}
|
||||
|
||||
{chat_target}
|
||||
{time_block}
|
||||
{chat_info}
|
||||
{reply_target_block}
|
||||
{identity}
|
||||
|
||||
{action_descriptions}
|
||||
你正在{chat_target_2},你现在的心情是:{mood_state}
|
||||
现在请你读读之前的聊天记录,并给出回复
|
||||
{config_expression_style}。注意不要复读你说过的话
|
||||
{keywords_reaction_prompt}
|
||||
{moderation_prompt}
|
||||
不要浮夸,不要夸张修辞,不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。只输出回复内容""",
|
||||
"default_generator_prompt",
|
||||
)
|
||||
|
||||
|
||||
Prompt(
|
||||
"""
|
||||
{expression_habits_block}
|
||||
@@ -109,7 +88,8 @@ def init_prompt():
|
||||
{core_dialogue_prompt}
|
||||
|
||||
{reply_target_block}
|
||||
对方最新发送的内容:{message_txt}
|
||||
|
||||
|
||||
你现在的心情是:{mood_state}
|
||||
{config_expression_style}
|
||||
注意不要复读你说过的话
|
||||
@@ -171,7 +151,6 @@ class DefaultReplyer:
|
||||
|
||||
async def generate_reply_with_context(
|
||||
self,
|
||||
reply_data: Optional[Dict[str, Any]] = None,
|
||||
reply_to: str = "",
|
||||
extra_info: str = "",
|
||||
available_actions: Optional[Dict[str, ActionInfo]] = None,
|
||||
@@ -179,30 +158,35 @@ class DefaultReplyer:
|
||||
enable_timeout: bool = False,
|
||||
) -> Tuple[bool, Optional[str], Optional[str]]:
|
||||
"""
|
||||
回复器 (Replier): 核心逻辑,负责生成回复文本。
|
||||
(已整合原 HeartFCGenerator 的功能)
|
||||
回复器 (Replier): 负责生成回复文本的核心逻辑。
|
||||
|
||||
Args:
|
||||
reply_to: 回复对象,格式为 "发送者:消息内容"
|
||||
extra_info: 额外信息,用于补充上下文
|
||||
available_actions: 可用的动作信息字典
|
||||
enable_tool: 是否启用工具调用
|
||||
enable_timeout: 是否启用超时处理
|
||||
|
||||
Returns:
|
||||
Tuple[bool, Optional[str], Optional[str]]: (是否成功, 生成的回复内容, 使用的prompt)
|
||||
"""
|
||||
prompt = None
|
||||
if available_actions is None:
|
||||
available_actions = {}
|
||||
try:
|
||||
if not reply_data:
|
||||
reply_data = {
|
||||
"reply_to": reply_to,
|
||||
"extra_info": extra_info,
|
||||
}
|
||||
for key, value in reply_data.items():
|
||||
if not value:
|
||||
logger.debug(f"回复数据跳过{key},生成回复时将忽略。")
|
||||
|
||||
# 3. 构建 Prompt
|
||||
with Timer("构建Prompt", {}): # 内部计时器,可选保留
|
||||
prompt = await self.build_prompt_reply_context(
|
||||
reply_data=reply_data, # 传递action_data
|
||||
reply_to = reply_to,
|
||||
extra_info=extra_info,
|
||||
available_actions=available_actions,
|
||||
enable_timeout=enable_timeout,
|
||||
enable_tool=enable_tool,
|
||||
)
|
||||
|
||||
if not prompt:
|
||||
logger.warning("构建prompt失败,跳过回复生成")
|
||||
return False, None, None
|
||||
|
||||
# 4. 调用 LLM 生成回复
|
||||
content = None
|
||||
@@ -245,25 +229,30 @@ class DefaultReplyer:
|
||||
|
||||
async def rewrite_reply_with_context(
|
||||
self,
|
||||
reply_data: Dict[str, Any],
|
||||
raw_reply: str = "",
|
||||
reason: str = "",
|
||||
reply_to: str = "",
|
||||
relation_info: str = "",
|
||||
) -> Tuple[bool, Optional[str]]:
|
||||
"""
|
||||
表达器 (Expressor): 核心逻辑,负责生成回复文本。
|
||||
表达器 (Expressor): 负责重写和优化回复文本。
|
||||
|
||||
Args:
|
||||
raw_reply: 原始回复内容
|
||||
reason: 回复原因
|
||||
reply_to: 回复对象,格式为 "发送者:消息内容"
|
||||
relation_info: 关系信息
|
||||
|
||||
Returns:
|
||||
Tuple[bool, Optional[str]]: (是否成功, 重写后的回复内容)
|
||||
"""
|
||||
try:
|
||||
if not reply_data:
|
||||
reply_data = {
|
||||
"reply_to": reply_to,
|
||||
"relation_info": relation_info,
|
||||
}
|
||||
|
||||
|
||||
with Timer("构建Prompt", {}): # 内部计时器,可选保留
|
||||
prompt = await self.build_prompt_rewrite_context(
|
||||
reply_data=reply_data,
|
||||
raw_reply=raw_reply,
|
||||
reason=reason,
|
||||
reply_to=reply_to,
|
||||
)
|
||||
|
||||
content = None
|
||||
@@ -302,14 +291,13 @@ class DefaultReplyer:
|
||||
traceback.print_exc()
|
||||
return False, None
|
||||
|
||||
async def build_relation_info(self, reply_data=None):
|
||||
async def build_relation_info(self, reply_to: str = ""):
|
||||
if not global_config.relationship.enable_relationship:
|
||||
return ""
|
||||
|
||||
relationship_fetcher = relationship_fetcher_manager.get_fetcher(self.chat_stream.stream_id)
|
||||
if not reply_data:
|
||||
if not reply_to:
|
||||
return ""
|
||||
reply_to = reply_data.get("reply_to", "")
|
||||
sender, text = self._parse_reply_target(reply_to)
|
||||
if not sender or not text:
|
||||
return ""
|
||||
@@ -323,7 +311,16 @@ class DefaultReplyer:
|
||||
|
||||
return await relationship_fetcher.build_relation_info(person_id, points_num=5)
|
||||
|
||||
async def build_expression_habits(self, chat_history, target):
|
||||
async def build_expression_habits(self, chat_history: str, target: str) -> str:
|
||||
"""构建表达习惯块
|
||||
|
||||
Args:
|
||||
chat_history: 聊天历史记录
|
||||
target: 目标消息内容
|
||||
|
||||
Returns:
|
||||
str: 表达习惯信息字符串
|
||||
"""
|
||||
if not global_config.expression.enable_expression:
|
||||
return ""
|
||||
|
||||
@@ -356,54 +353,67 @@ class DefaultReplyer:
|
||||
expression_habits_block = ""
|
||||
expression_habits_title = ""
|
||||
if style_habits_str.strip():
|
||||
expression_habits_title = "你可以参考以下的语言习惯,当情景合适就使用,但不要生硬使用,以合理的方式结合到你的回复中:"
|
||||
expression_habits_title = (
|
||||
"你可以参考以下的语言习惯,当情景合适就使用,但不要生硬使用,以合理的方式结合到你的回复中:"
|
||||
)
|
||||
expression_habits_block += f"{style_habits_str}\n"
|
||||
if grammar_habits_str.strip():
|
||||
expression_habits_title = "你可以选择下面的句法进行回复,如果情景合适就使用,不要盲目使用,不要生硬使用,以合理的方式使用:"
|
||||
expression_habits_title = (
|
||||
"你可以选择下面的句法进行回复,如果情景合适就使用,不要盲目使用,不要生硬使用,以合理的方式使用:"
|
||||
)
|
||||
expression_habits_block += f"{grammar_habits_str}\n"
|
||||
|
||||
|
||||
if style_habits_str.strip() and grammar_habits_str.strip():
|
||||
expression_habits_title = "你可以参考以下的语言习惯和句法,如果情景合适就使用,不要盲目使用,不要生硬使用,以合理的方式结合到你的回复中:"
|
||||
|
||||
|
||||
expression_habits_block = f"{expression_habits_title}\n{expression_habits_block}"
|
||||
|
||||
|
||||
return expression_habits_block
|
||||
|
||||
async def build_memory_block(self, chat_history, target):
|
||||
async def build_memory_block(self, chat_history: str, target: str) -> str:
|
||||
"""构建记忆块
|
||||
|
||||
Args:
|
||||
chat_history: 聊天历史记录
|
||||
target: 目标消息内容
|
||||
|
||||
Returns:
|
||||
str: 记忆信息字符串
|
||||
"""
|
||||
if not global_config.memory.enable_memory:
|
||||
return ""
|
||||
|
||||
instant_memory = None
|
||||
|
||||
|
||||
running_memories = await self.memory_activator.activate_memory_with_chat_history(
|
||||
target_message=target, chat_history_prompt=chat_history
|
||||
)
|
||||
|
||||
|
||||
if global_config.memory.enable_instant_memory:
|
||||
asyncio.create_task(self.instant_memory.create_and_store_memory(chat_history))
|
||||
|
||||
instant_memory = await self.instant_memory.get_memory(target)
|
||||
logger.info(f"即时记忆:{instant_memory}")
|
||||
|
||||
|
||||
if not running_memories:
|
||||
return ""
|
||||
|
||||
memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
|
||||
for running_memory in running_memories:
|
||||
memory_str += f"- {running_memory['content']}\n"
|
||||
|
||||
|
||||
if instant_memory:
|
||||
memory_str += f"- {instant_memory}\n"
|
||||
|
||||
|
||||
return memory_str
|
||||
|
||||
async def build_tool_info(self, chat_history, reply_data: Optional[Dict], enable_tool: bool = True):
|
||||
async def build_tool_info(self, chat_history: str, reply_to: str = "", enable_tool: bool = True) -> str:
|
||||
"""构建工具信息块
|
||||
|
||||
Args:
|
||||
reply_data: 回复数据,包含要回复的消息内容
|
||||
chat_history: 聊天历史
|
||||
chat_history: 聊天历史记录
|
||||
reply_to: 回复对象,格式为 "发送者:消息内容"
|
||||
enable_tool: 是否启用工具调用
|
||||
|
||||
Returns:
|
||||
str: 工具信息字符串
|
||||
@@ -412,10 +422,9 @@ class DefaultReplyer:
|
||||
if not enable_tool:
|
||||
return ""
|
||||
|
||||
if not reply_data:
|
||||
if not reply_to:
|
||||
return ""
|
||||
|
||||
reply_to = reply_data.get("reply_to", "")
|
||||
sender, text = self._parse_reply_target(reply_to)
|
||||
|
||||
if not text:
|
||||
@@ -438,7 +447,7 @@ class DefaultReplyer:
|
||||
|
||||
tool_info_str += "以上是你获取到的实时信息,请在回复时参考这些信息。"
|
||||
logger.info(f"获取到 {len(tool_results)} 个工具结果")
|
||||
|
||||
|
||||
return tool_info_str
|
||||
else:
|
||||
logger.debug("未获取到任何工具结果")
|
||||
@@ -448,7 +457,15 @@ class DefaultReplyer:
|
||||
logger.error(f"工具信息获取失败: {e}")
|
||||
return ""
|
||||
|
||||
def _parse_reply_target(self, target_message: str) -> tuple:
|
||||
def _parse_reply_target(self, target_message: str) -> Tuple[str, str]:
|
||||
"""解析回复目标消息
|
||||
|
||||
Args:
|
||||
target_message: 目标消息,格式为 "发送者:消息内容" 或 "发送者:消息内容"
|
||||
|
||||
Returns:
|
||||
Tuple[str, str]: (发送者名称, 消息内容)
|
||||
"""
|
||||
sender = ""
|
||||
target = ""
|
||||
# 添加None检查,防止NoneType错误
|
||||
@@ -462,14 +479,22 @@ class DefaultReplyer:
|
||||
target = parts[1].strip()
|
||||
return sender, target
|
||||
|
||||
async def build_keywords_reaction_prompt(self, target):
|
||||
async def build_keywords_reaction_prompt(self, target: Optional[str]) -> str:
|
||||
"""构建关键词反应提示
|
||||
|
||||
Args:
|
||||
target: 目标消息内容
|
||||
|
||||
Returns:
|
||||
str: 关键词反应提示字符串
|
||||
"""
|
||||
# 关键词检测与反应
|
||||
keywords_reaction_prompt = ""
|
||||
try:
|
||||
# 添加None检查,防止NoneType错误
|
||||
if target is None:
|
||||
return keywords_reaction_prompt
|
||||
|
||||
|
||||
# 处理关键词规则
|
||||
for rule in global_config.keyword_reaction.keyword_rules:
|
||||
if any(keyword in target for keyword in rule.keywords):
|
||||
@@ -496,15 +521,23 @@ class DefaultReplyer:
|
||||
|
||||
return keywords_reaction_prompt
|
||||
|
||||
async def _time_and_run_task(self, coroutine, name: str):
|
||||
"""一个简单的帮助函数,用于计时和运行异步任务,返回任务名、结果和耗时"""
|
||||
async def _time_and_run_task(self, coroutine, name: str) -> Tuple[str, Any, float]:
|
||||
"""计时并运行异步任务的辅助函数
|
||||
|
||||
Args:
|
||||
coroutine: 要执行的协程
|
||||
name: 任务名称
|
||||
|
||||
Returns:
|
||||
Tuple[str, Any, float]: (任务名称, 任务结果, 执行耗时)
|
||||
"""
|
||||
start_time = time.time()
|
||||
result = await coroutine
|
||||
end_time = time.time()
|
||||
duration = end_time - start_time
|
||||
return name, result, duration
|
||||
|
||||
def build_s4u_chat_history_prompts(self, message_list_before_now: list, target_user_id: str) -> tuple[str, str]:
|
||||
def build_s4u_chat_history_prompts(self, message_list_before_now: List[Dict[str, Any]], target_user_id: str) -> Tuple[str, str]:
|
||||
"""
|
||||
构建 s4u 风格的分离对话 prompt
|
||||
|
||||
@@ -513,7 +546,7 @@ class DefaultReplyer:
|
||||
target_user_id: 目标用户ID(当前对话对象)
|
||||
|
||||
Returns:
|
||||
tuple: (核心对话prompt, 背景对话prompt)
|
||||
Tuple[str, str]: (核心对话prompt, 背景对话prompt)
|
||||
"""
|
||||
core_dialogue_list = []
|
||||
background_dialogue_list = []
|
||||
@@ -532,7 +565,7 @@ class DefaultReplyer:
|
||||
# 其他用户的对话
|
||||
background_dialogue_list.append(msg_dict)
|
||||
except Exception as e:
|
||||
logger.error(f"记录: {msg_dict}, 错误: {e}")
|
||||
logger.error(f"处理消息记录时出错: {msg_dict}, 错误: {e}")
|
||||
|
||||
# 构建背景对话 prompt
|
||||
background_dialogue_prompt = ""
|
||||
@@ -577,8 +610,25 @@ class DefaultReplyer:
|
||||
sender: str,
|
||||
target: str,
|
||||
chat_info: str,
|
||||
):
|
||||
"""构建 mai_think 上下文信息"""
|
||||
) -> Any:
|
||||
"""构建 mai_think 上下文信息
|
||||
|
||||
Args:
|
||||
chat_id: 聊天ID
|
||||
memory_block: 记忆块内容
|
||||
relation_info: 关系信息
|
||||
time_block: 时间块内容
|
||||
chat_target_1: 聊天目标1
|
||||
chat_target_2: 聊天目标2
|
||||
mood_prompt: 情绪提示
|
||||
identity_block: 身份块内容
|
||||
sender: 发送者名称
|
||||
target: 目标消息内容
|
||||
chat_info: 聊天信息
|
||||
|
||||
Returns:
|
||||
Any: mai_think 实例
|
||||
"""
|
||||
mai_think = mai_thinking_manager.get_mai_think(chat_id)
|
||||
mai_think.memory_block = memory_block
|
||||
mai_think.relation_info_block = relation_info
|
||||
@@ -594,7 +644,8 @@ class DefaultReplyer:
|
||||
|
||||
async def build_prompt_reply_context(
|
||||
self,
|
||||
reply_data: Dict[str, Any],
|
||||
reply_to: str,
|
||||
extra_info: str = "",
|
||||
available_actions: Optional[Dict[str, ActionInfo]] = None,
|
||||
enable_timeout: bool = False,
|
||||
enable_tool: bool = True,
|
||||
@@ -619,9 +670,7 @@ class DefaultReplyer:
|
||||
chat_id = chat_stream.stream_id
|
||||
person_info_manager = get_person_info_manager()
|
||||
is_group_chat = bool(chat_stream.group_info)
|
||||
reply_to = reply_data.get("reply_to", "none")
|
||||
extra_info_block = reply_data.get("extra_info", "") or reply_data.get("extra_info_block", "")
|
||||
|
||||
|
||||
if global_config.mood.enable_mood:
|
||||
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
|
||||
mood_prompt = chat_mood.mood_state
|
||||
@@ -629,14 +678,15 @@ class DefaultReplyer:
|
||||
mood_prompt = ""
|
||||
|
||||
sender, target = self._parse_reply_target(reply_to)
|
||||
|
||||
target = replace_user_references_in_content(
|
||||
target,
|
||||
chat_stream.platform,
|
||||
is_async=False,
|
||||
replace_bot_name=True
|
||||
)
|
||||
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_id = person_info_manager.get_person_id_by_person_name(sender)
|
||||
user_id = person_info_manager.get_value_sync(person_id, "user_id")
|
||||
platform = chat_stream.platform
|
||||
if user_id == global_config.bot.qq_account and platform == global_config.bot.platform:
|
||||
logger.warning("选取了自身作为回复对象,跳过构建prompt")
|
||||
return ""
|
||||
|
||||
target = replace_user_references_sync(target, chat_stream.platform, replace_bot_name=True)
|
||||
|
||||
# 构建action描述 (如果启用planner)
|
||||
action_descriptions = ""
|
||||
@@ -653,21 +703,6 @@ class DefaultReplyer:
|
||||
limit=global_config.chat.max_context_size * 2,
|
||||
)
|
||||
|
||||
message_list_before_now = get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=global_config.chat.max_context_size,
|
||||
)
|
||||
chat_talking_prompt = build_readable_messages(
|
||||
message_list_before_now,
|
||||
replace_bot_name=True,
|
||||
merge_messages=False,
|
||||
timestamp_mode="normal_no_YMD",
|
||||
read_mark=0.0,
|
||||
truncate=True,
|
||||
show_actions=True,
|
||||
)
|
||||
|
||||
message_list_before_short = get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
@@ -687,25 +722,21 @@ class DefaultReplyer:
|
||||
self._time_and_run_task(
|
||||
self.build_expression_habits(chat_talking_prompt_short, target), "expression_habits"
|
||||
),
|
||||
self._time_and_run_task(
|
||||
self.build_relation_info(reply_data), "relation_info"
|
||||
),
|
||||
self._time_and_run_task(self.build_relation_info(reply_to), "relation_info"),
|
||||
self._time_and_run_task(self.build_memory_block(chat_talking_prompt_short, target), "memory_block"),
|
||||
self._time_and_run_task(
|
||||
self.build_tool_info(chat_talking_prompt_short, reply_data, enable_tool=enable_tool), "tool_info"
|
||||
),
|
||||
self._time_and_run_task(
|
||||
get_prompt_info(target, threshold=0.38), "prompt_info"
|
||||
self.build_tool_info(chat_talking_prompt_short, reply_to, enable_tool=enable_tool), "tool_info"
|
||||
),
|
||||
self._time_and_run_task(get_prompt_info(target, threshold=0.38), "prompt_info"),
|
||||
)
|
||||
|
||||
# 任务名称中英文映射
|
||||
task_name_mapping = {
|
||||
"expression_habits": "选取表达方式",
|
||||
"relation_info": "感受关系",
|
||||
"relation_info": "感受关系",
|
||||
"memory_block": "回忆",
|
||||
"tool_info": "使用工具",
|
||||
"prompt_info": "获取知识"
|
||||
"prompt_info": "获取知识",
|
||||
}
|
||||
|
||||
# 处理结果
|
||||
@@ -727,8 +758,8 @@ class DefaultReplyer:
|
||||
|
||||
keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target)
|
||||
|
||||
if extra_info_block:
|
||||
extra_info_block = f"以下是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策\n{extra_info_block}\n以上是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策"
|
||||
if extra_info:
|
||||
extra_info_block = f"以下是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策\n{extra_info}\n以上是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策"
|
||||
else:
|
||||
extra_info_block = ""
|
||||
|
||||
@@ -783,116 +814,74 @@ class DefaultReplyer:
|
||||
# 根据sender通过person_info_manager反向查找person_id,再获取user_id
|
||||
person_id = person_info_manager.get_person_id_by_person_name(sender)
|
||||
|
||||
# 根据配置选择使用哪种 prompt 构建模式
|
||||
if global_config.chat.use_s4u_prompt_mode and person_id:
|
||||
# 使用 s4u 对话构建模式:分离当前对话对象和其他对话
|
||||
try:
|
||||
user_id_value = await person_info_manager.get_value(person_id, "user_id")
|
||||
if user_id_value:
|
||||
target_user_id = str(user_id_value)
|
||||
except Exception as e:
|
||||
logger.warning(f"无法从person_id {person_id} 获取user_id: {e}")
|
||||
target_user_id = ""
|
||||
# 使用 s4u 对话构建模式:分离当前对话对象和其他对话
|
||||
try:
|
||||
user_id_value = await person_info_manager.get_value(person_id, "user_id")
|
||||
if user_id_value:
|
||||
target_user_id = str(user_id_value)
|
||||
except Exception as e:
|
||||
logger.warning(f"无法从person_id {person_id} 获取user_id: {e}")
|
||||
target_user_id = ""
|
||||
|
||||
# 构建分离的对话 prompt
|
||||
core_dialogue_prompt, background_dialogue_prompt = self.build_s4u_chat_history_prompts(
|
||||
message_list_before_now_long, target_user_id
|
||||
)
|
||||
|
||||
self.build_mai_think_context(
|
||||
chat_id=chat_id,
|
||||
memory_block=memory_block,
|
||||
relation_info=relation_info,
|
||||
time_block=time_block,
|
||||
chat_target_1=chat_target_1,
|
||||
chat_target_2=chat_target_2,
|
||||
mood_prompt=mood_prompt,
|
||||
identity_block=identity_block,
|
||||
sender=sender,
|
||||
target=target,
|
||||
chat_info=f"""
|
||||
# 构建分离的对话 prompt
|
||||
core_dialogue_prompt, background_dialogue_prompt = self.build_s4u_chat_history_prompts(
|
||||
message_list_before_now_long, target_user_id
|
||||
)
|
||||
|
||||
self.build_mai_think_context(
|
||||
chat_id=chat_id,
|
||||
memory_block=memory_block,
|
||||
relation_info=relation_info,
|
||||
time_block=time_block,
|
||||
chat_target_1=chat_target_1,
|
||||
chat_target_2=chat_target_2,
|
||||
mood_prompt=mood_prompt,
|
||||
identity_block=identity_block,
|
||||
sender=sender,
|
||||
target=target,
|
||||
chat_info=f"""
|
||||
{background_dialogue_prompt}
|
||||
--------------------------------
|
||||
{time_block}
|
||||
这是你和{sender}的对话,你们正在交流中:
|
||||
{core_dialogue_prompt}"""
|
||||
)
|
||||
|
||||
{core_dialogue_prompt}""",
|
||||
)
|
||||
|
||||
# 使用 s4u 风格的模板
|
||||
template_name = "s4u_style_prompt"
|
||||
# 使用 s4u 风格的模板
|
||||
template_name = "s4u_style_prompt"
|
||||
|
||||
return await global_prompt_manager.format_prompt(
|
||||
template_name,
|
||||
expression_habits_block=expression_habits_block,
|
||||
tool_info_block=tool_info,
|
||||
knowledge_prompt=prompt_info,
|
||||
memory_block=memory_block,
|
||||
relation_info_block=relation_info,
|
||||
extra_info_block=extra_info_block,
|
||||
identity=identity_block,
|
||||
action_descriptions=action_descriptions,
|
||||
sender_name=sender,
|
||||
mood_state=mood_prompt,
|
||||
background_dialogue_prompt=background_dialogue_prompt,
|
||||
time_block=time_block,
|
||||
core_dialogue_prompt=core_dialogue_prompt,
|
||||
reply_target_block=reply_target_block,
|
||||
message_txt=target,
|
||||
config_expression_style=global_config.expression.expression_style,
|
||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
||||
moderation_prompt=moderation_prompt_block,
|
||||
)
|
||||
else:
|
||||
self.build_mai_think_context(
|
||||
chat_id=chat_id,
|
||||
memory_block=memory_block,
|
||||
relation_info=relation_info,
|
||||
time_block=time_block,
|
||||
chat_target_1=chat_target_1,
|
||||
chat_target_2=chat_target_2,
|
||||
mood_prompt=mood_prompt,
|
||||
identity_block=identity_block,
|
||||
sender=sender,
|
||||
target=target,
|
||||
chat_info=chat_talking_prompt
|
||||
)
|
||||
|
||||
# 使用原有的模式
|
||||
return await global_prompt_manager.format_prompt(
|
||||
template_name,
|
||||
expression_habits_block=expression_habits_block,
|
||||
chat_target=chat_target_1,
|
||||
chat_info=chat_talking_prompt,
|
||||
memory_block=memory_block,
|
||||
tool_info_block=tool_info,
|
||||
knowledge_prompt=prompt_info,
|
||||
extra_info_block=extra_info_block,
|
||||
relation_info_block=relation_info,
|
||||
time_block=time_block,
|
||||
reply_target_block=reply_target_block,
|
||||
moderation_prompt=moderation_prompt_block,
|
||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
||||
identity=identity_block,
|
||||
target_message=target,
|
||||
sender_name=sender,
|
||||
config_expression_style=global_config.expression.expression_style,
|
||||
action_descriptions=action_descriptions,
|
||||
chat_target_2=chat_target_2,
|
||||
mood_state=mood_prompt,
|
||||
)
|
||||
return await global_prompt_manager.format_prompt(
|
||||
template_name,
|
||||
expression_habits_block=expression_habits_block,
|
||||
tool_info_block=tool_info,
|
||||
knowledge_prompt=prompt_info,
|
||||
memory_block=memory_block,
|
||||
relation_info_block=relation_info,
|
||||
extra_info_block=extra_info_block,
|
||||
identity=identity_block,
|
||||
action_descriptions=action_descriptions,
|
||||
sender_name=sender,
|
||||
mood_state=mood_prompt,
|
||||
background_dialogue_prompt=background_dialogue_prompt,
|
||||
time_block=time_block,
|
||||
core_dialogue_prompt=core_dialogue_prompt,
|
||||
reply_target_block=reply_target_block,
|
||||
message_txt=target,
|
||||
config_expression_style=global_config.expression.expression_style,
|
||||
keywords_reaction_prompt=keywords_reaction_prompt,
|
||||
moderation_prompt=moderation_prompt_block,
|
||||
)
|
||||
|
||||
async def build_prompt_rewrite_context(
|
||||
self,
|
||||
reply_data: Dict[str, Any],
|
||||
raw_reply: str,
|
||||
reason: str,
|
||||
reply_to: str,
|
||||
) -> str:
|
||||
chat_stream = self.chat_stream
|
||||
chat_id = chat_stream.stream_id
|
||||
is_group_chat = bool(chat_stream.group_info)
|
||||
|
||||
reply_to = reply_data.get("reply_to", "none")
|
||||
raw_reply = reply_data.get("raw_reply", "")
|
||||
reason = reply_data.get("reason", "")
|
||||
sender, target = self._parse_reply_target(reply_to)
|
||||
|
||||
# 添加情绪状态获取
|
||||
@@ -919,7 +908,7 @@ class DefaultReplyer:
|
||||
# 并行执行2个构建任务
|
||||
expression_habits_block, relation_info = await asyncio.gather(
|
||||
self.build_expression_habits(chat_talking_prompt_half, target),
|
||||
self.build_relation_info(reply_data),
|
||||
self.build_relation_info(reply_to),
|
||||
)
|
||||
|
||||
keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target)
|
||||
@@ -1079,9 +1068,9 @@ async def get_prompt_info(message: str, threshold: float):
|
||||
related_info += found_knowledge_from_lpmm
|
||||
logger.debug(f"获取知识库内容耗时: {(end_time - start_time):.3f}秒")
|
||||
logger.debug(f"获取知识库内容,相关信息:{related_info[:100]}...,信息长度: {len(related_info)}")
|
||||
|
||||
|
||||
# 格式化知识信息
|
||||
formatted_prompt_info = await global_prompt_manager.format_prompt("knowledge_prompt", prompt_info=related_info)
|
||||
formatted_prompt_info = f"你有以下这些**知识**:\n{related_info}\n请你**记住上面的知识**,之后可能会用到。\n"
|
||||
return formatted_prompt_info
|
||||
else:
|
||||
logger.debug("从LPMM知识库获取知识失败,可能是从未导入过知识,返回空知识...")
|
||||
|
||||
@@ -2,7 +2,7 @@ import time # 导入 time 模块以获取当前时间
|
||||
import random
|
||||
import re
|
||||
|
||||
from typing import List, Dict, Any, Tuple, Optional, Union, Callable
|
||||
from typing import List, Dict, Any, Tuple, Optional, Callable
|
||||
from rich.traceback import install
|
||||
|
||||
from src.config.config import global_config
|
||||
@@ -10,61 +10,48 @@ from src.common.message_repository import find_messages, count_messages
|
||||
from src.common.database.database_model import ActionRecords
|
||||
from src.common.database.database_model import Images
|
||||
from src.person_info.person_info import PersonInfoManager, get_person_info_manager
|
||||
from src.chat.utils.utils import translate_timestamp_to_human_readable,assign_message_ids
|
||||
from src.chat.utils.utils import translate_timestamp_to_human_readable, assign_message_ids
|
||||
|
||||
install(extra_lines=3)
|
||||
|
||||
|
||||
def replace_user_references_in_content(
|
||||
def replace_user_references_sync(
|
||||
content: str,
|
||||
platform: str,
|
||||
name_resolver: Union[Callable[[str, str], str], Callable[[str, str], Any]] = None,
|
||||
is_async: bool = False,
|
||||
replace_bot_name: bool = True
|
||||
) -> Union[str, Any]:
|
||||
name_resolver: Optional[Callable[[str, str], str]] = None,
|
||||
replace_bot_name: bool = True,
|
||||
) -> str:
|
||||
"""
|
||||
替换内容中的用户引用格式,包括回复<aaa:bbb>和@<aaa:bbb>格式
|
||||
|
||||
|
||||
Args:
|
||||
content: 要处理的内容字符串
|
||||
platform: 平台标识
|
||||
name_resolver: 名称解析函数,接收(platform, user_id)参数,返回用户名称
|
||||
如果为None,则使用默认的person_info_manager
|
||||
is_async: 是否为异步模式
|
||||
如果为None,则使用默认的person_info_manager
|
||||
replace_bot_name: 是否将机器人的user_id替换为"机器人昵称(你)"
|
||||
|
||||
|
||||
Returns:
|
||||
处理后的内容字符串(同步模式)或awaitable对象(异步模式)
|
||||
str: 处理后的内容字符串
|
||||
"""
|
||||
if is_async:
|
||||
return _replace_user_references_async(content, platform, name_resolver, replace_bot_name)
|
||||
else:
|
||||
return _replace_user_references_sync(content, platform, name_resolver, replace_bot_name)
|
||||
|
||||
|
||||
def _replace_user_references_sync(
|
||||
content: str,
|
||||
platform: str,
|
||||
name_resolver: Optional[Callable[[str, str], str]] = None,
|
||||
replace_bot_name: bool = True
|
||||
) -> str:
|
||||
"""同步版本的用户引用替换"""
|
||||
if name_resolver is None:
|
||||
person_info_manager = get_person_info_manager()
|
||||
|
||||
def default_resolver(platform: str, user_id: str) -> str:
|
||||
# 检查是否是机器人自己
|
||||
if replace_bot_name and user_id == global_config.bot.qq_account:
|
||||
return f"{global_config.bot.nickname}(你)"
|
||||
person_id = PersonInfoManager.get_person_id(platform, user_id)
|
||||
return person_info_manager.get_value_sync(person_id, "person_name") or user_id
|
||||
return person_info_manager.get_value_sync(person_id, "person_name") or user_id # type: ignore
|
||||
|
||||
name_resolver = default_resolver
|
||||
|
||||
|
||||
# 处理回复<aaa:bbb>格式
|
||||
reply_pattern = r"回复<([^:<>]+):([^:<>]+)>"
|
||||
match = re.search(reply_pattern, content)
|
||||
if match:
|
||||
aaa = match.group(1)
|
||||
bbb = match.group(2)
|
||||
aaa = match[1]
|
||||
bbb = match[2]
|
||||
try:
|
||||
# 检查是否是机器人自己
|
||||
if replace_bot_name and bbb == global_config.bot.qq_account:
|
||||
@@ -75,7 +62,7 @@ def _replace_user_references_sync(
|
||||
except Exception:
|
||||
# 如果解析失败,使用原始昵称
|
||||
content = re.sub(reply_pattern, f"回复 {aaa}", content, count=1)
|
||||
|
||||
|
||||
# 处理@<aaa:bbb>格式
|
||||
at_pattern = r"@<([^:<>]+):([^:<>]+)>"
|
||||
at_matches = list(re.finditer(at_pattern, content))
|
||||
@@ -83,7 +70,7 @@ def _replace_user_references_sync(
|
||||
new_content = ""
|
||||
last_end = 0
|
||||
for m in at_matches:
|
||||
new_content += content[last_end:m.start()]
|
||||
new_content += content[last_end : m.start()]
|
||||
aaa = m.group(1)
|
||||
bbb = m.group(2)
|
||||
try:
|
||||
@@ -99,27 +86,41 @@ def _replace_user_references_sync(
|
||||
last_end = m.end()
|
||||
new_content += content[last_end:]
|
||||
content = new_content
|
||||
|
||||
|
||||
return content
|
||||
|
||||
|
||||
async def _replace_user_references_async(
|
||||
async def replace_user_references_async(
|
||||
content: str,
|
||||
platform: str,
|
||||
name_resolver: Optional[Callable[[str, str], Any]] = None,
|
||||
replace_bot_name: bool = True
|
||||
replace_bot_name: bool = True,
|
||||
) -> str:
|
||||
"""异步版本的用户引用替换"""
|
||||
"""
|
||||
替换内容中的用户引用格式,包括回复<aaa:bbb>和@<aaa:bbb>格式
|
||||
|
||||
Args:
|
||||
content: 要处理的内容字符串
|
||||
platform: 平台标识
|
||||
name_resolver: 名称解析函数,接收(platform, user_id)参数,返回用户名称
|
||||
如果为None,则使用默认的person_info_manager
|
||||
replace_bot_name: 是否将机器人的user_id替换为"机器人昵称(你)"
|
||||
|
||||
Returns:
|
||||
str: 处理后的内容字符串
|
||||
"""
|
||||
if name_resolver is None:
|
||||
person_info_manager = get_person_info_manager()
|
||||
|
||||
async def default_resolver(platform: str, user_id: str) -> str:
|
||||
# 检查是否是机器人自己
|
||||
if replace_bot_name and user_id == global_config.bot.qq_account:
|
||||
return f"{global_config.bot.nickname}(你)"
|
||||
person_id = PersonInfoManager.get_person_id(platform, user_id)
|
||||
return await person_info_manager.get_value(person_id, "person_name") or user_id
|
||||
return await person_info_manager.get_value(person_id, "person_name") or user_id # type: ignore
|
||||
|
||||
name_resolver = default_resolver
|
||||
|
||||
|
||||
# 处理回复<aaa:bbb>格式
|
||||
reply_pattern = r"回复<([^:<>]+):([^:<>]+)>"
|
||||
match = re.search(reply_pattern, content)
|
||||
@@ -136,7 +137,7 @@ async def _replace_user_references_async(
|
||||
except Exception:
|
||||
# 如果解析失败,使用原始昵称
|
||||
content = re.sub(reply_pattern, f"回复 {aaa}", content, count=1)
|
||||
|
||||
|
||||
# 处理@<aaa:bbb>格式
|
||||
at_pattern = r"@<([^:<>]+):([^:<>]+)>"
|
||||
at_matches = list(re.finditer(at_pattern, content))
|
||||
@@ -144,7 +145,7 @@ async def _replace_user_references_async(
|
||||
new_content = ""
|
||||
last_end = 0
|
||||
for m in at_matches:
|
||||
new_content += content[last_end:m.start()]
|
||||
new_content += content[last_end : m.start()]
|
||||
aaa = m.group(1)
|
||||
bbb = m.group(2)
|
||||
try:
|
||||
@@ -160,7 +161,7 @@ async def _replace_user_references_async(
|
||||
last_end = m.end()
|
||||
new_content += content[last_end:]
|
||||
content = new_content
|
||||
|
||||
|
||||
return content
|
||||
|
||||
|
||||
@@ -524,7 +525,7 @@ def _build_readable_messages_internal(
|
||||
person_name = "某人"
|
||||
|
||||
# 使用独立函数处理用户引用格式
|
||||
content = replace_user_references_in_content(content, platform, is_async=False, replace_bot_name=replace_bot_name)
|
||||
content = replace_user_references_sync(content, platform, replace_bot_name=replace_bot_name)
|
||||
|
||||
target_str = "这是QQ的一个功能,用于提及某人,但没那么明显"
|
||||
if target_str in content and random.random() < 0.6:
|
||||
@@ -778,6 +779,7 @@ async def build_readable_messages_with_list(
|
||||
|
||||
return formatted_string, details_list
|
||||
|
||||
|
||||
def build_readable_messages_with_id(
|
||||
messages: List[Dict[str, Any]],
|
||||
replace_bot_name: bool = True,
|
||||
@@ -793,9 +795,9 @@ def build_readable_messages_with_id(
|
||||
允许通过参数控制格式化行为。
|
||||
"""
|
||||
message_id_list = assign_message_ids(messages)
|
||||
|
||||
|
||||
formatted_string = build_readable_messages(
|
||||
messages = messages,
|
||||
messages=messages,
|
||||
replace_bot_name=replace_bot_name,
|
||||
merge_messages=merge_messages,
|
||||
timestamp_mode=timestamp_mode,
|
||||
@@ -806,10 +808,7 @@ def build_readable_messages_with_id(
|
||||
message_id_list=message_id_list,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
return formatted_string , message_id_list
|
||||
return formatted_string, message_id_list
|
||||
|
||||
|
||||
def build_readable_messages(
|
||||
@@ -894,7 +893,13 @@ def build_readable_messages(
|
||||
if read_mark <= 0:
|
||||
# 没有有效的 read_mark,直接格式化所有消息
|
||||
formatted_string, _, pic_id_mapping, _ = _build_readable_messages_internal(
|
||||
copy_messages, replace_bot_name, merge_messages, timestamp_mode, truncate, show_pic=show_pic, message_id_list=message_id_list
|
||||
copy_messages,
|
||||
replace_bot_name,
|
||||
merge_messages,
|
||||
timestamp_mode,
|
||||
truncate,
|
||||
show_pic=show_pic,
|
||||
message_id_list=message_id_list,
|
||||
)
|
||||
|
||||
# 生成图片映射信息并添加到最前面
|
||||
@@ -1017,7 +1022,7 @@ async def build_anonymous_messages(messages: List[Dict[str, Any]]) -> str:
|
||||
|
||||
for msg in messages:
|
||||
try:
|
||||
platform = msg.get("chat_info_platform")
|
||||
platform: str = msg.get("chat_info_platform") # type: ignore
|
||||
user_id = msg.get("user_id")
|
||||
_timestamp = msg.get("time")
|
||||
content: str = ""
|
||||
@@ -1046,8 +1051,8 @@ async def build_anonymous_messages(messages: List[Dict[str, Any]]) -> str:
|
||||
return get_anon_name(platform, user_id)
|
||||
except Exception:
|
||||
return "?"
|
||||
|
||||
content = replace_user_references_in_content(content, platform, anon_name_resolver, is_async=False, replace_bot_name=False)
|
||||
|
||||
content = replace_user_references_sync(content, platform, anon_name_resolver, replace_bot_name=False)
|
||||
|
||||
header = f"{anon_name}说 "
|
||||
output_lines.append(header)
|
||||
|
||||
@@ -37,7 +37,7 @@ class ImageManager:
|
||||
self._ensure_image_dir()
|
||||
|
||||
self._initialized = True
|
||||
self._llm = LLMRequest(model=global_config.model.vlm, temperature=0.4, max_tokens=300, request_type="image")
|
||||
self.vlm = LLMRequest(model=global_config.model.vlm, temperature=0.4, max_tokens=300, request_type="image")
|
||||
|
||||
try:
|
||||
db.connect(reuse_if_open=True)
|
||||
@@ -94,7 +94,7 @@ class ImageManager:
|
||||
logger.error(f"保存描述到数据库失败 (Peewee): {str(e)}")
|
||||
|
||||
async def get_emoji_description(self, image_base64: str) -> str:
|
||||
"""获取表情包描述,使用二步走识别并带缓存优化"""
|
||||
"""获取表情包描述,优先使用Emoji表中的缓存数据"""
|
||||
try:
|
||||
# 计算图片哈希
|
||||
# 确保base64字符串只包含ASCII字符
|
||||
@@ -104,9 +104,21 @@ class ImageManager:
|
||||
image_hash = hashlib.md5(image_bytes).hexdigest()
|
||||
image_format = Image.open(io.BytesIO(image_bytes)).format.lower() # type: ignore
|
||||
|
||||
# 查询缓存的描述
|
||||
# 优先使用EmojiManager查询已注册表情包的描述
|
||||
try:
|
||||
from src.chat.emoji_system.emoji_manager import get_emoji_manager
|
||||
emoji_manager = get_emoji_manager()
|
||||
cached_emoji_description = await emoji_manager.get_emoji_description_by_hash(image_hash)
|
||||
if cached_emoji_description:
|
||||
logger.info(f"[缓存命中] 使用已注册表情包描述: {cached_emoji_description[:50]}...")
|
||||
return cached_emoji_description
|
||||
except Exception as e:
|
||||
logger.debug(f"查询EmojiManager时出错: {e}")
|
||||
|
||||
# 查询ImageDescriptions表的缓存描述
|
||||
cached_description = self._get_description_from_db(image_hash, "emoji")
|
||||
if cached_description:
|
||||
logger.info(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description[:50]}...")
|
||||
return f"[表情包:{cached_description}]"
|
||||
|
||||
# === 二步走识别流程 ===
|
||||
@@ -118,10 +130,10 @@ class ImageManager:
|
||||
logger.warning("GIF转换失败,无法获取描述")
|
||||
return "[表情包(GIF处理失败)]"
|
||||
vlm_prompt = "这是一个动态图表情包,每一张图代表了动态图的某一帧,黑色背景代表透明,描述一下表情包表达的情感和内容,描述细节,从互联网梗,meme的角度去分析"
|
||||
detailed_description, _ = await self._llm.generate_response_for_image(vlm_prompt, image_base64_processed, "jpg")
|
||||
detailed_description, _ = await self.vlm.generate_response_for_image(vlm_prompt, image_base64_processed, "jpg")
|
||||
else:
|
||||
vlm_prompt = "这是一个表情包,请详细描述一下表情包所表达的情感和内容,描述细节,从互联网梗,meme的角度去分析"
|
||||
detailed_description, _ = await self._llm.generate_response_for_image(vlm_prompt, image_base64, image_format)
|
||||
detailed_description, _ = await self.vlm.generate_response_for_image(vlm_prompt, image_base64, image_format)
|
||||
|
||||
if detailed_description is None:
|
||||
logger.warning("VLM未能生成表情包详细描述")
|
||||
@@ -158,7 +170,7 @@ class ImageManager:
|
||||
if len(emotions) > 1 and emotions[1] != emotions[0]:
|
||||
final_emotion = f"{emotions[0]},{emotions[1]}"
|
||||
|
||||
logger.info(f"[二步走识别] 详细描述: {detailed_description[:50]}... -> 情感标签: {final_emotion}")
|
||||
logger.info(f"[emoji识别] 详细描述: {detailed_description[:50]}... -> 情感标签: {final_emotion}")
|
||||
|
||||
# 再次检查缓存,防止并发写入时重复生成
|
||||
cached_description = self._get_description_from_db(image_hash, "emoji")
|
||||
@@ -201,13 +213,13 @@ class ImageManager:
|
||||
self._save_description_to_db(image_hash, final_emotion, "emoji")
|
||||
|
||||
return f"[表情包:{final_emotion}]"
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"获取表情包描述失败: {str(e)}")
|
||||
return "[表情包]"
|
||||
return "[表情包(处理失败)]"
|
||||
|
||||
async def get_image_description(self, image_base64: str) -> str:
|
||||
"""获取普通图片描述,带查重和保存功能"""
|
||||
"""获取普通图片描述,优先使用Images表中的缓存数据"""
|
||||
try:
|
||||
# 计算图片哈希
|
||||
if isinstance(image_base64, str):
|
||||
@@ -215,7 +227,7 @@ class ImageManager:
|
||||
image_bytes = base64.b64decode(image_base64)
|
||||
image_hash = hashlib.md5(image_bytes).hexdigest()
|
||||
|
||||
# 检查图片是否已存在
|
||||
# 优先检查Images表中是否已有完整的描述
|
||||
existing_image = Images.get_or_none(Images.emoji_hash == image_hash)
|
||||
if existing_image:
|
||||
# 更新计数
|
||||
@@ -227,18 +239,20 @@ class ImageManager:
|
||||
|
||||
# 如果已有描述,直接返回
|
||||
if existing_image.description:
|
||||
logger.debug(f"[缓存命中] 使用Images表中的图片描述: {existing_image.description[:50]}...")
|
||||
return f"[图片:{existing_image.description}]"
|
||||
|
||||
# 查询缓存的描述
|
||||
# 查询ImageDescriptions表的缓存描述
|
||||
cached_description = self._get_description_from_db(image_hash, "image")
|
||||
if cached_description:
|
||||
logger.debug(f"图片描述缓存中 {cached_description}")
|
||||
logger.debug(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description[:50]}...")
|
||||
return f"[图片:{cached_description}]"
|
||||
|
||||
# 调用AI获取描述
|
||||
image_format = Image.open(io.BytesIO(image_bytes)).format.lower() # type: ignore
|
||||
prompt = global_config.custom_prompt.image_prompt
|
||||
description, _ = await self._llm.generate_response_for_image(prompt, image_base64, image_format)
|
||||
logger.info(f"[VLM调用] 为图片生成新描述 (Hash: {image_hash[:8]}...)")
|
||||
description, _ = await self.vlm.generate_response_for_image(prompt, image_base64, image_format)
|
||||
|
||||
if description is None:
|
||||
logger.warning("AI未能生成图片描述")
|
||||
@@ -266,6 +280,7 @@ class ImageManager:
|
||||
if not hasattr(existing_image, "vlm_processed") or existing_image.vlm_processed is None:
|
||||
existing_image.vlm_processed = True
|
||||
existing_image.save()
|
||||
logger.debug(f"[数据库] 更新已有图片记录: {image_hash[:8]}...")
|
||||
else:
|
||||
Images.create(
|
||||
image_id=str(uuid.uuid4()),
|
||||
@@ -277,16 +292,18 @@ class ImageManager:
|
||||
vlm_processed=True,
|
||||
count=1,
|
||||
)
|
||||
logger.debug(f"[数据库] 创建新图片记录: {image_hash[:8]}...")
|
||||
except Exception as e:
|
||||
logger.error(f"保存图片文件或元数据失败: {str(e)}")
|
||||
|
||||
# 保存描述到ImageDescriptions表
|
||||
# 保存描述到ImageDescriptions表作为备用缓存
|
||||
self._save_description_to_db(image_hash, description, "image")
|
||||
|
||||
logger.info(f"[VLM完成] 图片描述生成: {description[:50]}...")
|
||||
return f"[图片:{description}]"
|
||||
except Exception as e:
|
||||
logger.error(f"获取图片描述失败: {str(e)}")
|
||||
return "[图片]"
|
||||
return "[图片(处理失败)]"
|
||||
|
||||
@staticmethod
|
||||
def transform_gif(gif_base64: str, similarity_threshold: float = 1000.0, max_frames: int = 15) -> Optional[str]:
|
||||
@@ -502,12 +519,28 @@ class ImageManager:
|
||||
image_bytes = base64.b64decode(image_base64)
|
||||
image_hash = hashlib.md5(image_bytes).hexdigest()
|
||||
|
||||
# 先检查缓存的描述
|
||||
# 获取当前图片记录
|
||||
image = Images.get(Images.image_id == image_id)
|
||||
|
||||
# 优先检查是否已有其他相同哈希的图片记录包含描述
|
||||
existing_with_description = Images.get_or_none(
|
||||
(Images.emoji_hash == image_hash) &
|
||||
(Images.description.is_null(False)) &
|
||||
(Images.description != "")
|
||||
)
|
||||
if existing_with_description and existing_with_description.id != image.id:
|
||||
logger.debug(f"[缓存复用] 从其他相同图片记录复用描述: {existing_with_description.description[:50]}...")
|
||||
image.description = existing_with_description.description
|
||||
image.vlm_processed = True
|
||||
image.save()
|
||||
# 同时保存到ImageDescriptions表作为备用缓存
|
||||
self._save_description_to_db(image_hash, existing_with_description.description, "image")
|
||||
return
|
||||
|
||||
# 检查ImageDescriptions表的缓存描述
|
||||
cached_description = self._get_description_from_db(image_hash, "image")
|
||||
if cached_description:
|
||||
logger.debug(f"VLM处理时发现缓存描述: {cached_description}")
|
||||
# 更新数据库
|
||||
image = Images.get(Images.image_id == image_id)
|
||||
logger.debug(f"[缓存复用] 从ImageDescriptions表复用描述: {cached_description[:50]}...")
|
||||
image.description = cached_description
|
||||
image.vlm_processed = True
|
||||
image.save()
|
||||
@@ -520,7 +553,8 @@ class ImageManager:
|
||||
prompt = global_config.custom_prompt.image_prompt
|
||||
|
||||
# 获取VLM描述
|
||||
description, _ = await self._llm.generate_response_for_image(prompt, image_base64, image_format)
|
||||
logger.info(f"[VLM异步调用] 为图片生成描述 (ID: {image_id}, Hash: {image_hash[:8]}...)")
|
||||
description, _ = await self.vlm.generate_response_for_image(prompt, image_base64, image_format)
|
||||
|
||||
if description is None:
|
||||
logger.warning("VLM未能生成图片描述")
|
||||
@@ -533,14 +567,15 @@ class ImageManager:
|
||||
description = cached_description
|
||||
|
||||
# 更新数据库
|
||||
image = Images.get(Images.image_id == image_id)
|
||||
image.description = description
|
||||
image.vlm_processed = True
|
||||
image.save()
|
||||
|
||||
# 保存描述到ImageDescriptions表
|
||||
# 保存描述到ImageDescriptions表作为备用缓存
|
||||
self._save_description_to_db(image_hash, description, "image")
|
||||
|
||||
logger.info(f"[VLM异步完成] 图片描述生成: {description[:50]}...")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"VLM处理图片失败: {str(e)}")
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ class ClassicalWillingManager(BaseWillingManager):
|
||||
|
||||
# print(f"[{chat_id}] 回复意愿: {current_willing}")
|
||||
|
||||
interested_rate = willing_info.interested_rate * global_config.normal_chat.response_interested_rate_amplifier
|
||||
interested_rate = willing_info.interested_rate
|
||||
|
||||
# print(f"[{chat_id}] 兴趣值: {interested_rate}")
|
||||
|
||||
@@ -36,20 +36,18 @@ class ClassicalWillingManager(BaseWillingManager):
|
||||
current_willing += interested_rate - 0.2
|
||||
|
||||
if willing_info.is_mentioned_bot and global_config.chat.mentioned_bot_inevitable_reply and current_willing < 2:
|
||||
current_willing += 1 if current_willing < 1.0 else 0.05
|
||||
current_willing += 1 if current_willing < 1.0 else 0.2
|
||||
|
||||
self.chat_reply_willing[chat_id] = min(current_willing, 1.0)
|
||||
|
||||
reply_probability = min(max((current_willing - 0.5), 0.01) * 2, 1)
|
||||
reply_probability = min(max((current_willing - 0.5), 0.01) * 2, 1.5)
|
||||
|
||||
# print(f"[{chat_id}] 回复概率: {reply_probability}")
|
||||
|
||||
return reply_probability
|
||||
|
||||
async def before_generate_reply_handle(self, message_id):
|
||||
chat_id = self.ongoing_messages[message_id].chat_id
|
||||
current_willing = self.chat_reply_willing.get(chat_id, 0)
|
||||
self.chat_reply_willing[chat_id] = max(0.0, current_willing - 1.8)
|
||||
pass
|
||||
|
||||
async def after_generate_reply_handle(self, message_id):
|
||||
if message_id not in self.ongoing_messages:
|
||||
@@ -58,7 +56,7 @@ class ClassicalWillingManager(BaseWillingManager):
|
||||
chat_id = self.ongoing_messages[message_id].chat_id
|
||||
current_willing = self.chat_reply_willing.get(chat_id, 0)
|
||||
if current_willing < 1:
|
||||
self.chat_reply_willing[chat_id] = min(1.0, current_willing + 0.4)
|
||||
self.chat_reply_willing[chat_id] = min(1.0, current_willing + 0.3)
|
||||
|
||||
async def not_reply_handle(self, message_id):
|
||||
return await super().not_reply_handle(message_id)
|
||||
|
||||
@@ -36,7 +36,7 @@ def compare_dicts(new, old, path=None, new_comments=None, old_comments=None, log
|
||||
continue
|
||||
if key not in old:
|
||||
comment = get_key_comment(new, key)
|
||||
logs.append(f"新增: {'.'.join(path + [str(key)])} 注释: {comment if comment else '无'}")
|
||||
logs.append(f"新增: {'.'.join(path + [str(key)])} 注释: {comment or '无'}")
|
||||
elif isinstance(new[key], (dict, Table)) and isinstance(old.get(key), (dict, Table)):
|
||||
compare_dicts(new[key], old[key], path + [str(key)], new_comments, old_comments, logs)
|
||||
# 删减项
|
||||
@@ -45,7 +45,7 @@ def compare_dicts(new, old, path=None, new_comments=None, old_comments=None, log
|
||||
continue
|
||||
if key not in new:
|
||||
comment = get_key_comment(old, key)
|
||||
logs.append(f"删减: {'.'.join(path + [str(key)])} 注释: {comment if comment else '无'}")
|
||||
logs.append(f"删减: {'.'.join(path + [str(key)])} 注释: {comment or '无'}")
|
||||
return logs
|
||||
|
||||
|
||||
|
||||
@@ -68,6 +68,8 @@ class ChatConfig(ConfigBase):
|
||||
|
||||
max_context_size: int = 18
|
||||
"""上下文长度"""
|
||||
|
||||
willing_amplifier: float = 1.0
|
||||
|
||||
replyer_random_probability: float = 0.5
|
||||
"""
|
||||
@@ -75,15 +77,12 @@ class ChatConfig(ConfigBase):
|
||||
选择普通模型的概率为 1 - reasoning_normal_model_probability
|
||||
"""
|
||||
|
||||
thinking_timeout: int = 30
|
||||
thinking_timeout: int = 40
|
||||
"""麦麦最长思考规划时间,超过这个时间的思考会放弃(往往是api反应太慢)"""
|
||||
|
||||
talk_frequency: float = 1
|
||||
"""回复频率阈值"""
|
||||
|
||||
use_s4u_prompt_mode: bool = False
|
||||
"""是否使用 s4u 对话构建模式,该模式会分开处理当前对话对象和其他所有对话的内容进行 prompt 构建"""
|
||||
|
||||
mentioned_bot_inevitable_reply: bool = False
|
||||
"""提及 bot 必然回复"""
|
||||
|
||||
@@ -273,12 +272,6 @@ class NormalChatConfig(ConfigBase):
|
||||
willing_mode: str = "classical"
|
||||
"""意愿模式"""
|
||||
|
||||
response_interested_rate_amplifier: float = 1.0
|
||||
"""回复兴趣度放大系数"""
|
||||
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class ExpressionConfig(ConfigBase):
|
||||
"""表达配置类"""
|
||||
@@ -306,11 +299,8 @@ class ExpressionConfig(ConfigBase):
|
||||
class ToolConfig(ConfigBase):
|
||||
"""工具配置类"""
|
||||
|
||||
enable_in_normal_chat: bool = False
|
||||
"""是否在普通聊天中启用工具"""
|
||||
|
||||
enable_in_focus_chat: bool = True
|
||||
"""是否在专注聊天中启用工具"""
|
||||
enable_tool: bool = False
|
||||
"""是否在聊天中启用工具"""
|
||||
|
||||
@dataclass
|
||||
class VoiceConfig(ConfigBase):
|
||||
|
||||
@@ -273,15 +273,19 @@ class Individuality:
|
||||
prompt=prompt,
|
||||
)
|
||||
|
||||
if response.strip():
|
||||
if response and response.strip():
|
||||
personality_parts.append(response.strip())
|
||||
logger.info(f"精简人格侧面: {response.strip()}")
|
||||
else:
|
||||
logger.error(f"使用LLM压缩人设时出错: {response}")
|
||||
# 压缩失败时使用原始内容
|
||||
if personality_side:
|
||||
personality_parts.append(personality_side)
|
||||
|
||||
if personality_parts:
|
||||
personality_result = "。".join(personality_parts)
|
||||
else:
|
||||
personality_result = personality_core
|
||||
personality_result = personality_core or "友好活泼"
|
||||
else:
|
||||
personality_result = personality_core
|
||||
if personality_side:
|
||||
@@ -308,13 +312,14 @@ class Individuality:
|
||||
prompt=prompt,
|
||||
)
|
||||
|
||||
if response.strip():
|
||||
if response and response.strip():
|
||||
identity_result = response.strip()
|
||||
logger.info(f"精简身份: {identity_result}")
|
||||
else:
|
||||
logger.error(f"使用LLM压缩身份时出错: {response}")
|
||||
identity_result = identity
|
||||
else:
|
||||
identity_result = "。".join(identity)
|
||||
identity_result = identity
|
||||
|
||||
return identity_result
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ class RelationshipManager:
|
||||
请用json格式输出,引起了你的兴趣,或者有什么需要你记忆的点。
|
||||
并为每个点赋予1-10的权重,权重越高,表示越重要。
|
||||
格式如下:
|
||||
{{
|
||||
[
|
||||
{{
|
||||
"point": "{person_name}想让我记住他的生日,我回答确认了,他的生日是11月23日",
|
||||
"weight": 10
|
||||
@@ -156,13 +156,10 @@ class RelationshipManager:
|
||||
"point": "{person_name}喜欢吃辣,具体来说,没有辣的食物ta都不喜欢吃,可能是因为ta是湖南人。",
|
||||
"weight": 7
|
||||
}}
|
||||
}}
|
||||
]
|
||||
|
||||
如果没有,就输出none,或points为空:
|
||||
{{
|
||||
"point": "none",
|
||||
"weight": 0
|
||||
}}
|
||||
如果没有,就输出none,或返回空数组:
|
||||
[]
|
||||
"""
|
||||
|
||||
# 调用LLM生成印象
|
||||
@@ -184,17 +181,25 @@ class RelationshipManager:
|
||||
try:
|
||||
points = repair_json(points)
|
||||
points_data = json.loads(points)
|
||||
if points_data == "none" or not points_data or points_data.get("point") == "none":
|
||||
|
||||
# 只处理正确的格式,错误格式直接跳过
|
||||
if points_data == "none" or not points_data:
|
||||
points_list = []
|
||||
elif isinstance(points_data, str) and points_data.lower() == "none":
|
||||
points_list = []
|
||||
elif isinstance(points_data, list):
|
||||
# 正确格式:数组格式 [{"point": "...", "weight": 10}, ...]
|
||||
if not points_data: # 空数组
|
||||
points_list = []
|
||||
else:
|
||||
points_list = [(item["point"], float(item["weight"]), current_time) for item in points_data]
|
||||
else:
|
||||
# logger.info(f"points_data: {points_data}")
|
||||
if isinstance(points_data, dict) and "points" in points_data:
|
||||
points_data = points_data["points"]
|
||||
if not isinstance(points_data, list):
|
||||
points_data = [points_data]
|
||||
# 添加可读时间到每个point
|
||||
points_list = [(item["point"], float(item["weight"]), current_time) for item in points_data]
|
||||
# 错误格式,直接跳过不解析
|
||||
logger.warning(f"LLM返回了错误的JSON格式,跳过解析: {type(points_data)}, 内容: {points_data}")
|
||||
points_list = []
|
||||
|
||||
# 权重过滤逻辑
|
||||
if points_list:
|
||||
original_points_list = list(points_list)
|
||||
points_list.clear()
|
||||
discarded_count = 0
|
||||
|
||||
@@ -32,6 +32,7 @@ class ChatManager:
|
||||
|
||||
@staticmethod
|
||||
def get_all_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
# sourcery skip: for-append-to-extend
|
||||
"""获取所有聊天流
|
||||
|
||||
Args:
|
||||
@@ -57,6 +58,7 @@ class ChatManager:
|
||||
|
||||
@staticmethod
|
||||
def get_group_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
# sourcery skip: for-append-to-extend
|
||||
"""获取所有群聊聊天流
|
||||
|
||||
Args:
|
||||
@@ -79,6 +81,7 @@ class ChatManager:
|
||||
|
||||
@staticmethod
|
||||
def get_private_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
# sourcery skip: for-append-to-extend
|
||||
"""获取所有私聊聊天流
|
||||
|
||||
Args:
|
||||
@@ -105,7 +108,7 @@ class ChatManager:
|
||||
@staticmethod
|
||||
def get_group_stream_by_group_id(
|
||||
group_id: str, platform: Optional[str] | SpecialTypes = "qq"
|
||||
) -> Optional[ChatStream]:
|
||||
) -> Optional[ChatStream]: # sourcery skip: remove-unnecessary-cast
|
||||
"""根据群ID获取聊天流
|
||||
|
||||
Args:
|
||||
@@ -142,7 +145,7 @@ class ChatManager:
|
||||
@staticmethod
|
||||
def get_private_stream_by_user_id(
|
||||
user_id: str, platform: Optional[str] | SpecialTypes = "qq"
|
||||
) -> Optional[ChatStream]:
|
||||
) -> Optional[ChatStream]: # sourcery skip: remove-unnecessary-cast
|
||||
"""根据用户ID获取私聊流
|
||||
|
||||
Args:
|
||||
@@ -207,7 +210,7 @@ class ChatManager:
|
||||
chat_stream: 聊天流对象
|
||||
|
||||
Returns:
|
||||
Dict[str, Any]: 聊天流信息字典
|
||||
Dict ({str: Any}): 聊天流信息字典
|
||||
|
||||
Raises:
|
||||
TypeError: 如果 chat_stream 不是 ChatStream 类型
|
||||
@@ -282,41 +285,41 @@ class ChatManager:
|
||||
# =============================================================================
|
||||
|
||||
|
||||
def get_all_streams(platform: Optional[str] | SpecialTypes = "qq"):
|
||||
def get_all_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
"""获取所有聊天流的便捷函数"""
|
||||
return ChatManager.get_all_streams(platform)
|
||||
|
||||
|
||||
def get_group_streams(platform: Optional[str] | SpecialTypes = "qq"):
|
||||
def get_group_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
"""获取群聊聊天流的便捷函数"""
|
||||
return ChatManager.get_group_streams(platform)
|
||||
|
||||
|
||||
def get_private_streams(platform: Optional[str] | SpecialTypes = "qq"):
|
||||
def get_private_streams(platform: Optional[str] | SpecialTypes = "qq") -> List[ChatStream]:
|
||||
"""获取私聊聊天流的便捷函数"""
|
||||
return ChatManager.get_private_streams(platform)
|
||||
|
||||
|
||||
def get_stream_by_group_id(group_id: str, platform: Optional[str] | SpecialTypes = "qq"):
|
||||
def get_stream_by_group_id(group_id: str, platform: Optional[str] | SpecialTypes = "qq") -> Optional[ChatStream]:
|
||||
"""根据群ID获取聊天流的便捷函数"""
|
||||
return ChatManager.get_group_stream_by_group_id(group_id, platform)
|
||||
|
||||
|
||||
def get_stream_by_user_id(user_id: str, platform: Optional[str] | SpecialTypes = "qq"):
|
||||
def get_stream_by_user_id(user_id: str, platform: Optional[str] | SpecialTypes = "qq") -> Optional[ChatStream]:
|
||||
"""根据用户ID获取私聊流的便捷函数"""
|
||||
return ChatManager.get_private_stream_by_user_id(user_id, platform)
|
||||
|
||||
|
||||
def get_stream_type(chat_stream: ChatStream):
|
||||
def get_stream_type(chat_stream: ChatStream) -> str:
|
||||
"""获取聊天流类型的便捷函数"""
|
||||
return ChatManager.get_stream_type(chat_stream)
|
||||
|
||||
|
||||
def get_stream_info(chat_stream: ChatStream):
|
||||
def get_stream_info(chat_stream: ChatStream) -> Dict[str, Any]:
|
||||
"""获取聊天流信息的便捷函数"""
|
||||
return ChatManager.get_stream_info(chat_stream)
|
||||
|
||||
|
||||
def get_streams_summary():
|
||||
def get_streams_summary() -> Dict[str, int]:
|
||||
"""获取聊天流统计摘要的便捷函数"""
|
||||
return ChatManager.get_streams_summary()
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
from typing import Any
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
from src.person_info.person_info import get_person_info_manager
|
||||
|
||||
logger = get_logger("config_api")
|
||||
|
||||
@@ -26,7 +25,7 @@ def get_global_config(key: str, default: Any = None) -> Any:
|
||||
插件应使用此方法读取全局配置,以保证只读和隔离性。
|
||||
|
||||
Args:
|
||||
key: 命名空间式配置键名,支持嵌套访问,如 "section.subsection.key",大小写敏感
|
||||
key: 命名空间式配置键名,使用嵌套访问,如 "section.subsection.key",大小写敏感
|
||||
default: 如果配置不存在时返回的默认值
|
||||
|
||||
Returns:
|
||||
@@ -76,50 +75,3 @@ def get_plugin_config(plugin_config: dict, key: str, default: Any = None) -> Any
|
||||
except Exception as e:
|
||||
logger.warning(f"[ConfigAPI] 获取插件配置 {key} 失败: {e}")
|
||||
return default
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 用户信息API函数
|
||||
# =============================================================================
|
||||
|
||||
|
||||
async def get_user_id_by_person_name(person_name: str) -> tuple[str, str]:
|
||||
"""根据内部用户名获取用户ID
|
||||
|
||||
Args:
|
||||
person_name: 用户名
|
||||
|
||||
Returns:
|
||||
tuple[str, str]: (平台, 用户ID)
|
||||
"""
|
||||
try:
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_id = person_info_manager.get_person_id_by_person_name(person_name)
|
||||
user_id: str = await person_info_manager.get_value(person_id, "user_id") # type: ignore
|
||||
platform: str = await person_info_manager.get_value(person_id, "platform") # type: ignore
|
||||
return platform, user_id
|
||||
except Exception as e:
|
||||
logger.error(f"[ConfigAPI] 根据用户名获取用户ID失败: {e}")
|
||||
return "", ""
|
||||
|
||||
|
||||
async def get_person_info(person_id: str, key: str, default: Any = None) -> Any:
|
||||
"""获取用户信息
|
||||
|
||||
Args:
|
||||
person_id: 用户ID
|
||||
key: 信息键名
|
||||
default: 默认值
|
||||
|
||||
Returns:
|
||||
Any: 用户信息值或默认值
|
||||
"""
|
||||
try:
|
||||
person_info_manager = get_person_info_manager()
|
||||
response = await person_info_manager.get_value(person_id, key)
|
||||
if not response:
|
||||
raise ValueError(f"[ConfigAPI] 获取用户 {person_id} 的信息 '{key}' 失败,返回默认值")
|
||||
return response
|
||||
except Exception as e:
|
||||
logger.error(f"[ConfigAPI] 获取用户信息失败: {e}")
|
||||
return default
|
||||
|
||||
@@ -107,10 +107,14 @@ async def generate_reply(
|
||||
return False, [], None
|
||||
|
||||
logger.debug("[GeneratorAPI] 开始生成回复")
|
||||
|
||||
if not reply_to and action_data:
|
||||
reply_to = action_data.get("reply_to", "")
|
||||
if not extra_info and action_data:
|
||||
extra_info = action_data.get("extra_info", "")
|
||||
|
||||
# 调用回复器生成回复
|
||||
success, content, prompt = await replyer.generate_reply_with_context(
|
||||
reply_data=action_data or {},
|
||||
reply_to=reply_to,
|
||||
extra_info=extra_info,
|
||||
available_actions=available_actions,
|
||||
@@ -136,6 +140,7 @@ async def generate_reply(
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"[GeneratorAPI] 生成回复时出错: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
return False, [], None
|
||||
|
||||
|
||||
@@ -146,15 +151,22 @@ async def rewrite_reply(
|
||||
enable_splitter: bool = True,
|
||||
enable_chinese_typo: bool = True,
|
||||
model_configs: Optional[List[Dict[str, Any]]] = None,
|
||||
raw_reply: str = "",
|
||||
reason: str = "",
|
||||
reply_to: str = "",
|
||||
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
||||
"""重写回复
|
||||
|
||||
Args:
|
||||
chat_stream: 聊天流对象(优先)
|
||||
reply_data: 回复数据
|
||||
reply_data: 回复数据字典(备用,当其他参数缺失时从此获取)
|
||||
chat_id: 聊天ID(备用)
|
||||
enable_splitter: 是否启用消息分割器
|
||||
enable_chinese_typo: 是否启用错字生成器
|
||||
model_configs: 模型配置列表
|
||||
raw_reply: 原始回复内容
|
||||
reason: 回复原因
|
||||
reply_to: 回复对象
|
||||
|
||||
Returns:
|
||||
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
||||
@@ -168,8 +180,18 @@ async def rewrite_reply(
|
||||
|
||||
logger.info("[GeneratorAPI] 开始重写回复")
|
||||
|
||||
# 如果参数缺失,从reply_data中获取
|
||||
if reply_data:
|
||||
raw_reply = raw_reply or reply_data.get("raw_reply", "")
|
||||
reason = reason or reply_data.get("reason", "")
|
||||
reply_to = reply_to or reply_data.get("reply_to", "")
|
||||
|
||||
# 调用回复器重写回复
|
||||
success, content = await replyer.rewrite_reply_with_context(reply_data=reply_data or {})
|
||||
success, content = await replyer.rewrite_reply_with_context(
|
||||
raw_reply=raw_reply,
|
||||
reason=reason,
|
||||
reply_to=reply_to,
|
||||
)
|
||||
reply_set = []
|
||||
if content:
|
||||
reply_set = await process_human_text(content, enable_splitter, enable_chinese_typo)
|
||||
|
||||
@@ -19,11 +19,9 @@
|
||||
await send_api.custom_message("video", video_data, "123456", True)
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import traceback
|
||||
import time
|
||||
import difflib
|
||||
import re
|
||||
from typing import Optional, Union
|
||||
from src.common.logger import get_logger
|
||||
|
||||
@@ -31,7 +29,7 @@ from src.common.logger import get_logger
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.chat.message_receive.uni_message_sender import HeartFCSender
|
||||
from src.chat.message_receive.message import MessageSending, MessageRecv
|
||||
from src.chat.utils.chat_message_builder import get_raw_msg_before_timestamp_with_chat, replace_user_references_in_content
|
||||
from src.chat.utils.chat_message_builder import get_raw_msg_before_timestamp_with_chat, replace_user_references_async
|
||||
from src.person_info.person_info import get_person_info_manager
|
||||
from maim_message import Seg, UserInfo
|
||||
from src.config.config import global_config
|
||||
@@ -185,7 +183,7 @@ async def _find_reply_message(target_stream, reply_to: str) -> Optional[MessageR
|
||||
translate_text = message["processed_plain_text"]
|
||||
|
||||
# 使用独立函数处理用户引用格式
|
||||
translate_text = await replace_user_references_in_content(translate_text, platform, is_async=True)
|
||||
translate_text = await replace_user_references_async(translate_text, platform)
|
||||
|
||||
similarity = difflib.SequenceMatcher(None, text, translate_text).ratio()
|
||||
if similarity >= 0.9:
|
||||
|
||||
@@ -384,7 +384,7 @@ class BaseAction(ABC):
|
||||
keyword_case_sensitive=getattr(cls, "keyword_case_sensitive", False),
|
||||
mode_enable=getattr(cls, "mode_enable", ChatMode.ALL),
|
||||
parallel_action=getattr(cls, "parallel_action", True),
|
||||
random_activation_probability=getattr(cls, "random_activation_probability", 0.3),
|
||||
random_activation_probability=getattr(cls, "random_activation_probability", 0.0),
|
||||
llm_judge_prompt=getattr(cls, "llm_judge_prompt", ""),
|
||||
# 使用正确的字段名
|
||||
action_parameters=getattr(cls, "action_parameters", {}).copy(),
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
|
||||
from src.plugin_system.core.plugin_manager import plugin_manager
|
||||
from src.plugin_system.core.component_registry import component_registry
|
||||
from src.plugin_system.core.dependency_manager import dependency_manager
|
||||
from src.plugin_system.core.events_manager import events_manager
|
||||
from src.plugin_system.core.global_announcement_manager import global_announcement_manager
|
||||
|
||||
__all__ = [
|
||||
"plugin_manager",
|
||||
"component_registry",
|
||||
"dependency_manager",
|
||||
"events_manager",
|
||||
"global_announcement_manager",
|
||||
]
|
||||
|
||||
@@ -1,190 +0,0 @@
|
||||
"""
|
||||
插件依赖管理器
|
||||
|
||||
负责检查和安装插件的Python包依赖
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import importlib
|
||||
from typing import List, Dict, Tuple, Any
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.base.component_types import PythonDependency
|
||||
|
||||
logger = get_logger("dependency_manager")
|
||||
|
||||
|
||||
class DependencyManager:
|
||||
"""依赖管理器"""
|
||||
|
||||
def __init__(self):
|
||||
self.install_log: List[str] = []
|
||||
self.failed_installs: Dict[str, str] = {}
|
||||
|
||||
def check_dependencies(
|
||||
self, dependencies: List[PythonDependency]
|
||||
) -> Tuple[List[PythonDependency], List[PythonDependency]]:
|
||||
"""检查依赖包状态
|
||||
|
||||
Args:
|
||||
dependencies: 依赖包列表
|
||||
|
||||
Returns:
|
||||
Tuple[List[PythonDependency], List[PythonDependency]]: (缺失的依赖, 可选缺失的依赖)
|
||||
"""
|
||||
missing_required = []
|
||||
missing_optional = []
|
||||
|
||||
for dep in dependencies:
|
||||
if self._is_package_available(dep.package_name):
|
||||
logger.debug(f"依赖包已存在: {dep.package_name}")
|
||||
elif dep.optional:
|
||||
missing_optional.append(dep)
|
||||
logger.warning(f"可选依赖包缺失: {dep.package_name} - {dep.description}")
|
||||
else:
|
||||
missing_required.append(dep)
|
||||
logger.error(f"必需依赖包缺失: {dep.package_name} - {dep.description}")
|
||||
return missing_required, missing_optional
|
||||
|
||||
def _is_package_available(self, package_name: str) -> bool:
|
||||
"""检查包是否可用"""
|
||||
try:
|
||||
importlib.import_module(package_name)
|
||||
return True
|
||||
except ImportError:
|
||||
return False
|
||||
|
||||
def install_dependencies(self, dependencies: List[PythonDependency], auto_install: bool = False) -> bool:
|
||||
"""安装依赖包
|
||||
|
||||
Args:
|
||||
dependencies: 需要安装的依赖包列表
|
||||
auto_install: 是否自动安装(True时不询问用户)
|
||||
|
||||
Returns:
|
||||
bool: 安装是否成功
|
||||
"""
|
||||
if not dependencies:
|
||||
return True
|
||||
|
||||
logger.info(f"需要安装 {len(dependencies)} 个依赖包")
|
||||
|
||||
# 显示将要安装的包
|
||||
for dep in dependencies:
|
||||
install_cmd = dep.get_pip_requirement()
|
||||
logger.info(f" - {install_cmd} {'(可选)' if dep.optional else '(必需)'}")
|
||||
if dep.description:
|
||||
logger.info(f" 说明: {dep.description}")
|
||||
|
||||
if not auto_install:
|
||||
# 这里可以添加用户确认逻辑
|
||||
logger.warning("手动安装模式:请手动运行 pip install 命令安装依赖包")
|
||||
return False
|
||||
|
||||
# 执行安装
|
||||
success_count = 0
|
||||
for dep in dependencies:
|
||||
if self._install_single_package(dep):
|
||||
success_count += 1
|
||||
else:
|
||||
self.failed_installs[dep.package_name] = f"安装失败: {dep.get_pip_requirement()}"
|
||||
|
||||
logger.info(f"依赖安装完成: {success_count}/{len(dependencies)} 个成功")
|
||||
return success_count == len(dependencies)
|
||||
|
||||
def _install_single_package(self, dependency: PythonDependency) -> bool:
|
||||
"""安装单个包"""
|
||||
pip_requirement = dependency.get_pip_requirement()
|
||||
|
||||
try:
|
||||
logger.info(f"正在安装: {pip_requirement}")
|
||||
|
||||
# 使用subprocess安装包
|
||||
cmd = [sys.executable, "-m", "pip", "install", pip_requirement]
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=300, # 5分钟超时
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
logger.info(f"✅ 成功安装: {pip_requirement}")
|
||||
self.install_log.append(f"成功安装: {pip_requirement}")
|
||||
return True
|
||||
else:
|
||||
logger.error(f"❌ 安装失败: {pip_requirement}")
|
||||
logger.error(f"错误输出: {result.stderr}")
|
||||
self.install_log.append(f"安装失败: {pip_requirement} - {result.stderr}")
|
||||
return False
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
logger.error(f"❌ 安装超时: {pip_requirement}")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f"❌ 安装异常: {pip_requirement} - {str(e)}")
|
||||
return False
|
||||
|
||||
def generate_requirements_file(
|
||||
self, plugins_dependencies: List[List[PythonDependency]], output_path: str = "plugin_requirements.txt"
|
||||
) -> bool:
|
||||
"""生成插件依赖的requirements文件
|
||||
|
||||
Args:
|
||||
plugins_dependencies: 所有插件的依赖列表
|
||||
output_path: 输出文件路径
|
||||
|
||||
Returns:
|
||||
bool: 生成是否成功
|
||||
"""
|
||||
try:
|
||||
all_deps = {}
|
||||
|
||||
# 合并所有插件的依赖
|
||||
for plugin_deps in plugins_dependencies:
|
||||
for dep in plugin_deps:
|
||||
key = dep.install_name
|
||||
if key in all_deps:
|
||||
# 如果已存在,可以添加版本兼容性检查逻辑
|
||||
existing = all_deps[key]
|
||||
if dep.version and existing.version != dep.version:
|
||||
logger.warning(f"依赖版本冲突: {key} ({existing.version} vs {dep.version})")
|
||||
else:
|
||||
all_deps[key] = dep
|
||||
|
||||
# 写入requirements文件
|
||||
with open(output_path, "w", encoding="utf-8") as f:
|
||||
f.write("# 插件依赖包自动生成\n")
|
||||
f.write("# Auto-generated plugin dependencies\n\n")
|
||||
|
||||
# 按包名排序
|
||||
sorted_deps = sorted(all_deps.values(), key=lambda x: x.install_name)
|
||||
|
||||
for dep in sorted_deps:
|
||||
requirement = dep.get_pip_requirement()
|
||||
if dep.description:
|
||||
f.write(f"# {dep.description}\n")
|
||||
if dep.optional:
|
||||
f.write("# Optional dependency\n")
|
||||
f.write(f"{requirement}\n\n")
|
||||
|
||||
logger.info(f"已生成插件依赖文件: {output_path} ({len(all_deps)} 个包)")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"生成requirements文件失败: {str(e)}")
|
||||
return False
|
||||
|
||||
def get_install_summary(self) -> Dict[str, Any]:
|
||||
"""获取安装摘要"""
|
||||
return {
|
||||
"install_log": self.install_log.copy(),
|
||||
"failed_installs": self.failed_installs.copy(),
|
||||
"total_attempts": len(self.install_log),
|
||||
"failed_count": len(self.failed_installs),
|
||||
}
|
||||
|
||||
|
||||
# 全局依赖管理器实例
|
||||
dependency_manager = DependencyManager()
|
||||
@@ -8,10 +8,9 @@ from pathlib import Path
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.plugin_system.base.plugin_base import PluginBase
|
||||
from src.plugin_system.base.component_types import ComponentType, PythonDependency
|
||||
from src.plugin_system.base.component_types import ComponentType
|
||||
from src.plugin_system.utils.manifest_utils import VersionComparator
|
||||
from .component_registry import component_registry
|
||||
from .dependency_manager import dependency_manager
|
||||
|
||||
logger = get_logger("plugin_manager")
|
||||
|
||||
@@ -207,104 +206,6 @@ class PluginManager:
|
||||
"""
|
||||
return self.loaded_plugins.get(plugin_name)
|
||||
|
||||
def check_all_dependencies(self, auto_install: bool = False) -> Dict[str, Any]:
|
||||
"""检查所有插件的Python依赖包
|
||||
|
||||
Args:
|
||||
auto_install: 是否自动安装缺失的依赖包
|
||||
|
||||
Returns:
|
||||
Dict[str, any]: 检查结果摘要
|
||||
"""
|
||||
logger.info("开始检查所有插件的Python依赖包...")
|
||||
|
||||
all_required_missing: List[PythonDependency] = []
|
||||
all_optional_missing: List[PythonDependency] = []
|
||||
plugin_status = {}
|
||||
|
||||
for plugin_name in self.loaded_plugins:
|
||||
plugin_info = component_registry.get_plugin_info(plugin_name)
|
||||
if not plugin_info or not plugin_info.python_dependencies:
|
||||
plugin_status[plugin_name] = {"status": "no_dependencies", "missing": []}
|
||||
continue
|
||||
|
||||
logger.info(f"检查插件 {plugin_name} 的依赖...")
|
||||
|
||||
missing_required, missing_optional = dependency_manager.check_dependencies(plugin_info.python_dependencies)
|
||||
|
||||
if missing_required:
|
||||
all_required_missing.extend(missing_required)
|
||||
plugin_status[plugin_name] = {
|
||||
"status": "missing_required",
|
||||
"missing": [dep.package_name for dep in missing_required],
|
||||
"optional_missing": [dep.package_name for dep in missing_optional],
|
||||
}
|
||||
logger.error(f"插件 {plugin_name} 缺少必需依赖: {[dep.package_name for dep in missing_required]}")
|
||||
elif missing_optional:
|
||||
all_optional_missing.extend(missing_optional)
|
||||
plugin_status[plugin_name] = {
|
||||
"status": "missing_optional",
|
||||
"missing": [],
|
||||
"optional_missing": [dep.package_name for dep in missing_optional],
|
||||
}
|
||||
logger.warning(f"插件 {plugin_name} 缺少可选依赖: {[dep.package_name for dep in missing_optional]}")
|
||||
else:
|
||||
plugin_status[plugin_name] = {"status": "ok", "missing": []}
|
||||
logger.info(f"插件 {plugin_name} 依赖检查通过")
|
||||
|
||||
# 汇总结果
|
||||
total_missing = len({dep.package_name for dep in all_required_missing})
|
||||
total_optional_missing = len({dep.package_name for dep in all_optional_missing})
|
||||
|
||||
logger.info(f"依赖检查完成 - 缺少必需包: {total_missing}个, 缺少可选包: {total_optional_missing}个")
|
||||
|
||||
# 如果需要自动安装
|
||||
install_success = True
|
||||
if auto_install and all_required_missing:
|
||||
unique_required = {dep.package_name: dep for dep in all_required_missing}
|
||||
logger.info(f"开始自动安装 {len(unique_required)} 个必需依赖包...")
|
||||
install_success = dependency_manager.install_dependencies(list(unique_required.values()), auto_install=True)
|
||||
|
||||
return {
|
||||
"total_plugins_checked": len(plugin_status),
|
||||
"plugins_with_missing_required": len(
|
||||
[p for p in plugin_status.values() if p["status"] == "missing_required"]
|
||||
),
|
||||
"plugins_with_missing_optional": len(
|
||||
[p for p in plugin_status.values() if p["status"] == "missing_optional"]
|
||||
),
|
||||
"total_missing_required": total_missing,
|
||||
"total_missing_optional": total_optional_missing,
|
||||
"plugin_status": plugin_status,
|
||||
"auto_install_attempted": auto_install and bool(all_required_missing),
|
||||
"auto_install_success": install_success,
|
||||
"install_summary": dependency_manager.get_install_summary(),
|
||||
}
|
||||
|
||||
def generate_plugin_requirements(self, output_path: str = "plugin_requirements.txt") -> bool:
|
||||
"""生成所有插件依赖的requirements文件
|
||||
|
||||
Args:
|
||||
output_path: 输出文件路径
|
||||
|
||||
Returns:
|
||||
bool: 生成是否成功
|
||||
"""
|
||||
logger.info("开始生成插件依赖requirements文件...")
|
||||
|
||||
all_dependencies = []
|
||||
|
||||
for plugin_name in self.loaded_plugins:
|
||||
plugin_info = component_registry.get_plugin_info(plugin_name)
|
||||
if plugin_info and plugin_info.python_dependencies:
|
||||
all_dependencies.append(plugin_info.python_dependencies)
|
||||
|
||||
if not all_dependencies:
|
||||
logger.info("没有找到任何插件依赖")
|
||||
return False
|
||||
|
||||
return dependency_manager.generate_requirements_file(all_dependencies, output_path)
|
||||
|
||||
# === 查询方法 ===
|
||||
def list_loaded_plugins(self) -> List[str]:
|
||||
"""
|
||||
|
||||
@@ -24,11 +24,6 @@
|
||||
"is_built_in": true,
|
||||
"plugin_type": "action_provider",
|
||||
"components": [
|
||||
{
|
||||
"type": "action",
|
||||
"name": "reply",
|
||||
"description": "参与聊天回复,发送文本进行表达"
|
||||
},
|
||||
{
|
||||
"type": "action",
|
||||
"name": "no_reply",
|
||||
|
||||
@@ -9,7 +9,8 @@ from src.common.logger import get_logger
|
||||
|
||||
# 导入API模块 - 标准Python包方式
|
||||
from src.plugin_system.apis import emoji_api, llm_api, message_api
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
# 注释:不再需要导入NoReplyAction,因为计数器管理已移至heartFC_chat.py
|
||||
# from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
from src.config.config import global_config
|
||||
|
||||
|
||||
@@ -20,10 +21,14 @@ class EmojiAction(BaseAction):
|
||||
"""表情动作 - 发送表情包"""
|
||||
|
||||
# 激活设置
|
||||
activation_type = ActionActivationType.RANDOM
|
||||
if global_config.emoji.emoji_activate_type == "llm":
|
||||
activation_type = ActionActivationType.LLM_JUDGE
|
||||
random_activation_probability = 0
|
||||
else:
|
||||
activation_type = ActionActivationType.RANDOM
|
||||
random_activation_probability = global_config.emoji.emoji_chance
|
||||
mode_enable = ChatMode.ALL
|
||||
parallel_action = True
|
||||
random_activation_probability = 0.2 # 默认值,可通过配置覆盖
|
||||
|
||||
# 动作基本信息
|
||||
action_name = "emoji"
|
||||
@@ -143,8 +148,8 @@ class EmojiAction(BaseAction):
|
||||
logger.error(f"{self.log_prefix} 表情包发送失败")
|
||||
return False, "表情包发送失败"
|
||||
|
||||
# 重置NoReplyAction的连续计数器
|
||||
NoReplyAction.reset_consecutive_count()
|
||||
# 注释:重置NoReplyAction的连续计数器现在由heartFC_chat.py统一管理
|
||||
# NoReplyAction.reset_consecutive_count()
|
||||
|
||||
return True, f"发送表情包: {emoji_description}"
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import random
|
||||
import time
|
||||
from typing import Tuple
|
||||
from typing import Tuple, List
|
||||
from collections import deque
|
||||
|
||||
# 导入新插件系统
|
||||
from src.plugin_system import BaseAction, ActionActivationType, ChatMode
|
||||
@@ -17,11 +18,15 @@ logger = get_logger("no_reply_action")
|
||||
|
||||
|
||||
class NoReplyAction(BaseAction):
|
||||
"""不回复动作,根据新消息的兴趣值或数量决定何时结束等待.
|
||||
"""不回复动作,支持waiting和breaking两种形式.
|
||||
|
||||
新的等待逻辑:
|
||||
1. 新消息累计兴趣值超过阈值 (默认10) 则结束等待
|
||||
2. 累计新消息数量达到随机阈值 (默认5-10条) 则结束等待
|
||||
waiting形式:
|
||||
- 只要有新消息就结束动作
|
||||
- 记录新消息的兴趣度到列表(最多保留最近三项)
|
||||
- 如果最近三次动作都是no_reply,且最近新消息列表兴趣度之和小于阈值,就进入breaking形式
|
||||
|
||||
breaking形式:
|
||||
- 和原有逻辑一致,需要消息满足一定数量或累计一定兴趣值才结束动作
|
||||
"""
|
||||
|
||||
focus_activation_type = ActionActivationType.NEVER
|
||||
@@ -35,112 +40,45 @@ class NoReplyAction(BaseAction):
|
||||
|
||||
# 连续no_reply计数器
|
||||
_consecutive_count = 0
|
||||
|
||||
# 最近三次no_reply的新消息兴趣度记录
|
||||
_recent_interest_records: deque = deque(maxlen=3)
|
||||
|
||||
# 新增:兴趣值退出阈值
|
||||
# 兴趣值退出阈值
|
||||
_interest_exit_threshold = 3.0
|
||||
# 新增:消息数量退出阈值
|
||||
_min_exit_message_count = 5
|
||||
_max_exit_message_count = 10
|
||||
# 消息数量退出阈值
|
||||
_min_exit_message_count = 3
|
||||
_max_exit_message_count = 6
|
||||
|
||||
# 动作参数定义
|
||||
action_parameters = {}
|
||||
|
||||
# 动作使用场景
|
||||
action_require = ["你发送了消息,目前无人回复"]
|
||||
action_require = [""]
|
||||
|
||||
# 关联类型
|
||||
associated_types = []
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
"""执行不回复动作"""
|
||||
import asyncio
|
||||
|
||||
try:
|
||||
# 增加连续计数
|
||||
NoReplyAction._consecutive_count += 1
|
||||
count = NoReplyAction._consecutive_count
|
||||
|
||||
reason = self.action_data.get("reason", "")
|
||||
start_time = self.action_data.get("loop_start_time", time.time())
|
||||
check_interval = 0.6 # 每秒检查一次
|
||||
check_interval = 0.6
|
||||
|
||||
# 随机生成本次等待需要的新消息数量阈值
|
||||
exit_message_count_threshold = random.randint(self._min_exit_message_count, self._max_exit_message_count)
|
||||
logger.info(
|
||||
f"{self.log_prefix} 本次no_reply需要 {exit_message_count_threshold} 条新消息或累计兴趣值超过 {self._interest_exit_threshold} 才能打断"
|
||||
)
|
||||
# 判断使用哪种形式
|
||||
form_type = self._determine_form_type()
|
||||
|
||||
logger.info(f"{self.log_prefix} 选择不回复(第{NoReplyAction._consecutive_count + 1}次),使用{form_type}形式,原因: {reason}")
|
||||
|
||||
logger.info(f"{self.log_prefix} 选择不回复(第{count}次),开始摸鱼,原因: {reason}")
|
||||
# 增加连续计数(在确定要执行no_reply时才增加)
|
||||
NoReplyAction._consecutive_count += 1
|
||||
|
||||
# 进入等待状态
|
||||
while True:
|
||||
current_time = time.time()
|
||||
elapsed_time = current_time - start_time
|
||||
|
||||
# 1. 检查新消息
|
||||
recent_messages_dict = message_api.get_messages_by_time_in_chat(
|
||||
chat_id=self.chat_id,
|
||||
start_time=start_time,
|
||||
end_time=current_time,
|
||||
filter_mai=True,
|
||||
filter_command=True,
|
||||
)
|
||||
new_message_count = len(recent_messages_dict)
|
||||
|
||||
# 2. 检查消息数量是否达到阈值
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.chat_id)
|
||||
if new_message_count >= exit_message_count_threshold / talk_frequency:
|
||||
logger.info(
|
||||
f"{self.log_prefix} 累计消息数量达到{new_message_count}条(>{exit_message_count_threshold / talk_frequency}),结束等待"
|
||||
)
|
||||
exit_reason = f"{global_config.bot.nickname}(你)看到了{new_message_count}条新消息,可以考虑一下是否要进行回复"
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=exit_reason,
|
||||
action_done=True,
|
||||
)
|
||||
return True, f"累计消息数量达到{new_message_count}条,结束等待 (等待时间: {elapsed_time:.1f}秒)"
|
||||
|
||||
# 3. 检查累计兴趣值
|
||||
if new_message_count > 0:
|
||||
accumulated_interest = 0.0
|
||||
for msg_dict in recent_messages_dict:
|
||||
text = msg_dict.get("processed_plain_text", "")
|
||||
interest_value = msg_dict.get("interest_value", 0.0)
|
||||
if text:
|
||||
accumulated_interest += interest_value
|
||||
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.chat_id)
|
||||
# 只在兴趣值变化时输出log
|
||||
if not hasattr(self, "_last_accumulated_interest") or accumulated_interest != self._last_accumulated_interest:
|
||||
logger.info(f"{self.log_prefix} 当前累计兴趣值: {accumulated_interest:.2f}, 当前聊天频率: {talk_frequency:.2f}")
|
||||
self._last_accumulated_interest = accumulated_interest
|
||||
|
||||
if accumulated_interest >= self._interest_exit_threshold / talk_frequency:
|
||||
logger.info(
|
||||
f"{self.log_prefix} 累计兴趣值达到{accumulated_interest:.2f}(>{self._interest_exit_threshold / talk_frequency}),结束等待"
|
||||
)
|
||||
exit_reason = f"{global_config.bot.nickname}(你)感觉到了大家浓厚的兴趣(兴趣值{accumulated_interest:.1f}),决定重新加入讨论"
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=exit_reason,
|
||||
action_done=True,
|
||||
)
|
||||
return (
|
||||
True,
|
||||
f"累计兴趣值达到{accumulated_interest:.2f},结束等待 (等待时间: {elapsed_time:.1f}秒)",
|
||||
)
|
||||
|
||||
# 每10秒输出一次等待状态
|
||||
if int(elapsed_time) > 0 and int(elapsed_time) % 10 == 0:
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 已等待{elapsed_time:.0f}秒,累计{new_message_count}条消息,继续等待..."
|
||||
)
|
||||
# 使用 asyncio.sleep(1) 来避免在同一秒内重复打印日志
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# 短暂等待后继续检查
|
||||
await asyncio.sleep(check_interval)
|
||||
if form_type == "waiting":
|
||||
return await self._execute_waiting_form(start_time, check_interval)
|
||||
else:
|
||||
return await self._execute_breaking_form(start_time, check_interval)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 不回复动作执行失败: {e}")
|
||||
@@ -153,8 +91,191 @@ class NoReplyAction(BaseAction):
|
||||
)
|
||||
return False, f"不回复动作执行失败: {e}"
|
||||
|
||||
def _determine_form_type(self) -> str:
|
||||
"""判断使用哪种形式的no_reply"""
|
||||
# 如果连续no_reply次数少于3次,使用waiting形式
|
||||
if NoReplyAction._consecutive_count < 3:
|
||||
return "waiting"
|
||||
|
||||
# 如果最近三次记录不足,使用waiting形式
|
||||
if len(NoReplyAction._recent_interest_records) < 3:
|
||||
return "waiting"
|
||||
|
||||
# 计算最近三次记录的兴趣度总和
|
||||
total_recent_interest = sum(NoReplyAction._recent_interest_records)
|
||||
|
||||
# 获取当前聊天频率和意愿系数
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.chat_id)
|
||||
willing_amplifier = global_config.chat.willing_amplifier
|
||||
|
||||
# 计算调整后的阈值
|
||||
adjusted_threshold = self._interest_exit_threshold / talk_frequency / willing_amplifier
|
||||
|
||||
logger.info(f"{self.log_prefix} 最近三次兴趣度总和: {total_recent_interest:.2f}, 调整后阈值: {adjusted_threshold:.2f}")
|
||||
|
||||
# 如果兴趣度总和小于阈值,进入breaking形式
|
||||
if total_recent_interest < adjusted_threshold:
|
||||
logger.info(f"{self.log_prefix} 兴趣度不足,进入breaking形式")
|
||||
return "breaking"
|
||||
else:
|
||||
logger.info(f"{self.log_prefix} 兴趣度充足,继续使用waiting形式")
|
||||
return "waiting"
|
||||
|
||||
async def _execute_waiting_form(self, start_time: float, check_interval: float) -> Tuple[bool, str]:
|
||||
"""执行waiting形式的no_reply"""
|
||||
import asyncio
|
||||
|
||||
logger.info(f"{self.log_prefix} 进入waiting形式,等待任何新消息")
|
||||
|
||||
while True:
|
||||
current_time = time.time()
|
||||
elapsed_time = current_time - start_time
|
||||
|
||||
# 检查新消息
|
||||
recent_messages_dict = message_api.get_messages_by_time_in_chat(
|
||||
chat_id=self.chat_id,
|
||||
start_time=start_time,
|
||||
end_time=current_time,
|
||||
filter_mai=True,
|
||||
filter_command=True,
|
||||
)
|
||||
new_message_count = len(recent_messages_dict)
|
||||
|
||||
# waiting形式:只要有新消息就结束
|
||||
if new_message_count > 0:
|
||||
# 计算新消息的总兴趣度
|
||||
total_interest = 0.0
|
||||
for msg_dict in recent_messages_dict:
|
||||
interest_value = msg_dict.get("interest_value", 0.0)
|
||||
if msg_dict.get("processed_plain_text", ""):
|
||||
total_interest += interest_value * global_config.chat.willing_amplifier
|
||||
|
||||
# 记录到最近兴趣度列表
|
||||
NoReplyAction._recent_interest_records.append(total_interest)
|
||||
|
||||
logger.info(
|
||||
f"{self.log_prefix} waiting形式检测到{new_message_count}条新消息,总兴趣度: {total_interest:.2f},结束等待"
|
||||
)
|
||||
|
||||
exit_reason = f"{global_config.bot.nickname}(你)看到了{new_message_count}条新消息,可以考虑一下是否要进行回复"
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=exit_reason,
|
||||
action_done=True,
|
||||
)
|
||||
return True, f"waiting形式检测到{new_message_count}条新消息,结束等待 (等待时间: {elapsed_time:.1f}秒)"
|
||||
|
||||
# 每10秒输出一次等待状态
|
||||
if int(elapsed_time) > 0 and int(elapsed_time) % 10 == 0:
|
||||
logger.debug(f"{self.log_prefix} waiting形式已等待{elapsed_time:.0f}秒,继续等待新消息...")
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# 短暂等待后继续检查
|
||||
await asyncio.sleep(check_interval)
|
||||
|
||||
async def _execute_breaking_form(self, start_time: float, check_interval: float) -> Tuple[bool, str]:
|
||||
"""执行breaking形式的no_reply(原有逻辑)"""
|
||||
import asyncio
|
||||
|
||||
# 随机生成本次等待需要的新消息数量阈值
|
||||
exit_message_count_threshold = random.randint(self._min_exit_message_count, self._max_exit_message_count)
|
||||
|
||||
logger.info(f"{self.log_prefix} 进入breaking形式,需要{exit_message_count_threshold}条消息或足够兴趣度")
|
||||
|
||||
while True:
|
||||
current_time = time.time()
|
||||
elapsed_time = current_time - start_time
|
||||
|
||||
# 检查新消息
|
||||
recent_messages_dict = message_api.get_messages_by_time_in_chat(
|
||||
chat_id=self.chat_id,
|
||||
start_time=start_time,
|
||||
end_time=current_time,
|
||||
filter_mai=True,
|
||||
filter_command=True,
|
||||
)
|
||||
new_message_count = len(recent_messages_dict)
|
||||
|
||||
# 检查消息数量是否达到阈值
|
||||
talk_frequency = global_config.chat.get_current_talk_frequency(self.chat_id)
|
||||
modified_exit_count_threshold = (exit_message_count_threshold / talk_frequency) / global_config.chat.willing_amplifier
|
||||
|
||||
if new_message_count >= modified_exit_count_threshold:
|
||||
# 记录兴趣度到列表
|
||||
total_interest = 0.0
|
||||
for msg_dict in recent_messages_dict:
|
||||
interest_value = msg_dict.get("interest_value", 0.0)
|
||||
if msg_dict.get("processed_plain_text", ""):
|
||||
total_interest += interest_value * global_config.chat.willing_amplifier
|
||||
|
||||
NoReplyAction._recent_interest_records.append(total_interest)
|
||||
|
||||
logger.info(
|
||||
f"{self.log_prefix} breaking形式累计消息数量达到{new_message_count}条(>{modified_exit_count_threshold}),结束等待"
|
||||
)
|
||||
exit_reason = f"{global_config.bot.nickname}(你)看到了{new_message_count}条新消息,可以考虑一下是否要进行回复"
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=exit_reason,
|
||||
action_done=True,
|
||||
)
|
||||
return True, f"breaking形式累计消息数量达到{new_message_count}条,结束等待 (等待时间: {elapsed_time:.1f}秒)"
|
||||
|
||||
# 检查累计兴趣值
|
||||
if new_message_count > 0:
|
||||
accumulated_interest = 0.0
|
||||
for msg_dict in recent_messages_dict:
|
||||
text = msg_dict.get("processed_plain_text", "")
|
||||
interest_value = msg_dict.get("interest_value", 0.0)
|
||||
if text:
|
||||
accumulated_interest += interest_value * global_config.chat.willing_amplifier
|
||||
|
||||
# 只在兴趣值变化时输出log
|
||||
if not hasattr(self, "_last_accumulated_interest") or accumulated_interest != self._last_accumulated_interest:
|
||||
logger.info(f"{self.log_prefix} breaking形式当前累计兴趣值: {accumulated_interest:.2f}, 当前聊天频率: {talk_frequency:.2f}")
|
||||
self._last_accumulated_interest = accumulated_interest
|
||||
|
||||
if accumulated_interest >= self._interest_exit_threshold / talk_frequency:
|
||||
# 记录兴趣度到列表
|
||||
NoReplyAction._recent_interest_records.append(accumulated_interest)
|
||||
|
||||
logger.info(
|
||||
f"{self.log_prefix} breaking形式累计兴趣值达到{accumulated_interest:.2f}(>{self._interest_exit_threshold / talk_frequency}),结束等待"
|
||||
)
|
||||
exit_reason = f"{global_config.bot.nickname}(你)感觉到了大家浓厚的兴趣(兴趣值{accumulated_interest:.1f}),决定重新加入讨论"
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=exit_reason,
|
||||
action_done=True,
|
||||
)
|
||||
return (
|
||||
True,
|
||||
f"breaking形式累计兴趣值达到{accumulated_interest:.2f},结束等待 (等待时间: {elapsed_time:.1f}秒)",
|
||||
)
|
||||
|
||||
# 每10秒输出一次等待状态
|
||||
if int(elapsed_time) > 0 and int(elapsed_time) % 10 == 0:
|
||||
logger.debug(
|
||||
f"{self.log_prefix} breaking形式已等待{elapsed_time:.0f}秒,累计{new_message_count}条消息,继续等待..."
|
||||
)
|
||||
await asyncio.sleep(1)
|
||||
|
||||
# 短暂等待后继续检查
|
||||
await asyncio.sleep(check_interval)
|
||||
|
||||
@classmethod
|
||||
def reset_consecutive_count(cls):
|
||||
"""重置连续计数器"""
|
||||
"""重置连续计数器和兴趣度记录"""
|
||||
cls._consecutive_count = 0
|
||||
logger.debug("NoReplyAction连续计数器已重置")
|
||||
cls._recent_interest_records.clear()
|
||||
logger.debug("NoReplyAction连续计数器和兴趣度记录已重置")
|
||||
|
||||
@classmethod
|
||||
def get_recent_interest_records(cls) -> List[float]:
|
||||
"""获取最近的兴趣度记录"""
|
||||
return list(cls._recent_interest_records)
|
||||
|
||||
@classmethod
|
||||
def get_consecutive_count(cls) -> int:
|
||||
"""获取连续计数"""
|
||||
return cls._consecutive_count
|
||||
|
||||
@@ -8,9 +8,8 @@
|
||||
from typing import List, Tuple, Type
|
||||
|
||||
# 导入新插件系统
|
||||
from src.plugin_system import BasePlugin, register_plugin, ComponentInfo, ActionActivationType
|
||||
from src.plugin_system import BasePlugin, register_plugin, ComponentInfo
|
||||
from src.plugin_system.base.config_types import ConfigField
|
||||
from src.config.config import global_config
|
||||
|
||||
# 导入依赖的系统组件
|
||||
from src.common.logger import get_logger
|
||||
@@ -18,7 +17,6 @@ from src.common.logger import get_logger
|
||||
# 导入API模块 - 标准Python包方式
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
from src.plugins.built_in.core_actions.emoji import EmojiAction
|
||||
from src.plugins.built_in.core_actions.reply import ReplyAction
|
||||
|
||||
logger = get_logger("core_actions")
|
||||
|
||||
@@ -52,10 +50,9 @@ class CoreActionsPlugin(BasePlugin):
|
||||
config_schema: dict = {
|
||||
"plugin": {
|
||||
"enabled": ConfigField(type=bool, default=True, description="是否启用插件"),
|
||||
"config_version": ConfigField(type=str, default="0.4.0", description="配置文件版本"),
|
||||
"config_version": ConfigField(type=str, default="0.5.0", description="配置文件版本"),
|
||||
},
|
||||
"components": {
|
||||
"enable_reply": ConfigField(type=bool, default=True, description="是否启用回复动作"),
|
||||
"enable_no_reply": ConfigField(type=bool, default=True, description="是否启用不回复动作"),
|
||||
"enable_emoji": ConfigField(type=bool, default=True, description="是否启用发送表情/图片动作"),
|
||||
},
|
||||
@@ -64,23 +61,12 @@ class CoreActionsPlugin(BasePlugin):
|
||||
def get_plugin_components(self) -> List[Tuple[ComponentInfo, Type]]:
|
||||
"""返回插件包含的组件列表"""
|
||||
|
||||
if global_config.emoji.emoji_activate_type == "llm":
|
||||
EmojiAction.random_activation_probability = 0.0
|
||||
EmojiAction.activation_type = ActionActivationType.LLM_JUDGE
|
||||
|
||||
elif global_config.emoji.emoji_activate_type == "random":
|
||||
EmojiAction.random_activation_probability = global_config.emoji.emoji_chance
|
||||
EmojiAction.activation_type = ActionActivationType.RANDOM
|
||||
|
||||
# --- 根据配置注册组件 ---
|
||||
components = []
|
||||
if self.get_config("components.enable_reply", True):
|
||||
components.append((ReplyAction.get_action_info(), ReplyAction))
|
||||
if self.get_config("components.enable_no_reply", True):
|
||||
components.append((NoReplyAction.get_action_info(), NoReplyAction))
|
||||
if self.get_config("components.enable_emoji", True):
|
||||
components.append((EmojiAction.get_action_info(), EmojiAction))
|
||||
|
||||
# components.append((DeepReplyAction.get_action_info(), DeepReplyAction))
|
||||
|
||||
return components
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
# 导入新插件系统
|
||||
from src.plugin_system import BaseAction, ActionActivationType, ChatMode
|
||||
from src.config.config import global_config
|
||||
import random
|
||||
import time
|
||||
from typing import Tuple
|
||||
import asyncio
|
||||
import re
|
||||
import traceback
|
||||
|
||||
# 导入依赖的系统组件
|
||||
from src.common.logger import get_logger
|
||||
|
||||
# 导入API模块 - 标准Python包方式
|
||||
from src.plugin_system.apis import generator_api, message_api
|
||||
from src.plugins.built_in.core_actions.no_reply import NoReplyAction
|
||||
from src.person_info.person_info import get_person_info_manager
|
||||
from src.mais4u.mai_think import mai_thinking_manager
|
||||
from src.mais4u.constant_s4u import ENABLE_S4U
|
||||
|
||||
logger = get_logger("reply_action")
|
||||
|
||||
|
||||
class ReplyAction(BaseAction):
|
||||
"""回复动作 - 参与聊天回复"""
|
||||
|
||||
# 激活设置
|
||||
focus_activation_type = ActionActivationType.NEVER
|
||||
normal_activation_type = ActionActivationType.NEVER
|
||||
mode_enable = ChatMode.FOCUS
|
||||
parallel_action = False
|
||||
|
||||
# 动作基本信息
|
||||
action_name = "reply"
|
||||
action_description = ""
|
||||
|
||||
# 动作参数定义
|
||||
action_parameters = {}
|
||||
|
||||
# 动作使用场景
|
||||
action_require = [""]
|
||||
|
||||
# 关联类型
|
||||
associated_types = ["text"]
|
||||
|
||||
def _parse_reply_target(self, target_message: str) -> tuple:
|
||||
sender = ""
|
||||
target = ""
|
||||
# 添加None检查,防止NoneType错误
|
||||
if target_message is None:
|
||||
return sender, target
|
||||
if ":" in target_message or ":" in target_message:
|
||||
# 使用正则表达式匹配中文或英文冒号
|
||||
parts = re.split(pattern=r"[::]", string=target_message, maxsplit=1)
|
||||
if len(parts) == 2:
|
||||
sender = parts[0].strip()
|
||||
target = parts[1].strip()
|
||||
return sender, target
|
||||
|
||||
async def execute(self) -> Tuple[bool, str]:
|
||||
"""执行回复动作"""
|
||||
logger.debug(f"{self.log_prefix} 决定进行回复")
|
||||
start_time = self.action_data.get("loop_start_time", time.time())
|
||||
|
||||
user_id = self.user_id
|
||||
platform = self.platform
|
||||
# logger.info(f"{self.log_prefix} 用户ID: {user_id}, 平台: {platform}")
|
||||
person_id = get_person_info_manager().get_person_id(platform, user_id) # type: ignore
|
||||
# logger.info(f"{self.log_prefix} 人物ID: {person_id}")
|
||||
person_name = get_person_info_manager().get_value_sync(person_id, "person_name")
|
||||
reply_to = f"{person_name}:{self.action_message.get('processed_plain_text', '')}" # type: ignore
|
||||
logger.info(f"{self.log_prefix} 决定进行回复,目标: {reply_to}")
|
||||
|
||||
try:
|
||||
if prepared_reply := self.action_data.get("prepared_reply", ""):
|
||||
reply_text = prepared_reply
|
||||
else:
|
||||
try:
|
||||
success, reply_set, _ = await asyncio.wait_for(
|
||||
generator_api.generate_reply(
|
||||
extra_info="",
|
||||
reply_to=reply_to,
|
||||
chat_id=self.chat_id,
|
||||
request_type="chat.replyer.focus",
|
||||
enable_tool=global_config.tool.enable_in_focus_chat,
|
||||
),
|
||||
timeout=global_config.chat.thinking_timeout,
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"{self.log_prefix} 回复生成超时 ({global_config.chat.thinking_timeout}s)")
|
||||
return False, "timeout"
|
||||
|
||||
# 检查从start_time以来的新消息数量
|
||||
# 获取动作触发时间或使用默认值
|
||||
current_time = time.time()
|
||||
new_message_count = message_api.count_new_messages(
|
||||
chat_id=self.chat_id, start_time=start_time, end_time=current_time
|
||||
)
|
||||
|
||||
# 根据新消息数量决定是否使用reply_to
|
||||
need_reply = new_message_count >= random.randint(2, 4)
|
||||
if need_reply:
|
||||
logger.info(
|
||||
f"{self.log_prefix} 从思考到回复,共有{new_message_count}条新消息,使用引用回复"
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"{self.log_prefix} 从思考到回复,共有{new_message_count}条新消息,不使用引用回复"
|
||||
)
|
||||
|
||||
# 构建回复文本
|
||||
reply_text = ""
|
||||
first_replied = False
|
||||
reply_to_platform_id = f"{platform}:{user_id}"
|
||||
for reply_seg in reply_set:
|
||||
data = reply_seg[1]
|
||||
if not first_replied:
|
||||
if need_reply:
|
||||
await self.send_text(
|
||||
content=data, reply_to=reply_to, reply_to_platform_id=reply_to_platform_id, typing=False
|
||||
)
|
||||
else:
|
||||
await self.send_text(content=data, reply_to_platform_id=reply_to_platform_id, typing=False)
|
||||
first_replied = True
|
||||
else:
|
||||
await self.send_text(content=data, reply_to_platform_id=reply_to_platform_id, typing=True)
|
||||
reply_text += data
|
||||
|
||||
# 存储动作记录
|
||||
reply_text = f"你对{person_name}进行了回复:{reply_text}"
|
||||
|
||||
if ENABLE_S4U:
|
||||
await mai_thinking_manager.get_mai_think(self.chat_id).do_think_after_response(reply_text)
|
||||
|
||||
await self.store_action_info(
|
||||
action_build_into_prompt=False,
|
||||
action_prompt_display=reply_text,
|
||||
action_done=True,
|
||||
)
|
||||
|
||||
# 重置NoReplyAction的连续计数器
|
||||
NoReplyAction.reset_consecutive_count()
|
||||
|
||||
return success, reply_text
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 回复动作执行失败: {e}")
|
||||
traceback.print_exc()
|
||||
return False, f"回复失败: {str(e)}"
|
||||
@@ -9,7 +9,7 @@
|
||||
},
|
||||
"license": "GPL-v3.0-or-later",
|
||||
"host_application": {
|
||||
"min_version": "0.9.0"
|
||||
"min_version": "0.9.1"
|
||||
},
|
||||
"homepage_url": "https://github.com/MaiM-with-u/maibot",
|
||||
"repository_url": "https://github.com/MaiM-with-u/maibot",
|
||||
|
||||
@@ -428,13 +428,14 @@ class PluginManagementPlugin(BasePlugin):
|
||||
config_file_name: str = "config.toml"
|
||||
config_schema: dict = {
|
||||
"plugin": {
|
||||
"enable": ConfigField(bool, default=False, description="是否启用插件"),
|
||||
"permission": ConfigField(list, default=[], description="有权限使用插件管理命令的用户列表"),
|
||||
"enabled": ConfigField(bool, default=False, description="是否启用插件"),
|
||||
"config_version": ConfigField(type=str, default="1.1.0", description="配置文件版本"),
|
||||
"permission": ConfigField(list, default=[], description="有权限使用插件管理命令的用户列表,请填写字符串形式的用户ID"),
|
||||
},
|
||||
}
|
||||
|
||||
def get_plugin_components(self) -> List[Tuple[CommandInfo, Type[BaseCommand]]]:
|
||||
components = []
|
||||
if self.get_config("plugin.enable", True):
|
||||
if self.get_config("plugin.enabled", True):
|
||||
components.append((ManagementCommand.get_command_info(), ManagementCommand))
|
||||
return components
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[inner]
|
||||
version = "4.4.8"
|
||||
version = "4.5.0"
|
||||
|
||||
#----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读----
|
||||
#如果你想要修改配置文件,请在修改后将version的值进行变更
|
||||
@@ -52,26 +52,26 @@ relation_frequency = 1 # 关系频率,麦麦构建关系的频率
|
||||
|
||||
[chat] #麦麦的聊天通用设置
|
||||
focus_value = 1
|
||||
# 麦麦的专注思考能力,越低越容易专注,消耗token也越多
|
||||
# 麦麦的专注思考能力,越高越容易专注,可能消耗更多token
|
||||
# 专注时能更好把握发言时机,能够进行持久的连续对话
|
||||
|
||||
willing_amplifier = 1 # 麦麦回复意愿
|
||||
|
||||
max_context_size = 25 # 上下文长度
|
||||
thinking_timeout = 20 # 麦麦一次回复最长思考规划时间,超过这个时间的思考会放弃(往往是api反应太慢)
|
||||
thinking_timeout = 40 # 麦麦一次回复最长思考规划时间,超过这个时间的思考会放弃(往往是api反应太慢)
|
||||
replyer_random_probability = 0.5 # 首要replyer模型被选择的概率
|
||||
|
||||
mentioned_bot_inevitable_reply = true # 提及 bot 大概率回复
|
||||
at_bot_inevitable_reply = true # @bot 或 提及bot 大概率回复
|
||||
|
||||
use_s4u_prompt_mode = true # 是否使用 s4u 对话构建模式,该模式会更好的把握当前对话对象的对话内容,但是对群聊整理理解能力较差(测试功能!!可能有未知问题!!)
|
||||
|
||||
|
||||
talk_frequency = 1 # 麦麦回复频率,越高,麦麦回复越频繁
|
||||
|
||||
time_based_talk_frequency = ["8:00,1", "12:00,1.5", "18:00,2", "01:00,0.5"]
|
||||
time_based_talk_frequency = ["8:00,1", "12:00,1.2", "18:00,1.5", "01:00,0.6"]
|
||||
# 基于时段的回复频率配置(可选)
|
||||
# 格式:time_based_talk_frequency = ["HH:MM,frequency", ...]
|
||||
# 示例:
|
||||
# time_based_talk_frequency = ["8:00,1", "12:00,2", "18:00,1.5", "00:00,0.5"]
|
||||
# time_based_talk_frequency = ["8:00,1", "12:00,1.2", "18:00,1.5", "00:00,0.6"]
|
||||
# 说明:表示从该时间开始使用该频率,直到下一个时间点
|
||||
# 注意:如果没有配置,则使用上面的默认 talk_frequency 值
|
||||
|
||||
@@ -105,11 +105,9 @@ ban_msgs_regex = [
|
||||
|
||||
[normal_chat] #普通聊天
|
||||
willing_mode = "classical" # 回复意愿模式 —— 经典模式:classical,mxp模式:mxp,自定义模式:custom(需要你自己实现)
|
||||
response_interested_rate_amplifier = 1 # 麦麦回复兴趣度放大系数
|
||||
|
||||
[tool]
|
||||
enable_in_normal_chat = false # 是否在普通聊天中启用工具
|
||||
enable_in_focus_chat = true # 是否在专注聊天中启用工具
|
||||
enable_tool = false # 是否在普通聊天中启用工具
|
||||
|
||||
[emoji]
|
||||
emoji_chance = 0.6 # 麦麦激活表情包动作的概率
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
HOST=127.0.0.1
|
||||
PORT=8000
|
||||
|
||||
#key and url
|
||||
# 密钥和url
|
||||
|
||||
# 硅基流动
|
||||
SILICONFLOW_BASE_URL=https://api.siliconflow.cn/v1/
|
||||
# DeepSeek官方
|
||||
DEEP_SEEK_BASE_URL=https://api.deepseek.com/v1
|
||||
CHAT_ANY_WHERE_BASE_URL=https://api.chatanywhere.tech/v1
|
||||
# 阿里百炼
|
||||
BAILIAN_BASE_URL = https://dashscope.aliyuncs.com/compatible-mode/v1
|
||||
# 火山引擎
|
||||
HUOSHAN_BASE_URL =
|
||||
# xxxxx平台
|
||||
xxxxxxx_BASE_URL=https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
|
||||
# 定义你要用的api的key(需要去对应网站申请哦)
|
||||
@@ -13,4 +19,5 @@ DEEP_SEEK_KEY=
|
||||
CHAT_ANY_WHERE_KEY=
|
||||
SILICONFLOW_KEY=
|
||||
BAILIAN_KEY =
|
||||
xxxxxxx_KEY=
|
||||
HUOSHAN_KEY =
|
||||
xxxxxxx_KEY=
|
||||
|
||||
Reference in New Issue
Block a user