feat(workflow): add translate_ts operation and rename test module to gallery

This commit is contained in:
jinli.yl 2026-02-01 15:25:49 +08:00
parent ad8b889461
commit 2233aa8403
6 changed files with 88 additions and 12 deletions

View file

@ -1,9 +1,9 @@
"""workflow"""
from . import test
from . import gallery
from . import procedural_memory
__all__ = [
"test",
"gallery",
"procedural_memory",
]

View file

@ -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)

View file

@ -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()

View file

@ -0,0 +1,16 @@
translate_prompt: |
{ts_code}
请将上方提供的 TypeScriptTS代码转换为功能等价的 Python 代码,并满足以下要求:
1. 在代码最开始,通过注释简要说明你将如何进行代码转换,包括对 TS 特性的处理策略、Python 替代方案的选择依据等。
2. 接着使用注释形式提供一个整体大纲总结,简明扼要地说明该程序的核心逻辑、主要模块或关键步骤。
3. 为所有关键代码行添加丰富的中文注释,解释其作用、设计意图或与原 TS 代码的对应关系。
4. 保持代码结构清晰、符合 Python 编程规范(如 PEP 8并尽可能使用 Python 的惯用写法idiomatic Python
5. 若 TS 代码中使用了特定类型、接口或高级特性如泛型、装饰器、Promise 等),请在 Python 中采用合理的方式模拟或替代例如使用类型提示、async/await、dataclass 等)。
输出格式
```python
```
只输出代码,不要进行额外的总结

View file

@ -1,10 +0,0 @@
"""test"""
from .test_op import TestOp
from ...core import R
__all__ = [
"TestOp",
]
R.op.register(TestOp)