From 2233aa840323490a70a784be62f8068167e30031 Mon Sep 17 00:00:00 2001 From: "jinli.yl" Date: Sun, 1 Feb 2026 15:25:49 +0800 Subject: [PATCH] feat(workflow): add translate_ts operation and rename test module to gallery --- reme/workflow/__init__.py | 4 +- reme/workflow/gallery/__init__.py | 14 ++++++ reme/workflow/{test => gallery}/test_op.py | 0 reme/workflow/gallery/translate_ts.py | 56 ++++++++++++++++++++++ reme/workflow/gallery/translate_ts.yaml | 16 +++++++ reme/workflow/test/__init__.py | 10 ---- 6 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 reme/workflow/gallery/__init__.py rename reme/workflow/{test => gallery}/test_op.py (100%) create mode 100644 reme/workflow/gallery/translate_ts.py create mode 100644 reme/workflow/gallery/translate_ts.yaml delete mode 100644 reme/workflow/test/__init__.py diff --git a/reme/workflow/__init__.py b/reme/workflow/__init__.py index bf4ed2d9..485fda51 100644 --- a/reme/workflow/__init__.py +++ b/reme/workflow/__init__.py @@ -1,9 +1,9 @@ """workflow""" -from . import test +from . import gallery from . import procedural_memory __all__ = [ - "test", + "gallery", "procedural_memory", ] diff --git a/reme/workflow/gallery/__init__.py b/reme/workflow/gallery/__init__.py new file mode 100644 index 00000000..8d18015c --- /dev/null +++ b/reme/workflow/gallery/__init__.py @@ -0,0 +1,14 @@ +"""test""" + +from .test_op import TestOp +from .translate_ts import TranslateTs +from ...core import R + +__all__ = [ + "TestOp", + "TranslateTs", +] + +for name in __all__: + agent_class = globals()[name] + R.op.register(agent_class) diff --git a/reme/workflow/test/test_op.py b/reme/workflow/gallery/test_op.py similarity index 100% rename from reme/workflow/test/test_op.py rename to reme/workflow/gallery/test_op.py diff --git a/reme/workflow/gallery/translate_ts.py b/reme/workflow/gallery/translate_ts.py new file mode 100644 index 00000000..6a454afc --- /dev/null +++ b/reme/workflow/gallery/translate_ts.py @@ -0,0 +1,56 @@ +"""Translate operation for translating text.""" + +from pathlib import Path + +from loguru import logger + +from ...core.enumeration import Role +from ...core.op import BaseOp +from ...core.schema import Message + + +class TranslateTs(BaseOp): + """ + Translate operation for translating text. + + reme2 backend=cmd cmd.flow="TranslateTs()" cmd.params.target_dir="" + """ + + async def execute_single_file(self, ts_file: Path): + """Translate a single ts file.""" + ts_code = ts_file.read_text(encoding="utf-8") + python_file = ts_file.with_suffix(".py") + + logger.info(f"Translating {ts_file}") + + def parse_python(assistant_message: Message): + python_code = assistant_message.content + assert "```python" in python_code, "Invalid python code" + python_code = python_code.split("```python", 1)[1] + python_code_split = python_code.split("```") + python_code = "```".join(python_code_split[:-1]) + return python_code.strip() + + output_code = await self.llm.chat( + messages=[ + Message( + role=Role.USER, + content=self.prompt_format(prompt_name="translate_prompt", ts_code=ts_code), + ), + ], + callback_fn=parse_python, + ) + + python_file.write_text(output_code, encoding="utf-8") + logger.info(f"Writing {python_file}") + + async def execute(self): + """Execute the operation.""" + target_dir = Path(self.context.target_dir) + ts_files = [p for p in target_dir.rglob("*.ts") if p.is_file() and not p.name.endswith(".test.ts")] + logger.info(f"Translating {target_dir}, finding {len(ts_files)} ts files") + + for ts_file in ts_files: + self.submit_async_task(self.execute_single_file, ts_file) + + await self.join_async_tasks() diff --git a/reme/workflow/gallery/translate_ts.yaml b/reme/workflow/gallery/translate_ts.yaml new file mode 100644 index 00000000..6f99e4d4 --- /dev/null +++ b/reme/workflow/gallery/translate_ts.yaml @@ -0,0 +1,16 @@ +translate_prompt: | + {ts_code} + + 请将上方提供的 TypeScript(TS)代码转换为功能等价的 Python 代码,并满足以下要求: + + 1. 在代码最开始,通过注释简要说明你将如何进行代码转换,包括对 TS 特性的处理策略、Python 替代方案的选择依据等。 + 2. 接着使用注释形式提供一个整体大纲总结,简明扼要地说明该程序的核心逻辑、主要模块或关键步骤。 + 3. 为所有关键代码行添加丰富的中文注释,解释其作用、设计意图或与原 TS 代码的对应关系。 + 4. 保持代码结构清晰、符合 Python 编程规范(如 PEP 8),并尽可能使用 Python 的惯用写法(idiomatic Python)。 + 5. 若 TS 代码中使用了特定类型、接口或高级特性(如泛型、装饰器、Promise 等),请在 Python 中采用合理的方式模拟或替代(例如使用类型提示、async/await、dataclass 等)。 + + 输出格式 + ```python + + ``` + 只输出代码,不要进行额外的总结 \ No newline at end of file diff --git a/reme/workflow/test/__init__.py b/reme/workflow/test/__init__.py deleted file mode 100644 index 15135455..00000000 --- a/reme/workflow/test/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -"""test""" - -from .test_op import TestOp -from ...core import R - -__all__ = [ - "TestOp", -] - -R.op.register(TestOp)