@puyinkai/aike-internal-cli 0.0.0-stage → 0.1.0

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.
package/README.md CHANGED
@@ -1,3 +1,32 @@
1
- # Temporary Holding Version
1
+ # aike-internal-cli
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Agent 在云电脑里调用内部工具的命令行,单文件,只依赖 Python 3 标准库。所有调用都经过 `aike-tool-bff`。
4
+
5
+ ## 身份与配置
6
+
7
+ CLI 读取平台下发的 `/state/runtime-secrets/aike/runtime-env.json`(读不到时用进程环境变量):
8
+
9
+ | Key | 用途 |
10
+ |---|---|
11
+ | `AIKE_TOOL_BFF_URL` | 工具服务地址(新增,需加入 aike-application 的环境同步) |
12
+ | `AIKE_MCP_ACCESS_TOKEN` | 身份凭证,bff 以它为准 |
13
+ | `AIKE_USER_ID` | 交叉校验,与 token 不一致时 bff 拒绝 |
14
+
15
+ ## 命令
16
+
17
+ ```bash
18
+ python3 aike_internal_cli.py digital-person status
19
+ python3 aike_internal_cli.py digital-person materials
20
+ python3 aike_internal_cli.py digital-person create --material-id 123
21
+ python3 aike_internal_cli.py digital-person video create --text "大家好" [--request-id X] [--wait 180]
22
+ python3 aike_internal_cli.py digital-person video status --request-id X
23
+ ```
24
+
25
+ 输出固定是一行 JSON:成功为 `{"ok": true, "data": ...}`,失败为 `{"ok": false, "error": {"code", "message"}}`,退出码 1。
26
+
27
+ ## 发布
28
+
29
+ ```bash
30
+ python3 test_aike_internal_cli.py
31
+ shasum -a 256 aike_internal_cli.py # 与文件一起发布,写进 Skill
32
+ ```
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/env python3
2
+ """aike-internal-cli: thin client for internal agent tools served by aike-tool-bff.
3
+
4
+ Zero dependencies (stdlib only). Identity is the platform-issued MCP token; the CLI never
5
+ decides who the user is. All output is one JSON object on stdout; exit code 0 = ok.
6
+ """
7
+
8
+ import argparse
9
+ import json
10
+ import os
11
+ import sys
12
+ import time
13
+ import urllib.error
14
+ import urllib.request
15
+ import uuid
16
+
17
+ VERSION = "0.1.0"
18
+ RUNTIME_ENV_FILE = "/state/runtime-secrets/aike/runtime-env.json"
19
+ VIDEO_POLL_SECONDS = 5
20
+
21
+
22
+ def runtime_value(name):
23
+ """runtime-env.json first (cloud-agent does not always export it), then process env."""
24
+ path = os.environ.get("AIKE_RUNTIME_ENV_FILE", RUNTIME_ENV_FILE)
25
+ try:
26
+ with open(path, encoding="utf-8") as f:
27
+ value = json.load(f).get(name)
28
+ if value not in (None, ""):
29
+ return str(value)
30
+ except (OSError, ValueError):
31
+ pass
32
+ return os.environ.get(name, "")
33
+
34
+
35
+ class ToolError(Exception):
36
+ def __init__(self, code, message, status=None):
37
+ super().__init__(message)
38
+ self.code = code
39
+ self.status = status
40
+
41
+
42
+ class Client:
43
+ def __init__(self):
44
+ self.base = runtime_value("AIKE_TOOL_BFF_URL").rstrip("/")
45
+ self.token = runtime_value("AIKE_MCP_ACCESS_TOKEN")
46
+ self.user_id = runtime_value("AIKE_USER_ID")
47
+ if not self.base:
48
+ raise ToolError("CONFIG_MISSING", "缺少 AIKE_TOOL_BFF_URL")
49
+ if not self.token:
50
+ raise ToolError("CONFIG_MISSING", "缺少 AIKE_MCP_ACCESS_TOKEN,请让用户重新登录 App")
51
+
52
+ def call(self, method, path, body=None):
53
+ token = self.token if self.token.lower().startswith("bearer ") else "Bearer " + self.token
54
+ headers = {"Authorization": token, "Content-Type": "application/json",
55
+ "User-Agent": "aike-internal-cli/" + VERSION}
56
+ if self.user_id:
57
+ headers["X-Aike-User-Id"] = self.user_id
58
+ data = None if body is None else json.dumps(body, ensure_ascii=False).encode("utf-8")
59
+ request = urllib.request.Request(self.base + path, data=data, headers=headers, method=method)
60
+ try:
61
+ with urllib.request.urlopen(request, timeout=30) as response:
62
+ return unwrap(json.load(response))
63
+ except urllib.error.HTTPError as error:
64
+ try:
65
+ payload = json.load(error)
66
+ except ValueError:
67
+ payload = {}
68
+ raise ToolError(str(payload.get("code") or error.code),
69
+ payload.get("message") or payload.get("msg") or str(error), error.code)
70
+ except urllib.error.URLError as error:
71
+ raise ToolError("NETWORK_ERROR", "工具服务不可达:%s" % error.reason)
72
+
73
+
74
+ def unwrap(payload):
75
+ """Persona responses are pierre Result {code, msg, data}; bff errors are {code, message}."""
76
+ if isinstance(payload, dict) and "data" in payload:
77
+ return payload["data"]
78
+ return payload
79
+
80
+
81
+ def cmd_status(client, args):
82
+ return client.call("GET", "/tools/digital-person")
83
+
84
+
85
+ def cmd_materials(client, args):
86
+ items = client.call("GET", "/tools/digital-person/materials") or []
87
+ keep = ("id", "fileName", "durationMs", "width", "height", "createTime")
88
+ return {"materials": [{k: m.get(k) for k in keep} for m in items]}
89
+
90
+
91
+ def cmd_create(client, args):
92
+ return client.call("POST", "/tools/digital-person", {"materialId": args.material_id})
93
+
94
+
95
+ def cmd_video_create(client, args):
96
+ request_id = args.request_id or uuid.uuid4().hex
97
+ job = client.call("POST", "/tools/digital-person/videos", {"requestId": request_id, "text": args.text})
98
+ deadline = time.monotonic() + args.wait
99
+ while job.get("status") == "GENERATING" and time.monotonic() < deadline:
100
+ time.sleep(VIDEO_POLL_SECONDS)
101
+ job = client.call("GET", "/tools/digital-person/videos/" + request_id)
102
+ if job.get("status") == "GENERATING":
103
+ job["message"] = "视频仍在生成,完成后会通过站内信通知用户;也可稍后用 video status 查询"
104
+ return job
105
+
106
+
107
+ def cmd_video_status(client, args):
108
+ return client.call("GET", "/tools/digital-person/videos/" + args.request_id)
109
+
110
+
111
+ def build_parser():
112
+ parser = argparse.ArgumentParser(prog="aike-internal-cli")
113
+ parser.add_argument("--version", action="version", version=VERSION)
114
+ groups = parser.add_subparsers(dest="group", required=True)
115
+
116
+ dp = groups.add_parser("digital-person", help="数字人形象与视频").add_subparsers(dest="action", required=True)
117
+ dp.add_parser("status", help="查询本人数字人形象状态").set_defaults(func=cmd_status)
118
+ dp.add_parser("materials", help="列出本人形象库里可用于训练的视频").set_defaults(func=cmd_materials)
119
+ create = dp.add_parser("create", help="用形象库视频生成数字人形象(异步,完成后站内信通知)")
120
+ create.add_argument("--material-id", type=int, required=True)
121
+ create.set_defaults(func=cmd_create)
122
+
123
+ video = dp.add_parser("video", help="数字人视频").add_subparsers(dest="video_action", required=True)
124
+ vcreate = video.add_parser("create", help="用本人数字人生成口播视频")
125
+ vcreate.add_argument("--text", required=True, help="口播文本,不超过 3900 字")
126
+ vcreate.add_argument("--request-id", help="幂等键;重试时传同一个值")
127
+ vcreate.add_argument("--wait", type=int, default=180, help="最多等待秒数,默认 180")
128
+ vcreate.set_defaults(func=cmd_video_create)
129
+ vstatus = video.add_parser("status", help="查询视频任务")
130
+ vstatus.add_argument("--request-id", required=True)
131
+ vstatus.set_defaults(func=cmd_video_status)
132
+ return parser
133
+
134
+
135
+ def main(argv=None):
136
+ args = build_parser().parse_args(argv)
137
+ try:
138
+ result = {"ok": True, "data": args.func(Client(), args)}
139
+ code = 0
140
+ except ToolError as error:
141
+ result = {"ok": False, "error": {"code": error.code, "message": str(error)}}
142
+ code = 1
143
+ print(json.dumps(result, ensure_ascii=False, default=str))
144
+ return code
145
+
146
+
147
+ if __name__ == "__main__":
148
+ sys.exit(main())
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env node
2
+ // Runs the bundled Python CLI with the same arguments and exit code.
3
+ const { spawnSync } = require("child_process");
4
+ const path = require("path");
5
+
6
+ const script = path.join(__dirname, "..", "aike_internal_cli.py");
7
+ for (const python of ["python3", "python"]) {
8
+ const result = spawnSync(python, [script, ...process.argv.slice(2)], { stdio: "inherit" });
9
+ if (result.error && result.error.code === "ENOENT") continue;
10
+ process.exit(result.status === null ? 1 : result.status);
11
+ }
12
+ console.log(JSON.stringify({ ok: false, error: { code: "PYTHON_MISSING", message: "需要 Python 3" } }));
13
+ process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,20 @@
1
1
  {
2
2
  "name": "@puyinkai/aike-internal-cli",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Thin client for AIKE internal agent tools (requires Python 3)",
5
+ "bin": {
6
+ "aike-internal-cli": "bin/aike-internal-cli.js"
7
+ },
8
+ "files": [
9
+ "bin/",
10
+ "aike_internal_cli.py",
11
+ "README.md"
12
+ ],
13
+ "engines": {
14
+ "node": ">=16"
15
+ },
16
+ "license": "UNLICENSED",
17
+ "publishConfig": {
18
+ "access": "public"
19
+ }
20
+ }