@lingjingai/lj-awb-cli 0.4.0 → 0.4.1
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 +18 -0
- package/package.json +2 -2
- package/packages/awb-cli/package.json +2 -2
- package/packages/awb-core/package.json +1 -1
- package/packages/awb-core/src/commands.js +37 -2
- package/packages/awb-core/src/output.js +45 -1
- package/packages/awb-core/src/services.js +456 -22
- package/packages/awb-core/src/standalone.js +68 -6
- package/packages/awb-core/src/update.js +327 -0
- package/skills/lj-awb/SKILL.md +27 -8
- package/skills/lj-awb/VERSION +1 -1
- package/skills/lj-awb/compat.json +3 -3
- package/skills/lj-awb/modules/create-contract.md +5 -2
- package/skills/lj-awb/modules/driver.md +1 -0
- package/skills/lj-awb/modules/image.md +3 -1
- package/skills/lj-awb/modules/model.md +5 -4
- package/skills/lj-awb/modules/video.md +4 -2
- package/skills/lj-awb/references/model-options-read.md +7 -6
- package/skills/lj-awb/references/output-fields.md +3 -2
- package/skills/lj-awb/scripts/resolve-lj-awb-cmd.sh +106 -4
|
@@ -46,6 +46,17 @@ const REQUEST_SOURCE_CLI = 'LINGJING_AWB_CLI';
|
|
|
46
46
|
const DEFAULT_TASK_RECORD_FILE_ENV = process.env.LINGJING_AWB_TASK_RECORD_FILE || process.env.AWB_TASK_RECORD_FILE;
|
|
47
47
|
const execFileAsync = promisify(execFile);
|
|
48
48
|
const COMMON_IMAGE_FORMATS = new Set(['jpg', 'jpeg', 'jfif', 'png', 'webp']);
|
|
49
|
+
const REMOTE_IMAGE_DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
50
|
+
const REMOTE_IMAGE_DOWNLOAD_MAX_BYTES = 100 * 1024 * 1024;
|
|
51
|
+
const REMOTE_IMAGE_MIME_TYPES = new Set([
|
|
52
|
+
'image/bmp',
|
|
53
|
+
'image/gif',
|
|
54
|
+
'image/jpeg',
|
|
55
|
+
'image/jpg',
|
|
56
|
+
'image/png',
|
|
57
|
+
'image/webp',
|
|
58
|
+
]);
|
|
59
|
+
const REMOTE_IMAGE_EXTENSIONS = new Set(['.bmp', '.gif', '.jfif', '.jpg', '.jpeg', '.png', '.webp']);
|
|
49
60
|
const REMOTE_VOICE_DOWNLOAD_TIMEOUT_MS = 30_000;
|
|
50
61
|
const REMOTE_VOICE_DOWNLOAD_MAX_BYTES = 100 * 1024 * 1024;
|
|
51
62
|
const REMOTE_VOICE_MIME_TYPES = new Set([
|
|
@@ -61,6 +72,36 @@ const REMOTE_VOICE_EXTENSIONS = new Set(['.mp3', '.wav', '.m4a', '.aac', '.ogg',
|
|
|
61
72
|
const ASSET_PLATFORM_CODES = ['JIMENG', 'BYTEPLUS'];
|
|
62
73
|
const ASSET_PLATFORM_CODE_SET = new Set(ASSET_PLATFORM_CODES);
|
|
63
74
|
const ASSET_PLATFORM_HINT = ASSET_PLATFORM_CODES.join('|');
|
|
75
|
+
const CREATE_STANDARD_KWARG_KEYS = new Set([
|
|
76
|
+
'modelGroupCode',
|
|
77
|
+
'projectGroupNo',
|
|
78
|
+
'customBizId',
|
|
79
|
+
'prompt',
|
|
80
|
+
'ratio',
|
|
81
|
+
'quality',
|
|
82
|
+
'generateNum',
|
|
83
|
+
'generate_num',
|
|
84
|
+
'duration',
|
|
85
|
+
'needAudio',
|
|
86
|
+
'need_audio',
|
|
87
|
+
'resource',
|
|
88
|
+
'resources',
|
|
89
|
+
'resourcesJson',
|
|
90
|
+
'resources_json',
|
|
91
|
+
'modelParam',
|
|
92
|
+
'model_param',
|
|
93
|
+
'modelParams',
|
|
94
|
+
'model_params',
|
|
95
|
+
'modelParamsJson',
|
|
96
|
+
'model_params_json',
|
|
97
|
+
'taskRecordFile',
|
|
98
|
+
'waitSeconds',
|
|
99
|
+
'pollIntervalMs',
|
|
100
|
+
'inputFile',
|
|
101
|
+
'concurrency',
|
|
102
|
+
'dryRun',
|
|
103
|
+
'yes',
|
|
104
|
+
]);
|
|
64
105
|
|
|
65
106
|
export function normalizeUserInfo(payload) {
|
|
66
107
|
const data = payload && typeof payload === 'object' ? payload : {};
|
|
@@ -1047,6 +1088,8 @@ function sourceAllowedByRule(rule = {}, sourceKind) {
|
|
|
1047
1088
|
function resourceFormatForValidation(detail = {}) {
|
|
1048
1089
|
if (detail.localFile?.format) return String(detail.localFile.format).toLowerCase();
|
|
1049
1090
|
const value = detail.resource?.source?.value;
|
|
1091
|
+
const transformedFormat = imageTransformFormat(value);
|
|
1092
|
+
if (transformedFormat) return transformedFormat;
|
|
1050
1093
|
const pathname = (() => {
|
|
1051
1094
|
try {
|
|
1052
1095
|
return new URL(String(value)).pathname;
|
|
@@ -1058,6 +1101,34 @@ function resourceFormatForValidation(detail = {}) {
|
|
|
1058
1101
|
return ext || null;
|
|
1059
1102
|
}
|
|
1060
1103
|
|
|
1104
|
+
function imageTransformFormat(value) {
|
|
1105
|
+
const text = String(value || '');
|
|
1106
|
+
if (!text.includes('?') && !text.includes('%2F') && !text.includes('/format/')) return null;
|
|
1107
|
+
const queryText = (() => {
|
|
1108
|
+
try {
|
|
1109
|
+
const url = new URL(text);
|
|
1110
|
+
return url.search || text;
|
|
1111
|
+
} catch {
|
|
1112
|
+
const queryIndex = text.indexOf('?');
|
|
1113
|
+
return queryIndex >= 0 ? text.slice(queryIndex) : text;
|
|
1114
|
+
}
|
|
1115
|
+
})();
|
|
1116
|
+
let decoded = queryText;
|
|
1117
|
+
try {
|
|
1118
|
+
decoded = decodeURIComponent(queryText);
|
|
1119
|
+
} catch {}
|
|
1120
|
+
for (const pattern of [
|
|
1121
|
+
/image(?:mogr2|view2)[^&#?]*?\/format\/([A-Za-z0-9]+)/i,
|
|
1122
|
+
/x-oss-process=image[^&#?]*?format,([A-Za-z0-9]+)/i,
|
|
1123
|
+
/(?:^|[?&#])format=([A-Za-z0-9]+)/i,
|
|
1124
|
+
]) {
|
|
1125
|
+
const match = decoded.match(pattern);
|
|
1126
|
+
const format = normalizeFileFormat(match?.[1]);
|
|
1127
|
+
if (format) return format;
|
|
1128
|
+
}
|
|
1129
|
+
return null;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1061
1132
|
function normalizeFileFormat(value) {
|
|
1062
1133
|
const format = trimToNull(value)?.toLowerCase();
|
|
1063
1134
|
if (!format) return null;
|
|
@@ -1122,6 +1193,15 @@ function isConvertibleLocalImage(detail = {}) {
|
|
|
1122
1193
|
return Boolean(detail.localFile?.exists && String(detail.localFile?.mimeType || '').startsWith('image/'));
|
|
1123
1194
|
}
|
|
1124
1195
|
|
|
1196
|
+
function isDownloadableImageSource(detail = {}) {
|
|
1197
|
+
const sourceKind = resourceSourceKindForValidation(detail);
|
|
1198
|
+
return detail.resource?.source?.kind === 'url' && ['http_url', 'backendPath'].includes(sourceKind);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
function isConvertibleImageResource(detail = {}) {
|
|
1202
|
+
return isConvertibleLocalImage(detail) || isDownloadableImageSource(detail);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1125
1205
|
function resourceFormatConversion(rule = {}, detail = {}) {
|
|
1126
1206
|
if (!isImageRule(rule)) return null;
|
|
1127
1207
|
const format = normalizeFileFormat(resourceFormatForValidation(detail));
|
|
@@ -1133,7 +1213,7 @@ function resourceFormatConversion(rule = {}, detail = {}) {
|
|
|
1133
1213
|
fromFormat: format,
|
|
1134
1214
|
toFormat: policy.autoConvertTo,
|
|
1135
1215
|
reason: 'image_webp_not_supported',
|
|
1136
|
-
possible:
|
|
1216
|
+
possible: isConvertibleImageResource(detail),
|
|
1137
1217
|
};
|
|
1138
1218
|
}
|
|
1139
1219
|
if (policy.kind === 'normal_plus_webp') return null;
|
|
@@ -1142,7 +1222,7 @@ function resourceFormatConversion(rule = {}, detail = {}) {
|
|
|
1142
1222
|
fromFormat: format,
|
|
1143
1223
|
toFormat: policy.autoConvertTo,
|
|
1144
1224
|
reason: 'image_strict_format_mismatch',
|
|
1145
|
-
possible:
|
|
1225
|
+
possible: isConvertibleImageResource(detail),
|
|
1146
1226
|
allowed: policy.allowed,
|
|
1147
1227
|
};
|
|
1148
1228
|
}
|
|
@@ -1182,7 +1262,7 @@ function assertResourceShapeAgainstModel(resourceDetails = [], resourceRules = [
|
|
|
1182
1262
|
if (conversion && !conversion.possible) {
|
|
1183
1263
|
throw argumentError(
|
|
1184
1264
|
`素材格式需要转换但无法自动处理:${resourceText(resource)}`,
|
|
1185
|
-
`当前格式 ${conversion.fromFormat},目标格式 ${conversion.toFormat}
|
|
1265
|
+
`当前格式 ${conversion.fromFormat},目标格式 ${conversion.toFormat};请使用本地文件、http(s) URL 或 material backendPath,asset 资源需先转换后再传。`,
|
|
1186
1266
|
);
|
|
1187
1267
|
}
|
|
1188
1268
|
if (!conversion && !fileTypeAllowedByRule(rule, format)) {
|
|
@@ -1309,12 +1389,16 @@ function promptParamValue(promptParams = {}, key) {
|
|
|
1309
1389
|
}
|
|
1310
1390
|
|
|
1311
1391
|
function providedPromptParamKeys(promptParams = {}) {
|
|
1312
|
-
const keys =
|
|
1392
|
+
const keys = new Set();
|
|
1313
1393
|
for (const key of ['prompt', 'ratio', 'quality', 'duration', 'generateNum', 'needAudio']) {
|
|
1314
1394
|
const value = promptParamValue(promptParams, key);
|
|
1315
|
-
if (value !== undefined && value !== null && value !== '') keys.
|
|
1395
|
+
if (value !== undefined && value !== null && value !== '') keys.add(key);
|
|
1316
1396
|
}
|
|
1317
|
-
|
|
1397
|
+
for (const [key, value] of Object.entries(promptParams || {})) {
|
|
1398
|
+
if (key === 'resources' || key === 'generate_num' || key === 'need_audio') continue;
|
|
1399
|
+
if (value !== undefined && value !== null && value !== '') keys.add(key);
|
|
1400
|
+
}
|
|
1401
|
+
return [...keys];
|
|
1318
1402
|
}
|
|
1319
1403
|
|
|
1320
1404
|
function inferGeneratedMode(resources = []) {
|
|
@@ -1512,7 +1596,7 @@ function mergeResourceLimits(base = {}, overrides = {}) {
|
|
|
1512
1596
|
function mergeModelParamRules(options = [], rawModel = null) {
|
|
1513
1597
|
const rawParams = Array.isArray(rawModel?.modelParams) ? rawModel.modelParams : [];
|
|
1514
1598
|
const rawByKey = new Map(rawParams.map((item) => [item?.paramKey, item]).filter(([key]) => key));
|
|
1515
|
-
|
|
1599
|
+
const merged = options.map((option) => {
|
|
1516
1600
|
const raw = rawByKey.get(option.paramKey);
|
|
1517
1601
|
return {
|
|
1518
1602
|
...option,
|
|
@@ -1520,6 +1604,52 @@ function mergeModelParamRules(options = [], rawModel = null) {
|
|
|
1520
1604
|
rules: option.rules ?? raw?.rules ?? null,
|
|
1521
1605
|
};
|
|
1522
1606
|
});
|
|
1607
|
+
const mergedKeys = new Set(merged.map((item) => item.paramKey).filter(Boolean));
|
|
1608
|
+
for (const raw of rawParams) {
|
|
1609
|
+
if (!raw?.paramKey || mergedKeys.has(raw.paramKey)) continue;
|
|
1610
|
+
merged.push(raw);
|
|
1611
|
+
}
|
|
1612
|
+
return merged;
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
function modelParamDefaultValue(option = {}) {
|
|
1616
|
+
return option.rules?.defaultValue
|
|
1617
|
+
?? option.defaultValue
|
|
1618
|
+
?? option.rules?.defaultName
|
|
1619
|
+
?? option.defaultName
|
|
1620
|
+
?? null;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
function modelParamValueSource(option = {}) {
|
|
1624
|
+
const allowedValues = optionAllowedValues(option);
|
|
1625
|
+
if (allowedValues.length) return '只能从 allowedValues 选择。';
|
|
1626
|
+
if (modelParamDefaultValue(option) != null) return '模型配置提供 defaultValue;这是候选默认值,不代表可以静默代选。';
|
|
1627
|
+
return '来自实时 model options 的通用模型配置参数;用户明确选择后通过 --model-param key=value 或 --model-params-json 传入。';
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
function isGenericModelConfigParam(option = {}) {
|
|
1631
|
+
const key = trimToNull(option.paramKey);
|
|
1632
|
+
if (!key) return false;
|
|
1633
|
+
if ([
|
|
1634
|
+
'generated_mode',
|
|
1635
|
+
'resources',
|
|
1636
|
+
'iref',
|
|
1637
|
+
'cref',
|
|
1638
|
+
'sref',
|
|
1639
|
+
'frames',
|
|
1640
|
+
'multi_param',
|
|
1641
|
+
'multi_prompt',
|
|
1642
|
+
].includes(key)) return false;
|
|
1643
|
+
if (/resource|frame|image|video|audio|subject/i.test(key) && ![
|
|
1644
|
+
'need_audio',
|
|
1645
|
+
'audio',
|
|
1646
|
+
].includes(key)) return false;
|
|
1647
|
+
const paramType = String(option.paramType || '');
|
|
1648
|
+
return Boolean(
|
|
1649
|
+
optionAllowedValues(option).length
|
|
1650
|
+
|| modelParamDefaultValue(option) != null
|
|
1651
|
+
|| /Enum|Boolean|Bool|Number|Integer|Float|Double|String|Text|Prompt/i.test(paramType),
|
|
1652
|
+
);
|
|
1523
1653
|
}
|
|
1524
1654
|
|
|
1525
1655
|
function createParamForModelOption(option = {}, taskKind = 'image') {
|
|
@@ -1655,15 +1785,26 @@ function createParamForModelOption(option = {}, taskKind = 'image') {
|
|
|
1655
1785
|
},
|
|
1656
1786
|
};
|
|
1657
1787
|
return {
|
|
1658
|
-
...(definitions[key]
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1788
|
+
...(definitions[key]
|
|
1789
|
+
?? (isGenericModelConfigParam(option)
|
|
1790
|
+
? {
|
|
1791
|
+
key,
|
|
1792
|
+
cliArg: '--model-param / --model-params-json',
|
|
1793
|
+
requestPath: `promptParams.${key}`,
|
|
1794
|
+
materialLegacyKey: key,
|
|
1795
|
+
meaning: option.paramName ? `模型配置参数:${option.paramName}` : '模型配置参数。',
|
|
1796
|
+
valueSource: modelParamValueSource(option),
|
|
1797
|
+
genericModelParam: true,
|
|
1798
|
+
}
|
|
1799
|
+
: {
|
|
1800
|
+
key,
|
|
1801
|
+
cliArg: null,
|
|
1802
|
+
requestPath: null,
|
|
1803
|
+
materialLegacyKey: key,
|
|
1804
|
+
meaning: '模型配置存在该参数,但当前 CLI 未将其识别为可安全透传的创建参数。',
|
|
1805
|
+
valueSource: '如确实需要,先确认该字段不是资源或旧 handler 内部字段,再扩展通用参数判定。',
|
|
1806
|
+
notExposedByCli: true,
|
|
1807
|
+
})),
|
|
1667
1808
|
...common,
|
|
1668
1809
|
};
|
|
1669
1810
|
}
|
|
@@ -2032,6 +2173,7 @@ function createSpecParameterControls(createParams, inputModes = []) {
|
|
|
2032
2173
|
valueSource: item.valueSource,
|
|
2033
2174
|
controlKind: item.controlKind,
|
|
2034
2175
|
allowedValues: item.allowedValues,
|
|
2176
|
+
genericModelParam: item.genericModelParam,
|
|
2035
2177
|
}));
|
|
2036
2178
|
const resourceParams = createParams
|
|
2037
2179
|
.filter((item) => item.key === 'resources')
|
|
@@ -2153,7 +2295,8 @@ function cliValueType(option = {}, allowedValues = []) {
|
|
|
2153
2295
|
if (allowedValues.length) return 'enum';
|
|
2154
2296
|
if (option.paramType === 'Prompt') return 'text';
|
|
2155
2297
|
if (option.paramType === 'BooleanType') return 'boolean';
|
|
2156
|
-
if (/
|
|
2298
|
+
if (/Enum/i.test(String(option.paramType || ''))) return 'enum';
|
|
2299
|
+
if (/Number|Integer|Float|Double/i.test(String(option.paramType || ''))) return 'number';
|
|
2157
2300
|
return option.paramType || undefined;
|
|
2158
2301
|
}
|
|
2159
2302
|
|
|
@@ -2163,6 +2306,7 @@ function modelOptionParams(options = [], taskKind = 'image') {
|
|
|
2163
2306
|
.filter(({ createParam }) => createParam.cliArg && createParam.key !== 'resources' && !createParam.internalOnly)
|
|
2164
2307
|
.map(({ option, createParam }) => {
|
|
2165
2308
|
const rules = option.rules || {};
|
|
2309
|
+
const defaultValue = modelParamDefaultValue(option);
|
|
2166
2310
|
const allowedValues = optionAllowedValues(option);
|
|
2167
2311
|
const required = option.required === true || rules.required === true ? true : undefined;
|
|
2168
2312
|
const maxLength = option.paramType === 'Prompt'
|
|
@@ -2173,10 +2317,15 @@ function modelOptionParams(options = [], taskKind = 'image') {
|
|
|
2173
2317
|
label: option.paramName,
|
|
2174
2318
|
valueType: cliValueType(option, allowedValues),
|
|
2175
2319
|
values: allowedValues.length ? allowedValues : undefined,
|
|
2176
|
-
defaultValue
|
|
2177
|
-
defaultName: rules.defaultName,
|
|
2320
|
+
defaultValue,
|
|
2321
|
+
defaultName: rules.defaultName ?? option.defaultName,
|
|
2178
2322
|
maxLength,
|
|
2179
2323
|
required,
|
|
2324
|
+
cliArg: createParam.cliArg,
|
|
2325
|
+
genericModelParam: createParam.genericModelParam || undefined,
|
|
2326
|
+
modelParamKey: createParam.modelParamKey,
|
|
2327
|
+
modelParamName: createParam.modelParamName,
|
|
2328
|
+
modelParamType: createParam.modelParamType,
|
|
2180
2329
|
});
|
|
2181
2330
|
});
|
|
2182
2331
|
}
|
|
@@ -2341,6 +2490,7 @@ export async function modelCreateSpec(kwargs = {}) {
|
|
|
2341
2490
|
inputRequirement,
|
|
2342
2491
|
supportedIntents,
|
|
2343
2492
|
validationRules: createSpecValidationRules(context.taskKind),
|
|
2493
|
+
parameterControls: createSpecParameterControls(context.createParams, context.inputModes),
|
|
2344
2494
|
agentGuidance: createSpecAgentGuidance(context.taskKind, inputRequirement),
|
|
2345
2495
|
preflight: createSpecPreflight(context.taskKind),
|
|
2346
2496
|
examples: createSpecExamples(context.taskKind, supportedIntents),
|
|
@@ -2518,6 +2668,11 @@ function remoteFileNameFromUrl(remoteUrl, fallback = 'voice-audio') {
|
|
|
2518
2668
|
|
|
2519
2669
|
function extensionFromContentType(contentType = '') {
|
|
2520
2670
|
const normalized = String(contentType || '').split(';')[0].trim().toLowerCase();
|
|
2671
|
+
if (normalized === 'image/bmp') return '.bmp';
|
|
2672
|
+
if (normalized === 'image/gif') return '.gif';
|
|
2673
|
+
if (normalized === 'image/jpeg' || normalized === 'image/jpg') return '.jpg';
|
|
2674
|
+
if (normalized === 'image/png') return '.png';
|
|
2675
|
+
if (normalized === 'image/webp') return '.webp';
|
|
2521
2676
|
if (normalized === 'audio/mpeg') return '.mp3';
|
|
2522
2677
|
if (normalized === 'audio/wav' || normalized === 'audio/x-wav') return '.wav';
|
|
2523
2678
|
if (normalized === 'audio/mp4') return '.m4a';
|
|
@@ -2535,6 +2690,150 @@ function isSupportedRemoteVoiceDownload(contentType = '', fileName = '') {
|
|
|
2535
2690
|
return false;
|
|
2536
2691
|
}
|
|
2537
2692
|
|
|
2693
|
+
function remoteImageFileNameFromValue(value, fallback = 'resource-image') {
|
|
2694
|
+
const text = trimToNull(value);
|
|
2695
|
+
if (text) {
|
|
2696
|
+
try {
|
|
2697
|
+
const pathname = decodeURIComponent(new URL(text).pathname);
|
|
2698
|
+
const baseName = safeFileName(path.basename(pathname));
|
|
2699
|
+
if (baseName && baseName !== '.' && baseName !== '/') return baseName;
|
|
2700
|
+
} catch {
|
|
2701
|
+
const baseName = safeFileName(path.basename(text.split('?')[0].split('#')[0]));
|
|
2702
|
+
if (baseName && baseName !== '.' && baseName !== '/') return baseName;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
return fallback;
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
function extensionForImageFormat(format) {
|
|
2709
|
+
const normalized = normalizeFileFormat(format);
|
|
2710
|
+
if (!normalized) return '';
|
|
2711
|
+
return normalized === 'jpg' ? '.jpg' : `.${normalized}`;
|
|
2712
|
+
}
|
|
2713
|
+
|
|
2714
|
+
function forceFileExtension(fileName, extension) {
|
|
2715
|
+
const safeName = safeFileName(fileName || 'resource-image');
|
|
2716
|
+
if (!extension) return safeName;
|
|
2717
|
+
return `${path.basename(safeName, path.extname(safeName))}${extension}`;
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
function isSupportedRemoteImageDownload(contentType = '', fileName = '', expectedFormat = null) {
|
|
2721
|
+
const normalized = String(contentType || '').split(';')[0].trim().toLowerCase();
|
|
2722
|
+
const extension = path.extname(fileName).toLowerCase();
|
|
2723
|
+
if (REMOTE_IMAGE_MIME_TYPES.has(normalized)) return true;
|
|
2724
|
+
if ((!normalized || normalized === 'application/octet-stream') && REMOTE_IMAGE_EXTENSIONS.has(extension)) return true;
|
|
2725
|
+
if ((!normalized || normalized === 'application/octet-stream') && expectedFormat && COMMON_IMAGE_FORMATS.has(normalizeFileFormat(expectedFormat))) return true;
|
|
2726
|
+
return false;
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
function firstHttpUrl(value, depth = 0) {
|
|
2730
|
+
if (depth > 4 || value == null) return null;
|
|
2731
|
+
if (typeof value === 'string') return /^https?:\/\//i.test(value) ? value : null;
|
|
2732
|
+
if (Array.isArray(value)) {
|
|
2733
|
+
for (const item of value) {
|
|
2734
|
+
const found = firstHttpUrl(item, depth + 1);
|
|
2735
|
+
if (found) return found;
|
|
2736
|
+
}
|
|
2737
|
+
return null;
|
|
2738
|
+
}
|
|
2739
|
+
if (typeof value !== 'object') return null;
|
|
2740
|
+
for (const key of ['url', 'signedUrl', 'signUrl', 'downloadUrl', 'cosUrl', 'previewUrl', 'data']) {
|
|
2741
|
+
const found = firstHttpUrl(value[key], depth + 1);
|
|
2742
|
+
if (found) return found;
|
|
2743
|
+
}
|
|
2744
|
+
for (const nested of Object.values(value)) {
|
|
2745
|
+
const found = firstHttpUrl(nested, depth + 1);
|
|
2746
|
+
if (found) return found;
|
|
2747
|
+
}
|
|
2748
|
+
return null;
|
|
2749
|
+
}
|
|
2750
|
+
|
|
2751
|
+
async function resolveRemoteResourceDownloadUrl(value) {
|
|
2752
|
+
const text = trimToNull(value);
|
|
2753
|
+
if (!text) throw argumentError('远程图片来源为空');
|
|
2754
|
+
if (/^https?:\/\//i.test(text)) return text;
|
|
2755
|
+
if (text.startsWith('material/') || text.startsWith('/material/')) {
|
|
2756
|
+
const objectName = text.replace(/^\/+/, '');
|
|
2757
|
+
const signed = await awbApi.fetchObjectSignUrl(objectName);
|
|
2758
|
+
const signedUrl = firstHttpUrl(signed);
|
|
2759
|
+
if (signedUrl) return signedUrl;
|
|
2760
|
+
throw new LingjingAwbCliError('获取素材签名 URL 失败', {
|
|
2761
|
+
type: 'api_error',
|
|
2762
|
+
exitCode: 30,
|
|
2763
|
+
details: { objectName, payload: signed },
|
|
2764
|
+
});
|
|
2765
|
+
}
|
|
2766
|
+
throw argumentError(
|
|
2767
|
+
`远程图片来源不支持自动转换:${text}`,
|
|
2768
|
+
'请传 http(s) URL、material backendPath,或先下载为本地文件后重试。',
|
|
2769
|
+
);
|
|
2770
|
+
}
|
|
2771
|
+
|
|
2772
|
+
async function downloadRemoteImageFileToTemp(sourceValue, options = {}) {
|
|
2773
|
+
const timeoutMs = Math.max(1_000, toInt(options.timeoutMs, REMOTE_IMAGE_DOWNLOAD_TIMEOUT_MS));
|
|
2774
|
+
const maxBytes = Math.max(1, toInt(options.maxBytes, REMOTE_IMAGE_DOWNLOAD_MAX_BYTES));
|
|
2775
|
+
const remoteUrl = await resolveRemoteResourceDownloadUrl(sourceValue);
|
|
2776
|
+
const controller = new AbortController();
|
|
2777
|
+
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
2778
|
+
let dir = null;
|
|
2779
|
+
try {
|
|
2780
|
+
const response = await fetch(remoteUrl, { signal: controller.signal });
|
|
2781
|
+
if (!response.ok) {
|
|
2782
|
+
throw new LingjingAwbCliError(`下载远程图片失败:${response.status} ${response.statusText}`, {
|
|
2783
|
+
type: 'http_error',
|
|
2784
|
+
exitCode: 30,
|
|
2785
|
+
details: { url: sourceValue, status: response.status },
|
|
2786
|
+
});
|
|
2787
|
+
}
|
|
2788
|
+
const contentType = response.headers.get('content-type') || '';
|
|
2789
|
+
const expectedExtension = extensionForImageFormat(options.expectedFormat);
|
|
2790
|
+
let fileName = remoteImageFileNameFromValue(sourceValue, options.fallbackName || 'resource-image');
|
|
2791
|
+
fileName = expectedExtension
|
|
2792
|
+
? forceFileExtension(fileName, expectedExtension)
|
|
2793
|
+
: (!path.extname(fileName) ? `${fileName}${extensionFromContentType(contentType) || '.img'}` : fileName);
|
|
2794
|
+
if (!isSupportedRemoteImageDownload(contentType, fileName, options.expectedFormat)) {
|
|
2795
|
+
throw argumentError(
|
|
2796
|
+
`远程图片类型不支持:${contentType || path.extname(fileName) || 'unknown'}`,
|
|
2797
|
+
'自动格式转换只支持常见图片输入,请先手动转换为 jpg/png/webp 后重试。',
|
|
2798
|
+
);
|
|
2799
|
+
}
|
|
2800
|
+
const contentLength = toInt(response.headers.get('content-length'), 0);
|
|
2801
|
+
if (contentLength > maxBytes) {
|
|
2802
|
+
throw argumentError(`远程图片过大:${contentLength} bytes`, `最大支持 ${maxBytes} bytes。`);
|
|
2803
|
+
}
|
|
2804
|
+
dir = await fs.mkdtemp(path.join(tmpdir(), 'lj-awb-image-'));
|
|
2805
|
+
const filePath = path.join(dir, fileName);
|
|
2806
|
+
const file = await fs.open(filePath, 'w');
|
|
2807
|
+
let size = 0;
|
|
2808
|
+
try {
|
|
2809
|
+
if (!response.body) throw new Error('empty response body');
|
|
2810
|
+
for await (const chunk of response.body) {
|
|
2811
|
+
const buffer = Buffer.from(chunk);
|
|
2812
|
+
size += buffer.length;
|
|
2813
|
+
if (size > maxBytes) {
|
|
2814
|
+
throw argumentError(`远程图片过大:超过 ${maxBytes} bytes`);
|
|
2815
|
+
}
|
|
2816
|
+
await file.write(buffer);
|
|
2817
|
+
}
|
|
2818
|
+
} finally {
|
|
2819
|
+
await file.close();
|
|
2820
|
+
}
|
|
2821
|
+
return { filePath, dir, contentType, size };
|
|
2822
|
+
} catch (error) {
|
|
2823
|
+
if (dir) await fs.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
2824
|
+
if (error instanceof LingjingAwbCliError) throw error;
|
|
2825
|
+
const isAbort = error?.name === 'AbortError';
|
|
2826
|
+
throw new LingjingAwbCliError(`下载远程图片失败:${error.message}`, {
|
|
2827
|
+
type: 'network_error',
|
|
2828
|
+
exitCode: 30,
|
|
2829
|
+
hint: isAbort ? `远程下载超过 ${timeoutMs}ms,请改用本地文件或已上传 material 路径。` : '',
|
|
2830
|
+
details: { url: sourceValue, causeCode: error?.cause?.code },
|
|
2831
|
+
});
|
|
2832
|
+
} finally {
|
|
2833
|
+
clearTimeout(timeout);
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2538
2837
|
async function downloadRemoteFileToTemp(remoteUrl, options = {}) {
|
|
2539
2838
|
const timeoutMs = Math.max(1_000, toInt(options.timeoutMs, REMOTE_VOICE_DOWNLOAD_TIMEOUT_MS));
|
|
2540
2839
|
const maxBytes = Math.max(1, toInt(options.maxBytes, REMOTE_VOICE_DOWNLOAD_MAX_BYTES));
|
|
@@ -2635,6 +2934,59 @@ function normalizeUnifiedPromptParams(promptParams) {
|
|
|
2635
2934
|
return promptParams;
|
|
2636
2935
|
}
|
|
2637
2936
|
|
|
2937
|
+
async function parseModelParamsJsonArg(value) {
|
|
2938
|
+
const parsed = await readJsonMaybeFile(value, null).catch((error) => {
|
|
2939
|
+
throw argumentError(`model-params-json 解析失败:${error.message}`);
|
|
2940
|
+
});
|
|
2941
|
+
if (parsed == null) return {};
|
|
2942
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
2943
|
+
throw argumentError('model-params-json 必须是 JSON 对象');
|
|
2944
|
+
}
|
|
2945
|
+
return parsed;
|
|
2946
|
+
}
|
|
2947
|
+
|
|
2948
|
+
function parseModelParamAssignments(value) {
|
|
2949
|
+
const params = {};
|
|
2950
|
+
for (const item of parseListArg(value)) {
|
|
2951
|
+
const text = trimToNull(item);
|
|
2952
|
+
if (!text) continue;
|
|
2953
|
+
const eqIndex = text.indexOf('=');
|
|
2954
|
+
if (eqIndex <= 0) {
|
|
2955
|
+
throw argumentError(`model-param 格式错误:${text}`, '格式为 --model-param key=value,可重复传。');
|
|
2956
|
+
}
|
|
2957
|
+
const key = trimToNull(text.slice(0, eqIndex));
|
|
2958
|
+
if (!key) {
|
|
2959
|
+
throw argumentError(`model-param 缺少 key:${text}`);
|
|
2960
|
+
}
|
|
2961
|
+
params[key] = text.slice(eqIndex + 1);
|
|
2962
|
+
}
|
|
2963
|
+
return params;
|
|
2964
|
+
}
|
|
2965
|
+
|
|
2966
|
+
async function collectGenericModelParams(kwargs = {}) {
|
|
2967
|
+
const fromJson = await parseModelParamsJsonArg(kwargs.modelParamsJson);
|
|
2968
|
+
const fromObject = kwargs.modelParams && typeof kwargs.modelParams === 'object' && !Array.isArray(kwargs.modelParams)
|
|
2969
|
+
? kwargs.modelParams
|
|
2970
|
+
: {};
|
|
2971
|
+
const fromAssignments = parseModelParamAssignments(kwargs.modelParam);
|
|
2972
|
+
const fromDynamicFlags = {};
|
|
2973
|
+
for (const [key, value] of Object.entries(kwargs || {})) {
|
|
2974
|
+
if (CREATE_STANDARD_KWARG_KEYS.has(key)) continue;
|
|
2975
|
+
if (!key.includes('_') && !isLikelyDynamicModelParamKey(key)) continue;
|
|
2976
|
+
fromDynamicFlags[key] = value;
|
|
2977
|
+
}
|
|
2978
|
+
return normalizeUnifiedPromptParams({
|
|
2979
|
+
...fromJson,
|
|
2980
|
+
...fromObject,
|
|
2981
|
+
...fromDynamicFlags,
|
|
2982
|
+
...fromAssignments,
|
|
2983
|
+
});
|
|
2984
|
+
}
|
|
2985
|
+
|
|
2986
|
+
function isLikelyDynamicModelParamKey(key) {
|
|
2987
|
+
return /^(generation|prompt|negative|seed|style|mode|cfg|guidance|aspect|image|output|response|media|safety|watermark|camera|motion|strength|steps)[A-Z]/.test(String(key || ''));
|
|
2988
|
+
}
|
|
2989
|
+
|
|
2638
2990
|
function normalizeResourceType(type, index) {
|
|
2639
2991
|
const normalized = String(type ?? '').trim().toLowerCase();
|
|
2640
2992
|
if (!RESOURCE_TYPES.has(normalized)) {
|
|
@@ -2847,6 +3199,18 @@ function normalizeMaterialUrlValue(value) {
|
|
|
2847
3199
|
return text;
|
|
2848
3200
|
}
|
|
2849
3201
|
|
|
3202
|
+
function displayResourceSourceValue(value) {
|
|
3203
|
+
const text = trimToNull(value);
|
|
3204
|
+
if (!text || !/^https?:\/\//i.test(text)) return text;
|
|
3205
|
+
try {
|
|
3206
|
+
const url = new URL(text);
|
|
3207
|
+
const format = imageTransformFormat(text);
|
|
3208
|
+
return `${url.origin}${decodeURIComponent(url.pathname)}${format ? `?format=${format}` : ''}`;
|
|
3209
|
+
} catch {
|
|
3210
|
+
return text.split('?')[0];
|
|
3211
|
+
}
|
|
3212
|
+
}
|
|
3213
|
+
|
|
2850
3214
|
function conversionOutputExtension(format) {
|
|
2851
3215
|
const normalized = normalizeFileFormat(format);
|
|
2852
3216
|
if (normalized === 'jpg') return 'jpg';
|
|
@@ -2911,6 +3275,60 @@ async function resolveResourceFileValue(resource, options = {}) {
|
|
|
2911
3275
|
}
|
|
2912
3276
|
const value = resource._source.value;
|
|
2913
3277
|
if (isRemoteOrBackendPath(value)) {
|
|
3278
|
+
const source = { kind: 'url', value };
|
|
3279
|
+
const conversion = options.resourceRules
|
|
3280
|
+
? resourceFormatConversion(
|
|
3281
|
+
options.resourceRules.find((item) => resourceRuleMatches(item, { ...base, source })),
|
|
3282
|
+
{ resource: { ...base, source } },
|
|
3283
|
+
)
|
|
3284
|
+
: null;
|
|
3285
|
+
if (conversion) {
|
|
3286
|
+
const conversionRecord = compactRecord({
|
|
3287
|
+
resource: resourceText(base),
|
|
3288
|
+
fromFormat: conversion.fromFormat,
|
|
3289
|
+
toFormat: conversion.toFormat,
|
|
3290
|
+
reason: conversion.reason,
|
|
3291
|
+
sourceValue: displayResourceSourceValue(value),
|
|
3292
|
+
dryRun: options.dryRun || undefined,
|
|
3293
|
+
});
|
|
3294
|
+
const convertedName = forceFileExtension(
|
|
3295
|
+
remoteImageFileNameFromValue(value, resourceText(base).replace(/[^0-9A-Za-z._-]+/g, '-')),
|
|
3296
|
+
extensionForImageFormat(conversion.toFormat),
|
|
3297
|
+
);
|
|
3298
|
+
if (options.dryRun) {
|
|
3299
|
+
return {
|
|
3300
|
+
resource: { ...base, source: { kind: 'url', value: dryRunBackendPath(convertedName, options.sceneType) } },
|
|
3301
|
+
upload: null,
|
|
3302
|
+
localFile: null,
|
|
3303
|
+
conversion: conversionRecord,
|
|
3304
|
+
};
|
|
3305
|
+
}
|
|
3306
|
+
const downloaded = await downloadRemoteImageFileToTemp(value, {
|
|
3307
|
+
expectedFormat: conversion.fromFormat,
|
|
3308
|
+
fallbackName: resourceText(base).replace(/[^0-9A-Za-z._-]+/g, '-'),
|
|
3309
|
+
});
|
|
3310
|
+
try {
|
|
3311
|
+
const converted = await convertLocalImageFile(downloaded.filePath, conversion.toFormat);
|
|
3312
|
+
try {
|
|
3313
|
+
const upload = await uploadLocalFile(converted.filePath, { sceneType: options.sceneType });
|
|
3314
|
+
return {
|
|
3315
|
+
resource: { ...base, source: { kind: 'url', value: upload.backendPath } },
|
|
3316
|
+
upload,
|
|
3317
|
+
localFile: null,
|
|
3318
|
+
conversion: {
|
|
3319
|
+
...conversionRecord,
|
|
3320
|
+
sourceSize: downloaded.size,
|
|
3321
|
+
convertedFile: converted.filePath,
|
|
3322
|
+
convertedSize: converted.size,
|
|
3323
|
+
},
|
|
3324
|
+
};
|
|
3325
|
+
} finally {
|
|
3326
|
+
await fs.rm(converted.filePath, { force: true }).catch(() => {});
|
|
3327
|
+
}
|
|
3328
|
+
} finally {
|
|
3329
|
+
await fs.rm(downloaded.dir, { recursive: true, force: true }).catch(() => {});
|
|
3330
|
+
}
|
|
3331
|
+
}
|
|
2914
3332
|
return { resource: { ...base, source: { kind: 'url', value: normalizeMaterialUrlValue(value) } }, upload: null, localFile: null };
|
|
2915
3333
|
}
|
|
2916
3334
|
const inspected = await inspectLocalFile(value);
|
|
@@ -3040,7 +3458,9 @@ async function buildImageRequest(kwargs = {}, options = {}) {
|
|
|
3040
3458
|
kind: 'image',
|
|
3041
3459
|
sceneType: TASK_UPLOAD_SCENE.IMAGE_CREATE,
|
|
3042
3460
|
});
|
|
3461
|
+
const genericModelParams = await collectGenericModelParams(kwargs);
|
|
3043
3462
|
const defaultParams = {
|
|
3463
|
+
...genericModelParams,
|
|
3044
3464
|
prompt,
|
|
3045
3465
|
resources: validationResources.resources,
|
|
3046
3466
|
};
|
|
@@ -3218,7 +3638,9 @@ async function buildVideoRequest(kwargs = {}, options = {}) {
|
|
|
3218
3638
|
throw argumentError('缺少视频生成输入', '至少传 --prompt 或 --resource。');
|
|
3219
3639
|
}
|
|
3220
3640
|
assertVideoReferenceKeysUsed(prompt, validationResources.resources);
|
|
3641
|
+
const genericModelParams = await collectGenericModelParams(kwargs);
|
|
3221
3642
|
const defaultParams = {
|
|
3643
|
+
...genericModelParams,
|
|
3222
3644
|
prompt,
|
|
3223
3645
|
resources: validationResources.resources,
|
|
3224
3646
|
};
|
|
@@ -3349,6 +3771,11 @@ const IMAGE_BATCH_ITEM_KEYS = new Set([
|
|
|
3349
3771
|
'quality',
|
|
3350
3772
|
'generate_num',
|
|
3351
3773
|
'generateNum',
|
|
3774
|
+
'model_param',
|
|
3775
|
+
'modelParam',
|
|
3776
|
+
'model_params',
|
|
3777
|
+
'modelParams',
|
|
3778
|
+
'modelParamsJson',
|
|
3352
3779
|
'resource',
|
|
3353
3780
|
'resources',
|
|
3354
3781
|
'resourcesJson',
|
|
@@ -3361,6 +3788,11 @@ const VIDEO_BATCH_ITEM_KEYS = new Set([
|
|
|
3361
3788
|
'duration',
|
|
3362
3789
|
'need_audio',
|
|
3363
3790
|
'needAudio',
|
|
3791
|
+
'model_param',
|
|
3792
|
+
'modelParam',
|
|
3793
|
+
'model_params',
|
|
3794
|
+
'modelParams',
|
|
3795
|
+
'modelParamsJson',
|
|
3364
3796
|
'resource',
|
|
3365
3797
|
'resources',
|
|
3366
3798
|
'resourcesJson',
|
|
@@ -3372,18 +3804,20 @@ function normalizeTaskBatchItem(item, kind, index) {
|
|
|
3372
3804
|
throw argumentError(`批量输入第 ${index + 1} 项必须是 JSON 对象或纯文本 prompt`);
|
|
3373
3805
|
}
|
|
3374
3806
|
const allowedKeys = kind === 'image' ? IMAGE_BATCH_ITEM_KEYS : VIDEO_BATCH_ITEM_KEYS;
|
|
3375
|
-
const unknownKeys = Object.keys(item).filter((key) => !allowedKeys.has(key));
|
|
3807
|
+
const unknownKeys = Object.keys(item).filter((key) => !allowedKeys.has(key) && !key.includes('_'));
|
|
3376
3808
|
if (unknownKeys.length) {
|
|
3377
3809
|
throw argumentError(
|
|
3378
3810
|
`批量输入第 ${index + 1} 项存在未知字段:${unknownKeys.join(', ')}`,
|
|
3379
3811
|
kind === 'image'
|
|
3380
|
-
? '生图批量项只接受 prompt、ratio、quality、generate_num、resources、resource、customBizId
|
|
3381
|
-
: '生视频批量项只接受 prompt、ratio、quality、duration、need_audio、resources、resource、customBizId
|
|
3812
|
+
? '生图批量项只接受 prompt、ratio、quality、generate_num、resources、resource、model_params、customBizId,或 model options.params[] 中的下划线参数。'
|
|
3813
|
+
: '生视频批量项只接受 prompt、ratio、quality、duration、need_audio、resources、resource、model_params、customBizId,或 model options.params[] 中的下划线参数。',
|
|
3382
3814
|
);
|
|
3383
3815
|
}
|
|
3384
3816
|
const next = { ...item };
|
|
3385
3817
|
if (next.generate_num != null && next.generateNum == null) next.generateNum = next.generate_num;
|
|
3386
3818
|
if (next.need_audio != null && next.needAudio == null) next.needAudio = next.need_audio;
|
|
3819
|
+
if (next.model_param != null && next.modelParam == null) next.modelParam = next.model_param;
|
|
3820
|
+
if (next.model_params != null && next.modelParams == null) next.modelParams = next.model_params;
|
|
3387
3821
|
return next;
|
|
3388
3822
|
}
|
|
3389
3823
|
|