@social-data/social-neuron-subscription 1.0.0-beta.3 → 1.0.0-beta.30

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/bin/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
- // ⚠️ 本文件由 backend/app/cli_spec/gen.py 生成,请勿手改;要改改 gen.py 的 _CLI_JS 后重跑 gen。
4
+ // ⚠️ 本文件由 core/cli/gen.py 生成,请勿手改;要改改 gen.py 的 _CLI_JS 后重跑 gen。
5
5
  //
6
6
  // 能力 CLI —— 通用薄运行时(纯 JS,零依赖):一能力一命令、一包、一配置文件,互不影响。
7
7
  // <cmd> <action> [--flags] 调用一条命令
@@ -39,11 +39,17 @@ function resolveConfig() {
39
39
  }
40
40
 
41
41
  function emit(env) {
42
+ const dataExit = env.ok && env.data && env.data.meta && env.data.meta.exit_code;
43
+ const status = env.ok
44
+ ? ([0, 1, 2].includes(dataExit) ? dataExit : 0)
45
+ : (EXIT[env.error && env.error.type] ?? 1);
46
+ // 管道输出为异步写入;自然退出让stdout缓冲排空,保留原有业务退出码。
47
+ process.exitCode = status;
42
48
  process.stdout.write(JSON.stringify(env) + "\n");
43
- process.exit(env.ok ? 0 : (EXIT[env.error && env.error.type] ?? 1));
44
49
  }
45
50
  function fail(type, code, message, extra) {
46
51
  emit({ ok: false, error: { type, code, message, ...(extra || {}) } });
52
+ return true; // parseArgs用此标记短路,错误后不得继续请求接口。
47
53
  }
48
54
 
49
55
  // 网关统一 {code,msg,data}(code=0 成功)→ 归一化 {ok,data|error}。
@@ -61,7 +67,10 @@ function normalize(body) {
61
67
  const type = forgeType(body.code);
62
68
  let message = body.msg || "";
63
69
  if (type === "outdated") message += "(升级后请重新加载 SKILL.md 与 manifest 再调用)";
64
- return { ok: false, error: { type, code: body.code, message } };
70
+ // 透传结构化诊断,并保留顶层 trace_id 的兼容读取。
71
+ const extra = body.data == null ? {} : { data: body.data };
72
+ if (body.data && body.data.trace_id) extra.trace_id = body.data.trace_id;
73
+ return { ok: false, error: { type, code: body.code, message, ...extra } };
65
74
  }
66
75
  return { ok: false, error: { type: "api_error", code: "UNKNOWN", message: JSON.stringify(body).slice(0, 200) } };
67
76
  }
@@ -109,15 +118,63 @@ function printWelcome() {
109
118
  w("──────────────────────────────────────────────");
110
119
  }
111
120
 
121
+ // ── 嵌套对象形状:object(json) / list[object](json) 这类 flag 光看类型名没法拼 JSON,
122
+ // 这里把 input_schema.$defs 里**被引用到的**对象摊开印出来(与 SKILL.md 同源同内容)。
123
+ // 没有这段时,调用方只能靠 422 报错反推字段名。
124
+ function typeStr(node) {
125
+ if (!node || typeof node !== "object") return "any";
126
+ if (typeof node.$ref === "string") return node.$ref.split("/").pop();
127
+ const union = node.anyOf || node.oneOf;
128
+ if (union) {
129
+ const parts = union.filter((n) => n && n.type !== "null").map(typeStr);
130
+ return parts.length ? parts.join("|") : "any";
131
+ }
132
+ if (Array.isArray(node.enum)) return "enum(" + node.enum.join("|") + ")";
133
+ if (node.type === "array") return "list[" + typeStr(node.items) + "]";
134
+ return node.type || "any";
135
+ }
136
+ function refsOf(node, acc) {
137
+ if (!node || typeof node !== "object") return acc;
138
+ if (Array.isArray(node)) { for (const n of node) refsOf(n, acc); return acc; }
139
+ if (typeof node.$ref === "string") acc.add(node.$ref.split("/").pop());
140
+ for (const k of Object.keys(node)) if (k !== "$defs") refsOf(node[k], acc);
141
+ return acc;
142
+ }
143
+ function objectBlocks(schema) {
144
+ const defs = (schema && schema.$defs) || {};
145
+ const queue = [...refsOf((schema && schema.properties) || {}, new Set())], seen = new Set();
146
+ while (queue.length) { // 递归:GroupLevel 会引用自己、Leaf 会被 AndNode 引用
147
+ const name = queue.shift();
148
+ if (seen.has(name) || !defs[name]) continue;
149
+ seen.add(name);
150
+ for (const r of refsOf(defs[name], new Set())) queue.push(r);
151
+ }
152
+ const out = [];
153
+ for (const name of [...seen].sort()) {
154
+ const d = defs[name], props = d.properties;
155
+ if (!props) continue;
156
+ const required = new Set(d.required || []);
157
+ const rows = Object.keys(props).map((k) => [k, typeStr(props[k]), required.has(k) ? "required" : "optional",
158
+ props[k].description || ""]);
159
+ const w0 = Math.max(...rows.map((r) => r[0].length)), w1 = Math.max(...rows.map((r) => r[1].length));
160
+ out.push("对象 " + name + ":" + (d.description ? " " + d.description.split("\n")[0] : ""));
161
+ for (const r of rows) out.push(" " + r[0].padEnd(w0) + " " + r[1].padEnd(w1) + " " + r[2].padEnd(8) + " " + r[3]);
162
+ }
163
+ return out;
164
+ }
165
+
112
166
  function usageOf(cmd) {
113
167
  const line = (f) => " " + f.flag + " " + f.type + (f.required ? " required" : " optional") + " " + (f.desc || "");
114
168
  const req = cmd.flags.filter((f) => f.required), opt = cmd.flags.filter((f) => !f.required);
169
+ const blocks = objectBlocks(cmd.input_schema);
115
170
  return {
116
171
  command: COMMAND + " " + cmd.action,
117
172
  required: req.map((f) => ({ flag: f.flag, type: f.type, desc: f.desc })),
118
173
  optional: opt.map((f) => ({ flag: f.flag, type: f.type, desc: f.desc })),
174
+ objects: blocks, // 同样带进 INVALID_ARGS 报错里,拼错时直接能看到形状
119
175
  example: (COMMAND + " " + cmd.action + " " + req.map((f) => f.flag + " <v>").join(" ")).trim(),
120
- _text: [COMMAND + " " + cmd.action + " — " + cmd.summary, "Inputs:", ...cmd.flags.map(line)].join("\n"),
176
+ _text: [COMMAND + " " + cmd.action + " — " + cmd.summary, "Inputs:", ...cmd.flags.map(line),
177
+ ...(blocks.length ? ["", ...blocks] : [])].join("\n"),
121
178
  };
122
179
  }
123
180
  function printHelp(action) {
@@ -125,12 +182,12 @@ function printHelp(action) {
125
182
  if (action) {
126
183
  const c = findCmd(action);
127
184
  if (!c) return fail("validation", "UNKNOWN_COMMAND", "未知命令 " + action);
128
- const u = usageOf(c); w(u._text); w(""); w("示例:" + u.example); return process.exit(0);
185
+ const u = usageOf(c); w(u._text); w(""); w("示例:" + u.example); process.exitCode = 0; return;
129
186
  }
130
187
  w(COMMAND + " v" + VERSION + " — Social Neuron 能力 CLI(能力:" + CAPABILITY + ";供 AI agent 调用)");
131
188
  w("");
132
189
  w("用法:" + COMMAND + " <action> [--flags]");
133
- w(" 标量 --flag v;数组 --flag a,b;对象 --flag '<json>';写/删/触发加 -y");
190
+ w(" 标量 --flag v;数组/对象一律 JSON:--flag '[\"a\",\"b\"]' / --flag '{\"k\":\"v\"}';写/删/触发加 -y");
134
191
  w("");
135
192
  w("命令:");
136
193
  for (const c of MANIFEST.tools) w(" " + COMMAND + " " + c.action + " — " + c.summary);
@@ -143,7 +200,7 @@ function printHelp(action) {
143
200
  w(" " + COMMAND + " --version");
144
201
  w("");
145
202
  w("鉴权/网关:env NEURON_API_BASE_URL / NEURON_TOKEN 覆盖配置;令牌走头 X-Social-Neuron-CLI-Key。");
146
- process.exit(0);
203
+ process.exitCode = 0;
147
204
  }
148
205
 
149
206
  function parseArgs(cmd, argv) {
@@ -158,16 +215,30 @@ function parseArgs(cmd, argv) {
158
215
  const raw = argv[++i];
159
216
  if (raw === undefined) return { err: fail("validation", "MISSING_VALUE", a + " 缺少取值") };
160
217
  let val = raw;
161
- if (f.type.startsWith("list")) val = raw.split(",").map((s) => s.trim()).filter(Boolean);
162
- else if (f.type.startsWith("object")) { try { val = JSON.parse(raw); } catch { return { err: fail("validation", "BAD_JSON", a + " 需为合法 JSON") }; } }
218
+ if (f.type.indexOf("(json)") >= 0) {
219
+ // 数组/对象一律按 JSON 解析(与 MCP 入参同形)。不做「逗号分隔」这类特例:
220
+ // 同一份文档要能同时用于两个出口,取值写法就只能有一种。
221
+ try { val = JSON.parse(raw); } catch {
222
+ const hint = f.type.startsWith("list") ? "JSON 数组,如 '[\"a\",\"b\"]'" : "JSON 对象,如 '{\"k\":\"v\"}'";
223
+ return { err: fail("validation", "BAD_JSON", a + " 需为 " + hint + "(当前取值:" + raw + ")") };
224
+ }
225
+ if (f.type.startsWith("list") && !Array.isArray(val)) {
226
+ return { err: fail("validation", "BAD_JSON", a + " 需为 JSON 数组,如 '[\"a\",\"b\"]';单个取值也要写成数组") };
227
+ }
228
+ }
163
229
  else if (f.type === "integer") val = Number(raw);
230
+ else if (f.type === "boolean") {
231
+ if (raw !== "true" && raw !== "false") return { err: fail("validation", "BAD_BOOLEAN", a + " 需为 true 或 false") };
232
+ val = raw === "true";
233
+ }
164
234
  args[f.field] = val;
165
235
  }
166
236
  return { args };
167
237
  }
168
238
 
169
239
  async function main() {
170
- const argv = process.argv.slice(2);
240
+ let argv = process.argv.slice(2);
241
+ if (argv.length === 0 && MANIFEST.default_action) argv = [MANIFEST.default_action];
171
242
  if (argv.length === 0 || argv[0] === "--help" || argv[0] === "-h") return printHelp();
172
243
  if (argv[0] === "--version" || argv[0] === "-V") { process.stdout.write(VERSION + "\n"); return; }
173
244
  if (argv[0] === "bind") return doBind(argv.slice(1));
@@ -201,7 +272,7 @@ async function main() {
201
272
  "X-Neuron-Manifest-Rev": String(MANIFEST.revision),
202
273
  },
203
274
  body: JSON.stringify(args),
204
- signal: AbortSignal.timeout(30000),
275
+ signal: AbortSignal.timeout(180000),
205
276
  });
206
277
  const text = await res.text();
207
278
  let body;
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
3
 
4
- // ⚠️ 本文件由 backend/app/cli_spec/gen.py 生成,请勿手改。
4
+ // ⚠️ 本文件由 core/cli/gen.py 生成,请勿手改。
5
5
  // postinstall:把包内 SKILL.md 覆盖写到 OpenClaw 技能目录(OPENCLAW_SKILLS_DIR 或 ~/.openclaw/skills)。
6
6
 
7
7
  const fs = require("fs");