@manturhub/cli 0.8.2 → 0.9.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 +5 -1
- package/bin/cli.js +52 -5
- package/lib/api.js +2 -1
- package/lib/billing-confirm.js +34 -0
- package/lib/params.js +8 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,6 +15,8 @@ manturhub login
|
|
|
15
15
|
printf '%s' "$YOUR_MANTURHUB_KEY" | manturhub login --key-stdin
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
CLI 默认连接生产站 `https://hub.mantur.ai`,Key 也必须在该生产站创建。`hub.mantur.cn` 是独立测试环境,其 Key 不能用于生产站。
|
|
19
|
+
|
|
18
20
|
需要 Node.js ≥ 18。也可免安装运行:`npx -y @manturhub/cli <命令>`。
|
|
19
21
|
|
|
20
22
|
## 快速开始
|
|
@@ -29,7 +31,9 @@ manturhub quote op.text.commerce-copy
|
|
|
29
31
|
manturhub run op.text.commerce-copy --json '{"product_info":"便携榨汁杯,USB 充电,300ml","scene":"product_title","tone":"lively"}'
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
CLI 会在付费调用前按算子实时 schema 校验未知字段、必填项、类型和枚举,校验失败不会发起 invoke
|
|
34
|
+
CLI 会在付费调用前按算子实时 schema 校验未知字段、必填项、类型和枚举,校验失败不会发起 invoke。校验通过后会显示本次预计消耗和计费依据,获得确认才调用;完成后显示实际消耗和退款。批量参数也按整批请求试算。
|
|
35
|
+
|
|
36
|
+
在 Agent 或脚本等非交互环境中,第一次运行只返回报价和 `quote_id`,不会调用或扣费;Agent 向用户确认后,使用提示中的 `--confirm <quote_id>` 执行。报价 5 分钟内有效且只能使用一次。
|
|
33
37
|
|
|
34
38
|
## 主要命令
|
|
35
39
|
|
package/bin/cli.js
CHANGED
|
@@ -10,6 +10,7 @@ import { createReadStream, readFileSync, statSync } from "node:fs";
|
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
11
|
import { dirname, join, basename, extname } from "node:path";
|
|
12
12
|
import { parseDynamicParams, validateParams } from "../lib/params.js";
|
|
13
|
+
import { confirmCharge, formatMantou, printBillingResult } from "../lib/billing-confirm.js";
|
|
13
14
|
|
|
14
15
|
// 本地文件 → MIME(presign 只接受 image/audio/video)
|
|
15
16
|
const MIME_BY_EXT = {
|
|
@@ -67,8 +68,8 @@ function assertFlags(tokens, { value = [], boolean = [] } = {}) {
|
|
|
67
68
|
function assertRunControlFlags(tokens) {
|
|
68
69
|
for (let i = 0; i < tokens.length; i++) {
|
|
69
70
|
const token = tokens[i];
|
|
70
|
-
if (token === "--no-wait" || token.startsWith("--json=") || token.startsWith("--json-file=")) continue;
|
|
71
|
-
if (token === "--json" || token === "--json-file") {
|
|
71
|
+
if (token === "--no-wait" || token.startsWith("--json=") || token.startsWith("--json-file=") || token.startsWith("--confirm=")) continue;
|
|
72
|
+
if (token === "--json" || token === "--json-file" || token === "--confirm") {
|
|
72
73
|
const value = tokens[i + 1];
|
|
73
74
|
if (value === undefined || value.startsWith("--")) throw new Error(`参数 ${token} 缺少值`);
|
|
74
75
|
i++;
|
|
@@ -92,7 +93,7 @@ const HELP = `manturhub — ManturHub 算子广场 CLI v${VERSION}
|
|
|
92
93
|
manturhub ls [--cat <分类>] [--json] 列出上线算子(无需登录)
|
|
93
94
|
manturhub describe <算子ID> [--json] 查看算子入参字段(无需登录)
|
|
94
95
|
manturhub quote <算子ID> 查询实时计费公式(不要使用 Skill 内的历史价格)
|
|
95
|
-
manturhub run <算子ID> --json '{}'
|
|
96
|
+
manturhub run <算子ID> --json '{}' 试算并确认费用后调用(异步算子自动轮询到出结果)
|
|
96
97
|
manturhub run <算子ID> --json-file x.json 从文件读参数(prompt 来自配方/用户时更安全)
|
|
97
98
|
manturhub upload <本地文件> 上传图片/音频/视频 → 公网 URL(喂算子前先转换本地文件)
|
|
98
99
|
manturhub status <poll_url> 查异步任务状态(配合 run --no-wait)
|
|
@@ -161,7 +162,14 @@ async function main() {
|
|
|
161
162
|
`✓ Key 已验证并保存。账号: ${r.json.email || "-"} 余额: ${usdFor(r.json.balance)}(${r.json.balance ?? "-"} 馒头)`
|
|
162
163
|
);
|
|
163
164
|
} else {
|
|
164
|
-
|
|
165
|
+
const base = getBaseUrl();
|
|
166
|
+
const productionHint = new URL(base).hostname === "hub.mantur.ai"
|
|
167
|
+
? " 对外用户请在 https://hub.mantur.ai 创建生产 Key;hub.mantur.cn 的测试 Key 不能用于生产。"
|
|
168
|
+
: "";
|
|
169
|
+
console.error(
|
|
170
|
+
`Key 验证失败(HTTP ${r.status}),未修改本地配置。当前连接:${base}。` +
|
|
171
|
+
`Key 必须由这个站点创建,请确认 Key 是否正确、是否已激活。${productionHint}`
|
|
172
|
+
);
|
|
165
173
|
process.exit(1);
|
|
166
174
|
}
|
|
167
175
|
break;
|
|
@@ -338,9 +346,46 @@ async function main() {
|
|
|
338
346
|
console.error(`参数校验失败: ${error.message}`);
|
|
339
347
|
process.exit(1);
|
|
340
348
|
}
|
|
349
|
+
let quoteId = getFlag("confirm");
|
|
350
|
+
if (!quoteId) {
|
|
351
|
+
const quote = await apiFetch(`/api/v1/operators/${encodeURIComponent(op)}/quote`, {
|
|
352
|
+
method: "POST",
|
|
353
|
+
body,
|
|
354
|
+
});
|
|
355
|
+
if (!quote.ok) {
|
|
356
|
+
console.error(`本次费用试算失败(HTTP ${quote.status}): ${JSON.stringify(quote.json)}`);
|
|
357
|
+
process.exit(1);
|
|
358
|
+
}
|
|
359
|
+
const estimated = Number(quote.json?.estimated_dumplings);
|
|
360
|
+
quoteId = quote.json?.quote_id;
|
|
361
|
+
if (Number.isFinite(estimated) && estimated > 0) {
|
|
362
|
+
if (process.stdin.isTTY && process.stderr.isTTY) {
|
|
363
|
+
if (!(await confirmCharge(quote.json))) {
|
|
364
|
+
console.error("已取消,未调用算子、未扣费。");
|
|
365
|
+
process.exit(2);
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
console.error(JSON.stringify({
|
|
369
|
+
error: "CONFIRMATION_REQUIRED",
|
|
370
|
+
message: `本次预计消耗 ${formatMantou(estimated)},请先取得用户确认`,
|
|
371
|
+
estimated_dumplings: estimated,
|
|
372
|
+
balance: quote.json?.balance,
|
|
373
|
+
formula: quote.json?.formula,
|
|
374
|
+
quote_id: quoteId,
|
|
375
|
+
retry_with: `--confirm ${quoteId}`,
|
|
376
|
+
}, null, 2));
|
|
377
|
+
process.exit(3);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
341
381
|
const r = await apiFetch(
|
|
342
382
|
`/api/v1/operators/${encodeURIComponent(op)}/invoke`,
|
|
343
|
-
{
|
|
383
|
+
{
|
|
384
|
+
method: "POST",
|
|
385
|
+
body,
|
|
386
|
+
timeoutMs: 120000,
|
|
387
|
+
headers: quoteId ? { "X-Mantur-Quote-Id": quoteId } : {},
|
|
388
|
+
}
|
|
344
389
|
);
|
|
345
390
|
// 异步算子(返回 poll_url)默认自动轮询到出结果;--no-wait 只拿 job_id。
|
|
346
391
|
const pollUrl = r.ok && r.json && r.json.poll_url;
|
|
@@ -355,10 +400,12 @@ async function main() {
|
|
|
355
400
|
),
|
|
356
401
|
});
|
|
357
402
|
console.log(JSON.stringify(final, null, 2));
|
|
403
|
+
printBillingResult(final);
|
|
358
404
|
const st = final && final.status;
|
|
359
405
|
if (st === "failed" || st === "error" || (final && final._timeout)) process.exit(1);
|
|
360
406
|
} else {
|
|
361
407
|
console.log(JSON.stringify(r.json, null, 2));
|
|
408
|
+
printBillingResult(r.json);
|
|
362
409
|
if (!r.ok) process.exit(1);
|
|
363
410
|
}
|
|
364
411
|
break;
|
package/lib/api.js
CHANGED
|
@@ -3,7 +3,7 @@ import { getKey, getBaseUrl } from "./config.js";
|
|
|
3
3
|
// Thin REST client against the ManturHub gateway. Public discovery calls may omit auth.
|
|
4
4
|
export async function apiFetch(
|
|
5
5
|
path,
|
|
6
|
-
{ method = "GET", body, key, auth = "required", timeoutMs = 30000 } = {}
|
|
6
|
+
{ method = "GET", body, key, auth = "required", timeoutMs = 30000, headers = {} } = {}
|
|
7
7
|
) {
|
|
8
8
|
const apiKey = key === undefined ? getKey() : key;
|
|
9
9
|
if (auth === "required" && !apiKey) {
|
|
@@ -22,6 +22,7 @@ export async function apiFetch(
|
|
|
22
22
|
headers: {
|
|
23
23
|
...(includeKey && apiKey ? { "x-api-key": apiKey } : {}),
|
|
24
24
|
...(body ? { "Content-Type": "application/json" } : {}),
|
|
25
|
+
...headers,
|
|
25
26
|
},
|
|
26
27
|
body: body ? JSON.stringify(body) : undefined,
|
|
27
28
|
signal: AbortSignal.timeout(timeoutMs),
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createInterface } from "node:readline/promises";
|
|
2
|
+
|
|
3
|
+
export function formatMantou(value) {
|
|
4
|
+
const number = Number(value);
|
|
5
|
+
return Number.isFinite(number) ? `${number} 馒头($${(number * 0.01).toFixed(2)} USD)` : "未知";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function confirmCharge(quote, { input = process.stdin, output = process.stderr } = {}) {
|
|
9
|
+
output.write(`\n⚠️ 本次预计消耗:${formatMantou(quote.estimated_dumplings)}\n`);
|
|
10
|
+
if (quote.formula) output.write(`计费依据:${quote.formula}\n`);
|
|
11
|
+
if (Number.isFinite(Number(quote.balance))) output.write(`当前余额:${quote.balance} 馒头\n`);
|
|
12
|
+
const rl = createInterface({ input, output });
|
|
13
|
+
try {
|
|
14
|
+
const answer = (await rl.question("是否继续?[y/N] ")).trim().toLowerCase();
|
|
15
|
+
return answer === "y" || answer === "yes";
|
|
16
|
+
} finally {
|
|
17
|
+
rl.close();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function printBillingResult(result, output = process.stderr) {
|
|
22
|
+
const billing = result?._billing;
|
|
23
|
+
if (!billing) return;
|
|
24
|
+
const estimated = Number(billing.estimated_dumplings);
|
|
25
|
+
const charged = Number(billing.charged_dumplings);
|
|
26
|
+
const refunded = Number(billing.refunded_dumplings);
|
|
27
|
+
if (billing.final) {
|
|
28
|
+
output.write(`\n✓ 本次实际消耗:${formatMantou(charged)}\n`);
|
|
29
|
+
if (Number.isFinite(refunded) && refunded > 0) output.write(` 已退款:${formatMantou(refunded)}\n`);
|
|
30
|
+
if (Number.isFinite(estimated) && estimated !== charged) output.write(` 调用前预计:${formatMantou(estimated)}\n`);
|
|
31
|
+
} else {
|
|
32
|
+
output.write(`\n⏳ 已预扣:${formatMantou(charged)},任务完成后以最终结算为准\n`);
|
|
33
|
+
}
|
|
34
|
+
}
|
package/lib/params.js
CHANGED
|
@@ -85,6 +85,14 @@ export function parseDynamicParams(tokens) {
|
|
|
85
85
|
for (let i = 0; i < tokens.length; i++) {
|
|
86
86
|
const token = tokens[i];
|
|
87
87
|
if (token === "--no-wait") continue;
|
|
88
|
+
if (token === "--confirm") {
|
|
89
|
+
if (tokens[i + 1] === undefined || tokens[i + 1].startsWith("--")) {
|
|
90
|
+
throw new Error("参数 --confirm 缺少值");
|
|
91
|
+
}
|
|
92
|
+
i++;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (token?.startsWith("--confirm=")) continue;
|
|
88
96
|
if (!token?.startsWith("--")) throw new Error(`无法识别的参数: ${token}`);
|
|
89
97
|
const equals = token.indexOf("=");
|
|
90
98
|
if (equals > 2) {
|