Skip to content

Commit 79345f5

Browse files
committed
feat: add label to block input/outputs
1 parent f50eb5c commit 79345f5

File tree

32 files changed

+262
-159
lines changed

32 files changed

+262
-159
lines changed

data/dispatch_rules/rules.yaml

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,39 +69,6 @@
6969
category: system
7070
permission: user
7171
prefix: /help
72-
- rule_id: system_status
73-
name: 系统状态
74-
description: 显示系统状态
75-
type: prefix
76-
priority: 10
77-
workflow_id: system:status
78-
enabled: true
79-
metadata:
80-
category: system
81-
permission: user
82-
prefix: /status
83-
- rule_id: system_admin
84-
name: 管理员命令
85-
description: 管理员命令,需要管理员权限
86-
type: regex
87-
priority: 20
88-
workflow_id: system:admin
89-
enabled: true
90-
metadata:
91-
category: system
92-
permission: admin
93-
pattern: ^/(ban|unban|kick|mute) \S+
94-
- rule_id: system_settings
95-
name: 系统设置
96-
description: 修改系统设置
97-
type: prefix
98-
priority: 10
99-
workflow_id: system:settings
100-
enabled: true
101-
metadata:
102-
category: system
103-
permission: admin
104-
prefix: /settings
10572
- rule_id: system_clear_memory
10673
name: 清空记忆
10774
description: 清空当前对话的记忆
@@ -121,3 +88,13 @@
12188
workflow_id: chat:normal
12289
enabled: true
12390
metadata: {}
91+
- rule_id: asd
92+
name: dsa
93+
description: asd
94+
type: keyword
95+
priority: 5
96+
workflow_id: game:gacha
97+
enabled: true
98+
metadata: {}
99+
keywords:
100+
- asdsd

framework/web/api/block/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class BlockType(BaseModel):
77
"""Block类型信息"""
88
type_name: str
99
name: str
10+
label: str
1011
description: str
1112
inputs: List[BlockInput]
1213
outputs: List[BlockOutput]

framework/web/api/block/routes.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ async def list_block_types():
1717
types = []
1818
for block_type in registry.get_all_types():
1919
inputs, outputs, configs = registry.extract_block_info(block_type)
20-
20+
type_name = registry.get_block_type_name(block_type)
2121
types.append(BlockType(
22-
type_name=registry.get_block_type_name(block_type),
22+
type_name=type_name,
2323
name=block_type.name,
24+
label=registry.get_localized_name(type_name),
2425
description=getattr(block_type, 'description', ''),
2526
inputs=inputs.values(),
2627
outputs=outputs.values(),
2728
configs=configs.values()
2829
))
30+
print(block_type.name, type_name, registry.get_localized_name(block_type.name))
2931

3032
return BlockTypeList(types=types).model_dump()
3133

@@ -44,6 +46,7 @@ async def get_block_type(type_name: str):
4446
for name, info in block_type.get_inputs().items():
4547
inputs.append(BlockInput(
4648
name=name,
49+
label=registry.get_localized_name(name),
4750
description=info.get('description', ''),
4851
type=info.get('type', 'any'),
4952
required=info.get('required', True),
@@ -71,6 +74,7 @@ async def get_block_type(type_name: str):
7174
return BlockTypeResponse(type=BlockType(
7275
type_name=registry.get_block_type_name(block_type),
7376
name=block_type.name,
77+
label=registry.get_localized_name(block_type.name),
7478
description=block_type.description,
7579
inputs=inputs,
7680
outputs=outputs,

framework/workflow/core/block/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def execute(self, **kwargs) -> Dict[str, Any]:
3131
class ConditionBlock(Block):
3232
"""条件判断块"""
3333
name: str = "condition"
34-
outputs: Dict[str, Output] = {"condition_result": Output("condition_result", bool, "Condition result")}
34+
outputs: Dict[str, Output] = {"condition_result": Output("condition_result", "条件结果", bool, "条件结果")}
3535

3636
def __init__(self, condition_func: Callable[[Dict[str, Any]], bool], inputs: Dict[str, 'Input']):
3737
super().__init__()
@@ -46,8 +46,8 @@ class LoopBlock(Block):
4646
"""循环控制块"""
4747
name: str = "loop"
4848
outputs: Dict[str, Output] = {
49-
"should_continue": Output("should_continue", bool, "Continue loop?"),
50-
"iteration": Output("iteration", dict, "Current iteration data")
49+
"should_continue": Output("should_continue", "是否继续", bool, "是否继续"),
50+
"iteration": Output("iteration", "当前迭代数据", dict, "当前迭代数据")
5151
}
5252

5353
def __init__(self,
@@ -74,7 +74,7 @@ def execute(self, **kwargs) -> Dict[str, Any]:
7474
class LoopEndBlock(Block):
7575
"""循环结束块,收集循环结果"""
7676
name: str = "loop_end"
77-
outputs: Dict[str, Output] = {"loop_results": Output("loop_results", list, "Collected loop results")}
77+
outputs: Dict[str, Output] = {"loop_results": Output("loop_results", "收集的循环结果", list, "收集的循环结果")}
7878

7979
def __init__(self, inputs: Dict[str, 'Input']):
8080
super().__init__()

framework/workflow/core/block/input_output.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33

44
class Input:
5-
def __init__(self, name: str, data_type: type, description: str,
6-
nullable: bool = False, default: Optional[Any] = None):
5+
def __init__(self, name: str, label: str, data_type: type, description: str,
6+
nullable: bool = False, default: Optional[Any] = None):
77
self.name = name
8+
self.label = label
89
self.data_type = data_type
910
self.description = description
1011
self.nullable = nullable
@@ -17,8 +18,9 @@ def validate(self, value: Any) -> bool:
1718

1819

1920
class Output:
20-
def __init__(self, name: str, data_type: type, description: str):
21+
def __init__(self, name: str, label: str, data_type: type, description: str):
2122
self.name = name
23+
self.label = label
2224
self.data_type = data_type
2325
self.description = description
2426

framework/workflow/core/block/registry.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,33 @@ class BlockRegistry:
99

1010
def __init__(self):
1111
self._blocks = {}
12+
self._localized_names = {}
1213

13-
def register(self, block_id: str, group_id: str, block_class: Type[Block]):
14+
def register(self, block_id: str, group_id: str, block_class: Type[Block], localized_name: Optional[str] = None):
1415
"""注册一个 block
1516
1617
Args:
1718
block_id: block 的唯一标识
1819
group_id: 组标识(internal 为框架内置)
1920
block_class: block 类
21+
localized_name: 本地化名称
2022
"""
2123
full_name = f"{group_id}:{block_id}"
2224
if full_name in self._blocks:
2325
raise ValueError(f"Block {full_name} already registered")
2426
self._blocks[full_name] = block_class
2527
block_class.id = block_id
28+
if localized_name:
29+
self._localized_names[full_name] = localized_name
2630

2731
def get(self, full_name: str) -> Optional[Type[Block]]:
2832
"""获取已注册的 block 类"""
2933
return self._blocks.get(full_name)
3034

35+
def get_localized_name(self, block_id: str) -> Optional[str]:
36+
"""获取本地化名称"""
37+
return self._localized_names.get(block_id, block_id)
38+
3139
def clear(self):
3240
"""清空注册表"""
3341
self._blocks.clear()
@@ -67,6 +75,7 @@ def extract_block_info(self, block_type: Type[Block]) -> Tuple[Dict[str, BlockIn
6775
for name, input_info in getattr(block_type, 'inputs', {}).items():
6876
inputs[name] = BlockInput(
6977
name=name,
78+
label=input_info.label,
7079
description=input_info.description,
7180
type=input_info.data_type.__name__ if hasattr(input_info.data_type, '__name__') else str(input_info.data_type),
7281
required=True, # 假设所有输入都是必需的
@@ -77,6 +86,7 @@ def extract_block_info(self, block_type: Type[Block]) -> Tuple[Dict[str, BlockIn
7786
for name, output_info in getattr(block_type, 'outputs', {}).items():
7887
outputs[name] = BlockOutput(
7988
name=name,
89+
label=output_info.label,
8090
description=output_info.description,
8191
type=output_info.data_type.__name__ if hasattr(output_info.data_type, '__name__') else str(output_info.data_type)
8292
)

framework/workflow/core/block/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class BlockInput(BaseModel):
77
"""Block输入定义"""
88
name: str
9+
label: str
910
description: str
1011
type: str
1112
required: bool = True
@@ -14,6 +15,7 @@ class BlockInput(BaseModel):
1415
class BlockOutput(BaseModel):
1516
"""Block输出定义"""
1617
name: str
18+
label: str
1719
description: str
1820
type: str
1921

framework/workflow/core/workflow/builder.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,24 @@ def end_loop(self) -> 'WorkflowBuilder':
297297
return self
298298

299299
def _connect_blocks(self, source_block: Block, target_block: Block):
300-
"""连接两个块,自动处理输入输出匹配"""
301-
for output_name, output in source_block.outputs.items():
302-
for input_name, input in target_block.inputs.items():
300+
"""连接两个块,自动处理输入输出匹配,只连接一次"""
301+
is_connected = False
302+
for input_name, input in target_block.inputs.items():
303+
for output_name, output in source_block.outputs.items():
303304
# 检查数据类型是否匹配
304305
if output.data_type == input.data_type:
305306
# 创建新的连线
306307
wire = Wire(source_block, output_name, target_block, input_name)
307-
# 检查是否已存在相同的连线
308-
if not any(w.source_block == wire.source_block and
309-
w.target_block == wire.target_block and
310-
w.source_output == wire.source_output and
311-
w.target_input == wire.target_input
312-
for w in self.wires):
308+
# 跳过已被连接的 input
309+
if not any(w.target_block == wire.target_block and
310+
w.target_input == wire.target_input
311+
for w in self.wires):
312+
is_connected = True
313313
self.wires.append(wire)
314+
break
315+
# 如果连接成功,则跳出循环
316+
if is_connected:
317+
break
314318

315319
def _find_parallel_nodes(self, start_node: Node) -> List[Node]:
316320
"""查找所有并行节点"""

framework/workflow/implementations/blocks/game/dice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class DiceRoll(Block):
1313
"""骰子掷点 block"""
1414
name = "dice_roll"
1515
inputs = {
16-
"message": Input("message", IMMessage, "Input message containing dice command")
16+
"message": Input("message", "输入消息", IMMessage, "输入消息包含骰子命令")
1717
}
1818
outputs = {
19-
"response": Output("response", IMMessage, "Response message with dice roll result")
19+
"response": Output("response", "响应消息", IMMessage, "响应消息包含骰子掷点结果")
2020
}
2121

2222
def execute(self, message: IMMessage) -> Dict[str, Any]:

framework/workflow/implementations/blocks/game/gacha.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class GachaSimulator(Block):
1010
"""抽卡模拟器 block"""
1111
name = "gacha_simulator"
1212
inputs = {
13-
"message": Input("message", IMMessage, "Input message containing gacha command")
13+
"message": Input("message", "输入消息", IMMessage, "输入消息包含抽卡命令")
1414
}
1515
outputs = {
16-
"response": Output("response", IMMessage, "Response message with gacha results")
16+
"response": Output("response", "响应消息", IMMessage, "响应消息包含抽卡结果")
1717
}
1818

1919
def __init__(self, rates: Dict[str, float] = None):

0 commit comments

Comments
 (0)