Skip to content

Commit 6508adb

Browse files
committed
feat: Add text manipulation blocks for workflow system
1 parent 2e2c1fc commit 6508adb

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

framework/workflow/implementations/blocks/system/basic.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ class TextReplaceBlock(Block):
3636
name = "text_replace_block"
3737
inputs = {
3838
"text": Input("text", "原始文本", str, "原始文本"),
39-
"variable": Input("variable", "被替换的文本", Any, "被替换的文本"),
39+
"new_text": Input("new_text", "新文本", Any, "新文本"),
4040
}
4141
outputs = {"text": Output("text", "替换后的文本", str, "替换后的文本")}
4242

4343
def __init__(
44-
self, name: Annotated[str, ParamMeta(label="变量名称", description="变量名称")]
44+
self, variable: Annotated[str, ParamMeta(label="被替换的文本", description="被替换的文本")]
4545
):
46-
self.name = name
46+
self.variable = variable
4747

48-
def execute(self) -> Dict[str, Any]:
48+
def execute(self, text: str, new_text: Any) -> Dict[str, Any]:
4949
return {
50-
"text": self.text.value.replace(self.variable.value, str(self.variable))
50+
"text": text.replace(self.variable, str(new_text))
5151
}
5252

5353

@@ -56,6 +56,10 @@ class TextExtractByRegexBlock(Block):
5656
name = "text_extract_by_regex_block"
5757
inputs = {"text": Input("text", "原始文本", str, "原始文本")}
5858
outputs = {"text": Output("text", "提取后的文本", str, "提取后的文本")}
59+
def __init__(
60+
self, regex: Annotated[str, ParamMeta(label="正则表达式", description="正则表达式")]
61+
):
62+
self.regex = regex
5963

6064
def execute(self) -> Dict[str, Any]:
6165
# 使用正则表达式提取文本
@@ -65,3 +69,4 @@ def execute(self) -> Dict[str, Any]:
6569
return {"text": match.group(0)}
6670
else:
6771
return {"text": ""}
72+

framework/workflow/implementations/blocks/system_blocks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from framework.workflow.implementations.blocks.im.basic import ExtractChatSender
33
from framework.workflow.implementations.blocks.llm.image import SimpleStableDiffusionWebUI
44
from framework.workflow.implementations.blocks.memory.clear_memory import ClearMemory
5-
from framework.workflow.implementations.blocks.system.basic import TextBlock
5+
from framework.workflow.implementations.blocks.system.basic import (TextBlock, TextConcatBlock, TextExtractByRegexBlock,
6+
TextReplaceBlock)
67

78
from .game.dice import DiceRoll
89
from .game.gacha import GachaSimulator
@@ -17,6 +18,9 @@ def register_system_blocks(registry: BlockRegistry):
1718
"""注册系统自带的 block"""
1819
# 基础 blocks
1920
registry.register("text_block", "internal", TextBlock, "基础:文本")
21+
registry.register("text_concat_block", "internal", TextConcatBlock, "基础:拼接文本")
22+
registry.register("text_replace_block", "internal", TextReplaceBlock, "基础:替换文本")
23+
registry.register("text_extract_by_regex_block", "internal", TextExtractByRegexBlock, "基础:正则表达式提取文本")
2024

2125
# IM 相关 blocks
2226
registry.register("get_message", "internal", GetIMMessage, "IM: 获取最新消息")

0 commit comments

Comments
 (0)