Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion community/github/command.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
description: Root of github commands.
description: Workflows for github.
steps:
- run: $devchat_python $command_path/main.py
2 changes: 1 addition & 1 deletion community/github/commit/command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ help:
en: README.md
zh: README.zh.md
steps:
- run: $devchat_python $command_path/commit.py "$input" "english"
- run: $devchat_python $command_path/main.py "$input" "english"
File renamed without changes.
8 changes: 0 additions & 8 deletions community/github/commit/zh/command.yml

This file was deleted.

13 changes: 13 additions & 0 deletions community/github/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from lib.workflow.call import print_sub_workflows, workflow_call
from lib.workflow.decorators import check_config


@check_config(["github_token"])
def main(github_token: bool):
print_sub_workflows()
if not github_token:
workflow_call("/github.config")


if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion community/gitlab/command.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
description: Root of gitlab commands.
description: Workflow for gitlab.
steps:
- run: $devchat_python $command_path/main.py
File renamed without changes.
2 changes: 1 addition & 1 deletion community/gitlab/commit/command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ help:
en: README.md
zh: README.zh.md
steps:
- run: $devchat_python $command_path/commit.py "$input" "english"
- run: $devchat_python $command_path/main.py "$input" "english"
File renamed without changes.
5 changes: 0 additions & 5 deletions community/gitlab/commit/zh/command.yml

This file was deleted.

13 changes: 13 additions & 0 deletions community/gitlab/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from lib.workflow.call import print_sub_workflows, workflow_call
from lib.workflow.decorators import check_config


@check_config(["gitlab_token"])
def main(gitlab_token: bool):
print_sub_workflows()
if not gitlab_token:
workflow_call("/gitlab.config")


if __name__ == "__main__":
main()
21 changes: 8 additions & 13 deletions community/refactor/api/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from devchat.llm import chat_json

from lib.chatmark import Button, Form, TextEditor
from lib.ide_service import IDEService
from lib.workflow import workflow_call
from lib.workflow.decorators import check_select_code

# 步骤3: 使用AI识别API路径和METHOD的提示词
API_ANALYSIS_PROMPT = """
Expand Down Expand Up @@ -37,28 +37,23 @@ def analyze_api(code: str) -> Dict[str, str]:
pass


def main() -> None:
@check_select_code("Please select code to refactor.")
def main(code: dict) -> None:
"""API重构工作流主函数"""
try:
# 步骤1: 获取用户输入的重构目标
if len(sys.argv) < 2:
print("错误: 请提供重构目标")
print("请提供重构目标", file=sys.stderr)
sys.exit(1)

refactor_target = sys.argv[1]

# 步骤2: 获取用户选中的代码
selected_code = IDEService().get_selected_range()
if not selected_code or not selected_code.text.strip():
print("错误: 请先选择需要重构的代码")
sys.exit(1)

# 步骤3: 使用AI识别API路径和METHOD
print("正在分析选中代码中的API信息...")
api_info = analyze_api(code=selected_code.text)
api_info = analyze_api(code=code["text"])

if not api_info or "api_path" not in api_info or "method" not in api_info:
print("错误: 无法识别API信息")
print("无法识别API信息", file=sys.stderr)
sys.exit(1)

api_path = api_info["api_path"]
Expand Down Expand Up @@ -91,7 +86,7 @@ def main() -> None:
refactor_result = workflow_call(f"/refactor {refactor_target}")

if refactor_result != 0:
print("错误: API重构失败")
print("API重构失败")
sys.exit(1)

print("API重构成功!")
Expand Down Expand Up @@ -124,7 +119,7 @@ def main() -> None:
print("API重构工作流执行完毕!")

except Exception as e:
print(f"错误: 执行过程中发生异常: {str(e)}")
print(f"执行过程中发生异常: {str(e)}", file=sys.stderr)
sys.exit(1)


Expand Down
21 changes: 21 additions & 0 deletions lib/workflow/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import yaml

import __main__


def find_workflow_script(command_name: str) -> str:
"""
Expand Down Expand Up @@ -147,3 +149,22 @@ def workflow_call(command: str) -> int:
print(f"命令执行失败,返回码: {return_code}")

return return_code


def print_sub_workflows():
base_dir = os.path.dirname(os.path.abspath(__main__.__file__))
root_dir = base_dir.split("/")[-1]
print(f"#### {root_dir.capitalize()} Workflows\n", flush=True)

for root, dirs, files in os.walk(base_dir):
if "command.yml" in files:
if root == base_dir:
continue
rel_path = os.path.relpath(root, base_dir)
workflow_name = f"/{root_dir}.{rel_path.replace(os.path.sep, '.')}"
if workflow_name.endswith(".zh"):
continue
with open(os.path.join(root, "command.yml"), "r", encoding="utf-8") as f:
config = yaml.safe_load(f)
description = config.get("description", "No description available")
print(f"- **{workflow_name}**: {description}", flush=True)
66 changes: 66 additions & 0 deletions lib/workflow/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import functools
import json
import os
import sys

from lib.ide_service import IDEService


def check_config(configs: list[str], is_global: bool = True):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
config_file = ".workflow_config.json"
if is_global:
config_path = os.path.join(os.path.expanduser("~/.chat"), config_file)
else:
config_path = os.path.join(os.getcwd(), ".chat", config_file)

if not os.path.exists(config_path):
return func(False, *args, **kwargs)

with open(config_path, "r") as f:
config = json.load(f)

for config_name in configs:
if config_name not in config:
return func(False, *args, **kwargs)

return func(True, *args, **kwargs)

return wrapper

return decorator


def check_select_code(description: str):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
selected_data = IDEService().get_selected_range().dict()
if (
selected_data["range"]["start"]["line"] == -1
or selected_data["range"]["start"] == selected_data["range"]["end"]
):
print(description, file=sys.stderr)
sys.exit(1)
return func(selected_data, *args, **kwargs)

return wrapper

return decorator


def check_input(description: str):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
arg = sys.argv[1]
if len(arg) == 0:
print(description, file=sys.stderr)
sys.exit(1)
return func(arg, *args, **kwargs)

return wrapper

return decorator
2 changes: 1 addition & 1 deletion merico/ask_issue/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### ask_issue

Automatically fix lint errors in code.
Ask issue for lint errors.

#### Purpose

Expand Down
2 changes: 1 addition & 1 deletion merico/ask_issue/README.zh.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### ask_issue

自动修复代码中的 lint 错误。
询问代码中的 lint 错误。

#### 用途

Expand Down
2 changes: 1 addition & 1 deletion merico/ask_issue/command.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description: Automatically fix lint errors.
description: Ask issue for lint errors.
help:
en: README.md
zh: README.zh.md
Expand Down
19 changes: 7 additions & 12 deletions merico/chatflow/ask/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from devchat.llm import chat

from lib.ide_service import IDEService
from lib.workflow.decorators import check_input, check_select_code

ROOT_WORKFLOW_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(ROOT_WORKFLOW_DIR)
Expand All @@ -28,20 +28,15 @@ def ask(question, selected_code, file_path):
pass


def get_selected_code():
"""Retrieves the selected lines of code from the user's selection."""
selected_data = IDEService().get_selected_range().dict()
return selected_data


def main(question):
selected_text = get_selected_code()
file_path = selected_text.get("abspath", "")
code_text = selected_text.get("text", "")
@check_input("请输入问题")
@check_select_code("请选中代码")
def main(code, question):
file_path = code.get("abspath", "")
code_text = code.get("text", "")

ask(question=question, selected_code=code_text, file_path=file_path)
sys.exit(0)


if __name__ == "__main__":
main(sys.argv[1])
main()
7 changes: 3 additions & 4 deletions merico/chatflow/gen/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from lib.chatmark import Form, Radio, Step, TextEditor
from lib.ide_service import IDEService
from lib.workflow.decorators import check_input

ROOT_WORKFLOW_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(ROOT_WORKFLOW_DIR)
Expand Down Expand Up @@ -323,10 +324,8 @@ def create_workflow_files(command_dir, command_name, description, input_required
f.write(readme_content)


def main():
# 获取用户输入
user_input = sys.argv[1] if len(sys.argv) > 1 else ""

@check_input("请输入工作流描述")
def main(user_input):
# 步骤1: 通过AI分析用户输入,提取必要信息
with Step("分析用户输入,提取工作流信息..."):
workflow_info = extract_workflow_info(user_input=user_input, contexts=CONTEXTS)
Expand Down
21 changes: 3 additions & 18 deletions merico/comments/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import re
import sys

from devchat.llm import chat
from devchat.memory import FixSizeChatMemory

from lib.ide_service import IDEService
from lib.workflow.decorators import check_select_code

PROMPT = """
file: {file_path}
Expand Down Expand Up @@ -194,21 +194,6 @@
]


def get_selected_code():
"""Retrieves the selected lines of code from the user's selection."""
selected_data = IDEService().get_selected_range().dict()
if selected_data["range"]["start"] == selected_data["range"]["end"]:
readme_path = os.path.join(os.path.dirname(__file__), "README.md")
if os.path.exists(readme_path):
with open(readme_path, "r", encoding="utf-8") as f:
readme_text = f.read()
print(readme_text)
sys.exit(0)
print("Please select some text.", file=sys.stderr, flush=True)
sys.exit(-1)
return selected_data


memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_FEW_SHOT)


Expand Down Expand Up @@ -253,8 +238,8 @@ def remove_unnecessary_escapes(code_a, code_b):
return code_copy


def main():
selected_text = get_selected_code()
@check_select_code("Please select code to add comments.")
def main(selected_text: dict):
file_path = selected_text.get("abspath", "")
code_text = selected_text.get("text", "")

Expand Down
Loading