feat: resolve/pick accounts by login_id or label
All checks were successful
技能自动化发布 / release (push) Successful in 11s

This commit is contained in:
2026-07-18 15:45:52 +08:00
parent 8d36e193b4
commit 7bac647673
11 changed files with 387 additions and 30 deletions

View File

@@ -59,23 +59,30 @@ def cmd_account_ensure_web(
finally:
conn.close()
lid = (login_id or "").strip() or None
lab = (label or "").strip() or None
existing_count = count_active_accounts(
platform_key=key,
environment=environment,
tenant_id=tenant_id,
role=role,
login_id=lid,
label=lab,
)
created = False
if existing_count == 0:
create_env = (environment or "").strip() or "production"
create_role = (role or "").strip() or "default"
create_label = (label or "").strip() or None
create_label = lab or None
if not create_label:
create_label = f"{key} 默认"
if lid:
create_label = f"{key} {lid}"
created_payload = create_web_account_record(
platform_input,
login_id,
lid,
login_id_type,
create_label,
tenant_id,
@@ -95,16 +102,19 @@ def cmd_account_ensure_web(
return
created = True
log.info(
"account_ensure_web_created id=%s platform=%s profile_dir=%s",
"account_ensure_web_created id=%s platform=%s login_id=%s profile_dir=%s",
created_payload.get("id"),
key,
lid,
created_payload.get("profile_dir"),
)
else:
log.info(
"account_ensure_web_reuse platform=%s existing_active=%s",
"account_ensure_web_reuse platform=%s existing_active=%s login_id=%s label=%s",
key,
existing_count,
lid,
lab,
)
# Capture pick output so we can annotate / remap lease-busy errors.
@@ -121,6 +131,8 @@ def cmd_account_ensure_web(
environment=environment,
tenant_id=tenant_id,
role=role,
login_id=lid,
label=lab,
do_lease=do_lease,
ttl_sec=ttl_sec,
holder=holder,
@@ -145,9 +157,10 @@ def cmd_account_ensure_web(
err = (payload.get("error") or {}) if isinstance(payload.get("error"), dict) else {}
code = str(err.get("code") or "")
if code == "ERROR:NO_ACCOUNT" and existing_count > 0 and not created:
scope = "该筛选条件下" if (lid or lab) else "该平台下"
_say(_err_json(
"ERROR:LEASE_CONFLICT",
"该平台下已有账号,但均被其他任务占用。请稍后重试或释放租约;不会自动新建账号。",
f"{scope}已有账号,但均被其他任务占用。请稍后重试或释放租约;不会自动新建账号。",
))
return
_say(raw)

View File

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

View File

@@ -26,6 +26,7 @@ from service.account_query_commands import (
cmd_account_get_credential,
cmd_account_list,
cmd_account_pick_web,
cmd_account_resolve,
cmd_get,
cmd_list_json,
cmd_pick_web,
@@ -52,6 +53,7 @@ __all__ = [
"cmd_account_mark_session",
"cmd_account_mark_used",
"cmd_account_pick_web",
"cmd_account_resolve",
"cmd_credential_pick",
"cmd_credential_resolve",
"cmd_delete_by_id",