@lingjingai/lj-awb-cli 0.3.11
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 +314 -0
- package/install.mjs +41 -0
- package/package.json +40 -0
- package/packages/awb-cli/README.md +19 -0
- package/packages/awb-cli/bin/lj-awb +19 -0
- package/packages/awb-cli/bin/lj-awb.js +11 -0
- package/packages/awb-cli/package.json +18 -0
- package/packages/awb-core/README.md +12 -0
- package/packages/awb-core/package.json +20 -0
- package/packages/awb-core/src/api.js +349 -0
- package/packages/awb-core/src/auth.js +80 -0
- package/packages/awb-core/src/commands.js +817 -0
- package/packages/awb-core/src/common.js +508 -0
- package/packages/awb-core/src/output.js +758 -0
- package/packages/awb-core/src/services.js +2966 -0
- package/packages/awb-core/src/standalone.js +820 -0
- package/skills/lj-awb/SKILL.md +97 -0
- package/skills/lj-awb/VERSION +1 -0
- package/skills/lj-awb/compat.json +6 -0
- package/skills/lj-awb/modules/account.md +17 -0
- package/skills/lj-awb/modules/asset.md +21 -0
- package/skills/lj-awb/modules/auth.md +23 -0
- package/skills/lj-awb/modules/create-contract.md +112 -0
- package/skills/lj-awb/modules/credits.md +20 -0
- package/skills/lj-awb/modules/evals.md +152 -0
- package/skills/lj-awb/modules/image.md +58 -0
- package/skills/lj-awb/modules/model.md +96 -0
- package/skills/lj-awb/modules/project.md +24 -0
- package/skills/lj-awb/modules/subject.md +22 -0
- package/skills/lj-awb/modules/task-manual.md +142 -0
- package/skills/lj-awb/modules/task.md +19 -0
- package/skills/lj-awb/modules/upload.md +18 -0
- package/skills/lj-awb/modules/video.md +81 -0
- package/skills/lj-awb/modules/workflows.md +223 -0
- package/skills/lj-awb/references/model-options-read.md +43 -0
- package/skills/lj-awb/scripts/resolve-lj-awb-cmd.sh +10 -0
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { LingjingAwbCliError, createEnvelope, toBool, toInt } from './common.js';
|
|
5
|
+
import { registerAwbCommands } from './commands.js';
|
|
6
|
+
import { formatTextError, formatTextOutput, normalizeJsonData } from './output.js';
|
|
7
|
+
|
|
8
|
+
function normalizeKey(value) {
|
|
9
|
+
return String(value || '').replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function formatOptionName(value) {
|
|
13
|
+
return `--${String(value || '').replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`)}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function commandPrefix() {
|
|
17
|
+
return process.env.LINGJING_AWB_COMMAND_PREFIX || 'lj-awb';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function renderCliText(value) {
|
|
21
|
+
return String(value ?? '').replace(/\blj-awb\b/g, commandPrefix());
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function shellArg(value) {
|
|
25
|
+
const text = String(value ?? '');
|
|
26
|
+
return /^[A-Za-z0-9_./:=@,+-]+$/.test(text) ? text : JSON.stringify(text);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function appendCommandOption(parts, key, value) {
|
|
30
|
+
if (value === undefined || value === null || value === '') return;
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
for (const item of value) appendCommandOption(parts, key, item);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const flag = formatOptionName(key);
|
|
36
|
+
if (value === true) {
|
|
37
|
+
parts.push(flag);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
parts.push(flag, shellArg(value));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function commandInvocation(commandName, kwargs = {}, options = {}) {
|
|
44
|
+
const omit = new Set(options.omit || []);
|
|
45
|
+
const sensitive = new Set(['accessKey']);
|
|
46
|
+
const next = { ...kwargs, ...(options.set || {}) };
|
|
47
|
+
const parts = [commandPrefix(), ...String(commandName || '').split(' ').filter(Boolean)];
|
|
48
|
+
for (const [key, value] of Object.entries(next)) {
|
|
49
|
+
if (omit.has(key) || sensitive.has(key)) continue;
|
|
50
|
+
appendCommandOption(parts, key, value);
|
|
51
|
+
}
|
|
52
|
+
return parts.join(' ');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function buildOutputContext(command, kwargs = {}) {
|
|
56
|
+
const optionKeys = new Set((command.args || []).map((arg) => normalizeKey(arg.name)));
|
|
57
|
+
const canSuggestDryRunExecution = command.name !== 'auth login-key';
|
|
58
|
+
const withoutDryRun = { omit: ['dryRun'] };
|
|
59
|
+
const isModelList = command.name === 'model image-models' || command.name === 'model video-models';
|
|
60
|
+
const modelListAll = isModelList && toBool(kwargs.all);
|
|
61
|
+
const modelListLimit = isModelList
|
|
62
|
+
? (modelListAll ? Number.POSITIVE_INFINITY : Math.max(1, toInt(kwargs.limit, 8)))
|
|
63
|
+
: undefined;
|
|
64
|
+
const modelListMoreCommand = isModelList && !modelListAll
|
|
65
|
+
? commandInvocation(command.name, kwargs, { omit: ['limit'], set: { all: true } })
|
|
66
|
+
: null;
|
|
67
|
+
return {
|
|
68
|
+
jsonCommand: `${commandInvocation(command.name, kwargs)} -f json`,
|
|
69
|
+
executeCommand: canSuggestDryRunExecution && toBool(kwargs.dryRun) ? commandInvocation(command.name, kwargs, withoutDryRun) : null,
|
|
70
|
+
confirmCommand: canSuggestDryRunExecution && toBool(kwargs.dryRun) && optionKeys.has('yes')
|
|
71
|
+
? commandInvocation(command.name, kwargs, { omit: ['dryRun'], set: { yes: true } })
|
|
72
|
+
: null,
|
|
73
|
+
...(isModelList ? { listLimit: modelListLimit, moreCommand: modelListMoreCommand } : {}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function assignKwarg(kwargs, key, value) {
|
|
78
|
+
if (kwargs[key] === undefined) {
|
|
79
|
+
kwargs[key] = value;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(kwargs[key])) {
|
|
83
|
+
kwargs[key].push(value);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
kwargs[key] = [kwargs[key], value];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function parseArgv(argv) {
|
|
90
|
+
const commandParts = [];
|
|
91
|
+
const kwargs = {};
|
|
92
|
+
let format = 'text';
|
|
93
|
+
|
|
94
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
95
|
+
const token = argv[index];
|
|
96
|
+
if (token === '-f' || token === '--format') {
|
|
97
|
+
format = argv[index + 1] || 'json';
|
|
98
|
+
index += 1;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (token === '--json') {
|
|
102
|
+
format = 'json';
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (token.startsWith('--')) {
|
|
106
|
+
const raw = token.slice(2);
|
|
107
|
+
const eqIndex = raw.indexOf('=');
|
|
108
|
+
const keyPart = eqIndex >= 0 ? raw.slice(0, eqIndex) : raw;
|
|
109
|
+
const inlineValue = eqIndex >= 0 ? raw.slice(eqIndex + 1) : undefined;
|
|
110
|
+
const key = normalizeKey(keyPart);
|
|
111
|
+
if (inlineValue !== undefined) {
|
|
112
|
+
assignKwarg(kwargs, key, inlineValue);
|
|
113
|
+
} else if (argv[index + 1] && !argv[index + 1].startsWith('-')) {
|
|
114
|
+
assignKwarg(kwargs, key, argv[index + 1]);
|
|
115
|
+
index += 1;
|
|
116
|
+
} else {
|
|
117
|
+
assignKwarg(kwargs, key, true);
|
|
118
|
+
}
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
commandParts.push(token);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { commandName: commandParts.join(' '), kwargs, format };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function readVersion() {
|
|
128
|
+
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
129
|
+
const packagePath = path.join(currentDir, '..', 'package.json');
|
|
130
|
+
const pkg = JSON.parse(await fs.readFile(packagePath, 'utf8'));
|
|
131
|
+
return pkg.version || 'unknown';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const VIRTUAL_COMMANDS = [
|
|
135
|
+
{
|
|
136
|
+
name: 'schema',
|
|
137
|
+
virtual: 'schema',
|
|
138
|
+
description: [
|
|
139
|
+
'输出机器可读命令 schema,供 Agent / 脚本理解命令、参数和示例',
|
|
140
|
+
'',
|
|
141
|
+
'Examples:',
|
|
142
|
+
' lj-awb schema -f json',
|
|
143
|
+
' lj-awb schema --domain video -f json',
|
|
144
|
+
' lj-awb schema --domain image --command create -f json',
|
|
145
|
+
'',
|
|
146
|
+
'Hint: Agent 应优先读取 schema,而不是解析自然语言 help。',
|
|
147
|
+
].join('\n'),
|
|
148
|
+
args: [
|
|
149
|
+
{ name: 'domain', valueName: 'name', description: '按命令域过滤,如 image / video / task / system' },
|
|
150
|
+
{ name: 'command', valueName: 'name', description: '按子命令过滤,如 create / wait / doctor' },
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
const RENAMED_COMMAND_HINTS = {
|
|
156
|
+
'workspace me': 'account info',
|
|
157
|
+
'workspace teams': 'account teams',
|
|
158
|
+
'workspace team-select': 'account switch-team',
|
|
159
|
+
'workspace project-groups': 'project list',
|
|
160
|
+
'workspace project-group-current': 'project current',
|
|
161
|
+
'workspace project-group-select': 'project use',
|
|
162
|
+
'workspace project-group-users': 'project users',
|
|
163
|
+
'workspace project-group-create': 'project create',
|
|
164
|
+
'workspace project-group-update': 'project update',
|
|
165
|
+
'workspace project-group-ensure': 'project ensure',
|
|
166
|
+
'billing points': 'credits balance',
|
|
167
|
+
'billing usage-summary': 'credits usage',
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
function buildRegistry() {
|
|
171
|
+
const commands = [];
|
|
172
|
+
registerAwbCommands((spec) => commands.push(spec));
|
|
173
|
+
return [...commands, ...VIRTUAL_COMMANDS];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function commandGroup(command) {
|
|
177
|
+
const parts = String(command.name || '').split(' ').filter(Boolean);
|
|
178
|
+
return parts.length === 1 ? 'system' : parts[0];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function commandSubcommand(command) {
|
|
182
|
+
const parts = String(command.name || '').split(' ').filter(Boolean);
|
|
183
|
+
return parts.length === 1 ? parts[0] : parts.slice(1).join(' ');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const GROUP_DESCRIPTIONS = {
|
|
187
|
+
system: '系统:环境体检和机器可读命令 schema',
|
|
188
|
+
auth: '认证:登录、查看和清空本地 access key',
|
|
189
|
+
account: '账号:查看账号和团队',
|
|
190
|
+
project: '项目:查看、切换、创建和更新项目组',
|
|
191
|
+
credits: '积分:查询团队 / 项目组积分和用量统计',
|
|
192
|
+
model: '模型:查询生图 / 生视频模型、参数约束和创建用法',
|
|
193
|
+
upload: '上传:把本地文件上传为平台可访问素材',
|
|
194
|
+
image: '图片:估价、提交、批量提交和查询生图任务',
|
|
195
|
+
video: '视频:估价、提交、批量提交、查询和去字幕',
|
|
196
|
+
task: '任务:任务列表、等待和本地任务台账',
|
|
197
|
+
asset: '资产:素材库匹配、素材组和素材注册',
|
|
198
|
+
subject: '主体:发布和查询可复用主体资产',
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const GROUP_EXAMPLES = {
|
|
202
|
+
system: [
|
|
203
|
+
'lj-awb doctor',
|
|
204
|
+
'lj-awb doctor --verify',
|
|
205
|
+
'lj-awb schema -f json',
|
|
206
|
+
'lj-awb schema --domain video -f json',
|
|
207
|
+
],
|
|
208
|
+
auth: [
|
|
209
|
+
'lj-awb auth status',
|
|
210
|
+
'lj-awb auth login-key --access-key <access-key>',
|
|
211
|
+
'LINGJING_AWB_ACCESS_KEY=<access-key> lj-awb auth login-key --from-env',
|
|
212
|
+
'lj-awb auth clear --dry-run',
|
|
213
|
+
],
|
|
214
|
+
account: [
|
|
215
|
+
'lj-awb account info',
|
|
216
|
+
'lj-awb account teams',
|
|
217
|
+
'lj-awb account switch-team --group-id <groupId> --yes',
|
|
218
|
+
],
|
|
219
|
+
project: [
|
|
220
|
+
'lj-awb project list',
|
|
221
|
+
'lj-awb project current',
|
|
222
|
+
'lj-awb project use --project-group-no <no> --yes',
|
|
223
|
+
'lj-awb project create --name "CLI 测试项目" --point 1000 --yes',
|
|
224
|
+
],
|
|
225
|
+
credits: [
|
|
226
|
+
'lj-awb credits balance',
|
|
227
|
+
'lj-awb credits usage --project-group-no <no> --last-hours 24',
|
|
228
|
+
],
|
|
229
|
+
model: [
|
|
230
|
+
'lj-awb model image-models --model Banana',
|
|
231
|
+
'lj-awb model video-models --model Seedance',
|
|
232
|
+
'lj-awb model options --model-group-code <code>',
|
|
233
|
+
'lj-awb model create-spec --model-group-code <code>',
|
|
234
|
+
],
|
|
235
|
+
upload: [
|
|
236
|
+
'lj-awb upload files --files ./a.png,./b.mp4 --dry-run',
|
|
237
|
+
],
|
|
238
|
+
image: [
|
|
239
|
+
'lj-awb image fee --model-group-code <code> --prompt "一只小狗"',
|
|
240
|
+
'lj-awb image create --model-group-code <code> --prompt "一只小狗" --dry-run',
|
|
241
|
+
'lj-awb image status --task-id <id>',
|
|
242
|
+
],
|
|
243
|
+
video: [
|
|
244
|
+
'lj-awb video fee --model-group-code <code> --prompt "雨夜奔跑" --duration 5',
|
|
245
|
+
'lj-awb video create --model-group-code <code> --prompt "镜头推进" --resource image:first_frame=./actor.png --dry-run',
|
|
246
|
+
'lj-awb video create --model-group-code <code> --prompt "镜头推进" --resource image:first_frame=asset:<assetId> --dry-run',
|
|
247
|
+
'lj-awb video subtitle-status --remote-task-id <id>',
|
|
248
|
+
],
|
|
249
|
+
task: [
|
|
250
|
+
'lj-awb task list --task-type IMAGE_CREATE --project-group-no <no>',
|
|
251
|
+
'lj-awb task wait --task-id <id> --task-type IMAGE_CREATE --wait-seconds 180',
|
|
252
|
+
'lj-awb task records --task-record-file .awb/tasks.jsonl',
|
|
253
|
+
],
|
|
254
|
+
asset: [
|
|
255
|
+
'lj-awb asset match-actor --description "十八岁少女,古风" --tags-json \'{"gender":"女"}\'',
|
|
256
|
+
'lj-awb asset group-create --name "女主素材组" --dry-run',
|
|
257
|
+
],
|
|
258
|
+
subject: [
|
|
259
|
+
'lj-awb subject list --name "女主"',
|
|
260
|
+
'lj-awb subject publish --name 女主 --primary-file ./three-view.png --dry-run',
|
|
261
|
+
],
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
const COMMAND_REQUIRED_OPTIONS = {
|
|
265
|
+
'account switch-team': ['groupId'],
|
|
266
|
+
'project create': ['name'],
|
|
267
|
+
'project ensure': ['name'],
|
|
268
|
+
'model options': ['modelGroupCode'],
|
|
269
|
+
'model create-spec': ['modelGroupCode'],
|
|
270
|
+
'image fee': ['modelGroupCode', 'prompt'],
|
|
271
|
+
'image create': ['modelGroupCode', 'prompt'],
|
|
272
|
+
'image create-batch': ['inputFile', 'modelGroupCode'],
|
|
273
|
+
'image status': ['taskId'],
|
|
274
|
+
'video fee': ['modelGroupCode'],
|
|
275
|
+
'video create': ['modelGroupCode'],
|
|
276
|
+
'video create-batch': ['inputFile', 'modelGroupCode'],
|
|
277
|
+
'video status': ['taskId'],
|
|
278
|
+
'video subtitle-remove': ['videoUrl'],
|
|
279
|
+
'task wait': ['taskId', 'taskType'],
|
|
280
|
+
'asset match-actor': ['description'],
|
|
281
|
+
'asset group-get': ['groupId'],
|
|
282
|
+
'asset group-create': ['name'],
|
|
283
|
+
'asset group-update': ['groupId'],
|
|
284
|
+
'asset register': ['groupId', 'name'],
|
|
285
|
+
'subject publish': ['name'],
|
|
286
|
+
'subject wait': ['elementId'],
|
|
287
|
+
'subject publish-batch': ['inputFile'],
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const COMMAND_REQUIRED_ANY_OPTIONS = {
|
|
291
|
+
'auth login-key': [['accessKey', 'fromEnv']],
|
|
292
|
+
'project update': [['name', 'point']],
|
|
293
|
+
'upload files': [['file', 'files', 'filesJson']],
|
|
294
|
+
'video fee': [['prompt', 'resource', 'resourcesJson']],
|
|
295
|
+
'video create': [['prompt', 'resource', 'resourcesJson']],
|
|
296
|
+
'asset group-update': [['name', 'description', 'projectName']],
|
|
297
|
+
'asset register': [['file', 'url', 'backendPath']],
|
|
298
|
+
'subject publish': [['primaryFile', 'primaryUrl', 'threeViewFile', 'threeViewUrl']],
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const CONFIRMATION_COMMANDS = new Set([
|
|
302
|
+
'auth clear',
|
|
303
|
+
'account switch-team',
|
|
304
|
+
'project use',
|
|
305
|
+
'project create',
|
|
306
|
+
'project update',
|
|
307
|
+
'project ensure',
|
|
308
|
+
'image create',
|
|
309
|
+
'image create-batch',
|
|
310
|
+
'video create',
|
|
311
|
+
'video create-batch',
|
|
312
|
+
'video subtitle-remove',
|
|
313
|
+
'asset group-create',
|
|
314
|
+
'asset group-update',
|
|
315
|
+
'asset register',
|
|
316
|
+
'subject publish',
|
|
317
|
+
'subject publish-batch',
|
|
318
|
+
]);
|
|
319
|
+
|
|
320
|
+
const COST_COMMANDS = new Set([
|
|
321
|
+
'image create',
|
|
322
|
+
'image create-batch',
|
|
323
|
+
'video create',
|
|
324
|
+
'video create-batch',
|
|
325
|
+
]);
|
|
326
|
+
|
|
327
|
+
const REMOTE_WRITE_COMMANDS = new Set([
|
|
328
|
+
'account switch-team',
|
|
329
|
+
'project use',
|
|
330
|
+
'project create',
|
|
331
|
+
'project update',
|
|
332
|
+
'project ensure',
|
|
333
|
+
'upload files',
|
|
334
|
+
'image create',
|
|
335
|
+
'image create-batch',
|
|
336
|
+
'video create',
|
|
337
|
+
'video create-batch',
|
|
338
|
+
'video subtitle-remove',
|
|
339
|
+
'asset group-create',
|
|
340
|
+
'asset group-update',
|
|
341
|
+
'asset register',
|
|
342
|
+
'subject publish',
|
|
343
|
+
'subject publish-batch',
|
|
344
|
+
]);
|
|
345
|
+
|
|
346
|
+
const LOCAL_STATE_WRITE_COMMANDS = new Set([
|
|
347
|
+
'auth login-key',
|
|
348
|
+
'auth clear',
|
|
349
|
+
'project use',
|
|
350
|
+
'project create',
|
|
351
|
+
'project ensure',
|
|
352
|
+
]);
|
|
353
|
+
|
|
354
|
+
const DESTRUCTIVE_COMMANDS = new Set(['auth clear']);
|
|
355
|
+
const LONG_RUNNING_COMMANDS = new Set(['task wait', 'task record-poll', 'subject wait']);
|
|
356
|
+
const NETWORK_NONE_COMMANDS = new Set(['schema', 'auth status', 'auth clear', 'task records']);
|
|
357
|
+
const NETWORK_CONDITIONAL_COMMANDS = new Set(['doctor', 'auth login-key']);
|
|
358
|
+
|
|
359
|
+
const OUTPUT_KIND_BY_COMMAND = {
|
|
360
|
+
doctor: 'environment_report',
|
|
361
|
+
schema: 'command_schema',
|
|
362
|
+
'auth status': 'auth_status',
|
|
363
|
+
'auth login-key': 'auth_result',
|
|
364
|
+
'account info': 'account_summary',
|
|
365
|
+
'account teams': 'team_list',
|
|
366
|
+
'project list': 'project_list',
|
|
367
|
+
'project current': 'project_summary',
|
|
368
|
+
'project users': 'project_user_list',
|
|
369
|
+
'credits balance': 'credits_balance',
|
|
370
|
+
'credits usage': 'credits_usage',
|
|
371
|
+
'model image-models': 'model_list',
|
|
372
|
+
'model video-models': 'model_list',
|
|
373
|
+
'model options': 'model_options',
|
|
374
|
+
'model create-spec': 'model_create_spec',
|
|
375
|
+
'upload files': 'upload_result',
|
|
376
|
+
'image fee': 'fee_estimate',
|
|
377
|
+
'image create': 'task_submission',
|
|
378
|
+
'image create-batch': 'batch_task_submission',
|
|
379
|
+
'image status': 'task_status',
|
|
380
|
+
'video fee': 'fee_estimate',
|
|
381
|
+
'video create': 'task_submission',
|
|
382
|
+
'video create-batch': 'batch_task_submission',
|
|
383
|
+
'video status': 'task_status',
|
|
384
|
+
'video subtitle-remove': 'subtitle_task_submission',
|
|
385
|
+
'video subtitle-status': 'subtitle_task_status',
|
|
386
|
+
'task list': 'task_list',
|
|
387
|
+
'task wait': 'task_status',
|
|
388
|
+
'task records': 'local_task_records',
|
|
389
|
+
'task record-poll': 'batch_task_status',
|
|
390
|
+
'asset match-actor': 'asset_match_list',
|
|
391
|
+
'asset group-list': 'asset_group_list',
|
|
392
|
+
'asset group-get': 'asset_group_detail',
|
|
393
|
+
'asset group-create': 'asset_group_write_result',
|
|
394
|
+
'asset group-update': 'asset_group_write_result',
|
|
395
|
+
'asset register': 'asset_register_result',
|
|
396
|
+
'subject list': 'subject_list',
|
|
397
|
+
'subject publish': 'subject_publish_result',
|
|
398
|
+
'subject wait': 'subject_status',
|
|
399
|
+
'subject publish-batch': 'batch_subject_publish_result',
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
const PREFLIGHTS_BY_COMMAND = {
|
|
403
|
+
'image create': ['doctor --verify', 'model image-models', 'model options', 'model create-spec', 'image fee', 'image create --dry-run'],
|
|
404
|
+
'image create-batch': ['doctor --verify', 'model image-models', 'model options', 'model create-spec', 'prepare JSONL input', 'image create-batch --dry-run'],
|
|
405
|
+
'video create': ['doctor --verify', 'model video-models', 'model options', 'model create-spec', 'video fee', 'video create --dry-run'],
|
|
406
|
+
'video create-batch': ['doctor --verify', 'model video-models', 'model options', 'model create-spec', 'prepare JSONL input', 'video create-batch --dry-run'],
|
|
407
|
+
'subject publish': ['doctor --verify', 'subject publish --dry-run'],
|
|
408
|
+
'subject publish-batch': ['doctor --verify', 'prepare JSONL input', 'subject publish-batch --dry-run'],
|
|
409
|
+
'video subtitle-remove': ['doctor --verify', 'video subtitle-remove --dry-run'],
|
|
410
|
+
'asset group-create': ['asset group-create --dry-run'],
|
|
411
|
+
'asset group-update': ['asset group-update --dry-run'],
|
|
412
|
+
'asset register': ['asset register --dry-run'],
|
|
413
|
+
'upload files': ['upload files --dry-run'],
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
function summaryOf(command) {
|
|
417
|
+
return String(command.description || '').split('\n')[0];
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function extractExamples(command) {
|
|
421
|
+
const lines = String(command.description || '').split('\n');
|
|
422
|
+
const examples = [];
|
|
423
|
+
let inExamples = false;
|
|
424
|
+
for (const line of lines) {
|
|
425
|
+
const trimmed = line.trim();
|
|
426
|
+
if (trimmed === 'Examples:') {
|
|
427
|
+
inExamples = true;
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (!inExamples) continue;
|
|
431
|
+
if (!trimmed) break;
|
|
432
|
+
if (/^[A-Za-z][A-Za-z ]+:$/.test(trimmed)) break;
|
|
433
|
+
examples.push(trimmed);
|
|
434
|
+
}
|
|
435
|
+
return examples;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function asJsonExample(example) {
|
|
439
|
+
const text = renderCliText(example);
|
|
440
|
+
if (/(^|\s)(-f\s+json|--format\s+json|--json)(\s|$)/.test(text)) return text;
|
|
441
|
+
return `${text} -f json`;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function commandNetworkMode(commandName) {
|
|
445
|
+
if (NETWORK_NONE_COMMANDS.has(commandName)) return 'none';
|
|
446
|
+
if (NETWORK_CONDITIONAL_COMMANDS.has(commandName)) return 'conditional';
|
|
447
|
+
return 'required';
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function unknownCommandHint(commandName) {
|
|
451
|
+
const replacement = RENAMED_COMMAND_HINTS[commandName];
|
|
452
|
+
if (replacement) return `该命令已产品化命名为 ${commandPrefix()} ${replacement}。运行 ${commandPrefix()} ${replacement.split(' ')[0]} -h 查看同组命令。`;
|
|
453
|
+
const group = String(commandName || '').split(' ').filter(Boolean)[0];
|
|
454
|
+
if (group === 'workspace') return `账号用 ${commandPrefix()} account ...,项目组用 ${commandPrefix()} project ...。运行 ${commandPrefix()} --help 查看可用命令。`;
|
|
455
|
+
if (group === 'billing') return `积分查询用 ${commandPrefix()} credits ...。运行 ${commandPrefix()} credits -h 查看可用命令。`;
|
|
456
|
+
return `运行 ${commandPrefix()} --help 查看可用命令。`;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function buildCommandSafety(command, optionKeys) {
|
|
460
|
+
const name = command.name;
|
|
461
|
+
const remoteWrite = REMOTE_WRITE_COMMANDS.has(name);
|
|
462
|
+
const localStateWrite = LOCAL_STATE_WRITE_COMMANDS.has(name);
|
|
463
|
+
const requiresConfirmation = CONFIRMATION_COMMANDS.has(name);
|
|
464
|
+
const costsPoints = COST_COMMANDS.has(name);
|
|
465
|
+
const destructive = DESTRUCTIVE_COMMANDS.has(name);
|
|
466
|
+
const supportsDryRun = optionKeys.has('dryRun');
|
|
467
|
+
return {
|
|
468
|
+
safeToAutoRun: !(remoteWrite || localStateWrite || requiresConfirmation || costsPoints || destructive),
|
|
469
|
+
network: commandNetworkMode(name),
|
|
470
|
+
remoteWrite,
|
|
471
|
+
localStateWrite,
|
|
472
|
+
costsPoints,
|
|
473
|
+
destructive,
|
|
474
|
+
requiresConfirmation,
|
|
475
|
+
confirmationFlag: requiresConfirmation ? '--yes' : null,
|
|
476
|
+
supportsDryRun,
|
|
477
|
+
dryRunFlag: supportsDryRun ? '--dry-run' : null,
|
|
478
|
+
longRunning: LONG_RUNNING_COMMANDS.has(name),
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function buildCommandWorkflow(command) {
|
|
483
|
+
const outputKind = OUTPUT_KIND_BY_COMMAND[command.name] || 'generic';
|
|
484
|
+
const next = [];
|
|
485
|
+
if (['image create', 'video create'].includes(command.name)) {
|
|
486
|
+
next.push('读取 data.taskId 和 data.nextCommand', '运行 task wait 等待结果');
|
|
487
|
+
}
|
|
488
|
+
if (command.name === 'subject publish') {
|
|
489
|
+
next.push('读取 data.elementId', '运行 subject wait 获取 externalId / nextRefSubject');
|
|
490
|
+
}
|
|
491
|
+
if (['image create-batch', 'video create-batch', 'subject publish-batch'].includes(command.name)) {
|
|
492
|
+
next.push('读取每项 status / taskId / error', '使用 task record-poll 或对应 wait 命令恢复批量结果');
|
|
493
|
+
}
|
|
494
|
+
return {
|
|
495
|
+
outputKind,
|
|
496
|
+
recommendedPreflight: PREFLIGHTS_BY_COMMAND[command.name] || [],
|
|
497
|
+
nextActions: next,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function buildAgentContract() {
|
|
502
|
+
return {
|
|
503
|
+
defaultOutputFormat: 'compact-text',
|
|
504
|
+
jsonFlag: '-f json',
|
|
505
|
+
outputPolicy: '默认输出精简 key=value 文本,只保留决策字段;需要稳定 JSON 或完整嵌套结构时显式传 -f json 或 --json。',
|
|
506
|
+
schemaPolicy: 'schema 是机器契约,只给摘要、参数 key、安全规则和示例;查看长帮助用 schema.commands[].helpCommand 或 lj-awb <domain> <command> -h。',
|
|
507
|
+
confirmationPolicy: 'schema.commands[].safety.requiresConfirmation=true 的命令,Agent 必须先向用户确认,再追加 --yes。',
|
|
508
|
+
dryRunPolicy: 'schema.commands[].safety.supportsDryRun=true 的写入 / 扣费命令,正式执行前优先跑 --dry-run。',
|
|
509
|
+
resourceShortcut: {
|
|
510
|
+
flag: '--resource',
|
|
511
|
+
syntax: 'type:usage[:reference_key]=path|url|asset:<id>',
|
|
512
|
+
examples: [
|
|
513
|
+
'image:reference=./ref.png',
|
|
514
|
+
'image:first_frame=./hero.png',
|
|
515
|
+
'image:first_frame=asset:<assetId>',
|
|
516
|
+
'subject:reference:hero=asset:<externalId>',
|
|
517
|
+
],
|
|
518
|
+
},
|
|
519
|
+
resourcesJson: {
|
|
520
|
+
flag: '--resources-json',
|
|
521
|
+
shape: [
|
|
522
|
+
{
|
|
523
|
+
type: 'image|video|audio|subject',
|
|
524
|
+
usage: 'reference|first_frame|last_frame|keyframe',
|
|
525
|
+
reference_key: 'optional key used by prompt placeholders',
|
|
526
|
+
source: { kind: 'url|asset_id', value: '<local-file-or-http-url-or-material-backendPath-or-asset-id>' },
|
|
527
|
+
},
|
|
528
|
+
],
|
|
529
|
+
},
|
|
530
|
+
modelOptions: {
|
|
531
|
+
command: `${commandPrefix()} model options --model-group-code <code>`,
|
|
532
|
+
jsonCommand: `${commandPrefix()} model options --model-group-code <code> -f json`,
|
|
533
|
+
purpose: '查询指定模型支持的 CLI 参数、枚举值、默认值和素材约束。',
|
|
534
|
+
useBefore: ['model create-spec', 'image fee', 'image create', 'video fee', 'video create'],
|
|
535
|
+
keyFields: ['params[].values', 'params[].defaultValue', 'resources[].mediaType', 'resources[].usage', 'resources[].fileTypes', 'resources[].maxFiles', 'resources[].minDurationMs', 'resources[].maxDurationMs'],
|
|
536
|
+
},
|
|
537
|
+
modelCreateSpec: {
|
|
538
|
+
command: `${commandPrefix()} model create-spec --model-group-code <code>`,
|
|
539
|
+
jsonCommand: `${commandPrefix()} model create-spec --model-group-code <code> -f json`,
|
|
540
|
+
purpose: '查看指定模型如何组织 create 命令:输入模式、prompt/key 规则、示例和创建前检查项。',
|
|
541
|
+
useBefore: ['image fee', 'image create', 'video fee', 'video create'],
|
|
542
|
+
keyFields: ['inputRequirement', 'supportedIntents', 'validationRules', 'agentGuidance', 'preflight', 'examples'],
|
|
543
|
+
},
|
|
544
|
+
taskTypes: {
|
|
545
|
+
image: 'IMAGE_CREATE',
|
|
546
|
+
video: 'VIDEO_GROUP',
|
|
547
|
+
},
|
|
548
|
+
exitCodes: [
|
|
549
|
+
{ code: 0, type: 'success', meaning: '命令成功' },
|
|
550
|
+
{ code: 1, type: 'runtime_error', meaning: '普通运行失败或平台业务失败' },
|
|
551
|
+
{ code: 2, type: 'argument_error|unknown_command|unknown_option', meaning: '参数或命令错误' },
|
|
552
|
+
{ code: 3, type: 'auth_required|auth_failed', meaning: '缺少认证或认证失败' },
|
|
553
|
+
{ code: 10, type: 'confirmation_required', meaning: '需要用户确认后追加 --yes' },
|
|
554
|
+
{ code: 20, type: 'task_still_running|subject_still_pending', meaning: '任务仍在运行,本轮等待窗口结束' },
|
|
555
|
+
{ code: 30, type: 'network_error', meaning: '网络、TLS、HTTP 或外部平台不可用' },
|
|
556
|
+
],
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function buildCommandSchema(commands, kwargs = {}, version = 'unknown') {
|
|
561
|
+
const domainFilter = String(kwargs.domain ?? '').trim();
|
|
562
|
+
const commandFilter = String(kwargs.command ?? '').trim();
|
|
563
|
+
const domains = new Map();
|
|
564
|
+
for (const command of commands) {
|
|
565
|
+
const group = commandGroup(command);
|
|
566
|
+
domains.set(group, {
|
|
567
|
+
name: group,
|
|
568
|
+
description: GROUP_DESCRIPTIONS[group] || '',
|
|
569
|
+
commandCount: (domains.get(group)?.commandCount || 0) + 1,
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
const filteredCommands = commands
|
|
573
|
+
.filter((command) => !domainFilter || commandGroup(command) === domainFilter)
|
|
574
|
+
.filter((command) => !commandFilter || command.name === commandFilter || commandSubcommand(command) === commandFilter);
|
|
575
|
+
const matchedDomains = new Set(filteredCommands.map((command) => commandGroup(command)));
|
|
576
|
+
const shouldFilterDomains = Boolean(domainFilter || commandFilter);
|
|
577
|
+
return {
|
|
578
|
+
schemaVersion: 1,
|
|
579
|
+
filters: {
|
|
580
|
+
domain: domainFilter || null,
|
|
581
|
+
command: commandFilter || null,
|
|
582
|
+
},
|
|
583
|
+
cli: {
|
|
584
|
+
name: 'lj-awb',
|
|
585
|
+
version,
|
|
586
|
+
commandPrefix: commandPrefix(),
|
|
587
|
+
},
|
|
588
|
+
usage: [
|
|
589
|
+
`${commandPrefix()} <domain> <command> [options]`,
|
|
590
|
+
`${commandPrefix()} <command> [options]`,
|
|
591
|
+
`${commandPrefix()} <domain> -h`,
|
|
592
|
+
`${commandPrefix()} <domain> <command> -h`,
|
|
593
|
+
],
|
|
594
|
+
globalOptions: [
|
|
595
|
+
{ flag: '-f, --format json', key: 'format', description: '输出 JSON;默认不传时输出精简文本' },
|
|
596
|
+
{ flag: '--json', key: 'format', value: 'json', description: '等同于 -f json' },
|
|
597
|
+
{ flag: '-h, --help', key: 'help', description: '查看帮助' },
|
|
598
|
+
{ flag: '-v, --version', key: 'version', description: '查看版本' },
|
|
599
|
+
],
|
|
600
|
+
agentContract: buildAgentContract(),
|
|
601
|
+
domains: Array.from(domains.values())
|
|
602
|
+
.filter((domain) => !shouldFilterDomains || matchedDomains.has(domain.name))
|
|
603
|
+
.sort((a, b) => a.name.localeCompare(b.name)),
|
|
604
|
+
commands: filteredCommands.map((command) => {
|
|
605
|
+
const requiredOptions = new Set(COMMAND_REQUIRED_OPTIONS[command.name] || []);
|
|
606
|
+
const requiredAnyOptions = COMMAND_REQUIRED_ANY_OPTIONS[command.name] || [];
|
|
607
|
+
const optionKeys = new Set((command.args || []).map((arg) => normalizeKey(arg.name)));
|
|
608
|
+
return {
|
|
609
|
+
name: command.name,
|
|
610
|
+
domain: commandGroup(command),
|
|
611
|
+
command: commandSubcommand(command),
|
|
612
|
+
invocation: `${commandPrefix()} ${command.name}`,
|
|
613
|
+
helpCommand: `${commandPrefix()} ${command.name} -h`,
|
|
614
|
+
summary: summaryOf(command),
|
|
615
|
+
description: renderCliText(summaryOf(command)),
|
|
616
|
+
examples: extractExamples(command).map(renderCliText),
|
|
617
|
+
jsonExamples: extractExamples(command).map(asJsonExample),
|
|
618
|
+
requiredOptions: [...requiredOptions],
|
|
619
|
+
requiredAnyOptions,
|
|
620
|
+
safety: buildCommandSafety(command, optionKeys),
|
|
621
|
+
workflow: buildCommandWorkflow(command),
|
|
622
|
+
options: (command.args || []).map((arg) => {
|
|
623
|
+
const key = normalizeKey(arg.name);
|
|
624
|
+
return {
|
|
625
|
+
name: arg.name,
|
|
626
|
+
key,
|
|
627
|
+
flag: formatOptionName(arg.name),
|
|
628
|
+
valueName: arg.valueName || null,
|
|
629
|
+
required: requiredOptions.has(key),
|
|
630
|
+
requiredAnyGroup: requiredAnyOptions.find((group) => group.includes(key)) || null,
|
|
631
|
+
description: arg.description || '',
|
|
632
|
+
};
|
|
633
|
+
}),
|
|
634
|
+
};
|
|
635
|
+
}),
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function printRootHelp(commands, version) {
|
|
640
|
+
const groups = new Map();
|
|
641
|
+
for (const command of commands) {
|
|
642
|
+
const group = commandGroup(command);
|
|
643
|
+
if (!groups.has(group)) groups.set(group, []);
|
|
644
|
+
groups.get(group).push(command);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const lines = [
|
|
648
|
+
`lj-awb v${version}`,
|
|
649
|
+
'',
|
|
650
|
+
'灵境 AWB 命令行工具。默认输出精简文本;需要 JSON 时传 -f json。',
|
|
651
|
+
'',
|
|
652
|
+
'Usage:',
|
|
653
|
+
` ${commandPrefix()} <domain> <command> [options]`,
|
|
654
|
+
` ${commandPrefix()} <command> [options]`,
|
|
655
|
+
` ${commandPrefix()} <domain> -h`,
|
|
656
|
+
` ${commandPrefix()} <domain> <command> -h`,
|
|
657
|
+
'',
|
|
658
|
+
'Quick start:',
|
|
659
|
+
` ${commandPrefix()} auth status`,
|
|
660
|
+
` ${commandPrefix()} auth login-key --access-key <access-key>`,
|
|
661
|
+
` ${commandPrefix()} model image-models --model Banana`,
|
|
662
|
+
` ${commandPrefix()} model options --model-group-code <code>`,
|
|
663
|
+
` ${commandPrefix()} model create-spec --model-group-code <code>`,
|
|
664
|
+
` ${commandPrefix()} image create --model-group-code <code> --prompt "一只小狗" --dry-run`,
|
|
665
|
+
'',
|
|
666
|
+
'Command groups:',
|
|
667
|
+
];
|
|
668
|
+
for (const [group] of groups.entries()) {
|
|
669
|
+
lines.push(` ${group.padEnd(12)} ${GROUP_DESCRIPTIONS[group] || ''}`);
|
|
670
|
+
}
|
|
671
|
+
lines.push(
|
|
672
|
+
'',
|
|
673
|
+
'More help:',
|
|
674
|
+
` ${commandPrefix()} <domain> -h`,
|
|
675
|
+
` ${commandPrefix()} <domain> <command> -h`,
|
|
676
|
+
` ${commandPrefix()} schema -f json`,
|
|
677
|
+
` ${commandPrefix()} doctor -h`,
|
|
678
|
+
'',
|
|
679
|
+
'Global options:',
|
|
680
|
+
' -f, --format json 输出 JSON;默认不传时输出精简文本',
|
|
681
|
+
' --json 等同于 -f json',
|
|
682
|
+
' -h, --help 查看帮助',
|
|
683
|
+
' -v, --version 查看版本',
|
|
684
|
+
);
|
|
685
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function printGroupHelp(group, commands, version) {
|
|
689
|
+
const items = commands.filter((command) => commandGroup(command) === group);
|
|
690
|
+
const lines = [
|
|
691
|
+
`lj-awb v${version}`,
|
|
692
|
+
'',
|
|
693
|
+
GROUP_DESCRIPTIONS[group] || `${group} 命令组`,
|
|
694
|
+
'',
|
|
695
|
+
'Usage:',
|
|
696
|
+
group === 'system' ? ` ${commandPrefix()} <command> [options]` : ` ${commandPrefix()} ${group} <command> [options]`,
|
|
697
|
+
'',
|
|
698
|
+
'Commands:',
|
|
699
|
+
];
|
|
700
|
+
for (const command of items) {
|
|
701
|
+
const subcommand = commandSubcommand(command);
|
|
702
|
+
lines.push(` ${subcommand.padEnd(28)} ${summaryOf(command)}`);
|
|
703
|
+
}
|
|
704
|
+
if (GROUP_EXAMPLES[group]?.length) {
|
|
705
|
+
lines.push('', 'Examples:');
|
|
706
|
+
for (const example of GROUP_EXAMPLES[group]) {
|
|
707
|
+
lines.push(` ${renderCliText(example)}`);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
lines.push('', 'More help:');
|
|
711
|
+
if (group === 'system') {
|
|
712
|
+
for (const command of items) lines.push(` ${commandPrefix()} ${command.name} -h`);
|
|
713
|
+
} else {
|
|
714
|
+
lines.push(` ${commandPrefix()} ${group} <command> -h`);
|
|
715
|
+
}
|
|
716
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function printCommandHelp(command) {
|
|
720
|
+
const lines = [`Usage: ${commandPrefix()} ${command.name} [options]`, '', renderCliText(command.description || '')];
|
|
721
|
+
if (command.args?.length) {
|
|
722
|
+
lines.push('', 'Options:');
|
|
723
|
+
for (const arg of command.args) {
|
|
724
|
+
const value = arg.valueName ? ` <${arg.valueName}>` : '';
|
|
725
|
+
const description = arg.description ? ` ${arg.description}` : '';
|
|
726
|
+
lines.push(` --${arg.name}${value}${description}`);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
lines.push('', 'Global options:', ' -f, --format json 输出 JSON;默认不传时输出精简文本', ' --json 等同于 -f json');
|
|
730
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
function validateCommandOptions(command, kwargs) {
|
|
734
|
+
const allowed = new Set((command.args || []).map((arg) => normalizeKey(arg.name)));
|
|
735
|
+
const unknown = Object.keys(kwargs).filter((key) => !allowed.has(key));
|
|
736
|
+
if (!unknown.length) return;
|
|
737
|
+
throw new LingjingAwbCliError(`未知参数:${unknown.map(formatOptionName).join(', ')}`, {
|
|
738
|
+
type: 'unknown_option',
|
|
739
|
+
exitCode: 2,
|
|
740
|
+
hint: `运行 ${commandPrefix()} ${command.name} -h 查看可用参数。`,
|
|
741
|
+
details: {
|
|
742
|
+
command: command.name,
|
|
743
|
+
unknownOptions: unknown.map(formatOptionName),
|
|
744
|
+
},
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
function printData(data, format, meta = {}, context = {}) {
|
|
749
|
+
if (format === 'json') {
|
|
750
|
+
process.stdout.write(`${JSON.stringify(createEnvelope({ status: 'success', data: normalizeJsonData(meta.command, data), meta }), null, 2)}\n`);
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
process.stdout.write(formatTextOutput(meta.command, data, context));
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
function printError(error, format, meta = {}) {
|
|
757
|
+
const payload = {
|
|
758
|
+
type: error.type || 'error',
|
|
759
|
+
message: error.message || String(error),
|
|
760
|
+
...(error.hint ? { hint: error.hint } : {}),
|
|
761
|
+
...(error.details ? { details: error.details } : {}),
|
|
762
|
+
};
|
|
763
|
+
if (format === 'json') {
|
|
764
|
+
process.stderr.write(`${JSON.stringify(createEnvelope({ status: 'error', error: payload, meta }), null, 2)}\n`);
|
|
765
|
+
} else {
|
|
766
|
+
process.stderr.write(formatTextError(payload));
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
export async function runStandaloneCli(argv = process.argv.slice(2)) {
|
|
771
|
+
process.env.LINGJING_AWB_COMMAND_PREFIX ??= 'lj-awb';
|
|
772
|
+
const commands = buildRegistry();
|
|
773
|
+
const version = await readVersion().catch(() => 'unknown');
|
|
774
|
+
|
|
775
|
+
if (argv.length === 0 || argv.includes('-h') || argv.includes('--help')) {
|
|
776
|
+
const parsed = parseArgv(argv.filter((item) => item !== '-h' && item !== '--help'));
|
|
777
|
+
const command = commands.find((item) => item.name === parsed.commandName);
|
|
778
|
+
if (command) {
|
|
779
|
+
printCommandHelp(command);
|
|
780
|
+
} else if (parsed.commandName && commands.some((item) => commandGroup(item) === parsed.commandName)) {
|
|
781
|
+
printGroupHelp(parsed.commandName, commands, version);
|
|
782
|
+
} else {
|
|
783
|
+
printRootHelp(commands, version);
|
|
784
|
+
}
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
if (argv.length === 1 && ['-v', '--version'].includes(argv[0])) {
|
|
789
|
+
process.stdout.write(`lj-awb v${version}\n`);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
const { commandName: rawCommandName, kwargs, format } = parseArgv(argv);
|
|
794
|
+
const commandName = rawCommandName;
|
|
795
|
+
const command = commands.find((item) => item.name === commandName);
|
|
796
|
+
if (!command) {
|
|
797
|
+
printError(new LingjingAwbCliError(`未知命令: ${rawCommandName || '(empty)'}`, {
|
|
798
|
+
type: 'unknown_command',
|
|
799
|
+
exitCode: 2,
|
|
800
|
+
hint: unknownCommandHint(rawCommandName),
|
|
801
|
+
}), format, { command: rawCommandName });
|
|
802
|
+
process.exitCode = 2;
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
const startedAt = Date.now();
|
|
807
|
+
try {
|
|
808
|
+
validateCommandOptions(command, kwargs);
|
|
809
|
+
const data = command.virtual === 'schema'
|
|
810
|
+
? buildCommandSchema(commands, kwargs, version)
|
|
811
|
+
: await command.func({ command }, kwargs);
|
|
812
|
+
printData(data, format, { command: command.name, elapsedMs: Date.now() - startedAt }, buildOutputContext(command, kwargs));
|
|
813
|
+
} catch (error) {
|
|
814
|
+
const cliError = error instanceof LingjingAwbCliError
|
|
815
|
+
? error
|
|
816
|
+
: new LingjingAwbCliError(error.message || String(error), { type: 'runtime_error', exitCode: 1 });
|
|
817
|
+
printError(cliError, format, { command: command.name, elapsedMs: Date.now() - startedAt });
|
|
818
|
+
process.exitCode = cliError.exitCode || 1;
|
|
819
|
+
}
|
|
820
|
+
}
|