@puyinkai/aike-internal-cli 0.1.0 → 0.2.1
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 +14 -4
- package/aike_internal_cli.py +51 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# aike-internal-cli
|
|
2
2
|
|
|
3
|
-
Agent 在云电脑里调用内部工具的命令行,单文件,只依赖 Python 3
|
|
3
|
+
Agent 在云电脑里调用内部工具的命令行,单文件,只依赖 Python 3 标准库。所有调用都以 MCP `tools/call` 发给 `aike-tool-bff`。
|
|
4
4
|
|
|
5
5
|
## 身份与配置
|
|
6
6
|
|
|
@@ -8,9 +8,12 @@ CLI 读取平台下发的 `/state/runtime-secrets/aike/runtime-env.json`(读
|
|
|
8
8
|
|
|
9
9
|
| Key | 用途 |
|
|
10
10
|
|---|---|
|
|
11
|
-
| `AIKE_TOOL_BFF_URL` |
|
|
12
|
-
| `AIKE_MCP_ACCESS_TOKEN` |
|
|
13
|
-
|
|
11
|
+
| `AIKE_TOOL_BFF_URL` | aike-tool-bff 在 MCP 网关上的完整地址:test `https://mcp.test.wangxiaobao.com/mcp/wow`,生产去掉 `.test`。由 aike-application 下发(配置项 `AIKE_TOOL_BFF_PUBLIC_URL`) |
|
|
12
|
+
| `AIKE_MCP_ACCESS_TOKEN` | MCP 网关令牌。网关校验后以 `X-MCP-Subject-Id` 注入用户,这是唯一的身份来源 |
|
|
13
|
+
|
|
14
|
+
协议:标准 MCP stateless streamable-HTTP,CLI 直接发 `tools/call`。tool-bff 响应为标准 JSON-RPC(不带 pierre 外壳),
|
|
15
|
+
工具失败时 `isError=true`、`structuredContent` 为 `{code, message}`,CLI 原样输出为 `error`。错误码见主仓库
|
|
16
|
+
`docs/contracts/digital-person.md`;本地联调获取令牌见主仓库 `docs/references/mcp-token.md`。
|
|
14
17
|
|
|
15
18
|
## 命令
|
|
16
19
|
|
|
@@ -24,6 +27,13 @@ python3 aike_internal_cli.py digital-person video status --request-id X
|
|
|
24
27
|
|
|
25
28
|
输出固定是一行 JSON:成功为 `{"ok": true, "data": ...}`,失败为 `{"ok": false, "error": {"code", "message"}}`,退出码 1。
|
|
26
29
|
|
|
30
|
+
## 本地联调
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
AIKE_TOOL_BFF_URL=https://mcp.test.wangxiaobao.com/mcp/wow AIKE_MCP_ACCESS_TOKEN=<MCP 令牌> \
|
|
34
|
+
python3 aike_internal_cli.py digital-person status
|
|
35
|
+
```
|
|
36
|
+
|
|
27
37
|
## 发布
|
|
28
38
|
|
|
29
39
|
```bash
|
package/aike_internal_cli.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""aike-internal-cli: thin client for internal agent tools served by aike-tool-bff.
|
|
2
|
+
"""aike-internal-cli: thin client for internal agent tools served by aike-tool-bff over MCP.
|
|
3
3
|
|
|
4
4
|
Zero dependencies (stdlib only). Identity is the platform-issued MCP token; the CLI never
|
|
5
5
|
decides who the user is. All output is one JSON object on stdout; exit code 0 = ok.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
import argparse
|
|
9
|
+
import itertools
|
|
9
10
|
import json
|
|
10
11
|
import os
|
|
11
12
|
import sys
|
|
@@ -14,9 +15,10 @@ import urllib.error
|
|
|
14
15
|
import urllib.request
|
|
15
16
|
import uuid
|
|
16
17
|
|
|
17
|
-
VERSION = "0.1
|
|
18
|
+
VERSION = "0.2.1"
|
|
18
19
|
RUNTIME_ENV_FILE = "/state/runtime-secrets/aike/runtime-env.json"
|
|
19
20
|
VIDEO_POLL_SECONDS = 5
|
|
21
|
+
_ids = itertools.count(1)
|
|
20
22
|
|
|
21
23
|
|
|
22
24
|
def runtime_value(name):
|
|
@@ -33,79 +35,100 @@ def runtime_value(name):
|
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
class ToolError(Exception):
|
|
36
|
-
def __init__(self, code, message
|
|
38
|
+
def __init__(self, code, message):
|
|
37
39
|
super().__init__(message)
|
|
38
40
|
self.code = code
|
|
39
|
-
self.status = status
|
|
40
41
|
|
|
41
42
|
|
|
42
43
|
class Client:
|
|
44
|
+
"""Calls MCP tools/call on the tool server (AIKE_TOOL_BFF_URL is the full MCP endpoint)."""
|
|
45
|
+
|
|
43
46
|
def __init__(self):
|
|
44
|
-
self.
|
|
47
|
+
self.url = runtime_value("AIKE_TOOL_BFF_URL")
|
|
45
48
|
self.token = runtime_value("AIKE_MCP_ACCESS_TOKEN")
|
|
46
|
-
self.
|
|
47
|
-
if not self.base:
|
|
49
|
+
if not self.url:
|
|
48
50
|
raise ToolError("CONFIG_MISSING", "缺少 AIKE_TOOL_BFF_URL")
|
|
49
51
|
if not self.token:
|
|
50
52
|
raise ToolError("CONFIG_MISSING", "缺少 AIKE_MCP_ACCESS_TOKEN,请让用户重新登录 App")
|
|
51
53
|
|
|
52
|
-
def call(self,
|
|
54
|
+
def call(self, tool, arguments=None):
|
|
53
55
|
token = self.token if self.token.lower().startswith("bearer ") else "Bearer " + self.token
|
|
54
56
|
headers = {"Authorization": token, "Content-Type": "application/json",
|
|
55
|
-
"User-Agent": "aike-internal-cli/" + VERSION}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
"Accept": "application/json, text/event-stream", "User-Agent": "aike-internal-cli/" + VERSION}
|
|
58
|
+
body = {"jsonrpc": "2.0", "id": next(_ids), "method": "tools/call",
|
|
59
|
+
"params": {"name": tool, "arguments": arguments or {}}}
|
|
60
|
+
request = urllib.request.Request(self.url, data=json.dumps(body, ensure_ascii=False).encode("utf-8"),
|
|
61
|
+
headers=headers, method="POST")
|
|
60
62
|
try:
|
|
61
63
|
with urllib.request.urlopen(request, timeout=30) as response:
|
|
62
|
-
|
|
64
|
+
payload = parse_body(response.read().decode("utf-8"))
|
|
63
65
|
except urllib.error.HTTPError as error:
|
|
66
|
+
# auth failures come back as HTTP 401/403 with {code, msg}
|
|
64
67
|
try:
|
|
65
68
|
payload = json.load(error)
|
|
66
69
|
except ValueError:
|
|
67
70
|
payload = {}
|
|
68
|
-
raise ToolError(str(payload.get("code") or error.code),
|
|
69
|
-
payload.get("message") or payload.get("msg") or str(error), error.code)
|
|
71
|
+
raise ToolError(str(payload.get("code") or error.code), payload.get("msg") or str(error))
|
|
70
72
|
except urllib.error.URLError as error:
|
|
71
73
|
raise ToolError("NETWORK_ERROR", "工具服务不可达:%s" % error.reason)
|
|
74
|
+
return unwrap(payload)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def parse_body(text):
|
|
78
|
+
"""MCP gateways may answer as JSON or as SSE ("data: {...}" lines); take the JSON-RPC message."""
|
|
79
|
+
stripped = text.strip()
|
|
80
|
+
if stripped.startswith("{"):
|
|
81
|
+
return json.loads(stripped)
|
|
82
|
+
for line in stripped.splitlines():
|
|
83
|
+
if line.startswith("data:"):
|
|
84
|
+
return json.loads(line[len("data:"):].strip())
|
|
85
|
+
raise ToolError("BAD_RESPONSE", "工具服务返回了无法解析的内容")
|
|
72
86
|
|
|
73
87
|
|
|
74
88
|
def unwrap(payload):
|
|
75
|
-
"""
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
89
|
+
"""JSON-RPC error -> protocol error; tool result with isError -> business error code.
|
|
90
|
+
|
|
91
|
+
aike MCP servers return JSON-RPC inside a pierre Result ({code, msg, data}); unwrap it first.
|
|
92
|
+
"""
|
|
93
|
+
if "jsonrpc" not in payload and isinstance(payload.get("data"), dict) and "jsonrpc" in payload["data"]:
|
|
94
|
+
payload = payload["data"]
|
|
95
|
+
if payload.get("error"):
|
|
96
|
+
error = payload["error"]
|
|
97
|
+
raise ToolError("RPC_%s" % error.get("code"), error.get("message", ""))
|
|
98
|
+
result = payload.get("result") or {}
|
|
99
|
+
data = result.get("structuredContent")
|
|
100
|
+
if result.get("isError"):
|
|
101
|
+
data = data or {}
|
|
102
|
+
raise ToolError(data.get("code") or "TOOL_ERROR", data.get("message") or "")
|
|
103
|
+
return data
|
|
79
104
|
|
|
80
105
|
|
|
81
106
|
def cmd_status(client, args):
|
|
82
|
-
return client.call("
|
|
107
|
+
return client.call("aike_digital_person_get")
|
|
83
108
|
|
|
84
109
|
|
|
85
110
|
def cmd_materials(client, args):
|
|
86
|
-
|
|
87
|
-
keep = ("id", "fileName", "durationMs", "width", "height", "createTime")
|
|
88
|
-
return {"materials": [{k: m.get(k) for k in keep} for m in items]}
|
|
111
|
+
return client.call("aike_digital_person_list_materials")
|
|
89
112
|
|
|
90
113
|
|
|
91
114
|
def cmd_create(client, args):
|
|
92
|
-
return client.call("
|
|
115
|
+
return client.call("aike_digital_person_create", {"materialId": args.material_id})
|
|
93
116
|
|
|
94
117
|
|
|
95
118
|
def cmd_video_create(client, args):
|
|
96
119
|
request_id = args.request_id or uuid.uuid4().hex
|
|
97
|
-
job = client.call("
|
|
120
|
+
job = client.call("aike_digital_person_video_create", {"requestId": request_id, "text": args.text})
|
|
98
121
|
deadline = time.monotonic() + args.wait
|
|
99
122
|
while job.get("status") == "GENERATING" and time.monotonic() < deadline:
|
|
100
123
|
time.sleep(VIDEO_POLL_SECONDS)
|
|
101
|
-
job = client.call("
|
|
124
|
+
job = client.call("aike_digital_person_video_get", {"requestId": request_id})
|
|
102
125
|
if job.get("status") == "GENERATING":
|
|
103
126
|
job["message"] = "视频仍在生成,完成后会通过站内信通知用户;也可稍后用 video status 查询"
|
|
104
127
|
return job
|
|
105
128
|
|
|
106
129
|
|
|
107
130
|
def cmd_video_status(client, args):
|
|
108
|
-
return client.call("
|
|
131
|
+
return client.call("aike_digital_person_video_get", {"requestId": args.request_id})
|
|
109
132
|
|
|
110
133
|
|
|
111
134
|
def build_parser():
|