ap-client 0.4.0.dev0__tar.gz → 0.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: ap-client
3
- Version: 0.4.0.dev0
3
+ Version: 0.4.1
4
4
  Summary: Agent Platform API Client & CLI
5
5
  Requires-Python: >=3.10
6
6
  Requires-Dist: pyyaml>=6.0
@@ -0,0 +1,179 @@
1
+ """``ap akpool`` 命令族 —— Account Pool(托管 AK)观测操作。
2
+
3
+ * ``akpool rpm get`` → apiserver ``GET /apis/v1/model-api-keys/rpm-usage``
4
+ (中心侧,走 ``APIClient._get_central``),按 AK + 模型查询当前 RPM 用量与上限。
5
+
6
+ AK 的两种指定方式互斥且必填其一:``--ak-id``(uak-xxx,仅平台托管 AK)或
7
+ ``--masked-ak``(掩码后 6 位,未托管 AK 走这条路)。服务端同口径(都缺或都给 →
8
+ 400),CLI 侧先拦一道,不发请求。``--time-range`` 是 Go duration 风格
9
+ (``30s``/``5m``/``1h``);显式 ``--from``/``--to``(RFC3339)时覆盖
10
+ ``--time-range``。
11
+
12
+ 响应是嵌套结构(无信封):``usage.current_rpm``/``usage.sampled_at``、
13
+ ``rpm.limit``/``rpm.limit_state``/``rpm.synced_at``、``usage_status``、
14
+ ``time_range``、``ak_id``/``masked_api_key``/``model_id``。json/yaml 原样透传;
15
+ plain/table 摊平成单行 key=value 展示。
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import re
21
+ from typing import Any, Optional
22
+
23
+ import typer
24
+ from ap_client import get_client
25
+
26
+ __all__ = ["akpool_app", "register"]
27
+
28
+ akpool_app = typer.Typer(help="Account Pool operations (managed API keys)")
29
+ rpm_app = typer.Typer(help="Account Pool RPM usage operations")
30
+
31
+ akpool_app.add_typer(rpm_app, name="rpm")
32
+
33
+
34
+ def register(app: typer.Typer) -> None:
35
+ """把 ``akpool`` 挂到 ``ap`` 根命令上。"""
36
+ app.add_typer(akpool_app, name="akpool")
37
+
38
+
39
+ _FORMAT_HELP = "Output format: plain/table/json/yaml (default: AP_FORMAT or command default)"
40
+ _TIME_RANGE_HELP = "Lookback window for the RPM reading (Go duration, e.g. 30s/5m/1h)"
41
+ _ISO_TIME_HELP = "RFC3339 timestamp, e.g. 2026-09-21T12:00:00+08:00"
42
+
43
+ _TIME_RANGE_RE = re.compile(r"^\d+[smh]$")
44
+
45
+ #: plain/table 输出的字段顺序,取值自嵌套响应(见 _flatten_rpm)。
46
+ _RPM_KEYS = (
47
+ "current_rpm",
48
+ "rpm_limit",
49
+ "model",
50
+ "sampled_at",
51
+ "time_range",
52
+ "ak_id",
53
+ "usage_status",
54
+ )
55
+
56
+
57
+ def _fmt(output_format: Optional[str]) -> str:
58
+ from .cli import _normalize_output_format
59
+
60
+ return _normalize_output_format(output_format, keep_table=True)
61
+
62
+
63
+ def _check_time_range(value: str) -> str:
64
+ normalized = str(value or "").strip().lower()
65
+ if not _TIME_RANGE_RE.fullmatch(normalized):
66
+ raise typer.BadParameter(f"--time-range must be a duration like 30s/5m/1h, got {value!r}")
67
+ return normalized
68
+
69
+
70
+ def _flatten_rpm(payload: dict) -> dict:
71
+ """把嵌套的 rpm-usage 响应摊平成 plain/table 展示用的扁平 dict。
72
+
73
+ null 值渲染为 ``-``(如未托管 AK 的 ``ak_id``、未知限额的 ``rpm.limit``)。
74
+ """
75
+ rpm = payload.get("rpm")
76
+ usage = payload.get("usage")
77
+ flat = {
78
+ "current_rpm": usage.get("current_rpm") if isinstance(usage, dict) else None,
79
+ "rpm_limit": rpm.get("limit") if isinstance(rpm, dict) else None,
80
+ "model": payload.get("model_id"),
81
+ "sampled_at": usage.get("sampled_at") if isinstance(usage, dict) else None,
82
+ "time_range": payload.get("time_range"),
83
+ "ak_id": payload.get("ak_id"),
84
+ "usage_status": payload.get("usage_status"),
85
+ }
86
+ return {key: (value if value is not None else "-") for key, value in flat.items()}
87
+
88
+
89
+ def _emit_rpm(payload: Any, output_format: str) -> None:
90
+ from .cli import _print_formatted, _print_rich_table
91
+
92
+ if not isinstance(payload, dict):
93
+ payload = {"value": payload}
94
+ if output_format in ("json", "yaml"):
95
+ _print_formatted(payload, output_format)
96
+ return
97
+ flat = _flatten_rpm(payload)
98
+ keys = [key for key in _RPM_KEYS if key in flat]
99
+ keys += [key for key in flat if key not in keys]
100
+ if output_format == "table":
101
+ _print_rich_table([flat], [(key, key) for key in keys])
102
+ return
103
+ _print_rpm_plain(payload)
104
+
105
+
106
+ def _print_rpm_plain(payload: dict) -> None:
107
+ """plain 输出:标题行(usage_status) + 对齐 key/value,与 _print_job_get_plain 同风格。
108
+
109
+ current_rpm 在限额已知时附利用率百分比;rpm_limit 未知/不限时展示 limit_state。
110
+ ak_id / masked_api_key 为 null(未托管 AK)时整行省略。
111
+ """
112
+ from .cli import _print_key_values
113
+
114
+ rpm = payload.get("rpm")
115
+ usage = payload.get("usage")
116
+ rpm = rpm if isinstance(rpm, dict) else {}
117
+ usage = usage if isinstance(usage, dict) else {}
118
+
119
+ current = usage.get("current_rpm")
120
+ limit = rpm.get("limit")
121
+ limit_state = rpm.get("limit_state") or "unknown"
122
+
123
+ if isinstance(current, (int, float)) and isinstance(limit, (int, float)) and limit > 0:
124
+ current_text = f"{current} ({current / limit * 100:.1f}% of limit)"
125
+ else:
126
+ current_text = current
127
+ limit_text = f"{limit} ({limit_state})" if limit is not None else limit_state
128
+
129
+ typer.echo(f"AK RPM Usage ({payload.get('usage_status') or 'unknown'})")
130
+ _print_key_values(
131
+ [
132
+ ("current_rpm", current_text),
133
+ ("rpm_limit", limit_text),
134
+ ("model", payload.get("model_id")),
135
+ ("ak_id", payload.get("ak_id")),
136
+ ("masked_api_key", payload.get("masked_api_key")),
137
+ ("sampled_at", usage.get("sampled_at")),
138
+ ("time_range", payload.get("time_range")),
139
+ ]
140
+ )
141
+
142
+
143
+ @rpm_app.command("get")
144
+ def rpm_get(
145
+ ak_id: Optional[str] = typer.Option(
146
+ None, "--ak-id", help="Managed AK ID (uak-xxx); mutually exclusive with --masked-ak"
147
+ ),
148
+ masked_ak: Optional[str] = typer.Option(
149
+ None, "--masked-ak", help="Last 6 characters of the AK; mutually exclusive with --ak-id"
150
+ ),
151
+ model: str = typer.Option(..., "--model", help="Model the RPM reading applies to"),
152
+ time_range: str = typer.Option("5m", "--time-range", help=_TIME_RANGE_HELP),
153
+ from_time: Optional[str] = typer.Option(
154
+ None, "--from", help=f"Window start ({_ISO_TIME_HELP}); overrides --time-range"
155
+ ),
156
+ to_time: Optional[str] = typer.Option(
157
+ None, "--to", help=f"Window end ({_ISO_TIME_HELP}); overrides --time-range"
158
+ ),
159
+ output_format: str = typer.Option(None, "--format", help=_FORMAT_HELP),
160
+ ):
161
+ """Show the current RPM usage and limit of a managed AK for one model."""
162
+ resolved_ak_id = (ak_id or "").strip()
163
+ resolved_masked_ak = (masked_ak or "").strip()
164
+ if bool(resolved_ak_id) == bool(resolved_masked_ak):
165
+ raise typer.BadParameter("exactly one of --ak-id or --masked-ak is required")
166
+ if not model.strip():
167
+ raise typer.BadParameter("--model must not be empty")
168
+ normalized_range = _check_time_range(time_range)
169
+ output_format = _fmt(output_format)
170
+
171
+ result = get_client().get_akpool_rpm(
172
+ model=model.strip(),
173
+ ak_id=resolved_ak_id or None,
174
+ masked_ak=resolved_masked_ak or None,
175
+ time_range=normalized_range,
176
+ from_time=from_time,
177
+ to_time=to_time,
178
+ )
179
+ _emit_rpm(result, output_format)
@@ -995,6 +995,42 @@ class APIClient:
995
995
  }
996
996
  }
997
997
 
998
+ # ==================== Account Pool operations ====================
999
+
1000
+ def get_akpool_rpm(
1001
+ self,
1002
+ *,
1003
+ model: str,
1004
+ ak_id: Optional[str] = None,
1005
+ masked_ak: Optional[str] = None,
1006
+ time_range: Optional[str] = None,
1007
+ from_time: Optional[str] = None,
1008
+ to_time: Optional[str] = None,
1009
+ timeout: TimeoutType = None,
1010
+ ) -> dict:
1011
+ """查询托管 AK 在指定模型上的当前 RPM 用量与上限(GET /apis/v1/model-api-keys/rpm-usage)。
1012
+
1013
+ ``ak_id``(uak-xxx) 与 ``masked_ak``(掩码后 6 位) 二选一,都缺或都给
1014
+ 服务端返回 400;``from_time``/``to_time``(RFC3339) 显式指定时覆盖
1015
+ ``time_range``(Go duration,如 5m/1h,服务端默认 5m)。
1016
+ 响应为嵌套结构:``usage.current_rpm``/``usage.sampled_at``、
1017
+ ``rpm.limit``/``rpm.limit_state``/``rpm.synced_at``、``usage_status``;
1018
+ 查未托管 AK 时 ``ak_id`` 为 null。
1019
+ """
1020
+ validate_ak_id(ak_id)
1021
+ params: dict = {"model": model}
1022
+ if ak_id is not None:
1023
+ params["ak_id"] = ak_id
1024
+ if masked_ak is not None:
1025
+ params["masked_ak"] = masked_ak
1026
+ if time_range is not None:
1027
+ params["time_range"] = time_range
1028
+ if from_time is not None:
1029
+ params["from"] = from_time
1030
+ if to_time is not None:
1031
+ params["to"] = to_time
1032
+ return self._get_central("/model-api-keys/rpm-usage", params=params, timeout=timeout)
1033
+
998
1034
  # ==================== Job operations ====================
999
1035
 
1000
1036
  def create_group(
@@ -13,6 +13,7 @@ from urllib.parse import quote
13
13
 
14
14
  import typer
15
15
  from ap_client import __version__, get_client, get_config, tbb
16
+ from ap_client.akpool_commands import register as _register_akpool_commands
16
17
  from ap_client.api import (
17
18
  _OTHERS_MODEL,
18
19
  APIError,
@@ -160,6 +161,7 @@ app.add_typer(checkpoint_app, name="checkpoint")
160
161
  _register_dataset_commands(dataset_app)
161
162
  _register_instance_commands(app)
162
163
  _register_fs_commands(app)
164
+ _register_akpool_commands(app)
163
165
 
164
166
  _PAI_RUNTIME_ENV_TAGS: tuple[tuple[str, str], ...] = (
165
167
  ("DLC_JOB_ID", "dlc_job_id"),
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "ap-client"
7
- version = "0.4.0.dev0"
7
+ version = "0.4.1"
8
8
  description = "Agent Platform API Client & CLI"
9
9
  readme = { text = "A lightweight Python SDK and command line interface for Agent Platform. It provides helpers for configuring API access and managing templates, datasets, jobs, and groups.", content-type = "text/markdown" }
10
10
  requires-python = ">=3.10"