mofox-plugin-dev-toolkit 0.2.1__py3-none-any.whl
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.
- mofox_plugin_dev_toolkit-0.2.1.dist-info/METADATA +409 -0
- mofox_plugin_dev_toolkit-0.2.1.dist-info/RECORD +43 -0
- mofox_plugin_dev_toolkit-0.2.1.dist-info/WHEEL +5 -0
- mofox_plugin_dev_toolkit-0.2.1.dist-info/entry_points.txt +2 -0
- mofox_plugin_dev_toolkit-0.2.1.dist-info/licenses/LICENSE +674 -0
- mofox_plugin_dev_toolkit-0.2.1.dist-info/top_level.txt +1 -0
- mpdt/__init__.py +15 -0
- mpdt/__main__.py +8 -0
- mpdt/cli.py +314 -0
- mpdt/commands/__init__.py +9 -0
- mpdt/commands/check.py +316 -0
- mpdt/commands/dev.py +550 -0
- mpdt/commands/generate.py +366 -0
- mpdt/commands/init.py +487 -0
- mpdt/dev/bridge_plugin/__init__.py +17 -0
- mpdt/dev/bridge_plugin/discovery_server.py +126 -0
- mpdt/dev/bridge_plugin/plugin.py +258 -0
- mpdt/templates/__init__.py +165 -0
- mpdt/templates/action_template.py +102 -0
- mpdt/templates/adapter_template.py +129 -0
- mpdt/templates/chatter_template.py +103 -0
- mpdt/templates/event_template.py +116 -0
- mpdt/templates/plus_command_template.py +150 -0
- mpdt/templates/prompt_template.py +92 -0
- mpdt/templates/router_template.py +175 -0
- mpdt/templates/tool_template.py +98 -0
- mpdt/utils/__init__.py +10 -0
- mpdt/utils/color_printer.py +99 -0
- mpdt/utils/config_loader.py +171 -0
- mpdt/utils/config_manager.py +297 -0
- mpdt/utils/file_ops.py +203 -0
- mpdt/utils/license_generator.py +980 -0
- mpdt/utils/plugin_parser.py +196 -0
- mpdt/utils/template_engine.py +112 -0
- mpdt/validators/__init__.py +26 -0
- mpdt/validators/auto_fix_validator.py +182 -0
- mpdt/validators/base.py +121 -0
- mpdt/validators/component_validator.py +415 -0
- mpdt/validators/config_validator.py +173 -0
- mpdt/validators/metadata_validator.py +125 -0
- mpdt/validators/structure_validator.py +70 -0
- mpdt/validators/style_validator.py +125 -0
- mpdt/validators/type_validator.py +223 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Chatter 组件模板
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
CHATTER_TEMPLATE = '''"""
|
|
6
|
+
{description}
|
|
7
|
+
|
|
8
|
+
Created by: {author}
|
|
9
|
+
Created at: {date}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from src.common.data_models.message_manager_data_model import StreamContext
|
|
13
|
+
from src.common.logger import get_logger
|
|
14
|
+
from src.plugin_system import BaseChatter, ChatType
|
|
15
|
+
|
|
16
|
+
logger = get_logger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class {class_name}(BaseChatter):
|
|
20
|
+
"""
|
|
21
|
+
{description}
|
|
22
|
+
|
|
23
|
+
Chatter 组件用于处理聊天流程,控制对话的整体逻辑。
|
|
24
|
+
|
|
25
|
+
使用场景:
|
|
26
|
+
- 自定义对话流程
|
|
27
|
+
- 特殊聊天模式处理
|
|
28
|
+
- 对话状态管理
|
|
29
|
+
- 多轮对话控制
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
# Chatter 元数据
|
|
33
|
+
chatter_name: str = "{chatter_name}"
|
|
34
|
+
chatter_description: str = "{description}"
|
|
35
|
+
chat_types: list[ChatType] = [ChatType.PRIVATE, ChatType.GROUP] # 支持的聊天类型
|
|
36
|
+
|
|
37
|
+
async def execute(self, context: StreamContext) -> dict:
|
|
38
|
+
"""
|
|
39
|
+
执行聊天处理逻辑
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
context: StreamContext对象,包含聊天上下文信息
|
|
43
|
+
- context.stream_id: 聊天流ID
|
|
44
|
+
- context.user_id: 用户ID
|
|
45
|
+
- context.user_name: 用户名
|
|
46
|
+
- context.message_content: 消息内容
|
|
47
|
+
- context.chat_type: 聊天类型
|
|
48
|
+
- 等等...
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
处理结果字典,包含:
|
|
52
|
+
- success: 是否成功
|
|
53
|
+
- response: 响应内容(可选)
|
|
54
|
+
- next_action: 下一步动作(可选)
|
|
55
|
+
"""
|
|
56
|
+
try:
|
|
57
|
+
logger.info(f"执行 Chatter: {{self.chatter_name}}")
|
|
58
|
+
logger.debug(f"聊天上下文: {{context}}")
|
|
59
|
+
|
|
60
|
+
# TODO: 实现聊天处理逻辑
|
|
61
|
+
|
|
62
|
+
# 示例:根据消息内容处理
|
|
63
|
+
message = context.message_content
|
|
64
|
+
user_name = context.user_name
|
|
65
|
+
|
|
66
|
+
# 可以使用 action_manager 调用 Action
|
|
67
|
+
# result = await self.action_manager.execute_action("action_name", {{}})
|
|
68
|
+
|
|
69
|
+
# 构建响应
|
|
70
|
+
response = self._generate_response(message, user_name)
|
|
71
|
+
|
|
72
|
+
return {{
|
|
73
|
+
"success": True,
|
|
74
|
+
"response": response,
|
|
75
|
+
"next_action": None
|
|
76
|
+
}}
|
|
77
|
+
|
|
78
|
+
except Exception as e:
|
|
79
|
+
logger.error(f"Chatter 执行失败: {{e}}")
|
|
80
|
+
return {{
|
|
81
|
+
"success": False,
|
|
82
|
+
"error": str(e)
|
|
83
|
+
}}
|
|
84
|
+
|
|
85
|
+
def _generate_response(self, message: str, user_name: str) -> str:
|
|
86
|
+
"""
|
|
87
|
+
生成响应内容
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
message: 用户消息
|
|
91
|
+
user_name: 用户名
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
响应文本
|
|
95
|
+
"""
|
|
96
|
+
# TODO: 实现响应生成逻辑
|
|
97
|
+
return f"收到 {{user_name}} 的消息: {{message}}"
|
|
98
|
+
'''
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def get_chatter_template() -> str:
|
|
102
|
+
"""获取 Chatter 组件模板"""
|
|
103
|
+
return CHATTER_TEMPLATE
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Event Handler 组件模板
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
EVENT_HANDLER_TEMPLATE = '''"""
|
|
6
|
+
{description}
|
|
7
|
+
|
|
8
|
+
Created by: {author}
|
|
9
|
+
Created at: {date}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from src.common.logger import get_logger
|
|
13
|
+
from src.plugin_system import BaseEventHandler, HandlerResult
|
|
14
|
+
|
|
15
|
+
logger = get_logger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class {class_name}(BaseEventHandler):
|
|
19
|
+
"""
|
|
20
|
+
{description}
|
|
21
|
+
|
|
22
|
+
Event Handler 组件用于处理系统事件。
|
|
23
|
+
|
|
24
|
+
处理的事件类型: {event_type}
|
|
25
|
+
|
|
26
|
+
使用场景:
|
|
27
|
+
- 监听消息事件
|
|
28
|
+
- 监听系统事件
|
|
29
|
+
- 实现事件驱动逻辑
|
|
30
|
+
- 在特定事件发生时执行操作
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
# Event Handler 元数据
|
|
34
|
+
handler_name: str = "{class_name}"
|
|
35
|
+
event_types: list[str] = ["{event_type}"] # 监听的事件类型列表
|
|
36
|
+
weight: int = 100 # 权重:0-1000,数字越大优先级越高
|
|
37
|
+
|
|
38
|
+
async def execute(self, params: dict) -> HandlerResult:
|
|
39
|
+
"""
|
|
40
|
+
处理事件
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
params: 事件参数字典,包含事件相关的所有信息
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
HandlerResult: 处理结果
|
|
47
|
+
- success: 是否成功
|
|
48
|
+
- continue_process: 是否继续处理后续 handler
|
|
49
|
+
- message: 返回消息
|
|
50
|
+
"""
|
|
51
|
+
try:
|
|
52
|
+
logger.info(f"处理事件: {{self.handler_name}}")
|
|
53
|
+
logger.debug(f"事件参数: {{params}}")
|
|
54
|
+
|
|
55
|
+
# 检查是否应该处理此事件
|
|
56
|
+
if not self._should_handle(params):
|
|
57
|
+
logger.debug("跳过此事件")
|
|
58
|
+
return HandlerResult(
|
|
59
|
+
success=True,
|
|
60
|
+
continue_process=True,
|
|
61
|
+
message="已跳过",
|
|
62
|
+
handler_name=self.handler_name
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
# TODO: 实现事件处理逻辑
|
|
66
|
+
result = await self._process_event(params)
|
|
67
|
+
|
|
68
|
+
logger.info("事件处理完成")
|
|
69
|
+
return HandlerResult(
|
|
70
|
+
success=True,
|
|
71
|
+
continue_process=True, # 设为 False 可以阻止后续 handler 执行
|
|
72
|
+
message=result,
|
|
73
|
+
handler_name=self.handler_name
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
except Exception as e:
|
|
77
|
+
logger.error(f"事件处理失败: {{e}}")
|
|
78
|
+
return HandlerResult(
|
|
79
|
+
success=False,
|
|
80
|
+
continue_process=True,
|
|
81
|
+
message=str(e),
|
|
82
|
+
handler_name=self.handler_name
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def _should_handle(self, params: dict) -> bool:
|
|
86
|
+
"""
|
|
87
|
+
判断是否应该处理该事件
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
params: 事件参数
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
是否处理
|
|
94
|
+
"""
|
|
95
|
+
# TODO: 实现判断逻辑
|
|
96
|
+
# 示例: 检查事件类型、来源、条件等
|
|
97
|
+
return True
|
|
98
|
+
|
|
99
|
+
async def _process_event(self, params: dict) -> str:
|
|
100
|
+
"""
|
|
101
|
+
处理事件的具体逻辑
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
params: 事件参数
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
处理结果消息
|
|
108
|
+
"""
|
|
109
|
+
# TODO: 实现具体的事件处理逻辑
|
|
110
|
+
return "处理成功"
|
|
111
|
+
'''
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def get_event_handler_template() -> str:
|
|
115
|
+
"""获取 Event Handler 组件模板"""
|
|
116
|
+
return EVENT_HANDLER_TEMPLATE
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""
|
|
2
|
+
PlusCommand 组件模板
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
PLUS_COMMAND_TEMPLATE = '''"""
|
|
6
|
+
{description}
|
|
7
|
+
|
|
8
|
+
Created by: {author}
|
|
9
|
+
Created at: {date}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from src.common.logger import get_logger
|
|
13
|
+
from src.plugin_system import BasePlusCommand
|
|
14
|
+
|
|
15
|
+
logger = get_logger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class {class_name}(BasePlusCommand):
|
|
19
|
+
"""
|
|
20
|
+
{description}
|
|
21
|
+
|
|
22
|
+
PlusCommand 是增强型命令,支持:
|
|
23
|
+
- 复杂的参数解析
|
|
24
|
+
- 子命令系统
|
|
25
|
+
- 权限检查
|
|
26
|
+
- 更丰富的交互方式
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
# PlusCommand 元数据
|
|
30
|
+
command_name: str = "{command_name}"
|
|
31
|
+
command_description: str = "{description}"
|
|
32
|
+
command_aliases: list[str] = [] # 命令别名
|
|
33
|
+
usage: str = "{command_name} [子命令] [选项]"
|
|
34
|
+
|
|
35
|
+
async def execute(self, **kwargs) -> tuple[bool, str]:
|
|
36
|
+
"""
|
|
37
|
+
执行命令
|
|
38
|
+
|
|
39
|
+
可以访问的参数:
|
|
40
|
+
- self.stream_context: 聊天流上下文
|
|
41
|
+
- self.raw_text: 原始命令文本
|
|
42
|
+
- self.command_args: 解析后的命令参数
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
(是否成功, 结果消息)
|
|
46
|
+
"""
|
|
47
|
+
try:
|
|
48
|
+
logger.info(f"执行 PlusCommand: {{self.command_name}}")
|
|
49
|
+
|
|
50
|
+
# 获取命令参数
|
|
51
|
+
args = self.command_args if hasattr(self, "command_args") else []
|
|
52
|
+
|
|
53
|
+
if not args:
|
|
54
|
+
return True, self._help_message()
|
|
55
|
+
|
|
56
|
+
subcommand = args[0]
|
|
57
|
+
subcommand_args = args[1:] if len(args) > 1 else []
|
|
58
|
+
|
|
59
|
+
# 执行子命令
|
|
60
|
+
if subcommand == "list":
|
|
61
|
+
return await self._list_command(subcommand_args)
|
|
62
|
+
elif subcommand == "add":
|
|
63
|
+
return await self._add_command(subcommand_args)
|
|
64
|
+
elif subcommand == "remove":
|
|
65
|
+
return await self._remove_command(subcommand_args)
|
|
66
|
+
elif subcommand == "help":
|
|
67
|
+
return True, self._help_message()
|
|
68
|
+
else:
|
|
69
|
+
return False, f"未知子命令: {{subcommand}}\\n{{self._help_message()}}"
|
|
70
|
+
|
|
71
|
+
except Exception as e:
|
|
72
|
+
logger.error(f"命令执行失败: {{e}}")
|
|
73
|
+
return False, f"执行失败: {{e}}"
|
|
74
|
+
|
|
75
|
+
async def _list_command(self, args: list[str]) -> tuple[bool, str]:
|
|
76
|
+
"""
|
|
77
|
+
列表子命令
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
args: 参数列表
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
(是否成功, 结果消息)
|
|
84
|
+
"""
|
|
85
|
+
# TODO: 实现列表功能
|
|
86
|
+
return True, "列表功能"
|
|
87
|
+
|
|
88
|
+
async def _add_command(self, args: list[str]) -> tuple[bool, str]:
|
|
89
|
+
"""
|
|
90
|
+
添加子命令
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
args: 参数列表
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
(是否成功, 结果消息)
|
|
97
|
+
"""
|
|
98
|
+
if not args:
|
|
99
|
+
return False, f"用法: {{self.command_name}} add <项目>"
|
|
100
|
+
|
|
101
|
+
item = " ".join(args)
|
|
102
|
+
# TODO: 实现添加功能
|
|
103
|
+
return True, f"已添加: {{item}}"
|
|
104
|
+
|
|
105
|
+
async def _remove_command(self, args: list[str]) -> tuple[bool, str]:
|
|
106
|
+
"""
|
|
107
|
+
删除子命令
|
|
108
|
+
|
|
109
|
+
Args:
|
|
110
|
+
args: 参数列表
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
(是否成功, 结果消息)
|
|
114
|
+
"""
|
|
115
|
+
if not args:
|
|
116
|
+
return False, f"用法: {{self.command_name}} remove <项目>"
|
|
117
|
+
|
|
118
|
+
item = " ".join(args)
|
|
119
|
+
# TODO: 实现删除功能
|
|
120
|
+
return True, f"已删除: {{item}}"
|
|
121
|
+
|
|
122
|
+
def _help_message(self) -> str:
|
|
123
|
+
"""
|
|
124
|
+
生成帮助信息
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
帮助信息文本
|
|
128
|
+
"""
|
|
129
|
+
return f"""
|
|
130
|
+
命令: {{self.command_name}}
|
|
131
|
+
描述: {{self.command_description}}
|
|
132
|
+
用法: {{self.usage}}
|
|
133
|
+
|
|
134
|
+
子命令:
|
|
135
|
+
list 列出所有项目
|
|
136
|
+
add 添加新项目
|
|
137
|
+
remove 删除项目
|
|
138
|
+
help 显示此帮助信息
|
|
139
|
+
|
|
140
|
+
示例:
|
|
141
|
+
{{self.command_name}} list
|
|
142
|
+
{{self.command_name}} add item1
|
|
143
|
+
{{self.command_name}} remove item1
|
|
144
|
+
"""
|
|
145
|
+
'''
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def get_plus_command_template() -> str:
|
|
149
|
+
"""获取 PlusCommand 组件模板"""
|
|
150
|
+
return PLUS_COMMAND_TEMPLATE
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Prompt 组件模板
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
PROMPT_TEMPLATE = '''"""
|
|
6
|
+
{description}
|
|
7
|
+
|
|
8
|
+
Created by: {author}
|
|
9
|
+
Created at: {date}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from src.chat.utils.prompt_params import PromptParameters
|
|
13
|
+
from src.common.logger import get_logger
|
|
14
|
+
from src.plugin_system.base.component_types import InjectionRule
|
|
15
|
+
logger = get_logger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class {class_name}(BasePrompt):
|
|
19
|
+
"""
|
|
20
|
+
{description}
|
|
21
|
+
|
|
22
|
+
Prompt 组件用于向核心 Prompt 模板注入额外的上下文信息。
|
|
23
|
+
|
|
24
|
+
使用场景:
|
|
25
|
+
- 向系统提示词添加自定义指令
|
|
26
|
+
- 注入动态上下文信息
|
|
27
|
+
- 添加角色设定或行为规则
|
|
28
|
+
- 提供额外的背景知识
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
# Prompt 组件元数据
|
|
32
|
+
prompt_name: str = "{prompt_name}"
|
|
33
|
+
prompt_description: str = "{description}"
|
|
34
|
+
|
|
35
|
+
# 定义注入规则:指定要注入到哪个核心 Prompt,以什么方式注入
|
|
36
|
+
injection_rules = [
|
|
37
|
+
InjectionRule(
|
|
38
|
+
target_prompt="planner_prompt", # 目标 Prompt 名称
|
|
39
|
+
injection_type=InjectionType.APPEND, # 注入方式:APPEND(追加) 或 PREPEND(前置)
|
|
40
|
+
priority=50 # 优先级:0-100,数字越大优先级越高
|
|
41
|
+
)
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
async def execute(self) -> str:
|
|
45
|
+
"""
|
|
46
|
+
生成要注入的 Prompt 内容
|
|
47
|
+
|
|
48
|
+
可以访问 self.params 来获取上下文信息:
|
|
49
|
+
- self.params.user_id: 用户ID
|
|
50
|
+
- self.params.user_name: 用户名
|
|
51
|
+
- self.params.bot_name: 机器人名称
|
|
52
|
+
- self.params.recent_messages: 最近的消息列表
|
|
53
|
+
- self.params.chat_type: 聊天类型(私聊/群聊)
|
|
54
|
+
- 等等...
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
要注入的文本内容
|
|
58
|
+
"""
|
|
59
|
+
try:
|
|
60
|
+
logger.info(f"生成 Prompt: {{self.prompt_name}}")
|
|
61
|
+
|
|
62
|
+
# TODO: 根据 self.params 构建要注入的内容
|
|
63
|
+
# 示例:根据用户信息生成个性化提示词
|
|
64
|
+
user_name = self.params.user_name or "用户"
|
|
65
|
+
|
|
66
|
+
prompt_content = f"""
|
|
67
|
+
# 特殊指令
|
|
68
|
+
|
|
69
|
+
你正在与 {{user_name}} 对话。
|
|
70
|
+
|
|
71
|
+
## 行为规则
|
|
72
|
+
- 保持友好和专业的态度
|
|
73
|
+
- 准确理解用户意图
|
|
74
|
+
- 提供有价值的回答
|
|
75
|
+
|
|
76
|
+
## 额外能力
|
|
77
|
+
- 你可以执行特定的操作
|
|
78
|
+
- 你有访问某些数据的权限
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
logger.debug(f"生成的内容: {{prompt_content[:100]}}...")
|
|
82
|
+
return prompt_content.strip()
|
|
83
|
+
|
|
84
|
+
except Exception as e:
|
|
85
|
+
logger.error(f"生成 Prompt 失败: {{e}}")
|
|
86
|
+
return ""
|
|
87
|
+
'''
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def get_prompt_template() -> str:
|
|
91
|
+
"""获取 Prompt 组件模板"""
|
|
92
|
+
return PROMPT_TEMPLATE
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Router 组件模板
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
ROUTER_TEMPLATE = '''"""
|
|
6
|
+
{description}
|
|
7
|
+
|
|
8
|
+
Created by: {author}
|
|
9
|
+
Created at: {date}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from fastapi import APIRouter, HTTPException
|
|
13
|
+
from src.common.logger import get_logger
|
|
14
|
+
from src.plugin_system import BaseRouterComponent
|
|
15
|
+
|
|
16
|
+
logger = get_logger(__name__)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class {class_name}(BaseRouterComponent):
|
|
20
|
+
"""
|
|
21
|
+
{description}
|
|
22
|
+
|
|
23
|
+
Router 组件用于对外暴露 HTTP 接口。
|
|
24
|
+
|
|
25
|
+
使用场景:
|
|
26
|
+
- 提供 RESTful API
|
|
27
|
+
- Webhook 接收端点
|
|
28
|
+
- 自定义 HTTP 服务
|
|
29
|
+
- 与外部系统集成
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
# Router 元数据
|
|
33
|
+
component_name: str = "{router_name}"
|
|
34
|
+
component_description: str = "{description}"
|
|
35
|
+
component_version: str = "1.0.0"
|
|
36
|
+
|
|
37
|
+
def register_endpoints(self) -> None:
|
|
38
|
+
"""
|
|
39
|
+
注册 HTTP 端点
|
|
40
|
+
|
|
41
|
+
使用 self.router 来添加路由:
|
|
42
|
+
- @self.router.get("/path")
|
|
43
|
+
- @self.router.post("/path")
|
|
44
|
+
- @self.router.put("/path")
|
|
45
|
+
- @self.router.delete("/path")
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
@self.router.get("/hello")
|
|
49
|
+
async def hello():
|
|
50
|
+
"""
|
|
51
|
+
示例 GET 端点
|
|
52
|
+
"""
|
|
53
|
+
return {{"message": "Hello from {{self.component_name}}"}}
|
|
54
|
+
|
|
55
|
+
@self.router.get("/status")
|
|
56
|
+
async def get_status():
|
|
57
|
+
"""
|
|
58
|
+
获取状态
|
|
59
|
+
"""
|
|
60
|
+
try:
|
|
61
|
+
# TODO: 实现状态检查逻辑
|
|
62
|
+
return {{
|
|
63
|
+
"status": "ok",
|
|
64
|
+
"component": self.component_name,
|
|
65
|
+
"version": self.component_version
|
|
66
|
+
}}
|
|
67
|
+
except Exception as e:
|
|
68
|
+
logger.error(f"获取状态失败: {{e}}")
|
|
69
|
+
raise HTTPException(status_code=500, detail=str(e))
|
|
70
|
+
|
|
71
|
+
@self.router.post("/webhook")
|
|
72
|
+
async def webhook(data: dict):
|
|
73
|
+
"""
|
|
74
|
+
Webhook 接收端点
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
data: 接收的数据
|
|
78
|
+
"""
|
|
79
|
+
try:
|
|
80
|
+
logger.info(f"收到 webhook 数据: {{data}}")
|
|
81
|
+
|
|
82
|
+
# TODO: 处理 webhook 数据
|
|
83
|
+
result = await self._process_webhook(data)
|
|
84
|
+
|
|
85
|
+
return {{
|
|
86
|
+
"success": True,
|
|
87
|
+
"result": result
|
|
88
|
+
}}
|
|
89
|
+
except Exception as e:
|
|
90
|
+
logger.error(f"处理 webhook 失败: {{e}}")
|
|
91
|
+
raise HTTPException(status_code=500, detail=str(e))
|
|
92
|
+
|
|
93
|
+
@self.router.get("/data/{{item_id}}")
|
|
94
|
+
async def get_item(item_id: str):
|
|
95
|
+
"""
|
|
96
|
+
获取指定项目
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
item_id: 项目ID
|
|
100
|
+
"""
|
|
101
|
+
try:
|
|
102
|
+
# TODO: 实现获取逻辑
|
|
103
|
+
item = await self._get_item(item_id)
|
|
104
|
+
if not item:
|
|
105
|
+
raise HTTPException(status_code=404, detail="Item not found")
|
|
106
|
+
return item
|
|
107
|
+
except HTTPException:
|
|
108
|
+
raise
|
|
109
|
+
except Exception as e:
|
|
110
|
+
logger.error(f"获取项目失败: {{e}}")
|
|
111
|
+
raise HTTPException(status_code=500, detail=str(e))
|
|
112
|
+
|
|
113
|
+
@self.router.post("/data")
|
|
114
|
+
async def create_item(data: dict):
|
|
115
|
+
"""
|
|
116
|
+
创建新项目
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
data: 项目数据
|
|
120
|
+
"""
|
|
121
|
+
try:
|
|
122
|
+
# TODO: 实现创建逻辑
|
|
123
|
+
result = await self._create_item(data)
|
|
124
|
+
return {{
|
|
125
|
+
"success": True,
|
|
126
|
+
"item_id": result
|
|
127
|
+
}}
|
|
128
|
+
except Exception as e:
|
|
129
|
+
logger.error(f"创建项目失败: {{e}}")
|
|
130
|
+
raise HTTPException(status_code=500, detail=str(e))
|
|
131
|
+
|
|
132
|
+
async def _process_webhook(self, data: dict) -> dict:
|
|
133
|
+
"""
|
|
134
|
+
处理 webhook 数据
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
data: webhook 数据
|
|
138
|
+
|
|
139
|
+
Returns:
|
|
140
|
+
处理结果
|
|
141
|
+
"""
|
|
142
|
+
# TODO: 实现 webhook 处理逻辑
|
|
143
|
+
return {{"processed": True}}
|
|
144
|
+
|
|
145
|
+
async def _get_item(self, item_id: str) -> dict | None:
|
|
146
|
+
"""
|
|
147
|
+
获取项目
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
item_id: 项目ID
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
项目数据或 None
|
|
154
|
+
"""
|
|
155
|
+
# TODO: 实现获取逻辑
|
|
156
|
+
return {{"id": item_id, "name": "示例项目"}}
|
|
157
|
+
|
|
158
|
+
async def _create_item(self, data: dict) -> str:
|
|
159
|
+
"""
|
|
160
|
+
创建项目
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
data: 项目数据
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
新项目ID
|
|
167
|
+
"""
|
|
168
|
+
# TODO: 实现创建逻辑
|
|
169
|
+
return "new_item_id"
|
|
170
|
+
'''
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def get_router_template() -> str:
|
|
174
|
+
"""获取 Router 组件模板"""
|
|
175
|
+
return ROUTER_TEMPLATE
|