@puyinkai/aike-internal-cli 0.1.0 → 0.2.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 +2 -2
- package/aike_internal_cli.py +51 -25
- 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,7 +8,7 @@ CLI 读取平台下发的 `/state/runtime-secrets/aike/runtime-env.json`(读
|
|
|
8
8
|
|
|
9
9
|
| Key | 用途 |
|
|
10
10
|
|---|---|
|
|
11
|
-
| `AIKE_TOOL_BFF_URL` |
|
|
11
|
+
| `AIKE_TOOL_BFF_URL` | 工具服务的 MCP 地址(完整 URL,如 `https://mcp.test.wangxiaobao.com/mcp/aike-tool`),由 aike-application 同步下发 |
|
|
12
12
|
| `AIKE_MCP_ACCESS_TOKEN` | 身份凭证,bff 以它为准 |
|
|
13
13
|
| `AIKE_USER_ID` | 交叉校验,与 token 不一致时 bff 拒绝 |
|
|
14
14
|
|
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.
|
|
18
|
+
VERSION = "0.2.0"
|
|
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,103 @@ 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
49
|
self.user_id = runtime_value("AIKE_USER_ID")
|
|
47
|
-
if not self.
|
|
50
|
+
if not self.url:
|
|
48
51
|
raise ToolError("CONFIG_MISSING", "缺少 AIKE_TOOL_BFF_URL")
|
|
49
52
|
if not self.token:
|
|
50
53
|
raise ToolError("CONFIG_MISSING", "缺少 AIKE_MCP_ACCESS_TOKEN,请让用户重新登录 App")
|
|
51
54
|
|
|
52
|
-
def call(self,
|
|
55
|
+
def call(self, tool, arguments=None):
|
|
53
56
|
token = self.token if self.token.lower().startswith("bearer ") else "Bearer " + self.token
|
|
54
57
|
headers = {"Authorization": token, "Content-Type": "application/json",
|
|
55
|
-
"User-Agent": "aike-internal-cli/" + VERSION}
|
|
58
|
+
"Accept": "application/json, text/event-stream", "User-Agent": "aike-internal-cli/" + VERSION}
|
|
56
59
|
if self.user_id:
|
|
57
60
|
headers["X-Aike-User-Id"] = self.user_id
|
|
58
|
-
|
|
59
|
-
|
|
61
|
+
body = {"jsonrpc": "2.0", "id": next(_ids), "method": "tools/call",
|
|
62
|
+
"params": {"name": tool, "arguments": arguments or {}}}
|
|
63
|
+
request = urllib.request.Request(self.url, data=json.dumps(body, ensure_ascii=False).encode("utf-8"),
|
|
64
|
+
headers=headers, method="POST")
|
|
60
65
|
try:
|
|
61
66
|
with urllib.request.urlopen(request, timeout=30) as response:
|
|
62
|
-
|
|
67
|
+
payload = parse_body(response.read().decode("utf-8"))
|
|
63
68
|
except urllib.error.HTTPError as error:
|
|
69
|
+
# auth failures come back as HTTP 401/403 with {code, msg}
|
|
64
70
|
try:
|
|
65
71
|
payload = json.load(error)
|
|
66
72
|
except ValueError:
|
|
67
73
|
payload = {}
|
|
68
|
-
raise ToolError(str(payload.get("code") or error.code),
|
|
69
|
-
payload.get("message") or payload.get("msg") or str(error), error.code)
|
|
74
|
+
raise ToolError(str(payload.get("code") or error.code), payload.get("msg") or str(error))
|
|
70
75
|
except urllib.error.URLError as error:
|
|
71
76
|
raise ToolError("NETWORK_ERROR", "工具服务不可达:%s" % error.reason)
|
|
77
|
+
return unwrap(payload)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def parse_body(text):
|
|
81
|
+
"""MCP gateways may answer as JSON or as SSE ("data: {...}" lines); take the JSON-RPC message."""
|
|
82
|
+
stripped = text.strip()
|
|
83
|
+
if stripped.startswith("{"):
|
|
84
|
+
return json.loads(stripped)
|
|
85
|
+
for line in stripped.splitlines():
|
|
86
|
+
if line.startswith("data:"):
|
|
87
|
+
return json.loads(line[len("data:"):].strip())
|
|
88
|
+
raise ToolError("BAD_RESPONSE", "工具服务返回了无法解析的内容")
|
|
72
89
|
|
|
73
90
|
|
|
74
91
|
def unwrap(payload):
|
|
75
|
-
"""
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
92
|
+
"""JSON-RPC error -> protocol error; tool result with isError -> business error code.
|
|
93
|
+
|
|
94
|
+
aike MCP servers return JSON-RPC inside a pierre Result ({code, msg, data}); unwrap it first.
|
|
95
|
+
"""
|
|
96
|
+
if "jsonrpc" not in payload and isinstance(payload.get("data"), dict) and "jsonrpc" in payload["data"]:
|
|
97
|
+
payload = payload["data"]
|
|
98
|
+
if payload.get("error"):
|
|
99
|
+
error = payload["error"]
|
|
100
|
+
raise ToolError("RPC_%s" % error.get("code"), error.get("message", ""))
|
|
101
|
+
result = payload.get("result") or {}
|
|
102
|
+
data = result.get("structuredContent")
|
|
103
|
+
if result.get("isError"):
|
|
104
|
+
data = data or {}
|
|
105
|
+
raise ToolError(data.get("code") or "TOOL_ERROR", data.get("message") or "")
|
|
106
|
+
return data
|
|
79
107
|
|
|
80
108
|
|
|
81
109
|
def cmd_status(client, args):
|
|
82
|
-
return client.call("
|
|
110
|
+
return client.call("aike_digital_person_get")
|
|
83
111
|
|
|
84
112
|
|
|
85
113
|
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]}
|
|
114
|
+
return client.call("aike_digital_person_list_materials")
|
|
89
115
|
|
|
90
116
|
|
|
91
117
|
def cmd_create(client, args):
|
|
92
|
-
return client.call("
|
|
118
|
+
return client.call("aike_digital_person_create", {"materialId": args.material_id})
|
|
93
119
|
|
|
94
120
|
|
|
95
121
|
def cmd_video_create(client, args):
|
|
96
122
|
request_id = args.request_id or uuid.uuid4().hex
|
|
97
|
-
job = client.call("
|
|
123
|
+
job = client.call("aike_digital_person_video_create", {"requestId": request_id, "text": args.text})
|
|
98
124
|
deadline = time.monotonic() + args.wait
|
|
99
125
|
while job.get("status") == "GENERATING" and time.monotonic() < deadline:
|
|
100
126
|
time.sleep(VIDEO_POLL_SECONDS)
|
|
101
|
-
job = client.call("
|
|
127
|
+
job = client.call("aike_digital_person_video_get", {"requestId": request_id})
|
|
102
128
|
if job.get("status") == "GENERATING":
|
|
103
129
|
job["message"] = "视频仍在生成,完成后会通过站内信通知用户;也可稍后用 video status 查询"
|
|
104
130
|
return job
|
|
105
131
|
|
|
106
132
|
|
|
107
133
|
def cmd_video_status(client, args):
|
|
108
|
-
return client.call("
|
|
134
|
+
return client.call("aike_digital_person_video_get", {"requestId": args.request_id})
|
|
109
135
|
|
|
110
136
|
|
|
111
137
|
def build_parser():
|