feat: add agent update command and ekb commands tree
- ekb agent update: patch-style update, only overrides provided fields - ekb commands [group]: rich tree view of full command hierarchy - api/agent.py: add save_agent() - CLAUDE.md/README.md: simplify to layered structure
This commit is contained in:
1 parent
1879decbcd
commit
1672d14d0b
7 files changed
+188
-49
No files matched your search
@@ -3,6 +3,7 @@
|
||||
import typer
|
||||
from ekb_cli.commands import auth
|
||||
from ekb_cli.commands.agent import app as agent_app
|
||||
from ekb_cli.commands.help import cmd_commands
|
||||
|
||||
app = typer.Typer(
|
||||
name="ekb",
|
||||
@@ -12,6 +13,7 @@ app = typer.Typer(
|
||||
|
||||
app.add_typer(auth.app, name="auth")
|
||||
app.add_typer(agent_app, name="agent")
|
||||
app.command("commands")(cmd_commands)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -9,3 +9,7 @@ def list_agents(client: EkbClient, page: int = 1, size: int = 1000) -> dict:
|
||||
|
||||
def get_agent(client: EkbClient, agent_id: str) -> dict:
|
||||
return client.get("/engine/hab/agent/detail", id=agent_id)
|
||||
|
||||
|
||||
def save_agent(client: EkbClient, payload: dict) -> dict:
|
||||
return client.post("/engine/hab/agent/save", payload)
|
||||
@@ -4,9 +4,11 @@ import typer
|
||||
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
|
||||
|
||||
app = typer.Typer(help="智能审核 Agent 管理")
|
||||
|
||||
app.command("list")(cmd_list)
|
||||
app.command("get")(cmd_detail)
|
||||
app.command("update")(cmd_update)
|
||||
app.command("logs")(cmd_logs)
|
||||
@@ -0,0 +1,81 @@
|
||||
"""ekb agent update <id>:修改 Agent 配置"""
|
||||
|
||||
from typing import Optional
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.api import get_client
|
||||
from ekb_cli.api.agent import get_agent, save_agent
|
||||
|
||||
# mode 映射:CLI 友好名称 → 接口值
|
||||
MODE_NAMES = {"auto": 0, "semi": 1, "assist": 2}
|
||||
MODE_DISPLAY = {0: "全自动", 1: "半自动", 2: "辅助"}
|
||||
STATE_DISPLAY = {0: "启用", 1: "禁用"}
|
||||
|
||||
|
||||
def cmd_update(
|
||||
agent_id: str = typer.Argument(..., help="Agent ID"),
|
||||
name: Optional[str] = typer.Option(None, "--name", "-n", help="名称"),
|
||||
description: Optional[str] = typer.Option(None, "--description", "-d", help="描述"),
|
||||
mode: Optional[str] = typer.Option(None, "--mode", "-m", help="模式:auto/semi/assist"),
|
||||
sampling_rate: Optional[int] = typer.Option(None, "--sampling-rate", "-r", help="半自动抽检比例 0-100"),
|
||||
state: Optional[int] = typer.Option(None, "--state", "-s", help="状态:0=启用 1=禁用"),
|
||||
agree_terms: Optional[str] = typer.Option(None, "--agree-terms", help="同意条款"),
|
||||
reject_terms: Optional[str] = typer.Option(None, "--reject-terms", help="拒绝条款"),
|
||||
exempt_terms: Optional[str] = typer.Option(None, "--exempt-terms", help="豁免条款"),
|
||||
review_terms: Optional[str] = typer.Option(None, "--review-terms", help="转人工条款"),
|
||||
):
|
||||
"""修改 Agent 配置(只传需要修改的字段)"""
|
||||
# 验证 mode 参数
|
||||
mode_val = None
|
||||
if mode is not None:
|
||||
if mode not in MODE_NAMES:
|
||||
rprint(f"[red]mode 必须是 auto / semi / assist,当前:{mode}[/red]")
|
||||
raise typer.Exit(1)
|
||||
mode_val = MODE_NAMES[mode]
|
||||
|
||||
if sampling_rate is not None and not (0 <= sampling_rate <= 100):
|
||||
rprint("[red]sampling-rate 范围是 0-100[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
with get_client() as client:
|
||||
resp = get_agent(client, agent_id)
|
||||
d = resp.get("data", {})
|
||||
if not d:
|
||||
rprint("[red]未找到该 Agent[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
# 基于现有数据构建 payload,仅覆盖传入的字段
|
||||
payload: dict = {
|
||||
"id": d["id"],
|
||||
"icon": d.get("icon", ""),
|
||||
"name": name if name is not None else d.get("name", ""),
|
||||
"description": description if description is not None else d.get("description", ""),
|
||||
"mode": mode_val if mode_val is not None else d.get("mode", 0),
|
||||
"agreeTerms": agree_terms if agree_terms is not None else d.get("agreeTerms", ""),
|
||||
"rejectTerms": reject_terms if reject_terms is not None else d.get("rejectTerms", ""),
|
||||
"exemptTerms": exempt_terms if exempt_terms is not None else d.get("exemptTerms", ""),
|
||||
"reviewTerms": review_terms if review_terms is not None else d.get("reviewTerms", ""),
|
||||
"state": state if state is not None else d.get("state", 0),
|
||||
"ruleIds": [r["id"] for r in d.get("agentRule", [])],
|
||||
}
|
||||
|
||||
# 半自动才需要 samplingRate
|
||||
effective_mode = payload["mode"]
|
||||
if effective_mode == 1:
|
||||
payload["samplingRate"] = (
|
||||
sampling_rate if sampling_rate is not None else d.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)
|
||||
|
||||
saved = save_resp.get("data", {})
|
||||
rprint(
|
||||
f"[green]✓[/green] 已更新 Agent [bold]{saved.get('name')}[/bold]"
|
||||
f" 模式={MODE_DISPLAY.get(saved.get('mode'), str(saved.get('mode')))}"
|
||||
f" 状态={STATE_DISPLAY.get(saved.get('state'), str(saved.get('state')))}"
|
||||
)
|
||||
@@ -0,0 +1,61 @@
|
||||
"""ekb commands [GROUP]:查看命令树"""
|
||||
|
||||
from typing import Optional
|
||||
import typer
|
||||
from rich.console import Console
|
||||
from rich.tree import Tree
|
||||
|
||||
console = Console()
|
||||
|
||||
# 命令树定义:新增命令组/命令时在此同步维护
|
||||
COMMAND_TREE = {
|
||||
"auth": {
|
||||
"_help": "管理企业认证",
|
||||
"add": "添加企业认证",
|
||||
"list": "列出所有企业",
|
||||
"use": "切换当前企业",
|
||||
"remove": "删除企业",
|
||||
"whoami": "查看当前企业",
|
||||
},
|
||||
"agent": {
|
||||
"_help": "智能审核 Agent 管理",
|
||||
"list": "列出所有 Agent",
|
||||
"get": "查看 Agent 详情 → ekb agent get <id> --help",
|
||||
"update": "修改 Agent 配置 → ekb agent update <id> --help",
|
||||
"logs": "查看审核日志",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def cmd_commands(group: Optional[str] = typer.Argument(None, help="命令组名称,如 auth / agent")):
|
||||
"""查看命令树(不带参数显示全树,带组名只看该分支)"""
|
||||
if group is not None:
|
||||
if group not in COMMAND_TREE:
|
||||
console.print(f"[red]未知命令组:{group}[/red]")
|
||||
raise typer.Exit(1)
|
||||
_print_group(group, COMMAND_TREE[group])
|
||||
else:
|
||||
root = Tree("[bold]ekb[/bold]")
|
||||
for grp, cmds in COMMAND_TREE.items():
|
||||
_build_branch(root, grp, cmds)
|
||||
console.print(root)
|
||||
console.print("\n[dim]查看某组详情:ekb commands <group>[/dim]")
|
||||
console.print("[dim]查看命令参数:ekb <group> <command> --help[/dim]")
|
||||
|
||||
|
||||
def _build_branch(parent, grp: str, cmds: dict):
|
||||
branch = parent.add(f"[bold cyan]{grp}[/bold cyan] {cmds.get('_help', '')}")
|
||||
for cmd, desc in cmds.items():
|
||||
if cmd == "_help":
|
||||
continue
|
||||
branch.add(f"[green]{cmd}[/green] {desc}")
|
||||
|
||||
|
||||
def _print_group(grp: str, cmds: dict):
|
||||
tree = Tree(f"[bold]ekb[/bold] [bold cyan]{grp}[/bold cyan] {cmds.get('_help', '')}")
|
||||
for cmd, desc in cmds.items():
|
||||
if cmd == "_help":
|
||||
continue
|
||||
tree.add(f"[green]{cmd}[/green] {desc}")
|
||||
console.print(tree)
|
||||
console.print(f"\n[dim]查看命令参数:ekb {grp} <command> --help[/dim]")
|
||||
Reference in new issue
Block a user