dsh-mo-image 0.7.0 → 1.0.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/CHANGELOG.md +28 -0
- package/README.md +283 -50
- package/lib/adapter-kit.js +150 -253
- package/lib/adapters/async.js +264 -277
- package/lib/adapters/cn.js +133 -81
- package/lib/adapters/openai.js +188 -157
- package/lib/adapters.js +3087 -944
- package/lib/client.js +6444 -858
- package/lib/comfy-workflow.js +96 -10
- package/lib/config-store.js +1072 -136
- package/lib/field-templates.js +385 -0
- package/lib/image-execution.js +40 -817
- package/lib/image-jobs.js +23 -12
- package/lib/image-request.js +830 -0
- package/lib/image-service.js +96 -133
- package/lib/image-worker.js +84 -71
- package/lib/index.js +833 -327
- package/lib/localized-error.js +4 -0
- package/lib/model-rules.js +1034 -0
- package/lib/net.js +60 -25
- package/lib/output-profiles.js +1225 -0
- package/lib/png-scale.js +53 -46
- package/lib/prompt-profiles.js +83 -38
- package/lib/provider-operations.js +291 -0
- package/lib/proxy-dispatcher.js +11 -5
- package/lib/proxy.js +10 -8
- package/lib/request-auth.js +86 -31
- package/lib/settings-memory.js +112 -28
- package/lib/status.js +6 -3
- package/locale/messages.csv +396 -226
- package/package.json +1 -1
- package/scripts/release.mjs +79 -64
- package/skills/mo-image-editing/SKILL.md +7 -13
- package/skills/mo-image-generation/SKILL.md +7 -6
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字段模板:把逻辑字段(prompt / negative / model / n / image)写进请求体的哪里。
|
|
3
|
+
*
|
|
4
|
+
* 协议只认识地址与传输形态;「这家网关把提示词叫什么、参考图怎么放」归预设与模型规则,
|
|
5
|
+
* 都写成这里的模板。能力封顶六种「写哪儿」,外加一个与位置无关的 as 编码形态;
|
|
6
|
+
* 多一种都不加——表达不了的网关单独做预设/协议,不往这一层加特性(红线见设计文档):
|
|
7
|
+
*
|
|
8
|
+
* { path: 'prompt' } 扁平;a.b.c 是嵌套
|
|
9
|
+
* { path: 'x[0].y[]' } 路径以 [] 结尾 = 追加到该数组末尾
|
|
10
|
+
* { path: 'x[]', wrap: 'text' } 追加的是 { text: 值 } 这种包装对象
|
|
11
|
+
* { inPrompt: true } / { appendToPrompt: '\n\nAvoid: ' } 不写 body,拼进提示词文本
|
|
12
|
+
* { numbered: 'input_image' } 数组逐个展开 input_image / input_image_2 …
|
|
13
|
+
* { multipart: 'image[]' } 走 FormData 的字段名(配 multipartFields)
|
|
14
|
+
* { path: 'image', as: 'dataUrl' | 'base64' | 'url' } as 修的是编码形态,不是第七种位置
|
|
15
|
+
*
|
|
16
|
+
* 三层合并顺序:用户覆盖 > 模型规则 > 预设;三层都没有 = 不发这个字段。
|
|
17
|
+
* 整条模板写 `null` 或 `false` = **显式不发**:它会原样留在合并结果里(`'image' in fields`
|
|
18
|
+
* 仍为真),后面的层不许把它盖掉,写 body 时跳过。
|
|
19
|
+
*
|
|
20
|
+
* 两个出口分开:applyFields 只写 JSON body,promptParts 只算提示词文本。模板形状认不出来
|
|
21
|
+
* 一律抛错,而且校验在写入之前整批做完——宁可在配置时就炸,也不要静默写错位置或留下半个 body。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** inPrompt 的默认粘合符:追加到提示词末尾时用换行隔开。 */
|
|
25
|
+
const PROMPT_GLUE = '\n';
|
|
26
|
+
|
|
27
|
+
const ENCODINGS = new Set(['dataUrl', 'base64', 'url']);
|
|
28
|
+
|
|
29
|
+
/** 每种形状允许出现的键;多出来的键一律当成「无法识别的形状」。 */
|
|
30
|
+
const SHAPES = {
|
|
31
|
+
path: ['path', 'as'],
|
|
32
|
+
append: ['path', 'wrap', 'as'],
|
|
33
|
+
prompt: ['inPrompt'],
|
|
34
|
+
appendPrompt: ['appendToPrompt'],
|
|
35
|
+
numbered: ['numbered', 'as'],
|
|
36
|
+
multipart: ['multipart']
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** 合并顺序,前面的压过后面的。 */
|
|
40
|
+
const LAYERS = ['override', 'rule', 'preset'];
|
|
41
|
+
|
|
42
|
+
/** 逻辑字段里唯一可能是「一组图」的那个:multipartFields 的 images 参数只喂给它。 */
|
|
43
|
+
const IMAGE_FIELD = 'image';
|
|
44
|
+
|
|
45
|
+
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
|
|
46
|
+
|
|
47
|
+
function fail(message) {
|
|
48
|
+
throw new Error('字段模板:' + message);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isPlainObject(value) {
|
|
52
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 图片对象是 { buffer, contentType };裸 Buffer 自带 .buffer(ArrayBuffer 池),不能算图片。 */
|
|
56
|
+
function isImage(value) {
|
|
57
|
+
return isPlainObject(value) && value.buffer != null && !Buffer.isBuffer(value);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** undefined / null / false 都是「不发这个字段」。 */
|
|
61
|
+
function isAbsent(value) {
|
|
62
|
+
return value === undefined || value === null || value === false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function bufferOf(image) {
|
|
66
|
+
return Buffer.isBuffer(image.buffer) ? image.buffer : Buffer.from(image.buffer);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/* ------------------------------------------------------------ 路径解析 */
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 把 'input.messages[0].content[]' 拆成步骤:{ key } / { index }。
|
|
73
|
+
* `[]` 只允许出现在末尾,表示「追加到这个数组」。
|
|
74
|
+
*/
|
|
75
|
+
function parsePath(field, raw) {
|
|
76
|
+
if (typeof raw !== 'string' || raw.trim() === '') {
|
|
77
|
+
fail(field + ' 的 path 必须是非空字符串');
|
|
78
|
+
}
|
|
79
|
+
const steps = [];
|
|
80
|
+
let append = false;
|
|
81
|
+
const parts = raw.split('.');
|
|
82
|
+
if (parts.length > 32) fail(field + ' 的 path 层数过多');
|
|
83
|
+
parts.forEach((part, partIndex) => {
|
|
84
|
+
const match = /^([^.[\]]+)((?:\[\d+\]|\[\])*)$/.exec(part);
|
|
85
|
+
if (!match) fail(field + ' 的 path 无法识别:' + raw);
|
|
86
|
+
if (['__proto__', 'constructor', 'prototype'].includes(match[1])) fail(field + ' 的 path 含无效字段');
|
|
87
|
+
steps.push({ key: match[1] });
|
|
88
|
+
const tails = Array.from(match[2].matchAll(/\[(\d*)\]/g), (item) => item[1]);
|
|
89
|
+
tails.forEach((tail, tailIndex) => {
|
|
90
|
+
if (tail !== '') {
|
|
91
|
+
if (Number(tail) > 1024) fail(field + ' 的 path 数组索引过大');
|
|
92
|
+
steps.push({ index: Number(tail) });
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const atEnd = partIndex === parts.length - 1 && tailIndex === tails.length - 1;
|
|
96
|
+
if (!atEnd) fail(field + ' 的 path 只有末尾才能用 []:' + raw);
|
|
97
|
+
append = true;
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
return { steps, append };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* ---------------------------------------------------------- 模板编译 */
|
|
104
|
+
|
|
105
|
+
function checkShape(field, spec, kind) {
|
|
106
|
+
const allowed = SHAPES[kind];
|
|
107
|
+
for (const key of Object.keys(spec)) {
|
|
108
|
+
if (!allowed.includes(key)) {
|
|
109
|
+
fail(field + ' 的模板有无法识别的键 ' + JSON.stringify(key) + ':' + JSON.stringify(spec));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function encodingOf(field, spec) {
|
|
115
|
+
if (!('as' in spec)) return null;
|
|
116
|
+
if (!ENCODINGS.has(spec.as)) {
|
|
117
|
+
fail(field + ' 的 as 只能是 dataUrl / base64 / url,收到 ' + JSON.stringify(spec.as));
|
|
118
|
+
}
|
|
119
|
+
return spec.as;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function nameOf(field, key, value) {
|
|
123
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
124
|
+
fail(field + ' 的 ' + key + ' 必须是非空字符串');
|
|
125
|
+
}
|
|
126
|
+
if (['__proto__', 'constructor', 'prototype'].includes(value)) fail(field + ' 的字段名无效');
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function otherKind(field, spec) {
|
|
131
|
+
if ('inPrompt' in spec) {
|
|
132
|
+
if (spec.inPrompt !== true) fail(field + ' 的 inPrompt 只能是 true');
|
|
133
|
+
return 'prompt';
|
|
134
|
+
}
|
|
135
|
+
if ('appendToPrompt' in spec) {
|
|
136
|
+
if (typeof spec.appendToPrompt !== 'string') {
|
|
137
|
+
fail(field + ' 的 appendToPrompt 必须是字符串');
|
|
138
|
+
}
|
|
139
|
+
return 'appendPrompt';
|
|
140
|
+
}
|
|
141
|
+
if ('numbered' in spec) return 'numbered';
|
|
142
|
+
if ('multipart' in spec) return 'multipart';
|
|
143
|
+
return fail(field + ' 的模板没有可识别的形状:' + JSON.stringify(spec));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function compileSpec(field, spec) {
|
|
147
|
+
if (!isPlainObject(spec)) {
|
|
148
|
+
fail(field + ' 的模板必须是对象(要「不发」请写 null 或 false),收到 ' + typeof spec);
|
|
149
|
+
}
|
|
150
|
+
const parsed = 'path' in spec ? parsePath(field, spec.path) : null;
|
|
151
|
+
const kind = parsed ? (parsed.append ? 'append' : 'path') : otherKind(field, spec);
|
|
152
|
+
checkShape(field, spec, kind);
|
|
153
|
+
const as = encodingOf(field, spec);
|
|
154
|
+
|
|
155
|
+
if (parsed) {
|
|
156
|
+
const descriptor = { field, kind, raw: spec.path, steps: parsed.steps, as };
|
|
157
|
+
if (kind === 'append') {
|
|
158
|
+
descriptor.wrap = hasOwn(spec, 'wrap') ? nameOf(field, 'wrap', spec.wrap) : null;
|
|
159
|
+
}
|
|
160
|
+
return descriptor;
|
|
161
|
+
}
|
|
162
|
+
if (kind === 'numbered') return { field, kind, name: nameOf(field, 'numbered', spec.numbered), as };
|
|
163
|
+
if (kind === 'multipart') return { field, kind, name: nameOf(field, 'multipart', spec.multipart) };
|
|
164
|
+
if (kind === 'appendPrompt') return { field, kind, prefix: spec.appendToPrompt };
|
|
165
|
+
return { field, kind };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 整批编译:先全部校验完,再让调用方动手写(所以坏模板不会留下半个 body)。 */
|
|
169
|
+
function compileFields(fields) {
|
|
170
|
+
if (fields === undefined || fields === null) return [];
|
|
171
|
+
if (!isPlainObject(fields)) fail('fields 必须是对象,收到 ' + typeof fields);
|
|
172
|
+
const descriptors = [];
|
|
173
|
+
for (const [field, spec] of Object.entries(fields)) {
|
|
174
|
+
if (spec === null || spec === false) continue; // 显式不发,连形状都不用认
|
|
175
|
+
descriptors.push(compileSpec(field, spec));
|
|
176
|
+
}
|
|
177
|
+
return descriptors;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* ------------------------------------------------------------ 值编码 */
|
|
181
|
+
|
|
182
|
+
function encode(value, as) {
|
|
183
|
+
if (as === null) return value;
|
|
184
|
+
if (as === 'url') {
|
|
185
|
+
const url = typeof value === 'string' ? value : value?.url;
|
|
186
|
+
if (typeof url !== 'string' || !/^https?:\/\//i.test(url)) fail('参考图映射要求 HTTP(S) URL');
|
|
187
|
+
return url;
|
|
188
|
+
}
|
|
189
|
+
if (as === 'base64') {
|
|
190
|
+
if (isImage(value)) return bufferOf(value).toString('base64');
|
|
191
|
+
if (Buffer.isBuffer(value)) return value.toString('base64');
|
|
192
|
+
return String(value);
|
|
193
|
+
}
|
|
194
|
+
if (isImage(value)) {
|
|
195
|
+
return 'data:' + (value.contentType || 'image/png') + ';base64,' + bufferOf(value).toString('base64');
|
|
196
|
+
}
|
|
197
|
+
return String(value);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function listOf(value) {
|
|
201
|
+
if (Array.isArray(value)) return value.filter((item) => !isAbsent(item));
|
|
202
|
+
return [value];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* --------------------------------------------------------- body 写入 */
|
|
206
|
+
|
|
207
|
+
function assertContainer(node, label) {
|
|
208
|
+
if (!isPlainObject(node) && !Array.isArray(node)) {
|
|
209
|
+
fail('路径 ' + label + ' 与已有的 body 结构冲突(中间是 ' + typeof node + ')');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function readChild(node, step, label) {
|
|
214
|
+
assertContainer(node, label);
|
|
215
|
+
if (step.index !== undefined) {
|
|
216
|
+
if (!Array.isArray(node)) fail('路径 ' + label + ' 的 [' + step.index + '] 遇到非数组容器');
|
|
217
|
+
return node[step.index];
|
|
218
|
+
}
|
|
219
|
+
if (Array.isArray(node)) fail('路径 ' + label + ' 的 ' + step.key + ' 遇到数组容器');
|
|
220
|
+
return node[step.key];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function writeChild(node, step, value, label) {
|
|
224
|
+
assertContainer(node, label);
|
|
225
|
+
if (step.index !== undefined) {
|
|
226
|
+
if (!Array.isArray(node)) fail('路径 ' + label + ' 的 [' + step.index + '] 遇到非数组容器');
|
|
227
|
+
node[step.index] = value;
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (Array.isArray(node)) fail('路径 ' + label + ' 的 ' + step.key + ' 遇到数组容器');
|
|
231
|
+
node[step.key] = value;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** 走到最后一步的持有者,沿途按下一步是下标还是键把中间容器建出来。 */
|
|
235
|
+
function walk(root, steps, label) {
|
|
236
|
+
let node = root;
|
|
237
|
+
for (let index = 0; index < steps.length; index += 1) {
|
|
238
|
+
const step = steps[index];
|
|
239
|
+
if (index === steps.length - 1) return { node, step };
|
|
240
|
+
let child = readChild(node, step, label);
|
|
241
|
+
if (child === undefined || child === null) {
|
|
242
|
+
child = steps[index + 1].index !== undefined ? [] : {};
|
|
243
|
+
writeChild(node, step, child, label);
|
|
244
|
+
}
|
|
245
|
+
node = child;
|
|
246
|
+
}
|
|
247
|
+
return fail('path 不能为空');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function writeAt(body, descriptor, value) {
|
|
251
|
+
const { node, step } = walk(body, descriptor.steps, descriptor.raw);
|
|
252
|
+
writeChild(node, step, value, descriptor.raw);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function appendAt(body, descriptor, value) {
|
|
256
|
+
const { node, step } = walk(body, descriptor.steps, descriptor.raw);
|
|
257
|
+
let list = readChild(node, step, descriptor.raw);
|
|
258
|
+
if (list === undefined || list === null) {
|
|
259
|
+
list = [];
|
|
260
|
+
writeChild(node, step, list, descriptor.raw);
|
|
261
|
+
} else if (!Array.isArray(list)) {
|
|
262
|
+
fail(descriptor.field + ' 的路径 ' + descriptor.raw + ' 不是数组,无法追加');
|
|
263
|
+
}
|
|
264
|
+
list.push(value);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/* --------------------------------------------------------------- API */
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 把一组逻辑字段按模板写进 body(就地修改)。
|
|
271
|
+
* @param {object} body 目标请求体
|
|
272
|
+
* @param {object} fields 字段 → 模板(resolveFields 的结果,可含 null/false)
|
|
273
|
+
* @param {object} values 逻辑字段值 { prompt, negative, model, n, image },image 单张或数组
|
|
274
|
+
* @returns {object} 同一个 body
|
|
275
|
+
*/
|
|
276
|
+
export function applyFields(body, fields, values) {
|
|
277
|
+
if (!isPlainObject(body)) fail('body 必须是对象');
|
|
278
|
+
const descriptors = compileFields(fields);
|
|
279
|
+
const source = isPlainObject(values) ? values : {};
|
|
280
|
+
for (const descriptor of descriptors) {
|
|
281
|
+
const value = source[descriptor.field];
|
|
282
|
+
if (isAbsent(value)) continue;
|
|
283
|
+
if (descriptor.kind === 'path') {
|
|
284
|
+
const items = listOf(value).map((item) => encode(item, descriptor.as));
|
|
285
|
+
if (items.length === 1) writeAt(body, descriptor, items[0]);
|
|
286
|
+
else if (items.length > 1) writeAt(body, descriptor, items);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
if (descriptor.kind === 'append') {
|
|
290
|
+
for (const item of listOf(value)) {
|
|
291
|
+
const encoded = encode(item, descriptor.as);
|
|
292
|
+
appendAt(body, descriptor, descriptor.wrap ? { [descriptor.wrap]: encoded } : encoded);
|
|
293
|
+
}
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (descriptor.kind === 'numbered') {
|
|
297
|
+
listOf(value).forEach((item, index) => {
|
|
298
|
+
const key = index === 0 ? descriptor.name : descriptor.name + '_' + (index + 1);
|
|
299
|
+
body[key] = encode(item, descriptor.as);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
// inPrompt / appendToPrompt / multipart 不写 body:分别交给 promptParts 与 multipartFields。
|
|
303
|
+
}
|
|
304
|
+
return body;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 合并三层字段模板:用户覆盖 > 模型规则 > 预设。
|
|
309
|
+
* 逐字段取第一个「有这个键」的层;整条为 null / false 的「显式不发」原样保留,
|
|
310
|
+
* 后面两层不许盖掉它。
|
|
311
|
+
* @param {{ override?: object, rule?: object, preset?: object }} layers
|
|
312
|
+
* @returns {object} 合并后的 fields
|
|
313
|
+
*/
|
|
314
|
+
export function resolveFields(layers) {
|
|
315
|
+
const input = isPlainObject(layers) ? layers : {};
|
|
316
|
+
const merged = {};
|
|
317
|
+
for (const name of LAYERS) {
|
|
318
|
+
const layer = input[name];
|
|
319
|
+
if (layer === undefined || layer === null) continue;
|
|
320
|
+
if (!isPlainObject(layer)) fail(name + ' 层必须是对象,收到 ' + typeof layer);
|
|
321
|
+
for (const [field, spec] of Object.entries(layer)) {
|
|
322
|
+
if (hasOwn(merged, field)) continue; // 更高层已经定过(包括「不发」)
|
|
323
|
+
if (spec !== null && spec !== false && !isPlainObject(spec)) {
|
|
324
|
+
fail(name + '.' + field + ' 必须是模板对象或 null/false,收到 ' + typeof spec);
|
|
325
|
+
}
|
|
326
|
+
merged[field] = spec;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return merged;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 给 multipart 端点用:把 multipart 字段算成 [{ name, value }],不写进 JSON body。
|
|
334
|
+
* 图片条目多带一个 contentType(调用方据此造 Blob),其余是字符串化的标量。
|
|
335
|
+
* @param {object} fields 字段 → 模板
|
|
336
|
+
* @param {object} values 逻辑字段值
|
|
337
|
+
* @param {object|object[]} [images] 图片列表;给了就以它为准(只喂给 image 字段)
|
|
338
|
+
* @returns {Array<{ name: string, value: string|Buffer, contentType?: string }>}
|
|
339
|
+
*/
|
|
340
|
+
export function multipartFields(fields, values, images) {
|
|
341
|
+
const descriptors = compileFields(fields);
|
|
342
|
+
const source = isPlainObject(values) ? values : {};
|
|
343
|
+
const entries = [];
|
|
344
|
+
for (const descriptor of descriptors) {
|
|
345
|
+
if (descriptor.kind !== 'multipart') continue;
|
|
346
|
+
const fromImages = descriptor.field === IMAGE_FIELD && images !== undefined;
|
|
347
|
+
const raw = fromImages ? images : source[descriptor.field];
|
|
348
|
+
if (isAbsent(raw)) continue;
|
|
349
|
+
for (const item of listOf(raw)) {
|
|
350
|
+
if (isImage(item)) {
|
|
351
|
+
entries.push({
|
|
352
|
+
name: descriptor.name,
|
|
353
|
+
value: bufferOf(item),
|
|
354
|
+
contentType: item.contentType || 'image/png'
|
|
355
|
+
});
|
|
356
|
+
} else {
|
|
357
|
+
entries.push({ name: descriptor.name, value: String(item) });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
return entries;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* 把 inPrompt / appendToPrompt 的字段算成提示词文本。
|
|
366
|
+
* inPrompt 用 '\n' 粘合,appendToPrompt 用给定的前缀;返回的 prompt 已把它们接在
|
|
367
|
+
* values.prompt 后面(prompt 缺省算空串)。
|
|
368
|
+
* @param {object} fields 字段 → 模板
|
|
369
|
+
* @param {object} values 逻辑字段值(可以有 prompt 之外的键,如 size)
|
|
370
|
+
* @returns {{ prompt: string, parts: Array<{ field: string, text: string }> }}
|
|
371
|
+
*/
|
|
372
|
+
export function promptParts(fields, values) {
|
|
373
|
+
const descriptors = compileFields(fields);
|
|
374
|
+
const source = isPlainObject(values) ? values : {};
|
|
375
|
+
const parts = [];
|
|
376
|
+
for (const descriptor of descriptors) {
|
|
377
|
+
if (descriptor.kind !== 'prompt' && descriptor.kind !== 'appendPrompt') continue;
|
|
378
|
+
const value = source[descriptor.field];
|
|
379
|
+
if (isAbsent(value)) continue;
|
|
380
|
+
const glue = descriptor.kind === 'appendPrompt' ? descriptor.prefix : PROMPT_GLUE;
|
|
381
|
+
parts.push({ field: descriptor.field, text: glue + String(value) });
|
|
382
|
+
}
|
|
383
|
+
const base = isAbsent(source.prompt) ? '' : String(source.prompt);
|
|
384
|
+
return { prompt: base + parts.map((part) => part.text).join(''), parts };
|
|
385
|
+
}
|