ability-cli 0.3.3 → 0.3.5

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 (3) hide show
  1. package/README.md +0 -1
  2. package/dist/index.js +28 -16
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -130,7 +130,6 @@ ability-cli doctor
130
130
  | `--trace-id <id>` | `traceId`;未指定时自动生成 |
131
131
  | `--timeout <ms>` | 超时(毫秒);不传则用配置文件 `defaults.timeout`,再默认 `30000` |
132
132
  | `--response-type <type>` | `NONE` / `MCP` / `CONVERT`(可选) |
133
- | `--mock-token <token>` | 模拟调用 token(可选,测试) |
134
133
  | `--json` | 以 JSON 展示返回 `data` |
135
134
 
136
135
  ### 下层命令:`raw`
package/dist/index.js CHANGED
@@ -135,14 +135,15 @@ function printAbilityDetail(a) {
135
135
  console.log(JSON.stringify(a.inputSchema, null, 2));
136
136
  }
137
137
  }
138
- function handleApiResponse(res, jsonMode, formatter) {
139
- const hasStandardEnvelope = res && typeof res === "object" && Object.prototype.hasOwnProperty.call(res, "code");
140
- if (hasStandardEnvelope && res.code !== 200) {
141
- const msg = res.msg ?? res.message ?? JSON.stringify(res);
142
- printError(`\u63A5\u53E3\u8FD4\u56DE\u9519\u8BEF: [${res.code}] ${msg}`);
143
- process.exit(4);
138
+ function extractApiPayload(res) {
139
+ if (res && typeof res === "object" && "data" in res) {
140
+ const data = res.data;
141
+ if (data !== void 0) return data;
144
142
  }
145
- const payload = hasStandardEnvelope ? res.data : res;
143
+ return res;
144
+ }
145
+ function handleApiResponse(res, jsonMode, formatter) {
146
+ const payload = extractApiPayload(res);
146
147
  if (jsonMode) {
147
148
  printJson(payload);
148
149
  } else {
@@ -261,7 +262,7 @@ async function apiGet(ctx, path2, params) {
261
262
  console.error(`[verbose] Headers: ${JSON.stringify(headers, null, 2)}`);
262
263
  }
263
264
  const res = await fetch(url, { headers });
264
- const body = await res.json();
265
+ const body = await readJsonResponse(res, ctx);
265
266
  if (ctx.verbose) console.error(`[verbose] Response: ${JSON.stringify(body, null, 2)}`);
266
267
  return body;
267
268
  }
@@ -279,10 +280,22 @@ async function apiPost(ctx, path2, data) {
279
280
  headers,
280
281
  body: JSON.stringify(data)
281
282
  });
282
- const body = await res.json();
283
+ const body = await readJsonResponse(res, ctx);
283
284
  if (ctx.verbose) console.error(`[verbose] Response: ${JSON.stringify(body, null, 2)}`);
284
285
  return body;
285
286
  }
287
+ async function readJsonResponse(res, ctx) {
288
+ const text = await res.text();
289
+ if (!res.ok) {
290
+ throw new Error(`HTTP ${res.status} ${res.statusText}: ${text.slice(0, 500)}`);
291
+ }
292
+ if (!text.trim()) return {};
293
+ try {
294
+ return JSON.parse(text);
295
+ } catch {
296
+ throw new Error(`\u54CD\u5E94\u4E0D\u662F\u5408\u6CD5 JSON (HTTP ${res.status}): ${text.slice(0, 200)}`);
297
+ }
298
+ }
286
299
 
287
300
  // src/commands/raw.ts
288
301
  import fs3 from "fs";
@@ -362,7 +375,7 @@ function registerExecCommand(program2) {
362
375
  program2.command("exec").description("\u6267\u884C\u80FD\u529B").addHelpText(
363
376
  "after",
364
377
  "\n\u63D0\u793A\uFF1A\u5FC5\u987B\u63D0\u4F9B --ability-id \u4E0E --tool-name \u4E8C\u8005\u4E4B\u4E00\uFF1B\u5176\u4F59\u9009\u9879\u8BF4\u660E\u4E2D\u5E26\u300C\u3010\u5FC5\u586B\u3011\u300D\u7684\u4E5F\u5FC5\u987B\u63D0\u4F9B\u3002\n"
365
- ).option("--ability-id <id>", "\u3010\u6761\u4EF6\u5FC5\u586B\u3011\u80FD\u529B ID\uFF1B\u4E0E --tool-name \u81F3\u5C11\u586B\u5176\u4E00").option("--tool-name <name>", "\u3010\u6761\u4EF6\u5FC5\u586B\u3011\u5DE5\u5177\u540D\uFF1B\u4E0E --ability-id \u81F3\u5C11\u586B\u5176\u4E00").option("--params <json>", "\u53C2\u6570 JSON \u5B57\u7B26\u4E32\u6216\u6587\u4EF6\u8DEF\u5F84").option("--device-id <id>", "\u8BBE\u5907ID").option("--message-id <id>", "messageId").option("--trace-id <id>", "traceId").option("--timeout <ms>", "\u8D85\u65F6\uFF08\u6BEB\u79D2\uFF09\uFF1B\u4E0D\u4F20\u5219\u7528\u914D\u7F6E\u6587\u4EF6 defaults.timeout\uFF0C\u9ED8\u8BA4 30000").option("--response-type <type>", "\u8FD4\u56DE\u503C\u5904\u7406\u65B9\u5F0F: NONE | MCP | CONVERT").option("--json", "JSON \u8F93\u51FA").action(async (opts) => {
378
+ ).option("--ability-id <id>", "\u3010\u6761\u4EF6\u5FC5\u586B\u3011\u80FD\u529B ID\uFF1B\u4E0E --tool-name \u81F3\u5C11\u586B\u5176\u4E00").option("--tool-name <name>", "\u3010\u6761\u4EF6\u5FC5\u586B\u3011\u5DE5\u5177\u540D\uFF1B\u4E0E --ability-id \u81F3\u5C11\u586B\u5176\u4E00").option("--params <json>", "\u80FD\u529B\u5165\u53C2 params\uFF1AJSON \u5B57\u7B26\u4E32\u6216\u6587\u4EF6\u8DEF\u5F84").option("--device-id <id>", "\u8BF7\u6C42\u4F53 deviceId\uFF1B\u672A\u6307\u5B9A\u65F6\u7528\u5168\u5C40/\u73AF\u5883\u53D8\u91CF/\u914D\u7F6E\u89E3\u6790\u7ED3\u679C").option("--session-id <id>", "sessionId").option("--tool-call-id <id>", "toolCallId").option("--task-id <id>", "taskId").option("--message-id <id>", "messageId").option("--trace-id <id>", "traceId\uFF1B\u672A\u6307\u5B9A\u65F6\u81EA\u52A8\u751F\u6210").option("--timeout <ms>", "timeoutMillis\uFF08\u6BEB\u79D2\uFF09\uFF1B\u4E0D\u4F20\u5219\u7528 defaults.timeout\uFF0C\u9ED8\u8BA4 30000").option("--response-type <type>", "responseType\uFF1ANONE | MCP | CONVERT").option("--json", "JSON \u8F93\u51FA").action(async (opts) => {
366
379
  const merged = { ...program2.opts(), ...opts };
367
380
  const ctx = buildAgentRequestContext(merged);
368
381
  const cliCfg = loadConfig();
@@ -377,18 +390,17 @@ function registerExecCommand(program2) {
377
390
  }
378
391
  }
379
392
  const body = {
380
- traceId: opts.traceId ?? randomUUID().replace(/-/g, ""),
381
- sessionId: opts.sessionId ?? "",
382
- toolCallId: opts.toolCallId ?? "",
383
- taskId: opts.taskId ?? "",
384
- messageId: opts.messageId ?? "",
393
+ traceId: opts.traceId !== void 0 && String(opts.traceId).trim() !== "" ? String(opts.traceId).trim() : randomUUID().replace(/-/g, ""),
394
+ sessionId: opts.sessionId !== void 0 ? String(opts.sessionId) : "",
395
+ toolCallId: opts.toolCallId !== void 0 ? String(opts.toolCallId) : "",
396
+ taskId: opts.taskId !== void 0 ? String(opts.taskId) : "",
397
+ messageId: opts.messageId !== void 0 ? String(opts.messageId) : "",
385
398
  deviceId: ctx.deviceId ?? "",
386
399
  timeoutMillis,
387
400
  params
388
401
  };
389
402
  applyAbilityOrToolToPayload(opts, body);
390
403
  if (opts.responseType) body.responseType = opts.responseType;
391
- if (opts.mockToken) body.mockToken = opts.mockToken;
392
404
  const res = await apiPost(ctx, agentPath.abilityCall, body);
393
405
  handleApiResponse(res, opts.json, (data) => {
394
406
  printSuccess("\u80FD\u529B\u6267\u884C\u5B8C\u6210");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ability-cli",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "原子能力平台 CLI 工具",
5
5
  "repository": {
6
6
  "type": "git",