diff --git a/src/ekb_cli/api/agent.py b/src/ekb_cli/api/agent.py index 84e8167..bf6ca6b 100644 --- a/src/ekb_cli/api/agent.py +++ b/src/ekb_cli/api/agent.py @@ -3,6 +3,12 @@ from ekb_cli.api import EkbClient +def check_agent_name(client: EkbClient, name: str) -> bool: + """检查名称是否可用:True=可用,False=已存在""" + resp = client.get("/engine/hab/agent/checkName", name=name) + return resp.get("data", True) + + def list_agents(client: EkbClient, page: int = 1, size: int = 1000) -> dict: return client.get("/engine/hab/agent/list", page=page, size=size) diff --git a/src/ekb_cli/commands/agent/__init__.py b/src/ekb_cli/commands/agent/__init__.py index 0302a8e..44e7099 100644 --- a/src/ekb_cli/commands/agent/__init__.py +++ b/src/ekb_cli/commands/agent/__init__.py @@ -5,6 +5,8 @@ from ekb_cli.commands.agent.list import cmd_list from ekb_cli.commands.agent.detail import cmd_detail from ekb_cli.commands.agent.logs import cmd_logs from ekb_cli.commands.agent.update import cmd_update +from ekb_cli.commands.agent.create import cmd_create +from ekb_cli.commands.agent.copy import cmd_copy from ekb_cli.commands.agent.rule_list import cmd_rule_list from ekb_cli.commands.agent.rule_detail import cmd_rule_detail from ekb_cli.commands.agent.rule_create import cmd_rule_create @@ -13,6 +15,8 @@ app = typer.Typer(help="智能审核 Agent 管理") app.command("list")(cmd_list) app.command("get")(cmd_detail) +app.command("create")(cmd_create) +app.command("copy")(cmd_copy) app.command("update")(cmd_update) app.command("logs")(cmd_logs) app.command("rules")(cmd_rule_list) diff --git a/src/ekb_cli/commands/agent/copy.py b/src/ekb_cli/commands/agent/copy.py new file mode 100644 index 0000000..2b1b625 --- /dev/null +++ b/src/ekb_cli/commands/agent/copy.py @@ -0,0 +1,57 @@ +"""ekb agent copy :复制智能审核 Agent""" + +import typer +from rich import print as rprint + +from ekb_cli.api import get_client +from ekb_cli.api.agent import get_agent, check_agent_name, save_agent + +MODE_DISPLAY = {0: "全自动", 1: "半自动", 2: "辅助"} + + +def cmd_copy( + agent_id: str = typer.Argument(..., help="要复制的 Agent ID"), +): + """复制 Agent(新 Agent 名称为原名称加「(副本)」)""" + with get_client() as client: + resp = get_agent(client, agent_id) + src = resp.get("data", {}) + if not src: + rprint("[red]未找到该 Agent[/red]") + raise typer.Exit(1) + + new_name = src.get("name", "") + "(副本)" + + # 校验名称唯一性 + name_ok = check_agent_name(client, new_name) + if not name_ok: + rprint(f"[red]名称「{new_name}」已存在,无法复制[/red]") + raise typer.Exit(1) + + payload = { + "icon": src.get("icon", ""), + "name": new_name, + "description": src.get("description", ""), + "mode": src.get("mode", 2), + "agreeTerms": src.get("agreeTerms", ""), + "rejectTerms": src.get("rejectTerms", ""), + "exemptTerms": src.get("exemptTerms", ""), + "reviewTerms": src.get("reviewTerms", ""), + "state": 1, + "ruleIds": [r["id"] for r in src.get("agentRule", [])], + } + if src.get("mode") == 1: + payload["samplingRate"] = src.get("samplingRate", 0) + + save_resp = save_agent(client, payload) + + if save_resp.get("code") != 200: + rprint(f"[red]复制失败:{save_resp.get('msg', '未知错误')}[/red]") + raise typer.Exit(1) + + data = save_resp.get("data", {}) + rprint(f"[green]✓ Agent 复制成功[/green]") + rprint(f" 原 Agent:[dim]{src.get('name')}[/dim] → 新 Agent:[bold]{data.get('name')}[/bold]") + rprint(f" 新 ID:[dim]{data.get('id')}[/dim]") + rprint(f" 模式:{MODE_DISPLAY.get(data.get('mode'), str(data.get('mode')))}") + rprint(f" 关联规则数:{len(data.get('agentRule', []))}") diff --git a/src/ekb_cli/commands/agent/create.py b/src/ekb_cli/commands/agent/create.py new file mode 100644 index 0000000..261afa3 --- /dev/null +++ b/src/ekb_cli/commands/agent/create.py @@ -0,0 +1,63 @@ +"""ekb agent create:新建智能审核 Agent""" + +from typing import Optional, List +import typer +from rich import print as rprint + +from ekb_cli.api import get_client +from ekb_cli.api.agent import check_agent_name, save_agent + +MODE_NAMES = {"auto": 0, "semi": 1, "assist": 2} +MODE_DISPLAY = {0: "全自动", 1: "半自动", 2: "辅助"} + +DEFAULT_ICON = "https://statics.ekuaibao.com/eui/eui-static/male5.png" + + +def cmd_create( + name: str = typer.Option(..., "--name", "-n", help="Agent 名称(唯一)"), + description: str = typer.Option("", "--description", "-d", help="描述"), + mode: str = typer.Option("assist", "--mode", "-m", help="模式:auto / semi / assist"), + agree_terms: str = typer.Option("", "--agree-terms", help="明确同意条款"), + reject_terms: str = typer.Option("", "--reject-terms", help="明确驳回条款"), + exempt_terms: str = typer.Option("", "--exempt-terms", help="豁免(备注同意)条款"), + review_terms: str = typer.Option("", "--review-terms", help="转人工条款"), + rule_ids: Optional[List[str]] = typer.Option(None, "--rule-id", "-r", help="关联规则 ID(可多次传入)"), + icon: str = typer.Option(DEFAULT_ICON, "--icon", help="头像 URL"), +): + """新建智能审核 Agent""" + if mode not in MODE_NAMES: + rprint(f"[red]mode 必须是 auto / semi / assist,当前:{mode}[/red]") + raise typer.Exit(1) + + with get_client() as client: + # 校验名称唯一性 + name_ok = check_agent_name(client, name) + if not name_ok: + rprint(f"[red]名称「{name}」已存在,请换一个[/red]") + raise typer.Exit(1) + + payload = { + "icon": icon, + "name": name, + "description": description, + "mode": MODE_NAMES[mode], + "agreeTerms": agree_terms, + "rejectTerms": reject_terms, + "exemptTerms": exempt_terms, + "reviewTerms": review_terms, + "state": 1, + "ruleIds": rule_ids or [], + } + resp = save_agent(client, payload) + + if resp.get("code") != 200: + rprint(f"[red]创建失败:{resp.get('msg', '未知错误')}[/red]") + raise typer.Exit(1) + + data = resp.get("data", {}) + rprint(f"[green]✓ Agent 创建成功[/green]") + rprint(f" ID:[dim]{data.get('id')}[/dim]") + rprint(f" 名称:[bold]{data.get('name')}[/bold]") + rprint(f" 模式:{MODE_DISPLAY.get(data.get('mode'), str(data.get('mode')))}") + rprint(f" 关联规则数:{len(data.get('agentRule', []))}") + rprint(f" 创建时间:[dim]{data.get('createTime')}[/dim]") diff --git a/src/ekb_cli/commands/help.py b/src/ekb_cli/commands/help.py index cc7a586..6a489a3 100644 --- a/src/ekb_cli/commands/help.py +++ b/src/ekb_cli/commands/help.py @@ -21,6 +21,8 @@ COMMAND_TREE = { "_help": "智能审核 Agent 管理", "list": "列出所有 Agent", "get": "查看 Agent 详情 → ekb agent get --help", + "create": "新建 Agent → ekb agent create --help", + "copy": "复制 Agent(名称自动加「副本」) → ekb agent copy ", "update": "修改 Agent 配置 → ekb agent update --help", "rules": "列出所有审核规则 → ekb agent rules --help", "rule-get": "查看规则详情 → ekb agent rule-get ",