feat: resolve/pick accounts by login_id or label
All checks were successful
技能自动化发布 / release (push) Successful in 11s
All checks were successful
技能自动化发布 / release (push) Successful in 11s
This commit is contained in:
@@ -193,12 +193,21 @@ def cmd_account_list(
|
||||
environment: Optional[str] = None,
|
||||
tenant_id: Optional[str] = None,
|
||||
role: Optional[str] = None,
|
||||
login_id: Optional[str] = None,
|
||||
label: Optional[str] = None,
|
||||
limit: int = 200,
|
||||
) -> None:
|
||||
"""account list — output JSON array of accounts matching filters."""
|
||||
log = get_skill_logger()
|
||||
log.info("cli_start subcommand=account_list platform=%s env=%s tenant=%s role=%s",
|
||||
platform_input, environment, tenant_id, role)
|
||||
log.info(
|
||||
"cli_start subcommand=account_list platform=%s env=%s tenant=%s role=%s login_id=%s label=%s",
|
||||
platform_input,
|
||||
environment,
|
||||
tenant_id,
|
||||
role,
|
||||
login_id,
|
||||
label,
|
||||
)
|
||||
init_db()
|
||||
key = None
|
||||
if platform_input and platform_input not in ("all", "全部", ""):
|
||||
@@ -211,11 +220,115 @@ def cmd_account_list(
|
||||
environment=environment,
|
||||
tenant_id=tenant_id,
|
||||
role=role,
|
||||
login_id=login_id,
|
||||
label=label,
|
||||
limit=limit,
|
||||
)
|
||||
_say(json.dumps(accounts, ensure_ascii=False))
|
||||
|
||||
|
||||
def cmd_account_resolve(
|
||||
*,
|
||||
account_id: Optional[str] = None,
|
||||
platform_input: Optional[str] = None,
|
||||
login_id: Optional[str] = None,
|
||||
label: Optional[str] = None,
|
||||
query: Optional[str] = None,
|
||||
) -> None:
|
||||
"""Resolve one account by id, login_id, label, or free-form query.
|
||||
|
||||
Success: same JSON shape as ``account get``.
|
||||
Errors: ACCOUNT_NOT_FOUND / NO_ACCOUNT / AMBIGUOUS_ACCOUNT / CLI_BAD_ARGS.
|
||||
"""
|
||||
log = get_skill_logger()
|
||||
init_db()
|
||||
|
||||
aid_raw = (account_id or "").strip()
|
||||
q = (query or "").strip()
|
||||
lid = (login_id or "").strip()
|
||||
lab = (label or "").strip()
|
||||
|
||||
# Free-form: digits → id; otherwise treat as login_id (then label fallback).
|
||||
if q and not aid_raw and not lid and not lab:
|
||||
if q.isdigit():
|
||||
aid_raw = q
|
||||
else:
|
||||
lid = q
|
||||
|
||||
if aid_raw:
|
||||
try:
|
||||
aid = int(aid_raw)
|
||||
except (TypeError, ValueError):
|
||||
_say(_err_json("ERROR:CLI_BAD_ARGS", f"账号 id 必须是整数:{aid_raw}"))
|
||||
return
|
||||
acc = get_account_by_id(aid)
|
||||
if not acc:
|
||||
_say(_err_json("ERROR:ACCOUNT_NOT_FOUND", f"账号 id={aid} 不存在。"))
|
||||
return
|
||||
log.info("account_resolve_by_id id=%s", aid)
|
||||
_say(json.dumps(acc, ensure_ascii=False))
|
||||
return
|
||||
|
||||
if not lid and not lab:
|
||||
_say(_err_json(
|
||||
"ERROR:CLI_BAD_ARGS",
|
||||
"account resolve 需要 --id、<数字 query>、--login-id 或 --label(也可用 --query 用户名/手机号)。",
|
||||
))
|
||||
return
|
||||
|
||||
key = None
|
||||
if platform_input and platform_input not in ("all", "全部", ""):
|
||||
key = _resolve_or_fail(platform_input)
|
||||
if not key:
|
||||
return
|
||||
elif not platform_input:
|
||||
# login_id/label without platform: search all platforms
|
||||
key = None
|
||||
|
||||
matches = find_accounts(
|
||||
platform_key=key,
|
||||
login_id=lid or None,
|
||||
label=lab or None,
|
||||
status="active",
|
||||
limit=20,
|
||||
)
|
||||
|
||||
# If query was used as login_id and miss, retry as label.
|
||||
if not matches and q and not login_id and not label and not q.isdigit():
|
||||
matches = find_accounts(
|
||||
platform_key=key,
|
||||
label=q,
|
||||
status="active",
|
||||
limit=20,
|
||||
)
|
||||
|
||||
if not matches:
|
||||
ref = lid or lab or q
|
||||
_say(_err_json(
|
||||
"ERROR:NO_ACCOUNT",
|
||||
f"未找到匹配的账号(ref={ref!r}"
|
||||
+ (f", platform={key}" if key else "")
|
||||
+ ")。",
|
||||
))
|
||||
return
|
||||
if len(matches) > 1:
|
||||
ids = [m.get("id") for m in matches[:10]]
|
||||
_say(_err_json(
|
||||
"ERROR:AMBIGUOUS_ACCOUNT",
|
||||
f"匹配到多条账号,请改用 --id 或补充 --platform。候选 id={ids}",
|
||||
))
|
||||
return
|
||||
|
||||
acc = matches[0]
|
||||
log.info(
|
||||
"account_resolve_ok id=%s login_id=%s label=%s",
|
||||
acc.get("id"),
|
||||
acc.get("login_id"),
|
||||
acc.get("account_label"),
|
||||
)
|
||||
_say(json.dumps(acc, ensure_ascii=False))
|
||||
|
||||
|
||||
def cmd_list_json(platform_input: str, limit: int = 200) -> None:
|
||||
"""Legacy: list-json <platform|all> [--limit N]"""
|
||||
log = get_skill_logger()
|
||||
@@ -238,6 +351,8 @@ def cmd_account_pick_web(
|
||||
environment: Optional[str] = None,
|
||||
tenant_id: Optional[str] = None,
|
||||
role: Optional[str] = None,
|
||||
login_id: Optional[str] = None,
|
||||
label: Optional[str] = None,
|
||||
do_lease: bool = False,
|
||||
ttl_sec: int = 900,
|
||||
holder: Optional[str] = None,
|
||||
@@ -246,8 +361,16 @@ def cmd_account_pick_web(
|
||||
) -> None:
|
||||
"""Pick a web/RPA account for use. Optionally acquire a lease."""
|
||||
log = get_skill_logger()
|
||||
log.info("account_pick_web_attempt platform=%s env=%s tenant=%s role=%s lease=%s",
|
||||
platform_input, environment, tenant_id, role, do_lease)
|
||||
log.info(
|
||||
"account_pick_web_attempt platform=%s env=%s tenant=%s role=%s login_id=%s label=%s lease=%s",
|
||||
platform_input,
|
||||
environment,
|
||||
tenant_id,
|
||||
role,
|
||||
login_id,
|
||||
label,
|
||||
do_lease,
|
||||
)
|
||||
|
||||
key = _resolve_or_auto_register(platform_input, platform_meta)
|
||||
if not key:
|
||||
@@ -260,15 +383,45 @@ def cmd_account_pick_web(
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
lid = (login_id or "").strip() or None
|
||||
lab = (label or "").strip() or None
|
||||
|
||||
# Human-ref filters should be unique; ambiguous active matches → explicit error.
|
||||
if lid or lab:
|
||||
active_matches = find_accounts(
|
||||
platform_key=key,
|
||||
environment=environment,
|
||||
tenant_id=tenant_id,
|
||||
role=role,
|
||||
status="active",
|
||||
login_id=lid,
|
||||
label=lab,
|
||||
limit=20,
|
||||
)
|
||||
if len(active_matches) > 1:
|
||||
ids = [m.get("id") for m in active_matches[:10]]
|
||||
_say(_err_json(
|
||||
"ERROR:AMBIGUOUS_ACCOUNT",
|
||||
f"login_id/label 匹配到多条账号,请改用 account get <id>。候选 id={ids}",
|
||||
))
|
||||
return
|
||||
|
||||
candidate_ids = find_account_ids(
|
||||
platform_key=key,
|
||||
environment=environment,
|
||||
tenant_id=tenant_id,
|
||||
role=role,
|
||||
login_id=lid,
|
||||
label=lab,
|
||||
)
|
||||
if not candidate_ids:
|
||||
_say(_err_json("ERROR:NO_ACCOUNT",
|
||||
"没有符合条件的可用账号(status=active 且未被其他 lease 占用)。"))
|
||||
hint = ""
|
||||
if lid or lab:
|
||||
hint = f"(login_id={lid!r} label={lab!r})"
|
||||
_say(_err_json(
|
||||
"ERROR:NO_ACCOUNT",
|
||||
f"没有符合条件的可用账号{hint}(status=active 且未被其他 lease 占用)。",
|
||||
))
|
||||
return
|
||||
|
||||
picked_id = candidate_ids[0]
|
||||
|
||||
Reference in New Issue
Block a user