smartcheck-cli 0.1.0__tar.gz → 0.1.2__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.
Files changed (22) hide show
  1. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/PKG-INFO +2 -2
  2. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/pyproject.toml +2 -2
  3. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli/commands.py +85 -2
  4. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli/http.py +8 -3
  5. smartcheck_cli-0.1.2/smartcheck_cli/insights.py +260 -0
  6. smartcheck_cli-0.1.2/smartcheck_cli/inspection.py +104 -0
  7. smartcheck_cli-0.1.2/smartcheck_cli/labs.py +127 -0
  8. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli/main.py +92 -16
  9. smartcheck_cli-0.1.2/smartcheck_cli/quality.py +114 -0
  10. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli.egg-info/PKG-INFO +2 -2
  11. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli.egg-info/SOURCES.txt +7 -1
  12. smartcheck_cli-0.1.2/tests/test_cli.py +488 -0
  13. smartcheck_cli-0.1.2/tests/test_inspection.py +89 -0
  14. smartcheck_cli-0.1.2/tests/test_labs.py +65 -0
  15. smartcheck_cli-0.1.0/tests/test_cli.py +0 -160
  16. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/setup.cfg +0 -0
  17. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli/__init__.py +0 -0
  18. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli/auth.py +0 -0
  19. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli.egg-info/dependency_links.txt +0 -0
  20. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli.egg-info/entry_points.txt +0 -0
  21. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli.egg-info/requires.txt +0 -0
  22. {smartcheck_cli-0.1.0 → smartcheck_cli-0.1.2}/smartcheck_cli.egg-info/top_level.txt +0 -0
@@ -1,7 +1,7 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: smartcheck-cli
3
- Version: 0.1.0
4
- Summary: SmartCheck 账号授权与通话管理命令行
3
+ Version: 0.1.2
4
+ Summary: SmartCheck 账号授权、通话管理、洞察与质检配置命令行
5
5
  Requires-Python: >=3.11
6
6
  Requires-Dist: requests<3,>=2.32
7
7
  Requires-Dist: filelock<4,>=3.13
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "smartcheck-cli"
7
- version = "0.1.0"
8
- description = "SmartCheck 账号授权与通话管理命令行"
7
+ version = "0.1.2"
8
+ description = "SmartCheck 账号授权、通话管理、洞察与质检配置命令行"
9
9
  requires-python = ">=3.11"
10
10
  dependencies = ["requests>=2.32,<3", "filelock>=3.13,<4", "keyring>=25,<26"]
11
11
 
@@ -5,11 +5,26 @@ from pathlib import Path
5
5
  import secrets
6
6
  import string
7
7
  import sys
8
+ from typing import NamedTuple
8
9
 
9
10
  from .auth import current
10
11
  from .http import Error, note, segment
11
12
 
12
13
 
14
+ class Command(NamedTuple):
15
+ method: str
16
+ path: str
17
+ purpose: str
18
+ example: str
19
+
20
+
21
+ def positive_id(value):
22
+ number = int(value)
23
+ if not 1 <= number <= 2147483647:
24
+ raise Error("编号须为 1 至 2147483647 的整数")
25
+ return number
26
+
27
+
13
28
  def read_object(path):
14
29
  try:
15
30
  body = json.loads(Path(path).read_text(encoding="utf-8"))
@@ -53,8 +68,14 @@ def confirm(client, pair, args, body):
53
68
  info = client.request("GET", "/call-fields/" + segment(target), token=pair["access_token"])
54
69
  message = (f"删除后不可恢复,字段键也不能再次使用。如果只是暂时不需要,建议停用。"
55
70
  f"确定删除以下字段? {info['label']}({target},{info['field_type']})")
71
+ elif args.group == "asr-settings":
72
+ message = "覆盖本企业的识别语种和整份即时热词,下次提交录音转写时生效。"
56
73
  else:
57
74
  message = f"保存话者识别方式 {body.get('mode', '')},仅对之后首次开始转写的录音生效。"
75
+ confirm_operation(client, pair, message)
76
+
77
+
78
+ def confirm_operation(client, pair, message):
58
79
  note(f"{client.host} · {pair['profile']['enterprise_name']}({pair['profile']['tenant_id']})\n{message} 输入 y 继续,默认取消。")
59
80
  if sys.stdin.readline().strip() != "y":
60
81
  raise Error("已取消", 1)
@@ -62,11 +83,11 @@ def confirm(client, pair, args, body):
62
83
 
63
84
  def run_business(client, vault, args):
64
85
  group, action = args.group, args.action
65
- guarded = action == "delete" or (group, action) == ("speaker-roles", "set")
86
+ guarded = action in ("delete", "set")
66
87
  path = "/" + group
67
88
  if hasattr(args, "identifier"):
68
89
  path += "/" + segment(args.identifier)
69
- methods = {"list": "GET", "get": "GET", "download-recording": "GET", "create": "POST",
90
+ methods = {"list": "GET", "get": "GET", "quality-results": "GET", "download-recording": "GET", "create": "POST",
70
91
  "update": "PUT", "set": "PUT", "enable": "PATCH", "disable": "PATCH",
71
92
  "reorder": "PUT", "delete": "DELETE"}
72
93
  method = methods[action]
@@ -92,6 +113,19 @@ def run_business(client, vault, args):
92
113
  params[key] = value
93
114
  if args.direction:
94
115
  params["direction"] = args.direction
116
+ if bool(args.tag_field) != bool(args.tag):
117
+ raise Error("--tag-field 与 --tag 须同时使用")
118
+ if args.tag:
119
+ tags = list(dict.fromkeys(tag.strip() for tag in args.tag))
120
+ if not all(tags):
121
+ raise Error("标签不能为空")
122
+ if len(tags) > 10:
123
+ raise Error("最多 10 个 --tag")
124
+ if any(len(tag) > 50 for tag in tags):
125
+ raise Error("标签最多 50 个字符")
126
+ params.update(tag_field=args.tag_field, tag=tags)
127
+ elif action == "quality-results":
128
+ path += "/quality-results"
95
129
  elif action == "download-recording":
96
130
  path += "/recording/" + segment(args.source) + "/audio"
97
131
  output = args.output
@@ -104,6 +138,11 @@ def run_business(client, vault, args):
104
138
  pair = current(client, vault)
105
139
  if group != "calls" and pair["profile"]["role"] != "admin":
106
140
  raise Error("无权访问系统设置", 3, code="FORBIDDEN", status=403)
141
+ if params and "tag_field" in params:
142
+ # The list API silently drops an unusable tag field; the CLI reports it instead.
143
+ fields = client.request("GET", "/calls/tag-fields", token=pair["access_token"])
144
+ if params["tag_field"] not in {field["field_key"] for field in fields}:
145
+ raise Error("标签字段不存在或不可筛选")
107
146
  if group == "accounts" and action in ("create", "update"):
108
147
  body = {key: getattr(args, key) for key in ("name", "email", "phone") if getattr(args, key) is not None}
109
148
  if action == "create":
@@ -127,3 +166,47 @@ def run_business(client, vault, args):
127
166
  result["generated_password"] = generated
128
167
  note("请立即保存该密码")
129
168
  return result
169
+
170
+
171
+ def reidentify_calls(client, vault, args):
172
+ pair = current(client, vault)
173
+ profile = pair["profile"]
174
+ if profile["role"] != "admin":
175
+ raise Error("无权访问系统设置", 3, code="FORBIDDEN", status=403)
176
+ config = client.request("GET", "/speaker-roles", token=pair["access_token"])
177
+ if not config["configured"]:
178
+ raise Error("尚未配置话者识别,请先在网页「系统设置 → 话者识别」保存配置")
179
+ total = len(args.call_ids)
180
+ mode = {"channel": "声道标注", "decision": "决策模型", "llm": "LLM 识别"}[config["mode"]]
181
+ if config["mode"] == "decision":
182
+ mode += ",LLM 兜底:" + ("开" if config["fallback"] else "关")
183
+ note(f"{client.host} · {profile['enterprise_name']}({profile['tenant_id']})")
184
+ note(f"按当前话者识别配置重新识别 {total} 通通话的话轮角色\n识别方式:{mode}")
185
+ note("只更新角色,不重新转写,原文和时间戳不变。识别结果将替换通话详情中当前显示的角色。")
186
+ if not args.yes:
187
+ note("输入 y 继续,默认取消。")
188
+ if sys.stdin.readline().strip() != "y":
189
+ raise Error("已取消", 1)
190
+
191
+ labels = {"reidentified": "已识别", "skipped": "已跳过", "failed": "失败", "unknown": "结果未知"}
192
+ counts = dict.fromkeys(labels, 0)
193
+ results = []
194
+ for index, call_id in enumerate(args.call_ids, 1):
195
+ try:
196
+ # Batches can outlive an access token. Keep the confirmed tenant and account.
197
+ active = current(client, vault)
198
+ if any(active["profile"][k] != profile[k] for k in ("tenant_id", "username", "role")):
199
+ raise Error("登录账号已变更")
200
+ result = client.request("POST", f"/calls/{segment(call_id)}/reidentify-speakers",
201
+ token=active["access_token"])
202
+ if not isinstance(result, dict) or result.get("status") not in ("reidentified", "skipped"):
203
+ raise Error("请求结果未知,请先查询确认后再决定是否重试", 4)
204
+ except Error as error:
205
+ uncertain = error.exit_code == 4
206
+ result = dict(call_id=call_id, status="unknown" if uncertain else "failed",
207
+ reason=error.args[0] if uncertain else str(error))
208
+ status = result["status"]
209
+ counts[status] += 1
210
+ results.append(result)
211
+ note(f"({index}/{total}) {call_id} {labels[status]}")
212
+ return {"results": results, "counts": counts}
@@ -46,12 +46,17 @@ class Client:
46
46
  self.session = requests.Session()
47
47
  self.session.trust_env = False
48
48
 
49
- def request(self, method, path, *, token=None, params=None, body=None, output=None, force=False):
50
- reading = method == "GET"
49
+ def request(self, method, path, *, token=None, params=None, body=None, output=None, force=False,
50
+ upload=None, data=None, read_only=False):
51
+ reading = method == "GET" or read_only
51
52
  attempts = 3 if reading else 1
52
53
  for attempt in range(attempts):
53
54
  try:
54
- with self.session.request(method, self.base + path, params=params, json=body,
55
+ files = None
56
+ if upload is not None:
57
+ upload.seek(0)
58
+ files = {"file": (Path(upload.name).name, upload)}
59
+ with self.session.request(method, self.base + path, params=params, json=body, files=files, data=data,
55
60
  headers={"Authorization": "Bearer " + token} if token else {},
56
61
  timeout=self.timeout, allow_redirects=False,
57
62
  stream=output is not None) as response:
@@ -0,0 +1,260 @@
1
+ """Commands for the tenant's insight configuration."""
2
+
3
+ import json
4
+ from pathlib import Path
5
+
6
+ from .auth import current
7
+ from .commands import confirm_operation, read_object
8
+ from .http import Error, segment
9
+
10
+
11
+ GROUPS = {
12
+ "tag-catalog": ("标签管理", [
13
+ ("list", "列出标签体系", ""), ("get", "读取体系及全部下级目录项", "1"),
14
+ ("create", "新建体系或下级目录项", "--json-file tag.json"),
15
+ ("update", "编辑目录项", "2 --json-file tag.json"),
16
+ ("enable", "启用标签", "4"), ("disable", "停用标签", "4"),
17
+ ("delete", "删除目录项", "4 --yes"),
18
+ ]),
19
+ "product-models": ("品型管理", [
20
+ ("list", "列出产品系列与型号", ""), ("get", "读取品型", "1"),
21
+ ("create", "新建系列或型号", "--json-file model.json"),
22
+ ("update", "保存品型名称、启用状态、别名与属性", "1 --json-file model.json"),
23
+ ("delete", "删除品型", "1 --yes"),
24
+ ]),
25
+ "insight-datasets": ("自定义数据集管理", [
26
+ ("list", "列出数据集", ""), ("get", "读取定义与版本", "ds_store"),
27
+ ("create", "创建数据集", "--json-file dataset.json"),
28
+ ("update", "按基准版本保存定义", "ds_store --json-file definition.json"),
29
+ ("query", "查询记录", "ds_store --json-file query.json"),
30
+ ("add-record", "按基准版本新增记录", "ds_store --json-file row.json"),
31
+ ("update-record", "按记录编号和基准版本编辑记录", "ds_store 1 --json-file row.json"),
32
+ ("enable", "启用数据集", "ds_store"), ("disable", "停用数据集", "ds_store"),
33
+ ("delete", "删除数据集", "ds_store --yes"),
34
+ ("workbook-inspect", "读取 Excel 工作表和表头", "--file stores.xlsx"),
35
+ ("workbook-preview", "校验并预览导入,返回提交令牌", "--file stores.xlsx --json-file plan.json"),
36
+ ("workbook-commit", "使用同一文件、方案和预览令牌确认导入或替换", "--file stores.xlsx --json-file plan.json --token TOKEN --yes"),
37
+ ("export", "导出当前版本的 Excel", "ds_store --output stores.xlsx"),
38
+ ]),
39
+ "tagging-code": ("打标代码管理", [
40
+ ("list", "搜索并分页列出代码", "--keyword 意向 --page 1"), ("get", "读取代码、配置和版本", "intent"),
41
+ ("create", "保存新代码", "--json-file code.json --source-file intent.py"),
42
+ ("update", "按版本保存代码", "intent --json-file update.json --source-file intent.py"),
43
+ ("delete", "删除代码", "intent --yes"),
44
+ ]),
45
+ "tagging-tasks": ("洞察打标任务", [
46
+ ("list", "按名称查询自动任务或临时任务", "--tab temp --keyword 意向"),
47
+ ("options", "查看筛选字段、可绑定代码和模型通道", ""),
48
+ ("get", "读取任务配置、状态和执行统计", "12"),
49
+ ("preview", "统计临时任务命中通话数及已有系统值、人工值的通话数", "--json-file preview.json"),
50
+ ("create", "创建并启用自动任务,或启动一次临时批次", "--tab temp --json-file task.json"),
51
+ ("update", "保存自动任务的完整配置", "12 --json-file task.json"),
52
+ ("enable", "启用自动任务", "12"), ("disable", "停用自动任务", "12"),
53
+ ("cancel", "取消临时批次中尚未开始的会话", "12 --yes"),
54
+ ("retry", "使用临时批次原快照重试未完成会话", "12 --yes"),
55
+ ("runs", "按状态或通话 ID 查询执行记录", "12 --status failed"),
56
+ ("run-detail", "查看单通执行结果、证据和失败原因", "12 9"),
57
+ ("delete", "删除打标任务", "12 --yes"),
58
+ ]),
59
+ "insight-fields": ("洞察字段设置", [
60
+ ("list", "按顺序列出洞察字段", ""), ("get", "按字段键读取定义", "intent"),
61
+ ("create", "新增洞察字段", "--json-file field.json"),
62
+ ("update", "保存字段定义", "intent --json-file update.json"),
63
+ ("enable", "启用字段", "intent"), ("disable", "停用字段", "intent"),
64
+ ("reorder", "排列全部自定义洞察字段", "intent budget"),
65
+ ("candidates", "查询标签、品型或数据集候选项", "--json-file candidates.json"),
66
+ ("delete", "删除字段", "intent --yes"),
67
+ ]),
68
+ }
69
+
70
+ READ_ACTIONS = {"list", "get", "candidates", "query", "export", "options", "preview", "runs", "run-detail"}
71
+ BODY_ACTIONS = {"create", "update", "query", "add-record", "update-record", "candidates",
72
+ "preview", "workbook-preview", "workbook-commit"}
73
+ TARGET_ACTIONS = {"get", "update", "enable", "disable", "delete", "query", "add-record", "update-record", "export",
74
+ "cancel", "retry", "runs", "run-detail"}
75
+
76
+
77
+ def role(group, action):
78
+ if group == "tagging-tasks" and (action == "preview" or action not in READ_ACTIONS):
79
+ return "管理员"
80
+ if action in READ_ACTIONS:
81
+ return "有洞察查看权限的账号"
82
+ return "管理员(洞察已启用)" if group == "tagging-code" else "有洞察编辑权限的账号"
83
+
84
+
85
+ def task_number(value):
86
+ try:
87
+ number = int(value)
88
+ except ValueError:
89
+ number = 0
90
+ if not 1 <= number <= 2**31 - 1:
91
+ raise Error("任务 ID、执行记录 ID 和页码须为 1 至 2147483647 的整数")
92
+ return number
93
+
94
+
95
+ def arguments(parser, group, action):
96
+ add = parser.add_argument
97
+ if action in TARGET_ACTIONS:
98
+ target = {"tag-catalog": "system_id" if action == "get" else "node_id", "product-models": "model_id",
99
+ "insight-fields": "field_key", "insight-datasets": "dataset_key", "tagging-code": "code_key",
100
+ "tagging-tasks": "task_id"}[group]
101
+ add("identifier", metavar=target, type=task_number if group == "tagging-tasks" else str,
102
+ help="当前企业的目标标识")
103
+ if action == "update-record":
104
+ add("number", type=int, metavar="number", help="query 返回的记录编号")
105
+ if action in BODY_ACTIONS:
106
+ add("--json-file", required=True, help="UTF-8 JSON 请求体或工作簿映射方案,示例见 guide")
107
+ if group in ("tagging-code", "tagging-tasks") and action in ("list", "runs"):
108
+ keyword = "通话 ID" if action == "runs" else "名称"
109
+ add("--keyword", default="", help=f"搜索{keyword}")
110
+ add("--page", type=task_number if group == "tagging-tasks" else int, default=1, help="页码,默认 1")
111
+ add("--per-page", type=int, choices=(20, 50, 100), default=20, help="每页条数,默认 20")
112
+ if group == "tagging-code" and action in ("create", "update"):
113
+ add("--source-file", help="从 UTF-8 Python 文件读取 source,覆盖 JSON 中的源码")
114
+ if group == "tagging-tasks":
115
+ if action in ("list", "create"):
116
+ add("--tab", choices=("auto", "temp"), required=action == "create", default="auto",
117
+ help="auto 为自动任务,temp 为临时批次;查询默认 auto,创建时必填")
118
+ if action == "list":
119
+ add("--exact", action="store_true", help="精确匹配任务名称")
120
+ if action == "runs":
121
+ add("--status", choices=("all", "pending", "running", "succeeded", "failed", "cancelled"),
122
+ default="all", help="执行记录状态,默认 all")
123
+ if action == "run-detail":
124
+ add("run_id", type=task_number, metavar="run_id", help="runs 返回的执行记录 ID")
125
+ if action == "reorder":
126
+ add("field_keys", nargs="+", metavar="field_key", help="按新顺序给出全部自定义字段键,系统预置字段固定在首位")
127
+ if action.startswith("workbook-"):
128
+ add("--file", required=True, help="本地 .xlsx 工作簿")
129
+ if action == "workbook-inspect":
130
+ add("--sheet", help="读取指定工作表的表头")
131
+ if action == "workbook-commit":
132
+ add("--token", required=True, help="同一工作簿和方案的预览令牌")
133
+ if action == "export":
134
+ add("--output", required=True, help="本地 .xlsx 目标文件")
135
+ add("--force", action="store_true", help="覆盖已有文件")
136
+ if action in ("delete", "workbook-commit", "cancel", "retry"):
137
+ add("--yes", action="store_true", help="确认执行,非交互环境必填")
138
+
139
+
140
+ def upload_workbook(client, pair, args, path, body):
141
+ action = args.action
142
+ data = {}
143
+ if action == "workbook-inspect":
144
+ if args.sheet is not None:
145
+ data["sheet"] = args.sheet
146
+ else:
147
+ data["plan"] = json.dumps(body, ensure_ascii=False)
148
+ if action == "workbook-commit":
149
+ data["token"] = args.token
150
+ if not args.yes:
151
+ target = body.get("dataset_key")
152
+ change = f"替换数据集 {target}" if target else f"创建数据集 {body.get('name', '')}"
153
+ confirm_operation(client, pair, f"确定使用 {args.file} {change}?")
154
+ source = Path(args.file)
155
+ if source.suffix.lower() != ".xlsx" or source.stat().st_size > 50 * 1024 * 1024:
156
+ raise Error("请选择不超过 50 MB 的 .xlsx 文件")
157
+ with source.open("rb") as workbook:
158
+ return client.request("POST", path + "/workbook/" + action.removeprefix("workbook-"),
159
+ upload=workbook, data=data, read_only=action != "workbook-commit",
160
+ token=pair["access_token"])
161
+
162
+
163
+ def run(client, vault, args):
164
+ group, action = args.group, args.action
165
+ path = "/insights/" + group
166
+ if action in TARGET_ACTIONS:
167
+ key = segment(str(args.identifier))
168
+ if (group, action) != ("insight-fields", "get"):
169
+ path += "/" + key
170
+ body = read_object(args.json_file) if action in BODY_ACTIONS else None
171
+ if getattr(args, "source_file", None):
172
+ try:
173
+ body["source"] = Path(args.source_file).read_text(encoding="utf-8")
174
+ except (OSError, UnicodeError) as exc:
175
+ raise Error("无法读取 UTF-8 Python 源文件") from exc
176
+ pair = current(client, vault)
177
+ options = {"token": pair["access_token"]}
178
+ if action == "delete" and not args.yes:
179
+ confirm_operation(client, pair, f"确定删除{GROUPS[group][0]}中的 {args.identifier}?")
180
+ if action.startswith("workbook-"):
181
+ return upload_workbook(client, pair, args, path, body)
182
+ if (group, action) == ("insight-fields", "get"):
183
+ catalog = client.request("GET", path, **options)
184
+ for field in catalog["fields"]:
185
+ if field["key"] == args.identifier:
186
+ return field
187
+ raise Error("洞察字段不存在", code="NOT_FOUND", status=404)
188
+ if group in ("tagging-code", "tagging-tasks") and action in ("list", "runs"):
189
+ if args.page < 1:
190
+ raise Error("页码须大于 0")
191
+ options["params"] = dict(q=args.keyword, page=args.page, pageSize=args.per_page)
192
+ if group == "tagging-tasks":
193
+ if action == "list":
194
+ options["params"].update(tab=args.tab, exact=args.exact)
195
+ elif action == "create":
196
+ options["params"] = {"tab": args.tab}
197
+ elif action in ("enable", "disable", "cancel", "retry"):
198
+ if action in ("cancel", "retry") and not args.yes:
199
+ change = "取消批次中尚未开始的会话" if action == "cancel" else "按批次原快照重试未完成会话"
200
+ confirm_operation(client, pair, f"任务 {args.identifier}:确定{change}?")
201
+ return client.request("POST", path + "/" + action, **options)
202
+ elif action in ("options", "preview", "runs"):
203
+ path += "/" + action
204
+ if action == "runs":
205
+ options["params"]["status"] = args.status
206
+ elif action == "run-detail":
207
+ path += f"/runs/{args.run_id}"
208
+ if action == "export":
209
+ if Path(args.output).exists() and not args.force:
210
+ raise Error("文件已存在,添加 --force 覆盖")
211
+ return client.request("GET", path + "/export", output=args.output, force=args.force, **options)
212
+ if action in ("enable", "disable"):
213
+ body = {"enabled": action == "enable"}
214
+ elif action == "reorder":
215
+ body = {"field_keys": args.field_keys}
216
+ path += "/_order"
217
+ elif action == "candidates":
218
+ path += "/_candidates"
219
+ elif action == "query":
220
+ path += "/query"
221
+ elif action == "add-record":
222
+ path += "/records"
223
+ elif action == "update-record":
224
+ path += f"/records/{args.number}"
225
+ methods = {"list": "GET", "get": "GET", "create": "POST", "update": "PUT", "delete": "DELETE",
226
+ "enable": "PATCH", "disable": "PATCH", "reorder": "PUT", "candidates": "POST",
227
+ "query": "POST", "add-record": "POST", "update-record": "PUT", "options": "GET",
228
+ "preview": "POST", "runs": "GET", "run-detail": "GET"}
229
+ return client.request(methods[action], path, body=body, read_only=action in READ_ACTIONS, **options)
230
+
231
+
232
+ def examples():
233
+ tag = {"name": "购车需求", "description": "客户提到的购车需求"}
234
+ model = {"name": "A 系列", "enabled": True, "aliases": ["系列 A"], "attributes": []}
235
+ field = {"name": "购车意向", "description": "客户表达的意向", "editable": True, "config": {}}
236
+ dataset = {"name": "门店", "meaning": "门店代码", "columns": [
237
+ {"key": "code", "name": "门店编码", "type": "text", "unique_code": True}]}
238
+ row = {"base_version": 1, "cells": {"code": "S001"}}
239
+ plan = {**dataset, "sheet": "门店", "mapping": {"code": 0}}
240
+ code = {"name": "意向摘要", "enabled": False, "output_fields": ["intent"],
241
+ "source": 'import insight\n\ndef run():\n insight.write("intent", "待确认")'}
242
+ preview = {"filters": {"direction": "inbound"}, "fields": ["intent"]}
243
+ task = {**preview, "name": "呼入购车意向", "kind": "llm",
244
+ "prompt": "根据 {{transcript}} 提取购车意向,以 JSON 返回 intent。", "mappings": {"intent": {"key": "intent"}}}
245
+ return {
246
+ "tag-catalog create": {**tag, "parent_id": None}, "tag-catalog update": tag,
247
+ "product-models create": {**model, "code": "series_a"}, "product-models update": model,
248
+ "insight-fields create": {**field, "key": "intent", "kind": "text"}, "insight-fields update": field,
249
+ "insight-fields candidates": {"kind": "model", "config": {"level": "series", "multiple": False}, "page": 1},
250
+ "insight-datasets create": dataset, "insight-datasets update": {**dataset, "base_version": 1},
251
+ "insight-datasets query": {"conditions": [{"key": "code", "value": "S001"}], "returns": ["code"], "page": 1},
252
+ "insight-datasets add-record": row, "insight-datasets update-record": {**row, "base_version": 2},
253
+ "insight-datasets workbook-preview": plan, "insight-datasets workbook-commit": plan,
254
+ "替换已有数据集的 Excel 方案": {**plan, "dataset_key": "ds_store", "base_version": 2},
255
+ "tagging-code create": {**code, "key": "intent_summary"}, "tagging-code update": {**code, "version": 1},
256
+ "tagging-tasks preview": preview, "tagging-tasks create": task, "tagging-tasks update": task,
257
+ "tagging-tasks create(决策模型,loan 为是否字段)": {"name": "贷款意向", "kind": "decision",
258
+ "prompt": "客户是否明确表示需要贷款?", "fields": ["loan"], "threshold": 0.65},
259
+ "tagging-tasks create(绑定已启用的代码)": {"name": "购车意向摘要", "kind": "code", "code_key": "intent_summary"},
260
+ }
@@ -0,0 +1,104 @@
1
+ """Tenant-scoped inspection tasks and frozen call results."""
2
+
3
+ import argparse
4
+ from pathlib import Path
5
+
6
+ from .auth import current
7
+ from .commands import Command, confirm_operation, positive_id, read_object
8
+ from .http import Error, segment
9
+
10
+ BASE = "/inspection/tasks"
11
+ TASK = BASE + "/{task_id}"
12
+ CALL = TASK + "/calls/{call_id}"
13
+
14
+ COMMANDS = {
15
+ "list": Command("GET", BASE, "查询自动任务或临时批次", "--tab temp --mode dry_run"),
16
+ "options": Command("GET", BASE + "/options", "列出已发布评分卡版本及可用筛选字段", ""),
17
+ "get": Command("GET", TASK, "读取配置、状态和执行统计", "12"),
18
+ "preview": Command("POST", BASE + "/preview", "预览临时任务命中数及已有正式结果数", "--json-file filters.json"),
19
+ "create": Command("POST", BASE, "创建并启用自动任务,或立即启动临时批次", "--tab temp --json-file task.json"),
20
+ "update": Command("PUT", TASK, "保存自动任务的完整配置", "12 --json-file task.json"),
21
+ "enable": Command("POST", TASK + "/enable", "启用自动任务", "12"),
22
+ "disable": Command("POST", TASK + "/disable", "停用自动任务", "12"),
23
+ "cancel": Command("POST", TASK + "/cancel", "取消临时任务中尚未开始的通话", "12 --yes"),
24
+ "retry": Command("POST", TASK + "/retry", "重试临时批次中的异常通话", "12 --no-include-fallback --yes"),
25
+ "retry-call": Command("POST", CALL + "/retry", "重跑自动任务中的一通异常通话", "12 CALL001 --yes"),
26
+ "runs": Command("GET", TASK + "/calls", "分页查询通话执行记录", "12 --risk redline"),
27
+ "run-detail": Command("GET", CALL, "查看单通质检快照、得分及证据", "12 CALL001"),
28
+ "debug": Command("POST", CALL + "/models/{pin}/debug", "用临时提示词测试锁定版本的模型",
29
+ "12 CALL001 --pin service_score:1 --json-file debug.json"),
30
+ "download-recording": Command("GET", CALL + "/recording/{source}/audio", "下载单通质检快照录音",
31
+ "12 CALL001 --source SOURCE --output call.mp3"),
32
+ "delete": Command("DELETE", TASK, "删除质检任务", "12 --yes"),
33
+ }
34
+ GROUPS = {"inspection-tasks": ("质检任务", [(key, cmd.purpose, cmd.example) for key, cmd in COMMANDS.items()])}
35
+ MEMBER_ACTIONS = {"list", "options", "get", "runs", "run-detail", "download-recording"}
36
+ CONFIRM_ACTIONS = {"cancel", "retry", "retry-call", "delete"}
37
+
38
+
39
+ def arguments(parser, action):
40
+ add = parser.add_argument
41
+ path = COMMANDS[action].path
42
+ if "{task_id}" in path:
43
+ add("task_id", metavar="task_id", type=positive_id, help="质检任务 ID")
44
+ if "{call_id}" in path:
45
+ add("call_id", metavar="call_id", type=segment, help="执行记录中的平台通话 ID")
46
+ if action in ("list", "create"):
47
+ add("--tab", choices=("auto", "temp"), required=action == "create", default="auto",
48
+ help="auto 为自动任务,temp 为临时批次;查询默认 auto,创建时必填")
49
+ if action in ("list", "runs"):
50
+ add("--page", type=positive_id, default=1, help="页码,默认 1")
51
+ add("--per-page", type=int, choices=(20, 50, 100), default=20, help="每页条数,默认 20")
52
+ if action == "list":
53
+ add("--keyword", help="搜索任务名称或评分卡名称")
54
+ add("--exact", action="store_true", help="精确匹配任务名称")
55
+ add("--mode", choices=("all", "formal", "dry_run"), help="运行模式")
56
+ add("--enabled", choices=("all", "enabled", "disabled"), help="自动任务启用状态")
57
+ add("--status", choices=("all", "queued", "running", "completed", "failed", "cancelled"), help="临时批次状态")
58
+ if action in ("runs", "run-detail"):
59
+ add("--keyword", help="按通话 ID 搜索;单通详情沿用此筛选定位相邻记录")
60
+ add("--risk", choices=("all", "fatal", "redline", "prohibited"), help="触发的风险类型")
61
+ add("--status", choices=("all", "waiting", "pending", "running", "succeeded", "failed", "fallback", "cancelled"),
62
+ help="通话执行状态")
63
+ if action in ("create", "update", "preview", "debug"):
64
+ add("--json-file", required=True, help="UTF-8 JSON 请求体,示例见 guide")
65
+ if action == "debug":
66
+ add("--pin", required=True, type=segment, help="run-detail 返回的模型标识与版本,例如 service_score:1")
67
+ if action == "retry":
68
+ add("--include-fallback", action=argparse.BooleanOptionalAction, default=True, help="默认包含兜底项,--no-include-fallback 仅重试失败或未执行的通话")
69
+ if action == "download-recording":
70
+ add("--source", required=True, type=segment, help="快照中的 recording.source_id")
71
+ add("--output", required=True, help="本地录音路径")
72
+ add("--force", action="store_true", help="覆盖已有文件")
73
+ if action in CONFIRM_ACTIONS:
74
+ add("--yes", action="store_true", help="确认执行,非交互环境必填")
75
+
76
+
77
+ def run(client, vault, args):
78
+ action = args.action
79
+ command = COMMANDS[action]
80
+ path = command.path.format(**vars(args))
81
+ body = read_object(args.json_file) if hasattr(args, "json_file") else None
82
+ if action == "retry":
83
+ body = {"include_fallback": args.include_fallback}
84
+ fields = {"tab": "tab", "keyword": "q", "exact": "exact", "page": "page", "per_page": "pageSize",
85
+ "mode": "mode", "enabled": "enabled", "status": "status", "risk": "risk"}
86
+ params = {key: getattr(args, name) for name, key in fields.items() if getattr(args, name, None) is not None}
87
+ pair = current(client, vault)
88
+ if action not in MEMBER_ACTIONS and pair["profile"]["role"] != "admin":
89
+ raise Error("仅租户管理员可执行此质检任务操作", 3, code="FORBIDDEN", status=403)
90
+ if action in CONFIRM_ACTIONS and not args.yes:
91
+ confirm_operation(client, pair, f"确定{command.purpose}?目标 {path}")
92
+ if action == "download-recording":
93
+ if Path(args.output).exists() and not args.force:
94
+ raise Error("文件已存在,添加 --force 覆盖")
95
+ return client.request("GET", path, token=pair["access_token"], output=args.output, force=args.force)
96
+ return client.request(command.method, path, token=pair["access_token"], body=body, params=params,
97
+ read_only=action == "preview")
98
+
99
+
100
+ def examples():
101
+ filters = {"direction": "outbound", "conditions": []}
102
+ draft = {"name": "外呼试跑", "scorecard_key": "service_card", "version": 1, "mode": "dry_run", "filters": filters}
103
+ return {"inspection-tasks create": draft, "inspection-tasks update": draft, "inspection-tasks preview": filters,
104
+ "inspection-tasks debug": {"prompt": "请根据当前通话转写评价服务质量。"}}