mofox-plugin-dev-toolkit 0.4.8__tar.gz → 0.4.10__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. {mofox_plugin_dev_toolkit-0.4.8/mofox_plugin_dev_toolkit.egg-info → mofox_plugin_dev_toolkit-0.4.10}/PKG-INFO +1 -1
  2. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10/mofox_plugin_dev_toolkit.egg-info}/PKG-INFO +1 -1
  3. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/__init__.py +1 -1
  4. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/cli.py +6 -4
  5. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/commands/build.py +4 -2
  6. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/commands/generate.py +2 -1
  7. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/dev/bridge_plugin/cleanup_handler.py +35 -7
  8. mofox_plugin_dev_toolkit-0.4.10/mpdt/templates/event_template.py +125 -0
  9. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/pyproject.toml +1 -1
  10. mofox_plugin_dev_toolkit-0.4.8/mpdt/templates/event_template.py +0 -101
  11. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/LICENSE +0 -0
  12. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/MANIFEST.in +0 -0
  13. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/README.md +0 -0
  14. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mofox_plugin_dev_toolkit.egg-info/SOURCES.txt +0 -0
  15. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mofox_plugin_dev_toolkit.egg-info/dependency_links.txt +0 -0
  16. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mofox_plugin_dev_toolkit.egg-info/entry_points.txt +0 -0
  17. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mofox_plugin_dev_toolkit.egg-info/requires.txt +0 -0
  18. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mofox_plugin_dev_toolkit.egg-info/top_level.txt +0 -0
  19. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/__main__.py +0 -0
  20. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/commands/__init__.py +0 -0
  21. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/commands/check.py +0 -0
  22. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/commands/dev.py +0 -0
  23. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/commands/init.py +0 -0
  24. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/dev/bridge_plugin/__init__.py +0 -0
  25. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/dev/bridge_plugin/dev_config.py +0 -0
  26. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/dev/bridge_plugin/file_watcher.py +0 -0
  27. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/dev/bridge_plugin/manifest.json +0 -0
  28. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/dev/bridge_plugin/plugin.py +0 -0
  29. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/__init__.py +0 -0
  30. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/action_template.py +0 -0
  31. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/adapter_template.py +0 -0
  32. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/chatter_template.py +0 -0
  33. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/collection_template.py +0 -0
  34. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/config_template.py +0 -0
  35. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/plus_command_template.py +0 -0
  36. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/prompt_template.py +0 -0
  37. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/router_template.py +0 -0
  38. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/service_template.py +0 -0
  39. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/templates/tool_template.py +0 -0
  40. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/__init__.py +0 -0
  41. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/code_parser.py +0 -0
  42. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/color_printer.py +0 -0
  43. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/config_loader.py +0 -0
  44. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/config_manager.py +0 -0
  45. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/file_ops.py +0 -0
  46. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/license_generator.py +0 -0
  47. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/plugin_parser.py +0 -0
  48. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/utils/template_engine.py +0 -0
  49. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/__init__.py +0 -0
  50. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/auto_fix_validator.py +0 -0
  51. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/base.py +0 -0
  52. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/component_validator.py +0 -0
  53. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/config_validator.py +0 -0
  54. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/metadata_validator.py +0 -0
  55. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/structure_validator.py +0 -0
  56. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/style_validator.py +0 -0
  57. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/mpdt/validators/type_validator.py +0 -0
  58. {mofox_plugin_dev_toolkit-0.4.8 → mofox_plugin_dev_toolkit-0.4.10}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mofox-plugin-dev-toolkit
3
- Version: 0.4.8
3
+ Version: 0.4.10
4
4
  Summary: 开发工具集,用于快速创建、开发和测试 MoFox-Bot 插件
5
5
  Author-email: MoFox-Studio <wwwww95915@qq.com>
6
6
  License: GPL-3.0-or-later
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mofox-plugin-dev-toolkit
3
- Version: 0.4.8
3
+ Version: 0.4.10
4
4
  Summary: 开发工具集,用于快速创建、开发和测试 MoFox-Bot 插件
5
5
  Author-email: MoFox-Studio <wwwww95915@qq.com>
6
6
  License: GPL-3.0-or-later
@@ -4,7 +4,7 @@ MoFox Plugin Dev Toolkit
4
4
  一个用于 MoFox-Bot 插件开发的工具集
5
5
  """
6
6
 
7
- __version__ = "0.4.7"
7
+ __version__ = "0.4.9"
8
8
  __author__ = "MoFox-Studio"
9
9
  __license__ = "GPL-3.0-or-later"
10
10
 
@@ -36,7 +36,7 @@ def cli(ctx: click.Context, verbose: bool, no_color: bool) -> None:
36
36
 
37
37
  @cli.command()
38
38
  @click.argument("plugin_name", required=False)
39
- @click.option("--template", "-t", type=click.Choice(["basic", "action", "tool", "plus_command", "full", "adapter"]),
39
+ @click.option("--template", "-t", type=click.Choice(["basic", "action", "tool", "collection", "router", "plus_command", "event_handler", "full", "adapter", "chatter"]),
40
40
  default="basic", help="插件模板类型")
41
41
  @click.option("--author", "-a", help="作者名称")
42
42
  @click.option("--email", "-e", help="作者电子邮箱")
@@ -69,14 +69,15 @@ def init(ctx: click.Context, plugin_name: str | None, template: str,email: str|
69
69
 
70
70
 
71
71
  @cli.command()
72
- @click.argument("component_type", type=click.Choice(["action", "tool", "event", "adapter", "plus-command", "router", "chatter", "service", "config"]), required=False)
72
+ @click.argument("component_type", type=click.Choice(["action", "tool", "collection", "event", "adapter", "plus-command", "router", "chatter", "service", "config"]), required=False)
73
73
  @click.argument("component_name", required=False)
74
74
  @click.option("--description", "-d", help="组件描述")
75
- @click.option("--output", "-o", type=click.Path(), help="输出目录")
75
+ @click.option("--output", "-o", type=click.Path(), help="插件根目录路径(默认为当前目录)")
76
76
  @click.option("--force", "-f", is_flag=True, help="覆盖已存在的文件")
77
+ @click.option("--root", is_flag=True, help="在插件根目录生成组件文件,而不是 components/ 文件夹")
77
78
  @click.pass_context
78
79
  def generate(ctx: click.Context, component_type: str | None, component_name: str | None, description: str | None,
79
- output: str | None, force: bool) -> None:
80
+ output: str | None, force: bool, root: bool) -> None:
80
81
  """生成插件组件(始终生成异步方法)
81
82
 
82
83
  如果不提供参数,将进入交互式问答模式
@@ -90,6 +91,7 @@ def generate(ctx: click.Context, component_type: str | None, component_name: str
90
91
  description=description,
91
92
  output_dir=output,
92
93
  force=force,
94
+ use_components_folder=not root,
93
95
  verbose=ctx.obj["verbose"],
94
96
  )
95
97
  except Exception as e:
@@ -178,6 +178,7 @@ def build_plugin(
178
178
  with_docs: 是否将文档文件打入包中
179
179
  fmt: 输出格式,"mfp"(推荐) 或 "zip"
180
180
  bump: 自动升级版本,"major" / "minor" / "patch" / None
181
+ (升级后会立即写回 manifest.json,打包时使用新版本号)
181
182
  verbose: 是否显示详细信息
182
183
  """
183
184
  plugin_dir = Path(plugin_path).resolve()
@@ -205,7 +206,7 @@ def build_plugin(
205
206
  plugin_name: str = manifest["name"]
206
207
  plugin_version: str = manifest["version"]
207
208
 
208
- # ── 2. 版本升级 ──────────────────────────────────────────────────────────
209
+ # ── 2. 版本升级(先写回文件,再使用新版本号进行打包) ────────────────────
209
210
  if bump:
210
211
  try:
211
212
  new_version = _bump_version(plugin_version, bump)
@@ -214,7 +215,8 @@ def build_plugin(
214
215
  return
215
216
  print_step(f"版本升级: [yellow]{plugin_version}[/yellow] → [green]{new_version}[/green]")
216
217
  manifest["version"] = new_version
217
- _save_manifest(plugin_dir, manifest)
218
+ # 立即写回 manifest.json,确保后续打包使用新版本
219
+ _save_manifest(plugin_dir, manifest)
218
220
  plugin_version = new_version
219
221
 
220
222
  # ── 3. 验证入口文件 ───────────────────────────────────────────────────────
@@ -68,6 +68,7 @@ def generate_component(
68
68
  description: str | None = None,
69
69
  output_dir: str | None = None,
70
70
  force: bool = False,
71
+ use_components_folder: bool = True,
71
72
  verbose: bool = False,
72
73
  ) -> None:
73
74
  """
@@ -79,6 +80,7 @@ def generate_component(
79
80
  description: 组件描述
80
81
  output_dir: 输出目录
81
82
  force: 是否覆盖
83
+ use_components_folder: 是否在 components/ 文件夹中生成(False 则在根目录生成)
82
84
  verbose: 详细输出
83
85
  """
84
86
  # 确定工作目录
@@ -98,7 +100,6 @@ def generate_component(
98
100
  console.print(f"[dim]检测到插件: {plugin_name}[/dim]")
99
101
 
100
102
  # 交互式获取组件信息
101
- use_components_folder = True # 默认使用 components 文件夹
102
103
  if not component_type or not component_name:
103
104
  component_info = _interactive_generate()
104
105
  component_type = component_info["component_type"]
@@ -5,13 +5,18 @@ DevBridge 清理事件处理器
5
5
 
6
6
  import shutil
7
7
  from pathlib import Path
8
+ from typing import TYPE_CHECKING, Any
8
9
 
9
10
  from src.core.components.base.event_handler import BaseEventHandler
10
11
  from src.core.components.types import EventType
12
+ from src.kernel.event import EventDecision
11
13
  from src.kernel.logger import get_logger
12
14
 
13
15
  from .dev_config import TARGET_PLUGIN_NAME, TARGET_PLUGIN_PATH
14
16
 
17
+ if TYPE_CHECKING:
18
+ from .plugin import DevBridgePlugin
19
+
15
20
  logger = get_logger("dev_bridge_cleanup")
16
21
 
17
22
 
@@ -19,22 +24,45 @@ class CleanupHandler(BaseEventHandler):
19
24
  """清理事件处理器 - 在程序停止时清理插件文件"""
20
25
 
21
26
  handler_name = "dev_bridge_cleanup"
22
- handler_description = "DevBridge 清理处理器"
23
- weight = 100 # 负权重,确保最后执行
27
+ handler_description = "DevBridge 清理处理器,在系统停止时移除临时插件文件"
28
+ weight = -100 # 负权重,确保最后执行
29
+ intercept_message = False
24
30
  init_subscribe: list[EventType | str] = [EventType.ON_STOP]
25
31
 
26
- def __init__(self, plugin=None):
32
+ def __init__(self, plugin: "DevBridgePlugin") -> None:
33
+ """初始化清理处理器
34
+
35
+ Args:
36
+ plugin: DevBridge 插件实例
37
+ """
27
38
  super().__init__(plugin)
28
39
  self._target_plugin_name = TARGET_PLUGIN_NAME
29
40
  self._target_plugin_path = TARGET_PLUGIN_PATH
30
41
 
31
- async def execute(self, kwargs: dict | None) -> tuple[bool, bool, str | None]:
32
- """程序停止时执行清理(同步删除)"""
42
+ async def execute(
43
+ self,
44
+ event_name: str,
45
+ params: dict[str, Any],
46
+ ) -> tuple[EventDecision, dict[str, Any]]:
47
+ """程序停止时执行清理(同步删除)
48
+
49
+ Args:
50
+ event_name: 事件名称(ON_STOP)
51
+ params: 事件参数
52
+
53
+ Returns:
54
+ 事件决策和更新后的参数
55
+ """
33
56
  logger.info("🛑 收到停止事件,准备清理 DevBridge...")
34
57
 
35
- self._delete_plugins()
58
+ try:
59
+ self._delete_plugins()
60
+ logger.info("✅ DevBridge 清理完成")
61
+ except Exception as e:
62
+ logger.error(f"❌ DevBridge 清理失败: {e}", exc_info=True)
36
63
 
37
- return True, True, None
64
+ # 返回 SUCCESS,继续执行后续清理处理器
65
+ return EventDecision.SUCCESS, params
38
66
 
39
67
  def _delete_plugins(self):
40
68
  """同步删除插件目录"""
@@ -0,0 +1,125 @@
1
+ """
2
+ Event Handler 组件模板(Neo-MoFox 架构)
3
+ """
4
+
5
+ EVENT_HANDLER_TEMPLATE = '''"""
6
+ {description}
7
+
8
+ Created by: {author}
9
+ Created at: {date}
10
+ """
11
+
12
+ from typing import Any
13
+
14
+ from src.app.plugin_system.api.log_api import get_logger
15
+ from src.core.components.base import BaseEventHandler
16
+ from src.core.components.types import EventType
17
+ from src.kernel.event import EventDecision
18
+
19
+ logger = get_logger(__name__)
20
+
21
+
22
+ class {class_name}(BaseEventHandler):
23
+ """
24
+ {description}
25
+
26
+ Event Handler 组件用于处理系统事件。
27
+
28
+ 使用场景:
29
+ - 监听消息事件
30
+ - 监听系统事件
31
+ - 实现事件驱动逻辑
32
+ - 在特定事件发生时执行操作
33
+ """
34
+
35
+ handler_name = "{component_name}"
36
+ handler_description = "{description}"
37
+ weight = 10 # 权重:数值越大优先级越高
38
+ intercept_message = False # 是否拦截消息(拦截后消息不再传递给后续处理器)
39
+ init_subscribe = [EventType.ON_MESSAGE_RECEIVED] # 初始订阅的事件类型列表
40
+
41
+ async def execute(
42
+ self, event_name: str, params: dict[str, Any]
43
+ ) -> tuple[EventDecision, dict[str, Any]]:
44
+ """
45
+ 执行事件处理的主要逻辑。
46
+
47
+ 与 kernel EventBus 订阅者协议保持一致:接受事件名称和参数字典,
48
+ 返回决策枚举与(可能已修改的)参数字典。
49
+
50
+ Args:
51
+ event_name: 触发本处理器的事件名称(由 EventBus 传入)
52
+ params: 事件参数字典(即 EventBus publish 时的 params,可就地修改)
53
+
54
+ Returns:
55
+ tuple[EventDecision, dict[str, Any]]:
56
+ - ``EventDecision.SUCCESS`` — 执行完成,继续后续处理器
57
+ - ``EventDecision.STOP`` — 拦截,阻止后续处理器执行
58
+ - ``EventDecision.PASS`` — 跳过本处理器,不传播参数变更
59
+ """
60
+ try:
61
+ logger.info(f"处理事件: {{event_name}}")
62
+ logger.debug(f"事件参数: {{params}}")
63
+
64
+ # 检查是否应该处理此事件
65
+ if not self._should_handle(event_name, params):
66
+ logger.debug("跳过此事件")
67
+ return EventDecision.PASS, params
68
+
69
+ # TODO: 实现事件处理逻辑
70
+ await self._process_event(event_name, params)
71
+
72
+ logger.info("事件处理完成")
73
+ # 正常处理,继续后续处理器
74
+ return EventDecision.SUCCESS, params
75
+
76
+ except Exception as e:
77
+ logger.error(f"事件处理失败: {{e}}", exc_info=True)
78
+ # 发生错误时跳过,不影响其他处理器
79
+ return EventDecision.PASS, params
80
+
81
+ def _should_handle(self, event_name: str, params: dict[str, Any]) -> bool:
82
+ """
83
+ 判断是否应该处理该事件
84
+
85
+ Args:
86
+ event_name: 事件名称
87
+ params: 事件参数
88
+
89
+ Returns:
90
+ 是否处理
91
+ """
92
+ # TODO: 实现判断逻辑
93
+ # 示例: 检查事件类型、来源、条件等
94
+ # if event_name == EventType.ON_MESSAGE_RECEIVED:
95
+ # message = params.get("message")
96
+ # if message and "关键词" in message:
97
+ # return True
98
+ return True
99
+
100
+ async def _process_event(self, event_name: str, params: dict[str, Any]) -> None:
101
+ """
102
+ 处理事件的核心逻辑
103
+
104
+ Args:
105
+ event_name: 事件名称
106
+ params: 事件参数(可就地修改)
107
+ """
108
+ # TODO: 实现处理逻辑
109
+ # 示例:修改参数
110
+ # params["processed"] = True
111
+ # params["handler_name"] = self.handler_name
112
+
113
+ # 示例:根据事件类型执行不同逻辑
114
+ # if event_name == EventType.ON_MESSAGE_RECEIVED:
115
+ # await self._handle_message(params)
116
+ # elif event_name == EventType.ON_START:
117
+ # await self._handle_bot_start(params)
118
+
119
+ logger.info(f"事件 {{event_name}} 处理完成")
120
+ '''
121
+
122
+
123
+ def get_event_handler_template() -> str:
124
+ """获取 Event Handler 组件模板"""
125
+ return EVENT_HANDLER_TEMPLATE
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "mofox-plugin-dev-toolkit"
7
- version = "0.4.8"
7
+ version = "0.4.10"
8
8
  description = "开发工具集,用于快速创建、开发和测试 MoFox-Bot 插件"
9
9
  authors = [
10
10
  {name = "MoFox-Studio", email = "wwwww95915@qq.com"}
@@ -1,101 +0,0 @@
1
- """
2
- Event Handler 组件模板(Neo-MoFox 架构)
3
- """
4
-
5
- EVENT_HANDLER_TEMPLATE = '''"""
6
- {description}
7
-
8
- Created by: {author}
9
- Created at: {date}
10
- """
11
-
12
- from src.app.plugin_system.api.log_api import get_logger
13
- from src.core.components.base import BaseEventHandler
14
- from src.core.components.types import EventType
15
-
16
- logger = get_logger(__name__)
17
-
18
-
19
- class {class_name}(BaseEventHandler):
20
- """
21
- {description}
22
-
23
- Event Handler 组件用于处理系统事件。
24
-
25
- 使用场景:
26
- - 监听消息事件
27
- - 监听系统事件
28
- - 实现事件驱动逻辑
29
- - 在特定事件发生时执行操作
30
- """
31
-
32
- handler_name = "{component_name}"
33
- handler_description = "{description}"
34
- weight = 10 # 权重:数值越大优先级越高
35
- intercept_message = False # 是否拦截消息(拦截后消息不再传递给后续处理器)
36
- init_subscribe = [EventType.ON_MESSAGE_RECEIVED] # 初始订阅的事件类型列表
37
-
38
- async def execute(self, kwargs: dict | None) -> tuple[bool, bool, str | None]:
39
- """
40
- 处理事件
41
-
42
- Args:
43
- kwargs: 事件参数字典,包含事件相关的所有信息
44
-
45
- Returns:
46
- tuple[bool, bool, str | None]: (success, intercept, message)
47
- - success: 是否成功
48
- - intercept: 是否拦截(True 则阻止后续处理器执行)
49
- - message: 返回消息(可选)
50
- """
51
- try:
52
- logger.info(f"处理事件: {{self.handler_name}}")
53
- logger.debug(f"事件参数: {{kwargs}}")
54
-
55
- # 检查是否应该处理此事件
56
- if not self._should_handle(kwargs):
57
- logger.debug("跳过此事件")
58
- return True, False, None
59
-
60
- # TODO: 实现事件处理逻辑
61
- result = await self._process_event(kwargs)
62
-
63
- logger.info("事件处理完成")
64
- return True, False, result
65
-
66
- except Exception as e:
67
- logger.error(f"事件处理失败: {{e}}")
68
- return False, False, str(e)
69
-
70
- def _should_handle(self, kwargs: dict | None) -> bool:
71
- """
72
- 判断是否应该处理该事件
73
-
74
- Args:
75
- kwargs: 事件参数
76
-
77
- Returns:
78
- 是否处理
79
- """
80
- # TODO: 实现判断逻辑
81
- # 示例: 检查事件类型、来源、条件等
82
- return True
83
-
84
- async def _process_event(self, kwargs: dict | None) -> str:
85
- """
86
- 处理事件的核心逻辑
87
-
88
- Args:
89
- kwargs: 事件参数
90
-
91
- Returns:
92
- 处理结果消息
93
- """
94
- # TODO: 实现处理逻辑
95
- return "事件处理成功"
96
- '''
97
-
98
-
99
- def get_event_handler_template() -> str:
100
- """获取 Event Handler 组件模板"""
101
- return EVENT_HANDLER_TEMPLATE